// Hands (forward pass velocity buffer glitch - AO smearing)
// ---- Created with 3Dmigoto v1.2.32 on Sat Mar 05 23:16:43 2016

cbuffer CViewportShaderParameterProvider : register(b0)
{
  float4x4 InvProjectionMatrix : packoffset(c0);
  float4x4 InvViewMatrix : packoffset(c4);
  float4x4 ProjectionMatrix : packoffset(c8);
  float4x4 ViewMatrix : packoffset(c12);
  float4x4 ViewProjectionMatrix : packoffset(c16);
  float4x4 ViewRotProjectionMatrix : packoffset(c20);
  float4x4 ViewRotProjectionMatrix_Previous : packoffset(c24);
  float4 AmbientSHR : packoffset(c28);
  float4 AmbientSHG : packoffset(c29);
  float4 AmbientSHB : packoffset(c30);
  float4 CameraDistances : packoffset(c31);
  float4 CameraNearPlaneSize : packoffset(c32);
  float4 DepthScale : packoffset(c33);
  float4 DepthTextureScaleOffset : packoffset(c34);
  float4 FogParams : packoffset(c35);
  float4 FogSHB : packoffset(c36);
  float4 FogSHG : packoffset(c37);
  float4 FogSHR : packoffset(c38);
  float4 HeightFogParams : packoffset(c39);
  float4 ViewportSize : packoffset(c40);
  float3 ViewPoint : packoffset(c41);
  float3 CameraPosition : packoffset(c42);
  float3 CameraPosition_Previous : packoffset(c43);
  float3 CameraPositionFractions : packoffset(c44);
  float3 CameraPositionFractions_Previous : packoffset(c45);
  float3 CameraRight : packoffset(c46);
  float3 CameraUp : packoffset(c47);
  float3 CameraDirection : packoffset(c48);
  float3 EnvironmentMapColorOffset : packoffset(c49);
  float3 EnvironmentMapColorScale : packoffset(c50);
  float3 AmbientOcclusionRanges : packoffset(c51);
  float3 DepthTextureRcpSize : packoffset(c52);
  float2 MotionVectorOffsets : packoffset(c53);
  float2 DepthRangeCompression : packoffset(c53.z);
  float ShadowProjDepthMinValue : packoffset(c54);
  float DistanceScale : packoffset(c54.y);
  float WorldSpaceZOffset : packoffset(c54.z);
  float WorldSpaceZOffset_Previous : packoffset(c54.w);
  float CameraFOVDeg : packoffset(c55);
  float EffectsEmissiveEVBias : packoffset(c55.y);
  float ShadowCSMLastSliceIndex : packoffset(c55.z);
  float UseNormalization : packoffset(c55.w);
  float FogUndergroundColorScale : packoffset(c56);
}

cbuffer Skin_Cinematic : register(b1)
{
  float WrinkleWeightContract[32] : packoffset(c0);
  float2 DetailDiffuseTiling : packoffset(c31.y);
  bool AllowDistanceBoost : packoffset(c31.w);
  float WrinkleWeightExtend[32] : packoffset(c32);
  float2 DetailNormalTiling : packoffset(c63.y);
  float DetailDiffuseIntensity : packoffset(c63.w);
  float4 MaterialPickingID : packoffset(c64);
  float4 SkinColor : packoffset(c65);
  float4 TanColor : packoffset(c66);
  bool DetailDiffuseLerp : packoffset(c67);
  float DetailNormalIntensity : packoffset(c67.y);
  float DetailSpecularIntensity : packoffset(c67.z);
  float DiffuseNormalIntensity : packoffset(c67.w);
  float NormalIntensity : packoffset(c68);
  float RoughnessFactor : packoffset(c68.y);
  float SpecularReflectance : packoffset(c68.z);
  float TranslucencyIntensity : packoffset(c68.w);
  float wrinkle_weight_normal0_mask0_a : packoffset(c69);
  float wrinkle_weight_normal0_mask0_b : packoffset(c69.y);
  float wrinkle_weight_normal0_mask0_g : packoffset(c69.z);
  float wrinkle_weight_normal0_mask0_r : packoffset(c69.w);
  float wrinkle_weight_normal0_mask1_a : packoffset(c70);
  float wrinkle_weight_normal0_mask1_b : packoffset(c70.y);
  float wrinkle_weight_normal0_mask1_g : packoffset(c70.z);
  float wrinkle_weight_normal0_mask1_r : packoffset(c70.w);
  float wrinkle_weight_normal1_mask0_a : packoffset(c71);
  float wrinkle_weight_normal1_mask0_b : packoffset(c71.y);
  float wrinkle_weight_normal1_mask0_g : packoffset(c71.z);
  float wrinkle_weight_normal1_mask0_r : packoffset(c71.w);
  float wrinkle_weight_normal1_mask1_a : packoffset(c72);
  float wrinkle_weight_normal1_mask1_b : packoffset(c72.y);
  float wrinkle_weight_normal1_mask1_g : packoffset(c72.z);
  float wrinkle_weight_normal1_mask1_r : packoffset(c72.w);
}

SamplerState DiffuseTexture__SampObj___s : register(s0);
SamplerState NormalTexture__SampObj___s : register(s1);
SamplerState DetailDiffuseTexture__SampObj___s : register(s2);
SamplerState LargeSpecularTexture__SampObj___s : register(s3);
SamplerState DetailSpecularTexture__SampObj___s : register(s4);
SamplerState DetailNormalTexture__SampObj___s : register(s5);
Texture2D<float4> DiffuseTexture__TexObj__ : register(t0);
Texture2D<float4> NormalTexture__TexObj__ : register(t1);
Texture2D<float4> DetailDiffuseTexture__TexObj__ : register(t2);
Texture2D<float4> LargeSpecularTexture__TexObj__ : register(t3);
Texture2D<float4> DetailSpecularTexture__TexObj__ : register(t4);
Texture2D<float4> DetailNormalTexture__TexObj__ : register(t5);


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


void main( 
  float4 v0 : SV_Position0,
  float v1 : SV_ClipDistance0,
  linear centroid float4 v2 : TEXCOORD0,
  linear centroid float4 v3 : TEXCOORD1,
  float4 v4 : TEXCOORD2,
  float4 v5 : TEXCOORD3,
  float4 v6 : TEXCOORD4,
  float4 v7 : TEXCOORD5,
  float3 v8 : TEXCOORD6,
  uint v9 : SV_IsFrontFace0,
  out float4 o0 : SV_Target0,
  out float4 o1 : SV_Target1,
  out float4 o2 : SV_Target2,
  out float4 o3 : SV_Target3,
  out float4 o4 : SV_Target4)
{
  float4 r0,r1,r2,r3;
  uint4 bitmask, uiDest;
  float4 fDest;

  r0.xy = (int2)v0.xy;
  r0.xy = (int2)r0.xy & int2(1,1);
  r0.x = cmp((int)r0.x == (int)r0.y);
  r0.yzw = -TanColor.xyz + SkinColor.xyz;
  r0.yzw = v3.yyy * r0.yzw + TanColor.xyz;
  r1.xy = DetailDiffuseTiling.xy * v2.xy;
  r1.xyz = DetailDiffuseTexture__TexObj__.Sample(DetailDiffuseTexture__SampObj___s, r1.xy).xyz;
  r2.xyz = DiffuseTexture__TexObj__.Sample(DiffuseTexture__SampObj___s, v2.xy).xyz;
  r3.xyz = -r2.xyz * r0.yzw + r1.xyz;
  r0.yzw = r2.xyz * r0.yzw;
  r1.xyz = float3(-1,-1,-1) + r1.xyz;
  r2.xyz = LargeSpecularTexture__TexObj__.Sample(LargeSpecularTexture__SampObj___s, v2.xy).xyz;
  r1.w = DetailDiffuseIntensity * r2.z;
  r3.xyz = r1.www * r3.xyz + r0.yzw;
  r1.xyz = r1.www * r1.xyz + float3(1,1,1);
  r0.yzw = r1.xyz * r0.yzw;
  r0.yzw = DetailDiffuseLerp ? r3.xyz : r0.yzw;
  r0.yzw = sqrt(r0.yzw);
  r1.x = dot(float3(0.25,0.5,0.25), r0.yzw);
  r1.y = dot(float2(0.5,-0.5), r0.yw);
  r1.z = dot(float3(-0.25,0.5,-0.25), r0.yzw);
  r0.yzw = float3(0,0.498039216,0.498039216) + r1.xyz;
  o0.y = r0.x ? r0.z : r0.w;
  o0.x = r0.y;
  o0.w = AllowDistanceBoost ? 0.0470588244 : 0.0156862754;
  r0.xyz = NormalTexture__TexObj__.Sample(NormalTexture__SampObj___s, v2.xy).xyw;
  r0.w = r2.y + -r0.x;
  r0.x = r1.w * r0.w + r0.x;
  r0.yz = r0.zy * float2(2.00787401,2.00787401) + float2(-1,-1);
  r1.xy = DetailNormalTiling.xy * v2.xy;
  r2.yzw = DetailNormalTexture__TexObj__.Sample(DetailNormalTexture__SampObj___s, r1.xy).xyw;
  r0.w = DetailSpecularTexture__TexObj__.Sample(DetailSpecularTexture__SampObj___s, r1.xy).y;
  r0.w = -1 + r0.w;
  r1.x = -1 + r2.y;
  r1.yz = r2.wz * float2(2.00787401,2.00787401) + float2(-1,-1);
  r1.w = DetailDiffuseLerp * r2.x;
  r1.x = r1.w * r1.x + 1;
  r2.yz = r1.yz * r1.ww;
  r0.x = r1.x * r0.x;
  o0.z = RoughnessFactor * r0.x;
  r0.x = DetailDiffuseLerp * DetailDiffuseLerp;
  r0.x = r0.x * r2.x;
  r1.x = v4.w * r2.x;
  r1.x = DetailDiffuseLerp * r1.x;
  r0.w = r1.x * r0.w + 1;
  o2.y = v3.z * r0.w;
  r0.xw = r1.yz * r0.xx;
  r1.x = dot(r0.xw, r0.xw);
  r1.x = min(1, r1.x);
  r1.x = -r1.x * 0.999000013 + 1;
  r1.y = NormalIntensity * DetailDiffuseLerp;
  r1.yz = r1.yy * r0.yz;
  r0.yz = NormalIntensity * r0.yz;
  r1.w = dot(r1.yz, r1.yz);
  r1.w = min(1, r1.w);
  r1.w = -r1.w * 0.999000013 + 1;
  r1.xw = sqrt(r1.xw);
  r0.xw = r1.ww * r0.xw;
  r3.z = r1.w * r1.x;
  r3.xy = r1.yz * r1.xx + r0.xw;
  r0.x = dot(r3.xyz, r3.xyz);
  r0.x = rsqrt(r0.x);
  r1.xyz = r3.xyz * r0.xxx;
  r3.xyz = v7.yxz * r1.yyy;
  r1.xyw = r1.xxx * v8.yxz + r3.xyz;
  r1.xyz = r1.zzz * v6.yxz + r1.xyw;
  r0.x = dot(r1.xyz, r1.xyz);
  r0.x = rsqrt(r0.x);
  r1.xyw = r1.xyz * r0.xxx;
  r0.x = r1.z * r0.x + 1;
  r3.xyz = r1.xyw * float3(1,-1,1) + float3(0,0,1);
  r0.w = dot(r3.xyz, r3.xyz);
  r0.w = rsqrt(r0.w);
  r3.x = r1.x * r0.w;
  r3.y = -r1.y * r0.w;
  r3.w = r0.x * r0.w;
  r0.x = cmp(abs(r3.y) >= abs(r3.x));
  r1.xy = cmp(r3.yx < float2(0,0));
  r0.w = r1.x ? 3 : 2;
  r1.x = r1.y ? 1.000000 : 0;
  r0.x = r0.x ? r0.w : r1.x;
  r0.x = 0.5 * r0.x;
  r0.x = (uint)r0.x;
  r1.xy = cmp(float2(-0,-0) >= abs(r3.xy));
  r0.w = r1.y ? r1.x : 0;
  r0.x = r0.w ? 2 : r0.x;
  r0.w = max(abs(r3.x), abs(r3.y));
  r0.w = cmp(r0.w < abs(r3.w));
  r0.x = r0.w ? 3 : r0.x;
  r1.xy = r0.xx ? r3.xw : r3.wx;
  r2.xw = cmp((int2)r0.xx == int2(1,2));
  r0.x = (uint)r0.x;
  o1.w = 0.333333343 * r0.x;
  r0.x = r2.x ? r3.y : r1.y;
  r1.z = r2.x ? r3.w : r3.y;
  r1.w = r2.w ? r3.w : 0;
  r0.x = r2.w ? 0 : r0.x;
  r0.w = cmp(0 < r0.x);
  r0.x = cmp(r0.x < 0);
  r0.x = (int)r0.x + (int)-r0.w;
  r0.x = (int)r0.x;
  r1.xyz = r1.xzw * r0.xxx;
  o1.xyz = r1.xyz * float3(0.707106769,0.707106769,0.707106769) + float3(0.5,0.5,0.5);
  r0.x = 3.75 * SpecularReflectance;
  r0.x = (uint)r0.x;
  r0.x = (uint)r0.x;
  o2.x = 0.00392156886 * r0.x;
  o2.zw = float2(1,0);
  r0.x = dot(r0.yz, r0.yz);
  r0.x = min(1, r0.x);
  r0.x = -r0.x * 0.999000013 + 1;
  r0.x = sqrt(r0.x);
  r1.xy = r2.yz * r0.xx;
  r0.w = dot(r2.yz, r2.yz);
  r0.w = min(1, r0.w);
  r0.w = -r0.w * 0.999000013 + 1;
  r0.w = sqrt(r0.w);
  r1.xy = r0.yz * r0.ww + r1.xy;
  r1.z = r0.x * r0.w;
  r0.x = dot(r1.xyz, r1.xyz);
  r0.x = rsqrt(r0.x);
  r0.xyz = r1.xyz * r0.xxx;
  r1.xyz = v7.xyz * r0.yyy;
  r0.xyw = r0.xxx * v8.xyz + r1.xyz;
  r0.xyz = r0.zzz * v6.xyz + r0.xyw;
  r0.w = dot(r0.xyz, r0.xyz);
  r0.w = rsqrt(r0.w);
  r0.xyz = r0.xyz * r0.www;
  o3.yzw = r0.xyz * float3(0.5,0.5,0.5) + float3(0.5,0.5,0.5);
  r0.x = TranslucencyIntensity * v3.x;
  o3.x = v4.w * r0.x;

// Use the 2D position for the velocity buffer to avoid AO getting smeared on
// these surfaces (and possibly other glitches):
float4 stereo = StereoParams.Load(0);
float4 velocity_v0 = v0;
velocity_v0.x -= stereo.x * (v5.z - stereo.y) * ViewportSize.x;

  r0.xy = -MotionVectorOffsets.xy + velocity_v0.xy;
  r0.xy = r0.xy * ViewportSize.zw + float2(-0.5,-0.5);
  r0.z = rcp(v5.z);
  r0.zw = v5.xy * r0.zz;
  o4.xy = r0.zw * float2(0.5,-0.5) + -r0.xy;
  o4.zw = float2(0,0);
  return;
}
