Skip to content

Commit d5cc305

Browse files
committed
Add a configurable LOD bias
1 parent 502856e commit d5cc305

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

impl.cpp

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -834,11 +834,28 @@ class DeviceWrapper final : public ID3D11Device, IDXGIDevice1 {
834834

835835
HRESULT STDMETHODCALLTYPE CreateSamplerState(const D3D11_SAMPLER_DESC* pSamplerDesc, ID3D11SamplerState** ppSamplerState) override {
836836
D3D11_SAMPLER_DESC desc = *pSamplerDesc;
837+
bool modified = false;
837838
if (config.anisotropy && desc.Filter == D3D11_FILTER_MIN_MAG_MIP_LINEAR) {
838839
desc.Filter = D3D11_FILTER_ANISOTROPIC;
839840
desc.MaxAnisotropy = config.anisotropy;
841+
modified = true;
840842
}
841-
return dev->CreateSamplerState(&desc, ppSamplerState);
843+
float bias;
844+
if (config.lodBias < 0)
845+
bias = std::max(config.lodBias, config.lodBias + desc.MipLODBias);
846+
else
847+
bias = std::min(config.lodBias, config.lodBias + desc.MipLODBias);
848+
if (bias != desc.MipLODBias) {
849+
desc.MipLODBias = bias;
850+
modified = true;
851+
}
852+
HRESULT hr = dev->CreateSamplerState(&desc, ppSamplerState);
853+
if (SUCCEEDED(hr)) {
854+
ID3D11SamplerState* orig;
855+
if (SUCCEEDED(dev->CreateSamplerState(pSamplerDesc, &orig)))
856+
(*ppSamplerState)->SetPrivateDataInterface(IID_OriginalShader, orig);
857+
}
858+
return hr;
842859
}
843860

844861
HRESULT STDMETHODCALLTYPE CreateShaderResourceView(ID3D11Resource* pResource, const D3D11_SHADER_RESOURCE_VIEW_DESC* pDesc, ID3D11ShaderResourceView** ppSRView) override {
@@ -1039,7 +1056,10 @@ class ContextWrapper final : public ID3D11DeviceContext {
10391056
ID3D11BlendState* requestedBlend = nullptr;
10401057
ID3D11PixelShader* requestedPS = nullptr;
10411058
D3D11_MAPPED_SUBRESOURCE lastMap;
1059+
void* tmpMemory = nullptr;
1060+
UINT tmpMemorySize = 0;
10421061
UINT scissorWidth, scissorHeight, viewportWidth, viewportHeight;
1062+
bool shouldUseOldShaders = false;
10431063
bool blendStateSupportsA2C = false;
10441064
bool psSupportsA2C = false;
10451065
bool rtsizeDirty = false;
@@ -1097,7 +1117,6 @@ class ContextWrapper final : public ID3D11DeviceContext {
10971117

10981118
// ID3D11DeviceContext
10991119
void VSSetConstantBuffers(UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppConstantBuffers) override { ctx->VSSetConstantBuffers(StartSlot, NumBuffers, ppConstantBuffers); }
1100-
void PSSetSamplers(UINT StartSlot, UINT NumSamplers, ID3D11SamplerState* const* ppSamplers) override { ctx->PSSetSamplers(StartSlot, NumSamplers, ppSamplers); }
11011120
void VSSetShader(ID3D11VertexShader* pVertexShader, ID3D11ClassInstance* const* ppClassInstances, UINT NumClassInstances) override { ctx->VSSetShader(pVertexShader, ppClassInstances, NumClassInstances); }
11021121
void PSSetConstantBuffers(UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppConstantBuffers) override { ctx->PSSetConstantBuffers(StartSlot, NumBuffers, ppConstantBuffers); }
11031122
void IASetInputLayout(ID3D11InputLayout* pInputLayout) override { ctx->IASetInputLayout(pInputLayout); }
@@ -1345,6 +1364,31 @@ class ContextWrapper final : public ID3D11DeviceContext {
13451364
ctx->DrawIndexedInstancedIndirect(pBufferForArgs, AlignedByteOffsetForArgs);
13461365
}
13471366

1367+
void PSSetSamplers(UINT StartSlot, UINT NumSamplers, ID3D11SamplerState* const* ppSamplers) override {
1368+
bool replacedSamplers = false;
1369+
if (shouldUseOldShaders) {
1370+
for (UINT i = 0; i < NumSamplers; i++) {
1371+
ID3D11SamplerState* orig;
1372+
UINT size = sizeof(orig);
1373+
if (SUCCEEDED(ppSamplers[i]->GetPrivateData(IID_OriginalShader, &size, &orig))) {
1374+
orig->Release(); // Still being held by sampler private data
1375+
if (!replacedSamplers) {
1376+
replacedSamplers = true;
1377+
SIZE_T size = sizeof(*ppSamplers) * NumSamplers;
1378+
if (tmpMemorySize < size) {
1379+
tmpMemory = realloc(tmpMemory, size);
1380+
tmpMemorySize = size;
1381+
}
1382+
memcpy(tmpMemory, ppSamplers, size);
1383+
ppSamplers = static_cast<ID3D11SamplerState**>(tmpMemory);
1384+
}
1385+
const_cast<ID3D11SamplerState**>(ppSamplers)[i] = orig;
1386+
}
1387+
}
1388+
}
1389+
ctx->PSSetSamplers(StartSlot, NumSamplers, ppSamplers);
1390+
}
1391+
13481392
void PSSetShaderResources(UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView* const* ppShaderResourceViews) override {
13491393
for (UINT i = 0; i < NumViews; i++) {
13501394
if (ppShaderResourceViews[i]) {
@@ -1360,7 +1404,7 @@ class ContextWrapper final : public ID3D11DeviceContext {
13601404
void PSSetShader(ID3D11PixelShader* pPixelShader, ID3D11ClassInstance* const* ppClassInstances, UINT NumClassInstances) override {
13611405
requestedPS = pPixelShader;
13621406
ID3D11PixelShader* a2c = nullptr;
1363-
bool useOriginalShaders = shouldUseOldShaders();
1407+
bool useOriginalShaders = shouldUseOldShaders;
13641408
bool oldUseA2C = ShouldUseA2C();
13651409
if (pPixelShader && !useOriginalShaders) {
13661410
UINT size = sizeof(a2c);
@@ -1410,6 +1454,8 @@ class ContextWrapper final : public ID3D11DeviceContext {
14101454
}
14111455

14121456
void ClearDepthStencilView(ID3D11DepthStencilView* pDepthStencilView, UINT ClearFlags, FLOAT Depth, UINT8 Stencil) override {
1457+
// Pick something that happens fairly infrequently to update this so we don't spam XInputGetState
1458+
shouldUseOldShaders = ::atfix::shouldUseOldShaders();
14131459
if (ID3D11DepthStencilView* msaa = getMSAADSV(pDepthStencilView)) {
14141460
ctx->ClearDepthStencilView(msaa, ClearFlags, Depth, Stencil);
14151461
msaa->Release();

impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern Log log;
1414
extern struct Config {
1515
DWORD msaaSamples;
1616
DWORD anisotropy;
17+
float lodBias;
1718
bool ssaaCharacters;
1819
bool sampleRateAlpha;
1920
bool ssaaAll;

main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ D3D11Proc loadSystemD3D11() {
7979
"[Other]\n"
8080
"; Set to 1-16 to enable anisotropic filtering\n"
8181
"AF = 0\n"
82+
"; Apply an additional bias to texture LODs (negative makes things less blurry and more shimmery, positive does the opposite)\n"
83+
"LODBias = 0\n"
8284
"; Allow toggling shader enhancements by holding BACK / SELECT (will not toggle MSAA but will toggle most other things)\n"
8385
"EnhancementToggle = 0\n";
8486
HANDLE file = CreateFileA("atfix.ini", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
@@ -92,23 +94,26 @@ D3D11Proc loadSystemD3D11() {
9294
char SampleRateAlpha[8];
9395
char FullSSAA[8];
9496
char AF[8];
97+
char LODBias[8];
9598
char UseShaderToggle[8];
9699
bool ok = true;
97100
GetPrivateProfileStringA("MSAA", "NumSamples", "8", NumSamples, sizeof(NumSamples), ".\\atfix.ini");
98101
GetPrivateProfileStringA("MSAA", "CharacterSSAA", "1", CharacterSSAA, sizeof(CharacterSSAA), ".\\atfix.ini");
99102
GetPrivateProfileStringA("MSAA", "AlphaSSAA", "1", SampleRateAlpha, sizeof(SampleRateAlpha), ".\\atfix.ini");
100103
GetPrivateProfileStringA("MSAA", "FullSSAA", "0", FullSSAA, sizeof(FullSSAA), ".\\atfix.ini");
101104
GetPrivateProfileStringA("Other", "AF", "0", AF, sizeof(AF), ".\\atfix.ini");
105+
GetPrivateProfileStringA("Other", "LODBias", "0", LODBias, sizeof(LODBias), ".\\atfix.ini");
102106
GetPrivateProfileStringA("Other", "EnhancementToggle", "0", UseShaderToggle, sizeof(UseShaderToggle), ".\\atfix.ini");
103107
config.msaaSamples = atoi(NumSamples);
104108
config.ssaaCharacters = atoi(CharacterSSAA);
105109
config.sampleRateAlpha = atoi(SampleRateAlpha);
106110
config.ssaaAll = atoi(FullSSAA);
107111
config.anisotropy = atoi(AF);
112+
config.lodBias = atof(LODBias);
108113
config.allowShaderToggle = atoi(UseShaderToggle);
109114
if (config.msaaSamples < 1)
110115
config.msaaSamples = 1;
111-
log("Loaded config, ", config.msaaSamples, " samples, ", config.ssaaCharacters, " characterSSAA, ", config.sampleRateAlpha, " sampleRateAlpha, ", config.ssaaAll, " fullSSAA, ", config.anisotropy, " AF, ", config.allowShaderToggle, " enhancementToggle ");
116+
log("Loaded config, ", config.msaaSamples, " samples, ", config.ssaaCharacters, " characterSSAA, ", config.sampleRateAlpha, " sampleRateAlpha, ", config.ssaaAll, " fullSSAA, ", config.anisotropy, " AF, ", config.lodBias, " lodBias, ", config.allowShaderToggle, " enhancementToggle ");
112117
}
113118

114119
d3d11Proc.D3D11CreateDevice = reinterpret_cast<PFN_D3D11CreateDevice>(

0 commit comments

Comments
 (0)