// ---- Created with 3Dmigoto v1.2.35 on Tue Apr 26 22:30:27 2016

cbuffer cbLightAccParam : register(b0)
{
  float4x4 g_InvProjMatrix : packoffset(c0);
  float4x3 g_ViewMatrix : packoffset(c4);
  float4 g_ScreenSize : packoffset(c7);
  float4x4 g_CameraMatrix : packoffset(c8);
  float4 g_specPowerParam : packoffset(c12);
  float4x4 g_ProjMatrix : packoffset(c13);
  uint g_lightCount : packoffset(c17);
  bool g_visualizeLightCount : packoffset(c17.y);
  uint g_spotLightCount : packoffset(c17.z);
  uint g_lineLightCount : packoffset(c17.w);
  float g_frustumNear : packoffset(c18);
  float g_frustumFar : packoffset(c18.y);
  float g_frustumSliceScale : packoffset(c18.z);
  float g_frustumSliceBias : packoffset(c18.w);
  uint4 g_lightTileSize : packoffset(c19);
}

cbuffer cbTileComputeParam : register(b5)
{
  uint4 g_FrameBufferDimensions : packoffset(c0);
}

Texture2D<float4> g_DepthTexture : register(t1);
StructuredBuffer<g_LightBuffer> g_LightBuffer : register(t19);


// 3Dmigoto declarations
#define cmp -
Texture1D<float4> IniParams : register(t120);
Texture2D<float4> StereoParams : register(t125);


void main()
{
// Needs manual fix for instruction: 
// unknown dcl_: dcl_resource_structured t19, 64 
// Needs manual fix for instruction: 
// unknown dcl_: dcl_uav_structured u0, 8
// Needs manual fix for instruction: 
// unknown dcl_: dcl_uav_structured u3, 40
  float4 r0,r1,r2,r3,r4,r5;
  uint4 bitmask, uiDest;
  float4 fDest;

  float4 x0[6];
// Needs manual fix for instruction: 
// unknown dcl_: dcl_tgsm_raw g0, 4
// Needs manual fix for instruction: 
// unknown dcl_: dcl_tgsm_raw g1, 4
// Needs manual fix for instruction: 
// unknown dcl_: dcl_tgsm_raw g2, 4
// Needs manual fix for instruction: 
// unknown dcl_: dcl_thread_group 32, 32, 1
  r0.x = mad((int)vThreadIDInGroup.y, 32, (int)vThreadIDInGroup.x);
  r0.yz = (uint2)vThreadID.xy;
  r0.yz = float2(0.5,0.5) + r0.yz;
  r0.yz = g_ScreenSize.zw * r0.yz;
  r1.xy = r0.yz * float2(2,-2) + float2(-1,1);
  r2.xy = vThreadID.xy;
  r2.zw = float2(0,0);
  r1.z = g_DepthTexture.Load(r2.xyz).x;
  r1.w = 1;
  r0.y = dot(r1.xyzw, g_InvProjMatrix._m02_m12_m22_m32);
  r0.z = dot(r1.xyzw, g_InvProjMatrix._m03_m13_m23_m33);
  r0.y = r0.y / r0.z;
  r0.z = cmp((int)r0.x == 0);
  if (r0.x == 0) {
  // No code for instruction (needs manual fix):
    store_raw g2.x, l(0), l(0)
  // No code for instruction (needs manual fix):
    store_raw g0.x, l(0), l(340282346638528860000000000000000000000.000000)
  // No code for instruction (needs manual fix):
    store_raw g1.x, l(0), l(0)
  }
  GroupMemoryBarrierWithGroupSync();
  // Needs manual fix for instruction:
atomic_umin g0, l(0), r0.y
  InterlockedMin(dest, value, orig_value);
  // Needs manual fix for instruction:
atomic_umax g1, l(0), r0.y
  InterlockedMax(dest, value, orig_value);
  GroupMemoryBarrierWithGroupSync();
// No code for instruction (needs manual fix):
ld_raw r1.y, l(0), g0.xxxx
// No code for instruction (needs manual fix):
ld_raw r1.z, l(0), g1.xxxx
  r0.y = (uint)g_lightTileSize.x << 1;
  r0.y = (uint)r0.y;
  r0.y = rcp(r0.y);
  r2.xy = g_ScreenSize.xy * r0.yy;
  r2.zw = (uint2)vThreadGroupID.xy;
  r3.zw = g_ScreenSize.xy * r0.yy + -r2.zw;
  r3.y = g_ProjMatrix._m00 * r2.x;
  r3.x = -g_ProjMatrix._m11 * r2.y;
  r2.xyzw = float4(0,1,0,1) + -r3.yzxw;
  r3.xyzw = float4(0,1,0,1) + r3.yzxw;
  r4.w = -r1.y;
  r0.y = dot(r2.xy, r2.xy);
  r0.y = sqrt(r0.y);
  r0.y = rcp(r0.y);
  r0.yw = r2.xy * r0.yy;
  r2.x = dot(r3.xy, r3.xy);
  r2.x = sqrt(r2.x);
  r2.x = rcp(r2.x);
  r2.xy = r3.xy * r2.xx;
  r3.x = dot(r2.zw, r2.zw);
  r3.x = sqrt(r3.x);
  r3.x = rcp(r3.x);
  r2.zw = r3.xx * r2.zw;
  r3.x = dot(r3.zw, r3.zw);
  r3.x = sqrt(r3.x);
  r3.x = rcp(r3.x);
  r3.xy = r3.zw * r3.xx;
  x0[0].x = 0;
  x0[1].x = 0;
  x0[2].x = 0;
  x0[3].x = 0;
  x0[4].x = 0;
  x0[5].x = 0;
  r3.z = r0.x;
  while (true) {
    r3.w = cmp((uint)r3.z >= g_lineLightCount);
    if (r3.w != 0) break;
  // Known bad code for instruction (needs manual fix):
    ld_structured_indexable(structured_buffer, stride=64)(mixed,mixed,mixed,mixed) r4.xyz, r3.z, l(0), t19.xyzx
  r4.x = g_DepthTexture[]..swiz;
  r4.y = g_DepthTexture[]..swiz;
  r4.z = g_DepthTexture[]..swiz;
  // Known bad code for instruction (needs manual fix):
    ld_structured_indexable(structured_buffer, stride=64)(mixed,mixed,mixed,mixed) r3.w, r3.z, l(60), t19.xxxx
  r3.w = g_DepthTexture[]..swiz;
    r5.x = dot(r0.yw, r4.xz);
    r5.x = cmp(r5.x >= -r3.w);
    x0[0].x = r5.x;
    r4.x = dot(r2.xy, r4.xz);
    r4.x = cmp(r4.x >= -r3.w);
    x0[1].x = r4.x;
    r4.x = r4.x ? r5.x : 0;
    r5.x = dot(r2.zw, r4.yz);
    r5.x = cmp(r5.x >= -r3.w);
    x0[2].x = r5.x;
    r4.x = r4.x ? r5.x : 0;
    r4.y = dot(r3.xy, r4.yz);
    r4.y = cmp(r4.y >= -r3.w);
    x0[3].x = r4.y;
    r4.x = r4.y ? r4.x : 0;
    r4.y = dot(float2(1,1), r4.zw);
    r4.y = cmp(r4.y >= -r3.w);
    x0[4].x = r4.y;
    r4.x = r4.y ? r4.x : 0;
    r1.x = r4.z;
    r4.y = dot(float2(-1,1), r1.xz);
    r3.w = cmp(r4.y >= -r3.w);
    x0[5].x = r3.w;
    r3.w = r3.w ? r4.x : 0;
    if (r3.w != 0) {
      // Needs manual fix for instruction:
        atomic_iadd g2, l(0), l(1)
      InterlockedAdd(dest, value, orig_value);
    }
    r3.z = (int)r3.z + 1024;
  }
  GroupMemoryBarrierWithGroupSync();
  r0.xy = (uint2)g_ScreenSize.xy;
  r0.xy = cmp((uint2)vThreadID.xy < (uint2)r0.xy);
  r0.x = r0.y ? r0.x : 0;
  if (r0.x != 0) {
    if (g_visualizeLightCount != 0) {
    // No code for instruction (needs manual fix):
        ld_raw r0.x, l(0), g2.xxxx
      r0.x = (uint)r0.x;
      r0.x = 0.0078125 * r0.x;
      r0.y = mad((int)vThreadID.y, (int)g_FrameBufferDimensions.x, (int)vThreadID.x);
      r0.x = f32tof16(r0.x);
      r0.x = mad((int)r0.x, 0x00010000, (int)r0.x);
      [r0.y].0 = u0.xy;
    } else {
      r0.x = mad((int)vThreadID.y, (int)g_FrameBufferDimensions.x, (int)vThreadID.x);
      [r0.x].0 = u0.xy;
    }
  }
  r0.x = cmp(0 < g_lineLightCount);
  r0.x = r0.x ? r0.z : 0;
  if (r0.x != 0) {
    r0.x = g_ScreenSize.x + 255;
    r0.x = 0.00390625 * r0.x;
    r0.x = (uint)r0.x;
    r0.yz = (uint2)vThreadID.xy >> int2(8,8);
    r0.xyz = (uint3)r0.xyz;
    r0.x = r0.z * r0.x + r0.y;
    r0.x = (uint)r0.x;
  // No code for instruction (needs manual fix):
    ld_raw r1.w, l(0), g2.xxxx
    r1.x = 0;
    [r0.x].0 = u3.xyzw;
    r1.x = x0[0].x;
    r1.y = x0[1].x;
    r1.z = x0[2].x;
    r1.w = x0[3].x;
    [r0.x].16 = u3.xyzw;
    r1.x = x0[4].x;
    r1.y = x0[5].x;
    [r0.x].32 = u3.xy;
  }
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Original ASM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.30.9200.20546
//
//
// Buffer Definitions: 
//
// cbuffer cbLightAccParam
// {
//
//   float4x4 g_InvProjMatrix;          // Offset:    0 Size:    64
//   float4x3 g_ViewMatrix;             // Offset:   64 Size:    48 [unused]
//   float4 g_ScreenSize;               // Offset:  112 Size:    16
//   float4x4 g_CameraMatrix;           // Offset:  128 Size:    64 [unused]
//   float4 g_specPowerParam;           // Offset:  192 Size:    16 [unused]
//   float4x4 g_ProjMatrix;             // Offset:  208 Size:    64
//   uint g_lightCount;                 // Offset:  272 Size:     4 [unused]
//   bool g_visualizeLightCount;        // Offset:  276 Size:     4
//   uint g_spotLightCount;             // Offset:  280 Size:     4 [unused]
//   uint g_lineLightCount;             // Offset:  284 Size:     4
//   float g_frustumNear;               // Offset:  288 Size:     4 [unused]
//   float g_frustumFar;                // Offset:  292 Size:     4 [unused]
//   float g_frustumSliceScale;         // Offset:  296 Size:     4 [unused]
//   float g_frustumSliceBias;          // Offset:  300 Size:     4 [unused]
//   uint4 g_lightTileSize;             // Offset:  304 Size:    16
//
// }
//
// cbuffer cbTileComputeParam
// {
//
//   uint4 g_FrameBufferDimensions;     // Offset:    0 Size:    16
//
// }
//
// Resource bind info for g_LightBuffer
// {
//
//   struct PointLight
//   {
//       
//       float4 PositionAndRadius;      // Offset:    0
//       float4 DiffColor;              // Offset:   16
//       float4 SpecColor;              // Offset:   32
//       float4 TransLightParam;        // Offset:   48
//
//   } $Element;                        // Offset:    0 Size:    64
//
// }
//
// Resource bind info for g_FrameBuffer
// {
//
//   uint2 $Element;                    // Offset:    0 Size:     8
//
// }
//
// Resource bind info for g_debugInfo
// {
//
//   struct DebugInfo
//   {
//       
//       float info;                    // Offset:    0
//       float minZ;                    // Offset:    4
//       float maxZ;                    // Offset:    8
//       uint numLights;                // Offset:   12
//       bool inFrustum[6];             // Offset:   16
//
//   } $Element;                        // Offset:    0 Size:    40
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// g_DepthTexture                    texture  float4          2d    1        1
// g_LightBuffer                     texture  struct         r/o   19        1
// g_FrameBuffer                         UAV  struct         r/w    0        1
// g_debugInfo                           UAV  struct         r/w    3        1
// cbLightAccParam                   cbuffer      NA          NA    0        1
// cbTileComputeParam                cbuffer      NA          NA    5        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[20], immediateIndexed
dcl_constantbuffer cb5[1], immediateIndexed
dcl_resource_texture2d (float,float,float,float) t1
dcl_resource_structured t19, 64 
dcl_uav_structured u0, 8
dcl_uav_structured u3, 40
dcl_input vThreadGroupID.xy
dcl_input vThreadIDInGroup.xy
dcl_input vThreadID.xy
dcl_temps 6
dcl_indexableTemp x0[6], 4
dcl_tgsm_raw g0, 4
dcl_tgsm_raw g1, 4
dcl_tgsm_raw g2, 4
dcl_thread_group 32, 32, 1
imad r0.x, vThreadIDInGroup.y, l(32), vThreadIDInGroup.x
utof r0.yz, vThreadID.xxyx
add r0.yz, r0.yyzy, l(0.000000, 0.500000, 0.500000, 0.000000)
mul r0.yz, r0.yyzy, cb0[7].zzwz
mad r1.xy, r0.yzyy, l(2.000000, -2.000000, 0.000000, 0.000000), l(-1.000000, 1.000000, 0.000000, 0.000000)
mov r2.xy, vThreadID.xyxx
mov r2.zw, l(0,0,0,0)
ld_indexable(texture2d)(float,float,float,float) r1.z, r2.xyzw, t1.yzxw
mov r1.w, l(1.000000)
dp4 r0.y, r1.xyzw, cb0[2].xyzw
dp4 r0.z, r1.xyzw, cb0[3].xyzw
div r0.y, r0.y, r0.z
ieq r0.z, r0.x, l(0)
if_z r0.x
  store_raw g2.x, l(0), l(0)
  store_raw g0.x, l(0), l(340282346638528860000000000000000000000.000000)
  store_raw g1.x, l(0), l(0)
endif 
sync_g_t
atomic_umin g0, l(0), r0.y
atomic_umax g1, l(0), r0.y
sync_g_t
ld_raw r1.y, l(0), g0.xxxx
ld_raw r1.z, l(0), g1.xxxx
ishl r0.y, cb0[19].x, l(1)
utof r0.y, r0.y
rcp r0.y, r0.y
mul r2.xy, r0.yyyy, cb0[7].xyxx
utof r2.zw, vThreadGroupID.xxxy
mad r3.zw, cb0[7].xxxy, r0.yyyy, -r2.zzzw
mul r3.y, r2.x, cb0[13].x
mul r3.x, r2.y, -cb0[14].y
add r2.xyzw, -r3.yzxw, l(0.000000, 1.000000, 0.000000, 1.000000)
add r3.xyzw, r3.yzxw, l(0.000000, 1.000000, 0.000000, 1.000000)
mov r4.w, -r1.y
dp2 r0.y, r2.xyxx, r2.xyxx
sqrt r0.y, r0.y
rcp r0.y, r0.y
mul r0.yw, r0.yyyy, r2.xxxy
dp2 r2.x, r3.xyxx, r3.xyxx
sqrt r2.x, r2.x
rcp r2.x, r2.x
mul r2.xy, r2.xxxx, r3.xyxx
dp2 r3.x, r2.zwzz, r2.zwzz
sqrt r3.x, r3.x
rcp r3.x, r3.x
mul r2.zw, r2.zzzw, r3.xxxx
dp2 r3.x, r3.zwzz, r3.zwzz
sqrt r3.x, r3.x
rcp r3.x, r3.x
mul r3.xy, r3.xxxx, r3.zwzz
mov x0[0].x, l(0)
mov x0[1].x, l(0)
mov x0[2].x, l(0)
mov x0[3].x, l(0)
mov x0[4].x, l(0)
mov x0[5].x, l(0)
mov r3.z, r0.x
loop 
  uge r3.w, r3.z, cb0[17].w
  breakc_nz r3.w
  ld_structured_indexable(structured_buffer, stride=64)(mixed,mixed,mixed,mixed) r4.xyz, r3.z, l(0), t19.xyzx
  ld_structured_indexable(structured_buffer, stride=64)(mixed,mixed,mixed,mixed) r3.w, r3.z, l(60), t19.xxxx
  dp2 r5.x, r0.ywyy, r4.xzxx
  ge r5.x, r5.x, -r3.w
  mov x0[0].x, r5.x
  dp2 r4.x, r2.xyxx, r4.xzxx
  ge r4.x, r4.x, -r3.w
  mov x0[1].x, r4.x
  and r4.x, r4.x, r5.x
  dp2 r5.x, r2.zwzz, r4.yzyy
  ge r5.x, r5.x, -r3.w
  mov x0[2].x, r5.x
  and r4.x, r4.x, r5.x
  dp2 r4.y, r3.xyxx, r4.yzyy
  ge r4.y, r4.y, -r3.w
  mov x0[3].x, r4.y
  and r4.x, r4.y, r4.x
  dp2 r4.y, l(1.000000, 1.000000, 0.000000, 0.000000), r4.zwzz
  ge r4.y, r4.y, -r3.w
  mov x0[4].x, r4.y
  and r4.x, r4.y, r4.x
  mov r1.x, r4.z
  dp2 r4.y, l(-1.000000, 1.000000, 0.000000, 0.000000), r1.xzxx
  ge r3.w, r4.y, -r3.w
  mov x0[5].x, r3.w
  and r3.w, r3.w, r4.x
  if_nz r3.w
    atomic_iadd g2, l(0), l(1)
  endif 
  iadd r3.z, r3.z, l(1024)
endloop 
sync_g_t
ftou r0.xy, cb0[7].xyxx
ult r0.xy, vThreadID.xyxx, r0.xyxx
and r0.x, r0.y, r0.x
if_nz r0.x
  if_nz cb0[17].y
    ld_raw r0.x, l(0), g2.xxxx
    utof r0.x, r0.x
    mul r0.x, r0.x, l(0.007813)
    imad r0.y, vThreadID.y, cb5[0].x, vThreadID.x
    f32tof16 r0.x, r0.x
    imad r0.x, r0.x, l(0x00010000), r0.x
    store_structured u0.xy, r0.y, l(0), r0.xxxx
  else 
    imad r0.x, vThreadID.y, cb5[0].x, vThreadID.x
    store_structured u0.xy, r0.x, l(0), l(0,0,0,0)
  endif 
endif 
ult r0.x, l(0), cb0[17].w
and r0.x, r0.x, r0.z
if_nz r0.x
  add r0.x, l(255.000000), cb0[7].x
  mul r0.x, r0.x, l(0.003906)
  ftou r0.x, r0.x
  ushr r0.yz, vThreadID.xxyx, l(0, 8, 8, 0)
  utof r0.xyz, r0.xyzx
  mad r0.x, r0.z, r0.x, r0.y
  ftou r0.x, r0.x
  ld_raw r1.w, l(0), g2.xxxx
  mov r1.x, l(0)
  store_structured u3.xyzw, r0.x, l(0), r1.xyzw
  mov r1.x, x0[0].x
  mov r1.y, x0[1].x
  mov r1.z, x0[2].x
  mov r1.w, x0[3].x
  store_structured u3.xyzw, r0.x, l(16), r1.xyzw
  mov r1.x, x0[4].x
  mov r1.y, x0[5].x
  store_structured u3.xy, r0.x, l(32), r1.xyxx
endif 
ret 
// Approximately 133 instruction slots used

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
