• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 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 <stdint.h>
13 #include <cstddef>
14 #include <ostream>
15 
16 #include "angle_gl.h"
17 #include "common/android_util.h"
18 #include "libANGLE/Caps.h"
19 #include "libANGLE/Error.h"
20 #include "libANGLE/Version.h"
21 #include "libANGLE/VertexAttribute.h"
22 #include "libANGLE/angletypes.h"
23 
24 namespace gl
25 {
26 struct VertexAttribute;
27 
28 struct FormatType final
29 {
30     FormatType();
31     FormatType(GLenum format_, GLenum type_);
32     FormatType(const FormatType &other) = default;
33     FormatType &operator=(const FormatType &other) = default;
34 
35     bool operator<(const FormatType &other) const;
36 
37     GLenum format;
38     GLenum type;
39 };
40 
41 struct Type
42 {
TypeType43     Type() : bytes(0), bytesShift(0), specialInterpretation(0) {}
44 
TypeType45     explicit Type(uint32_t packedTypeInfo)
46         : bytes(packedTypeInfo & 0xff),
47           bytesShift((packedTypeInfo >> 8) & 0xff),
48           specialInterpretation((packedTypeInfo >> 16) & 1)
49     {}
50 
51     GLuint bytes;
52     GLuint bytesShift;  // Bit shift by this value to effectively divide/multiply by "bytes" in a
53                         // more optimal way
54     bool specialInterpretation;
55 };
56 
57 uint32_t GetPackedTypeInfo(GLenum type);
58 
GetNonLinearFormat(const GLenum format)59 ANGLE_INLINE GLenum GetNonLinearFormat(const GLenum format)
60 {
61     switch (format)
62     {
63         case GL_BGRA8_EXT:
64             return GL_BGRA8_SRGB_ANGLEX;
65         case GL_RGBA8:
66             return GL_SRGB8_ALPHA8;
67         case GL_RGB8:
68         case GL_BGRX8_ANGLEX:
69             return GL_SRGB8;
70         case GL_RGBA16F:
71             return GL_RGBA16F;
72         default:
73             return GL_NONE;
74     }
75 }
76 
ColorspaceFormatOverride(const EGLenum colorspace,GLenum * rendertargetformat)77 ANGLE_INLINE bool ColorspaceFormatOverride(const EGLenum colorspace, GLenum *rendertargetformat)
78 {
79     // Override the rendertargetformat based on colorpsace
80     switch (colorspace)
81     {
82         case EGL_GL_COLORSPACE_LINEAR:                 // linear colorspace no translation needed
83         case EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT:       // linear colorspace no translation needed
84         case EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT:  // linear colorspace no translation needed
85         case EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT:  // App, not the HW, will specify the
86                                                             // transfer function
87         case EGL_GL_COLORSPACE_SCRGB_EXT:  // App, not the HW, will specify the transfer function
88             // No translation
89             return true;
90         case EGL_GL_COLORSPACE_SRGB_KHR:
91         case EGL_GL_COLORSPACE_DISPLAY_P3_EXT:
92         {
93             GLenum nonLinearFormat = GetNonLinearFormat(*rendertargetformat);
94             if (nonLinearFormat != GL_NONE)
95             {
96                 *rendertargetformat = nonLinearFormat;
97                 return true;
98             }
99             else
100             {
101                 return false;
102             }
103         }
104         break;
105         default:
106             UNREACHABLE();
107             return false;
108     }
109 }
110 
GetTypeInfo(GLenum type)111 ANGLE_INLINE const Type GetTypeInfo(GLenum type)
112 {
113     return Type(GetPackedTypeInfo(type));
114 }
115 
116 // This helpers use tricks based on the assumption that the type has certain values.
117 static_assert(static_cast<GLuint>(DrawElementsType::UnsignedByte) == 0, "Please update this code.");
118 static_assert(static_cast<GLuint>(DrawElementsType::UnsignedShort) == 1,
119               "Please update this code.");
120 static_assert(static_cast<GLuint>(DrawElementsType::UnsignedInt) == 2, "Please update this code.");
GetDrawElementsTypeSize(DrawElementsType type)121 ANGLE_INLINE GLuint GetDrawElementsTypeSize(DrawElementsType type)
122 {
123     return (1 << static_cast<GLuint>(type));
124 }
125 
GetDrawElementsTypeShift(DrawElementsType type)126 ANGLE_INLINE GLuint GetDrawElementsTypeShift(DrawElementsType type)
127 {
128     return static_cast<GLuint>(type);
129 }
130 
131 // Information about an OpenGL internal format.  Can be keyed on the internalFormat and type
132 // members.
133 struct InternalFormat
134 {
135     InternalFormat();
136     InternalFormat(const InternalFormat &other);
137 
138     GLuint computePixelBytes(GLenum formatType) const;
139 
140     ANGLE_NO_DISCARD bool computeRowPitch(GLenum formatType,
141                                           GLsizei width,
142                                           GLint alignment,
143                                           GLint rowLength,
144                                           GLuint *resultOut) const;
145     ANGLE_NO_DISCARD bool computeDepthPitch(GLsizei height,
146                                             GLint imageHeight,
147                                             GLuint rowPitch,
148                                             GLuint *resultOut) const;
149     ANGLE_NO_DISCARD bool computeDepthPitch(GLenum formatType,
150                                             GLsizei width,
151                                             GLsizei height,
152                                             GLint alignment,
153                                             GLint rowLength,
154                                             GLint imageHeight,
155                                             GLuint *resultOut) const;
156 
157     ANGLE_NO_DISCARD bool computeCompressedImageSize(const Extents &size, GLuint *resultOut) const;
158 
159     ANGLE_NO_DISCARD std::pair<GLuint, GLuint> getCompressedImageMinBlocks() const;
160 
161     ANGLE_NO_DISCARD bool computeSkipBytes(GLenum formatType,
162                                            GLuint rowPitch,
163                                            GLuint depthPitch,
164                                            const PixelStoreStateBase &state,
165                                            bool is3D,
166                                            GLuint *resultOut) const;
167 
168     ANGLE_NO_DISCARD bool computePackUnpackEndByte(GLenum formatType,
169                                                    const Extents &size,
170                                                    const PixelStoreStateBase &state,
171                                                    bool is3D,
172                                                    GLuint *resultOut) const;
173 
174     bool isLUMA() const;
175     GLenum getReadPixelsFormat(const Extensions &extensions) const;
176     GLenum getReadPixelsType(const Version &version) const;
177 
178     // Support upload a portion of image?
179     bool supportSubImage() const;
180 
181     // Return true if the format is a required renderbuffer format in the given version of the core
182     // spec. Note that it isn't always clear whether all the rules that apply to core required
183     // renderbuffer formats also apply to additional formats added by extensions. Because of this
184     // extension formats are conservatively not included.
185     bool isRequiredRenderbufferFormat(const Version &version) const;
186 
187     bool isInt() const;
188     bool isDepthOrStencil() const;
189 
190     bool operator==(const InternalFormat &other) const;
191     bool operator!=(const InternalFormat &other) const;
192 
193     GLenum internalFormat;
194 
195     bool sized;
196     GLenum sizedInternalFormat;
197 
198     GLuint redBits;
199     GLuint greenBits;
200     GLuint blueBits;
201 
202     GLuint luminanceBits;
203 
204     GLuint alphaBits;
205     GLuint sharedBits;
206 
207     GLuint depthBits;
208     GLuint stencilBits;
209 
210     GLuint pixelBytes;
211 
212     GLuint componentCount;
213 
214     bool compressed;
215     GLuint compressedBlockWidth;
216     GLuint compressedBlockHeight;
217     GLuint compressedBlockDepth;
218 
219     GLenum format;
220     GLenum type;
221 
222     GLenum componentType;
223     GLenum colorEncoding;
224 
225     typedef bool (*SupportCheckFunction)(const Version &, const Extensions &);
226     SupportCheckFunction textureSupport;
227     SupportCheckFunction filterSupport;
228     SupportCheckFunction textureAttachmentSupport;  // glFramebufferTexture2D
229     SupportCheckFunction renderbufferSupport;       // glFramebufferRenderbuffer
230     SupportCheckFunction blendSupport;
231 };
232 
233 // A "Format" wraps an InternalFormat struct, querying it from either a sized internal format or
234 // unsized internal format and type.
235 // TODO(geofflang): Remove this, it doesn't add any more information than the InternalFormat object.
236 struct Format
237 {
238     // Sized types only.
239     explicit Format(GLenum internalFormat);
240 
241     // Sized or unsized types.
242     explicit Format(const InternalFormat &internalFormat);
243     Format(GLenum internalFormat, GLenum type);
244 
245     Format(const Format &other);
246     Format &operator=(const Format &other);
247 
248     bool valid() const;
249 
250     static Format Invalid();
251     static bool SameSized(const Format &a, const Format &b);
252     static bool EquivalentForBlit(const Format &a, const Format &b);
253 
254     friend std::ostream &operator<<(std::ostream &os, const Format &fmt);
255 
256     // This is the sized info.
257     const InternalFormat *info;
258 };
259 
260 const InternalFormat &GetSizedInternalFormatInfo(GLenum internalFormat);
261 const InternalFormat &GetInternalFormatInfo(GLenum internalFormat, GLenum type);
262 
263 // Strip sizing information from an internal format.  Doesn't necessarily validate that the internal
264 // format is valid.
265 GLenum GetUnsizedFormat(GLenum internalFormat);
266 
267 // Return whether the compressed format requires whole image/mip level to be uploaded to texture.
268 bool CompressedFormatRequiresWholeImage(GLenum internalFormat);
269 
270 typedef std::set<GLenum> FormatSet;
271 const FormatSet &GetAllSizedInternalFormats();
272 
273 typedef std::unordered_map<GLenum, std::unordered_map<GLenum, InternalFormat>>
274     InternalFormatInfoMap;
275 const InternalFormatInfoMap &GetInternalFormatMap();
276 
GetNativeVisualID(const InternalFormat & internalFormat)277 ANGLE_INLINE int GetNativeVisualID(const InternalFormat &internalFormat)
278 {
279     int nativeVisualId = 0;
280 #if defined(ANGLE_PLATFORM_ANDROID)
281     nativeVisualId =
282         angle::android::GLInternalFormatToNativePixelFormat(internalFormat.internalFormat);
283 #endif
284     return nativeVisualId;
285 }
286 
287 // From the ESSL 3.00.4 spec:
288 // Vertex shader inputs can only be float, floating-point vectors, matrices, signed and unsigned
289 // integers and integer vectors. Vertex shader inputs cannot be arrays or structures.
290 
291 enum AttributeType
292 {
293     ATTRIBUTE_FLOAT,
294     ATTRIBUTE_VEC2,
295     ATTRIBUTE_VEC3,
296     ATTRIBUTE_VEC4,
297     ATTRIBUTE_INT,
298     ATTRIBUTE_IVEC2,
299     ATTRIBUTE_IVEC3,
300     ATTRIBUTE_IVEC4,
301     ATTRIBUTE_UINT,
302     ATTRIBUTE_UVEC2,
303     ATTRIBUTE_UVEC3,
304     ATTRIBUTE_UVEC4,
305     ATTRIBUTE_MAT2,
306     ATTRIBUTE_MAT3,
307     ATTRIBUTE_MAT4,
308     ATTRIBUTE_MAT2x3,
309     ATTRIBUTE_MAT2x4,
310     ATTRIBUTE_MAT3x2,
311     ATTRIBUTE_MAT3x4,
312     ATTRIBUTE_MAT4x2,
313     ATTRIBUTE_MAT4x3,
314 };
315 
316 AttributeType GetAttributeType(GLenum enumValue);
317 
318 typedef std::vector<angle::FormatID> InputLayout;
319 
320 struct VertexFormat : private angle::NonCopyable
321 {
322     VertexFormat(GLenum typeIn, GLboolean normalizedIn, GLuint componentsIn, bool pureIntegerIn);
323 
324     GLenum type;
325     GLboolean normalized;
326     GLuint components;
327     bool pureInteger;
328 };
329 
330 angle::FormatID GetVertexFormatID(VertexAttribType type,
331                                   GLboolean normalized,
332                                   GLuint components,
333                                   bool pureInteger);
334 
335 angle::FormatID GetVertexFormatID(const VertexAttribute &attrib, VertexAttribType currentValueType);
336 angle::FormatID GetCurrentValueFormatID(VertexAttribType currentValueType);
337 const VertexFormat &GetVertexFormatFromID(angle::FormatID vertexFormatID);
338 size_t GetVertexFormatSize(angle::FormatID vertexFormatID);
339 
IsS3TCFormat(const GLenum format)340 ANGLE_INLINE bool IsS3TCFormat(const GLenum format)
341 {
342     switch (format)
343     {
344         case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
345         case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
346         case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
347         case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
348         case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
349         case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
350         case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
351         case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
352             return true;
353 
354         default:
355             return false;
356     }
357 }
358 
IsRGTCFormat(const GLenum format)359 ANGLE_INLINE bool IsRGTCFormat(const GLenum format)
360 {
361     switch (format)
362     {
363         case GL_COMPRESSED_RED_RGTC1_EXT:
364         case GL_COMPRESSED_SIGNED_RED_RGTC1_EXT:
365         case GL_COMPRESSED_RED_GREEN_RGTC2_EXT:
366         case GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT:
367             return true;
368 
369         default:
370             return false;
371     }
372 }
373 
IsASTC2DFormat(const GLenum format)374 ANGLE_INLINE bool IsASTC2DFormat(const GLenum format)
375 {
376     if ((format >= GL_COMPRESSED_RGBA_ASTC_4x4_KHR &&
377          format <= GL_COMPRESSED_RGBA_ASTC_12x12_KHR) ||
378         (format >= GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR &&
379          format <= GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR))
380     {
381         return true;
382     }
383     return false;
384 }
385 
IsETC2EACFormat(const GLenum format)386 ANGLE_INLINE bool IsETC2EACFormat(const GLenum format)
387 {
388     // ES 3.1, Table 8.19
389     switch (format)
390     {
391         case GL_COMPRESSED_R11_EAC:
392         case GL_COMPRESSED_SIGNED_R11_EAC:
393         case GL_COMPRESSED_RG11_EAC:
394         case GL_COMPRESSED_SIGNED_RG11_EAC:
395         case GL_COMPRESSED_RGB8_ETC2:
396         case GL_COMPRESSED_SRGB8_ETC2:
397         case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
398         case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
399         case GL_COMPRESSED_RGBA8_ETC2_EAC:
400         case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
401             return true;
402 
403         default:
404             return false;
405     }
406 }
407 
408 // Check if an internal format is ever valid in ES3.  Makes no checks about support for a specific
409 // context.
410 bool ValidES3InternalFormat(GLenum internalFormat);
411 
412 // Implemented in format_map_autogen.cpp
413 bool ValidES3Format(GLenum format);
414 bool ValidES3Type(GLenum type);
415 bool ValidES3FormatCombination(GLenum format, GLenum type, GLenum internalFormat);
416 
417 // Implemented in format_map_desktop.cpp
418 bool ValidDesktopFormat(GLenum format);
419 bool ValidDesktopType(GLenum type);
420 bool ValidDesktopFormatCombination(GLenum format, GLenum type, GLenum internalFormat);
421 
422 // Implemented in es3_copy_conversion_table_autogen.cpp
423 bool ValidES3CopyConversion(GLenum textureFormat, GLenum framebufferFormat);
424 
GetVertexAttributeComponentType(bool pureInteger,VertexAttribType type)425 ANGLE_INLINE ComponentType GetVertexAttributeComponentType(bool pureInteger, VertexAttribType type)
426 {
427     if (pureInteger)
428     {
429         switch (type)
430         {
431             case VertexAttribType::Byte:
432             case VertexAttribType::Short:
433             case VertexAttribType::Int:
434                 return ComponentType::Int;
435 
436             case VertexAttribType::UnsignedByte:
437             case VertexAttribType::UnsignedShort:
438             case VertexAttribType::UnsignedInt:
439                 return ComponentType::UnsignedInt;
440 
441             default:
442                 UNREACHABLE();
443                 return ComponentType::NoType;
444         }
445     }
446     else
447     {
448         return ComponentType::Float;
449     }
450 }
451 }  // namespace gl
452 
453 #endif  // LIBANGLE_FORMATUTILS_H_
454