shader_gles3.h 9.3 KB

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