From 11625b367c85ccc4e817a9d9c4c0f52cf78c302d Mon Sep 17 00:00:00 2001 From: Nick Blakely Date: Sun, 8 Nov 2020 18:02:39 -0800 Subject: [PATCH] Use global variables with static names instead of annotations. --- Shaders/Shaders.vcxproj | 3 - Shaders/Shaders.vcxproj.filters | 8 --- Shaders/fx/LandBump.fx | 34 +++++++---- Shaders/fx/Object.fx | 99 ++++++++++++++++++++++++++------- Shaders/fx/Ocean.fx | 9 +-- Shaders/fx/Water.fx | 12 ++-- Shaders/fxh/SystemVariables.fxh | 33 ----------- 7 files changed, 117 insertions(+), 81 deletions(-) delete mode 100644 Shaders/fxh/SystemVariables.fxh diff --git a/Shaders/Shaders.vcxproj b/Shaders/Shaders.vcxproj index 2bc2a53..74a1709 100644 --- a/Shaders/Shaders.vcxproj +++ b/Shaders/Shaders.vcxproj @@ -118,9 +118,6 @@ true - - - diff --git a/Shaders/Shaders.vcxproj.filters b/Shaders/Shaders.vcxproj.filters index 0f6411e..694d57b 100644 --- a/Shaders/Shaders.vcxproj.filters +++ b/Shaders/Shaders.vcxproj.filters @@ -10,9 +10,6 @@ {026956f1-0f27-4b3d-80ab-f81eafce58f5} - - {0ead58ec-1210-4df1-88c8-dfd73f6fb054} - @@ -28,9 +25,4 @@ - - - Include - - \ No newline at end of file diff --git a/Shaders/fx/LandBump.fx b/Shaders/fx/LandBump.fx index 3c77b4d..667a12c 100644 --- a/Shaders/fx/LandBump.fx +++ b/Shaders/fx/LandBump.fx @@ -4,7 +4,21 @@ // Land bumpmapping and lighting shader. //-------------------------------------------------------------------------------------- -#include "../fxh/SystemVariables.fxh" +#include "../fxh/Constants.fxh" +#include "../fxh/Lighting.fxh" + +shared texture g_Texture0; +shared texture g_Texture1; +shared float4x4 g_WorldViewProjection; +shared float4x4 g_World; + +shared float4x4 g_TexGenMatrix0; +shared float4x4 g_TexGenMatrix1; + +shared float4 g_PointLightDiffuse[MAX_LIGHTS]; +shared float3 g_PointLightPosition[MAX_LIGHTS]; +shared float g_PointLightRange[MAX_LIGHTS]; +shared int g_PointLightCount; //float4x4 g_texGenMatrix2 : TexGenTransform2; // Shore texgen @@ -16,7 +30,7 @@ sampler g_LandTextureSampler = sampler_state { - Texture = ; + Texture = ; MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; @@ -25,7 +39,7 @@ sampler_state sampler g_LandBumpTextureSampler = sampler_state { - Texture = ; + Texture = ; MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; @@ -89,8 +103,8 @@ VS_OUTPUT_BUMP LandBumpVS( Output.WorldPos = mul(vPos, g_World); // Set dynamically generated tex coords - Output.LandBumpTextureUV = mul(vPos, g_texGenMatrix0); - Output.LandTextureUV = mul(vPos, g_texGenMatrix1); + 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 @@ -108,7 +122,7 @@ 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 < g_numPointLights; i++) + for (int i = 0; i < g_PointLightCount; i++) { // Get light direction for this fragment float3 lightDir = normalize(input.WorldPos - g_PointLightPosition[i]); // per pixel diffuse lighting @@ -117,7 +131,7 @@ float4 LandBumpPS(VS_OUTPUT_BUMP input) : COLOR0 float diffuseLighting = saturate(dot(input.Normal, -lightDir)); // Introduce fall-off of light intensity - diffuseLighting *= (g_PointLightRangeSquared[i] / dot(g_PointLightPosition[i] - input.WorldPos, g_PointLightPosition[i] - input.WorldPos)); + diffuseLighting *= (g_PointLightRange[i] / dot(g_PointLightPosition[i] - input.WorldPos, g_PointLightPosition[i] - input.WorldPos)); float4 diffuseColor = diffuseLighting * g_PointLightDiffuse[i]; @@ -167,7 +181,7 @@ VS_OUTPUT LandscapeVS( Output.WorldPos = mul(vPos, g_World); // Set dynamically generated tex coords - Output.TextureUV = mul(vPos, g_texGenMatrix0); + Output.TextureUV = mul(vPos, g_TexGenMatrix0); return Output; } @@ -176,7 +190,7 @@ float4 LandscapePS(VS_OUTPUT input) : COLOR0 { float4 finalColor = 0; - for (int i = 0; i < g_numPointLights; i++) + for (int i = 0; i < g_PointLightCount; i++) { // Get light direction for this fragment float3 lightDir = normalize(input.WorldPos - g_PointLightPosition[i]); // per pixel diffuse lighting @@ -185,7 +199,7 @@ float4 LandscapePS(VS_OUTPUT input) : COLOR0 float diffuseLighting = saturate(dot(input.Normal, -lightDir)); // Introduce fall-off of light intensity - diffuseLighting *= (g_PointLightRangeSquared[i] / dot(g_PointLightPosition[i] - input.WorldPos, g_PointLightPosition[i] - input.WorldPos)); + diffuseLighting *= (g_PointLightRange[i] / dot(g_PointLightPosition[i] - input.WorldPos, g_PointLightPosition[i] - input.WorldPos)); float4 diffuseColor = diffuseLighting * g_PointLightDiffuse[i]; diff --git a/Shaders/fx/Object.fx b/Shaders/fx/Object.fx index 6efe5b5..d1df8cd 100644 --- a/Shaders/fx/Object.fx +++ b/Shaders/fx/Object.fx @@ -1,16 +1,35 @@ -#include "../fxh/SystemVariables.fxh" +#include "../fxh/Constants.fxh" +#include "../fxh/Lighting.fxh" -sampler g_ObjTextureSampler = +shared texture g_Texture0 : Texture0; + +shared float4 g_DirectionalLightAmbientSum; +shared float4 g_DirectionalLightDiffuse[MAX_DIRECTIONAL_LIGHTS]; +shared float3 g_DirectionalLightDirection[MAX_DIRECTIONAL_LIGHTS]; +shared float4 g_DirectionalLightSpecular[MAX_DIRECTIONAL_LIGHTS]; +shared int g_DirectionalLightCount; +shared Material g_Material; + +shared float4x4 g_WorldViewProjection; +shared float4x4 g_WorldView; +shared float4x4 g_World; + +shared float4 g_TextureFactor; +shared TextureBlendStage g_BlendStages[MAX_BLEND_STAGES]; +shared int g_BlendStageCount; +shared float3 g_CameraPosition; + +sampler g_ObjTextureSampler : register(s0) = sampler_state { - Texture = ; + Texture = ; MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; }; // ======================================================= -// Textured per pixel lighting +// Pixel and vertex lighting techniques // float4 CalculateDiffuse(float3 N, float3 L, float4 diffuseColor) @@ -59,18 +78,18 @@ Lighting ComputeLighting(float3 worldPos, float3 N) { Lighting finalLighting = (Lighting)0; - for (int i = 0; i < g_numDirectionalLights; i++) + for (int i = 0; i < g_DirectionalLightCount; i++) { Lighting lighting = DoDirectionalLight(worldPos, N, i); finalLighting.Diffuse += lighting.Diffuse; finalLighting.Specular += lighting.Specular; } - float4 ambient = g_Material.Ambient * g_DirectionalAmbientLightSum; + float4 ambient = g_Material.Ambient * g_DirectionalLightAmbientSum; float4 diffuse = g_Material.Diffuse * finalLighting.Diffuse; float4 specular = g_Material.Specular * finalLighting.Specular; - finalLighting.Diffuse = saturate(ambient + diffuse); + finalLighting.Diffuse = saturate(ambient + diffuse + g_Material.Emissive); finalLighting.Specular = saturate(specular); return finalLighting; @@ -104,7 +123,7 @@ float4 GetColorArg(int colorArg, float4 textureColor, float4 diffuseColor) float4 result; if (colorArg == D3DTA_TEXTURE) result = textureColor; else if (colorArg == D3DTA_DIFFUSE) result = diffuseColor; - else if (colorArg == D3DTA_TFACTOR) result = g_textureFactor; + else if (colorArg == D3DTA_TFACTOR) result = g_TextureFactor; else result = float4(1.f, 1.f, 1.f, 1.f); return result; @@ -112,8 +131,8 @@ float4 GetColorArg(int colorArg, float4 textureColor, float4 diffuseColor) float4 Modulate(int stageIndex, float4 textureColor, float4 diffuseColor, float factor) { - float4 left = GetColorArg(g_blendStages[stageIndex].colorArg1, textureColor, diffuseColor); - float4 right = GetColorArg(g_blendStages[stageIndex].colorArg2, textureColor, diffuseColor); + float4 left = GetColorArg(g_BlendStages[stageIndex].colorArg1, textureColor, diffuseColor); + float4 right = GetColorArg(g_BlendStages[stageIndex].colorArg2, textureColor, diffuseColor); return (left * right) * factor; } @@ -121,9 +140,9 @@ float4 Modulate(int stageIndex, float4 textureColor, float4 diffuseColor, float float4 ProcessStages(float4 textureColor, float4 diffuseColor) { float4 output = 0; - for (int i = 0; i < g_numBlendStages; i++) + for (int i = 0; i < g_BlendStageCount; i++) { - if (g_blendStages[i].colorOp == D3DTOP_MODULATE4X) + if (g_BlendStages[i].colorOp == D3DTOP_MODULATE4X) { output += Modulate(i, textureColor, diffuseColor, 4.0f); } @@ -139,7 +158,6 @@ float4 ProcessStages(float4 textureColor, float4 diffuseColor) struct PixelLightingPSOutput { float4 Diffuse : COLOR0; - float4 Specular : COLOR1; }; PixelLightingPSOutput PixelLightingPS(PixelLightingVSOutput input) @@ -149,8 +167,7 @@ PixelLightingPSOutput PixelLightingPS(PixelLightingVSOutput input) Lighting lighting = ComputeLighting(input.WorldPos, input.Normal); PixelLightingPSOutput output; - output.Diffuse = ProcessStages(color, lighting.Diffuse); - output.Specular = color * lighting.Specular; + output.Diffuse = ProcessStages(color, lighting.Diffuse) + lighting.Specular; return output; } @@ -163,6 +180,53 @@ technique PixelLighting } } +struct VertexLightingVSOutput +{ + float4 Pos : POSITION; + float4 Diffuse : COLOR0; + float4 Specular : COLOR1; + float2 Tex0 : TEXCOORD0; + float4 BumpColor : TEXCOORD1; + float Fog : FOG; +}; + +float fFogStart = 25.f; +float fFogEnd = 1525.f; + +VertexLightingVSOutput VertexLightingVS( + float4 vPosition : POSITION0, + float3 vNormal : NORMAL0, + float4 color : COLOR0, + float2 tc : TEXCOORD0) +{ + VertexLightingVSOutput output; + output.Pos = mul(vPosition, g_WorldViewProjection); + output.Tex0 = tc; + + float4 worldPos = mul(vPosition, g_World); + float3 normal = mul(normalize(vNormal), (float3x3)g_World); + + Lighting lighting = ComputeLighting(worldPos, normal); + + output.Diffuse = lighting.Diffuse; + output.Specular = lighting.Specular; + output.BumpColor = color; + + float3 P = mul(vPosition, g_WorldView); + float d = length(P); + output.Fog = saturate((fFogEnd - d) / (fFogEnd - fFogStart)); + + return output; +} + +technique VertexLighting +{ + pass P0 + { + VertexShader = compile vs_3_0 VertexLightingVS(); + } +} + // ======================================================= // Color per vertex // @@ -181,9 +245,6 @@ float4 ColorPerVertexPS(ColorPerVertexVSOutput input) : COLOR0 return color; } -float fFogStart = 25.f; -float fFogEnd = 1525.f; - ColorPerVertexVSOutput ColorPerVertexVS( float4 vPosition : POSITION0, float2 tc : TEXCOORD0, @@ -200,7 +261,7 @@ ColorPerVertexVSOutput ColorPerVertexVS( float3 P = mul(vPosition, g_WorldView); //position in view space float d = length(P); - output.Fog = 0.0; //saturate((fFogEnd - d) / (fFogEnd - fFogStart)); + output.Fog = saturate((fFogEnd - d) / (fFogEnd - fFogStart)); return output; } diff --git a/Shaders/fx/Ocean.fx b/Shaders/fx/Ocean.fx index 19d608a..7fafe8f 100644 --- a/Shaders/fx/Ocean.fx +++ b/Shaders/fx/Ocean.fx @@ -4,8 +4,6 @@ // Ocean water reflection shader. //-------------------------------------------------------------------------------------- -#include "../fxh/SystemVariables.fxh" - /* Original asm code: ps_1_1 tex t0 @@ -13,6 +11,9 @@ lrp r0, -v0.w, t0, t1 // lrp = linear interpolate */ +shared texture g_Texture0; +shared texture g_Texture1; + struct PS_INPUT { float4 color : COLOR0; @@ -84,8 +85,8 @@ 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 diff --git a/Shaders/fx/Water.fx b/Shaders/fx/Water.fx index 404cccb..3a9d070 100644 --- a/Shaders/fx/Water.fx +++ b/Shaders/fx/Water.fx @@ -5,12 +5,16 @@ // the original fixed-function water rendering behavior. //-------------------------------------------------------------------------------------- -#include "../fxh/SystemVariables.fxh" +//#include "../fxh/SystemVariables.fxh" + +shared texture g_Texture0; +shared float4x4 g_WorldViewProjection; +shared float4 g_TextureFactor; sampler2D g_WaterTextureSampler = sampler_state { - Texture = ; + Texture = ; }; struct VS_OUTPUT @@ -42,8 +46,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 deleted file mode 100644 index 7e6faa8..0000000 --- a/Shaders/fxh/SystemVariables.fxh +++ /dev/null @@ -1,33 +0,0 @@ -#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