1 //
2 // Copyright (c) 2013 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 
7 // formatutils.h: Queries for GL image formats.
8 
9 #ifndef LIBANGLE_FORMATUTILS_H_
10 #define LIBANGLE_FORMATUTILS_H_
11 
12 #include <cstddef>
13 #include <ostream>
14 #include <stdint.h>
15 
16 #include "angle_gl.h"
17 #include "libANGLE/Caps.h"
18 #include "libANGLE/Error.h"
19 #include "libANGLE/Version.h"
20 #include "libANGLE/angletypes.h"
21 
22 namespace gl
23 {
24 struct VertexAttribute;
25 
26 struct FormatType final
27 {
28     FormatType();
29     FormatType(GLenum format_, GLenum type_);
30     FormatType(const FormatType &other) = default;
31     FormatType &operator=(const FormatType &other) = default;
32 
33     bool operator<(const FormatType &other) const;
34 
35     GLenum format;
36     GLenum type;
37 };
38 
39 struct Type
40 {
41     Type();
42 
43     GLuint bytes;
44     GLuint bytesShift; // Bit shift by this value to effectively divide/multiply by "bytes" in a more optimal way
45     bool specialInterpretation;
46 };
47 const Type &GetTypeInfo(GLenum type);
48 
49 // Information about an OpenGL internal format.  Can be keyed on the internalFormat and type
50 // members.
51 struct InternalFormat
52 {
53     InternalFormat();
54     InternalFormat(const InternalFormat &other);
55 
56     ErrorOrResult<GLuint> computeRowPitch(GLsizei width,
57                                           GLint alignment,
58                                           GLint rowLength) const;
59     static ErrorOrResult<GLuint> computeDepthPitch(GLsizei height,
60                                                    GLint imageHeight,
61                                                    GLuint rowPitch);
62     ErrorOrResult<GLuint> computeDepthPitch(GLsizei width,
63                                             GLsizei height,
64                                             GLint alignment,
65                                             GLint rowLength,
66                                             GLint imageHeight) const;
67 
68     ErrorOrResult<GLuint> computeCompressedImageSize(const Extents &size) const;
69 
70     ErrorOrResult<GLuint> computeSkipBytes(GLuint rowPitch,
71                                            GLuint depthPitch,
72                                            const PixelStoreStateBase &state,
73                                            bool is3D) const;
74 
75     ErrorOrResult<GLuint> computePackUnpackEndByte(const Extents &size,
76                                                    const PixelStoreStateBase &state,
77                                                    bool is3D) const;
78 
79     bool isLUMA() const;
80     GLenum getReadPixelsFormat() const;
81     GLenum getReadPixelsType(const Version &version) const;
82 
83     // Return true if the format is a required renderbuffer format in the given version of the core
84     // spec. Note that it isn't always clear whether all the rules that apply to core required
85     // renderbuffer formats also apply to additional formats added by extensions. Because of this
86     // extension formats are conservatively not included.
87     bool isRequiredRenderbufferFormat(const Version &version) const;
88 
89     bool operator==(const InternalFormat &other) const;
90     bool operator!=(const InternalFormat &other) const;
91 
92     GLenum internalFormat;
93 
94     bool sized;
95     GLenum sizedInternalFormat;
96 
97     GLuint redBits;
98     GLuint greenBits;
99     GLuint blueBits;
100 
101     GLuint luminanceBits;
102 
103     GLuint alphaBits;
104     GLuint sharedBits;
105 
106     GLuint depthBits;
107     GLuint stencilBits;
108 
109     GLuint pixelBytes;
110 
111     GLuint componentCount;
112 
113     bool compressed;
114     GLuint compressedBlockWidth;
115     GLuint compressedBlockHeight;
116 
117     GLenum format;
118     GLenum type;
119 
120     GLenum componentType;
121     GLenum colorEncoding;
122 
123     typedef bool (*SupportCheckFunction)(const Version &, const Extensions &);
124     SupportCheckFunction textureSupport;
125     SupportCheckFunction renderSupport;
126     SupportCheckFunction filterSupport;
127 };
128 
129 // A "Format" wraps an InternalFormat struct, querying it from either a sized internal format or
130 // unsized internal format and type.
131 // TODO(geofflang): Remove this, it doesn't add any more information than the InternalFormat object.
132 struct Format
133 {
134     // Sized types only.
135     explicit Format(GLenum internalFormat);
136 
137     // Sized or unsized types.
138     explicit Format(const InternalFormat &internalFormat);
139     Format(GLenum internalFormat, GLenum type);
140 
141     Format(const Format &other);
142     Format &operator=(const Format &other);
143 
144     bool valid() const;
145 
146     static Format Invalid();
147     static bool SameSized(const Format &a, const Format &b);
148     static bool EquivalentForBlit(const Format &a, const Format &b);
149 
150     friend std::ostream &operator<<(std::ostream &os, const Format &fmt);
151 
152     // This is the sized info.
153     const InternalFormat *info;
154 };
155 
156 const InternalFormat &GetSizedInternalFormatInfo(GLenum internalFormat);
157 const InternalFormat &GetInternalFormatInfo(GLenum internalFormat, GLenum type);
158 
159 // Strip sizing information from an internal format.  Doesn't necessarily validate that the internal
160 // format is valid.
161 GLenum GetUnsizedFormat(GLenum internalFormat);
162 
GetPackFormatInfo(GLenum internalFormat,GLenum type)163 inline const InternalFormat &GetPackFormatInfo(GLenum internalFormat, GLenum type)
164 {
165     return GetInternalFormatInfo(GetUnsizedFormat(internalFormat), type);
166 }
167 
168 typedef std::set<GLenum> FormatSet;
169 const FormatSet &GetAllSizedInternalFormats();
170 
171 // From the ESSL 3.00.4 spec:
172 // Vertex shader inputs can only be float, floating-point vectors, matrices, signed and unsigned
173 // integers and integer vectors. Vertex shader inputs cannot be arrays or structures.
174 
175 enum AttributeType
176 {
177     ATTRIBUTE_FLOAT,
178     ATTRIBUTE_VEC2,
179     ATTRIBUTE_VEC3,
180     ATTRIBUTE_VEC4,
181     ATTRIBUTE_INT,
182     ATTRIBUTE_IVEC2,
183     ATTRIBUTE_IVEC3,
184     ATTRIBUTE_IVEC4,
185     ATTRIBUTE_UINT,
186     ATTRIBUTE_UVEC2,
187     ATTRIBUTE_UVEC3,
188     ATTRIBUTE_UVEC4,
189     ATTRIBUTE_MAT2,
190     ATTRIBUTE_MAT3,
191     ATTRIBUTE_MAT4,
192     ATTRIBUTE_MAT2x3,
193     ATTRIBUTE_MAT2x4,
194     ATTRIBUTE_MAT3x2,
195     ATTRIBUTE_MAT3x4,
196     ATTRIBUTE_MAT4x2,
197     ATTRIBUTE_MAT4x3,
198 };
199 
200 AttributeType GetAttributeType(GLenum enumValue);
201 
202 enum VertexFormatType
203 {
204     VERTEX_FORMAT_INVALID,
205     VERTEX_FORMAT_SBYTE1,
206     VERTEX_FORMAT_SBYTE1_NORM,
207     VERTEX_FORMAT_SBYTE2,
208     VERTEX_FORMAT_SBYTE2_NORM,
209     VERTEX_FORMAT_SBYTE3,
210     VERTEX_FORMAT_SBYTE3_NORM,
211     VERTEX_FORMAT_SBYTE4,
212     VERTEX_FORMAT_SBYTE4_NORM,
213     VERTEX_FORMAT_UBYTE1,
214     VERTEX_FORMAT_UBYTE1_NORM,
215     VERTEX_FORMAT_UBYTE2,
216     VERTEX_FORMAT_UBYTE2_NORM,
217     VERTEX_FORMAT_UBYTE3,
218     VERTEX_FORMAT_UBYTE3_NORM,
219     VERTEX_FORMAT_UBYTE4,
220     VERTEX_FORMAT_UBYTE4_NORM,
221     VERTEX_FORMAT_SSHORT1,
222     VERTEX_FORMAT_SSHORT1_NORM,
223     VERTEX_FORMAT_SSHORT2,
224     VERTEX_FORMAT_SSHORT2_NORM,
225     VERTEX_FORMAT_SSHORT3,
226     VERTEX_FORMAT_SSHORT3_NORM,
227     VERTEX_FORMAT_SSHORT4,
228     VERTEX_FORMAT_SSHORT4_NORM,
229     VERTEX_FORMAT_USHORT1,
230     VERTEX_FORMAT_USHORT1_NORM,
231     VERTEX_FORMAT_USHORT2,
232     VERTEX_FORMAT_USHORT2_NORM,
233     VERTEX_FORMAT_USHORT3,
234     VERTEX_FORMAT_USHORT3_NORM,
235     VERTEX_FORMAT_USHORT4,
236     VERTEX_FORMAT_USHORT4_NORM,
237     VERTEX_FORMAT_SINT1,
238     VERTEX_FORMAT_SINT1_NORM,
239     VERTEX_FORMAT_SINT2,
240     VERTEX_FORMAT_SINT2_NORM,
241     VERTEX_FORMAT_SINT3,
242     VERTEX_FORMAT_SINT3_NORM,
243     VERTEX_FORMAT_SINT4,
244     VERTEX_FORMAT_SINT4_NORM,
245     VERTEX_FORMAT_UINT1,
246     VERTEX_FORMAT_UINT1_NORM,
247     VERTEX_FORMAT_UINT2,
248     VERTEX_FORMAT_UINT2_NORM,
249     VERTEX_FORMAT_UINT3,
250     VERTEX_FORMAT_UINT3_NORM,
251     VERTEX_FORMAT_UINT4,
252     VERTEX_FORMAT_UINT4_NORM,
253     VERTEX_FORMAT_SBYTE1_INT,
254     VERTEX_FORMAT_SBYTE2_INT,
255     VERTEX_FORMAT_SBYTE3_INT,
256     VERTEX_FORMAT_SBYTE4_INT,
257     VERTEX_FORMAT_UBYTE1_INT,
258     VERTEX_FORMAT_UBYTE2_INT,
259     VERTEX_FORMAT_UBYTE3_INT,
260     VERTEX_FORMAT_UBYTE4_INT,
261     VERTEX_FORMAT_SSHORT1_INT,
262     VERTEX_FORMAT_SSHORT2_INT,
263     VERTEX_FORMAT_SSHORT3_INT,
264     VERTEX_FORMAT_SSHORT4_INT,
265     VERTEX_FORMAT_USHORT1_INT,
266     VERTEX_FORMAT_USHORT2_INT,
267     VERTEX_FORMAT_USHORT3_INT,
268     VERTEX_FORMAT_USHORT4_INT,
269     VERTEX_FORMAT_SINT1_INT,
270     VERTEX_FORMAT_SINT2_INT,
271     VERTEX_FORMAT_SINT3_INT,
272     VERTEX_FORMAT_SINT4_INT,
273     VERTEX_FORMAT_UINT1_INT,
274     VERTEX_FORMAT_UINT2_INT,
275     VERTEX_FORMAT_UINT3_INT,
276     VERTEX_FORMAT_UINT4_INT,
277     VERTEX_FORMAT_FIXED1,
278     VERTEX_FORMAT_FIXED2,
279     VERTEX_FORMAT_FIXED3,
280     VERTEX_FORMAT_FIXED4,
281     VERTEX_FORMAT_HALF1,
282     VERTEX_FORMAT_HALF2,
283     VERTEX_FORMAT_HALF3,
284     VERTEX_FORMAT_HALF4,
285     VERTEX_FORMAT_FLOAT1,
286     VERTEX_FORMAT_FLOAT2,
287     VERTEX_FORMAT_FLOAT3,
288     VERTEX_FORMAT_FLOAT4,
289     VERTEX_FORMAT_SINT210,
290     VERTEX_FORMAT_UINT210,
291     VERTEX_FORMAT_SINT210_NORM,
292     VERTEX_FORMAT_UINT210_NORM,
293     VERTEX_FORMAT_SINT210_INT,
294     VERTEX_FORMAT_UINT210_INT,
295 };
296 
297 typedef std::vector<VertexFormatType> InputLayout;
298 
299 struct VertexFormat : private angle::NonCopyable
300 {
301     VertexFormat(GLenum typeIn, GLboolean normalizedIn, GLuint componentsIn, bool pureIntegerIn);
302 
303     GLenum type;
304     GLboolean normalized;
305     GLuint components;
306     bool pureInteger;
307 };
308 
309 VertexFormatType GetVertexFormatType(GLenum type, GLboolean normalized, GLuint components, bool pureInteger);
310 VertexFormatType GetVertexFormatType(const VertexAttribute &attrib);
311 VertexFormatType GetVertexFormatType(const VertexAttribute &attrib, GLenum currentValueType);
312 const VertexFormat &GetVertexFormatFromType(VertexFormatType vertexFormatType);
313 size_t GetVertexFormatTypeSize(VertexFormatType vertexFormatType);
314 
315 // Check if an internal format is ever valid in ES3.  Makes no checks about support for a specific
316 // context.
317 bool ValidES3InternalFormat(GLenum internalFormat);
318 
319 // Implemented in format_map_autogen.cpp
320 bool ValidES3Format(GLenum format);
321 bool ValidES3Type(GLenum type);
322 bool ValidES3FormatCombination(GLenum format, GLenum type, GLenum internalFormat);
323 
324 // Implemented in es3_copy_conversion_table_autogen.cpp
325 bool ValidES3CopyConversion(GLenum textureFormat, GLenum framebufferFormat);
326 
327 }  // namespace gl
328 
329 #endif // LIBANGLE_FORMATUTILS_H_
330