Skip to content

Commit

Permalink
Allow array_index to be unsigned
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Apr 4, 2023
1 parent da8e911 commit 1fe2c2f
Show file tree
Hide file tree
Showing 15 changed files with 843 additions and 823 deletions.
52 changes: 29 additions & 23 deletions src/back/spv/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,41 +314,47 @@ impl<'w> BlockContext<'w> {
};

// Convert the index to the coordinate component type, if necessary.
let array_index_i32_id = self.cached[array_index];
let reconciled_array_index_id = if component_kind == crate::ScalarKind::Sint {
array_index_i32_id
} else if component_kind == crate::ScalarKind::Uint {
let u32_id = self.get_type_id(LookupType::Local(LocalType::Value {
vector_size: None,
kind: crate::ScalarKind::Uint,
width: 4,
pointer_space: None,
}));

let reconciled_id = self.gen_id();
block.body.push(Instruction::unary(
spirv::Op::Bitcast,
u32_id,
reconciled_id,
array_index_i32_id,
let array_index_id = self.cached[array_index];
let ty = &self.fun_info[array_index].ty;
let inner_ty = ty.inner_with(&self.ir_module.types);
let array_index_kind = if let Ti::Scalar { kind, width: 4 } = *inner_ty {
debug_assert!(matches!(
kind,
crate::ScalarKind::Sint | crate::ScalarKind::Uint
));
reconciled_id
kind
} else {
let component_type_id = self.get_type_id(LookupType::Local(LocalType::Value {
unreachable!("we only allow i32 and u32");
};
let cast = match (component_kind, array_index_kind) {
(crate::ScalarKind::Sint, crate::ScalarKind::Sint)
| (crate::ScalarKind::Uint, crate::ScalarKind::Uint) => None,
(crate::ScalarKind::Sint, crate::ScalarKind::Uint)
| (crate::ScalarKind::Uint, crate::ScalarKind::Sint) => Some(spirv::Op::Bitcast),
(crate::ScalarKind::Float, crate::ScalarKind::Sint) => Some(spirv::Op::ConvertSToF),
(crate::ScalarKind::Float, crate::ScalarKind::Uint) => Some(spirv::Op::ConvertUToF),
(crate::ScalarKind::Bool, _) => unreachable!("we don't allow bool for component"),
(_, crate::ScalarKind::Bool | crate::ScalarKind::Float) => {
unreachable!("we don't allow bool or float for array index")
}
};
let reconciled_array_index_id = if let Some(cast) = cast {
let component_ty_id = self.get_type_id(LookupType::Local(LocalType::Value {
vector_size: None,
kind: component_kind,
width: 4,
pointer_space: None,
}));

let reconciled_id = self.gen_id();
block.body.push(Instruction::unary(
spirv::Op::ConvertUToF,
component_type_id,
cast,
component_ty_id,
reconciled_id,
array_index_i32_id,
array_index_id,
));
reconciled_id
} else {
array_index_id
};

// Find the SPIR-V type for the combined coordinates/index vector.
Expand Down
4 changes: 2 additions & 2 deletions src/valid/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl super::Validator {
if let Some(expr) = array_index {
match resolver[expr] {
Ti::Scalar {
kind: Sk::Sint,
kind: Sk::Sint | Sk::Uint,
width: _,
} => {}
_ => return Err(ExpressionError::InvalidImageArrayIndexType(expr)),
Expand Down Expand Up @@ -548,7 +548,7 @@ impl super::Validator {
if let Some(expr) = array_index {
match resolver[expr] {
Ti::Scalar {
kind: Sk::Sint,
kind: Sk::Sint | Sk::Uint,
width: _,
} => {}
_ => return Err(ExpressionError::InvalidImageArrayIndexType(expr)),
Expand Down
2 changes: 1 addition & 1 deletion src/valid/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ impl super::Validator {
if let Some(expr) = array_index {
match *context.resolve_type(expr, &self.valid_expression_set)? {
Ti::Scalar {
kind: crate::ScalarKind::Sint,
kind: crate::ScalarKind::Sint | crate::ScalarKind::Uint,
width: _,
} => {}
_ => {
Expand Down
1 change: 1 addition & 0 deletions tests/in/binding-arrays.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn main(fragment_in: FragmentIn) -> @location(0) vec4<f32> {
v4 += textureLoad(texture_array_unbounded[non_uniform_index], pix, 0);

u1 += textureNumLayers(texture_array_2darray[0]);
u1 += textureNumLayers(texture_array_2darray[0u]);
u1 += textureNumLayers(texture_array_2darray[uniform_index]);
u1 += textureNumLayers(texture_array_2darray[non_uniform_index]);

Expand Down
4 changes: 2 additions & 2 deletions tests/in/image.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ fn main(@builtin(local_invocation_id) local_id: vec3<u32>) {
let value1 = textureLoad(image_mipmapped_src, itc, i32(local_id.z));
let value2 = textureLoad(image_multisampled_src, itc, i32(local_id.z));
let value4 = textureLoad(image_storage_src, itc);
let value5 = textureLoad(image_array_src, itc, i32(local_id.z), i32(local_id.z) + 1);
let value5 = textureLoad(image_array_src, itc, local_id.z, i32(local_id.z) + 1);
let value6 = textureLoad(image_1d_src, i32(local_id.x), i32(local_id.z));
// loads with uvec2 coords.
let value1u = textureLoad(image_mipmapped_src, vec2<u32>(itc), i32(local_id.z));
let value2u = textureLoad(image_multisampled_src, vec2<u32>(itc), i32(local_id.z));
let value4u = textureLoad(image_storage_src, vec2<u32>(itc));
let value5u = textureLoad(image_array_src, vec2<u32>(itc), i32(local_id.z), i32(local_id.z) + 1);
let value5u = textureLoad(image_array_src, vec2<u32>(itc), local_id.z, i32(local_id.z) + 1);
let value6u = textureLoad(image_1d_src, u32(local_id.x), i32(local_id.z));
// store with ivec2 coords.
textureStore(image_dst, itc.x, value1 + value2 + value4 + value5 + value6);
Expand Down
4 changes: 2 additions & 2 deletions tests/out/glsl/image.main.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ void main() {
uvec4 value1_ = texelFetch(_group_0_binding_0_cs, itc, int(local_id.z));
uvec4 value2_ = texelFetch(_group_0_binding_3_cs, itc, int(local_id.z));
uvec4 value4_ = imageLoad(_group_0_binding_1_cs, itc);
uvec4 value5_ = texelFetch(_group_0_binding_5_cs, ivec3(itc, int(local_id.z)), (int(local_id.z) + 1));
uvec4 value5_ = texelFetch(_group_0_binding_5_cs, ivec3(itc, local_id.z), (int(local_id.z) + 1));
uvec4 value6_ = texelFetch(_group_0_binding_7_cs, ivec2(int(local_id.x), 0), int(local_id.z));
uvec4 value1u = texelFetch(_group_0_binding_0_cs, ivec2(uvec2(itc)), int(local_id.z));
uvec4 value2u = texelFetch(_group_0_binding_3_cs, ivec2(uvec2(itc)), int(local_id.z));
uvec4 value4u = imageLoad(_group_0_binding_1_cs, ivec2(uvec2(itc)));
uvec4 value5u = texelFetch(_group_0_binding_5_cs, ivec3(uvec2(itc), int(local_id.z)), (int(local_id.z) + 1));
uvec4 value5u = texelFetch(_group_0_binding_5_cs, ivec3(uvec2(itc), local_id.z), (int(local_id.z) + 1));
uvec4 value6u = texelFetch(_group_0_binding_7_cs, ivec2(uint(local_id.x), 0), int(local_id.z));
imageStore(_group_0_binding_2_cs, ivec2(itc.x, 0), ((((value1_ + value2_) + value4_) + value5_) + value6_));
imageStore(_group_0_binding_2_cs, ivec2(uint(itc.x), 0), ((((value1u + value2u) + value4u) + value5u) + value6u));
Expand Down
158 changes: 80 additions & 78 deletions tests/out/hlsl/binding-arrays.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -100,86 +100,88 @@ float4 main(FragmentInput_main fragmentinput_main) : SV_Target0
v4_ = (_expr101 + _expr100);
uint _expr107 = u1_;
u1_ = (_expr107 + NagaNumLayers2DArray(texture_array_2darray[0]));
uint _expr112 = u1_;
u1_ = (_expr112 + NagaNumLayers2DArray(texture_array_2darray[uniform_index]));
uint _expr117 = u1_;
u1_ = (_expr117 + NagaNumLayers2DArray(texture_array_2darray[NonUniformResourceIndex(non_uniform_index)]));
uint _expr113 = u1_;
u1_ = (_expr113 + NagaNumLayers2DArray(texture_array_2darray[0]));
uint _expr118 = u1_;
u1_ = (_expr118 + NagaNumLayers2DArray(texture_array_2darray[uniform_index]));
uint _expr123 = u1_;
u1_ = (_expr123 + NagaNumLevels2D(texture_array_bounded[0]));
uint _expr128 = u1_;
u1_ = (_expr128 + NagaNumLevels2D(texture_array_bounded[uniform_index]));
uint _expr133 = u1_;
u1_ = (_expr133 + NagaNumLevels2D(texture_array_bounded[NonUniformResourceIndex(non_uniform_index)]));
u1_ = (_expr123 + NagaNumLayers2DArray(texture_array_2darray[NonUniformResourceIndex(non_uniform_index)]));
uint _expr129 = u1_;
u1_ = (_expr129 + NagaNumLevels2D(texture_array_bounded[0]));
uint _expr134 = u1_;
u1_ = (_expr134 + NagaNumLevels2D(texture_array_bounded[uniform_index]));
uint _expr139 = u1_;
u1_ = (_expr139 + NagaMSNumSamples2D(texture_array_multisampled[0]));
uint _expr144 = u1_;
u1_ = (_expr144 + NagaMSNumSamples2D(texture_array_multisampled[uniform_index]));
uint _expr149 = u1_;
u1_ = (_expr149 + NagaMSNumSamples2D(texture_array_multisampled[NonUniformResourceIndex(non_uniform_index)]));
float4 _expr157 = texture_array_bounded[0].Sample(samp[0], uv);
float4 _expr158 = v4_;
v4_ = (_expr158 + _expr157);
float4 _expr164 = texture_array_bounded[uniform_index].Sample(samp[uniform_index], uv);
float4 _expr165 = v4_;
v4_ = (_expr165 + _expr164);
float4 _expr171 = texture_array_bounded[NonUniformResourceIndex(non_uniform_index)].Sample(samp[NonUniformResourceIndex(non_uniform_index)], uv);
float4 _expr172 = v4_;
v4_ = (_expr172 + _expr171);
float4 _expr181 = texture_array_bounded[0].SampleBias(samp[0], uv, 0.0);
float4 _expr182 = v4_;
v4_ = (_expr182 + _expr181);
float4 _expr189 = texture_array_bounded[uniform_index].SampleBias(samp[uniform_index], uv, 0.0);
float4 _expr190 = v4_;
v4_ = (_expr190 + _expr189);
float4 _expr197 = texture_array_bounded[NonUniformResourceIndex(non_uniform_index)].SampleBias(samp[NonUniformResourceIndex(non_uniform_index)], uv, 0.0);
float4 _expr198 = v4_;
v4_ = (_expr198 + _expr197);
float _expr207 = texture_array_depth[0].SampleCmp(samp_comp[0], uv, 0.0);
float _expr208 = v1_;
v1_ = (_expr208 + _expr207);
float _expr215 = texture_array_depth[uniform_index].SampleCmp(samp_comp[uniform_index], uv, 0.0);
float _expr216 = v1_;
v1_ = (_expr216 + _expr215);
float _expr223 = texture_array_depth[NonUniformResourceIndex(non_uniform_index)].SampleCmp(samp_comp[NonUniformResourceIndex(non_uniform_index)], uv, 0.0);
float _expr224 = v1_;
v1_ = (_expr224 + _expr223);
float _expr233 = texture_array_depth[0].SampleCmpLevelZero(samp_comp[0], uv, 0.0);
float _expr234 = v1_;
v1_ = (_expr234 + _expr233);
float _expr241 = texture_array_depth[uniform_index].SampleCmpLevelZero(samp_comp[uniform_index], uv, 0.0);
float _expr242 = v1_;
v1_ = (_expr242 + _expr241);
float _expr249 = texture_array_depth[NonUniformResourceIndex(non_uniform_index)].SampleCmpLevelZero(samp_comp[NonUniformResourceIndex(non_uniform_index)], uv, 0.0);
float _expr250 = v1_;
v1_ = (_expr250 + _expr249);
float4 _expr258 = texture_array_bounded[0].SampleGrad(samp[0], uv, uv, uv);
float4 _expr259 = v4_;
v4_ = (_expr259 + _expr258);
float4 _expr265 = texture_array_bounded[uniform_index].SampleGrad(samp[uniform_index], uv, uv, uv);
float4 _expr266 = v4_;
v4_ = (_expr266 + _expr265);
float4 _expr272 = texture_array_bounded[NonUniformResourceIndex(non_uniform_index)].SampleGrad(samp[NonUniformResourceIndex(non_uniform_index)], uv, uv, uv);
float4 _expr273 = v4_;
v4_ = (_expr273 + _expr272);
float4 _expr282 = texture_array_bounded[0].SampleLevel(samp[0], uv, 0.0);
float4 _expr283 = v4_;
v4_ = (_expr283 + _expr282);
float4 _expr290 = texture_array_bounded[uniform_index].SampleLevel(samp[uniform_index], uv, 0.0);
float4 _expr291 = v4_;
v4_ = (_expr291 + _expr290);
float4 _expr298 = texture_array_bounded[NonUniformResourceIndex(non_uniform_index)].SampleLevel(samp[NonUniformResourceIndex(non_uniform_index)], uv, 0.0);
float4 _expr299 = v4_;
v4_ = (_expr299 + _expr298);
float4 _expr304 = v4_;
texture_array_storage[0][pix] = _expr304;
float4 _expr307 = v4_;
texture_array_storage[uniform_index][pix] = _expr307;
u1_ = (_expr139 + NagaNumLevels2D(texture_array_bounded[NonUniformResourceIndex(non_uniform_index)]));
uint _expr145 = u1_;
u1_ = (_expr145 + NagaMSNumSamples2D(texture_array_multisampled[0]));
uint _expr150 = u1_;
u1_ = (_expr150 + NagaMSNumSamples2D(texture_array_multisampled[uniform_index]));
uint _expr155 = u1_;
u1_ = (_expr155 + NagaMSNumSamples2D(texture_array_multisampled[NonUniformResourceIndex(non_uniform_index)]));
float4 _expr163 = texture_array_bounded[0].Sample(samp[0], uv);
float4 _expr164 = v4_;
v4_ = (_expr164 + _expr163);
float4 _expr170 = texture_array_bounded[uniform_index].Sample(samp[uniform_index], uv);
float4 _expr171 = v4_;
v4_ = (_expr171 + _expr170);
float4 _expr177 = texture_array_bounded[NonUniformResourceIndex(non_uniform_index)].Sample(samp[NonUniformResourceIndex(non_uniform_index)], uv);
float4 _expr178 = v4_;
v4_ = (_expr178 + _expr177);
float4 _expr187 = texture_array_bounded[0].SampleBias(samp[0], uv, 0.0);
float4 _expr188 = v4_;
v4_ = (_expr188 + _expr187);
float4 _expr195 = texture_array_bounded[uniform_index].SampleBias(samp[uniform_index], uv, 0.0);
float4 _expr196 = v4_;
v4_ = (_expr196 + _expr195);
float4 _expr203 = texture_array_bounded[NonUniformResourceIndex(non_uniform_index)].SampleBias(samp[NonUniformResourceIndex(non_uniform_index)], uv, 0.0);
float4 _expr204 = v4_;
v4_ = (_expr204 + _expr203);
float _expr213 = texture_array_depth[0].SampleCmp(samp_comp[0], uv, 0.0);
float _expr214 = v1_;
v1_ = (_expr214 + _expr213);
float _expr221 = texture_array_depth[uniform_index].SampleCmp(samp_comp[uniform_index], uv, 0.0);
float _expr222 = v1_;
v1_ = (_expr222 + _expr221);
float _expr229 = texture_array_depth[NonUniformResourceIndex(non_uniform_index)].SampleCmp(samp_comp[NonUniformResourceIndex(non_uniform_index)], uv, 0.0);
float _expr230 = v1_;
v1_ = (_expr230 + _expr229);
float _expr239 = texture_array_depth[0].SampleCmpLevelZero(samp_comp[0], uv, 0.0);
float _expr240 = v1_;
v1_ = (_expr240 + _expr239);
float _expr247 = texture_array_depth[uniform_index].SampleCmpLevelZero(samp_comp[uniform_index], uv, 0.0);
float _expr248 = v1_;
v1_ = (_expr248 + _expr247);
float _expr255 = texture_array_depth[NonUniformResourceIndex(non_uniform_index)].SampleCmpLevelZero(samp_comp[NonUniformResourceIndex(non_uniform_index)], uv, 0.0);
float _expr256 = v1_;
v1_ = (_expr256 + _expr255);
float4 _expr264 = texture_array_bounded[0].SampleGrad(samp[0], uv, uv, uv);
float4 _expr265 = v4_;
v4_ = (_expr265 + _expr264);
float4 _expr271 = texture_array_bounded[uniform_index].SampleGrad(samp[uniform_index], uv, uv, uv);
float4 _expr272 = v4_;
v4_ = (_expr272 + _expr271);
float4 _expr278 = texture_array_bounded[NonUniformResourceIndex(non_uniform_index)].SampleGrad(samp[NonUniformResourceIndex(non_uniform_index)], uv, uv, uv);
float4 _expr279 = v4_;
v4_ = (_expr279 + _expr278);
float4 _expr288 = texture_array_bounded[0].SampleLevel(samp[0], uv, 0.0);
float4 _expr289 = v4_;
v4_ = (_expr289 + _expr288);
float4 _expr296 = texture_array_bounded[uniform_index].SampleLevel(samp[uniform_index], uv, 0.0);
float4 _expr297 = v4_;
v4_ = (_expr297 + _expr296);
float4 _expr304 = texture_array_bounded[NonUniformResourceIndex(non_uniform_index)].SampleLevel(samp[NonUniformResourceIndex(non_uniform_index)], uv, 0.0);
float4 _expr305 = v4_;
v4_ = (_expr305 + _expr304);
float4 _expr310 = v4_;
texture_array_storage[NonUniformResourceIndex(non_uniform_index)][pix] = _expr310;
uint2 _expr311 = u2_;
uint _expr312 = u1_;
float2 v2_ = float2((_expr311 + (_expr312).xx));
texture_array_storage[0][pix] = _expr310;
float4 _expr313 = v4_;
texture_array_storage[uniform_index][pix] = _expr313;
float4 _expr316 = v4_;
float _expr323 = v1_;
return ((_expr316 + float4(v2_.x, v2_.y, v2_.x, v2_.y)) + (_expr323).xxxx);
texture_array_storage[NonUniformResourceIndex(non_uniform_index)][pix] = _expr316;
uint2 _expr317 = u2_;
uint _expr318 = u1_;
float2 v2_ = float2((_expr317 + (_expr318).xx));
float4 _expr322 = v4_;
float _expr329 = v1_;
return ((_expr322 + float4(v2_.x, v2_.y, v2_.x, v2_.y)) + (_expr329).xxxx);
}
4 changes: 2 additions & 2 deletions tests/out/hlsl/image.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ void main(uint3 local_id : SV_GroupThreadID)
uint4 value1_ = image_mipmapped_src.Load(int3(itc, int(local_id.z)));
uint4 value2_ = image_multisampled_src.Load(itc, int(local_id.z));
uint4 value4_ = image_storage_src.Load(itc);
uint4 value5_ = image_array_src.Load(int4(itc, int(local_id.z), (int(local_id.z) + 1)));
uint4 value5_ = image_array_src.Load(int4(itc, local_id.z, (int(local_id.z) + 1)));
uint4 value6_ = image_1d_src.Load(int2(int(local_id.x), int(local_id.z)));
uint4 value1u = image_mipmapped_src.Load(int3(uint2(itc), int(local_id.z)));
uint4 value2u = image_multisampled_src.Load(uint2(itc), int(local_id.z));
uint4 value4u = image_storage_src.Load(uint2(itc));
uint4 value5u = image_array_src.Load(int4(uint2(itc), int(local_id.z), (int(local_id.z) + 1)));
uint4 value5u = image_array_src.Load(int4(uint2(itc), local_id.z, (int(local_id.z) + 1)));
uint4 value6u = image_1d_src.Load(int2(uint(local_id.x), int(local_id.z)));
image_dst[itc.x] = ((((value1_ + value2_) + value4_) + value5_) + value6_);
image_dst[uint(itc.x)] = ((((value1u + value2u) + value4u) + value5u) + value6u);
Expand Down
Loading

0 comments on commit 1fe2c2f

Please sign in to comment.