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 /* struct list_head inputs; */
63 /* unsigned * input_mappings *//* From TGSI to SI hw */
64 /* struct tgsi_shader_info info;*/
67 static struct si_shader_context
* si_shader_context(
68 struct lp_build_tgsi_context
* bld_base
)
70 return (struct si_shader_context
*)bld_base
;
74 #define PERSPECTIVE_BASE 0
77 #define SAMPLE_OFFSET 0
78 #define CENTER_OFFSET 2
79 #define CENTROID_OFSET 4
81 #define USE_SGPR_MAX_SUFFIX_LEN 5
82 #define CONST_ADDR_SPACE 2
83 #define USER_SGPR_ADDR_SPACE 8
86 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
88 * @param offset The offset parameter specifies the number of
89 * elements to offset, not the number of bytes or dwords. An element is the
90 * the type pointed to by the base_ptr parameter (e.g. int is the element of
93 * When LLVM lowers the load instruction, it will convert the element offset
94 * into a dword offset automatically.
97 static LLVMValueRef
build_indexed_load(
98 struct gallivm_state
* gallivm
,
99 LLVMValueRef base_ptr
,
102 LLVMValueRef computed_ptr
= LLVMBuildGEP(
103 gallivm
->builder
, base_ptr
, &offset
, 1, "");
105 return LLVMBuildLoad(gallivm
->builder
, computed_ptr
, "");
108 static void declare_input_vs(
109 struct si_shader_context
* si_shader_ctx
,
110 unsigned input_index
,
111 const struct tgsi_full_declaration
*decl
)
113 LLVMValueRef t_list_ptr
;
114 LLVMValueRef t_offset
;
116 LLVMValueRef attribute_offset
;
117 LLVMValueRef buffer_index_reg
;
118 LLVMValueRef args
[3];
119 LLVMTypeRef vec4_type
;
121 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
122 //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index];
125 /* Load the T list */
126 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFER
);
128 t_offset
= lp_build_const_int32(base
->gallivm
, input_index
);
130 t_list
= build_indexed_load(base
->gallivm
, t_list_ptr
, t_offset
);
132 /* Build the attribute offset */
133 attribute_offset
= lp_build_const_int32(base
->gallivm
, 0);
135 /* Load the buffer index, which is always stored in VGPR0
136 * for Vertex Shaders */
137 buffer_index_reg
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_INDEX
);
139 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
141 args
[1] = attribute_offset
;
142 args
[2] = buffer_index_reg
;
143 input
= lp_build_intrinsic(base
->gallivm
->builder
,
144 "llvm.SI.vs.load.input", vec4_type
, args
, 3);
146 /* Break up the vec4 into individual components */
147 for (chan
= 0; chan
< 4; chan
++) {
148 LLVMValueRef llvm_chan
= lp_build_const_int32(base
->gallivm
, chan
);
149 /* XXX: Use a helper function for this. There is one in
151 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
152 LLVMBuildExtractElement(base
->gallivm
->builder
,
153 input
, llvm_chan
, "");
157 static void declare_input_fs(
158 struct si_shader_context
* si_shader_ctx
,
159 unsigned input_index
,
160 const struct tgsi_full_declaration
*decl
)
162 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
163 struct lp_build_context
* base
=
164 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
165 struct gallivm_state
* gallivm
= base
->gallivm
;
166 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
167 LLVMValueRef main_fn
= si_shader_ctx
->radeon_bld
.main_fn
;
169 LLVMValueRef interp_param
;
170 const char * intr_name
;
173 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
174 * quad begins a new primitive. Bit 0 always needs
176 * [32:16] ParamOffset
179 LLVMValueRef params
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
180 LLVMValueRef attr_number
;
184 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
185 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
187 radeon_llvm_reg_index_soa(input_index
, chan
);
188 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
189 LLVMGetParam(main_fn
, SI_PARAM_POS_X_FLOAT
+ chan
);
192 /* RCP for fragcoord.w */
193 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
194 LLVMBuildFDiv(gallivm
->builder
,
195 lp_build_const_float(gallivm
, 1.0f
),
196 si_shader_ctx
->radeon_bld
.inputs
[soa_index
],
202 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
203 LLVMValueRef face
, is_face_positive
;
205 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
207 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
209 lp_build_const_float(gallivm
, 0.0f
),
212 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
213 LLVMBuildSelect(gallivm
->builder
,
215 lp_build_const_float(gallivm
, 1.0f
),
216 lp_build_const_float(gallivm
, 0.0f
),
218 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
219 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
220 lp_build_const_float(gallivm
, 0.0f
);
221 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
222 lp_build_const_float(gallivm
, 1.0f
);
227 shader
->input
[input_index
].param_offset
= shader
->ninterp
++;
228 attr_number
= lp_build_const_int32(gallivm
,
229 shader
->input
[input_index
].param_offset
);
231 /* XXX: Handle all possible interpolation modes */
232 switch (decl
->Interp
.Interpolate
) {
233 case TGSI_INTERPOLATE_COLOR
:
234 if (si_shader_ctx
->key
.flatshade
) {
237 if (decl
->Interp
.Centroid
)
238 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
240 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
243 case TGSI_INTERPOLATE_CONSTANT
:
246 case TGSI_INTERPOLATE_LINEAR
:
247 if (decl
->Interp
.Centroid
)
248 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTROID
);
250 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTER
);
252 case TGSI_INTERPOLATE_PERSPECTIVE
:
253 if (decl
->Interp
.Centroid
)
254 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
256 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
259 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
263 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
265 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
266 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
267 si_shader_ctx
->key
.color_two_side
) {
268 LLVMValueRef args
[4];
269 LLVMValueRef face
, is_face_positive
;
270 LLVMValueRef back_attr_number
=
271 lp_build_const_int32(gallivm
,
272 shader
->input
[input_index
].param_offset
+ 1);
274 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
276 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
278 lp_build_const_float(gallivm
, 0.0f
),
282 args
[3] = interp_param
;
283 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
284 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
285 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
286 LLVMValueRef front
, back
;
289 args
[1] = attr_number
;
290 front
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
291 input_type
, args
, args
[3] ? 4 : 3,
292 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
294 args
[1] = back_attr_number
;
295 back
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
296 input_type
, args
, args
[3] ? 4 : 3,
297 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
299 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
300 LLVMBuildSelect(gallivm
->builder
,
309 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
310 LLVMValueRef args
[4];
311 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
312 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
314 args
[1] = attr_number
;
316 args
[3] = interp_param
;
317 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
318 build_intrinsic(base
->gallivm
->builder
, intr_name
,
319 input_type
, args
, args
[3] ? 4 : 3,
320 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
325 static void declare_input(
326 struct radeon_llvm_context
* radeon_bld
,
327 unsigned input_index
,
328 const struct tgsi_full_declaration
*decl
)
330 struct si_shader_context
* si_shader_ctx
=
331 si_shader_context(&radeon_bld
->soa
.bld_base
);
332 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
333 declare_input_vs(si_shader_ctx
, input_index
, decl
);
334 } else if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
335 declare_input_fs(si_shader_ctx
, input_index
, decl
);
337 fprintf(stderr
, "Warning: Unsupported shader type,\n");
341 static LLVMValueRef
fetch_constant(
342 struct lp_build_tgsi_context
* bld_base
,
343 const struct tgsi_full_src_register
*reg
,
344 enum tgsi_opcode_type type
,
347 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
348 struct lp_build_context
* base
= &bld_base
->base
;
351 LLVMValueRef args
[2];
354 if (swizzle
== LP_CHAN_ALL
) {
356 LLVMValueRef values
[4];
357 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
358 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
360 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
363 /* Load the resource descriptor */
364 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
365 args
[0] = build_indexed_load(base
->gallivm
, ptr
, bld_base
->uint_bld
.zero
);
367 args
[1] = lp_build_const_int32(base
->gallivm
, (reg
->Register
.Index
* 4 + swizzle
) * 4);
368 if (reg
->Register
.Indirect
) {
369 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
370 LLVMValueRef addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
371 LLVMValueRef idx
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
372 idx
= lp_build_mul_imm(&bld_base
->uint_bld
, idx
, 16);
373 args
[1] = lp_build_add(&bld_base
->uint_bld
, idx
, args
[1]);
376 result
= build_intrinsic(base
->gallivm
->builder
, "llvm.SI.load.const", base
->elem_type
,
377 args
, 2, LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
379 return bitcast(bld_base
, type
, result
);
382 /* Initialize arguments for the shader export intrinsic */
383 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
384 struct tgsi_full_declaration
*d
,
389 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
390 struct lp_build_context
*uint
=
391 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
392 struct lp_build_context
*base
= &bld_base
->base
;
393 unsigned compressed
= 0;
396 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
397 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
399 if (cbuf
>= 0 && cbuf
< 8) {
400 compressed
= (si_shader_ctx
->key
.export_16bpc
>> cbuf
) & 0x1;
403 si_shader_ctx
->shader
->spi_shader_col_format
|=
404 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
406 si_shader_ctx
->shader
->spi_shader_col_format
|=
407 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
412 /* Pixel shader needs to pack output values before export */
413 for (chan
= 0; chan
< 2; chan
++ ) {
414 LLVMValueRef
*out_ptr
=
415 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
];
416 args
[0] = LLVMBuildLoad(base
->gallivm
->builder
,
417 out_ptr
[2 * chan
], "");
418 args
[1] = LLVMBuildLoad(base
->gallivm
->builder
,
419 out_ptr
[2 * chan
+ 1], "");
421 build_intrinsic(base
->gallivm
->builder
,
423 LLVMInt32TypeInContext(base
->gallivm
->context
),
425 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
426 args
[chan
+ 7] = args
[chan
+ 5] =
427 LLVMBuildBitCast(base
->gallivm
->builder
,
429 LLVMFloatTypeInContext(base
->gallivm
->context
),
436 for (chan
= 0; chan
< 4; chan
++ ) {
437 LLVMValueRef out_ptr
=
438 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][chan
];
439 /* +5 because the first output value will be
440 * the 6th argument to the intrinsic. */
441 args
[chan
+ 5] = LLVMBuildLoad(base
->gallivm
->builder
,
445 /* Clear COMPR flag */
446 args
[4] = uint
->zero
;
449 /* XXX: This controls which components of the output
450 * registers actually get exported. (e.g bit 0 means export
451 * X component, bit 1 means export Y component, etc.) I'm
452 * hard coding this to 0xf for now. In the future, we might
453 * want to do something else. */
454 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
456 /* Specify whether the EXEC mask represents the valid mask */
457 args
[1] = uint
->zero
;
459 /* Specify whether this is the last export */
460 args
[2] = uint
->zero
;
462 /* Specify the target we are exporting */
463 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
465 /* XXX: We probably need to keep track of the output
466 * values, so we know what we are passing to the next
470 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
473 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
474 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
476 if (si_shader_ctx
->key
.alpha_func
!= PIPE_FUNC_NEVER
) {
477 LLVMValueRef out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][3];
478 LLVMValueRef alpha_pass
=
479 lp_build_cmp(&bld_base
->base
,
480 si_shader_ctx
->key
.alpha_func
,
481 LLVMBuildLoad(gallivm
->builder
, out_ptr
, ""),
482 lp_build_const_float(gallivm
, si_shader_ctx
->key
.alpha_ref
));
484 lp_build_select(&bld_base
->base
,
486 lp_build_const_float(gallivm
, 1.0f
),
487 lp_build_const_float(gallivm
, -1.0f
));
489 build_intrinsic(gallivm
->builder
,
491 LLVMVoidTypeInContext(gallivm
->context
),
494 build_intrinsic(gallivm
->builder
,
496 LLVMVoidTypeInContext(gallivm
->context
),
501 /* XXX: This is partially implemented for VS only at this point. It is not complete */
502 static void si_llvm_emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
504 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
505 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
506 struct lp_build_context
* base
= &bld_base
->base
;
507 struct lp_build_context
* uint
=
508 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
509 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
510 LLVMValueRef args
[9];
511 LLVMValueRef last_args
[9] = { 0 };
512 unsigned color_count
= 0;
513 unsigned param_count
= 0;
514 int depth_index
= -1, stencil_index
= -1;
516 while (!tgsi_parse_end_of_tokens(parse
)) {
517 struct tgsi_full_declaration
*d
=
518 &parse
->FullToken
.FullDeclaration
;
523 tgsi_parse_token(parse
);
525 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_PROPERTY
&&
526 parse
->FullToken
.FullProperty
.Property
.PropertyName
==
527 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
)
528 shader
->fs_write_all
= TRUE
;
530 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
533 switch (d
->Declaration
.File
) {
534 case TGSI_FILE_INPUT
:
535 i
= shader
->ninput
++;
536 shader
->input
[i
].name
= d
->Semantic
.Name
;
537 shader
->input
[i
].sid
= d
->Semantic
.Index
;
538 shader
->input
[i
].interpolate
= d
->Interp
.Interpolate
;
539 shader
->input
[i
].centroid
= d
->Interp
.Centroid
;
542 case TGSI_FILE_OUTPUT
:
543 i
= shader
->noutput
++;
544 shader
->output
[i
].name
= d
->Semantic
.Name
;
545 shader
->output
[i
].sid
= d
->Semantic
.Index
;
546 shader
->output
[i
].interpolate
= d
->Interp
.Interpolate
;
553 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
554 /* Select the correct target */
555 switch(d
->Semantic
.Name
) {
556 case TGSI_SEMANTIC_PSIZE
:
557 target
= V_008DFC_SQ_EXP_POS
;
559 case TGSI_SEMANTIC_POSITION
:
560 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
561 target
= V_008DFC_SQ_EXP_POS
;
567 case TGSI_SEMANTIC_STENCIL
:
568 stencil_index
= index
;
570 case TGSI_SEMANTIC_COLOR
:
571 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
572 case TGSI_SEMANTIC_BCOLOR
:
573 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
574 shader
->output
[i
].param_offset
= param_count
;
577 target
= V_008DFC_SQ_EXP_MRT
+ color_count
;
578 if (color_count
== 0 &&
579 si_shader_ctx
->key
.alpha_func
!= PIPE_FUNC_ALWAYS
)
580 si_alpha_test(bld_base
, index
);
585 case TGSI_SEMANTIC_FOG
:
586 case TGSI_SEMANTIC_GENERIC
:
587 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
588 shader
->output
[i
].param_offset
= param_count
;
594 "Warning: SI unhandled output type:%d\n",
598 si_llvm_init_export_args(bld_base
, d
, index
, target
, args
);
600 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
?
601 (d
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) :
602 (d
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
)) {
604 lp_build_intrinsic(base
->gallivm
->builder
,
606 LLVMVoidTypeInContext(base
->gallivm
->context
),
610 memcpy(last_args
, args
, sizeof(args
));
612 lp_build_intrinsic(base
->gallivm
->builder
,
614 LLVMVoidTypeInContext(base
->gallivm
->context
),
621 if (depth_index
>= 0 || stencil_index
>= 0) {
622 LLVMValueRef out_ptr
;
625 /* Specify the target we are exporting */
626 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
628 if (depth_index
>= 0) {
629 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
630 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
633 if (stencil_index
< 0) {
640 if (stencil_index
>= 0) {
641 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
644 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
651 /* Specify which components to enable */
652 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
656 args
[4] = uint
->zero
;
659 lp_build_intrinsic(base
->gallivm
->builder
,
661 LLVMVoidTypeInContext(base
->gallivm
->context
),
664 memcpy(last_args
, args
, sizeof(args
));
668 assert(si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
670 /* Specify which components to enable */
671 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
673 /* Specify the target we are exporting */
674 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
676 /* Set COMPR flag to zero to export data as 32-bit */
677 last_args
[4] = uint
->zero
;
680 last_args
[5]= uint
->zero
;
681 last_args
[6]= uint
->zero
;
682 last_args
[7]= uint
->zero
;
683 last_args
[8]= uint
->zero
;
685 si_shader_ctx
->shader
->spi_shader_col_format
|=
686 V_028714_SPI_SHADER_32_ABGR
;
689 /* Specify whether the EXEC mask represents the valid mask */
690 last_args
[1] = lp_build_const_int32(base
->gallivm
,
691 si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
693 if (shader
->fs_write_all
&& shader
->nr_cbufs
> 1) {
696 /* Specify that this is not yet the last export */
697 last_args
[2] = lp_build_const_int32(base
->gallivm
, 0);
699 for (i
= 1; i
< shader
->nr_cbufs
; i
++) {
700 /* Specify the target we are exporting */
701 last_args
[3] = lp_build_const_int32(base
->gallivm
,
702 V_008DFC_SQ_EXP_MRT
+ i
);
704 lp_build_intrinsic(base
->gallivm
->builder
,
706 LLVMVoidTypeInContext(base
->gallivm
->context
),
709 si_shader_ctx
->shader
->spi_shader_col_format
|=
710 si_shader_ctx
->shader
->spi_shader_col_format
<< 4;
713 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
716 /* Specify that this is the last export */
717 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
719 lp_build_intrinsic(base
->gallivm
->builder
,
721 LLVMVoidTypeInContext(base
->gallivm
->context
),
724 /* XXX: Look up what this function does */
725 /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
728 static void tex_fetch_args(
729 struct lp_build_tgsi_context
* bld_base
,
730 struct lp_build_emit_data
* emit_data
)
732 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
733 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
734 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
735 unsigned opcode
= inst
->Instruction
.Opcode
;
736 unsigned target
= inst
->Texture
.Texture
;
739 LLVMValueRef coords
[4];
740 LLVMValueRef address
[16];
745 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
746 emit_data
->args
[0] = lp_build_const_int32(bld_base
->base
.gallivm
, 0xf);
748 /* Fetch and project texture coordinates */
749 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
750 for (chan
= 0; chan
< 3; chan
++ ) {
751 coords
[chan
] = lp_build_emit_fetch(bld_base
,
754 if (opcode
== TGSI_OPCODE_TXP
)
755 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
761 if (opcode
== TGSI_OPCODE_TXP
)
762 coords
[3] = bld_base
->base
.one
;
764 /* Pack LOD bias value */
765 if (opcode
== TGSI_OPCODE_TXB
)
766 address
[count
++] = coords
[3];
768 if ((target
== TGSI_TEXTURE_CUBE
|| target
== TGSI_TEXTURE_SHADOWCUBE
) &&
769 opcode
!= TGSI_OPCODE_TXQ
)
770 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
772 /* Pack depth comparison value */
774 case TGSI_TEXTURE_SHADOW1D
:
775 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
776 case TGSI_TEXTURE_SHADOW2D
:
777 case TGSI_TEXTURE_SHADOWRECT
:
778 address
[count
++] = coords
[2];
780 case TGSI_TEXTURE_SHADOWCUBE
:
781 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
782 address
[count
++] = coords
[3];
784 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
785 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
788 /* Pack texture coordinates */
789 address
[count
++] = coords
[0];
791 case TGSI_TEXTURE_2D
:
792 case TGSI_TEXTURE_2D_ARRAY
:
793 case TGSI_TEXTURE_3D
:
794 case TGSI_TEXTURE_CUBE
:
795 case TGSI_TEXTURE_RECT
:
796 case TGSI_TEXTURE_SHADOW2D
:
797 case TGSI_TEXTURE_SHADOWRECT
:
798 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
799 case TGSI_TEXTURE_SHADOWCUBE
:
800 case TGSI_TEXTURE_2D_MSAA
:
801 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
802 case TGSI_TEXTURE_CUBE_ARRAY
:
803 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
804 address
[count
++] = coords
[1];
807 case TGSI_TEXTURE_3D
:
808 case TGSI_TEXTURE_CUBE
:
809 case TGSI_TEXTURE_SHADOWCUBE
:
810 case TGSI_TEXTURE_CUBE_ARRAY
:
811 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
812 address
[count
++] = coords
[2];
815 /* Pack array slice */
817 case TGSI_TEXTURE_1D_ARRAY
:
818 address
[count
++] = coords
[1];
821 case TGSI_TEXTURE_2D_ARRAY
:
822 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
823 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
824 address
[count
++] = coords
[2];
827 case TGSI_TEXTURE_CUBE_ARRAY
:
828 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
829 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
830 address
[count
++] = coords
[3];
834 if (opcode
== TGSI_OPCODE_TXL
)
835 address
[count
++] = coords
[3];
838 assert(!"Cannot handle more than 16 texture address parameters");
842 for (chan
= 0; chan
< count
; chan
++ ) {
843 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
845 LLVMInt32TypeInContext(gallivm
->context
),
849 /* Pad to power of two vector */
850 while (count
< util_next_power_of_two(count
))
851 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
853 emit_data
->args
[1] = lp_build_gather_values(gallivm
, address
, count
);
856 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
857 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
858 emit_data
->inst
->Src
[1].Register
.Index
);
859 emit_data
->args
[2] = build_indexed_load(bld_base
->base
.gallivm
,
863 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
864 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
865 emit_data
->inst
->Src
[1].Register
.Index
);
866 emit_data
->args
[3] = build_indexed_load(bld_base
->base
.gallivm
,
870 emit_data
->args
[4] = lp_build_const_int32(bld_base
->base
.gallivm
, target
);
872 emit_data
->arg_count
= 5;
873 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
874 * the writemask are clear */
875 emit_data
->dst_type
= LLVMVectorType(
876 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
880 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
881 struct lp_build_tgsi_context
* bld_base
,
882 struct lp_build_emit_data
* emit_data
)
884 struct lp_build_context
* base
= &bld_base
->base
;
887 sprintf(intr_name
, "%sv%ui32", action
->intr_name
,
888 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[1])));
890 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
891 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
892 emit_data
->args
, emit_data
->arg_count
);
895 static const struct lp_build_tgsi_action tex_action
= {
896 .fetch_args
= tex_fetch_args
,
897 .emit
= build_tex_intrinsic
,
898 .intr_name
= "llvm.SI.sample."
901 static const struct lp_build_tgsi_action txb_action
= {
902 .fetch_args
= tex_fetch_args
,
903 .emit
= build_tex_intrinsic
,
904 .intr_name
= "llvm.SI.sampleb."
907 static const struct lp_build_tgsi_action txl_action
= {
908 .fetch_args
= tex_fetch_args
,
909 .emit
= build_tex_intrinsic
,
910 .intr_name
= "llvm.SI.samplel."
913 static void create_function(struct si_shader_context
*si_shader_ctx
)
915 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
916 LLVMTypeRef params
[20], f32
, i8
, i32
, v2i32
, v3i32
;
919 i8
= LLVMInt8TypeInContext(gallivm
->context
);
920 i32
= LLVMInt32TypeInContext(gallivm
->context
);
921 f32
= LLVMFloatTypeInContext(gallivm
->context
);
922 v2i32
= LLVMVectorType(i32
, 2);
923 v3i32
= LLVMVectorType(i32
, 3);
925 params
[SI_PARAM_CONST
] = LLVMPointerType(LLVMVectorType(i8
, 16), CONST_ADDR_SPACE
);
926 params
[SI_PARAM_SAMPLER
] = params
[SI_PARAM_CONST
];
927 params
[SI_PARAM_RESOURCE
] = LLVMPointerType(LLVMVectorType(i8
, 32), CONST_ADDR_SPACE
);
929 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
930 params
[SI_PARAM_VERTEX_BUFFER
] = params
[SI_PARAM_SAMPLER
];
931 params
[SI_PARAM_VERTEX_INDEX
] = i32
;
932 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, 5);
935 params
[SI_PARAM_PRIM_MASK
] = i32
;
936 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
937 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
938 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
939 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
940 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
941 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
942 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
943 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
944 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
945 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
946 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
947 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
948 params
[SI_PARAM_FRONT_FACE
] = f32
;
949 params
[SI_PARAM_ANCILLARY
] = f32
;
950 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
951 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
952 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, 20);
955 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
956 for (i
= SI_PARAM_CONST
; i
<= SI_PARAM_VERTEX_BUFFER
; ++i
) {
957 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
958 LLVMAddAttribute(P
, LLVMInRegAttribute
);
962 int si_pipe_shader_create(
963 struct pipe_context
*ctx
,
964 struct si_pipe_shader
*shader
,
965 struct si_shader_key key
)
967 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
968 struct si_pipe_shader_selector
*sel
= shader
->selector
;
969 struct si_shader_context si_shader_ctx
;
970 struct tgsi_shader_info shader_info
;
971 struct lp_build_tgsi_context
* bld_base
;
973 unsigned char * inst_bytes
;
974 unsigned inst_byte_count
;
979 dump
= debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE
);
981 assert(shader
->shader
.noutput
== 0);
982 assert(shader
->shader
.ninterp
== 0);
983 assert(shader
->shader
.ninput
== 0);
985 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
986 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
987 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
989 tgsi_scan_shader(sel
->tokens
, &shader_info
);
990 shader
->shader
.uses_kill
= shader_info
.uses_kill
;
991 bld_base
->info
= &shader_info
;
992 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
993 bld_base
->emit_epilogue
= si_llvm_emit_epilogue
;
995 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
996 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = txb_action
;
997 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = txl_action
;
998 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
1000 si_shader_ctx
.radeon_bld
.load_input
= declare_input
;
1001 si_shader_ctx
.tokens
= sel
->tokens
;
1002 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
1003 si_shader_ctx
.shader
= shader
;
1004 si_shader_ctx
.key
= key
;
1005 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
1006 si_shader_ctx
.rctx
= rctx
;
1008 create_function(&si_shader_ctx
);
1010 shader
->shader
.nr_cbufs
= rctx
->framebuffer
.nr_cbufs
;
1012 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1013 * conversion fails. */
1015 tgsi_dump(sel
->tokens
, 0);
1018 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
1019 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
1023 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
1025 mod
= bld_base
->base
.gallivm
->module
;
1027 LLVMDumpModule(mod
);
1029 radeon_llvm_compile(mod
, &inst_bytes
, &inst_byte_count
, "SI", dump
);
1031 fprintf(stderr
, "SI CODE:\n");
1032 for (i
= 0; i
< inst_byte_count
; i
+=4 ) {
1033 fprintf(stderr
, "%02x%02x%02x%02x\n", inst_bytes
[i
+ 3],
1034 inst_bytes
[i
+ 2], inst_bytes
[i
+ 1],
1039 shader
->num_sgprs
= util_le32_to_cpu(*(uint32_t*)inst_bytes
);
1040 shader
->num_vgprs
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 4));
1041 shader
->spi_ps_input_ena
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 8));
1043 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
1044 tgsi_parse_free(&si_shader_ctx
.parse
);
1046 /* copy new shader */
1047 si_resource_reference(&shader
->bo
, NULL
);
1048 shader
->bo
= si_resource_create_custom(ctx
->screen
, PIPE_USAGE_IMMUTABLE
,
1049 inst_byte_count
- 12);
1050 if (shader
->bo
== NULL
) {
1054 ptr
= (uint32_t*)rctx
->ws
->buffer_map(shader
->bo
->cs_buf
, rctx
->cs
, PIPE_TRANSFER_WRITE
);
1055 if (0 /*R600_BIG_ENDIAN*/) {
1056 for (i
= 0; i
< (inst_byte_count
-12)/4; ++i
) {
1057 ptr
[i
] = util_bswap32(*(uint32_t*)(inst_bytes
+12 + i
*4));
1060 memcpy(ptr
, inst_bytes
+ 12, inst_byte_count
- 12);
1062 rctx
->ws
->buffer_unmap(shader
->bo
->cs_buf
);
1069 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
1071 si_resource_reference(&shader
->bo
, NULL
);