register_types.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*************************************************************************/
  2. /* register_types.cpp */
  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. #include "register_types.h"
  31. #include "servers/rendering/rendering_device.h"
  32. #include "glslang_resource_limits.h"
  33. #include <glslang/Include/Types.h>
  34. #include <glslang/Public/ShaderLang.h>
  35. #include <glslang/SPIRV/GlslangToSpv.h>
  36. static Vector<uint8_t> _compile_shader_glsl(RenderingDevice::ShaderStage p_stage, const String &p_source_code, RenderingDevice::ShaderLanguage p_language, String *r_error, const RenderingDevice *p_render_device) {
  37. const RD::Capabilities *capabilities = p_render_device->get_device_capabilities();
  38. Vector<uint8_t> ret;
  39. ERR_FAIL_COND_V(p_language == RenderingDevice::SHADER_LANGUAGE_HLSL, ret);
  40. EShLanguage stages[RenderingDevice::SHADER_STAGE_MAX] = {
  41. EShLangVertex,
  42. EShLangFragment,
  43. EShLangTessControl,
  44. EShLangTessEvaluation,
  45. EShLangCompute
  46. };
  47. int ClientInputSemanticsVersion = 100; // maps to, say, #define VULKAN 100
  48. bool check_subgroup_support = true; // assume we support subgroups
  49. glslang::EShTargetClientVersion ClientVersion = glslang::EShTargetVulkan_1_2;
  50. glslang::EShTargetLanguageVersion TargetVersion = glslang::EShTargetSpv_1_5;
  51. glslang::TShader::ForbidIncluder includer;
  52. if (capabilities->device_family == RenderingDevice::DeviceFamily::DEVICE_VULKAN) {
  53. if (capabilities->version_major == 1 && capabilities->version_minor == 0) {
  54. ClientVersion = glslang::EShTargetVulkan_1_0;
  55. TargetVersion = glslang::EShTargetSpv_1_0;
  56. check_subgroup_support = false; // subgroups are not supported in Vulkan 1.0
  57. } else if (capabilities->version_major == 1 && capabilities->version_minor == 1) {
  58. ClientVersion = glslang::EShTargetVulkan_1_1;
  59. TargetVersion = glslang::EShTargetSpv_1_3;
  60. } else {
  61. // use defaults
  62. }
  63. } else {
  64. // once we support other backends we'll need to do something here
  65. if (r_error) {
  66. (*r_error) = "GLSLANG - Unsupported device family";
  67. }
  68. return ret;
  69. }
  70. glslang::TShader shader(stages[p_stage]);
  71. CharString cs = p_source_code.ascii();
  72. const char *cs_strings = cs.get_data();
  73. std::string preamble = "";
  74. shader.setStrings(&cs_strings, 1);
  75. shader.setEnvInput(glslang::EShSourceGlsl, stages[p_stage], glslang::EShClientVulkan, ClientInputSemanticsVersion);
  76. shader.setEnvClient(glslang::EShClientVulkan, ClientVersion);
  77. shader.setEnvTarget(glslang::EShTargetSpv, TargetVersion);
  78. if (check_subgroup_support) {
  79. uint32_t stage_bit = 1 << p_stage;
  80. uint32_t subgroup_in_shaders = uint32_t(p_render_device->limit_get(RD::LIMIT_SUBGROUP_IN_SHADERS));
  81. uint32_t subgroup_operations = uint32_t(p_render_device->limit_get(RD::LIMIT_SUBGROUP_OPERATIONS));
  82. if ((subgroup_in_shaders & stage_bit) == stage_bit) {
  83. // stage supports subgroups
  84. preamble += "#define has_GL_KHR_shader_subgroup_basic 1\n";
  85. if (subgroup_operations & RenderingDevice::SUBGROUP_VOTE_BIT) {
  86. preamble += "#define has_GL_KHR_shader_subgroup_vote 1\n";
  87. }
  88. if (subgroup_operations & RenderingDevice::SUBGROUP_ARITHMETIC_BIT) {
  89. preamble += "#define has_GL_KHR_shader_subgroup_arithmetic 1\n";
  90. }
  91. if (subgroup_operations & RenderingDevice::SUBGROUP_BALLOT_BIT) {
  92. preamble += "#define has_GL_KHR_shader_subgroup_ballot 1\n";
  93. }
  94. if (subgroup_operations & RenderingDevice::SUBGROUP_SHUFFLE_BIT) {
  95. preamble += "#define has_GL_KHR_shader_subgroup_shuffle 1\n";
  96. }
  97. if (subgroup_operations & RenderingDevice::SUBGROUP_SHUFFLE_RELATIVE_BIT) {
  98. preamble += "#define has_GL_KHR_shader_subgroup_shuffle_relative 1\n";
  99. }
  100. if (subgroup_operations & RenderingDevice::SUBGROUP_CLUSTERED_BIT) {
  101. preamble += "#define has_GL_KHR_shader_subgroup_clustered 1\n";
  102. }
  103. if (subgroup_operations & RenderingDevice::SUBGROUP_QUAD_BIT) {
  104. preamble += "#define has_GL_KHR_shader_subgroup_quad 1\n";
  105. }
  106. }
  107. }
  108. if (p_render_device->has_feature(RD::SUPPORTS_MULTIVIEW)) {
  109. preamble += "#define has_VK_KHR_multiview 1\n";
  110. }
  111. if (!preamble.empty()) {
  112. shader.setPreamble(preamble.c_str());
  113. }
  114. EShMessages messages = (EShMessages)(EShMsgSpvRules | EShMsgVulkanRules);
  115. const int DefaultVersion = 100;
  116. std::string pre_processed_code;
  117. //preprocess
  118. if (!shader.preprocess(&DefaultTBuiltInResource, DefaultVersion, ENoProfile, false, false, messages, &pre_processed_code, includer)) {
  119. if (r_error) {
  120. (*r_error) = "Failed pre-process:\n";
  121. (*r_error) += shader.getInfoLog();
  122. (*r_error) += "\n";
  123. (*r_error) += shader.getInfoDebugLog();
  124. }
  125. return ret;
  126. }
  127. //set back..
  128. cs_strings = pre_processed_code.c_str();
  129. shader.setStrings(&cs_strings, 1);
  130. //parse
  131. if (!shader.parse(&DefaultTBuiltInResource, DefaultVersion, false, messages)) {
  132. if (r_error) {
  133. (*r_error) = "Failed parse:\n";
  134. (*r_error) += shader.getInfoLog();
  135. (*r_error) += "\n";
  136. (*r_error) += shader.getInfoDebugLog();
  137. }
  138. return ret;
  139. }
  140. //link
  141. glslang::TProgram program;
  142. program.addShader(&shader);
  143. if (!program.link(messages)) {
  144. if (r_error) {
  145. (*r_error) = "Failed link:\n";
  146. (*r_error) += program.getInfoLog();
  147. (*r_error) += "\n";
  148. (*r_error) += program.getInfoDebugLog();
  149. }
  150. return ret;
  151. }
  152. std::vector<uint32_t> SpirV;
  153. spv::SpvBuildLogger logger;
  154. glslang::SpvOptions spvOptions;
  155. glslang::GlslangToSpv(*program.getIntermediate(stages[p_stage]), SpirV, &logger, &spvOptions);
  156. ret.resize(SpirV.size() * sizeof(uint32_t));
  157. {
  158. uint8_t *w = ret.ptrw();
  159. memcpy(w, &SpirV[0], SpirV.size() * sizeof(uint32_t));
  160. }
  161. return ret;
  162. }
  163. static String _get_cache_key_function_glsl(const RenderingDevice *p_render_device) {
  164. const RD::Capabilities *capabilities = p_render_device->get_device_capabilities();
  165. String version;
  166. version = "SpirVGen=" + itos(glslang::GetSpirvGeneratorVersion()) + ", major=" + itos(capabilities->version_major) + ", minor=" + itos(capabilities->version_minor) + " , subgroup_size=" + itos(p_render_device->limit_get(RD::LIMIT_SUBGROUP_SIZE)) + " , subgroup_ops=" + itos(p_render_device->limit_get(RD::LIMIT_SUBGROUP_OPERATIONS)) + " , subgroup_in_shaders=" + itos(p_render_device->limit_get(RD::LIMIT_SUBGROUP_IN_SHADERS));
  167. return version;
  168. }
  169. void initialize_glslang_module(ModuleInitializationLevel p_level) {
  170. if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) {
  171. return;
  172. }
  173. // Initialize in case it's not initialized. This is done once per thread
  174. // and it's safe to call multiple times.
  175. glslang::InitializeProcess();
  176. RenderingDevice::shader_set_compile_to_spirv_function(_compile_shader_glsl);
  177. RenderingDevice::shader_set_get_cache_key_function(_get_cache_key_function_glsl);
  178. }
  179. void uninitialize_glslang_module(ModuleInitializationLevel p_level) {
  180. if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) {
  181. return;
  182. }
  183. glslang::FinalizeProcess();
  184. }