1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-21 21:55:38 +01:00

Optimizations to landscape shader. Use shared variables in shaders.

This commit is contained in:
Nick Blakely 2020-11-06 11:22:56 -08:00
parent b6adcfc8b1
commit 0c527fc9d2
8 changed files with 353 additions and 117 deletions

View File

@ -63,59 +63,63 @@
<ItemGroup> <ItemGroup>
<CustomBuild Include="fx\LandBump.fx"> <CustomBuild Include="fx\LandBump.fx">
<FileType>Document</FileType> <FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command> <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command> <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxo\%(Filename).fxo</Outputs> <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxo\%(Filename).fxo</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxo\%(Filename).fxo</Outputs> <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxo\%(Filename).fxo</Outputs>
</CustomBuild> </CustomBuild>
<CustomBuild Include="fx\Ocean.fx"> <CustomBuild Include="fx\Ocean.fx">
<FileType>Document</FileType> <FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo /Fc fxo\%(Filename).txt</Command> <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo /Fc fxo\%(Filename).txt</Command> <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxo\%(Filename).fxo</Outputs> <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxo\%(Filename).fxo</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxo\%(Filename).fxo</Outputs> <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxo\%(Filename).fxo</Outputs>
</CustomBuild> </CustomBuild>
<CustomBuild Include="fx\Sky.fx"> <None Include="fx\Sky.fx">
<EntryPointName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <EntryPointName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
</EntryPointName> </EntryPointName>
<FileType>Document</FileType> <FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command> <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command> <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxo\%(Filename).fxo</Outputs> <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxo\%(Filename).fxo</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxo\%(Filename).fxo</Outputs> <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxo\%(Filename).fxo</Outputs>
</CustomBuild> </None>
<CustomBuild Include="fx\Water.fx"> <CustomBuild Include="fx\Water.fx">
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</AdditionalIncludeDirectories> <AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</AdditionalIncludeDirectories> <AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</AdditionalIncludeDirectories>
<FileType>Document</FileType> <FileType>Document</FileType>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxo\%(Filename).fxo</Outputs> <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxo\%(Filename).fxo</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxo\%(Filename).fxo</Outputs> <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxo\%(Filename).fxo</Outputs>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command> <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command> <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
</CustomBuild> </CustomBuild>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CustomBuild Include="fx\Object.fx"> <CustomBuild Include="fx\Object.fx">
<FileType>Document</FileType> <FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command> <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command> <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxo\%(Filename).fxo</Outputs> <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">fxo\%(Filename).fxo</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxo\%(Filename).fxo</Outputs> <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">fxo\%(Filename).fxo</Outputs>
</CustomBuild> </CustomBuild>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="FFP_orig.fx" />
<None Include="fxh\Constants.fxh" /> <None Include="fxh\Constants.fxh" />
<None Include="fxh\Lighting.fxh" /> <None Include="fxh\Lighting.fxh" />
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<FxCompile Include="reference\FFP_orig.fx">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</FxCompile>
<FxCompile Include="reference\FixedFuncEMU.fx"> <FxCompile Include="reference\FixedFuncEMU.fx">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</FxCompile> </FxCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="fxh\SystemVariables.fxh" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -3,7 +3,6 @@
<ItemGroup> <ItemGroup>
<CustomBuild Include="fx\LandBump.fx" /> <CustomBuild Include="fx\LandBump.fx" />
<CustomBuild Include="fx\Ocean.fx" /> <CustomBuild Include="fx\Ocean.fx" />
<CustomBuild Include="fx\Sky.fx" />
<CustomBuild Include="fx\Water.fx" /> <CustomBuild Include="fx\Water.fx" />
<CustomBuild Include="fx\Object.fx" /> <CustomBuild Include="fx\Object.fx" />
</ItemGroup> </ItemGroup>
@ -16,7 +15,6 @@
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="FFP_orig.fx" />
<None Include="fxh\Constants.fxh"> <None Include="fxh\Constants.fxh">
<Filter>Include</Filter> <Filter>Include</Filter>
</None> </None>
@ -24,8 +22,15 @@
<Filter>Include</Filter> <Filter>Include</Filter>
</None> </None>
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="fx\Sky.fx" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<FxCompile Include="reference\FixedFuncEMU.fx" /> <FxCompile Include="reference\FixedFuncEMU.fx" />
<FxCompile Include="reference\FFP_orig.fx" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="fxh\SystemVariables.fxh">
<Filter>Include</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -4,36 +4,19 @@
// Land bumpmapping and lighting shader. // Land bumpmapping and lighting shader.
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
#include "../fxh/Constants.fxh" #include "../fxh/SystemVariables.fxh"
float4x4 g_mWorldViewProjection : WorldViewProjection; //float4x4 g_texGenMatrix2 : TexGenTransform2; // Shore texgen
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;
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
texture g_LandTexture : Texture0;
texture g_LandBumpTexture : Texture1;
//texture g_LandDetailTexture : Texture2;
//texture g_ShoreTexture : Texture3;
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Texture samplers // Texture samplers
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
sampler g_LandTextureSampler = sampler g_LandTextureSampler =
sampler_state sampler_state
{ {
Texture = <g_LandTexture>; Texture = <g_texture0>;
MipFilter = LINEAR; MipFilter = LINEAR;
MinFilter = LINEAR; MinFilter = LINEAR;
MagFilter = LINEAR; MagFilter = LINEAR;
@ -42,7 +25,7 @@ sampler_state
sampler g_LandBumpTextureSampler = sampler g_LandBumpTextureSampler =
sampler_state sampler_state
{ {
Texture = <g_LandBumpTexture>; Texture = <g_texture1>;
MipFilter = LINEAR; MipFilter = LINEAR;
MinFilter = LINEAR; MinFilter = LINEAR;
MagFilter = LINEAR; MagFilter = LINEAR;
@ -78,7 +61,7 @@ struct VS_OUTPUT_BUMP
float2 LandTextureUV : TEXCOORD1; float2 LandTextureUV : TEXCOORD1;
float3 WorldPos : TEXCOORD2; float3 WorldPos : TEXCOORD2;
float3 Normal : TEXCOORD3; float3 Normal : TEXCOORD3;
float3 ShoreTextureUV : TEXCOORD4; //float3 ShoreTextureUV : TEXCOORD4;
}; };
float4 bx2(float4 x) float4 bx2(float4 x)
@ -95,7 +78,7 @@ VS_OUTPUT_BUMP LandBumpVS(
VS_OUTPUT_BUMP Output; VS_OUTPUT_BUMP Output;
// Transform the position from object space to homogeneous projection space // 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 = vDiffuse2 * .5f;
Output.LandBumpDiffuse.a = 1.0f; Output.LandBumpDiffuse.a = 1.0f;
@ -106,9 +89,9 @@ VS_OUTPUT_BUMP LandBumpVS(
Output.WorldPos = mul(vPos, g_World); Output.WorldPos = mul(vPos, g_World);
// Set dynamically generated tex coords // Set dynamically generated tex coords
Output.LandBumpTextureUV = mul(vPos, g_TexGenTransform0); Output.LandBumpTextureUV = mul(vPos, g_texGenMatrix0);
Output.LandTextureUV = mul(vPos, g_TexGenTransform1); Output.LandTextureUV = mul(vPos, g_texGenMatrix1);
Output.ShoreTextureUV = mul(vPos, g_ShoreGen); //Output.ShoreTextureUV = mul(vPos, g_texGenMatrix2);
// Transform the normal from object space to world space // Transform the normal from object space to world space
Output.Normal = normalize(mul(vNormal, (float3x3)g_World)); // normal (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; normalMap = saturate((float4)dot((float3)normal, (float3)normalcol)).xyz;
float3 finalColor = 2.0 * (normalMap * (tex2D(g_LandTextureSampler, input.LandTextureUV)) + input.LandBumpDiffuse); 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_PointLightPosition[i]); // per pixel diffuse lighting
// Get light direction for this fragment
float3 lightDir = normalize(input.WorldPos - g_LightPositions[i]); // per pixel diffuse lighting
// Note: Non-uniform scaling not supported // Note: Non-uniform scaling not supported
float diffuseLighting = saturate(dot(input.Normal, -lightDir)); float diffuseLighting = saturate(dot(input.Normal, -lightDir));
// Introduce fall-off of light intensity // Introduce fall-off of light intensity
diffuseLighting *= (g_LightRangeSquared[i] / dot(g_LightPositions[i] - input.WorldPos, g_LightPositions[i] - input.WorldPos)); 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); return float4(finalColor, 1);
@ -176,7 +156,7 @@ VS_OUTPUT LandscapeVS(
VS_OUTPUT Output; VS_OUTPUT Output;
// Transform the position from object space to homogeneous projection space // 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 // Transform the normal from object space to world space
Output.Normal = normalize(mul(vNormal, (float3x3)g_World)); // normal (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); Output.WorldPos = mul(vPos, g_World);
// Set dynamically generated tex coords // Set dynamically generated tex coords
Output.TextureUV = mul(vPos, g_TexGenTransform0); Output.TextureUV = mul(vPos, g_texGenMatrix0);
return Output; return Output;
} }
@ -196,23 +176,20 @@ float4 LandscapePS(VS_OUTPUT input) : COLOR0
{ {
float4 finalColor = 0; 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_PointLightPosition[i]); // per pixel diffuse lighting
// Get light direction for this fragment
float3 lightDir = normalize(input.WorldPos - g_LightPositions[i]); // per pixel diffuse lighting
// Note: Non-uniform scaling not supported // Note: Non-uniform scaling not supported
float diffuseLighting = saturate(dot(input.Normal, -lightDir)); float diffuseLighting = saturate(dot(input.Normal, -lightDir));
// Introduce fall-off of light intensity // Introduce fall-off of light intensity
diffuseLighting *= (g_LightRangeSquared[i] / dot(g_LightPositions[i] - input.WorldPos, g_LightPositions[i] - input.WorldPos)); 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); float3 texel = tex2D(g_LandTextureSampler, input.TextureUV);

View File

@ -1,39 +1,14 @@
#include "../fxh/Constants.fxh" #include "../fxh/SystemVariables.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;
sampler g_ObjTextureSampler = sampler g_ObjTextureSampler =
sampler_state sampler_state
{ {
Texture = <g_ObjTexture>; Texture = <g_texture0>;
MipFilter = LINEAR; MipFilter = LINEAR;
MinFilter = LINEAR; MinFilter = LINEAR;
MagFilter = 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 // Textured per pixel lighting
// //
@ -197,6 +172,7 @@ struct ColorPerVertexVSOutput
float4 Pos : POSITION; float4 Pos : POSITION;
float2 Tex0 : TEXCOORD0; float2 Tex0 : TEXCOORD0;
float4 Color : COLOR0; float4 Color : COLOR0;
float Fog : FOG;
}; };
float4 ColorPerVertexPS(ColorPerVertexVSOutput input) : COLOR0 float4 ColorPerVertexPS(ColorPerVertexVSOutput input) : COLOR0
@ -205,10 +181,14 @@ float4 ColorPerVertexPS(ColorPerVertexVSOutput input) : COLOR0
return color; return color;
} }
float fFogStart = 25.f;
float fFogEnd = 1525.f;
ColorPerVertexVSOutput ColorPerVertexVS( ColorPerVertexVSOutput ColorPerVertexVS(
float4 vPosition : POSITION0, float4 vPosition : POSITION0,
float2 tc : TEXCOORD0, 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 // Simple transform, pre-compute as much as we can for the pixel shader
ColorPerVertexVSOutput output = (ColorPerVertexVSOutput)0; ColorPerVertexVSOutput output = (ColorPerVertexVSOutput)0;
@ -216,6 +196,11 @@ ColorPerVertexVSOutput ColorPerVertexVS(
output.Pos = mul(vPosition, g_WorldViewProjection); output.Pos = mul(vPosition, g_WorldViewProjection);
output.Tex0 = tc; output.Tex0 = tc;
output.Color = color; 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; return output;
} }

View File

@ -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 = <g_ObjTexture>;
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();
}
}

View File

@ -4,6 +4,8 @@
// Ocean water reflection shader. // Ocean water reflection shader.
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
#include "../fxh/SystemVariables.fxh"
/* Original asm code: /* Original asm code:
ps_1_1 ps_1_1
tex t0 tex t0
@ -32,18 +34,13 @@ float4 MainPS(const PS_INPUT input) : COLOR0
// C26: {minfog, maxfog, 1.0f / (maxfog - minfog), 1.0} // C26: {minfog, maxfog, 1.0f / (maxfog - minfog), 1.0}
float4 const44 : register(c44); float4 const44 : register(c44);
matrix<float, 4, 4> matWorld : OceanWorldViewProjection : register(c2); shared matrix<float, 4, 4> g_OceanWorldViewProjection : OceanWorldViewProjection : register(c2);
float4 fog : register (c26); float4 fog : register (c26);
texture g_SeabedTexture : Texture0;
texture g_EnvironmentTexture : Texture1;
struct VS_OUTPUT struct VS_OUTPUT
{ {
float4 pos : POSITION; float4 pos : POSITION;
float4 texCoord1 : TEXCOORD1; float4 texCoord1 : TEXCOORD1;
//float4 texCoord0 : TEXCOORD0;
//float4 color : COLOR0;
//float fog : FOG;
}; };
struct VS_INPUT struct VS_INPUT
@ -56,7 +53,7 @@ VS_OUTPUT MainVS(const VS_INPUT input)
float4 const27 = {0.0f, 1.0f, 0.5f, 0.75f}; float4 const27 = {0.0f, 1.0f, 0.5f, 0.75f};
VS_OUTPUT output; VS_OUTPUT output;
output.pos = mul(input.pos, matWorld); output.pos = mul(input.pos, g_OceanWorldViewProjection);
// add r0, v0, -c44 // add r0, v0, -c44
float4 r0 = input.pos + -const44; float4 r0 = input.pos + -const44;
@ -87,17 +84,17 @@ technique t0
{ {
pass p0 pass p0
{ {
Texture[0] = <g_SeabedTexture>; // Seabed texture Texture[0] = <g_texture0>; // Seabed texture
Texture[1] = <g_EnvironmentTexture>; // Environment texture Texture[1] = <g_texture1>; // Environment texture
// All of these constants are set by the game engine before drawing the shader // 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 // Each constant register (c# in the asm code) has 4 floating point values
// World view matrix // Special world * view * projection matrix for ocean shader
VertexShaderConstant[2] = <matWorld[0]>; VertexShaderConstant[2] = <g_OceanWorldViewProjection[0]>;
VertexShaderConstant[3] = <matWorld[1]>; VertexShaderConstant[3] = <g_OceanWorldViewProjection[1]>;
VertexShaderConstant[4] = <matWorld[2]>; VertexShaderConstant[4] = <g_OceanWorldViewProjection[2]>;
VertexShaderConstant[5] = <matWorld[3]>; VertexShaderConstant[5] = <g_OceanWorldViewProjection[3]>;
VertexShaderConstant[26] = <fog>; // This constant is calculated from the current fog min/max values VertexShaderConstant[26] = <fog>; // 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 VertexShaderConstant[27] = {0.0f, 1.0f, 0.5f, 0.75f}; // I don't know what this does but it doesn't change

View File

@ -5,15 +5,12 @@
// the original fixed-function water rendering behavior. // the original fixed-function water rendering behavior.
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
float4x4 g_WorldViewProjection : WorldViewProjection; #include "../fxh/SystemVariables.fxh"
float4 g_TextureFactor : TextureFactor;
//float4 g_Fog : Fog;
texture2D g_WaterTexture : Texture0;
sampler2D g_WaterTextureSampler = sampler2D g_WaterTextureSampler =
sampler_state sampler_state
{ {
Texture = <g_WaterTexture>; Texture = <g_texture0>;
}; };
struct VS_OUTPUT struct VS_OUTPUT
@ -21,7 +18,6 @@ struct VS_OUTPUT
float4 Position : POSITION; // vertex position float4 Position : POSITION; // vertex position
float2 TextureUV : TEXCOORD0; // vertex texture coords float2 TextureUV : TEXCOORD0; // vertex texture coords
float4 Color : COLOR0; float4 Color : COLOR0;
//float Fog : FOG;
}; };
VS_OUTPUT RenderSceneVS(float4 inPos : POSITION, VS_OUTPUT RenderSceneVS(float4 inPos : POSITION,
@ -46,8 +42,8 @@ VS_OUTPUT RenderSceneVS(float4 inPos : POSITION,
float4 RenderScenePS(VS_OUTPUT input) : COLOR0 float4 RenderScenePS(VS_OUTPUT input) : COLOR0
{ {
float4 texel = tex2D(g_WaterTextureSampler, input.TextureUV); float4 texel = tex2D(g_WaterTextureSampler, input.TextureUV);
float4 result = saturate(texel + (1 - texel) * g_TextureFactor); // equivalent to saturate((texel + g_TextureFactor) - (texel * g_TextureFactor)); 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; result.a = input.Color.a * g_textureFactor.a;
return result; return result;
} }

View File

@ -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;