Skip to content

Removed return type inference from Image API #987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/spirv-std/src/float.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Traits and helper functions related to floats.

use crate::vector::Vector;
use crate::vector::{Vector, VectorTypeRef};
#[cfg(target_arch = "spirv")]
use core::arch::asm;

Expand Down Expand Up @@ -71,7 +71,10 @@ struct F32x2 {
x: f32,
y: f32,
}
unsafe impl Vector<f32, 2> for F32x2 {}
unsafe impl Vector<f32, 2> for F32x2 { type VectorTypeLib = F32x2TypeLib; }

struct F32x2TypeLib;
impl VectorTypeRef<f32, 2> for F32x2TypeLib { type Vector = F32x2; }

/// Converts an f32 (float) into an f16 (half). The result is a u32, not a u16, due to GPU support
/// for u16 not being universal - the upper 16 bits will always be zero.
Expand Down
139 changes: 84 additions & 55 deletions crates/spirv-std/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ pub use spirv_std_types::image_params::{
AccessQualifier, Arrayed, Dimensionality, ImageDepth, ImageFormat, Multisampled, Sampled,
};

use crate::{float::Float, integer::Integer, vector::Vector, Sampler};
use crate::{
float::Float,
integer::Integer,
vector::{Vector, VectorTypeRef},
Sampler, VectorFromVector,
};

/// Re-export of primitive types to ensure the `Image` proc macro always points
/// to the right type.
Expand Down Expand Up @@ -119,12 +124,14 @@ impl<
/// Fetch a single texel with a sampler set at compile time
#[crate::macros::gpu_only]
#[doc(alias = "OpImageFetch")]
pub fn fetch<V, I>(&self, coordinate: impl ImageCoordinate<I, DIM, ARRAYED>) -> V
pub fn fetch<I, C, const N: usize>(&self, coordinate: C) -> VectorFromVector!(C, SampledType, 4)
//<<C>::VectorTypeLib as VectorTypeRef<SampledType, 4>>::Vector
where
V: Vector<SampledType, 4>,
I: Integer,
C: ImageCoordinate<I, DIM, ARRAYED> + Vector<I, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = V::default();
let mut result = Default::default();
unsafe {
asm! {
"%image = OpLoad _ {this}",
Expand Down Expand Up @@ -154,18 +161,19 @@ impl<
#[crate::macros::gpu_only]
#[doc(alias = "OpImageGather")]
#[inline]
pub fn gather<F, V>(
pub fn gather<F, C, const N: usize>(
&self,
sampler: Sampler,
coordinate: impl ImageCoordinate<F, DIM, ARRAYED>,
coordinate: C,
component: u32,
) -> V
) -> VectorFromVector!(C, SampledType, 4)
where
Self: HasGather,
F: Float,
V: Vector<SampledType, 4>,
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = V::default();
let mut result = Default::default();
unsafe {
asm! {
"%typeSampledImage = OpTypeSampledImage typeof*{this}",
Expand All @@ -187,10 +195,15 @@ impl<

/// Sample texels at `coord` from the image using `sampler`.
#[crate::macros::gpu_only]
pub fn sample<F, V>(&self, sampler: Sampler, coord: impl ImageCoordinate<F, DIM, ARRAYED>) -> V
pub fn sample<F, C, const N: usize>(
&self,
sampler: Sampler,
coord: C,
) -> VectorFromVector!(C, SampledType, 4)
where
F: Float,
V: Vector<SampledType, 4>,
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
unsafe {
let mut result = Default::default();
Expand All @@ -214,15 +227,16 @@ impl<
/// Sample texels at `coord` from the image using `sampler`, after adding the input bias to the
/// implicit level of detail.
#[crate::macros::gpu_only]
pub fn sample_bias<F, V>(
pub fn sample_bias<F, C, const N: usize>(
&self,
sampler: Sampler,
coord: impl ImageCoordinate<F, DIM, ARRAYED>,
coord: C,
bias: f32,
) -> V
) -> VectorFromVector!(C, SampledType, 4)
where
F: Float,
V: Vector<SampledType, 4>,
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
unsafe {
let mut result = Default::default();
Expand All @@ -248,15 +262,16 @@ impl<
#[crate::macros::gpu_only]
#[doc(alias = "OpImageSampleExplicitLod")]
/// Sample the image at a coordinate by a lod
pub fn sample_by_lod<F, V>(
pub fn sample_by_lod<F, C, const N: usize>(
&self,
sampler: Sampler,
coordinate: impl ImageCoordinate<F, DIM, ARRAYED>,
coordinate: C,
lod: f32,
) -> V
) -> VectorFromVector!(C, SampledType, 4)
where
F: Float,
V: Vector<SampledType, 4>,
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = Default::default();
unsafe {
Expand All @@ -281,16 +296,17 @@ impl<
#[crate::macros::gpu_only]
#[doc(alias = "OpImageSampleExplicitLod")]
/// Sample the image based on a gradient formed by (dx, dy). Specifically, ([du/dx, dv/dx], [du/dy, dv/dy])
pub fn sample_by_gradient<F, V>(
pub fn sample_by_gradient<F, C, const N: usize>(
&self,
sampler: Sampler,
coordinate: impl ImageCoordinate<F, DIM, ARRAYED>,
coordinate: C,
gradient_dx: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
gradient_dy: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
) -> V
) -> VectorFromVector!(C, SampledType, 4)
where
F: Float,
V: Vector<SampledType, 4>,
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = Default::default();
unsafe {
Expand Down Expand Up @@ -441,14 +457,15 @@ impl<
/// Sample the image with a project coordinate
#[crate::macros::gpu_only]
#[doc(alias = "OpImageSampleProjImplicitLod")]
pub fn sample_with_project_coordinate<F, V>(
pub fn sample_with_project_coordinate<F, C, const N: usize>(
&self,
sampler: Sampler,
project_coordinate: impl ImageCoordinate<F, DIM, { Arrayed::True as u32 }>,
) -> V
project_coordinate: C,
) -> VectorFromVector!(C, SampledType, 4)
where
F: Float,
V: Vector<SampledType, 4>,
C: ImageCoordinate<F, DIM, { Arrayed::True as u32 }> + Vector<F, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
unsafe {
let mut result = Default::default();
Expand All @@ -471,15 +488,16 @@ impl<
#[crate::macros::gpu_only]
#[doc(alias = "OpImageSampleProjExplicitLod")]
/// Sample the image with a project coordinate by a lod
pub fn sample_with_project_coordinate_by_lod<F, V>(
pub fn sample_with_project_coordinate_by_lod<F, C, const N: usize>(
&self,
sampler: Sampler,
project_coordinate: impl ImageCoordinate<F, DIM, { Arrayed::True as u32 }>,
project_coordinate: C,
lod: f32,
) -> V
) -> VectorFromVector!(C, SampledType, 4)
where
F: Float,
V: Vector<SampledType, 4>,
C: ImageCoordinate<F, DIM, { Arrayed::True as u32 }> + Vector<F, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = Default::default();
unsafe {
Expand All @@ -504,16 +522,17 @@ impl<
#[crate::macros::gpu_only]
#[doc(alias = "OpImageSampleProjExplicitLod")]
/// Sample the image with a project coordinate based on a gradient formed by (dx, dy). Specifically, ([du/dx, dv/dx], [du/dy, dv/dy])
pub fn sample_with_project_coordinate_by_gradient<F, V>(
pub fn sample_with_project_coordinate_by_gradient<F, C, const N: usize>(
&self,
sampler: Sampler,
project_coordinate: impl ImageCoordinate<F, DIM, { Arrayed::True as u32 }>,
project_coordinate: C,
gradient_dx: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
gradient_dy: impl ImageCoordinate<F, DIM, { Arrayed::False as u32 }>,
) -> V
) -> VectorFromVector!(C, SampledType, 4)
where
F: Float,
V: Vector<SampledType, 4>,
C: ImageCoordinate<F, DIM, { Arrayed::True as u32 }> + Vector<F, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = Default::default();
unsafe {
Expand Down Expand Up @@ -656,12 +675,13 @@ impl<
/// Read a texel from an image without a sampler.
#[crate::macros::gpu_only]
#[doc(alias = "OpImageRead")]
pub fn read<I, V, const N: usize>(&self, coordinate: impl ImageCoordinate<I, DIM, ARRAYED>) -> V
pub fn read<I, C, const N: usize>(&self, coordinate: C) -> VectorFromVector!(C, SampledType, 4)
where
I: Integer,
V: Vector<SampledType, N>,
C: ImageCoordinate<I, DIM, ARRAYED> + Vector<I, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = V::default();
let mut result = Default::default();

unsafe {
asm! {
Expand Down Expand Up @@ -712,12 +732,13 @@ impl<
/// Read a texel from an image without a sampler.
#[crate::macros::gpu_only]
#[doc(alias = "OpImageRead")]
pub fn read<I, V, const N: usize>(&self, coordinate: impl ImageCoordinate<I, DIM, ARRAYED>) -> V
pub fn read<I, C, const N: usize>(&self, coordinate: C) -> VectorFromVector!(C, SampledType, 4)
where
I: Integer,
V: Vector<SampledType, N>,
C: ImageCoordinate<I, DIM, ARRAYED> + Vector<I, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = V::default();
let mut result = Default::default();

unsafe {
asm! {
Expand Down Expand Up @@ -777,15 +798,16 @@ impl<
/// Note: Vulkan only allows the read if the first two components of the coordinate are zero.
#[crate::macros::gpu_only]
#[doc(alias = "OpImageRead")]
pub fn read_subpass<I, V, const N: usize>(
pub fn read_subpass<I, C, const N: usize>(
&self,
coordinate: impl ImageCoordinateSubpassData<I, ARRAYED>,
) -> V
coordinate: C,
) -> VectorFromVector!(C, SampledType, 4)
where
I: Integer,
V: Vector<SampledType, N>,
C: ImageCoordinateSubpassData<I, ARRAYED> + Vector<I, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = V::default();
let mut result = Default::default();

unsafe {
asm! {
Expand Down Expand Up @@ -838,13 +860,15 @@ impl<
/// detail relative to the base level.
#[crate::macros::gpu_only]
#[doc(alias = "OpImageQueryLod")]
pub fn query_lod<V: Vector<f32, 2>>(
pub fn query_lod<C, const N: usize>(
&self,
sampler: Sampler,
coord: impl ImageCoordinate<f32, DIM, { Arrayed::False as u32 }>,
) -> V
coord: C,
) -> VectorFromVector!(C, SampledType, 2)
where
Self: HasQueryLevels,
C: ImageCoordinate<f32, DIM, { Arrayed::False as u32 }> + Vector<f32, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 2>,
{
// Note: Arrayed::False isn't a typo in the ImageCoordinate, the spec states:
// Coordinate must be a scalar or vector of floating-point type or integer type. It
Expand Down Expand Up @@ -988,10 +1012,14 @@ impl<
/// Sampling with a type (`S`) that doesn't match the image's image format
/// will result in undefined behaviour.
#[crate::macros::gpu_only]
pub unsafe fn sample<F, V>(&self, coord: impl ImageCoordinate<F, DIM, ARRAYED>) -> V
pub unsafe fn sample<F, C, const N: usize>(
&self,
coord: C,
) -> VectorFromVector!(C, SampledType, 4)
where
F: Float,
V: Vector<SampledType, 4>,
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = Default::default();
asm!(
Expand All @@ -1012,14 +1040,15 @@ impl<
/// Sampling with a type (`S`) that doesn't match the image's image format
/// will result in undefined behaviour.
#[crate::macros::gpu_only]
pub unsafe fn sample_by_lod<F, V>(
pub unsafe fn sample_by_lod<F, C, const N: usize>(
&self,
coord: impl ImageCoordinate<F, DIM, ARRAYED>,
coord: C,
lod: f32,
) -> V
) -> VectorFromVector!(C, SampledType, 4)
where
F: Float,
V: Vector<SampledType, 4>,
C: ImageCoordinate<F, DIM, ARRAYED> + Vector<F, N>,
C::VectorTypeLib: VectorTypeRef<SampledType, 4>,
{
let mut result = Default::default();
asm!(
Expand Down
Loading