-
Notifications
You must be signed in to change notification settings - Fork 138
fix(upscaling): replace NIS with RCAS for DLSS #1620
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
93c535c
Use RCAS for sharpening
midona-rhel 048406c
style: 🎨 apply pre-commit.ci formatting
pre-commit-ci[bot] 2d4d39e
Updated RCAS with guards for oob
midona-rhel 5e7b71e
Fixed nitpicks for the rabbit, including destrucors
midona-rhel ff2a178
Some comments for the rabbit
midona-rhel d58794c
Test
midona-rhel 962ad1f
Update Upscaling.cpp to match Doodlums suggestion
midona-rhel cfe6977
style: 🎨 apply pre-commit.ci formatting
pre-commit-ci[bot] 02aa5b3
lol
midona-rhel 80270fe
Update RCAS.hlsl
midona-rhel 3c817a4
Updated RCAS to be a direct port
midona-rhel cfc8f85
Removed double noise removal, dlss already does this
midona-rhel 0dd4a9f
re added noise
midona-rhel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // FidelityFX Super Resolution - Robust Contrast Adaptive Sharpening (RCAS) | ||
| // Based on https://github.com/GPUOpen-Effects/FidelityFX-FSR/blob/master/ffx-fsr/ffx_fsr1.h | ||
| // | ||
| // Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files(the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
|
|
||
| #define FSR_RCAS_LIMIT (0.25 - (1.0 / 16.0)) | ||
|
|
||
| cbuffer RCASConfig : register(b0) | ||
| { | ||
| float sharpness; | ||
| float3 pad; | ||
| }; | ||
|
|
||
| Texture2D<float4> Source : register(t0); | ||
| RWTexture2D<float4> Dest : register(u0); | ||
|
|
||
| [numthreads(8, 8, 1)] void main(uint3 DTid : SV_DispatchThreadID) | ||
| { | ||
| uint2 texDim; | ||
| Dest.GetDimensions(texDim.x, texDim.y); | ||
|
|
||
| if (DTid.x >= texDim.x || DTid.y >= texDim.y) | ||
| return; | ||
|
|
||
| // Algorithm uses minimal 3x3 pixel neighborhood. | ||
| // b | ||
| // d e f | ||
| // h | ||
| int2 sp = int2(DTid.xy); | ||
| float3 b = Source.Load(int3(sp + int2(0, -1), 0)).rgb; | ||
| float3 d = Source.Load(int3(sp + int2(-1, 0), 0)).rgb; | ||
| float3 e = Source.Load(int3(sp, 0)).rgb; | ||
| float3 f = Source.Load(int3(sp + int2(1, 0), 0)).rgb; | ||
| float3 h = Source.Load(int3(sp + int2(0, 1), 0)).rgb; | ||
|
|
||
| // Rename (32-bit) or regroup (16-bit). | ||
| float bR = b.r; | ||
| float bG = b.g; | ||
| float bB = b.b; | ||
| float dR = d.r; | ||
| float dG = d.g; | ||
| float dB = d.b; | ||
| float eR = e.r; | ||
| float eG = e.g; | ||
| float eB = e.b; | ||
| float fR = f.r; | ||
| float fG = f.g; | ||
| float fB = f.b; | ||
| float hR = h.r; | ||
| float hG = h.g; | ||
| float hB = h.b; | ||
|
|
||
| // Luma times 2. | ||
| float bL = bB * 0.5 + (bR * 0.5 + bG); | ||
| float dL = dB * 0.5 + (dR * 0.5 + dG); | ||
| float eL = eB * 0.5 + (eR * 0.5 + eG); | ||
| float fL = fB * 0.5 + (fR * 0.5 + fG); | ||
| float hL = hB * 0.5 + (hR * 0.5 + hG); | ||
|
|
||
| // Noise detection. | ||
| float nz = 0.25 * bL + 0.25 * dL + 0.25 * fL + 0.25 * hL - eL; | ||
| nz = saturate(abs(nz) * rcp(max(max(max(bL, dL), max(eL, fL)), hL) - min(min(min(bL, dL), min(eL, fL)), hL))); | ||
| nz = -0.5 * nz + 1.0; | ||
|
|
||
| // Min and max of ring. | ||
| float mn4R = min(min(min(bR, dR), fR), hR); | ||
| float mn4G = min(min(min(bG, dG), fG), hG); | ||
| float mn4B = min(min(min(bB, dB), fB), hB); | ||
| float mx4R = max(max(max(bR, dR), fR), hR); | ||
| float mx4G = max(max(max(bG, dG), fG), hG); | ||
| float mx4B = max(max(max(bB, dB), fB), hB); | ||
|
|
||
| // Immediate constants for peak range. | ||
| float2 peakC = float2(1.0, -1.0 * 4.0); | ||
|
|
||
| // Limiters, these need to be high precision RCPs. | ||
| float hitMinR = min(mn4R, eR) * rcp(4.0 * mx4R); | ||
| float hitMinG = min(mn4G, eG) * rcp(4.0 * mx4G); | ||
| float hitMinB = min(mn4B, eB) * rcp(4.0 * mx4B); | ||
| float hitMaxR = (peakC.x - max(mx4R, eR)) * rcp(4.0 * mn4R + peakC.y); | ||
| float hitMaxG = (peakC.x - max(mx4G, eG)) * rcp(4.0 * mn4G + peakC.y); | ||
| float hitMaxB = (peakC.x - max(mx4B, eB)) * rcp(4.0 * mn4B + peakC.y); | ||
| float lobeR = max(-hitMinR, hitMaxR); | ||
| float lobeG = max(-hitMinG, hitMaxG); | ||
| float lobeB = max(-hitMinB, hitMaxB); | ||
| float lobe = max(-FSR_RCAS_LIMIT, min(max(lobeR, max(lobeG, lobeB)), 0.0)) * sharpness; | ||
|
|
||
| // Apply noise removal. | ||
| lobe *= nz; | ||
|
|
||
| // Resolve, which needs the medium precision rcp approximation to avoid visible tonality changes. | ||
| float rcpL = rcp(4.0 * lobe + 1.0); | ||
| float pixR = (lobe * bR + lobe * dR + lobe * hR + lobe * fR + eR) * rcpL; | ||
| float pixG = (lobe * bG + lobe * dG + lobe * hG + lobe * fG + eG) * rcpL; | ||
| float pixB = (lobe * bB + lobe * dB + lobe * hB + lobe * fB + eB) * rcpL; | ||
|
|
||
| Dest[DTid.xy] = float4(pixR, pixG, pixB, 1.0); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #include "RCAS.h" | ||
|
|
||
| #include "../../../Deferred.h" | ||
| #include "../../../State.h" | ||
| #include "../../../Util.h" | ||
|
|
||
| struct RCASConfig | ||
| { | ||
| float sharpness; | ||
| float3 pad; | ||
| }; | ||
|
|
||
| RCAS::~RCAS() | ||
| { | ||
| delete rcasConfigCB; | ||
| rcasConfigCB = nullptr; | ||
| } | ||
|
|
||
| void RCAS::Initialize() | ||
| { | ||
| if (rcasConfigCB) | ||
| return; | ||
|
|
||
| logger::info("[RCAS] Creating resources"); | ||
| CreateComputeShader(); | ||
| rcasConfigCB = new ConstantBuffer(ConstantBufferDesc<RCASConfig>()); | ||
| } | ||
|
|
||
| void RCAS::CreateComputeShader() | ||
| { | ||
| std::vector<std::pair<const char*, const char*>> defines; | ||
| rcasComputeShader.attach((ID3D11ComputeShader*)Util::CompileShader(L"Data\\Shaders\\Upscaling\\RCAS\\RCAS.hlsl", defines, "cs_5_0")); | ||
| } | ||
|
|
||
| void RCAS::ApplySharpen(ID3D11ShaderResourceView* inputSRV, ID3D11UnorderedAccessView* outputUAV, float sharpness) | ||
| { | ||
| auto state = globals::state; | ||
| auto context = globals::d3d::context; | ||
|
|
||
| if (!rcasComputeShader) { | ||
| logger::warn("[RCAS] Compute shader not compiled"); | ||
| return; | ||
| } | ||
|
|
||
| state->BeginPerfEvent("RCAS Sharpening"); | ||
|
|
||
| uint32_t screenWidth = (uint32_t)state->screenSize.x; | ||
| uint32_t screenHeight = (uint32_t)state->screenSize.y; | ||
|
|
||
| RCASConfig config{}; | ||
| config.sharpness = sharpness; | ||
|
|
||
| rcasConfigCB->Update(config); | ||
| auto bufferArray = rcasConfigCB->CB(); | ||
|
|
||
| context->CSSetShader(rcasComputeShader.get(), nullptr, 0); | ||
| context->CSSetConstantBuffers(0, 1, &bufferArray); | ||
|
|
||
| ID3D11ShaderResourceView* srvs[] = { inputSRV }; | ||
| context->CSSetShaderResources(0, 1, srvs); | ||
|
|
||
| ID3D11UnorderedAccessView* uavs[] = { outputUAV }; | ||
| context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr); | ||
|
|
||
| uint32_t dispatchX = (screenWidth + 7) / 8; | ||
| uint32_t dispatchY = (screenHeight + 7) / 8; | ||
| context->Dispatch(dispatchX, dispatchY, 1); | ||
|
|
||
| ID3D11ShaderResourceView* nullSRVs[] = { nullptr }; | ||
| context->CSSetShaderResources(0, 1, nullSRVs); | ||
|
|
||
| ID3D11UnorderedAccessView* nullUAVs[] = { nullptr }; | ||
| context->CSSetUnorderedAccessViews(0, 1, nullUAVs, nullptr); | ||
|
|
||
| context->CSSetShader(nullptr, nullptr, 0); | ||
|
|
||
| state->EndPerfEvent(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix compilation error and remove dead code.
Lines 1430-1431 reference undefined
settings.sharpness(should besettings.sharpnessDLSS) and calculatecurrentSharpnessthat is never used. Based on the PR comments, this appears to be leftover code from an attempted formula that broke sharpening.Apply this diff to remove the dead code and fix the compilation error:
void Upscaling::ApplySharpening() { if (settings.sharpnessDLSS <= 0.0f) return; if (!sharpenerTexture) return; - float currentSharpness = (-2.0f * settings.sharpness) + 2.0f; - currentSharpness = exp2(-currentSharpness); auto context = globals::d3d::context; auto renderer = globals::game::renderer; auto& main = renderer->GetRuntimeData().renderTargets[RE::RENDER_TARGETS::kMAIN]; ID3D11Resource* mainResource = nullptr; main.SRV->GetResource(&mainResource); if (!mainResource) return; context->OMSetRenderTargets(0, nullptr, nullptr); rcas.ApplySharpen(main.SRV, sharpenerTexture->uav.get(), settings.sharpnessDLSS); context->CopyResource(mainResource, sharpenerTexture->resource.get()); mainResource->Release(); globals::game::stateUpdateFlags->set(RE::BSGraphics::ShaderFlags::DIRTY_RENDERTARGET); }📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@midona-rhel the build error is complaining
settings.sharpnessdoesn't exist. This comment specifically pointed out the issue. Please review all comments and verify.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.