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"
|
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
|
|
|
|
|
|
|
void
|
|
|
|
WebGL2Context::BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
|
|
|
|
GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
|
|
|
|
GLbitfield mask, GLenum filter)
|
|
|
|
{
|
2015-12-15 03:11:59 +03:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
2015-01-13 02:05:21 +03:00
|
|
|
const GLbitfield validBits = LOCAL_GL_COLOR_BUFFER_BIT |
|
|
|
|
LOCAL_GL_DEPTH_BUFFER_BIT |
|
|
|
|
LOCAL_GL_STENCIL_BUFFER_BIT;
|
|
|
|
if ((mask | validBits) != validBits) {
|
|
|
|
ErrorInvalidValue("blitFramebuffer: Invalid bit set in mask.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (filter) {
|
|
|
|
case LOCAL_GL_NEAREST:
|
|
|
|
case LOCAL_GL_LINEAR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ErrorInvalidEnumInfo("blitFramebuffer: Bad `filter`:", filter);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
////
|
2015-11-24 06:27:13 +03:00
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
const auto& readFB = mBoundReadFramebuffer;
|
|
|
|
if (readFB &&
|
|
|
|
!readFB->ValidateAndInitAttachments("blitFramebuffer's READ_FRAMEBUFFER"))
|
2015-01-13 02:05:21 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
const auto& drawFB = mBoundDrawFramebuffer;
|
|
|
|
if (drawFB &&
|
|
|
|
!drawFB->ValidateAndInitAttachments("blitFramebuffer's DRAW_FRAMEBUFFER"))
|
2015-01-13 02:05:21 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
////
|
2015-07-03 04:55:43 +03:00
|
|
|
|
2016-12-21 10:31:22 +03:00
|
|
|
if (!mBoundReadFramebuffer) {
|
|
|
|
ClearBackbufferIfNeeded();
|
|
|
|
}
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
WebGLFramebuffer::BlitFramebuffer(this,
|
|
|
|
readFB, srcX0, srcY0, srcX1, srcY1,
|
|
|
|
drawFB, dstX0, dstY0, dstX1, dstY1,
|
|
|
|
mask, filter);
|
2015-07-03 04:55:43 +03:00
|
|
|
}
|
|
|
|
|
2014-09-18 02:08:41 +04:00
|
|
|
void
|
2015-07-03 04:55:43 +03:00
|
|
|
WebGL2Context::FramebufferTextureLayer(GLenum target, GLenum attachment,
|
|
|
|
WebGLTexture* texture, GLint level, GLint layer)
|
2014-09-18 02:08:41 +04:00
|
|
|
{
|
2016-07-19 10:36:54 +03:00
|
|
|
const char funcName[] = "framebufferTextureLayer";
|
2015-07-03 04:55:43 +03:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
if (!ValidateFramebufferTarget(target, funcName))
|
2015-07-03 04:55:43 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
WebGLFramebuffer* fb;
|
|
|
|
switch (target) {
|
|
|
|
case LOCAL_GL_FRAMEBUFFER:
|
|
|
|
case LOCAL_GL_DRAW_FRAMEBUFFER:
|
|
|
|
fb = mBoundDrawFramebuffer;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_READ_FRAMEBUFFER:
|
|
|
|
fb = mBoundReadFramebuffer;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_CRASH("GFX: Bad target.");
|
2015-07-03 04:55:43 +03:00
|
|
|
}
|
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
if (!fb)
|
2016-09-28 23:54:07 +03:00
|
|
|
return ErrorInvalidOperation("%s: Cannot modify framebuffer 0.", funcName);
|
2015-07-03 04:55:43 +03:00
|
|
|
|
2016-07-19 10:36:54 +03:00
|
|
|
fb->FramebufferTextureLayer(funcName, attachment, texture, level, layer);
|
2014-09-18 02:08:41 +04:00
|
|
|
}
|
|
|
|
|
2015-08-06 09:31:00 +03:00
|
|
|
JS::Value
|
|
|
|
WebGL2Context::GetFramebufferAttachmentParameter(JSContext* cx,
|
|
|
|
GLenum target,
|
|
|
|
GLenum attachment,
|
|
|
|
GLenum pname,
|
2015-11-25 07:15:29 +03:00
|
|
|
ErrorResult& out_error)
|
2015-08-06 09:31:00 +03:00
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
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
|
|
|
////
|
|
|
|
|
2015-05-25 14:50:15 +03:00
|
|
|
static bool
|
2016-12-01 05:46:06 +03:00
|
|
|
ValidateBackbufferAttachmentEnum(WebGLContext* webgl, const char* funcName,
|
|
|
|
GLenum attachment)
|
2014-10-15 01:49:49 +04:00
|
|
|
{
|
2016-12-01 05:46:06 +03:00
|
|
|
switch (attachment) {
|
|
|
|
case LOCAL_GL_COLOR:
|
|
|
|
case LOCAL_GL_DEPTH:
|
|
|
|
case LOCAL_GL_STENCIL:
|
|
|
|
return true;
|
2015-05-25 14:50:15 +03:00
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
default:
|
|
|
|
webgl->ErrorInvalidEnum("%s: attachment: invalid enum value 0x%x.",
|
|
|
|
funcName, attachment);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 14:50:15 +03:00
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
static bool
|
|
|
|
ValidateFramebufferAttachmentEnum(WebGLContext* webgl, const char* funcName,
|
|
|
|
GLenum attachment)
|
|
|
|
{
|
|
|
|
switch (attachment) {
|
|
|
|
case LOCAL_GL_DEPTH_ATTACHMENT:
|
|
|
|
case LOCAL_GL_STENCIL_ATTACHMENT:
|
|
|
|
case LOCAL_GL_DEPTH_STENCIL_ATTACHMENT:
|
|
|
|
return true;
|
2016-12-23 04:08:35 +03:00
|
|
|
}
|
2016-12-01 05:46:06 +03:00
|
|
|
|
2016-12-23 04:08:35 +03:00
|
|
|
if (attachment < LOCAL_GL_COLOR_ATTACHMENT0) {
|
2016-12-01 05:46:06 +03:00
|
|
|
webgl->ErrorInvalidEnum("%s: attachment: invalid enum value 0x%x.",
|
|
|
|
funcName, attachment);
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-23 04:08:35 +03:00
|
|
|
|
|
|
|
if (attachment > webgl->LastColorAttachmentEnum()) {
|
|
|
|
// That these errors have different types is ridiculous.
|
|
|
|
webgl->ErrorInvalidOperation("%s: Too-large LOCAL_GL_COLOR_ATTACHMENTn.",
|
|
|
|
funcName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-10-15 01:49:49 +04:00
|
|
|
}
|
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
bool
|
|
|
|
WebGLContext::ValidateInvalidateFramebuffer(const char* funcName, 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)
|
2014-09-18 02:08:41 +04:00
|
|
|
{
|
2014-10-15 01:49:49 +04:00
|
|
|
if (IsContextLost())
|
2016-12-01 05:46:06 +03:00
|
|
|
return false;
|
2015-01-13 02:05:21 +03:00
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
gl->MakeCurrent();
|
2014-10-15 01:49:49 +04:00
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
if (!ValidateFramebufferTarget(target, funcName))
|
2016-12-01 05:46:06 +03:00
|
|
|
return false;
|
2015-01-13 02:05:21 +03:00
|
|
|
|
|
|
|
const WebGLFramebuffer* fb;
|
|
|
|
bool isDefaultFB;
|
|
|
|
switch (target) {
|
|
|
|
case LOCAL_GL_FRAMEBUFFER:
|
|
|
|
case LOCAL_GL_DRAW_FRAMEBUFFER:
|
|
|
|
fb = mBoundDrawFramebuffer;
|
|
|
|
isDefaultFB = gl->Screen()->IsDrawFramebufferDefault();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_GL_READ_FRAMEBUFFER:
|
|
|
|
fb = mBoundReadFramebuffer;
|
|
|
|
isDefaultFB = gl->Screen()->IsReadFramebufferDefault();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_CRASH("GFX: Bad target.");
|
2015-01-13 02:05:21 +03:00
|
|
|
}
|
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
*out_glNumAttachments = attachments.Length();
|
|
|
|
*out_glAttachments = attachments.Elements();
|
2015-03-17 02:29:37 +03:00
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
if (fb) {
|
|
|
|
for (const auto& attachment : attachments) {
|
|
|
|
if (!ValidateFramebufferAttachmentEnum(this, funcName, attachment))
|
|
|
|
return false;
|
2015-05-25 14:50:15 +03:00
|
|
|
}
|
2014-10-15 01:49:49 +04:00
|
|
|
} else {
|
2016-12-01 05:46:06 +03:00
|
|
|
for (const auto& attachment : attachments) {
|
|
|
|
if (!ValidateBackbufferAttachmentEnum(this, funcName, attachment))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*out_glNumAttachments = scopedVector->size();
|
|
|
|
*out_glAttachments = scopedVector->data();
|
|
|
|
}
|
2014-10-15 01:49:49 +04:00
|
|
|
}
|
2016-12-01 05:46:06 +03:00
|
|
|
|
2016-12-21 10:31:22 +03:00
|
|
|
////
|
|
|
|
|
|
|
|
if (!fb) {
|
|
|
|
ClearBackbufferIfNeeded();
|
|
|
|
|
|
|
|
// Don't do more validation after these.
|
|
|
|
Invalidate();
|
|
|
|
mShouldPresent = true;
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
return true;
|
2014-09-18 02:08:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-12-01 05:46:06 +03:00
|
|
|
WebGL2Context::InvalidateFramebuffer(GLenum target,
|
|
|
|
const dom::Sequence<GLenum>& attachments,
|
|
|
|
ErrorResult& rv)
|
2014-09-18 02:08:41 +04:00
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
const char funcName[] = "invalidateSubFramebuffer";
|
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
std::vector<GLenum> scopedVector;
|
|
|
|
GLsizei glNumAttachments;
|
|
|
|
const GLenum* glAttachments;
|
|
|
|
if (!ValidateInvalidateFramebuffer(funcName, target, attachments, &rv, &scopedVector,
|
|
|
|
&glNumAttachments, &glAttachments))
|
|
|
|
{
|
2014-10-15 01:49:49 +04:00
|
|
|
return;
|
2016-12-01 05:46:06 +03:00
|
|
|
}
|
2015-01-13 02:05:21 +03:00
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
////
|
2015-01-13 02:05:21 +03:00
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
// 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);
|
2015-11-25 07:15:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
// Use clear instead?
|
|
|
|
// No-op for now.
|
|
|
|
}
|
2015-01-13 02:05:21 +03:00
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
void
|
|
|
|
WebGL2Context::InvalidateSubFramebuffer(GLenum target, const dom::Sequence<GLenum>& attachments,
|
|
|
|
GLint x, GLint y, GLsizei width, GLsizei height,
|
|
|
|
ErrorResult& rv)
|
|
|
|
{
|
|
|
|
const char funcName[] = "invalidateSubFramebuffer";
|
2015-01-13 02:05:21 +03:00
|
|
|
|
2016-12-21 10:31:22 +03:00
|
|
|
if (!ValidateNonNegative(funcName, "width", width) ||
|
|
|
|
!ValidateNonNegative(funcName, "height", height))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
std::vector<GLenum> scopedVector;
|
|
|
|
GLsizei glNumAttachments;
|
|
|
|
const GLenum* glAttachments;
|
|
|
|
if (!ValidateInvalidateFramebuffer(funcName, target, attachments, &rv, &scopedVector,
|
|
|
|
&glNumAttachments, &glAttachments))
|
|
|
|
{
|
|
|
|
return;
|
2015-01-13 02:05:21 +03:00
|
|
|
}
|
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
////
|
2014-10-15 01:49:49 +04:00
|
|
|
|
2016-12-01 05:46:06 +03:00
|
|
|
// 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);
|
2015-03-17 02:29:37 +03:00
|
|
|
return;
|
2014-10-15 01:49:49 +04:00
|
|
|
}
|
2016-12-01 05:46:06 +03:00
|
|
|
|
|
|
|
// Use clear instead?
|
|
|
|
// No-op for now.
|
2014-09-18 02:08:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGL2Context::ReadBuffer(GLenum mode)
|
|
|
|
{
|
2016-07-19 10:36:54 +03:00
|
|
|
const char funcName[] = "readBuffer";
|
2015-02-19 03:57:05 +03:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mBoundReadFramebuffer) {
|
2016-07-19 10:36:54 +03:00
|
|
|
mBoundReadFramebuffer->ReadBuffer(funcName, mode);
|
2015-02-19 03:57:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operating on the default framebuffer.
|
|
|
|
if (mode != LOCAL_GL_NONE &&
|
|
|
|
mode != LOCAL_GL_BACK)
|
|
|
|
{
|
2016-12-22 06:42:07 +03:00
|
|
|
nsCString enumName;
|
|
|
|
EnumName(mode, &enumName);
|
2016-07-19 10:36:54 +03:00
|
|
|
ErrorInvalidOperation("%s: If READ_FRAMEBUFFER is null, `mode` must be BACK or"
|
2016-12-22 06:42:07 +03:00
|
|
|
" NONE. Was %s.",
|
|
|
|
funcName, enumName.BeginReading());
|
2015-02-19 03:57:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->Screen()->SetReadBuffer(mode);
|
2014-09-18 02:08:41 +04:00
|
|
|
}
|
|
|
|
|
2015-07-15 03:37:28 +03:00
|
|
|
} // namespace mozilla
|