Skip to content

Commit

Permalink
fix: minor dynamic cubemap issues
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlum committed Nov 26, 2024
1 parent e605839 commit fa2b5a7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ float3 GetSamplingVector(uint3 ThreadID, in RWTexture2DArray<float4> OutputTextu
}

#if defined(REFLECTIONS)
color.rgb = lerp(color.rgb, Color::GammaToLinear(ReflectionsTexture.SampleLevel(LinearSampler, uv, 0).rgb), saturate(mipLevel / 8.0));
color.rgb = lerp(color.rgb, Color::GammaToLinear(ReflectionsTexture.SampleLevel(LinearSampler, uv, 0).rgb), saturate(mipLevel / 7.0));
#else
color.rgb = lerp(color.rgb, color.rgb * DefaultCubemap.SampleLevel(LinearSampler, uv, 0), saturate(mipLevel / 8.0));
color.rgb = lerp(color.rgb, color.rgb * DefaultCubemap.SampleLevel(LinearSampler, uv, 0), saturate(mipLevel / 7.0));
#endif

color.rgb = Color::LinearToGamma(color.rgb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,10 @@ float3 GetSamplingVector(uint3 ThreadID, in RWTexture2DArray<float4> OutputTextu
return normalize(result);
}

cbuffer UpdateData : register(b1)
cbuffer UpdateData : register(b0)
{
uint Reset;
float3 CameraPreviousPosAdjust2;
}

bool IsSaturated(float value) { return value == saturate(value); }
bool IsSaturated(float2 value) { return IsSaturated(value.x) && IsSaturated(value.y); }

// Inverse project UV + raw depth into world space.
float3 InverseProjectUVZ(float2 uv, float z)
{
uv.y = 1 - uv.y;
float4 cp = float4(float3(uv, z) * 2 - 1, 1);
float4 vp = mul(CameraViewProjInverse[0], cp);
return float3(vp.xy, vp.z) / vp.w;
uint padb10;
}

float smoothbumpstep(float edge0, float edge1, float x)
Expand All @@ -80,14 +68,7 @@ float smoothbumpstep(float edge0, float edge1, float x)
float3 viewDirection = FrameBuffer::WorldToView(captureDirection, false);
float2 uv = FrameBuffer::ViewToUV(viewDirection, false);

if (Reset) {
DynamicCubemap[ThreadID] = 0.0;
DynamicCubemapRaw[ThreadID] = 0.0;
DynamicCubemapPosition[ThreadID] = 0.0;
return;
}

if (IsSaturated(uv) && viewDirection.z < 0.0) { // Check that the view direction exists in screenspace and that it is in front of the camera
if (!FrameBuffer::isOutsideFrame(uv) && viewDirection.z < 0.0) { // Check that the view direction exists in screenspace and that it is in front of the camera
float3 color = 0.0;
float3 position = 0.0;
float weight = 0.0;
Expand Down
1 change: 1 addition & 0 deletions package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
dynamicCubemap = true;
envColor = 1.0;
envRoughness = 0.0;
color.xyz = 0;
# endif
# endif
# endif
Expand Down
39 changes: 23 additions & 16 deletions src/Features/DynamicCubemaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,11 @@ RE::BSEventNotifyControl MenuOpenCloseEventHandler::ProcessEvent(const RE::MenuO
{
// When entering a new cell, reset the capture
if (a_event->menuName == RE::LoadingMenu::MENU_NAME) {
if (!a_event->opening)
DynamicCubemaps::GetSingleton()->resetCapture = true;
if (!a_event->opening) {
auto dynamicCubemaps = DynamicCubemaps::GetSingleton();
dynamicCubemaps->resetCapture[0] = true;
dynamicCubemaps->resetCapture[1] = true;
}
}
return RE::BSEventNotifyControl::kContinue;
}
Expand Down Expand Up @@ -302,6 +305,8 @@ void DynamicCubemaps::UpdateCubemapCapture(bool a_reflections)
ID3D11ShaderResourceView* srvs[2] = { depth.depthSRV, main.SRV };
context->CSSetShaderResources(0, 2, srvs);

uint index = a_reflections ? 1 : 0;

ID3D11UnorderedAccessView* uavs[3];
if (a_reflections) {
uavs[0] = envCaptureReflectionsTexture->uav.get();
Expand All @@ -313,27 +318,30 @@ void DynamicCubemaps::UpdateCubemapCapture(bool a_reflections)
uavs[2] = envCapturePositionTexture->uav.get();
}

context->CSSetUnorderedAccessViews(0, 3, uavs, nullptr);
if (resetCapture[index])
{
float clearColor[4]{ 0, 0, 0, 0 };
context->ClearUnorderedAccessViewFloat(uavs[0], clearColor);
context->ClearUnorderedAccessViewFloat(uavs[1], clearColor);
context->ClearUnorderedAccessViewFloat(uavs[2], clearColor);
resetCapture[index] = false;
}

ID3D11Buffer* buffers[2];
context->PSGetConstantBuffers(12, 1, buffers);
context->CSSetUnorderedAccessViews(0, 3, uavs, nullptr);

UpdateCubemapCB updateData{};
updateData.Reset = resetCapture;

static float3 cameraPreviousPosAdjust = { 0, 0, 0 };
updateData.CameraPreviousPosAdjust = cameraPreviousPosAdjust;
static float3 cameraPreviousPosAdjust[2] = { { 0, 0, 0 }, { 0, 0, 0 } };
updateData.CameraPreviousPosAdjust = cameraPreviousPosAdjust[index];

auto eyePosition = Util::GetEyePosition(0);

cameraPreviousPosAdjust = { eyePosition.x, eyePosition.y, eyePosition.z };
cameraPreviousPosAdjust[index] = { eyePosition.x, eyePosition.y, eyePosition.z };

updateCubemapCB->Update(updateData);
buffers[1] = updateCubemapCB->CB();

context->CSSetConstantBuffers(0, 2, buffers);

resetCapture = false;
ID3D11Buffer* buffer = updateCubemapCB->CB();
context->CSSetConstantBuffers(0, 1, &buffer);

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

Expand All @@ -350,9 +358,8 @@ void DynamicCubemaps::UpdateCubemapCapture(bool a_reflections)
srvs[1] = nullptr;
context->CSSetShaderResources(0, 2, srvs);

buffers[0] = nullptr;
buffers[1] = nullptr;
context->CSSetConstantBuffers(0, 2, buffers);
buffer = nullptr;
context->CSSetConstantBuffers(0, 1, &buffer);

context->CSSetShader(nullptr, nullptr, 0);

Expand Down
4 changes: 2 additions & 2 deletions src/Features/DynamicCubemaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ struct DynamicCubemaps : Feature

struct alignas(16) UpdateCubemapCB
{
uint Reset;
float3 CameraPreviousPosAdjust;
uint pad0;
};

ID3D11ComputeShader* updateCubemapCS = nullptr;
Expand All @@ -68,7 +68,7 @@ struct DynamicCubemaps : Feature
ID3D11ShaderResourceView* defaultCubemap = nullptr;

bool activeReflections = false;
bool resetCapture = true;
bool resetCapture[2] = { true, true };
bool recompileFlag = false;

enum class NextTask
Expand Down
1 change: 0 additions & 1 deletion src/ShaderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,6 @@ namespace SIE
processedTasks.insert(task);
tasksInProgress.erase(task);
conditionVariable.notify_one();
DynamicCubemaps::GetSingleton()->resetCapture = true;
}

void CompilationSet::Clear()
Expand Down

0 comments on commit fa2b5a7

Please sign in to comment.