// hlsltool.py -I ../112 --auto-fix-vertex-halo --fix-unity-reflection --only-autofixed --no-validate --ignore-other-errors 663e4518b418ce24-vs_replace.txt
// 3DMigoto: 663e4518b418ce24 |    Unity headers extracted from SpeedTree.shader
//    Shader "Nature/SpeedTree" {
//      Properties {
//       _Color ("Main Color", Color) = (1,1,1,1)
//       _SpecColor ("Specular Color", Color) = (0,0,0,0)
//       _HueVariation ("Hue Variation", Color) = (1,0.5,0,0.1)
//       _Shininess ("Shininess", Range(0.01,1)) = 0.1
//       _MainTex ("Base (RGB) Trans (A)", 2D) = "white" { }
//       _DetailTex ("Detail", 2D) = "black" { }
//       _BumpMap ("Normal Map", 2D) = "bump" { }
//       _Cutoff ("Alpha Cutoff", Range(0,1)) = 0.333
//      [MaterialEnum(Off,0,Front,1,Back,2)]  _Cull ("Cull", Float) = 2
//      [MaterialEnum(None,0,Fastest,1,Fast,2,Better,3,Best,4,Palm,5)]  _WindQuality ("Wind Quality", Range(0,5)) = 0
//      }
//      Fallback "Transparent/Cutout/VertexLit"
//      SubShader 1/2 {
//        LOD 400
//        Tags { "QUEUE"="Geometry" "IGNOREPROJECTOR"="true" "DisableBatching"="LodFading" "RenderType"="Opaque" }
//        Pass 1/7 {
//          Name "FORWARD"
//          Tags { "LIGHTMODE"="ForwardBase" "QUEUE"="Geometry" "IGNOREPROJECTOR"="true" "SHADOWSUPPORT"="true" "DisableBatching"="LodFading" "RenderType"="Opaque" }
//          Cull [_Cull]
//          GpuProgramID 58463
//          Program "vp" {
//            SubProgram "d3d11 " {
//              GpuProgramIndex 131
//            }
//          }
//        }
//      }
//    }
//
// Unity 5.3 headers extracted from SpeedTree.shader.decompressed:
//   API d3d11
//   Shader model vs_4_0
//   undeciphered1: 201509030 167 0 3
//   Keywords { "DIRECTIONAL" "SHADOWS_SCREEN" "LIGHTMAP_OFF" "DIRLIGHTMAP_OFF" "DYNAMICLIGHTMAP_OFF" "LOD_FADE_PERCENTAGE" "EFFECT_BUMP" "GEOM_TYPE_BRANCH" }
//   undeciphered2: 1 0 5 0 0
//   undeciphered3: 159 6 0 0 2 1 1 2 3 4 4 5 7 8
//   ConstBuffer "$Globals" 176
//   Float 128 [_WindQuality]
//   Float 132 [_WindEnabled]
//   Vector 144 [_Color]
//   ConstBuffer "UnityPerCamera" 144
//   Vector 80 [_ProjectionParams]
//   ConstBuffer "UnityLighting" 720
//   Vector 656 [unity_SHBr]
//   Vector 672 [unity_SHBg]
//   Vector 688 [unity_SHBb]
//   Vector 704 [unity_SHC]
//   ConstBuffer "UnityPerDraw" 352
//   Vector 320 [unity_LODFade]
//   Vector 336 [unity_WorldTransformParams]
//   Matrix 0 [glstate_matrix_mvp]
//   Matrix 192 [_Object2World]
//   Matrix 256 [_World2Object]
//   ConstBuffer "SpeedTreeWind" 256
//   Vector 0 [_ST_WindVector]
//   Vector 16 [_ST_WindGlobal]
//   Vector 32 [_ST_WindBranch]
//   Vector 48 [_ST_WindBranchTwitch]
//   Vector 64 [_ST_WindBranchWhip]
//   Vector 80 [_ST_WindBranchAnchor]
//   Vector 96 [_ST_WindBranchAdherences]
//   Vector 112 [_ST_WindTurbulences]
//   Vector 240 [_ST_WindAnimation]
//   BindCB "$Globals" 0
//   BindCB "UnityPerCamera" 1
//   BindCB "UnityLighting" 2
//   BindCB "UnityPerDraw" 3
//   BindCB "SpeedTreeWind" 4
//
// Headers extracted with DarkStarSword's extract_unity53_shaders.py
// https://raw.githubusercontent.com/DarkStarSword/3d-fixes/master/extract_unity53_shaders.py

// ---- Created with 3Dmigoto v1.2.38 on Thu Feb 09 12:28:39 2017
cbuffer cb4 : register(b4)
{
  float4 cb4[16];
}

cbuffer cb3 : register(b3)
{
  float4 cb3[22];
}

cbuffer cb2 : register(b2)
{
  float4 cb2[45];
}

cbuffer cb1 : register(b1)
{
  float4 cb1[6];
}

cbuffer cb0 : register(b0)
{
  float4 cb0[10];
}




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


void main( 
  float4 v0 : POSITION0,
  float4 v1 : TANGENT0,
  float3 v2 : NORMAL0,
  float4 v3 : TEXCOORD0,
  float4 v4 : TEXCOORD1,
  float4 v5 : TEXCOORD2,
  float2 v6 : TEXCOORD3,
  float4 v7 : COLOR0,
  out float4 o0 : SV_POSITION0,
  out float4 o1 : TEXCOORD0,
  out float4 o2 : TEXCOORD1,
  out float4 o3 : TEXCOORD2,
  out float4 o4 : TEXCOORD3,
  out float4 o5 : TEXCOORD4,
  out float4 o6 : TEXCOORD5,
  out float4 o7 : TEXCOORD6)
{
  float4 r0,r1,r2,r3,r4,r5,r6;
  uint4 bitmask, uiDest;
  float4 fDest;

float4 stereo = StereoParams.Load(0);
float separation = stereo.x, convergence = stereo.y, eye = stereo.z;

  o4.xyz = cb0[9].xyz * v7.xxx;
  r0.x = cb0[8].x * cb0[8].y;
  r0.y = cmp(0 >= r0.x);
  r1.xyz = cb4[0].yyy * cb3[17].xyz;
  r1.xyz = cb3[16].xyz * cb4[0].xxx + r1.xyz;
  r1.xyz = cb3[18].xyz * cb4[0].zzz + r1.xyz;
  r0.z = dot(r1.xyz, r1.xyz);
  r0.z = rsqrt(r0.z);
  r0.zw = r1.xz * r0.zz;
  r0.yz = r0.yy ? float2(0,0) : r0.zw;
  r1.xyz = v4.xyz + -v0.xyz;
  r1.xyz = cb3[20].xxx * r1.xyz + v0.xyz;
  r0.w = cmp(r0.x >= 3);
  if (r0.w != 0) {
    r0.w = cmp(r0.x == 5.000000);
    if (r0.w != 0) {
      r2.xyz = cb4[5].yyy * cb3[17].xyz;
      r2.xyz = cb3[16].xyz * cb4[5].xxx + r2.xyz;
      r2.xyz = cb3[18].xyz * cb4[5].zzz + r2.xyz;
      r0.w = dot(r2.xyz, r2.xyz);
      r0.w = rsqrt(r0.w);
      r2.xyz = r2.xyz * r0.www;
      r3.xyz = float3(0.0625,1,16) * v3.www;
      r3.xyz = frac(r3.xyz);
      r3.xyz = r3.xyz * float3(2,2,2) + float3(-1,-1,-1);
      r3.xyz = v3.zzz * r3.xyz;
      r0.w = cb3[15].x + cb3[15].y;
      r0.w = cb4[2].x + r0.w;
      r4.x = v3.w + r0.w;
      r4.y = r0.w * cb4[3].y + v3.w;
      r1.w = cb4[3].y * r4.x;
      r4.z = 0.5 * r1.w;
      r4.w = -v3.z + r4.x;
      r4.xyzw = float4(0.5,0.5,0.5,1.5) + r4.xyzw;
      r4.xyzw = frac(r4.xyzw);
      r4.xyzw = r4.xyzw * float4(2,2,2,2) + float4(-1,-1,-1,-1);
      r5.xyzw = abs(r4.xyzw) * abs(r4.xyzw);
      r4.xyzw = -abs(r4.xyzw) * float4(2,2,2,2) + float4(3,3,3,3);
      r4.xyzw = r5.xyzw * r4.xyzw + float4(-0.5,-0.5,-0.5,-0.5);
      r4.xyzw = r4.xyzw + r4.xyzw;
      r5.w = r4.y * r4.z;
      r1.w = cmp(r5.w < 0);
      r5.y = -r5.w;
      r5.xz = float2(-1,1);
      r5.xy = r1.ww ? r5.xy : r5.zw;
      r1.w = -r4.y * r4.z + r5.x;
      r1.w = r5.y * r1.w + r5.w;
      r2.w = r5.x + -r1.w;
      r1.w = r5.y * r2.w + r1.w;
      r1.w = cb4[3].x * r1.w;
      r2.w = 1 + -cb4[0].w;
      r3.w = 1 + -cb4[3].x;
      r3.w = r4.x * r3.w;
      r1.w = r1.w * r2.w + r3.w;
      r2.w = r4.w * cb4[4].x + 1;
      r1.w = r2.w * r1.w;
      r3.xyz = r3.xyz * r1.www;
      r3.xyz = r3.xyz * cb4[2].yyy + r1.xyz;
      r4.x = r0.w * 0.100000001 + v3.w;
      r0.w = cb4[15].x * cb4[7].x;
      r4.y = r0.w * 0.100000001 + v3.w;
      r4.xy = float2(0.5,0.5) + r4.xy;
      r4.xy = frac(r4.xy);
      r4.xy = r4.xy * float2(2,2) + float2(-1,-1);
      r5.xy = abs(r4.xy) * abs(r4.xy);
      r4.xy = -abs(r4.xy) * float2(2,2) + float2(3,3);
      r4.xy = r5.xy * r4.xy + float2(-0.5,-0.5);
      r4.xy = r4.xy + r4.xy;
      r4.xy = r4.xy * r4.xy;
      r0.w = r4.y * r4.x;
      r0.w = -r0.w * cb4[7].x + 1;
      r1.w = cb4[0].w * r4.w;
      r0.w = r1.w * cb4[4].x + r0.w;
      r2.xyz = r2.xyz * cb4[5].www + -r3.xyz;
      r2.xyz = cb4[6].yyy * r2.xyz;
      r2.xyz = r2.xyz * r0.www;
      r1.xyz = r2.xyz * v3.zzz + r3.xyz;
    } else {
      r2.xyz = float3(0.0625,1,16) * v3.www;
      r2.xyz = frac(r2.xyz);
      r2.xyz = r2.xyz * float3(2,2,2) + float3(-1,-1,-1);
      r2.xyz = v3.zzz * r2.xyz;
      r0.w = cb3[15].x + cb3[15].y;
      r0.w = cb4[2].x + r0.w;
      r3.x = v3.w + r0.w;
      r3.y = r0.w * cb4[3].y + v3.w;
      r0.w = cb4[3].y * r3.x;
      r3.z = 0.5 * r0.w;
      r3.xyz = float3(0.5,0.5,0.5) + r3.xyz;
      r3.xyz = frac(r3.xyz);
      r3.xyz = r3.xyz * float3(2,2,2) + float3(-1,-1,-1);
      r4.xyz = abs(r3.xyz) * abs(r3.xyz);
      r3.xyz = -abs(r3.xyz) * float3(2,2,2) + float3(3,3,3);
      r3.xyz = r4.xyz * r3.xyz + float3(-0.5,-0.5,-0.5);
      r3.xyz = r3.xyz + r3.xyz;
      r4.w = r3.y * r3.z;
      r0.w = cmp(r4.w < 0);
      r4.y = -r4.w;
      r4.xz = float2(-1,1);
      r4.xy = r0.ww ? r4.xy : r4.zw;
      r0.w = -r3.y * r3.z + r4.x;
      r0.w = r4.y * r0.w + r4.w;
      r1.w = r4.x + -r0.w;
      r0.w = r4.y * r1.w + r0.w;
      r0.w = cb4[3].x * r0.w;
      r1.w = 1 + -cb4[0].w;
      r2.w = 1 + -cb4[3].x;
      r2.w = r3.x * r2.w;
      r0.w = r0.w * r1.w + r2.w;
      r2.xyz = r2.xyz * r0.www;
      r1.xyz = r2.xyz * cb4[2].yyy + r1.xyz;
    }
  }
  r0.x = cmp(0 < r0.x);
  r0.w = dot(r1.xyz, r1.xyz);
  r0.w = sqrt(r0.w);
  r1.w = 1 / cb4[1].z;
  r1.w = -r1.w * 0.25 + r1.y;
  r1.w = max(0, r1.w);
  r1.w = cb4[1].z * r1.w;
  r2.x = cmp(r1.w != 0.000000);
  r2.y = log2(r1.w);
  r2.y = cb4[1].w * r2.y;
  r2.y = exp2(r2.y);
  r1.w = r2.x ? r2.y : r1.w;
  r2.x = cb4[1].x + cb3[15].x;
  r2.y = cb4[1].x * 0.800000012 + cb3[15].y;
  r2.xy = float2(0.5,0.5) + r2.xy;
  r2.xy = frac(r2.xy);
  r2.xy = r2.xy * float2(2,2) + float2(-1,-1);
  r2.zw = abs(r2.xy) * abs(r2.xy);
  r2.xy = -abs(r2.xy) * float2(2,2) + float2(3,3);
  r2.xy = r2.zw * r2.xy + float2(-0.5,-0.5);
  r2.xy = r2.xy + r2.xy;
  r2.x = r2.y * r2.y + r2.x;
  r2.y = cb4[6].x / cb4[1].z;
  r2.x = cb4[1].y * r2.x + r2.y;
  r1.w = r2.x * r1.w;
  r2.xz = r0.yz * r1.ww + r1.xz;
  r2.y = r1.y;
  r0.y = dot(r2.xyz, r2.xyz);
  r0.y = rsqrt(r0.y);
  r2.xyz = r2.xyz * r0.yyy;
  r0.yzw = r2.xyz * r0.www;
  r0.xyz = r0.xxx ? r0.yzw : r1.xyz;
  r1.xyzw = cb3[1].xyzw * r0.yyyy;
  r1.xyzw = cb3[0].xyzw * r0.xxxx + r1.xyzw;
  r1.xyzw = cb3[2].xyzw * r0.zzzz + r1.xyzw;
  r1.xyzw = cb3[3].xyzw * v0.wwww + r1.xyzw;
  r2.xyz = cb3[13].xyz * r0.yyy;
  r0.xyw = cb3[12].xyz * r0.xxx + r2.xyz;
  r0.xyz = cb3[14].xyz * r0.zzz + r0.xyw;
  r0.xyz = cb3[15].xyz * v0.www + r0.xyz;
  r2.x = cb3[16].x;
  r2.y = cb3[17].x;
  r2.zw = cb3[18].xx;
  r3.x = cb3[16].y;
  r3.y = cb3[17].y;
  r3.zw = cb3[18].yy;
  r3.xyzw = v2.yyyy * r3.xyzw;
  r2.xyzw = r2.xyzw * v2.xxxx + r3.xyzw;
  r3.x = cb3[16].z;
  r3.y = cb3[17].z;
  r3.zw = cb3[18].zz;
  r2.xyzw = r3.xyzw * v2.zzzz + r2.xyzw;
  r0.w = dot(r2.xyw, r2.xyw);
  r0.w = rsqrt(r0.w);
  r2.xyzw = r2.xyzw * r0.wwww;
  r3.xyz = cb3[13].yzx * v1.yyy;
  r3.xyz = cb3[12].yzx * v1.xxx + r3.xyz;
  r3.xyz = cb3[14].yzx * v1.zzz + r3.xyz;
  r0.w = dot(r3.xyz, r3.xyz);
  r0.w = rsqrt(r0.w);
  r3.xyz = r3.xyz * r0.www;
  r0.w = cb3[21].w * v1.w;
  r4.xyz = r3.xyz * r2.wxy;
  r4.xyz = r2.ywx * r3.yzx + -r4.xyz;
  r4.xyz = r4.xyz * r0.www;
  r5.xyzw = r2.xyzw * r2.ywzx;
  r6.x = dot(cb2[41].xyzw, r5.xyzw);
  r6.y = dot(cb2[42].xyzw, r5.xyzw);
  r6.z = dot(cb2[43].xyzw, r5.xyzw);
  r0.w = r2.y * r2.y;
  r0.w = r2.x * r2.x + -r0.w;
  o6.xyz = cb2[44].xyz * r0.www + r6.xyz;

o0.xyzw = r1.xyzw; // Relocated to here with hlsltool.py

// Automatic vertex shader halo fix inserted with DarkStarSword's hlsltool.py:
// hlsltool.py -I ../112 --auto-fix-vertex-halo --fix-unity-reflection --only-autofixed --no-validate --ignore-other-errors 663e4518b418ce24-vs_replace.txt
if (r1.w != 1.0) { r1.x += separation * (r1.w - convergence); }

  r5.xz = float2(0.5,0.5) * r1.xw;
  r0.w = cb1[5].x * r1.y;
  r5.w = 0.5 * r0.w;
  o7.xy = r5.xw + r5.zz;

  // o0.xyzw = r1.xyzw; // Relocated from here with hlsltool.py

  o1.x = r3.z;
  o1.y = r4.x;
  o1.z = r2.x;
  o1.w = r0.x;
  o2.x = r3.x;
  o2.y = r4.y;
  o2.z = r2.y;
  o2.w = r0.y;
  o3.x = r3.y;
  o3.y = r4.z;
  o3.z = r2.w;
  o3.w = r0.z;
  o4.w = cb0[9].w;
  o7.zw = r1.zw;
  o5.xy = v3.xy;
  o5.z = 0;
  return;
}
