utilities.h (12069B)
1 // 2 // Copyright 2002 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 // utilities.h: Conversion functions and other utility routines. 8 9 #ifndef COMMON_UTILITIES_H_ 10 #define COMMON_UTILITIES_H_ 11 12 #include <EGL/egl.h> 13 #include <EGL/eglext.h> 14 #include <GLSLANG/ShaderLang.h> 15 16 #include <math.h> 17 #include <string> 18 #include <vector> 19 20 #include "angle_gl.h" 21 22 #include "common/PackedEnums.h" 23 #include "common/mathutil.h" 24 #include "common/platform.h" 25 26 namespace sh 27 { 28 struct ShaderVariable; 29 } 30 31 constexpr bool ShPixelLocalStorageTypeUsesImages(ShPixelLocalStorageType type) 32 { 33 return type == ShPixelLocalStorageType::ImageStoreR32PackedFormats || 34 type == ShPixelLocalStorageType::ImageStoreNativeFormats; 35 } 36 37 namespace gl 38 { 39 40 int VariableComponentCount(GLenum type); 41 GLenum VariableComponentType(GLenum type); 42 size_t VariableComponentSize(GLenum type); 43 size_t VariableInternalSize(GLenum type); 44 size_t VariableExternalSize(GLenum type); 45 int VariableRowCount(GLenum type); 46 int VariableColumnCount(GLenum type); 47 bool IsSamplerType(GLenum type); 48 bool IsSamplerCubeType(GLenum type); 49 bool IsSamplerYUVType(GLenum type); 50 bool IsImageType(GLenum type); 51 bool IsImage2DType(GLenum type); 52 bool IsAtomicCounterType(GLenum type); 53 bool IsOpaqueType(GLenum type); 54 bool IsMatrixType(GLenum type); 55 GLenum TransposeMatrixType(GLenum type); 56 int VariableRegisterCount(GLenum type); 57 int MatrixRegisterCount(GLenum type, bool isRowMajorMatrix); 58 int MatrixComponentCount(GLenum type, bool isRowMajorMatrix); 59 int VariableSortOrder(GLenum type); 60 GLenum VariableBoolVectorType(GLenum type); 61 std::string GetGLSLTypeString(GLenum type); 62 63 int AllocateFirstFreeBits(unsigned int *bits, unsigned int allocationSize, unsigned int bitsSize); 64 65 // Parse the base resource name and array indices. Returns the base name of the resource. 66 // If the provided name doesn't index an array, the outSubscripts vector will be empty. 67 // If the provided name indexes an array, the outSubscripts vector will contain indices with 68 // outermost array indices in the back. If an array index is invalid, GL_INVALID_INDEX is added to 69 // outSubscripts. 70 std::string ParseResourceName(const std::string &name, std::vector<unsigned int> *outSubscripts); 71 72 bool IsBuiltInName(const char *name); 73 ANGLE_INLINE bool IsBuiltInName(const std::string &name) 74 { 75 return IsBuiltInName(name.c_str()); 76 } 77 78 // Strips only the last array index from a resource name. 79 std::string StripLastArrayIndex(const std::string &name); 80 81 bool SamplerNameContainsNonZeroArrayElement(const std::string &name); 82 83 // Find the range of index values in the provided indices pointer. Primitive restart indices are 84 // only counted in the range if primitive restart is disabled. 85 IndexRange ComputeIndexRange(DrawElementsType indexType, 86 const GLvoid *indices, 87 size_t count, 88 bool primitiveRestartEnabled); 89 90 // Get the primitive restart index value for the given index type. 91 GLuint GetPrimitiveRestartIndex(DrawElementsType indexType); 92 93 // Get the primitive restart index value with the given C++ type. 94 template <typename T> 95 constexpr T GetPrimitiveRestartIndexFromType() 96 { 97 return std::numeric_limits<T>::max(); 98 } 99 100 static_assert(GetPrimitiveRestartIndexFromType<uint8_t>() == 0xFF, 101 "verify restart index for uint8_t values"); 102 static_assert(GetPrimitiveRestartIndexFromType<uint16_t>() == 0xFFFF, 103 "verify restart index for uint8_t values"); 104 static_assert(GetPrimitiveRestartIndexFromType<uint32_t>() == 0xFFFFFFFF, 105 "verify restart index for uint8_t values"); 106 107 bool IsTriangleMode(PrimitiveMode drawMode); 108 bool IsPolygonMode(PrimitiveMode mode); 109 110 namespace priv 111 { 112 extern const angle::PackedEnumMap<PrimitiveMode, bool> gLineModes; 113 } // namespace priv 114 115 ANGLE_INLINE bool IsLineMode(PrimitiveMode primitiveMode) 116 { 117 return priv::gLineModes[primitiveMode]; 118 } 119 120 bool IsIntegerFormat(GLenum unsizedFormat); 121 122 // Returns the product of the sizes in the vector, or 1 if the vector is empty. Doesn't currently 123 // perform overflow checks. 124 unsigned int ArraySizeProduct(const std::vector<unsigned int> &arraySizes); 125 // Returns the product of the sizes in the vector except for the outermost dimension, or 1 if the 126 // vector is empty. 127 unsigned int InnerArraySizeProduct(const std::vector<unsigned int> &arraySizes); 128 // Returns the outermost array dimension, or 1 if the vector is empty. 129 unsigned int OutermostArraySize(const std::vector<unsigned int> &arraySizes); 130 131 // Return the array index at the end of name, and write the length of name before the final array 132 // index into nameLengthWithoutArrayIndexOut. In case name doesn't include an array index, return 133 // GL_INVALID_INDEX and write the length of the original string. 134 unsigned int ParseArrayIndex(const std::string &name, size_t *nameLengthWithoutArrayIndexOut); 135 136 enum class SamplerFormat : uint8_t 137 { 138 Float = 0, 139 Unsigned = 1, 140 Signed = 2, 141 Shadow = 3, 142 143 InvalidEnum = 4, 144 EnumCount = 4, 145 }; 146 147 struct UniformTypeInfo final : angle::NonCopyable 148 { 149 inline constexpr UniformTypeInfo(GLenum type, 150 GLenum componentType, 151 GLenum textureType, 152 GLenum transposedMatrixType, 153 GLenum boolVectorType, 154 SamplerFormat samplerFormat, 155 int rowCount, 156 int columnCount, 157 int componentCount, 158 size_t componentSize, 159 size_t internalSize, 160 size_t externalSize, 161 bool isSampler, 162 bool isMatrixType, 163 bool isImageType); 164 165 GLenum type; 166 GLenum componentType; 167 GLenum textureType; 168 GLenum transposedMatrixType; 169 GLenum boolVectorType; 170 SamplerFormat samplerFormat; 171 int rowCount; 172 int columnCount; 173 int componentCount; 174 size_t componentSize; 175 size_t internalSize; 176 size_t externalSize; 177 bool isSampler; 178 bool isMatrixType; 179 bool isImageType; 180 }; 181 182 inline constexpr UniformTypeInfo::UniformTypeInfo(GLenum type, 183 GLenum componentType, 184 GLenum textureType, 185 GLenum transposedMatrixType, 186 GLenum boolVectorType, 187 SamplerFormat samplerFormat, 188 int rowCount, 189 int columnCount, 190 int componentCount, 191 size_t componentSize, 192 size_t internalSize, 193 size_t externalSize, 194 bool isSampler, 195 bool isMatrixType, 196 bool isImageType) 197 : type(type), 198 componentType(componentType), 199 textureType(textureType), 200 transposedMatrixType(transposedMatrixType), 201 boolVectorType(boolVectorType), 202 samplerFormat(samplerFormat), 203 rowCount(rowCount), 204 columnCount(columnCount), 205 componentCount(componentCount), 206 componentSize(componentSize), 207 internalSize(internalSize), 208 externalSize(externalSize), 209 isSampler(isSampler), 210 isMatrixType(isMatrixType), 211 isImageType(isImageType) 212 {} 213 214 const UniformTypeInfo &GetUniformTypeInfo(GLenum uniformType); 215 216 const char *GetGenericErrorMessage(GLenum error); 217 218 unsigned int ElementTypeSize(GLenum elementType); 219 220 bool IsMipmapFiltered(GLenum minFilterMode); 221 222 template <typename T> 223 T GetClampedVertexCount(size_t vertexCount) 224 { 225 static constexpr size_t kMax = static_cast<size_t>(std::numeric_limits<T>::max()); 226 return static_cast<T>(vertexCount > kMax ? kMax : vertexCount); 227 } 228 229 enum class PipelineType 230 { 231 GraphicsPipeline = 0, 232 ComputePipeline = 1, 233 }; 234 235 PipelineType GetPipelineType(ShaderType shaderType); 236 237 // For use with KHR_debug. 238 const char *GetDebugMessageSourceString(GLenum source); 239 const char *GetDebugMessageTypeString(GLenum type); 240 const char *GetDebugMessageSeverityString(GLenum severity); 241 242 // For use with EXT_texture_format_sRGB_override and EXT_texture_sRGB_decode 243 // A texture may be forced to decode to a nonlinear colorspace, to a linear colorspace, or to the 244 // default colorspace of its current format. 245 // 246 // Default corresponds to "the texture should use the imageview that corresponds to its format" 247 // Linear corresponds to "the texture has sRGB decoding disabled by extension, and should use a 248 // linear imageview even if it is in a nonlinear format" NonLinear corresponds to "the texture has 249 // sRGB override enabled by extension, and should use a nonlinear imageview even if it is in a 250 // linear format" 251 enum class SrgbOverride 252 { 253 Default = 0, 254 SRGB, 255 Linear 256 }; 257 258 // For use with EXT_sRGB_write_control 259 // A render target may be forced to convert to a linear colorspace, or may be allowed to do whatever 260 // colorspace conversion is appropriate for its format. There is no option to force linear->sRGB, it 261 // can only convert from sRGB->linear 262 enum class SrgbWriteControlMode 263 { 264 Default = 0, 265 Linear = 1 266 }; 267 268 // For use with EXT_YUV_target 269 // A sampler of external YUV textures may either implicitly perform RGB conversion (regular 270 // samplerExternalOES) or skip the conversion and sample raw YUV values (__samplerExternal2DY2Y). 271 enum class YuvSamplingMode 272 { 273 Default = 0, 274 Y2Y = 1 275 }; 276 277 ShaderType GetShaderTypeFromBitfield(size_t singleShaderType); 278 GLbitfield GetBitfieldFromShaderType(ShaderType shaderType); 279 bool ShaderTypeSupportsTransformFeedback(ShaderType shaderType); 280 // Given a set of shader stages, returns the last vertex processing stage. This is the stage that 281 // interfaces the fragment shader. 282 ShaderType GetLastPreFragmentStage(ShaderBitSet shaderTypes); 283 284 } // namespace gl 285 286 namespace egl 287 { 288 static const EGLenum FirstCubeMapTextureTarget = EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR; 289 static const EGLenum LastCubeMapTextureTarget = EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR; 290 bool IsCubeMapTextureTarget(EGLenum target); 291 size_t CubeMapTextureTargetToLayerIndex(EGLenum target); 292 EGLenum LayerIndexToCubeMapTextureTarget(size_t index); 293 bool IsTextureTarget(EGLenum target); 294 bool IsRenderbufferTarget(EGLenum target); 295 bool IsExternalImageTarget(EGLenum target); 296 297 const char *GetGenericErrorMessage(EGLint error); 298 } // namespace egl 299 300 namespace egl_gl 301 { 302 GLuint EGLClientBufferToGLObjectHandle(EGLClientBuffer buffer); 303 } 304 305 namespace gl_egl 306 { 307 EGLenum GLComponentTypeToEGLColorComponentType(GLenum glComponentType); 308 EGLClientBuffer GLObjectHandleToEGLClientBuffer(GLuint handle); 309 } // namespace gl_egl 310 311 namespace angle 312 { 313 bool IsDrawEntryPoint(EntryPoint entryPoint); 314 bool IsDispatchEntryPoint(EntryPoint entryPoint); 315 bool IsClearEntryPoint(EntryPoint entryPoint); 316 bool IsQueryEntryPoint(EntryPoint entryPoint); 317 } // namespace angle 318 319 #if !defined(ANGLE_ENABLE_WINDOWS_UWP) 320 void writeFile(const char *path, const void *data, size_t size); 321 #endif 322 323 #if defined(ANGLE_PLATFORM_WINDOWS) 324 void ScheduleYield(); 325 #endif 326 327 // Get the underlying type. Useful for indexing into arrays with enum values by avoiding the clutter 328 // of the extraneous static_cast<>() calls. 329 // https://stackoverflow.com/a/8357462 330 template <typename E> 331 constexpr typename std::underlying_type<E>::type ToUnderlying(E e) noexcept 332 { 333 return static_cast<typename std::underlying_type<E>::type>(e); 334 } 335 336 #endif // COMMON_UTILITIES_H_