Debian Codegraph @

Source/Core/VideoCommon/PixelShaderManager.cpp

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/PixelShaderManager.h"
5
6 #include <iterator>
7
8 #include "Common/ChunkFile.h"
9 #include "Common/CommonTypes.h"
10 #include "VideoCommon/FramebufferManager.h"
11 #include "VideoCommon/VideoCommon.h"
12 #include "VideoCommon/VideoConfig.h"
13 #include "VideoCommon/XFMemory.h"
14
15 void PixelShaderManager::Init()
16 {
17 constants = {};
18
19 // Init any initial constants which aren't zero when bpmem is zero.
20 m_fog_range_adjusted_changed = true;
21 m_viewport_changed = false;
22
23 SetIndMatrixChanged(0);
24 SetIndMatrixChanged(1);
25 SetIndMatrixChanged(2);
26 SetZTextureTypeChanged();
27 SetTexCoordChanged(0);
28 SetTexCoordChanged(1);
29 SetTexCoordChanged(2);
30 SetTexCoordChanged(3);
31 SetTexCoordChanged(4);
32 SetTexCoordChanged(5);
33 SetTexCoordChanged(6);
34 SetTexCoordChanged(7);
35
36 // fixed Konstants
37 for (int component = 0; component < 4; component++)
38 {
39 constants.konst[0][component] = 255; // 1
40 constants.konst[1][component] = 223; // 7/8
41 constants.konst[2][component] = 191; // 3/4
42 constants.konst[3][component] = 159; // 5/8
43 constants.konst[4][component] = 128; // 1/2
44 constants.konst[5][component] = 96; // 3/8
45 constants.konst[6][component] = 64; // 1/4
46 constants.konst[7][component] = 32; // 1/8
47
48 // Invalid Konstants (reads as zero on hardware)
49 constants.konst[8][component] = 0;
50 constants.konst[9][component] = 0;
51 constants.konst[10][component] = 0;
52 constants.konst[11][component] = 0;
53
54 // Annoyingly, alpha reads zero values for the .rgb colors (officially
55 // defined as invalid)
56 // If it wasn't for this, we could just use one of the first 3 columns
57 // instead of
58 // wasting an entire 4th column just for alpha.
59 if (component == 3)
60 {
61 constants.konst[12][component] = 0;
62 constants.konst[13][component] = 0;
63 constants.konst[14][component] = 0;
64 constants.konst[15][component] = 0;
65 }
66 }
67
68 Dirty();
69 }
70
71 void PixelShaderManager::Dirty()
72 {
73 // This function is called after a savestate is loaded.
74 // Any constants that can changed based on settings should be re-calculated
75 m_fog_range_adjusted_changed = true;
76
77 SetEfbScaleChanged(g_framebuffer_manager->EFBToScaledXf(1),
78 g_framebuffer_manager->EFBToScaledYf(1));
79 SetFogParamChanged();
80
81 dirty = true;
82 }
83
84 void PixelShaderManager::SetConstants()
85 {
86 if (m_fog_range_adjusted_changed)
87 {
88 // set by two components, so keep changed flag here
89 // TODO: try to split both registers and move this logic to the shader
90 if (!g_ActiveConfig.bDisableFog && bpmem.fogRange.Base.Enabled == 1)
91 {
92 // bpmem.fogRange.Base.Center : center of the viewport in x axis. observation:
93 // bpmem.fogRange.Base.Center = realcenter + 342;
94 int center = ((u32)bpmem.fogRange.Base.Center) - 342;
95 // normalize center to make calculations easy
96 float ScreenSpaceCenter = center / (2.0f * xfmem.viewport.wd);
97 ScreenSpaceCenter = (ScreenSpaceCenter * 2.0f) - 1.0f;
98 // bpmem.fogRange.K seems to be a table of precalculated coefficients for the adjust factor
99 // observations: bpmem.fogRange.K[0].LO appears to be the lowest value and
100 // bpmem.fogRange.K[4].HI the largest
101 // they always seems to be larger than 256 so my theory is :
102 // they are the coefficients from the center to the border of the screen
103 // so to simplify I use the hi coefficient as K in the shader taking 256 as the scale
104 // TODO: Shouldn't this be EFBToScaledXf?
105 constants.fogf[2] = ScreenSpaceCenter;
106 constants.fogf[3] = static_cast<float>(
107 g_framebuffer_manager->EFBToScaledX(static_cast<int>(2.0f * xfmem.viewport.wd)));
108
109 for (size_t i = 0, vec_index = 0; i < std::size(bpmem.fogRange.K); i++)
110 {
111 constexpr float scale = 4.0f;
112 constants.fogrange[vec_index / 4][vec_index % 4] = bpmem.fogRange.K[i].GetValue(0) * scale;
113 vec_index++;
114 constants.fogrange[vec_index / 4][vec_index % 4] = bpmem.fogRange.K[i].GetValue(1) * scale;
115 vec_index++;
116 }
117 }
118 else
119 {
120 constants.fogf[2] = 0;
121 constants.fogf[3] = 1;
122 }
123 dirty = true;
124
125 m_fog_range_adjusted_changed = false;
126 }
127
128 if (m_viewport_changed)
129 {
130 constants.zbias[1][0] = (s32)xfmem.viewport.farZ;
131 constants.zbias[1][1] = (s32)xfmem.viewport.zRange;
132 dirty = true;
133 m_viewport_changed = false;
134 }
135
136 if (m_indirect_dirty)
137 {
138 for (int i = 0; i < 4; i++)
139 constants.pack1[i][3] = 0;
140
141 for (u32 i = 0; i < (bpmem.genMode.numtevstages + 1); ++i)
142 {
143 // Note: a tevind of zero just happens to be a passthrough, so no need
144 // to set an extra bit. Furthermore, wrap and add to previous apply even if there is no
145 // indirect stage.
146 constants.pack1[i][2] = bpmem.tevind[i].hex;
147
148 u32 stage = bpmem.tevind[i].bt;
149
150 // We use an extra bit (1 << 16) to provide a fast way of testing if this feature is in use.
151 // Note also that this is indexed by indirect stage, not by TEV stage.
152 if (bpmem.tevind[i].IsActive() && stage < bpmem.genMode.numindstages)
153 constants.pack1[stage][3] =
154 bpmem.tevindref.getTexCoord(stage) | bpmem.tevindref.getTexMap(stage) << 8 | 1 << 16;
155 }
156
157 dirty = true;
158 m_indirect_dirty = false;
159 }
160
161 if (m_dest_alpha_dirty)
162 {
163 // Destination alpha is only enabled if alpha writes are enabled. Force entire uniform to zero
164 // when disabled.
165 u32 dstalpha = bpmem.blendmode.alpha_update && bpmem.dstalpha.enable &&
166 bpmem.zcontrol.pixel_format == PixelFormat::RGBA6_Z24 ?
167 bpmem.dstalpha.hex :
168 0;
169
170 if (constants.dstalpha != dstalpha)
171 {
172 constants.dstalpha = dstalpha;
173 dirty = true;
174 }
175 }
176 }
177
178 void PixelShaderManager::SetTevColor(int index, int component, s32 value)
179 {
180 auto& c = constants.colors[index];
181 c[component] = value;
182 dirty = true;
183
184 PRIM_LOG("tev color{}: {} {} {} {}", index, c[0], c[1], c[2], c[3]);
185 }
186
187 void PixelShaderManager::SetTevKonstColor(int index, int component, s32 value)
188 {
189 auto& c = constants.kcolors[index];
190 c[component] = value;
191 dirty = true;
192
193 // Konst for ubershaders. We build the whole array on cpu so the gpu can do a single indirect
194 // access.
195 if (component != 3) // Alpha doesn't included in the .rgb konsts
196 constants.konst[index + 12][component] = value;
197
198 // .rrrr .gggg .bbbb .aaaa konsts
199 constants.konst[index + 16 + component * 4][0] = value;
200 constants.konst[index + 16 + component * 4][1] = value;
201 constants.konst[index + 16 + component * 4][2] = value;
202 constants.konst[index + 16 + component * 4][3] = value;
203
204 PRIM_LOG("tev konst color{}: {} {} {} {}", index, c[0], c[1], c[2], c[3]);
205 }
206
207 void PixelShaderManager::SetTevOrder(int index, u32 order)
208 {
209 if (constants.pack2[index][0] != order)
210 {
211 constants.pack2[index][0] = order;
212 dirty = true;
213 }
214 }
215
216 void PixelShaderManager::SetTevKSel(int index, u32 ksel)
217 {
218 if (constants.pack2[index][1] != ksel)
219 {
220 constants.pack2[index][1] = ksel;
221 dirty = true;
222 }
223 }
224
225 void PixelShaderManager::SetTevCombiner(int index, int alpha, u32 combiner)
226 {
227 if (constants.pack1[index][alpha] != combiner)
228 {
229 constants.pack1[index][alpha] = combiner;
230 dirty = true;
231 }
232 }
233
234 void PixelShaderManager::SetTevIndirectChanged()
235 {
236 m_indirect_dirty = true;
237 }
238
239 void PixelShaderManager::SetAlpha()
240 {
241 constants.alpha[0] = bpmem.alpha_test.ref0;
242 constants.alpha[1] = bpmem.alpha_test.ref1;
243 constants.alpha[3] = static_cast<s32>(bpmem.dstalpha.alpha);
244 dirty = true;
245 }
246
247 void PixelShaderManager::SetAlphaTestChanged()
248 {
249 // Force alphaTest Uniform to zero if it will always pass.
250 // (set an extra bit to distinguish from "never && never")
251 // TODO: we could optimize this further and check the actual constants,
252 // i.e. "a <= 0" and "a >= 255" will always pass.
253 u32 alpha_test =
254 bpmem.alpha_test.TestResult() != AlphaTestResult::Pass ? bpmem.alpha_test.hex | 1 << 31 : 0;
255 if (constants.alphaTest != alpha_test)
256 {
257 constants.alphaTest = alpha_test;
258 dirty = true;
259 }
260 }
261
262 void PixelShaderManager::SetDestAlphaChanged()
263 {
264 m_dest_alpha_dirty = true;
265 }
266
267 void PixelShaderManager::SetTexDims(int texmapid, u32 width, u32 height)
268 {
269 // TODO: move this check out to callee. There we could just call this function on texture changes
270 // or better, use textureSize() in glsl
271 if (constants.texdims[texmapid][0] != width || constants.texdims[texmapid][1] != height)
272 dirty = true;
273
274 constants.texdims[texmapid][0] = width;
275 constants.texdims[texmapid][1] = height;
276 }
277
278 void PixelShaderManager::SetSamplerState(int texmapid, u32 tm0, u32 tm1)
279 {
280 if (constants.pack2[texmapid][2] != tm0 || constants.pack2[texmapid][3] != tm1)
281 dirty = true;
282
283 constants.pack2[texmapid][2] = tm0;
284 constants.pack2[texmapid][3] = tm1;
285 }
286
287 void PixelShaderManager::SetZTextureBias()
288 {
289 constants.zbias[1][3] = bpmem.ztex1.bias;
290 dirty = true;
291 }
292
293 void PixelShaderManager::SetViewportChanged()
294 {
295 m_viewport_changed = true;
296 m_fog_range_adjusted_changed =
297 true; // TODO: Shouldn't be necessary with an accurate fog range adjust implementation
298 }
299
300 void PixelShaderManager::SetEfbScaleChanged(float scalex, float scaley)
301 {
302 constants.efbscale[0] = 1.0f / scalex;
303 constants.efbscale[1] = 1.0f / scaley;
304 dirty = true;
305 }
306
307 void PixelShaderManager::SetZSlope(float dfdx, float dfdy, float f0)
308 {
309 constants.zslope[0] = dfdx;
310 constants.zslope[1] = dfdy;
311 constants.zslope[2] = f0;
312 dirty = true;
313 }
314
315 void PixelShaderManager::SetIndTexScaleChanged(bool high)
316 {
317 constants.indtexscale[high][0] = bpmem.texscale[high].ss0;
318 constants.indtexscale[high][1] = bpmem.texscale[high].ts0;
319 constants.indtexscale[high][2] = bpmem.texscale[high].ss1;
320 constants.indtexscale[high][3] = bpmem.texscale[high].ts1;
321 dirty = true;
322 }
323
324 void PixelShaderManager::SetIndMatrixChanged(int matrixidx)
325 {
326 const u8 scale = bpmem.indmtx[matrixidx].GetScale();
327
328 // xyz - static matrix
329 // w - dynamic matrix scale / 128
330 constants.indtexmtx[2 * matrixidx][0] = bpmem.indmtx[matrixidx].col0.ma;
331 constants.indtexmtx[2 * matrixidx][1] = bpmem.indmtx[matrixidx].col1.mc;
332 constants.indtexmtx[2 * matrixidx][2] = bpmem.indmtx[matrixidx].col2.me;
333 constants.indtexmtx[2 * matrixidx][3] = 17 - scale;
334 constants.indtexmtx[2 * matrixidx + 1][0] = bpmem.indmtx[matrixidx].col0.mb;
335 constants.indtexmtx[2 * matrixidx + 1][1] = bpmem.indmtx[matrixidx].col1.md;
336 constants.indtexmtx[2 * matrixidx + 1][2] = bpmem.indmtx[matrixidx].col2.mf;
337 constants.indtexmtx[2 * matrixidx + 1][3] = 17 - scale;
338 dirty = true;
339
340 PRIM_LOG("indmtx{}: scale={}, mat=({} {} {}; {} {} {})", matrixidx, scale,
341 bpmem.indmtx[matrixidx].col0.ma, bpmem.indmtx[matrixidx].col1.mc,
342 bpmem.indmtx[matrixidx].col2.me, bpmem.indmtx[matrixidx].col0.mb,
343 bpmem.indmtx[matrixidx].col1.md, bpmem.indmtx[matrixidx].col2.mf);
344 }
345
346 void PixelShaderManager::SetZTextureTypeChanged()
347 {
348 switch (bpmem.ztex2.type)
349 {
350 case ZTexFormat::U8:
351 constants.zbias[0][0] = 0;
352 constants.zbias[0][1] = 0;
353 constants.zbias[0][2] = 0;
354 constants.zbias[0][3] = 1;
355 break;
356 case ZTexFormat::U16:
357 constants.zbias[0][0] = 1;
358 constants.zbias[0][1] = 0;
359 constants.zbias[0][2] = 0;
360 constants.zbias[0][3] = 256;
361 break;
362 case ZTexFormat::U24:
363 constants.zbias[0][0] = 65536;
364 constants.zbias[0][1] = 256;
365 constants.zbias[0][2] = 1;
366 constants.zbias[0][3] = 0;
367 break;
368 default:
369 PanicAlertFmt("Invalid ztex format {}", bpmem.ztex2.type);
370 break;
371 }
372 dirty = true;
373 }
374
375 void PixelShaderManager::SetZTextureOpChanged()
376 {
377 constants.ztex_op = bpmem.ztex2.op;
378 dirty = true;
379 }
380
381 void PixelShaderManager::SetTexCoordChanged(u8 texmapid)
382 {
383 TCoordInfo& tc = bpmem.texcoords[texmapid];
384 constants.texdims[texmapid][2] = tc.s.scale_minus_1 + 1;
385 constants.texdims[texmapid][3] = tc.t.scale_minus_1 + 1;
386 dirty = true;
387 }
388
389 void PixelShaderManager::SetFogColorChanged()
390 {
391 if (g_ActiveConfig.bDisableFog)
392 return;
393
394 constants.fogcolor[0] = bpmem.fog.color.r;
395 constants.fogcolor[1] = bpmem.fog.color.g;
396 constants.fogcolor[2] = bpmem.fog.color.b;
397 dirty = true;
398 }
399
400 void PixelShaderManager::SetFogParamChanged()
401 {
402 if (!g_ActiveConfig.bDisableFog)
403 {
404 constants.fogf[0] = bpmem.fog.GetA();
405 constants.fogf[1] = bpmem.fog.GetC();
406 constants.fogi[1] = bpmem.fog.b_magnitude;
407 constants.fogi[3] = bpmem.fog.b_shift;
408 constants.fogParam3 = bpmem.fog.c_proj_fsel.hex;
409 }
410 else
411 {
412 constants.fogf[0] = 0.f;
413 constants.fogf[1] = 0.f;
414 constants.fogi[1] = 1;
415 constants.fogi[3] = 1;
416 constants.fogParam3 = 0;
417 }
418 dirty = true;
419 }
420
421 void PixelShaderManager::SetFogRangeAdjustChanged()
422 {
423 if (g_ActiveConfig.bDisableFog)
424 return;
425
426 m_fog_range_adjusted_changed = true;
427
428 if (constants.fogRangeBase != bpmem.fogRange.Base.hex)
429 {
430 constants.fogRangeBase = bpmem.fogRange.Base.hex;
431 dirty = true;
432 }
433 }
434
435 void PixelShaderManager::SetGenModeChanged()
436 {
437 constants.genmode = bpmem.genMode.hex;
438 m_indirect_dirty = true;
439 dirty = true;
440 }
441
442 void PixelShaderManager::SetZModeControl()
443 {
444 u32 late_ztest = bpmem.GetEmulatedZ() == EmulatedZ::Late;
445 u32 rgba6_format =
446 (bpmem.zcontrol.pixel_format == PixelFormat::RGBA6_Z24 && !g_ActiveConfig.bForceTrueColor) ?
447 1 :
448 0;
449 u32 dither = rgba6_format && bpmem.blendmode.dither;
450 if (constants.late_ztest != late_ztest || constants.rgba6_format != rgba6_format ||
451 constants.dither != dither)
452 {
453 constants.late_ztest = late_ztest;
454 constants.rgba6_format = rgba6_format;
455 constants.dither = dither;
456 dirty = true;
457 }
458 m_dest_alpha_dirty = true;
459 }
460
461 void PixelShaderManager::SetBlendModeChanged()
462 {
463 u32 dither = constants.rgba6_format && bpmem.blendmode.dither;
464 if (constants.dither != dither)
465 {
466 constants.dither = dither;
467 dirty = true;
468 }
469 BlendingState state = {};
470 state.Generate(bpmem);
471 if (constants.blend_enable != state.blend_enable)
472 {
473 constants.blend_enable = state.blend_enable;
474 dirty = true;
475 }
476 if (constants.blend_src_factor != state.src_factor)
477 {
478 constants.blend_src_factor = state.src_factor;
479 dirty = true;
480 }
481 if (constants.blend_src_factor_alpha != state.src_factor_alpha)
482 {
483 constants.blend_src_factor_alpha = state.src_factor_alpha;
484 dirty = true;
485 }
486 if (constants.blend_dst_factor != state.dst_factor)
487 {
488 constants.blend_dst_factor = state.dst_factor;
489 dirty = true;
490 }
491 if (constants.blend_dst_factor_alpha != state.dst_factor_alpha)
492 {
493 constants.blend_dst_factor_alpha = state.dst_factor_alpha;
494 dirty = true;
495 }
496 if (constants.blend_subtract != state.subtract)
497 {
498 constants.blend_subtract = state.subtract;
499 dirty = true;
500 }
501 if (constants.blend_subtract_alpha != state.subtract_alpha)
502 {
503 constants.blend_subtract_alpha = state.subtract_alpha;
504 dirty = true;
505 }
506 if (constants.logic_op_enable != state.logic_op_enable)
507 {
508 constants.logic_op_enable = state.logic_op_enable;
509 dirty = true;
510 }
511 if (constants.logic_op_mode != state.logic_mode)
512 {
513 constants.logic_op_mode = state.logic_mode;
514 dirty = true;
515 }
516 m_dest_alpha_dirty = true;
517 }
518
519 void PixelShaderManager::SetBoundingBoxActive(bool active)
520 {
521 const bool enable = active && g_ActiveConfig.bBBoxEnable;
522 if (enable == (constants.bounding_box != 0))
523 return;
524
525 constants.bounding_box = active;
526 dirty = true;
527 }
528
529 void PixelShaderManager::DoState(PointerWrap& p)
530 {
531 p.Do(m_fog_range_adjusted_changed);
532 p.Do(m_viewport_changed);
533 p.Do(m_indirect_dirty);
534 p.Do(m_dest_alpha_dirty);
535
536 p.Do(constants);
537
538 if (p.IsReadMode())
539 {
540 // Fixup the current state from global GPU state
541 // NOTE: This requires that all GPU memory has been loaded already.
542 Dirty();
543 }
544 }