r600/sfn: rework getting a vector and uniforms from the value pool
[mesa.git] / src / gallium / drivers / r600 / sfn / sfn_shader_geometry.cpp
1 /* -*- mesa-c++ -*-
2 *
3 * Copyright (c) 2018 Collabora LTD
4 *
5 * Author: Gert Wollny <gert.wollny@collabora.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * on the rights to use, copy, modify, merge, publish, distribute, sub
11 * license, and/or sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 #include "sfn_shader_geometry.h"
28 #include "sfn_instruction_misc.h"
29 #include "sfn_instruction_fetch.h"
30 #include "sfn_shaderio.h"
31
32 namespace r600 {
33
34 GeometryShaderFromNir::GeometryShaderFromNir(r600_pipe_shader *sh,
35 r600_pipe_shader_selector &sel,
36 const r600_shader_key &key,
37 enum chip_class chip_class):
38 VertexStage(PIPE_SHADER_GEOMETRY, sel, sh->shader,
39 sh->scratch_space_needed, chip_class),
40 m_pipe_shader(sh),
41 m_so_info(&sel.so),
42 m_first_vertex_emitted(false),
43 m_offset(0),
44 m_next_input_ring_offset(0),
45 m_key(key),
46 m_num_clip_dist(0),
47 m_cur_ring_output(0),
48 m_gs_tri_strip_adj_fix(false)
49 {
50 sh_info().atomic_base = key.gs.first_atomic_counter;
51 }
52
53 bool GeometryShaderFromNir::do_emit_load_deref(UNUSED const nir_variable *in_var, UNUSED nir_intrinsic_instr* instr)
54 {
55 return false;
56 }
57
58 bool GeometryShaderFromNir::do_emit_store_deref(const nir_variable *out_var, nir_intrinsic_instr* instr)
59 {
60 uint32_t write_mask = nir_intrinsic_write_mask(instr);
61 GPRVector::Swizzle swz = swizzle_from_mask(write_mask);
62 auto out_value = vec_from_nir_with_fetch_constant(instr->src[1], write_mask, swz, true);
63
64 sh_info().output[out_var->data.driver_location].write_mask = write_mask;
65
66 auto ir = new MemRingOutIntruction(cf_mem_ring, mem_write_ind, out_value,
67 4 * out_var->data.driver_location,
68 instr->num_components, m_export_base);
69 emit_instruction(ir);
70
71 return true;
72 }
73
74 bool GeometryShaderFromNir::scan_sysvalue_access(UNUSED nir_instr *instr)
75 {
76 return true;
77 }
78
79 bool GeometryShaderFromNir::do_process_inputs(nir_variable *input)
80 {
81
82 if (input->data.location == VARYING_SLOT_POS ||
83 input->data.location == VARYING_SLOT_PSIZ ||
84 input->data.location == VARYING_SLOT_CLIP_VERTEX ||
85 input->data.location == VARYING_SLOT_CLIP_DIST0 ||
86 input->data.location == VARYING_SLOT_CLIP_DIST1 ||
87 input->data.location == VARYING_SLOT_COL0 ||
88 input->data.location == VARYING_SLOT_COL1 ||
89 (input->data.location >= VARYING_SLOT_VAR0 &&
90 input->data.location <= VARYING_SLOT_VAR31) ||
91 (input->data.location >= VARYING_SLOT_TEX0 &&
92 input->data.location <= VARYING_SLOT_TEX7)) {
93
94 r600_shader_io& io = sh_info().input[input->data.driver_location];
95 auto semantic = r600_get_varying_semantic(input->data.location);
96 io.name = semantic.first;
97 io.sid = semantic.second;
98
99 io.ring_offset = 16 * input->data.driver_location;
100 ++sh_info().ninput;
101 m_next_input_ring_offset += 16;
102 return true;
103 }
104
105 return false;
106 }
107
108 bool GeometryShaderFromNir::do_process_outputs(nir_variable *output)
109 {
110 if (output->data.location == VARYING_SLOT_COL0 ||
111 output->data.location == VARYING_SLOT_COL1 ||
112 (output->data.location >= VARYING_SLOT_VAR0 &&
113 output->data.location <= VARYING_SLOT_VAR31) ||
114 (output->data.location >= VARYING_SLOT_TEX0 &&
115 output->data.location <= VARYING_SLOT_TEX7) ||
116 output->data.location == VARYING_SLOT_BFC0 ||
117 output->data.location == VARYING_SLOT_BFC1 ||
118 output->data.location == VARYING_SLOT_CLIP_VERTEX ||
119 output->data.location == VARYING_SLOT_CLIP_DIST0 ||
120 output->data.location == VARYING_SLOT_CLIP_DIST1 ||
121 output->data.location == VARYING_SLOT_PRIMITIVE_ID ||
122 output->data.location == VARYING_SLOT_POS ||
123 output->data.location == VARYING_SLOT_PSIZ ||
124 output->data.location == VARYING_SLOT_LAYER ||
125 output->data.location == VARYING_SLOT_VIEWPORT ||
126 output->data.location == VARYING_SLOT_FOGC) {
127 r600_shader_io& io = sh_info().output[output->data.driver_location];
128
129 auto semantic = r600_get_varying_semantic(output->data.location);
130 io.name = semantic.first;
131 io.sid = semantic.second;
132
133 evaluate_spi_sid(io);
134 ++sh_info().noutput;
135
136 if (output->data.location == VARYING_SLOT_CLIP_DIST0 ||
137 output->data.location == VARYING_SLOT_CLIP_DIST1) {
138 m_num_clip_dist += 4;
139 }
140 return true;
141 }
142 return false;
143 }
144
145
146 bool GeometryShaderFromNir::allocate_reserved_registers()
147 {
148 const int sel[6] = {0, 0 ,0, 1, 1, 1};
149 const int chan[6] = {0, 1 ,3, 0, 1, 2};
150
151 increment_reserved_registers();
152 increment_reserved_registers();
153
154 /* Reserve registers used by the shaders (should check how many
155 * components are actually used */
156 for (int i = 0; i < 6; ++i) {
157 auto reg = new GPRValue(sel[i], chan[i]);
158 reg->set_as_input();
159 m_per_vertex_offsets[i].reset(reg);
160 inject_register(sel[i], chan[i], m_per_vertex_offsets[i], false);
161 }
162 auto reg = new GPRValue(0, 2);
163 reg->set_as_input();
164 m_primitive_id.reset(reg);
165 inject_register(0, 2, m_primitive_id, false);
166
167 reg = new GPRValue(1, 3);
168 reg->set_as_input();
169 m_invocation_id.reset(reg);
170 inject_register(1, 3, m_invocation_id, false);
171
172 m_export_base = get_temp_register();
173 emit_instruction(new AluInstruction(op1_mov, m_export_base, Value::zero, {alu_write, alu_last_instr}));
174
175 sh_info().ring_item_sizes[0] = m_next_input_ring_offset;
176
177 if (m_key.gs.tri_strip_adj_fix)
178 emit_adj_fix();
179
180 return true;
181 }
182
183 void GeometryShaderFromNir::emit_adj_fix()
184 {
185 PValue adjhelp0(new GPRValue(m_export_base->sel(), 1));
186 emit_instruction(op2_and_int, adjhelp0, {m_primitive_id, Value::one_i}, {alu_write, alu_last_instr});
187
188 int help2 = allocate_temp_register();
189 int reg_indices[6];
190 int reg_chanels[6] = {0, 1, 2, 3, 2, 3};
191
192 int rotate_indices[6] = {4, 5, 0, 1, 2, 3};
193
194 reg_indices[0] = reg_indices[1] = reg_indices[2] = reg_indices[3] = help2;
195 reg_indices[4] = reg_indices[5] = m_export_base->sel();
196
197 std::array<PValue, 6> adjhelp;
198
199 AluInstruction *ir = nullptr;
200 for (int i = 0; i < 6; i++) {
201 adjhelp[i].reset(new GPRValue(reg_indices[i], reg_chanels[i]));
202 ir = new AluInstruction(op3_cnde_int, adjhelp[i],
203 {adjhelp0, m_per_vertex_offsets[i],
204 m_per_vertex_offsets[rotate_indices[i]]},
205 {alu_write});
206 if (i == 3)
207 ir->set_flag(alu_last_instr);
208 emit_instruction(ir);
209 }
210 ir->set_flag(alu_last_instr);
211
212 for (int i = 0; i < 6; i++)
213 m_per_vertex_offsets[i] = adjhelp[i];
214 }
215
216 bool GeometryShaderFromNir::emit_deref_instruction_override(nir_deref_instr* instr)
217 {
218 if (instr->deref_type == nir_deref_type_array) {
219 auto var = get_deref_location(instr->parent);
220 ArrayDeref ad = {var, &instr->arr.index};
221 assert(instr->dest.is_ssa);
222 m_in_array_deref[instr->dest.ssa.index] = ad;
223
224 /* Problem: nir_intrinsice_load_deref tries to lookup the
225 * variable, and will not find it, need to override that too */
226 return true;
227 }
228 return false;
229 }
230
231 bool GeometryShaderFromNir::emit_intrinsic_instruction_override(nir_intrinsic_instr* instr)
232 {
233 switch (instr->intrinsic) {
234 case nir_intrinsic_load_deref: {
235 auto& src = instr->src[0];
236 assert(src.is_ssa);
237 auto array = m_in_array_deref.find(src.ssa->index);
238 if (array != m_in_array_deref.end())
239 return emit_load_from_array(instr, array->second);
240 } break;
241 case nir_intrinsic_emit_vertex:
242 return emit_vertex(instr, false);
243 case nir_intrinsic_end_primitive:
244 return emit_vertex(instr, true);
245 case nir_intrinsic_load_primitive_id:
246 return load_preloaded_value(instr->dest, 0, m_primitive_id);
247 case nir_intrinsic_load_invocation_id:
248 return load_preloaded_value(instr->dest, 0, m_invocation_id);
249 default:
250 ;
251 }
252 return false;
253 }
254
255 bool GeometryShaderFromNir::emit_vertex(nir_intrinsic_instr* instr, bool cut)
256 {
257 int stream = nir_intrinsic_stream_id(instr);
258 assert(stream < 4);
259
260 emit_instruction(new EmitVertex(stream, cut));
261
262 if (!cut)
263 emit_instruction(new AluInstruction(op2_add_int, m_export_base, m_export_base,
264 PValue(new LiteralValue(sh_info().noutput)),
265 {alu_write, alu_last_instr}));
266
267 return true;
268 }
269
270 bool GeometryShaderFromNir::emit_load_from_array(nir_intrinsic_instr* instr,
271 const ArrayDeref& array_deref)
272 {
273 auto dest = vec_from_nir(instr->dest, instr->num_components);
274
275 const nir_load_const_instr* literal_index = nullptr;
276
277 if (array_deref.index->is_ssa)
278 literal_index = get_literal_constant(array_deref.index->ssa->index);
279
280 if (!literal_index) {
281 sfn_log << SfnLog::err << "GS: Indirect input addressing not (yet) supported\n";
282 return false;
283 }
284 assert(literal_index->value[0].u32 < 6);
285 PValue addr = m_per_vertex_offsets[literal_index->value[0].u32];
286
287 auto fetch = new FetchInstruction(vc_fetch, no_index_offset, dest, addr,
288 16 * array_deref.var->data.driver_location,
289 R600_GS_RING_CONST_BUFFER, PValue(), bim_none, true);
290 emit_instruction(fetch);
291 return true;
292 }
293
294 void GeometryShaderFromNir::do_finalize()
295 {
296 if (m_num_clip_dist) {
297 sh_info().cc_dist_mask = (1 << m_num_clip_dist) - 1;
298 sh_info().clip_dist_write = (1 << m_num_clip_dist) - 1;
299 }
300 }
301
302 }