1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
//======= Copyright (c) 1996-2009, Valve Corporation, All rights reserved. ======
// STATIC: "HALFLAMBERT" "0..1"
// STATIC: "USE_WITH_2B" "0..1"
// STATIC: "DECAL" "0..1" [vs30]
// STATIC: "FLASHLIGHT" "0..1" [XBOX]
// STATIC: "USE_STATIC_CONTROL_FLOW" "0..1" [vs20]
// DYNAMIC: "COMPRESSED_VERTS" "0..1"
// DYNAMIC: "DOWATERFOG" "0..1"
// DYNAMIC: "SKINNING" "0..1"
// DYNAMIC: "MORPHING" "0..1" [vs30]
// DYNAMIC: "NUM_LIGHTS" "0..2" [vs20]
// If using static control flow on Direct3D, we should use the NUM_LIGHTS=0 combo
// SKIP: $USE_STATIC_CONTROL_FLOW && ( $NUM_LIGHTS > 0 ) [vs20]
#include "common_vs_fxc.h"
static const bool g_bSkinning = SKINNING ? true : false;
static const int g_FogType = DOWATERFOG;
const float4 cBaseTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_0 ); // 0 & 1
const float4 cDetailTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_4 ); // 4 & 5
const float4x4 g_FlashlightWorldToTexture : register( SHADER_SPECIFIC_CONST_6 ); // 6, 7, 8, 9
#ifdef SHADER_MODEL_VS_3_0
// NOTE: cMorphTargetTextureDim.xy = target dimensions,
// cMorphTargetTextureDim.z = 4tuples/morph
const float3 cMorphTargetTextureDim : register( SHADER_SPECIFIC_CONST_10 );
const float4 cMorphSubrect : register( SHADER_SPECIFIC_CONST_11 );
sampler2D morphSampler : register( D3DVERTEXTEXTURESAMPLER0, s0 );
#endif
//-----------------------------------------------------------------------------
// Input vertex format
//-----------------------------------------------------------------------------
struct VS_INPUT
{
// This is all of the stuff that we ever use.
float4 vPos : POSITION;
float4 vBoneWeights : BLENDWEIGHT;
float4 vBoneIndices : BLENDINDICES;
float4 vNormal : NORMAL;
float4 vColor : COLOR0;
float3 vSpecular : COLOR1;
// make these float2's and stick the [n n 0 1] in the dot math.
float4 vTexCoord0 : TEXCOORD0;
float4 vTexCoord1 : TEXCOORD1;
float4 vTexCoord2 : TEXCOORD2;
float4 vTexCoord3 : TEXCOORD3;
float3 vTangentS : TANGENT;
float3 vTangentT : BINORMAL;
float4 vUserData : TANGENT;
// Position and normal/tangent deltas
float3 vPosFlex : POSITION1;
float3 vNormalFlex : NORMAL1;
#ifdef SHADER_MODEL_VS_3_0
float vVertexID : POSITION2;
#endif
};
//-----------------------------------------------------------------------------
// Output vertex format
//-----------------------------------------------------------------------------
struct VS_OUTPUT
{
// Stuff that isn't seen by the pixel shader
float4 projPos : POSITION;
#if !defined( _X360 )
float fog : FOG;
#endif
// Stuff that is seen by the pixel shader
float4 baseTexCoord2_tangentSpaceVertToEyeVectorXY : TEXCOORD0;
float3 lightAtten : TEXCOORD1;
float4 worldVertToEyeVectorXYZ_tangentSpaceVertToEyeVectorZ : TEXCOORD2;
float3 vWorldNormal : TEXCOORD3; // World-space normal
float4 vWorldTangent : TEXCOORD4;
#if USE_WITH_2B
float4 vProjPos : TEXCOORD5;
#else
float3 vWorldBinormal : TEXCOORD5;
#endif
float4 worldPos_projPosZ : TEXCOORD6;
float3 detailTexCoord_atten3 : TEXCOORD7;
float4 fogFactorW : COLOR1;
#if defined( _X360 ) && FLASHLIGHT
float4 flashlightSpacePos : TEXCOORD8;
#endif
};
//-----------------------------------------------------------------------------
// Main shader entry point
//-----------------------------------------------------------------------------
VS_OUTPUT main( const VS_INPUT v )
{
VS_OUTPUT o = ( VS_OUTPUT )0;
float4 vPosition = v.vPos;
float3 vNormal;
float4 vTangent;
DecompressVertex_NormalTangent( v.vNormal, v.vUserData, vNormal, vTangent );
#if !defined( SHADER_MODEL_VS_3_0 ) || !MORPHING
ApplyMorph( v.vPosFlex, v.vNormalFlex, vPosition.xyz, vNormal, vTangent.xyz );
#else
ApplyMorph( morphSampler, cMorphTargetTextureDim, cMorphSubrect,
v.vVertexID, v.vTexCoord2, vPosition.xyz, vNormal, vTangent.xyz );
#endif
// Perform skinning
float3 worldNormal, worldPos, worldTangentS, worldTangentT;
SkinPositionNormalAndTangentSpace( g_bSkinning, vPosition, vNormal, vTangent,
v.vBoneWeights, v.vBoneIndices, worldPos,
worldNormal, worldTangentS, worldTangentT );
// Always normalize since flex path is controlled by runtime
// constant not a shader combo and will always generate the normalization
worldNormal = normalize( worldNormal );
worldTangentS = normalize( worldTangentS );
worldTangentT = normalize( worldTangentT );
#if defined( SHADER_MODEL_VS_3_0 ) && MORPHING && DECAL
// Avoid z precision errors
worldPos += worldNormal * 0.05f * v.vTexCoord2.z;
#endif
o.vWorldNormal.xyz = worldNormal.xyz;
o.vWorldTangent = float4( worldTangentS.xyz, vTangent.w ); // Propagate binormal sign in world tangent.w
// Transform into projection space
float4 vProjPos = mul( float4( worldPos, 1 ), cViewProj );
o.projPos = vProjPos;
vProjPos.z = dot( float4( worldPos, 1 ), cViewProjZ );
#if USE_WITH_2B
o.vProjPos = vProjPos;
#else
o.vWorldBinormal.xyz = worldTangentT.xyz;
#endif
o.fogFactorW = CalcFog( worldPos, vProjPos, g_FogType );
#if !defined( _X360 )
o.fog = o.fogFactorW;
#endif
// Needed for water fog alpha and diffuse lighting
// FIXME: we shouldn't have to compute this all the time.
o.worldPos_projPosZ = float4( worldPos, vProjPos.z );
// Needed for cubemapping + parallax mapping
// FIXME: We shouldn't have to compute this all the time.
//o.worldVertToEyeVectorXYZ_tangentSpaceVertToEyeVectorZ.xyz = VSHADER_VECT_SCALE * (cEyePos - worldPos);
o.worldVertToEyeVectorXYZ_tangentSpaceVertToEyeVectorZ.xyz = normalize( cEyePos.xyz - worldPos.xyz );
#if defined( SHADER_MODEL_VS_2_0 ) && ( !USE_STATIC_CONTROL_FLOW )
o.lightAtten.xyz = float3(0,0,0);
o.detailTexCoord_atten3.z = 0.0f;
#if ( NUM_LIGHTS > 0 )
o.lightAtten.x = GetVertexAttenForLight( worldPos, 0, false );
#endif
#if ( NUM_LIGHTS > 1 )
o.lightAtten.y = GetVertexAttenForLight( worldPos, 1, false );
#endif
#if ( NUM_LIGHTS > 2 )
o.lightAtten.z = GetVertexAttenForLight( worldPos, 2, false );
#endif
#if ( NUM_LIGHTS > 3 )
o.detailTexCoord_atten3.z = GetVertexAttenForLight( worldPos, 3, false );
#endif
#else
// Scalar light attenuation
o.lightAtten.x = GetVertexAttenForLight( worldPos, 0, true );
o.lightAtten.y = GetVertexAttenForLight( worldPos, 1, true );
o.lightAtten.z = GetVertexAttenForLight( worldPos, 2, true );
o.detailTexCoord_atten3.z = GetVertexAttenForLight( worldPos, 3, true );
#endif
// Base texture coordinate transform
o.baseTexCoord2_tangentSpaceVertToEyeVectorXY.x = dot( v.vTexCoord0, cBaseTexCoordTransform[0] );
o.baseTexCoord2_tangentSpaceVertToEyeVectorXY.y = dot( v.vTexCoord0, cBaseTexCoordTransform[1] );
// Detail texture coordinate transform
o.detailTexCoord_atten3.x = dot( v.vTexCoord0, cDetailTexCoordTransform[0] );
o.detailTexCoord_atten3.y = dot( v.vTexCoord0, cDetailTexCoordTransform[1] );
#if defined( _X360 ) && FLASHLIGHT
o.flashlightSpacePos = mul( float4( worldPos, 1.0f ), g_FlashlightWorldToTexture );
#endif
return o;
}
|