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 "gallivm/lp_bld_flow.h"
38 #include "radeon_llvm.h"
39 #include "radeon_llvm_emit.h"
40 #include "util/u_memory.h"
41 #include "tgsi/tgsi_info.h"
42 #include "tgsi/tgsi_parse.h"
43 #include "tgsi/tgsi_scan.h"
44 #include "tgsi/tgsi_util.h"
45 #include "tgsi/tgsi_dump.h"
47 #include "radeonsi_pipe.h"
48 #include "radeonsi_shader.h"
56 struct si_shader_context
58 struct radeon_llvm_context radeon_bld
;
59 struct tgsi_parse_context parse
;
60 struct tgsi_token
* tokens
;
61 struct si_pipe_shader
*shader
;
62 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
63 int param_streamout_config
;
64 int param_streamout_write_index
;
65 int param_streamout_offset
[4];
67 int param_instance_id
;
68 LLVMValueRef const_md
;
69 LLVMValueRef const_resource
;
70 #if HAVE_LLVM >= 0x0304
71 LLVMValueRef ddxy_lds
;
73 LLVMValueRef
*constants
;
74 LLVMValueRef
*resources
;
75 LLVMValueRef
*samplers
;
76 LLVMValueRef so_buffers
[4];
79 static struct si_shader_context
* si_shader_context(
80 struct lp_build_tgsi_context
* bld_base
)
82 return (struct si_shader_context
*)bld_base
;
86 #define PERSPECTIVE_BASE 0
89 #define SAMPLE_OFFSET 0
90 #define CENTER_OFFSET 2
91 #define CENTROID_OFSET 4
93 #define USE_SGPR_MAX_SUFFIX_LEN 5
94 #define CONST_ADDR_SPACE 2
95 #define LOCAL_ADDR_SPACE 3
96 #define USER_SGPR_ADDR_SPACE 8
99 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
101 * @param offset The offset parameter specifies the number of
102 * elements to offset, not the number of bytes or dwords. An element is the
103 * the type pointed to by the base_ptr parameter (e.g. int is the element of
106 * When LLVM lowers the load instruction, it will convert the element offset
107 * into a dword offset automatically.
110 static LLVMValueRef
build_indexed_load(
111 struct si_shader_context
* si_shader_ctx
,
112 LLVMValueRef base_ptr
,
115 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
117 LLVMValueRef indices
[2] = {
118 LLVMConstInt(LLVMInt64TypeInContext(base
->gallivm
->context
), 0, false),
121 LLVMValueRef computed_ptr
= LLVMBuildGEP(
122 base
->gallivm
->builder
, base_ptr
, indices
, 2, "");
124 LLVMValueRef result
= LLVMBuildLoad(base
->gallivm
->builder
, computed_ptr
, "");
125 LLVMSetMetadata(result
, 1, si_shader_ctx
->const_md
);
129 static LLVMValueRef
get_instance_index_for_fetch(
130 struct radeon_llvm_context
* radeon_bld
,
133 struct si_shader_context
*si_shader_ctx
=
134 si_shader_context(&radeon_bld
->soa
.bld_base
);
135 struct gallivm_state
* gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
137 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
138 si_shader_ctx
->param_instance_id
);
139 result
= LLVMBuildAdd(gallivm
->builder
, result
, LLVMGetParam(
140 radeon_bld
->main_fn
, SI_PARAM_START_INSTANCE
), "");
143 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
144 lp_build_const_int32(gallivm
, divisor
), "");
149 static void declare_input_vs(
150 struct si_shader_context
* si_shader_ctx
,
151 unsigned input_index
,
152 const struct tgsi_full_declaration
*decl
)
154 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
155 unsigned divisor
= si_shader_ctx
->shader
->key
.vs
.instance_divisors
[input_index
];
159 LLVMValueRef t_list_ptr
;
160 LLVMValueRef t_offset
;
162 LLVMValueRef attribute_offset
;
163 LLVMValueRef buffer_index
;
164 LLVMValueRef args
[3];
165 LLVMTypeRef vec4_type
;
168 /* Load the T list */
169 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFER
);
171 t_offset
= lp_build_const_int32(base
->gallivm
, input_index
);
173 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
, t_offset
);
175 /* Build the attribute offset */
176 attribute_offset
= lp_build_const_int32(base
->gallivm
, 0);
179 /* Build index from instance ID, start instance and divisor */
180 si_shader_ctx
->shader
->shader
.uses_instanceid
= true;
181 buffer_index
= get_instance_index_for_fetch(&si_shader_ctx
->radeon_bld
, divisor
);
183 /* Load the buffer index, which is always stored in VGPR0
184 * for Vertex Shaders */
185 buffer_index
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
186 si_shader_ctx
->param_vertex_id
);
189 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
191 args
[1] = attribute_offset
;
192 args
[2] = buffer_index
;
193 input
= build_intrinsic(base
->gallivm
->builder
,
194 "llvm.SI.vs.load.input", vec4_type
, args
, 3,
195 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
197 /* Break up the vec4 into individual components */
198 for (chan
= 0; chan
< 4; chan
++) {
199 LLVMValueRef llvm_chan
= lp_build_const_int32(base
->gallivm
, chan
);
200 /* XXX: Use a helper function for this. There is one in
202 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
203 LLVMBuildExtractElement(base
->gallivm
->builder
,
204 input
, llvm_chan
, "");
208 static void declare_input_fs(
209 struct si_shader_context
* si_shader_ctx
,
210 unsigned input_index
,
211 const struct tgsi_full_declaration
*decl
)
213 struct si_shader
*shader
= &si_shader_ctx
->shader
->shader
;
214 struct lp_build_context
* base
=
215 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
216 struct lp_build_context
*uint
=
217 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
218 struct gallivm_state
* gallivm
= base
->gallivm
;
219 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
220 LLVMValueRef main_fn
= si_shader_ctx
->radeon_bld
.main_fn
;
222 LLVMValueRef interp_param
;
223 const char * intr_name
;
226 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
227 * quad begins a new primitive. Bit 0 always needs
229 * [32:16] ParamOffset
232 LLVMValueRef params
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_PRIM_MASK
);
233 LLVMValueRef attr_number
;
237 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
238 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
240 radeon_llvm_reg_index_soa(input_index
, chan
);
241 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
242 LLVMGetParam(main_fn
, SI_PARAM_POS_X_FLOAT
+ chan
);
245 /* RCP for fragcoord.w */
246 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
247 LLVMBuildFDiv(gallivm
->builder
,
248 lp_build_const_float(gallivm
, 1.0f
),
249 si_shader_ctx
->radeon_bld
.inputs
[soa_index
],
255 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
256 LLVMValueRef face
, is_face_positive
;
258 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
260 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
262 lp_build_const_float(gallivm
, 0.0f
),
265 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
266 LLVMBuildSelect(gallivm
->builder
,
268 lp_build_const_float(gallivm
, 1.0f
),
269 lp_build_const_float(gallivm
, 0.0f
),
271 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
272 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
273 lp_build_const_float(gallivm
, 0.0f
);
274 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
275 lp_build_const_float(gallivm
, 1.0f
);
280 shader
->input
[input_index
].param_offset
= shader
->ninterp
++;
281 attr_number
= lp_build_const_int32(gallivm
,
282 shader
->input
[input_index
].param_offset
);
284 /* XXX: Handle all possible interpolation modes */
285 switch (decl
->Interp
.Interpolate
) {
286 case TGSI_INTERPOLATE_COLOR
:
287 if (si_shader_ctx
->shader
->key
.ps
.flatshade
) {
290 if (decl
->Interp
.Centroid
)
291 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
293 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
296 case TGSI_INTERPOLATE_CONSTANT
:
299 case TGSI_INTERPOLATE_LINEAR
:
300 if (decl
->Interp
.Centroid
)
301 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTROID
);
303 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTER
);
305 case TGSI_INTERPOLATE_PERSPECTIVE
:
306 if (decl
->Interp
.Centroid
)
307 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
309 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
312 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
316 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
318 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
319 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
320 si_shader_ctx
->shader
->key
.ps
.color_two_side
) {
321 LLVMValueRef args
[4];
322 LLVMValueRef face
, is_face_positive
;
323 LLVMValueRef back_attr_number
=
324 lp_build_const_int32(gallivm
,
325 shader
->input
[input_index
].param_offset
+ 1);
327 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
329 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
331 lp_build_const_float(gallivm
, 0.0f
),
335 args
[3] = interp_param
;
336 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
337 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
338 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
339 LLVMValueRef front
, back
;
342 args
[1] = attr_number
;
343 front
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
344 input_type
, args
, args
[3] ? 4 : 3,
345 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
347 args
[1] = back_attr_number
;
348 back
= build_intrinsic(base
->gallivm
->builder
, intr_name
,
349 input_type
, args
, args
[3] ? 4 : 3,
350 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
352 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
353 LLVMBuildSelect(gallivm
->builder
,
361 } else if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FOG
) {
362 LLVMValueRef args
[4];
364 args
[0] = uint
->zero
;
365 args
[1] = attr_number
;
367 args
[3] = interp_param
;
368 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
369 build_intrinsic(base
->gallivm
->builder
, intr_name
,
370 input_type
, args
, args
[3] ? 4 : 3,
371 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
372 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
373 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
374 lp_build_const_float(gallivm
, 0.0f
);
375 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
376 lp_build_const_float(gallivm
, 1.0f
);
378 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
379 LLVMValueRef args
[4];
380 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
381 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
383 args
[1] = attr_number
;
385 args
[3] = interp_param
;
386 si_shader_ctx
->radeon_bld
.inputs
[soa_index
] =
387 build_intrinsic(base
->gallivm
->builder
, intr_name
,
388 input_type
, args
, args
[3] ? 4 : 3,
389 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
394 static void declare_input(
395 struct radeon_llvm_context
* radeon_bld
,
396 unsigned input_index
,
397 const struct tgsi_full_declaration
*decl
)
399 struct si_shader_context
* si_shader_ctx
=
400 si_shader_context(&radeon_bld
->soa
.bld_base
);
401 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
402 declare_input_vs(si_shader_ctx
, input_index
, decl
);
403 } else if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
404 declare_input_fs(si_shader_ctx
, input_index
, decl
);
406 fprintf(stderr
, "Warning: Unsupported shader type,\n");
410 static void declare_system_value(
411 struct radeon_llvm_context
* radeon_bld
,
413 const struct tgsi_full_declaration
*decl
)
415 struct si_shader_context
*si_shader_ctx
=
416 si_shader_context(&radeon_bld
->soa
.bld_base
);
417 LLVMValueRef value
= 0;
419 switch (decl
->Semantic
.Name
) {
420 case TGSI_SEMANTIC_INSTANCEID
:
421 value
= LLVMGetParam(radeon_bld
->main_fn
,
422 si_shader_ctx
->param_instance_id
);
425 case TGSI_SEMANTIC_VERTEXID
:
426 value
= LLVMGetParam(radeon_bld
->main_fn
,
427 si_shader_ctx
->param_vertex_id
);
431 assert(!"unknown system value");
435 radeon_bld
->system_values
[index
] = value
;
438 static LLVMValueRef
fetch_constant(
439 struct lp_build_tgsi_context
* bld_base
,
440 const struct tgsi_full_src_register
*reg
,
441 enum tgsi_opcode_type type
,
444 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
445 struct lp_build_context
* base
= &bld_base
->base
;
446 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
449 LLVMValueRef args
[2];
453 if (swizzle
== LP_CHAN_ALL
) {
455 LLVMValueRef values
[4];
456 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
457 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
459 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
462 idx
= reg
->Register
.Index
* 4 + swizzle
;
463 if (!reg
->Register
.Indirect
)
464 return bitcast(bld_base
, type
, si_shader_ctx
->constants
[idx
]);
466 args
[0] = si_shader_ctx
->const_resource
;
467 args
[1] = lp_build_const_int32(base
->gallivm
, idx
* 4);
468 addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
469 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
470 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
471 args
[1] = lp_build_add(&bld_base
->uint_bld
, addr
, args
[1]);
473 result
= build_intrinsic(base
->gallivm
->builder
, "llvm.SI.load.const", base
->elem_type
,
474 args
, 2, LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
476 return bitcast(bld_base
, type
, result
);
479 /* Initialize arguments for the shader export intrinsic */
480 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
481 struct tgsi_full_declaration
*d
,
486 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
487 struct lp_build_context
*uint
=
488 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
489 struct lp_build_context
*base
= &bld_base
->base
;
490 unsigned compressed
= 0;
493 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
494 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
496 if (cbuf
>= 0 && cbuf
< 8) {
497 compressed
= (si_shader_ctx
->shader
->key
.ps
.export_16bpc
>> cbuf
) & 0x1;
500 si_shader_ctx
->shader
->spi_shader_col_format
|=
501 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
503 si_shader_ctx
->shader
->spi_shader_col_format
|=
504 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
506 si_shader_ctx
->shader
->cb_shader_mask
|= 0xf << (4 * cbuf
);
511 /* Pixel shader needs to pack output values before export */
512 for (chan
= 0; chan
< 2; chan
++ ) {
513 LLVMValueRef
*out_ptr
=
514 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
];
515 args
[0] = LLVMBuildLoad(base
->gallivm
->builder
,
516 out_ptr
[2 * chan
], "");
517 args
[1] = LLVMBuildLoad(base
->gallivm
->builder
,
518 out_ptr
[2 * chan
+ 1], "");
520 build_intrinsic(base
->gallivm
->builder
,
522 LLVMInt32TypeInContext(base
->gallivm
->context
),
524 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
525 args
[chan
+ 7] = args
[chan
+ 5] =
526 LLVMBuildBitCast(base
->gallivm
->builder
,
528 LLVMFloatTypeInContext(base
->gallivm
->context
),
535 for (chan
= 0; chan
< 4; chan
++ ) {
536 LLVMValueRef out_ptr
=
537 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][chan
];
538 /* +5 because the first output value will be
539 * the 6th argument to the intrinsic. */
540 args
[chan
+ 5] = LLVMBuildLoad(base
->gallivm
->builder
,
544 /* Clear COMPR flag */
545 args
[4] = uint
->zero
;
548 /* XXX: This controls which components of the output
549 * registers actually get exported. (e.g bit 0 means export
550 * X component, bit 1 means export Y component, etc.) I'm
551 * hard coding this to 0xf for now. In the future, we might
552 * want to do something else. */
553 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
555 /* Specify whether the EXEC mask represents the valid mask */
556 args
[1] = uint
->zero
;
558 /* Specify whether this is the last export */
559 args
[2] = uint
->zero
;
561 /* Specify the target we are exporting */
562 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
564 /* XXX: We probably need to keep track of the output
565 * values, so we know what we are passing to the next
569 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
572 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
573 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
575 if (si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_NEVER
) {
576 LLVMValueRef out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][3];
577 LLVMValueRef alpha_ref
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
580 LLVMValueRef alpha_pass
=
581 lp_build_cmp(&bld_base
->base
,
582 si_shader_ctx
->shader
->key
.ps
.alpha_func
,
583 LLVMBuildLoad(gallivm
->builder
, out_ptr
, ""),
586 lp_build_select(&bld_base
->base
,
588 lp_build_const_float(gallivm
, 1.0f
),
589 lp_build_const_float(gallivm
, -1.0f
));
591 build_intrinsic(gallivm
->builder
,
593 LLVMVoidTypeInContext(gallivm
->context
),
596 build_intrinsic(gallivm
->builder
,
598 LLVMVoidTypeInContext(gallivm
->context
),
603 static void si_alpha_to_one(struct lp_build_tgsi_context
*bld_base
,
606 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
608 /* set alpha to one */
609 LLVMBuildStore(bld_base
->base
.gallivm
->builder
,
611 si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][3]);
614 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
* bld_base
,
615 LLVMValueRef (*pos
)[9], unsigned index
)
617 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
618 struct si_pipe_shader
*shader
= si_shader_ctx
->shader
;
619 struct lp_build_context
*base
= &bld_base
->base
;
620 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
624 LLVMValueRef out_elts
[4];
625 LLVMValueRef base_elt
;
626 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
627 LLVMValueRef const_resource
= build_indexed_load(si_shader_ctx
, ptr
, uint
->one
);
629 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
630 LLVMValueRef out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[index
][chan
];
631 out_elts
[chan
] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
634 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
635 LLVMValueRef
*args
= pos
[2 + reg_index
];
637 if (!(shader
->key
.vs
.ucps_enabled
& (1 << reg_index
)))
640 shader
->shader
.clip_dist_write
|= 0xf << (4 * reg_index
);
645 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
647 /* Compute dot products of position and user clip plane vectors */
648 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
649 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
650 args
[0] = const_resource
;
651 args
[1] = lp_build_const_int32(base
->gallivm
,
652 ((reg_index
* 4 + chan
) * 4 +
654 base_elt
= build_intrinsic(base
->gallivm
->builder
,
655 "llvm.SI.load.const",
658 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
660 lp_build_add(base
, args
[5 + chan
],
661 lp_build_mul(base
, base_elt
,
662 out_elts
[const_chan
]));
666 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
667 args
[1] = uint
->zero
;
668 args
[2] = uint
->zero
;
669 args
[3] = lp_build_const_int32(base
->gallivm
,
670 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
671 args
[4] = uint
->zero
;
675 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
680 fprintf(stderr
, "STREAMOUT\n");
682 for (i
= 0; i
< so
->num_outputs
; i
++) {
683 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
684 so
->output
[i
].start_component
;
685 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
686 i
, so
->output
[i
].output_buffer
,
687 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
688 so
->output
[i
].register_index
,
692 mask
& 8 ? "w" : "");
696 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
697 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
698 * or v4i32 (num_channels=3,4). */
699 static void build_tbuffer_store(struct si_shader_context
*shader
,
702 unsigned num_channels
,
704 LLVMValueRef soffset
,
705 unsigned inst_offset
,
714 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
715 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
716 LLVMValueRef args
[] = {
719 LLVMConstInt(i32
, num_channels
, 0),
722 LLVMConstInt(i32
, inst_offset
, 0),
723 LLVMConstInt(i32
, dfmt
, 0),
724 LLVMConstInt(i32
, nfmt
, 0),
725 LLVMConstInt(i32
, offen
, 0),
726 LLVMConstInt(i32
, idxen
, 0),
727 LLVMConstInt(i32
, glc
, 0),
728 LLVMConstInt(i32
, slc
, 0),
729 LLVMConstInt(i32
, tfe
, 0)
732 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
733 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
734 const char *types
[] = {"i32", "v2i32", "v4i32"};
736 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
738 lp_build_intrinsic(gallivm
->builder
, name
,
739 LLVMVoidTypeInContext(gallivm
->context
),
740 args
, Elements(args
));
743 static void build_streamout_store(struct si_shader_context
*shader
,
746 unsigned num_channels
,
748 LLVMValueRef soffset
,
749 unsigned inst_offset
)
751 static unsigned dfmt
[] = {
752 V_008F0C_BUF_DATA_FORMAT_32
,
753 V_008F0C_BUF_DATA_FORMAT_32_32
,
754 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
755 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
757 assert(num_channels
>= 1 && num_channels
<= 4);
759 build_tbuffer_store(shader
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
760 inst_offset
, dfmt
[num_channels
-1],
761 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
764 /* On SI, the vertex shader is responsible for writing streamout data
766 static void si_llvm_emit_streamout(struct si_shader_context
*shader
)
768 struct pipe_stream_output_info
*so
= &shader
->shader
->selector
->so
;
769 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
770 LLVMBuilderRef builder
= gallivm
->builder
;
772 struct lp_build_if_state if_ctx
;
774 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
776 LLVMValueRef so_param
=
777 LLVMGetParam(shader
->radeon_bld
.main_fn
,
778 shader
->param_streamout_config
);
780 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
781 LLVMValueRef so_vtx_count
=
782 LLVMBuildAnd(builder
,
783 LLVMBuildLShr(builder
, so_param
,
784 LLVMConstInt(i32
, 16, 0), ""),
785 LLVMConstInt(i32
, 127, 0), "");
787 LLVMValueRef tid
= build_intrinsic(builder
, "llvm.SI.tid", i32
,
788 NULL
, 0, LLVMReadNoneAttribute
);
790 /* can_emit = tid < so_vtx_count; */
791 LLVMValueRef can_emit
=
792 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
794 /* Emit the streamout code conditionally. This actually avoids
795 * out-of-bounds buffer access. The hw tells us via the SGPR
796 * (so_vtx_count) which threads are allowed to emit streamout data. */
797 lp_build_if(&if_ctx
, gallivm
, can_emit
);
799 /* The buffer offset is computed as follows:
800 * ByteOffset = streamout_offset[buffer_id]*4 +
801 * (streamout_write_index + thread_id)*stride[buffer_id] +
805 LLVMValueRef so_write_index
=
806 LLVMGetParam(shader
->radeon_bld
.main_fn
,
807 shader
->param_streamout_write_index
);
809 /* Compute (streamout_write_index + thread_id). */
810 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
812 /* Compute the write offset for each enabled buffer. */
813 LLVMValueRef so_write_offset
[4] = {};
814 for (i
= 0; i
< 4; i
++) {
818 LLVMValueRef so_offset
= LLVMGetParam(shader
->radeon_bld
.main_fn
,
819 shader
->param_streamout_offset
[i
]);
820 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(i32
, 4, 0), "");
822 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
823 LLVMConstInt(i32
, so
->stride
[i
]*4, 0), "");
824 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
827 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
] = shader
->radeon_bld
.soa
.outputs
;
829 /* Write streamout data. */
830 for (i
= 0; i
< so
->num_outputs
; i
++) {
831 unsigned buf_idx
= so
->output
[i
].output_buffer
;
832 unsigned reg
= so
->output
[i
].register_index
;
833 unsigned start
= so
->output
[i
].start_component
;
834 unsigned num_comps
= so
->output
[i
].num_components
;
837 assert(num_comps
&& num_comps
<= 4);
838 if (!num_comps
|| num_comps
> 4)
841 /* Load the output as int. */
842 for (j
= 0; j
< num_comps
; j
++) {
843 out
[j
] = LLVMBuildLoad(builder
, outputs
[reg
][start
+j
], "");
844 out
[j
] = LLVMBuildBitCast(builder
, out
[j
], i32
, "");
847 /* Pack the output. */
848 LLVMValueRef vdata
= NULL
;
854 case 2: /* as v2i32 */
855 case 3: /* as v4i32 (aligned to 4) */
856 case 4: /* as v4i32 */
857 vdata
= LLVMGetUndef(LLVMVectorType(i32
, util_next_power_of_two(num_comps
)));
858 for (j
= 0; j
< num_comps
; j
++) {
859 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
860 LLVMConstInt(i32
, j
, 0), "");
865 build_streamout_store(shader
, shader
->so_buffers
[buf_idx
],
867 so_write_offset
[buf_idx
],
868 LLVMConstInt(i32
, 0, 0),
869 so
->output
[i
].dst_offset
*4);
872 lp_build_endif(&if_ctx
);
875 /* XXX: This is partially implemented for VS only at this point. It is not complete */
876 static void si_llvm_emit_epilogue(struct lp_build_tgsi_context
* bld_base
)
878 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
879 struct si_shader
* shader
= &si_shader_ctx
->shader
->shader
;
880 struct lp_build_context
* base
= &bld_base
->base
;
881 struct lp_build_context
* uint
=
882 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
883 struct tgsi_parse_context
*parse
= &si_shader_ctx
->parse
;
884 LLVMValueRef args
[9];
885 LLVMValueRef last_args
[9] = { 0 };
886 LLVMValueRef pos_args
[4][9] = { { 0 } };
887 unsigned semantic_name
;
888 unsigned param_count
= 0;
889 int depth_index
= -1, stencil_index
= -1;
892 if (si_shader_ctx
->shader
->selector
->so
.num_outputs
) {
893 si_llvm_emit_streamout(si_shader_ctx
);
896 while (!tgsi_parse_end_of_tokens(parse
)) {
897 struct tgsi_full_declaration
*d
=
898 &parse
->FullToken
.FullDeclaration
;
902 tgsi_parse_token(parse
);
904 if (parse
->FullToken
.Token
.Type
== TGSI_TOKEN_TYPE_PROPERTY
&&
905 parse
->FullToken
.FullProperty
.Property
.PropertyName
==
906 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
)
907 shader
->fs_write_all
= TRUE
;
909 if (parse
->FullToken
.Token
.Type
!= TGSI_TOKEN_TYPE_DECLARATION
)
912 switch (d
->Declaration
.File
) {
913 case TGSI_FILE_INPUT
:
914 i
= shader
->ninput
++;
915 assert(i
< Elements(shader
->input
));
916 shader
->input
[i
].name
= d
->Semantic
.Name
;
917 shader
->input
[i
].sid
= d
->Semantic
.Index
;
918 shader
->input
[i
].interpolate
= d
->Interp
.Interpolate
;
919 shader
->input
[i
].centroid
= d
->Interp
.Centroid
;
922 case TGSI_FILE_OUTPUT
:
923 i
= shader
->noutput
++;
924 assert(i
< Elements(shader
->output
));
925 shader
->output
[i
].name
= d
->Semantic
.Name
;
926 shader
->output
[i
].sid
= d
->Semantic
.Index
;
927 shader
->output
[i
].interpolate
= d
->Interp
.Interpolate
;
934 semantic_name
= d
->Semantic
.Name
;
936 for (index
= d
->Range
.First
; index
<= d
->Range
.Last
; index
++) {
937 /* Select the correct target */
938 switch(semantic_name
) {
939 case TGSI_SEMANTIC_PSIZE
:
940 shader
->vs_out_misc_write
= 1;
941 shader
->vs_out_point_size
= 1;
942 target
= V_008DFC_SQ_EXP_POS
+ 1;
944 case TGSI_SEMANTIC_POSITION
:
945 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
946 target
= V_008DFC_SQ_EXP_POS
;
952 case TGSI_SEMANTIC_STENCIL
:
953 stencil_index
= index
;
955 case TGSI_SEMANTIC_COLOR
:
956 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
957 case TGSI_SEMANTIC_BCOLOR
:
958 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
959 shader
->output
[i
].param_offset
= param_count
;
962 target
= V_008DFC_SQ_EXP_MRT
+ shader
->output
[i
].sid
;
963 if (si_shader_ctx
->shader
->key
.ps
.alpha_to_one
) {
964 si_alpha_to_one(bld_base
, index
);
966 if (shader
->output
[i
].sid
== 0 &&
967 si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_ALWAYS
)
968 si_alpha_test(bld_base
, index
);
971 case TGSI_SEMANTIC_CLIPDIST
:
972 if (!(si_shader_ctx
->shader
->key
.vs
.ucps_enabled
&
973 (1 << d
->Semantic
.Index
)))
975 shader
->clip_dist_write
|=
976 d
->Declaration
.UsageMask
<< (d
->Semantic
.Index
<< 2);
977 target
= V_008DFC_SQ_EXP_POS
+ 2 + d
->Semantic
.Index
;
979 case TGSI_SEMANTIC_CLIPVERTEX
:
980 si_llvm_emit_clipvertex(bld_base
, pos_args
, index
);
982 case TGSI_SEMANTIC_FOG
:
983 case TGSI_SEMANTIC_GENERIC
:
984 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
985 shader
->output
[i
].param_offset
= param_count
;
991 "Warning: SI unhandled output type:%d\n",
995 si_llvm_init_export_args(bld_base
, d
, index
, target
, args
);
997 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
&&
998 target
>= V_008DFC_SQ_EXP_POS
&&
999 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1000 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1001 args
, sizeof(args
));
1002 } else if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
&&
1003 semantic_name
== TGSI_SEMANTIC_COLOR
) {
1005 lp_build_intrinsic(base
->gallivm
->builder
,
1007 LLVMVoidTypeInContext(base
->gallivm
->context
),
1011 memcpy(last_args
, args
, sizeof(args
));
1013 lp_build_intrinsic(base
->gallivm
->builder
,
1015 LLVMVoidTypeInContext(base
->gallivm
->context
),
1021 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1022 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1023 goto handle_semantic
;
1027 if (depth_index
>= 0 || stencil_index
>= 0) {
1028 LLVMValueRef out_ptr
;
1031 /* Specify the target we are exporting */
1032 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
1034 if (depth_index
>= 0) {
1035 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
1036 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1039 if (stencil_index
< 0) {
1046 if (stencil_index
>= 0) {
1047 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
1050 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1051 /* Only setting the stencil component bit (0x2) here
1052 * breaks some stencil piglit tests
1056 if (depth_index
< 0)
1060 /* Specify which components to enable */
1061 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
1065 args
[4] = uint
->zero
;
1068 lp_build_intrinsic(base
->gallivm
->builder
,
1070 LLVMVoidTypeInContext(base
->gallivm
->context
),
1073 memcpy(last_args
, args
, sizeof(args
));
1076 if (si_shader_ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
1077 unsigned pos_idx
= 0;
1079 for (i
= 0; i
< 4; i
++)
1081 shader
->nr_pos_exports
++;
1083 for (i
= 0; i
< 4; i
++) {
1084 if (!pos_args
[i
][0])
1087 /* Specify the target we are exporting */
1088 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
1090 if (pos_idx
== shader
->nr_pos_exports
)
1091 /* Specify that this is the last export */
1092 pos_args
[i
][2] = uint
->one
;
1094 lp_build_intrinsic(base
->gallivm
->builder
,
1096 LLVMVoidTypeInContext(base
->gallivm
->context
),
1100 if (!last_args
[0]) {
1101 /* Specify which components to enable */
1102 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
1104 /* Specify the target we are exporting */
1105 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
1107 /* Set COMPR flag to zero to export data as 32-bit */
1108 last_args
[4] = uint
->zero
;
1111 last_args
[5]= uint
->zero
;
1112 last_args
[6]= uint
->zero
;
1113 last_args
[7]= uint
->zero
;
1114 last_args
[8]= uint
->zero
;
1116 si_shader_ctx
->shader
->spi_shader_col_format
|=
1117 V_028714_SPI_SHADER_32_ABGR
;
1118 si_shader_ctx
->shader
->cb_shader_mask
|= S_02823C_OUTPUT0_ENABLE(0xf);
1121 /* Specify whether the EXEC mask represents the valid mask */
1122 last_args
[1] = uint
->one
;
1124 if (shader
->fs_write_all
&& shader
->nr_cbufs
> 1) {
1127 /* Specify that this is not yet the last export */
1128 last_args
[2] = lp_build_const_int32(base
->gallivm
, 0);
1130 for (i
= 1; i
< shader
->nr_cbufs
; i
++) {
1131 /* Specify the target we are exporting */
1132 last_args
[3] = lp_build_const_int32(base
->gallivm
,
1133 V_008DFC_SQ_EXP_MRT
+ i
);
1135 lp_build_intrinsic(base
->gallivm
->builder
,
1137 LLVMVoidTypeInContext(base
->gallivm
->context
),
1140 si_shader_ctx
->shader
->spi_shader_col_format
|=
1141 si_shader_ctx
->shader
->spi_shader_col_format
<< 4;
1142 si_shader_ctx
->shader
->cb_shader_mask
|=
1143 si_shader_ctx
->shader
->cb_shader_mask
<< 4;
1146 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
1149 /* Specify that this is the last export */
1150 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
1152 lp_build_intrinsic(base
->gallivm
->builder
,
1154 LLVMVoidTypeInContext(base
->gallivm
->context
),
1157 /* XXX: Look up what this function does */
1158 /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
1161 static const struct lp_build_tgsi_action txf_action
;
1163 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1164 struct lp_build_tgsi_context
* bld_base
,
1165 struct lp_build_emit_data
* emit_data
);
1167 static void tex_fetch_args(
1168 struct lp_build_tgsi_context
* bld_base
,
1169 struct lp_build_emit_data
* emit_data
)
1171 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1172 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1173 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
1174 unsigned opcode
= inst
->Instruction
.Opcode
;
1175 unsigned target
= inst
->Texture
.Texture
;
1176 unsigned sampler_src
, sampler_index
;
1177 LLVMValueRef coords
[4];
1178 LLVMValueRef address
[16];
1180 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
1184 /* Fetch and project texture coordinates */
1185 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1186 for (chan
= 0; chan
< 3; chan
++ ) {
1187 coords
[chan
] = lp_build_emit_fetch(bld_base
,
1190 if (opcode
== TGSI_OPCODE_TXP
)
1191 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1197 if (opcode
== TGSI_OPCODE_TXP
)
1198 coords
[3] = bld_base
->base
.one
;
1200 /* Pack LOD bias value */
1201 if (opcode
== TGSI_OPCODE_TXB
)
1202 address
[count
++] = coords
[3];
1204 if (target
== TGSI_TEXTURE_CUBE
|| target
== TGSI_TEXTURE_SHADOWCUBE
)
1205 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
1207 /* Pack depth comparison value */
1209 case TGSI_TEXTURE_SHADOW1D
:
1210 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1211 case TGSI_TEXTURE_SHADOW2D
:
1212 case TGSI_TEXTURE_SHADOWRECT
:
1213 case TGSI_TEXTURE_SHADOWCUBE
:
1214 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1215 assert(ref_pos
>= 0);
1216 address
[count
++] = coords
[ref_pos
];
1218 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1219 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
1222 /* Pack user derivatives */
1223 if (opcode
== TGSI_OPCODE_TXD
) {
1224 for (chan
= 0; chan
< 2; chan
++) {
1225 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
1227 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 2, chan
);
1231 /* Pack texture coordinates */
1232 address
[count
++] = coords
[0];
1234 address
[count
++] = coords
[1];
1236 address
[count
++] = coords
[2];
1238 /* Pack LOD or sample index */
1239 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
1240 address
[count
++] = coords
[3];
1243 assert(!"Cannot handle more than 16 texture address parameters");
1247 for (chan
= 0; chan
< count
; chan
++ ) {
1248 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
1250 LLVMInt32TypeInContext(gallivm
->context
),
1254 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1255 sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
1257 /* Adjust the sample index according to FMASK.
1259 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1260 * which is the identity mapping. Each nibble says which physical sample
1261 * should be fetched to get that sample.
1263 * For example, 0x11111100 means there are only 2 samples stored and
1264 * the second sample covers 3/4 of the pixel. When reading samples 0
1265 * and 1, return physical sample 0 (determined by the first two 0s
1266 * in FMASK), otherwise return physical sample 1.
1268 * The sample index should be adjusted as follows:
1269 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1271 if (target
== TGSI_TEXTURE_2D_MSAA
||
1272 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1273 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1274 struct lp_build_emit_data txf_emit_data
= *emit_data
;
1275 LLVMValueRef txf_address
[4];
1276 unsigned txf_count
= count
;
1278 memcpy(txf_address
, address
, sizeof(txf_address
));
1280 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1281 txf_address
[2] = bld_base
->uint_bld
.zero
;
1283 txf_address
[3] = bld_base
->uint_bld
.zero
;
1285 /* Pad to a power-of-two size. */
1286 while (txf_count
< util_next_power_of_two(txf_count
))
1287 txf_address
[txf_count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1289 /* Read FMASK using TXF. */
1290 txf_emit_data
.chan
= 0;
1291 txf_emit_data
.dst_type
= LLVMVectorType(
1292 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
), 4);
1293 txf_emit_data
.args
[0] = lp_build_gather_values(gallivm
, txf_address
, txf_count
);
1294 txf_emit_data
.args
[1] = si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ sampler_index
];
1295 txf_emit_data
.args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
1296 target
== TGSI_TEXTURE_2D_MSAA
? TGSI_TEXTURE_2D
: TGSI_TEXTURE_2D_ARRAY
);
1297 txf_emit_data
.arg_count
= 3;
1299 build_tex_intrinsic(&txf_action
, bld_base
, &txf_emit_data
);
1301 /* Initialize some constants. */
1302 LLVMValueRef four
= LLVMConstInt(uint_bld
->elem_type
, 4, 0);
1303 LLVMValueRef F
= LLVMConstInt(uint_bld
->elem_type
, 0xF, 0);
1305 /* Apply the formula. */
1306 LLVMValueRef fmask
=
1307 LLVMBuildExtractElement(gallivm
->builder
,
1308 txf_emit_data
.output
[0],
1309 uint_bld
->zero
, "");
1311 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
1313 LLVMValueRef sample_index4
=
1314 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
1316 LLVMValueRef shifted_fmask
=
1317 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
1319 LLVMValueRef final_sample
=
1320 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
1322 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1323 * resource descriptor is 0 (invalid),
1325 LLVMValueRef fmask_desc
=
1326 LLVMBuildBitCast(gallivm
->builder
,
1327 si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ sampler_index
],
1328 LLVMVectorType(uint_bld
->elem_type
, 8), "");
1330 LLVMValueRef fmask_word1
=
1331 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
1334 LLVMValueRef word1_is_nonzero
=
1335 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1336 fmask_word1
, uint_bld
->zero
, "");
1338 /* Replace the MSAA sample index. */
1339 address
[sample_chan
] =
1340 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
1341 final_sample
, address
[sample_chan
], "");
1345 emit_data
->args
[1] = si_shader_ctx
->resources
[sampler_index
];
1347 if (opcode
== TGSI_OPCODE_TXF
) {
1348 /* add tex offsets */
1349 if (inst
->Texture
.NumOffsets
) {
1350 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1351 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1352 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
1354 assert(inst
->Texture
.NumOffsets
== 1);
1357 case TGSI_TEXTURE_3D
:
1358 address
[2] = lp_build_add(uint_bld
, address
[2],
1359 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
1361 case TGSI_TEXTURE_2D
:
1362 case TGSI_TEXTURE_SHADOW2D
:
1363 case TGSI_TEXTURE_RECT
:
1364 case TGSI_TEXTURE_SHADOWRECT
:
1365 case TGSI_TEXTURE_2D_ARRAY
:
1366 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1368 lp_build_add(uint_bld
, address
[1],
1369 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
1371 case TGSI_TEXTURE_1D
:
1372 case TGSI_TEXTURE_SHADOW1D
:
1373 case TGSI_TEXTURE_1D_ARRAY
:
1374 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1376 lp_build_add(uint_bld
, address
[0],
1377 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
1379 /* texture offsets do not apply to other texture targets */
1383 emit_data
->dst_type
= LLVMVectorType(
1384 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
1387 emit_data
->arg_count
= 3;
1390 emit_data
->args
[2] = si_shader_ctx
->samplers
[sampler_index
];
1392 emit_data
->dst_type
= LLVMVectorType(
1393 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
1396 emit_data
->arg_count
= 4;
1400 emit_data
->args
[emit_data
->arg_count
- 1] =
1401 lp_build_const_int32(bld_base
->base
.gallivm
, target
);
1403 /* Pad to power of two vector */
1404 while (count
< util_next_power_of_two(count
))
1405 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1407 emit_data
->args
[0] = lp_build_gather_values(gallivm
, address
, count
);
1410 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1411 struct lp_build_tgsi_context
* bld_base
,
1412 struct lp_build_emit_data
* emit_data
)
1414 struct lp_build_context
* base
= &bld_base
->base
;
1417 sprintf(intr_name
, "%sv%ui32", action
->intr_name
,
1418 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
1420 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1421 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
1422 emit_data
->args
, emit_data
->arg_count
,
1423 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1426 static void txq_fetch_args(
1427 struct lp_build_tgsi_context
* bld_base
,
1428 struct lp_build_emit_data
* emit_data
)
1430 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1431 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1434 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1437 emit_data
->args
[1] = si_shader_ctx
->resources
[inst
->Src
[1].Register
.Index
];
1440 emit_data
->args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
1441 inst
->Texture
.Texture
);
1443 emit_data
->arg_count
= 3;
1445 emit_data
->dst_type
= LLVMVectorType(
1446 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
1450 #if HAVE_LLVM >= 0x0304
1452 static void si_llvm_emit_ddxy(
1453 const struct lp_build_tgsi_action
* action
,
1454 struct lp_build_tgsi_context
* bld_base
,
1455 struct lp_build_emit_data
* emit_data
)
1457 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1458 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1459 struct lp_build_context
* base
= &bld_base
->base
;
1460 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1461 unsigned opcode
= inst
->Instruction
.Opcode
;
1462 LLVMValueRef indices
[2];
1463 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1464 LLVMValueRef tl
, trbl
, result
[4];
1466 unsigned swizzle
[4];
1469 i32
= LLVMInt32TypeInContext(gallivm
->context
);
1471 indices
[0] = bld_base
->uint_bld
.zero
;
1472 indices
[1] = build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
1473 NULL
, 0, LLVMReadNoneAttribute
);
1474 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1477 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
1478 lp_build_const_int32(gallivm
, 0xfffffffc), "");
1479 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1482 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
1483 lp_build_const_int32(gallivm
,
1484 opcode
== TGSI_OPCODE_DDX
? 1 : 2),
1486 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1489 for (c
= 0; c
< 4; ++c
) {
1492 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
1493 for (i
= 0; i
< c
; ++i
) {
1494 if (swizzle
[i
] == swizzle
[c
]) {
1495 result
[c
] = result
[i
];
1502 LLVMBuildStore(gallivm
->builder
,
1503 LLVMBuildBitCast(gallivm
->builder
,
1504 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
1508 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
1509 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
1511 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
1512 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, base
->elem_type
, "");
1514 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
1517 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
1520 #endif /* HAVE_LLVM >= 0x0304 */
1522 static const struct lp_build_tgsi_action tex_action
= {
1523 .fetch_args
= tex_fetch_args
,
1524 .emit
= build_tex_intrinsic
,
1525 .intr_name
= "llvm.SI.sample."
1528 static const struct lp_build_tgsi_action txb_action
= {
1529 .fetch_args
= tex_fetch_args
,
1530 .emit
= build_tex_intrinsic
,
1531 .intr_name
= "llvm.SI.sampleb."
1534 #if HAVE_LLVM >= 0x0304
1535 static const struct lp_build_tgsi_action txd_action
= {
1536 .fetch_args
= tex_fetch_args
,
1537 .emit
= build_tex_intrinsic
,
1538 .intr_name
= "llvm.SI.sampled."
1542 static const struct lp_build_tgsi_action txf_action
= {
1543 .fetch_args
= tex_fetch_args
,
1544 .emit
= build_tex_intrinsic
,
1545 .intr_name
= "llvm.SI.imageload."
1548 static const struct lp_build_tgsi_action txl_action
= {
1549 .fetch_args
= tex_fetch_args
,
1550 .emit
= build_tex_intrinsic
,
1551 .intr_name
= "llvm.SI.samplel."
1554 static const struct lp_build_tgsi_action txq_action
= {
1555 .fetch_args
= txq_fetch_args
,
1556 .emit
= build_tgsi_intrinsic_nomem
,
1557 .intr_name
= "llvm.SI.resinfo"
1560 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
1562 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
1563 LLVMValueRef args
[3];
1565 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
1567 args
[2] = lp_build_const_int32(gallivm
, 1);
1569 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
1572 static void create_function(struct si_shader_context
*si_shader_ctx
)
1574 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
1575 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1576 LLVMTypeRef params
[21], f32
, i8
, i32
, v2i32
, v3i32
;
1577 unsigned i
, last_sgpr
, num_params
;
1579 i8
= LLVMInt8TypeInContext(gallivm
->context
);
1580 i32
= LLVMInt32TypeInContext(gallivm
->context
);
1581 f32
= LLVMFloatTypeInContext(gallivm
->context
);
1582 v2i32
= LLVMVectorType(i32
, 2);
1583 v3i32
= LLVMVectorType(i32
, 3);
1585 params
[SI_PARAM_CONST
] = LLVMPointerType(
1586 LLVMArrayType(LLVMVectorType(i8
, 16), NUM_CONST_BUFFERS
), CONST_ADDR_SPACE
);
1587 /* We assume at most 16 textures per program at the moment.
1588 * This need probably need to be changed to support bindless textures */
1589 params
[SI_PARAM_SAMPLER
] = LLVMPointerType(
1590 LLVMArrayType(LLVMVectorType(i8
, 16), NUM_SAMPLER_VIEWS
), CONST_ADDR_SPACE
);
1591 params
[SI_PARAM_RESOURCE
] = LLVMPointerType(
1592 LLVMArrayType(LLVMVectorType(i8
, 32), NUM_SAMPLER_STATES
), CONST_ADDR_SPACE
);
1594 switch (si_shader_ctx
->type
) {
1595 case TGSI_PROCESSOR_VERTEX
:
1596 params
[SI_PARAM_VERTEX_BUFFER
] = params
[SI_PARAM_CONST
];
1597 params
[SI_PARAM_SO_BUFFER
] = params
[SI_PARAM_CONST
];
1598 params
[SI_PARAM_START_INSTANCE
] = i32
;
1599 num_params
= SI_PARAM_START_INSTANCE
+1;
1601 /* The locations of the other parameters are assigned dynamically. */
1603 /* Streamout SGPRs. */
1604 if (si_shader_ctx
->shader
->selector
->so
.num_outputs
) {
1605 params
[si_shader_ctx
->param_streamout_config
= num_params
++] = i32
;
1606 params
[si_shader_ctx
->param_streamout_write_index
= num_params
++] = i32
;
1608 /* A streamout buffer offset is loaded if the stride is non-zero. */
1609 for (i
= 0; i
< 4; i
++) {
1610 if (!si_shader_ctx
->shader
->selector
->so
.stride
[i
])
1613 params
[si_shader_ctx
->param_streamout_offset
[i
] = num_params
++] = i32
;
1616 last_sgpr
= num_params
-1;
1619 params
[si_shader_ctx
->param_vertex_id
= num_params
++] = i32
;
1620 params
[num_params
++] = i32
; /* unused*/
1621 params
[num_params
++] = i32
; /* unused */
1622 params
[si_shader_ctx
->param_instance_id
= num_params
++] = i32
;
1625 case TGSI_PROCESSOR_FRAGMENT
:
1626 params
[SI_PARAM_ALPHA_REF
] = f32
;
1627 params
[SI_PARAM_PRIM_MASK
] = i32
;
1628 last_sgpr
= SI_PARAM_PRIM_MASK
;
1629 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
1630 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
1631 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
1632 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
1633 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
1634 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
1635 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
1636 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
1637 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
1638 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
1639 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
1640 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
1641 params
[SI_PARAM_FRONT_FACE
] = f32
;
1642 params
[SI_PARAM_ANCILLARY
] = f32
;
1643 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
1644 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
1645 num_params
= SI_PARAM_POS_FIXED_PT
+1;
1649 assert(0 && "unimplemented shader");
1653 assert(num_params
<= Elements(params
));
1654 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, num_params
);
1655 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
1657 for (i
= 0; i
<= last_sgpr
; ++i
) {
1658 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
1661 LLVMAddAttribute(P
, LLVMInRegAttribute
);
1663 #if HAVE_LLVM >= 0x0304
1664 /* We tell llvm that array inputs are passed by value to allow Sinking pass
1665 * to move load. Inputs are constant so this is fine. */
1666 case SI_PARAM_CONST
:
1667 case SI_PARAM_SAMPLER
:
1668 case SI_PARAM_RESOURCE
:
1669 LLVMAddAttribute(P
, LLVMByValAttribute
);
1675 #if HAVE_LLVM >= 0x0304
1676 if (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
1677 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0)
1678 si_shader_ctx
->ddxy_lds
=
1679 LLVMAddGlobalInAddressSpace(gallivm
->module
,
1680 LLVMArrayType(i32
, 64),
1686 static void preload_constants(struct si_shader_context
*si_shader_ctx
)
1688 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
1689 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1690 const struct tgsi_shader_info
* info
= bld_base
->info
;
1692 unsigned i
, num_const
= info
->file_max
[TGSI_FILE_CONSTANT
] + 1;
1699 /* Allocate space for the constant values */
1700 si_shader_ctx
->constants
= CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
1702 /* Load the resource descriptor */
1703 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1704 si_shader_ctx
->const_resource
= build_indexed_load(si_shader_ctx
, ptr
, bld_base
->uint_bld
.zero
);
1706 /* Load the constants, we rely on the code sinking to do the rest */
1707 for (i
= 0; i
< num_const
* 4; ++i
) {
1708 LLVMValueRef args
[2] = {
1709 si_shader_ctx
->const_resource
,
1710 lp_build_const_int32(gallivm
, i
* 4)
1712 si_shader_ctx
->constants
[i
] = build_intrinsic(gallivm
->builder
, "llvm.SI.load.const",
1713 bld_base
->base
.elem_type
, args
, 2, LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1717 static void preload_samplers(struct si_shader_context
*si_shader_ctx
)
1719 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
1720 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1721 const struct tgsi_shader_info
* info
= bld_base
->info
;
1723 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
1725 LLVMValueRef res_ptr
, samp_ptr
;
1726 LLVMValueRef offset
;
1728 if (num_samplers
== 0)
1731 /* Allocate space for the values */
1732 si_shader_ctx
->resources
= CALLOC(NUM_SAMPLER_VIEWS
, sizeof(LLVMValueRef
));
1733 si_shader_ctx
->samplers
= CALLOC(num_samplers
, sizeof(LLVMValueRef
));
1735 res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
1736 samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
1738 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1739 for (i
= 0; i
< num_samplers
; ++i
) {
1741 offset
= lp_build_const_int32(gallivm
, i
);
1742 si_shader_ctx
->resources
[i
] = build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
1745 offset
= lp_build_const_int32(gallivm
, i
);
1746 si_shader_ctx
->samplers
[i
] = build_indexed_load(si_shader_ctx
, samp_ptr
, offset
);
1748 /* FMASK resource */
1749 if (info
->is_msaa_sampler
[i
]) {
1750 offset
= lp_build_const_int32(gallivm
, FMASK_TEX_OFFSET
+ i
);
1751 si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ i
] =
1752 build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
1757 static void preload_streamout_buffers(struct si_shader_context
*si_shader_ctx
)
1759 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
1760 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1763 if (!si_shader_ctx
->shader
->selector
->so
.num_outputs
)
1766 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1767 SI_PARAM_SO_BUFFER
);
1769 /* Load the resources, we rely on the code sinking to do the rest */
1770 for (i
= 0; i
< 4; ++i
) {
1771 if (si_shader_ctx
->shader
->selector
->so
.stride
[i
]) {
1772 LLVMValueRef offset
= lp_build_const_int32(gallivm
, i
);
1774 si_shader_ctx
->so_buffers
[i
] = build_indexed_load(si_shader_ctx
, buf_ptr
, offset
);
1779 int si_compile_llvm(struct r600_context
*rctx
, struct si_pipe_shader
*shader
,
1784 struct radeon_llvm_binary binary
;
1785 bool dump
= r600_can_dump_shader(&rctx
->screen
->b
,
1786 shader
->selector
? shader
->selector
->tokens
: NULL
);
1787 memset(&binary
, 0, sizeof(binary
));
1788 radeon_llvm_compile(mod
, &binary
,
1789 r600_get_llvm_processor_name(rctx
->screen
->b
.family
), dump
);
1790 if (dump
&& ! binary
.disassembled
) {
1791 fprintf(stderr
, "SI CODE:\n");
1792 for (i
= 0; i
< binary
.code_size
; i
+=4 ) {
1793 fprintf(stderr
, "%02x%02x%02x%02x\n", binary
.code
[i
+ 3],
1794 binary
.code
[i
+ 2], binary
.code
[i
+ 1],
1799 /* XXX: We may be able to emit some of these values directly rather than
1800 * extracting fields to be emitted later.
1802 for (i
= 0; i
< binary
.config_size
; i
+= 8) {
1803 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
));
1804 unsigned value
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
+ 4));
1806 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
1807 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
1808 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
1809 case R_00B848_COMPUTE_PGM_RSRC1
:
1810 shader
->num_sgprs
= (G_00B028_SGPRS(value
) + 1) * 8;
1811 shader
->num_vgprs
= (G_00B028_VGPRS(value
) + 1) * 4;
1813 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
1814 shader
->lds_size
= G_00B02C_EXTRA_LDS_SIZE(value
);
1816 case R_00B84C_COMPUTE_PGM_RSRC2
:
1817 shader
->lds_size
= G_00B84C_LDS_SIZE(value
);
1819 case R_0286CC_SPI_PS_INPUT_ENA
:
1820 shader
->spi_ps_input_ena
= value
;
1823 fprintf(stderr
, "Warning: Compiler emitted unknown "
1824 "config register: 0x%x\n", reg
);
1829 /* copy new shader */
1830 r600_resource_reference(&shader
->bo
, NULL
);
1831 shader
->bo
= r600_resource_create_custom(rctx
->b
.b
.screen
, PIPE_USAGE_IMMUTABLE
,
1833 if (shader
->bo
== NULL
) {
1837 ptr
= (uint32_t*)rctx
->b
.ws
->buffer_map(shader
->bo
->cs_buf
, rctx
->b
.rings
.gfx
.cs
, PIPE_TRANSFER_WRITE
);
1838 if (0 /*R600_BIG_ENDIAN*/) {
1839 for (i
= 0; i
< binary
.code_size
/ 4; ++i
) {
1840 ptr
[i
] = util_bswap32(*(uint32_t*)(binary
.code
+ i
*4));
1843 memcpy(ptr
, binary
.code
, binary
.code_size
);
1845 rctx
->b
.ws
->buffer_unmap(shader
->bo
->cs_buf
);
1848 free(binary
.config
);
1853 int si_pipe_shader_create(
1854 struct pipe_context
*ctx
,
1855 struct si_pipe_shader
*shader
)
1857 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
1858 struct si_pipe_shader_selector
*sel
= shader
->selector
;
1859 struct si_shader_context si_shader_ctx
;
1860 struct tgsi_shader_info shader_info
;
1861 struct lp_build_tgsi_context
* bld_base
;
1864 bool dump
= r600_can_dump_shader(&rctx
->screen
->b
, shader
->selector
->tokens
);
1866 assert(shader
->shader
.noutput
== 0);
1867 assert(shader
->shader
.ninterp
== 0);
1868 assert(shader
->shader
.ninput
== 0);
1870 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
1871 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
1872 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
1874 tgsi_scan_shader(sel
->tokens
, &shader_info
);
1876 shader
->shader
.uses_kill
= shader_info
.uses_kill
;
1877 shader
->shader
.uses_instanceid
= shader_info
.uses_instanceid
;
1878 bld_base
->info
= &shader_info
;
1879 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
1880 bld_base
->emit_epilogue
= si_llvm_emit_epilogue
;
1882 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
1883 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = txb_action
;
1884 #if HAVE_LLVM >= 0x0304
1885 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = txd_action
;
1887 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = txf_action
;
1888 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = txl_action
;
1889 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
1890 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = txq_action
;
1892 #if HAVE_LLVM >= 0x0304
1893 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
1894 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
1897 si_shader_ctx
.radeon_bld
.load_input
= declare_input
;
1898 si_shader_ctx
.radeon_bld
.load_system_value
= declare_system_value
;
1899 si_shader_ctx
.tokens
= sel
->tokens
;
1900 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
1901 si_shader_ctx
.shader
= shader
;
1902 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
1904 create_meta_data(&si_shader_ctx
);
1905 create_function(&si_shader_ctx
);
1906 preload_constants(&si_shader_ctx
);
1907 preload_samplers(&si_shader_ctx
);
1908 preload_streamout_buffers(&si_shader_ctx
);
1910 shader
->shader
.nr_cbufs
= rctx
->framebuffer
.nr_cbufs
;
1912 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1913 * conversion fails. */
1915 tgsi_dump(sel
->tokens
, 0);
1916 si_dump_streamout(&sel
->so
);
1919 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
1920 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
1921 FREE(si_shader_ctx
.constants
);
1922 FREE(si_shader_ctx
.resources
);
1923 FREE(si_shader_ctx
.samplers
);
1927 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
1929 mod
= bld_base
->base
.gallivm
->module
;
1930 r
= si_compile_llvm(rctx
, shader
, mod
);
1932 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
1933 tgsi_parse_free(&si_shader_ctx
.parse
);
1935 FREE(si_shader_ctx
.constants
);
1936 FREE(si_shader_ctx
.resources
);
1937 FREE(si_shader_ctx
.samplers
);
1942 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
1944 r600_resource_reference(&shader
->bo
, NULL
);