Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow user specific configs #67

Merged
merged 9 commits into from
Aug 31, 2023
26 changes: 13 additions & 13 deletions features/Grass Lighting/Shaders/RunGrass.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
float3 normalizedLightDirection = normalize(lightDirection);

float lightAngle = dot(worldNormal.xyz, normalizedLightDirection.xyz);

float3 normalizedLightDirectionVS = WorldToView(normalizedLightDirection, true, eyeIndex);
if (light.firstPersonShadow)
lightColor *= ContactShadows(viewPosition, screenUV, screenNoise, normalizedLightDirectionVS, shadowQualityScale, 0.0, eyeIndex);
Expand Down Expand Up @@ -557,21 +557,21 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
specularColor *= specColor.w * SpecularStrength;
color.xyz += specularColor;

# if defined(LIGHT_LIMIT_FIX)
if (perPassLLF[0].EnableLightsVisualisation){
if (perPassLLF[0].LightsVisualisationMode == 0){
psout.Albedo.xyz = TurboColormap(0);
} else if (perPassLLF[0].LightsVisualisationMode == 1){
psout.Albedo.xyz = TurboColormap(0);
# if defined(LIGHT_LIMIT_FIX)
if (perPassLLF[0].EnableLightsVisualisation) {
if (perPassLLF[0].LightsVisualisationMode == 0) {
psout.Albedo.xyz = TurboColormap(0);
} else if (perPassLLF[0].LightsVisualisationMode == 1) {
psout.Albedo.xyz = TurboColormap(0);
} else {
psout.Albedo.xyz = TurboColormap((float)lightCount / 128.0);
}
} else {
psout.Albedo.xyz = TurboColormap((float)lightCount / 128.0);
psout.Albedo.xyz = color;
}
} else {
psout.Albedo.xyz = color;
}
# else
# else
psout.Albedo.xyz = color;
# endif
# endif

psout.Albedo.w = 1;

Expand Down
32 changes: 16 additions & 16 deletions features/Light Limit Fix/Shaders/LightLimitFix/LightLimitFix.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ uint GetClusterIndex(float2 uv, float z)
uint clusterZ = uint(max((log2(clampedZ) - log2(perPassLLF[0].LightsNear)) * 16.0 / log2(perPassLLF[0].LightsFar / perPassLLF[0].LightsNear), 0.0));
uint2 clusterDim = ceil(perPassLLF[0].BufferDim / float2(32, 16));
uint3 cluster = uint3(uint2((uv * perPassLLF[0].BufferDim) / clusterDim), clusterZ);
return cluster.x + (32 * cluster.y) + (32 * 16 * cluster.z);;
return cluster.x + (32 * cluster.y) + (32 * 16 * cluster.z);
;
}

// Get a raw depth from the depth buffer. [0,1] in uv space
Expand Down Expand Up @@ -139,19 +140,18 @@ float ContactShadows(float3 rayPos, float2 texcoord, float offset, float3 lightD

float3 TurboColormap(float x)
{
const float4 kRedVec4 = float4(0.13572138, 4.61539260, -42.66032258, 132.13108234);
const float4 kGreenVec4 = float4(0.09140261, 2.19418839, 4.84296658, -14.18503333);
const float4 kBlueVec4 = float4(0.10667330, 12.64194608, -60.58204836, 110.36276771);
const float2 kRedVec2 = float2(-152.94239396, 59.28637943);
const float2 kGreenVec2 = float2(4.27729857, 2.82956604);
const float2 kBlueVec2 = float2(-89.90310912, 27.34824973);

x = saturate(x);
float4 v4 = float4(1.0, x, x * x, x * x * x);
float2 v2 = v4.zw * v4.z;
return float3(
dot(v4, kRedVec4) + dot(v2, kRedVec2),
dot(v4, kGreenVec4) + dot(v2, kGreenVec2),
dot(v4, kBlueVec4) + dot(v2, kBlueVec2)
);
const float4 kRedVec4 = float4(0.13572138, 4.61539260, -42.66032258, 132.13108234);
const float4 kGreenVec4 = float4(0.09140261, 2.19418839, 4.84296658, -14.18503333);
const float4 kBlueVec4 = float4(0.10667330, 12.64194608, -60.58204836, 110.36276771);
const float2 kRedVec2 = float2(-152.94239396, 59.28637943);
const float2 kGreenVec2 = float2(4.27729857, 2.82956604);
const float2 kBlueVec2 = float2(-89.90310912, 27.34824973);

x = saturate(x);
float4 v4 = float4(1.0, x, x * x, x * x * x);
float2 v2 = v4.zw * v4.z;
return float3(
dot(v4, kRedVec4) + dot(v2, kRedVec2),
dot(v4, kGreenVec4) + dot(v2, kGreenVec2),
dot(v4, kBlueVec4) + dot(v2, kBlueVec2));
}
55 changes: 27 additions & 28 deletions package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
float screenNoise = InterleavedGradientNoise(screenUV * perPassLLF[0].BufferDim);
# if defined(SKINNED) || !defined(MODELSPACENORMALS)
float3 vertexNormal = normalize(transpose(tbn)[2]);
# endif
# endif
# endif

if (numLights > 0) {
Expand Down Expand Up @@ -1713,28 +1713,28 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
lightColor *= shadowComponent.xxx;
# endif
float3 normalizedLightDirection = normalize(lightDirection);
# if defined(LIGHT_LIMIT_FIX)
# if defined(DEFSHADOW)
# if defined(LIGHT_LIMIT_FIX)
# if defined(DEFSHADOW)
if (perPassLLF[0].EnableContactShadows && !FrameParams.z && FrameParams.y && shadowComponent != 0.0) {
# else
# else
if (perPassLLF[0].EnableContactShadows && !FrameParams.z && FrameParams.y) {
# endif
# endif
float3 normalizedLightDirectionWS = normalizedLightDirection;
# if (defined(SKINNED) || !defined(MODELSPACENORMALS)) && !defined(DRAW_IN_WORLDSPACE)
# if (defined(SKINNED) || !defined(MODELSPACENORMALS)) && !defined(DRAW_IN_WORLDSPACE)
if (!input.WorldSpace) {
normalizedLightDirectionWS = normalize(mul(input.World[eyeIndex], float4(normalizedLightDirection, 0)));
}
# endif
}
# endif
float3 normalizedLightDirectionVS = WorldToView(normalizedLightDirectionWS, true, eyeIndex);
# if defined(SKINNED) || !defined(MODELSPACENORMALS)

# if defined(SKINNED) || !defined(MODELSPACENORMALS)
float shadowIntensityFactor = saturate(dot(vertexNormal, normalizedLightDirection.xyz) * PI);
lightColor *= lerp(1.0, ContactShadows(viewPosition, screenUV, screenNoise, normalizedLightDirectionVS, 1.0, 0.0, eyeIndex), shadowIntensityFactor);
# else
# else
lightColor *= ContactShadows(viewPosition, screenUV, screenNoise, normalizedLightDirectionVS, 1.0, 0.0, eyeIndex);
# endif
# endif
}
# endif
# endif

# if defined(CPM_AVAILABLE)
if (perPassParallax[0].EnableShadows) {
Expand All @@ -1754,8 +1754,8 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
if (perPassParallax[0].EnableTerrainParallax)
lightColor *= GetParallaxSoftShadowMultiplierTerrain(input, terrainUVs, mipLevel, lightDirectionTS, sh0, lightIsLit * parallaxShadowQuality);
# elif defined(ENVMAP)
if (complexMaterialParallax)
lightColor *= GetParallaxSoftShadowMultiplier(uv, mipLevel, lightDirectionTS, sh0, TexEnvMaskSampler, SampEnvMaskSampler, 3, lightIsLit * parallaxShadowQuality);
if (complexMaterialParallax)
lightColor *= GetParallaxSoftShadowMultiplier(uv, mipLevel, lightDirectionTS, sh0, TexEnvMaskSampler, SampEnvMaskSampler, 3, lightIsLit * parallaxShadowQuality);
# endif
}
# endif
Expand Down Expand Up @@ -1846,15 +1846,15 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

if (!FrameParams.z && FrameParams.y) {
float3 normalizedLightDirectionVS = WorldToView(normalizedLightDirection, true, eyeIndex);
if (light.firstPersonShadow){
if (light.firstPersonShadow) {
lightColor *= ContactShadows(viewPosition, screenUV, screenNoise, normalizedLightDirectionVS, shadowQualityScale, light.radius, eyeIndex);
} else if (perPassLLF[0].EnableContactShadows){
} else if (perPassLLF[0].EnableContactShadows) {
# if defined(SKINNED) || !defined(MODELSPACENORMALS)
float shadowIntensityFactor = saturate(dot(worldSpaceVertexNormal, normalizedLightDirection.xyz) * PI);
lightColor *= lerp(1.0, ContactShadows(viewPosition, screenUV, screenNoise, normalizedLightDirectionVS, shadowQualityScale, 0.0, eyeIndex), shadowIntensityFactor);
# else
lightColor *= ContactShadows(viewPosition, screenUV, screenNoise, normalizedLightDirectionVS, shadowQualityScale, 0.0, eyeIndex);
# endif
# endif
}
}

Expand Down Expand Up @@ -2029,7 +2029,6 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

# endif // !defined(PBR)


# if defined(LANDSCAPE) && !defined(LOD_LAND_BLEND)
psout.Albedo.w = 0;
# else
Expand Down Expand Up @@ -2083,17 +2082,17 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# endif

# if defined(LIGHT_LIMIT_FIX)
if (perPassLLF[0].EnableLightsVisualisation){
if (perPassLLF[0].LightsVisualisationMode == 0){
psout.Albedo.xyz = TurboColormap(perPassLLF[0].StrictLightsCount > 7);
} else if (perPassLLF[0].LightsVisualisationMode == 1){
psout.Albedo.xyz = TurboColormap((float)perPassLLF[0].StrictLightsCount / 7.0);
if (perPassLLF[0].EnableLightsVisualisation) {
if (perPassLLF[0].LightsVisualisationMode == 0) {
psout.Albedo.xyz = TurboColormap(perPassLLF[0].StrictLightsCount > 7);
} else if (perPassLLF[0].LightsVisualisationMode == 1) {
psout.Albedo.xyz = TurboColormap((float)perPassLLF[0].StrictLightsCount / 7.0);
} else {
psout.Albedo.xyz = TurboColormap((float)lightCount / 128.0);
}
} else {
psout.Albedo.xyz = TurboColormap((float)lightCount / 128.0);
psout.Albedo.xyz = color.xyz - tmpColor.xyz * FrameParams.zzz;
}
} else {
psout.Albedo.xyz = color.xyz - tmpColor.xyz * FrameParams.zzz;
}
# else
psout.Albedo.xyz = color.xyz - tmpColor.xyz * FrameParams.zzz;
# endif // defined(LIGHT_LIMIT_FIX)
Expand Down
17 changes: 7 additions & 10 deletions src/Features/LightLimitFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ void LightLimitFix::SetLightPosition(LightLimitFix::LightData& a_light, RE::NiPo
for (int eyeIndex = 0; eyeIndex < eyeCount; eyeIndex++) {
auto eyePosition = eyeCount == 1 ?
state->GetRuntimeData().posAdjust.getEye(eyeIndex) :
state->GetVRRuntimeData().posAdjust.getEye(eyeIndex);
state->GetVRRuntimeData().posAdjust.getEye(eyeIndex);
auto viewMatrix = eyeCount == 1 ?
state->GetRuntimeData().cameraData.getEye(eyeIndex).viewMat :
state->GetVRRuntimeData().cameraData.getEye(eyeIndex).viewMat;
state->GetVRRuntimeData().cameraData.getEye(eyeIndex).viewMat;
auto worldPos = a_initialPosition - eyePosition;
a_light.positionWS[eyeIndex].x = worldPos.x;
a_light.positionWS[eyeIndex].y = worldPos.y;
Expand Down Expand Up @@ -239,7 +239,7 @@ void LightLimitFix::Bind()

auto reflections = (!REL::Module::IsVR() ?
RE::BSGraphics::RendererShadowState::GetSingleton()->GetRuntimeData().cubeMapRenderTarget :
RE::BSGraphics::RendererShadowState::GetSingleton()->GetVRRuntimeData().cubeMapRenderTarget) == RE::RENDER_TARGETS_CUBEMAP::kREFLECTIONS;
RE::BSGraphics::RendererShadowState::GetSingleton()->GetVRRuntimeData().cubeMapRenderTarget) == RE::RENDER_TARGETS_CUBEMAP::kREFLECTIONS;

if (reflections || accumulator->GetRuntimeData().activeShadowSceneNode != RE::BSShaderManager::State::GetSingleton().shadowSceneNode[0]) {
PerPass perPassData{};
Expand Down Expand Up @@ -431,14 +431,12 @@ bool LightLimitFix::CheckParticleLights(RE::BSRenderPass* a_pass, uint32_t)
}
}


if (gradientConfig) {
auto grey = float3(config->colorMult.red, config->colorMult.green, config->colorMult.blue).Dot(float3(0.3f, 0.59f, 0.11f));
color.red *= grey * gradientConfig->color.red;
color.green *= grey * gradientConfig->color.green;
color.blue *= grey * gradientConfig->color.blue;
}
else {
} else {
color.red *= config->colorMult.red;
color.green *= config->colorMult.green;
color.blue *= config->colorMult.blue;
Expand All @@ -455,7 +453,6 @@ bool LightLimitFix::CheckParticleLights(RE::BSRenderPass* a_pass, uint32_t)
return false;
}


enum class GrassShaderTechniques
{
RenderDepth = 8,
Expand Down Expand Up @@ -550,12 +547,12 @@ bool LightLimitFix::AddCachedParticleLights(eastl::vector<LightData>& lightsData
auto state = RE::BSGraphics::RendererShadowState::GetSingleton();
auto eyePosition = eyeCount == 1 ?
state->GetRuntimeData().posAdjust.getEye(a_eyeIndex) :
state->GetVRRuntimeData().posAdjust.getEye(a_eyeIndex);
state->GetVRRuntimeData().posAdjust.getEye(a_eyeIndex);
cachedParticleLight.position = { light.positionWS[a_eyeIndex].x + eyePosition.x, light.positionWS[a_eyeIndex].y + eyePosition.y, light.positionWS[a_eyeIndex].z + eyePosition.z };
for (int eyeIndex = 0; eyeIndex < eyeCount; eyeIndex++) {
auto viewMatrix = eyeCount == 1 ?
state->GetRuntimeData().cameraData.getEye(eyeIndex).viewMat :
state->GetVRRuntimeData().cameraData.getEye(eyeIndex).viewMat;
state->GetVRRuntimeData().cameraData.getEye(eyeIndex).viewMat;
light.positionVS[eyeIndex] = DirectX::SimpleMath::Vector3::Transform(light.positionWS[eyeIndex], viewMatrix);
}

Expand Down Expand Up @@ -678,7 +675,7 @@ void LightLimitFix::UpdateLights()
auto eyeIndex = 0; // only calculate for left eye
auto eyePosition = eyeCount == 1 ?
state->GetRuntimeData().posAdjust.getEye(eyeIndex) :
state->GetVRRuntimeData().posAdjust.getEye(eyeIndex);
state->GetVRRuntimeData().posAdjust.getEye(eyeIndex);

for (const auto& particleLight : particleLights) {
if (const auto particleSystem = netimmerse_cast<RE::NiParticleSystem*>(particleLight.first);
Expand Down
21 changes: 16 additions & 5 deletions src/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ void State::Load()
{
auto& shaderCache = SIE::ShaderCache::Instance();

std::string configPath = "Data\\SKSE\\Plugins\\CommunityShaders.json";

std::string configPath = userConfigPath;
std::ifstream i(configPath);
if (!i.is_open()) {
logger::error("Error opening config file ({})\n", configPath);
return;
logger::info("Unable to open user config file ({}); trying default ({})", configPath, defaultConfigPath);
configPath = defaultConfigPath;
i.open(configPath);
if (!i.is_open()) {
logger::error("Error opening config file ({})\n", configPath);
return;
}
}
logger::info("Loading config file ({})", configPath);

json settings;
try {
Expand Down Expand Up @@ -110,12 +115,17 @@ void State::Load()

for (auto* feature : Feature::GetFeatureList())
feature->Load(settings);
i.close();
if (settings["Version"].is_string() && settings["Version"].get<std::string>() != Plugin::VERSION.string()) {
logger::info("Found older config for version {}; upgrading to {}", (std::string)settings["Version"], Plugin::VERSION.string());
Save();
}
}

void State::Save()
{
auto& shaderCache = SIE::ShaderCache::Instance();
std::ofstream o(L"Data\\SKSE\\Plugins\\CommunityShaders.json");
std::ofstream o(userConfigPath);
json settings;

Menu::GetSingleton()->Save(settings);
Expand Down Expand Up @@ -145,6 +155,7 @@ void State::Save()
feature->Save(settings);

o << settings.dump(1);
logger::info("Saving settings to {}", userConfigPath);
}

bool State::ValidateCache(CSimpleIniA& a_ini)
Expand Down
2 changes: 2 additions & 0 deletions src/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class State
spdlog::level::level_enum logLevel = spdlog::level::info;
std::string shaderDefinesString = "";
std::vector<std::pair<std::string, std::string>> shaderDefines{}; // data structure to parse string into; needed to avoid dangling pointers
const std::string userConfigPath = "Data\\SKSE\\Plugins\\CommunityShadersUSER.json";
const std::string defaultConfigPath = "Data\\SKSE\\Plugins\\CommunityShaders.json";

void Draw();
void Reset();
Expand Down
5 changes: 3 additions & 2 deletions src/XSEPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extern "C" DLLEXPORT bool SKSEAPI SKSEPlugin_Load(const SKSE::LoadInterface* a_s
while (!WinAPI::IsDebuggerPresent()) {};
#endif
InitializeLog();
logger::info("Loaded plugin");
logger::info("Loaded {} {}", Plugin::NAME, Plugin::VERSION.string());
SKSE::Init(a_skse);
return Load();
}
Expand Down Expand Up @@ -152,7 +152,8 @@ bool Load()

auto state = State::GetSingleton();
state->Load();
InitializeLog(state->GetLogLevel());
auto log = spdlog::default_logger();
log->set_level(state->GetLogLevel());

return true;
}