shader_gles2.h 8.1 KB

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