1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2016 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // Format:
7*8975f5c5SAndroid Build Coastguard Worker // A universal description of typed GPU storage. Across multiple
8*8975f5c5SAndroid Build Coastguard Worker // renderer back-ends, there are common formats and some distinct
9*8975f5c5SAndroid Build Coastguard Worker // permutations, this enum encapsulates them all. Formats apply to
10*8975f5c5SAndroid Build Coastguard Worker // textures, but could also apply to any typed data.
11*8975f5c5SAndroid Build Coastguard Worker
12*8975f5c5SAndroid Build Coastguard Worker #ifndef LIBANGLE_RENDERER_FORMAT_H_
13*8975f5c5SAndroid Build Coastguard Worker #define LIBANGLE_RENDERER_FORMAT_H_
14*8975f5c5SAndroid Build Coastguard Worker
15*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/cl_types.h"
16*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/FormatID_autogen.h"
17*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/renderer_utils.h"
18*8975f5c5SAndroid Build Coastguard Worker
19*8975f5c5SAndroid Build Coastguard Worker namespace angle
20*8975f5c5SAndroid Build Coastguard Worker {
21*8975f5c5SAndroid Build Coastguard Worker enum class FormatID;
22*8975f5c5SAndroid Build Coastguard Worker
23*8975f5c5SAndroid Build Coastguard Worker extern const Format gFormatInfoTable[];
24*8975f5c5SAndroid Build Coastguard Worker
25*8975f5c5SAndroid Build Coastguard Worker struct Format final : private angle::NonCopyable
26*8975f5c5SAndroid Build Coastguard Worker {
27*8975f5c5SAndroid Build Coastguard Worker inline constexpr Format(FormatID id,
28*8975f5c5SAndroid Build Coastguard Worker GLenum glFormat,
29*8975f5c5SAndroid Build Coastguard Worker GLenum fboFormat,
30*8975f5c5SAndroid Build Coastguard Worker rx::MipGenerationFunction mipGen,
31*8975f5c5SAndroid Build Coastguard Worker const rx::FastCopyFunctionMap &fastCopyFunctions,
32*8975f5c5SAndroid Build Coastguard Worker rx::PixelReadFunction colorRead,
33*8975f5c5SAndroid Build Coastguard Worker rx::PixelWriteFunction colorWrite,
34*8975f5c5SAndroid Build Coastguard Worker GLenum componentType,
35*8975f5c5SAndroid Build Coastguard Worker GLuint redBits,
36*8975f5c5SAndroid Build Coastguard Worker GLuint greenBits,
37*8975f5c5SAndroid Build Coastguard Worker GLuint blueBits,
38*8975f5c5SAndroid Build Coastguard Worker GLuint alphaBits,
39*8975f5c5SAndroid Build Coastguard Worker GLuint luminanceBits,
40*8975f5c5SAndroid Build Coastguard Worker GLuint depthBits,
41*8975f5c5SAndroid Build Coastguard Worker GLuint stencilBits,
42*8975f5c5SAndroid Build Coastguard Worker GLuint pixelBytes,
43*8975f5c5SAndroid Build Coastguard Worker GLuint componentAlignmentMask,
44*8975f5c5SAndroid Build Coastguard Worker bool isBlock,
45*8975f5c5SAndroid Build Coastguard Worker bool isFixed,
46*8975f5c5SAndroid Build Coastguard Worker bool isScaled,
47*8975f5c5SAndroid Build Coastguard Worker bool isSRGB,
48*8975f5c5SAndroid Build Coastguard Worker bool isYUV,
49*8975f5c5SAndroid Build Coastguard Worker gl::VertexAttribType vertexAttribType);
50*8975f5c5SAndroid Build Coastguard Worker
Getfinal51*8975f5c5SAndroid Build Coastguard Worker static const Format &Get(FormatID id) { return gFormatInfoTable[static_cast<int>(id)]; }
52*8975f5c5SAndroid Build Coastguard Worker
53*8975f5c5SAndroid Build Coastguard Worker static FormatID InternalFormatToID(GLenum internalFormat);
54*8975f5c5SAndroid Build Coastguard Worker
55*8975f5c5SAndroid Build Coastguard Worker #if defined(ANGLE_ENABLE_CL)
56*8975f5c5SAndroid Build Coastguard Worker static FormatID CLRFormatToID(cl_channel_type internalChannelType);
57*8975f5c5SAndroid Build Coastguard Worker static FormatID CLRGFormatToID(cl_channel_type internalChannelType);
58*8975f5c5SAndroid Build Coastguard Worker static FormatID CLRGBFormatToID(cl_channel_type internalChannelType);
59*8975f5c5SAndroid Build Coastguard Worker static FormatID CLRGBAFormatToID(cl_channel_type internalChannelType);
60*8975f5c5SAndroid Build Coastguard Worker static FormatID CLBGRAFormatToID(cl_channel_type internalChannelType);
61*8975f5c5SAndroid Build Coastguard Worker static FormatID CLsRGBAFormatToID(cl_channel_type internalChannelType);
62*8975f5c5SAndroid Build Coastguard Worker static FormatID CLDEPTHFormatToID(cl_channel_type internalChannelType);
63*8975f5c5SAndroid Build Coastguard Worker static FormatID CLDEPTHSTENCILFormatToID(cl_channel_type internalChannelType);
64*8975f5c5SAndroid Build Coastguard Worker #endif // ANGLE_ENABLE_CL
65*8975f5c5SAndroid Build Coastguard Worker
66*8975f5c5SAndroid Build Coastguard Worker constexpr bool hasDepthOrStencilBits() const;
67*8975f5c5SAndroid Build Coastguard Worker constexpr bool hasDepthAndStencilBits() const;
68*8975f5c5SAndroid Build Coastguard Worker constexpr bool isLUMA() const;
69*8975f5c5SAndroid Build Coastguard Worker constexpr bool isBGRA() const;
70*8975f5c5SAndroid Build Coastguard Worker
71*8975f5c5SAndroid Build Coastguard Worker constexpr bool isSint() const;
72*8975f5c5SAndroid Build Coastguard Worker constexpr bool isUint() const;
73*8975f5c5SAndroid Build Coastguard Worker constexpr bool isSnorm() const;
74*8975f5c5SAndroid Build Coastguard Worker constexpr bool isUnorm() const;
75*8975f5c5SAndroid Build Coastguard Worker constexpr bool isFloat() const;
76*8975f5c5SAndroid Build Coastguard Worker constexpr bool isVertexTypeHalfFloat() const;
77*8975f5c5SAndroid Build Coastguard Worker
isIntfinal78*8975f5c5SAndroid Build Coastguard Worker constexpr bool isInt() const { return isSint() || isUint(); }
isNormfinal79*8975f5c5SAndroid Build Coastguard Worker constexpr bool isNorm() const { return isSnorm() || isUnorm(); }
isPureIntfinal80*8975f5c5SAndroid Build Coastguard Worker constexpr bool isPureInt() const { return isInt() && !isScaled; }
81*8975f5c5SAndroid Build Coastguard Worker
82*8975f5c5SAndroid Build Coastguard Worker bool operator==(const Format &other) const { return this->id == other.id; }
83*8975f5c5SAndroid Build Coastguard Worker
84*8975f5c5SAndroid Build Coastguard Worker FormatID id;
85*8975f5c5SAndroid Build Coastguard Worker
86*8975f5c5SAndroid Build Coastguard Worker // The closest matching GL internal format for the storage this format uses. Note that this
87*8975f5c5SAndroid Build Coastguard Worker // may be a different internal format than the one this ANGLE format is used for.
88*8975f5c5SAndroid Build Coastguard Worker GLenum glInternalFormat;
89*8975f5c5SAndroid Build Coastguard Worker
90*8975f5c5SAndroid Build Coastguard Worker // The format we should report to the GL layer when querying implementation formats from a FBO.
91*8975f5c5SAndroid Build Coastguard Worker // This might not be the same as the glInternalFormat, since some DXGI formats don't have
92*8975f5c5SAndroid Build Coastguard Worker // matching GL format enums, like BGRA4, BGR5A1 and B5G6R6.
93*8975f5c5SAndroid Build Coastguard Worker GLenum fboImplementationInternalFormat;
94*8975f5c5SAndroid Build Coastguard Worker
95*8975f5c5SAndroid Build Coastguard Worker rx::MipGenerationFunction mipGenerationFunction;
96*8975f5c5SAndroid Build Coastguard Worker rx::PixelReadFunction pixelReadFunction;
97*8975f5c5SAndroid Build Coastguard Worker rx::PixelWriteFunction pixelWriteFunction;
98*8975f5c5SAndroid Build Coastguard Worker
99*8975f5c5SAndroid Build Coastguard Worker // A map from a gl::FormatType to a fast pixel copy function for this format.
100*8975f5c5SAndroid Build Coastguard Worker const rx::FastCopyFunctionMap &fastCopyFunctions;
101*8975f5c5SAndroid Build Coastguard Worker
102*8975f5c5SAndroid Build Coastguard Worker GLenum componentType;
103*8975f5c5SAndroid Build Coastguard Worker
104*8975f5c5SAndroid Build Coastguard Worker GLuint redBits;
105*8975f5c5SAndroid Build Coastguard Worker GLuint greenBits;
106*8975f5c5SAndroid Build Coastguard Worker GLuint blueBits;
107*8975f5c5SAndroid Build Coastguard Worker GLuint alphaBits;
108*8975f5c5SAndroid Build Coastguard Worker GLuint luminanceBits;
109*8975f5c5SAndroid Build Coastguard Worker GLuint depthBits;
110*8975f5c5SAndroid Build Coastguard Worker GLuint stencilBits;
111*8975f5c5SAndroid Build Coastguard Worker
112*8975f5c5SAndroid Build Coastguard Worker GLuint pixelBytes;
113*8975f5c5SAndroid Build Coastguard Worker
114*8975f5c5SAndroid Build Coastguard Worker // For 1-byte components, is 0x0. For 2-byte, is 0x1. For 4-byte, is 0x3. For all others,
115*8975f5c5SAndroid Build Coastguard Worker // MAX_UINT.
116*8975f5c5SAndroid Build Coastguard Worker GLuint componentAlignmentMask;
117*8975f5c5SAndroid Build Coastguard Worker
118*8975f5c5SAndroid Build Coastguard Worker GLuint channelCount;
119*8975f5c5SAndroid Build Coastguard Worker
120*8975f5c5SAndroid Build Coastguard Worker bool isBlock;
121*8975f5c5SAndroid Build Coastguard Worker bool isFixed;
122*8975f5c5SAndroid Build Coastguard Worker bool isScaled;
123*8975f5c5SAndroid Build Coastguard Worker bool isSRGB;
124*8975f5c5SAndroid Build Coastguard Worker bool isYUV;
125*8975f5c5SAndroid Build Coastguard Worker
126*8975f5c5SAndroid Build Coastguard Worker // For vertex formats only. Returns the "type" value for glVertexAttribPointer etc.
127*8975f5c5SAndroid Build Coastguard Worker gl::VertexAttribType vertexAttribType;
128*8975f5c5SAndroid Build Coastguard Worker };
129*8975f5c5SAndroid Build Coastguard Worker
GetChannelCount(GLuint redBits,GLuint greenBits,GLuint blueBits,GLuint alphaBits,GLuint luminanceBits,GLuint depthBits,GLuint stencilBits)130*8975f5c5SAndroid Build Coastguard Worker constexpr GLuint GetChannelCount(GLuint redBits,
131*8975f5c5SAndroid Build Coastguard Worker GLuint greenBits,
132*8975f5c5SAndroid Build Coastguard Worker GLuint blueBits,
133*8975f5c5SAndroid Build Coastguard Worker GLuint alphaBits,
134*8975f5c5SAndroid Build Coastguard Worker GLuint luminanceBits,
135*8975f5c5SAndroid Build Coastguard Worker GLuint depthBits,
136*8975f5c5SAndroid Build Coastguard Worker GLuint stencilBits)
137*8975f5c5SAndroid Build Coastguard Worker {
138*8975f5c5SAndroid Build Coastguard Worker return (redBits > 0 ? 1 : 0) + (greenBits > 0 ? 1 : 0) + (blueBits > 0 ? 1 : 0) +
139*8975f5c5SAndroid Build Coastguard Worker (alphaBits > 0 ? 1 : 0) + (luminanceBits > 0 ? 1 : 0) + (depthBits > 0 ? 1 : 0) +
140*8975f5c5SAndroid Build Coastguard Worker (stencilBits > 0 ? 1 : 0);
141*8975f5c5SAndroid Build Coastguard Worker }
142*8975f5c5SAndroid Build Coastguard Worker
Format(FormatID id,GLenum glFormat,GLenum fboFormat,rx::MipGenerationFunction mipGen,const rx::FastCopyFunctionMap & fastCopyFunctions,rx::PixelReadFunction colorRead,rx::PixelWriteFunction colorWrite,GLenum componentType,GLuint redBits,GLuint greenBits,GLuint blueBits,GLuint alphaBits,GLuint luminanceBits,GLuint depthBits,GLuint stencilBits,GLuint pixelBytes,GLuint componentAlignmentMask,bool isBlock,bool isFixed,bool isScaled,bool isSRGB,bool isYUV,gl::VertexAttribType vertexAttribType)143*8975f5c5SAndroid Build Coastguard Worker constexpr Format::Format(FormatID id,
144*8975f5c5SAndroid Build Coastguard Worker GLenum glFormat,
145*8975f5c5SAndroid Build Coastguard Worker GLenum fboFormat,
146*8975f5c5SAndroid Build Coastguard Worker rx::MipGenerationFunction mipGen,
147*8975f5c5SAndroid Build Coastguard Worker const rx::FastCopyFunctionMap &fastCopyFunctions,
148*8975f5c5SAndroid Build Coastguard Worker rx::PixelReadFunction colorRead,
149*8975f5c5SAndroid Build Coastguard Worker rx::PixelWriteFunction colorWrite,
150*8975f5c5SAndroid Build Coastguard Worker GLenum componentType,
151*8975f5c5SAndroid Build Coastguard Worker GLuint redBits,
152*8975f5c5SAndroid Build Coastguard Worker GLuint greenBits,
153*8975f5c5SAndroid Build Coastguard Worker GLuint blueBits,
154*8975f5c5SAndroid Build Coastguard Worker GLuint alphaBits,
155*8975f5c5SAndroid Build Coastguard Worker GLuint luminanceBits,
156*8975f5c5SAndroid Build Coastguard Worker GLuint depthBits,
157*8975f5c5SAndroid Build Coastguard Worker GLuint stencilBits,
158*8975f5c5SAndroid Build Coastguard Worker GLuint pixelBytes,
159*8975f5c5SAndroid Build Coastguard Worker GLuint componentAlignmentMask,
160*8975f5c5SAndroid Build Coastguard Worker bool isBlock,
161*8975f5c5SAndroid Build Coastguard Worker bool isFixed,
162*8975f5c5SAndroid Build Coastguard Worker bool isScaled,
163*8975f5c5SAndroid Build Coastguard Worker bool isSRGB,
164*8975f5c5SAndroid Build Coastguard Worker bool isYUV,
165*8975f5c5SAndroid Build Coastguard Worker gl::VertexAttribType vertexAttribType)
166*8975f5c5SAndroid Build Coastguard Worker : id(id),
167*8975f5c5SAndroid Build Coastguard Worker glInternalFormat(glFormat),
168*8975f5c5SAndroid Build Coastguard Worker fboImplementationInternalFormat(fboFormat),
169*8975f5c5SAndroid Build Coastguard Worker mipGenerationFunction(mipGen),
170*8975f5c5SAndroid Build Coastguard Worker pixelReadFunction(colorRead),
171*8975f5c5SAndroid Build Coastguard Worker pixelWriteFunction(colorWrite),
172*8975f5c5SAndroid Build Coastguard Worker fastCopyFunctions(fastCopyFunctions),
173*8975f5c5SAndroid Build Coastguard Worker componentType(componentType),
174*8975f5c5SAndroid Build Coastguard Worker redBits(redBits),
175*8975f5c5SAndroid Build Coastguard Worker greenBits(greenBits),
176*8975f5c5SAndroid Build Coastguard Worker blueBits(blueBits),
177*8975f5c5SAndroid Build Coastguard Worker alphaBits(alphaBits),
178*8975f5c5SAndroid Build Coastguard Worker luminanceBits(luminanceBits),
179*8975f5c5SAndroid Build Coastguard Worker depthBits(depthBits),
180*8975f5c5SAndroid Build Coastguard Worker stencilBits(stencilBits),
181*8975f5c5SAndroid Build Coastguard Worker pixelBytes(pixelBytes),
182*8975f5c5SAndroid Build Coastguard Worker componentAlignmentMask(componentAlignmentMask),
183*8975f5c5SAndroid Build Coastguard Worker channelCount(GetChannelCount(redBits,
184*8975f5c5SAndroid Build Coastguard Worker greenBits,
185*8975f5c5SAndroid Build Coastguard Worker blueBits,
186*8975f5c5SAndroid Build Coastguard Worker alphaBits,
187*8975f5c5SAndroid Build Coastguard Worker luminanceBits,
188*8975f5c5SAndroid Build Coastguard Worker depthBits,
189*8975f5c5SAndroid Build Coastguard Worker stencilBits)),
190*8975f5c5SAndroid Build Coastguard Worker isBlock(isBlock),
191*8975f5c5SAndroid Build Coastguard Worker isFixed(isFixed),
192*8975f5c5SAndroid Build Coastguard Worker isScaled(isScaled),
193*8975f5c5SAndroid Build Coastguard Worker isSRGB(isSRGB),
194*8975f5c5SAndroid Build Coastguard Worker isYUV(isYUV),
195*8975f5c5SAndroid Build Coastguard Worker vertexAttribType(vertexAttribType)
196*8975f5c5SAndroid Build Coastguard Worker {}
197*8975f5c5SAndroid Build Coastguard Worker
hasDepthOrStencilBits()198*8975f5c5SAndroid Build Coastguard Worker constexpr bool Format::hasDepthOrStencilBits() const
199*8975f5c5SAndroid Build Coastguard Worker {
200*8975f5c5SAndroid Build Coastguard Worker return depthBits > 0 || stencilBits > 0;
201*8975f5c5SAndroid Build Coastguard Worker }
202*8975f5c5SAndroid Build Coastguard Worker
hasDepthAndStencilBits()203*8975f5c5SAndroid Build Coastguard Worker constexpr bool Format::hasDepthAndStencilBits() const
204*8975f5c5SAndroid Build Coastguard Worker {
205*8975f5c5SAndroid Build Coastguard Worker return depthBits > 0 && stencilBits > 0;
206*8975f5c5SAndroid Build Coastguard Worker }
207*8975f5c5SAndroid Build Coastguard Worker
isLUMA()208*8975f5c5SAndroid Build Coastguard Worker constexpr bool Format::isLUMA() const
209*8975f5c5SAndroid Build Coastguard Worker {
210*8975f5c5SAndroid Build Coastguard Worker // There's no format with G or B without R
211*8975f5c5SAndroid Build Coastguard Worker ASSERT(redBits > 0 || (greenBits == 0 && blueBits == 0));
212*8975f5c5SAndroid Build Coastguard Worker return redBits == 0 && (luminanceBits > 0 || alphaBits > 0);
213*8975f5c5SAndroid Build Coastguard Worker }
214*8975f5c5SAndroid Build Coastguard Worker
isBGRA()215*8975f5c5SAndroid Build Coastguard Worker constexpr bool Format::isBGRA() const
216*8975f5c5SAndroid Build Coastguard Worker {
217*8975f5c5SAndroid Build Coastguard Worker return id == FormatID::B8G8R8A8_UNORM || id == FormatID::B8G8R8A8_UNORM_SRGB ||
218*8975f5c5SAndroid Build Coastguard Worker id == FormatID::B8G8R8A8_TYPELESS || id == FormatID::B8G8R8A8_TYPELESS_SRGB;
219*8975f5c5SAndroid Build Coastguard Worker }
220*8975f5c5SAndroid Build Coastguard Worker
isSint()221*8975f5c5SAndroid Build Coastguard Worker constexpr bool Format::isSint() const
222*8975f5c5SAndroid Build Coastguard Worker {
223*8975f5c5SAndroid Build Coastguard Worker return componentType == GL_INT;
224*8975f5c5SAndroid Build Coastguard Worker }
225*8975f5c5SAndroid Build Coastguard Worker
isUint()226*8975f5c5SAndroid Build Coastguard Worker constexpr bool Format::isUint() const
227*8975f5c5SAndroid Build Coastguard Worker {
228*8975f5c5SAndroid Build Coastguard Worker return componentType == GL_UNSIGNED_INT;
229*8975f5c5SAndroid Build Coastguard Worker }
230*8975f5c5SAndroid Build Coastguard Worker
isSnorm()231*8975f5c5SAndroid Build Coastguard Worker constexpr bool Format::isSnorm() const
232*8975f5c5SAndroid Build Coastguard Worker {
233*8975f5c5SAndroid Build Coastguard Worker return componentType == GL_SIGNED_NORMALIZED;
234*8975f5c5SAndroid Build Coastguard Worker }
235*8975f5c5SAndroid Build Coastguard Worker
isUnorm()236*8975f5c5SAndroid Build Coastguard Worker constexpr bool Format::isUnorm() const
237*8975f5c5SAndroid Build Coastguard Worker {
238*8975f5c5SAndroid Build Coastguard Worker return componentType == GL_UNSIGNED_NORMALIZED;
239*8975f5c5SAndroid Build Coastguard Worker }
240*8975f5c5SAndroid Build Coastguard Worker
isFloat()241*8975f5c5SAndroid Build Coastguard Worker constexpr bool Format::isFloat() const
242*8975f5c5SAndroid Build Coastguard Worker {
243*8975f5c5SAndroid Build Coastguard Worker return componentType == GL_FLOAT;
244*8975f5c5SAndroid Build Coastguard Worker }
245*8975f5c5SAndroid Build Coastguard Worker
isVertexTypeHalfFloat()246*8975f5c5SAndroid Build Coastguard Worker constexpr bool Format::isVertexTypeHalfFloat() const
247*8975f5c5SAndroid Build Coastguard Worker {
248*8975f5c5SAndroid Build Coastguard Worker return vertexAttribType == gl::VertexAttribType::HalfFloat;
249*8975f5c5SAndroid Build Coastguard Worker }
250*8975f5c5SAndroid Build Coastguard Worker
251*8975f5c5SAndroid Build Coastguard Worker template <typename T>
252*8975f5c5SAndroid Build Coastguard Worker using FormatMap = PackedEnumMap<FormatID, T, kNumANGLEFormats>;
253*8975f5c5SAndroid Build Coastguard Worker
254*8975f5c5SAndroid Build Coastguard Worker } // namespace angle
255*8975f5c5SAndroid Build Coastguard Worker
256*8975f5c5SAndroid Build Coastguard Worker #endif // LIBANGLE_RENDERER_FORMAT_H_
257