ShaderProgram.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #ifndef ANKI_GL_SHADER_PROGRAM_H
  2. #define ANKI_GL_SHADER_PROGRAM_H
  3. #include "anki/util/ConstCharPtrHashMap.h"
  4. #include "anki/util/Assert.h"
  5. #include "anki/util/Flags.h"
  6. #include "anki/math/Forward.h"
  7. #include "anki/util/NonCopyable.h"
  8. #include "anki/gl/Ogl.h"
  9. #include "anki/util/Vector.h"
  10. #include "anki/util/StdTypes.h"
  11. #include <string>
  12. #include <memory>
  13. namespace anki {
  14. class ShaderProgram;
  15. class Texture;
  16. /// @addtogroup gl
  17. /// @{
  18. /// Shader program variable. The type is attribute or uniform
  19. class ShaderProgramVariable: public NonCopyable
  20. {
  21. public:
  22. /// Shader var types
  23. enum ShaderProgramVariableType
  24. {
  25. SPVT_ATTRIBUTE,
  26. SPVT_UNIFORM
  27. };
  28. ShaderProgramVariable(
  29. GLint loc,
  30. const char* name,
  31. GLenum glDataType,
  32. size_t size,
  33. ShaderProgramVariableType type,
  34. const ShaderProgram* fatherSProg);
  35. virtual ~ShaderProgramVariable()
  36. {}
  37. /// @name Accessors
  38. /// @{
  39. const ShaderProgram& getFatherShaderProgram() const
  40. {
  41. return *fatherSProg;
  42. }
  43. GLint getLocation() const
  44. {
  45. return loc;
  46. }
  47. const std::string& getName() const
  48. {
  49. return name;
  50. }
  51. GLenum getGlDataType() const
  52. {
  53. return glDataType;
  54. }
  55. ShaderProgramVariableType getType() const
  56. {
  57. return type;
  58. }
  59. size_t getSize() const
  60. {
  61. return size;
  62. }
  63. /// @}
  64. private:
  65. GLint loc; ///< GL location
  66. std::string name; ///< The name inside the shader program
  67. /// GL_FLOAT, GL_FLOAT_VEC2 etc. See
  68. /// http://www.opengl.org/sdk/docs/man/xhtml/glGetActiveUniform.xml
  69. GLenum glDataType;
  70. size_t size; ///< Its 1 if it is a single or >1 if it is an array
  71. ShaderProgramVariableType type;
  72. /// We need the ShaderProg of this variable mainly for sanity checks
  73. const ShaderProgram* fatherSProg;
  74. };
  75. /// Uniform shader variable
  76. class ShaderProgramUniformVariable: public ShaderProgramVariable,
  77. public Flags<uint32_t>
  78. {
  79. public:
  80. enum ShaderProgramUniformVariableFlag
  81. {
  82. SPUVF_NONE = 0,
  83. SPUVF_DIRTY = (1 << 0) ///< This means that a setter was called
  84. };
  85. ShaderProgramUniformVariable(
  86. int loc,
  87. const char* name,
  88. GLenum glDataType,
  89. size_t size,
  90. const ShaderProgram* fatherSProg)
  91. : ShaderProgramVariable(loc, name, glDataType, size, SPVT_UNIFORM,
  92. fatherSProg)
  93. {
  94. enableFlag(SPUVF_DIRTY);
  95. }
  96. /// @name Set the var
  97. /// @{
  98. void set(const float x) const;
  99. void set(const Vec2& x) const;
  100. void set(const Vec3& x) const
  101. {
  102. set(&x, 1);
  103. }
  104. void set(const Vec4& x) const
  105. {
  106. set(&x, 1);
  107. }
  108. void set(const Mat3& x) const
  109. {
  110. set(&x, 1);
  111. }
  112. void set(const Mat4& x) const
  113. {
  114. set(&x, 1);
  115. }
  116. void set(const Texture& tex) const;
  117. void set(const float x[], uint size) const;
  118. void set(const Vec2 x[], uint size) const;
  119. void set(const Vec3 x[], uint size) const;
  120. void set(const Vec4 x[], uint size) const;
  121. void set(const Mat3 x[], uint size) const;
  122. void set(const Mat4 x[], uint size) const;
  123. /// @tparam Container It could be something like array<float, X> or
  124. /// vector<Vec2> etc
  125. template<typename Container>
  126. void setContainer(const Container& c) const
  127. {
  128. set(&c[0], c.size());
  129. }
  130. /// @}
  131. private:
  132. GLuint index;
  133. GLint offset; ///< Offset inside the uniform block. -1 if it's inside the
  134. ///< default uniform block
  135. /// Standard set uniform checks
  136. /// - Check if initialized
  137. /// - if the current shader program is the var's shader program
  138. /// - if the GL driver gives the same location as the one the var has
  139. void doCommonSetCode() const;
  140. };
  141. /// Attribute shader program variable
  142. class ShaderProgramAttributeVariable: public ShaderProgramVariable
  143. {
  144. public:
  145. ShaderProgramAttributeVariable(
  146. int loc_, const char* name_, GLenum glDataType_, size_t size,
  147. const ShaderProgram* fatherSProg_)
  148. : ShaderProgramVariable(loc_, name_, glDataType_, size, SPVT_ATTRIBUTE,
  149. fatherSProg_)
  150. {}
  151. };
  152. /// Uniform shader block
  153. class ShaderProgramUniformBlock
  154. {
  155. public:
  156. ShaderProgramUniformBlock()
  157. {}
  158. ShaderProgramUniformBlock(const ShaderProgramUniformBlock& b)
  159. {
  160. operator=(b);
  161. }
  162. ~ShaderProgramUniformBlock()
  163. {}
  164. /// @name Accessors
  165. /// @{
  166. GLuint getIndex() const
  167. {
  168. return index;
  169. }
  170. uint32_t getSize() const
  171. {
  172. return size;
  173. }
  174. const std::string& getName() const
  175. {
  176. return name;
  177. }
  178. GLuint getBindingPoint() const
  179. {
  180. return bindingPoint;
  181. }
  182. void setBindingPoint(GLuint bp) const
  183. {
  184. // Don't try any opts with existing binding point. Binding points
  185. // should break
  186. glUniformBlockBinding(progId, index, bp);
  187. bindingPoint = bp;
  188. }
  189. /// @}
  190. ShaderProgramUniformBlock& operator=(const ShaderProgramUniformBlock& b);
  191. void init(ShaderProgram& prog, const char* blockName);
  192. private:
  193. Vector<ShaderProgramUniformVariable*> uniforms;
  194. GLuint index = GL_INVALID_INDEX;
  195. U32 size = 0; ///< In bytes
  196. std::string name;
  197. /// Ask the program to get you the binding point
  198. mutable GLuint bindingPoint;
  199. GLuint progId;
  200. };
  201. /// Shader program object
  202. class ShaderProgram: public NonCopyable
  203. {
  204. public:
  205. typedef Vector<std::shared_ptr<ShaderProgramVariable>>
  206. VariablesContainer;
  207. typedef Vector<ShaderProgramUniformVariable*>
  208. UniformVariablesContainer;
  209. typedef Vector<ShaderProgramAttributeVariable*>
  210. AttributeVariablesContainer;
  211. typedef Vector<ShaderProgramUniformBlock>
  212. UniformBlocksContainer;
  213. /// @name Constructors/Destructor
  214. /// @{
  215. ShaderProgram()
  216. {
  217. init();
  218. }
  219. ShaderProgram(const char* vertSource, const char* tcSource,
  220. const char* teSource, const char* geomSource, const char* fragSource,
  221. const char* transformFeedbackVaryings[])
  222. {
  223. init();
  224. create(vertSource, tcSource, teSource, geomSource, fragSource,
  225. transformFeedbackVaryings);
  226. }
  227. ~ShaderProgram()
  228. {
  229. if(isCreated())
  230. {
  231. destroy();
  232. }
  233. }
  234. /// @}
  235. /// @name Accessors
  236. /// @{
  237. GLuint getGlId() const
  238. {
  239. ANKI_ASSERT(isCreated());
  240. return glId;
  241. }
  242. /// Get all variables container
  243. const VariablesContainer& getVariables() const
  244. {
  245. return vars;
  246. }
  247. const UniformVariablesContainer& getUniformVariables() const
  248. {
  249. return unis;
  250. }
  251. const AttributeVariablesContainer& getAttributeVariables() const
  252. {
  253. return attribs;
  254. }
  255. /// @}
  256. /// Create the program
  257. /// @param vertSource Vertex shader source
  258. /// @param tcSource Tessellation control shader source. Can be nullptr
  259. /// @param teSource Tessellation evaluation shader source. Can be nullptr
  260. /// @param geomSource Geometry shader source. Can be nullptr
  261. /// @param fragSource Fragment shader source. Can be nullptr
  262. /// @param transformFeedbackVaryings An array of varyings names. Eg
  263. /// {"var0", "var1", nullptr} or {nullptr}
  264. void create(const char* vertSource, const char* tcSource,
  265. const char* teSource, const char* geomSource, const char* fragSource,
  266. const char* transformFeedbackVaryings[]);
  267. /// Bind the shader program
  268. void bind() const
  269. {
  270. ANKI_ASSERT(isCreated());
  271. if(current != this)
  272. {
  273. glUseProgram(glId);
  274. current = this;
  275. }
  276. }
  277. // Unbinds only @a this if its binded
  278. void unbind() const
  279. {
  280. ANKI_ASSERT(isCreated());
  281. if(current == this)
  282. {
  283. glUseProgram(0);
  284. current = nullptr;
  285. }
  286. }
  287. /// @name Variable finders
  288. /// Used to find and return the variable. They return nullptr if the
  289. /// variable is not found
  290. /// @{
  291. const ShaderProgramVariable* tryFindVariable(const char* varName) const;
  292. const ShaderProgramVariable& findVariable(const char* varName) const;
  293. const ShaderProgramUniformVariable* tryFindUniformVariable(
  294. const char* varName) const;
  295. const ShaderProgramUniformVariable& findUniformVariable(
  296. const char* varName) const;
  297. const ShaderProgramAttributeVariable* tryFindAttributeVariable(
  298. const char* varName) const;
  299. const ShaderProgramAttributeVariable& findAttributeVariable(
  300. const char* varName) const;
  301. const ShaderProgramUniformBlock* tryFindUniformBlock(
  302. const char* name) const;
  303. const ShaderProgramUniformBlock& findUniformBlock(const char* name) const;
  304. /// @}
  305. /// For all uniforms set the SPUVF_DIRTY bit to 0
  306. void cleanAllUniformsDirtyFlags();
  307. static GLuint getCurrentProgramGlId()
  308. {
  309. int i;
  310. glGetIntegerv(GL_CURRENT_PROGRAM, &i);
  311. return i;
  312. }
  313. /// For debugging
  314. friend std::ostream& operator<<(std::ostream& s,
  315. const ShaderProgram& x);
  316. private:
  317. typedef ConstCharPtrHashMap<ShaderProgramVariable*>::Type
  318. NameToVarHashMap;
  319. typedef ConstCharPtrHashMap<ShaderProgramUniformVariable*>::Type
  320. NameToUniVarHashMap;
  321. typedef ConstCharPtrHashMap<ShaderProgramAttributeVariable*>::Type
  322. NameToAttribVarHashMap;
  323. typedef ConstCharPtrHashMap<ShaderProgramUniformBlock*>::Type
  324. NameToUniformBlockHashMap;
  325. static thread_local const ShaderProgram* current;
  326. /// Shader source that is used in ALL shader programs
  327. static const char* stdSourceCode;
  328. GLuint glId; ///< The OpenGL ID of the shader program
  329. GLuint vertShaderGlId; ///< Vertex shader OpenGL id
  330. GLuint tcShaderGlId; ///< Tessellation control shader OpenGL id
  331. GLuint teShaderGlId; ///< Tessellation eval shader OpenGL id
  332. GLuint geomShaderGlId; ///< Geometry shader OpenGL id
  333. GLuint fragShaderGlId; ///< Fragment shader OpenGL id
  334. /// @name Containers
  335. /// @{
  336. VariablesContainer vars; ///< All the vars. Does garbage collection
  337. UniformVariablesContainer unis;
  338. AttributeVariablesContainer attribs;
  339. NameToVarHashMap nameToVar; ///< Variable searching
  340. NameToUniVarHashMap nameToUniVar; ///< Uniform searching
  341. NameToAttribVarHashMap nameToAttribVar; ///< Attribute searching
  342. UniformBlocksContainer blocks;
  343. NameToUniformBlockHashMap nameToBlock;
  344. /// @}
  345. /// Query the driver to get the vars. After the linking of the shader
  346. /// prog is done gather all the vars in custom containers
  347. void getUniAndAttribVars();
  348. /// Get info about the uniform blocks
  349. void initUniformBlocks();
  350. /// Create and compile shader
  351. /// @return The shader's OpenGL id
  352. /// @exception Exception
  353. static GLuint createAndCompileShader(const char* sourceCode,
  354. const char* preproc, GLenum type);
  355. /// Link the shader program
  356. /// @exception Exception
  357. void link() const;
  358. /// Returns true if the class points to a valid GL ID
  359. bool isCreated() const
  360. {
  361. return glId != 0;
  362. }
  363. /// Common construction code
  364. void init()
  365. {
  366. glId = vertShaderGlId = tcShaderGlId = teShaderGlId =
  367. geomShaderGlId = fragShaderGlId = 0;
  368. }
  369. void destroy();
  370. };
  371. /// @}
  372. } // end namespace anki
  373. #endif