2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-03-31 13:10:49 +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"
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
#include "GeckoProfiler.h"
|
2018-01-07 20:35:00 +03:00
|
|
|
#include "MozFramebuffer.h"
|
2014-03-31 13:10:49 +04:00
|
|
|
#include "GLContext.h"
|
|
|
|
#include "mozilla/CheckedInt.h"
|
2015-11-06 00:24:24 +03:00
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2016-07-22 09:25:41 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2014-03-31 13:10:49 +04:00
|
|
|
#include "WebGLBuffer.h"
|
2014-05-02 07:15:58 +04:00
|
|
|
#include "WebGLContextUtils.h"
|
2014-03-31 13:10:49 +04:00
|
|
|
#include "WebGLFramebuffer.h"
|
|
|
|
#include "WebGLProgram.h"
|
|
|
|
#include "WebGLRenderbuffer.h"
|
|
|
|
#include "WebGLShader.h"
|
|
|
|
#include "WebGLTexture.h"
|
2017-01-12 03:03:56 +03:00
|
|
|
#include "WebGLTransformFeedback.h"
|
2014-03-31 13:10:49 +04:00
|
|
|
#include "WebGLVertexArray.h"
|
|
|
|
|
2016-11-18 04:50:38 +03:00
|
|
|
#include <algorithm>
|
|
|
|
|
2015-07-15 03:37:28 +03:00
|
|
|
namespace mozilla {
|
2014-03-31 13:10:49 +04:00
|
|
|
|
|
|
|
// For a Tegra workaround.
|
|
|
|
static const int MAX_DRAW_CALLS_SINCE_FLUSH = 100;
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
class ScopedResolveTexturesForDraw {
|
|
|
|
struct TexRebindRequest {
|
|
|
|
uint32_t texUnit;
|
|
|
|
WebGLTexture* tex;
|
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
WebGLContext* const mWebGL;
|
|
|
|
std::vector<TexRebindRequest> mRebindRequests;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
public:
|
2018-07-27 07:46:33 +03:00
|
|
|
ScopedResolveTexturesForDraw(WebGLContext* webgl, bool* const out_error);
|
2015-11-25 07:15:29 +03:00
|
|
|
~ScopedResolveTexturesForDraw();
|
|
|
|
};
|
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
static bool ValidateNoSamplingFeedback(const WebGLTexture& tex,
|
|
|
|
const uint32_t sampledLevels,
|
|
|
|
const WebGLFramebuffer* const fb,
|
|
|
|
const uint32_t texUnit) {
|
|
|
|
if (!fb) return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
const auto& texAttachments = fb->GetCompletenessInfo()->texAttachments;
|
|
|
|
for (const auto& attach : texAttachments) {
|
|
|
|
if (attach->Texture() != &tex) continue;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
const auto& srcBase = tex.BaseMipmapLevel();
|
|
|
|
const auto srcLast = srcBase + sampledLevels - 1;
|
|
|
|
const auto& dstLevel = attach->MipLevel();
|
|
|
|
if (MOZ_UNLIKELY(srcBase <= dstLevel && dstLevel <= srcLast)) {
|
|
|
|
const auto& webgl = tex.mContext;
|
|
|
|
const auto& texTargetStr = EnumString(tex.Target().get());
|
|
|
|
const auto& attachStr = EnumString(attach->mAttachmentPoint);
|
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"Texture level %u would be read by %s unit %u,"
|
|
|
|
" but written by framebuffer attachment %s,"
|
|
|
|
" which would be illegal feedback.",
|
|
|
|
dstLevel, texTargetStr.c_str(), texUnit, attachStr.c_str());
|
|
|
|
return false;
|
2016-07-14 22:13:19 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 07:18:15 +03:00
|
|
|
return true;
|
2016-07-14 22:13:19 +03:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
ScopedResolveTexturesForDraw::ScopedResolveTexturesForDraw(
|
|
|
|
WebGLContext* webgl, bool* const out_error)
|
|
|
|
: mWebGL(webgl) {
|
2016-07-19 10:36:54 +03:00
|
|
|
const auto& fb = mWebGL->mBoundDrawFramebuffer;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-14 22:13:19 +03:00
|
|
|
MOZ_ASSERT(mWebGL->mActiveProgramLinkInfo);
|
2020-01-09 01:19:16 +03:00
|
|
|
const auto& samplerUniforms = mWebGL->mActiveProgramLinkInfo->samplerUniforms;
|
|
|
|
for (const auto& pUniform : samplerUniforms) {
|
|
|
|
const auto& uniform = *pUniform;
|
|
|
|
const auto& texList = uniform.texListForType;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
const auto& uniformBaseType = uniform.texBaseType;
|
|
|
|
for (const auto& texUnit : uniform.texUnits) {
|
|
|
|
MOZ_ASSERT(texUnit < texList.Length());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
const auto& tex = texList[texUnit];
|
|
|
|
if (!tex) continue;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
const auto& sampler = mWebGL->mBoundSamplers[texUnit];
|
|
|
|
const auto& samplingInfo = tex->GetSampleableInfo(sampler.get());
|
|
|
|
if (!samplingInfo) { // There was an error.
|
2016-07-14 22:13:19 +03:00
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
2018-10-17 07:18:15 +03:00
|
|
|
if (!samplingInfo->IsComplete()) {
|
|
|
|
if (samplingInfo->incompleteReason) {
|
|
|
|
const auto& targetName = GetEnumName(tex->Target().get());
|
|
|
|
mWebGL->GenerateWarning("%s at unit %u is incomplete: %s", targetName,
|
|
|
|
texUnit, samplingInfo->incompleteReason);
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
2018-10-17 07:18:15 +03:00
|
|
|
mRebindRequests.push_back({texUnit, tex});
|
2015-11-25 07:15:29 +03:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
// We have more validation to do if we're otherwise complete:
|
|
|
|
const auto& texBaseType = samplingInfo->usage->format->baseType;
|
|
|
|
if (texBaseType != uniformBaseType) {
|
|
|
|
const auto& targetName = GetEnumName(tex->Target().get());
|
|
|
|
const auto& srcType = ToString(texBaseType);
|
|
|
|
const auto& dstType = ToString(uniformBaseType);
|
2018-07-27 07:46:33 +03:00
|
|
|
mWebGL->ErrorInvalidOperation(
|
2018-10-17 07:18:15 +03:00
|
|
|
"%s at unit %u is of type %s, but"
|
|
|
|
" the shader samples as %s.",
|
|
|
|
targetName, texUnit, srcType, dstType);
|
2016-07-14 22:13:19 +03:00
|
|
|
*out_error = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
if (uniform.isShadowSampler != samplingInfo->isDepthTexCompare) {
|
2018-10-17 07:18:15 +03:00
|
|
|
const auto& targetName = GetEnumName(tex->Target().get());
|
2018-07-27 07:46:33 +03:00
|
|
|
mWebGL->ErrorInvalidOperation(
|
2018-10-17 07:18:15 +03:00
|
|
|
"%s at unit %u is%s a depth texture"
|
|
|
|
" with TEXTURE_COMPARE_MODE, but"
|
|
|
|
" the shader sampler is%s a shadow"
|
|
|
|
" sampler.",
|
|
|
|
targetName, texUnit, samplingInfo->isDepthTexCompare ? "" : " not",
|
2020-01-09 01:19:16 +03:00
|
|
|
uniform.isShadowSampler ? "" : " not");
|
2016-07-27 04:42:09 +03:00
|
|
|
*out_error = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-12-15 04:38:33 +03:00
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
if (!ValidateNoSamplingFeedback(*tex, samplingInfo->levels, fb.get(),
|
|
|
|
texUnit)) {
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-10-17 07:18:15 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
const auto& gl = mWebGL->gl;
|
|
|
|
for (const auto& itr : mRebindRequests) {
|
|
|
|
gl->fActiveTexture(LOCAL_GL_TEXTURE0 + itr.texUnit);
|
2020-07-04 12:38:43 +03:00
|
|
|
GLuint incompleteTex = 0; // Tex 0 is always incomplete.
|
2020-07-02 04:42:21 +03:00
|
|
|
const auto& overrideTex = webgl->mIncompleteTexOverride;
|
|
|
|
if (overrideTex) {
|
2020-07-04 12:38:43 +03:00
|
|
|
// In all but the simplest cases, this will be incomplete anyway, since
|
|
|
|
// e.g. int-samplers need int-textures. This is useful for e.g.
|
|
|
|
// dom-to-texture failures, though.
|
2020-07-02 04:42:21 +03:00
|
|
|
incompleteTex = overrideTex->name;
|
|
|
|
}
|
|
|
|
gl->fBindTexture(itr.tex->Target().get(), incompleteTex);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedResolveTexturesForDraw::~ScopedResolveTexturesForDraw() {
|
2017-08-03 13:01:38 +03:00
|
|
|
if (mRebindRequests.empty()) return;
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
gl::GLContext* gl = mWebGL->gl;
|
|
|
|
|
|
|
|
for (const auto& itr : mRebindRequests) {
|
|
|
|
gl->fActiveTexture(LOCAL_GL_TEXTURE0 + itr.texUnit);
|
|
|
|
gl->fBindTexture(itr.tex->Target().get(), itr.tex->mGLName);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->fActiveTexture(LOCAL_GL_TEXTURE0 + mWebGL->mActiveTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
bool WebGLContext::ValidateStencilParamsForDrawCall() const {
|
2018-03-10 03:48:51 +03:00
|
|
|
const auto stencilBits = [&]() -> uint8_t {
|
|
|
|
if (!mStencilTestEnabled) return 0;
|
|
|
|
|
|
|
|
if (!mBoundDrawFramebuffer) return mOptions.stencil ? 8 : 0;
|
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
if (mBoundDrawFramebuffer->StencilAttachment().HasAttachment()) return 8;
|
2018-03-10 03:48:51 +03:00
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
if (mBoundDrawFramebuffer->DepthStencilAttachment().HasAttachment())
|
2018-03-10 03:48:51 +03:00
|
|
|
return 8;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}();
|
|
|
|
const uint32_t stencilMax = (1 << stencilBits) - 1;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-10 03:48:51 +03:00
|
|
|
const auto fnMask = [&](const uint32_t x) { return x & stencilMax; };
|
|
|
|
const auto fnClamp = [&](const int32_t x) {
|
|
|
|
return std::max(0, std::min(x, (int32_t)stencilMax));
|
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-10 03:48:51 +03:00
|
|
|
bool ok = true;
|
|
|
|
ok &= (fnMask(mStencilWriteMaskFront) == fnMask(mStencilWriteMaskBack));
|
|
|
|
ok &= (fnMask(mStencilValueMaskFront) == fnMask(mStencilValueMaskBack));
|
|
|
|
ok &= (fnClamp(mStencilRefFront) == fnClamp(mStencilRefBack));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-10 03:48:51 +03:00
|
|
|
if (!ok) {
|
2018-07-27 07:46:33 +03:00
|
|
|
ErrorInvalidOperation(
|
|
|
|
"Stencil front/back state must effectively match."
|
2018-03-10 03:48:51 +03:00
|
|
|
" (before front/back comparison, WRITEMASK and VALUE_MASK"
|
|
|
|
" are masked with (2^s)-1, and REF is clamped to"
|
|
|
|
" [0, (2^s)-1], where `s` is the number of enabled stencil"
|
2018-07-27 07:46:33 +03:00
|
|
|
" bits in the draw framebuffer)");
|
2018-03-10 03:48:51 +03:00
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2019-06-30 04:29:42 +03:00
|
|
|
// -
|
|
|
|
|
2019-07-03 05:22:52 +03:00
|
|
|
void WebGLContext::GenErrorIllegalUse(const GLenum useTarget,
|
|
|
|
const uint32_t useId,
|
|
|
|
const GLenum boundTarget,
|
|
|
|
const uint32_t boundId) const {
|
2019-06-30 04:29:42 +03:00
|
|
|
const auto fnName = [&](const GLenum target, const uint32_t id) {
|
|
|
|
auto name = nsCString(EnumString(target).c_str());
|
|
|
|
if (id != static_cast<uint32_t>(-1)) {
|
|
|
|
name += nsPrintfCString("[%u]", id);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
};
|
|
|
|
const auto& useName = fnName(useTarget, useId);
|
|
|
|
const auto& boundName = fnName(boundTarget, boundId);
|
2019-07-03 05:22:52 +03:00
|
|
|
GenerateError(LOCAL_GL_INVALID_OPERATION,
|
|
|
|
"Illegal use of buffer at %s"
|
|
|
|
" while also bound to %s.",
|
|
|
|
useName.BeginReading(), boundName.BeginReading());
|
2019-06-30 04:29:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebGLContext::ValidateBufferForNonTf(const WebGLBuffer& nonTfBuffer,
|
|
|
|
const GLenum nonTfTarget,
|
|
|
|
const uint32_t nonTfId) const {
|
|
|
|
bool dupe = false;
|
|
|
|
const auto& tfAttribs = mBoundTransformFeedback->mIndexedBindings;
|
|
|
|
for (const auto& cur : tfAttribs) {
|
|
|
|
dupe |= (&nonTfBuffer == cur.mBufferBinding.get());
|
|
|
|
}
|
|
|
|
if (MOZ_LIKELY(!dupe)) return true;
|
|
|
|
|
|
|
|
dupe = false;
|
|
|
|
for (const auto tfId : IntegerRange(tfAttribs.size())) {
|
|
|
|
const auto& tfBuffer = tfAttribs[tfId].mBufferBinding;
|
|
|
|
if (&nonTfBuffer == tfBuffer) {
|
|
|
|
dupe = true;
|
2019-07-03 05:22:52 +03:00
|
|
|
GenErrorIllegalUse(nonTfTarget, nonTfId,
|
2019-06-30 04:29:42 +03:00
|
|
|
LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER, tfId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(dupe);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebGLContext::ValidateBuffersForTf(
|
|
|
|
const WebGLTransformFeedback& tfo,
|
|
|
|
const webgl::LinkedProgramInfo& linkInfo) const {
|
|
|
|
size_t numUsed;
|
|
|
|
switch (linkInfo.transformFeedbackBufferMode) {
|
|
|
|
case LOCAL_GL_INTERLEAVED_ATTRIBS:
|
|
|
|
numUsed = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_SEPARATE_ATTRIBS:
|
2020-01-09 01:19:16 +03:00
|
|
|
numUsed = linkInfo.active.activeTfVaryings.size();
|
2019-06-30 04:29:42 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<webgl::BufferAndIndex> tfBuffers;
|
|
|
|
tfBuffers.reserve(numUsed);
|
|
|
|
for (const auto i : IntegerRange(numUsed)) {
|
|
|
|
tfBuffers.push_back({tfo.mIndexedBindings[i].mBufferBinding.get(),
|
|
|
|
static_cast<uint32_t>(i)});
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidateBuffersForTf(tfBuffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebGLContext::ValidateBuffersForTf(
|
|
|
|
const std::vector<webgl::BufferAndIndex>& tfBuffers) const {
|
|
|
|
bool dupe = false;
|
|
|
|
const auto fnCheck = [&](const WebGLBuffer* const nonTf,
|
|
|
|
const GLenum nonTfTarget, const uint32_t nonTfId) {
|
|
|
|
for (const auto& tf : tfBuffers) {
|
|
|
|
dupe |= (nonTf && tf.buffer == nonTf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MOZ_LIKELY(!dupe)) return false;
|
|
|
|
|
|
|
|
for (const auto& tf : tfBuffers) {
|
|
|
|
if (nonTf && tf.buffer == nonTf) {
|
|
|
|
dupe = true;
|
2019-07-03 05:22:52 +03:00
|
|
|
GenErrorIllegalUse(LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER, tf.id,
|
2019-06-30 04:29:42 +03:00
|
|
|
nonTfTarget, nonTfId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
fnCheck(mBoundArrayBuffer.get(), LOCAL_GL_ARRAY_BUFFER, -1);
|
|
|
|
fnCheck(mBoundCopyReadBuffer.get(), LOCAL_GL_COPY_READ_BUFFER, -1);
|
|
|
|
fnCheck(mBoundCopyWriteBuffer.get(), LOCAL_GL_COPY_WRITE_BUFFER, -1);
|
|
|
|
fnCheck(mBoundPixelPackBuffer.get(), LOCAL_GL_PIXEL_PACK_BUFFER, -1);
|
|
|
|
fnCheck(mBoundPixelUnpackBuffer.get(), LOCAL_GL_PIXEL_UNPACK_BUFFER, -1);
|
|
|
|
// fnCheck(mBoundTransformFeedbackBuffer.get(),
|
|
|
|
// LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER, -1);
|
|
|
|
fnCheck(mBoundUniformBuffer.get(), LOCAL_GL_UNIFORM_BUFFER, -1);
|
|
|
|
|
|
|
|
for (const auto i : IntegerRange(mIndexedUniformBufferBindings.size())) {
|
|
|
|
const auto& cur = mIndexedUniformBufferBindings[i];
|
|
|
|
fnCheck(cur.mBufferBinding.get(), LOCAL_GL_UNIFORM_BUFFER, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
fnCheck(mBoundVertexArray->mElementArrayBuffer.get(),
|
|
|
|
LOCAL_GL_ELEMENT_ARRAY_BUFFER, -1);
|
2020-02-26 03:44:26 +03:00
|
|
|
for (const auto i : IntegerRange(MaxVertexAttribs())) {
|
|
|
|
const auto& binding = mBoundVertexArray->AttribBinding(i);
|
|
|
|
fnCheck(binding.buffer.get(), LOCAL_GL_ARRAY_BUFFER, i);
|
2019-06-30 04:29:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return !dupe;
|
|
|
|
}
|
|
|
|
|
2018-03-10 03:48:51 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2016-11-18 04:50:38 +03:00
|
|
|
template <typename T>
|
|
|
|
static bool DoSetsIntersect(const std::set<T>& a, const std::set<T>& b) {
|
|
|
|
std::vector<T> intersection;
|
|
|
|
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(),
|
|
|
|
std::back_inserter(intersection));
|
|
|
|
return bool(intersection.size());
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
const webgl::CachedDrawFetchLimits* ValidateDraw(WebGLContext* const webgl,
|
|
|
|
const GLenum mode,
|
2018-07-30 18:33:10 +03:00
|
|
|
const uint32_t instanceCount) {
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!webgl->BindCurFBForDraw()) return nullptr;
|
2017-10-21 01:40:12 +03:00
|
|
|
|
2019-02-01 02:39:12 +03:00
|
|
|
const auto& fb = webgl->mBoundDrawFramebuffer;
|
2019-07-09 08:21:30 +03:00
|
|
|
if (fb && webgl->mBlendEnabled) {
|
2019-02-01 02:39:12 +03:00
|
|
|
const auto& info = *fb->GetCompletenessInfo();
|
|
|
|
if (info.hasFloat32) {
|
2019-07-09 08:21:30 +03:00
|
|
|
if (!webgl->IsExtensionEnabled(WebGLExtensionID::EXT_float_blend)) {
|
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"Float32 blending requires EXT_float_blend.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
webgl->WarnIfImplicit(WebGLExtensionID::EXT_float_blend);
|
2019-02-01 02:39:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
switch (mode) {
|
|
|
|
case LOCAL_GL_TRIANGLES:
|
|
|
|
case LOCAL_GL_TRIANGLE_STRIP:
|
|
|
|
case LOCAL_GL_TRIANGLE_FAN:
|
|
|
|
case LOCAL_GL_POINTS:
|
|
|
|
case LOCAL_GL_LINE_STRIP:
|
|
|
|
case LOCAL_GL_LINE_LOOP:
|
|
|
|
case LOCAL_GL_LINES:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
webgl->ErrorInvalidEnumInfo("mode", mode);
|
2018-07-30 18:33:10 +03:00
|
|
|
return nullptr;
|
2018-07-27 07:46:33 +03:00
|
|
|
}
|
2017-12-19 05:30:56 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!webgl->ValidateStencilParamsForDrawCall()) return nullptr;
|
2018-07-30 18:33:10 +03:00
|
|
|
|
|
|
|
if (!webgl->mActiveProgramLinkInfo) {
|
|
|
|
webgl->ErrorInvalidOperation("The current program is not linked.");
|
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-07-30 18:33:10 +03:00
|
|
|
const auto& linkInfo = webgl->mActiveProgramLinkInfo;
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// -
|
2018-07-30 18:33:10 +03:00
|
|
|
// Check UBO sizes.
|
2016-09-10 07:02:54 +03:00
|
|
|
|
2019-06-30 04:29:42 +03:00
|
|
|
for (const auto i : IntegerRange(linkInfo->uniformBlocks.size())) {
|
|
|
|
const auto& cur = linkInfo->uniformBlocks[i];
|
2020-01-09 01:19:16 +03:00
|
|
|
const auto& dataSize = cur.info.dataSize;
|
|
|
|
const auto& binding = cur.binding;
|
2018-07-30 18:33:10 +03:00
|
|
|
if (!binding) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorInvalidOperation("Buffer for uniform block is null.");
|
2018-07-30 18:33:10 +03:00
|
|
|
return nullptr;
|
2017-10-21 01:40:12 +03:00
|
|
|
}
|
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
const auto availByteCount = binding->ByteCount();
|
|
|
|
if (dataSize > availByteCount) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"Buffer for uniform block is smaller"
|
|
|
|
" than UNIFORM_BLOCK_DATA_SIZE.");
|
2018-07-30 18:33:10 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-09-10 07:02:54 +03:00
|
|
|
|
2019-06-30 04:29:42 +03:00
|
|
|
if (!webgl->ValidateBufferForNonTf(binding->mBufferBinding,
|
|
|
|
LOCAL_GL_UNIFORM_BUFFER, i))
|
2018-07-30 18:33:10 +03:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-09-10 07:02:54 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// -
|
2016-09-10 07:02:54 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
const auto& tfo = webgl->mBoundTransformFeedback;
|
|
|
|
if (tfo && tfo->IsActiveAndNotPaused()) {
|
2019-05-23 23:48:21 +03:00
|
|
|
if (fb) {
|
|
|
|
const auto& info = *fb->GetCompletenessInfo();
|
|
|
|
if (info.isMultiview) {
|
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"Cannot render to multiview with transform feedback.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 04:29:42 +03:00
|
|
|
if (!webgl->ValidateBuffersForTf(*tfo, *linkInfo)) return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-11-18 04:50:38 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
// -
|
2016-11-18 04:50:38 +03:00
|
|
|
|
2019-01-25 02:38:15 +03:00
|
|
|
const auto& fragOutputs = linkInfo->fragOutputs;
|
|
|
|
const auto fnValidateFragOutputType =
|
|
|
|
[&](const uint8_t loc, const webgl::TextureBaseType dstBaseType) {
|
|
|
|
const auto itr = fragOutputs.find(loc);
|
|
|
|
if (MOZ_UNLIKELY(itr == fragOutputs.end())) {
|
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"Program has no frag output at location %u, but"
|
|
|
|
" destination draw buffer has an attached"
|
|
|
|
" image.",
|
|
|
|
uint32_t(loc));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& info = itr->second;
|
|
|
|
const auto& srcBaseType = info.baseType;
|
|
|
|
if (MOZ_UNLIKELY(dstBaseType != srcBaseType)) {
|
|
|
|
const auto& srcStr = ToString(srcBaseType);
|
|
|
|
const auto& dstStr = ToString(dstBaseType);
|
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"Program frag output at location %u is type %s,"
|
|
|
|
" but destination draw buffer is type %s.",
|
|
|
|
uint32_t(loc), srcStr, dstStr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!webgl->mRasterizerDiscardEnabled) {
|
2019-05-23 23:48:21 +03:00
|
|
|
uint8_t fbZLayerCount = 1;
|
2019-01-25 02:38:15 +03:00
|
|
|
if (fb) {
|
2019-05-23 23:48:21 +03:00
|
|
|
const auto& info = *fb->GetCompletenessInfo();
|
|
|
|
fbZLayerCount = info.zLayerCount;
|
2019-01-25 02:38:15 +03:00
|
|
|
}
|
2019-05-23 23:48:21 +03:00
|
|
|
|
|
|
|
if (fbZLayerCount != linkInfo->zLayerCount) {
|
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"Multiview count mismatch: shader: %u, framebuffer: %u",
|
|
|
|
uint32_t{linkInfo->zLayerCount}, uint32_t{fbZLayerCount});
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-02-19 23:47:30 +03:00
|
|
|
|
|
|
|
if (webgl->mColorWriteMask) {
|
|
|
|
if (fb) {
|
|
|
|
for (const auto& attach : fb->ColorDrawBuffers()) {
|
|
|
|
const auto i =
|
|
|
|
uint8_t(attach->mAttachmentPoint - LOCAL_GL_COLOR_ATTACHMENT0);
|
|
|
|
const auto& imageInfo = attach->GetImageInfo();
|
|
|
|
if (!imageInfo) continue;
|
|
|
|
const auto& dstBaseType = imageInfo->mFormat->format->baseType;
|
|
|
|
if (!fnValidateFragOutputType(i, dstBaseType)) return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!fnValidateFragOutputType(0, webgl::TextureBaseType::Float))
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 02:38:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// -
|
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
const auto fetchLimits = linkInfo->GetDrawFetchLimits();
|
|
|
|
if (!fetchLimits) return nullptr;
|
2016-12-29 11:08:39 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
if (instanceCount > fetchLimits->maxInstances) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorInvalidOperation(
|
|
|
|
"Instance fetch requires %u, but attribs only"
|
2018-07-30 18:33:10 +03:00
|
|
|
" supply %u.",
|
|
|
|
instanceCount, uint32_t(fetchLimits->maxInstances));
|
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 04:29:42 +03:00
|
|
|
if (tfo) {
|
|
|
|
for (const auto& used : fetchLimits->usedBuffers) {
|
|
|
|
MOZ_ASSERT(used.buffer);
|
|
|
|
if (!webgl->ValidateBufferForNonTf(*used.buffer, LOCAL_GL_ARRAY_BUFFER,
|
|
|
|
used.id))
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// -
|
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
webgl->RunContextLossTimer();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
return fetchLimits;
|
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
////////////////////////////////////////
|
2016-12-22 06:42:07 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
class ScopedFakeVertexAttrib0 final {
|
|
|
|
WebGLContext* const mWebGL;
|
|
|
|
bool mDidFake = false;
|
2016-12-22 06:42:07 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
public:
|
2018-07-27 07:46:33 +03:00
|
|
|
ScopedFakeVertexAttrib0(WebGLContext* const webgl, const uint64_t vertexCount,
|
2018-07-30 18:33:10 +03:00
|
|
|
bool* const out_error)
|
|
|
|
: mWebGL(webgl) {
|
|
|
|
*out_error = false;
|
2016-12-22 06:42:07 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!mWebGL->DoFakeVertexAttrib0(vertexCount)) {
|
2018-07-30 18:33:10 +03:00
|
|
|
*out_error = true;
|
|
|
|
return;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2018-07-30 18:33:10 +03:00
|
|
|
mDidFake = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
~ScopedFakeVertexAttrib0() {
|
2016-09-10 07:02:54 +03:00
|
|
|
if (mDidFake) {
|
|
|
|
mWebGL->UndoFakeVertexAttrib0();
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-09-10 07:02:54 +03:00
|
|
|
};
|
2014-06-11 04:23:49 +04:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
static uint32_t UsedVertsForTFDraw(GLenum mode, uint32_t vertCount) {
|
|
|
|
uint8_t vertsPerPrim;
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case LOCAL_GL_POINTS:
|
|
|
|
vertsPerPrim = 1;
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_LINES:
|
|
|
|
vertsPerPrim = 2;
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_TRIANGLES:
|
|
|
|
vertsPerPrim = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("`mode`");
|
|
|
|
}
|
|
|
|
|
|
|
|
return vertCount / vertsPerPrim * vertsPerPrim;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
class ScopedDrawWithTransformFeedback final {
|
|
|
|
WebGLContext* const mWebGL;
|
|
|
|
WebGLTransformFeedback* const mTFO;
|
|
|
|
const bool mWithTF;
|
|
|
|
uint32_t mUsedVerts;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
public:
|
2018-07-27 07:46:33 +03:00
|
|
|
ScopedDrawWithTransformFeedback(WebGLContext* webgl, GLenum mode,
|
2016-09-10 07:02:54 +03:00
|
|
|
uint32_t vertCount, uint32_t instanceCount,
|
|
|
|
bool* const out_error)
|
|
|
|
: mWebGL(webgl),
|
|
|
|
mTFO(mWebGL->mBoundTransformFeedback),
|
|
|
|
mWithTF(mTFO && mTFO->mIsActive && !mTFO->mIsPaused),
|
|
|
|
mUsedVerts(0) {
|
|
|
|
*out_error = false;
|
|
|
|
if (!mWithTF) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (mode != mTFO->mActive_PrimMode) {
|
2018-07-27 07:46:33 +03:00
|
|
|
mWebGL->ErrorInvalidOperation(
|
|
|
|
"Drawing with transform feedback requires"
|
2016-09-10 07:02:54 +03:00
|
|
|
" `mode` to match BeginTransformFeedback's"
|
2018-07-27 07:46:33 +03:00
|
|
|
" `primitiveMode`.");
|
2016-09-10 07:02:54 +03:00
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const auto usedVertsPerInstance = UsedVertsForTFDraw(mode, vertCount);
|
|
|
|
const auto usedVerts =
|
|
|
|
CheckedInt<uint32_t>(usedVertsPerInstance) * instanceCount;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const auto remainingCapacity =
|
|
|
|
mTFO->mActive_VertCapacity - mTFO->mActive_VertPosition;
|
|
|
|
if (!usedVerts.isValid() || usedVerts.value() > remainingCapacity) {
|
2018-07-27 07:46:33 +03:00
|
|
|
mWebGL->ErrorInvalidOperation(
|
|
|
|
"Insufficient buffer capacity remaining for"
|
|
|
|
" transform feedback.");
|
2016-09-10 07:02:54 +03:00
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
mUsedVerts = usedVerts.value();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
void Advance() const {
|
|
|
|
if (!mWithTF) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
mTFO->mActive_VertPosition += mUsedVerts;
|
2019-06-30 04:29:42 +03:00
|
|
|
|
|
|
|
for (const auto& cur : mTFO->mIndexedBindings) {
|
|
|
|
const auto& buffer = cur.mBufferBinding;
|
|
|
|
if (buffer) {
|
|
|
|
buffer->ResetLastUpdateFenceId();
|
|
|
|
}
|
|
|
|
}
|
2016-09-10 07:02:54 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-15 01:10:30 +03:00
|
|
|
static bool HasInstancedDrawing(const WebGLContext& webgl) {
|
|
|
|
return webgl.IsWebGL2() ||
|
|
|
|
webgl.IsExtensionEnabled(WebGLExtensionID::ANGLE_instanced_arrays);
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
void WebGLContext::DrawArraysInstanced(GLenum mode, GLint first,
|
|
|
|
GLsizei vertCount,
|
2018-07-27 07:46:33 +03:00
|
|
|
GLsizei instanceCount) {
|
|
|
|
const FuncScope funcScope(*this, "drawArraysInstanced");
|
2018-07-30 18:33:10 +03:00
|
|
|
AUTO_PROFILER_LABEL("WebGLContext::DrawArraysInstanced", GRAPHICS);
|
|
|
|
if (IsContextLost()) return;
|
|
|
|
const gl::GLContext::TlsScope inTls(gl);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
// -
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!ValidateNonNegative("first", first) ||
|
|
|
|
!ValidateNonNegative("vertCount", vertCount) ||
|
|
|
|
!ValidateNonNegative("instanceCount", instanceCount)) {
|
2018-07-30 18:33:10 +03:00
|
|
|
return;
|
2017-10-21 01:40:12 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
if (IsWebGL2() && !gl->IsSupported(gl::GLFeature::prim_restart_fixed)) {
|
|
|
|
MOZ_ASSERT(gl->IsSupported(gl::GLFeature::prim_restart));
|
|
|
|
if (mPrimRestartTypeBytes != 0) {
|
|
|
|
mPrimRestartTypeBytes = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
// OSX appears to have severe perf issues with leaving this enabled.
|
|
|
|
gl->fDisable(LOCAL_GL_PRIMITIVE_RESTART);
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
// -
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
const auto fetchLimits = ValidateDraw(this, mode, instanceCount);
|
2018-07-30 18:33:10 +03:00
|
|
|
if (!fetchLimits) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
// -
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
const auto totalVertCount_safe = CheckedInt<uint32_t>(first) + vertCount;
|
|
|
|
if (!totalVertCount_safe.isValid()) {
|
2018-07-27 07:46:33 +03:00
|
|
|
ErrorOutOfMemory("`first+vertCount` out of range.");
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
2018-07-30 18:33:10 +03:00
|
|
|
}
|
|
|
|
auto totalVertCount = totalVertCount_safe.value();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
if (vertCount && instanceCount && totalVertCount > fetchLimits->maxVerts) {
|
2018-07-27 07:46:33 +03:00
|
|
|
ErrorInvalidOperation(
|
|
|
|
"Vertex fetch requires %u, but attribs only supply %u.", totalVertCount,
|
|
|
|
uint32_t(fetchLimits->maxVerts));
|
2018-07-30 18:33:10 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
// -
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-12-19 05:30:56 +03:00
|
|
|
bool error = false;
|
2018-07-27 07:46:33 +03:00
|
|
|
const ScopedFakeVertexAttrib0 attrib0(this, totalVertCount, &error);
|
2016-09-10 07:02:54 +03:00
|
|
|
if (error) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
const ScopedResolveTexturesForDraw scopedResolve(this, &error);
|
2017-12-19 05:30:56 +03:00
|
|
|
if (error) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
const ScopedDrawWithTransformFeedback scopedTF(this, mode, vertCount,
|
2016-09-10 07:02:54 +03:00
|
|
|
instanceCount, &error);
|
|
|
|
if (error) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-10-02 04:05:34 +04:00
|
|
|
{
|
2016-12-21 10:31:22 +03:00
|
|
|
ScopedDrawCallWrapper wrapper(*this);
|
2017-10-21 01:40:12 +03:00
|
|
|
if (vertCount && instanceCount) {
|
|
|
|
AUTO_PROFILER_LABEL("glDrawArraysInstanced", GRAPHICS);
|
2017-12-15 01:10:30 +03:00
|
|
|
if (HasInstancedDrawing(*this)) {
|
|
|
|
gl->fDrawArraysInstanced(mode, first, vertCount, instanceCount);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(instanceCount == 1);
|
|
|
|
gl->fDrawArrays(mode, first, vertCount);
|
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
}
|
2014-10-02 04:05:34 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
Draw_cleanup();
|
2016-09-10 07:02:54 +03:00
|
|
|
scopedTF.Advance();
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
WebGLBuffer* WebGLContext::DrawElements_check(const GLsizei rawIndexCount,
|
2017-10-21 01:40:12 +03:00
|
|
|
const GLenum type,
|
|
|
|
const WebGLintptr byteOffset,
|
2018-07-30 18:33:10 +03:00
|
|
|
const GLsizei instanceCount) {
|
2016-09-10 07:02:54 +03:00
|
|
|
if (mBoundTransformFeedback && mBoundTransformFeedback->mIsActive &&
|
|
|
|
!mBoundTransformFeedback->mIsPaused) {
|
2018-07-27 07:46:33 +03:00
|
|
|
ErrorInvalidOperation(
|
|
|
|
"DrawElements* functions are incompatible with"
|
|
|
|
" transform feedback.");
|
2018-07-30 18:33:10 +03:00
|
|
|
return nullptr;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
if (!ValidateNonNegative("vertCount", rawIndexCount) ||
|
|
|
|
!ValidateNonNegative("byteOffset", byteOffset) ||
|
|
|
|
!ValidateNonNegative("instanceCount", instanceCount)) {
|
2018-07-30 18:33:10 +03:00
|
|
|
return nullptr;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
const auto indexCount = uint32_t(rawIndexCount);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
uint8_t bytesPerIndex = 0;
|
2015-11-25 07:15:29 +03:00
|
|
|
switch (type) {
|
|
|
|
case LOCAL_GL_UNSIGNED_BYTE:
|
2017-10-21 01:40:12 +03:00
|
|
|
bytesPerIndex = 1;
|
2015-11-25 07:15:29 +03:00
|
|
|
break;
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
case LOCAL_GL_UNSIGNED_SHORT:
|
2017-10-21 01:40:12 +03:00
|
|
|
bytesPerIndex = 2;
|
2015-11-25 07:15:29 +03:00
|
|
|
break;
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
case LOCAL_GL_UNSIGNED_INT:
|
|
|
|
if (IsWebGL2() ||
|
|
|
|
IsExtensionEnabled(WebGLExtensionID::OES_element_index_uint)) {
|
2017-10-21 01:40:12 +03:00
|
|
|
bytesPerIndex = 4;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
break;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
if (!bytesPerIndex) {
|
2018-07-27 07:46:33 +03:00
|
|
|
ErrorInvalidEnumInfo("type", type);
|
2018-07-30 18:33:10 +03:00
|
|
|
return nullptr;
|
2015-11-24 08:55:59 +03:00
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
if (byteOffset % bytesPerIndex != 0) {
|
2018-07-27 07:46:33 +03:00
|
|
|
ErrorInvalidOperation(
|
|
|
|
"`byteOffset` must be a multiple of the size of `type`");
|
2018-07-30 18:33:10 +03:00
|
|
|
return nullptr;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-27 06:05:45 +03:00
|
|
|
////
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-27 06:05:45 +03:00
|
|
|
if (IsWebGL2() && !gl->IsSupported(gl::GLFeature::prim_restart_fixed)) {
|
|
|
|
MOZ_ASSERT(gl->IsSupported(gl::GLFeature::prim_restart));
|
2017-10-21 01:40:12 +03:00
|
|
|
if (mPrimRestartTypeBytes != bytesPerIndex) {
|
|
|
|
mPrimRestartTypeBytes = bytesPerIndex;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-21 03:01:52 +03:00
|
|
|
const uint32_t ones = UINT32_MAX >> (32 - 8 * mPrimRestartTypeBytes);
|
2016-12-17 03:17:29 +03:00
|
|
|
gl->fEnable(LOCAL_GL_PRIMITIVE_RESTART);
|
2016-07-27 06:05:45 +03:00
|
|
|
gl->fPrimitiveRestartIndex(ones);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-27 06:05:45 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-27 06:05:45 +03:00
|
|
|
////
|
2017-10-21 01:40:12 +03:00
|
|
|
// Index fetching
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-30 03:28:56 +03:00
|
|
|
const auto& indexBuffer = mBoundVertexArray->mElementArrayBuffer;
|
|
|
|
if (!indexBuffer) {
|
2018-07-27 07:46:33 +03:00
|
|
|
ErrorInvalidOperation("Index buffer not bound.");
|
2018-07-30 18:33:10 +03:00
|
|
|
return nullptr;
|
2018-03-30 03:28:56 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-30 03:28:56 +03:00
|
|
|
const size_t availBytes = indexBuffer->ByteLength();
|
2017-10-21 01:40:12 +03:00
|
|
|
const auto availIndices =
|
|
|
|
AvailGroups(availBytes, byteOffset, bytesPerIndex, bytesPerIndex);
|
2018-07-30 18:33:10 +03:00
|
|
|
if (instanceCount && indexCount > availIndices) {
|
2018-07-27 07:46:33 +03:00
|
|
|
ErrorInvalidOperation("Index buffer too small.");
|
2018-07-30 18:33:10 +03:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
return indexBuffer.get();
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
static void HandleDrawElementsErrors(
|
2016-12-21 05:44:28 +03:00
|
|
|
WebGLContext* webgl, gl::GLContext::LocalErrorScope& errorScope) {
|
2018-07-30 18:33:10 +03:00
|
|
|
const auto err = errorScope.GetError();
|
2016-12-21 05:44:28 +03:00
|
|
|
if (err == LOCAL_GL_INVALID_OPERATION) {
|
2018-07-27 07:46:33 +03:00
|
|
|
webgl->ErrorInvalidOperation(
|
2018-07-30 18:33:10 +03:00
|
|
|
"Driver rejected indexed draw call, possibly"
|
2018-07-27 07:46:33 +03:00
|
|
|
" due to out-of-bounds indices.");
|
2018-07-30 18:33:10 +03:00
|
|
|
return;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
MOZ_ASSERT(!err);
|
2016-12-21 05:44:28 +03:00
|
|
|
if (err) {
|
2018-07-30 18:33:10 +03:00
|
|
|
webgl->ErrorImplementationBug(
|
2018-07-27 07:46:33 +03:00
|
|
|
"Unexpected driver error during indexed draw"
|
|
|
|
" call. Please file a bug.");
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2016-12-21 05:44:28 +03:00
|
|
|
void WebGLContext::DrawElementsInstanced(GLenum mode, GLsizei indexCount,
|
2018-07-27 07:46:33 +03:00
|
|
|
GLenum type, WebGLintptr byteOffset,
|
|
|
|
GLsizei instanceCount) {
|
|
|
|
const FuncScope funcScope(*this, "drawElementsInstanced");
|
|
|
|
AUTO_PROFILER_LABEL("WebGLContext::DrawElementsInstanced", GRAPHICS);
|
2016-12-21 05:44:28 +03:00
|
|
|
if (IsContextLost()) return;
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
const gl::GLContext::TlsScope inTls(gl);
|
2016-12-21 05:44:28 +03:00
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
const auto indexBuffer =
|
|
|
|
DrawElements_check(indexCount, type, byteOffset, instanceCount);
|
2014-03-31 13:10:49 +04:00
|
|
|
if (!indexBuffer) return;
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// -
|
2017-11-29 04:11:12 +03:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
const auto fetchLimits = ValidateDraw(this, mode, instanceCount);
|
2014-03-31 13:10:49 +04:00
|
|
|
if (!fetchLimits) return;
|
2018-07-30 18:33:10 +03:00
|
|
|
|
|
|
|
bool collapseToDrawArrays = false;
|
|
|
|
auto fakeVertCount = fetchLimits->maxVerts;
|
|
|
|
if (fetchLimits->maxVerts == UINT64_MAX) {
|
|
|
|
// This isn't observable, and keeps FakeVertexAttrib0 sane.
|
|
|
|
collapseToDrawArrays = true;
|
|
|
|
fakeVertCount = 1;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-07-30 18:33:10 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// -
|
2018-07-30 18:33:10 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
uint64_t indexCapacity = indexBuffer->ByteLength();
|
|
|
|
switch (type) {
|
|
|
|
case LOCAL_GL_UNSIGNED_BYTE:
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_UNSIGNED_SHORT:
|
|
|
|
indexCapacity /= 2;
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2018-07-30 18:33:10 +03:00
|
|
|
case LOCAL_GL_UNSIGNED_INT:
|
|
|
|
indexCapacity /= 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t maxVertId = 0;
|
|
|
|
const auto isFetchValid = [&]() {
|
|
|
|
if (!indexCount || !instanceCount) return true;
|
|
|
|
|
|
|
|
const auto globalMaxVertId =
|
|
|
|
indexBuffer->GetIndexedFetchMaxVert(type, 0, indexCapacity);
|
|
|
|
if (!globalMaxVertId) return true;
|
|
|
|
if (globalMaxVertId.value() < fetchLimits->maxVerts) return true;
|
|
|
|
|
|
|
|
const auto exactMaxVertId =
|
|
|
|
indexBuffer->GetIndexedFetchMaxVert(type, byteOffset, indexCount);
|
|
|
|
maxVertId = exactMaxVertId.value();
|
|
|
|
return maxVertId < fetchLimits->maxVerts;
|
|
|
|
}();
|
|
|
|
if (!isFetchValid) {
|
2018-07-27 07:46:33 +03:00
|
|
|
ErrorInvalidOperation(
|
|
|
|
"Indexed vertex fetch requires %u vertices, but"
|
2018-07-30 18:33:10 +03:00
|
|
|
" attribs only supply %u.",
|
2018-07-27 07:46:33 +03:00
|
|
|
maxVertId + 1, uint32_t(fetchLimits->maxVerts));
|
2018-07-30 18:33:10 +03:00
|
|
|
return;
|
2017-10-21 01:40:12 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2018-07-30 18:33:10 +03:00
|
|
|
// -
|
|
|
|
|
2017-12-19 05:30:56 +03:00
|
|
|
bool error = false;
|
2018-07-27 07:46:33 +03:00
|
|
|
const ScopedFakeVertexAttrib0 attrib0(this, fakeVertCount, &error);
|
2016-09-10 07:02:54 +03:00
|
|
|
if (error) return;
|
2014-10-02 04:05:34 +04:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
const ScopedResolveTexturesForDraw scopedResolve(this, &error);
|
2017-12-19 05:30:56 +03:00
|
|
|
if (error) return;
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
{
|
2016-12-21 10:31:22 +03:00
|
|
|
ScopedDrawCallWrapper wrapper(*this);
|
2014-10-02 04:05:34 +04:00
|
|
|
{
|
2016-12-21 05:44:28 +03:00
|
|
|
UniquePtr<gl::GLContext::LocalErrorScope> errorScope;
|
2018-07-04 01:40:58 +03:00
|
|
|
if (MOZ_UNLIKELY(gl->IsANGLE() &&
|
|
|
|
gl->mDebugFlags &
|
|
|
|
gl::GLContext::DebugFlagAbortOnError)) {
|
|
|
|
// ANGLE does range validation even when it doesn't need to.
|
|
|
|
// With MOZ_GL_ABORT_ON_ERROR, we need to catch it or hit assertions.
|
2016-12-21 05:44:28 +03:00
|
|
|
errorScope.reset(new gl::GLContext::LocalErrorScope(*gl));
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-11-07 02:29:16 +03:00
|
|
|
if (indexCount && instanceCount) {
|
2017-10-21 01:40:12 +03:00
|
|
|
AUTO_PROFILER_LABEL("glDrawElementsInstanced", GRAPHICS);
|
2017-12-15 01:10:30 +03:00
|
|
|
if (HasInstancedDrawing(*this)) {
|
2018-07-30 18:33:10 +03:00
|
|
|
if (MOZ_UNLIKELY(collapseToDrawArrays)) {
|
|
|
|
gl->fDrawArraysInstanced(mode, 0, 1, instanceCount);
|
|
|
|
} else {
|
|
|
|
gl->fDrawElementsInstanced(mode, indexCount, type,
|
|
|
|
reinterpret_cast<GLvoid*>(byteOffset),
|
|
|
|
instanceCount);
|
|
|
|
}
|
2017-12-15 01:10:30 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(instanceCount == 1);
|
2018-07-30 18:33:10 +03:00
|
|
|
if (MOZ_UNLIKELY(collapseToDrawArrays)) {
|
|
|
|
gl->fDrawArrays(mode, 0, 1);
|
|
|
|
} else {
|
|
|
|
gl->fDrawElements(mode, indexCount, type,
|
|
|
|
reinterpret_cast<GLvoid*>(byteOffset));
|
2016-12-21 05:44:28 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-12-21 05:44:28 +03:00
|
|
|
if (errorScope) {
|
2018-07-27 07:46:33 +03:00
|
|
|
HandleDrawElementsErrors(this, *errorScope);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-10-02 04:05:34 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
Draw_cleanup();
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
void WebGLContext::Draw_cleanup() {
|
2014-03-31 13:10:49 +04:00
|
|
|
if (gl->WorkAroundDriverBugs()) {
|
|
|
|
if (gl->Renderer() == gl::GLRenderer::Tegra) {
|
|
|
|
mDrawCallsSinceLastFlush++;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
if (mDrawCallsSinceLastFlush >= MAX_DRAW_CALLS_SINCE_FLUSH) {
|
|
|
|
gl->fFlush();
|
|
|
|
mDrawCallsSinceLastFlush = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
// Let's check for a really common error: Viewport is larger than the actual
|
|
|
|
// destination framebuffer.
|
2018-10-17 07:18:15 +03:00
|
|
|
uint32_t destWidth;
|
|
|
|
uint32_t destHeight;
|
2015-11-25 07:15:29 +03:00
|
|
|
if (mBoundDrawFramebuffer) {
|
2018-10-17 07:18:15 +03:00
|
|
|
const auto& info = mBoundDrawFramebuffer->GetCompletenessInfo();
|
|
|
|
destWidth = info->width;
|
|
|
|
destHeight = info->height;
|
2015-11-25 07:15:29 +03:00
|
|
|
} else {
|
2017-12-19 05:30:56 +03:00
|
|
|
destWidth = mDefaultFB->mSize.width;
|
|
|
|
destHeight = mDefaultFB->mSize.height;
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
if (mViewportWidth > int32_t(destWidth) ||
|
|
|
|
mViewportHeight > int32_t(destHeight)) {
|
|
|
|
if (!mAlreadyWarnedAboutViewportLargerThanDest) {
|
2018-07-27 07:46:33 +03:00
|
|
|
GenerateWarning(
|
|
|
|
"Drawing to a destination rect smaller than the viewport"
|
|
|
|
" rect. (This warning will only be given once)");
|
2015-11-25 07:15:29 +03:00
|
|
|
mAlreadyWarnedAboutViewportLargerThanDest = true;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
WebGLVertexAttrib0Status WebGLContext::WhatDoesVertexAttrib0Need() const {
|
2014-03-31 13:10:49 +04:00
|
|
|
MOZ_ASSERT(mCurrentProgram);
|
2015-01-16 02:40:39 +03:00
|
|
|
MOZ_ASSERT(mActiveProgramLinkInfo);
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-12-30 08:44:51 +03:00
|
|
|
bool legacyAttrib0 = gl->IsCompatibilityProfile();
|
2014-03-31 13:10:49 +04:00
|
|
|
#ifdef XP_MACOSX
|
2016-12-30 08:44:51 +03:00
|
|
|
if (gl->WorkAroundDriverBugs()) {
|
|
|
|
// Failures in conformance/attribs/gl-disabled-vertex-attrib.
|
|
|
|
// Even in Core profiles on NV. Sigh.
|
|
|
|
legacyAttrib0 |= (gl->Vendor() == gl::GLVendor::NVIDIA);
|
2019-06-27 04:52:47 +03:00
|
|
|
|
|
|
|
// Also programs with no attribs:
|
|
|
|
// conformance/attribs/gl-vertex-attrib-unconsumed-out-of-bounds.html
|
2020-01-09 01:19:16 +03:00
|
|
|
legacyAttrib0 |= !mActiveProgramLinkInfo->active.activeAttribs.size();
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
if (!legacyAttrib0) return WebGLVertexAttrib0Status::Default;
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
if (!mActiveProgramLinkInfo->attrib0Active) {
|
|
|
|
// Ensure that the legacy code has enough buffer.
|
|
|
|
return WebGLVertexAttrib0Status::EmulatedUninitializedArray;
|
|
|
|
}
|
2016-12-30 08:44:51 +03:00
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
const auto& isAttribArray0Enabled =
|
|
|
|
mBoundVertexArray->AttribBinding(0).layout.isArray;
|
2017-10-21 01:40:12 +03:00
|
|
|
return isAttribArray0Enabled
|
|
|
|
? WebGLVertexAttrib0Status::Default
|
|
|
|
: WebGLVertexAttrib0Status::EmulatedInitializedArray;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2018-07-27 07:46:33 +03:00
|
|
|
bool WebGLContext::DoFakeVertexAttrib0(const uint64_t vertexCount) {
|
2016-12-22 06:42:07 +03:00
|
|
|
const auto whatDoesAttrib0Need = WhatDoesVertexAttrib0Need();
|
2014-03-31 13:10:49 +04:00
|
|
|
if (MOZ_LIKELY(whatDoesAttrib0Need == WebGLVertexAttrib0Status::Default))
|
2014-03-31 13:10:49 +04:00
|
|
|
return true;
|
|
|
|
|
2016-12-30 08:44:51 +03:00
|
|
|
if (!mAlreadyWarnedAboutFakeVertexAttrib0) {
|
|
|
|
GenerateWarning(
|
|
|
|
"Drawing without vertex attrib 0 array enabled forces the browser "
|
|
|
|
"to do expensive emulation work when running on desktop OpenGL "
|
|
|
|
"platforms, for example on Mac. It is preferable to always draw "
|
2014-03-31 13:10:49 +04:00
|
|
|
"with vertex attrib 0 array enabled, by using bindAttribLocation "
|
2016-12-30 08:44:51 +03:00
|
|
|
"to bind some always-used attribute to location 0.");
|
2014-03-31 13:10:49 +04:00
|
|
|
mAlreadyWarnedAboutFakeVertexAttrib0 = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-12-30 08:44:51 +03:00
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
gl->fEnableVertexAttribArray(0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
if (!mFakeVertexAttrib0BufferObject) {
|
|
|
|
gl->fGenBuffers(1, &mFakeVertexAttrib0BufferObject);
|
|
|
|
mFakeVertexAttrib0BufferObjectSize = 0;
|
|
|
|
}
|
|
|
|
gl->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mFakeVertexAttrib0BufferObject);
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
switch (mGenericVertexAttribTypes[0]) {
|
2018-11-10 00:38:31 +03:00
|
|
|
case webgl::AttribBaseType::Boolean:
|
2018-10-17 07:18:15 +03:00
|
|
|
case webgl::AttribBaseType::Float:
|
2016-12-22 06:42:07 +03:00
|
|
|
gl->fVertexAttribPointer(0, 4, LOCAL_GL_FLOAT, false, 0, 0);
|
|
|
|
break;
|
|
|
|
|
2018-10-17 07:18:15 +03:00
|
|
|
case webgl::AttribBaseType::Int:
|
2016-12-22 06:42:07 +03:00
|
|
|
gl->fVertexAttribIPointer(0, 4, LOCAL_GL_INT, 0, 0);
|
|
|
|
break;
|
|
|
|
|
2020-01-09 01:19:16 +03:00
|
|
|
case webgl::AttribBaseType::Uint:
|
2016-12-22 06:42:07 +03:00
|
|
|
gl->fVertexAttribIPointer(0, 4, LOCAL_GL_UNSIGNED_INT, 0, 0);
|
|
|
|
break;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
////
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
const auto bytesPerVert = sizeof(mFakeVertexAttrib0Data);
|
2018-07-30 18:33:10 +03:00
|
|
|
const auto checked_dataSize = CheckedUint32(vertexCount) * bytesPerVert;
|
2014-03-31 13:10:49 +04:00
|
|
|
if (!checked_dataSize.isValid()) {
|
2017-10-21 01:40:12 +03:00
|
|
|
ErrorOutOfMemory(
|
|
|
|
"Integer overflow trying to construct a fake vertex attrib 0"
|
|
|
|
" array for a draw-operation with %" PRIu64
|
|
|
|
" vertices. Try"
|
|
|
|
" reducing the number of vertices.",
|
2018-07-30 18:33:10 +03:00
|
|
|
vertexCount);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-22 06:42:07 +03:00
|
|
|
const auto dataSize = checked_dataSize.value();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
if (mFakeVertexAttrib0BufferObjectSize < dataSize) {
|
|
|
|
gl->fBufferData(LOCAL_GL_ARRAY_BUFFER, dataSize, nullptr,
|
|
|
|
LOCAL_GL_DYNAMIC_DRAW);
|
|
|
|
mFakeVertexAttrib0BufferObjectSize = dataSize;
|
|
|
|
mFakeVertexAttrib0DataDefined = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
if (whatDoesAttrib0Need ==
|
|
|
|
WebGLVertexAttrib0Status::EmulatedUninitializedArray)
|
|
|
|
return true;
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
////
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
if (mFakeVertexAttrib0DataDefined &&
|
|
|
|
memcmp(mFakeVertexAttrib0Data, mGenericVertexAttrib0Data, bytesPerVert) ==
|
2018-11-30 13:46:48 +03:00
|
|
|
0) {
|
2016-12-22 06:42:07 +03:00
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-12-22 06:42:07 +03:00
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
const UniqueBuffer data(malloc(dataSize));
|
|
|
|
if (!data) {
|
|
|
|
ErrorOutOfMemory("Failed to allocate fake vertex attrib 0 array.");
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-12-22 06:42:07 +03:00
|
|
|
auto itr = (uint8_t*)data.get();
|
|
|
|
const auto itrEnd = itr + dataSize;
|
|
|
|
while (itr != itrEnd) {
|
|
|
|
memcpy(itr, mGenericVertexAttrib0Data, bytesPerVert);
|
|
|
|
itr += bytesPerVert;
|
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
{
|
2016-12-22 06:42:07 +03:00
|
|
|
gl::GLContext::LocalErrorScope errorScope(*gl);
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
gl->fBufferSubData(LOCAL_GL_ARRAY_BUFFER, 0, dataSize, data.get());
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
const auto err = errorScope.GetError();
|
|
|
|
if (err) {
|
2018-07-27 07:46:33 +03:00
|
|
|
ErrorOutOfMemory("Failed to upload fake vertex attrib 0 data.");
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
////
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
memcpy(mFakeVertexAttrib0Data, mGenericVertexAttrib0Data, bytesPerVert);
|
|
|
|
mFakeVertexAttrib0DataDefined = true;
|
2014-03-31 13:10:49 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebGLContext::UndoFakeVertexAttrib0() {
|
2016-12-22 06:42:07 +03:00
|
|
|
const auto whatDoesAttrib0Need = WhatDoesVertexAttrib0Need();
|
2014-03-31 13:10:49 +04:00
|
|
|
if (MOZ_LIKELY(whatDoesAttrib0Need == WebGLVertexAttrib0Status::Default))
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-02-26 03:44:26 +03:00
|
|
|
const auto& binding = mBoundVertexArray->AttribBinding(0);
|
|
|
|
const auto& buffer = binding.buffer;
|
|
|
|
|
|
|
|
static_assert(IsBufferTargetLazilyBound(LOCAL_GL_ARRAY_BUFFER));
|
|
|
|
|
|
|
|
if (buffer) {
|
|
|
|
const auto& desc = mBoundVertexArray->AttribDesc(0);
|
|
|
|
|
|
|
|
gl->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, buffer->mGLName);
|
|
|
|
DoVertexAttribPointer(*gl, 0, desc);
|
2014-03-31 13:10:49 +04:00
|
|
|
gl->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 03:37:28 +03:00
|
|
|
} // namespace mozilla
|