summaryrefslogtreecommitdiffstats
path: root/dom/canvas/WebGLBuffer.cpp
blob: 391a92055ea76199e9c31a412b34678337b26781 (plain)
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
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "WebGLBuffer.h"

#include "GLContext.h"
#include "mozilla/dom/WebGLRenderingContextBinding.h"
#include "WebGLContext.h"

namespace mozilla {

WebGLBuffer::WebGLBuffer(WebGLContext* webgl, GLuint buf)
    : WebGLContextBoundObject(webgl), mGLName(buf) {}

WebGLBuffer::~WebGLBuffer() {
  mByteLength = 0;
  mFetchInvalidator.InvalidateCaches();

  mIndexCache.reset();
  mIndexRanges.clear();

  if (!mContext) return;
  mContext->gl->fDeleteBuffers(1, &mGLName);
}

void WebGLBuffer::SetContentAfterBind(GLenum target) {
  if (mContent != Kind::Undefined) return;

  switch (target) {
    case LOCAL_GL_ELEMENT_ARRAY_BUFFER:
      mContent = Kind::ElementArray;
      break;

    case LOCAL_GL_ARRAY_BUFFER:
    case LOCAL_GL_PIXEL_PACK_BUFFER:
    case LOCAL_GL_PIXEL_UNPACK_BUFFER:
    case LOCAL_GL_UNIFORM_BUFFER:
    case LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER:
    case LOCAL_GL_COPY_READ_BUFFER:
    case LOCAL_GL_COPY_WRITE_BUFFER:
      mContent = Kind::OtherData;
      break;

    default:
      MOZ_CRASH("GFX: invalid target");
  }
}

////////////////////////////////////////

static bool ValidateBufferUsageEnum(WebGLContext* webgl, GLenum usage) {
  switch (usage) {
    case LOCAL_GL_STREAM_DRAW:
    case LOCAL_GL_STATIC_DRAW:
    case LOCAL_GL_DYNAMIC_DRAW:
      return true;

    case LOCAL_GL_DYNAMIC_COPY:
    case LOCAL_GL_DYNAMIC_READ:
    case LOCAL_GL_STATIC_COPY:
    case LOCAL_GL_STATIC_READ:
    case LOCAL_GL_STREAM_COPY:
    case LOCAL_GL_STREAM_READ:
      if (MOZ_LIKELY(webgl->IsWebGL2())) return true;
      break;

    default:
      break;
  }

  webgl->ErrorInvalidEnumInfo("usage", usage);
  return false;
}

void WebGLBuffer::BufferData(const GLenum target, const uint64_t size,
                             const void* const maybeData, const GLenum usage) {
  // The driver knows only GLsizeiptr, which is int32_t on 32bit!
  bool sizeValid = CheckedInt<GLsizeiptr>(size).isValid();

  if (mContext->gl->WorkAroundDriverBugs()) {
    // Bug 790879
#if defined(XP_MACOSX) || defined(MOZ_WIDGET_GTK)
    sizeValid &= CheckedInt<int32_t>(size).isValid();
#endif

    // Bug 1610383
    if (mContext->gl->IsANGLE()) {
      // While ANGLE seems to support up to `unsigned int`, UINT32_MAX-4 causes
      // GL_OUT_OF_MEMORY in glFlush??
      sizeValid &= CheckedInt<int32_t>(size).isValid();
    }
  }

  if (!sizeValid) {
    mContext->ErrorOutOfMemory("Size not valid for platform: %" PRIu64, size);
    return;
  }

  // -

  if (!ValidateBufferUsageEnum(mContext, usage)) return;

  const void* uploadData = maybeData;
  UniqueBuffer maybeCalloc;
  if (!uploadData) {
    maybeCalloc = UniqueBuffer::Take(calloc(1, AssertedCast<size_t>(size)));
    if (!maybeCalloc) {
      mContext->ErrorOutOfMemory("Failed to alloc zeros.");
      return;
    }
    uploadData = maybeCalloc.get();
  }
  MOZ_ASSERT(uploadData);

  UniqueBuffer newIndexCache;
  const bool needsIndexCache = mContext->mNeedsIndexValidation ||
                               mContext->mMaybeNeedsLegacyVertexAttrib0Handling;
  if (target == LOCAL_GL_ELEMENT_ARRAY_BUFFER && needsIndexCache) {
    newIndexCache = UniqueBuffer::Take(malloc(AssertedCast<size_t>(size)));
    if (!newIndexCache) {
      mContext->ErrorOutOfMemory("Failed to alloc index cache.");
      return;
    }
    // memcpy out of SharedArrayBuffers can be racey, and should generally use
    // memcpySafeWhenRacy. But it's safe here:
    // * We only memcpy in one place.
    // * We only read out of the single copy, and only after copying.
    // * If we get data value corruption from racing read-during-write, that's
    // fine.
    memcpy(newIndexCache.get(), uploadData, size);
    uploadData = newIndexCache.get();
  }

  const auto& gl = mContext->gl;
  const ScopedLazyBind lazyBind(gl, target, this);

  const bool sizeChanges = (size != ByteLength());
  if (sizeChanges) {
    gl::GLContext::LocalErrorScope errorScope(*gl);
    gl->fBufferData(target, size, uploadData, usage);
    const auto error = errorScope.GetError();

    if (error) {
      MOZ_ASSERT(error == LOCAL_GL_OUT_OF_MEMORY);
      mContext->ErrorOutOfMemory("Error from driver: 0x%04x", error);

      // Truncate
      mByteLength = 0;
      mFetchInvalidator.InvalidateCaches();
      mIndexCache.reset();
      return;
    }
  } else {
    gl->fBufferData(target, size, uploadData, usage);
  }

  mContext->OnDataAllocCall();

  mUsage = usage;
  mByteLength = size;
  mFetchInvalidator.InvalidateCaches();
  mIndexCache = std::move(newIndexCache);

  if (mIndexCache) {
    if (!mIndexRanges.empty()) {
      mContext->GeneratePerfWarning("[%p] Invalidating %u ranges.", this,
                                    uint32_t(mIndexRanges.size()));
      mIndexRanges.clear();
    }
  }

  ResetLastUpdateFenceId();
}

void WebGLBuffer::BufferSubData(GLenum target, uint64_t rawDstByteOffset,
                                uint64_t rawDataLen, const void* data,
                                bool unsynchronized) const {
  if (!ValidateRange(rawDstByteOffset, rawDataLen)) return;

  const CheckedInt<GLintptr> dstByteOffset = rawDstByteOffset;
  const CheckedInt<GLsizeiptr> dataLen = rawDataLen;
  if (!dstByteOffset.isValid() || !dataLen.isValid()) {
    return mContext->ErrorOutOfMemory("offset or size too large for platform.");
  }

  ////

  if (!rawDataLen) return;  // With validation successful, nothing else to do.

  const void* uploadData = data;
  if (mIndexCache) {
    auto* const cachedDataBegin =
        (uint8_t*)mIndexCache.get() + rawDstByteOffset;
    memcpy(cachedDataBegin, data, dataLen.value());
    uploadData = cachedDataBegin;

    InvalidateCacheRange(dstByteOffset.value(), dataLen.value());
  }

  ////

  const auto& gl = mContext->gl;
  const ScopedLazyBind lazyBind(gl, target, this);

  void* mapping = nullptr;
  // Repeated calls to glMapBufferRange is slow on ANGLE, so fall back to the
  // glBufferSubData path. See bug 1827047.
  if (unsynchronized && gl->IsSupported(gl::GLFeature::map_buffer_range) &&
      !gl->IsANGLE()) {
    GLbitfield access = LOCAL_GL_MAP_WRITE_BIT |
                        LOCAL_GL_MAP_UNSYNCHRONIZED_BIT |
                        LOCAL_GL_MAP_INVALIDATE_RANGE_BIT;
    // On some devices there are known performance issues with the combination
    // of GL_MAP_UNSYNCHRONIZED_BIT and GL_MAP_INVALIDATE_RANGE_BIT, so omit the
    // latter.
    if (gl->Renderer() == gl::GLRenderer::MaliT ||
        gl->Vendor() == gl::GLVendor::Qualcomm) {
      access &= ~LOCAL_GL_MAP_INVALIDATE_RANGE_BIT;
    }
    mapping = gl->fMapBufferRange(target, dstByteOffset.value(),
                                  dataLen.value(), access);
  }

  if (mapping) {
    memcpy(mapping, uploadData, dataLen.value());
    gl->fUnmapBuffer(target);
  } else {
    gl->fBufferSubData(target, dstByteOffset.value(), dataLen.value(),
                       uploadData);
  }

  ResetLastUpdateFenceId();
}

bool WebGLBuffer::ValidateRange(size_t byteOffset, size_t byteLen) const {
  auto availLength = mByteLength;
  if (byteOffset > availLength) {
    mContext->ErrorInvalidValue("Offset passes the end of the buffer.");
    return false;
  }
  availLength -= byteOffset;

  if (byteLen > availLength) {
    mContext->ErrorInvalidValue("Offset+size passes the end of the buffer.");
    return false;
  }

  return true;
}

////////////////////////////////////////

static uint8_t IndexByteSizeByType(GLenum type) {
  switch (type) {
    case LOCAL_GL_UNSIGNED_BYTE:
      return 1;
    case LOCAL_GL_UNSIGNED_SHORT:
      return 2;
    case LOCAL_GL_UNSIGNED_INT:
      return 4;
    default:
      MOZ_CRASH();
  }
}

void WebGLBuffer::InvalidateCacheRange(uint64_t byteOffset,
                                       uint64_t byteLength) const {
  MOZ_ASSERT(mIndexCache);

  std::vector<IndexRange> invalids;
  const uint64_t updateBegin = byteOffset;
  const uint64_t updateEnd = updateBegin + byteLength;
  for (const auto& cur : mIndexRanges) {
    const auto& range = cur.first;
    const auto& indexByteSize = IndexByteSizeByType(range.type);
    const auto rangeBegin = range.byteOffset * indexByteSize;
    const auto rangeEnd =
        rangeBegin + uint64_t(range.indexCount) * indexByteSize;
    if (rangeBegin >= updateEnd || rangeEnd <= updateBegin) continue;
    invalids.push_back(range);
  }

  if (!invalids.empty()) {
    mContext->GeneratePerfWarning("[%p] Invalidating %u/%u ranges.", this,
                                  uint32_t(invalids.size()),
                                  uint32_t(mIndexRanges.size()));

    for (const auto& cur : invalids) {
      mIndexRanges.erase(cur);
    }
  }
}

size_t WebGLBuffer::SizeOfIncludingThis(
    mozilla::MallocSizeOf mallocSizeOf) const {
  size_t size = mallocSizeOf(this);
  if (mIndexCache) {
    size += mByteLength;
  }
  return size;
}

template <typename T>
static Maybe<uint32_t> MaxForRange(const void* const start,
                                   const uint32_t count,
                                   const Maybe<uint32_t>& untypedIgnoredVal) {
  const Maybe<T> ignoredVal =
      (untypedIgnoredVal ? Some(T(untypedIgnoredVal.value())) : Nothing());
  Maybe<uint32_t> maxVal;

  auto itr = (const T*)start;
  const auto end = itr + count;

  for (; itr != end; ++itr) {
    const auto& val = *itr;
    if (ignoredVal && val == ignoredVal.value()) continue;

    if (maxVal && val <= maxVal.value()) continue;

    maxVal = Some(val);
  }

  return maxVal;
}

static const uint32_t kMaxIndexRanges = 256;

Maybe<uint32_t> WebGLBuffer::GetIndexedFetchMaxVert(
    const GLenum type, const uint64_t byteOffset,
    const uint32_t indexCount) const {
  if (!mIndexCache) return Nothing();

  const IndexRange range = {type, byteOffset, indexCount};
  auto res = mIndexRanges.insert({range, Nothing()});
  if (mIndexRanges.size() > kMaxIndexRanges) {
    mContext->GeneratePerfWarning(
        "[%p] Clearing mIndexRanges after exceeding %u.", this,
        kMaxIndexRanges);
    mIndexRanges.clear();
    res = mIndexRanges.insert({range, Nothing()});
  }

  const auto& itr = res.first;
  const auto& didInsert = res.second;

  auto& maxFetchIndex = itr->second;
  if (didInsert) {
    const auto& data = mIndexCache.get();

    const auto start = (const uint8_t*)data + byteOffset;

    Maybe<uint32_t> ignoredVal;
    if (mContext->IsWebGL2()) {
      ignoredVal = Some(UINT32_MAX);
    }

    switch (type) {
      case LOCAL_GL_UNSIGNED_BYTE:
        maxFetchIndex = MaxForRange<uint8_t>(start, indexCount, ignoredVal);
        break;
      case LOCAL_GL_UNSIGNED_SHORT:
        maxFetchIndex = MaxForRange<uint16_t>(start, indexCount, ignoredVal);
        break;
      case LOCAL_GL_UNSIGNED_INT:
        maxFetchIndex = MaxForRange<uint32_t>(start, indexCount, ignoredVal);
        break;
      default:
        MOZ_CRASH();
    }
    const auto displayMaxVertIndex =
        maxFetchIndex ? int64_t(maxFetchIndex.value()) : -1;
    mContext->GeneratePerfWarning("[%p] New range #%u: (0x%04x, %" PRIu64
                                  ", %u):"
                                  " %" PRIi64,
                                  this, uint32_t(mIndexRanges.size()),
                                  range.type, range.byteOffset,
                                  range.indexCount, displayMaxVertIndex);
  }

  return maxFetchIndex;
}

////

bool WebGLBuffer::ValidateCanBindToTarget(GLenum target) {
  /* https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.1
   *
   * In the WebGL 2 API, buffers have their WebGL buffer type
   * initially set to undefined. Calling bindBuffer, bindBufferRange
   * or bindBufferBase with the target argument set to any buffer
   * binding point except COPY_READ_BUFFER or COPY_WRITE_BUFFER will
   * then set the WebGL buffer type of the buffer being bound
   * according to the table above.
   *
   * Any call to one of these functions which attempts to bind a
   * WebGLBuffer that has the element array WebGL buffer type to a
   * binding point that falls under other data, or bind a
   * WebGLBuffer which has the other data WebGL buffer type to
   * ELEMENT_ARRAY_BUFFER will generate an INVALID_OPERATION error,
   * and the state of the binding point will remain untouched.
   */

  if (mContent == WebGLBuffer::Kind::Undefined) return true;

  switch (target) {
    case LOCAL_GL_COPY_READ_BUFFER:
    case LOCAL_GL_COPY_WRITE_BUFFER:
      return true;

    case LOCAL_GL_ELEMENT_ARRAY_BUFFER:
      if (mContent == WebGLBuffer::Kind::ElementArray) return true;
      break;

    case LOCAL_GL_ARRAY_BUFFER:
    case LOCAL_GL_PIXEL_PACK_BUFFER:
    case LOCAL_GL_PIXEL_UNPACK_BUFFER:
    case LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER:
    case LOCAL_GL_UNIFORM_BUFFER:
      if (mContent == WebGLBuffer::Kind::OtherData) return true;
      break;

    default:
      MOZ_CRASH();
  }

  const auto dataType =
      (mContent == WebGLBuffer::Kind::OtherData) ? "other" : "element";
  mContext->ErrorInvalidOperation("Buffer already contains %s data.", dataType);
  return false;
}

void WebGLBuffer::ResetLastUpdateFenceId() const {
  mLastUpdateFenceId = mContext->mNextFenceId;
}

}  // namespace mozilla