3 * Copyright 2012 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Tom Stellard <thomas.stellard@amd.com>
26 * Michel Dänzer <michel.daenzer@amd.com>
27 * Christian König <christian.koenig@amd.com>
30 #include "gallivm/lp_bld_tgsi_action.h"
31 #include "gallivm/lp_bld_const.h"
32 #include "gallivm/lp_bld_gather.h"
33 #include "gallivm/lp_bld_intr.h"
34 #include "gallivm/lp_bld_logic.h"
35 #include "gallivm/lp_bld_tgsi.h"
36 #include "gallivm/lp_bld_arit.h"
37 #include "radeon_llvm.h"
38 #include "radeon_llvm_emit.h"
39 #include "tgsi/tgsi_info.h"
40 #include "tgsi/tgsi_parse.h"
41 #include "tgsi/tgsi_scan.h"
42 #include "tgsi/tgsi_dump.h"
44 #include "radeonsi_pipe.h"
45 #include "radeonsi_shader.h"
53 struct si_shader_context
55 struct radeon_llvm_context radeon_bld
;
56 struct r600_context
*rctx
;
57 struct tgsi_parse_context parse
;
58 struct tgsi_token
* tokens
;
59 struct si_pipe_shader
*shader
;
60 struct si_shader_key key
;
61 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
62 LLVMValueRef const_md
;
63 /* struct list_head inputs; */
64 /* unsigned * input_mappings *//* From TGSI to SI hw */
65 /* struct tgsi_shader_info info;*/
68 static struct si_shader_context
* si_shader_context(
69 struct lp_build_tgsi_context
* bld_base
)
71 return (struct si_shader_context
*)bld_base
;
75 #define PERSPECTIVE_BASE 0
78 #define SAMPLE_OFFSET 0
79 #define CENTER_OFFSET 2
80 #define CENTROID_OFSET 4
82 #define USE_SGPR_MAX_SUFFIX_LEN 5
83 #define CONST_ADDR_SPACE 2
84 #define USER_SGPR_ADDR_SPACE 8
87 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
89 * @param offset The offset parameter specifies the number of
90 * elements to offset, not the number of bytes or dwords. An element is the
91 * the type pointed to by the base_ptr parameter (e.g. int is the element of
94 * When LLVM lowers the load instruction, it will convert the element offset
95 * into a dword offset automatically.
98 static LLVMValueRef
build_indexed_load(
99 struct si_shader_context
* si_shader_ctx
,
100 LLVMValueRef base_ptr
,
103 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
105 LLVMValueRef computed_ptr
= LLVMBuildGEP(
106 base
->gallivm
->builder
, base_ptr
, &offset
, 1, "");
108 LLVMValueRef result
= LLVMBuildLoad(base
->gallivm
->builder
, computed_ptr
, "");
109 LLVMSetMetadata(result
, 1, si_shader_ctx
->const_md
);
113 static void declare_input_vs(
114 struct si_shader_context
* si_shader_ctx
,
115 unsigned input_index
,
116 const struct tgsi_full_declaration
*decl
)
118 LLVMValueRef t_list_ptr
;
119 LLVMValueRef t_offset
;
121 LLVMValueRef attribute_offset
;
122 LLVMValueRef buffer_index_reg
;
123 LLVMValueRef args
[3];
124 LLVMTypeRef vec4_type
;
126 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
127 //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index];
130 /* Load the T list */
131 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFER
);
133 t_offset
= lp_build_const_int32(base
->gallivm
, input_index
);
135 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
, t_offset
);
137 /* Build the attribute offset */
138 attribute_offset
= lp_build_const_int32(base
->gallivm
, 0);
140 /* Load the buffer index, which is always stored in VGPR0
141 * for Vertex Shaders */
142 buffer_index_reg
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_INDEX
);
144 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
146 args
[1] = attribute_offset
;
147 args
[2] = buffer_index_reg
;
148 input
= lp_build_intrinsic(base
->gallivm
->builder
,
149 "llvm.SI.vs.load.input", vec4_type
, args
, 3);
151 /* Break up the vec4 into individual components */
152 for (chan
= 0; chan
< 4; chan
++) {
153 LLVMValueRef llvm_chan
= lp_build_const_int32(base
->gallivm
, chan
);
154 /* XXX: Use a helper function for this. There is one in
156 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
157 LLVMBuildExtractElement(base
->gallivm
->builder
,
158 input
, llvm_chan
, "");
162 static void declare_input_fs(
163 struct si_shader_context
* si_shader_ctx
,
164 unsigned input_index
,
165 const struct tgsi_full_declaration
*decl
)
167 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
168 struct lp_build_context
* base
=
169 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
170 struct gallivm_state
* gallivm
= base
->gallivm
;
171 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
172 LLVMValueRef main_fn
= si_shader_ctx
->radeon_bld
.main_fn
;
174 LLVMValueRef interp_param
;
175 const char * intr_name
;
178 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
179 * quad begins a new primitive. Bit 0 always needs
181 * [32:16] ParamOffset
184 LLVMValueRef params
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
185 LLVMValueRef attr_number
;
189 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
190 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
192 radeon_llvm_reg_index_soa(input_index
, chan
);
193 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
194 LLVMGetParam(main_fn
, SI_PARAM_POS_X_FLOAT
+ chan
);
197 /* RCP for fragcoord.w */
198 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
199 LLVMBuildFDiv(gallivm
->builder
,
200 lp_build_const_float(gallivm
, 1.0f
),
201 si_shader_ctx
->radeon_bld
.inputs
[soa_index
],
207 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
208 LLVMValueRef face
, is_face_positive
;
210 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
212 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
214 lp_build_const_float(gallivm
, 0.0f
),
217 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
218 LLVMBuildSelect(gallivm
->builder
,
220 lp_build_const_float(gallivm
, 1.0f
),
221 lp_build_const_float(gallivm
, 0.0f
),
223 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
224 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
225 lp_build_const_float(gallivm
, 0.0f
);
226 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
227 lp_build_const_float(gallivm
, 1.0f
);
232 shader
->input
[input_index
].param_offset
= shader
->ninterp
++;
233 attr_number
= lp_build_const_int32(gallivm
,
234 shader
->input
[input_index
].param_offset
);
236 /* XXX: Handle all possible interpolation modes */
237 switch (decl
->Interp
.Interpolate
) {
238 case TGSI_INTERPOLATE_COLOR
:
239 if (si_shader_ctx
->key
.flatshade
) {
242 if (decl
->Interp
.Centroid
)
243 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
245 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
248 case TGSI_INTERPOLATE_CONSTANT
:
251 case TGSI_INTERPOLATE_LINEAR
:
252 if (decl
->Interp
.Centroid
)
253 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTROID
);
255 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTER
);
257 case TGSI_INTERPOLATE_PERSPECTIVE
:
258 if (decl
->Interp
.Centroid
)
259 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
261 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
264 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
268 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
270 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
271 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
272 si_shader_ctx
->key
.color_two_side
) {
273 LLVMValueRef args
[4];
274 LLVMValueRef face
, is_face_positive
;
275 LLVMValueRef back_attr_number
=
276 lp_build_const_int32(gallivm
,
277 shader
->input
[input_index
].param_offset
+ 1);
279 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
281 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
283 lp_build_const_float(gallivm
, 0.0f
),
287 args
[3] = interp_param
;
288 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
289 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
290 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
291 LLVMValueRef front
, back
;
294 args
[1] = attr_number
;
295 front
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
296 input_type
, args
, args
[3] ? 4 : 3,
297 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
299 args
[1] = back_attr_number
;
300 back
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
301 input_type
, args
, args
[3] ? 4 : 3,
302 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
304 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
305 LLVMBuildSelect(gallivm
->builder
,
314 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
315 LLVMValueRef args
[4];
316 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
317 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
319 args
[1] = attr_number
;
321 args
[3] = interp_param
;
322 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
323 build_intrinsic(base
->gallivm
->builder
, intr_name
,
324 input_type
, args
, args
[3] ? 4 : 3,
325 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
330 static void declare_input(
331 struct radeon_llvm_context
* radeon_bld
,
332 unsigned input_index
,
333 const struct tgsi_full_declaration
*decl
)
335 struct si_shader_context
* si_shader_ctx
=
336 si_shader_context(&radeon_bld
->soa
.bld_base
);
337 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
338 declare_input_vs(si_shader_ctx
, input_index
, decl
);
339 } else if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
340 declare_input_fs(si_shader_ctx
, input_index
, decl
);
342 fprintf(stderr
, "Warning: Unsupported shader type,\n");
346 static LLVMValueRef
fetch_constant(
347 struct lp_build_tgsi_context
* bld_base
,
348 const struct tgsi_full_src_register
*reg
,
349 enum tgsi_opcode_type type
,
352 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
353 struct lp_build_context
* base
= &bld_base
->base
;
356 LLVMValueRef args
[2];
359 if (swizzle
== LP_CHAN_ALL
) {
361 LLVMValueRef values
[4];
362 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
363 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
365 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
368 /* Load the resource descriptor */
369 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
370 args
[0] = build_indexed_load(si_shader_ctx
, ptr
, bld_base
->uint_bld
.zero
);
372 args
[1] = lp_build_const_int32(base
->gallivm
, (reg
->Register
.Index
* 4 + swizzle
) * 4);
373 if (reg
->Register
.Indirect
) {
374 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
375 LLVMValueRef addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
376 LLVMValueRef idx
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
377 idx
= lp_build_mul_imm(&bld_base
->uint_bld
, idx
, 16);
378 args
[1] = lp_build_add(&bld_base
->uint_bld
, idx
, args
[1]);
381 result
= build_intrinsic(base
->gallivm
->builder
, "llvm.SI.load.const", base
->elem_type
,
382 args
, 2, LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
384 return bitcast(bld_base
, type
, result
);
387 /* Initialize arguments for the shader export intrinsic */
388 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
389 struct tgsi_full_declaration
*d
,
394 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
395 struct lp_build_context
*uint
=
396 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
397 struct lp_build_context
*base
= &bld_base
->base
;
398 unsigned compressed
= 0;
401 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
402 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
404 if (cbuf
>= 0 && cbuf
< 8) {
405 compressed
= (si_shader_ctx
->key
.export_16bpc
>> cbuf
) & 0x1;
408 si_shader_ctx
->shader
->spi_shader_col_format
|=
409 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
411 si_shader_ctx
->shader
->spi_shader_col_format
|=
412 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
417 /* Pixel shader needs to pack output values before export */
418 for (chan
= 0; chan
< 2; chan
++ ) {
419 LLVMValueRef
*out_ptr
=
420 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
];
421 args
[0] = LLVMBuildLoad(base
->gallivm
->builder
,
422 out_ptr
[2 * chan
], "");
423 args
[1] = LLVMBuildLoad(base
->gallivm
->builder
,
424 out_ptr
[2 * chan
+ 1], "");
426 build_intrinsic(base
->gallivm
->builder
,
428 LLVMInt32TypeInContext(base
->gallivm
->context
),
430 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
431 args
[chan
+ 7] = args
[chan
+ 5] =
432 LLVMBuildBitCast(base
->gallivm
->builder
,
434 LLVMFloatTypeInContext(base
->gallivm
->context
),
441 for (chan
= 0; chan
< 4; chan
++ ) {
442 LLVMValueRef out_ptr
=
443 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][chan
];
444 /* +5 because the first output value will be
445 * the 6th argument to the intrinsic. */
446 args
[chan
+ 5] = LLVMBuildLoad(base
->gallivm
->builder
,
450 /* Clear COMPR flag */
451 args
[4] = uint
->zero
;
454 /* XXX: This controls which components of the output
455 * registers actually get exported. (e.g bit 0 means export
456 * X component, bit 1 means export Y component, etc.) I'm
457 * hard coding this to 0xf for now. In the future, we might
458 * want to do something else. */
459 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
461 /* Specify whether the EXEC mask represents the valid mask */
462 args
[1] = uint
->zero
;
464 /* Specify whether this is the last export */
465 args
[2] = uint
->zero
;
467 /* Specify the target we are exporting */
468 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
470 /* XXX: We probably need to keep track of the output
471 * values, so we know what we are passing to the next
475 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
478 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
479 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
481 if (si_shader_ctx
->key
.alpha_func
!= PIPE_FUNC_NEVER
) {
482 LLVMValueRef out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][3];
483 LLVMValueRef alpha_pass
=
484 lp_build_cmp(&bld_base
->base
,
485 si_shader_ctx
->key
.alpha_func
,
486 LLVMBuildLoad(gallivm
->builder
, out_ptr
, ""),
487 lp_build_const_float(gallivm
, si_shader_ctx
->key
.alpha_ref
));
489 lp_build_select(&bld_base
->base
,
491 lp_build_const_float(gallivm
, 1.0f
),
492 lp_build_const_float(gallivm
, -1.0f
));
494 build_intrinsic(gallivm
->builder
,
496 LLVMVoidTypeInContext(gallivm
->context
),
499 build_intrinsic(gallivm
->builder
,
501 LLVMVoidTypeInContext(gallivm
->context
),
506 /* XXX: This is partially implemented for VS only at this point. It is not complete */
507 static void si_llvm_emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
509 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
510 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
511 struct lp_build_context
* base
= &bld_base
->base
;
512 struct lp_build_context
* uint
=
513 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
514 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
515 LLVMValueRef args
[9];
516 LLVMValueRef last_args
[9] = { 0 };
517 unsigned color_count
= 0;
518 unsigned param_count
= 0;
519 int depth_index
= -1, stencil_index
= -1;
521 while (!tgsi_parse_end_of_tokens(parse
)) {
522 struct tgsi_full_declaration
*d
=
523 &parse
->FullToken
.FullDeclaration
;
528 tgsi_parse_token(parse
);
530 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_PROPERTY
&&
531 parse
->FullToken
.FullProperty
.Property
.PropertyName
==
532 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
)
533 shader
->fs_write_all
= TRUE
;
535 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
538 switch (d
->Declaration
.File
) {
539 case TGSI_FILE_INPUT
:
540 i
= shader
->ninput
++;
541 shader
->input
[i
].name
= d
->Semantic
.Name
;
542 shader
->input
[i
].sid
= d
->Semantic
.Index
;
543 shader
->input
[i
].interpolate
= d
->Interp
.Interpolate
;
544 shader
->input
[i
].centroid
= d
->Interp
.Centroid
;
547 case TGSI_FILE_OUTPUT
:
548 i
= shader
->noutput
++;
549 shader
->output
[i
].name
= d
->Semantic
.Name
;
550 shader
->output
[i
].sid
= d
->Semantic
.Index
;
551 shader
->output
[i
].interpolate
= d
->Interp
.Interpolate
;
558 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
559 /* Select the correct target */
560 switch(d
->Semantic
.Name
) {
561 case TGSI_SEMANTIC_PSIZE
:
562 target
= V_008DFC_SQ_EXP_POS
;
564 case TGSI_SEMANTIC_POSITION
:
565 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
566 target
= V_008DFC_SQ_EXP_POS
;
572 case TGSI_SEMANTIC_STENCIL
:
573 stencil_index
= index
;
575 case TGSI_SEMANTIC_COLOR
:
576 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
577 case TGSI_SEMANTIC_BCOLOR
:
578 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
579 shader
->output
[i
].param_offset
= param_count
;
582 target
= V_008DFC_SQ_EXP_MRT
+ color_count
;
583 if (color_count
== 0 &&
584 si_shader_ctx
->key
.alpha_func
!= PIPE_FUNC_ALWAYS
)
585 si_alpha_test(bld_base
, index
);
590 case TGSI_SEMANTIC_FOG
:
591 case TGSI_SEMANTIC_GENERIC
:
592 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
593 shader
->output
[i
].param_offset
= param_count
;
599 "Warning: SI unhandled output type:%d\n",
603 si_llvm_init_export_args(bld_base
, d
, index
, target
, args
);
605 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
?
606 (d
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) :
607 (d
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
)) {
609 lp_build_intrinsic(base
->gallivm
->builder
,
611 LLVMVoidTypeInContext(base
->gallivm
->context
),
615 memcpy(last_args
, args
, sizeof(args
));
617 lp_build_intrinsic(base
->gallivm
->builder
,
619 LLVMVoidTypeInContext(base
->gallivm
->context
),
626 if (depth_index
>= 0 || stencil_index
>= 0) {
627 LLVMValueRef out_ptr
;
630 /* Specify the target we are exporting */
631 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
633 if (depth_index
>= 0) {
634 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
635 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
638 if (stencil_index
< 0) {
645 if (stencil_index
>= 0) {
646 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
649 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
656 /* Specify which components to enable */
657 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
661 args
[4] = uint
->zero
;
664 lp_build_intrinsic(base
->gallivm
->builder
,
666 LLVMVoidTypeInContext(base
->gallivm
->context
),
669 memcpy(last_args
, args
, sizeof(args
));
673 assert(si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
675 /* Specify which components to enable */
676 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
678 /* Specify the target we are exporting */
679 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
681 /* Set COMPR flag to zero to export data as 32-bit */
682 last_args
[4] = uint
->zero
;
685 last_args
[5]= uint
->zero
;
686 last_args
[6]= uint
->zero
;
687 last_args
[7]= uint
->zero
;
688 last_args
[8]= uint
->zero
;
690 si_shader_ctx
->shader
->spi_shader_col_format
|=
691 V_028714_SPI_SHADER_32_ABGR
;
694 /* Specify whether the EXEC mask represents the valid mask */
695 last_args
[1] = lp_build_const_int32(base
->gallivm
,
696 si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
698 if (shader
->fs_write_all
&& shader
->nr_cbufs
> 1) {
701 /* Specify that this is not yet the last export */
702 last_args
[2] = lp_build_const_int32(base
->gallivm
, 0);
704 for (i
= 1; i
< shader
->nr_cbufs
; i
++) {
705 /* Specify the target we are exporting */
706 last_args
[3] = lp_build_const_int32(base
->gallivm
,
707 V_008DFC_SQ_EXP_MRT
+ i
);
709 lp_build_intrinsic(base
->gallivm
->builder
,
711 LLVMVoidTypeInContext(base
->gallivm
->context
),
714 si_shader_ctx
->shader
->spi_shader_col_format
|=
715 si_shader_ctx
->shader
->spi_shader_col_format
<< 4;
718 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
721 /* Specify that this is the last export */
722 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
724 lp_build_intrinsic(base
->gallivm
->builder
,
726 LLVMVoidTypeInContext(base
->gallivm
->context
),
729 /* XXX: Look up what this function does */
730 /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
733 static void tex_fetch_args(
734 struct lp_build_tgsi_context
* bld_base
,
735 struct lp_build_emit_data
* emit_data
)
737 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
738 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
739 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
740 unsigned opcode
= inst
->Instruction
.Opcode
;
741 unsigned target
= inst
->Texture
.Texture
;
744 LLVMValueRef coords
[4];
745 LLVMValueRef address
[16];
750 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
751 emit_data
->args
[0] = lp_build_const_int32(bld_base
->base
.gallivm
, 0xf);
753 /* Fetch and project texture coordinates */
754 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
755 for (chan
= 0; chan
< 3; chan
++ ) {
756 coords
[chan
] = lp_build_emit_fetch(bld_base
,
759 if (opcode
== TGSI_OPCODE_TXP
)
760 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
766 if (opcode
== TGSI_OPCODE_TXP
)
767 coords
[3] = bld_base
->base
.one
;
769 /* Pack LOD bias value */
770 if (opcode
== TGSI_OPCODE_TXB
)
771 address
[count
++] = coords
[3];
773 if ((target
== TGSI_TEXTURE_CUBE
|| target
== TGSI_TEXTURE_SHADOWCUBE
) &&
774 opcode
!= TGSI_OPCODE_TXQ
)
775 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
777 /* Pack depth comparison value */
779 case TGSI_TEXTURE_SHADOW1D
:
780 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
781 case TGSI_TEXTURE_SHADOW2D
:
782 case TGSI_TEXTURE_SHADOWRECT
:
783 address
[count
++] = coords
[2];
785 case TGSI_TEXTURE_SHADOWCUBE
:
786 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
787 address
[count
++] = coords
[3];
789 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
790 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
793 /* Pack texture coordinates */
794 address
[count
++] = coords
[0];
796 case TGSI_TEXTURE_2D
:
797 case TGSI_TEXTURE_2D_ARRAY
:
798 case TGSI_TEXTURE_3D
:
799 case TGSI_TEXTURE_CUBE
:
800 case TGSI_TEXTURE_RECT
:
801 case TGSI_TEXTURE_SHADOW2D
:
802 case TGSI_TEXTURE_SHADOWRECT
:
803 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
804 case TGSI_TEXTURE_SHADOWCUBE
:
805 case TGSI_TEXTURE_2D_MSAA
:
806 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
807 case TGSI_TEXTURE_CUBE_ARRAY
:
808 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
809 address
[count
++] = coords
[1];
812 case TGSI_TEXTURE_3D
:
813 case TGSI_TEXTURE_CUBE
:
814 case TGSI_TEXTURE_SHADOWCUBE
:
815 case TGSI_TEXTURE_CUBE_ARRAY
:
816 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
817 address
[count
++] = coords
[2];
820 /* Pack array slice */
822 case TGSI_TEXTURE_1D_ARRAY
:
823 address
[count
++] = coords
[1];
826 case TGSI_TEXTURE_2D_ARRAY
:
827 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
828 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
829 address
[count
++] = coords
[2];
832 case TGSI_TEXTURE_CUBE_ARRAY
:
833 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
834 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
835 address
[count
++] = coords
[3];
839 if (opcode
== TGSI_OPCODE_TXL
)
840 address
[count
++] = coords
[3];
843 assert(!"Cannot handle more than 16 texture address parameters");
847 for (chan
= 0; chan
< count
; chan
++ ) {
848 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
850 LLVMInt32TypeInContext(gallivm
->context
),
854 /* Pad to power of two vector */
855 while (count
< util_next_power_of_two(count
))
856 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
858 emit_data
->args
[1] = lp_build_gather_values(gallivm
, address
, count
);
861 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
862 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
863 emit_data
->inst
->Src
[1].Register
.Index
);
864 emit_data
->args
[2] = build_indexed_load(si_shader_ctx
,
868 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
869 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
870 emit_data
->inst
->Src
[1].Register
.Index
);
871 emit_data
->args
[3] = build_indexed_load(si_shader_ctx
,
875 emit_data
->args
[4] = lp_build_const_int32(bld_base
->base
.gallivm
, target
);
877 emit_data
->arg_count
= 5;
878 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
879 * the writemask are clear */
880 emit_data
->dst_type
= LLVMVectorType(
881 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
885 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
886 struct lp_build_tgsi_context
* bld_base
,
887 struct lp_build_emit_data
* emit_data
)
889 struct lp_build_context
* base
= &bld_base
->base
;
892 sprintf(intr_name
, "%sv%ui32", action
->intr_name
,
893 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[1])));
895 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
896 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
897 emit_data
->args
, emit_data
->arg_count
);
900 static const struct lp_build_tgsi_action tex_action
= {
901 .fetch_args
= tex_fetch_args
,
902 .emit
= build_tex_intrinsic
,
903 .intr_name
= "llvm.SI.sample."
906 static const struct lp_build_tgsi_action txb_action
= {
907 .fetch_args
= tex_fetch_args
,
908 .emit
= build_tex_intrinsic
,
909 .intr_name
= "llvm.SI.sampleb."
912 static const struct lp_build_tgsi_action txl_action
= {
913 .fetch_args
= tex_fetch_args
,
914 .emit
= build_tex_intrinsic
,
915 .intr_name
= "llvm.SI.samplel."
918 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
920 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
921 LLVMValueRef args
[3];
923 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
925 args
[2] = lp_build_const_int32(gallivm
, 1);
927 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
930 static void create_function(struct si_shader_context
*si_shader_ctx
)
932 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
933 LLVMTypeRef params
[20], f32
, i8
, i32
, v2i32
, v3i32
;
936 i8
= LLVMInt8TypeInContext(gallivm
->context
);
937 i32
= LLVMInt32TypeInContext(gallivm
->context
);
938 f32
= LLVMFloatTypeInContext(gallivm
->context
);
939 v2i32
= LLVMVectorType(i32
, 2);
940 v3i32
= LLVMVectorType(i32
, 3);
942 params
[SI_PARAM_CONST
] = LLVMPointerType(LLVMVectorType(i8
, 16), CONST_ADDR_SPACE
);
943 params
[SI_PARAM_SAMPLER
] = params
[SI_PARAM_CONST
];
944 params
[SI_PARAM_RESOURCE
] = LLVMPointerType(LLVMVectorType(i8
, 32), CONST_ADDR_SPACE
);
946 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
947 params
[SI_PARAM_VERTEX_BUFFER
] = params
[SI_PARAM_SAMPLER
];
948 params
[SI_PARAM_VERTEX_INDEX
] = i32
;
949 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, 5);
952 params
[SI_PARAM_PRIM_MASK
] = i32
;
953 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
954 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
955 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
956 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
957 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
958 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
959 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
960 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
961 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
962 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
963 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
964 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
965 params
[SI_PARAM_FRONT_FACE
] = f32
;
966 params
[SI_PARAM_ANCILLARY
] = f32
;
967 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
968 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
969 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, 20);
972 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
973 for (i
= SI_PARAM_CONST
; i
<= SI_PARAM_VERTEX_BUFFER
; ++i
) {
974 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
975 LLVMAddAttribute(P
, LLVMInRegAttribute
);
979 int si_pipe_shader_create(
980 struct pipe_context
*ctx
,
981 struct si_pipe_shader
*shader
,
982 struct si_shader_key key
)
984 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
985 struct si_pipe_shader_selector
*sel
= shader
->selector
;
986 struct si_shader_context si_shader_ctx
;
987 struct tgsi_shader_info shader_info
;
988 struct lp_build_tgsi_context
* bld_base
;
990 unsigned char * inst_bytes
;
991 unsigned inst_byte_count
;
996 dump
= debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE
);
998 assert(shader
->shader
.noutput
== 0);
999 assert(shader
->shader
.ninterp
== 0);
1000 assert(shader
->shader
.ninput
== 0);
1002 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
1003 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
1004 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
1006 tgsi_scan_shader(sel
->tokens
, &shader_info
);
1007 shader
->shader
.uses_kill
= shader_info
.uses_kill
;
1008 bld_base
->info
= &shader_info
;
1009 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
1010 bld_base
->emit_epilogue
= si_llvm_emit_epilogue
;
1012 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
1013 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = txb_action
;
1014 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = txl_action
;
1015 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
1017 si_shader_ctx
.radeon_bld
.load_input
= declare_input
;
1018 si_shader_ctx
.tokens
= sel
->tokens
;
1019 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
1020 si_shader_ctx
.shader
= shader
;
1021 si_shader_ctx
.key
= key
;
1022 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
1023 si_shader_ctx
.rctx
= rctx
;
1025 create_meta_data(&si_shader_ctx
);
1026 create_function(&si_shader_ctx
);
1028 shader
->shader
.nr_cbufs
= rctx
->framebuffer
.nr_cbufs
;
1030 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1031 * conversion fails. */
1033 tgsi_dump(sel
->tokens
, 0);
1036 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
1037 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
1041 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
1043 mod
= bld_base
->base
.gallivm
->module
;
1045 LLVMDumpModule(mod
);
1047 radeon_llvm_compile(mod
, &inst_bytes
, &inst_byte_count
, "SI", dump
);
1049 fprintf(stderr
, "SI CODE:\n");
1050 for (i
= 0; i
< inst_byte_count
; i
+=4 ) {
1051 fprintf(stderr
, "%02x%02x%02x%02x\n", inst_bytes
[i
+ 3],
1052 inst_bytes
[i
+ 2], inst_bytes
[i
+ 1],
1057 shader
->num_sgprs
= util_le32_to_cpu(*(uint32_t*)inst_bytes
);
1058 shader
->num_vgprs
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 4));
1059 shader
->spi_ps_input_ena
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 8));
1061 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
1062 tgsi_parse_free(&si_shader_ctx
.parse
);
1064 /* copy new shader */
1065 si_resource_reference(&shader
->bo
, NULL
);
1066 shader
->bo
= si_resource_create_custom(ctx
->screen
, PIPE_USAGE_IMMUTABLE
,
1067 inst_byte_count
- 12);
1068 if (shader
->bo
== NULL
) {
1072 ptr
= (uint32_t*)rctx
->ws
->buffer_map(shader
->bo
->cs_buf
, rctx
->cs
, PIPE_TRANSFER_WRITE
);
1073 if (0 /*R600_BIG_ENDIAN*/) {
1074 for (i
= 0; i
< (inst_byte_count
-12)/4; ++i
) {
1075 ptr
[i
] = util_bswap32(*(uint32_t*)(inst_bytes
+12 + i
*4));
1078 memcpy(ptr
, inst_bytes
+ 12, inst_byte_count
- 12);
1080 rctx
->ws
->buffer_unmap(shader
->bo
->cs_buf
);
1087 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
1089 si_resource_reference(&shader
->bo
, NULL
);