shader_gles3.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/math/projection.h"
  33. #include "core/os/mutex.h"
  34. #include "core/string/string_builder.h"
  35. #include "core/templates/hash_map.h"
  36. #include "core/templates/local_vector.h"
  37. #include "core/templates/rb_map.h"
  38. #include "core/templates/rid_owner.h"
  39. #include "core/variant/variant.h"
  40. #include "servers/rendering_server.h"
  41. #ifdef GLES3_ENABLED
  42. #include "platform_gl.h"
  43. #include <stdio.h>
  44. class ShaderGLES3 {
  45. public:
  46. struct TextureUniformData {
  47. StringName name;
  48. int array_size;
  49. };
  50. protected:
  51. struct TexUnitPair {
  52. const char *name;
  53. int index;
  54. };
  55. struct UBOPair {
  56. const char *name;
  57. int index;
  58. };
  59. struct Specialization {
  60. const char *name;
  61. bool default_value = false;
  62. };
  63. struct Feedback {
  64. const char *name;
  65. uint64_t specialization;
  66. };
  67. private:
  68. //versions
  69. CharString general_defines;
  70. // A version is a high-level construct which is a combination of built-in and user-defined shader code, Each user-created Shader makes one version
  71. // Variants use #ifdefs to toggle behavior on and off to change behavior of the shader
  72. // All variants are compiled each time a new version is created
  73. // Specializations use #ifdefs to toggle behavior on and off for performance, on supporting hardware, they will compile a version with everything enabled, and then compile more copies to improve performance
  74. // Use specializations to enable and disabled advanced features, use variants to toggle behavior when different data may be used (e.g. using a samplerArray vs a sampler, or doing a depth prepass vs a color pass)
  75. struct Version {
  76. LocalVector<TextureUniformData> texture_uniforms;
  77. CharString uniforms;
  78. CharString vertex_globals;
  79. CharString fragment_globals;
  80. HashMap<StringName, CharString> code_sections;
  81. Vector<CharString> custom_defines;
  82. struct Specialization {
  83. GLuint id;
  84. GLuint vert_id;
  85. GLuint frag_id;
  86. LocalVector<GLint> uniform_location;
  87. LocalVector<GLint> texture_uniform_locations;
  88. bool build_queued = false;
  89. bool ok = false;
  90. Specialization() {
  91. id = 0;
  92. vert_id = 0;
  93. frag_id = 0;
  94. }
  95. };
  96. LocalVector<OAHashMap<uint64_t, Specialization>> variants;
  97. };
  98. Mutex variant_set_mutex;
  99. void _get_uniform_locations(Version::Specialization &spec, Version *p_version);
  100. void _compile_specialization(Version::Specialization &spec, uint32_t p_variant, Version *p_version, uint64_t p_specialization);
  101. void _clear_version(Version *p_version);
  102. void _initialize_version(Version *p_version);
  103. RID_Owner<Version, true> version_owner;
  104. struct StageTemplate {
  105. struct Chunk {
  106. enum Type {
  107. TYPE_MATERIAL_UNIFORMS,
  108. TYPE_VERTEX_GLOBALS,
  109. TYPE_FRAGMENT_GLOBALS,
  110. TYPE_CODE,
  111. TYPE_TEXT
  112. };
  113. Type type;
  114. StringName code;
  115. CharString text;
  116. };
  117. LocalVector<Chunk> chunks;
  118. };
  119. String name;
  120. String base_sha256;
  121. static String shader_cache_dir;
  122. static bool shader_cache_cleanup_on_start;
  123. static bool shader_cache_save_compressed;
  124. static bool shader_cache_save_compressed_zstd;
  125. static bool shader_cache_save_debug;
  126. bool shader_cache_dir_valid = false;
  127. GLint max_image_units = 0;
  128. enum StageType {
  129. STAGE_TYPE_VERTEX,
  130. STAGE_TYPE_FRAGMENT,
  131. STAGE_TYPE_MAX,
  132. };
  133. StageTemplate stage_templates[STAGE_TYPE_MAX];
  134. void _build_variant_code(StringBuilder &p_builder, uint32_t p_variant, const Version *p_version, StageType p_stage_type, uint64_t p_specialization);
  135. void _add_stage(const char *p_code, StageType p_stage_type);
  136. String _version_get_sha1(Version *p_version) const;
  137. bool _load_from_cache(Version *p_version);
  138. void _save_to_cache(Version *p_version);
  139. const char **uniform_names = nullptr;
  140. int uniform_count = 0;
  141. const UBOPair *ubo_pairs = nullptr;
  142. int ubo_count = 0;
  143. const Feedback *feedbacks;
  144. int feedback_count = 0;
  145. const TexUnitPair *texunit_pairs = nullptr;
  146. int texunit_pair_count = 0;
  147. int specialization_count = 0;
  148. const Specialization *specializations = nullptr;
  149. uint64_t specialization_default_mask = 0;
  150. const char **variant_defines = nullptr;
  151. int variant_count = 0;
  152. int base_texture_index = 0;
  153. Version::Specialization *current_shader = nullptr;
  154. protected:
  155. ShaderGLES3();
  156. void _setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_name, int p_uniform_count, const char **p_uniform_names, int p_ubo_count, const UBOPair *p_ubos, int p_feedback_count, const Feedback *p_feedback, int p_texture_count, const TexUnitPair *p_tex_units, int p_specialization_count, const Specialization *p_specializations, int p_variant_count, const char **p_variants);
  157. _FORCE_INLINE_ bool _version_bind_shader(RID p_version, int p_variant, uint64_t p_specialization) {
  158. ERR_FAIL_INDEX_V(p_variant, variant_count, false);
  159. Version *version = version_owner.get_or_null(p_version);
  160. ERR_FAIL_NULL_V(version, false);
  161. if (version->variants.size() == 0) {
  162. _initialize_version(version); //may lack initialization
  163. }
  164. Version::Specialization *spec = version->variants[p_variant].lookup_ptr(p_specialization);
  165. if (!spec) {
  166. if (false) {
  167. // Queue load this specialization and use defaults in the meantime (TODO)
  168. spec = version->variants[p_variant].lookup_ptr(specialization_default_mask);
  169. } else {
  170. // Compile on the spot
  171. Version::Specialization s;
  172. _compile_specialization(s, p_variant, version, p_specialization);
  173. version->variants[p_variant].insert(p_specialization, s);
  174. spec = version->variants[p_variant].lookup_ptr(p_specialization);
  175. if (shader_cache_dir_valid) {
  176. _save_to_cache(version);
  177. }
  178. }
  179. } else if (spec->build_queued) {
  180. // Still queued, wait
  181. spec = version->variants[p_variant].lookup_ptr(specialization_default_mask);
  182. }
  183. if (!spec || !spec->ok) {
  184. WARN_PRINT_ONCE("shader failed to compile, unable to bind shader.");
  185. return false;
  186. }
  187. glUseProgram(spec->id);
  188. current_shader = spec;
  189. return true;
  190. }
  191. _FORCE_INLINE_ int _version_get_uniform(int p_which, RID p_version, int p_variant, uint64_t p_specialization) {
  192. ERR_FAIL_INDEX_V(p_which, uniform_count, -1);
  193. Version *version = version_owner.get_or_null(p_version);
  194. ERR_FAIL_NULL_V(version, -1);
  195. ERR_FAIL_INDEX_V(p_variant, int(version->variants.size()), -1);
  196. Version::Specialization *spec = version->variants[p_variant].lookup_ptr(p_specialization);
  197. ERR_FAIL_NULL_V(spec, -1);
  198. ERR_FAIL_INDEX_V(p_which, int(spec->uniform_location.size()), -1);
  199. return spec->uniform_location[p_which];
  200. }
  201. virtual void _init() = 0;
  202. public:
  203. RID version_create();
  204. void version_set_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const Vector<String> &p_custom_defines, const LocalVector<ShaderGLES3::TextureUniformData> &p_texture_uniforms, bool p_initialize = false);
  205. bool version_is_valid(RID p_version);
  206. bool version_free(RID p_version);
  207. static void set_shader_cache_dir(const String &p_dir);
  208. static void set_shader_cache_save_compressed(bool p_enable);
  209. static void set_shader_cache_save_compressed_zstd(bool p_enable);
  210. static void set_shader_cache_save_debug(bool p_enable);
  211. RS::ShaderNativeSourceCode version_get_native_source_code(RID p_version);
  212. void initialize(const String &p_general_defines = "", int p_base_texture_index = 0);
  213. virtual ~ShaderGLES3();
  214. };
  215. #endif // GLES3_ENABLED
  216. #endif // SHADER_GLES3_H