//P5S post processing including lens flares.
// ---- Created with 3Dmigoto v1.3.16 on Thu Mar 03 00:17:45 2022

cbuffer cbComposite : register(b2)
{
  float4 g_vSceneTexSize : packoffset(c0);
  float4 g_vCompositeInfo : packoffset(c1);
  float4 g_vSun2dInfo : packoffset(c2);
  float4 g_vEtcEffect : packoffset(c3);
  float4 g_vBloomInfo : packoffset(c4);
  float4 g_vLimbDarkenningInfo : packoffset(c5);
  float4 g_vFxaaParams : packoffset(c6);
  float4 g_vGammaCorrection : packoffset(c7);
  float4 g_vRadialBlurCenter : packoffset(c8);
  float4 g_vRadialBlurInfo : packoffset(c9);
  float4 g_vFxaaQualityParams : packoffset(c10);
  float4 g_vCompositeLastViewport : packoffset(c11);
  float4 g_vMaxUV : packoffset(c12);
}

SamplerState sampleLinear_s : register(s7);
Texture2D<float4> g_tSceneMap : register(t0);
Texture2D<float4> g_tLensFlareMap : register(t1);
Texture2D<float4> g_tExposureScaleInfo : register(t2);
Texture3D<float4> g_tHdrLut : register(t3);
Texture3D<float4> g_tLdrLut : register(t4);


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


void main(
  float4 v0 : SV_Position0,
  float2 v1 : TEXCOORD0,
  out float4 o0 : SV_Target0)
{
  const float4 icb[] = { { 0, 0, 1.000000, 0},
                              { 0, 1.000000, 0, 0},
                              { 1.000000, 0, 0, 0},
                              { 1.000000, 0, 0, 0},
                              { 0, 1.000000, 0, 0},
                              { 0, 0, 1.000000, 0},
                              { 0, 1.000000, 0, 0},
                              { 1.000000, 0, 0, 0},
                              { 1.000000, 0, 1.000000, 0},
                              { 1.000000, 0, 1.000000, 0},
                              { 1.000000, 0, 0, 0},
                              { 0, 1.000000, 0, 0} };
  float4 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11;
  uint4 bitmask, uiDest;
  float4 fDest;
  
  float4 stereo = StereoParams.Load(0);
  
  r0.x = cmp(0 < g_vSun2dInfo.z);
  r0.y = cmp(0 < g_vLimbDarkenningInfo.w);
  r0.z = cmp(g_vCompositeInfo.z < 0);
  r0.w = g_tExposureScaleInfo.Load(float4(0,0,0,0)).x;
  r1.xy = cmp(float2(0,0) < g_vCompositeInfo.zy);
  r1.x = r1.x ? g_vCompositeInfo.z : 1;
  r0.z = r0.z ? r0.w : r1.x;
  r1.xz = v1.xy * g_vCompositeLastViewport.zw + g_vCompositeLastViewport.xy;
  r2.xyz = g_tSceneMap.SampleLevel(sampleLinear_s, r1.xz, 0).xyz;
  r2.xyz = min(float3(65024,65024,65024), r2.xyz);
  r0.w = cmp(0 < g_vEtcEffect.x);
  if (r0.w != 0) {
    r0.w = (uint)g_vEtcEffect.y;
    r3.xy = r1.xz * float2(2,2) + float2(-1,-1);
	
	r3.x-=stereo.x;
	
    r1.w = dot(r3.xy, r3.xy);
    r3.xy = r3.xy * r1.ww;
    r3.xy = g_vEtcEffect.xx * r3.xy;
    r3.zw = g_vSceneTexSize.xy * -r3.xy;
    r3.zw = float2(0.5,0.5) * r3.zw;
    r1.w = dot(r3.zw, r3.zw);
    r1.w = sqrt(r1.w);
    r1.w = (int)r1.w;
    r1.w = max(3, (int)r1.w);
    r1.w = min(16, (int)r1.w);
    r2.w = (int)r1.w;
    r3.xy = -r3.xy / r2.ww;
    r4.xyz = icb[r0.w+0].xyz * r2.xyz;
    r3.zw = (int2)r0.ww + int2(1,2);
    r5.xyz = icb[r3.z+0].xyz + -icb[r0.w+0].xyz;
    r6.xyz = icb[r3.w+0].xyz + -icb[r3.z+0].xyz;
    r7.xyz = r4.xyz;
    r8.xyz = icb[r0.w+0].xyz;
    r9.xy = r1.xz;
    r3.w = 1;
    while (true) {
      r4.w = cmp((int)r3.w >= (int)r1.w);
      if (r4.w != 0) break;
      r9.xy = r9.xy + r3.xy;
      r10.xyz = g_tSceneMap.SampleLevel(sampleLinear_s, r9.xy, 0).xyz;
      r10.xyz = min(float3(65024,65024,65024), r10.xyz);
      r4.w = (int)r3.w;
      r4.w = r4.w / r2.w;
      r5.w = cmp(r4.w < 0.5);
      if (r5.w != 0) {
        r5.w = r4.w + r4.w;
        r11.xyz = r5.www * r5.xyz + icb[r0.w+0].xyz;
      } else {
        r4.w = r4.w * 2 + -1;
        r11.xyz = r4.www * r6.xyz + icb[r3.z+0].xyz;
      }
      r7.xyz = r10.xyz * r11.xyz + r7.xyz;
      r8.xyz = r11.xyz + r8.xyz;
      r3.w = (int)r3.w + 1;
    }
    r2.xyz = r7.xyz / r8.xyz;
  }
  r2.xyz = r2.xyz * r0.zzz;
  if (r0.x != 0) {
    
	//Sun and lens flares clipping.
	r11.xy=g_vSun2dInfo.xy;
	r11.x+=stereo.x*0.5;
	
    r3.xyz = g_tSceneMap.SampleLevel(sampleLinear_s, r11.xy, 0).xyz;
    r3.xyz = min(float3(65024,65024,65024), r3.xyz);
    r0.xzw = r3.xyz * r0.zzz;
	
	//Sun and lens flares.
	r1.x-=stereo.x*0.5;
	
    r3.xyz = g_tLensFlareMap.SampleLevel(sampleLinear_s, r1.xz, 0).xyz;
    r3.xyz = min(float3(65024,65024,65024), r3.xyz);
    r1.w = dot(r0.xzw, float3(0.222014993,0.706655025,0.0713300034));
    r1.w = cmp(g_vEtcEffect.w < r1.w);
    r1.w = r1.w ? g_vEtcEffect.z : 0;
    r0.xzw = r3.xyz * r0.xzw;
    r2.xyz = r0.xzw * r1.www + r2.xyz;
  }
  if (r0.y != 0) {
    r0.yz = float2(-0.5,-0.5) + r1.xz;
    r0.x = g_vCompositeInfo.x * r0.y;
    r0.x = dot(r0.xz, r0.xz);
    r0.y = sqrt(r0.x);
    r0.y = -g_vLimbDarkenningInfo.y + r0.y;
    r0.z = cmp(0 < r0.y);
    r0.y = saturate(-r0.y * g_vLimbDarkenningInfo.z + 1);
    r0.y = r0.z ? r0.y : 1;
    r0.z = cmp(0 < r0.y);
    r0.x = g_vLimbDarkenningInfo.x + r0.x;
    r0.x = g_vLimbDarkenningInfo.x / r0.x;
    r0.x = r0.x * r0.x;
    r0.x = r0.z ? r0.x : 1;
    r0.x = r0.x * r0.y;
    r0.y = 1 + -g_vLimbDarkenningInfo.w;
    r0.x = r0.x * g_vLimbDarkenningInfo.w + r0.y;
    r2.xyz = r2.xyz * r0.xxx;
  }
  r0.xyz = r2.xyz * float3(5.55555582,5.55555582,5.55555582) + float3(0.0479959995,0.0479959995,0.0479959995);
  r0.xyz = log2(r0.xyz);
  r0.xyz = saturate(r0.xyz * float3(0.0734997839,0.0734997839,0.0734997839) + float3(0.386036009,0.386036009,0.386036009));
  r0.xyz = g_tHdrLut.SampleLevel(sampleLinear_s, r0.xyz, 0).xyz;
  if (r1.y != 0) {
    r1.xyz = saturate(r0.xyz);
    r1.xyz = log2(r1.xyz);
    r1.xyz = float3(0.454545468,0.454545468,0.454545468) * r1.xyz;
    r1.xyz = exp2(r1.xyz);
    r1.xyz = g_tLdrLut.SampleLevel(sampleLinear_s, r1.xyz, 0).xyz;
    r1.xyz = r1.xyz + -r0.xyz;
    r0.xyz = g_vCompositeInfo.yyy * r1.xyz + r0.xyz;
  }
  r0.w = cmp(g_vGammaCorrection.x != 1.000000);
  r1.xyz = log2(abs(r0.xyz));
  r1.xyz = g_vGammaCorrection.xxx * r1.xyz;
  r1.xyz = exp2(r1.xyz);
  r0.xyz = r0.www ? r1.xyz : r0.xyz;
  o0.xyz = g_vRadialBlurCenter.zzz * r0.xyz;
  o0.w = 1;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//   using 3Dmigoto v1.3.16 on Thu Mar 03 00:17:45 2022
//
//
// Buffer Definitions:
//
// cbuffer cbComposite
// {
//
//   float4 g_vSceneTexSize;            // Offset:    0 Size:    16
//   float4 g_vCompositeInfo;           // Offset:   16 Size:    16
//   float4 g_vSun2dInfo;               // Offset:   32 Size:    16
//   float4 g_vEtcEffect;               // Offset:   48 Size:    16
//   float4 g_vBloomInfo;               // Offset:   64 Size:    16 [unused]
//   float4 g_vLimbDarkenningInfo;      // Offset:   80 Size:    16
//   float4 g_vFxaaParams;              // Offset:   96 Size:    16 [unused]
//   float4 g_vGammaCorrection;         // Offset:  112 Size:    16
//   float4 g_vRadialBlurCenter;        // Offset:  128 Size:    16
//   float4 g_vRadialBlurInfo;          // Offset:  144 Size:    16 [unused]
//   float4 g_vFxaaQualityParams;       // Offset:  160 Size:    16 [unused]
//   float4 g_vCompositeLastViewport;   // Offset:  176 Size:    16
//   float4 g_vMaxUV;                   // Offset:  192 Size:    16 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// sampleLinear                      sampler      NA          NA    7        1
// g_tSceneMap                       texture  float4          2d    0        1
// g_tLensFlareMap                   texture  float4          2d    1        1
// g_tExposureScaleInfo              texture  float4          2d    2        1
// g_tHdrLut                         texture  float4          3d    3        1
// g_tLdrLut                         texture  float4          3d    4        1
// cbComposite                       cbuffer      NA          NA    2        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_Position              0   xyzw        0      POS   float
// TEXCOORD                 0   xy          1     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_immediateConstantBuffer { { 0, 0, 1.000000, 0},
                              { 0, 1.000000, 0, 0},
                              { 1.000000, 0, 0, 0},
                              { 1.000000, 0, 0, 0},
                              { 0, 1.000000, 0, 0},
                              { 0, 0, 1.000000, 0},
                              { 0, 1.000000, 0, 0},
                              { 1.000000, 0, 0, 0},
                              { 1.000000, 0, 1.000000, 0},
                              { 1.000000, 0, 1.000000, 0},
                              { 1.000000, 0, 0, 0},
                              { 0, 1.000000, 0, 0} }
dcl_constantbuffer cb2[12], immediateIndexed
dcl_sampler s7, 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_texture3d (float,float,float,float) t3
dcl_resource_texture3d (float,float,float,float) t4
dcl_input_ps linear v1.xy
dcl_output o0.xyzw
dcl_temps 12
lt r0.x, l(0.000000), cb2[2].z
lt r0.y, l(0.000000), cb2[5].w
lt r0.z, cb2[1].z, l(0.000000)
ld_indexable(texture2d)(float,float,float,float) r0.w, l(0, 0, 0, 0), t2.yzwx
lt r1.xy, l(0.000000, 0.000000, 0.000000, 0.000000), cb2[1].zyzz
movc r1.x, r1.x, cb2[1].z, l(1.000000)
movc r0.z, r0.z, r0.w, r1.x
mad r1.xz, v1.xxyx, cb2[11].zzwz, cb2[11].xxyx
sample_l_indexable(texture2d)(float,float,float,float) r2.xyz, r1.xzxx, t0.xyzw, s7, l(0.000000)
min r2.xyz, r2.xyzx, l(65024.000000, 65024.000000, 65024.000000, 0.000000)
lt r0.w, l(0.000000), cb2[3].x
if_nz r0.w
  ftou r0.w, cb2[3].y
  mad r3.xy, r1.xzxx, l(2.000000, 2.000000, 0.000000, 0.000000), l(-1.000000, -1.000000, 0.000000, 0.000000)
  dp2 r1.w, r3.xyxx, r3.xyxx
  mul r3.xy, r1.wwww, r3.xyxx
  mul r3.xy, r3.xyxx, cb2[3].xxxx
  mul r3.zw, -r3.xxxy, cb2[0].xxxy
  mul r3.zw, r3.zzzw, l(0.000000, 0.000000, 0.500000, 0.500000)
  dp2 r1.w, r3.zwzz, r3.zwzz
  sqrt r1.w, r1.w
  ftoi r1.w, r1.w
  imax r1.w, r1.w, l(3)
  imin r1.w, r1.w, l(16)
  itof r2.w, r1.w
  div r3.xy, -r3.xyxx, r2.wwww
  mul r4.xyz, r2.xyzx, icb[r0.w + 0].xyzx
  iadd r3.zw, r0.wwww, l(0, 0, 1, 2)
  add r5.xyz, -icb[r0.w + 0].xyzx, icb[r3.z + 0].xyzx
  add r6.xyz, -icb[r3.z + 0].xyzx, icb[r3.w + 0].xyzx
  mov r7.xyz, r4.xyzx
  mov r8.xyz, icb[r0.w + 0].xyzx
  mov r9.xy, r1.xzxx
  mov r3.w, l(1)
  loop
    ige r4.w, r3.w, r1.w
    breakc_nz r4.w
    add r9.xy, r3.xyxx, r9.xyxx
    sample_l_indexable(texture2d)(float,float,float,float) r10.xyz, r9.xyxx, t0.xyzw, s7, l(0.000000)
    min r10.xyz, r10.xyzx, l(65024.000000, 65024.000000, 65024.000000, 0.000000)
    itof r4.w, r3.w
    div r4.w, r4.w, r2.w
    lt r5.w, r4.w, l(0.500000)
    if_nz r5.w
      add r5.w, r4.w, r4.w
      mad r11.xyz, r5.wwww, r5.xyzx, icb[r0.w + 0].xyzx
    else
      mad r4.w, r4.w, l(2.000000), l(-1.000000)
      mad r11.xyz, r4.wwww, r6.xyzx, icb[r3.z + 0].xyzx
    endif
    mad r7.xyz, r10.xyzx, r11.xyzx, r7.xyzx
    add r8.xyz, r8.xyzx, r11.xyzx
    iadd r3.w, r3.w, l(1)
  endloop
  div r2.xyz, r7.xyzx, r8.xyzx
endif
mul r2.xyz, r0.zzzz, r2.xyzx
if_nz r0.x
  sample_l_indexable(texture2d)(float,float,float,float) r3.xyz, cb2[2].xyxx, t0.xyzw, s7, l(0.000000)
  min r3.xyz, r3.xyzx, l(65024.000000, 65024.000000, 65024.000000, 0.000000)
  mul r0.xzw, r0.zzzz, r3.xxyz
  sample_l_indexable(texture2d)(float,float,float,float) r3.xyz, r1.xzxx, t1.xyzw, s7, l(0.000000)
  min r3.xyz, r3.xyzx, l(65024.000000, 65024.000000, 65024.000000, 0.000000)
  dp3 r1.w, r0.xzwx, l(0.222015, 0.706655, 0.071330, 0.000000)
  lt r1.w, cb2[3].w, r1.w
  and r1.w, r1.w, cb2[3].z
  mul r0.xzw, r0.xxzw, r3.xxyz
  mad r2.xyz, r0.xzwx, r1.wwww, r2.xyzx
endif
if_nz r0.y
  add r0.yz, r1.xxzx, l(0.000000, -0.500000, -0.500000, 0.000000)
  mul r0.x, r0.y, cb2[1].x
  dp2 r0.x, r0.xzxx, r0.xzxx
  sqrt r0.y, r0.x
  add r0.y, r0.y, -cb2[5].y
  lt r0.z, l(0.000000), r0.y
  mad_sat r0.y, -r0.y, cb2[5].z, l(1.000000)
  movc r0.y, r0.z, r0.y, l(1.000000)
  lt r0.z, l(0.000000), r0.y
  add r0.x, r0.x, cb2[5].x
  div r0.x, cb2[5].x, r0.x
  mul r0.x, r0.x, r0.x
  movc r0.x, r0.z, r0.x, l(1.000000)
  mul r0.x, r0.y, r0.x
  add r0.y, -cb2[5].w, l(1.000000)
  mad r0.x, r0.x, cb2[5].w, r0.y
  mul r2.xyz, r0.xxxx, r2.xyzx
endif
mad r0.xyz, r2.xyzx, l(5.555556, 5.555556, 5.555556, 0.000000), l(0.047996, 0.047996, 0.047996, 0.000000)
log r0.xyz, r0.xyzx
mad_sat r0.xyz, r0.xyzx, l(0.0734997839, 0.0734997839, 0.0734997839, 0.000000), l(0.386036, 0.386036, 0.386036, 0.000000)
sample_l_indexable(texture3d)(float,float,float,float) r0.xyz, r0.xyzx, t3.xyzw, s7, l(0.000000)
if_nz r1.y
  mov_sat r1.xyz, r0.xyzx
  log r1.xyz, r1.xyzx
  mul r1.xyz, r1.xyzx, l(0.454545468, 0.454545468, 0.454545468, 0.000000)
  exp r1.xyz, r1.xyzx
  sample_l_indexable(texture3d)(float,float,float,float) r1.xyz, r1.xyzx, t4.xyzw, s7, l(0.000000)
  add r1.xyz, -r0.xyzx, r1.xyzx
  mad r0.xyz, cb2[1].yyyy, r1.xyzx, r0.xyzx
endif
ne r0.w, cb2[7].x, l(1.000000)
log r1.xyz, |r0.xyzx|
mul r1.xyz, r1.xyzx, cb2[7].xxxx
exp r1.xyz, r1.xyzx
movc r0.xyz, r0.wwww, r1.xyzx, r0.xyzx
mul o0.xyz, r0.xyzx, cb2[8].zzzz
mov o0.w, l(1.000000)
ret
// Approximately 109 instruction slots used

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