1 // 2 // Copyright (c) 2013-2014 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 // shadervars.h: 7 // Types to represent GL variables (varyings, uniforms, etc) 8 // 9 10 #ifndef COMMON_SHADERVARIABLE_H_ 11 #define COMMON_SHADERVARIABLE_H_ 12 13 #include <string> 14 #include <vector> 15 #include <algorithm> 16 17 #include <GLES3/gl3.h> 18 #include <GLES2/gl2.h> 19 20 namespace gl 21 { 22 23 // Varying interpolation qualifier, see section 4.3.9 of the ESSL 3.00.4 spec 24 enum InterpolationType 25 { 26 INTERPOLATION_SMOOTH, 27 INTERPOLATION_CENTROID, 28 INTERPOLATION_FLAT 29 }; 30 31 // Uniform block layout qualifier, see section 4.3.8.3 of the ESSL 3.00.4 spec 32 enum BlockLayoutType 33 { 34 BLOCKLAYOUT_STANDARD, 35 BLOCKLAYOUT_PACKED, 36 BLOCKLAYOUT_SHARED 37 }; 38 39 // Base class for all variables defined in shaders, including Varyings, Uniforms, etc 40 struct ShaderVariable 41 { 42 GLenum type; 43 GLenum precision; 44 std::string name; 45 unsigned int arraySize; 46 ShaderVariableShaderVariable47 ShaderVariable(GLenum typeIn, GLenum precisionIn, const char *nameIn, unsigned int arraySizeIn) 48 : type(typeIn), 49 precision(precisionIn), 50 name(nameIn), 51 arraySize(arraySizeIn) 52 {} 53 isArrayShaderVariable54 bool isArray() const { return arraySize > 0; } elementCountShaderVariable55 unsigned int elementCount() const { return std::max(1u, arraySize); } 56 }; 57 58 // Uniform registers (and element indices) are assigned when outputting shader code 59 struct Uniform : public ShaderVariable 60 { 61 unsigned int registerIndex; 62 unsigned int elementIndex; // Offset within a register, for struct members 63 std::vector<Uniform> fields; 64 UniformUniform65 Uniform(GLenum typeIn, GLenum precisionIn, const char *nameIn, unsigned int arraySizeIn, 66 unsigned int registerIndexIn, unsigned int elementIndexIn) 67 : ShaderVariable(typeIn, precisionIn, nameIn, arraySizeIn), 68 registerIndex(registerIndexIn), 69 elementIndex(elementIndexIn) 70 {} 71 isStructUniform72 bool isStruct() const { return !fields.empty(); } 73 }; 74 75 struct Attribute : public ShaderVariable 76 { 77 int location; 78 AttributeAttribute79 Attribute() 80 : ShaderVariable(GL_NONE, GL_NONE, "", 0), 81 location(-1) 82 {} 83 AttributeAttribute84 Attribute(GLenum typeIn, GLenum precisionIn, const char *nameIn, unsigned int arraySizeIn, int locationIn) 85 : ShaderVariable(typeIn, precisionIn, nameIn, arraySizeIn), 86 location(locationIn) 87 {} 88 }; 89 90 struct InterfaceBlockField : public ShaderVariable 91 { 92 bool isRowMajorMatrix; 93 std::vector<InterfaceBlockField> fields; 94 InterfaceBlockFieldInterfaceBlockField95 InterfaceBlockField(GLenum typeIn, GLenum precisionIn, const char *nameIn, unsigned int arraySizeIn, bool isRowMajorMatrix) 96 : ShaderVariable(typeIn, precisionIn, nameIn, arraySizeIn), 97 isRowMajorMatrix(isRowMajorMatrix) 98 {} 99 isStructInterfaceBlockField100 bool isStruct() const { return !fields.empty(); } 101 }; 102 103 struct Varying : public ShaderVariable 104 { 105 InterpolationType interpolation; 106 std::vector<Varying> fields; 107 std::string structName; 108 VaryingVarying109 Varying(GLenum typeIn, GLenum precisionIn, const char *nameIn, unsigned int arraySizeIn, InterpolationType interpolationIn) 110 : ShaderVariable(typeIn, precisionIn, nameIn, arraySizeIn), 111 interpolation(interpolationIn) 112 {} 113 isStructVarying114 bool isStruct() const { return !fields.empty(); } 115 }; 116 117 struct BlockMemberInfo 118 { 119 int offset; 120 int arrayStride; 121 int matrixStride; 122 bool isRowMajorMatrix; 123 getDefaultBlockInfoBlockMemberInfo124 static BlockMemberInfo getDefaultBlockInfo() 125 { 126 return BlockMemberInfo(-1, -1, -1, false); 127 } 128 BlockMemberInfoBlockMemberInfo129 BlockMemberInfo(int offset, int arrayStride, int matrixStride, bool isRowMajorMatrix) 130 : offset(offset), 131 arrayStride(arrayStride), 132 matrixStride(matrixStride), 133 isRowMajorMatrix(isRowMajorMatrix) 134 {} 135 }; 136 137 typedef std::vector<BlockMemberInfo> BlockMemberInfoArray; 138 139 struct InterfaceBlock 140 { 141 std::string name; 142 unsigned int arraySize; 143 size_t dataSize; 144 BlockLayoutType layout; 145 bool isRowMajorLayout; 146 std::vector<InterfaceBlockField> fields; 147 std::vector<BlockMemberInfo> blockInfo; 148 149 unsigned int registerIndex; 150 InterfaceBlockInterfaceBlock151 InterfaceBlock(const char *name, unsigned int arraySize, unsigned int registerIndex) 152 : name(name), 153 arraySize(arraySize), 154 layout(BLOCKLAYOUT_SHARED), 155 registerIndex(registerIndex), 156 isRowMajorLayout(false) 157 {} 158 }; 159 160 } 161 162 #endif // COMMON_SHADERVARIABLE_H_ 163