From a14ad93689ae770514f59fb6c4374ca0dc7bb9b7 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Mon, 26 Aug 2024 23:14:46 -0700 Subject: [PATCH] Include shader path in the debug info --- blade-graphics/src/vulkan/init.rs | 11 +++++++++-- blade-graphics/src/vulkan/mod.rs | 3 ++- blade-graphics/src/vulkan/pipeline.rs | 17 ++++++++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/blade-graphics/src/vulkan/init.rs b/blade-graphics/src/vulkan/init.rs index f55a001c..4a0c95da 100644 --- a/blade-graphics/src/vulkan/init.rs +++ b/blade-graphics/src/vulkan/init.rs @@ -695,9 +695,15 @@ impl super::Context { }); let mut naga_flags = spv::WriterFlags::FORCE_POINT_SIZE; - if desc.validation || desc.capture { + let shader_debug_path = if desc.validation || desc.capture { + use std::{env, fs}; naga_flags |= spv::WriterFlags::DEBUG; - } + let dir = env::temp_dir().join("blade"); + let _ = fs::create_dir(&dir); + Some(dir) + } else { + None + }; Ok(super::Context { memory: Mutex::new(memory_manager), @@ -712,6 +718,7 @@ impl super::Context { surface, physical_device, naga_flags, + shader_debug_path, instance, _entry: entry, }) diff --git a/blade-graphics/src/vulkan/mod.rs b/blade-graphics/src/vulkan/mod.rs index 894ad8ce..4707fc8f 100644 --- a/blade-graphics/src/vulkan/mod.rs +++ b/blade-graphics/src/vulkan/mod.rs @@ -1,5 +1,5 @@ use ash::{khr, vk}; -use std::{num::NonZeroU32, ptr, sync::Mutex}; +use std::{num::NonZeroU32, path::PathBuf, ptr, sync::Mutex}; mod command; mod descriptor; @@ -107,6 +107,7 @@ pub struct Context { surface: Option>, physical_device: vk::PhysicalDevice, naga_flags: naga::back::spv::WriterFlags, + shader_debug_path: Option, instance: Instance, _entry: ash::Entry, } diff --git a/blade-graphics/src/vulkan/pipeline.rs b/blade-graphics/src/vulkan/pipeline.rs index aa98cd76..775ee1cf 100644 --- a/blade-graphics/src/vulkan/pipeline.rs +++ b/blade-graphics/src/vulkan/pipeline.rs @@ -1,6 +1,6 @@ use ash::vk; use naga::back::spv; -use std::{ffi, mem, path::Path, str}; +use std::{ffi, mem, str}; const DUMP_PREFIX: Option<&str> = None; @@ -79,12 +79,23 @@ impl super::Context { shader_stage: ep.stage, entry_point: sf.entry_point.to_string(), }; + let file_path; let mut naga_options_debug; - let naga_options = if self.naga_flags.contains(spv::WriterFlags::DEBUG) { + let naga_options = if let Some(ref temp_dir) = self.shader_debug_path { + use std::{ + fs, + hash::{DefaultHasher, Hash as _, Hasher as _}, + }; + let mut hasher = DefaultHasher::new(); + sf.shader.source.hash(&mut hasher); + file_path = temp_dir.join(format!("{}-{:x}.wgsl", sf.entry_point, hasher.finish())); + log::debug!("Dumping processed shader code to: {}", file_path.display()); + let _ = fs::write(&file_path, &sf.shader.source); + naga_options_debug = naga_options_base.clone(); naga_options_debug.debug_info = Some(naga::back::spv::DebugInfo { source_code: &sf.shader.source, - file_name: Path::new(""), + file_name: &file_path, }); &naga_options_debug } else {