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