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,bool passthrough_prim_id)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 bool passthrough_prim_id)
128 {
129 unsigned int vertices_out = vertices_for_prim(primitive_type);
130 emulate_edgeflags = emulate_edgeflags && (prev_stage->info.outputs_written & VARYING_BIT_EDGE);
131 enum mesa_prim original_our_prim = gs_out_prim_for_topology(output_primitive_type);
132 nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_GEOMETRY,
133 options,
134 "gs passthrough");
135
136 bool output_lines = force_line_strip_out || original_our_prim == MESA_PRIM_LINE_STRIP;
137 bool needs_closing = (force_line_strip_out || (emulate_edgeflags && output_lines)) && vertices_out >= 3;
138
139 nir_shader *nir = b.shader;
140 nir->info.gs.input_primitive = gs_in_prim_for_topology(primitive_type);
141 nir->info.gs.output_primitive = force_line_strip_out ? MESA_PRIM_LINE_STRIP : original_our_prim;
142 nir->info.gs.vertices_in = mesa_vertices_per_prim(primitive_type);
143 nir->info.gs.vertices_out = needs_closing ? vertices_out + 1 : vertices_out;
144 nir->info.gs.invocations = 1;
145 nir->info.gs.active_stream_mask = 1;
146
147 nir->info.has_transform_feedback_varyings = prev_stage->info.has_transform_feedback_varyings;
148 memcpy(nir->info.xfb_stride, prev_stage->info.xfb_stride, sizeof(prev_stage->info.xfb_stride));
149 if (prev_stage->xfb_info) {
150 size_t size = nir_xfb_info_size(prev_stage->xfb_info->output_count);
151 nir->xfb_info = ralloc_memdup(nir, prev_stage->xfb_info, size);
152 }
153
154 bool handle_flat = output_lines && nir->info.gs.output_primitive != gs_out_prim_for_topology(primitive_type);
155 nir_variable *in_vars[VARYING_SLOT_MAX * 4];
156 nir_variable *out_vars[VARYING_SLOT_MAX * 4];
157 unsigned num_inputs = 0, num_outputs = 0;
158
159 /* Create input/output variables. */
160 nir_foreach_shader_out_variable(var, prev_stage) {
161 assert(!var->data.patch);
162 assert(var->data.location != VARYING_SLOT_PRIMITIVE_ID &&
163 "not a VS output");
164
165 /* input vars can't be created for those */
166 if (var->data.location == VARYING_SLOT_LAYER ||
167 var->data.location == VARYING_SLOT_VIEW_INDEX)
168 continue;
169
170 char name[100];
171 if (var->name)
172 snprintf(name, sizeof(name), "in_%s", var->name);
173 else
174 snprintf(name, sizeof(name), "in_%d", var->data.driver_location);
175
176 nir_variable *in = nir_variable_clone(var, nir);
177 ralloc_free(in->name);
178 in->name = ralloc_strdup(in, name);
179 in->type = glsl_array_type(var->type, 6, false);
180 in->data.mode = nir_var_shader_in;
181 nir_shader_add_variable(nir, in);
182
183 in_vars[num_inputs++] = in;
184
185 nir->num_inputs++;
186 if (in->data.location == VARYING_SLOT_EDGE)
187 continue;
188
189 if (var->data.location != VARYING_SLOT_POS)
190 nir->num_outputs++;
191
192 if (var->name)
193 snprintf(name, sizeof(name), "out_%s", var->name);
194 else
195 snprintf(name, sizeof(name), "out_%d", var->data.driver_location);
196
197 nir_variable *out = nir_variable_clone(var, nir);
198 ralloc_free(out->name);
199 out->name = ralloc_strdup(out, name);
200 out->data.mode = nir_var_shader_out;
201 nir_shader_add_variable(nir, out);
202
203 out_vars[num_outputs++] = out;
204 }
205
206 if (passthrough_prim_id) {
207 /* When a geometry shader is not used, a fragment shader may read primitive
208 * ID and get an implicit value without the vertex shader writing an ID. This
209 * case needs to work even when we inject a GS internally.
210 *
211 * However, if a geometry shader precedes a fragment shader that reads
212 * primitive ID, Vulkan requires that the geometry shader write primitive ID.
213 * To handle this case correctly, we must write primitive ID, copying the
214 * fixed-function gl_PrimitiveIDIn input which matches what the fragment
215 * shader will expect.
216 */
217 in_vars[num_inputs++] = nir_create_variable_with_location(
218 nir, nir_var_shader_in, VARYING_SLOT_PRIMITIVE_ID, glsl_int_type());
219
220 out_vars[num_outputs++] = nir_create_variable_with_location(
221 nir, nir_var_shader_out, VARYING_SLOT_PRIMITIVE_ID, glsl_int_type());
222 }
223
224 unsigned int start_vert = 0;
225 unsigned int end_vert = vertices_out;
226 unsigned int vert_step = 1;
227 switch (primitive_type) {
228 case MESA_PRIM_LINES_ADJACENCY:
229 case MESA_PRIM_LINE_STRIP_ADJACENCY:
230 start_vert = 1;
231 end_vert += 1;
232 break;
233 case MESA_PRIM_TRIANGLES_ADJACENCY:
234 case MESA_PRIM_TRIANGLE_STRIP_ADJACENCY:
235 end_vert = 5;
236 vert_step = 2;
237 break;
238 default:
239 break;
240 }
241
242 nir_variable *edge_var = nir_find_variable_with_location(nir, nir_var_shader_in, VARYING_SLOT_EDGE);
243 nir_def *flat_interp_mask_def = nir_load_flat_mask(&b);
244 nir_def *last_pv_vert_def = nir_load_provoking_last(&b);
245 last_pv_vert_def = nir_ine_imm(&b, last_pv_vert_def, 0);
246 nir_def *start_vert_index = nir_imm_int(&b, start_vert);
247 nir_def *end_vert_index = nir_imm_int(&b, end_vert - 1);
248 nir_def *pv_vert_index = nir_bcsel(&b, last_pv_vert_def, end_vert_index, start_vert_index);
249 for (unsigned i = start_vert; i < end_vert || needs_closing; i += vert_step) {
250 int idx = i < end_vert ? i : start_vert;
251 /* Copy inputs to outputs. */
252 for (unsigned j = 0, oj = 0; j < num_inputs; ++j) {
253 if (in_vars[j]->data.location == VARYING_SLOT_EDGE) {
254 continue;
255 }
256 /* no need to use copy_var to save a lower pass */
257 nir_def *index;
258 if (in_vars[j]->data.location == VARYING_SLOT_POS || !handle_flat)
259 index = nir_imm_int(&b, idx);
260 else {
261 uint64_t mask = BITFIELD64_BIT(in_vars[j]->data.location);
262 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);
263 }
264
265 /* gl_PrimitiveIDIn is not arrayed, all other inputs are */
266 nir_deref_instr *value = nir_build_deref_var(&b, in_vars[j]);
267 if (in_vars[j]->data.location != VARYING_SLOT_PRIMITIVE_ID)
268 value = nir_build_deref_array(&b, value, index);
269
270 copy_vars(&b, nir_build_deref_var(&b, out_vars[oj]), value);
271 ++oj;
272 }
273
274 if (emulate_edgeflags && !output_lines) {
275 nir_def *edge_value = nir_channel(&b, nir_load_array_var_imm(&b, edge_var, idx), 0);
276 nir_push_if(&b, nir_feq_imm(&b, edge_value, 1.0));
277 }
278
279 nir_emit_vertex(&b, 0);
280
281 if (emulate_edgeflags) {
282 if (nir->info.gs.output_primitive == MESA_PRIM_LINE_STRIP) {
283 nir_def *edge_value = nir_channel(&b, nir_load_array_var_imm(&b, edge_var, idx), 0);
284 nir_push_if(&b, nir_fneu_imm(&b, edge_value, 1.0));
285 nir_end_primitive(&b, 0);
286
287 }
288 nir_pop_if(&b, NULL);
289 }
290
291 if (i >= end_vert)
292 break;
293 }
294
295 nir_end_primitive(&b, 0);
296 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
297 nir_validate_shader(nir, "in nir_create_passthrough_gs");
298
299 return nir;
300 }
301