shader_gles3.h 9.3 KB

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