//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // stuff which should most certainly not be enabled in final build //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // visual aids are stuff like the thread profiler, light probe visualizer, ... // this should most certainly be disabled in final builds //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // standard defines //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //#define SCREEN_ROTATE_180 // rotates scene 180 degrees, this should theoretically give a slight gain; however, we don't see this. (speedtree?) //#define OCCLUSION_QUERIES_ENABLE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // radiosity+light probes are now integrated into the same gathering pass //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // defined by project settings //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // enable this to gather radiosity for the scene / enable the radiosity gathered //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // light probes integrate dynamic objects into the static world of indirect lighting //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //#define LIGHTPROBE_SAMPLES_DEBUG // shows where the probes will be, but does no actual sampling - this is for debugging //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // enable the vertex cube renderer if either radiosity or light probes are enabled //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // debug stuff //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // shadows - additional defines //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //#define SHADOWS_DEBUG_AID_ENABLE //#define SHADOWS_TRAPEZOIDAL_ZCORRECTED // really, really slow as sets the depth in the pixelshader //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // HDR range - rendering to 7e3 buffer //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // floating point rendertarget? // the 360 can't use a 10F_10F_10F as a texture, so we resolve as 10_10_10's, and convert to 10F's in the pixel shaders //#define USE_FP_RENDERTARGET_RUNTIME_CONVERSION //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // point light shadows on the PC are necessary for radiosity gathering, but too expensive otherwise //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // interpolator fixing - this is effectively hacking the VS->PS interpolators //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // this is starting to be a strain on the profiling.... //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //#define PIX_SET_MARKER_ENABLE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // crash reporting? //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //#define USE_MORTENS_DODGY_MEMORY_TRACKER //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // if this is enabled, fog blending will use the color of the atmospheric scattering - if not, a static value is used //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // multithreaded FIND_ALL_DATA? //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //PS3 has been updated -- use this define in case it is ever required to revert to the old method -- contains multiple model bug //#define USE_OLD_SINGLE_PASS_FIND_FIRST_DATA //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // texture defines //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // THIS CAUSES AI CODE TO RUN IN A THREAD ALONGSIDE RENDERING // FRAMETIME SAVINGS ARE SIGNIFICANT // Some generic definitions to simplify shader code - Morv //!MP! I use this in shadows.h, so I can use the same header file for C code //MP! with hierarchical z-testing, this is no longer LE of all platforms // Some max-specific definitions //////////////////////////////////////////////////////////////////////////////////////////////////// // constants in HLSL //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// // these are VERTEX SHADER constants //////////////////////////////////////////////////////////////////////////////////////////////////// // 15..26 used by generic constant table, and facial morphing weights float4 PA_StartColour : register(c9); float4 PA_EndColour : register(c10); float4 PA_StartMMEndMMSize : register(c11); float4 PA_Velocity_Alpha_Rotation_Min_AspectRatio : register(c14); float4 PA_Velocity_Alpha_Rotation_Max_Gravity : register(c15); float PA_DrawAsBillboard : register(c16); float4 SpreadNumParticlesAnimationCountLoopSystem : register(c18); float4 PA_MidMinMaxEndMinMaxVelocity : register(c19); float4 PA_EmissionArea : register(c21); float4 PA_ExpiryTime : register(c23); float4 PA_AnimationFrames : register(c24); float4 PA_Wind : register(c26); float4 ParticleFadeValue : register(c28); float4 Radiosity_YCbCr [3] : register(c36); float4 TrapezoidalShadowEpsilon : register(c39); float4 LightIndexScalar : register(c40); float4 LightIndexOffset : register(c41); float4 Foliage_Animation_Scalars : register(c42); float4 Foliage_Animation_Offsets : register(c43); float4 LeafUnitSquare [4] : register(c44); float4 FontXYScalarOffset : register(c48); float4 FontUVScalarOffset : register(c49); float4 VCMainLightDirection : register(c50); float4 CubeShadowMap_LightPosition : register(c51); float4 FarFogPlane : register(c52); float4 CharacterInertiaAndVertexScale : register(c53); float4 DualBumpLODBias : register(c54); float4 VertexBonePaletteTexture : register(c55); float4x4 ShadowMapMatrix : register(c56); float4x4 WorldViewProjection : register(c60); float4x4 WorldMatrix : register(c64); float4x4 ViewMatrix : register(c68); float4x4 ViewProj : register(c72); float4x4 TextureTransform : register(c76); float4 ScreenRes [2] : register(c80); float4 ParticleRoot : register(c88); float4 TimeIntoEffect : register(c89); float4 RadiosityInfluence : register(c90); float4 TexCoordScaleLODBlur : register(c91); // used by the cube variance shadow map blending float4x4 ProjMatrix : register(c92); float4 RicsFontShaderConstants : register(c100); float4 BillboardTextureCoords [60] : register(c100); float4 CameraRightAxis : register(c160); float4 CameraUpAxis : register(c161); float4 InstancingData : register(c162); float4 TreePos : register(c163); float4 TreeRot : register(c164); float4 TreeScale : register(c165); float4x4 WindMatrices [7] : register(c180); float4 WindSettings : register(c208); float4 SpeedTreeLightSettings : register(c209); float4 CameraAngles : register(c210); float4 SelfShadowMotion : register(c211); float4 LeafAngles [20] : register(c216); float4x4 BillboardNormals : register(c236); float4x4 BillboardBinormals : register(c240); float4x4 BillboardTangents : register(c244); float4 BillboardSpecialAzimuth : register(c248); float4 FoliageSettings : register(c249); float4 PS3StaticLightData [33 * 2] : register(c352); //////////////////////////////////////////////////////////////////////////////////////////////////// // these are PIXEL SHADER constants //////////////////////////////////////////////////////////////////////////////////////////////////// float4 FogColour : register(c0); float4 SpecularPower : register(c2); float4 MainLightInvColor : register(c7); // inverse direction light color - to show off bump maps float4 DualBumpNormalErrorThreshold : register(c8); float4 ShadowBias : register(c9); float4 MainLightDirection : register(c10); float4 MainLightColor : register(c11); float4 VarianceShadowMap_Setup : register(c12); float4 VarianceShadowMap_EdgeDelta : register(c13); float4 ShadowMapDimensions : register(c14); // 15..26 used by generic constant table float4 RadiosityCSC : register(c29); float4 RayleighMieConstants : register(c30); float BodySolidity : register(c31); float4 PostProcessing_BloomScalarOffset : register(c32); float4 PostProcessing_OverlaySettings : register(c33); float4 PostProcessing_Distortion : register(c34); // xy=distortion range, z=LOD range, w=luminance range float4 PostProcessing_TintScalar : register(c35); float4 PostProcessing_TintOffset : register(c36); float4 FilterOptions : register(c37); float4 DirectionalLightingIntensities : register(c38); float4 FogEquation [2] : register(c39); float4 HDR_Constants : register(c41); float4 AmbientDiffuse : register(c42); float4 DirectLightingInfluence : register(c43); float4 DynamicLightData [12] : register(c44); // 12 used (ie 4 lights, 1 extra just in case...), followed by static data float4 DynamicLight0_Data : register(c44); float4 DynamicLight1_Data : register(c47); float4 DynamicLight2_Data : register(c50); float4 DynamicLight3_Data : register(c53); float4 StaticLightData [96] : register(c56); float4 AmbientScalar : register(c142); // used by dynamic objects to offset the ambient values float4 AmbientOffset : register(c143); // ... float4 LightProbeColors [6] : register(c144); half4 PostProcessing_TintEq [4] : register(c150); float4 DOFFocalPointSpread : register(c154); float4 DOFReverseZPerspective : register(c155); float4 DOFKernelConstants : register(c156); float4 MotionBlurMovement : register(c157); float4 MotionBlurConstants : register(c158); float4x4 MotionBlurCurrInvViewProjection : register(c160); float4x4 MotionBlurPrevViewProjection : register(c164); float4 RayleighMieScatteringWavelengthsQuad : register(c168); // wavelengths used by rayleigh/mie to the 4th, ie "lambda^4" float4 CloudTextureDef0 : register(c169); // scrolling float4 CloudTextureDef1 : register(c170); // scrolling float4 CloudTextureDef2 : register(c171); // scrolling float4 CloudTextureDef3 : register(c172); // rotating float4 CloudBlendParams : register(c173); // blend params float4 RayleighMieSunPosition : register(c174); // blend params float4 RayleighMieRGBTranslation [3] : register(c175); // translates RGB -> whichever color float4 ShadowParameters : register(c179); // scalar, offset float4 ScreenResolution [4] : register(c180); // we only use 1, but we need 4 for tiled hack to work //////////////////////////////////////////////////////////////////////////////////////////////////// bool CubicShadowReceive : register(b0); bool RadiosityGatheringInProgress : register(b1); //////////////////////////////////////////////////////////////////////////////////////////////////// // these are VERTEX AND PIXEL SHADER constants //////////////////////////////////////////////////////////////////////////////////////////////////// float4 ConstantColour : register(c1); // !MP! do not rely on this; it's part of the old lighting system and will disappear after this milestone float4 TimeAndRainLevel : register(c3); // in .x float4 RainLevel : register(c3); // in .y float4 CameraPosition : register(c5); // 15..26 used by generic constant table //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// // Generic constants (loaded into a table at c15, but the shaders that use them don't know that) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // radiosity backface hack //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// float lfInputFaceDirection = 0.f; // less than ideal - this will internally be 'overwritten', if a variable of this name exists locally float4 BlankReverseSideDuringRadiosityGathering( float lfFaceDirection, float4 lOutputColor) { //not necessary any longer i think return lOutputColor; return lOutputColor; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // snow //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// float3 SnowValue (float3 input_colour, float world_space_normal_y) { return lerp (input_colour, float3 (1, 1, 1), saturate (world_space_normal_y - 0.5f) * (TimeAndRainLevel.z)); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // transformed vertex z position to pixel depth calculation : used for soft sprites //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// float VertexToPixelDepth(float PositionZ) // PositionZ = mul(float4(world_pos, 1), ViewProj).z; { PositionZ = 1.f - PositionZ; // hierarchical z flips the range return DOFReverseZPerspective.x / (PositionZ * DOFReverseZPerspective.y - DOFReverseZPerspective.z); // m32/(o*m23-m22) } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // color range tweak //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static const float g_overbright = 32; // Ivan: experimentally shown that hdr range of 99% of the scene is 0..4 //static const float MAX_HDR_RANGE = 3.5f; // test stuff //half4 RGBHighlightRange( half4 c, float mv ) //{ // half scale = max( c.x, max( c.y, c.z ) ); // // if( scale > mv ) // { // c = half4( 1, 0, 0, 1 ); // } // // return c; //} // classic rgbs //half4 RGB_to_RGBS( half4 c ) //{ // half scale = max( c.x, max( c.y, c.z ) ); // // half3 rgb = c.xyz / scale; // half a = saturate( scale / MAX_HDR_RANGE ); // // return half4( rgb, a ); //} // classic rgbs //half4 RGBS_to_RGB( half4 c ) //{ // return half4( c.xyz * c.a * MAX_HDR_RANGE, c.a ); //} // classic exponential //half4 RGB_to_ScaledRGB( half4 c ) //{ // return half4( c.xyz / MAX_HDR_RANGE, c.a ); //} // classic exponential //half4 ScaledRGB_to_RGB( half4 c ) //{ // return half4( c.xyz * MAX_HDR_RANGE, c.a ); //} // classic exponential // TODO: use halfs throughout for better compilation //half4 RGB_to_PPP( half4 c ) //{ // return half4( sqrt( ( half3( c.xyz ) / half( MAX_HDR_RANGE ) ) ), c.a ); //} // classic exponential //half4 PPP_to_RGB( half4 c ) //{ // return half4( c.xyz * c.xyz * MAX_HDR_RANGE, c.a ); //} //float4 RGBA_to_SonyRGBS( float4 ) static const float fLinearRange = 2.f; // scales the output color float4 HDRColorCorrected(float4 lColor) { float3 corrected = lColor.xyz / fLinearRange; return float4( corrected, lColor.a ); } // scales the output color float4 HDRColorCorrected0(float4 lColor) { return float4( lColor.xyz / fLinearRange, lColor.a ); } // scales the output color half4 HDRColorCorrected(half4 lColor) { half3 corrected = lColor.xyz / (half)fLinearRange; return half4( corrected, lColor.a ); } float4 HDRColorReverted(float4 lColor) { float multiply = lColor.a == 0 ? g_overbright : 1; return float4( lColor.xyz * fLinearRange * multiply, 1.f ); } half4 HDRColorReverted(half4 lColor) { half multiply = lColor.a == 0 ? (half)g_overbright : 1; return half4( lColor.xyz * (half)fLinearRange * multiply, 1.f ); } float4 HDRForceReversion(float4 lColor) { return lColor; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // normal = tangent X normal //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// float3 CreateBinormal( float3 lNormal, float3 lTangent) { //!MP! i actually thought it was the other way around, but this produces consistent results return cross(lTangent,lNormal); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // color space conversion //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // performs color space conversion float3 ConvertYCbCr2RGB( float3 lYCbCr) { float3x4 lNewMatrix; // Keef - Casting to float3x4 doesn't work with Cg for some reason lNewMatrix._m00_m01_m02_m03 = Radiosity_YCbCr[0]; lNewMatrix._m10_m11_m12_m13 = Radiosity_YCbCr[1]; lNewMatrix._m20_m21_m22_m23 = Radiosity_YCbCr[2]; //!MP! the PS3 should use this version as well return max(0,mul(lNewMatrix,float4(lYCbCr,1))); } // we use YCbCr rather than YUV due to YUV's limited range float3 DecompressYCbCr844( int2 lYCbCr) { // get Y:8,Cb:4,Cr:4 from the zw components (Cb,Cr are packed into the second byte) float lfY = lYCbCr.x; float lfCr = lYCbCr.y*0.0625f; //1/16 float lfCb = floor(lfCr); lfCr = lfCr-lfCb; float4 lfYCbCr1 = float4(lfY,lfCb,lfCr,1); // Keef - Casting to float3x4 doesn't work with Cg for some reason float3x4 lNewMatrix; lNewMatrix._m00_m01_m02_m03 = Radiosity_YCbCr[0]; lNewMatrix._m10_m11_m12_m13 = Radiosity_YCbCr[1]; lNewMatrix._m20_m21_m22_m23 = Radiosity_YCbCr[2]; float3 lRGB = saturate(mul(lNewMatrix,lfYCbCr1)); return lRGB; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // character UV scalar - centralized, as editing only the standard shaders mess up the shadows //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //The 360 FXLite differs from the PC FX interfaces in one important area. //The textures can only be shared on the PC and the 360 can only share and read samplers. //So when you need to define and use a texture and sampler use the macros below. // Max MapChannels make me cry texture2D font_texture : TEXTURE2D < String name = "font_pack.tga"; >; float4 COLOURS[16] : register(c100); // will overwrite BillboardTextureCoords (SpeedTree) float SCALES [16] : register(c116); // will overwrite BillboardTextureCoords (SpeedTree) float4 CHANNELS[4] : register(c132); // will overwrite BillboardTextureCoords (SpeedTree) sampler font_sampler : register(s0) = sampler_state { Texture = (font_texture); AddressU = CLAMP; AddressV = CLAMP; MinFilter = Point; MagFilter = Point; MipFilter = Point; }; // temp bodge for PS3 to avoid shader input semantic collisions! // temporary bodge until ATI and nVidia fix their bugs! // // Shader model 3 version, requires ATI and nVidia to fix some of their driver bugs to use safely // struct VS2PS3 { // not passed to PS float point_size : PSIZE; float4 centre : POSITION; // replaced by Point Sprite HW float2 sprite_uvs : TEXCOORD0; // values passed from VS float4 uv_scale_offset : TEXCOORD5; float4 channel_dot : TEXCOORD1; float4 channel_add : TEXCOORD2; float4 colour : COLOR0; }; VS2PS3 font_vs3(const float4 top_left : POSITION, const int4 uvsi : BLENDINDICES) { VS2PS3 vs2ps; // Swap to get correct stuff here! int4 uvsicopy = uvsi.wzyx; const int colour_scale = uvsicopy.x % 16; const int encoding = int(floor(uvsicopy.x / 16)) % 4; const int channel_index = int(floor(uvsicopy.x / 64)) % 4; const float scale = SCALES[colour_scale]; vs2ps.point_size = uvsicopy.w * scale; vs2ps.centre = float4((top_left * scale + vs2ps.point_size / 2.0f) * FontXYScalarOffset.xy + FontXYScalarOffset.zw, 0, 1); vs2ps.sprite_uvs = 0; // filled in by point-sprite hardware vs2ps.uv_scale_offset = float4(uvsicopy.w, uvsicopy.w, uvsicopy.y, uvsicopy.z) * FontUVScalarOffset; if (encoding == 2) // 4444 { vs2ps.channel_dot = float4(0,0,0,0); vs2ps.channel_add = float4(0,0,0,0); } else if (encoding == 1) // transparent..black..white { vs2ps.channel_dot = CHANNELS[channel_index] * 2; vs2ps.channel_add = float4(-1,-1,-1, 0); } else // white { vs2ps.channel_dot = CHANNELS[channel_index]; vs2ps.channel_add = float4( 1, 1, 1, 0); } vs2ps.colour = COLOURS[colour_scale]; return vs2ps; } const float4 font_ps3( const VS2PS3 vs2ps) : COLOR { const float2 uv = vs2ps.sprite_uvs * vs2ps.uv_scale_offset.xy + vs2ps.uv_scale_offset.zw; float4 pixel = tex2D(font_sampler, uv).zwxy; if (vs2ps.channel_add.x) // !4444 { pixel = dot(pixel, vs2ps.channel_dot) + vs2ps.channel_add; } return saturate(pixel * vs2ps.colour); } technique no_ps_indexing_technique { pass pass0 { VertexShader = compile vs_3_0 font_vs3(); PixelShader = compile ps_3_0 font_ps3(); PointSpriteEnable = true /*RESTORE*/; HighPrecisionBlendEnable = true /*RESTORE*/; // HALFPIXELOFFSET = false; //*RESTORE*//; // as per dx1..9 CullMode = None; AlphaBlendEnable = true; SrcBlend = SrcAlpha; DestBlend = InvSrcAlpha; AlphaRef = 1; AlphaTestEnable = true; ZEnable = false; ZWriteEnable = false; } }