Program.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include "../Macro.h"
  22. #include "../Types.h"
  23. #include "GraphicsHandle.h"
  24. #include <string>
  25. #include <vector>
  26. RENDERER_BEGIN
  27. /**
  28. * @addtogroup gfx
  29. * @{
  30. */
  31. class DeviceGraphics;
  32. /**
  33. * Program class manages the internal GL shader program.
  34. */
  35. class Program final: public GraphicsHandle
  36. {
  37. public:
  38. /**
  39. * Describes vertex attribute informations used in the program
  40. * @struct Attribute
  41. */
  42. struct Attribute
  43. {
  44. /**
  45. * Attribute name
  46. */
  47. std::string name;
  48. size_t hashName;
  49. /**
  50. * Number of components per attribute
  51. */
  52. GLsizei size;
  53. /**
  54. * Attribute location
  55. */
  56. GLuint location;
  57. /**
  58. * Attribute type
  59. */
  60. GLenum type;
  61. };
  62. /**
  63. * Describes uniform informations used in the program
  64. * @struct Uniform
  65. */
  66. struct Uniform
  67. {
  68. /**
  69. * Uniform name
  70. */
  71. std::string name;
  72. /**
  73. * Uniform hash name
  74. */
  75. size_t hashName;
  76. /**
  77. * The length of the array for uniforms declared as arrays, default value is 1
  78. */
  79. GLsizei size;
  80. /**
  81. * Uniform location
  82. */
  83. GLint location;
  84. /**
  85. * Uniform type
  86. */
  87. GLenum type;
  88. /**
  89. * Sets the uniform value
  90. */
  91. void setUniform(const void* value, UniformElementType elementType, size_t uniformCount) const;
  92. /**
  93. * Sets the callback which will be called when uniform updated
  94. */
  95. using SetUniformCallback = void (*)(GLint, GLsizei, const void*, UniformElementType); // location, count, value, elementType
  96. private:
  97. SetUniformCallback _callback;
  98. friend class Program;
  99. };
  100. /**
  101. * Creates a Program with device and shader sources
  102. */
  103. RENDERER_DEFINE_CREATE_METHOD_3(Program, init, DeviceGraphics*, const char*, const char*)
  104. Program();
  105. virtual ~Program();
  106. /**
  107. * Initializes a Program with device and program sources
  108. * @param[in] device DeviceGraphics pointer
  109. * @param[in] vertSource Vertex shader program
  110. * @param[in] fragSource Fragment shader program
  111. */
  112. bool init(DeviceGraphics* device, const char* vertSource, const char* fragSource);
  113. /**
  114. * Gets the id of program
  115. */
  116. inline uint32_t getID() const { return _id; }
  117. /**
  118. * Gets the attibutes used in the program
  119. */
  120. inline const std::vector<Attribute>& getAttributes() const { return _attributes; }
  121. /**
  122. * Gets the uniforms used in the program
  123. */
  124. inline const std::vector<Uniform>& getUniforms() const { return _uniforms; }
  125. /**
  126. * Indicates whether the program is successfully linked
  127. */
  128. inline bool isLinked() const { return _linked; }
  129. /**
  130. * Link the program with its shader sources
  131. */
  132. void link();
  133. inline size_t getHash() const { return _hash; }
  134. inline void setHash(size_t hash) { _hash = hash; }
  135. private:
  136. DeviceGraphics* _device;
  137. std::vector<Attribute> _attributes;
  138. std::vector<Uniform> _uniforms;
  139. std::string _vertSource;
  140. std::string _fragSource;
  141. uint32_t _id;
  142. bool _linked;
  143. size_t _hash = 0;
  144. };
  145. // end of gfx group
  146. /// @}
  147. RENDERER_END