summaryrefslogtreecommitdiffstats
path: root/dom/canvas/WebGLRenderbuffer.cpp
blob: 7f6d8e791403356adb46ccc895af36b18e126bb6 (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
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "WebGLFormats.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;
}

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)
    : WebGLContextBoundObject(webgl),
      mPrimaryRB(DoCreateRenderbuffer(webgl->gl)),
      mEmulatePackedDepthStencil(EmulatePackedDepthStencil(webgl->gl)),
      mSecondaryRB(0) {
  // Bind our RB, or we might end up calling FramebufferRenderbuffer before we
  // ever call BindRenderbuffer, since webgl.bindRenderbuffer doesn't actually
  // call glBindRenderbuffer anymore.
  mContext->gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, mPrimaryRB);
  mContext->gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, 0);
}

WebGLRenderbuffer::~WebGLRenderbuffer() {
  mImageInfo = webgl::ImageInfo();

  if (!mContext) return;

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

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) {
  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);
  gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, 0);
  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);
    gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, 0);
    if (error) return error;
  } else if (mSecondaryRB) {
    gl->fDeleteRenderbuffers(1, &mSecondaryRB);
    mSecondaryRB = 0;
  }

  return 0;
}

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

  if (width > mContext->mGLMaxRenderbufferSize ||
      height > mContext->mGLMaxRenderbufferSize) {
    mContext->ErrorInvalidValue(
        "Width or height exceeds maximum renderbuffer"
        " size.");
    return;
  }

  const auto maxSamples = usage->MaxSamples(*mContext->gl);
  if (samples > maxSamples) {
    mContext->ErrorInvalidOperation("`samples` is out of the valid range.");
    return;
  }

  // Validation complete.

  const GLenum error = DoRenderbufferStorage(samples, usage, width, height);
  if (error) {
    mContext->GenerateWarning("Unexpected error %s", EnumString(error).c_str());
    if (error == LOCAL_GL_OUT_OF_MEMORY) {
      // Truncate.
      mImageInfo = {};
      InvalidateCaches();
    }
    return;
  }

  mContext->OnDataAllocCall();

  const uint32_t depth = 1;
  auto uninitializedSlices = Some(std::vector<bool>(depth, true));
  mImageInfo = {
      usage,           width, height, depth, std::move(uninitializedSlices),
      uint8_t(samples)};
  InvalidateCaches();
}

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

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

  gl->fFramebufferRenderbuffer(LOCAL_GL_FRAMEBUFFER, attachment,
                               LOCAL_GL_RENDERBUFFER, mPrimaryRB);
}

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

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

      if (!mImageInfo.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(LOCAL_GL_RENDERBUFFER, pname.get(), &i);
      gl->fBindRenderbuffer(LOCAL_GL_RENDERBUFFER, 0);
      return i;
    }

    case LOCAL_GL_RENDERBUFFER_INTERNAL_FORMAT: {
      GLenum ret = LOCAL_GL_RGBA4;
      if (mImageInfo.mFormat) {
        ret = mImageInfo.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;
}

}  // namespace mozilla