shader_gles3.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**************************************************************************/
  2. /* shader_gles3.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_GLES3_H
  31. #define SHADER_GLES3_H
  32. #include "core/hash_map.h"
  33. #include "core/local_vector.h"
  34. #include "core/map.h"
  35. #include "core/math/camera_matrix.h"
  36. #include "core/safe_refcount.h"
  37. #include "core/self_list.h"
  38. #include "core/variant.h"
  39. #include "platform_config.h"
  40. #ifndef GLES3_INCLUDE_H
  41. #include <GLES3/gl3.h>
  42. #else
  43. #include GLES3_INCLUDE_H
  44. #endif
  45. #include <stdio.h>
  46. template <class K>
  47. class ThreadedCallableQueue;
  48. class ShaderCacheGLES3;
  49. class ShaderGLES3 {
  50. protected:
  51. struct Enum {
  52. uint64_t mask;
  53. uint64_t shift;
  54. const char *defines[16];
  55. };
  56. struct EnumValue {
  57. uint64_t set_mask;
  58. uint64_t clear_mask;
  59. };
  60. struct AttributePair {
  61. const char *name;
  62. int index;
  63. };
  64. struct UniformPair {
  65. const char *name;
  66. Variant::Type type_hint;
  67. };
  68. struct TexUnitPair {
  69. const char *name;
  70. int index;
  71. };
  72. struct UBOPair {
  73. const char *name;
  74. int index;
  75. };
  76. struct Feedback {
  77. const char *name;
  78. int conditional;
  79. };
  80. virtual int get_ubershader_flags_uniform() const { return -1; }
  81. private:
  82. //@TODO Optimize to a fixed set of shader pools and use a LRU
  83. int uniform_count;
  84. int texunit_pair_count;
  85. int conditional_count;
  86. int ubo_count;
  87. int feedback_count;
  88. int vertex_code_start;
  89. int fragment_code_start;
  90. int attribute_pair_count;
  91. public:
  92. enum AsyncMode {
  93. ASYNC_MODE_VISIBLE,
  94. ASYNC_MODE_HIDDEN,
  95. };
  96. private:
  97. struct CustomCode {
  98. String vertex;
  99. String vertex_globals;
  100. String fragment;
  101. String fragment_globals;
  102. String light;
  103. String uniforms;
  104. AsyncMode async_mode;
  105. uint32_t version;
  106. Vector<StringName> texture_uniforms;
  107. Vector<CharString> custom_defines;
  108. Set<uint32_t> versions;
  109. };
  110. public:
  111. static ShaderCacheGLES3 *shader_cache;
  112. static ThreadedCallableQueue<GLuint> *cache_write_queue;
  113. static ThreadedCallableQueue<GLuint> *compile_queue; // Non-null if using queued asynchronous compilation (via secondary context)
  114. static bool parallel_compile_supported; // True if using natively supported asyncrhonous compilation
  115. static bool async_hidden_forbidden;
  116. static uint32_t *compiles_started_this_frame;
  117. static uint32_t *max_frame_compiles_in_progress;
  118. static uint32_t max_simultaneous_compiles;
  119. static uint32_t active_compiles_count;
  120. #ifdef DEBUG_ENABLED
  121. static bool log_active_async_compiles_count;
  122. #endif
  123. static uint64_t current_frame;
  124. static void advance_async_shaders_compilation();
  125. private:
  126. union VersionKey {
  127. static const uint32_t UBERSHADER_FLAG = ((uint32_t)1) << 31;
  128. struct {
  129. uint32_t version;
  130. uint32_t code_version;
  131. };
  132. uint64_t key;
  133. bool operator==(const VersionKey &p_key) const { return key == p_key.key; }
  134. bool operator<(const VersionKey &p_key) const { return key < p_key.key; }
  135. VersionKey() {}
  136. VersionKey(uint64_t p_key) :
  137. key(p_key) {}
  138. _FORCE_INLINE_ bool is_subject_to_caching() const { return (version & UBERSHADER_FLAG); }
  139. };
  140. struct Version {
  141. VersionKey version_key;
  142. // Set by the render thread upfront; the compile thread (for queued async.) reads them
  143. struct Ids {
  144. GLuint main;
  145. GLuint vert;
  146. GLuint frag;
  147. } ids;
  148. ShaderGLES3 *shader;
  149. uint32_t code_version;
  150. AsyncMode async_mode;
  151. GLint *uniform_location;
  152. Vector<GLint> texture_uniform_locations;
  153. bool uniforms_ready;
  154. uint64_t last_frame_processed;
  155. enum CompileStatus {
  156. COMPILE_STATUS_PENDING,
  157. COMPILE_STATUS_SOURCE_PROVIDED,
  158. COMPILE_STATUS_COMPILING_VERTEX,
  159. COMPILE_STATUS_COMPILING_FRAGMENT,
  160. COMPILE_STATUS_COMPILING_VERTEX_AND_FRAGMENT,
  161. COMPILE_STATUS_PROCESSING_AT_QUEUE,
  162. COMPILE_STATUS_BINARY_READY,
  163. COMPILE_STATUS_BINARY_READY_FROM_CACHE,
  164. COMPILE_STATUS_LINKING,
  165. COMPILE_STATUS_ERROR,
  166. COMPILE_STATUS_RESTART_NEEDED,
  167. COMPILE_STATUS_OK,
  168. };
  169. CompileStatus compile_status;
  170. SelfList<Version> compiling_list;
  171. struct ProgramBinary {
  172. String cache_hash;
  173. enum Source {
  174. SOURCE_NONE,
  175. SOURCE_LOCAL, // Binary data will only be available if cache enabled
  176. SOURCE_QUEUE,
  177. SOURCE_CACHE,
  178. } source;
  179. // Shared with the compile thread (for queued async.); otherwise render thread only
  180. GLenum format;
  181. PoolByteArray data;
  182. SafeNumeric<int> result_from_queue;
  183. } program_binary;
  184. Version() :
  185. version_key(0),
  186. ids(),
  187. shader(nullptr),
  188. code_version(0),
  189. async_mode(ASYNC_MODE_VISIBLE),
  190. uniform_location(nullptr),
  191. uniforms_ready(false),
  192. last_frame_processed(UINT64_MAX),
  193. compile_status(COMPILE_STATUS_PENDING),
  194. compiling_list(this),
  195. program_binary() {}
  196. };
  197. static SelfList<Version>::List versions_compiling;
  198. Version *version;
  199. struct VersionKeyHash {
  200. static _FORCE_INLINE_ uint32_t hash(const VersionKey &p_key) { return HashMapHasherDefault::hash(p_key.key); };
  201. };
  202. //this should use a way more cachefriendly version..
  203. HashMap<VersionKey, Version, VersionKeyHash> version_map;
  204. HashMap<uint32_t, CustomCode> custom_code_map;
  205. uint32_t last_custom_code;
  206. VersionKey conditional_version;
  207. VersionKey new_conditional_version;
  208. virtual String get_shader_name() const = 0;
  209. const char **conditional_defines;
  210. const char **uniform_names;
  211. const AttributePair *attribute_pairs;
  212. const TexUnitPair *texunit_pairs;
  213. const UBOPair *ubo_pairs;
  214. const Feedback *feedbacks;
  215. const char *vertex_code;
  216. const char *fragment_code;
  217. CharString fragment_code0;
  218. CharString fragment_code1;
  219. CharString fragment_code2;
  220. CharString fragment_code3;
  221. CharString fragment_code4;
  222. CharString vertex_code0;
  223. CharString vertex_code1;
  224. CharString vertex_code2;
  225. CharString vertex_code3;
  226. Vector<CharString> custom_defines;
  227. int base_material_tex_index;
  228. Version *get_current_version(bool &r_async_forbidden);
  229. // These will run on the shader compile thread if using que compile queue approach to async.
  230. void _set_source(Version::Ids p_ids, const LocalVector<const char *> &p_vertex_strings, const LocalVector<const char *> &p_fragment_strings) const;
  231. bool _complete_compile(Version::Ids p_ids, bool p_retrievable) const;
  232. bool _complete_link(Version::Ids p_ids, GLenum *r_program_format = nullptr, PoolByteArray *r_program_binary = nullptr) const;
  233. // ---
  234. static void _log_active_compiles();
  235. static bool _process_program_state(Version *p_version, bool p_async_forbidden);
  236. void _setup_uniforms(CustomCode *p_cc) const;
  237. void _dispose_program(Version *p_version);
  238. static ShaderGLES3 *active;
  239. int max_image_units;
  240. _FORCE_INLINE_ void _set_uniform_variant(GLint p_uniform, const Variant &p_value) {
  241. if (p_uniform < 0) {
  242. return; // do none
  243. }
  244. switch (p_value.get_type()) {
  245. case Variant::BOOL:
  246. case Variant::INT: {
  247. int val = p_value;
  248. glUniform1i(p_uniform, val);
  249. } break;
  250. case Variant::REAL: {
  251. real_t val = p_value;
  252. glUniform1f(p_uniform, val);
  253. } break;
  254. case Variant::COLOR: {
  255. Color val = p_value;
  256. glUniform4f(p_uniform, val.r, val.g, val.b, val.a);
  257. } break;
  258. case Variant::VECTOR2: {
  259. Vector2 val = p_value;
  260. glUniform2f(p_uniform, val.x, val.y);
  261. } break;
  262. case Variant::VECTOR3: {
  263. Vector3 val = p_value;
  264. glUniform3f(p_uniform, val.x, val.y, val.z);
  265. } break;
  266. case Variant::PLANE: {
  267. Plane val = p_value;
  268. glUniform4f(p_uniform, val.normal.x, val.normal.y, val.normal.z, val.d);
  269. } break;
  270. case Variant::QUAT: {
  271. Quat val = p_value;
  272. glUniform4f(p_uniform, val.x, val.y, val.z, val.w);
  273. } break;
  274. case Variant::TRANSFORM2D: {
  275. Transform2D tr = p_value;
  276. GLfloat matrix[16] = { /* build a 16x16 matrix */
  277. tr.elements[0][0],
  278. tr.elements[0][1],
  279. 0,
  280. 0,
  281. tr.elements[1][0],
  282. tr.elements[1][1],
  283. 0,
  284. 0,
  285. 0,
  286. 0,
  287. 1,
  288. 0,
  289. tr.elements[2][0],
  290. tr.elements[2][1],
  291. 0,
  292. 1
  293. };
  294. glUniformMatrix4fv(p_uniform, 1, false, matrix);
  295. } break;
  296. case Variant::BASIS:
  297. case Variant::TRANSFORM: {
  298. Transform tr = p_value;
  299. GLfloat matrix[16] = { /* build a 16x16 matrix */
  300. tr.basis.elements[0][0],
  301. tr.basis.elements[1][0],
  302. tr.basis.elements[2][0],
  303. 0,
  304. tr.basis.elements[0][1],
  305. tr.basis.elements[1][1],
  306. tr.basis.elements[2][1],
  307. 0,
  308. tr.basis.elements[0][2],
  309. tr.basis.elements[1][2],
  310. tr.basis.elements[2][2],
  311. 0,
  312. tr.origin.x,
  313. tr.origin.y,
  314. tr.origin.z,
  315. 1
  316. };
  317. glUniformMatrix4fv(p_uniform, 1, false, matrix);
  318. } break;
  319. default: {
  320. ERR_FAIL();
  321. } // do nothing
  322. }
  323. }
  324. bool _bind(bool p_binding_fallback);
  325. bool _bind_ubershader(bool p_for_warmrup = false);
  326. protected:
  327. _FORCE_INLINE_ int _get_uniform(int p_which) const;
  328. _FORCE_INLINE_ void _set_conditional(int p_which, bool p_value);
  329. 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);
  330. ShaderGLES3();
  331. public:
  332. enum {
  333. CUSTOM_SHADER_DISABLED = 0
  334. };
  335. GLint get_uniform_location(const String &p_name) const;
  336. GLint get_uniform_location(int p_index) const;
  337. static _FORCE_INLINE_ ShaderGLES3 *get_active() { return active; };
  338. bool bind();
  339. void unbind();
  340. void clear_caches();
  341. uint32_t create_custom_shader();
  342. 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, AsyncMode p_async_mode);
  343. void set_custom_shader(uint32_t p_code_id);
  344. void free_custom_shader(uint32_t p_code_id);
  345. bool is_custom_code_ready_for_render(uint32_t p_code_id);
  346. uint32_t get_version() const { return new_conditional_version.version; }
  347. _FORCE_INLINE_ bool is_version_valid() const { return version && version->compile_status == Version::COMPILE_STATUS_OK; }
  348. virtual void init() = 0;
  349. void init_async_compilation();
  350. bool is_async_compilation_supported();
  351. void finish();
  352. void set_base_material_tex_index(int p_idx);
  353. void add_custom_define(const String &p_define) {
  354. custom_defines.push_back(p_define.utf8());
  355. }
  356. void get_custom_defines(Vector<String> *p_defines) {
  357. for (int i = 0; i < custom_defines.size(); i++) {
  358. p_defines->push_back(custom_defines[i].get_data());
  359. }
  360. }
  361. void remove_custom_define(const String &p_define) {
  362. custom_defines.erase(p_define.utf8());
  363. }
  364. virtual ~ShaderGLES3();
  365. };
  366. // called a lot, made inline
  367. int ShaderGLES3::_get_uniform(int p_which) const {
  368. ERR_FAIL_INDEX_V(p_which, uniform_count, -1);
  369. ERR_FAIL_COND_V(!version, -1);
  370. return version->uniform_location[p_which];
  371. }
  372. void ShaderGLES3::_set_conditional(int p_which, bool p_value) {
  373. ERR_FAIL_INDEX(p_which, conditional_count);
  374. if (p_value) {
  375. new_conditional_version.version |= (1 << p_which);
  376. } else {
  377. new_conditional_version.version &= ~(1 << p_which);
  378. }
  379. }
  380. #endif // SHADER_GLES3_H