shader_opengl.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*************************************************************************/
  2. /* shader_opengl.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef SHADER_OPENGL_H
  31. #define SHADER_OPENGL_H
  32. #include "drivers/opengl/rasterizer_platforms.h"
  33. #ifdef OPENGL_BACKEND_ENABLED
  34. // This must come first to avoid windows.h mess
  35. #include "platform_config.h"
  36. #ifndef OPENGL_INCLUDE_H
  37. #include <GLES3/gl3.h>
  38. #else
  39. #include OPENGL_INCLUDE_H
  40. #endif
  41. #include "core/math/camera_matrix.h"
  42. #include "core/templates/hash_map.h"
  43. #include "core/templates/map.h"
  44. #include "core/templates/pair.h"
  45. #include "core/variant/variant.h"
  46. #include "servers/rendering/shader_language.h"
  47. #include <stdio.h>
  48. class RasterizerStorageOpenGL;
  49. class ShaderOpenGL {
  50. protected:
  51. struct Enum {
  52. uint64_t mask;
  53. uint64_t shift;
  54. const char *defines[16];
  55. };
  56. struct EnumValue {
  57. uint64_t set_mask;
  58. uint64_t clear_mask;
  59. };
  60. struct AttributePair {
  61. const char *name;
  62. int index;
  63. };
  64. struct UniformPair {
  65. const char *name;
  66. Variant::Type type_hint;
  67. };
  68. struct TexUnitPair {
  69. const char *name;
  70. int index;
  71. };
  72. bool uniforms_dirty;
  73. private:
  74. bool valid = false;
  75. //@TODO Optimize to a fixed set of shader pools and use a LRU
  76. int uniform_count;
  77. int texunit_pair_count;
  78. int conditional_count;
  79. int vertex_code_start;
  80. int fragment_code_start;
  81. int attribute_pair_count;
  82. struct CustomCode {
  83. String vertex;
  84. String vertex_globals;
  85. String fragment;
  86. String fragment_globals;
  87. String light;
  88. uint32_t version;
  89. Vector<StringName> texture_uniforms;
  90. Vector<StringName> custom_uniforms;
  91. Vector<CharString> custom_defines;
  92. Set<uint32_t> versions;
  93. };
  94. struct Version {
  95. GLuint id;
  96. GLuint vert_id;
  97. GLuint frag_id;
  98. GLint *uniform_location;
  99. Vector<GLint> texture_uniform_locations;
  100. Map<StringName, GLint> custom_uniform_locations;
  101. uint32_t code_version;
  102. bool ok;
  103. Version() {
  104. id = 0;
  105. vert_id = 0;
  106. frag_id = 0;
  107. uniform_location = NULL;
  108. code_version = 0;
  109. ok = false;
  110. }
  111. };
  112. Version *version;
  113. union VersionKey {
  114. struct {
  115. uint32_t version;
  116. uint32_t code_version;
  117. };
  118. uint64_t key;
  119. bool operator==(const VersionKey &p_key) const { return key == p_key.key; }
  120. bool operator<(const VersionKey &p_key) const { return key < p_key.key; }
  121. };
  122. struct VersionKeyHash {
  123. static _FORCE_INLINE_ uint32_t hash(const VersionKey &p_key) { return HashMapHasherDefault::hash(p_key.key); }
  124. };
  125. //this should use a way more cachefriendly version..
  126. HashMap<VersionKey, Version, VersionKeyHash> version_map;
  127. HashMap<uint32_t, CustomCode> custom_code_map;
  128. uint32_t last_custom_code;
  129. VersionKey conditional_version;
  130. VersionKey new_conditional_version;
  131. virtual String get_shader_name() const = 0;
  132. const char **conditional_defines;
  133. const char **uniform_names;
  134. const AttributePair *attribute_pairs;
  135. const TexUnitPair *texunit_pairs;
  136. const char *vertex_code;
  137. const char *fragment_code;
  138. CharString fragment_code0;
  139. CharString fragment_code1;
  140. CharString fragment_code2;
  141. CharString fragment_code3;
  142. CharString vertex_code0;
  143. CharString vertex_code1;
  144. CharString vertex_code2;
  145. Vector<CharString> custom_defines;
  146. Version *get_current_version();
  147. static ShaderOpenGL *active;
  148. int max_image_units;
  149. Map<StringName, Pair<ShaderLanguage::DataType, Vector<ShaderLanguage::ConstantNode::Value>>> uniform_values;
  150. protected:
  151. _FORCE_INLINE_ int _get_uniform(int p_which) const;
  152. _FORCE_INLINE_ void _set_conditional(int p_which, bool p_value);
  153. void setup(const char **p_conditional_defines,
  154. int p_conditional_count,
  155. const char **p_uniform_names,
  156. int p_uniform_count,
  157. const AttributePair *p_attribute_pairs,
  158. int p_attribute_count,
  159. const TexUnitPair *p_texunit_pairs,
  160. int p_texunit_pair_count,
  161. const char *p_vertex_code,
  162. const char *p_fragment_code,
  163. int p_vertex_code_start,
  164. int p_fragment_code_start);
  165. ShaderOpenGL();
  166. public:
  167. enum {
  168. CUSTOM_SHADER_DISABLED = 0
  169. };
  170. GLint get_uniform_location(const String &p_name) const;
  171. GLint get_uniform_location(int p_index) const;
  172. static _FORCE_INLINE_ ShaderOpenGL *get_active() { return active; }
  173. bool bind();
  174. void unbind();
  175. inline GLuint get_program() const { return version ? version->id : 0; }
  176. void clear_caches();
  177. uint32_t create_custom_shader();
  178. void set_custom_shader_code(uint32_t p_code_id,
  179. const String &p_vertex,
  180. const String &p_vertex_globals,
  181. const String &p_fragment,
  182. const String &p_light,
  183. const String &p_fragment_globals,
  184. const Vector<StringName> &p_uniforms,
  185. const Vector<StringName> &p_texture_uniforms,
  186. const Vector<CharString> &p_custom_defines);
  187. void set_custom_shader(uint32_t p_code_id);
  188. void free_custom_shader(uint32_t p_code_id);
  189. uint32_t get_version_key() const { return conditional_version.version; }
  190. // this void* is actually a RasterizerStorageOpenGL::Material, but C++ doesn't
  191. // like forward declared nested classes.
  192. void use_material(void *p_material);
  193. _FORCE_INLINE_ uint32_t get_version() const { return new_conditional_version.version; }
  194. _FORCE_INLINE_ bool is_version_valid() const { return version && version->ok; }
  195. virtual void init() = 0;
  196. void finish();
  197. void add_custom_define(const String &p_define) {
  198. custom_defines.push_back(p_define.utf8());
  199. }
  200. void get_custom_defines(Vector<String> *p_defines) {
  201. for (int i = 0; i < custom_defines.size(); i++) {
  202. p_defines->push_back(custom_defines[i].get_data());
  203. }
  204. }
  205. void remove_custom_define(const String &p_define) {
  206. custom_defines.erase(p_define.utf8());
  207. }
  208. virtual ~ShaderOpenGL();
  209. };
  210. // called a lot, made inline
  211. int ShaderOpenGL::_get_uniform(int p_which) const {
  212. ERR_FAIL_INDEX_V(p_which, uniform_count, -1);
  213. ERR_FAIL_COND_V(!version, -1);
  214. return version->uniform_location[p_which];
  215. }
  216. void ShaderOpenGL::_set_conditional(int p_which, bool p_value) {
  217. ERR_FAIL_INDEX(p_which, conditional_count);
  218. if (p_value)
  219. new_conditional_version.version |= (1 << p_which);
  220. else
  221. new_conditional_version.version &= ~(1 << p_which);
  222. }
  223. #endif // OPENGL_BACKEND_ENABLED
  224. #endif // SHADER_OPENGL_H