Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef WEBGLTYPES_H_
7
#define WEBGLTYPES_H_
8
9
#include <limits>
10
11
// Most WebIDL typedefs are identical to their OpenGL counterparts.
12
#include "GLTypes.h"
13
#include "mozilla/Casting.h"
14
15
// Manual reflection of WebIDL typedefs that are different from their
16
// OpenGL counterparts.
17
typedef int64_t WebGLsizeiptr;
18
typedef int64_t WebGLintptr;
19
typedef bool WebGLboolean;
20
21
// -
22
23
namespace mozilla {
24
namespace gl {
25
class GLContext; // This is going to be needed a lot.
26
} // namespace gl
27
28
// -
29
// Prevent implicit conversions into calloc and malloc. (mozilla namespace
30
// only!)
31
32
template <typename DestT>
33
class ForbidNarrowing final {
34
DestT mVal;
35
36
public:
37
template <typename SrcT>
38
MOZ_IMPLICIT ForbidNarrowing(SrcT val) : mVal(val) {
39
static_assert(
40
std::numeric_limits<SrcT>::min() >= std::numeric_limits<DestT>::min(),
41
"SrcT must be narrower than DestT.");
42
static_assert(
43
std::numeric_limits<SrcT>::max() <= std::numeric_limits<DestT>::max(),
44
"SrcT must be narrower than DestT.");
45
}
46
47
explicit operator DestT() const { return mVal; }
48
};
49
50
inline void* malloc(const ForbidNarrowing<size_t> s) {
51
return ::malloc(size_t(s));
52
}
53
54
inline void* calloc(const ForbidNarrowing<size_t> n,
55
const ForbidNarrowing<size_t> size) {
56
return ::calloc(size_t(n), size_t(size));
57
}
58
59
// -
60
61
namespace detail {
62
63
template <typename From>
64
class AutoAssertCastT final {
65
const From mVal;
66
67
public:
68
explicit AutoAssertCastT(const From val) : mVal(val) {}
69
70
template <typename To>
71
operator To() const {
72
return AssertedCast<To>(mVal);
73
}
74
};
75
76
} // namespace detail
77
78
template <typename From>
79
inline auto AutoAssertCast(const From val) {
80
return detail::AutoAssertCastT<From>(val);
81
}
82
83
/*
84
* Implementing WebGL (or OpenGL ES 2.0) on top of desktop OpenGL requires
85
* emulating the vertex attrib 0 array when it's not enabled. Indeed,
86
* OpenGL ES 2.0 allows drawing without vertex attrib 0 array enabled, but
87
* desktop OpenGL does not allow that.
88
*/
89
enum class WebGLVertexAttrib0Status : uint8_t {
90
Default, // default status - no emulation needed
91
EmulatedUninitializedArray, // need an artificial attrib 0 array, but
92
// contents may be left uninitialized
93
EmulatedInitializedArray // need an artificial attrib 0 array, and contents
94
// must be initialized
95
};
96
97
/*
98
* The formats that may participate, either as source or destination formats,
99
* in WebGL texture conversions. This includes:
100
* - all the formats accepted by WebGL.texImage2D, e.g. RGBA4444
101
* - additional formats provided by extensions, e.g. RGB32F
102
* - additional source formats, depending on browser details, used when
103
* uploading textures from DOM elements. See gfxImageSurface::Format().
104
*/
105
enum class WebGLTexelFormat : uint8_t {
106
// returned by SurfaceFromElementResultToImageSurface to indicate absence of
107
// image data
108
None,
109
// common value for formats for which format conversions are not supported
110
FormatNotSupportingAnyConversion,
111
// dummy pseudo-format meaning "use the other format".
112
// For example, if SrcFormat=Auto and DstFormat=RGB8, then the source
113
// is implicitly treated as being RGB8 itself.
114
Auto,
115
// 1-channel formats
116
A8,
117
A16F, // OES_texture_half_float
118
A32F, // OES_texture_float
119
R8,
120
R16F, // OES_texture_half_float
121
R32F, // OES_texture_float
122
// 2-channel formats
123
RA8,
124
RA16F, // OES_texture_half_float
125
RA32F, // OES_texture_float
126
RG8,
127
RG16F,
128
RG32F,
129
// 3-channel formats
130
RGB8,
131
RGB565,
132
RGB11F11F10F,
133
RGB16F, // OES_texture_half_float
134
RGB32F, // OES_texture_float
135
// 4-channel formats
136
RGBA8,
137
RGBA5551,
138
RGBA4444,
139
RGBA16F, // OES_texture_half_float
140
RGBA32F, // OES_texture_float
141
// DOM element source only formats.
142
RGBX8,
143
BGRX8,
144
BGRA8
145
};
146
147
enum class WebGLTexImageFunc : uint8_t {
148
TexImage,
149
TexSubImage,
150
CopyTexImage,
151
CopyTexSubImage,
152
CompTexImage,
153
CompTexSubImage,
154
};
155
156
enum class WebGLTexDimensions : uint8_t { Tex2D, Tex3D };
157
158
// Please keep extensions in alphabetic order.
159
enum class WebGLExtensionID : uint8_t {
160
ANGLE_instanced_arrays,
161
EXT_blend_minmax,
162
EXT_color_buffer_float,
163
EXT_color_buffer_half_float,
164
EXT_disjoint_timer_query,
165
EXT_float_blend,
166
EXT_frag_depth,
167
EXT_shader_texture_lod,
168
EXT_sRGB,
169
EXT_texture_compression_bptc,
170
EXT_texture_compression_rgtc,
171
EXT_texture_filter_anisotropic,
172
MOZ_debug,
173
OES_element_index_uint,
174
OES_fbo_render_mipmap,
175
OES_standard_derivatives,
176
OES_texture_float,
177
OES_texture_float_linear,
178
OES_texture_half_float,
179
OES_texture_half_float_linear,
180
OES_vertex_array_object,
181
OVR_multiview2,
182
WEBGL_color_buffer_float,
183
WEBGL_compressed_texture_astc,
184
WEBGL_compressed_texture_etc,
185
WEBGL_compressed_texture_etc1,
186
WEBGL_compressed_texture_pvrtc,
187
WEBGL_compressed_texture_s3tc,
188
WEBGL_compressed_texture_s3tc_srgb,
189
WEBGL_debug_renderer_info,
190
WEBGL_debug_shaders,
191
WEBGL_depth_texture,
192
WEBGL_draw_buffers,
193
WEBGL_explicit_present,
194
WEBGL_lose_context,
195
Max
196
};
197
198
class UniqueBuffer {
199
// Like UniquePtr<>, but for void* and malloc/calloc/free.
200
void* mBuffer;
201
202
public:
203
UniqueBuffer() : mBuffer(nullptr) {}
204
205
MOZ_IMPLICIT UniqueBuffer(void* buffer) : mBuffer(buffer) {}
206
207
~UniqueBuffer() { free(mBuffer); }
208
209
UniqueBuffer(UniqueBuffer&& other) {
210
this->mBuffer = other.mBuffer;
211
other.mBuffer = nullptr;
212
}
213
214
UniqueBuffer& operator=(UniqueBuffer&& other) {
215
free(this->mBuffer);
216
this->mBuffer = other.mBuffer;
217
other.mBuffer = nullptr;
218
return *this;
219
}
220
221
UniqueBuffer& operator=(void* newBuffer) {
222
free(this->mBuffer);
223
this->mBuffer = newBuffer;
224
return *this;
225
}
226
227
explicit operator bool() const { return bool(mBuffer); }
228
229
void* get() const { return mBuffer; }
230
231
UniqueBuffer(const UniqueBuffer& other) =
232
delete; // construct using std::move()!
233
void operator=(const UniqueBuffer& other) =
234
delete; // assign using std::move()!
235
};
236
237
namespace webgl {
238
struct FormatUsageInfo;
239
240
struct SampleableInfo final {
241
const char* incompleteReason = nullptr;
242
uint32_t levels = 0;
243
const webgl::FormatUsageInfo* usage = nullptr;
244
bool isDepthTexCompare = false;
245
246
bool IsComplete() const { return bool(levels); }
247
};
248
249
enum class AttribBaseType : uint8_t {
250
Int,
251
UInt,
252
Float, // Also includes NormU?Int
253
Boolean, // Can convert from anything.
254
};
255
const char* ToString(AttribBaseType);
256
257
} // namespace webgl
258
259
} // namespace mozilla
260
261
#endif