Skip to content

Commit 3f64340

Browse files
committed
Removed return type inference from Image API
By referring to concrete 'sibling' vector types from same vector library based on input.
1 parent ebc143b commit 3f64340

11 files changed

+317
-102
lines changed

crates/spirv-std/src/float.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Traits and helper functions related to floats.
22
3-
use crate::vector::Vector;
3+
use crate::vector::{Vector, VectorTypeRef};
44
#[cfg(target_arch = "spirv")]
55
use core::arch::asm;
66

@@ -71,7 +71,10 @@ struct F32x2 {
7171
x: f32,
7272
y: f32,
7373
}
74-
unsafe impl Vector<f32, 2> for F32x2 {}
74+
unsafe impl Vector<f32, 2> for F32x2 { type VectorTypeLib = F32x2TypeLib; }
75+
76+
struct F32x2TypeLib;
77+
impl VectorTypeRef<f32, 2> for F32x2TypeLib { type Vector = F32x2; }
7578

7679
/// Converts an f32 (float) into an f16 (half). The result is a u32, not a u16, due to GPU support
7780
/// for u16 not being universal - the upper 16 bits will always be zero.

crates/spirv-std/src/image.rs

Lines changed: 92 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ pub use spirv_std_types::image_params::{
1414
AccessQualifier, Arrayed, Dimensionality, ImageDepth, ImageFormat, Multisampled, Sampled,
1515
};
1616

17-
use crate::{float::Float, integer::Integer, vector::Vector, Sampler};
17+
use crate::{
18+
float::Float,
19+
integer::Integer,
20+
vector::{Vector, VectorTypeRef},
21+
Sampler,
22+
};
1823

1924
/// Re-export of primitive types to ensure the `Image` proc macro always points
2025
/// to the right type.
@@ -119,12 +124,16 @@ impl<
119124
/// Fetch a single texel with a sampler set at compile time
120125
#[crate::macros::gpu_only]
121126
#[doc(alias = "OpImageFetch")]
122-
pub fn fetch<V, I>(&self, coordinate: impl ImageCoordinate<I, DIM, ARRAYED>) -> V
127+
pub fn fetch<I, C, const N: usize>(
128+
&self,
129+
coordinate: C,
130+
) -> <<C>::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
123131
where
124-
V: Vector<SampledType, 4>,
125132
I: Integer,
133+
C: ImageCoordinate<I, DIM, ARRAYED> + Vector<I, N>,
134+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
126135
{
127-
let mut result = V::default();
136+
let mut result = Default::default();
128137
unsafe {
129138
asm! {
130139
"%image = OpLoad _ {this}",
@@ -154,18 +163,19 @@ impl<
154163
#[crate::macros::gpu_only]
155164
#[doc(alias = "OpImageGather")]
156165
#[inline]
157-
pub fn gather<F, V>(
166+
pub fn gather<F, C, const N: usize>(
158167
&self,
159168
sampler: Sampler,
160-
coordinate: impl ImageCoordinate<F, DIM, ARRAYED>,
169+
coordinate: C,
161170
component: u32,
162-
) -> V
171+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
163172
where
164173
Self: HasGather,
165174
F: Float,
166-
V: Vector<SampledType, 4>,
175+
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
176+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
167177
{
168-
let mut result = V::default();
178+
let mut result = Default::default();
169179
unsafe {
170180
asm! {
171181
"%typeSampledImage = OpTypeSampledImage typeof*{this}",
@@ -187,10 +197,15 @@ impl<
187197

188198
/// Sample texels at `coord` from the image using `sampler`.
189199
#[crate::macros::gpu_only]
190-
pub fn sample<F, V>(&self, sampler: Sampler, coord: impl ImageCoordinate<F, DIM, ARRAYED>) -> V
200+
pub fn sample<F, C, const N: usize>(
201+
&self,
202+
sampler: Sampler,
203+
coord: C,
204+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
191205
where
192206
F: Float,
193-
V: Vector<SampledType, 4>,
207+
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
208+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
194209
{
195210
unsafe {
196211
let mut result = Default::default();
@@ -214,15 +229,16 @@ impl<
214229
/// Sample texels at `coord` from the image using `sampler`, after adding the input bias to the
215230
/// implicit level of detail.
216231
#[crate::macros::gpu_only]
217-
pub fn sample_bias<F, V>(
232+
pub fn sample_bias<F, C, const N: usize>(
218233
&self,
219234
sampler: Sampler,
220-
coord: impl ImageCoordinate<F, DIM, ARRAYED>,
235+
coord: C,
221236
bias: f32,
222-
) -> V
237+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
223238
where
224239
F: Float,
225-
V: Vector<SampledType, 4>,
240+
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
241+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
226242
{
227243
unsafe {
228244
let mut result = Default::default();
@@ -248,15 +264,16 @@ impl<
248264
#[crate::macros::gpu_only]
249265
#[doc(alias = "OpImageSampleExplicitLod")]
250266
/// Sample the image at a coordinate by a lod
251-
pub fn sample_by_lod<F, V>(
267+
pub fn sample_by_lod<F, C, const N: usize>(
252268
&self,
253269
sampler: Sampler,
254-
coordinate: impl ImageCoordinate<F, DIM, ARRAYED>,
270+
coordinate: C,
255271
lod: f32,
256-
) -> V
272+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
257273
where
258274
F: Float,
259-
V: Vector<SampledType, 4>,
275+
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
276+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
260277
{
261278
let mut result = Default::default();
262279
unsafe {
@@ -281,16 +298,17 @@ impl<
281298
#[crate::macros::gpu_only]
282299
#[doc(alias = "OpImageSampleExplicitLod")]
283300
/// Sample the image based on a gradient formed by (dx, dy). Specifically, ([du/dx, dv/dx], [du/dy, dv/dy])
284-
pub fn sample_by_gradient<F, V>(
301+
pub fn sample_by_gradient<F, C, const N: usize>(
285302
&self,
286303
sampler: Sampler,
287-
coordinate: impl ImageCoordinate<F, DIM, ARRAYED>,
304+
coordinate: C,
288305
gradient_dx: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
289306
gradient_dy: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
290-
) -> V
307+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
291308
where
292309
F: Float,
293-
V: Vector<SampledType, 4>,
310+
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
311+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
294312
{
295313
let mut result = Default::default();
296314
unsafe {
@@ -441,14 +459,15 @@ impl<
441459
/// Sample the image with a project coordinate
442460
#[crate::macros::gpu_only]
443461
#[doc(alias = "OpImageSampleProjImplicitLod")]
444-
pub fn sample_with_project_coordinate<F, V>(
462+
pub fn sample_with_project_coordinate<F, C, const N: usize>(
445463
&self,
446464
sampler: Sampler,
447-
project_coordinate: impl ImageCoordinate<F, DIM, { Arrayed::True as u32 }>,
448-
) -> V
465+
project_coordinate: C,
466+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
449467
where
450468
F: Float,
451-
V: Vector<SampledType, 4>,
469+
C: ImageCoordinate<F, DIM, { Arrayed::True as u32 }> + Vector<F, N>,
470+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
452471
{
453472
unsafe {
454473
let mut result = Default::default();
@@ -471,15 +490,16 @@ impl<
471490
#[crate::macros::gpu_only]
472491
#[doc(alias = "OpImageSampleProjExplicitLod")]
473492
/// Sample the image with a project coordinate by a lod
474-
pub fn sample_with_project_coordinate_by_lod<F, V>(
493+
pub fn sample_with_project_coordinate_by_lod<F, C, const N: usize>(
475494
&self,
476495
sampler: Sampler,
477-
project_coordinate: impl ImageCoordinate<F, DIM, { Arrayed::True as u32 }>,
496+
project_coordinate: C,
478497
lod: f32,
479-
) -> V
498+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
480499
where
481500
F: Float,
482-
V: Vector<SampledType, 4>,
501+
C: ImageCoordinate<F, DIM, { Arrayed::True as u32 }> + Vector<F, N>,
502+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
483503
{
484504
let mut result = Default::default();
485505
unsafe {
@@ -504,16 +524,17 @@ impl<
504524
#[crate::macros::gpu_only]
505525
#[doc(alias = "OpImageSampleProjExplicitLod")]
506526
/// Sample the image with a project coordinate based on a gradient formed by (dx, dy). Specifically, ([du/dx, dv/dx], [du/dy, dv/dy])
507-
pub fn sample_with_project_coordinate_by_gradient<F, V>(
527+
pub fn sample_with_project_coordinate_by_gradient<F, C, const N: usize>(
508528
&self,
509529
sampler: Sampler,
510-
project_coordinate: impl ImageCoordinate<F, DIM, { Arrayed::True as u32 }>,
530+
project_coordinate: C,
511531
gradient_dx: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
512532
gradient_dy: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
513-
) -> V
533+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
514534
where
515535
F: Float,
516-
V: Vector<SampledType, 4>,
536+
C: ImageCoordinate<F, DIM, { Arrayed::True as u32 }> + Vector<F, N>,
537+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
517538
{
518539
let mut result = Default::default();
519540
unsafe {
@@ -656,12 +677,16 @@ impl<
656677
/// Read a texel from an image without a sampler.
657678
#[crate::macros::gpu_only]
658679
#[doc(alias = "OpImageRead")]
659-
pub fn read<I, V, const N: usize>(&self, coordinate: impl ImageCoordinate<I, DIM, ARRAYED>) -> V
680+
pub fn read<I, C, const N: usize>(
681+
&self,
682+
coordinate: C,
683+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
660684
where
661685
I: Integer,
662-
V: Vector<SampledType, N>,
686+
C: ImageCoordinate<I, DIM, ARRAYED> + Vector<I, N>,
687+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
663688
{
664-
let mut result = V::default();
689+
let mut result = Default::default();
665690

666691
unsafe {
667692
asm! {
@@ -712,12 +737,16 @@ impl<
712737
/// Read a texel from an image without a sampler.
713738
#[crate::macros::gpu_only]
714739
#[doc(alias = "OpImageRead")]
715-
pub fn read<I, V, const N: usize>(&self, coordinate: impl ImageCoordinate<I, DIM, ARRAYED>) -> V
740+
pub fn read<I, C, const N: usize>(
741+
&self,
742+
coordinate: C,
743+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
716744
where
717745
I: Integer,
718-
V: Vector<SampledType, N>,
746+
C: ImageCoordinate<I, DIM, ARRAYED> + Vector<I, N>,
747+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
719748
{
720-
let mut result = V::default();
749+
let mut result = Default::default();
721750

722751
unsafe {
723752
asm! {
@@ -777,15 +806,16 @@ impl<
777806
/// Note: Vulkan only allows the read if the first two components of the coordinate are zero.
778807
#[crate::macros::gpu_only]
779808
#[doc(alias = "OpImageRead")]
780-
pub fn read_subpass<I, V, const N: usize>(
809+
pub fn read_subpass<I, C, const N: usize>(
781810
&self,
782-
coordinate: impl ImageCoordinateSubpassData<I, ARRAYED>,
783-
) -> V
811+
coordinate: C,
812+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
784813
where
785814
I: Integer,
786-
V: Vector<SampledType, N>,
815+
C: ImageCoordinateSubpassData<I, ARRAYED> + Vector<I, N>,
816+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
787817
{
788-
let mut result = V::default();
818+
let mut result = Default::default();
789819

790820
unsafe {
791821
asm! {
@@ -838,13 +868,15 @@ impl<
838868
/// detail relative to the base level.
839869
#[crate::macros::gpu_only]
840870
#[doc(alias = "OpImageQueryLod")]
841-
pub fn query_lod<V: Vector<f32, 2>>(
871+
pub fn query_lod<C, const N: usize>(
842872
&self,
843873
sampler: Sampler,
844-
coord: impl ImageCoordinate<f32, DIM, { Arrayed::False as u32 }>,
845-
) -> V
874+
coord: C,
875+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 2>>::Vector
846876
where
847877
Self: HasQueryLevels,
878+
C: ImageCoordinate<f32, DIM, { Arrayed::False as u32 }> + Vector<f32, N>,
879+
C::VectorTypeLib: VectorTypeRef<SampledType, 2>,
848880
{
849881
// Note: Arrayed::False isn't a typo in the ImageCoordinate, the spec states:
850882
// Coordinate must be a scalar or vector of floating-point type or integer type. It
@@ -988,10 +1020,14 @@ impl<
9881020
/// Sampling with a type (`S`) that doesn't match the image's image format
9891021
/// will result in undefined behaviour.
9901022
#[crate::macros::gpu_only]
991-
pub unsafe fn sample<F, V>(&self, coord: impl ImageCoordinate<F, DIM, ARRAYED>) -> V
1023+
pub unsafe fn sample<F, C, const N: usize>(
1024+
&self,
1025+
coord: C,
1026+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
9921027
where
9931028
F: Float,
994-
V: Vector<SampledType, 4>,
1029+
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
1030+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
9951031
{
9961032
let mut result = Default::default();
9971033
asm!(
@@ -1012,14 +1048,15 @@ impl<
10121048
/// Sampling with a type (`S`) that doesn't match the image's image format
10131049
/// will result in undefined behaviour.
10141050
#[crate::macros::gpu_only]
1015-
pub unsafe fn sample_by_lod<F, V>(
1051+
pub unsafe fn sample_by_lod<F, C, const N: usize>(
10161052
&self,
1017-
coord: impl ImageCoordinate<F, DIM, ARRAYED>,
1053+
coord: C,
10181054
lod: f32,
1019-
) -> V
1055+
) -> <C::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
10201056
where
10211057
F: Float,
1022-
V: Vector<SampledType, 4>,
1058+
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
1059+
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
10231060
{
10241061
let mut result = Default::default();
10251062
asm!(

0 commit comments

Comments
 (0)