1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // BufferGL.cpp: Implements the class methods for BufferGL.
8 
9 #include "libANGLE/renderer/gl/BufferGL.h"
10 
11 #include "common/debug.h"
12 #include "common/utilities.h"
13 #include "libANGLE/Context.h"
14 #include "libANGLE/angletypes.h"
15 #include "libANGLE/formatutils.h"
16 #include "libANGLE/renderer/gl/ContextGL.h"
17 #include "libANGLE/renderer/gl/FunctionsGL.h"
18 #include "libANGLE/renderer/gl/StateManagerGL.h"
19 #include "libANGLE/renderer/gl/renderergl_utils.h"
20 
21 namespace rx
22 {
23 
24 // Use the GL_COPY_READ_BUFFER binding when two buffers need to be bound simultaneously.
25 // GL_ELEMENT_ARRAY_BUFFER is supported on more versions but can modify the state of the currently
26 // bound VAO.  Two simultaneous buffer bindings are only needed for glCopyBufferSubData which also
27 // adds the GL_COPY_READ_BUFFER binding.
28 static constexpr gl::BufferBinding SourceBufferOperationTarget = gl::BufferBinding::CopyRead;
29 
30 // Use the GL_ELEMENT_ARRAY_BUFFER binding for most operations since it's available on all
31 // supported GL versions and doesn't affect any current state when it changes.
32 static constexpr gl::BufferBinding DestBufferOperationTarget = gl::BufferBinding::Array;
33 
BufferGL(const gl::BufferState & state,GLuint buffer)34 BufferGL::BufferGL(const gl::BufferState &state, GLuint buffer)
35     : BufferImpl(state),
36       mIsMapped(false),
37       mMapOffset(0),
38       mMapSize(0),
39       mShadowCopy(),
40       mBufferSize(0),
41       mBufferID(buffer)
42 {}
43 
~BufferGL()44 BufferGL::~BufferGL()
45 {
46     ASSERT(mBufferID == 0);
47 }
48 
destroy(const gl::Context * context)49 void BufferGL::destroy(const gl::Context *context)
50 {
51     StateManagerGL *stateManager = GetStateManagerGL(context);
52     stateManager->deleteBuffer(mBufferID);
53     mBufferID = 0;
54 }
55 
setData(const gl::Context * context,gl::BufferBinding target,const void * data,size_t size,gl::BufferUsage usage)56 angle::Result BufferGL::setData(const gl::Context *context,
57                                 gl::BufferBinding target,
58                                 const void *data,
59                                 size_t size,
60                                 gl::BufferUsage usage)
61 {
62     ContextGL *contextGL              = GetImplAs<ContextGL>(context);
63     const FunctionsGL *functions      = GetFunctionsGL(context);
64     StateManagerGL *stateManager      = GetStateManagerGL(context);
65     const angle::FeaturesGL &features = GetFeaturesGL(context);
66 
67     stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
68     ANGLE_GL_TRY(context, functions->bufferData(gl::ToGLenum(DestBufferOperationTarget), size, data,
69                                                 ToGLenum(usage)));
70 
71     if (features.keepBufferShadowCopy.enabled)
72     {
73         ANGLE_CHECK_GL_ALLOC(contextGL, mShadowCopy.resize(size));
74 
75         if (size > 0 && data != nullptr)
76         {
77             memcpy(mShadowCopy.data(), data, size);
78         }
79     }
80 
81     mBufferSize = size;
82 
83     contextGL->markWorkSubmitted();
84 
85     return angle::Result::Continue;
86 }
87 
setSubData(const gl::Context * context,gl::BufferBinding target,const void * data,size_t size,size_t offset)88 angle::Result BufferGL::setSubData(const gl::Context *context,
89                                    gl::BufferBinding target,
90                                    const void *data,
91                                    size_t size,
92                                    size_t offset)
93 {
94     ContextGL *contextGL              = GetImplAs<ContextGL>(context);
95     const FunctionsGL *functions      = GetFunctionsGL(context);
96     StateManagerGL *stateManager      = GetStateManagerGL(context);
97     const angle::FeaturesGL &features = GetFeaturesGL(context);
98 
99     stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
100     ANGLE_GL_TRY(context, functions->bufferSubData(gl::ToGLenum(DestBufferOperationTarget), offset,
101                                                    size, data));
102 
103     if (features.keepBufferShadowCopy.enabled && size > 0)
104     {
105         memcpy(mShadowCopy.data() + offset, data, size);
106     }
107 
108     contextGL->markWorkSubmitted();
109 
110     return angle::Result::Continue;
111 }
112 
copySubData(const gl::Context * context,BufferImpl * source,GLintptr sourceOffset,GLintptr destOffset,GLsizeiptr size)113 angle::Result BufferGL::copySubData(const gl::Context *context,
114                                     BufferImpl *source,
115                                     GLintptr sourceOffset,
116                                     GLintptr destOffset,
117                                     GLsizeiptr size)
118 {
119     ContextGL *contextGL              = GetImplAs<ContextGL>(context);
120     const FunctionsGL *functions      = GetFunctionsGL(context);
121     StateManagerGL *stateManager      = GetStateManagerGL(context);
122     const angle::FeaturesGL &features = GetFeaturesGL(context);
123 
124     BufferGL *sourceGL = GetAs<BufferGL>(source);
125 
126     stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
127     stateManager->bindBuffer(SourceBufferOperationTarget, sourceGL->getBufferID());
128 
129     ANGLE_GL_TRY(context, functions->copyBufferSubData(gl::ToGLenum(SourceBufferOperationTarget),
130                                                        gl::ToGLenum(DestBufferOperationTarget),
131                                                        sourceOffset, destOffset, size));
132 
133     if (features.keepBufferShadowCopy.enabled && size > 0)
134     {
135         ASSERT(sourceGL->mShadowCopy.size() >= static_cast<size_t>(sourceOffset + size));
136         memcpy(mShadowCopy.data() + destOffset, sourceGL->mShadowCopy.data() + sourceOffset, size);
137     }
138 
139     contextGL->markWorkSubmitted();
140 
141     return angle::Result::Continue;
142 }
143 
map(const gl::Context * context,GLenum access,void ** mapPtr)144 angle::Result BufferGL::map(const gl::Context *context, GLenum access, void **mapPtr)
145 {
146     ContextGL *contextGL              = GetImplAs<ContextGL>(context);
147     const FunctionsGL *functions      = GetFunctionsGL(context);
148     StateManagerGL *stateManager      = GetStateManagerGL(context);
149     const angle::FeaturesGL &features = GetFeaturesGL(context);
150 
151     if (features.keepBufferShadowCopy.enabled)
152     {
153         *mapPtr = mShadowCopy.data();
154     }
155     else if (functions->mapBuffer)
156     {
157         stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
158         *mapPtr = ANGLE_GL_TRY(
159             context, functions->mapBuffer(gl::ToGLenum(DestBufferOperationTarget), access));
160     }
161     else
162     {
163         ASSERT(functions->mapBufferRange && access == GL_WRITE_ONLY_OES);
164         stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
165         *mapPtr =
166             ANGLE_GL_TRY(context, functions->mapBufferRange(gl::ToGLenum(DestBufferOperationTarget),
167                                                             0, mBufferSize, GL_MAP_WRITE_BIT));
168     }
169 
170     mIsMapped  = true;
171     mMapOffset = 0;
172     mMapSize   = mBufferSize;
173 
174     contextGL->markWorkSubmitted();
175 
176     return angle::Result::Continue;
177 }
178 
mapRange(const gl::Context * context,size_t offset,size_t length,GLbitfield access,void ** mapPtr)179 angle::Result BufferGL::mapRange(const gl::Context *context,
180                                  size_t offset,
181                                  size_t length,
182                                  GLbitfield access,
183                                  void **mapPtr)
184 {
185     ContextGL *contextGL              = GetImplAs<ContextGL>(context);
186     const FunctionsGL *functions      = GetFunctionsGL(context);
187     StateManagerGL *stateManager      = GetStateManagerGL(context);
188     const angle::FeaturesGL &features = GetFeaturesGL(context);
189 
190     if (features.keepBufferShadowCopy.enabled)
191     {
192         *mapPtr = mShadowCopy.data() + offset;
193     }
194     else
195     {
196         stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
197         *mapPtr =
198             ANGLE_GL_TRY(context, functions->mapBufferRange(gl::ToGLenum(DestBufferOperationTarget),
199                                                             offset, length, access));
200     }
201 
202     mIsMapped  = true;
203     mMapOffset = offset;
204     mMapSize   = length;
205 
206     contextGL->markWorkSubmitted();
207 
208     return angle::Result::Continue;
209 }
210 
unmap(const gl::Context * context,GLboolean * result)211 angle::Result BufferGL::unmap(const gl::Context *context, GLboolean *result)
212 {
213     ContextGL *contextGL              = GetImplAs<ContextGL>(context);
214     const FunctionsGL *functions      = GetFunctionsGL(context);
215     StateManagerGL *stateManager      = GetStateManagerGL(context);
216     const angle::FeaturesGL &features = GetFeaturesGL(context);
217 
218     ASSERT(result);
219     ASSERT(mIsMapped);
220 
221     if (features.keepBufferShadowCopy.enabled)
222     {
223         stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
224         ANGLE_GL_TRY(context,
225                      functions->bufferSubData(gl::ToGLenum(DestBufferOperationTarget), mMapOffset,
226                                               mMapSize, mShadowCopy.data() + mMapOffset));
227         *result = GL_TRUE;
228     }
229     else
230     {
231         stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
232         *result =
233             ANGLE_GL_TRY(context, functions->unmapBuffer(gl::ToGLenum(DestBufferOperationTarget)));
234     }
235 
236     mIsMapped = false;
237 
238     contextGL->markWorkSubmitted();
239 
240     return angle::Result::Continue;
241 }
242 
getIndexRange(const gl::Context * context,gl::DrawElementsType type,size_t offset,size_t count,bool primitiveRestartEnabled,gl::IndexRange * outRange)243 angle::Result BufferGL::getIndexRange(const gl::Context *context,
244                                       gl::DrawElementsType type,
245                                       size_t offset,
246                                       size_t count,
247                                       bool primitiveRestartEnabled,
248                                       gl::IndexRange *outRange)
249 {
250     ContextGL *contextGL              = GetImplAs<ContextGL>(context);
251     const FunctionsGL *functions      = GetFunctionsGL(context);
252     StateManagerGL *stateManager      = GetStateManagerGL(context);
253     const angle::FeaturesGL &features = GetFeaturesGL(context);
254 
255     ASSERT(!mIsMapped);
256 
257     if (features.keepBufferShadowCopy.enabled)
258     {
259         *outRange = gl::ComputeIndexRange(type, mShadowCopy.data() + offset, count,
260                                           primitiveRestartEnabled);
261     }
262     else
263     {
264         stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
265 
266         const GLuint typeBytes = gl::GetDrawElementsTypeSize(type);
267         const uint8_t *bufferData =
268             MapBufferRangeWithFallback(functions, gl::ToGLenum(DestBufferOperationTarget), offset,
269                                        count * typeBytes, GL_MAP_READ_BIT);
270         if (bufferData)
271         {
272             *outRange = gl::ComputeIndexRange(type, bufferData, count, primitiveRestartEnabled);
273             ANGLE_GL_TRY(context, functions->unmapBuffer(gl::ToGLenum(DestBufferOperationTarget)));
274         }
275         else
276         {
277             // Workaround the null driver not having map support.
278             *outRange = gl::IndexRange(0, 0, 1);
279         }
280     }
281 
282     contextGL->markWorkSubmitted();
283 
284     return angle::Result::Continue;
285 }
286 
getBufferID() const287 GLuint BufferGL::getBufferID() const
288 {
289     return mBufferID;
290 }
291 }  // namespace rx
292