ShaderVars.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 _COMPILER_INTERFACE_VARIABLES_
  10. #define _COMPILER_INTERFACE_VARIABLES_
  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. // Uniform block layout qualifier, see section 4.3.8.3 of the ESSL 3.00.4 spec
  26. enum BlockLayoutType
  27. {
  28. BLOCKLAYOUT_STANDARD,
  29. BLOCKLAYOUT_PACKED,
  30. BLOCKLAYOUT_SHARED
  31. };
  32. // Base class for all variables defined in shaders, including Varyings, Uniforms, etc
  33. // Note: we must override the copy constructor and assignment operator so we can
  34. // work around excessive GCC binary bloating:
  35. // See https://code.google.com/p/angleproject/issues/detail?id=697
  36. struct COMPILER_EXPORT ShaderVariable
  37. {
  38. ShaderVariable();
  39. ShaderVariable(GLenum typeIn, unsigned int arraySizeIn);
  40. ~ShaderVariable();
  41. ShaderVariable(const ShaderVariable &other);
  42. ShaderVariable &operator=(const ShaderVariable &other);
  43. bool isArray() const { return arraySize > 0; }
  44. unsigned int elementCount() const { return std::max(1u, arraySize); }
  45. bool isStruct() const { return !fields.empty(); }
  46. // All of the shader's variables are described using nested data
  47. // structures. This is needed in order to disambiguate similar looking
  48. // types, such as two structs containing the same fields, but in
  49. // different orders. "findInfoByMappedName" provides an easy query for
  50. // users to dive into the data structure and fetch the unique variable
  51. // instance corresponding to a dereferencing chain of the top-level
  52. // variable.
  53. // Given a mapped name like 'a[0].b.c[0]', return the ShaderVariable
  54. // that defines 'c' in |leafVar|, and the original name 'A[0].B.C[0]'
  55. // in |originalName|, based on the assumption that |this| defines 'a'.
  56. // If no match is found, return false.
  57. bool findInfoByMappedName(const std::string &mappedFullName,
  58. const ShaderVariable **leafVar,
  59. std::string* originalFullName) const;
  60. GLenum type;
  61. GLenum precision;
  62. std::string name;
  63. std::string mappedName;
  64. unsigned int arraySize;
  65. bool staticUse;
  66. std::vector<ShaderVariable> fields;
  67. std::string structName;
  68. protected:
  69. bool isSameVariableAtLinkTime(const ShaderVariable &other,
  70. bool matchPrecision) const;
  71. bool operator==(const ShaderVariable &other) const;
  72. bool operator!=(const ShaderVariable &other) const
  73. {
  74. return !operator==(other);
  75. }
  76. };
  77. struct COMPILER_EXPORT Uniform : public ShaderVariable
  78. {
  79. Uniform();
  80. ~Uniform();
  81. Uniform(const Uniform &other);
  82. Uniform &operator=(const Uniform &other);
  83. bool operator==(const Uniform &other) const;
  84. bool operator!=(const Uniform &other) const
  85. {
  86. return !operator==(other);
  87. }
  88. // Decide whether two uniforms are the same at shader link time,
  89. // assuming one from vertex shader and the other from fragment shader.
  90. // See GLSL ES Spec 3.00.3, sec 4.3.5.
  91. bool isSameUniformAtLinkTime(const Uniform &other) const;
  92. };
  93. struct COMPILER_EXPORT Attribute : public ShaderVariable
  94. {
  95. Attribute();
  96. ~Attribute();
  97. Attribute(const Attribute &other);
  98. Attribute &operator=(const Attribute &other);
  99. bool operator==(const Attribute &other) const;
  100. bool operator!=(const Attribute &other) const
  101. {
  102. return !operator==(other);
  103. }
  104. int location;
  105. };
  106. struct COMPILER_EXPORT InterfaceBlockField : public ShaderVariable
  107. {
  108. InterfaceBlockField();
  109. ~InterfaceBlockField();
  110. InterfaceBlockField(const InterfaceBlockField &other);
  111. InterfaceBlockField &operator=(const InterfaceBlockField &other);
  112. bool operator==(const InterfaceBlockField &other) const;
  113. bool operator!=(const InterfaceBlockField &other) const
  114. {
  115. return !operator==(other);
  116. }
  117. // Decide whether two InterfaceBlock fields are the same at shader
  118. // link time, assuming one from vertex shader and the other from
  119. // fragment shader.
  120. // See GLSL ES Spec 3.00.3, sec 4.3.7.
  121. bool isSameInterfaceBlockFieldAtLinkTime(
  122. const InterfaceBlockField &other) const;
  123. bool isRowMajorLayout;
  124. };
  125. struct COMPILER_EXPORT Varying : public ShaderVariable
  126. {
  127. Varying();
  128. ~Varying();
  129. Varying(const Varying &otherg);
  130. Varying &operator=(const Varying &other);
  131. bool operator==(const Varying &other) const;
  132. bool operator!=(const Varying &other) const
  133. {
  134. return !operator==(other);
  135. }
  136. // Decide whether two varyings are the same at shader link time,
  137. // assuming one from vertex shader and the other from fragment shader.
  138. // See GLSL ES Spec 3.00.3, sec 4.3.9.
  139. bool isSameVaryingAtLinkTime(const Varying &other) const;
  140. InterpolationType interpolation;
  141. bool isInvariant;
  142. };
  143. struct COMPILER_EXPORT InterfaceBlock
  144. {
  145. InterfaceBlock();
  146. ~InterfaceBlock();
  147. InterfaceBlock(const InterfaceBlock &other);
  148. InterfaceBlock &operator=(const InterfaceBlock &other);
  149. std::string name;
  150. std::string mappedName;
  151. std::string instanceName;
  152. unsigned int arraySize;
  153. BlockLayoutType layout;
  154. bool isRowMajorLayout;
  155. bool staticUse;
  156. std::vector<InterfaceBlockField> fields;
  157. };
  158. }
  159. #endif // _COMPILER_INTERFACE_VARIABLES_