Skip to content

Commit

Permalink
debug_printf: Add writer flags to conditionally emit/ignore debugPrin…
Browse files Browse the repository at this point in the history
…tf statements
  • Loading branch information
exrook committed Nov 2, 2023
1 parent b4d327c commit 9c40eb4
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 48 deletions.
16 changes: 12 additions & 4 deletions naga/src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ bitflags::bitflags! {
/// The variable gl_PointSize is intended for a shader to write the size of the point to be rasterized. It is measured in pixels.
/// If gl_PointSize is not written to, its value is undefined in subsequent pipe stages.
const FORCE_POINT_SIZE = 0x10;
/// Emit debug printf statements
const EMIT_DEBUG_PRINTF = 0x20;
}
}

Expand Down Expand Up @@ -2298,10 +2300,16 @@ impl<'a, W: Write> Writer<'a, W> {
ref format,
ref arguments,
} => {
write!(self.out, "{level}")?;
write!(self.out, "debugPrintfEXT(\"{format}\",")?;
self.write_slice(arguments, |this, _, arg| this.write_expr(*arg, ctx))?;
writeln!(self.out, ");")?
if self
.options
.writer_flags
.contains(WriterFlags::EMIT_DEBUG_PRINTF)
{
write!(self.out, "{level}")?;
write!(self.out, "debugPrintfEXT(\"{format}\",")?;
self.write_slice(arguments, |this, _, arg| this.write_expr(*arg, ctx))?;
writeln!(self.out, ");")?
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions naga/src/back/hlsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,23 @@ pub enum EntryPointError {
MissingBinding(crate::ResourceBinding),
}

bitflags::bitflags! {
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Default)]
pub struct WriterFlags: u32 {
/// Emit debug printf statements
const EMIT_DEBUG_PRINTF = 0x1;
}
}

/// Configuration used in the [`Writer`].
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct Options {
/// Configuration flags for the writer.
pub flags: WriterFlags,
/// The hlsl shader model to be used
pub shader_model: ShaderModel,
/// Map of resources association to binding locations.
Expand All @@ -198,6 +210,7 @@ pub struct Options {
impl Default for Options {
fn default() -> Self {
Options {
flags: WriterFlags::empty(),
shader_model: ShaderModel::V5_1,
binding_map: BindingMap::default(),
fake_missing_bindings: true,
Expand Down
18 changes: 10 additions & 8 deletions naga/src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
BackendResult, Error, Options,
};
use crate::{
back,
back::{self, hlsl::WriterFlags},
proc::{self, NameKey},
valid, Handle, Module, ScalarKind, ShaderStage, TypeInner,
};
Expand Down Expand Up @@ -2008,15 +2008,17 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
ref format,
ref arguments,
} => {
write!(self.out, "{level}")?;
write!(self.out, "printf(\"{format}\",")?;
for (index, argument) in arguments.iter().enumerate() {
if index != 0 {
write!(self.out, ", ")?;
if self.options.flags.contains(WriterFlags::EMIT_DEBUG_PRINTF) {
write!(self.out, "{level}")?;
write!(self.out, "printf(\"{format}\",")?;
for (index, argument) in arguments.iter().enumerate() {
if index != 0 {
write!(self.out, ", ")?;
}
self.write_expr(module, *argument, func_ctx)?;
}
self.write_expr(module, *argument, func_ctx)?;
writeln!(self.out, ");")?
}
writeln!(self.out, ");")?
}
}

Expand Down
2 changes: 1 addition & 1 deletion naga/src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3011,7 +3011,7 @@ impl<W: Write> Writer<W> {
}
}
crate::Statement::DebugPrintf { .. } => {
return Err(Error::FeatureNotImplemented("debug printf".to_string()));
// metal doesn't provide a debug printf implementation
}
}
}
Expand Down
40 changes: 21 additions & 19 deletions naga/src/back/spv/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2343,27 +2343,29 @@ impl<'w> BlockContext<'w> {
ref format,
ref arguments,
} => {
self.writer.use_extension("SPV_KHR_non_semantic_info");
let format_id = self.gen_id();
self.writer
.strings
.push(Instruction::string(format, format_id));
let id = self.gen_id();
if self.writer.flags.contains(WriterFlags::EMIT_DEBUG_PRINTF) {
self.writer.use_extension("SPV_KHR_non_semantic_info");
let format_id = self.gen_id();
self.writer
.strings
.push(Instruction::string(format, format_id));
let id = self.gen_id();

self.temp_list.clear();
self.temp_list.push(format_id);
for &argument in arguments {
self.temp_list.push(self.cached[argument]);
}
self.temp_list.clear();
self.temp_list.push(format_id);
for &argument in arguments {
self.temp_list.push(self.cached[argument]);
}

let set_id = self.writer.extension_inst_import("NonSemantic.DebugPrintf");
block.body.push(Instruction::ext_inst(
set_id,
1,
self.writer.void_type,
id,
&self.temp_list,
));
let set_id = self.writer.extension_inst_import("NonSemantic.DebugPrintf");
block.body.push(Instruction::ext_inst(
set_id,
1,
self.writer.void_type,
id,
&self.temp_list,
));
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion naga/src/back/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,9 @@ pub struct Writer {
}

bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
pub struct WriterFlags: u32 {
/// Include debug labels for everything.
const DEBUG = 0x1;
Expand All @@ -653,6 +655,8 @@ bitflags::bitflags! {
const FORCE_POINT_SIZE = 0x8;
/// Clamp `BuiltIn::FragDepth` output between 0 and 1.
const CLAMP_FRAG_DEPTH = 0x10;
/// Emit debug printf statements
const EMIT_DEBUG_PRINTF = 0x20;
}
}

Expand Down
20 changes: 12 additions & 8 deletions naga/src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ enum Indirection {
bitflags::bitflags! {
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
pub struct WriterFlags: u32 {
/// Always annotate the type information instead of inferring.
const EXPLICIT_TYPES = 0x1;
/// Emit debug printf statements
const EMIT_DEBUG_PRINTF = 0x2;
}
}

Expand Down Expand Up @@ -925,15 +927,17 @@ impl<W: Write> Writer<W> {
ref format,
ref arguments,
} => {
write!(self.out, "{level}")?;
write!(self.out, "debugPrintf(\"{format}\",")?;
for (index, &argument) in arguments.iter().enumerate() {
if index != 0 {
write!(self.out, ", ")?;
if self.flags.contains(WriterFlags::EMIT_DEBUG_PRINTF) {
write!(self.out, "{level}")?;
write!(self.out, "debugPrintf(\"{format}\",")?;
for (index, &argument) in arguments.iter().enumerate() {
if index != 0 {
write!(self.out, ", ")?;
}
self.write_expr(module, argument, func_ctx)?;
}
self.write_expr(module, argument, func_ctx)?;
writeln!(self.out, ");")?
}
writeln!(self.out, ");")?
}
}

Expand Down
1 change: 1 addition & 0 deletions naga/tests/in/binding-arrays.param.ron
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
god_mode: true,
hlsl: (
shader_model: V5_1,
flags: (""),
binding_map: {
(group: 0, binding: 0): (space: 0, register: 0, binding_array_size: Some(10)),
(group: 0, binding: 1): (space: 1, register: 0),
Expand Down
13 changes: 13 additions & 0 deletions naga/tests/in/debug-printf.param.ron
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
(
god_mode: true,
spv: (
version: (1, 1),
emit_debug_printf: true,
),
wgsl: (
emit_debug_printf: true,
),
glsl: (
version: Desktop(450),
writer_flags: ("EMIT_DEBUG_PRINTF"),
binding_map: {},
zero_initialize_workgroup_memory: true,
),
)
1 change: 1 addition & 0 deletions naga/tests/in/interface.param.ron
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
),
hlsl: (
shader_model: V5_1,
flags: (""),
binding_map: {},
fake_missing_bindings: false,
special_constants_binding: Some((space: 1, register: 0)),
Expand Down
1 change: 1 addition & 0 deletions naga/tests/in/push-constants.param.ron
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
),
hlsl: (
shader_model: V5_1,
flags: (""),
binding_map: {},
fake_missing_bindings: true,
special_constants_binding: Some((space: 1, register: 0)),
Expand Down
1 change: 1 addition & 0 deletions naga/tests/in/skybox.param.ron
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
),
hlsl: (
shader_model: V5_1,
flags: (""),
binding_map: {
(group: 0, binding: 0): (space: 0, register: 0),
(group: 0, binding: 1): (space: 0, register: 0),
Expand Down
13 changes: 13 additions & 0 deletions naga/tests/in/spv/debug-printf-s.param.ron
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
(
god_mode: true,
spv: (
version: (1, 1),
emit_debug_printf: true,
),
wgsl: (
emit_debug_printf: true,
),
glsl: (
version: Desktop(450),
writer_flags: ("EMIT_DEBUG_PRINTF"),
binding_map: {},
zero_initialize_workgroup_memory: true,
),
)
1 change: 0 additions & 1 deletion naga/tests/out/glsl/debug-printf-s.main.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;


void main_1() {
debugPrintfEXT("%d",42);
return;
}

Expand Down
7 changes: 2 additions & 5 deletions naga/tests/out/glsl/debug-printf.main.Compute.glsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#version 310 es
#version 450 core
#extension GL_ARB_compute_shader : require
#extension GL_EXT_debug_printf : enable

precision highp float;
precision highp int;

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;


Expand Down
1 change: 0 additions & 1 deletion naga/tests/out/wgsl/debug-printf-s.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
fn main_1() {
debugPrintf("%d",42);
return;
}

Expand Down
12 changes: 12 additions & 0 deletions naga/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct SpirvOutParameters {
#[serde(default)]
separate_entry_points: bool,
#[serde(default)]
emit_debug_printf: bool,
#[serde(default)]
#[cfg(all(feature = "deserialize", feature = "spv-out"))]
binding_map: naga::back::spv::BindingMap,
}
Expand All @@ -57,6 +59,8 @@ struct SpirvOutParameters {
struct WgslOutParameters {
#[serde(default)]
explicit_types: bool,
#[serde(default)]
emit_debug_printf: bool,
}

#[derive(Default, serde::Deserialize)]
Expand Down Expand Up @@ -405,6 +409,10 @@ fn write_output_spv(
);
flags.set(spv::WriterFlags::FORCE_POINT_SIZE, params.force_point_size);
flags.set(spv::WriterFlags::CLAMP_FRAG_DEPTH, params.clamp_frag_depth);
flags.set(
spv::WriterFlags::EMIT_DEBUG_PRINTF,
params.emit_debug_printf,
);

let options = spv::Options {
lang_version: (params.version.0, params.version.1),
Expand Down Expand Up @@ -589,6 +597,10 @@ fn write_output_wgsl(

let mut flags = wgsl::WriterFlags::empty();
flags.set(wgsl::WriterFlags::EXPLICIT_TYPES, params.explicit_types);
flags.set(
wgsl::WriterFlags::EMIT_DEBUG_PRINTF,
params.emit_debug_printf,
);

let string = wgsl::write_string(module, info, flags).expect("WGSL write failed");

Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/dx12/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@ impl crate::Device<super::Api> for super::Device {
// FXC doesn't support SM 6.0
None => hlsl::ShaderModel::V5_1,
},
flags: hlsl::WriterFlags::default(),
binding_map,
fake_missing_bindings: false,
special_constants_binding,
Expand Down
4 changes: 4 additions & 0 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,10 @@ impl super::Adapter {
// But this requires cloning the `spv::Options` struct, which has heap allocations.
true, // could check `super::Workarounds::SEPARATE_ENTRY_POINTS`
);
flags.set(
spv::WriterFlags::EMIT_DEBUG_PRINTF,
features.contains(wgt::Features::DEBUG_PRINTF),
);
spv::Options {
lang_version: (1, 0),
flags,
Expand Down

0 comments on commit 9c40eb4

Please sign in to comment.