blob: 694b34f1ec3ac4749fb5ba4dd7f53a8c578b0e70 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
2 * Copyright (c) 2017 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 */
24
25#include "arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h"
26
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"
31
32using namespace arm_compute;
33
34GCTensorAllocator::GCTensorAllocator()
35 : _gl_buffer(), _mapping(nullptr)
36{
37}
38
39uint8_t *GCTensorAllocator::data()
40{
41 return _mapping;
42}
43
44void GCTensorAllocator::allocate()
45{
46 _gl_buffer = support::cpp14::make_unique<GLBufferWrapper>();
47 ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _gl_buffer->_ssbo_name));
48 ARM_COMPUTE_GL_CHECK(glBufferData(GL_SHADER_STORAGE_BUFFER, static_cast<GLsizeiptr>(info().total_size()), nullptr, GL_STATIC_DRAW));
49 ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));
50 info().set_is_resizable(false);
51}
52
53void GCTensorAllocator::free()
54{
55 _gl_buffer.reset();
56 info().set_is_resizable(true);
57}
58
59uint8_t *GCTensorAllocator::lock()
60{
61 return map(true);
62}
63
64void GCTensorAllocator::unlock()
65{
66 unmap();
67}
68
69GLuint GCTensorAllocator::get_gl_ssbo_name() const
70{
71 return _gl_buffer->_ssbo_name;
72}
73
74uint8_t *GCTensorAllocator::map(bool blocking)
75{
76 ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
77 ARM_COMPUTE_UNUSED(blocking);
78
79 ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _gl_buffer->_ssbo_name));
80 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));
81 _mapping = reinterpret_cast<uint8_t *>(p);
82
83 return _mapping;
84}
85
86void GCTensorAllocator::unmap()
87{
88 ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
89
90 ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, _gl_buffer->_ssbo_name));
91 ARM_COMPUTE_GL_CHECK(glUnmapBuffer(GL_SHADER_STORAGE_BUFFER));
92 ARM_COMPUTE_GL_CHECK(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));
93 _mapping = nullptr;
94}