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