From b6adcfc8b1d4713e10e495727024df98bab8e773 Mon Sep 17 00:00:00 2001 From: Nick Blakely Date: Thu, 5 Nov 2020 02:17:12 -0800 Subject: [PATCH] Syncing with game code changes. Support CPV objects in object shader. --- Shaders/Shaders.vcxproj | 22 ++- Shaders/Shaders.vcxproj.filters | 18 ++- Shaders/fx/LandBump.fx | 44 +++--- Shaders/fx/Object.fx | 257 ++++++++++++++++++-------------- Shaders/fx/Ocean.fx | 8 +- Shaders/fx/Water.fx | 2 +- Shaders/fxh/Constants.fxh | 63 +++++++- Shaders/fxh/Lighting.fxh | 12 +- 8 files changed, 266 insertions(+), 160 deletions(-) diff --git a/Shaders/Shaders.vcxproj b/Shaders/Shaders.vcxproj index db35ce9..d9f6247 100644 --- a/Shaders/Shaders.vcxproj +++ b/Shaders/Shaders.vcxproj @@ -97,29 +97,25 @@ 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 /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo + "$(JUN10SDKUTILPATH)\fxc.exe" /O1 /LD /Zi /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 - fxo\%(Filename).fxo - fxo\%(Filename).fxo + + + + + + + true true - - - - - diff --git a/Shaders/Shaders.vcxproj.filters b/Shaders/Shaders.vcxproj.filters index bc187bd..0345678 100644 --- a/Shaders/Shaders.vcxproj.filters +++ b/Shaders/Shaders.vcxproj.filters @@ -11,15 +11,21 @@ {026956f1-0f27-4b3d-80ab-f81eafce58f5} + + {0ead58ec-1210-4df1-88c8-dfd73f6fb054} + - - Include - - - - + + Include + + Include + + + + + \ No newline at end of file diff --git a/Shaders/fx/LandBump.fx b/Shaders/fx/LandBump.fx index e16028d..99566b3 100644 --- a/Shaders/fx/LandBump.fx +++ b/Shaders/fx/LandBump.fx @@ -4,7 +4,7 @@ // Land bumpmapping and lighting shader. //-------------------------------------------------------------------------------------- -#include "Constants.fxh" +#include "../fxh/Constants.fxh" float4x4 g_mWorldViewProjection : WorldViewProjection; float4x4 g_World : World; @@ -22,10 +22,10 @@ float4 g_TextureFactor : TextureFactor; ////////////////////////////////////////////////////// -texture g_LandTexture; -texture g_LandBumpTexture; -texture g_ShoreTexture; -texture g_LandDetailTexture; +texture g_LandTexture : Texture0; +texture g_LandBumpTexture : Texture1; +//texture g_LandDetailTexture : Texture2; +//texture g_ShoreTexture : Texture3; //-------------------------------------------------------------------------------------- // Texture samplers @@ -48,23 +48,23 @@ sampler_state MagFilter = LINEAR; }; -sampler g_LandDetailTextureSampler = -sampler_state -{ - Texture = ; - MipFilter = LINEAR; - MinFilter = LINEAR; - MagFilter = LINEAR; -}; - -sampler g_ShoreTextureSampler = -sampler_state -{ - Texture = ; - MipFilter = LINEAR; - MinFilter = LINEAR; - MagFilter = LINEAR; -}; +//sampler g_LandDetailTextureSampler = +//sampler_state +//{ +// Texture = ; +// MipFilter = LINEAR; +// MinFilter = LINEAR; +// MagFilter = LINEAR; +//}; +// +//sampler g_ShoreTextureSampler = +//sampler_state +//{ +// Texture = ; +// MipFilter = LINEAR; +// MinFilter = LINEAR; +// MagFilter = LINEAR; +//}; //-------------------------------------------------------------------------------------- // Vertex shader output structure diff --git a/Shaders/fx/Object.fx b/Shaders/fx/Object.fx index f79df76..f684188 100644 --- a/Shaders/fx/Object.fx +++ b/Shaders/fx/Object.fx @@ -2,11 +2,27 @@ #include "../fxh/Lighting.fxh" // Lighting state -float4 g_DirectionalLightAmbient[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightAmbient; +float4 g_DirectionalAmbientLightSum : DirectionalLightAmbientSum; 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; +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_state +{ + Texture = ; + MipFilter = LINEAR; + MinFilter = LINEAR; + MagFilter = LINEAR; +}; // Camera float3 g_CameraPosition : CameraPosition; @@ -18,27 +34,9 @@ Material g_Material : Material; float4x4 g_WorldViewProjection : WorldViewProjection; float4x4 g_World : World; -struct VS_OUTPUT -{ - float4 Pos : POSITION; - float4 Diffuse : COLOR0; - float4 Specular : COLOR1; - float4 Tex0 : TEXCOORD0; -}; - -float4 CalculateAmbientLight() -{ - float4 ambient = 0; - for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++) - { - if (g_DirectionalLightEnabled[i]) - { - ambient += g_DirectionalLightAmbient[i]; - } - } - - return ambient; -} +// ======================================================= +// Textured per pixel lighting +// float4 CalculateDiffuse(float3 N, float3 L, float4 diffuseColor) { @@ -71,13 +69,13 @@ Lighting DoDirectionalLight(float3 worldPos, float3 N, int i) { Lighting Out; Out.Diffuse = CalculateDiffuse( - N, - -g_DirectionalLightDirection[i], + N, + -g_DirectionalLightDirection[i], g_DirectionalLightDiffuse[i]); Out.Specular = CalculateSpecular( - worldPos, - N, - -g_DirectionalLightDirection[i], + worldPos, + N, + -g_DirectionalLightDirection[i], g_DirectionalLightSpecular[i]); return Out; } @@ -86,17 +84,14 @@ Lighting ComputeLighting(float3 worldPos, float3 N) { Lighting finalLighting = (Lighting)0; - for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++) + for (int i = 0; i < g_numDirectionalLights; i++) { - if (g_DirectionalLightEnabled[i]) - { - Lighting lighting = DoDirectionalLight(worldPos, N, i); - finalLighting.Diffuse += lighting.Diffuse; - finalLighting.Specular += lighting.Specular; - } + Lighting lighting = DoDirectionalLight(worldPos, N, i); + finalLighting.Diffuse += lighting.Diffuse; + finalLighting.Specular += lighting.Specular; } - float4 ambient = g_Material.Ambient * CalculateAmbientLight(); + float4 ambient = g_Material.Ambient * g_DirectionalAmbientLightSum; float4 diffuse = g_Material.Diffuse * finalLighting.Diffuse; float4 specular = g_Material.Specular * finalLighting.Specular; @@ -106,87 +101,129 @@ Lighting ComputeLighting(float3 worldPos, float3 N) 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; -//} - -VS_OUTPUT VSMain ( - float4 vPosition : POSITION0, - float3 vNormal : NORMAL0, - float2 tc : TEXCOORD0) +struct PixelLightingVSOutput { - VS_OUTPUT Out = (VS_OUTPUT)0; + float4 Pos : POSITION; + float2 Tex0 : TEXCOORD0; + float3 Normal : TEXCOORD1; + float3 WorldPos : TEXCOORD2; +}; - vNormal = normalize(vNormal); - Out.Pos = mul(vPosition, g_WorldViewProjection); +PixelLightingVSOutput PixelLightingVS( + float4 vPosition : POSITION0, + float3 vNormal : NORMAL0, + float2 tc : TEXCOORD0) +{ + // Simple transform, pre-compute as much as we can for the pixel shader + PixelLightingVSOutput output; - //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; + output.Pos = mul(vPosition, g_WorldViewProjection); + output.Normal = mul(normalize(vNormal), (float3x3)g_World); + output.WorldPos = mul(vPosition, g_World); + output.Tex0 = tc; + return output; } -float4 ps_main(VS_OUTPUT input) : COLOR0 +float4 GetColorArg(int colorArg, float4 textureColor, float4 diffuseColor) { - return input.Diffuse; + float4 result; + if (colorArg == D3DTA_TEXTURE) result = textureColor; + else if (colorArg == D3DTA_DIFFUSE) result = diffuseColor; + else if (colorArg == D3DTA_TFACTOR) result = g_textureFactor; + else result = float4(1.f, 1.f, 1.f, 1.f); + + return result; } -technique TexturedVertexLighting +float4 Modulate(int stageIndex, float4 textureColor, float4 diffuseColor, float factor) { - pass P0 - { - //PixelShader = compile ps_2_0 ps_main(); - VertexShader = compile vs_2_0 VSMain(); - } + float4 left = GetColorArg(g_blendStages[stageIndex].colorArg1, textureColor, diffuseColor); + float4 right = GetColorArg(g_blendStages[stageIndex].colorArg2, textureColor, diffuseColor); + + return (left * right) * factor; +} + +float4 ProcessStages(float4 textureColor, float4 diffuseColor) +{ + float4 output = 0; + for (int i = 0; i < g_numBlendStages; i++) + { + if (g_blendStages[i].colorOp == D3DTOP_MODULATE4X) + { + output += Modulate(i, textureColor, diffuseColor, 4.0f); + } + else + { + output += Modulate(i, textureColor, diffuseColor, 1.0f); + } + } + + return output; +} + +struct PixelLightingPSOutput +{ + float4 Diffuse : COLOR0; + float4 Specular : COLOR1; +}; + +PixelLightingPSOutput PixelLightingPS(PixelLightingVSOutput input) +{ + float4 color = tex2D(g_ObjTextureSampler, input.Tex0); + + Lighting lighting = ComputeLighting(input.WorldPos, input.Normal); + + PixelLightingPSOutput output; + output.Diffuse = ProcessStages(color, lighting.Diffuse); + output.Specular = color * lighting.Specular; + return output; +} + +technique PixelLighting +{ + pass P0 + { + PixelShader = compile ps_3_0 PixelLightingPS(); + VertexShader = compile vs_3_0 PixelLightingVS(); + } +} + +// ======================================================= +// Color per vertex +// + +struct ColorPerVertexVSOutput +{ + float4 Pos : POSITION; + float2 Tex0 : TEXCOORD0; + float4 Color : COLOR0; +}; + +float4 ColorPerVertexPS(ColorPerVertexVSOutput input) : COLOR0 +{ + float4 color = tex2D(g_ObjTextureSampler, input.Tex0); //* input.Color; + return color; +} + +ColorPerVertexVSOutput ColorPerVertexVS( + float4 vPosition : POSITION0, + float2 tc : TEXCOORD0, + float4 color : COLOR0) +{ + // Simple transform, pre-compute as much as we can for the pixel shader + ColorPerVertexVSOutput output = (ColorPerVertexVSOutput)0; + + output.Pos = mul(vPosition, g_WorldViewProjection); + output.Tex0 = tc; + output.Color = color; + return output; +} + +technique ColorPerVertex +{ + pass P0 + { + //PixelShader = compile ps_3_0 ColorPerVertexPS(); + VertexShader = compile vs_3_0 ColorPerVertexVS(); + } } \ No newline at end of file diff --git a/Shaders/fx/Ocean.fx b/Shaders/fx/Ocean.fx index 24978b1..8383aba 100644 --- a/Shaders/fx/Ocean.fx +++ b/Shaders/fx/Ocean.fx @@ -34,8 +34,8 @@ float4 MainPS(const PS_INPUT input) : COLOR0 float4 const44 : register(c44); matrix matWorld : OceanWorldViewProjection : register(c2); float4 fog : register (c26); -texture tex0; -texture tex1; +texture g_SeabedTexture : Texture0; +texture g_EnvironmentTexture : Texture1; struct VS_OUTPUT { @@ -87,8 +87,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 4eb501d..07c0a63 100644 --- a/Shaders/fx/Water.fx +++ b/Shaders/fx/Water.fx @@ -9,7 +9,7 @@ float4x4 g_WorldViewProjection : WorldViewProjection; float4 g_TextureFactor : TextureFactor; //float4 g_Fog : Fog; -texture2D g_WaterTexture; +texture2D g_WaterTexture : Texture0; sampler2D g_WaterTextureSampler = sampler_state { diff --git a/Shaders/fxh/Constants.fxh b/Shaders/fxh/Constants.fxh index d39e71b..9bfc462 100644 --- a/Shaders/fxh/Constants.fxh +++ b/Shaders/fxh/Constants.fxh @@ -1,4 +1,63 @@ -#pragma once - #define MAX_DIRECTIONAL_LIGHTS 3 #define MAX_LIGHTS 4 + +#define MAX_BLEND_STAGES 2 // Giants does not currently use more than 2 + +#define D3DTOP_DISABLE 1 // disables stage +#define D3DTOP_SELECTARG1 2 // the default +#define D3DTOP_SELECTARG2 3 +#define D3DTOP_MODULATE 4 // multiply args together +#define D3DTOP_MODULATE2X 5 // multiply and 1 bit +#define D3DTOP_MODULATE4X 6 // multiply and 2 bits +// Add +#define D3DTOP_ADD 7 // add arguments together +#define D3DTOP_ADDSIGNED 8 // add with -0.5 bias +#define D3DTOP_ADDSIGNED2X 9 // as above but left 1 bit +#define D3DTOP_SUBTRACT 10 // Arg1 - Arg2, with no saturation +#define D3DTOP_ADDSMOOTH 11 // add 2 args, subtract product + // Arg1 + Arg2 - Arg1*Arg2 + // Arg1 + (1-Arg1)*Arg2 +// Linear alpha blend: Arg1*(Alpha) + Arg2*(1-Alpha) +#define D3DTOP_BLENDDIFFUSEALPHA 12 // iterated alpha +#define D3DTOP_BLENDTEXTUREALPHA 13 // texture alpha +#define D3DTOP_BLENDFACTORALPHA 14 // alpha from D3DRS_TEXTUREFACTOR +// Linear alpha blend with pre-multiplied arg1 input: Arg1 + Arg2*(1-Alpha) +#define D3DTOP_BLENDTEXTUREALPHAPM 15 // texture alpha +#define D3DTOP_BLENDCURRENTALPHA 16 // by alpha of current color +// Specular mapping +#define D3DTOP_PREMODULATE 17 // modulate with next texture before use +#define D3DTOP_MODULATEALPHA_ADDCOLOR 18 // Arg1.RGB + Arg1.A*Arg2.RGB + // COLOROP only +#define D3DTOP_MODULATECOLOR_ADDALPHA 19 // Arg1.RGB*Arg2.RGB + Arg1.A + // COLOROP only +#define D3DTOP_MODULATEINVALPHA_ADDCOLOR 20 // (1-Arg1.A)*Arg2.RGB + Arg1.RGB +// COLOROP only +#define D3DTOP_MODULATEINVCOLOR_ADDALPHA 21 // (1-Arg1.RGB)*Arg2.RGB + Arg1.A + // COLOROP only +// Bump mapping +#define D3DTOP_BUMPENVMAP 22 // per pixel env map perturbation +#define D3DTOP_BUMPENVMAPLUMINANCE 23 // with luminance channel +// This can do either diffuse or specular bump mapping with correct input. +// Performs the function (Arg1.R*Arg2.R + Arg1.G*Arg2.G + Arg1.B*Arg2.B) +// where each component has been scaled and offset to make it signed. +// The result is replicated into all four (including alpha) channels. +// This is a valid COLOROP only. +#define D3DTOP_DOTPRODUCT3 24, +// Triadic ops +#define D3DTOP_MULTIPLYADD 25, // Arg0 + Arg1*Arg2 +#define D3DTOP_LERP 26 // (Arg0)*Arg1 + (1-Arg0)*Arg2 + +/* + * Values for COLORARG0,1,2, ALPHAARG0,1,2, and RESULTARG texture blending + * operations set in texture processing stage controls in D3DRENDERSTATE. + */ +#define D3DTA_SELECTMASK 0x0000000f // mask for arg selector +#define D3DTA_DIFFUSE 0x00000000 // select diffuse color (read only) +#define D3DTA_CURRENT 0x00000001 // select stage destination register (read/write) +#define D3DTA_TEXTURE 0x00000002 // select texture color (read only) +#define D3DTA_TFACTOR 0x00000003 // select D3DRS_TEXTUREFACTOR (read only) +#define D3DTA_SPECULAR 0x00000004 // select specular color (read only) +#define D3DTA_TEMP 0x00000005 // select temporary register color (read/write) +#define D3DTA_CONSTANT 0x00000006 // select texture stage constant +#define D3DTA_COMPLEMENT 0x00000010 // take 1.0 - x (read modifier) +#define D3DTA_ALPHAREPLICATE 0x00000020 // replicate alpha to color components (read modifier) \ No newline at end of file diff --git a/Shaders/fxh/Lighting.fxh b/Shaders/fxh/Lighting.fxh index fe8249b..fea7e28 100644 --- a/Shaders/fxh/Lighting.fxh +++ b/Shaders/fxh/Lighting.fxh @@ -1,5 +1,3 @@ -#pragma once - struct Material { float4 Diffuse; @@ -14,3 +12,13 @@ struct Lighting float4 Diffuse : COLOR0; float4 Specular : COLOR1; }; + +struct TextureBlendStage +{ + int colorOp; + int colorArg1; + int colorArg2; + int alphaOp; + int alphaArg1; + int alphaArg2; +}; \ No newline at end of file