1 /*
2 * Copyright © 2022 Collabora Ltc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "util/ralloc.h"
25 #include "util/u_memory.h"
26 #include "nir.h"
27 #include "nir_builder.h"
28 #include "nir_xfb_info.h"
29
30 static enum mesa_prim
gs_in_prim_for_topology(enum mesa_prim prim)31 gs_in_prim_for_topology(enum mesa_prim prim)
32 {
33 switch (prim) {
34 case MESA_PRIM_QUADS:
35 return MESA_PRIM_LINES_ADJACENCY;
36 default:
37 return u_decomposed_prim(prim);
38 }
39 }
40
41 static enum mesa_prim
gs_out_prim_for_topology(enum mesa_prim prim)42 gs_out_prim_for_topology(enum mesa_prim prim)
43 {
44 switch (prim) {
45 case MESA_PRIM_POINTS:
46 return MESA_PRIM_POINTS;
47 case MESA_PRIM_LINES:
48 case MESA_PRIM_LINE_LOOP:
49 case MESA_PRIM_LINES_ADJACENCY:
50 case MESA_PRIM_LINE_STRIP_ADJACENCY:
51 case MESA_PRIM_LINE_STRIP:
52 return MESA_PRIM_LINE_STRIP;
53 case MESA_PRIM_TRIANGLES:
54 case MESA_PRIM_TRIANGLE_STRIP:
55 case MESA_PRIM_TRIANGLE_FAN:
56 case MESA_PRIM_TRIANGLES_ADJACENCY:
57 case MESA_PRIM_TRIANGLE_STRIP_ADJACENCY:
58 case MESA_PRIM_POLYGON:
59 return MESA_PRIM_TRIANGLE_STRIP;
60 case MESA_PRIM_QUADS:
61 case MESA_PRIM_QUAD_STRIP:
62 case MESA_PRIM_PATCHES:
63 default:
64 return MESA_PRIM_QUADS;
65 }
66 }
67
68 static unsigned int
vertices_for_prim(enum mesa_prim prim)69 vertices_for_prim(enum mesa_prim prim)
70 {
71 switch (prim) {
72 case MESA_PRIM_POINTS:
73 return 1;
74 case MESA_PRIM_LINES:
75 case MESA_PRIM_LINE_LOOP:
76 case MESA_PRIM_LINES_ADJACENCY:
77 case MESA_PRIM_LINE_STRIP_ADJACENCY:
78 case MESA_PRIM_LINE_STRIP:
79 return 2;
80 case MESA_PRIM_TRIANGLES:
81 case MESA_PRIM_TRIANGLE_STRIP:
82 case MESA_PRIM_TRIANGLE_FAN:
83 case MESA_PRIM_TRIANGLES_ADJACENCY:
84 case MESA_PRIM_TRIANGLE_STRIP_ADJACENCY:
85 case MESA_PRIM_POLYGON:
86 return 3;
87 case MESA_PRIM_QUADS:
88 case MESA_PRIM_QUAD_STRIP:
89 return 4;
90 case MESA_PRIM_PATCHES:
91 default:
92 unreachable("unsupported primitive for gs input");
93 }
94 }
95
96 static void
copy_vars(nir_builder * b,nir_deref_instr * dst,nir_deref_instr * src)97 copy_vars(nir_builder *b, nir_deref_instr *dst, nir_deref_instr *src)
98 {
99 assert(glsl_get_bare_type(dst->type) == glsl_get_bare_type(src->type));
100 if (glsl_type_is_struct_or_ifc(dst->type)) {
101 for (unsigned i = 0; i < glsl_get_length(dst->type); ++i) {
102 copy_vars(b, nir_build_deref_struct(b, dst, i), nir_build_deref_struct(b, src, i));
103 }
104 } else if (glsl_type_is_array_or_matrix(dst->type)) {
105 unsigned count = glsl_type_is_array(dst->type) ? glsl_array_size(dst->type) : glsl_get_matrix_columns(dst->type);
106 for (unsigned i = 0; i < count; i++) {
107 copy_vars(b, nir_build_deref_array_imm(b, dst, i), nir_build_deref_array_imm(b, src, i));
108 }
109 } else {
110 nir_def *load = nir_load_deref(b, src);
111 nir_store_deref(b, dst, load, BITFIELD_MASK(load->num_components));
112 }
113 }
114
115 /*
116 * A helper to create a passthrough GS shader for drivers that needs to lower
117 * some rendering tasks to the GS.
118 */
119
120 nir_shader *
nir_create_passthrough_gs(const nir_shader_compiler_options * options,const nir_shader * prev_stage,enum mesa_prim primitive_type,enum mesa_prim output_primitive_type,bool emulate_edgeflags,bool force_line_strip_out)121 nir_create_passthrough_gs(const nir_shader_compiler_options *options,
122 const nir_shader *prev_stage,
123 enum mesa_prim primitive_type,
124 enum mesa_prim output_primitive_type,
125 bool emulate_edgeflags,
126 bool force_line_strip_out)
127 {
128 unsigned int vertices_out = vertices_for_prim(primitive_type);
129 emulate_edgeflags = emulate_edgeflags && (prev_stage->info.outputs_written & VARYING_BIT_EDGE);
130 enum mesa_prim original_our_prim = gs_out_prim_for_topology(output_primitive_type);
131 nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_GEOMETRY,
132 options,
133 "gs passthrough");
134
135 bool output_lines = force_line_strip_out || original_our_prim == MESA_PRIM_LINE_STRIP;
136 bool needs_closing = (force_line_strip_out || (emulate_edgeflags && output_lines)) && vertices_out >= 3;
137
138 nir_shader *nir = b.shader;
139 nir->info.gs.input_primitive = gs_in_prim_for_topology(primitive_type);
140 nir->info.gs.output_primitive = force_line_strip_out ? MESA_PRIM_LINE_STRIP : original_our_prim;
141 nir->info.gs.vertices_in = mesa_vertices_per_prim(primitive_type);
142 nir->info.gs.vertices_out = needs_closing ? vertices_out + 1 : vertices_out;
143 nir->info.gs.invocations = 1;
144 nir->info.gs.active_stream_mask = 1;
145
146 nir->info.has_transform_feedback_varyings = prev_stage->info.has_transform_feedback_varyings;
147 memcpy(nir->info.xfb_stride, prev_stage->info.xfb_stride, sizeof(prev_stage->info.xfb_stride));
148 if (prev_stage->xfb_info) {
149 size_t size = nir_xfb_info_size(prev_stage->xfb_info->output_count);
150 nir->xfb_info = ralloc_memdup(nir, prev_stage->xfb_info, size);
151 }
152
153 bool handle_flat = output_lines && nir->info.gs.output_primitive != gs_out_prim_for_topology(primitive_type);
154 nir_variable *in_vars[VARYING_SLOT_MAX * 4];
155 nir_variable *out_vars[VARYING_SLOT_MAX * 4];
156 unsigned num_inputs = 0, num_outputs = 0;
157
158 /* Create input/output variables. */
159 nir_foreach_shader_out_variable(var, prev_stage) {
160 assert(!var->data.patch);
161
162 /* input vars can't be created for those */
163 if (var->data.location == VARYING_SLOT_LAYER ||
164 var->data.location == VARYING_SLOT_VIEW_INDEX)
165 continue;
166
167 char name[100];
168 if (var->name)
169 snprintf(name, sizeof(name), "in_%s", var->name);
170 else
171 snprintf(name, sizeof(name), "in_%d", var->data.driver_location);
172
173 nir_variable *in = nir_variable_clone(var, nir);
174 ralloc_free(in->name);
175 in->name = ralloc_strdup(in, name);
176 in->type = glsl_array_type(var->type, 6, false);
177 in->data.mode = nir_var_shader_in;
178 nir_shader_add_variable(nir, in);
179
180 in_vars[num_inputs++] = in;
181
182 nir->num_inputs++;
183 if (in->data.location == VARYING_SLOT_EDGE)
184 continue;
185
186 if (var->data.location != VARYING_SLOT_POS)
187 nir->num_outputs++;
188
189 if (var->name)
190 snprintf(name, sizeof(name), "out_%s", var->name);
191 else
192 snprintf(name, sizeof(name), "out_%d", var->data.driver_location);
193
194 nir_variable *out = nir_variable_clone(var, nir);
195 ralloc_free(out->name);
196 out->name = ralloc_strdup(out, name);
197 out->data.mode = nir_var_shader_out;
198 nir_shader_add_variable(nir, out);
199
200 out_vars[num_outputs++] = out;
201 }
202
203 unsigned int start_vert = 0;
204 unsigned int end_vert = vertices_out;
205 unsigned int vert_step = 1;
206 switch (primitive_type) {
207 case MESA_PRIM_LINES_ADJACENCY:
208 case MESA_PRIM_LINE_STRIP_ADJACENCY:
209 start_vert = 1;
210 end_vert += 1;
211 break;
212 case MESA_PRIM_TRIANGLES_ADJACENCY:
213 case MESA_PRIM_TRIANGLE_STRIP_ADJACENCY:
214 end_vert = 5;
215 vert_step = 2;
216 break;
217 default:
218 break;
219 }
220
221 nir_variable *edge_var = nir_find_variable_with_location(nir, nir_var_shader_in, VARYING_SLOT_EDGE);
222 nir_def *flat_interp_mask_def = nir_load_flat_mask(&b);
223 nir_def *last_pv_vert_def = nir_load_provoking_last(&b);
224 last_pv_vert_def = nir_ine_imm(&b, last_pv_vert_def, 0);
225 nir_def *start_vert_index = nir_imm_int(&b, start_vert);
226 nir_def *end_vert_index = nir_imm_int(&b, end_vert - 1);
227 nir_def *pv_vert_index = nir_bcsel(&b, last_pv_vert_def, end_vert_index, start_vert_index);
228 for (unsigned i = start_vert; i < end_vert || needs_closing; i += vert_step) {
229 int idx = i < end_vert ? i : start_vert;
230 /* Copy inputs to outputs. */
231 for (unsigned j = 0, oj = 0; j < num_inputs; ++j) {
232 if (in_vars[j]->data.location == VARYING_SLOT_EDGE) {
233 continue;
234 }
235 /* no need to use copy_var to save a lower pass */
236 nir_def *index;
237 if (in_vars[j]->data.location == VARYING_SLOT_POS || !handle_flat)
238 index = nir_imm_int(&b, idx);
239 else {
240 uint64_t mask = BITFIELD64_BIT(in_vars[j]->data.location);
241 index = nir_bcsel(&b, nir_ieq_imm(&b, nir_iand_imm(&b, flat_interp_mask_def, mask), 0), nir_imm_int(&b, idx), pv_vert_index);
242 }
243 nir_deref_instr *value = nir_build_deref_array(&b, nir_build_deref_var(&b, in_vars[j]), index);
244 copy_vars(&b, nir_build_deref_var(&b, out_vars[oj]), value);
245 ++oj;
246 }
247
248 if (emulate_edgeflags && !output_lines) {
249 nir_def *edge_value = nir_channel(&b, nir_load_array_var_imm(&b, edge_var, idx), 0);
250 nir_push_if(&b, nir_feq_imm(&b, edge_value, 1.0));
251 }
252
253 nir_emit_vertex(&b, 0);
254
255 if (emulate_edgeflags) {
256 if (nir->info.gs.output_primitive == MESA_PRIM_LINE_STRIP) {
257 nir_def *edge_value = nir_channel(&b, nir_load_array_var_imm(&b, edge_var, idx), 0);
258 nir_push_if(&b, nir_fneu_imm(&b, edge_value, 1.0));
259 nir_end_primitive(&b, 0);
260
261 }
262 nir_pop_if(&b, NULL);
263 }
264
265 if (i >= end_vert)
266 break;
267 }
268
269 nir_end_primitive(&b, 0);
270 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
271 nir_validate_shader(nir, "in nir_create_passthrough_gs");
272
273 return nir;
274 }
275