Skip to content

Commit

Permalink
pixel shader lights are probably backwards (at the very least, z dire…
Browse files Browse the repository at this point in the history
…ction is messed up) will fix later once we get into the game.
  • Loading branch information
PazerOP committed May 26, 2019
1 parent 3d41696 commit c07cead
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 45 deletions.
8 changes: 8 additions & 0 deletions TF2VulkanUtil/include/TF2Vulkan/Util/Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,11 @@ static constexpr const char* PRINTF_BOOL(bool val) { return val ? "true" : "fals
#define DISABLE_OBJ_COPY_MOVE(type) \
DISABLE_OBJ_MOVE(type); \
DISABLE_OBJ_COPY(type);

#define CHECK_OFFSET(type, member, expectedOffset) \
static_assert(!(offsetof(type, member) < expectedOffset), "Actual offset less than expected"); \
static_assert(!(offsetof(type, member) > expectedOffset), "Actual offset greater than expected");

#define CHECK_SIZE(type, expectedSize) \
static_assert(!(sizeof(type) < expectedSize), "Actual size less than expected"); \
static_assert(!(sizeof(type) > expectedSize), "Actual size greater than expected");
2 changes: 1 addition & 1 deletion sdk2013
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ void IShaderAPI_TextureManager::DeleteTexture(ShaderAPITextureHandle_t tex)
{
const auto dbgName = realTex.GetDebugName();

char buf[128];
char buf[512];
sprintf_s(buf, "[DELETED] Texture: %.*s", PRINTF_SV(dbgName));
g_ShaderDevice.SetDebugName(realTex.m_Image.GetImage(), buf);

Expand Down
20 changes: 16 additions & 4 deletions stdshader_vulkan/include/stdshader_vulkan/ShaderData.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "ShaderDataShared.h"

#include <TF2Vulkan/Util/Macros.h>
#include <TF2Vulkan/Util/std_array.h>
#include <TF2Vulkan/Util/std_compare.h>

Expand Down Expand Up @@ -40,10 +41,21 @@ namespace TF2Vulkan{ namespace Shaders
std::array<LightInfo, 4> m_LightInfo;
AmbientLightCube m_AmbientCube;

float4 m_SelfIllumTint;
float4 m_DiffuseModulation;
float4 m_EnvmapTintShadowTweaks;
float4 m_LinearFogColor;
float4 m_LightScale;
};

static_assert(offsetof(ShaderDataCommon, m_LightInfo) == 208);
CHECK_OFFSET(ShaderDataCommon, m_ModelViewProj, 0); // 0
CHECK_OFFSET(ShaderDataCommon, m_ViewProj, 64); // 1
CHECK_OFFSET(ShaderDataCommon, m_OOGamma, 128); // 2
CHECK_OFFSET(ShaderDataCommon, m_LightCount, 132); // 3
CHECK_OFFSET(ShaderDataCommon, m_LightEnabled, 144); // 4
CHECK_OFFSET(ShaderDataCommon, m_EyePos, 160); // 5
CHECK_OFFSET(ShaderDataCommon, m_WaterZ, 172); // 6
CHECK_OFFSET(ShaderDataCommon, m_FlexScale, 176); // 7
CHECK_OFFSET(ShaderDataCommon, m_FogParams, 192); // 8
CHECK_OFFSET(ShaderDataCommon, m_LightInfo, 208); // 9
CHECK_OFFSET(ShaderDataCommon, m_AmbientCube, 528); // 10
CHECK_OFFSET(ShaderDataCommon, m_LinearFogColor, 624); // 11
CHECK_OFFSET(ShaderDataCommon, m_LightScale, 640); // 12
} }
35 changes: 25 additions & 10 deletions stdshader_vulkan/include/stdshader_vulkan/ShaderDataShared.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#ifdef __cplusplus
#include <TF2Vulkan/AlignedTypes.h>
#include <TF2Vulkan/Util/Macros.h>
#include <TF2Vulkan/Util/std_compare.h>
namespace TF2Vulkan{ namespace Shaders
{
Expand All @@ -26,19 +27,27 @@ namespace TF2Vulkan{ namespace Shaders
float1 stopdot1;
float1 stopdot2;
float1 OOdot;

#ifdef __cplusplus
// Padding
uint32_t : 32;
uint32_t : 32;
#endif
};

#ifdef __cplusplus
static_assert(offsetof(LightInfo, color) == 0); // 0
static_assert(offsetof(LightInfo, bIsDirectional) == 12); // 1
static_assert(offsetof(LightInfo, dir) == 16); // 2
static_assert(offsetof(LightInfo, bIsSpot) == 28); // 3
static_assert(offsetof(LightInfo, pos) == 32); // 4
static_assert(offsetof(LightInfo, falloff) == 44); // 5
static_assert(offsetof(LightInfo, atten) == 48); // 6
static_assert(offsetof(LightInfo, stopdot1) == 60); // 7
static_assert(offsetof(LightInfo, stopdot2) == 64); // 8
static_assert(offsetof(LightInfo, OOdot) == 68); // 9
CHECK_OFFSET(LightInfo, color, 0);
CHECK_OFFSET(LightInfo, bIsDirectional, 12);
CHECK_OFFSET(LightInfo, dir, 16);
CHECK_OFFSET(LightInfo, bIsSpot, 28);
CHECK_OFFSET(LightInfo, pos, 32);
CHECK_OFFSET(LightInfo, falloff, 44);
CHECK_OFFSET(LightInfo, atten, 48);
CHECK_OFFSET(LightInfo, stopdot1, 60);
CHECK_OFFSET(LightInfo, stopdot2, 64);
CHECK_OFFSET(LightInfo, OOdot, 68);

CHECK_SIZE(LightInfo, 80);
#endif

#if false
Expand All @@ -60,6 +69,12 @@ namespace TF2Vulkan{ namespace Shaders
};

#ifdef __cplusplus
static_assert(offsetof(AmbientLightCube, x[0]) == 0);
static_assert(offsetof(AmbientLightCube, x[1]) == 16);
static_assert(offsetof(AmbientLightCube, y[0]) == 32);
static_assert(offsetof(AmbientLightCube, y[1]) == 48);
static_assert(offsetof(AmbientLightCube, z[0]) == 64);
static_assert(offsetof(AmbientLightCube, z[1]) == 80);
static_assert(sizeof(AmbientLightCube) == 96);
#endif

Expand Down
2 changes: 0 additions & 2 deletions stdshader_vulkan/src/shaders/HLSL/common_fxc.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ static const float cOneThird = 1.0f / 3.0f;
// PS standard
float4 g_LinearFogColor;
float4 cLightScale;
float4 cFlashlightColor;
float4 cFlashlightScreenScale;
};

#define NUM_LIGHTS g_nLightCountRegister
Expand Down
9 changes: 5 additions & 4 deletions stdshader_vulkan/src/shaders/HLSL/xlitgeneric.common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ struct VS_TO_PS_INTERSTAGE_DATA
#endif

float4 color : COLOR0;
float fog : FOG;

float3 baseTexCoord : TEXCOORD0;
float3 detailTexCoord : TEXCOORD1;
Expand All @@ -39,13 +38,15 @@ struct VS_TO_PS_INTERSTAGE_DATA
float3 worldSpaceBinormal : BINORMAL;
float4 lightAtten : LIGHT_ATTEN;

float4 vProjPos : TEXCOORD6;
float4 worldPos_ProjPosZ : TEXCOORD7;
float4 vProjPos : POS_PROJ;
float3 worldSpacePos : POS_WORLD;

float3 SeamlessWeights : TEXCOORD8;
float4 fogFactorW : TEXCOORD9;

float3 boneWeightsOut : TEST_BONEWEIGHTSOUT;

float1 fog : FOG;
float4 fogFactorW : TEXCOORD9;
};

#ifdef VERTEX_SHADER
Expand Down
9 changes: 5 additions & 4 deletions stdshader_vulkan/src/shaders/HLSL/xlitgeneric.frag.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ static float3 PixelShaderDoGeneralDiffuseLight(const float fAtten, const float3
// ... actually, now only 9: mul, cmp, cmp, mul, mad, mad, mad, mad, mad
static float3 PixelShaderAmbientLight(const float3 worldNormal)
{
float3 linearColor, nSquared = worldNormal * worldNormal;
float3 linearColor;
float3 nSquared = worldNormal * worldNormal;
float3 isNegative = (worldNormal >= 0.0) ? 0 : nSquared;
float3 isPositive = (worldNormal >= 0.0) ? nSquared : 0;
linearColor = isPositive.x * cAmbientCube.x[0] + isNegative.x * cAmbientCube.x[1] +

return isPositive.x * cAmbientCube.x[0] + isNegative.x * cAmbientCube.x[1] +
isPositive.y * cAmbientCube.y[0] + isNegative.y * cAmbientCube.y[1] +
isPositive.z * cAmbientCube.z[0] + isNegative.z * cAmbientCube.z[1];
return linearColor;
}

static void UnpackPSLightInfo(const uint index, out float3 pos, out float3 color)
Expand Down Expand Up @@ -150,7 +151,7 @@ float4 main(PS_INPUT i) : SV_TARGET

if (DIFFUSELIGHTING)
{
diffuseColor = PixelShaderDoLighting(i.worldPos_ProjPosZ.xyz, i.worldSpaceNormal,
diffuseColor = PixelShaderDoLighting(i.worldSpacePos, i.worldSpaceNormal,
float3(0.0f, 0.0f, 0.0f), false, i.lightAtten,
false, 1.0f);
}
Expand Down
29 changes: 10 additions & 19 deletions stdshader_vulkan/src/shaders/HLSL/xlitgeneric.vert.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ VS_OUTPUT main(const VS_INPUT v)
}

// Perform skinning
float3 worldPos, worldTangentS, worldTangentT;
float3 worldTangentS, worldTangentT;

if (NORMALMAPPING)
{
SkinPositionNormalAndTangentSpace(g_bSkinning, vPosition, vNormal, vTangent,
v.vBoneWeights, v.vBoneIndices, worldPos,
v.vBoneWeights, v.vBoneIndices, o.worldSpacePos,
o.worldSpaceNormal, worldTangentS, worldTangentT);

worldTangentS = normalize(worldTangentS);
Expand All @@ -101,14 +101,14 @@ VS_OUTPUT main(const VS_INPUT v)
g_bSkinning,
vPosition, vNormal,
v.vBoneWeights, v.vBoneIndices,
worldPos, o.worldSpaceNormal);
o.worldSpacePos, o.worldSpaceNormal);
}

if (NORMALMAPPING || !VERTEXCOLOR)
o.worldSpaceNormal = normalize(o.worldSpaceNormal);

if (MORPHING && DECAL)
worldPos += o.worldSpaceNormal * 0.05f * v.vTexCoord2.z;
o.worldSpacePos += o.worldSpaceNormal * 0.05f * v.vTexCoord2.z;

if (NORMALMAPPING)
{
Expand All @@ -117,19 +117,15 @@ VS_OUTPUT main(const VS_INPUT v)
}

// Transform into projection space
float4 vProjPos = mul(float4(worldPos, 1), cViewProj);
o.projPos = vProjPos;
vProjPos.z = o.projPos.z; //dot(float4(worldPos, 1), cViewProjZ);
o.vProjPos = mul(float4(o.worldSpacePos, 1), cViewProj);
o.projPos = o.vProjPos;

o.vProjPos = vProjPos;
o.fogFactorW.w = CalcFog(worldPos, vProjPos.xyz, DOWATERFOG);
o.fogFactorW.w = CalcFog(o.worldSpacePos, o.vProjPos.xyz, DOWATERFOG);
o.fog = o.fogFactorW.w;
o.worldPos_ProjPosZ.xyz = worldPos.xyz;
o.worldPos_ProjPosZ.w = vProjPos.z;

// Needed for cubemaps
if (CUBEMAP)
o.worldVertToEyeVector.xyz = cEyePos - worldPos;
o.worldVertToEyeVector.xyz = cEyePos - o.worldSpacePos;

if (VERTEXCOLOR)
{
Expand All @@ -140,7 +136,7 @@ VS_OUTPUT main(const VS_INPUT v)
else
{
//o.color = float4(worldPos, 1);
o.color = float4(DoLighting(worldPos, o.worldSpaceNormal, v.vSpecular, STATIC_LIGHT_VERTEX, DYNAMIC_LIGHT, HALFLAMBERT), 1);
o.color = float4(DoLighting(o.worldSpacePos, o.worldSpaceNormal, v.vSpecular, STATIC_LIGHT_VERTEX, DYNAMIC_LIGHT, HALFLAMBERT), 1);
}

if (SEAMLESS_BASE)
Expand Down Expand Up @@ -168,15 +164,10 @@ VS_OUTPUT main(const VS_INPUT v)

// Light attenuation
for (uint i = 0; i < NUM_LIGHTS; i++)
o.lightAtten[i] = GetVertexAttenForLight(worldPos, i);
o.lightAtten[i] = GetVertexAttenForLight(o.worldSpacePos, i);

if (SEPARATE_DETAIL_UVS)
o.detailTexCoord.xy = v.vTexCoord1.xy;

#ifdef INVERT_Y
o.projPos.y = 1 - o.projPos.y;
#endif

//o.color = v.vColor.rgba;
return o;
}
4 changes: 4 additions & 0 deletions stdshader_vulkan/src/shaders/XLitGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ inline namespace XLitGeneric
bool32 DIFFUSELIGHTING;
bool32 GAMMA_CONVERT_VERTEX_COLOR;

bool32 AMBIENT_LIGHT;
bool32 DYNAMIC_LIGHT;

bool32 TEXACTIVE_BASETEXTURE;
Expand All @@ -98,6 +99,7 @@ inline namespace XLitGeneric
SPEC_CONST_BUF_ENTRY(SpecConstBuf, DIFFUSELIGHTING);
SPEC_CONST_BUF_ENTRY(SpecConstBuf, GAMMA_CONVERT_VERTEX_COLOR);

SPEC_CONST_BUF_ENTRY(SpecConstBuf, AMBIENT_LIGHT);
SPEC_CONST_BUF_ENTRY(SpecConstBuf, DYNAMIC_LIGHT);

SPEC_CONST_BUF_ENTRY(SpecConstBuf, TEXACTIVE_BASETEXTURE);
Expand Down Expand Up @@ -567,6 +569,8 @@ void Shader::OnDrawElements(const OnDrawElementsParams& params)
{
drawParams.m_SpecConsts.DYNAMIC_LIGHT = true;
LoadLights(common);

drawParams.m_SpecConsts.AMBIENT_LIGHT = (common.m_AmbientCube != AmbientLightCube());
}

custom.m_VertexAlpha = bHasVertexAlpha ? 1 : 0;
Expand Down

0 comments on commit c07cead

Please sign in to comment.