ShaderVars.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #ifndef GLSLANG_SHADERVARS_H_
  10. #define GLSLANG_SHADERVARS_H_
  11. #include <algorithm>
  12. #include <string>
  13. #include <vector>
  14. // Assume ShaderLang.h is included before ShaderVars.h, for sh::GLenum
  15. // Note: make sure to increment ANGLE_SH_VERSION when changing ShaderVars.h
  16. namespace sh
  17. {
  18. // Varying interpolation qualifier, see section 4.3.9 of the ESSL 3.00.4 spec
  19. enum InterpolationType
  20. {
  21. INTERPOLATION_SMOOTH,
  22. INTERPOLATION_CENTROID,
  23. INTERPOLATION_FLAT
  24. };
  25. // Validate link & SSO consistency of interpolation qualifiers
  26. COMPILER_EXPORT bool InterpolationTypesMatch(InterpolationType a, InterpolationType b);
  27. // Uniform block layout qualifier, see section 4.3.8.3 of the ESSL 3.00.4 spec
  28. enum BlockLayoutType
  29. {
  30. BLOCKLAYOUT_STANDARD,
  31. BLOCKLAYOUT_PACKED,
  32. BLOCKLAYOUT_SHARED
  33. };
  34. // Base class for all variables defined in shaders, including Varyings, Uniforms, etc
  35. // Note: we must override the copy constructor and assignment operator so we can
  36. // work around excessive GCC binary bloating:
  37. // See https://code.google.com/p/angleproject/issues/detail?id=697
  38. struct COMPILER_EXPORT ShaderVariable
  39. {
  40. ShaderVariable();
  41. ShaderVariable(GLenum typeIn, unsigned int arraySizeIn);
  42. ~ShaderVariable();
  43. ShaderVariable(const ShaderVariable &other);
  44. ShaderVariable &operator=(const ShaderVariable &other);
  45. bool isArray() const { return arraySize > 0; }
  46. unsigned int elementCount() const { return std::max(1u, arraySize); }
  47. bool isStruct() const { return !fields.empty(); }
  48. // All of the shader's variables are described using nested data
  49. // structures. This is needed in order to disambiguate similar looking
  50. // types, such as two structs containing the same fields, but in
  51. // different orders. "findInfoByMappedName" provides an easy query for
  52. // users to dive into the data structure and fetch the unique variable
  53. // instance corresponding to a dereferencing chain of the top-level
  54. // variable.
  55. // Given a mapped name like 'a[0].b.c[0]', return the ShaderVariable
  56. // that defines 'c' in |leafVar|, and the original name 'A[0].B.C[0]'
  57. // in |originalName|, based on the assumption that |this| defines 'a'.
  58. // If no match is found, return false.
  59. bool findInfoByMappedName(const std::string &mappedFullName,
  60. const ShaderVariable **leafVar,
  61. std::string* originalFullName) const;
  62. bool isBuiltIn() const { return name.compare(0, 3, "gl_") == 0; }
  63. GLenum type;
  64. GLenum precision;
  65. std::string name;
  66. std::string mappedName;
  67. unsigned int arraySize;
  68. bool staticUse;
  69. std::vector<ShaderVariable> fields;
  70. std::string structName;
  71. protected:
  72. bool isSameVariableAtLinkTime(const ShaderVariable &other,
  73. bool matchPrecision) const;
  74. bool operator==(const ShaderVariable &other) const;
  75. bool operator!=(const ShaderVariable &other) const
  76. {
  77. return !operator==(other);
  78. }
  79. };
  80. struct COMPILER_EXPORT Uniform : public ShaderVariable
  81. {
  82. Uniform();
  83. ~Uniform();
  84. Uniform(const Uniform &other);
  85. Uniform &operator=(const Uniform &other);
  86. bool operator==(const Uniform &other) const;
  87. bool operator!=(const Uniform &other) const
  88. {
  89. return !operator==(other);
  90. }
  91. // Decide whether two uniforms are the same at shader link time,
  92. // assuming one from vertex shader and the other from fragment shader.
  93. // See GLSL ES Spec 3.00.3, sec 4.3.5.
  94. bool isSameUniformAtLinkTime(const Uniform &other) const;
  95. };
  96. // An interface variable is a variable which passes data between the GL data structures and the
  97. // shader execution: either vertex shader inputs or fragment shader outputs. These variables can
  98. // have integer locations to pass back to the GL API.
  99. struct COMPILER_EXPORT InterfaceVariable : public ShaderVariable
  100. {
  101. InterfaceVariable();
  102. ~InterfaceVariable();
  103. InterfaceVariable(const InterfaceVariable &other);
  104. InterfaceVariable &operator=(const InterfaceVariable &other);
  105. bool operator==(const InterfaceVariable &other) const;
  106. bool operator!=(const InterfaceVariable &other) const { return !operator==(other); }
  107. int location;
  108. };
  109. struct COMPILER_EXPORT Attribute : public InterfaceVariable
  110. {
  111. Attribute();
  112. ~Attribute();
  113. Attribute(const Attribute &other);
  114. Attribute &operator=(const Attribute &other);
  115. bool operator==(const Attribute &other) const;
  116. bool operator!=(const Attribute &other) const { return !operator==(other); }
  117. };
  118. struct COMPILER_EXPORT OutputVariable : public InterfaceVariable
  119. {
  120. OutputVariable();
  121. ~OutputVariable();
  122. OutputVariable(const OutputVariable &other);
  123. OutputVariable &operator=(const OutputVariable &other);
  124. bool operator==(const OutputVariable &other) const;
  125. bool operator!=(const OutputVariable &other) const { return !operator==(other); }
  126. };
  127. struct COMPILER_EXPORT InterfaceBlockField : public ShaderVariable
  128. {
  129. InterfaceBlockField();
  130. ~InterfaceBlockField();
  131. InterfaceBlockField(const InterfaceBlockField &other);
  132. InterfaceBlockField &operator=(const InterfaceBlockField &other);
  133. bool operator==(const InterfaceBlockField &other) const;
  134. bool operator!=(const InterfaceBlockField &other) const
  135. {
  136. return !operator==(other);
  137. }
  138. // Decide whether two InterfaceBlock fields are the same at shader
  139. // link time, assuming one from vertex shader and the other from
  140. // fragment shader.
  141. // See GLSL ES Spec 3.00.3, sec 4.3.7.
  142. bool isSameInterfaceBlockFieldAtLinkTime(
  143. const InterfaceBlockField &other) const;
  144. bool isRowMajorLayout;
  145. };
  146. struct COMPILER_EXPORT Varying : public ShaderVariable
  147. {
  148. Varying();
  149. ~Varying();
  150. Varying(const Varying &otherg);
  151. Varying &operator=(const Varying &other);
  152. bool operator==(const Varying &other) const;
  153. bool operator!=(const Varying &other) const
  154. {
  155. return !operator==(other);
  156. }
  157. // Decide whether two varyings are the same at shader link time,
  158. // assuming one from vertex shader and the other from fragment shader.
  159. // Invariance needs to match only in ESSL1. Relevant spec sections:
  160. // GLSL ES 3.00.4, sections 4.6.1 and 4.3.9.
  161. // GLSL ES 1.00.17, section 4.6.4.
  162. bool isSameVaryingAtLinkTime(const Varying &other, int shaderVersion) const;
  163. // Deprecated version of isSameVaryingAtLinkTime, which assumes ESSL1.
  164. bool isSameVaryingAtLinkTime(const Varying &other) const;
  165. InterpolationType interpolation;
  166. bool isInvariant;
  167. };
  168. struct COMPILER_EXPORT InterfaceBlock
  169. {
  170. InterfaceBlock();
  171. ~InterfaceBlock();
  172. InterfaceBlock(const InterfaceBlock &other);
  173. InterfaceBlock &operator=(const InterfaceBlock &other);
  174. // Fields from blocks with non-empty instance names are prefixed with the block name.
  175. std::string fieldPrefix() const;
  176. // Decide whether two interface blocks are the same at shader link time.
  177. bool isSameInterfaceBlockAtLinkTime(const InterfaceBlock &other) const;
  178. std::string name;
  179. std::string mappedName;
  180. std::string instanceName;
  181. unsigned int arraySize;
  182. BlockLayoutType layout;
  183. bool isRowMajorLayout;
  184. bool staticUse;
  185. std::vector<InterfaceBlockField> fields;
  186. };
  187. struct COMPILER_EXPORT WorkGroupSize
  188. {
  189. void fill(int fillValue);
  190. void setLocalSize(int localSizeX, int localSizeY, int localSizeZ);
  191. int &operator[](size_t index);
  192. int operator[](size_t index) const;
  193. size_t size() const;
  194. // Checks whether two work group size declarations match.
  195. // Two work group size declarations are the same if the explicitly specified elements are the
  196. // same or if one of them is specified as one and the other one is not specified
  197. bool isWorkGroupSizeMatching(const WorkGroupSize &right) const;
  198. // Checks whether any of the values are set.
  199. bool isAnyValueSet() const;
  200. // Checks whether all of the values are set.
  201. bool isDeclared() const;
  202. // Checks whether either all of the values are set, or none of them are.
  203. bool isLocalSizeValid() const;
  204. int localSizeQualifiers[3];
  205. };
  206. } // namespace sh
  207. #endif // GLSLANG_SHADERVARS_H_