2013-07-17 20:13:38 +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 "WebGLTexture.h"
|
|
|
|
#include "WebGLRenderbuffer.h"
|
|
|
|
#include "WebGLFramebuffer.h"
|
2013-09-04 16:14:52 +04:00
|
|
|
#include "GLContext.h"
|
2015-07-31 22:56:32 +03:00
|
|
|
#include "GLScreenBuffer.h"
|
2013-07-17 20:13:38 +04:00
|
|
|
|
2015-07-15 03:37:28 +03:00
|
|
|
namespace mozilla {
|
2013-07-17 20:13:38 +04:00
|
|
|
|
|
|
|
void
|
2013-09-04 16:14:43 +04:00
|
|
|
WebGLContext::Clear(GLbitfield mask)
|
2013-07-17 20:13:38 +04:00
|
|
|
{
|
2015-12-15 03:11:59 +03:00
|
|
|
const char funcName[] = "clear";
|
|
|
|
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2013-07-17 20:13:38 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
MakeContextCurrent();
|
|
|
|
|
|
|
|
uint32_t m = mask & (LOCAL_GL_COLOR_BUFFER_BIT | LOCAL_GL_DEPTH_BUFFER_BIT | LOCAL_GL_STENCIL_BUFFER_BIT);
|
|
|
|
if (mask != m)
|
2015-12-15 03:11:59 +03:00
|
|
|
return ErrorInvalidValue("%s: invalid mask bits", funcName);
|
2013-07-17 20:13:38 +04:00
|
|
|
|
2013-08-06 18:40:37 +04:00
|
|
|
if (mask == 0) {
|
|
|
|
GenerateWarning("Calling gl.clear(0) has no effect.");
|
2013-08-23 04:11:40 +04:00
|
|
|
} else if (mRasterizerDiscardEnabled) {
|
|
|
|
GenerateWarning("Calling gl.clear() with RASTERIZER_DISCARD enabled has no effects.");
|
2013-08-06 18:40:37 +04:00
|
|
|
}
|
|
|
|
|
2015-01-13 02:05:21 +03:00
|
|
|
if (mBoundDrawFramebuffer) {
|
2015-12-15 03:11:59 +03:00
|
|
|
if (!mBoundDrawFramebuffer->ValidateAndInitAttachments(funcName))
|
|
|
|
return;
|
2013-07-17 20:13:38 +04:00
|
|
|
|
|
|
|
gl->fClear(mask);
|
|
|
|
return;
|
2014-04-18 00:15:41 +04:00
|
|
|
} else {
|
|
|
|
ClearBackbufferIfNeeded();
|
2013-07-17 20:13:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, we're clearing the default framebuffer/screen.
|
2014-10-02 04:05:34 +04:00
|
|
|
{
|
|
|
|
ScopedMaskWorkaround autoMask(*this);
|
|
|
|
gl->fClear(mask);
|
|
|
|
}
|
2013-07-17 20:13:38 +04:00
|
|
|
|
|
|
|
Invalidate();
|
|
|
|
mShouldPresent = true;
|
|
|
|
}
|
|
|
|
|
2015-02-25 01:09:09 +03:00
|
|
|
static GLfloat
|
|
|
|
GLClampFloat(GLfloat val)
|
2013-07-17 20:13:38 +04:00
|
|
|
{
|
|
|
|
if (val < 0.0)
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
if (val > 1.0)
|
|
|
|
return 1.0;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-25 01:09:09 +03:00
|
|
|
WebGLContext::ClearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
|
2013-07-17 20:13:38 +04:00
|
|
|
{
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2013-07-17 20:13:38 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
MakeContextCurrent();
|
2015-02-25 01:09:09 +03:00
|
|
|
|
2016-05-28 04:55:13 +03:00
|
|
|
const bool supportsFloatColorBuffers = (IsExtensionEnabled(WebGLExtensionID::EXT_color_buffer_float) ||
|
|
|
|
IsExtensionEnabled(WebGLExtensionID::EXT_color_buffer_half_float) ||
|
2015-02-25 01:09:09 +03:00
|
|
|
IsExtensionEnabled(WebGLExtensionID::WEBGL_color_buffer_float));
|
|
|
|
if (!supportsFloatColorBuffers) {
|
|
|
|
r = GLClampFloat(r);
|
|
|
|
g = GLClampFloat(g);
|
|
|
|
b = GLClampFloat(b);
|
|
|
|
a = GLClampFloat(a);
|
|
|
|
}
|
|
|
|
|
2013-07-17 20:13:38 +04:00
|
|
|
gl->fClearColor(r, g, b, a);
|
2015-02-25 01:09:09 +03:00
|
|
|
|
|
|
|
mColorClearValue[0] = r;
|
|
|
|
mColorClearValue[1] = g;
|
|
|
|
mColorClearValue[2] = b;
|
|
|
|
mColorClearValue[3] = a;
|
2013-07-17 20:13:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-04 16:14:43 +04:00
|
|
|
WebGLContext::ClearDepth(GLclampf v)
|
2013-07-17 20:13:38 +04:00
|
|
|
{
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2013-07-17 20:13:38 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
MakeContextCurrent();
|
|
|
|
mDepthClearValue = GLClampFloat(v);
|
2015-08-20 00:50:06 +03:00
|
|
|
gl->fClearDepth(mDepthClearValue);
|
2013-07-17 20:13:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-04 16:14:43 +04:00
|
|
|
WebGLContext::ClearStencil(GLint v)
|
2013-07-17 20:13:38 +04:00
|
|
|
{
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2013-07-17 20:13:38 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
MakeContextCurrent();
|
|
|
|
mStencilClearValue = v;
|
|
|
|
gl->fClearStencil(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLContext::ColorMask(WebGLboolean r, WebGLboolean g, WebGLboolean b, WebGLboolean a)
|
|
|
|
{
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2013-07-17 20:13:38 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
MakeContextCurrent();
|
|
|
|
mColorWriteMask[0] = r;
|
|
|
|
mColorWriteMask[1] = g;
|
|
|
|
mColorWriteMask[2] = b;
|
|
|
|
mColorWriteMask[3] = a;
|
|
|
|
gl->fColorMask(r, g, b, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLContext::DepthMask(WebGLboolean b)
|
|
|
|
{
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2013-07-17 20:13:38 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
MakeContextCurrent();
|
|
|
|
mDepthWriteMask = b;
|
|
|
|
gl->fDepthMask(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebGLContext::DrawBuffers(const dom::Sequence<GLenum>& buffers)
|
|
|
|
{
|
2015-11-25 07:15:29 +03:00
|
|
|
const char funcName[] = "drawBuffers";
|
2013-11-21 10:53:00 +04:00
|
|
|
if (IsContextLost())
|
|
|
|
return;
|
|
|
|
|
2015-01-13 02:05:21 +03:00
|
|
|
if (!mBoundDrawFramebuffer) {
|
2015-11-25 07:15:29 +03:00
|
|
|
// GLES 3.0.4 p186:
|
|
|
|
// "If the GL is bound to the default framebuffer, then `n` must be 1 and the
|
|
|
|
// constant must be BACK or NONE. [...] If DrawBuffers is supplied with a
|
|
|
|
// constant other than BACK and NONE, or with a value of `n` other than 1, the
|
|
|
|
// error INVALID_OPERATION is generated."
|
|
|
|
if (buffers.Length() != 1) {
|
|
|
|
ErrorInvalidOperation("%s: For the default framebuffer, `buffers` must have a"
|
|
|
|
" length of 1.",
|
|
|
|
funcName);
|
|
|
|
return;
|
2013-07-17 20:13:38 +04:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
switch (buffers[0]) {
|
|
|
|
case LOCAL_GL_NONE:
|
|
|
|
case LOCAL_GL_BACK:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ErrorInvalidOperation("%s: For the default framebuffer, `buffers[0]` must be"
|
|
|
|
" BACK or NONE.",
|
|
|
|
funcName);
|
2013-07-17 20:13:38 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-11-25 07:15:29 +03:00
|
|
|
|
|
|
|
mDefaultFB_DrawBuffer0 = buffers[0];
|
|
|
|
gl->Screen()->SetDrawBuffer(buffers[0]);
|
|
|
|
return;
|
2013-07-17 20:13:38 +04:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
// Framebuffer object (not default framebuffer)
|
|
|
|
|
|
|
|
if (buffers.Length() > mImplMaxDrawBuffers) {
|
|
|
|
// "An INVALID_VALUE error is generated if `n` is greater than MAX_DRAW_BUFFERS."
|
|
|
|
ErrorInvalidValue("%s: `buffers` must have a length <= MAX_DRAW_BUFFERS.",
|
|
|
|
funcName);
|
|
|
|
return;
|
2013-07-17 20:13:38 +04:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
for (size_t i = 0; i < buffers.Length(); i++) {
|
|
|
|
// "If the GL is bound to a draw framebuffer object, the `i`th buffer listed in
|
|
|
|
// bufs must be COLOR_ATTACHMENTi or NONE. Specifying a buffer out of order,
|
|
|
|
// BACK, or COLOR_ATTACHMENTm where `m` is greater than or equal to the value of
|
|
|
|
// MAX_COLOR_ATTACHMENTS, will generate the error INVALID_OPERATION.
|
|
|
|
|
|
|
|
// WEBGL_draw_buffers:
|
|
|
|
// "The value of the MAX_COLOR_ATTACHMENTS_WEBGL parameter must be greater than or
|
|
|
|
// equal to that of the MAX_DRAW_BUFFERS_WEBGL parameter."
|
|
|
|
// This means that if buffers.Length() isn't larger than MaxDrawBuffers, it won't
|
|
|
|
// be larger than MaxColorAttachments.
|
2013-07-17 20:13:38 +04:00
|
|
|
if (buffers[i] != LOCAL_GL_NONE &&
|
2015-11-25 07:15:29 +03:00
|
|
|
buffers[i] != LOCAL_GL_COLOR_ATTACHMENT0 + i)
|
|
|
|
{
|
|
|
|
ErrorInvalidOperation("%s: `buffers[i]` must be NONE or COLOR_ATTACHMENTi.",
|
|
|
|
funcName);
|
|
|
|
return;
|
2013-07-17 20:13:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MakeContextCurrent();
|
|
|
|
|
2015-11-25 07:15:29 +03:00
|
|
|
const GLenum* ptr = nullptr;
|
|
|
|
if (buffers.Length()) {
|
|
|
|
ptr = buffers.Elements();
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->fDrawBuffers(buffers.Length(), ptr);
|
|
|
|
|
|
|
|
const auto end = ptr + buffers.Length();
|
|
|
|
mBoundDrawFramebuffer->mDrawBuffers.assign(ptr, end);
|
2013-07-17 20:13:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-04 16:14:43 +04:00
|
|
|
WebGLContext::StencilMask(GLuint mask)
|
2013-07-17 20:13:38 +04:00
|
|
|
{
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2013-07-17 20:13:38 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
mStencilWriteMaskFront = mask;
|
|
|
|
mStencilWriteMaskBack = mask;
|
|
|
|
|
|
|
|
MakeContextCurrent();
|
|
|
|
gl->fStencilMask(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-04 16:14:43 +04:00
|
|
|
WebGLContext::StencilMaskSeparate(GLenum face, GLuint mask)
|
2013-07-17 20:13:38 +04:00
|
|
|
{
|
2013-09-04 16:14:44 +04:00
|
|
|
if (IsContextLost())
|
2013-07-17 20:13:38 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!ValidateFaceEnum(face, "stencilMaskSeparate: face"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (face) {
|
|
|
|
case LOCAL_GL_FRONT_AND_BACK:
|
|
|
|
mStencilWriteMaskFront = mask;
|
|
|
|
mStencilWriteMaskBack = mask;
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_FRONT:
|
|
|
|
mStencilWriteMaskFront = mask;
|
|
|
|
break;
|
|
|
|
case LOCAL_GL_BACK:
|
|
|
|
mStencilWriteMaskBack = mask;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MakeContextCurrent();
|
|
|
|
gl->fStencilMaskSeparate(face, mask);
|
|
|
|
}
|
|
|
|
|
2015-07-15 03:37:28 +03:00
|
|
|
} // namespace mozilla
|