1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "WebGLContext.h"
#include "GLContext.h"
#include "WebGLBuffer.h"
#include "WebGLVertexArray.h"
#include "mozilla/CheckedInt.h"
namespace mozilla {
WebGLRefPtr<WebGLBuffer>*
WebGLContext::ValidateBufferSlot(const char* funcName, GLenum target)
{
WebGLRefPtr<WebGLBuffer>* slot = nullptr;
switch (target) {
case LOCAL_GL_ARRAY_BUFFER:
slot = &mBoundArrayBuffer;
break;
case LOCAL_GL_ELEMENT_ARRAY_BUFFER:
slot = &(mBoundVertexArray->mElementArrayBuffer);
break;
}
if (IsWebGL2()) {
switch (target) {
case LOCAL_GL_COPY_READ_BUFFER:
slot = &mBoundCopyReadBuffer;
break;
case LOCAL_GL_COPY_WRITE_BUFFER:
slot = &mBoundCopyWriteBuffer;
break;
case LOCAL_GL_PIXEL_PACK_BUFFER:
slot = &mBoundPixelPackBuffer;
break;
case LOCAL_GL_PIXEL_UNPACK_BUFFER:
slot = &mBoundPixelUnpackBuffer;
break;
case LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER:
slot = &(mBoundTransformFeedback->mGenericBufferBinding);
break;
case LOCAL_GL_UNIFORM_BUFFER:
slot = &mBoundUniformBuffer;
break;
}
}
if (!slot) {
ErrorInvalidEnum("%s: Bad `target`: 0x%04x", funcName, target);
return nullptr;
}
return slot;
}
WebGLBuffer*
WebGLContext::ValidateBufferSelection(const char* funcName, GLenum target)
{
const auto& slot = ValidateBufferSlot(funcName, target);
if (!slot)
return nullptr;
const auto& buffer = *slot;
if (!buffer) {
ErrorInvalidOperation("%s: Buffer for `target` is null.", funcName);
return nullptr;
}
if (target == LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER) {
if (mBoundTransformFeedback->IsActiveAndNotPaused()) {
ErrorInvalidOperation("%s: Cannot select TRANSFORM_FEEDBACK_BUFFER when"
" transform feedback is active and unpaused.",
funcName);
return nullptr;
}
if (buffer->IsBoundForNonTF()) {
ErrorInvalidOperation("%s: Specified WebGLBuffer is currently bound for"
" non-transform-feedback.",
funcName);
return nullptr;
}
} else {
if (buffer->IsBoundForTF()) {
ErrorInvalidOperation("%s: Specified WebGLBuffer is currently bound for"
" transform feedback.",
funcName);
return nullptr;
}
}
return buffer.get();
}
IndexedBufferBinding*
WebGLContext::ValidateIndexedBufferSlot(const char* funcName, GLenum target, GLuint index)
{
decltype(mIndexedUniformBufferBindings)* bindings;
const char* maxIndexEnum;
switch (target) {
case LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER:
bindings = &(mBoundTransformFeedback->mIndexedBindings);
maxIndexEnum = "MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS";
break;
case LOCAL_GL_UNIFORM_BUFFER:
bindings = &mIndexedUniformBufferBindings;
maxIndexEnum = "MAX_UNIFORM_BUFFER_BINDINGS";
break;
default:
ErrorInvalidEnum("%s: Bad `target`: 0x%04x", funcName, target);
return nullptr;
}
if (index >= bindings->size()) {
ErrorInvalidValue("%s: `index` >= %s.", funcName, maxIndexEnum);
return nullptr;
}
return &(*bindings)[index];
}
////////////////////////////////////////
void
WebGLContext::BindBuffer(GLenum target, WebGLBuffer* buffer)
{
const char funcName[] = "bindBuffer";
if (IsContextLost())
return;
if (buffer && !ValidateObject(funcName, *buffer))
return;
const auto& slot = ValidateBufferSlot(funcName, target);
if (!slot)
return;
if (buffer && !buffer->ValidateCanBindToTarget(funcName, target))
return;
gl->MakeCurrent();
gl->fBindBuffer(target, buffer ? buffer->mGLName : 0);
WebGLBuffer::SetSlot(target, buffer, slot);
if (buffer) {
buffer->SetContentAfterBind(target);
}
switch (target) {
case LOCAL_GL_PIXEL_PACK_BUFFER:
case LOCAL_GL_PIXEL_UNPACK_BUFFER:
gl->fBindBuffer(target, 0);
break;
}
}
////////////////////////////////////////
bool
WebGLContext::ValidateIndexedBufferBinding(const char* funcName, GLenum target,
GLuint index,
WebGLRefPtr<WebGLBuffer>** const out_genericBinding,
IndexedBufferBinding** const out_indexedBinding)
{
*out_genericBinding = ValidateBufferSlot(funcName, target);
if (!*out_genericBinding)
return false;
*out_indexedBinding = ValidateIndexedBufferSlot(funcName, target, index);
if (!*out_indexedBinding)
return false;
if (target == LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER &&
mBoundTransformFeedback->mIsActive)
{
ErrorInvalidOperation("%s: Cannot update indexed buffer bindings on active"
" transform feedback objects.",
funcName);
return false;
}
return true;
}
void
WebGLContext::BindBufferBase(GLenum target, GLuint index, WebGLBuffer* buffer)
{
const char funcName[] = "bindBufferBase";
if (IsContextLost())
return;
if (buffer && !ValidateObject(funcName, *buffer))
return;
WebGLRefPtr<WebGLBuffer>* genericBinding;
IndexedBufferBinding* indexedBinding;
if (!ValidateIndexedBufferBinding(funcName, target, index, &genericBinding,
&indexedBinding))
{
return;
}
if (buffer && !buffer->ValidateCanBindToTarget(funcName, target))
return;
////
gl->MakeCurrent();
gl->fBindBufferBase(target, index, buffer ? buffer->mGLName : 0);
////
WebGLBuffer::SetSlot(target, buffer, genericBinding);
WebGLBuffer::SetSlot(target, buffer, &indexedBinding->mBufferBinding);
indexedBinding->mRangeStart = 0;
indexedBinding->mRangeSize = 0;
if (buffer) {
buffer->SetContentAfterBind(target);
}
}
void
WebGLContext::BindBufferRange(GLenum target, GLuint index, WebGLBuffer* buffer,
WebGLintptr offset, WebGLsizeiptr size)
{
const char funcName[] = "bindBufferRange";
if (IsContextLost())
return;
if (buffer && !ValidateObject(funcName, *buffer))
return;
if (!ValidateNonNegative(funcName, "offset", offset) ||
!ValidateNonNegative(funcName, "size", size))
{
return;
}
WebGLRefPtr<WebGLBuffer>* genericBinding;
IndexedBufferBinding* indexedBinding;
if (!ValidateIndexedBufferBinding(funcName, target, index, &genericBinding,
&indexedBinding))
{
return;
}
if (buffer && !buffer->ValidateCanBindToTarget(funcName, target))
return;
if (buffer && !size) {
ErrorInvalidValue("%s: size must be non-zero for non-null buffer.", funcName);
return;
}
////
gl->MakeCurrent();
switch (target) {
case LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER:
if (offset % 4 != 0 || size % 4 != 0) {
ErrorInvalidValue("%s: For %s, `offset` and `size` must be multiples of 4.",
funcName, "TRANSFORM_FEEDBACK_BUFFER");
return;
}
break;
case LOCAL_GL_UNIFORM_BUFFER:
{
GLuint offsetAlignment = 0;
gl->GetUIntegerv(LOCAL_GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &offsetAlignment);
if (offset % offsetAlignment != 0) {
ErrorInvalidValue("%s: For %s, `offset` must be a multiple of %s.",
funcName, "UNIFORM_BUFFER",
"UNIFORM_BUFFER_OFFSET_ALIGNMENT");
return;
}
}
break;
}
////
#ifdef XP_MACOSX
if (buffer && buffer->Content() == WebGLBuffer::Kind::Undefined &&
gl->WorkAroundDriverBugs())
{
// BindBufferRange will fail if the buffer's contents is undefined.
// Bind so driver initializes the buffer.
gl->fBindBuffer(target, buffer->mGLName);
}
#endif
gl->fBindBufferRange(target, index, buffer ? buffer->mGLName : 0, offset, size);
////
WebGLBuffer::SetSlot(target, buffer, genericBinding);
WebGLBuffer::SetSlot(target, buffer, &indexedBinding->mBufferBinding);
indexedBinding->mRangeStart = offset;
indexedBinding->mRangeSize = size;
if (buffer) {
buffer->SetContentAfterBind(target);
}
}
////////////////////////////////////////
void
WebGLContext::BufferDataImpl(GLenum target, size_t dataLen, const uint8_t* data,
GLenum usage)
{
const char funcName[] = "bufferData";
const auto& buffer = ValidateBufferSelection(funcName, target);
if (!buffer)
return;
buffer->BufferData(target, dataLen, data, usage);
}
////
void
WebGLContext::BufferData(GLenum target, WebGLsizeiptr size, GLenum usage)
{
const char funcName[] = "bufferData";
if (IsContextLost())
return;
if (!ValidateNonNegative(funcName, "size", size))
return;
////
const auto checkedSize = CheckedInt<size_t>(size);
if (!checkedSize.isValid())
return ErrorOutOfMemory("%s: Size too large for platform.", funcName);
#if defined(XP_MACOSX)
if (gl->WorkAroundDriverBugs() && size > 1200000000) {
return ErrorOutOfMemory("Allocations larger than 1200000000 fail on MacOS.");
}
#endif
const UniqueBuffer zeroBuffer(calloc(size, 1));
if (!zeroBuffer)
return ErrorOutOfMemory("%s: Failed to allocate zeros.", funcName);
BufferDataImpl(target, size_t(size), (const uint8_t*)zeroBuffer.get(), usage);
}
void
WebGLContext::BufferData(GLenum target, const dom::Nullable<dom::ArrayBuffer>& maybeSrc,
GLenum usage)
{
if (IsContextLost())
return;
if (!ValidateNonNull("bufferData", maybeSrc))
return;
const auto& src = maybeSrc.Value();
src.ComputeLengthAndData();
BufferDataImpl(target, src.LengthAllowShared(), src.DataAllowShared(), usage);
}
void
WebGLContext::BufferData(GLenum target, const dom::ArrayBufferView& src, GLenum usage,
GLuint srcElemOffset, GLuint srcElemCountOverride)
{
const char funcName[] = "bufferData";
if (IsContextLost())
return;
uint8_t* bytes;
size_t byteLen;
if (!ValidateArrayBufferView(funcName, src, srcElemOffset, srcElemCountOverride,
&bytes, &byteLen))
{
return;
}
BufferDataImpl(target, byteLen, bytes, usage);
}
////////////////////////////////////////
void
WebGLContext::BufferSubDataImpl(GLenum target, WebGLsizeiptr dstByteOffset,
size_t dataLen, const uint8_t* data)
{
const char funcName[] = "bufferSubData";
if (!ValidateNonNegative(funcName, "byteOffset", dstByteOffset))
return;
const auto& buffer = ValidateBufferSelection(funcName, target);
if (!buffer)
return;
if (!buffer->ValidateRange(funcName, dstByteOffset, dataLen))
return;
if (!CheckedInt<GLintptr>(dataLen).isValid()) {
ErrorOutOfMemory("%s: Size too large.", funcName);
return;
}
const GLintptr glDataLen(dataLen);
////
MakeContextCurrent();
const ScopedLazyBind lazyBind(gl, target, buffer);
// Warning: Possibly shared memory. See bug 1225033.
gl->fBufferSubData(target, dstByteOffset, glDataLen, data);
// Warning: Possibly shared memory. See bug 1225033.
buffer->ElementArrayCacheBufferSubData(dstByteOffset, data, size_t(glDataLen));
}
void
WebGLContext::BufferSubData(GLenum target, WebGLsizeiptr dstByteOffset,
const dom::ArrayBuffer& src)
{
if (IsContextLost())
return;
src.ComputeLengthAndData();
BufferSubDataImpl(target, dstByteOffset, src.LengthAllowShared(),
src.DataAllowShared());
}
void
WebGLContext::BufferSubData(GLenum target, WebGLsizeiptr dstByteOffset,
const dom::ArrayBufferView& src, GLuint srcElemOffset,
GLuint srcElemCountOverride)
{
const char funcName[] = "bufferSubData";
if (IsContextLost())
return;
uint8_t* bytes;
size_t byteLen;
if (!ValidateArrayBufferView(funcName, src, srcElemOffset, srcElemCountOverride,
&bytes, &byteLen))
{
return;
}
BufferSubDataImpl(target, dstByteOffset, byteLen, bytes);
}
////////////////////////////////////////
already_AddRefed<WebGLBuffer>
WebGLContext::CreateBuffer()
{
if (IsContextLost())
return nullptr;
GLuint buf = 0;
MakeContextCurrent();
gl->fGenBuffers(1, &buf);
RefPtr<WebGLBuffer> globj = new WebGLBuffer(this, buf);
return globj.forget();
}
void
WebGLContext::DeleteBuffer(WebGLBuffer* buffer)
{
if (!ValidateDeleteObject("deleteBuffer", buffer))
return;
////
const auto fnClearIfBuffer = [&](GLenum target, WebGLRefPtr<WebGLBuffer>& bindPoint) {
if (bindPoint == buffer) {
WebGLBuffer::SetSlot(target, nullptr, &bindPoint);
}
};
fnClearIfBuffer(0, mBoundArrayBuffer);
fnClearIfBuffer(0, mBoundVertexArray->mElementArrayBuffer);
for (auto& cur : mBoundVertexArray->mAttribs) {
fnClearIfBuffer(0, cur.mBuf);
}
// WebGL binding points
if (IsWebGL2()) {
fnClearIfBuffer(0, mBoundCopyReadBuffer);
fnClearIfBuffer(0, mBoundCopyWriteBuffer);
fnClearIfBuffer(0, mBoundPixelPackBuffer);
fnClearIfBuffer(0, mBoundPixelUnpackBuffer);
fnClearIfBuffer(0, mBoundUniformBuffer);
fnClearIfBuffer(LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER,
mBoundTransformFeedback->mGenericBufferBinding);
if (!mBoundTransformFeedback->mIsActive) {
for (auto& binding : mBoundTransformFeedback->mIndexedBindings) {
fnClearIfBuffer(LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER,
binding.mBufferBinding);
}
}
for (auto& binding : mIndexedUniformBufferBindings) {
fnClearIfBuffer(0, binding.mBufferBinding);
}
}
////
buffer->RequestDelete();
}
bool
WebGLContext::IsBuffer(WebGLBuffer* buffer)
{
if (!ValidateIsObject("isBuffer", buffer))
return false;
MakeContextCurrent();
return gl->fIsBuffer(buffer->mGLName);
}
} // namespace mozilla
|