//========== Copyright (c) Valve Corporation, All rights reserved. ==========// // STATIC: "SFM" "0..0" [vs20] [PC] // STATIC: "SFM" "0..1" [vs30] [PC] // STATIC: "SFM" "0..0" [CONSOLE] // STATIC: "HALFLAMBERT" "0..1" // STATIC: "USE_WITH_2B" "0..1" // STATIC: "DECAL" "0..1" [vs30] // STATIC: "FLASHLIGHT" "0..1" [CONSOLE] // STATIC: "WORLD_NORMAL" "0..0" [vs20] [PC] // STATIC: "WORLD_NORMAL" "0..1" [vs30] [PC] // STATIC: "WORLD_NORMAL" "0..0" [CONSOLE] // STATIC: "FLATTEN_STATIC_CONTROL_FLOW" "0..1" [vs20] [PC] // STATIC: "FLATTEN_STATIC_CONTROL_FLOW" "0..0" [CONSOLE] // STATIC: "CASCADED_SHADOW_MAPPING" "0..1" [vs20] [PC] // STATIC: "CASCADED_SHADOW_MAPPING" "0..0" [vs30] [PC] // DYNAMIC: "COMPRESSED_VERTS" "0..1" // DYNAMIC: "SKINNING" "0..1" // DYNAMIC: "MORPHING" "0..0" [ = false ] // DYNAMIC: "TESSELLATION" "0..0" [vs30] [PC] // DYNAMIC: "TESSELLATION" "0..0" [CONSOLE] // DYNAMIC: "TESSELLATION" "0..0" [vs20] [PC] // DYNAMIC: "NUM_LIGHTS" "0..2" [vs20] [PC] // DYNAMIC: "NUM_LIGHTS" "0..0" [vs20] [CONSOLE] // DYNAMIC: "STATICLIGHT3" "0..2" // If using static control flow on Direct3D, we should use the NUM_LIGHTS=0 combo // SKIP: ( $FLATTEN_STATIC_CONTROL_FLOW == 0 ) && ( $NUM_LIGHTS > 0 ) [vs20] [PC] // When tessellating, we don't have room in the super-prim vertices for more colors, tex coords or objects space positions // SKIP: ( $STATICLIGHT3 || $MORPHING || $SKINNING || $COMPRESSED_VERTS) && $TESSELLATION // SKIP: ( $SFM == 0 ) && ( $MORPHING ) // SKIP: ( $SFM == 0 ) && ( $TESSELLATION ) #include "common_fog_vs_supportsvertexfog_fxc.h" #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 #if ( WORLD_NORMAL ) const float4 g_vEyeVector : register( c26 ); #endif const float4 cBumpTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_2 ); const float4 cDetailTexCoordTransform[2] : register( SHADER_SPECIFIC_CONST_4 ); // 4 & 5 const float4x4 g_FlashlightWorldToTexture : register( SHADER_SPECIFIC_CONST_6 ); // 6, 7, 8, 9 #if ( MORPHING ) // 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( s0 ); #endif #if ( CASCADED_SHADOW_MAPPING ) const float4x4 g_matWorldToShadowTexMatrices[2] : register( SHADER_SPECIFIC_CONST_CSM_0 ); const float4 g_matWorldToShadowTexMatrix0_z : register( SHADER_SPECIFIC_CONST_CSM_1 ); #endif const float4 g_vCSMLightColor : register( SHADER_SPECIFIC_CONST_12 ); #if ( TESSELLATION ) // VS_INPUT defined in header #include "tessellation_vs_fxc.h" const float4 g_SubDControls : register( SHADER_SPECIFIC_CONST_3 ); sampler2D BezierSampler : register( s1 ); sampler2D DispSampler : register( s2 ); #else // no TESSELLATION 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; float4 vStaticLight : COLOR1; #if STATICLIGHT3 float4 vStaticLight2 : COLOR2; float4 vStaticLight3 : COLOR3; #endif // 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; #if ( SFM ) float vVertexID : POSITION2; #endif }; #endif // TESSELLATION //----------------------------------------------------------------------------- // Output vertex format //----------------------------------------------------------------------------- struct VS_OUTPUT { // Stuff that isn't seen by the pixel shader float4 projPos : POSITION; #if !defined( _X360 ) && !defined( SHADER_MODEL_VS_3_0 ) float fog : FOG; #endif // Stuff that is seen by the pixel shader float4 baseTexCoord_baseTexCoord2 : TEXCOORD0; // Base texture coordinates in .xy, 2nd (decal) uv set in .zw float4 bumpTexCoord_detailTexCoord : TEXCOORD1; float4 vWorldNormal_fogFactorW : TEXCOORD2; // world space normal in .xyz, fog factor in .w float4 vWorldTangent : TEXCOORD3; // world tangent in .xyz, tangent sign in .w float4 vWorldPos_linearDepth : TEXCOORD4; // world pos in .xyz, view space linear depth in w float4 vProjPos : TEXCOORD5; // proj pos .xyzw #if defined( SHADER_MODEL_VS_3_0 ) float4 staticLight0 : TEXCOORD6; // STATICLIGHT3 output basis 0 in .xyz float4 staticLight1 : TEXCOORD7; // STATICLIGHT3 output basis 1 in .xyz float4 staticLight2 : TEXCOORD8; // STATICLIGHT3 output basis 2 in .xyz float4 lightAtten : TEXCOORD9; // light attenuation for 4 lights #else float4 csmXform0or1_csmXform2 : TEXCOORD6; // csm lightToWorldxformcascade0 or 1.xy in .xy lightToWorldxformcascade2.xy in .zw float4 staticLight_csmXform0z : TEXCOORD7; // STATICLIGHT3 average rgb in .xyz, csm lightToWorldxformcascade0.z in .w float4 lightAtten : COLOR0; // light attenuation for lights 0 and 1 in .xy (lower precision for sm2_b/OSX) #endif #if defined( _GAMECONSOLE ) && FLASHLIGHT float4 flashlightSpacePos : TEXCOORD8; #endif }; float Luminance( float3 cColor ) { // Formula for calculating luminance based on NTSC standard return dot( cColor.rgb, float3( 0.2125, 0.7154, 0.0721 ) ); } //----------------------------------------------------------------------------- // Main shader entry point //----------------------------------------------------------------------------- VS_OUTPUT main( const VS_INPUT v ) { VS_OUTPUT o = ( VS_OUTPUT )0; float4 vPosition, vTangent; float4 worldPos = float4(0.0f, 0.0f, 0.0f, 1.0f); float3 vNormal, worldNormal, worldTangentS, worldTangentT; #if ( TESSELLATION ) { float flDummyWrinkle; EvaluateSubdivisionSurface( v, g_SubDControls.x, g_SubDControls.y, g_SubDControls.z, BezierSampler, DispSampler, worldNormal, worldPos.xyz, worldTangentS, worldTangentT, vTangent.w, flDummyWrinkle, o.baseTexCoord_baseTexCoord2.xy, o.bumpTexCoord_detailTexCoord.zw ); } #else // no TESSELLATION { vPosition = v.vPos; DecompressVertex_NormalTangent( v.vNormal, v.vUserData, vNormal, vTangent ); #if ( !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 SkinPositionNormalAndTangentSpace( g_bSkinning, vPosition, vNormal, vTangent, v.vBoneWeights, v.vBoneIndices, worldPos.xyz, 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 ( MORPHING && DECAL ) { // Avoid z precision errors worldPos.xyz += worldNormal * 0.05f * v.vTexCoord2.z; } #endif #if ( STATICLIGHT3 ) { #if defined( SHADER_MODEL_VS_3_0 ) // Adjust lighting data into full range o.staticLight0.rgb = GammaToLinear( v.vStaticLight.rgb * cOverbright ); o.staticLight1.rgb = GammaToLinear( v.vStaticLight2.rgb * cOverbright ); o.staticLight2.rgb = GammaToLinear( v.vStaticLight3.rgb * cOverbright ); #if ( STATICLIGHT3 == 2 ) // Fallback for maps that still bake sunlight into the vertex color stream. Subtract it out here. // It gets added back in the pixel shader. Doing this here in the VS saves pixel shader combos. o.staticLight0.rgb -= g_vCSMLightColor.rgb * v.vStaticLight.a; o.staticLight1.rgb -= g_vCSMLightColor.rgb * v.vStaticLight2.a; o.staticLight2.rgb -= g_vCSMLightColor.rgb * v.vStaticLight3.a; o.staticLight0.rgb = max( 0.0f, o.staticLight0.rgb ); o.staticLight1.rgb = max( 0.0f, o.staticLight1.rgb ); o.staticLight2.rgb = max( 0.0f, o.staticLight2.rgb ); #endif o.staticLight0.w = v.vStaticLight.a; o.staticLight1.w = v.vStaticLight2.a; o.staticLight2.w = v.vStaticLight3.a; #else // take average of STATICLIGHT3 data for shader model 2b (OSX) float4 vStaticLightAvg; vStaticLightAvg.rgb = GammaToLinear( v.vStaticLight.rgb * cOverbright ); vStaticLightAvg.rgb += GammaToLinear( v.vStaticLight2.rgb * cOverbright ); vStaticLightAvg.rgb += GammaToLinear( v.vStaticLight3.rgb * cOverbright ); vStaticLightAvg.a = v.vStaticLight.a + v.vStaticLight2.a + v.vStaticLight3.a; #if ( STATICLIGHT3 == 2 ) vStaticLightAvg.rgb -= g_vCSMLightColor.rgb * vStaticLightAvg.a; vStaticLightAvg.rgb = max( 0.0f, vStaticLightAvg.rgb ); #endif vStaticLightAvg *= 0.3333f; o.staticLight_csmXform0z.rgb = vStaticLightAvg.rgb; o.lightAtten.w = vStaticLightAvg.a; #endif } #endif // Base texture coordinate transform o.baseTexCoord_baseTexCoord2.x = dot( v.vTexCoord0, cBaseTexCoordTransform[0] ); o.baseTexCoord_baseTexCoord2.y = dot( v.vTexCoord0, cBaseTexCoordTransform[1] ); // 2nd uv set, pass through for now o.baseTexCoord_baseTexCoord2.zw = v.vTexCoord1.xy; // bump texture coordinate transform - temp o.bumpTexCoord_detailTexCoord.x = dot( v.vTexCoord0, cBumpTexCoordTransform[0] ); o.bumpTexCoord_detailTexCoord.y = dot( v.vTexCoord0, cBumpTexCoordTransform[1] ); // Detail texture coordinate transform o.bumpTexCoord_detailTexCoord.z = dot( v.vTexCoord0, cDetailTexCoordTransform[0] ); o.bumpTexCoord_detailTexCoord.w = dot( v.vTexCoord0, cDetailTexCoordTransform[1] ); } #endif // TESSELLATION // Transform into projection space o.projPos = mul( worldPos, cViewProj ); #ifdef _PS3 // Account for OpenGL's flipped y coordinate and expanded z range [-1,1] instead of [0,1] o.projPos.y = -o.projPos.y; o.projPos.z = 2.0f * o.projPos.z - o.projPos.w; #endif // _PS3 o.vWorldNormal_fogFactorW.xyz = worldNormal.xyz; o.vWorldTangent = float4( worldTangentS.xyz, vTangent.w ); // Propagate binormal sign in world tangent.w // pack required proj pos components (used for screen space pos in flashlight shadow map noise) o.vProjPos = o.projPos; #if ( defined( SHADER_MODEL_VS_2_0 ) && FLATTEN_STATIC_CONTROL_FLOW ) { #if ( NUM_LIGHTS > 0 ) o.lightAtten.x = GetVertexAttenForLight( worldPos.xyz, 0, false ); #endif #if ( NUM_LIGHTS > 1 ) o.lightAtten.y = GetVertexAttenForLight( worldPos.xyz, 1, false ); #endif } #else { // Scalar light attenuation o.lightAtten.x = GetVertexAttenForLight( worldPos.xyz, 0 ); o.lightAtten.y = GetVertexAttenForLight( worldPos.xyz, 1 ); #if defined( SHADER_MODEL_VS_3_0 ) o.lightAtten.z = GetVertexAttenForLight( worldPos.xyz, 2 ); o.lightAtten.w = GetVertexAttenForLight( worldPos.xyz, 3 ); #endif } #endif // Needed for water fog alpha and diffuse lighting o.vWorldPos_linearDepth.xyz = worldPos.xyz; #if ( WORLD_NORMAL ) { o.vWorldPos_linearDepth.w = dot( g_vEyeVector, worldPos.xyz - cEyePos.xyz ); // Linear depth } #endif #if ( HARDWAREFOGBLEND ) { o.fog = CalcFixedFunctionFog( worldPos.xyz, g_FogType ); } #endif #if ( !DOPIXELFOG && !HARDWAREFOGBLEND ) { o.vWorldNormal_fogFactorW.w = CalcNonFixedFunctionFog( worldPos.xyz, g_FogType ); } #endif #if ( defined( _GAMECONSOLE ) && FLASHLIGHT ) { o.flashlightSpacePos = TransformFlashlightWorldToTexture( worldPos.xyz, g_FlashlightWorldToTexture ); } #endif #if( CASCADED_SHADOW_MAPPING ) { o.csmXform0or1_csmXform2.xy = mul( worldPos, g_matWorldToShadowTexMatrices[0] ).xy; o.csmXform0or1_csmXform2.zw = mul( worldPos, g_matWorldToShadowTexMatrices[1] ).xy; o.staticLight_csmXform0z.w = mul( worldPos, g_matWorldToShadowTexMatrix0_z ); } #endif return o; }