Skip to content

Code fails to unroll when compiled for ps_5_0 #56

@billgan1024

Description

@billgan1024

Type: Bug


vec4 operator*(mat4 m, vec4 v)
{
   return mul(m, v);
}
mat4 operator*(mat4 a, mat4 b)
{
   return mul(a, b);
}
vec3 operator*(mat3 m, vec3 v)
{
   return mul(m, v);
}
mat3 operator*(mat3 a, mat3 b)
{
   return mul(a, b);
}
void psShadow(vec4 svp: SV_Position, vec3 normal: normal, vec2 uv: uv, vec3 pos: pos,
              Texture2D diffuse, Texture2D<float> shadow[4],
              SamplerState sampler, SamplerComparisonState cmp,
              ConstantBuffer<vec3> light, 
              ConstantBuffer<mat4[4]> cascades,
              out vec4 target: SV_Target)
{
   normal = normalize(normal);
   float threshold = 0.25;
   float lambert = (1 - threshold) * ((dot(normal, light) + 1) / 2) + threshold;

   float inLight = 1;
   if (dot(normal, light) > 0)
   {
      for (int i = 0; i < 4; i++)
      {
         vec3 ndc = (cascades[i] * vec4(pos, 1)).xyz;
         if (abs(ndc.x) < 1 && abs(ndc.y) < 1 && ndc.z > 0 && ndc.z < 1)
         {
            inLight = shadow[i].SampleCmp(cmp, vec2((ndc.x + 1) / 2, 1 - (ndc.y + 1) / 2), ndc.z);
            break;
         }
      }
   }
   target = lambert * lerp(0.65, 1, inLight) * diffuse.Sample(sampler, uv);
}

compile with
slangc -matrix-layout-row-major -profile ps_5_0 shaders.hlsl -entry psShadow -o out/psShadow.dxbc gives
fxc 47.0: shaders.hlsl(177): error X3511: forced to unroll loop, but unrolling failed.

Here is the generated HLSL:

#pragma pack_matrix(row_major)
#ifdef SLANG_HLSL_ENABLE_NVAPI
#include "nvHLSLExtns.h"
#endif

#ifndef __DXC_VERSION_MAJOR
// warning X3557: loop doesn't seem to do anything, forcing loop to unroll
#pragma warning(disable: 3557)
#endif

Texture2D<float4> entryPointParams_diffuse_0 : register(t0);

Texture2D<float> entryPointParams_shadow_0[int(4)] : register(t1);

SamplerState entryPointParams_sampler_0 : register(s0);

SamplerComparisonState entryPointParams_cmp_0 : register(s1);

cbuffer entryPointParams_light_0 : register(b0)
{
   float3 entryPointParams_light_0;
}

cbuffer entryPointParams_cascades_0 : register(b1)
{
   float4x4 entryPointParams_cascades_0[int(4)];
}

float4 x2A_0(float4x4 m_0, float4 v_0)
{
   return mul(m_0, v_0);
}

void psShadow(float4 svp_0: SV_Position, float3 normal_0: normal, float2 uv_0: uv, float3 pos_0: pos, out float4 target_0: SV_Target)
{

   float2 _S1 = uv_0;

   float3 _S2 = pos_0;

   float _S3 = dot(normalize(normal_0), entryPointParams_light_0);

   float lambert_0 = 0.75f * ((_S3 + 1.0f) / 2.0f) + 0.25f;

   float inLight_0;

   if (_S3 > 0.0f)
   {

      int i_0 = int(0);

      for (;;)
      {

         if (i_0 < int(4))
         {
         }
         else
         {

            inLight_0 = 1.0f;

            break;
         }
         float3 ndc_0 = x2A_0(entryPointParams_cascades_0[i_0], float4(_S2, 1.0f)).xyz;
         float _S4 = ndc_0.x;

         bool _S5;

         if ((abs(_S4)) < 1.0f)
         {

            _S5 = (abs(ndc_0.y)) < 1.0f;
         }
         else
         {

            _S5 = false;
         }

         bool _S6;

         if (_S5)
         {

            _S6 = (ndc_0.z) > 0.0f;
         }
         else
         {

            _S6 = false;
         }

         bool _S7;

         if (_S6)
         {

            _S7 = (ndc_0.z) < 1.0f;
         }
         else
         {

            _S7 = false;
         }

         if (_S7)
         {

            inLight_0 = entryPointParams_shadow_0[i_0].SampleCmp(entryPointParams_cmp_0, float2((_S4 + 1.0f) / 2.0f, 1.0f - (ndc_0.y + 1.0f) / 2.0f), ndc_0.z);

            break;
         }

         i_0 = i_0 + int(1);
      }
   }
   else
   {

      inLight_0 = 1.0f;
   }

   target_0 = lambert_0 * lerp(0.64999997615814209f, 1.0f, inLight_0) * entryPointParams_diffuse_0.Sample(entryPointParams_sampler_0, _S1);
   return;
}

the for loop should be simplified to make fxc work properly.

Extension version: 2.0.3
VS Code version: Code 1.107.0 (618725e67565b290ba4da6fe2d29f8fa1d4e3622, 2025-12-10T07:43:47.883Z)
OS version: Windows_NT x64 10.0.19045
Modes: Unsupported
Remote OS version: Linux x64 6.6.87.2-microsoft-standard-WSL2
Remote OS version: Linux x64 6.6.87.2-microsoft-standard-WSL2

System Info
Item Value
CPUs 12th Gen Intel(R) Core(TM) i9-12900K (24 x 3187)
GPU Status 2d_canvas: enabled
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
trees_in_viz: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) undefined
Memory (System) 63.75GB (48.71GB free)
Process Argv --crash-reporter-id 0a3edcfc-5c4b-4c1f-a092-6afdf9563f4b
Screen Reader no
VM 0%
Item Value
Remote WSL: Ubuntu
OS Linux x64 6.6.87.2-microsoft-standard-WSL2
CPUs 12th Gen Intel(R) Core(TM) i9-12900K (24 x 0)
Memory (System) 31.22GB (28.75GB free)
VM 0%
Item Value
Remote WSL: Ubuntu
OS Linux x64 6.6.87.2-microsoft-standard-WSL2
CPUs 12th Gen Intel(R) Core(TM) i9-12900K (24 x 0)
Memory (System) 31.22GB (28.75GB free)
VM 0%
A/B Experiments
vsliv368:30146709
binariesv615:30325510
nativeloc1:31344060
dwcopilot:31170013
dwoutputs:31242946
copilot_t_ci:31333650
e5gg6876:31282496
pythonrdcb7:31342333
6518g693:31436602
aj953862:31281341
nes-set-on:31351930
6abeh943:31336334
envsactivate1:31353494
cloudbuttont:31379625
todos-1:31405332
42190218_ostrepl:31403339
trigger-command-fix:31379601
use-responses-api:31390855
6j29d282:31407606
2a0ce269:31428710
ec5jj548:31422691
terminalsuggestenabled:31431119
cmp-ext-treat:31426748
cp_cls_c_966_ss:31435507
copilot6169-t3000:31431383
c0683394:31419495
478ah919:31426797
ge8j1254_inline_auto_hint_haiku:31427726
a5gib710:31434435
38bie571_auto:31426784
request_with_suggest:31435827
rename_enabled:31436409
7a04d226_do_not_restore_last_panel_session:31434268
anthropic_thinking_t:31432745
4g4b7572_control_gpt_5_mini:31434516
406hc587_ask_agent:31428393
cp_cls_c_1081:31433293
copilot-nes-callisto-trt:31434412

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions