//DMC5 van mirror PS.
// ---- Created with 3Dmigoto v1.3.15-re2_workaround on Mon Mar 11 19:30:17 2019

cbuffer SceneInfo : register(b0)
{
  row_major float4x4 viewProjMat : packoffset(c0);
  row_major float3x4 transposeViewMat : packoffset(c4);
  row_major float3x4 transposeViewInvMat : packoffset(c7);
  float4 projElement[2] : packoffset(c10);
  float4 projInvElements[2] : packoffset(c12);
  row_major float4x4 viewProjInvMat : packoffset(c14);
  row_major float4x4 prevViewProjMat : packoffset(c18);
  float3 ZToLinear : packoffset(c22);
  float subdivisionLevel : packoffset(c22.w);
  float2 screenSize : packoffset(c23);
  float2 screenInverseSize : packoffset(c23.z);
  float2 cullingHelper : packoffset(c24);
  float cameraNearPlane : packoffset(c24.z);
  float cameraFarPlane : packoffset(c24.w);
  float4 viewFrustum[6] : packoffset(c25);
  float4 clipplane : packoffset(c31);
}

cbuffer GBufferType : register(b1)
{
  float gbufferTypeFlag : packoffset(c0);
  float gbufferTypeReserve0 : packoffset(c0.y);
  float gbufferTypeReserve1 : packoffset(c0.z);
  float gbufferTypeReserve2 : packoffset(c0.w);
}

cbuffer UserMaterial : register(b2)
{
  float4 VAR_BaseColor : packoffset(c0);
  float VAR_MirrorIntensity : packoffset(c1);
  float VAR_MirrorRefractByNormal : packoffset(c1.y);
  float VAR_MirrorAttenFromRoughness : packoffset(c1.z);
  float VAR_Metallic : packoffset(c1.w);
  float VAR_Roughness : packoffset(c2);
  float VAR_Translucency : packoffset(c2.y);
  float VAR_Occlusion : packoffset(c2.z);
  float VAR_AlphaValue : packoffset(c2.w);
}

SamplerState AutomaticWrap_s : register(s0);
Texture2D<float4> Mirror_RenderTarget : register(t0);
Texture2D<float4> BaseMetalMap : register(t1);
Texture2D<float4> NormalRoughnessMap : register(t2);


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

void main(
  float4 v0 : SV_POSITION0,
  float4 v1 : INTERPOLATOR0,
  float4 v2 : INTERPOLATOR1,
  float4 v3 : INTERPOLATOR2,
  float3 v4 : INTERPOLATOR3,
  out float4 o0 : SV_TARGET0,
  out float4 o1 : SV_TARGET1,
  out float4 o2 : SV_TARGET2,
  out float4 o3 : SV_TARGET3)
{
  float4 r0,r1,r2,r3,r4,r5,r6;
  uint4 bitmask, uiDest;
  float4 fDest;

  float4 stereo = StereoParams.Load(0);
  
  r0.x = dot(v1.xyz, v1.xyz);
  r0.x = rsqrt(r0.x);
  r0.xyz = v1.xzy * r0.xxx;
  r0.w = dot(v2.yzw, v2.yzw);
  r0.w = rsqrt(r0.w);
  r1.xyz = v2.ywz * r0.www;
  r2.xyz = r1.zxy * r0.yzx;
  r2.xyz = r0.zxy * r1.yzx + -r2.xyz;
  r0.w = cmp(v3.x < 0);
  r2.xyz = r0.www ? -r2.xyz : r2.xyz;
  r3.yz = screenInverseSize.xy * v0.xy;
  r4.xy = r3.yz * float2(2,-2) + float2(-1,1);
  r5.x = -transposeViewInvMat._m03;
  r5.y = -transposeViewInvMat._m13;
  r5.z = -transposeViewInvMat._m23;
  r5.xyz = v3.yzw + r5.xyz;
  r0.w = dot(r5.xyz, r5.xyz);
  r0.w = rsqrt(r0.w);
  r5.xyz = r5.xyz * r0.www;
  
  v4.x-=stereo.x*stereo.y;
  
  r4.zw = v4.xy / v4.zz;
  o3.yz = r4.zw + -r4.xy;
  r4.x = v1.w;
  r4.y = v2.x;
  r6.xyzw = NormalRoughnessMap.Sample(AutomaticWrap_s, r4.xy).xyzw;
  r6.xyz = r6.xyz * float3(2,2,2) + float3(-1,-1,-1);
  r0.w = dot(r2.xyz, r2.xyz);
  r0.w = rsqrt(r0.w);
  r2.xyz = r2.xyz * r0.www;
  r2.xyz = r6.yyy * r2.xyz;
  r1.xyz = r6.xxx * r1.xyz + r2.xyz;
  r0.xyz = r6.zzz * r0.xyz + r1.xyz;
  r0.w = dot(r0.xyz, r0.xyz);
  r0.w = rsqrt(r0.w);
  r0.xyz = r0.xyz * r0.www;
  r1.xyzw = BaseMetalMap.Sample(AutomaticWrap_s, r4.xy).xyzw;
  r0.w = dot(-r5.xzy, r0.xyz);
  r2.x = 0.100000001 * VAR_MirrorRefractByNormal;
  r1.w = VAR_Metallic * r1.w;
  r0.w = 1 + -r0.w;
  r4.y = r0.w * r2.x;
  r3.x = r0.w * r2.x + -r3.y;
  r0.w = 1 + -VAR_MirrorAttenFromRoughness;
  r1.w = saturate(r1.w * 1.01999998 + -0.0199999996);
  r4.x = 1;
  r2.xy = r4.xy + r3.xz;
  
  float2 pos = r2.xy;
  pos.x = pos.x / 2;
  if (stereo.z == -1)
    pos.x += 0.5;
  //r2.xyz = stereo2mono.Sample(AutomaticWrap_s, pos).xyz;
  
  r2.xyz = Mirror_RenderTarget.Sample(AutomaticWrap_s, r2.xy).xyz;
  r2.w = VAR_MirrorAttenFromRoughness * r6.w;
  r3.xyz = saturate(r1.xyz * VAR_BaseColor.xyz + r2.www);
  r0.w = VAR_MirrorIntensity * r0.w + -VAR_MirrorIntensity;
  r0.w = r6.w * r0.w + VAR_MirrorIntensity;
  o0.xyz = r2.xyz * r0.www;
  o2.z = VAR_Roughness * r6.w;
  r0.w = cmp(0 < r1.w);
  r1.x = cmp(0 >= VAR_Translucency);
  r0.w = (int)r0.w | (int)r1.x;
  if (r0.w != 0) {
    r3.w = max(0.0399999991, r1.w);
    r0.w = 0.666666985;
  } else {
    r1.x = VAR_Translucency * 15.4899998 + 0.5;
    r1.x = floor(r1.x);
    r3.w = r1.x * 0.0627451017 + 0.0313725509;
    r0.w = 0;
  }
  r1.x = abs(r0.x) + abs(r0.y);
  r1.x = r1.x + abs(r0.z);
  r1.x = 1 / r1.x;
  r0.xy = r1.xx * r0.xy;
  r0.z = cmp(0 >= r0.z);
  r1.xy = float2(1,1) + -abs(r0.yx);
  r1.zw = cmp(r0.xy >= float2(0,0));
  r1.xy = r1.zw ? r1.xy : -r1.xy;
  r0.xy = r0.zz ? r1.xy : r0.xy;
  o2.xy = r0.xy * float2(0.5,0.5) + float2(0.5,0.5);
  o2.w = gbufferTypeFlag * 0.333333343 + r0.w;
  o0.w = 0;
  o1.xyzw = r3.xyzw;
  o3.x = VAR_Occlusion;
  o3.w = 1;
  return;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Generated by Microsoft (R) HLSL Shader Compiler 10.1
//
//   using 3Dmigoto v1.3.15-re2_workaround on Mon Mar 11 19:30:17 2019
//
//
// Buffer Definitions:
//
// cbuffer SceneInfo
// {
//
//   row_major float4x4 viewProjMat;    // Offset:    0 Size:    64 [unused]
//   row_major float3x4 transposeViewMat;// Offset:   64 Size:    48 [unused]
//   row_major float3x4 transposeViewInvMat;// Offset:  112 Size:    48
//   float4 projElement[2];             // Offset:  160 Size:    32 [unused]
//   float4 projInvElements[2];         // Offset:  192 Size:    32 [unused]
//   row_major float4x4 viewProjInvMat; // Offset:  224 Size:    64 [unused]
//   row_major float4x4 prevViewProjMat;// Offset:  288 Size:    64 [unused]
//   float3 ZToLinear;                  // Offset:  352 Size:    12 [unused]
//   float subdivisionLevel;            // Offset:  364 Size:     4 [unused]
//   float2 screenSize;                 // Offset:  368 Size:     8 [unused]
//   float2 screenInverseSize;          // Offset:  376 Size:     8
//   float2 cullingHelper;              // Offset:  384 Size:     8 [unused]
//   float cameraNearPlane;             // Offset:  392 Size:     4 [unused]
//   float cameraFarPlane;              // Offset:  396 Size:     4 [unused]
//   float4 viewFrustum[6];             // Offset:  400 Size:    96 [unused]
//   float4 clipplane;                  // Offset:  496 Size:    16 [unused]
//
// }
//
// cbuffer GBufferType
// {
//
//   float gbufferTypeFlag;             // Offset:    0 Size:     4
//   float gbufferTypeReserve0;         // Offset:    4 Size:     4 [unused]
//   float gbufferTypeReserve1;         // Offset:    8 Size:     4 [unused]
//   float gbufferTypeReserve2;         // Offset:   12 Size:     4 [unused]
//
// }
//
// cbuffer UserMaterial
// {
//
//   float4 VAR_BaseColor;              // Offset:    0 Size:    16
//   float VAR_MirrorIntensity;         // Offset:   16 Size:     4
//   float VAR_MirrorRefractByNormal;   // Offset:   20 Size:     4
//   float VAR_MirrorAttenFromRoughness;// Offset:   24 Size:     4
//   float VAR_Metallic;                // Offset:   28 Size:     4
//   float VAR_Roughness;               // Offset:   32 Size:     4
//   float VAR_Translucency;            // Offset:   36 Size:     4
//   float VAR_Occlusion;               // Offset:   40 Size:     4
//   float VAR_AlphaValue;              // Offset:   44 Size:     4 [unused]
//
// }
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// AutomaticWrap                     sampler      NA          NA    0        1
// Mirror_RenderTarget               texture  float4          2d    0        1
// BaseMetalMap                      texture  float4          2d    1        1
// NormalRoughnessMap                texture  float4          2d    2        1
// SceneInfo                         cbuffer      NA          NA    0        1
// GBufferType                       cbuffer      NA          NA    1        1
// UserMaterial                      cbuffer      NA          NA    2        1
//
//
//
// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_POSITION              0   xyzw        0      POS   float   xy
// INTERPOLATOR             0   xyzw        1     NONE   float   xyzw
// INTERPOLATOR             1   xyzw        2     NONE   float   xyzw
// INTERPOLATOR             2   xyzw        3     NONE   float   xyzw
// INTERPOLATOR             3   xyz         4     NONE   float   xyz
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// SV_TARGET                0   xyzw        0   TARGET   float   xyzw
// SV_TARGET                1   xyzw        1   TARGET   float   xyzw
// SV_TARGET                2   xyzw        2   TARGET   float   xyzw
// SV_TARGET                3   xyzw        3   TARGET   float   xyzw
//
ps_5_0
dcl_globalFlags refactoringAllowed
dcl_constantbuffer cb0[24], immediateIndexed
dcl_constantbuffer cb1[1], immediateIndexed
dcl_constantbuffer cb2[3], immediateIndexed
dcl_sampler s0, 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_input_ps_siv linear noperspective v0.xy, position
dcl_input_ps linear v1.xyzw
dcl_input_ps linear v2.xyzw
dcl_input_ps linear v3.xyzw
dcl_input_ps linear v4.xyz
dcl_output o0.xyzw
dcl_output o1.xyzw
dcl_output o2.xyzw
dcl_output o3.xyzw
dcl_temps 40
dcl_resource_texture2d (float,float,float,float) t125
ld_indexable(texture2d)(float,float,float,float) r25.xyzw, l(0, 0, 0, 0), t125.xyzw
dp3 r0.x, v1.xyzx, v1.xyzx
rsq r0.x, r0.x
mul r0.xyz, r0.xxxx, v1.xzyx
dp3 r0.w, v2.yzwy, v2.yzwy
rsq r0.w, r0.w
mul r1.xyz, r0.wwww, v2.ywzy
mul r2.xyz, r0.yzxy, r1.zxyz
mad r2.xyz, r0.zxyz, r1.yzxy, -r2.xyzx
lt r0.w, v3.x, l(0.000000)
movc r2.xyz, r0.wwww, -r2.xyzx, r2.xyzx
mul r3.yz, v0.xxyx, cb0[23].zzwz
mad r4.xy, r3.yzyy, l(2.000000, -2.000000, 0.000000, 0.000000), l(-1.000000, 1.000000, 0.000000, 0.000000)
mov r5.x, -cb0[7].w
mov r5.y, -cb0[8].w
mov r5.z, -cb0[9].w
add r5.xyz, r5.xyzx, v3.yzwy
dp3 r0.w, r5.xyzx, r5.xyzx
rsq r0.w, r0.w
mul r5.xyz, r0.wwww, r5.xyzx

//TAA fix.
mov r24.xyz, v4.xyzx
mad r24.x, -r25.x, r25.y, r24.x

div r4.zw, r24.xxxy, v4.zzzz
add o3.yz, -r4.xxyx, r4.zzwz
mov r4.x, v1.w
mov r4.y, v2.x
sample_indexable(texture2d)(float,float,float,float) r6.xyzw, r4.xyxx, t2.xyzw, s0
mad r6.xyz, r6.xyzx, l(2.000000, 2.000000, 2.000000, 0.000000), l(-1.000000, -1.000000, -1.000000, 0.000000)
dp3 r0.w, r2.xyzx, r2.xyzx
rsq r0.w, r0.w
mul r2.xyz, r0.wwww, r2.xyzx
mul r2.xyz, r2.xyzx, r6.yyyy
mad r1.xyz, r6.xxxx, r1.xyzx, r2.xyzx
mad r0.xyz, r6.zzzz, r0.xyzx, r1.xyzx
dp3 r0.w, r0.xyzx, r0.xyzx
rsq r0.w, r0.w
mul r0.xyz, r0.wwww, r0.xyzx
sample_indexable(texture2d)(float,float,float,float) r1.xyzw, r4.xyxx, t1.xyzw, s0
dp3 r0.w, -r5.xzyx, r0.xyzx
mul r2.x, cb2[1].y, l(0.100000)
mul r1.w, r1.w, cb2[1].w
add r0.w, -r0.w, l(1.000000)
mul r4.y, r2.x, r0.w
mad r3.x, r0.w, r2.x, -r3.y
add r0.w, -cb2[1].z, l(1.000000)
mad_sat r1.w, r1.w, l(1.020000), l(-0.020000)
mov r4.x, l(1.000000)
add r2.xy, r3.xzxx, r4.xyxx
sample_indexable(texture2d)(float,float,float,float) r2.xyz, r2.xyxx, t0.xyzw, s0
mul r2.w, r6.w, cb2[1].z
mad_sat r3.xyz, r1.xyzx, cb2[0].xyzx, r2.wwww
mad r0.w, cb2[1].x, r0.w, -cb2[1].x
mad r0.w, r6.w, r0.w, cb2[1].x
mul o0.xyz, r0.wwww, r2.xyzx
mul o2.z, r6.w, cb2[2].x
lt r0.w, l(0.000000), r1.w
ge r1.x, l(0.000000), cb2[2].y
or r0.w, r0.w, r1.x
if_nz r0.w
  max r3.w, r1.w, l(0.040000)
  mov r0.w, l(0.666667)
else
  mad r1.x, cb2[2].y, l(15.490000), l(0.500000)
  round_ni r1.x, r1.x
  mad r3.w, r1.x, l(0.0627451017), l(0.0313725509)
  mov r0.w, l(0)
endif
add r1.x, |r0.y|, |r0.x|
add r1.x, |r0.z|, r1.x
div r1.x, l(1.000000, 1.000000, 1.000000, 1.000000), r1.x
mul r0.xy, r0.xyxx, r1.xxxx
ge r0.z, l(0.000000), r0.z
add r1.xy, -|r0.yxyy|, l(1.000000, 1.000000, 0.000000, 0.000000)
ge r1.zw, r0.xxxy, l(0.000000, 0.000000, 0.000000, 0.000000)
movc r1.xy, r1.zwzz, r1.xyxx, -r1.xyxx
movc r0.xy, r0.zzzz, r1.xyxx, r0.xyxx
mad o2.xy, r0.xyxx, l(0.500000, 0.500000, 0.000000, 0.000000), l(0.500000, 0.500000, 0.000000, 0.000000)
mad o2.w, cb1[0].x, l(0.333333343), r0.w
mov o0.w, l(0)
mov o1.xyzw, r3.xyzw
mov o3.x, cb2[2].z
mov o3.w, l(1.000000)
ret
// Approximately 80 instruction slots used

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