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