register_types.cpp 8.9 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) 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. #include "register_types.h"
  31. #include "core/config/engine.h"
  32. #include "servers/rendering/rendering_device.h"
  33. #include <glslang/Public/ResourceLimits.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 RDD::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. glslang::EShTargetClientVersion ClientVersion = glslang::EShTargetVulkan_1_2;
  49. glslang::EShTargetLanguageVersion TargetVersion = glslang::EShTargetSpv_1_5;
  50. if (capabilities.device_family == RDD::DEVICE_VULKAN) {
  51. if (capabilities.version_major == 1 && capabilities.version_minor == 0) {
  52. ClientVersion = glslang::EShTargetVulkan_1_0;
  53. TargetVersion = glslang::EShTargetSpv_1_0;
  54. } else if (capabilities.version_major == 1 && capabilities.version_minor == 1) {
  55. ClientVersion = glslang::EShTargetVulkan_1_1;
  56. TargetVersion = glslang::EShTargetSpv_1_3;
  57. } else {
  58. // use defaults
  59. }
  60. } else if (capabilities.device_family == RDD::DEVICE_DIRECTX) {
  61. // NIR-DXIL is Vulkan 1.1-conformant.
  62. ClientVersion = glslang::EShTargetVulkan_1_1;
  63. // The SPIR-V part of Mesa supports 1.6, but:
  64. // - SPIRV-Reflect won't be able to parse the compute workgroup size.
  65. // - We want to play it safe with NIR-DXIL.
  66. TargetVersion = glslang::EShTargetSpv_1_3;
  67. } else if (capabilities.device_family == RDD::DEVICE_METAL) {
  68. ClientVersion = glslang::EShTargetVulkan_1_1;
  69. TargetVersion = glslang::EShTargetSpv_1_6;
  70. } else {
  71. // once we support other backends we'll need to do something here
  72. if (r_error) {
  73. (*r_error) = "GLSLANG - Unsupported device family";
  74. }
  75. return ret;
  76. }
  77. glslang::TShader shader(stages[p_stage]);
  78. CharString cs = p_source_code.ascii();
  79. const char *cs_strings = cs.get_data();
  80. std::string preamble = "";
  81. shader.setStrings(&cs_strings, 1);
  82. shader.setEnvInput(glslang::EShSourceGlsl, stages[p_stage], glslang::EShClientVulkan, ClientInputSemanticsVersion);
  83. shader.setEnvClient(glslang::EShClientVulkan, ClientVersion);
  84. shader.setEnvTarget(glslang::EShTargetSpv, TargetVersion);
  85. {
  86. uint32_t stage_bit = 1 << p_stage;
  87. uint32_t subgroup_in_shaders = uint32_t(p_render_device->limit_get(RD::LIMIT_SUBGROUP_IN_SHADERS));
  88. uint32_t subgroup_operations = uint32_t(p_render_device->limit_get(RD::LIMIT_SUBGROUP_OPERATIONS));
  89. if ((subgroup_in_shaders & stage_bit) == stage_bit) {
  90. // stage supports subgroups
  91. preamble += "#define has_GL_KHR_shader_subgroup_basic 1\n";
  92. if (subgroup_operations & RenderingDevice::SUBGROUP_VOTE_BIT) {
  93. preamble += "#define has_GL_KHR_shader_subgroup_vote 1\n";
  94. }
  95. if (subgroup_operations & RenderingDevice::SUBGROUP_ARITHMETIC_BIT) {
  96. preamble += "#define has_GL_KHR_shader_subgroup_arithmetic 1\n";
  97. }
  98. if (subgroup_operations & RenderingDevice::SUBGROUP_BALLOT_BIT) {
  99. preamble += "#define has_GL_KHR_shader_subgroup_ballot 1\n";
  100. }
  101. if (subgroup_operations & RenderingDevice::SUBGROUP_SHUFFLE_BIT) {
  102. preamble += "#define has_GL_KHR_shader_subgroup_shuffle 1\n";
  103. }
  104. if (subgroup_operations & RenderingDevice::SUBGROUP_SHUFFLE_RELATIVE_BIT) {
  105. preamble += "#define has_GL_KHR_shader_subgroup_shuffle_relative 1\n";
  106. }
  107. if (subgroup_operations & RenderingDevice::SUBGROUP_CLUSTERED_BIT) {
  108. preamble += "#define has_GL_KHR_shader_subgroup_clustered 1\n";
  109. }
  110. if (subgroup_operations & RenderingDevice::SUBGROUP_QUAD_BIT) {
  111. preamble += "#define has_GL_KHR_shader_subgroup_quad 1\n";
  112. }
  113. }
  114. }
  115. if (p_render_device->has_feature(RD::SUPPORTS_MULTIVIEW)) {
  116. preamble += "#define has_VK_KHR_multiview 1\n";
  117. }
  118. if (!preamble.empty()) {
  119. shader.setPreamble(preamble.c_str());
  120. }
  121. EShMessages messages = (EShMessages)(EShMsgSpvRules | EShMsgVulkanRules);
  122. if (Engine::get_singleton()->is_generate_spirv_debug_info_enabled()) {
  123. messages = (EShMessages)(messages | EShMsgDebugInfo);
  124. }
  125. const int DefaultVersion = 100;
  126. //parse
  127. if (!shader.parse(GetDefaultResources(), DefaultVersion, false, messages)) {
  128. if (r_error) {
  129. (*r_error) = "Failed parse:\n";
  130. (*r_error) += shader.getInfoLog();
  131. (*r_error) += "\n";
  132. (*r_error) += shader.getInfoDebugLog();
  133. }
  134. return ret;
  135. }
  136. //link
  137. glslang::TProgram program;
  138. program.addShader(&shader);
  139. if (!program.link(messages)) {
  140. if (r_error) {
  141. (*r_error) = "Failed link:\n";
  142. (*r_error) += program.getInfoLog();
  143. (*r_error) += "\n";
  144. (*r_error) += program.getInfoDebugLog();
  145. }
  146. return ret;
  147. }
  148. std::vector<uint32_t> SpirV;
  149. spv::SpvBuildLogger logger;
  150. glslang::SpvOptions spvOptions;
  151. if (Engine::get_singleton()->is_generate_spirv_debug_info_enabled()) {
  152. spvOptions.generateDebugInfo = true;
  153. spvOptions.emitNonSemanticShaderDebugInfo = true;
  154. spvOptions.emitNonSemanticShaderDebugSource = true;
  155. }
  156. glslang::GlslangToSpv(*program.getIntermediate(stages[p_stage]), SpirV, &logger, &spvOptions);
  157. ret.resize(SpirV.size() * sizeof(uint32_t));
  158. {
  159. uint8_t *w = ret.ptrw();
  160. memcpy(w, &SpirV[0], SpirV.size() * sizeof(uint32_t));
  161. }
  162. return ret;
  163. }
  164. static String _get_cache_key_function_glsl(const RenderingDevice *p_render_device) {
  165. const RenderingDeviceDriver::Capabilities &capabilities = p_render_device->get_device_capabilities();
  166. String version;
  167. 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)) + " , debug=" + itos(Engine::get_singleton()->is_generate_spirv_debug_info_enabled());
  168. return version;
  169. }
  170. void initialize_glslang_module(ModuleInitializationLevel p_level) {
  171. if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) {
  172. return;
  173. }
  174. // Initialize in case it's not initialized. This is done once per thread
  175. // and it's safe to call multiple times.
  176. glslang::InitializeProcess();
  177. RenderingDevice::shader_set_compile_to_spirv_function(_compile_shader_glsl);
  178. RenderingDevice::shader_set_get_cache_key_function(_get_cache_key_function_glsl);
  179. }
  180. void uninitialize_glslang_module(ModuleInitializationLevel p_level) {
  181. if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) {
  182. return;
  183. }
  184. glslang::FinalizeProcess();
  185. }