//Nioh 2 vignette.
// ---- Created with 3Dmigoto v1.3.16 on Fri Feb 05 19:55:45 2021

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);
  float4 g_vMinUV : packoffset(c13);
}

cbuffer cbCompositeCustom : register(b5)
{
  float4 g_vColorScale : packoffset(c0);
  float4 g_vSaturationScale : packoffset(c1);
  float4 g_vHdrTvParams : packoffset(c2);
}

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);
Texture3D<float4> g_tSpecificShaft : register(t5);


// 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);
  float4 iniparams1 = IniParams.Load(int2(1,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;
  r2.xy = v1.xy * g_vCompositeLastViewport.zw + g_vCompositeLastViewport.xy;
  r3.xyzw = g_tSceneMap.SampleLevel(sampleLinear_s, r2.xy, 0).xyzw;
  r3.xyzw = min(float4(65024,65024,65024,65024), r3.xyzw);
  r0.w = cmp(0 < g_vEtcEffect.x);
  if (r0.w != 0 && iniparams1.z!=2) {
    r0.w = (uint)g_vEtcEffect.y;
    r1.xz = r2.xy * float2(2,2) + float2(-1,-1);
	
	r1.x-=stereo.x;
	
    r1.w = dot(r1.xz, r1.xz);
    r1.xz = r1.xz * r1.ww;
    r1.xz = g_vEtcEffect.xx * r1.xz;
    r4.xy = g_vSceneTexSize.xy * -r1.xz;
    r4.xy = float2(0.5,0.5) * r4.xy;
    r1.w = dot(r4.xy, r4.xy);
    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;
    r1.xz = -r1.xz / r2.ww;
    r4.xyz = icb[r0.w+0].xyz * r3.xyz;
    r5.xy = (int2)r0.ww + int2(1,2);
    r6.xyz = icb[r5.x+0].xyz + -icb[r0.w+0].xyz;
    r5.yzw = icb[r5.y+0].xyz + -icb[r5.x+0].xyz;
    r7.xyz = r4.xyz;
    r8.xyz = icb[r0.w+0].xyz;
    r9.xy = r2.xy;
    r4.w = 1;
    while (true) {
      r6.w = cmp((int)r4.w >= (int)r1.w);
      if (r6.w != 0) break;
      r9.xy = r9.xy + r1.xz;
      r10.xyz = g_tSceneMap.SampleLevel(sampleLinear_s, r9.xy, 0).xyz;
      r10.xyz = min(float3(65024,65024,65024), r10.xyz);
      r6.w = (int)r4.w;
      r6.w = r6.w / r2.w;
      r7.w = cmp(r6.w < 0.5);
      if (r7.w != 0) {
        r7.w = r6.w + r6.w;
        r11.xyz = r7.www * r6.xyz + icb[r0.w+0].xyz;
      } else {
        r6.w = r6.w * 2 + -1;
        r11.xyz = r6.www * r5.yzw + icb[r5.x+0].xyz;
      }
      r7.xyz = r10.xyz * r11.xyz + r7.xyz;
      r8.xyz = r11.xyz + r8.xyz;
      r4.w = (int)r4.w + 1;
    }
    r3.xyz = r7.xyz / r8.xyz;
  }
  r1.xzw = r3.xyz * r0.zzz;
  if (r0.x != 0 && iniparams1.z!=2) {
    r3.xyz = g_tSceneMap.SampleLevel(sampleLinear_s, g_vSun2dInfo.xy, 0).xyz;
    r3.xyz = min(float3(65024,65024,65024), r3.xyz);
    r0.xzw = r3.xyz * r0.zzz;
	
	//Sun and lens flares.
	r2.x-=stereo.x*0.5;
	
    r3.xyz = g_tLensFlareMap.SampleLevel(sampleLinear_s, r2.xy, 0).xyz;
    r3.xyz = min(float3(65024,65024,65024), r3.xyz);
    r2.w = dot(r0.xzw, float3(0.222014993,0.706655025,0.0713300034));
    r2.w = cmp(g_vEtcEffect.w < r2.w);
    r2.w = r2.w ? g_vEtcEffect.z : 0;
    r0.xzw = r3.xyz * r0.xzw;
    r1.xzw = r0.xzw * r2.www + r1.xzw;
  }
  r0.x = cmp(0 < g_vColorScale.w);
  if (r0.x != 0 && iniparams1.z!=2) {
    r2.z = 0;
    r0.xzw = g_tSpecificShaft.SampleLevel(sampleLinear_s, r2.xyz, 0).xyz;
    r0.xzw = min(float3(65024,65024,65024), r0.xzw);
    r1.xzw = r1.xzw + r0.xzw;
  }
  if (r0.y != 0 && iniparams1.z!=1 && iniparams1.z!=2) {
    r0.yz = float2(-0.5,-0.5) + r2.xy;
    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;
    r1.xzw = r1.xzw * r0.xxx;
  }
  r0.x = cmp(0 < g_vSaturationScale.w);
  if (r0.x != 0) {
    r0.xyz = r1.xzw * 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));
    r1.xzw = g_tHdrLut.SampleLevel(sampleLinear_s, r0.xyz, 0).xyz;
  }
  if (r1.y != 0) {
    r0.xyz = saturate(g_vHdrTvParams.xxx * r1.xzw);
    r0.xyz = log2(r0.xyz);
    r0.xyz = float3(0.454545468,0.454545468,0.454545468) * r0.xyz;
    r0.xyz = exp2(r0.xyz);
    r0.xyz = g_tLdrLut.SampleLevel(sampleLinear_s, r0.xyz, 0).xyz;
    r2.xyz = cmp(g_vHdrTvParams.yyy >= r1.xzw);
    r0.xyz = g_vHdrTvParams.yyy * r0.xyz;
    r0.xyz = r2.xyz ? r0.xyz : r1.xzw;
    r0.xyz = r0.xyz + -r1.xzw;
    r1.xzw = g_vCompositeInfo.yyy * r0.xyz + r1.xzw;
  }
  r0.xyz = g_vColorScale.xyz * r1.xzw;
  r0.x = dot(r0.xyz, float3(0.298909992,0.586610019,0.114480004));
  r0.yzw = r1.xzw * g_vColorScale.xyz + -r0.xxx;
  r0.xyz = g_vSaturationScale.xyz * r0.yzw + r0.xxx;
  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 = r3.w;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
//   using 3Dmigoto v1.3.16 on Fri Feb 05 19:55:45 2021
//
//
// 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]
//   float4 g_vMinUV;                   // Offset:  208 Size:    16 [unused]
//
// }
//
// cbuffer cbCompositeCustom
// {
//
//   float4 g_vColorScale;              // Offset:    0 Size:    16
//   float4 g_vSaturationScale;         // Offset:   16 Size:    16
//   float4 g_vHdrTvParams;             // Offset:   32 Size:    16
//
// }
//
//
// 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
// g_tSpecificShaft                  texture  float4          3d    5        1
// cbComposite                       cbuffer      NA          NA    2        1
// cbCompositeCustom                 cbuffer      NA          NA    5        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_constantbuffer cb5[3], 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_resource_texture3d (float,float,float,float) t5
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 r2.xy, v1.xyxx, cb2[11].zwzz, cb2[11].xyxx
sample_l_indexable(texture2d)(float,float,float,float) r3.xyzw, r2.xyxx, t0.xyzw, s7, l(0.000000)
min r3.xyzw, r3.xyzw, l(65024.000000, 65024.000000, 65024.000000, 65024.000000)
lt r0.w, l(0.000000), cb2[3].x
if_nz r0.w
  ftou r0.w, cb2[3].y
  mad r1.xz, r2.xxyx, l(2.000000, 0.000000, 2.000000, 0.000000), l(-1.000000, 0.000000, -1.000000, 0.000000)
  dp2 r1.w, r1.xzxx, r1.xzxx
  mul r1.xz, r1.wwww, r1.xxzx
  mul r1.xz, r1.xxzx, cb2[3].xxxx
  mul r4.xy, -r1.xzxx, cb2[0].xyxx
  mul r4.xy, r4.xyxx, l(0.500000, 0.500000, 0.000000, 0.000000)
  dp2 r1.w, r4.xyxx, r4.xyxx
  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 r1.xz, -r1.xxzx, r2.wwww
  mul r4.xyz, r3.xyzx, icb[r0.w + 0].xyzx
  iadd r5.xy, r0.wwww, l(1, 2, 0, 0)
  add r6.xyz, -icb[r0.w + 0].xyzx, icb[r5.x + 0].xyzx
  add r5.yzw, -icb[r5.x + 0].xxyz, icb[r5.y + 0].xxyz
  mov r7.xyz, r4.xyzx
  mov r8.xyz, icb[r0.w + 0].xyzx
  mov r9.xy, r2.xyxx
  mov r4.w, l(1)
  loop
    ige r6.w, r4.w, r1.w
    breakc_nz r6.w
    add r9.xy, r1.xzxx, 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 r6.w, r4.w
    div r6.w, r6.w, r2.w
    lt r7.w, r6.w, l(0.500000)
    if_nz r7.w
      add r7.w, r6.w, r6.w
      mad r11.xyz, r7.wwww, r6.xyzx, icb[r0.w + 0].xyzx
    else
      mad r6.w, r6.w, l(2.000000), l(-1.000000)
      mad r11.xyz, r6.wwww, r5.yzwy, icb[r5.x + 0].xyzx
    endif
    mad r7.xyz, r10.xyzx, r11.xyzx, r7.xyzx
    add r8.xyz, r8.xyzx, r11.xyzx
    iadd r4.w, r4.w, l(1)
  endloop
  div r3.xyz, r7.xyzx, r8.xyzx
endif
mul r1.xzw, r0.zzzz, r3.xxyz
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, r2.xyxx, t1.xyzw, s7, l(0.000000)
  min r3.xyz, r3.xyzx, l(65024.000000, 65024.000000, 65024.000000, 0.000000)
  dp3 r2.w, r0.xzwx, l(0.222015, 0.706655, 0.071330, 0.000000)
  lt r2.w, cb2[3].w, r2.w
  and r2.w, r2.w, cb2[3].z
  mul r0.xzw, r0.xxzw, r3.xxyz
  mad r1.xzw, r0.xxzw, r2.wwww, r1.xxzw
endif
lt r0.x, l(0.000000), cb5[0].w
if_nz r0.x
  mov r2.z, l(0)
  sample_l_indexable(texture3d)(float,float,float,float) r0.xzw, r2.xyzx, t5.xwyz, s7, l(0.000000)
  min r0.xzw, r0.xxzw, l(65024.000000, 0.000000, 65024.000000, 65024.000000)
  add r1.xzw, r0.xxzw, r1.xxzw
endif
if_nz r0.y
  add r0.yz, r2.xxyx, 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 r1.xzw, r0.xxxx, r1.xxzw
endif
lt r0.x, l(0.000000), cb5[1].w
if_nz r0.x
  mad r0.xyz, r1.xzwx, 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) r1.xzw, r0.xyzx, t3.xwyz, s7, l(0.000000)
endif
if_nz r1.y
  mul_sat r0.xyz, r1.xzwx, cb5[2].xxxx
  log r0.xyz, r0.xyzx
  mul r0.xyz, r0.xyzx, l(0.454545468, 0.454545468, 0.454545468, 0.000000)
  exp r0.xyz, r0.xyzx
  sample_l_indexable(texture3d)(float,float,float,float) r0.xyz, r0.xyzx, t4.xyzw, s7, l(0.000000)
  ge r2.xyz, cb5[2].yyyy, r1.xzwx
  mul r0.xyz, r0.xyzx, cb5[2].yyyy
  movc r0.xyz, r2.xyzx, r0.xyzx, r1.xzwx
  add r0.xyz, -r1.xzwx, r0.xyzx
  mad r1.xzw, cb2[1].yyyy, r0.xxyz, r1.xxzw
endif
mul r0.xyz, r1.xzwx, cb5[0].xyzx
dp3 r0.x, r0.xyzx, l(0.298910, 0.586610, 0.114480, 0.000000)
mad r0.yzw, r1.xxzw, cb5[0].xxyz, -r0.xxxx
mad r0.xyz, cb5[1].xyzx, r0.yzwy, r0.xxxx
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, r3.w
ret
// Approximately 126 instruction slots used

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