803de344c2acd9cab301b3290b6fe1cf4e76bfbf
[mesa.git] / src / gallium / drivers / radeonsi / si_shader.c
1
2 /*
3 * Copyright 2012 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Tom Stellard <thomas.stellard@amd.com>
26 * Michel Dänzer <michel.daenzer@amd.com>
27 * Christian König <christian.koenig@amd.com>
28 */
29
30 #include "gallivm/lp_bld_tgsi_action.h"
31 #include "gallivm/lp_bld_const.h"
32 #include "gallivm/lp_bld_gather.h"
33 #include "gallivm/lp_bld_intr.h"
34 #include "gallivm/lp_bld_logic.h"
35 #include "gallivm/lp_bld_tgsi.h"
36 #include "gallivm/lp_bld_arit.h"
37 #include "gallivm/lp_bld_flow.h"
38 #include "radeon_llvm.h"
39 #include "radeon_llvm_emit.h"
40 #include "util/u_memory.h"
41 #include "tgsi/tgsi_info.h"
42 #include "tgsi/tgsi_parse.h"
43 #include "tgsi/tgsi_scan.h"
44 #include "tgsi/tgsi_util.h"
45 #include "tgsi/tgsi_dump.h"
46
47 #include "si_pipe.h"
48 #include "si_shader.h"
49 #include "si_state.h"
50 #include "sid.h"
51
52 #include <assert.h>
53 #include <errno.h>
54 #include <stdio.h>
55
56 struct si_shader_context
57 {
58 struct radeon_llvm_context radeon_bld;
59 struct tgsi_parse_context parse;
60 struct tgsi_token * tokens;
61 struct si_pipe_shader *shader;
62 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
63 int param_streamout_config;
64 int param_streamout_write_index;
65 int param_streamout_offset[4];
66 int param_vertex_id;
67 int param_instance_id;
68 LLVMValueRef const_md;
69 LLVMValueRef const_resource[NUM_CONST_BUFFERS];
70 #if HAVE_LLVM >= 0x0304
71 LLVMValueRef ddxy_lds;
72 #endif
73 LLVMValueRef *constants[NUM_CONST_BUFFERS];
74 LLVMValueRef *resources;
75 LLVMValueRef *samplers;
76 LLVMValueRef so_buffers[4];
77 };
78
79 static struct si_shader_context * si_shader_context(
80 struct lp_build_tgsi_context * bld_base)
81 {
82 return (struct si_shader_context *)bld_base;
83 }
84
85
86 #define PERSPECTIVE_BASE 0
87 #define LINEAR_BASE 9
88
89 #define SAMPLE_OFFSET 0
90 #define CENTER_OFFSET 2
91 #define CENTROID_OFSET 4
92
93 #define USE_SGPR_MAX_SUFFIX_LEN 5
94 #define CONST_ADDR_SPACE 2
95 #define LOCAL_ADDR_SPACE 3
96 #define USER_SGPR_ADDR_SPACE 8
97
98 /**
99 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
100 *
101 * @param offset The offset parameter specifies the number of
102 * elements to offset, not the number of bytes or dwords. An element is the
103 * the type pointed to by the base_ptr parameter (e.g. int is the element of
104 * an int* pointer)
105 *
106 * When LLVM lowers the load instruction, it will convert the element offset
107 * into a dword offset automatically.
108 *
109 */
110 static LLVMValueRef build_indexed_load(
111 struct si_shader_context * si_shader_ctx,
112 LLVMValueRef base_ptr,
113 LLVMValueRef offset)
114 {
115 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
116
117 LLVMValueRef indices[2] = {
118 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
119 offset
120 };
121 LLVMValueRef computed_ptr = LLVMBuildGEP(
122 base->gallivm->builder, base_ptr, indices, 2, "");
123
124 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
125 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
126 return result;
127 }
128
129 static LLVMValueRef get_instance_index_for_fetch(
130 struct radeon_llvm_context * radeon_bld,
131 unsigned divisor)
132 {
133 struct si_shader_context *si_shader_ctx =
134 si_shader_context(&radeon_bld->soa.bld_base);
135 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
136
137 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
138 si_shader_ctx->param_instance_id);
139 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
140 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
141
142 if (divisor > 1)
143 result = LLVMBuildUDiv(gallivm->builder, result,
144 lp_build_const_int32(gallivm, divisor), "");
145
146 return result;
147 }
148
149 static void declare_input_vs(
150 struct si_shader_context * si_shader_ctx,
151 unsigned input_index,
152 const struct tgsi_full_declaration *decl)
153 {
154 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
155 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
156
157 unsigned chan;
158
159 LLVMValueRef t_list_ptr;
160 LLVMValueRef t_offset;
161 LLVMValueRef t_list;
162 LLVMValueRef attribute_offset;
163 LLVMValueRef buffer_index;
164 LLVMValueRef args[3];
165 LLVMTypeRef vec4_type;
166 LLVMValueRef input;
167
168 /* Load the T list */
169 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
170
171 t_offset = lp_build_const_int32(base->gallivm, input_index);
172
173 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
174
175 /* Build the attribute offset */
176 attribute_offset = lp_build_const_int32(base->gallivm, 0);
177
178 if (divisor) {
179 /* Build index from instance ID, start instance and divisor */
180 si_shader_ctx->shader->shader.uses_instanceid = true;
181 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
182 } else {
183 /* Load the buffer index, which is always stored in VGPR0
184 * for Vertex Shaders */
185 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
186 si_shader_ctx->param_vertex_id);
187 }
188
189 vec4_type = LLVMVectorType(base->elem_type, 4);
190 args[0] = t_list;
191 args[1] = attribute_offset;
192 args[2] = buffer_index;
193 input = build_intrinsic(base->gallivm->builder,
194 "llvm.SI.vs.load.input", vec4_type, args, 3,
195 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
196
197 /* Break up the vec4 into individual components */
198 for (chan = 0; chan < 4; chan++) {
199 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
200 /* XXX: Use a helper function for this. There is one in
201 * tgsi_llvm.c. */
202 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
203 LLVMBuildExtractElement(base->gallivm->builder,
204 input, llvm_chan, "");
205 }
206 }
207
208 static void declare_input_fs(
209 struct si_shader_context * si_shader_ctx,
210 unsigned input_index,
211 const struct tgsi_full_declaration *decl)
212 {
213 struct si_shader *shader = &si_shader_ctx->shader->shader;
214 struct lp_build_context * base =
215 &si_shader_ctx->radeon_bld.soa.bld_base.base;
216 struct lp_build_context *uint =
217 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
218 struct gallivm_state * gallivm = base->gallivm;
219 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
220 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
221
222 LLVMValueRef interp_param;
223 const char * intr_name;
224
225 /* This value is:
226 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
227 * quad begins a new primitive. Bit 0 always needs
228 * to be unset)
229 * [32:16] ParamOffset
230 *
231 */
232 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
233 LLVMValueRef attr_number;
234
235 unsigned chan;
236
237 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
238 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
239 unsigned soa_index =
240 radeon_llvm_reg_index_soa(input_index, chan);
241 si_shader_ctx->radeon_bld.inputs[soa_index] =
242 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
243
244 if (chan == 3)
245 /* RCP for fragcoord.w */
246 si_shader_ctx->radeon_bld.inputs[soa_index] =
247 LLVMBuildFDiv(gallivm->builder,
248 lp_build_const_float(gallivm, 1.0f),
249 si_shader_ctx->radeon_bld.inputs[soa_index],
250 "");
251 }
252 return;
253 }
254
255 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
256 LLVMValueRef face, is_face_positive;
257
258 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
259
260 is_face_positive = LLVMBuildFCmp(gallivm->builder,
261 LLVMRealUGT, face,
262 lp_build_const_float(gallivm, 0.0f),
263 "");
264
265 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
266 LLVMBuildSelect(gallivm->builder,
267 is_face_positive,
268 lp_build_const_float(gallivm, 1.0f),
269 lp_build_const_float(gallivm, 0.0f),
270 "");
271 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
272 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
273 lp_build_const_float(gallivm, 0.0f);
274 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
275 lp_build_const_float(gallivm, 1.0f);
276
277 return;
278 }
279
280 shader->input[input_index].param_offset = shader->ninterp++;
281 attr_number = lp_build_const_int32(gallivm,
282 shader->input[input_index].param_offset);
283
284 switch (decl->Interp.Interpolate) {
285 case TGSI_INTERPOLATE_COLOR:
286 if (si_shader_ctx->shader->key.ps.flatshade) {
287 interp_param = 0;
288 } else {
289 if (decl->Interp.Centroid)
290 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
291 else
292 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
293 }
294 break;
295 case TGSI_INTERPOLATE_CONSTANT:
296 interp_param = 0;
297 break;
298 case TGSI_INTERPOLATE_LINEAR:
299 if (decl->Interp.Centroid)
300 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
301 else
302 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
303 break;
304 case TGSI_INTERPOLATE_PERSPECTIVE:
305 if (decl->Interp.Centroid)
306 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
307 else
308 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
309 break;
310 default:
311 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
312 return;
313 }
314
315 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
316
317 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
318 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
319 si_shader_ctx->shader->key.ps.color_two_side) {
320 LLVMValueRef args[4];
321 LLVMValueRef face, is_face_positive;
322 LLVMValueRef back_attr_number =
323 lp_build_const_int32(gallivm,
324 shader->input[input_index].param_offset + 1);
325
326 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
327
328 is_face_positive = LLVMBuildFCmp(gallivm->builder,
329 LLVMRealUGT, face,
330 lp_build_const_float(gallivm, 0.0f),
331 "");
332
333 args[2] = params;
334 args[3] = interp_param;
335 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
336 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
337 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
338 LLVMValueRef front, back;
339
340 args[0] = llvm_chan;
341 args[1] = attr_number;
342 front = build_intrinsic(base->gallivm->builder, intr_name,
343 input_type, args, args[3] ? 4 : 3,
344 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
345
346 args[1] = back_attr_number;
347 back = build_intrinsic(base->gallivm->builder, intr_name,
348 input_type, args, args[3] ? 4 : 3,
349 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
350
351 si_shader_ctx->radeon_bld.inputs[soa_index] =
352 LLVMBuildSelect(gallivm->builder,
353 is_face_positive,
354 front,
355 back,
356 "");
357 }
358
359 shader->ninterp++;
360 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
361 LLVMValueRef args[4];
362
363 args[0] = uint->zero;
364 args[1] = attr_number;
365 args[2] = params;
366 args[3] = interp_param;
367 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
368 build_intrinsic(base->gallivm->builder, intr_name,
369 input_type, args, args[3] ? 4 : 3,
370 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
371 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
372 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
373 lp_build_const_float(gallivm, 0.0f);
374 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
375 lp_build_const_float(gallivm, 1.0f);
376 } else {
377 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
378 LLVMValueRef args[4];
379 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
380 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
381 args[0] = llvm_chan;
382 args[1] = attr_number;
383 args[2] = params;
384 args[3] = interp_param;
385 si_shader_ctx->radeon_bld.inputs[soa_index] =
386 build_intrinsic(base->gallivm->builder, intr_name,
387 input_type, args, args[3] ? 4 : 3,
388 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
389 }
390 }
391 }
392
393 static void declare_input(
394 struct radeon_llvm_context * radeon_bld,
395 unsigned input_index,
396 const struct tgsi_full_declaration *decl)
397 {
398 struct si_shader_context * si_shader_ctx =
399 si_shader_context(&radeon_bld->soa.bld_base);
400 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
401 declare_input_vs(si_shader_ctx, input_index, decl);
402 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
403 declare_input_fs(si_shader_ctx, input_index, decl);
404 } else {
405 fprintf(stderr, "Warning: Unsupported shader type,\n");
406 }
407 }
408
409 static void declare_system_value(
410 struct radeon_llvm_context * radeon_bld,
411 unsigned index,
412 const struct tgsi_full_declaration *decl)
413 {
414 struct si_shader_context *si_shader_ctx =
415 si_shader_context(&radeon_bld->soa.bld_base);
416 LLVMValueRef value = 0;
417
418 switch (decl->Semantic.Name) {
419 case TGSI_SEMANTIC_INSTANCEID:
420 value = LLVMGetParam(radeon_bld->main_fn,
421 si_shader_ctx->param_instance_id);
422 break;
423
424 case TGSI_SEMANTIC_VERTEXID:
425 value = LLVMGetParam(radeon_bld->main_fn,
426 si_shader_ctx->param_vertex_id);
427 break;
428
429 default:
430 assert(!"unknown system value");
431 return;
432 }
433
434 radeon_bld->system_values[index] = value;
435 }
436
437 static LLVMValueRef fetch_constant(
438 struct lp_build_tgsi_context * bld_base,
439 const struct tgsi_full_src_register *reg,
440 enum tgsi_opcode_type type,
441 unsigned swizzle)
442 {
443 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
444 struct lp_build_context * base = &bld_base->base;
445 const struct tgsi_ind_register *ireg = &reg->Indirect;
446 unsigned buf, idx;
447
448 LLVMValueRef args[2];
449 LLVMValueRef addr;
450 LLVMValueRef result;
451
452 if (swizzle == LP_CHAN_ALL) {
453 unsigned chan;
454 LLVMValueRef values[4];
455 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
456 values[chan] = fetch_constant(bld_base, reg, type, chan);
457
458 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
459 }
460
461 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
462 idx = reg->Register.Index * 4 + swizzle;
463
464 if (!reg->Register.Indirect)
465 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
466
467 args[0] = si_shader_ctx->const_resource[buf];
468 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
469 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
470 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
471 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
472 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
473
474 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
475 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
476
477 return bitcast(bld_base, type, result);
478 }
479
480 /* Initialize arguments for the shader export intrinsic */
481 static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
482 struct tgsi_full_declaration *d,
483 unsigned index,
484 unsigned target,
485 LLVMValueRef *args)
486 {
487 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
488 struct lp_build_context *uint =
489 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
490 struct lp_build_context *base = &bld_base->base;
491 unsigned compressed = 0;
492 unsigned chan;
493
494 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
495 int cbuf = target - V_008DFC_SQ_EXP_MRT;
496
497 if (cbuf >= 0 && cbuf < 8) {
498 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
499
500 if (compressed)
501 si_shader_ctx->shader->spi_shader_col_format |=
502 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
503 else
504 si_shader_ctx->shader->spi_shader_col_format |=
505 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
506
507 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
508 }
509 }
510
511 if (compressed) {
512 /* Pixel shader needs to pack output values before export */
513 for (chan = 0; chan < 2; chan++ ) {
514 LLVMValueRef *out_ptr =
515 si_shader_ctx->radeon_bld.soa.outputs[index];
516 args[0] = LLVMBuildLoad(base->gallivm->builder,
517 out_ptr[2 * chan], "");
518 args[1] = LLVMBuildLoad(base->gallivm->builder,
519 out_ptr[2 * chan + 1], "");
520 args[chan + 5] =
521 build_intrinsic(base->gallivm->builder,
522 "llvm.SI.packf16",
523 LLVMInt32TypeInContext(base->gallivm->context),
524 args, 2,
525 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
526 args[chan + 7] = args[chan + 5] =
527 LLVMBuildBitCast(base->gallivm->builder,
528 args[chan + 5],
529 LLVMFloatTypeInContext(base->gallivm->context),
530 "");
531 }
532
533 /* Set COMPR flag */
534 args[4] = uint->one;
535 } else {
536 for (chan = 0; chan < 4; chan++ ) {
537 LLVMValueRef out_ptr =
538 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
539 /* +5 because the first output value will be
540 * the 6th argument to the intrinsic. */
541 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
542 out_ptr, "");
543 }
544
545 /* Clear COMPR flag */
546 args[4] = uint->zero;
547 }
548
549 /* XXX: This controls which components of the output
550 * registers actually get exported. (e.g bit 0 means export
551 * X component, bit 1 means export Y component, etc.) I'm
552 * hard coding this to 0xf for now. In the future, we might
553 * want to do something else. */
554 args[0] = lp_build_const_int32(base->gallivm, 0xf);
555
556 /* Specify whether the EXEC mask represents the valid mask */
557 args[1] = uint->zero;
558
559 /* Specify whether this is the last export */
560 args[2] = uint->zero;
561
562 /* Specify the target we are exporting */
563 args[3] = lp_build_const_int32(base->gallivm, target);
564
565 /* XXX: We probably need to keep track of the output
566 * values, so we know what we are passing to the next
567 * stage. */
568 }
569
570 static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
571 unsigned index)
572 {
573 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
574 struct gallivm_state *gallivm = bld_base->base.gallivm;
575
576 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
577 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
578 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
579 SI_PARAM_ALPHA_REF);
580
581 LLVMValueRef alpha_pass =
582 lp_build_cmp(&bld_base->base,
583 si_shader_ctx->shader->key.ps.alpha_func,
584 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
585 alpha_ref);
586 LLVMValueRef arg =
587 lp_build_select(&bld_base->base,
588 alpha_pass,
589 lp_build_const_float(gallivm, 1.0f),
590 lp_build_const_float(gallivm, -1.0f));
591
592 build_intrinsic(gallivm->builder,
593 "llvm.AMDGPU.kill",
594 LLVMVoidTypeInContext(gallivm->context),
595 &arg, 1, 0);
596 } else {
597 build_intrinsic(gallivm->builder,
598 "llvm.AMDGPU.kilp",
599 LLVMVoidTypeInContext(gallivm->context),
600 NULL, 0, 0);
601 }
602 }
603
604 static void si_alpha_to_one(struct lp_build_tgsi_context *bld_base,
605 unsigned index)
606 {
607 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
608
609 /* set alpha to one */
610 LLVMBuildStore(bld_base->base.gallivm->builder,
611 bld_base->base.one,
612 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
613 }
614
615 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
616 LLVMValueRef (*pos)[9], unsigned index)
617 {
618 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
619 struct si_pipe_shader *shader = si_shader_ctx->shader;
620 struct lp_build_context *base = &bld_base->base;
621 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
622 unsigned reg_index;
623 unsigned chan;
624 unsigned const_chan;
625 LLVMValueRef out_elts[4];
626 LLVMValueRef base_elt;
627 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
628 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, NUM_PIPE_CONST_BUFFERS);
629 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
630
631 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
632 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
633 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
634 }
635
636 for (reg_index = 0; reg_index < 2; reg_index ++) {
637 LLVMValueRef *args = pos[2 + reg_index];
638
639 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
640 continue;
641
642 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
643
644 args[5] =
645 args[6] =
646 args[7] =
647 args[8] = lp_build_const_float(base->gallivm, 0.0f);
648
649 /* Compute dot products of position and user clip plane vectors */
650 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
651 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
652 args[0] = const_resource;
653 args[1] = lp_build_const_int32(base->gallivm,
654 ((reg_index * 4 + chan) * 4 +
655 const_chan) * 4);
656 base_elt = build_intrinsic(base->gallivm->builder,
657 "llvm.SI.load.const",
658 base->elem_type,
659 args, 2,
660 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
661 args[5 + chan] =
662 lp_build_add(base, args[5 + chan],
663 lp_build_mul(base, base_elt,
664 out_elts[const_chan]));
665 }
666 }
667
668 args[0] = lp_build_const_int32(base->gallivm, 0xf);
669 args[1] = uint->zero;
670 args[2] = uint->zero;
671 args[3] = lp_build_const_int32(base->gallivm,
672 V_008DFC_SQ_EXP_POS + 2 + reg_index);
673 args[4] = uint->zero;
674 }
675 }
676
677 static void si_dump_streamout(struct pipe_stream_output_info *so)
678 {
679 unsigned i;
680
681 if (so->num_outputs)
682 fprintf(stderr, "STREAMOUT\n");
683
684 for (i = 0; i < so->num_outputs; i++) {
685 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
686 so->output[i].start_component;
687 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
688 i, so->output[i].output_buffer,
689 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
690 so->output[i].register_index,
691 mask & 1 ? "x" : "",
692 mask & 2 ? "y" : "",
693 mask & 4 ? "z" : "",
694 mask & 8 ? "w" : "");
695 }
696 }
697
698 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
699 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
700 * or v4i32 (num_channels=3,4). */
701 static void build_tbuffer_store(struct si_shader_context *shader,
702 LLVMValueRef rsrc,
703 LLVMValueRef vdata,
704 unsigned num_channels,
705 LLVMValueRef vaddr,
706 LLVMValueRef soffset,
707 unsigned inst_offset,
708 unsigned dfmt,
709 unsigned nfmt,
710 unsigned offen,
711 unsigned idxen,
712 unsigned glc,
713 unsigned slc,
714 unsigned tfe)
715 {
716 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
717 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
718 LLVMValueRef args[] = {
719 rsrc,
720 vdata,
721 LLVMConstInt(i32, num_channels, 0),
722 vaddr,
723 soffset,
724 LLVMConstInt(i32, inst_offset, 0),
725 LLVMConstInt(i32, dfmt, 0),
726 LLVMConstInt(i32, nfmt, 0),
727 LLVMConstInt(i32, offen, 0),
728 LLVMConstInt(i32, idxen, 0),
729 LLVMConstInt(i32, glc, 0),
730 LLVMConstInt(i32, slc, 0),
731 LLVMConstInt(i32, tfe, 0)
732 };
733
734 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
735 unsigned func = CLAMP(num_channels, 1, 3) - 1;
736 const char *types[] = {"i32", "v2i32", "v4i32"};
737 char name[256];
738 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
739
740 lp_build_intrinsic(gallivm->builder, name,
741 LLVMVoidTypeInContext(gallivm->context),
742 args, Elements(args));
743 }
744
745 static void build_streamout_store(struct si_shader_context *shader,
746 LLVMValueRef rsrc,
747 LLVMValueRef vdata,
748 unsigned num_channels,
749 LLVMValueRef vaddr,
750 LLVMValueRef soffset,
751 unsigned inst_offset)
752 {
753 static unsigned dfmt[] = {
754 V_008F0C_BUF_DATA_FORMAT_32,
755 V_008F0C_BUF_DATA_FORMAT_32_32,
756 V_008F0C_BUF_DATA_FORMAT_32_32_32,
757 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
758 };
759 assert(num_channels >= 1 && num_channels <= 4);
760
761 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
762 inst_offset, dfmt[num_channels-1],
763 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
764 }
765
766 /* On SI, the vertex shader is responsible for writing streamout data
767 * to buffers. */
768 static void si_llvm_emit_streamout(struct si_shader_context *shader)
769 {
770 struct pipe_stream_output_info *so = &shader->shader->selector->so;
771 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
772 LLVMBuilderRef builder = gallivm->builder;
773 int i, j;
774 struct lp_build_if_state if_ctx;
775
776 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
777
778 LLVMValueRef so_param =
779 LLVMGetParam(shader->radeon_bld.main_fn,
780 shader->param_streamout_config);
781
782 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
783 LLVMValueRef so_vtx_count =
784 LLVMBuildAnd(builder,
785 LLVMBuildLShr(builder, so_param,
786 LLVMConstInt(i32, 16, 0), ""),
787 LLVMConstInt(i32, 127, 0), "");
788
789 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
790 NULL, 0, LLVMReadNoneAttribute);
791
792 /* can_emit = tid < so_vtx_count; */
793 LLVMValueRef can_emit =
794 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
795
796 /* Emit the streamout code conditionally. This actually avoids
797 * out-of-bounds buffer access. The hw tells us via the SGPR
798 * (so_vtx_count) which threads are allowed to emit streamout data. */
799 lp_build_if(&if_ctx, gallivm, can_emit);
800 {
801 /* The buffer offset is computed as follows:
802 * ByteOffset = streamout_offset[buffer_id]*4 +
803 * (streamout_write_index + thread_id)*stride[buffer_id] +
804 * attrib_offset
805 */
806
807 LLVMValueRef so_write_index =
808 LLVMGetParam(shader->radeon_bld.main_fn,
809 shader->param_streamout_write_index);
810
811 /* Compute (streamout_write_index + thread_id). */
812 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
813
814 /* Compute the write offset for each enabled buffer. */
815 LLVMValueRef so_write_offset[4] = {};
816 for (i = 0; i < 4; i++) {
817 if (!so->stride[i])
818 continue;
819
820 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
821 shader->param_streamout_offset[i]);
822 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
823
824 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
825 LLVMConstInt(i32, so->stride[i]*4, 0), "");
826 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
827 }
828
829 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS] = shader->radeon_bld.soa.outputs;
830
831 /* Write streamout data. */
832 for (i = 0; i < so->num_outputs; i++) {
833 unsigned buf_idx = so->output[i].output_buffer;
834 unsigned reg = so->output[i].register_index;
835 unsigned start = so->output[i].start_component;
836 unsigned num_comps = so->output[i].num_components;
837 LLVMValueRef out[4];
838
839 assert(num_comps && num_comps <= 4);
840 if (!num_comps || num_comps > 4)
841 continue;
842
843 /* Load the output as int. */
844 for (j = 0; j < num_comps; j++) {
845 out[j] = LLVMBuildLoad(builder, outputs[reg][start+j], "");
846 out[j] = LLVMBuildBitCast(builder, out[j], i32, "");
847 }
848
849 /* Pack the output. */
850 LLVMValueRef vdata = NULL;
851
852 switch (num_comps) {
853 case 1: /* as i32 */
854 vdata = out[0];
855 break;
856 case 2: /* as v2i32 */
857 case 3: /* as v4i32 (aligned to 4) */
858 case 4: /* as v4i32 */
859 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
860 for (j = 0; j < num_comps; j++) {
861 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
862 LLVMConstInt(i32, j, 0), "");
863 }
864 break;
865 }
866
867 build_streamout_store(shader, shader->so_buffers[buf_idx],
868 vdata, num_comps,
869 so_write_offset[buf_idx],
870 LLVMConstInt(i32, 0, 0),
871 so->output[i].dst_offset*4);
872 }
873 }
874 lp_build_endif(&if_ctx);
875 }
876
877
878 static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
879 {
880 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
881 struct si_shader * shader = &si_shader_ctx->shader->shader;
882 struct lp_build_context * base = &bld_base->base;
883 struct lp_build_context * uint =
884 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
885 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
886 LLVMValueRef args[9];
887 LLVMValueRef last_args[9] = { 0 };
888 LLVMValueRef pos_args[4][9] = { { 0 } };
889 unsigned semantic_name;
890 unsigned param_count = 0;
891 int depth_index = -1, stencil_index = -1, psize_index = -1, edgeflag_index = -1;
892 int layer_index = -1;
893 int i;
894
895 if (si_shader_ctx->shader->selector->so.num_outputs) {
896 si_llvm_emit_streamout(si_shader_ctx);
897 }
898
899 while (!tgsi_parse_end_of_tokens(parse)) {
900 struct tgsi_full_declaration *d =
901 &parse->FullToken.FullDeclaration;
902 unsigned target;
903 unsigned index;
904
905 tgsi_parse_token(parse);
906
907 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
908 parse->FullToken.FullProperty.Property.PropertyName ==
909 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
910 shader->fs_write_all = TRUE;
911
912 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
913 continue;
914
915 switch (d->Declaration.File) {
916 case TGSI_FILE_INPUT:
917 i = shader->ninput++;
918 assert(i < Elements(shader->input));
919 shader->input[i].name = d->Semantic.Name;
920 shader->input[i].sid = d->Semantic.Index;
921 shader->input[i].interpolate = d->Interp.Interpolate;
922 shader->input[i].centroid = d->Interp.Centroid;
923 continue;
924
925 case TGSI_FILE_OUTPUT:
926 i = shader->noutput++;
927 assert(i < Elements(shader->output));
928 shader->output[i].name = d->Semantic.Name;
929 shader->output[i].sid = d->Semantic.Index;
930 shader->output[i].interpolate = d->Interp.Interpolate;
931 break;
932
933 default:
934 continue;
935 }
936
937 semantic_name = d->Semantic.Name;
938 handle_semantic:
939 for (index = d->Range.First; index <= d->Range.Last; index++) {
940 /* Select the correct target */
941 switch(semantic_name) {
942 case TGSI_SEMANTIC_PSIZE:
943 shader->vs_out_misc_write = true;
944 shader->vs_out_point_size = true;
945 psize_index = index;
946 continue;
947 case TGSI_SEMANTIC_EDGEFLAG:
948 shader->vs_out_misc_write = true;
949 shader->vs_out_edgeflag = true;
950 edgeflag_index = index;
951 continue;
952 case TGSI_SEMANTIC_LAYER:
953 shader->vs_out_misc_write = true;
954 shader->vs_out_layer = true;
955 layer_index = index;
956 continue;
957 case TGSI_SEMANTIC_POSITION:
958 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
959 target = V_008DFC_SQ_EXP_POS;
960 break;
961 } else {
962 depth_index = index;
963 continue;
964 }
965 case TGSI_SEMANTIC_STENCIL:
966 stencil_index = index;
967 continue;
968 case TGSI_SEMANTIC_COLOR:
969 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
970 case TGSI_SEMANTIC_BCOLOR:
971 target = V_008DFC_SQ_EXP_PARAM + param_count;
972 shader->output[i].param_offset = param_count;
973 param_count++;
974 } else {
975 target = V_008DFC_SQ_EXP_MRT + shader->output[i].sid;
976 if (si_shader_ctx->shader->key.ps.alpha_to_one) {
977 si_alpha_to_one(bld_base, index);
978 }
979 if (shader->output[i].sid == 0 &&
980 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
981 si_alpha_test(bld_base, index);
982 }
983 break;
984 case TGSI_SEMANTIC_CLIPDIST:
985 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
986 (1 << d->Semantic.Index)))
987 continue;
988 shader->clip_dist_write |=
989 d->Declaration.UsageMask << (d->Semantic.Index << 2);
990 target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index;
991 break;
992 case TGSI_SEMANTIC_CLIPVERTEX:
993 si_llvm_emit_clipvertex(bld_base, pos_args, index);
994 continue;
995 case TGSI_SEMANTIC_FOG:
996 case TGSI_SEMANTIC_GENERIC:
997 target = V_008DFC_SQ_EXP_PARAM + param_count;
998 shader->output[i].param_offset = param_count;
999 param_count++;
1000 break;
1001 default:
1002 target = 0;
1003 fprintf(stderr,
1004 "Warning: SI unhandled output type:%d\n",
1005 semantic_name);
1006 }
1007
1008 si_llvm_init_export_args(bld_base, d, index, target, args);
1009
1010 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
1011 target >= V_008DFC_SQ_EXP_POS &&
1012 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1013 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1014 args, sizeof(args));
1015 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT &&
1016 semantic_name == TGSI_SEMANTIC_COLOR) {
1017 /* If there is an export instruction waiting to be emitted, do so now. */
1018 if (last_args[0]) {
1019 lp_build_intrinsic(base->gallivm->builder,
1020 "llvm.SI.export",
1021 LLVMVoidTypeInContext(base->gallivm->context),
1022 last_args, 9);
1023 }
1024
1025 /* This instruction will be emitted at the end of the shader. */
1026 memcpy(last_args, args, sizeof(args));
1027
1028 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1029 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1030 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1031 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
1032 si_llvm_init_export_args(bld_base, d, index,
1033 V_008DFC_SQ_EXP_MRT + c, args);
1034 lp_build_intrinsic(base->gallivm->builder,
1035 "llvm.SI.export",
1036 LLVMVoidTypeInContext(base->gallivm->context),
1037 args, 9);
1038 }
1039 }
1040 } else {
1041 lp_build_intrinsic(base->gallivm->builder,
1042 "llvm.SI.export",
1043 LLVMVoidTypeInContext(base->gallivm->context),
1044 args, 9);
1045 }
1046 }
1047
1048 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1049 semantic_name = TGSI_SEMANTIC_GENERIC;
1050 goto handle_semantic;
1051 }
1052 }
1053
1054 if (depth_index >= 0 || stencil_index >= 0) {
1055 LLVMValueRef out_ptr;
1056 unsigned mask = 0;
1057
1058 /* Specify the target we are exporting */
1059 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1060
1061 if (depth_index >= 0) {
1062 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1063 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1064 mask |= 0x1;
1065
1066 if (stencil_index < 0) {
1067 args[6] =
1068 args[7] =
1069 args[8] = args[5];
1070 }
1071 }
1072
1073 if (stencil_index >= 0) {
1074 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
1075 args[7] =
1076 args[8] =
1077 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1078 /* Only setting the stencil component bit (0x2) here
1079 * breaks some stencil piglit tests
1080 */
1081 mask |= 0x3;
1082
1083 if (depth_index < 0)
1084 args[5] = args[6];
1085 }
1086
1087 /* Specify which components to enable */
1088 args[0] = lp_build_const_int32(base->gallivm, mask);
1089
1090 args[1] =
1091 args[2] =
1092 args[4] = uint->zero;
1093
1094 if (last_args[0])
1095 lp_build_intrinsic(base->gallivm->builder,
1096 "llvm.SI.export",
1097 LLVMVoidTypeInContext(base->gallivm->context),
1098 args, 9);
1099 else
1100 memcpy(last_args, args, sizeof(args));
1101 }
1102
1103 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
1104 unsigned pos_idx = 0;
1105
1106 /* We need to add the position output manually if it's missing. */
1107 if (!pos_args[0][0]) {
1108 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1109 pos_args[0][1] = uint->zero; /* EXEC mask */
1110 pos_args[0][2] = uint->zero; /* last export? */
1111 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1112 pos_args[0][4] = uint->zero; /* COMPR flag */
1113 pos_args[0][5] = base->zero; /* X */
1114 pos_args[0][6] = base->zero; /* Y */
1115 pos_args[0][7] = base->zero; /* Z */
1116 pos_args[0][8] = base->one; /* W */
1117 }
1118
1119 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1120 if (shader->vs_out_misc_write) {
1121 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1122 shader->vs_out_point_size |
1123 (shader->vs_out_edgeflag << 1) |
1124 (shader->vs_out_layer << 2));
1125 pos_args[1][1] = uint->zero; /* EXEC mask */
1126 pos_args[1][2] = uint->zero; /* last export? */
1127 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1128 pos_args[1][4] = uint->zero; /* COMPR flag */
1129 pos_args[1][5] = base->zero; /* X */
1130 pos_args[1][6] = base->zero; /* Y */
1131 pos_args[1][7] = base->zero; /* Z */
1132 pos_args[1][8] = base->zero; /* W */
1133
1134 if (shader->vs_out_point_size) {
1135 pos_args[1][5] = LLVMBuildLoad(base->gallivm->builder,
1136 si_shader_ctx->radeon_bld.soa.outputs[psize_index][0], "");
1137 }
1138
1139 if (shader->vs_out_edgeflag) {
1140 LLVMValueRef output = LLVMBuildLoad(base->gallivm->builder,
1141 si_shader_ctx->radeon_bld.soa.outputs[edgeflag_index][0], "");
1142
1143 /* The output is a float, but the hw expects an integer
1144 * with the first bit containing the edge flag. */
1145 output = LLVMBuildFPToUI(base->gallivm->builder, output,
1146 bld_base->uint_bld.elem_type, "");
1147
1148 output = lp_build_min(&bld_base->int_bld, output, bld_base->int_bld.one);
1149
1150 /* The LLVM intrinsic expects a float. */
1151 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder, output,
1152 base->elem_type, "");
1153 }
1154
1155 if (shader->vs_out_layer) {
1156 pos_args[1][7] = LLVMBuildLoad(base->gallivm->builder,
1157 si_shader_ctx->radeon_bld.soa.outputs[layer_index][0], "");
1158 }
1159 }
1160
1161 for (i = 0; i < 4; i++)
1162 if (pos_args[i][0])
1163 shader->nr_pos_exports++;
1164
1165 for (i = 0; i < 4; i++) {
1166 if (!pos_args[i][0])
1167 continue;
1168
1169 /* Specify the target we are exporting */
1170 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1171
1172 if (pos_idx == shader->nr_pos_exports)
1173 /* Specify that this is the last export */
1174 pos_args[i][2] = uint->one;
1175
1176 lp_build_intrinsic(base->gallivm->builder,
1177 "llvm.SI.export",
1178 LLVMVoidTypeInContext(base->gallivm->context),
1179 pos_args[i], 9);
1180 }
1181 } else {
1182 if (!last_args[0]) {
1183 /* Specify which components to enable */
1184 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
1185
1186 /* Specify the target we are exporting */
1187 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
1188
1189 /* Set COMPR flag to zero to export data as 32-bit */
1190 last_args[4] = uint->zero;
1191
1192 /* dummy bits */
1193 last_args[5]= uint->zero;
1194 last_args[6]= uint->zero;
1195 last_args[7]= uint->zero;
1196 last_args[8]= uint->zero;
1197
1198 si_shader_ctx->shader->spi_shader_col_format |=
1199 V_028714_SPI_SHADER_32_ABGR;
1200 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
1201 }
1202
1203 /* Specify whether the EXEC mask represents the valid mask */
1204 last_args[1] = uint->one;
1205
1206 /* Specify that this is the last export */
1207 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1208
1209 lp_build_intrinsic(base->gallivm->builder,
1210 "llvm.SI.export",
1211 LLVMVoidTypeInContext(base->gallivm->context),
1212 last_args, 9);
1213 }
1214 }
1215
1216 static const struct lp_build_tgsi_action txf_action;
1217
1218 static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1219 struct lp_build_tgsi_context * bld_base,
1220 struct lp_build_emit_data * emit_data);
1221
1222 static void tex_fetch_args(
1223 struct lp_build_tgsi_context * bld_base,
1224 struct lp_build_emit_data * emit_data)
1225 {
1226 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1227 struct gallivm_state *gallivm = bld_base->base.gallivm;
1228 const struct tgsi_full_instruction * inst = emit_data->inst;
1229 unsigned opcode = inst->Instruction.Opcode;
1230 unsigned target = inst->Texture.Texture;
1231 LLVMValueRef coords[4];
1232 LLVMValueRef address[16];
1233 int ref_pos;
1234 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
1235 unsigned count = 0;
1236 unsigned chan;
1237 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1238 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1239
1240 if (target == TGSI_TEXTURE_BUFFER) {
1241 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1242 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1243 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1244 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1245
1246 /* Truncate v32i8 to v16i8. */
1247 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1248 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1249 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1250 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1251
1252 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1253 emit_data->args[0] = res;
1254 emit_data->args[1] = bld_base->uint_bld.zero;
1255 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1256 emit_data->arg_count = 3;
1257 return;
1258 }
1259
1260 /* Fetch and project texture coordinates */
1261 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
1262 for (chan = 0; chan < 3; chan++ ) {
1263 coords[chan] = lp_build_emit_fetch(bld_base,
1264 emit_data->inst, 0,
1265 chan);
1266 if (opcode == TGSI_OPCODE_TXP)
1267 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1268 TGSI_OPCODE_DIV,
1269 coords[chan],
1270 coords[3]);
1271 }
1272
1273 if (opcode == TGSI_OPCODE_TXP)
1274 coords[3] = bld_base->base.one;
1275
1276 /* Pack LOD bias value */
1277 if (opcode == TGSI_OPCODE_TXB)
1278 address[count++] = coords[3];
1279
1280 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
1281 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1282
1283 /* Pack depth comparison value */
1284 switch (target) {
1285 case TGSI_TEXTURE_SHADOW1D:
1286 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1287 case TGSI_TEXTURE_SHADOW2D:
1288 case TGSI_TEXTURE_SHADOWRECT:
1289 case TGSI_TEXTURE_SHADOWCUBE:
1290 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1291 assert(ref_pos >= 0);
1292 address[count++] = coords[ref_pos];
1293 break;
1294 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1295 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1296 }
1297
1298 /* Pack user derivatives */
1299 if (opcode == TGSI_OPCODE_TXD) {
1300 for (chan = 0; chan < 2; chan++) {
1301 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1302 if (num_coords > 1)
1303 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1304 }
1305 }
1306
1307 /* Pack texture coordinates */
1308 address[count++] = coords[0];
1309 if (num_coords > 1)
1310 address[count++] = coords[1];
1311 if (num_coords > 2)
1312 address[count++] = coords[2];
1313
1314 /* Pack LOD or sample index */
1315 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
1316 address[count++] = coords[3];
1317
1318 if (count > 16) {
1319 assert(!"Cannot handle more than 16 texture address parameters");
1320 count = 16;
1321 }
1322
1323 for (chan = 0; chan < count; chan++ ) {
1324 address[chan] = LLVMBuildBitCast(gallivm->builder,
1325 address[chan],
1326 LLVMInt32TypeInContext(gallivm->context),
1327 "");
1328 }
1329
1330 /* Adjust the sample index according to FMASK.
1331 *
1332 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1333 * which is the identity mapping. Each nibble says which physical sample
1334 * should be fetched to get that sample.
1335 *
1336 * For example, 0x11111100 means there are only 2 samples stored and
1337 * the second sample covers 3/4 of the pixel. When reading samples 0
1338 * and 1, return physical sample 0 (determined by the first two 0s
1339 * in FMASK), otherwise return physical sample 1.
1340 *
1341 * The sample index should be adjusted as follows:
1342 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1343 */
1344 if (target == TGSI_TEXTURE_2D_MSAA ||
1345 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1346 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1347 struct lp_build_emit_data txf_emit_data = *emit_data;
1348 LLVMValueRef txf_address[4];
1349 unsigned txf_count = count;
1350
1351 memcpy(txf_address, address, sizeof(txf_address));
1352
1353 if (target == TGSI_TEXTURE_2D_MSAA) {
1354 txf_address[2] = bld_base->uint_bld.zero;
1355 }
1356 txf_address[3] = bld_base->uint_bld.zero;
1357
1358 /* Pad to a power-of-two size. */
1359 while (txf_count < util_next_power_of_two(txf_count))
1360 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1361
1362 /* Read FMASK using TXF. */
1363 txf_emit_data.chan = 0;
1364 txf_emit_data.dst_type = LLVMVectorType(
1365 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1366 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1367 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
1368 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1369 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
1370 txf_emit_data.arg_count = 3;
1371
1372 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1373
1374 /* Initialize some constants. */
1375 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1376 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1377
1378 /* Apply the formula. */
1379 LLVMValueRef fmask =
1380 LLVMBuildExtractElement(gallivm->builder,
1381 txf_emit_data.output[0],
1382 uint_bld->zero, "");
1383
1384 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
1385
1386 LLVMValueRef sample_index4 =
1387 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
1388
1389 LLVMValueRef shifted_fmask =
1390 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1391
1392 LLVMValueRef final_sample =
1393 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1394
1395 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1396 * resource descriptor is 0 (invalid),
1397 */
1398 LLVMValueRef fmask_desc =
1399 LLVMBuildBitCast(gallivm->builder,
1400 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1401 LLVMVectorType(uint_bld->elem_type, 8), "");
1402
1403 LLVMValueRef fmask_word1 =
1404 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1405 uint_bld->one, "");
1406
1407 LLVMValueRef word1_is_nonzero =
1408 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1409 fmask_word1, uint_bld->zero, "");
1410
1411 /* Replace the MSAA sample index. */
1412 address[sample_chan] =
1413 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
1414 final_sample, address[sample_chan], "");
1415 }
1416
1417 /* Resource */
1418 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
1419
1420 if (opcode == TGSI_OPCODE_TXF) {
1421 /* add tex offsets */
1422 if (inst->Texture.NumOffsets) {
1423 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1424 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1425 const struct tgsi_texture_offset * off = inst->TexOffsets;
1426
1427 assert(inst->Texture.NumOffsets == 1);
1428
1429 switch (target) {
1430 case TGSI_TEXTURE_3D:
1431 address[2] = lp_build_add(uint_bld, address[2],
1432 bld->immediates[off->Index][off->SwizzleZ]);
1433 /* fall through */
1434 case TGSI_TEXTURE_2D:
1435 case TGSI_TEXTURE_SHADOW2D:
1436 case TGSI_TEXTURE_RECT:
1437 case TGSI_TEXTURE_SHADOWRECT:
1438 case TGSI_TEXTURE_2D_ARRAY:
1439 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1440 address[1] =
1441 lp_build_add(uint_bld, address[1],
1442 bld->immediates[off->Index][off->SwizzleY]);
1443 /* fall through */
1444 case TGSI_TEXTURE_1D:
1445 case TGSI_TEXTURE_SHADOW1D:
1446 case TGSI_TEXTURE_1D_ARRAY:
1447 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1448 address[0] =
1449 lp_build_add(uint_bld, address[0],
1450 bld->immediates[off->Index][off->SwizzleX]);
1451 break;
1452 /* texture offsets do not apply to other texture targets */
1453 }
1454 }
1455
1456 emit_data->dst_type = LLVMVectorType(
1457 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1458 4);
1459
1460 emit_data->arg_count = 3;
1461 } else {
1462 /* Sampler */
1463 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1464
1465 emit_data->dst_type = LLVMVectorType(
1466 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1467 4);
1468
1469 emit_data->arg_count = 4;
1470 }
1471
1472 /* Dimensions */
1473 emit_data->args[emit_data->arg_count - 1] =
1474 lp_build_const_int32(bld_base->base.gallivm, target);
1475
1476 /* Pad to power of two vector */
1477 while (count < util_next_power_of_two(count))
1478 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1479
1480 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
1481 }
1482
1483 static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1484 struct lp_build_tgsi_context * bld_base,
1485 struct lp_build_emit_data * emit_data)
1486 {
1487 struct lp_build_context * base = &bld_base->base;
1488 char intr_name[127];
1489
1490 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1491 emit_data->output[emit_data->chan] = build_intrinsic(
1492 base->gallivm->builder,
1493 "llvm.SI.vs.load.input", emit_data->dst_type,
1494 emit_data->args, emit_data->arg_count,
1495 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1496 return;
1497 }
1498
1499 sprintf(intr_name, "%sv%ui32", action->intr_name,
1500 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1501
1502 emit_data->output[emit_data->chan] = build_intrinsic(
1503 base->gallivm->builder, intr_name, emit_data->dst_type,
1504 emit_data->args, emit_data->arg_count,
1505 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1506 }
1507
1508 static void txq_fetch_args(
1509 struct lp_build_tgsi_context * bld_base,
1510 struct lp_build_emit_data * emit_data)
1511 {
1512 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1513 const struct tgsi_full_instruction *inst = emit_data->inst;
1514 struct gallivm_state *gallivm = bld_base->base.gallivm;
1515
1516 if (inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1517 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1518 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1519
1520 /* Read the size from the buffer descriptor directly. */
1521 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1522 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1523 size = LLVMBuildExtractElement(gallivm->builder, size,
1524 lp_build_const_int32(gallivm, 2), "");
1525 emit_data->args[0] = size;
1526 return;
1527 }
1528
1529 /* Mip level */
1530 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1531
1532 /* Resource */
1533 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1534
1535 /* Dimensions */
1536 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1537 inst->Texture.Texture);
1538
1539 emit_data->arg_count = 3;
1540
1541 emit_data->dst_type = LLVMVectorType(
1542 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1543 4);
1544 }
1545
1546 static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
1547 struct lp_build_tgsi_context * bld_base,
1548 struct lp_build_emit_data * emit_data)
1549 {
1550 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1551 /* Just return the buffer size. */
1552 emit_data->output[emit_data->chan] = emit_data->args[0];
1553 return;
1554 }
1555
1556 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
1557 }
1558
1559 #if HAVE_LLVM >= 0x0304
1560
1561 static void si_llvm_emit_ddxy(
1562 const struct lp_build_tgsi_action * action,
1563 struct lp_build_tgsi_context * bld_base,
1564 struct lp_build_emit_data * emit_data)
1565 {
1566 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1567 struct gallivm_state *gallivm = bld_base->base.gallivm;
1568 struct lp_build_context * base = &bld_base->base;
1569 const struct tgsi_full_instruction *inst = emit_data->inst;
1570 unsigned opcode = inst->Instruction.Opcode;
1571 LLVMValueRef indices[2];
1572 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1573 LLVMValueRef tl, trbl, result[4];
1574 LLVMTypeRef i32;
1575 unsigned swizzle[4];
1576 unsigned c;
1577
1578 i32 = LLVMInt32TypeInContext(gallivm->context);
1579
1580 indices[0] = bld_base->uint_bld.zero;
1581 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1582 NULL, 0, LLVMReadNoneAttribute);
1583 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1584 indices, 2, "");
1585
1586 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1587 lp_build_const_int32(gallivm, 0xfffffffc), "");
1588 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1589 indices, 2, "");
1590
1591 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1592 lp_build_const_int32(gallivm,
1593 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1594 "");
1595 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1596 indices, 2, "");
1597
1598 for (c = 0; c < 4; ++c) {
1599 unsigned i;
1600
1601 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1602 for (i = 0; i < c; ++i) {
1603 if (swizzle[i] == swizzle[c]) {
1604 result[c] = result[i];
1605 break;
1606 }
1607 }
1608 if (i != c)
1609 continue;
1610
1611 LLVMBuildStore(gallivm->builder,
1612 LLVMBuildBitCast(gallivm->builder,
1613 lp_build_emit_fetch(bld_base, inst, 0, c),
1614 i32, ""),
1615 store_ptr);
1616
1617 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1618 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1619
1620 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1621 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1622
1623 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1624 }
1625
1626 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1627 }
1628
1629 #endif /* HAVE_LLVM >= 0x0304 */
1630
1631 static const struct lp_build_tgsi_action tex_action = {
1632 .fetch_args = tex_fetch_args,
1633 .emit = build_tex_intrinsic,
1634 .intr_name = "llvm.SI.sample."
1635 };
1636
1637 static const struct lp_build_tgsi_action txb_action = {
1638 .fetch_args = tex_fetch_args,
1639 .emit = build_tex_intrinsic,
1640 .intr_name = "llvm.SI.sampleb."
1641 };
1642
1643 #if HAVE_LLVM >= 0x0304
1644 static const struct lp_build_tgsi_action txd_action = {
1645 .fetch_args = tex_fetch_args,
1646 .emit = build_tex_intrinsic,
1647 .intr_name = "llvm.SI.sampled."
1648 };
1649 #endif
1650
1651 static const struct lp_build_tgsi_action txf_action = {
1652 .fetch_args = tex_fetch_args,
1653 .emit = build_tex_intrinsic,
1654 .intr_name = "llvm.SI.imageload."
1655 };
1656
1657 static const struct lp_build_tgsi_action txl_action = {
1658 .fetch_args = tex_fetch_args,
1659 .emit = build_tex_intrinsic,
1660 .intr_name = "llvm.SI.samplel."
1661 };
1662
1663 static const struct lp_build_tgsi_action txq_action = {
1664 .fetch_args = txq_fetch_args,
1665 .emit = build_txq_intrinsic,
1666 .intr_name = "llvm.SI.resinfo"
1667 };
1668
1669 static void create_meta_data(struct si_shader_context *si_shader_ctx)
1670 {
1671 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1672 LLVMValueRef args[3];
1673
1674 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1675 args[1] = 0;
1676 args[2] = lp_build_const_int32(gallivm, 1);
1677
1678 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1679 }
1680
1681 static void create_function(struct si_shader_context *si_shader_ctx)
1682 {
1683 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1684 struct gallivm_state *gallivm = bld_base->base.gallivm;
1685 LLVMTypeRef params[21], f32, i8, i32, v2i32, v3i32;
1686 unsigned i, last_sgpr, num_params;
1687
1688 i8 = LLVMInt8TypeInContext(gallivm->context);
1689 i32 = LLVMInt32TypeInContext(gallivm->context);
1690 f32 = LLVMFloatTypeInContext(gallivm->context);
1691 v2i32 = LLVMVectorType(i32, 2);
1692 v3i32 = LLVMVectorType(i32, 3);
1693
1694 params[SI_PARAM_CONST] = LLVMPointerType(
1695 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
1696 /* We assume at most 16 textures per program at the moment.
1697 * This need probably need to be changed to support bindless textures */
1698 params[SI_PARAM_SAMPLER] = LLVMPointerType(
1699 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
1700 params[SI_PARAM_RESOURCE] = LLVMPointerType(
1701 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
1702
1703 switch (si_shader_ctx->type) {
1704 case TGSI_PROCESSOR_VERTEX:
1705 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
1706 params[SI_PARAM_SO_BUFFER] = params[SI_PARAM_CONST];
1707 params[SI_PARAM_START_INSTANCE] = i32;
1708 num_params = SI_PARAM_START_INSTANCE+1;
1709
1710 /* The locations of the other parameters are assigned dynamically. */
1711
1712 /* Streamout SGPRs. */
1713 if (si_shader_ctx->shader->selector->so.num_outputs) {
1714 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
1715 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
1716 }
1717 /* A streamout buffer offset is loaded if the stride is non-zero. */
1718 for (i = 0; i < 4; i++) {
1719 if (!si_shader_ctx->shader->selector->so.stride[i])
1720 continue;
1721
1722 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
1723 }
1724
1725 last_sgpr = num_params-1;
1726
1727 /* VGPRs */
1728 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
1729 params[num_params++] = i32; /* unused*/
1730 params[num_params++] = i32; /* unused */
1731 params[si_shader_ctx->param_instance_id = num_params++] = i32;
1732 break;
1733
1734 case TGSI_PROCESSOR_FRAGMENT:
1735 params[SI_PARAM_ALPHA_REF] = f32;
1736 params[SI_PARAM_PRIM_MASK] = i32;
1737 last_sgpr = SI_PARAM_PRIM_MASK;
1738 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
1739 params[SI_PARAM_PERSP_CENTER] = v2i32;
1740 params[SI_PARAM_PERSP_CENTROID] = v2i32;
1741 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
1742 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
1743 params[SI_PARAM_LINEAR_CENTER] = v2i32;
1744 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
1745 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
1746 params[SI_PARAM_POS_X_FLOAT] = f32;
1747 params[SI_PARAM_POS_Y_FLOAT] = f32;
1748 params[SI_PARAM_POS_Z_FLOAT] = f32;
1749 params[SI_PARAM_POS_W_FLOAT] = f32;
1750 params[SI_PARAM_FRONT_FACE] = f32;
1751 params[SI_PARAM_ANCILLARY] = f32;
1752 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
1753 params[SI_PARAM_POS_FIXED_PT] = f32;
1754 num_params = SI_PARAM_POS_FIXED_PT+1;
1755 break;
1756
1757 default:
1758 assert(0 && "unimplemented shader");
1759 return;
1760 }
1761
1762 assert(num_params <= Elements(params));
1763 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
1764 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
1765
1766 for (i = 0; i <= last_sgpr; ++i) {
1767 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
1768 switch (i) {
1769 default:
1770 LLVMAddAttribute(P, LLVMInRegAttribute);
1771 break;
1772 #if HAVE_LLVM >= 0x0304
1773 /* We tell llvm that array inputs are passed by value to allow Sinking pass
1774 * to move load. Inputs are constant so this is fine. */
1775 case SI_PARAM_CONST:
1776 case SI_PARAM_SAMPLER:
1777 case SI_PARAM_RESOURCE:
1778 LLVMAddAttribute(P, LLVMByValAttribute);
1779 break;
1780 #endif
1781 }
1782 }
1783
1784 #if HAVE_LLVM >= 0x0304
1785 if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
1786 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0)
1787 si_shader_ctx->ddxy_lds =
1788 LLVMAddGlobalInAddressSpace(gallivm->module,
1789 LLVMArrayType(i32, 64),
1790 "ddxy_lds",
1791 LOCAL_ADDR_SPACE);
1792 #endif
1793 }
1794
1795 static void preload_constants(struct si_shader_context *si_shader_ctx)
1796 {
1797 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1798 struct gallivm_state * gallivm = bld_base->base.gallivm;
1799 const struct tgsi_shader_info * info = bld_base->info;
1800 unsigned buf;
1801 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1802
1803 for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) {
1804 unsigned i, num_const = info->const_file_max[buf] + 1;
1805
1806 if (num_const == 0)
1807 continue;
1808
1809 /* Allocate space for the constant values */
1810 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
1811
1812 /* Load the resource descriptor */
1813 si_shader_ctx->const_resource[buf] =
1814 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
1815
1816 /* Load the constants, we rely on the code sinking to do the rest */
1817 for (i = 0; i < num_const * 4; ++i) {
1818 LLVMValueRef args[2] = {
1819 si_shader_ctx->const_resource[buf],
1820 lp_build_const_int32(gallivm, i * 4)
1821 };
1822 si_shader_ctx->constants[buf][i] =
1823 build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1824 bld_base->base.elem_type, args, 2,
1825 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1826 }
1827 }
1828 }
1829
1830 static void preload_samplers(struct si_shader_context *si_shader_ctx)
1831 {
1832 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1833 struct gallivm_state * gallivm = bld_base->base.gallivm;
1834 const struct tgsi_shader_info * info = bld_base->info;
1835
1836 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1837
1838 LLVMValueRef res_ptr, samp_ptr;
1839 LLVMValueRef offset;
1840
1841 if (num_samplers == 0)
1842 return;
1843
1844 /* Allocate space for the values */
1845 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
1846 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1847
1848 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1849 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1850
1851 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1852 for (i = 0; i < num_samplers; ++i) {
1853 /* Resource */
1854 offset = lp_build_const_int32(gallivm, i);
1855 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1856
1857 /* Sampler */
1858 offset = lp_build_const_int32(gallivm, i);
1859 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
1860
1861 /* FMASK resource */
1862 if (info->is_msaa_sampler[i]) {
1863 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
1864 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
1865 build_indexed_load(si_shader_ctx, res_ptr, offset);
1866 }
1867 }
1868 }
1869
1870 static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
1871 {
1872 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1873 struct gallivm_state * gallivm = bld_base->base.gallivm;
1874 unsigned i;
1875
1876 if (!si_shader_ctx->shader->selector->so.num_outputs)
1877 return;
1878
1879 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1880 SI_PARAM_SO_BUFFER);
1881
1882 /* Load the resources, we rely on the code sinking to do the rest */
1883 for (i = 0; i < 4; ++i) {
1884 if (si_shader_ctx->shader->selector->so.stride[i]) {
1885 LLVMValueRef offset = lp_build_const_int32(gallivm, i);
1886
1887 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
1888 }
1889 }
1890 }
1891
1892 int si_compile_llvm(struct si_context *rctx, struct si_pipe_shader *shader,
1893 LLVMModuleRef mod)
1894 {
1895 unsigned i;
1896 uint32_t *ptr;
1897 struct radeon_llvm_binary binary;
1898 bool dump = r600_can_dump_shader(&rctx->screen->b,
1899 shader->selector ? shader->selector->tokens : NULL);
1900 memset(&binary, 0, sizeof(binary));
1901 radeon_llvm_compile(mod, &binary,
1902 r600_get_llvm_processor_name(rctx->screen->b.family), dump);
1903 if (dump && ! binary.disassembled) {
1904 fprintf(stderr, "SI CODE:\n");
1905 for (i = 0; i < binary.code_size; i+=4 ) {
1906 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
1907 binary.code[i + 2], binary.code[i + 1],
1908 binary.code[i]);
1909 }
1910 }
1911
1912 /* XXX: We may be able to emit some of these values directly rather than
1913 * extracting fields to be emitted later.
1914 */
1915 for (i = 0; i < binary.config_size; i+= 8) {
1916 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
1917 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
1918 switch (reg) {
1919 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
1920 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
1921 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
1922 case R_00B848_COMPUTE_PGM_RSRC1:
1923 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
1924 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
1925 break;
1926 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
1927 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
1928 break;
1929 case R_00B84C_COMPUTE_PGM_RSRC2:
1930 shader->lds_size = G_00B84C_LDS_SIZE(value);
1931 break;
1932 case R_0286CC_SPI_PS_INPUT_ENA:
1933 shader->spi_ps_input_ena = value;
1934 break;
1935 default:
1936 fprintf(stderr, "Warning: Compiler emitted unknown "
1937 "config register: 0x%x\n", reg);
1938 break;
1939 }
1940 }
1941
1942 /* copy new shader */
1943 r600_resource_reference(&shader->bo, NULL);
1944 shader->bo = si_resource_create_custom(rctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
1945 binary.code_size);
1946 if (shader->bo == NULL) {
1947 return -ENOMEM;
1948 }
1949
1950 ptr = (uint32_t*)rctx->b.ws->buffer_map(shader->bo->cs_buf, rctx->b.rings.gfx.cs, PIPE_TRANSFER_WRITE);
1951 if (0 /*R600_BIG_ENDIAN*/) {
1952 for (i = 0; i < binary.code_size / 4; ++i) {
1953 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
1954 }
1955 } else {
1956 memcpy(ptr, binary.code, binary.code_size);
1957 }
1958 rctx->b.ws->buffer_unmap(shader->bo->cs_buf);
1959
1960 free(binary.code);
1961 free(binary.config);
1962
1963 return 0;
1964 }
1965
1966 int si_pipe_shader_create(
1967 struct pipe_context *ctx,
1968 struct si_pipe_shader *shader)
1969 {
1970 struct si_context *rctx = (struct si_context*)ctx;
1971 struct si_pipe_shader_selector *sel = shader->selector;
1972 struct si_shader_context si_shader_ctx;
1973 struct tgsi_shader_info shader_info;
1974 struct lp_build_tgsi_context * bld_base;
1975 LLVMModuleRef mod;
1976 int r = 0;
1977 bool dump = r600_can_dump_shader(&rctx->screen->b, shader->selector->tokens);
1978
1979 assert(shader->shader.noutput == 0);
1980 assert(shader->shader.ninterp == 0);
1981 assert(shader->shader.ninput == 0);
1982
1983 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
1984 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1985 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1986
1987 tgsi_scan_shader(sel->tokens, &shader_info);
1988
1989 shader->shader.uses_kill = shader_info.uses_kill;
1990 shader->shader.uses_instanceid = shader_info.uses_instanceid;
1991 bld_base->info = &shader_info;
1992 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
1993 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1994
1995 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
1996 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
1997 #if HAVE_LLVM >= 0x0304
1998 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
1999 #endif
2000 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
2001 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
2002 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
2003 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
2004
2005 #if HAVE_LLVM >= 0x0304
2006 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2007 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
2008 #endif
2009
2010 si_shader_ctx.radeon_bld.load_input = declare_input;
2011 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
2012 si_shader_ctx.tokens = sel->tokens;
2013 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2014 si_shader_ctx.shader = shader;
2015 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
2016
2017 create_meta_data(&si_shader_ctx);
2018 create_function(&si_shader_ctx);
2019 preload_constants(&si_shader_ctx);
2020 preload_samplers(&si_shader_ctx);
2021 preload_streamout_buffers(&si_shader_ctx);
2022
2023 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2024 * conversion fails. */
2025 if (dump) {
2026 tgsi_dump(sel->tokens, 0);
2027 si_dump_streamout(&sel->so);
2028 }
2029
2030 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
2031 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
2032 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2033 FREE(si_shader_ctx.constants[i]);
2034 FREE(si_shader_ctx.resources);
2035 FREE(si_shader_ctx.samplers);
2036 return -EINVAL;
2037 }
2038
2039 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2040
2041 mod = bld_base->base.gallivm->module;
2042 r = si_compile_llvm(rctx, shader, mod);
2043
2044 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
2045 tgsi_parse_free(&si_shader_ctx.parse);
2046
2047 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2048 FREE(si_shader_ctx.constants[i]);
2049 FREE(si_shader_ctx.resources);
2050 FREE(si_shader_ctx.samplers);
2051
2052 return r;
2053 }
2054
2055 void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
2056 {
2057 r600_resource_reference(&shader->bo, NULL);
2058 }