2 * Copyright (c) 2017-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
25 #include "arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h"
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/TensorInfo.h"
29 #include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
30 #include "support/ToolchainSupport.h"
32 using namespace arm_compute;
34 GCTensorAllocator::GCTensorAllocator(GCTensor *owner)
35 : _associated_memory_group(nullptr), _gl_buffer(), _mapping(nullptr), _owner(owner)
39 GCTensorAllocator::~GCTensorAllocator()
41 _gl_buffer = support::cpp14::make_unique<GLBufferWrapper>();
44 uint8_t *GCTensorAllocator::data()
49 void GCTensorAllocator::allocate()
51 if(_associated_memory_group == nullptr)
53 _gl_buffer = support::cpp14::make_unique<GLBufferWrapper>();
54 ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _gl_buffer->_ssbo_name));
55 ARM_COMPUTE_GL_CHECK(glBufferData(GL_SHADER_STORAGE_BUFFER, static_cast<GLsizeiptr>(info().total_size()), nullptr, GL_STATIC_DRAW));
56 ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));
60 _associated_memory_group->finalize_memory(_owner, reinterpret_cast<void **>(&_gl_buffer), info().total_size());
62 info().set_is_resizable(false);
65 void GCTensorAllocator::free()
67 if(_associated_memory_group == nullptr)
70 info().set_is_resizable(true);
74 void GCTensorAllocator::set_associated_memory_group(GCMemoryGroup *associated_memory_group)
76 ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
77 ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr);
78 ARM_COMPUTE_ERROR_ON(_gl_buffer.get() != nullptr);
79 _associated_memory_group = associated_memory_group;
82 uint8_t *GCTensorAllocator::lock()
87 void GCTensorAllocator::unlock()
92 GLuint GCTensorAllocator::get_gl_ssbo_name() const
94 return _gl_buffer->_ssbo_name;
97 uint8_t *GCTensorAllocator::map(bool blocking)
99 ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
100 ARM_COMPUTE_UNUSED(blocking);
102 ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _gl_buffer->_ssbo_name));
103 void *p = ARM_COMPUTE_GL_CHECK(glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, static_cast<GLsizeiptr>(info().total_size()), GL_MAP_READ_BIT | GL_MAP_WRITE_BIT));
104 _mapping = reinterpret_cast<uint8_t *>(p);
109 void GCTensorAllocator::unmap()
111 ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
113 ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _gl_buffer->_ssbo_name));
114 ARM_COMPUTE_GL_CHECK(glUnmapBuffer(GL_SHADER_STORAGE_BUFFER));
115 ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));