Skip to content

Commit

Permalink
refactor: misc code cleanup (#363)
Browse files Browse the repository at this point in the history
* refactor: tidier blendStates

* performance: less get/set in `CopyShadowData`

* style: 🎨 apply clang-format changes

* chore: remove unused `CountedTimer`

* refactor: templatise `DumpShader`

* refactor: misc CppCheck complaints

* chore: oct id for `DumpShader`

* performance: fix nonstatic relocations

* performance: avoid new string alloc

* refactor: rename `GetDXBlendStates` to `GetBlendStates`

---------

Co-authored-by: Pentalimbed <Pentalimbed@users.noreply.github.com>
  • Loading branch information
Pentalimbed and Pentalimbed authored Jul 31, 2024
1 parent 58577b1 commit 6c76c05
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 296 deletions.
50 changes: 25 additions & 25 deletions package/Shaders/ShadowTest/CopyShadowData.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,13 @@ struct PerGeometry
float4x4 CameraViewProjInverse[2];
};

// copied from UtilShader(b2). Assume RENDER_SHADOWMASK
cbuffer PerFrame : register(b0)
{
float4 DebugColor : packoffset(c0);
float4 PropertyColor : packoffset(c1);
float4 AlphaTestRef : packoffset(c2);
float4 ShadowLightParam : packoffset(c3); // Falloff in x, ShadowDistance squared in z
#if !defined(VR)
float4x3 FocusShadowMapProj[4] : packoffset(c4);
float4x3 ShadowMapProj[1][3] : packoffset(c16); // 16, 19, 22
#else
float4 VRUnknown : packoffset(c4); // used to multiply by identity matrix, see e.g., 4202499.ps.bin.hlsl
/*
r1.x = dot(cb2[4].xz, icb[r0.w+0].xz);
r1.x = r0.x * cb12[86].x + -r1.x;
r0.w = (int)r0.w + 1;
r0.w = (int)r0.w + -1;
r0.w = dot(cb2[4].yw, icb[r0.w+0].xz);
*/
float4x3 FocusShadowMapProj[4] : packoffset(c5);
float4x3 ShadowMapProj[2][3] : packoffset(c29); // VR has a couple of offsets of 3, e.g., {29, 32, 35} and {38, 41, 44}, compare to Flat which does [16, 19, 22]
#endif // VR
float4 VPOSOffset : packoffset(c0);
float4 ShadowSampleParam : packoffset(c1); // fPoissonRadiusScale / iShadowMapResolution in z and w
float4 EndSplitDistances : packoffset(c2); // cascade end distances int xyz, cascade count int z
float4 StartSplitDistances : packoffset(c3); // cascade start ditances int xyz, 4 int z
float4 FocusShadowFadeParam : packoffset(c4);
}

cbuffer PerFrame2 : register(b1)
Expand Down Expand Up @@ -89,13 +74,28 @@ cbuffer PerFrame2 : register(b1)
#endif // !VR
}

// copied from UtilShader(b2). Assume RENDER_SHADOWMASK
cbuffer PerFrame3 : register(b2)
{
float4 VPOSOffset : packoffset(c0);
float4 ShadowSampleParam : packoffset(c1); // fPoissonRadiusScale / iShadowMapResolution in z and w
float4 EndSplitDistances : packoffset(c2); // cascade end distances int xyz, cascade count int z
float4 StartSplitDistances : packoffset(c3); // cascade start ditances int xyz, 4 int z
float4 FocusShadowFadeParam : packoffset(c4);
float4 DebugColor : packoffset(c0);
float4 PropertyColor : packoffset(c1);
float4 AlphaTestRef : packoffset(c2);
float4 ShadowLightParam : packoffset(c3); // Falloff in x, ShadowDistance squared in z
#if !defined(VR)
float4x3 FocusShadowMapProj[4] : packoffset(c4);
float4x3 ShadowMapProj[1][3] : packoffset(c16); // 16, 19, 22
#else
float4 VRUnknown : packoffset(c4); // used to multiply by identity matrix, see e.g., 4202499.ps.bin.hlsl
/*
r1.x = dot(cb2[4].xz, icb[r0.w+0].xz);
r1.x = r0.x * cb12[86].x + -r1.x;
r0.w = (int)r0.w + 1;
r0.w = (int)r0.w + -1;
r0.w = dot(cb2[4].yw, icb[r0.w+0].xz);
*/
float4x3 FocusShadowMapProj[4] : packoffset(c5);
float4x3 ShadowMapProj[2][3] : packoffset(c29); // VR has a couple of offsets of 3, e.g., {29, 32, 35} and {38, 41, 44}, compare to Flat which does [16, 19, 22]
#endif // VR
}

RWStructuredBuffer<PerGeometry> copiedData : register(u0);
Expand Down
236 changes: 51 additions & 185 deletions src/Deferred.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ struct DepthStates
struct BlendStates
{
ID3D11BlendState* a[7][2][13][2];

static BlendStates* GetSingleton()
{
static auto blendStates = reinterpret_cast<BlendStates*>(REL::RelocationID(524749, 411364).address());
return blendStates;
}

static std::array<ID3D11BlendState**, 6> GetBlendStates()
{
auto blendStates = GetSingleton();
return {
&blendStates->a[0][0][1][0],
&blendStates->a[0][0][10][0],
&blendStates->a[1][0][1][0],
&blendStates->a[1][0][11][0],
&blendStates->a[2][0][1][0],
&blendStates->a[3][0][11][0]
};
}
};

void SetupRenderTarget(RE::RENDER_TARGET target, D3D11_TEXTURE2D_DESC texDesc, D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc, D3D11_RENDER_TARGET_VIEW_DESC rtvDesc, D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc, DXGI_FORMAT format)
Expand Down Expand Up @@ -175,17 +194,11 @@ void Deferred::CopyShadowData()
ID3D11UnorderedAccessView* uavs[1]{ perShadow->uav.get() };
context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr);

ID3D11Buffer* buffers[1];
context->PSGetConstantBuffers(2, 1, buffers);
context->CSSetConstantBuffers(0, 1, buffers);

context->PSGetConstantBuffers(12, 1, buffers);
context->CSSetConstantBuffers(1, 1, buffers);
ID3D11Buffer* buffers[3];
context->PSGetConstantBuffers(0, 3, buffers);
context->PSGetConstantBuffers(12, 1, buffers + 1);

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

context->PSGetShaderResources(4, 1, &shadowView);
context->CSSetConstantBuffers(0, 3, buffers);

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

Expand All @@ -194,14 +207,14 @@ void Deferred::CopyShadowData()
uavs[0] = nullptr;
context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr);

buffers[0] = nullptr;
context->CSSetConstantBuffers(0, 1, buffers);
context->CSSetConstantBuffers(1, 1, buffers);
context->CSSetConstantBuffers(2, 1, buffers);
std::fill(buffers, buffers + ARRAYSIZE(buffers), nullptr);
context->CSSetConstantBuffers(0, 3, buffers);

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

{
context->PSGetShaderResources(4, 1, &shadowView);

ID3D11ShaderResourceView* srvs[2]{
shadowView,
perShadow->srv.get(),
Expand Down Expand Up @@ -271,90 +284,23 @@ void Deferred::StartDeferred()

State::GetSingleton()->UpdateSharedData();

static bool setup = false;
if (!setup) {
static std::once_flag setup;
std::call_once(setup, [&]() {
auto& device = State::GetSingleton()->device;

static BlendStates* blendStates = reinterpret_cast<BlendStates*>(REL::RelocationID(524749, 411364).address());

{
forwardBlendStates[0] = blendStates->a[0][0][1][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[0]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[0]));
}

{
forwardBlendStates[1] = blendStates->a[0][0][10][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[1]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[1]));
}

{
forwardBlendStates[2] = blendStates->a[1][0][1][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[2]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[2]));
}
auto blendStates = BlendStates::GetBlendStates();

{
forwardBlendStates[3] = blendStates->a[1][0][11][0];
for (int i = 0; i < blendStates.size(); ++i) {
forwardBlendStates[i] = *blendStates[i];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[3]->GetDesc(&blendDesc);
forwardBlendStates[i]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[3]));
DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[i]));
}

{
forwardBlendStates[4] = blendStates->a[2][0][1][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[4]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[4]));
}

{
forwardBlendStates[5] = blendStates->a[2][0][11][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[5]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[5]));
}

{
forwardBlendStates[6] = blendStates->a[3][0][11][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[6]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[6]));
}
setup = true;
}
});

auto shadowState = RE::BSGraphics::RendererShadowState::GetSingleton();
GET_INSTANCE_MEMBER(renderTargets, shadowState)
Expand Down Expand Up @@ -397,7 +343,7 @@ void Deferred::DeferredPasses()
UpdateConstantBuffer();

{
REL::Relocation<ID3D11Buffer**> perFrame{ REL::RelocationID(524768, 411384) };
static REL::Relocation<ID3D11Buffer**> perFrame{ REL::RelocationID(524768, 411384) };
ID3D11Buffer* buffers[2] = { deferredCB->CB(), *perFrame.get() };

context->CSSetConstantBuffers(11, 2, buffers);
Expand Down Expand Up @@ -555,101 +501,27 @@ void Deferred::EndDeferred()

void Deferred::OverrideBlendStates()
{
static bool setup = false;
if (!setup) {
auto& device = State::GetSingleton()->device;

static BlendStates* blendStates = reinterpret_cast<BlendStates*>(REL::RelocationID(524749, 411364).address());

{
forwardBlendStates[0] = blendStates->a[0][0][1][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[0]->GetDesc(&blendDesc);
auto blendStates = BlendStates::GetBlendStates();

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[0]));
}

{
forwardBlendStates[1] = blendStates->a[0][0][10][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[1]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[1]));
}

{
forwardBlendStates[2] = blendStates->a[1][0][1][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[2]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[2]));
}

{
forwardBlendStates[3] = blendStates->a[1][0][11][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[3]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[3]));
}

{
forwardBlendStates[4] = blendStates->a[2][0][1][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[4]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[4]));
}

{
forwardBlendStates[5] = blendStates->a[2][0][11][0];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[5]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[5]));
}
static std::once_flag setup;
std::call_once(setup, [&]() {
auto& device = State::GetSingleton()->device;

{
forwardBlendStates[6] = blendStates->a[3][0][11][0];
for (int i = 0; i < blendStates.size(); ++i) {
forwardBlendStates[i] = *blendStates[i];

D3D11_BLEND_DESC blendDesc;
forwardBlendStates[6]->GetDesc(&blendDesc);
forwardBlendStates[i]->GetDesc(&blendDesc);

blendDesc.IndependentBlendEnable = false;

DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[6]));
DX::ThrowIfFailed(device->CreateBlendState(&blendDesc, &deferredBlendStates[i]));
}
setup = true;
}

static BlendStates* blendStates = reinterpret_cast<BlendStates*>(REL::RelocationID(524749, 411364).address());
});

// Set modified blend states
blendStates->a[0][0][1][0] = deferredBlendStates[0];
blendStates->a[0][0][10][0] = deferredBlendStates[1];
blendStates->a[1][0][1][0] = deferredBlendStates[2];
blendStates->a[1][0][11][0] = deferredBlendStates[3];
blendStates->a[2][0][1][0] = deferredBlendStates[4];
blendStates->a[2][0][11][0] = deferredBlendStates[5];
blendStates->a[3][0][11][0] = deferredBlendStates[6];
for (int i = 0; i < blendStates.size(); ++i)
*blendStates[i] = deferredBlendStates[i];

auto shadowState = RE::BSGraphics::RendererShadowState::GetSingleton();
GET_INSTANCE_MEMBER(stateUpdateFlags, shadowState)
Expand All @@ -659,16 +531,11 @@ void Deferred::OverrideBlendStates()

void Deferred::ResetBlendStates()
{
static BlendStates* blendStates = reinterpret_cast<BlendStates*>(REL::RelocationID(524749, 411364).address());
auto blendStates = BlendStates::GetBlendStates();

// Restore modified blend states
blendStates->a[0][0][1][0] = forwardBlendStates[0];
blendStates->a[0][0][10][0] = forwardBlendStates[1];
blendStates->a[1][0][1][0] = forwardBlendStates[2];
blendStates->a[1][0][11][0] = forwardBlendStates[3];
blendStates->a[2][0][1][0] = forwardBlendStates[4];
blendStates->a[2][0][11][0] = forwardBlendStates[5];
blendStates->a[3][0][11][0] = forwardBlendStates[6];
for (int i = 0; i < blendStates.size(); ++i)
*blendStates[i] = forwardBlendStates[i];

auto shadowState = RE::BSGraphics::RendererShadowState::GetSingleton();
GET_INSTANCE_MEMBER(stateUpdateFlags, shadowState)
Expand Down Expand Up @@ -756,8 +623,7 @@ ID3D11ComputeShader* Deferred::GetComputeMainCompositeInterior()
std::vector<std::pair<const char*, const char*>> defines;
defines.push_back({ "INTERIOR", nullptr });

auto dynamicCubemaps = DynamicCubemaps::GetSingleton();
if (dynamicCubemaps->loaded)
if (DynamicCubemaps::GetSingleton()->loaded)
defines.push_back({ "DYNAMIC_CUBEMAPS", nullptr });

mainCompositeInteriorCS = static_cast<ID3D11ComputeShader*>(Util::CompileShader(L"Data\\Shaders\\DeferredCompositeCS.hlsl", defines, "cs_5_0"));
Expand Down
2 changes: 1 addition & 1 deletion src/Feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void Feature::Load(json& o_json)
loaded = false;

std::string minimalVersionString = minimalFeatureVersion.string();
minimalVersionString = minimalVersionString.substr(0, minimalVersionString.size() - 2);
minimalVersionString.resize(minimalVersionString.size() - 2);

if (majorVersionMismatch) {
failedLoadedMessage = std::format("{} {} requires a newer version of community shaders, the feature version should be {}", GetShortName(), value, minimalVersionString);
Expand Down
Loading

0 comments on commit 6c76c05

Please sign in to comment.