shader_gles3.h 9.5 KB

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