Skip to content

Commit 2d4c464

Browse files
committed
Make MSAA samples configurable
1 parent 5155fd9 commit 2d4c464

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

ShaderSampleRateConverter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ bool shouldUseSampleRate(const void* data, SIZE_T length) {
4848
for (const ShaderHash& hash : sampleRateShaders)
4949
if (hash == header->hash)
5050
return true;
51+
if (config.ssaaTransparentObjects && header->hash == ShaderHash({ 0x0cd1b9e5, 0x22e7069e, 0x476455ff, 0x98bfd850 }))
52+
return true;
5153
return false;
5254
}
5355

impl.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ ID3D11RenderTargetView* getOrCreateMSAARTV(ID3D11Device* dev, ID3D11RenderTarget
226226
D3D11_TEXTURE2D_DESC desc;
227227
hostTex->GetDesc(&desc);
228228
desc.Format = vdesc.Format;
229-
desc.SampleDesc.Count = 8;
229+
desc.SampleDesc.Count = config.msaaSamples;
230230
desc.SampleDesc.Quality = 0;
231231
while (desc.SampleDesc.Count > 1) {
232232
UINT quality = 0;
@@ -263,7 +263,7 @@ ID3D11DepthStencilView* getOrCreateMSAADSV(ID3D11Device* dev, ID3D11DepthStencil
263263
D3D11_TEXTURE2D_DESC desc;
264264
hostTex->GetDesc(&desc);
265265
desc.Format = vdesc.Format;
266-
desc.SampleDesc.Count = 8;
266+
desc.SampleDesc.Count = config.msaaSamples;
267267
desc.SampleDesc.Quality = 0;
268268
while (desc.SampleDesc.Count > 1) {
269269
UINT quality = 0;
@@ -779,7 +779,7 @@ class DeviceWrapper final : public ID3D11Device, IDXGIDevice1 {
779779

780780
HRESULT STDMETHODCALLTYPE CreatePixelShader(const void* pShaderBytecode, SIZE_T BytecodeLength, ID3D11ClassLinkage* pClassLinkage, ID3D11PixelShader** ppPixelShader) override {
781781
void* converted = nullptr;
782-
if (shouldUseSampleRate(pShaderBytecode, BytecodeLength))
782+
if (config.msaaSamples > 1 && shouldUseSampleRate(pShaderBytecode, BytecodeLength))
783783
converted = convertShaderToSampleRate(pShaderBytecode, BytecodeLength);
784784
HRESULT res = dev->CreatePixelShader(converted ? converted : pShaderBytecode, BytecodeLength, pClassLinkage, ppPixelShader);
785785
if (converted)
@@ -789,7 +789,8 @@ class DeviceWrapper final : public ID3D11Device, IDXGIDevice1 {
789789

790790
HRESULT STDMETHODCALLTYPE CreateRasterizerState(const D3D11_RASTERIZER_DESC* pRasterizerDesc, ID3D11RasterizerState** ppRasterizerState) override {
791791
D3D11_RASTERIZER_DESC desc = *pRasterizerDesc;
792-
desc.MultisampleEnable = TRUE;
792+
if (config.msaaSamples > 1)
793+
desc.MultisampleEnable = TRUE;
793794
return dev->CreateRasterizerState(&desc, ppRasterizerState);
794795
}
795796

@@ -1159,7 +1160,7 @@ class ContextWrapper final : public ID3D11DeviceContext {
11591160
ID3D11RenderTargetView* msaaTex = nullptr;
11601161
ID3D11DepthStencilView* msaaDepth = nullptr;
11611162

1162-
if (ppRTVs && RTVCount == 1 && pDSV) {
1163+
if (config.msaaSamples > 1 && ppRTVs && RTVCount == 1 && pDSV) {
11631164
ppRTVs[0]->GetResource(&base);
11641165
UINT info;
11651166
UINT size = sizeof(info);

impl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ ID3D11DeviceContext* hookContext(ID3D11DeviceContext* pContext);
1111

1212
/* lives in main.cpp */
1313
extern Log log;
14-
14+
extern struct Config {
15+
DWORD msaaSamples;
16+
BOOL ssaaTransparentObjects;
17+
} config;
1518
}

main.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace atfix {
1818

1919
Log log("atfix.log");
20+
Config config { 8, 0 };
2021

2122
/** Load system D3D11 DLL and return entry points */
2223
using PFN_D3D11CreateDevice = HRESULT (__stdcall *) (
@@ -65,6 +66,30 @@ D3D11Proc loadSystemD3D11() {
6566
}
6667
}
6768

69+
if (GetFileAttributesA("atfix.ini") == ~0) {
70+
const char* str = "[MSAA]\n"
71+
"; Number of samples (1 = no MSAA)\n"
72+
"NumSamples = 8\n"
73+
"; Whether to do SSAA on transparent objects like grass (expensive)\n"
74+
"ObjectSSAA = 0\n";
75+
HANDLE file = CreateFileA("atfix.ini", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
76+
if (file != INVALID_HANDLE_VALUE) {
77+
WriteFile(file, str, strlen(str), nullptr, nullptr);
78+
CloseHandle(file);
79+
}
80+
} else {
81+
char NumSamples[8];
82+
char ObjectSSAA[8];
83+
bool ok = true;
84+
GetPrivateProfileStringA("MSAA", "NumSamples", "8", NumSamples, sizeof(NumSamples), ".\\atfix.ini");
85+
GetPrivateProfileStringA("MSAA", "ObjectSSAA", "8", ObjectSSAA, sizeof(ObjectSSAA), ".\\atfix.ini");
86+
config.msaaSamples = atoi(NumSamples);
87+
config.ssaaTransparentObjects = atoi(ObjectSSAA);
88+
if (config.msaaSamples < 1)
89+
config.msaaSamples = 1;
90+
log("Loaded config, ", config.msaaSamples, " samples, ", config.ssaaTransparentObjects, " objectSSAA");
91+
}
92+
6893
d3d11Proc.D3D11CreateDevice = reinterpret_cast<PFN_D3D11CreateDevice>(
6994
GetProcAddress(libD3D11, "D3D11CreateDevice"));
7095
d3d11Proc.D3D11CreateDeviceAndSwapChain = reinterpret_cast<PFN_D3D11CreateDeviceAndSwapChain>(

0 commit comments

Comments
 (0)