Compute Library
 21.02
GCMemoryRegion.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
26 #include "arm_compute/core/Error.h"
27 
28 namespace arm_compute
29 {
31  : IMemoryRegion(size), _mapping(nullptr), _ssbo_name(0)
32 {
33 }
34 
35 const GLuint &IGCMemoryRegion::gc_ssbo_name() const
36 {
37  return _ssbo_name;
38 }
39 
41 {
42  return _mapping;
43 }
44 
45 const void *IGCMemoryRegion::buffer() const
46 {
47  return _mapping;
48 }
49 
51  : IGCMemoryRegion(size)
52 {
53  ARM_COMPUTE_GL_CHECK(glGenBuffers(1, &_ssbo_name));
54  ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _ssbo_name));
55  ARM_COMPUTE_GL_CHECK(glBufferData(GL_SHADER_STORAGE_BUFFER, static_cast<GLsizeiptr>(size), nullptr, GL_STATIC_DRAW));
56  ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));
57 }
58 
60 {
61  ARM_COMPUTE_GL_CHECK(glDeleteBuffers(1, &_ssbo_name));
62 }
63 
65 {
66  return nullptr;
67 }
68 
69 void *GCBufferMemoryRegion::map(bool blocking)
70 {
71  ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
72  ARM_COMPUTE_UNUSED(blocking);
73 
74  ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _ssbo_name));
75  void *p = ARM_COMPUTE_GL_CHECK(glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, static_cast<GLsizeiptr>(size()), GL_MAP_READ_BIT | GL_MAP_WRITE_BIT));
76  _mapping = reinterpret_cast<uint8_t *>(p);
77 
78  return _mapping;
79 }
80 
82 {
83  ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
84 
85  ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _ssbo_name));
86  ARM_COMPUTE_GL_CHECK(glUnmapBuffer(GL_SHADER_STORAGE_BUFFER));
87  ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));
88  _mapping = nullptr;
89 }
90 
91 std::unique_ptr<IMemoryRegion> GCBufferMemoryRegion::extract_subregion(size_t offset, size_t size)
92 {
93  ARM_COMPUTE_UNUSED(offset, size);
94  return nullptr;
95 }
96 } // namespace arm_compute
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:846
#define ARM_COMPUTE_GL_CHECK(x)
Definition: OpenGLES.h:45
const GLuint & gc_ssbo_name() const
Returns the underlying CL buffer.
void GL_APIENTRY glDeleteBuffers(GLsizei n, const GLuint *buffers)
Definition: OpenGLES.cpp:529
void * ptr() final
Host/SVM pointer accessor.
std::unique_ptr< IMemoryRegion > extract_subregion(size_t offset, size_t size) final
Extract a sub-region from the memory.
GLboolean GL_APIENTRY glUnmapBuffer(GLenum target)
Definition: OpenGLES.cpp:646
void * map(bool blocking) final
Enqueue a map operation of the allocated buffer on the given queue.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Memory region interface.
Definition: IMemoryRegion.h:33
void * buffer() override
Returns the pointer to the allocated data.
Copyright (c) 2017-2021 Arm Limited.
void GL_APIENTRY glBindBuffer(GLenum target, GLuint buffer)
Definition: OpenGLES.cpp:490
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
void *GL_APIENTRY glMapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length, GLbitfield access)
Definition: OpenGLES.cpp:607
void GL_APIENTRY glGenBuffers(GLsizei n, GLuint *buffers)
Definition: OpenGLES.cpp:568
IGCMemoryRegion(size_t size)
Constructor.
GLES memory region interface.
GCBufferMemoryRegion(size_t size)
Constructor.
size_t size() const
Memory region size accessor.
Definition: IMemoryRegion.h:73
void GL_APIENTRY glBufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage)
Definition: OpenGLES.cpp:516
void unmap() final
Enqueue an unmap operation of the allocated buffer on the given queue.