//light from fireplace-- needs vpos correction
// ---- Created with 3Dmigoto v1.2.39 on Sat Jun 11 12:15:51 2016

cbuffer _Globals : register(b0)
{
  float4x4 ScreenToWorldMatrix : packoffset(c0);
  bool bDecompressSceneColor : packoffset(c4);
  float SubSurfaceQuality : packoffset(c4.y);
  float4 SampleDeltaUVs[9] : packoffset(c5);
  float2 ClipToViewScaleXY : packoffset(c14);
  float2 ViewToClipScaleXY : packoffset(c14.z);
  float4 NoiseScaleAndOffset : packoffset(c15);
}

cbuffer PSOffsetConstants : register(b2)
{
  float4 ScreenPositionScaleBias : packoffset(c0);
  float4 MinZ_MaxZRatio : packoffset(c1);
  float NvStereoEnabled : packoffset(c2);
  float4 DiffuseOverrideParameter : packoffset(c3);
  float4 SpecularOverrideParameter : packoffset(c4);
  float4 CameraPositionPS : packoffset(c5);
  float4 ScreenAndTexelSize : packoffset(c6);
}

SamplerState SceneDepthTextureSampler_s : register(s0);
SamplerState NvStereoFixTextureSampler_s : register(s1);
SamplerState SubsurfaceInscatteringTextureSampler_s : register(s2);
SamplerState SubsurfaceScatteringAttenuationTextureSampler_s : register(s3);
SamplerState RandomAngleTextureSampler_s : register(s4);
Texture2D<float4> SceneDepthTexture : register(t0);
Texture2D<float4> NvStereoFixTexture : register(t1);
Texture2D<float4> SubsurfaceInscatteringTexture : register(t2);
Texture2D<float4> SubsurfaceScatteringAttenuationTexture : register(t3);
Texture2D<float4> RandomAngleTexture : register(t4);


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


void main( 
  float4 v0 : TEXCOORD0,
  float4 v1 : SV_POSITION0,
  out float4 o0 : SV_Target0)
{
  float4 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r11,r12;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.xy = v0.xy * ScreenPositionScaleBias.xy + ScreenPositionScaleBias.wz;

float4 res = IniParams.Load(int2(1, 0));
r12.xy = ((1 / res.xy) * v1.xy) + (0.5 / res.xy);

//r11.z = 1 / res.x;
//r11.w = 1 / res.y;
//r11.xy = r11.zw * v1.xy;
//r11.zw = r11.zw * 0.5;
//r12.xy = r11.xy + r11.zw;

  r1.xyzw = SubsurfaceScatteringAttenuationTexture.SampleLevel(SubsurfaceScatteringAttenuationTextureSampler_s, r0.xy, 0).xyzw;
  r0.z = cmp(r1.w >= 0.00392156886);
  if (r0.z != 0) {
    r0.z = r1.w * r1.w;
    r2.xyz = float3(32,32,32) * r0.zzz;
    r1.xyz = log2(r1.xyz);
    r1.xyz = r1.xyz / r2.xxx;
    r0.z = cmp(0 != NvStereoEnabled);
    if (r0.z != 0) {
      r3.xy = NvStereoFixTexture.Sample(NvStereoFixTextureSampler_s, float2(0.0625,0)).xy;
      r0.w = 1 + -r3.y;
      r3.x = r3.x * r0.w + v0.x;
      r3.y = v0.y;
      r3.z = 1;
    }
    r4.xy = v0.xy;
    r4.z = 1;
    r3.xyz = r0.zzz ? r3.xyz : r4.xyz;
    r3.xy = r3.xy / r3.zz;
    r3.xy = r3.xy * ScreenPositionScaleBias.xy + ScreenPositionScaleBias.wz;

    r0.w = SceneDepthTexture.SampleLevel(SceneDepthTextureSampler_s, r12.xy, 0).x;
    r0.w = min(0.999000013, r0.w);
    r0.w = r0.w * MinZ_MaxZRatio.z + -MinZ_MaxZRatio.w;
    r3.w = 1 / r0.w;
    r3.yz = v0.xy * r3.ww;
    r0.xy = r0.xy * NoiseScaleAndOffset.xy + NoiseScaleAndOffset.wz;
    r0.xy = RandomAngleTexture.SampleLevel(RandomAngleTextureSampler_s, r0.xy, 0).xy;
    r0.xy = r0.xy * float2(2,2) + float2(-1,-1);
    r4.xy = NvStereoFixTexture.Sample(NvStereoFixTextureSampler_s, float2(0.0625,0)).xy;
    r0.w = 1 / r2.x;
    r5.w = 0;
    r6.w = 0;
    r7.xyzw = float4(0,0,0,0);
    r1.w = 0;
    while (true) {
      r2.x = cmp((int)r1.w >= 9);
      if (r2.x != 0) break;
      r2.x = dot(SampleDeltaUVs[r1.w].xy, r0.xy);
      r2.xw = r2.xx * r0.xy;
      r2.xw = -r2.xw * float2(2,2) + SampleDeltaUVs[r1.w].xy;
      r2.xw = ViewToClipScaleXY.xy * r2.xw;
      r5.yz = r2.xw * r2.yy;
      r8.yzw = r5.yzw + r3.yzw;
      r2.xw = r8.yz / r8.ww;
      r4.zw = r2.xw * ScreenPositionScaleBias.xy + ScreenPositionScaleBias.wz;

      r5.xyz = SubsurfaceInscatteringTexture.SampleLevel(SubsurfaceInscatteringTextureSampler_s, r12.xy, 0).xyz;
      if (r0.z != 0) {
        r3.x = r8.w + -r4.y;
        r8.x = r4.x * r3.x + r8.y;
        r9.xyz = r8.xzw;
      }
      r8.xyz = r0.zzz ? r9.xyz : r8.yzw;
      r4.zw = r8.xy / r8.zz;
      r4.zw = r4.zw * ScreenPositionScaleBias.xy + ScreenPositionScaleBias.wz;

      r3.x = SceneDepthTexture.SampleLevel(SceneDepthTextureSampler_s, r12.xy, 0).x;
      r3.x = min(0.999000013, r3.x);
      r3.x = r3.x * MinZ_MaxZRatio.z + -MinZ_MaxZRatio.w;
      r8.z = 1 / r3.x;
      r8.xy = r8.zz * r2.xw;
      r8.xyz = r8.xyz + -r3.yzw;
      r8.xy = ClipToViewScaleXY.xy * r8.xy;
      r2.x = dot(r8.xyz, r8.xyz);
      r2.x = sqrt(r2.x);
      r2.w = cmp(0 != r2.x);
      r2.x = r2.w ? r2.x : 1;
      r8.xyz = r2.xxx * r1.xyz;
      r8.xyz = exp2(r8.xyz);
      r2.x = 1 / r2.x;
      r9.w = saturate(r2.x + -r0.w);
      r8.xyz = r9.www * r8.xyz;
      r9.xyz = r8.xyz * r5.xyz;
      r8.xyzw = r7.xyzw + r9.xyzw;
      r2.x = dot(SampleDeltaUVs[r1.w].zw, r0.xy);
      r2.xw = r2.xx * r0.xy;
      r2.xw = -r2.xw * float2(2,2) + SampleDeltaUVs[r1.w].zw;
      r2.xw = ViewToClipScaleXY.xy * r2.xw;
      r6.yz = r2.xw * r2.zy;
      r9.yzw = r6.yzw + r3.yzw;
      r2.xw = r9.yz / r9.ww;
      r4.zw = r2.xw * ScreenPositionScaleBias.xy + ScreenPositionScaleBias.wz;

      r5.xyz = SubsurfaceInscatteringTexture.SampleLevel(SubsurfaceInscatteringTextureSampler_s, r12.xy, 0).xyz;
      if (r0.z != 0) {
        r3.x = r9.w + -r4.y;
        r9.x = r4.x * r3.x + r9.y;
        r6.xyz = r9.xzw;
      }
      r6.xyz = r0.zzz ? r6.xyz : r9.yzw;
      r4.zw = r6.xy / r6.zz;
      r4.zw = r4.zw * ScreenPositionScaleBias.xy + ScreenPositionScaleBias.wz;

      r3.x = SceneDepthTexture.SampleLevel(SceneDepthTextureSampler_s, r12.xy, 0).x;
      r3.x = min(0.999000013, r3.x);
      r3.x = r3.x * MinZ_MaxZRatio.z + -MinZ_MaxZRatio.w;
      r6.z = 1 / r3.x;
      r6.xy = r6.zz * r2.xw;
      r6.xyz = r6.xyz + -r3.yzw;
      r6.xy = ClipToViewScaleXY.xy * r6.xy;
      r2.x = dot(r6.xyz, r6.xyz);
      r2.x = sqrt(r2.x);
      r2.w = cmp(0 != r2.x);
      r2.x = r2.w ? r2.x : 1;
      r6.xyz = r2.xxx * r1.xyz;
      r6.xyz = exp2(r6.xyz);
      r2.x = 1 / r2.x;
      r9.w = saturate(r2.x + -r0.w);
      r6.xyz = r9.www * r6.xyz;
      r9.xyz = r6.xyz * r5.xyz;
      r7.xyzw = r9.xyzw + r8.xyzw;
      r1.w = (int)r1.w + 1;
    }
    r0.x = max(9.99999975e-005, r7.w);
    r0.xyz = r7.xyz / r0.xxx;
  } else {
    r0.xyz = float3(0,0,0);
  }
  o0.xyz = max(float3(0,0,0), r0.xyz);
  o0.w = 0;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//   using 3Dmigoto v1.2.39 on Sat Jun 11 12:15:51 2016
//
//
// Buffer Definitions: 
//
// cbuffer $Globals
// {
//
//   float4x4 ScreenToWorldMatrix;      // Offset:    0 Size:    64 [unused]
//   bool bDecompressSceneColor;        // Offset:   64 Size:     4 [unused]
//   float SubSurfaceQuality;           // Offset:   68 Size:     4 [unused]
//   float4 SampleDeltaUVs[9];          // Offset:   80 Size:   144
//   float2 ClipToViewScaleXY;          // Offset:  224 Size:     8
//   float2 ViewToClipScaleXY;          // Offset:  232 Size:     8
//   float4 NoiseScaleAndOffset;        // Offset:  240 Size:    16
//
// }
//
// cbuffer PSOffsetConstants
// {
//
//   float4 ScreenPositionScaleBias;    // Offset:    0 Size:    16
//   float4 MinZ_MaxZRatio;             // Offset:   16 Size:    16
//   float NvStereoEnabled;             // Offset:   32 Size:     4
//   float4 DiffuseOverrideParameter;   // Offset:   48 Size:    16 [unused]
//   float4 SpecularOverrideParameter;  // Offset:   64 Size:    16 [unused]
//   float4 CameraPositionPS;           // Offset:   80 Size:    16 [unused]
//   float4 ScreenAndTexelSize;         // Offset:   96 Size:    16 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// SceneDepthTextureSampler          sampler      NA          NA    0        1
// NvStereoFixTextureSampler         sampler      NA          NA    1        1
// SubsurfaceInscatteringTextureSampler    sampler      NA          NA    2        1
// SubsurfaceScatteringAttenuationTextureSampler    sampler      NA          NA    3        1
// RandomAngleTextureSampler         sampler      NA          NA    4        1
// SceneDepthTexture                 texture  float4          2d    0        1
// NvStereoFixTexture                texture  float4          2d    1        1
// SubsurfaceInscatteringTexture     texture  float4          2d    2        1
// SubsurfaceScatteringAttenuationTexture    texture  float4          2d    3        1
// RandomAngleTexture                texture  float4          2d    4        1
// $Globals                          cbuffer      NA          NA    0        1
// PSOffsetConstants                 cbuffer      NA          NA    2        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD                 0   xyzw        0     NONE   float   xy  
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Target                0   xyzw        0   TARGET   float   xyzw
//
ps_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb0[16], dynamicIndexed
dcl_constantbuffer cb2[3], immediateIndexed
dcl_sampler s0, mode_default
dcl_sampler s1, mode_default
dcl_sampler s2, mode_default
dcl_sampler s3, mode_default
dcl_sampler s4, mode_default
dcl_resource_texture2d (float,float,float,float) t0
dcl_resource_texture2d (float,float,float,float) t1
dcl_resource_texture2d (float,float,float,float) t2
dcl_resource_texture2d (float,float,float,float) t3
dcl_resource_texture2d (float,float,float,float) t4
dcl_input_ps linear v0.xy
dcl_output o0.xyzw
dcl_temps 10
mad r0.xy, v0.xyxx, cb2[0].xyxx, cb2[0].wzww
sample_l_indexable(texture2d)(float,float,float,float) r1.xyzw, r0.xyxx, t3.xyzw, s3, l(0.000000)
ge r0.z, r1.w, l(0.00392156886)
if_nz r0.z
  mul r0.z, r1.w, r1.w
  mul r2.xyz, r0.zzzz, l(32.000000, 32.000000, 32.000000, 0.000000)
  log r1.xyz, r1.xyzx
  div r1.xyz, r1.xyzx, r2.xxxx
  ne r0.z, l(0.000000, 0.000000, 0.000000, 0.000000), cb2[2].x
  if_nz r0.z
    sample_indexable(texture2d)(float,float,float,float) r3.xy, l(0.062500, 0.000000, 0.000000, 0.000000), t1.xyzw, s1
    add r0.w, -r3.y, l(1.000000)
    mad r3.x, r3.x, r0.w, v0.x
    mov r3.y, v0.y
    mov r3.z, l(1.000000)
  endif 
  mov r4.xy, v0.xyxx
  mov r4.z, l(1.000000)
  movc r3.xyz, r0.zzzz, r3.xyzx, r4.xyzx
  div r3.xy, r3.xyxx, r3.zzzz
  mad r3.xy, r3.xyxx, cb2[0].xyxx, cb2[0].wzww
  sample_l_indexable(texture2d)(float,float,float,float) r0.w, r3.xyxx, t0.yzwx, s0, l(0.000000)
  min r0.w, r0.w, l(0.999000)
  mad r0.w, r0.w, cb2[1].z, -cb2[1].w
  div r3.w, l(1.000000, 1.000000, 1.000000, 1.000000), r0.w
  mul r3.yz, r3.wwww, v0.xxyx
  mad r0.xy, r0.xyxx, cb0[15].xyxx, cb0[15].wzww
  sample_l_indexable(texture2d)(float,float,float,float) r0.xy, r0.xyxx, t4.xyzw, s4, l(0.000000)
  mad r0.xy, r0.xyxx, l(2.000000, 2.000000, 0.000000, 0.000000), l(-1.000000, -1.000000, 0.000000, 0.000000)
  sample_indexable(texture2d)(float,float,float,float) r4.xy, l(0.062500, 0.000000, 0.000000, 0.000000), t1.xyzw, s1
  div r0.w, l(1.000000, 1.000000, 1.000000, 1.000000), r2.x
  mov r5.w, l(0)
  mov r6.w, l(0)
  mov r7.xyzw, l(0,0,0,0)
  mov r1.w, l(0)
  loop 
    ige r2.x, r1.w, l(9)
    breakc_nz r2.x
    dp2 r2.x, cb0[r1.w + 5].xyxx, r0.xyxx
    mul r2.xw, r0.xxxy, r2.xxxx
    mad r2.xw, -r2.xxxw, l(2.000000, 0.000000, 0.000000, 2.000000), cb0[r1.w + 5].xxxy
    mul r2.xw, r2.xxxw, cb0[14].zzzw
    mul r5.yz, r2.yyyy, r2.xxwx
    add r8.yzw, r3.yyzw, r5.yyzw
    div r2.xw, r8.yyyz, r8.wwww
    mad r4.zw, r2.xxxw, cb2[0].xxxy, cb2[0].wwwz
    sample_l_indexable(texture2d)(float,float,float,float) r5.xyz, r4.zwzz, t2.xyzw, s2, l(0.000000)
    if_nz r0.z
      add r3.x, -r4.y, r8.w
      mad r8.x, r4.x, r3.x, r8.y
      mov r9.xyz, r8.xzwx
    endif 
    movc r8.xyz, r0.zzzz, r9.xyzx, r8.yzwy
    div r4.zw, r8.xxxy, r8.zzzz
    mad r4.zw, r4.zzzw, cb2[0].xxxy, cb2[0].wwwz
    sample_l_indexable(texture2d)(float,float,float,float) r3.x, r4.zwzz, t0.xyzw, s0, l(0.000000)
    min r3.x, r3.x, l(0.999000)
    mad r3.x, r3.x, cb2[1].z, -cb2[1].w
    div r8.z, l(1.000000, 1.000000, 1.000000, 1.000000), r3.x
    mul r8.xy, r2.xwxx, r8.zzzz
    add r8.xyz, -r3.yzwy, r8.xyzx
    mul r8.xy, r8.xyxx, cb0[14].xyxx
    dp3 r2.x, r8.xyzx, r8.xyzx
    sqrt r2.x, r2.x
    ne r2.w, l(0.000000, 0.000000, 0.000000, 0.000000), r2.x
    movc r2.x, r2.w, r2.x, l(1.000000)
    mul r8.xyz, r1.xyzx, r2.xxxx
    exp r8.xyz, r8.xyzx
    div r2.x, l(1.000000, 1.000000, 1.000000, 1.000000), r2.x
    add_sat r9.w, -r0.w, r2.x
    mul r8.xyz, r8.xyzx, r9.wwww
    mul r9.xyz, r5.xyzx, r8.xyzx
    add r8.xyzw, r9.xyzw, r7.xyzw
    dp2 r2.x, cb0[r1.w + 5].zwzz, r0.xyxx
    mul r2.xw, r0.xxxy, r2.xxxx
    mad r2.xw, -r2.xxxw, l(2.000000, 0.000000, 0.000000, 2.000000), cb0[r1.w + 5].zzzw
    mul r2.xw, r2.xxxw, cb0[14].zzzw
    mul r6.yz, r2.zzyz, r2.xxwx
    add r9.yzw, r3.yyzw, r6.yyzw
    div r2.xw, r9.yyyz, r9.wwww
    mad r4.zw, r2.xxxw, cb2[0].xxxy, cb2[0].wwwz
    sample_l_indexable(texture2d)(float,float,float,float) r5.xyz, r4.zwzz, t2.xyzw, s2, l(0.000000)
    if_nz r0.z
      add r3.x, -r4.y, r9.w
      mad r9.x, r4.x, r3.x, r9.y
      mov r6.xyz, r9.xzwx
    endif 
    movc r6.xyz, r0.zzzz, r6.xyzx, r9.yzwy
    div r4.zw, r6.xxxy, r6.zzzz
    mad r4.zw, r4.zzzw, cb2[0].xxxy, cb2[0].wwwz
    sample_l_indexable(texture2d)(float,float,float,float) r3.x, r4.zwzz, t0.xyzw, s0, l(0.000000)
    min r3.x, r3.x, l(0.999000)
    mad r3.x, r3.x, cb2[1].z, -cb2[1].w
    div r6.z, l(1.000000, 1.000000, 1.000000, 1.000000), r3.x
    mul r6.xy, r2.xwxx, r6.zzzz
    add r6.xyz, -r3.yzwy, r6.xyzx
    mul r6.xy, r6.xyxx, cb0[14].xyxx
    dp3 r2.x, r6.xyzx, r6.xyzx
    sqrt r2.x, r2.x
    ne r2.w, l(0.000000, 0.000000, 0.000000, 0.000000), r2.x
    movc r2.x, r2.w, r2.x, l(1.000000)
    mul r6.xyz, r1.xyzx, r2.xxxx
    exp r6.xyz, r6.xyzx
    div r2.x, l(1.000000, 1.000000, 1.000000, 1.000000), r2.x
    add_sat r9.w, -r0.w, r2.x
    mul r6.xyz, r6.xyzx, r9.wwww
    mul r9.xyz, r5.xyzx, r6.xyzx
    add r7.xyzw, r8.xyzw, r9.xyzw
    iadd r1.w, r1.w, l(1)
  endloop 
  max r0.x, r7.w, l(0.000100)
  div r0.xyz, r7.xyzx, r0.xxxx
else 
  mov r0.xyz, l(0,0,0,0)
endif 
max o0.xyz, r0.xyzx, l(0.000000, 0.000000, 0.000000, 0.000000)
mov o0.w, l(0)
ret 
// Approximately 118 instruction slots used

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