register_types.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "core/os/os.h"
  33. #include "shader_compile.h"
  34. GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wshadow")
  35. #include <glslang/Public/ResourceLimits.h>
  36. #include <glslang/Public/ShaderLang.h>
  37. #include <glslang/SPIRV/GlslangToSpv.h>
  38. GODOT_GCC_WARNING_POP
  39. Vector<uint8_t> compile_glslang_shader(RenderingDeviceCommons::ShaderStage p_stage, const String &p_source_code, RenderingDeviceCommons::ShaderLanguageVersion p_language_version, RenderingDeviceCommons::ShaderSpirvVersion p_spirv_version, String *r_error) {
  40. Vector<uint8_t> ret;
  41. EShLanguage stages[RenderingDeviceCommons::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. // The enum values can be converted directly.
  50. glslang::EShTargetClientVersion ClientVersion = (glslang::EShTargetClientVersion)p_language_version;
  51. glslang::EShTargetLanguageVersion TargetVersion = (glslang::EShTargetLanguageVersion)p_spirv_version;
  52. glslang::TShader shader(stages[p_stage]);
  53. CharString cs = p_source_code.utf8();
  54. const char *cs_strings = cs.get_data();
  55. std::string preamble = "";
  56. shader.setStrings(&cs_strings, 1);
  57. shader.setEnvInput(glslang::EShSourceGlsl, stages[p_stage], glslang::EShClientVulkan, ClientInputSemanticsVersion);
  58. shader.setEnvClient(glslang::EShClientVulkan, ClientVersion);
  59. shader.setEnvTarget(glslang::EShTargetSpv, TargetVersion);
  60. if (!preamble.empty()) {
  61. shader.setPreamble(preamble.c_str());
  62. }
  63. bool generate_spirv_debug_info = Engine::get_singleton()->is_generate_spirv_debug_info_enabled();
  64. #ifdef D3D12_ENABLED
  65. if (OS::get_singleton()->get_current_rendering_driver_name() == "d3d12") {
  66. // SPIRV to DXIL conversion does not support debug info.
  67. generate_spirv_debug_info = false;
  68. }
  69. #endif
  70. EShMessages messages = (EShMessages)(EShMsgSpvRules | EShMsgVulkanRules);
  71. if (generate_spirv_debug_info) {
  72. messages = (EShMessages)(messages | EShMsgDebugInfo);
  73. }
  74. const int DefaultVersion = 100;
  75. //parse
  76. if (!shader.parse(GetDefaultResources(), DefaultVersion, false, messages)) {
  77. if (r_error) {
  78. (*r_error) = "Failed parse:\n";
  79. (*r_error) += shader.getInfoLog();
  80. (*r_error) += "\n";
  81. (*r_error) += shader.getInfoDebugLog();
  82. }
  83. return ret;
  84. }
  85. //link
  86. glslang::TProgram program;
  87. program.addShader(&shader);
  88. if (!program.link(messages)) {
  89. if (r_error) {
  90. (*r_error) = "Failed link:\n";
  91. (*r_error) += program.getInfoLog();
  92. (*r_error) += "\n";
  93. (*r_error) += program.getInfoDebugLog();
  94. }
  95. return ret;
  96. }
  97. std::vector<uint32_t> SpirV;
  98. spv::SpvBuildLogger logger;
  99. glslang::SpvOptions spvOptions;
  100. if (generate_spirv_debug_info) {
  101. spvOptions.generateDebugInfo = true;
  102. spvOptions.emitNonSemanticShaderDebugInfo = true;
  103. spvOptions.emitNonSemanticShaderDebugSource = true;
  104. }
  105. glslang::GlslangToSpv(*program.getIntermediate(stages[p_stage]), SpirV, &logger, &spvOptions);
  106. ret.resize(SpirV.size() * sizeof(uint32_t));
  107. {
  108. uint8_t *w = ret.ptrw();
  109. memcpy(w, &SpirV[0], SpirV.size() * sizeof(uint32_t));
  110. }
  111. return ret;
  112. }
  113. void initialize_glslang_module(ModuleInitializationLevel p_level) {
  114. if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) {
  115. return;
  116. }
  117. // Initialize in case it's not initialized. This is done once per thread
  118. // and it's safe to call multiple times.
  119. glslang::InitializeProcess();
  120. }
  121. void uninitialize_glslang_module(ModuleInitializationLevel p_level) {
  122. if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) {
  123. return;
  124. }
  125. glslang::FinalizeProcess();
  126. }