Skip to content

Commit

Permalink
Change shaders to use much more reliable makefiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
PazerOP committed May 25, 2019
1 parent 9b66b7a commit 3d41696
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,4 @@ healthchecksdb
stdshader_dx9_tf2vulkan/src/Generated/
*.vcxproj.filters
stdshader_vulkan/src/shaders/Generated/
*.tlog
11 changes: 9 additions & 2 deletions shaderapivulkan/src/TF2Vulkan/shaders/VulkanShaderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ StructMember::StructMember(const spirv_cross::Compiler& comp, uint32_t parent, u
}

Struct::Struct(const spirv_cross::Compiler& comp, const spirv_cross::Resource& resource) :
m_Name(comp.get_name(resource.base_type_id))
m_TypeName(comp.get_name(resource.base_type_id))
{
const auto& type = comp.get_type(resource.base_type_id);

Expand All @@ -162,8 +162,15 @@ ShaderResource::ShaderResource(const spirv_cross::Compiler& comp, uint32_t id) :

UniformBuffer::UniformBuffer(const spirv_cross::Compiler& comp, const spirv_cross::Resource& resource) :
Struct(comp, resource),
ShaderResource(comp, resource.id)
ShaderResource(comp, resource.id),
m_Name(comp.get_name(resource.id))
{
if (const auto & name = comp.get_name(resource.id); !name.empty())
m_Name = name;
else
m_Name = m_TypeName;

assert(!m_Name.empty());
}

Sampler::Sampler(const spirv_cross::Compiler& comp, uint32_t id) :
Expand Down
3 changes: 2 additions & 1 deletion shaderapivulkan/src/TF2Vulkan/shaders/VulkanShaderManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace TF2Vulkan
Struct(const spirv_cross::Compiler& comp,
const spirv_cross::Resource& resource);

std::string m_Name;
std::string m_TypeName;
size_t m_Size;
std::vector<StructMember> m_Members;
};
Expand All @@ -91,6 +91,7 @@ namespace TF2Vulkan
{
UniformBuffer(const spirv_cross::Compiler& comp,
const spirv_cross::Resource& resource);
std::string m_Name;
};

struct Sampler final : ShaderVariable, ShaderResource
Expand Down
5 changes: 5 additions & 0 deletions stdshader_vulkan/src/shaders/HLSL/common_ps_fxc.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ float3 BlendPixelFog(const float3 vShaderColor, float pixelFogFactor, const floa
{
return vShaderColor;
}
else
{
// Magic number for invalid pixel fog type
return float3(1.3, 0.2, 5.8);
}
}

float4 FinalOutput(const float4 vShaderColor, float pixelFogFactor, const int iPIXELFOGTYPE,
Expand Down
4 changes: 2 additions & 2 deletions stdshader_vulkan/src/shaders/HLSL/common_vs_fxc.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ float3 DecompressBoneWeights(const uint3 weights)
}

void SkinPosition(bool bSkinning, const float4 modelPos,
const uint3 boneWeights, const uint4 boneIndices,
const uint3 boneWeights, const uint3 boneIndices,
out float3 worldPos)
{
if (!bSkinning)
Expand Down Expand Up @@ -576,7 +576,7 @@ float CosineTermInternal(const float3 worldPos, const float3 worldNormal, int li
float3 lightDir = normalize(cLightInfo[lightNum].pos.xyz - worldPos);

// Select the above direction or the one in the structure, based upon light type
lightDir = lerp(lightDir, -cLightInfo[lightNum].dir.xyz, cLightInfo[lightNum].bIsDirectional);
lightDir = lerp(lightDir, -cLightInfo[lightNum].dir.xyz, cLightInfo[lightNum].bIsDirectional.xxx);

// compute N dot L
float NDotL = dot(worldNormal, lightDir);
Expand Down
2 changes: 1 addition & 1 deletion stdshader_vulkan/src/shaders/HLSL/fillrate.frag.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cbuffer PixelShaderCustomConstants
float4 g_ColorConstant;
};

float4 main() : COLOR
float4 main() : SV_TARGET
{
float4 result = (g_ColorConstant * (1.0 / 2.0));

Expand Down
8 changes: 6 additions & 2 deletions stdshader_vulkan/src/shaders/HLSL/fillrate.vert.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
struct VS_INPUT
{
float4 vPos : POSITION;
float4 vBoneWeights : BLENDWEIGHT;
float4 vBoneIndices : BLENDINDICES;
float3 vBoneWeights : BLENDWEIGHT;
uint3 vBoneIndices : BLENDINDICES;
};

struct VS_OUTPUT
Expand All @@ -21,5 +21,9 @@ VS_OUTPUT main(const VS_INPUT v)

o.vProjPos = mul(float4(worldPos, 1), cViewProj);

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

return o;
}
53 changes: 53 additions & 0 deletions stdshader_vulkan/src/shaders/HLSL/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# DirectXShaderCompiler
# SHADER_COMPILER:=dxc.exe
# COMPILER_ARGS:=-spirv -E main -WX -fspv-debug=line -fspv-reflect -No -fvk-s-shift 100 0 -fvk-t-shift 200 0
# COMPILER_ARGS_VERT:=-T vs_6_0 -fvk-invert-y
# COMPILER_ARGS_FRAG:=-T ps_6_0
# COMPILER_ARGS_TARGET:=-Fo


# glslc
# SHADER_COMPILER:=glslc.exe
# COMPILER_ARGS:= -fhlsl-functionality1 -fsampler-binding-base 100 -ftexture-binding-base 200 -g -DINVERT_Y
# COMPILER_ARGS_VERT:= -fshader-stage=vert
# COMPILER_ARGS_FRAG:= -fshader-stage=frag
# COMPILER_ARGS_TARGET:= -o

# glslangValidator
SHADER_COMPILER:=glslangValidator.exe
COMPILER_ARGS:= -fhlsl_functionality1 --shift-sampler-binding 100 --shift-texture-binding 200 --invert-y -V -e main
COMPILER_ARGS_VERT:=
COMPILER_ARGS_FRAG:=
COMPILER_ARGS_TARGET:= -o

SPIRV_VAL:=spirv-val.exe
SPIRV_DIS:=spirv-dis.exe

SOURCE_DIR:=
TARGET_DIR:=../Generated/

SOURCE_FILES:=$(wildcard *.hlsl)
INCLUDE_FILES:=$(wildcard *.hlsli)
OUTPUT_FILES_HEADER:=$(patsubst $(SOURCE_DIR)%.hlsl, $(TARGET_DIR)%.h, $(SOURCE_FILES))
OUTPUT_FILES_SPIRV:=$(patsubst $(SOURCE_DIR)%.hlsl, $(TARGET_DIR)%.spirv, $(SOURCE_FILES))
OUTPUT_FILES_SPIRV_DIS:=$(patsubst $(SOURCE_DIR)%.hlsl, $(TARGET_DIR)%.spirv_dis, $(SOURCE_FILES))

marker.tlog : $(OUTPUT_FILES_SPIRV) $(OUTPUT_FILES_SPIRV_DIS) $(OUTPUT_FILES_HEADER)
touch marker.tlog

clean :
rm -rf $(TARGET_DIR)*

$(TARGET_DIR)%.vert.spirv : %.vert.hlsl $(INCLUDE_FILES) makefile
$(SHADER_COMPILER) $(COMPILER_ARGS) $(COMPILER_ARGS_VERT) $< $(COMPILER_ARGS_TARGET) $@
$(TARGET_DIR)%.frag.spirv : %.frag.hlsl $(INCLUDE_FILES) makefile
$(SHADER_COMPILER) $(COMPILER_ARGS) $(COMPILER_ARGS_FRAG) $< $(COMPILER_ARGS_TARGET) $@

$(TARGET_DIR)%.h : $(TARGET_DIR)%.spirv $(TARGET_DIR)%.spirv_val
cd $(TARGET_DIR) && xxd -i -u $(notdir $<) $(notdir $@)

$(TARGET_DIR)%.spirv_dis : $(TARGET_DIR)%.spirv
$(SPIRV_DIS) -o $@ $<

$(TARGET_DIR)%.spirv_val : $(TARGET_DIR)%.spirv
$(SPIRV_VAL) $< && touch $@
39 changes: 19 additions & 20 deletions stdshader_vulkan/src/shaders/HLSL/xlitgeneric.common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ struct VS_TO_PS_INTERSTAGE_DATA

float3 worldVertToEyeVector : TEXCOORD3;
float3 worldSpaceNormal : TEXCOORD4;
float4 worldSpaceTangent;
float3 worldSpaceBinormal;
float4 lightAtten;
float4 worldSpaceTangent : TANGENT;
float3 worldSpaceBinormal : BINORMAL;
float4 lightAtten : LIGHT_ATTEN;

float4 vProjPos : TEXCOORD6;
float4 worldPos_ProjPosZ : TEXCOORD7;
Expand All @@ -56,33 +56,32 @@ struct VS_TO_PS_INTERSTAGE_DATA
#error "Either VERTEX_SHADER or PIXEL_SHADER must be defined"
#endif

static int SPEC_CONST_ID_NEXT = SPEC_CONST_ID_BASE + 1;
[[vk::constant_id(SPEC_CONST_ID_NEXT++)]] const bool AMBIENT_LIGHT = false;
[[vk::constant_id(SPEC_CONST_ID_BASE + 1)]] const bool TEXACTIVE_BASETEXTURE = false;
[[vk::constant_id(SPEC_CONST_ID_BASE + 2)]] const bool TEXACTIVE_BUMPMAP = false;
[[vk::constant_id(SPEC_CONST_ID_BASE + 3)]] const bool TEXACTIVE_LIGHTWARP = false;
[[vk::constant_id(SPEC_CONST_ID_BASE + 1)]] const bool AMBIENT_LIGHT = false;
[[vk::constant_id(SPEC_CONST_ID_BASE + 2)]] const bool TEXACTIVE_BASETEXTURE = false;
[[vk::constant_id(SPEC_CONST_ID_BASE + 3)]] const bool TEXACTIVE_BUMPMAP = false;
[[vk::constant_id(SPEC_CONST_ID_BASE + 4)]] const bool TEXACTIVE_LIGHTWARP = false;

#if false // TODO: Array-based textures
[[vk::constant_id(SPEC_CONST_ID_BASE + 3)]] const uint TEXTURE_COUNT = 1;
[[vk::constant_id(SPEC_CONST_ID_BASE + 4)]] const uint SAMPLER_COUNT = 1;
[[vk::constant_id(SPEC_CONST_ID_BASE + 5)]] const uint TEXTURE_COUNT = 1;
[[vk::constant_id(SPEC_CONST_ID_BASE + 6)]] const uint SAMPLER_COUNT = 1;

[[vk::constant_id(SPEC_CONST_ID_BASE + 5)]] const uint TEXINDEX_BASE = 0;
[[vk::constant_id(SPEC_CONST_ID_BASE + 5)]] const uint SMPINDEX_BASE = 0;
[[vk::constant_id(SPEC_CONST_ID_BASE + 7)]] const uint TEXINDEX_BASE = 0;
[[vk::constant_id(SPEC_CONST_ID_BASE + 8)]] const uint SMPINDEX_BASE = 0;

[[vk::binding(100)]] Texture2D g_Textures[TEXTURE_COUNT];
[[vk::binding(100)]] SamplerState g_Samplers[SAMPLER_COUNT];
#endif

[[vk::binding(0)]] Texture2D BaseTexture;
[[vk::binding(0)]] SamplerState BaseTextureSampler;
Texture2D BaseTexture : register(t0);
SamplerState BaseTextureSampler : register(s0);

[[vk::binding(1)]] Texture2D BumpMapTexture;
[[vk::binding(1)]] SamplerState BumpMapTextureSampler;
Texture2D BumpMapTexture : register(t1);
SamplerState BumpMapTextureSampler : register(s1);

[[vk::binding(2)]] Texture2D morphTexture;
[[vk::binding(2)]] SamplerState morphSampler;
Texture2D morphTexture : register(t2);
SamplerState morphSampler : register(s2);

[[vk::binding(3)]] Texture2D LightWarpTexture;
[[vk::binding(3)]] SamplerState LightWarpTextureSampler;
Texture2D LightWarpTexture : register(t3);
SamplerState LightWarpTextureSampler : register(s3);

#endif // XLITGENERIC_COMMON_HLSLI_INCLUDE_GUARD
6 changes: 3 additions & 3 deletions stdshader_vulkan/src/shaders/HLSL/xlitgeneric.frag.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static float3 DiffuseTerm(const float3 worldNormal, const float3 lightDir,
float3 fOut = float3(fResult, fResult, fResult);
if (TEXACTIVE_LIGHTWARP)
{
fOut = 2.0f * LightWarpTexture.Sample(LightWarpTextureSampler, fResult);
fOut = 2.0f * LightWarpTexture.Sample(LightWarpTextureSampler, fResult).rgb;
}

return fOut;
Expand Down Expand Up @@ -135,7 +135,7 @@ static float3 PixelShaderDoLighting(const float3 worldPos, const float3 worldNor
return linearColor;
}

float4 main(PS_INPUT i) : SV_Target
float4 main(PS_INPUT i) : SV_TARGET
{
float3 diffuseColor = (float3)1;
if (DIFFUSELIGHTING || VERTEXCOLOR)
Expand All @@ -146,7 +146,7 @@ float4 main(PS_INPUT i) : SV_Target
baseTextureColor = BaseTexture.Sample(BaseTextureSampler, i.baseTexCoord.xy);

if (NORMALMAPPING)
UpdateWorldNormalFromNormalMap(i.worldSpaceNormal, i.baseTexCoord.xy, i.worldSpaceTangent, i.worldSpaceBinormal);
UpdateWorldNormalFromNormalMap(i.worldSpaceNormal, i.baseTexCoord.xy, i.worldSpaceTangent.xyz, i.worldSpaceBinormal);

if (DIFFUSELIGHTING)
{
Expand Down
4 changes: 4 additions & 0 deletions stdshader_vulkan/src/shaders/HLSL/xlitgeneric.vert.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ VS_OUTPUT main(const VS_INPUT v)
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;
}
24 changes: 10 additions & 14 deletions stdshader_vulkan/stdshader_vulkan_Shared_Project.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@
<Command>copy /Y "$(TargetPath)" "C:\Program Files (x86)\Steam\steamapps\common\Team Fortress 2\bin\stdshader_vulkan.dll"</Command>
</PostBuildEvent>
<Link />
<CustomBuild>
<Command>pushd $(ProjectDir)src\shaders\Generated\
DEL /S %(Filename).h 2&gt;nul
DEL /S %(Filename).spirv 2&gt;nul

glslangvalidator -fhlsl_functionality1 --auto-map-bindings --shift-sampler-binding vert 100 --shift-texture-binding vert 200 --shift-sampler-binding frag 100 --shift-texture-binding frag 200 --invert-y -e main -V %(FullPath) -o %(Filename).spirv

spirv-val %(Filename).spirv
spirv-dis -o %(Filename).spirv_dis --offsets %(Filename).spirv
xxd -i %(Filename).spirv %(Filename).h
<CustomBuild />
<CustomBuildStep>
<Command>pushd $(ProjectDir)src\shaders\HLSL\
C:\Windows\Sysnative\wsl.exe make
popd</Command>
</CustomBuildStep>
<CustomBuildStep>
<Message>Compiling shaders...</Message>
<Outputs>$(ProjectDir)src\shaders\Generated\%(Filename).h;%(Outputs)</Outputs>
<BuildInParallel>true</BuildInParallel>
<OutputItemType>ClInclude</OutputItemType>
</CustomBuild>
<Outputs>$(ProjectDir)src\shaders\Generated\marker.tlog;%(Outputs)</Outputs>
<Inputs>$(TargetFileName);%(Inputs)</Inputs>
<TreatOutputAsContent>false</TreatOutputAsContent>
</CustomBuildStep>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

0 comments on commit 3d41696

Please sign in to comment.