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