Debian Codegraph @

Source/Core/VideoCommon/GeometryShaderGen.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/GeometryShaderGen.h"
5
6 #include "Common/CommonTypes.h"
7 #include "Common/EnumMap.h"
8 #include "VideoCommon/DriverDetails.h"
9 #include "VideoCommon/LightingShaderGen.h"
10 #include "VideoCommon/VideoCommon.h"
11 #include "VideoCommon/VideoConfig.h"
12 #include "VideoCommon/XFMemory.h"
13
14 constexpr Common::EnumMap<const char*, PrimitiveType::TriangleStrip> primitives_ogl{
15 "points",
16 "lines",
17 "triangles",
18 "triangles",
19 };
20 constexpr Common::EnumMap<const char*, PrimitiveType::TriangleStrip> primitives_d3d{
21 "point",
22 "line",
23 "triangle",
24 "triangle",
25 };
26
27 constexpr Common::EnumMap<u32, PrimitiveType::TriangleStrip> vertex_in_map{1u, 2u, 3u, 3u};
28 constexpr Common::EnumMap<u32, PrimitiveType::TriangleStrip> vertex_out_map{4u, 4u, 4u, 3u};
29
30 bool geometry_shader_uid_data::IsPassthrough() const
31 {
32 const bool stereo = g_ActiveConfig.stereo_mode != StereoMode::Off;
33 const bool wireframe = g_ActiveConfig.bWireFrame;
34 return primitive_type >= static_cast<u32>(PrimitiveType::Triangles) && !stereo && !wireframe;
35 }
36
37 GeometryShaderUid GetGeometryShaderUid(PrimitiveType primitive_type)
38 {
39 GeometryShaderUid out;
40
41 geometry_shader_uid_data* const uid_data = out.GetUidData();
42 uid_data->primitive_type = static_cast<u32>(primitive_type);
43 uid_data->numTexGens = xfmem.numTexGen.numTexGens;
44
45 return out;
46 }
47
48 static void EmitVertex(ShaderCode& out, const ShaderHostConfig& host_config,
49 const geometry_shader_uid_data* uid_data, const char* vertex,
50 APIType api_type, bool wireframe, bool stereo, bool first_vertex = false);
51 static void EndPrimitive(ShaderCode& out, const ShaderHostConfig& host_config,
52 const geometry_shader_uid_data* uid_data, APIType api_type, bool wireframe,
53 bool stereo);
54
55 ShaderCode GenerateGeometryShaderCode(APIType api_type, const ShaderHostConfig& host_config,
56 const geometry_shader_uid_data* uid_data)
57 {
58 ShaderCode out;
59 // Non-uid template parameters will write to the dummy data (=> gets optimized out)
60
61 const bool wireframe = host_config.wireframe;
62 const bool msaa = host_config.msaa;
63 const bool ssaa = host_config.ssaa;
64 const bool stereo = host_config.stereo;
65 const auto primitive_type = static_cast<PrimitiveType>(uid_data->primitive_type);
66 const u32 vertex_in = vertex_in_map[primitive_type];
67 u32 vertex_out = vertex_out_map[primitive_type];
68
69 if (wireframe)
70 vertex_out++;
71
72 if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
73 {
74 // Insert layout parameters
75 if (host_config.backend_gs_instancing)
76 {
77 out.Write("layout({}, invocations = {}) in;\n", primitives_ogl[primitive_type],
78 stereo ? 2 : 1);
79 out.Write("layout({}_strip, max_vertices = {}) out;\n", wireframe ? "line" : "triangle",
80 vertex_out);
81 }
82 else
83 {
84 out.Write("layout({}) in;\n", primitives_ogl[primitive_type]);
85 out.Write("layout({}_strip, max_vertices = {}) out;\n", wireframe ? "line" : "triangle",
86 stereo ? vertex_out * 2 : vertex_out);
87 }
88 }
89
90 out.Write("{}", s_lighting_struct);
91
92 // uniforms
93 if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
94 out.Write("UBO_BINDING(std140, 4) uniform GSBlock {{\n");
95 else
96 out.Write("cbuffer GSBlock {{\n");
97
98 out.Write("{}", s_geometry_shader_uniforms);
99 out.Write("}};\n");
100
101 out.Write("struct VS_OUTPUT {{\n");
102 GenerateVSOutputMembers(out, api_type, uid_data->numTexGens, host_config, "",
103 ShaderStage::Geometry);
104 out.Write("}};\n");
105
106 if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
107 {
108 if (host_config.backend_gs_instancing)
109 out.Write("#define InstanceID gl_InvocationID\n");
110
111 out.Write("VARYING_LOCATION(0) in VertexData {{\n");
112 GenerateVSOutputMembers(out, api_type, uid_data->numTexGens, host_config,
113 GetInterpolationQualifier(msaa, ssaa, true, true),
114 ShaderStage::Geometry);
115 out.Write("}} vs[{}];\n", vertex_in);
116
117 out.Write("VARYING_LOCATION(0) out VertexData {{\n");
118 GenerateVSOutputMembers(out, api_type, uid_data->numTexGens, host_config,
119 GetInterpolationQualifier(msaa, ssaa, true, false),
120 ShaderStage::Geometry);
121
122 out.Write("}} ps;\n");
123 if (stereo && !host_config.backend_gl_layer_in_fs)
124 out.Write("flat out int layer;");
125
126 out.Write("void main()\n{{\n");
127 }
128 else // D3D
129 {
130 out.Write("struct VertexData {{\n");
131 out.Write("\tVS_OUTPUT o;\n");
132
133 if (stereo)
134 {
135 out.Write("\tuint layer : SV_RenderTargetArrayIndex;\n");
136 }
137 out.Write("\tfloat4 posout : SV_Position;\n");
138
139 out.Write("}};\n");
140
141 if (host_config.backend_gs_instancing)
142 {
143 out.Write("[maxvertexcount({})]\n[instance({})]\n", vertex_out, stereo ? 2 : 1);
144 out.Write("void main({} VS_OUTPUT o[{}], inout {}Stream<VertexData> output, in uint "
145 "InstanceID : SV_GSInstanceID)\n{{\n",
146 primitives_d3d[primitive_type], vertex_in, wireframe ? "Line" : "Triangle");
147 }
148 else
149 {
150 out.Write("[maxvertexcount({})]\n", stereo ? vertex_out * 2 : vertex_out);
151 out.Write("void main({} VS_OUTPUT o[{}], inout {}Stream<VertexData> output)\n{{\n",
152 primitives_d3d[primitive_type], vertex_in, wireframe ? "Line" : "Triangle");
153 }
154
155 out.Write("\tVertexData ps;\n");
156 }
157
158 if (primitive_type == PrimitiveType::Lines)
159 {
160 if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
161 {
162 out.Write("\tVS_OUTPUT start, end;\n");
163 AssignVSOutputMembers(out, "start", "vs[0]", uid_data->numTexGens, host_config);
164 AssignVSOutputMembers(out, "end", "vs[1]", uid_data->numTexGens, host_config);
165 }
166 else
167 {
168 out.Write("\tVS_OUTPUT start = o[0];\n"
169 "\tVS_OUTPUT end = o[1];\n");
170 }
171
172 GenerateLineOffset(out, "\t", "\t\t", "end.pos", "start.pos", "");
173 }
174 else if (primitive_type == PrimitiveType::Points)
175 {
176 if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
177 {
178 out.Write("\tVS_OUTPUT center;\n");
179 AssignVSOutputMembers(out, "center", "vs[0]", uid_data->numTexGens, host_config);
180 }
181 else
182 {
183 out.Write("\tVS_OUTPUT center = o[0];\n");
184 }
185
186 // Offset from center to upper right vertex
187 // Lerp PointSize/2 from [0,0..VpWidth,VpHeight] to [-1,1..1,-1]
188 out.Write("\tfloat2 offset = float2(" I_LINEPTPARAMS ".w / " I_LINEPTPARAMS
189 ".x, -" I_LINEPTPARAMS ".w / " I_LINEPTPARAMS ".y) * center.pos.w;\n");
190 }
191
192 if (stereo)
193 {
194 // If the GPU supports invocation we don't need a for loop and can simply use the
195 // invocation identifier to determine which layer we're rendering.
196 if (host_config.backend_gs_instancing)
197 out.Write("\tint eye = InstanceID;\n");
198 else
199 out.Write("\tfor (int eye = 0; eye < 2; ++eye) {{\n");
200 }
201
202 if (wireframe)
203 out.Write("\tVS_OUTPUT first;\n");
204
205 // Avoid D3D warning about forced unrolling of single-iteration loop
206 if (vertex_in > 1)
207 out.Write("\tfor (int i = 0; i < {}; ++i) {{\n", vertex_in);
208 else
209 out.Write("\tint i = 0;\n");
210
211 if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
212 {
213 out.Write("\tVS_OUTPUT f;\n");
214 AssignVSOutputMembers(out, "f", "vs[i]", uid_data->numTexGens, host_config);
215
216 if (host_config.backend_depth_clamp &&
217 DriverDetails::HasBug(DriverDetails::BUG_BROKEN_CLIP_DISTANCE))
218 {
219 // On certain GPUs we have to consume the clip distance from the vertex shader
220 // or else the other vertex shader outputs will get corrupted.
221 out.Write("\tf.clipDist0 = gl_in[i].gl_ClipDistance[0];\n"
222 "\tf.clipDist1 = gl_in[i].gl_ClipDistance[1];\n");
223 }
224 }
225 else
226 {
227 out.Write("\tVS_OUTPUT f = o[i];\n");
228 }
229
230 if (stereo)
231 {
232 // For stereoscopy add a small horizontal offset in Normalized Device Coordinates proportional
233 // to the depth of the vertex. We retrieve the depth value from the w-component of the projected
234 // vertex which contains the negated z-component of the original vertex.
235 // For negative parallax (out-of-screen effects) we subtract a convergence value from
236 // the depth value. This results in objects at a distance smaller than the convergence
237 // distance to seemingly appear in front of the screen.
238 // This formula is based on page 13 of the "Nvidia 3D Vision Automatic, Best Practices Guide"
239 out.Write("\tfloat hoffset = (eye == 0) ? " I_STEREOPARAMS ".x : " I_STEREOPARAMS ".y;\n");
240 out.Write("\tf.pos.x += hoffset * (f.pos.w - " I_STEREOPARAMS ".z);\n");
241 }
242
243 if (primitive_type == PrimitiveType::Lines)
244 {
245 out.Write("\tVS_OUTPUT l = f;\n"
246 "\tVS_OUTPUT r = f;\n");
247
248 out.Write("\tl.pos.xy -= offset * l.pos.w;\n"
249 "\tr.pos.xy += offset * r.pos.w;\n");
250
251 out.Write("\tif (" I_TEXOFFSET "[2] != 0) {{\n");
252 out.Write("\tfloat texOffset = 1.0 / float(" I_TEXOFFSET "[2]);\n");
253
254 for (u32 i = 0; i < uid_data->numTexGens; ++i)
255 {
256 out.Write("\tif (((" I_TEXOFFSET "[0] >> {}) & 0x1) != 0)\n", i);
257 out.Write("\t\tr.tex{}.x += texOffset;\n", i);
258 }
259 out.Write("\t}}\n");
260
261 EmitVertex(out, host_config, uid_data, "l", api_type, wireframe, stereo, true);
262 EmitVertex(out, host_config, uid_data, "r", api_type, wireframe, stereo);
263 }
264 else if (primitive_type == PrimitiveType::Points)
265 {
266 out.Write("\tVS_OUTPUT ll = f;\n"
267 "\tVS_OUTPUT lr = f;\n"
268 "\tVS_OUTPUT ul = f;\n"
269 "\tVS_OUTPUT ur = f;\n");
270
271 out.Write("\tll.pos.xy += float2(-1,-1) * offset;\n"
272 "\tlr.pos.xy += float2(1,-1) * offset;\n"
273 "\tul.pos.xy += float2(-1,1) * offset;\n"
274 "\tur.pos.xy += offset;\n");
275
276 out.Write("\tif (" I_TEXOFFSET "[3] != 0) {{\n");
277 out.Write("\tfloat2 texOffset = float2(1.0 / float(" I_TEXOFFSET
278 "[3]), 1.0 / float(" I_TEXOFFSET "[3]));\n");
279
280 for (u32 i = 0; i < uid_data->numTexGens; ++i)
281 {
282 out.Write("\tif (((" I_TEXOFFSET "[1] >> {}) & 0x1) != 0) {{\n", i);
283 out.Write("\t\tul.tex{}.xy += float2(0,1) * texOffset;\n", i);
284 out.Write("\t\tur.tex{}.xy += texOffset;\n", i);
285 out.Write("\t\tlr.tex{}.xy += float2(1,0) * texOffset;\n", i);
286 out.Write("\t}}\n");
287 }
288 out.Write("\t}}\n");
289
290 EmitVertex(out, host_config, uid_data, "ll", api_type, wireframe, stereo, true);
291 EmitVertex(out, host_config, uid_data, "lr", api_type, wireframe, stereo);
292 EmitVertex(out, host_config, uid_data, "ul", api_type, wireframe, stereo);
293 EmitVertex(out, host_config, uid_data, "ur", api_type, wireframe, stereo);
294 }
295 else
296 {
297 EmitVertex(out, host_config, uid_data, "f", api_type, wireframe, stereo, true);
298 }
299
300 // Only close loop if previous code was in one (See D3D warning above)
301 if (vertex_in > 1)
302 out.Write("\t}}\n");
303
304 EndPrimitive(out, host_config, uid_data, api_type, wireframe, stereo);
305
306 if (stereo && !host_config.backend_gs_instancing)
307 out.Write("\t}}\n");
308
309 out.Write("}}\n");
310
311 return out;
312 }
313
314 static void EmitVertex(ShaderCode& out, const ShaderHostConfig& host_config,
315 const geometry_shader_uid_data* uid_data, const char* vertex,
316 APIType api_type, bool wireframe, bool stereo, bool first_vertex)
317 {
318 if (wireframe && first_vertex)
319 out.Write("\tif (i == 0) first = {};\n", vertex);
320
321 if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
322 {
323 // Vulkan NDC space has Y pointing down (right-handed NDC space).
324 if (api_type == APIType::Vulkan)
325 out.Write("\tgl_Position = float4({0}.pos.x, -{0}.pos.y, {0}.pos.z, {0}.pos.w);\n", vertex);
326 else
327 out.Write("\tgl_Position = {}.pos;\n", vertex);
328
329 if (host_config.backend_depth_clamp)
330 {
331 out.Write("\tgl_ClipDistance[0] = {}.clipDist0;\n", vertex);
332 out.Write("\tgl_ClipDistance[1] = {}.clipDist1;\n", vertex);
333 }
334 AssignVSOutputMembers(out, "ps", vertex, uid_data->numTexGens, host_config);
335 }
336 else
337 {
338 out.Write("\tps.o = {};\n", vertex);
339 out.Write("\tps.posout = {}.pos;\n", vertex);
340 }
341
342 if (stereo)
343 {
344 // Select the output layer
345 if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
346 out.Write("\tgl_Layer = eye;\n");
347 else
348 {
349 out.Write("\tps.layer = eye;\n");
350 }
351 if (!host_config.backend_gl_layer_in_fs)
352 out.Write("\tlayer = eye;\n");
353 }
354
355 if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
356 out.Write("\tEmitVertex();\n");
357 else
358 out.Write("\toutput.Append(ps);\n");
359 }
360
361 static void EndPrimitive(ShaderCode& out, const ShaderHostConfig& host_config,
362 const geometry_shader_uid_data* uid_data, APIType api_type, bool wireframe,
363 bool stereo)
364 {
365 if (wireframe)
366 EmitVertex(out, host_config, uid_data, "first", api_type, wireframe, stereo);
367
368 if (api_type == APIType::OpenGL || api_type == APIType::Vulkan)
369 out.Write("\tEndPrimitive();\n");
370 else
371 out.Write("\toutput.RestartStrip();\n");
372 }
373
374 void EnumerateGeometryShaderUids(const std::function<void(const GeometryShaderUid&)>& callback)
375 {
376 GeometryShaderUid uid;
377
378 const std::array<PrimitiveType, 3> primitive_lut = {
379 {g_backend_info.bSupportsPrimitiveRestart ? PrimitiveType::TriangleStrip :
380 PrimitiveType::Triangles,
381 PrimitiveType::Lines, PrimitiveType::Points}};
382 for (PrimitiveType primitive : primitive_lut)
383 {
384 geometry_shader_uid_data* const guid = uid.GetUidData();
385 guid->primitive_type = static_cast<u32>(primitive);
386
387 for (u32 texgens = 0; texgens <= 8; texgens++)
388 {
389 guid->numTexGens = texgens;
390 callback(uid);
391 }
392 }
393 }