Skip to content
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

naga: Add support for GLSL signed/unsigned samplers #6513

Merged
merged 6 commits into from
Nov 15, 2024
Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148]
- Fix an issue where `naga` CLI would incorrectly skip the first positional argument when `--stdin-file-path` was specified. By @ErichDonGubler in [#6480](https://github.com/gfx-rs/wgpu/pull/6480).
- Fix textureNumLevels in the GLSL backend. By @magcius in [#6483](https://github.com/gfx-rs/wgpu/pull/6483).
- Implement `quantizeToF16()` for WGSL frontend, and WGSL, SPIR-V, HLSL, MSL, and GLSL backends. By @jamienicol in [#6519](https://github.com/gfx-rs/wgpu/pull/6519).
- Add support for GLSL `usampler*` and `isampler*`. By @DavidPeicho in [#6513](https://github.com/gfx-rs/wgpu/pull/6513).

#### General

Expand Down Expand Up @@ -3481,4 +3482,4 @@ DeviceDescriptor {
- concept of the storage hub
- basic recording of passes and command buffers
- submission-based lifetime tracking and command buffer recycling
- automatic resource transitions
- automatic resource transitions
75 changes: 46 additions & 29 deletions naga/src/front/glsl/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,37 +462,54 @@ fn inject_standard_builtins(
module: &mut Module,
name: &str,
) {
match name {
"sampler1D" | "sampler1DArray" | "sampler2D" | "sampler2DArray" | "sampler2DMS"
| "sampler2DMSArray" | "sampler3D" | "samplerCube" | "samplerCubeArray" => {
declaration.overloads.push(module.add_builtin(
vec![
TypeInner::Image {
dim: match name {
"sampler1D" | "sampler1DArray" => Dim::D1,
"sampler2D" | "sampler2DArray" | "sampler2DMS" | "sampler2DMSArray" => {
Dim::D2
}
"sampler3D" => Dim::D3,
_ => Dim::Cube,
},
arrayed: matches!(
name,
"sampler1DArray"
| "sampler2DArray"
| "sampler2DMSArray"
| "samplerCubeArray"
),
class: ImageClass::Sampled {
kind: Sk::Float,
multi: matches!(name, "sampler2DMS" | "sampler2DMSArray"),
// Some samplers (sampler1D, etc...) can be float, int, or uint
let anykind_sampler = if name.starts_with("sampler") {
Some((name, Sk::Float))
} else if name.starts_with("usampler") {
Some((&name[1..], Sk::Uint))
} else if name.starts_with("isampler") {
Some((&name[1..], Sk::Sint))
} else {
None
};
if let Some((sampler, kind)) = anykind_sampler {
match sampler {
"sampler1D" | "sampler1DArray" | "sampler2D" | "sampler2DArray" | "sampler2DMS"
| "sampler2DMSArray" | "sampler3D" | "samplerCube" | "samplerCubeArray" => {
declaration.overloads.push(module.add_builtin(
vec![
TypeInner::Image {
dim: match sampler {
"sampler1D" | "sampler1DArray" => Dim::D1,
"sampler2D" | "sampler2DArray" | "sampler2DMS"
| "sampler2DMSArray" => Dim::D2,
"sampler3D" => Dim::D3,
_ => Dim::Cube,
},
arrayed: matches!(
sampler,
"sampler1DArray"
| "sampler2DArray"
| "sampler2DMSArray"
| "samplerCubeArray"
),
class: ImageClass::Sampled {
kind,
multi: matches!(sampler, "sampler2DMS" | "sampler2DMSArray"),
},
},
},
TypeInner::Sampler { comparison: false },
],
MacroCall::Sampler,
))
TypeInner::Sampler { comparison: false },
],
MacroCall::Sampler,
));
return;
}
_ => (),
}
}

match name {
// Shadow sampler can only be of kind `Sk::Float`
"sampler1DShadow"
| "sampler1DArrayShadow"
| "sampler2DShadow"
Expand Down
22 changes: 21 additions & 1 deletion naga/tests/in/glsl/samplers.frag
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ layout(set = 1, binding = 3) uniform texture2DArray tex2DArray;
layout(set = 1, binding = 4) uniform textureCube texCube;
layout(set = 1, binding = 5) uniform textureCubeArray texCubeArray;
layout(set = 1, binding = 6) uniform texture3D tex3D;
layout(set = 1, binding = 7) uniform sampler samp;

layout(set = 1, binding = 7) uniform utexture2D utex2D;
layout(set = 1, binding = 8) uniform itexture2D itex2D;

layout(set = 2, binding = 0) uniform sampler samp;

// WGSL doesn't have 1D depth samplers.
#define HAS_1D_DEPTH_TEXTURES 0
Expand Down Expand Up @@ -129,16 +133,26 @@ void testTex2D(in vec2 coord) {
vec4 c;
c = texture(sampler2D(tex2D, samp), coord);
c = texture(sampler2D(tex2D, samp), coord, 2.0);
/* Signed/Unsigned samplers not supported in the WGSL specification with `textureSample()` */

c = textureGrad(sampler2D(tex2D, samp), coord, vec2(4.0), vec2(4.0));
/* Signed/Unsigned samplers not supported in the WGSL specification with `textureSampleGrad()` */

c = textureGradOffset(sampler2D(tex2D, samp), coord, vec2(4.0), vec2(4.0), ivec2(5));
/* Signed/Unsigned samplers not supported in the WGSL specification with `textureSampleGrad()` */

c = textureLod(sampler2D(tex2D, samp), coord, 3.0);
/* Signed/Unsigned samplers not supported in the WGSL specification with `textureSampleLevel()` */

c = textureLodOffset(sampler2D(tex2D, samp), coord, 3.0, ivec2(5));
c = textureOffset(sampler2D(tex2D, samp), coord, ivec2(5));
c = textureOffset(sampler2D(tex2D, samp), coord, ivec2(5), 2.0);

c = textureProj(sampler2D(tex2D, samp), vec3(coord, 6.0));
c = textureProj(sampler2D(tex2D, samp), vec4(coord, 0.0, 6.0));
c = textureProj(sampler2D(tex2D, samp), vec3(coord, 6.0), 2.0);
c = textureProj(sampler2D(tex2D, samp), vec4(coord, 0.0, 6.0), 2.0);

c = textureProjGrad(sampler2D(tex2D, samp), vec3(coord, 6.0), vec2(4.0), vec2(4.0));
c = textureProjGrad(sampler2D(tex2D, samp), vec4(coord, 0.0, 6.0), vec2(4.0), vec2(4.0));
c = textureProjGradOffset(sampler2D(tex2D, samp), vec3(coord, 6.0), vec2(4.0), vec2(4.0), ivec2(5));
Expand All @@ -151,8 +165,14 @@ void testTex2D(in vec2 coord) {
c = textureProjOffset(sampler2D(tex2D, samp), vec4(coord, 0.0, 6.0), ivec2(5));
c = textureProjOffset(sampler2D(tex2D, samp), vec3(coord, 6.0), ivec2(5), 2.0);
c = textureProjOffset(sampler2D(tex2D, samp), vec4(coord, 0.0, 6.0), ivec2(5), 2.0);

c = texelFetch(sampler2D(tex2D, samp), ivec2(coord), 3);
c = vec4(texelFetch(usampler2D(utex2D, samp), ivec2(coord), 3));
c = vec4(texelFetch(isampler2D(itex2D, samp), ivec2(coord), 3));

c = texelFetchOffset(sampler2D(tex2D, samp), ivec2(coord), 3, ivec2(5));
c = vec4(texelFetchOffset(usampler2D(utex2D, samp), ivec2(coord), 3, ivec2(5)));
c = vec4(texelFetchOffset(isampler2D(itex2D, samp), ivec2(coord), 3, ivec2(5)));
}

void testTex2DShadow(vec2 coord) {
Expand Down
Loading