// Tile lighting - populates LightIndicesPerTile & LightOutputPerTile

//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//   using 3Dmigoto v1.2.31 on Wed Mar 02 08:07:15 2016
//
//
// Buffer Definitions: 
//
// cbuffer CViewportShaderParameterProvider
// {
//
//   float4x4 InvProjectionMatrix;      // Offset:    0 Size:    64
//   float4x4 InvViewMatrix;            // Offset:   64 Size:    64 [unused]
//   float4x4 ProjectionMatrix;         // Offset:  128 Size:    64
//   float4x4 ViewMatrix;               // Offset:  192 Size:    64
//   float4x4 ViewProjectionMatrix;     // Offset:  256 Size:    64 [unused]
//   float4x4 ViewRotProjectionMatrix;  // Offset:  320 Size:    64 [unused]
//   float4x4 ViewRotProjectionMatrix_Previous;// Offset:  384 Size:    64 [unused]
//   float4 AmbientSHR;                 // Offset:  448 Size:    16 [unused]
//   float4 AmbientSHG;                 // Offset:  464 Size:    16 [unused]
//   float4 AmbientSHB;                 // Offset:  480 Size:    16 [unused]
//   float4 CameraDistances;            // Offset:  496 Size:    16
//   float4 CameraNearPlaneSize;        // Offset:  512 Size:    16 [unused]
//   float4 DepthScale;                 // Offset:  528 Size:    16 [unused]
//   float4 DepthTextureScaleOffset;    // Offset:  544 Size:    16 [unused]
//   float4 FogParams;                  // Offset:  560 Size:    16 [unused]
//   float4 FogSHB;                     // Offset:  576 Size:    16 [unused]
//   float4 FogSHG;                     // Offset:  592 Size:    16 [unused]
//   float4 FogSHR;                     // Offset:  608 Size:    16 [unused]
//   float4 HeightFogParams;            // Offset:  624 Size:    16 [unused]
//   float4 ViewportSize;               // Offset:  640 Size:    16 [unused]
//   float3 ViewPoint;                  // Offset:  656 Size:    12 [unused]
//   float3 CameraPosition;             // Offset:  672 Size:    12 [unused]
//   float3 CameraPosition_Previous;    // Offset:  688 Size:    12 [unused]
//   float3 CameraPositionFractions;    // Offset:  704 Size:    12 [unused]
//   float3 CameraPositionFractions_Previous;// Offset:  720 Size:    12 [unused]
//   float3 CameraRight;                // Offset:  736 Size:    12 [unused]
//   float3 CameraUp;                   // Offset:  752 Size:    12 [unused]
//   float3 CameraDirection;            // Offset:  768 Size:    12 [unused]
//   float3 EnvironmentMapColorOffset;  // Offset:  784 Size:    12 [unused]
//   float3 EnvironmentMapColorScale;   // Offset:  800 Size:    12 [unused]
//   float3 AmbientOcclusionRanges;     // Offset:  816 Size:    12 [unused]
//   float3 DepthTextureRcpSize;        // Offset:  832 Size:    12 [unused]
//   float2 MotionVectorOffsets;        // Offset:  848 Size:     8 [unused]
//   float2 DepthRangeCompression;      // Offset:  856 Size:     8 [unused]
//   float ShadowProjDepthMinValue;     // Offset:  864 Size:     4 [unused]
//   float DistanceScale;               // Offset:  868 Size:     4 [unused]
//   float WorldSpaceZOffset;           // Offset:  872 Size:     4 [unused]
//   float WorldSpaceZOffset_Previous;  // Offset:  876 Size:     4 [unused]
//   float CameraFOVDeg;                // Offset:  880 Size:     4 [unused]
//   float EffectsEmissiveEVBias;       // Offset:  884 Size:     4 [unused]
//   float ShadowCSMLastSliceIndex;     // Offset:  888 Size:     4 [unused]
//   float UseNormalization;            // Offset:  892 Size:     4 [unused]
//   float FogUndergroundColorScale;    // Offset:  896 Size:     4 [unused]
//
// }
//
// cbuffer CTileLightCullingParameterProvider
// {
//
//   float4x4 InvViewProjection;        // Offset:    0 Size:    64 [unused]
//   float4 DepthValueScale;            // Offset:   64 Size:    16
//   float4 OutputTextureSize;          // Offset:   80 Size:    16
//   int4 NumTiles;                     // Offset:   96 Size:    16
//   int LightCount;                    // Offset:  112 Size:     4
//
// }
//
// Resource bind info for Lights
// {
//
//   struct LightCullingData
//   {
//       
//       float4 pos;                    // Offset:    0
//       float radius;                  // Offset:   16
//       float maxDistance;             // Offset:   20
//       uint properties;               // Offset:   24
//       uint padding;                  // Offset:   28
//
//   } $Element;                        // Offset:    0 Size:    32
//
// }
//
// Resource bind info for LightIndicesPerTile
// {
//
//   uint $Element;                     // Offset:    0 Size:     4
//
// }
//
// Resource bind info for LightOutputPerTile
// {
//
//   uint $Element;                     // Offset:    0 Size:     4
//
// }
//
// Resource bind info for LightIndicesPerTileForBlended
// {
//
//   uint $Element;                     // Offset:    0 Size:     4
//
// }
//
// Resource bind info for LightOutputPerTileForBlended
// {
//
//   uint $Element;                     // Offset:    0 Size:     4
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// MinMaxDepth                       texture  float4          2d    0        1
// Lights                            texture  struct         r/o    1        1
// LightIndicesPerTile                   UAV  struct         r/w    0        1
// LightOutputPerTile                    UAV  struct         r/w    1        1
// LightIndicesPerTileForBlended         UAV  struct         r/w    2        1
// LightOutputPerTileForBlended          UAV  struct         r/w    3        1
// CViewportShaderParameterProvider    cbuffer      NA          NA    0        1
// CTileLightCullingParameterProvider    cbuffer      NA          NA    1        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// no Input
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// no Output
cs_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb0[32], immediateIndexed
dcl_constantbuffer cb1[8], immediateIndexed
dcl_resource_texture2d (float,float,float,float) t0
dcl_resource_structured t1, 32 
dcl_uav_structured u0, 4
dcl_uav_structured u1, 4
dcl_uav_structured u2, 4
dcl_uav_structured u3, 4
dcl_input vThreadID.xy
dcl_temps 24
dcl_thread_group 64, 1, 1

dcl_resource_texture2d (float,float,float,float) t125
dcl_resource_texture1d (float,float,float,float) t120

ld_indexable(texture2d)(float,float,float,float) r20.xyzw, l(0, 0, 0, 0), t125.xyzw

uge r0.x, vThreadID.x, cb1[6].x
if_nz r0.x
  ret 
endif 
imad r0.x, vThreadID.y, cb1[6].x, vThreadID.x
mov r1.xy, vThreadID.xyxx
mov r1.zw, l(0,0,0,0)
ld_indexable(texture2d)(float,float,float,float) r0.yz, r1.xyzw, t0.zxyw
rcp r1.xy, r0.yzyy
add r1.xy, r1.xyxx, -cb1[4].xxxx
div r1.xy, r1.xyxx, cb1[4].yyyy
mul r2.xw, r0.yyyz, cb0[31].yyyy
mul r0.yz, cb1[5].xxyx, l(0.000000, 0.062500, 0.062500, 0.000000)
utof r3.xyz, vThreadID.xyxx
mad r4.zw, cb1[5].xxxy, l(0.000000, 0.000000, 0.062500, 0.062500), -r3.xxxy
mul r4.y, r0.y, -cb0[8].x
mul r4.x, r0.z, cb0[9].y
add r5.xyzw, -r4.yzxw, l(0.000000, -1.000000, 0.000000, -1.000000)
add r4.xyzw, r4.yzxw, l(0.000000, -1.000000, 0.000000, -1.000000)
mov r6.y, -r2.x
dp2 r0.y, r5.xyxx, r5.xyxx
sqrt r0.y, r0.y
rcp r0.y, r0.y
mul r0.yz, r0.yyyy, r5.xxyx
dp2 r0.w, r4.xyxx, r4.xyxx
sqrt r0.w, r0.w
rcp r0.w, r0.w
mul r1.zw, r0.wwww, r4.xxxy
dp2 r0.w, r5.zwzz, r5.zwzz
sqrt r0.w, r0.w
rcp r0.w, r0.w
mul r4.xy, r0.wwww, r5.zwzz
dp2 r0.w, r4.zwzz, r4.zwzz
sqrt r0.w, r0.w
rcp r0.w, r0.w
mul r4.zw, r0.wwww, r4.zzzw
itof r5.xy, cb1[6].xyxx
div r5.xy, l(2.000000, -2.000000, 0.000000, 0.000000), r5.xyxx
mad r3.xy, r3.xyxx, r5.xyxx, l(-1.000000, 1.000000, 0.000000, 0.000000)
iadd r5.zw, vThreadID.xxxy, l(0, 0, 1, 1)
utof r3.w, r5.w
mad r3.zw, r3.zzzw, r5.xxxy, l(0.000000, 0.000000, -1.000000, 1.000000)
utof r7.xzw, r5.zzzw
utof r7.y, vThreadID.y
mad r5.xyzw, r7.xyzw, r5.xyxy, l(-1.000000, 1.000000, -1.000000, 1.000000)
mul r7.x, r3.x, cb0[0].x
mul r7.y, r3.y, cb0[1].y
mad r1.xy, r1.xyxx, cb0[3].zzzz, cb0[3].wwww
mov r7.z, l(-1.000000)
div r8.xyz, r7.xyzx, r1.xxxx
mul r3.x, r3.z, cb0[0].x
mul r3.y, r3.w, cb0[1].y
mov r3.z, l(-1.000000)
div r9.xyz, r3.xyzx, r1.xxxx
mul r10.x, r5.x, cb0[0].x
mul r10.y, r5.y, cb0[1].y
mov r10.z, l(-1.000000)
div r11.xyz, r10.xyzx, r1.xxxx
mul r5.x, r5.z, cb0[0].x
mul r5.y, r5.w, cb0[1].y
mov r5.zw, l(0,0,-1.000000,1.000000)
div r12.xyz, r5.xyzx, r1.xxxx
div r13.xyz, r7.xyzx, r1.yyyy
div r14.xyz, r3.xyzx, r1.yyyy
div r15.xyz, r10.xyzx, r1.yyyy
div r16.xyz, r5.xyzx, r1.yyyy
add r0.w, cb0[3].w, cb0[3].z
div r7.xyz, r7.xyzx, r0.wwww
div r3.xyz, r3.xyzx, r0.wwww
div r10.xyz, r10.xyzx, r0.wwww
div r5.xyz, r5.xyzx, r0.wwww
min r17.xyz, r8.xyzx, r9.xyzx
min r18.xyz, r11.xyzx, r12.xyzx
min r17.xyz, r17.xyzx, r18.xyzx
min r18.xyz, r13.xyzx, r14.xyzx
min r19.xyz, r15.xyzx, r16.xyzx
min r18.xyz, r18.xyzx, r19.xyzx
min r17.xyz, r17.xyzx, r18.xyzx
max r8.xyz, r8.xyzx, r9.xyzx
max r9.xyz, r11.xyzx, r12.xyzx
max r8.xyz, r8.xyzx, r9.xyzx
max r9.xyz, r13.xyzx, r14.xyzx
max r11.xyz, r15.xyzx, r16.xyzx
max r9.xyz, r9.xyzx, r11.xyzx
max r8.xyz, r8.xyzx, r9.xyzx
min r11.xyz, r3.xyzx, r7.xyzx
min r12.xyz, r5.xyzx, r10.xyzx
min r11.xyz, r11.xyzx, r12.xyzx
min r11.xyz, r18.xyzx, r11.xyzx
max r3.xyz, r3.xyzx, r7.xyzx
max r5.xyz, r5.xyzx, r10.xyzx
max r3.xyz, r3.xyzx, r5.xyzx
max r3.xyz, r9.xyzx, r3.xyzx
ishl r0.w, r0.x, l(1)
mov r7.xyz, l(0,0,0,0)
mov r9.xyz, l(0,0,0,0)
mov r1.xy, l(0,0,0,0)
mov r3.w, l(0)
loop 
  uge r6.z, r3.w, l(64)
  breakc_nz r6.z
  uge r6.z, r3.w, cb1[7].x
  breakc_nz r6.z

// LightCullingData.pos:
  ld_structured_indexable(structured_buffer, stride=32)(mixed,mixed,mixed,mixed) r5.xyz, r3.w, l(0), t1.xyzx
  ld_structured_indexable(structured_buffer, stride=32)(mixed,mixed,mixed,mixed) r6.z, r3.w, l(16), t1.xxxx
  ld_structured_indexable(structured_buffer, stride=32)(mixed,mixed,mixed,mixed) r6.w, r3.w, l(24), t1.xxxx

// Apply world-space correction to light position to remove tile clipping:

mov r21.xyz, r5.xyz
mov r21.w, l(1.0)

dp4 r22.w, r21.xyzw, cb0[19]

add r22.x, r22.w, -r20.y
mul r22.x, r22.x, r20.x
mul r22.x, r22.x, cb0[0]
mov r22.yzw, l(0.0)

dp4 r23.x, r22.xyzw, cb0[4]
dp4 r23.y, r22.xyzw, cb0[5]
dp4 r23.z, r22.xyzw, cb0[6]

add r5.xyz, r5.xyz, r23.xyz

  dp4 r2.x, r5.xyzw, cb0[12].xyzw
  dp4 r2.y, r5.xyzw, cb0[13].xyzw
  dp4 r2.z, r5.xyzw, cb0[14].xyzw
  add r5.xyz, -r2.xyzx, r17.xyzx
  max r5.xyz, r5.xyzx, l(0.000000, 0.000000, 0.000000, 0.000000)
  add r10.xyz, -r8.xyzx, r2.xyzx
  max r10.xyz, r10.xyzx, l(0.000000, 0.000000, 0.000000, 0.000000)
  add r5.xyz, r5.xyzx, r10.xyzx
  dp3 r5.x, r5.xyzx, r5.xyzx
  mul r5.y, r6.z, r6.z
  ge r5.x, r5.y, r5.x
  add r10.xyz, -r2.xyzx, r11.xyzx
  max r10.xyz, r10.xyzx, l(0.000000, 0.000000, 0.000000, 0.000000)
  add r12.xyz, -r3.xyzx, r2.xyzx
  max r12.xyz, r12.xyzx, l(0.000000, 0.000000, 0.000000, 0.000000)
  add r10.xyz, r10.xyzx, r12.xyzx
  dp3 r5.z, r10.xyzx, r10.xyzx
  ge r5.y, r5.y, r5.z
  dp2 r5.z, r0.yzyy, r2.xzxx
  ge r5.z, r5.z, -r6.z
  and r5.xy, r5.zzzz, r5.xyxx
  dp2 r2.x, r1.zwzz, r2.xzxx
  ge r2.x, r2.x, -r6.z
  and r5.x, r2.x, r5.x
  and r2.x, r2.x, r5.y
  dp2 r5.y, r4.xyxx, r2.yzyy
  ge r5.y, r5.y, -r6.z
  and r5.x, r5.y, r5.x
  and r2.x, r2.x, r5.y
  dp2 r2.y, r4.zwzz, r2.yzyy
  ge r2.y, r2.y, -r6.z
  and r5.x, r2.y, r5.x
  and r2.x, r2.y, r2.x
  dp2 r2.y, l(1.000000, 1.000000, 0.000000, 0.000000), r2.zwzz
  ge r2.y, r2.y, -r6.z
  and r5.x, r2.y, r5.x
  and r2.x, r2.y, r2.x
  mov r6.x, r2.z
  dp2 r2.y, l(-1.000000, 1.000000, 0.000000, 0.000000), r6.xyxx
  ge r2.y, r2.y, -r6.z
  and r2.y, r2.y, r5.x
  ushr r2.z, r3.w, l(5)
  and r5.x, r3.w, l(-32)
  iadd r5.x, r3.w, -r5.x
  if_nz r2.y
    iadd r2.y, r0.w, r2.z
    ld_structured_indexable(structured_buffer, stride=4)(mixed,mixed,mixed,mixed) r5.y, r2.y, l(0), u0.xxxx
    bfi r5.y, l(1), r5.x, l(1), r5.y
    store_structured u0.x, r2.y, l(0), r5.y
    iadd r1.x, r1.x, l(1)
    and r10.xyz, r6.wwww, l(0x80000000, 0x40000000, 0x20000000, 0)
    movc r10.xyz, r10.xyzx, l(1,1,1,0), l(0,0,0,0)
    iadd r7.xyz, r7.xyzx, r10.xyzx
  endif 
  if_nz r2.x
    iadd r2.x, r0.w, r2.z
    ld_structured_indexable(structured_buffer, stride=4)(mixed,mixed,mixed,mixed) r2.y, r2.x, l(0), u2.xxxx
    bfi r2.y, l(1), r5.x, l(1), r2.y
    store_structured u2.x, r2.x, l(0), r2.y
    iadd r1.y, r1.y, l(1)
    and r2.xyz, r6.wwww, l(0x80000000, 0x40000000, 0x20000000, 0)
    movc r2.xyz, r2.xyzx, l(1,1,1,0), l(0,0,0,0)
    iadd r9.xyz, r9.xyzx, r2.xyzx
  endif 
  iadd r3.w, r3.w, l(1)
endloop 
ult r0.yzw, l(0, 0, 0, 0), r7.xxyz
and r0.yzw, r0.yyzw, l(0, 0x80000000, 0x40000000, 0x20000000)
or r0.y, r0.y, r1.x
or r0.y, r0.z, r0.y
or r0.y, r0.w, r0.y
store_structured u1.x, r0.x, l(0), r0.y
ult r0.yzw, l(0, 0, 0, 0), r9.xxyz
and r0.yzw, r0.yyzw, l(0, 0x80000000, 0x40000000, 0x20000000)
or r0.y, r0.y, r1.y
or r0.y, r0.z, r0.y
or r0.y, r0.w, r0.y
store_structured u3.x, r0.x, l(0), r0.y
ret 
// Approximately 185 instruction slots used