From 0c527fc9d285f895da73314743a447645f5e3c21 Mon Sep 17 00:00:00 2001 From: Nick Blakely Date: Fri, 6 Nov 2020 11:22:56 -0800 Subject: [PATCH] Optimizations to landscape shader. Use shared variables in shaders. --- Shaders/Shaders.vcxproj | 30 ++-- Shaders/Shaders.vcxproj.filters | 9 +- Shaders/fx/LandBump.fx | 81 ++++------ Shaders/fx/Object.fx | 41 ++--- Shaders/fx/ObjectVertexLighting.fx | 239 +++++++++++++++++++++++++++++ Shaders/fx/Ocean.fx | 25 ++- Shaders/fx/Water.fx | 12 +- Shaders/fxh/SystemVariables.fxh | 33 ++++ 8 files changed, 353 insertions(+), 117 deletions(-) create mode 100644 Shaders/fx/ObjectVertexLighting.fx create mode 100644 Shaders/fxh/SystemVariables.fxh diff --git a/Shaders/Shaders.vcxproj b/Shaders/Shaders.vcxproj index d9f6247..2bc2a53 100644 --- a/Shaders/Shaders.vcxproj +++ b/Shaders/Shaders.vcxproj @@ -63,59 +63,63 @@ Document - "$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo - "$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo fxo\%(Filename).fxo fxo\%(Filename).fxo Document - "$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo /Fc fxo\%(Filename).txt - "$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo /Fc fxo\%(Filename).txt + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo fxo\%(Filename).fxo fxo\%(Filename).fxo - + Document - "$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo - "$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo fxo\%(Filename).fxo fxo\%(Filename).fxo - + "$(JUN10SDKUTILPATH)\fxc.exe" /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo "$(JUN10SDKUTILPATH)\fxc.exe" /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo Document fxo\%(Filename).fxo fxo\%(Filename).fxo - "$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo - "$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo Document - "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo - "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo fxo\%(Filename).fxo fxo\%(Filename).fxo - + + true + true + true true + diff --git a/Shaders/Shaders.vcxproj.filters b/Shaders/Shaders.vcxproj.filters index 0345678..0f6411e 100644 --- a/Shaders/Shaders.vcxproj.filters +++ b/Shaders/Shaders.vcxproj.filters @@ -3,7 +3,6 @@ - @@ -16,7 +15,6 @@ - Include @@ -24,8 +22,15 @@ Include + + + + + + Include + \ No newline at end of file diff --git a/Shaders/fx/LandBump.fx b/Shaders/fx/LandBump.fx index 99566b3..3c77b4d 100644 --- a/Shaders/fx/LandBump.fx +++ b/Shaders/fx/LandBump.fx @@ -4,36 +4,19 @@ // Land bumpmapping and lighting shader. //-------------------------------------------------------------------------------------- -#include "../fxh/Constants.fxh" +#include "../fxh/SystemVariables.fxh" -float4x4 g_mWorldViewProjection : WorldViewProjection; -float4x4 g_World : World; - -float4x4 g_TexGenTransform0 : TexGenTransform0; -float4x4 g_TexGenTransform1 : TexGenTransform1; -float4x4 g_ShoreGen : TexGenTransform2; - -float4 g_LightDiffuseColors[MAX_LIGHTS] : PointLightDiffuse; -float3 g_LightPositions[MAX_LIGHTS] : PointLightPosition; -float g_LightRangeSquared[MAX_LIGHTS] : PointLightRange; -bool g_LightEnabled[MAX_LIGHTS] : PointLightEnabled; - -float4 g_TextureFactor : TextureFactor; +//float4x4 g_texGenMatrix2 : TexGenTransform2; // Shore texgen ////////////////////////////////////////////////////// -texture g_LandTexture : Texture0; -texture g_LandBumpTexture : Texture1; -//texture g_LandDetailTexture : Texture2; -//texture g_ShoreTexture : Texture3; - //-------------------------------------------------------------------------------------- // Texture samplers //-------------------------------------------------------------------------------------- sampler g_LandTextureSampler = sampler_state { - Texture = ; + Texture = ; MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; @@ -42,7 +25,7 @@ sampler_state sampler g_LandBumpTextureSampler = sampler_state { - Texture = ; + Texture = ; MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; @@ -78,7 +61,7 @@ struct VS_OUTPUT_BUMP float2 LandTextureUV : TEXCOORD1; float3 WorldPos : TEXCOORD2; float3 Normal : TEXCOORD3; - float3 ShoreTextureUV : TEXCOORD4; + //float3 ShoreTextureUV : TEXCOORD4; }; float4 bx2(float4 x) @@ -95,7 +78,7 @@ VS_OUTPUT_BUMP LandBumpVS( VS_OUTPUT_BUMP Output; // Transform the position from object space to homogeneous projection space - Output.Position = mul(vPos, g_mWorldViewProjection); + Output.Position = mul(vPos, g_WorldViewProjection); Output.LandBumpDiffuse = vDiffuse2 * .5f; Output.LandBumpDiffuse.a = 1.0f; @@ -106,9 +89,9 @@ VS_OUTPUT_BUMP LandBumpVS( Output.WorldPos = mul(vPos, g_World); // Set dynamically generated tex coords - Output.LandBumpTextureUV = mul(vPos, g_TexGenTransform0); - Output.LandTextureUV = mul(vPos, g_TexGenTransform1); - Output.ShoreTextureUV = mul(vPos, g_ShoreGen); + Output.LandBumpTextureUV = mul(vPos, g_texGenMatrix0); + Output.LandTextureUV = mul(vPos, g_texGenMatrix1); + //Output.ShoreTextureUV = mul(vPos, g_texGenMatrix2); // Transform the normal from object space to world space Output.Normal = normalize(mul(vNormal, (float3x3)g_World)); // normal (world space) @@ -125,23 +108,20 @@ float4 LandBumpPS(VS_OUTPUT_BUMP input) : COLOR0 normalMap = saturate((float4)dot((float3)normal, (float3)normalcol)).xyz; float3 finalColor = 2.0 * (normalMap * (tex2D(g_LandTextureSampler, input.LandTextureUV)) + input.LandBumpDiffuse); - for (int i = 0; i < MAX_LIGHTS; i++) + for (int i = 0; i < g_numPointLights; i++) { - if (g_LightEnabled[i]) - { - // Get light direction for this fragment - float3 lightDir = normalize(input.WorldPos - g_LightPositions[i]); // per pixel diffuse lighting + // Get light direction for this fragment + float3 lightDir = normalize(input.WorldPos - g_PointLightPosition[i]); // per pixel diffuse lighting - // Note: Non-uniform scaling not supported - float diffuseLighting = saturate(dot(input.Normal, -lightDir)); + // Note: Non-uniform scaling not supported + float diffuseLighting = saturate(dot(input.Normal, -lightDir)); - // Introduce fall-off of light intensity - diffuseLighting *= (g_LightRangeSquared[i] / dot(g_LightPositions[i] - input.WorldPos, g_LightPositions[i] - input.WorldPos)); + // Introduce fall-off of light intensity + diffuseLighting *= (g_PointLightRangeSquared[i] / dot(g_PointLightPosition[i] - input.WorldPos, g_PointLightPosition[i] - input.WorldPos)); - float4 diffuseColor = diffuseLighting * g_LightDiffuseColors[i]; + float4 diffuseColor = diffuseLighting * g_PointLightDiffuse[i]; - finalColor += diffuseColor; - } + finalColor += diffuseColor; } return float4(finalColor, 1); @@ -176,7 +156,7 @@ VS_OUTPUT LandscapeVS( VS_OUTPUT Output; // Transform the position from object space to homogeneous projection space - Output.Position = mul(vPos, g_mWorldViewProjection); + Output.Position = mul(vPos, g_WorldViewProjection); // Transform the normal from object space to world space Output.Normal = normalize(mul(vNormal, (float3x3)g_World)); // normal (world space) @@ -187,7 +167,7 @@ VS_OUTPUT LandscapeVS( Output.WorldPos = mul(vPos, g_World); // Set dynamically generated tex coords - Output.TextureUV = mul(vPos, g_TexGenTransform0); + Output.TextureUV = mul(vPos, g_texGenMatrix0); return Output; } @@ -196,23 +176,20 @@ float4 LandscapePS(VS_OUTPUT input) : COLOR0 { float4 finalColor = 0; - for (int i = 0; i < MAX_LIGHTS; i++) + for (int i = 0; i < g_numPointLights; i++) { - if (g_LightEnabled[i]) - { - // Get light direction for this fragment - float3 lightDir = normalize(input.WorldPos - g_LightPositions[i]); // per pixel diffuse lighting + // Get light direction for this fragment + float3 lightDir = normalize(input.WorldPos - g_PointLightPosition[i]); // per pixel diffuse lighting - // Note: Non-uniform scaling not supported - float diffuseLighting = saturate(dot(input.Normal, -lightDir)); + // Note: Non-uniform scaling not supported + float diffuseLighting = saturate(dot(input.Normal, -lightDir)); - // Introduce fall-off of light intensity - diffuseLighting *= (g_LightRangeSquared[i] / dot(g_LightPositions[i] - input.WorldPos, g_LightPositions[i] - input.WorldPos)); + // Introduce fall-off of light intensity + diffuseLighting *= (g_PointLightRangeSquared[i] / dot(g_PointLightPosition[i] - input.WorldPos, g_PointLightPosition[i] - input.WorldPos)); - float4 diffuseColor = diffuseLighting * g_LightDiffuseColors[i]; + float4 diffuseColor = diffuseLighting * g_PointLightDiffuse[i]; - finalColor += diffuseColor; - } + finalColor += diffuseColor; } float3 texel = tex2D(g_LandTextureSampler, input.TextureUV); diff --git a/Shaders/fx/Object.fx b/Shaders/fx/Object.fx index f684188..6efe5b5 100644 --- a/Shaders/fx/Object.fx +++ b/Shaders/fx/Object.fx @@ -1,39 +1,14 @@ -#include "../fxh/Constants.fxh" -#include "../fxh/Lighting.fxh" - -// Lighting state -float4 g_DirectionalAmbientLightSum : DirectionalLightAmbientSum; -float4 g_DirectionalLightDiffuse[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightDiffuse; -float3 g_DirectionalLightDirection[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightDirection; -float4 g_DirectionalLightSpecular[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightSpecular; -int g_numDirectionalLights : DirectionalLightCount; - -// Texturing and blending -TextureBlendStage g_blendStages[MAX_BLEND_STAGES] : BlendStages; -int g_numBlendStages : BlendStageCount; -float4 g_textureFactor : TextureFactor; - -texture g_ObjTexture : Texture0; +#include "../fxh/SystemVariables.fxh" sampler g_ObjTextureSampler = sampler_state { - Texture = ; + Texture = ; MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; }; -// Camera -float3 g_CameraPosition : CameraPosition; - -// Current material -Material g_Material : Material; - -// Transforms -float4x4 g_WorldViewProjection : WorldViewProjection; -float4x4 g_World : World; - // ======================================================= // Textured per pixel lighting // @@ -197,6 +172,7 @@ struct ColorPerVertexVSOutput float4 Pos : POSITION; float2 Tex0 : TEXCOORD0; float4 Color : COLOR0; + float Fog : FOG; }; float4 ColorPerVertexPS(ColorPerVertexVSOutput input) : COLOR0 @@ -205,10 +181,14 @@ float4 ColorPerVertexPS(ColorPerVertexVSOutput input) : COLOR0 return color; } +float fFogStart = 25.f; +float fFogEnd = 1525.f; + ColorPerVertexVSOutput ColorPerVertexVS( float4 vPosition : POSITION0, float2 tc : TEXCOORD0, - float4 color : COLOR0) + float4 color : COLOR0, + float fog : FOG) { // Simple transform, pre-compute as much as we can for the pixel shader ColorPerVertexVSOutput output = (ColorPerVertexVSOutput)0; @@ -216,6 +196,11 @@ ColorPerVertexVSOutput ColorPerVertexVS( output.Pos = mul(vPosition, g_WorldViewProjection); output.Tex0 = tc; output.Color = color; + + float3 P = mul(vPosition, g_WorldView); //position in view space + float d = length(P); + + output.Fog = 0.0; //saturate((fFogEnd - d) / (fFogEnd - fFogStart)); return output; } diff --git a/Shaders/fx/ObjectVertexLighting.fx b/Shaders/fx/ObjectVertexLighting.fx new file mode 100644 index 0000000..2f0efce --- /dev/null +++ b/Shaders/fx/ObjectVertexLighting.fx @@ -0,0 +1,239 @@ +#include "../fxh/Constants.fxh" +#include "../fxh/Lighting.fxh" + +// Lighting state +float4 g_DirectionalLightAmbient[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightAmbient; +float4 g_DirectionalLightDiffuse[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightDiffuse; +float3 g_DirectionalLightDirection[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightDirection; +bool g_DirectionalLightEnabled[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightEnabled; +float4 g_DirectionalLightSpecular[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightSpecular; + +texture g_ObjTexture : ObjTexture; +sampler g_ObjTextureSampler = +sampler_state +{ + Texture = ; + MipFilter = LINEAR; + MinFilter = LINEAR; + MagFilter = LINEAR; +}; + +// Camera +float3 g_CameraPosition : CameraPosition; + +// Current material +Material g_Material : Material; + +// Transforms +float4x4 g_WorldViewProjection : WorldViewProjection; +float4x4 g_World : World; + +struct VSOutputLit +{ + float4 Pos : POSITION; + float4 Diffuse : COLOR0; + float4 Specular : COLOR1; + float4 Tex0 : TEXCOORD0; + float3 Normal : TEXCOORD1; + float3 WorldPos : TEXCOORD2; +}; + +struct VSOutput +{ + float4 Pos : POSITION; + float4 Tex0 : TEXCOORD0; + float3 Normal : TEXCOORD1; + float3 WorldPos : TEXCOORD2; +}; + +float4 CalculateAmbientLight() +{ + float4 ambient = 0; + for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++) + { + if (g_DirectionalLightEnabled[i]) + { + ambient += g_DirectionalLightAmbient[i]; + } + } + + return ambient; +} + +float4 CalculateDiffuse(float3 N, float3 L, float4 diffuseColor) +{ + float NDotL = dot(N, L); + float4 finalColor = 0; + if (NDotL > 0.0f) + { + finalColor = max(0, NDotL * diffuseColor); + } + + return finalColor; +} + +float4 CalculateSpecular(float3 worldPos, float3 N, float3 L, float4 specularColor) +{ + float4 finalColor = 0; + if (g_Material.Power > 0) + { + float3 toEye = normalize(g_CameraPosition.xyz - worldPos); + float3 halfway = normalize(toEye + L); + float NDotH = saturate(dot(halfway, N)); + + finalColor = max(0, pow(NDotH, g_Material.Power) * specularColor); + } + + return finalColor; +} + +Lighting DoDirectionalLight(float3 worldPos, float3 N, int i) +{ + Lighting Out; + Out.Diffuse = CalculateDiffuse( + N, + -g_DirectionalLightDirection[i], + g_DirectionalLightDiffuse[i]); + Out.Specular = CalculateSpecular( + worldPos, + N, + -g_DirectionalLightDirection[i], + g_DirectionalLightSpecular[i]); + return Out; +} + +Lighting ComputeLighting(float3 worldPos, float3 N) +{ + Lighting finalLighting = (Lighting)0; + + for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++) + { + if (g_DirectionalLightEnabled[i]) + { + Lighting lighting = DoDirectionalLight(worldPos, N, i); + finalLighting.Diffuse += lighting.Diffuse; + finalLighting.Specular += lighting.Specular; + } + } + + float4 ambient = g_Material.Ambient * CalculateAmbientLight(); + float4 diffuse = g_Material.Diffuse * finalLighting.Diffuse; + float4 specular = g_Material.Specular * finalLighting.Specular; + + finalLighting.Diffuse = saturate(ambient + diffuse); + finalLighting.Specular = saturate(specular); + + return finalLighting; +} + +//----------------------------------------------------------------------------- +// Name: DoPointLight() +// Desc: Point light computation +//----------------------------------------------------------------------------- +//COLOR_PAIR DoPointLight(float4 vPosition, float3 N, float3 V, int i) +//{ +// float3 L = mul((float3x3)matViewIT, normalize((lights[i].vPos-(float3)mul(matWorld,vPosition)))); +// COLOR_PAIR Out; +// float NdotL = dot(N, L); +// Out.Color = lights[i].vAmbient; +// Out.Specular = 0; +// float fAtten = 1.f; +// if(NdotL >= 0.f) +// { +// //compute diffuse color +// Out.Color += NdotL * lights[i].vDiffuse; +// +// //add specular component +// if(bSpecular) +// { +// float3 H = normalize(L + V); //half vector +// Out.Specular = pow(max(0, dot(H, N)), fMaterialPower) * lights[i].vSpecular; +// } +// +// float LD = length(lights[i].vPos-(float3)mul(matWorld,vPosition)); +// if(LD > lights[i].fRange) +// { +// fAtten = 0.f; +// } +// else +// { +// fAtten *= 1.f/(lights[i].vAttenuation.x + lights[i].vAttenuation.y*LD + lights[i].vAttenuation.z*LD*LD); +// } +// Out.Color *= fAtten; +// Out.Specular *= fAtten; +// } +// return Out; +//} + +VSOutputLit VSMainLighting( + float4 vPosition : POSITION0, + float3 vNormal : NORMAL0, + float2 tc : TEXCOORD0) +{ + VSOutputLit Out = (VSOutputLit)0; + + vNormal = normalize(vNormal); + Out.Pos = mul(vPosition, g_WorldViewProjection); + + //automatic texture coordinate generation + Out.Tex0.xy = tc; + + //directional lights + float4 worldPos = mul(vPosition, g_World); //position in view space + float3 normal = mul(vNormal, (float3x3)g_World); + Lighting lighting = ComputeLighting(worldPos, normal); + + ////point lights + //for(int i = 0; i < iLightPointNum; i++) + //{ + // COLOR_PAIR ColOut = DoPointLight(vPosition, N, V, i+iLightPointIni); + // Out.Color += ColOut.Color; + // Out.Specular += ColOut.Specular; + //} + + Out.Diffuse = lighting.Diffuse; + Out.Specular = lighting.Specular; + + return Out; +} + +VSOutput VSMain( + float4 vPosition : POSITION0, + float3 vNormal : NORMAL0, + float2 tc : TEXCOORD0) +{ + VSOutput Out = (VSOutput)0; + Out.Pos = mul(vPosition, g_WorldViewProjection); + Out.Normal = normalize(vNormal); + Out.WorldPos = mul(vPosition, g_World); + Out.Tex0.xy = tc; + return Out; +} + +float4 PSMain(VSOutput input) : COLOR0 +{ + float4 color = tex2D(g_ObjTextureSampler, input.Tex0); + + Lighting lighting = ComputeLighting(input.WorldPos, input.Normal); + + color = (lighting.Diffuse + lighting.Specular) * color; + return color; +} + +technique TexturedVertexLighting +{ + pass P0 + { + //PixelShader = compile ps_2_0 ps_main(); + VertexShader = compile vs_2_0 VSMainLighting(); + } +} + +technique TexturedPixelLighting +{ + pass P0 + { + PixelShader = compile ps_2_0 PSMain(); + VertexShader = compile vs_2_0 VSMain(); + } +} \ No newline at end of file diff --git a/Shaders/fx/Ocean.fx b/Shaders/fx/Ocean.fx index 8383aba..19d608a 100644 --- a/Shaders/fx/Ocean.fx +++ b/Shaders/fx/Ocean.fx @@ -4,6 +4,8 @@ // Ocean water reflection shader. //-------------------------------------------------------------------------------------- +#include "../fxh/SystemVariables.fxh" + /* Original asm code: ps_1_1 tex t0 @@ -32,18 +34,13 @@ float4 MainPS(const PS_INPUT input) : COLOR0 // C26: {minfog, maxfog, 1.0f / (maxfog - minfog), 1.0} float4 const44 : register(c44); -matrix matWorld : OceanWorldViewProjection : register(c2); +shared matrix g_OceanWorldViewProjection : OceanWorldViewProjection : register(c2); float4 fog : register (c26); -texture g_SeabedTexture : Texture0; -texture g_EnvironmentTexture : Texture1; struct VS_OUTPUT { float4 pos : POSITION; float4 texCoord1 : TEXCOORD1; - //float4 texCoord0 : TEXCOORD0; - //float4 color : COLOR0; - //float fog : FOG; }; struct VS_INPUT @@ -56,7 +53,7 @@ VS_OUTPUT MainVS(const VS_INPUT input) float4 const27 = {0.0f, 1.0f, 0.5f, 0.75f}; VS_OUTPUT output; - output.pos = mul(input.pos, matWorld); + output.pos = mul(input.pos, g_OceanWorldViewProjection); // add r0, v0, -c44 float4 r0 = input.pos + -const44; @@ -87,17 +84,17 @@ technique t0 { pass p0 { - Texture[0] = ; // Seabed texture - Texture[1] = ; // Environment texture + Texture[0] = ; // Seabed texture + Texture[1] = ; // Environment texture // All of these constants are set by the game engine before drawing the shader // Each constant register (c# in the asm code) has 4 floating point values - // World view matrix - VertexShaderConstant[2] = ; - VertexShaderConstant[3] = ; - VertexShaderConstant[4] = ; - VertexShaderConstant[5] = ; + // Special world * view * projection matrix for ocean shader + VertexShaderConstant[2] = ; + VertexShaderConstant[3] = ; + VertexShaderConstant[4] = ; + VertexShaderConstant[5] = ; VertexShaderConstant[26] = ; // This constant is calculated from the current fog min/max values VertexShaderConstant[27] = {0.0f, 1.0f, 0.5f, 0.75f}; // I don't know what this does but it doesn't change diff --git a/Shaders/fx/Water.fx b/Shaders/fx/Water.fx index 07c0a63..404cccb 100644 --- a/Shaders/fx/Water.fx +++ b/Shaders/fx/Water.fx @@ -5,15 +5,12 @@ // the original fixed-function water rendering behavior. //-------------------------------------------------------------------------------------- -float4x4 g_WorldViewProjection : WorldViewProjection; -float4 g_TextureFactor : TextureFactor; -//float4 g_Fog : Fog; +#include "../fxh/SystemVariables.fxh" -texture2D g_WaterTexture : Texture0; sampler2D g_WaterTextureSampler = sampler_state { - Texture = ; + Texture = ; }; struct VS_OUTPUT @@ -21,7 +18,6 @@ struct VS_OUTPUT float4 Position : POSITION; // vertex position float2 TextureUV : TEXCOORD0; // vertex texture coords float4 Color : COLOR0; - //float Fog : FOG; }; VS_OUTPUT RenderSceneVS(float4 inPos : POSITION, @@ -46,8 +42,8 @@ VS_OUTPUT RenderSceneVS(float4 inPos : POSITION, float4 RenderScenePS(VS_OUTPUT input) : COLOR0 { float4 texel = tex2D(g_WaterTextureSampler, input.TextureUV); - float4 result = saturate(texel + (1 - texel) * g_TextureFactor); // equivalent to saturate((texel + g_TextureFactor) - (texel * g_TextureFactor)); - result.a = input.Color.a * g_TextureFactor.a; + float4 result = saturate(texel + (1 - texel) * g_textureFactor); // equivalent to saturate((texel + g_TextureFactor) - (texel * g_TextureFactor)); + result.a = input.Color.a * g_textureFactor.a; return result; } diff --git a/Shaders/fxh/SystemVariables.fxh b/Shaders/fxh/SystemVariables.fxh new file mode 100644 index 0000000..7e6faa8 --- /dev/null +++ b/Shaders/fxh/SystemVariables.fxh @@ -0,0 +1,33 @@ +#include "Constants.fxh" +#include "Lighting.fxh" + +// Lighting +shared float4 g_DirectionalAmbientLightSum : DirectionalLightAmbientSum; +shared float4 g_DirectionalLightDiffuse[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightDiffuse; +shared float3 g_DirectionalLightDirection[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightDirection; +shared float4 g_DirectionalLightSpecular[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightSpecular; +shared int g_numDirectionalLights : DirectionalLightCount; +shared float4 g_PointLightDiffuse[MAX_LIGHTS] : PointLightDiffuse; +shared float3 g_PointLightPosition[MAX_LIGHTS] : PointLightPosition; +shared float g_PointLightRangeSquared[MAX_LIGHTS] : PointLightRange; +shared int g_numPointLights : PointLightCount; +shared Material g_Material : Material; + +// Camera +shared float3 g_CameraPosition : CameraPosition; + +// Transforms +shared float4x4 g_WorldViewProjection : WorldViewProjection; +shared float4x4 g_WorldView : WorldView; +shared float4x4 g_World : World; + +// Texturing +shared texture g_texture0 : Texture0; +shared texture g_texture1 : Texture1; +shared texture g_texture2 : Texture2; +shared texture g_texture3 : Texture3; +shared float4x4 g_texGenMatrix0 : TexGenTransform0; +shared float4x4 g_texGenMatrix1 : TexGenTransform1; +shared float4 g_textureFactor : TextureFactor; +shared TextureBlendStage g_blendStages[MAX_BLEND_STAGES] : BlendStages; +shared int g_numBlendStages : BlendStageCount; \ No newline at end of file