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 "radeon_llvm.h"
37 #include "radeon_llvm_emit.h"
38 #include "tgsi/tgsi_info.h"
39 #include "tgsi/tgsi_parse.h"
40 #include "tgsi/tgsi_scan.h"
41 #include "tgsi/tgsi_dump.h"
43 #include "radeonsi_pipe.h"
44 #include "radeonsi_shader.h"
52 struct si_shader_context
54 struct radeon_llvm_context radeon_bld
;
55 struct r600_context
*rctx
;
56 struct tgsi_parse_context parse
;
57 struct tgsi_token
* tokens
;
58 struct si_pipe_shader
*shader
;
59 struct si_shader_key key
;
60 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
61 unsigned ninput_emitted
;
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
94 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
96 * @param offset The offset parameter specifies the number of
97 * elements to offset, not the number of bytes or dwords. An element is the
98 * the type pointed to by the base_ptr parameter (e.g. int is the element of
101 * When LLVM lowers the load instruction, it will convert the element offset
102 * into a dword offset automatically.
105 static LLVMValueRef
build_indexed_load(
106 struct gallivm_state
* gallivm
,
107 LLVMValueRef base_ptr
,
110 LLVMValueRef computed_ptr
= LLVMBuildGEP(
111 gallivm
->builder
, base_ptr
, &offset
, 1, "");
113 return LLVMBuildLoad(gallivm
->builder
, computed_ptr
, "");
117 * Load a value stored in one of the user SGPRs
119 * @param sgpr This is the sgpr to load the value from. If you need to load a
120 * value that is stored in consecutive SGPR registers (e.g. a 64-bit pointer),
121 * then you should pass the index of the first SGPR that holds the value. For
122 * example, if you want to load a pointer that is stored in SGPRs 2 and 3, then
123 * use pass 2 for the sgpr parameter.
125 * The value of the sgpr parameter must also be aligned to the width of the type
126 * being loaded, so that the sgpr parameter is divisible by the dword width of the
127 * type. For example, if the value being loaded is two dwords wide, then the sgpr
128 * parameter must be divisible by two.
130 static LLVMValueRef
use_sgpr(
131 struct gallivm_state
* gallivm
,
135 LLVMValueRef sgpr_index
;
136 LLVMTypeRef ret_type
;
139 sgpr_index
= lp_build_const_int32(gallivm
, sgpr
);
142 case SGPR_CONST_PTR_F32
:
143 assert(sgpr
% 2 == 0);
144 ret_type
= LLVMFloatTypeInContext(gallivm
->context
);
145 ret_type
= LLVMPointerType(ret_type
, CONST_ADDR_SPACE
);
149 ret_type
= LLVMInt32TypeInContext(gallivm
->context
);
153 assert(sgpr
% 2 == 0);
154 ret_type
= LLVMInt64TypeInContext(gallivm
->context
);
157 case SGPR_CONST_PTR_V4I32
:
158 assert(sgpr
% 2 == 0);
159 ret_type
= LLVMInt32TypeInContext(gallivm
->context
);
160 ret_type
= LLVMVectorType(ret_type
, 4);
161 ret_type
= LLVMPointerType(ret_type
, CONST_ADDR_SPACE
);
164 case SGPR_CONST_PTR_V8I32
:
165 assert(sgpr
% 2 == 0);
166 ret_type
= LLVMInt32TypeInContext(gallivm
->context
);
167 ret_type
= LLVMVectorType(ret_type
, 8);
168 ret_type
= LLVMPointerType(ret_type
, CONST_ADDR_SPACE
);
172 assert(!"Unsupported SGPR type in use_sgpr()");
176 ret_type
= LLVMPointerType(ret_type
, USER_SGPR_ADDR_SPACE
);
177 ptr
= LLVMBuildIntToPtr(gallivm
->builder
, sgpr_index
, ret_type
, "");
178 return LLVMBuildLoad(gallivm
->builder
, ptr
, "");
181 static void declare_input_vs(
182 struct si_shader_context
* si_shader_ctx
,
183 unsigned input_index
,
184 const struct tgsi_full_declaration
*decl
)
186 LLVMValueRef t_list_ptr
;
187 LLVMValueRef t_offset
;
189 LLVMValueRef attribute_offset
;
190 LLVMValueRef buffer_index_reg
;
191 LLVMValueRef args
[3];
192 LLVMTypeRef vec4_type
;
194 struct lp_build_context
* uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
195 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
196 //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index];
199 /* Load the T list */
200 t_list_ptr
= use_sgpr(base
->gallivm
, SGPR_CONST_PTR_V4I32
, SI_SGPR_VERTEX_BUFFER
);
202 t_offset
= lp_build_const_int32(base
->gallivm
, input_index
);
204 t_list
= build_indexed_load(base
->gallivm
, t_list_ptr
, t_offset
);
206 /* Build the attribute offset */
207 attribute_offset
= lp_build_const_int32(base
->gallivm
, 0);
209 /* Load the buffer index is always, which is always stored in VGPR0
210 * for Vertex Shaders */
211 buffer_index_reg
= build_intrinsic(base
->gallivm
->builder
,
212 "llvm.SI.vs.load.buffer.index", uint
->elem_type
, NULL
, 0,
213 LLVMReadNoneAttribute
);
215 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
217 args
[1] = attribute_offset
;
218 args
[2] = buffer_index_reg
;
219 input
= lp_build_intrinsic(base
->gallivm
->builder
,
220 "llvm.SI.vs.load.input", vec4_type
, args
, 3);
222 /* Break up the vec4 into individual components */
223 for (chan
= 0; chan
< 4; chan
++) {
224 LLVMValueRef llvm_chan
= lp_build_const_int32(base
->gallivm
, chan
);
225 /* XXX: Use a helper function for this. There is one in
227 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
228 LLVMBuildExtractElement(base
->gallivm
->builder
,
229 input
, llvm_chan
, "");
233 static void declare_input_fs(
234 struct si_shader_context
* si_shader_ctx
,
235 unsigned input_index
,
236 const struct tgsi_full_declaration
*decl
)
238 const char * intr_name
;
240 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
241 struct lp_build_context
* base
=
242 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
243 struct gallivm_state
* gallivm
= base
->gallivm
;
244 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
247 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
248 * quad begins a new primitive. Bit 0 always needs
250 * [32:16] ParamOffset
253 LLVMValueRef params
= use_sgpr(base
->gallivm
, SGPR_I32
, SI_PS_NUM_USER_SGPR
);
254 LLVMValueRef attr_number
;
256 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
257 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
258 LLVMValueRef args
[1];
260 radeon_llvm_reg_index_soa(input_index
, chan
);
261 args
[0] = lp_build_const_int32(gallivm
, chan
);
262 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
263 build_intrinsic(base
->gallivm
->builder
,
264 "llvm.SI.fs.read.pos", input_type
,
265 args
, 1, LLVMReadNoneAttribute
);
270 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
271 LLVMValueRef face
, is_face_positive
;
273 face
= build_intrinsic(gallivm
->builder
,
274 "llvm.SI.fs.read.face",
276 NULL
, 0, LLVMReadNoneAttribute
);
277 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
279 lp_build_const_float(gallivm
, 0.0f
),
282 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
283 LLVMBuildSelect(gallivm
->builder
,
285 lp_build_const_float(gallivm
, 1.0f
),
286 lp_build_const_float(gallivm
, 0.0f
),
288 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
289 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
290 lp_build_const_float(gallivm
, 0.0f
);
291 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
292 lp_build_const_float(gallivm
, 1.0f
);
297 shader
->input
[input_index
].param_offset
= shader
->ninterp
++;
298 attr_number
= lp_build_const_int32(gallivm
,
299 shader
->input
[input_index
].param_offset
);
301 /* XXX: Handle all possible interpolation modes */
302 switch (decl
->Interp
.Interpolate
) {
303 case TGSI_INTERPOLATE_COLOR
:
304 /* XXX: Flat shading hangs the GPU */
305 if (si_shader_ctx
->rctx
->queued
.named
.rasterizer
&&
306 si_shader_ctx
->rctx
->queued
.named
.rasterizer
->flatshade
) {
308 intr_name
= "llvm.SI.fs.interp.constant";
310 intr_name
= "llvm.SI.fs.interp.linear.center";
313 if (decl
->Interp
.Centroid
)
314 intr_name
= "llvm.SI.fs.interp.persp.centroid";
316 intr_name
= "llvm.SI.fs.interp.persp.center";
319 case TGSI_INTERPOLATE_CONSTANT
:
320 /* XXX: Flat shading hangs the GPU */
322 intr_name
= "llvm.SI.fs.interp.constant";
325 case TGSI_INTERPOLATE_LINEAR
:
326 if (decl
->Interp
.Centroid
)
327 intr_name
= "llvm.SI.fs.interp.linear.centroid";
329 intr_name
= "llvm.SI.fs.interp.linear.center";
331 case TGSI_INTERPOLATE_PERSPECTIVE
:
332 if (decl
->Interp
.Centroid
)
333 intr_name
= "llvm.SI.fs.interp.persp.centroid";
335 intr_name
= "llvm.SI.fs.interp.persp.center";
338 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
342 if (!si_shader_ctx
->ninput_emitted
++) {
343 /* Enable whole quad mode */
344 lp_build_intrinsic(gallivm
->builder
,
346 LLVMVoidTypeInContext(gallivm
->context
),
350 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
351 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
352 si_shader_ctx
->key
.color_two_side
) {
353 LLVMValueRef args
[3];
354 LLVMValueRef face
, is_face_positive
;
355 LLVMValueRef back_attr_number
=
356 lp_build_const_int32(gallivm
,
357 shader
->input
[input_index
].param_offset
+ 1);
359 face
= build_intrinsic(gallivm
->builder
,
360 "llvm.SI.fs.read.face",
362 NULL
, 0, LLVMReadNoneAttribute
);
363 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
365 lp_build_const_float(gallivm
, 0.0f
),
369 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
370 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
371 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
372 LLVMValueRef front
, back
;
375 args
[1] = attr_number
;
376 front
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
377 input_type
, args
, 3, LLVMReadOnlyAttribute
);
379 args
[1] = back_attr_number
;
380 back
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
381 input_type
, args
, 3, LLVMReadOnlyAttribute
);
383 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
384 LLVMBuildSelect(gallivm
->builder
,
393 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
394 LLVMValueRef args
[3];
395 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
396 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
398 args
[1] = attr_number
;
400 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
401 build_intrinsic(base
->gallivm
->builder
, intr_name
,
402 input_type
, args
, 3, LLVMReadOnlyAttribute
);
407 static void declare_input(
408 struct radeon_llvm_context
* radeon_bld
,
409 unsigned input_index
,
410 const struct tgsi_full_declaration
*decl
)
412 struct si_shader_context
* si_shader_ctx
=
413 si_shader_context(&radeon_bld
->soa
.bld_base
);
414 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
415 declare_input_vs(si_shader_ctx
, input_index
, decl
);
416 } else if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
417 declare_input_fs(si_shader_ctx
, input_index
, decl
);
419 fprintf(stderr
, "Warning: Unsupported shader type,\n");
423 static LLVMValueRef
fetch_constant(
424 struct lp_build_tgsi_context
* bld_base
,
425 const struct tgsi_full_src_register
*reg
,
426 enum tgsi_opcode_type type
,
429 struct lp_build_context
* base
= &bld_base
->base
;
432 LLVMValueRef const_ptr
;
436 if (swizzle
== LP_CHAN_ALL
) {
438 LLVMValueRef values
[4];
439 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
440 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
442 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
445 /* currently not supported */
446 if (reg
->Register
.Indirect
) {
448 load
= lp_build_const_int32(base
->gallivm
, 0);
449 return bitcast(bld_base
, type
, load
);
452 const_ptr
= use_sgpr(base
->gallivm
, SGPR_CONST_PTR_F32
, SI_SGPR_CONST
);
454 /* XXX: This assumes that the constant buffer is not packed, so
455 * CONST[0].x will have an offset of 0 and CONST[1].x will have an
457 idx
= (reg
->Register
.Index
* 4) + swizzle
;
459 /* index loads above 255 are currently not supported */
464 offset
= lp_build_const_int32(base
->gallivm
, idx
);
466 load
= build_indexed_load(base
->gallivm
, const_ptr
, offset
);
467 return bitcast(bld_base
, type
, load
);
470 /* Initialize arguments for the shader export intrinsic */
471 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
472 struct tgsi_full_declaration
*d
,
477 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
478 struct lp_build_context
*uint
=
479 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
480 struct lp_build_context
*base
= &bld_base
->base
;
481 unsigned compressed
= 0;
484 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
485 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
487 if (cbuf
>= 0 && cbuf
< 8) {
488 struct r600_context
*rctx
= si_shader_ctx
->rctx
;
489 compressed
= (si_shader_ctx
->key
.export_16bpc
>> cbuf
) & 0x1;
492 si_shader_ctx
->shader
->spi_shader_col_format
|=
493 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
495 si_shader_ctx
->shader
->spi_shader_col_format
|=
496 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
501 /* Pixel shader needs to pack output values before export */
502 for (chan
= 0; chan
< 2; chan
++ ) {
503 LLVMValueRef
*out_ptr
=
504 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
];
505 args
[0] = LLVMBuildLoad(base
->gallivm
->builder
,
506 out_ptr
[2 * chan
], "");
507 args
[1] = LLVMBuildLoad(base
->gallivm
->builder
,
508 out_ptr
[2 * chan
+ 1], "");
510 build_intrinsic(base
->gallivm
->builder
,
512 LLVMInt32TypeInContext(base
->gallivm
->context
),
514 LLVMReadNoneAttribute
);
515 args
[chan
+ 7] = args
[chan
+ 5] =
516 LLVMBuildBitCast(base
->gallivm
->builder
,
518 LLVMFloatTypeInContext(base
->gallivm
->context
),
525 for (chan
= 0; chan
< 4; chan
++ ) {
526 LLVMValueRef out_ptr
=
527 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][chan
];
528 /* +5 because the first output value will be
529 * the 6th argument to the intrinsic. */
530 args
[chan
+ 5] = LLVMBuildLoad(base
->gallivm
->builder
,
534 /* Clear COMPR flag */
535 args
[4] = uint
->zero
;
538 /* XXX: This controls which components of the output
539 * registers actually get exported. (e.g bit 0 means export
540 * X component, bit 1 means export Y component, etc.) I'm
541 * hard coding this to 0xf for now. In the future, we might
542 * want to do something else. */
543 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
545 /* Specify whether the EXEC mask represents the valid mask */
546 args
[1] = uint
->zero
;
548 /* Specify whether this is the last export */
549 args
[2] = uint
->zero
;
551 /* Specify the target we are exporting */
552 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
554 /* XXX: We probably need to keep track of the output
555 * values, so we know what we are passing to the next
559 static void si_llvm_emit_prologue(struct lp_build_tgsi_context
*bld_base
)
561 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
562 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
563 lp_build_intrinsic_unary(gallivm
->builder
,
564 "llvm.AMDGPU.shader.type",
565 LLVMVoidTypeInContext(gallivm
->context
),
566 lp_build_const_int32(gallivm
, si_shader_ctx
->type
));
570 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
573 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
574 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
576 if (si_shader_ctx
->key
.alpha_func
!= PIPE_FUNC_NEVER
) {
577 LLVMValueRef out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][3];
578 LLVMValueRef alpha_pass
=
579 lp_build_cmp(&bld_base
->base
,
580 si_shader_ctx
->key
.alpha_func
,
581 LLVMBuildLoad(gallivm
->builder
, out_ptr
, ""),
582 lp_build_const_float(gallivm
, si_shader_ctx
->key
.alpha_ref
));
584 lp_build_select(&bld_base
->base
,
586 lp_build_const_float(gallivm
, 1.0f
),
587 lp_build_const_float(gallivm
, -1.0f
));
589 build_intrinsic(gallivm
->builder
,
591 LLVMVoidTypeInContext(gallivm
->context
),
594 build_intrinsic(gallivm
->builder
,
596 LLVMVoidTypeInContext(gallivm
->context
),
601 /* XXX: This is partially implemented for VS only at this point. It is not complete */
602 static void si_llvm_emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
604 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
605 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
606 struct lp_build_context
* base
= &bld_base
->base
;
607 struct lp_build_context
* uint
=
608 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
609 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
610 LLVMValueRef args
[9];
611 LLVMValueRef last_args
[9] = { 0 };
612 unsigned color_count
= 0;
613 unsigned param_count
= 0;
614 int depth_index
= -1, stencil_index
= -1;
616 while (!tgsi_parse_end_of_tokens(parse
)) {
617 struct tgsi_full_declaration
*d
=
618 &parse
->FullToken
.FullDeclaration
;
623 tgsi_parse_token(parse
);
624 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
627 switch (d
->Declaration
.File
) {
628 case TGSI_FILE_INPUT
:
629 i
= shader
->ninput
++;
630 shader
->input
[i
].name
= d
->Semantic
.Name
;
631 shader
->input
[i
].sid
= d
->Semantic
.Index
;
632 shader
->input
[i
].interpolate
= d
->Interp
.Interpolate
;
633 shader
->input
[i
].centroid
= d
->Interp
.Centroid
;
636 case TGSI_FILE_OUTPUT
:
637 i
= shader
->noutput
++;
638 shader
->output
[i
].name
= d
->Semantic
.Name
;
639 shader
->output
[i
].sid
= d
->Semantic
.Index
;
640 shader
->output
[i
].interpolate
= d
->Interp
.Interpolate
;
647 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
648 /* Select the correct target */
649 switch(d
->Semantic
.Name
) {
650 case TGSI_SEMANTIC_PSIZE
:
651 target
= V_008DFC_SQ_EXP_POS
;
653 case TGSI_SEMANTIC_POSITION
:
654 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
655 target
= V_008DFC_SQ_EXP_POS
;
661 case TGSI_SEMANTIC_STENCIL
:
662 stencil_index
= index
;
664 case TGSI_SEMANTIC_COLOR
:
665 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
666 case TGSI_SEMANTIC_BCOLOR
:
667 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
668 shader
->output
[i
].param_offset
= param_count
;
671 target
= V_008DFC_SQ_EXP_MRT
+ color_count
;
672 if (color_count
== 0 &&
673 si_shader_ctx
->key
.alpha_func
!= PIPE_FUNC_ALWAYS
)
674 si_alpha_test(bld_base
, index
);
679 case TGSI_SEMANTIC_FOG
:
680 case TGSI_SEMANTIC_GENERIC
:
681 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
682 shader
->output
[i
].param_offset
= param_count
;
688 "Warning: SI unhandled output type:%d\n",
692 si_llvm_init_export_args(bld_base
, d
, index
, target
, args
);
694 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
?
695 (d
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) :
696 (d
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
)) {
698 lp_build_intrinsic(base
->gallivm
->builder
,
700 LLVMVoidTypeInContext(base
->gallivm
->context
),
704 memcpy(last_args
, args
, sizeof(args
));
706 lp_build_intrinsic(base
->gallivm
->builder
,
708 LLVMVoidTypeInContext(base
->gallivm
->context
),
715 if (depth_index
>= 0 || stencil_index
>= 0) {
716 LLVMValueRef out_ptr
;
719 /* Specify the target we are exporting */
720 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
722 if (depth_index
>= 0) {
723 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
724 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
727 if (stencil_index
< 0) {
734 if (stencil_index
>= 0) {
735 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
738 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
745 /* Specify which components to enable */
746 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
750 args
[4] = uint
->zero
;
753 lp_build_intrinsic(base
->gallivm
->builder
,
755 LLVMVoidTypeInContext(base
->gallivm
->context
),
758 memcpy(last_args
, args
, sizeof(args
));
762 assert(si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
764 /* Specify which components to enable */
765 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
767 /* Specify the target we are exporting */
768 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
770 /* Set COMPR flag to zero to export data as 32-bit */
771 last_args
[4] = uint
->zero
;
774 last_args
[5]= uint
->zero
;
775 last_args
[6]= uint
->zero
;
776 last_args
[7]= uint
->zero
;
777 last_args
[8]= uint
->zero
;
779 si_shader_ctx
->shader
->spi_shader_col_format
|=
780 V_028714_SPI_SHADER_32_ABGR
;
783 /* Specify whether the EXEC mask represents the valid mask */
784 last_args
[1] = lp_build_const_int32(base
->gallivm
,
785 si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
787 /* Specify that this is the last export */
788 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
790 lp_build_intrinsic(base
->gallivm
->builder
,
792 LLVMVoidTypeInContext(base
->gallivm
->context
),
795 /* XXX: Look up what this function does */
796 /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
799 static void tex_fetch_args(
800 struct lp_build_tgsi_context
* bld_base
,
801 struct lp_build_emit_data
* emit_data
)
803 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
804 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
805 unsigned opcode
= inst
->Instruction
.Opcode
;
806 unsigned target
= inst
->Texture
.Texture
;
809 LLVMValueRef coords
[4];
810 LLVMValueRef address
[16];
815 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
816 emit_data
->args
[0] = lp_build_const_int32(bld_base
->base
.gallivm
, 0xf);
818 /* Fetch and project texture coordinates */
819 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
820 for (chan
= 0; chan
< 3; chan
++ ) {
821 coords
[chan
] = lp_build_emit_fetch(bld_base
,
824 if (opcode
== TGSI_OPCODE_TXP
)
825 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
831 if (opcode
== TGSI_OPCODE_TXP
)
832 coords
[3] = bld_base
->base
.one
;
834 /* Pack LOD bias value */
835 if (opcode
== TGSI_OPCODE_TXB
)
836 address
[count
++] = coords
[3];
838 if ((target
== TGSI_TEXTURE_CUBE
|| target
== TGSI_TEXTURE_SHADOWCUBE
) &&
839 opcode
!= TGSI_OPCODE_TXQ
)
840 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
842 /* Pack depth comparison value */
844 case TGSI_TEXTURE_SHADOW1D
:
845 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
846 case TGSI_TEXTURE_SHADOW2D
:
847 case TGSI_TEXTURE_SHADOWRECT
:
848 address
[count
++] = coords
[2];
850 case TGSI_TEXTURE_SHADOWCUBE
:
851 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
852 address
[count
++] = coords
[3];
854 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
855 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
858 /* Pack texture coordinates */
859 address
[count
++] = coords
[0];
861 case TGSI_TEXTURE_2D
:
862 case TGSI_TEXTURE_2D_ARRAY
:
863 case TGSI_TEXTURE_3D
:
864 case TGSI_TEXTURE_CUBE
:
865 case TGSI_TEXTURE_RECT
:
866 case TGSI_TEXTURE_SHADOW2D
:
867 case TGSI_TEXTURE_SHADOWRECT
:
868 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
869 case TGSI_TEXTURE_SHADOWCUBE
:
870 case TGSI_TEXTURE_2D_MSAA
:
871 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
872 case TGSI_TEXTURE_CUBE_ARRAY
:
873 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
874 address
[count
++] = coords
[1];
877 case TGSI_TEXTURE_3D
:
878 case TGSI_TEXTURE_CUBE
:
879 case TGSI_TEXTURE_SHADOWCUBE
:
880 case TGSI_TEXTURE_CUBE_ARRAY
:
881 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
882 address
[count
++] = coords
[2];
885 /* Pack array slice */
887 case TGSI_TEXTURE_1D_ARRAY
:
888 address
[count
++] = coords
[1];
891 case TGSI_TEXTURE_2D_ARRAY
:
892 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
893 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
894 address
[count
++] = coords
[2];
897 case TGSI_TEXTURE_CUBE_ARRAY
:
898 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
899 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
900 address
[count
++] = coords
[3];
904 if (opcode
== TGSI_OPCODE_TXL
)
905 address
[count
++] = coords
[3];
908 assert(!"Cannot handle more than 16 texture address parameters");
912 for (chan
= 0; chan
< count
; chan
++ ) {
913 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
915 LLVMInt32TypeInContext(gallivm
->context
),
919 /* Pad to power of two vector */
920 while (count
< util_next_power_of_two(count
))
921 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
923 emit_data
->args
[1] = lp_build_gather_values(gallivm
, address
, count
);
926 ptr
= use_sgpr(bld_base
->base
.gallivm
, SGPR_CONST_PTR_V8I32
, SI_SGPR_RESOURCE
);
927 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
928 emit_data
->inst
->Src
[1].Register
.Index
);
929 emit_data
->args
[2] = build_indexed_load(bld_base
->base
.gallivm
,
933 ptr
= use_sgpr(bld_base
->base
.gallivm
, SGPR_CONST_PTR_V4I32
, SI_SGPR_SAMPLER
);
934 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
935 emit_data
->inst
->Src
[1].Register
.Index
);
936 emit_data
->args
[3] = build_indexed_load(bld_base
->base
.gallivm
,
940 emit_data
->args
[4] = lp_build_const_int32(bld_base
->base
.gallivm
, target
);
942 emit_data
->arg_count
= 5;
943 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
944 * the writemask are clear */
945 emit_data
->dst_type
= LLVMVectorType(
946 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
950 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
951 struct lp_build_tgsi_context
* bld_base
,
952 struct lp_build_emit_data
* emit_data
)
954 struct lp_build_context
* base
= &bld_base
->base
;
957 sprintf(intr_name
, "%sv%ui32", action
->intr_name
,
958 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[1])));
960 emit_data
->output
[emit_data
->chan
] = lp_build_intrinsic(
961 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
962 emit_data
->args
, emit_data
->arg_count
);
965 static const struct lp_build_tgsi_action tex_action
= {
966 .fetch_args
= tex_fetch_args
,
967 .emit
= build_tex_intrinsic
,
968 .intr_name
= "llvm.SI.sample."
971 static const struct lp_build_tgsi_action txb_action
= {
972 .fetch_args
= tex_fetch_args
,
973 .emit
= build_tex_intrinsic
,
974 .intr_name
= "llvm.SI.sampleb."
977 static const struct lp_build_tgsi_action txl_action
= {
978 .fetch_args
= tex_fetch_args
,
979 .emit
= build_tex_intrinsic
,
980 .intr_name
= "llvm.SI.samplel."
984 int si_pipe_shader_create(
985 struct pipe_context
*ctx
,
986 struct si_pipe_shader
*shader
,
987 struct si_shader_key key
)
989 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
990 struct si_pipe_shader_selector
*sel
= shader
->selector
;
991 struct si_shader_context si_shader_ctx
;
992 struct tgsi_shader_info shader_info
;
993 struct lp_build_tgsi_context
* bld_base
;
995 unsigned char * inst_bytes
;
996 unsigned inst_byte_count
;
1001 dump
= debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE
);
1003 assert(shader
->shader
.noutput
== 0);
1004 assert(shader
->shader
.ninterp
== 0);
1005 assert(shader
->shader
.ninput
== 0);
1007 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
1008 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
1009 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
1011 tgsi_scan_shader(sel
->tokens
, &shader_info
);
1012 if (shader_info
.indirect_files
!= 0) {
1013 fprintf(stderr
, "Indirect addressing not fully handled yet\n");
1017 shader
->shader
.uses_kill
= shader_info
.uses_kill
;
1018 bld_base
->info
= &shader_info
;
1019 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
1020 bld_base
->emit_prologue
= si_llvm_emit_prologue
;
1021 bld_base
->emit_epilogue
= si_llvm_emit_epilogue
;
1023 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
1024 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = txb_action
;
1025 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = txl_action
;
1026 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
1028 si_shader_ctx
.radeon_bld
.load_input
= declare_input
;
1029 si_shader_ctx
.tokens
= sel
->tokens
;
1030 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
1031 si_shader_ctx
.shader
= shader
;
1032 si_shader_ctx
.key
= key
;
1033 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
1034 si_shader_ctx
.rctx
= rctx
;
1036 shader
->shader
.nr_cbufs
= rctx
->framebuffer
.nr_cbufs
;
1038 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1039 * conversion fails. */
1041 tgsi_dump(sel
->tokens
, 0);
1044 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
1045 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
1049 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
1051 mod
= bld_base
->base
.gallivm
->module
;
1053 LLVMDumpModule(mod
);
1055 radeon_llvm_compile(mod
, &inst_bytes
, &inst_byte_count
, "SI", dump
);
1057 fprintf(stderr
, "SI CODE:\n");
1058 for (i
= 0; i
< inst_byte_count
; i
+=4 ) {
1059 fprintf(stderr
, "%02x%02x%02x%02x\n", inst_bytes
[i
+ 3],
1060 inst_bytes
[i
+ 2], inst_bytes
[i
+ 1],
1065 shader
->num_sgprs
= util_le32_to_cpu(*(uint32_t*)inst_bytes
);
1066 shader
->num_vgprs
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 4));
1067 shader
->spi_ps_input_ena
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 8));
1069 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
1070 tgsi_parse_free(&si_shader_ctx
.parse
);
1072 /* copy new shader */
1073 si_resource_reference(&shader
->bo
, NULL
);
1074 shader
->bo
= si_resource_create_custom(ctx
->screen
, PIPE_USAGE_IMMUTABLE
,
1075 inst_byte_count
- 12);
1076 if (shader
->bo
== NULL
) {
1080 ptr
= (uint32_t*)rctx
->ws
->buffer_map(shader
->bo
->cs_buf
, rctx
->cs
, PIPE_TRANSFER_WRITE
);
1081 if (0 /*R600_BIG_ENDIAN*/) {
1082 for (i
= 0; i
< (inst_byte_count
-12)/4; ++i
) {
1083 ptr
[i
] = util_bswap32(*(uint32_t*)(inst_bytes
+12 + i
*4));
1086 memcpy(ptr
, inst_bytes
+ 12, inst_byte_count
- 12);
1088 rctx
->ws
->buffer_unmap(shader
->bo
->cs_buf
);
1095 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
1097 si_resource_reference(&shader
->bo
, NULL
);