summaryrefslogtreecommitdiffstats
path: root/dom/canvas/WebGLContextFramebufferOperations.cpp
blob: 04cabd1db8fc6a1445e3dc67686c11d090084b00 (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
/* -*- Mode: C++; tab-width: 4; 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 "WebGLContext.h"

#include "GLContext.h"
#include "GLScreenBuffer.h"
#include "WebGLFormats.h"
#include "WebGLFramebuffer.h"
#include "WebGLRenderbuffer.h"
#include "WebGLTexture.h"

namespace mozilla {

void WebGLContext::Clear(GLbitfield mask) {
  const FuncScope funcScope(*this, "clear");
  if (IsContextLost()) return;

  uint32_t m = mask & (LOCAL_GL_COLOR_BUFFER_BIT | LOCAL_GL_DEPTH_BUFFER_BIT |
                       LOCAL_GL_STENCIL_BUFFER_BIT);
  if (mask != m) return ErrorInvalidValue("Invalid mask bits.");

  if (mask == 0) {
    GenerateWarning("Calling gl.clear(0) has no effect.");
  } else if (mRasterizerDiscardEnabled) {
    GenerateWarning(
        "Calling gl.clear() with RASTERIZER_DISCARD enabled has no effects.");
  }

  if (mask & LOCAL_GL_COLOR_BUFFER_BIT && mBoundDrawFramebuffer) {
    for (const auto& cur : mBoundDrawFramebuffer->ColorDrawBuffers()) {
      const auto imageInfo = cur->GetImageInfo();
      if (!imageInfo || !imageInfo->mFormat) continue;

      if (imageInfo->mFormat->format->baseType !=
          webgl::TextureBaseType::Float) {
        ErrorInvalidOperation(
            "Color draw buffers must be floating-point"
            " or fixed-point. (normalized (u)ints)");
        return;
      }
    }
  }

  if (!BindCurFBForDraw()) return;

  auto driverMask = mask;
  if (!mBoundDrawFramebuffer) {
    if (mNeedsFakeNoDepth) {
      driverMask &= ~LOCAL_GL_DEPTH_BUFFER_BIT;
    }
    if (mNeedsFakeNoStencil) {
      driverMask &= ~LOCAL_GL_STENCIL_BUFFER_BIT;
    }
  }

  const ScopedDrawCallWrapper wrapper(*this);
  gl->fClear(driverMask);
}

static GLfloat GLClampFloat(GLfloat val) {
  if (val < 0.0) return 0.0;

  if (val > 1.0) return 1.0;

  return val;
}

void WebGLContext::ClearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a) {
  const FuncScope funcScope(*this, "clearColor");
  if (IsContextLost()) return;

  if (IsExtensionEnabled(WebGLExtensionID::EXT_color_buffer_float)) {
    MOZ_ASSERT(IsExtensionExplicit(WebGLExtensionID::EXT_color_buffer_float));

  } else if (IsExtensionEnabled(
                 WebGLExtensionID::EXT_color_buffer_half_float) ||
             IsExtensionEnabled(WebGLExtensionID::WEBGL_color_buffer_float)) {
    const bool explict =
        (IsExtensionExplicit(WebGLExtensionID::EXT_color_buffer_half_float) ||
         IsExtensionExplicit(WebGLExtensionID::WEBGL_color_buffer_float));
    const bool wouldHaveClamped =
        (r != GLClampFloat(r) || g != GLClampFloat(g) || b != GLClampFloat(b) ||
         a != GLClampFloat(a));
    if (!explict && wouldHaveClamped) {
      if (IsExtensionEnabled(WebGLExtensionID::EXT_color_buffer_half_float)) {
        WarnIfImplicit(WebGLExtensionID::EXT_color_buffer_half_float);
      } else if (IsExtensionEnabled(
                     WebGLExtensionID::WEBGL_color_buffer_float)) {
        WarnIfImplicit(WebGLExtensionID::WEBGL_color_buffer_float);
      }
    }
  } else {
    r = GLClampFloat(r);
    g = GLClampFloat(g);
    b = GLClampFloat(b);
    a = GLClampFloat(a);
  }

  gl->fClearColor(r, g, b, a);

  mColorClearValue[0] = r;
  mColorClearValue[1] = g;
  mColorClearValue[2] = b;
  mColorClearValue[3] = a;
}

void WebGLContext::ClearDepth(GLclampf v) {
  const FuncScope funcScope(*this, "clearDepth");
  if (IsContextLost()) return;

  mDepthClearValue = GLClampFloat(v);
  gl->fClearDepth(mDepthClearValue);
}

void WebGLContext::ClearStencil(GLint v) {
  const FuncScope funcScope(*this, "clearStencil");
  if (IsContextLost()) return;

  mStencilClearValue = v;
  gl->fClearStencil(v);
}

void WebGLContext::ColorMask(const Maybe<GLuint> i, const uint8_t mask) {
  const FuncScope funcScope(*this, "colorMask");
  if (IsContextLost()) return;

  if (i) {
    MOZ_RELEASE_ASSERT(
        IsExtensionEnabled(WebGLExtensionID::OES_draw_buffers_indexed));
    const auto limit = MaxValidDrawBuffers();
    if (*i >= limit) {
      ErrorInvalidValue("`index` (%u) must be < %s (%u)", *i,
                        "MAX_DRAW_BUFFERS", limit);
      return;
    }
    if (*i == 0) {
      mColorWriteMask0 = mask;
    }
    mColorWriteMaskNonzero[*i] = bool(mask);
  } else {
    mColorWriteMask0 = mask;
    if (mask) {
      mColorWriteMaskNonzero.set();
    } else {
      mColorWriteMaskNonzero.reset();
    }
  }

  DoColorMask(i, mask);
}

void WebGLContext::DepthMask(WebGLboolean b) {
  const FuncScope funcScope(*this, "depthMask");
  if (IsContextLost()) return;

  mDepthWriteMask = b;
  gl->fDepthMask(b);
}

void WebGLContext::DrawBuffers(const std::vector<GLenum>& buffers) {
  const FuncScope funcScope(*this, "drawBuffers");
  if (IsContextLost()) return;

  if (mBoundDrawFramebuffer) {
    mBoundDrawFramebuffer->DrawBuffers(buffers);
    return;
  }

  // GLES 3.0.4 p186:
  // "If the GL is bound to the default framebuffer, then `n` must be 1 and the
  //  constant must be BACK or NONE. [...] If DrawBuffers is supplied with a
  //  constant other than BACK and NONE, or with a value of `n` other than 1,
  //  the error INVALID_OPERATION is generated."
  if (buffers.size() != 1) {
    ErrorInvalidOperation(
        "For the default framebuffer, `buffers` must have a"
        " length of 1.");
    return;
  }

  switch (buffers[0]) {
    case LOCAL_GL_NONE:
    case LOCAL_GL_BACK:
      break;

    default:
      ErrorInvalidOperation(
          "For the default framebuffer, `buffers[0]` must be"
          " BACK or NONE.");
      return;
  }

  mDefaultFB_DrawBuffer0 = buffers[0];
  // Don't actually set it.
}

void WebGLContext::StencilMaskSeparate(GLenum face, GLuint mask) {
  const FuncScope funcScope(*this, "stencilMaskSeparate");
  if (IsContextLost()) return;

  if (!ValidateFaceEnum(face)) return;

  switch (face) {
    case LOCAL_GL_FRONT_AND_BACK:
      mStencilWriteMaskFront = mask;
      mStencilWriteMaskBack = mask;
      break;
    case LOCAL_GL_FRONT:
      mStencilWriteMaskFront = mask;
      break;
    case LOCAL_GL_BACK:
      mStencilWriteMaskBack = mask;
      break;
  }

  gl->fStencilMaskSeparate(face, mask);
}

}  // namespace mozilla