shader_gles3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*************************************************************************/
  2. /* shader_gles3.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_GLES3_H
  31. #define SHADER_GLES3_H
  32. #include "core/hash_map.h"
  33. #include "core/map.h"
  34. #include "core/math/camera_matrix.h"
  35. #include "core/variant.h"
  36. #include "platform_config.h"
  37. #ifndef GLES3_INCLUDE_H
  38. #include <GLES3/gl3.h>
  39. #else
  40. #include GLES3_INCLUDE_H
  41. #endif
  42. #include <stdio.h>
  43. /**
  44. @author Juan Linietsky <reduzio@gmail.com>
  45. */
  46. class ShaderGLES3 {
  47. protected:
  48. struct Enum {
  49. uint64_t mask;
  50. uint64_t shift;
  51. const char *defines[16];
  52. };
  53. struct EnumValue {
  54. uint64_t set_mask;
  55. uint64_t clear_mask;
  56. };
  57. struct AttributePair {
  58. const char *name;
  59. int index;
  60. };
  61. struct UniformPair {
  62. const char *name;
  63. Variant::Type type_hint;
  64. };
  65. struct TexUnitPair {
  66. const char *name;
  67. int index;
  68. };
  69. struct UBOPair {
  70. const char *name;
  71. int index;
  72. };
  73. struct Feedback {
  74. const char *name;
  75. int conditional;
  76. };
  77. bool uniforms_dirty;
  78. private:
  79. //@TODO Optimize to a fixed set of shader pools and use a LRU
  80. int uniform_count;
  81. int texunit_pair_count;
  82. int conditional_count;
  83. int ubo_count;
  84. int feedback_count;
  85. int vertex_code_start;
  86. int fragment_code_start;
  87. int attribute_pair_count;
  88. struct CustomCode {
  89. String vertex;
  90. String vertex_globals;
  91. String fragment;
  92. String fragment_globals;
  93. String light;
  94. String uniforms;
  95. uint32_t version;
  96. Vector<StringName> texture_uniforms;
  97. Vector<CharString> custom_defines;
  98. };
  99. struct Version {
  100. GLuint id;
  101. GLuint vert_id;
  102. GLuint frag_id;
  103. GLint *uniform_location;
  104. Vector<GLint> texture_uniform_locations;
  105. uint32_t code_version;
  106. bool ok;
  107. Version() :
  108. id(0),
  109. vert_id(0),
  110. frag_id(0),
  111. uniform_location(NULL),
  112. code_version(0),
  113. ok(false) {}
  114. };
  115. Version *version;
  116. union VersionKey {
  117. struct {
  118. uint32_t version;
  119. uint32_t code_version;
  120. };
  121. uint64_t key;
  122. bool operator==(const VersionKey &p_key) const { return key == p_key.key; }
  123. bool operator<(const VersionKey &p_key) const { return key < p_key.key; }
  124. };
  125. struct VersionKeyHash {
  126. static _FORCE_INLINE_ uint32_t hash(const VersionKey &p_key) { return HashMapHasherDefault::hash(p_key.key); };
  127. };
  128. //this should use a way more cachefriendly version..
  129. HashMap<VersionKey, Version, VersionKeyHash> version_map;
  130. HashMap<uint32_t, CustomCode> custom_code_map;
  131. uint32_t last_custom_code;
  132. VersionKey conditional_version;
  133. VersionKey new_conditional_version;
  134. virtual String get_shader_name() const = 0;
  135. const char **conditional_defines;
  136. const char **uniform_names;
  137. const AttributePair *attribute_pairs;
  138. const TexUnitPair *texunit_pairs;
  139. const UBOPair *ubo_pairs;
  140. const Feedback *feedbacks;
  141. const char *vertex_code;
  142. const char *fragment_code;
  143. CharString fragment_code0;
  144. CharString fragment_code1;
  145. CharString fragment_code2;
  146. CharString fragment_code3;
  147. CharString fragment_code4;
  148. CharString vertex_code0;
  149. CharString vertex_code1;
  150. CharString vertex_code2;
  151. CharString vertex_code3;
  152. Vector<CharString> custom_defines;
  153. int base_material_tex_index;
  154. Version *get_current_version();
  155. static ShaderGLES3 *active;
  156. int max_image_units;
  157. _FORCE_INLINE_ void _set_uniform_variant(GLint p_uniform, const Variant &p_value) {
  158. if (p_uniform < 0)
  159. return; // do none
  160. switch (p_value.get_type()) {
  161. case Variant::BOOL:
  162. case Variant::INT: {
  163. int val = p_value;
  164. glUniform1i(p_uniform, val);
  165. } break;
  166. case Variant::REAL: {
  167. real_t val = p_value;
  168. glUniform1f(p_uniform, val);
  169. } break;
  170. case Variant::COLOR: {
  171. Color val = p_value;
  172. glUniform4f(p_uniform, val.r, val.g, val.b, val.a);
  173. } break;
  174. case Variant::VECTOR2: {
  175. Vector2 val = p_value;
  176. glUniform2f(p_uniform, val.x, val.y);
  177. } break;
  178. case Variant::VECTOR3: {
  179. Vector3 val = p_value;
  180. glUniform3f(p_uniform, val.x, val.y, val.z);
  181. } break;
  182. case Variant::PLANE: {
  183. Plane val = p_value;
  184. glUniform4f(p_uniform, val.normal.x, val.normal.y, val.normal.z, val.d);
  185. } break;
  186. case Variant::QUAT: {
  187. Quat val = p_value;
  188. glUniform4f(p_uniform, val.x, val.y, val.z, val.w);
  189. } break;
  190. case Variant::TRANSFORM2D: {
  191. Transform2D tr = p_value;
  192. GLfloat matrix[16] = { /* build a 16x16 matrix */
  193. tr.elements[0][0],
  194. tr.elements[0][1],
  195. 0,
  196. 0,
  197. tr.elements[1][0],
  198. tr.elements[1][1],
  199. 0,
  200. 0,
  201. 0,
  202. 0,
  203. 1,
  204. 0,
  205. tr.elements[2][0],
  206. tr.elements[2][1],
  207. 0,
  208. 1
  209. };
  210. glUniformMatrix4fv(p_uniform, 1, false, matrix);
  211. } break;
  212. case Variant::BASIS:
  213. case Variant::TRANSFORM: {
  214. Transform tr = p_value;
  215. GLfloat matrix[16] = { /* build a 16x16 matrix */
  216. tr.basis.elements[0][0],
  217. tr.basis.elements[1][0],
  218. tr.basis.elements[2][0],
  219. 0,
  220. tr.basis.elements[0][1],
  221. tr.basis.elements[1][1],
  222. tr.basis.elements[2][1],
  223. 0,
  224. tr.basis.elements[0][2],
  225. tr.basis.elements[1][2],
  226. tr.basis.elements[2][2],
  227. 0,
  228. tr.origin.x,
  229. tr.origin.y,
  230. tr.origin.z,
  231. 1
  232. };
  233. glUniformMatrix4fv(p_uniform, 1, false, matrix);
  234. } break;
  235. default: { ERR_FAIL(); } // do nothing
  236. }
  237. }
  238. Map<uint32_t, Variant> uniform_defaults;
  239. Map<uint32_t, CameraMatrix> uniform_cameras;
  240. protected:
  241. _FORCE_INLINE_ int _get_uniform(int p_which) const;
  242. _FORCE_INLINE_ void _set_conditional(int p_which, bool p_value);
  243. void setup(const char **p_conditional_defines, int p_conditional_count, const char **p_uniform_names, int p_uniform_count, const AttributePair *p_attribute_pairs, int p_attribute_count, const TexUnitPair *p_texunit_pairs, int p_texunit_pair_count, const UBOPair *p_ubo_pairs, int p_ubo_pair_count, const Feedback *p_feedback, int p_feedback_count, const char *p_vertex_code, const char *p_fragment_code, int p_vertex_code_start, int p_fragment_code_start);
  244. ShaderGLES3();
  245. public:
  246. enum {
  247. CUSTOM_SHADER_DISABLED = 0
  248. };
  249. GLint get_uniform_location(const String &p_name) const;
  250. GLint get_uniform_location(int p_index) const;
  251. static _FORCE_INLINE_ ShaderGLES3 *get_active() { return active; };
  252. bool bind();
  253. void unbind();
  254. void bind_uniforms();
  255. inline GLuint get_program() const { return version ? version->id : 0; }
  256. void clear_caches();
  257. uint32_t create_custom_shader();
  258. void set_custom_shader_code(uint32_t p_code_id, const String &p_vertex, const String &p_vertex_globals, const String &p_fragment, const String &p_light, const String &p_fragment_globals, const String &p_uniforms, const Vector<StringName> &p_texture_uniforms, const Vector<CharString> &p_custom_defines);
  259. void set_custom_shader(uint32_t p_code_id);
  260. void free_custom_shader(uint32_t p_code_id);
  261. void set_uniform_default(int p_idx, const Variant &p_value) {
  262. if (p_value.get_type() == Variant::NIL) {
  263. uniform_defaults.erase(p_idx);
  264. } else {
  265. uniform_defaults[p_idx] = p_value;
  266. }
  267. uniforms_dirty = true;
  268. }
  269. uint32_t get_version() const { return new_conditional_version.version; }
  270. _FORCE_INLINE_ bool is_version_valid() const { return version && version->ok; }
  271. void set_uniform_camera(int p_idx, const CameraMatrix &p_mat) {
  272. uniform_cameras[p_idx] = p_mat;
  273. uniforms_dirty = true;
  274. };
  275. _FORCE_INLINE_ void set_texture_uniform(int p_idx, const Variant &p_value) {
  276. ERR_FAIL_COND(!version);
  277. ERR_FAIL_INDEX(p_idx, version->texture_uniform_locations.size());
  278. _set_uniform_variant(version->texture_uniform_locations[p_idx], p_value);
  279. }
  280. _FORCE_INLINE_ GLint get_texture_uniform_location(int p_idx) {
  281. ERR_FAIL_COND_V(!version, -1);
  282. ERR_FAIL_INDEX_V(p_idx, version->texture_uniform_locations.size(), -1);
  283. return version->texture_uniform_locations[p_idx];
  284. }
  285. virtual void init() = 0;
  286. void finish();
  287. void set_base_material_tex_index(int p_idx);
  288. void add_custom_define(const String &p_define) {
  289. custom_defines.push_back(p_define.utf8());
  290. }
  291. virtual ~ShaderGLES3();
  292. };
  293. // called a lot, made inline
  294. int ShaderGLES3::_get_uniform(int p_which) const {
  295. ERR_FAIL_INDEX_V(p_which, uniform_count, -1);
  296. ERR_FAIL_COND_V(!version, -1);
  297. return version->uniform_location[p_which];
  298. }
  299. void ShaderGLES3::_set_conditional(int p_which, bool p_value) {
  300. ERR_FAIL_INDEX(p_which, conditional_count);
  301. if (p_value)
  302. new_conditional_version.version |= (1 << p_which);
  303. else
  304. new_conditional_version.version &= ~(1 << p_which);
  305. }
  306. #endif