Skip to content

Commit

Permalink
fix: skylighting reflecting sky
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlum committed Nov 25, 2024
1 parent 85f2c18 commit 48e721a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ float smoothbumpstep(float edge0, float edge1, float x)
float depth = DepthTexture.SampleLevel(LinearSampler, uv, 0);
float linearDepth = SharedData::GetScreenDepth(depth);

#if defined(REFLECTIONS)
if (linearDepth > 16.5) { // Ignore objects which are too close
#else
if (linearDepth > 16.5 && depth != 1.0) { // Ignore objects which are too close or the sky
#endif
half4 positionCS = half4(2 * half2(uv.x, -uv.y + 1) - 1, depth, 1);
positionCS = mul(CameraViewProjInverse[0], positionCS);
positionCS.xyz = positionCS.xyz / positionCS.w;
Expand Down
5 changes: 1 addition & 4 deletions package/Shaders/ISReflectionsRayTracing.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ float4 GetReflectionColor(
binaryMinRaySample = binaryRaySample;
}

// Cubemap skies blend better
float skyFadeFactor = iterationDepth != 1.0;

// Fade based on ray length
float ssrMarchingRadiusFadeFactor = 1.0 - saturate(length(binaryRaySample - projPosition) / rayLength);

Expand All @@ -110,7 +107,7 @@ float4 GetReflectionColor(
// Fade out around screen edges
float2 centerDistanceFadeFactor = sqrt(saturate(1.0 - centerDistance));

float fadeFactor = depthThicknessFactor * skyFadeFactor * sqrt(ssrMarchingRadiusFadeFactor) * min(centerDistanceFadeFactor.x, centerDistanceFadeFactor.y);
float fadeFactor = depthThicknessFactor * sqrt(ssrMarchingRadiusFadeFactor) * min(centerDistanceFadeFactor.x, centerDistanceFadeFactor.y);

if (fadeFactor > 0.0) {
float3 color = ColorTex.SampleLevel(ColorSampler, ConvertRaySample(binaryRaySample.xy, eyeIndex), 0);
Expand Down
61 changes: 50 additions & 11 deletions src/Features/DynamicCubemaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ ID3D11ComputeShader* DynamicCubemaps::GetComputeShaderUpdate()
return updateCubemapCS;
}

ID3D11ComputeShader* DynamicCubemaps::GetComputeShaderUpdateReflections()
{
if (!updateCubemapReflectionsCS) {
logger::debug("Compiling UpdateCubemapCS REFLECTIONS");
updateCubemapReflectionsCS = static_cast<ID3D11ComputeShader*>(Util::CompileShader(L"Data\\Shaders\\DynamicCubemaps\\UpdateCubemapCS.hlsl", { { "REFLECTIONS", "" } }, "cs_5_0"));
}
return updateCubemapReflectionsCS;
}

ID3D11ComputeShader* DynamicCubemaps::GetComputeShaderInferrence()
{
if (!inferCubemapCS) {
Expand Down Expand Up @@ -281,7 +290,7 @@ ID3D11ComputeShader* DynamicCubemaps::GetComputeShaderSpecularIrradiance()
return specularIrradianceCS;
}

void DynamicCubemaps::UpdateCubemapCapture()
void DynamicCubemaps::UpdateCubemapCapture(bool a_reflections)
{
auto renderer = RE::BSGraphics::Renderer::GetSingleton();

Expand All @@ -293,7 +302,18 @@ void DynamicCubemaps::UpdateCubemapCapture()
ID3D11ShaderResourceView* srvs[2] = { depth.depthSRV, main.SRV };
context->CSSetShaderResources(0, 2, srvs);

ID3D11UnorderedAccessView* uavs[3] = { envCaptureTexture->uav.get(), envCaptureRawTexture->uav.get(), envCapturePositionTexture->uav.get() };
ID3D11UnorderedAccessView* uavs[3];
if (a_reflections)
{
uavs[0] = envCaptureReflectionsTexture->uav.get();
uavs[1] = envCaptureRawReflectionsTexture->uav.get();
uavs[2] = envCapturePositionReflectionsTexture->uav.get();
} else {
uavs[0] = envCaptureTexture->uav.get();
uavs[1] = envCaptureRawTexture->uav.get();
uavs[2] = envCapturePositionTexture->uav.get();
}

context->CSSetUnorderedAccessViews(0, 3, uavs, nullptr);

ID3D11Buffer* buffers[2];
Expand All @@ -318,7 +338,8 @@ void DynamicCubemaps::UpdateCubemapCapture()

context->CSSetSamplers(0, 1, &computeSampler);

context->CSSetShader(GetComputeShaderUpdate(), nullptr, 0);
context->CSSetShader(a_reflections ? GetComputeShaderUpdateReflections() : GetComputeShaderUpdate(), nullptr, 0);

context->Dispatch((uint32_t)std::ceil(envCaptureTexture->desc.Width / 8.0f), (uint32_t)std::ceil(envCaptureTexture->desc.Height / 8.0f), 6);

uavs[0] = nullptr;
Expand Down Expand Up @@ -350,11 +371,11 @@ void DynamicCubemaps::Inferrence(bool a_reflections)

context->CSSetUnorderedAccessViews(0, 1, &uav, nullptr);

context->GenerateMips(envCaptureTexture->srv.get());
context->GenerateMips((a_reflections ? envCaptureReflectionsTexture : envCaptureTexture)->srv.get());

auto& cubemap = renderer->GetRendererData().cubemapRenderTargets[RE::RENDER_TARGETS_CUBEMAP::kREFLECTIONS];

ID3D11ShaderResourceView* srvs[3] = { envCaptureTexture->srv.get(), cubemap.SRV, defaultCubemap };
ID3D11ShaderResourceView* srvs[3] = { (a_reflections ? envCaptureReflectionsTexture : envCaptureTexture)->srv.get(), cubemap.SRV, defaultCubemap };
context->CSSetShaderResources(0, 3, srvs);

context->CSSetSamplers(0, 1, &computeSampler);
Expand Down Expand Up @@ -442,19 +463,29 @@ void DynamicCubemaps::UpdateCubemap()
}

switch (nextTask) {
case NextTask::kCapture:
UpdateCubemapCapture(false);
nextTask = NextTask::kInferrence;
break;

case NextTask::kInferrence:
nextTask = NextTask::kIrradiance;
Inferrence(false);
break;

case NextTask::kIrradiance:
if (activeReflections)
nextTask = NextTask::kInferrence2;
nextTask = NextTask::kCapture2;
else
nextTask = NextTask::kCapture;
Irradiance(false);
break;

case NextTask::kCapture2:
UpdateCubemapCapture(true);
nextTask = NextTask::kInferrence2;
break;

case NextTask::kInferrence2:
Inferrence(true);
nextTask = NextTask::kIrradiance2;
Expand All @@ -464,11 +495,6 @@ void DynamicCubemaps::UpdateCubemap()
nextTask = NextTask::kCapture;
Irradiance(true);
break;

case NextTask::kCapture:
UpdateCubemapCapture();
nextTask = NextTask::kInferrence;
break;
}
}

Expand All @@ -483,6 +509,7 @@ void DynamicCubemaps::PostDeferred()
void DynamicCubemaps::SetupResources()
{
GetComputeShaderUpdate();
GetComputeShaderUpdateReflections();
GetComputeShaderInferrence();
GetComputeShaderInferrenceReflections();
GetComputeShaderSpecularIrradiance();
Expand Down Expand Up @@ -538,6 +565,18 @@ void DynamicCubemaps::SetupResources()
envCapturePositionTexture->CreateSRV(srvDesc);
envCapturePositionTexture->CreateUAV(uavDesc);

envCaptureReflectionsTexture = new Texture2D(texDesc);
envCaptureReflectionsTexture->CreateSRV(srvDesc);
envCaptureReflectionsTexture->CreateUAV(uavDesc);

envCaptureRawReflectionsTexture = new Texture2D(texDesc);
envCaptureRawReflectionsTexture->CreateSRV(srvDesc);
envCaptureRawReflectionsTexture->CreateUAV(uavDesc);

envCapturePositionReflectionsTexture = new Texture2D(texDesc);
envCapturePositionReflectionsTexture->CreateSRV(srvDesc);
envCapturePositionReflectionsTexture->CreateUAV(uavDesc);

texDesc.Format = DXGI_FORMAT_R11G11B10_FLOAT;
srvDesc.Format = texDesc.Format;
uavDesc.Format = texDesc.Format;
Expand Down
14 changes: 12 additions & 2 deletions src/Features/DynamicCubemaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ struct DynamicCubemaps : Feature
};

ID3D11ComputeShader* updateCubemapCS = nullptr;
ID3D11ComputeShader* updateCubemapReflectionsCS = nullptr;

ConstantBuffer* updateCubemapCB = nullptr;

ID3D11ComputeShader* inferCubemapCS = nullptr;
Expand All @@ -56,6 +58,12 @@ struct DynamicCubemaps : Feature
Texture2D* envCaptureTexture = nullptr;
Texture2D* envCaptureRawTexture = nullptr;
Texture2D* envCapturePositionTexture = nullptr;


Texture2D* envCaptureReflectionsTexture = nullptr;
Texture2D* envCaptureRawReflectionsTexture = nullptr;
Texture2D* envCapturePositionReflectionsTexture = nullptr;

Texture2D* envInferredTexture = nullptr;

ID3D11ShaderResourceView* defaultCubemap = nullptr;
Expand All @@ -68,8 +76,9 @@ struct DynamicCubemaps : Feature
{
kCapture,
kInferrence,
kInferrence2,
kIrradiance,
kCapture2,
kInferrence2,
kIrradiance2
};

Expand Down Expand Up @@ -131,11 +140,12 @@ struct DynamicCubemaps : Feature

virtual void ClearShaderCache() override;
ID3D11ComputeShader* GetComputeShaderUpdate();
ID3D11ComputeShader* GetComputeShaderUpdateReflections();
ID3D11ComputeShader* GetComputeShaderInferrence();
ID3D11ComputeShader* GetComputeShaderInferrenceReflections();
ID3D11ComputeShader* GetComputeShaderSpecularIrradiance();

void UpdateCubemapCapture();
void UpdateCubemapCapture(bool a_reflections);

void Inferrence(bool a_reflections);

Expand Down

0 comments on commit 48e721a

Please sign in to comment.