1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "WebGLContext.h"
7 #include "WebGLTexture.h"
8 #include "WebGLRenderbuffer.h"
9 #include "WebGLFramebuffer.h"
10 #include "GLContext.h"
12 using namespace mozilla
;
15 WebGLContext::Clear(GLbitfield mask
)
22 uint32_t m
= mask
& (LOCAL_GL_COLOR_BUFFER_BIT
| LOCAL_GL_DEPTH_BUFFER_BIT
| LOCAL_GL_STENCIL_BUFFER_BIT
);
24 return ErrorInvalidValue("clear: invalid mask bits");
27 GenerateWarning("Calling gl.clear(0) has no effect.");
28 } else if (mRasterizerDiscardEnabled
) {
29 GenerateWarning("Calling gl.clear() with RASTERIZER_DISCARD enabled has no effects.");
32 if (mBoundFramebuffer
) {
33 if (!mBoundFramebuffer
->CheckAndInitializeAttachments())
34 return ErrorInvalidFramebufferOperation("clear: incomplete framebuffer");
39 ClearBackbufferIfNeeded();
42 // Ok, we're clearing the default framebuffer/screen.
44 ScopedMaskWorkaround
autoMask(*this);
49 mShouldPresent
= true;
53 GLClampFloat(GLclampf val
)
65 WebGLContext::ClearColor(GLclampf r
, GLclampf g
,
66 GLclampf b
, GLclampf a
)
72 mColorClearValue
[0] = GLClampFloat(r
);
73 mColorClearValue
[1] = GLClampFloat(g
);
74 mColorClearValue
[2] = GLClampFloat(b
);
75 mColorClearValue
[3] = GLClampFloat(a
);
76 gl
->fClearColor(r
, g
, b
, a
);
80 WebGLContext::ClearDepth(GLclampf v
)
86 mDepthClearValue
= GLClampFloat(v
);
91 WebGLContext::ClearStencil(GLint v
)
97 mStencilClearValue
= v
;
102 WebGLContext::ColorMask(WebGLboolean r
, WebGLboolean g
, WebGLboolean b
, WebGLboolean a
)
107 MakeContextCurrent();
108 mColorWriteMask
[0] = r
;
109 mColorWriteMask
[1] = g
;
110 mColorWriteMask
[2] = b
;
111 mColorWriteMask
[3] = a
;
112 gl
->fColorMask(r
, g
, b
, a
);
116 WebGLContext::DepthMask(WebGLboolean b
)
121 MakeContextCurrent();
127 WebGLContext::DrawBuffers(const dom::Sequence
<GLenum
>& buffers
)
132 const size_t buffersLength
= buffers
.Length();
134 if (buffersLength
== 0) {
135 return ErrorInvalidValue("drawBuffers: invalid <buffers> (buffers must not be empty)");
138 if (mBoundFramebuffer
== 0)
140 // OK: we are rendering in the default framebuffer
142 /* EXT_draw_buffers :
143 If the GL is bound to the default framebuffer, then <buffersLength> must be 1
144 and the constant must be BACK or NONE. When draw buffer zero is
145 BACK, color values are written into the sole buffer for single-
146 buffered contexts, or into the back buffer for double-buffered
147 contexts. If DrawBuffersEXT is supplied with a constant other than
148 BACK and NONE, the error INVALID_OPERATION is generated.
150 if (buffersLength
!= 1) {
151 return ErrorInvalidValue("drawBuffers: invalid <buffers> (main framebuffer: buffers.length must be 1)");
154 MakeContextCurrent();
156 if (buffers
[0] == LOCAL_GL_NONE
) {
157 const GLenum drawBuffersCommand
= LOCAL_GL_NONE
;
158 gl
->fDrawBuffers(1, &drawBuffersCommand
);
161 else if (buffers
[0] == LOCAL_GL_BACK
) {
162 const GLenum drawBuffersCommand
= LOCAL_GL_COLOR_ATTACHMENT0
;
163 gl
->fDrawBuffers(1, &drawBuffersCommand
);
166 return ErrorInvalidOperation("drawBuffers: invalid operation (main framebuffer: buffers[0] must be GL_NONE or GL_BACK)");
169 // OK: we are rendering in a framebuffer object
171 if (buffersLength
> size_t(mGLMaxDrawBuffers
)) {
172 /* EXT_draw_buffers :
173 The maximum number of draw buffers is implementation-dependent. The
174 number of draw buffers supported can be queried by calling
175 GetIntegerv with the symbolic constant MAX_DRAW_BUFFERS_EXT. An
176 INVALID_VALUE error is generated if <buffersLength> is greater than
177 MAX_DRAW_BUFFERS_EXT.
179 return ErrorInvalidValue("drawBuffers: invalid <buffers> (buffers.length > GL_MAX_DRAW_BUFFERS)");
182 for (uint32_t i
= 0; i
< buffersLength
; i
++)
184 /* EXT_draw_buffers :
185 If the GL is bound to a draw framebuffer object, the <i>th buffer listed
186 in <bufs> must be COLOR_ATTACHMENT<i>_EXT or NONE. Specifying a
187 buffer out of order, BACK, or COLOR_ATTACHMENT<m>_EXT where <m> is
188 greater than or equal to the value of MAX_COLOR_ATTACHMENTS_EXT,
189 will generate the error INVALID_OPERATION.
191 /* WEBGL_draw_buffers :
192 The value of the MAX_COLOR_ATTACHMENTS_WEBGL parameter must be greater than or equal to that of the MAX_DRAW_BUFFERS_WEBGL parameter.
194 if (buffers
[i
] != LOCAL_GL_NONE
&&
195 buffers
[i
] != GLenum(LOCAL_GL_COLOR_ATTACHMENT0
+ i
)) {
196 return ErrorInvalidOperation("drawBuffers: invalid operation (buffers[i] must be GL_NONE or GL_COLOR_ATTACHMENTi)");
200 MakeContextCurrent();
202 gl
->fDrawBuffers(buffersLength
, buffers
.Elements());
206 WebGLContext::StencilMask(GLuint mask
)
211 mStencilWriteMaskFront
= mask
;
212 mStencilWriteMaskBack
= mask
;
214 MakeContextCurrent();
215 gl
->fStencilMask(mask
);
219 WebGLContext::StencilMaskSeparate(GLenum face
, GLuint mask
)
224 if (!ValidateFaceEnum(face
, "stencilMaskSeparate: face"))
228 case LOCAL_GL_FRONT_AND_BACK
:
229 mStencilWriteMaskFront
= mask
;
230 mStencilWriteMaskBack
= mask
;
233 mStencilWriteMaskFront
= mask
;
236 mStencilWriteMaskBack
= mask
;
240 MakeContextCurrent();
241 gl
->fStencilMaskSeparate(face
, mask
);