content/canvas/src/WebGLContextVertices.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "WebGLContext.h"
michael@0 7
michael@0 8 #include "GLContext.h"
michael@0 9 #include "mozilla/CheckedInt.h"
michael@0 10 #include "WebGLBuffer.h"
michael@0 11 #include "WebGLFramebuffer.h"
michael@0 12 #include "WebGLProgram.h"
michael@0 13 #include "WebGLRenderbuffer.h"
michael@0 14 #include "WebGLShader.h"
michael@0 15 #include "WebGLTexture.h"
michael@0 16 #include "WebGLUniformInfo.h"
michael@0 17 #include "WebGLVertexArray.h"
michael@0 18 #include "WebGLVertexAttribData.h"
michael@0 19
michael@0 20 using namespace mozilla;
michael@0 21 using namespace dom;
michael@0 22
michael@0 23 void
michael@0 24 WebGLContext::VertexAttrib1f(GLuint index, GLfloat x0)
michael@0 25 {
michael@0 26 if (IsContextLost())
michael@0 27 return;
michael@0 28
michael@0 29 MakeContextCurrent();
michael@0 30
michael@0 31 if (index) {
michael@0 32 gl->fVertexAttrib1f(index, x0);
michael@0 33 } else {
michael@0 34 mVertexAttrib0Vector[0] = x0;
michael@0 35 mVertexAttrib0Vector[1] = 0;
michael@0 36 mVertexAttrib0Vector[2] = 0;
michael@0 37 mVertexAttrib0Vector[3] = 1;
michael@0 38 if (gl->IsGLES())
michael@0 39 gl->fVertexAttrib1f(index, x0);
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 void
michael@0 44 WebGLContext::VertexAttrib2f(GLuint index, GLfloat x0, GLfloat x1)
michael@0 45 {
michael@0 46 if (IsContextLost())
michael@0 47 return;
michael@0 48
michael@0 49 MakeContextCurrent();
michael@0 50
michael@0 51 if (index) {
michael@0 52 gl->fVertexAttrib2f(index, x0, x1);
michael@0 53 } else {
michael@0 54 mVertexAttrib0Vector[0] = x0;
michael@0 55 mVertexAttrib0Vector[1] = x1;
michael@0 56 mVertexAttrib0Vector[2] = 0;
michael@0 57 mVertexAttrib0Vector[3] = 1;
michael@0 58 if (gl->IsGLES())
michael@0 59 gl->fVertexAttrib2f(index, x0, x1);
michael@0 60 }
michael@0 61 }
michael@0 62
michael@0 63 void
michael@0 64 WebGLContext::VertexAttrib3f(GLuint index, GLfloat x0, GLfloat x1, GLfloat x2)
michael@0 65 {
michael@0 66 if (IsContextLost())
michael@0 67 return;
michael@0 68
michael@0 69 MakeContextCurrent();
michael@0 70
michael@0 71 if (index) {
michael@0 72 gl->fVertexAttrib3f(index, x0, x1, x2);
michael@0 73 } else {
michael@0 74 mVertexAttrib0Vector[0] = x0;
michael@0 75 mVertexAttrib0Vector[1] = x1;
michael@0 76 mVertexAttrib0Vector[2] = x2;
michael@0 77 mVertexAttrib0Vector[3] = 1;
michael@0 78 if (gl->IsGLES())
michael@0 79 gl->fVertexAttrib3f(index, x0, x1, x2);
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 void
michael@0 84 WebGLContext::VertexAttrib4f(GLuint index, GLfloat x0, GLfloat x1,
michael@0 85 GLfloat x2, GLfloat x3)
michael@0 86 {
michael@0 87 if (IsContextLost())
michael@0 88 return;
michael@0 89
michael@0 90 MakeContextCurrent();
michael@0 91
michael@0 92 if (index) {
michael@0 93 gl->fVertexAttrib4f(index, x0, x1, x2, x3);
michael@0 94 } else {
michael@0 95 mVertexAttrib0Vector[0] = x0;
michael@0 96 mVertexAttrib0Vector[1] = x1;
michael@0 97 mVertexAttrib0Vector[2] = x2;
michael@0 98 mVertexAttrib0Vector[3] = x3;
michael@0 99 if (gl->IsGLES())
michael@0 100 gl->fVertexAttrib4f(index, x0, x1, x2, x3);
michael@0 101 }
michael@0 102 }
michael@0 103
michael@0 104
michael@0 105 void
michael@0 106 WebGLContext::VertexAttrib1fv_base(GLuint idx, uint32_t arrayLength,
michael@0 107 const GLfloat* ptr)
michael@0 108 {
michael@0 109 if (!ValidateAttribArraySetter("VertexAttrib1fv", 1, arrayLength))
michael@0 110 return;
michael@0 111
michael@0 112 MakeContextCurrent();
michael@0 113 if (idx) {
michael@0 114 gl->fVertexAttrib1fv(idx, ptr);
michael@0 115 } else {
michael@0 116 mVertexAttrib0Vector[0] = ptr[0];
michael@0 117 mVertexAttrib0Vector[1] = GLfloat(0);
michael@0 118 mVertexAttrib0Vector[2] = GLfloat(0);
michael@0 119 mVertexAttrib0Vector[3] = GLfloat(1);
michael@0 120 if (gl->IsGLES())
michael@0 121 gl->fVertexAttrib1fv(idx, ptr);
michael@0 122 }
michael@0 123 }
michael@0 124
michael@0 125 void
michael@0 126 WebGLContext::VertexAttrib2fv_base(GLuint idx, uint32_t arrayLength,
michael@0 127 const GLfloat* ptr)
michael@0 128 {
michael@0 129 if (!ValidateAttribArraySetter("VertexAttrib2fv", 2, arrayLength))
michael@0 130 return;
michael@0 131
michael@0 132 MakeContextCurrent();
michael@0 133 if (idx) {
michael@0 134 gl->fVertexAttrib2fv(idx, ptr);
michael@0 135 } else {
michael@0 136 mVertexAttrib0Vector[0] = ptr[0];
michael@0 137 mVertexAttrib0Vector[1] = ptr[1];
michael@0 138 mVertexAttrib0Vector[2] = GLfloat(0);
michael@0 139 mVertexAttrib0Vector[3] = GLfloat(1);
michael@0 140 if (gl->IsGLES())
michael@0 141 gl->fVertexAttrib2fv(idx, ptr);
michael@0 142 }
michael@0 143 }
michael@0 144
michael@0 145 void
michael@0 146 WebGLContext::VertexAttrib3fv_base(GLuint idx, uint32_t arrayLength,
michael@0 147 const GLfloat* ptr)
michael@0 148 {
michael@0 149 if (!ValidateAttribArraySetter("VertexAttrib3fv", 3, arrayLength))
michael@0 150 return;
michael@0 151
michael@0 152 MakeContextCurrent();
michael@0 153 if (idx) {
michael@0 154 gl->fVertexAttrib3fv(idx, ptr);
michael@0 155 } else {
michael@0 156 mVertexAttrib0Vector[0] = ptr[0];
michael@0 157 mVertexAttrib0Vector[1] = ptr[1];
michael@0 158 mVertexAttrib0Vector[2] = ptr[2];
michael@0 159 mVertexAttrib0Vector[3] = GLfloat(1);
michael@0 160 if (gl->IsGLES())
michael@0 161 gl->fVertexAttrib3fv(idx, ptr);
michael@0 162 }
michael@0 163 }
michael@0 164
michael@0 165 void
michael@0 166 WebGLContext::VertexAttrib4fv_base(GLuint idx, uint32_t arrayLength,
michael@0 167 const GLfloat* ptr)
michael@0 168 {
michael@0 169 if (!ValidateAttribArraySetter("VertexAttrib4fv", 4, arrayLength))
michael@0 170 return;
michael@0 171
michael@0 172 MakeContextCurrent();
michael@0 173 if (idx) {
michael@0 174 gl->fVertexAttrib4fv(idx, ptr);
michael@0 175 } else {
michael@0 176 mVertexAttrib0Vector[0] = ptr[0];
michael@0 177 mVertexAttrib0Vector[1] = ptr[1];
michael@0 178 mVertexAttrib0Vector[2] = ptr[2];
michael@0 179 mVertexAttrib0Vector[3] = ptr[3];
michael@0 180 if (gl->IsGLES())
michael@0 181 gl->fVertexAttrib4fv(idx, ptr);
michael@0 182 }
michael@0 183 }
michael@0 184
michael@0 185 void
michael@0 186 WebGLContext::EnableVertexAttribArray(GLuint index)
michael@0 187 {
michael@0 188 if (IsContextLost())
michael@0 189 return;
michael@0 190
michael@0 191 if (!ValidateAttribIndex(index, "enableVertexAttribArray"))
michael@0 192 return;
michael@0 193
michael@0 194 MakeContextCurrent();
michael@0 195 InvalidateBufferFetching();
michael@0 196
michael@0 197 gl->fEnableVertexAttribArray(index);
michael@0 198 MOZ_ASSERT(mBoundVertexArray->HasAttrib(index)); // should have been validated earlier
michael@0 199 mBoundVertexArray->mAttribs[index].enabled = true;
michael@0 200 }
michael@0 201
michael@0 202 void
michael@0 203 WebGLContext::DisableVertexAttribArray(GLuint index)
michael@0 204 {
michael@0 205 if (IsContextLost())
michael@0 206 return;
michael@0 207
michael@0 208 if (!ValidateAttribIndex(index, "disableVertexAttribArray"))
michael@0 209 return;
michael@0 210
michael@0 211 MakeContextCurrent();
michael@0 212 InvalidateBufferFetching();
michael@0 213
michael@0 214 if (index || gl->IsGLES())
michael@0 215 gl->fDisableVertexAttribArray(index);
michael@0 216
michael@0 217 MOZ_ASSERT(mBoundVertexArray->HasAttrib(index)); // should have been validated earlier
michael@0 218 mBoundVertexArray->mAttribs[index].enabled = false;
michael@0 219 }
michael@0 220
michael@0 221
michael@0 222 JS::Value
michael@0 223 WebGLContext::GetVertexAttrib(JSContext* cx, GLuint index, GLenum pname,
michael@0 224 ErrorResult& rv)
michael@0 225 {
michael@0 226 if (IsContextLost())
michael@0 227 return JS::NullValue();
michael@0 228
michael@0 229 if (!ValidateAttribIndex(index, "getVertexAttrib"))
michael@0 230 return JS::NullValue();
michael@0 231
michael@0 232 MakeContextCurrent();
michael@0 233
michael@0 234 switch (pname) {
michael@0 235 case LOCAL_GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
michael@0 236 {
michael@0 237 return WebGLObjectAsJSValue(cx, mBoundVertexArray->mAttribs[index].buf.get(), rv);
michael@0 238 }
michael@0 239
michael@0 240 case LOCAL_GL_VERTEX_ATTRIB_ARRAY_STRIDE:
michael@0 241 {
michael@0 242 return JS::Int32Value(mBoundVertexArray->mAttribs[index].stride);
michael@0 243 }
michael@0 244
michael@0 245 case LOCAL_GL_VERTEX_ATTRIB_ARRAY_SIZE:
michael@0 246 {
michael@0 247 if (!ValidateAttribIndex(index, "getVertexAttrib"))
michael@0 248 return JS::NullValue();
michael@0 249
michael@0 250 if (!mBoundVertexArray->mAttribs[index].enabled)
michael@0 251 return JS::Int32Value(4);
michael@0 252
michael@0 253 // Don't break; fall through.
michael@0 254 }
michael@0 255 case LOCAL_GL_VERTEX_ATTRIB_ARRAY_TYPE:
michael@0 256 {
michael@0 257 GLint i = 0;
michael@0 258 gl->fGetVertexAttribiv(index, pname, &i);
michael@0 259 if (pname == LOCAL_GL_VERTEX_ATTRIB_ARRAY_SIZE)
michael@0 260 return JS::Int32Value(i);
michael@0 261 MOZ_ASSERT(pname == LOCAL_GL_VERTEX_ATTRIB_ARRAY_TYPE);
michael@0 262 return JS::NumberValue(uint32_t(i));
michael@0 263 }
michael@0 264
michael@0 265 case LOCAL_GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
michael@0 266 {
michael@0 267 if (IsExtensionEnabled(WebGLExtensionID::ANGLE_instanced_arrays))
michael@0 268 {
michael@0 269 return JS::Int32Value(mBoundVertexArray->mAttribs[index].divisor);
michael@0 270 }
michael@0 271 break;
michael@0 272 }
michael@0 273
michael@0 274 case LOCAL_GL_CURRENT_VERTEX_ATTRIB:
michael@0 275 {
michael@0 276 GLfloat vec[4] = {0, 0, 0, 1};
michael@0 277 if (index) {
michael@0 278 gl->fGetVertexAttribfv(index, LOCAL_GL_CURRENT_VERTEX_ATTRIB, &vec[0]);
michael@0 279 } else {
michael@0 280 vec[0] = mVertexAttrib0Vector[0];
michael@0 281 vec[1] = mVertexAttrib0Vector[1];
michael@0 282 vec[2] = mVertexAttrib0Vector[2];
michael@0 283 vec[3] = mVertexAttrib0Vector[3];
michael@0 284 }
michael@0 285 JSObject* obj = Float32Array::Create(cx, this, 4, vec);
michael@0 286 if (!obj) {
michael@0 287 rv.Throw(NS_ERROR_OUT_OF_MEMORY);
michael@0 288 }
michael@0 289 return JS::ObjectOrNullValue(obj);
michael@0 290 }
michael@0 291
michael@0 292 case LOCAL_GL_VERTEX_ATTRIB_ARRAY_ENABLED:
michael@0 293 {
michael@0 294 return JS::BooleanValue(mBoundVertexArray->mAttribs[index].enabled);
michael@0 295 }
michael@0 296
michael@0 297 case LOCAL_GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
michael@0 298 {
michael@0 299 return JS::BooleanValue(mBoundVertexArray->mAttribs[index].normalized);
michael@0 300 }
michael@0 301
michael@0 302 default:
michael@0 303 break;
michael@0 304 }
michael@0 305
michael@0 306 ErrorInvalidEnumInfo("getVertexAttrib: parameter", pname);
michael@0 307
michael@0 308 return JS::NullValue();
michael@0 309 }
michael@0 310
michael@0 311 WebGLsizeiptr
michael@0 312 WebGLContext::GetVertexAttribOffset(GLuint index, GLenum pname)
michael@0 313 {
michael@0 314 if (IsContextLost())
michael@0 315 return 0;
michael@0 316
michael@0 317 if (!ValidateAttribIndex(index, "getVertexAttribOffset"))
michael@0 318 return 0;
michael@0 319
michael@0 320 if (pname != LOCAL_GL_VERTEX_ATTRIB_ARRAY_POINTER) {
michael@0 321 ErrorInvalidEnum("getVertexAttribOffset: bad parameter");
michael@0 322 return 0;
michael@0 323 }
michael@0 324
michael@0 325 return mBoundVertexArray->mAttribs[index].byteOffset;
michael@0 326 }
michael@0 327
michael@0 328 void
michael@0 329 WebGLContext::VertexAttribPointer(GLuint index, GLint size, GLenum type,
michael@0 330 WebGLboolean normalized, GLsizei stride,
michael@0 331 WebGLintptr byteOffset)
michael@0 332 {
michael@0 333 if (IsContextLost())
michael@0 334 return;
michael@0 335
michael@0 336 if (mBoundArrayBuffer == nullptr)
michael@0 337 return ErrorInvalidOperation("vertexAttribPointer: must have valid GL_ARRAY_BUFFER binding");
michael@0 338
michael@0 339 GLsizei requiredAlignment = 1;
michael@0 340 switch (type) {
michael@0 341 case LOCAL_GL_BYTE:
michael@0 342 case LOCAL_GL_UNSIGNED_BYTE:
michael@0 343 requiredAlignment = 1;
michael@0 344 break;
michael@0 345 case LOCAL_GL_SHORT:
michael@0 346 case LOCAL_GL_UNSIGNED_SHORT:
michael@0 347 requiredAlignment = 2;
michael@0 348 break;
michael@0 349 // XXX case LOCAL_GL_FIXED:
michael@0 350 case LOCAL_GL_FLOAT:
michael@0 351 requiredAlignment = 4;
michael@0 352 break;
michael@0 353 default:
michael@0 354 return ErrorInvalidEnumInfo("vertexAttribPointer: type", type);
michael@0 355 }
michael@0 356
michael@0 357 // requiredAlignment should always be a power of two.
michael@0 358 GLsizei requiredAlignmentMask = requiredAlignment - 1;
michael@0 359
michael@0 360 if (!ValidateAttribIndex(index, "vertexAttribPointer")) {
michael@0 361 return;
michael@0 362 }
michael@0 363
michael@0 364 if (size < 1 || size > 4)
michael@0 365 return ErrorInvalidValue("vertexAttribPointer: invalid element size");
michael@0 366
michael@0 367 if (stride < 0 || stride > 255) // see WebGL spec section 6.6 "Vertex Attribute Data Stride"
michael@0 368 return ErrorInvalidValue("vertexAttribPointer: negative or too large stride");
michael@0 369
michael@0 370 if (byteOffset < 0)
michael@0 371 return ErrorInvalidValue("vertexAttribPointer: negative offset");
michael@0 372
michael@0 373 if (stride & requiredAlignmentMask) {
michael@0 374 return ErrorInvalidOperation("vertexAttribPointer: stride doesn't satisfy the alignment "
michael@0 375 "requirement of given type");
michael@0 376 }
michael@0 377
michael@0 378 if (byteOffset & requiredAlignmentMask) {
michael@0 379 return ErrorInvalidOperation("vertexAttribPointer: byteOffset doesn't satisfy the alignment "
michael@0 380 "requirement of given type");
michael@0 381
michael@0 382 }
michael@0 383
michael@0 384 InvalidateBufferFetching();
michael@0 385
michael@0 386 /* XXX make work with bufferSubData & heterogeneous types
michael@0 387 if (type != mBoundArrayBuffer->GLType())
michael@0 388 return ErrorInvalidOperation("vertexAttribPointer: type must match bound VBO type: %d != %d", type, mBoundArrayBuffer->GLType());
michael@0 389 */
michael@0 390
michael@0 391 WebGLVertexAttribData &vd = mBoundVertexArray->mAttribs[index];
michael@0 392
michael@0 393 vd.buf = mBoundArrayBuffer;
michael@0 394 vd.stride = stride;
michael@0 395 vd.size = size;
michael@0 396 vd.byteOffset = byteOffset;
michael@0 397 vd.type = type;
michael@0 398 vd.normalized = normalized;
michael@0 399
michael@0 400 MakeContextCurrent();
michael@0 401
michael@0 402 gl->fVertexAttribPointer(index, size, type, normalized,
michael@0 403 stride,
michael@0 404 reinterpret_cast<void*>(byteOffset));
michael@0 405 }
michael@0 406
michael@0 407 void
michael@0 408 WebGLContext::VertexAttribDivisor(GLuint index, GLuint divisor)
michael@0 409 {
michael@0 410 if (IsContextLost())
michael@0 411 return;
michael@0 412
michael@0 413 if (!ValidateAttribIndex(index, "vertexAttribDivisor")) {
michael@0 414 return;
michael@0 415 }
michael@0 416
michael@0 417 WebGLVertexAttribData& vd = mBoundVertexArray->mAttribs[index];
michael@0 418 vd.divisor = divisor;
michael@0 419
michael@0 420 InvalidateBufferFetching();
michael@0 421
michael@0 422 MakeContextCurrent();
michael@0 423
michael@0 424 gl->fVertexAttribDivisor(index, divisor);
michael@0 425 }

mercurial