1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 "WebGLRenderbuffer.h"
9 #include "mozilla/dom/WebGLRenderingContextBinding.h"
10 #include "ScopedGLHelpers.h"
11 #include "WebGLContext.h"
12 #include "WebGLFormats.h"
13 #include "WebGLStrongTypes.h"
14 #include "WebGLTexture.h"
18 static GLenum
DepthFormatForDepthStencilEmu(gl::GLContext
* gl
) {
19 // We might not be able to get 24-bit, so let's pretend!
20 if (gl
->IsGLES() && !gl
->IsExtensionSupported(gl::GLContext::OES_depth24
))
21 return LOCAL_GL_DEPTH_COMPONENT16
;
23 return LOCAL_GL_DEPTH_COMPONENT24
;
26 static GLuint
DoCreateRenderbuffer(gl::GLContext
* gl
) {
27 MOZ_ASSERT(gl
->IsCurrent());
30 gl
->fGenRenderbuffers(1, &ret
);
34 static bool EmulatePackedDepthStencil(gl::GLContext
* gl
) {
35 return !gl
->IsSupported(gl::GLFeature::packed_depth_stencil
);
38 WebGLRenderbuffer::WebGLRenderbuffer(WebGLContext
* webgl
)
39 : WebGLContextBoundObject(webgl
),
40 mPrimaryRB(DoCreateRenderbuffer(webgl
->gl
)),
41 mEmulatePackedDepthStencil(EmulatePackedDepthStencil(webgl
->gl
)),
43 // Bind our RB, or we might end up calling FramebufferRenderbuffer before we
44 // ever call BindRenderbuffer, since webgl.bindRenderbuffer doesn't actually
45 // call glBindRenderbuffer anymore.
46 mContext
->gl
->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER
, mPrimaryRB
);
47 mContext
->gl
->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER
, 0);
50 WebGLRenderbuffer::~WebGLRenderbuffer() {
51 mImageInfo
= webgl::ImageInfo();
53 if (!mContext
) return;
55 mContext
->gl
->fDeleteRenderbuffers(1, &mPrimaryRB
);
57 mContext
->gl
->fDeleteRenderbuffers(1, &mSecondaryRB
);
61 static GLenum
DoRenderbufferStorageMaybeMultisample(gl::GLContext
* gl
,
63 GLenum internalFormat
,
66 MOZ_ASSERT_IF(samples
>= 1,
67 gl
->IsSupported(gl::GLFeature::framebuffer_multisample
));
69 // Certain OpenGL ES renderbuffer formats may not exist on desktop OpenGL.
70 switch (internalFormat
) {
72 case LOCAL_GL_RGB5_A1
:
73 // 16-bit RGBA formats are not supported on desktop GL.
74 if (!gl
->IsGLES()) internalFormat
= LOCAL_GL_RGBA8
;
78 // RGB565 is not supported on desktop GL.
79 if (!gl
->IsGLES()) internalFormat
= LOCAL_GL_RGB8
;
82 case LOCAL_GL_DEPTH_COMPONENT16
:
83 if (!gl
->IsGLES() || gl
->IsExtensionSupported(gl::GLContext::OES_depth24
))
84 internalFormat
= LOCAL_GL_DEPTH_COMPONENT24
;
85 else if (gl
->IsSupported(gl::GLFeature::packed_depth_stencil
))
86 internalFormat
= LOCAL_GL_DEPTH24_STENCIL8
;
89 case LOCAL_GL_DEPTH_STENCIL
:
90 MOZ_CRASH("GFX: GL_DEPTH_STENCIL is not valid here.");
97 gl::GLContext::LocalErrorScope
errorScope(*gl
);
100 gl
->fRenderbufferStorageMultisample(LOCAL_GL_RENDERBUFFER
, samples
,
101 internalFormat
, width
, height
);
103 gl
->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER
, internalFormat
, width
,
107 return errorScope
.GetError();
110 GLenum
WebGLRenderbuffer::DoRenderbufferStorage(
111 uint32_t samples
, const webgl::FormatUsageInfo
* format
, uint32_t width
,
113 gl::GLContext
* gl
= mContext
->gl
;
114 MOZ_ASSERT(samples
<= 256); // Sanity check.
116 GLenum primaryFormat
= format
->format
->sizedFormat
;
117 GLenum secondaryFormat
= 0;
119 if (mEmulatePackedDepthStencil
&&
120 primaryFormat
== LOCAL_GL_DEPTH24_STENCIL8
) {
121 primaryFormat
= DepthFormatForDepthStencilEmu(gl
);
122 secondaryFormat
= LOCAL_GL_STENCIL_INDEX8
;
125 gl
->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER
, mPrimaryRB
);
126 GLenum error
= DoRenderbufferStorageMaybeMultisample(
127 gl
, samples
, primaryFormat
, width
, height
);
128 gl
->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER
, 0);
129 if (error
) return error
;
131 if (secondaryFormat
) {
133 gl
->fGenRenderbuffers(1, &mSecondaryRB
);
136 gl
->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER
, mSecondaryRB
);
137 error
= DoRenderbufferStorageMaybeMultisample(gl
, samples
, secondaryFormat
,
139 gl
->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER
, 0);
140 if (error
) return error
;
141 } else if (mSecondaryRB
) {
142 gl
->fDeleteRenderbuffers(1, &mSecondaryRB
);
149 void WebGLRenderbuffer::RenderbufferStorage(uint32_t samples
,
150 GLenum internalFormat
,
151 uint32_t width
, uint32_t height
) {
152 const auto usage
= mContext
->mFormatUsage
->GetRBUsage(internalFormat
);
154 mContext
->ErrorInvalidEnum("Invalid `internalFormat`: 0x%04x.",
159 if (width
> mContext
->mGLMaxRenderbufferSize
||
160 height
> mContext
->mGLMaxRenderbufferSize
) {
161 mContext
->ErrorInvalidValue(
162 "Width or height exceeds maximum renderbuffer"
167 const auto maxSamples
= usage
->MaxSamples(*mContext
->gl
);
168 if (samples
> maxSamples
) {
169 mContext
->ErrorInvalidOperation("`samples` is out of the valid range.");
173 // Validation complete.
175 const GLenum error
= DoRenderbufferStorage(samples
, usage
, width
, height
);
177 mContext
->GenerateWarning("Unexpected error %s", EnumString(error
).c_str());
178 if (error
== LOCAL_GL_OUT_OF_MEMORY
) {
186 mContext
->OnDataAllocCall();
188 const uint32_t depth
= 1;
189 auto uninitializedSlices
= Some(std::vector
<bool>(depth
, true));
191 usage
, width
, height
, depth
, std::move(uninitializedSlices
),
196 void WebGLRenderbuffer::DoFramebufferRenderbuffer(
197 const GLenum attachment
) const {
198 gl::GLContext
* gl
= mContext
->gl
;
200 if (attachment
== LOCAL_GL_DEPTH_STENCIL_ATTACHMENT
) {
201 const GLuint stencilRB
= (mSecondaryRB
? mSecondaryRB
: mPrimaryRB
);
202 gl
->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER
,
203 LOCAL_GL_DEPTH_ATTACHMENT
,
204 LOCAL_GL_RENDERBUFFER
, mPrimaryRB
);
205 gl
->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER
,
206 LOCAL_GL_STENCIL_ATTACHMENT
,
207 LOCAL_GL_RENDERBUFFER
, stencilRB
);
211 gl
->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER
, attachment
,
212 LOCAL_GL_RENDERBUFFER
, mPrimaryRB
);
215 GLint
WebGLRenderbuffer::GetRenderbufferParameter(RBParam pname
) const {
216 gl::GLContext
* gl
= mContext
->gl
;
218 switch (pname
.get()) {
219 case LOCAL_GL_RENDERBUFFER_STENCIL_SIZE
:
220 if (!mImageInfo
.mFormat
) return 0;
222 if (!mImageInfo
.mFormat
->format
->s
) return 0;
226 case LOCAL_GL_RENDERBUFFER_SAMPLES
:
227 case LOCAL_GL_RENDERBUFFER_WIDTH
:
228 case LOCAL_GL_RENDERBUFFER_HEIGHT
:
229 case LOCAL_GL_RENDERBUFFER_RED_SIZE
:
230 case LOCAL_GL_RENDERBUFFER_GREEN_SIZE
:
231 case LOCAL_GL_RENDERBUFFER_BLUE_SIZE
:
232 case LOCAL_GL_RENDERBUFFER_ALPHA_SIZE
:
233 case LOCAL_GL_RENDERBUFFER_DEPTH_SIZE
: {
234 gl
->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER
, mPrimaryRB
);
236 gl
->fGetRenderbufferParameteriv(LOCAL_GL_RENDERBUFFER
, pname
.get(), &i
);
237 gl
->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER
, 0);
241 case LOCAL_GL_RENDERBUFFER_INTERNAL_FORMAT
: {
242 GLenum ret
= LOCAL_GL_RGBA4
;
243 if (mImageInfo
.mFormat
) {
244 ret
= mImageInfo
.mFormat
->format
->sizedFormat
;
246 if (!mContext
->IsWebGL2() && ret
== LOCAL_GL_DEPTH24_STENCIL8
) {
247 ret
= LOCAL_GL_DEPTH_STENCIL
;
254 MOZ_ASSERT(false, "This function should only be called with valid `pname`.");
258 } // namespace mozilla