radeonsi: fix doubles and int64
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_llvm_tess.c
1 /*
2 * Copyright 2020 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "si_shader_internal.h"
26 #include "si_pipe.h"
27 #include "sid.h"
28
29 static LLVMValueRef get_rel_patch_id(struct si_shader_context *ctx)
30 {
31 switch (ctx->type) {
32 case PIPE_SHADER_TESS_CTRL:
33 return si_unpack_param(ctx, ctx->args.tcs_rel_ids, 0, 8);
34
35 case PIPE_SHADER_TESS_EVAL:
36 return ac_get_arg(&ctx->ac, ctx->tes_rel_patch_id);
37
38 default:
39 assert(0);
40 return NULL;
41 }
42 }
43
44 /* Tessellation shaders pass outputs to the next shader using LDS.
45 *
46 * LS outputs = TCS inputs
47 * TCS outputs = TES inputs
48 *
49 * The LDS layout is:
50 * - TCS inputs for patch 0
51 * - TCS inputs for patch 1
52 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
53 * - ...
54 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
55 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
56 * - TCS outputs for patch 1
57 * - Per-patch TCS outputs for patch 1
58 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
59 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
60 * - ...
61 *
62 * All three shaders VS(LS), TCS, TES share the same LDS space.
63 */
64
65 static LLVMValueRef
66 get_tcs_in_patch_stride(struct si_shader_context *ctx)
67 {
68 return si_unpack_param(ctx, ctx->vs_state_bits, 11, 13);
69 }
70
71 static unsigned get_tcs_out_vertex_dw_stride_constant(struct si_shader_context *ctx)
72 {
73 assert(ctx->type == PIPE_SHADER_TESS_CTRL);
74
75 if (ctx->shader->key.mono.u.ff_tcs_inputs_to_copy)
76 return util_last_bit64(ctx->shader->key.mono.u.ff_tcs_inputs_to_copy) * 4;
77
78 return util_last_bit64(ctx->shader->selector->outputs_written) * 4;
79 }
80
81 static LLVMValueRef get_tcs_out_vertex_dw_stride(struct si_shader_context *ctx)
82 {
83 unsigned stride = get_tcs_out_vertex_dw_stride_constant(ctx);
84
85 return LLVMConstInt(ctx->i32, stride, 0);
86 }
87
88 static LLVMValueRef get_tcs_out_patch_stride(struct si_shader_context *ctx)
89 {
90 if (ctx->shader->key.mono.u.ff_tcs_inputs_to_copy)
91 return si_unpack_param(ctx, ctx->tcs_out_lds_layout, 0, 13);
92
93 const struct si_shader_info *info = &ctx->shader->selector->info;
94 unsigned tcs_out_vertices = info->properties[TGSI_PROPERTY_TCS_VERTICES_OUT];
95 unsigned vertex_dw_stride = get_tcs_out_vertex_dw_stride_constant(ctx);
96 unsigned num_patch_outputs = util_last_bit64(ctx->shader->selector->patch_outputs_written);
97 unsigned patch_dw_stride = tcs_out_vertices * vertex_dw_stride +
98 num_patch_outputs * 4;
99 return LLVMConstInt(ctx->i32, patch_dw_stride, 0);
100 }
101
102 static LLVMValueRef
103 get_tcs_out_patch0_offset(struct si_shader_context *ctx)
104 {
105 return LLVMBuildMul(ctx->ac.builder,
106 si_unpack_param(ctx, ctx->tcs_out_lds_offsets, 0, 16),
107 LLVMConstInt(ctx->i32, 4, 0), "");
108 }
109
110 static LLVMValueRef
111 get_tcs_out_patch0_patch_data_offset(struct si_shader_context *ctx)
112 {
113 return LLVMBuildMul(ctx->ac.builder,
114 si_unpack_param(ctx, ctx->tcs_out_lds_offsets, 16, 16),
115 LLVMConstInt(ctx->i32, 4, 0), "");
116 }
117
118 static LLVMValueRef
119 get_tcs_in_current_patch_offset(struct si_shader_context *ctx)
120 {
121 LLVMValueRef patch_stride = get_tcs_in_patch_stride(ctx);
122 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
123
124 return LLVMBuildMul(ctx->ac.builder, patch_stride, rel_patch_id, "");
125 }
126
127 static LLVMValueRef
128 get_tcs_out_current_patch_offset(struct si_shader_context *ctx)
129 {
130 LLVMValueRef patch0_offset = get_tcs_out_patch0_offset(ctx);
131 LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
132 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
133
134 return ac_build_imad(&ctx->ac, patch_stride, rel_patch_id, patch0_offset);
135 }
136
137 static LLVMValueRef
138 get_tcs_out_current_patch_data_offset(struct si_shader_context *ctx)
139 {
140 LLVMValueRef patch0_patch_data_offset =
141 get_tcs_out_patch0_patch_data_offset(ctx);
142 LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
143 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
144
145 return ac_build_imad(&ctx->ac, patch_stride, rel_patch_id, patch0_patch_data_offset);
146 }
147
148 static LLVMValueRef get_num_tcs_out_vertices(struct si_shader_context *ctx)
149 {
150 unsigned tcs_out_vertices =
151 ctx->shader->selector ?
152 ctx->shader->selector->info.properties[TGSI_PROPERTY_TCS_VERTICES_OUT] : 0;
153
154 /* If !tcs_out_vertices, it's either the fixed-func TCS or the TCS epilog. */
155 if (ctx->type == PIPE_SHADER_TESS_CTRL && tcs_out_vertices)
156 return LLVMConstInt(ctx->i32, tcs_out_vertices, 0);
157
158 return si_unpack_param(ctx, ctx->tcs_offchip_layout, 6, 6);
159 }
160
161 static LLVMValueRef get_tcs_in_vertex_dw_stride(struct si_shader_context *ctx)
162 {
163 unsigned stride;
164
165 switch (ctx->type) {
166 case PIPE_SHADER_VERTEX:
167 stride = ctx->shader->selector->lshs_vertex_stride / 4;
168 return LLVMConstInt(ctx->i32, stride, 0);
169
170 case PIPE_SHADER_TESS_CTRL:
171 if (ctx->screen->info.chip_class >= GFX9 &&
172 ctx->shader->is_monolithic) {
173 stride = ctx->shader->key.part.tcs.ls->lshs_vertex_stride / 4;
174 return LLVMConstInt(ctx->i32, stride, 0);
175 }
176 return si_unpack_param(ctx, ctx->vs_state_bits, 24, 8);
177
178 default:
179 assert(0);
180 return NULL;
181 }
182 }
183
184 static LLVMValueRef get_dw_address_from_generic_indices(struct si_shader_context *ctx,
185 LLVMValueRef vertex_dw_stride,
186 LLVMValueRef base_addr,
187 LLVMValueRef vertex_index,
188 LLVMValueRef param_index,
189 ubyte name, ubyte index)
190 {
191 if (vertex_dw_stride) {
192 base_addr = ac_build_imad(&ctx->ac, vertex_index,
193 vertex_dw_stride, base_addr);
194 }
195
196 if (param_index) {
197 base_addr = ac_build_imad(&ctx->ac, param_index,
198 LLVMConstInt(ctx->i32, 4, 0), base_addr);
199 }
200
201 int param = name == TGSI_SEMANTIC_PATCH ||
202 name == TGSI_SEMANTIC_TESSINNER ||
203 name == TGSI_SEMANTIC_TESSOUTER ?
204 si_shader_io_get_unique_index_patch(name, index) :
205 si_shader_io_get_unique_index(name, index, false);
206
207 /* Add the base address of the element. */
208 return LLVMBuildAdd(ctx->ac.builder, base_addr,
209 LLVMConstInt(ctx->i32, param * 4, 0), "");
210 }
211
212 /* The offchip buffer layout for TCS->TES is
213 *
214 * - attribute 0 of patch 0 vertex 0
215 * - attribute 0 of patch 0 vertex 1
216 * - attribute 0 of patch 0 vertex 2
217 * ...
218 * - attribute 0 of patch 1 vertex 0
219 * - attribute 0 of patch 1 vertex 1
220 * ...
221 * - attribute 1 of patch 0 vertex 0
222 * - attribute 1 of patch 0 vertex 1
223 * ...
224 * - per patch attribute 0 of patch 0
225 * - per patch attribute 0 of patch 1
226 * ...
227 *
228 * Note that every attribute has 4 components.
229 */
230 static LLVMValueRef get_tcs_tes_buffer_address(struct si_shader_context *ctx,
231 LLVMValueRef rel_patch_id,
232 LLVMValueRef vertex_index,
233 LLVMValueRef param_index)
234 {
235 LLVMValueRef base_addr, vertices_per_patch, num_patches, total_vertices;
236 LLVMValueRef param_stride, constant16;
237
238 vertices_per_patch = get_num_tcs_out_vertices(ctx);
239 num_patches = si_unpack_param(ctx, ctx->tcs_offchip_layout, 0, 6);
240 total_vertices = LLVMBuildMul(ctx->ac.builder, vertices_per_patch,
241 num_patches, "");
242
243 constant16 = LLVMConstInt(ctx->i32, 16, 0);
244 if (vertex_index) {
245 base_addr = ac_build_imad(&ctx->ac, rel_patch_id,
246 vertices_per_patch, vertex_index);
247 param_stride = total_vertices;
248 } else {
249 base_addr = rel_patch_id;
250 param_stride = num_patches;
251 }
252
253 base_addr = ac_build_imad(&ctx->ac, param_index, param_stride, base_addr);
254 base_addr = LLVMBuildMul(ctx->ac.builder, base_addr, constant16, "");
255
256 if (!vertex_index) {
257 LLVMValueRef patch_data_offset =
258 si_unpack_param(ctx, ctx->tcs_offchip_layout, 12, 20);
259
260 base_addr = LLVMBuildAdd(ctx->ac.builder, base_addr,
261 patch_data_offset, "");
262 }
263 return base_addr;
264 }
265
266 static LLVMValueRef get_tcs_tes_buffer_address_from_generic_indices(
267 struct si_shader_context *ctx,
268 LLVMValueRef vertex_index,
269 LLVMValueRef param_index,
270 ubyte name, ubyte index)
271 {
272 unsigned param_index_base;
273
274 param_index_base = name == TGSI_SEMANTIC_PATCH ||
275 name == TGSI_SEMANTIC_TESSINNER ||
276 name == TGSI_SEMANTIC_TESSOUTER ?
277 si_shader_io_get_unique_index_patch(name, index) :
278 si_shader_io_get_unique_index(name, index, false);
279
280 if (param_index) {
281 param_index = LLVMBuildAdd(ctx->ac.builder, param_index,
282 LLVMConstInt(ctx->i32, param_index_base, 0),
283 "");
284 } else {
285 param_index = LLVMConstInt(ctx->i32, param_index_base, 0);
286 }
287
288 return get_tcs_tes_buffer_address(ctx, get_rel_patch_id(ctx),
289 vertex_index, param_index);
290 }
291
292 static LLVMValueRef buffer_load(struct si_shader_context *ctx,
293 LLVMTypeRef type, unsigned swizzle,
294 LLVMValueRef buffer, LLVMValueRef offset,
295 LLVMValueRef base, bool can_speculate)
296 {
297 LLVMValueRef value, value2;
298 LLVMTypeRef vec_type = LLVMVectorType(type, 4);
299
300 if (swizzle == ~0) {
301 value = ac_build_buffer_load(&ctx->ac, buffer, 4, NULL, base, offset,
302 0, ac_glc, can_speculate, false);
303
304 return LLVMBuildBitCast(ctx->ac.builder, value, vec_type, "");
305 }
306
307 if (ac_get_type_size(type) != 8) {
308 value = ac_build_buffer_load(&ctx->ac, buffer, 4, NULL, base, offset,
309 0, ac_glc, can_speculate, false);
310
311 value = LLVMBuildBitCast(ctx->ac.builder, value, vec_type, "");
312 return LLVMBuildExtractElement(ctx->ac.builder, value,
313 LLVMConstInt(ctx->i32, swizzle, 0), "");
314 }
315
316 value = ac_build_buffer_load(&ctx->ac, buffer, 1, NULL, base, offset,
317 swizzle * 4, ac_glc, can_speculate, false);
318
319 value2 = ac_build_buffer_load(&ctx->ac, buffer, 1, NULL, base, offset,
320 swizzle * 4 + 4, ac_glc, can_speculate, false);
321
322 return si_build_gather_64bit(ctx, type, value, value2);
323 }
324
325 /**
326 * Load from LSHS LDS storage.
327 *
328 * \param type output value type
329 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
330 * \param dw_addr address in dwords
331 */
332 static LLVMValueRef lshs_lds_load(struct si_shader_context *ctx,
333 LLVMTypeRef type, unsigned swizzle,
334 LLVMValueRef dw_addr)
335 {
336 LLVMValueRef value;
337
338 if (swizzle == ~0) {
339 LLVMValueRef values[4];
340
341 for (unsigned chan = 0; chan < 4; chan++)
342 values[chan] = lshs_lds_load(ctx, type, chan, dw_addr);
343
344 return ac_build_gather_values(&ctx->ac, values, 4);
345 }
346
347 /* Split 64-bit loads. */
348 if (ac_get_type_size(type) == 8) {
349 LLVMValueRef lo, hi;
350
351 lo = lshs_lds_load(ctx, ctx->i32, swizzle, dw_addr);
352 hi = lshs_lds_load(ctx, ctx->i32, swizzle + 1, dw_addr);
353 return si_build_gather_64bit(ctx, type, lo, hi);
354 }
355
356 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
357 LLVMConstInt(ctx->i32, swizzle, 0), "");
358
359 value = ac_lds_load(&ctx->ac, dw_addr);
360
361 return LLVMBuildBitCast(ctx->ac.builder, value, type, "");
362 }
363
364 /**
365 * Store to LSHS LDS storage.
366 *
367 * \param swizzle offset (typically 0..3)
368 * \param dw_addr address in dwords
369 * \param value value to store
370 */
371 static void lshs_lds_store(struct si_shader_context *ctx,
372 unsigned dw_offset_imm, LLVMValueRef dw_addr,
373 LLVMValueRef value)
374 {
375 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
376 LLVMConstInt(ctx->i32, dw_offset_imm, 0), "");
377
378 ac_lds_store(&ctx->ac, dw_addr, value);
379 }
380
381 enum si_tess_ring {
382 TCS_FACTOR_RING,
383 TESS_OFFCHIP_RING_TCS,
384 TESS_OFFCHIP_RING_TES,
385 };
386
387 static LLVMValueRef get_tess_ring_descriptor(struct si_shader_context *ctx,
388 enum si_tess_ring ring)
389 {
390 LLVMBuilderRef builder = ctx->ac.builder;
391 LLVMValueRef addr = ac_get_arg(&ctx->ac,
392 ring == TESS_OFFCHIP_RING_TES ?
393 ctx->tes_offchip_addr :
394 ctx->tcs_out_lds_layout);
395
396 /* TCS only receives high 13 bits of the address. */
397 if (ring == TESS_OFFCHIP_RING_TCS || ring == TCS_FACTOR_RING) {
398 addr = LLVMBuildAnd(builder, addr,
399 LLVMConstInt(ctx->i32, 0xfff80000, 0), "");
400 }
401
402 if (ring == TCS_FACTOR_RING) {
403 unsigned tf_offset = ctx->screen->tess_offchip_ring_size;
404 addr = LLVMBuildAdd(builder, addr,
405 LLVMConstInt(ctx->i32, tf_offset, 0), "");
406 }
407
408 uint32_t rsrc3 = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
409 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
410 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
411 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
412
413 if (ctx->screen->info.chip_class >= GFX10)
414 rsrc3 |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
415 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
416 S_008F0C_RESOURCE_LEVEL(1);
417 else
418 rsrc3 |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
419 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
420
421 LLVMValueRef desc[4];
422 desc[0] = addr;
423 desc[1] = LLVMConstInt(ctx->i32,
424 S_008F04_BASE_ADDRESS_HI(ctx->screen->info.address32_hi), 0);
425 desc[2] = LLVMConstInt(ctx->i32, 0xffffffff, 0);
426 desc[3] = LLVMConstInt(ctx->i32, rsrc3, false);
427
428 return ac_build_gather_values(&ctx->ac, desc, 4);
429 }
430
431 void si_llvm_preload_tes_rings(struct si_shader_context *ctx)
432 {
433 ctx->tess_offchip_ring = get_tess_ring_descriptor(ctx, TESS_OFFCHIP_RING_TES);
434 }
435
436 static LLVMValueRef si_nir_load_tcs_varyings(struct ac_shader_abi *abi,
437 LLVMTypeRef type,
438 LLVMValueRef vertex_index,
439 LLVMValueRef param_index,
440 unsigned const_index,
441 unsigned location,
442 unsigned driver_location,
443 unsigned component,
444 unsigned num_components,
445 bool is_patch,
446 bool is_compact,
447 bool load_input)
448 {
449 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
450 struct si_shader_info *info = &ctx->shader->selector->info;
451 LLVMValueRef dw_addr, stride;
452 ubyte name, index;
453
454 driver_location = driver_location / 4;
455
456 if (load_input) {
457 name = info->input_semantic_name[driver_location];
458 index = info->input_semantic_index[driver_location];
459 } else {
460 name = info->output_semantic_name[driver_location];
461 index = info->output_semantic_index[driver_location];
462 }
463
464 assert((name == TGSI_SEMANTIC_PATCH ||
465 name == TGSI_SEMANTIC_TESSINNER ||
466 name == TGSI_SEMANTIC_TESSOUTER) == is_patch);
467
468 if (load_input) {
469 stride = get_tcs_in_vertex_dw_stride(ctx);
470 dw_addr = get_tcs_in_current_patch_offset(ctx);
471 } else {
472 if (is_patch) {
473 stride = NULL;
474 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
475 } else {
476 stride = get_tcs_out_vertex_dw_stride(ctx);
477 dw_addr = get_tcs_out_current_patch_offset(ctx);
478 }
479 }
480
481 if (!param_index) {
482 param_index = LLVMConstInt(ctx->i32, const_index, 0);
483 }
484
485 dw_addr = get_dw_address_from_generic_indices(ctx, stride, dw_addr,
486 vertex_index, param_index,
487 name, index);
488
489 LLVMValueRef value[4];
490 for (unsigned i = 0; i < num_components; i++) {
491 unsigned offset = i;
492 if (ac_get_type_size(type) == 8)
493 offset *= 2;
494
495 offset += component;
496 value[i + component] = lshs_lds_load(ctx, type, offset, dw_addr);
497 }
498
499 return ac_build_varying_gather_values(&ctx->ac, value, num_components, component);
500 }
501
502 LLVMValueRef si_nir_load_input_tes(struct ac_shader_abi *abi,
503 LLVMTypeRef type,
504 LLVMValueRef vertex_index,
505 LLVMValueRef param_index,
506 unsigned const_index,
507 unsigned location,
508 unsigned driver_location,
509 unsigned component,
510 unsigned num_components,
511 bool is_patch,
512 bool is_compact,
513 bool load_input)
514 {
515 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
516 struct si_shader_info *info = &ctx->shader->selector->info;
517 LLVMValueRef base, addr;
518
519 driver_location = driver_location / 4;
520 ubyte name = info->input_semantic_name[driver_location];
521 ubyte index = info->input_semantic_index[driver_location];
522
523 assert((name == TGSI_SEMANTIC_PATCH ||
524 name == TGSI_SEMANTIC_TESSINNER ||
525 name == TGSI_SEMANTIC_TESSOUTER) == is_patch);
526
527 base = ac_get_arg(&ctx->ac, ctx->tcs_offchip_offset);
528
529 if (!param_index) {
530 param_index = LLVMConstInt(ctx->i32, const_index, 0);
531 }
532
533 addr = get_tcs_tes_buffer_address_from_generic_indices(ctx, vertex_index,
534 param_index,
535 name, index);
536
537 /* TODO: This will generate rather ordinary llvm code, although it
538 * should be easy for the optimiser to fix up. In future we might want
539 * to refactor buffer_load().
540 */
541 LLVMValueRef value[4];
542 for (unsigned i = 0; i < num_components; i++) {
543 unsigned offset = i;
544 if (ac_get_type_size(type) == 8) {
545 offset *= 2;
546 if (offset == 4) {
547 ubyte name = info->input_semantic_name[driver_location + 1];
548 ubyte index = info->input_semantic_index[driver_location + 1];
549 addr = get_tcs_tes_buffer_address_from_generic_indices(ctx,
550 vertex_index,
551 param_index,
552 name, index);
553 }
554
555 offset = offset % 4;
556 }
557
558 offset += component;
559 value[i + component] = buffer_load(ctx, type, offset,
560 ctx->tess_offchip_ring, base, addr, true);
561 }
562
563 return ac_build_varying_gather_values(&ctx->ac, value, num_components, component);
564 }
565
566 static void si_nir_store_output_tcs(struct ac_shader_abi *abi,
567 const struct nir_variable *var,
568 LLVMValueRef vertex_index,
569 LLVMValueRef param_index,
570 unsigned const_index,
571 LLVMValueRef src,
572 unsigned writemask)
573 {
574 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
575 struct si_shader_info *info = &ctx->shader->selector->info;
576 const unsigned component = var->data.location_frac;
577 unsigned driver_location = var->data.driver_location;
578 LLVMValueRef dw_addr, stride;
579 LLVMValueRef buffer, base, addr;
580 LLVMValueRef values[8];
581 bool skip_lds_store;
582 bool is_tess_factor = false, is_tess_inner = false;
583
584 driver_location = driver_location / 4;
585 ubyte name = info->output_semantic_name[driver_location];
586 ubyte index = info->output_semantic_index[driver_location];
587
588 bool is_const = !param_index;
589 if (!param_index)
590 param_index = LLVMConstInt(ctx->i32, const_index, 0);
591
592 const bool is_patch = var->data.patch ||
593 var->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
594 var->data.location == VARYING_SLOT_TESS_LEVEL_OUTER;
595
596 assert((name == TGSI_SEMANTIC_PATCH ||
597 name == TGSI_SEMANTIC_TESSINNER ||
598 name == TGSI_SEMANTIC_TESSOUTER) == is_patch);
599
600 if (!is_patch) {
601 stride = get_tcs_out_vertex_dw_stride(ctx);
602 dw_addr = get_tcs_out_current_patch_offset(ctx);
603 dw_addr = get_dw_address_from_generic_indices(ctx, stride, dw_addr,
604 vertex_index, param_index,
605 name, index);
606
607 skip_lds_store = !info->reads_pervertex_outputs;
608 } else {
609 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
610 dw_addr = get_dw_address_from_generic_indices(ctx, NULL, dw_addr,
611 vertex_index, param_index,
612 name, index);
613
614 skip_lds_store = !info->reads_perpatch_outputs;
615
616 if (is_const && const_index == 0) {
617 int name = info->output_semantic_name[driver_location];
618
619 /* Always write tess factors into LDS for the TCS epilog. */
620 if (name == TGSI_SEMANTIC_TESSINNER ||
621 name == TGSI_SEMANTIC_TESSOUTER) {
622 /* The epilog doesn't read LDS if invocation 0 defines tess factors. */
623 skip_lds_store = !info->reads_tessfactor_outputs &&
624 ctx->shader->selector->info.tessfactors_are_def_in_all_invocs;
625 is_tess_factor = true;
626 is_tess_inner = name == TGSI_SEMANTIC_TESSINNER;
627 }
628 }
629 }
630
631 buffer = get_tess_ring_descriptor(ctx, TESS_OFFCHIP_RING_TCS);
632
633 base = ac_get_arg(&ctx->ac, ctx->tcs_offchip_offset);
634
635 addr = get_tcs_tes_buffer_address_from_generic_indices(ctx, vertex_index,
636 param_index, name, index);
637
638 for (unsigned chan = component; chan < 8; chan++) {
639 if (!(writemask & (1 << chan)))
640 continue;
641 LLVMValueRef value = ac_llvm_extract_elem(&ctx->ac, src, chan - component);
642
643 unsigned buffer_store_offset = chan % 4;
644 if (chan == 4) {
645 ubyte name = info->output_semantic_name[driver_location + 1];
646 ubyte index = info->output_semantic_index[driver_location + 1];
647 addr = get_tcs_tes_buffer_address_from_generic_indices(ctx,
648 vertex_index,
649 param_index,
650 name, index);
651 }
652
653 /* Skip LDS stores if there is no LDS read of this output. */
654 if (!skip_lds_store)
655 lshs_lds_store(ctx, chan, dw_addr, value);
656
657 value = ac_to_integer(&ctx->ac, value);
658 values[chan] = value;
659
660 if (writemask != 0xF && !is_tess_factor) {
661 ac_build_buffer_store_dword(&ctx->ac, buffer, value, 1,
662 addr, base,
663 4 * buffer_store_offset,
664 ac_glc);
665 }
666
667 /* Write tess factors into VGPRs for the epilog. */
668 if (is_tess_factor &&
669 ctx->shader->selector->info.tessfactors_are_def_in_all_invocs) {
670 if (!is_tess_inner) {
671 LLVMBuildStore(ctx->ac.builder, value, /* outer */
672 ctx->invoc0_tess_factors[chan]);
673 } else if (chan < 2) {
674 LLVMBuildStore(ctx->ac.builder, value, /* inner */
675 ctx->invoc0_tess_factors[4 + chan]);
676 }
677 }
678 }
679
680 if (writemask == 0xF && !is_tess_factor) {
681 LLVMValueRef value = ac_build_gather_values(&ctx->ac,
682 values, 4);
683 ac_build_buffer_store_dword(&ctx->ac, buffer, value, 4, addr,
684 base, 0, ac_glc);
685 }
686 }
687
688 static LLVMValueRef si_load_tess_coord(struct ac_shader_abi *abi)
689 {
690 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
691 LLVMValueRef coord[4] = {
692 ac_get_arg(&ctx->ac, ctx->tes_u),
693 ac_get_arg(&ctx->ac, ctx->tes_v),
694 ctx->ac.f32_0,
695 ctx->ac.f32_0
696 };
697
698 /* For triangles, the vector should be (u, v, 1-u-v). */
699 if (ctx->shader->selector->info.properties[TGSI_PROPERTY_TES_PRIM_MODE] ==
700 PIPE_PRIM_TRIANGLES) {
701 coord[2] = LLVMBuildFSub(ctx->ac.builder, ctx->ac.f32_1,
702 LLVMBuildFAdd(ctx->ac.builder,
703 coord[0], coord[1], ""), "");
704 }
705 return ac_build_gather_values(&ctx->ac, coord, 4);
706 }
707
708 static LLVMValueRef load_tess_level(struct si_shader_context *ctx,
709 unsigned semantic_name)
710 {
711 LLVMValueRef base, addr;
712
713 int param = si_shader_io_get_unique_index_patch(semantic_name, 0);
714
715 base = ac_get_arg(&ctx->ac, ctx->tcs_offchip_offset);
716 addr = get_tcs_tes_buffer_address(ctx, get_rel_patch_id(ctx), NULL,
717 LLVMConstInt(ctx->i32, param, 0));
718
719 return buffer_load(ctx, ctx->f32,
720 ~0, ctx->tess_offchip_ring, base, addr, true);
721
722 }
723
724 static LLVMValueRef load_tess_level_default(struct si_shader_context *ctx,
725 unsigned semantic_name)
726 {
727 LLVMValueRef buf, slot, val[4];
728 int i, offset;
729
730 slot = LLVMConstInt(ctx->i32, SI_HS_CONST_DEFAULT_TESS_LEVELS, 0);
731 buf = ac_get_arg(&ctx->ac, ctx->rw_buffers);
732 buf = ac_build_load_to_sgpr(&ctx->ac, buf, slot);
733 offset = semantic_name == TGSI_SEMANTIC_TESS_DEFAULT_INNER_LEVEL ? 4 : 0;
734
735 for (i = 0; i < 4; i++)
736 val[i] = si_buffer_load_const(ctx, buf,
737 LLVMConstInt(ctx->i32, (offset + i) * 4, 0));
738 return ac_build_gather_values(&ctx->ac, val, 4);
739 }
740
741 static LLVMValueRef si_load_tess_level(struct ac_shader_abi *abi,
742 unsigned varying_id,
743 bool load_default_state)
744 {
745 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
746 unsigned semantic_name;
747
748 if (load_default_state) {
749 switch (varying_id) {
750 case VARYING_SLOT_TESS_LEVEL_INNER:
751 semantic_name = TGSI_SEMANTIC_TESS_DEFAULT_INNER_LEVEL;
752 break;
753 case VARYING_SLOT_TESS_LEVEL_OUTER:
754 semantic_name = TGSI_SEMANTIC_TESS_DEFAULT_OUTER_LEVEL;
755 break;
756 default:
757 unreachable("unknown tess level");
758 }
759 return load_tess_level_default(ctx, semantic_name);
760 }
761
762 switch (varying_id) {
763 case VARYING_SLOT_TESS_LEVEL_INNER:
764 semantic_name = TGSI_SEMANTIC_TESSINNER;
765 break;
766 case VARYING_SLOT_TESS_LEVEL_OUTER:
767 semantic_name = TGSI_SEMANTIC_TESSOUTER;
768 break;
769 default:
770 unreachable("unknown tess level");
771 }
772
773 return load_tess_level(ctx, semantic_name);
774
775 }
776
777 static LLVMValueRef si_load_patch_vertices_in(struct ac_shader_abi *abi)
778 {
779 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
780 if (ctx->type == PIPE_SHADER_TESS_CTRL)
781 return si_unpack_param(ctx, ctx->tcs_out_lds_layout, 13, 6);
782 else if (ctx->type == PIPE_SHADER_TESS_EVAL)
783 return get_num_tcs_out_vertices(ctx);
784 else
785 unreachable("invalid shader stage for TGSI_SEMANTIC_VERTICESIN");
786 }
787
788 /**
789 * Forward all outputs from the vertex shader to the TES. This is only used
790 * for the fixed function TCS.
791 */
792 static void si_copy_tcs_inputs(struct si_shader_context *ctx)
793 {
794 LLVMValueRef invocation_id, buffer, buffer_offset;
795 LLVMValueRef lds_vertex_stride, lds_base;
796 uint64_t inputs;
797
798 invocation_id = si_unpack_param(ctx, ctx->args.tcs_rel_ids, 8, 5);
799 buffer = get_tess_ring_descriptor(ctx, TESS_OFFCHIP_RING_TCS);
800 buffer_offset = ac_get_arg(&ctx->ac, ctx->tcs_offchip_offset);
801
802 lds_vertex_stride = get_tcs_in_vertex_dw_stride(ctx);
803 lds_base = get_tcs_in_current_patch_offset(ctx);
804 lds_base = ac_build_imad(&ctx->ac, invocation_id, lds_vertex_stride,
805 lds_base);
806
807 inputs = ctx->shader->key.mono.u.ff_tcs_inputs_to_copy;
808 while (inputs) {
809 unsigned i = u_bit_scan64(&inputs);
810
811 LLVMValueRef lds_ptr = LLVMBuildAdd(ctx->ac.builder, lds_base,
812 LLVMConstInt(ctx->i32, 4 * i, 0),
813 "");
814
815 LLVMValueRef buffer_addr = get_tcs_tes_buffer_address(ctx,
816 get_rel_patch_id(ctx),
817 invocation_id,
818 LLVMConstInt(ctx->i32, i, 0));
819
820 LLVMValueRef value = lshs_lds_load(ctx, ctx->ac.i32, ~0, lds_ptr);
821
822 ac_build_buffer_store_dword(&ctx->ac, buffer, value, 4, buffer_addr,
823 buffer_offset, 0, ac_glc);
824 }
825 }
826
827 static void si_write_tess_factors(struct si_shader_context *ctx,
828 LLVMValueRef rel_patch_id,
829 LLVMValueRef invocation_id,
830 LLVMValueRef tcs_out_current_patch_data_offset,
831 LLVMValueRef invoc0_tf_outer[4],
832 LLVMValueRef invoc0_tf_inner[2])
833 {
834 struct si_shader *shader = ctx->shader;
835 unsigned tess_inner_index, tess_outer_index;
836 LLVMValueRef lds_base, lds_inner, lds_outer, byteoffset, buffer;
837 LLVMValueRef out[6], vec0, vec1, tf_base, inner[4], outer[4];
838 unsigned stride, outer_comps, inner_comps, i, offset;
839
840 /* Add a barrier before loading tess factors from LDS. */
841 if (!shader->key.part.tcs.epilog.invoc0_tess_factors_are_def)
842 si_llvm_emit_barrier(ctx);
843
844 /* Do this only for invocation 0, because the tess levels are per-patch,
845 * not per-vertex.
846 *
847 * This can't jump, because invocation 0 executes this. It should
848 * at least mask out the loads and stores for other invocations.
849 */
850 ac_build_ifcc(&ctx->ac,
851 LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
852 invocation_id, ctx->i32_0, ""), 6503);
853
854 /* Determine the layout of one tess factor element in the buffer. */
855 switch (shader->key.part.tcs.epilog.prim_mode) {
856 case PIPE_PRIM_LINES:
857 stride = 2; /* 2 dwords, 1 vec2 store */
858 outer_comps = 2;
859 inner_comps = 0;
860 break;
861 case PIPE_PRIM_TRIANGLES:
862 stride = 4; /* 4 dwords, 1 vec4 store */
863 outer_comps = 3;
864 inner_comps = 1;
865 break;
866 case PIPE_PRIM_QUADS:
867 stride = 6; /* 6 dwords, 2 stores (vec4 + vec2) */
868 outer_comps = 4;
869 inner_comps = 2;
870 break;
871 default:
872 assert(0);
873 return;
874 }
875
876 for (i = 0; i < 4; i++) {
877 inner[i] = LLVMGetUndef(ctx->i32);
878 outer[i] = LLVMGetUndef(ctx->i32);
879 }
880
881 if (shader->key.part.tcs.epilog.invoc0_tess_factors_are_def) {
882 /* Tess factors are in VGPRs. */
883 for (i = 0; i < outer_comps; i++)
884 outer[i] = out[i] = invoc0_tf_outer[i];
885 for (i = 0; i < inner_comps; i++)
886 inner[i] = out[outer_comps+i] = invoc0_tf_inner[i];
887 } else {
888 /* Load tess_inner and tess_outer from LDS.
889 * Any invocation can write them, so we can't get them from a temporary.
890 */
891 tess_inner_index = si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSINNER, 0);
892 tess_outer_index = si_shader_io_get_unique_index_patch(TGSI_SEMANTIC_TESSOUTER, 0);
893
894 lds_base = tcs_out_current_patch_data_offset;
895 lds_inner = LLVMBuildAdd(ctx->ac.builder, lds_base,
896 LLVMConstInt(ctx->i32,
897 tess_inner_index * 4, 0), "");
898 lds_outer = LLVMBuildAdd(ctx->ac.builder, lds_base,
899 LLVMConstInt(ctx->i32,
900 tess_outer_index * 4, 0), "");
901
902 for (i = 0; i < outer_comps; i++) {
903 outer[i] = out[i] =
904 lshs_lds_load(ctx, ctx->ac.i32, i, lds_outer);
905 }
906 for (i = 0; i < inner_comps; i++) {
907 inner[i] = out[outer_comps+i] =
908 lshs_lds_load(ctx, ctx->ac.i32, i, lds_inner);
909 }
910 }
911
912 if (shader->key.part.tcs.epilog.prim_mode == PIPE_PRIM_LINES) {
913 /* For isolines, the hardware expects tess factors in the
914 * reverse order from what NIR specifies.
915 */
916 LLVMValueRef tmp = out[0];
917 out[0] = out[1];
918 out[1] = tmp;
919 }
920
921 /* Convert the outputs to vectors for stores. */
922 vec0 = ac_build_gather_values(&ctx->ac, out, MIN2(stride, 4));
923 vec1 = NULL;
924
925 if (stride > 4)
926 vec1 = ac_build_gather_values(&ctx->ac, out+4, stride - 4);
927
928 /* Get the buffer. */
929 buffer = get_tess_ring_descriptor(ctx, TCS_FACTOR_RING);
930
931 /* Get the offset. */
932 tf_base = ac_get_arg(&ctx->ac,
933 ctx->tcs_factor_offset);
934 byteoffset = LLVMBuildMul(ctx->ac.builder, rel_patch_id,
935 LLVMConstInt(ctx->i32, 4 * stride, 0), "");
936
937 ac_build_ifcc(&ctx->ac,
938 LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
939 rel_patch_id, ctx->i32_0, ""), 6504);
940
941 /* Store the dynamic HS control word. */
942 offset = 0;
943 if (ctx->screen->info.chip_class <= GFX8) {
944 ac_build_buffer_store_dword(&ctx->ac, buffer,
945 LLVMConstInt(ctx->i32, 0x80000000, 0),
946 1, ctx->i32_0, tf_base,
947 offset, ac_glc);
948 offset += 4;
949 }
950
951 ac_build_endif(&ctx->ac, 6504);
952
953 /* Store the tessellation factors. */
954 ac_build_buffer_store_dword(&ctx->ac, buffer, vec0,
955 MIN2(stride, 4), byteoffset, tf_base,
956 offset, ac_glc);
957 offset += 16;
958 if (vec1)
959 ac_build_buffer_store_dword(&ctx->ac, buffer, vec1,
960 stride - 4, byteoffset, tf_base,
961 offset, ac_glc);
962
963 /* Store the tess factors into the offchip buffer if TES reads them. */
964 if (shader->key.part.tcs.epilog.tes_reads_tess_factors) {
965 LLVMValueRef buf, base, inner_vec, outer_vec, tf_outer_offset;
966 LLVMValueRef tf_inner_offset;
967 unsigned param_outer, param_inner;
968
969 buf = get_tess_ring_descriptor(ctx, TESS_OFFCHIP_RING_TCS);
970 base = ac_get_arg(&ctx->ac, ctx->tcs_offchip_offset);
971
972 param_outer = si_shader_io_get_unique_index_patch(
973 TGSI_SEMANTIC_TESSOUTER, 0);
974 tf_outer_offset = get_tcs_tes_buffer_address(ctx, rel_patch_id, NULL,
975 LLVMConstInt(ctx->i32, param_outer, 0));
976
977 unsigned outer_vec_size =
978 ac_has_vec3_support(ctx->screen->info.chip_class, false) ?
979 outer_comps : util_next_power_of_two(outer_comps);
980 outer_vec = ac_build_gather_values(&ctx->ac, outer, outer_vec_size);
981
982 ac_build_buffer_store_dword(&ctx->ac, buf, outer_vec,
983 outer_comps, tf_outer_offset,
984 base, 0, ac_glc);
985 if (inner_comps) {
986 param_inner = si_shader_io_get_unique_index_patch(
987 TGSI_SEMANTIC_TESSINNER, 0);
988 tf_inner_offset = get_tcs_tes_buffer_address(ctx, rel_patch_id, NULL,
989 LLVMConstInt(ctx->i32, param_inner, 0));
990
991 inner_vec = inner_comps == 1 ? inner[0] :
992 ac_build_gather_values(&ctx->ac, inner, inner_comps);
993 ac_build_buffer_store_dword(&ctx->ac, buf, inner_vec,
994 inner_comps, tf_inner_offset,
995 base, 0, ac_glc);
996 }
997 }
998
999 ac_build_endif(&ctx->ac, 6503);
1000 }
1001
1002 /* This only writes the tessellation factor levels. */
1003 static void si_llvm_emit_tcs_epilogue(struct ac_shader_abi *abi,
1004 unsigned max_outputs,
1005 LLVMValueRef *addrs)
1006 {
1007 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1008 LLVMBuilderRef builder = ctx->ac.builder;
1009 LLVMValueRef rel_patch_id, invocation_id, tf_lds_offset;
1010
1011 si_copy_tcs_inputs(ctx);
1012
1013 rel_patch_id = get_rel_patch_id(ctx);
1014 invocation_id = si_unpack_param(ctx, ctx->args.tcs_rel_ids, 8, 5);
1015 tf_lds_offset = get_tcs_out_current_patch_data_offset(ctx);
1016
1017 if (ctx->screen->info.chip_class >= GFX9) {
1018 LLVMBasicBlockRef blocks[2] = {
1019 LLVMGetInsertBlock(builder),
1020 ctx->merged_wrap_if_entry_block
1021 };
1022 LLVMValueRef values[2];
1023
1024 ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);
1025
1026 values[0] = rel_patch_id;
1027 values[1] = LLVMGetUndef(ctx->i32);
1028 rel_patch_id = ac_build_phi(&ctx->ac, ctx->i32, 2, values, blocks);
1029
1030 values[0] = tf_lds_offset;
1031 values[1] = LLVMGetUndef(ctx->i32);
1032 tf_lds_offset = ac_build_phi(&ctx->ac, ctx->i32, 2, values, blocks);
1033
1034 values[0] = invocation_id;
1035 values[1] = ctx->i32_1; /* cause the epilog to skip threads */
1036 invocation_id = ac_build_phi(&ctx->ac, ctx->i32, 2, values, blocks);
1037 }
1038
1039 /* Return epilog parameters from this function. */
1040 LLVMValueRef ret = ctx->return_value;
1041 unsigned vgpr;
1042
1043 if (ctx->screen->info.chip_class >= GFX9) {
1044 ret = si_insert_input_ret(ctx, ret, ctx->tcs_offchip_layout,
1045 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT);
1046 ret = si_insert_input_ret(ctx, ret, ctx->tcs_out_lds_layout,
1047 8 + GFX9_SGPR_TCS_OUT_LAYOUT);
1048 /* Tess offchip and tess factor offsets are at the beginning. */
1049 ret = si_insert_input_ret(ctx, ret, ctx->tcs_offchip_offset, 2);
1050 ret = si_insert_input_ret(ctx, ret, ctx->tcs_factor_offset, 4);
1051 vgpr = 8 + GFX9_SGPR_TCS_OUT_LAYOUT + 1;
1052 } else {
1053 ret = si_insert_input_ret(ctx, ret, ctx->tcs_offchip_layout,
1054 GFX6_SGPR_TCS_OFFCHIP_LAYOUT);
1055 ret = si_insert_input_ret(ctx, ret, ctx->tcs_out_lds_layout,
1056 GFX6_SGPR_TCS_OUT_LAYOUT);
1057 /* Tess offchip and tess factor offsets are after user SGPRs. */
1058 ret = si_insert_input_ret(ctx, ret, ctx->tcs_offchip_offset,
1059 GFX6_TCS_NUM_USER_SGPR);
1060 ret = si_insert_input_ret(ctx, ret, ctx->tcs_factor_offset,
1061 GFX6_TCS_NUM_USER_SGPR + 1);
1062 vgpr = GFX6_TCS_NUM_USER_SGPR + 2;
1063 }
1064
1065 /* VGPRs */
1066 rel_patch_id = ac_to_float(&ctx->ac, rel_patch_id);
1067 invocation_id = ac_to_float(&ctx->ac, invocation_id);
1068 tf_lds_offset = ac_to_float(&ctx->ac, tf_lds_offset);
1069
1070 /* Leave a hole corresponding to the two input VGPRs. This ensures that
1071 * the invocation_id output does not alias the tcs_rel_ids input,
1072 * which saves a V_MOV on gfx9.
1073 */
1074 vgpr += 2;
1075
1076 ret = LLVMBuildInsertValue(builder, ret, rel_patch_id, vgpr++, "");
1077 ret = LLVMBuildInsertValue(builder, ret, invocation_id, vgpr++, "");
1078
1079 if (ctx->shader->selector->info.tessfactors_are_def_in_all_invocs) {
1080 vgpr++; /* skip the tess factor LDS offset */
1081 for (unsigned i = 0; i < 6; i++) {
1082 LLVMValueRef value =
1083 LLVMBuildLoad(builder, ctx->invoc0_tess_factors[i], "");
1084 value = ac_to_float(&ctx->ac, value);
1085 ret = LLVMBuildInsertValue(builder, ret, value, vgpr++, "");
1086 }
1087 } else {
1088 ret = LLVMBuildInsertValue(builder, ret, tf_lds_offset, vgpr++, "");
1089 }
1090 ctx->return_value = ret;
1091 }
1092
1093 /* Pass TCS inputs from LS to TCS on GFX9. */
1094 static void si_set_ls_return_value_for_tcs(struct si_shader_context *ctx)
1095 {
1096 LLVMValueRef ret = ctx->return_value;
1097
1098 ret = si_insert_input_ptr(ctx, ret, ctx->other_const_and_shader_buffers, 0);
1099 ret = si_insert_input_ptr(ctx, ret, ctx->other_samplers_and_images, 1);
1100 ret = si_insert_input_ret(ctx, ret, ctx->tcs_offchip_offset, 2);
1101 ret = si_insert_input_ret(ctx, ret, ctx->merged_wave_info, 3);
1102 ret = si_insert_input_ret(ctx, ret, ctx->tcs_factor_offset, 4);
1103 ret = si_insert_input_ret(ctx, ret, ctx->merged_scratch_offset, 5);
1104
1105 ret = si_insert_input_ptr(ctx, ret, ctx->rw_buffers,
1106 8 + SI_SGPR_RW_BUFFERS);
1107 ret = si_insert_input_ptr(ctx, ret,
1108 ctx->bindless_samplers_and_images,
1109 8 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES);
1110
1111 ret = si_insert_input_ret(ctx, ret, ctx->vs_state_bits,
1112 8 + SI_SGPR_VS_STATE_BITS);
1113
1114 ret = si_insert_input_ret(ctx, ret, ctx->tcs_offchip_layout,
1115 8 + GFX9_SGPR_TCS_OFFCHIP_LAYOUT);
1116 ret = si_insert_input_ret(ctx, ret, ctx->tcs_out_lds_offsets,
1117 8 + GFX9_SGPR_TCS_OUT_OFFSETS);
1118 ret = si_insert_input_ret(ctx, ret, ctx->tcs_out_lds_layout,
1119 8 + GFX9_SGPR_TCS_OUT_LAYOUT);
1120
1121 unsigned vgpr = 8 + GFX9_TCS_NUM_USER_SGPR;
1122 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
1123 ac_to_float(&ctx->ac,
1124 ac_get_arg(&ctx->ac, ctx->args.tcs_patch_id)),
1125 vgpr++, "");
1126 ret = LLVMBuildInsertValue(ctx->ac.builder, ret,
1127 ac_to_float(&ctx->ac,
1128 ac_get_arg(&ctx->ac, ctx->args.tcs_rel_ids)),
1129 vgpr++, "");
1130 ctx->return_value = ret;
1131 }
1132
1133 void si_llvm_emit_ls_epilogue(struct ac_shader_abi *abi, unsigned max_outputs,
1134 LLVMValueRef *addrs)
1135 {
1136 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1137 struct si_shader *shader = ctx->shader;
1138 struct si_shader_info *info = &shader->selector->info;
1139 unsigned i, chan;
1140 LLVMValueRef vertex_id = ac_get_arg(&ctx->ac, ctx->rel_auto_id);
1141 LLVMValueRef vertex_dw_stride = get_tcs_in_vertex_dw_stride(ctx);
1142 LLVMValueRef base_dw_addr = LLVMBuildMul(ctx->ac.builder, vertex_id,
1143 vertex_dw_stride, "");
1144
1145 /* Write outputs to LDS. The next shader (TCS aka HS) will read
1146 * its inputs from it. */
1147 for (i = 0; i < info->num_outputs; i++) {
1148 unsigned name = info->output_semantic_name[i];
1149 unsigned index = info->output_semantic_index[i];
1150
1151 /* The ARB_shader_viewport_layer_array spec contains the
1152 * following issue:
1153 *
1154 * 2) What happens if gl_ViewportIndex or gl_Layer is
1155 * written in the vertex shader and a geometry shader is
1156 * present?
1157 *
1158 * RESOLVED: The value written by the last vertex processing
1159 * stage is used. If the last vertex processing stage
1160 * (vertex, tessellation evaluation or geometry) does not
1161 * statically assign to gl_ViewportIndex or gl_Layer, index
1162 * or layer zero is assumed.
1163 *
1164 * So writes to those outputs in VS-as-LS are simply ignored.
1165 */
1166 if (name == TGSI_SEMANTIC_LAYER ||
1167 name == TGSI_SEMANTIC_VIEWPORT_INDEX)
1168 continue;
1169
1170 int param = si_shader_io_get_unique_index(name, index, false);
1171 LLVMValueRef dw_addr = LLVMBuildAdd(ctx->ac.builder, base_dw_addr,
1172 LLVMConstInt(ctx->i32, param * 4, 0), "");
1173
1174 for (chan = 0; chan < 4; chan++) {
1175 if (!(info->output_usagemask[i] & (1 << chan)))
1176 continue;
1177
1178 lshs_lds_store(ctx, chan, dw_addr,
1179 LLVMBuildLoad(ctx->ac.builder, addrs[4 * i + chan], ""));
1180 }
1181 }
1182
1183 if (ctx->screen->info.chip_class >= GFX9)
1184 si_set_ls_return_value_for_tcs(ctx);
1185 }
1186
1187 /**
1188 * Compile the TCS epilog function. This writes tesselation factors to memory
1189 * based on the output primitive type of the tesselator (determined by TES).
1190 */
1191 void si_llvm_build_tcs_epilog(struct si_shader_context *ctx,
1192 union si_shader_part_key *key)
1193 {
1194 memset(&ctx->args, 0, sizeof(ctx->args));
1195
1196 if (ctx->screen->info.chip_class >= GFX9) {
1197 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1198 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1199 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
1200 &ctx->tcs_offchip_offset);
1201 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL); /* wave info */
1202 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
1203 &ctx->tcs_factor_offset);
1204 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1205 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1206 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1207 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1208 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1209 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1210 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1211 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1212 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1213 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1214 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1215 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
1216 &ctx->tcs_offchip_layout);
1217 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1218 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
1219 &ctx->tcs_out_lds_layout);
1220 } else {
1221 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1222 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1223 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1224 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1225 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
1226 &ctx->tcs_offchip_layout);
1227 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1228 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
1229 &ctx->tcs_out_lds_layout);
1230 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
1231 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
1232 &ctx->tcs_offchip_offset);
1233 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
1234 &ctx->tcs_factor_offset);
1235 }
1236
1237 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, NULL); /* VGPR gap */
1238 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, NULL); /* VGPR gap */
1239 struct ac_arg rel_patch_id; /* patch index within the wave (REL_PATCH_ID) */
1240 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &rel_patch_id);
1241 struct ac_arg invocation_id; /* invocation ID within the patch */
1242 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &invocation_id);
1243 struct ac_arg tcs_out_current_patch_data_offset; /* LDS offset where tess factors should be loaded from */
1244 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT,
1245 &tcs_out_current_patch_data_offset);
1246
1247 struct ac_arg tess_factors[6];
1248 for (unsigned i = 0; i < 6; i++)
1249 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &tess_factors[i]);
1250
1251 /* Create the function. */
1252 si_llvm_create_func(ctx, "tcs_epilog", NULL, 0,
1253 ctx->screen->info.chip_class >= GFX7 ? 128 : 0);
1254 ac_declare_lds_as_pointer(&ctx->ac);
1255
1256 LLVMValueRef invoc0_tess_factors[6];
1257 for (unsigned i = 0; i < 6; i++)
1258 invoc0_tess_factors[i] = ac_get_arg(&ctx->ac, tess_factors[i]);
1259
1260 si_write_tess_factors(ctx,
1261 ac_get_arg(&ctx->ac, rel_patch_id),
1262 ac_get_arg(&ctx->ac, invocation_id),
1263 ac_get_arg(&ctx->ac, tcs_out_current_patch_data_offset),
1264 invoc0_tess_factors, invoc0_tess_factors + 4);
1265
1266 LLVMBuildRetVoid(ctx->ac.builder);
1267 }
1268
1269 void si_llvm_init_tcs_callbacks(struct si_shader_context *ctx)
1270 {
1271 ctx->abi.load_tess_varyings = si_nir_load_tcs_varyings;
1272 ctx->abi.load_tess_level = si_load_tess_level;
1273 ctx->abi.store_tcs_outputs = si_nir_store_output_tcs;
1274 ctx->abi.emit_outputs = si_llvm_emit_tcs_epilogue;
1275 ctx->abi.load_patch_vertices_in = si_load_patch_vertices_in;
1276 }
1277
1278 void si_llvm_init_tes_callbacks(struct si_shader_context *ctx)
1279 {
1280 ctx->abi.load_tess_varyings = si_nir_load_input_tes;
1281 ctx->abi.load_tess_coord = si_load_tess_coord;
1282 ctx->abi.load_tess_level = si_load_tess_level;
1283 ctx->abi.load_patch_vertices_in = si_load_patch_vertices_in;
1284 }