1 /*
2  * Copyright © 2013 Intel Corporation
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
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * \file brw_vec4_tcs.cpp
26  *
27  * Tessellaton control shader specific code derived from the vec4_visitor class.
28  */
29 
30 #include "brw_nir.h"
31 #include "brw_vec4_tcs.h"
32 #include "brw_fs.h"
33 #include "dev/intel_debug.h"
34 
35 namespace brw {
36 
vec4_tcs_visitor(const struct brw_compiler * compiler,void * log_data,const struct brw_tcs_prog_key * key,struct brw_tcs_prog_data * prog_data,const nir_shader * nir,void * mem_ctx,int shader_time_index,bool debug_enabled)37 vec4_tcs_visitor::vec4_tcs_visitor(const struct brw_compiler *compiler,
38                                    void *log_data,
39                                    const struct brw_tcs_prog_key *key,
40                                    struct brw_tcs_prog_data *prog_data,
41                                    const nir_shader *nir,
42                                    void *mem_ctx,
43                                    int shader_time_index,
44                                    bool debug_enabled)
45    : vec4_visitor(compiler, log_data, &key->base.tex, &prog_data->base,
46                   nir, mem_ctx, false, shader_time_index, debug_enabled),
47      key(key)
48 {
49 }
50 
51 
52 void
setup_payload()53 vec4_tcs_visitor::setup_payload()
54 {
55    int reg = 0;
56 
57    /* The payload always contains important data in r0, which contains
58     * the URB handles that are passed on to the URB write at the end
59     * of the thread.
60     */
61    reg++;
62 
63    /* r1.0 - r4.7 may contain the input control point URB handles,
64     * which we use to pull vertex data.
65     */
66    reg += 4;
67 
68    /* Push constants may start at r5.0 */
69    reg = setup_uniforms(reg);
70 
71    this->first_non_payload_grf = reg;
72 }
73 
74 
75 void
emit_prolog()76 vec4_tcs_visitor::emit_prolog()
77 {
78    invocation_id = src_reg(this, glsl_type::uint_type);
79    emit(TCS_OPCODE_GET_INSTANCE_ID, dst_reg(invocation_id));
80 
81    /* HS threads are dispatched with the dispatch mask set to 0xFF.
82     * If there are an odd number of output vertices, then the final
83     * HS instance dispatched will only have its bottom half doing real
84     * work, and so we need to disable the upper half:
85     */
86    if (nir->info.tess.tcs_vertices_out % 2) {
87       emit(CMP(dst_null_d(), invocation_id,
88                brw_imm_ud(nir->info.tess.tcs_vertices_out),
89                BRW_CONDITIONAL_L));
90 
91       /* Matching ENDIF is in emit_thread_end() */
92       emit(IF(BRW_PREDICATE_NORMAL));
93    }
94 }
95 
96 
97 void
emit_thread_end()98 vec4_tcs_visitor::emit_thread_end()
99 {
100    vec4_instruction *inst;
101    current_annotation = "thread end";
102 
103    if (nir->info.tess.tcs_vertices_out % 2) {
104       emit(BRW_OPCODE_ENDIF);
105    }
106 
107    if (devinfo->ver == 7) {
108       struct brw_tcs_prog_data *tcs_prog_data =
109          (struct brw_tcs_prog_data *) prog_data;
110 
111       current_annotation = "release input vertices";
112 
113       /* Synchronize all threads, so we know that no one is still
114        * using the input URB handles.
115        */
116       if (tcs_prog_data->instances > 1) {
117          dst_reg header = dst_reg(this, glsl_type::uvec4_type);
118          emit(TCS_OPCODE_CREATE_BARRIER_HEADER, header);
119          emit(SHADER_OPCODE_BARRIER, dst_null_ud(), src_reg(header));
120       }
121 
122       /* Make thread 0 (invocations <1, 0>) release pairs of ICP handles.
123        * We want to compare the bottom half of invocation_id with 0, but
124        * use that truth value for the top half as well.  Unfortunately,
125        * we don't have stride in the vec4 world, nor UV immediates in
126        * align16, so we need an opcode to get invocation_id<0,4,0>.
127        */
128       set_condmod(BRW_CONDITIONAL_Z,
129                   emit(TCS_OPCODE_SRC0_010_IS_ZERO, dst_null_d(),
130                        invocation_id));
131       emit(IF(BRW_PREDICATE_NORMAL));
132       for (unsigned i = 0; i < key->input_vertices; i += 2) {
133          /* If we have an odd number of input vertices, the last will be
134           * unpaired.  We don't want to use an interleaved URB write in
135           * that case.
136           */
137          const bool is_unpaired = i == key->input_vertices - 1;
138 
139          dst_reg header(this, glsl_type::uvec4_type);
140          emit(TCS_OPCODE_RELEASE_INPUT, header, brw_imm_ud(i),
141               brw_imm_ud(is_unpaired));
142       }
143       emit(BRW_OPCODE_ENDIF);
144    }
145 
146    if (INTEL_DEBUG(DEBUG_SHADER_TIME))
147       emit_shader_time_end();
148 
149    inst = emit(TCS_OPCODE_THREAD_END);
150    inst->base_mrf = 14;
151    inst->mlen = 2;
152 }
153 
154 
155 void
emit_input_urb_read(const dst_reg & dst,const src_reg & vertex_index,unsigned base_offset,unsigned first_component,const src_reg & indirect_offset)156 vec4_tcs_visitor::emit_input_urb_read(const dst_reg &dst,
157                                       const src_reg &vertex_index,
158                                       unsigned base_offset,
159                                       unsigned first_component,
160                                       const src_reg &indirect_offset)
161 {
162    vec4_instruction *inst;
163    dst_reg temp(this, glsl_type::ivec4_type);
164    temp.type = dst.type;
165 
166    /* Set up the message header to reference the proper parts of the URB */
167    dst_reg header = dst_reg(this, glsl_type::uvec4_type);
168    inst = emit(TCS_OPCODE_SET_INPUT_URB_OFFSETS, header, vertex_index,
169                indirect_offset);
170    inst->force_writemask_all = true;
171 
172    /* Read into a temporary, ignoring writemasking. */
173    inst = emit(VEC4_OPCODE_URB_READ, temp, src_reg(header));
174    inst->offset = base_offset;
175    inst->mlen = 1;
176    inst->base_mrf = -1;
177 
178    /* Copy the temporary to the destination to deal with writemasking.
179     *
180     * Also attempt to deal with gl_PointSize being in the .w component.
181     */
182    if (inst->offset == 0 && indirect_offset.file == BAD_FILE) {
183       emit(MOV(dst, swizzle(src_reg(temp), BRW_SWIZZLE_WWWW)));
184    } else {
185       src_reg src = src_reg(temp);
186       src.swizzle = BRW_SWZ_COMP_INPUT(first_component);
187       emit(MOV(dst, src));
188    }
189 }
190 
191 void
emit_output_urb_read(const dst_reg & dst,unsigned base_offset,unsigned first_component,const src_reg & indirect_offset)192 vec4_tcs_visitor::emit_output_urb_read(const dst_reg &dst,
193                                        unsigned base_offset,
194                                        unsigned first_component,
195                                        const src_reg &indirect_offset)
196 {
197    vec4_instruction *inst;
198 
199    /* Set up the message header to reference the proper parts of the URB */
200    dst_reg header = dst_reg(this, glsl_type::uvec4_type);
201    inst = emit(TCS_OPCODE_SET_OUTPUT_URB_OFFSETS, header,
202                brw_imm_ud(dst.writemask << first_component), indirect_offset);
203    inst->force_writemask_all = true;
204 
205    vec4_instruction *read = emit(VEC4_OPCODE_URB_READ, dst, src_reg(header));
206    read->offset = base_offset;
207    read->mlen = 1;
208    read->base_mrf = -1;
209 
210    if (first_component) {
211       /* Read into a temporary and copy with a swizzle and writemask. */
212       read->dst = retype(dst_reg(this, glsl_type::ivec4_type), dst.type);
213       emit(MOV(dst, swizzle(src_reg(read->dst),
214                             BRW_SWZ_COMP_INPUT(first_component))));
215    }
216 }
217 
218 void
emit_urb_write(const src_reg & value,unsigned writemask,unsigned base_offset,const src_reg & indirect_offset)219 vec4_tcs_visitor::emit_urb_write(const src_reg &value,
220                                  unsigned writemask,
221                                  unsigned base_offset,
222                                  const src_reg &indirect_offset)
223 {
224    if (writemask == 0)
225       return;
226 
227    src_reg message(this, glsl_type::uvec4_type, 2);
228    vec4_instruction *inst;
229 
230    inst = emit(TCS_OPCODE_SET_OUTPUT_URB_OFFSETS, dst_reg(message),
231                brw_imm_ud(writemask), indirect_offset);
232    inst->force_writemask_all = true;
233    inst = emit(MOV(byte_offset(dst_reg(retype(message, value.type)), REG_SIZE),
234                    value));
235    inst->force_writemask_all = true;
236 
237    inst = emit(TCS_OPCODE_URB_WRITE, dst_null_f(), message);
238    inst->offset = base_offset;
239    inst->mlen = 2;
240    inst->base_mrf = -1;
241 }
242 
243 void
nir_emit_intrinsic(nir_intrinsic_instr * instr)244 vec4_tcs_visitor::nir_emit_intrinsic(nir_intrinsic_instr *instr)
245 {
246    switch (instr->intrinsic) {
247    case nir_intrinsic_load_invocation_id:
248       emit(MOV(get_nir_dest(instr->dest, BRW_REGISTER_TYPE_UD),
249                invocation_id));
250       break;
251    case nir_intrinsic_load_primitive_id:
252       emit(TCS_OPCODE_GET_PRIMITIVE_ID,
253            get_nir_dest(instr->dest, BRW_REGISTER_TYPE_UD));
254       break;
255    case nir_intrinsic_load_patch_vertices_in:
256       emit(MOV(get_nir_dest(instr->dest, BRW_REGISTER_TYPE_D),
257                brw_imm_d(key->input_vertices)));
258       break;
259    case nir_intrinsic_load_per_vertex_input: {
260       assert(nir_dest_bit_size(instr->dest) == 32);
261       src_reg indirect_offset = get_indirect_offset(instr);
262       unsigned imm_offset = instr->const_index[0];
263 
264       src_reg vertex_index = retype(get_nir_src_imm(instr->src[0]),
265                                     BRW_REGISTER_TYPE_UD);
266 
267       unsigned first_component = nir_intrinsic_component(instr);
268       dst_reg dst = get_nir_dest(instr->dest, BRW_REGISTER_TYPE_D);
269       dst.writemask = brw_writemask_for_size(instr->num_components);
270       emit_input_urb_read(dst, vertex_index, imm_offset,
271                           first_component, indirect_offset);
272       break;
273    }
274    case nir_intrinsic_load_input:
275       unreachable("nir_lower_io should use load_per_vertex_input intrinsics");
276       break;
277    case nir_intrinsic_load_output:
278    case nir_intrinsic_load_per_vertex_output: {
279       src_reg indirect_offset = get_indirect_offset(instr);
280       unsigned imm_offset = instr->const_index[0];
281 
282       dst_reg dst = get_nir_dest(instr->dest, BRW_REGISTER_TYPE_D);
283       dst.writemask = brw_writemask_for_size(instr->num_components);
284 
285       emit_output_urb_read(dst, imm_offset, nir_intrinsic_component(instr),
286                            indirect_offset);
287       break;
288    }
289    case nir_intrinsic_store_output:
290    case nir_intrinsic_store_per_vertex_output: {
291       assert(nir_src_bit_size(instr->src[0]) == 32);
292       src_reg value = get_nir_src(instr->src[0]);
293       unsigned mask = instr->const_index[1];
294       unsigned swiz = BRW_SWIZZLE_XYZW;
295 
296       src_reg indirect_offset = get_indirect_offset(instr);
297       unsigned imm_offset = instr->const_index[0];
298 
299       unsigned first_component = nir_intrinsic_component(instr);
300       if (first_component) {
301          assert(swiz == BRW_SWIZZLE_XYZW);
302          swiz = BRW_SWZ_COMP_OUTPUT(first_component);
303          mask = mask << first_component;
304       }
305 
306       emit_urb_write(swizzle(value, swiz), mask,
307                      imm_offset, indirect_offset);
308       break;
309    }
310 
311    case nir_intrinsic_control_barrier: {
312       dst_reg header = dst_reg(this, glsl_type::uvec4_type);
313       emit(TCS_OPCODE_CREATE_BARRIER_HEADER, header);
314       emit(SHADER_OPCODE_BARRIER, dst_null_ud(), src_reg(header));
315       break;
316    }
317 
318    case nir_intrinsic_memory_barrier_tcs_patch:
319       break;
320 
321    default:
322       vec4_visitor::nir_emit_intrinsic(instr);
323    }
324 }
325 
326 /**
327  * Return the number of patches to accumulate before an 8_PATCH mode thread is
328  * launched.  In cases with a large number of input control points and a large
329  * amount of VS outputs, the VS URB space needed to store an entire 8 patches
330  * worth of data can be prohibitive, so it can be beneficial to launch threads
331  * early.
332  *
333  * See the 3DSTATE_HS::Patch Count Threshold documentation for the recommended
334  * values.  Note that 0 means to "disable" early dispatch, meaning to wait for
335  * a full 8 patches as normal.
336  */
337 static int
get_patch_count_threshold(int input_control_points)338 get_patch_count_threshold(int input_control_points)
339 {
340    if (input_control_points <= 4)
341       return 0;
342    else if (input_control_points <= 6)
343       return 5;
344    else if (input_control_points <= 8)
345       return 4;
346    else if (input_control_points <= 10)
347       return 3;
348    else if (input_control_points <= 14)
349       return 2;
350 
351    /* Return patch count 1 for PATCHLIST_15 - PATCHLIST_32 */
352    return 1;
353 }
354 
355 } /* namespace brw */
356 
357 extern "C" const unsigned *
brw_compile_tcs(const struct brw_compiler * compiler,void * log_data,void * mem_ctx,const struct brw_tcs_prog_key * key,struct brw_tcs_prog_data * prog_data,nir_shader * nir,int shader_time_index,struct brw_compile_stats * stats,char ** error_str)358 brw_compile_tcs(const struct brw_compiler *compiler,
359                 void *log_data,
360                 void *mem_ctx,
361                 const struct brw_tcs_prog_key *key,
362                 struct brw_tcs_prog_data *prog_data,
363                 nir_shader *nir,
364                 int shader_time_index,
365                 struct brw_compile_stats *stats,
366                 char **error_str)
367 {
368    const struct intel_device_info *devinfo = compiler->devinfo;
369    struct brw_vue_prog_data *vue_prog_data = &prog_data->base;
370    const bool is_scalar = compiler->scalar_stage[MESA_SHADER_TESS_CTRL];
371    const bool debug_enabled = INTEL_DEBUG(DEBUG_TCS);
372    const unsigned *assembly;
373 
374    vue_prog_data->base.stage = MESA_SHADER_TESS_CTRL;
375 
376    nir->info.outputs_written = key->outputs_written;
377    nir->info.patch_outputs_written = key->patch_outputs_written;
378 
379    struct brw_vue_map input_vue_map;
380    brw_compute_vue_map(devinfo, &input_vue_map, nir->info.inputs_read,
381                        nir->info.separate_shader, 1);
382    brw_compute_tess_vue_map(&vue_prog_data->vue_map,
383                             nir->info.outputs_written,
384                             nir->info.patch_outputs_written);
385 
386    brw_nir_apply_key(nir, compiler, &key->base, 8, is_scalar);
387    brw_nir_lower_vue_inputs(nir, &input_vue_map);
388    brw_nir_lower_tcs_outputs(nir, &vue_prog_data->vue_map,
389                              key->tes_primitive_mode);
390    if (key->quads_workaround)
391       brw_nir_apply_tcs_quads_workaround(nir);
392 
393    brw_postprocess_nir(nir, compiler, is_scalar, debug_enabled,
394                        key->base.robust_buffer_access);
395 
396    bool has_primitive_id =
397       BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_PRIMITIVE_ID);
398 
399    prog_data->patch_count_threshold = brw::get_patch_count_threshold(key->input_vertices);
400 
401    if (compiler->use_tcs_8_patch &&
402        nir->info.tess.tcs_vertices_out <= (devinfo->ver >= 12 ? 32 : 16) &&
403        2 + has_primitive_id + key->input_vertices <= (devinfo->ver >= 12 ? 63 : 31)) {
404       /* 3DSTATE_HS imposes two constraints on using 8_PATCH mode. First, the
405        * "Instance" field limits the number of output vertices to [1, 16] on
406        * gfx11 and below, or [1, 32] on gfx12 and above. Secondly, the
407        * "Dispatch GRF Start Register for URB Data" field is limited to [0,
408        * 31] - which imposes a limit on the input vertices.
409        */
410       vue_prog_data->dispatch_mode = DISPATCH_MODE_TCS_8_PATCH;
411       prog_data->instances = nir->info.tess.tcs_vertices_out;
412       prog_data->include_primitive_id = has_primitive_id;
413    } else {
414       unsigned verts_per_thread = is_scalar ? 8 : 2;
415       vue_prog_data->dispatch_mode = DISPATCH_MODE_TCS_SINGLE_PATCH;
416       prog_data->instances =
417          DIV_ROUND_UP(nir->info.tess.tcs_vertices_out, verts_per_thread);
418    }
419 
420    /* Compute URB entry size.  The maximum allowed URB entry size is 32k.
421     * That divides up as follows:
422     *
423     *     32 bytes for the patch header (tessellation factors)
424     *    480 bytes for per-patch varyings (a varying component is 4 bytes and
425     *              gl_MaxTessPatchComponents = 120)
426     *  16384 bytes for per-vertex varyings (a varying component is 4 bytes,
427     *              gl_MaxPatchVertices = 32 and
428     *              gl_MaxTessControlOutputComponents = 128)
429     *
430     *  15808 bytes left for varying packing overhead
431     */
432    const int num_per_patch_slots = vue_prog_data->vue_map.num_per_patch_slots;
433    const int num_per_vertex_slots = vue_prog_data->vue_map.num_per_vertex_slots;
434    unsigned output_size_bytes = 0;
435    /* Note that the patch header is counted in num_per_patch_slots. */
436    output_size_bytes += num_per_patch_slots * 16;
437    output_size_bytes += nir->info.tess.tcs_vertices_out *
438                         num_per_vertex_slots * 16;
439 
440    assert(output_size_bytes >= 1);
441    if (output_size_bytes > GFX7_MAX_HS_URB_ENTRY_SIZE_BYTES)
442       return NULL;
443 
444    /* URB entry sizes are stored as a multiple of 64 bytes. */
445    vue_prog_data->urb_entry_size = ALIGN(output_size_bytes, 64) / 64;
446 
447    /* HS does not use the usual payload pushing from URB to GRFs,
448     * because we don't have enough registers for a full-size payload, and
449     * the hardware is broken on Haswell anyway.
450     */
451    vue_prog_data->urb_read_length = 0;
452 
453    if (unlikely(debug_enabled)) {
454       fprintf(stderr, "TCS Input ");
455       brw_print_vue_map(stderr, &input_vue_map, MESA_SHADER_TESS_CTRL);
456       fprintf(stderr, "TCS Output ");
457       brw_print_vue_map(stderr, &vue_prog_data->vue_map, MESA_SHADER_TESS_CTRL);
458    }
459 
460    if (is_scalar) {
461       fs_visitor v(compiler, log_data, mem_ctx, &key->base,
462                    &prog_data->base.base, nir, 8,
463                    shader_time_index, debug_enabled);
464       if (!v.run_tcs()) {
465          if (error_str)
466             *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
467          return NULL;
468       }
469 
470       prog_data->base.base.dispatch_grf_start_reg = v.payload.num_regs;
471 
472       fs_generator g(compiler, log_data, mem_ctx,
473                      &prog_data->base.base, false, MESA_SHADER_TESS_CTRL);
474       if (unlikely(debug_enabled)) {
475          g.enable_debug(ralloc_asprintf(mem_ctx,
476                                         "%s tessellation control shader %s",
477                                         nir->info.label ? nir->info.label
478                                                         : "unnamed",
479                                         nir->info.name));
480       }
481 
482       g.generate_code(v.cfg, 8, v.shader_stats,
483                       v.performance_analysis.require(), stats);
484 
485       g.add_const_data(nir->constant_data, nir->constant_data_size);
486 
487       assembly = g.get_assembly();
488    } else {
489       brw::vec4_tcs_visitor v(compiler, log_data, key, prog_data,
490                               nir, mem_ctx, shader_time_index,
491                               debug_enabled);
492       if (!v.run()) {
493          if (error_str)
494             *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
495          return NULL;
496       }
497 
498       if (INTEL_DEBUG(DEBUG_TCS))
499          v.dump_instructions();
500 
501 
502       assembly = brw_vec4_generate_assembly(compiler, log_data, mem_ctx, nir,
503                                             &prog_data->base, v.cfg,
504                                             v.performance_analysis.require(),
505                                             stats, debug_enabled);
506    }
507 
508    return assembly;
509 }
510