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"
23 static ps_remap_inputs(
24 struct tgsi_llvm_context * tl_ctx,
33 struct list_head head;
41 struct si_shader_context
43 struct radeon_llvm_context radeon_bld
;
44 struct r600_context
*rctx
;
45 struct tgsi_parse_context parse
;
46 struct tgsi_token
* tokens
;
47 struct si_pipe_shader
*shader
;
48 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
49 /* unsigned num_inputs; */
50 /* struct list_head inputs; */
51 /* unsigned * input_mappings *//* From TGSI to SI hw */
52 /* struct tgsi_shader_info info;*/
55 static struct si_shader_context
* si_shader_context(
56 struct lp_build_tgsi_context
* bld_base
)
58 return (struct si_shader_context
*)bld_base
;
62 #define PERSPECTIVE_BASE 0
65 #define SAMPLE_OFFSET 0
66 #define CENTER_OFFSET 2
67 #define CENTROID_OFSET 4
69 #define USE_SGPR_MAX_SUFFIX_LEN 5
70 #define CONST_ADDR_SPACE 2
71 #define USER_SGPR_ADDR_SPACE 8
82 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
84 * @param offset The offset parameter specifies the number of
85 * elements to offset, not the number of bytes or dwords. An element is the
86 * the type pointed to by the base_ptr parameter (e.g. int is the element of
89 * When LLVM lowers the load instruction, it will convert the element offset
90 * into a dword offset automatically.
93 static LLVMValueRef
build_indexed_load(
94 struct gallivm_state
* gallivm
,
95 LLVMValueRef base_ptr
,
98 LLVMValueRef computed_ptr
= LLVMBuildGEP(
99 gallivm
->builder
, base_ptr
, &offset
, 1, "");
101 return LLVMBuildLoad(gallivm
->builder
, computed_ptr
, "");
105 * Load a value stored in one of the user SGPRs
107 * @param sgpr This is the sgpr to load the value from. If you need to load a
108 * value that is stored in consecutive SGPR registers (e.g. a 64-bit pointer),
109 * then you should pass the index of the first SGPR that holds the value. For
110 * example, if you want to load a pointer that is stored in SGPRs 2 and 3, then
111 * use pass 2 for the sgpr parameter.
113 * The value of the sgpr parameter must also be aligned to the width of the type
114 * being loaded, so that the sgpr parameter is divisible by the dword width of the
115 * type. For example, if the value being loaded is two dwords wide, then the sgpr
116 * parameter must be divisible by two.
118 static LLVMValueRef
use_sgpr(
119 struct gallivm_state
* gallivm
,
123 LLVMValueRef sgpr_index
;
124 LLVMTypeRef ret_type
;
127 sgpr_index
= lp_build_const_int32(gallivm
, sgpr
);
130 case SGPR_CONST_PTR_F32
:
131 assert(sgpr
% 2 == 0);
132 ret_type
= LLVMFloatTypeInContext(gallivm
->context
);
133 ret_type
= LLVMPointerType(ret_type
, CONST_ADDR_SPACE
);
137 ret_type
= LLVMInt32TypeInContext(gallivm
->context
);
141 assert(sgpr
% 2 == 0);
142 ret_type
= LLVMInt64TypeInContext(gallivm
->context
);
145 case SGPR_CONST_PTR_V4I32
:
146 assert(sgpr
% 2 == 0);
147 ret_type
= LLVMInt32TypeInContext(gallivm
->context
);
148 ret_type
= LLVMVectorType(ret_type
, 4);
149 ret_type
= LLVMPointerType(ret_type
, CONST_ADDR_SPACE
);
152 case SGPR_CONST_PTR_V8I32
:
153 assert(sgpr
% 2 == 0);
154 ret_type
= LLVMInt32TypeInContext(gallivm
->context
);
155 ret_type
= LLVMVectorType(ret_type
, 8);
156 ret_type
= LLVMPointerType(ret_type
, CONST_ADDR_SPACE
);
160 assert(!"Unsupported SGPR type in use_sgpr()");
164 ret_type
= LLVMPointerType(ret_type
, USER_SGPR_ADDR_SPACE
);
165 ptr
= LLVMBuildIntToPtr(gallivm
->builder
, sgpr_index
, ret_type
, "");
166 return LLVMBuildLoad(gallivm
->builder
, ptr
, "");
169 static void declare_input_vs(
170 struct si_shader_context
* si_shader_ctx
,
171 unsigned input_index
,
172 const struct tgsi_full_declaration
*decl
)
174 LLVMValueRef t_list_ptr
;
175 LLVMValueRef t_offset
;
177 LLVMValueRef attribute_offset
;
178 LLVMValueRef buffer_index_reg
;
179 LLVMValueRef args
[3];
180 LLVMTypeRef vec4_type
;
182 struct lp_build_context
* uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
183 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
184 struct r600_context
*rctx
= si_shader_ctx
->rctx
;
185 struct pipe_vertex_element
*velem
= &rctx
->vertex_elements
->elements
[input_index
];
188 /* Load the T list */
189 /* XXX: Communicate with the rest of the driver about which SGPR the T#
190 * list pointer is going to be stored in. Hard code to SGPR[6:7] for
192 t_list_ptr
= use_sgpr(base
->gallivm
, SGPR_CONST_PTR_V4I32
, 6);
194 t_offset
= lp_build_const_int32(base
->gallivm
, velem
->vertex_buffer_index
);
196 t_list
= build_indexed_load(base
->gallivm
, t_list_ptr
, t_offset
);
198 /* Build the attribute offset */
199 attribute_offset
= lp_build_const_int32(base
->gallivm
, velem
->src_offset
);
201 /* Load the buffer index is always, which is always stored in VGPR0
202 * for Vertex Shaders */
203 buffer_index_reg
= lp_build_intrinsic(base
->gallivm
->builder
,
204 "llvm.SI.vs.load.buffer.index", uint
->elem_type
, NULL
, 0);
206 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
208 args
[1] = attribute_offset
;
209 args
[2] = buffer_index_reg
;
210 input
= lp_build_intrinsic(base
->gallivm
->builder
,
211 "llvm.SI.vs.load.input", vec4_type
, args
, 3);
213 /* Break up the vec4 into individual components */
214 for (chan
= 0; chan
< 4; chan
++) {
215 LLVMValueRef llvm_chan
= lp_build_const_int32(base
->gallivm
, chan
);
216 /* XXX: Use a helper function for this. There is one in
218 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
219 LLVMBuildExtractElement(base
->gallivm
->builder
,
220 input
, llvm_chan
, "");
224 static void declare_input_fs(
225 struct si_shader_context
* si_shader_ctx
,
226 unsigned input_index
,
227 const struct tgsi_full_declaration
*decl
)
229 const char * intr_name
;
231 struct lp_build_context
* base
=
232 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
233 struct gallivm_state
* gallivm
= base
->gallivm
;
236 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
237 * quad begins a new primitive. Bit 0 always needs
239 * [32:16] ParamOffset
242 /* XXX: This register number must be identical to the S_00B02C_USER_SGPR
243 * register field value
245 LLVMValueRef params
= use_sgpr(base
->gallivm
, SGPR_I32
, 6);
248 /* XXX: Is this the input_index? */
249 LLVMValueRef attr_number
= lp_build_const_int32(gallivm
, input_index
);
251 /* XXX: Handle all possible interpolation modes */
252 switch (decl
->Interp
.Interpolate
) {
253 case TGSI_INTERPOLATE_COLOR
:
254 /* XXX: Flat shading hangs the GPU */
255 if (si_shader_ctx
->rctx
->queued
.named
.rasterizer
->flatshade
) {
257 intr_name
= "llvm.SI.fs.interp.constant";
259 intr_name
= "llvm.SI.fs.interp.linear.center";
262 if (decl
->Interp
.Centroid
)
263 intr_name
= "llvm.SI.fs.interp.persp.centroid";
265 intr_name
= "llvm.SI.fs.interp.persp.center";
268 case TGSI_INTERPOLATE_CONSTANT
:
269 /* XXX: Flat shading hangs the GPU */
271 intr_name
= "llvm.SI.fs.interp.constant";
274 case TGSI_INTERPOLATE_LINEAR
:
275 if (decl
->Interp
.Centroid
)
276 intr_name
= "llvm.SI.fs.interp.linear.centroid";
278 intr_name
= "llvm.SI.fs.interp.linear.center";
280 case TGSI_INTERPOLATE_PERSPECTIVE
:
281 if (decl
->Interp
.Centroid
)
282 intr_name
= "llvm.SI.fs.interp.persp.centroid";
284 intr_name
= "llvm.SI.fs.interp.persp.center";
287 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
291 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
292 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
293 LLVMValueRef args
[3];
294 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
295 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
296 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
298 args
[1] = attr_number
;
300 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
301 lp_build_intrinsic(gallivm
->builder
, intr_name
,
302 input_type
, args
, 3);
306 static void declare_input(
307 struct radeon_llvm_context
* radeon_bld
,
308 unsigned input_index
,
309 const struct tgsi_full_declaration
*decl
)
311 struct si_shader_context
* si_shader_ctx
=
312 si_shader_context(&radeon_bld
->soa
.bld_base
);
313 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
314 declare_input_vs(si_shader_ctx
, input_index
, decl
);
315 } else if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
316 declare_input_fs(si_shader_ctx
, input_index
, decl
);
318 fprintf(stderr
, "Warning: Unsupported shader type,\n");
322 static LLVMValueRef
fetch_constant(
323 struct lp_build_tgsi_context
* bld_base
,
324 const struct tgsi_full_src_register
*reg
,
325 enum tgsi_opcode_type type
,
328 struct lp_build_context
* base
= &bld_base
->base
;
330 LLVMValueRef const_ptr
;
334 /* XXX: Assume the pointer to the constant buffer is being stored in
336 const_ptr
= use_sgpr(base
->gallivm
, SGPR_CONST_PTR_F32
, 0);
338 /* XXX: This assumes that the constant buffer is not packed, so
339 * CONST[0].x will have an offset of 0 and CONST[1].x will have an
341 offset
= lp_build_const_int32(base
->gallivm
,
342 (reg
->Register
.Index
* 4) + swizzle
);
344 load
= build_indexed_load(base
->gallivm
, const_ptr
, offset
);
345 return bitcast(bld_base
, type
, load
);
348 /* XXX: This is partially implemented for VS only at this point. It is not complete */
349 static void si_llvm_emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
351 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
352 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
353 struct lp_build_context
* base
= &bld_base
->base
;
354 struct lp_build_context
* uint
=
355 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
356 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
357 LLVMValueRef last_args
[9] = { 0 };
359 while (!tgsi_parse_end_of_tokens(parse
)) {
360 /* XXX: component_bits controls which components of the output
361 * registers actually get exported. (e.g bit 0 means export
362 * X component, bit 1 means export Y component, etc.) I'm
363 * hard coding this to 0xf for now. In the future, we might
364 * want to do something else. */
365 unsigned component_bits
= 0xf;
367 struct tgsi_full_declaration
*d
=
368 &parse
->FullToken
.FullDeclaration
;
369 LLVMValueRef args
[9];
372 unsigned color_count
= 0;
373 unsigned param_count
= 0;
376 tgsi_parse_token(parse
);
377 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
380 switch (d
->Declaration
.File
) {
381 case TGSI_FILE_INPUT
:
382 i
= shader
->ninput
++;
383 shader
->input
[i
].name
= d
->Semantic
.Name
;
384 shader
->input
[i
].sid
= d
->Semantic
.Index
;
385 shader
->input
[i
].interpolate
= d
->Interp
.Interpolate
;
386 shader
->input
[i
].centroid
= d
->Interp
.Centroid
;
388 case TGSI_FILE_OUTPUT
:
389 i
= shader
->noutput
++;
390 shader
->output
[i
].name
= d
->Semantic
.Name
;
391 shader
->output
[i
].sid
= d
->Semantic
.Index
;
392 shader
->output
[i
].interpolate
= d
->Interp
.Interpolate
;
396 if (d
->Declaration
.File
!= TGSI_FILE_OUTPUT
)
399 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
400 for (chan
= 0; chan
< 4; chan
++ ) {
401 LLVMValueRef out_ptr
=
402 si_shader_ctx
->radeon_bld
.soa
.outputs
404 /* +5 because the first output value will be
405 * the 6th argument to the intrinsic. */
406 args
[chan
+ 5]= LLVMBuildLoad(
407 base
->gallivm
->builder
, out_ptr
, "");
410 /* XXX: We probably need to keep track of the output
411 * values, so we know what we are passing to the next
414 /* Select the correct target */
415 switch(d
->Semantic
.Name
) {
416 case TGSI_SEMANTIC_POSITION
:
417 target
= V_008DFC_SQ_EXP_POS
;
419 case TGSI_SEMANTIC_COLOR
:
420 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
421 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
422 shader
->output
[i
].param_offset
= param_count
;
425 target
= V_008DFC_SQ_EXP_MRT
+ color_count
;
429 case TGSI_SEMANTIC_GENERIC
:
430 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
431 shader
->output
[i
].param_offset
= param_count
;
437 "Warning: SI unhandled output type:%d\n",
441 /* Specify which components to enable */
442 args
[0] = lp_build_const_int32(base
->gallivm
,
445 /* Specify whether the EXEC mask represents the valid mask */
446 args
[1] = lp_build_const_int32(base
->gallivm
, 0);
448 /* Specify whether this is the last export */
449 args
[2] = lp_build_const_int32(base
->gallivm
, 0);
451 /* Specify the target we are exporting */
452 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
454 /* Set COMPR flag to zero to export data as 32-bit */
455 args
[4] = uint
->zero
;
457 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
?
458 (d
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) :
459 (d
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
)) {
461 lp_build_intrinsic(base
->gallivm
->builder
,
463 LLVMVoidTypeInContext(base
->gallivm
->context
),
467 memcpy(last_args
, args
, sizeof(args
));
469 lp_build_intrinsic(base
->gallivm
->builder
,
471 LLVMVoidTypeInContext(base
->gallivm
->context
),
478 /* Specify whether the EXEC mask represents the valid mask */
479 last_args
[1] = lp_build_const_int32(base
->gallivm
,
480 si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
482 /* Specify that this is the last export */
483 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
485 lp_build_intrinsic(base
->gallivm
->builder
,
487 LLVMVoidTypeInContext(base
->gallivm
->context
),
490 /* XXX: Look up what this function does */
491 /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
494 static void tex_fetch_args(
495 struct lp_build_tgsi_context
* bld_base
,
496 struct lp_build_emit_data
* emit_data
)
502 emit_data
->args
[0] = lp_build_const_int32(bld_base
->base
.gallivm
,
503 emit_data
->inst
->Dst
[0].Register
.WriteMask
);
506 /* XXX: Not all sample instructions need 4 address arguments. */
507 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
511 ptr
= use_sgpr(bld_base
->base
.gallivm
, SGPR_CONST_PTR_V8I32
, 4);
512 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
513 8 * emit_data
->inst
->Src
[1].Register
.Index
);
514 emit_data
->args
[2] = build_indexed_load(bld_base
->base
.gallivm
,
518 ptr
= use_sgpr(bld_base
->base
.gallivm
, SGPR_CONST_PTR_V4I32
, 2);
519 offset
= lp_build_const_int32(bld_base
->base
.gallivm
,
520 4 * emit_data
->inst
->Src
[1].Register
.Index
);
521 emit_data
->args
[3] = build_indexed_load(bld_base
->base
.gallivm
,
525 /* XXX: We might want to pass this information to the shader at some. */
526 /* emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm,
527 emit_data->inst->Texture.Texture);
530 emit_data
->arg_count
= 4;
531 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
532 * the writemask are clear */
533 emit_data
->dst_type
= LLVMVectorType(
534 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
538 static const struct lp_build_tgsi_action tex_action
= {
539 .fetch_args
= tex_fetch_args
,
540 .emit
= lp_build_tgsi_intrinsic
,
541 .intr_name
= "llvm.SI.sample"
545 int si_pipe_shader_create(
546 struct pipe_context
*ctx
,
547 struct si_pipe_shader
*shader
)
549 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
550 struct si_shader_context si_shader_ctx
;
551 struct tgsi_shader_info shader_info
;
552 struct lp_build_tgsi_context
* bld_base
;
554 unsigned char * inst_bytes
;
555 unsigned inst_byte_count
;
559 dump
= debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE
);
561 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
562 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
564 tgsi_scan_shader(shader
->tokens
, &shader_info
);
565 bld_base
->info
= &shader_info
;
566 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
567 bld_base
->emit_epilogue
= si_llvm_emit_epilogue
;
569 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
571 si_shader_ctx
.radeon_bld
.load_input
= declare_input
;
572 si_shader_ctx
.tokens
= shader
->tokens
;
573 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
574 si_shader_ctx
.shader
= shader
;
575 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
576 si_shader_ctx
.rctx
= rctx
;
578 shader
->shader
.nr_cbufs
= rctx
->framebuffer
.nr_cbufs
;
580 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
581 * conversion fails. */
583 tgsi_dump(shader
->tokens
, 0);
586 lp_build_tgsi_llvm(bld_base
, shader
->tokens
);
588 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
590 mod
= bld_base
->base
.gallivm
->module
;
594 radeon_llvm_compile(mod
, &inst_bytes
, &inst_byte_count
, "SI", dump
);
596 fprintf(stderr
, "SI CODE:\n");
597 for (i
= 0; i
< inst_byte_count
; i
+=4 ) {
598 fprintf(stderr
, "%02x%02x%02x%02x\n", inst_bytes
[i
+ 3],
599 inst_bytes
[i
+ 2], inst_bytes
[i
+ 1],
604 shader
->num_sgprs
= util_le32_to_cpu(*(uint32_t*)inst_bytes
);
605 shader
->num_vgprs
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 4));
606 shader
->spi_ps_input_ena
= util_le32_to_cpu(*(uint32_t*)(inst_bytes
+ 8));
608 tgsi_parse_free(&si_shader_ctx
.parse
);
610 /* copy new shader */
611 if (shader
->bo
== NULL
) {
614 shader
->bo
= (struct r600_resource
*)
615 pipe_buffer_create(ctx
->screen
, PIPE_BIND_CUSTOM
, PIPE_USAGE_IMMUTABLE
, inst_byte_count
);
616 if (shader
->bo
== NULL
) {
619 ptr
= (uint32_t*)rctx
->ws
->buffer_map(shader
->bo
->cs_buf
, rctx
->cs
, PIPE_TRANSFER_WRITE
);
620 if (0 /*R600_BIG_ENDIAN*/) {
621 for (i
= 0; i
< (inst_byte_count
-12)/4; ++i
) {
622 ptr
[i
] = util_bswap32(*(uint32_t*)(inst_bytes
+12 + i
*4));
625 memcpy(ptr
, inst_bytes
+ 12, inst_byte_count
- 12);
627 rctx
->ws
->buffer_unmap(shader
->bo
->cs_buf
);
635 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
637 pipe_resource_reference((struct pipe_resource
**)&shader
->bo
, NULL
);
639 memset(&shader
->shader
,0,sizeof(struct si_shader
));