summaryrefslogtreecommitdiffstats
path: root/dom/canvas/WebGLContextVertices.cpp
blob: e50f5f1c3d335aa9cc6aa4b5cb86d371ddfc935e (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
/* -*- Mode: C++; tab-width: 4; 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 "WebGLContext.h"

#include "GLContext.h"
#include "mozilla/Casting.h"
#include "mozilla/CheckedInt.h"
#include "WebGLBuffer.h"
#include "WebGLFramebuffer.h"
#include "WebGLProgram.h"
#include "WebGLRenderbuffer.h"
#include "WebGLShader.h"
#include "WebGLTexture.h"
#include "WebGLTypes.h"
#include "WebGLVertexArray.h"

#include "mozilla/ResultVariant.h"

namespace mozilla {

static bool ValidateAttribIndex(WebGLContext& webgl, GLuint index) {
  bool valid = (index < webgl.MaxVertexAttribs());

  if (!valid) {
    if (index == GLuint(-1)) {
      webgl.ErrorInvalidValue(
          "-1 is not a valid `index`. This value"
          " probably comes from a getAttribLocation()"
          " call, where this return value -1 means"
          " that the passed name didn't correspond to"
          " an active attribute in the specified"
          " program.");
    } else {
      webgl.ErrorInvalidValue(
          "`index` must be less than"
          " MAX_VERTEX_ATTRIBS.");
    }
  }

  return valid;
}

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

void WebGLContext::VertexAttrib4T(GLuint index, const webgl::TypedQuad& src) {
  const FuncScope funcScope(*this, "vertexAttrib[1234]u?[fi]v?");
  if (IsContextLost()) return;

  if (!ValidateAttribIndex(*this, index)) return;

  ////

  if (index || !gl->IsCompatibilityProfile()) {
    switch (src.type) {
      case webgl::AttribBaseType::Boolean:
      case webgl::AttribBaseType::Float:
        gl->fVertexAttrib4fv(index,
                             reinterpret_cast<const float*>(src.data.data()));
        break;
      case webgl::AttribBaseType::Int:
        gl->fVertexAttribI4iv(
            index, reinterpret_cast<const int32_t*>(src.data.data()));
        break;
      case webgl::AttribBaseType::Uint:
        gl->fVertexAttribI4uiv(
            index, reinterpret_cast<const uint32_t*>(src.data.data()));
        break;
    }
  }

  ////

  mGenericVertexAttribTypes[index] = src.type;
  mGenericVertexAttribTypeInvalidator.InvalidateCaches();

  if (!index) {
    memcpy(mGenericVertexAttrib0Data, src.data.data(),
           sizeof(mGenericVertexAttrib0Data));
  }
}

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

void WebGLContext::EnableVertexAttribArray(GLuint index) {
  const FuncScope funcScope(*this, "enableVertexAttribArray");
  if (IsContextLost()) return;

  if (!ValidateAttribIndex(*this, index)) return;

  gl->fEnableVertexAttribArray(index);

  MOZ_ASSERT(mBoundVertexArray);
  mBoundVertexArray->SetAttribIsArray(index, true);
}

void WebGLContext::DisableVertexAttribArray(GLuint index) {
  const FuncScope funcScope(*this, "disableVertexAttribArray");
  if (IsContextLost()) return;

  if (!ValidateAttribIndex(*this, index)) return;

  if (index || !gl->IsCompatibilityProfile()) {
    gl->fDisableVertexAttribArray(index);
  }

  MOZ_ASSERT(mBoundVertexArray);
  mBoundVertexArray->SetAttribIsArray(index, false);
}

Maybe<double> WebGLContext::GetVertexAttrib(GLuint index, GLenum pname) {
  const FuncScope funcScope(*this, "getVertexAttrib");
  if (IsContextLost()) return Nothing();

  if (!ValidateAttribIndex(*this, index)) return Nothing();

  MOZ_ASSERT(mBoundVertexArray);
  auto ret = mBoundVertexArray->GetVertexAttrib(index, pname);

  switch (pname) {
    case LOCAL_GL_VERTEX_ATTRIB_ARRAY_INTEGER:
      if (!IsWebGL2()) {
        ret = Nothing();
      }
      break;

    case LOCAL_GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
      if (!IsWebGL2() &&
          !IsExtensionEnabled(WebGLExtensionID::ANGLE_instanced_arrays)) {
        ret = Nothing();
      }
      break;
  }

  if (!ret) {
    ErrorInvalidEnumInfo("pname", pname);
  }
  return ret;
}

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

Result<webgl::VertAttribPointerCalculated, webgl::ErrorInfo>
CheckVertexAttribPointer(const bool isWebgl2,
                         const webgl::VertAttribPointerDesc& desc) {
  if (desc.channels < 1 || desc.channels > 4) {
    return Err(webgl::ErrorInfo{LOCAL_GL_INVALID_VALUE,
                                "Channel count `size` must be within [1,4]."});
  }

  ////

  webgl::VertAttribPointerCalculated calc;

  bool isTypeValid = true;
  bool isPackedType = false;
  uint8_t bytesPerType = 0;
  switch (desc.type) {
    // WebGL 1:
    case LOCAL_GL_BYTE:
      bytesPerType = 1;
      calc.baseType = webgl::AttribBaseType::Int;
      break;
    case LOCAL_GL_UNSIGNED_BYTE:
      bytesPerType = 1;
      calc.baseType = webgl::AttribBaseType::Uint;
      break;

    case LOCAL_GL_SHORT:
      bytesPerType = 2;
      calc.baseType = webgl::AttribBaseType::Int;
      break;
    case LOCAL_GL_UNSIGNED_SHORT:
      bytesPerType = 2;
      calc.baseType = webgl::AttribBaseType::Uint;
      break;

    case LOCAL_GL_FLOAT:
      bytesPerType = 4;
      calc.baseType = webgl::AttribBaseType::Float;
      break;

    // WebGL 2:
    case LOCAL_GL_INT:
      isTypeValid = isWebgl2;
      bytesPerType = 4;
      calc.baseType = webgl::AttribBaseType::Int;
      break;
    case LOCAL_GL_UNSIGNED_INT:
      isTypeValid = isWebgl2;
      bytesPerType = 4;
      calc.baseType = webgl::AttribBaseType::Uint;
      break;

    case LOCAL_GL_HALF_FLOAT:
      isTypeValid = isWebgl2;
      bytesPerType = 2;
      calc.baseType = webgl::AttribBaseType::Float;
      break;

    case LOCAL_GL_INT_2_10_10_10_REV:
    case LOCAL_GL_UNSIGNED_INT_2_10_10_10_REV:
      if (desc.channels != 4) {
        return Err(webgl::ErrorInfo{LOCAL_GL_INVALID_OPERATION,
                                    "Size must be 4 for this type."});
      }
      isTypeValid = isWebgl2;
      bytesPerType = 4;
      calc.baseType =
          webgl::AttribBaseType::Float;  // Invalid for intFunc:true.
      isPackedType = true;
      break;

    default:
      isTypeValid = false;
      break;
  }
  if (desc.intFunc) {
    isTypeValid = (calc.baseType != webgl::AttribBaseType::Float);
  } else {
    calc.baseType = webgl::AttribBaseType::Float;
  }
  if (!isTypeValid) {
    const auto info =
        nsPrintfCString("Bad `type`: %s", EnumString(desc.type).c_str());
    return Err(webgl::ErrorInfo{LOCAL_GL_INVALID_ENUM, info.BeginReading()});
  }

  ////

  calc.byteSize = bytesPerType;
  if (!isPackedType) {
    calc.byteSize *= desc.channels;
  }

  calc.byteStride =
      desc.byteStrideOrZero ? desc.byteStrideOrZero : calc.byteSize;

  // `alignment` should always be a power of two.
  MOZ_ASSERT(IsPowerOfTwo(bytesPerType));
  const auto typeAlignmentMask = bytesPerType - 1;

  if (calc.byteStride & typeAlignmentMask ||
      desc.byteOffset & typeAlignmentMask) {
    return Err(
        webgl::ErrorInfo{LOCAL_GL_INVALID_OPERATION,
                         "`stride` and `byteOffset` must satisfy the alignment"
                         " requirement of `type`."});
  }

  return calc;
}

void DoVertexAttribPointer(gl::GLContext& gl, const uint32_t index,
                           const webgl::VertAttribPointerDesc& desc) {
  if (desc.intFunc) {
    gl.fVertexAttribIPointer(index, desc.channels, desc.type,
                             desc.byteStrideOrZero,
                             reinterpret_cast<const void*>(desc.byteOffset));
  } else {
    gl.fVertexAttribPointer(index, desc.channels, desc.type, desc.normalized,
                            desc.byteStrideOrZero,
                            reinterpret_cast<const void*>(desc.byteOffset));
  }
}

void WebGLContext::VertexAttribPointer(
    const uint32_t index, const webgl::VertAttribPointerDesc& desc) {
  if (IsContextLost()) return;
  if (!ValidateAttribIndex(*this, index)) return;

  const auto res = CheckVertexAttribPointer(IsWebGL2(), desc);
  if (res.isErr()) {
    const auto& err = res.inspectErr();
    GenerateError(err.type, "%s", err.info.c_str());
    return;
  }
  const auto& calc = res.inspect();

  ////

  const auto& buffer = mBoundArrayBuffer;

  mBoundVertexArray->AttribPointer(index, buffer, desc, calc);

  const ScopedLazyBind lazyBind(gl, LOCAL_GL_ARRAY_BUFFER, buffer);
  DoVertexAttribPointer(*gl, index, desc);
}

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

void WebGLContext::VertexAttribDivisor(GLuint index, GLuint divisor) {
  const FuncScope funcScope(*this, "vertexAttribDivisor");
  if (IsContextLost()) return;

  if (!ValidateAttribIndex(*this, index)) return;

  MOZ_ASSERT(mBoundVertexArray);
  mBoundVertexArray->AttribDivisor(index, divisor);
  gl->fVertexAttribDivisor(index, divisor);
}

}  // namespace mozilla