2
0

shader_gles2.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*************************************************************************/
  2. /* shader_gles2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_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<uint32_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. code_version = 0;
  102. ok = false;
  103. uniform_location = NULL;
  104. }
  105. };
  106. Version *version;
  107. union VersionKey {
  108. struct {
  109. uint32_t version;
  110. uint32_t code_version;
  111. };
  112. uint64_t key;
  113. bool operator==(const VersionKey &p_key) const { return key == p_key.key; }
  114. bool operator<(const VersionKey &p_key) const { return key < p_key.key; }
  115. };
  116. struct VersionKeyHash {
  117. static _FORCE_INLINE_ uint32_t hash(const VersionKey &p_key) { return HashMapHasherDefault::hash(p_key.key); }
  118. };
  119. //this should use a way more cachefriendly version..
  120. HashMap<VersionKey, Version, VersionKeyHash> version_map;
  121. HashMap<uint32_t, CustomCode> custom_code_map;
  122. uint32_t last_custom_code;
  123. VersionKey conditional_version;
  124. VersionKey new_conditional_version;
  125. virtual String get_shader_name() const = 0;
  126. const char **conditional_defines;
  127. const char **uniform_names;
  128. const AttributePair *attribute_pairs;
  129. const TexUnitPair *texunit_pairs;
  130. const char *vertex_code;
  131. const char *fragment_code;
  132. CharString fragment_code0;
  133. CharString fragment_code1;
  134. CharString fragment_code2;
  135. CharString fragment_code3;
  136. CharString vertex_code0;
  137. CharString vertex_code1;
  138. CharString vertex_code2;
  139. Vector<CharString> custom_defines;
  140. Version *get_current_version();
  141. static ShaderGLES2 *active;
  142. int max_image_units;
  143. Map<StringName, Pair<ShaderLanguage::DataType, Vector<ShaderLanguage::ConstantNode::Value> > > uniform_values;
  144. protected:
  145. _FORCE_INLINE_ int _get_uniform(int p_which) const;
  146. _FORCE_INLINE_ void _set_conditional(int p_which, bool p_value);
  147. void setup(const char **p_conditional_defines,
  148. int p_conditional_count,
  149. const char **p_uniform_names,
  150. int p_uniform_count,
  151. const AttributePair *p_attribute_pairs,
  152. int p_attribute_count,
  153. const TexUnitPair *p_texunit_pairs,
  154. int p_texunit_pair_count,
  155. const char *p_vertex_code,
  156. const char *p_fragment_code,
  157. int p_vertex_code_start,
  158. int p_fragment_code_start);
  159. ShaderGLES2();
  160. public:
  161. enum {
  162. CUSTOM_SHADER_DISABLED = 0
  163. };
  164. GLint get_uniform_location(const String &p_name) const;
  165. GLint get_uniform_location(int p_index) const;
  166. static _FORCE_INLINE_ ShaderGLES2 *get_active() { return active; }
  167. bool bind();
  168. void unbind();
  169. inline GLuint get_program() const { return version ? version->id : 0; }
  170. void clear_caches();
  171. uint32_t create_custom_shader();
  172. void set_custom_shader_code(uint32_t p_code_id,
  173. const String &p_vertex,
  174. const String &p_vertex_globals,
  175. const String &p_fragment,
  176. const String &p_light,
  177. const String &p_fragment_globals,
  178. const Vector<StringName> &p_uniforms,
  179. const Vector<StringName> &p_texture_uniforms,
  180. const Vector<CharString> &p_custom_defines);
  181. void set_custom_shader(uint32_t p_code_id);
  182. void free_custom_shader(uint32_t p_code_id);
  183. uint32_t get_version_key() const { return conditional_version.version; }
  184. // this void* is actually a RasterizerStorageGLES2::Material, but C++ doesn't
  185. // like forward declared nested classes.
  186. void use_material(void *p_material);
  187. _FORCE_INLINE_ uint32_t get_version() const { return new_conditional_version.version; }
  188. _FORCE_INLINE_ bool is_version_valid() const { return version && version->ok; }
  189. virtual void init() = 0;
  190. void finish();
  191. void add_custom_define(const String &p_define) {
  192. custom_defines.push_back(p_define.utf8());
  193. }
  194. virtual ~ShaderGLES2();
  195. };
  196. // called a lot, made inline
  197. int ShaderGLES2::_get_uniform(int p_which) const {
  198. ERR_FAIL_INDEX_V(p_which, uniform_count, -1);
  199. ERR_FAIL_COND_V(!version, -1);
  200. return version->uniform_location[p_which];
  201. }
  202. void ShaderGLES2::_set_conditional(int p_which, bool p_value) {
  203. ERR_FAIL_INDEX(p_which, conditional_count);
  204. if (p_value)
  205. new_conditional_version.version |= (1 << p_which);
  206. else
  207. new_conditional_version.version &= ~(1 << p_which);
  208. }
  209. #endif