2014-03-31 13:10:49 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* 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"
|
|
|
|
#include "WebGLVertexAttribData.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;
|
|
|
|
};
|
|
|
|
|
|
|
|
WebGLContext* const mWebGL;
|
|
|
|
std::vector<TexRebindRequest> mRebindRequests;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedResolveTexturesForDraw(WebGLContext* webgl, const char* funcName,
|
|
|
|
bool* const out_error);
|
|
|
|
~ScopedResolveTexturesForDraw();
|
|
|
|
};
|
|
|
|
|
2016-07-14 22:13:19 +03:00
|
|
|
bool
|
|
|
|
WebGLTexture::IsFeedback(WebGLContext* webgl, const char* funcName, uint32_t texUnit,
|
|
|
|
const std::vector<const WebGLFBAttachPoint*>& fbAttachments) const
|
|
|
|
{
|
|
|
|
auto itr = fbAttachments.cbegin();
|
|
|
|
for (; itr != fbAttachments.cend(); ++itr) {
|
|
|
|
const auto& attach = *itr;
|
|
|
|
if (attach->Texture() == this)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itr == fbAttachments.cend())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
const auto minLevel = mBaseMipmapLevel;
|
|
|
|
uint32_t maxLevel;
|
|
|
|
if (!MaxEffectiveMipmapLevel(texUnit, &maxLevel)) {
|
|
|
|
// No valid mips. Will need fake-black.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
for (; itr != fbAttachments.cend(); ++itr) {
|
|
|
|
const auto& attach = *itr;
|
|
|
|
if (attach->Texture() != this)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto dstLevel = attach->MipLevel();
|
|
|
|
|
|
|
|
if (minLevel <= dstLevel && dstLevel <= maxLevel) {
|
|
|
|
webgl->ErrorInvalidOperation("%s: Feedback loop detected between tex target"
|
|
|
|
" 0x%04x, tex unit %u, levels %u-%u; and"
|
|
|
|
" framebuffer attachment 0x%04x, level %u.",
|
|
|
|
funcName, mTarget.get(), texUnit, minLevel,
|
|
|
|
maxLevel, attach->mAttachmentPoint, dstLevel);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
ScopedResolveTexturesForDraw::ScopedResolveTexturesForDraw(WebGLContext* webgl,
|
|
|
|
const char* funcName,
|
|
|
|
bool* const out_error)
|
|
|
|
: mWebGL(webgl)
|
|
|
|
{
|
2016-07-14 22:13:19 +03:00
|
|
|
MOZ_ASSERT(mWebGL->gl->IsCurrent());
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
const std::vector<const WebGLFBAttachPoint*>* attachList = nullptr;
|
|
|
|
const auto& fb = mWebGL->mBoundDrawFramebuffer;
|
|
|
|
if (fb) {
|
|
|
|
attachList = &(fb->ResolvedCompleteData()->texDrawBuffers);
|
2016-07-14 22:13:19 +03:00
|
|
|
}
|
2016-03-18 09:00:34 +03:00
|
|
|
|
2016-07-14 22:13:19 +03:00
|
|
|
MOZ_ASSERT(mWebGL->mActiveProgramLinkInfo);
|
|
|
|
const auto& uniformSamplers = mWebGL->mActiveProgramLinkInfo->uniformSamplers;
|
|
|
|
for (const auto& uniform : uniformSamplers) {
|
|
|
|
const auto& texList = *(uniform->mSamplerTexList);
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2016-07-14 22:13:19 +03:00
|
|
|
for (const auto& texUnit : uniform->mSamplerValues) {
|
|
|
|
if (texUnit >= texList.Length())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto& tex = texList[texUnit];
|
2015-11-25 07:15:29 +03:00
|
|
|
if (!tex)
|
|
|
|
continue;
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
if (attachList &&
|
|
|
|
tex->IsFeedback(mWebGL, funcName, texUnit, *attachList))
|
|
|
|
{
|
2016-07-14 22:13:19 +03:00
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
FakeBlackType fakeBlack;
|
2016-07-14 22:13:19 +03:00
|
|
|
if (!tex->ResolveForDraw(funcName, texUnit, &fakeBlack)) {
|
|
|
|
mWebGL->ErrorOutOfMemory("%s: Failed to resolve textures for draw.",
|
|
|
|
funcName);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
if (fakeBlack == FakeBlackType::None)
|
|
|
|
continue;
|
|
|
|
|
2016-07-27 04:42:09 +03:00
|
|
|
if (!mWebGL->BindFakeBlack(texUnit, tex->Target(), fakeBlack)) {
|
|
|
|
mWebGL->ErrorOutOfMemory("%s: Failed to create fake black texture.",
|
|
|
|
funcName);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
mRebindRequests.push_back({texUnit, tex});
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 04:38:33 +03:00
|
|
|
|
2016-07-14 22:13:19 +03:00
|
|
|
*out_error = false;
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedResolveTexturesForDraw::~ScopedResolveTexturesForDraw()
|
|
|
|
{
|
2017-08-03 13:01:38 +03:00
|
|
|
if (mRebindRequests.empty())
|
2015-11-25 07:15:29 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-27 04:42:09 +03:00
|
|
|
bool
|
2015-11-25 07:15:29 +03:00
|
|
|
WebGLContext::BindFakeBlack(uint32_t texUnit, TexTarget target, FakeBlackType fakeBlack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(fakeBlack == FakeBlackType::RGBA0000 ||
|
|
|
|
fakeBlack == FakeBlackType::RGBA0001);
|
|
|
|
|
|
|
|
const auto fnGetSlot = [this, target, fakeBlack]() -> UniquePtr<FakeBlackTexture>*
|
|
|
|
{
|
|
|
|
switch (fakeBlack) {
|
|
|
|
case FakeBlackType::RGBA0000:
|
|
|
|
switch (target.get()) {
|
|
|
|
case LOCAL_GL_TEXTURE_2D : return &mFakeBlack_2D_0000;
|
|
|
|
case LOCAL_GL_TEXTURE_CUBE_MAP: return &mFakeBlack_CubeMap_0000;
|
|
|
|
case LOCAL_GL_TEXTURE_3D : return &mFakeBlack_3D_0000;
|
|
|
|
case LOCAL_GL_TEXTURE_2D_ARRAY: return &mFakeBlack_2D_Array_0000;
|
|
|
|
default: return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
case FakeBlackType::RGBA0001:
|
|
|
|
switch (target.get()) {
|
|
|
|
case LOCAL_GL_TEXTURE_2D : return &mFakeBlack_2D_0001;
|
|
|
|
case LOCAL_GL_TEXTURE_CUBE_MAP: return &mFakeBlack_CubeMap_0001;
|
|
|
|
case LOCAL_GL_TEXTURE_3D : return &mFakeBlack_3D_0001;
|
|
|
|
case LOCAL_GL_TEXTURE_2D_ARRAY: return &mFakeBlack_2D_Array_0001;
|
|
|
|
default: return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
UniquePtr<FakeBlackTexture>* slot = fnGetSlot();
|
|
|
|
if (!slot) {
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_CRASH("GFX: fnGetSlot failed.");
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
UniquePtr<FakeBlackTexture>& fakeBlackTex = *slot;
|
|
|
|
|
|
|
|
if (!fakeBlackTex) {
|
2016-07-27 04:42:09 +03:00
|
|
|
fakeBlackTex = FakeBlackTexture::Create(gl, target, fakeBlack);
|
|
|
|
if (!fakeBlackTex) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gl->fActiveTexture(LOCAL_GL_TEXTURE0 + texUnit);
|
|
|
|
gl->fBindTexture(target.get(), fakeBlackTex->mGLName);
|
|
|
|
gl->fActiveTexture(LOCAL_GL_TEXTURE0 + mActiveTexture);
|
2016-07-27 04:42:09 +03:00
|
|
|
return true;
|
2015-11-25 07:15:29 +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());
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
class ScopedDrawHelper final
|
|
|
|
{
|
|
|
|
WebGLContext* const mWebGL;
|
|
|
|
bool mDidFake;
|
|
|
|
|
|
|
|
public:
|
2017-10-21 01:40:12 +03:00
|
|
|
ScopedDrawHelper(WebGLContext* const webgl, const char* const funcName,
|
|
|
|
const GLenum mode, const Maybe<uint32_t>& lastRequiredVertex,
|
|
|
|
const uint32_t instanceCount, bool* const out_error)
|
2016-09-10 07:02:54 +03:00
|
|
|
: mWebGL(webgl)
|
|
|
|
, mDidFake(false)
|
|
|
|
{
|
2017-10-21 01:40:12 +03:00
|
|
|
MOZ_ASSERT(mWebGL->gl->IsCurrent());
|
|
|
|
|
2017-12-19 05:30:56 +03:00
|
|
|
if (!mWebGL->BindCurFBForDraw(funcName)) {
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
if (!mWebGL->ValidateDrawModeEnum(mode, funcName)) {
|
2016-09-10 07:02:54 +03:00
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
if (!mWebGL->ValidateStencilParamsForDrawCall()) {
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-19 05:30:56 +03:00
|
|
|
if (!mWebGL->mActiveProgramLinkInfo) {
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: The current program is not linked.", funcName);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
2016-09-10 07:02:54 +03:00
|
|
|
}
|
2017-12-19 05:30:56 +03:00
|
|
|
const auto& linkInfo = mWebGL->mActiveProgramLinkInfo;
|
2016-09-10 07:02:54 +03:00
|
|
|
|
|
|
|
////
|
|
|
|
// Check UBO sizes.
|
|
|
|
|
|
|
|
for (const auto& cur : linkInfo->uniformBlocks) {
|
|
|
|
const auto& dataSize = cur->mDataSize;
|
|
|
|
const auto& binding = cur->mBinding;
|
|
|
|
if (!binding) {
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: Buffer for uniform block is null.",
|
|
|
|
funcName);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto availByteCount = binding->ByteCount();
|
|
|
|
if (dataSize > availByteCount) {
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: Buffer for uniform block is smaller"
|
|
|
|
" than UNIFORM_BLOCK_DATA_SIZE.",
|
|
|
|
funcName);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
2016-12-29 11:08:39 +03:00
|
|
|
|
|
|
|
if (binding->mBufferBinding->IsBoundForTF()) {
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: Buffer for uniform block is bound or"
|
|
|
|
" in use for transform feedback.",
|
|
|
|
funcName);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
2016-09-10 07:02:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2016-11-18 04:50:38 +03:00
|
|
|
const auto& tfo = mWebGL->mBoundTransformFeedback;
|
2016-12-29 11:08:39 +03:00
|
|
|
if (tfo && tfo->IsActiveAndNotPaused()) {
|
|
|
|
uint32_t numUsed;
|
|
|
|
switch (linkInfo->transformFeedbackBufferMode) {
|
|
|
|
case LOCAL_GL_INTERLEAVED_ATTRIBS:
|
|
|
|
numUsed = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_SEPARATE_ATTRIBS:
|
|
|
|
numUsed = linkInfo->transformFeedbackVaryings.size();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < numUsed; ++i) {
|
|
|
|
const auto& buffer = tfo->mIndexedBindings[i].mBufferBinding;
|
|
|
|
if (buffer->IsBoundForNonTF()) {
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: Transform feedback varying %u's"
|
|
|
|
" buffer is bound for"
|
|
|
|
" non-transform-feedback.",
|
|
|
|
funcName, i);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
2017-12-21 03:31:58 +03:00
|
|
|
|
|
|
|
// Technically we don't know that this will be updated yet, but we can
|
|
|
|
// speculatively mark it.
|
|
|
|
buffer->ResetLastUpdateFenceId();
|
2016-11-18 04:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
const auto& fetchLimits = linkInfo->GetDrawFetchLimits(funcName);
|
|
|
|
if (!fetchLimits) {
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
2016-12-29 11:08:39 +03:00
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
if (lastRequiredVertex && instanceCount) {
|
|
|
|
if (lastRequiredVertex.value() >= fetchLimits->maxVerts) {
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: Vertex fetch requires vertex #%u, but"
|
|
|
|
" attribs only supply %" PRIu64 ".",
|
|
|
|
funcName, lastRequiredVertex.value(),
|
|
|
|
fetchLimits->maxVerts);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (instanceCount > fetchLimits->maxInstances) {
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: Instance fetch requires %u, but"
|
|
|
|
" attribs only supply %" PRIu64 ".",
|
|
|
|
funcName, instanceCount,
|
|
|
|
fetchLimits->maxInstances);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
2016-12-22 06:42:07 +03:00
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
2016-12-22 06:42:07 +03:00
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
if (lastRequiredVertex) {
|
|
|
|
if (!mWebGL->DoFakeVertexAttrib0(funcName, lastRequiredVertex.value())) {
|
2016-12-22 06:42:07 +03:00
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
mDidFake = true;
|
2016-12-22 06:42:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
mWebGL->RunContextLossTimer();
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
~ScopedDrawHelper() {
|
|
|
|
if (mDidFake) {
|
|
|
|
mWebGL->UndoFakeVertexAttrib0();
|
|
|
|
}
|
2014-03-31 13:10:49 +04: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;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedDrawWithTransformFeedback(WebGLContext* webgl, const char* funcName,
|
|
|
|
GLenum mode, 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;
|
|
|
|
|
|
|
|
if (mode != mTFO->mActive_PrimMode) {
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: Drawing with transform feedback requires"
|
|
|
|
" `mode` to match BeginTransformFeedback's"
|
|
|
|
" `primitiveMode`.",
|
|
|
|
funcName);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto usedVertsPerInstance = UsedVertsForTFDraw(mode, vertCount);
|
|
|
|
const auto usedVerts = CheckedInt<uint32_t>(usedVertsPerInstance) * instanceCount;
|
|
|
|
|
|
|
|
const auto remainingCapacity = mTFO->mActive_VertCapacity - mTFO->mActive_VertPosition;
|
|
|
|
if (!usedVerts.isValid() ||
|
|
|
|
usedVerts.value() > remainingCapacity)
|
|
|
|
{
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: Insufficient buffer capacity remaining for"
|
|
|
|
" transform feedback.",
|
|
|
|
funcName);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mUsedVerts = usedVerts.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Advance() const {
|
|
|
|
if (!mWithTF)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mTFO->mActive_VertPosition += mUsedVerts;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
////////////////////////////////////////
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
bool
|
|
|
|
WebGLContext::DrawArrays_check(const char* const funcName, const GLint first,
|
|
|
|
const GLsizei vertCount, const GLsizei instanceCount,
|
|
|
|
Maybe<uint32_t>* const out_lastVert)
|
|
|
|
{
|
|
|
|
if (!ValidateNonNegative(funcName, "first", first) ||
|
|
|
|
!ValidateNonNegative(funcName, "vertCount", vertCount) ||
|
|
|
|
!ValidateNonNegative(funcName, "instanceCount", instanceCount))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsWebGL2() && !gl->IsSupported(gl::GLFeature::prim_restart_fixed)) {
|
|
|
|
MOZ_ASSERT(gl->IsSupported(gl::GLFeature::prim_restart));
|
|
|
|
if (mPrimRestartTypeBytes != 0) {
|
|
|
|
mPrimRestartTypeBytes = 0;
|
|
|
|
|
|
|
|
// OSX appears to have severe perf issues with leaving this enabled.
|
|
|
|
gl->fDisable(LOCAL_GL_PRIMITIVE_RESTART);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vertCount) {
|
|
|
|
*out_lastVert = Nothing();
|
|
|
|
} else {
|
|
|
|
const auto lastVert_checked = CheckedInt<uint32_t>(first) + vertCount - 1;
|
|
|
|
if (!lastVert_checked.isValid()) {
|
|
|
|
ErrorOutOfMemory("%s: `first+vertCount` out of range.", funcName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*out_lastVert = Some(lastVert_checked.value());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
void
|
2016-09-10 07:02:54 +03:00
|
|
|
WebGLContext::DrawArraysInstanced(GLenum mode, GLint first, GLsizei vertCount,
|
2017-12-15 01:10:30 +03:00
|
|
|
GLsizei instanceCount, const char* const funcName)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2017-10-21 02:56:28 +03:00
|
|
|
AUTO_PROFILER_LABEL("WebGLContext::DrawArraysInstanced", GRAPHICS);
|
2014-03-31 13:10:49 +04:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
Maybe<uint32_t> lastVert;
|
|
|
|
if (!DrawArrays_check(funcName, first, vertCount, instanceCount, &lastVert))
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
|
|
|
|
2017-12-19 05:30:56 +03:00
|
|
|
bool error = false;
|
2017-10-21 01:40:12 +03:00
|
|
|
const ScopedDrawHelper scopedHelper(this, funcName, mode, lastVert, instanceCount,
|
|
|
|
&error);
|
2016-09-10 07:02:54 +03:00
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
2017-12-19 05:30:56 +03:00
|
|
|
const ScopedResolveTexturesForDraw scopedResolve(this, funcName, &error);
|
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const ScopedDrawWithTransformFeedback scopedTF(this, funcName, mode, vertCount,
|
|
|
|
instanceCount, &error);
|
|
|
|
if (error)
|
|
|
|
return;
|
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
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
Draw_cleanup(funcName);
|
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
|
|
|
////////////////////////////////////////
|
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
bool
|
2017-10-21 01:40:12 +03:00
|
|
|
WebGLContext::DrawElements_check(const char* const funcName, const GLsizei rawIndexCount,
|
|
|
|
const GLenum type, const WebGLintptr byteOffset,
|
|
|
|
const GLsizei instanceCount,
|
|
|
|
Maybe<uint32_t>* const out_lastVert)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2016-09-10 07:02:54 +03:00
|
|
|
if (mBoundTransformFeedback &&
|
|
|
|
mBoundTransformFeedback->mIsActive &&
|
|
|
|
!mBoundTransformFeedback->mIsPaused)
|
|
|
|
{
|
|
|
|
ErrorInvalidOperation("%s: DrawElements* functions are incompatible with"
|
|
|
|
" transform feedback.",
|
|
|
|
funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
if (!ValidateNonNegative(funcName, "vertCount", rawIndexCount) ||
|
2016-09-10 07:02:54 +03:00
|
|
|
!ValidateNonNegative(funcName, "byteOffset", byteOffset) ||
|
|
|
|
!ValidateNonNegative(funcName, "instanceCount", instanceCount))
|
|
|
|
{
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
const auto indexCount = uint32_t(rawIndexCount);
|
2014-03-31 13:10:49 +04: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) {
|
2016-09-10 07:02:54 +03:00
|
|
|
ErrorInvalidEnum("%s: Invalid `type`: 0x%04x", funcName, type);
|
2015-11-25 07:15:29 +03:00
|
|
|
return false;
|
2015-11-24 08:55:59 +03:00
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
if (byteOffset % bytesPerIndex != 0) {
|
2015-11-25 07:15:29 +03:00
|
|
|
ErrorInvalidOperation("%s: `byteOffset` must be a multiple of the size of `type`",
|
2016-09-10 07:02:54 +03:00
|
|
|
funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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;
|
2016-07-27 06:05:45 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
2017-10-21 01:40:12 +03:00
|
|
|
// Index fetching
|
2016-07-27 06:05:45 +03:00
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
if (!indexCount || !instanceCount) {
|
|
|
|
*out_lastVert = Nothing();
|
|
|
|
return true;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
const auto& indexBuffer = mBoundVertexArray->mElementArrayBuffer;
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
size_t availBytes = 0;
|
|
|
|
if (indexBuffer) {
|
|
|
|
MOZ_ASSERT(!indexBuffer->IsBoundForTF(), "This should be impossible.");
|
|
|
|
availBytes = indexBuffer->ByteLength();
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
const auto availIndices = AvailGroups(availBytes, byteOffset, bytesPerIndex,
|
|
|
|
bytesPerIndex);
|
|
|
|
if (indexCount > availIndices) {
|
|
|
|
ErrorInvalidOperation("%s: Index buffer too small.", funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
*out_lastVert = indexBuffer->GetIndexedFetchMaxVert(type, byteOffset, indexCount);
|
2014-03-31 13:10:49 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-21 05:44:28 +03:00
|
|
|
static void
|
|
|
|
HandleDrawElementsErrors(WebGLContext* webgl, const char* funcName,
|
|
|
|
gl::GLContext::LocalErrorScope& errorScope)
|
|
|
|
{
|
|
|
|
const auto err = errorScope.GetError();
|
|
|
|
if (err == LOCAL_GL_INVALID_OPERATION) {
|
|
|
|
webgl->ErrorInvalidOperation("%s: Driver rejected indexed draw call, possibly"
|
|
|
|
" due to out-of-bounds indices.", funcName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!err);
|
|
|
|
if (err) {
|
|
|
|
webgl->ErrorImplementationBug("%s: Unexpected driver error during indexed draw"
|
|
|
|
" call. Please file a bug.",
|
|
|
|
funcName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
void
|
2017-10-21 01:40:12 +03:00
|
|
|
WebGLContext::DrawElementsInstanced(GLenum mode, GLsizei indexCount, GLenum type,
|
2017-12-15 01:10:30 +03:00
|
|
|
WebGLintptr byteOffset, GLsizei instanceCount,
|
|
|
|
const char* const funcName)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2017-10-21 02:56:28 +03:00
|
|
|
AUTO_PROFILER_LABEL("WebGLContext::DrawElementsInstanced", GRAPHICS);
|
2014-03-31 13:10:49 +04:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
Maybe<uint32_t> lastVert;
|
|
|
|
if (!DrawElements_check(funcName, indexCount, type, byteOffset, instanceCount,
|
|
|
|
&lastVert))
|
|
|
|
{
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
2017-10-21 01:40:12 +03:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2017-12-19 05:30:56 +03:00
|
|
|
bool error = false;
|
2017-10-21 01:40:12 +03:00
|
|
|
const ScopedDrawHelper scopedHelper(this, funcName, mode, lastVert, instanceCount,
|
2016-09-10 07:02:54 +03:00
|
|
|
&error);
|
|
|
|
if (error)
|
|
|
|
return;
|
2014-10-02 04:05:34 +04:00
|
|
|
|
2017-12-19 05:30:56 +03:00
|
|
|
const ScopedResolveTexturesForDraw scopedResolve(this, funcName, &error);
|
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
2014-10-02 04:05:34 +04:00
|
|
|
{
|
2016-12-21 10:31:22 +03:00
|
|
|
ScopedDrawCallWrapper wrapper(*this);
|
2016-12-21 05:44:28 +03:00
|
|
|
{
|
|
|
|
UniquePtr<gl::GLContext::LocalErrorScope> errorScope;
|
|
|
|
|
|
|
|
if (gl->IsANGLE()) {
|
|
|
|
errorScope.reset(new gl::GLContext::LocalErrorScope(*gl));
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
gl->fDrawElementsInstanced(mode, indexCount, type,
|
|
|
|
reinterpret_cast<GLvoid*>(byteOffset),
|
|
|
|
instanceCount);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(instanceCount == 1);
|
|
|
|
gl->fDrawElements(mode, indexCount, type,
|
|
|
|
reinterpret_cast<GLvoid*>(byteOffset));
|
|
|
|
}
|
2017-10-21 01:40:12 +03:00
|
|
|
}
|
2017-10-21 02:56:28 +03:00
|
|
|
|
2016-12-21 05:44:28 +03:00
|
|
|
if (errorScope) {
|
|
|
|
HandleDrawElementsErrors(this, funcName, *errorScope);
|
|
|
|
}
|
|
|
|
}
|
2014-10-02 04:05:34 +04:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
Draw_cleanup(funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
void
|
|
|
|
WebGLContext::Draw_cleanup(const char* funcName)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
|
|
|
if (gl->WorkAroundDriverBugs()) {
|
|
|
|
if (gl->Renderer() == gl::GLRenderer::Tegra) {
|
|
|
|
mDrawCallsSinceLastFlush++;
|
|
|
|
|
|
|
|
if (mDrawCallsSinceLastFlush >= MAX_DRAW_CALLS_SINCE_FLUSH) {
|
|
|
|
gl->fFlush();
|
|
|
|
mDrawCallsSinceLastFlush = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
// Let's check for a really common error: Viewport is larger than the actual
|
|
|
|
// destination framebuffer.
|
|
|
|
uint32_t destWidth = mViewportWidth;
|
|
|
|
uint32_t destHeight = mViewportHeight;
|
|
|
|
|
|
|
|
if (mBoundDrawFramebuffer) {
|
2016-07-19 10:36:54 +03:00
|
|
|
const auto& drawBuffers = mBoundDrawFramebuffer->ColorDrawBuffers();
|
|
|
|
for (const auto& cur : drawBuffers) {
|
|
|
|
if (!cur->IsDefined())
|
|
|
|
continue;
|
|
|
|
cur->Size(&destWidth, &destHeight);
|
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (mViewportWidth > int32_t(destWidth) ||
|
|
|
|
mViewportHeight > int32_t(destHeight))
|
|
|
|
{
|
|
|
|
if (!mAlreadyWarnedAboutViewportLargerThanDest) {
|
|
|
|
GenerateWarning("%s: Drawing to a destination rect smaller than the viewport"
|
|
|
|
" rect. (This warning will only be given once)",
|
|
|
|
funcName);
|
|
|
|
mAlreadyWarnedAboutViewportLargerThanDest = true;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebGLVertexAttrib0Status
|
2016-12-22 06:42:07 +03:00
|
|
|
WebGLContext::WhatDoesVertexAttrib0Need() const
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
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);
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-12-30 08:44:51 +03:00
|
|
|
if (!legacyAttrib0)
|
2014-03-31 13:10:49 +04:00
|
|
|
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
|
|
|
|
2017-10-21 01:40:12 +03:00
|
|
|
const auto& isAttribArray0Enabled = mBoundVertexArray->mAttribs[0].mEnabled;
|
|
|
|
return isAttribArray0Enabled ? WebGLVertexAttrib0Status::Default
|
|
|
|
: WebGLVertexAttrib0Status::EmulatedInitializedArray;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-10-21 01:40:12 +03:00
|
|
|
WebGLContext::DoFakeVertexAttrib0(const char* const funcName, const uint32_t lastVert)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
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 "
|
|
|
|
"with vertex attrib 0 array enabled, by using bindAttribLocation "
|
|
|
|
"to bind some always-used attribute to location 0.");
|
|
|
|
mAlreadyWarnedAboutFakeVertexAttrib0 = true;
|
|
|
|
}
|
|
|
|
|
2016-12-30 08:44:51 +03:00
|
|
|
gl->fEnableVertexAttribArray(0);
|
|
|
|
|
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]) {
|
|
|
|
case LOCAL_GL_FLOAT:
|
|
|
|
gl->fVertexAttribPointer(0, 4, LOCAL_GL_FLOAT, false, 0, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_INT:
|
|
|
|
gl->fVertexAttribIPointer(0, 4, LOCAL_GL_INT, 0, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_UNSIGNED_INT:
|
|
|
|
gl->fVertexAttribIPointer(0, 4, LOCAL_GL_UNSIGNED_INT, 0, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
const auto bytesPerVert = sizeof(mFakeVertexAttrib0Data);
|
2017-10-21 01:40:12 +03:00
|
|
|
const auto checked_dataSize = (CheckedUint32(lastVert)+1) * 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.",
|
|
|
|
uint64_t(lastVert) + 1);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-22 06:42:07 +03:00
|
|
|
const auto dataSize = checked_dataSize.value();
|
2014-03-31 13:10:49 +04: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;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
if (whatDoesAttrib0Need == WebGLVertexAttrib0Status::EmulatedUninitializedArray)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
if (mFakeVertexAttrib0DataDefined &&
|
|
|
|
memcmp(mFakeVertexAttrib0Data, mGenericVertexAttrib0Data, bytesPerVert) == 0)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2016-12-22 06:42:07 +03:00
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
const UniqueBuffer data(malloc(dataSize));
|
|
|
|
if (!data) {
|
|
|
|
ErrorOutOfMemory("%s: Failed to allocate fake vertex attrib 0 array.",
|
|
|
|
funcName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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) {
|
|
|
|
ErrorOutOfMemory("%s: Failed to upload fake vertex attrib 0 data.", funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-12-22 06:42:07 +03:00
|
|
|
if (mBoundVertexArray->mAttribs[0].mBuf) {
|
2014-03-31 13:10:49 +04:00
|
|
|
const WebGLVertexAttribData& attrib0 = mBoundVertexArray->mAttribs[0];
|
2016-09-24 03:28:58 +03:00
|
|
|
gl->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, attrib0.mBuf->mGLName);
|
|
|
|
attrib0.DoVertexAttribPointer(gl, 0);
|
2014-03-31 13:10:49 +04:00
|
|
|
} else {
|
|
|
|
gl->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
2015-05-22 08:49:30 +03:00
|
|
|
gl->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mBoundArrayBuffer ? mBoundArrayBuffer->mGLName : 0);
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
static GLuint
|
|
|
|
CreateGLTexture(gl::GLContext* gl)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
MOZ_ASSERT(gl->IsCurrent());
|
|
|
|
GLuint ret = 0;
|
|
|
|
gl->fGenTextures(1, &ret);
|
|
|
|
return ret;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
2016-07-27 04:42:09 +03:00
|
|
|
UniquePtr<WebGLContext::FakeBlackTexture>
|
|
|
|
WebGLContext::FakeBlackTexture::Create(gl::GLContext* gl, TexTarget target,
|
|
|
|
FakeBlackType type)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
GLenum texFormat;
|
|
|
|
switch (type) {
|
|
|
|
case FakeBlackType::RGBA0000:
|
|
|
|
texFormat = LOCAL_GL_RGBA;
|
|
|
|
break;
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
case FakeBlackType::RGBA0001:
|
|
|
|
texFormat = LOCAL_GL_RGB;
|
|
|
|
break;
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
default:
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_CRASH("GFX: bad type");
|
2015-11-24 08:55:59 +03:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-07-27 04:42:09 +03:00
|
|
|
UniquePtr<FakeBlackTexture> result(new FakeBlackTexture(gl));
|
|
|
|
gl::ScopedBindTexture scopedBind(gl, result->mGLName, target.get());
|
2015-11-24 06:27:13 +03:00
|
|
|
|
2016-07-27 04:42:09 +03:00
|
|
|
gl->fTexParameteri(target.get(), LOCAL_GL_TEXTURE_MIN_FILTER, LOCAL_GL_NEAREST);
|
|
|
|
gl->fTexParameteri(target.get(), LOCAL_GL_TEXTURE_MAG_FILTER, LOCAL_GL_NEAREST);
|
2015-11-24 08:55:59 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
// We allocate our zeros on the heap, and we overallocate (16 bytes instead of 4) to
|
|
|
|
// minimize the risk of running into a driver bug in texImage2D, as it is a bit
|
|
|
|
// unusual maybe to create 1x1 textures, and the stack may not have the alignment that
|
|
|
|
// TexImage2D expects.
|
2015-11-24 08:55:59 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
const webgl::DriverUnpackInfo dui = {texFormat, texFormat, LOCAL_GL_UNSIGNED_BYTE};
|
|
|
|
UniqueBuffer zeros = moz_xcalloc(1, 16); // Infallible allocation.
|
|
|
|
|
2016-04-01 17:13:25 +03:00
|
|
|
MOZ_ASSERT(gl->IsCurrent());
|
2016-06-28 05:37:38 +03:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
if (target == LOCAL_GL_TEXTURE_CUBE_MAP) {
|
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
const TexImageTarget curTarget = LOCAL_GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
|
2016-07-27 04:42:09 +03:00
|
|
|
const GLenum error = DoTexImage(gl, curTarget.get(), 0, &dui, 1, 1, 1,
|
2015-11-25 07:15:29 +03:00
|
|
|
zeros.get());
|
2016-04-01 17:13:25 +03:00
|
|
|
if (error) {
|
2016-07-27 04:42:09 +03:00
|
|
|
return nullptr;
|
2016-04-01 17:13:25 +03:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
} else {
|
2016-07-27 04:42:09 +03:00
|
|
|
const GLenum error = DoTexImage(gl, target.get(), 0, &dui, 1, 1, 1,
|
2015-11-25 07:15:29 +03:00
|
|
|
zeros.get());
|
2016-04-01 17:13:25 +03:00
|
|
|
if (error) {
|
2016-07-27 04:42:09 +03:00
|
|
|
return nullptr;
|
2016-04-01 17:13:25 +03:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2016-06-28 05:37:38 +03:00
|
|
|
|
2016-07-27 04:42:09 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebGLContext::FakeBlackTexture::FakeBlackTexture(gl::GLContext* gl)
|
|
|
|
: mGL(gl)
|
|
|
|
, mGLName(CreateGLTexture(gl))
|
|
|
|
{
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
WebGLContext::FakeBlackTexture::~FakeBlackTexture()
|
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
mGL->fDeleteTextures(1, &mGLName);
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2015-07-15 03:37:28 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|