summaryrefslogtreecommitdiff
path: root/dom/canvas/WebGLRenderbuffer.cpp
blob: 32397dd1ad5bbc8cbcde01eabc5f09a450e883e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* -*- Mode: C++; tab-width: 20; 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 "WebGLRenderbuffer.h"

#include "GLContext.h"
#include "mozilla/dom/WebGLRenderingContextBinding.h"
#include "ScopedGLHelpers.h"
#include "WebGLContext.h"
#include "WebGLStrongTypes.h"
#include "WebGLTexture.h"

namespace mozilla {

static GLenum
DepthFormatForDepthStencilEmu(gl::GLContext* gl)
{
    // We might not be able to get 24-bit, so let's pretend!
    if (gl->IsGLES() && !gl->IsExtensionSupported(gl::GLContext::OES_depth24))
        return LOCAL_GL_DEPTH_COMPONENT16;

    return LOCAL_GL_DEPTH_COMPONENT24;
}

JSObject*
WebGLRenderbuffer::WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto)
{
    return dom::WebGLRenderbufferBinding::Wrap(cx, this, givenProto);
}

static GLuint
DoCreateRenderbuffer(gl::GLContext* gl)
{
    MOZ_ASSERT(gl->IsCurrent());

    GLuint ret = 0;
    gl->fGenRenderbuffers(1, &ret);
    return ret;
}

static bool
EmulatePackedDepthStencil(gl::GLContext* gl)
{
    return !gl->IsSupported(gl::GLFeature::packed_depth_stencil);
}

WebGLRenderbuffer::WebGLRenderbuffer(WebGLContext* webgl)
    : WebGLRefCountedObject(webgl)
    , mPrimaryRB( DoCreateRenderbuffer(webgl->gl) )
    , mEmulatePackedDepthStencil( EmulatePackedDepthStencil(webgl->gl) )
    , mSecondaryRB(0)
    , mFormat(nullptr)
    , mSamples(0)
    , mImageDataStatus(WebGLImageDataStatus::NoImageData)
    , mHasBeenBound(false)
{
    mContext->mRenderbuffers.insertBack(this);
}

void
WebGLRenderbuffer::Delete()
{
    mContext->MakeContextCurrent();

    mContext->gl->fDeleteRenderbuffers(1, &mPrimaryRB);
    if (mSecondaryRB)
        mContext->gl->fDeleteRenderbuffers(1, &mSecondaryRB);

    LinkedListElement<WebGLRenderbuffer>::removeFrom(mContext->mRenderbuffers);
}

int64_t
WebGLRenderbuffer::MemoryUsage() const
{
    // If there is no defined format, we're not taking up any memory
    if (!mFormat)
        return 0;

    const auto bytesPerPixel = mFormat->format->estimatedBytesPerPixel;
    const int64_t pixels = int64_t(mWidth) * int64_t(mHeight);

    const int64_t totalSize = pixels * bytesPerPixel;
    return totalSize;
}

static GLenum
DoRenderbufferStorageMaybeMultisample(gl::GLContext* gl, GLsizei samples,
                                      GLenum internalFormat, GLsizei width,
                                      GLsizei height)
{
    MOZ_ASSERT_IF(samples >= 1, gl->IsSupported(gl::GLFeature::framebuffer_multisample));

    // Certain OpenGL ES renderbuffer formats may not exist on desktop OpenGL.
    switch (internalFormat) {
    case LOCAL_GL_RGBA4:
    case LOCAL_GL_RGB5_A1:
        // 16-bit RGBA formats are not supported on desktop GL.
        if (!gl->IsGLES())
            internalFormat = LOCAL_GL_RGBA8;
        break;

    case LOCAL_GL_RGB565:
        // RGB565 is not supported on desktop GL.
        if (!gl->IsGLES())
            internalFormat = LOCAL_GL_RGB8;
        break;

    case LOCAL_GL_DEPTH_COMPONENT16:
        if (!gl->IsGLES() || gl->IsExtensionSupported(gl::GLContext::OES_depth24))
            internalFormat = LOCAL_GL_DEPTH_COMPONENT24;
        else if (gl->IsSupported(gl::GLFeature::packed_depth_stencil))
            internalFormat = LOCAL_GL_DEPTH24_STENCIL8;
        break;

    case LOCAL_GL_DEPTH_STENCIL:
        MOZ_CRASH("GFX: GL_DEPTH_STENCIL is not valid here.");
        break;

    default:
        break;
    }

    gl::GLContext::LocalErrorScope errorScope(*gl);

    if (samples > 0) {
        gl->fRenderbufferStorageMultisample(LOCAL_GL_RENDERBUFFER, samples,
                                            internalFormat, width, height);
    } else {
        gl->fRenderbufferStorage(LOCAL_GL_RENDERBUFFER, internalFormat, width, height);
    }

    return errorScope.GetError();
}

GLenum
WebGLRenderbuffer::DoRenderbufferStorage(uint32_t samples,
                                         const webgl::FormatUsageInfo* format,
                                         uint32_t width, uint32_t height)
{
    MOZ_ASSERT(mContext->mBoundRenderbuffer == this);

    gl::GLContext* gl = mContext->gl;
    MOZ_ASSERT(samples <= 256); // Sanity check.

    GLenum primaryFormat = format->format->sizedFormat;
    GLenum secondaryFormat = 0;

    if (mEmulatePackedDepthStencil && primaryFormat == LOCAL_GL_DEPTH24_STENCIL8) {
        primaryFormat = DepthFormatForDepthStencilEmu(gl);
        secondaryFormat = LOCAL_GL_STENCIL_INDEX8;
    }

    gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mPrimaryRB);
    GLenum error = DoRenderbufferStorageMaybeMultisample(gl, samples, primaryFormat,
                                                         width, height);
    if (error)
        return error;

    if (secondaryFormat) {
        if (!mSecondaryRB) {
            gl->fGenRenderbuffers(1, &mSecondaryRB);
        }

        gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mSecondaryRB);
        error = DoRenderbufferStorageMaybeMultisample(gl, samples, secondaryFormat,
                                                      width, height);
        if (error)
            return error;
    } else if (mSecondaryRB) {
        gl->fDeleteRenderbuffers(1, &mSecondaryRB);
        mSecondaryRB = 0;
    }

    return 0;
}

void
WebGLRenderbuffer::RenderbufferStorage(const char* funcName, uint32_t samples,
                                       GLenum internalFormat, uint32_t width,
                                       uint32_t height)
{
    const auto usage = mContext->mFormatUsage->GetRBUsage(internalFormat);
    if (!usage) {
        mContext->ErrorInvalidEnum("%s: Invalid `internalFormat`: 0x%04x.", funcName,
                                   internalFormat);
        return;
    }

    if (width > mContext->mImplMaxRenderbufferSize ||
        height > mContext->mImplMaxRenderbufferSize)
    {
        mContext->ErrorInvalidValue("%s: Width or height exceeds maximum renderbuffer"
                                    " size.",
                                    funcName);
        return;
    }

    mContext->MakeContextCurrent();

    if (!usage->maxSamplesKnown) {
        const_cast<webgl::FormatUsageInfo*>(usage)->ResolveMaxSamples(mContext->gl);
    }
    MOZ_ASSERT(usage->maxSamplesKnown);

    if (samples > usage->maxSamples) {
        mContext->ErrorInvalidOperation("%s: `samples` is out of the valid range.", funcName);
        return;
    }

    // Validation complete.

    const GLenum error = DoRenderbufferStorage(samples, usage, width, height);
    if (error) {
        const char* errorName = mContext->ErrorName(error);
        mContext->GenerateWarning("%s generated error %s", funcName, errorName);
        if (error == LOCAL_GL_OUT_OF_MEMORY) {
          // Truncate.
          mSamples = 0;
          mFormat = nullptr;
          mWidth = 0;
          mHeight = 0;
          mImageDataStatus = WebGLImageDataStatus::NoImageData;

          InvalidateStatusOfAttachedFBs();
        }
        return;
    }

    mSamples = samples;
    mFormat = usage;
    mWidth = width;
    mHeight = height;
    mImageDataStatus = WebGLImageDataStatus::UninitializedImageData;

    InvalidateStatusOfAttachedFBs();
}

void
WebGLRenderbuffer::DoFramebufferRenderbuffer(FBTarget target, GLenum attachment) const
{
    gl::GLContext* gl = mContext->gl;

    if (attachment == LOCAL_GL_DEPTH_STENCIL_ATTACHMENT) {
        const GLuint stencilRB = (mSecondaryRB ? mSecondaryRB : mPrimaryRB);
        gl->fFramebufferRenderbuffer(target.get(), LOCAL_GL_DEPTH_ATTACHMENT,
                                     LOCAL_GL_RENDERBUFFER, mPrimaryRB);
        gl->fFramebufferRenderbuffer(target.get(), LOCAL_GL_STENCIL_ATTACHMENT,
                                     LOCAL_GL_RENDERBUFFER, stencilRB);
        return;
    }

    gl->fFramebufferRenderbuffer(target.get(), attachment,
                                 LOCAL_GL_RENDERBUFFER, mPrimaryRB);
}

GLint
WebGLRenderbuffer::GetRenderbufferParameter(RBTarget target,
                                            RBParam pname) const
{
    gl::GLContext* gl = mContext->gl;

    switch (pname.get()) {
    case LOCAL_GL_RENDERBUFFER_STENCIL_SIZE:
        if (!mFormat)
            return 0;

        if (!mFormat->format->s)
            return 0;

        return 8;

    case LOCAL_GL_RENDERBUFFER_SAMPLES:
    case LOCAL_GL_RENDERBUFFER_WIDTH:
    case LOCAL_GL_RENDERBUFFER_HEIGHT:
    case LOCAL_GL_RENDERBUFFER_RED_SIZE:
    case LOCAL_GL_RENDERBUFFER_GREEN_SIZE:
    case LOCAL_GL_RENDERBUFFER_BLUE_SIZE:
    case LOCAL_GL_RENDERBUFFER_ALPHA_SIZE:
    case LOCAL_GL_RENDERBUFFER_DEPTH_SIZE:
        {
            gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mPrimaryRB);
            GLint i = 0;
            gl->fGetRenderbufferParameteriv(target.get(), pname.get(), &i);
            return i;
        }

    case LOCAL_GL_RENDERBUFFER_INTERNAL_FORMAT:
        {
            GLenum ret = LOCAL_GL_RGBA4;
            if (mFormat) {
                ret = mFormat->format->sizedFormat;

                if (!mContext->IsWebGL2() && ret == LOCAL_GL_DEPTH24_STENCIL8) {
                    ret = LOCAL_GL_DEPTH_STENCIL;
                }
            }
            return ret;
        }
    }

    MOZ_ASSERT(false, "This function should only be called with valid `pname`.");
    return 0;
}

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WebGLRenderbuffer)

NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebGLRenderbuffer, AddRef)
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebGLRenderbuffer, Release)

} // namespace mozilla