ShaderVars.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <string>
  12. #include <vector>
  13. #include <algorithm>
  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. struct COMPILER_EXPORT Attribute : public ShaderVariable
  97. {
  98. Attribute();
  99. ~Attribute();
  100. Attribute(const Attribute &other);
  101. Attribute &operator=(const Attribute &other);
  102. bool operator==(const Attribute &other) const;
  103. bool operator!=(const Attribute &other) const
  104. {
  105. return !operator==(other);
  106. }
  107. int location;
  108. };
  109. struct COMPILER_EXPORT InterfaceBlockField : public ShaderVariable
  110. {
  111. InterfaceBlockField();
  112. ~InterfaceBlockField();
  113. InterfaceBlockField(const InterfaceBlockField &other);
  114. InterfaceBlockField &operator=(const InterfaceBlockField &other);
  115. bool operator==(const InterfaceBlockField &other) const;
  116. bool operator!=(const InterfaceBlockField &other) const
  117. {
  118. return !operator==(other);
  119. }
  120. // Decide whether two InterfaceBlock fields are the same at shader
  121. // link time, assuming one from vertex shader and the other from
  122. // fragment shader.
  123. // See GLSL ES Spec 3.00.3, sec 4.3.7.
  124. bool isSameInterfaceBlockFieldAtLinkTime(
  125. const InterfaceBlockField &other) const;
  126. bool isRowMajorLayout;
  127. };
  128. struct COMPILER_EXPORT Varying : public ShaderVariable
  129. {
  130. Varying();
  131. ~Varying();
  132. Varying(const Varying &otherg);
  133. Varying &operator=(const Varying &other);
  134. bool operator==(const Varying &other) const;
  135. bool operator!=(const Varying &other) const
  136. {
  137. return !operator==(other);
  138. }
  139. // Decide whether two varyings are the same at shader link time,
  140. // assuming one from vertex shader and the other from fragment shader.
  141. // Invariance needs to match only in ESSL1. Relevant spec sections:
  142. // GLSL ES 3.00.4, sections 4.6.1 and 4.3.9.
  143. // GLSL ES 1.00.17, section 4.6.4.
  144. bool isSameVaryingAtLinkTime(const Varying &other, int shaderVersion) const;
  145. // Deprecated version of isSameVaryingAtLinkTime, which assumes ESSL1.
  146. bool isSameVaryingAtLinkTime(const Varying &other) const;
  147. InterpolationType interpolation;
  148. bool isInvariant;
  149. };
  150. struct COMPILER_EXPORT InterfaceBlock
  151. {
  152. InterfaceBlock();
  153. ~InterfaceBlock();
  154. InterfaceBlock(const InterfaceBlock &other);
  155. InterfaceBlock &operator=(const InterfaceBlock &other);
  156. std::string name;
  157. std::string mappedName;
  158. std::string instanceName;
  159. unsigned int arraySize;
  160. BlockLayoutType layout;
  161. bool isRowMajorLayout;
  162. bool staticUse;
  163. std::vector<InterfaceBlockField> fields;
  164. };
  165. }
  166. #endif // GLSLANG_SHADERVARS_H_