Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/vulkan/rendering_shader_container_vulkan.cpp
9903 views
1
/**************************************************************************/
2
/* rendering_shader_container_vulkan.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 "rendering_shader_container_vulkan.h"
32
33
#include "thirdparty/misc/smolv.h"
34
35
// RenderingShaderContainerVulkan
36
37
const uint32_t RenderingShaderContainerVulkan::FORMAT_VERSION = 1;
38
39
uint32_t RenderingShaderContainerVulkan::_format() const {
40
return 0x43565053;
41
}
42
43
uint32_t RenderingShaderContainerVulkan::_format_version() const {
44
return FORMAT_VERSION;
45
}
46
47
bool RenderingShaderContainerVulkan::_set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv) {
48
PackedByteArray code_bytes;
49
shaders.resize(p_spirv.size());
50
for (int64_t i = 0; i < p_spirv.size(); i++) {
51
RenderingShaderContainer::Shader &shader = shaders.ptrw()[i];
52
53
if (debug_info_enabled) {
54
// Store SPIR-V as is when debug info is required.
55
shader.code_compressed_bytes = p_spirv[i].spirv;
56
shader.code_compression_flags = 0;
57
shader.code_decompressed_size = 0;
58
} else {
59
// Encode into smolv.
60
smolv::ByteArray smolv_bytes;
61
bool smolv_encoded = smolv::Encode(p_spirv[i].spirv.ptr(), p_spirv[i].spirv.size(), smolv_bytes, smolv::kEncodeFlagStripDebugInfo);
62
ERR_FAIL_COND_V_MSG(!smolv_encoded, false, "Failed to compress SPIR-V into smolv.");
63
64
code_bytes.resize(smolv_bytes.size());
65
memcpy(code_bytes.ptrw(), smolv_bytes.data(), code_bytes.size());
66
67
// Compress.
68
uint32_t compressed_size = 0;
69
shader.code_decompressed_size = code_bytes.size();
70
shader.code_compressed_bytes.resize(code_bytes.size());
71
72
bool compressed = compress_code(code_bytes.ptr(), code_bytes.size(), shader.code_compressed_bytes.ptrw(), &compressed_size, &shader.code_compression_flags);
73
ERR_FAIL_COND_V_MSG(!compressed, false, vformat("Failed to compress native code to native for SPIR-V #%d.", i));
74
75
shader.code_compressed_bytes.resize(compressed_size);
76
77
// Indicate it uses smolv for compression.
78
shader.code_compression_flags |= COMPRESSION_FLAG_SMOLV;
79
}
80
81
shader.shader_stage = p_spirv[i].shader_stage;
82
}
83
84
return true;
85
}
86
87
RenderingShaderContainerVulkan::RenderingShaderContainerVulkan(bool p_debug_info_enabled) {
88
debug_info_enabled = p_debug_info_enabled;
89
}
90
91
// RenderingShaderContainerFormatVulkan
92
93
Ref<RenderingShaderContainer> RenderingShaderContainerFormatVulkan::create_container() const {
94
return memnew(RenderingShaderContainerVulkan(debug_info_enabled));
95
}
96
97
RenderingDeviceCommons::ShaderLanguageVersion RenderingShaderContainerFormatVulkan::get_shader_language_version() const {
98
return SHADER_LANGUAGE_VULKAN_VERSION_1_1;
99
}
100
101
RenderingDeviceCommons::ShaderSpirvVersion RenderingShaderContainerFormatVulkan::get_shader_spirv_version() const {
102
return SHADER_SPIRV_VERSION_1_3;
103
}
104
105
void RenderingShaderContainerFormatVulkan::set_debug_info_enabled(bool p_debug_info_enabled) {
106
debug_info_enabled = p_debug_info_enabled;
107
}
108
109
RenderingShaderContainerFormatVulkan::RenderingShaderContainerFormatVulkan() {}
110
111
RenderingShaderContainerFormatVulkan::~RenderingShaderContainerFormatVulkan() {}
112
113