shader_gles3.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*************************************************************************/
  2. /* shader_gles3.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_OPENGL_H
  31. #define SHADER_OPENGL_H
  32. #include "core/os/mutex.h"
  33. #include "core/string/string_builder.h"
  34. #include "core/templates/hash_map.h"
  35. #include "core/templates/local_vector.h"
  36. #include "core/templates/map.h"
  37. #include "core/templates/rid_owner.h"
  38. #include "core/variant/variant.h"
  39. #include "servers/rendering_server.h"
  40. #ifdef GLES3_ENABLED
  41. // This must come first to avoid windows.h mess
  42. #include "platform_config.h"
  43. #ifndef OPENGL_INCLUDE_H
  44. #include <GLES3/gl3.h>
  45. #else
  46. #include OPENGL_INCLUDE_H
  47. #endif
  48. #include <stdio.h>
  49. /**
  50. @author Juan Linietsky <[email protected]>
  51. */
  52. class ShaderGLES3 {
  53. protected:
  54. struct TexUnitPair {
  55. const char *name;
  56. int index;
  57. };
  58. struct UBOPair {
  59. const char *name;
  60. int index;
  61. };
  62. struct Specialization {
  63. const char *name;
  64. bool default_value = false;
  65. };
  66. private:
  67. //versions
  68. CharString general_defines;
  69. // A version is a high-level construct which is a combination of built-in and user-defined shader code
  70. // Variants use #idefs to toggle behaviour on and off to change behaviour of the shader
  71. // Specializations use #ifdefs to toggle behaviour on and off for performance, on supporting hardware, they will compile a version with everything enabled, and then compile more copies to improve performance
  72. // Use specializations to enable and disabled advanced features, use variants to toggle behaviour when different data may be used (e.g. using a samplerArray vs a sampler)
  73. struct Version {
  74. Vector<StringName> texture_uniforms;
  75. CharString uniforms;
  76. CharString vertex_globals;
  77. CharString fragment_globals;
  78. Map<StringName, CharString> code_sections;
  79. Vector<CharString> custom_defines;
  80. struct Specialization {
  81. GLuint id;
  82. GLuint vert_id;
  83. GLuint frag_id;
  84. LocalVector<GLint> uniform_location;
  85. LocalVector<GLint> texture_uniform_locations;
  86. Map<StringName, GLint> custom_uniform_locations;
  87. bool build_queued = false;
  88. bool ok = false;
  89. Specialization() {
  90. id = 0;
  91. vert_id = 0;
  92. frag_id = 0;
  93. }
  94. };
  95. LocalVector<OAHashMap<uint64_t, Specialization>> variants;
  96. };
  97. Mutex variant_set_mutex;
  98. void _compile_specialization(Version::Specialization &spec, uint32_t p_variant, Version *p_version, uint64_t p_specialization);
  99. void _clear_version(Version *p_version);
  100. void _initialize_version(Version *p_version);
  101. RID_Owner<Version> version_owner;
  102. struct StageTemplate {
  103. struct Chunk {
  104. enum Type {
  105. TYPE_MATERIAL_UNIFORMS,
  106. TYPE_VERTEX_GLOBALS,
  107. TYPE_FRAGMENT_GLOBALS,
  108. TYPE_CODE,
  109. TYPE_TEXT
  110. };
  111. Type type;
  112. StringName code;
  113. CharString text;
  114. };
  115. LocalVector<Chunk> chunks;
  116. };
  117. String name;
  118. String base_sha256;
  119. static String shader_cache_dir;
  120. static bool shader_cache_cleanup_on_start;
  121. static bool shader_cache_save_compressed;
  122. static bool shader_cache_save_compressed_zstd;
  123. static bool shader_cache_save_debug;
  124. bool shader_cache_dir_valid = false;
  125. GLint max_image_units;
  126. enum StageType {
  127. STAGE_TYPE_VERTEX,
  128. STAGE_TYPE_FRAGMENT,
  129. STAGE_TYPE_MAX,
  130. };
  131. StageTemplate stage_templates[STAGE_TYPE_MAX];
  132. void _build_variant_code(StringBuilder &p_builder, uint32_t p_variant, const Version *p_version, const StageTemplate &p_template, uint64_t p_specialization);
  133. void _add_stage(const char *p_code, StageType p_stage_type);
  134. String _version_get_sha1(Version *p_version) const;
  135. bool _load_from_cache(Version *p_version);
  136. void _save_to_cache(Version *p_version);
  137. const char **uniform_names = nullptr;
  138. int uniform_count = 0;
  139. const UBOPair *ubo_pairs = nullptr;
  140. int ubo_count = 0;
  141. const TexUnitPair *texunit_pairs = nullptr;
  142. int texunit_pair_count = 0;
  143. int specialization_count = 0;
  144. const Specialization *specializations = nullptr;
  145. uint64_t specialization_default_mask = 0;
  146. const char **variant_defines = nullptr;
  147. int variant_count = 0;
  148. int base_texture_index = 0;
  149. Version::Specialization *current_shader = nullptr;
  150. protected:
  151. ShaderGLES3();
  152. 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_texture_count, const TexUnitPair *p_tex_units, int p_specialization_count, const Specialization *p_specializations, int p_variant_count, const char **p_variants);
  153. _FORCE_INLINE_ void _version_bind_shader(RID p_version, int p_variant, uint64_t p_specialization) {
  154. ERR_FAIL_INDEX(p_variant, variant_count);
  155. Version *version = version_owner.get_or_null(p_version);
  156. ERR_FAIL_COND(!version);
  157. if (version->variants.size() == 0) {
  158. _initialize_version(version); //may lack initialization
  159. }
  160. Version::Specialization *spec = version->variants[p_variant].lookup_ptr(p_specialization);
  161. if (!spec) {
  162. if (false) {
  163. // Queue load this specialization and use defaults in the meantime (TODO)
  164. spec = version->variants[p_variant].lookup_ptr(specialization_default_mask);
  165. } else {
  166. // Compile on the spot
  167. Version::Specialization s;
  168. _compile_specialization(s, p_variant, version, p_specialization);
  169. version->variants[p_variant].insert(p_specialization, s);
  170. spec = version->variants[p_variant].lookup_ptr(p_specialization);
  171. }
  172. } else if (spec->build_queued) {
  173. // Still queued, wait
  174. spec = version->variants[p_variant].lookup_ptr(specialization_default_mask);
  175. }
  176. ERR_FAIL_COND(!spec); // Should never happen
  177. ERR_FAIL_COND(!spec->ok); // Should never happen
  178. glUseProgram(spec->id);
  179. current_shader = spec;
  180. }
  181. _FORCE_INLINE_ int _version_get_uniform(int p_which, RID p_version, int p_variant, uint64_t p_specialization) {
  182. ERR_FAIL_INDEX_V(p_which, uniform_count, -1);
  183. Version *version = version_owner.get_or_null(p_version);
  184. ERR_FAIL_COND_V(!version, -1);
  185. return version->variants[p_variant].lookup_ptr(p_specialization)->uniform_location[p_which];
  186. }
  187. virtual void _init() = 0;
  188. public:
  189. RID version_create();
  190. void version_set_code(RID p_version, const Map<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);
  191. bool version_is_valid(RID p_version);
  192. bool version_free(RID p_version);
  193. static void set_shader_cache_dir(const String &p_dir);
  194. static void set_shader_cache_save_compressed(bool p_enable);
  195. static void set_shader_cache_save_compressed_zstd(bool p_enable);
  196. static void set_shader_cache_save_debug(bool p_enable);
  197. RS::ShaderNativeSourceCode version_get_native_source_code(RID p_version);
  198. void initialize(const String &p_general_defines = "", int p_base_texture_index = 0);
  199. virtual ~ShaderGLES3();
  200. };
  201. #endif // SHADER_OPENGL_H
  202. #endif