| 1 |
/* |
| 2 |
* Copyright (C) 2022 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "genxml/gen_macros.h" |
| 7 |
|
| 8 |
#include "pan_earlyzs.h" |
| 9 |
#include "panfrost/compiler/pan_compiler.h" |
| 10 |
|
| 11 |
|
| 12 |
/* |
| 13 |
* Return an "early" mode. If it is known that the depth/stencil tests always |
| 14 |
* pass (so the shader is always executed), weak early is usually faster than |
| 15 |
* force early. |
| 16 |
*/ |
| 17 |
static enum mali_pixel_kill |
| 18 |
best_early_mode(bool zs_always_passes, bool force_early) |
| 19 |
{ |
| 20 |
if (zs_always_passes && !force_early) |
| 21 |
return MALI_PIXEL_KILL_WEAK_EARLY; |
| 22 |
else |
| 23 |
return MALI_PIXEL_KILL_FORCE_EARLY; |
| 24 |
} |
| 25 |
|
| 26 |
/* |
| 27 |
* Analyze a fragment shader and provided API state to determine the early-ZS |
| 28 |
* configuration. The order of arguments must match the order of states in the |
| 29 |
* lookup table, synchronized with pan_earlyzs_get. |
| 30 |
*/ |
| 31 |
static struct pan_earlyzs_state |
| 32 |
analyze(const struct pan_shader_info *s, bool writes_zs_or_oq, |
| 33 |
bool alpha_to_coverage, bool zs_always_passes, |
| 34 |
enum pan_earlyzs_zs_tilebuf_read zs_read, |
| 35 |
bool should_force_early_update) |
| 36 |
{ |
| 37 |
/* If the shader writes depth or stencil, all depth/stencil tests must |
| 38 |
* be deferred until the value is known after the ZS_EMIT instruction, |
| 39 |
* if present. ZS_EMIT must precede ATEST, so the value is known when |
| 40 |
* ATEST executes, justifying the late test/update. |
| 41 |
* Also, if alpha_to_coverage is set that also forces a late update. |
| 42 |
* NOTE: it's not at all clear why alpha_to_coverage always requires |
| 43 |
* a late update; the late update should only really be required if |
| 44 |
* we're writing z or stencil, or testing for occlusion queries. |
| 45 |
* The docs are somewhat contradictory on this point. |
| 46 |
* But empirically we observe this requirement on Valhall, and doing |
| 47 |
* the update later is never wrong (just potentially a bit slower). |
| 48 |
*/ |
| 49 |
bool shader_writes_zs = (s->fs.writes_depth || s->fs.writes_stencil); |
| 50 |
bool late_update = shader_writes_zs || alpha_to_coverage; |
| 51 |
bool late_kill = shader_writes_zs; |
| 52 |
bool force_early_update = |
| 53 |
s->fs.early_fragment_tests || should_force_early_update; |
| 54 |
bool force_early_kill = s->fs.early_fragment_tests; |
| 55 |
|
| 56 |
/* Late coverage updates are required if the coverage mask depends on |
| 57 |
* the results of the shader. Discards are implemented as coverage mask |
| 58 |
* updates and must be considered. Strictly, depth/stencil writes may |
| 59 |
* also update the coverage mask, but these already force late updates. |
| 60 |
*/ |
| 61 |
bool late_coverage = |
| 62 |
s->fs.writes_coverage || s->fs.can_discard || alpha_to_coverage; |
| 63 |
|
| 64 |
/* Late coverage mask updates may affect the value written to the |
| 65 |
* depth/stencil buffer (if a pixel is discarded entirely). However, |
| 66 |
* they do not affect depth/stencil testing. So they may only matter if |
| 67 |
* depth or stencil is written. |
| 68 |
* |
| 69 |
* That dependency does mean late coverage mask updates require late |
| 70 |
* depth/stencil updates. |
| 71 |
* |
| 72 |
* Similarly, occlusion queries count samples that pass the |
| 73 |
* depth/stencil tests, so occlusion queries with late coverage also |
| 74 |
* require a late update. |
| 75 |
*/ |
| 76 |
late_update |= (late_coverage && writes_zs_or_oq); |
| 77 |
|
| 78 |
/* Side effects require late depth/stencil tests to ensure the shader |
| 79 |
* isn't killed before the side effects execute. |
| 80 |
*/ |
| 81 |
late_kill |= s->writes_global; |
| 82 |
|
| 83 |
/* Shader reads require late depth/stencil tests to ensure the shader |
| 84 |
* isn't killed before the side effects execute, unless the HW supports |
| 85 |
* read-only ZS optimization, in which case it can be lowered to |
| 86 |
* force-early. */ |
| 87 |
bool optimize_shader_read_only_zs = false; |
| 88 |
if (zs_read != PAN_EARLYZS_ZS_TILEBUF_NOT_READ) { |
| 89 |
if (!late_update && zs_read == PAN_EARLYZS_ZS_TILEBUF_READ_OPT) { |
| 90 |
optimize_shader_read_only_zs = true; |
| 91 |
force_early_update |= true; |
| 92 |
} else { |
| 93 |
late_update |= true; |
| 94 |
} |
| 95 |
|
| 96 |
if (!late_kill && zs_read == PAN_EARLYZS_ZS_TILEBUF_READ_OPT) { |
| 97 |
optimize_shader_read_only_zs = true; |
| 98 |
force_early_kill |= true; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/* Finally, the shader may override and force early fragment tests */ |
| 103 |
late_update &= !s->fs.early_fragment_tests; |
| 104 |
late_kill &= !s->fs.early_fragment_tests; |
| 105 |
|
| 106 |
/* Collect results */ |
| 107 |
return (struct pan_earlyzs_state){ |
| 108 |
.update = late_update |
| 109 |
? MALI_PIXEL_KILL_FORCE_LATE |
| 110 |
: best_early_mode(zs_always_passes, force_early_update), |
| 111 |
.kill = late_kill ? MALI_PIXEL_KILL_FORCE_LATE |
| 112 |
: best_early_mode(zs_always_passes, force_early_kill), |
| 113 |
.shader_readonly_zs = optimize_shader_read_only_zs, |
| 114 |
}; |
| 115 |
} |
| 116 |
|
| 117 |
/* |
| 118 |
* Analyze a fragment shader to determine all possible early-ZS configurations. |
| 119 |
* Returns a lookup table of configurations indexed by the API state. |
| 120 |
*/ |
| 121 |
struct pan_earlyzs_lut |
| 122 |
pan_earlyzs_analyze(const struct pan_shader_info *s, unsigned arch) |
| 123 |
{ |
| 124 |
/* On v11+, update operation cannot be weak early */ |
| 125 |
bool should_force_early_update = arch >= 11; |
| 126 |
|
| 127 |
struct pan_earlyzs_lut lut; |
| 128 |
|
| 129 |
for (unsigned v0 = 0; v0 < 2; ++v0) { |
| 130 |
for (unsigned v1 = 0; v1 < 2; ++v1) { |
| 131 |
for (unsigned v2 = 0; v2 < 2; ++v2) { |
| 132 |
for (unsigned v3 = 0; v3 < PAN_EARLYZS_ZS_TILEBUF_MODE_COUNT; |
| 133 |
++v3) { |
| 134 |
enum pan_earlyzs_zs_tilebuf_read zs_read = v3; |
| 135 |
|
| 136 |
/* Shader read-only ZS optimization only exists on v10. */ |
| 137 |
if (arch != 10 && v3 == PAN_EARLYZS_ZS_TILEBUF_READ_OPT) |
| 138 |
zs_read = PAN_EARLYZS_ZS_TILEBUF_READ_NO_OPT; |
| 139 |
|
| 140 |
lut.states[v0][v1][v2][v3] = |
| 141 |
analyze(s, v0, v1, v2, zs_read, should_force_early_update); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return lut; |
| 148 |
} |