You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-14 13:41:12 +00:00
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.
This commit is contained in:
@@ -41,7 +41,7 @@ class RDAttachmentFormat;
|
||||
class RDSamplerState;
|
||||
class RDVertexAttribute;
|
||||
class RDShaderSource;
|
||||
class RDShaderBytecode;
|
||||
class RDShaderSPIRV;
|
||||
class RDUniforms;
|
||||
class RDPipelineRasterizationState;
|
||||
class RDPipelineMultisampleState;
|
||||
@@ -105,14 +105,14 @@ public:
|
||||
bool supports_multiview = false; // If true this device supports multiview options
|
||||
};
|
||||
|
||||
typedef String (*ShaderGetCacheKeyFunction)(const Capabilities *p_capabilities);
|
||||
typedef Vector<uint8_t> (*ShaderCompileFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language, String *r_error, const Capabilities *p_capabilities);
|
||||
typedef String (*ShaderSPIRVGetCacheKeyFunction)(const Capabilities *p_capabilities);
|
||||
typedef Vector<uint8_t> (*ShaderCompileToSPIRVFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language, String *r_error, const Capabilities *p_capabilities);
|
||||
typedef Vector<uint8_t> (*ShaderCacheFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language);
|
||||
|
||||
private:
|
||||
static ShaderCompileFunction compile_function;
|
||||
static ShaderCompileToSPIRVFunction compile_to_spirv_function;
|
||||
static ShaderCacheFunction cache_function;
|
||||
static ShaderGetCacheKeyFunction get_cache_key_function;
|
||||
static ShaderSPIRVGetCacheKeyFunction get_spirv_cache_key_function;
|
||||
|
||||
static RenderingDevice *singleton;
|
||||
|
||||
@@ -651,24 +651,28 @@ public:
|
||||
|
||||
const Capabilities *get_device_capabilities() const { return &device_capabilities; };
|
||||
|
||||
virtual Vector<uint8_t> shader_compile_from_source(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language = SHADER_LANGUAGE_GLSL, String *r_error = nullptr, bool p_allow_cache = true);
|
||||
virtual String shader_get_cache_key() const;
|
||||
virtual Vector<uint8_t> shader_compile_spirv_from_source(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language = SHADER_LANGUAGE_GLSL, String *r_error = nullptr, bool p_allow_cache = true);
|
||||
virtual String shader_get_spirv_cache_key() const;
|
||||
|
||||
static void shader_set_compile_function(ShaderCompileFunction p_function);
|
||||
static void shader_set_cache_function(ShaderCacheFunction p_function);
|
||||
static void shader_set_get_cache_key_function(ShaderGetCacheKeyFunction p_function);
|
||||
static void shader_set_compile_to_spirv_function(ShaderCompileToSPIRVFunction p_function);
|
||||
static void shader_set_spirv_cache_function(ShaderCacheFunction p_function);
|
||||
static void shader_set_get_cache_key_function(ShaderSPIRVGetCacheKeyFunction p_function);
|
||||
|
||||
struct ShaderStageData {
|
||||
struct ShaderStageSPIRVData {
|
||||
ShaderStage shader_stage;
|
||||
Vector<uint8_t> spir_v;
|
||||
|
||||
ShaderStageData() {
|
||||
ShaderStageSPIRVData() {
|
||||
shader_stage = SHADER_STAGE_VERTEX;
|
||||
}
|
||||
};
|
||||
|
||||
RID shader_create_from_bytecode(const Ref<RDShaderBytecode> &p_bytecode);
|
||||
virtual RID shader_create(const Vector<ShaderStageData> &p_stages) = 0;
|
||||
virtual String shader_get_binary_cache_key() const = 0;
|
||||
virtual Vector<uint8_t> shader_compile_binary_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv) = 0;
|
||||
|
||||
virtual RID shader_create_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv);
|
||||
virtual RID shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary) = 0;
|
||||
|
||||
virtual uint32_t shader_get_vertex_input_attribute_mask(RID p_shader) = 0;
|
||||
|
||||
/******************/
|
||||
@@ -1194,7 +1198,9 @@ protected:
|
||||
VertexFormatID _vertex_format_create(const TypedArray<RDVertexAttribute> &p_vertex_formats);
|
||||
RID _vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const TypedArray<RID> &p_src_buffers);
|
||||
|
||||
Ref<RDShaderBytecode> _shader_compile_from_source(const Ref<RDShaderSource> &p_source, bool p_allow_cache = true);
|
||||
Ref<RDShaderSPIRV> _shader_compile_spirv_from_source(const Ref<RDShaderSource> &p_source, bool p_allow_cache = true);
|
||||
Vector<uint8_t> _shader_compile_binary_from_spirv(const Ref<RDShaderSPIRV> &p_bytecode);
|
||||
RID _shader_create_from_spirv(const Ref<RDShaderSPIRV> &p_spirv);
|
||||
|
||||
RID _uniform_set_create(const Array &p_uniforms, RID p_shader, uint32_t p_shader_set);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user