shader_gles2.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**************************************************************************/
  2. /* shader_gles2.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_GLES2_H
  31. #define SHADER_GLES2_H
  32. // This must come first to avoid windows.h mess
  33. #include "platform_config.h"
  34. #ifndef GLES2_INCLUDE_H
  35. #include <GLES2/gl2.h>
  36. #else
  37. #include GLES2_INCLUDE_H
  38. #endif
  39. #include "core/hash_map.h"
  40. #include "core/map.h"
  41. #include "core/math/camera_matrix.h"
  42. #include "core/pair.h"
  43. #include "core/variant.h"
  44. #include "servers/visual/shader_language.h"
  45. #include <stdio.h>
  46. class RasterizerStorageGLES2;
  47. class ShaderGLES2 {
  48. protected:
  49. struct Enum {
  50. uint64_t mask;
  51. uint64_t shift;
  52. const char *defines[16];
  53. };
  54. struct EnumValue {
  55. uint64_t set_mask;
  56. uint64_t clear_mask;
  57. };
  58. struct AttributePair {
  59. const char *name;
  60. int index;
  61. };
  62. struct UniformPair {
  63. const char *name;
  64. Variant::Type type_hint;
  65. };
  66. struct TexUnitPair {
  67. const char *name;
  68. int index;
  69. };
  70. bool uniforms_dirty;
  71. private:
  72. //@TODO Optimize to a fixed set of shader pools and use a LRU
  73. int uniform_count;
  74. int texunit_pair_count;
  75. int conditional_count;
  76. int vertex_code_start;
  77. int fragment_code_start;
  78. int attribute_pair_count;
  79. struct CustomCode {
  80. String vertex;
  81. String vertex_globals;
  82. String fragment;
  83. String fragment_globals;
  84. String light;
  85. uint32_t version;
  86. Vector<StringName> texture_uniforms;
  87. Vector<StringName> custom_uniforms;
  88. Vector<CharString> custom_defines;
  89. Set<uint64_t> versions;
  90. };
  91. struct Version {
  92. GLuint id;
  93. GLuint vert_id;
  94. GLuint frag_id;
  95. GLint *uniform_location;
  96. Vector<GLint> texture_uniform_locations;
  97. Map<StringName, GLint> custom_uniform_locations;
  98. uint32_t code_version;
  99. bool ok;
  100. Version() {
  101. id = 0;
  102. vert_id = 0;
  103. frag_id = 0;
  104. uniform_location = nullptr;
  105. code_version = 0;
  106. ok = false;
  107. }
  108. };
  109. Version *version;
  110. union VersionKey {
  111. struct {
  112. uint64_t version;
  113. uint32_t code_version;
  114. };
  115. unsigned char key[12];
  116. bool operator==(const VersionKey &p_key) const { return version == p_key.version && code_version == p_key.code_version; }
  117. bool operator<(const VersionKey &p_key) const { return version < p_key.version || (version == p_key.version && code_version < p_key.code_version); }
  118. };
  119. struct VersionKeyHash {
  120. static _FORCE_INLINE_ uint32_t hash(const VersionKey &p_key) { return hash_djb2_buffer(p_key.key, sizeof(p_key.key)); }
  121. };
  122. //this should use a way more cachefriendly version..
  123. HashMap<VersionKey, Version, VersionKeyHash> version_map;
  124. HashMap<uint32_t, CustomCode> custom_code_map;
  125. uint32_t last_custom_code;
  126. VersionKey conditional_version;
  127. VersionKey new_conditional_version;
  128. virtual String get_shader_name() const = 0;
  129. const char **conditional_defines;
  130. const char **uniform_names;
  131. const AttributePair *attribute_pairs;
  132. const TexUnitPair *texunit_pairs;
  133. const char *vertex_code;
  134. const char *fragment_code;
  135. CharString fragment_code0;
  136. CharString fragment_code1;
  137. CharString fragment_code2;
  138. CharString fragment_code3;
  139. CharString vertex_code0;
  140. CharString vertex_code1;
  141. CharString vertex_code2;
  142. Vector<CharString> custom_defines;
  143. Version *get_current_version();
  144. static ShaderGLES2 *active;
  145. int max_image_units;
  146. Map<StringName, Pair<ShaderLanguage::DataType, Vector<ShaderLanguage::ConstantNode::Value>>> uniform_values;
  147. protected:
  148. _FORCE_INLINE_ int _get_uniform(int p_which) const;
  149. _FORCE_INLINE_ void _set_conditional(int p_which, bool p_value);
  150. void setup(const char **p_conditional_defines,
  151. int p_conditional_count,
  152. const char **p_uniform_names,
  153. int p_uniform_count,
  154. const AttributePair *p_attribute_pairs,
  155. int p_attribute_count,
  156. const TexUnitPair *p_texunit_pairs,
  157. int p_texunit_pair_count,
  158. const char *p_vertex_code,
  159. const char *p_fragment_code,
  160. int p_vertex_code_start,
  161. int p_fragment_code_start);
  162. ShaderGLES2();
  163. public:
  164. enum {
  165. CUSTOM_SHADER_DISABLED = 0
  166. };
  167. GLint get_uniform_location(const String &p_name) const;
  168. GLint get_uniform_location(int p_index) const;
  169. static _FORCE_INLINE_ ShaderGLES2 *get_active() { return active; }
  170. bool bind();
  171. void unbind();
  172. inline GLuint get_program() const { return version ? version->id : 0; }
  173. void clear_caches();
  174. uint32_t create_custom_shader();
  175. void set_custom_shader_code(uint32_t p_code_id,
  176. const String &p_vertex,
  177. const String &p_vertex_globals,
  178. const String &p_fragment,
  179. const String &p_light,
  180. const String &p_fragment_globals,
  181. const Vector<StringName> &p_uniforms,
  182. const Vector<StringName> &p_texture_uniforms,
  183. const Vector<CharString> &p_custom_defines);
  184. void set_custom_shader(uint32_t p_code_id);
  185. void free_custom_shader(uint32_t p_code_id);
  186. uint64_t get_version_key() const { return conditional_version.version; }
  187. // this void* is actually a RasterizerStorageGLES2::Material, but C++ doesn't
  188. // like forward declared nested classes.
  189. void use_material(void *p_material);
  190. _FORCE_INLINE_ uint64_t get_version() const { return new_conditional_version.version; }
  191. _FORCE_INLINE_ bool is_version_valid() const { return version && version->ok; }
  192. virtual void init() = 0;
  193. void finish();
  194. void add_custom_define(const String &p_define) {
  195. custom_defines.push_back(p_define.utf8());
  196. }
  197. void get_custom_defines(Vector<String> *p_defines) {
  198. for (int i = 0; i < custom_defines.size(); i++) {
  199. p_defines->push_back(custom_defines[i].get_data());
  200. }
  201. }
  202. void remove_custom_define(const String &p_define) {
  203. custom_defines.erase(p_define.utf8());
  204. }
  205. virtual ~ShaderGLES2();
  206. };
  207. // called a lot, made inline
  208. int ShaderGLES2::_get_uniform(int p_which) const {
  209. ERR_FAIL_INDEX_V(p_which, uniform_count, -1);
  210. ERR_FAIL_COND_V(!version, -1);
  211. return version->uniform_location[p_which];
  212. }
  213. void ShaderGLES2::_set_conditional(int p_which, bool p_value) {
  214. ERR_FAIL_INDEX(p_which, conditional_count);
  215. ERR_FAIL_INDEX(p_which, (int)sizeof(new_conditional_version.version) * 8);
  216. if (p_value) {
  217. new_conditional_version.version |= (uint64_t(1) << p_which);
  218. } else {
  219. new_conditional_version.version &= ~(uint64_t(1) << p_which);
  220. }
  221. }
  222. #endif // SHADER_GLES2_H