Debian Codegraph @

Source/Core/VideoCommon/ShaderGenCommon.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/ShaderGenCommon.h"
5
6 #include <fmt/format.h>
7
8 #include "Common/FileUtil.h"
9 #include "Core/ConfigManager.h"
10 #include "VideoCommon/VideoCommon.h"
11 #include "VideoCommon/VideoConfig.h"
12
13 ShaderHostConfig ShaderHostConfig::GetCurrent()
14 {
15 ShaderHostConfig bits = {};
16 bits.msaa = g_ActiveConfig.iMultisamples > 1;
17 bits.ssaa =
18 g_ActiveConfig.iMultisamples > 1 && g_ActiveConfig.bSSAA && g_backend_info.bSupportsSSAA;
19 bits.stereo = g_ActiveConfig.stereo_mode != StereoMode::Off;
20 bits.wireframe = g_ActiveConfig.bWireFrame;
21 bits.per_pixel_lighting = g_ActiveConfig.bEnablePixelLighting;
22 bits.vertex_rounding = g_ActiveConfig.UseVertexRounding();
23 bits.fast_depth_calc = g_ActiveConfig.bFastDepthCalc;
24 bits.bounding_box = g_ActiveConfig.bBBoxEnable;
25 bits.backend_dual_source_blend = g_backend_info.bSupportsDualSourceBlend;
26 bits.backend_geometry_shaders = g_backend_info.bSupportsGeometryShaders;
27 bits.backend_early_z = g_backend_info.bSupportsEarlyZ;
28 bits.backend_bbox = g_backend_info.bSupportsBBox;
29 bits.backend_gs_instancing = g_backend_info.bSupportsGSInstancing;
30 bits.backend_clip_control = g_backend_info.bSupportsClipControl;
31 bits.backend_ssaa = g_backend_info.bSupportsSSAA;
32 bits.backend_atomics = g_backend_info.bSupportsFragmentStoresAndAtomics;
33 bits.backend_depth_clamp = g_backend_info.bSupportsDepthClamp;
34 bits.backend_reversed_depth_range = g_backend_info.bSupportsReversedDepthRange;
35 bits.backend_bitfield = g_backend_info.bSupportsBitfield;
36 bits.backend_dynamic_sampler_indexing = g_backend_info.bSupportsDynamicSamplerIndexing;
37 bits.backend_shader_framebuffer_fetch = g_backend_info.bSupportsFramebufferFetch;
38 bits.backend_logic_op = g_backend_info.bSupportsLogicOp;
39 bits.backend_palette_conversion = g_backend_info.bSupportsPaletteConversion;
40 bits.enable_validation_layer = g_ActiveConfig.bEnableValidationLayer;
41 bits.manual_texture_sampling = !g_ActiveConfig.bFastTextureSampling;
42 bits.manual_texture_sampling_custom_texture_sizes =
43 g_ActiveConfig.ManualTextureSamplingWithCustomTextureSizes();
44 bits.backend_sampler_lod_bias = g_backend_info.bSupportsLodBiasInSampler;
45 bits.backend_dynamic_vertex_loader = g_backend_info.bSupportsDynamicVertexLoader;
46 bits.backend_vs_point_line_expand = g_ActiveConfig.UseVSForLinePointExpand();
47 bits.backend_gl_layer_in_fs = g_backend_info.bSupportsGLLayerInFS;
48 return bits;
49 }
50
51 std::string GetDiskShaderCacheFileName(APIType api_type, const char* type, bool include_gameid,
52 bool include_host_config, bool include_api)
53 {
54 if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
55 File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
56
57 std::string filename = File::GetUserPath(D_SHADERCACHE_IDX);
58 if (include_api)
59 {
60 switch (api_type)
61 {
62 case APIType::D3D:
63 filename += "D3D";
64 break;
65 case APIType::Metal:
66 filename += "Metal";
67 break;
68 case APIType::OpenGL:
69 filename += "OpenGL";
70 break;
71 case APIType::Vulkan:
72 filename += "Vulkan";
73 break;
74 default:
75 break;
76 }
77 filename += '-';
78 }
79
80 filename += type;
81
82 if (include_gameid)
83 {
84 filename += '-';
85 filename += SConfig::GetInstance().GetGameID();
86 }
87
88 if (include_host_config)
89 {
90 // We're using 21 bits, so 6 hex characters.
91 ShaderHostConfig host_config = ShaderHostConfig::GetCurrent();
92 filename += fmt::format("-{:06X}", host_config.bits);
93 }
94
95 filename += ".cache";
96 return filename;
97 }
98
99 void WriteIsNanHeader(ShaderCode& out, APIType api_type)
100 {
101 if (api_type == APIType::D3D)
102 {
103 out.Write("bool dolphin_isnan(float f) {{\n"
104 " // Workaround for the HLSL compiler deciding that isnan can never be true and\n"
105 " // optimising away the call, even though the value can actually be NaN\n"
106 " // Just look for the bit pattern that indicates NaN instead\n"
107 " return (floatBitsToInt(f) & 0x7FFFFFFF) > 0x7F800000;\n"
108 "}}\n\n");
109 // If isfinite is needed, (floatBitsToInt(f) & 0x7F800000) != 0x7F800000 can be used
110 }
111 else
112 {
113 out.Write("#define dolphin_isnan(f) isnan(f)\n");
114 }
115 }
116
117 void WriteBitfieldExtractHeader(ShaderCode& out, APIType api_type,
118 const ShaderHostConfig& host_config)
119 {
120 // ==============================================
121 // BitfieldExtract for APIs which don't have it
122 // ==============================================
123 if (!host_config.backend_bitfield)
124 {
125 out.Write("uint bitfieldExtract(uint val, int off, int size) {{\n"
126 " // This built-in function is only supported in OpenGL 4.0+ and ES 3.1+\n"
127 " // Microsoft's HLSL compiler automatically optimises this to a bitfield extract "
128 "instruction.\n"
129 " uint mask = uint((1 << size) - 1);\n"
130 " return uint(val >> off) & mask;\n"
131 "}}\n\n");
132 out.Write("int bitfieldExtract(int val, int off, int size) {{\n"
133 " // This built-in function is only supported in OpenGL 4.0+ and ES 3.1+\n"
134 " // Microsoft's HLSL compiler automatically optimises this to a bitfield extract "
135 "instruction.\n"
136 " return ((val << (32 - size - off)) >> (32 - size));\n"
137 "}}\n\n");
138 }
139 }
140
141 static void DefineOutputMember(ShaderCode& object, APIType api_type, std::string_view qualifier,
142 std::string_view type, std::string_view name, int var_index,
143 ShaderStage stage, std::string_view semantic = {},
144 int semantic_index = -1)
145 {
146 object.Write("\t{} {} {}", qualifier, type, name);
147
148 if (var_index != -1)
149 object.Write("{}", var_index);
150
151 if (api_type == APIType::D3D && !semantic.empty() && stage == ShaderStage::Geometry)
152 {
153 if (semantic_index != -1)
154 object.Write(" : {}{}", semantic, semantic_index);
155 else
156 object.Write(" : {}", semantic);
157 }
158
159 object.Write(";\n");
160 }
161
162 void GenerateVSOutputMembers(ShaderCode& object, APIType api_type, u32 texgens,
163 const ShaderHostConfig& host_config, std::string_view qualifier,
164 ShaderStage stage)
165 {
166 // SPIRV-Cross names all semantics as "TEXCOORD"
167 // Unfortunately Geometry shaders (which also uses this function)
168 // aren't supported. The output semantic name needs to match
169 // up with the input semantic name for both the next stage (pixel shader)
170 // and the previous stage (vertex shader), so
171 // we need to handle geometry in a special way...
172 if (api_type == APIType::D3D && stage == ShaderStage::Geometry)
173 {
174 DefineOutputMember(object, api_type, qualifier, "float4", "pos", -1, stage, "TEXCOORD", 0);
175 DefineOutputMember(object, api_type, qualifier, "float4", "colors_", 0, stage, "TEXCOORD", 1);
176 DefineOutputMember(object, api_type, qualifier, "float4", "colors_", 1, stage, "TEXCOORD", 2);
177
178 const unsigned int index_base = 3;
179 unsigned int index_offset = 0;
180 if (host_config.backend_geometry_shaders)
181 {
182 DefineOutputMember(object, api_type, qualifier, "float", "clipDist", 0, stage, "TEXCOORD",
183 index_base + index_offset);
184 DefineOutputMember(object, api_type, qualifier, "float", "clipDist", 1, stage, "TEXCOORD",
185 index_base + index_offset + 1);
186 index_offset += 2;
187 }
188
189 for (unsigned int i = 0; i < texgens; ++i)
190 {
191 DefineOutputMember(object, api_type, qualifier, "float3", "tex", i, stage, "TEXCOORD",
192 index_base + index_offset + i);
193 }
194 index_offset += texgens;
195
196 if (!host_config.fast_depth_calc)
197 {
198 DefineOutputMember(object, api_type, qualifier, "float4", "clipPos", -1, stage, "TEXCOORD",
199 index_base + index_offset);
200 index_offset++;
201 }
202
203 if (host_config.per_pixel_lighting)
204 {
205 DefineOutputMember(object, api_type, qualifier, "float3", "Normal", -1, stage, "TEXCOORD",
206 index_base + index_offset);
207 DefineOutputMember(object, api_type, qualifier, "float3", "WorldPos", -1, stage, "TEXCOORD",
208 index_base + index_offset + 1);
209 index_offset += 2;
210 }
211 }
212 else
213 {
214 DefineOutputMember(object, api_type, qualifier, "float4", "pos", -1, stage, "SV_Position");
215 DefineOutputMember(object, api_type, qualifier, "float4", "colors_", 0, stage, "COLOR", 0);
216 DefineOutputMember(object, api_type, qualifier, "float4", "colors_", 1, stage, "COLOR", 1);
217
218 if (host_config.backend_geometry_shaders)
219 {
220 DefineOutputMember(object, api_type, qualifier, "float", "clipDist", 0, stage,
221 "SV_ClipDistance", 0);
222 DefineOutputMember(object, api_type, qualifier, "float", "clipDist", 1, stage,
223 "SV_ClipDistance", 1);
224 }
225
226 for (unsigned int i = 0; i < texgens; ++i)
227 DefineOutputMember(object, api_type, qualifier, "float3", "tex", i, stage, "TEXCOORD", i);
228
229 if (!host_config.fast_depth_calc)
230 DefineOutputMember(object, api_type, qualifier, "float4", "clipPos", -1, stage, "TEXCOORD",
231 texgens);
232
233 if (host_config.per_pixel_lighting)
234 {
235 DefineOutputMember(object, api_type, qualifier, "float3", "Normal", -1, stage, "TEXCOORD",
236 texgens + 1);
237 DefineOutputMember(object, api_type, qualifier, "float3", "WorldPos", -1, stage, "TEXCOORD",
238 texgens + 2);
239 }
240 }
241 }
242
243 void AssignVSOutputMembers(ShaderCode& object, std::string_view a, std::string_view b, u32 texgens,
244 const ShaderHostConfig& host_config)
245 {
246 object.Write("\t{}.pos = {}.pos;\n", a, b);
247 object.Write("\t{}.colors_0 = {}.colors_0;\n", a, b);
248 object.Write("\t{}.colors_1 = {}.colors_1;\n", a, b);
249
250 for (unsigned int i = 0; i < texgens; ++i)
251 object.Write("\t{}.tex{} = {}.tex{};\n", a, i, b, i);
252
253 if (!host_config.fast_depth_calc)
254 object.Write("\t{}.clipPos = {}.clipPos;\n", a, b);
255
256 if (host_config.per_pixel_lighting)
257 {
258 object.Write("\t{}.Normal = {}.Normal;\n", a, b);
259 object.Write("\t{}.WorldPos = {}.WorldPos;\n", a, b);
260 }
261
262 if (host_config.backend_geometry_shaders)
263 {
264 object.Write("\t{}.clipDist0 = {}.clipDist0;\n", a, b);
265 object.Write("\t{}.clipDist1 = {}.clipDist1;\n", a, b);
266 }
267 }
268
269 void GenerateLineOffset(ShaderCode& object, std::string_view indent0, std::string_view indent1,
270 std::string_view pos_a, std::string_view pos_b, std::string_view sign)
271 {
272 // GameCube/Wii's line drawing algorithm is a little quirky. It does not
273 // use the correct line caps. Instead, the line caps are vertical or
274 // horizontal depending the slope of the line.
275 object.Write("{indent0}float2 offset;\n"
276 "{indent0}float2 to = abs({pos_a}.xy / {pos_a}.w - {pos_b}.xy / {pos_b}.w);\n"
277 // FIXME: What does real hardware do when line is at a 45-degree angle?
278 // FIXME: Lines aren't drawn at the correct width. See Twilight Princess map.
279 "{indent0}if (" I_LINEPTPARAMS ".y * to.y > " I_LINEPTPARAMS ".x * to.x) {{\n"
280 // Line is more tall. Extend geometry left and right.
281 // Lerp LineWidth/2 from [0..VpWidth] to [-1..1]
282 "{indent1}offset = float2({sign}" I_LINEPTPARAMS ".z / " I_LINEPTPARAMS ".x, 0);\n"
283 "{indent0}}} else {{\n"
284 // Line is more wide. Extend geometry up and down.
285 // Lerp LineWidth/2 from [0..VpHeight] to [1..-1]
286 "{indent1}offset = float2(0, {sign}-" I_LINEPTPARAMS ".z / " I_LINEPTPARAMS ".y);\n"
287 "{indent0}}}\n",
288 fmt::arg("indent0", indent0), fmt::arg("indent1", indent1), //
289 fmt::arg("pos_a", pos_a), fmt::arg("pos_b", pos_b), fmt::arg("sign", sign));
290 }
291
292 void GenerateVSLineExpansion(ShaderCode& object, std::string_view indent, u32 texgens)
293 {
294 std::string indent1 = std::string(indent) + " ";
295 object.Write("{0}other_pos = float4(dot(" I_PROJECTION "[0], other_pos), dot(" I_PROJECTION
296 "[1], other_pos), dot(" I_PROJECTION "[2], other_pos), dot(" I_PROJECTION
297 "[3], other_pos));\n"
298 "\n"
299 "{0}float expand_sign = is_right ? 1.0f : -1.0f;\n",
300 indent);
301 GenerateLineOffset(object, indent, indent1, "o.pos", "other_pos", "expand_sign * ");
302 object.Write("\n"
303 "{}o.pos.xy += offset * o.pos.w;\n",
304 indent);
305 if (texgens > 0)
306 {
307 object.Write("{}if ((" I_TEXOFFSET "[2] != 0) && is_right) {{\n", indent);
308 object.Write("{} float texOffset = 1.0 / float(" I_TEXOFFSET "[2]);\n", indent);
309 for (u32 i = 0; i < texgens; i++)
310 {
311 object.Write("{} if (((" I_TEXOFFSET "[0] >> {}) & 0x1) != 0)\n", indent, i);
312 object.Write("{} o.tex{}.x += texOffset;\n", indent, i);
313 }
314 object.Write("{}}}\n", indent);
315 }
316 }
317
318 void GenerateVSPointExpansion(ShaderCode& object, std::string_view indent, u32 texgens)
319 {
320 object.Write(
321 "{0}float2 expand_sign = float2(is_right ? 1.0f : -1.0f, is_bottom ? -1.0f : 1.0f);\n"
322 "{0}float2 offset = expand_sign * " I_LINEPTPARAMS ".ww / " I_LINEPTPARAMS ".xy;\n"
323 "{0}o.pos.xy += offset * o.pos.w;\n",
324 indent);
325 if (texgens > 0)
326 {
327 object.Write("{0}if (" I_TEXOFFSET "[3] != 0) {{\n"
328 "{0} float texOffsetMagnitude = 1.0f / float(" I_TEXOFFSET "[3]);\n"
329 "{0} float2 texOffset = float2(is_right ? texOffsetMagnitude : 0.0f, "
330 "is_bottom ? texOffsetMagnitude : 0.0f);",
331 indent);
332 for (u32 i = 0; i < texgens; i++)
333 {
334 object.Write("{} if (((" I_TEXOFFSET "[1] >> {}) & 0x1) != 0)\n", indent, i);
335 object.Write("{} o.tex{}.xy += texOffset;\n", indent, i);
336 }
337 object.Write("{}}}\n", indent);
338 }
339 }
340
341 const char* GetInterpolationQualifier(bool msaa, bool ssaa, bool in_glsl_interface_block, bool in)
342 {
343 if (!msaa)
344 return "";
345
346 // Without GL_ARB_shading_language_420pack support, the interpolation qualifier must be
347 // "centroid in" and not "centroid", even within an interface block.
348 if (in_glsl_interface_block && !g_backend_info.bSupportsBindingLayout)
349 {
350 if (!ssaa)
351 return in ? "centroid in" : "centroid out";
352 else
353 return in ? "sample in" : "sample out";
354 }
355 else
356 {
357 if (!ssaa)
358 return "centroid";
359 else
360 return "sample";
361 }
362 }