From 179db8ad212586ffe3d7d9881c57c01843168ec8 Mon Sep 17 00:00:00 2001 From: Ilya Perapechka Date: Fri, 1 Nov 2024 17:05:37 +0200 Subject: [PATCH 1/7] feat: added missing pbr permutations for pre-compilation. --- src/ShaderCache.cpp | 36 +++++++++++++------------ src/ShaderCache.h | 2 +- src/State.cpp | 2 +- src/TruePBR.cpp | 66 ++++++--------------------------------------- 4 files changed, 29 insertions(+), 77 deletions(-) diff --git a/src/ShaderCache.cpp b/src/ShaderCache.cpp index 9b1317a31..d907dac48 100644 --- a/src/ShaderCache.cpp +++ b/src/ShaderCache.cpp @@ -17,7 +17,7 @@ namespace SIE { namespace SShaderCache { - static void GetShaderDefines(const RE::BSShader&, uint32_t, D3D_SHADER_MACRO*); + static void GetShaderDefines(const RE::BSShader&, ShaderClass, uint32_t, D3D_SHADER_MACRO*); static std::string GetShaderString(ShaderClass, const RE::BSShader&, uint32_t, bool = false); /** @brief Get the BSShader::Type from the ShaderString @@ -52,7 +52,7 @@ namespace SIE return 0x3F & (descriptor >> 24); } - static void GetLightingShaderDefines(uint32_t descriptor, std::span defines) + static void GetLightingShaderDefines(ShaderClass shaderClass, uint32_t descriptor, std::span defines) { static REL::Relocation VanillaGetLightingShaderDefines(RELOCATION_ID(101631, 108698)); VanillaGetLightingShaderDefines(descriptor, defines.data()); @@ -62,10 +62,12 @@ namespace SIE if (descriptor & static_cast(ShaderCache::LightingShaderFlags::Deferred)) { defines[lastIndex++] = { "DEFERRED", nullptr }; } - if ((descriptor & static_cast(ShaderCache::LightingShaderFlags::TruePbr)) != 0) { - defines[lastIndex++] = { "TRUE_PBR", nullptr }; - if ((descriptor & static_cast(ShaderCache::LightingShaderFlags::AnisoLighting)) != 0) { - defines[lastIndex++] = { "GLINT", nullptr }; + if (shaderClass == ShaderClass::Pixel) { + if ((descriptor & static_cast(ShaderCache::LightingShaderFlags::TruePbr)) != 0) { + defines[lastIndex++] = { "TRUE_PBR", nullptr }; + if ((descriptor & static_cast(ShaderCache::LightingShaderFlags::AnisoLighting)) != 0) { + defines[lastIndex++] = { "GLINT", nullptr }; + } } } @@ -196,13 +198,13 @@ namespace SIE defines[lastIndex] = { nullptr, nullptr }; } - static void GetGrassShaderDefines(uint32_t descriptor, std::span defines) + static void GetGrassShaderDefines(ShaderClass shaderClass, uint32_t descriptor, std::span defines) { const auto technique = descriptor & 0b1111; size_t lastIndex = 0; if (technique == static_cast(ShaderCache::GrassShaderTechniques::RenderDepth)) { defines[lastIndex++] = { "RENDER_DEPTH", nullptr }; - } else if (technique == static_cast(ShaderCache::GrassShaderTechniques::TruePbr)) { + } else if (shaderClass == ShaderClass::Pixel && technique == static_cast(ShaderCache::GrassShaderTechniques::TruePbr)) { defines[lastIndex++] = { "TRUE_PBR", nullptr }; } if (descriptor & static_cast(ShaderCache::GrassShaderFlags::AlphaTest)) { @@ -592,11 +594,11 @@ namespace SIE return; } - static void GetShaderDefines(const RE::BSShader& shader, uint32_t descriptor, std::span defines) + static void GetShaderDefines(const RE::BSShader& shader, ShaderClass shaderClass, uint32_t descriptor, std::span defines) { switch (shader.shaderType.get()) { case RE::BSShader::Type::Grass: - GetGrassShaderDefines(descriptor, defines); + GetGrassShaderDefines(shaderClass, descriptor, defines); break; case RE::BSShader::Type::Sky: GetSkyShaderDefines(descriptor, defines); @@ -611,7 +613,7 @@ namespace SIE GetImagespaceShaderDefines(shader, defines); break; case RE::BSShader::Type::Lighting: - GetLightingShaderDefines(descriptor, defines); + GetLightingShaderDefines(shaderClass, descriptor, defines); break; case RE::BSShader::Type::DistantTree: GetDistantTreeShaderDefines(descriptor, defines); @@ -880,7 +882,7 @@ namespace SIE { "PLightColorB", effectPSConstants.PLightColorB }, { "DLightColor", effectPSConstants.DLightColor }, { "VPOSOffset", effectPSConstants.VPOSOffset }, - { "CameraData", effectPSConstants.CameraData }, + { "CameraDataEffect", effectPSConstants.CameraData }, { "FilteringParam", effectPSConstants.FilteringParam }, { "BaseColor", effectPSConstants.BaseColor }, { "BaseColorScale", effectPSConstants.BaseColorScale }, @@ -926,7 +928,7 @@ namespace SIE { "BlendRadius", 5 }, { "PosAdjust", 6 }, { "ReflectPlane", 7 }, - { "CameraData", 8 }, + { "CameraDataWater", 8 }, { "ProjData", 9 }, { "VarAmounts", 10 }, { "FogParam", 11 }, @@ -1238,7 +1240,7 @@ namespace SIE { auto sourceShaderFile = shader.fxpFilename; std::array defines{}; - SIE::SShaderCache::GetShaderDefines(shader, descriptor, std::span{ defines }); + SIE::SShaderCache::GetShaderDefines(shader, shaderClass, descriptor, std::span{ defines }); std::string result; if (hashkey) // generate hashkey so don't include descriptor result = fmt::format("{}:{}:{}", sourceShaderFile, magic_enum::enum_name(shaderClass), SIE::SShaderCache::MergeDefinesString(defines, true)); @@ -1315,7 +1317,7 @@ namespace SIE defines[lastIndex++] = { shaderDefines->at(i).first.c_str(), shaderDefines->at(i).second.c_str() }; } defines[lastIndex] = { nullptr, nullptr }; // do final entry - GetShaderDefines(shader, descriptor, std::span{ defines }.subspan(lastIndex)); + GetShaderDefines(shader, shaderClass, descriptor, std::span{ defines }.subspan(lastIndex)); const std::wstring path = GetShaderPath( shader.shaderType == RE::BSShader::Type::ImageSpace ? @@ -2365,10 +2367,10 @@ namespace SIE return nullptr; } - std::string ShaderCache::GetDefinesString(const RE::BSShader& shader, uint32_t descriptor) + std::string ShaderCache::GetDefinesString(const RE::BSShader& shader, ShaderClass shaderClass, uint32_t descriptor) { std::array defines{}; - SIE::SShaderCache::GetShaderDefines(shader, descriptor, std::span{ defines }); + SIE::SShaderCache::GetShaderDefines(shader, shaderClass, descriptor, std::span{ defines }); return SIE::SShaderCache::MergeDefinesString(defines, true); } diff --git a/src/ShaderCache.h b/src/ShaderCache.h index 7fca89607..0981d02a3 100644 --- a/src/ShaderCache.h +++ b/src/ShaderCache.h @@ -408,7 +408,7 @@ namespace SIE RE::BSGraphics::ComputeShader* MakeAndAddComputeShader(const RE::BSShader& shader, uint32_t descriptor); - static std::string GetDefinesString(const RE::BSShader& shader, uint32_t descriptor); + static std::string GetDefinesString(const RE::BSShader& shader, ShaderClass shaderClass, uint32_t descriptor); uint64_t GetCachedHitTasks(); uint64_t GetCompletedTasks(); diff --git a/src/State.cpp b/src/State.cpp index 474ffb52f..0099a7b65 100644 --- a/src/State.cpp +++ b/src/State.cpp @@ -79,7 +79,7 @@ void State::Draw() if (IsDeveloperMode()) { BeginPerfEvent(std::format("Draw: CS {}::{:x}::{}", magic_enum::enum_name(currentShader->shaderType.get()), currentPixelDescriptor, currentShader->fxpFilename)); - SetPerfMarker(std::format("Defines: {}", SIE::ShaderCache::GetDefinesString(*currentShader, currentPixelDescriptor))); + SetPerfMarker(std::format("Defines: {}", SIE::ShaderCache::GetDefinesString(*currentShader, SIE::ShaderClass::Pixel, currentPixelDescriptor))); EndPerfEvent(); } } diff --git a/src/TruePBR.cpp b/src/TruePBR.cpp index 2cbb78619..9c0f5ea73 100644 --- a/src/TruePBR.cpp +++ b/src/TruePBR.cpp @@ -489,40 +489,15 @@ namespace Permutations } } - std::unordered_set GeneratePBRLightingVertexPermutations() - { - using enum SIE::ShaderCache::LightingShaderFlags; - - constexpr std::array defaultFlags{ VC, Skinned, WorldMap }; - constexpr std::array projectedUvFlags{ VC, WorldMap }; - constexpr std::array treeFlags{ VC, Skinned }; - constexpr std::array landFlags{ VC }; - - constexpr uint32_t defaultConstantFlags = static_cast(TruePbr); - constexpr uint32_t projectedUvConstantFlags = static_cast(TruePbr) | static_cast(ProjectedUV); - - const std::unordered_set defaultFlagValues = GenerateFlagPermutations(defaultFlags, defaultConstantFlags); - const std::unordered_set projectedUvFlagValues = GenerateFlagPermutations(projectedUvFlags, projectedUvConstantFlags); - const std::unordered_set treeFlagValues = GenerateFlagPermutations(treeFlags, defaultConstantFlags); - const std::unordered_set landFlagValues = GenerateFlagPermutations(landFlags, defaultConstantFlags); - - std::unordered_set result; - AddLightingShaderDescriptors(SIE::ShaderCache::LightingShaderTechniques::None, defaultFlagValues, result); - AddLightingShaderDescriptors(SIE::ShaderCache::LightingShaderTechniques::None, projectedUvFlagValues, result); - AddLightingShaderDescriptors(SIE::ShaderCache::LightingShaderTechniques::TreeAnim, treeFlagValues, result); - AddLightingShaderDescriptors(SIE::ShaderCache::LightingShaderTechniques::MTLand, landFlagValues, result); - AddLightingShaderDescriptors(SIE::ShaderCache::LightingShaderTechniques::MTLandLODBlend, landFlagValues, result); - return result; - } - std::unordered_set GeneratePBRLightingPixelPermutations() { using enum SIE::ShaderCache::LightingShaderFlags; - constexpr std::array defaultFlags{ Skinned, DoAlphaTest, AdditionalAlphaMask }; - constexpr std::array projectedUvFlags{ DoAlphaTest, AdditionalAlphaMask, Snow, BaseObjectIsSnow }; - constexpr std::array lodObjectsFlags{ WorldMap, DoAlphaTest, AdditionalAlphaMask, ProjectedUV }; - constexpr std::array treeFlags{ Skinned, DoAlphaTest, AdditionalAlphaMask }; + constexpr std::array defaultFlags{ Deferred, AnisoLighting, Skinned, DoAlphaTest, AdditionalAlphaMask }; + constexpr std::array projectedUvFlags{ Deferred, AnisoLighting, DoAlphaTest, AdditionalAlphaMask, Snow, BaseObjectIsSnow }; + constexpr std::array lodObjectsFlags{ Deferred, WorldMap, DoAlphaTest, AdditionalAlphaMask, ProjectedUV }; + constexpr std::array treeFlags{ Deferred, AnisoLighting, Skinned, DoAlphaTest, AdditionalAlphaMask }; + constexpr std::array landFlags{ Deferred, AnisoLighting }; constexpr uint32_t defaultConstantFlags = static_cast(TruePbr) | static_cast(VC); constexpr uint32_t projectedUvConstantFlags = static_cast(TruePbr) | static_cast(VC) | static_cast(ProjectedUV); @@ -531,7 +506,7 @@ namespace Permutations const std::unordered_set projectedUvFlagValues = GenerateFlagPermutations(projectedUvFlags, projectedUvConstantFlags); const std::unordered_set lodObjectsFlagValues = GenerateFlagPermutations(lodObjectsFlags, defaultConstantFlags); const std::unordered_set treeFlagValues = GenerateFlagPermutations(treeFlags, defaultConstantFlags); - const std::unordered_set landFlagValues = { defaultConstantFlags }; + const std::unordered_set landFlagValues = GenerateFlagPermutations(landFlags, defaultConstantFlags); std::unordered_set result; AddLightingShaderDescriptors(SIE::ShaderCache::LightingShaderTechniques::None, defaultFlagValues, result); @@ -544,7 +519,7 @@ namespace Permutations return result; } - std::unordered_set GeneratePBRGrassPermutations() + std::unordered_set GeneratePBRGrassPixelPermutations() { using enum SIE::ShaderCache::GrassShaderTechniques; using enum SIE::ShaderCache::GrassShaderFlags; @@ -552,30 +527,12 @@ namespace Permutations return { static_cast(TruePbr), static_cast(TruePbr) | static_cast(AlphaTest) }; } - - std::unordered_set GeneratePBRGrassVertexPermutations() - { - return GeneratePBRGrassPermutations(); - } - - std::unordered_set GeneratePBRGrassPixelPermutations() - { - return GeneratePBRGrassPermutations(); - } } void TruePBR::GenerateShaderPermutations(RE::BSShader* shader) { auto& shaderCache = SIE::ShaderCache::Instance(); if (shader->shaderType == RE::BSShader::Type::Lighting) { - const auto vertexPermutations = Permutations::GeneratePBRLightingVertexPermutations(); - for (auto descriptor : vertexPermutations) { - auto vertexShaderDesriptor = descriptor; - auto pixelShaderDescriptor = descriptor; - State::GetSingleton()->ModifyShaderLookup(*shader, vertexShaderDesriptor, pixelShaderDescriptor); - std::ignore = shaderCache.GetVertexShader(*shader, vertexShaderDesriptor); - } - const auto pixelPermutations = Permutations::GeneratePBRLightingPixelPermutations(); for (auto descriptor : pixelPermutations) { auto vertexShaderDesriptor = descriptor; @@ -584,14 +541,6 @@ void TruePBR::GenerateShaderPermutations(RE::BSShader* shader) std::ignore = shaderCache.GetPixelShader(*shader, pixelShaderDescriptor); } } else if (shader->shaderType == RE::BSShader::Type::Grass) { - const auto vertexPermutations = Permutations::GeneratePBRGrassVertexPermutations(); - for (auto descriptor : vertexPermutations) { - auto vertexShaderDesriptor = descriptor; - auto pixelShaderDescriptor = descriptor; - State::GetSingleton()->ModifyShaderLookup(*shader, vertexShaderDesriptor, pixelShaderDescriptor); - std::ignore = shaderCache.GetVertexShader(*shader, vertexShaderDesriptor); - } - const auto pixelPermutations = Permutations::GeneratePBRGrassPixelPermutations(); for (auto descriptor : pixelPermutations) { auto vertexShaderDesriptor = descriptor; @@ -750,6 +699,7 @@ struct BSLightingShaderProperty_GetRenderPasses lightingFlags &= ~0b111000u; if (isPbr) { lightingFlags |= static_cast(SIE::ShaderCache::LightingShaderFlags::TruePbr); + lightingFlags &= ~static_cast(SIE::ShaderCache::LightingShaderFlags::Specular); if (property->flags.any(RE::BSShaderProperty::EShaderPropertyFlag::kMultiTextureLandscape)) { auto* material = static_cast(property->material); if (material->HasGlint()) { From 2b24471d4b3d78c7aace91a670ea1c87e0409716 Mon Sep 17 00:00:00 2001 From: Ilya Perapechka Date: Sat, 2 Nov 2024 12:21:45 +0200 Subject: [PATCH 2/7] chore: turned BASE_OBJECT_IS_SNOW into flag. --- package/Shaders/Lighting.hlsl | 9 ++++----- src/State.cpp | 3 ++- src/TruePBR.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package/Shaders/Lighting.hlsl b/package/Shaders/Lighting.hlsl index 404eccaac..f9a5a57bd 100644 --- a/package/Shaders/Lighting.hlsl +++ b/package/Shaders/Lighting.hlsl @@ -916,11 +916,10 @@ float3 GetWorldMapBaseColor(float3 originalBaseColor, float3 rawBaseColor, float float GetSnowParameterY(float texProjTmp, float alpha) { -# if defined(BASE_OBJECT_IS_SNOW) - return min(1, texProjTmp + alpha); -# else + if (PixelShaderDescriptor & LightingFlags::BaseObjectIsSnow) { + return min(1, texProjTmp + alpha); + } return texProjTmp; -# endif } # if defined(LOD) @@ -1158,7 +1157,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace # endif // TRUE_PBR # endif // EMAT - + # if defined(SNOW) bool useSnowSpecular = true; # else diff --git a/src/State.cpp b/src/State.cpp index 0099a7b65..8289721b1 100644 --- a/src/State.cpp +++ b/src/State.cpp @@ -515,7 +515,8 @@ void State::ModifyShaderLookup(const RE::BSShader& a_shader, uint& a_vertexDescr a_pixelDescriptor &= ~((uint32_t)SIE::ShaderCache::LightingShaderFlags::AmbientSpecular | (uint32_t)SIE::ShaderCache::LightingShaderFlags::ShadowDir | (uint32_t)SIE::ShaderCache::LightingShaderFlags::DefShadow | - (uint32_t)SIE::ShaderCache::LightingShaderFlags::CharacterLight); + (uint32_t)SIE::ShaderCache::LightingShaderFlags::CharacterLight | + (uint32_t)SIE::ShaderCache::LightingShaderFlags::BaseObjectIsSnow); static auto enableImprovedSnow = RE::GetINISetting("bEnableImprovedSnow:Display"); static bool vr = REL::Module::IsVR(); diff --git a/src/TruePBR.cpp b/src/TruePBR.cpp index 9c0f5ea73..a59b13e31 100644 --- a/src/TruePBR.cpp +++ b/src/TruePBR.cpp @@ -494,7 +494,7 @@ namespace Permutations using enum SIE::ShaderCache::LightingShaderFlags; constexpr std::array defaultFlags{ Deferred, AnisoLighting, Skinned, DoAlphaTest, AdditionalAlphaMask }; - constexpr std::array projectedUvFlags{ Deferred, AnisoLighting, DoAlphaTest, AdditionalAlphaMask, Snow, BaseObjectIsSnow }; + constexpr std::array projectedUvFlags{ Deferred, AnisoLighting, DoAlphaTest, AdditionalAlphaMask, Snow }; constexpr std::array lodObjectsFlags{ Deferred, WorldMap, DoAlphaTest, AdditionalAlphaMask, ProjectedUV }; constexpr std::array treeFlags{ Deferred, AnisoLighting, Skinned, DoAlphaTest, AdditionalAlphaMask }; constexpr std::array landFlags{ Deferred, AnisoLighting }; From 7a4ea9c57444c0a7dd5f23fc424761fa6db44454 Mon Sep 17 00:00:00 2001 From: Jonahex Date: Sat, 2 Nov 2024 10:22:13 +0000 Subject: [PATCH 3/7] =?UTF-8?q?style:=20=F0=9F=8E=A8=20apply=20clang-forma?= =?UTF-8?q?t=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package/Shaders/Lighting.hlsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/Shaders/Lighting.hlsl b/package/Shaders/Lighting.hlsl index f9a5a57bd..60eb92a73 100644 --- a/package/Shaders/Lighting.hlsl +++ b/package/Shaders/Lighting.hlsl @@ -1157,7 +1157,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace # endif // TRUE_PBR # endif // EMAT - + # if defined(SNOW) bool useSnowSpecular = true; # else From 4bbdd60f6a1157d65755e65770502b398ad94165 Mon Sep 17 00:00:00 2001 From: Ilya Perapechka Date: Sat, 2 Nov 2024 20:46:44 +0200 Subject: [PATCH 4/7] chore: merged DO_ALPHA_TEST and ADDITIONAL_ALPHA_MASK into single permutation. --- package/Shaders/Lighting.hlsl | 80 +++++++++++++++++++---------------- src/ShaderCache.cpp | 4 +- src/State.cpp | 5 +++ src/TruePBR.cpp | 8 ++-- 4 files changed, 55 insertions(+), 42 deletions(-) diff --git a/package/Shaders/Lighting.hlsl b/package/Shaders/Lighting.hlsl index f9a5a57bd..ae56be6af 100644 --- a/package/Shaders/Lighting.hlsl +++ b/package/Shaders/Lighting.hlsl @@ -2567,48 +2567,56 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace } # endif # endif -# if !defined(ADDITIONAL_ALPHA_MASK) - alpha *= MaterialData.z; -# else - uint2 alphaMask = input.Position.xy; - alphaMask.x = ((alphaMask.x << 2) & 12); - alphaMask.x = (alphaMask.y & 3) | (alphaMask.x & ~3); - const float maskValues[16] = { - 0.003922, - 0.533333, - 0.133333, - 0.666667, - 0.800000, - 0.266667, - 0.933333, - 0.400000, - 0.200000, - 0.733333, - 0.066667, - 0.600000, - 0.996078, - 0.466667, - 0.866667, - 0.333333, - }; - - float testTmp = 0; - if (MaterialData.z - maskValues[alphaMask.x] < 0) { - discard; - } -# endif // !defined(ADDITIONAL_ALPHA_MASK) +# if defined(DO_ALPHA_TEST) + [branch] if ((PixelShaderDescriptor & LightingFlags::AdditionalAlphaMask) != 0) + { + uint2 alphaMask = input.Position.xy; + alphaMask.x = ((alphaMask.x << 2) & 12); + alphaMask.x = (alphaMask.y & 3) | (alphaMask.x & ~3); + const float maskValues[16] = { + 0.003922, + 0.533333, + 0.133333, + 0.666667, + 0.800000, + 0.266667, + 0.933333, + 0.400000, + 0.200000, + 0.733333, + 0.066667, + 0.600000, + 0.996078, + 0.466667, + 0.866667, + 0.333333, + }; + + float testTmp = 0; + if (MaterialData.z - maskValues[alphaMask.x] < 0) { + discard; + } + } + else +# endif // defined(DO_ALPHA_TEST) + { + alpha *= MaterialData.z; + } # if !(defined(TREE_ANIM) || defined(LODOBJECTSHD) || defined(LODOBJECTS)) alpha *= input.Color.w; # endif // !(defined(TREE_ANIM) || defined(LODOBJECTSHD) || defined(LODOBJECTS)) # if defined(DO_ALPHA_TEST) + [branch] if ((PixelShaderDescriptor & LightingFlags::DoAlphaTest) != 0) + { # if defined(DEPTH_WRITE_DECALS) - if (alpha - 0.0156862754 < 0) { - discard; - } - alpha = saturate(1.05 * alpha); + if (alpha - 0.0156862754 < 0) { + discard; + } + alpha = saturate(1.05 * alpha); # endif // DEPTH_WRITE_DECALS - if (alpha - AlphaTestRefRS < 0) { - discard; + if (alpha - AlphaTestRefRS < 0) { + discard; + } } # endif // DO_ALPHA_TEST psout.Diffuse.w = alpha; diff --git a/src/ShaderCache.cpp b/src/ShaderCache.cpp index d907dac48..33de0a835 100644 --- a/src/ShaderCache.cpp +++ b/src/ShaderCache.cpp @@ -204,7 +204,7 @@ namespace SIE size_t lastIndex = 0; if (technique == static_cast(ShaderCache::GrassShaderTechniques::RenderDepth)) { defines[lastIndex++] = { "RENDER_DEPTH", nullptr }; - } else if (shaderClass == ShaderClass::Pixel && technique == static_cast(ShaderCache::GrassShaderTechniques::TruePbr)) { + } else if (technique == static_cast(ShaderCache::GrassShaderTechniques::TruePbr)) { defines[lastIndex++] = { "TRUE_PBR", nullptr }; } if (descriptor & static_cast(ShaderCache::GrassShaderFlags::AlphaTest)) { @@ -598,7 +598,7 @@ namespace SIE { switch (shader.shaderType.get()) { case RE::BSShader::Type::Grass: - GetGrassShaderDefines(shaderClass, descriptor, defines); + GetGrassShaderDefines(descriptor, defines); break; case RE::BSShader::Type::Sky: GetSkyShaderDefines(descriptor, defines); diff --git a/src/State.cpp b/src/State.cpp index 8289721b1..da35c53b8 100644 --- a/src/State.cpp +++ b/src/State.cpp @@ -517,6 +517,11 @@ void State::ModifyShaderLookup(const RE::BSShader& a_shader, uint& a_vertexDescr (uint32_t)SIE::ShaderCache::LightingShaderFlags::DefShadow | (uint32_t)SIE::ShaderCache::LightingShaderFlags::CharacterLight | (uint32_t)SIE::ShaderCache::LightingShaderFlags::BaseObjectIsSnow); + if (a_pixelDescriptor & (uint32_t)SIE::ShaderCache::LightingShaderFlags::AdditionalAlphaMask) + { + a_pixelDescriptor |= (uint32_t)SIE::ShaderCache::LightingShaderFlags::DoAlphaTest; + a_pixelDescriptor &= ~(uint32_t)SIE::ShaderCache::LightingShaderFlags::AdditionalAlphaMask; + } static auto enableImprovedSnow = RE::GetINISetting("bEnableImprovedSnow:Display"); static bool vr = REL::Module::IsVR(); diff --git a/src/TruePBR.cpp b/src/TruePBR.cpp index a59b13e31..fc0b94636 100644 --- a/src/TruePBR.cpp +++ b/src/TruePBR.cpp @@ -493,10 +493,10 @@ namespace Permutations { using enum SIE::ShaderCache::LightingShaderFlags; - constexpr std::array defaultFlags{ Deferred, AnisoLighting, Skinned, DoAlphaTest, AdditionalAlphaMask }; - constexpr std::array projectedUvFlags{ Deferred, AnisoLighting, DoAlphaTest, AdditionalAlphaMask, Snow }; - constexpr std::array lodObjectsFlags{ Deferred, WorldMap, DoAlphaTest, AdditionalAlphaMask, ProjectedUV }; - constexpr std::array treeFlags{ Deferred, AnisoLighting, Skinned, DoAlphaTest, AdditionalAlphaMask }; + constexpr std::array defaultFlags{ Deferred, AnisoLighting, Skinned, DoAlphaTest }; + constexpr std::array projectedUvFlags{ Deferred, AnisoLighting, DoAlphaTest, Snow }; + constexpr std::array lodObjectsFlags{ Deferred, WorldMap, DoAlphaTest, ProjectedUV }; + constexpr std::array treeFlags{ Deferred, AnisoLighting, Skinned, DoAlphaTest }; constexpr std::array landFlags{ Deferred, AnisoLighting }; constexpr uint32_t defaultConstantFlags = static_cast(TruePbr) | static_cast(VC); From 378cd5d62a6a40f20666e734ee69de75c1b0a113 Mon Sep 17 00:00:00 2001 From: Jonahex Date: Sat, 2 Nov 2024 18:47:39 +0000 Subject: [PATCH 5/7] =?UTF-8?q?style:=20=F0=9F=8E=A8=20apply=20clang-forma?= =?UTF-8?q?t=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package/Shaders/Lighting.hlsl | 10 +++++----- src/State.cpp | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/package/Shaders/Lighting.hlsl b/package/Shaders/Lighting.hlsl index c0c637768..789a7d18a 100644 --- a/package/Shaders/Lighting.hlsl +++ b/package/Shaders/Lighting.hlsl @@ -2569,7 +2569,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace # endif # if defined(DO_ALPHA_TEST) [branch] if ((PixelShaderDescriptor & LightingFlags::AdditionalAlphaMask) != 0) - { + { uint2 alphaMask = input.Position.xy; alphaMask.x = ((alphaMask.x << 2) & 12); alphaMask.x = (alphaMask.y & 3) | (alphaMask.x & ~3); @@ -2596,18 +2596,18 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace if (MaterialData.z - maskValues[alphaMask.x] < 0) { discard; } - } + } else # endif // defined(DO_ALPHA_TEST) - { + { alpha *= MaterialData.z; - } + } # if !(defined(TREE_ANIM) || defined(LODOBJECTSHD) || defined(LODOBJECTS)) alpha *= input.Color.w; # endif // !(defined(TREE_ANIM) || defined(LODOBJECTSHD) || defined(LODOBJECTS)) # if defined(DO_ALPHA_TEST) [branch] if ((PixelShaderDescriptor & LightingFlags::DoAlphaTest) != 0) - { + { # if defined(DEPTH_WRITE_DECALS) if (alpha - 0.0156862754 < 0) { discard; diff --git a/src/State.cpp b/src/State.cpp index da35c53b8..fa2d45585 100644 --- a/src/State.cpp +++ b/src/State.cpp @@ -517,8 +517,7 @@ void State::ModifyShaderLookup(const RE::BSShader& a_shader, uint& a_vertexDescr (uint32_t)SIE::ShaderCache::LightingShaderFlags::DefShadow | (uint32_t)SIE::ShaderCache::LightingShaderFlags::CharacterLight | (uint32_t)SIE::ShaderCache::LightingShaderFlags::BaseObjectIsSnow); - if (a_pixelDescriptor & (uint32_t)SIE::ShaderCache::LightingShaderFlags::AdditionalAlphaMask) - { + if (a_pixelDescriptor & (uint32_t)SIE::ShaderCache::LightingShaderFlags::AdditionalAlphaMask) { a_pixelDescriptor |= (uint32_t)SIE::ShaderCache::LightingShaderFlags::DoAlphaTest; a_pixelDescriptor &= ~(uint32_t)SIE::ShaderCache::LightingShaderFlags::AdditionalAlphaMask; } From c1227f8d685ec30853b4ba355a207ded68041389 Mon Sep 17 00:00:00 2001 From: Ilya Perapechka Date: Sat, 2 Nov 2024 22:16:33 +0200 Subject: [PATCH 6/7] chore: true exclusion of pbr vertex permutations. --- src/ShaderCache.cpp | 30 ++++++++++++++---------------- src/ShaderCache.h | 2 +- src/State.cpp | 16 ++++++++++++++-- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/ShaderCache.cpp b/src/ShaderCache.cpp index 33de0a835..f44152603 100644 --- a/src/ShaderCache.cpp +++ b/src/ShaderCache.cpp @@ -17,7 +17,7 @@ namespace SIE { namespace SShaderCache { - static void GetShaderDefines(const RE::BSShader&, ShaderClass, uint32_t, D3D_SHADER_MACRO*); + static void GetShaderDefines(const RE::BSShader&, uint32_t, D3D_SHADER_MACRO*); static std::string GetShaderString(ShaderClass, const RE::BSShader&, uint32_t, bool = false); /** @brief Get the BSShader::Type from the ShaderString @@ -52,7 +52,7 @@ namespace SIE return 0x3F & (descriptor >> 24); } - static void GetLightingShaderDefines(ShaderClass shaderClass, uint32_t descriptor, std::span defines) + static void GetLightingShaderDefines(uint32_t descriptor, std::span defines) { static REL::Relocation VanillaGetLightingShaderDefines(RELOCATION_ID(101631, 108698)); VanillaGetLightingShaderDefines(descriptor, defines.data()); @@ -62,12 +62,10 @@ namespace SIE if (descriptor & static_cast(ShaderCache::LightingShaderFlags::Deferred)) { defines[lastIndex++] = { "DEFERRED", nullptr }; } - if (shaderClass == ShaderClass::Pixel) { - if ((descriptor & static_cast(ShaderCache::LightingShaderFlags::TruePbr)) != 0) { - defines[lastIndex++] = { "TRUE_PBR", nullptr }; - if ((descriptor & static_cast(ShaderCache::LightingShaderFlags::AnisoLighting)) != 0) { - defines[lastIndex++] = { "GLINT", nullptr }; - } + if ((descriptor & static_cast(ShaderCache::LightingShaderFlags::TruePbr)) != 0) { + defines[lastIndex++] = { "TRUE_PBR", nullptr }; + if ((descriptor & static_cast(ShaderCache::LightingShaderFlags::AnisoLighting)) != 0) { + defines[lastIndex++] = { "GLINT", nullptr }; } } @@ -198,7 +196,7 @@ namespace SIE defines[lastIndex] = { nullptr, nullptr }; } - static void GetGrassShaderDefines(ShaderClass shaderClass, uint32_t descriptor, std::span defines) + static void GetGrassShaderDefines(uint32_t descriptor, std::span defines) { const auto technique = descriptor & 0b1111; size_t lastIndex = 0; @@ -594,7 +592,7 @@ namespace SIE return; } - static void GetShaderDefines(const RE::BSShader& shader, ShaderClass shaderClass, uint32_t descriptor, std::span defines) + static void GetShaderDefines(const RE::BSShader& shader, uint32_t descriptor, std::span defines) { switch (shader.shaderType.get()) { case RE::BSShader::Type::Grass: @@ -613,7 +611,7 @@ namespace SIE GetImagespaceShaderDefines(shader, defines); break; case RE::BSShader::Type::Lighting: - GetLightingShaderDefines(shaderClass, descriptor, defines); + GetLightingShaderDefines(descriptor, defines); break; case RE::BSShader::Type::DistantTree: GetDistantTreeShaderDefines(descriptor, defines); @@ -1240,7 +1238,7 @@ namespace SIE { auto sourceShaderFile = shader.fxpFilename; std::array defines{}; - SIE::SShaderCache::GetShaderDefines(shader, shaderClass, descriptor, std::span{ defines }); + SIE::SShaderCache::GetShaderDefines(shader, descriptor, std::span{ defines }); std::string result; if (hashkey) // generate hashkey so don't include descriptor result = fmt::format("{}:{}:{}", sourceShaderFile, magic_enum::enum_name(shaderClass), SIE::SShaderCache::MergeDefinesString(defines, true)); @@ -1317,7 +1315,7 @@ namespace SIE defines[lastIndex++] = { shaderDefines->at(i).first.c_str(), shaderDefines->at(i).second.c_str() }; } defines[lastIndex] = { nullptr, nullptr }; // do final entry - GetShaderDefines(shader, shaderClass, descriptor, std::span{ defines }.subspan(lastIndex)); + GetShaderDefines(shader, descriptor, std::span{ defines }.subspan(lastIndex)); const std::wstring path = GetShaderPath( shader.shaderType == RE::BSShader::Type::ImageSpace ? @@ -1335,7 +1333,7 @@ namespace SIE const uint32_t flags = !State::GetSingleton()->IsDeveloperMode() ? D3DCOMPILE_OPTIMIZATION_LEVEL3 : D3DCOMPILE_DEBUG; const HRESULT compileResult = D3DCompileFromFile(path.c_str(), defines.data(), D3D_COMPILE_STANDARD_FILE_INCLUDE, "main", GetShaderProfile(shaderClass), flags, 0, &shaderBlob, &errorBlob); - + if (FAILED(compileResult)) { if (errorBlob != nullptr) { logger::error("Failed to compile {} shader {}::{:X}:\n{}", @@ -2367,10 +2365,10 @@ namespace SIE return nullptr; } - std::string ShaderCache::GetDefinesString(const RE::BSShader& shader, ShaderClass shaderClass, uint32_t descriptor) + std::string ShaderCache::GetDefinesString(const RE::BSShader& shader, uint32_t descriptor) { std::array defines{}; - SIE::SShaderCache::GetShaderDefines(shader, shaderClass, descriptor, std::span{ defines }); + SIE::SShaderCache::GetShaderDefines(shader, descriptor, std::span{ defines }); return SIE::SShaderCache::MergeDefinesString(defines, true); } diff --git a/src/ShaderCache.h b/src/ShaderCache.h index 0981d02a3..7fca89607 100644 --- a/src/ShaderCache.h +++ b/src/ShaderCache.h @@ -408,7 +408,7 @@ namespace SIE RE::BSGraphics::ComputeShader* MakeAndAddComputeShader(const RE::BSShader& shader, uint32_t descriptor); - static std::string GetDefinesString(const RE::BSShader& shader, ShaderClass shaderClass, uint32_t descriptor); + static std::string GetDefinesString(const RE::BSShader& shader, uint32_t descriptor); uint64_t GetCachedHitTasks(); uint64_t GetCompletedTasks(); diff --git a/src/State.cpp b/src/State.cpp index da35c53b8..0360b8fb9 100644 --- a/src/State.cpp +++ b/src/State.cpp @@ -79,7 +79,7 @@ void State::Draw() if (IsDeveloperMode()) { BeginPerfEvent(std::format("Draw: CS {}::{:x}::{}", magic_enum::enum_name(currentShader->shaderType.get()), currentPixelDescriptor, currentShader->fxpFilename)); - SetPerfMarker(std::format("Defines: {}", SIE::ShaderCache::GetDefinesString(*currentShader, SIE::ShaderClass::Pixel, currentPixelDescriptor))); + SetPerfMarker(std::format("Defines: {}", SIE::ShaderCache::GetDefinesString(*currentShader, currentPixelDescriptor))); EndPerfEvent(); } } @@ -510,7 +510,8 @@ void State::ModifyShaderLookup(const RE::BSShader& a_shader, uint& a_vertexDescr (uint32_t)SIE::ShaderCache::LightingShaderFlags::Specular | (uint32_t)SIE::ShaderCache::LightingShaderFlags::AnisoLighting | (uint32_t)SIE::ShaderCache::LightingShaderFlags::BaseObjectIsSnow | - (uint32_t)SIE::ShaderCache::LightingShaderFlags::Snow); + (uint32_t)SIE::ShaderCache::LightingShaderFlags::Snow | + (uint32_t)SIE::ShaderCache::LightingShaderFlags::TruePbr); a_pixelDescriptor &= ~((uint32_t)SIE::ShaderCache::LightingShaderFlags::AmbientSpecular | (uint32_t)SIE::ShaderCache::LightingShaderFlags::ShadowDir | @@ -585,6 +586,17 @@ void State::ModifyShaderLookup(const RE::BSShader& a_shader, uint& a_vertexDescr a_pixelDescriptor |= 256; } break; + case RE::BSShader::Type::Grass: + { + auto technique = a_vertexDescriptor & 0xF; + auto flags = a_vertexDescriptor & ~0xF; + if (technique == static_cast(SIE::ShaderCache::GrassShaderTechniques::TruePbr)) + { + technique = 0; + } + a_vertexDescriptor = flags | technique; + } + break; } } } From 38a08ed072076ac8ee0f3fca577f3b8ca0b6c068 Mon Sep 17 00:00:00 2001 From: Jonahex Date: Sat, 2 Nov 2024 20:17:06 +0000 Subject: [PATCH 7/7] =?UTF-8?q?style:=20=F0=9F=8E=A8=20apply=20clang-forma?= =?UTF-8?q?t=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ShaderCache.cpp | 2 +- src/State.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ShaderCache.cpp b/src/ShaderCache.cpp index f44152603..ea3de2497 100644 --- a/src/ShaderCache.cpp +++ b/src/ShaderCache.cpp @@ -1333,7 +1333,7 @@ namespace SIE const uint32_t flags = !State::GetSingleton()->IsDeveloperMode() ? D3DCOMPILE_OPTIMIZATION_LEVEL3 : D3DCOMPILE_DEBUG; const HRESULT compileResult = D3DCompileFromFile(path.c_str(), defines.data(), D3D_COMPILE_STANDARD_FILE_INCLUDE, "main", GetShaderProfile(shaderClass), flags, 0, &shaderBlob, &errorBlob); - + if (FAILED(compileResult)) { if (errorBlob != nullptr) { logger::error("Failed to compile {} shader {}::{:X}:\n{}", diff --git a/src/State.cpp b/src/State.cpp index 1702698ff..930d7e5a6 100644 --- a/src/State.cpp +++ b/src/State.cpp @@ -589,8 +589,7 @@ void State::ModifyShaderLookup(const RE::BSShader& a_shader, uint& a_vertexDescr { auto technique = a_vertexDescriptor & 0xF; auto flags = a_vertexDescriptor & ~0xF; - if (technique == static_cast(SIE::ShaderCache::GrassShaderTechniques::TruePbr)) - { + if (technique == static_cast(SIE::ShaderCache::GrassShaderTechniques::TruePbr)) { technique = 0; } a_vertexDescriptor = flags | technique;