Skip to content

Commit

Permalink
feat: variable rate shading (#270)
Browse files Browse the repository at this point in the history
* feat: variable rate shading with nvapi extensions

* feat: FFR test

* feat: adaptive rate shading

* feat: motion VRS

* feat: finalising VRS
  • Loading branch information
doodlum authored Apr 10, 2024
1 parent d64a56e commit c9acb7b
Show file tree
Hide file tree
Showing 12 changed files with 634 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "extern/CommonLibSSE-NG"]
path = extern/CommonLibSSE-NG
url = https://github.com/alandtse/CommonLibVR.git
[submodule "extern/NVAPI"]
path = extern/NVAPI
url = https://github.com/NVIDIA/nvapi.git
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ find_package(pystring CONFIG REQUIRED)
find_package(cppwinrt CONFIG REQUIRED)
find_package(unordered_dense CONFIG REQUIRED)
find_package(efsw CONFIG REQUIRED)

set(NVAPI_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/extern/nvapi/" CACHE STRING "Path to NVAPI include headers/shaders" )
set(NVAPI_LIBRARY "${CMAKE_SOURCE_DIR}/extern/nvapi/amd64/nvapi64.lib" CACHE STRING "Path to NVAPI .lib file")

target_include_directories(
${PROJECT_NAME}
PRIVATE
${BSHOSHANY_THREAD_POOL_INCLUDE_DIRS}
${CLIB_UTIL_INCLUDE_DIRS}
${NVAPI_INCLUDE_DIR}
)

target_link_libraries(
Expand All @@ -63,6 +68,7 @@ target_link_libraries(
pystring::pystring
unordered_dense::unordered_dense
efsw::efsw
${NVAPI_LIBRARY}
)

# https://gitlab.kitware.com/cmake/cmake/-/issues/24922#note_1371990
Expand Down
1 change: 1 addition & 0 deletions extern/NVAPI
Submodule NVAPI added at 3a83ef
102 changes: 102 additions & 0 deletions package/Shaders/VariableRateShading/ComputeNASData.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#define GROUP_SIZE (8 * 8)

Texture2D<float3> InputTexture : register(t0);
Texture2D<float2> InputTextureMotionVectors : register(t1);
RWTexture2D<float4> OutputTexture : register(u0);

groupshared float4 sampleCache[GROUP_SIZE];
groupshared float2 sampleCacheMotionVectors[GROUP_SIZE];
groupshared float errXCache[GROUP_SIZE];
groupshared float errYCache[GROUP_SIZE];

float RgbToLuminance(float3 color)
{
return dot(color, float3(0.299, 0.587, 0.114));
}

[numthreads(8, 8, 1)]
void main(uint3 GroupID : SV_GroupID, uint3 GroupThreadID : SV_GroupThreadID)
{
const uint threadIndex = GroupThreadID.y * 8 + GroupThreadID.x;
const uint2 sampleIndex = (GroupID.xy * 8 + GroupThreadID.xy) * 2.0;

// Fetch color (final post-AA) data
// l0.x l0.y
// l0.z l0.w l2.x
// l1.x l1.y
// l1.z l1.w l2.y
// l2.z
float4 l0;
l0.x = RgbToLuminance(InputTexture[sampleIndex + uint2(0, 0)]);
l0.y = RgbToLuminance(InputTexture[sampleIndex + uint2(1, 0)]);
l0.z = RgbToLuminance(InputTexture[sampleIndex + uint2(0, 1)]);
l0.w = RgbToLuminance(InputTexture[sampleIndex + uint2(1, 1)]);

float4 l1;
l1.x = RgbToLuminance(InputTexture[sampleIndex + uint2(0, 2)]);
l1.y = RgbToLuminance(InputTexture[sampleIndex + uint2(1, 2)]);
l1.z = RgbToLuminance(InputTexture[sampleIndex + uint2(0, 3)]);
l1.w = RgbToLuminance(InputTexture[sampleIndex + uint2(1, 3)]);

float3 l2;
l2.x = RgbToLuminance(InputTexture[sampleIndex + uint2(2, 1)]);
l2.y = RgbToLuminance(InputTexture[sampleIndex + uint2(2, 3)]);
l2.z = RgbToLuminance(InputTexture[sampleIndex + uint2(1, 4)]);

sampleCache[threadIndex] = l0 + l1;

float2 m = 0.0f;
m += InputTextureMotionVectors[sampleIndex + uint2(0, 0)];
m += InputTextureMotionVectors[sampleIndex + uint2(1, 0)];
m += InputTextureMotionVectors[sampleIndex + uint2(0, 1)];
m += InputTextureMotionVectors[sampleIndex + uint2(1, 1)];

m += InputTextureMotionVectors[sampleIndex + uint2(0, 2)];
m += InputTextureMotionVectors[sampleIndex + uint2(1, 2)];
m += InputTextureMotionVectors[sampleIndex + uint2(0, 3)];
m += InputTextureMotionVectors[sampleIndex + uint2(1, 3)];

sampleCacheMotionVectors[threadIndex] = m;

// Derivatives X
float4 a = float4(l0.y, l2.x, l1.y, l2.y);
float4 b = float4(l0.x, l0.w, l1.x, l1.w);
float4 dx = abs(a - b);

// Derivatives Y
a = float4(l0.z, l1.y, l1.z, l2.z);
b = float4(l0.x, l0.w, l1.x, l1.w);
float4 dy = abs(a - b);

// Compute maximum partial derivative of all 16x16 pixels (256 total)
// this approach is more "sensitive" to individual outliers in a tile, since it takes the max instead of the average
float maxDx = max(max(dx.x, dx.y), max(dx.z, dx.w));
float maxDy = max(max(dy.x, dy.y), max(dy.z, dy.w));

errXCache[threadIndex] = maxDx;
errYCache[threadIndex] = maxDy;

GroupMemoryBarrierWithGroupSync();

// Parallel reduction
[unroll] for (uint s = (64 >> 1); s > 0; s >>= 1)
{
if(threadIndex < s){
sampleCache[threadIndex] += sampleCache[threadIndex + s];
sampleCacheMotionVectors[threadIndex] += sampleCacheMotionVectors[threadIndex + s];
errXCache[threadIndex] = max(errXCache[threadIndex], errXCache[threadIndex + s]);
errYCache[threadIndex] = max(errYCache[threadIndex], errYCache[threadIndex + s]);
}

GroupMemoryBarrierWithGroupSync();
}

// Average
if(threadIndex == 0){
float avgLuma = dot(sampleCache[0], 1.0 / 8.0) / GROUP_SIZE + 0.1;
float2 avgMotionVectors = dot(sampleCacheMotionVectors[0], 1.0 / 8.0) / GROUP_SIZE;
float errX = errXCache[0];
float errY = errYCache[0];
OutputTexture[GroupID.xy] = float4(float2(errX, errY) / abs(avgLuma), avgMotionVectors);
}
}
80 changes: 80 additions & 0 deletions package/Shaders/VariableRateShading/ComputeShadingRate.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
RWTexture2D<uint> vrsSurface : register(u0);
Texture2D<float4> nasDataSurface : register(t0);

[numthreads(32, 32, 1)]
void main(uint3 DispatchThreadID : SV_DispatchThreadID, uint3 GroupThreadID : SV_GroupThreadID, uint3 GroupID : SV_GroupID)
{
float4 nasData = nasDataSurface[DispatchThreadID.xy];

float2 mVec = abs(nasData.zw);

// Error scalers (equations from the I3D 2019 paper)
// bhv for half rate, bqv for quarter rate
float2 bhv = pow(1.0 / (1 + pow(1.05 * mVec, 3.1)), 0.35);
float2 bqv = 2.13 * pow(1.0 / (1 + pow(0.55 * mVec, 2.41)), 0.49);

// Sample block error data from NAS data pass and apply the error scalars
float2 diff = nasData.xy;
float2 diff2 = diff * bhv;
float2 diff4 = diff * bqv;

uint screenWidth, screenHeight;
nasDataSurface.GetDimensions(screenWidth, screenHeight);

float2 uv = DispatchThreadID.xy * rcp(float2(screenWidth, screenHeight));
float threshold = lerp(0.1, 0.2, distance(float2(0.5, 0.5), uv));

/*`
D3D12_SHADING_RATE_1X1 = 0, // 0b0000
D3D12_SHADING_RATE_1X2 = 0x1, // 0b0001
D3D12_SHADING_RATE_2X1 = 0x4, // 0b0100
D3D12_SHADING_RATE_2X2 = 0x5, // 0b0101
D3D12_SHADING_RATE_2X4 = 0x6, // 0b0110
D3D12_SHADING_RATE_4X2 = 0x9, // 0b1001
D3D12_SHADING_RATE_4X4 = 0xa // 0b1010
*/

// Compute block shading rate based on if the error computation goes over the threshold
// shading rates in D3D are purposely designed to be able to combined, e.g. 2x1 | 1x2 = 2x2
uint ShadingRate = 0;
ShadingRate |= ((diff2.x >= threshold) ? 0 : ((diff4.x > threshold) ? 0x4 : 0x8));
ShadingRate |= ((diff2.y >= threshold) ? 0 : ((diff4.y > threshold) ? 0x1 : 0x2));

// Disable 4x4 shading rate (low quality, limited perf gain)
if (ShadingRate == 0xa)
{
ShadingRate = (diff2.x > diff2.y) ? 0x6 : 0x9; // use 2x4 or 4x2 based on directional gradient
}
// Disable 4x1 or 1x4 shading rate (unsupported)
else if (ShadingRate == 0x8)
{
ShadingRate = 0x4;
}
else if (ShadingRate == 0x2)
{
ShadingRate = 0x1;
}

// vsrd[i].shadingRateTable[0] = NV_PIXEL_X1_PER_RASTER_PIXEL;
// vsrd[i].shadingRateTable[1] = NV_PIXEL_X1_PER_2X1_RASTER_PIXELS;
// vsrd[i].shadingRateTable[2] = NV_PIXEL_X1_PER_1X2_RASTER_PIXELS;
// vsrd[i].shadingRateTable[3] = NV_PIXEL_X1_PER_2X2_RASTER_PIXELS;
// vsrd[i].shadingRateTable[4] = NV_PIXEL_X1_PER_4X2_RASTER_PIXELS;
// vsrd[i].shadingRateTable[5] = NV_PIXEL_X1_PER_2X4_RASTER_PIXELS;
// vsrd[i].shadingRateTable[6] = NV_PIXEL_X1_PER_4X4_RASTER_PIXELS;

if (ShadingRate == 0x1)
ShadingRate = 2;
else if (ShadingRate == 0x4)
ShadingRate = 1;
else if (ShadingRate == 0x5)
ShadingRate = 3;
else if (ShadingRate == 0x6)
ShadingRate = 5;
else if (ShadingRate == 0x9)
ShadingRate = 4;
else if (ShadingRate == 0xa)
ShadingRate = 6;

vrsSurface[DispatchThreadID.xy] = ShadingRate;
}
19 changes: 19 additions & 0 deletions package/Shaders/VariableRateShading/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2014-2021, NVIDIA CORPORATION. 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.
1 change: 1 addition & 0 deletions src/Bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Features/CloudShadows.h>
#include <Features/ScreenSpaceGI.h>
#include <Features/ScreenSpaceShadows.h>
#include <VariableRateShading.h>
#include <Features/SubsurfaceScattering.h>
#include <Features/TerrainOcclusion.h>
#include <ShaderCache.h>
Expand Down
7 changes: 1 addition & 6 deletions src/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Menu.h"
#include "ShaderCache.h"
#include "State.h"
#include "VariableRateShading.h"

#include "ShaderTools/BSShaderHooks.h"

Expand Down Expand Up @@ -142,13 +143,7 @@ decltype(&hk_BSGraphics_SetDirtyStates) ptr_BSGraphics_SetDirtyStates;

void hk_BSGraphics_SetDirtyStates(bool isCompute)
{
//auto& shaderCache = SIE::ShaderCache::Instance();

//if (shaderCache.IsEnabled())
// Bindings::GetSingleton()->SetDirtyStates(isCompute);

(ptr_BSGraphics_SetDirtyStates)(isCompute);

State::GetSingleton()->Draw();
}

Expand Down
5 changes: 5 additions & 0 deletions src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include "Bindings.h"

#include "VariableRateShading.h"

#define SETTING_MENU_TOGGLEKEY "Toggle Key"
#define SETTING_MENU_SKIPKEY "Skip Compilation Key"
#define SETTING_MENU_FONTSCALE "Font Scale"
Expand Down Expand Up @@ -161,6 +163,7 @@ void Menu::DrawSettings()
if (ImGui::Button("Clear Shader Cache", { -1, 0 })) {
shaderCache.Clear();
Bindings::GetSingleton()->ClearShaderCache();
VariableRateShading::GetSingleton()->ClearShaderCache();
for (auto* feature : Feature::GetFeatureList()) {
if (feature->loaded) {
feature->ClearShaderCache();
Expand Down Expand Up @@ -427,6 +430,8 @@ void Menu::DrawSettings()

ImGui::Separator();

VariableRateShading::GetSingleton()->DrawSettings();

if (ImGui::BeginTable("Feature Table", 2, ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_Resizable)) {
ImGui::TableSetupColumn("##ListOfFeatures", 0, 3);
ImGui::TableSetupColumn("##FeatureConfig", 0, 7);
Expand Down
53 changes: 26 additions & 27 deletions src/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,39 @@
#include "Bindings.h"
#include "Features/TerrainBlending.h"

#include "VariableRateShading.h"

void State::Draw()
{
Bindings::GetSingleton()->CheckOpaque();
auto& shaderCache = SIE::ShaderCache::Instance();
if (shaderCache.IsEnabled() && currentShader && updateShader) {
if (currentShader && updateShader) {
auto type = currentShader->shaderType.get();
if (type > 0 && type < RE::BSShader::Type::Total) {
if (enabledClasses[type - 1]) {
ModifyShaderLookup(*currentShader, currentVertexDescriptor, currentPixelDescriptor);
UpdateSharedData(currentShader, currentPixelDescriptor);
VariableRateShading::GetSingleton()->UpdateViews(type != RE::BSShader::Type::ImageSpace && type != RE::BSShader::Type::Sky);
auto& shaderCache = SIE::ShaderCache::Instance();
if (shaderCache.IsEnabled()) {
if (type > 0 && type < RE::BSShader::Type::Total) {
if (enabledClasses[type - 1]) {
ModifyShaderLookup(*currentShader, currentVertexDescriptor, currentPixelDescriptor);
UpdateSharedData(currentShader, currentPixelDescriptor);

static RE::BSGraphics::VertexShader* vertexShader = nullptr;
static RE::BSGraphics::PixelShader* pixelShader = nullptr;
auto context = RE::BSGraphics::Renderer::GetSingleton()->GetRuntimeData().context;

vertexShader = shaderCache.GetVertexShader(*currentShader, currentVertexDescriptor);
pixelShader = shaderCache.GetPixelShader(*currentShader, currentPixelDescriptor);
static RE::BSGraphics::VertexShader* vertexShader = nullptr;
static RE::BSGraphics::PixelShader* pixelShader = nullptr;

if (vertexShader && pixelShader) {
context->VSSetShader(reinterpret_cast<ID3D11VertexShader*>(vertexShader->shader), NULL, NULL);
context->PSSetShader(reinterpret_cast<ID3D11PixelShader*>(pixelShader->shader), NULL, NULL);
}
vertexShader = shaderCache.GetVertexShader(*currentShader, currentVertexDescriptor);
pixelShader = shaderCache.GetPixelShader(*currentShader, currentPixelDescriptor);

BeginPerfEvent(std::format("Draw: CommunityShaders {}::{}", magic_enum::enum_name(currentShader->shaderType.get()), currentPixelDescriptor));
if (IsDeveloperMode()) {
SetPerfMarker(std::format("Defines: {}", SIE::ShaderCache::GetDefinesString(currentShader->shaderType.get(), currentPixelDescriptor)));
}
if (vertexShader && pixelShader) {
context->VSSetShader(vertexShader->shader, NULL, NULL);
context->PSSetShader(pixelShader->shader, NULL, NULL);
}

if (vertexShader && pixelShader) {
for (auto* feature : Feature::GetFeatureList()) {
if (feature->loaded) {
auto hasShaderDefine = feature->HasShaderDefine(currentShader->shaderType.get());
if (hasShaderDefine)
BeginPerfEvent(feature->GetShortName());
feature->Draw(currentShader, currentPixelDescriptor);
if (hasShaderDefine)
EndPerfEvent();
if (vertexShader && pixelShader) {
for (auto* feature : Feature::GetFeatureList()) {
if (feature->loaded) {
feature->Draw(currentShader, currentPixelDescriptor);
}
}
}
}
Expand Down Expand Up @@ -173,6 +170,7 @@ void State::Reset()
Bindings::GetSingleton()->Reset();
if (!RE::UI::GetSingleton()->GameIsPaused())
timer += RE::GetSecondsSinceLastFrame();
VariableRateShading::GetSingleton()->UpdateVRS();
}

void State::Setup()
Expand All @@ -182,6 +180,7 @@ void State::Setup()
if (feature->loaded)
feature->SetupResources();
Bindings::GetSingleton()->SetupResources();
VariableRateShading::GetSingleton()->Setup();
}

static const std::string& GetConfigPath(State::ConfigMode a_configMode)
Expand Down
Loading

0 comments on commit c9acb7b

Please sign in to comment.