Skip to content

Commit

Permalink
spv-in sampling from depth textures returns a scalar, and is splatted…
Browse files Browse the repository at this point in the history
… to a vector for SPIR-V compatibility, fixes gfx-rs#4551
  • Loading branch information
schell committed Oct 8, 2024
1 parent 3693da2 commit dfc3783
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
67 changes: 49 additions & 18 deletions naga/src/front/spv/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,36 +377,54 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
let coord_handle =
self.get_expr_handle(coordinate_id, coord_lexp, ctx, emitter, block, body_idx);
let coord_type_handle = self.lookup_type.lookup(coord_lexp.type_id)?.handle;
let (coordinate, array_index) = match ctx.type_arena[image_ty].inner {
let (coordinate, array_index, is_depth) = match ctx.type_arena[image_ty].inner {
crate::TypeInner::Image {
dim,
arrayed,
class: _,
} => extract_image_coordinates(
dim,
if arrayed {
ExtraCoordinate::ArrayLayer
} else {
ExtraCoordinate::Garbage
},
coord_handle,
coord_type_handle,
ctx,
),
class,
} => {
let (coord, array_index) = extract_image_coordinates(
dim,
if arrayed {
ExtraCoordinate::ArrayLayer
} else {
ExtraCoordinate::Garbage
},
coord_handle,
coord_type_handle,
ctx,
);
(coord, array_index, class.is_depth())
}
_ => return Err(Error::InvalidImage(image_ty)),
};

let expr = crate::Expression::ImageLoad {
let image_load_expr = crate::Expression::ImageLoad {
image: image_lexp.handle,
coordinate,
array_index,
sample,
level,
};
let image_load_handle = ctx
.expressions
.append(image_load_expr, self.span_from_with_op(start));

let handle = if is_depth {
let splat_expr = crate::Expression::Splat {
size: crate::VectorSize::Quad,
value: image_load_handle,
};
ctx.expressions
.append(splat_expr, self.span_from_with_op(start))
} else {
image_load_handle
};

self.lookup_expression.insert(
result_id,
LookupExpression {
handle: ctx.expressions.append(expr, self.span_from_with_op(start)),
handle,
type_id: result_type_id,
block_id,
},
Expand Down Expand Up @@ -593,11 +611,12 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
ref other => return Err(Error::InvalidGlobalVar(other.clone())),
}

let ((coordinate, array_index), depth_ref) = match ctx.type_arena[image_ty].inner {
let ((coordinate, array_index), depth_ref, is_depth) = match ctx.type_arena[image_ty].inner
{
crate::TypeInner::Image {
dim,
arrayed,
class: _,
class,
} => (
extract_image_coordinates(
dim,
Expand Down Expand Up @@ -642,6 +661,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
None => None,
}
},
class.is_depth(),
),
_ => return Err(Error::InvalidImage(image_ty)),
};
Expand All @@ -656,10 +676,21 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
level,
depth_ref,
};
let image_sample_handle = ctx.expressions.append(expr, self.span_from_with_op(start));
let handle = if is_depth {
let splat_expr = crate::Expression::Splat {
size: crate::VectorSize::Quad,
value: image_sample_handle,
};
ctx.expressions
.append(splat_expr, self.span_from_with_op(start))
} else {
image_sample_handle
};
self.lookup_expression.insert(
result_id,
LookupExpression {
handle: ctx.expressions.append(expr, self.span_from_with_op(start)),
handle,
type_id: result_type_id,
block_id,
},
Expand Down
4 changes: 4 additions & 0 deletions naga/src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ impl super::ImageClass {
crate::ImageClass::Storage { .. } => false,
}
}

pub const fn is_depth(self) -> bool {
matches!(self, crate::ImageClass::Depth { .. })
}
}

impl crate::Module {
Expand Down

0 comments on commit dfc3783

Please sign in to comment.