2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-07-26 04:38:58 +04:00
|
|
|
/* 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"
|
2014-03-17 18:52:56 +04:00
|
|
|
|
|
|
|
#include "GLContext.h"
|
2019-04-02 20:00:41 +03:00
|
|
|
#include "mozilla/Casting.h"
|
2014-03-17 18:52:56 +04:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2013-07-26 04:38:58 +04:00
|
|
|
#include "WebGLBuffer.h"
|
|
|
|
#include "WebGLFramebuffer.h"
|
2013-08-07 04:05:51 +04:00
|
|
|
#include "WebGLProgram.h"
|
2014-03-17 18:52:56 +04:00
|
|
|
#include "WebGLRenderbuffer.h"
|
|
|
|
#include "WebGLShader.h"
|
|
|
|
#include "WebGLTexture.h"
|
2020-01-09 01:19:14 +03:00
|
|
|
#include "WebGLTypes.h"
|
2014-03-17 18:52:56 +04:00
|
|
|
#include "WebGLVertexArray.h"
|
2013-07-26 04:38:58 +04:00
|
|
|
|
2015-07-20 10:56:17 +03:00
|
|
|
#include "mozilla/Casting.h"
|
|
|
|
|
2015-07-15 03:37:28 +03:00
|
|
|
namespace mozilla {
|
2013-07-26 04:38:58 +04:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
static bool ValidateAttribIndex(WebGLContext& webgl, GLuint index) {
|
|
|
|
bool valid = (index < webgl.MaxVertexAttribs());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
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.");
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-07-27 07:46:33 +03:00
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
void WebGLContext::VertexAttrib4T(GLuint index, const webgl::TypedQuad& src) {
|
|
|
|
const FuncScope funcScope(*this, "vertexAttrib[1234]u?[fi]v?");
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost()) return;
|
2013-08-07 04:05:51 +04:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!ValidateAttribIndex(*this, index)) return;
|
2014-10-24 02:10:57 +04:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
////
|
2013-08-07 04:05:51 +04:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
if (index || !gl->IsCompatibilityProfile()) {
|
2020-01-09 01:19:16 +03:00
|
|
|
switch (src.type) {
|
|
|
|
case webgl::AttribBaseType::Boolean:
|
|
|
|
case webgl::AttribBaseType::Float:
|
|
|
|
gl->fVertexAttrib4fv(index, reinterpret_cast<const float*>(src.data));
|
|
|
|
break;
|
|
|
|
case webgl::AttribBaseType::Int:
|
|
|
|
gl->fVertexAttribI4iv(index,
|
|
|
|
reinterpret_cast<const int32_t*>(src.data));
|
|
|
|
break;
|
|
|
|
case webgl::AttribBaseType::Uint:
|
|
|
|
gl->fVertexAttribI4uiv(index,
|
|
|
|
reinterpret_cast<const uint32_t*>(src.data));
|
|
|
|
break;
|
|
|
|
}
|
2013-08-07 04:05:51 +04:00
|
|
|
}
|
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
////
|
2014-10-24 02:10:57 +04:00
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
mGenericVertexAttribTypes[index] = src.type;
|
2017-10-21 01:40:12 +03:00
|
|
|
mGenericVertexAttribTypeInvalidator.InvalidateCaches();
|
2015-07-20 10:56:17 +03:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
if (!index) {
|
2020-01-09 01:19:16 +03:00
|
|
|
memcpy(mGenericVertexAttrib0Data, src.data,
|
|
|
|
sizeof(mGenericVertexAttrib0Data));
|
2013-08-07 04:05:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2013-09-04 16:14:43 +04:00
|
|
|
void WebGLContext::EnableVertexAttribArray(GLuint index) {
|
2018-07-27 07:46:33 +03:00
|
|
|
const FuncScope funcScope(*this, "enableVertexAttribArray");
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost()) return;
|
2013-08-07 04:05:51 +04:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!ValidateAttribIndex(*this, index)) return;
|
2013-08-07 04:05:51 +04:00
|
|
|
|
|
|
|
gl->fEnableVertexAttribArray(index);
|
2014-10-24 02:10:57 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mBoundVertexArray);
|
2020-02-26 03:44:26 +03:00
|
|
|
mBoundVertexArray->SetAttribIsArray(index, true);
|
2013-08-07 04:05:51 +04:00
|
|
|
}
|
|
|
|
|
2013-09-04 16:14:43 +04:00
|
|
|
void WebGLContext::DisableVertexAttribArray(GLuint index) {
|
2018-07-27 07:46:33 +03:00
|
|
|
const FuncScope funcScope(*this, "disableVertexAttribArray");
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost()) return;
|
2013-08-07 04:05:51 +04:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!ValidateAttribIndex(*this, index)) return;
|
2013-08-07 04:05:51 +04:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
if (index || !gl->IsCompatibilityProfile()) {
|
2013-08-07 04:05:51 +04:00
|
|
|
gl->fDisableVertexAttribArray(index);
|
2016-09-24 03:28:58 +03:00
|
|
|
}
|
2013-08-07 04:05:51 +04:00
|
|
|
|
2014-10-24 02:10:57 +04:00
|
|
|
MOZ_ASSERT(mBoundVertexArray);
|
2020-02-26 03:44:26 +03:00
|
|
|
mBoundVertexArray->SetAttribIsArray(index, false);
|
2013-08-07 04:05:51 +04:00
|
|
|
}
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
Maybe<double> WebGLContext::GetVertexAttrib(GLuint index, GLenum pname) {
|
2018-07-27 07:46:33 +03:00
|
|
|
const FuncScope funcScope(*this, "getVertexAttrib");
|
2020-01-09 01:19:14 +03:00
|
|
|
if (IsContextLost()) return Nothing();
|
2013-08-07 04:05:51 +04:00
|
|
|
|
2020-01-09 01:19:14 +03:00
|
|
|
if (!ValidateAttribIndex(*this, index)) return Nothing();
|
2013-08-07 04:05:51 +04:00
|
|
|
|
2014-10-24 02:10:57 +04:00
|
|
|
MOZ_ASSERT(mBoundVertexArray);
|
2020-02-26 03:44:26 +03:00
|
|
|
auto ret = mBoundVertexArray->GetVertexAttrib(index, pname);
|
2014-10-24 02:10:57 +04:00
|
|
|
|
2013-08-07 04:05:51 +04:00
|
|
|
switch (pname) {
|
2016-01-22 04:44:01 +03:00
|
|
|
case LOCAL_GL_VERTEX_ATTRIB_ARRAY_INTEGER:
|
2020-02-26 03:44:26 +03:00
|
|
|
if (!IsWebGL2()) {
|
2020-03-27 17:10:45 +03:00
|
|
|
ret = Nothing();
|
2020-02-26 03:44:26 +03:00
|
|
|
}
|
2016-01-22 04:44:01 +03:00
|
|
|
break;
|
|
|
|
|
2015-07-20 05:55:38 +03:00
|
|
|
case LOCAL_GL_VERTEX_ATTRIB_ARRAY_DIVISOR:
|
2020-02-26 03:44:26 +03:00
|
|
|
if (!IsWebGL2() &&
|
|
|
|
!IsExtensionEnabled(WebGLExtensionID::ANGLE_instanced_arrays)) {
|
2020-03-27 17:10:45 +03:00
|
|
|
ret = Nothing();
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
2015-07-20 05:55:38 +03:00
|
|
|
break;
|
2013-08-07 04:05:51 +04:00
|
|
|
}
|
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
if (!ret) {
|
|
|
|
ErrorInvalidEnumInfo("pname", pname);
|
|
|
|
}
|
|
|
|
return ret;
|
2013-08-07 04:05:51 +04:00
|
|
|
}
|
|
|
|
|
2017-01-12 02:57:29 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
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]."});
|
2017-01-12 02:57:29 +03:00
|
|
|
}
|
2017-01-12 02:57:29 +03:00
|
|
|
|
2017-01-12 02:57:29 +03:00
|
|
|
////
|
2017-01-12 02:57:29 +03:00
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
webgl::VertAttribPointerCalculated calc;
|
|
|
|
|
2017-01-12 02:57:29 +03:00
|
|
|
bool isTypeValid = true;
|
2020-02-26 03:44:26 +03:00
|
|
|
bool isPackedType = false;
|
|
|
|
uint8_t bytesPerType = 0;
|
|
|
|
switch (desc.type) {
|
2017-01-12 02:57:29 +03:00
|
|
|
// WebGL 1:
|
|
|
|
case LOCAL_GL_BYTE:
|
2020-02-26 03:44:26 +03:00
|
|
|
bytesPerType = 1;
|
|
|
|
calc.baseType = webgl::AttribBaseType::Int;
|
|
|
|
break;
|
2017-01-12 02:57:29 +03:00
|
|
|
case LOCAL_GL_UNSIGNED_BYTE:
|
2020-02-26 03:44:26 +03:00
|
|
|
bytesPerType = 1;
|
|
|
|
calc.baseType = webgl::AttribBaseType::Uint;
|
2017-01-12 02:57:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_SHORT:
|
2020-02-26 03:44:26 +03:00
|
|
|
bytesPerType = 2;
|
|
|
|
calc.baseType = webgl::AttribBaseType::Int;
|
|
|
|
break;
|
2017-01-12 02:57:29 +03:00
|
|
|
case LOCAL_GL_UNSIGNED_SHORT:
|
2020-02-26 03:44:26 +03:00
|
|
|
bytesPerType = 2;
|
|
|
|
calc.baseType = webgl::AttribBaseType::Uint;
|
2017-01-12 02:57:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_FLOAT:
|
2020-02-26 03:44:26 +03:00
|
|
|
bytesPerType = 4;
|
|
|
|
calc.baseType = webgl::AttribBaseType::Float;
|
2017-01-12 02:57:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
// WebGL 2:
|
|
|
|
case LOCAL_GL_INT:
|
2020-02-26 03:44:26 +03:00
|
|
|
isTypeValid = isWebgl2;
|
|
|
|
bytesPerType = 4;
|
|
|
|
calc.baseType = webgl::AttribBaseType::Int;
|
|
|
|
break;
|
2017-01-12 02:57:29 +03:00
|
|
|
case LOCAL_GL_UNSIGNED_INT:
|
2020-02-26 03:44:26 +03:00
|
|
|
isTypeValid = isWebgl2;
|
|
|
|
bytesPerType = 4;
|
|
|
|
calc.baseType = webgl::AttribBaseType::Uint;
|
2017-01-12 02:57:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_HALF_FLOAT:
|
2020-02-26 03:44:26 +03:00
|
|
|
isTypeValid = isWebgl2;
|
|
|
|
bytesPerType = 2;
|
|
|
|
calc.baseType = webgl::AttribBaseType::Float;
|
2017-01-12 02:57:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_FIXED:
|
2020-02-26 03:44:26 +03:00
|
|
|
isTypeValid = isWebgl2;
|
|
|
|
bytesPerType = 4;
|
|
|
|
calc.baseType = webgl::AttribBaseType::Float;
|
2017-01-12 02:57:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_INT_2_10_10_10_REV:
|
|
|
|
case LOCAL_GL_UNSIGNED_INT_2_10_10_10_REV:
|
2020-02-26 03:44:26 +03:00
|
|
|
if (desc.channels != 4) {
|
|
|
|
return Err(webgl::ErrorInfo{LOCAL_GL_INVALID_OPERATION,
|
|
|
|
"Size must be 4 for this type."});
|
2017-01-12 02:57:29 +03:00
|
|
|
}
|
2020-02-26 03:44:26 +03:00
|
|
|
isTypeValid = isWebgl2;
|
|
|
|
bytesPerType = 4;
|
|
|
|
calc.baseType =
|
|
|
|
webgl::AttribBaseType::Float; // Invalid for intFunc:true.
|
|
|
|
isPackedType = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2017-01-12 02:57:29 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
default:
|
2017-01-12 02:57:29 +03:00
|
|
|
isTypeValid = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-02-26 03:44:26 +03:00
|
|
|
if (desc.intFunc) {
|
|
|
|
isTypeValid = (calc.baseType != webgl::AttribBaseType::Float);
|
|
|
|
} else {
|
|
|
|
calc.baseType = webgl::AttribBaseType::Float;
|
|
|
|
}
|
2017-01-12 02:57:29 +03:00
|
|
|
if (!isTypeValid) {
|
2020-01-09 01:19:16 +03:00
|
|
|
const auto info =
|
2020-02-26 03:44:26 +03:00
|
|
|
nsPrintfCString("Bad `type`: %s", EnumString(desc.type).c_str());
|
|
|
|
return Err(webgl::ErrorInfo{LOCAL_GL_INVALID_ENUM, info.BeginReading()});
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
calc.byteSize = bytesPerType;
|
|
|
|
if (!isPackedType) {
|
|
|
|
calc.byteSize *= desc.channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
calc.byteStride =
|
|
|
|
desc.byteStrideOrZero ? desc.byteStrideOrZero : calc.byteSize;
|
|
|
|
|
2017-01-12 02:57:29 +03:00
|
|
|
// `alignment` should always be a power of two.
|
2020-02-26 03:44:26 +03:00
|
|
|
MOZ_ASSERT(IsPowerOfTwo(bytesPerType));
|
|
|
|
const auto typeAlignmentMask = bytesPerType - 1;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
if (calc.byteStride & typeAlignmentMask ||
|
|
|
|
desc.byteOffset & typeAlignmentMask) {
|
|
|
|
return Err(
|
2020-01-09 01:19:16 +03:00
|
|
|
webgl::ErrorInfo{LOCAL_GL_INVALID_OPERATION,
|
|
|
|
"`stride` and `byteOffset` must satisfy the alignment"
|
|
|
|
" requirement of `type`."});
|
|
|
|
}
|
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
return calc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoVertexAttribPointer(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));
|
|
|
|
}
|
2020-01-09 01:19:16 +03:00
|
|
|
}
|
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
void WebGLContext::VertexAttribPointer(
|
|
|
|
const uint32_t index, const webgl::VertAttribPointerDesc& desc) {
|
2020-01-09 01:19:16 +03:00
|
|
|
if (IsContextLost()) return;
|
|
|
|
if (!ValidateAttribIndex(*this, index)) return;
|
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
const auto res = CheckVertexAttribPointer(IsWebGL2(), desc);
|
|
|
|
if (res.isErr()) {
|
|
|
|
const auto& err = res.inspectErr();
|
|
|
|
GenerateError(err.type, "%s", err.info.c_str());
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-02-26 03:44:26 +03:00
|
|
|
const auto& calc = res.inspect();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
////
|
|
|
|
|
2017-01-12 02:57:29 +03:00
|
|
|
const auto& buffer = mBoundArrayBuffer;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
mBoundVertexArray->AttribPointer(index, buffer, desc, calc);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
const ScopedLazyBind lazyBind(gl, LOCAL_GL_ARRAY_BUFFER, buffer);
|
|
|
|
DoVertexAttribPointer(*gl, index, desc);
|
2013-08-07 04:05:51 +04:00
|
|
|
}
|
|
|
|
|
2017-01-12 02:57:29 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2013-09-04 16:14:43 +04:00
|
|
|
void WebGLContext::VertexAttribDivisor(GLuint index, GLuint divisor) {
|
2018-07-27 07:46:33 +03:00
|
|
|
const FuncScope funcScope(*this, "vertexAttribDivisor");
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost()) return;
|
2013-08-07 04:05:51 +04:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!ValidateAttribIndex(*this, index)) return;
|
2014-10-24 02:10:57 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mBoundVertexArray);
|
2020-02-26 03:44:26 +03:00
|
|
|
mBoundVertexArray->AttribDivisor(index, divisor);
|
2013-08-07 04:05:51 +04:00
|
|
|
gl->fVertexAttribDivisor(index, divisor);
|
|
|
|
}
|
2015-07-15 03:37:28 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|