summaryrefslogtreecommitdiff
path: root/dom/canvas/WebGL2ContextFramebuffers.cpp
blob: c22bb919063ac3e36851e15aecb69321675da900 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* -*- 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"

#include "GLContext.h"
#include "GLScreenBuffer.h"
#include "WebGLContextUtils.h"
#include "WebGLFormats.h"
#include "WebGLFramebuffer.h"

namespace mozilla {

void
WebGL2Context::BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
                               GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
                               GLbitfield mask, GLenum filter)
{
    if (IsContextLost())
        return;

    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;
    }

    ////

    const auto& readFB = mBoundReadFramebuffer;
    if (readFB &&
        !readFB->ValidateAndInitAttachments("blitFramebuffer's READ_FRAMEBUFFER"))
    {
        return;
    }

    const auto& drawFB = mBoundDrawFramebuffer;
    if (drawFB &&
        !drawFB->ValidateAndInitAttachments("blitFramebuffer's DRAW_FRAMEBUFFER"))
    {
        return;
    }

    ////

    if (!mBoundReadFramebuffer) {
        ClearBackbufferIfNeeded();
    }

    WebGLFramebuffer::BlitFramebuffer(this,
                                      readFB, srcX0, srcY0, srcX1, srcY1,
                                      drawFB, dstX0, dstY0, dstX1, dstY1,
                                      mask, filter);
}

void
WebGL2Context::FramebufferTextureLayer(GLenum target, GLenum attachment,
                                       WebGLTexture* texture, GLint level, GLint layer)
{
    const char funcName[] = "framebufferTextureLayer";
    if (IsContextLost())
        return;

    if (!ValidateFramebufferTarget(target, funcName))
        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:
        MOZ_CRASH("GFX: Bad target.");
    }

    if (!fb)
        return ErrorInvalidOperation("%s: Cannot modify framebuffer 0.", funcName);

    fb->FramebufferTextureLayer(funcName, attachment, texture, level, layer);
}

JS::Value
WebGL2Context::GetFramebufferAttachmentParameter(JSContext* cx,
                                                 GLenum target,
                                                 GLenum attachment,
                                                 GLenum pname,
                                                 ErrorResult& out_error)
{
    return WebGLContext::GetFramebufferAttachmentParameter(cx, target, attachment, pname,
                                                           out_error);
}

////

static bool
ValidateBackbufferAttachmentEnum(WebGLContext* webgl, const char* funcName,
                                 GLenum attachment)
{
    switch (attachment) {
    case LOCAL_GL_COLOR:
    case LOCAL_GL_DEPTH:
    case LOCAL_GL_STENCIL:
        return true;

    default:
        webgl->ErrorInvalidEnum("%s: attachment: invalid enum value 0x%x.",
                                funcName, attachment);
        return false;
    }
}

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;
    }

    if (attachment < LOCAL_GL_COLOR_ATTACHMENT0) {
        webgl->ErrorInvalidEnum("%s: attachment: invalid enum value 0x%x.",
                                funcName, attachment);
        return false;
    }

    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;
}

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)
{
    if (IsContextLost())
        return false;

    gl->MakeCurrent();

    if (!ValidateFramebufferTarget(target, funcName))
        return false;

    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:
        MOZ_CRASH("GFX: Bad target.");
    }

    *out_glNumAttachments = attachments.Length();
    *out_glAttachments = attachments.Elements();

    if (fb) {
        for (const auto& attachment : attachments) {
            if (!ValidateFramebufferAttachmentEnum(this, funcName, attachment))
                return false;
        }
    } else {
        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();
        }
    }

    ////

    if (!fb) {
        ClearBackbufferIfNeeded();

        // Don't do more validation after these.
        Invalidate();
        mShouldPresent = true;
    }

    return true;
}

void
WebGL2Context::InvalidateFramebuffer(GLenum target,
                                     const dom::Sequence<GLenum>& attachments,
                                     ErrorResult& rv)
{
    const char funcName[] = "invalidateSubFramebuffer";

    std::vector<GLenum> scopedVector;
    GLsizei glNumAttachments;
    const GLenum* glAttachments;
    if (!ValidateInvalidateFramebuffer(funcName, target, attachments, &rv, &scopedVector,
                                       &glNumAttachments, &glAttachments))
    {
        return;
    }

    ////

    // 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);
        return;
    }

    // Use clear instead?
    // No-op for now.
}

void
WebGL2Context::InvalidateSubFramebuffer(GLenum target, const dom::Sequence<GLenum>& attachments,
                                        GLint x, GLint y, GLsizei width, GLsizei height,
                                        ErrorResult& rv)
{
    const char funcName[] = "invalidateSubFramebuffer";

    if (!ValidateNonNegative(funcName, "width", width) ||
        !ValidateNonNegative(funcName, "height", height))
    {
        return;
    }

    std::vector<GLenum> scopedVector;
    GLsizei glNumAttachments;
    const GLenum* glAttachments;
    if (!ValidateInvalidateFramebuffer(funcName, target, attachments, &rv, &scopedVector,
                                       &glNumAttachments, &glAttachments))
    {
        return;
    }

    ////

    // 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);
        return;
    }

    // Use clear instead?
    // No-op for now.
}

void
WebGL2Context::ReadBuffer(GLenum mode)
{
    const char funcName[] = "readBuffer";
    if (IsContextLost())
        return;

    if (mBoundReadFramebuffer) {
        mBoundReadFramebuffer->ReadBuffer(funcName, mode);
        return;
    }

    // Operating on the default framebuffer.
    if (mode != LOCAL_GL_NONE &&
        mode != LOCAL_GL_BACK)
    {
        nsCString enumName;
        EnumName(mode, &enumName);
        ErrorInvalidOperation("%s: If READ_FRAMEBUFFER is null, `mode` must be BACK or"
                              " NONE. Was %s.",
                              funcName, enumName.BeginReading());
        return;
    }

    gl->Screen()->SetReadBuffer(mode);
}

} // namespace mozilla