shader_gles3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*************************************************************************/
  2. /* shader_gles3.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_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. class ShaderGLES3 {
  44. protected:
  45. struct Enum {
  46. uint64_t mask;
  47. uint64_t shift;
  48. const char *defines[16];
  49. };
  50. struct EnumValue {
  51. uint64_t set_mask;
  52. uint64_t clear_mask;
  53. };
  54. struct AttributePair {
  55. const char *name;
  56. int index;
  57. };
  58. struct UniformPair {
  59. const char *name;
  60. Variant::Type type_hint;
  61. };
  62. struct TexUnitPair {
  63. const char *name;
  64. int index;
  65. };
  66. struct UBOPair {
  67. const char *name;
  68. int index;
  69. };
  70. struct Feedback {
  71. const char *name;
  72. int conditional;
  73. };
  74. bool uniforms_dirty;
  75. private:
  76. //@TODO Optimize to a fixed set of shader pools and use a LRU
  77. int uniform_count;
  78. int texunit_pair_count;
  79. int conditional_count;
  80. int ubo_count;
  81. int feedback_count;
  82. int vertex_code_start;
  83. int fragment_code_start;
  84. int attribute_pair_count;
  85. struct CustomCode {
  86. String vertex;
  87. String vertex_globals;
  88. String fragment;
  89. String fragment_globals;
  90. String light;
  91. String uniforms;
  92. uint32_t version;
  93. Vector<StringName> texture_uniforms;
  94. Vector<CharString> custom_defines;
  95. Set<uint32_t> versions;
  96. };
  97. struct Version {
  98. GLuint id;
  99. GLuint vert_id;
  100. GLuint frag_id;
  101. GLint *uniform_location;
  102. Vector<GLint> texture_uniform_locations;
  103. uint32_t code_version;
  104. bool ok;
  105. Version() :
  106. id(0),
  107. vert_id(0),
  108. frag_id(0),
  109. uniform_location(NULL),
  110. code_version(0),
  111. ok(false) {}
  112. };
  113. Version *version;
  114. union VersionKey {
  115. struct {
  116. uint32_t version;
  117. uint32_t code_version;
  118. };
  119. uint64_t key;
  120. bool operator==(const VersionKey &p_key) const { return key == p_key.key; }
  121. bool operator<(const VersionKey &p_key) const { return key < p_key.key; }
  122. };
  123. struct VersionKeyHash {
  124. static _FORCE_INLINE_ uint32_t hash(const VersionKey &p_key) { return HashMapHasherDefault::hash(p_key.key); };
  125. };
  126. //this should use a way more cachefriendly version..
  127. HashMap<VersionKey, Version, VersionKeyHash> version_map;
  128. HashMap<uint32_t, CustomCode> custom_code_map;
  129. uint32_t last_custom_code;
  130. VersionKey conditional_version;
  131. VersionKey new_conditional_version;
  132. virtual String get_shader_name() const = 0;
  133. const char **conditional_defines;
  134. const char **uniform_names;
  135. const AttributePair *attribute_pairs;
  136. const TexUnitPair *texunit_pairs;
  137. const UBOPair *ubo_pairs;
  138. const Feedback *feedbacks;
  139. const char *vertex_code;
  140. const char *fragment_code;
  141. CharString fragment_code0;
  142. CharString fragment_code1;
  143. CharString fragment_code2;
  144. CharString fragment_code3;
  145. CharString fragment_code4;
  146. CharString vertex_code0;
  147. CharString vertex_code1;
  148. CharString vertex_code2;
  149. CharString vertex_code3;
  150. Vector<CharString> custom_defines;
  151. int base_material_tex_index;
  152. Version *get_current_version();
  153. static ShaderGLES3 *active;
  154. int max_image_units;
  155. _FORCE_INLINE_ void _set_uniform_variant(GLint p_uniform, const Variant &p_value) {
  156. if (p_uniform < 0)
  157. return; // do none
  158. switch (p_value.get_type()) {
  159. case Variant::BOOL:
  160. case Variant::INT: {
  161. int val = p_value;
  162. glUniform1i(p_uniform, val);
  163. } break;
  164. case Variant::REAL: {
  165. real_t val = p_value;
  166. glUniform1f(p_uniform, val);
  167. } break;
  168. case Variant::COLOR: {
  169. Color val = p_value;
  170. glUniform4f(p_uniform, val.r, val.g, val.b, val.a);
  171. } break;
  172. case Variant::VECTOR2: {
  173. Vector2 val = p_value;
  174. glUniform2f(p_uniform, val.x, val.y);
  175. } break;
  176. case Variant::VECTOR3: {
  177. Vector3 val = p_value;
  178. glUniform3f(p_uniform, val.x, val.y, val.z);
  179. } break;
  180. case Variant::PLANE: {
  181. Plane val = p_value;
  182. glUniform4f(p_uniform, val.normal.x, val.normal.y, val.normal.z, val.d);
  183. } break;
  184. case Variant::QUAT: {
  185. Quat val = p_value;
  186. glUniform4f(p_uniform, val.x, val.y, val.z, val.w);
  187. } break;
  188. case Variant::TRANSFORM2D: {
  189. Transform2D tr = p_value;
  190. GLfloat matrix[16] = { /* build a 16x16 matrix */
  191. tr.elements[0][0],
  192. tr.elements[0][1],
  193. 0,
  194. 0,
  195. tr.elements[1][0],
  196. tr.elements[1][1],
  197. 0,
  198. 0,
  199. 0,
  200. 0,
  201. 1,
  202. 0,
  203. tr.elements[2][0],
  204. tr.elements[2][1],
  205. 0,
  206. 1
  207. };
  208. glUniformMatrix4fv(p_uniform, 1, false, matrix);
  209. } break;
  210. case Variant::BASIS:
  211. case Variant::TRANSFORM: {
  212. Transform tr = p_value;
  213. GLfloat matrix[16] = { /* build a 16x16 matrix */
  214. tr.basis.elements[0][0],
  215. tr.basis.elements[1][0],
  216. tr.basis.elements[2][0],
  217. 0,
  218. tr.basis.elements[0][1],
  219. tr.basis.elements[1][1],
  220. tr.basis.elements[2][1],
  221. 0,
  222. tr.basis.elements[0][2],
  223. tr.basis.elements[1][2],
  224. tr.basis.elements[2][2],
  225. 0,
  226. tr.origin.x,
  227. tr.origin.y,
  228. tr.origin.z,
  229. 1
  230. };
  231. glUniformMatrix4fv(p_uniform, 1, false, matrix);
  232. } break;
  233. default: {
  234. ERR_FAIL();
  235. } // 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. void get_custom_defines(Vector<String> *p_defines) {
  292. for (int i = 0; i < custom_defines.size(); i++) {
  293. p_defines->push_back(custom_defines[i].get_data());
  294. }
  295. }
  296. void remove_custom_define(const String &p_define) {
  297. custom_defines.erase(p_define.utf8());
  298. }
  299. virtual ~ShaderGLES3();
  300. };
  301. // called a lot, made inline
  302. int ShaderGLES3::_get_uniform(int p_which) const {
  303. ERR_FAIL_INDEX_V(p_which, uniform_count, -1);
  304. ERR_FAIL_COND_V(!version, -1);
  305. return version->uniform_location[p_which];
  306. }
  307. void ShaderGLES3::_set_conditional(int p_which, bool p_value) {
  308. ERR_FAIL_INDEX(p_which, conditional_count);
  309. if (p_value)
  310. new_conditional_version.version |= (1 << p_which);
  311. else
  312. new_conditional_version.version &= ~(1 << p_which);
  313. }
  314. #endif