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
, SI_PARAM_CONST
);
323 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
324 lp_build_const_int32(gallivm
,
325 NUM_PIPE_CONST_BUFFERS
+ 1));
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 intrinsic is overloaded, we need to add a type suffix for overloading to work. */
847 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
848 const char *types
[] = {"i32", "v2i32", "v4i32"};
850 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
852 lp_build_intrinsic(gallivm
->builder
, name
,
853 LLVMVoidTypeInContext(gallivm
->context
),
854 args
, Elements(args
));
857 static void build_streamout_store(struct si_shader_context
*shader
,
860 unsigned num_channels
,
862 LLVMValueRef soffset
,
863 unsigned inst_offset
)
865 static unsigned dfmt
[] = {
866 V_008F0C_BUF_DATA_FORMAT_32
,
867 V_008F0C_BUF_DATA_FORMAT_32_32
,
868 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
869 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
871 assert(num_channels
>= 1 && num_channels
<= 4);
873 build_tbuffer_store(shader
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
874 inst_offset
, dfmt
[num_channels
-1],
875 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
878 /* On SI, the vertex shader is responsible for writing streamout data
880 static void si_llvm_emit_streamout(struct si_shader_context
*shader
,
881 struct si_shader_output_values
*outputs
,
884 struct pipe_stream_output_info
*so
= &shader
->shader
->selector
->so
;
885 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
886 LLVMBuilderRef builder
= gallivm
->builder
;
888 struct lp_build_if_state if_ctx
;
890 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
892 LLVMValueRef so_param
=
893 LLVMGetParam(shader
->radeon_bld
.main_fn
,
894 shader
->param_streamout_config
);
896 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
897 LLVMValueRef so_vtx_count
=
898 LLVMBuildAnd(builder
,
899 LLVMBuildLShr(builder
, so_param
,
900 LLVMConstInt(i32
, 16, 0), ""),
901 LLVMConstInt(i32
, 127, 0), "");
903 LLVMValueRef tid
= build_intrinsic(builder
, "llvm.SI.tid", i32
,
904 NULL
, 0, LLVMReadNoneAttribute
);
906 /* can_emit = tid < so_vtx_count; */
907 LLVMValueRef can_emit
=
908 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
910 /* Emit the streamout code conditionally. This actually avoids
911 * out-of-bounds buffer access. The hw tells us via the SGPR
912 * (so_vtx_count) which threads are allowed to emit streamout data. */
913 lp_build_if(&if_ctx
, gallivm
, can_emit
);
915 /* The buffer offset is computed as follows:
916 * ByteOffset = streamout_offset[buffer_id]*4 +
917 * (streamout_write_index + thread_id)*stride[buffer_id] +
921 LLVMValueRef so_write_index
=
922 LLVMGetParam(shader
->radeon_bld
.main_fn
,
923 shader
->param_streamout_write_index
);
925 /* Compute (streamout_write_index + thread_id). */
926 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
928 /* Compute the write offset for each enabled buffer. */
929 LLVMValueRef so_write_offset
[4] = {};
930 for (i
= 0; i
< 4; i
++) {
934 LLVMValueRef so_offset
= LLVMGetParam(shader
->radeon_bld
.main_fn
,
935 shader
->param_streamout_offset
[i
]);
936 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(i32
, 4, 0), "");
938 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
939 LLVMConstInt(i32
, so
->stride
[i
]*4, 0), "");
940 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
943 /* Write streamout data. */
944 for (i
= 0; i
< so
->num_outputs
; i
++) {
945 unsigned buf_idx
= so
->output
[i
].output_buffer
;
946 unsigned reg
= so
->output
[i
].register_index
;
947 unsigned start
= so
->output
[i
].start_component
;
948 unsigned num_comps
= so
->output
[i
].num_components
;
951 assert(num_comps
&& num_comps
<= 4);
952 if (!num_comps
|| num_comps
> 4)
955 /* Load the output as int. */
956 for (j
= 0; j
< num_comps
; j
++) {
959 while (outidx
< noutput
&& outputs
[outidx
].index
!= reg
)
962 if (outidx
< noutput
)
963 out
[j
] = LLVMBuildBitCast(builder
,
964 outputs
[outidx
].values
[start
+j
],
973 /* Pack the output. */
974 LLVMValueRef vdata
= NULL
;
980 case 2: /* as v2i32 */
981 case 3: /* as v4i32 (aligned to 4) */
982 case 4: /* as v4i32 */
983 vdata
= LLVMGetUndef(LLVMVectorType(i32
, util_next_power_of_two(num_comps
)));
984 for (j
= 0; j
< num_comps
; j
++) {
985 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
986 LLVMConstInt(i32
, j
, 0), "");
991 build_streamout_store(shader
, shader
->so_buffers
[buf_idx
],
993 so_write_offset
[buf_idx
],
994 LLVMConstInt(i32
, 0, 0),
995 so
->output
[i
].dst_offset
*4);
998 lp_build_endif(&if_ctx
);
1002 /* Generate export instructions for hardware VS shader stage */
1003 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
1004 struct si_shader_output_values
*outputs
,
1007 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1008 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
1009 struct lp_build_context
* base
= &bld_base
->base
;
1010 struct lp_build_context
* uint
=
1011 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1012 LLVMValueRef args
[9];
1013 LLVMValueRef pos_args
[4][9] = { { 0 } };
1014 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
;
1015 unsigned semantic_name
, semantic_index
, semantic_usage
;
1017 unsigned param_count
= 0;
1021 if (outputs
&& si_shader_ctx
->shader
->selector
->so
.num_outputs
) {
1022 si_llvm_emit_streamout(si_shader_ctx
, outputs
, noutput
);
1025 for (i
= 0; i
< noutput
; i
++) {
1026 semantic_name
= outputs
[i
].name
;
1027 semantic_index
= outputs
[i
].sid
;
1028 semantic_usage
= outputs
[i
].usage
;
1031 /* Select the correct target */
1032 switch(semantic_name
) {
1033 case TGSI_SEMANTIC_PSIZE
:
1034 shader
->vs_out_misc_write
= true;
1035 shader
->vs_out_point_size
= true;
1036 psize_value
= outputs
[i
].values
[0];
1038 case TGSI_SEMANTIC_EDGEFLAG
:
1039 shader
->vs_out_misc_write
= true;
1040 shader
->vs_out_edgeflag
= true;
1041 edgeflag_value
= outputs
[i
].values
[0];
1043 case TGSI_SEMANTIC_LAYER
:
1044 shader
->vs_out_misc_write
= true;
1045 shader
->vs_out_layer
= true;
1046 layer_value
= outputs
[i
].values
[0];
1048 case TGSI_SEMANTIC_POSITION
:
1049 target
= V_008DFC_SQ_EXP_POS
;
1051 case TGSI_SEMANTIC_COLOR
:
1052 case TGSI_SEMANTIC_BCOLOR
:
1053 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1054 shader
->output
[i
].param_offset
= param_count
;
1057 case TGSI_SEMANTIC_CLIPDIST
:
1058 if (!(si_shader_ctx
->shader
->key
.vs
.ucps_enabled
&
1059 (1 << semantic_index
)))
1061 shader
->clip_dist_write
|=
1062 semantic_usage
<< (semantic_index
<< 2);
1063 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
1065 case TGSI_SEMANTIC_CLIPVERTEX
:
1066 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
1068 case TGSI_SEMANTIC_PRIMID
:
1069 case TGSI_SEMANTIC_FOG
:
1070 case TGSI_SEMANTIC_GENERIC
:
1071 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1072 shader
->output
[i
].param_offset
= param_count
;
1078 "Warning: SI unhandled vs output type:%d\n",
1082 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
1084 if (target
>= V_008DFC_SQ_EXP_POS
&&
1085 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1086 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1087 args
, sizeof(args
));
1089 lp_build_intrinsic(base
->gallivm
->builder
,
1091 LLVMVoidTypeInContext(base
->gallivm
->context
),
1095 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1096 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1097 goto handle_semantic
;
1101 /* We need to add the position output manually if it's missing. */
1102 if (!pos_args
[0][0]) {
1103 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1104 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1105 pos_args
[0][2] = uint
->zero
; /* last export? */
1106 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1107 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1108 pos_args
[0][5] = base
->zero
; /* X */
1109 pos_args
[0][6] = base
->zero
; /* Y */
1110 pos_args
[0][7] = base
->zero
; /* Z */
1111 pos_args
[0][8] = base
->one
; /* W */
1114 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1115 if (shader
->vs_out_misc_write
) {
1116 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
1117 shader
->vs_out_point_size
|
1118 (shader
->vs_out_edgeflag
<< 1) |
1119 (shader
->vs_out_layer
<< 2));
1120 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
1121 pos_args
[1][2] = uint
->zero
; /* last export? */
1122 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
1123 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
1124 pos_args
[1][5] = base
->zero
; /* X */
1125 pos_args
[1][6] = base
->zero
; /* Y */
1126 pos_args
[1][7] = base
->zero
; /* Z */
1127 pos_args
[1][8] = base
->zero
; /* W */
1129 if (shader
->vs_out_point_size
)
1130 pos_args
[1][5] = psize_value
;
1132 if (shader
->vs_out_edgeflag
) {
1133 /* The output is a float, but the hw expects an integer
1134 * with the first bit containing the edge flag. */
1135 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
1137 bld_base
->uint_bld
.elem_type
, "");
1138 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
1140 bld_base
->int_bld
.one
);
1142 /* The LLVM intrinsic expects a float. */
1143 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
1145 base
->elem_type
, "");
1148 if (shader
->vs_out_layer
)
1149 pos_args
[1][7] = layer_value
;
1152 for (i
= 0; i
< 4; i
++)
1154 shader
->nr_pos_exports
++;
1157 for (i
= 0; i
< 4; i
++) {
1158 if (!pos_args
[i
][0])
1161 /* Specify the target we are exporting */
1162 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
1164 if (pos_idx
== shader
->nr_pos_exports
)
1165 /* Specify that this is the last export */
1166 pos_args
[i
][2] = uint
->one
;
1168 lp_build_intrinsic(base
->gallivm
->builder
,
1170 LLVMVoidTypeInContext(base
->gallivm
->context
),
1175 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
* bld_base
)
1177 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1178 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1179 struct si_shader
*es
= &si_shader_ctx
->shader
->shader
;
1180 struct si_shader
*gs
= si_shader_ctx
->gs_for_vs
;
1181 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1182 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1183 LLVMValueRef t_list_ptr
;
1184 LLVMValueRef t_list
;
1188 while (!tgsi_parse_end_of_tokens(parse
)) {
1189 struct tgsi_full_declaration
*d
=
1190 &parse
->FullToken
.FullDeclaration
;
1192 tgsi_parse_token(parse
);
1194 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
1197 si_store_shader_io_attribs(es
, d
);
1200 /* Load the ESGS ring resource descriptor */
1201 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1202 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
1203 lp_build_const_int32(gallivm
,
1204 NUM_PIPE_CONST_BUFFERS
+ 1));
1206 for (i
= 0; i
< es
->noutput
; i
++) {
1207 LLVMValueRef
*out_ptr
=
1208 si_shader_ctx
->radeon_bld
.soa
.outputs
[es
->output
[i
].index
];
1211 for (j
= 0; j
< gs
->ninput
; j
++) {
1212 if (gs
->input
[j
].name
== es
->output
[i
].name
&&
1213 gs
->input
[j
].sid
== es
->output
[i
].sid
)
1216 if (j
== gs
->ninput
)
1219 for (chan
= 0; chan
< 4; chan
++) {
1220 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
1221 LLVMValueRef voffset
=
1222 lp_build_const_int32(gallivm
,
1223 (4 * gs
->input
[j
].param_offset
+ chan
) * 4);
1224 LLVMValueRef soffset
=
1225 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1226 SI_PARAM_ES2GS_OFFSET
);
1228 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
1230 build_tbuffer_store(si_shader_ctx
, t_list
, out_val
, 1,
1231 voffset
, soffset
, 0,
1232 V_008F0C_BUF_DATA_FORMAT_32
,
1233 V_008F0C_BUF_NUM_FORMAT_UINT
,
1239 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
1241 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1242 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1243 LLVMValueRef args
[2];
1245 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
1246 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
1247 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
1248 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
1249 LLVMNoUnwindAttribute
);
1252 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
* bld_base
)
1254 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1255 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1256 struct si_pipe_shader
*shader
= si_shader_ctx
->shader
;
1257 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1258 struct si_shader_output_values
*outputs
= NULL
;
1259 unsigned noutput
= 0;
1262 while (!tgsi_parse_end_of_tokens(parse
)) {
1263 struct tgsi_full_declaration
*d
=
1264 &parse
->FullToken
.FullDeclaration
;
1267 tgsi_parse_token(parse
);
1269 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
1272 i
= si_store_shader_io_attribs(&shader
->shader
, d
);
1276 outputs
= REALLOC(outputs
, noutput
* sizeof(outputs
[0]),
1277 (noutput
+ 1) * sizeof(outputs
[0]));
1278 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
1279 outputs
[noutput
].index
= index
;
1280 outputs
[noutput
].name
= d
->Semantic
.Name
;
1281 outputs
[noutput
].sid
= d
->Semantic
.Index
;
1282 outputs
[noutput
].usage
= d
->Declaration
.UsageMask
;
1284 for (i
= 0; i
< 4; i
++)
1285 outputs
[noutput
].values
[i
] =
1286 LLVMBuildLoad(gallivm
->builder
,
1287 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][i
],
1293 si_llvm_export_vs(bld_base
, outputs
, noutput
);
1297 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context
* bld_base
)
1299 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1300 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
1301 struct lp_build_context
* base
= &bld_base
->base
;
1302 struct lp_build_context
* uint
=
1303 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1304 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1305 LLVMValueRef args
[9];
1306 LLVMValueRef last_args
[9] = { 0 };
1307 unsigned semantic_name
;
1308 int depth_index
= -1, stencil_index
= -1;
1311 while (!tgsi_parse_end_of_tokens(parse
)) {
1312 struct tgsi_full_declaration
*d
=
1313 &parse
->FullToken
.FullDeclaration
;
1317 tgsi_parse_token(parse
);
1319 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_PROPERTY
&&
1320 parse
->FullToken
.FullProperty
.Property
.PropertyName
==
1321 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
)
1322 shader
->fs_write_all
= TRUE
;
1324 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
1327 i
= si_store_shader_io_attribs(shader
, d
);
1331 semantic_name
= d
->Semantic
.Name
;
1332 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
1333 /* Select the correct target */
1334 switch(semantic_name
) {
1335 case TGSI_SEMANTIC_POSITION
:
1336 depth_index
= index
;
1338 case TGSI_SEMANTIC_STENCIL
:
1339 stencil_index
= index
;
1341 case TGSI_SEMANTIC_COLOR
:
1342 target
= V_008DFC_SQ_EXP_MRT
+ d
->Semantic
.Index
;
1343 if (si_shader_ctx
->shader
->key
.ps
.alpha_to_one
)
1344 LLVMBuildStore(bld_base
->base
.gallivm
->builder
,
1346 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][3]);
1348 if (d
->Semantic
.Index
== 0 &&
1349 si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_ALWAYS
)
1350 si_alpha_test(bld_base
,
1351 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
]);
1356 "Warning: SI unhandled fs output type:%d\n",
1360 si_llvm_init_export_args_load(bld_base
,
1361 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
],
1364 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
1365 /* If there is an export instruction waiting to be emitted, do so now. */
1367 lp_build_intrinsic(base
->gallivm
->builder
,
1369 LLVMVoidTypeInContext(base
->gallivm
->context
),
1373 /* This instruction will be emitted at the end of the shader. */
1374 memcpy(last_args
, args
, sizeof(args
));
1376 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1377 if (shader
->fs_write_all
&& shader
->output
[i
].sid
== 0 &&
1378 si_shader_ctx
->shader
->key
.ps
.nr_cbufs
> 1) {
1379 for (int c
= 1; c
< si_shader_ctx
->shader
->key
.ps
.nr_cbufs
; c
++) {
1380 si_llvm_init_export_args_load(bld_base
,
1381 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
],
1382 V_008DFC_SQ_EXP_MRT
+ c
, args
);
1383 lp_build_intrinsic(base
->gallivm
->builder
,
1385 LLVMVoidTypeInContext(base
->gallivm
->context
),
1390 lp_build_intrinsic(base
->gallivm
->builder
,
1392 LLVMVoidTypeInContext(base
->gallivm
->context
),
1398 if (depth_index
>= 0 || stencil_index
>= 0) {
1399 LLVMValueRef out_ptr
;
1402 /* Specify the target we are exporting */
1403 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
1405 if (depth_index
>= 0) {
1406 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
1407 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1410 if (stencil_index
< 0) {
1417 if (stencil_index
>= 0) {
1418 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
1421 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1422 /* Only setting the stencil component bit (0x2) here
1423 * breaks some stencil piglit tests
1427 if (depth_index
< 0)
1431 /* Specify which components to enable */
1432 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
1436 args
[4] = uint
->zero
;
1439 lp_build_intrinsic(base
->gallivm
->builder
,
1441 LLVMVoidTypeInContext(base
->gallivm
->context
),
1444 memcpy(last_args
, args
, sizeof(args
));
1447 if (!last_args
[0]) {
1448 /* Specify which components to enable */
1449 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
1451 /* Specify the target we are exporting */
1452 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
1454 /* Set COMPR flag to zero to export data as 32-bit */
1455 last_args
[4] = uint
->zero
;
1458 last_args
[5]= uint
->zero
;
1459 last_args
[6]= uint
->zero
;
1460 last_args
[7]= uint
->zero
;
1461 last_args
[8]= uint
->zero
;
1463 si_shader_ctx
->shader
->spi_shader_col_format
|=
1464 V_028714_SPI_SHADER_32_ABGR
;
1465 si_shader_ctx
->shader
->cb_shader_mask
|= S_02823C_OUTPUT0_ENABLE(0xf);
1468 /* Specify whether the EXEC mask represents the valid mask */
1469 last_args
[1] = uint
->one
;
1471 /* Specify that this is the last export */
1472 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
1474 lp_build_intrinsic(base
->gallivm
->builder
,
1476 LLVMVoidTypeInContext(base
->gallivm
->context
),
1480 static const struct lp_build_tgsi_action txf_action
;
1482 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1483 struct lp_build_tgsi_context
* bld_base
,
1484 struct lp_build_emit_data
* emit_data
);
1486 static void tex_fetch_args(
1487 struct lp_build_tgsi_context
* bld_base
,
1488 struct lp_build_emit_data
* emit_data
)
1490 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1491 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1492 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
1493 unsigned opcode
= inst
->Instruction
.Opcode
;
1494 unsigned target
= inst
->Texture
.Texture
;
1495 LLVMValueRef coords
[4];
1496 LLVMValueRef address
[16];
1498 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
1501 unsigned sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1502 unsigned sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
1504 if (target
== TGSI_TEXTURE_BUFFER
) {
1505 LLVMTypeRef i128
= LLVMIntTypeInContext(gallivm
->context
, 128);
1506 LLVMTypeRef v2i128
= LLVMVectorType(i128
, 2);
1507 LLVMTypeRef i8
= LLVMInt8TypeInContext(gallivm
->context
);
1508 LLVMTypeRef v16i8
= LLVMVectorType(i8
, 16);
1510 /* Truncate v32i8 to v16i8. */
1511 LLVMValueRef res
= si_shader_ctx
->resources
[sampler_index
];
1512 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v2i128
, "");
1513 res
= LLVMBuildExtractElement(gallivm
->builder
, res
, bld_base
->uint_bld
.zero
, "");
1514 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v16i8
, "");
1516 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
1517 emit_data
->args
[0] = res
;
1518 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
1519 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
1520 emit_data
->arg_count
= 3;
1524 /* Fetch and project texture coordinates */
1525 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1526 for (chan
= 0; chan
< 3; chan
++ ) {
1527 coords
[chan
] = lp_build_emit_fetch(bld_base
,
1530 if (opcode
== TGSI_OPCODE_TXP
)
1531 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1537 if (opcode
== TGSI_OPCODE_TXP
)
1538 coords
[3] = bld_base
->base
.one
;
1540 /* Pack LOD bias value */
1541 if (opcode
== TGSI_OPCODE_TXB
)
1542 address
[count
++] = coords
[3];
1544 if (target
== TGSI_TEXTURE_CUBE
|| target
== TGSI_TEXTURE_SHADOWCUBE
)
1545 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
1547 /* Pack depth comparison value */
1549 case TGSI_TEXTURE_SHADOW1D
:
1550 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1551 case TGSI_TEXTURE_SHADOW2D
:
1552 case TGSI_TEXTURE_SHADOWRECT
:
1553 case TGSI_TEXTURE_SHADOWCUBE
:
1554 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1555 assert(ref_pos
>= 0);
1556 address
[count
++] = coords
[ref_pos
];
1558 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1559 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
1562 /* Pack user derivatives */
1563 if (opcode
== TGSI_OPCODE_TXD
) {
1564 for (chan
= 0; chan
< 2; chan
++) {
1565 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
1567 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 2, chan
);
1571 /* Pack texture coordinates */
1572 address
[count
++] = coords
[0];
1574 address
[count
++] = coords
[1];
1576 address
[count
++] = coords
[2];
1578 /* Pack LOD or sample index */
1579 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
1580 address
[count
++] = coords
[3];
1583 assert(!"Cannot handle more than 16 texture address parameters");
1587 for (chan
= 0; chan
< count
; chan
++ ) {
1588 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
1590 LLVMInt32TypeInContext(gallivm
->context
),
1594 /* Adjust the sample index according to FMASK.
1596 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1597 * which is the identity mapping. Each nibble says which physical sample
1598 * should be fetched to get that sample.
1600 * For example, 0x11111100 means there are only 2 samples stored and
1601 * the second sample covers 3/4 of the pixel. When reading samples 0
1602 * and 1, return physical sample 0 (determined by the first two 0s
1603 * in FMASK), otherwise return physical sample 1.
1605 * The sample index should be adjusted as follows:
1606 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1608 if (target
== TGSI_TEXTURE_2D_MSAA
||
1609 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1610 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1611 struct lp_build_emit_data txf_emit_data
= *emit_data
;
1612 LLVMValueRef txf_address
[4];
1613 unsigned txf_count
= count
;
1615 memcpy(txf_address
, address
, sizeof(txf_address
));
1617 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1618 txf_address
[2] = bld_base
->uint_bld
.zero
;
1620 txf_address
[3] = bld_base
->uint_bld
.zero
;
1622 /* Pad to a power-of-two size. */
1623 while (txf_count
< util_next_power_of_two(txf_count
))
1624 txf_address
[txf_count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1626 /* Read FMASK using TXF. */
1627 txf_emit_data
.chan
= 0;
1628 txf_emit_data
.dst_type
= LLVMVectorType(
1629 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
), 4);
1630 txf_emit_data
.args
[0] = lp_build_gather_values(gallivm
, txf_address
, txf_count
);
1631 txf_emit_data
.args
[1] = si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ sampler_index
];
1632 txf_emit_data
.args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
1633 target
== TGSI_TEXTURE_2D_MSAA
? TGSI_TEXTURE_2D
: TGSI_TEXTURE_2D_ARRAY
);
1634 txf_emit_data
.arg_count
= 3;
1636 build_tex_intrinsic(&txf_action
, bld_base
, &txf_emit_data
);
1638 /* Initialize some constants. */
1639 LLVMValueRef four
= LLVMConstInt(uint_bld
->elem_type
, 4, 0);
1640 LLVMValueRef F
= LLVMConstInt(uint_bld
->elem_type
, 0xF, 0);
1642 /* Apply the formula. */
1643 LLVMValueRef fmask
=
1644 LLVMBuildExtractElement(gallivm
->builder
,
1645 txf_emit_data
.output
[0],
1646 uint_bld
->zero
, "");
1648 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
1650 LLVMValueRef sample_index4
=
1651 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
1653 LLVMValueRef shifted_fmask
=
1654 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
1656 LLVMValueRef final_sample
=
1657 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
1659 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1660 * resource descriptor is 0 (invalid),
1662 LLVMValueRef fmask_desc
=
1663 LLVMBuildBitCast(gallivm
->builder
,
1664 si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ sampler_index
],
1665 LLVMVectorType(uint_bld
->elem_type
, 8), "");
1667 LLVMValueRef fmask_word1
=
1668 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
1671 LLVMValueRef word1_is_nonzero
=
1672 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1673 fmask_word1
, uint_bld
->zero
, "");
1675 /* Replace the MSAA sample index. */
1676 address
[sample_chan
] =
1677 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
1678 final_sample
, address
[sample_chan
], "");
1682 emit_data
->args
[1] = si_shader_ctx
->resources
[sampler_index
];
1684 if (opcode
== TGSI_OPCODE_TXF
) {
1685 /* add tex offsets */
1686 if (inst
->Texture
.NumOffsets
) {
1687 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1688 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1689 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
1691 assert(inst
->Texture
.NumOffsets
== 1);
1694 case TGSI_TEXTURE_3D
:
1695 address
[2] = lp_build_add(uint_bld
, address
[2],
1696 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
1698 case TGSI_TEXTURE_2D
:
1699 case TGSI_TEXTURE_SHADOW2D
:
1700 case TGSI_TEXTURE_RECT
:
1701 case TGSI_TEXTURE_SHADOWRECT
:
1702 case TGSI_TEXTURE_2D_ARRAY
:
1703 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1705 lp_build_add(uint_bld
, address
[1],
1706 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
1708 case TGSI_TEXTURE_1D
:
1709 case TGSI_TEXTURE_SHADOW1D
:
1710 case TGSI_TEXTURE_1D_ARRAY
:
1711 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1713 lp_build_add(uint_bld
, address
[0],
1714 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
1716 /* texture offsets do not apply to other texture targets */
1720 emit_data
->dst_type
= LLVMVectorType(
1721 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
1724 emit_data
->arg_count
= 3;
1727 emit_data
->args
[2] = si_shader_ctx
->samplers
[sampler_index
];
1729 emit_data
->dst_type
= LLVMVectorType(
1730 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
1733 emit_data
->arg_count
= 4;
1737 emit_data
->args
[emit_data
->arg_count
- 1] =
1738 lp_build_const_int32(bld_base
->base
.gallivm
, target
);
1740 /* Pad to power of two vector */
1741 while (count
< util_next_power_of_two(count
))
1742 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1744 emit_data
->args
[0] = lp_build_gather_values(gallivm
, address
, count
);
1747 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1748 struct lp_build_tgsi_context
* bld_base
,
1749 struct lp_build_emit_data
* emit_data
)
1751 struct lp_build_context
* base
= &bld_base
->base
;
1752 char intr_name
[127];
1754 if (emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
1755 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1756 base
->gallivm
->builder
,
1757 "llvm.SI.vs.load.input", emit_data
->dst_type
,
1758 emit_data
->args
, emit_data
->arg_count
,
1759 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1763 sprintf(intr_name
, "%sv%ui32", action
->intr_name
,
1764 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
1766 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1767 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
1768 emit_data
->args
, emit_data
->arg_count
,
1769 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1772 static void txq_fetch_args(
1773 struct lp_build_tgsi_context
* bld_base
,
1774 struct lp_build_emit_data
* emit_data
)
1776 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1777 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1778 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1780 if (inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
1781 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1782 LLVMTypeRef v8i32
= LLVMVectorType(i32
, 8);
1784 /* Read the size from the buffer descriptor directly. */
1785 LLVMValueRef size
= si_shader_ctx
->resources
[inst
->Src
[1].Register
.Index
];
1786 size
= LLVMBuildBitCast(gallivm
->builder
, size
, v8i32
, "");
1787 size
= LLVMBuildExtractElement(gallivm
->builder
, size
,
1788 lp_build_const_int32(gallivm
, 2), "");
1789 emit_data
->args
[0] = size
;
1794 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1797 emit_data
->args
[1] = si_shader_ctx
->resources
[inst
->Src
[1].Register
.Index
];
1800 emit_data
->args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
1801 inst
->Texture
.Texture
);
1803 emit_data
->arg_count
= 3;
1805 emit_data
->dst_type
= LLVMVectorType(
1806 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
1810 static void build_txq_intrinsic(const struct lp_build_tgsi_action
* action
,
1811 struct lp_build_tgsi_context
* bld_base
,
1812 struct lp_build_emit_data
* emit_data
)
1814 if (emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
1815 /* Just return the buffer size. */
1816 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
1820 build_tgsi_intrinsic_nomem(action
, bld_base
, emit_data
);
1823 #if HAVE_LLVM >= 0x0304
1825 static void si_llvm_emit_ddxy(
1826 const struct lp_build_tgsi_action
* action
,
1827 struct lp_build_tgsi_context
* bld_base
,
1828 struct lp_build_emit_data
* emit_data
)
1830 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1831 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1832 struct lp_build_context
* base
= &bld_base
->base
;
1833 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1834 unsigned opcode
= inst
->Instruction
.Opcode
;
1835 LLVMValueRef indices
[2];
1836 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1837 LLVMValueRef tl
, trbl
, result
[4];
1839 unsigned swizzle
[4];
1842 i32
= LLVMInt32TypeInContext(gallivm
->context
);
1844 indices
[0] = bld_base
->uint_bld
.zero
;
1845 indices
[1] = build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
1846 NULL
, 0, LLVMReadNoneAttribute
);
1847 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1850 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
1851 lp_build_const_int32(gallivm
, 0xfffffffc), "");
1852 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1855 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
1856 lp_build_const_int32(gallivm
,
1857 opcode
== TGSI_OPCODE_DDX
? 1 : 2),
1859 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1862 for (c
= 0; c
< 4; ++c
) {
1865 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
1866 for (i
= 0; i
< c
; ++i
) {
1867 if (swizzle
[i
] == swizzle
[c
]) {
1868 result
[c
] = result
[i
];
1875 LLVMBuildStore(gallivm
->builder
,
1876 LLVMBuildBitCast(gallivm
->builder
,
1877 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
1881 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
1882 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
1884 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
1885 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, base
->elem_type
, "");
1887 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
1890 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
1893 #endif /* HAVE_LLVM >= 0x0304 */
1895 /* Emit one vertex from the geometry shader */
1896 static void si_llvm_emit_vertex(
1897 const struct lp_build_tgsi_action
*action
,
1898 struct lp_build_tgsi_context
*bld_base
,
1899 struct lp_build_emit_data
*emit_data
)
1901 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1902 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
1903 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
1904 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1905 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1906 LLVMValueRef gs_next_vertex
;
1907 LLVMValueRef t_list_ptr
;
1908 LLVMValueRef t_list
;
1909 LLVMValueRef args
[2];
1913 /* Load the GSVS ring resource descriptor */
1914 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1915 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
1916 lp_build_const_int32(gallivm
,
1917 NUM_PIPE_CONST_BUFFERS
+ 2));
1919 if (shader
->noutput
== 0) {
1920 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1922 while (!tgsi_parse_end_of_tokens(parse
)) {
1923 tgsi_parse_token(parse
);
1925 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
1926 struct tgsi_full_declaration
*d
= &parse
->FullToken
.FullDeclaration
;
1928 if (d
->Declaration
.File
== TGSI_FILE_OUTPUT
)
1929 si_store_shader_io_attribs(shader
, d
);
1934 /* Write vertex attribute values to GSVS ring */
1935 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
, si_shader_ctx
->gs_next_vertex
, "");
1936 for (i
= 0; i
< shader
->noutput
; i
++) {
1937 LLVMValueRef
*out_ptr
=
1938 si_shader_ctx
->radeon_bld
.soa
.outputs
[shader
->output
[i
].index
];
1940 for (chan
= 0; chan
< 4; chan
++) {
1941 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
1942 LLVMValueRef soffset
=
1943 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1944 SI_PARAM_GS2VS_OFFSET
);
1945 LLVMValueRef voffset
=
1946 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
1947 shader
->gs_max_out_vertices
);
1949 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
1950 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
1952 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
1954 build_tbuffer_store(si_shader_ctx
, t_list
, out_val
, 1,
1955 voffset
, soffset
, 0,
1956 V_008F0C_BUF_DATA_FORMAT_32
,
1957 V_008F0C_BUF_NUM_FORMAT_UINT
,
1961 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
1962 lp_build_const_int32(gallivm
, 1));
1963 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, si_shader_ctx
->gs_next_vertex
);
1965 /* Signal vertex emission */
1966 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
);
1967 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
1968 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
1969 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
1970 LLVMNoUnwindAttribute
);
1973 /* Cut one primitive from the geometry shader */
1974 static void si_llvm_emit_primitive(
1975 const struct lp_build_tgsi_action
*action
,
1976 struct lp_build_tgsi_context
*bld_base
,
1977 struct lp_build_emit_data
*emit_data
)
1979 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1980 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1981 LLVMValueRef args
[2];
1983 /* Signal primitive cut */
1984 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
);
1985 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
1986 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
1987 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
1988 LLVMNoUnwindAttribute
);
1991 static const struct lp_build_tgsi_action tex_action
= {
1992 .fetch_args
= tex_fetch_args
,
1993 .emit
= build_tex_intrinsic
,
1994 .intr_name
= "llvm.SI.sample."
1997 static const struct lp_build_tgsi_action txb_action
= {
1998 .fetch_args
= tex_fetch_args
,
1999 .emit
= build_tex_intrinsic
,
2000 .intr_name
= "llvm.SI.sampleb."
2003 #if HAVE_LLVM >= 0x0304
2004 static const struct lp_build_tgsi_action txd_action
= {
2005 .fetch_args
= tex_fetch_args
,
2006 .emit
= build_tex_intrinsic
,
2007 .intr_name
= "llvm.SI.sampled."
2011 static const struct lp_build_tgsi_action txf_action
= {
2012 .fetch_args
= tex_fetch_args
,
2013 .emit
= build_tex_intrinsic
,
2014 .intr_name
= "llvm.SI.imageload."
2017 static const struct lp_build_tgsi_action txl_action
= {
2018 .fetch_args
= tex_fetch_args
,
2019 .emit
= build_tex_intrinsic
,
2020 .intr_name
= "llvm.SI.samplel."
2023 static const struct lp_build_tgsi_action txq_action
= {
2024 .fetch_args
= txq_fetch_args
,
2025 .emit
= build_txq_intrinsic
,
2026 .intr_name
= "llvm.SI.resinfo"
2029 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
2031 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
2032 LLVMValueRef args
[3];
2034 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
2036 args
[2] = lp_build_const_int32(gallivm
, 1);
2038 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
2041 static void create_function(struct si_shader_context
*si_shader_ctx
)
2043 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2044 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2045 struct si_pipe_shader
*shader
= si_shader_ctx
->shader
;
2046 LLVMTypeRef params
[21], f32
, i8
, i32
, v2i32
, v3i32
;
2047 unsigned i
, last_sgpr
, num_params
;
2049 i8
= LLVMInt8TypeInContext(gallivm
->context
);
2050 i32
= LLVMInt32TypeInContext(gallivm
->context
);
2051 f32
= LLVMFloatTypeInContext(gallivm
->context
);
2052 v2i32
= LLVMVectorType(i32
, 2);
2053 v3i32
= LLVMVectorType(i32
, 3);
2055 params
[SI_PARAM_CONST
] = LLVMPointerType(
2056 LLVMArrayType(LLVMVectorType(i8
, 16), NUM_CONST_BUFFERS
), CONST_ADDR_SPACE
);
2057 /* We assume at most 16 textures per program at the moment.
2058 * This need probably need to be changed to support bindless textures */
2059 params
[SI_PARAM_SAMPLER
] = LLVMPointerType(
2060 LLVMArrayType(LLVMVectorType(i8
, 16), NUM_SAMPLER_VIEWS
), CONST_ADDR_SPACE
);
2061 params
[SI_PARAM_RESOURCE
] = LLVMPointerType(
2062 LLVMArrayType(LLVMVectorType(i8
, 32), NUM_SAMPLER_STATES
), CONST_ADDR_SPACE
);
2064 switch (si_shader_ctx
->type
) {
2065 case TGSI_PROCESSOR_VERTEX
:
2066 params
[SI_PARAM_VERTEX_BUFFER
] = params
[SI_PARAM_CONST
];
2067 params
[SI_PARAM_SO_BUFFER
] = params
[SI_PARAM_CONST
];
2068 params
[SI_PARAM_START_INSTANCE
] = i32
;
2069 num_params
= SI_PARAM_START_INSTANCE
+1;
2070 if (shader
->key
.vs
.as_es
) {
2071 params
[SI_PARAM_ES2GS_OFFSET
] = i32
;
2074 /* The locations of the other parameters are assigned dynamically. */
2076 /* Streamout SGPRs. */
2077 if (shader
->selector
->so
.num_outputs
) {
2078 params
[si_shader_ctx
->param_streamout_config
= num_params
++] = i32
;
2079 params
[si_shader_ctx
->param_streamout_write_index
= num_params
++] = i32
;
2081 /* A streamout buffer offset is loaded if the stride is non-zero. */
2082 for (i
= 0; i
< 4; i
++) {
2083 if (!shader
->selector
->so
.stride
[i
])
2086 params
[si_shader_ctx
->param_streamout_offset
[i
] = num_params
++] = i32
;
2090 last_sgpr
= num_params
-1;
2093 params
[si_shader_ctx
->param_vertex_id
= num_params
++] = i32
;
2094 params
[num_params
++] = i32
; /* unused*/
2095 params
[num_params
++] = i32
; /* unused */
2096 params
[si_shader_ctx
->param_instance_id
= num_params
++] = i32
;
2099 case TGSI_PROCESSOR_GEOMETRY
:
2100 params
[SI_PARAM_GS2VS_OFFSET
] = i32
;
2101 params
[SI_PARAM_GS_WAVE_ID
] = i32
;
2102 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
2105 params
[SI_PARAM_VTX0_OFFSET
] = i32
;
2106 params
[SI_PARAM_VTX1_OFFSET
] = i32
;
2107 params
[SI_PARAM_PRIMITIVE_ID
] = i32
;
2108 params
[SI_PARAM_VTX2_OFFSET
] = i32
;
2109 params
[SI_PARAM_VTX3_OFFSET
] = i32
;
2110 params
[SI_PARAM_VTX4_OFFSET
] = i32
;
2111 params
[SI_PARAM_VTX5_OFFSET
] = i32
;
2112 params
[SI_PARAM_GS_INSTANCE_ID
] = i32
;
2113 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
2116 case TGSI_PROCESSOR_FRAGMENT
:
2117 params
[SI_PARAM_ALPHA_REF
] = f32
;
2118 params
[SI_PARAM_PRIM_MASK
] = i32
;
2119 last_sgpr
= SI_PARAM_PRIM_MASK
;
2120 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
2121 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
2122 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
2123 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
2124 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
2125 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
2126 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
2127 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
2128 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
2129 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
2130 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
2131 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
2132 params
[SI_PARAM_FRONT_FACE
] = f32
;
2133 params
[SI_PARAM_ANCILLARY
] = f32
;
2134 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
2135 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
2136 num_params
= SI_PARAM_POS_FIXED_PT
+1;
2140 assert(0 && "unimplemented shader");
2144 assert(num_params
<= Elements(params
));
2145 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, num_params
);
2146 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
2148 for (i
= 0; i
<= last_sgpr
; ++i
) {
2149 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
2152 LLVMAddAttribute(P
, LLVMInRegAttribute
);
2154 #if HAVE_LLVM >= 0x0304
2155 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2156 * to move load. Inputs are constant so this is fine. */
2157 case SI_PARAM_CONST
:
2158 case SI_PARAM_SAMPLER
:
2159 case SI_PARAM_RESOURCE
:
2160 LLVMAddAttribute(P
, LLVMByValAttribute
);
2166 #if HAVE_LLVM >= 0x0304
2167 if (bld_base
->info
&&
2168 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
2169 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0))
2170 si_shader_ctx
->ddxy_lds
=
2171 LLVMAddGlobalInAddressSpace(gallivm
->module
,
2172 LLVMArrayType(i32
, 64),
2178 static void preload_constants(struct si_shader_context
*si_shader_ctx
)
2180 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2181 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2182 const struct tgsi_shader_info
* info
= bld_base
->info
;
2184 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
2186 for (buf
= 0; buf
< NUM_CONST_BUFFERS
; buf
++) {
2187 unsigned i
, num_const
= info
->const_file_max
[buf
] + 1;
2192 /* Allocate space for the constant values */
2193 si_shader_ctx
->constants
[buf
] = CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
2195 /* Load the resource descriptor */
2196 si_shader_ctx
->const_resource
[buf
] =
2197 build_indexed_load(si_shader_ctx
, ptr
, lp_build_const_int32(gallivm
, buf
));
2199 /* Load the constants, we rely on the code sinking to do the rest */
2200 for (i
= 0; i
< num_const
* 4; ++i
) {
2201 LLVMValueRef args
[2] = {
2202 si_shader_ctx
->const_resource
[buf
],
2203 lp_build_const_int32(gallivm
, i
* 4)
2205 si_shader_ctx
->constants
[buf
][i
] =
2206 build_intrinsic(gallivm
->builder
, "llvm.SI.load.const",
2207 bld_base
->base
.elem_type
, args
, 2,
2208 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
2213 static void preload_samplers(struct si_shader_context
*si_shader_ctx
)
2215 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2216 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2217 const struct tgsi_shader_info
* info
= bld_base
->info
;
2219 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
2221 LLVMValueRef res_ptr
, samp_ptr
;
2222 LLVMValueRef offset
;
2224 if (num_samplers
== 0)
2227 /* Allocate space for the values */
2228 si_shader_ctx
->resources
= CALLOC(NUM_SAMPLER_VIEWS
, sizeof(LLVMValueRef
));
2229 si_shader_ctx
->samplers
= CALLOC(num_samplers
, sizeof(LLVMValueRef
));
2231 res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
2232 samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
2234 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2235 for (i
= 0; i
< num_samplers
; ++i
) {
2237 offset
= lp_build_const_int32(gallivm
, i
);
2238 si_shader_ctx
->resources
[i
] = build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
2241 offset
= lp_build_const_int32(gallivm
, i
);
2242 si_shader_ctx
->samplers
[i
] = build_indexed_load(si_shader_ctx
, samp_ptr
, offset
);
2244 /* FMASK resource */
2245 if (info
->is_msaa_sampler
[i
]) {
2246 offset
= lp_build_const_int32(gallivm
, FMASK_TEX_OFFSET
+ i
);
2247 si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ i
] =
2248 build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
2253 static void preload_streamout_buffers(struct si_shader_context
*si_shader_ctx
)
2255 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2256 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2259 if (si_shader_ctx
->type
!= TGSI_PROCESSOR_VERTEX
||
2260 si_shader_ctx
->shader
->key
.vs
.as_es
||
2261 !si_shader_ctx
->shader
->selector
->so
.num_outputs
)
2264 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2265 SI_PARAM_SO_BUFFER
);
2267 /* Load the resources, we rely on the code sinking to do the rest */
2268 for (i
= 0; i
< 4; ++i
) {
2269 if (si_shader_ctx
->shader
->selector
->so
.stride
[i
]) {
2270 LLVMValueRef offset
= lp_build_const_int32(gallivm
, i
);
2272 si_shader_ctx
->so_buffers
[i
] = build_indexed_load(si_shader_ctx
, buf_ptr
, offset
);
2277 int si_compile_llvm(struct si_context
*sctx
, struct si_pipe_shader
*shader
,
2282 struct radeon_llvm_binary binary
;
2283 bool dump
= r600_can_dump_shader(&sctx
->screen
->b
,
2284 shader
->selector
? shader
->selector
->tokens
: NULL
);
2285 memset(&binary
, 0, sizeof(binary
));
2286 radeon_llvm_compile(mod
, &binary
,
2287 r600_get_llvm_processor_name(sctx
->screen
->b
.family
), dump
);
2288 if (dump
&& ! binary
.disassembled
) {
2289 fprintf(stderr
, "SI CODE:\n");
2290 for (i
= 0; i
< binary
.code_size
; i
+=4 ) {
2291 fprintf(stderr
, "%02x%02x%02x%02x\n", binary
.code
[i
+ 3],
2292 binary
.code
[i
+ 2], binary
.code
[i
+ 1],
2297 /* XXX: We may be able to emit some of these values directly rather than
2298 * extracting fields to be emitted later.
2300 for (i
= 0; i
< binary
.config_size
; i
+= 8) {
2301 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
));
2302 unsigned value
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
+ 4));
2304 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
2305 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
2306 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
2307 case R_00B848_COMPUTE_PGM_RSRC1
:
2308 shader
->num_sgprs
= (G_00B028_SGPRS(value
) + 1) * 8;
2309 shader
->num_vgprs
= (G_00B028_VGPRS(value
) + 1) * 4;
2311 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
2312 shader
->lds_size
= G_00B02C_EXTRA_LDS_SIZE(value
);
2314 case R_00B84C_COMPUTE_PGM_RSRC2
:
2315 shader
->lds_size
= G_00B84C_LDS_SIZE(value
);
2317 case R_0286CC_SPI_PS_INPUT_ENA
:
2318 shader
->spi_ps_input_ena
= value
;
2321 fprintf(stderr
, "Warning: Compiler emitted unknown "
2322 "config register: 0x%x\n", reg
);
2327 /* copy new shader */
2328 r600_resource_reference(&shader
->bo
, NULL
);
2329 shader
->bo
= si_resource_create_custom(sctx
->b
.b
.screen
, PIPE_USAGE_IMMUTABLE
,
2331 if (shader
->bo
== NULL
) {
2335 ptr
= (uint32_t*)sctx
->b
.ws
->buffer_map(shader
->bo
->cs_buf
, sctx
->b
.rings
.gfx
.cs
, PIPE_TRANSFER_WRITE
);
2336 if (0 /*SI_BIG_ENDIAN*/) {
2337 for (i
= 0; i
< binary
.code_size
/ 4; ++i
) {
2338 ptr
[i
] = util_bswap32(*(uint32_t*)(binary
.code
+ i
*4));
2341 memcpy(ptr
, binary
.code
, binary
.code_size
);
2343 sctx
->b
.ws
->buffer_unmap(shader
->bo
->cs_buf
);
2346 free(binary
.config
);
2351 /* Generate code for the hardware VS shader stage to go with a geometry shader */
2352 static int si_generate_gs_copy_shader(struct si_context
*sctx
,
2353 struct si_shader_context
*si_shader_ctx
,
2356 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
2357 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2358 struct lp_build_context
*base
= &bld_base
->base
;
2359 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2360 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
2361 struct si_shader
*gs
= &si_shader_ctx
->shader
->selector
->current
->shader
;
2362 struct si_shader_output_values
*outputs
;
2363 LLVMValueRef t_list_ptr
, t_list
;
2364 LLVMValueRef args
[9];
2367 outputs
= MALLOC(gs
->noutput
* sizeof(outputs
[0]));
2369 si_shader_ctx
->type
= TGSI_PROCESSOR_VERTEX
;
2370 si_shader_ctx
->gs_for_vs
= gs
;
2372 radeon_llvm_context_init(&si_shader_ctx
->radeon_bld
);
2374 create_meta_data(si_shader_ctx
);
2375 create_function(si_shader_ctx
);
2376 preload_streamout_buffers(si_shader_ctx
);
2378 /* Load the GSVS ring resource descriptor */
2379 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
2380 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
2381 lp_build_const_int32(gallivm
,
2382 NUM_PIPE_CONST_BUFFERS
+ 1));
2385 args
[1] = lp_build_mul_imm(uint
,
2386 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2387 si_shader_ctx
->param_vertex_id
),
2389 args
[3] = uint
->zero
;
2390 args
[4] = uint
->one
; /* OFFEN */
2391 args
[5] = uint
->zero
; /* IDXEN */
2392 args
[6] = uint
->one
; /* GLC */
2393 args
[7] = uint
->one
; /* SLC */
2394 args
[8] = uint
->zero
; /* TFE */
2396 /* Fetch vertex data from GSVS ring */
2397 for (i
= 0; i
< gs
->noutput
; ++i
) {
2398 struct si_shader_output
*out
= gs
->output
+ i
;
2401 shader
->output
[i
] = *out
;
2403 outputs
[i
].name
= out
->name
;
2404 outputs
[i
].index
= out
->index
;
2405 outputs
[i
].sid
= out
->sid
;
2406 outputs
[i
].usage
= out
->usage
;
2408 for (chan
= 0; chan
< 4; chan
++) {
2409 args
[2] = lp_build_const_int32(gallivm
,
2411 gs
->gs_max_out_vertices
* 16 * 4);
2413 outputs
[i
].values
[chan
] =
2414 LLVMBuildBitCast(gallivm
->builder
,
2415 build_intrinsic(gallivm
->builder
,
2416 "llvm.SI.buffer.load.dword.i32.i32",
2417 LLVMInt32TypeInContext(gallivm
->context
),
2419 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
2420 base
->elem_type
, "");
2423 shader
->noutput
= gs
->noutput
;
2425 si_llvm_export_vs(bld_base
, outputs
, gs
->noutput
);
2427 radeon_llvm_finalize_module(&si_shader_ctx
->radeon_bld
);
2430 fprintf(stderr
, "Copy Vertex Shader for Geometry Shader:\n\n");
2432 r
= si_compile_llvm(sctx
, si_shader_ctx
->shader
,
2433 bld_base
->base
.gallivm
->module
);
2435 radeon_llvm_dispose(&si_shader_ctx
->radeon_bld
);
2441 int si_pipe_shader_create(
2442 struct pipe_context
*ctx
,
2443 struct si_pipe_shader
*shader
)
2445 struct si_context
*sctx
= (struct si_context
*)ctx
;
2446 struct si_pipe_shader_selector
*sel
= shader
->selector
;
2447 struct si_shader_context si_shader_ctx
;
2448 struct tgsi_shader_info shader_info
;
2449 struct lp_build_tgsi_context
* bld_base
;
2452 bool dump
= r600_can_dump_shader(&sctx
->screen
->b
, sel
->tokens
);
2454 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2455 * conversion fails. */
2457 tgsi_dump(sel
->tokens
, 0);
2458 si_dump_streamout(&sel
->so
);
2461 assert(shader
->shader
.noutput
== 0);
2462 assert(shader
->shader
.nparam
== 0);
2463 assert(shader
->shader
.ninput
== 0);
2465 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
2466 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
2467 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
2469 tgsi_scan_shader(sel
->tokens
, &shader_info
);
2471 shader
->shader
.uses_kill
= shader_info
.uses_kill
;
2472 shader
->shader
.uses_instanceid
= shader_info
.uses_instanceid
;
2473 bld_base
->info
= &shader_info
;
2474 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
2476 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
2477 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = txb_action
;
2478 #if HAVE_LLVM >= 0x0304
2479 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = txd_action
;
2481 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = txf_action
;
2482 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = txl_action
;
2483 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
2484 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = txq_action
;
2486 #if HAVE_LLVM >= 0x0304
2487 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
2488 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
2491 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
2492 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
2494 si_shader_ctx
.radeon_bld
.load_system_value
= declare_system_value
;
2495 si_shader_ctx
.tokens
= sel
->tokens
;
2496 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
2497 si_shader_ctx
.shader
= shader
;
2498 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
2500 switch (si_shader_ctx
.type
) {
2501 case TGSI_PROCESSOR_VERTEX
:
2502 si_shader_ctx
.radeon_bld
.load_input
= declare_input_vs
;
2503 if (shader
->key
.vs
.as_es
) {
2504 si_shader_ctx
.gs_for_vs
= &sctx
->gs_shader
->current
->shader
;
2505 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
2507 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
2510 case TGSI_PROCESSOR_GEOMETRY
: {
2513 si_shader_ctx
.radeon_bld
.load_input
= declare_input_gs
;
2514 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
2515 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
2517 for (i
= 0; i
< shader_info
.num_properties
; i
++) {
2518 switch (shader_info
.properties
[i
].name
) {
2519 case TGSI_PROPERTY_GS_INPUT_PRIM
:
2520 shader
->shader
.gs_input_prim
= shader_info
.properties
[i
].data
[0];
2522 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
2523 shader
->shader
.gs_output_prim
= shader_info
.properties
[i
].data
[0];
2525 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
:
2526 shader
->shader
.gs_max_out_vertices
= shader_info
.properties
[i
].data
[0];
2532 case TGSI_PROCESSOR_FRAGMENT
:
2533 si_shader_ctx
.radeon_bld
.load_input
= declare_input_fs
;
2534 bld_base
->emit_epilogue
= si_llvm_emit_fs_epilogue
;
2537 assert(!"Unsupported shader type");
2541 create_meta_data(&si_shader_ctx
);
2542 create_function(&si_shader_ctx
);
2543 preload_constants(&si_shader_ctx
);
2544 preload_samplers(&si_shader_ctx
);
2545 preload_streamout_buffers(&si_shader_ctx
);
2547 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
2548 si_shader_ctx
.gs_next_vertex
=
2549 lp_build_alloca(bld_base
->base
.gallivm
,
2550 bld_base
->uint_bld
.elem_type
, "");
2553 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
2554 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
2558 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
2560 mod
= bld_base
->base
.gallivm
->module
;
2561 r
= si_compile_llvm(sctx
, shader
, mod
);
2563 fprintf(stderr
, "LLVM failed to compile shader\n");
2567 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
2569 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
2570 shader
->gs_copy_shader
= CALLOC_STRUCT(si_pipe_shader
);
2571 shader
->gs_copy_shader
->selector
= shader
->selector
;
2572 shader
->gs_copy_shader
->key
= shader
->key
;
2573 si_shader_ctx
.shader
= shader
->gs_copy_shader
;
2574 if ((r
= si_generate_gs_copy_shader(sctx
, &si_shader_ctx
, dump
))) {
2575 free(shader
->gs_copy_shader
);
2576 shader
->gs_copy_shader
= NULL
;
2581 tgsi_parse_free(&si_shader_ctx
.parse
);
2584 for (int i
= 0; i
< NUM_CONST_BUFFERS
; i
++)
2585 FREE(si_shader_ctx
.constants
[i
]);
2586 FREE(si_shader_ctx
.resources
);
2587 FREE(si_shader_ctx
.samplers
);
2592 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
2594 r600_resource_reference(&shader
->bo
, NULL
);