Indentation fixes & add a 'const' prefix to all single-assigned variables, and 'static' for all literal assignments.

This commit is contained in:
PatrickvL 2020-12-11 13:53:41 +01:00 committed by Anthony
parent 39e91ffec6
commit bd76d67c78
1 changed files with 164 additions and 162 deletions

View File

@ -119,11 +119,11 @@ LightingInfo DoSpecular(const float3 toLightVN, const float3 toViewerVN, const f
{
// Blinn-Phong
// https://learnopengl.com/Advanced-Lighting/Advanced-Lighting
float3 halfway = normalize(toViewerVN + toLightVN);
float NdotH = dot(View.Normal, halfway);
const float3 halfway = normalize(toViewerVN + toLightVN);
const float NdotH = dot(View.Normal, halfway);
float3 frontSpecular = pow(abs(NdotH), powers[0]) * lightSpecular.rgb;
float3 backSpecular = pow(abs(NdotH), powers[1]) * lightSpecular.rgb;
const float3 frontSpecular = pow(abs(NdotH), powers[0]) * lightSpecular.rgb;
const float3 backSpecular = pow(abs(NdotH), powers[1]) * lightSpecular.rgb;
if (NdotH >= 0)
o.Front = frontSpecular;
@ -143,9 +143,9 @@ LightingOutput DoPointLight(const Light l, const float3 toViewerVN, const float2
o.Specular.Front = o.Specular.Back = float3(0, 0, 0);
// Diffuse
float3 toLightV = l.PositionV - View.Position.xyz;
float lightDist = length(toLightV);
float3 toLightVN = normalize(toLightV);
const float3 toLightV = l.PositionV - View.Position.xyz;
const float lightDist = length(toLightV);
const float3 toLightVN = normalize(toLightV);
// A(Constant) + A(Linear) * dist + A(Exp) * dist^2
float attenuation =
@ -157,8 +157,8 @@ LightingOutput DoPointLight(const Light l, const float3 toViewerVN, const float2
if (lightDist > l.Range)
attenuation = 0;
float NdotL = dot(View.Normal, toLightVN);
float3 lightDiffuse = abs(NdotL) * attenuation * l.Diffuse.rgb;
const float NdotL = dot(View.Normal, toLightVN);
const float3 lightDiffuse = abs(NdotL) * attenuation * l.Diffuse.rgb;
if (NdotL >= 0)
o.Diffuse.Front = lightDiffuse;
@ -180,16 +180,16 @@ LightingOutput DoSpotLight(const Light l, const float3 toViewerVN, const float2
o.Specular.Front = o.Specular.Back = float3(0, 0, 0);
// Diffuse
float3 toLightV = l.PositionV - View.Position.xyz;
float lightDist = length(toLightV);
float3 toLightVN = normalize(toLightV);
float3 toVertexVN = -toLightVN;
const float3 toLightV = l.PositionV - View.Position.xyz;
const float lightDist = length(toLightV);
const float3 toLightVN = normalize(toLightV);
const float3 toVertexVN = -toLightVN;
// https://docs.microsoft.com/en-us/windows/win32/direct3d9/light-types
float cosAlpha = dot(l.DirectionVN, toVertexVN);
const float cosAlpha = dot(l.DirectionVN, toVertexVN);
// I = ( cos(a) - cos(phi/2) ) / ( cos(theta/2) - cos(phi/2) )
float spotBase = saturate((cosAlpha - l.CosHalfPhi) / l.SpotIntensityDivisor);
float spotIntensity = pow(spotBase, l.Falloff);
const float spotBase = saturate((cosAlpha - l.CosHalfPhi) / l.SpotIntensityDivisor);
const float spotIntensity = pow(spotBase, l.Falloff);
// A(Constant) + A(Linear) * dist + A(Exp) * dist^2
float attenuation =
@ -201,8 +201,8 @@ LightingOutput DoSpotLight(const Light l, const float3 toViewerVN, const float2
if (lightDist > l.Range)
attenuation = 0;
float NdotL = dot(View.Normal, toLightVN);
float3 lightDiffuse = abs(NdotL) * attenuation * l.Diffuse.rgb * spotIntensity;
const float NdotL = dot(View.Normal, toLightVN);
const float3 lightDiffuse = abs(NdotL) * attenuation * l.Diffuse.rgb * spotIntensity;
if (NdotL >= 0)
o.Diffuse.Front = lightDiffuse;
@ -226,9 +226,9 @@ LightingOutput DoDirectionalLight(const Light l, const float3 toViewerVN, const
// Diffuse
// Intensity from N . L
float3 toLightVN = -l.DirectionVN;
float NdotL = dot(View.Normal, toLightVN);
float3 lightDiffuse = abs(NdotL * l.Diffuse.rgb);
const float3 toLightVN = -l.DirectionVN;
const float NdotL = dot(View.Normal, toLightVN);
const float3 lightDiffuse = abs(NdotL * l.Diffuse.rgb);
// Apply light contribution to front or back face
// as the case may be
@ -246,10 +246,10 @@ LightingOutput DoDirectionalLight(const Light l, const float3 toViewerVN, const
LightingOutput CalcLighting(const float2 powers)
{
const int LIGHT_TYPE_NONE = 0;
const int LIGHT_TYPE_POINT = 1;
const int LIGHT_TYPE_SPOT = 2;
const int LIGHT_TYPE_DIRECTIONAL = 3;
static const int LIGHT_TYPE_NONE = 0;
static const int LIGHT_TYPE_POINT = 1;
static const int LIGHT_TYPE_SPOT = 2;
static const int LIGHT_TYPE_DIRECTIONAL = 3;
LightingOutput totalLightOutput;
totalLightOutput.Diffuse.Front = float3(0, 0, 0);
@ -257,7 +257,7 @@ LightingOutput CalcLighting(const float2 powers)
totalLightOutput.Specular.Front = float3(0, 0, 0);
totalLightOutput.Specular.Back = float3(0, 0, 0);
float3 toViewerVN = state.Modes.LocalViewer
const float3 toViewerVN = state.Modes.LocalViewer
? float3(0, 0, 1)
: normalize(-View.Position.xyz);
@ -291,17 +291,17 @@ TransformInfo DoTransform(const float4 position, const float3 normal, const floa
output.Normal = float3(0, 0, 0);
// The number of matrices to blend (always in the range [1..4])
int matrices = state.Modes.VertexBlend_NrOfMatrices;
const int matrices = state.Modes.VertexBlend_NrOfMatrices;
// Initialize the final matrix its blend weight at 1, from which all preceding blend weights will be deducted :
float lastBlend = 1;
for (int i = 0; i < matrices; i++)
{
// Do we have to calculate the last blend value (never happens when there's already 4 matrices) ?
bool bCalcFinalWeight = (state.Modes.VertexBlend_CalcLastWeight > 0) && (i == (matrices - 1));
const bool bCalcFinalWeight = (state.Modes.VertexBlend_CalcLastWeight > 0) && (i == (matrices - 1));
// Note : In case of X_D3DVBF_DISABLE, no prior weights have been deducted from lastBlend, so it will still be 1.
// The number of matrices will also be 1, which effectively turns this into non-weighted single-matrix multiplications :
float blendWeight = bCalcFinalWeight ? lastBlend : blendWeights[i];
const float blendWeight = bCalcFinalWeight ? lastBlend : blendWeights[i];
// Reduce the blend weight for the final matrix :
lastBlend -= blendWeights[i];
// Add this matrix (multiplied by its blend weight) to the output :
@ -320,15 +320,15 @@ Material DoMaterial(const uint index, const uint diffuseReg, const uint specular
// Note : If X_D3DRS_COLORVERTEX is FALSE, UpdateFixedFunctionVertexShaderState has already changed all MaterialSource's into D3DMCS_MATERIAL
// https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dmaterialcolorsource
const int D3DMCS_MATERIAL = 0;
const int D3DMCS_COLOR1 = 1;
const int D3DMCS_COLOR2 = 2;
static const int D3DMCS_MATERIAL = 0;
static const int D3DMCS_COLOR1 = 1;
static const int D3DMCS_COLOR2 = 2;
// If COLORVERTEX mode, AND the desired diffuse or specular colour is defined in the vertex declaration
// Then use the vertex colour instead of the material
if (!vRegisterDefaultFlags[diffuseReg]) {
float4 diffuseVertexColour = Get(xIn, diffuseReg);
const float4 diffuseVertexColour = Get(xIn, diffuseReg);
if (state.Modes.AmbientMaterialSource == D3DMCS_COLOR1) material.Ambient = diffuseVertexColour;
if (state.Modes.DiffuseMaterialSource == D3DMCS_COLOR1) material.Diffuse = diffuseVertexColour;
if (state.Modes.SpecularMaterialSource == D3DMCS_COLOR1) material.Specular = diffuseVertexColour;
@ -336,7 +336,7 @@ Material DoMaterial(const uint index, const uint diffuseReg, const uint specular
}
if (!vRegisterDefaultFlags[specularReg]) {
float4 specularVertexColour = Get(xIn, specularReg);
const float4 specularVertexColour = Get(xIn, specularReg);
if (state.Modes.AmbientMaterialSource == D3DMCS_COLOR2) material.Ambient = specularVertexColour;
if (state.Modes.DiffuseMaterialSource == D3DMCS_COLOR2) material.Diffuse = specularVertexColour;
if (state.Modes.SpecularMaterialSource == D3DMCS_COLOR2) material.Specular = specularVertexColour;
@ -371,27 +371,27 @@ float4 DoTexCoord(const uint stage, const VS_INPUT xIn)
{
// Texture transform flags
// https://docs.microsoft.com/en-gb/windows/win32/direct3d9/d3dtexturetransformflags
const int D3DTTFF_DISABLE = 0;
const int D3DTTFF_COUNT1 = 1;
const int D3DTTFF_COUNT2 = 2;
const int D3DTTFF_COUNT3 = 3;
const int D3DTTFF_COUNT4 = 4;
const int D3DTTFF_PROJECTED = 256; // This is the only real flag
static const int D3DTTFF_DISABLE = 0;
static const int D3DTTFF_COUNT1 = 1;
static const int D3DTTFF_COUNT2 = 2;
static const int D3DTTFF_COUNT3 = 3;
static const int D3DTTFF_COUNT4 = 4;
static const int D3DTTFF_PROJECTED = 256; // This is the only real flag
// https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dtss-tci
// Pre-shifted
const int TCI_PASSTHRU = 0;
const int TCI_CAMERASPACENORMAL = 1;
const int TCI_CAMERASPACEPOSITION = 2;
const int TCI_CAMERASPACEREFLECTIONVECTOR = 3;
const int TCI_OBJECT = 4; // Xbox
const int TCI_SPHERE = 5; // Xbox
static const int TCI_PASSTHRU = 0;
static const int TCI_CAMERASPACENORMAL = 1;
static const int TCI_CAMERASPACEPOSITION = 2;
static const int TCI_CAMERASPACEREFLECTIONVECTOR = 3;
static const int TCI_OBJECT = 4; // Xbox
static const int TCI_SPHERE = 5; // Xbox
const TextureState tState = state.TextureStates[stage];
// Extract transform flags
int countFlag = tState.TextureTransformFlagsCount;
bool projected = tState.TextureTransformFlagsProjected;
const int countFlag = tState.TextureTransformFlagsCount;
const bool projected = tState.TextureTransformFlagsProjected;
// Get texture coordinates
// Coordinates are either from the vertex texcoord data
@ -400,7 +400,7 @@ float4 DoTexCoord(const uint stage, const VS_INPUT xIn)
if (tState.TexCoordIndexGen == TCI_PASSTHRU)
{
// Get from vertex data
uint texCoordIndex = abs(tState.TexCoordIndex); // Note : abs() avoids error X3548 : in vs_3_0 uints can only be used with known - positive values, use int if possible
const uint texCoordIndex = abs(tState.TexCoordIndex); // Note : abs() avoids error X3548 : in vs_3_0 uints can only be used with known - positive values, use int if possible
texCoord = Get(xIn, texcoord0+texCoordIndex);
// Make coordinates homogenous
@ -412,32 +412,31 @@ float4 DoTexCoord(const uint stage, const VS_INPUT xIn)
// Test case: Modify pixel shader sample
// TODO move alongside the texture transformation when it stops angering the HLSL compiler
float componentCount = state.TexCoordComponentCount[texCoordIndex];
const float componentCount = state.TexCoordComponentCount[texCoordIndex];
if (componentCount == 1)
texCoord.yzw = float3(1, 0, 0);
if (componentCount == 2)
texCoord.zw = float2(1, 0);
if (componentCount == 3)
texCoord.w = 1;
}
else
{
// Generate texture coordinates
float3 reflected = reflect(normalize(View.Position.xyz), View.Normal);
if (tState.TexCoordIndexGen == TCI_CAMERASPACENORMAL)
} // Generate texture coordinates
else if (tState.TexCoordIndexGen == TCI_CAMERASPACENORMAL)
texCoord = float4(View.Normal, 1);
else if (tState.TexCoordIndexGen == TCI_CAMERASPACEPOSITION)
texCoord = View.Position;
else if (tState.TexCoordIndexGen == TCI_CAMERASPACEREFLECTIONVECTOR)
else
{
const float3 reflected = reflect(normalize(View.Position.xyz), View.Normal);
if (tState.TexCoordIndexGen == TCI_CAMERASPACEREFLECTIONVECTOR)
texCoord.xyz = reflected;
// else if TCI_OBJECT TODO is this just model position?
else if (tState.TexCoordIndexGen == TCI_SPHERE)
{
// TODO verify
// http://www.bluevoid.com/opengl/sig99/advanced99/notes/node177.html
float3 R = reflected;
float p = sqrt(pow(R.x, 2) + pow(R.y, 2) + pow(R.z + 1, 2));
const float3 R = reflected;
const float p = sqrt(pow(R.x, 2) + pow(R.y, 2) + pow(R.z + 1, 2));
texCoord.x = R.x / 2 * p + 0.5f;
texCoord.y = R.y / 2 * p + 0.5f;
}
@ -472,12 +471,14 @@ float4 DoTexCoord(const uint stage, const VS_INPUT xIn)
// Test case: Point sprite sample
float DoPointSpriteSize()
{
PointSprite ps = state.PointSprite;
const PointSprite ps = state.PointSprite;
float pointSize = ps.PointSize;
if (ps.PointScaleEnable)
{
float eyeDistance = length(View.Position);
float factor = ps.ScaleA + ps.ScaleB * eyeDistance + ps.ScaleC * (eyeDistance * eyeDistance);
const float eyeDistance = length(View.Position);
const float factor = ps.ScaleA + ps.ScaleB * eyeDistance + ps.ScaleC * (eyeDistance * eyeDistance);
pointSize *= ps.RenderTargetHeight * sqrt(1 / factor);
}
@ -490,8 +491,9 @@ VS_INPUT InitializeInputRegisters(const VS_INPUT xInput)
// Initialize input registers from the vertex buffer data
// Or use the register's default value (which can be changed by the title)
for (uint i = 0; i < 16; i++) {
float4 value = lerp(Get(xInput, i), vRegisterDefaultValues[i], vRegisterDefaultFlags[i]);
for (uint i = 0; i < 16; i++)
{
const float4 value = lerp(Get(xInput, i), vRegisterDefaultValues[i], vRegisterDefaultFlags[i]);
#ifdef CXBX_ALL_TEXCOORD_INPUTS
xIn.v[i] = value;
#else
@ -543,25 +545,25 @@ VS_OUTPUT main(const VS_INPUT xInput)
if (state.Modes.Lighting || state.Modes.TwoSidedLighting)
{
// Materials
Material material = DoMaterial(0, diffuse, specular, xIn);
Material backMaterial = DoMaterial(1, backDiffuse, backSpecular, xIn);
const Material material = DoMaterial(0, diffuse, specular, xIn);
const Material backMaterial = DoMaterial(1, backDiffuse, backSpecular, xIn);
float2 powers = float2(material.Power, backMaterial.Power);
const float2 powers = float2(material.Power, backMaterial.Power);
LightingOutput lighting = CalcLighting(powers);
// Compute each lighting component
float3 _ambient = material.Ambient.rgb * state.AmbientPlusLightAmbient.rgb;
float3 _backAmbient = backMaterial.Ambient.rgb * state.BackAmbientPlusLightAmbient.rgb;
const float3 _ambient = material.Ambient.rgb * state.AmbientPlusLightAmbient.rgb;
const float3 _backAmbient = backMaterial.Ambient.rgb * state.BackAmbientPlusLightAmbient.rgb;
float3 _diffuse = material.Diffuse.rgb * lighting.Diffuse.Front;
float3 _backDiffuse = backMaterial.Diffuse.rgb * lighting.Diffuse.Back;
const float3 _diffuse = material.Diffuse.rgb * lighting.Diffuse.Front;
const float3 _backDiffuse = backMaterial.Diffuse.rgb * lighting.Diffuse.Back;
float3 _specular = material.Specular.rgb * lighting.Specular.Front;
float3 _backSpecular = backMaterial.Specular.rgb * lighting.Specular.Back;
const float3 _specular = material.Specular.rgb * lighting.Specular.Front;
const float3 _backSpecular = backMaterial.Specular.rgb * lighting.Specular.Back;
float3 _emissive = material.Emissive.rgb;
float3 _backEmissive = backMaterial.Emissive.rgb;
const float3 _emissive = material.Emissive.rgb;
const float3 _backEmissive = backMaterial.Emissive.rgb;
// Frontface
xOut.oD0 = float4(_ambient + _diffuse + _emissive, material.Diffuse.a);