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
156 {
157 stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
158 if (functions->mapBuffer)
159 {
160 *mapPtr = ANGLE_GL_TRY(
161 context, functions->mapBuffer(gl::ToGLenum(DestBufferOperationTarget), access));
162 }
163 else
164 {
165 ASSERT(functions->mapBufferRange && access == GL_WRITE_ONLY_OES);
166 *mapPtr = ANGLE_GL_TRY(
167 context, functions->mapBufferRange(gl::ToGLenum(DestBufferOperationTarget), 0,
168 mBufferSize, GL_MAP_WRITE_BIT));
169 }
170
171 // Unbind the mapped buffer from the array buffer binding. Some drivers generate errors if
172 // any mapped buffer is bound to array buffer bindings.
173 // crbug.com/1345777
174 stateManager->bindBuffer(DestBufferOperationTarget, 0);
175 }
176
177 mIsMapped = true;
178 mMapOffset = 0;
179 mMapSize = mBufferSize;
180
181 contextGL->markWorkSubmitted();
182
183 return angle::Result::Continue;
184 }
185
mapRange(const gl::Context * context,size_t offset,size_t length,GLbitfield access,void ** mapPtr)186 angle::Result BufferGL::mapRange(const gl::Context *context,
187 size_t offset,
188 size_t length,
189 GLbitfield access,
190 void **mapPtr)
191 {
192 ContextGL *contextGL = GetImplAs<ContextGL>(context);
193 const FunctionsGL *functions = GetFunctionsGL(context);
194 StateManagerGL *stateManager = GetStateManagerGL(context);
195 const angle::FeaturesGL &features = GetFeaturesGL(context);
196
197 if (features.keepBufferShadowCopy.enabled)
198 {
199 *mapPtr = mShadowCopy.data() + offset;
200 }
201 else
202 {
203 stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
204 *mapPtr =
205 ANGLE_GL_TRY(context, functions->mapBufferRange(gl::ToGLenum(DestBufferOperationTarget),
206 offset, length, access));
207
208 // Unbind the mapped buffer from the array buffer binding. Some drivers generate errors if
209 // any mapped buffer is bound to array buffer bindings.
210 // crbug.com/1345777
211 stateManager->bindBuffer(DestBufferOperationTarget, 0);
212 }
213
214 mIsMapped = true;
215 mMapOffset = offset;
216 mMapSize = length;
217
218 contextGL->markWorkSubmitted();
219
220 return angle::Result::Continue;
221 }
222
unmap(const gl::Context * context,GLboolean * result)223 angle::Result BufferGL::unmap(const gl::Context *context, GLboolean *result)
224 {
225 ContextGL *contextGL = GetImplAs<ContextGL>(context);
226 const FunctionsGL *functions = GetFunctionsGL(context);
227 StateManagerGL *stateManager = GetStateManagerGL(context);
228 const angle::FeaturesGL &features = GetFeaturesGL(context);
229
230 ASSERT(result);
231 ASSERT(mIsMapped);
232
233 if (features.keepBufferShadowCopy.enabled)
234 {
235 stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
236 ANGLE_GL_TRY(context,
237 functions->bufferSubData(gl::ToGLenum(DestBufferOperationTarget), mMapOffset,
238 mMapSize, mShadowCopy.data() + mMapOffset));
239 *result = GL_TRUE;
240 }
241 else
242 {
243 stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
244 *result =
245 ANGLE_GL_TRY(context, functions->unmapBuffer(gl::ToGLenum(DestBufferOperationTarget)));
246 }
247
248 mIsMapped = false;
249
250 contextGL->markWorkSubmitted();
251
252 return angle::Result::Continue;
253 }
254
getIndexRange(const gl::Context * context,gl::DrawElementsType type,size_t offset,size_t count,bool primitiveRestartEnabled,gl::IndexRange * outRange)255 angle::Result BufferGL::getIndexRange(const gl::Context *context,
256 gl::DrawElementsType type,
257 size_t offset,
258 size_t count,
259 bool primitiveRestartEnabled,
260 gl::IndexRange *outRange)
261 {
262 ContextGL *contextGL = GetImplAs<ContextGL>(context);
263 const FunctionsGL *functions = GetFunctionsGL(context);
264 StateManagerGL *stateManager = GetStateManagerGL(context);
265 const angle::FeaturesGL &features = GetFeaturesGL(context);
266
267 ASSERT(!mIsMapped);
268
269 if (features.keepBufferShadowCopy.enabled)
270 {
271 *outRange = gl::ComputeIndexRange(type, mShadowCopy.data() + offset, count,
272 primitiveRestartEnabled);
273 }
274 else
275 {
276 stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
277
278 const GLuint typeBytes = gl::GetDrawElementsTypeSize(type);
279 const uint8_t *bufferData =
280 MapBufferRangeWithFallback(functions, gl::ToGLenum(DestBufferOperationTarget), offset,
281 count * typeBytes, GL_MAP_READ_BIT);
282 if (bufferData)
283 {
284 *outRange = gl::ComputeIndexRange(type, bufferData, count, primitiveRestartEnabled);
285 ANGLE_GL_TRY(context, functions->unmapBuffer(gl::ToGLenum(DestBufferOperationTarget)));
286 }
287 else
288 {
289 // Workaround the null driver not having map support.
290 *outRange = gl::IndexRange(0, 0, 1);
291 }
292 }
293
294 contextGL->markWorkSubmitted();
295
296 return angle::Result::Continue;
297 }
298
getBufferSize() const299 size_t BufferGL::getBufferSize() const
300 {
301 return mBufferSize;
302 }
303
getBufferID() const304 GLuint BufferGL::getBufferID() const
305 {
306 return mBufferID;
307 }
308 } // namespace rx
309