r600/sfn: Add support for geometry shader
[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 "tgsi/tgsi_from_mesa.h"
28 #include "sfn_shader_geometry.h"
29 #include "sfn_instruction_misc.h"
30 #include "sfn_instruction_fetch.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 ShaderFromNirProcessor (PIPE_SHADER_GEOMETRY, sel, sh->shader,
38 sh->scratch_space_needed),
39 m_pipe_shader(sh),
40 m_so_info(&sel.so),
41 m_first_vertex_emitted(false),
42 m_offset(0),
43 m_next_input_ring_offset(0),
44 m_key(key),
45 m_num_clip_dist(0),
46 m_cur_ring_output(0),
47 m_gs_tri_strip_adj_fix(false)
48 {
49 sh_info().atomic_base = key.gs.first_atomic_counter;
50 }
51
52 bool GeometryShaderFromNir::do_emit_load_deref(UNUSED const nir_variable *in_var, UNUSED nir_intrinsic_instr* instr)
53 {
54 return false;
55 }
56
57 bool GeometryShaderFromNir::do_emit_store_deref(const nir_variable *out_var, nir_intrinsic_instr* instr)
58 {
59 uint32_t write_mask = (1 << instr->num_components) - 1;
60 GPRVector::Swizzle swz = swizzle_from_mask(instr->num_components);
61 std::unique_ptr<GPRVector> vec(vec_from_nir_with_fetch_constant(instr->src[1], write_mask, swz));
62
63 GPRVector out_value = *vec;
64
65 sh_info().output[out_var->data.driver_location].write_mask =
66 (1 << instr->num_components) - 1;
67
68 auto ir = new MemRingOutIntruction(cf_mem_ring, mem_write_ind, out_value,
69 4 * out_var->data.driver_location,
70 4, m_export_base);
71 emit_instruction(ir);
72
73 return true;
74 }
75
76 bool GeometryShaderFromNir::scan_sysvalue_access(UNUSED nir_instr *instr)
77 {
78 return true;
79 }
80
81 bool GeometryShaderFromNir::do_process_inputs(nir_variable *input)
82 {
83
84 if (input->data.location == VARYING_SLOT_POS ||
85 input->data.location == VARYING_SLOT_PSIZ ||
86 input->data.location == VARYING_SLOT_CLIP_DIST0 ||
87 input->data.location == VARYING_SLOT_CLIP_DIST1 ||
88 (input->data.location >= VARYING_SLOT_VAR0 &&
89 input->data.location <= VARYING_SLOT_VAR31) ||
90 (input->data.location >= VARYING_SLOT_TEX0 &&
91 input->data.location <= VARYING_SLOT_TEX7)) {
92
93 r600_shader_io& io = sh_info().input[input->data.driver_location];
94 tgsi_get_gl_varying_semantic(static_cast<gl_varying_slot>( input->data.location),
95 true, &io.name, &io.sid);
96 io.ring_offset = 16 * input->data.driver_location;
97 ++sh_info().ninput;
98 m_next_input_ring_offset += 16;
99 return true;
100 }
101
102 return false;
103 }
104
105 bool GeometryShaderFromNir::do_process_outputs(nir_variable *output)
106 {
107 if (output->data.location == VARYING_SLOT_COL0 ||
108 output->data.location == VARYING_SLOT_COL1 ||
109 (output->data.location >= VARYING_SLOT_VAR0 &&
110 output->data.location <= VARYING_SLOT_VAR31) ||
111 (output->data.location >= VARYING_SLOT_TEX0 &&
112 output->data.location <= VARYING_SLOT_TEX7) ||
113 output->data.location == VARYING_SLOT_BFC0 ||
114 output->data.location == VARYING_SLOT_BFC1 ||
115 output->data.location == VARYING_SLOT_CLIP_VERTEX ||
116 output->data.location == VARYING_SLOT_CLIP_DIST0 ||
117 output->data.location == VARYING_SLOT_CLIP_DIST1 ||
118 output->data.location == VARYING_SLOT_PRIMITIVE_ID ||
119 output->data.location == VARYING_SLOT_POS ||
120 output->data.location == VARYING_SLOT_PSIZ ||
121 output->data.location == VARYING_SLOT_LAYER ||
122 output->data.location == VARYING_SLOT_VIEWPORT ||
123 output->data.location == VARYING_SLOT_FOGC) {
124 r600_shader_io& io = sh_info().output[output->data.driver_location];
125
126 tgsi_get_gl_varying_semantic(static_cast<gl_varying_slot>( output->data.location),
127 true, &io.name, &io.sid);
128 evaluate_spi_sid(io);
129 ++sh_info().noutput;
130
131 if (output->data.location == VARYING_SLOT_CLIP_DIST0 ||
132 output->data.location == VARYING_SLOT_CLIP_DIST1) {
133 m_num_clip_dist += 4;
134 }
135 return true;
136 }
137 return false;
138 }
139
140
141 bool GeometryShaderFromNir::allocate_reserved_registers()
142 {
143 const int sel[6] = {0, 0 ,0, 1, 1, 1};
144 const int chan[6] = {0, 1 ,3, 0, 1, 2};
145
146 increment_reserved_registers();
147 increment_reserved_registers();
148
149 /* Reserve registers used by the shaders (should check how many
150 * components are actually used */
151 for (int i = 0; i < 6; ++i) {
152 auto reg = new GPRValue(sel[i], chan[i]);
153 reg->set_as_input();
154 m_per_vertex_offsets[i].reset(reg);
155 inject_register(sel[i], chan[i], m_per_vertex_offsets[i], false);
156 }
157 auto reg = new GPRValue(0, 2);
158 reg->set_as_input();
159 m_primitive_id.reset(reg);
160 inject_register(0, 2, m_primitive_id, false);
161
162 reg = new GPRValue(1, 3);
163 reg->set_as_input();
164 m_invocation_id.reset(reg);
165 inject_register(1, 3, m_invocation_id, false);
166
167 m_export_base = get_temp_register();
168 emit_instruction(new AluInstruction(op1_mov, m_export_base, Value::zero, {alu_write, alu_last_instr}));
169
170 sh_info().ring_item_sizes[0] = m_next_input_ring_offset;
171
172 if (m_key.gs.tri_strip_adj_fix)
173 emit_adj_fix();
174
175 return true;
176 }
177
178 void GeometryShaderFromNir::emit_adj_fix()
179 {
180 PValue adjhelp0(new GPRValue(m_export_base->sel(), 1));
181 emit_instruction(op2_and_int, adjhelp0, {m_primitive_id, Value::one_i}, {alu_write, alu_last_instr});
182
183 int help2 = allocate_temp_register();
184 int reg_indices[6];
185 int reg_chanels[6] = {0, 1, 2, 3, 2, 3};
186
187 int rotate_indices[6] = {4, 5, 0, 1, 2, 3};
188
189 reg_indices[0] = reg_indices[1] = reg_indices[2] = reg_indices[3] = help2;
190 reg_indices[4] = reg_indices[5] = m_export_base->sel();
191
192 std::array<PValue, 6> adjhelp;
193
194 AluInstruction *ir = nullptr;
195 for (int i = 0; i < 6; i++) {
196 adjhelp[i].reset(new GPRValue(reg_indices[i], reg_chanels[i]));
197 ir = new AluInstruction(op3_cnde_int, adjhelp[i],
198 {adjhelp0, m_per_vertex_offsets[i],
199 m_per_vertex_offsets[rotate_indices[i]]},
200 {alu_write});
201 if (i == 3)
202 ir->set_flag(alu_last_instr);
203 emit_instruction(ir);
204 }
205 ir->set_flag(alu_last_instr);
206
207 for (int i = 0; i < 6; i++)
208 m_per_vertex_offsets[i] = adjhelp[i];
209 }
210
211 bool GeometryShaderFromNir::emit_deref_instruction_override(nir_deref_instr* instr)
212 {
213 if (instr->deref_type == nir_deref_type_array) {
214 auto var = get_deref_location(instr->parent);
215 ArrayDeref ad = {var, &instr->arr.index};
216 assert(instr->dest.is_ssa);
217 m_in_array_deref[instr->dest.ssa.index] = ad;
218
219 /* Problem: nir_intrinsice_load_deref tries to lookup the
220 * variable, and will not find it, need to override that too */
221 return true;
222 }
223 return false;
224 }
225
226 bool GeometryShaderFromNir::emit_intrinsic_instruction_override(nir_intrinsic_instr* instr)
227 {
228 switch (instr->intrinsic) {
229 case nir_intrinsic_load_deref: {
230 auto& src = instr->src[0];
231 assert(src.is_ssa);
232 auto array = m_in_array_deref.find(src.ssa->index);
233 if (array != m_in_array_deref.end())
234 return emit_load_from_array(instr, array->second);
235 } break;
236 case nir_intrinsic_emit_vertex:
237 return emit_vertex(instr, false);
238 case nir_intrinsic_end_primitive:
239 return emit_vertex(instr, true);
240 case nir_intrinsic_load_primitive_id:
241 return load_preloaded_value(instr->dest, 0, m_primitive_id);
242 case nir_intrinsic_load_invocation_id:
243 return load_preloaded_value(instr->dest, 0, m_invocation_id);
244 default:
245 ;
246 }
247 return false;
248 }
249
250 bool GeometryShaderFromNir::emit_vertex(nir_intrinsic_instr* instr, bool cut)
251 {
252 int stream = nir_intrinsic_stream_id(instr);
253 assert(stream < 4);
254
255 emit_instruction(new EmitVertex(stream, cut));
256
257 if (!cut)
258 emit_instruction(new AluInstruction(op2_add_int, m_export_base, m_export_base,
259 PValue(new LiteralValue(sh_info().noutput)),
260 {alu_write, alu_last_instr}));
261
262 return true;
263 }
264
265 bool GeometryShaderFromNir::emit_load_from_array(nir_intrinsic_instr* instr,
266 const ArrayDeref& array_deref)
267 {
268 auto dest = vec_from_nir(instr->dest, instr->num_components);
269
270 const nir_load_const_instr* literal_index = nullptr;
271
272 if (array_deref.index->is_ssa)
273 literal_index = get_literal_constant(array_deref.index->ssa->index);
274
275 if (!literal_index) {
276 sfn_log << SfnLog::err << "GS: Indirect input addressing not (yet) supported\n";
277 return false;
278 }
279 assert(literal_index->value[0].u32 < 6);
280 PValue addr = m_per_vertex_offsets[literal_index->value[0].u32];
281
282 auto fetch = new FetchInstruction(vc_fetch, no_index_offset, dest, addr,
283 16 * array_deref.var->data.driver_location,
284 R600_GS_RING_CONST_BUFFER, PValue(), bim_none, true);
285 emit_instruction(fetch);
286 return true;
287 }
288
289 void GeometryShaderFromNir::do_finalize()
290 {
291 if (m_num_clip_dist) {
292 sh_info().cc_dist_mask = (1 << m_num_clip_dist) - 1;
293 sh_info().clip_dist_write = (1 << m_num_clip_dist) - 1;
294 }
295 }
296
297 }