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 /* We need to add the position output manually if it's missing. */
1080 if (!pos_args
[0][0]) {
1081 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1082 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1083 pos_args
[0][2] = uint
->zero
; /* last export? */
1084 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1085 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1086 pos_args
[0][5] = base
->zero
; /* X */
1087 pos_args
[0][6] = base
->zero
; /* Y */
1088 pos_args
[0][7] = base
->zero
; /* Z */
1089 pos_args
[0][8] = base
->one
; /* W */
1092 for (i
= 0; i
< 4; i
++)
1094 shader
->nr_pos_exports
++;
1096 for (i
= 0; i
< 4; i
++) {
1097 if (!pos_args
[i
][0])
1100 /* Specify the target we are exporting */
1101 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
1103 if (pos_idx
== shader
->nr_pos_exports
)
1104 /* Specify that this is the last export */
1105 pos_args
[i
][2] = uint
->one
;
1107 lp_build_intrinsic(base
->gallivm
->builder
,
1109 LLVMVoidTypeInContext(base
->gallivm
->context
),
1113 if (!last_args
[0]) {
1114 /* Specify which components to enable */
1115 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
1117 /* Specify the target we are exporting */
1118 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
1120 /* Set COMPR flag to zero to export data as 32-bit */
1121 last_args
[4] = uint
->zero
;
1124 last_args
[5]= uint
->zero
;
1125 last_args
[6]= uint
->zero
;
1126 last_args
[7]= uint
->zero
;
1127 last_args
[8]= uint
->zero
;
1129 si_shader_ctx
->shader
->spi_shader_col_format
|=
1130 V_028714_SPI_SHADER_32_ABGR
;
1131 si_shader_ctx
->shader
->cb_shader_mask
|= S_02823C_OUTPUT0_ENABLE(0xf);
1134 /* Specify whether the EXEC mask represents the valid mask */
1135 last_args
[1] = uint
->one
;
1137 if (shader
->fs_write_all
&& shader
->nr_cbufs
> 1) {
1140 /* Specify that this is not yet the last export */
1141 last_args
[2] = lp_build_const_int32(base
->gallivm
, 0);
1143 for (i
= 1; i
< shader
->nr_cbufs
; i
++) {
1144 /* Specify the target we are exporting */
1145 last_args
[3] = lp_build_const_int32(base
->gallivm
,
1146 V_008DFC_SQ_EXP_MRT
+ i
);
1148 lp_build_intrinsic(base
->gallivm
->builder
,
1150 LLVMVoidTypeInContext(base
->gallivm
->context
),
1153 si_shader_ctx
->shader
->spi_shader_col_format
|=
1154 si_shader_ctx
->shader
->spi_shader_col_format
<< 4;
1155 si_shader_ctx
->shader
->cb_shader_mask
|=
1156 si_shader_ctx
->shader
->cb_shader_mask
<< 4;
1159 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
1162 /* Specify that this is the last export */
1163 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
1165 lp_build_intrinsic(base
->gallivm
->builder
,
1167 LLVMVoidTypeInContext(base
->gallivm
->context
),
1170 /* XXX: Look up what this function does */
1171 /* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
1174 static const struct lp_build_tgsi_action txf_action
;
1176 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1177 struct lp_build_tgsi_context
* bld_base
,
1178 struct lp_build_emit_data
* emit_data
);
1180 static void tex_fetch_args(
1181 struct lp_build_tgsi_context
* bld_base
,
1182 struct lp_build_emit_data
* emit_data
)
1184 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1185 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1186 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
1187 unsigned opcode
= inst
->Instruction
.Opcode
;
1188 unsigned target
= inst
->Texture
.Texture
;
1189 unsigned sampler_src
, sampler_index
;
1190 LLVMValueRef coords
[4];
1191 LLVMValueRef address
[16];
1193 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
1197 /* Fetch and project texture coordinates */
1198 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1199 for (chan
= 0; chan
< 3; chan
++ ) {
1200 coords
[chan
] = lp_build_emit_fetch(bld_base
,
1203 if (opcode
== TGSI_OPCODE_TXP
)
1204 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1210 if (opcode
== TGSI_OPCODE_TXP
)
1211 coords
[3] = bld_base
->base
.one
;
1213 /* Pack LOD bias value */
1214 if (opcode
== TGSI_OPCODE_TXB
)
1215 address
[count
++] = coords
[3];
1217 if (target
== TGSI_TEXTURE_CUBE
|| target
== TGSI_TEXTURE_SHADOWCUBE
)
1218 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
1220 /* Pack depth comparison value */
1222 case TGSI_TEXTURE_SHADOW1D
:
1223 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1224 case TGSI_TEXTURE_SHADOW2D
:
1225 case TGSI_TEXTURE_SHADOWRECT
:
1226 case TGSI_TEXTURE_SHADOWCUBE
:
1227 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1228 assert(ref_pos
>= 0);
1229 address
[count
++] = coords
[ref_pos
];
1231 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1232 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
1235 /* Pack user derivatives */
1236 if (opcode
== TGSI_OPCODE_TXD
) {
1237 for (chan
= 0; chan
< 2; chan
++) {
1238 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, chan
);
1240 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 2, chan
);
1244 /* Pack texture coordinates */
1245 address
[count
++] = coords
[0];
1247 address
[count
++] = coords
[1];
1249 address
[count
++] = coords
[2];
1251 /* Pack LOD or sample index */
1252 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
1253 address
[count
++] = coords
[3];
1256 assert(!"Cannot handle more than 16 texture address parameters");
1260 for (chan
= 0; chan
< count
; chan
++ ) {
1261 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
1263 LLVMInt32TypeInContext(gallivm
->context
),
1267 sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1268 sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
1270 /* Adjust the sample index according to FMASK.
1272 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1273 * which is the identity mapping. Each nibble says which physical sample
1274 * should be fetched to get that sample.
1276 * For example, 0x11111100 means there are only 2 samples stored and
1277 * the second sample covers 3/4 of the pixel. When reading samples 0
1278 * and 1, return physical sample 0 (determined by the first two 0s
1279 * in FMASK), otherwise return physical sample 1.
1281 * The sample index should be adjusted as follows:
1282 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1284 if (target
== TGSI_TEXTURE_2D_MSAA
||
1285 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1286 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1287 struct lp_build_emit_data txf_emit_data
= *emit_data
;
1288 LLVMValueRef txf_address
[4];
1289 unsigned txf_count
= count
;
1291 memcpy(txf_address
, address
, sizeof(txf_address
));
1293 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1294 txf_address
[2] = bld_base
->uint_bld
.zero
;
1296 txf_address
[3] = bld_base
->uint_bld
.zero
;
1298 /* Pad to a power-of-two size. */
1299 while (txf_count
< util_next_power_of_two(txf_count
))
1300 txf_address
[txf_count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1302 /* Read FMASK using TXF. */
1303 txf_emit_data
.chan
= 0;
1304 txf_emit_data
.dst_type
= LLVMVectorType(
1305 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
), 4);
1306 txf_emit_data
.args
[0] = lp_build_gather_values(gallivm
, txf_address
, txf_count
);
1307 txf_emit_data
.args
[1] = si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ sampler_index
];
1308 txf_emit_data
.args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
1309 target
== TGSI_TEXTURE_2D_MSAA
? TGSI_TEXTURE_2D
: TGSI_TEXTURE_2D_ARRAY
);
1310 txf_emit_data
.arg_count
= 3;
1312 build_tex_intrinsic(&txf_action
, bld_base
, &txf_emit_data
);
1314 /* Initialize some constants. */
1315 LLVMValueRef four
= LLVMConstInt(uint_bld
->elem_type
, 4, 0);
1316 LLVMValueRef F
= LLVMConstInt(uint_bld
->elem_type
, 0xF, 0);
1318 /* Apply the formula. */
1319 LLVMValueRef fmask
=
1320 LLVMBuildExtractElement(gallivm
->builder
,
1321 txf_emit_data
.output
[0],
1322 uint_bld
->zero
, "");
1324 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
1326 LLVMValueRef sample_index4
=
1327 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
1329 LLVMValueRef shifted_fmask
=
1330 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
1332 LLVMValueRef final_sample
=
1333 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
1335 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1336 * resource descriptor is 0 (invalid),
1338 LLVMValueRef fmask_desc
=
1339 LLVMBuildBitCast(gallivm
->builder
,
1340 si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ sampler_index
],
1341 LLVMVectorType(uint_bld
->elem_type
, 8), "");
1343 LLVMValueRef fmask_word1
=
1344 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
1347 LLVMValueRef word1_is_nonzero
=
1348 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1349 fmask_word1
, uint_bld
->zero
, "");
1351 /* Replace the MSAA sample index. */
1352 address
[sample_chan
] =
1353 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
1354 final_sample
, address
[sample_chan
], "");
1358 emit_data
->args
[1] = si_shader_ctx
->resources
[sampler_index
];
1360 if (opcode
== TGSI_OPCODE_TXF
) {
1361 /* add tex offsets */
1362 if (inst
->Texture
.NumOffsets
) {
1363 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1364 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1365 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
1367 assert(inst
->Texture
.NumOffsets
== 1);
1370 case TGSI_TEXTURE_3D
:
1371 address
[2] = lp_build_add(uint_bld
, address
[2],
1372 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
1374 case TGSI_TEXTURE_2D
:
1375 case TGSI_TEXTURE_SHADOW2D
:
1376 case TGSI_TEXTURE_RECT
:
1377 case TGSI_TEXTURE_SHADOWRECT
:
1378 case TGSI_TEXTURE_2D_ARRAY
:
1379 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1381 lp_build_add(uint_bld
, address
[1],
1382 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
1384 case TGSI_TEXTURE_1D
:
1385 case TGSI_TEXTURE_SHADOW1D
:
1386 case TGSI_TEXTURE_1D_ARRAY
:
1387 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1389 lp_build_add(uint_bld
, address
[0],
1390 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
1392 /* texture offsets do not apply to other texture targets */
1396 emit_data
->dst_type
= LLVMVectorType(
1397 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
1400 emit_data
->arg_count
= 3;
1403 emit_data
->args
[2] = si_shader_ctx
->samplers
[sampler_index
];
1405 emit_data
->dst_type
= LLVMVectorType(
1406 LLVMFloatTypeInContext(bld_base
->base
.gallivm
->context
),
1409 emit_data
->arg_count
= 4;
1413 emit_data
->args
[emit_data
->arg_count
- 1] =
1414 lp_build_const_int32(bld_base
->base
.gallivm
, target
);
1416 /* Pad to power of two vector */
1417 while (count
< util_next_power_of_two(count
))
1418 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1420 emit_data
->args
[0] = lp_build_gather_values(gallivm
, address
, count
);
1423 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1424 struct lp_build_tgsi_context
* bld_base
,
1425 struct lp_build_emit_data
* emit_data
)
1427 struct lp_build_context
* base
= &bld_base
->base
;
1430 sprintf(intr_name
, "%sv%ui32", action
->intr_name
,
1431 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
1433 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1434 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
1435 emit_data
->args
, emit_data
->arg_count
,
1436 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1439 static void txq_fetch_args(
1440 struct lp_build_tgsi_context
* bld_base
,
1441 struct lp_build_emit_data
* emit_data
)
1443 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1444 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1447 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
1450 emit_data
->args
[1] = si_shader_ctx
->resources
[inst
->Src
[1].Register
.Index
];
1453 emit_data
->args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
1454 inst
->Texture
.Texture
);
1456 emit_data
->arg_count
= 3;
1458 emit_data
->dst_type
= LLVMVectorType(
1459 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
1463 #if HAVE_LLVM >= 0x0304
1465 static void si_llvm_emit_ddxy(
1466 const struct lp_build_tgsi_action
* action
,
1467 struct lp_build_tgsi_context
* bld_base
,
1468 struct lp_build_emit_data
* emit_data
)
1470 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1471 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1472 struct lp_build_context
* base
= &bld_base
->base
;
1473 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1474 unsigned opcode
= inst
->Instruction
.Opcode
;
1475 LLVMValueRef indices
[2];
1476 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
1477 LLVMValueRef tl
, trbl
, result
[4];
1479 unsigned swizzle
[4];
1482 i32
= LLVMInt32TypeInContext(gallivm
->context
);
1484 indices
[0] = bld_base
->uint_bld
.zero
;
1485 indices
[1] = build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
1486 NULL
, 0, LLVMReadNoneAttribute
);
1487 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1490 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
1491 lp_build_const_int32(gallivm
, 0xfffffffc), "");
1492 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1495 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
1496 lp_build_const_int32(gallivm
,
1497 opcode
== TGSI_OPCODE_DDX
? 1 : 2),
1499 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
1502 for (c
= 0; c
< 4; ++c
) {
1505 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
1506 for (i
= 0; i
< c
; ++i
) {
1507 if (swizzle
[i
] == swizzle
[c
]) {
1508 result
[c
] = result
[i
];
1515 LLVMBuildStore(gallivm
->builder
,
1516 LLVMBuildBitCast(gallivm
->builder
,
1517 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
1521 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
1522 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
1524 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
1525 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, base
->elem_type
, "");
1527 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
1530 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
1533 #endif /* HAVE_LLVM >= 0x0304 */
1535 static const struct lp_build_tgsi_action tex_action
= {
1536 .fetch_args
= tex_fetch_args
,
1537 .emit
= build_tex_intrinsic
,
1538 .intr_name
= "llvm.SI.sample."
1541 static const struct lp_build_tgsi_action txb_action
= {
1542 .fetch_args
= tex_fetch_args
,
1543 .emit
= build_tex_intrinsic
,
1544 .intr_name
= "llvm.SI.sampleb."
1547 #if HAVE_LLVM >= 0x0304
1548 static const struct lp_build_tgsi_action txd_action
= {
1549 .fetch_args
= tex_fetch_args
,
1550 .emit
= build_tex_intrinsic
,
1551 .intr_name
= "llvm.SI.sampled."
1555 static const struct lp_build_tgsi_action txf_action
= {
1556 .fetch_args
= tex_fetch_args
,
1557 .emit
= build_tex_intrinsic
,
1558 .intr_name
= "llvm.SI.imageload."
1561 static const struct lp_build_tgsi_action txl_action
= {
1562 .fetch_args
= tex_fetch_args
,
1563 .emit
= build_tex_intrinsic
,
1564 .intr_name
= "llvm.SI.samplel."
1567 static const struct lp_build_tgsi_action txq_action
= {
1568 .fetch_args
= txq_fetch_args
,
1569 .emit
= build_tgsi_intrinsic_nomem
,
1570 .intr_name
= "llvm.SI.resinfo"
1573 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
1575 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
1576 LLVMValueRef args
[3];
1578 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
1580 args
[2] = lp_build_const_int32(gallivm
, 1);
1582 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
1585 static void create_function(struct si_shader_context
*si_shader_ctx
)
1587 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
1588 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1589 LLVMTypeRef params
[21], f32
, i8
, i32
, v2i32
, v3i32
;
1590 unsigned i
, last_sgpr
, num_params
;
1592 i8
= LLVMInt8TypeInContext(gallivm
->context
);
1593 i32
= LLVMInt32TypeInContext(gallivm
->context
);
1594 f32
= LLVMFloatTypeInContext(gallivm
->context
);
1595 v2i32
= LLVMVectorType(i32
, 2);
1596 v3i32
= LLVMVectorType(i32
, 3);
1598 params
[SI_PARAM_CONST
] = LLVMPointerType(
1599 LLVMArrayType(LLVMVectorType(i8
, 16), NUM_CONST_BUFFERS
), CONST_ADDR_SPACE
);
1600 /* We assume at most 16 textures per program at the moment.
1601 * This need probably need to be changed to support bindless textures */
1602 params
[SI_PARAM_SAMPLER
] = LLVMPointerType(
1603 LLVMArrayType(LLVMVectorType(i8
, 16), NUM_SAMPLER_VIEWS
), CONST_ADDR_SPACE
);
1604 params
[SI_PARAM_RESOURCE
] = LLVMPointerType(
1605 LLVMArrayType(LLVMVectorType(i8
, 32), NUM_SAMPLER_STATES
), CONST_ADDR_SPACE
);
1607 switch (si_shader_ctx
->type
) {
1608 case TGSI_PROCESSOR_VERTEX
:
1609 params
[SI_PARAM_VERTEX_BUFFER
] = params
[SI_PARAM_CONST
];
1610 params
[SI_PARAM_SO_BUFFER
] = params
[SI_PARAM_CONST
];
1611 params
[SI_PARAM_START_INSTANCE
] = i32
;
1612 num_params
= SI_PARAM_START_INSTANCE
+1;
1614 /* The locations of the other parameters are assigned dynamically. */
1616 /* Streamout SGPRs. */
1617 if (si_shader_ctx
->shader
->selector
->so
.num_outputs
) {
1618 params
[si_shader_ctx
->param_streamout_config
= num_params
++] = i32
;
1619 params
[si_shader_ctx
->param_streamout_write_index
= num_params
++] = i32
;
1621 /* A streamout buffer offset is loaded if the stride is non-zero. */
1622 for (i
= 0; i
< 4; i
++) {
1623 if (!si_shader_ctx
->shader
->selector
->so
.stride
[i
])
1626 params
[si_shader_ctx
->param_streamout_offset
[i
] = num_params
++] = i32
;
1629 last_sgpr
= num_params
-1;
1632 params
[si_shader_ctx
->param_vertex_id
= num_params
++] = i32
;
1633 params
[num_params
++] = i32
; /* unused*/
1634 params
[num_params
++] = i32
; /* unused */
1635 params
[si_shader_ctx
->param_instance_id
= num_params
++] = i32
;
1638 case TGSI_PROCESSOR_FRAGMENT
:
1639 params
[SI_PARAM_ALPHA_REF
] = f32
;
1640 params
[SI_PARAM_PRIM_MASK
] = i32
;
1641 last_sgpr
= SI_PARAM_PRIM_MASK
;
1642 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
1643 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
1644 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
1645 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
1646 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
1647 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
1648 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
1649 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
1650 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
1651 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
1652 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
1653 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
1654 params
[SI_PARAM_FRONT_FACE
] = f32
;
1655 params
[SI_PARAM_ANCILLARY
] = f32
;
1656 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
1657 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
1658 num_params
= SI_PARAM_POS_FIXED_PT
+1;
1662 assert(0 && "unimplemented shader");
1666 assert(num_params
<= Elements(params
));
1667 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, num_params
);
1668 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
1670 for (i
= 0; i
<= last_sgpr
; ++i
) {
1671 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
1674 LLVMAddAttribute(P
, LLVMInRegAttribute
);
1676 #if HAVE_LLVM >= 0x0304
1677 /* We tell llvm that array inputs are passed by value to allow Sinking pass
1678 * to move load. Inputs are constant so this is fine. */
1679 case SI_PARAM_CONST
:
1680 case SI_PARAM_SAMPLER
:
1681 case SI_PARAM_RESOURCE
:
1682 LLVMAddAttribute(P
, LLVMByValAttribute
);
1688 #if HAVE_LLVM >= 0x0304
1689 if (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
1690 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0)
1691 si_shader_ctx
->ddxy_lds
=
1692 LLVMAddGlobalInAddressSpace(gallivm
->module
,
1693 LLVMArrayType(i32
, 64),
1699 static void preload_constants(struct si_shader_context
*si_shader_ctx
)
1701 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
1702 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1703 const struct tgsi_shader_info
* info
= bld_base
->info
;
1705 unsigned i
, num_const
= info
->file_max
[TGSI_FILE_CONSTANT
] + 1;
1712 /* Allocate space for the constant values */
1713 si_shader_ctx
->constants
= CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
1715 /* Load the resource descriptor */
1716 ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
1717 si_shader_ctx
->const_resource
= build_indexed_load(si_shader_ctx
, ptr
, bld_base
->uint_bld
.zero
);
1719 /* Load the constants, we rely on the code sinking to do the rest */
1720 for (i
= 0; i
< num_const
* 4; ++i
) {
1721 LLVMValueRef args
[2] = {
1722 si_shader_ctx
->const_resource
,
1723 lp_build_const_int32(gallivm
, i
* 4)
1725 si_shader_ctx
->constants
[i
] = build_intrinsic(gallivm
->builder
, "llvm.SI.load.const",
1726 bld_base
->base
.elem_type
, args
, 2, LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1730 static void preload_samplers(struct si_shader_context
*si_shader_ctx
)
1732 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
1733 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1734 const struct tgsi_shader_info
* info
= bld_base
->info
;
1736 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
1738 LLVMValueRef res_ptr
, samp_ptr
;
1739 LLVMValueRef offset
;
1741 if (num_samplers
== 0)
1744 /* Allocate space for the values */
1745 si_shader_ctx
->resources
= CALLOC(NUM_SAMPLER_VIEWS
, sizeof(LLVMValueRef
));
1746 si_shader_ctx
->samplers
= CALLOC(num_samplers
, sizeof(LLVMValueRef
));
1748 res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
1749 samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
1751 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1752 for (i
= 0; i
< num_samplers
; ++i
) {
1754 offset
= lp_build_const_int32(gallivm
, i
);
1755 si_shader_ctx
->resources
[i
] = build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
1758 offset
= lp_build_const_int32(gallivm
, i
);
1759 si_shader_ctx
->samplers
[i
] = build_indexed_load(si_shader_ctx
, samp_ptr
, offset
);
1761 /* FMASK resource */
1762 if (info
->is_msaa_sampler
[i
]) {
1763 offset
= lp_build_const_int32(gallivm
, FMASK_TEX_OFFSET
+ i
);
1764 si_shader_ctx
->resources
[FMASK_TEX_OFFSET
+ i
] =
1765 build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
1770 static void preload_streamout_buffers(struct si_shader_context
*si_shader_ctx
)
1772 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
1773 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
1776 if (!si_shader_ctx
->shader
->selector
->so
.num_outputs
)
1779 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1780 SI_PARAM_SO_BUFFER
);
1782 /* Load the resources, we rely on the code sinking to do the rest */
1783 for (i
= 0; i
< 4; ++i
) {
1784 if (si_shader_ctx
->shader
->selector
->so
.stride
[i
]) {
1785 LLVMValueRef offset
= lp_build_const_int32(gallivm
, i
);
1787 si_shader_ctx
->so_buffers
[i
] = build_indexed_load(si_shader_ctx
, buf_ptr
, offset
);
1792 int si_compile_llvm(struct r600_context
*rctx
, struct si_pipe_shader
*shader
,
1797 struct radeon_llvm_binary binary
;
1798 bool dump
= r600_can_dump_shader(&rctx
->screen
->b
,
1799 shader
->selector
? shader
->selector
->tokens
: NULL
);
1800 memset(&binary
, 0, sizeof(binary
));
1801 radeon_llvm_compile(mod
, &binary
,
1802 r600_get_llvm_processor_name(rctx
->screen
->b
.family
), dump
);
1803 if (dump
&& ! binary
.disassembled
) {
1804 fprintf(stderr
, "SI CODE:\n");
1805 for (i
= 0; i
< binary
.code_size
; i
+=4 ) {
1806 fprintf(stderr
, "%02x%02x%02x%02x\n", binary
.code
[i
+ 3],
1807 binary
.code
[i
+ 2], binary
.code
[i
+ 1],
1812 /* XXX: We may be able to emit some of these values directly rather than
1813 * extracting fields to be emitted later.
1815 for (i
= 0; i
< binary
.config_size
; i
+= 8) {
1816 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
));
1817 unsigned value
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
+ 4));
1819 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
1820 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
1821 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
1822 case R_00B848_COMPUTE_PGM_RSRC1
:
1823 shader
->num_sgprs
= (G_00B028_SGPRS(value
) + 1) * 8;
1824 shader
->num_vgprs
= (G_00B028_VGPRS(value
) + 1) * 4;
1826 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
1827 shader
->lds_size
= G_00B02C_EXTRA_LDS_SIZE(value
);
1829 case R_00B84C_COMPUTE_PGM_RSRC2
:
1830 shader
->lds_size
= G_00B84C_LDS_SIZE(value
);
1832 case R_0286CC_SPI_PS_INPUT_ENA
:
1833 shader
->spi_ps_input_ena
= value
;
1836 fprintf(stderr
, "Warning: Compiler emitted unknown "
1837 "config register: 0x%x\n", reg
);
1842 /* copy new shader */
1843 r600_resource_reference(&shader
->bo
, NULL
);
1844 shader
->bo
= r600_resource_create_custom(rctx
->b
.b
.screen
, PIPE_USAGE_IMMUTABLE
,
1846 if (shader
->bo
== NULL
) {
1850 ptr
= (uint32_t*)rctx
->b
.ws
->buffer_map(shader
->bo
->cs_buf
, rctx
->b
.rings
.gfx
.cs
, PIPE_TRANSFER_WRITE
);
1851 if (0 /*R600_BIG_ENDIAN*/) {
1852 for (i
= 0; i
< binary
.code_size
/ 4; ++i
) {
1853 ptr
[i
] = util_bswap32(*(uint32_t*)(binary
.code
+ i
*4));
1856 memcpy(ptr
, binary
.code
, binary
.code_size
);
1858 rctx
->b
.ws
->buffer_unmap(shader
->bo
->cs_buf
);
1861 free(binary
.config
);
1866 int si_pipe_shader_create(
1867 struct pipe_context
*ctx
,
1868 struct si_pipe_shader
*shader
)
1870 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
1871 struct si_pipe_shader_selector
*sel
= shader
->selector
;
1872 struct si_shader_context si_shader_ctx
;
1873 struct tgsi_shader_info shader_info
;
1874 struct lp_build_tgsi_context
* bld_base
;
1877 bool dump
= r600_can_dump_shader(&rctx
->screen
->b
, shader
->selector
->tokens
);
1879 assert(shader
->shader
.noutput
== 0);
1880 assert(shader
->shader
.ninterp
== 0);
1881 assert(shader
->shader
.ninput
== 0);
1883 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
1884 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
1885 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
1887 tgsi_scan_shader(sel
->tokens
, &shader_info
);
1889 shader
->shader
.uses_kill
= shader_info
.uses_kill
;
1890 shader
->shader
.uses_instanceid
= shader_info
.uses_instanceid
;
1891 bld_base
->info
= &shader_info
;
1892 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
1893 bld_base
->emit_epilogue
= si_llvm_emit_epilogue
;
1895 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
1896 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = txb_action
;
1897 #if HAVE_LLVM >= 0x0304
1898 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = txd_action
;
1900 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = txf_action
;
1901 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = txl_action
;
1902 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
1903 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = txq_action
;
1905 #if HAVE_LLVM >= 0x0304
1906 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
1907 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
1910 si_shader_ctx
.radeon_bld
.load_input
= declare_input
;
1911 si_shader_ctx
.radeon_bld
.load_system_value
= declare_system_value
;
1912 si_shader_ctx
.tokens
= sel
->tokens
;
1913 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
1914 si_shader_ctx
.shader
= shader
;
1915 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
1917 create_meta_data(&si_shader_ctx
);
1918 create_function(&si_shader_ctx
);
1919 preload_constants(&si_shader_ctx
);
1920 preload_samplers(&si_shader_ctx
);
1921 preload_streamout_buffers(&si_shader_ctx
);
1923 shader
->shader
.nr_cbufs
= rctx
->framebuffer
.nr_cbufs
;
1925 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1926 * conversion fails. */
1928 tgsi_dump(sel
->tokens
, 0);
1929 si_dump_streamout(&sel
->so
);
1932 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
1933 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
1934 FREE(si_shader_ctx
.constants
);
1935 FREE(si_shader_ctx
.resources
);
1936 FREE(si_shader_ctx
.samplers
);
1940 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
1942 mod
= bld_base
->base
.gallivm
->module
;
1943 r
= si_compile_llvm(rctx
, shader
, mod
);
1945 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
1946 tgsi_parse_free(&si_shader_ctx
.parse
);
1948 FREE(si_shader_ctx
.constants
);
1949 FREE(si_shader_ctx
.resources
);
1950 FREE(si_shader_ctx
.samplers
);
1955 void si_pipe_shader_destroy(struct pipe_context
*ctx
, struct si_pipe_shader
*shader
)
1957 r600_resource_reference(&shader
->bo
, NULL
);