summaryrefslogtreecommitdiffstats
path: root/dom/canvas/WebGL2ContextMRTs.cpp
blob: 544f8e024a2dc6074f060f399c6d2fecc7b93e71 (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
/* -*- 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 "WebGL2Context.h"

#include "GLContext.h"
#include "WebGLFramebuffer.h"

namespace mozilla {

bool WebGL2Context::ValidateClearBuffer(const GLenum buffer,
                                        const GLint drawBuffer,
                                        const webgl::AttribBaseType funcType) {
  GLint maxDrawBuffer;
  switch (buffer) {
    case LOCAL_GL_COLOR:
      maxDrawBuffer = Limits().maxColorDrawBuffers - 1;
      break;

    case LOCAL_GL_DEPTH:
    case LOCAL_GL_STENCIL:
      maxDrawBuffer = 0;
      break;

    case LOCAL_GL_DEPTH_STENCIL:
      maxDrawBuffer = 0;
      break;

    default:
      ErrorInvalidEnumInfo("buffer", buffer);
      return false;
  }

  if (drawBuffer < 0 || drawBuffer > maxDrawBuffer) {
    ErrorInvalidValue(
        "Invalid drawbuffer %d. This buffer only supports"
        " `drawbuffer` values between 0 and %u.",
        drawBuffer, maxDrawBuffer);
    return false;
  }

  ////

  if (!BindCurFBForDraw()) return false;

  const auto& fb = mBoundDrawFramebuffer;
  if (fb) {
    if (!fb->ValidateClearBufferType(buffer, drawBuffer, funcType))
      return false;
  } else if (buffer == LOCAL_GL_COLOR) {
    if (drawBuffer != 0) return true;

    if (mDefaultFB_DrawBuffer0 == LOCAL_GL_NONE) return true;

    if (funcType != webgl::AttribBaseType::Float) {
      ErrorInvalidOperation(
          "For default framebuffer, COLOR is always of type"
          " FLOAT.");
      return false;
    }
  }

  return true;
}

////

void WebGL2Context::ClearBufferTv(GLenum buffer, GLint drawBuffer,
                                  const webgl::TypedQuad& data) {
  const FuncScope funcScope(*this, "clearBufferu?[fi]v");
  if (IsContextLost()) return;

  switch (data.type) {
    case webgl::AttribBaseType::Boolean:
      MOZ_ASSERT(false);
      return;

    case webgl::AttribBaseType::Float:
      if (buffer != LOCAL_GL_COLOR && buffer != LOCAL_GL_DEPTH) {
        ErrorInvalidEnum("`buffer` must be COLOR or DEPTH.");
        return;
      }
      break;

    case webgl::AttribBaseType::Int:
      if (buffer != LOCAL_GL_COLOR && buffer != LOCAL_GL_STENCIL) {
        ErrorInvalidEnum("`buffer` must be COLOR or STENCIL.");
        return;
      }
      break;

    case webgl::AttribBaseType::Uint:
      if (buffer != LOCAL_GL_COLOR) {
        ErrorInvalidEnum("`buffer` must be COLOR.");
        return;
      }
      break;
  }

  if (!ValidateClearBuffer(buffer, drawBuffer, data.type)) {
    return;
  }

  if (!mBoundDrawFramebuffer && buffer == LOCAL_GL_DEPTH && mNeedsFakeNoDepth) {
    return;
  }
  if (!mBoundDrawFramebuffer && buffer == LOCAL_GL_STENCIL &&
      mNeedsFakeNoStencil) {
    return;
  }

  ScopedDrawCallWrapper wrapper(*this);
  switch (data.type) {
    case webgl::AttribBaseType::Boolean:
      MOZ_ASSERT(false);
      return;

    case webgl::AttribBaseType::Float:
      gl->fClearBufferfv(buffer, drawBuffer,
                         reinterpret_cast<const float*>(data.data.data()));
      break;

    case webgl::AttribBaseType::Int:
      gl->fClearBufferiv(buffer, drawBuffer,
                         reinterpret_cast<const int32_t*>(data.data.data()));
      break;

    case webgl::AttribBaseType::Uint:
      gl->fClearBufferuiv(buffer, drawBuffer,
                          reinterpret_cast<const uint32_t*>(data.data.data()));
      break;
  }
}

////

void WebGL2Context::ClearBufferfi(GLenum buffer, GLint drawBuffer,
                                  GLfloat depth, GLint stencil) {
  const FuncScope funcScope(*this, "clearBufferfi");
  if (IsContextLost()) return;

  if (buffer != LOCAL_GL_DEPTH_STENCIL)
    return ErrorInvalidEnum("`buffer` must be DEPTH_STENCIL.");

  const auto ignored = webgl::AttribBaseType::Float;
  if (!ValidateClearBuffer(buffer, drawBuffer, ignored)) return;

  auto driverDepth = depth;
  auto driverStencil = stencil;
  if (!mBoundDrawFramebuffer) {
    if (mNeedsFakeNoDepth) {
      driverDepth = 1.0f;
    } else if (mNeedsFakeNoStencil) {
      driverStencil = 0;
    }
  }

  ScopedDrawCallWrapper wrapper(*this);
  gl->fClearBufferfi(buffer, drawBuffer, driverDepth, driverStencil);
}

}  // namespace mozilla