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"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "WebGLVertexArray.h"
|
|
|
|
#include "WebGLVertexAttribData.h"
|
|
|
|
|
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-14 20:52:20 +03:00
|
|
|
if (!mWebGL->mActiveProgramLinkInfo) {
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: The current program is not linked.", funcName);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
const std::vector<const WebGLFBAttachPoint*>* attachList = nullptr;
|
|
|
|
const auto& fb = mWebGL->mBoundDrawFramebuffer;
|
|
|
|
if (fb) {
|
2016-09-01 23:51:03 +03:00
|
|
|
if (!fb->ValidateAndInitAttachments(funcName)) {
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
attachList = &(fb->ResolvedCompleteData()->texDrawBuffers);
|
2016-09-01 23:51:03 +03:00
|
|
|
} else {
|
|
|
|
webgl->ClearBackbufferIfNeeded();
|
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()
|
|
|
|
{
|
|
|
|
if (!mRebindRequests.size())
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
bool
|
|
|
|
WebGLContext::DrawInstanced_check(const char* info)
|
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
MOZ_ASSERT(IsWebGL2() ||
|
|
|
|
IsExtensionEnabled(WebGLExtensionID::ANGLE_instanced_arrays));
|
|
|
|
if (!mBufferFetchingHasPerVertex) {
|
2014-03-31 13:10:49 +04:00
|
|
|
/* http://www.khronos.org/registry/gles/extensions/ANGLE/ANGLE_instanced_arrays.txt
|
|
|
|
* If all of the enabled vertex attribute arrays that are bound to active
|
|
|
|
* generic attributes in the program have a non-zero divisor, the draw
|
|
|
|
* call should return INVALID_OPERATION.
|
|
|
|
*
|
|
|
|
* NB: This also appears to apply to NV_instanced_arrays, though the
|
|
|
|
* INVALID_OPERATION emission is not explicitly stated.
|
|
|
|
* ARB_instanced_arrays does not have this restriction.
|
|
|
|
*/
|
|
|
|
ErrorInvalidOperation("%s: at least one vertex attribute divisor should be 0", info);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-07 05:05:11 +03:00
|
|
|
bool
|
2016-09-10 07:02:54 +03:00
|
|
|
WebGLContext::DrawArrays_check(const char* funcName, GLenum mode, GLint first,
|
|
|
|
GLsizei vertCount, GLsizei instanceCount)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!ValidateDrawModeEnum(mode, funcName))
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!ValidateNonNegative(funcName, "first", first) ||
|
|
|
|
!ValidateNonNegative(funcName, "vertCount", vertCount) ||
|
|
|
|
!ValidateNonNegative(funcName, "instanceCount", instanceCount))
|
|
|
|
{
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!ValidateStencilParamsForDrawCall())
|
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));
|
2016-07-29 23:05:56 +03:00
|
|
|
if (mPrimRestartTypeBytes != 4) {
|
|
|
|
mPrimRestartTypeBytes = 4;
|
2016-07-27 06:05:45 +03:00
|
|
|
|
2016-07-29 23:05:56 +03:00
|
|
|
// OSX has issues leaving this as 0.
|
|
|
|
gl->fPrimitiveRestartIndex(UINT32_MAX);
|
2016-07-27 06:05:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!vertCount || !instanceCount)
|
|
|
|
return false; // No error, just early out.
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!ValidateBufferFetching(funcName))
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const auto checked_firstPlusCount = CheckedInt<GLsizei>(first) + vertCount;
|
2014-03-31 13:10:49 +04:00
|
|
|
if (!checked_firstPlusCount.isValid()) {
|
2016-09-10 07:02:54 +03:00
|
|
|
ErrorInvalidOperation("%s: overflow in first+vertCount", funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uint32_t(checked_firstPlusCount.value()) > mMaxFetchedVertices) {
|
2016-09-10 07:02:54 +03:00
|
|
|
ErrorInvalidOperation("%s: Bound vertex attribute buffers do not have sufficient"
|
|
|
|
" size for given first and count.",
|
|
|
|
funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
class ScopedDrawHelper final
|
|
|
|
{
|
|
|
|
WebGLContext* const mWebGL;
|
|
|
|
bool mDidFake;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedDrawHelper(WebGLContext* webgl, const char* funcName, uint32_t firstVertex,
|
|
|
|
uint32_t vertCount, uint32_t instanceCount, bool* const out_error)
|
|
|
|
: mWebGL(webgl)
|
|
|
|
, mDidFake(false)
|
|
|
|
{
|
|
|
|
if (instanceCount > mWebGL->mMaxFetchedInstances) {
|
|
|
|
mWebGL->ErrorInvalidOperation("%s: Bound instance attribute buffers do not"
|
|
|
|
" have sufficient size for given"
|
|
|
|
" `instanceCount`.",
|
|
|
|
funcName);
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWebGL->gl->IsCurrent());
|
|
|
|
|
|
|
|
if (mWebGL->mBoundDrawFramebuffer) {
|
|
|
|
if (!mWebGL->mBoundDrawFramebuffer->ValidateAndInitAttachments(funcName)) {
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mWebGL->ClearBackbufferIfNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
const size_t requiredVerts = firstVertex + vertCount;
|
|
|
|
if (!mWebGL->DoFakeVertexAttrib0(requiredVerts)) {
|
|
|
|
*out_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDidFake = true;
|
|
|
|
|
|
|
|
////
|
|
|
|
// Check UBO sizes.
|
|
|
|
|
|
|
|
const auto& linkInfo = webgl->mActiveProgramLinkInfo;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
void
|
2016-09-10 07:02:54 +03:00
|
|
|
WebGLContext::DrawArrays(GLenum mode, GLint first, GLsizei vertCount)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
const char funcName[] = "drawArrays";
|
2014-03-31 13:10:49 +04:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
2016-03-18 09:00:34 +03:00
|
|
|
MakeContextCurrent();
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
bool error = false;
|
2015-11-25 07:15:29 +03:00
|
|
|
ScopedResolveTexturesForDraw scopedResolve(this, funcName, &error);
|
|
|
|
if (error)
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const GLsizei instanceCount = 1;
|
|
|
|
if (!DrawArrays_check(funcName, mode, first, vertCount, instanceCount))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ScopedDrawHelper scopedHelper(this, funcName, first, vertCount, instanceCount, &error);
|
|
|
|
if (error)
|
2014-03-31 13:10:49 +04:00
|
|
|
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
|
|
|
|
|
|
|
{
|
|
|
|
ScopedMaskWorkaround autoMask(*this);
|
2016-09-10 07:02:54 +03:00
|
|
|
gl->fDrawArrays(mode, first, vertCount);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-10 07:02:54 +03:00
|
|
|
WebGLContext::DrawArraysInstanced(GLenum mode, GLint first, GLsizei vertCount,
|
|
|
|
GLsizei instanceCount)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
const char funcName[] = "drawArraysInstanced";
|
2014-03-31 13:10:49 +04:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
2016-03-18 09:00:34 +03:00
|
|
|
MakeContextCurrent();
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
bool error = false;
|
2015-11-25 07:15:29 +03:00
|
|
|
ScopedResolveTexturesForDraw scopedResolve(this, funcName, &error);
|
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!DrawArrays_check(funcName, mode, first, vertCount, instanceCount))
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
if (!DrawInstanced_check(funcName))
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const ScopedDrawHelper scopedHelper(this, funcName, first, vertCount, instanceCount, &error);
|
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ScopedDrawWithTransformFeedback scopedTF(this, funcName, mode, vertCount,
|
|
|
|
instanceCount, &error);
|
|
|
|
if (error)
|
|
|
|
return;
|
2014-10-02 04:05:34 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
ScopedMaskWorkaround autoMask(*this);
|
2016-09-10 07:02:54 +03:00
|
|
|
gl->fDrawArraysInstanced(mode, first, vertCount, instanceCount);
|
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
|
2016-09-10 07:02:54 +03:00
|
|
|
WebGLContext::DrawElements_check(const char* funcName, GLenum mode, GLsizei vertCount,
|
|
|
|
GLenum type, WebGLintptr byteOffset,
|
|
|
|
GLsizei instanceCount)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!ValidateDrawModeEnum(mode, funcName))
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!ValidateNonNegative(funcName, "vertCount", vertCount) ||
|
|
|
|
!ValidateNonNegative(funcName, "byteOffset", byteOffset) ||
|
|
|
|
!ValidateNonNegative(funcName, "instanceCount", instanceCount))
|
|
|
|
{
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!ValidateStencilParamsForDrawCall())
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!vertCount || !instanceCount)
|
|
|
|
return false; // No error, just early out.
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
uint8_t bytesPerElem = 0;
|
|
|
|
switch (type) {
|
|
|
|
case LOCAL_GL_UNSIGNED_BYTE:
|
|
|
|
bytesPerElem = 1;
|
|
|
|
break;
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
case LOCAL_GL_UNSIGNED_SHORT:
|
|
|
|
bytesPerElem = 2;
|
|
|
|
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)) {
|
|
|
|
bytesPerElem = 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
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
if (!bytesPerElem) {
|
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
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
if (byteOffset % bytesPerElem != 0) {
|
|
|
|
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));
|
|
|
|
if (mPrimRestartTypeBytes != bytesPerElem) {
|
|
|
|
mPrimRestartTypeBytes = bytesPerElem;
|
|
|
|
|
|
|
|
const uint32_t ones = UINT32_MAX >> (4 - mPrimRestartTypeBytes);
|
|
|
|
gl->fPrimitiveRestartIndex(ones);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
const GLsizei first = byteOffset / bytesPerElem;
|
2016-09-10 07:02:54 +03:00
|
|
|
const CheckedUint32 checked_byteCount = bytesPerElem * CheckedUint32(vertCount);
|
2015-11-25 07:15:29 +03:00
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
if (!checked_byteCount.isValid()) {
|
2016-09-10 07:02:54 +03:00
|
|
|
ErrorInvalidValue("%s: Overflow in byteCount.", funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-06 03:38:27 +04:00
|
|
|
if (!mBoundVertexArray->mElementArrayBuffer) {
|
2016-09-10 07:02:54 +03:00
|
|
|
ErrorInvalidOperation("%s: Must have element array buffer binding.", funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-06 03:38:27 +04:00
|
|
|
WebGLBuffer& elemArrayBuffer = *mBoundVertexArray->mElementArrayBuffer;
|
2014-03-31 13:10:49 +04:00
|
|
|
|
|
|
|
if (!elemArrayBuffer.ByteLength()) {
|
2016-09-10 07:02:54 +03:00
|
|
|
ErrorInvalidOperation("%s: Bound element array buffer doesn't have any data.",
|
|
|
|
funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckedInt<GLsizei> checked_neededByteCount = checked_byteCount.toChecked<GLsizei>() + byteOffset;
|
|
|
|
|
|
|
|
if (!checked_neededByteCount.isValid()) {
|
2016-09-10 07:02:54 +03:00
|
|
|
ErrorInvalidOperation("%s: Overflow in byteOffset+byteCount.", funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uint32_t(checked_neededByteCount.value()) > elemArrayBuffer.ByteLength()) {
|
2016-09-10 07:02:54 +03:00
|
|
|
ErrorInvalidOperation("%s: Bound element array buffer is too small for given"
|
|
|
|
" count and offset.",
|
|
|
|
funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!ValidateBufferFetching(funcName))
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!mMaxFetchedVertices ||
|
2016-09-10 07:02:54 +03:00
|
|
|
!elemArrayBuffer.Validate(type, mMaxFetchedVertices - 1, first, vertCount))
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2016-09-10 07:02:54 +03:00
|
|
|
ErrorInvalidOperation("%s: bound vertex attribute buffers do not have sufficient "
|
|
|
|
"size for given indices from the bound element array",
|
|
|
|
funcName);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-03 00:30:00 +04:00
|
|
|
// Bug 1008310 - Check if buffer has been used with a different previous type
|
|
|
|
if (elemArrayBuffer.IsElementArrayUsedWithMultipleTypes()) {
|
|
|
|
GenerateWarning("%s: bound element array buffer previously used with a type other than "
|
|
|
|
"%s, this will affect performance.",
|
2016-09-10 07:02:54 +03:00
|
|
|
funcName, WebGLContext::EnumName(type));
|
2014-06-03 00:30:00 +04:00
|
|
|
}
|
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-10 07:02:54 +03:00
|
|
|
WebGLContext::DrawElements(GLenum mode, GLsizei vertCount, GLenum type,
|
2014-03-31 13:10:49 +04:00
|
|
|
WebGLintptr byteOffset)
|
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
const char funcName[] = "drawElements";
|
2014-03-31 13:10:49 +04:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
2016-03-18 09:00:34 +03:00
|
|
|
MakeContextCurrent();
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
bool error = false;
|
2015-11-25 07:15:29 +03:00
|
|
|
ScopedResolveTexturesForDraw scopedResolve(this, funcName, &error);
|
|
|
|
if (error)
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const GLsizei instanceCount = 1;
|
|
|
|
if (!DrawElements_check(funcName, mode, vertCount, type, byteOffset, instanceCount))
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const ScopedDrawHelper scopedHelper(this, funcName, 0, mMaxFetchedVertices, instanceCount,
|
|
|
|
&error);
|
|
|
|
if (error)
|
|
|
|
return;
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2014-10-02 04:05:34 +04:00
|
|
|
{
|
|
|
|
ScopedMaskWorkaround autoMask(*this);
|
2016-09-10 07:02:54 +03:00
|
|
|
gl->fDrawElements(mode, vertCount, type,
|
|
|
|
reinterpret_cast<GLvoid*>(byteOffset));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-10 07:02:54 +03:00
|
|
|
WebGLContext::DrawElementsInstanced(GLenum mode, GLsizei vertCount, GLenum type,
|
|
|
|
WebGLintptr byteOffset, GLsizei instanceCount)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
const char funcName[] = "drawElementsInstanced";
|
2014-03-31 13:10:49 +04:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
2016-03-18 09:00:34 +03:00
|
|
|
MakeContextCurrent();
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
bool error = false;
|
2015-11-25 07:15:29 +03:00
|
|
|
ScopedResolveTexturesForDraw scopedResolve(this, funcName, &error);
|
|
|
|
if (error)
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
if (!DrawElements_check(funcName, mode, vertCount, type, byteOffset, instanceCount))
|
2015-11-25 07:15:29 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!DrawInstanced_check(funcName))
|
2014-03-31 13:10:49 +04:00
|
|
|
return;
|
|
|
|
|
2016-09-10 07:02:54 +03:00
|
|
|
const ScopedDrawHelper scopedHelper(this, funcName, 0, mMaxFetchedVertices, instanceCount,
|
|
|
|
&error);
|
|
|
|
if (error)
|
|
|
|
return;
|
2014-10-02 04:05:34 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
ScopedMaskWorkaround autoMask(*this);
|
2016-09-10 07:02:54 +03:00
|
|
|
gl->fDrawElementsInstanced(mode, vertCount, type,
|
2014-10-02 04:05:34 +04:00
|
|
|
reinterpret_cast<GLvoid*>(byteOffset),
|
2016-09-10 07:02:54 +03:00
|
|
|
instanceCount);
|
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
|
|
|
{
|
2015-01-13 02:05:21 +03:00
|
|
|
if (!mBoundDrawFramebuffer) {
|
2014-03-31 13:10:49 +04:00
|
|
|
Invalidate();
|
|
|
|
mShouldPresent = true;
|
2014-04-18 00:15:41 +04:00
|
|
|
MOZ_ASSERT(!mBackbufferNeedsClear);
|
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 {
|
|
|
|
destWidth = mWidth;
|
|
|
|
destHeight = mHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that state is consistent for drawing, and compute max number of elements (maxAllowedCount)
|
|
|
|
* that will be legal to be read from bound VBOs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
2014-11-14 07:03:50 +03:00
|
|
|
WebGLContext::ValidateBufferFetching(const char* info)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
2015-01-16 02:40:39 +03:00
|
|
|
MOZ_ASSERT(mCurrentProgram);
|
|
|
|
// Note that mCurrentProgram->IsLinked() is NOT GUARANTEED.
|
|
|
|
MOZ_ASSERT(mActiveProgramLinkInfo);
|
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
GLint currentProgram = 0;
|
|
|
|
MakeContextCurrent();
|
|
|
|
gl->fGetIntegerv(LOCAL_GL_CURRENT_PROGRAM, ¤tProgram);
|
2015-01-16 02:40:39 +03:00
|
|
|
MOZ_ASSERT(GLuint(currentProgram) == mCurrentProgram->mGLName,
|
2014-03-31 13:10:49 +04:00
|
|
|
"WebGL: current program doesn't agree with GL state");
|
|
|
|
#endif
|
|
|
|
|
2014-11-14 07:03:50 +03:00
|
|
|
if (mBufferFetchingIsVerified)
|
2014-03-31 13:10:49 +04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
bool hasPerVertex = false;
|
|
|
|
uint32_t maxVertices = UINT32_MAX;
|
|
|
|
uint32_t maxInstances = UINT32_MAX;
|
2016-07-07 19:12:10 +03:00
|
|
|
const uint32_t attribCount = mBoundVertexArray->mAttribs.Length();
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-07-07 19:12:10 +03:00
|
|
|
uint32_t i = 0;
|
|
|
|
for (const auto& vd : mBoundVertexArray->mAttribs) {
|
2014-03-31 13:10:49 +04:00
|
|
|
// If the attrib array isn't enabled, there's nothing to check;
|
|
|
|
// it's a static value.
|
2016-09-24 03:28:58 +03:00
|
|
|
if (!vd.mEnabled)
|
2014-03-31 13:10:49 +04:00
|
|
|
continue;
|
|
|
|
|
2016-09-24 03:28:58 +03:00
|
|
|
if (!vd.mBuf) {
|
2016-07-07 19:12:10 +03:00
|
|
|
ErrorInvalidOperation("%s: no VBO bound to enabled vertex attrib index %du!",
|
|
|
|
info, i);
|
2014-03-31 13:10:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-07 19:12:10 +03:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBufferFetch_IsAttrib0Active = false;
|
|
|
|
|
2016-07-14 22:13:19 +03:00
|
|
|
for (const auto& attrib : mActiveProgramLinkInfo->attribs) {
|
|
|
|
const auto& attribLoc = attrib.mLoc;
|
2016-07-07 19:12:10 +03:00
|
|
|
|
|
|
|
if (attribLoc >= attribCount)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (attribLoc == 0) {
|
|
|
|
mBufferFetch_IsAttrib0Active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& vd = mBoundVertexArray->mAttribs[attribLoc];
|
2016-09-24 03:28:58 +03:00
|
|
|
if (!vd.mEnabled)
|
2014-03-31 13:10:49 +04:00
|
|
|
continue;
|
|
|
|
|
2016-09-24 03:28:58 +03:00
|
|
|
const auto& bufByteLen = vd.mBuf->ByteLength();
|
|
|
|
if (vd.ByteOffset() > bufByteLen) {
|
2014-03-31 13:10:49 +04:00
|
|
|
maxVertices = 0;
|
|
|
|
maxInstances = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-24 03:28:58 +03:00
|
|
|
size_t availBytes = bufByteLen - vd.ByteOffset();
|
|
|
|
if (vd.BytesPerVertex() > availBytes) {
|
|
|
|
maxVertices = 0;
|
|
|
|
maxInstances = 0;
|
|
|
|
break;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2016-09-24 03:28:58 +03:00
|
|
|
availBytes -= vd.BytesPerVertex();
|
|
|
|
const size_t vertCapacity = 1 + availBytes / vd.ExplicitStride();
|
2014-03-31 13:10:49 +04:00
|
|
|
|
2016-09-24 03:28:58 +03:00
|
|
|
if (vd.mDivisor == 0) {
|
|
|
|
if (vertCapacity < maxVertices) {
|
|
|
|
maxVertices = vertCapacity;
|
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
hasPerVertex = true;
|
|
|
|
} else {
|
2016-09-24 03:28:58 +03:00
|
|
|
const auto curMaxInstances = CheckedInt<size_t>(vertCapacity) * vd.mDivisor;
|
|
|
|
// If this isn't valid, it's because we overflowed, which means we can support
|
|
|
|
// *too much*. Don't update maxInstances in this case.
|
|
|
|
if (curMaxInstances.isValid() &&
|
|
|
|
curMaxInstances.value() < maxInstances)
|
|
|
|
{
|
|
|
|
maxInstances = curMaxInstances.value();
|
2014-07-12 08:53:57 +04:00
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mBufferFetchingIsVerified = true;
|
|
|
|
mBufferFetchingHasPerVertex = hasPerVertex;
|
|
|
|
mMaxFetchedVertices = maxVertices;
|
|
|
|
mMaxFetchedInstances = maxInstances;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebGLVertexAttrib0Status
|
|
|
|
WebGLContext::WhatDoesVertexAttrib0Need()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
// work around Mac OSX crash, see bug 631420
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
if (gl->WorkAroundDriverBugs() &&
|
|
|
|
mBoundVertexArray->IsAttribArrayEnabled(0) &&
|
2016-07-07 19:12:10 +03:00
|
|
|
!mBufferFetch_IsAttrib0Active)
|
2014-03-31 13:10:49 +04:00
|
|
|
{
|
|
|
|
return WebGLVertexAttrib0Status::EmulatedUninitializedArray;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-31 13:10:49 +04:00
|
|
|
if (MOZ_LIKELY(gl->IsGLES() ||
|
2014-03-31 13:10:49 +04:00
|
|
|
mBoundVertexArray->IsAttribArrayEnabled(0)))
|
|
|
|
{
|
|
|
|
return WebGLVertexAttrib0Status::Default;
|
|
|
|
}
|
|
|
|
|
2016-07-07 19:12:10 +03:00
|
|
|
return mBufferFetch_IsAttrib0Active
|
2014-03-31 13:10:49 +04:00
|
|
|
? WebGLVertexAttrib0Status::EmulatedInitializedArray
|
|
|
|
: WebGLVertexAttrib0Status::EmulatedUninitializedArray;
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebGLContext::DoFakeVertexAttrib0(GLuint vertexCount)
|
|
|
|
{
|
|
|
|
WebGLVertexAttrib0Status 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckedUint32 checked_dataSize = CheckedUint32(vertexCount) * 4 * sizeof(GLfloat);
|
|
|
|
|
|
|
|
if (!checked_dataSize.isValid()) {
|
|
|
|
ErrorOutOfMemory("Integer overflow trying to construct a fake vertex attrib 0 array for a draw-operation "
|
|
|
|
"with %d vertices. Try reducing the number of vertices.", vertexCount);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint dataSize = checked_dataSize.value();
|
|
|
|
|
|
|
|
if (!mFakeVertexAttrib0BufferObject) {
|
|
|
|
gl->fGenBuffers(1, &mFakeVertexAttrib0BufferObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the VBO status is already exactly what we need, or if the only difference is that it's initialized and
|
|
|
|
// we don't need it to be, then consider it OK
|
|
|
|
bool vertexAttrib0BufferStatusOK =
|
|
|
|
mFakeVertexAttrib0BufferStatus == whatDoesAttrib0Need ||
|
|
|
|
(mFakeVertexAttrib0BufferStatus == WebGLVertexAttrib0Status::EmulatedInitializedArray &&
|
|
|
|
whatDoesAttrib0Need == WebGLVertexAttrib0Status::EmulatedUninitializedArray);
|
|
|
|
|
|
|
|
if (!vertexAttrib0BufferStatusOK ||
|
|
|
|
mFakeVertexAttrib0BufferObjectSize < dataSize ||
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[0] != mVertexAttrib0Vector[0] ||
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[1] != mVertexAttrib0Vector[1] ||
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[2] != mVertexAttrib0Vector[2] ||
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[3] != mVertexAttrib0Vector[3])
|
|
|
|
{
|
|
|
|
mFakeVertexAttrib0BufferStatus = whatDoesAttrib0Need;
|
|
|
|
mFakeVertexAttrib0BufferObjectSize = dataSize;
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[0] = mVertexAttrib0Vector[0];
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[1] = mVertexAttrib0Vector[1];
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[2] = mVertexAttrib0Vector[2];
|
|
|
|
mFakeVertexAttrib0BufferObjectVector[3] = mVertexAttrib0Vector[3];
|
|
|
|
|
|
|
|
gl->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mFakeVertexAttrib0BufferObject);
|
|
|
|
|
|
|
|
GetAndFlushUnderlyingGLErrors();
|
|
|
|
|
|
|
|
if (mFakeVertexAttrib0BufferStatus == WebGLVertexAttrib0Status::EmulatedInitializedArray) {
|
2015-11-06 00:24:24 +03:00
|
|
|
auto array = MakeUniqueFallible<GLfloat[]>(4 * vertexCount);
|
2014-10-16 07:11:30 +04:00
|
|
|
if (!array) {
|
|
|
|
ErrorOutOfMemory("Fake attrib0 array.");
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-31 13:10:49 +04:00
|
|
|
for(size_t i = 0; i < vertexCount; ++i) {
|
|
|
|
array[4 * i + 0] = mVertexAttrib0Vector[0];
|
|
|
|
array[4 * i + 1] = mVertexAttrib0Vector[1];
|
|
|
|
array[4 * i + 2] = mVertexAttrib0Vector[2];
|
|
|
|
array[4 * i + 3] = mVertexAttrib0Vector[3];
|
|
|
|
}
|
2014-10-16 07:11:30 +04:00
|
|
|
gl->fBufferData(LOCAL_GL_ARRAY_BUFFER, dataSize, array.get(), LOCAL_GL_DYNAMIC_DRAW);
|
2014-03-31 13:10:49 +04:00
|
|
|
} else {
|
|
|
|
gl->fBufferData(LOCAL_GL_ARRAY_BUFFER, dataSize, nullptr, LOCAL_GL_DYNAMIC_DRAW);
|
|
|
|
}
|
|
|
|
GLenum error = GetAndFlushUnderlyingGLErrors();
|
|
|
|
|
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
|
|
|
|
|
|
|
// note that we do this error checking and early return AFTER having restored the buffer binding above
|
|
|
|
if (error) {
|
|
|
|
ErrorOutOfMemory("Ran out of memory trying to construct a fake vertex attrib 0 array for a draw-operation "
|
|
|
|
"with %d vertices. Try reducing the number of vertices.", vertexCount);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mFakeVertexAttrib0BufferObject);
|
|
|
|
gl->fVertexAttribPointer(0, 4, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0, 0);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLContext::UndoFakeVertexAttrib0()
|
|
|
|
{
|
|
|
|
WebGLVertexAttrib0Status 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-09-24 03:28:58 +03:00
|
|
|
if (mBoundVertexArray->HasAttrib(0) && 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->MakeCurrent();
|
|
|
|
mGL->fDeleteTextures(1, &mGLName);
|
2014-03-31 13:10:49 +04:00
|
|
|
}
|
2015-07-15 03:37:28 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|