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 unsigned ninput_emitted
;
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 gallivm_state
* gallivm
,
100 LLVMValueRef base_ptr
,
103 LLVMValueRef computed_ptr
= LLVMBuildGEP(
104 gallivm
->builder
, base_ptr
, &offset
, 1, "");
106 return LLVMBuildLoad(gallivm
->builder
, computed_ptr
, "");
109 static void declare_input_vs(
110 struct si_shader_context
* si_shader_ctx
,
111 unsigned input_index
,
112 const struct tgsi_full_declaration
*decl
)
114 LLVMValueRef t_list_ptr
;
115 LLVMValueRef t_offset
;
117 LLVMValueRef attribute_offset
;
118 LLVMValueRef buffer_index_reg
;
119 LLVMValueRef args
[3];
120 LLVMTypeRef vec4_type
;
122 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
123 //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index];
126 /* Load the T list */
127 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFER
);
129 t_offset
= lp_build_const_int32(base
->gallivm
, input_index
);
131 t_list
= build_indexed_load(base
->gallivm
, t_list_ptr
, t_offset
);
133 /* Build the attribute offset */
134 attribute_offset
= lp_build_const_int32(base
->gallivm
, 0);
136 /* Load the buffer index, which is always stored in VGPR0
137 * for Vertex Shaders */
138 buffer_index_reg
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_INDEX
);
140 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
142 args
[1] = attribute_offset
;
143 args
[2] = buffer_index_reg
;
144 input
= lp_build_intrinsic(base
->gallivm
->builder
,
145 "llvm.SI.vs.load.input", vec4_type
, args
, 3);
147 /* Break up the vec4 into individual components */
148 for (chan
= 0; chan
< 4; chan
++) {
149 LLVMValueRef llvm_chan
= lp_build_const_int32(base
->gallivm
, chan
);
150 /* XXX: Use a helper function for this. There is one in
152 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
153 LLVMBuildExtractElement(base
->gallivm
->builder
,
154 input
, llvm_chan
, "");
158 static void declare_input_fs(
159 struct si_shader_context
* si_shader_ctx
,
160 unsigned input_index
,
161 const struct tgsi_full_declaration
*decl
)
163 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
164 struct lp_build_context
* base
=
165 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
166 struct gallivm_state
* gallivm
= base
->gallivm
;
167 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
168 LLVMValueRef main_fn
= si_shader_ctx
->radeon_bld
.main_fn
;
170 LLVMValueRef interp_param
;
171 const char * intr_name
;
174 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
175 * quad begins a new primitive. Bit 0 always needs
177 * [32:16] ParamOffset
180 LLVMValueRef params
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
181 LLVMValueRef attr_number
;
185 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
186 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
188 radeon_llvm_reg_index_soa(input_index
, chan
);
189 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
190 LLVMGetParam(main_fn
, SI_PARAM_POS_X_FLOAT
+ chan
);
193 /* RCP for fragcoord.w */
194 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
195 LLVMBuildFDiv(gallivm
->builder
,
196 lp_build_const_float(gallivm
, 1.0f
),
197 si_shader_ctx
->radeon_bld
.inputs
[soa_index
],
203 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
204 LLVMValueRef face
, is_face_positive
;
206 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
208 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
210 lp_build_const_float(gallivm
, 0.0f
),
213 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
214 LLVMBuildSelect(gallivm
->builder
,
216 lp_build_const_float(gallivm
, 1.0f
),
217 lp_build_const_float(gallivm
, 0.0f
),
219 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
220 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
221 lp_build_const_float(gallivm
, 0.0f
);
222 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
223 lp_build_const_float(gallivm
, 1.0f
);
228 shader
->input
[input_index
].param_offset
= shader
->ninterp
++;
229 attr_number
= lp_build_const_int32(gallivm
,
230 shader
->input
[input_index
].param_offset
);
232 /* XXX: Handle all possible interpolation modes */
233 switch (decl
->Interp
.Interpolate
) {
234 case TGSI_INTERPOLATE_COLOR
:
235 if (si_shader_ctx
->key
.flatshade
) {
238 if (decl
->Interp
.Centroid
)
239 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
241 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
244 case TGSI_INTERPOLATE_CONSTANT
:
247 case TGSI_INTERPOLATE_LINEAR
:
248 if (decl
->Interp
.Centroid
)
249 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTROID
);
251 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTER
);
253 case TGSI_INTERPOLATE_PERSPECTIVE
:
254 if (decl
->Interp
.Centroid
)
255 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
257 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
260 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
264 if (!si_shader_ctx
->ninput_emitted
++) {
265 /* Enable whole quad mode */
266 lp_build_intrinsic(gallivm
->builder
,
268 LLVMVoidTypeInContext(gallivm
->context
),
272 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
274 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
275 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
276 si_shader_ctx
->key
.color_two_side
) {
277 LLVMValueRef args
[4];
278 LLVMValueRef face
, is_face_positive
;
279 LLVMValueRef back_attr_number
=
280 lp_build_const_int32(gallivm
,
281 shader
->input
[input_index
].param_offset
+ 1);
283 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
285 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
287 lp_build_const_float(gallivm
, 0.0f
),
291 args
[3] = interp_param
;
292 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
293 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
294 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
295 LLVMValueRef front
, back
;
298 args
[1] = attr_number
;
299 front
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
300 input_type
, args
, args
[3] ? 4 : 3,
301 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
303 args
[1] = back_attr_number
;
304 back
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
305 input_type
, args
, args
[3] ? 4 : 3,
306 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
308 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
309 LLVMBuildSelect(gallivm
->builder
,
318 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
319 LLVMValueRef args
[4];
320 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
321 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
323 args
[1] = attr_number
;
325 args
[3] = interp_param
;
326 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
327 build_intrinsic(base
->gallivm
->builder
, intr_name
,
328 input_type
, args
, args
[3] ? 4 : 3,
329 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
334 static void declare_input(
335 struct radeon_llvm_context
* radeon_bld
,
336 unsigned input_index
,
337 const struct tgsi_full_declaration
*decl
)
339 struct si_shader_context
* si_shader_ctx
=
340 si_shader_context(&radeon_bld
->soa
.bld_base
);
341 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
342 declare_input_vs(si_shader_ctx
, input_index
, decl
);
343 } else if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
344 declare_input_fs(si_shader_ctx
, input_index
, decl
);
346 fprintf(stderr
, "Warning: Unsupported shader type,\n");
350 static LLVMValueRef
fetch_constant(
351 struct lp_build_tgsi_context
* bld_base
,
352 const struct tgsi_full_src_register
*reg
,
353 enum tgsi_opcode_type type
,
356 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
357 struct lp_build_context
* base
= &bld_base
->base
;
360 LLVMValueRef args
[2];
363 if (swizzle
== LP_CHAN_ALL
) {
365 LLVMValueRef values
[4];
366 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
367 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
369 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
372 /* Load the resource descriptor */
373 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
374 args
[0] = build_indexed_load(base
->gallivm
, ptr
, bld_base
->uint_bld
.zero
);
376 args
[1] = lp_build_const_int32(base
->gallivm
, (reg
->Register
.Index
* 4 + swizzle
) * 4);
377 if (reg
->Register
.Indirect
) {
378 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
379 LLVMValueRef addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
380 LLVMValueRef idx
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
381 idx
= lp_build_mul_imm(&bld_base
->uint_bld
, idx
, 16);
382 args
[1] = lp_build_add(&bld_base
->uint_bld
, idx
, args
[1]);
385 result
= build_intrinsic(base
->gallivm
->builder
, "llvm.SI.load.const", base
->elem_type
,
386 args
, 2, LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
);
388 return bitcast(bld_base
, type
, result
);
391 /* Initialize arguments for the shader export intrinsic */
392 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
393 struct tgsi_full_declaration
*d
,
398 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
399 struct lp_build_context
*uint
=
400 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
401 struct lp_build_context
*base
= &bld_base
->base
;
402 unsigned compressed
= 0;
405 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
406 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
408 if (cbuf
>= 0 && cbuf
< 8) {
409 compressed
= (si_shader_ctx
->key
.export_16bpc
>> cbuf
) & 0x1;
412 si_shader_ctx
->shader
->spi_shader_col_format
|=
413 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
415 si_shader_ctx
->shader
->spi_shader_col_format
|=
416 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
421 /* Pixel shader needs to pack output values before export */
422 for (chan
= 0; chan
< 2; chan
++ ) {
423 LLVMValueRef
*out_ptr
=
424 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
];
425 args
[0] = LLVMBuildLoad(base
->gallivm
->builder
,
426 out_ptr
[2 * chan
], "");
427 args
[1] = LLVMBuildLoad(base
->gallivm
->builder
,
428 out_ptr
[2 * chan
+ 1], "");
430 build_intrinsic(base
->gallivm
->builder
,
432 LLVMInt32TypeInContext(base
->gallivm
->context
),
434 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
435 args
[chan
+ 7] = args
[chan
+ 5] =
436 LLVMBuildBitCast(base
->gallivm
->builder
,
438 LLVMFloatTypeInContext(base
->gallivm
->context
),
445 for (chan
= 0; chan
< 4; chan
++ ) {
446 LLVMValueRef out_ptr
=
447 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][chan
];
448 /* +5 because the first output value will be
449 * the 6th argument to the intrinsic. */
450 args
[chan
+ 5] = LLVMBuildLoad(base
->gallivm
->builder
,
454 /* Clear COMPR flag */
455 args
[4] = uint
->zero
;
458 /* XXX: This controls which components of the output
459 * registers actually get exported. (e.g bit 0 means export
460 * X component, bit 1 means export Y component, etc.) I'm
461 * hard coding this to 0xf for now. In the future, we might
462 * want to do something else. */
463 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
465 /* Specify whether the EXEC mask represents the valid mask */
466 args
[1] = uint
->zero
;
468 /* Specify whether this is the last export */
469 args
[2] = uint
->zero
;
471 /* Specify the target we are exporting */
472 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
474 /* XXX: We probably need to keep track of the output
475 * values, so we know what we are passing to the next
479 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
482 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
483 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
485 if (si_shader_ctx
->key
.alpha_func
!= PIPE_FUNC_NEVER
) {
486 LLVMValueRef out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][3];
487 LLVMValueRef alpha_pass
=
488 lp_build_cmp(&bld_base
->base
,
489 si_shader_ctx
->key
.alpha_func
,
490 LLVMBuildLoad(gallivm
->builder
, out_ptr
, ""),
491 lp_build_const_float(gallivm
, si_shader_ctx
->key
.alpha_ref
));
493 lp_build_select(&bld_base
->base
,
495 lp_build_const_float(gallivm
, 1.0f
),
496 lp_build_const_float(gallivm
, -1.0f
));
498 build_intrinsic(gallivm
->builder
,
500 LLVMVoidTypeInContext(gallivm
->context
),
503 build_intrinsic(gallivm
->builder
,
505 LLVMVoidTypeInContext(gallivm
->context
),
510 /* XXX: This is partially implemented for VS only at this point. It is not complete */
511 static void si_llvm_emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
513 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
514 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
515 struct lp_build_context
* base
= &bld_base
->base
;
516 struct lp_build_context
* uint
=
517 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
518 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
519 LLVMValueRef args
[9];
520 LLVMValueRef last_args
[9] = { 0 };
521 unsigned color_count
= 0;
522 unsigned param_count
= 0;
523 int depth_index
= -1, stencil_index
= -1;
525 while (!tgsi_parse_end_of_tokens(parse
)) {
526 struct tgsi_full_declaration
*d
=
527 &parse
->FullToken
.FullDeclaration
;
532 tgsi_parse_token(parse
);
534 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_PROPERTY
&&
535 parse
->FullToken
.FullProperty
.Property
.PropertyName
==
536 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
)
537 shader
->fs_write_all
= TRUE
;
539 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
542 switch (d
->Declaration
.File
) {
543 case TGSI_FILE_INPUT
:
544 i
= shader
->ninput
++;
545 shader
->input
[i
].name
= d
->Semantic
.Name
;
546 shader
->input
[i
].sid
= d
->Semantic
.Index
;
547 shader
->input
[i
].interpolate
= d
->Interp
.Interpolate
;
548 shader
->input
[i
].centroid
= d
->Interp
.Centroid
;
551 case TGSI_FILE_OUTPUT
:
552 i
= shader
->noutput
++;
553 shader
->output
[i
].name
= d
->Semantic
.Name
;
554 shader
->output
[i
].sid
= d
->Semantic
.Index
;
555 shader
->output
[i
].interpolate
= d
->Interp
.Interpolate
;
562 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
563 /* Select the correct target */
564 switch(d
->Semantic
.Name
) {
565 case TGSI_SEMANTIC_PSIZE
:
566 target
= V_008DFC_SQ_EXP_POS
;
568 case TGSI_SEMANTIC_POSITION
:
569 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
570 target
= V_008DFC_SQ_EXP_POS
;
576 case TGSI_SEMANTIC_STENCIL
:
577 stencil_index
= index
;
579 case TGSI_SEMANTIC_COLOR
:
580 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
581 case TGSI_SEMANTIC_BCOLOR
:
582 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
583 shader
->output
[i
].param_offset
= param_count
;
586 target
= V_008DFC_SQ_EXP_MRT
+ color_count
;
587 if (color_count
== 0 &&
588 si_shader_ctx
->key
.alpha_func
!= PIPE_FUNC_ALWAYS
)
589 si_alpha_test(bld_base
, index
);
594 case TGSI_SEMANTIC_FOG
:
595 case TGSI_SEMANTIC_GENERIC
:
596 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
597 shader
->output
[i
].param_offset
= param_count
;
603 "Warning: SI unhandled output type:%d\n",
607 si_llvm_init_export_args(bld_base
, d
, index
, target
, args
);
609 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
?
610 (d
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) :
611 (d
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
)) {
613 lp_build_intrinsic(base
->gallivm
->builder
,
615 LLVMVoidTypeInContext(base
->gallivm
->context
),
619 memcpy(last_args
, args
, sizeof(args
));
621 lp_build_intrinsic(base
->gallivm
->builder
,
623 LLVMVoidTypeInContext(base
->gallivm
->context
),
630 if (depth_index
>= 0 || stencil_index
>= 0) {
631 LLVMValueRef out_ptr
;
634 /* Specify the target we are exporting */
635 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
637 if (depth_index
>= 0) {
638 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
639 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
642 if (stencil_index
< 0) {
649 if (stencil_index
>= 0) {
650 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
653 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
660 /* Specify which components to enable */
661 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
665 args
[4] = uint
->zero
;
668 lp_build_intrinsic(base
->gallivm
->builder
,
670 LLVMVoidTypeInContext(base
->gallivm
->context
),
673 memcpy(last_args
, args
, sizeof(args
));
677 assert(si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
679 /* Specify which components to enable */
680 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
682 /* Specify the target we are exporting */
683 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
685 /* Set COMPR flag to zero to export data as 32-bit */
686 last_args
[4] = uint
->zero
;
689 last_args
[5]= uint
->zero
;
690 last_args
[6]= uint
->zero
;
691 last_args
[7]= uint
->zero
;
692 last_args
[8]= uint
->zero
;
694 si_shader_ctx
->shader
->spi_shader_col_format
|=
695 V_028714_SPI_SHADER_32_ABGR
;
698 /* Specify whether the EXEC mask represents the valid mask */
699 last_args
[1] = lp_build_const_int32(base
->gallivm
,
700 si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
702 if (shader
->fs_write_all
&& shader
->nr_cbufs
> 1) {
705 /* Specify that this is not yet the last export */
706 last_args
[2] = lp_build_const_int32(base
->gallivm
, 0);
708 for (i
= 1; i
< shader
->nr_cbufs
; i
++) {
709 /* Specify the target we are exporting */
710 last_args
[3] = lp_build_const_int32(base
->gallivm
,
711 V_008DFC_SQ_EXP_MRT
+ i
);
713 lp_build_intrinsic(base
->gallivm
->builder
,
715 LLVMVoidTypeInContext(base
->gallivm
->context
),
718 si_shader_ctx
->shader
->spi_shader_col_format
|=
719 si_shader_ctx
->shader
->spi_shader_col_format
<< 4;
722 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
725 /* Specify that this is the last export */
726 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
728 lp_build_intrinsic(base
->gallivm
->builder
,
730 LLVMVoidTypeInContext(base
->gallivm
->context
),
733 /* XXX: Look up what this function does */
734 /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
737 static void tex_fetch_args(
738 struct lp_build_tgsi_context
* bld_base
,
739 struct lp_build_emit_data
* emit_data
)
741 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
742 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
743 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
744 unsigned opcode
= inst
->Instruction
.Opcode
;
745 unsigned target
= inst
->Texture
.Texture
;
748 LLVMValueRef coords
[4];
749 LLVMValueRef address
[16];
754 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
755 emit_data
->args
[0] = lp_build_const_int32(bld_base
->base
.gallivm
, 0xf);
757 /* Fetch and project texture coordinates */
758 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
759 for (chan
= 0; chan
< 3; chan
++ ) {
760 coords
[chan
] = lp_build_emit_fetch(bld_base
,
763 if (opcode
== TGSI_OPCODE_TXP
)
764 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
770 if (opcode
== TGSI_OPCODE_TXP
)
771 coords
[3] = bld_base
->base
.one
;
773 /* Pack LOD bias value */
774 if (opcode
== TGSI_OPCODE_TXB
)
775 address
[count
++] = coords
[3];
777 if ((target
== TGSI_TEXTURE_CUBE
|| target
== TGSI_TEXTURE_SHADOWCUBE
) &&
778 opcode
!= TGSI_OPCODE_TXQ
)
779 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
781 /* Pack depth comparison value */
783 case TGSI_TEXTURE_SHADOW1D
:
784 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
785 case TGSI_TEXTURE_SHADOW2D
:
786 case TGSI_TEXTURE_SHADOWRECT
:
787 address
[count
++] = coords
[2];
789 case TGSI_TEXTURE_SHADOWCUBE
:
790 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
791 address
[count
++] = coords
[3];
793 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
794 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
797 /* Pack texture coordinates */
798 address
[count
++] = coords
[0];
800 case TGSI_TEXTURE_2D
:
801 case TGSI_TEXTURE_2D_ARRAY
:
802 case TGSI_TEXTURE_3D
:
803 case TGSI_TEXTURE_CUBE
:
804 case TGSI_TEXTURE_RECT
:
805 case TGSI_TEXTURE_SHADOW2D
:
806 case TGSI_TEXTURE_SHADOWRECT
:
807 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
808 case TGSI_TEXTURE_SHADOWCUBE
:
809 case TGSI_TEXTURE_2D_MSAA
:
810 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
811 case TGSI_TEXTURE_CUBE_ARRAY
:
812 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
813 address
[count
++] = coords
[1];
816 case TGSI_TEXTURE_3D
:
817 case TGSI_TEXTURE_CUBE
:
818 case TGSI_TEXTURE_SHADOWCUBE
:
819 case TGSI_TEXTURE_CUBE_ARRAY
:
820 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
821 address
[count
++] = coords
[2];
824 /* Pack array slice */
826 case TGSI_TEXTURE_1D_ARRAY
:
827 address
[count
++] = coords
[1];
830 case TGSI_TEXTURE_2D_ARRAY
:
831 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
832 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
833 address
[count
++] = coords
[2];
836 case TGSI_TEXTURE_CUBE_ARRAY
:
837 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
838 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
839 address
[count
++] = coords
[3];
843 if (opcode
== TGSI_OPCODE_TXL
)
844 address
[count
++] = coords
[3];
847 assert(!"Cannot handle more than 16 texture address parameters");
851 for (chan
= 0; chan
< count
; chan
++ ) {
852 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
854 LLVMInt32TypeInContext(gallivm
->context
),
858 /* Pad to power of two vector */
859 while (count
< util_next_power_of_two(count
))
860 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
862 emit_data
->args
[1] = lp_build_gather_values(gallivm
, address
, count
);
865 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
866 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
867 emit_data
->inst
->Src
[1].Register
.Index
);
868 emit_data
->args
[2] = build_indexed_load(bld_base
->base
.gallivm
,
872 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
873 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
874 emit_data
->inst
->Src
[1].Register
.Index
);
875 emit_data
->args
[3] = build_indexed_load(bld_base
->base
.gallivm
,
879 emit_data
->args
[4] = lp_build_const_int32(bld_base
->base
.gallivm
, target
);
881 emit_data
->arg_count
= 5;
882 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
883 * the writemask are clear */
884 emit_data
->dst_type
= LLVMVectorType(
885 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
889 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
890 struct lp_build_tgsi_context
* bld_base
,
891 struct lp_build_emit_data
* emit_data
)
893 struct lp_build_context
* base
= &bld_base
->base
;
896 sprintf(intr_name
, "%sv%ui32", action
->intr_name
,
897 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[1])));
899 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
900 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
901 emit_data
->args
, emit_data
->arg_count
);
904 static const struct lp_build_tgsi_action tex_action
= {
905 .fetch_args
= tex_fetch_args
,
906 .emit
= build_tex_intrinsic
,
907 .intr_name
= "llvm.SI.sample."
910 static const struct lp_build_tgsi_action txb_action
= {
911 .fetch_args
= tex_fetch_args
,
912 .emit
= build_tex_intrinsic
,
913 .intr_name
= "llvm.SI.sampleb."
916 static const struct lp_build_tgsi_action txl_action
= {
917 .fetch_args
= tex_fetch_args
,
918 .emit
= build_tex_intrinsic
,
919 .intr_name
= "llvm.SI.samplel."
922 static void create_function(struct si_shader_context
*si_shader_ctx
)
924 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
925 LLVMTypeRef params
[20], f32
, i8
, i32
, v2i32
, v3i32
;
928 i8
= LLVMInt8TypeInContext(gallivm
->context
);
929 i32
= LLVMInt32TypeInContext(gallivm
->context
);
930 f32
= LLVMFloatTypeInContext(gallivm
->context
);
931 v2i32
= LLVMVectorType(i32
, 2);
932 v3i32
= LLVMVectorType(i32
, 3);
934 params
[SI_PARAM_CONST
] = LLVMPointerType(LLVMVectorType(i8
, 16), CONST_ADDR_SPACE
);
935 params
[SI_PARAM_SAMPLER
] = params
[SI_PARAM_CONST
];
936 params
[SI_PARAM_RESOURCE
] = LLVMPointerType(LLVMVectorType(i8
, 32), CONST_ADDR_SPACE
);
938 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
939 params
[SI_PARAM_VERTEX_BUFFER
] = params
[SI_PARAM_SAMPLER
];
940 params
[SI_PARAM_VERTEX_INDEX
] = i32
;
941 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, 5);
944 params
[SI_PARAM_PRIM_MASK
] = i32
;
945 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
946 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
947 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
948 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
949 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
950 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
951 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
952 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
953 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
954 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
955 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
956 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
957 params
[SI_PARAM_FRONT_FACE
] = f32
;
958 params
[SI_PARAM_ANCILLARY
] = f32
;
959 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
960 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
961 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, 20);
964 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
965 for (i
= SI_PARAM_CONST
; i
<= SI_PARAM_VERTEX_BUFFER
; ++i
) {
966 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
967 LLVMAddAttribute(P
, LLVMInRegAttribute
);
971 int si_pipe_shader_create(
972 struct pipe_context
*ctx
,
973 struct si_pipe_shader
*shader
,
974 struct si_shader_key key
)
976 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
977 struct si_pipe_shader_selector
*sel
= shader
->selector
;
978 struct si_shader_context si_shader_ctx
;
979 struct tgsi_shader_info shader_info
;
980 struct lp_build_tgsi_context
* bld_base
;
982 unsigned char * inst_bytes
;
983 unsigned inst_byte_count
;
988 dump
= debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE
);
990 assert(shader
->shader
.noutput
== 0);
991 assert(shader
->shader
.ninterp
== 0);
992 assert(shader
->shader
.ninput
== 0);
994 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
995 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
996 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
998 tgsi_scan_shader(sel
->tokens
, &shader_info
);
999 shader
->shader
.uses_kill
= shader_info
.uses_kill
;
1000 bld_base
->info
= &shader_info
;
1001 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
1002 bld_base
->emit_epilogue
= si_llvm_emit_epilogue
;
1004 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
1005 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = txb_action
;
1006 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = txl_action
;
1007 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
1009 si_shader_ctx
.radeon_bld
.load_input
= declare_input
;
1010 si_shader_ctx
.tokens
= sel
->tokens
;
1011 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
1012 si_shader_ctx
.shader
= shader
;
1013 si_shader_ctx
.key
= key
;
1014 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
1015 si_shader_ctx
.rctx
= rctx
;
1017 create_function(&si_shader_ctx
);
1019 shader
->shader
.nr_cbufs
= rctx
->framebuffer
.nr_cbufs
;
1021 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1022 * conversion fails. */
1024 tgsi_dump(sel
->tokens
, 0);
1027 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
1028 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
1032 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
1034 mod
= bld_base
->base
.gallivm
->module
;
1036 LLVMDumpModule(mod
);
1038 radeon_llvm_compile(mod
, &inst_bytes
, &inst_byte_count
, "SI", dump
);
1040 fprintf(stderr
, "SI CODE:\n");
1041 for (i
= 0; i
< inst_byte_count
; i
+=4 ) {
1042 fprintf(stderr
, "%02x%02x%02x%02x\n", inst_bytes
[i
+ 3],
1043 inst_bytes
[i
+ 2], inst_bytes
[i
+ 1],
1048 shader
->num_sgprs
= util_le32_to_cpu(*(uint32_t*)inst_bytes
);
1049 shader
->num_vgprs
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 4));
1050 shader
->spi_ps_input_ena
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 8));
1052 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
1053 tgsi_parse_free(&si_shader_ctx
.parse
);
1055 /* copy new shader */
1056 si_resource_reference(&shader
->bo
, NULL
);
1057 shader
->bo
= si_resource_create_custom(ctx
->screen
, PIPE_USAGE_IMMUTABLE
,
1058 inst_byte_count
- 12);
1059 if (shader
->bo
== NULL
) {
1063 ptr
= (uint32_t*)rctx
->ws
->buffer_map(shader
->bo
->cs_buf
, rctx
->cs
, PIPE_TRANSFER_WRITE
);
1064 if (0 /*R600_BIG_ENDIAN*/) {
1065 for (i
= 0; i
< (inst_byte_count
-12)/4; ++i
) {
1066 ptr
[i
] = util_bswap32(*(uint32_t*)(inst_bytes
+12 + i
*4));
1069 memcpy(ptr
, inst_bytes
+ 12, inst_byte_count
- 12);
1071 rctx
->ws
->buffer_unmap(shader
->bo
->cs_buf
);
1078 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
1080 si_resource_reference(&shader
->bo
, NULL
);