Skip to content

Commit 5a35de5

Browse files
committed
Add option for sample rate alpha instead of alpha to coverage
1 parent bf0e420 commit 5a35de5

File tree

7 files changed

+97
-40
lines changed

7 files changed

+97
-40
lines changed

Environment.hlsl

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ struct PSInput
8585
#endif
8686
};
8787

88-
float4 main(float4 pos : SV_Position, PSInput input) : SV_TARGET{
88+
#if ALPHA == 2 && MSAA_SAMPLE_COUNT != 0
89+
#define COVERAGE_OUT , out uint coverage : SV_Coverage
90+
#else
91+
#define COVERAGE_OUT
92+
#endif
93+
94+
float4 main(float4 pos : SV_Position, PSInput input COVERAGE_OUT) : SV_TARGET {
8995
#if ALPHA != 0
9096
float3 lit = sLit.Sample(smpsLit, input.litCoord.xy).xyz * input.litColor;
9197
float aref = vATest;
@@ -103,10 +109,28 @@ float4 main(float4 pos : SV_Position, PSInput input) : SV_TARGET{
103109
#if ALPHA == 1
104110
clip(color.a - aref);
105111
#elif ALPHA == 2
112+
#if MSAA_SAMPLE_COUNT == 0
106113
float pxAlpha = abs(ddx_fine(color.a)) + abs(ddy_fine(color.a));
107114
float a2c = saturate((color.a - aref) / pxAlpha + 0.5);
108115
if (a2c == 0)
109116
discard;
117+
#else // MSAA_SAMPLE_COUNT == 0
118+
if (ddx_coarse(color.a) != 0 || ddy_coarse(color.a) != 0) {
119+
// In-shader AA
120+
coverage = 0;
121+
[unroll]
122+
for (uint bit = 0; bit < MSAA_SAMPLE_COUNT; bit++) {
123+
coverage |= (sStage0.Sample(smpsStage0, EvaluateAttributeAtSample(input.litCoord, bit).zw).a > aref) << bit;
124+
}
125+
#if MSAA_SAMPLE_COUNT == 16
126+
// 16 is the max we compile for
127+
coverage |= coverage << 16;
128+
#endif
129+
} else {
130+
clip(color.a - aref);
131+
coverage = ~0;
132+
}
133+
#endif // MSAA_SAMPLE_COUNT == 0
110134
#endif
111135
#endif
112136
float2 shadowBase = (input.shadow.xy / input.shadow.w);
@@ -237,11 +261,10 @@ float4 main(float4 pos : SV_Position, PSInput input) : SV_TARGET{
237261
color.a *= input.litBase.a;
238262
color.rgb = lerp(input.baseColor, lit * color.rgb, input.texColorWeight);
239263
color = color * saturate(atColor) + saturate(atColor - 1);
240-
#if ALPHA == 1
264+
#if ALPHA == 1 || MSAA_SAMPLE_COUNT != 0
241265
return float4(color.rgb * HdrRangeInv, color.a);
242266
#else
243267
return float4(color.rgb * HdrRangeInv, a2c);
244268
#endif
245-
246269
#endif
247270
}

ShaderSampleRateConverter.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ bool shouldUseSampleRate(const void* data, SIZE_T length) {
5858
for (const ShaderHash& hash : sampleRateShaders)
5959
if (config.ssaaCharacters && hash == header->hash)
6060
return true;
61-
if (config.ssaaTransparentObjects && header->hash == ShaderHash({ 0x0cd1b9e5, 0x22e7069e, 0x476455ff, 0x98bfd850 }))
62-
return true;
6361
return config.ssaaAll;
6462
}
6563

@@ -136,7 +134,21 @@ Buffer getReplacementShader(const void* data, SIZE_T length) {
136134
}
137135

138136
Buffer getAlphaToCoverageShader(const void* data, SIZE_T length) {
139-
return findReplacement(data, length, shaderReplacementsAlphaToCoverage);
137+
ShaderReplacementList list;
138+
if (config.msaaSamples < 2) {
139+
return {};
140+
} else if (!config.sampleRateAlpha) {
141+
list = shaderReplacementsAlphaToCoverage0;
142+
} else if (config.msaaSamples > 8) {
143+
list = shaderReplacementsAlphaToCoverage16;
144+
} else if (config.msaaSamples > 4) {
145+
list = shaderReplacementsAlphaToCoverage8;
146+
} else if (config.msaaSamples > 2) {
147+
list = shaderReplacementsAlphaToCoverage4;
148+
} else {
149+
list = shaderReplacementsAlphaToCoverage2;
150+
}
151+
return findReplacement(data, length, list);
140152
}
141153

142154
} // namespace atfix

Shaders.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ struct ShaderReplacementList {
2727
};
2828

2929
extern const ShaderReplacementList shaderReplacements;
30-
extern const ShaderReplacementList shaderReplacementsAlphaToCoverage;
30+
extern const ShaderReplacementList shaderReplacementsAlphaToCoverage0;
31+
extern const ShaderReplacementList shaderReplacementsAlphaToCoverage2;
32+
extern const ShaderReplacementList shaderReplacementsAlphaToCoverage4;
33+
extern const ShaderReplacementList shaderReplacementsAlphaToCoverage8;
34+
extern const ShaderReplacementList shaderReplacementsAlphaToCoverage16;
3135
}

build-shaders.ps1

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ $header += "namespace atfix {`n"
55
Set-Content -NoNewline -Path Shaders.cpp $header
66

77
$shaders = [System.Collections.ArrayList]@()
8-
$shadersA2C = [System.Collections.ArrayList]@()
8+
$shadersA2C = @{
9+
0 = [System.Collections.ArrayList]@();
10+
2 = [System.Collections.ArrayList]@();
11+
4 = [System.Collections.ArrayList]@();
12+
8 = [System.Collections.ArrayList]@();
13+
16 = [System.Collections.ArrayList]@();
14+
}
915

1016
function Add-Shader {
1117
[CmdletBinding()]
@@ -18,34 +24,38 @@ function Add-Shader {
1824
[string[]]$FxcArgs
1925
)
2026
$hash = $HashString.Split("-") | % { [System.Convert]::ToUInt32($_, 16) }
21-
$a2cstr = if ($AlphaToCoverage) { "_a2c" } else { "" }
22-
$hashDecl = "0x{0:x8}, 0x{1:x8}, 0x{2:x8}, 0x{3:x8}" -f $hash[0], $hash[1], $hash[2], $hash[3]
23-
$name = "shader${a2cstr}_{0:x8}_{1:x8}_{2:x8}_{3:x8}" -f $hash[0], $hash[1], $hash[2], $hash[3]
24-
$decl = "{{$hashDecl}, $name, sizeof($name)}"
25-
$path = "Shaders\$name.fxc"
27+
$variants = if ($AlphaToCoverage) { 0, 2, 4, 8, 16 } else { 0 }
28+
foreach ($variant in $variants) {
29+
$a2cstr = if ($AlphaToCoverage) { "_a2c$variant" } else { "" }
30+
$hashDecl = "0x{0:x8}, 0x{1:x8}, 0x{2:x8}, 0x{3:x8}" -f $hash[0], $hash[1], $hash[2], $hash[3]
31+
$name = "shader${a2cstr}_{0:x8}_{1:x8}_{2:x8}_{3:x8}" -f $hash[0], $hash[1], $hash[2], $hash[3]
32+
$decl = "{{$hashDecl}, $name, sizeof($name)}"
33+
$path = "Shaders\$name.fxc"
34+
$extra = if ($AlphaToCoverage) { "/DMSAA_SAMPLE_COUNT=$variant" } else { "" }
2635

27-
fxc /T ps_5_0 /Fo $path $FxcArgs $File
28-
$content = [System.Io.File]::ReadAllBytes($path)
29-
$array = "{"
36+
fxc /T ps_5_0 /Fo $path $FxcArgs $extra $File
37+
$content = [System.Io.File]::ReadAllBytes($path)
38+
$array = "{"
3039

31-
for ($i = 0; $i -lt $content.Length; $i += 4) {
32-
$num = [UInt32]$content[$i] -bor ([UInt32]$content[$i + 1] -shl 8) -bor ([UInt32]$content[$i + 2] -shl 16) -bor ([UInt32]$content[$i + 3] -shl 24)
33-
if ($i % 32 -eq 0) {
34-
$array += "`n "
35-
} else {
36-
$array += " "
40+
for ($i = 0; $i -lt $content.Length; $i += 4) {
41+
$num = [UInt32]$content[$i] -bor ([UInt32]$content[$i + 1] -shl 8) -bor ([UInt32]$content[$i + 2] -shl 16) -bor ([UInt32]$content[$i + 3] -shl 24)
42+
if ($i % 32 -eq 0) {
43+
$array += "`n "
44+
} else {
45+
$array += " "
46+
}
47+
$array += "0x{0:x8}," -f $num
3748
}
38-
$array += "0x{0:x8}," -f $num
39-
}
4049

41-
$array += "`n};"
50+
$array += "`n};"
4251

43-
Add-Content -NoNewline -Path Shaders.cpp "constexpr static DWORD $name[] = $array`n"
52+
Add-Content -NoNewline -Path Shaders.cpp "constexpr static DWORD $name[] = $array`n"
4453

45-
if ($AlphaToCoverage) {
46-
$shadersA2C.Add($decl)
47-
} else {
48-
$shaders.Add($decl)
54+
if ($AlphaToCoverage) {
55+
$shadersA2C[$variant].Add($decl)
56+
} else {
57+
$shaders.Add($decl)
58+
}
4959
}
5060
}
5161

@@ -79,6 +89,10 @@ function Add-ShaderList($name, $array) {
7989
Add-Content -NoNewline -Path Shaders.cpp "`n`n"
8090

8191
Add-ShaderList shaderReplacements $shaders
82-
Add-ShaderList shaderReplacementsAlphaToCoverage $shadersA2C
92+
Add-ShaderList shaderReplacementsAlphaToCoverage0 $shadersA2C[0]
93+
Add-ShaderList shaderReplacementsAlphaToCoverage2 $shadersA2C[2]
94+
Add-ShaderList shaderReplacementsAlphaToCoverage4 $shadersA2C[4]
95+
Add-ShaderList shaderReplacementsAlphaToCoverage8 $shadersA2C[8]
96+
Add-ShaderList shaderReplacementsAlphaToCoverage16 $shadersA2C[16]
8397

8498
Add-Content -NoNewline -Path Shaders.cpp "} // namespace atfix`n"

impl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,10 +891,14 @@ class DeviceWrapper final : public ID3D11Device, IDXGIDevice1 {
891891
bytecode = buffer.data;
892892
length = buffer.length;
893893
}
894-
if (config.msaaSamples > 1 && shouldUseSampleRate(pShaderBytecode, BytecodeLength))
894+
if (config.msaaSamples > 1 && shouldUseSampleRate(pShaderBytecode, BytecodeLength)) {
895895
converted = convertShaderToSampleRate(bytecode, length);
896+
} else if (config.msaaSamples > 1 && config.sampleRateAlpha && (buffer = getAlphaToCoverageShader(pShaderBytecode, BytecodeLength)).data) {
897+
bytecode = buffer.data;
898+
length = buffer.length;
899+
}
896900
HRESULT res = dev->CreatePixelShader(converted ? converted : bytecode, length, pClassLinkage, ppPixelShader);
897-
if (config.msaaSamples > 1 && !converted && SUCCEEDED(res) && (buffer = getAlphaToCoverageShader(pShaderBytecode, BytecodeLength)).data) {
901+
if (config.msaaSamples > 1 && !config.sampleRateAlpha && !converted && SUCCEEDED(res) && (buffer = getAlphaToCoverageShader(pShaderBytecode, BytecodeLength)).data) {
898902
ID3D11PixelShader* a2cShader;
899903
if (SUCCEEDED(dev->CreatePixelShader(buffer.data, buffer.length, pClassLinkage, &a2cShader))) {
900904
(*ppPixelShader)->SetPrivateDataInterface(IID_AlphaToCoverage, a2cShader);

impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extern struct Config {
1515
DWORD msaaSamples;
1616
DWORD anisotropy;
1717
bool ssaaCharacters;
18-
bool ssaaTransparentObjects;
18+
bool sampleRateAlpha;
1919
bool ssaaAll;
2020
bool allowShaderToggle;
2121
} config;

main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ D3D11Proc loadSystemD3D11() {
7272
"NumSamples = 8\n"
7373
"; Whether to do SSAA on characters (fairly cheap, improves thin lines on clothing)\n"
7474
"CharacterSSAA = 1\n"
75-
"; Whether to do SSAA on transparent objects like grass and tree leaves (somewhat expensive, but prevents them from shimmering when the camera moves)\n"
76-
"ObjectSSAA = 0\n"
75+
"; Whether to use sample rate calculations for alpha of transparent objects like grass and tree leaves (somewhat cheap, reduces shimmering when the camera moves)\n"
76+
"AlphaSSAA = 1\n"
7777
"; Apply SSAA to everything, because you have more GPU power than you know what to do with\n"
7878
"FullSSAA = 0\n"
7979
"[Other]\n"
@@ -89,26 +89,26 @@ D3D11Proc loadSystemD3D11() {
8989
} else {
9090
char NumSamples[8];
9191
char CharacterSSAA[8];
92-
char ObjectSSAA[8];
92+
char SampleRateAlpha[8];
9393
char FullSSAA[8];
9494
char AF[8];
9595
char UseShaderToggle[8];
9696
bool ok = true;
9797
GetPrivateProfileStringA("MSAA", "NumSamples", "8", NumSamples, sizeof(NumSamples), ".\\atfix.ini");
9898
GetPrivateProfileStringA("MSAA", "CharacterSSAA", "1", CharacterSSAA, sizeof(CharacterSSAA), ".\\atfix.ini");
99-
GetPrivateProfileStringA("MSAA", "ObjectSSAA", "0", ObjectSSAA, sizeof(ObjectSSAA), ".\\atfix.ini");
99+
GetPrivateProfileStringA("MSAA", "AlphaSSAA", "1", SampleRateAlpha, sizeof(SampleRateAlpha), ".\\atfix.ini");
100100
GetPrivateProfileStringA("MSAA", "FullSSAA", "0", FullSSAA, sizeof(FullSSAA), ".\\atfix.ini");
101101
GetPrivateProfileStringA("Other", "AF", "0", AF, sizeof(AF), ".\\atfix.ini");
102102
GetPrivateProfileStringA("Other", "EnhancementToggle", "0", UseShaderToggle, sizeof(UseShaderToggle), ".\\atfix.ini");
103103
config.msaaSamples = atoi(NumSamples);
104104
config.ssaaCharacters = atoi(CharacterSSAA);
105-
config.ssaaTransparentObjects = atoi(ObjectSSAA);
105+
config.sampleRateAlpha = atoi(SampleRateAlpha);
106106
config.ssaaAll = atoi(FullSSAA);
107107
config.anisotropy = atoi(AF);
108108
config.allowShaderToggle = atoi(UseShaderToggle);
109109
if (config.msaaSamples < 1)
110110
config.msaaSamples = 1;
111-
log("Loaded config, ", config.msaaSamples, " samples, ", config.ssaaCharacters, " characterSSAA, ", config.ssaaTransparentObjects, " objectSSAA, ", config.ssaaAll, " fullSSAA, ", config.anisotropy, " AF, ", config.allowShaderToggle, " enhancementToggle ");
111+
log("Loaded config, ", config.msaaSamples, " samples, ", config.ssaaCharacters, " characterSSAA, ", config.sampleRateAlpha, " sampleRateAlpha, ", config.ssaaAll, " fullSSAA, ", config.anisotropy, " AF, ", config.allowShaderToggle, " enhancementToggle ");
112112
}
113113

114114
d3d11Proc.D3D11CreateDevice = reinterpret_cast<PFN_D3D11CreateDevice>(

0 commit comments

Comments
 (0)