| 1 |
/* |
| 2 |
* Copyright 2023 Alyssa Rosenzweig |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "compiler/list.h" |
| 7 |
#include "compiler/nir/nir_builder.h" |
| 8 |
#include "agx_compiler.h" |
| 9 |
#include "nir.h" |
| 10 |
#include "nir_builder_opcodes.h" |
| 11 |
#include "nir_intrinsics.h" |
| 12 |
|
| 13 |
/* |
| 14 |
* sample_mask takes two bitmasks as arguments, TARGET and LIVE. Each bit refers |
| 15 |
* to an indexed sample. Roughly, the instruction does: |
| 16 |
* |
| 17 |
* foreach sample in TARGET { |
| 18 |
* if sample in LIVE { |
| 19 |
* run depth/stencil/occlusion test/update |
| 20 |
* } else { |
| 21 |
* kill sample |
| 22 |
* } |
| 23 |
* } |
| 24 |
* |
| 25 |
* As a special case, TARGET may be set to all-1s (~0) to refer to all samples |
| 26 |
* regardless of the framebuffer sample count. |
| 27 |
* |
| 28 |
* For example, to discard an entire pixel unconditionally, we could run: |
| 29 |
* |
| 30 |
* sample_mask ~0, 0 |
| 31 |
* |
| 32 |
* sample_mask must follow these rules: |
| 33 |
* |
| 34 |
* 1. All sample_mask instructions affecting a sample must execute before a |
| 35 |
* local_store_pixel instruction targeting that sample. This ensures that |
| 36 |
* nothing is written for discarded samples (whether discarded in shader or |
| 37 |
* due to a failed depth/stencil test). |
| 38 |
* |
| 39 |
* 2. If sample_mask is used anywhere in a shader, then on every execution path, |
| 40 |
* every sample must be killed or else run depth/stencil tests exactly ONCE. |
| 41 |
* |
| 42 |
* 3. If a sample is killed, future sample_mask instructions have |
| 43 |
* no effect on that sample. The following code sequence correctly implements |
| 44 |
* a conditional discard (if there are no other sample_mask instructions in |
| 45 |
* the shader): |
| 46 |
* |
| 47 |
* sample_mask discarded, 0 |
| 48 |
* sample_mask ~0, ~0 |
| 49 |
* |
| 50 |
* but this sequence is incorrect: |
| 51 |
* |
| 52 |
* sample_mask ~0, ~discarded |
| 53 |
* sample_mask ~0, ~0 <-- incorrect: depth/stencil tests run twice |
| 54 |
* |
| 55 |
* 4. Conversely, if a sample is tested, future sample_mask instructions may not |
| 56 |
* discard that sample. The following code is invalid: |
| 57 |
* |
| 58 |
* sample_mask ~0, ~0 |
| 59 |
* sample_mask discarded, 0 |
| 60 |
* st_tile |
| 61 |
* |
| 62 |
* To implement the semantic of "force early tests with discard", manual |
| 63 |
* colour masking must be used. It's a weird case, but CTS does it. |
| 64 |
* |
| 65 |
* 5. zs_emit may be used in the shader exactly once to trigger tests. |
| 66 |
* sample_mask with 0 may be used to discard early. |
| 67 |
* |
| 68 |
* This pass lowers demote_samples to sample_mask instructions satisfying these |
| 69 |
* rules. Other passes should not generate sample_mask instructions, as there |
| 70 |
* are too many footguns. |
| 71 |
*/ |
| 72 |
|
| 73 |
#define ALL_SAMPLES (0xFF) |
| 74 |
#define BASE_Z 1 |
| 75 |
#define BASE_S 2 |
| 76 |
|
| 77 |
static bool |
| 78 |
lower_discard_to_sample_mask_0(nir_builder *b, nir_intrinsic_instr *intr, |
| 79 |
UNUSED void *data) |
| 80 |
{ |
| 81 |
if (intr->intrinsic != nir_intrinsic_demote_samples) |
| 82 |
return false; |
| 83 |
|
| 84 |
b->cursor = nir_before_instr(&intr->instr); |
| 85 |
nir_sample_mask_agx(b, nir_u2u16(b, intr->src[0].ssa), |
| 86 |
nir_imm_intN_t(b, 0, 16)); |
| 87 |
nir_instr_remove(&intr->instr); |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
static nir_intrinsic_instr * |
| 92 |
last_discard_in_block(nir_block *block) |
| 93 |
{ |
| 94 |
nir_foreach_instr_reverse(instr, block) { |
| 95 |
if (instr->type != nir_instr_type_intrinsic) |
| 96 |
continue; |
| 97 |
|
| 98 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 99 |
if (intr->intrinsic == nir_intrinsic_demote_samples) |
| 100 |
return intr; |
| 101 |
} |
| 102 |
|
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
static bool |
| 107 |
cf_node_contains_discard(nir_cf_node *node) |
| 108 |
{ |
| 109 |
nir_foreach_block_in_cf_node(block, node) { |
| 110 |
if (last_discard_in_block(block)) |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
/* |
| 118 |
* We want to run depth/stencil tests as early as possible, but we have to |
| 119 |
* wait until after the last discard. We find the last discard and |
| 120 |
* execute depth/stencil tests in the first unconditional block after (if |
| 121 |
* in conditional control flow), or fuse depth/stencil tests into the |
| 122 |
* sample instruction (if in unconditional control flow). |
| 123 |
* |
| 124 |
* To do so, we walk the root control flow list backwards, looking for the |
| 125 |
* earliest unconditionally executed instruction after all discard. |
| 126 |
*/ |
| 127 |
static void |
| 128 |
run_tests_after_last_discard(nir_builder *b) |
| 129 |
{ |
| 130 |
foreach_list_typed_reverse(nir_cf_node, node, node, &b->impl->body) { |
| 131 |
if (node->type == nir_cf_node_block) { |
| 132 |
/* Unconditionally executed block */ |
| 133 |
nir_block *block = nir_cf_node_as_block(node); |
| 134 |
nir_intrinsic_instr *intr = last_discard_in_block(block); |
| 135 |
|
| 136 |
if (intr) { |
| 137 |
/* Last discard is executed unconditionally, so fuse tests: |
| 138 |
* |
| 139 |
* sample_mask (testing | killed), ~killed |
| 140 |
* |
| 141 |
* When testing, this is `sample_mask ~0, ~killed` which kills the |
| 142 |
* kill set and triggers tests on the rest. |
| 143 |
* |
| 144 |
* When not testing, this is `sample_mask killed, ~killed` which is |
| 145 |
* equivalent to `sample_mask killed, 0`, killing without testing. |
| 146 |
*/ |
| 147 |
b->cursor = nir_before_instr(&intr->instr); |
| 148 |
|
| 149 |
nir_def *all_samples = nir_imm_intN_t(b, ALL_SAMPLES, 16); |
| 150 |
nir_def *killed = nir_u2u16(b, intr->src[0].ssa); |
| 151 |
nir_def *live = nir_ixor(b, killed, all_samples); |
| 152 |
|
| 153 |
nir_def *testing = nir_load_shader_part_tests_zs_agx(b); |
| 154 |
nir_def *affected = nir_ior(b, testing, killed); |
| 155 |
|
| 156 |
nir_sample_mask_agx(b, affected, live); |
| 157 |
nir_instr_remove(&intr->instr); |
| 158 |
return; |
| 159 |
} else { |
| 160 |
/* Set cursor for insertion due to a preceding conditionally |
| 161 |
* executed discard. |
| 162 |
*/ |
| 163 |
b->cursor = nir_before_block_after_phis(block); |
| 164 |
} |
| 165 |
} else if (cf_node_contains_discard(node)) { |
| 166 |
/* Conditionally executed block contains the last discard. Test |
| 167 |
* depth/stencil for remaining samples in unconditional code after. |
| 168 |
* |
| 169 |
* If we're not testing, this turns into sample_mask(0, ~0) which is a |
| 170 |
* no-op. |
| 171 |
*/ |
| 172 |
nir_sample_mask_agx(b, nir_load_shader_part_tests_zs_agx(b), |
| 173 |
nir_imm_intN_t(b, ALL_SAMPLES, 16)); |
| 174 |
return; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
static void |
| 180 |
run_tests_at_start(nir_shader *shader) |
| 181 |
{ |
| 182 |
nir_function_impl *impl = nir_shader_get_entrypoint(shader); |
| 183 |
nir_builder b = nir_builder_at(nir_before_impl(impl)); |
| 184 |
|
| 185 |
nir_sample_mask_agx(&b, nir_imm_intN_t(&b, ALL_SAMPLES, 16), |
| 186 |
nir_imm_intN_t(&b, ALL_SAMPLES, 16)); |
| 187 |
} |
| 188 |
|
| 189 |
bool |
| 190 |
agx_nir_lower_sample_mask(nir_shader *shader) |
| 191 |
{ |
| 192 |
nir_function_impl *impl = nir_shader_get_entrypoint(shader); |
| 193 |
|
| 194 |
bool writes_zs = |
| 195 |
shader->info.outputs_written & |
| 196 |
(BITFIELD64_BIT(FRAG_RESULT_STENCIL) | BITFIELD64_BIT(FRAG_RESULT_DEPTH)); |
| 197 |
|
| 198 |
if (shader->info.fs.early_fragment_tests) { |
| 199 |
/* run tests early, if we need testing */ |
| 200 |
if (shader->info.fs.uses_discard || writes_zs || |
| 201 |
shader->info.writes_memory) { |
| 202 |
|
| 203 |
run_tests_at_start(shader); |
| 204 |
} |
| 205 |
} else if (shader->info.fs.uses_discard) { |
| 206 |
/* If we have zs_emit, the tests will be triggered by zs_emit, otherwise |
| 207 |
* we need to trigger tests explicitly. Allow sample_mask with zs_emit. |
| 208 |
*/ |
| 209 |
if (!writes_zs) { |
| 210 |
nir_builder b = nir_builder_create(impl); |
| 211 |
|
| 212 |
/* run tests late */ |
| 213 |
run_tests_after_last_discard(&b); |
| 214 |
} |
| 215 |
} else { |
| 216 |
/* regular shaders that don't use discard have nothing to lower */ |
| 217 |
return nir_no_progress(impl); |
| 218 |
} |
| 219 |
|
| 220 |
nir_progress(true, impl, nir_metadata_control_flow); |
| 221 |
|
| 222 |
nir_shader_intrinsics_pass(shader, lower_discard_to_sample_mask_0, |
| 223 |
nir_metadata_control_flow, NULL); |
| 224 |
|
| 225 |
return true; |
| 226 |
} |