Blender V5.3
vk_shader_compiler.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_appdir.hh"
10
11#include "BLI_fileops.hh"
12#include "BLI_path_utils.hh"
13#include "BLI_simd.hh"
14#ifdef _WIN32
15# include "BLI_winstuff.hh"
16#endif
17
18#include "vk_backend.hh"
19#include "vk_device.hh"
20#include "vk_shader.hh"
21#include "vk_shader_compiler.hh"
22
23#include <cstring>
24#include <iostream>
25#include <string>
26
27#include "CLG_log.h"
28
29#include "spirv/unified1/spirv.h"
30
31#ifdef WITH_GPU_BACKEND_TESTS
32# if 0
33# include "spirv-tools/libspirv.h"
34# endif
35#endif
36
37namespace blender::gpu {
38
39static CLG_LogRef LOG = {"gpu.vulkan"};
40
41static std::optional<std::string> cache_dir_get()
42{
43 static std::optional<std::string> result = []() -> std::optional<std::string> {
44 static char tmp_dir_buffer[FILE_MAX];
45 /* Shader builder doesn't return the correct appdir. */
46 BKE_appdir_folder_caches(tmp_dir_buffer, sizeof(tmp_dir_buffer));
47
48 std::string cache_dir = std::string(tmp_dir_buffer) + "vk-spirv-cache" + SEP_STR;
49 BLI_dir_create_recursive(cache_dir.c_str());
50 return cache_dir;
51 }();
52
53 return result;
54}
55
56/* -------------------------------------------------------------------- */
59
66
68{
69 const VKDevice &device = VKBackend::get().device;
70 return device.extensions_get().spirv_1_4 ? shaderc_spirv_version_1_4 : shaderc_spirv_version_1_3;
71}
72
73static bool read_spirv_from_disk(VKShaderModule &shader_module, StringRef hash_extra)
74{
75 if (G.debug & G_DEBUG_GPU_SHADER_DEBUG_INFO) {
76 /* Debug information is not part of the cached SPIR-V, so don't use the cache in this case. */
77 return false;
78 }
79 if (!cache_dir_get().has_value()) {
80 return false;
81 }
82 shader_module.build_sources_hash(hash_extra);
83 std::string spirv_path = (*cache_dir_get()) + SEP_STR + shader_module.sources_hash + ".spv";
84 std::string sidecar_path = (*cache_dir_get()) + SEP_STR + shader_module.sources_hash +
85 ".sidecar.bin";
86
87 if (!BLI_exists(spirv_path.c_str()) || !BLI_exists(sidecar_path.c_str())) {
88 return false;
89 }
90
91 BLI_file_touch(spirv_path.c_str());
92 BLI_file_touch(sidecar_path.c_str());
93
94 /* Read sidecar. */
95 fstream sidecar_file(sidecar_path, std::ios::binary | std::ios::in | std::ios::ate);
96 std::streamsize sidecar_size_on_disk = sidecar_file.tellg();
97 SPIRVSidecar sidecar = {};
98 if (sidecar_size_on_disk != sizeof(sidecar)) {
99 return false;
100 }
101 sidecar_file.seekg(0, std::ios::beg);
102 sidecar_file.read(reinterpret_cast<char *>(&sidecar), sizeof(sidecar));
103
104 if (sidecar.spirv_version != spirv_target_version_get()) {
106 &LOG,
107 "Recompiling SPIR-V cache entry for %s. Cached SPIR-V version %08x, but %08x requested.",
108 spirv_path.c_str(),
109 sidecar.spirv_version,
111 return false;
112 }
113
114 /* Read spirv binary. */
115 fstream spirv_file(spirv_path, std::ios::binary | std::ios::in | std::ios::ate);
116 std::streamsize size = spirv_file.tellg();
117 if (size != sidecar.spirv_size) {
118 return false;
119 }
120 spirv_file.seekg(0, std::ios::beg);
121 shader_module.spirv_binary.resize(size / 4);
122 spirv_file.read(reinterpret_cast<char *>(shader_module.spirv_binary.data()), size);
123
124 CLOG_TRACE(&LOG, "reading SpirV from disk %s", spirv_path.c_str());
125 return true;
126}
127
128static void write_spirv_to_disk(VKShaderModule &shader_module)
129{
130 if (G.debug & G_DEBUG_GPU_SHADER_DEBUG_INFO) {
131 /* Debug information is not part of the cached SPIR-V, so don't use the cache in this case. */
132 return;
133 }
134 if (!cache_dir_get().has_value()) {
135 return;
136 }
137
138 /* Write the SPIR-V binary. Note that spirv_binary is used instead of compilation_result so
139 * that any post-processing (e.g. the injected OpString shader name) is stored in the cache and
140 * does not have to be redone every time the cache entry is read back. */
141 std::string spirv_path = (*cache_dir_get()) + SEP_STR + shader_module.sources_hash + ".spv";
142 CLOG_TRACE(&LOG, "write SpirV to disk %s", spirv_path.c_str());
143 size_t size = shader_module.spirv_binary.size() * sizeof(uint32_t);
144 fstream spirv_file(spirv_path, std::ios::binary | std::ios::out);
145 spirv_file.write(reinterpret_cast<const char *>(shader_module.spirv_binary.data()), size);
146
147 /* Write the sidecar */
148 SPIRVSidecar sidecar;
149 sidecar.spirv_size = size;
151 std::string sidecar_path = (*cache_dir_get()) + SEP_STR + shader_module.sources_hash +
152 ".sidecar.bin";
153 fstream sidecar_file(sidecar_path, std::ios::binary | std::ios::out);
154 sidecar_file.write(reinterpret_cast<const char *>(&sidecar), sizeof(SPIRVSidecar));
155}
156
158{
159 if (!cache_dir_get().has_value()) {
160 return;
161 }
162
163 direntry *entries = nullptr;
164 uint32_t dir_len = BLI_filelist_dir_contents(cache_dir_get()->c_str(), &entries);
165 for (int i : IndexRange(dir_len)) {
166 direntry entry = entries[i];
167 if (S_ISDIR(entry.s.st_mode)) {
168 continue;
169 }
170 const time_t ts_now = time(nullptr);
171 const time_t delete_threshold = 60 /*seconds*/ * 60 /*minutes*/ * 24 /*hours*/ * 30 /*days*/;
172 if (entry.s.st_mtime + delete_threshold < ts_now) {
173 BLI_delete(entry.path, false, false);
174 }
175 }
176 BLI_filelist_free(entries, dir_len);
177}
178
180
181/* -------------------------------------------------------------------- */
184
185static StringRef to_stage_name(shaderc_shader_kind stage)
186{
187 switch (stage) {
188 case shaderc_vertex_shader:
189 return "vertex";
190 case shaderc_geometry_shader:
191 return "geometry";
192 case shaderc_fragment_shader:
193 return "fragment";
194 case shaderc_compute_shader:
195 return "compute";
196
197 default:
198 BLI_assert_msg(false, "Do not know how to convert shaderc_shader_kind to stage name.");
199 break;
200 }
201 return "unknown stage";
202}
203
213static void spirv_inject_op_string(const Span<uint32_t> &spirv_in,
214 Vector<uint32_t> &spirv_out,
215 StringRef name,
216 uint32_t glsl_version)
217{
218 struct SpirvHeader {
219 uint32_t magic_number;
220 uint32_t version;
221 uint32_t generator;
222 uint32_t bound; /* Indicates the next available result ID. */
223 uint32_t schema;
224 };
225 constexpr uint32_t SPIRV_HEADER_WORD_COUNT = sizeof(SpirvHeader) / sizeof(uint32_t);
226 const SpirvHeader *header_in = reinterpret_cast<const SpirvHeader *>(spirv_in.data());
227 if (spirv_in.size() < SPIRV_HEADER_WORD_COUNT || header_in->magic_number != SpvMagicNumber) {
228 spirv_out.extend(spirv_in); /* pass through unmodified */
229 BLI_assert_msg(false, "Invalid SPIR-V header");
230 return;
231 }
232
233 /* Insertion point = start of the debug section: After all OpCapability / OpExtension /
234 * OpExtInstImport / OpMemoryModel / OpEntryPoint / OpExecutionMode instructions. */
235 int64_t pos_inject = SPIRV_HEADER_WORD_COUNT;
236 while (pos_inject < spirv_in.size()) {
237 const uint32_t opcode = spirv_in[pos_inject] & SpvOpCodeMask;
238 const uint32_t length = spirv_in[pos_inject] >> SpvWordCountShift;
239 if (length == 0) {
240 /* Invalid SPIR-V binary. */
241 spirv_out.extend(spirv_in); /* pass through unmodified */
242 BLI_assert_msg(false, "Invalid SPIR-V code word length");
243 return;
244 }
245 const bool pre_debug = ELEM(opcode,
246 SpvOpCapability,
247 SpvOpExtension,
248 SpvOpExtInstImport,
249 SpvOpMemoryModel,
250 SpvOpEntryPoint,
251 SpvOpExecutionMode,
252 SpvOpExecutionModeId);
253 if (!pre_debug) {
254 break;
255 }
256 pos_inject += length;
257 }
258
259 /* Literal string: UTF-8, null-terminated, packed 4 bytes/word, zero-padded. */
260 const size_t byte_len = name.size() + 1; /* +1 for null terminator. */
261 const uint32_t num_string_words = divide_ceil_u(byte_len, 4);
262
263 /* Compute the number of words to inject; OpString (2 + string) + OpSource (4). */
264 const uint32_t num_words_inject = 2u + num_string_words + 4u;
265
266 /* Compute the size of the SPIR-V output binary and add the part until the injection point. */
267 spirv_out.reserve(spirv_in.size() + num_words_inject);
268 spirv_out.extend(spirv_in.take_front(pos_inject));
269
270 /* Inject OpString holding the shader name. */
271 const uint32_t op_string_word_count = 2u + num_string_words;
272 spirv_out.append((op_string_word_count << SpvWordCountShift) | SpvOpString);
273 spirv_out.append(header_in->bound);
274 const int64_t string_offset = spirv_out.size();
275 spirv_out.resize(spirv_out.size() + num_string_words, 0u);
276 memcpy(spirv_out.data() + string_offset, name.data(), name.size());
277
278 /* Inject OpSource referencing the OpString above as the source file so tools can associate the
279 * shader name with this module. */
280 constexpr uint32_t op_source_word_count = 4u;
281 spirv_out.append((op_source_word_count << SpvWordCountShift) | SpvOpSource);
282 spirv_out.append(SpvSourceLanguageGLSL);
283 spirv_out.append(glsl_version);
284 spirv_out.append(header_in->bound);
285
286 /* Add the part from the source binary after the injection position to the output. */
287 spirv_out.extend(spirv_in.drop_front(pos_inject));
288
289 /* Raise the result ID bound in the output binary by one due to adding OpString. */
290 SpirvHeader *header_out = reinterpret_cast<SpirvHeader *>(spirv_out.data());
291 header_out->bound = header_in->bound + 1u;
292}
293
294#ifdef WITH_GPU_BACKEND_TESTS
295/* Validate the SPIR-V binary using SPIRV-Tools. Only compiled into test builds since validation
296 * is expensive. Used to catch breakage of spirv_inject_op_string with future SPIR-V versions.
297 * \todo Enable the code below once SPIRV-Tools static library is included in the dependencies. */
298# if 0
299static bool spirv_validate(const Span<uint32_t> &spirv, StringRef name)
300{
301 spv_context context = spvContextCreate(SPV_ENV_VULKAN_1_2);
302 spv_diagnostic diagnostic = nullptr;
303 const spv_result_t result = spvValidateBinary(context, spirv.data(), spirv.size(), &diagnostic);
304 const bool valid = result == SPV_SUCCESS;
305 if (!valid) {
307 "SPIR-V validation failed for %s: %s",
308 std::string(name).c_str(),
309 (diagnostic && diagnostic->error) ? diagnostic->error : "unknown error");
310 }
311 spvDiagnosticDestroy(diagnostic);
312 spvContextDestroy(context);
313 return valid;
314}
315# endif
316#endif
317
318static std::string patch_line_directives(std::string source)
319{
320 /* Patch line directives so that we can make error reporting consistent. */
321 size_t start_pos = 0;
322 while ((start_pos = source.find("#line ", start_pos)) != std::string::npos) {
323 source[start_pos] = '/';
324 source[start_pos + 1] = '/';
325 }
326 return source;
327}
328
329static bool compile_ex(shaderc::Compiler &compiler,
331 shaderc_shader_kind stage,
332 VKShaderModule &shader_module)
333{
334 std::string full_name = shader.name_get() + "_" + to_stage_name(stage);
335
336 shader_module.original_sources = std::move(shader_module.combined_sources);
337
339 shader.name_get(), full_name, ".glsl", shader_module.original_sources);
340
341 if (!shader.skip_preprocessor) {
342 shader_module.combined_sources = Shader::run_preprocessor(shader_module.original_sources,
344
346 shader.name_get(), full_name + ".expanded", ".glsl", shader_module.combined_sources);
347 }
348 else {
349 shader_module.combined_sources = shader_module.original_sources;
350 }
351
352 /* The cached SPIR-V binary contains an injected OpString, which depends on the shader name.
353 * Include the name in the cache key so an entry is only reused for an identical result. This
354 * also invalidates older caches that stored the SPIR-V without the injected OpString. */
355 const std::string hash_extra = std::string("op_string:") + full_name;
356
357 if (read_spirv_from_disk(shader_module, hash_extra)) {
358 return true;
359 }
360
361 shaderc::CompileOptions options;
362 bool do_optimize = true;
363 if (VKBackend::get().device.extensions_get().spirv_1_4) {
364 options.SetTargetEnvironment(shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_2);
365 options.SetTargetSpirv(shaderc_spirv_version_1_4);
366 }
367 else {
368 options.SetTargetEnvironment(shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_1);
369 }
370 if (G.debug & G_DEBUG_GPU_RENDERDOC) {
371 do_optimize = false;
372 }
373 /* WORKAROUND: Qualcomm driver can crash when handling optimized SPIR-V. */
375 do_optimize = false;
376 }
377 options.SetOptimizationLevel(do_optimize ? shaderc_optimization_level_performance :
378 shaderc_optimization_level_zero);
379
380 /* Increase the max id bound.
381 *
382 * SPIR-V has a default max id bound set to 0x3fffff which is the minimum amount of ids that
383 * needs to be supported by any platform. However during optimization the max id bound can
384 * increase very fast and lowered at the end. As glslang uses max id bound in their internal
385 * structures to allocate arrays out of bound errors can occur.
386 *
387 * Increasing the max id bound to a larger number to increase the internal arrays of the
388 * compiler to work around the compiler crash.
389 *
390 * NOTE: Test-files in #144614 and #143516 would surpass the default limit during compilation.
391 * The final optimized SPIR-V is far less than the default so be fine to be used on platforms
392 * with minimum spec.
393 *
394 * https://registry.khronos.org/SPIR-V/specs/1.0/SPIRV.html#_a_id_limits_a_universal_limits
395 */
396 options.SetMaxIdBound(0xffffff);
397
398 /* Should always be called after setting the optimization level. Setting optimization level
399 * resets all previous passes. */
400 if (G.debug & G_DEBUG_GPU_SHADER_DEBUG_INFO) {
401 options.SetGenerateDebugInfo();
402 }
403
404 /* Removes line directive. */
405 std::string sources = patch_line_directives(shader_module.combined_sources);
406
407 /* The task scheduler enables flush-to-zero. Some platforms convert floating point literals using
408 * floating point instructions, which would then turn denormal literals into zero. That happens
409 * for example with the `1.17549435e-38` guard inside OCIO generated for the Filmic view
410 * transform. */
411 {
412 ScopedNoFlushToZero scoped_no_flush_to_zero;
413 shader_module.compilation_result = compiler.CompileGlslToSpv(
414 sources, stage, full_name.c_str(), options);
415 }
416 bool compilation_succeeded = shader_module.compilation_result.GetCompilationStatus() ==
417 shaderc_compilation_status_success;
418 if (compilation_succeeded) {
419 /* Copy the compiled SPIR-V code into spirv_binary so it can be post-processed.
420 * The function VKShaderModule::finalize prefers spirv_binary when it is not empty. */
421 const uint32_t *begin = shader_module.compilation_result.begin();
422 const uint32_t *end = shader_module.compilation_result.end();
423 Span<uint32_t> compilation_result_span(begin, end - begin);
424 if ((G.debug & G_DEBUG_GPU_SHADER_DEBUG_INFO) == 0) {
425 const VKDevice &device = VKBackend::get().device;
426 const bool stage_use_ray_query = stage != shaderc_geometry_shader &&
427 shader.use_ray_query_get();
428 const uint32_t glsl_version = device.glsl_patch_version_get(stage_use_ray_query);
430 compilation_result_span, shader_module.spirv_binary, full_name, glsl_version);
431 }
432 else {
433 shader_module.spirv_binary = Vector<uint32_t>(compilation_result_span);
434 }
435
436#ifdef WITH_GPU_BACKEND_TESTS
437 /* TODO(@chrismile): enable the code below once SPIRV-Tools library is available. */
438# if 0
439 if (!spirv_validate(shader_module.spirv_binary, full_name)) {
440 return false;
441 }
442# endif
443#endif
444
445 write_spirv_to_disk(shader_module);
446 }
447 return compilation_succeeded;
448}
449
451 shaderc_shader_kind stage,
452 VKShaderModule &shader_module)
453{
454 shaderc::Compiler compiler;
455 return compile_ex(compiler, shader, stage, shader_module);
456}
457
459
460} // namespace blender::gpu
File and directory operations.
#define FILE_MAX
Compatibility-like things for windows.
#define S_ISDIR(x)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_TRACE(clg_ref,...)
Definition CLG_log.h:192
#define ELEM(...)
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
constexpr Span drop_front(int64_t n) const
Definition BLI_span.hh:175
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr Span take_front(int64_t n) const
Definition BLI_span.hh:197
constexpr int64_t size() const
constexpr const char * data() const
static void dump_source_to_disk(StringRef shader_name, StringRef shader_name_with_stage_name, StringRef extension, StringRef source)
Definition gpu_shader.cc:43
static std::string run_preprocessor(StringRef source, bool no_dead_code_elimination)
static VKBackend & get()
const VKExtensions & extensions_get() const
Definition vk_device.hh:398
uint32_t glsl_patch_version_get(bool use_ray_query) const
Definition vk_device.cc:293
static bool compile_module(VKShader &shader, shaderc_shader_kind stage, VKShaderModule &shader_module)
shaderc::SpvCompilationResult compilation_result
void build_sources_hash(StringRef extra)
CCL_NAMESPACE_BEGIN struct Options options
float length(VecOp< float, D >) RET
static bool compile_ex(shaderc::Compiler &compiler, VKShader &shader, shaderc_shader_kind stage, VKShaderModule &shader_module)
static void write_spirv_to_disk(VKShaderModule &shader_module)
static std::optional< std::string > cache_dir_get()
static bool read_spirv_from_disk(VKShaderModule &shader_module, StringRef hash_extra)
static uint32_t spirv_target_version_get()
static CLG_LogRef LOG
static void spirv_inject_op_string(const Span< uint32_t > &spirv_in, Vector< uint32_t > &spirv_out, StringRef name, uint32_t glsl_version)
static StringRef to_stage_name(shaderc_shader_kind stage)
static std::string patch_line_directives(std::string source)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
@ G_DEBUG_GPU_SHADER_DEBUG_INFO
@ G_DEBUG_GPU_SHADER_NO_DCE
@ G_DEBUG_GPU_RENDERDOC
Global G
Definition blender.cc:57
unsigned int uint32_t
Definition BKE_text.h:81
bool GPU_type_matches(GPUDeviceType device, GPUOSType os, GPUDriverType driver)
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
bool BLI_file_touch(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:318
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
void BKE_appdir_folder_caches(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
Definition appdir.cc:206
iter begin(iter)
MINLINE uint divide_ceil_u(uint a, uint b)
static CLG_LogRef LOG
Definition logging.cpp:12
@ GPU_DEVICE_QUALCOMM
int size() const
Definition vector.hh:130
void reserve(int new_size)
Definition vector.hh:78
void resize(int new_size)
Definition vector.hh:94
#define SEP_STR
Definition unit.cc:41