2014-09-18 02:08:41 +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 "WebGL2Context.h"
|
2015-01-13 02:05:21 +03:00
|
|
|
|
2014-09-18 02:08:41 +04:00
|
|
|
#include "GLContext.h"
|
2015-07-15 03:37:28 +03:00
|
|
|
#include "GLScreenBuffer.h"
|
2018-07-03 05:20:47 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2015-01-13 02:05:21 +03:00
|
|
|
#include "WebGLContextUtils.h"
|
2015-08-06 09:31:00 +03:00
|
|
|
#include "WebGLFormats.h"
|
2015-07-15 03:37:28 +03:00
|
|
|
#include "WebGLFramebuffer.h"
|
2014-09-18 02:08:41 +04:00
|
|
|
|
2015-07-15 03:37:28 +03:00
|
|
|
namespace mozilla {
|
2014-09-18 02:08:41 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void WebGL2Context::BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1,
|
|
|
|
GLint srcY1, GLint dstX0, GLint dstY0,
|
|
|
|
GLint dstX1, GLint dstY1, GLbitfield mask,
|
|
|
|
GLenum filter) {
|
|
|
|
const FuncScope funcScope(*this, "blitFramebuffer");
|
|
|
|
if (IsContextLost()) return;
|
|
|
|
|
|
|
|
const GLbitfield validBits = LOCAL_GL_COLOR_BUFFER_BIT |
|
|
|
|
LOCAL_GL_DEPTH_BUFFER_BIT |
|
|
|
|
LOCAL_GL_STENCIL_BUFFER_BIT;
|
|
|
|
if ((mask | validBits) != validBits) {
|
|
|
|
ErrorInvalidValue("Invalid bit set in mask.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (filter) {
|
2015-01-13 02:05:21 +03:00
|
|
|
case LOCAL_GL_NEAREST:
|
|
|
|
case LOCAL_GL_LINEAR:
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2015-01-13 02:05:21 +03:00
|
|
|
default:
|
2018-11-30 13:46:48 +03:00
|
|
|
ErrorInvalidEnumInfo("filter", filter);
|
|
|
|
return;
|
|
|
|
}
|
2015-01-13 02:05:21 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// --
|
2018-07-03 05:20:47 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
const auto fnLikelyOverflow = [](GLint p0, GLint p1) {
|
|
|
|
auto checked = CheckedInt<GLint>(p1) - p0;
|
|
|
|
checked = -checked; // And check the negation!
|
|
|
|
return !checked.isValid();
|
|
|
|
};
|
2018-07-03 05:20:47 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
if (fnLikelyOverflow(srcX0, srcX1) || fnLikelyOverflow(srcY0, srcY1) ||
|
|
|
|
fnLikelyOverflow(dstX0, dstX1) || fnLikelyOverflow(dstY0, dstY1)) {
|
|
|
|
ErrorInvalidValue("Likely-to-overflow large ranges are forbidden.");
|
|
|
|
return;
|
|
|
|
}
|
2018-07-03 05:20:47 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// --
|
2015-11-24 06:27:13 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
if (!ValidateAndInitFB(mBoundReadFramebuffer) ||
|
|
|
|
!ValidateAndInitFB(mBoundDrawFramebuffer)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-13 02:05:21 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
DoBindFB(mBoundReadFramebuffer, LOCAL_GL_READ_FRAMEBUFFER);
|
|
|
|
DoBindFB(mBoundDrawFramebuffer, LOCAL_GL_DRAW_FRAMEBUFFER);
|
2016-12-21 10:31:22 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
WebGLFramebuffer::BlitFramebuffer(this, srcX0, srcY0, srcX1, srcY1, dstX0,
|
|
|
|
dstY0, dstX1, dstY1, mask, filter);
|
2015-07-03 04:55:43 +03:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void WebGL2Context::FramebufferTextureLayer(GLenum target, GLenum attachment,
|
|
|
|
WebGLTexture* texture, GLint level,
|
|
|
|
GLint layer) {
|
|
|
|
const FuncScope funcScope(*this, "framebufferTextureLayer");
|
|
|
|
if (IsContextLost()) return;
|
2015-07-03 04:55:43 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
if (!ValidateFramebufferTarget(target)) return;
|
2015-07-03 04:55:43 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
WebGLFramebuffer* fb;
|
|
|
|
switch (target) {
|
2015-07-03 04:55:43 +03:00
|
|
|
case LOCAL_GL_FRAMEBUFFER:
|
|
|
|
case LOCAL_GL_DRAW_FRAMEBUFFER:
|
2018-11-30 13:46:48 +03:00
|
|
|
fb = mBoundDrawFramebuffer;
|
|
|
|
break;
|
2015-07-03 04:55:43 +03:00
|
|
|
|
|
|
|
case LOCAL_GL_READ_FRAMEBUFFER:
|
2018-11-30 13:46:48 +03:00
|
|
|
fb = mBoundReadFramebuffer;
|
|
|
|
break;
|
2015-07-03 04:55:43 +03:00
|
|
|
|
|
|
|
default:
|
2018-11-30 13:46:48 +03:00
|
|
|
MOZ_CRASH("GFX: Bad target.");
|
|
|
|
}
|
2015-07-03 04:55:43 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
if (!fb) return ErrorInvalidOperation("Cannot modify framebuffer 0.");
|
2015-07-03 04:55:43 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
fb->FramebufferTextureLayer(attachment, texture, level, layer);
|
2014-09-18 02:08:41 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
JS::Value WebGL2Context::GetFramebufferAttachmentParameter(
|
|
|
|
JSContext* cx, GLenum target, GLenum attachment, GLenum pname,
|
|
|
|
ErrorResult& out_error) {
|
|
|
|
return WebGLContext::GetFramebufferAttachmentParameter(cx, target, attachment,
|
|
|
|
pname, out_error);
|
2015-08-06 09:31:00 +03:00
|
|
|
}
|
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
////
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
static bool ValidateBackbufferAttachmentEnum(WebGLContext* webgl,
|
|
|
|
GLenum attachment) {
|
|
|
|
switch (attachment) {
|
2016-12-01 05:46:06 +03:00
|
|
|
case LOCAL_GL_COLOR:
|
|
|
|
case LOCAL_GL_DEPTH:
|
|
|
|
case LOCAL_GL_STENCIL:
|
2018-11-30 13:46:48 +03:00
|
|
|
return true;
|
2015-05-25 14:50:15 +03:00
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
default:
|
2018-11-30 13:46:48 +03:00
|
|
|
webgl->ErrorInvalidEnumInfo("attachment", attachment);
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-01 05:46:06 +03:00
|
|
|
}
|
2015-05-25 14:50:15 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
static bool ValidateFramebufferAttachmentEnum(WebGLContext* webgl,
|
|
|
|
GLenum attachment) {
|
|
|
|
switch (attachment) {
|
2016-12-01 05:46:06 +03:00
|
|
|
case LOCAL_GL_DEPTH_ATTACHMENT:
|
|
|
|
case LOCAL_GL_STENCIL_ATTACHMENT:
|
|
|
|
case LOCAL_GL_DEPTH_STENCIL_ATTACHMENT:
|
2018-11-30 13:46:48 +03:00
|
|
|
return true;
|
|
|
|
}
|
2016-12-01 05:46:06 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
if (attachment < LOCAL_GL_COLOR_ATTACHMENT0) {
|
|
|
|
webgl->ErrorInvalidEnumInfo("attachment", attachment);
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-23 04:08:35 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
if (attachment > webgl->LastColorAttachmentEnum()) {
|
|
|
|
// That these errors have different types is ridiculous.
|
|
|
|
webgl->ErrorInvalidOperation("Too-large LOCAL_GL_COLOR_ATTACHMENTn.");
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-23 04:08:35 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
return true;
|
2014-10-15 01:49:49 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
bool WebGLContext::ValidateInvalidateFramebuffer(
|
|
|
|
GLenum target, const dom::Sequence<GLenum>& attachments,
|
|
|
|
ErrorResult* const out_rv, std::vector<GLenum>* const scopedVector,
|
|
|
|
GLsizei* const out_glNumAttachments,
|
|
|
|
const GLenum** const out_glAttachments) {
|
|
|
|
if (IsContextLost()) return false;
|
|
|
|
|
|
|
|
if (!ValidateFramebufferTarget(target)) return false;
|
|
|
|
|
|
|
|
const WebGLFramebuffer* fb;
|
|
|
|
bool isDefaultFB = false;
|
|
|
|
switch (target) {
|
2015-01-13 02:05:21 +03:00
|
|
|
case LOCAL_GL_FRAMEBUFFER:
|
|
|
|
case LOCAL_GL_DRAW_FRAMEBUFFER:
|
2018-11-30 13:46:48 +03:00
|
|
|
fb = mBoundDrawFramebuffer;
|
|
|
|
break;
|
2015-01-13 02:05:21 +03:00
|
|
|
|
|
|
|
case LOCAL_GL_READ_FRAMEBUFFER:
|
2018-11-30 13:46:48 +03:00
|
|
|
fb = mBoundReadFramebuffer;
|
|
|
|
break;
|
2015-01-13 02:05:21 +03:00
|
|
|
|
|
|
|
default:
|
2018-11-30 13:46:48 +03:00
|
|
|
MOZ_CRASH("GFX: Bad target.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fb) {
|
|
|
|
const auto fbStatus = fb->CheckFramebufferStatus();
|
|
|
|
if (fbStatus != LOCAL_GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
return false; // Not an error, but don't run forward to driver either.
|
|
|
|
} else {
|
|
|
|
if (!EnsureDefaultFB()) return false;
|
|
|
|
}
|
|
|
|
DoBindFB(fb, target);
|
|
|
|
|
|
|
|
*out_glNumAttachments = attachments.Length();
|
|
|
|
*out_glAttachments = attachments.Elements();
|
|
|
|
|
|
|
|
if (fb) {
|
|
|
|
for (const auto& attachment : attachments) {
|
|
|
|
if (!ValidateFramebufferAttachmentEnum(this, attachment)) return false;
|
2015-01-13 02:05:21 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
|
|
|
for (const auto& attachment : attachments) {
|
|
|
|
if (!ValidateBackbufferAttachmentEnum(this, attachment)) return false;
|
2017-12-20 23:16:43 +03:00
|
|
|
}
|
2016-12-01 05:46:06 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
if (!isDefaultFB) {
|
|
|
|
MOZ_ASSERT(scopedVector->empty());
|
|
|
|
scopedVector->reserve(attachments.Length());
|
|
|
|
for (const auto& attachment : attachments) {
|
|
|
|
switch (attachment) {
|
|
|
|
case LOCAL_GL_COLOR:
|
|
|
|
scopedVector->push_back(LOCAL_GL_COLOR_ATTACHMENT0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_DEPTH:
|
|
|
|
scopedVector->push_back(LOCAL_GL_DEPTH_ATTACHMENT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_STENCIL:
|
|
|
|
scopedVector->push_back(LOCAL_GL_STENCIL_ATTACHMENT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH();
|
2016-12-01 05:46:06 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
*out_glNumAttachments = scopedVector->size();
|
|
|
|
*out_glAttachments = scopedVector->data();
|
2014-10-15 01:49:49 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-12-01 05:46:06 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
////
|
2016-12-21 10:31:22 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
return true;
|
2014-09-18 02:08:41 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void WebGL2Context::InvalidateFramebuffer(
|
|
|
|
GLenum target, const dom::Sequence<GLenum>& attachments, ErrorResult& rv) {
|
|
|
|
const FuncScope funcScope(*this, "invalidateFramebuffer");
|
|
|
|
|
|
|
|
std::vector<GLenum> scopedVector;
|
|
|
|
GLsizei glNumAttachments;
|
|
|
|
const GLenum* glAttachments;
|
|
|
|
if (!ValidateInvalidateFramebuffer(target, attachments, &rv, &scopedVector,
|
|
|
|
&glNumAttachments, &glAttachments)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
// Some drivers (like OSX 10.9 GL) just don't support invalidate_framebuffer.
|
|
|
|
const bool useFBInvalidation =
|
|
|
|
(mAllowFBInvalidation &&
|
|
|
|
gl->IsSupported(gl::GLFeature::invalidate_framebuffer));
|
|
|
|
if (useFBInvalidation) {
|
|
|
|
gl->fInvalidateFramebuffer(target, glNumAttachments, glAttachments);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use clear instead?
|
|
|
|
// No-op for now.
|
2016-12-01 05:46:06 +03:00
|
|
|
}
|
2015-01-13 02:05:21 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void WebGL2Context::InvalidateSubFramebuffer(
|
|
|
|
GLenum target, const dom::Sequence<GLenum>& attachments, GLint x, GLint y,
|
|
|
|
GLsizei width, GLsizei height, ErrorResult& rv) {
|
|
|
|
const FuncScope funcScope(*this, "invalidateSubFramebuffer");
|
|
|
|
|
|
|
|
std::vector<GLenum> scopedVector;
|
|
|
|
GLsizei glNumAttachments;
|
|
|
|
const GLenum* glAttachments;
|
|
|
|
if (!ValidateInvalidateFramebuffer(target, attachments, &rv, &scopedVector,
|
|
|
|
&glNumAttachments, &glAttachments)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ValidateNonNegative("width", width) ||
|
|
|
|
!ValidateNonNegative("height", height)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
// Some drivers (like OSX 10.9 GL) just don't support invalidate_framebuffer.
|
|
|
|
const bool useFBInvalidation =
|
|
|
|
(mAllowFBInvalidation &&
|
|
|
|
gl->IsSupported(gl::GLFeature::invalidate_framebuffer));
|
|
|
|
if (useFBInvalidation) {
|
|
|
|
gl->fInvalidateSubFramebuffer(target, glNumAttachments, glAttachments, x, y,
|
|
|
|
width, height);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use clear instead?
|
|
|
|
// No-op for now.
|
2014-09-18 02:08:41 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
void WebGL2Context::ReadBuffer(GLenum mode) {
|
|
|
|
const FuncScope funcScope(*this, "readBuffer");
|
|
|
|
if (IsContextLost()) return;
|
|
|
|
|
|
|
|
if (mBoundReadFramebuffer) {
|
|
|
|
mBoundReadFramebuffer->ReadBuffer(mode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operating on the default framebuffer.
|
|
|
|
if (mode != LOCAL_GL_NONE && mode != LOCAL_GL_BACK) {
|
|
|
|
nsCString enumName;
|
|
|
|
EnumName(mode, &enumName);
|
|
|
|
ErrorInvalidOperation(
|
|
|
|
"If READ_FRAMEBUFFER is null, `mode` must be BACK or"
|
|
|
|
" NONE. Was %s.",
|
|
|
|
enumName.BeginReading());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDefaultFB_ReadBuffer = mode;
|
2014-09-18 02:08:41 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
} // namespace mozilla
|