summaryrefslogtreecommitdiffstats
path: root/dom/canvas/WebGLFormats.h
blob: 41d89e06315bb14daba4b83fc0761def7f536626 (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
340
341
342
343
344
345
346
347
348
349
/* -*- 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/. */

#ifndef WEBGL_FORMATS_H_
#define WEBGL_FORMATS_H_

#include <map>
#include <set>

#include "mozilla/UniquePtr.h"
#include "WebGLTypes.h"

namespace mozilla {
namespace webgl {

typedef uint8_t EffectiveFormatValueT;

enum class EffectiveFormat : EffectiveFormatValueT {
    // GLES 3.0.4, p128-129, "Required Texture Formats"
    // "Texture and renderbuffer color formats"
    RGBA32I,
    RGBA32UI,
    RGBA16I,
    RGBA16UI,
    RGBA8,
    RGBA8I,
    RGBA8UI,
    SRGB8_ALPHA8,
    RGB10_A2,
    RGB10_A2UI,
    RGBA4,
    RGB5_A1,

    RGB8,
    RGB565,

    RG32I,
    RG32UI,
    RG16I,
    RG16UI,
    RG8,
    RG8I,
    RG8UI,

    R32I,
    R32UI,
    R16I,
    R16UI,
    R8,
    R8I,
    R8UI,

    // "Texture-only color formats"
    RGBA32F,
    RGBA16F,
    RGBA8_SNORM,

    RGB32F,
    RGB32I,
    RGB32UI,

    RGB16F,
    RGB16I,
    RGB16UI,

    RGB8_SNORM,
    RGB8I,
    RGB8UI,
    SRGB8,

    R11F_G11F_B10F,
    RGB9_E5,

    RG32F,
    RG16F,
    RG8_SNORM,

    R32F,
    R16F,
    R8_SNORM,

    // "Depth formats"
    DEPTH_COMPONENT32F,
    DEPTH_COMPONENT24,
    DEPTH_COMPONENT16,

    // "Combined depth+stencil formats"
    DEPTH32F_STENCIL8,
    DEPTH24_STENCIL8,

    // GLES 3.0.4, p205-206, "Required Renderbuffer Formats"
    STENCIL_INDEX8,

    ////////////////////////////////////

    // GLES 3.0.4, p147, table 3.19
    // GLES 3.0.4, p286+, $C.1 "ETC Compressed Texture Image Formats"
    COMPRESSED_R11_EAC,
    COMPRESSED_SIGNED_R11_EAC,
    COMPRESSED_RG11_EAC,
    COMPRESSED_SIGNED_RG11_EAC,
    COMPRESSED_RGB8_ETC2,
    COMPRESSED_SRGB8_ETC2,
    COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
    COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
    COMPRESSED_RGBA8_ETC2_EAC,
    COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,

    // AMD_compressed_ATC_texture
    ATC_RGB_AMD,
    ATC_RGBA_EXPLICIT_ALPHA_AMD,
    ATC_RGBA_INTERPOLATED_ALPHA_AMD,

    // EXT_texture_compression_s3tc
    COMPRESSED_RGB_S3TC_DXT1_EXT,
    COMPRESSED_RGBA_S3TC_DXT1_EXT,
    COMPRESSED_RGBA_S3TC_DXT3_EXT,
    COMPRESSED_RGBA_S3TC_DXT5_EXT,

    // IMG_texture_compression_pvrtc
    COMPRESSED_RGB_PVRTC_4BPPV1,
    COMPRESSED_RGBA_PVRTC_4BPPV1,
    COMPRESSED_RGB_PVRTC_2BPPV1,
    COMPRESSED_RGBA_PVRTC_2BPPV1,

    // OES_compressed_ETC1_RGB8_texture
    ETC1_RGB8_OES,

    ////////////////////////////////////

    // GLES 3.0.4, p128, table 3.12.
    Luminance8Alpha8,
    Luminance8,
    Alpha8,

    // OES_texture_float
    Luminance32FAlpha32F,
    Luminance32F,
    Alpha32F,

    // OES_texture_half_float
    Luminance16FAlpha16F,
    Luminance16F,
    Alpha16F,

    MAX,
};

enum class UnsizedFormat : uint8_t {
    R,
    RG,
    RGB,
    RGBA,
    LA,
    L,
    A,
    D,
    S,
    DEPTH_STENCIL, // `DS` is a macro on Solaris. (regset.h)
};

// GLES 3.0.4 p114 Table 3.4, p240
enum class ComponentType : uint8_t {
    None,
    Int,          // RGBA32I
    UInt,         // RGBA32UI, STENCIL_INDEX8
    NormInt,      // RGBA8_SNORM
    NormUInt,     // RGBA8, DEPTH_COMPONENT16
    Float,        // RGBA32F
    Special,      // DEPTH24_STENCIL8
};

enum class CompressionFamily : uint8_t {
    ETC1,
    ES3, // ETC2 or EAC
    ATC,
    S3TC,
    PVRTC,
};

////////////////////////////////////////////////////////////////////////////////

struct CompressedFormatInfo
{
    const EffectiveFormat effectiveFormat;
    const uint8_t bytesPerBlock;
    const uint8_t blockWidth;
    const uint8_t blockHeight;
    const CompressionFamily family;
};

struct FormatInfo
{
    const EffectiveFormat effectiveFormat;
    const char* const name;
    const GLenum sizedFormat;
    const UnsizedFormat unsizedFormat;
    const ComponentType componentType;
    const bool isSRGB;

    const CompressedFormatInfo* const compression;

    const uint8_t estimatedBytesPerPixel; // 0 iff bool(compression).

    // In bits. Iff bool(compression), active channels are 1.
    const uint8_t r;
    const uint8_t g;
    const uint8_t b;
    const uint8_t a;
    const uint8_t d;
    const uint8_t s;

    //////

    std::map<UnsizedFormat, const FormatInfo*> copyDecayFormats;

    const FormatInfo* GetCopyDecayFormat(UnsizedFormat) const;

    bool IsColorFormat() const {
         // Alpha is a 'color format' since it's 'color-attachable'.
        return bool(compression) ||
               bool(r | g | b | a);
    }
};

struct PackingInfo
{
    GLenum format;
    GLenum type;

    bool operator <(const PackingInfo& x) const
    {
        if (format != x.format)
            return format < x.format;

        return type < x.type;
    }

    bool operator ==(const PackingInfo& x) const {
        return (format == x.format &&
                type == x.type);
    }
};

struct DriverUnpackInfo
{
    GLenum internalFormat;
    GLenum unpackFormat;
    GLenum unpackType;

    PackingInfo ToPacking() const {
        return {unpackFormat, unpackType};
    }
};

//////////////////////////////////////////////////////////////////////////////////////////

const FormatInfo* GetFormat(EffectiveFormat format);
uint8_t BytesPerPixel(const PackingInfo& packing);
bool GetBytesPerPixel(const PackingInfo& packing, uint8_t* const out_bytes);
/*
GLint ComponentSize(const FormatInfo* format, GLenum component);
GLenum ComponentType(const FormatInfo* format);
*/
////////////////////////////////////////

struct FormatUsageInfo
{
    const FormatInfo* const format;
private:
    bool isRenderable;
public:
    bool isFilterable;

    std::map<PackingInfo, DriverUnpackInfo> validUnpacks;
    const DriverUnpackInfo* idealUnpack;

    const GLint* textureSwizzleRGBA;

    bool maxSamplesKnown;
    uint32_t maxSamples;

    static const GLint kLuminanceSwizzleRGBA[4];
    static const GLint kAlphaSwizzleRGBA[4];
    static const GLint kLumAlphaSwizzleRGBA[4];

    explicit FormatUsageInfo(const FormatInfo* _format)
        : format(_format)
        , isRenderable(false)
        , isFilterable(false)
        , idealUnpack(nullptr)
        , textureSwizzleRGBA(nullptr)
        , maxSamplesKnown(false)
        , maxSamples(0)
    { }

    bool IsRenderable() const { return isRenderable; }
    void SetRenderable();

    bool IsUnpackValid(const PackingInfo& key,
                       const DriverUnpackInfo** const out_value) const;

    void ResolveMaxSamples(gl::GLContext* gl);
};

class FormatUsageAuthority
{
    std::map<EffectiveFormat, FormatUsageInfo> mUsageMap;

    std::map<GLenum, const FormatUsageInfo*> mRBFormatMap;
    std::map<GLenum, const FormatUsageInfo*> mSizedTexFormatMap;
    std::map<PackingInfo, const FormatUsageInfo*> mUnsizedTexFormatMap;

    std::set<GLenum> mValidTexInternalFormats;
    std::set<GLenum> mValidTexUnpackFormats;
    std::set<GLenum> mValidTexUnpackTypes;

public:
    static UniquePtr<FormatUsageAuthority> CreateForWebGL1(gl::GLContext* gl);
    static UniquePtr<FormatUsageAuthority> CreateForWebGL2(gl::GLContext* gl);

private:
    FormatUsageAuthority() { }

public:
    FormatUsageInfo* EditUsage(EffectiveFormat format);
    const FormatUsageInfo* GetUsage(EffectiveFormat format) const;

    void AddTexUnpack(FormatUsageInfo* usage, const PackingInfo& pi,
                      const DriverUnpackInfo& dui);

    bool IsInternalFormatEnumValid(GLenum internalFormat) const;
    bool AreUnpackEnumsValid(GLenum unpackFormat, GLenum unpackType) const;

    void AllowRBFormat(GLenum sizedFormat, const FormatUsageInfo* usage);
    void AllowSizedTexFormat(GLenum sizedFormat, const FormatUsageInfo* usage);
    void AllowUnsizedTexFormat(const PackingInfo& pi, const FormatUsageInfo* usage);

    const FormatUsageInfo* GetRBUsage(GLenum sizedFormat) const;
    const FormatUsageInfo* GetSizedTexUsage(GLenum sizedFormat) const;
    const FormatUsageInfo* GetUnsizedTexUsage(const PackingInfo& pi) const;
};

} // namespace webgl
} // namespace mozilla

#endif // WEBGL_FORMATS_H_