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 "util/u_memory.h"
40 #include "tgsi/tgsi_info.h"
41 #include "tgsi/tgsi_parse.h"
42 #include "tgsi/tgsi_scan.h"
43 #include "tgsi/tgsi_dump.h"
45 #include "radeonsi_pipe.h"
46 #include "radeonsi_shader.h"
54 struct si_shader_context
56 struct radeon_llvm_context radeon_bld
;
57 struct tgsi_parse_context parse
;
58 struct tgsi_token
* tokens
;
59 struct si_pipe_shader
*shader
;
60 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
61 LLVMValueRef const_md
;
62 LLVMValueRef const_resource
;
63 LLVMValueRef
*constants
;
64 LLVMValueRef
*resources
;
65 LLVMValueRef
*samplers
;
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 si_shader_context
* si_shader_ctx
,
100 LLVMValueRef base_ptr
,
103 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
105 LLVMValueRef computed_ptr
= LLVMBuildGEP(
106 base
->gallivm
->builder
, base_ptr
, &offset
, 1, "");
108 LLVMValueRef result
= LLVMBuildLoad(base
->gallivm
->builder
, computed_ptr
, "");
109 LLVMSetMetadata(result
, 1, si_shader_ctx
->const_md
);
113 static LLVMValueRef
get_instance_index(
114 struct radeon_llvm_context
* radeon_bld
,
117 struct gallivm_state
* gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
119 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_INSTANCE_ID
);
120 result
= LLVMBuildAdd(gallivm
->builder
, result
, LLVMGetParam(
121 radeon_bld
->main_fn
, SI_PARAM_START_INSTANCE
), "");
124 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
125 lp_build_const_int32(gallivm
, divisor
), "");
130 static void declare_input_vs(
131 struct si_shader_context
* si_shader_ctx
,
132 unsigned input_index
,
133 const struct tgsi_full_declaration
*decl
)
135 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
136 unsigned divisor
= si_shader_ctx
->shader
->key
.vs
.instance_divisors
[input_index
];
140 LLVMValueRef t_list_ptr
;
141 LLVMValueRef t_offset
;
143 LLVMValueRef attribute_offset
;
144 LLVMValueRef buffer_index
;
145 LLVMValueRef args
[3];
146 LLVMTypeRef vec4_type
;
149 /* Load the T list */
150 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFER
);
152 t_offset
= lp_build_const_int32(base
->gallivm
, input_index
);
154 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
, t_offset
);
156 /* Build the attribute offset */
157 attribute_offset
= lp_build_const_int32(base
->gallivm
, 0);
160 /* Build index from instance ID, start instance and divisor */
161 si_shader_ctx
->shader
->shader
.uses_instanceid
= true;
162 buffer_index
= get_instance_index(&si_shader_ctx
->radeon_bld
, divisor
);
164 /* Load the buffer index, which is always stored in VGPR0
165 * for Vertex Shaders */
166 buffer_index
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_ID
);
169 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
171 args
[1] = attribute_offset
;
172 args
[2] = buffer_index
;
173 input
= build_intrinsic(base
->gallivm
->builder
,
174 "llvm.SI.vs.load.input", vec4_type
, args
, 3,
175 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
177 /* Break up the vec4 into individual components */
178 for (chan
= 0; chan
< 4; chan
++) {
179 LLVMValueRef llvm_chan
= lp_build_const_int32(base
->gallivm
, chan
);
180 /* XXX: Use a helper function for this. There is one in
182 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
183 LLVMBuildExtractElement(base
->gallivm
->builder
,
184 input
, llvm_chan
, "");
188 static void declare_input_fs(
189 struct si_shader_context
* si_shader_ctx
,
190 unsigned input_index
,
191 const struct tgsi_full_declaration
*decl
)
193 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
194 struct lp_build_context
* base
=
195 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
196 struct gallivm_state
* gallivm
= base
->gallivm
;
197 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
198 LLVMValueRef main_fn
= si_shader_ctx
->radeon_bld
.main_fn
;
200 LLVMValueRef interp_param
;
201 const char * intr_name
;
204 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
205 * quad begins a new primitive. Bit 0 always needs
207 * [32:16] ParamOffset
210 LLVMValueRef params
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
211 LLVMValueRef attr_number
;
215 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
216 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
218 radeon_llvm_reg_index_soa(input_index
, chan
);
219 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
220 LLVMGetParam(main_fn
, SI_PARAM_POS_X_FLOAT
+ chan
);
223 /* RCP for fragcoord.w */
224 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
225 LLVMBuildFDiv(gallivm
->builder
,
226 lp_build_const_float(gallivm
, 1.0f
),
227 si_shader_ctx
->radeon_bld
.inputs
[soa_index
],
233 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
234 LLVMValueRef face
, is_face_positive
;
236 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
238 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
240 lp_build_const_float(gallivm
, 0.0f
),
243 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
244 LLVMBuildSelect(gallivm
->builder
,
246 lp_build_const_float(gallivm
, 1.0f
),
247 lp_build_const_float(gallivm
, 0.0f
),
249 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
250 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
251 lp_build_const_float(gallivm
, 0.0f
);
252 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
253 lp_build_const_float(gallivm
, 1.0f
);
258 shader
->input
[input_index
].param_offset
= shader
->ninterp
++;
259 attr_number
= lp_build_const_int32(gallivm
,
260 shader
->input
[input_index
].param_offset
);
262 /* XXX: Handle all possible interpolation modes */
263 switch (decl
->Interp
.Interpolate
) {
264 case TGSI_INTERPOLATE_COLOR
:
265 if (si_shader_ctx
->shader
->key
.ps
.flatshade
) {
268 if (decl
->Interp
.Centroid
)
269 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
271 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
274 case TGSI_INTERPOLATE_CONSTANT
:
277 case TGSI_INTERPOLATE_LINEAR
:
278 if (decl
->Interp
.Centroid
)
279 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTROID
);
281 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTER
);
283 case TGSI_INTERPOLATE_PERSPECTIVE
:
284 if (decl
->Interp
.Centroid
)
285 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
287 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
290 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
294 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
296 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
297 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
298 si_shader_ctx
->shader
->key
.ps
.color_two_side
) {
299 LLVMValueRef args
[4];
300 LLVMValueRef face
, is_face_positive
;
301 LLVMValueRef back_attr_number
=
302 lp_build_const_int32(gallivm
,
303 shader
->input
[input_index
].param_offset
+ 1);
305 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
307 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
309 lp_build_const_float(gallivm
, 0.0f
),
313 args
[3] = interp_param
;
314 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
315 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
316 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
317 LLVMValueRef front
, back
;
320 args
[1] = attr_number
;
321 front
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
322 input_type
, args
, args
[3] ? 4 : 3,
323 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
325 args
[1] = back_attr_number
;
326 back
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
327 input_type
, args
, args
[3] ? 4 : 3,
328 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
330 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
331 LLVMBuildSelect(gallivm
->builder
,
340 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
341 LLVMValueRef args
[4];
342 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
343 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
345 args
[1] = attr_number
;
347 args
[3] = interp_param
;
348 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
349 build_intrinsic(base
->gallivm
->builder
, intr_name
,
350 input_type
, args
, args
[3] ? 4 : 3,
351 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
356 static void declare_input(
357 struct radeon_llvm_context
* radeon_bld
,
358 unsigned input_index
,
359 const struct tgsi_full_declaration
*decl
)
361 struct si_shader_context
* si_shader_ctx
=
362 si_shader_context(&radeon_bld
->soa
.bld_base
);
363 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
364 declare_input_vs(si_shader_ctx
, input_index
, decl
);
365 } else if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
366 declare_input_fs(si_shader_ctx
, input_index
, decl
);
368 fprintf(stderr
, "Warning: Unsupported shader type,\n");
372 static void declare_system_value(
373 struct radeon_llvm_context
* radeon_bld
,
375 const struct tgsi_full_declaration
*decl
)
378 LLVMValueRef value
= 0;
380 switch (decl
->Semantic
.Name
) {
381 case TGSI_SEMANTIC_INSTANCEID
:
382 value
= get_instance_index(radeon_bld
, 1);
385 case TGSI_SEMANTIC_VERTEXID
:
386 value
= LLVMGetParam(radeon_bld
->main_fn
, SI_PARAM_VERTEX_ID
);
390 assert(!"unknown system value");
394 radeon_bld
->system_values
[index
] = value
;
397 static LLVMValueRef
fetch_constant(
398 struct lp_build_tgsi_context
* bld_base
,
399 const struct tgsi_full_src_register
*reg
,
400 enum tgsi_opcode_type type
,
403 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
404 struct lp_build_context
* base
= &bld_base
->base
;
405 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
408 LLVMValueRef args
[2];
412 if (swizzle
== LP_CHAN_ALL
) {
414 LLVMValueRef values
[4];
415 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
416 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
418 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
421 idx
= reg
->Register
.Index
* 4 + swizzle
;
422 if (!reg
->Register
.Indirect
)
423 return bitcast(bld_base
, type
, si_shader_ctx
->constants
[idx
]);
425 args
[0] = si_shader_ctx
->const_resource
;
426 args
[1] = lp_build_const_int32(base
->gallivm
, idx
* 4);
427 addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
428 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
429 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
430 args
[1] = lp_build_add(&bld_base
->uint_bld
, addr
, args
[1]);
432 result
= build_intrinsic(base
->gallivm
->builder
, "llvm.SI.load.const", base
->elem_type
,
433 args
, 2, LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
435 return bitcast(bld_base
, type
, result
);
438 /* Initialize arguments for the shader export intrinsic */
439 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
440 struct tgsi_full_declaration
*d
,
445 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
446 struct lp_build_context
*uint
=
447 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
448 struct lp_build_context
*base
= &bld_base
->base
;
449 unsigned compressed
= 0;
452 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
453 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
455 if (cbuf
>= 0 && cbuf
< 8) {
456 compressed
= (si_shader_ctx
->shader
->key
.ps
.export_16bpc
>> cbuf
) & 0x1;
459 si_shader_ctx
->shader
->spi_shader_col_format
|=
460 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
462 si_shader_ctx
->shader
->spi_shader_col_format
|=
463 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
468 /* Pixel shader needs to pack output values before export */
469 for (chan
= 0; chan
< 2; chan
++ ) {
470 LLVMValueRef
*out_ptr
=
471 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
];
472 args
[0] = LLVMBuildLoad(base
->gallivm
->builder
,
473 out_ptr
[2 * chan
], "");
474 args
[1] = LLVMBuildLoad(base
->gallivm
->builder
,
475 out_ptr
[2 * chan
+ 1], "");
477 build_intrinsic(base
->gallivm
->builder
,
479 LLVMInt32TypeInContext(base
->gallivm
->context
),
481 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
482 args
[chan
+ 7] = args
[chan
+ 5] =
483 LLVMBuildBitCast(base
->gallivm
->builder
,
485 LLVMFloatTypeInContext(base
->gallivm
->context
),
492 for (chan
= 0; chan
< 4; chan
++ ) {
493 LLVMValueRef out_ptr
=
494 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][chan
];
495 /* +5 because the first output value will be
496 * the 6th argument to the intrinsic. */
497 args
[chan
+ 5] = LLVMBuildLoad(base
->gallivm
->builder
,
501 /* Clear COMPR flag */
502 args
[4] = uint
->zero
;
505 /* XXX: This controls which components of the output
506 * registers actually get exported. (e.g bit 0 means export
507 * X component, bit 1 means export Y component, etc.) I'm
508 * hard coding this to 0xf for now. In the future, we might
509 * want to do something else. */
510 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
512 /* Specify whether the EXEC mask represents the valid mask */
513 args
[1] = uint
->zero
;
515 /* Specify whether this is the last export */
516 args
[2] = uint
->zero
;
518 /* Specify the target we are exporting */
519 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
521 /* XXX: We probably need to keep track of the output
522 * values, so we know what we are passing to the next
526 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
529 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
530 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
532 if (si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_NEVER
) {
533 LLVMValueRef out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][3];
534 LLVMValueRef alpha_pass
=
535 lp_build_cmp(&bld_base
->base
,
536 si_shader_ctx
->shader
->key
.ps
.alpha_func
,
537 LLVMBuildLoad(gallivm
->builder
, out_ptr
, ""),
538 lp_build_const_float(gallivm
, si_shader_ctx
->shader
->key
.ps
.alpha_ref
));
540 lp_build_select(&bld_base
->base
,
542 lp_build_const_float(gallivm
, 1.0f
),
543 lp_build_const_float(gallivm
, -1.0f
));
545 build_intrinsic(gallivm
->builder
,
547 LLVMVoidTypeInContext(gallivm
->context
),
550 build_intrinsic(gallivm
->builder
,
552 LLVMVoidTypeInContext(gallivm
->context
),
557 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
* bld_base
,
560 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
561 struct lp_build_context
*base
= &bld_base
->base
;
562 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
563 LLVMValueRef args
[9];
567 LLVMValueRef out_elts
[4];
568 LLVMValueRef base_elt
;
569 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
570 LLVMValueRef const_resource
= build_indexed_load(si_shader_ctx
, ptr
, uint
->one
);
572 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
573 LLVMValueRef out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][chan
];
574 out_elts
[chan
] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
577 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
581 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
583 /* Compute dot products of position and user clip plane vectors */
584 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
585 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
586 args
[0] = const_resource
;
587 args
[1] = lp_build_const_int32(base
->gallivm
,
588 ((reg_index
* 4 + chan
) * 4 +
590 base_elt
= build_intrinsic(base
->gallivm
->builder
,
591 "llvm.SI.load.const",
594 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
596 lp_build_add(base
, args
[5 + chan
],
597 lp_build_mul(base
, base_elt
,
598 out_elts
[const_chan
]));
602 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
603 args
[1] = uint
->zero
;
604 args
[2] = uint
->zero
;
605 args
[3] = lp_build_const_int32(base
->gallivm
,
606 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
607 args
[4] = uint
->zero
;
608 lp_build_intrinsic(base
->gallivm
->builder
,
610 LLVMVoidTypeInContext(base
->gallivm
->context
),
615 /* XXX: This is partially implemented for VS only at this point. It is not complete */
616 static void si_llvm_emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
618 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
619 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
620 struct lp_build_context
* base
= &bld_base
->base
;
621 struct lp_build_context
* uint
=
622 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
623 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
624 LLVMValueRef args
[9];
625 LLVMValueRef last_args
[9] = { 0 };
626 unsigned color_count
= 0;
627 unsigned param_count
= 0;
628 int depth_index
= -1, stencil_index
= -1;
630 while (!tgsi_parse_end_of_tokens(parse
)) {
631 struct tgsi_full_declaration
*d
=
632 &parse
->FullToken
.FullDeclaration
;
637 tgsi_parse_token(parse
);
639 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_PROPERTY
&&
640 parse
->FullToken
.FullProperty
.Property
.PropertyName
==
641 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
)
642 shader
->fs_write_all
= TRUE
;
644 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
647 switch (d
->Declaration
.File
) {
648 case TGSI_FILE_INPUT
:
649 i
= shader
->ninput
++;
650 assert(i
< Elements(shader
->input
));
651 shader
->input
[i
].name
= d
->Semantic
.Name
;
652 shader
->input
[i
].sid
= d
->Semantic
.Index
;
653 shader
->input
[i
].interpolate
= d
->Interp
.Interpolate
;
654 shader
->input
[i
].centroid
= d
->Interp
.Centroid
;
657 case TGSI_FILE_OUTPUT
:
658 i
= shader
->noutput
++;
659 assert(i
< Elements(shader
->output
));
660 shader
->output
[i
].name
= d
->Semantic
.Name
;
661 shader
->output
[i
].sid
= d
->Semantic
.Index
;
662 shader
->output
[i
].interpolate
= d
->Interp
.Interpolate
;
669 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
670 /* Select the correct target */
671 switch(d
->Semantic
.Name
) {
672 case TGSI_SEMANTIC_PSIZE
:
673 shader
->vs_out_misc_write
= 1;
674 shader
->vs_out_point_size
= 1;
675 target
= V_008DFC_SQ_EXP_POS
+ 1;
677 case TGSI_SEMANTIC_POSITION
:
678 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
679 target
= V_008DFC_SQ_EXP_POS
;
685 case TGSI_SEMANTIC_STENCIL
:
686 stencil_index
= index
;
688 case TGSI_SEMANTIC_COLOR
:
689 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
690 case TGSI_SEMANTIC_BCOLOR
:
691 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
692 shader
->output
[i
].param_offset
= param_count
;
695 target
= V_008DFC_SQ_EXP_MRT
+ color_count
;
696 if (color_count
== 0 &&
697 si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_ALWAYS
)
698 si_alpha_test(bld_base
, index
);
703 case TGSI_SEMANTIC_CLIPVERTEX
:
704 si_llvm_emit_clipvertex(bld_base
, index
);
705 shader
->clip_dist_write
= 0xFF;
707 case TGSI_SEMANTIC_FOG
:
708 case TGSI_SEMANTIC_GENERIC
:
709 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
710 shader
->output
[i
].param_offset
= param_count
;
716 "Warning: SI unhandled output type:%d\n",
720 si_llvm_init_export_args(bld_base
, d
, index
, target
, args
);
722 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
?
723 (d
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) :
724 (d
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
)) {
726 lp_build_intrinsic(base
->gallivm
->builder
,
728 LLVMVoidTypeInContext(base
->gallivm
->context
),
732 memcpy(last_args
, args
, sizeof(args
));
734 lp_build_intrinsic(base
->gallivm
->builder
,
736 LLVMVoidTypeInContext(base
->gallivm
->context
),
743 if (depth_index
>= 0 || stencil_index
>= 0) {
744 LLVMValueRef out_ptr
;
747 /* Specify the target we are exporting */
748 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
750 if (depth_index
>= 0) {
751 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
752 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
755 if (stencil_index
< 0) {
762 if (stencil_index
>= 0) {
763 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
766 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
773 /* Specify which components to enable */
774 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
778 args
[4] = uint
->zero
;
781 lp_build_intrinsic(base
->gallivm
->builder
,
783 LLVMVoidTypeInContext(base
->gallivm
->context
),
786 memcpy(last_args
, args
, sizeof(args
));
790 assert(si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
792 /* Specify which components to enable */
793 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
795 /* Specify the target we are exporting */
796 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
798 /* Set COMPR flag to zero to export data as 32-bit */
799 last_args
[4] = uint
->zero
;
802 last_args
[5]= uint
->zero
;
803 last_args
[6]= uint
->zero
;
804 last_args
[7]= uint
->zero
;
805 last_args
[8]= uint
->zero
;
807 si_shader_ctx
->shader
->spi_shader_col_format
|=
808 V_028714_SPI_SHADER_32_ABGR
;
811 /* Specify whether the EXEC mask represents the valid mask */
812 last_args
[1] = lp_build_const_int32(base
->gallivm
,
813 si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
);
815 if (shader
->fs_write_all
&& shader
->nr_cbufs
> 1) {
818 /* Specify that this is not yet the last export */
819 last_args
[2] = lp_build_const_int32(base
->gallivm
, 0);
821 for (i
= 1; i
< shader
->nr_cbufs
; i
++) {
822 /* Specify the target we are exporting */
823 last_args
[3] = lp_build_const_int32(base
->gallivm
,
824 V_008DFC_SQ_EXP_MRT
+ i
);
826 lp_build_intrinsic(base
->gallivm
->builder
,
828 LLVMVoidTypeInContext(base
->gallivm
->context
),
831 si_shader_ctx
->shader
->spi_shader_col_format
|=
832 si_shader_ctx
->shader
->spi_shader_col_format
<< 4;
835 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
838 /* Specify that this is the last export */
839 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
841 lp_build_intrinsic(base
->gallivm
->builder
,
843 LLVMVoidTypeInContext(base
->gallivm
->context
),
846 /* XXX: Look up what this function does */
847 /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
850 static void tex_fetch_args(
851 struct lp_build_tgsi_context
* bld_base
,
852 struct lp_build_emit_data
* emit_data
)
854 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
855 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
856 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
857 unsigned opcode
= inst
->Instruction
.Opcode
;
858 unsigned target
= inst
->Texture
.Texture
;
859 LLVMValueRef coords
[4];
860 LLVMValueRef address
[16];
864 /* Fetch and project texture coordinates */
865 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
866 for (chan
= 0; chan
< 3; chan
++ ) {
867 coords
[chan
] = lp_build_emit_fetch(bld_base
,
870 if (opcode
== TGSI_OPCODE_TXP
)
871 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
877 if (opcode
== TGSI_OPCODE_TXP
)
878 coords
[3] = bld_base
->base
.one
;
880 /* Pack LOD bias value */
881 if (opcode
== TGSI_OPCODE_TXB
)
882 address
[count
++] = coords
[3];
884 if ((target
== TGSI_TEXTURE_CUBE
|| target
== TGSI_TEXTURE_SHADOWCUBE
) &&
885 opcode
!= TGSI_OPCODE_TXQ
)
886 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
888 /* Pack depth comparison value */
890 case TGSI_TEXTURE_SHADOW1D
:
891 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
892 case TGSI_TEXTURE_SHADOW2D
:
893 case TGSI_TEXTURE_SHADOWRECT
:
894 address
[count
++] = coords
[2];
896 case TGSI_TEXTURE_SHADOWCUBE
:
897 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
898 address
[count
++] = coords
[3];
900 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
901 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
904 /* Pack texture coordinates */
905 address
[count
++] = coords
[0];
907 case TGSI_TEXTURE_2D
:
908 case TGSI_TEXTURE_2D_ARRAY
:
909 case TGSI_TEXTURE_3D
:
910 case TGSI_TEXTURE_CUBE
:
911 case TGSI_TEXTURE_RECT
:
912 case TGSI_TEXTURE_SHADOW2D
:
913 case TGSI_TEXTURE_SHADOWRECT
:
914 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
915 case TGSI_TEXTURE_SHADOWCUBE
:
916 case TGSI_TEXTURE_2D_MSAA
:
917 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
918 case TGSI_TEXTURE_CUBE_ARRAY
:
919 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
920 address
[count
++] = coords
[1];
923 case TGSI_TEXTURE_3D
:
924 case TGSI_TEXTURE_CUBE
:
925 case TGSI_TEXTURE_SHADOWCUBE
:
926 case TGSI_TEXTURE_CUBE_ARRAY
:
927 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
928 address
[count
++] = coords
[2];
931 /* Pack array slice */
933 case TGSI_TEXTURE_1D_ARRAY
:
934 address
[count
++] = coords
[1];
937 case TGSI_TEXTURE_2D_ARRAY
:
938 case TGSI_TEXTURE_2D_ARRAY_MSAA
:
939 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
940 address
[count
++] = coords
[2];
943 case TGSI_TEXTURE_CUBE_ARRAY
:
944 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
945 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
946 address
[count
++] = coords
[3];
950 if (opcode
== TGSI_OPCODE_TXL
)
951 address
[count
++] = coords
[3];
954 assert(!"Cannot handle more than 16 texture address parameters");
958 for (chan
= 0; chan
< count
; chan
++ ) {
959 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
961 LLVMInt32TypeInContext(gallivm
->context
),
965 /* Pad to power of two vector */
966 while (count
< util_next_power_of_two(count
))
967 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
969 emit_data
->args
[0] = lp_build_gather_values(gallivm
, address
, count
);
972 emit_data
->args
[1] = si_shader_ctx
->resources
[emit_data
->inst
->Src
[1].Register
.Index
];
975 emit_data
->args
[2] = si_shader_ctx
->samplers
[emit_data
->inst
->Src
[1].Register
.Index
];
978 emit_data
->args
[3] = lp_build_const_int32(bld_base
->base
.gallivm
, target
);
980 emit_data
->arg_count
= 4;
982 emit_data
->dst_type
= LLVMVectorType(
983 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
987 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
988 struct lp_build_tgsi_context
* bld_base
,
989 struct lp_build_emit_data
* emit_data
)
991 struct lp_build_context
* base
= &bld_base
->base
;
994 sprintf(intr_name
, "%sv%ui32", action
->intr_name
,
995 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
997 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
998 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
999 emit_data
->args
, emit_data
->arg_count
,
1000 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1003 static const struct lp_build_tgsi_action tex_action
= {
1004 .fetch_args
= tex_fetch_args
,
1005 .emit
= build_tex_intrinsic
,
1006 .intr_name
= "llvm.SI.sample."
1009 static const struct lp_build_tgsi_action txb_action
= {
1010 .fetch_args
= tex_fetch_args
,
1011 .emit
= build_tex_intrinsic
,
1012 .intr_name
= "llvm.SI.sampleb."
1015 static const struct lp_build_tgsi_action txl_action
= {
1016 .fetch_args
= tex_fetch_args
,
1017 .emit
= build_tex_intrinsic
,
1018 .intr_name
= "llvm.SI.samplel."
1021 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
1023 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
1024 LLVMValueRef args
[3];
1026 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
1028 args
[2] = lp_build_const_int32(gallivm
, 1);
1030 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
1033 static void create_function(struct si_shader_context
*si_shader_ctx
)
1035 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
1036 LLVMTypeRef params
[20], f32
, i8
, i32
, v2i32
, v3i32
;
1039 i8
= LLVMInt8TypeInContext(gallivm
->context
);
1040 i32
= LLVMInt32TypeInContext(gallivm
->context
);
1041 f32
= LLVMFloatTypeInContext(gallivm
->context
);
1042 v2i32
= LLVMVectorType(i32
, 2);
1043 v3i32
= LLVMVectorType(i32
, 3);
1045 params
[SI_PARAM_CONST
] = LLVMPointerType(LLVMVectorType(i8
, 16), CONST_ADDR_SPACE
);
1046 params
[SI_PARAM_SAMPLER
] = params
[SI_PARAM_CONST
];
1047 params
[SI_PARAM_RESOURCE
] = LLVMPointerType(LLVMVectorType(i8
, 32), CONST_ADDR_SPACE
);
1049 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
1050 params
[SI_PARAM_VERTEX_BUFFER
] = params
[SI_PARAM_SAMPLER
];
1051 params
[SI_PARAM_START_INSTANCE
] = i32
;
1052 params
[SI_PARAM_VERTEX_ID
] = i32
;
1053 params
[SI_PARAM_DUMMY_0
] = i32
;
1054 params
[SI_PARAM_DUMMY_1
] = i32
;
1055 params
[SI_PARAM_INSTANCE_ID
] = i32
;
1056 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, 9);
1059 params
[SI_PARAM_PRIM_MASK
] = i32
;
1060 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
1061 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
1062 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
1063 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
1064 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
1065 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
1066 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
1067 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
1068 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
1069 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
1070 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
1071 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
1072 params
[SI_PARAM_FRONT_FACE
] = f32
;
1073 params
[SI_PARAM_ANCILLARY
] = f32
;
1074 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
1075 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
1076 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, 20);
1079 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
1080 for (i
= SI_PARAM_CONST
; i
<= SI_PARAM_VERTEX_BUFFER
; ++i
) {
1081 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
1082 LLVMAddAttribute(P
, LLVMInRegAttribute
);
1085 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
1086 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1087 SI_PARAM_START_INSTANCE
);
1088 LLVMAddAttribute(P
, LLVMInRegAttribute
);
1092 static void preload_constants(struct si_shader_context
*si_shader_ctx
)
1094 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
1095 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1096 const struct tgsi_shader_info
* info
= bld_base
->info
;
1098 unsigned i
, num_const
= info
->file_max
[TGSI_FILE_CONSTANT
] + 1;
1105 /* Allocate space for the constant values */
1106 si_shader_ctx
->constants
= CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
1108 /* Load the resource descriptor */
1109 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1110 si_shader_ctx
->const_resource
= build_indexed_load(si_shader_ctx
, ptr
, bld_base
->uint_bld
.zero
);
1112 /* Load the constants, we rely on the code sinking to do the rest */
1113 for (i
= 0; i
< num_const
* 4; ++i
) {
1114 LLVMValueRef args
[2] = {
1115 si_shader_ctx
->const_resource
,
1116 lp_build_const_int32(gallivm
, i
* 4)
1118 si_shader_ctx
->constants
[i
] = build_intrinsic(gallivm
->builder
, "llvm.SI.load.const",
1119 bld_base
->base
.elem_type
, args
, 2, LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1123 static void preload_samplers(struct si_shader_context
*si_shader_ctx
)
1125 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
1126 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1127 const struct tgsi_shader_info
* info
= bld_base
->info
;
1129 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
1131 LLVMValueRef res_ptr
, samp_ptr
;
1132 LLVMValueRef offset
;
1134 if (num_samplers
== 0)
1137 /* Allocate space for the values */
1138 si_shader_ctx
->resources
= CALLOC(num_samplers
, sizeof(LLVMValueRef
));
1139 si_shader_ctx
->samplers
= CALLOC(num_samplers
, sizeof(LLVMValueRef
));
1141 res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
1142 samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
1144 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1145 for (i
= 0; i
< num_samplers
; ++i
) {
1148 offset
= lp_build_const_int32(gallivm
, i
);
1149 si_shader_ctx
->resources
[i
] = build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
1152 offset
= lp_build_const_int32(gallivm
, i
);
1153 si_shader_ctx
->samplers
[i
] = build_indexed_load(si_shader_ctx
, samp_ptr
, offset
);
1157 int si_compile_llvm(struct r600_context
*rctx
, struct si_pipe_shader
*shader
,
1163 struct radeon_llvm_binary binary
;
1165 dump
= debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE
);
1167 memset(&binary
, 0, sizeof(binary
));
1168 radeon_llvm_compile(mod
, &binary
,
1169 r600_get_llvm_processor_name(rctx
->screen
->family
), dump
);
1171 fprintf(stderr
, "SI CODE:\n");
1172 for (i
= 0; i
< binary
.code_size
; i
+=4 ) {
1173 fprintf(stderr
, "%02x%02x%02x%02x\n", binary
.code
[i
+ 3],
1174 binary
.code
[i
+ 2], binary
.code
[i
+ 1],
1179 /* XXX: We may be able to emit some of these values directly rather than
1180 * extracting fields to be emitted later.
1182 for (i
= 0; i
< binary
.config_size
; i
+= 8) {
1183 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
));
1184 unsigned value
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
+ 4));
1186 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
1187 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
1188 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
1189 case R_00B848_COMPUTE_PGM_RSRC1
:
1190 shader
->num_sgprs
= (G_00B028_SGPRS(value
) + 1) * 8;
1191 shader
->num_vgprs
= (G_00B028_VGPRS(value
) + 1) * 4;
1193 case R_0286CC_SPI_PS_INPUT_ENA
:
1194 shader
->spi_ps_input_ena
= value
;
1197 fprintf(stderr
, "Warning: Compiler emitted unknown "
1198 "config register: 0x%x\n", reg
);
1203 /* copy new shader */
1204 si_resource_reference(&shader
->bo
, NULL
);
1205 shader
->bo
= si_resource_create_custom(rctx
->context
.screen
, PIPE_USAGE_IMMUTABLE
,
1207 if (shader
->bo
== NULL
) {
1211 ptr
= (uint32_t*)rctx
->ws
->buffer_map(shader
->bo
->cs_buf
, rctx
->cs
, PIPE_TRANSFER_WRITE
);
1212 if (0 /*R600_BIG_ENDIAN*/) {
1213 for (i
= 0; i
< binary
.code_size
/ 4; ++i
) {
1214 ptr
[i
] = util_bswap32(*(uint32_t*)(binary
.code
+ i
*4));
1217 memcpy(ptr
, binary
.code
, binary
.code_size
);
1219 rctx
->ws
->buffer_unmap(shader
->bo
->cs_buf
);
1222 free(binary
.config
);
1227 int si_pipe_shader_create(
1228 struct pipe_context
*ctx
,
1229 struct si_pipe_shader
*shader
)
1231 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
1232 struct si_pipe_shader_selector
*sel
= shader
->selector
;
1233 struct si_shader_context si_shader_ctx
;
1234 struct tgsi_shader_info shader_info
;
1235 struct lp_build_tgsi_context
* bld_base
;
1240 dump
= debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE
);
1242 assert(shader
->shader
.noutput
== 0);
1243 assert(shader
->shader
.ninterp
== 0);
1244 assert(shader
->shader
.ninput
== 0);
1246 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
1247 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
1248 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
1250 tgsi_scan_shader(sel
->tokens
, &shader_info
);
1251 shader
->shader
.uses_kill
= shader_info
.uses_kill
;
1252 shader
->shader
.uses_instanceid
= shader_info
.uses_instanceid
;
1253 bld_base
->info
= &shader_info
;
1254 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
1255 bld_base
->emit_epilogue
= si_llvm_emit_epilogue
;
1257 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
1258 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = txb_action
;
1259 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = txl_action
;
1260 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
1262 si_shader_ctx
.radeon_bld
.load_input
= declare_input
;
1263 si_shader_ctx
.radeon_bld
.load_system_value
= declare_system_value
;
1264 si_shader_ctx
.tokens
= sel
->tokens
;
1265 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
1266 si_shader_ctx
.shader
= shader
;
1267 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
1269 create_meta_data(&si_shader_ctx
);
1270 create_function(&si_shader_ctx
);
1271 preload_constants(&si_shader_ctx
);
1272 preload_samplers(&si_shader_ctx
);
1274 shader
->shader
.nr_cbufs
= rctx
->framebuffer
.nr_cbufs
;
1276 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1277 * conversion fails. */
1279 tgsi_dump(sel
->tokens
, 0);
1282 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
1283 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
1284 FREE(si_shader_ctx
.constants
);
1285 FREE(si_shader_ctx
.resources
);
1286 FREE(si_shader_ctx
.samplers
);
1290 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
1292 mod
= bld_base
->base
.gallivm
->module
;
1293 r
= si_compile_llvm(rctx
, shader
, mod
);
1295 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
1296 tgsi_parse_free(&si_shader_ctx
.parse
);
1298 FREE(si_shader_ctx
.constants
);
1299 FREE(si_shader_ctx
.resources
);
1300 FREE(si_shader_ctx
.samplers
);
1305 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
1307 si_resource_reference(&shader
->bo
, NULL
);