Skip to content

Commit

Permalink
naga: Add support for textureQueryLevels to GLSL parser (#6415)
Browse files Browse the repository at this point in the history
  • Loading branch information
magcius authored Oct 18, 2024
1 parent a8214b6 commit 1b2ef86
Show file tree
Hide file tree
Showing 4 changed files with 566 additions and 480 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ By @bradwerth [#6216](https://github.com/gfx-rs/wgpu/pull/6216).
- Fix handling of phony statements, so they are actually emitted. By @sagudev in [#6328](https://github.com/gfx-rs/wgpu/pull/6328).
- Added `gl_DrawID` to glsl and `DrawIndex` to spv. By @ChosenName in [#6325](https://github.com/gfx-rs/wgpu/pull/6325).
- Matrices can now be indexed by value (#4337), and indexing arrays by value no longer causes excessive spilling (#6358). By @jimblandy in [#6390](https://github.com/gfx-rs/wgpu/pull/6390).
- Add support for `textureQueryLevels` to the GLSL parser. By @magcius in [#6325](https://github.com/gfx-rs/wgpu/pull/6415).

#### General

Expand Down
39 changes: 39 additions & 0 deletions naga/src/front/glsl/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,26 @@ pub fn inject_builtin(
f,
)
}
"textureQueryLevels" => {
let f = |kind, dim, arrayed, multi, shadow| {
let class = match shadow {
true => ImageClass::Depth { multi },
false => ImageClass::Sampled { kind, multi },
};

let image = TypeInner::Image {
dim,
arrayed,
class,
};

declaration
.overloads
.push(module.add_builtin(vec![image], MacroCall::TextureQueryLevels))
};

texture_args_generator(TextureArgsOptions::SHADOW | variations.into(), f)
}
"texelFetch" | "texelFetchOffset" => {
let offset = "texelFetchOffset" == name;
let f = |kind, dim, arrayed, multi, _shadow| {
Expand Down Expand Up @@ -1515,6 +1535,7 @@ pub enum MacroCall {
TextureSize {
arrayed: bool,
},
TextureQueryLevels,
ImageLoad {
multi: bool,
},
Expand Down Expand Up @@ -1747,6 +1768,24 @@ impl MacroCall {
Span::default(),
)?
}
MacroCall::TextureQueryLevels => {
let expr = ctx.add_expression(
Expression::ImageQuery {
image: args[0],
query: ImageQuery::NumLevels,
},
Span::default(),
)?;

ctx.add_expression(
Expression::As {
expr,
kind: Sk::Sint,
convert: Some(4),
},
Span::default(),
)?
}
MacroCall::ImageLoad { multi } => {
let comps = frontend.coordinate_components(ctx, args[0], args[1], None, meta)?;
let (sample, level) = match (multi, args.get(2)) {
Expand Down
13 changes: 13 additions & 0 deletions naga/tests/in/glsl/samplers.frag
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ layout(binding = 19) uniform texture2DMSArray tex2DMSArray;

void testTex1D(in float coord) {
int size1D = textureSize(sampler1D(tex1D, samp), 0);
int levels = textureQueryLevels(sampler1D(tex1D, samp));
vec4 c;
c = texture(sampler1D(tex1D, samp), coord);
c = texture(sampler1D(tex1D, samp), coord, 2.0);
Expand Down Expand Up @@ -70,6 +71,7 @@ void testTex1D(in float coord) {
#if HAS_1D_DEPTH_TEXTURES
void testTex1DShadow(float coord) {
int size1DShadow = textureSize(sampler1DShadow(tex1DShadow, sampShadow), 0);
int levels = textureQueryLevels(sampler1DShadow(tex1DShadow, sampShadow));
float d;
d = texture(sampler1DShadow(tex1DShadow, sampShadow), vec3(coord, 1.0, 1.0));
// d = texture(sampler1DShadow(tex1DShadow, sampShadow), vec3(coord, 1.0, 1.0), 2.0);
Expand All @@ -92,6 +94,7 @@ void testTex1DShadow(float coord) {

void testTex1DArray(in vec2 coord) {
ivec2 size1DArray = textureSize(sampler1DArray(tex1DArray, samp), 0);
int levels = textureQueryLevels(sampler1DArray(tex1DArray, samp));
vec4 c;
c = texture(sampler1DArray(tex1DArray, samp), coord);
c = texture(sampler1DArray(tex1DArray, samp), coord, 2.0);
Expand All @@ -108,6 +111,7 @@ void testTex1DArray(in vec2 coord) {
#if HAS_1D_DEPTH_TEXTURES
void testTex1DArrayShadow(in vec2 coord) {
ivec2 size1DArrayShadow = textureSize(sampler1DArrayShadow(tex1DArrayShadow, sampShadow), 0);
int levels = textureQueryLevels(sampler1DArrayShadow(tex1DArrayShadow, sampShadow));
float d;
d = texture(sampler1DArrayShadow(tex1DArrayShadow, sampShadow), vec3(coord, 1.0));
d = textureGrad(sampler1DArrayShadow(tex1DArrayShadow, sampShadow), vec3(coord, 1.0), 4.0, 4.0);
Expand All @@ -121,6 +125,7 @@ void testTex1DArrayShadow(in vec2 coord) {

void testTex2D(in vec2 coord) {
ivec2 size2D = textureSize(sampler2D(tex2D, samp), 0);
int levels = textureQueryLevels(sampler2D(tex2D, samp));
vec4 c;
c = texture(sampler2D(tex2D, samp), coord);
c = texture(sampler2D(tex2D, samp), coord, 2.0);
Expand Down Expand Up @@ -152,6 +157,7 @@ void testTex2D(in vec2 coord) {

void testTex2DShadow(vec2 coord) {
ivec2 size2DShadow = textureSize(sampler2DShadow(tex2DShadow, sampShadow), 0);
int levels = textureQueryLevels(sampler2DShadow(tex2DShadow, sampShadow));
float d;
d = texture(sampler2DShadow(tex2DShadow, sampShadow), vec3(coord, 1.0));
// d = texture(sampler2DShadow(tex2DShadow, sampShadow), vec3(coord, 1.0), 2.0);
Expand All @@ -173,6 +179,7 @@ void testTex2DShadow(vec2 coord) {

void testTex2DArray(in vec3 coord) {
ivec3 size2DArray = textureSize(sampler2DArray(tex2DArray, samp), 0);
int levels = textureQueryLevels(sampler2DArray(tex2DArray, samp));
vec4 c;
c = texture(sampler2DArray(tex2DArray, samp), coord);
c = texture(sampler2DArray(tex2DArray, samp), coord, 2.0);
Expand All @@ -188,6 +195,7 @@ void testTex2DArray(in vec3 coord) {

void testTex2DArrayShadow(in vec3 coord) {
ivec3 size2DArrayShadow = textureSize(sampler2DArrayShadow(tex2DArrayShadow, sampShadow), 0);
int levels = textureQueryLevels(sampler2DArrayShadow(tex2DArrayShadow, sampShadow));
float d;
d = texture(sampler2DArrayShadow(tex2DArrayShadow, sampShadow), vec4(coord, 1.0));
d = textureGrad(sampler2DArrayShadow(tex2DArrayShadow, sampShadow), vec4(coord, 1.0), vec2(4.0), vec2(4.0));
Expand All @@ -197,6 +205,7 @@ void testTex2DArrayShadow(in vec3 coord) {

void testTexCube(in vec3 coord) {
ivec2 sizeCube = textureSize(samplerCube(texCube, samp), 0);
int levels = textureQueryLevels(samplerCube(texCube, samp));
vec4 c;
c = texture(samplerCube(texCube, samp), coord);
c = texture(samplerCube(texCube, samp), coord, 2.0);
Expand All @@ -206,13 +215,15 @@ void testTexCube(in vec3 coord) {

void testTexCubeShadow(in vec3 coord) {
ivec2 sizeCubeShadow = textureSize(samplerCubeShadow(texCubeShadow, sampShadow), 0);
int levels = textureQueryLevels(samplerCubeShadow(texCubeShadow, sampShadow));
float d;
d = texture(samplerCubeShadow(texCubeShadow, sampShadow), vec4(coord, 1.0));
d = textureGrad(samplerCubeShadow(texCubeShadow, sampShadow), vec4(coord, 1.0), vec3(4.0), vec3(4.0));
}

void testTexCubeArray(in vec4 coord) {
ivec3 sizeCubeArray = textureSize(samplerCubeArray(texCubeArray, samp), 0);
int levels = textureQueryLevels(samplerCubeArray(texCubeArray, samp));
vec4 c;
c = texture(samplerCubeArray(texCubeArray, samp), coord);
c = texture(samplerCubeArray(texCubeArray, samp), coord, 2.0);
Expand All @@ -222,13 +233,15 @@ void testTexCubeArray(in vec4 coord) {

void testTexCubeArrayShadow(in vec4 coord) {
ivec3 sizeCubeArrayShadow = textureSize(samplerCubeArrayShadow(texCubeArrayShadow, sampShadow), 0);
int levels = textureQueryLevels(samplerCubeArrayShadow(texCubeArrayShadow, sampShadow));
float d;
d = texture(samplerCubeArrayShadow(texCubeArrayShadow, sampShadow), coord, 1.0);
// The rest of the variants aren't defined by GLSL.
}

void testTex3D(in vec3 coord) {
ivec3 size3D = textureSize(sampler3D(tex3D, samp), 0);
int levels = textureQueryLevels(sampler3D(tex3D, samp));
vec4 c;
c = texture(sampler3D(tex3D, samp), coord);
c = texture(sampler3D(tex3D, samp), coord, 2.0);
Expand Down
Loading

0 comments on commit 1b2ef86

Please sign in to comment.