Skip to content

Commit

Permalink
debug_printf: spv-in
Browse files Browse the repository at this point in the history
  • Loading branch information
exrook committed Oct 16, 2023
1 parent c6aac9b commit 54f02a5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
10 changes: 6 additions & 4 deletions src/front/spv/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ pub enum Error {
UnsupportedExtension(String),
#[error("unsupported extension set {0}")]
UnsupportedExtSet(String),
#[error("unsupported extension instantiation set %{0}")]
UnsupportedExtInstSet(spirv::Word),
#[error("unsupported extension instantiation %{0}")]
UnsupportedExtInst(spirv::Word),
#[error("unsupported extension instantiation instruction id %{0} from set %{1}")]
UnsupportedExtInst(spirv::Word, &'static str),
#[error(
"extension instantiation references id %{0} which is not an imported extension instantation set"
)]
InvalidExtInst(spirv::Word),
#[error("unsupported type {0:?}")]
UnsupportedType(Handle<crate::Type>),
#[error("unsupported execution model %{0}")]
Expand Down
48 changes: 41 additions & 7 deletions src/front/spv/ext_inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct ExtInst {
}

impl<I: Iterator<Item = u32>> super::Frontend<I> {
#[allow(clippy::too_many_arguments)]
pub(super) fn parse_ext_inst(
&mut self,
inst: super::Instruction,
Expand All @@ -32,20 +33,53 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
let ext_name = if let Some(name) = self.ext_inst_imports.get(&ext_inst.set_id) {
name
} else {
return Err(Error::UnsupportedExtInstSet(ext_inst.set_id));
// We get here only if the set_id doesn't point to an earlier OpExtInstImport.
// If the earlier ExtInstSet was unsupported we would have emitted an error then.
return Err(Error::InvalidExtInst(ext_inst.set_id));
};

match *ext_name {
"GLSL.std.450" => self.parse_ext_inst_glsl_std(
inst, ext_inst, span, ctx, emitter, block, block_id, body_idx,
ext_name, inst, ext_inst, span, ctx, emitter, block, block_id, body_idx,
),
_ => {
return Err(Error::UnsupportedExtInstSet(ext_inst.set_id));
"NonSemantic.DebugPrintf" if ext_inst.inst_id == 1 => {
self.parse_ext_inst_debug_printf(inst, span, ctx, emitter, block, body_idx)
}
_ => Err(Error::UnsupportedExtInst(ext_inst.inst_id, ext_name)),
}
}
fn parse_ext_inst_debug_printf(
&mut self,
inst: super::Instruction,
span: crate::Span,
ctx: &mut super::BlockContext,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
body_idx: usize,
) -> Result<(), Error> {
let base_wc = 5;
inst.expect_at_least(base_wc + 1)?;
let format_id = self.next()?;
let format = self.strings.lookup(format_id)?.clone();

block.extend(emitter.finish(ctx.expressions));

let mut arguments = Vec::with_capacity(inst.wc as usize - (base_wc as usize + 1));
for _ in 0..arguments.capacity() {
let arg_id = self.next()?;
let lexp = self.lookup_expression.lookup(arg_id)?;
arguments.push(self.get_expr_handle(arg_id, lexp, ctx, emitter, block, body_idx));
}

block.push(crate::Statement::DebugPrintf { format, arguments }, span);
emitter.start(ctx.expressions);

Ok(())
}
#[allow(clippy::too_many_arguments)]
fn parse_ext_inst_glsl_std(
&mut self,
set_name: &'static str,
inst: super::Instruction,
ext_inst: ExtInst,
span: crate::Span,
Expand All @@ -59,8 +93,8 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
use crate::MathFunction as Mf;
use spirv::GLOp as Glo;

let gl_op =
Glo::from_u32(ext_inst.inst_id).ok_or(Error::UnsupportedExtInst(ext_inst.inst_id))?;
let gl_op = Glo::from_u32(ext_inst.inst_id)
.ok_or(Error::UnsupportedExtInst(ext_inst.inst_id, set_name))?;

let fun = match gl_op {
Glo::Round => Mf::Round,
Expand Down Expand Up @@ -124,7 +158,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
Glo::UnpackSnorm2x16 => Mf::Unpack2x16snorm,
Glo::FindILsb => Mf::FindLsb,
Glo::FindUMsb | Glo::FindSMsb => Mf::FindMsb,
_ => return Err(Error::UnsupportedExtInst(ext_inst.inst_id)),
_ => return Err(Error::UnsupportedExtInst(ext_inst.inst_id, set_name)),
};

let arg_count = fun.argument_count();
Expand Down
10 changes: 7 additions & 3 deletions src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ pub const SUPPORTED_EXTENSIONS: &[&str] = &[
"SPV_KHR_storage_buffer_storage_class",
"SPV_KHR_vulkan_memory_model",
"SPV_KHR_multiview",
"SPV_KHR_non_semantic_info",
];
pub const SUPPORTED_EXT_SETS: &[&str] = &["GLSL.std.450"];
pub const SUPPORTED_EXT_SETS: &[&str] = &["GLSL.std.450", "NonSemantic.DebugPrintf"];

#[derive(Copy, Clone)]
pub struct Instruction {
Expand Down Expand Up @@ -566,6 +567,7 @@ pub struct Frontend<I> {
layouter: Layouter,
temp_bytes: Vec<u8>,
ext_inst_imports: FastHashMap<spirv::Word, &'static str>,
strings: FastHashMap<spirv::Word, String>,
future_decor: FastHashMap<spirv::Word, Decoration>,
future_member_decor: FastHashMap<(spirv::Word, MemberIndex), Decoration>,
lookup_member: FastHashMap<(Handle<crate::Type>, MemberIndex), LookupMember>,
Expand Down Expand Up @@ -619,6 +621,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
layouter: Layouter::default(),
temp_bytes: Vec::new(),
ext_inst_imports: FastHashMap::default(),
strings: FastHashMap::default(),
future_decor: FastHashMap::default(),
future_member_decor: FastHashMap::default(),
handle_sampling: FastHashMap::default(),
Expand Down Expand Up @@ -4025,8 +4028,9 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
fn parse_string(&mut self, inst: Instruction) -> Result<(), Error> {
self.switch(ModuleState::Source, inst.op)?;
inst.expect_at_least(3)?;
let _id = self.next()?;
let (_name, _) = self.next_string(inst.wc - 2)?;
let id = self.next()?;
let (name, _) = self.next_string(inst.wc - 2)?;
self.strings.entry(id).or_insert(name);
Ok(())
}

Expand Down

0 comments on commit 54f02a5

Please sign in to comment.