Debian Codegraph @

src/panfrost/compiler/midgard/midgard_nir_lower_writeout.c

raw
1 /*
2 * Copyright (C) 2018-2020 Collabora, Ltd.
3 * Copyright (C) 2019-2020 Icecream95
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "compiler.h"
8 #include "midgard_nir.h"
9 #include "nir_builder.h"
10
11 /* Midgard can write all of color, depth and stencil in a single writeout
12 * operation, so we merge depth/stencil stores with color stores.
13 * If there are no color stores, we add a write to the "depth RT".
14 */
15
16 /*
17 * Get the type to report for a piece of a combined store, given the store it
18 * is combining from. If there is no store to render target #0, a dummy <0.0,
19 * 0.0, 0.0, 0.0> write is used, so report a matching float32 type.
20 */
21 static nir_alu_type
22 pan_nir_rt_store_type(nir_intrinsic_instr *store)
23 {
24 return store ? nir_intrinsic_src_type(store) : nir_type_float32;
25 }
26
27 static void
28 pan_nir_emit_combined_store(nir_builder *b, nir_intrinsic_instr *rt0_store,
29 unsigned writeout, nir_intrinsic_instr **stores)
30 {
31 nir_intrinsic_instr *intr = nir_intrinsic_instr_create(
32 b->shader, nir_intrinsic_store_combined_output_pan);
33
34 intr->num_components = rt0_store ? rt0_store->src[0].ssa->num_components : 4;
35
36 if (rt0_store)
37 nir_intrinsic_set_io_semantics(intr,
38 nir_intrinsic_io_semantics(rt0_store));
39 nir_intrinsic_set_src_type(intr, pan_nir_rt_store_type(rt0_store));
40 nir_intrinsic_set_dest_type(intr, pan_nir_rt_store_type(stores[2]));
41 nir_intrinsic_set_component(intr, writeout);
42
43 nir_def *zero = nir_imm_int(b, 0);
44 nir_def *zero4 = nir_imm_ivec4(b, 0, 0, 0, 0);
45
46 nir_def *src[] = {
47 rt0_store ? rt0_store->src[0].ssa : zero4,
48 rt0_store ? rt0_store->src[1].ssa : zero,
49 stores[0] ? stores[0]->src[0].ssa : zero,
50 stores[1] ? stores[1]->src[0].ssa : zero,
51 stores[2] ? stores[2]->src[0].ssa : zero4,
52 };
53
54 for (int i = 0; i < ARRAY_SIZE(src); ++i)
55 intr->src[i] = nir_src_for_ssa(src[i]);
56
57 nir_builder_instr_insert(b, &intr->instr);
58 }
59
60 static bool
61 kill_depth_stencil_writes(nir_builder *b, nir_intrinsic_instr *intr,
62 UNUSED void *data)
63 {
64 if (intr->intrinsic != nir_intrinsic_store_output)
65 return false;
66
67 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
68 if (sem.location != FRAG_RESULT_DEPTH && sem.location != FRAG_RESULT_STENCIL)
69 return false;
70
71 nir_instr_remove(&intr->instr);
72 return true;
73 }
74
75 bool
76 midgard_nir_lower_zs_store(nir_shader *nir)
77 {
78 bool progress = false;
79
80 if (nir->info.stage != MESA_SHADER_FRAGMENT)
81 return false;
82
83 /* Remove all stencil/depth writes if early fragment test is forced. */
84 if (nir->info.fs.early_fragment_tests)
85 progress |= nir_shader_intrinsics_pass(nir, kill_depth_stencil_writes,
86 nir_metadata_control_flow, NULL);
87
88 nir_foreach_function_impl(impl, nir) {
89 nir_intrinsic_instr *stores[3] = {NULL};
90 nir_intrinsic_instr *last_mask_store = NULL;
91 nir_block *mask_block = NULL;
92 unsigned writeout = 0;
93
94 nir_foreach_block(block, impl) {
95 nir_foreach_instr_safe(instr, block) {
96 if (instr->type != nir_instr_type_intrinsic)
97 continue;
98
99 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
100 if (intr->intrinsic != nir_intrinsic_store_output)
101 continue;
102
103 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
104 if (sem.location == FRAG_RESULT_DEPTH) {
105 stores[0] = intr;
106 writeout |= MIR_WRITEOUT_Z;
107 } else if (sem.location == FRAG_RESULT_STENCIL) {
108 stores[1] = intr;
109 writeout |= MIR_WRITEOUT_S;
110 } else if (sem.dual_source_blend_index) {
111 assert(!stores[2]); /* there should be only 1 source for dual blending */
112 stores[2] = intr;
113 writeout |= MIR_WRITEOUT_2;
114 } else if (sem.location == FRAG_RESULT_SAMPLE_MASK) {
115 last_mask_store = intr;
116 mask_block = intr->instr.block;
117 }
118 }
119 }
120
121 if (!writeout && !last_mask_store)
122 continue;
123
124 nir_block *common_block = mask_block;
125
126 /* Ensure all stores are in the same block */
127 for (unsigned i = 0; i < ARRAY_SIZE(stores); ++i) {
128 if (!stores[i])
129 continue;
130
131 nir_block *block = stores[i]->instr.block;
132
133 if (common_block)
134 assert(common_block == block);
135 else
136 common_block = block;
137 }
138
139 /* move data stores in the common block to after the last mask store */
140 if (last_mask_store) {
141 nir_cursor insert_point = nir_after_instr(&last_mask_store->instr);
142 nir_foreach_instr_safe(instr, mask_block) {
143 if (instr->type != nir_instr_type_intrinsic)
144 continue;
145 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
146
147 /* stop when we've reached the last store to mask */
148 if (intr == last_mask_store)
149 break;
150 if (intr->intrinsic != nir_intrinsic_store_output)
151 continue;
152 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
153 if (sem.location >= FRAG_RESULT_DATA0 &&
154 sem.location <= FRAG_RESULT_DATA7) {
155 nir_instr_move(insert_point, instr);
156 insert_point = nir_after_instr(instr);
157 }
158 }
159 }
160
161 bool replaced = false;
162
163 nir_foreach_block(block, impl) {
164 nir_foreach_instr_safe(instr, block) {
165 if (instr->type != nir_instr_type_intrinsic)
166 continue;
167
168 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
169 if (intr->intrinsic != nir_intrinsic_store_output)
170 continue;
171
172 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
173
174 if (sem.location < FRAG_RESULT_DATA0)
175 continue;
176
177 if (sem.dual_source_blend_index)
178 continue;
179
180 assert(nir_src_is_const(intr->src[1]) && "no indirect outputs");
181
182 nir_builder b =
183 nir_builder_at(nir_after_block_before_jump(instr->block));
184
185 /* Trying to write depth twice results in the
186 * wrong blend shader being executed on
187 * Midgard */
188 unsigned this_store = MIR_WRITEOUT_C | (replaced ? 0 : writeout);
189
190 pan_nir_emit_combined_store(&b, intr, this_store, stores);
191
192 nir_instr_remove(instr);
193
194 replaced = true;
195 }
196 }
197
198 /* Insert a store to the depth RT (0xff) if needed */
199 if (!replaced) {
200 nir_builder b =
201 nir_builder_at(nir_after_block_before_jump(common_block));
202
203 pan_nir_emit_combined_store(&b, NULL, writeout, stores);
204 }
205
206 for (unsigned i = 0; i < ARRAY_SIZE(stores); ++i) {
207 if (stores[i])
208 nir_instr_remove(&stores[i]->instr);
209 }
210
211 progress = nir_progress(true, impl, nir_metadata_control_flow);
212 }
213
214 return progress;
215 }