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 gl::VertexAttribType vertexAttribType);
47
Getfinal48 static const Format &Get(FormatID id) { return gFormatInfoTable[static_cast<int>(id)]; }
49
50 static FormatID InternalFormatToID(GLenum internalFormat);
51
52 constexpr bool hasDepthOrStencilBits() const;
53 constexpr bool isLUMA() const;
54
55 constexpr bool isSint() const;
56 constexpr bool isUint() const;
57 constexpr bool isSnorm() const;
58 constexpr bool isUnorm() const;
59 constexpr bool isFloat() const;
60 constexpr bool isVertexTypeHalfFloat() const;
61
isIntfinal62 constexpr bool isInt() const { return isSint() || isUint(); }
isNormfinal63 constexpr bool isNorm() const { return isSnorm() || isUnorm(); }
isPureIntfinal64 constexpr bool isPureInt() const { return isInt() && !isScaled; }
65
66 bool operator==(const Format &other) const { return this->id == other.id; }
67
68 FormatID id;
69
70 // The closest matching GL internal format for the storage this format uses. Note that this
71 // may be a different internal format than the one this ANGLE format is used for.
72 GLenum glInternalFormat;
73
74 // The format we should report to the GL layer when querying implementation formats from a FBO.
75 // This might not be the same as the glInternalFormat, since some DXGI formats don't have
76 // matching GL format enums, like BGRA4, BGR5A1 and B5G6R6.
77 GLenum fboImplementationInternalFormat;
78
79 rx::MipGenerationFunction mipGenerationFunction;
80 rx::PixelReadFunction pixelReadFunction;
81 rx::PixelWriteFunction pixelWriteFunction;
82
83 // A map from a gl::FormatType to a fast pixel copy function for this format.
84 const rx::FastCopyFunctionMap &fastCopyFunctions;
85
86 GLenum componentType;
87
88 GLuint redBits;
89 GLuint greenBits;
90 GLuint blueBits;
91 GLuint alphaBits;
92 GLuint luminanceBits;
93 GLuint depthBits;
94 GLuint stencilBits;
95
96 GLuint pixelBytes;
97
98 // For 1-byte components, is 0x0. For 2-byte, is 0x1. For 4-byte, is 0x3. For all others,
99 // MAX_UINT.
100 GLuint componentAlignmentMask;
101
102 GLuint channelCount;
103
104 bool isBlock;
105 bool isFixed;
106 bool isScaled;
107
108 // For vertex formats only. Returns the "type" value for glVertexAttribPointer etc.
109 gl::VertexAttribType vertexAttribType;
110 };
111
GetChannelCount(GLuint redBits,GLuint greenBits,GLuint blueBits,GLuint alphaBits,GLuint luminanceBits,GLuint depthBits,GLuint stencilBits)112 constexpr GLuint GetChannelCount(GLuint redBits,
113 GLuint greenBits,
114 GLuint blueBits,
115 GLuint alphaBits,
116 GLuint luminanceBits,
117 GLuint depthBits,
118 GLuint stencilBits)
119 {
120 return (redBits > 0 ? 1 : 0) + (greenBits > 0 ? 1 : 0) + (blueBits > 0 ? 1 : 0) +
121 (alphaBits > 0 ? 1 : 0) + (luminanceBits > 0 ? 1 : 0) + (depthBits > 0 ? 1 : 0) +
122 (stencilBits > 0 ? 1 : 0);
123 }
124
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,gl::VertexAttribType vertexAttribType)125 constexpr Format::Format(FormatID id,
126 GLenum glFormat,
127 GLenum fboFormat,
128 rx::MipGenerationFunction mipGen,
129 const rx::FastCopyFunctionMap &fastCopyFunctions,
130 rx::PixelReadFunction colorRead,
131 rx::PixelWriteFunction colorWrite,
132 GLenum componentType,
133 GLuint redBits,
134 GLuint greenBits,
135 GLuint blueBits,
136 GLuint alphaBits,
137 GLuint luminanceBits,
138 GLuint depthBits,
139 GLuint stencilBits,
140 GLuint pixelBytes,
141 GLuint componentAlignmentMask,
142 bool isBlock,
143 bool isFixed,
144 bool isScaled,
145 gl::VertexAttribType vertexAttribType)
146 : id(id),
147 glInternalFormat(glFormat),
148 fboImplementationInternalFormat(fboFormat),
149 mipGenerationFunction(mipGen),
150 pixelReadFunction(colorRead),
151 pixelWriteFunction(colorWrite),
152 fastCopyFunctions(fastCopyFunctions),
153 componentType(componentType),
154 redBits(redBits),
155 greenBits(greenBits),
156 blueBits(blueBits),
157 alphaBits(alphaBits),
158 luminanceBits(luminanceBits),
159 depthBits(depthBits),
160 stencilBits(stencilBits),
161 pixelBytes(pixelBytes),
162 componentAlignmentMask(componentAlignmentMask),
163 channelCount(GetChannelCount(redBits,
164 greenBits,
165 blueBits,
166 alphaBits,
167 luminanceBits,
168 depthBits,
169 stencilBits)),
170 isBlock(isBlock),
171 isFixed(isFixed),
172 isScaled(isScaled),
173 vertexAttribType(vertexAttribType)
174 {}
175
hasDepthOrStencilBits()176 constexpr bool Format::hasDepthOrStencilBits() const
177 {
178 return depthBits > 0 || stencilBits > 0;
179 }
180
isLUMA()181 constexpr bool Format::isLUMA() const
182 {
183 // There's no format with G or B without R
184 ASSERT(redBits > 0 || (greenBits == 0 && blueBits == 0));
185 return redBits == 0 && (luminanceBits > 0 || alphaBits > 0);
186 }
187
isSint()188 constexpr bool Format::isSint() const
189 {
190 return componentType == GL_INT;
191 }
192
isUint()193 constexpr bool Format::isUint() const
194 {
195 return componentType == GL_UNSIGNED_INT;
196 }
197
isSnorm()198 constexpr bool Format::isSnorm() const
199 {
200 return componentType == GL_SIGNED_NORMALIZED;
201 }
202
isUnorm()203 constexpr bool Format::isUnorm() const
204 {
205 return componentType == GL_UNSIGNED_NORMALIZED;
206 }
207
isFloat()208 constexpr bool Format::isFloat() const
209 {
210 return componentType == GL_FLOAT;
211 }
212
isVertexTypeHalfFloat()213 constexpr bool Format::isVertexTypeHalfFloat() const
214 {
215 return vertexAttribType == gl::VertexAttribType::HalfFloat;
216 }
217
218 } // namespace angle
219
220 #endif // LIBANGLE_RENDERER_FORMAT_H_
221