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
, SI_RING_ESGS
));
327 args
[1] = vtx_offset
;
328 args
[2] = lp_build_const_int32(gallivm
,
329 ((shader
->input
[reg
->Register
.Index
].param_offset
* 4) +
331 args
[3] = uint
->zero
;
332 args
[4] = uint
->one
; /* OFFEN */
333 args
[5] = uint
->zero
; /* IDXEN */
334 args
[6] = uint
->one
; /* GLC */
335 args
[7] = uint
->zero
; /* SLC */
336 args
[8] = uint
->zero
; /* TFE */
338 return LLVMBuildBitCast(gallivm
->builder
,
339 build_intrinsic(gallivm
->builder
,
340 "llvm.SI.buffer.load.dword.i32.i32",
342 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
343 tgsi2llvmtype(bld_base
, type
), "");
346 static void declare_input_fs(
347 struct radeon_llvm_context
*radeon_bld
,
348 unsigned input_index
,
349 const struct tgsi_full_declaration
*decl
)
351 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
352 struct si_shader_context
*si_shader_ctx
=
353 si_shader_context(&radeon_bld
->soa
.bld_base
);
354 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
355 struct lp_build_context
*uint
= &radeon_bld
->soa
.bld_base
.uint_bld
;
356 struct gallivm_state
*gallivm
= base
->gallivm
;
357 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
358 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
360 LLVMValueRef interp_param
;
361 const char * intr_name
;
364 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
365 * quad begins a new primitive. Bit 0 always needs
367 * [32:16] ParamOffset
370 LLVMValueRef params
= LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
);
371 LLVMValueRef attr_number
;
375 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
376 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
378 radeon_llvm_reg_index_soa(input_index
, chan
);
379 radeon_bld
->inputs
[soa_index
] =
380 LLVMGetParam(main_fn
, SI_PARAM_POS_X_FLOAT
+ chan
);
383 /* RCP for fragcoord.w */
384 radeon_bld
->inputs
[soa_index
] =
385 LLVMBuildFDiv(gallivm
->builder
,
386 lp_build_const_float(gallivm
, 1.0f
),
387 radeon_bld
->inputs
[soa_index
],
393 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
394 LLVMValueRef face
, is_face_positive
;
396 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
398 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
400 lp_build_const_float(gallivm
, 0.0f
),
403 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
404 LLVMBuildSelect(gallivm
->builder
,
406 lp_build_const_float(gallivm
, 1.0f
),
407 lp_build_const_float(gallivm
, 0.0f
),
409 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
410 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
411 lp_build_const_float(gallivm
, 0.0f
);
412 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
413 lp_build_const_float(gallivm
, 1.0f
);
418 shader
->input
[input_index
].param_offset
= shader
->nparam
++;
419 attr_number
= lp_build_const_int32(gallivm
,
420 shader
->input
[input_index
].param_offset
);
422 switch (decl
->Interp
.Interpolate
) {
423 case TGSI_INTERPOLATE_COLOR
:
424 if (si_shader_ctx
->shader
->key
.ps
.flatshade
) {
427 if (decl
->Interp
.Centroid
)
428 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
430 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
433 case TGSI_INTERPOLATE_CONSTANT
:
436 case TGSI_INTERPOLATE_LINEAR
:
437 if (decl
->Interp
.Centroid
)
438 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTROID
);
440 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTER
);
442 case TGSI_INTERPOLATE_PERSPECTIVE
:
443 if (decl
->Interp
.Centroid
)
444 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
446 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
449 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
453 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
455 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
456 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
457 si_shader_ctx
->shader
->key
.ps
.color_two_side
) {
458 LLVMValueRef args
[4];
459 LLVMValueRef face
, is_face_positive
;
460 LLVMValueRef back_attr_number
=
461 lp_build_const_int32(gallivm
,
462 shader
->input
[input_index
].param_offset
+ 1);
464 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
466 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
468 lp_build_const_float(gallivm
, 0.0f
),
472 args
[3] = interp_param
;
473 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
474 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
475 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
476 LLVMValueRef front
, back
;
479 args
[1] = attr_number
;
480 front
= build_intrinsic(gallivm
->builder
, intr_name
,
481 input_type
, args
, args
[3] ? 4 : 3,
482 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
484 args
[1] = back_attr_number
;
485 back
= build_intrinsic(gallivm
->builder
, intr_name
,
486 input_type
, args
, args
[3] ? 4 : 3,
487 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
489 radeon_bld
->inputs
[soa_index
] =
490 LLVMBuildSelect(gallivm
->builder
,
498 } else if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FOG
) {
499 LLVMValueRef args
[4];
501 args
[0] = uint
->zero
;
502 args
[1] = attr_number
;
504 args
[3] = interp_param
;
505 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
506 build_intrinsic(gallivm
->builder
, intr_name
,
507 input_type
, args
, args
[3] ? 4 : 3,
508 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
509 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
510 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
511 lp_build_const_float(gallivm
, 0.0f
);
512 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
513 lp_build_const_float(gallivm
, 1.0f
);
515 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
516 LLVMValueRef args
[4];
517 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
518 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
520 args
[1] = attr_number
;
522 args
[3] = interp_param
;
523 radeon_bld
->inputs
[soa_index
] =
524 build_intrinsic(gallivm
->builder
, intr_name
,
525 input_type
, args
, args
[3] ? 4 : 3,
526 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
531 static void declare_system_value(
532 struct radeon_llvm_context
* radeon_bld
,
534 const struct tgsi_full_declaration
*decl
)
536 struct si_shader_context
*si_shader_ctx
=
537 si_shader_context(&radeon_bld
->soa
.bld_base
);
538 LLVMValueRef value
= 0;
540 switch (decl
->Semantic
.Name
) {
541 case TGSI_SEMANTIC_INSTANCEID
:
542 value
= LLVMGetParam(radeon_bld
->main_fn
,
543 si_shader_ctx
->param_instance_id
);
546 case TGSI_SEMANTIC_VERTEXID
:
547 value
= LLVMGetParam(radeon_bld
->main_fn
,
548 si_shader_ctx
->param_vertex_id
);
552 assert(!"unknown system value");
556 radeon_bld
->system_values
[index
] = value
;
559 static LLVMValueRef
fetch_constant(
560 struct lp_build_tgsi_context
* bld_base
,
561 const struct tgsi_full_src_register
*reg
,
562 enum tgsi_opcode_type type
,
565 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
566 struct lp_build_context
* base
= &bld_base
->base
;
567 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
570 LLVMValueRef args
[2];
574 if (swizzle
== LP_CHAN_ALL
) {
576 LLVMValueRef values
[4];
577 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
578 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
580 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
583 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
584 idx
= reg
->Register
.Index
* 4 + swizzle
;
586 if (!reg
->Register
.Indirect
)
587 return bitcast(bld_base
, type
, si_shader_ctx
->constants
[buf
][idx
]);
589 args
[0] = si_shader_ctx
->const_resource
[buf
];
590 args
[1] = lp_build_const_int32(base
->gallivm
, idx
* 4);
591 addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
592 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
593 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
594 args
[1] = lp_build_add(&bld_base
->uint_bld
, addr
, args
[1]);
596 result
= build_intrinsic(base
->gallivm
->builder
, "llvm.SI.load.const", base
->elem_type
,
597 args
, 2, LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
599 return bitcast(bld_base
, type
, result
);
602 /* Initialize arguments for the shader export intrinsic */
603 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
604 LLVMValueRef
*values
,
608 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
609 struct lp_build_context
*uint
=
610 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
611 struct lp_build_context
*base
= &bld_base
->base
;
612 unsigned compressed
= 0;
615 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
616 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
618 if (cbuf
>= 0 && cbuf
< 8) {
619 compressed
= (si_shader_ctx
->shader
->key
.ps
.export_16bpc
>> cbuf
) & 0x1;
622 si_shader_ctx
->shader
->spi_shader_col_format
|=
623 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
625 si_shader_ctx
->shader
->spi_shader_col_format
|=
626 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
628 si_shader_ctx
->shader
->cb_shader_mask
|= 0xf << (4 * cbuf
);
633 /* Pixel shader needs to pack output values before export */
634 for (chan
= 0; chan
< 2; chan
++ ) {
635 args
[0] = values
[2 * chan
];
636 args
[1] = values
[2 * chan
+ 1];
638 build_intrinsic(base
->gallivm
->builder
,
640 LLVMInt32TypeInContext(base
->gallivm
->context
),
642 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
643 args
[chan
+ 7] = args
[chan
+ 5] =
644 LLVMBuildBitCast(base
->gallivm
->builder
,
646 LLVMFloatTypeInContext(base
->gallivm
->context
),
653 for (chan
= 0; chan
< 4; chan
++ )
654 /* +5 because the first output value will be
655 * the 6th argument to the intrinsic. */
656 args
[chan
+ 5] = values
[chan
];
658 /* Clear COMPR flag */
659 args
[4] = uint
->zero
;
662 /* XXX: This controls which components of the output
663 * registers actually get exported. (e.g bit 0 means export
664 * X component, bit 1 means export Y component, etc.) I'm
665 * hard coding this to 0xf for now. In the future, we might
666 * want to do something else. */
667 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
669 /* Specify whether the EXEC mask represents the valid mask */
670 args
[1] = uint
->zero
;
672 /* Specify whether this is the last export */
673 args
[2] = uint
->zero
;
675 /* Specify the target we are exporting */
676 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
678 /* XXX: We probably need to keep track of the output
679 * values, so we know what we are passing to the next
683 /* Load from output pointers and initialize arguments for the shader export intrinsic */
684 static void si_llvm_init_export_args_load(struct lp_build_tgsi_context
*bld_base
,
685 LLVMValueRef
*out_ptr
,
689 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
690 LLVMValueRef values
[4];
693 for (i
= 0; i
< 4; i
++)
694 values
[i
] = LLVMBuildLoad(gallivm
->builder
, out_ptr
[i
], "");
696 si_llvm_init_export_args(bld_base
, values
, target
, args
);
699 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
700 LLVMValueRef
*out_ptr
)
702 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
703 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
705 if (si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_NEVER
) {
706 LLVMValueRef alpha_ref
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
709 LLVMValueRef alpha_pass
=
710 lp_build_cmp(&bld_base
->base
,
711 si_shader_ctx
->shader
->key
.ps
.alpha_func
,
712 LLVMBuildLoad(gallivm
->builder
, out_ptr
[3], ""),
715 lp_build_select(&bld_base
->base
,
717 lp_build_const_float(gallivm
, 1.0f
),
718 lp_build_const_float(gallivm
, -1.0f
));
720 build_intrinsic(gallivm
->builder
,
722 LLVMVoidTypeInContext(gallivm
->context
),
725 build_intrinsic(gallivm
->builder
,
727 LLVMVoidTypeInContext(gallivm
->context
),
732 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
* bld_base
,
733 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
735 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
736 struct si_pipe_shader
*shader
= si_shader_ctx
->shader
;
737 struct lp_build_context
*base
= &bld_base
->base
;
738 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
742 LLVMValueRef base_elt
;
743 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
744 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
, NUM_PIPE_CONST_BUFFERS
);
745 LLVMValueRef const_resource
= build_indexed_load(si_shader_ctx
, ptr
, constbuf_index
);
747 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
748 LLVMValueRef
*args
= pos
[2 + reg_index
];
750 if (!(shader
->key
.vs
.ucps_enabled
& (1 << reg_index
)))
753 shader
->shader
.clip_dist_write
|= 0xf << (4 * reg_index
);
758 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
760 /* Compute dot products of position and user clip plane vectors */
761 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
762 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
763 args
[0] = const_resource
;
764 args
[1] = lp_build_const_int32(base
->gallivm
,
765 ((reg_index
* 4 + chan
) * 4 +
767 base_elt
= build_intrinsic(base
->gallivm
->builder
,
768 "llvm.SI.load.const",
771 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
773 lp_build_add(base
, args
[5 + chan
],
774 lp_build_mul(base
, base_elt
,
775 out_elts
[const_chan
]));
779 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
780 args
[1] = uint
->zero
;
781 args
[2] = uint
->zero
;
782 args
[3] = lp_build_const_int32(base
->gallivm
,
783 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
784 args
[4] = uint
->zero
;
788 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
793 fprintf(stderr
, "STREAMOUT\n");
795 for (i
= 0; i
< so
->num_outputs
; i
++) {
796 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
797 so
->output
[i
].start_component
;
798 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
799 i
, so
->output
[i
].output_buffer
,
800 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
801 so
->output
[i
].register_index
,
805 mask
& 8 ? "w" : "");
809 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
810 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
811 * or v4i32 (num_channels=3,4). */
812 static void build_tbuffer_store(struct si_shader_context
*shader
,
815 unsigned num_channels
,
817 LLVMValueRef soffset
,
818 unsigned inst_offset
,
827 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
828 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
829 LLVMValueRef args
[] = {
832 LLVMConstInt(i32
, num_channels
, 0),
835 LLVMConstInt(i32
, inst_offset
, 0),
836 LLVMConstInt(i32
, dfmt
, 0),
837 LLVMConstInt(i32
, nfmt
, 0),
838 LLVMConstInt(i32
, offen
, 0),
839 LLVMConstInt(i32
, idxen
, 0),
840 LLVMConstInt(i32
, glc
, 0),
841 LLVMConstInt(i32
, slc
, 0),
842 LLVMConstInt(i32
, tfe
, 0)
845 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
846 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
847 const char *types
[] = {"i32", "v2i32", "v4i32"};
849 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
851 lp_build_intrinsic(gallivm
->builder
, name
,
852 LLVMVoidTypeInContext(gallivm
->context
),
853 args
, Elements(args
));
856 static void build_streamout_store(struct si_shader_context
*shader
,
859 unsigned num_channels
,
861 LLVMValueRef soffset
,
862 unsigned inst_offset
)
864 static unsigned dfmt
[] = {
865 V_008F0C_BUF_DATA_FORMAT_32
,
866 V_008F0C_BUF_DATA_FORMAT_32_32
,
867 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
868 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
870 assert(num_channels
>= 1 && num_channels
<= 4);
872 build_tbuffer_store(shader
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
873 inst_offset
, dfmt
[num_channels
-1],
874 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
877 /* On SI, the vertex shader is responsible for writing streamout data
879 static void si_llvm_emit_streamout(struct si_shader_context
*shader
,
880 struct si_shader_output_values
*outputs
,
883 struct pipe_stream_output_info
*so
= &shader
->shader
->selector
->so
;
884 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
885 LLVMBuilderRef builder
= gallivm
->builder
;
887 struct lp_build_if_state if_ctx
;
889 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
891 LLVMValueRef so_param
=
892 LLVMGetParam(shader
->radeon_bld
.main_fn
,
893 shader
->param_streamout_config
);
895 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
896 LLVMValueRef so_vtx_count
=
897 LLVMBuildAnd(builder
,
898 LLVMBuildLShr(builder
, so_param
,
899 LLVMConstInt(i32
, 16, 0), ""),
900 LLVMConstInt(i32
, 127, 0), "");
902 LLVMValueRef tid
= build_intrinsic(builder
, "llvm.SI.tid", i32
,
903 NULL
, 0, LLVMReadNoneAttribute
);
905 /* can_emit = tid < so_vtx_count; */
906 LLVMValueRef can_emit
=
907 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
909 /* Emit the streamout code conditionally. This actually avoids
910 * out-of-bounds buffer access. The hw tells us via the SGPR
911 * (so_vtx_count) which threads are allowed to emit streamout data. */
912 lp_build_if(&if_ctx
, gallivm
, can_emit
);
914 /* The buffer offset is computed as follows:
915 * ByteOffset = streamout_offset[buffer_id]*4 +
916 * (streamout_write_index + thread_id)*stride[buffer_id] +
920 LLVMValueRef so_write_index
=
921 LLVMGetParam(shader
->radeon_bld
.main_fn
,
922 shader
->param_streamout_write_index
);
924 /* Compute (streamout_write_index + thread_id). */
925 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
927 /* Compute the write offset for each enabled buffer. */
928 LLVMValueRef so_write_offset
[4] = {};
929 for (i
= 0; i
< 4; i
++) {
933 LLVMValueRef so_offset
= LLVMGetParam(shader
->radeon_bld
.main_fn
,
934 shader
->param_streamout_offset
[i
]);
935 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(i32
, 4, 0), "");
937 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
938 LLVMConstInt(i32
, so
->stride
[i
]*4, 0), "");
939 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
942 /* Write streamout data. */
943 for (i
= 0; i
< so
->num_outputs
; i
++) {
944 unsigned buf_idx
= so
->output
[i
].output_buffer
;
945 unsigned reg
= so
->output
[i
].register_index
;
946 unsigned start
= so
->output
[i
].start_component
;
947 unsigned num_comps
= so
->output
[i
].num_components
;
950 assert(num_comps
&& num_comps
<= 4);
951 if (!num_comps
|| num_comps
> 4)
954 /* Load the output as int. */
955 for (j
= 0; j
< num_comps
; j
++) {
958 while (outidx
< noutput
&& outputs
[outidx
].index
!= reg
)
961 if (outidx
< noutput
)
962 out
[j
] = LLVMBuildBitCast(builder
,
963 outputs
[outidx
].values
[start
+j
],
972 /* Pack the output. */
973 LLVMValueRef vdata
= NULL
;
979 case 2: /* as v2i32 */
980 case 3: /* as v4i32 (aligned to 4) */
981 case 4: /* as v4i32 */
982 vdata
= LLVMGetUndef(LLVMVectorType(i32
, util_next_power_of_two(num_comps
)));
983 for (j
= 0; j
< num_comps
; j
++) {
984 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
985 LLVMConstInt(i32
, j
, 0), "");
990 build_streamout_store(shader
, shader
->so_buffers
[buf_idx
],
992 so_write_offset
[buf_idx
],
993 LLVMConstInt(i32
, 0, 0),
994 so
->output
[i
].dst_offset
*4);
997 lp_build_endif(&if_ctx
);
1001 /* Generate export instructions for hardware VS shader stage */
1002 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
1003 struct si_shader_output_values
*outputs
,
1006 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1007 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
1008 struct lp_build_context
* base
= &bld_base
->base
;
1009 struct lp_build_context
* uint
=
1010 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1011 LLVMValueRef args
[9];
1012 LLVMValueRef pos_args
[4][9] = { { 0 } };
1013 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
;
1014 unsigned semantic_name
, semantic_index
, semantic_usage
;
1016 unsigned param_count
= 0;
1020 if (outputs
&& si_shader_ctx
->shader
->selector
->so
.num_outputs
) {
1021 si_llvm_emit_streamout(si_shader_ctx
, outputs
, noutput
);
1024 for (i
= 0; i
< noutput
; i
++) {
1025 semantic_name
= outputs
[i
].name
;
1026 semantic_index
= outputs
[i
].sid
;
1027 semantic_usage
= outputs
[i
].usage
;
1030 /* Select the correct target */
1031 switch(semantic_name
) {
1032 case TGSI_SEMANTIC_PSIZE
:
1033 shader
->vs_out_misc_write
= true;
1034 shader
->vs_out_point_size
= true;
1035 psize_value
= outputs
[i
].values
[0];
1037 case TGSI_SEMANTIC_EDGEFLAG
:
1038 shader
->vs_out_misc_write
= true;
1039 shader
->vs_out_edgeflag
= true;
1040 edgeflag_value
= outputs
[i
].values
[0];
1042 case TGSI_SEMANTIC_LAYER
:
1043 shader
->vs_out_misc_write
= true;
1044 shader
->vs_out_layer
= true;
1045 layer_value
= outputs
[i
].values
[0];
1047 case TGSI_SEMANTIC_POSITION
:
1048 target
= V_008DFC_SQ_EXP_POS
;
1050 case TGSI_SEMANTIC_COLOR
:
1051 case TGSI_SEMANTIC_BCOLOR
:
1052 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1053 shader
->output
[i
].param_offset
= param_count
;
1056 case TGSI_SEMANTIC_CLIPDIST
:
1057 if (!(si_shader_ctx
->shader
->key
.vs
.ucps_enabled
&
1058 (1 << semantic_index
)))
1060 shader
->clip_dist_write
|=
1061 semantic_usage
<< (semantic_index
<< 2);
1062 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
1064 case TGSI_SEMANTIC_CLIPVERTEX
:
1065 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
1067 case TGSI_SEMANTIC_PRIMID
:
1068 case TGSI_SEMANTIC_FOG
:
1069 case TGSI_SEMANTIC_GENERIC
:
1070 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1071 shader
->output
[i
].param_offset
= param_count
;
1077 "Warning: SI unhandled vs output type:%d\n",
1081 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
1083 if (target
>= V_008DFC_SQ_EXP_POS
&&
1084 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1085 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1086 args
, sizeof(args
));
1088 lp_build_intrinsic(base
->gallivm
->builder
,
1090 LLVMVoidTypeInContext(base
->gallivm
->context
),
1094 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1095 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1096 goto handle_semantic
;
1100 /* We need to add the position output manually if it's missing. */
1101 if (!pos_args
[0][0]) {
1102 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1103 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1104 pos_args
[0][2] = uint
->zero
; /* last export? */
1105 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1106 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1107 pos_args
[0][5] = base
->zero
; /* X */
1108 pos_args
[0][6] = base
->zero
; /* Y */
1109 pos_args
[0][7] = base
->zero
; /* Z */
1110 pos_args
[0][8] = base
->one
; /* W */
1113 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1114 if (shader
->vs_out_misc_write
) {
1115 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
1116 shader
->vs_out_point_size
|
1117 (shader
->vs_out_edgeflag
<< 1) |
1118 (shader
->vs_out_layer
<< 2));
1119 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
1120 pos_args
[1][2] = uint
->zero
; /* last export? */
1121 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
1122 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
1123 pos_args
[1][5] = base
->zero
; /* X */
1124 pos_args
[1][6] = base
->zero
; /* Y */
1125 pos_args
[1][7] = base
->zero
; /* Z */
1126 pos_args
[1][8] = base
->zero
; /* W */
1128 if (shader
->vs_out_point_size
)
1129 pos_args
[1][5] = psize_value
;
1131 if (shader
->vs_out_edgeflag
) {
1132 /* The output is a float, but the hw expects an integer
1133 * with the first bit containing the edge flag. */
1134 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
1136 bld_base
->uint_bld
.elem_type
, "");
1137 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
1139 bld_base
->int_bld
.one
);
1141 /* The LLVM intrinsic expects a float. */
1142 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
1144 base
->elem_type
, "");
1147 if (shader
->vs_out_layer
)
1148 pos_args
[1][7] = layer_value
;
1151 for (i
= 0; i
< 4; i
++)
1153 shader
->nr_pos_exports
++;
1156 for (i
= 0; i
< 4; i
++) {
1157 if (!pos_args
[i
][0])
1160 /* Specify the target we are exporting */
1161 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
1163 if (pos_idx
== shader
->nr_pos_exports
)
1164 /* Specify that this is the last export */
1165 pos_args
[i
][2] = uint
->one
;
1167 lp_build_intrinsic(base
->gallivm
->builder
,
1169 LLVMVoidTypeInContext(base
->gallivm
->context
),
1174 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
* bld_base
)
1176 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1177 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1178 struct si_shader
*es
= &si_shader_ctx
->shader
->shader
;
1179 struct si_shader
*gs
= si_shader_ctx
->gs_for_vs
;
1180 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1181 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1182 LLVMValueRef t_list_ptr
;
1183 LLVMValueRef t_list
;
1187 while (!tgsi_parse_end_of_tokens(parse
)) {
1188 struct tgsi_full_declaration
*d
=
1189 &parse
->FullToken
.FullDeclaration
;
1191 tgsi_parse_token(parse
);
1193 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
1196 si_store_shader_io_attribs(es
, d
);
1199 /* Load the ESGS ring resource descriptor */
1200 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1201 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
1202 lp_build_const_int32(gallivm
, SI_RING_ESGS
));
1204 for (i
= 0; i
< es
->noutput
; i
++) {
1205 LLVMValueRef
*out_ptr
=
1206 si_shader_ctx
->radeon_bld
.soa
.outputs
[es
->output
[i
].index
];
1209 for (j
= 0; j
< gs
->ninput
; j
++) {
1210 if (gs
->input
[j
].name
== es
->output
[i
].name
&&
1211 gs
->input
[j
].sid
== es
->output
[i
].sid
)
1214 if (j
== gs
->ninput
)
1217 for (chan
= 0; chan
< 4; chan
++) {
1218 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
1219 LLVMValueRef voffset
=
1220 lp_build_const_int32(gallivm
,
1221 (4 * gs
->input
[j
].param_offset
+ chan
) * 4);
1222 LLVMValueRef soffset
=
1223 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1224 SI_PARAM_ES2GS_OFFSET
);
1226 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
1228 build_tbuffer_store(si_shader_ctx
, t_list
, out_val
, 1,
1229 voffset
, soffset
, 0,
1230 V_008F0C_BUF_DATA_FORMAT_32
,
1231 V_008F0C_BUF_NUM_FORMAT_UINT
,
1237 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
1239 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1240 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1241 LLVMValueRef args
[2];
1243 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
1244 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
1245 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
1246 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
1247 LLVMNoUnwindAttribute
);
1250 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
* bld_base
)
1252 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1253 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1254 struct si_pipe_shader
*shader
= si_shader_ctx
->shader
;
1255 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1256 struct si_shader_output_values
*outputs
= NULL
;
1257 unsigned noutput
= 0;
1260 while (!tgsi_parse_end_of_tokens(parse
)) {
1261 struct tgsi_full_declaration
*d
=
1262 &parse
->FullToken
.FullDeclaration
;
1265 tgsi_parse_token(parse
);
1267 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
1270 i
= si_store_shader_io_attribs(&shader
->shader
, d
);
1274 outputs
= REALLOC(outputs
, noutput
* sizeof(outputs
[0]),
1275 (noutput
+ 1) * sizeof(outputs
[0]));
1276 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
1277 outputs
[noutput
].index
= index
;
1278 outputs
[noutput
].name
= d
->Semantic
.Name
;
1279 outputs
[noutput
].sid
= d
->Semantic
.Index
;
1280 outputs
[noutput
].usage
= d
->Declaration
.UsageMask
;
1282 for (i
= 0; i
< 4; i
++)
1283 outputs
[noutput
].values
[i
] =
1284 LLVMBuildLoad(gallivm
->builder
,
1285 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][i
],
1291 si_llvm_export_vs(bld_base
, outputs
, noutput
);
1295 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context
* bld_base
)
1297 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1298 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
1299 struct lp_build_context
* base
= &bld_base
->base
;
1300 struct lp_build_context
* uint
=
1301 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1302 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1303 LLVMValueRef args
[9];
1304 LLVMValueRef last_args
[9] = { 0 };
1305 unsigned semantic_name
;
1306 int depth_index
= -1, stencil_index
= -1;
1309 while (!tgsi_parse_end_of_tokens(parse
)) {
1310 struct tgsi_full_declaration
*d
=
1311 &parse
->FullToken
.FullDeclaration
;
1315 tgsi_parse_token(parse
);
1317 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_PROPERTY
&&
1318 parse
->FullToken
.FullProperty
.Property
.PropertyName
==
1319 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
)
1320 shader
->fs_write_all
= TRUE
;
1322 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
1325 i
= si_store_shader_io_attribs(shader
, d
);
1329 semantic_name
= d
->Semantic
.Name
;
1330 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
1331 /* Select the correct target */
1332 switch(semantic_name
) {
1333 case TGSI_SEMANTIC_POSITION
:
1334 depth_index
= index
;
1336 case TGSI_SEMANTIC_STENCIL
:
1337 stencil_index
= index
;
1339 case TGSI_SEMANTIC_COLOR
:
1340 target
= V_008DFC_SQ_EXP_MRT
+ d
->Semantic
.Index
;
1341 if (si_shader_ctx
->shader
->key
.ps
.alpha_to_one
)
1342 LLVMBuildStore(bld_base
->base
.gallivm
->builder
,
1344 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][3]);
1346 if (d
->Semantic
.Index
== 0 &&
1347 si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_ALWAYS
)
1348 si_alpha_test(bld_base
,
1349 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
]);
1354 "Warning: SI unhandled fs output type:%d\n",
1358 si_llvm_init_export_args_load(bld_base
,
1359 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
],
1362 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
1363 /* If there is an export instruction waiting to be emitted, do so now. */
1365 lp_build_intrinsic(base
->gallivm
->builder
,
1367 LLVMVoidTypeInContext(base
->gallivm
->context
),
1371 /* This instruction will be emitted at the end of the shader. */
1372 memcpy(last_args
, args
, sizeof(args
));
1374 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1375 if (shader
->fs_write_all
&& shader
->output
[i
].sid
== 0 &&
1376 si_shader_ctx
->shader
->key
.ps
.nr_cbufs
> 1) {
1377 for (int c
= 1; c
< si_shader_ctx
->shader
->key
.ps
.nr_cbufs
; c
++) {
1378 si_llvm_init_export_args_load(bld_base
,
1379 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
],
1380 V_008DFC_SQ_EXP_MRT
+ c
, args
);
1381 lp_build_intrinsic(base
->gallivm
->builder
,
1383 LLVMVoidTypeInContext(base
->gallivm
->context
),
1388 lp_build_intrinsic(base
->gallivm
->builder
,
1390 LLVMVoidTypeInContext(base
->gallivm
->context
),
1396 if (depth_index
>= 0 || stencil_index
>= 0) {
1397 LLVMValueRef out_ptr
;
1400 /* Specify the target we are exporting */
1401 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
1403 if (depth_index
>= 0) {
1404 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
1405 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1408 if (stencil_index
< 0) {
1415 if (stencil_index
>= 0) {
1416 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
1419 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1420 /* Only setting the stencil component bit (0x2) here
1421 * breaks some stencil piglit tests
1425 if (depth_index
< 0)
1429 /* Specify which components to enable */
1430 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
1434 args
[4] = uint
->zero
;
1437 lp_build_intrinsic(base
->gallivm
->builder
,
1439 LLVMVoidTypeInContext(base
->gallivm
->context
),
1442 memcpy(last_args
, args
, sizeof(args
));
1445 if (!last_args
[0]) {
1446 /* Specify which components to enable */
1447 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
1449 /* Specify the target we are exporting */
1450 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
1452 /* Set COMPR flag to zero to export data as 32-bit */
1453 last_args
[4] = uint
->zero
;
1456 last_args
[5]= uint
->zero
;
1457 last_args
[6]= uint
->zero
;
1458 last_args
[7]= uint
->zero
;
1459 last_args
[8]= uint
->zero
;
1461 si_shader_ctx
->shader
->spi_shader_col_format
|=
1462 V_028714_SPI_SHADER_32_ABGR
;
1463 si_shader_ctx
->shader
->cb_shader_mask
|= S_02823C_OUTPUT0_ENABLE(0xf);
1466 /* Specify whether the EXEC mask represents the valid mask */
1467 last_args
[1] = uint
->one
;
1469 /* Specify that this is the last export */
1470 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
1472 lp_build_intrinsic(base
->gallivm
->builder
,
1474 LLVMVoidTypeInContext(base
->gallivm
->context
),
1478 static const struct lp_build_tgsi_action txf_action
;
1480 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1481 struct lp_build_tgsi_context
* bld_base
,
1482 struct lp_build_emit_data
* emit_data
);
1484 static void tex_fetch_args(
1485 struct lp_build_tgsi_context
* bld_base
,
1486 struct lp_build_emit_data
* emit_data
)
1488 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1489 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1490 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
1491 unsigned opcode
= inst
->Instruction
.Opcode
;
1492 unsigned target
= inst
->Texture
.Texture
;
1493 LLVMValueRef coords
[4];
1494 LLVMValueRef address
[16];
1496 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
1499 unsigned sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1500 unsigned sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
1502 if (target
== TGSI_TEXTURE_BUFFER
) {
1503 LLVMTypeRef i128
= LLVMIntTypeInContext(gallivm
->context
, 128);
1504 LLVMTypeRef v2i128
= LLVMVectorType(i128
, 2);
1505 LLVMTypeRef i8
= LLVMInt8TypeInContext(gallivm
->context
);
1506 LLVMTypeRef v16i8
= LLVMVectorType(i8
, 16);
1508 /* Truncate v32i8 to v16i8. */
1509 LLVMValueRef res
= si_shader_ctx
->resources
[sampler_index
];
1510 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v2i128
, "");
1511 res
= LLVMBuildExtractElement(gallivm
->builder
, res
, bld_base
->uint_bld
.zero
, "");
1512 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v16i8
, "");
1514 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
1515 emit_data
->args
[0] = res
;
1516 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
1517 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
1518 emit_data
->arg_count
= 3;
1522 /* Fetch and project texture coordinates */
1523 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1524 for (chan
= 0; chan
< 3; chan
++ ) {
1525 coords
[chan
] = lp_build_emit_fetch(bld_base
,
1528 if (opcode
== TGSI_OPCODE_TXP
)
1529 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1535 if (opcode
== TGSI_OPCODE_TXP
)
1536 coords
[3] = bld_base
->base
.one
;
1538 /* Pack LOD bias value */
1539 if (opcode
== TGSI_OPCODE_TXB
)
1540 address
[count
++] = coords
[3];
1542 if (target
== TGSI_TEXTURE_CUBE
|| target
== TGSI_TEXTURE_SHADOWCUBE
)
1543 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
1545 /* Pack depth comparison value */
1547 case TGSI_TEXTURE_SHADOW1D
:
1548 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1549 case TGSI_TEXTURE_SHADOW2D
:
1550 case TGSI_TEXTURE_SHADOWRECT
:
1551 case TGSI_TEXTURE_SHADOWCUBE
:
1552 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1553 assert(ref_pos
>= 0);
1554 address
[count
++] = coords
[ref_pos
];
1556 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1557 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
1560 /* Pack user derivatives */
1561 if (opcode
== TGSI_OPCODE_TXD
) {
1562 for (chan
= 0; chan
< 2; chan
++) {
1563 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
1565 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 2, chan
);
1569 /* Pack texture coordinates */
1570 address
[count
++] = coords
[0];
1572 address
[count
++] = coords
[1];
1574 address
[count
++] = coords
[2];
1576 /* Pack LOD or sample index */
1577 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
1578 address
[count
++] = coords
[3];
1581 assert(!"Cannot handle more than 16 texture address parameters");
1585 for (chan
= 0; chan
< count
; chan
++ ) {
1586 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
1588 LLVMInt32TypeInContext(gallivm
->context
),
1592 /* Adjust the sample index according to FMASK.
1594 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1595 * which is the identity mapping. Each nibble says which physical sample
1596 * should be fetched to get that sample.
1598 * For example, 0x11111100 means there are only 2 samples stored and
1599 * the second sample covers 3/4 of the pixel. When reading samples 0
1600 * and 1, return physical sample 0 (determined by the first two 0s
1601 * in FMASK), otherwise return physical sample 1.
1603 * The sample index should be adjusted as follows:
1604 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1606 if (target
== TGSI_TEXTURE_2D_MSAA
||
1607 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1608 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1609 struct lp_build_emit_data txf_emit_data
= *emit_data
;
1610 LLVMValueRef txf_address
[4];
1611 unsigned txf_count
= count
;
1613 memcpy(txf_address
, address
, sizeof(txf_address
));
1615 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1616 txf_address
[2] = bld_base
->uint_bld
.zero
;
1618 txf_address
[3] = bld_base
->uint_bld
.zero
;
1620 /* Pad to a power-of-two size. */
1621 while (txf_count
< util_next_power_of_two(txf_count
))
1622 txf_address
[txf_count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1624 /* Read FMASK using TXF. */
1625 txf_emit_data
.chan
= 0;
1626 txf_emit_data
.dst_type
= LLVMVectorType(
1627 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
), 4);
1628 txf_emit_data
.args
[0] = lp_build_gather_values(gallivm
, txf_address
, txf_count
);
1629 txf_emit_data
.args
[1] = si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ sampler_index
];
1630 txf_emit_data
.args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
1631 target
== TGSI_TEXTURE_2D_MSAA
? TGSI_TEXTURE_2D
: TGSI_TEXTURE_2D_ARRAY
);
1632 txf_emit_data
.arg_count
= 3;
1634 build_tex_intrinsic(&txf_action
, bld_base
, &txf_emit_data
);
1636 /* Initialize some constants. */
1637 LLVMValueRef four
= LLVMConstInt(uint_bld
->elem_type
, 4, 0);
1638 LLVMValueRef F
= LLVMConstInt(uint_bld
->elem_type
, 0xF, 0);
1640 /* Apply the formula. */
1641 LLVMValueRef fmask
=
1642 LLVMBuildExtractElement(gallivm
->builder
,
1643 txf_emit_data
.output
[0],
1644 uint_bld
->zero
, "");
1646 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
1648 LLVMValueRef sample_index4
=
1649 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
1651 LLVMValueRef shifted_fmask
=
1652 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
1654 LLVMValueRef final_sample
=
1655 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
1657 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1658 * resource descriptor is 0 (invalid),
1660 LLVMValueRef fmask_desc
=
1661 LLVMBuildBitCast(gallivm
->builder
,
1662 si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ sampler_index
],
1663 LLVMVectorType(uint_bld
->elem_type
, 8), "");
1665 LLVMValueRef fmask_word1
=
1666 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
1669 LLVMValueRef word1_is_nonzero
=
1670 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1671 fmask_word1
, uint_bld
->zero
, "");
1673 /* Replace the MSAA sample index. */
1674 address
[sample_chan
] =
1675 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
1676 final_sample
, address
[sample_chan
], "");
1680 emit_data
->args
[1] = si_shader_ctx
->resources
[sampler_index
];
1682 if (opcode
== TGSI_OPCODE_TXF
) {
1683 /* add tex offsets */
1684 if (inst
->Texture
.NumOffsets
) {
1685 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1686 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1687 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
1689 assert(inst
->Texture
.NumOffsets
== 1);
1692 case TGSI_TEXTURE_3D
:
1693 address
[2] = lp_build_add(uint_bld
, address
[2],
1694 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
1696 case TGSI_TEXTURE_2D
:
1697 case TGSI_TEXTURE_SHADOW2D
:
1698 case TGSI_TEXTURE_RECT
:
1699 case TGSI_TEXTURE_SHADOWRECT
:
1700 case TGSI_TEXTURE_2D_ARRAY
:
1701 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1703 lp_build_add(uint_bld
, address
[1],
1704 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
1706 case TGSI_TEXTURE_1D
:
1707 case TGSI_TEXTURE_SHADOW1D
:
1708 case TGSI_TEXTURE_1D_ARRAY
:
1709 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1711 lp_build_add(uint_bld
, address
[0],
1712 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
1714 /* texture offsets do not apply to other texture targets */
1718 emit_data
->dst_type
= LLVMVectorType(
1719 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
1722 emit_data
->arg_count
= 3;
1725 emit_data
->args
[2] = si_shader_ctx
->samplers
[sampler_index
];
1727 emit_data
->dst_type
= LLVMVectorType(
1728 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
1731 emit_data
->arg_count
= 4;
1735 emit_data
->args
[emit_data
->arg_count
- 1] =
1736 lp_build_const_int32(bld_base
->base
.gallivm
, target
);
1738 /* Pad to power of two vector */
1739 while (count
< util_next_power_of_two(count
))
1740 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1742 emit_data
->args
[0] = lp_build_gather_values(gallivm
, address
, count
);
1745 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1746 struct lp_build_tgsi_context
* bld_base
,
1747 struct lp_build_emit_data
* emit_data
)
1749 struct lp_build_context
* base
= &bld_base
->base
;
1750 char intr_name
[127];
1752 if (emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
1753 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1754 base
->gallivm
->builder
,
1755 "llvm.SI.vs.load.input", emit_data
->dst_type
,
1756 emit_data
->args
, emit_data
->arg_count
,
1757 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1761 sprintf(intr_name
, "%sv%ui32", action
->intr_name
,
1762 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
1764 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1765 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
1766 emit_data
->args
, emit_data
->arg_count
,
1767 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1770 static void txq_fetch_args(
1771 struct lp_build_tgsi_context
* bld_base
,
1772 struct lp_build_emit_data
* emit_data
)
1774 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1775 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1776 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1778 if (inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
1779 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1780 LLVMTypeRef v8i32
= LLVMVectorType(i32
, 8);
1782 /* Read the size from the buffer descriptor directly. */
1783 LLVMValueRef size
= si_shader_ctx
->resources
[inst
->Src
[1].Register
.Index
];
1784 size
= LLVMBuildBitCast(gallivm
->builder
, size
, v8i32
, "");
1785 size
= LLVMBuildExtractElement(gallivm
->builder
, size
,
1786 lp_build_const_int32(gallivm
, 2), "");
1787 emit_data
->args
[0] = size
;
1792 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1795 emit_data
->args
[1] = si_shader_ctx
->resources
[inst
->Src
[1].Register
.Index
];
1798 emit_data
->args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
1799 inst
->Texture
.Texture
);
1801 emit_data
->arg_count
= 3;
1803 emit_data
->dst_type
= LLVMVectorType(
1804 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
1808 static void build_txq_intrinsic(const struct lp_build_tgsi_action
* action
,
1809 struct lp_build_tgsi_context
* bld_base
,
1810 struct lp_build_emit_data
* emit_data
)
1812 if (emit_data
->inst
->Texture
.Texture
== TGSI_TEXTURE_BUFFER
) {
1813 /* Just return the buffer size. */
1814 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
1818 build_tgsi_intrinsic_nomem(action
, bld_base
, emit_data
);
1821 #if HAVE_LLVM >= 0x0304
1823 static void si_llvm_emit_ddxy(
1824 const struct lp_build_tgsi_action
* action
,
1825 struct lp_build_tgsi_context
* bld_base
,
1826 struct lp_build_emit_data
* emit_data
)
1828 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1829 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1830 struct lp_build_context
* base
= &bld_base
->base
;
1831 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1832 unsigned opcode
= inst
->Instruction
.Opcode
;
1833 LLVMValueRef indices
[2];
1834 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1835 LLVMValueRef tl
, trbl
, result
[4];
1837 unsigned swizzle
[4];
1840 i32
= LLVMInt32TypeInContext(gallivm
->context
);
1842 indices
[0] = bld_base
->uint_bld
.zero
;
1843 indices
[1] = build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
1844 NULL
, 0, LLVMReadNoneAttribute
);
1845 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1848 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
1849 lp_build_const_int32(gallivm
, 0xfffffffc), "");
1850 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1853 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
1854 lp_build_const_int32(gallivm
,
1855 opcode
== TGSI_OPCODE_DDX
? 1 : 2),
1857 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1860 for (c
= 0; c
< 4; ++c
) {
1863 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
1864 for (i
= 0; i
< c
; ++i
) {
1865 if (swizzle
[i
] == swizzle
[c
]) {
1866 result
[c
] = result
[i
];
1873 LLVMBuildStore(gallivm
->builder
,
1874 LLVMBuildBitCast(gallivm
->builder
,
1875 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
1879 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
1880 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
1882 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
1883 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, base
->elem_type
, "");
1885 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
1888 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
1891 #endif /* HAVE_LLVM >= 0x0304 */
1893 /* Emit one vertex from the geometry shader */
1894 static void si_llvm_emit_vertex(
1895 const struct lp_build_tgsi_action
*action
,
1896 struct lp_build_tgsi_context
*bld_base
,
1897 struct lp_build_emit_data
*emit_data
)
1899 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1900 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
1901 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
1902 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1903 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1904 LLVMValueRef gs_next_vertex
;
1905 LLVMValueRef t_list_ptr
;
1906 LLVMValueRef t_list
;
1907 LLVMValueRef args
[2];
1911 /* Load the GSVS ring resource descriptor */
1912 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1913 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
1914 lp_build_const_int32(gallivm
, SI_RING_GSVS
));
1916 if (shader
->noutput
== 0) {
1917 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
1919 while (!tgsi_parse_end_of_tokens(parse
)) {
1920 tgsi_parse_token(parse
);
1922 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_DECLARATION
) {
1923 struct tgsi_full_declaration
*d
= &parse
->FullToken
.FullDeclaration
;
1925 if (d
->Declaration
.File
== TGSI_FILE_OUTPUT
)
1926 si_store_shader_io_attribs(shader
, d
);
1931 /* Write vertex attribute values to GSVS ring */
1932 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
, si_shader_ctx
->gs_next_vertex
, "");
1933 for (i
= 0; i
< shader
->noutput
; i
++) {
1934 LLVMValueRef
*out_ptr
=
1935 si_shader_ctx
->radeon_bld
.soa
.outputs
[shader
->output
[i
].index
];
1937 for (chan
= 0; chan
< 4; chan
++) {
1938 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
1939 LLVMValueRef soffset
=
1940 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1941 SI_PARAM_GS2VS_OFFSET
);
1942 LLVMValueRef voffset
=
1943 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
1944 shader
->gs_max_out_vertices
);
1946 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
1947 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
1949 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
1951 build_tbuffer_store(si_shader_ctx
, t_list
, out_val
, 1,
1952 voffset
, soffset
, 0,
1953 V_008F0C_BUF_DATA_FORMAT_32
,
1954 V_008F0C_BUF_NUM_FORMAT_UINT
,
1958 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
1959 lp_build_const_int32(gallivm
, 1));
1960 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, si_shader_ctx
->gs_next_vertex
);
1962 /* Signal vertex emission */
1963 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
);
1964 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
1965 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
1966 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
1967 LLVMNoUnwindAttribute
);
1970 /* Cut one primitive from the geometry shader */
1971 static void si_llvm_emit_primitive(
1972 const struct lp_build_tgsi_action
*action
,
1973 struct lp_build_tgsi_context
*bld_base
,
1974 struct lp_build_emit_data
*emit_data
)
1976 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1977 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1978 LLVMValueRef args
[2];
1980 /* Signal primitive cut */
1981 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
);
1982 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
1983 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
1984 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
1985 LLVMNoUnwindAttribute
);
1988 static const struct lp_build_tgsi_action tex_action
= {
1989 .fetch_args
= tex_fetch_args
,
1990 .emit
= build_tex_intrinsic
,
1991 .intr_name
= "llvm.SI.sample."
1994 static const struct lp_build_tgsi_action txb_action
= {
1995 .fetch_args
= tex_fetch_args
,
1996 .emit
= build_tex_intrinsic
,
1997 .intr_name
= "llvm.SI.sampleb."
2000 #if HAVE_LLVM >= 0x0304
2001 static const struct lp_build_tgsi_action txd_action
= {
2002 .fetch_args
= tex_fetch_args
,
2003 .emit
= build_tex_intrinsic
,
2004 .intr_name
= "llvm.SI.sampled."
2008 static const struct lp_build_tgsi_action txf_action
= {
2009 .fetch_args
= tex_fetch_args
,
2010 .emit
= build_tex_intrinsic
,
2011 .intr_name
= "llvm.SI.imageload."
2014 static const struct lp_build_tgsi_action txl_action
= {
2015 .fetch_args
= tex_fetch_args
,
2016 .emit
= build_tex_intrinsic
,
2017 .intr_name
= "llvm.SI.samplel."
2020 static const struct lp_build_tgsi_action txq_action
= {
2021 .fetch_args
= txq_fetch_args
,
2022 .emit
= build_txq_intrinsic
,
2023 .intr_name
= "llvm.SI.resinfo"
2026 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
2028 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
2029 LLVMValueRef args
[3];
2031 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
2033 args
[2] = lp_build_const_int32(gallivm
, 1);
2035 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
2038 static void create_function(struct si_shader_context
*si_shader_ctx
)
2040 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2041 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2042 struct si_pipe_shader
*shader
= si_shader_ctx
->shader
;
2043 LLVMTypeRef params
[21], f32
, i8
, i32
, v2i32
, v3i32
;
2044 unsigned i
, last_sgpr
, num_params
;
2046 i8
= LLVMInt8TypeInContext(gallivm
->context
);
2047 i32
= LLVMInt32TypeInContext(gallivm
->context
);
2048 f32
= LLVMFloatTypeInContext(gallivm
->context
);
2049 v2i32
= LLVMVectorType(i32
, 2);
2050 v3i32
= LLVMVectorType(i32
, 3);
2052 params
[SI_PARAM_CONST
] = LLVMPointerType(
2053 LLVMArrayType(LLVMVectorType(i8
, 16), NUM_CONST_BUFFERS
), CONST_ADDR_SPACE
);
2054 /* We assume at most 16 textures per program at the moment.
2055 * This need probably need to be changed to support bindless textures */
2056 params
[SI_PARAM_SAMPLER
] = LLVMPointerType(
2057 LLVMArrayType(LLVMVectorType(i8
, 16), NUM_SAMPLER_VIEWS
), CONST_ADDR_SPACE
);
2058 params
[SI_PARAM_RESOURCE
] = LLVMPointerType(
2059 LLVMArrayType(LLVMVectorType(i8
, 32), NUM_SAMPLER_STATES
), CONST_ADDR_SPACE
);
2061 switch (si_shader_ctx
->type
) {
2062 case TGSI_PROCESSOR_VERTEX
:
2063 params
[SI_PARAM_VERTEX_BUFFER
] = params
[SI_PARAM_CONST
];
2064 params
[SI_PARAM_SO_BUFFER
] = params
[SI_PARAM_CONST
];
2065 params
[SI_PARAM_START_INSTANCE
] = i32
;
2066 num_params
= SI_PARAM_START_INSTANCE
+1;
2067 if (shader
->key
.vs
.as_es
) {
2068 params
[SI_PARAM_ES2GS_OFFSET
] = i32
;
2071 /* The locations of the other parameters are assigned dynamically. */
2073 /* Streamout SGPRs. */
2074 if (shader
->selector
->so
.num_outputs
) {
2075 params
[si_shader_ctx
->param_streamout_config
= num_params
++] = i32
;
2076 params
[si_shader_ctx
->param_streamout_write_index
= num_params
++] = i32
;
2078 /* A streamout buffer offset is loaded if the stride is non-zero. */
2079 for (i
= 0; i
< 4; i
++) {
2080 if (!shader
->selector
->so
.stride
[i
])
2083 params
[si_shader_ctx
->param_streamout_offset
[i
] = num_params
++] = i32
;
2087 last_sgpr
= num_params
-1;
2090 params
[si_shader_ctx
->param_vertex_id
= num_params
++] = i32
;
2091 params
[num_params
++] = i32
; /* unused*/
2092 params
[num_params
++] = i32
; /* unused */
2093 params
[si_shader_ctx
->param_instance_id
= num_params
++] = i32
;
2096 case TGSI_PROCESSOR_GEOMETRY
:
2097 params
[SI_PARAM_GS2VS_OFFSET
] = i32
;
2098 params
[SI_PARAM_GS_WAVE_ID
] = i32
;
2099 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
2102 params
[SI_PARAM_VTX0_OFFSET
] = i32
;
2103 params
[SI_PARAM_VTX1_OFFSET
] = i32
;
2104 params
[SI_PARAM_PRIMITIVE_ID
] = i32
;
2105 params
[SI_PARAM_VTX2_OFFSET
] = i32
;
2106 params
[SI_PARAM_VTX3_OFFSET
] = i32
;
2107 params
[SI_PARAM_VTX4_OFFSET
] = i32
;
2108 params
[SI_PARAM_VTX5_OFFSET
] = i32
;
2109 params
[SI_PARAM_GS_INSTANCE_ID
] = i32
;
2110 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
2113 case TGSI_PROCESSOR_FRAGMENT
:
2114 params
[SI_PARAM_ALPHA_REF
] = f32
;
2115 params
[SI_PARAM_PRIM_MASK
] = i32
;
2116 last_sgpr
= SI_PARAM_PRIM_MASK
;
2117 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
2118 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
2119 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
2120 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
2121 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
2122 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
2123 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
2124 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
2125 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
2126 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
2127 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
2128 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
2129 params
[SI_PARAM_FRONT_FACE
] = f32
;
2130 params
[SI_PARAM_ANCILLARY
] = f32
;
2131 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
2132 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
2133 num_params
= SI_PARAM_POS_FIXED_PT
+1;
2137 assert(0 && "unimplemented shader");
2141 assert(num_params
<= Elements(params
));
2142 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, num_params
);
2143 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
2145 for (i
= 0; i
<= last_sgpr
; ++i
) {
2146 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
2149 LLVMAddAttribute(P
, LLVMInRegAttribute
);
2151 #if HAVE_LLVM >= 0x0304
2152 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2153 * to move load. Inputs are constant so this is fine. */
2154 case SI_PARAM_CONST
:
2155 case SI_PARAM_SAMPLER
:
2156 case SI_PARAM_RESOURCE
:
2157 LLVMAddAttribute(P
, LLVMByValAttribute
);
2163 #if HAVE_LLVM >= 0x0304
2164 if (bld_base
->info
&&
2165 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
2166 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0))
2167 si_shader_ctx
->ddxy_lds
=
2168 LLVMAddGlobalInAddressSpace(gallivm
->module
,
2169 LLVMArrayType(i32
, 64),
2175 static void preload_constants(struct si_shader_context
*si_shader_ctx
)
2177 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2178 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2179 const struct tgsi_shader_info
* info
= bld_base
->info
;
2181 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
2183 for (buf
= 0; buf
< NUM_CONST_BUFFERS
; buf
++) {
2184 unsigned i
, num_const
= info
->const_file_max
[buf
] + 1;
2189 /* Allocate space for the constant values */
2190 si_shader_ctx
->constants
[buf
] = CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
2192 /* Load the resource descriptor */
2193 si_shader_ctx
->const_resource
[buf
] =
2194 build_indexed_load(si_shader_ctx
, ptr
, lp_build_const_int32(gallivm
, buf
));
2196 /* Load the constants, we rely on the code sinking to do the rest */
2197 for (i
= 0; i
< num_const
* 4; ++i
) {
2198 LLVMValueRef args
[2] = {
2199 si_shader_ctx
->const_resource
[buf
],
2200 lp_build_const_int32(gallivm
, i
* 4)
2202 si_shader_ctx
->constants
[buf
][i
] =
2203 build_intrinsic(gallivm
->builder
, "llvm.SI.load.const",
2204 bld_base
->base
.elem_type
, args
, 2,
2205 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
2210 static void preload_samplers(struct si_shader_context
*si_shader_ctx
)
2212 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2213 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2214 const struct tgsi_shader_info
* info
= bld_base
->info
;
2216 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
2218 LLVMValueRef res_ptr
, samp_ptr
;
2219 LLVMValueRef offset
;
2221 if (num_samplers
== 0)
2224 /* Allocate space for the values */
2225 si_shader_ctx
->resources
= CALLOC(NUM_SAMPLER_VIEWS
, sizeof(LLVMValueRef
));
2226 si_shader_ctx
->samplers
= CALLOC(num_samplers
, sizeof(LLVMValueRef
));
2228 res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
2229 samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
2231 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2232 for (i
= 0; i
< num_samplers
; ++i
) {
2234 offset
= lp_build_const_int32(gallivm
, i
);
2235 si_shader_ctx
->resources
[i
] = build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
2238 offset
= lp_build_const_int32(gallivm
, i
);
2239 si_shader_ctx
->samplers
[i
] = build_indexed_load(si_shader_ctx
, samp_ptr
, offset
);
2241 /* FMASK resource */
2242 if (info
->is_msaa_sampler
[i
]) {
2243 offset
= lp_build_const_int32(gallivm
, FMASK_TEX_OFFSET
+ i
);
2244 si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ i
] =
2245 build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
2250 static void preload_streamout_buffers(struct si_shader_context
*si_shader_ctx
)
2252 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2253 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2256 if (si_shader_ctx
->type
!= TGSI_PROCESSOR_VERTEX
||
2257 si_shader_ctx
->shader
->key
.vs
.as_es
||
2258 !si_shader_ctx
->shader
->selector
->so
.num_outputs
)
2261 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2262 SI_PARAM_SO_BUFFER
);
2264 /* Load the resources, we rely on the code sinking to do the rest */
2265 for (i
= 0; i
< 4; ++i
) {
2266 if (si_shader_ctx
->shader
->selector
->so
.stride
[i
]) {
2267 LLVMValueRef offset
= lp_build_const_int32(gallivm
, i
);
2269 si_shader_ctx
->so_buffers
[i
] = build_indexed_load(si_shader_ctx
, buf_ptr
, offset
);
2274 int si_compile_llvm(struct si_context
*sctx
, struct si_pipe_shader
*shader
,
2279 struct radeon_llvm_binary binary
;
2280 bool dump
= r600_can_dump_shader(&sctx
->screen
->b
,
2281 shader
->selector
? shader
->selector
->tokens
: NULL
);
2282 memset(&binary
, 0, sizeof(binary
));
2283 radeon_llvm_compile(mod
, &binary
,
2284 r600_get_llvm_processor_name(sctx
->screen
->b
.family
), dump
);
2285 if (dump
&& ! binary
.disassembled
) {
2286 fprintf(stderr
, "SI CODE:\n");
2287 for (i
= 0; i
< binary
.code_size
; i
+=4 ) {
2288 fprintf(stderr
, "%02x%02x%02x%02x\n", binary
.code
[i
+ 3],
2289 binary
.code
[i
+ 2], binary
.code
[i
+ 1],
2294 /* XXX: We may be able to emit some of these values directly rather than
2295 * extracting fields to be emitted later.
2297 for (i
= 0; i
< binary
.config_size
; i
+= 8) {
2298 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
));
2299 unsigned value
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
+ 4));
2301 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
2302 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
2303 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
2304 case R_00B848_COMPUTE_PGM_RSRC1
:
2305 shader
->num_sgprs
= (G_00B028_SGPRS(value
) + 1) * 8;
2306 shader
->num_vgprs
= (G_00B028_VGPRS(value
) + 1) * 4;
2308 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
2309 shader
->lds_size
= G_00B02C_EXTRA_LDS_SIZE(value
);
2311 case R_00B84C_COMPUTE_PGM_RSRC2
:
2312 shader
->lds_size
= G_00B84C_LDS_SIZE(value
);
2314 case R_0286CC_SPI_PS_INPUT_ENA
:
2315 shader
->spi_ps_input_ena
= value
;
2318 fprintf(stderr
, "Warning: Compiler emitted unknown "
2319 "config register: 0x%x\n", reg
);
2324 /* copy new shader */
2325 r600_resource_reference(&shader
->bo
, NULL
);
2326 shader
->bo
= si_resource_create_custom(sctx
->b
.b
.screen
, PIPE_USAGE_IMMUTABLE
,
2328 if (shader
->bo
== NULL
) {
2332 ptr
= (uint32_t*)sctx
->b
.ws
->buffer_map(shader
->bo
->cs_buf
, sctx
->b
.rings
.gfx
.cs
, PIPE_TRANSFER_WRITE
);
2333 if (0 /*SI_BIG_ENDIAN*/) {
2334 for (i
= 0; i
< binary
.code_size
/ 4; ++i
) {
2335 ptr
[i
] = util_bswap32(*(uint32_t*)(binary
.code
+ i
*4));
2338 memcpy(ptr
, binary
.code
, binary
.code_size
);
2340 sctx
->b
.ws
->buffer_unmap(shader
->bo
->cs_buf
);
2343 free(binary
.config
);
2348 /* Generate code for the hardware VS shader stage to go with a geometry shader */
2349 static int si_generate_gs_copy_shader(struct si_context
*sctx
,
2350 struct si_shader_context
*si_shader_ctx
,
2353 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
2354 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2355 struct lp_build_context
*base
= &bld_base
->base
;
2356 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2357 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
2358 struct si_shader
*gs
= &si_shader_ctx
->shader
->selector
->current
->shader
;
2359 struct si_shader_output_values
*outputs
;
2360 LLVMValueRef t_list_ptr
, t_list
;
2361 LLVMValueRef args
[9];
2364 outputs
= MALLOC(gs
->noutput
* sizeof(outputs
[0]));
2366 si_shader_ctx
->type
= TGSI_PROCESSOR_VERTEX
;
2367 si_shader_ctx
->gs_for_vs
= gs
;
2369 radeon_llvm_context_init(&si_shader_ctx
->radeon_bld
);
2371 create_meta_data(si_shader_ctx
);
2372 create_function(si_shader_ctx
);
2373 preload_streamout_buffers(si_shader_ctx
);
2375 /* Load the GSVS ring resource descriptor */
2376 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
2377 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
2378 lp_build_const_int32(gallivm
, SI_RING_GSVS
));
2381 args
[1] = lp_build_mul_imm(uint
,
2382 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2383 si_shader_ctx
->param_vertex_id
),
2385 args
[3] = uint
->zero
;
2386 args
[4] = uint
->one
; /* OFFEN */
2387 args
[5] = uint
->zero
; /* IDXEN */
2388 args
[6] = uint
->one
; /* GLC */
2389 args
[7] = uint
->one
; /* SLC */
2390 args
[8] = uint
->zero
; /* TFE */
2392 /* Fetch vertex data from GSVS ring */
2393 for (i
= 0; i
< gs
->noutput
; ++i
) {
2394 struct si_shader_output
*out
= gs
->output
+ i
;
2397 shader
->output
[i
] = *out
;
2399 outputs
[i
].name
= out
->name
;
2400 outputs
[i
].index
= out
->index
;
2401 outputs
[i
].sid
= out
->sid
;
2402 outputs
[i
].usage
= out
->usage
;
2404 for (chan
= 0; chan
< 4; chan
++) {
2405 args
[2] = lp_build_const_int32(gallivm
,
2407 gs
->gs_max_out_vertices
* 16 * 4);
2409 outputs
[i
].values
[chan
] =
2410 LLVMBuildBitCast(gallivm
->builder
,
2411 build_intrinsic(gallivm
->builder
,
2412 "llvm.SI.buffer.load.dword.i32.i32",
2413 LLVMInt32TypeInContext(gallivm
->context
),
2415 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
2416 base
->elem_type
, "");
2419 shader
->noutput
= gs
->noutput
;
2421 si_llvm_export_vs(bld_base
, outputs
, gs
->noutput
);
2423 radeon_llvm_finalize_module(&si_shader_ctx
->radeon_bld
);
2426 fprintf(stderr
, "Copy Vertex Shader for Geometry Shader:\n\n");
2428 r
= si_compile_llvm(sctx
, si_shader_ctx
->shader
,
2429 bld_base
->base
.gallivm
->module
);
2431 radeon_llvm_dispose(&si_shader_ctx
->radeon_bld
);
2437 int si_pipe_shader_create(
2438 struct pipe_context
*ctx
,
2439 struct si_pipe_shader
*shader
)
2441 struct si_context
*sctx
= (struct si_context
*)ctx
;
2442 struct si_pipe_shader_selector
*sel
= shader
->selector
;
2443 struct si_shader_context si_shader_ctx
;
2444 struct tgsi_shader_info shader_info
;
2445 struct lp_build_tgsi_context
* bld_base
;
2448 bool dump
= r600_can_dump_shader(&sctx
->screen
->b
, sel
->tokens
);
2450 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2451 * conversion fails. */
2453 tgsi_dump(sel
->tokens
, 0);
2454 si_dump_streamout(&sel
->so
);
2457 assert(shader
->shader
.noutput
== 0);
2458 assert(shader
->shader
.nparam
== 0);
2459 assert(shader
->shader
.ninput
== 0);
2461 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
2462 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
2463 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
2465 tgsi_scan_shader(sel
->tokens
, &shader_info
);
2467 shader
->shader
.uses_kill
= shader_info
.uses_kill
;
2468 shader
->shader
.uses_instanceid
= shader_info
.uses_instanceid
;
2469 bld_base
->info
= &shader_info
;
2470 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
2472 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
2473 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = txb_action
;
2474 #if HAVE_LLVM >= 0x0304
2475 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = txd_action
;
2477 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = txf_action
;
2478 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = txl_action
;
2479 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
2480 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = txq_action
;
2482 #if HAVE_LLVM >= 0x0304
2483 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
2484 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
2487 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
2488 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
2490 si_shader_ctx
.radeon_bld
.load_system_value
= declare_system_value
;
2491 si_shader_ctx
.tokens
= sel
->tokens
;
2492 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
2493 si_shader_ctx
.shader
= shader
;
2494 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
2496 switch (si_shader_ctx
.type
) {
2497 case TGSI_PROCESSOR_VERTEX
:
2498 si_shader_ctx
.radeon_bld
.load_input
= declare_input_vs
;
2499 if (shader
->key
.vs
.as_es
) {
2500 si_shader_ctx
.gs_for_vs
= &sctx
->gs_shader
->current
->shader
;
2501 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
2503 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
2506 case TGSI_PROCESSOR_GEOMETRY
: {
2509 si_shader_ctx
.radeon_bld
.load_input
= declare_input_gs
;
2510 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
2511 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
2513 for (i
= 0; i
< shader_info
.num_properties
; i
++) {
2514 switch (shader_info
.properties
[i
].name
) {
2515 case TGSI_PROPERTY_GS_INPUT_PRIM
:
2516 shader
->shader
.gs_input_prim
= shader_info
.properties
[i
].data
[0];
2518 case TGSI_PROPERTY_GS_OUTPUT_PRIM
:
2519 shader
->shader
.gs_output_prim
= shader_info
.properties
[i
].data
[0];
2521 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
:
2522 shader
->shader
.gs_max_out_vertices
= shader_info
.properties
[i
].data
[0];
2528 case TGSI_PROCESSOR_FRAGMENT
:
2529 si_shader_ctx
.radeon_bld
.load_input
= declare_input_fs
;
2530 bld_base
->emit_epilogue
= si_llvm_emit_fs_epilogue
;
2533 assert(!"Unsupported shader type");
2537 create_meta_data(&si_shader_ctx
);
2538 create_function(&si_shader_ctx
);
2539 preload_constants(&si_shader_ctx
);
2540 preload_samplers(&si_shader_ctx
);
2541 preload_streamout_buffers(&si_shader_ctx
);
2543 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
2544 si_shader_ctx
.gs_next_vertex
=
2545 lp_build_alloca(bld_base
->base
.gallivm
,
2546 bld_base
->uint_bld
.elem_type
, "");
2549 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
2550 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
2554 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
2556 mod
= bld_base
->base
.gallivm
->module
;
2557 r
= si_compile_llvm(sctx
, shader
, mod
);
2559 fprintf(stderr
, "LLVM failed to compile shader\n");
2563 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
2565 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
2566 shader
->gs_copy_shader
= CALLOC_STRUCT(si_pipe_shader
);
2567 shader
->gs_copy_shader
->selector
= shader
->selector
;
2568 shader
->gs_copy_shader
->key
= shader
->key
;
2569 si_shader_ctx
.shader
= shader
->gs_copy_shader
;
2570 if ((r
= si_generate_gs_copy_shader(sctx
, &si_shader_ctx
, dump
))) {
2571 free(shader
->gs_copy_shader
);
2572 shader
->gs_copy_shader
= NULL
;
2577 tgsi_parse_free(&si_shader_ctx
.parse
);
2580 for (int i
= 0; i
< NUM_CONST_BUFFERS
; i
++)
2581 FREE(si_shader_ctx
.constants
[i
]);
2582 FREE(si_shader_ctx
.resources
);
2583 FREE(si_shader_ctx
.samplers
);
2588 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
2590 r600_resource_reference(&shader
->bo
, NULL
);