Skip to content

Commit

Permalink
[spv] Fix OpImageQueries to allow Uints (#2404)
Browse files Browse the repository at this point in the history
  • Loading branch information
evahop authored Jul 24, 2023
1 parent bac2d82 commit 5f8e4f6
Show file tree
Hide file tree
Showing 5 changed files with 978 additions and 1,066 deletions.
89 changes: 12 additions & 77 deletions src/back/spv/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ impl<'w> BlockContext<'w> {
};
let extended_size_type_id = self.get_type_id(LookupType::Local(LocalType::Value {
vector_size,
kind: crate::ScalarKind::Sint,
kind: crate::ScalarKind::Uint,
width: 4,
pointer_space: None,
}));
Expand Down Expand Up @@ -1077,24 +1077,7 @@ impl<'w> BlockContext<'w> {
}
block.body.push(inst);

let bitcast_type_id = self.get_type_id(
LocalType::Value {
vector_size,
kind: crate::ScalarKind::Uint,
width: 4,
pointer_space: None,
}
.into(),
);
let bitcast_id = self.gen_id();
block.body.push(Instruction::unary(
spirv::Op::Bitcast,
bitcast_type_id,
bitcast_id,
id_extended,
));

if result_type_id != bitcast_type_id {
if result_type_id != extended_size_type_id {
let id = self.gen_id();
let components = match dim {
// always pick the first component, and duplicate it for all 3 dimensions
Expand All @@ -1104,42 +1087,26 @@ impl<'w> BlockContext<'w> {
block.body.push(Instruction::vector_shuffle(
result_type_id,
id,
bitcast_id,
bitcast_id,
id_extended,
id_extended,
components,
));

id
} else {
bitcast_id
id_extended
}
}
Iq::NumLevels => {
let query_id = self.gen_id();
block.body.push(Instruction::image_query(
spirv::Op::ImageQueryLevels,
self.get_type_id(
LocalType::Value {
vector_size: None,
kind: crate::ScalarKind::Sint,
width: 4,
pointer_space: None,
}
.into(),
),
query_id,
image_id,
));

let id = self.gen_id();
block.body.push(Instruction::unary(
spirv::Op::Bitcast,
result_type_id,
id,
query_id,
image_id,
));

id
query_id
}
Iq::NumLayers => {
let vec_size = match dim {
Expand All @@ -1149,7 +1116,7 @@ impl<'w> BlockContext<'w> {
};
let extended_size_type_id = self.get_type_id(LookupType::Local(LocalType::Value {
vector_size: Some(vec_size),
kind: crate::ScalarKind::Sint,
kind: crate::ScalarKind::Uint,
width: 4,
pointer_space: None,
}));
Expand All @@ -1165,56 +1132,24 @@ impl<'w> BlockContext<'w> {

let extract_id = self.gen_id();
block.body.push(Instruction::composite_extract(
self.get_type_id(
LocalType::Value {
vector_size: None,
kind: crate::ScalarKind::Sint,
width: 4,
pointer_space: None,
}
.into(),
),
result_type_id,
extract_id,
id_extended,
&[vec_size as u32 - 1],
));

let id = self.gen_id();
block.body.push(Instruction::unary(
spirv::Op::Bitcast,
result_type_id,
id,
extract_id,
));

id
extract_id
}
Iq::NumSamples => {
let query_id = self.gen_id();
block.body.push(Instruction::image_query(
spirv::Op::ImageQuerySamples,
self.get_type_id(
LocalType::Value {
vector_size: None,
kind: crate::ScalarKind::Sint,
width: 4,
pointer_space: None,
}
.into(),
),
query_id,
image_id,
));

let id = self.gen_id();
block.body.push(Instruction::unary(
spirv::Op::Bitcast,
result_type_id,
id,
query_id,
image_id,
));

id
query_id
}
};

Expand Down
42 changes: 31 additions & 11 deletions src/front/spv/image.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::arena::{Arena, Handle, UniqueArena};
use crate::arena::{Handle, UniqueArena};

use super::{Error, LookupExpression, LookupHelper as _};

Expand Down Expand Up @@ -689,11 +689,20 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
image: image_lexp.handle,
query: crate::ImageQuery::Size { level },
};
let expr = crate::Expression::As {
expr: ctx.expressions.append(expr, self.span_from_with_op(start)),
kind: crate::ScalarKind::Sint,
convert: Some(4),

let result_type_handle = self.lookup_type.lookup(result_type_id)?.handle;
let maybe_scalar_kind = ctx.type_arena[result_type_handle].inner.scalar_kind();

let expr = if maybe_scalar_kind == Some(crate::ScalarKind::Sint) {
crate::Expression::As {
expr: ctx.expressions.append(expr, self.span_from_with_op(start)),
kind: crate::ScalarKind::Sint,
convert: Some(4),
}
} else {
expr
};

self.lookup_expression.insert(
result_id,
LookupExpression {
Expand All @@ -702,13 +711,14 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
block_id,
},
);

Ok(())
}

pub(super) fn parse_image_query_other(
&mut self,
query: crate::ImageQuery,
expressions: &mut Arena<crate::Expression>,
ctx: &mut super::BlockContext,
block_id: spirv::Word,
) -> Result<(), Error> {
let start = self.data_offset;
Expand All @@ -724,19 +734,29 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
image: image_lexp.handle,
query,
};
let expr = crate::Expression::As {
expr: expressions.append(expr, self.span_from_with_op(start)),
kind: crate::ScalarKind::Sint,
convert: Some(4),

let result_type_handle = self.lookup_type.lookup(result_type_id)?.handle;
let maybe_scalar_kind = ctx.type_arena[result_type_handle].inner.scalar_kind();

let expr = if maybe_scalar_kind == Some(crate::ScalarKind::Sint) {
crate::Expression::As {
expr: ctx.expressions.append(expr, self.span_from_with_op(start)),
kind: crate::ScalarKind::Sint,
convert: Some(4),
}
} else {
expr
};

self.lookup_expression.insert(
result_id,
LookupExpression {
handle: expressions.append(expr, self.span_from_with_op(start)),
handle: ctx.expressions.append(expr, self.span_from_with_op(start)),
type_id: result_type_id,
block_id,
},
);

Ok(())
}
}
12 changes: 2 additions & 10 deletions src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2695,19 +2695,11 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
}
Op::ImageQueryLevels => {
inst.expect(4)?;
self.parse_image_query_other(
crate::ImageQuery::NumLevels,
ctx.expressions,
block_id,
)?;
self.parse_image_query_other(crate::ImageQuery::NumLevels, ctx, block_id)?;
}
Op::ImageQuerySamples => {
inst.expect(4)?;
self.parse_image_query_other(
crate::ImageQuery::NumSamples,
ctx.expressions,
block_id,
)?;
self.parse_image_query_other(crate::ImageQuery::NumSamples, ctx, block_id)?;
}
// other ops
Op::Select => {
Expand Down
Loading

0 comments on commit 5f8e4f6

Please sign in to comment.