diff --git a/src/back/hlsl/storage.rs b/src/back/hlsl/storage.rs index 3f296fe2ad..a48507a669 100644 --- a/src/back/hlsl/storage.rs +++ b/src/back/hlsl/storage.rs @@ -365,10 +365,17 @@ impl super::Writer<'_, W> { ) -> Result, Error> { self.temp_access_chain.clear(); loop { - let stride = func_ctx.info[cur_expr] - .ty - .inner_with(&module.types) - .span(&module.constants); + // determine the size of the pontee + let stride = match *func_ctx.info[cur_expr].ty.inner_with(&module.types) { + crate::TypeInner::Pointer { base, class: _ } => { + module.types[base].inner.span(&module.constants) + } + crate::TypeInner::ValuePointer { size, width, .. } => { + size.map_or(1, |s| s as u32) * width as u32 + } + _ => 0, + }; + let (next_expr, sub) = match func_ctx.expressions[cur_expr] { crate::Expression::GlobalVariable(handle) => return Ok(handle), crate::Expression::Access { base, index } => ( diff --git a/src/back/hlsl/writer.rs b/src/back/hlsl/writer.rs index b331d8c278..bbecb3edc0 100644 --- a/src/back/hlsl/writer.rs +++ b/src/back/hlsl/writer.rs @@ -1008,23 +1008,42 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { ref body, ref continuing, } => { - write!(self.out, "{}", INDENT.repeat(indent))?; - writeln!(self.out, "while(true) {{")?; - - for sta in body.iter().chain(continuing.iter()) { - self.write_stmt(module, sta, func_ctx, indent + 1)?; + if !continuing.is_empty() { + let gate_name = self.namer.call("loop_init"); + writeln!( + self.out, + "{}bool {} = true;", + INDENT.repeat(indent), + gate_name + )?; + writeln!(self.out, "{}while(true) {{", INDENT.repeat(indent))?; + writeln!( + self.out, + "{}if (!{}) {{", + INDENT.repeat(indent + 1), + gate_name + )?; + for sta in continuing.iter() { + self.write_stmt(module, sta, func_ctx, indent + 1)?; + } + writeln!(self.out, "{}}}", INDENT.repeat(indent + 1))?; + writeln!( + self.out, + "{}{} = false;", + INDENT.repeat(indent + 1), + gate_name + )?; + } else { + writeln!(self.out, "{}while(true) {{", INDENT.repeat(indent))?; } + for sta in body.iter() { + self.write_stmt(module, sta, func_ctx, indent + 1)?; + } writeln!(self.out, "{}}}", INDENT.repeat(indent))? } - Statement::Break => { - write!(self.out, "{}", INDENT.repeat(indent))?; - writeln!(self.out, "break;")? - } - Statement::Continue => { - write!(self.out, "{}", INDENT.repeat(indent))?; - writeln!(self.out, "continue;")? - } + Statement::Break => writeln!(self.out, "{}break;", INDENT.repeat(indent))?, + Statement::Continue => writeln!(self.out, "{}continue;", INDENT.repeat(indent))?, Statement::Barrier(barrier) => { if barrier.contains(crate::Barrier::STORAGE) { writeln!( diff --git a/tests/out/hlsl/access.hlsl b/tests/out/hlsl/access.hlsl index 14201429d4..5ff07ea5fa 100644 --- a/tests/out/hlsl/access.hlsl +++ b/tests/out/hlsl/access.hlsl @@ -20,11 +20,11 @@ float4 foo(VertexInput_foo vertexinput_foo) : SV_Position float baz = foo1; foo1 = 1.0; float4x4 matrix1 = transpose(float4x4(asfloat(bar.Load4(0+0)), asfloat(bar.Load4(0+16)), asfloat(bar.Load4(0+32)), asfloat(bar.Load4(0+48)))); - uint2 arr[2] = {asuint(bar.Load2(4+0)), asuint(bar.Load2(4+8))}; - float4 _expr13 = asfloat(bar.Load4(12+0)); + uint2 arr[2] = {asuint(bar.Load2(16+0)), asuint(bar.Load2(16+8))}; + float4 _expr13 = asfloat(bar.Load4(48+0)); float b = _expr13.x; int a = asint(bar.Load((((NagaBufferLengthRW(bar) - 80) / 4) - 2u)*4+8)); - bar.Store(8+4+0, asuint(1.0)); + bar.Store(8+16+0, asuint(1.0)); { float4x4 _value2 = transpose(float4x4(float4(0.0.xxxx), float4(1.0.xxxx), float4(2.0.xxxx), float4(3.0.xxxx))); bar.Store4(0+0, asuint(_value2[0])); @@ -34,8 +34,8 @@ float4 foo(VertexInput_foo vertexinput_foo) : SV_Position } { uint2 _value2[2] = { uint2(0u.xx), uint2(1u.xx) }; - bar.Store2(4+0, asuint(_value2[0])); - bar.Store2(4+8, asuint(_value2[1])); + bar.Store2(16+0, asuint(_value2[0])); + bar.Store2(16+8, asuint(_value2[1])); } { int _result[5]={ a, int(b), 3, 4, 5 }; diff --git a/tests/out/hlsl/boids.hlsl b/tests/out/hlsl/boids.hlsl index c3e742ca75..03fd86d059 100644 --- a/tests/out/hlsl/boids.hlsl +++ b/tests/out/hlsl/boids.hlsl @@ -41,14 +41,20 @@ void main(ComputeInput_main computeinput_main) if ((index >= NUM_PARTICLES)) { return; } - float2 _expr10 = asfloat(particlesSrc.Load2(0+index*4+0)); + float2 _expr10 = asfloat(particlesSrc.Load2(0+index*16+0)); vPos = _expr10; - float2 _expr15 = asfloat(particlesSrc.Load2(4+index*4+0)); + float2 _expr15 = asfloat(particlesSrc.Load2(8+index*16+0)); vVel = _expr15; cMass = float2(0.0, 0.0); cVel = float2(0.0, 0.0); colVel = float2(0.0, 0.0); + bool loop_init = true; while(true) { + if (!loop_init) { + uint _expr86 = i; + i = (_expr86 + 1u); + } + loop_init = false; uint _expr37 = i; if ((_expr37 >= NUM_PARTICLES)) { break; @@ -58,10 +64,10 @@ void main(ComputeInput_main computeinput_main) continue; } uint _expr42 = i; - float2 _expr45 = asfloat(particlesSrc.Load2(0+_expr42*4+0)); + float2 _expr45 = asfloat(particlesSrc.Load2(0+_expr42*16+0)); pos = _expr45; uint _expr47 = i; - float2 _expr50 = asfloat(particlesSrc.Load2(4+_expr47*4+0)); + float2 _expr50 = asfloat(particlesSrc.Load2(8+_expr47*16+0)); vel = _expr50; float2 _expr51 = pos; float2 _expr52 = vPos; @@ -92,8 +98,6 @@ void main(ComputeInput_main computeinput_main) int _expr83 = cVelCount; cVelCount = (_expr83 + 1); } - uint _expr86 = i; - i = (_expr86 + 1u); } int _expr89 = cMassCount; if ((_expr89 > 0)) { @@ -140,8 +144,8 @@ void main(ComputeInput_main computeinput_main) vPos.y = -1.0; } float2 _expr164 = vPos; - particlesDst.Store2(0+index*4+0, asuint(_expr164)); + particlesDst.Store2(0+index*16+0, asuint(_expr164)); float2 _expr168 = vVel; - particlesDst.Store2(4+index*4+0, asuint(_expr168)); + particlesDst.Store2(8+index*16+0, asuint(_expr168)); return; } diff --git a/tests/out/hlsl/shadow.hlsl b/tests/out/hlsl/shadow.hlsl index 4982029a1d..32d23dd931 100644 --- a/tests/out/hlsl/shadow.hlsl +++ b/tests/out/hlsl/shadow.hlsl @@ -38,22 +38,26 @@ float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0 uint i = 0u; float3 normal = normalize(fragmentinput_fs_main.raw_normal1); + bool loop_init = true; while(true) { + if (!loop_init) { + uint _expr40 = i; + i = (_expr40 + 1u); + } + loop_init = false; uint _expr12 = i; uint4 _expr14 = u_globals.num_lights; if ((_expr12 >= min(_expr14.x, c_max_lights))) { break; } uint _expr19 = i; - Light light = {transpose(float4x4(asfloat(s_lights.Load4(_expr19*4+0+0+0)), asfloat(s_lights.Load4(_expr19*4+0+0+16)), asfloat(s_lights.Load4(_expr19*4+0+0+32)), asfloat(s_lights.Load4(_expr19*4+0+0+48)))), asfloat(s_lights.Load4(_expr19*4+0+64)), asfloat(s_lights.Load4(_expr19*4+0+80))}; + Light light = {transpose(float4x4(asfloat(s_lights.Load4(_expr19*96+0+0+0)), asfloat(s_lights.Load4(_expr19*96+0+0+16)), asfloat(s_lights.Load4(_expr19*96+0+0+32)), asfloat(s_lights.Load4(_expr19*96+0+0+48)))), asfloat(s_lights.Load4(_expr19*96+0+64)), asfloat(s_lights.Load4(_expr19*96+0+80))}; uint _expr22 = i; const float _e25 = fetch_shadow(_expr22, mul(light.proj, fragmentinput_fs_main.position1)); float3 light_dir = normalize((light.pos.xyz - fragmentinput_fs_main.position1.xyz)); float diffuse = max(0.0, dot(normal, light_dir)); float3 _expr34 = color; color = (_expr34 + ((_e25 * diffuse) * light.color.xyz)); - uint _expr40 = i; - i = (_expr40 + 1u); } float3 _expr43 = color; return float4(_expr43, 1.0);