2 #include "gallivm/lp_bld_tgsi_action.h"
3 #include "gallivm/lp_bld_const.h"
4 #include "gallivm/lp_bld_intr.h"
5 #include "gallivm/lp_bld_tgsi.h"
6 #include "radeon_llvm.h"
7 #include "radeon_llvm_emit.h"
8 #include "tgsi/tgsi_info.h"
9 #include "tgsi/tgsi_parse.h"
10 #include "tgsi/tgsi_scan.h"
11 #include "tgsi/tgsi_dump.h"
13 #include "radeonsi_pipe.h"
14 #include "radeonsi_shader.h"
22 static ps_remap_inputs(
23 struct tgsi_llvm_context * tl_ctx,
32 struct list_head head;
40 struct si_shader_context
42 struct radeon_llvm_context radeon_bld
;
43 struct r600_context
*rctx
;
44 struct tgsi_parse_context parse
;
45 struct tgsi_token
* tokens
;
46 struct si_pipe_shader
*shader
;
47 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
48 /* unsigned num_inputs; */
49 /* struct list_head inputs; */
50 /* unsigned * input_mappings *//* From TGSI to SI hw */
51 /* struct tgsi_shader_info info;*/
54 static struct si_shader_context
* si_shader_context(
55 struct lp_build_tgsi_context
* bld_base
)
57 return (struct si_shader_context
*)bld_base
;
61 #define PERSPECTIVE_BASE 0
64 #define SAMPLE_OFFSET 0
65 #define CENTER_OFFSET 2
66 #define CENTROID_OFSET 4
68 #define USE_SGPR_MAX_SUFFIX_LEN 5
69 #define CONST_ADDR_SPACE 2
80 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
82 * @param offset The offset parameter specifies the number of
83 * elements to offset, not the number of bytes or dwords. An element is the
84 * the type pointed to by the base_ptr parameter (e.g. int is the element of
87 * When LLVM lowers the load instruction, it will convert the element offset
88 * into a dword offset automatically.
91 static LLVMValueRef
build_indexed_load(
92 struct gallivm_state
* gallivm
,
93 LLVMValueRef base_ptr
,
96 LLVMValueRef computed_ptr
= LLVMBuildGEP(
97 gallivm
->builder
, base_ptr
, &offset
, 1, "");
99 return LLVMBuildLoad(gallivm
->builder
, computed_ptr
, "");
103 * XXX: Instead of using an intrinsic to use a specific SGPR, we should be
104 * using load instructions. The loads should load from the USER_SGPR address
105 * space and use the sgpr index as the pointer.
107 static LLVMValueRef
use_sgpr(
108 struct gallivm_state
* gallivm
,
112 LLVMValueRef sgpr_index
;
113 LLVMTypeRef ret_type
;
115 sgpr_index
= lp_build_const_int32(gallivm
, sgpr
);
118 case SGPR_CONST_PTR_F32
:
119 ret_type
= LLVMFloatTypeInContext(gallivm
->context
);
120 ret_type
= LLVMPointerType(ret_type
, CONST_ADDR_SPACE
);
121 return lp_build_intrinsic_unary(gallivm
->builder
,
122 "llvm.SI.use.sgprptrcf32.",
123 ret_type
, sgpr_index
);
125 ret_type
= LLVMInt32TypeInContext(gallivm
->context
);
126 return lp_build_intrinsic_unary(gallivm
->builder
,
127 "llvm.SI.use.sgpr.i32",
128 ret_type
, sgpr_index
);
130 ret_type
= LLVMInt64TypeInContext(gallivm
->context
);
131 return lp_build_intrinsic_unary(gallivm
->builder
,
132 "llvm.SI.use.sgpr.i64",
133 ret_type
, sgpr_index
);
134 case SGPR_CONST_PTR_V4I32
:
135 ret_type
= LLVMInt32TypeInContext(gallivm
->context
);
136 ret_type
= LLVMVectorType(ret_type
, 4);
137 ret_type
= LLVMPointerType(ret_type
, CONST_ADDR_SPACE
);
138 return lp_build_intrinsic_unary(gallivm
->builder
,
139 "llvm.SI.use.sgprptrci128.",
140 ret_type
, sgpr_index
);
141 case SGPR_CONST_PTR_V8I32
:
142 ret_type
= LLVMInt32TypeInContext(gallivm
->context
);
143 ret_type
= LLVMVectorType(ret_type
, 8);
144 ret_type
= LLVMPointerType(ret_type
, CONST_ADDR_SPACE
);
145 return lp_build_intrinsic_unary(gallivm
->builder
,
146 "llvm.SI.use.sgprptrci256.",
147 ret_type
, sgpr_index
);
149 assert(!"Unsupported SGPR type in use_sgpr()");
154 static void declare_input_vs(
155 struct si_shader_context
* si_shader_ctx
,
156 unsigned input_index
,
157 const struct tgsi_full_declaration
*decl
)
159 LLVMValueRef t_list_ptr
;
160 LLVMValueRef t_offset
;
162 LLVMValueRef attribute_offset
;
163 LLVMValueRef buffer_index_reg
;
164 LLVMValueRef args
[3];
165 LLVMTypeRef vec4_type
;
167 struct lp_build_context
* uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
168 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
169 struct r600_context
*rctx
= si_shader_ctx
->rctx
;
170 struct pipe_vertex_element
*velem
= &rctx
->vertex_elements
->elements
[input_index
];
173 /* Load the T list */
174 /* XXX: Communicate with the rest of the driver about which SGPR the T#
175 * list pointer is going to be stored in. Hard code to SGPR[6:7] for
177 t_list_ptr
= use_sgpr(base
->gallivm
, SGPR_CONST_PTR_V4I32
, 3);
179 t_offset
= lp_build_const_int32(base
->gallivm
, velem
->vertex_buffer_index
);
181 t_list
= build_indexed_load(base
->gallivm
, t_list_ptr
, t_offset
);
183 /* Build the attribute offset */
184 attribute_offset
= lp_build_const_int32(base
->gallivm
, velem
->src_offset
);
186 /* Load the buffer index is always, which is always stored in VGPR0
187 * for Vertex Shaders */
188 buffer_index_reg
= lp_build_intrinsic(base
->gallivm
->builder
,
189 "llvm.SI.vs.load.buffer.index", uint
->elem_type
, NULL
, 0);
191 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
193 args
[1] = attribute_offset
;
194 args
[2] = buffer_index_reg
;
195 input
= lp_build_intrinsic(base
->gallivm
->builder
,
196 "llvm.SI.vs.load.input", vec4_type
, args
, 3);
198 /* Break up the vec4 into individual components */
199 for (chan
= 0; chan
< 4; chan
++) {
200 LLVMValueRef llvm_chan
= lp_build_const_int32(base
->gallivm
, chan
);
201 /* XXX: Use a helper function for this. There is one in
203 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
204 LLVMBuildExtractElement(base
->gallivm
->builder
,
205 input
, llvm_chan
, "");
209 static void declare_input_fs(
210 struct si_shader_context
* si_shader_ctx
,
211 unsigned input_index
,
212 const struct tgsi_full_declaration
*decl
)
214 const char * intr_name
;
216 struct lp_build_context
* base
=
217 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
218 struct gallivm_state
* gallivm
= base
->gallivm
;
221 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
222 * quad begins a new primitive. Bit 0 always needs
224 * [32:16] ParamOffset
227 /* XXX: This register number must be identical to the S_00B02C_USER_SGPR
228 * register field value
230 LLVMValueRef params
= use_sgpr(base
->gallivm
, SGPR_I32
, 6);
233 /* XXX: Is this the input_index? */
234 LLVMValueRef attr_number
= lp_build_const_int32(gallivm
, input_index
);
236 /* XXX: Handle all possible interpolation modes */
237 switch (decl
->Interp
.Interpolate
) {
238 case TGSI_INTERPOLATE_COLOR
:
239 if (si_shader_ctx
->rctx
->rasterizer
->flatshade
) {
240 intr_name
= "llvm.SI.fs.interp.constant";
242 if (decl
->Interp
.Centroid
)
243 intr_name
= "llvm.SI.fs.interp.persp.centroid";
245 intr_name
= "llvm.SI.fs.interp.persp.center";
248 case TGSI_INTERPOLATE_CONSTANT
:
249 intr_name
= "llvm.SI.fs.interp.constant";
251 case TGSI_INTERPOLATE_LINEAR
:
252 if (decl
->Interp
.Centroid
)
253 intr_name
= "llvm.SI.fs.interp.linear.centroid";
255 intr_name
= "llvm.SI.fs.interp.linear.center";
257 case TGSI_INTERPOLATE_PERSPECTIVE
:
258 if (decl
->Interp
.Centroid
)
259 intr_name
= "llvm.SI.fs.interp.persp.centroid";
261 intr_name
= "llvm.SI.fs.interp.persp.center";
264 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
268 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
269 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
270 LLVMValueRef args
[3];
271 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
272 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
273 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
275 args
[1] = attr_number
;
277 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
278 lp_build_intrinsic(gallivm
->builder
, intr_name
,
279 input_type
, args
, 3);
283 static void declare_input(
284 struct radeon_llvm_context
* radeon_bld
,
285 unsigned input_index
,
286 const struct tgsi_full_declaration
*decl
)
288 struct si_shader_context
* si_shader_ctx
=
289 si_shader_context(&radeon_bld
->soa
.bld_base
);
290 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
291 declare_input_vs(si_shader_ctx
, input_index
, decl
);
292 } else if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
293 declare_input_fs(si_shader_ctx
, input_index
, decl
);
295 fprintf(stderr
, "Warning: Unsupported shader type,\n");
299 static LLVMValueRef
fetch_constant(
300 struct lp_build_tgsi_context
* bld_base
,
301 const struct tgsi_full_src_register
*reg
,
302 enum tgsi_opcode_type type
,
305 struct lp_build_context
* base
= &bld_base
->base
;
307 LLVMValueRef const_ptr
;
310 /* XXX: Assume the pointer to the constant buffer is being stored in
312 const_ptr
= use_sgpr(base
->gallivm
, SGPR_CONST_PTR_F32
, 0);
314 /* XXX: This assumes that the constant buffer is not packed, so
315 * CONST[0].x will have an offset of 0 and CONST[1].x will have an
317 offset
= lp_build_const_int32(base
->gallivm
,
318 (reg
->Register
.Index
* 4) + swizzle
);
320 return build_indexed_load(base
->gallivm
, const_ptr
, offset
);
324 /* Declare some intrinsics with the correct attributes */
325 static void si_llvm_emit_prologue(struct lp_build_tgsi_context
* bld_base
)
327 LLVMValueRef function
;
328 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
330 LLVMTypeRef i64
= LLVMInt64TypeInContext(gallivm
->context
);
331 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
333 /* declare i32 @llvm.SI.use.sgpr.i32(i32) */
334 function
= lp_declare_intrinsic(gallivm
->module
, "llvm.SI.use.sgpr.i32",
336 LLVMAddFunctionAttr(function
, LLVMReadNoneAttribute
);
338 /* declare i64 @llvm.SI.use.sgpr.i64(i32) */
339 function
= lp_declare_intrinsic(gallivm
->module
, "llvm.SI.use.sgpr.i64",
341 LLVMAddFunctionAttr(function
, LLVMReadNoneAttribute
);
344 /* XXX: This is partially implemented for VS only at this point. It is not complete */
345 static void si_llvm_emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
347 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
348 struct r600_shader
* shader
= &si_shader_ctx
->shader
->shader
;
349 struct lp_build_context
* base
= &bld_base
->base
;
350 struct lp_build_context
* uint
=
351 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
352 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
353 LLVMValueRef last_args
[9] = { 0 };
355 while (!tgsi_parse_end_of_tokens(parse
)) {
356 /* XXX: component_bits controls which components of the output
357 * registers actually get exported. (e.g bit 0 means export
358 * X component, bit 1 means export Y component, etc.) I'm
359 * hard coding this to 0xf for now. In the future, we might
360 * want to do something else. */
361 unsigned component_bits
= 0xf;
363 struct tgsi_full_declaration
*d
=
364 &parse
->FullToken
.FullDeclaration
;
365 LLVMValueRef args
[9];
368 unsigned color_count
= 0;
369 unsigned param_count
= 0;
372 tgsi_parse_token(parse
);
373 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
376 switch (d
->Declaration
.File
) {
377 case TGSI_FILE_INPUT
:
378 i
= shader
->ninput
++;
379 shader
->input
[i
].name
= d
->Semantic
.Name
;
380 shader
->input
[i
].sid
= d
->Semantic
.Index
;
381 shader
->input
[i
].interpolate
= d
->Interp
.Interpolate
;
382 shader
->input
[i
].centroid
= d
->Interp
.Centroid
;
384 case TGSI_FILE_OUTPUT
:
385 i
= shader
->noutput
++;
386 shader
->output
[i
].name
= d
->Semantic
.Name
;
387 shader
->output
[i
].sid
= d
->Semantic
.Index
;
388 shader
->output
[i
].interpolate
= d
->Interp
.Interpolate
;
392 if (d
->Declaration
.File
!= TGSI_FILE_OUTPUT
)
395 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
396 for (chan
= 0; chan
< 4; chan
++ ) {
397 LLVMValueRef out_ptr
=
398 si_shader_ctx
->radeon_bld
.soa
.outputs
400 /* +5 because the first output value will be
401 * the 6th argument to the intrinsic. */
402 args
[chan
+ 5]= LLVMBuildLoad(
403 base
->gallivm
->builder
, out_ptr
, "");
406 /* XXX: We probably need to keep track of the output
407 * values, so we know what we are passing to the next
410 /* Select the correct target */
411 switch(d
->Semantic
.Name
) {
412 case TGSI_SEMANTIC_POSITION
:
413 target
= V_008DFC_SQ_EXP_POS
;
415 case TGSI_SEMANTIC_COLOR
:
416 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
417 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
418 shader
->output
[i
].param_offset
= param_count
;
421 target
= V_008DFC_SQ_EXP_MRT
+ color_count
;
425 case TGSI_SEMANTIC_GENERIC
:
426 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
427 shader
->output
[i
].param_offset
= param_count
;
433 "Warning: SI unhandled output type:%d\n",
437 /* Specify which components to enable */
438 args
[0] = lp_build_const_int32(base
->gallivm
,
441 /* Specify whether the EXEC mask represents the valid mask */
442 args
[1] = lp_build_const_int32(base
->gallivm
, 0);
444 /* Specify whether this is the last export */
445 args
[2] = lp_build_const_int32(base
->gallivm
, 0);
447 /* Specify the target we are exporting */
448 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
450 /* Set COMPR flag to zero to export data as 32-bit */
451 args
[4] = uint
->zero
;
453 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
?
454 (d
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) :
455 (d
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
)) {
457 lp_build_intrinsic(base
->gallivm
->builder
,
459 LLVMVoidTypeInContext(base
->gallivm
->context
),
463 memcpy(last_args
, args
, sizeof(args
));
465 lp_build_intrinsic(base
->gallivm
->builder
,
467 LLVMVoidTypeInContext(base
->gallivm
->context
),
474 /* Specify whether the EXEC mask represents the valid mask */
475 last_args
[1] = lp_build_const_int32(base
->gallivm
,
476 si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
478 /* Specify that this is the last export */
479 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
481 lp_build_intrinsic(base
->gallivm
->builder
,
483 LLVMVoidTypeInContext(base
->gallivm
->context
),
486 /* XXX: Look up what this function does */
487 /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
490 static void tex_fetch_args(
491 struct lp_build_tgsi_context
* bld_base
,
492 struct lp_build_emit_data
* emit_data
)
498 emit_data
->args
[0] = lp_build_const_int32(bld_base
->base
.gallivm
,
499 emit_data
->inst
->Dst
[0].Register
.WriteMask
);
502 /* XXX: Not all sample instructions need 4 address arguments. */
503 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
507 ptr
= use_sgpr(bld_base
->base
.gallivm
, SGPR_CONST_PTR_V8I32
, 2);
508 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
509 8 * emit_data
->inst
->Src
[1].Register
.Index
);
510 emit_data
->args
[2] = build_indexed_load(bld_base
->base
.gallivm
,
514 ptr
= use_sgpr(bld_base
->base
.gallivm
, SGPR_CONST_PTR_V4I32
, 1);
515 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
516 4 * emit_data
->inst
->Src
[1].Register
.Index
);
517 emit_data
->args
[3] = build_indexed_load(bld_base
->base
.gallivm
,
521 /* XXX: We might want to pass this information to the shader at some. */
522 /* emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm,
523 emit_data->inst->Texture.Texture);
526 emit_data
->arg_count
= 4;
527 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
528 * the writemask are clear */
529 emit_data
->dst_type
= LLVMVectorType(
530 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
534 static const struct lp_build_tgsi_action tex_action
= {
535 .fetch_args
= tex_fetch_args
,
536 .emit
= lp_build_tgsi_intrinsic
,
537 .intr_name
= "llvm.SI.sample"
541 int si_pipe_shader_create(
542 struct pipe_context
*ctx
,
543 struct si_pipe_shader
*shader
)
545 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
546 struct si_shader_context si_shader_ctx
;
547 struct tgsi_shader_info shader_info
;
548 struct lp_build_tgsi_context
* bld_base
;
550 unsigned char * inst_bytes
;
551 unsigned inst_byte_count
;
554 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
555 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
557 tgsi_scan_shader(shader
->tokens
, &shader_info
);
558 bld_base
->info
= &shader_info
;
559 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
560 bld_base
->emit_prologue
= si_llvm_emit_prologue
;
561 bld_base
->emit_epilogue
= si_llvm_emit_epilogue
;
563 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
565 si_shader_ctx
.radeon_bld
.load_input
= declare_input
;
566 si_shader_ctx
.tokens
= shader
->tokens
;
567 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
568 si_shader_ctx
.shader
= shader
;
569 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
570 si_shader_ctx
.rctx
= rctx
;
572 shader
->shader
.nr_cbufs
= rctx
->nr_cbufs
;
574 lp_build_tgsi_llvm(bld_base
, shader
->tokens
);
576 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
578 mod
= bld_base
->base
.gallivm
->module
;
579 tgsi_dump(shader
->tokens
, 0);
581 radeon_llvm_compile(mod
, &inst_bytes
, &inst_byte_count
, "SI", 1 /* dump */);
582 fprintf(stderr
, "SI CODE:\n");
583 for (i
= 0; i
< inst_byte_count
; i
+=4 ) {
584 fprintf(stderr
, "%02x%02x%02x%02x\n", inst_bytes
[i
+ 3],
585 inst_bytes
[i
+ 2], inst_bytes
[i
+ 1],
589 shader
->num_sgprs
= util_le32_to_cpu(*(uint32_t*)inst_bytes
);
590 shader
->num_vgprs
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 4));
591 shader
->spi_ps_input_ena
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 8));
593 tgsi_parse_free(&si_shader_ctx
.parse
);
595 /* copy new shader */
596 if (shader
->bo
== NULL
) {
599 shader
->bo
= (struct r600_resource
*)
600 pipe_buffer_create(ctx
->screen
, PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
, inst_byte_count
);
601 if (shader
->bo
== NULL
) {
604 ptr
= (uint32_t*)rctx
->ws
->buffer_map(shader
->bo
->cs_buf
, rctx
->cs
, PIPE_TRANSFER_WRITE
);
605 if (0 /*R600_BIG_ENDIAN*/) {
606 for (i
= 0; i
< (inst_byte_count
-12)/4; ++i
) {
607 ptr
[i
] = util_bswap32(*(uint32_t*)(inst_bytes
+12 + i
*4));
610 memcpy(ptr
, inst_bytes
+ 12, inst_byte_count
- 12);
612 rctx
->ws
->buffer_unmap(shader
->bo
->cs_buf
);
620 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
622 pipe_resource_reference((struct pipe_resource
**)&shader
->bo
, NULL
);
624 memset(&shader
->shader
,0,sizeof(struct r600_shader
));