Skip to content

Commit

Permalink
feat(spv): shader debug option (#4028)
Browse files Browse the repository at this point in the history
Co-authored-by: Connor Fitzgerald <[email protected]>
  • Loading branch information
wicast and cwfitzgerald authored Oct 23, 2023
1 parent dd84765 commit 9dc5761
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
21 changes: 19 additions & 2 deletions wgpu-core/src/device/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,19 @@ impl<A: HalApi> Device<A> {
.contains(wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES),
);

let debug_source = if self.instance_flags.contains(wgt::InstanceFlags::DEBUG) {
Some(hal::DebugSource {
file_name: Cow::Owned(
desc.label
.as_ref()
.map_or("shader".to_string(), |l| l.to_string()),
),
source_code: Cow::Owned(source.clone()),
})
} else {
None
};

let info = naga::valid::Validator::new(naga::valid::ValidationFlags::all(), caps)
.validate(&module)
.map_err(|inner| {
Expand All @@ -1326,10 +1339,14 @@ impl<A: HalApi> Device<A> {
inner: Box::new(inner),
})
})?;

let interface =
validation::Interface::new(&module, &info, self.limits.clone(), self.features);
let hal_shader = hal::ShaderInput::Naga(hal::NagaShader { module, info });

let hal_shader = hal::ShaderInput::Naga(hal::NagaShader {
module,
info,
debug_source,
});
let hal_desc = hal::ShaderModuleDescriptor {
label: desc.label.to_hal(self.instance_flags),
runtime_checks: desc.shader_bound_checks.runtime_checks(),
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/examples/halmark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl<A: hal::Api> Example<A> {
hal::NagaShader {
module: Cow::Owned(module),
info,
debug_source: None,
}
};
let shader_desc = hal::ShaderModuleDescriptor {
Expand Down
8 changes: 8 additions & 0 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,8 @@ pub struct NagaShader {
pub module: Cow<'static, naga::Module>,
/// Analysis information of the module.
pub info: naga::valid::ModuleInfo,
/// Source codes for debug
pub debug_source: Option<DebugSource>,
}

// Custom implementation avoids the need to generate Debug impl code
Expand All @@ -1119,6 +1121,12 @@ pub struct ShaderModuleDescriptor<'a> {
pub runtime_checks: bool,
}

#[derive(Debug, Clone)]
pub struct DebugSource {
pub file_name: Cow<'static, str>,
pub source_code: Cow<'static, str>,
}

/// Describes a programmable pipeline stage.
#[derive(Debug)]
pub struct ProgrammableStage<'a, A: Api> {
Expand Down
20 changes: 19 additions & 1 deletion wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,9 @@ impl super::Device {
entry_point: stage.entry_point.to_string(),
shader_stage: naga_stage,
};
let needs_temp_options = !runtime_checks || !binding_map.is_empty();
let needs_temp_options = !runtime_checks
|| !binding_map.is_empty()
|| naga_shader.debug_source.is_some();
let mut temp_options;
let options = if needs_temp_options {
temp_options = self.naga_options.clone();
Expand All @@ -739,6 +741,14 @@ impl super::Device {
if !binding_map.is_empty() {
temp_options.binding_map = binding_map.clone();
}

if let Some(ref debug) = naga_shader.debug_source {
temp_options.debug_info = Some(naga::back::spv::DebugInfo {
source_code: &debug.source_code,
file_name: debug.file_name.as_ref().as_ref(),
})
}

&temp_options
} else {
&self.naga_options
Expand Down Expand Up @@ -1513,6 +1523,14 @@ impl crate::Device<super::Api> for super::Device {
});
}
let mut naga_options = self.naga_options.clone();
naga_options.debug_info =
naga_shader
.debug_source
.as_ref()
.map(|d| naga::back::spv::DebugInfo {
source_code: d.source_code.as_ref(),
file_name: d.file_name.as_ref().as_ref(),
});
if !desc.runtime_checks {
naga_options.bounds_check_policies = naga::proc::BoundsCheckPolicies {
index: naga::proc::BoundsCheckPolicy::Unchecked,
Expand Down

0 comments on commit 9dc5761

Please sign in to comment.