Skip to content

Commit 44086f9

Browse files
committed
Preliminary HDR patch
Meaningful changes picked from doodlum#1019 Get rid of tonemapping code and settings I plan on letting DXVK or the compositor to handle proper PQ Set DXGI buffer types based on HDR option
1 parent f9dcd69 commit 44086f9

File tree

11 files changed

+184
-17
lines changed

11 files changed

+184
-17
lines changed

src/Deferred.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ void Deferred::SetupResources()
9595
// TEMPORAL_AA_WATER_2
9696

9797
// Albedo
98-
SetupRenderTarget(ALBEDO, texDesc, srvDesc, rtvDesc, uavDesc, DXGI_FORMAT_R8G8B8A8_UNORM, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
98+
SetupRenderTarget(ALBEDO, texDesc, srvDesc, rtvDesc, uavDesc, globals::shaderCache->UpgradeDxgiFormat(DXGI_FORMAT_R8G8B8A8_UNORM), D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
9999
// Specular
100-
SetupRenderTarget(SPECULAR, texDesc, srvDesc, rtvDesc, uavDesc, DXGI_FORMAT_R11G11B10_FLOAT, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
100+
SetupRenderTarget(SPECULAR, texDesc, srvDesc, rtvDesc, uavDesc, globals::shaderCache->UpgradeDxgiFormat(DXGI_FORMAT_R11G11B10_FLOAT), D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
101101
// Reflectance
102-
SetupRenderTarget(REFLECTANCE, texDesc, srvDesc, rtvDesc, uavDesc, DXGI_FORMAT_R8G8B8A8_UNORM, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
102+
SetupRenderTarget(REFLECTANCE, texDesc, srvDesc, rtvDesc, uavDesc, globals::shaderCache->UpgradeDxgiFormat(DXGI_FORMAT_R8G8B8A8_UNORM), D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
103103
// Normal + Roughness
104-
SetupRenderTarget(NORMALROUGHNESS, texDesc, srvDesc, rtvDesc, uavDesc, DXGI_FORMAT_R10G10B10A2_UNORM, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
104+
SetupRenderTarget(NORMALROUGHNESS, texDesc, srvDesc, rtvDesc, uavDesc, globals::shaderCache->UpgradeDxgiFormat(DXGI_FORMAT_R10G10B10A2_UNORM), D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
105105
// Masks
106-
SetupRenderTarget(MASKS, texDesc, srvDesc, rtvDesc, uavDesc, DXGI_FORMAT_R8G8B8A8_UNORM, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
106+
SetupRenderTarget(MASKS, texDesc, srvDesc, rtvDesc, uavDesc, globals::shaderCache->UpgradeDxgiFormat(DXGI_FORMAT_R8G8B8A8_UNORM), D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
107107
}
108108

109109
{
@@ -156,7 +156,7 @@ void Deferred::SetupResources()
156156
auto mainTex = renderer->GetRuntimeData().renderTargets[RE::RENDER_TARGETS::kMAIN];
157157
mainTex.texture->GetDesc(&texDesc);
158158

159-
texDesc.Format = DXGI_FORMAT_R11G11B10_FLOAT;
159+
texDesc.Format = globals::shaderCache->UpgradeDxgiFormat(DXGI_FORMAT_R11G11B10_FLOAT);
160160
texDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
161161

162162
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {

src/Features/DynamicCubemaps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void DynamicCubemaps::DrawSettings()
5252
auto context = globals::d3d::context;
5353

5454
D3D11_TEXTURE2D_DESC texDesc{};
55-
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
55+
texDesc.Format = globals::shaderCache->UpgradeDxgiFormat(DXGI_FORMAT_R8G8B8A8_UNORM);
5656
texDesc.Height = 1;
5757
texDesc.Width = 1;
5858
texDesc.ArraySize = 6;
@@ -604,7 +604,7 @@ void DynamicCubemaps::SetupResources()
604604
envCapturePositionReflectionsTexture->CreateSRV(srvDesc);
605605
envCapturePositionReflectionsTexture->CreateUAV(uavDesc);
606606

607-
texDesc.Format = DXGI_FORMAT_R11G11B10_FLOAT;
607+
texDesc.Format = globals::shaderCache->UpgradeDxgiFormat(DXGI_FORMAT_R11G11B10_FLOAT);
608608
srvDesc.Format = texDesc.Format;
609609
uavDesc.Format = texDesc.Format;
610610

src/Features/ScreenSpaceGI.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ void ScreenSpaceGI::SetupResources()
364364

365365
auto mainTex = renderer->GetRuntimeData().renderTargets[RE::RENDER_TARGETS::kMAIN];
366366
mainTex.texture->GetDesc(&texDesc);
367-
srvDesc.Format = uavDesc.Format = texDesc.Format = DXGI_FORMAT_R11G11B10_FLOAT;
367+
srvDesc.Format = uavDesc.Format = texDesc.Format = globals::shaderCache->UpgradeDxgiFormat(DXGI_FORMAT_R11G11B10_FLOAT);
368368
texDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
369369
texDesc.MipLevels = srvDesc.Texture2D.MipLevels = 5;
370370
texDesc.MiscFlags |= D3D11_RESOURCE_MISC_GENERATE_MIPS;
@@ -390,7 +390,7 @@ void ScreenSpaceGI::SetupResources()
390390

391391
uavDesc.Texture2D.MipSlice = 0;
392392
texDesc.MipLevels = srvDesc.Texture2D.MipLevels = 1;
393-
srvDesc.Format = uavDesc.Format = texDesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
393+
srvDesc.Format = uavDesc.Format = texDesc.Format = globals::shaderCache->UpgradeDxgiFormat(DXGI_FORMAT_R11G11B10_FLOAT);
394394
{
395395
texIlY[0] = eastl::make_unique<Texture2D>(texDesc);
396396
texIlY[0]->CreateSRV(srvDesc);
@@ -438,7 +438,7 @@ void ScreenSpaceGI::SetupResources()
438438
texAccumFrames[1]->CreateUAV(uavDesc);
439439
}
440440

441-
srvDesc.Format = uavDesc.Format = texDesc.Format = DXGI_FORMAT_R11G11B10_FLOAT;
441+
srvDesc.Format = uavDesc.Format = texDesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
442442
{
443443
texPrevGeo = eastl::make_unique<Texture2D>(texDesc);
444444
texPrevGeo->CreateSRV(srvDesc);

src/FidelityFX.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ void FidelityFX::CreateFSRResources()
178178
contextDescription.flags = FFX_FSR3_ENABLE_UPSCALING_ONLY | FFX_FSR3_ENABLE_AUTO_EXPOSURE;
179179
contextDescription.backBufferFormat = FFX_SURFACE_FORMAT_R8G8B8A8_UNORM;
180180

181+
if (globals::shaderCache->IsHdrRendering()) {
182+
logger::info("[FidelityFX] Enabling HDR rendering");
183+
contextDescription.flags |= FFX_FSR3_ENABLE_HIGH_DYNAMIC_RANGE;
184+
contextDescription.backBufferFormat = FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT;
185+
}
186+
181187
contextDescription.backendInterfaceUpscaling = fsrInterface;
182188

183189
if (ffxFsr3ContextCreate(&fsrContext, &contextDescription) != FFX_OK)

src/Hooks.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ HRESULT WINAPI hk_D3D11CreateDeviceAndSwapChain(
299299
pAdapter->GetDesc(&adapterDesc);
300300
globals::state->SetAdapterDescription(adapterDesc.Description);
301301

302+
if (globals::shaderCache->IsHdrRendering()) {
303+
pSwapChainDesc->BufferDesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
304+
}
305+
302306
if (!REL::Module::IsVR()) {
303307
pSwapChainDesc->SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
304308

@@ -452,6 +456,18 @@ HRESULT WINAPI hk_D3D11CreateDeviceAndSwapChain(
452456
streamline->PostDevice();
453457
}
454458

459+
if (*ppSwapChain && globals::shaderCache->IsHdrRendering()) {
460+
IDXGISwapChain4* swapChain4 = nullptr;
461+
if (SUCCEEDED((*ppSwapChain)->QueryInterface(IID_PPV_ARGS(&swapChain4)))) {
462+
// Traditionally color space would be set to HDR10 equivalent, however as we do no tonemapping in-engine,
463+
// we use scRGB as the format and thus Rec. 709 primaries
464+
//
465+
// Tonemapping is performed either by the compositor or other system components into PQ
466+
swapChain4->SetColorSpace1(DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709);
467+
swapChain4->Release();
468+
}
469+
}
470+
455471
return ret;
456472
}
457473

@@ -563,9 +579,96 @@ namespace Hooks
563579
{
564580
static void thunk(RE::BSGraphics::Renderer* This, RE::RENDER_TARGETS::RENDER_TARGET a_target, RE::BSGraphics::RenderTargetProperties* a_properties)
565581
{
582+
if (globals::shaderCache->IsHdrRendering()) {
583+
a_properties->format = RE::BSGraphics::Format::kR16G16B16A16_FLOAT;
584+
}
585+
globals::state->ModifyRenderTarget(a_target, a_properties);
586+
func(This, a_target, a_properties);
587+
}
588+
static inline REL::Relocation<decltype(thunk)> func;
589+
};
590+
591+
struct CreateRenderTarget_ImagespaceTempCopy
592+
{
593+
static void thunk(RE::BSGraphics::Renderer* This, RE::RENDER_TARGETS::RENDER_TARGET a_target, RE::BSGraphics::RenderTargetProperties* a_properties)
594+
{
595+
if (globals::shaderCache->IsHdrRendering()) {
596+
a_properties->format = RE::BSGraphics::Format::kR16G16B16A16_FLOAT;
597+
}
598+
globals::state->ModifyRenderTarget(a_target, a_properties);
599+
func(This, a_target, a_properties);
600+
}
601+
602+
static inline REL::Relocation<decltype(thunk)> func;
603+
};
604+
605+
struct CreateRenderTarget_ImagespaceTempCopy2
606+
{
607+
static void thunk(RE::BSGraphics::Renderer* This, RE::RENDER_TARGETS::RENDER_TARGET a_target, RE::BSGraphics::RenderTargetProperties* a_properties)
608+
{
609+
if (globals::shaderCache->IsHdrRendering()) {
610+
a_properties->format = RE::BSGraphics::Format::kR16G16B16A16_FLOAT;
611+
}
612+
globals::state->ModifyRenderTarget(a_target, a_properties);
613+
func(This, a_target, a_properties);
614+
}
615+
616+
static inline REL::Relocation<decltype(thunk)> func;
617+
};
618+
619+
struct CreateRenderTarget_LDRBlurSwap
620+
{
621+
static void thunk(RE::BSGraphics::Renderer* This, RE::RENDER_TARGETS::RENDER_TARGET a_target, RE::BSGraphics::RenderTargetProperties* a_properties)
622+
{
623+
if (globals::shaderCache->IsHdrRendering()) {
624+
a_properties->format = RE::BSGraphics::Format::kR16G16B16A16_FLOAT;
625+
}
626+
globals::state->ModifyRenderTarget(a_target, a_properties);
627+
func(This, a_target, a_properties);
628+
}
629+
630+
static inline REL::Relocation<decltype(thunk)> func;
631+
};
632+
633+
struct CreateRenderTarget_LDRDownsample
634+
{
635+
static void thunk(RE::BSGraphics::Renderer* This, RE::RENDER_TARGETS::RENDER_TARGET a_target, RE::BSGraphics::RenderTargetProperties* a_properties)
636+
{
637+
if (globals::shaderCache->IsHdrRendering()) {
638+
a_properties->format = RE::BSGraphics::Format::kR16G16B16A16_FLOAT;
639+
}
640+
globals::state->ModifyRenderTarget(a_target, a_properties);
641+
func(This, a_target, a_properties);
642+
}
643+
644+
static inline REL::Relocation<decltype(thunk)> func;
645+
};
646+
647+
struct CreateRenderTarget_TemporalAAAccumulation0
648+
{
649+
static void thunk(RE::BSGraphics::Renderer* This, RE::RENDER_TARGETS::RENDER_TARGET a_target, RE::BSGraphics::RenderTargetProperties* a_properties)
650+
{
651+
if (globals::shaderCache->IsHdrRendering()) {
652+
a_properties->format = RE::BSGraphics::Format::kR16G16B16A16_FLOAT;
653+
}
654+
globals::state->ModifyRenderTarget(a_target, a_properties);
655+
func(This, a_target, a_properties);
656+
}
657+
658+
static inline REL::Relocation<decltype(thunk)> func;
659+
};
660+
661+
struct CreateRenderTarget_TemporalAAAccumulation1
662+
{
663+
static void thunk(RE::BSGraphics::Renderer* This, RE::RENDER_TARGETS::RENDER_TARGET a_target, RE::BSGraphics::RenderTargetProperties* a_properties)
664+
{
665+
if (globals::shaderCache->IsHdrRendering()) {
666+
a_properties->format = RE::BSGraphics::Format::kR16G16B16A16_FLOAT;
667+
}
566668
globals::state->ModifyRenderTarget(a_target, a_properties);
567669
func(This, a_target, a_properties);
568670
}
671+
569672
static inline REL::Relocation<decltype(thunk)> func;
570673
};
571674

@@ -958,6 +1061,12 @@ namespace Hooks
9581061

9591062
logger::info("Hooking BSShaderRenderTargets::Create::CreateRenderTarget(s)");
9601063
stl::write_thunk_call<CreateRenderTarget_Main>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0x3F0, 0x3F3, 0x548));
1064+
stl::write_thunk_call<CreateRenderTarget_ImagespaceTempCopy>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0x62F, 0x62E));
1065+
stl::write_thunk_call<CreateRenderTarget_ImagespaceTempCopy2>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0x642, 0x641));
1066+
stl::write_thunk_call<CreateRenderTarget_LDRBlurSwap>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0x529, 0x528));
1067+
stl::write_thunk_call<CreateRenderTarget_LDRDownsample>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0xB2E, 0xB2E));
1068+
stl::write_thunk_call<CreateRenderTarget_TemporalAAAccumulation0>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0xE68, 0xE6A));
1069+
stl::write_thunk_call<CreateRenderTarget_TemporalAAAccumulation1>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0xE7E, 0xE80));
9611070
stl::write_thunk_call<CreateRenderTarget_Normals>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0x458, 0x45B, 0x5B0));
9621071
stl::write_thunk_call<CreateRenderTarget_NormalsSwap>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0x46B, 0x46E, 0x5C3));
9631072
stl::write_thunk_call<CreateRenderTarget_Snow>(REL::RelocationID(100458, 107175).address() + REL::Relocate(0x406, 0x409, 0x55e));

src/Menu.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,15 @@ void Menu::DrawGeneralSettings()
598598
ImGui::Text("Skips a shader being replaced if it hasn't been compiled yet. Also makes compilation blazingly fast!");
599599
}
600600

601+
bool useHdrRendering = shaderCache->IsHdrRendering();
602+
ImGui::TableNextColumn();
603+
if (ImGui::Checkbox("Enable HDR", &useHdrRendering)) {
604+
shaderCache->SetHdrRendering(useHdrRendering);
605+
}
606+
if (auto _tt = Util::HoverTooltipWrapper()) {
607+
ImGui::Text('"Vittu meni ikuisuus koodata" -tssge');
608+
}
609+
601610
ImGui::EndTable();
602611
}
603612
}

src/ShaderCache.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,11 +2100,21 @@ namespace SIE
21002100
return isAsync;
21012101
}
21022102

2103+
bool ShaderCache::IsHdrRendering() const
2104+
{
2105+
return isHdrRendering;
2106+
}
2107+
21032108
void ShaderCache::SetAsync(bool value)
21042109
{
21052110
isAsync = value;
21062111
}
21072112

2113+
void ShaderCache::SetHdrRendering(bool value)
2114+
{
2115+
isHdrRendering = value;
2116+
}
2117+
21082118
bool ShaderCache::IsDump() const
21092119
{
21102120
return isDump;
@@ -2125,6 +2135,17 @@ namespace SIE
21252135
isDiskCache = value;
21262136
}
21272137

2138+
DXGI_FORMAT ShaderCache::UpgradeDxgiFormat(DXGI_FORMAT original) const
2139+
{
2140+
if (!isHdrRendering)
2141+
return original;
2142+
2143+
switch (original) {
2144+
default:
2145+
return DXGI_FORMAT_R16G16B16A16_FLOAT;
2146+
}
2147+
}
2148+
21282149
void ShaderCache::DeleteDiskCache()
21292150
{
21302151
std::scoped_lock lock{ compilationSet.compilationMutex };

src/ShaderCache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,15 @@ namespace SIE
319319
bool IsEnabled() const;
320320
void SetEnabled(bool value);
321321
bool IsAsync() const;
322+
bool IsHdrRendering() const;
322323
void SetAsync(bool value);
324+
void SetHdrRendering(bool value);
323325
bool IsDump() const;
324326
void SetDump(bool value);
325327

326328
bool IsDiskCache() const;
327329
void SetDiskCache(bool value);
330+
DXGI_FORMAT UpgradeDxgiFormat(DXGI_FORMAT original) const;
328331
void DeleteDiskCache();
329332
void ValidateDiskCache();
330333
void WriteDiskCacheInfo();
@@ -660,6 +663,7 @@ namespace SIE
660663
bool isEnabled = true;
661664
bool isDiskCache = true;
662665
bool isAsync = true;
666+
bool isHdrRendering = true;
663667
bool isDump = false;
664668
bool hideError = false;
665669
bool useFileWatcher = false;

src/State.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ void State::Save(ConfigMode a_configMode)
368368
general["Enable Shaders"] = shaderCache->IsEnabled();
369369
general["Enable Disk Cache"] = shaderCache->IsDiskCache();
370370
general["Enable Async"] = shaderCache->IsAsync();
371+
general["Enable Hdr Rendering"] = shaderCache->IsHdrRendering();
371372

372373
settings["General"] = general;
373374

@@ -777,8 +778,15 @@ void State::SetupReShade()
777778
reshade::api::resource reShadeSwapChainResource = reShadeDevice->get_resource_from_view(reshade::api::resource_view{ reinterpret_cast<uintptr_t>(swapChainRTV) });
778779
reshade::api::resource_desc reShadeSwapChainDesc = reShadeDevice->get_resource_desc(reShadeSwapChainResource);
779780

780-
reShadeDevice->create_resource_view(reShadeSwapChainResource, reshade::api::resource_usage::render_target, reshade::api::resource_view_desc(reshade::api::format_to_default_typed(reShadeSwapChainDesc.texture.format, 0), 0, 1, 0, 1), &reshadeSwapChainRTV);
781-
reShadeDevice->create_resource_view(reShadeSwapChainResource, reshade::api::resource_usage::render_target, reshade::api::resource_view_desc(reshade::api::format_to_default_typed(reShadeSwapChainDesc.texture.format, 1), 0, 1, 0, 1), &reshadeSwapChainRTVsRGB);
781+
if (globals::shaderCache->IsHdrRendering()) {
782+
reShadeSwapChainDesc.texture.format = reshade::api::format::r16g16b16a16_float;
783+
reShadeRuntime->set_color_space(reshade::api::color_space::extended_srgb_linear);
784+
}
785+
786+
reShadeDevice->create_resource_view(reShadeSwapChainResource, reshade::api::resource_usage::render_target,
787+
reshade::api::resource_view_desc(reshade::api::format_to_default_typed(reShadeSwapChainDesc.texture.format, 0), 0, 1, 0, 1), &reshadeSwapChainRTV);
788+
reShadeDevice->create_resource_view(reShadeSwapChainResource, reshade::api::resource_usage::render_target,
789+
reshade::api::resource_view_desc(reshade::api::format_to_default_typed(reShadeSwapChainDesc.texture.format, 1), 0, 1, 0, 1), &reshadeSwapChainRTVsRGB);
782790

783791
auto& depth = globals::game::renderer->GetDepthStencilData().depthStencils[RE::RENDER_TARGETS_DEPTHSTENCIL::kPOST_ZPREPASS_COPY];
784792

src/Streamline.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,11 @@ void Streamline::Upscale(Texture2D* a_upscaleTexture, Texture2D* a_alphaMask, sl
257257
dlssOptions.mode = sl::DLSSMode::eMaxQuality;
258258
dlssOptions.outputWidth = (uint)state->screenSize.x;
259259
dlssOptions.outputHeight = (uint)state->screenSize.y;
260-
dlssOptions.colorBuffersHDR = sl::Boolean::eFalse;
260+
if (globals::shaderCache->IsHdrRendering()) {
261+
dlssOptions.colorBuffersHDR = sl::Boolean::eTrue;
262+
} else {
263+
dlssOptions.colorBuffersHDR = sl::Boolean::eFalse;
264+
}
261265
dlssOptions.preExposure = 1.0f;
262266
dlssOptions.sharpness = 0.0f;
263267

0 commit comments

Comments
 (0)