register_types.cpp 8.4 KB

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