shader_rd.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*************************************************************************/
  2. /* shader_rd.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_RD_H
  31. #define SHADER_RD_H
  32. #include "core/os/mutex.h"
  33. #include "core/templates/hash_map.h"
  34. #include "core/templates/map.h"
  35. #include "core/templates/rid_owner.h"
  36. #include "core/variant/variant.h"
  37. #include "servers/rendering_server.h"
  38. #include <stdio.h>
  39. /**
  40. @author Juan Linietsky <[email protected]>
  41. */
  42. class ShaderRD {
  43. //versions
  44. CharString general_defines;
  45. Vector<CharString> variant_defines;
  46. Vector<bool> variants_enabled;
  47. struct Version {
  48. CharString uniforms;
  49. CharString vertex_globals;
  50. CharString vertex_code;
  51. CharString compute_globals;
  52. CharString compute_code;
  53. CharString fragment_light;
  54. CharString fragment_globals;
  55. CharString fragment_code;
  56. Vector<CharString> custom_defines;
  57. RID *variants; //same size as version defines
  58. bool valid;
  59. bool dirty;
  60. bool initialize_needed;
  61. };
  62. Mutex variant_set_mutex;
  63. void _compile_variant(uint32_t p_variant, Version *p_version);
  64. void _clear_version(Version *p_version);
  65. void _compile_version(Version *p_version);
  66. RID_Owner<Version> version_owner;
  67. CharString fragment_codev; //for version and extensions
  68. CharString fragment_code0;
  69. CharString fragment_code1;
  70. CharString fragment_code2;
  71. CharString fragment_code3;
  72. CharString fragment_code4;
  73. CharString vertex_codev; //for version and extensions
  74. CharString vertex_code0;
  75. CharString vertex_code1;
  76. CharString vertex_code2;
  77. CharString vertex_code3;
  78. bool is_compute = false;
  79. CharString compute_codev; //for version and extensions
  80. CharString compute_code0;
  81. CharString compute_code1;
  82. CharString compute_code2;
  83. CharString compute_code3;
  84. const char *name;
  85. CharString base_compute_defines;
  86. protected:
  87. ShaderRD();
  88. void setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name);
  89. public:
  90. RID version_create();
  91. void version_set_code(RID p_version, const String &p_uniforms, const String &p_vertex_globals, const String &p_vertex_code, const String &p_fragment_globals, const String &p_fragment_light, const String &p_fragment_code, const Vector<String> &p_custom_defines);
  92. void version_set_compute_code(RID p_version, const String &p_uniforms, const String &p_compute_globals, const String &p_compute_code, const Vector<String> &p_custom_defines);
  93. _FORCE_INLINE_ RID version_get_shader(RID p_version, int p_variant) {
  94. ERR_FAIL_INDEX_V(p_variant, variant_defines.size(), RID());
  95. ERR_FAIL_COND_V(!variants_enabled[p_variant], RID());
  96. Version *version = version_owner.getornull(p_version);
  97. ERR_FAIL_COND_V(!version, RID());
  98. if (version->dirty) {
  99. _compile_version(version);
  100. }
  101. if (!version->valid) {
  102. return RID();
  103. }
  104. return version->variants[p_variant];
  105. }
  106. bool version_is_valid(RID p_version);
  107. bool version_free(RID p_version);
  108. void set_variant_enabled(int p_variant, bool p_enabled);
  109. bool is_variant_enabled(int p_variant) const;
  110. RS::ShaderNativeSourceCode version_get_native_source_code(RID p_version);
  111. void initialize(const Vector<String> &p_variant_defines, const String &p_general_defines = "");
  112. virtual ~ShaderRD();
  113. };
  114. #endif