29#include "spirv/unified1/spirv.h"
31#ifdef WITH_GPU_BACKEND_TESTS
33# include "spirv-tools/libspirv.h"
43 static std::optional<std::string>
result = []() -> std::optional<std::string> {
44 static char tmp_dir_buffer[
FILE_MAX];
48 std::string cache_dir = std::string(tmp_dir_buffer) +
"vk-spirv-cache" +
SEP_STR;
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();
98 if (sidecar_size_on_disk !=
sizeof(sidecar)) {
101 sidecar_file.seekg(0, std::ios::beg);
102 sidecar_file.read(
reinterpret_cast<char *
>(&sidecar),
sizeof(sidecar));
107 "Recompiling SPIR-V cache entry for %s. Cached SPIR-V version %08x, but %08x requested.",
115 fstream spirv_file(spirv_path, std::ios::binary | std::ios::in | std::ios::ate);
116 std::streamsize size = spirv_file.tellg();
120 spirv_file.seekg(0, std::ios::beg);
122 spirv_file.read(
reinterpret_cast<char *
>(shader_module.
spirv_binary.
data()), size);
124 CLOG_TRACE(&
LOG,
"reading SpirV from disk %s", spirv_path.c_str());
142 CLOG_TRACE(&
LOG,
"write SpirV to disk %s", spirv_path.c_str());
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);
153 fstream sidecar_file(sidecar_path, std::ios::binary | std::ios::out);
154 sidecar_file.write(
reinterpret_cast<const char *
>(&sidecar),
sizeof(
SPIRVSidecar));
170 const time_t ts_now = time(
nullptr);
171 const time_t delete_threshold = 60 * 60 * 24 * 30 ;
172 if (entry.
s.st_mtime + delete_threshold < ts_now) {
188 case shaderc_vertex_shader:
190 case shaderc_geometry_shader:
192 case shaderc_fragment_shader:
194 case shaderc_compute_shader:
198 BLI_assert_msg(
false,
"Do not know how to convert shaderc_shader_kind to stage name.");
201 return "unknown stage";
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);
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;
241 spirv_out.extend(spirv_in);
245 const bool pre_debug =
ELEM(opcode,
252 SpvOpExecutionModeId);
260 const size_t byte_len = name.
size() + 1;
264 const uint32_t num_words_inject = 2u + num_string_words + 4u;
267 spirv_out.
reserve(spirv_in.
size() + num_words_inject);
268 spirv_out.extend(spirv_in.
take_front(pos_inject));
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);
275 spirv_out.
resize(spirv_out.
size() + num_string_words, 0u);
276 memcpy(spirv_out.
data() + string_offset, name.
data(), name.
size());
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);
287 spirv_out.extend(spirv_in.
drop_front(pos_inject));
290 SpirvHeader *header_out =
reinterpret_cast<SpirvHeader *
>(spirv_out.
data());
291 header_out->bound = header_in->bound + 1u;
294#ifdef WITH_GPU_BACKEND_TESTS
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;
307 "SPIR-V validation failed for %s: %s",
308 std::string(name).c_str(),
309 (diagnostic && diagnostic->error) ? diagnostic->error :
"unknown error");
311 spvDiagnosticDestroy(diagnostic);
312 spvContextDestroy(context);
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] =
'/';
331 shaderc_shader_kind stage,
341 if (!
shader.skip_preprocessor) {
355 const std::string hash_extra = std::string(
"op_string:") + full_name;
361 shaderc::CompileOptions
options;
362 bool do_optimize =
true;
364 options.SetTargetEnvironment(shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_2);
365 options.SetTargetSpirv(shaderc_spirv_version_1_4);
368 options.SetTargetEnvironment(shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_1);
377 options.SetOptimizationLevel(do_optimize ? shaderc_optimization_level_performance :
378 shaderc_optimization_level_zero);
396 options.SetMaxIdBound(0xffffff);
401 options.SetGenerateDebugInfo();
414 sources, stage, full_name.c_str(),
options);
416 bool compilation_succeeded = shader_module.
compilation_result.GetCompilationStatus() ==
417 shaderc_compilation_status_success;
418 if (compilation_succeeded) {
426 const bool stage_use_ray_query = stage != shaderc_geometry_shader &&
427 shader.use_ray_query_get();
430 compilation_result_span, shader_module.
spirv_binary, full_name, glsl_version);
436#ifdef WITH_GPU_BACKEND_TESTS
439 if (!spirv_validate(shader_module.
spirv_binary, full_name)) {
447 return compilation_succeeded;
451 shaderc_shader_kind stage,
454 shaderc::Compiler compiler;
File and directory operations.
Compatibility-like things for windows.
#define CLOG_ERROR(clg_ref,...)
#define CLOG_TRACE(clg_ref,...)
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
constexpr Span drop_front(int64_t n) const
constexpr const T * data() const
constexpr int64_t size() const
constexpr Span take_front(int64_t n) const
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)
static std::string run_preprocessor(StringRef source, bool no_dead_code_elimination)
const VKExtensions & extensions_get() const
uint32_t glsl_patch_version_get(bool use_ray_query) const
static bool compile_module(VKShader &shader, shaderc_shader_kind stage, VKShaderModule &shader_module)
static void cache_dir_clear_old()
std::string original_sources
shaderc::SpvCompilationResult compilation_result
std::string combined_sources
Vector< uint32_t > spirv_binary
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 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
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
@ G_DEBUG_GPU_SHADER_DEBUG_INFO
@ G_DEBUG_GPU_SHADER_NO_DCE
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)
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
void BKE_appdir_folder_caches(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
MINLINE uint divide_ceil_u(uint a, uint b)
void reserve(int new_size)
void resize(int new_size)