2019-07-28 22:58:32 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* register_types.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
2020-02-11 13:01:43 +00:00
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
2019-07-28 22:58:32 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#include "register_types.h"
|
2019-09-27 07:39:53 +00:00
|
|
|
|
2023-08-12 12:56:49 +00:00
|
|
|
#include "core/config/engine.h"
|
2023-06-13 14:56:21 +00:00
|
|
|
#include "servers/rendering/rendering_device.h"
|
|
|
|
|
2023-09-01 09:11:12 +00:00
|
|
|
#include <glslang/Public/ResourceLimits.h>
|
2019-09-27 07:39:53 +00:00
|
|
|
#include <glslang/Public/ShaderLang.h>
|
2022-01-12 11:28:43 +00:00
|
|
|
#include <glslang/SPIRV/GlslangToSpv.h>
|
2019-07-28 22:58:32 +00:00
|
|
|
|
2022-02-11 11:33:54 +00:00
|
|
|
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) {
|
2023-12-19 17:57:56 +00:00
|
|
|
const RDD::Capabilities &capabilities = p_render_device->get_device_capabilities();
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> ret;
|
2019-07-28 22:58:32 +00:00
|
|
|
|
2019-09-27 07:39:53 +00:00
|
|
|
ERR_FAIL_COND_V(p_language == RenderingDevice::SHADER_LANGUAGE_HLSL, ret);
|
2019-07-28 22:58:32 +00:00
|
|
|
|
|
|
|
EShLanguage stages[RenderingDevice::SHADER_STAGE_MAX] = {
|
|
|
|
EShLangVertex,
|
|
|
|
EShLangFragment,
|
|
|
|
EShLangTessControl,
|
|
|
|
EShLangTessEvaluation,
|
|
|
|
EShLangCompute
|
|
|
|
};
|
|
|
|
|
|
|
|
int ClientInputSemanticsVersion = 100; // maps to, say, #define VULKAN 100
|
|
|
|
|
2021-03-22 10:04:55 +00:00
|
|
|
glslang::EShTargetClientVersion ClientVersion = glslang::EShTargetVulkan_1_2;
|
|
|
|
glslang::EShTargetLanguageVersion TargetVersion = glslang::EShTargetSpv_1_5;
|
2019-07-28 22:58:32 +00:00
|
|
|
|
2023-12-19 17:57:56 +00:00
|
|
|
if (capabilities.device_family == RDD::DEVICE_VULKAN) {
|
|
|
|
if (capabilities.version_major == 1 && capabilities.version_minor == 0) {
|
2021-03-22 10:04:55 +00:00
|
|
|
ClientVersion = glslang::EShTargetVulkan_1_0;
|
|
|
|
TargetVersion = glslang::EShTargetSpv_1_0;
|
2023-12-19 17:57:56 +00:00
|
|
|
} else if (capabilities.version_major == 1 && capabilities.version_minor == 1) {
|
2021-03-22 10:04:55 +00:00
|
|
|
ClientVersion = glslang::EShTargetVulkan_1_1;
|
|
|
|
TargetVersion = glslang::EShTargetSpv_1_3;
|
|
|
|
} else {
|
|
|
|
// use defaults
|
|
|
|
}
|
2023-12-19 17:57:56 +00:00
|
|
|
} else if (capabilities.device_family == RDD::DEVICE_DIRECTX) {
|
2023-01-09 15:56:16 +00:00
|
|
|
// NIR-DXIL is Vulkan 1.1-conformant.
|
|
|
|
ClientVersion = glslang::EShTargetVulkan_1_1;
|
|
|
|
// The SPIR-V part of Mesa supports 1.6, but:
|
|
|
|
// - SPIRV-Reflect won't be able to parse the compute workgroup size.
|
|
|
|
// - We want to play it safe with NIR-DXIL.
|
|
|
|
TargetVersion = glslang::EShTargetSpv_1_3;
|
2021-03-22 10:04:55 +00:00
|
|
|
} else {
|
|
|
|
// once we support other backends we'll need to do something here
|
|
|
|
if (r_error) {
|
|
|
|
(*r_error) = "GLSLANG - Unsupported device family";
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-28 22:58:32 +00:00
|
|
|
glslang::TShader shader(stages[p_stage]);
|
|
|
|
CharString cs = p_source_code.ascii();
|
|
|
|
const char *cs_strings = cs.get_data();
|
2021-03-31 10:31:27 +00:00
|
|
|
std::string preamble = "";
|
2019-07-28 22:58:32 +00:00
|
|
|
|
|
|
|
shader.setStrings(&cs_strings, 1);
|
|
|
|
shader.setEnvInput(glslang::EShSourceGlsl, stages[p_stage], glslang::EShClientVulkan, ClientInputSemanticsVersion);
|
2021-03-22 10:04:55 +00:00
|
|
|
shader.setEnvClient(glslang::EShClientVulkan, ClientVersion);
|
2019-07-28 22:58:32 +00:00
|
|
|
shader.setEnvTarget(glslang::EShTargetSpv, TargetVersion);
|
|
|
|
|
2022-10-17 14:51:33 +00:00
|
|
|
{
|
2021-03-31 10:31:27 +00:00
|
|
|
uint32_t stage_bit = 1 << p_stage;
|
|
|
|
|
2022-02-11 11:33:54 +00:00
|
|
|
uint32_t subgroup_in_shaders = uint32_t(p_render_device->limit_get(RD::LIMIT_SUBGROUP_IN_SHADERS));
|
|
|
|
uint32_t subgroup_operations = uint32_t(p_render_device->limit_get(RD::LIMIT_SUBGROUP_OPERATIONS));
|
|
|
|
if ((subgroup_in_shaders & stage_bit) == stage_bit) {
|
2021-03-31 10:31:27 +00:00
|
|
|
// stage supports subgroups
|
|
|
|
preamble += "#define has_GL_KHR_shader_subgroup_basic 1\n";
|
2022-02-11 11:33:54 +00:00
|
|
|
if (subgroup_operations & RenderingDevice::SUBGROUP_VOTE_BIT) {
|
2021-03-31 10:31:27 +00:00
|
|
|
preamble += "#define has_GL_KHR_shader_subgroup_vote 1\n";
|
|
|
|
}
|
2022-02-11 11:33:54 +00:00
|
|
|
if (subgroup_operations & RenderingDevice::SUBGROUP_ARITHMETIC_BIT) {
|
2021-03-31 10:31:27 +00:00
|
|
|
preamble += "#define has_GL_KHR_shader_subgroup_arithmetic 1\n";
|
|
|
|
}
|
2022-02-11 11:33:54 +00:00
|
|
|
if (subgroup_operations & RenderingDevice::SUBGROUP_BALLOT_BIT) {
|
2021-03-31 10:31:27 +00:00
|
|
|
preamble += "#define has_GL_KHR_shader_subgroup_ballot 1\n";
|
|
|
|
}
|
2022-02-11 11:33:54 +00:00
|
|
|
if (subgroup_operations & RenderingDevice::SUBGROUP_SHUFFLE_BIT) {
|
2021-03-31 10:31:27 +00:00
|
|
|
preamble += "#define has_GL_KHR_shader_subgroup_shuffle 1\n";
|
|
|
|
}
|
2022-02-11 11:33:54 +00:00
|
|
|
if (subgroup_operations & RenderingDevice::SUBGROUP_SHUFFLE_RELATIVE_BIT) {
|
2021-03-31 10:31:27 +00:00
|
|
|
preamble += "#define has_GL_KHR_shader_subgroup_shuffle_relative 1\n";
|
|
|
|
}
|
2022-02-11 11:33:54 +00:00
|
|
|
if (subgroup_operations & RenderingDevice::SUBGROUP_CLUSTERED_BIT) {
|
2021-03-31 10:31:27 +00:00
|
|
|
preamble += "#define has_GL_KHR_shader_subgroup_clustered 1\n";
|
|
|
|
}
|
2022-02-11 11:33:54 +00:00
|
|
|
if (subgroup_operations & RenderingDevice::SUBGROUP_QUAD_BIT) {
|
2021-03-31 10:31:27 +00:00
|
|
|
preamble += "#define has_GL_KHR_shader_subgroup_quad 1\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 11:33:54 +00:00
|
|
|
if (p_render_device->has_feature(RD::SUPPORTS_MULTIVIEW)) {
|
2021-05-07 13:19:04 +00:00
|
|
|
preamble += "#define has_VK_KHR_multiview 1\n";
|
|
|
|
}
|
|
|
|
|
2021-12-09 09:42:46 +00:00
|
|
|
if (!preamble.empty()) {
|
2021-03-31 10:31:27 +00:00
|
|
|
shader.setPreamble(preamble.c_str());
|
|
|
|
}
|
|
|
|
|
2019-07-28 22:58:32 +00:00
|
|
|
EShMessages messages = (EShMessages)(EShMsgSpvRules | EShMsgVulkanRules);
|
2023-08-12 12:56:49 +00:00
|
|
|
if (Engine::get_singleton()->is_generate_spirv_debug_info_enabled()) {
|
|
|
|
messages = (EShMessages)(messages | EShMsgDebugInfo);
|
2019-07-28 22:58:32 +00:00
|
|
|
}
|
2023-08-12 12:56:49 +00:00
|
|
|
const int DefaultVersion = 100;
|
2019-07-28 22:58:32 +00:00
|
|
|
|
|
|
|
//parse
|
2023-09-01 09:11:12 +00:00
|
|
|
if (!shader.parse(GetDefaultResources(), DefaultVersion, false, messages)) {
|
2019-07-28 22:58:32 +00:00
|
|
|
if (r_error) {
|
|
|
|
(*r_error) = "Failed parse:\n";
|
|
|
|
(*r_error) += shader.getInfoLog();
|
|
|
|
(*r_error) += "\n";
|
|
|
|
(*r_error) += shader.getInfoDebugLog();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//link
|
|
|
|
glslang::TProgram program;
|
|
|
|
program.addShader(&shader);
|
|
|
|
|
|
|
|
if (!program.link(messages)) {
|
|
|
|
if (r_error) {
|
|
|
|
(*r_error) = "Failed link:\n";
|
|
|
|
(*r_error) += program.getInfoLog();
|
|
|
|
(*r_error) += "\n";
|
|
|
|
(*r_error) += program.getInfoDebugLog();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint32_t> SpirV;
|
|
|
|
spv::SpvBuildLogger logger;
|
|
|
|
glslang::SpvOptions spvOptions;
|
2023-08-12 12:56:49 +00:00
|
|
|
|
|
|
|
if (Engine::get_singleton()->is_generate_spirv_debug_info_enabled()) {
|
|
|
|
spvOptions.generateDebugInfo = true;
|
|
|
|
spvOptions.emitNonSemanticShaderDebugInfo = true;
|
|
|
|
spvOptions.emitNonSemanticShaderDebugSource = true;
|
|
|
|
}
|
|
|
|
|
2019-07-28 22:58:32 +00:00
|
|
|
glslang::GlslangToSpv(*program.getIntermediate(stages[p_stage]), SpirV, &logger, &spvOptions);
|
|
|
|
|
|
|
|
ret.resize(SpirV.size() * sizeof(uint32_t));
|
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *w = ret.ptrw();
|
2021-04-27 14:19:21 +00:00
|
|
|
memcpy(w, &SpirV[0], SpirV.size() * sizeof(uint32_t));
|
2019-07-28 22:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-11 11:33:54 +00:00
|
|
|
static String _get_cache_key_function_glsl(const RenderingDevice *p_render_device) {
|
2023-12-19 17:57:56 +00:00
|
|
|
const RenderingDeviceDriver::Capabilities &capabilities = p_render_device->get_device_capabilities();
|
2021-05-25 00:25:11 +00:00
|
|
|
String version;
|
2023-12-19 17:57:56 +00:00
|
|
|
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());
|
2021-05-25 00:25:11 +00:00
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2022-05-03 09:56:08 +00:00
|
|
|
void initialize_glslang_module(ModuleInitializationLevel p_level) {
|
|
|
|
if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-12 11:28:43 +00:00
|
|
|
// Initialize in case it's not initialized. This is done once per thread
|
|
|
|
// and it's safe to call multiple times.
|
2019-07-29 15:59:18 +00:00
|
|
|
glslang::InitializeProcess();
|
Implement Binary Shader Compilation
* Added an extra stage before compiling shader, which is generating a binary blob.
* On Vulkan, this allows caching the SPIRV reflection information, which is expensive to parse.
* On other (future) RenderingDevices, it allows caching converted binary data, such as DXIL or MSL.
This PR makes the shader cache include the reflection information, hence editor startup times are significantly improved.
I tested this well and it appears to work, and I added a lot of consistency checks, but because it includes writing and reading binary information, rare bugs may pop up, so be aware.
There was not much of a choice for storing the reflection information, given shaders can be a lot, take a lot of space and take time to parse.
2021-07-25 14:22:55 +00:00
|
|
|
RenderingDevice::shader_set_compile_to_spirv_function(_compile_shader_glsl);
|
2021-05-25 00:25:11 +00:00
|
|
|
RenderingDevice::shader_set_get_cache_key_function(_get_cache_key_function_glsl);
|
2019-07-28 22:58:32 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 09:56:08 +00:00
|
|
|
void uninitialize_glslang_module(ModuleInitializationLevel p_level) {
|
|
|
|
if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-07-29 15:59:18 +00:00
|
|
|
glslang::FinalizeProcess();
|
2019-07-28 22:58:32 +00:00
|
|
|
}
|