radeonsi: don't call build_gep0 in si_llvm_emit_ddxy on VI
[mesa.git] / src / gallium / drivers / radeonsi / si_shader.c
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
27 */
28
29 #include "gallivm/lp_bld_const.h"
30 #include "gallivm/lp_bld_gather.h"
31 #include "gallivm/lp_bld_intr.h"
32 #include "gallivm/lp_bld_logic.h"
33 #include "gallivm/lp_bld_arit.h"
34 #include "gallivm/lp_bld_flow.h"
35 #include "gallivm/lp_bld_misc.h"
36 #include "radeon/radeon_llvm.h"
37 #include "radeon/radeon_elf_util.h"
38 #include "radeon/radeon_llvm_emit.h"
39 #include "util/u_memory.h"
40 #include "util/u_string.h"
41 #include "tgsi/tgsi_build.h"
42 #include "tgsi/tgsi_util.h"
43 #include "tgsi/tgsi_dump.h"
44
45 #include "si_pipe.h"
46 #include "sid.h"
47
48
49 static const char *scratch_rsrc_dword0_symbol =
50 "SCRATCH_RSRC_DWORD0";
51
52 static const char *scratch_rsrc_dword1_symbol =
53 "SCRATCH_RSRC_DWORD1";
54
55 struct si_shader_output_values
56 {
57 LLVMValueRef values[4];
58 unsigned name;
59 unsigned sid;
60 };
61
62 struct si_shader_context
63 {
64 struct radeon_llvm_context radeon_bld;
65 struct si_shader *shader;
66 struct si_screen *screen;
67
68 unsigned type; /* PIPE_SHADER_* specifies the type of shader. */
69 bool is_gs_copy_shader;
70
71 /* Whether to generate the optimized shader variant compiled as a whole
72 * (without a prolog and epilog)
73 */
74 bool is_monolithic;
75
76 int param_streamout_config;
77 int param_streamout_write_index;
78 int param_streamout_offset[4];
79 int param_vertex_id;
80 int param_rel_auto_id;
81 int param_vs_prim_id;
82 int param_instance_id;
83 int param_vertex_index0;
84 int param_tes_u;
85 int param_tes_v;
86 int param_tes_rel_patch_id;
87 int param_tes_patch_id;
88 int param_es2gs_offset;
89 int param_oc_lds;
90
91 /* Sets a bit if the dynamic HS control word was 0x80000000. The bit is
92 * 0x800000 for VS, 0x1 for ES.
93 */
94 int param_tess_offchip;
95
96 LLVMTargetMachineRef tm;
97
98 unsigned invariant_load_md_kind;
99 unsigned range_md_kind;
100 unsigned uniform_md_kind;
101 LLVMValueRef empty_md;
102
103 /* Preloaded descriptors. */
104 LLVMValueRef esgs_ring;
105 LLVMValueRef gsvs_ring[4];
106
107 LLVMValueRef lds;
108 LLVMValueRef gs_next_vertex[4];
109 LLVMValueRef return_value;
110
111 LLVMTypeRef voidt;
112 LLVMTypeRef i1;
113 LLVMTypeRef i8;
114 LLVMTypeRef i32;
115 LLVMTypeRef i64;
116 LLVMTypeRef i128;
117 LLVMTypeRef f32;
118 LLVMTypeRef v16i8;
119 LLVMTypeRef v2i32;
120 LLVMTypeRef v4i32;
121 LLVMTypeRef v4f32;
122 LLVMTypeRef v8i32;
123
124 LLVMValueRef shared_memory;
125 };
126
127 static struct si_shader_context *si_shader_context(
128 struct lp_build_tgsi_context *bld_base)
129 {
130 return (struct si_shader_context *)bld_base;
131 }
132
133 static void si_init_shader_ctx(struct si_shader_context *ctx,
134 struct si_screen *sscreen,
135 struct si_shader *shader,
136 LLVMTargetMachineRef tm);
137
138 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action *action,
139 struct lp_build_tgsi_context *bld_base,
140 struct lp_build_emit_data *emit_data);
141
142 static void si_dump_shader_key(unsigned shader, union si_shader_key *key,
143 FILE *f);
144
145 /* Ideally pass the sample mask input to the PS epilog as v13, which
146 * is its usual location, so that the shader doesn't have to add v_mov.
147 */
148 #define PS_EPILOG_SAMPLEMASK_MIN_LOC 13
149
150 /* The VS location of the PrimitiveID input is the same in the epilog,
151 * so that the main shader part doesn't have to move it.
152 */
153 #define VS_EPILOG_PRIMID_LOC 2
154
155 enum {
156 CONST_ADDR_SPACE = 2,
157 LOCAL_ADDR_SPACE = 3,
158 };
159
160 #define SENDMSG_GS 2
161 #define SENDMSG_GS_DONE 3
162
163 #define SENDMSG_GS_OP_NOP (0 << 4)
164 #define SENDMSG_GS_OP_CUT (1 << 4)
165 #define SENDMSG_GS_OP_EMIT (2 << 4)
166 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
167
168 /**
169 * Returns a unique index for a semantic name and index. The index must be
170 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
171 * calculated.
172 */
173 unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
174 {
175 switch (semantic_name) {
176 case TGSI_SEMANTIC_POSITION:
177 return 0;
178 case TGSI_SEMANTIC_PSIZE:
179 return 1;
180 case TGSI_SEMANTIC_CLIPDIST:
181 assert(index <= 1);
182 return 2 + index;
183 case TGSI_SEMANTIC_GENERIC:
184 if (index <= 63-4)
185 return 4 + index;
186 else
187 /* same explanation as in the default statement,
188 * the only user hitting this is st/nine.
189 */
190 return 0;
191
192 /* patch indices are completely separate and thus start from 0 */
193 case TGSI_SEMANTIC_TESSOUTER:
194 return 0;
195 case TGSI_SEMANTIC_TESSINNER:
196 return 1;
197 case TGSI_SEMANTIC_PATCH:
198 return 2 + index;
199
200 default:
201 /* Don't fail here. The result of this function is only used
202 * for LS, TCS, TES, and GS, where legacy GL semantics can't
203 * occur, but this function is called for all vertex shaders
204 * before it's known whether LS will be compiled or not.
205 */
206 return 0;
207 }
208 }
209
210 /**
211 * Get the value of a shader input parameter and extract a bitfield.
212 */
213 static LLVMValueRef unpack_param(struct si_shader_context *ctx,
214 unsigned param, unsigned rshift,
215 unsigned bitwidth)
216 {
217 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
218 LLVMValueRef value = LLVMGetParam(ctx->radeon_bld.main_fn,
219 param);
220
221 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMFloatTypeKind)
222 value = bitcast(&ctx->radeon_bld.soa.bld_base,
223 TGSI_TYPE_UNSIGNED, value);
224
225 if (rshift)
226 value = LLVMBuildLShr(gallivm->builder, value,
227 lp_build_const_int32(gallivm, rshift), "");
228
229 if (rshift + bitwidth < 32) {
230 unsigned mask = (1 << bitwidth) - 1;
231 value = LLVMBuildAnd(gallivm->builder, value,
232 lp_build_const_int32(gallivm, mask), "");
233 }
234
235 return value;
236 }
237
238 static LLVMValueRef get_rel_patch_id(struct si_shader_context *ctx)
239 {
240 switch (ctx->type) {
241 case PIPE_SHADER_TESS_CTRL:
242 return unpack_param(ctx, SI_PARAM_REL_IDS, 0, 8);
243
244 case PIPE_SHADER_TESS_EVAL:
245 return LLVMGetParam(ctx->radeon_bld.main_fn,
246 ctx->param_tes_rel_patch_id);
247
248 default:
249 assert(0);
250 return NULL;
251 }
252 }
253
254 /* Tessellation shaders pass outputs to the next shader using LDS.
255 *
256 * LS outputs = TCS inputs
257 * TCS outputs = TES inputs
258 *
259 * The LDS layout is:
260 * - TCS inputs for patch 0
261 * - TCS inputs for patch 1
262 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
263 * - ...
264 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
265 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
266 * - TCS outputs for patch 1
267 * - Per-patch TCS outputs for patch 1
268 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
269 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
270 * - ...
271 *
272 * All three shaders VS(LS), TCS, TES share the same LDS space.
273 */
274
275 static LLVMValueRef
276 get_tcs_in_patch_stride(struct si_shader_context *ctx)
277 {
278 if (ctx->type == PIPE_SHADER_VERTEX)
279 return unpack_param(ctx, SI_PARAM_LS_OUT_LAYOUT, 0, 13);
280 else if (ctx->type == PIPE_SHADER_TESS_CTRL)
281 return unpack_param(ctx, SI_PARAM_TCS_IN_LAYOUT, 0, 13);
282 else {
283 assert(0);
284 return NULL;
285 }
286 }
287
288 static LLVMValueRef
289 get_tcs_out_patch_stride(struct si_shader_context *ctx)
290 {
291 return unpack_param(ctx, SI_PARAM_TCS_OUT_LAYOUT, 0, 13);
292 }
293
294 static LLVMValueRef
295 get_tcs_out_patch0_offset(struct si_shader_context *ctx)
296 {
297 return lp_build_mul_imm(&ctx->radeon_bld.soa.bld_base.uint_bld,
298 unpack_param(ctx,
299 SI_PARAM_TCS_OUT_OFFSETS,
300 0, 16),
301 4);
302 }
303
304 static LLVMValueRef
305 get_tcs_out_patch0_patch_data_offset(struct si_shader_context *ctx)
306 {
307 return lp_build_mul_imm(&ctx->radeon_bld.soa.bld_base.uint_bld,
308 unpack_param(ctx,
309 SI_PARAM_TCS_OUT_OFFSETS,
310 16, 16),
311 4);
312 }
313
314 static LLVMValueRef
315 get_tcs_in_current_patch_offset(struct si_shader_context *ctx)
316 {
317 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
318 LLVMValueRef patch_stride = get_tcs_in_patch_stride(ctx);
319 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
320
321 return LLVMBuildMul(gallivm->builder, patch_stride, rel_patch_id, "");
322 }
323
324 static LLVMValueRef
325 get_tcs_out_current_patch_offset(struct si_shader_context *ctx)
326 {
327 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
328 LLVMValueRef patch0_offset = get_tcs_out_patch0_offset(ctx);
329 LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
330 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
331
332 return LLVMBuildAdd(gallivm->builder, patch0_offset,
333 LLVMBuildMul(gallivm->builder, patch_stride,
334 rel_patch_id, ""),
335 "");
336 }
337
338 static LLVMValueRef
339 get_tcs_out_current_patch_data_offset(struct si_shader_context *ctx)
340 {
341 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
342 LLVMValueRef patch0_patch_data_offset =
343 get_tcs_out_patch0_patch_data_offset(ctx);
344 LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
345 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
346
347 return LLVMBuildAdd(gallivm->builder, patch0_patch_data_offset,
348 LLVMBuildMul(gallivm->builder, patch_stride,
349 rel_patch_id, ""),
350 "");
351 }
352
353 static LLVMValueRef build_gep0(struct si_shader_context *ctx,
354 LLVMValueRef base_ptr, LLVMValueRef index)
355 {
356 LLVMValueRef indices[2] = {
357 LLVMConstInt(ctx->i32, 0, 0),
358 index,
359 };
360 return LLVMBuildGEP(ctx->radeon_bld.gallivm.builder, base_ptr,
361 indices, 2, "");
362 }
363
364 static void build_indexed_store(struct si_shader_context *ctx,
365 LLVMValueRef base_ptr, LLVMValueRef index,
366 LLVMValueRef value)
367 {
368 struct lp_build_tgsi_context *bld_base = &ctx->radeon_bld.soa.bld_base;
369 struct gallivm_state *gallivm = bld_base->base.gallivm;
370
371 LLVMBuildStore(gallivm->builder, value,
372 build_gep0(ctx, base_ptr, index));
373 }
374
375 /**
376 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
377 * It's equivalent to doing a load from &base_ptr[index].
378 *
379 * \param base_ptr Where the array starts.
380 * \param index The element index into the array.
381 * \param uniform Whether the base_ptr and index can be assumed to be
382 * dynamically uniform
383 */
384 static LLVMValueRef build_indexed_load(struct si_shader_context *ctx,
385 LLVMValueRef base_ptr, LLVMValueRef index,
386 bool uniform)
387 {
388 struct lp_build_tgsi_context *bld_base = &ctx->radeon_bld.soa.bld_base;
389 struct gallivm_state *gallivm = bld_base->base.gallivm;
390 LLVMValueRef pointer;
391
392 pointer = build_gep0(ctx, base_ptr, index);
393 if (uniform)
394 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
395 return LLVMBuildLoad(gallivm->builder, pointer, "");
396 }
397
398 /**
399 * Do a load from &base_ptr[index], but also add a flag that it's loading
400 * a constant from a dynamically uniform index.
401 */
402 static LLVMValueRef build_indexed_load_const(
403 struct si_shader_context *ctx,
404 LLVMValueRef base_ptr, LLVMValueRef index)
405 {
406 LLVMValueRef result = build_indexed_load(ctx, base_ptr, index, true);
407 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
408 return result;
409 }
410
411 static LLVMValueRef get_instance_index_for_fetch(
412 struct radeon_llvm_context *radeon_bld,
413 unsigned param_start_instance, unsigned divisor)
414 {
415 struct si_shader_context *ctx =
416 si_shader_context(&radeon_bld->soa.bld_base);
417 struct gallivm_state *gallivm = radeon_bld->soa.bld_base.base.gallivm;
418
419 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
420 ctx->param_instance_id);
421
422 /* The division must be done before START_INSTANCE is added. */
423 if (divisor > 1)
424 result = LLVMBuildUDiv(gallivm->builder, result,
425 lp_build_const_int32(gallivm, divisor), "");
426
427 return LLVMBuildAdd(gallivm->builder, result,
428 LLVMGetParam(radeon_bld->main_fn, param_start_instance), "");
429 }
430
431 static void declare_input_vs(
432 struct radeon_llvm_context *radeon_bld,
433 unsigned input_index,
434 const struct tgsi_full_declaration *decl,
435 LLVMValueRef out[4])
436 {
437 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
438 struct gallivm_state *gallivm = base->gallivm;
439 struct si_shader_context *ctx =
440 si_shader_context(&radeon_bld->soa.bld_base);
441 unsigned divisor =
442 ctx->shader->key.vs.prolog.instance_divisors[input_index];
443
444 unsigned chan;
445
446 LLVMValueRef t_list_ptr;
447 LLVMValueRef t_offset;
448 LLVMValueRef t_list;
449 LLVMValueRef attribute_offset;
450 LLVMValueRef buffer_index;
451 LLVMValueRef args[3];
452 LLVMValueRef input;
453
454 /* Load the T list */
455 t_list_ptr = LLVMGetParam(ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFERS);
456
457 t_offset = lp_build_const_int32(gallivm, input_index);
458
459 t_list = build_indexed_load_const(ctx, t_list_ptr, t_offset);
460
461 /* Build the attribute offset */
462 attribute_offset = lp_build_const_int32(gallivm, 0);
463
464 if (!ctx->is_monolithic) {
465 buffer_index = LLVMGetParam(radeon_bld->main_fn,
466 ctx->param_vertex_index0 +
467 input_index);
468 } else if (divisor) {
469 /* Build index from instance ID, start instance and divisor */
470 ctx->shader->info.uses_instanceid = true;
471 buffer_index = get_instance_index_for_fetch(&ctx->radeon_bld,
472 SI_PARAM_START_INSTANCE,
473 divisor);
474 } else {
475 /* Load the buffer index for vertices. */
476 LLVMValueRef vertex_id = LLVMGetParam(ctx->radeon_bld.main_fn,
477 ctx->param_vertex_id);
478 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
479 SI_PARAM_BASE_VERTEX);
480 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
481 }
482
483 args[0] = t_list;
484 args[1] = attribute_offset;
485 args[2] = buffer_index;
486 input = lp_build_intrinsic(gallivm->builder,
487 "llvm.SI.vs.load.input", ctx->v4f32, args, 3,
488 LLVMReadNoneAttribute);
489
490 /* Break up the vec4 into individual components */
491 for (chan = 0; chan < 4; chan++) {
492 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
493 out[chan] = LLVMBuildExtractElement(gallivm->builder,
494 input, llvm_chan, "");
495 }
496 }
497
498 static LLVMValueRef get_primitive_id(struct lp_build_tgsi_context *bld_base,
499 unsigned swizzle)
500 {
501 struct si_shader_context *ctx = si_shader_context(bld_base);
502
503 if (swizzle > 0)
504 return bld_base->uint_bld.zero;
505
506 switch (ctx->type) {
507 case PIPE_SHADER_VERTEX:
508 return LLVMGetParam(ctx->radeon_bld.main_fn,
509 ctx->param_vs_prim_id);
510 case PIPE_SHADER_TESS_CTRL:
511 return LLVMGetParam(ctx->radeon_bld.main_fn,
512 SI_PARAM_PATCH_ID);
513 case PIPE_SHADER_TESS_EVAL:
514 return LLVMGetParam(ctx->radeon_bld.main_fn,
515 ctx->param_tes_patch_id);
516 case PIPE_SHADER_GEOMETRY:
517 return LLVMGetParam(ctx->radeon_bld.main_fn,
518 SI_PARAM_PRIMITIVE_ID);
519 default:
520 assert(0);
521 return bld_base->uint_bld.zero;
522 }
523 }
524
525 /**
526 * Return the value of tgsi_ind_register for indexing.
527 * This is the indirect index with the constant offset added to it.
528 */
529 static LLVMValueRef get_indirect_index(struct si_shader_context *ctx,
530 const struct tgsi_ind_register *ind,
531 int rel_index)
532 {
533 struct gallivm_state *gallivm = ctx->radeon_bld.soa.bld_base.base.gallivm;
534 LLVMValueRef result;
535
536 result = ctx->radeon_bld.soa.addr[ind->Index][ind->Swizzle];
537 result = LLVMBuildLoad(gallivm->builder, result, "");
538 result = LLVMBuildAdd(gallivm->builder, result,
539 lp_build_const_int32(gallivm, rel_index), "");
540 return result;
541 }
542
543 /**
544 * Like get_indirect_index, but restricts the return value to a (possibly
545 * undefined) value inside [0..num).
546 */
547 static LLVMValueRef get_bounded_indirect_index(struct si_shader_context *ctx,
548 const struct tgsi_ind_register *ind,
549 int rel_index, unsigned num)
550 {
551 LLVMValueRef result = get_indirect_index(ctx, ind, rel_index);
552
553 /* LLVM 3.8: If indirect resource indexing is used:
554 * - SI & CIK hang
555 * - VI crashes
556 */
557 if (HAVE_LLVM <= 0x0308)
558 return LLVMGetUndef(ctx->i32);
559
560 return radeon_llvm_bound_index(&ctx->radeon_bld, result, num);
561 }
562
563
564 /**
565 * Calculate a dword address given an input or output register and a stride.
566 */
567 static LLVMValueRef get_dw_address(struct si_shader_context *ctx,
568 const struct tgsi_full_dst_register *dst,
569 const struct tgsi_full_src_register *src,
570 LLVMValueRef vertex_dw_stride,
571 LLVMValueRef base_addr)
572 {
573 struct gallivm_state *gallivm = ctx->radeon_bld.soa.bld_base.base.gallivm;
574 struct tgsi_shader_info *info = &ctx->shader->selector->info;
575 ubyte *name, *index, *array_first;
576 int first, param;
577 struct tgsi_full_dst_register reg;
578
579 /* Set the register description. The address computation is the same
580 * for sources and destinations. */
581 if (src) {
582 reg.Register.File = src->Register.File;
583 reg.Register.Index = src->Register.Index;
584 reg.Register.Indirect = src->Register.Indirect;
585 reg.Register.Dimension = src->Register.Dimension;
586 reg.Indirect = src->Indirect;
587 reg.Dimension = src->Dimension;
588 reg.DimIndirect = src->DimIndirect;
589 } else
590 reg = *dst;
591
592 /* If the register is 2-dimensional (e.g. an array of vertices
593 * in a primitive), calculate the base address of the vertex. */
594 if (reg.Register.Dimension) {
595 LLVMValueRef index;
596
597 if (reg.Dimension.Indirect)
598 index = get_indirect_index(ctx, &reg.DimIndirect,
599 reg.Dimension.Index);
600 else
601 index = lp_build_const_int32(gallivm, reg.Dimension.Index);
602
603 base_addr = LLVMBuildAdd(gallivm->builder, base_addr,
604 LLVMBuildMul(gallivm->builder, index,
605 vertex_dw_stride, ""), "");
606 }
607
608 /* Get information about the register. */
609 if (reg.Register.File == TGSI_FILE_INPUT) {
610 name = info->input_semantic_name;
611 index = info->input_semantic_index;
612 array_first = info->input_array_first;
613 } else if (reg.Register.File == TGSI_FILE_OUTPUT) {
614 name = info->output_semantic_name;
615 index = info->output_semantic_index;
616 array_first = info->output_array_first;
617 } else {
618 assert(0);
619 return NULL;
620 }
621
622 if (reg.Register.Indirect) {
623 /* Add the relative address of the element. */
624 LLVMValueRef ind_index;
625
626 if (reg.Indirect.ArrayID)
627 first = array_first[reg.Indirect.ArrayID];
628 else
629 first = reg.Register.Index;
630
631 ind_index = get_indirect_index(ctx, &reg.Indirect,
632 reg.Register.Index - first);
633
634 base_addr = LLVMBuildAdd(gallivm->builder, base_addr,
635 LLVMBuildMul(gallivm->builder, ind_index,
636 lp_build_const_int32(gallivm, 4), ""), "");
637
638 param = si_shader_io_get_unique_index(name[first], index[first]);
639 } else {
640 param = si_shader_io_get_unique_index(name[reg.Register.Index],
641 index[reg.Register.Index]);
642 }
643
644 /* Add the base address of the element. */
645 return LLVMBuildAdd(gallivm->builder, base_addr,
646 lp_build_const_int32(gallivm, param * 4), "");
647 }
648
649 /* The offchip buffer layout for TCS->TES is
650 *
651 * - attribute 0 of patch 0 vertex 0
652 * - attribute 0 of patch 0 vertex 1
653 * - attribute 0 of patch 0 vertex 2
654 * ...
655 * - attribute 0 of patch 1 vertex 0
656 * - attribute 0 of patch 1 vertex 1
657 * ...
658 * - attribute 1 of patch 0 vertex 0
659 * - attribute 1 of patch 0 vertex 1
660 * ...
661 * - per patch attribute 0 of patch 0
662 * - per patch attribute 0 of patch 1
663 * ...
664 *
665 * Note that every attribute has 4 components.
666 */
667 static LLVMValueRef get_tcs_tes_buffer_address(struct si_shader_context *ctx,
668 LLVMValueRef vertex_index,
669 LLVMValueRef param_index)
670 {
671 struct gallivm_state *gallivm = ctx->radeon_bld.soa.bld_base.base.gallivm;
672 LLVMValueRef base_addr, vertices_per_patch, num_patches, total_vertices;
673 LLVMValueRef param_stride, constant16;
674
675 vertices_per_patch = unpack_param(ctx, SI_PARAM_TCS_OFFCHIP_LAYOUT, 9, 6);
676 num_patches = unpack_param(ctx, SI_PARAM_TCS_OFFCHIP_LAYOUT, 0, 9);
677 total_vertices = LLVMBuildMul(gallivm->builder, vertices_per_patch,
678 num_patches, "");
679
680 constant16 = lp_build_const_int32(gallivm, 16);
681 if (vertex_index) {
682 base_addr = LLVMBuildMul(gallivm->builder, get_rel_patch_id(ctx),
683 vertices_per_patch, "");
684
685 base_addr = LLVMBuildAdd(gallivm->builder, base_addr,
686 vertex_index, "");
687
688 param_stride = total_vertices;
689 } else {
690 base_addr = get_rel_patch_id(ctx);
691 param_stride = num_patches;
692 }
693
694 base_addr = LLVMBuildAdd(gallivm->builder, base_addr,
695 LLVMBuildMul(gallivm->builder, param_index,
696 param_stride, ""), "");
697
698 base_addr = LLVMBuildMul(gallivm->builder, base_addr, constant16, "");
699
700 if (!vertex_index) {
701 LLVMValueRef patch_data_offset =
702 unpack_param(ctx, SI_PARAM_TCS_OFFCHIP_LAYOUT, 16, 16);
703
704 base_addr = LLVMBuildAdd(gallivm->builder, base_addr,
705 patch_data_offset, "");
706 }
707 return base_addr;
708 }
709
710 static LLVMValueRef get_tcs_tes_buffer_address_from_reg(
711 struct si_shader_context *ctx,
712 const struct tgsi_full_dst_register *dst,
713 const struct tgsi_full_src_register *src)
714 {
715 struct gallivm_state *gallivm = ctx->radeon_bld.soa.bld_base.base.gallivm;
716 struct tgsi_shader_info *info = &ctx->shader->selector->info;
717 ubyte *name, *index, *array_first;
718 struct tgsi_full_src_register reg;
719 LLVMValueRef vertex_index = NULL;
720 LLVMValueRef param_index = NULL;
721 unsigned param_index_base, param_base;
722
723 reg = src ? *src : tgsi_full_src_register_from_dst(dst);
724
725 if (reg.Register.Dimension) {
726
727 if (reg.Dimension.Indirect)
728 vertex_index = get_indirect_index(ctx, &reg.DimIndirect,
729 reg.Dimension.Index);
730 else
731 vertex_index = lp_build_const_int32(gallivm,
732 reg.Dimension.Index);
733 }
734
735 /* Get information about the register. */
736 if (reg.Register.File == TGSI_FILE_INPUT) {
737 name = info->input_semantic_name;
738 index = info->input_semantic_index;
739 array_first = info->input_array_first;
740 } else if (reg.Register.File == TGSI_FILE_OUTPUT) {
741 name = info->output_semantic_name;
742 index = info->output_semantic_index;
743 array_first = info->output_array_first;
744 } else {
745 assert(0);
746 return NULL;
747 }
748
749 if (reg.Register.Indirect) {
750 if (reg.Indirect.ArrayID)
751 param_base = array_first[reg.Indirect.ArrayID];
752 else
753 param_base = reg.Register.Index;
754
755 param_index = get_indirect_index(ctx, &reg.Indirect,
756 reg.Register.Index - param_base);
757
758 } else {
759 param_base = reg.Register.Index;
760 param_index = lp_build_const_int32(gallivm, 0);
761 }
762
763 param_index_base = si_shader_io_get_unique_index(name[param_base],
764 index[param_base]);
765
766 param_index = LLVMBuildAdd(gallivm->builder, param_index,
767 lp_build_const_int32(gallivm, param_index_base),
768 "");
769
770 return get_tcs_tes_buffer_address(ctx, vertex_index, param_index);
771 }
772
773 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
774 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
775 * or v4i32 (num_channels=3,4). */
776 static void build_tbuffer_store(struct si_shader_context *ctx,
777 LLVMValueRef rsrc,
778 LLVMValueRef vdata,
779 unsigned num_channels,
780 LLVMValueRef vaddr,
781 LLVMValueRef soffset,
782 unsigned inst_offset,
783 unsigned dfmt,
784 unsigned nfmt,
785 unsigned offen,
786 unsigned idxen,
787 unsigned glc,
788 unsigned slc,
789 unsigned tfe)
790 {
791 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
792 LLVMValueRef args[] = {
793 rsrc,
794 vdata,
795 LLVMConstInt(ctx->i32, num_channels, 0),
796 vaddr,
797 soffset,
798 LLVMConstInt(ctx->i32, inst_offset, 0),
799 LLVMConstInt(ctx->i32, dfmt, 0),
800 LLVMConstInt(ctx->i32, nfmt, 0),
801 LLVMConstInt(ctx->i32, offen, 0),
802 LLVMConstInt(ctx->i32, idxen, 0),
803 LLVMConstInt(ctx->i32, glc, 0),
804 LLVMConstInt(ctx->i32, slc, 0),
805 LLVMConstInt(ctx->i32, tfe, 0)
806 };
807
808 /* The instruction offset field has 12 bits */
809 assert(offen || inst_offset < (1 << 12));
810
811 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
812 unsigned func = CLAMP(num_channels, 1, 3) - 1;
813 const char *types[] = {"i32", "v2i32", "v4i32"};
814 char name[256];
815 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
816
817 lp_build_intrinsic(gallivm->builder, name, ctx->voidt,
818 args, ARRAY_SIZE(args), 0);
819 }
820
821 static void build_tbuffer_store_dwords(struct si_shader_context *ctx,
822 LLVMValueRef rsrc,
823 LLVMValueRef vdata,
824 unsigned num_channels,
825 LLVMValueRef vaddr,
826 LLVMValueRef soffset,
827 unsigned inst_offset)
828 {
829 static unsigned dfmt[] = {
830 V_008F0C_BUF_DATA_FORMAT_32,
831 V_008F0C_BUF_DATA_FORMAT_32_32,
832 V_008F0C_BUF_DATA_FORMAT_32_32_32,
833 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
834 };
835 assert(num_channels >= 1 && num_channels <= 4);
836
837 build_tbuffer_store(ctx, rsrc, vdata, num_channels, vaddr, soffset,
838 inst_offset, dfmt[num_channels-1],
839 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
840 }
841
842 static LLVMValueRef build_buffer_load(struct si_shader_context *ctx,
843 LLVMValueRef rsrc,
844 int num_channels,
845 LLVMValueRef vindex,
846 LLVMValueRef voffset,
847 LLVMValueRef soffset,
848 unsigned inst_offset,
849 unsigned glc,
850 unsigned slc)
851 {
852 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
853 unsigned func = CLAMP(num_channels, 1, 3) - 1;
854
855 if (HAVE_LLVM >= 0x309) {
856 LLVMValueRef args[] = {
857 LLVMBuildBitCast(gallivm->builder, rsrc, ctx->v4i32, ""),
858 vindex ? vindex : LLVMConstInt(ctx->i32, 0, 0),
859 LLVMConstInt(ctx->i32, inst_offset, 0),
860 LLVMConstInt(ctx->i1, glc, 0),
861 LLVMConstInt(ctx->i1, slc, 0)
862 };
863
864 LLVMTypeRef types[] = {ctx->f32, LLVMVectorType(ctx->f32, 2),
865 ctx->v4f32};
866 const char *type_names[] = {"f32", "v2f32", "v4f32"};
867 char name[256];
868
869 if (voffset) {
870 args[2] = LLVMBuildAdd(gallivm->builder, args[2], voffset,
871 "");
872 }
873
874 if (soffset) {
875 args[2] = LLVMBuildAdd(gallivm->builder, args[2], soffset,
876 "");
877 }
878
879 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
880 type_names[func]);
881
882 return lp_build_intrinsic(gallivm->builder, name, types[func], args,
883 ARRAY_SIZE(args), LLVMReadOnlyAttribute);
884 } else {
885 LLVMValueRef args[] = {
886 LLVMBuildBitCast(gallivm->builder, rsrc, ctx->v16i8, ""),
887 voffset ? voffset : vindex,
888 soffset,
889 LLVMConstInt(ctx->i32, inst_offset, 0),
890 LLVMConstInt(ctx->i32, voffset ? 1 : 0, 0), // offen
891 LLVMConstInt(ctx->i32, vindex ? 1 : 0, 0), //idxen
892 LLVMConstInt(ctx->i32, glc, 0),
893 LLVMConstInt(ctx->i32, slc, 0),
894 LLVMConstInt(ctx->i32, 0, 0), // TFE
895 };
896
897 LLVMTypeRef types[] = {ctx->i32, LLVMVectorType(ctx->i32, 2),
898 ctx->v4i32};
899 const char *type_names[] = {"i32", "v2i32", "v4i32"};
900 const char *arg_type = "i32";
901 char name[256];
902
903 if (voffset && vindex) {
904 LLVMValueRef vaddr[] = {vindex, voffset};
905
906 arg_type = "v2i32";
907 args[1] = lp_build_gather_values(gallivm, vaddr, 2);
908 }
909
910 snprintf(name, sizeof(name), "llvm.SI.buffer.load.dword.%s.%s",
911 type_names[func], arg_type);
912
913 return lp_build_intrinsic(gallivm->builder, name, types[func], args,
914 ARRAY_SIZE(args), LLVMReadOnlyAttribute);
915 }
916 }
917
918 static LLVMValueRef buffer_load(struct lp_build_tgsi_context *bld_base,
919 enum tgsi_opcode_type type, unsigned swizzle,
920 LLVMValueRef buffer, LLVMValueRef offset,
921 LLVMValueRef base)
922 {
923 struct si_shader_context *ctx = si_shader_context(bld_base);
924 struct gallivm_state *gallivm = bld_base->base.gallivm;
925 LLVMValueRef value, value2;
926 LLVMTypeRef llvm_type = tgsi2llvmtype(bld_base, type);
927 LLVMTypeRef vec_type = LLVMVectorType(llvm_type, 4);
928
929 if (swizzle == ~0) {
930 value = build_buffer_load(ctx, buffer, 4, NULL, base, offset,
931 0, 1, 0);
932
933 return LLVMBuildBitCast(gallivm->builder, value, vec_type, "");
934 }
935
936 if (!tgsi_type_is_64bit(type)) {
937 value = build_buffer_load(ctx, buffer, 4, NULL, base, offset,
938 0, 1, 0);
939
940 value = LLVMBuildBitCast(gallivm->builder, value, vec_type, "");
941 return LLVMBuildExtractElement(gallivm->builder, value,
942 lp_build_const_int32(gallivm, swizzle), "");
943 }
944
945 value = build_buffer_load(ctx, buffer, 1, NULL, base, offset,
946 swizzle * 4, 1, 0);
947
948 value2 = build_buffer_load(ctx, buffer, 1, NULL, base, offset,
949 swizzle * 4 + 4, 1, 0);
950
951 return radeon_llvm_emit_fetch_64bit(bld_base, type, value, value2);
952 }
953
954 /**
955 * Load from LDS.
956 *
957 * \param type output value type
958 * \param swizzle offset (typically 0..3); it can be ~0, which loads a vec4
959 * \param dw_addr address in dwords
960 */
961 static LLVMValueRef lds_load(struct lp_build_tgsi_context *bld_base,
962 enum tgsi_opcode_type type, unsigned swizzle,
963 LLVMValueRef dw_addr)
964 {
965 struct si_shader_context *ctx = si_shader_context(bld_base);
966 struct gallivm_state *gallivm = bld_base->base.gallivm;
967 LLVMValueRef value;
968
969 if (swizzle == ~0) {
970 LLVMValueRef values[TGSI_NUM_CHANNELS];
971
972 for (unsigned chan = 0; chan < TGSI_NUM_CHANNELS; chan++)
973 values[chan] = lds_load(bld_base, type, chan, dw_addr);
974
975 return lp_build_gather_values(bld_base->base.gallivm, values,
976 TGSI_NUM_CHANNELS);
977 }
978
979 dw_addr = lp_build_add(&bld_base->uint_bld, dw_addr,
980 lp_build_const_int32(gallivm, swizzle));
981
982 value = build_indexed_load(ctx, ctx->lds, dw_addr, false);
983 if (tgsi_type_is_64bit(type)) {
984 LLVMValueRef value2;
985 dw_addr = lp_build_add(&bld_base->uint_bld, dw_addr,
986 lp_build_const_int32(gallivm, swizzle + 1));
987 value2 = build_indexed_load(ctx, ctx->lds, dw_addr, false);
988 return radeon_llvm_emit_fetch_64bit(bld_base, type, value, value2);
989 }
990
991 return LLVMBuildBitCast(gallivm->builder, value,
992 tgsi2llvmtype(bld_base, type), "");
993 }
994
995 /**
996 * Store to LDS.
997 *
998 * \param swizzle offset (typically 0..3)
999 * \param dw_addr address in dwords
1000 * \param value value to store
1001 */
1002 static void lds_store(struct lp_build_tgsi_context *bld_base,
1003 unsigned swizzle, LLVMValueRef dw_addr,
1004 LLVMValueRef value)
1005 {
1006 struct si_shader_context *ctx = si_shader_context(bld_base);
1007 struct gallivm_state *gallivm = bld_base->base.gallivm;
1008
1009 dw_addr = lp_build_add(&bld_base->uint_bld, dw_addr,
1010 lp_build_const_int32(gallivm, swizzle));
1011
1012 value = LLVMBuildBitCast(gallivm->builder, value, ctx->i32, "");
1013 build_indexed_store(ctx, ctx->lds,
1014 dw_addr, value);
1015 }
1016
1017 static LLVMValueRef fetch_input_tcs(
1018 struct lp_build_tgsi_context *bld_base,
1019 const struct tgsi_full_src_register *reg,
1020 enum tgsi_opcode_type type, unsigned swizzle)
1021 {
1022 struct si_shader_context *ctx = si_shader_context(bld_base);
1023 LLVMValueRef dw_addr, stride;
1024
1025 stride = unpack_param(ctx, SI_PARAM_TCS_IN_LAYOUT, 13, 8);
1026 dw_addr = get_tcs_in_current_patch_offset(ctx);
1027 dw_addr = get_dw_address(ctx, NULL, reg, stride, dw_addr);
1028
1029 return lds_load(bld_base, type, swizzle, dw_addr);
1030 }
1031
1032 static LLVMValueRef fetch_output_tcs(
1033 struct lp_build_tgsi_context *bld_base,
1034 const struct tgsi_full_src_register *reg,
1035 enum tgsi_opcode_type type, unsigned swizzle)
1036 {
1037 struct si_shader_context *ctx = si_shader_context(bld_base);
1038 LLVMValueRef dw_addr, stride;
1039
1040 if (reg->Register.Dimension) {
1041 stride = unpack_param(ctx, SI_PARAM_TCS_OUT_LAYOUT, 13, 8);
1042 dw_addr = get_tcs_out_current_patch_offset(ctx);
1043 dw_addr = get_dw_address(ctx, NULL, reg, stride, dw_addr);
1044 } else {
1045 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
1046 dw_addr = get_dw_address(ctx, NULL, reg, NULL, dw_addr);
1047 }
1048
1049 return lds_load(bld_base, type, swizzle, dw_addr);
1050 }
1051
1052 static LLVMValueRef fetch_input_tes(
1053 struct lp_build_tgsi_context *bld_base,
1054 const struct tgsi_full_src_register *reg,
1055 enum tgsi_opcode_type type, unsigned swizzle)
1056 {
1057 struct si_shader_context *ctx = si_shader_context(bld_base);
1058 struct gallivm_state *gallivm = bld_base->base.gallivm;
1059 LLVMValueRef rw_buffers, buffer, base, addr;
1060
1061 rw_buffers = LLVMGetParam(ctx->radeon_bld.main_fn,
1062 SI_PARAM_RW_BUFFERS);
1063 buffer = build_indexed_load_const(ctx, rw_buffers,
1064 lp_build_const_int32(gallivm, SI_HS_RING_TESS_OFFCHIP));
1065
1066 base = LLVMGetParam(ctx->radeon_bld.main_fn, ctx->param_oc_lds);
1067 addr = get_tcs_tes_buffer_address_from_reg(ctx, NULL, reg);
1068
1069 return buffer_load(bld_base, type, swizzle, buffer, base, addr);
1070 }
1071
1072 static void store_output_tcs(struct lp_build_tgsi_context *bld_base,
1073 const struct tgsi_full_instruction *inst,
1074 const struct tgsi_opcode_info *info,
1075 LLVMValueRef dst[4])
1076 {
1077 struct si_shader_context *ctx = si_shader_context(bld_base);
1078 struct gallivm_state *gallivm = bld_base->base.gallivm;
1079 const struct tgsi_full_dst_register *reg = &inst->Dst[0];
1080 unsigned chan_index;
1081 LLVMValueRef dw_addr, stride;
1082 LLVMValueRef rw_buffers, buffer, base, buf_addr;
1083 LLVMValueRef values[4];
1084
1085 /* Only handle per-patch and per-vertex outputs here.
1086 * Vectors will be lowered to scalars and this function will be called again.
1087 */
1088 if (reg->Register.File != TGSI_FILE_OUTPUT ||
1089 (dst[0] && LLVMGetTypeKind(LLVMTypeOf(dst[0])) == LLVMVectorTypeKind)) {
1090 radeon_llvm_emit_store(bld_base, inst, info, dst);
1091 return;
1092 }
1093
1094 if (reg->Register.Dimension) {
1095 stride = unpack_param(ctx, SI_PARAM_TCS_OUT_LAYOUT, 13, 8);
1096 dw_addr = get_tcs_out_current_patch_offset(ctx);
1097 dw_addr = get_dw_address(ctx, reg, NULL, stride, dw_addr);
1098 } else {
1099 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
1100 dw_addr = get_dw_address(ctx, reg, NULL, NULL, dw_addr);
1101 }
1102
1103 rw_buffers = LLVMGetParam(ctx->radeon_bld.main_fn,
1104 SI_PARAM_RW_BUFFERS);
1105 buffer = build_indexed_load_const(ctx, rw_buffers,
1106 lp_build_const_int32(gallivm, SI_HS_RING_TESS_OFFCHIP));
1107
1108 base = LLVMGetParam(ctx->radeon_bld.main_fn, ctx->param_oc_lds);
1109 buf_addr = get_tcs_tes_buffer_address_from_reg(ctx, reg, NULL);
1110
1111
1112 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst, chan_index) {
1113 LLVMValueRef value = dst[chan_index];
1114
1115 if (inst->Instruction.Saturate)
1116 value = radeon_llvm_saturate(bld_base, value);
1117
1118 lds_store(bld_base, chan_index, dw_addr, value);
1119
1120 value = LLVMBuildBitCast(gallivm->builder, value, ctx->i32, "");
1121 values[chan_index] = value;
1122
1123 if (inst->Dst[0].Register.WriteMask != 0xF) {
1124 build_tbuffer_store_dwords(ctx, buffer, value, 1,
1125 buf_addr, base,
1126 4 * chan_index);
1127 }
1128 }
1129
1130 if (inst->Dst[0].Register.WriteMask == 0xF) {
1131 LLVMValueRef value = lp_build_gather_values(bld_base->base.gallivm,
1132 values, 4);
1133 build_tbuffer_store_dwords(ctx, buffer, value, 4, buf_addr,
1134 base, 0);
1135 }
1136 }
1137
1138 static LLVMValueRef fetch_input_gs(
1139 struct lp_build_tgsi_context *bld_base,
1140 const struct tgsi_full_src_register *reg,
1141 enum tgsi_opcode_type type,
1142 unsigned swizzle)
1143 {
1144 struct lp_build_context *base = &bld_base->base;
1145 struct si_shader_context *ctx = si_shader_context(bld_base);
1146 struct si_shader *shader = ctx->shader;
1147 struct lp_build_context *uint = &ctx->radeon_bld.soa.bld_base.uint_bld;
1148 struct gallivm_state *gallivm = base->gallivm;
1149 LLVMValueRef vtx_offset;
1150 LLVMValueRef args[9];
1151 unsigned vtx_offset_param;
1152 struct tgsi_shader_info *info = &shader->selector->info;
1153 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
1154 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
1155 unsigned param;
1156 LLVMValueRef value;
1157
1158 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID)
1159 return get_primitive_id(bld_base, swizzle);
1160
1161 if (!reg->Register.Dimension)
1162 return NULL;
1163
1164 if (swizzle == ~0) {
1165 LLVMValueRef values[TGSI_NUM_CHANNELS];
1166 unsigned chan;
1167 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
1168 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
1169 }
1170 return lp_build_gather_values(bld_base->base.gallivm, values,
1171 TGSI_NUM_CHANNELS);
1172 }
1173
1174 /* Get the vertex offset parameter */
1175 vtx_offset_param = reg->Dimension.Index;
1176 if (vtx_offset_param < 2) {
1177 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
1178 } else {
1179 assert(vtx_offset_param < 6);
1180 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
1181 }
1182 vtx_offset = lp_build_mul_imm(uint,
1183 LLVMGetParam(ctx->radeon_bld.main_fn,
1184 vtx_offset_param),
1185 4);
1186
1187 param = si_shader_io_get_unique_index(semantic_name, semantic_index);
1188 args[0] = ctx->esgs_ring;
1189 args[1] = vtx_offset;
1190 args[2] = lp_build_const_int32(gallivm, (param * 4 + swizzle) * 256);
1191 args[3] = uint->zero;
1192 args[4] = uint->one; /* OFFEN */
1193 args[5] = uint->zero; /* IDXEN */
1194 args[6] = uint->one; /* GLC */
1195 args[7] = uint->zero; /* SLC */
1196 args[8] = uint->zero; /* TFE */
1197
1198 value = lp_build_intrinsic(gallivm->builder,
1199 "llvm.SI.buffer.load.dword.i32.i32",
1200 ctx->i32, args, 9,
1201 LLVMReadOnlyAttribute);
1202 if (tgsi_type_is_64bit(type)) {
1203 LLVMValueRef value2;
1204 args[2] = lp_build_const_int32(gallivm, (param * 4 + swizzle + 1) * 256);
1205 value2 = lp_build_intrinsic(gallivm->builder,
1206 "llvm.SI.buffer.load.dword.i32.i32",
1207 ctx->i32, args, 9,
1208 LLVMReadOnlyAttribute);
1209 return radeon_llvm_emit_fetch_64bit(bld_base, type,
1210 value, value2);
1211 }
1212 return LLVMBuildBitCast(gallivm->builder,
1213 value,
1214 tgsi2llvmtype(bld_base, type), "");
1215 }
1216
1217 static int lookup_interp_param_index(unsigned interpolate, unsigned location)
1218 {
1219 switch (interpolate) {
1220 case TGSI_INTERPOLATE_CONSTANT:
1221 return 0;
1222
1223 case TGSI_INTERPOLATE_LINEAR:
1224 if (location == TGSI_INTERPOLATE_LOC_SAMPLE)
1225 return SI_PARAM_LINEAR_SAMPLE;
1226 else if (location == TGSI_INTERPOLATE_LOC_CENTROID)
1227 return SI_PARAM_LINEAR_CENTROID;
1228 else
1229 return SI_PARAM_LINEAR_CENTER;
1230 break;
1231 case TGSI_INTERPOLATE_COLOR:
1232 case TGSI_INTERPOLATE_PERSPECTIVE:
1233 if (location == TGSI_INTERPOLATE_LOC_SAMPLE)
1234 return SI_PARAM_PERSP_SAMPLE;
1235 else if (location == TGSI_INTERPOLATE_LOC_CENTROID)
1236 return SI_PARAM_PERSP_CENTROID;
1237 else
1238 return SI_PARAM_PERSP_CENTER;
1239 break;
1240 default:
1241 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
1242 return -1;
1243 }
1244 }
1245
1246 /* This shouldn't be used by explicit INTERP opcodes. */
1247 static unsigned select_interp_param(struct si_shader_context *ctx,
1248 unsigned param)
1249 {
1250 if (!ctx->is_monolithic)
1251 return param;
1252
1253 if (ctx->shader->key.ps.prolog.force_persp_sample_interp) {
1254 switch (param) {
1255 case SI_PARAM_PERSP_CENTROID:
1256 case SI_PARAM_PERSP_CENTER:
1257 return SI_PARAM_PERSP_SAMPLE;
1258 }
1259 }
1260 if (ctx->shader->key.ps.prolog.force_linear_sample_interp) {
1261 switch (param) {
1262 case SI_PARAM_LINEAR_CENTROID:
1263 case SI_PARAM_LINEAR_CENTER:
1264 return SI_PARAM_LINEAR_SAMPLE;
1265 }
1266 }
1267 if (ctx->shader->key.ps.prolog.force_persp_center_interp) {
1268 switch (param) {
1269 case SI_PARAM_PERSP_CENTROID:
1270 case SI_PARAM_PERSP_SAMPLE:
1271 return SI_PARAM_PERSP_CENTER;
1272 }
1273 }
1274 if (ctx->shader->key.ps.prolog.force_linear_center_interp) {
1275 switch (param) {
1276 case SI_PARAM_LINEAR_CENTROID:
1277 case SI_PARAM_LINEAR_SAMPLE:
1278 return SI_PARAM_LINEAR_CENTER;
1279 }
1280 }
1281
1282 return param;
1283 }
1284
1285 /**
1286 * Interpolate a fragment shader input.
1287 *
1288 * @param ctx context
1289 * @param input_index index of the input in hardware
1290 * @param semantic_name TGSI_SEMANTIC_*
1291 * @param semantic_index semantic index
1292 * @param num_interp_inputs number of all interpolated inputs (= BCOLOR offset)
1293 * @param colors_read_mask color components read (4 bits for each color, 8 bits in total)
1294 * @param interp_param interpolation weights (i,j)
1295 * @param prim_mask SI_PARAM_PRIM_MASK
1296 * @param face SI_PARAM_FRONT_FACE
1297 * @param result the return value (4 components)
1298 */
1299 static void interp_fs_input(struct si_shader_context *ctx,
1300 unsigned input_index,
1301 unsigned semantic_name,
1302 unsigned semantic_index,
1303 unsigned num_interp_inputs,
1304 unsigned colors_read_mask,
1305 LLVMValueRef interp_param,
1306 LLVMValueRef prim_mask,
1307 LLVMValueRef face,
1308 LLVMValueRef result[4])
1309 {
1310 struct lp_build_context *base = &ctx->radeon_bld.soa.bld_base.base;
1311 struct lp_build_context *uint = &ctx->radeon_bld.soa.bld_base.uint_bld;
1312 struct gallivm_state *gallivm = base->gallivm;
1313 const char *intr_name;
1314 LLVMValueRef attr_number;
1315
1316 unsigned chan;
1317
1318 attr_number = lp_build_const_int32(gallivm, input_index);
1319
1320 /* fs.constant returns the param from the middle vertex, so it's not
1321 * really useful for flat shading. It's meant to be used for custom
1322 * interpolation (but the intrinsic can't fetch from the other two
1323 * vertices).
1324 *
1325 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
1326 * to do the right thing. The only reason we use fs.constant is that
1327 * fs.interp cannot be used on integers, because they can be equal
1328 * to NaN.
1329 */
1330 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
1331
1332 if (semantic_name == TGSI_SEMANTIC_COLOR &&
1333 ctx->shader->key.ps.prolog.color_two_side) {
1334 LLVMValueRef args[4];
1335 LLVMValueRef is_face_positive;
1336 LLVMValueRef back_attr_number;
1337
1338 /* If BCOLOR0 is used, BCOLOR1 is at offset "num_inputs + 1",
1339 * otherwise it's at offset "num_inputs".
1340 */
1341 unsigned back_attr_offset = num_interp_inputs;
1342 if (semantic_index == 1 && colors_read_mask & 0xf)
1343 back_attr_offset += 1;
1344
1345 back_attr_number = lp_build_const_int32(gallivm, back_attr_offset);
1346
1347 is_face_positive = LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1348 face, uint->zero, "");
1349
1350 args[2] = prim_mask;
1351 args[3] = interp_param;
1352 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
1353 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
1354 LLVMValueRef front, back;
1355
1356 args[0] = llvm_chan;
1357 args[1] = attr_number;
1358 front = lp_build_intrinsic(gallivm->builder, intr_name,
1359 ctx->f32, args, args[3] ? 4 : 3,
1360 LLVMReadNoneAttribute);
1361
1362 args[1] = back_attr_number;
1363 back = lp_build_intrinsic(gallivm->builder, intr_name,
1364 ctx->f32, args, args[3] ? 4 : 3,
1365 LLVMReadNoneAttribute);
1366
1367 result[chan] = LLVMBuildSelect(gallivm->builder,
1368 is_face_positive,
1369 front,
1370 back,
1371 "");
1372 }
1373 } else if (semantic_name == TGSI_SEMANTIC_FOG) {
1374 LLVMValueRef args[4];
1375
1376 args[0] = uint->zero;
1377 args[1] = attr_number;
1378 args[2] = prim_mask;
1379 args[3] = interp_param;
1380 result[0] = lp_build_intrinsic(gallivm->builder, intr_name,
1381 ctx->f32, args, args[3] ? 4 : 3,
1382 LLVMReadNoneAttribute);
1383 result[1] =
1384 result[2] = lp_build_const_float(gallivm, 0.0f);
1385 result[3] = lp_build_const_float(gallivm, 1.0f);
1386 } else {
1387 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
1388 LLVMValueRef args[4];
1389 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
1390
1391 args[0] = llvm_chan;
1392 args[1] = attr_number;
1393 args[2] = prim_mask;
1394 args[3] = interp_param;
1395 result[chan] = lp_build_intrinsic(gallivm->builder, intr_name,
1396 ctx->f32, args, args[3] ? 4 : 3,
1397 LLVMReadNoneAttribute);
1398 }
1399 }
1400 }
1401
1402 /* LLVMGetParam with bc_optimize resolved. */
1403 static LLVMValueRef get_interp_param(struct si_shader_context *ctx,
1404 int interp_param_idx)
1405 {
1406 LLVMBuilderRef builder = ctx->radeon_bld.gallivm.builder;
1407 LLVMValueRef main_fn = ctx->radeon_bld.main_fn;
1408 LLVMValueRef param = NULL;
1409
1410 /* Handle PRIM_MASK[31] (bc_optimize). */
1411 if (ctx->is_monolithic &&
1412 ((ctx->shader->key.ps.prolog.bc_optimize_for_persp &&
1413 interp_param_idx == SI_PARAM_PERSP_CENTROID) ||
1414 (ctx->shader->key.ps.prolog.bc_optimize_for_linear &&
1415 interp_param_idx == SI_PARAM_LINEAR_CENTROID))) {
1416 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
1417 * The hw doesn't compute CENTROID if the whole wave only
1418 * contains fully-covered quads.
1419 */
1420 LLVMValueRef bc_optimize =
1421 LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
1422 bc_optimize = LLVMBuildLShr(builder,
1423 bc_optimize,
1424 LLVMConstInt(ctx->i32, 31, 0), "");
1425 bc_optimize = LLVMBuildTrunc(builder, bc_optimize, ctx->i1, "");
1426
1427 if (ctx->shader->key.ps.prolog.bc_optimize_for_persp &&
1428 interp_param_idx == SI_PARAM_PERSP_CENTROID) {
1429 param = LLVMBuildSelect(builder, bc_optimize,
1430 LLVMGetParam(main_fn,
1431 SI_PARAM_PERSP_CENTER),
1432 LLVMGetParam(main_fn,
1433 SI_PARAM_PERSP_CENTROID),
1434 "");
1435 }
1436 if (ctx->shader->key.ps.prolog.bc_optimize_for_linear &&
1437 interp_param_idx == SI_PARAM_LINEAR_CENTROID) {
1438 param = LLVMBuildSelect(builder, bc_optimize,
1439 LLVMGetParam(main_fn,
1440 SI_PARAM_LINEAR_CENTER),
1441 LLVMGetParam(main_fn,
1442 SI_PARAM_LINEAR_CENTROID),
1443 "");
1444 }
1445 }
1446
1447 if (!param)
1448 param = LLVMGetParam(main_fn, interp_param_idx);
1449 return param;
1450 }
1451
1452 static void declare_input_fs(
1453 struct radeon_llvm_context *radeon_bld,
1454 unsigned input_index,
1455 const struct tgsi_full_declaration *decl,
1456 LLVMValueRef out[4])
1457 {
1458 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
1459 struct si_shader_context *ctx =
1460 si_shader_context(&radeon_bld->soa.bld_base);
1461 struct si_shader *shader = ctx->shader;
1462 LLVMValueRef main_fn = radeon_bld->main_fn;
1463 LLVMValueRef interp_param = NULL;
1464 int interp_param_idx;
1465
1466 /* Get colors from input VGPRs (set by the prolog). */
1467 if (!ctx->is_monolithic &&
1468 decl->Semantic.Name == TGSI_SEMANTIC_COLOR) {
1469 unsigned i = decl->Semantic.Index;
1470 unsigned colors_read = shader->selector->info.colors_read;
1471 unsigned mask = colors_read >> (i * 4);
1472 unsigned offset = SI_PARAM_POS_FIXED_PT + 1 +
1473 (i ? util_bitcount(colors_read & 0xf) : 0);
1474
1475 out[0] = mask & 0x1 ? LLVMGetParam(main_fn, offset++) : base->undef;
1476 out[1] = mask & 0x2 ? LLVMGetParam(main_fn, offset++) : base->undef;
1477 out[2] = mask & 0x4 ? LLVMGetParam(main_fn, offset++) : base->undef;
1478 out[3] = mask & 0x8 ? LLVMGetParam(main_fn, offset++) : base->undef;
1479 return;
1480 }
1481
1482 interp_param_idx = lookup_interp_param_index(decl->Interp.Interpolate,
1483 decl->Interp.Location);
1484 if (interp_param_idx == -1)
1485 return;
1486 else if (interp_param_idx) {
1487 interp_param_idx = select_interp_param(ctx,
1488 interp_param_idx);
1489 interp_param = get_interp_param(ctx, interp_param_idx);
1490 }
1491
1492 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
1493 decl->Interp.Interpolate == TGSI_INTERPOLATE_COLOR &&
1494 ctx->shader->key.ps.prolog.flatshade_colors)
1495 interp_param = NULL; /* load the constant color */
1496
1497 interp_fs_input(ctx, input_index, decl->Semantic.Name,
1498 decl->Semantic.Index, shader->selector->info.num_inputs,
1499 shader->selector->info.colors_read, interp_param,
1500 LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK),
1501 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE),
1502 &out[0]);
1503 }
1504
1505 static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
1506 {
1507 return unpack_param(si_shader_context(&radeon_bld->soa.bld_base),
1508 SI_PARAM_ANCILLARY, 8, 4);
1509 }
1510
1511 /**
1512 * Set range metadata on an instruction. This can only be used on load and
1513 * call instructions. If you know an instruction can only produce the values
1514 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1515 * \p lo is the minimum value inclusive.
1516 * \p hi is the maximum value exclusive.
1517 */
1518 static void set_range_metadata(struct si_shader_context *ctx,
1519 LLVMValueRef value, unsigned lo, unsigned hi)
1520 {
1521 LLVMValueRef range_md, md_args[2];
1522 LLVMTypeRef type = LLVMTypeOf(value);
1523 LLVMContextRef context = LLVMGetTypeContext(type);
1524
1525 md_args[0] = LLVMConstInt(type, lo, false);
1526 md_args[1] = LLVMConstInt(type, hi, false);
1527 range_md = LLVMMDNodeInContext(context, md_args, 2);
1528 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
1529 }
1530
1531 static LLVMValueRef get_thread_id(struct si_shader_context *ctx)
1532 {
1533 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
1534 LLVMValueRef tid;
1535
1536 if (HAVE_LLVM < 0x0308) {
1537 tid = lp_build_intrinsic(gallivm->builder, "llvm.SI.tid",
1538 ctx->i32, NULL, 0, LLVMReadNoneAttribute);
1539 } else {
1540 LLVMValueRef tid_args[2];
1541 tid_args[0] = lp_build_const_int32(gallivm, 0xffffffff);
1542 tid_args[1] = lp_build_const_int32(gallivm, 0);
1543 tid_args[1] = lp_build_intrinsic(gallivm->builder,
1544 "llvm.amdgcn.mbcnt.lo", ctx->i32,
1545 tid_args, 2, LLVMReadNoneAttribute);
1546
1547 tid = lp_build_intrinsic(gallivm->builder,
1548 "llvm.amdgcn.mbcnt.hi", ctx->i32,
1549 tid_args, 2, LLVMReadNoneAttribute);
1550 }
1551 set_range_metadata(ctx, tid, 0, 64);
1552 return tid;
1553 }
1554
1555 /**
1556 * Load a dword from a constant buffer.
1557 */
1558 static LLVMValueRef buffer_load_const(struct si_shader_context *ctx,
1559 LLVMValueRef resource,
1560 LLVMValueRef offset)
1561 {
1562 LLVMBuilderRef builder = ctx->radeon_bld.gallivm.builder;
1563 LLVMValueRef args[2] = {resource, offset};
1564
1565 return lp_build_intrinsic(builder, "llvm.SI.load.const", ctx->f32, args, 2,
1566 LLVMReadNoneAttribute);
1567 }
1568
1569 static LLVMValueRef load_sample_position(struct radeon_llvm_context *radeon_bld, LLVMValueRef sample_id)
1570 {
1571 struct si_shader_context *ctx =
1572 si_shader_context(&radeon_bld->soa.bld_base);
1573 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
1574 struct gallivm_state *gallivm = &radeon_bld->gallivm;
1575 LLVMBuilderRef builder = gallivm->builder;
1576 LLVMValueRef desc = LLVMGetParam(ctx->radeon_bld.main_fn, SI_PARAM_RW_BUFFERS);
1577 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_PS_CONST_SAMPLE_POSITIONS);
1578 LLVMValueRef resource = build_indexed_load_const(ctx, desc, buf_index);
1579
1580 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
1581 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, sample_id, 8);
1582 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
1583
1584 LLVMValueRef pos[4] = {
1585 buffer_load_const(ctx, resource, offset0),
1586 buffer_load_const(ctx, resource, offset1),
1587 lp_build_const_float(gallivm, 0),
1588 lp_build_const_float(gallivm, 0)
1589 };
1590
1591 return lp_build_gather_values(gallivm, pos, 4);
1592 }
1593
1594 static void declare_system_value(
1595 struct radeon_llvm_context *radeon_bld,
1596 unsigned index,
1597 const struct tgsi_full_declaration *decl)
1598 {
1599 struct si_shader_context *ctx =
1600 si_shader_context(&radeon_bld->soa.bld_base);
1601 struct lp_build_context *bld = &radeon_bld->soa.bld_base.base;
1602 struct gallivm_state *gallivm = &radeon_bld->gallivm;
1603 LLVMValueRef value = 0;
1604
1605 switch (decl->Semantic.Name) {
1606 case TGSI_SEMANTIC_INSTANCEID:
1607 value = LLVMGetParam(radeon_bld->main_fn,
1608 ctx->param_instance_id);
1609 break;
1610
1611 case TGSI_SEMANTIC_VERTEXID:
1612 value = LLVMBuildAdd(gallivm->builder,
1613 LLVMGetParam(radeon_bld->main_fn,
1614 ctx->param_vertex_id),
1615 LLVMGetParam(radeon_bld->main_fn,
1616 SI_PARAM_BASE_VERTEX), "");
1617 break;
1618
1619 case TGSI_SEMANTIC_VERTEXID_NOBASE:
1620 value = LLVMGetParam(radeon_bld->main_fn,
1621 ctx->param_vertex_id);
1622 break;
1623
1624 case TGSI_SEMANTIC_BASEVERTEX:
1625 value = LLVMGetParam(radeon_bld->main_fn,
1626 SI_PARAM_BASE_VERTEX);
1627 break;
1628
1629 case TGSI_SEMANTIC_BASEINSTANCE:
1630 value = LLVMGetParam(radeon_bld->main_fn,
1631 SI_PARAM_START_INSTANCE);
1632 break;
1633
1634 case TGSI_SEMANTIC_DRAWID:
1635 value = LLVMGetParam(radeon_bld->main_fn,
1636 SI_PARAM_DRAWID);
1637 break;
1638
1639 case TGSI_SEMANTIC_INVOCATIONID:
1640 if (ctx->type == PIPE_SHADER_TESS_CTRL)
1641 value = unpack_param(ctx, SI_PARAM_REL_IDS, 8, 5);
1642 else if (ctx->type == PIPE_SHADER_GEOMETRY)
1643 value = LLVMGetParam(radeon_bld->main_fn,
1644 SI_PARAM_GS_INSTANCE_ID);
1645 else
1646 assert(!"INVOCATIONID not implemented");
1647 break;
1648
1649 case TGSI_SEMANTIC_POSITION:
1650 {
1651 LLVMValueRef pos[4] = {
1652 LLVMGetParam(radeon_bld->main_fn, SI_PARAM_POS_X_FLOAT),
1653 LLVMGetParam(radeon_bld->main_fn, SI_PARAM_POS_Y_FLOAT),
1654 LLVMGetParam(radeon_bld->main_fn, SI_PARAM_POS_Z_FLOAT),
1655 lp_build_emit_llvm_unary(&radeon_bld->soa.bld_base, TGSI_OPCODE_RCP,
1656 LLVMGetParam(radeon_bld->main_fn,
1657 SI_PARAM_POS_W_FLOAT)),
1658 };
1659 value = lp_build_gather_values(gallivm, pos, 4);
1660 break;
1661 }
1662
1663 case TGSI_SEMANTIC_FACE:
1664 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_FRONT_FACE);
1665 break;
1666
1667 case TGSI_SEMANTIC_SAMPLEID:
1668 value = get_sample_id(radeon_bld);
1669 break;
1670
1671 case TGSI_SEMANTIC_SAMPLEPOS: {
1672 LLVMValueRef pos[4] = {
1673 LLVMGetParam(radeon_bld->main_fn, SI_PARAM_POS_X_FLOAT),
1674 LLVMGetParam(radeon_bld->main_fn, SI_PARAM_POS_Y_FLOAT),
1675 lp_build_const_float(gallivm, 0),
1676 lp_build_const_float(gallivm, 0)
1677 };
1678 pos[0] = lp_build_emit_llvm_unary(&radeon_bld->soa.bld_base,
1679 TGSI_OPCODE_FRC, pos[0]);
1680 pos[1] = lp_build_emit_llvm_unary(&radeon_bld->soa.bld_base,
1681 TGSI_OPCODE_FRC, pos[1]);
1682 value = lp_build_gather_values(gallivm, pos, 4);
1683 break;
1684 }
1685
1686 case TGSI_SEMANTIC_SAMPLEMASK:
1687 /* This can only occur with the OpenGL Core profile, which
1688 * doesn't support smoothing.
1689 */
1690 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_SAMPLE_COVERAGE);
1691 break;
1692
1693 case TGSI_SEMANTIC_TESSCOORD:
1694 {
1695 LLVMValueRef coord[4] = {
1696 LLVMGetParam(radeon_bld->main_fn, ctx->param_tes_u),
1697 LLVMGetParam(radeon_bld->main_fn, ctx->param_tes_v),
1698 bld->zero,
1699 bld->zero
1700 };
1701
1702 /* For triangles, the vector should be (u, v, 1-u-v). */
1703 if (ctx->shader->selector->info.properties[TGSI_PROPERTY_TES_PRIM_MODE] ==
1704 PIPE_PRIM_TRIANGLES)
1705 coord[2] = lp_build_sub(bld, bld->one,
1706 lp_build_add(bld, coord[0], coord[1]));
1707
1708 value = lp_build_gather_values(gallivm, coord, 4);
1709 break;
1710 }
1711
1712 case TGSI_SEMANTIC_VERTICESIN:
1713 if (ctx->type == PIPE_SHADER_TESS_CTRL)
1714 value = unpack_param(ctx, SI_PARAM_TCS_OUT_LAYOUT, 26, 6);
1715 else if (ctx->type == PIPE_SHADER_TESS_EVAL)
1716 value = unpack_param(ctx, SI_PARAM_TCS_OFFCHIP_LAYOUT, 9, 7);
1717 else
1718 assert(!"invalid shader stage for TGSI_SEMANTIC_VERTICESIN");
1719 break;
1720
1721 case TGSI_SEMANTIC_TESSINNER:
1722 case TGSI_SEMANTIC_TESSOUTER:
1723 {
1724 LLVMValueRef rw_buffers, buffer, base, addr;
1725 int param = si_shader_io_get_unique_index(decl->Semantic.Name, 0);
1726
1727 rw_buffers = LLVMGetParam(ctx->radeon_bld.main_fn,
1728 SI_PARAM_RW_BUFFERS);
1729 buffer = build_indexed_load_const(ctx, rw_buffers,
1730 lp_build_const_int32(gallivm, SI_HS_RING_TESS_OFFCHIP));
1731
1732 base = LLVMGetParam(ctx->radeon_bld.main_fn, ctx->param_oc_lds);
1733 addr = get_tcs_tes_buffer_address(ctx, NULL,
1734 lp_build_const_int32(gallivm, param));
1735
1736 value = buffer_load(&radeon_bld->soa.bld_base, TGSI_TYPE_FLOAT,
1737 ~0, buffer, base, addr);
1738
1739 break;
1740 }
1741
1742 case TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI:
1743 case TGSI_SEMANTIC_DEFAULT_TESSINNER_SI:
1744 {
1745 LLVMValueRef buf, slot, val[4];
1746 int i, offset;
1747
1748 slot = lp_build_const_int32(gallivm, SI_HS_CONST_DEFAULT_TESS_LEVELS);
1749 buf = LLVMGetParam(ctx->radeon_bld.main_fn, SI_PARAM_RW_BUFFERS);
1750 buf = build_indexed_load_const(ctx, buf, slot);
1751 offset = decl->Semantic.Name == TGSI_SEMANTIC_DEFAULT_TESSINNER_SI ? 4 : 0;
1752
1753 for (i = 0; i < 4; i++)
1754 val[i] = buffer_load_const(ctx, buf,
1755 lp_build_const_int32(gallivm, (offset + i) * 4));
1756 value = lp_build_gather_values(gallivm, val, 4);
1757 break;
1758 }
1759
1760 case TGSI_SEMANTIC_PRIMID:
1761 value = get_primitive_id(&radeon_bld->soa.bld_base, 0);
1762 break;
1763
1764 case TGSI_SEMANTIC_GRID_SIZE:
1765 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_GRID_SIZE);
1766 break;
1767
1768 case TGSI_SEMANTIC_BLOCK_SIZE:
1769 {
1770 LLVMValueRef values[3];
1771 unsigned i;
1772 unsigned *properties = ctx->shader->selector->info.properties;
1773 unsigned sizes[3] = {
1774 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH],
1775 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT],
1776 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH]
1777 };
1778
1779 for (i = 0; i < 3; ++i)
1780 values[i] = lp_build_const_int32(gallivm, sizes[i]);
1781
1782 value = lp_build_gather_values(gallivm, values, 3);
1783 break;
1784 }
1785
1786 case TGSI_SEMANTIC_BLOCK_ID:
1787 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_BLOCK_ID);
1788 break;
1789
1790 case TGSI_SEMANTIC_THREAD_ID:
1791 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_THREAD_ID);
1792 break;
1793
1794 #if HAVE_LLVM >= 0x0309
1795 case TGSI_SEMANTIC_HELPER_INVOCATION:
1796 value = lp_build_intrinsic(gallivm->builder,
1797 "llvm.amdgcn.ps.live",
1798 ctx->i1, NULL, 0,
1799 LLVMReadNoneAttribute);
1800 value = LLVMBuildNot(gallivm->builder, value, "");
1801 value = LLVMBuildSExt(gallivm->builder, value, ctx->i32, "");
1802 break;
1803 #endif
1804
1805 default:
1806 assert(!"unknown system value");
1807 return;
1808 }
1809
1810 radeon_bld->system_values[index] = value;
1811 }
1812
1813 static void declare_compute_memory(struct radeon_llvm_context *radeon_bld,
1814 const struct tgsi_full_declaration *decl)
1815 {
1816 struct si_shader_context *ctx =
1817 si_shader_context(&radeon_bld->soa.bld_base);
1818 struct si_shader_selector *sel = ctx->shader->selector;
1819 struct gallivm_state *gallivm = &radeon_bld->gallivm;
1820
1821 LLVMTypeRef i8p = LLVMPointerType(ctx->i8, LOCAL_ADDR_SPACE);
1822 LLVMValueRef var;
1823
1824 assert(decl->Declaration.MemType == TGSI_MEMORY_TYPE_SHARED);
1825 assert(decl->Range.First == decl->Range.Last);
1826 assert(!ctx->shared_memory);
1827
1828 var = LLVMAddGlobalInAddressSpace(gallivm->module,
1829 LLVMArrayType(ctx->i8, sel->local_size),
1830 "compute_lds",
1831 LOCAL_ADDR_SPACE);
1832 LLVMSetAlignment(var, 4);
1833
1834 ctx->shared_memory = LLVMBuildBitCast(gallivm->builder, var, i8p, "");
1835 }
1836
1837 static LLVMValueRef load_const_buffer_desc(struct si_shader_context *ctx, int i)
1838 {
1839 LLVMValueRef list_ptr = LLVMGetParam(ctx->radeon_bld.main_fn,
1840 SI_PARAM_CONST_BUFFERS);
1841
1842 return build_indexed_load_const(ctx, list_ptr,
1843 LLVMConstInt(ctx->i32, i, 0));
1844 }
1845
1846 static LLVMValueRef fetch_constant(
1847 struct lp_build_tgsi_context *bld_base,
1848 const struct tgsi_full_src_register *reg,
1849 enum tgsi_opcode_type type,
1850 unsigned swizzle)
1851 {
1852 struct si_shader_context *ctx = si_shader_context(bld_base);
1853 struct lp_build_context *base = &bld_base->base;
1854 const struct tgsi_ind_register *ireg = &reg->Indirect;
1855 unsigned buf, idx;
1856
1857 LLVMValueRef addr, bufp;
1858 LLVMValueRef result;
1859
1860 if (swizzle == LP_CHAN_ALL) {
1861 unsigned chan;
1862 LLVMValueRef values[4];
1863 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
1864 values[chan] = fetch_constant(bld_base, reg, type, chan);
1865
1866 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
1867 }
1868
1869 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
1870 idx = reg->Register.Index * 4 + swizzle;
1871
1872 if (!reg->Register.Indirect && !reg->Dimension.Indirect) {
1873 LLVMValueRef c0, c1, desc;
1874
1875 desc = load_const_buffer_desc(ctx, buf);
1876 c0 = buffer_load_const(ctx, desc,
1877 LLVMConstInt(ctx->i32, idx * 4, 0));
1878
1879 if (!tgsi_type_is_64bit(type))
1880 return bitcast(bld_base, type, c0);
1881 else {
1882 c1 = buffer_load_const(ctx, desc,
1883 LLVMConstInt(ctx->i32,
1884 (idx + 1) * 4, 0));
1885 return radeon_llvm_emit_fetch_64bit(bld_base, type,
1886 c0, c1);
1887 }
1888 }
1889
1890 if (reg->Register.Dimension && reg->Dimension.Indirect) {
1891 LLVMValueRef ptr = LLVMGetParam(ctx->radeon_bld.main_fn, SI_PARAM_CONST_BUFFERS);
1892 LLVMValueRef index;
1893 index = get_bounded_indirect_index(ctx, &reg->DimIndirect,
1894 reg->Dimension.Index,
1895 SI_NUM_CONST_BUFFERS);
1896 bufp = build_indexed_load_const(ctx, ptr, index);
1897 } else
1898 bufp = load_const_buffer_desc(ctx, buf);
1899
1900 addr = ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
1901 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
1902 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
1903 addr = lp_build_add(&bld_base->uint_bld, addr,
1904 lp_build_const_int32(base->gallivm, idx * 4));
1905
1906 result = buffer_load_const(ctx, bufp, addr);
1907
1908 if (!tgsi_type_is_64bit(type))
1909 result = bitcast(bld_base, type, result);
1910 else {
1911 LLVMValueRef addr2, result2;
1912 addr2 = ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle + 1];
1913 addr2 = LLVMBuildLoad(base->gallivm->builder, addr2, "load addr reg2");
1914 addr2 = lp_build_mul_imm(&bld_base->uint_bld, addr2, 16);
1915 addr2 = lp_build_add(&bld_base->uint_bld, addr2,
1916 lp_build_const_int32(base->gallivm, idx * 4));
1917
1918 result2 = buffer_load_const(ctx, bufp, addr2);
1919
1920 result = radeon_llvm_emit_fetch_64bit(bld_base, type,
1921 result, result2);
1922 }
1923 return result;
1924 }
1925
1926 /* Upper 16 bits must be zero. */
1927 static LLVMValueRef si_llvm_pack_two_int16(struct gallivm_state *gallivm,
1928 LLVMValueRef val[2])
1929 {
1930 return LLVMBuildOr(gallivm->builder, val[0],
1931 LLVMBuildShl(gallivm->builder, val[1],
1932 lp_build_const_int32(gallivm, 16),
1933 ""), "");
1934 }
1935
1936 /* Upper 16 bits are ignored and will be dropped. */
1937 static LLVMValueRef si_llvm_pack_two_int32_as_int16(struct gallivm_state *gallivm,
1938 LLVMValueRef val[2])
1939 {
1940 LLVMValueRef v[2] = {
1941 LLVMBuildAnd(gallivm->builder, val[0],
1942 lp_build_const_int32(gallivm, 0xffff), ""),
1943 val[1],
1944 };
1945 return si_llvm_pack_two_int16(gallivm, v);
1946 }
1947
1948 /* Initialize arguments for the shader export intrinsic */
1949 static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
1950 LLVMValueRef *values,
1951 unsigned target,
1952 LLVMValueRef *args)
1953 {
1954 struct si_shader_context *ctx = si_shader_context(bld_base);
1955 struct lp_build_context *uint =
1956 &ctx->radeon_bld.soa.bld_base.uint_bld;
1957 struct lp_build_context *base = &bld_base->base;
1958 struct gallivm_state *gallivm = base->gallivm;
1959 LLVMBuilderRef builder = base->gallivm->builder;
1960 LLVMValueRef val[4];
1961 unsigned spi_shader_col_format = V_028714_SPI_SHADER_32_ABGR;
1962 unsigned chan;
1963 bool is_int8;
1964
1965 /* Default is 0xf. Adjusted below depending on the format. */
1966 args[0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1967
1968 /* Specify whether the EXEC mask represents the valid mask */
1969 args[1] = uint->zero;
1970
1971 /* Specify whether this is the last export */
1972 args[2] = uint->zero;
1973
1974 /* Specify the target we are exporting */
1975 args[3] = lp_build_const_int32(base->gallivm, target);
1976
1977 if (ctx->type == PIPE_SHADER_FRAGMENT) {
1978 const union si_shader_key *key = &ctx->shader->key;
1979 unsigned col_formats = key->ps.epilog.spi_shader_col_format;
1980 int cbuf = target - V_008DFC_SQ_EXP_MRT;
1981
1982 assert(cbuf >= 0 && cbuf < 8);
1983 spi_shader_col_format = (col_formats >> (cbuf * 4)) & 0xf;
1984 is_int8 = (key->ps.epilog.color_is_int8 >> cbuf) & 0x1;
1985 }
1986
1987 args[4] = uint->zero; /* COMPR flag */
1988 args[5] = base->undef;
1989 args[6] = base->undef;
1990 args[7] = base->undef;
1991 args[8] = base->undef;
1992
1993 switch (spi_shader_col_format) {
1994 case V_028714_SPI_SHADER_ZERO:
1995 args[0] = uint->zero; /* writemask */
1996 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_NULL);
1997 break;
1998
1999 case V_028714_SPI_SHADER_32_R:
2000 args[0] = uint->one; /* writemask */
2001 args[5] = values[0];
2002 break;
2003
2004 case V_028714_SPI_SHADER_32_GR:
2005 args[0] = lp_build_const_int32(base->gallivm, 0x3); /* writemask */
2006 args[5] = values[0];
2007 args[6] = values[1];
2008 break;
2009
2010 case V_028714_SPI_SHADER_32_AR:
2011 args[0] = lp_build_const_int32(base->gallivm, 0x9); /* writemask */
2012 args[5] = values[0];
2013 args[8] = values[3];
2014 break;
2015
2016 case V_028714_SPI_SHADER_FP16_ABGR:
2017 args[4] = uint->one; /* COMPR flag */
2018
2019 for (chan = 0; chan < 2; chan++) {
2020 LLVMValueRef pack_args[2] = {
2021 values[2 * chan],
2022 values[2 * chan + 1]
2023 };
2024 LLVMValueRef packed;
2025
2026 packed = lp_build_intrinsic(base->gallivm->builder,
2027 "llvm.SI.packf16",
2028 ctx->i32, pack_args, 2,
2029 LLVMReadNoneAttribute);
2030 args[chan + 5] =
2031 LLVMBuildBitCast(base->gallivm->builder,
2032 packed, ctx->f32, "");
2033 }
2034 break;
2035
2036 case V_028714_SPI_SHADER_UNORM16_ABGR:
2037 for (chan = 0; chan < 4; chan++) {
2038 val[chan] = radeon_llvm_saturate(bld_base, values[chan]);
2039 val[chan] = LLVMBuildFMul(builder, val[chan],
2040 lp_build_const_float(gallivm, 65535), "");
2041 val[chan] = LLVMBuildFAdd(builder, val[chan],
2042 lp_build_const_float(gallivm, 0.5), "");
2043 val[chan] = LLVMBuildFPToUI(builder, val[chan],
2044 ctx->i32, "");
2045 }
2046
2047 args[4] = uint->one; /* COMPR flag */
2048 args[5] = bitcast(bld_base, TGSI_TYPE_FLOAT,
2049 si_llvm_pack_two_int16(gallivm, val));
2050 args[6] = bitcast(bld_base, TGSI_TYPE_FLOAT,
2051 si_llvm_pack_two_int16(gallivm, val+2));
2052 break;
2053
2054 case V_028714_SPI_SHADER_SNORM16_ABGR:
2055 for (chan = 0; chan < 4; chan++) {
2056 /* Clamp between [-1, 1]. */
2057 val[chan] = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MIN,
2058 values[chan],
2059 lp_build_const_float(gallivm, 1));
2060 val[chan] = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MAX,
2061 val[chan],
2062 lp_build_const_float(gallivm, -1));
2063 /* Convert to a signed integer in [-32767, 32767]. */
2064 val[chan] = LLVMBuildFMul(builder, val[chan],
2065 lp_build_const_float(gallivm, 32767), "");
2066 /* If positive, add 0.5, else add -0.5. */
2067 val[chan] = LLVMBuildFAdd(builder, val[chan],
2068 LLVMBuildSelect(builder,
2069 LLVMBuildFCmp(builder, LLVMRealOGE,
2070 val[chan], base->zero, ""),
2071 lp_build_const_float(gallivm, 0.5),
2072 lp_build_const_float(gallivm, -0.5), ""), "");
2073 val[chan] = LLVMBuildFPToSI(builder, val[chan], ctx->i32, "");
2074 }
2075
2076 args[4] = uint->one; /* COMPR flag */
2077 args[5] = bitcast(bld_base, TGSI_TYPE_FLOAT,
2078 si_llvm_pack_two_int32_as_int16(gallivm, val));
2079 args[6] = bitcast(bld_base, TGSI_TYPE_FLOAT,
2080 si_llvm_pack_two_int32_as_int16(gallivm, val+2));
2081 break;
2082
2083 case V_028714_SPI_SHADER_UINT16_ABGR: {
2084 LLVMValueRef max = lp_build_const_int32(gallivm, is_int8 ?
2085 255 : 65535);
2086 /* Clamp. */
2087 for (chan = 0; chan < 4; chan++) {
2088 val[chan] = bitcast(bld_base, TGSI_TYPE_UNSIGNED, values[chan]);
2089 val[chan] = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_UMIN,
2090 val[chan], max);
2091 }
2092
2093 args[4] = uint->one; /* COMPR flag */
2094 args[5] = bitcast(bld_base, TGSI_TYPE_FLOAT,
2095 si_llvm_pack_two_int16(gallivm, val));
2096 args[6] = bitcast(bld_base, TGSI_TYPE_FLOAT,
2097 si_llvm_pack_two_int16(gallivm, val+2));
2098 break;
2099 }
2100
2101 case V_028714_SPI_SHADER_SINT16_ABGR: {
2102 LLVMValueRef max = lp_build_const_int32(gallivm, is_int8 ?
2103 127 : 32767);
2104 LLVMValueRef min = lp_build_const_int32(gallivm, is_int8 ?
2105 -128 : -32768);
2106 /* Clamp. */
2107 for (chan = 0; chan < 4; chan++) {
2108 val[chan] = bitcast(bld_base, TGSI_TYPE_UNSIGNED, values[chan]);
2109 val[chan] = lp_build_emit_llvm_binary(bld_base,
2110 TGSI_OPCODE_IMIN,
2111 val[chan], max);
2112 val[chan] = lp_build_emit_llvm_binary(bld_base,
2113 TGSI_OPCODE_IMAX,
2114 val[chan], min);
2115 }
2116
2117 args[4] = uint->one; /* COMPR flag */
2118 args[5] = bitcast(bld_base, TGSI_TYPE_FLOAT,
2119 si_llvm_pack_two_int32_as_int16(gallivm, val));
2120 args[6] = bitcast(bld_base, TGSI_TYPE_FLOAT,
2121 si_llvm_pack_two_int32_as_int16(gallivm, val+2));
2122 break;
2123 }
2124
2125 case V_028714_SPI_SHADER_32_ABGR:
2126 memcpy(&args[5], values, sizeof(values[0]) * 4);
2127 break;
2128 }
2129 }
2130
2131 static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
2132 LLVMValueRef alpha)
2133 {
2134 struct si_shader_context *ctx = si_shader_context(bld_base);
2135 struct gallivm_state *gallivm = bld_base->base.gallivm;
2136
2137 if (ctx->shader->key.ps.epilog.alpha_func != PIPE_FUNC_NEVER) {
2138 LLVMValueRef alpha_ref = LLVMGetParam(ctx->radeon_bld.main_fn,
2139 SI_PARAM_ALPHA_REF);
2140
2141 LLVMValueRef alpha_pass =
2142 lp_build_cmp(&bld_base->base,
2143 ctx->shader->key.ps.epilog.alpha_func,
2144 alpha, alpha_ref);
2145 LLVMValueRef arg =
2146 lp_build_select(&bld_base->base,
2147 alpha_pass,
2148 lp_build_const_float(gallivm, 1.0f),
2149 lp_build_const_float(gallivm, -1.0f));
2150
2151 lp_build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2152 ctx->voidt, &arg, 1, 0);
2153 } else {
2154 lp_build_intrinsic(gallivm->builder, "llvm.AMDGPU.kilp",
2155 ctx->voidt, NULL, 0, 0);
2156 }
2157 }
2158
2159 static LLVMValueRef si_scale_alpha_by_sample_mask(struct lp_build_tgsi_context *bld_base,
2160 LLVMValueRef alpha,
2161 unsigned samplemask_param)
2162 {
2163 struct si_shader_context *ctx = si_shader_context(bld_base);
2164 struct gallivm_state *gallivm = bld_base->base.gallivm;
2165 LLVMValueRef coverage;
2166
2167 /* alpha = alpha * popcount(coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
2168 coverage = LLVMGetParam(ctx->radeon_bld.main_fn,
2169 samplemask_param);
2170 coverage = bitcast(bld_base, TGSI_TYPE_SIGNED, coverage);
2171
2172 coverage = lp_build_intrinsic(gallivm->builder, "llvm.ctpop.i32",
2173 ctx->i32,
2174 &coverage, 1, LLVMReadNoneAttribute);
2175
2176 coverage = LLVMBuildUIToFP(gallivm->builder, coverage,
2177 ctx->f32, "");
2178
2179 coverage = LLVMBuildFMul(gallivm->builder, coverage,
2180 lp_build_const_float(gallivm,
2181 1.0 / SI_NUM_SMOOTH_AA_SAMPLES), "");
2182
2183 return LLVMBuildFMul(gallivm->builder, alpha, coverage, "");
2184 }
2185
2186 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context *bld_base,
2187 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
2188 {
2189 struct si_shader_context *ctx = si_shader_context(bld_base);
2190 struct lp_build_context *base = &bld_base->base;
2191 struct lp_build_context *uint = &ctx->radeon_bld.soa.bld_base.uint_bld;
2192 unsigned reg_index;
2193 unsigned chan;
2194 unsigned const_chan;
2195 LLVMValueRef base_elt;
2196 LLVMValueRef ptr = LLVMGetParam(ctx->radeon_bld.main_fn, SI_PARAM_RW_BUFFERS);
2197 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm,
2198 SI_VS_CONST_CLIP_PLANES);
2199 LLVMValueRef const_resource = build_indexed_load_const(ctx, ptr, constbuf_index);
2200
2201 for (reg_index = 0; reg_index < 2; reg_index ++) {
2202 LLVMValueRef *args = pos[2 + reg_index];
2203
2204 args[5] =
2205 args[6] =
2206 args[7] =
2207 args[8] = lp_build_const_float(base->gallivm, 0.0f);
2208
2209 /* Compute dot products of position and user clip plane vectors */
2210 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
2211 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
2212 args[1] = lp_build_const_int32(base->gallivm,
2213 ((reg_index * 4 + chan) * 4 +
2214 const_chan) * 4);
2215 base_elt = buffer_load_const(ctx, const_resource,
2216 args[1]);
2217 args[5 + chan] =
2218 lp_build_add(base, args[5 + chan],
2219 lp_build_mul(base, base_elt,
2220 out_elts[const_chan]));
2221 }
2222 }
2223
2224 args[0] = lp_build_const_int32(base->gallivm, 0xf);
2225 args[1] = uint->zero;
2226 args[2] = uint->zero;
2227 args[3] = lp_build_const_int32(base->gallivm,
2228 V_008DFC_SQ_EXP_POS + 2 + reg_index);
2229 args[4] = uint->zero;
2230 }
2231 }
2232
2233 static void si_dump_streamout(struct pipe_stream_output_info *so)
2234 {
2235 unsigned i;
2236
2237 if (so->num_outputs)
2238 fprintf(stderr, "STREAMOUT\n");
2239
2240 for (i = 0; i < so->num_outputs; i++) {
2241 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
2242 so->output[i].start_component;
2243 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
2244 i, so->output[i].output_buffer,
2245 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
2246 so->output[i].register_index,
2247 mask & 1 ? "x" : "",
2248 mask & 2 ? "y" : "",
2249 mask & 4 ? "z" : "",
2250 mask & 8 ? "w" : "");
2251 }
2252 }
2253
2254 /* On SI, the vertex shader is responsible for writing streamout data
2255 * to buffers. */
2256 static void si_llvm_emit_streamout(struct si_shader_context *ctx,
2257 struct si_shader_output_values *outputs,
2258 unsigned noutput)
2259 {
2260 struct pipe_stream_output_info *so = &ctx->shader->selector->so;
2261 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
2262 LLVMBuilderRef builder = gallivm->builder;
2263 int i, j;
2264 struct lp_build_if_state if_ctx;
2265 LLVMValueRef so_buffers[4];
2266 LLVMValueRef buf_ptr = LLVMGetParam(ctx->radeon_bld.main_fn,
2267 SI_PARAM_RW_BUFFERS);
2268
2269 /* Load the descriptors. */
2270 for (i = 0; i < 4; ++i) {
2271 if (ctx->shader->selector->so.stride[i]) {
2272 LLVMValueRef offset = lp_build_const_int32(gallivm,
2273 SI_VS_STREAMOUT_BUF0 + i);
2274
2275 so_buffers[i] = build_indexed_load_const(ctx, buf_ptr, offset);
2276 }
2277 }
2278
2279 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2280 LLVMValueRef so_vtx_count =
2281 unpack_param(ctx, ctx->param_streamout_config, 16, 7);
2282
2283 LLVMValueRef tid = get_thread_id(ctx);
2284
2285 /* can_emit = tid < so_vtx_count; */
2286 LLVMValueRef can_emit =
2287 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
2288
2289 LLVMValueRef stream_id =
2290 unpack_param(ctx, ctx->param_streamout_config, 24, 2);
2291
2292 /* Emit the streamout code conditionally. This actually avoids
2293 * out-of-bounds buffer access. The hw tells us via the SGPR
2294 * (so_vtx_count) which threads are allowed to emit streamout data. */
2295 lp_build_if(&if_ctx, gallivm, can_emit);
2296 {
2297 /* The buffer offset is computed as follows:
2298 * ByteOffset = streamout_offset[buffer_id]*4 +
2299 * (streamout_write_index + thread_id)*stride[buffer_id] +
2300 * attrib_offset
2301 */
2302
2303 LLVMValueRef so_write_index =
2304 LLVMGetParam(ctx->radeon_bld.main_fn,
2305 ctx->param_streamout_write_index);
2306
2307 /* Compute (streamout_write_index + thread_id). */
2308 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
2309
2310 /* Compute the write offset for each enabled buffer. */
2311 LLVMValueRef so_write_offset[4] = {};
2312 for (i = 0; i < 4; i++) {
2313 if (!so->stride[i])
2314 continue;
2315
2316 LLVMValueRef so_offset = LLVMGetParam(ctx->radeon_bld.main_fn,
2317 ctx->param_streamout_offset[i]);
2318 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(ctx->i32, 4, 0), "");
2319
2320 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
2321 LLVMConstInt(ctx->i32, so->stride[i]*4, 0), "");
2322 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
2323 }
2324
2325 /* Write streamout data. */
2326 for (i = 0; i < so->num_outputs; i++) {
2327 unsigned buf_idx = so->output[i].output_buffer;
2328 unsigned reg = so->output[i].register_index;
2329 unsigned start = so->output[i].start_component;
2330 unsigned num_comps = so->output[i].num_components;
2331 unsigned stream = so->output[i].stream;
2332 LLVMValueRef out[4];
2333 struct lp_build_if_state if_ctx_stream;
2334
2335 assert(num_comps && num_comps <= 4);
2336 if (!num_comps || num_comps > 4)
2337 continue;
2338
2339 if (reg >= noutput)
2340 continue;
2341
2342 /* Load the output as int. */
2343 for (j = 0; j < num_comps; j++) {
2344 out[j] = LLVMBuildBitCast(builder,
2345 outputs[reg].values[start+j],
2346 ctx->i32, "");
2347 }
2348
2349 /* Pack the output. */
2350 LLVMValueRef vdata = NULL;
2351
2352 switch (num_comps) {
2353 case 1: /* as i32 */
2354 vdata = out[0];
2355 break;
2356 case 2: /* as v2i32 */
2357 case 3: /* as v4i32 (aligned to 4) */
2358 case 4: /* as v4i32 */
2359 vdata = LLVMGetUndef(LLVMVectorType(ctx->i32, util_next_power_of_two(num_comps)));
2360 for (j = 0; j < num_comps; j++) {
2361 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
2362 LLVMConstInt(ctx->i32, j, 0), "");
2363 }
2364 break;
2365 }
2366
2367 LLVMValueRef can_emit_stream =
2368 LLVMBuildICmp(builder, LLVMIntEQ,
2369 stream_id,
2370 lp_build_const_int32(gallivm, stream), "");
2371
2372 lp_build_if(&if_ctx_stream, gallivm, can_emit_stream);
2373 build_tbuffer_store_dwords(ctx, so_buffers[buf_idx],
2374 vdata, num_comps,
2375 so_write_offset[buf_idx],
2376 LLVMConstInt(ctx->i32, 0, 0),
2377 so->output[i].dst_offset*4);
2378 lp_build_endif(&if_ctx_stream);
2379 }
2380 }
2381 lp_build_endif(&if_ctx);
2382 }
2383
2384
2385 /* Generate export instructions for hardware VS shader stage */
2386 static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
2387 struct si_shader_output_values *outputs,
2388 unsigned noutput)
2389 {
2390 struct si_shader_context *ctx = si_shader_context(bld_base);
2391 struct si_shader *shader = ctx->shader;
2392 struct lp_build_context *base = &bld_base->base;
2393 struct lp_build_context *uint =
2394 &ctx->radeon_bld.soa.bld_base.uint_bld;
2395 LLVMValueRef args[9];
2396 LLVMValueRef pos_args[4][9] = { { 0 } };
2397 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL, viewport_index_value = NULL;
2398 unsigned semantic_name, semantic_index;
2399 unsigned target;
2400 unsigned param_count = 0;
2401 unsigned pos_idx;
2402 int i;
2403
2404 if (outputs && ctx->shader->selector->so.num_outputs) {
2405 si_llvm_emit_streamout(ctx, outputs, noutput);
2406 }
2407
2408 for (i = 0; i < noutput; i++) {
2409 semantic_name = outputs[i].name;
2410 semantic_index = outputs[i].sid;
2411
2412 handle_semantic:
2413 /* Select the correct target */
2414 switch(semantic_name) {
2415 case TGSI_SEMANTIC_PSIZE:
2416 psize_value = outputs[i].values[0];
2417 continue;
2418 case TGSI_SEMANTIC_EDGEFLAG:
2419 edgeflag_value = outputs[i].values[0];
2420 continue;
2421 case TGSI_SEMANTIC_LAYER:
2422 layer_value = outputs[i].values[0];
2423 semantic_name = TGSI_SEMANTIC_GENERIC;
2424 goto handle_semantic;
2425 case TGSI_SEMANTIC_VIEWPORT_INDEX:
2426 viewport_index_value = outputs[i].values[0];
2427 semantic_name = TGSI_SEMANTIC_GENERIC;
2428 goto handle_semantic;
2429 case TGSI_SEMANTIC_POSITION:
2430 target = V_008DFC_SQ_EXP_POS;
2431 break;
2432 case TGSI_SEMANTIC_COLOR:
2433 case TGSI_SEMANTIC_BCOLOR:
2434 target = V_008DFC_SQ_EXP_PARAM + param_count;
2435 assert(i < ARRAY_SIZE(shader->info.vs_output_param_offset));
2436 shader->info.vs_output_param_offset[i] = param_count;
2437 param_count++;
2438 break;
2439 case TGSI_SEMANTIC_CLIPDIST:
2440 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
2441 break;
2442 case TGSI_SEMANTIC_CLIPVERTEX:
2443 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
2444 continue;
2445 case TGSI_SEMANTIC_PRIMID:
2446 case TGSI_SEMANTIC_FOG:
2447 case TGSI_SEMANTIC_TEXCOORD:
2448 case TGSI_SEMANTIC_GENERIC:
2449 target = V_008DFC_SQ_EXP_PARAM + param_count;
2450 assert(i < ARRAY_SIZE(shader->info.vs_output_param_offset));
2451 shader->info.vs_output_param_offset[i] = param_count;
2452 param_count++;
2453 break;
2454 default:
2455 target = 0;
2456 fprintf(stderr,
2457 "Warning: SI unhandled vs output type:%d\n",
2458 semantic_name);
2459 }
2460
2461 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
2462
2463 if (target >= V_008DFC_SQ_EXP_POS &&
2464 target <= (V_008DFC_SQ_EXP_POS + 3)) {
2465 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
2466 args, sizeof(args));
2467 } else {
2468 lp_build_intrinsic(base->gallivm->builder,
2469 "llvm.SI.export", ctx->voidt,
2470 args, 9, 0);
2471 }
2472
2473 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
2474 semantic_name = TGSI_SEMANTIC_GENERIC;
2475 goto handle_semantic;
2476 }
2477 }
2478
2479 shader->info.nr_param_exports = param_count;
2480
2481 /* We need to add the position output manually if it's missing. */
2482 if (!pos_args[0][0]) {
2483 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
2484 pos_args[0][1] = uint->zero; /* EXEC mask */
2485 pos_args[0][2] = uint->zero; /* last export? */
2486 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
2487 pos_args[0][4] = uint->zero; /* COMPR flag */
2488 pos_args[0][5] = base->zero; /* X */
2489 pos_args[0][6] = base->zero; /* Y */
2490 pos_args[0][7] = base->zero; /* Z */
2491 pos_args[0][8] = base->one; /* W */
2492 }
2493
2494 /* Write the misc vector (point size, edgeflag, layer, viewport). */
2495 if (shader->selector->info.writes_psize ||
2496 shader->selector->info.writes_edgeflag ||
2497 shader->selector->info.writes_viewport_index ||
2498 shader->selector->info.writes_layer) {
2499 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
2500 shader->selector->info.writes_psize |
2501 (shader->selector->info.writes_edgeflag << 1) |
2502 (shader->selector->info.writes_layer << 2) |
2503 (shader->selector->info.writes_viewport_index << 3));
2504 pos_args[1][1] = uint->zero; /* EXEC mask */
2505 pos_args[1][2] = uint->zero; /* last export? */
2506 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
2507 pos_args[1][4] = uint->zero; /* COMPR flag */
2508 pos_args[1][5] = base->zero; /* X */
2509 pos_args[1][6] = base->zero; /* Y */
2510 pos_args[1][7] = base->zero; /* Z */
2511 pos_args[1][8] = base->zero; /* W */
2512
2513 if (shader->selector->info.writes_psize)
2514 pos_args[1][5] = psize_value;
2515
2516 if (shader->selector->info.writes_edgeflag) {
2517 /* The output is a float, but the hw expects an integer
2518 * with the first bit containing the edge flag. */
2519 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
2520 edgeflag_value,
2521 ctx->i32, "");
2522 edgeflag_value = lp_build_min(&bld_base->int_bld,
2523 edgeflag_value,
2524 bld_base->int_bld.one);
2525
2526 /* The LLVM intrinsic expects a float. */
2527 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
2528 edgeflag_value,
2529 ctx->f32, "");
2530 }
2531
2532 if (shader->selector->info.writes_layer)
2533 pos_args[1][7] = layer_value;
2534
2535 if (shader->selector->info.writes_viewport_index)
2536 pos_args[1][8] = viewport_index_value;
2537 }
2538
2539 for (i = 0; i < 4; i++)
2540 if (pos_args[i][0])
2541 shader->info.nr_pos_exports++;
2542
2543 pos_idx = 0;
2544 for (i = 0; i < 4; i++) {
2545 if (!pos_args[i][0])
2546 continue;
2547
2548 /* Specify the target we are exporting */
2549 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
2550
2551 if (pos_idx == shader->info.nr_pos_exports)
2552 /* Specify that this is the last export */
2553 pos_args[i][2] = uint->one;
2554
2555 lp_build_intrinsic(base->gallivm->builder, "llvm.SI.export",
2556 ctx->voidt, pos_args[i], 9, 0);
2557 }
2558 }
2559
2560 static void si_copy_tcs_inputs(struct lp_build_tgsi_context *bld_base)
2561 {
2562 struct si_shader_context *ctx = si_shader_context(bld_base);
2563 struct gallivm_state *gallivm = bld_base->base.gallivm;
2564 LLVMValueRef invocation_id, rw_buffers, buffer, buffer_offset;
2565 LLVMValueRef lds_vertex_stride, lds_vertex_offset, lds_base;
2566 uint64_t inputs;
2567
2568 invocation_id = unpack_param(ctx, SI_PARAM_REL_IDS, 8, 5);
2569
2570 rw_buffers = LLVMGetParam(ctx->radeon_bld.main_fn, SI_PARAM_RW_BUFFERS);
2571 buffer = build_indexed_load_const(ctx, rw_buffers,
2572 lp_build_const_int32(gallivm, SI_HS_RING_TESS_OFFCHIP));
2573
2574 buffer_offset = LLVMGetParam(ctx->radeon_bld.main_fn, ctx->param_oc_lds);
2575
2576 lds_vertex_stride = unpack_param(ctx, SI_PARAM_TCS_IN_LAYOUT, 13, 8);
2577 lds_vertex_offset = LLVMBuildMul(gallivm->builder, invocation_id,
2578 lds_vertex_stride, "");
2579 lds_base = get_tcs_in_current_patch_offset(ctx);
2580 lds_base = LLVMBuildAdd(gallivm->builder, lds_base, lds_vertex_offset, "");
2581
2582 inputs = ctx->shader->key.tcs.epilog.inputs_to_copy;
2583 while (inputs) {
2584 unsigned i = u_bit_scan64(&inputs);
2585
2586 LLVMValueRef lds_ptr = LLVMBuildAdd(gallivm->builder, lds_base,
2587 lp_build_const_int32(gallivm, 4 * i),
2588 "");
2589
2590 LLVMValueRef buffer_addr = get_tcs_tes_buffer_address(ctx,
2591 invocation_id,
2592 lp_build_const_int32(gallivm, i));
2593
2594 LLVMValueRef value = lds_load(bld_base, TGSI_TYPE_SIGNED, ~0,
2595 lds_ptr);
2596
2597 build_tbuffer_store_dwords(ctx, buffer, value, 4, buffer_addr,
2598 buffer_offset, 0);
2599 }
2600 }
2601
2602 static void si_write_tess_factors(struct lp_build_tgsi_context *bld_base,
2603 LLVMValueRef rel_patch_id,
2604 LLVMValueRef invocation_id,
2605 LLVMValueRef tcs_out_current_patch_data_offset)
2606 {
2607 struct si_shader_context *ctx = si_shader_context(bld_base);
2608 struct gallivm_state *gallivm = bld_base->base.gallivm;
2609 struct si_shader *shader = ctx->shader;
2610 unsigned tess_inner_index, tess_outer_index;
2611 LLVMValueRef lds_base, lds_inner, lds_outer, byteoffset, buffer;
2612 LLVMValueRef out[6], vec0, vec1, rw_buffers, tf_base;
2613 unsigned stride, outer_comps, inner_comps, i;
2614 struct lp_build_if_state if_ctx, inner_if_ctx;
2615
2616 si_llvm_emit_barrier(NULL, bld_base, NULL);
2617
2618 /* Do this only for invocation 0, because the tess levels are per-patch,
2619 * not per-vertex.
2620 *
2621 * This can't jump, because invocation 0 executes this. It should
2622 * at least mask out the loads and stores for other invocations.
2623 */
2624 lp_build_if(&if_ctx, gallivm,
2625 LLVMBuildICmp(gallivm->builder, LLVMIntEQ,
2626 invocation_id, bld_base->uint_bld.zero, ""));
2627
2628 /* Determine the layout of one tess factor element in the buffer. */
2629 switch (shader->key.tcs.epilog.prim_mode) {
2630 case PIPE_PRIM_LINES:
2631 stride = 2; /* 2 dwords, 1 vec2 store */
2632 outer_comps = 2;
2633 inner_comps = 0;
2634 break;
2635 case PIPE_PRIM_TRIANGLES:
2636 stride = 4; /* 4 dwords, 1 vec4 store */
2637 outer_comps = 3;
2638 inner_comps = 1;
2639 break;
2640 case PIPE_PRIM_QUADS:
2641 stride = 6; /* 6 dwords, 2 stores (vec4 + vec2) */
2642 outer_comps = 4;
2643 inner_comps = 2;
2644 break;
2645 default:
2646 assert(0);
2647 return;
2648 }
2649
2650 /* Load tess_inner and tess_outer from LDS.
2651 * Any invocation can write them, so we can't get them from a temporary.
2652 */
2653 tess_inner_index = si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER, 0);
2654 tess_outer_index = si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER, 0);
2655
2656 lds_base = tcs_out_current_patch_data_offset;
2657 lds_inner = LLVMBuildAdd(gallivm->builder, lds_base,
2658 lp_build_const_int32(gallivm,
2659 tess_inner_index * 4), "");
2660 lds_outer = LLVMBuildAdd(gallivm->builder, lds_base,
2661 lp_build_const_int32(gallivm,
2662 tess_outer_index * 4), "");
2663
2664 for (i = 0; i < outer_comps; i++)
2665 out[i] = lds_load(bld_base, TGSI_TYPE_SIGNED, i, lds_outer);
2666 for (i = 0; i < inner_comps; i++)
2667 out[outer_comps+i] = lds_load(bld_base, TGSI_TYPE_SIGNED, i, lds_inner);
2668
2669 /* Convert the outputs to vectors for stores. */
2670 vec0 = lp_build_gather_values(gallivm, out, MIN2(stride, 4));
2671 vec1 = NULL;
2672
2673 if (stride > 4)
2674 vec1 = lp_build_gather_values(gallivm, out+4, stride - 4);
2675
2676 /* Get the buffer. */
2677 rw_buffers = LLVMGetParam(ctx->radeon_bld.main_fn,
2678 SI_PARAM_RW_BUFFERS);
2679 buffer = build_indexed_load_const(ctx, rw_buffers,
2680 lp_build_const_int32(gallivm, SI_HS_RING_TESS_FACTOR));
2681
2682 /* Get the offset. */
2683 tf_base = LLVMGetParam(ctx->radeon_bld.main_fn,
2684 SI_PARAM_TESS_FACTOR_OFFSET);
2685 byteoffset = LLVMBuildMul(gallivm->builder, rel_patch_id,
2686 lp_build_const_int32(gallivm, 4 * stride), "");
2687
2688 lp_build_if(&inner_if_ctx, gallivm,
2689 LLVMBuildICmp(gallivm->builder, LLVMIntEQ,
2690 rel_patch_id, bld_base->uint_bld.zero, ""));
2691
2692 /* Store the dynamic HS control word. */
2693 build_tbuffer_store_dwords(ctx, buffer,
2694 lp_build_const_int32(gallivm, 0x80000000),
2695 1, lp_build_const_int32(gallivm, 0), tf_base, 0);
2696
2697 lp_build_endif(&inner_if_ctx);
2698
2699 /* Store the tessellation factors. */
2700 build_tbuffer_store_dwords(ctx, buffer, vec0,
2701 MIN2(stride, 4), byteoffset, tf_base, 4);
2702 if (vec1)
2703 build_tbuffer_store_dwords(ctx, buffer, vec1,
2704 stride - 4, byteoffset, tf_base, 20);
2705 lp_build_endif(&if_ctx);
2706 }
2707
2708 /* This only writes the tessellation factor levels. */
2709 static void si_llvm_emit_tcs_epilogue(struct lp_build_tgsi_context *bld_base)
2710 {
2711 struct si_shader_context *ctx = si_shader_context(bld_base);
2712 LLVMValueRef rel_patch_id, invocation_id, tf_lds_offset;
2713
2714 rel_patch_id = get_rel_patch_id(ctx);
2715 invocation_id = unpack_param(ctx, SI_PARAM_REL_IDS, 8, 5);
2716 tf_lds_offset = get_tcs_out_current_patch_data_offset(ctx);
2717
2718 if (!ctx->is_monolithic) {
2719 /* Return epilog parameters from this function. */
2720 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2721 LLVMValueRef ret = ctx->return_value;
2722 LLVMValueRef rw_buffers, rw0, rw1, tf_soffset;
2723 unsigned vgpr;
2724
2725 /* RW_BUFFERS pointer */
2726 rw_buffers = LLVMGetParam(ctx->radeon_bld.main_fn,
2727 SI_PARAM_RW_BUFFERS);
2728 rw_buffers = LLVMBuildPtrToInt(builder, rw_buffers, ctx->i64, "");
2729 rw_buffers = LLVMBuildBitCast(builder, rw_buffers, ctx->v2i32, "");
2730 rw0 = LLVMBuildExtractElement(builder, rw_buffers,
2731 bld_base->uint_bld.zero, "");
2732 rw1 = LLVMBuildExtractElement(builder, rw_buffers,
2733 bld_base->uint_bld.one, "");
2734 ret = LLVMBuildInsertValue(builder, ret, rw0, 0, "");
2735 ret = LLVMBuildInsertValue(builder, ret, rw1, 1, "");
2736
2737 /* Tess factor buffer soffset is after user SGPRs. */
2738 tf_soffset = LLVMGetParam(ctx->radeon_bld.main_fn,
2739 SI_PARAM_TESS_FACTOR_OFFSET);
2740 ret = LLVMBuildInsertValue(builder, ret, tf_soffset,
2741 SI_TCS_NUM_USER_SGPR + 1, "");
2742
2743 /* VGPRs */
2744 rel_patch_id = bitcast(bld_base, TGSI_TYPE_FLOAT, rel_patch_id);
2745 invocation_id = bitcast(bld_base, TGSI_TYPE_FLOAT, invocation_id);
2746 tf_lds_offset = bitcast(bld_base, TGSI_TYPE_FLOAT, tf_lds_offset);
2747
2748 vgpr = SI_TCS_NUM_USER_SGPR + 2;
2749 ret = LLVMBuildInsertValue(builder, ret, rel_patch_id, vgpr++, "");
2750 ret = LLVMBuildInsertValue(builder, ret, invocation_id, vgpr++, "");
2751 ret = LLVMBuildInsertValue(builder, ret, tf_lds_offset, vgpr++, "");
2752 ctx->return_value = ret;
2753 return;
2754 }
2755
2756 si_copy_tcs_inputs(bld_base);
2757 si_write_tess_factors(bld_base, rel_patch_id, invocation_id, tf_lds_offset);
2758 }
2759
2760 static void si_llvm_emit_ls_epilogue(struct lp_build_tgsi_context *bld_base)
2761 {
2762 struct si_shader_context *ctx = si_shader_context(bld_base);
2763 struct si_shader *shader = ctx->shader;
2764 struct tgsi_shader_info *info = &shader->selector->info;
2765 struct gallivm_state *gallivm = bld_base->base.gallivm;
2766 unsigned i, chan;
2767 LLVMValueRef vertex_id = LLVMGetParam(ctx->radeon_bld.main_fn,
2768 ctx->param_rel_auto_id);
2769 LLVMValueRef vertex_dw_stride =
2770 unpack_param(ctx, SI_PARAM_LS_OUT_LAYOUT, 13, 8);
2771 LLVMValueRef base_dw_addr = LLVMBuildMul(gallivm->builder, vertex_id,
2772 vertex_dw_stride, "");
2773
2774 /* Write outputs to LDS. The next shader (TCS aka HS) will read
2775 * its inputs from it. */
2776 for (i = 0; i < info->num_outputs; i++) {
2777 LLVMValueRef *out_ptr = ctx->radeon_bld.soa.outputs[i];
2778 unsigned name = info->output_semantic_name[i];
2779 unsigned index = info->output_semantic_index[i];
2780 int param = si_shader_io_get_unique_index(name, index);
2781 LLVMValueRef dw_addr = LLVMBuildAdd(gallivm->builder, base_dw_addr,
2782 lp_build_const_int32(gallivm, param * 4), "");
2783
2784 for (chan = 0; chan < 4; chan++) {
2785 lds_store(bld_base, chan, dw_addr,
2786 LLVMBuildLoad(gallivm->builder, out_ptr[chan], ""));
2787 }
2788 }
2789 }
2790
2791 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context *bld_base)
2792 {
2793 struct si_shader_context *ctx = si_shader_context(bld_base);
2794 struct gallivm_state *gallivm = bld_base->base.gallivm;
2795 struct si_shader *es = ctx->shader;
2796 struct tgsi_shader_info *info = &es->selector->info;
2797 LLVMValueRef soffset = LLVMGetParam(ctx->radeon_bld.main_fn,
2798 ctx->param_es2gs_offset);
2799 unsigned chan;
2800 int i;
2801
2802 for (i = 0; i < info->num_outputs; i++) {
2803 LLVMValueRef *out_ptr =
2804 ctx->radeon_bld.soa.outputs[i];
2805 int param_index;
2806
2807 if (info->output_semantic_name[i] == TGSI_SEMANTIC_VIEWPORT_INDEX ||
2808 info->output_semantic_name[i] == TGSI_SEMANTIC_LAYER)
2809 continue;
2810
2811 param_index = si_shader_io_get_unique_index(info->output_semantic_name[i],
2812 info->output_semantic_index[i]);
2813
2814 for (chan = 0; chan < 4; chan++) {
2815 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
2816 out_val = LLVMBuildBitCast(gallivm->builder, out_val, ctx->i32, "");
2817
2818 build_tbuffer_store(ctx,
2819 ctx->esgs_ring,
2820 out_val, 1,
2821 LLVMGetUndef(ctx->i32), soffset,
2822 (4 * param_index + chan) * 4,
2823 V_008F0C_BUF_DATA_FORMAT_32,
2824 V_008F0C_BUF_NUM_FORMAT_UINT,
2825 0, 0, 1, 1, 0);
2826 }
2827 }
2828 }
2829
2830 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
2831 {
2832 struct si_shader_context *ctx = si_shader_context(bld_base);
2833 struct gallivm_state *gallivm = bld_base->base.gallivm;
2834 LLVMValueRef args[2];
2835
2836 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
2837 args[1] = LLVMGetParam(ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2838 lp_build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2839 ctx->voidt, args, 2, 0);
2840 }
2841
2842 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context *bld_base)
2843 {
2844 struct si_shader_context *ctx = si_shader_context(bld_base);
2845 struct gallivm_state *gallivm = bld_base->base.gallivm;
2846 struct tgsi_shader_info *info = &ctx->shader->selector->info;
2847 struct si_shader_output_values *outputs = NULL;
2848 int i,j;
2849
2850 assert(!ctx->is_gs_copy_shader);
2851
2852 outputs = MALLOC((info->num_outputs + 1) * sizeof(outputs[0]));
2853
2854 /* Vertex color clamping.
2855 *
2856 * This uses a state constant loaded in a user data SGPR and
2857 * an IF statement is added that clamps all colors if the constant
2858 * is true.
2859 */
2860 if (ctx->type == PIPE_SHADER_VERTEX) {
2861 struct lp_build_if_state if_ctx;
2862 LLVMValueRef cond = NULL;
2863 LLVMValueRef addr, val;
2864
2865 for (i = 0; i < info->num_outputs; i++) {
2866 if (info->output_semantic_name[i] != TGSI_SEMANTIC_COLOR &&
2867 info->output_semantic_name[i] != TGSI_SEMANTIC_BCOLOR)
2868 continue;
2869
2870 /* We've found a color. */
2871 if (!cond) {
2872 /* The state is in the first bit of the user SGPR. */
2873 cond = LLVMGetParam(ctx->radeon_bld.main_fn,
2874 SI_PARAM_VS_STATE_BITS);
2875 cond = LLVMBuildTrunc(gallivm->builder, cond,
2876 ctx->i1, "");
2877 lp_build_if(&if_ctx, gallivm, cond);
2878 }
2879
2880 for (j = 0; j < 4; j++) {
2881 addr = ctx->radeon_bld.soa.outputs[i][j];
2882 val = LLVMBuildLoad(gallivm->builder, addr, "");
2883 val = radeon_llvm_saturate(bld_base, val);
2884 LLVMBuildStore(gallivm->builder, val, addr);
2885 }
2886 }
2887
2888 if (cond)
2889 lp_build_endif(&if_ctx);
2890 }
2891
2892 for (i = 0; i < info->num_outputs; i++) {
2893 outputs[i].name = info->output_semantic_name[i];
2894 outputs[i].sid = info->output_semantic_index[i];
2895
2896 for (j = 0; j < 4; j++)
2897 outputs[i].values[j] =
2898 LLVMBuildLoad(gallivm->builder,
2899 ctx->radeon_bld.soa.outputs[i][j],
2900 "");
2901 }
2902
2903 if (ctx->is_monolithic) {
2904 /* Export PrimitiveID when PS needs it. */
2905 if (si_vs_exports_prim_id(ctx->shader)) {
2906 outputs[i].name = TGSI_SEMANTIC_PRIMID;
2907 outputs[i].sid = 0;
2908 outputs[i].values[0] = bitcast(bld_base, TGSI_TYPE_FLOAT,
2909 get_primitive_id(bld_base, 0));
2910 outputs[i].values[1] = bld_base->base.undef;
2911 outputs[i].values[2] = bld_base->base.undef;
2912 outputs[i].values[3] = bld_base->base.undef;
2913 i++;
2914 }
2915 } else {
2916 /* Return the primitive ID from the LLVM function. */
2917 ctx->return_value =
2918 LLVMBuildInsertValue(gallivm->builder,
2919 ctx->return_value,
2920 bitcast(bld_base, TGSI_TYPE_FLOAT,
2921 get_primitive_id(bld_base, 0)),
2922 VS_EPILOG_PRIMID_LOC, "");
2923 }
2924
2925 si_llvm_export_vs(bld_base, outputs, i);
2926 FREE(outputs);
2927 }
2928
2929 struct si_ps_exports {
2930 unsigned num;
2931 LLVMValueRef args[10][9];
2932 };
2933
2934 unsigned si_get_spi_shader_z_format(bool writes_z, bool writes_stencil,
2935 bool writes_samplemask)
2936 {
2937 if (writes_z) {
2938 /* Z needs 32 bits. */
2939 if (writes_samplemask)
2940 return V_028710_SPI_SHADER_32_ABGR;
2941 else if (writes_stencil)
2942 return V_028710_SPI_SHADER_32_GR;
2943 else
2944 return V_028710_SPI_SHADER_32_R;
2945 } else if (writes_stencil || writes_samplemask) {
2946 /* Both stencil and sample mask need only 16 bits. */
2947 return V_028710_SPI_SHADER_UINT16_ABGR;
2948 } else {
2949 return V_028710_SPI_SHADER_ZERO;
2950 }
2951 }
2952
2953 static void si_export_mrt_z(struct lp_build_tgsi_context *bld_base,
2954 LLVMValueRef depth, LLVMValueRef stencil,
2955 LLVMValueRef samplemask, struct si_ps_exports *exp)
2956 {
2957 struct si_shader_context *ctx = si_shader_context(bld_base);
2958 struct lp_build_context *base = &bld_base->base;
2959 struct lp_build_context *uint = &bld_base->uint_bld;
2960 LLVMValueRef args[9];
2961 unsigned mask = 0;
2962 unsigned format = si_get_spi_shader_z_format(depth != NULL,
2963 stencil != NULL,
2964 samplemask != NULL);
2965
2966 assert(depth || stencil || samplemask);
2967
2968 args[1] = uint->one; /* whether the EXEC mask is valid */
2969 args[2] = uint->one; /* DONE bit */
2970
2971 /* Specify the target we are exporting */
2972 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
2973
2974 args[4] = uint->zero; /* COMP flag */
2975 args[5] = base->undef; /* R, depth */
2976 args[6] = base->undef; /* G, stencil test value[0:7], stencil op value[8:15] */
2977 args[7] = base->undef; /* B, sample mask */
2978 args[8] = base->undef; /* A, alpha to mask */
2979
2980 if (format == V_028710_SPI_SHADER_UINT16_ABGR) {
2981 assert(!depth);
2982 args[4] = uint->one; /* COMPR flag */
2983
2984 if (stencil) {
2985 /* Stencil should be in X[23:16]. */
2986 stencil = bitcast(bld_base, TGSI_TYPE_UNSIGNED, stencil);
2987 stencil = LLVMBuildShl(base->gallivm->builder, stencil,
2988 LLVMConstInt(ctx->i32, 16, 0), "");
2989 args[5] = bitcast(bld_base, TGSI_TYPE_FLOAT, stencil);
2990 mask |= 0x3;
2991 }
2992 if (samplemask) {
2993 /* SampleMask should be in Y[15:0]. */
2994 args[6] = samplemask;
2995 mask |= 0xc;
2996 }
2997 } else {
2998 if (depth) {
2999 args[5] = depth;
3000 mask |= 0x1;
3001 }
3002 if (stencil) {
3003 args[6] = stencil;
3004 mask |= 0x2;
3005 }
3006 if (samplemask) {
3007 args[7] = samplemask;
3008 mask |= 0x4;
3009 }
3010 }
3011
3012 /* SI (except OLAND) has a bug that it only looks
3013 * at the X writemask component. */
3014 if (ctx->screen->b.chip_class == SI &&
3015 ctx->screen->b.family != CHIP_OLAND)
3016 mask |= 0x1;
3017
3018 /* Specify which components to enable */
3019 args[0] = lp_build_const_int32(base->gallivm, mask);
3020
3021 memcpy(exp->args[exp->num++], args, sizeof(args));
3022 }
3023
3024 static void si_export_mrt_color(struct lp_build_tgsi_context *bld_base,
3025 LLVMValueRef *color, unsigned index,
3026 unsigned samplemask_param,
3027 bool is_last, struct si_ps_exports *exp)
3028 {
3029 struct si_shader_context *ctx = si_shader_context(bld_base);
3030 struct lp_build_context *base = &bld_base->base;
3031 int i;
3032
3033 /* Clamp color */
3034 if (ctx->shader->key.ps.epilog.clamp_color)
3035 for (i = 0; i < 4; i++)
3036 color[i] = radeon_llvm_saturate(bld_base, color[i]);
3037
3038 /* Alpha to one */
3039 if (ctx->shader->key.ps.epilog.alpha_to_one)
3040 color[3] = base->one;
3041
3042 /* Alpha test */
3043 if (index == 0 &&
3044 ctx->shader->key.ps.epilog.alpha_func != PIPE_FUNC_ALWAYS)
3045 si_alpha_test(bld_base, color[3]);
3046
3047 /* Line & polygon smoothing */
3048 if (ctx->shader->key.ps.epilog.poly_line_smoothing)
3049 color[3] = si_scale_alpha_by_sample_mask(bld_base, color[3],
3050 samplemask_param);
3051
3052 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
3053 if (ctx->shader->key.ps.epilog.last_cbuf > 0) {
3054 LLVMValueRef args[8][9];
3055 int c, last = -1;
3056
3057 /* Get the export arguments, also find out what the last one is. */
3058 for (c = 0; c <= ctx->shader->key.ps.epilog.last_cbuf; c++) {
3059 si_llvm_init_export_args(bld_base, color,
3060 V_008DFC_SQ_EXP_MRT + c, args[c]);
3061 if (args[c][0] != bld_base->uint_bld.zero)
3062 last = c;
3063 }
3064
3065 /* Emit all exports. */
3066 for (c = 0; c <= ctx->shader->key.ps.epilog.last_cbuf; c++) {
3067 if (is_last && last == c) {
3068 args[c][1] = bld_base->uint_bld.one; /* whether the EXEC mask is valid */
3069 args[c][2] = bld_base->uint_bld.one; /* DONE bit */
3070 } else if (args[c][0] == bld_base->uint_bld.zero)
3071 continue; /* unnecessary NULL export */
3072
3073 memcpy(exp->args[exp->num++], args[c], sizeof(args[c]));
3074 }
3075 } else {
3076 LLVMValueRef args[9];
3077
3078 /* Export */
3079 si_llvm_init_export_args(bld_base, color, V_008DFC_SQ_EXP_MRT + index,
3080 args);
3081 if (is_last) {
3082 args[1] = bld_base->uint_bld.one; /* whether the EXEC mask is valid */
3083 args[2] = bld_base->uint_bld.one; /* DONE bit */
3084 } else if (args[0] == bld_base->uint_bld.zero)
3085 return; /* unnecessary NULL export */
3086
3087 memcpy(exp->args[exp->num++], args, sizeof(args));
3088 }
3089 }
3090
3091 static void si_emit_ps_exports(struct si_shader_context *ctx,
3092 struct si_ps_exports *exp)
3093 {
3094 for (unsigned i = 0; i < exp->num; i++)
3095 lp_build_intrinsic(ctx->radeon_bld.gallivm.builder,
3096 "llvm.SI.export", ctx->voidt,
3097 exp->args[i], 9, 0);
3098 }
3099
3100 static void si_export_null(struct lp_build_tgsi_context *bld_base)
3101 {
3102 struct si_shader_context *ctx = si_shader_context(bld_base);
3103 struct lp_build_context *base = &bld_base->base;
3104 struct lp_build_context *uint = &bld_base->uint_bld;
3105 LLVMValueRef args[9];
3106
3107 args[0] = lp_build_const_int32(base->gallivm, 0x0); /* enabled channels */
3108 args[1] = uint->one; /* whether the EXEC mask is valid */
3109 args[2] = uint->one; /* DONE bit */
3110 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_NULL);
3111 args[4] = uint->zero; /* COMPR flag (0 = 32-bit export) */
3112 args[5] = base->undef; /* R */
3113 args[6] = base->undef; /* G */
3114 args[7] = base->undef; /* B */
3115 args[8] = base->undef; /* A */
3116
3117 lp_build_intrinsic(base->gallivm->builder, "llvm.SI.export",
3118 ctx->voidt, args, 9, 0);
3119 }
3120
3121 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context *bld_base)
3122 {
3123 struct si_shader_context *ctx = si_shader_context(bld_base);
3124 struct si_shader *shader = ctx->shader;
3125 struct lp_build_context *base = &bld_base->base;
3126 struct tgsi_shader_info *info = &shader->selector->info;
3127 LLVMBuilderRef builder = base->gallivm->builder;
3128 LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
3129 int last_color_export = -1;
3130 int i;
3131 struct si_ps_exports exp = {};
3132
3133 /* Determine the last export. If MRTZ is present, it's always last.
3134 * Otherwise, find the last color export.
3135 */
3136 if (!info->writes_z && !info->writes_stencil && !info->writes_samplemask) {
3137 unsigned spi_format = shader->key.ps.epilog.spi_shader_col_format;
3138
3139 /* Don't export NULL and return if alpha-test is enabled. */
3140 if (shader->key.ps.epilog.alpha_func != PIPE_FUNC_ALWAYS &&
3141 shader->key.ps.epilog.alpha_func != PIPE_FUNC_NEVER &&
3142 (spi_format & 0xf) == 0)
3143 spi_format |= V_028714_SPI_SHADER_32_AR;
3144
3145 for (i = 0; i < info->num_outputs; i++) {
3146 unsigned index = info->output_semantic_index[i];
3147
3148 if (info->output_semantic_name[i] != TGSI_SEMANTIC_COLOR)
3149 continue;
3150
3151 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
3152 if (shader->key.ps.epilog.last_cbuf > 0) {
3153 /* Just set this if any of the colorbuffers are enabled. */
3154 if (spi_format &
3155 ((1llu << (4 * (shader->key.ps.epilog.last_cbuf + 1))) - 1))
3156 last_color_export = i;
3157 continue;
3158 }
3159
3160 if ((spi_format >> (index * 4)) & 0xf)
3161 last_color_export = i;
3162 }
3163
3164 /* If there are no outputs, export NULL. */
3165 if (last_color_export == -1) {
3166 si_export_null(bld_base);
3167 return;
3168 }
3169 }
3170
3171 for (i = 0; i < info->num_outputs; i++) {
3172 unsigned semantic_name = info->output_semantic_name[i];
3173 unsigned semantic_index = info->output_semantic_index[i];
3174 unsigned j;
3175 LLVMValueRef color[4] = {};
3176
3177 /* Select the correct target */
3178 switch (semantic_name) {
3179 case TGSI_SEMANTIC_POSITION:
3180 depth = LLVMBuildLoad(builder,
3181 ctx->radeon_bld.soa.outputs[i][2], "");
3182 break;
3183 case TGSI_SEMANTIC_STENCIL:
3184 stencil = LLVMBuildLoad(builder,
3185 ctx->radeon_bld.soa.outputs[i][1], "");
3186 break;
3187 case TGSI_SEMANTIC_SAMPLEMASK:
3188 samplemask = LLVMBuildLoad(builder,
3189 ctx->radeon_bld.soa.outputs[i][0], "");
3190 break;
3191 case TGSI_SEMANTIC_COLOR:
3192 for (j = 0; j < 4; j++)
3193 color[j] = LLVMBuildLoad(builder,
3194 ctx->radeon_bld.soa.outputs[i][j], "");
3195
3196 si_export_mrt_color(bld_base, color, semantic_index,
3197 SI_PARAM_SAMPLE_COVERAGE,
3198 last_color_export == i, &exp);
3199 break;
3200 default:
3201 fprintf(stderr,
3202 "Warning: SI unhandled fs output type:%d\n",
3203 semantic_name);
3204 }
3205 }
3206
3207 if (depth || stencil || samplemask)
3208 si_export_mrt_z(bld_base, depth, stencil, samplemask, &exp);
3209
3210 si_emit_ps_exports(ctx, &exp);
3211 }
3212
3213 /**
3214 * Return PS outputs in this order:
3215 *
3216 * v[0:3] = color0.xyzw
3217 * v[4:7] = color1.xyzw
3218 * ...
3219 * vN+0 = Depth
3220 * vN+1 = Stencil
3221 * vN+2 = SampleMask
3222 * vN+3 = SampleMaskIn (used for OpenGL smoothing)
3223 *
3224 * The alpha-ref SGPR is returned via its original location.
3225 */
3226 static void si_llvm_return_fs_outputs(struct lp_build_tgsi_context *bld_base)
3227 {
3228 struct si_shader_context *ctx = si_shader_context(bld_base);
3229 struct si_shader *shader = ctx->shader;
3230 struct lp_build_context *base = &bld_base->base;
3231 struct tgsi_shader_info *info = &shader->selector->info;
3232 LLVMBuilderRef builder = base->gallivm->builder;
3233 unsigned i, j, first_vgpr, vgpr;
3234
3235 LLVMValueRef color[8][4] = {};
3236 LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
3237 LLVMValueRef ret;
3238
3239 /* Read the output values. */
3240 for (i = 0; i < info->num_outputs; i++) {
3241 unsigned semantic_name = info->output_semantic_name[i];
3242 unsigned semantic_index = info->output_semantic_index[i];
3243
3244 switch (semantic_name) {
3245 case TGSI_SEMANTIC_COLOR:
3246 assert(semantic_index < 8);
3247 for (j = 0; j < 4; j++) {
3248 LLVMValueRef ptr = ctx->radeon_bld.soa.outputs[i][j];
3249 LLVMValueRef result = LLVMBuildLoad(builder, ptr, "");
3250 color[semantic_index][j] = result;
3251 }
3252 break;
3253 case TGSI_SEMANTIC_POSITION:
3254 depth = LLVMBuildLoad(builder,
3255 ctx->radeon_bld.soa.outputs[i][2], "");
3256 break;
3257 case TGSI_SEMANTIC_STENCIL:
3258 stencil = LLVMBuildLoad(builder,
3259 ctx->radeon_bld.soa.outputs[i][1], "");
3260 break;
3261 case TGSI_SEMANTIC_SAMPLEMASK:
3262 samplemask = LLVMBuildLoad(builder,
3263 ctx->radeon_bld.soa.outputs[i][0], "");
3264 break;
3265 default:
3266 fprintf(stderr, "Warning: SI unhandled fs output type:%d\n",
3267 semantic_name);
3268 }
3269 }
3270
3271 /* Fill the return structure. */
3272 ret = ctx->return_value;
3273
3274 /* Set SGPRs. */
3275 ret = LLVMBuildInsertValue(builder, ret,
3276 bitcast(bld_base, TGSI_TYPE_SIGNED,
3277 LLVMGetParam(ctx->radeon_bld.main_fn,
3278 SI_PARAM_ALPHA_REF)),
3279 SI_SGPR_ALPHA_REF, "");
3280
3281 /* Set VGPRs */
3282 first_vgpr = vgpr = SI_SGPR_ALPHA_REF + 1;
3283 for (i = 0; i < ARRAY_SIZE(color); i++) {
3284 if (!color[i][0])
3285 continue;
3286
3287 for (j = 0; j < 4; j++)
3288 ret = LLVMBuildInsertValue(builder, ret, color[i][j], vgpr++, "");
3289 }
3290 if (depth)
3291 ret = LLVMBuildInsertValue(builder, ret, depth, vgpr++, "");
3292 if (stencil)
3293 ret = LLVMBuildInsertValue(builder, ret, stencil, vgpr++, "");
3294 if (samplemask)
3295 ret = LLVMBuildInsertValue(builder, ret, samplemask, vgpr++, "");
3296
3297 /* Add the input sample mask for smoothing at the end. */
3298 if (vgpr < first_vgpr + PS_EPILOG_SAMPLEMASK_MIN_LOC)
3299 vgpr = first_vgpr + PS_EPILOG_SAMPLEMASK_MIN_LOC;
3300 ret = LLVMBuildInsertValue(builder, ret,
3301 LLVMGetParam(ctx->radeon_bld.main_fn,
3302 SI_PARAM_SAMPLE_COVERAGE), vgpr++, "");
3303
3304 ctx->return_value = ret;
3305 }
3306
3307 /**
3308 * Given a v8i32 resource descriptor for a buffer, extract the size of the
3309 * buffer in number of elements and return it as an i32.
3310 */
3311 static LLVMValueRef get_buffer_size(
3312 struct lp_build_tgsi_context *bld_base,
3313 LLVMValueRef descriptor)
3314 {
3315 struct si_shader_context *ctx = si_shader_context(bld_base);
3316 struct gallivm_state *gallivm = bld_base->base.gallivm;
3317 LLVMBuilderRef builder = gallivm->builder;
3318 LLVMValueRef size =
3319 LLVMBuildExtractElement(builder, descriptor,
3320 lp_build_const_int32(gallivm, 6), "");
3321
3322 if (ctx->screen->b.chip_class >= VI) {
3323 /* On VI, the descriptor contains the size in bytes,
3324 * but TXQ must return the size in elements.
3325 * The stride is always non-zero for resources using TXQ.
3326 */
3327 LLVMValueRef stride =
3328 LLVMBuildExtractElement(builder, descriptor,
3329 lp_build_const_int32(gallivm, 5), "");
3330 stride = LLVMBuildLShr(builder, stride,
3331 lp_build_const_int32(gallivm, 16), "");
3332 stride = LLVMBuildAnd(builder, stride,
3333 lp_build_const_int32(gallivm, 0x3FFF), "");
3334
3335 size = LLVMBuildUDiv(builder, size, stride, "");
3336 }
3337
3338 return size;
3339 }
3340
3341 /**
3342 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
3343 * intrinsic names).
3344 */
3345 static void build_int_type_name(
3346 LLVMTypeRef type,
3347 char *buf, unsigned bufsize)
3348 {
3349 assert(bufsize >= 6);
3350
3351 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
3352 snprintf(buf, bufsize, "v%ui32",
3353 LLVMGetVectorSize(type));
3354 else
3355 strcpy(buf, "i32");
3356 }
3357
3358 static void build_tex_intrinsic(const struct lp_build_tgsi_action *action,
3359 struct lp_build_tgsi_context *bld_base,
3360 struct lp_build_emit_data *emit_data);
3361
3362 /* Prevent optimizations (at least of memory accesses) across the current
3363 * point in the program by emitting empty inline assembly that is marked as
3364 * having side effects.
3365 */
3366 static void emit_optimization_barrier(struct si_shader_context *ctx)
3367 {
3368 LLVMBuilderRef builder = ctx->radeon_bld.gallivm.builder;
3369 LLVMTypeRef ftype = LLVMFunctionType(ctx->voidt, NULL, 0, false);
3370 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, "", "", true, false);
3371 LLVMBuildCall(builder, inlineasm, NULL, 0, "");
3372 }
3373
3374 static void emit_waitcnt(struct si_shader_context *ctx)
3375 {
3376 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
3377 LLVMBuilderRef builder = gallivm->builder;
3378 LLVMValueRef args[1] = {
3379 lp_build_const_int32(gallivm, 0xf70)
3380 };
3381 lp_build_intrinsic(builder, "llvm.amdgcn.s.waitcnt",
3382 ctx->voidt, args, 1, 0);
3383 }
3384
3385 static void membar_emit(
3386 const struct lp_build_tgsi_action *action,
3387 struct lp_build_tgsi_context *bld_base,
3388 struct lp_build_emit_data *emit_data)
3389 {
3390 struct si_shader_context *ctx = si_shader_context(bld_base);
3391
3392 emit_waitcnt(ctx);
3393 }
3394
3395 static LLVMValueRef
3396 shader_buffer_fetch_rsrc(struct si_shader_context *ctx,
3397 const struct tgsi_full_src_register *reg)
3398 {
3399 LLVMValueRef index;
3400 LLVMValueRef rsrc_ptr = LLVMGetParam(ctx->radeon_bld.main_fn,
3401 SI_PARAM_SHADER_BUFFERS);
3402
3403 if (!reg->Register.Indirect)
3404 index = LLVMConstInt(ctx->i32, reg->Register.Index, 0);
3405 else
3406 index = get_bounded_indirect_index(ctx, &reg->Indirect,
3407 reg->Register.Index,
3408 SI_NUM_SHADER_BUFFERS);
3409
3410 return build_indexed_load_const(ctx, rsrc_ptr, index);
3411 }
3412
3413 static bool tgsi_is_array_sampler(unsigned target)
3414 {
3415 return target == TGSI_TEXTURE_1D_ARRAY ||
3416 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
3417 target == TGSI_TEXTURE_2D_ARRAY ||
3418 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
3419 target == TGSI_TEXTURE_CUBE_ARRAY ||
3420 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
3421 target == TGSI_TEXTURE_2D_ARRAY_MSAA;
3422 }
3423
3424 static bool tgsi_is_array_image(unsigned target)
3425 {
3426 return target == TGSI_TEXTURE_3D ||
3427 target == TGSI_TEXTURE_CUBE ||
3428 target == TGSI_TEXTURE_1D_ARRAY ||
3429 target == TGSI_TEXTURE_2D_ARRAY ||
3430 target == TGSI_TEXTURE_CUBE_ARRAY ||
3431 target == TGSI_TEXTURE_2D_ARRAY_MSAA;
3432 }
3433
3434 /**
3435 * Given a 256-bit resource descriptor, force the DCC enable bit to off.
3436 *
3437 * At least on Tonga, executing image stores on images with DCC enabled and
3438 * non-trivial can eventually lead to lockups. This can occur when an
3439 * application binds an image as read-only but then uses a shader that writes
3440 * to it. The OpenGL spec allows almost arbitrarily bad behavior (including
3441 * program termination) in this case, but it doesn't cost much to be a bit
3442 * nicer: disabling DCC in the shader still leads to undefined results but
3443 * avoids the lockup.
3444 */
3445 static LLVMValueRef force_dcc_off(struct si_shader_context *ctx,
3446 LLVMValueRef rsrc)
3447 {
3448 if (ctx->screen->b.chip_class <= CIK) {
3449 return rsrc;
3450 } else {
3451 LLVMBuilderRef builder = ctx->radeon_bld.gallivm.builder;
3452 LLVMValueRef i32_6 = LLVMConstInt(ctx->i32, 6, 0);
3453 LLVMValueRef i32_C = LLVMConstInt(ctx->i32, C_008F28_COMPRESSION_EN, 0);
3454 LLVMValueRef tmp;
3455
3456 tmp = LLVMBuildExtractElement(builder, rsrc, i32_6, "");
3457 tmp = LLVMBuildAnd(builder, tmp, i32_C, "");
3458 return LLVMBuildInsertElement(builder, rsrc, tmp, i32_6, "");
3459 }
3460 }
3461
3462 /**
3463 * Load the resource descriptor for \p image.
3464 */
3465 static void
3466 image_fetch_rsrc(
3467 struct lp_build_tgsi_context *bld_base,
3468 const struct tgsi_full_src_register *image,
3469 bool dcc_off,
3470 LLVMValueRef *rsrc)
3471 {
3472 struct si_shader_context *ctx = si_shader_context(bld_base);
3473 LLVMValueRef rsrc_ptr = LLVMGetParam(ctx->radeon_bld.main_fn,
3474 SI_PARAM_IMAGES);
3475 LLVMValueRef index, tmp;
3476
3477 assert(image->Register.File == TGSI_FILE_IMAGE);
3478
3479 if (!image->Register.Indirect) {
3480 const struct tgsi_shader_info *info = bld_base->info;
3481
3482 index = LLVMConstInt(ctx->i32, image->Register.Index, 0);
3483
3484 if (info->images_writemask & (1 << image->Register.Index) &&
3485 !(info->images_buffers & (1 << image->Register.Index)))
3486 dcc_off = true;
3487 } else {
3488 /* From the GL_ARB_shader_image_load_store extension spec:
3489 *
3490 * If a shader performs an image load, store, or atomic
3491 * operation using an image variable declared as an array,
3492 * and if the index used to select an individual element is
3493 * negative or greater than or equal to the size of the
3494 * array, the results of the operation are undefined but may
3495 * not lead to termination.
3496 */
3497 index = get_bounded_indirect_index(ctx, &image->Indirect,
3498 image->Register.Index,
3499 SI_NUM_IMAGES);
3500 }
3501
3502 tmp = build_indexed_load_const(ctx, rsrc_ptr, index);
3503 if (dcc_off)
3504 tmp = force_dcc_off(ctx, tmp);
3505 *rsrc = tmp;
3506 }
3507
3508 static LLVMValueRef image_fetch_coords(
3509 struct lp_build_tgsi_context *bld_base,
3510 const struct tgsi_full_instruction *inst,
3511 unsigned src)
3512 {
3513 struct gallivm_state *gallivm = bld_base->base.gallivm;
3514 LLVMBuilderRef builder = gallivm->builder;
3515 unsigned target = inst->Memory.Texture;
3516 unsigned num_coords = tgsi_util_get_texture_coord_dim(target);
3517 LLVMValueRef coords[4];
3518 LLVMValueRef tmp;
3519 int chan;
3520
3521 for (chan = 0; chan < num_coords; ++chan) {
3522 tmp = lp_build_emit_fetch(bld_base, inst, src, chan);
3523 tmp = LLVMBuildBitCast(builder, tmp, bld_base->uint_bld.elem_type, "");
3524 coords[chan] = tmp;
3525 }
3526
3527 if (num_coords == 1)
3528 return coords[0];
3529
3530 if (num_coords == 3) {
3531 /* LLVM has difficulties lowering 3-element vectors. */
3532 coords[3] = bld_base->uint_bld.undef;
3533 num_coords = 4;
3534 }
3535
3536 return lp_build_gather_values(gallivm, coords, num_coords);
3537 }
3538
3539 /**
3540 * Append the extra mode bits that are used by image load and store.
3541 */
3542 static void image_append_args(
3543 struct si_shader_context *ctx,
3544 struct lp_build_emit_data * emit_data,
3545 unsigned target,
3546 bool atomic)
3547 {
3548 const struct tgsi_full_instruction *inst = emit_data->inst;
3549 LLVMValueRef i1false = LLVMConstInt(ctx->i1, 0, 0);
3550 LLVMValueRef i1true = LLVMConstInt(ctx->i1, 1, 0);
3551
3552 emit_data->args[emit_data->arg_count++] = i1false; /* r128 */
3553 emit_data->args[emit_data->arg_count++] =
3554 tgsi_is_array_image(target) ? i1true : i1false; /* da */
3555 if (!atomic) {
3556 emit_data->args[emit_data->arg_count++] =
3557 inst->Memory.Qualifier & (TGSI_MEMORY_COHERENT | TGSI_MEMORY_VOLATILE) ?
3558 i1true : i1false; /* glc */
3559 }
3560 emit_data->args[emit_data->arg_count++] = i1false; /* slc */
3561 }
3562
3563 /**
3564 * Given a 256 bit resource, extract the top half (which stores the buffer
3565 * resource in the case of textures and images).
3566 */
3567 static LLVMValueRef extract_rsrc_top_half(
3568 struct si_shader_context *ctx,
3569 LLVMValueRef rsrc)
3570 {
3571 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
3572 struct lp_build_tgsi_context *bld_base = &ctx->radeon_bld.soa.bld_base;
3573 LLVMTypeRef v2i128 = LLVMVectorType(ctx->i128, 2);
3574
3575 rsrc = LLVMBuildBitCast(gallivm->builder, rsrc, v2i128, "");
3576 rsrc = LLVMBuildExtractElement(gallivm->builder, rsrc, bld_base->uint_bld.one, "");
3577 rsrc = LLVMBuildBitCast(gallivm->builder, rsrc, ctx->v4i32, "");
3578
3579 return rsrc;
3580 }
3581
3582 /**
3583 * Append the resource and indexing arguments for buffer intrinsics.
3584 *
3585 * \param rsrc the v4i32 buffer resource
3586 * \param index index into the buffer (stride-based)
3587 * \param offset byte offset into the buffer
3588 */
3589 static void buffer_append_args(
3590 struct si_shader_context *ctx,
3591 struct lp_build_emit_data *emit_data,
3592 LLVMValueRef rsrc,
3593 LLVMValueRef index,
3594 LLVMValueRef offset,
3595 bool atomic)
3596 {
3597 const struct tgsi_full_instruction *inst = emit_data->inst;
3598 LLVMValueRef i1false = LLVMConstInt(ctx->i1, 0, 0);
3599 LLVMValueRef i1true = LLVMConstInt(ctx->i1, 1, 0);
3600
3601 emit_data->args[emit_data->arg_count++] = rsrc;
3602 emit_data->args[emit_data->arg_count++] = index; /* vindex */
3603 emit_data->args[emit_data->arg_count++] = offset; /* voffset */
3604 if (!atomic) {
3605 emit_data->args[emit_data->arg_count++] =
3606 inst->Memory.Qualifier & (TGSI_MEMORY_COHERENT | TGSI_MEMORY_VOLATILE) ?
3607 i1true : i1false; /* glc */
3608 }
3609 emit_data->args[emit_data->arg_count++] = i1false; /* slc */
3610 }
3611
3612 static void load_fetch_args(
3613 struct lp_build_tgsi_context * bld_base,
3614 struct lp_build_emit_data * emit_data)
3615 {
3616 struct si_shader_context *ctx = si_shader_context(bld_base);
3617 struct gallivm_state *gallivm = bld_base->base.gallivm;
3618 const struct tgsi_full_instruction * inst = emit_data->inst;
3619 unsigned target = inst->Memory.Texture;
3620 LLVMValueRef rsrc;
3621
3622 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
3623
3624 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER) {
3625 LLVMBuilderRef builder = gallivm->builder;
3626 LLVMValueRef offset;
3627 LLVMValueRef tmp;
3628
3629 rsrc = shader_buffer_fetch_rsrc(ctx, &inst->Src[0]);
3630
3631 tmp = lp_build_emit_fetch(bld_base, inst, 1, 0);
3632 offset = LLVMBuildBitCast(builder, tmp, bld_base->uint_bld.elem_type, "");
3633
3634 buffer_append_args(ctx, emit_data, rsrc, bld_base->uint_bld.zero,
3635 offset, false);
3636 } else if (inst->Src[0].Register.File == TGSI_FILE_IMAGE) {
3637 LLVMValueRef coords;
3638
3639 image_fetch_rsrc(bld_base, &inst->Src[0], false, &rsrc);
3640 coords = image_fetch_coords(bld_base, inst, 1);
3641
3642 if (target == TGSI_TEXTURE_BUFFER) {
3643 rsrc = extract_rsrc_top_half(ctx, rsrc);
3644 buffer_append_args(ctx, emit_data, rsrc, coords,
3645 bld_base->uint_bld.zero, false);
3646 } else {
3647 emit_data->args[0] = coords;
3648 emit_data->args[1] = rsrc;
3649 emit_data->args[2] = lp_build_const_int32(gallivm, 15); /* dmask */
3650 emit_data->arg_count = 3;
3651
3652 image_append_args(ctx, emit_data, target, false);
3653 }
3654 }
3655 }
3656
3657 static void load_emit_buffer(struct si_shader_context *ctx,
3658 struct lp_build_emit_data *emit_data)
3659 {
3660 const struct tgsi_full_instruction *inst = emit_data->inst;
3661 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
3662 LLVMBuilderRef builder = gallivm->builder;
3663 uint writemask = inst->Dst[0].Register.WriteMask;
3664 uint count = util_last_bit(writemask);
3665 const char *intrinsic_name;
3666 LLVMTypeRef dst_type;
3667
3668 switch (count) {
3669 case 1:
3670 intrinsic_name = "llvm.amdgcn.buffer.load.f32";
3671 dst_type = ctx->f32;
3672 break;
3673 case 2:
3674 intrinsic_name = "llvm.amdgcn.buffer.load.v2f32";
3675 dst_type = LLVMVectorType(ctx->f32, 2);
3676 break;
3677 default: // 3 & 4
3678 intrinsic_name = "llvm.amdgcn.buffer.load.v4f32";
3679 dst_type = ctx->v4f32;
3680 count = 4;
3681 }
3682
3683 emit_data->output[emit_data->chan] = lp_build_intrinsic(
3684 builder, intrinsic_name, dst_type,
3685 emit_data->args, emit_data->arg_count,
3686 LLVMReadOnlyAttribute);
3687 }
3688
3689 static LLVMValueRef get_memory_ptr(struct si_shader_context *ctx,
3690 const struct tgsi_full_instruction *inst,
3691 LLVMTypeRef type, int arg)
3692 {
3693 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
3694 LLVMBuilderRef builder = gallivm->builder;
3695 LLVMValueRef offset, ptr;
3696 int addr_space;
3697
3698 offset = lp_build_emit_fetch(&ctx->radeon_bld.soa.bld_base, inst, arg, 0);
3699 offset = LLVMBuildBitCast(builder, offset, ctx->i32, "");
3700
3701 ptr = ctx->shared_memory;
3702 ptr = LLVMBuildGEP(builder, ptr, &offset, 1, "");
3703 addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
3704 ptr = LLVMBuildBitCast(builder, ptr, LLVMPointerType(type, addr_space), "");
3705
3706 return ptr;
3707 }
3708
3709 static void load_emit_memory(
3710 struct si_shader_context *ctx,
3711 struct lp_build_emit_data *emit_data)
3712 {
3713 const struct tgsi_full_instruction *inst = emit_data->inst;
3714 struct lp_build_context *base = &ctx->radeon_bld.soa.bld_base.base;
3715 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
3716 LLVMBuilderRef builder = gallivm->builder;
3717 unsigned writemask = inst->Dst[0].Register.WriteMask;
3718 LLVMValueRef channels[4], ptr, derived_ptr, index;
3719 int chan;
3720
3721 ptr = get_memory_ptr(ctx, inst, base->elem_type, 1);
3722
3723 for (chan = 0; chan < 4; ++chan) {
3724 if (!(writemask & (1 << chan))) {
3725 channels[chan] = LLVMGetUndef(base->elem_type);
3726 continue;
3727 }
3728
3729 index = lp_build_const_int32(gallivm, chan);
3730 derived_ptr = LLVMBuildGEP(builder, ptr, &index, 1, "");
3731 channels[chan] = LLVMBuildLoad(builder, derived_ptr, "");
3732 }
3733 emit_data->output[emit_data->chan] = lp_build_gather_values(gallivm, channels, 4);
3734 }
3735
3736 static void load_emit(
3737 const struct lp_build_tgsi_action *action,
3738 struct lp_build_tgsi_context *bld_base,
3739 struct lp_build_emit_data *emit_data)
3740 {
3741 struct si_shader_context *ctx = si_shader_context(bld_base);
3742 struct gallivm_state *gallivm = bld_base->base.gallivm;
3743 LLVMBuilderRef builder = gallivm->builder;
3744 const struct tgsi_full_instruction * inst = emit_data->inst;
3745 char intrinsic_name[32];
3746 char coords_type[8];
3747
3748 if (inst->Src[0].Register.File == TGSI_FILE_MEMORY) {
3749 load_emit_memory(ctx, emit_data);
3750 return;
3751 }
3752
3753 if (inst->Memory.Qualifier & TGSI_MEMORY_VOLATILE)
3754 emit_waitcnt(ctx);
3755
3756 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER) {
3757 load_emit_buffer(ctx, emit_data);
3758 return;
3759 }
3760
3761 if (inst->Memory.Texture == TGSI_TEXTURE_BUFFER) {
3762 emit_data->output[emit_data->chan] =
3763 lp_build_intrinsic(
3764 builder, "llvm.amdgcn.buffer.load.format.v4f32", emit_data->dst_type,
3765 emit_data->args, emit_data->arg_count,
3766 LLVMReadOnlyAttribute);
3767 } else {
3768 build_int_type_name(LLVMTypeOf(emit_data->args[0]),
3769 coords_type, sizeof(coords_type));
3770
3771 snprintf(intrinsic_name, sizeof(intrinsic_name),
3772 "llvm.amdgcn.image.load.%s", coords_type);
3773
3774 emit_data->output[emit_data->chan] =
3775 lp_build_intrinsic(
3776 builder, intrinsic_name, emit_data->dst_type,
3777 emit_data->args, emit_data->arg_count,
3778 LLVMReadOnlyAttribute);
3779 }
3780 }
3781
3782 static void store_fetch_args(
3783 struct lp_build_tgsi_context * bld_base,
3784 struct lp_build_emit_data * emit_data)
3785 {
3786 struct si_shader_context *ctx = si_shader_context(bld_base);
3787 struct gallivm_state *gallivm = bld_base->base.gallivm;
3788 LLVMBuilderRef builder = gallivm->builder;
3789 const struct tgsi_full_instruction * inst = emit_data->inst;
3790 struct tgsi_full_src_register memory;
3791 LLVMValueRef chans[4];
3792 LLVMValueRef data;
3793 LLVMValueRef rsrc;
3794 unsigned chan;
3795
3796 emit_data->dst_type = LLVMVoidTypeInContext(gallivm->context);
3797
3798 for (chan = 0; chan < 4; ++chan) {
3799 chans[chan] = lp_build_emit_fetch(bld_base, inst, 1, chan);
3800 }
3801 data = lp_build_gather_values(gallivm, chans, 4);
3802
3803 emit_data->args[emit_data->arg_count++] = data;
3804
3805 memory = tgsi_full_src_register_from_dst(&inst->Dst[0]);
3806
3807 if (inst->Dst[0].Register.File == TGSI_FILE_BUFFER) {
3808 LLVMValueRef offset;
3809 LLVMValueRef tmp;
3810
3811 rsrc = shader_buffer_fetch_rsrc(ctx, &memory);
3812
3813 tmp = lp_build_emit_fetch(bld_base, inst, 0, 0);
3814 offset = LLVMBuildBitCast(builder, tmp, bld_base->uint_bld.elem_type, "");
3815
3816 buffer_append_args(ctx, emit_data, rsrc, bld_base->uint_bld.zero,
3817 offset, false);
3818 } else if (inst->Dst[0].Register.File == TGSI_FILE_IMAGE) {
3819 unsigned target = inst->Memory.Texture;
3820 LLVMValueRef coords;
3821
3822 coords = image_fetch_coords(bld_base, inst, 0);
3823
3824 if (target == TGSI_TEXTURE_BUFFER) {
3825 image_fetch_rsrc(bld_base, &memory, false, &rsrc);
3826
3827 rsrc = extract_rsrc_top_half(ctx, rsrc);
3828 buffer_append_args(ctx, emit_data, rsrc, coords,
3829 bld_base->uint_bld.zero, false);
3830 } else {
3831 emit_data->args[1] = coords;
3832 image_fetch_rsrc(bld_base, &memory, true, &emit_data->args[2]);
3833 emit_data->args[3] = lp_build_const_int32(gallivm, 15); /* dmask */
3834 emit_data->arg_count = 4;
3835
3836 image_append_args(ctx, emit_data, target, false);
3837 }
3838 }
3839 }
3840
3841 static void store_emit_buffer(
3842 struct si_shader_context *ctx,
3843 struct lp_build_emit_data *emit_data)
3844 {
3845 const struct tgsi_full_instruction *inst = emit_data->inst;
3846 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
3847 LLVMBuilderRef builder = gallivm->builder;
3848 struct lp_build_context *uint_bld = &ctx->radeon_bld.soa.bld_base.uint_bld;
3849 LLVMValueRef base_data = emit_data->args[0];
3850 LLVMValueRef base_offset = emit_data->args[3];
3851 unsigned writemask = inst->Dst[0].Register.WriteMask;
3852
3853 while (writemask) {
3854 int start, count;
3855 const char *intrinsic_name;
3856 LLVMValueRef data;
3857 LLVMValueRef offset;
3858 LLVMValueRef tmp;
3859
3860 u_bit_scan_consecutive_range(&writemask, &start, &count);
3861
3862 /* Due to an LLVM limitation, split 3-element writes
3863 * into a 2-element and a 1-element write. */
3864 if (count == 3) {
3865 writemask |= 1 << (start + 2);
3866 count = 2;
3867 }
3868
3869 if (count == 4) {
3870 data = base_data;
3871 intrinsic_name = "llvm.amdgcn.buffer.store.v4f32";
3872 } else if (count == 2) {
3873 LLVMTypeRef v2f32 = LLVMVectorType(ctx->f32, 2);
3874
3875 tmp = LLVMBuildExtractElement(
3876 builder, base_data,
3877 lp_build_const_int32(gallivm, start), "");
3878 data = LLVMBuildInsertElement(
3879 builder, LLVMGetUndef(v2f32), tmp,
3880 uint_bld->zero, "");
3881
3882 tmp = LLVMBuildExtractElement(
3883 builder, base_data,
3884 lp_build_const_int32(gallivm, start + 1), "");
3885 data = LLVMBuildInsertElement(
3886 builder, data, tmp, uint_bld->one, "");
3887
3888 intrinsic_name = "llvm.amdgcn.buffer.store.v2f32";
3889 } else {
3890 assert(count == 1);
3891 data = LLVMBuildExtractElement(
3892 builder, base_data,
3893 lp_build_const_int32(gallivm, start), "");
3894 intrinsic_name = "llvm.amdgcn.buffer.store.f32";
3895 }
3896
3897 offset = base_offset;
3898 if (start != 0) {
3899 offset = LLVMBuildAdd(
3900 builder, offset,
3901 lp_build_const_int32(gallivm, start * 4), "");
3902 }
3903
3904 emit_data->args[0] = data;
3905 emit_data->args[3] = offset;
3906
3907 lp_build_intrinsic(
3908 builder, intrinsic_name, emit_data->dst_type,
3909 emit_data->args, emit_data->arg_count, 0);
3910 }
3911 }
3912
3913 static void store_emit_memory(
3914 struct si_shader_context *ctx,
3915 struct lp_build_emit_data *emit_data)
3916 {
3917 const struct tgsi_full_instruction *inst = emit_data->inst;
3918 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
3919 struct lp_build_context *base = &ctx->radeon_bld.soa.bld_base.base;
3920 LLVMBuilderRef builder = gallivm->builder;
3921 unsigned writemask = inst->Dst[0].Register.WriteMask;
3922 LLVMValueRef ptr, derived_ptr, data, index;
3923 int chan;
3924
3925 ptr = get_memory_ptr(ctx, inst, base->elem_type, 0);
3926
3927 for (chan = 0; chan < 4; ++chan) {
3928 if (!(writemask & (1 << chan))) {
3929 continue;
3930 }
3931 data = lp_build_emit_fetch(&ctx->radeon_bld.soa.bld_base, inst, 1, chan);
3932 index = lp_build_const_int32(gallivm, chan);
3933 derived_ptr = LLVMBuildGEP(builder, ptr, &index, 1, "");
3934 LLVMBuildStore(builder, data, derived_ptr);
3935 }
3936 }
3937
3938 static void store_emit(
3939 const struct lp_build_tgsi_action *action,
3940 struct lp_build_tgsi_context *bld_base,
3941 struct lp_build_emit_data *emit_data)
3942 {
3943 struct si_shader_context *ctx = si_shader_context(bld_base);
3944 struct gallivm_state *gallivm = bld_base->base.gallivm;
3945 LLVMBuilderRef builder = gallivm->builder;
3946 const struct tgsi_full_instruction * inst = emit_data->inst;
3947 unsigned target = inst->Memory.Texture;
3948 char intrinsic_name[32];
3949 char coords_type[8];
3950
3951 if (inst->Dst[0].Register.File == TGSI_FILE_MEMORY) {
3952 store_emit_memory(ctx, emit_data);
3953 return;
3954 }
3955
3956 if (inst->Memory.Qualifier & TGSI_MEMORY_VOLATILE)
3957 emit_waitcnt(ctx);
3958
3959 if (inst->Dst[0].Register.File == TGSI_FILE_BUFFER) {
3960 store_emit_buffer(ctx, emit_data);
3961 return;
3962 }
3963
3964 if (target == TGSI_TEXTURE_BUFFER) {
3965 emit_data->output[emit_data->chan] = lp_build_intrinsic(
3966 builder, "llvm.amdgcn.buffer.store.format.v4f32",
3967 emit_data->dst_type, emit_data->args,
3968 emit_data->arg_count, 0);
3969 } else {
3970 build_int_type_name(LLVMTypeOf(emit_data->args[1]),
3971 coords_type, sizeof(coords_type));
3972 snprintf(intrinsic_name, sizeof(intrinsic_name),
3973 "llvm.amdgcn.image.store.%s", coords_type);
3974
3975 emit_data->output[emit_data->chan] =
3976 lp_build_intrinsic(
3977 builder, intrinsic_name, emit_data->dst_type,
3978 emit_data->args, emit_data->arg_count, 0);
3979 }
3980 }
3981
3982 static void atomic_fetch_args(
3983 struct lp_build_tgsi_context * bld_base,
3984 struct lp_build_emit_data * emit_data)
3985 {
3986 struct si_shader_context *ctx = si_shader_context(bld_base);
3987 struct gallivm_state *gallivm = bld_base->base.gallivm;
3988 LLVMBuilderRef builder = gallivm->builder;
3989 const struct tgsi_full_instruction * inst = emit_data->inst;
3990 LLVMValueRef data1, data2;
3991 LLVMValueRef rsrc;
3992 LLVMValueRef tmp;
3993
3994 emit_data->dst_type = bld_base->base.elem_type;
3995
3996 tmp = lp_build_emit_fetch(bld_base, inst, 2, 0);
3997 data1 = LLVMBuildBitCast(builder, tmp, bld_base->uint_bld.elem_type, "");
3998
3999 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
4000 tmp = lp_build_emit_fetch(bld_base, inst, 3, 0);
4001 data2 = LLVMBuildBitCast(builder, tmp, bld_base->uint_bld.elem_type, "");
4002 }
4003
4004 /* llvm.amdgcn.image/buffer.atomic.cmpswap reflect the hardware order
4005 * of arguments, which is reversed relative to TGSI (and GLSL)
4006 */
4007 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS)
4008 emit_data->args[emit_data->arg_count++] = data2;
4009 emit_data->args[emit_data->arg_count++] = data1;
4010
4011 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER) {
4012 LLVMValueRef offset;
4013
4014 rsrc = shader_buffer_fetch_rsrc(ctx, &inst->Src[0]);
4015
4016 tmp = lp_build_emit_fetch(bld_base, inst, 1, 0);
4017 offset = LLVMBuildBitCast(builder, tmp, bld_base->uint_bld.elem_type, "");
4018
4019 buffer_append_args(ctx, emit_data, rsrc, bld_base->uint_bld.zero,
4020 offset, true);
4021 } else if (inst->Src[0].Register.File == TGSI_FILE_IMAGE) {
4022 unsigned target = inst->Memory.Texture;
4023 LLVMValueRef coords;
4024
4025 image_fetch_rsrc(bld_base, &inst->Src[0],
4026 target != TGSI_TEXTURE_BUFFER, &rsrc);
4027 coords = image_fetch_coords(bld_base, inst, 1);
4028
4029 if (target == TGSI_TEXTURE_BUFFER) {
4030 rsrc = extract_rsrc_top_half(ctx, rsrc);
4031 buffer_append_args(ctx, emit_data, rsrc, coords,
4032 bld_base->uint_bld.zero, true);
4033 } else {
4034 emit_data->args[emit_data->arg_count++] = coords;
4035 emit_data->args[emit_data->arg_count++] = rsrc;
4036
4037 image_append_args(ctx, emit_data, target, true);
4038 }
4039 }
4040 }
4041
4042 static void atomic_emit_memory(struct si_shader_context *ctx,
4043 struct lp_build_emit_data *emit_data) {
4044 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
4045 LLVMBuilderRef builder = gallivm->builder;
4046 const struct tgsi_full_instruction * inst = emit_data->inst;
4047 LLVMValueRef ptr, result, arg;
4048
4049 ptr = get_memory_ptr(ctx, inst, ctx->i32, 1);
4050
4051 arg = lp_build_emit_fetch(&ctx->radeon_bld.soa.bld_base, inst, 2, 0);
4052 arg = LLVMBuildBitCast(builder, arg, ctx->i32, "");
4053
4054 if (inst->Instruction.Opcode == TGSI_OPCODE_ATOMCAS) {
4055 LLVMValueRef new_data;
4056 new_data = lp_build_emit_fetch(&ctx->radeon_bld.soa.bld_base,
4057 inst, 3, 0);
4058
4059 new_data = LLVMBuildBitCast(builder, new_data, ctx->i32, "");
4060
4061 #if HAVE_LLVM >= 0x309
4062 result = LLVMBuildAtomicCmpXchg(builder, ptr, arg, new_data,
4063 LLVMAtomicOrderingSequentiallyConsistent,
4064 LLVMAtomicOrderingSequentiallyConsistent,
4065 false);
4066 #endif
4067
4068 result = LLVMBuildExtractValue(builder, result, 0, "");
4069 } else {
4070 LLVMAtomicRMWBinOp op;
4071
4072 switch(inst->Instruction.Opcode) {
4073 case TGSI_OPCODE_ATOMUADD:
4074 op = LLVMAtomicRMWBinOpAdd;
4075 break;
4076 case TGSI_OPCODE_ATOMXCHG:
4077 op = LLVMAtomicRMWBinOpXchg;
4078 break;
4079 case TGSI_OPCODE_ATOMAND:
4080 op = LLVMAtomicRMWBinOpAnd;
4081 break;
4082 case TGSI_OPCODE_ATOMOR:
4083 op = LLVMAtomicRMWBinOpOr;
4084 break;
4085 case TGSI_OPCODE_ATOMXOR:
4086 op = LLVMAtomicRMWBinOpXor;
4087 break;
4088 case TGSI_OPCODE_ATOMUMIN:
4089 op = LLVMAtomicRMWBinOpUMin;
4090 break;
4091 case TGSI_OPCODE_ATOMUMAX:
4092 op = LLVMAtomicRMWBinOpUMax;
4093 break;
4094 case TGSI_OPCODE_ATOMIMIN:
4095 op = LLVMAtomicRMWBinOpMin;
4096 break;
4097 case TGSI_OPCODE_ATOMIMAX:
4098 op = LLVMAtomicRMWBinOpMax;
4099 break;
4100 default:
4101 unreachable("unknown atomic opcode");
4102 }
4103
4104 result = LLVMBuildAtomicRMW(builder, op, ptr, arg,
4105 LLVMAtomicOrderingSequentiallyConsistent,
4106 false);
4107 }
4108 emit_data->output[emit_data->chan] = LLVMBuildBitCast(builder, result, emit_data->dst_type, "");
4109 }
4110
4111 static void atomic_emit(
4112 const struct lp_build_tgsi_action *action,
4113 struct lp_build_tgsi_context *bld_base,
4114 struct lp_build_emit_data *emit_data)
4115 {
4116 struct si_shader_context *ctx = si_shader_context(bld_base);
4117 struct gallivm_state *gallivm = bld_base->base.gallivm;
4118 LLVMBuilderRef builder = gallivm->builder;
4119 const struct tgsi_full_instruction * inst = emit_data->inst;
4120 char intrinsic_name[40];
4121 LLVMValueRef tmp;
4122
4123 if (inst->Src[0].Register.File == TGSI_FILE_MEMORY) {
4124 atomic_emit_memory(ctx, emit_data);
4125 return;
4126 }
4127
4128 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER ||
4129 inst->Memory.Texture == TGSI_TEXTURE_BUFFER) {
4130 snprintf(intrinsic_name, sizeof(intrinsic_name),
4131 "llvm.amdgcn.buffer.atomic.%s", action->intr_name);
4132 } else {
4133 char coords_type[8];
4134
4135 build_int_type_name(LLVMTypeOf(emit_data->args[1]),
4136 coords_type, sizeof(coords_type));
4137 snprintf(intrinsic_name, sizeof(intrinsic_name),
4138 "llvm.amdgcn.image.atomic.%s.%s",
4139 action->intr_name, coords_type);
4140 }
4141
4142 tmp = lp_build_intrinsic(
4143 builder, intrinsic_name, bld_base->uint_bld.elem_type,
4144 emit_data->args, emit_data->arg_count, 0);
4145 emit_data->output[emit_data->chan] =
4146 LLVMBuildBitCast(builder, tmp, bld_base->base.elem_type, "");
4147 }
4148
4149 static void resq_fetch_args(
4150 struct lp_build_tgsi_context * bld_base,
4151 struct lp_build_emit_data * emit_data)
4152 {
4153 struct si_shader_context *ctx = si_shader_context(bld_base);
4154 struct gallivm_state *gallivm = bld_base->base.gallivm;
4155 const struct tgsi_full_instruction *inst = emit_data->inst;
4156 const struct tgsi_full_src_register *reg = &inst->Src[0];
4157
4158 emit_data->dst_type = ctx->v4i32;
4159
4160 if (reg->Register.File == TGSI_FILE_BUFFER) {
4161 emit_data->args[0] = shader_buffer_fetch_rsrc(ctx, reg);
4162 emit_data->arg_count = 1;
4163 } else if (inst->Memory.Texture == TGSI_TEXTURE_BUFFER) {
4164 image_fetch_rsrc(bld_base, reg, false, &emit_data->args[0]);
4165 emit_data->arg_count = 1;
4166 } else {
4167 emit_data->args[0] = bld_base->uint_bld.zero; /* mip level */
4168 image_fetch_rsrc(bld_base, reg, false, &emit_data->args[1]);
4169 emit_data->args[2] = lp_build_const_int32(gallivm, 15); /* dmask */
4170 emit_data->args[3] = bld_base->uint_bld.zero; /* unorm */
4171 emit_data->args[4] = bld_base->uint_bld.zero; /* r128 */
4172 emit_data->args[5] = tgsi_is_array_image(inst->Memory.Texture) ?
4173 bld_base->uint_bld.one : bld_base->uint_bld.zero; /* da */
4174 emit_data->args[6] = bld_base->uint_bld.zero; /* glc */
4175 emit_data->args[7] = bld_base->uint_bld.zero; /* slc */
4176 emit_data->args[8] = bld_base->uint_bld.zero; /* tfe */
4177 emit_data->args[9] = bld_base->uint_bld.zero; /* lwe */
4178 emit_data->arg_count = 10;
4179 }
4180 }
4181
4182 static void resq_emit(
4183 const struct lp_build_tgsi_action *action,
4184 struct lp_build_tgsi_context *bld_base,
4185 struct lp_build_emit_data *emit_data)
4186 {
4187 struct gallivm_state *gallivm = bld_base->base.gallivm;
4188 LLVMBuilderRef builder = gallivm->builder;
4189 const struct tgsi_full_instruction *inst = emit_data->inst;
4190 LLVMValueRef out;
4191
4192 if (inst->Src[0].Register.File == TGSI_FILE_BUFFER) {
4193 out = LLVMBuildExtractElement(builder, emit_data->args[0],
4194 lp_build_const_int32(gallivm, 2), "");
4195 } else if (inst->Memory.Texture == TGSI_TEXTURE_BUFFER) {
4196 out = get_buffer_size(bld_base, emit_data->args[0]);
4197 } else {
4198 out = lp_build_intrinsic(
4199 builder, "llvm.SI.getresinfo.i32", emit_data->dst_type,
4200 emit_data->args, emit_data->arg_count,
4201 LLVMReadNoneAttribute);
4202
4203 /* Divide the number of layers by 6 to get the number of cubes. */
4204 if (inst->Memory.Texture == TGSI_TEXTURE_CUBE_ARRAY) {
4205 LLVMValueRef imm2 = lp_build_const_int32(gallivm, 2);
4206 LLVMValueRef imm6 = lp_build_const_int32(gallivm, 6);
4207
4208 LLVMValueRef z = LLVMBuildExtractElement(builder, out, imm2, "");
4209 z = LLVMBuildSDiv(builder, z, imm6, "");
4210 out = LLVMBuildInsertElement(builder, out, z, imm2, "");
4211 }
4212 }
4213
4214 emit_data->output[emit_data->chan] = out;
4215 }
4216
4217 static void set_tex_fetch_args(struct si_shader_context *ctx,
4218 struct lp_build_emit_data *emit_data,
4219 unsigned opcode, unsigned target,
4220 LLVMValueRef res_ptr, LLVMValueRef samp_ptr,
4221 LLVMValueRef *param, unsigned count,
4222 unsigned dmask)
4223 {
4224 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
4225 unsigned num_args;
4226 unsigned is_rect = target == TGSI_TEXTURE_RECT;
4227
4228 /* Pad to power of two vector */
4229 while (count < util_next_power_of_two(count))
4230 param[count++] = LLVMGetUndef(ctx->i32);
4231
4232 /* Texture coordinates. */
4233 if (count > 1)
4234 emit_data->args[0] = lp_build_gather_values(gallivm, param, count);
4235 else
4236 emit_data->args[0] = param[0];
4237
4238 /* Resource. */
4239 emit_data->args[1] = res_ptr;
4240 num_args = 2;
4241
4242 if (opcode == TGSI_OPCODE_TXF || opcode == TGSI_OPCODE_TXQ)
4243 emit_data->dst_type = ctx->v4i32;
4244 else {
4245 emit_data->dst_type = ctx->v4f32;
4246
4247 emit_data->args[num_args++] = samp_ptr;
4248 }
4249
4250 emit_data->args[num_args++] = lp_build_const_int32(gallivm, dmask);
4251 emit_data->args[num_args++] = lp_build_const_int32(gallivm, is_rect); /* unorm */
4252 emit_data->args[num_args++] = lp_build_const_int32(gallivm, 0); /* r128 */
4253 emit_data->args[num_args++] = lp_build_const_int32(gallivm,
4254 tgsi_is_array_sampler(target)); /* da */
4255 emit_data->args[num_args++] = lp_build_const_int32(gallivm, 0); /* glc */
4256 emit_data->args[num_args++] = lp_build_const_int32(gallivm, 0); /* slc */
4257 emit_data->args[num_args++] = lp_build_const_int32(gallivm, 0); /* tfe */
4258 emit_data->args[num_args++] = lp_build_const_int32(gallivm, 0); /* lwe */
4259
4260 emit_data->arg_count = num_args;
4261 }
4262
4263 static const struct lp_build_tgsi_action tex_action;
4264
4265 enum desc_type {
4266 DESC_IMAGE,
4267 DESC_FMASK,
4268 DESC_SAMPLER
4269 };
4270
4271 static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
4272 {
4273 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
4274 CONST_ADDR_SPACE);
4275 }
4276
4277 /**
4278 * Load an image view, fmask view. or sampler state descriptor.
4279 */
4280 static LLVMValueRef load_sampler_desc_custom(struct si_shader_context *ctx,
4281 LLVMValueRef list, LLVMValueRef index,
4282 enum desc_type type)
4283 {
4284 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
4285 LLVMBuilderRef builder = gallivm->builder;
4286
4287 switch (type) {
4288 case DESC_IMAGE:
4289 /* The image is at [0:7]. */
4290 index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, 2, 0), "");
4291 break;
4292 case DESC_FMASK:
4293 /* The FMASK is at [8:15]. */
4294 index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, 2, 0), "");
4295 index = LLVMBuildAdd(builder, index, LLVMConstInt(ctx->i32, 1, 0), "");
4296 break;
4297 case DESC_SAMPLER:
4298 /* The sampler state is at [12:15]. */
4299 index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, 4, 0), "");
4300 index = LLVMBuildAdd(builder, index, LLVMConstInt(ctx->i32, 3, 0), "");
4301 list = LLVMBuildPointerCast(builder, list,
4302 const_array(ctx->v4i32, 0), "");
4303 break;
4304 }
4305
4306 return build_indexed_load_const(ctx, list, index);
4307 }
4308
4309 static LLVMValueRef load_sampler_desc(struct si_shader_context *ctx,
4310 LLVMValueRef index, enum desc_type type)
4311 {
4312 LLVMValueRef list = LLVMGetParam(ctx->radeon_bld.main_fn,
4313 SI_PARAM_SAMPLERS);
4314
4315 return load_sampler_desc_custom(ctx, list, index, type);
4316 }
4317
4318 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
4319 *
4320 * SI-CI:
4321 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
4322 * filtering manually. The driver sets img7 to a mask clearing
4323 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
4324 * s_and_b32 samp0, samp0, img7
4325 *
4326 * VI:
4327 * The ANISO_OVERRIDE sampler field enables this fix in TA.
4328 */
4329 static LLVMValueRef sici_fix_sampler_aniso(struct si_shader_context *ctx,
4330 LLVMValueRef res, LLVMValueRef samp)
4331 {
4332 LLVMBuilderRef builder = ctx->radeon_bld.gallivm.builder;
4333 LLVMValueRef img7, samp0;
4334
4335 if (ctx->screen->b.chip_class >= VI)
4336 return samp;
4337
4338 img7 = LLVMBuildExtractElement(builder, res,
4339 LLVMConstInt(ctx->i32, 7, 0), "");
4340 samp0 = LLVMBuildExtractElement(builder, samp,
4341 LLVMConstInt(ctx->i32, 0, 0), "");
4342 samp0 = LLVMBuildAnd(builder, samp0, img7, "");
4343 return LLVMBuildInsertElement(builder, samp, samp0,
4344 LLVMConstInt(ctx->i32, 0, 0), "");
4345 }
4346
4347 static void tex_fetch_ptrs(
4348 struct lp_build_tgsi_context *bld_base,
4349 struct lp_build_emit_data *emit_data,
4350 LLVMValueRef *res_ptr, LLVMValueRef *samp_ptr, LLVMValueRef *fmask_ptr)
4351 {
4352 struct si_shader_context *ctx = si_shader_context(bld_base);
4353 const struct tgsi_full_instruction *inst = emit_data->inst;
4354 unsigned target = inst->Texture.Texture;
4355 unsigned sampler_src;
4356 unsigned sampler_index;
4357 LLVMValueRef index;
4358
4359 sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
4360 sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
4361
4362 if (emit_data->inst->Src[sampler_src].Register.Indirect) {
4363 const struct tgsi_full_src_register *reg = &emit_data->inst->Src[sampler_src];
4364
4365 index = get_bounded_indirect_index(ctx,
4366 &reg->Indirect,
4367 reg->Register.Index,
4368 SI_NUM_SAMPLERS);
4369 } else {
4370 index = LLVMConstInt(ctx->i32, sampler_index, 0);
4371 }
4372
4373 *res_ptr = load_sampler_desc(ctx, index, DESC_IMAGE);
4374
4375 if (target == TGSI_TEXTURE_2D_MSAA ||
4376 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
4377 if (samp_ptr)
4378 *samp_ptr = NULL;
4379 if (fmask_ptr)
4380 *fmask_ptr = load_sampler_desc(ctx, index, DESC_FMASK);
4381 } else {
4382 if (samp_ptr) {
4383 *samp_ptr = load_sampler_desc(ctx, index, DESC_SAMPLER);
4384 *samp_ptr = sici_fix_sampler_aniso(ctx, *res_ptr, *samp_ptr);
4385 }
4386 if (fmask_ptr)
4387 *fmask_ptr = NULL;
4388 }
4389 }
4390
4391 static void txq_fetch_args(
4392 struct lp_build_tgsi_context *bld_base,
4393 struct lp_build_emit_data *emit_data)
4394 {
4395 struct si_shader_context *ctx = si_shader_context(bld_base);
4396 struct gallivm_state *gallivm = bld_base->base.gallivm;
4397 LLVMBuilderRef builder = gallivm->builder;
4398 const struct tgsi_full_instruction *inst = emit_data->inst;
4399 unsigned target = inst->Texture.Texture;
4400 LLVMValueRef res_ptr;
4401 LLVMValueRef address;
4402
4403 tex_fetch_ptrs(bld_base, emit_data, &res_ptr, NULL, NULL);
4404
4405 if (target == TGSI_TEXTURE_BUFFER) {
4406 /* Read the size from the buffer descriptor directly. */
4407 LLVMValueRef res = LLVMBuildBitCast(builder, res_ptr, ctx->v8i32, "");
4408 emit_data->args[0] = get_buffer_size(bld_base, res);
4409 return;
4410 }
4411
4412 /* Textures - set the mip level. */
4413 address = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
4414
4415 set_tex_fetch_args(ctx, emit_data, TGSI_OPCODE_TXQ, target, res_ptr,
4416 NULL, &address, 1, 0xf);
4417 }
4418
4419 static void txq_emit(const struct lp_build_tgsi_action *action,
4420 struct lp_build_tgsi_context *bld_base,
4421 struct lp_build_emit_data *emit_data)
4422 {
4423 struct lp_build_context *base = &bld_base->base;
4424 unsigned target = emit_data->inst->Texture.Texture;
4425
4426 if (target == TGSI_TEXTURE_BUFFER) {
4427 /* Just return the buffer size. */
4428 emit_data->output[emit_data->chan] = emit_data->args[0];
4429 return;
4430 }
4431
4432 emit_data->output[emit_data->chan] = lp_build_intrinsic(
4433 base->gallivm->builder, "llvm.SI.getresinfo.i32",
4434 emit_data->dst_type, emit_data->args, emit_data->arg_count,
4435 LLVMReadNoneAttribute);
4436
4437 /* Divide the number of layers by 6 to get the number of cubes. */
4438 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
4439 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
4440 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
4441 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
4442 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
4443
4444 LLVMValueRef v4 = emit_data->output[emit_data->chan];
4445 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
4446 z = LLVMBuildSDiv(builder, z, six, "");
4447
4448 emit_data->output[emit_data->chan] =
4449 LLVMBuildInsertElement(builder, v4, z, two, "");
4450 }
4451 }
4452
4453 static void tex_fetch_args(
4454 struct lp_build_tgsi_context *bld_base,
4455 struct lp_build_emit_data *emit_data)
4456 {
4457 struct si_shader_context *ctx = si_shader_context(bld_base);
4458 struct gallivm_state *gallivm = bld_base->base.gallivm;
4459 const struct tgsi_full_instruction *inst = emit_data->inst;
4460 unsigned opcode = inst->Instruction.Opcode;
4461 unsigned target = inst->Texture.Texture;
4462 LLVMValueRef coords[5], derivs[6];
4463 LLVMValueRef address[16];
4464 unsigned num_coords = tgsi_util_get_texture_coord_dim(target);
4465 int ref_pos = tgsi_util_get_shadow_ref_src_index(target);
4466 unsigned count = 0;
4467 unsigned chan;
4468 unsigned num_deriv_channels = 0;
4469 bool has_offset = inst->Texture.NumOffsets > 0;
4470 LLVMValueRef res_ptr, samp_ptr, fmask_ptr = NULL;
4471 unsigned dmask = 0xf;
4472
4473 tex_fetch_ptrs(bld_base, emit_data, &res_ptr, &samp_ptr, &fmask_ptr);
4474
4475 if (target == TGSI_TEXTURE_BUFFER) {
4476 LLVMTypeRef v2i128 = LLVMVectorType(ctx->i128, 2);
4477
4478 /* Bitcast and truncate v8i32 to v16i8. */
4479 LLVMValueRef res = res_ptr;
4480 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
4481 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.one, "");
4482 res = LLVMBuildBitCast(gallivm->builder, res, ctx->v16i8, "");
4483
4484 emit_data->dst_type = ctx->v4f32;
4485 emit_data->args[0] = res;
4486 emit_data->args[1] = bld_base->uint_bld.zero;
4487 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X);
4488 emit_data->arg_count = 3;
4489 return;
4490 }
4491
4492 /* Fetch and project texture coordinates */
4493 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
4494 for (chan = 0; chan < 3; chan++ ) {
4495 coords[chan] = lp_build_emit_fetch(bld_base,
4496 emit_data->inst, 0,
4497 chan);
4498 if (opcode == TGSI_OPCODE_TXP)
4499 coords[chan] = lp_build_emit_llvm_binary(bld_base,
4500 TGSI_OPCODE_DIV,
4501 coords[chan],
4502 coords[3]);
4503 }
4504
4505 if (opcode == TGSI_OPCODE_TXP)
4506 coords[3] = bld_base->base.one;
4507
4508 /* Pack offsets. */
4509 if (has_offset && opcode != TGSI_OPCODE_TXF) {
4510 /* The offsets are six-bit signed integers packed like this:
4511 * X=[5:0], Y=[13:8], and Z=[21:16].
4512 */
4513 LLVMValueRef offset[3], pack;
4514
4515 assert(inst->Texture.NumOffsets == 1);
4516
4517 for (chan = 0; chan < 3; chan++) {
4518 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
4519 emit_data->inst, 0, chan);
4520 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
4521 lp_build_const_int32(gallivm, 0x3f), "");
4522 if (chan)
4523 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
4524 lp_build_const_int32(gallivm, chan*8), "");
4525 }
4526
4527 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
4528 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
4529 address[count++] = pack;
4530 }
4531
4532 /* Pack LOD bias value */
4533 if (opcode == TGSI_OPCODE_TXB)
4534 address[count++] = coords[3];
4535 if (opcode == TGSI_OPCODE_TXB2)
4536 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, TGSI_CHAN_X);
4537
4538 /* Pack depth comparison value */
4539 if (tgsi_is_shadow_target(target) && opcode != TGSI_OPCODE_LODQ) {
4540 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
4541 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, TGSI_CHAN_X);
4542 } else {
4543 assert(ref_pos >= 0);
4544 address[count++] = coords[ref_pos];
4545 }
4546 }
4547
4548 /* Pack user derivatives */
4549 if (opcode == TGSI_OPCODE_TXD) {
4550 int param, num_src_deriv_channels;
4551
4552 switch (target) {
4553 case TGSI_TEXTURE_3D:
4554 num_src_deriv_channels = 3;
4555 num_deriv_channels = 3;
4556 break;
4557 case TGSI_TEXTURE_2D:
4558 case TGSI_TEXTURE_SHADOW2D:
4559 case TGSI_TEXTURE_RECT:
4560 case TGSI_TEXTURE_SHADOWRECT:
4561 case TGSI_TEXTURE_2D_ARRAY:
4562 case TGSI_TEXTURE_SHADOW2D_ARRAY:
4563 num_src_deriv_channels = 2;
4564 num_deriv_channels = 2;
4565 break;
4566 case TGSI_TEXTURE_CUBE:
4567 case TGSI_TEXTURE_SHADOWCUBE:
4568 case TGSI_TEXTURE_CUBE_ARRAY:
4569 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
4570 /* Cube derivatives will be converted to 2D. */
4571 num_src_deriv_channels = 3;
4572 num_deriv_channels = 2;
4573 break;
4574 case TGSI_TEXTURE_1D:
4575 case TGSI_TEXTURE_SHADOW1D:
4576 case TGSI_TEXTURE_1D_ARRAY:
4577 case TGSI_TEXTURE_SHADOW1D_ARRAY:
4578 num_src_deriv_channels = 1;
4579 num_deriv_channels = 1;
4580 break;
4581 default:
4582 unreachable("invalid target");
4583 }
4584
4585 for (param = 0; param < 2; param++)
4586 for (chan = 0; chan < num_src_deriv_channels; chan++)
4587 derivs[param * num_src_deriv_channels + chan] =
4588 lp_build_emit_fetch(bld_base, inst, param+1, chan);
4589 }
4590
4591 if (target == TGSI_TEXTURE_CUBE ||
4592 target == TGSI_TEXTURE_CUBE_ARRAY ||
4593 target == TGSI_TEXTURE_SHADOWCUBE ||
4594 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
4595 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords, derivs);
4596
4597 if (opcode == TGSI_OPCODE_TXD)
4598 for (int i = 0; i < num_deriv_channels * 2; i++)
4599 address[count++] = derivs[i];
4600
4601 /* Pack texture coordinates */
4602 address[count++] = coords[0];
4603 if (num_coords > 1)
4604 address[count++] = coords[1];
4605 if (num_coords > 2)
4606 address[count++] = coords[2];
4607
4608 /* Pack LOD or sample index */
4609 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
4610 address[count++] = coords[3];
4611 else if (opcode == TGSI_OPCODE_TXL2)
4612 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, TGSI_CHAN_X);
4613
4614 if (count > 16) {
4615 assert(!"Cannot handle more than 16 texture address parameters");
4616 count = 16;
4617 }
4618
4619 for (chan = 0; chan < count; chan++ ) {
4620 address[chan] = LLVMBuildBitCast(gallivm->builder,
4621 address[chan], ctx->i32, "");
4622 }
4623
4624 /* Adjust the sample index according to FMASK.
4625 *
4626 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
4627 * which is the identity mapping. Each nibble says which physical sample
4628 * should be fetched to get that sample.
4629 *
4630 * For example, 0x11111100 means there are only 2 samples stored and
4631 * the second sample covers 3/4 of the pixel. When reading samples 0
4632 * and 1, return physical sample 0 (determined by the first two 0s
4633 * in FMASK), otherwise return physical sample 1.
4634 *
4635 * The sample index should be adjusted as follows:
4636 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
4637 */
4638 if (target == TGSI_TEXTURE_2D_MSAA ||
4639 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
4640 struct lp_build_context *uint_bld = &bld_base->uint_bld;
4641 struct lp_build_emit_data txf_emit_data = *emit_data;
4642 LLVMValueRef txf_address[4];
4643 unsigned txf_count = count;
4644 struct tgsi_full_instruction inst = {};
4645
4646 memcpy(txf_address, address, sizeof(txf_address));
4647
4648 if (target == TGSI_TEXTURE_2D_MSAA) {
4649 txf_address[2] = bld_base->uint_bld.zero;
4650 }
4651 txf_address[3] = bld_base->uint_bld.zero;
4652
4653 /* Read FMASK using TXF. */
4654 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
4655 inst.Texture.Texture = target;
4656 txf_emit_data.inst = &inst;
4657 txf_emit_data.chan = 0;
4658 set_tex_fetch_args(ctx, &txf_emit_data, TGSI_OPCODE_TXF,
4659 target, fmask_ptr, NULL,
4660 txf_address, txf_count, 0xf);
4661 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
4662
4663 /* Initialize some constants. */
4664 LLVMValueRef four = LLVMConstInt(ctx->i32, 4, 0);
4665 LLVMValueRef F = LLVMConstInt(ctx->i32, 0xF, 0);
4666
4667 /* Apply the formula. */
4668 LLVMValueRef fmask =
4669 LLVMBuildExtractElement(gallivm->builder,
4670 txf_emit_data.output[0],
4671 uint_bld->zero, "");
4672
4673 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
4674
4675 LLVMValueRef sample_index4 =
4676 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
4677
4678 LLVMValueRef shifted_fmask =
4679 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
4680
4681 LLVMValueRef final_sample =
4682 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
4683
4684 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
4685 * resource descriptor is 0 (invalid),
4686 */
4687 LLVMValueRef fmask_desc =
4688 LLVMBuildBitCast(gallivm->builder, fmask_ptr,
4689 ctx->v8i32, "");
4690
4691 LLVMValueRef fmask_word1 =
4692 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
4693 uint_bld->one, "");
4694
4695 LLVMValueRef word1_is_nonzero =
4696 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
4697 fmask_word1, uint_bld->zero, "");
4698
4699 /* Replace the MSAA sample index. */
4700 address[sample_chan] =
4701 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
4702 final_sample, address[sample_chan], "");
4703 }
4704
4705 if (opcode == TGSI_OPCODE_TXF) {
4706 /* add tex offsets */
4707 if (inst->Texture.NumOffsets) {
4708 struct lp_build_context *uint_bld = &bld_base->uint_bld;
4709 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
4710 const struct tgsi_texture_offset *off = inst->TexOffsets;
4711
4712 assert(inst->Texture.NumOffsets == 1);
4713
4714 switch (target) {
4715 case TGSI_TEXTURE_3D:
4716 address[2] = lp_build_add(uint_bld, address[2],
4717 bld->immediates[off->Index][off->SwizzleZ]);
4718 /* fall through */
4719 case TGSI_TEXTURE_2D:
4720 case TGSI_TEXTURE_SHADOW2D:
4721 case TGSI_TEXTURE_RECT:
4722 case TGSI_TEXTURE_SHADOWRECT:
4723 case TGSI_TEXTURE_2D_ARRAY:
4724 case TGSI_TEXTURE_SHADOW2D_ARRAY:
4725 address[1] =
4726 lp_build_add(uint_bld, address[1],
4727 bld->immediates[off->Index][off->SwizzleY]);
4728 /* fall through */
4729 case TGSI_TEXTURE_1D:
4730 case TGSI_TEXTURE_SHADOW1D:
4731 case TGSI_TEXTURE_1D_ARRAY:
4732 case TGSI_TEXTURE_SHADOW1D_ARRAY:
4733 address[0] =
4734 lp_build_add(uint_bld, address[0],
4735 bld->immediates[off->Index][off->SwizzleX]);
4736 break;
4737 /* texture offsets do not apply to other texture targets */
4738 }
4739 }
4740 }
4741
4742 if (opcode == TGSI_OPCODE_TG4) {
4743 unsigned gather_comp = 0;
4744
4745 /* DMASK was repurposed for GATHER4. 4 components are always
4746 * returned and DMASK works like a swizzle - it selects
4747 * the component to fetch. The only valid DMASK values are
4748 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
4749 * (red,red,red,red) etc.) The ISA document doesn't mention
4750 * this.
4751 */
4752
4753 /* Get the component index from src1.x for Gather4. */
4754 if (!tgsi_is_shadow_target(target)) {
4755 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
4756 LLVMValueRef comp_imm;
4757 struct tgsi_src_register src1 = inst->Src[1].Register;
4758
4759 assert(src1.File == TGSI_FILE_IMMEDIATE);
4760
4761 comp_imm = imms[src1.Index][src1.SwizzleX];
4762 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
4763 gather_comp = CLAMP(gather_comp, 0, 3);
4764 }
4765
4766 dmask = 1 << gather_comp;
4767 }
4768
4769 set_tex_fetch_args(ctx, emit_data, opcode, target, res_ptr,
4770 samp_ptr, address, count, dmask);
4771 }
4772
4773 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
4774 * incorrectly forces nearest filtering if the texture format is integer.
4775 * The only effect it has on Gather4, which always returns 4 texels for
4776 * bilinear filtering, is that the final coordinates are off by 0.5 of
4777 * the texel size.
4778 *
4779 * The workaround is to subtract 0.5 from the unnormalized coordinates,
4780 * or (0.5 / size) from the normalized coordinates.
4781 */
4782 static void si_lower_gather4_integer(struct si_shader_context *ctx,
4783 struct lp_build_emit_data *emit_data,
4784 const char *intr_name,
4785 unsigned coord_vgpr_index)
4786 {
4787 LLVMBuilderRef builder = ctx->radeon_bld.gallivm.builder;
4788 LLVMValueRef coord = emit_data->args[0];
4789 LLVMValueRef half_texel[2];
4790 int c;
4791
4792 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_RECT ||
4793 emit_data->inst->Texture.Texture == TGSI_TEXTURE_SHADOWRECT) {
4794 half_texel[0] = half_texel[1] = LLVMConstReal(ctx->f32, -0.5);
4795 } else {
4796 struct tgsi_full_instruction txq_inst = {};
4797 struct lp_build_emit_data txq_emit_data = {};
4798
4799 /* Query the texture size. */
4800 txq_inst.Texture.Texture = emit_data->inst->Texture.Texture;
4801 txq_emit_data.inst = &txq_inst;
4802 txq_emit_data.dst_type = ctx->v4i32;
4803 set_tex_fetch_args(ctx, &txq_emit_data, TGSI_OPCODE_TXQ,
4804 txq_inst.Texture.Texture,
4805 emit_data->args[1], NULL,
4806 &ctx->radeon_bld.soa.bld_base.uint_bld.zero,
4807 1, 0xf);
4808 txq_emit(NULL, &ctx->radeon_bld.soa.bld_base, &txq_emit_data);
4809
4810 /* Compute -0.5 / size. */
4811 for (c = 0; c < 2; c++) {
4812 half_texel[c] =
4813 LLVMBuildExtractElement(builder, txq_emit_data.output[0],
4814 LLVMConstInt(ctx->i32, c, 0), "");
4815 half_texel[c] = LLVMBuildUIToFP(builder, half_texel[c], ctx->f32, "");
4816 half_texel[c] =
4817 lp_build_emit_llvm_unary(&ctx->radeon_bld.soa.bld_base,
4818 TGSI_OPCODE_RCP, half_texel[c]);
4819 half_texel[c] = LLVMBuildFMul(builder, half_texel[c],
4820 LLVMConstReal(ctx->f32, -0.5), "");
4821 }
4822 }
4823
4824 for (c = 0; c < 2; c++) {
4825 LLVMValueRef tmp;
4826 LLVMValueRef index = LLVMConstInt(ctx->i32, coord_vgpr_index + c, 0);
4827
4828 tmp = LLVMBuildExtractElement(builder, coord, index, "");
4829 tmp = LLVMBuildBitCast(builder, tmp, ctx->f32, "");
4830 tmp = LLVMBuildFAdd(builder, tmp, half_texel[c], "");
4831 tmp = LLVMBuildBitCast(builder, tmp, ctx->i32, "");
4832 coord = LLVMBuildInsertElement(builder, coord, tmp, index, "");
4833 }
4834
4835 emit_data->args[0] = coord;
4836 emit_data->output[emit_data->chan] =
4837 lp_build_intrinsic(builder, intr_name, emit_data->dst_type,
4838 emit_data->args, emit_data->arg_count,
4839 LLVMReadNoneAttribute);
4840 }
4841
4842 static void build_tex_intrinsic(const struct lp_build_tgsi_action *action,
4843 struct lp_build_tgsi_context *bld_base,
4844 struct lp_build_emit_data *emit_data)
4845 {
4846 struct si_shader_context *ctx = si_shader_context(bld_base);
4847 struct lp_build_context *base = &bld_base->base;
4848 const struct tgsi_full_instruction *inst = emit_data->inst;
4849 unsigned opcode = inst->Instruction.Opcode;
4850 unsigned target = inst->Texture.Texture;
4851 char intr_name[127];
4852 bool has_offset = inst->Texture.NumOffsets > 0;
4853 bool is_shadow = tgsi_is_shadow_target(target);
4854 char type[64];
4855 const char *name = "llvm.SI.image.sample";
4856 const char *infix = "";
4857
4858 if (target == TGSI_TEXTURE_BUFFER) {
4859 emit_data->output[emit_data->chan] = lp_build_intrinsic(
4860 base->gallivm->builder,
4861 "llvm.SI.vs.load.input", emit_data->dst_type,
4862 emit_data->args, emit_data->arg_count,
4863 LLVMReadNoneAttribute);
4864 return;
4865 }
4866
4867 switch (opcode) {
4868 case TGSI_OPCODE_TXF:
4869 name = target == TGSI_TEXTURE_2D_MSAA ||
4870 target == TGSI_TEXTURE_2D_ARRAY_MSAA ?
4871 "llvm.SI.image.load" :
4872 "llvm.SI.image.load.mip";
4873 is_shadow = false;
4874 has_offset = false;
4875 break;
4876 case TGSI_OPCODE_LODQ:
4877 name = "llvm.SI.getlod";
4878 is_shadow = false;
4879 has_offset = false;
4880 break;
4881 case TGSI_OPCODE_TEX:
4882 case TGSI_OPCODE_TEX2:
4883 case TGSI_OPCODE_TXP:
4884 if (ctx->type != PIPE_SHADER_FRAGMENT)
4885 infix = ".lz";
4886 break;
4887 case TGSI_OPCODE_TXB:
4888 case TGSI_OPCODE_TXB2:
4889 assert(ctx->type == PIPE_SHADER_FRAGMENT);
4890 infix = ".b";
4891 break;
4892 case TGSI_OPCODE_TXL:
4893 case TGSI_OPCODE_TXL2:
4894 infix = ".l";
4895 break;
4896 case TGSI_OPCODE_TXD:
4897 infix = ".d";
4898 break;
4899 case TGSI_OPCODE_TG4:
4900 name = "llvm.SI.gather4";
4901 infix = ".lz";
4902 break;
4903 default:
4904 assert(0);
4905 return;
4906 }
4907
4908 /* Add the type and suffixes .c, .o if needed. */
4909 build_int_type_name(LLVMTypeOf(emit_data->args[0]), type, sizeof(type));
4910 sprintf(intr_name, "%s%s%s%s.%s",
4911 name, is_shadow ? ".c" : "", infix,
4912 has_offset ? ".o" : "", type);
4913
4914 /* The hardware needs special lowering for Gather4 with integer formats. */
4915 if (opcode == TGSI_OPCODE_TG4) {
4916 struct tgsi_shader_info *info = &ctx->shader->selector->info;
4917 /* This will also work with non-constant indexing because of how
4918 * glsl_to_tgsi works and we intent to preserve that behavior.
4919 */
4920 const unsigned src_idx = 2;
4921 unsigned sampler = inst->Src[src_idx].Register.Index;
4922
4923 assert(inst->Src[src_idx].Register.File == TGSI_FILE_SAMPLER);
4924
4925 if (info->sampler_type[sampler] == TGSI_RETURN_TYPE_SINT ||
4926 info->sampler_type[sampler] == TGSI_RETURN_TYPE_UINT) {
4927 /* Texture coordinates start after:
4928 * {offset, bias, z-compare, derivatives}
4929 * Only the offset and z-compare can occur here.
4930 */
4931 si_lower_gather4_integer(ctx, emit_data, intr_name,
4932 (int)has_offset + (int)is_shadow);
4933 return;
4934 }
4935 }
4936
4937 emit_data->output[emit_data->chan] = lp_build_intrinsic(
4938 base->gallivm->builder, intr_name, emit_data->dst_type,
4939 emit_data->args, emit_data->arg_count,
4940 LLVMReadNoneAttribute);
4941 }
4942
4943 static void si_llvm_emit_txqs(
4944 const struct lp_build_tgsi_action *action,
4945 struct lp_build_tgsi_context *bld_base,
4946 struct lp_build_emit_data *emit_data)
4947 {
4948 struct si_shader_context *ctx = si_shader_context(bld_base);
4949 struct gallivm_state *gallivm = bld_base->base.gallivm;
4950 LLVMBuilderRef builder = gallivm->builder;
4951 LLVMValueRef res, samples;
4952 LLVMValueRef res_ptr, samp_ptr, fmask_ptr = NULL;
4953
4954 tex_fetch_ptrs(bld_base, emit_data, &res_ptr, &samp_ptr, &fmask_ptr);
4955
4956
4957 /* Read the samples from the descriptor directly. */
4958 res = LLVMBuildBitCast(builder, res_ptr, ctx->v8i32, "");
4959 samples = LLVMBuildExtractElement(
4960 builder, res,
4961 lp_build_const_int32(gallivm, 3), "");
4962 samples = LLVMBuildLShr(builder, samples,
4963 lp_build_const_int32(gallivm, 16), "");
4964 samples = LLVMBuildAnd(builder, samples,
4965 lp_build_const_int32(gallivm, 0xf), "");
4966 samples = LLVMBuildShl(builder, lp_build_const_int32(gallivm, 1),
4967 samples, "");
4968
4969 emit_data->output[emit_data->chan] = samples;
4970 }
4971
4972 /*
4973 * SI implements derivatives using the local data store (LDS)
4974 * All writes to the LDS happen in all executing threads at
4975 * the same time. TID is the Thread ID for the current
4976 * thread and is a value between 0 and 63, representing
4977 * the thread's position in the wavefront.
4978 *
4979 * For the pixel shader threads are grouped into quads of four pixels.
4980 * The TIDs of the pixels of a quad are:
4981 *
4982 * +------+------+
4983 * |4n + 0|4n + 1|
4984 * +------+------+
4985 * |4n + 2|4n + 3|
4986 * +------+------+
4987 *
4988 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
4989 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
4990 * the current pixel's column, and masking with 0xfffffffe yields the TID
4991 * of the left pixel of the current pixel's row.
4992 *
4993 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
4994 * adding 2 yields the TID of the pixel below the top pixel.
4995 */
4996 /* masks for thread ID. */
4997 #define TID_MASK_TOP_LEFT 0xfffffffc
4998 #define TID_MASK_TOP 0xfffffffd
4999 #define TID_MASK_LEFT 0xfffffffe
5000
5001 static void si_llvm_emit_ddxy(
5002 const struct lp_build_tgsi_action *action,
5003 struct lp_build_tgsi_context *bld_base,
5004 struct lp_build_emit_data *emit_data)
5005 {
5006 struct si_shader_context *ctx = si_shader_context(bld_base);
5007 struct gallivm_state *gallivm = bld_base->base.gallivm;
5008 const struct tgsi_full_instruction *inst = emit_data->inst;
5009 unsigned opcode = inst->Instruction.Opcode;
5010 LLVMValueRef store_ptr, load_ptr0, load_ptr1, thread_id;
5011 LLVMValueRef tl, trbl, result[4];
5012 LLVMValueRef tl_tid, trbl_tid;
5013 unsigned swizzle[4];
5014 unsigned c;
5015 int idx;
5016 unsigned mask;
5017 bool has_ds_bpermute = HAVE_LLVM >= 0x0309 &&
5018 ctx->screen->b.chip_class >= VI;
5019
5020 thread_id = get_thread_id(ctx);;
5021
5022 if (opcode == TGSI_OPCODE_DDX_FINE)
5023 mask = TID_MASK_LEFT;
5024 else if (opcode == TGSI_OPCODE_DDY_FINE)
5025 mask = TID_MASK_TOP;
5026 else
5027 mask = TID_MASK_TOP_LEFT;
5028
5029 tl_tid = LLVMBuildAnd(gallivm->builder, thread_id,
5030 lp_build_const_int32(gallivm, mask), "");
5031
5032 /* for DDX we want to next X pixel, DDY next Y pixel. */
5033 idx = (opcode == TGSI_OPCODE_DDX || opcode == TGSI_OPCODE_DDX_FINE) ? 1 : 2;
5034 trbl_tid = LLVMBuildAdd(gallivm->builder, tl_tid,
5035 lp_build_const_int32(gallivm, idx), "");
5036
5037 if (!has_ds_bpermute) {
5038 store_ptr = build_gep0(ctx, ctx->lds, thread_id);
5039 load_ptr0 = build_gep0(ctx, ctx->lds, tl_tid);
5040 load_ptr1 = build_gep0(ctx, ctx->lds, trbl_tid);
5041 }
5042
5043 for (c = 0; c < 4; ++c) {
5044 unsigned i;
5045 LLVMValueRef val;
5046 LLVMValueRef args[2];
5047
5048 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
5049 for (i = 0; i < c; ++i) {
5050 if (swizzle[i] == swizzle[c]) {
5051 result[c] = result[i];
5052 break;
5053 }
5054 }
5055 if (i != c)
5056 continue;
5057
5058 val = LLVMBuildBitCast(gallivm->builder,
5059 lp_build_emit_fetch(bld_base, inst, 0, c),
5060 ctx->i32, "");
5061
5062 if (has_ds_bpermute) {
5063 args[0] = LLVMBuildMul(gallivm->builder, tl_tid,
5064 lp_build_const_int32(gallivm, 4), "");
5065 args[1] = val;
5066 tl = lp_build_intrinsic(gallivm->builder,
5067 "llvm.amdgcn.ds.bpermute", ctx->i32,
5068 args, 2, LLVMReadNoneAttribute);
5069
5070 args[0] = LLVMBuildMul(gallivm->builder, trbl_tid,
5071 lp_build_const_int32(gallivm, 4), "");
5072 trbl = lp_build_intrinsic(gallivm->builder,
5073 "llvm.amdgcn.ds.bpermute", ctx->i32,
5074 args, 2, LLVMReadNoneAttribute);
5075 } else {
5076 LLVMBuildStore(gallivm->builder, val, store_ptr);
5077 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
5078 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
5079 }
5080 tl = LLVMBuildBitCast(gallivm->builder, tl, ctx->f32, "");
5081 trbl = LLVMBuildBitCast(gallivm->builder, trbl, ctx->f32, "");
5082 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
5083 }
5084
5085 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
5086 }
5087
5088 /*
5089 * this takes an I,J coordinate pair,
5090 * and works out the X and Y derivatives.
5091 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
5092 */
5093 static LLVMValueRef si_llvm_emit_ddxy_interp(
5094 struct lp_build_tgsi_context *bld_base,
5095 LLVMValueRef interp_ij)
5096 {
5097 struct si_shader_context *ctx = si_shader_context(bld_base);
5098 struct gallivm_state *gallivm = bld_base->base.gallivm;
5099 LLVMValueRef store_ptr, load_ptr_x, load_ptr_y, load_ptr_ddx, load_ptr_ddy, temp, temp2;
5100 LLVMValueRef tl, tr, bl, result[4], thread_id;
5101 unsigned c;
5102
5103 thread_id = get_thread_id(ctx);
5104 store_ptr = build_gep0(ctx, ctx->lds, thread_id);
5105
5106 temp = LLVMBuildAnd(gallivm->builder, thread_id,
5107 lp_build_const_int32(gallivm, TID_MASK_LEFT), "");
5108
5109 temp2 = LLVMBuildAnd(gallivm->builder, thread_id,
5110 lp_build_const_int32(gallivm, TID_MASK_TOP), "");
5111
5112 load_ptr_x = build_gep0(ctx, ctx->lds, temp);
5113
5114 load_ptr_y = build_gep0(ctx, ctx->lds, temp2);
5115
5116 load_ptr_ddx = build_gep0(ctx, ctx->lds,
5117 LLVMBuildAdd(gallivm->builder, temp,
5118 lp_build_const_int32(gallivm, 1), ""));
5119
5120 load_ptr_ddy = build_gep0(ctx, ctx->lds,
5121 LLVMBuildAdd(gallivm->builder, temp2,
5122 lp_build_const_int32(gallivm, 2), ""));
5123
5124 for (c = 0; c < 2; ++c) {
5125 LLVMValueRef store_val;
5126 LLVMValueRef c_ll = lp_build_const_int32(gallivm, c);
5127
5128 store_val = LLVMBuildExtractElement(gallivm->builder,
5129 interp_ij, c_ll, "");
5130 LLVMBuildStore(gallivm->builder,
5131 store_val,
5132 store_ptr);
5133
5134 tl = LLVMBuildLoad(gallivm->builder, load_ptr_x, "");
5135 tl = LLVMBuildBitCast(gallivm->builder, tl, ctx->f32, "");
5136
5137 tr = LLVMBuildLoad(gallivm->builder, load_ptr_ddx, "");
5138 tr = LLVMBuildBitCast(gallivm->builder, tr, ctx->f32, "");
5139
5140 result[c] = LLVMBuildFSub(gallivm->builder, tr, tl, "");
5141
5142 tl = LLVMBuildLoad(gallivm->builder, load_ptr_y, "");
5143 tl = LLVMBuildBitCast(gallivm->builder, tl, ctx->f32, "");
5144
5145 bl = LLVMBuildLoad(gallivm->builder, load_ptr_ddy, "");
5146 bl = LLVMBuildBitCast(gallivm->builder, bl, ctx->f32, "");
5147
5148 result[c + 2] = LLVMBuildFSub(gallivm->builder, bl, tl, "");
5149 }
5150
5151 return lp_build_gather_values(gallivm, result, 4);
5152 }
5153
5154 static void interp_fetch_args(
5155 struct lp_build_tgsi_context *bld_base,
5156 struct lp_build_emit_data *emit_data)
5157 {
5158 struct si_shader_context *ctx = si_shader_context(bld_base);
5159 struct gallivm_state *gallivm = bld_base->base.gallivm;
5160 const struct tgsi_full_instruction *inst = emit_data->inst;
5161
5162 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET) {
5163 /* offset is in second src, first two channels */
5164 emit_data->args[0] = lp_build_emit_fetch(bld_base,
5165 emit_data->inst, 1,
5166 TGSI_CHAN_X);
5167 emit_data->args[1] = lp_build_emit_fetch(bld_base,
5168 emit_data->inst, 1,
5169 TGSI_CHAN_Y);
5170 emit_data->arg_count = 2;
5171 } else if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
5172 LLVMValueRef sample_position;
5173 LLVMValueRef sample_id;
5174 LLVMValueRef halfval = lp_build_const_float(gallivm, 0.5f);
5175
5176 /* fetch sample ID, then fetch its sample position,
5177 * and place into first two channels.
5178 */
5179 sample_id = lp_build_emit_fetch(bld_base,
5180 emit_data->inst, 1, TGSI_CHAN_X);
5181 sample_id = LLVMBuildBitCast(gallivm->builder, sample_id,
5182 ctx->i32, "");
5183 sample_position = load_sample_position(&ctx->radeon_bld, sample_id);
5184
5185 emit_data->args[0] = LLVMBuildExtractElement(gallivm->builder,
5186 sample_position,
5187 lp_build_const_int32(gallivm, 0), "");
5188
5189 emit_data->args[0] = LLVMBuildFSub(gallivm->builder, emit_data->args[0], halfval, "");
5190 emit_data->args[1] = LLVMBuildExtractElement(gallivm->builder,
5191 sample_position,
5192 lp_build_const_int32(gallivm, 1), "");
5193 emit_data->args[1] = LLVMBuildFSub(gallivm->builder, emit_data->args[1], halfval, "");
5194 emit_data->arg_count = 2;
5195 }
5196 }
5197
5198 static void build_interp_intrinsic(const struct lp_build_tgsi_action *action,
5199 struct lp_build_tgsi_context *bld_base,
5200 struct lp_build_emit_data *emit_data)
5201 {
5202 struct si_shader_context *ctx = si_shader_context(bld_base);
5203 struct si_shader *shader = ctx->shader;
5204 struct gallivm_state *gallivm = bld_base->base.gallivm;
5205 LLVMValueRef interp_param;
5206 const struct tgsi_full_instruction *inst = emit_data->inst;
5207 const char *intr_name;
5208 int input_index = inst->Src[0].Register.Index;
5209 int chan;
5210 int i;
5211 LLVMValueRef attr_number;
5212 LLVMValueRef params = LLVMGetParam(ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
5213 int interp_param_idx;
5214 unsigned interp = shader->selector->info.input_interpolate[input_index];
5215 unsigned location;
5216
5217 assert(inst->Src[0].Register.File == TGSI_FILE_INPUT);
5218
5219 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
5220 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE)
5221 location = TGSI_INTERPOLATE_LOC_CENTER;
5222 else
5223 location = TGSI_INTERPOLATE_LOC_CENTROID;
5224
5225 interp_param_idx = lookup_interp_param_index(interp, location);
5226 if (interp_param_idx == -1)
5227 return;
5228 else if (interp_param_idx)
5229 interp_param = get_interp_param(ctx, interp_param_idx);
5230 else
5231 interp_param = NULL;
5232
5233 attr_number = lp_build_const_int32(gallivm, input_index);
5234
5235 if (inst->Instruction.Opcode == TGSI_OPCODE_INTERP_OFFSET ||
5236 inst->Instruction.Opcode == TGSI_OPCODE_INTERP_SAMPLE) {
5237 LLVMValueRef ij_out[2];
5238 LLVMValueRef ddxy_out = si_llvm_emit_ddxy_interp(bld_base, interp_param);
5239
5240 /*
5241 * take the I then J parameters, and the DDX/Y for it, and
5242 * calculate the IJ inputs for the interpolator.
5243 * temp1 = ddx * offset/sample.x + I;
5244 * interp_param.I = ddy * offset/sample.y + temp1;
5245 * temp1 = ddx * offset/sample.x + J;
5246 * interp_param.J = ddy * offset/sample.y + temp1;
5247 */
5248 for (i = 0; i < 2; i++) {
5249 LLVMValueRef ix_ll = lp_build_const_int32(gallivm, i);
5250 LLVMValueRef iy_ll = lp_build_const_int32(gallivm, i + 2);
5251 LLVMValueRef ddx_el = LLVMBuildExtractElement(gallivm->builder,
5252 ddxy_out, ix_ll, "");
5253 LLVMValueRef ddy_el = LLVMBuildExtractElement(gallivm->builder,
5254 ddxy_out, iy_ll, "");
5255 LLVMValueRef interp_el = LLVMBuildExtractElement(gallivm->builder,
5256 interp_param, ix_ll, "");
5257 LLVMValueRef temp1, temp2;
5258
5259 interp_el = LLVMBuildBitCast(gallivm->builder, interp_el,
5260 ctx->f32, "");
5261
5262 temp1 = LLVMBuildFMul(gallivm->builder, ddx_el, emit_data->args[0], "");
5263
5264 temp1 = LLVMBuildFAdd(gallivm->builder, temp1, interp_el, "");
5265
5266 temp2 = LLVMBuildFMul(gallivm->builder, ddy_el, emit_data->args[1], "");
5267
5268 temp2 = LLVMBuildFAdd(gallivm->builder, temp2, temp1, "");
5269
5270 ij_out[i] = LLVMBuildBitCast(gallivm->builder,
5271 temp2, ctx->i32, "");
5272 }
5273 interp_param = lp_build_gather_values(bld_base->base.gallivm, ij_out, 2);
5274 }
5275
5276 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
5277 for (chan = 0; chan < 2; chan++) {
5278 LLVMValueRef args[4];
5279 LLVMValueRef llvm_chan;
5280 unsigned schan;
5281
5282 schan = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], chan);
5283 llvm_chan = lp_build_const_int32(gallivm, schan);
5284
5285 args[0] = llvm_chan;
5286 args[1] = attr_number;
5287 args[2] = params;
5288 args[3] = interp_param;
5289
5290 emit_data->output[chan] =
5291 lp_build_intrinsic(gallivm->builder, intr_name,
5292 ctx->f32, args, args[3] ? 4 : 3,
5293 LLVMReadNoneAttribute);
5294 }
5295 }
5296
5297 static unsigned si_llvm_get_stream(struct lp_build_tgsi_context *bld_base,
5298 struct lp_build_emit_data *emit_data)
5299 {
5300 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
5301 struct tgsi_src_register src0 = emit_data->inst->Src[0].Register;
5302 unsigned stream;
5303
5304 assert(src0.File == TGSI_FILE_IMMEDIATE);
5305
5306 stream = LLVMConstIntGetZExtValue(imms[src0.Index][src0.SwizzleX]) & 0x3;
5307 return stream;
5308 }
5309
5310 /* Emit one vertex from the geometry shader */
5311 static void si_llvm_emit_vertex(
5312 const struct lp_build_tgsi_action *action,
5313 struct lp_build_tgsi_context *bld_base,
5314 struct lp_build_emit_data *emit_data)
5315 {
5316 struct si_shader_context *ctx = si_shader_context(bld_base);
5317 struct lp_build_context *uint = &bld_base->uint_bld;
5318 struct si_shader *shader = ctx->shader;
5319 struct tgsi_shader_info *info = &shader->selector->info;
5320 struct gallivm_state *gallivm = bld_base->base.gallivm;
5321 LLVMValueRef soffset = LLVMGetParam(ctx->radeon_bld.main_fn,
5322 SI_PARAM_GS2VS_OFFSET);
5323 LLVMValueRef gs_next_vertex;
5324 LLVMValueRef can_emit, kill;
5325 LLVMValueRef args[2];
5326 unsigned chan;
5327 int i;
5328 unsigned stream;
5329
5330 stream = si_llvm_get_stream(bld_base, emit_data);
5331
5332 /* Write vertex attribute values to GSVS ring */
5333 gs_next_vertex = LLVMBuildLoad(gallivm->builder,
5334 ctx->gs_next_vertex[stream],
5335 "");
5336
5337 /* If this thread has already emitted the declared maximum number of
5338 * vertices, kill it: excessive vertex emissions are not supposed to
5339 * have any effect, and GS threads have no externally observable
5340 * effects other than emitting vertices.
5341 */
5342 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
5343 lp_build_const_int32(gallivm,
5344 shader->selector->gs_max_out_vertices), "");
5345 kill = lp_build_select(&bld_base->base, can_emit,
5346 lp_build_const_float(gallivm, 1.0f),
5347 lp_build_const_float(gallivm, -1.0f));
5348
5349 lp_build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
5350 ctx->voidt, &kill, 1, 0);
5351
5352 for (i = 0; i < info->num_outputs; i++) {
5353 LLVMValueRef *out_ptr =
5354 ctx->radeon_bld.soa.outputs[i];
5355
5356 for (chan = 0; chan < 4; chan++) {
5357 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
5358 LLVMValueRef voffset =
5359 lp_build_const_int32(gallivm, (i * 4 + chan) *
5360 shader->selector->gs_max_out_vertices);
5361
5362 voffset = lp_build_add(uint, voffset, gs_next_vertex);
5363 voffset = lp_build_mul_imm(uint, voffset, 4);
5364
5365 out_val = LLVMBuildBitCast(gallivm->builder, out_val, ctx->i32, "");
5366
5367 build_tbuffer_store(ctx,
5368 ctx->gsvs_ring[stream],
5369 out_val, 1,
5370 voffset, soffset, 0,
5371 V_008F0C_BUF_DATA_FORMAT_32,
5372 V_008F0C_BUF_NUM_FORMAT_UINT,
5373 1, 0, 1, 1, 0);
5374 }
5375 }
5376 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
5377 lp_build_const_int32(gallivm, 1));
5378
5379 LLVMBuildStore(gallivm->builder, gs_next_vertex, ctx->gs_next_vertex[stream]);
5380
5381 /* Signal vertex emission */
5382 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS | (stream << 8));
5383 args[1] = LLVMGetParam(ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
5384 lp_build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
5385 ctx->voidt, args, 2, 0);
5386 }
5387
5388 /* Cut one primitive from the geometry shader */
5389 static void si_llvm_emit_primitive(
5390 const struct lp_build_tgsi_action *action,
5391 struct lp_build_tgsi_context *bld_base,
5392 struct lp_build_emit_data *emit_data)
5393 {
5394 struct si_shader_context *ctx = si_shader_context(bld_base);
5395 struct gallivm_state *gallivm = bld_base->base.gallivm;
5396 LLVMValueRef args[2];
5397 unsigned stream;
5398
5399 /* Signal primitive cut */
5400 stream = si_llvm_get_stream(bld_base, emit_data);
5401 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS | (stream << 8));
5402 args[1] = LLVMGetParam(ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
5403 lp_build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
5404 ctx->voidt, args, 2, 0);
5405 }
5406
5407 static void si_llvm_emit_barrier(const struct lp_build_tgsi_action *action,
5408 struct lp_build_tgsi_context *bld_base,
5409 struct lp_build_emit_data *emit_data)
5410 {
5411 struct si_shader_context *ctx = si_shader_context(bld_base);
5412 struct gallivm_state *gallivm = bld_base->base.gallivm;
5413
5414 /* The real barrier instruction isn’t needed, because an entire patch
5415 * always fits into a single wave.
5416 */
5417 if (ctx->type == PIPE_SHADER_TESS_CTRL) {
5418 emit_optimization_barrier(ctx);
5419 return;
5420 }
5421
5422 lp_build_intrinsic(gallivm->builder,
5423 HAVE_LLVM >= 0x0309 ? "llvm.amdgcn.s.barrier"
5424 : "llvm.AMDGPU.barrier.local",
5425 ctx->voidt, NULL, 0, 0);
5426 }
5427
5428 static const struct lp_build_tgsi_action tex_action = {
5429 .fetch_args = tex_fetch_args,
5430 .emit = build_tex_intrinsic,
5431 };
5432
5433 static const struct lp_build_tgsi_action interp_action = {
5434 .fetch_args = interp_fetch_args,
5435 .emit = build_interp_intrinsic,
5436 };
5437
5438 static void si_create_function(struct si_shader_context *ctx,
5439 LLVMTypeRef *returns, unsigned num_returns,
5440 LLVMTypeRef *params, unsigned num_params,
5441 int last_sgpr)
5442 {
5443 int i;
5444
5445 radeon_llvm_create_func(&ctx->radeon_bld, returns, num_returns,
5446 params, num_params);
5447 radeon_llvm_shader_type(ctx->radeon_bld.main_fn, ctx->type);
5448 ctx->return_value = LLVMGetUndef(ctx->radeon_bld.return_type);
5449
5450 for (i = 0; i <= last_sgpr; ++i) {
5451 LLVMValueRef P = LLVMGetParam(ctx->radeon_bld.main_fn, i);
5452
5453 /* The combination of:
5454 * - ByVal
5455 * - dereferenceable
5456 * - invariant.load
5457 * allows the optimization passes to move loads and reduces
5458 * SGPR spilling significantly.
5459 */
5460 if (LLVMGetTypeKind(LLVMTypeOf(P)) == LLVMPointerTypeKind) {
5461 LLVMAddAttribute(P, LLVMByValAttribute);
5462 lp_add_attr_dereferenceable(P, UINT64_MAX);
5463 } else
5464 LLVMAddAttribute(P, LLVMInRegAttribute);
5465 }
5466
5467 if (ctx->screen->b.debug_flags & DBG_UNSAFE_MATH) {
5468 /* These were copied from some LLVM test. */
5469 LLVMAddTargetDependentFunctionAttr(ctx->radeon_bld.main_fn,
5470 "less-precise-fpmad",
5471 "true");
5472 LLVMAddTargetDependentFunctionAttr(ctx->radeon_bld.main_fn,
5473 "no-infs-fp-math",
5474 "true");
5475 LLVMAddTargetDependentFunctionAttr(ctx->radeon_bld.main_fn,
5476 "no-nans-fp-math",
5477 "true");
5478 LLVMAddTargetDependentFunctionAttr(ctx->radeon_bld.main_fn,
5479 "unsafe-fp-math",
5480 "true");
5481 }
5482 }
5483
5484 static void create_meta_data(struct si_shader_context *ctx)
5485 {
5486 struct gallivm_state *gallivm = ctx->radeon_bld.soa.bld_base.base.gallivm;
5487
5488 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(gallivm->context,
5489 "invariant.load", 14);
5490 ctx->range_md_kind = LLVMGetMDKindIDInContext(gallivm->context,
5491 "range", 5);
5492 ctx->uniform_md_kind = LLVMGetMDKindIDInContext(gallivm->context,
5493 "amdgpu.uniform", 14);
5494
5495 ctx->empty_md = LLVMMDNodeInContext(gallivm->context, NULL, 0);
5496 }
5497
5498 static void declare_streamout_params(struct si_shader_context *ctx,
5499 struct pipe_stream_output_info *so,
5500 LLVMTypeRef *params, LLVMTypeRef i32,
5501 unsigned *num_params)
5502 {
5503 int i;
5504
5505 /* Streamout SGPRs. */
5506 if (so->num_outputs) {
5507 if (ctx->type != PIPE_SHADER_TESS_EVAL)
5508 params[ctx->param_streamout_config = (*num_params)++] = i32;
5509 else
5510 ctx->param_streamout_config = ctx->param_tess_offchip;
5511
5512 params[ctx->param_streamout_write_index = (*num_params)++] = i32;
5513 }
5514 /* A streamout buffer offset is loaded if the stride is non-zero. */
5515 for (i = 0; i < 4; i++) {
5516 if (!so->stride[i])
5517 continue;
5518
5519 params[ctx->param_streamout_offset[i] = (*num_params)++] = i32;
5520 }
5521 }
5522
5523 static unsigned llvm_get_type_size(LLVMTypeRef type)
5524 {
5525 LLVMTypeKind kind = LLVMGetTypeKind(type);
5526
5527 switch (kind) {
5528 case LLVMIntegerTypeKind:
5529 return LLVMGetIntTypeWidth(type) / 8;
5530 case LLVMFloatTypeKind:
5531 return 4;
5532 case LLVMPointerTypeKind:
5533 return 8;
5534 case LLVMVectorTypeKind:
5535 return LLVMGetVectorSize(type) *
5536 llvm_get_type_size(LLVMGetElementType(type));
5537 default:
5538 assert(0);
5539 return 0;
5540 }
5541 }
5542
5543 static void declare_tess_lds(struct si_shader_context *ctx)
5544 {
5545 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
5546 struct lp_build_tgsi_context *bld_base = &ctx->radeon_bld.soa.bld_base;
5547 struct lp_build_context *uint = &bld_base->uint_bld;
5548
5549 unsigned lds_size = ctx->screen->b.chip_class >= CIK ? 65536 : 32768;
5550 ctx->lds = LLVMBuildIntToPtr(gallivm->builder, uint->zero,
5551 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), LOCAL_ADDR_SPACE),
5552 "tess_lds");
5553 }
5554
5555 static void create_function(struct si_shader_context *ctx)
5556 {
5557 struct lp_build_tgsi_context *bld_base = &ctx->radeon_bld.soa.bld_base;
5558 struct gallivm_state *gallivm = bld_base->base.gallivm;
5559 struct si_shader *shader = ctx->shader;
5560 LLVMTypeRef params[SI_NUM_PARAMS + SI_NUM_VERTEX_BUFFERS], v3i32;
5561 LLVMTypeRef returns[16+32*4];
5562 unsigned i, last_sgpr, num_params, num_return_sgprs;
5563 unsigned num_returns = 0;
5564
5565 v3i32 = LLVMVectorType(ctx->i32, 3);
5566
5567 params[SI_PARAM_RW_BUFFERS] = const_array(ctx->v16i8, SI_NUM_RW_BUFFERS);
5568 params[SI_PARAM_CONST_BUFFERS] = const_array(ctx->v16i8, SI_NUM_CONST_BUFFERS);
5569 params[SI_PARAM_SAMPLERS] = const_array(ctx->v8i32, SI_NUM_SAMPLERS);
5570 params[SI_PARAM_IMAGES] = const_array(ctx->v8i32, SI_NUM_IMAGES);
5571 params[SI_PARAM_SHADER_BUFFERS] = const_array(ctx->v4i32, SI_NUM_SHADER_BUFFERS);
5572
5573 switch (ctx->type) {
5574 case PIPE_SHADER_VERTEX:
5575 params[SI_PARAM_VERTEX_BUFFERS] = const_array(ctx->v16i8, SI_NUM_VERTEX_BUFFERS);
5576 params[SI_PARAM_BASE_VERTEX] = ctx->i32;
5577 params[SI_PARAM_START_INSTANCE] = ctx->i32;
5578 params[SI_PARAM_DRAWID] = ctx->i32;
5579 num_params = SI_PARAM_DRAWID+1;
5580
5581 if (shader->key.vs.as_es) {
5582 params[ctx->param_es2gs_offset = num_params++] = ctx->i32;
5583 } else if (shader->key.vs.as_ls) {
5584 params[SI_PARAM_LS_OUT_LAYOUT] = ctx->i32;
5585 num_params = SI_PARAM_LS_OUT_LAYOUT+1;
5586 } else {
5587 if (ctx->is_gs_copy_shader) {
5588 num_params = SI_PARAM_RW_BUFFERS+1;
5589 } else {
5590 params[SI_PARAM_VS_STATE_BITS] = ctx->i32;
5591 num_params = SI_PARAM_VS_STATE_BITS+1;
5592 }
5593
5594 /* The locations of the other parameters are assigned dynamically. */
5595 declare_streamout_params(ctx, &shader->selector->so,
5596 params, ctx->i32, &num_params);
5597 }
5598
5599 last_sgpr = num_params-1;
5600
5601 /* VGPRs */
5602 params[ctx->param_vertex_id = num_params++] = ctx->i32;
5603 params[ctx->param_rel_auto_id = num_params++] = ctx->i32;
5604 params[ctx->param_vs_prim_id = num_params++] = ctx->i32;
5605 params[ctx->param_instance_id = num_params++] = ctx->i32;
5606
5607 if (!ctx->is_monolithic &&
5608 !ctx->is_gs_copy_shader) {
5609 /* Vertex load indices. */
5610 ctx->param_vertex_index0 = num_params;
5611
5612 for (i = 0; i < shader->selector->info.num_inputs; i++)
5613 params[num_params++] = ctx->i32;
5614
5615 /* PrimitiveID output. */
5616 if (!shader->key.vs.as_es && !shader->key.vs.as_ls)
5617 for (i = 0; i <= VS_EPILOG_PRIMID_LOC; i++)
5618 returns[num_returns++] = ctx->f32;
5619 }
5620 break;
5621
5622 case PIPE_SHADER_TESS_CTRL:
5623 params[SI_PARAM_TCS_OFFCHIP_LAYOUT] = ctx->i32;
5624 params[SI_PARAM_TCS_OUT_OFFSETS] = ctx->i32;
5625 params[SI_PARAM_TCS_OUT_LAYOUT] = ctx->i32;
5626 params[SI_PARAM_TCS_IN_LAYOUT] = ctx->i32;
5627 params[ctx->param_oc_lds = SI_PARAM_TCS_OC_LDS] = ctx->i32;
5628 params[SI_PARAM_TESS_FACTOR_OFFSET] = ctx->i32;
5629 last_sgpr = SI_PARAM_TESS_FACTOR_OFFSET;
5630
5631 /* VGPRs */
5632 params[SI_PARAM_PATCH_ID] = ctx->i32;
5633 params[SI_PARAM_REL_IDS] = ctx->i32;
5634 num_params = SI_PARAM_REL_IDS+1;
5635
5636 if (!ctx->is_monolithic) {
5637 /* SI_PARAM_TCS_OC_LDS and PARAM_TESS_FACTOR_OFFSET are
5638 * placed after the user SGPRs.
5639 */
5640 for (i = 0; i < SI_TCS_NUM_USER_SGPR + 2; i++)
5641 returns[num_returns++] = ctx->i32; /* SGPRs */
5642
5643 for (i = 0; i < 3; i++)
5644 returns[num_returns++] = ctx->f32; /* VGPRs */
5645 }
5646 break;
5647
5648 case PIPE_SHADER_TESS_EVAL:
5649 params[SI_PARAM_TCS_OFFCHIP_LAYOUT] = ctx->i32;
5650 num_params = SI_PARAM_TCS_OFFCHIP_LAYOUT+1;
5651
5652 if (shader->key.tes.as_es) {
5653 params[ctx->param_oc_lds = num_params++] = ctx->i32;
5654 params[ctx->param_tess_offchip = num_params++] = ctx->i32;
5655 params[ctx->param_es2gs_offset = num_params++] = ctx->i32;
5656 } else {
5657 params[ctx->param_tess_offchip = num_params++] = ctx->i32;
5658 declare_streamout_params(ctx, &shader->selector->so,
5659 params, ctx->i32, &num_params);
5660 params[ctx->param_oc_lds = num_params++] = ctx->i32;
5661 }
5662 last_sgpr = num_params - 1;
5663
5664 /* VGPRs */
5665 params[ctx->param_tes_u = num_params++] = ctx->f32;
5666 params[ctx->param_tes_v = num_params++] = ctx->f32;
5667 params[ctx->param_tes_rel_patch_id = num_params++] = ctx->i32;
5668 params[ctx->param_tes_patch_id = num_params++] = ctx->i32;
5669
5670 /* PrimitiveID output. */
5671 if (!ctx->is_monolithic && !shader->key.tes.as_es)
5672 for (i = 0; i <= VS_EPILOG_PRIMID_LOC; i++)
5673 returns[num_returns++] = ctx->f32;
5674 break;
5675
5676 case PIPE_SHADER_GEOMETRY:
5677 params[SI_PARAM_GS2VS_OFFSET] = ctx->i32;
5678 params[SI_PARAM_GS_WAVE_ID] = ctx->i32;
5679 last_sgpr = SI_PARAM_GS_WAVE_ID;
5680
5681 /* VGPRs */
5682 params[SI_PARAM_VTX0_OFFSET] = ctx->i32;
5683 params[SI_PARAM_VTX1_OFFSET] = ctx->i32;
5684 params[SI_PARAM_PRIMITIVE_ID] = ctx->i32;
5685 params[SI_PARAM_VTX2_OFFSET] = ctx->i32;
5686 params[SI_PARAM_VTX3_OFFSET] = ctx->i32;
5687 params[SI_PARAM_VTX4_OFFSET] = ctx->i32;
5688 params[SI_PARAM_VTX5_OFFSET] = ctx->i32;
5689 params[SI_PARAM_GS_INSTANCE_ID] = ctx->i32;
5690 num_params = SI_PARAM_GS_INSTANCE_ID+1;
5691 break;
5692
5693 case PIPE_SHADER_FRAGMENT:
5694 params[SI_PARAM_ALPHA_REF] = ctx->f32;
5695 params[SI_PARAM_PRIM_MASK] = ctx->i32;
5696 last_sgpr = SI_PARAM_PRIM_MASK;
5697 params[SI_PARAM_PERSP_SAMPLE] = ctx->v2i32;
5698 params[SI_PARAM_PERSP_CENTER] = ctx->v2i32;
5699 params[SI_PARAM_PERSP_CENTROID] = ctx->v2i32;
5700 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
5701 params[SI_PARAM_LINEAR_SAMPLE] = ctx->v2i32;
5702 params[SI_PARAM_LINEAR_CENTER] = ctx->v2i32;
5703 params[SI_PARAM_LINEAR_CENTROID] = ctx->v2i32;
5704 params[SI_PARAM_LINE_STIPPLE_TEX] = ctx->f32;
5705 params[SI_PARAM_POS_X_FLOAT] = ctx->f32;
5706 params[SI_PARAM_POS_Y_FLOAT] = ctx->f32;
5707 params[SI_PARAM_POS_Z_FLOAT] = ctx->f32;
5708 params[SI_PARAM_POS_W_FLOAT] = ctx->f32;
5709 params[SI_PARAM_FRONT_FACE] = ctx->i32;
5710 params[SI_PARAM_ANCILLARY] = ctx->i32;
5711 params[SI_PARAM_SAMPLE_COVERAGE] = ctx->f32;
5712 params[SI_PARAM_POS_FIXED_PT] = ctx->i32;
5713 num_params = SI_PARAM_POS_FIXED_PT+1;
5714
5715 if (!ctx->is_monolithic) {
5716 /* Color inputs from the prolog. */
5717 if (shader->selector->info.colors_read) {
5718 unsigned num_color_elements =
5719 util_bitcount(shader->selector->info.colors_read);
5720
5721 assert(num_params + num_color_elements <= ARRAY_SIZE(params));
5722 for (i = 0; i < num_color_elements; i++)
5723 params[num_params++] = ctx->f32;
5724 }
5725
5726 /* Outputs for the epilog. */
5727 num_return_sgprs = SI_SGPR_ALPHA_REF + 1;
5728 num_returns =
5729 num_return_sgprs +
5730 util_bitcount(shader->selector->info.colors_written) * 4 +
5731 shader->selector->info.writes_z +
5732 shader->selector->info.writes_stencil +
5733 shader->selector->info.writes_samplemask +
5734 1 /* SampleMaskIn */;
5735
5736 num_returns = MAX2(num_returns,
5737 num_return_sgprs +
5738 PS_EPILOG_SAMPLEMASK_MIN_LOC + 1);
5739
5740 for (i = 0; i < num_return_sgprs; i++)
5741 returns[i] = ctx->i32;
5742 for (; i < num_returns; i++)
5743 returns[i] = ctx->f32;
5744 }
5745 break;
5746
5747 case PIPE_SHADER_COMPUTE:
5748 params[SI_PARAM_GRID_SIZE] = v3i32;
5749 params[SI_PARAM_BLOCK_ID] = v3i32;
5750 last_sgpr = SI_PARAM_BLOCK_ID;
5751
5752 params[SI_PARAM_THREAD_ID] = v3i32;
5753 num_params = SI_PARAM_THREAD_ID + 1;
5754 break;
5755 default:
5756 assert(0 && "unimplemented shader");
5757 return;
5758 }
5759
5760 assert(num_params <= ARRAY_SIZE(params));
5761
5762 si_create_function(ctx, returns, num_returns, params,
5763 num_params, last_sgpr);
5764
5765 /* Reserve register locations for VGPR inputs the PS prolog may need. */
5766 if (ctx->type == PIPE_SHADER_FRAGMENT &&
5767 !ctx->is_monolithic) {
5768 radeon_llvm_add_attribute(ctx->radeon_bld.main_fn,
5769 "InitialPSInputAddr",
5770 S_0286D0_PERSP_SAMPLE_ENA(1) |
5771 S_0286D0_PERSP_CENTER_ENA(1) |
5772 S_0286D0_PERSP_CENTROID_ENA(1) |
5773 S_0286D0_LINEAR_SAMPLE_ENA(1) |
5774 S_0286D0_LINEAR_CENTER_ENA(1) |
5775 S_0286D0_LINEAR_CENTROID_ENA(1) |
5776 S_0286D0_FRONT_FACE_ENA(1) |
5777 S_0286D0_POS_FIXED_PT_ENA(1));
5778 } else if (ctx->type == PIPE_SHADER_COMPUTE) {
5779 const unsigned *properties = shader->selector->info.properties;
5780 unsigned max_work_group_size =
5781 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] *
5782 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT] *
5783 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH];
5784
5785 assert(max_work_group_size);
5786
5787 radeon_llvm_add_attribute(ctx->radeon_bld.main_fn,
5788 "amdgpu-max-work-group-size",
5789 max_work_group_size);
5790 }
5791
5792 shader->info.num_input_sgprs = 0;
5793 shader->info.num_input_vgprs = 0;
5794
5795 for (i = 0; i <= last_sgpr; ++i)
5796 shader->info.num_input_sgprs += llvm_get_type_size(params[i]) / 4;
5797
5798 /* Unused fragment shader inputs are eliminated by the compiler,
5799 * so we don't know yet how many there will be.
5800 */
5801 if (ctx->type != PIPE_SHADER_FRAGMENT)
5802 for (; i < num_params; ++i)
5803 shader->info.num_input_vgprs += llvm_get_type_size(params[i]) / 4;
5804
5805 if (bld_base->info &&
5806 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
5807 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0 ||
5808 bld_base->info->opcode_count[TGSI_OPCODE_DDX_FINE] > 0 ||
5809 bld_base->info->opcode_count[TGSI_OPCODE_DDY_FINE] > 0 ||
5810 bld_base->info->opcode_count[TGSI_OPCODE_INTERP_OFFSET] > 0 ||
5811 bld_base->info->opcode_count[TGSI_OPCODE_INTERP_SAMPLE] > 0))
5812 ctx->lds =
5813 LLVMAddGlobalInAddressSpace(gallivm->module,
5814 LLVMArrayType(ctx->i32, 64),
5815 "ddxy_lds",
5816 LOCAL_ADDR_SPACE);
5817
5818 if ((ctx->type == PIPE_SHADER_VERTEX && shader->key.vs.as_ls) ||
5819 ctx->type == PIPE_SHADER_TESS_CTRL ||
5820 ctx->type == PIPE_SHADER_TESS_EVAL)
5821 declare_tess_lds(ctx);
5822 }
5823
5824 /**
5825 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
5826 * for later use.
5827 */
5828 static void preload_ring_buffers(struct si_shader_context *ctx)
5829 {
5830 struct gallivm_state *gallivm =
5831 ctx->radeon_bld.soa.bld_base.base.gallivm;
5832
5833 LLVMValueRef buf_ptr = LLVMGetParam(ctx->radeon_bld.main_fn,
5834 SI_PARAM_RW_BUFFERS);
5835
5836 if ((ctx->type == PIPE_SHADER_VERTEX &&
5837 ctx->shader->key.vs.as_es) ||
5838 (ctx->type == PIPE_SHADER_TESS_EVAL &&
5839 ctx->shader->key.tes.as_es) ||
5840 ctx->type == PIPE_SHADER_GEOMETRY) {
5841 unsigned ring =
5842 ctx->type == PIPE_SHADER_GEOMETRY ? SI_GS_RING_ESGS
5843 : SI_ES_RING_ESGS;
5844 LLVMValueRef offset = lp_build_const_int32(gallivm, ring);
5845
5846 ctx->esgs_ring =
5847 build_indexed_load_const(ctx, buf_ptr, offset);
5848 }
5849
5850 if (ctx->is_gs_copy_shader) {
5851 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_VS_RING_GSVS);
5852
5853 ctx->gsvs_ring[0] =
5854 build_indexed_load_const(ctx, buf_ptr, offset);
5855 }
5856 if (ctx->type == PIPE_SHADER_GEOMETRY) {
5857 int i;
5858 for (i = 0; i < 4; i++) {
5859 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_GS_RING_GSVS0 + i);
5860
5861 ctx->gsvs_ring[i] =
5862 build_indexed_load_const(ctx, buf_ptr, offset);
5863 }
5864 }
5865 }
5866
5867 static void si_llvm_emit_polygon_stipple(struct si_shader_context *ctx,
5868 LLVMValueRef param_rw_buffers,
5869 unsigned param_pos_fixed_pt)
5870 {
5871 struct lp_build_tgsi_context *bld_base =
5872 &ctx->radeon_bld.soa.bld_base;
5873 struct gallivm_state *gallivm = bld_base->base.gallivm;
5874 LLVMBuilderRef builder = gallivm->builder;
5875 LLVMValueRef slot, desc, offset, row, bit, address[2];
5876
5877 /* Use the fixed-point gl_FragCoord input.
5878 * Since the stipple pattern is 32x32 and it repeats, just get 5 bits
5879 * per coordinate to get the repeating effect.
5880 */
5881 address[0] = unpack_param(ctx, param_pos_fixed_pt, 0, 5);
5882 address[1] = unpack_param(ctx, param_pos_fixed_pt, 16, 5);
5883
5884 /* Load the buffer descriptor. */
5885 slot = lp_build_const_int32(gallivm, SI_PS_CONST_POLY_STIPPLE);
5886 desc = build_indexed_load_const(ctx, param_rw_buffers, slot);
5887
5888 /* The stipple pattern is 32x32, each row has 32 bits. */
5889 offset = LLVMBuildMul(builder, address[1],
5890 LLVMConstInt(ctx->i32, 4, 0), "");
5891 row = buffer_load_const(ctx, desc, offset);
5892 row = LLVMBuildBitCast(builder, row, ctx->i32, "");
5893 bit = LLVMBuildLShr(builder, row, address[0], "");
5894 bit = LLVMBuildTrunc(builder, bit, ctx->i1, "");
5895
5896 /* The intrinsic kills the thread if arg < 0. */
5897 bit = LLVMBuildSelect(builder, bit, LLVMConstReal(ctx->f32, 0),
5898 LLVMConstReal(ctx->f32, -1), "");
5899 lp_build_intrinsic(builder, "llvm.AMDGPU.kill", ctx->voidt, &bit, 1, 0);
5900 }
5901
5902 void si_shader_binary_read_config(struct radeon_shader_binary *binary,
5903 struct si_shader_config *conf,
5904 unsigned symbol_offset)
5905 {
5906 unsigned i;
5907 const unsigned char *config =
5908 radeon_shader_binary_config_start(binary, symbol_offset);
5909 bool really_needs_scratch = false;
5910
5911 /* LLVM adds SGPR spills to the scratch size.
5912 * Find out if we really need the scratch buffer.
5913 */
5914 for (i = 0; i < binary->reloc_count; i++) {
5915 const struct radeon_shader_reloc *reloc = &binary->relocs[i];
5916
5917 if (!strcmp(scratch_rsrc_dword0_symbol, reloc->name) ||
5918 !strcmp(scratch_rsrc_dword1_symbol, reloc->name)) {
5919 really_needs_scratch = true;
5920 break;
5921 }
5922 }
5923
5924 /* XXX: We may be able to emit some of these values directly rather than
5925 * extracting fields to be emitted later.
5926 */
5927
5928 for (i = 0; i < binary->config_size_per_symbol; i+= 8) {
5929 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
5930 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
5931 switch (reg) {
5932 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
5933 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
5934 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
5935 case R_00B848_COMPUTE_PGM_RSRC1:
5936 conf->num_sgprs = MAX2(conf->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
5937 conf->num_vgprs = MAX2(conf->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
5938 conf->float_mode = G_00B028_FLOAT_MODE(value);
5939 conf->rsrc1 = value;
5940 break;
5941 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
5942 conf->lds_size = MAX2(conf->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
5943 break;
5944 case R_00B84C_COMPUTE_PGM_RSRC2:
5945 conf->lds_size = MAX2(conf->lds_size, G_00B84C_LDS_SIZE(value));
5946 conf->rsrc2 = value;
5947 break;
5948 case R_0286CC_SPI_PS_INPUT_ENA:
5949 conf->spi_ps_input_ena = value;
5950 break;
5951 case R_0286D0_SPI_PS_INPUT_ADDR:
5952 conf->spi_ps_input_addr = value;
5953 break;
5954 case R_0286E8_SPI_TMPRING_SIZE:
5955 case R_00B860_COMPUTE_TMPRING_SIZE:
5956 /* WAVESIZE is in units of 256 dwords. */
5957 if (really_needs_scratch)
5958 conf->scratch_bytes_per_wave =
5959 G_00B860_WAVESIZE(value) * 256 * 4;
5960 break;
5961 case 0x4: /* SPILLED_SGPRS */
5962 conf->spilled_sgprs = value;
5963 break;
5964 case 0x8: /* SPILLED_VGPRS */
5965 conf->spilled_vgprs = value;
5966 break;
5967 default:
5968 {
5969 static bool printed;
5970
5971 if (!printed) {
5972 fprintf(stderr, "Warning: LLVM emitted unknown "
5973 "config register: 0x%x\n", reg);
5974 printed = true;
5975 }
5976 }
5977 break;
5978 }
5979 }
5980
5981 if (!conf->spi_ps_input_addr)
5982 conf->spi_ps_input_addr = conf->spi_ps_input_ena;
5983 }
5984
5985 void si_shader_apply_scratch_relocs(struct si_context *sctx,
5986 struct si_shader *shader,
5987 struct si_shader_config *config,
5988 uint64_t scratch_va)
5989 {
5990 unsigned i;
5991 uint32_t scratch_rsrc_dword0 = scratch_va;
5992 uint32_t scratch_rsrc_dword1 =
5993 S_008F04_BASE_ADDRESS_HI(scratch_va >> 32);
5994
5995 /* Enable scratch coalescing if LLVM sets ELEMENT_SIZE & INDEX_STRIDE
5996 * correctly.
5997 */
5998 if (HAVE_LLVM >= 0x0309)
5999 scratch_rsrc_dword1 |= S_008F04_SWIZZLE_ENABLE(1);
6000 else
6001 scratch_rsrc_dword1 |=
6002 S_008F04_STRIDE(config->scratch_bytes_per_wave / 64);
6003
6004 for (i = 0 ; i < shader->binary.reloc_count; i++) {
6005 const struct radeon_shader_reloc *reloc =
6006 &shader->binary.relocs[i];
6007 if (!strcmp(scratch_rsrc_dword0_symbol, reloc->name)) {
6008 util_memcpy_cpu_to_le32(shader->binary.code + reloc->offset,
6009 &scratch_rsrc_dword0, 4);
6010 } else if (!strcmp(scratch_rsrc_dword1_symbol, reloc->name)) {
6011 util_memcpy_cpu_to_le32(shader->binary.code + reloc->offset,
6012 &scratch_rsrc_dword1, 4);
6013 }
6014 }
6015 }
6016
6017 static unsigned si_get_shader_binary_size(struct si_shader *shader)
6018 {
6019 unsigned size = shader->binary.code_size;
6020
6021 if (shader->prolog)
6022 size += shader->prolog->binary.code_size;
6023 if (shader->epilog)
6024 size += shader->epilog->binary.code_size;
6025 return size;
6026 }
6027
6028 int si_shader_binary_upload(struct si_screen *sscreen, struct si_shader *shader)
6029 {
6030 const struct radeon_shader_binary *prolog =
6031 shader->prolog ? &shader->prolog->binary : NULL;
6032 const struct radeon_shader_binary *epilog =
6033 shader->epilog ? &shader->epilog->binary : NULL;
6034 const struct radeon_shader_binary *mainb = &shader->binary;
6035 unsigned bo_size = si_get_shader_binary_size(shader) +
6036 (!epilog ? mainb->rodata_size : 0);
6037 unsigned char *ptr;
6038
6039 assert(!prolog || !prolog->rodata_size);
6040 assert((!prolog && !epilog) || !mainb->rodata_size);
6041 assert(!epilog || !epilog->rodata_size);
6042
6043 r600_resource_reference(&shader->bo, NULL);
6044 shader->bo = si_resource_create_custom(&sscreen->b.b,
6045 PIPE_USAGE_IMMUTABLE,
6046 bo_size);
6047 if (!shader->bo)
6048 return -ENOMEM;
6049
6050 /* Upload. */
6051 ptr = sscreen->b.ws->buffer_map(shader->bo->buf, NULL,
6052 PIPE_TRANSFER_READ_WRITE);
6053
6054 if (prolog) {
6055 util_memcpy_cpu_to_le32(ptr, prolog->code, prolog->code_size);
6056 ptr += prolog->code_size;
6057 }
6058
6059 util_memcpy_cpu_to_le32(ptr, mainb->code, mainb->code_size);
6060 ptr += mainb->code_size;
6061
6062 if (epilog)
6063 util_memcpy_cpu_to_le32(ptr, epilog->code, epilog->code_size);
6064 else if (mainb->rodata_size > 0)
6065 util_memcpy_cpu_to_le32(ptr, mainb->rodata, mainb->rodata_size);
6066
6067 sscreen->b.ws->buffer_unmap(shader->bo->buf);
6068 return 0;
6069 }
6070
6071 static void si_shader_dump_disassembly(const struct radeon_shader_binary *binary,
6072 struct pipe_debug_callback *debug,
6073 const char *name, FILE *file)
6074 {
6075 char *line, *p;
6076 unsigned i, count;
6077
6078 if (binary->disasm_string) {
6079 fprintf(file, "Shader %s disassembly:\n", name);
6080 fprintf(file, "%s", binary->disasm_string);
6081
6082 if (debug && debug->debug_message) {
6083 /* Very long debug messages are cut off, so send the
6084 * disassembly one line at a time. This causes more
6085 * overhead, but on the plus side it simplifies
6086 * parsing of resulting logs.
6087 */
6088 pipe_debug_message(debug, SHADER_INFO,
6089 "Shader Disassembly Begin");
6090
6091 line = binary->disasm_string;
6092 while (*line) {
6093 p = util_strchrnul(line, '\n');
6094 count = p - line;
6095
6096 if (count) {
6097 pipe_debug_message(debug, SHADER_INFO,
6098 "%.*s", count, line);
6099 }
6100
6101 if (!*p)
6102 break;
6103 line = p + 1;
6104 }
6105
6106 pipe_debug_message(debug, SHADER_INFO,
6107 "Shader Disassembly End");
6108 }
6109 } else {
6110 fprintf(file, "Shader %s binary:\n", name);
6111 for (i = 0; i < binary->code_size; i += 4) {
6112 fprintf(file, "@0x%x: %02x%02x%02x%02x\n", i,
6113 binary->code[i + 3], binary->code[i + 2],
6114 binary->code[i + 1], binary->code[i]);
6115 }
6116 }
6117 }
6118
6119 static void si_shader_dump_stats(struct si_screen *sscreen,
6120 struct si_shader_config *conf,
6121 unsigned num_inputs,
6122 unsigned code_size,
6123 struct pipe_debug_callback *debug,
6124 unsigned processor,
6125 FILE *file)
6126 {
6127 unsigned lds_increment = sscreen->b.chip_class >= CIK ? 512 : 256;
6128 unsigned lds_per_wave = 0;
6129 unsigned max_simd_waves = 10;
6130
6131 /* Compute LDS usage for PS. */
6132 if (processor == PIPE_SHADER_FRAGMENT) {
6133 /* The minimum usage per wave is (num_inputs * 48). The maximum
6134 * usage is (num_inputs * 48 * 16).
6135 * We can get anything in between and it varies between waves.
6136 *
6137 * The 48 bytes per input for a single primitive is equal to
6138 * 4 bytes/component * 4 components/input * 3 points.
6139 *
6140 * Other stages don't know the size at compile time or don't
6141 * allocate LDS per wave, but instead they do it per thread group.
6142 */
6143 lds_per_wave = conf->lds_size * lds_increment +
6144 align(num_inputs * 48, lds_increment);
6145 }
6146
6147 /* Compute the per-SIMD wave counts. */
6148 if (conf->num_sgprs) {
6149 if (sscreen->b.chip_class >= VI)
6150 max_simd_waves = MIN2(max_simd_waves, 800 / conf->num_sgprs);
6151 else
6152 max_simd_waves = MIN2(max_simd_waves, 512 / conf->num_sgprs);
6153 }
6154
6155 if (conf->num_vgprs)
6156 max_simd_waves = MIN2(max_simd_waves, 256 / conf->num_vgprs);
6157
6158 /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD
6159 * that PS can use.
6160 */
6161 if (lds_per_wave)
6162 max_simd_waves = MIN2(max_simd_waves, 16384 / lds_per_wave);
6163
6164 if (file != stderr ||
6165 r600_can_dump_shader(&sscreen->b, processor)) {
6166 if (processor == PIPE_SHADER_FRAGMENT) {
6167 fprintf(file, "*** SHADER CONFIG ***\n"
6168 "SPI_PS_INPUT_ADDR = 0x%04x\n"
6169 "SPI_PS_INPUT_ENA = 0x%04x\n",
6170 conf->spi_ps_input_addr, conf->spi_ps_input_ena);
6171 }
6172
6173 fprintf(file, "*** SHADER STATS ***\n"
6174 "SGPRS: %d\n"
6175 "VGPRS: %d\n"
6176 "Spilled SGPRs: %d\n"
6177 "Spilled VGPRs: %d\n"
6178 "Code Size: %d bytes\n"
6179 "LDS: %d blocks\n"
6180 "Scratch: %d bytes per wave\n"
6181 "Max Waves: %d\n"
6182 "********************\n\n\n",
6183 conf->num_sgprs, conf->num_vgprs,
6184 conf->spilled_sgprs, conf->spilled_vgprs, code_size,
6185 conf->lds_size, conf->scratch_bytes_per_wave,
6186 max_simd_waves);
6187 }
6188
6189 pipe_debug_message(debug, SHADER_INFO,
6190 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
6191 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
6192 "Spilled VGPRs: %d",
6193 conf->num_sgprs, conf->num_vgprs, code_size,
6194 conf->lds_size, conf->scratch_bytes_per_wave,
6195 max_simd_waves, conf->spilled_sgprs,
6196 conf->spilled_vgprs);
6197 }
6198
6199 static const char *si_get_shader_name(struct si_shader *shader,
6200 unsigned processor)
6201 {
6202 switch (processor) {
6203 case PIPE_SHADER_VERTEX:
6204 if (shader->key.vs.as_es)
6205 return "Vertex Shader as ES";
6206 else if (shader->key.vs.as_ls)
6207 return "Vertex Shader as LS";
6208 else
6209 return "Vertex Shader as VS";
6210 case PIPE_SHADER_TESS_CTRL:
6211 return "Tessellation Control Shader";
6212 case PIPE_SHADER_TESS_EVAL:
6213 if (shader->key.tes.as_es)
6214 return "Tessellation Evaluation Shader as ES";
6215 else
6216 return "Tessellation Evaluation Shader as VS";
6217 case PIPE_SHADER_GEOMETRY:
6218 if (shader->gs_copy_shader == NULL)
6219 return "GS Copy Shader as VS";
6220 else
6221 return "Geometry Shader";
6222 case PIPE_SHADER_FRAGMENT:
6223 return "Pixel Shader";
6224 case PIPE_SHADER_COMPUTE:
6225 return "Compute Shader";
6226 default:
6227 return "Unknown Shader";
6228 }
6229 }
6230
6231 void si_shader_dump(struct si_screen *sscreen, struct si_shader *shader,
6232 struct pipe_debug_callback *debug, unsigned processor,
6233 FILE *file)
6234 {
6235 if (file != stderr ||
6236 r600_can_dump_shader(&sscreen->b, processor))
6237 si_dump_shader_key(processor, &shader->key, file);
6238
6239 if (file != stderr && shader->binary.llvm_ir_string) {
6240 fprintf(file, "\n%s - main shader part - LLVM IR:\n\n",
6241 si_get_shader_name(shader, processor));
6242 fprintf(file, "%s\n", shader->binary.llvm_ir_string);
6243 }
6244
6245 if (file != stderr ||
6246 (r600_can_dump_shader(&sscreen->b, processor) &&
6247 !(sscreen->b.debug_flags & DBG_NO_ASM))) {
6248 fprintf(file, "\n%s:\n", si_get_shader_name(shader, processor));
6249
6250 if (shader->prolog)
6251 si_shader_dump_disassembly(&shader->prolog->binary,
6252 debug, "prolog", file);
6253
6254 si_shader_dump_disassembly(&shader->binary, debug, "main", file);
6255
6256 if (shader->epilog)
6257 si_shader_dump_disassembly(&shader->epilog->binary,
6258 debug, "epilog", file);
6259 fprintf(file, "\n");
6260 }
6261
6262 si_shader_dump_stats(sscreen, &shader->config,
6263 shader->selector ? shader->selector->info.num_inputs : 0,
6264 si_get_shader_binary_size(shader), debug, processor,
6265 file);
6266 }
6267
6268 int si_compile_llvm(struct si_screen *sscreen,
6269 struct radeon_shader_binary *binary,
6270 struct si_shader_config *conf,
6271 LLVMTargetMachineRef tm,
6272 LLVMModuleRef mod,
6273 struct pipe_debug_callback *debug,
6274 unsigned processor,
6275 const char *name)
6276 {
6277 int r = 0;
6278 unsigned count = p_atomic_inc_return(&sscreen->b.num_compilations);
6279
6280 if (r600_can_dump_shader(&sscreen->b, processor)) {
6281 fprintf(stderr, "radeonsi: Compiling shader %d\n", count);
6282
6283 if (!(sscreen->b.debug_flags & (DBG_NO_IR | DBG_PREOPT_IR))) {
6284 fprintf(stderr, "%s LLVM IR:\n\n", name);
6285 LLVMDumpModule(mod);
6286 fprintf(stderr, "\n");
6287 }
6288 }
6289
6290 if (sscreen->record_llvm_ir) {
6291 char *ir = LLVMPrintModuleToString(mod);
6292 binary->llvm_ir_string = strdup(ir);
6293 LLVMDisposeMessage(ir);
6294 }
6295
6296 if (!si_replace_shader(count, binary)) {
6297 r = radeon_llvm_compile(mod, binary, tm, debug);
6298 if (r)
6299 return r;
6300 }
6301
6302 si_shader_binary_read_config(binary, conf, 0);
6303
6304 /* Enable 64-bit and 16-bit denormals, because there is no performance
6305 * cost.
6306 *
6307 * If denormals are enabled, all floating-point output modifiers are
6308 * ignored.
6309 *
6310 * Don't enable denormals for 32-bit floats, because:
6311 * - Floating-point output modifiers would be ignored by the hw.
6312 * - Some opcodes don't support denormals, such as v_mad_f32. We would
6313 * have to stop using those.
6314 * - SI & CI would be very slow.
6315 */
6316 conf->float_mode |= V_00B028_FP_64_DENORMS;
6317
6318 FREE(binary->config);
6319 FREE(binary->global_symbol_offsets);
6320 binary->config = NULL;
6321 binary->global_symbol_offsets = NULL;
6322
6323 /* Some shaders can't have rodata because their binaries can be
6324 * concatenated.
6325 */
6326 if (binary->rodata_size &&
6327 (processor == PIPE_SHADER_VERTEX ||
6328 processor == PIPE_SHADER_TESS_CTRL ||
6329 processor == PIPE_SHADER_TESS_EVAL ||
6330 processor == PIPE_SHADER_FRAGMENT)) {
6331 fprintf(stderr, "radeonsi: The shader can't have rodata.");
6332 return -EINVAL;
6333 }
6334
6335 return r;
6336 }
6337
6338 static void si_llvm_build_ret(struct si_shader_context *ctx, LLVMValueRef ret)
6339 {
6340 if (LLVMGetTypeKind(LLVMTypeOf(ret)) == LLVMVoidTypeKind)
6341 LLVMBuildRetVoid(ctx->radeon_bld.gallivm.builder);
6342 else
6343 LLVMBuildRet(ctx->radeon_bld.gallivm.builder, ret);
6344 }
6345
6346 /* Generate code for the hardware VS shader stage to go with a geometry shader */
6347 static int si_generate_gs_copy_shader(struct si_screen *sscreen,
6348 struct si_shader_context *ctx,
6349 struct si_shader *gs,
6350 struct pipe_debug_callback *debug)
6351 {
6352 struct gallivm_state *gallivm = &ctx->radeon_bld.gallivm;
6353 struct lp_build_tgsi_context *bld_base = &ctx->radeon_bld.soa.bld_base;
6354 struct lp_build_context *uint = &bld_base->uint_bld;
6355 struct si_shader_output_values *outputs;
6356 struct tgsi_shader_info *gsinfo = &gs->selector->info;
6357 LLVMValueRef args[9];
6358 int i, r;
6359
6360 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
6361
6362 si_init_shader_ctx(ctx, sscreen, ctx->shader, ctx->tm);
6363 ctx->type = PIPE_SHADER_VERTEX;
6364 ctx->is_gs_copy_shader = true;
6365
6366 create_meta_data(ctx);
6367 create_function(ctx);
6368 preload_ring_buffers(ctx);
6369
6370 args[0] = ctx->gsvs_ring[0];
6371 args[1] = lp_build_mul_imm(uint,
6372 LLVMGetParam(ctx->radeon_bld.main_fn,
6373 ctx->param_vertex_id),
6374 4);
6375 args[3] = uint->zero;
6376 args[4] = uint->one; /* OFFEN */
6377 args[5] = uint->zero; /* IDXEN */
6378 args[6] = uint->one; /* GLC */
6379 args[7] = uint->one; /* SLC */
6380 args[8] = uint->zero; /* TFE */
6381
6382 /* Fetch vertex data from GSVS ring */
6383 for (i = 0; i < gsinfo->num_outputs; ++i) {
6384 unsigned chan;
6385
6386 outputs[i].name = gsinfo->output_semantic_name[i];
6387 outputs[i].sid = gsinfo->output_semantic_index[i];
6388
6389 for (chan = 0; chan < 4; chan++) {
6390 args[2] = lp_build_const_int32(gallivm,
6391 (i * 4 + chan) *
6392 gs->selector->gs_max_out_vertices * 16 * 4);
6393
6394 outputs[i].values[chan] =
6395 LLVMBuildBitCast(gallivm->builder,
6396 lp_build_intrinsic(gallivm->builder,
6397 "llvm.SI.buffer.load.dword.i32.i32",
6398 ctx->i32, args, 9,
6399 LLVMReadOnlyAttribute),
6400 ctx->f32, "");
6401 }
6402 }
6403
6404 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
6405
6406 LLVMBuildRetVoid(gallivm->builder);
6407
6408 /* Dump LLVM IR before any optimization passes */
6409 if (sscreen->b.debug_flags & DBG_PREOPT_IR &&
6410 r600_can_dump_shader(&sscreen->b, PIPE_SHADER_GEOMETRY))
6411 LLVMDumpModule(bld_base->base.gallivm->module);
6412
6413 radeon_llvm_finalize_module(&ctx->radeon_bld);
6414
6415 r = si_compile_llvm(sscreen, &ctx->shader->binary,
6416 &ctx->shader->config, ctx->tm,
6417 bld_base->base.gallivm->module,
6418 debug, PIPE_SHADER_GEOMETRY,
6419 "GS Copy Shader");
6420 if (!r) {
6421 if (r600_can_dump_shader(&sscreen->b, PIPE_SHADER_GEOMETRY))
6422 fprintf(stderr, "GS Copy Shader:\n");
6423 si_shader_dump(sscreen, ctx->shader, debug,
6424 PIPE_SHADER_GEOMETRY, stderr);
6425 r = si_shader_binary_upload(sscreen, ctx->shader);
6426 }
6427
6428 radeon_llvm_dispose(&ctx->radeon_bld);
6429
6430 FREE(outputs);
6431 return r;
6432 }
6433
6434 static void si_dump_shader_key(unsigned shader, union si_shader_key *key,
6435 FILE *f)
6436 {
6437 int i;
6438
6439 fprintf(f, "SHADER KEY\n");
6440
6441 switch (shader) {
6442 case PIPE_SHADER_VERTEX:
6443 fprintf(f, " instance_divisors = {");
6444 for (i = 0; i < ARRAY_SIZE(key->vs.prolog.instance_divisors); i++)
6445 fprintf(f, !i ? "%u" : ", %u",
6446 key->vs.prolog.instance_divisors[i]);
6447 fprintf(f, "}\n");
6448 fprintf(f, " as_es = %u\n", key->vs.as_es);
6449 fprintf(f, " as_ls = %u\n", key->vs.as_ls);
6450 fprintf(f, " export_prim_id = %u\n", key->vs.epilog.export_prim_id);
6451 break;
6452
6453 case PIPE_SHADER_TESS_CTRL:
6454 fprintf(f, " prim_mode = %u\n", key->tcs.epilog.prim_mode);
6455 break;
6456
6457 case PIPE_SHADER_TESS_EVAL:
6458 fprintf(f, " as_es = %u\n", key->tes.as_es);
6459 fprintf(f, " export_prim_id = %u\n", key->tes.epilog.export_prim_id);
6460 break;
6461
6462 case PIPE_SHADER_GEOMETRY:
6463 case PIPE_SHADER_COMPUTE:
6464 break;
6465
6466 case PIPE_SHADER_FRAGMENT:
6467 fprintf(f, " prolog.color_two_side = %u\n", key->ps.prolog.color_two_side);
6468 fprintf(f, " prolog.flatshade_colors = %u\n", key->ps.prolog.flatshade_colors);
6469 fprintf(f, " prolog.poly_stipple = %u\n", key->ps.prolog.poly_stipple);
6470 fprintf(f, " prolog.force_persp_sample_interp = %u\n", key->ps.prolog.force_persp_sample_interp);
6471 fprintf(f, " prolog.force_linear_sample_interp = %u\n", key->ps.prolog.force_linear_sample_interp);
6472 fprintf(f, " prolog.force_persp_center_interp = %u\n", key->ps.prolog.force_persp_center_interp);
6473 fprintf(f, " prolog.force_linear_center_interp = %u\n", key->ps.prolog.force_linear_center_interp);
6474 fprintf(f, " prolog.bc_optimize_for_persp = %u\n", key->ps.prolog.bc_optimize_for_persp);
6475 fprintf(f, " prolog.bc_optimize_for_linear = %u\n", key->ps.prolog.bc_optimize_for_linear);
6476 fprintf(f, " epilog.spi_shader_col_format = 0x%x\n", key->ps.epilog.spi_shader_col_format);
6477 fprintf(f, " epilog.color_is_int8 = 0x%X\n", key->ps.epilog.color_is_int8);
6478 fprintf(f, " epilog.last_cbuf = %u\n", key->ps.epilog.last_cbuf);
6479 fprintf(f, " epilog.alpha_func = %u\n", key->ps.epilog.alpha_func);
6480 fprintf(f, " epilog.alpha_to_one = %u\n", key->ps.epilog.alpha_to_one);
6481 fprintf(f, " epilog.poly_line_smoothing = %u\n", key->ps.epilog.poly_line_smoothing);
6482 fprintf(f, " epilog.clamp_color = %u\n", key->ps.epilog.clamp_color);
6483 break;
6484
6485 default:
6486 assert(0);
6487 }
6488 }
6489
6490 static void si_init_shader_ctx(struct si_shader_context *ctx,
6491 struct si_screen *sscreen,
6492 struct si_shader *shader,
6493 LLVMTargetMachineRef tm)
6494 {
6495 struct lp_build_tgsi_context *bld_base;
6496 struct lp_build_tgsi_action tmpl = {};
6497
6498 memset(ctx, 0, sizeof(*ctx));
6499 radeon_llvm_context_init(
6500 &ctx->radeon_bld, "amdgcn--",
6501 (shader && shader->selector) ? &shader->selector->info : NULL,
6502 (shader && shader->selector) ? shader->selector->tokens : NULL);
6503 ctx->tm = tm;
6504 ctx->screen = sscreen;
6505 if (shader && shader->selector)
6506 ctx->type = shader->selector->info.processor;
6507 else
6508 ctx->type = -1;
6509 ctx->shader = shader;
6510
6511 ctx->voidt = LLVMVoidTypeInContext(ctx->radeon_bld.gallivm.context);
6512 ctx->i1 = LLVMInt1TypeInContext(ctx->radeon_bld.gallivm.context);
6513 ctx->i8 = LLVMInt8TypeInContext(ctx->radeon_bld.gallivm.context);
6514 ctx->i32 = LLVMInt32TypeInContext(ctx->radeon_bld.gallivm.context);
6515 ctx->i64 = LLVMInt64TypeInContext(ctx->radeon_bld.gallivm.context);
6516 ctx->i128 = LLVMIntTypeInContext(ctx->radeon_bld.gallivm.context, 128);
6517 ctx->f32 = LLVMFloatTypeInContext(ctx->radeon_bld.gallivm.context);
6518 ctx->v16i8 = LLVMVectorType(ctx->i8, 16);
6519 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
6520 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
6521 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
6522 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
6523
6524 bld_base = &ctx->radeon_bld.soa.bld_base;
6525 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
6526
6527 bld_base->op_actions[TGSI_OPCODE_INTERP_CENTROID] = interp_action;
6528 bld_base->op_actions[TGSI_OPCODE_INTERP_SAMPLE] = interp_action;
6529 bld_base->op_actions[TGSI_OPCODE_INTERP_OFFSET] = interp_action;
6530
6531 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
6532 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
6533 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
6534 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
6535 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
6536 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
6537 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
6538 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
6539 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
6540 bld_base->op_actions[TGSI_OPCODE_TXQ].fetch_args = txq_fetch_args;
6541 bld_base->op_actions[TGSI_OPCODE_TXQ].emit = txq_emit;
6542 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
6543 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
6544 bld_base->op_actions[TGSI_OPCODE_TXQS].emit = si_llvm_emit_txqs;
6545
6546 bld_base->op_actions[TGSI_OPCODE_LOAD].fetch_args = load_fetch_args;
6547 bld_base->op_actions[TGSI_OPCODE_LOAD].emit = load_emit;
6548 bld_base->op_actions[TGSI_OPCODE_STORE].fetch_args = store_fetch_args;
6549 bld_base->op_actions[TGSI_OPCODE_STORE].emit = store_emit;
6550 bld_base->op_actions[TGSI_OPCODE_RESQ].fetch_args = resq_fetch_args;
6551 bld_base->op_actions[TGSI_OPCODE_RESQ].emit = resq_emit;
6552
6553 tmpl.fetch_args = atomic_fetch_args;
6554 tmpl.emit = atomic_emit;
6555 bld_base->op_actions[TGSI_OPCODE_ATOMUADD] = tmpl;
6556 bld_base->op_actions[TGSI_OPCODE_ATOMUADD].intr_name = "add";
6557 bld_base->op_actions[TGSI_OPCODE_ATOMXCHG] = tmpl;
6558 bld_base->op_actions[TGSI_OPCODE_ATOMXCHG].intr_name = "swap";
6559 bld_base->op_actions[TGSI_OPCODE_ATOMCAS] = tmpl;
6560 bld_base->op_actions[TGSI_OPCODE_ATOMCAS].intr_name = "cmpswap";
6561 bld_base->op_actions[TGSI_OPCODE_ATOMAND] = tmpl;
6562 bld_base->op_actions[TGSI_OPCODE_ATOMAND].intr_name = "and";
6563 bld_base->op_actions[TGSI_OPCODE_ATOMOR] = tmpl;
6564 bld_base->op_actions[TGSI_OPCODE_ATOMOR].intr_name = "or";
6565 bld_base->op_actions[TGSI_OPCODE_ATOMXOR] = tmpl;
6566 bld_base->op_actions[TGSI_OPCODE_ATOMXOR].intr_name = "xor";
6567 bld_base->op_actions[TGSI_OPCODE_ATOMUMIN] = tmpl;
6568 bld_base->op_actions[TGSI_OPCODE_ATOMUMIN].intr_name = "umin";
6569 bld_base->op_actions[TGSI_OPCODE_ATOMUMAX] = tmpl;
6570 bld_base->op_actions[TGSI_OPCODE_ATOMUMAX].intr_name = "umax";
6571 bld_base->op_actions[TGSI_OPCODE_ATOMIMIN] = tmpl;
6572 bld_base->op_actions[TGSI_OPCODE_ATOMIMIN].intr_name = "smin";
6573 bld_base->op_actions[TGSI_OPCODE_ATOMIMAX] = tmpl;
6574 bld_base->op_actions[TGSI_OPCODE_ATOMIMAX].intr_name = "smax";
6575
6576 bld_base->op_actions[TGSI_OPCODE_MEMBAR].emit = membar_emit;
6577
6578 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
6579 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
6580 bld_base->op_actions[TGSI_OPCODE_DDX_FINE].emit = si_llvm_emit_ddxy;
6581 bld_base->op_actions[TGSI_OPCODE_DDY_FINE].emit = si_llvm_emit_ddxy;
6582
6583 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
6584 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
6585 bld_base->op_actions[TGSI_OPCODE_BARRIER].emit = si_llvm_emit_barrier;
6586
6587 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
6588 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
6589 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
6590 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
6591 }
6592
6593 int si_compile_tgsi_shader(struct si_screen *sscreen,
6594 LLVMTargetMachineRef tm,
6595 struct si_shader *shader,
6596 bool is_monolithic,
6597 struct pipe_debug_callback *debug)
6598 {
6599 struct si_shader_selector *sel = shader->selector;
6600 struct si_shader_context ctx;
6601 struct lp_build_tgsi_context *bld_base;
6602 LLVMModuleRef mod;
6603 int r = 0;
6604
6605 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
6606 * conversion fails. */
6607 if (r600_can_dump_shader(&sscreen->b, sel->info.processor) &&
6608 !(sscreen->b.debug_flags & DBG_NO_TGSI)) {
6609 tgsi_dump(sel->tokens, 0);
6610 si_dump_streamout(&sel->so);
6611 }
6612
6613 si_init_shader_ctx(&ctx, sscreen, shader, tm);
6614 ctx.is_monolithic = is_monolithic;
6615
6616 shader->info.uses_instanceid = sel->info.uses_instanceid;
6617
6618 bld_base = &ctx.radeon_bld.soa.bld_base;
6619 ctx.radeon_bld.load_system_value = declare_system_value;
6620
6621 switch (ctx.type) {
6622 case PIPE_SHADER_VERTEX:
6623 ctx.radeon_bld.load_input = declare_input_vs;
6624 if (shader->key.vs.as_ls)
6625 bld_base->emit_epilogue = si_llvm_emit_ls_epilogue;
6626 else if (shader->key.vs.as_es)
6627 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
6628 else
6629 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
6630 break;
6631 case PIPE_SHADER_TESS_CTRL:
6632 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_tcs;
6633 bld_base->emit_fetch_funcs[TGSI_FILE_OUTPUT] = fetch_output_tcs;
6634 bld_base->emit_store = store_output_tcs;
6635 bld_base->emit_epilogue = si_llvm_emit_tcs_epilogue;
6636 break;
6637 case PIPE_SHADER_TESS_EVAL:
6638 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_tes;
6639 if (shader->key.tes.as_es)
6640 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
6641 else
6642 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
6643 break;
6644 case PIPE_SHADER_GEOMETRY:
6645 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
6646 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
6647 break;
6648 case PIPE_SHADER_FRAGMENT:
6649 ctx.radeon_bld.load_input = declare_input_fs;
6650 if (is_monolithic)
6651 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
6652 else
6653 bld_base->emit_epilogue = si_llvm_return_fs_outputs;
6654 break;
6655 case PIPE_SHADER_COMPUTE:
6656 ctx.radeon_bld.declare_memory_region = declare_compute_memory;
6657 break;
6658 default:
6659 assert(!"Unsupported shader type");
6660 return -1;
6661 }
6662
6663 create_meta_data(&ctx);
6664 create_function(&ctx);
6665 preload_ring_buffers(&ctx);
6666
6667 if (ctx.is_monolithic && sel->type == PIPE_SHADER_FRAGMENT &&
6668 shader->key.ps.prolog.poly_stipple) {
6669 LLVMValueRef list = LLVMGetParam(ctx.radeon_bld.main_fn,
6670 SI_PARAM_RW_BUFFERS);
6671 si_llvm_emit_polygon_stipple(&ctx, list,
6672 SI_PARAM_POS_FIXED_PT);
6673 }
6674
6675 if (ctx.type == PIPE_SHADER_GEOMETRY) {
6676 int i;
6677 for (i = 0; i < 4; i++) {
6678 ctx.gs_next_vertex[i] =
6679 lp_build_alloca(bld_base->base.gallivm,
6680 ctx.i32, "");
6681 }
6682 }
6683
6684 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
6685 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
6686 goto out;
6687 }
6688
6689 si_llvm_build_ret(&ctx, ctx.return_value);
6690 mod = bld_base->base.gallivm->module;
6691
6692 /* Dump LLVM IR before any optimization passes */
6693 if (sscreen->b.debug_flags & DBG_PREOPT_IR &&
6694 r600_can_dump_shader(&sscreen->b, ctx.type))
6695 LLVMDumpModule(mod);
6696
6697 radeon_llvm_finalize_module(&ctx.radeon_bld);
6698
6699 r = si_compile_llvm(sscreen, &shader->binary, &shader->config, tm,
6700 mod, debug, ctx.type, "TGSI shader");
6701 if (r) {
6702 fprintf(stderr, "LLVM failed to compile shader\n");
6703 goto out;
6704 }
6705
6706 radeon_llvm_dispose(&ctx.radeon_bld);
6707
6708 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
6709 * LLVM 3.9svn has this bug.
6710 */
6711 if (sel->type == PIPE_SHADER_COMPUTE) {
6712 unsigned *props = sel->info.properties;
6713 unsigned wave_size = 64;
6714 unsigned max_vgprs = 256;
6715 unsigned max_sgprs = sscreen->b.chip_class >= VI ? 800 : 512;
6716 unsigned max_sgprs_per_wave = 128;
6717 unsigned min_waves_per_cu =
6718 DIV_ROUND_UP(props[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] *
6719 props[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT] *
6720 props[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH],
6721 wave_size);
6722 unsigned min_waves_per_simd = DIV_ROUND_UP(min_waves_per_cu, 4);
6723
6724 max_vgprs = max_vgprs / min_waves_per_simd;
6725 max_sgprs = MIN2(max_sgprs / min_waves_per_simd, max_sgprs_per_wave);
6726
6727 if (shader->config.num_sgprs > max_sgprs ||
6728 shader->config.num_vgprs > max_vgprs) {
6729 fprintf(stderr, "LLVM failed to compile a shader correctly: "
6730 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
6731 shader->config.num_sgprs, shader->config.num_vgprs,
6732 max_sgprs, max_vgprs);
6733
6734 /* Just terminate the process, because dependent
6735 * shaders can hang due to bad input data, but use
6736 * the env var to allow shader-db to work.
6737 */
6738 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
6739 abort();
6740 }
6741 }
6742
6743 /* Add the scratch offset to input SGPRs. */
6744 if (shader->config.scratch_bytes_per_wave)
6745 shader->info.num_input_sgprs += 1; /* scratch byte offset */
6746
6747 /* Calculate the number of fragment input VGPRs. */
6748 if (ctx.type == PIPE_SHADER_FRAGMENT) {
6749 shader->info.num_input_vgprs = 0;
6750 shader->info.face_vgpr_index = -1;
6751
6752 if (G_0286CC_PERSP_SAMPLE_ENA(shader->config.spi_ps_input_addr))
6753 shader->info.num_input_vgprs += 2;
6754 if (G_0286CC_PERSP_CENTER_ENA(shader->config.spi_ps_input_addr))
6755 shader->info.num_input_vgprs += 2;
6756 if (G_0286CC_PERSP_CENTROID_ENA(shader->config.spi_ps_input_addr))
6757 shader->info.num_input_vgprs += 2;
6758 if (G_0286CC_PERSP_PULL_MODEL_ENA(shader->config.spi_ps_input_addr))
6759 shader->info.num_input_vgprs += 3;
6760 if (G_0286CC_LINEAR_SAMPLE_ENA(shader->config.spi_ps_input_addr))
6761 shader->info.num_input_vgprs += 2;
6762 if (G_0286CC_LINEAR_CENTER_ENA(shader->config.spi_ps_input_addr))
6763 shader->info.num_input_vgprs += 2;
6764 if (G_0286CC_LINEAR_CENTROID_ENA(shader->config.spi_ps_input_addr))
6765 shader->info.num_input_vgprs += 2;
6766 if (G_0286CC_LINE_STIPPLE_TEX_ENA(shader->config.spi_ps_input_addr))
6767 shader->info.num_input_vgprs += 1;
6768 if (G_0286CC_POS_X_FLOAT_ENA(shader->config.spi_ps_input_addr))
6769 shader->info.num_input_vgprs += 1;
6770 if (G_0286CC_POS_Y_FLOAT_ENA(shader->config.spi_ps_input_addr))
6771 shader->info.num_input_vgprs += 1;
6772 if (G_0286CC_POS_Z_FLOAT_ENA(shader->config.spi_ps_input_addr))
6773 shader->info.num_input_vgprs += 1;
6774 if (G_0286CC_POS_W_FLOAT_ENA(shader->config.spi_ps_input_addr))
6775 shader->info.num_input_vgprs += 1;
6776 if (G_0286CC_FRONT_FACE_ENA(shader->config.spi_ps_input_addr)) {
6777 shader->info.face_vgpr_index = shader->info.num_input_vgprs;
6778 shader->info.num_input_vgprs += 1;
6779 }
6780 if (G_0286CC_ANCILLARY_ENA(shader->config.spi_ps_input_addr))
6781 shader->info.num_input_vgprs += 1;
6782 if (G_0286CC_SAMPLE_COVERAGE_ENA(shader->config.spi_ps_input_addr))
6783 shader->info.num_input_vgprs += 1;
6784 if (G_0286CC_POS_FIXED_PT_ENA(shader->config.spi_ps_input_addr))
6785 shader->info.num_input_vgprs += 1;
6786 }
6787
6788 if (ctx.type == PIPE_SHADER_GEOMETRY) {
6789 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
6790 shader->gs_copy_shader->selector = shader->selector;
6791 ctx.shader = shader->gs_copy_shader;
6792 if ((r = si_generate_gs_copy_shader(sscreen, &ctx,
6793 shader, debug))) {
6794 free(shader->gs_copy_shader);
6795 shader->gs_copy_shader = NULL;
6796 goto out;
6797 }
6798 }
6799
6800 out:
6801 return r;
6802 }
6803
6804 /**
6805 * Create, compile and return a shader part (prolog or epilog).
6806 *
6807 * \param sscreen screen
6808 * \param list list of shader parts of the same category
6809 * \param key shader part key
6810 * \param tm LLVM target machine
6811 * \param debug debug callback
6812 * \param compile the callback responsible for compilation
6813 * \return non-NULL on success
6814 */
6815 static struct si_shader_part *
6816 si_get_shader_part(struct si_screen *sscreen,
6817 struct si_shader_part **list,
6818 union si_shader_part_key *key,
6819 LLVMTargetMachineRef tm,
6820 struct pipe_debug_callback *debug,
6821 bool (*compile)(struct si_screen *,
6822 LLVMTargetMachineRef,
6823 struct pipe_debug_callback *,
6824 struct si_shader_part *))
6825 {
6826 struct si_shader_part *result;
6827
6828 pipe_mutex_lock(sscreen->shader_parts_mutex);
6829
6830 /* Find existing. */
6831 for (result = *list; result; result = result->next) {
6832 if (memcmp(&result->key, key, sizeof(*key)) == 0) {
6833 pipe_mutex_unlock(sscreen->shader_parts_mutex);
6834 return result;
6835 }
6836 }
6837
6838 /* Compile a new one. */
6839 result = CALLOC_STRUCT(si_shader_part);
6840 result->key = *key;
6841 if (!compile(sscreen, tm, debug, result)) {
6842 FREE(result);
6843 pipe_mutex_unlock(sscreen->shader_parts_mutex);
6844 return NULL;
6845 }
6846
6847 result->next = *list;
6848 *list = result;
6849 pipe_mutex_unlock(sscreen->shader_parts_mutex);
6850 return result;
6851 }
6852
6853 /**
6854 * Create a vertex shader prolog.
6855 *
6856 * The inputs are the same as VS (a lot of SGPRs and 4 VGPR system values).
6857 * All inputs are returned unmodified. The vertex load indices are
6858 * stored after them, which will used by the API VS for fetching inputs.
6859 *
6860 * For example, the expected outputs for instance_divisors[] = {0, 1, 2} are:
6861 * input_v0,
6862 * input_v1,
6863 * input_v2,
6864 * input_v3,
6865 * (VertexID + BaseVertex),
6866 * (InstanceID + StartInstance),
6867 * (InstanceID / 2 + StartInstance)
6868 */
6869 static bool si_compile_vs_prolog(struct si_screen *sscreen,
6870 LLVMTargetMachineRef tm,
6871 struct pipe_debug_callback *debug,
6872 struct si_shader_part *out)
6873 {
6874 union si_shader_part_key *key = &out->key;
6875 struct si_shader shader = {};
6876 struct si_shader_context ctx;
6877 struct gallivm_state *gallivm = &ctx.radeon_bld.gallivm;
6878 LLVMTypeRef *params, *returns;
6879 LLVMValueRef ret, func;
6880 int last_sgpr, num_params, num_returns, i;
6881 bool status = true;
6882
6883 si_init_shader_ctx(&ctx, sscreen, &shader, tm);
6884 ctx.type = PIPE_SHADER_VERTEX;
6885 ctx.param_vertex_id = key->vs_prolog.num_input_sgprs;
6886 ctx.param_instance_id = key->vs_prolog.num_input_sgprs + 3;
6887
6888 /* 4 preloaded VGPRs + vertex load indices as prolog outputs */
6889 params = alloca((key->vs_prolog.num_input_sgprs + 4) *
6890 sizeof(LLVMTypeRef));
6891 returns = alloca((key->vs_prolog.num_input_sgprs + 4 +
6892 key->vs_prolog.last_input + 1) *
6893 sizeof(LLVMTypeRef));
6894 num_params = 0;
6895 num_returns = 0;
6896
6897 /* Declare input and output SGPRs. */
6898 num_params = 0;
6899 for (i = 0; i < key->vs_prolog.num_input_sgprs; i++) {
6900 params[num_params++] = ctx.i32;
6901 returns[num_returns++] = ctx.i32;
6902 }
6903 last_sgpr = num_params - 1;
6904
6905 /* 4 preloaded VGPRs (outputs must be floats) */
6906 for (i = 0; i < 4; i++) {
6907 params[num_params++] = ctx.i32;
6908 returns[num_returns++] = ctx.f32;
6909 }
6910
6911 /* Vertex load indices. */
6912 for (i = 0; i <= key->vs_prolog.last_input; i++)
6913 returns[num_returns++] = ctx.f32;
6914
6915 /* Create the function. */
6916 si_create_function(&ctx, returns, num_returns, params,
6917 num_params, last_sgpr);
6918 func = ctx.radeon_bld.main_fn;
6919
6920 /* Copy inputs to outputs. This should be no-op, as the registers match,
6921 * but it will prevent the compiler from overwriting them unintentionally.
6922 */
6923 ret = ctx.return_value;
6924 for (i = 0; i < key->vs_prolog.num_input_sgprs; i++) {
6925 LLVMValueRef p = LLVMGetParam(func, i);
6926 ret = LLVMBuildInsertValue(gallivm->builder, ret, p, i, "");
6927 }
6928 for (i = num_params - 4; i < num_params; i++) {
6929 LLVMValueRef p = LLVMGetParam(func, i);
6930 p = LLVMBuildBitCast(gallivm->builder, p, ctx.f32, "");
6931 ret = LLVMBuildInsertValue(gallivm->builder, ret, p, i, "");
6932 }
6933
6934 /* Compute vertex load indices from instance divisors. */
6935 for (i = 0; i <= key->vs_prolog.last_input; i++) {
6936 unsigned divisor = key->vs_prolog.states.instance_divisors[i];
6937 LLVMValueRef index;
6938
6939 if (divisor) {
6940 /* InstanceID / Divisor + StartInstance */
6941 index = get_instance_index_for_fetch(&ctx.radeon_bld,
6942 SI_SGPR_START_INSTANCE,
6943 divisor);
6944 } else {
6945 /* VertexID + BaseVertex */
6946 index = LLVMBuildAdd(gallivm->builder,
6947 LLVMGetParam(func, ctx.param_vertex_id),
6948 LLVMGetParam(func, SI_SGPR_BASE_VERTEX), "");
6949 }
6950
6951 index = LLVMBuildBitCast(gallivm->builder, index, ctx.f32, "");
6952 ret = LLVMBuildInsertValue(gallivm->builder, ret, index,
6953 num_params++, "");
6954 }
6955
6956 /* Compile. */
6957 si_llvm_build_ret(&ctx, ret);
6958 radeon_llvm_finalize_module(&ctx.radeon_bld);
6959
6960 if (si_compile_llvm(sscreen, &out->binary, &out->config, tm,
6961 gallivm->module, debug, ctx.type,
6962 "Vertex Shader Prolog"))
6963 status = false;
6964
6965 radeon_llvm_dispose(&ctx.radeon_bld);
6966 return status;
6967 }
6968
6969 /**
6970 * Compile the vertex shader epilog. This is also used by the tessellation
6971 * evaluation shader compiled as VS.
6972 *
6973 * The input is PrimitiveID.
6974 *
6975 * If PrimitiveID is required by the pixel shader, export it.
6976 * Otherwise, do nothing.
6977 */
6978 static bool si_compile_vs_epilog(struct si_screen *sscreen,
6979 LLVMTargetMachineRef tm,
6980 struct pipe_debug_callback *debug,
6981 struct si_shader_part *out)
6982 {
6983 union si_shader_part_key *key = &out->key;
6984 struct si_shader_context ctx;
6985 struct gallivm_state *gallivm = &ctx.radeon_bld.gallivm;
6986 struct lp_build_tgsi_context *bld_base = &ctx.radeon_bld.soa.bld_base;
6987 LLVMTypeRef params[5];
6988 int num_params, i;
6989 bool status = true;
6990
6991 si_init_shader_ctx(&ctx, sscreen, NULL, tm);
6992 ctx.type = PIPE_SHADER_VERTEX;
6993
6994 /* Declare input VGPRs. */
6995 num_params = key->vs_epilog.states.export_prim_id ?
6996 (VS_EPILOG_PRIMID_LOC + 1) : 0;
6997 assert(num_params <= ARRAY_SIZE(params));
6998
6999 for (i = 0; i < num_params; i++)
7000 params[i] = ctx.f32;
7001
7002 /* Create the function. */
7003 si_create_function(&ctx, NULL, 0, params, num_params, -1);
7004
7005 /* Emit exports. */
7006 if (key->vs_epilog.states.export_prim_id) {
7007 struct lp_build_context *base = &bld_base->base;
7008 struct lp_build_context *uint = &bld_base->uint_bld;
7009 LLVMValueRef args[9];
7010
7011 args[0] = lp_build_const_int32(base->gallivm, 0x0); /* enabled channels */
7012 args[1] = uint->zero; /* whether the EXEC mask is valid */
7013 args[2] = uint->zero; /* DONE bit */
7014 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_PARAM +
7015 key->vs_epilog.prim_id_param_offset);
7016 args[4] = uint->zero; /* COMPR flag (0 = 32-bit export) */
7017 args[5] = LLVMGetParam(ctx.radeon_bld.main_fn,
7018 VS_EPILOG_PRIMID_LOC); /* X */
7019 args[6] = uint->undef; /* Y */
7020 args[7] = uint->undef; /* Z */
7021 args[8] = uint->undef; /* W */
7022
7023 lp_build_intrinsic(base->gallivm->builder, "llvm.SI.export",
7024 LLVMVoidTypeInContext(base->gallivm->context),
7025 args, 9, 0);
7026 }
7027
7028 /* Compile. */
7029 LLVMBuildRetVoid(gallivm->builder);
7030 radeon_llvm_finalize_module(&ctx.radeon_bld);
7031
7032 if (si_compile_llvm(sscreen, &out->binary, &out->config, tm,
7033 gallivm->module, debug, ctx.type,
7034 "Vertex Shader Epilog"))
7035 status = false;
7036
7037 radeon_llvm_dispose(&ctx.radeon_bld);
7038 return status;
7039 }
7040
7041 /**
7042 * Create & compile a vertex shader epilog. This a helper used by VS and TES.
7043 */
7044 static bool si_get_vs_epilog(struct si_screen *sscreen,
7045 LLVMTargetMachineRef tm,
7046 struct si_shader *shader,
7047 struct pipe_debug_callback *debug,
7048 struct si_vs_epilog_bits *states)
7049 {
7050 union si_shader_part_key epilog_key;
7051
7052 memset(&epilog_key, 0, sizeof(epilog_key));
7053 epilog_key.vs_epilog.states = *states;
7054
7055 /* Set up the PrimitiveID output. */
7056 if (shader->key.vs.epilog.export_prim_id) {
7057 unsigned index = shader->selector->info.num_outputs;
7058 unsigned offset = shader->info.nr_param_exports++;
7059
7060 epilog_key.vs_epilog.prim_id_param_offset = offset;
7061 assert(index < ARRAY_SIZE(shader->info.vs_output_param_offset));
7062 shader->info.vs_output_param_offset[index] = offset;
7063 }
7064
7065 shader->epilog = si_get_shader_part(sscreen, &sscreen->vs_epilogs,
7066 &epilog_key, tm, debug,
7067 si_compile_vs_epilog);
7068 return shader->epilog != NULL;
7069 }
7070
7071 /**
7072 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
7073 */
7074 static bool si_shader_select_vs_parts(struct si_screen *sscreen,
7075 LLVMTargetMachineRef tm,
7076 struct si_shader *shader,
7077 struct pipe_debug_callback *debug)
7078 {
7079 struct tgsi_shader_info *info = &shader->selector->info;
7080 union si_shader_part_key prolog_key;
7081 unsigned i;
7082
7083 /* Get the prolog. */
7084 memset(&prolog_key, 0, sizeof(prolog_key));
7085 prolog_key.vs_prolog.states = shader->key.vs.prolog;
7086 prolog_key.vs_prolog.num_input_sgprs = shader->info.num_input_sgprs;
7087 prolog_key.vs_prolog.last_input = MAX2(1, info->num_inputs) - 1;
7088
7089 /* The prolog is a no-op if there are no inputs. */
7090 if (info->num_inputs) {
7091 shader->prolog =
7092 si_get_shader_part(sscreen, &sscreen->vs_prologs,
7093 &prolog_key, tm, debug,
7094 si_compile_vs_prolog);
7095 if (!shader->prolog)
7096 return false;
7097 }
7098
7099 /* Get the epilog. */
7100 if (!shader->key.vs.as_es && !shader->key.vs.as_ls &&
7101 !si_get_vs_epilog(sscreen, tm, shader, debug,
7102 &shader->key.vs.epilog))
7103 return false;
7104
7105 /* Set the instanceID flag. */
7106 for (i = 0; i < info->num_inputs; i++)
7107 if (prolog_key.vs_prolog.states.instance_divisors[i])
7108 shader->info.uses_instanceid = true;
7109
7110 return true;
7111 }
7112
7113 /**
7114 * Select and compile (or reuse) TES parts (epilog).
7115 */
7116 static bool si_shader_select_tes_parts(struct si_screen *sscreen,
7117 LLVMTargetMachineRef tm,
7118 struct si_shader *shader,
7119 struct pipe_debug_callback *debug)
7120 {
7121 if (shader->key.tes.as_es)
7122 return true;
7123
7124 /* TES compiled as VS. */
7125 return si_get_vs_epilog(sscreen, tm, shader, debug,
7126 &shader->key.tes.epilog);
7127 }
7128
7129 /**
7130 * Compile the TCS epilog. This writes tesselation factors to memory based on
7131 * the output primitive type of the tesselator (determined by TES).
7132 */
7133 static bool si_compile_tcs_epilog(struct si_screen *sscreen,
7134 LLVMTargetMachineRef tm,
7135 struct pipe_debug_callback *debug,
7136 struct si_shader_part *out)
7137 {
7138 union si_shader_part_key *key = &out->key;
7139 struct si_shader shader = {};
7140 struct si_shader_context ctx;
7141 struct gallivm_state *gallivm = &ctx.radeon_bld.gallivm;
7142 struct lp_build_tgsi_context *bld_base = &ctx.radeon_bld.soa.bld_base;
7143 LLVMTypeRef params[16];
7144 LLVMValueRef func;
7145 int last_sgpr, num_params;
7146 bool status = true;
7147
7148 si_init_shader_ctx(&ctx, sscreen, &shader, tm);
7149 ctx.type = PIPE_SHADER_TESS_CTRL;
7150 shader.key.tcs.epilog = key->tcs_epilog.states;
7151
7152 /* Declare inputs. Only RW_BUFFERS and TESS_FACTOR_OFFSET are used. */
7153 params[SI_PARAM_RW_BUFFERS] = const_array(ctx.v16i8, SI_NUM_RW_BUFFERS);
7154 params[SI_PARAM_CONST_BUFFERS] = ctx.i64;
7155 params[SI_PARAM_SAMPLERS] = ctx.i64;
7156 params[SI_PARAM_IMAGES] = ctx.i64;
7157 params[SI_PARAM_SHADER_BUFFERS] = ctx.i64;
7158 params[SI_PARAM_TCS_OFFCHIP_LAYOUT] = ctx.i32;
7159 params[SI_PARAM_TCS_OUT_OFFSETS] = ctx.i32;
7160 params[SI_PARAM_TCS_OUT_LAYOUT] = ctx.i32;
7161 params[SI_PARAM_TCS_IN_LAYOUT] = ctx.i32;
7162 params[ctx.param_oc_lds = SI_PARAM_TCS_OC_LDS] = ctx.i32;
7163 params[SI_PARAM_TESS_FACTOR_OFFSET] = ctx.i32;
7164 last_sgpr = SI_PARAM_TESS_FACTOR_OFFSET;
7165 num_params = last_sgpr + 1;
7166
7167 params[num_params++] = ctx.i32; /* patch index within the wave (REL_PATCH_ID) */
7168 params[num_params++] = ctx.i32; /* invocation ID within the patch */
7169 params[num_params++] = ctx.i32; /* LDS offset where tess factors should be loaded from */
7170
7171 /* Create the function. */
7172 si_create_function(&ctx, NULL, 0, params, num_params, last_sgpr);
7173 declare_tess_lds(&ctx);
7174 func = ctx.radeon_bld.main_fn;
7175
7176 si_write_tess_factors(bld_base,
7177 LLVMGetParam(func, last_sgpr + 1),
7178 LLVMGetParam(func, last_sgpr + 2),
7179 LLVMGetParam(func, last_sgpr + 3));
7180
7181 /* Compile. */
7182 LLVMBuildRetVoid(gallivm->builder);
7183 radeon_llvm_finalize_module(&ctx.radeon_bld);
7184
7185 if (si_compile_llvm(sscreen, &out->binary, &out->config, tm,
7186 gallivm->module, debug, ctx.type,
7187 "Tessellation Control Shader Epilog"))
7188 status = false;
7189
7190 radeon_llvm_dispose(&ctx.radeon_bld);
7191 return status;
7192 }
7193
7194 /**
7195 * Select and compile (or reuse) TCS parts (epilog).
7196 */
7197 static bool si_shader_select_tcs_parts(struct si_screen *sscreen,
7198 LLVMTargetMachineRef tm,
7199 struct si_shader *shader,
7200 struct pipe_debug_callback *debug)
7201 {
7202 union si_shader_part_key epilog_key;
7203
7204 /* Get the epilog. */
7205 memset(&epilog_key, 0, sizeof(epilog_key));
7206 epilog_key.tcs_epilog.states = shader->key.tcs.epilog;
7207
7208 shader->epilog = si_get_shader_part(sscreen, &sscreen->tcs_epilogs,
7209 &epilog_key, tm, debug,
7210 si_compile_tcs_epilog);
7211 return shader->epilog != NULL;
7212 }
7213
7214 /**
7215 * Compile the pixel shader prolog. This handles:
7216 * - two-side color selection and interpolation
7217 * - overriding interpolation parameters for the API PS
7218 * - polygon stippling
7219 *
7220 * All preloaded SGPRs and VGPRs are passed through unmodified unless they are
7221 * overriden by other states. (e.g. per-sample interpolation)
7222 * Interpolated colors are stored after the preloaded VGPRs.
7223 */
7224 static bool si_compile_ps_prolog(struct si_screen *sscreen,
7225 LLVMTargetMachineRef tm,
7226 struct pipe_debug_callback *debug,
7227 struct si_shader_part *out)
7228 {
7229 union si_shader_part_key *key = &out->key;
7230 struct si_shader shader = {};
7231 struct si_shader_context ctx;
7232 struct gallivm_state *gallivm = &ctx.radeon_bld.gallivm;
7233 LLVMTypeRef *params;
7234 LLVMValueRef ret, func;
7235 int last_sgpr, num_params, num_returns, i, num_color_channels;
7236 bool status = true;
7237
7238 si_init_shader_ctx(&ctx, sscreen, &shader, tm);
7239 ctx.type = PIPE_SHADER_FRAGMENT;
7240 shader.key.ps.prolog = key->ps_prolog.states;
7241
7242 /* Number of inputs + 8 color elements. */
7243 params = alloca((key->ps_prolog.num_input_sgprs +
7244 key->ps_prolog.num_input_vgprs + 8) *
7245 sizeof(LLVMTypeRef));
7246
7247 /* Declare inputs. */
7248 num_params = 0;
7249 for (i = 0; i < key->ps_prolog.num_input_sgprs; i++)
7250 params[num_params++] = ctx.i32;
7251 last_sgpr = num_params - 1;
7252
7253 for (i = 0; i < key->ps_prolog.num_input_vgprs; i++)
7254 params[num_params++] = ctx.f32;
7255
7256 /* Declare outputs (same as inputs + add colors if needed) */
7257 num_returns = num_params;
7258 num_color_channels = util_bitcount(key->ps_prolog.colors_read);
7259 for (i = 0; i < num_color_channels; i++)
7260 params[num_returns++] = ctx.f32;
7261
7262 /* Create the function. */
7263 si_create_function(&ctx, params, num_returns, params,
7264 num_params, last_sgpr);
7265 func = ctx.radeon_bld.main_fn;
7266
7267 /* Copy inputs to outputs. This should be no-op, as the registers match,
7268 * but it will prevent the compiler from overwriting them unintentionally.
7269 */
7270 ret = ctx.return_value;
7271 for (i = 0; i < num_params; i++) {
7272 LLVMValueRef p = LLVMGetParam(func, i);
7273 ret = LLVMBuildInsertValue(gallivm->builder, ret, p, i, "");
7274 }
7275
7276 /* Polygon stippling. */
7277 if (key->ps_prolog.states.poly_stipple) {
7278 /* POS_FIXED_PT is always last. */
7279 unsigned pos = key->ps_prolog.num_input_sgprs +
7280 key->ps_prolog.num_input_vgprs - 1;
7281 LLVMValueRef ptr[2], list;
7282
7283 /* Get the pointer to rw buffers. */
7284 ptr[0] = LLVMGetParam(func, SI_SGPR_RW_BUFFERS);
7285 ptr[1] = LLVMGetParam(func, SI_SGPR_RW_BUFFERS_HI);
7286 list = lp_build_gather_values(gallivm, ptr, 2);
7287 list = LLVMBuildBitCast(gallivm->builder, list, ctx.i64, "");
7288 list = LLVMBuildIntToPtr(gallivm->builder, list,
7289 const_array(ctx.v16i8, SI_NUM_RW_BUFFERS), "");
7290
7291 si_llvm_emit_polygon_stipple(&ctx, list, pos);
7292 }
7293
7294 if (key->ps_prolog.states.bc_optimize_for_persp ||
7295 key->ps_prolog.states.bc_optimize_for_linear) {
7296 unsigned i, base = key->ps_prolog.num_input_sgprs;
7297 LLVMValueRef center[2], centroid[2], tmp, bc_optimize;
7298
7299 /* The shader should do: if (PRIM_MASK[31]) CENTROID = CENTER;
7300 * The hw doesn't compute CENTROID if the whole wave only
7301 * contains fully-covered quads.
7302 *
7303 * PRIM_MASK is after user SGPRs.
7304 */
7305 bc_optimize = LLVMGetParam(func, SI_PS_NUM_USER_SGPR);
7306 bc_optimize = LLVMBuildLShr(gallivm->builder, bc_optimize,
7307 LLVMConstInt(ctx.i32, 31, 0), "");
7308 bc_optimize = LLVMBuildTrunc(gallivm->builder, bc_optimize,
7309 ctx.i1, "");
7310
7311 if (key->ps_prolog.states.bc_optimize_for_persp) {
7312 /* Read PERSP_CENTER. */
7313 for (i = 0; i < 2; i++)
7314 center[i] = LLVMGetParam(func, base + 2 + i);
7315 /* Read PERSP_CENTROID. */
7316 for (i = 0; i < 2; i++)
7317 centroid[i] = LLVMGetParam(func, base + 4 + i);
7318 /* Select PERSP_CENTROID. */
7319 for (i = 0; i < 2; i++) {
7320 tmp = LLVMBuildSelect(gallivm->builder, bc_optimize,
7321 center[i], centroid[i], "");
7322 ret = LLVMBuildInsertValue(gallivm->builder, ret,
7323 tmp, base + 4 + i, "");
7324 }
7325 }
7326 if (key->ps_prolog.states.bc_optimize_for_linear) {
7327 /* Read LINEAR_CENTER. */
7328 for (i = 0; i < 2; i++)
7329 center[i] = LLVMGetParam(func, base + 8 + i);
7330 /* Read LINEAR_CENTROID. */
7331 for (i = 0; i < 2; i++)
7332 centroid[i] = LLVMGetParam(func, base + 10 + i);
7333 /* Select LINEAR_CENTROID. */
7334 for (i = 0; i < 2; i++) {
7335 tmp = LLVMBuildSelect(gallivm->builder, bc_optimize,
7336 center[i], centroid[i], "");
7337 ret = LLVMBuildInsertValue(gallivm->builder, ret,
7338 tmp, base + 10 + i, "");
7339 }
7340 }
7341 }
7342
7343 /* Interpolate colors. */
7344 for (i = 0; i < 2; i++) {
7345 unsigned writemask = (key->ps_prolog.colors_read >> (i * 4)) & 0xf;
7346 unsigned face_vgpr = key->ps_prolog.num_input_sgprs +
7347 key->ps_prolog.face_vgpr_index;
7348 LLVMValueRef interp[2], color[4];
7349 LLVMValueRef interp_ij = NULL, prim_mask = NULL, face = NULL;
7350
7351 if (!writemask)
7352 continue;
7353
7354 /* If the interpolation qualifier is not CONSTANT (-1). */
7355 if (key->ps_prolog.color_interp_vgpr_index[i] != -1) {
7356 unsigned interp_vgpr = key->ps_prolog.num_input_sgprs +
7357 key->ps_prolog.color_interp_vgpr_index[i];
7358
7359 /* Get the (i,j) updated by bc_optimize handling. */
7360 interp[0] = LLVMBuildExtractValue(gallivm->builder, ret,
7361 interp_vgpr, "");
7362 interp[1] = LLVMBuildExtractValue(gallivm->builder, ret,
7363 interp_vgpr + 1, "");
7364 interp_ij = lp_build_gather_values(gallivm, interp, 2);
7365 interp_ij = LLVMBuildBitCast(gallivm->builder, interp_ij,
7366 ctx.v2i32, "");
7367 }
7368
7369 /* Use the absolute location of the input. */
7370 prim_mask = LLVMGetParam(func, SI_PS_NUM_USER_SGPR);
7371
7372 if (key->ps_prolog.states.color_two_side) {
7373 face = LLVMGetParam(func, face_vgpr);
7374 face = LLVMBuildBitCast(gallivm->builder, face, ctx.i32, "");
7375 }
7376
7377 interp_fs_input(&ctx,
7378 key->ps_prolog.color_attr_index[i],
7379 TGSI_SEMANTIC_COLOR, i,
7380 key->ps_prolog.num_interp_inputs,
7381 key->ps_prolog.colors_read, interp_ij,
7382 prim_mask, face, color);
7383
7384 while (writemask) {
7385 unsigned chan = u_bit_scan(&writemask);
7386 ret = LLVMBuildInsertValue(gallivm->builder, ret, color[chan],
7387 num_params++, "");
7388 }
7389 }
7390
7391 /* Force per-sample interpolation. */
7392 if (key->ps_prolog.states.force_persp_sample_interp) {
7393 unsigned i, base = key->ps_prolog.num_input_sgprs;
7394 LLVMValueRef persp_sample[2];
7395
7396 /* Read PERSP_SAMPLE. */
7397 for (i = 0; i < 2; i++)
7398 persp_sample[i] = LLVMGetParam(func, base + i);
7399 /* Overwrite PERSP_CENTER. */
7400 for (i = 0; i < 2; i++)
7401 ret = LLVMBuildInsertValue(gallivm->builder, ret,
7402 persp_sample[i], base + 2 + i, "");
7403 /* Overwrite PERSP_CENTROID. */
7404 for (i = 0; i < 2; i++)
7405 ret = LLVMBuildInsertValue(gallivm->builder, ret,
7406 persp_sample[i], base + 4 + i, "");
7407 }
7408 if (key->ps_prolog.states.force_linear_sample_interp) {
7409 unsigned i, base = key->ps_prolog.num_input_sgprs;
7410 LLVMValueRef linear_sample[2];
7411
7412 /* Read LINEAR_SAMPLE. */
7413 for (i = 0; i < 2; i++)
7414 linear_sample[i] = LLVMGetParam(func, base + 6 + i);
7415 /* Overwrite LINEAR_CENTER. */
7416 for (i = 0; i < 2; i++)
7417 ret = LLVMBuildInsertValue(gallivm->builder, ret,
7418 linear_sample[i], base + 8 + i, "");
7419 /* Overwrite LINEAR_CENTROID. */
7420 for (i = 0; i < 2; i++)
7421 ret = LLVMBuildInsertValue(gallivm->builder, ret,
7422 linear_sample[i], base + 10 + i, "");
7423 }
7424
7425 /* Force center interpolation. */
7426 if (key->ps_prolog.states.force_persp_center_interp) {
7427 unsigned i, base = key->ps_prolog.num_input_sgprs;
7428 LLVMValueRef persp_center[2];
7429
7430 /* Read PERSP_CENTER. */
7431 for (i = 0; i < 2; i++)
7432 persp_center[i] = LLVMGetParam(func, base + 2 + i);
7433 /* Overwrite PERSP_SAMPLE. */
7434 for (i = 0; i < 2; i++)
7435 ret = LLVMBuildInsertValue(gallivm->builder, ret,
7436 persp_center[i], base + i, "");
7437 /* Overwrite PERSP_CENTROID. */
7438 for (i = 0; i < 2; i++)
7439 ret = LLVMBuildInsertValue(gallivm->builder, ret,
7440 persp_center[i], base + 4 + i, "");
7441 }
7442 if (key->ps_prolog.states.force_linear_center_interp) {
7443 unsigned i, base = key->ps_prolog.num_input_sgprs;
7444 LLVMValueRef linear_center[2];
7445
7446 /* Read LINEAR_CENTER. */
7447 for (i = 0; i < 2; i++)
7448 linear_center[i] = LLVMGetParam(func, base + 8 + i);
7449 /* Overwrite LINEAR_SAMPLE. */
7450 for (i = 0; i < 2; i++)
7451 ret = LLVMBuildInsertValue(gallivm->builder, ret,
7452 linear_center[i], base + 6 + i, "");
7453 /* Overwrite LINEAR_CENTROID. */
7454 for (i = 0; i < 2; i++)
7455 ret = LLVMBuildInsertValue(gallivm->builder, ret,
7456 linear_center[i], base + 10 + i, "");
7457 }
7458
7459 /* Tell LLVM to insert WQM instruction sequence when needed. */
7460 if (key->ps_prolog.wqm) {
7461 LLVMAddTargetDependentFunctionAttr(func,
7462 "amdgpu-ps-wqm-outputs", "");
7463 }
7464
7465 /* Compile. */
7466 si_llvm_build_ret(&ctx, ret);
7467 radeon_llvm_finalize_module(&ctx.radeon_bld);
7468
7469 if (si_compile_llvm(sscreen, &out->binary, &out->config, tm,
7470 gallivm->module, debug, ctx.type,
7471 "Fragment Shader Prolog"))
7472 status = false;
7473
7474 radeon_llvm_dispose(&ctx.radeon_bld);
7475 return status;
7476 }
7477
7478 /**
7479 * Compile the pixel shader epilog. This handles everything that must be
7480 * emulated for pixel shader exports. (alpha-test, format conversions, etc)
7481 */
7482 static bool si_compile_ps_epilog(struct si_screen *sscreen,
7483 LLVMTargetMachineRef tm,
7484 struct pipe_debug_callback *debug,
7485 struct si_shader_part *out)
7486 {
7487 union si_shader_part_key *key = &out->key;
7488 struct si_shader shader = {};
7489 struct si_shader_context ctx;
7490 struct gallivm_state *gallivm = &ctx.radeon_bld.gallivm;
7491 struct lp_build_tgsi_context *bld_base = &ctx.radeon_bld.soa.bld_base;
7492 LLVMTypeRef params[16+8*4+3];
7493 LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
7494 int last_sgpr, num_params, i;
7495 bool status = true;
7496 struct si_ps_exports exp = {};
7497
7498 si_init_shader_ctx(&ctx, sscreen, &shader, tm);
7499 ctx.type = PIPE_SHADER_FRAGMENT;
7500 shader.key.ps.epilog = key->ps_epilog.states;
7501
7502 /* Declare input SGPRs. */
7503 params[SI_PARAM_RW_BUFFERS] = ctx.i64;
7504 params[SI_PARAM_CONST_BUFFERS] = ctx.i64;
7505 params[SI_PARAM_SAMPLERS] = ctx.i64;
7506 params[SI_PARAM_IMAGES] = ctx.i64;
7507 params[SI_PARAM_SHADER_BUFFERS] = ctx.i64;
7508 params[SI_PARAM_ALPHA_REF] = ctx.f32;
7509 last_sgpr = SI_PARAM_ALPHA_REF;
7510
7511 /* Declare input VGPRs. */
7512 num_params = (last_sgpr + 1) +
7513 util_bitcount(key->ps_epilog.colors_written) * 4 +
7514 key->ps_epilog.writes_z +
7515 key->ps_epilog.writes_stencil +
7516 key->ps_epilog.writes_samplemask;
7517
7518 num_params = MAX2(num_params,
7519 last_sgpr + 1 + PS_EPILOG_SAMPLEMASK_MIN_LOC + 1);
7520
7521 assert(num_params <= ARRAY_SIZE(params));
7522
7523 for (i = last_sgpr + 1; i < num_params; i++)
7524 params[i] = ctx.f32;
7525
7526 /* Create the function. */
7527 si_create_function(&ctx, NULL, 0, params, num_params, last_sgpr);
7528 /* Disable elimination of unused inputs. */
7529 radeon_llvm_add_attribute(ctx.radeon_bld.main_fn,
7530 "InitialPSInputAddr", 0xffffff);
7531
7532 /* Process colors. */
7533 unsigned vgpr = last_sgpr + 1;
7534 unsigned colors_written = key->ps_epilog.colors_written;
7535 int last_color_export = -1;
7536
7537 /* Find the last color export. */
7538 if (!key->ps_epilog.writes_z &&
7539 !key->ps_epilog.writes_stencil &&
7540 !key->ps_epilog.writes_samplemask) {
7541 unsigned spi_format = key->ps_epilog.states.spi_shader_col_format;
7542
7543 /* If last_cbuf > 0, FS_COLOR0_WRITES_ALL_CBUFS is true. */
7544 if (colors_written == 0x1 && key->ps_epilog.states.last_cbuf > 0) {
7545 /* Just set this if any of the colorbuffers are enabled. */
7546 if (spi_format &
7547 ((1llu << (4 * (key->ps_epilog.states.last_cbuf + 1))) - 1))
7548 last_color_export = 0;
7549 } else {
7550 for (i = 0; i < 8; i++)
7551 if (colors_written & (1 << i) &&
7552 (spi_format >> (i * 4)) & 0xf)
7553 last_color_export = i;
7554 }
7555 }
7556
7557 while (colors_written) {
7558 LLVMValueRef color[4];
7559 int mrt = u_bit_scan(&colors_written);
7560
7561 for (i = 0; i < 4; i++)
7562 color[i] = LLVMGetParam(ctx.radeon_bld.main_fn, vgpr++);
7563
7564 si_export_mrt_color(bld_base, color, mrt,
7565 num_params - 1,
7566 mrt == last_color_export, &exp);
7567 }
7568
7569 /* Process depth, stencil, samplemask. */
7570 if (key->ps_epilog.writes_z)
7571 depth = LLVMGetParam(ctx.radeon_bld.main_fn, vgpr++);
7572 if (key->ps_epilog.writes_stencil)
7573 stencil = LLVMGetParam(ctx.radeon_bld.main_fn, vgpr++);
7574 if (key->ps_epilog.writes_samplemask)
7575 samplemask = LLVMGetParam(ctx.radeon_bld.main_fn, vgpr++);
7576
7577 if (depth || stencil || samplemask)
7578 si_export_mrt_z(bld_base, depth, stencil, samplemask, &exp);
7579 else if (last_color_export == -1)
7580 si_export_null(bld_base);
7581
7582 if (exp.num)
7583 si_emit_ps_exports(&ctx, &exp);
7584
7585 /* Compile. */
7586 LLVMBuildRetVoid(gallivm->builder);
7587 radeon_llvm_finalize_module(&ctx.radeon_bld);
7588
7589 if (si_compile_llvm(sscreen, &out->binary, &out->config, tm,
7590 gallivm->module, debug, ctx.type,
7591 "Fragment Shader Epilog"))
7592 status = false;
7593
7594 radeon_llvm_dispose(&ctx.radeon_bld);
7595 return status;
7596 }
7597
7598 /**
7599 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
7600 */
7601 static bool si_shader_select_ps_parts(struct si_screen *sscreen,
7602 LLVMTargetMachineRef tm,
7603 struct si_shader *shader,
7604 struct pipe_debug_callback *debug)
7605 {
7606 struct tgsi_shader_info *info = &shader->selector->info;
7607 union si_shader_part_key prolog_key;
7608 union si_shader_part_key epilog_key;
7609 unsigned i;
7610
7611 /* Get the prolog. */
7612 memset(&prolog_key, 0, sizeof(prolog_key));
7613 prolog_key.ps_prolog.states = shader->key.ps.prolog;
7614 prolog_key.ps_prolog.colors_read = info->colors_read;
7615 prolog_key.ps_prolog.num_input_sgprs = shader->info.num_input_sgprs;
7616 prolog_key.ps_prolog.num_input_vgprs = shader->info.num_input_vgprs;
7617 prolog_key.ps_prolog.wqm = info->uses_derivatives &&
7618 (prolog_key.ps_prolog.colors_read ||
7619 prolog_key.ps_prolog.states.force_persp_sample_interp ||
7620 prolog_key.ps_prolog.states.force_linear_sample_interp ||
7621 prolog_key.ps_prolog.states.force_persp_center_interp ||
7622 prolog_key.ps_prolog.states.force_linear_center_interp ||
7623 prolog_key.ps_prolog.states.bc_optimize_for_persp ||
7624 prolog_key.ps_prolog.states.bc_optimize_for_linear);
7625
7626 if (info->colors_read) {
7627 unsigned *color = shader->selector->color_attr_index;
7628
7629 if (shader->key.ps.prolog.color_two_side) {
7630 /* BCOLORs are stored after the last input. */
7631 prolog_key.ps_prolog.num_interp_inputs = info->num_inputs;
7632 prolog_key.ps_prolog.face_vgpr_index = shader->info.face_vgpr_index;
7633 shader->config.spi_ps_input_ena |= S_0286CC_FRONT_FACE_ENA(1);
7634 }
7635
7636 for (i = 0; i < 2; i++) {
7637 unsigned interp = info->input_interpolate[color[i]];
7638 unsigned location = info->input_interpolate_loc[color[i]];
7639
7640 if (!(info->colors_read & (0xf << i*4)))
7641 continue;
7642
7643 prolog_key.ps_prolog.color_attr_index[i] = color[i];
7644
7645 if (shader->key.ps.prolog.flatshade_colors &&
7646 interp == TGSI_INTERPOLATE_COLOR)
7647 interp = TGSI_INTERPOLATE_CONSTANT;
7648
7649 switch (interp) {
7650 case TGSI_INTERPOLATE_CONSTANT:
7651 prolog_key.ps_prolog.color_interp_vgpr_index[i] = -1;
7652 break;
7653 case TGSI_INTERPOLATE_PERSPECTIVE:
7654 case TGSI_INTERPOLATE_COLOR:
7655 /* Force the interpolation location for colors here. */
7656 if (shader->key.ps.prolog.force_persp_sample_interp)
7657 location = TGSI_INTERPOLATE_LOC_SAMPLE;
7658 if (shader->key.ps.prolog.force_persp_center_interp)
7659 location = TGSI_INTERPOLATE_LOC_CENTER;
7660
7661 switch (location) {
7662 case TGSI_INTERPOLATE_LOC_SAMPLE:
7663 prolog_key.ps_prolog.color_interp_vgpr_index[i] = 0;
7664 shader->config.spi_ps_input_ena |=
7665 S_0286CC_PERSP_SAMPLE_ENA(1);
7666 break;
7667 case TGSI_INTERPOLATE_LOC_CENTER:
7668 prolog_key.ps_prolog.color_interp_vgpr_index[i] = 2;
7669 shader->config.spi_ps_input_ena |=
7670 S_0286CC_PERSP_CENTER_ENA(1);
7671 break;
7672 case TGSI_INTERPOLATE_LOC_CENTROID:
7673 prolog_key.ps_prolog.color_interp_vgpr_index[i] = 4;
7674 shader->config.spi_ps_input_ena |=
7675 S_0286CC_PERSP_CENTROID_ENA(1);
7676 break;
7677 default:
7678 assert(0);
7679 }
7680 break;
7681 case TGSI_INTERPOLATE_LINEAR:
7682 /* Force the interpolation location for colors here. */
7683 if (shader->key.ps.prolog.force_linear_sample_interp)
7684 location = TGSI_INTERPOLATE_LOC_SAMPLE;
7685 if (shader->key.ps.prolog.force_linear_center_interp)
7686 location = TGSI_INTERPOLATE_LOC_CENTER;
7687
7688 switch (location) {
7689 case TGSI_INTERPOLATE_LOC_SAMPLE:
7690 prolog_key.ps_prolog.color_interp_vgpr_index[i] = 6;
7691 shader->config.spi_ps_input_ena |=
7692 S_0286CC_LINEAR_SAMPLE_ENA(1);
7693 break;
7694 case TGSI_INTERPOLATE_LOC_CENTER:
7695 prolog_key.ps_prolog.color_interp_vgpr_index[i] = 8;
7696 shader->config.spi_ps_input_ena |=
7697 S_0286CC_LINEAR_CENTER_ENA(1);
7698 break;
7699 case TGSI_INTERPOLATE_LOC_CENTROID:
7700 prolog_key.ps_prolog.color_interp_vgpr_index[i] = 10;
7701 shader->config.spi_ps_input_ena |=
7702 S_0286CC_LINEAR_CENTROID_ENA(1);
7703 break;
7704 default:
7705 assert(0);
7706 }
7707 break;
7708 default:
7709 assert(0);
7710 }
7711 }
7712 }
7713
7714 /* The prolog is a no-op if these aren't set. */
7715 if (prolog_key.ps_prolog.colors_read ||
7716 prolog_key.ps_prolog.states.force_persp_sample_interp ||
7717 prolog_key.ps_prolog.states.force_linear_sample_interp ||
7718 prolog_key.ps_prolog.states.force_persp_center_interp ||
7719 prolog_key.ps_prolog.states.force_linear_center_interp ||
7720 prolog_key.ps_prolog.states.bc_optimize_for_persp ||
7721 prolog_key.ps_prolog.states.bc_optimize_for_linear ||
7722 prolog_key.ps_prolog.states.poly_stipple) {
7723 shader->prolog =
7724 si_get_shader_part(sscreen, &sscreen->ps_prologs,
7725 &prolog_key, tm, debug,
7726 si_compile_ps_prolog);
7727 if (!shader->prolog)
7728 return false;
7729 }
7730
7731 /* Get the epilog. */
7732 memset(&epilog_key, 0, sizeof(epilog_key));
7733 epilog_key.ps_epilog.colors_written = info->colors_written;
7734 epilog_key.ps_epilog.writes_z = info->writes_z;
7735 epilog_key.ps_epilog.writes_stencil = info->writes_stencil;
7736 epilog_key.ps_epilog.writes_samplemask = info->writes_samplemask;
7737 epilog_key.ps_epilog.states = shader->key.ps.epilog;
7738
7739 shader->epilog =
7740 si_get_shader_part(sscreen, &sscreen->ps_epilogs,
7741 &epilog_key, tm, debug,
7742 si_compile_ps_epilog);
7743 if (!shader->epilog)
7744 return false;
7745
7746 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
7747 if (shader->key.ps.prolog.poly_stipple) {
7748 shader->config.spi_ps_input_ena |= S_0286CC_POS_FIXED_PT_ENA(1);
7749 assert(G_0286CC_POS_FIXED_PT_ENA(shader->config.spi_ps_input_addr));
7750 }
7751
7752 /* Set up the enable bits for per-sample shading if needed. */
7753 if (shader->key.ps.prolog.force_persp_sample_interp &&
7754 (G_0286CC_PERSP_CENTER_ENA(shader->config.spi_ps_input_ena) ||
7755 G_0286CC_PERSP_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
7756 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_CENTER_ENA;
7757 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_CENTROID_ENA;
7758 shader->config.spi_ps_input_ena |= S_0286CC_PERSP_SAMPLE_ENA(1);
7759 }
7760 if (shader->key.ps.prolog.force_linear_sample_interp &&
7761 (G_0286CC_LINEAR_CENTER_ENA(shader->config.spi_ps_input_ena) ||
7762 G_0286CC_LINEAR_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
7763 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_CENTER_ENA;
7764 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_CENTROID_ENA;
7765 shader->config.spi_ps_input_ena |= S_0286CC_LINEAR_SAMPLE_ENA(1);
7766 }
7767 if (shader->key.ps.prolog.force_persp_center_interp &&
7768 (G_0286CC_PERSP_SAMPLE_ENA(shader->config.spi_ps_input_ena) ||
7769 G_0286CC_PERSP_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
7770 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_SAMPLE_ENA;
7771 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_CENTROID_ENA;
7772 shader->config.spi_ps_input_ena |= S_0286CC_PERSP_CENTER_ENA(1);
7773 }
7774 if (shader->key.ps.prolog.force_linear_center_interp &&
7775 (G_0286CC_LINEAR_SAMPLE_ENA(shader->config.spi_ps_input_ena) ||
7776 G_0286CC_LINEAR_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
7777 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_SAMPLE_ENA;
7778 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_CENTROID_ENA;
7779 shader->config.spi_ps_input_ena |= S_0286CC_LINEAR_CENTER_ENA(1);
7780 }
7781
7782 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
7783 if (G_0286CC_POS_W_FLOAT_ENA(shader->config.spi_ps_input_ena) &&
7784 !(shader->config.spi_ps_input_ena & 0xf)) {
7785 shader->config.spi_ps_input_ena |= S_0286CC_PERSP_CENTER_ENA(1);
7786 assert(G_0286CC_PERSP_CENTER_ENA(shader->config.spi_ps_input_addr));
7787 }
7788
7789 /* At least one pair of interpolation weights must be enabled. */
7790 if (!(shader->config.spi_ps_input_ena & 0x7f)) {
7791 shader->config.spi_ps_input_ena |= S_0286CC_LINEAR_CENTER_ENA(1);
7792 assert(G_0286CC_LINEAR_CENTER_ENA(shader->config.spi_ps_input_addr));
7793 }
7794
7795 /* The sample mask input is always enabled, because the API shader always
7796 * passes it through to the epilog. Disable it here if it's unused.
7797 */
7798 if (!shader->key.ps.epilog.poly_line_smoothing &&
7799 !shader->selector->info.reads_samplemask)
7800 shader->config.spi_ps_input_ena &= C_0286CC_SAMPLE_COVERAGE_ENA;
7801
7802 return true;
7803 }
7804
7805 static void si_fix_num_sgprs(struct si_shader *shader)
7806 {
7807 unsigned min_sgprs = shader->info.num_input_sgprs + 2; /* VCC */
7808
7809 shader->config.num_sgprs = MAX2(shader->config.num_sgprs, min_sgprs);
7810 }
7811
7812 int si_shader_create(struct si_screen *sscreen, LLVMTargetMachineRef tm,
7813 struct si_shader *shader,
7814 struct pipe_debug_callback *debug)
7815 {
7816 struct si_shader *mainp = shader->selector->main_shader_part;
7817 int r;
7818
7819 /* LS, ES, VS are compiled on demand if the main part hasn't been
7820 * compiled for that stage.
7821 */
7822 if (!mainp ||
7823 (shader->selector->type == PIPE_SHADER_VERTEX &&
7824 (shader->key.vs.as_es != mainp->key.vs.as_es ||
7825 shader->key.vs.as_ls != mainp->key.vs.as_ls)) ||
7826 (shader->selector->type == PIPE_SHADER_TESS_EVAL &&
7827 shader->key.tes.as_es != mainp->key.tes.as_es) ||
7828 (shader->selector->type == PIPE_SHADER_TESS_CTRL &&
7829 shader->key.tcs.epilog.inputs_to_copy) ||
7830 shader->selector->type == PIPE_SHADER_COMPUTE) {
7831 /* Monolithic shader (compiled as a whole, has many variants,
7832 * may take a long time to compile).
7833 */
7834 r = si_compile_tgsi_shader(sscreen, tm, shader, true, debug);
7835 if (r)
7836 return r;
7837 } else {
7838 /* The shader consists of 2-3 parts:
7839 *
7840 * - the middle part is the user shader, it has 1 variant only
7841 * and it was compiled during the creation of the shader
7842 * selector
7843 * - the prolog part is inserted at the beginning
7844 * - the epilog part is inserted at the end
7845 *
7846 * The prolog and epilog have many (but simple) variants.
7847 */
7848
7849 /* Copy the compiled TGSI shader data over. */
7850 shader->is_binary_shared = true;
7851 shader->binary = mainp->binary;
7852 shader->config = mainp->config;
7853 shader->info.num_input_sgprs = mainp->info.num_input_sgprs;
7854 shader->info.num_input_vgprs = mainp->info.num_input_vgprs;
7855 shader->info.face_vgpr_index = mainp->info.face_vgpr_index;
7856 memcpy(shader->info.vs_output_param_offset,
7857 mainp->info.vs_output_param_offset,
7858 sizeof(mainp->info.vs_output_param_offset));
7859 shader->info.uses_instanceid = mainp->info.uses_instanceid;
7860 shader->info.nr_pos_exports = mainp->info.nr_pos_exports;
7861 shader->info.nr_param_exports = mainp->info.nr_param_exports;
7862
7863 /* Select prologs and/or epilogs. */
7864 switch (shader->selector->type) {
7865 case PIPE_SHADER_VERTEX:
7866 if (!si_shader_select_vs_parts(sscreen, tm, shader, debug))
7867 return -1;
7868 break;
7869 case PIPE_SHADER_TESS_CTRL:
7870 if (!si_shader_select_tcs_parts(sscreen, tm, shader, debug))
7871 return -1;
7872 break;
7873 case PIPE_SHADER_TESS_EVAL:
7874 if (!si_shader_select_tes_parts(sscreen, tm, shader, debug))
7875 return -1;
7876 break;
7877 case PIPE_SHADER_FRAGMENT:
7878 if (!si_shader_select_ps_parts(sscreen, tm, shader, debug))
7879 return -1;
7880
7881 /* Make sure we have at least as many VGPRs as there
7882 * are allocated inputs.
7883 */
7884 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
7885 shader->info.num_input_vgprs);
7886 break;
7887 }
7888
7889 /* Update SGPR and VGPR counts. */
7890 if (shader->prolog) {
7891 shader->config.num_sgprs = MAX2(shader->config.num_sgprs,
7892 shader->prolog->config.num_sgprs);
7893 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
7894 shader->prolog->config.num_vgprs);
7895 }
7896 if (shader->epilog) {
7897 shader->config.num_sgprs = MAX2(shader->config.num_sgprs,
7898 shader->epilog->config.num_sgprs);
7899 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
7900 shader->epilog->config.num_vgprs);
7901 }
7902 }
7903
7904 si_fix_num_sgprs(shader);
7905 si_shader_dump(sscreen, shader, debug, shader->selector->info.processor,
7906 stderr);
7907
7908 /* Upload. */
7909 r = si_shader_binary_upload(sscreen, shader);
7910 if (r) {
7911 fprintf(stderr, "LLVM failed to upload shader\n");
7912 return r;
7913 }
7914
7915 return 0;
7916 }
7917
7918 void si_shader_destroy(struct si_shader *shader)
7919 {
7920 if (shader->gs_copy_shader) {
7921 si_shader_destroy(shader->gs_copy_shader);
7922 FREE(shader->gs_copy_shader);
7923 }
7924
7925 if (shader->scratch_bo)
7926 r600_resource_reference(&shader->scratch_bo, NULL);
7927
7928 r600_resource_reference(&shader->bo, NULL);
7929
7930 if (!shader->is_binary_shared)
7931 radeon_shader_binary_clean(&shader->binary);
7932
7933 free(shader->shader_log);
7934 }