2 * Copyright 2012 Advanced Micro Devices, Inc.
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:
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
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.
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
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 "radeon_llvm.h"
36 #include "radeon_llvm_emit.h"
37 #include "util/u_memory.h"
38 #include "tgsi/tgsi_parse.h"
39 #include "tgsi/tgsi_util.h"
40 #include "tgsi/tgsi_dump.h"
43 #include "si_shader.h"
48 struct si_shader_output_values
50 LLVMValueRef values
[4];
57 struct si_shader_context
59 struct radeon_llvm_context radeon_bld
;
60 struct tgsi_parse_context parse
;
61 struct tgsi_token
* tokens
;
62 struct si_pipe_shader
*shader
;
63 struct si_shader
*gs_for_vs
;
64 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
65 int param_streamout_config
;
66 int param_streamout_write_index
;
67 int param_streamout_offset
[4];
69 int param_instance_id
;
70 LLVMValueRef const_md
;
71 LLVMValueRef const_resource
[NUM_CONST_BUFFERS
];
72 #if HAVE_LLVM >= 0x0304
73 LLVMValueRef ddxy_lds
;
75 LLVMValueRef
*constants
[NUM_CONST_BUFFERS
];
76 LLVMValueRef
*resources
;
77 LLVMValueRef
*samplers
;
78 LLVMValueRef so_buffers
[4];
79 LLVMValueRef gs_next_vertex
;
82 static struct si_shader_context
* si_shader_context(
83 struct lp_build_tgsi_context
* bld_base
)
85 return (struct si_shader_context
*)bld_base
;
89 #define PERSPECTIVE_BASE 0
92 #define SAMPLE_OFFSET 0
93 #define CENTER_OFFSET 2
94 #define CENTROID_OFSET 4
96 #define USE_SGPR_MAX_SUFFIX_LEN 5
97 #define CONST_ADDR_SPACE 2
98 #define LOCAL_ADDR_SPACE 3
99 #define USER_SGPR_ADDR_SPACE 8
103 #define SENDMSG_GS_DONE 3
105 #define SENDMSG_GS_OP_NOP (0 << 4)
106 #define SENDMSG_GS_OP_CUT (1 << 4)
107 #define SENDMSG_GS_OP_EMIT (2 << 4)
108 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
112 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
114 * @param offset The offset parameter specifies the number of
115 * elements to offset, not the number of bytes or dwords. An element is the
116 * the type pointed to by the base_ptr parameter (e.g. int is the element of
119 * When LLVM lowers the load instruction, it will convert the element offset
120 * into a dword offset automatically.
123 static LLVMValueRef
build_indexed_load(
124 struct si_shader_context
* si_shader_ctx
,
125 LLVMValueRef base_ptr
,
128 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
130 LLVMValueRef indices
[2] = {
131 LLVMConstInt(LLVMInt64TypeInContext(base
->gallivm
->context
), 0, false),
134 LLVMValueRef computed_ptr
= LLVMBuildGEP(
135 base
->gallivm
->builder
, base_ptr
, indices
, 2, "");
137 LLVMValueRef result
= LLVMBuildLoad(base
->gallivm
->builder
, computed_ptr
, "");
138 LLVMSetMetadata(result
, 1, si_shader_ctx
->const_md
);
142 static LLVMValueRef
get_instance_index_for_fetch(
143 struct radeon_llvm_context
* radeon_bld
,
146 struct si_shader_context
*si_shader_ctx
=
147 si_shader_context(&radeon_bld
->soa
.bld_base
);
148 struct gallivm_state
* gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
150 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
151 si_shader_ctx
->param_instance_id
);
152 result
= LLVMBuildAdd(gallivm
->builder
, result
, LLVMGetParam(
153 radeon_bld
->main_fn
, SI_PARAM_START_INSTANCE
), "");
156 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
157 lp_build_const_int32(gallivm
, divisor
), "");
162 static int si_store_shader_io_attribs(struct si_shader
*shader
,
163 const struct tgsi_full_declaration
*d
)
167 switch (d
->Declaration
.File
) {
168 case TGSI_FILE_INPUT
:
169 i
= shader
->ninput
++;
170 assert(i
< Elements(shader
->input
));
171 shader
->input
[i
].name
= d
->Semantic
.Name
;
172 shader
->input
[i
].sid
= d
->Semantic
.Index
;
173 shader
->input
[i
].index
= d
->Range
.First
;
174 shader
->input
[i
].interpolate
= d
->Interp
.Interpolate
;
175 shader
->input
[i
].centroid
= d
->Interp
.Centroid
;
178 case TGSI_FILE_OUTPUT
:
179 i
= shader
->noutput
++;
180 assert(i
< Elements(shader
->output
));
181 shader
->output
[i
].name
= d
->Semantic
.Name
;
182 shader
->output
[i
].sid
= d
->Semantic
.Index
;
183 shader
->output
[i
].index
= d
->Range
.First
;
184 shader
->output
[i
].usage
= d
->Declaration
.UsageMask
;
191 static void declare_input_vs(
192 struct radeon_llvm_context
*radeon_bld
,
193 unsigned input_index
,
194 const struct tgsi_full_declaration
*decl
)
196 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
197 struct gallivm_state
*gallivm
= base
->gallivm
;
198 struct si_shader_context
*si_shader_ctx
=
199 si_shader_context(&radeon_bld
->soa
.bld_base
);
200 unsigned divisor
= si_shader_ctx
->shader
->key
.vs
.instance_divisors
[input_index
];
204 LLVMValueRef t_list_ptr
;
205 LLVMValueRef t_offset
;
207 LLVMValueRef attribute_offset
;
208 LLVMValueRef buffer_index
;
209 LLVMValueRef args
[3];
210 LLVMTypeRef vec4_type
;
213 /* Load the T list */
214 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFER
);
216 t_offset
= lp_build_const_int32(gallivm
, input_index
);
218 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
, t_offset
);
220 /* Build the attribute offset */
221 attribute_offset
= lp_build_const_int32(gallivm
, 0);
224 /* Build index from instance ID, start instance and divisor */
225 si_shader_ctx
->shader
->shader
.uses_instanceid
= true;
226 buffer_index
= get_instance_index_for_fetch(&si_shader_ctx
->radeon_bld
, divisor
);
228 /* Load the buffer index, which is always stored in VGPR0
229 * for Vertex Shaders */
230 buffer_index
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
231 si_shader_ctx
->param_vertex_id
);
234 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
236 args
[1] = attribute_offset
;
237 args
[2] = buffer_index
;
238 input
= build_intrinsic(gallivm
->builder
,
239 "llvm.SI.vs.load.input", vec4_type
, args
, 3,
240 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
242 /* Break up the vec4 into individual components */
243 for (chan
= 0; chan
< 4; chan
++) {
244 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
245 /* XXX: Use a helper function for this. There is one in
247 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
248 LLVMBuildExtractElement(gallivm
->builder
,
249 input
, llvm_chan
, "");
253 static void declare_input_gs(
254 struct radeon_llvm_context
*radeon_bld
,
255 unsigned input_index
,
256 const struct tgsi_full_declaration
*decl
)
258 struct si_shader_context
*si_shader_ctx
=
259 si_shader_context(&radeon_bld
->soa
.bld_base
);
260 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
262 si_store_shader_io_attribs(shader
, decl
);
264 if (decl
->Semantic
.Name
!= TGSI_SEMANTIC_PRIMID
)
265 shader
->input
[input_index
].param_offset
= shader
->nparam
++;
268 static LLVMValueRef
fetch_input_gs(
269 struct lp_build_tgsi_context
*bld_base
,
270 const struct tgsi_full_src_register
*reg
,
271 enum tgsi_opcode_type type
,
274 struct lp_build_context
*base
= &bld_base
->base
;
275 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
276 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
277 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
278 struct gallivm_state
*gallivm
= base
->gallivm
;
279 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
280 LLVMValueRef vtx_offset
;
281 LLVMValueRef t_list_ptr
;
283 LLVMValueRef args
[9];
284 unsigned vtx_offset_param
;
287 shader
->input
[reg
->Register
.Index
].name
== TGSI_SEMANTIC_PRIMID
) {
289 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
290 SI_PARAM_PRIMITIVE_ID
);
295 if (!reg
->Register
.Dimension
)
299 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
301 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
302 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
304 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
308 /* Get the vertex offset parameter */
309 vtx_offset_param
= reg
->Dimension
.Index
;
310 if (vtx_offset_param
< 2) {
311 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
313 assert(vtx_offset_param
< 6);
314 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
316 vtx_offset
= lp_build_mul_imm(uint
,
317 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
321 /* Load the ESGS ring resource descriptor */
322 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
323 SI_PARAM_RW_BUFFERS
);
324 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
325 lp_build_const_int32(gallivm
, SI_RING_ESGS
));
328 args
[1] = vtx_offset
;
329 args
[2] = lp_build_const_int32(gallivm
,
330 ((shader
->input
[reg
->Register
.Index
].param_offset
* 4) +
332 args
[3] = uint
->zero
;
333 args
[4] = uint
->one
; /* OFFEN */
334 args
[5] = uint
->zero
; /* IDXEN */
335 args
[6] = uint
->one
; /* GLC */
336 args
[7] = uint
->zero
; /* SLC */
337 args
[8] = uint
->zero
; /* TFE */
339 return LLVMBuildBitCast(gallivm
->builder
,
340 build_intrinsic(gallivm
->builder
,
341 "llvm.SI.buffer.load.dword.i32.i32",
343 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
344 tgsi2llvmtype(bld_base
, type
), "");
347 static void declare_input_fs(
348 struct radeon_llvm_context
*radeon_bld
,
349 unsigned input_index
,
350 const struct tgsi_full_declaration
*decl
)
352 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
353 struct si_shader_context
*si_shader_ctx
=
354 si_shader_context(&radeon_bld
->soa
.bld_base
);
355 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
356 struct lp_build_context
*uint
= &radeon_bld
->soa
.bld_base
.uint_bld
;
357 struct gallivm_state
*gallivm
= base
->gallivm
;
358 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
359 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
361 LLVMValueRef interp_param
;
362 const char * intr_name
;
365 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
366 * quad begins a new primitive. Bit 0 always needs
368 * [32:16] ParamOffset
371 LLVMValueRef params
= LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
);
372 LLVMValueRef attr_number
;
376 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
377 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
379 radeon_llvm_reg_index_soa(input_index
, chan
);
380 radeon_bld
->inputs
[soa_index
] =
381 LLVMGetParam(main_fn
, SI_PARAM_POS_X_FLOAT
+ chan
);
384 /* RCP for fragcoord.w */
385 radeon_bld
->inputs
[soa_index
] =
386 LLVMBuildFDiv(gallivm
->builder
,
387 lp_build_const_float(gallivm
, 1.0f
),
388 radeon_bld
->inputs
[soa_index
],
394 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
395 LLVMValueRef face
, is_face_positive
;
397 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
399 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
401 lp_build_const_float(gallivm
, 0.0f
),
404 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
405 LLVMBuildSelect(gallivm
->builder
,
407 lp_build_const_float(gallivm
, 1.0f
),
408 lp_build_const_float(gallivm
, 0.0f
),
410 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
411 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
412 lp_build_const_float(gallivm
, 0.0f
);
413 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
414 lp_build_const_float(gallivm
, 1.0f
);
419 shader
->input
[input_index
].param_offset
= shader
->nparam
++;
420 attr_number
= lp_build_const_int32(gallivm
,
421 shader
->input
[input_index
].param_offset
);
423 switch (decl
->Interp
.Interpolate
) {
424 case TGSI_INTERPOLATE_COLOR
:
425 if (si_shader_ctx
->shader
->key
.ps
.flatshade
) {
428 if (decl
->Interp
.Centroid
)
429 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
431 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
434 case TGSI_INTERPOLATE_CONSTANT
:
437 case TGSI_INTERPOLATE_LINEAR
:
438 if (decl
->Interp
.Centroid
)
439 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTROID
);
441 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTER
);
443 case TGSI_INTERPOLATE_PERSPECTIVE
:
444 if (decl
->Interp
.Centroid
)
445 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
447 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
450 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
454 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
456 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
457 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
458 si_shader_ctx
->shader
->key
.ps
.color_two_side
) {
459 LLVMValueRef args
[4];
460 LLVMValueRef face
, is_face_positive
;
461 LLVMValueRef back_attr_number
=
462 lp_build_const_int32(gallivm
,
463 shader
->input
[input_index
].param_offset
+ 1);
465 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
467 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
469 lp_build_const_float(gallivm
, 0.0f
),
473 args
[3] = interp_param
;
474 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
475 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
476 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
477 LLVMValueRef front
, back
;
480 args
[1] = attr_number
;
481 front
= build_intrinsic(gallivm
->builder
, intr_name
,
482 input_type
, args
, args
[3] ? 4 : 3,
483 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
485 args
[1] = back_attr_number
;
486 back
= build_intrinsic(gallivm
->builder
, intr_name
,
487 input_type
, args
, args
[3] ? 4 : 3,
488 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
490 radeon_bld
->inputs
[soa_index
] =
491 LLVMBuildSelect(gallivm
->builder
,
499 } else if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FOG
) {
500 LLVMValueRef args
[4];
502 args
[0] = uint
->zero
;
503 args
[1] = attr_number
;
505 args
[3] = interp_param
;
506 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
507 build_intrinsic(gallivm
->builder
, intr_name
,
508 input_type
, args
, args
[3] ? 4 : 3,
509 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
510 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
511 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
512 lp_build_const_float(gallivm
, 0.0f
);
513 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
514 lp_build_const_float(gallivm
, 1.0f
);
516 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
517 LLVMValueRef args
[4];
518 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
519 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
521 args
[1] = attr_number
;
523 args
[3] = interp_param
;
524 radeon_bld
->inputs
[soa_index
] =
525 build_intrinsic(gallivm
->builder
, intr_name
,
526 input_type
, args
, args
[3] ? 4 : 3,
527 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
532 static void declare_system_value(
533 struct radeon_llvm_context
* radeon_bld
,
535 const struct tgsi_full_declaration
*decl
)
537 struct si_shader_context
*si_shader_ctx
=
538 si_shader_context(&radeon_bld
->soa
.bld_base
);
539 LLVMValueRef value
= 0;
541 switch (decl
->Semantic
.Name
) {
542 case TGSI_SEMANTIC_INSTANCEID
:
543 value
= LLVMGetParam(radeon_bld
->main_fn
,
544 si_shader_ctx
->param_instance_id
);
547 case TGSI_SEMANTIC_VERTEXID
:
548 value
= LLVMGetParam(radeon_bld
->main_fn
,
549 si_shader_ctx
->param_vertex_id
);
553 assert(!"unknown system value");
557 radeon_bld
->system_values
[index
] = value
;
560 static LLVMValueRef
fetch_constant(
561 struct lp_build_tgsi_context
* bld_base
,
562 const struct tgsi_full_src_register
*reg
,
563 enum tgsi_opcode_type type
,
566 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
567 struct lp_build_context
* base
= &bld_base
->base
;
568 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
571 LLVMValueRef args
[2];
575 if (swizzle
== LP_CHAN_ALL
) {
577 LLVMValueRef values
[4];
578 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
579 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
581 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
584 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
585 idx
= reg
->Register
.Index
* 4 + swizzle
;
587 if (!reg
->Register
.Indirect
)
588 return bitcast(bld_base
, type
, si_shader_ctx
->constants
[buf
][idx
]);
590 args
[0] = si_shader_ctx
->const_resource
[buf
];
591 args
[1] = lp_build_const_int32(base
->gallivm
, idx
* 4);
592 addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
593 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
594 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
595 args
[1] = lp_build_add(&bld_base
->uint_bld
, addr
, args
[1]);
597 result
= build_intrinsic(base
->gallivm
->builder
, "llvm.SI.load.const", base
->elem_type
,
598 args
, 2, LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
600 return bitcast(bld_base
, type
, result
);
603 /* Initialize arguments for the shader export intrinsic */
604 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
605 LLVMValueRef
*values
,
609 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
610 struct lp_build_context
*uint
=
611 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
612 struct lp_build_context
*base
= &bld_base
->base
;
613 unsigned compressed
= 0;
616 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
617 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
619 if (cbuf
>= 0 && cbuf
< 8) {
620 compressed
= (si_shader_ctx
->shader
->key
.ps
.export_16bpc
>> cbuf
) & 0x1;
623 si_shader_ctx
->shader
->spi_shader_col_format
|=
624 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
626 si_shader_ctx
->shader
->spi_shader_col_format
|=
627 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
629 si_shader_ctx
->shader
->cb_shader_mask
|= 0xf << (4 * cbuf
);
634 /* Pixel shader needs to pack output values before export */
635 for (chan
= 0; chan
< 2; chan
++ ) {
636 args
[0] = values
[2 * chan
];
637 args
[1] = values
[2 * chan
+ 1];
639 build_intrinsic(base
->gallivm
->builder
,
641 LLVMInt32TypeInContext(base
->gallivm
->context
),
643 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
644 args
[chan
+ 7] = args
[chan
+ 5] =
645 LLVMBuildBitCast(base
->gallivm
->builder
,
647 LLVMFloatTypeInContext(base
->gallivm
->context
),
654 for (chan
= 0; chan
< 4; chan
++ )
655 /* +5 because the first output value will be
656 * the 6th argument to the intrinsic. */
657 args
[chan
+ 5] = values
[chan
];
659 /* Clear COMPR flag */
660 args
[4] = uint
->zero
;
663 /* XXX: This controls which components of the output
664 * registers actually get exported. (e.g bit 0 means export
665 * X component, bit 1 means export Y component, etc.) I'm
666 * hard coding this to 0xf for now. In the future, we might
667 * want to do something else. */
668 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
670 /* Specify whether the EXEC mask represents the valid mask */
671 args
[1] = uint
->zero
;
673 /* Specify whether this is the last export */
674 args
[2] = uint
->zero
;
676 /* Specify the target we are exporting */
677 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
679 /* XXX: We probably need to keep track of the output
680 * values, so we know what we are passing to the next
684 /* Load from output pointers and initialize arguments for the shader export intrinsic */
685 static void si_llvm_init_export_args_load(struct lp_build_tgsi_context
*bld_base
,
686 LLVMValueRef
*out_ptr
,
690 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
691 LLVMValueRef values
[4];
694 for (i
= 0; i
< 4; i
++)
695 values
[i
] = LLVMBuildLoad(gallivm
->builder
, out_ptr
[i
], "");
697 si_llvm_init_export_args(bld_base
, values
, target
, args
);
700 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
701 LLVMValueRef
*out_ptr
)
703 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
704 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
706 if (si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_NEVER
) {
707 LLVMValueRef alpha_ref
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
710 LLVMValueRef alpha_pass
=
711 lp_build_cmp(&bld_base
->base
,
712 si_shader_ctx
->shader
->key
.ps
.alpha_func
,
713 LLVMBuildLoad(gallivm
->builder
, out_ptr
[3], ""),
716 lp_build_select(&bld_base
->base
,
718 lp_build_const_float(gallivm
, 1.0f
),
719 lp_build_const_float(gallivm
, -1.0f
));
721 build_intrinsic(gallivm
->builder
,
723 LLVMVoidTypeInContext(gallivm
->context
),
726 build_intrinsic(gallivm
->builder
,
728 LLVMVoidTypeInContext(gallivm
->context
),
733 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
* bld_base
,
734 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
736 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
737 struct si_pipe_shader
*shader
= si_shader_ctx
->shader
;
738 struct lp_build_context
*base
= &bld_base
->base
;
739 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
743 LLVMValueRef base_elt
;
744 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
745 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
, NUM_PIPE_CONST_BUFFERS
);
746 LLVMValueRef const_resource
= build_indexed_load(si_shader_ctx
, ptr
, constbuf_index
);
748 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
749 LLVMValueRef
*args
= pos
[2 + reg_index
];
751 if (!(shader
->key
.vs
.ucps_enabled
& (1 << reg_index
)))
754 shader
->shader
.clip_dist_write
|= 0xf << (4 * reg_index
);
759 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
761 /* Compute dot products of position and user clip plane vectors */
762 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
763 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
764 args
[0] = const_resource
;
765 args
[1] = lp_build_const_int32(base
->gallivm
,
766 ((reg_index
* 4 + chan
) * 4 +
768 base_elt
= build_intrinsic(base
->gallivm
->builder
,
769 "llvm.SI.load.const",
772 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
774 lp_build_add(base
, args
[5 + chan
],
775 lp_build_mul(base
, base_elt
,
776 out_elts
[const_chan
]));
780 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
781 args
[1] = uint
->zero
;
782 args
[2] = uint
->zero
;
783 args
[3] = lp_build_const_int32(base
->gallivm
,
784 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
785 args
[4] = uint
->zero
;
789 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
794 fprintf(stderr
, "STREAMOUT\n");
796 for (i
= 0; i
< so
->num_outputs
; i
++) {
797 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
798 so
->output
[i
].start_component
;
799 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
800 i
, so
->output
[i
].output_buffer
,
801 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
802 so
->output
[i
].register_index
,
806 mask
& 8 ? "w" : "");
810 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
811 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
812 * or v4i32 (num_channels=3,4). */
813 static void build_tbuffer_store(struct si_shader_context
*shader
,
816 unsigned num_channels
,
818 LLVMValueRef soffset
,
819 unsigned inst_offset
,
828 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
829 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
830 LLVMValueRef args
[] = {
833 LLVMConstInt(i32
, num_channels
, 0),
836 LLVMConstInt(i32
, inst_offset
, 0),
837 LLVMConstInt(i32
, dfmt
, 0),
838 LLVMConstInt(i32
, nfmt
, 0),
839 LLVMConstInt(i32
, offen
, 0),
840 LLVMConstInt(i32
, idxen
, 0),
841 LLVMConstInt(i32
, glc
, 0),
842 LLVMConstInt(i32
, slc
, 0),
843 LLVMConstInt(i32
, tfe
, 0)
846 /* The instruction offset field has 12 bits */
847 assert(offen
|| inst_offset
< (1 << 12));
849 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
850 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
851 const char *types
[] = {"i32", "v2i32", "v4i32"};
853 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
855 lp_build_intrinsic(gallivm
->builder
, name
,
856 LLVMVoidTypeInContext(gallivm
->context
),
857 args
, Elements(args
));
860 static void build_streamout_store(struct si_shader_context
*shader
,
863 unsigned num_channels
,
865 LLVMValueRef soffset
,
866 unsigned inst_offset
)
868 static unsigned dfmt
[] = {
869 V_008F0C_BUF_DATA_FORMAT_32
,
870 V_008F0C_BUF_DATA_FORMAT_32_32
,
871 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
872 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
874 assert(num_channels
>= 1 && num_channels
<= 4);
876 build_tbuffer_store(shader
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
877 inst_offset
, dfmt
[num_channels
-1],
878 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
881 /* On SI, the vertex shader is responsible for writing streamout data
883 static void si_llvm_emit_streamout(struct si_shader_context
*shader
,
884 struct si_shader_output_values
*outputs
,
887 struct pipe_stream_output_info
*so
= &shader
->shader
->selector
->so
;
888 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
889 LLVMBuilderRef builder
= gallivm
->builder
;
891 struct lp_build_if_state if_ctx
;
893 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
895 LLVMValueRef so_param
=
896 LLVMGetParam(shader
->radeon_bld
.main_fn
,
897 shader
->param_streamout_config
);
899 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
900 LLVMValueRef so_vtx_count
=
901 LLVMBuildAnd(builder
,
902 LLVMBuildLShr(builder
, so_param
,
903 LLVMConstInt(i32
, 16, 0), ""),
904 LLVMConstInt(i32
, 127, 0), "");
906 LLVMValueRef tid
= build_intrinsic(builder
, "llvm.SI.tid", i32
,
907 NULL
, 0, LLVMReadNoneAttribute
);
909 /* can_emit = tid < so_vtx_count; */
910 LLVMValueRef can_emit
=
911 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
913 /* Emit the streamout code conditionally. This actually avoids
914 * out-of-bounds buffer access. The hw tells us via the SGPR
915 * (so_vtx_count) which threads are allowed to emit streamout data. */
916 lp_build_if(&if_ctx
, gallivm
, can_emit
);
918 /* The buffer offset is computed as follows:
919 * ByteOffset = streamout_offset[buffer_id]*4 +
920 * (streamout_write_index + thread_id)*stride[buffer_id] +
924 LLVMValueRef so_write_index
=
925 LLVMGetParam(shader
->radeon_bld
.main_fn
,
926 shader
->param_streamout_write_index
);
928 /* Compute (streamout_write_index + thread_id). */
929 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
931 /* Compute the write offset for each enabled buffer. */
932 LLVMValueRef so_write_offset
[4] = {};
933 for (i
= 0; i
< 4; i
++) {
937 LLVMValueRef so_offset
= LLVMGetParam(shader
->radeon_bld
.main_fn
,
938 shader
->param_streamout_offset
[i
]);
939 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(i32
, 4, 0), "");
941 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
942 LLVMConstInt(i32
, so
->stride
[i
]*4, 0), "");
943 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
946 /* Write streamout data. */
947 for (i
= 0; i
< so
->num_outputs
; i
++) {
948 unsigned buf_idx
= so
->output
[i
].output_buffer
;
949 unsigned reg
= so
->output
[i
].register_index
;
950 unsigned start
= so
->output
[i
].start_component
;
951 unsigned num_comps
= so
->output
[i
].num_components
;
954 assert(num_comps
&& num_comps
<= 4);
955 if (!num_comps
|| num_comps
> 4)
958 /* Load the output as int. */
959 for (j
= 0; j
< num_comps
; j
++) {
962 while (outidx
< noutput
&& outputs
[outidx
].index
!= reg
)
965 if (outidx
< noutput
)
966 out
[j
] = LLVMBuildBitCast(builder
,
967 outputs
[outidx
].values
[start
+j
],
976 /* Pack the output. */
977 LLVMValueRef vdata
= NULL
;
983 case 2: /* as v2i32 */
984 case 3: /* as v4i32 (aligned to 4) */
985 case 4: /* as v4i32 */
986 vdata
= LLVMGetUndef(LLVMVectorType(i32
, util_next_power_of_two(num_comps
)));
987 for (j
= 0; j
< num_comps
; j
++) {
988 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
989 LLVMConstInt(i32
, j
, 0), "");
994 build_streamout_store(shader
, shader
->so_buffers
[buf_idx
],
996 so_write_offset
[buf_idx
],
997 LLVMConstInt(i32
, 0, 0),
998 so
->output
[i
].dst_offset
*4);
1001 lp_build_endif(&if_ctx
);
1005 /* Generate export instructions for hardware VS shader stage */
1006 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
1007 struct si_shader_output_values
*outputs
,
1010 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1011 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
1012 struct lp_build_context
* base
= &bld_base
->base
;
1013 struct lp_build_context
* uint
=
1014 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1015 LLVMValueRef args
[9];
1016 LLVMValueRef pos_args
[4][9] = { { 0 } };
1017 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
;
1018 unsigned semantic_name
, semantic_index
, semantic_usage
;
1020 unsigned param_count
= 0;
1024 if (outputs
&& si_shader_ctx
->shader
->selector
->so
.num_outputs
) {
1025 si_llvm_emit_streamout(si_shader_ctx
, outputs
, noutput
);
1028 for (i
= 0; i
< noutput
; i
++) {
1029 semantic_name
= outputs
[i
].name
;
1030 semantic_index
= outputs
[i
].sid
;
1031 semantic_usage
= outputs
[i
].usage
;
1034 /* Select the correct target */
1035 switch(semantic_name
) {
1036 case TGSI_SEMANTIC_PSIZE
:
1037 shader
->vs_out_misc_write
= true;
1038 shader
->vs_out_point_size
= true;
1039 psize_value
= outputs
[i
].values
[0];
1041 case TGSI_SEMANTIC_EDGEFLAG
:
1042 shader
->vs_out_misc_write
= true;
1043 shader
->vs_out_edgeflag
= true;
1044 edgeflag_value
= outputs
[i
].values
[0];
1046 case TGSI_SEMANTIC_LAYER
:
1047 shader
->vs_out_misc_write
= true;
1048 shader
->vs_out_layer
= true;
1049 layer_value
= outputs
[i
].values
[0];
1051 case TGSI_SEMANTIC_POSITION
:
1052 target
= V_008DFC_SQ_EXP_POS
;
1054 case TGSI_SEMANTIC_COLOR
:
1055 case TGSI_SEMANTIC_BCOLOR
:
1056 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1057 shader
->output
[i
].param_offset
= param_count
;
1060 case TGSI_SEMANTIC_CLIPDIST
:
1061 if (!(si_shader_ctx
->shader
->key
.vs
.ucps_enabled
&
1062 (1 << semantic_index
)))
1064 shader
->clip_dist_write
|=
1065 semantic_usage
<< (semantic_index
<< 2);
1066 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
1068 case TGSI_SEMANTIC_CLIPVERTEX
:
1069 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
1071 case TGSI_SEMANTIC_PRIMID
:
1072 case TGSI_SEMANTIC_FOG
:
1073 case TGSI_SEMANTIC_GENERIC
:
1074 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1075 shader
->output
[i
].param_offset
= param_count
;
1081 "Warning: SI unhandled vs output type:%d\n",
1085 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
1087 if (target
>= V_008DFC_SQ_EXP_POS
&&
1088 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1089 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1090 args
, sizeof(args
));
1092 lp_build_intrinsic(base
->gallivm
->builder
,
1094 LLVMVoidTypeInContext(base
->gallivm
->context
),
1098 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1099 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1100 goto handle_semantic
;
1104 /* We need to add the position output manually if it's missing. */
1105 if (!pos_args
[0][0]) {
1106 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1107 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1108 pos_args
[0][2] = uint
->zero
; /* last export? */
1109 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1110 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1111 pos_args
[0][5] = base
->zero
; /* X */
1112 pos_args
[0][6] = base
->zero
; /* Y */
1113 pos_args
[0][7] = base
->zero
; /* Z */
1114 pos_args
[0][8] = base
->one
; /* W */
1117 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1118 if (shader
->vs_out_misc_write
) {
1119 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
1120 shader
->vs_out_point_size
|
1121 (shader
->vs_out_edgeflag
<< 1) |
1122 (shader
->vs_out_layer
<< 2));
1123 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
1124 pos_args
[1][2] = uint
->zero
; /* last export? */
1125 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
1126 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
1127 pos_args
[1][5] = base
->zero
; /* X */
1128 pos_args
[1][6] = base
->zero
; /* Y */
1129 pos_args
[1][7] = base
->zero
; /* Z */
1130 pos_args
[1][8] = base
->zero
; /* W */
1132 if (shader
->vs_out_point_size
)
1133 pos_args
[1][5] = psize_value
;
1135 if (shader
->vs_out_edgeflag
) {
1136 /* The output is a float, but the hw expects an integer
1137 * with the first bit containing the edge flag. */
1138 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
1140 bld_base
->uint_bld
.elem_type
, "");
1141 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
1143 bld_base
->int_bld
.one
);
1145 /* The LLVM intrinsic expects a float. */
1146 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
1148 base
->elem_type
, "");
1151 if (shader
->vs_out_layer
)
1152 pos_args
[1][7] = layer_value
;
1155 for (i
= 0; i
< 4; i
++)
1157 shader
->nr_pos_exports
++;
1160 for (i
= 0; i
< 4; i
++) {
1161 if (!pos_args
[i
][0])
1164 /* Specify the target we are exporting */
1165 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
1167 if (pos_idx
== shader
->nr_pos_exports
)
1168 /* Specify that this is the last export */
1169 pos_args
[i
][2] = uint
->one
;
1171 lp_build_intrinsic(base
->gallivm
->builder
,
1173 LLVMVoidTypeInContext(base
->gallivm
->context
),
1178 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
* bld_base
)
1180 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1181 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1182 struct si_shader
*es
= &si_shader_ctx
->shader
->shader
;
1183 struct si_shader
*gs
= si_shader_ctx
->gs_for_vs
;
1184 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1185 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1186 LLVMValueRef soffset
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1187 SI_PARAM_ES2GS_OFFSET
);
1188 LLVMValueRef t_list_ptr
;
1189 LLVMValueRef t_list
;
1193 while (!tgsi_parse_end_of_tokens(parse
)) {
1194 struct tgsi_full_declaration
*d
=
1195 &parse
->FullToken
.FullDeclaration
;
1197 tgsi_parse_token(parse
);
1199 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
1202 si_store_shader_io_attribs(es
, d
);
1205 /* Load the ESGS ring resource descriptor */
1206 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1207 SI_PARAM_RW_BUFFERS
);
1208 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
1209 lp_build_const_int32(gallivm
, SI_RING_ESGS
));
1211 for (i
= 0; i
< es
->noutput
; i
++) {
1212 LLVMValueRef
*out_ptr
=
1213 si_shader_ctx
->radeon_bld
.soa
.outputs
[es
->output
[i
].index
];
1216 for (j
= 0; j
< gs
->ninput
; j
++) {
1217 if (gs
->input
[j
].name
== es
->output
[i
].name
&&
1218 gs
->input
[j
].sid
== es
->output
[i
].sid
)
1221 if (j
== gs
->ninput
)
1224 for (chan
= 0; chan
< 4; chan
++) {
1225 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
1226 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
1228 build_tbuffer_store(si_shader_ctx
, t_list
, out_val
, 1,
1229 LLVMGetUndef(i32
), soffset
,
1230 (4 * gs
->input
[j
].param_offset
+ chan
) * 4,
1231 V_008F0C_BUF_DATA_FORMAT_32
,
1232 V_008F0C_BUF_NUM_FORMAT_UINT
,
1238 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
1240 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1241 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1242 LLVMValueRef args
[2];
1244 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
1245 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
1246 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
1247 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
1248 LLVMNoUnwindAttribute
);
1251 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
* bld_base
)
1253 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1254 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1255 struct si_pipe_shader
*shader
= si_shader_ctx
->shader
;
1256 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1257 struct si_shader_output_values
*outputs
= NULL
;
1258 unsigned noutput
= 0;
1261 while (!tgsi_parse_end_of_tokens(parse
)) {
1262 struct tgsi_full_declaration
*d
=
1263 &parse
->FullToken
.FullDeclaration
;
1266 tgsi_parse_token(parse
);
1268 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
1271 i
= si_store_shader_io_attribs(&shader
->shader
, d
);
1275 outputs
= REALLOC(outputs
, noutput
* sizeof(outputs
[0]),
1276 (noutput
+ 1) * sizeof(outputs
[0]));
1277 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
1278 outputs
[noutput
].index
= index
;
1279 outputs
[noutput
].name
= d
->Semantic
.Name
;
1280 outputs
[noutput
].sid
= d
->Semantic
.Index
;
1281 outputs
[noutput
].usage
= d
->Declaration
.UsageMask
;
1283 for (i
= 0; i
< 4; i
++)
1284 outputs
[noutput
].values
[i
] =
1285 LLVMBuildLoad(gallivm
->builder
,
1286 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][i
],
1292 si_llvm_export_vs(bld_base
, outputs
, noutput
);
1296 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context
* bld_base
)
1298 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1299 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
1300 struct lp_build_context
* base
= &bld_base
->base
;
1301 struct lp_build_context
* uint
=
1302 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1303 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1304 LLVMValueRef args
[9];
1305 LLVMValueRef last_args
[9] = { 0 };
1306 unsigned semantic_name
;
1307 int depth_index
= -1, stencil_index
= -1;
1310 while (!tgsi_parse_end_of_tokens(parse
)) {
1311 struct tgsi_full_declaration
*d
=
1312 &parse
->FullToken
.FullDeclaration
;
1316 tgsi_parse_token(parse
);
1318 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_PROPERTY
&&
1319 parse
->FullToken
.FullProperty
.Property
.PropertyName
==
1320 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
)
1321 shader
->fs_write_all
= TRUE
;
1323 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
1326 i
= si_store_shader_io_attribs(shader
, d
);
1330 semantic_name
= d
->Semantic
.Name
;
1331 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
1332 /* Select the correct target */
1333 switch(semantic_name
) {
1334 case TGSI_SEMANTIC_POSITION
:
1335 depth_index
= index
;
1337 case TGSI_SEMANTIC_STENCIL
:
1338 stencil_index
= index
;
1340 case TGSI_SEMANTIC_COLOR
:
1341 target
= V_008DFC_SQ_EXP_MRT
+ d
->Semantic
.Index
;
1342 if (si_shader_ctx
->shader
->key
.ps
.alpha_to_one
)
1343 LLVMBuildStore(bld_base
->base
.gallivm
->builder
,
1345 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][3]);
1347 if (d
->Semantic
.Index
== 0 &&
1348 si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_ALWAYS
)
1349 si_alpha_test(bld_base
,
1350 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
]);
1355 "Warning: SI unhandled fs output type:%d\n",
1359 si_llvm_init_export_args_load(bld_base
,
1360 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
],
1363 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
1364 /* If there is an export instruction waiting to be emitted, do so now. */
1366 lp_build_intrinsic(base
->gallivm
->builder
,
1368 LLVMVoidTypeInContext(base
->gallivm
->context
),
1372 /* This instruction will be emitted at the end of the shader. */
1373 memcpy(last_args
, args
, sizeof(args
));
1375 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1376 if (shader
->fs_write_all
&& shader
->output
[i
].sid
== 0 &&
1377 si_shader_ctx
->shader
->key
.ps
.nr_cbufs
> 1) {
1378 for (int c
= 1; c
< si_shader_ctx
->shader
->key
.ps
.nr_cbufs
; c
++) {
1379 si_llvm_init_export_args_load(bld_base
,
1380 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
],
1381 V_008DFC_SQ_EXP_MRT
+ c
, args
);
1382 lp_build_intrinsic(base
->gallivm
->builder
,
1384 LLVMVoidTypeInContext(base
->gallivm
->context
),
1389 lp_build_intrinsic(base
->gallivm
->builder
,
1391 LLVMVoidTypeInContext(base
->gallivm
->context
),
1397 if (depth_index
>= 0 || stencil_index
>= 0) {
1398 LLVMValueRef out_ptr
;
1401 /* Specify the target we are exporting */
1402 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
1404 if (depth_index
>= 0) {
1405 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
1406 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1409 if (stencil_index
< 0) {
1416 if (stencil_index
>= 0) {
1417 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
1420 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1421 /* Only setting the stencil component bit (0x2) here
1422 * breaks some stencil piglit tests
1426 if (depth_index
< 0)
1430 /* Specify which components to enable */
1431 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
1435 args
[4] = uint
->zero
;
1438 lp_build_intrinsic(base
->gallivm
->builder
,
1440 LLVMVoidTypeInContext(base
->gallivm
->context
),
1443 memcpy(last_args
, args
, sizeof(args
));
1446 if (!last_args
[0]) {
1447 /* Specify which components to enable */
1448 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
1450 /* Specify the target we are exporting */
1451 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
1453 /* Set COMPR flag to zero to export data as 32-bit */
1454 last_args
[4] = uint
->zero
;
1457 last_args
[5]= uint
->zero
;
1458 last_args
[6]= uint
->zero
;
1459 last_args
[7]= uint
->zero
;
1460 last_args
[8]= uint
->zero
;
1462 si_shader_ctx
->shader
->spi_shader_col_format
|=
1463 V_028714_SPI_SHADER_32_ABGR
;
1464 si_shader_ctx
->shader
->cb_shader_mask
|= S_02823C_OUTPUT0_ENABLE(0xf);
1467 /* Specify whether the EXEC mask represents the valid mask */
1468 last_args
[1] = uint
->one
;
1470 /* Specify that this is the last export */
1471 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
1473 lp_build_intrinsic(base
->gallivm
->builder
,
1475 LLVMVoidTypeInContext(base
->gallivm
->context
),
1479 static const struct lp_build_tgsi_action txf_action
;
1481 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1482 struct lp_build_tgsi_context
* bld_base
,
1483 struct lp_build_emit_data
* emit_data
);
1485 static void tex_fetch_args(
1486 struct lp_build_tgsi_context
* bld_base
,
1487 struct lp_build_emit_data
* emit_data
)
1489 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1490 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1491 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
1492 unsigned opcode
= inst
->Instruction
.Opcode
;
1493 unsigned target
= inst
->Texture
.Texture
;
1494 LLVMValueRef coords
[4];
1495 LLVMValueRef address
[16];
1497 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
1500 unsigned sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1501 unsigned sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
1503 if (target
== TGSI_TEXTURE_BUFFER
) {
1504 LLVMTypeRef i128
= LLVMIntTypeInContext(gallivm
->context
, 128);
1505 LLVMTypeRef v2i128
= LLVMVectorType(i128
, 2);
1506 LLVMTypeRef i8
= LLVMInt8TypeInContext(gallivm
->context
);
1507 LLVMTypeRef v16i8
= LLVMVectorType(i8
, 16);
1509 /* Truncate v32i8 to v16i8. */
1510 LLVMValueRef res
= si_shader_ctx
->resources
[sampler_index
];
1511 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v2i128
, "");
1512 res
= LLVMBuildExtractElement(gallivm
->builder
, res
, bld_base
->uint_bld
.zero
, "");
1513 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v16i8
, "");
1515 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
1516 emit_data
->args
[0] = res
;
1517 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
1518 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
1519 emit_data
->arg_count
= 3;
1523 /* Fetch and project texture coordinates */
1524 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1525 for (chan
= 0; chan
< 3; chan
++ ) {
1526 coords
[chan
] = lp_build_emit_fetch(bld_base
,
1529 if (opcode
== TGSI_OPCODE_TXP
)
1530 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1536 if (opcode
== TGSI_OPCODE_TXP
)
1537 coords
[3] = bld_base
->base
.one
;
1539 /* Pack LOD bias value */
1540 if (opcode
== TGSI_OPCODE_TXB
)
1541 address
[count
++] = coords
[3];
1543 if (target
== TGSI_TEXTURE_CUBE
|| target
== TGSI_TEXTURE_SHADOWCUBE
)
1544 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
1546 /* Pack depth comparison value */
1548 case TGSI_TEXTURE_SHADOW1D
:
1549 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1550 case TGSI_TEXTURE_SHADOW2D
:
1551 case TGSI_TEXTURE_SHADOWRECT
:
1552 case TGSI_TEXTURE_SHADOWCUBE
:
1553 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1554 assert(ref_pos
>= 0);
1555 address
[count
++] = coords
[ref_pos
];
1557 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1558 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
1561 /* Pack user derivatives */
1562 if (opcode
== TGSI_OPCODE_TXD
) {
1563 for (chan
= 0; chan
< 2; chan
++) {
1564 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
1566 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 2, chan
);
1570 /* Pack texture coordinates */
1571 address
[count
++] = coords
[0];
1573 address
[count
++] = coords
[1];
1575 address
[count
++] = coords
[2];
1577 /* Pack LOD or sample index */
1578 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
1579 address
[count
++] = coords
[3];
1582 assert(!"Cannot handle more than 16 texture address parameters");
1586 for (chan
= 0; chan
< count
; chan
++ ) {
1587 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
1589 LLVMInt32TypeInContext(gallivm
->context
),
1593 /* Adjust the sample index according to FMASK.
1595 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1596 * which is the identity mapping. Each nibble says which physical sample
1597 * should be fetched to get that sample.
1599 * For example, 0x11111100 means there are only 2 samples stored and
1600 * the second sample covers 3/4 of the pixel. When reading samples 0
1601 * and 1, return physical sample 0 (determined by the first two 0s
1602 * in FMASK), otherwise return physical sample 1.
1604 * The sample index should be adjusted as follows:
1605 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1607 if (target
== TGSI_TEXTURE_2D_MSAA
||
1608 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1609 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1610 struct lp_build_emit_data txf_emit_data
= *emit_data
;
1611 LLVMValueRef txf_address
[4];
1612 unsigned txf_count
= count
;
1614 memcpy(txf_address
, address
, sizeof(txf_address
));
1616 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1617 txf_address
[2] = bld_base
->uint_bld
.zero
;
1619 txf_address
[3] = bld_base
->uint_bld
.zero
;
1621 /* Pad to a power-of-two size. */
1622 while (txf_count
< util_next_power_of_two(txf_count
))
1623 txf_address
[txf_count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1625 /* Read FMASK using TXF. */
1626 txf_emit_data
.chan
= 0;
1627 txf_emit_data
.dst_type
= LLVMVectorType(
1628 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
), 4);
1629 txf_emit_data
.args
[0] = lp_build_gather_values(gallivm
, txf_address
, txf_count
);
1630 txf_emit_data
.args
[1] = si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ sampler_index
];
1631 txf_emit_data
.args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
1632 target
== TGSI_TEXTURE_2D_MSAA
? TGSI_TEXTURE_2D
: TGSI_TEXTURE_2D_ARRAY
);
1633 txf_emit_data
.arg_count
= 3;
1635 build_tex_intrinsic(&txf_action
, bld_base
, &txf_emit_data
);
1637 /* Initialize some constants. */
1638 LLVMValueRef four
= LLVMConstInt(uint_bld
->elem_type
, 4, 0);
1639 LLVMValueRef F
= LLVMConstInt(uint_bld
->elem_type
, 0xF, 0);
1641 /* Apply the formula. */
1642 LLVMValueRef fmask
=
1643 LLVMBuildExtractElement(gallivm
->builder
,
1644 txf_emit_data
.output
[0],
1645 uint_bld
->zero
, "");
1647 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
1649 LLVMValueRef sample_index4
=
1650 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
1652 LLVMValueRef shifted_fmask
=
1653 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
1655 LLVMValueRef final_sample
=
1656 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
1658 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1659 * resource descriptor is 0 (invalid),
1661 LLVMValueRef fmask_desc
=
1662 LLVMBuildBitCast(gallivm
->builder
,
1663 si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ sampler_index
],
1664 LLVMVectorType(uint_bld
->elem_type
, 8), "");
1666 LLVMValueRef fmask_word1
=
1667 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
1670 LLVMValueRef word1_is_nonzero
=
1671 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1672 fmask_word1
, uint_bld
->zero
, "");
1674 /* Replace the MSAA sample index. */
1675 address
[sample_chan
] =
1676 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
1677 final_sample
, address
[sample_chan
], "");
1681 emit_data
->args
[1] = si_shader_ctx
->resources
[sampler_index
];
1683 if (opcode
== TGSI_OPCODE_TXF
) {
1684 /* add tex offsets */
1685 if (inst
->Texture
.NumOffsets
) {
1686 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1687 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1688 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
1690 assert(inst
->Texture
.NumOffsets
== 1);
1693 case TGSI_TEXTURE_3D
:
1694 address
[2] = lp_build_add(uint_bld
, address
[2],
1695 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
1697 case TGSI_TEXTURE_2D
:
1698 case TGSI_TEXTURE_SHADOW2D
:
1699 case TGSI_TEXTURE_RECT
:
1700 case TGSI_TEXTURE_SHADOWRECT
:
1701 case TGSI_TEXTURE_2D_ARRAY
:
1702 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1704 lp_build_add(uint_bld
, address
[1],
1705 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
1707 case TGSI_TEXTURE_1D
:
1708 case TGSI_TEXTURE_SHADOW1D
:
1709 case TGSI_TEXTURE_1D_ARRAY
:
1710 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1712 lp_build_add(uint_bld
, address
[0],
1713 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
1715 /* texture offsets do not apply to other texture targets */
1719 emit_data
->dst_type
= LLVMVectorType(
1720 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
1723 emit_data
->arg_count
= 3;
1726 emit_data
->args
[2] = si_shader_ctx
->samplers
[sampler_index
];
1728 emit_data
->dst_type
= LLVMVectorType(
1729 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
1732 emit_data
->arg_count
= 4;
1736 emit_data
->args
[emit_data
->arg_count
- 1] =
1737 lp_build_const_int32(bld_base
->base
.gallivm
, target
);
1739 /* Pad to power of two vector */
1740 while (count
< util_next_power_of_two(count
))
1741 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1743 emit_data
->args
[0] = lp_build_gather_values(gallivm
, address
, count
);
1746 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1747 struct lp_build_tgsi_context
* bld_base
,
1748 struct lp_build_emit_data
* emit_data
)
1750 struct lp_build_context
* base
= &bld_base
->base
;
1751 char intr_name
[127];
1753 if (emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
1754 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1755 base
->gallivm
->builder
,
1756 "llvm.SI.vs.load.input", emit_data
->dst_type
,
1757 emit_data
->args
, emit_data
->arg_count
,
1758 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1762 sprintf(intr_name
, "%sv%ui32", action
->intr_name
,
1763 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
1765 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1766 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
1767 emit_data
->args
, emit_data
->arg_count
,
1768 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1771 static void txq_fetch_args(
1772 struct lp_build_tgsi_context
* bld_base
,
1773 struct lp_build_emit_data
* emit_data
)
1775 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1776 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1777 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1779 if (inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
1780 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1781 LLVMTypeRef v8i32
= LLVMVectorType(i32
, 8);
1783 /* Read the size from the buffer descriptor directly. */
1784 LLVMValueRef size
= si_shader_ctx
->resources
[inst
->Src
[1].Register
.Index
];
1785 size
= LLVMBuildBitCast(gallivm
->builder
, size
, v8i32
, "");
1786 size
= LLVMBuildExtractElement(gallivm
->builder
, size
,
1787 lp_build_const_int32(gallivm
, 2), "");
1788 emit_data
->args
[0] = size
;
1793 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1796 emit_data
->args
[1] = si_shader_ctx
->resources
[inst
->Src
[1].Register
.Index
];
1799 emit_data
->args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
1800 inst
->Texture
.Texture
);
1802 emit_data
->arg_count
= 3;
1804 emit_data
->dst_type
= LLVMVectorType(
1805 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
1809 static void build_txq_intrinsic(const struct lp_build_tgsi_action
* action
,
1810 struct lp_build_tgsi_context
* bld_base
,
1811 struct lp_build_emit_data
* emit_data
)
1813 if (emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
1814 /* Just return the buffer size. */
1815 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
1819 build_tgsi_intrinsic_nomem(action
, bld_base
, emit_data
);
1822 #if HAVE_LLVM >= 0x0304
1824 static void si_llvm_emit_ddxy(
1825 const struct lp_build_tgsi_action
* action
,
1826 struct lp_build_tgsi_context
* bld_base
,
1827 struct lp_build_emit_data
* emit_data
)
1829 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1830 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1831 struct lp_build_context
* base
= &bld_base
->base
;
1832 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1833 unsigned opcode
= inst
->Instruction
.Opcode
;
1834 LLVMValueRef indices
[2];
1835 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1836 LLVMValueRef tl
, trbl
, result
[4];
1838 unsigned swizzle
[4];
1841 i32
= LLVMInt32TypeInContext(gallivm
->context
);
1843 indices
[0] = bld_base
->uint_bld
.zero
;
1844 indices
[1] = build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
1845 NULL
, 0, LLVMReadNoneAttribute
);
1846 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1849 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
1850 lp_build_const_int32(gallivm
, 0xfffffffc), "");
1851 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1854 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
1855 lp_build_const_int32(gallivm
,
1856 opcode
== TGSI_OPCODE_DDX
? 1 : 2),
1858 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1861 for (c
= 0; c
< 4; ++c
) {
1864 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
1865 for (i
= 0; i
< c
; ++i
) {
1866 if (swizzle
[i
] == swizzle
[c
]) {
1867 result
[c
] = result
[i
];
1874 LLVMBuildStore(gallivm
->builder
,
1875 LLVMBuildBitCast(gallivm
->builder
,
1876 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
1880 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
1881 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
1883 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
1884 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, base
->elem_type
, "");
1886 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
1889 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
1892 #endif /* HAVE_LLVM >= 0x0304 */
1894 /* Emit one vertex from the geometry shader */
1895 static void si_llvm_emit_vertex(
1896 const struct lp_build_tgsi_action
*action
,
1897 struct lp_build_tgsi_context
*bld_base
,
1898 struct lp_build_emit_data
*emit_data
)
1900 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1901 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
1902 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
1903 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1904 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1905 LLVMValueRef soffset
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1906 SI_PARAM_GS2VS_OFFSET
);
1907 LLVMValueRef gs_next_vertex
;
1908 LLVMValueRef can_emit
, kill
;
1909 LLVMValueRef t_list_ptr
;
1910 LLVMValueRef t_list
;
1911 LLVMValueRef args
[2];
1915 /* Load the GSVS ring resource descriptor */
1916 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1917 SI_PARAM_RW_BUFFERS
);
1918 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
1919 lp_build_const_int32(gallivm
, SI_RING_GSVS
));
1921 if (shader
->noutput
== 0) {
1922 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1924 while (!tgsi_parse_end_of_tokens(parse
)) {
1925 tgsi_parse_token(parse
);
1927 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
1928 struct tgsi_full_declaration
*d
= &parse
->FullToken
.FullDeclaration
;
1930 if (d
->Declaration
.File
== TGSI_FILE_OUTPUT
)
1931 si_store_shader_io_attribs(shader
, d
);
1936 /* Write vertex attribute values to GSVS ring */
1937 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
, si_shader_ctx
->gs_next_vertex
, "");
1939 /* If this thread has already emitted the declared maximum number of
1940 * vertices, kill it: excessive vertex emissions are not supposed to
1941 * have any effect, and GS threads have no externally observable
1942 * effects other than emitting vertices.
1944 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULE
, gs_next_vertex
,
1945 lp_build_const_int32(gallivm
,
1946 shader
->gs_max_out_vertices
), "");
1947 kill
= lp_build_select(&bld_base
->base
, can_emit
,
1948 lp_build_const_float(gallivm
, 1.0f
),
1949 lp_build_const_float(gallivm
, -1.0f
));
1950 build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
1951 LLVMVoidTypeInContext(gallivm
->context
), &kill
, 1, 0);
1953 for (i
= 0; i
< shader
->noutput
; i
++) {
1954 LLVMValueRef
*out_ptr
=
1955 si_shader_ctx
->radeon_bld
.soa
.outputs
[shader
->output
[i
].index
];
1957 for (chan
= 0; chan
< 4; chan
++) {
1958 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
1959 LLVMValueRef voffset
=
1960 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
1961 shader
->gs_max_out_vertices
);
1963 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
1964 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
1966 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
1968 build_tbuffer_store(si_shader_ctx
, t_list
, out_val
, 1,
1969 voffset
, soffset
, 0,
1970 V_008F0C_BUF_DATA_FORMAT_32
,
1971 V_008F0C_BUF_NUM_FORMAT_UINT
,
1975 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
1976 lp_build_const_int32(gallivm
, 1));
1977 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, si_shader_ctx
->gs_next_vertex
);
1979 /* Signal vertex emission */
1980 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
);
1981 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
1982 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
1983 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
1984 LLVMNoUnwindAttribute
);
1987 /* Cut one primitive from the geometry shader */
1988 static void si_llvm_emit_primitive(
1989 const struct lp_build_tgsi_action
*action
,
1990 struct lp_build_tgsi_context
*bld_base
,
1991 struct lp_build_emit_data
*emit_data
)
1993 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1994 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1995 LLVMValueRef args
[2];
1997 /* Signal primitive cut */
1998 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
);
1999 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
2000 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2001 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
2002 LLVMNoUnwindAttribute
);
2005 static const struct lp_build_tgsi_action tex_action
= {
2006 .fetch_args
= tex_fetch_args
,
2007 .emit
= build_tex_intrinsic
,
2008 .intr_name
= "llvm.SI.sample."
2011 static const struct lp_build_tgsi_action txb_action
= {
2012 .fetch_args
= tex_fetch_args
,
2013 .emit
= build_tex_intrinsic
,
2014 .intr_name
= "llvm.SI.sampleb."
2017 #if HAVE_LLVM >= 0x0304
2018 static const struct lp_build_tgsi_action txd_action
= {
2019 .fetch_args
= tex_fetch_args
,
2020 .emit
= build_tex_intrinsic
,
2021 .intr_name
= "llvm.SI.sampled."
2025 static const struct lp_build_tgsi_action txf_action
= {
2026 .fetch_args
= tex_fetch_args
,
2027 .emit
= build_tex_intrinsic
,
2028 .intr_name
= "llvm.SI.imageload."
2031 static const struct lp_build_tgsi_action txl_action
= {
2032 .fetch_args
= tex_fetch_args
,
2033 .emit
= build_tex_intrinsic
,
2034 .intr_name
= "llvm.SI.samplel."
2037 static const struct lp_build_tgsi_action txq_action
= {
2038 .fetch_args
= txq_fetch_args
,
2039 .emit
= build_txq_intrinsic
,
2040 .intr_name
= "llvm.SI.resinfo"
2043 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
2045 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
2046 LLVMValueRef args
[3];
2048 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
2050 args
[2] = lp_build_const_int32(gallivm
, 1);
2052 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
2055 static void create_function(struct si_shader_context
*si_shader_ctx
)
2057 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2058 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2059 struct si_pipe_shader
*shader
= si_shader_ctx
->shader
;
2060 LLVMTypeRef params
[SI_NUM_PARAMS
], f32
, i8
, i32
, v2i32
, v3i32
;
2061 unsigned i
, last_sgpr
, num_params
;
2063 i8
= LLVMInt8TypeInContext(gallivm
->context
);
2064 i32
= LLVMInt32TypeInContext(gallivm
->context
);
2065 f32
= LLVMFloatTypeInContext(gallivm
->context
);
2066 v2i32
= LLVMVectorType(i32
, 2);
2067 v3i32
= LLVMVectorType(i32
, 3);
2069 params
[SI_PARAM_CONST
] = LLVMPointerType(
2070 LLVMArrayType(LLVMVectorType(i8
, 16), NUM_CONST_BUFFERS
), CONST_ADDR_SPACE
);
2071 params
[SI_PARAM_RW_BUFFERS
] = params
[SI_PARAM_CONST
];
2073 /* We assume at most 16 textures per program at the moment.
2074 * This need probably need to be changed to support bindless textures */
2075 params
[SI_PARAM_SAMPLER
] = LLVMPointerType(
2076 LLVMArrayType(LLVMVectorType(i8
, 16), NUM_SAMPLER_VIEWS
), CONST_ADDR_SPACE
);
2077 params
[SI_PARAM_RESOURCE
] = LLVMPointerType(
2078 LLVMArrayType(LLVMVectorType(i8
, 32), NUM_SAMPLER_STATES
), CONST_ADDR_SPACE
);
2080 switch (si_shader_ctx
->type
) {
2081 case TGSI_PROCESSOR_VERTEX
:
2082 params
[SI_PARAM_VERTEX_BUFFER
] = params
[SI_PARAM_CONST
];
2083 params
[SI_PARAM_START_INSTANCE
] = i32
;
2084 num_params
= SI_PARAM_START_INSTANCE
+1;
2085 if (shader
->key
.vs
.as_es
) {
2086 params
[SI_PARAM_ES2GS_OFFSET
] = i32
;
2089 /* The locations of the other parameters are assigned dynamically. */
2091 /* Streamout SGPRs. */
2092 if (shader
->selector
->so
.num_outputs
) {
2093 params
[si_shader_ctx
->param_streamout_config
= num_params
++] = i32
;
2094 params
[si_shader_ctx
->param_streamout_write_index
= num_params
++] = i32
;
2096 /* A streamout buffer offset is loaded if the stride is non-zero. */
2097 for (i
= 0; i
< 4; i
++) {
2098 if (!shader
->selector
->so
.stride
[i
])
2101 params
[si_shader_ctx
->param_streamout_offset
[i
] = num_params
++] = i32
;
2105 last_sgpr
= num_params
-1;
2108 params
[si_shader_ctx
->param_vertex_id
= num_params
++] = i32
;
2109 params
[num_params
++] = i32
; /* unused*/
2110 params
[num_params
++] = i32
; /* unused */
2111 params
[si_shader_ctx
->param_instance_id
= num_params
++] = i32
;
2114 case TGSI_PROCESSOR_GEOMETRY
:
2115 params
[SI_PARAM_GS2VS_OFFSET
] = i32
;
2116 params
[SI_PARAM_GS_WAVE_ID
] = i32
;
2117 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
2120 params
[SI_PARAM_VTX0_OFFSET
] = i32
;
2121 params
[SI_PARAM_VTX1_OFFSET
] = i32
;
2122 params
[SI_PARAM_PRIMITIVE_ID
] = i32
;
2123 params
[SI_PARAM_VTX2_OFFSET
] = i32
;
2124 params
[SI_PARAM_VTX3_OFFSET
] = i32
;
2125 params
[SI_PARAM_VTX4_OFFSET
] = i32
;
2126 params
[SI_PARAM_VTX5_OFFSET
] = i32
;
2127 params
[SI_PARAM_GS_INSTANCE_ID
] = i32
;
2128 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
2131 case TGSI_PROCESSOR_FRAGMENT
:
2132 params
[SI_PARAM_ALPHA_REF
] = f32
;
2133 params
[SI_PARAM_PRIM_MASK
] = i32
;
2134 last_sgpr
= SI_PARAM_PRIM_MASK
;
2135 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
2136 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
2137 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
2138 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
2139 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
2140 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
2141 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
2142 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
2143 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
2144 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
2145 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
2146 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
2147 params
[SI_PARAM_FRONT_FACE
] = f32
;
2148 params
[SI_PARAM_ANCILLARY
] = f32
;
2149 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
2150 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
2151 num_params
= SI_PARAM_POS_FIXED_PT
+1;
2155 assert(0 && "unimplemented shader");
2159 assert(num_params
<= Elements(params
));
2160 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, num_params
);
2161 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
2163 for (i
= 0; i
<= last_sgpr
; ++i
) {
2164 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
2167 LLVMAddAttribute(P
, LLVMInRegAttribute
);
2169 #if HAVE_LLVM >= 0x0304
2170 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2171 * to move load. Inputs are constant so this is fine. */
2172 case SI_PARAM_CONST
:
2173 case SI_PARAM_SAMPLER
:
2174 case SI_PARAM_RESOURCE
:
2175 LLVMAddAttribute(P
, LLVMByValAttribute
);
2181 #if HAVE_LLVM >= 0x0304
2182 if (bld_base
->info
&&
2183 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
2184 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0))
2185 si_shader_ctx
->ddxy_lds
=
2186 LLVMAddGlobalInAddressSpace(gallivm
->module
,
2187 LLVMArrayType(i32
, 64),
2193 static void preload_constants(struct si_shader_context
*si_shader_ctx
)
2195 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2196 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2197 const struct tgsi_shader_info
* info
= bld_base
->info
;
2199 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
2201 for (buf
= 0; buf
< NUM_CONST_BUFFERS
; buf
++) {
2202 unsigned i
, num_const
= info
->const_file_max
[buf
] + 1;
2207 /* Allocate space for the constant values */
2208 si_shader_ctx
->constants
[buf
] = CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
2210 /* Load the resource descriptor */
2211 si_shader_ctx
->const_resource
[buf
] =
2212 build_indexed_load(si_shader_ctx
, ptr
, lp_build_const_int32(gallivm
, buf
));
2214 /* Load the constants, we rely on the code sinking to do the rest */
2215 for (i
= 0; i
< num_const
* 4; ++i
) {
2216 LLVMValueRef args
[2] = {
2217 si_shader_ctx
->const_resource
[buf
],
2218 lp_build_const_int32(gallivm
, i
* 4)
2220 si_shader_ctx
->constants
[buf
][i
] =
2221 build_intrinsic(gallivm
->builder
, "llvm.SI.load.const",
2222 bld_base
->base
.elem_type
, args
, 2,
2223 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
2228 static void preload_samplers(struct si_shader_context
*si_shader_ctx
)
2230 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2231 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2232 const struct tgsi_shader_info
* info
= bld_base
->info
;
2234 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
2236 LLVMValueRef res_ptr
, samp_ptr
;
2237 LLVMValueRef offset
;
2239 if (num_samplers
== 0)
2242 /* Allocate space for the values */
2243 si_shader_ctx
->resources
= CALLOC(NUM_SAMPLER_VIEWS
, sizeof(LLVMValueRef
));
2244 si_shader_ctx
->samplers
= CALLOC(num_samplers
, sizeof(LLVMValueRef
));
2246 res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
2247 samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
2249 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2250 for (i
= 0; i
< num_samplers
; ++i
) {
2252 offset
= lp_build_const_int32(gallivm
, i
);
2253 si_shader_ctx
->resources
[i
] = build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
2256 offset
= lp_build_const_int32(gallivm
, i
);
2257 si_shader_ctx
->samplers
[i
] = build_indexed_load(si_shader_ctx
, samp_ptr
, offset
);
2259 /* FMASK resource */
2260 if (info
->is_msaa_sampler
[i
]) {
2261 offset
= lp_build_const_int32(gallivm
, FMASK_TEX_OFFSET
+ i
);
2262 si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ i
] =
2263 build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
2268 static void preload_streamout_buffers(struct si_shader_context
*si_shader_ctx
)
2270 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2271 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2274 if (si_shader_ctx
->type
!= TGSI_PROCESSOR_VERTEX
||
2275 si_shader_ctx
->shader
->key
.vs
.as_es
||
2276 !si_shader_ctx
->shader
->selector
->so
.num_outputs
)
2279 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2280 SI_PARAM_RW_BUFFERS
);
2282 /* Load the resources, we rely on the code sinking to do the rest */
2283 for (i
= 0; i
< 4; ++i
) {
2284 if (si_shader_ctx
->shader
->selector
->so
.stride
[i
]) {
2285 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
2288 si_shader_ctx
->so_buffers
[i
] = build_indexed_load(si_shader_ctx
, buf_ptr
, offset
);
2293 int si_compile_llvm(struct si_context
*sctx
, struct si_pipe_shader
*shader
,
2298 struct radeon_shader_binary binary
;
2299 bool dump
= r600_can_dump_shader(&sctx
->screen
->b
,
2300 shader
->selector
? shader
->selector
->tokens
: NULL
);
2301 memset(&binary
, 0, sizeof(binary
));
2302 radeon_llvm_compile(mod
, &binary
,
2303 r600_get_llvm_processor_name(sctx
->screen
->b
.family
), dump
);
2304 if (dump
&& ! binary
.disassembled
) {
2305 fprintf(stderr
, "SI CODE:\n");
2306 for (i
= 0; i
< binary
.code_size
; i
+=4 ) {
2307 fprintf(stderr
, "%02x%02x%02x%02x\n", binary
.code
[i
+ 3],
2308 binary
.code
[i
+ 2], binary
.code
[i
+ 1],
2313 /* XXX: We may be able to emit some of these values directly rather than
2314 * extracting fields to be emitted later.
2316 for (i
= 0; i
< binary
.config_size
; i
+= 8) {
2317 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
));
2318 unsigned value
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
+ 4));
2320 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
2321 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
2322 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
2323 case R_00B848_COMPUTE_PGM_RSRC1
:
2324 shader
->num_sgprs
= (G_00B028_SGPRS(value
) + 1) * 8;
2325 shader
->num_vgprs
= (G_00B028_VGPRS(value
) + 1) * 4;
2327 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
2328 shader
->lds_size
= G_00B02C_EXTRA_LDS_SIZE(value
);
2330 case R_00B84C_COMPUTE_PGM_RSRC2
:
2331 shader
->lds_size
= G_00B84C_LDS_SIZE(value
);
2333 case R_0286CC_SPI_PS_INPUT_ENA
:
2334 shader
->spi_ps_input_ena
= value
;
2337 fprintf(stderr
, "Warning: Compiler emitted unknown "
2338 "config register: 0x%x\n", reg
);
2343 /* copy new shader */
2344 r600_resource_reference(&shader
->bo
, NULL
);
2345 shader
->bo
= si_resource_create_custom(sctx
->b
.b
.screen
, PIPE_USAGE_IMMUTABLE
,
2347 if (shader
->bo
== NULL
) {
2351 ptr
= (uint32_t*)sctx
->b
.ws
->buffer_map(shader
->bo
->cs_buf
, sctx
->b
.rings
.gfx
.cs
, PIPE_TRANSFER_WRITE
);
2352 if (SI_BIG_ENDIAN
) {
2353 for (i
= 0; i
< binary
.code_size
/ 4; ++i
) {
2354 ptr
[i
] = util_cpu_to_le32((*(uint32_t*)(binary
.code
+ i
*4)));
2357 memcpy(ptr
, binary
.code
, binary
.code_size
);
2359 sctx
->b
.ws
->buffer_unmap(shader
->bo
->cs_buf
);
2362 free(binary
.config
);
2367 /* Generate code for the hardware VS shader stage to go with a geometry shader */
2368 static int si_generate_gs_copy_shader(struct si_context
*sctx
,
2369 struct si_shader_context
*si_shader_ctx
,
2372 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
2373 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2374 struct lp_build_context
*base
= &bld_base
->base
;
2375 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2376 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
2377 struct si_shader
*gs
= &si_shader_ctx
->shader
->selector
->current
->shader
;
2378 struct si_shader_output_values
*outputs
;
2379 LLVMValueRef t_list_ptr
, t_list
;
2380 LLVMValueRef args
[9];
2383 outputs
= MALLOC(gs
->noutput
* sizeof(outputs
[0]));
2385 si_shader_ctx
->type
= TGSI_PROCESSOR_VERTEX
;
2386 si_shader_ctx
->gs_for_vs
= gs
;
2388 radeon_llvm_context_init(&si_shader_ctx
->radeon_bld
);
2390 create_meta_data(si_shader_ctx
);
2391 create_function(si_shader_ctx
);
2392 preload_streamout_buffers(si_shader_ctx
);
2394 /* Load the GSVS ring resource descriptor */
2395 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2396 SI_PARAM_RW_BUFFERS
);
2397 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
2398 lp_build_const_int32(gallivm
, SI_RING_GSVS
));
2401 args
[1] = lp_build_mul_imm(uint
,
2402 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2403 si_shader_ctx
->param_vertex_id
),
2405 args
[3] = uint
->zero
;
2406 args
[4] = uint
->one
; /* OFFEN */
2407 args
[5] = uint
->zero
; /* IDXEN */
2408 args
[6] = uint
->one
; /* GLC */
2409 args
[7] = uint
->one
; /* SLC */
2410 args
[8] = uint
->zero
; /* TFE */
2412 /* Fetch vertex data from GSVS ring */
2413 for (i
= 0; i
< gs
->noutput
; ++i
) {
2414 struct si_shader_output
*out
= gs
->output
+ i
;
2417 shader
->output
[i
] = *out
;
2419 outputs
[i
].name
= out
->name
;
2420 outputs
[i
].index
= out
->index
;
2421 outputs
[i
].sid
= out
->sid
;
2422 outputs
[i
].usage
= out
->usage
;
2424 for (chan
= 0; chan
< 4; chan
++) {
2425 args
[2] = lp_build_const_int32(gallivm
,
2427 gs
->gs_max_out_vertices
* 16 * 4);
2429 outputs
[i
].values
[chan
] =
2430 LLVMBuildBitCast(gallivm
->builder
,
2431 build_intrinsic(gallivm
->builder
,
2432 "llvm.SI.buffer.load.dword.i32.i32",
2433 LLVMInt32TypeInContext(gallivm
->context
),
2435 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
2436 base
->elem_type
, "");
2439 shader
->noutput
= gs
->noutput
;
2441 si_llvm_export_vs(bld_base
, outputs
, gs
->noutput
);
2443 radeon_llvm_finalize_module(&si_shader_ctx
->radeon_bld
);
2446 fprintf(stderr
, "Copy Vertex Shader for Geometry Shader:\n\n");
2448 r
= si_compile_llvm(sctx
, si_shader_ctx
->shader
,
2449 bld_base
->base
.gallivm
->module
);
2451 radeon_llvm_dispose(&si_shader_ctx
->radeon_bld
);
2457 int si_pipe_shader_create(
2458 struct pipe_context
*ctx
,
2459 struct si_pipe_shader
*shader
)
2461 struct si_context
*sctx
= (struct si_context
*)ctx
;
2462 struct si_pipe_shader_selector
*sel
= shader
->selector
;
2463 struct si_shader_context si_shader_ctx
;
2464 struct tgsi_shader_info shader_info
;
2465 struct lp_build_tgsi_context
* bld_base
;
2468 bool dump
= r600_can_dump_shader(&sctx
->screen
->b
, sel
->tokens
);
2470 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2471 * conversion fails. */
2473 tgsi_dump(sel
->tokens
, 0);
2474 si_dump_streamout(&sel
->so
);
2477 assert(shader
->shader
.noutput
== 0);
2478 assert(shader
->shader
.nparam
== 0);
2479 assert(shader
->shader
.ninput
== 0);
2481 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
2482 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
2483 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
2485 tgsi_scan_shader(sel
->tokens
, &shader_info
);
2487 shader
->shader
.uses_kill
= shader_info
.uses_kill
;
2488 shader
->shader
.uses_instanceid
= shader_info
.uses_instanceid
;
2489 bld_base
->info
= &shader_info
;
2490 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
2492 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
2493 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = txb_action
;
2494 #if HAVE_LLVM >= 0x0304
2495 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = txd_action
;
2497 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = txf_action
;
2498 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = txl_action
;
2499 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
2500 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = txq_action
;
2502 #if HAVE_LLVM >= 0x0304
2503 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
2504 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
2507 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
2508 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
2510 si_shader_ctx
.radeon_bld
.load_system_value
= declare_system_value
;
2511 si_shader_ctx
.tokens
= sel
->tokens
;
2512 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
2513 si_shader_ctx
.shader
= shader
;
2514 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
2516 switch (si_shader_ctx
.type
) {
2517 case TGSI_PROCESSOR_VERTEX
:
2518 si_shader_ctx
.radeon_bld
.load_input
= declare_input_vs
;
2519 if (shader
->key
.vs
.as_es
) {
2520 si_shader_ctx
.gs_for_vs
= &sctx
->gs_shader
->current
->shader
;
2521 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
2523 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
2526 case TGSI_PROCESSOR_GEOMETRY
: {
2529 si_shader_ctx
.radeon_bld
.load_input
= declare_input_gs
;
2530 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
2531 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
2533 for (i
= 0; i
< shader_info
.num_properties
; i
++) {
2534 switch (shader_info
.properties
[i
].name
) {
2535 case TGSI_PROPERTY_GS_INPUT_PRIM
:
2536 shader
->shader
.gs_input_prim
= shader_info
.properties
[i
].data
[0];
2538 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
2539 shader
->shader
.gs_output_prim
= shader_info
.properties
[i
].data
[0];
2541 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
:
2542 shader
->shader
.gs_max_out_vertices
= shader_info
.properties
[i
].data
[0];
2548 case TGSI_PROCESSOR_FRAGMENT
:
2549 si_shader_ctx
.radeon_bld
.load_input
= declare_input_fs
;
2550 bld_base
->emit_epilogue
= si_llvm_emit_fs_epilogue
;
2553 assert(!"Unsupported shader type");
2557 create_meta_data(&si_shader_ctx
);
2558 create_function(&si_shader_ctx
);
2559 preload_constants(&si_shader_ctx
);
2560 preload_samplers(&si_shader_ctx
);
2561 preload_streamout_buffers(&si_shader_ctx
);
2563 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
2564 si_shader_ctx
.gs_next_vertex
=
2565 lp_build_alloca(bld_base
->base
.gallivm
,
2566 bld_base
->uint_bld
.elem_type
, "");
2569 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
2570 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
2574 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
2576 mod
= bld_base
->base
.gallivm
->module
;
2577 r
= si_compile_llvm(sctx
, shader
, mod
);
2579 fprintf(stderr
, "LLVM failed to compile shader\n");
2583 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
2585 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
2586 shader
->gs_copy_shader
= CALLOC_STRUCT(si_pipe_shader
);
2587 shader
->gs_copy_shader
->selector
= shader
->selector
;
2588 shader
->gs_copy_shader
->key
= shader
->key
;
2589 si_shader_ctx
.shader
= shader
->gs_copy_shader
;
2590 if ((r
= si_generate_gs_copy_shader(sctx
, &si_shader_ctx
, dump
))) {
2591 free(shader
->gs_copy_shader
);
2592 shader
->gs_copy_shader
= NULL
;
2597 tgsi_parse_free(&si_shader_ctx
.parse
);
2600 for (int i
= 0; i
< NUM_CONST_BUFFERS
; i
++)
2601 FREE(si_shader_ctx
.constants
[i
]);
2602 FREE(si_shader_ctx
.resources
);
2603 FREE(si_shader_ctx
.samplers
);
2608 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
2610 r600_resource_reference(&shader
->bo
, NULL
);