2 * Copyright 2012 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
29 #include "gallivm/lp_bld_const.h"
30 #include "gallivm/lp_bld_gather.h"
31 #include "gallivm/lp_bld_intr.h"
32 #include "gallivm/lp_bld_logic.h"
33 #include "gallivm/lp_bld_arit.h"
34 #include "gallivm/lp_bld_flow.h"
35 #include "radeon/radeon_llvm.h"
36 #include "radeon/radeon_llvm_emit.h"
37 #include "util/u_memory.h"
38 #include "tgsi/tgsi_parse.h"
39 #include "tgsi/tgsi_util.h"
40 #include "tgsi/tgsi_dump.h"
43 #include "si_shader.h"
48 struct si_shader_output_values
50 LLVMValueRef values
[4];
55 struct si_shader_context
57 struct radeon_llvm_context radeon_bld
;
58 struct tgsi_parse_context parse
;
59 struct tgsi_token
* tokens
;
60 struct si_shader
*shader
;
61 unsigned type
; /* TGSI_PROCESSOR_* specifies the type of shader. */
62 int param_streamout_config
;
63 int param_streamout_write_index
;
64 int param_streamout_offset
[4];
66 int param_instance_id
;
67 LLVMValueRef const_md
;
68 LLVMValueRef const_resource
[SI_NUM_CONST_BUFFERS
];
69 LLVMValueRef ddxy_lds
;
70 LLVMValueRef
*constants
[SI_NUM_CONST_BUFFERS
];
71 LLVMValueRef resources
[SI_NUM_SAMPLER_VIEWS
];
72 LLVMValueRef samplers
[SI_NUM_SAMPLER_STATES
];
73 LLVMValueRef so_buffers
[4];
74 LLVMValueRef gs_next_vertex
;
77 static struct si_shader_context
* si_shader_context(
78 struct lp_build_tgsi_context
* bld_base
)
80 return (struct si_shader_context
*)bld_base
;
84 #define PERSPECTIVE_BASE 0
87 #define SAMPLE_OFFSET 0
88 #define CENTER_OFFSET 2
89 #define CENTROID_OFSET 4
91 #define USE_SGPR_MAX_SUFFIX_LEN 5
92 #define CONST_ADDR_SPACE 2
93 #define LOCAL_ADDR_SPACE 3
94 #define USER_SGPR_ADDR_SPACE 8
98 #define SENDMSG_GS_DONE 3
100 #define SENDMSG_GS_OP_NOP (0 << 4)
101 #define SENDMSG_GS_OP_CUT (1 << 4)
102 #define SENDMSG_GS_OP_EMIT (2 << 4)
103 #define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
106 * Returns a unique index for a semantic name and index. The index must be
107 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
110 unsigned si_shader_io_get_unique_index(unsigned semantic_name
, unsigned index
)
112 switch (semantic_name
) {
113 case TGSI_SEMANTIC_POSITION
:
115 case TGSI_SEMANTIC_PSIZE
:
117 case TGSI_SEMANTIC_CLIPDIST
:
120 case TGSI_SEMANTIC_CLIPVERTEX
:
122 case TGSI_SEMANTIC_COLOR
:
125 case TGSI_SEMANTIC_BCOLOR
:
128 case TGSI_SEMANTIC_FOG
:
130 case TGSI_SEMANTIC_EDGEFLAG
:
132 case TGSI_SEMANTIC_GENERIC
:
133 assert(index
<= 63-11);
142 * Given a semantic name and index of a parameter and a mask of used parameters
143 * (inputs or outputs), return the index of the parameter in the list of all
146 * For example, assume this list of parameters:
147 * POSITION, PSIZE, GENERIC0, GENERIC2
148 * which has the mask:
151 * querying POSITION returns 0,
152 * querying PSIZE returns 1,
153 * querying GENERIC0 returns 2,
154 * querying GENERIC2 returns 3.
156 * Which can be used as an offset to a parameter buffer in units of vec4s.
158 static int get_param_index(unsigned semantic_name
, unsigned index
,
161 unsigned unique_index
= si_shader_io_get_unique_index(semantic_name
, index
);
162 int i
, param_index
= 0;
164 /* If not present... */
165 if (!((1llu << unique_index
) & mask
))
168 for (i
= 0; mask
; i
++) {
169 uint64_t bit
= 1llu << i
;
172 if (i
== unique_index
)
180 assert(!"unreachable");
185 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
187 * @param offset The offset parameter specifies the number of
188 * elements to offset, not the number of bytes or dwords. An element is the
189 * the type pointed to by the base_ptr parameter (e.g. int is the element of
192 * When LLVM lowers the load instruction, it will convert the element offset
193 * into a dword offset automatically.
196 static LLVMValueRef
build_indexed_load(
197 struct si_shader_context
* si_shader_ctx
,
198 LLVMValueRef base_ptr
,
201 struct lp_build_context
* base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
;
203 LLVMValueRef indices
[2] = {
204 LLVMConstInt(LLVMInt64TypeInContext(base
->gallivm
->context
), 0, false),
207 LLVMValueRef computed_ptr
= LLVMBuildGEP(
208 base
->gallivm
->builder
, base_ptr
, indices
, 2, "");
210 LLVMValueRef result
= LLVMBuildLoad(base
->gallivm
->builder
, computed_ptr
, "");
211 LLVMSetMetadata(result
, 1, si_shader_ctx
->const_md
);
215 static LLVMValueRef
get_instance_index_for_fetch(
216 struct radeon_llvm_context
* radeon_bld
,
219 struct si_shader_context
*si_shader_ctx
=
220 si_shader_context(&radeon_bld
->soa
.bld_base
);
221 struct gallivm_state
* gallivm
= radeon_bld
->soa
.bld_base
.base
.gallivm
;
223 LLVMValueRef result
= LLVMGetParam(radeon_bld
->main_fn
,
224 si_shader_ctx
->param_instance_id
);
225 result
= LLVMBuildAdd(gallivm
->builder
, result
, LLVMGetParam(
226 radeon_bld
->main_fn
, SI_PARAM_START_INSTANCE
), "");
229 result
= LLVMBuildUDiv(gallivm
->builder
, result
,
230 lp_build_const_int32(gallivm
, divisor
), "");
235 static void declare_input_vs(
236 struct radeon_llvm_context
*radeon_bld
,
237 unsigned input_index
,
238 const struct tgsi_full_declaration
*decl
)
240 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
241 struct gallivm_state
*gallivm
= base
->gallivm
;
242 struct si_shader_context
*si_shader_ctx
=
243 si_shader_context(&radeon_bld
->soa
.bld_base
);
244 unsigned divisor
= si_shader_ctx
->shader
->key
.vs
.instance_divisors
[input_index
];
248 LLVMValueRef t_list_ptr
;
249 LLVMValueRef t_offset
;
251 LLVMValueRef attribute_offset
;
252 LLVMValueRef buffer_index
;
253 LLVMValueRef args
[3];
254 LLVMTypeRef vec4_type
;
257 /* Load the T list */
258 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_VERTEX_BUFFER
);
260 t_offset
= lp_build_const_int32(gallivm
, input_index
);
262 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
, t_offset
);
264 /* Build the attribute offset */
265 attribute_offset
= lp_build_const_int32(gallivm
, 0);
268 /* Build index from instance ID, start instance and divisor */
269 si_shader_ctx
->shader
->uses_instanceid
= true;
270 buffer_index
= get_instance_index_for_fetch(&si_shader_ctx
->radeon_bld
, divisor
);
272 /* Load the buffer index for vertices. */
273 LLVMValueRef vertex_id
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
274 si_shader_ctx
->param_vertex_id
);
275 LLVMValueRef base_vertex
= LLVMGetParam(radeon_bld
->main_fn
,
276 SI_PARAM_BASE_VERTEX
);
277 buffer_index
= LLVMBuildAdd(gallivm
->builder
, base_vertex
, vertex_id
, "");
280 vec4_type
= LLVMVectorType(base
->elem_type
, 4);
282 args
[1] = attribute_offset
;
283 args
[2] = buffer_index
;
284 input
= build_intrinsic(gallivm
->builder
,
285 "llvm.SI.vs.load.input", vec4_type
, args
, 3,
286 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
288 /* Break up the vec4 into individual components */
289 for (chan
= 0; chan
< 4; chan
++) {
290 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
291 /* XXX: Use a helper function for this. There is one in
293 si_shader_ctx
->radeon_bld
.inputs
[radeon_llvm_reg_index_soa(input_index
, chan
)] =
294 LLVMBuildExtractElement(gallivm
->builder
,
295 input
, llvm_chan
, "");
299 static LLVMValueRef
fetch_input_gs(
300 struct lp_build_tgsi_context
*bld_base
,
301 const struct tgsi_full_src_register
*reg
,
302 enum tgsi_opcode_type type
,
305 struct lp_build_context
*base
= &bld_base
->base
;
306 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
307 struct si_shader
*shader
= si_shader_ctx
->shader
;
308 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
309 struct gallivm_state
*gallivm
= base
->gallivm
;
310 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
311 LLVMValueRef vtx_offset
;
312 LLVMValueRef t_list_ptr
;
314 LLVMValueRef args
[9];
315 unsigned vtx_offset_param
;
316 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
317 unsigned semantic_name
= info
->input_semantic_name
[reg
->Register
.Index
];
318 unsigned semantic_index
= info
->input_semantic_index
[reg
->Register
.Index
];
320 if (swizzle
!= ~0 && semantic_name
== TGSI_SEMANTIC_PRIMID
) {
322 return LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
323 SI_PARAM_PRIMITIVE_ID
);
328 if (!reg
->Register
.Dimension
)
332 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
334 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
335 values
[chan
] = fetch_input_gs(bld_base
, reg
, type
, chan
);
337 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
341 /* Get the vertex offset parameter */
342 vtx_offset_param
= reg
->Dimension
.Index
;
343 if (vtx_offset_param
< 2) {
344 vtx_offset_param
+= SI_PARAM_VTX0_OFFSET
;
346 assert(vtx_offset_param
< 6);
347 vtx_offset_param
+= SI_PARAM_VTX2_OFFSET
- 2;
349 vtx_offset
= lp_build_mul_imm(uint
,
350 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
354 /* Load the ESGS ring resource descriptor */
355 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
356 SI_PARAM_RW_BUFFERS
);
357 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
358 lp_build_const_int32(gallivm
, SI_RING_ESGS
));
361 args
[1] = vtx_offset
;
362 args
[2] = lp_build_const_int32(gallivm
,
363 (get_param_index(semantic_name
, semantic_index
,
364 shader
->selector
->gs_used_inputs
) * 4 +
366 args
[3] = uint
->zero
;
367 args
[4] = uint
->one
; /* OFFEN */
368 args
[5] = uint
->zero
; /* IDXEN */
369 args
[6] = uint
->one
; /* GLC */
370 args
[7] = uint
->zero
; /* SLC */
371 args
[8] = uint
->zero
; /* TFE */
373 return LLVMBuildBitCast(gallivm
->builder
,
374 build_intrinsic(gallivm
->builder
,
375 "llvm.SI.buffer.load.dword.i32.i32",
377 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
378 tgsi2llvmtype(bld_base
, type
), "");
381 static void declare_input_fs(
382 struct radeon_llvm_context
*radeon_bld
,
383 unsigned input_index
,
384 const struct tgsi_full_declaration
*decl
)
386 struct lp_build_context
*base
= &radeon_bld
->soa
.bld_base
.base
;
387 struct si_shader_context
*si_shader_ctx
=
388 si_shader_context(&radeon_bld
->soa
.bld_base
);
389 struct si_shader
*shader
= si_shader_ctx
->shader
;
390 struct lp_build_context
*uint
= &radeon_bld
->soa
.bld_base
.uint_bld
;
391 struct gallivm_state
*gallivm
= base
->gallivm
;
392 LLVMTypeRef input_type
= LLVMFloatTypeInContext(gallivm
->context
);
393 LLVMValueRef main_fn
= radeon_bld
->main_fn
;
395 LLVMValueRef interp_param
;
396 const char * intr_name
;
399 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
400 * quad begins a new primitive. Bit 0 always needs
402 * [32:16] ParamOffset
405 LLVMValueRef params
= LLVMGetParam(main_fn
, SI_PARAM_PRIM_MASK
);
406 LLVMValueRef attr_number
;
410 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
411 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
413 radeon_llvm_reg_index_soa(input_index
, chan
);
414 radeon_bld
->inputs
[soa_index
] =
415 LLVMGetParam(main_fn
, SI_PARAM_POS_X_FLOAT
+ chan
);
418 /* RCP for fragcoord.w */
419 radeon_bld
->inputs
[soa_index
] =
420 LLVMBuildFDiv(gallivm
->builder
,
421 lp_build_const_float(gallivm
, 1.0f
),
422 radeon_bld
->inputs
[soa_index
],
428 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FACE
) {
429 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
430 LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
431 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
432 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
433 lp_build_const_float(gallivm
, 0.0f
);
434 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
435 lp_build_const_float(gallivm
, 1.0f
);
440 shader
->ps_input_param_offset
[input_index
] = shader
->nparam
++;
441 attr_number
= lp_build_const_int32(gallivm
,
442 shader
->ps_input_param_offset
[input_index
]);
444 switch (decl
->Interp
.Interpolate
) {
445 case TGSI_INTERPOLATE_CONSTANT
:
448 case TGSI_INTERPOLATE_LINEAR
:
449 if (decl
->Interp
.Location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
450 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_SAMPLE
);
451 else if (decl
->Interp
.Location
== TGSI_INTERPOLATE_LOC_CENTROID
)
452 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTROID
);
454 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_LINEAR_CENTER
);
456 case TGSI_INTERPOLATE_COLOR
:
457 if (si_shader_ctx
->shader
->key
.ps
.flatshade
) {
461 /* fall through to perspective */
462 case TGSI_INTERPOLATE_PERSPECTIVE
:
463 if (decl
->Interp
.Location
== TGSI_INTERPOLATE_LOC_SAMPLE
)
464 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_SAMPLE
);
465 else if (decl
->Interp
.Location
== TGSI_INTERPOLATE_LOC_CENTROID
)
466 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTROID
);
468 interp_param
= LLVMGetParam(main_fn
, SI_PARAM_PERSP_CENTER
);
471 fprintf(stderr
, "Warning: Unhandled interpolation mode.\n");
475 intr_name
= interp_param
? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
477 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
478 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
479 si_shader_ctx
->shader
->key
.ps
.color_two_side
) {
480 LLVMValueRef args
[4];
481 LLVMValueRef face
, is_face_positive
;
482 LLVMValueRef back_attr_number
=
483 lp_build_const_int32(gallivm
,
484 shader
->ps_input_param_offset
[input_index
] + 1);
486 face
= LLVMGetParam(main_fn
, SI_PARAM_FRONT_FACE
);
488 is_face_positive
= LLVMBuildFCmp(gallivm
->builder
,
490 lp_build_const_float(gallivm
, 0.0f
),
494 args
[3] = interp_param
;
495 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
496 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
497 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
498 LLVMValueRef front
, back
;
501 args
[1] = attr_number
;
502 front
= build_intrinsic(gallivm
->builder
, intr_name
,
503 input_type
, args
, args
[3] ? 4 : 3,
504 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
506 args
[1] = back_attr_number
;
507 back
= build_intrinsic(gallivm
->builder
, intr_name
,
508 input_type
, args
, args
[3] ? 4 : 3,
509 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
511 radeon_bld
->inputs
[soa_index
] =
512 LLVMBuildSelect(gallivm
->builder
,
520 } else if (decl
->Semantic
.Name
== TGSI_SEMANTIC_FOG
) {
521 LLVMValueRef args
[4];
523 args
[0] = uint
->zero
;
524 args
[1] = attr_number
;
526 args
[3] = interp_param
;
527 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 0)] =
528 build_intrinsic(gallivm
->builder
, intr_name
,
529 input_type
, args
, args
[3] ? 4 : 3,
530 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
531 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 1)] =
532 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 2)] =
533 lp_build_const_float(gallivm
, 0.0f
);
534 radeon_bld
->inputs
[radeon_llvm_reg_index_soa(input_index
, 3)] =
535 lp_build_const_float(gallivm
, 1.0f
);
537 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
538 LLVMValueRef args
[4];
539 LLVMValueRef llvm_chan
= lp_build_const_int32(gallivm
, chan
);
540 unsigned soa_index
= radeon_llvm_reg_index_soa(input_index
, chan
);
542 args
[1] = attr_number
;
544 args
[3] = interp_param
;
545 radeon_bld
->inputs
[soa_index
] =
546 build_intrinsic(gallivm
->builder
, intr_name
,
547 input_type
, args
, args
[3] ? 4 : 3,
548 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
553 static LLVMValueRef
get_sample_id(struct radeon_llvm_context
*radeon_bld
)
555 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
556 LLVMValueRef value
= LLVMGetParam(radeon_bld
->main_fn
,
558 value
= LLVMBuildLShr(gallivm
->builder
, value
,
559 lp_build_const_int32(gallivm
, 8), "");
560 value
= LLVMBuildAnd(gallivm
->builder
, value
,
561 lp_build_const_int32(gallivm
, 0xf), "");
565 static LLVMValueRef
load_const(LLVMBuilderRef builder
, LLVMValueRef resource
,
566 LLVMValueRef offset
, LLVMTypeRef return_type
)
568 LLVMValueRef args
[2] = {resource
, offset
};
570 return build_intrinsic(builder
, "llvm.SI.load.const", return_type
, args
, 2,
571 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
574 static void declare_system_value(
575 struct radeon_llvm_context
* radeon_bld
,
577 const struct tgsi_full_declaration
*decl
)
579 struct si_shader_context
*si_shader_ctx
=
580 si_shader_context(&radeon_bld
->soa
.bld_base
);
581 struct lp_build_context
*uint_bld
= &radeon_bld
->soa
.bld_base
.uint_bld
;
582 struct gallivm_state
*gallivm
= &radeon_bld
->gallivm
;
583 LLVMValueRef value
= 0;
585 switch (decl
->Semantic
.Name
) {
586 case TGSI_SEMANTIC_INSTANCEID
:
587 value
= LLVMGetParam(radeon_bld
->main_fn
,
588 si_shader_ctx
->param_instance_id
);
591 case TGSI_SEMANTIC_VERTEXID
:
592 value
= LLVMGetParam(radeon_bld
->main_fn
,
593 si_shader_ctx
->param_vertex_id
);
596 case TGSI_SEMANTIC_SAMPLEID
:
597 value
= get_sample_id(radeon_bld
);
600 case TGSI_SEMANTIC_SAMPLEPOS
:
602 LLVMBuilderRef builder
= gallivm
->builder
;
603 LLVMValueRef desc
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
604 LLVMValueRef buf_index
= lp_build_const_int32(gallivm
, SI_DRIVER_STATE_CONST_BUF
);
605 LLVMValueRef resource
= build_indexed_load(si_shader_ctx
, desc
, buf_index
);
607 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
608 LLVMValueRef offset0
= lp_build_mul_imm(uint_bld
, get_sample_id(radeon_bld
), 8);
609 LLVMValueRef offset1
= LLVMBuildAdd(builder
, offset0
, lp_build_const_int32(gallivm
, 4), "");
611 LLVMValueRef pos
[4] = {
612 load_const(builder
, resource
, offset0
, radeon_bld
->soa
.bld_base
.base
.elem_type
),
613 load_const(builder
, resource
, offset1
, radeon_bld
->soa
.bld_base
.base
.elem_type
),
614 lp_build_const_float(gallivm
, 0),
615 lp_build_const_float(gallivm
, 0)
617 value
= lp_build_gather_values(gallivm
, pos
, 4);
622 assert(!"unknown system value");
626 radeon_bld
->system_values
[index
] = value
;
629 static LLVMValueRef
fetch_constant(
630 struct lp_build_tgsi_context
* bld_base
,
631 const struct tgsi_full_src_register
*reg
,
632 enum tgsi_opcode_type type
,
635 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
636 struct lp_build_context
* base
= &bld_base
->base
;
637 const struct tgsi_ind_register
*ireg
= ®
->Indirect
;
643 if (swizzle
== LP_CHAN_ALL
) {
645 LLVMValueRef values
[4];
646 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
)
647 values
[chan
] = fetch_constant(bld_base
, reg
, type
, chan
);
649 return lp_build_gather_values(bld_base
->base
.gallivm
, values
, 4);
652 buf
= reg
->Register
.Dimension
? reg
->Dimension
.Index
: 0;
653 idx
= reg
->Register
.Index
* 4 + swizzle
;
655 if (!reg
->Register
.Indirect
)
656 return bitcast(bld_base
, type
, si_shader_ctx
->constants
[buf
][idx
]);
658 addr
= si_shader_ctx
->radeon_bld
.soa
.addr
[ireg
->Index
][ireg
->Swizzle
];
659 addr
= LLVMBuildLoad(base
->gallivm
->builder
, addr
, "load addr reg");
660 addr
= lp_build_mul_imm(&bld_base
->uint_bld
, addr
, 16);
661 addr
= lp_build_add(&bld_base
->uint_bld
, addr
,
662 lp_build_const_int32(base
->gallivm
, idx
* 4));
664 result
= load_const(base
->gallivm
->builder
, si_shader_ctx
->const_resource
[buf
],
665 addr
, base
->elem_type
);
667 return bitcast(bld_base
, type
, result
);
670 /* Initialize arguments for the shader export intrinsic */
671 static void si_llvm_init_export_args(struct lp_build_tgsi_context
*bld_base
,
672 LLVMValueRef
*values
,
676 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
677 struct lp_build_context
*uint
=
678 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
679 struct lp_build_context
*base
= &bld_base
->base
;
680 unsigned compressed
= 0;
683 if (si_shader_ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
684 int cbuf
= target
- V_008DFC_SQ_EXP_MRT
;
686 if (cbuf
>= 0 && cbuf
< 8) {
687 compressed
= (si_shader_ctx
->shader
->key
.ps
.export_16bpc
>> cbuf
) & 0x1;
690 si_shader_ctx
->shader
->spi_shader_col_format
|=
691 V_028714_SPI_SHADER_FP16_ABGR
<< (4 * cbuf
);
693 si_shader_ctx
->shader
->spi_shader_col_format
|=
694 V_028714_SPI_SHADER_32_ABGR
<< (4 * cbuf
);
696 si_shader_ctx
->shader
->cb_shader_mask
|= 0xf << (4 * cbuf
);
701 /* Pixel shader needs to pack output values before export */
702 for (chan
= 0; chan
< 2; chan
++ ) {
703 args
[0] = values
[2 * chan
];
704 args
[1] = values
[2 * chan
+ 1];
706 build_intrinsic(base
->gallivm
->builder
,
708 LLVMInt32TypeInContext(base
->gallivm
->context
),
710 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
711 args
[chan
+ 7] = args
[chan
+ 5] =
712 LLVMBuildBitCast(base
->gallivm
->builder
,
714 LLVMFloatTypeInContext(base
->gallivm
->context
),
721 for (chan
= 0; chan
< 4; chan
++ )
722 /* +5 because the first output value will be
723 * the 6th argument to the intrinsic. */
724 args
[chan
+ 5] = values
[chan
];
726 /* Clear COMPR flag */
727 args
[4] = uint
->zero
;
730 /* XXX: This controls which components of the output
731 * registers actually get exported. (e.g bit 0 means export
732 * X component, bit 1 means export Y component, etc.) I'm
733 * hard coding this to 0xf for now. In the future, we might
734 * want to do something else. */
735 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
737 /* Specify whether the EXEC mask represents the valid mask */
738 args
[1] = uint
->zero
;
740 /* Specify whether this is the last export */
741 args
[2] = uint
->zero
;
743 /* Specify the target we are exporting */
744 args
[3] = lp_build_const_int32(base
->gallivm
, target
);
746 /* XXX: We probably need to keep track of the output
747 * values, so we know what we are passing to the next
751 /* Load from output pointers and initialize arguments for the shader export intrinsic */
752 static void si_llvm_init_export_args_load(struct lp_build_tgsi_context
*bld_base
,
753 LLVMValueRef
*out_ptr
,
757 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
758 LLVMValueRef values
[4];
761 for (i
= 0; i
< 4; i
++)
762 values
[i
] = LLVMBuildLoad(gallivm
->builder
, out_ptr
[i
], "");
764 si_llvm_init_export_args(bld_base
, values
, target
, args
);
767 static void si_alpha_test(struct lp_build_tgsi_context
*bld_base
,
768 LLVMValueRef
*out_ptr
)
770 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
771 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
773 if (si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_NEVER
) {
774 LLVMValueRef alpha_ref
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
777 LLVMValueRef alpha_pass
=
778 lp_build_cmp(&bld_base
->base
,
779 si_shader_ctx
->shader
->key
.ps
.alpha_func
,
780 LLVMBuildLoad(gallivm
->builder
, out_ptr
[3], ""),
783 lp_build_select(&bld_base
->base
,
785 lp_build_const_float(gallivm
, 1.0f
),
786 lp_build_const_float(gallivm
, -1.0f
));
788 build_intrinsic(gallivm
->builder
,
790 LLVMVoidTypeInContext(gallivm
->context
),
793 build_intrinsic(gallivm
->builder
,
795 LLVMVoidTypeInContext(gallivm
->context
),
799 si_shader_ctx
->shader
->db_shader_control
|= S_02880C_KILL_ENABLE(1);
802 static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context
* bld_base
,
803 LLVMValueRef (*pos
)[9], LLVMValueRef
*out_elts
)
805 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
806 struct si_shader
*shader
= si_shader_ctx
->shader
;
807 struct lp_build_context
*base
= &bld_base
->base
;
808 struct lp_build_context
*uint
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
812 LLVMValueRef base_elt
;
813 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
814 LLVMValueRef constbuf_index
= lp_build_const_int32(base
->gallivm
, SI_DRIVER_STATE_CONST_BUF
);
815 LLVMValueRef const_resource
= build_indexed_load(si_shader_ctx
, ptr
, constbuf_index
);
817 for (reg_index
= 0; reg_index
< 2; reg_index
++) {
818 LLVMValueRef
*args
= pos
[2 + reg_index
];
820 shader
->clip_dist_write
|= 0xf << (4 * reg_index
);
825 args
[8] = lp_build_const_float(base
->gallivm
, 0.0f
);
827 /* Compute dot products of position and user clip plane vectors */
828 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
829 for (const_chan
= 0; const_chan
< TGSI_NUM_CHANNELS
; const_chan
++) {
830 args
[1] = lp_build_const_int32(base
->gallivm
,
831 ((reg_index
* 4 + chan
) * 4 +
833 base_elt
= load_const(base
->gallivm
->builder
, const_resource
,
834 args
[1], base
->elem_type
);
836 lp_build_add(base
, args
[5 + chan
],
837 lp_build_mul(base
, base_elt
,
838 out_elts
[const_chan
]));
842 args
[0] = lp_build_const_int32(base
->gallivm
, 0xf);
843 args
[1] = uint
->zero
;
844 args
[2] = uint
->zero
;
845 args
[3] = lp_build_const_int32(base
->gallivm
,
846 V_008DFC_SQ_EXP_POS
+ 2 + reg_index
);
847 args
[4] = uint
->zero
;
851 static void si_dump_streamout(struct pipe_stream_output_info
*so
)
856 fprintf(stderr
, "STREAMOUT\n");
858 for (i
= 0; i
< so
->num_outputs
; i
++) {
859 unsigned mask
= ((1 << so
->output
[i
].num_components
) - 1) <<
860 so
->output
[i
].start_component
;
861 fprintf(stderr
, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
862 i
, so
->output
[i
].output_buffer
,
863 so
->output
[i
].dst_offset
, so
->output
[i
].dst_offset
+ so
->output
[i
].num_components
- 1,
864 so
->output
[i
].register_index
,
868 mask
& 8 ? "w" : "");
872 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
873 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
874 * or v4i32 (num_channels=3,4). */
875 static void build_tbuffer_store(struct si_shader_context
*shader
,
878 unsigned num_channels
,
880 LLVMValueRef soffset
,
881 unsigned inst_offset
,
890 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
891 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
892 LLVMValueRef args
[] = {
895 LLVMConstInt(i32
, num_channels
, 0),
898 LLVMConstInt(i32
, inst_offset
, 0),
899 LLVMConstInt(i32
, dfmt
, 0),
900 LLVMConstInt(i32
, nfmt
, 0),
901 LLVMConstInt(i32
, offen
, 0),
902 LLVMConstInt(i32
, idxen
, 0),
903 LLVMConstInt(i32
, glc
, 0),
904 LLVMConstInt(i32
, slc
, 0),
905 LLVMConstInt(i32
, tfe
, 0)
908 /* The instruction offset field has 12 bits */
909 assert(offen
|| inst_offset
< (1 << 12));
911 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
912 unsigned func
= CLAMP(num_channels
, 1, 3) - 1;
913 const char *types
[] = {"i32", "v2i32", "v4i32"};
915 snprintf(name
, sizeof(name
), "llvm.SI.tbuffer.store.%s", types
[func
]);
917 lp_build_intrinsic(gallivm
->builder
, name
,
918 LLVMVoidTypeInContext(gallivm
->context
),
919 args
, Elements(args
));
922 static void build_streamout_store(struct si_shader_context
*shader
,
925 unsigned num_channels
,
927 LLVMValueRef soffset
,
928 unsigned inst_offset
)
930 static unsigned dfmt
[] = {
931 V_008F0C_BUF_DATA_FORMAT_32
,
932 V_008F0C_BUF_DATA_FORMAT_32_32
,
933 V_008F0C_BUF_DATA_FORMAT_32_32_32
,
934 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
936 assert(num_channels
>= 1 && num_channels
<= 4);
938 build_tbuffer_store(shader
, rsrc
, vdata
, num_channels
, vaddr
, soffset
,
939 inst_offset
, dfmt
[num_channels
-1],
940 V_008F0C_BUF_NUM_FORMAT_UINT
, 1, 0, 1, 1, 0);
943 /* On SI, the vertex shader is responsible for writing streamout data
945 static void si_llvm_emit_streamout(struct si_shader_context
*shader
,
946 struct si_shader_output_values
*outputs
,
949 struct pipe_stream_output_info
*so
= &shader
->shader
->selector
->so
;
950 struct gallivm_state
*gallivm
= &shader
->radeon_bld
.gallivm
;
951 LLVMBuilderRef builder
= gallivm
->builder
;
953 struct lp_build_if_state if_ctx
;
955 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
957 LLVMValueRef so_param
=
958 LLVMGetParam(shader
->radeon_bld
.main_fn
,
959 shader
->param_streamout_config
);
961 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
962 LLVMValueRef so_vtx_count
=
963 LLVMBuildAnd(builder
,
964 LLVMBuildLShr(builder
, so_param
,
965 LLVMConstInt(i32
, 16, 0), ""),
966 LLVMConstInt(i32
, 127, 0), "");
968 LLVMValueRef tid
= build_intrinsic(builder
, "llvm.SI.tid", i32
,
969 NULL
, 0, LLVMReadNoneAttribute
);
971 /* can_emit = tid < so_vtx_count; */
972 LLVMValueRef can_emit
=
973 LLVMBuildICmp(builder
, LLVMIntULT
, tid
, so_vtx_count
, "");
975 /* Emit the streamout code conditionally. This actually avoids
976 * out-of-bounds buffer access. The hw tells us via the SGPR
977 * (so_vtx_count) which threads are allowed to emit streamout data. */
978 lp_build_if(&if_ctx
, gallivm
, can_emit
);
980 /* The buffer offset is computed as follows:
981 * ByteOffset = streamout_offset[buffer_id]*4 +
982 * (streamout_write_index + thread_id)*stride[buffer_id] +
986 LLVMValueRef so_write_index
=
987 LLVMGetParam(shader
->radeon_bld
.main_fn
,
988 shader
->param_streamout_write_index
);
990 /* Compute (streamout_write_index + thread_id). */
991 so_write_index
= LLVMBuildAdd(builder
, so_write_index
, tid
, "");
993 /* Compute the write offset for each enabled buffer. */
994 LLVMValueRef so_write_offset
[4] = {};
995 for (i
= 0; i
< 4; i
++) {
999 LLVMValueRef so_offset
= LLVMGetParam(shader
->radeon_bld
.main_fn
,
1000 shader
->param_streamout_offset
[i
]);
1001 so_offset
= LLVMBuildMul(builder
, so_offset
, LLVMConstInt(i32
, 4, 0), "");
1003 so_write_offset
[i
] = LLVMBuildMul(builder
, so_write_index
,
1004 LLVMConstInt(i32
, so
->stride
[i
]*4, 0), "");
1005 so_write_offset
[i
] = LLVMBuildAdd(builder
, so_write_offset
[i
], so_offset
, "");
1008 /* Write streamout data. */
1009 for (i
= 0; i
< so
->num_outputs
; i
++) {
1010 unsigned buf_idx
= so
->output
[i
].output_buffer
;
1011 unsigned reg
= so
->output
[i
].register_index
;
1012 unsigned start
= so
->output
[i
].start_component
;
1013 unsigned num_comps
= so
->output
[i
].num_components
;
1014 LLVMValueRef out
[4];
1016 assert(num_comps
&& num_comps
<= 4);
1017 if (!num_comps
|| num_comps
> 4)
1023 /* Load the output as int. */
1024 for (j
= 0; j
< num_comps
; j
++) {
1025 out
[j
] = LLVMBuildBitCast(builder
,
1026 outputs
[reg
].values
[start
+j
],
1030 /* Pack the output. */
1031 LLVMValueRef vdata
= NULL
;
1033 switch (num_comps
) {
1034 case 1: /* as i32 */
1037 case 2: /* as v2i32 */
1038 case 3: /* as v4i32 (aligned to 4) */
1039 case 4: /* as v4i32 */
1040 vdata
= LLVMGetUndef(LLVMVectorType(i32
, util_next_power_of_two(num_comps
)));
1041 for (j
= 0; j
< num_comps
; j
++) {
1042 vdata
= LLVMBuildInsertElement(builder
, vdata
, out
[j
],
1043 LLVMConstInt(i32
, j
, 0), "");
1048 build_streamout_store(shader
, shader
->so_buffers
[buf_idx
],
1050 so_write_offset
[buf_idx
],
1051 LLVMConstInt(i32
, 0, 0),
1052 so
->output
[i
].dst_offset
*4);
1055 lp_build_endif(&if_ctx
);
1059 /* Generate export instructions for hardware VS shader stage */
1060 static void si_llvm_export_vs(struct lp_build_tgsi_context
*bld_base
,
1061 struct si_shader_output_values
*outputs
,
1064 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1065 struct si_shader
* shader
= si_shader_ctx
->shader
;
1066 struct lp_build_context
* base
= &bld_base
->base
;
1067 struct lp_build_context
* uint
=
1068 &si_shader_ctx
->radeon_bld
.soa
.bld_base
.uint_bld
;
1069 LLVMValueRef args
[9];
1070 LLVMValueRef pos_args
[4][9] = { { 0 } };
1071 LLVMValueRef psize_value
= NULL
, edgeflag_value
= NULL
, layer_value
= NULL
;
1072 unsigned semantic_name
, semantic_index
;
1074 unsigned param_count
= 0;
1078 if (outputs
&& si_shader_ctx
->shader
->selector
->so
.num_outputs
) {
1079 si_llvm_emit_streamout(si_shader_ctx
, outputs
, noutput
);
1082 for (i
= 0; i
< noutput
; i
++) {
1083 semantic_name
= outputs
[i
].name
;
1084 semantic_index
= outputs
[i
].sid
;
1087 /* Select the correct target */
1088 switch(semantic_name
) {
1089 case TGSI_SEMANTIC_PSIZE
:
1090 shader
->vs_out_misc_write
= true;
1091 shader
->vs_out_point_size
= true;
1092 psize_value
= outputs
[i
].values
[0];
1094 case TGSI_SEMANTIC_EDGEFLAG
:
1095 shader
->vs_out_misc_write
= true;
1096 shader
->vs_out_edgeflag
= true;
1097 edgeflag_value
= outputs
[i
].values
[0];
1099 case TGSI_SEMANTIC_LAYER
:
1100 shader
->vs_out_misc_write
= true;
1101 shader
->vs_out_layer
= true;
1102 layer_value
= outputs
[i
].values
[0];
1104 case TGSI_SEMANTIC_POSITION
:
1105 target
= V_008DFC_SQ_EXP_POS
;
1107 case TGSI_SEMANTIC_COLOR
:
1108 case TGSI_SEMANTIC_BCOLOR
:
1109 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1110 shader
->vs_output_param_offset
[i
] = param_count
;
1113 case TGSI_SEMANTIC_CLIPDIST
:
1114 shader
->clip_dist_write
|=
1115 0xf << (semantic_index
* 4);
1116 target
= V_008DFC_SQ_EXP_POS
+ 2 + semantic_index
;
1118 case TGSI_SEMANTIC_CLIPVERTEX
:
1119 si_llvm_emit_clipvertex(bld_base
, pos_args
, outputs
[i
].values
);
1121 case TGSI_SEMANTIC_PRIMID
:
1122 case TGSI_SEMANTIC_FOG
:
1123 case TGSI_SEMANTIC_GENERIC
:
1124 target
= V_008DFC_SQ_EXP_PARAM
+ param_count
;
1125 shader
->vs_output_param_offset
[i
] = param_count
;
1131 "Warning: SI unhandled vs output type:%d\n",
1135 si_llvm_init_export_args(bld_base
, outputs
[i
].values
, target
, args
);
1137 if (target
>= V_008DFC_SQ_EXP_POS
&&
1138 target
<= (V_008DFC_SQ_EXP_POS
+ 3)) {
1139 memcpy(pos_args
[target
- V_008DFC_SQ_EXP_POS
],
1140 args
, sizeof(args
));
1142 lp_build_intrinsic(base
->gallivm
->builder
,
1144 LLVMVoidTypeInContext(base
->gallivm
->context
),
1148 if (semantic_name
== TGSI_SEMANTIC_CLIPDIST
) {
1149 semantic_name
= TGSI_SEMANTIC_GENERIC
;
1150 goto handle_semantic
;
1154 /* We need to add the position output manually if it's missing. */
1155 if (!pos_args
[0][0]) {
1156 pos_args
[0][0] = lp_build_const_int32(base
->gallivm
, 0xf); /* writemask */
1157 pos_args
[0][1] = uint
->zero
; /* EXEC mask */
1158 pos_args
[0][2] = uint
->zero
; /* last export? */
1159 pos_args
[0][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
);
1160 pos_args
[0][4] = uint
->zero
; /* COMPR flag */
1161 pos_args
[0][5] = base
->zero
; /* X */
1162 pos_args
[0][6] = base
->zero
; /* Y */
1163 pos_args
[0][7] = base
->zero
; /* Z */
1164 pos_args
[0][8] = base
->one
; /* W */
1167 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1168 if (shader
->vs_out_misc_write
) {
1169 pos_args
[1][0] = lp_build_const_int32(base
->gallivm
, /* writemask */
1170 shader
->vs_out_point_size
|
1171 (shader
->vs_out_edgeflag
<< 1) |
1172 (shader
->vs_out_layer
<< 2));
1173 pos_args
[1][1] = uint
->zero
; /* EXEC mask */
1174 pos_args
[1][2] = uint
->zero
; /* last export? */
1175 pos_args
[1][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ 1);
1176 pos_args
[1][4] = uint
->zero
; /* COMPR flag */
1177 pos_args
[1][5] = base
->zero
; /* X */
1178 pos_args
[1][6] = base
->zero
; /* Y */
1179 pos_args
[1][7] = base
->zero
; /* Z */
1180 pos_args
[1][8] = base
->zero
; /* W */
1182 if (shader
->vs_out_point_size
)
1183 pos_args
[1][5] = psize_value
;
1185 if (shader
->vs_out_edgeflag
) {
1186 /* The output is a float, but the hw expects an integer
1187 * with the first bit containing the edge flag. */
1188 edgeflag_value
= LLVMBuildFPToUI(base
->gallivm
->builder
,
1190 bld_base
->uint_bld
.elem_type
, "");
1191 edgeflag_value
= lp_build_min(&bld_base
->int_bld
,
1193 bld_base
->int_bld
.one
);
1195 /* The LLVM intrinsic expects a float. */
1196 pos_args
[1][6] = LLVMBuildBitCast(base
->gallivm
->builder
,
1198 base
->elem_type
, "");
1201 if (shader
->vs_out_layer
)
1202 pos_args
[1][7] = layer_value
;
1205 for (i
= 0; i
< 4; i
++)
1207 shader
->nr_pos_exports
++;
1210 for (i
= 0; i
< 4; i
++) {
1211 if (!pos_args
[i
][0])
1214 /* Specify the target we are exporting */
1215 pos_args
[i
][3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_POS
+ pos_idx
++);
1217 if (pos_idx
== shader
->nr_pos_exports
)
1218 /* Specify that this is the last export */
1219 pos_args
[i
][2] = uint
->one
;
1221 lp_build_intrinsic(base
->gallivm
->builder
,
1223 LLVMVoidTypeInContext(base
->gallivm
->context
),
1228 static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context
* bld_base
)
1230 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1231 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1232 struct si_shader
*es
= si_shader_ctx
->shader
;
1233 struct tgsi_shader_info
*info
= &es
->selector
->info
;
1234 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
1235 LLVMValueRef soffset
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1236 SI_PARAM_ES2GS_OFFSET
);
1237 LLVMValueRef t_list_ptr
;
1238 LLVMValueRef t_list
;
1242 /* Load the ESGS ring resource descriptor */
1243 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
1244 SI_PARAM_RW_BUFFERS
);
1245 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
1246 lp_build_const_int32(gallivm
, SI_RING_ESGS
));
1248 for (i
= 0; i
< info
->num_outputs
; i
++) {
1249 LLVMValueRef
*out_ptr
=
1250 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
1251 int param_index
= get_param_index(info
->output_semantic_name
[i
],
1252 info
->output_semantic_index
[i
],
1253 es
->key
.vs
.gs_used_inputs
);
1255 if (param_index
< 0)
1258 for (chan
= 0; chan
< 4; chan
++) {
1259 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
1260 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
1262 build_tbuffer_store(si_shader_ctx
, t_list
, out_val
, 1,
1263 LLVMGetUndef(i32
), soffset
,
1264 (4 * param_index
+ chan
) * 4,
1265 V_008F0C_BUF_DATA_FORMAT_32
,
1266 V_008F0C_BUF_NUM_FORMAT_UINT
,
1272 static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context
*bld_base
)
1274 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1275 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1276 LLVMValueRef args
[2];
1278 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_NOP
| SENDMSG_GS_DONE
);
1279 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
1280 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
1281 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
1282 LLVMNoUnwindAttribute
);
1285 static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context
* bld_base
)
1287 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1288 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1289 struct tgsi_shader_info
*info
= &si_shader_ctx
->shader
->selector
->info
;
1290 struct si_shader_output_values
*outputs
= NULL
;
1293 outputs
= MALLOC(info
->num_outputs
* sizeof(outputs
[0]));
1295 for (i
= 0; i
< info
->num_outputs
; i
++) {
1296 outputs
[i
].name
= info
->output_semantic_name
[i
];
1297 outputs
[i
].sid
= info
->output_semantic_index
[i
];
1299 for (j
= 0; j
< 4; j
++)
1300 outputs
[i
].values
[j
] =
1301 LLVMBuildLoad(gallivm
->builder
,
1302 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][j
],
1306 si_llvm_export_vs(bld_base
, outputs
, info
->num_outputs
);
1310 static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context
* bld_base
)
1312 struct si_shader_context
* si_shader_ctx
= si_shader_context(bld_base
);
1313 struct si_shader
* shader
= si_shader_ctx
->shader
;
1314 struct lp_build_context
* base
= &bld_base
->base
;
1315 struct lp_build_context
* uint
= &bld_base
->uint_bld
;
1316 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
1317 LLVMValueRef args
[9];
1318 LLVMValueRef last_args
[9] = { 0 };
1319 int depth_index
= -1, stencil_index
= -1, samplemask_index
= -1;
1322 for (i
= 0; i
< info
->num_outputs
; i
++) {
1323 unsigned semantic_name
= info
->output_semantic_name
[i
];
1324 unsigned semantic_index
= info
->output_semantic_index
[i
];
1327 /* Select the correct target */
1328 switch (semantic_name
) {
1329 case TGSI_SEMANTIC_POSITION
:
1332 case TGSI_SEMANTIC_STENCIL
:
1335 case TGSI_SEMANTIC_SAMPLEMASK
:
1336 samplemask_index
= i
;
1338 case TGSI_SEMANTIC_COLOR
:
1339 target
= V_008DFC_SQ_EXP_MRT
+ semantic_index
;
1340 if (si_shader_ctx
->shader
->key
.ps
.alpha_to_one
)
1341 LLVMBuildStore(bld_base
->base
.gallivm
->builder
,
1343 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
][3]);
1345 if (semantic_index
== 0 &&
1346 si_shader_ctx
->shader
->key
.ps
.alpha_func
!= PIPE_FUNC_ALWAYS
)
1347 si_alpha_test(bld_base
,
1348 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
]);
1353 "Warning: SI unhandled fs output type:%d\n",
1357 si_llvm_init_export_args_load(bld_base
,
1358 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
],
1361 if (semantic_name
== TGSI_SEMANTIC_COLOR
) {
1362 /* If there is an export instruction waiting to be emitted, do so now. */
1364 lp_build_intrinsic(base
->gallivm
->builder
,
1366 LLVMVoidTypeInContext(base
->gallivm
->context
),
1370 /* This instruction will be emitted at the end of the shader. */
1371 memcpy(last_args
, args
, sizeof(args
));
1373 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1374 if (shader
->selector
->info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
1375 semantic_index
== 0 &&
1376 si_shader_ctx
->shader
->key
.ps
.last_cbuf
> 0) {
1377 for (int c
= 1; c
<= si_shader_ctx
->shader
->key
.ps
.last_cbuf
; c
++) {
1378 si_llvm_init_export_args_load(bld_base
,
1379 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
],
1380 V_008DFC_SQ_EXP_MRT
+ c
, args
);
1381 lp_build_intrinsic(base
->gallivm
->builder
,
1383 LLVMVoidTypeInContext(base
->gallivm
->context
),
1388 lp_build_intrinsic(base
->gallivm
->builder
,
1390 LLVMVoidTypeInContext(base
->gallivm
->context
),
1395 if (depth_index
>= 0 || stencil_index
>= 0 || samplemask_index
>= 0) {
1396 LLVMValueRef out_ptr
;
1399 /* Specify the target we are exporting */
1400 args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRTZ
);
1402 args
[5] = base
->zero
; /* R, depth */
1403 args
[6] = base
->zero
; /* G, stencil test value[0:7], stencil op value[8:15] */
1404 args
[7] = base
->zero
; /* B, sample mask */
1405 args
[8] = base
->zero
; /* A, alpha to mask */
1407 if (depth_index
>= 0) {
1408 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[depth_index
][2];
1409 args
[5] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1411 si_shader_ctx
->shader
->db_shader_control
|= S_02880C_Z_EXPORT_ENABLE(1);
1414 if (stencil_index
>= 0) {
1415 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[stencil_index
][1];
1416 args
[6] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1417 /* Only setting the stencil component bit (0x2) here
1418 * breaks some stencil piglit tests
1421 si_shader_ctx
->shader
->db_shader_control
|=
1422 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
1425 if (samplemask_index
>= 0) {
1426 out_ptr
= si_shader_ctx
->radeon_bld
.soa
.outputs
[samplemask_index
][0];
1427 args
[7] = LLVMBuildLoad(base
->gallivm
->builder
, out_ptr
, "");
1428 mask
|= 0xf; /* Set all components. */
1429 si_shader_ctx
->shader
->db_shader_control
|= S_02880C_MASK_EXPORT_ENABLE(1);
1432 if (samplemask_index
>= 0)
1433 si_shader_ctx
->shader
->spi_shader_z_format
= V_028710_SPI_SHADER_32_ABGR
;
1434 else if (stencil_index
>= 0)
1435 si_shader_ctx
->shader
->spi_shader_z_format
= V_028710_SPI_SHADER_32_GR
;
1437 si_shader_ctx
->shader
->spi_shader_z_format
= V_028710_SPI_SHADER_32_R
;
1439 /* Specify which components to enable */
1440 args
[0] = lp_build_const_int32(base
->gallivm
, mask
);
1444 args
[4] = uint
->zero
;
1447 lp_build_intrinsic(base
->gallivm
->builder
,
1449 LLVMVoidTypeInContext(base
->gallivm
->context
),
1452 memcpy(last_args
, args
, sizeof(args
));
1455 if (!last_args
[0]) {
1456 /* Specify which components to enable */
1457 last_args
[0] = lp_build_const_int32(base
->gallivm
, 0x0);
1459 /* Specify the target we are exporting */
1460 last_args
[3] = lp_build_const_int32(base
->gallivm
, V_008DFC_SQ_EXP_MRT
);
1462 /* Set COMPR flag to zero to export data as 32-bit */
1463 last_args
[4] = uint
->zero
;
1466 last_args
[5]= uint
->zero
;
1467 last_args
[6]= uint
->zero
;
1468 last_args
[7]= uint
->zero
;
1469 last_args
[8]= uint
->zero
;
1472 /* Specify whether the EXEC mask represents the valid mask */
1473 last_args
[1] = uint
->one
;
1475 /* Specify that this is the last export */
1476 last_args
[2] = lp_build_const_int32(base
->gallivm
, 1);
1478 lp_build_intrinsic(base
->gallivm
->builder
,
1480 LLVMVoidTypeInContext(base
->gallivm
->context
),
1484 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1485 struct lp_build_tgsi_context
* bld_base
,
1486 struct lp_build_emit_data
* emit_data
);
1488 static bool tgsi_is_shadow_sampler(unsigned target
)
1490 return target
== TGSI_TEXTURE_SHADOW1D
||
1491 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
1492 target
== TGSI_TEXTURE_SHADOW2D
||
1493 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
1494 target
== TGSI_TEXTURE_SHADOWCUBE
||
1495 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
||
1496 target
== TGSI_TEXTURE_SHADOWRECT
;
1499 static const struct lp_build_tgsi_action tex_action
;
1501 static void tex_fetch_args(
1502 struct lp_build_tgsi_context
* bld_base
,
1503 struct lp_build_emit_data
* emit_data
)
1505 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1506 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1507 const struct tgsi_full_instruction
* inst
= emit_data
->inst
;
1508 unsigned opcode
= inst
->Instruction
.Opcode
;
1509 unsigned target
= inst
->Texture
.Texture
;
1510 LLVMValueRef coords
[4];
1511 LLVMValueRef address
[16];
1513 unsigned num_coords
= tgsi_util_get_texture_coord_dim(target
, &ref_pos
);
1516 unsigned sampler_src
= emit_data
->inst
->Instruction
.NumSrcRegs
- 1;
1517 unsigned sampler_index
= emit_data
->inst
->Src
[sampler_src
].Register
.Index
;
1518 bool has_offset
= HAVE_LLVM
>= 0x0305 ? inst
->Texture
.NumOffsets
> 0 : false;
1520 if (target
== TGSI_TEXTURE_BUFFER
) {
1521 LLVMTypeRef i128
= LLVMIntTypeInContext(gallivm
->context
, 128);
1522 LLVMTypeRef v2i128
= LLVMVectorType(i128
, 2);
1523 LLVMTypeRef i8
= LLVMInt8TypeInContext(gallivm
->context
);
1524 LLVMTypeRef v16i8
= LLVMVectorType(i8
, 16);
1526 /* Bitcast and truncate v8i32 to v16i8. */
1527 LLVMValueRef res
= si_shader_ctx
->resources
[sampler_index
];
1528 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v2i128
, "");
1529 res
= LLVMBuildExtractElement(gallivm
->builder
, res
, bld_base
->uint_bld
.zero
, "");
1530 res
= LLVMBuildBitCast(gallivm
->builder
, res
, v16i8
, "");
1532 emit_data
->dst_type
= LLVMVectorType(bld_base
->base
.elem_type
, 4);
1533 emit_data
->args
[0] = res
;
1534 emit_data
->args
[1] = bld_base
->uint_bld
.zero
;
1535 emit_data
->args
[2] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, 0);
1536 emit_data
->arg_count
= 3;
1540 /* Fetch and project texture coordinates */
1541 coords
[3] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_W
);
1542 for (chan
= 0; chan
< 3; chan
++ ) {
1543 coords
[chan
] = lp_build_emit_fetch(bld_base
,
1546 if (opcode
== TGSI_OPCODE_TXP
)
1547 coords
[chan
] = lp_build_emit_llvm_binary(bld_base
,
1553 if (opcode
== TGSI_OPCODE_TXP
)
1554 coords
[3] = bld_base
->base
.one
;
1557 if (has_offset
&& opcode
!= TGSI_OPCODE_TXF
) {
1558 /* The offsets are six-bit signed integers packed like this:
1559 * X=[5:0], Y=[13:8], and Z=[21:16].
1561 LLVMValueRef offset
[3], pack
;
1563 assert(inst
->Texture
.NumOffsets
== 1);
1565 for (chan
= 0; chan
< 3; chan
++) {
1566 offset
[chan
] = lp_build_emit_fetch_texoffset(bld_base
,
1567 emit_data
->inst
, 0, chan
);
1568 offset
[chan
] = LLVMBuildAnd(gallivm
->builder
, offset
[chan
],
1569 lp_build_const_int32(gallivm
, 0x3f), "");
1571 offset
[chan
] = LLVMBuildShl(gallivm
->builder
, offset
[chan
],
1572 lp_build_const_int32(gallivm
, chan
*8), "");
1575 pack
= LLVMBuildOr(gallivm
->builder
, offset
[0], offset
[1], "");
1576 pack
= LLVMBuildOr(gallivm
->builder
, pack
, offset
[2], "");
1577 address
[count
++] = pack
;
1580 /* Pack LOD bias value */
1581 if (opcode
== TGSI_OPCODE_TXB
)
1582 address
[count
++] = coords
[3];
1583 if (opcode
== TGSI_OPCODE_TXB2
)
1584 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
1586 /* Pack depth comparison value */
1587 if (tgsi_is_shadow_sampler(target
) && opcode
!= TGSI_OPCODE_LODQ
) {
1588 if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
1589 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
1591 assert(ref_pos
>= 0);
1592 address
[count
++] = coords
[ref_pos
];
1596 if (target
== TGSI_TEXTURE_CUBE
||
1597 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1598 target
== TGSI_TEXTURE_SHADOWCUBE
||
1599 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
1600 radeon_llvm_emit_prepare_cube_coords(bld_base
, emit_data
, coords
);
1602 /* Pack user derivatives */
1603 if (opcode
== TGSI_OPCODE_TXD
) {
1604 int num_deriv_channels
, param
;
1607 case TGSI_TEXTURE_3D
:
1608 num_deriv_channels
= 3;
1610 case TGSI_TEXTURE_2D
:
1611 case TGSI_TEXTURE_SHADOW2D
:
1612 case TGSI_TEXTURE_RECT
:
1613 case TGSI_TEXTURE_SHADOWRECT
:
1614 case TGSI_TEXTURE_2D_ARRAY
:
1615 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1616 case TGSI_TEXTURE_CUBE
:
1617 case TGSI_TEXTURE_SHADOWCUBE
:
1618 case TGSI_TEXTURE_CUBE_ARRAY
:
1619 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
1620 num_deriv_channels
= 2;
1622 case TGSI_TEXTURE_1D
:
1623 case TGSI_TEXTURE_SHADOW1D
:
1624 case TGSI_TEXTURE_1D_ARRAY
:
1625 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1626 num_deriv_channels
= 1;
1629 assert(0); /* no other targets are valid here */
1632 for (param
= 1; param
<= 2; param
++)
1633 for (chan
= 0; chan
< num_deriv_channels
; chan
++)
1634 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, param
, chan
);
1637 /* Pack texture coordinates */
1638 address
[count
++] = coords
[0];
1640 address
[count
++] = coords
[1];
1642 address
[count
++] = coords
[2];
1644 /* Pack LOD or sample index */
1645 if (opcode
== TGSI_OPCODE_TXL
|| opcode
== TGSI_OPCODE_TXF
)
1646 address
[count
++] = coords
[3];
1647 else if (opcode
== TGSI_OPCODE_TXL2
)
1648 address
[count
++] = lp_build_emit_fetch(bld_base
, inst
, 1, 0);
1651 assert(!"Cannot handle more than 16 texture address parameters");
1655 for (chan
= 0; chan
< count
; chan
++ ) {
1656 address
[chan
] = LLVMBuildBitCast(gallivm
->builder
,
1658 LLVMInt32TypeInContext(gallivm
->context
),
1662 /* Adjust the sample index according to FMASK.
1664 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1665 * which is the identity mapping. Each nibble says which physical sample
1666 * should be fetched to get that sample.
1668 * For example, 0x11111100 means there are only 2 samples stored and
1669 * the second sample covers 3/4 of the pixel. When reading samples 0
1670 * and 1, return physical sample 0 (determined by the first two 0s
1671 * in FMASK), otherwise return physical sample 1.
1673 * The sample index should be adjusted as follows:
1674 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1676 if (target
== TGSI_TEXTURE_2D_MSAA
||
1677 target
== TGSI_TEXTURE_2D_ARRAY_MSAA
) {
1678 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1679 struct lp_build_emit_data txf_emit_data
= *emit_data
;
1680 LLVMValueRef txf_address
[4];
1681 unsigned txf_count
= count
;
1682 struct tgsi_full_instruction inst
= {};
1684 memcpy(txf_address
, address
, sizeof(txf_address
));
1686 if (target
== TGSI_TEXTURE_2D_MSAA
) {
1687 txf_address
[2] = bld_base
->uint_bld
.zero
;
1689 txf_address
[3] = bld_base
->uint_bld
.zero
;
1691 /* Pad to a power-of-two size. */
1692 while (txf_count
< util_next_power_of_two(txf_count
))
1693 txf_address
[txf_count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1695 /* Read FMASK using TXF. */
1696 inst
.Instruction
.Opcode
= TGSI_OPCODE_TXF
;
1697 inst
.Texture
.Texture
= target
== TGSI_TEXTURE_2D_MSAA
? TGSI_TEXTURE_2D
: TGSI_TEXTURE_2D_ARRAY
;
1698 txf_emit_data
.inst
= &inst
;
1699 txf_emit_data
.chan
= 0;
1700 txf_emit_data
.dst_type
= LLVMVectorType(
1701 LLVMInt32TypeInContext(gallivm
->context
), 4);
1702 txf_emit_data
.args
[0] = lp_build_gather_values(gallivm
, txf_address
, txf_count
);
1703 txf_emit_data
.args
[1] = si_shader_ctx
->resources
[SI_FMASK_TEX_OFFSET
+ sampler_index
];
1704 txf_emit_data
.args
[2] = lp_build_const_int32(gallivm
, inst
.Texture
.Texture
);
1705 txf_emit_data
.arg_count
= 3;
1707 build_tex_intrinsic(&tex_action
, bld_base
, &txf_emit_data
);
1709 /* Initialize some constants. */
1710 LLVMValueRef four
= LLVMConstInt(uint_bld
->elem_type
, 4, 0);
1711 LLVMValueRef F
= LLVMConstInt(uint_bld
->elem_type
, 0xF, 0);
1713 /* Apply the formula. */
1714 LLVMValueRef fmask
=
1715 LLVMBuildExtractElement(gallivm
->builder
,
1716 txf_emit_data
.output
[0],
1717 uint_bld
->zero
, "");
1719 unsigned sample_chan
= target
== TGSI_TEXTURE_2D_MSAA
? 2 : 3;
1721 LLVMValueRef sample_index4
=
1722 LLVMBuildMul(gallivm
->builder
, address
[sample_chan
], four
, "");
1724 LLVMValueRef shifted_fmask
=
1725 LLVMBuildLShr(gallivm
->builder
, fmask
, sample_index4
, "");
1727 LLVMValueRef final_sample
=
1728 LLVMBuildAnd(gallivm
->builder
, shifted_fmask
, F
, "");
1730 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1731 * resource descriptor is 0 (invalid),
1733 LLVMValueRef fmask_desc
=
1734 LLVMBuildBitCast(gallivm
->builder
,
1735 si_shader_ctx
->resources
[SI_FMASK_TEX_OFFSET
+ sampler_index
],
1736 LLVMVectorType(uint_bld
->elem_type
, 8), "");
1738 LLVMValueRef fmask_word1
=
1739 LLVMBuildExtractElement(gallivm
->builder
, fmask_desc
,
1742 LLVMValueRef word1_is_nonzero
=
1743 LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1744 fmask_word1
, uint_bld
->zero
, "");
1746 /* Replace the MSAA sample index. */
1747 address
[sample_chan
] =
1748 LLVMBuildSelect(gallivm
->builder
, word1_is_nonzero
,
1749 final_sample
, address
[sample_chan
], "");
1753 emit_data
->args
[1] = si_shader_ctx
->resources
[sampler_index
];
1755 if (opcode
== TGSI_OPCODE_TXF
) {
1756 /* add tex offsets */
1757 if (inst
->Texture
.NumOffsets
) {
1758 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1759 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
1760 const struct tgsi_texture_offset
* off
= inst
->TexOffsets
;
1762 assert(inst
->Texture
.NumOffsets
== 1);
1765 case TGSI_TEXTURE_3D
:
1766 address
[2] = lp_build_add(uint_bld
, address
[2],
1767 bld
->immediates
[off
->Index
][off
->SwizzleZ
]);
1769 case TGSI_TEXTURE_2D
:
1770 case TGSI_TEXTURE_SHADOW2D
:
1771 case TGSI_TEXTURE_RECT
:
1772 case TGSI_TEXTURE_SHADOWRECT
:
1773 case TGSI_TEXTURE_2D_ARRAY
:
1774 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
1776 lp_build_add(uint_bld
, address
[1],
1777 bld
->immediates
[off
->Index
][off
->SwizzleY
]);
1779 case TGSI_TEXTURE_1D
:
1780 case TGSI_TEXTURE_SHADOW1D
:
1781 case TGSI_TEXTURE_1D_ARRAY
:
1782 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
1784 lp_build_add(uint_bld
, address
[0],
1785 bld
->immediates
[off
->Index
][off
->SwizzleX
]);
1787 /* texture offsets do not apply to other texture targets */
1791 emit_data
->args
[2] = lp_build_const_int32(gallivm
, target
);
1792 emit_data
->arg_count
= 3;
1794 emit_data
->dst_type
= LLVMVectorType(
1795 LLVMInt32TypeInContext(gallivm
->context
),
1797 } else if (opcode
== TGSI_OPCODE_TG4
||
1798 opcode
== TGSI_OPCODE_LODQ
||
1800 unsigned is_array
= target
== TGSI_TEXTURE_1D_ARRAY
||
1801 target
== TGSI_TEXTURE_SHADOW1D_ARRAY
||
1802 target
== TGSI_TEXTURE_2D_ARRAY
||
1803 target
== TGSI_TEXTURE_SHADOW2D_ARRAY
||
1804 target
== TGSI_TEXTURE_CUBE_ARRAY
||
1805 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
;
1806 unsigned is_rect
= target
== TGSI_TEXTURE_RECT
;
1807 unsigned dmask
= 0xf;
1809 if (opcode
== TGSI_OPCODE_TG4
) {
1810 unsigned gather_comp
= 0;
1812 /* DMASK was repurposed for GATHER4. 4 components are always
1813 * returned and DMASK works like a swizzle - it selects
1814 * the component to fetch. The only valid DMASK values are
1815 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1816 * (red,red,red,red) etc.) The ISA document doesn't mention
1820 /* Get the component index from src1.x for Gather4. */
1821 if (!tgsi_is_shadow_sampler(target
)) {
1822 LLVMValueRef (*imms
)[4] = lp_soa_context(bld_base
)->immediates
;
1823 LLVMValueRef comp_imm
;
1824 struct tgsi_src_register src1
= inst
->Src
[1].Register
;
1826 assert(src1
.File
== TGSI_FILE_IMMEDIATE
);
1828 comp_imm
= imms
[src1
.Index
][src1
.SwizzleX
];
1829 gather_comp
= LLVMConstIntGetZExtValue(comp_imm
);
1830 gather_comp
= CLAMP(gather_comp
, 0, 3);
1833 dmask
= 1 << gather_comp
;
1836 emit_data
->args
[2] = si_shader_ctx
->samplers
[sampler_index
];
1837 emit_data
->args
[3] = lp_build_const_int32(gallivm
, dmask
);
1838 emit_data
->args
[4] = lp_build_const_int32(gallivm
, is_rect
); /* unorm */
1839 emit_data
->args
[5] = lp_build_const_int32(gallivm
, 0); /* r128 */
1840 emit_data
->args
[6] = lp_build_const_int32(gallivm
, is_array
); /* da */
1841 emit_data
->args
[7] = lp_build_const_int32(gallivm
, 0); /* glc */
1842 emit_data
->args
[8] = lp_build_const_int32(gallivm
, 0); /* slc */
1843 emit_data
->args
[9] = lp_build_const_int32(gallivm
, 0); /* tfe */
1844 emit_data
->args
[10] = lp_build_const_int32(gallivm
, 0); /* lwe */
1846 emit_data
->arg_count
= 11;
1848 emit_data
->dst_type
= LLVMVectorType(
1849 LLVMFloatTypeInContext(gallivm
->context
),
1852 emit_data
->args
[2] = si_shader_ctx
->samplers
[sampler_index
];
1853 emit_data
->args
[3] = lp_build_const_int32(gallivm
, target
);
1854 emit_data
->arg_count
= 4;
1856 emit_data
->dst_type
= LLVMVectorType(
1857 LLVMFloatTypeInContext(gallivm
->context
),
1861 /* The fetch opcode has been converted to a 2D array fetch.
1862 * This simplifies the LLVM backend. */
1863 if (target
== TGSI_TEXTURE_CUBE_ARRAY
)
1864 target
= TGSI_TEXTURE_2D_ARRAY
;
1865 else if (target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
1866 target
= TGSI_TEXTURE_SHADOW2D_ARRAY
;
1868 /* Pad to power of two vector */
1869 while (count
< util_next_power_of_two(count
))
1870 address
[count
++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
1872 emit_data
->args
[0] = lp_build_gather_values(gallivm
, address
, count
);
1875 static void build_tex_intrinsic(const struct lp_build_tgsi_action
* action
,
1876 struct lp_build_tgsi_context
* bld_base
,
1877 struct lp_build_emit_data
* emit_data
)
1879 struct lp_build_context
* base
= &bld_base
->base
;
1880 unsigned opcode
= emit_data
->inst
->Instruction
.Opcode
;
1881 unsigned target
= emit_data
->inst
->Texture
.Texture
;
1882 char intr_name
[127];
1883 bool has_offset
= HAVE_LLVM
>= 0x0305 ?
1884 emit_data
->inst
->Texture
.NumOffsets
> 0 : false;
1886 if (target
== TGSI_TEXTURE_BUFFER
) {
1887 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1888 base
->gallivm
->builder
,
1889 "llvm.SI.vs.load.input", emit_data
->dst_type
,
1890 emit_data
->args
, emit_data
->arg_count
,
1891 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1895 if (opcode
== TGSI_OPCODE_TG4
||
1896 opcode
== TGSI_OPCODE_LODQ
||
1897 (opcode
!= TGSI_OPCODE_TXF
&& has_offset
)) {
1898 bool is_shadow
= tgsi_is_shadow_sampler(target
);
1899 const char *name
= "llvm.SI.image.sample";
1900 const char *infix
= "";
1903 case TGSI_OPCODE_TEX
:
1904 case TGSI_OPCODE_TEX2
:
1905 case TGSI_OPCODE_TXP
:
1907 case TGSI_OPCODE_TXB
:
1908 case TGSI_OPCODE_TXB2
:
1911 case TGSI_OPCODE_TXL
:
1912 case TGSI_OPCODE_TXL2
:
1915 case TGSI_OPCODE_TXD
:
1918 case TGSI_OPCODE_TG4
:
1919 name
= "llvm.SI.gather4";
1921 case TGSI_OPCODE_LODQ
:
1922 name
= "llvm.SI.getlod";
1931 /* Add the type and suffixes .c, .o if needed. */
1932 sprintf(intr_name
, "%s%s%s%s.v%ui32", name
,
1933 is_shadow
? ".c" : "", infix
, has_offset
? ".o" : "",
1934 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
1936 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1937 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
1938 emit_data
->args
, emit_data
->arg_count
,
1939 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1941 LLVMTypeRef i8
, v16i8
, v32i8
;
1945 case TGSI_OPCODE_TEX
:
1946 case TGSI_OPCODE_TEX2
:
1947 case TGSI_OPCODE_TXP
:
1948 name
= "llvm.SI.sample";
1950 case TGSI_OPCODE_TXB
:
1951 case TGSI_OPCODE_TXB2
:
1952 name
= "llvm.SI.sampleb";
1954 case TGSI_OPCODE_TXD
:
1955 name
= "llvm.SI.sampled";
1957 case TGSI_OPCODE_TXF
:
1958 name
= "llvm.SI.imageload";
1960 case TGSI_OPCODE_TXL
:
1961 case TGSI_OPCODE_TXL2
:
1962 name
= "llvm.SI.samplel";
1969 i8
= LLVMInt8TypeInContext(base
->gallivm
->context
);
1970 v16i8
= LLVMVectorType(i8
, 16);
1971 v32i8
= LLVMVectorType(i8
, 32);
1973 emit_data
->args
[1] = LLVMBuildBitCast(base
->gallivm
->builder
,
1974 emit_data
->args
[1], v32i8
, "");
1975 if (opcode
!= TGSI_OPCODE_TXF
) {
1976 emit_data
->args
[2] = LLVMBuildBitCast(base
->gallivm
->builder
,
1977 emit_data
->args
[2], v16i8
, "");
1980 sprintf(intr_name
, "%s.v%ui32", name
,
1981 LLVMGetVectorSize(LLVMTypeOf(emit_data
->args
[0])));
1983 emit_data
->output
[emit_data
->chan
] = build_intrinsic(
1984 base
->gallivm
->builder
, intr_name
, emit_data
->dst_type
,
1985 emit_data
->args
, emit_data
->arg_count
,
1986 LLVMReadNoneAttribute
| LLVMNoUnwindAttribute
);
1990 static void txq_fetch_args(
1991 struct lp_build_tgsi_context
* bld_base
,
1992 struct lp_build_emit_data
* emit_data
)
1994 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
1995 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
1996 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1997 unsigned target
= inst
->Texture
.Texture
;
1999 if (target
== TGSI_TEXTURE_BUFFER
) {
2000 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2001 LLVMTypeRef v8i32
= LLVMVectorType(i32
, 8);
2003 /* Read the size from the buffer descriptor directly. */
2004 LLVMValueRef size
= si_shader_ctx
->resources
[inst
->Src
[1].Register
.Index
];
2005 size
= LLVMBuildBitCast(gallivm
->builder
, size
, v8i32
, "");
2006 size
= LLVMBuildExtractElement(gallivm
->builder
, size
,
2007 lp_build_const_int32(gallivm
, 2), "");
2008 emit_data
->args
[0] = size
;
2013 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, inst
, 0, TGSI_CHAN_X
);
2016 emit_data
->args
[1] = si_shader_ctx
->resources
[inst
->Src
[1].Register
.Index
];
2018 /* Texture target */
2019 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
2020 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
)
2021 target
= TGSI_TEXTURE_2D_ARRAY
;
2023 emit_data
->args
[2] = lp_build_const_int32(bld_base
->base
.gallivm
,
2026 emit_data
->arg_count
= 3;
2028 emit_data
->dst_type
= LLVMVectorType(
2029 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
),
2033 static void build_txq_intrinsic(const struct lp_build_tgsi_action
* action
,
2034 struct lp_build_tgsi_context
* bld_base
,
2035 struct lp_build_emit_data
* emit_data
)
2037 unsigned target
= emit_data
->inst
->Texture
.Texture
;
2039 if (target
== TGSI_TEXTURE_BUFFER
) {
2040 /* Just return the buffer size. */
2041 emit_data
->output
[emit_data
->chan
] = emit_data
->args
[0];
2045 build_tgsi_intrinsic_nomem(action
, bld_base
, emit_data
);
2047 /* Divide the number of layers by 6 to get the number of cubes. */
2048 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
2049 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
2050 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
2051 LLVMValueRef two
= lp_build_const_int32(bld_base
->base
.gallivm
, 2);
2052 LLVMValueRef six
= lp_build_const_int32(bld_base
->base
.gallivm
, 6);
2054 LLVMValueRef v4
= emit_data
->output
[emit_data
->chan
];
2055 LLVMValueRef z
= LLVMBuildExtractElement(builder
, v4
, two
, "");
2056 z
= LLVMBuildSDiv(builder
, z
, six
, "");
2058 emit_data
->output
[emit_data
->chan
] =
2059 LLVMBuildInsertElement(builder
, v4
, z
, two
, "");
2063 static void si_llvm_emit_ddxy(
2064 const struct lp_build_tgsi_action
* action
,
2065 struct lp_build_tgsi_context
* bld_base
,
2066 struct lp_build_emit_data
* emit_data
)
2068 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2069 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2070 struct lp_build_context
* base
= &bld_base
->base
;
2071 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
2072 unsigned opcode
= inst
->Instruction
.Opcode
;
2073 LLVMValueRef indices
[2];
2074 LLVMValueRef store_ptr
, load_ptr0
, load_ptr1
;
2075 LLVMValueRef tl
, trbl
, result
[4];
2077 unsigned swizzle
[4];
2080 i32
= LLVMInt32TypeInContext(gallivm
->context
);
2082 indices
[0] = bld_base
->uint_bld
.zero
;
2083 indices
[1] = build_intrinsic(gallivm
->builder
, "llvm.SI.tid", i32
,
2084 NULL
, 0, LLVMReadNoneAttribute
);
2085 store_ptr
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
2088 indices
[1] = LLVMBuildAnd(gallivm
->builder
, indices
[1],
2089 lp_build_const_int32(gallivm
, 0xfffffffc), "");
2090 load_ptr0
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
2093 indices
[1] = LLVMBuildAdd(gallivm
->builder
, indices
[1],
2094 lp_build_const_int32(gallivm
,
2095 opcode
== TGSI_OPCODE_DDX
? 1 : 2),
2097 load_ptr1
= LLVMBuildGEP(gallivm
->builder
, si_shader_ctx
->ddxy_lds
,
2100 for (c
= 0; c
< 4; ++c
) {
2103 swizzle
[c
] = tgsi_util_get_full_src_register_swizzle(&inst
->Src
[0], c
);
2104 for (i
= 0; i
< c
; ++i
) {
2105 if (swizzle
[i
] == swizzle
[c
]) {
2106 result
[c
] = result
[i
];
2113 LLVMBuildStore(gallivm
->builder
,
2114 LLVMBuildBitCast(gallivm
->builder
,
2115 lp_build_emit_fetch(bld_base
, inst
, 0, c
),
2119 tl
= LLVMBuildLoad(gallivm
->builder
, load_ptr0
, "");
2120 tl
= LLVMBuildBitCast(gallivm
->builder
, tl
, base
->elem_type
, "");
2122 trbl
= LLVMBuildLoad(gallivm
->builder
, load_ptr1
, "");
2123 trbl
= LLVMBuildBitCast(gallivm
->builder
, trbl
, base
->elem_type
, "");
2125 result
[c
] = LLVMBuildFSub(gallivm
->builder
, trbl
, tl
, "");
2128 emit_data
->output
[0] = lp_build_gather_values(gallivm
, result
, 4);
2131 /* Emit one vertex from the geometry shader */
2132 static void si_llvm_emit_vertex(
2133 const struct lp_build_tgsi_action
*action
,
2134 struct lp_build_tgsi_context
*bld_base
,
2135 struct lp_build_emit_data
*emit_data
)
2137 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2138 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2139 struct si_shader
*shader
= si_shader_ctx
->shader
;
2140 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
2141 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2142 LLVMTypeRef i32
= LLVMInt32TypeInContext(gallivm
->context
);
2143 LLVMValueRef soffset
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2144 SI_PARAM_GS2VS_OFFSET
);
2145 LLVMValueRef gs_next_vertex
;
2146 LLVMValueRef can_emit
, kill
;
2147 LLVMValueRef t_list_ptr
;
2148 LLVMValueRef t_list
;
2149 LLVMValueRef args
[2];
2153 /* Load the GSVS ring resource descriptor */
2154 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2155 SI_PARAM_RW_BUFFERS
);
2156 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
2157 lp_build_const_int32(gallivm
, SI_RING_GSVS
));
2159 /* Write vertex attribute values to GSVS ring */
2160 gs_next_vertex
= LLVMBuildLoad(gallivm
->builder
, si_shader_ctx
->gs_next_vertex
, "");
2162 /* If this thread has already emitted the declared maximum number of
2163 * vertices, kill it: excessive vertex emissions are not supposed to
2164 * have any effect, and GS threads have no externally observable
2165 * effects other than emitting vertices.
2167 can_emit
= LLVMBuildICmp(gallivm
->builder
, LLVMIntULE
, gs_next_vertex
,
2168 lp_build_const_int32(gallivm
,
2169 shader
->selector
->gs_max_out_vertices
), "");
2170 kill
= lp_build_select(&bld_base
->base
, can_emit
,
2171 lp_build_const_float(gallivm
, 1.0f
),
2172 lp_build_const_float(gallivm
, -1.0f
));
2173 build_intrinsic(gallivm
->builder
, "llvm.AMDGPU.kill",
2174 LLVMVoidTypeInContext(gallivm
->context
), &kill
, 1, 0);
2176 for (i
= 0; i
< info
->num_outputs
; i
++) {
2177 LLVMValueRef
*out_ptr
=
2178 si_shader_ctx
->radeon_bld
.soa
.outputs
[i
];
2180 for (chan
= 0; chan
< 4; chan
++) {
2181 LLVMValueRef out_val
= LLVMBuildLoad(gallivm
->builder
, out_ptr
[chan
], "");
2182 LLVMValueRef voffset
=
2183 lp_build_const_int32(gallivm
, (i
* 4 + chan
) *
2184 shader
->selector
->gs_max_out_vertices
);
2186 voffset
= lp_build_add(uint
, voffset
, gs_next_vertex
);
2187 voffset
= lp_build_mul_imm(uint
, voffset
, 4);
2189 out_val
= LLVMBuildBitCast(gallivm
->builder
, out_val
, i32
, "");
2191 build_tbuffer_store(si_shader_ctx
, t_list
, out_val
, 1,
2192 voffset
, soffset
, 0,
2193 V_008F0C_BUF_DATA_FORMAT_32
,
2194 V_008F0C_BUF_NUM_FORMAT_UINT
,
2198 gs_next_vertex
= lp_build_add(uint
, gs_next_vertex
,
2199 lp_build_const_int32(gallivm
, 1));
2200 LLVMBuildStore(gallivm
->builder
, gs_next_vertex
, si_shader_ctx
->gs_next_vertex
);
2202 /* Signal vertex emission */
2203 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_EMIT
| SENDMSG_GS
);
2204 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
2205 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2206 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
2207 LLVMNoUnwindAttribute
);
2210 /* Cut one primitive from the geometry shader */
2211 static void si_llvm_emit_primitive(
2212 const struct lp_build_tgsi_action
*action
,
2213 struct lp_build_tgsi_context
*bld_base
,
2214 struct lp_build_emit_data
*emit_data
)
2216 struct si_shader_context
*si_shader_ctx
= si_shader_context(bld_base
);
2217 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2218 LLVMValueRef args
[2];
2220 /* Signal primitive cut */
2221 args
[0] = lp_build_const_int32(gallivm
, SENDMSG_GS_OP_CUT
| SENDMSG_GS
);
2222 args
[1] = LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_GS_WAVE_ID
);
2223 build_intrinsic(gallivm
->builder
, "llvm.SI.sendmsg",
2224 LLVMVoidTypeInContext(gallivm
->context
), args
, 2,
2225 LLVMNoUnwindAttribute
);
2228 static const struct lp_build_tgsi_action tex_action
= {
2229 .fetch_args
= tex_fetch_args
,
2230 .emit
= build_tex_intrinsic
,
2233 static const struct lp_build_tgsi_action txq_action
= {
2234 .fetch_args
= txq_fetch_args
,
2235 .emit
= build_txq_intrinsic
,
2236 .intr_name
= "llvm.SI.resinfo"
2239 static void create_meta_data(struct si_shader_context
*si_shader_ctx
)
2241 struct gallivm_state
*gallivm
= si_shader_ctx
->radeon_bld
.soa
.bld_base
.base
.gallivm
;
2242 LLVMValueRef args
[3];
2244 args
[0] = LLVMMDStringInContext(gallivm
->context
, "const", 5);
2246 args
[2] = lp_build_const_int32(gallivm
, 1);
2248 si_shader_ctx
->const_md
= LLVMMDNodeInContext(gallivm
->context
, args
, 3);
2251 static LLVMTypeRef
const_array(LLVMTypeRef elem_type
, int num_elements
)
2253 return LLVMPointerType(LLVMArrayType(elem_type
, num_elements
),
2257 static void create_function(struct si_shader_context
*si_shader_ctx
)
2259 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2260 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
2261 struct si_shader
*shader
= si_shader_ctx
->shader
;
2262 LLVMTypeRef params
[SI_NUM_PARAMS
], f32
, i8
, i32
, v2i32
, v3i32
, v16i8
, v4i32
, v8i32
;
2263 unsigned i
, last_array_pointer
, last_sgpr
, num_params
;
2265 i8
= LLVMInt8TypeInContext(gallivm
->context
);
2266 i32
= LLVMInt32TypeInContext(gallivm
->context
);
2267 f32
= LLVMFloatTypeInContext(gallivm
->context
);
2268 v2i32
= LLVMVectorType(i32
, 2);
2269 v3i32
= LLVMVectorType(i32
, 3);
2270 v4i32
= LLVMVectorType(i32
, 4);
2271 v8i32
= LLVMVectorType(i32
, 8);
2272 v16i8
= LLVMVectorType(i8
, 16);
2274 params
[SI_PARAM_RW_BUFFERS
] = const_array(v16i8
, SI_NUM_RW_BUFFERS
);
2275 params
[SI_PARAM_CONST
] = const_array(v16i8
, SI_NUM_CONST_BUFFERS
);
2276 params
[SI_PARAM_SAMPLER
] = const_array(v4i32
, SI_NUM_SAMPLER_STATES
);
2277 params
[SI_PARAM_RESOURCE
] = const_array(v8i32
, SI_NUM_SAMPLER_VIEWS
);
2278 last_array_pointer
= SI_PARAM_RESOURCE
;
2280 switch (si_shader_ctx
->type
) {
2281 case TGSI_PROCESSOR_VERTEX
:
2282 params
[SI_PARAM_VERTEX_BUFFER
] = const_array(v16i8
, SI_NUM_VERTEX_BUFFERS
);
2283 last_array_pointer
= SI_PARAM_VERTEX_BUFFER
;
2284 params
[SI_PARAM_BASE_VERTEX
] = i32
;
2285 params
[SI_PARAM_START_INSTANCE
] = i32
;
2286 num_params
= SI_PARAM_START_INSTANCE
+1;
2288 if (shader
->key
.vs
.as_es
) {
2289 params
[SI_PARAM_ES2GS_OFFSET
] = i32
;
2292 if (shader
->is_gs_copy_shader
) {
2293 last_array_pointer
= SI_PARAM_CONST
;
2294 num_params
= SI_PARAM_CONST
+1;
2297 /* The locations of the other parameters are assigned dynamically. */
2299 /* Streamout SGPRs. */
2300 if (shader
->selector
->so
.num_outputs
) {
2301 params
[si_shader_ctx
->param_streamout_config
= num_params
++] = i32
;
2302 params
[si_shader_ctx
->param_streamout_write_index
= num_params
++] = i32
;
2304 /* A streamout buffer offset is loaded if the stride is non-zero. */
2305 for (i
= 0; i
< 4; i
++) {
2306 if (!shader
->selector
->so
.stride
[i
])
2309 params
[si_shader_ctx
->param_streamout_offset
[i
] = num_params
++] = i32
;
2313 last_sgpr
= num_params
-1;
2316 params
[si_shader_ctx
->param_vertex_id
= num_params
++] = i32
;
2317 params
[num_params
++] = i32
; /* unused*/
2318 params
[num_params
++] = i32
; /* unused */
2319 params
[si_shader_ctx
->param_instance_id
= num_params
++] = i32
;
2322 case TGSI_PROCESSOR_GEOMETRY
:
2323 params
[SI_PARAM_GS2VS_OFFSET
] = i32
;
2324 params
[SI_PARAM_GS_WAVE_ID
] = i32
;
2325 last_sgpr
= SI_PARAM_GS_WAVE_ID
;
2328 params
[SI_PARAM_VTX0_OFFSET
] = i32
;
2329 params
[SI_PARAM_VTX1_OFFSET
] = i32
;
2330 params
[SI_PARAM_PRIMITIVE_ID
] = i32
;
2331 params
[SI_PARAM_VTX2_OFFSET
] = i32
;
2332 params
[SI_PARAM_VTX3_OFFSET
] = i32
;
2333 params
[SI_PARAM_VTX4_OFFSET
] = i32
;
2334 params
[SI_PARAM_VTX5_OFFSET
] = i32
;
2335 params
[SI_PARAM_GS_INSTANCE_ID
] = i32
;
2336 num_params
= SI_PARAM_GS_INSTANCE_ID
+1;
2339 case TGSI_PROCESSOR_FRAGMENT
:
2340 params
[SI_PARAM_ALPHA_REF
] = f32
;
2341 params
[SI_PARAM_PRIM_MASK
] = i32
;
2342 last_sgpr
= SI_PARAM_PRIM_MASK
;
2343 params
[SI_PARAM_PERSP_SAMPLE
] = v2i32
;
2344 params
[SI_PARAM_PERSP_CENTER
] = v2i32
;
2345 params
[SI_PARAM_PERSP_CENTROID
] = v2i32
;
2346 params
[SI_PARAM_PERSP_PULL_MODEL
] = v3i32
;
2347 params
[SI_PARAM_LINEAR_SAMPLE
] = v2i32
;
2348 params
[SI_PARAM_LINEAR_CENTER
] = v2i32
;
2349 params
[SI_PARAM_LINEAR_CENTROID
] = v2i32
;
2350 params
[SI_PARAM_LINE_STIPPLE_TEX
] = f32
;
2351 params
[SI_PARAM_POS_X_FLOAT
] = f32
;
2352 params
[SI_PARAM_POS_Y_FLOAT
] = f32
;
2353 params
[SI_PARAM_POS_Z_FLOAT
] = f32
;
2354 params
[SI_PARAM_POS_W_FLOAT
] = f32
;
2355 params
[SI_PARAM_FRONT_FACE
] = f32
;
2356 params
[SI_PARAM_ANCILLARY
] = i32
;
2357 params
[SI_PARAM_SAMPLE_COVERAGE
] = f32
;
2358 params
[SI_PARAM_POS_FIXED_PT
] = f32
;
2359 num_params
= SI_PARAM_POS_FIXED_PT
+1;
2363 assert(0 && "unimplemented shader");
2367 assert(num_params
<= Elements(params
));
2368 radeon_llvm_create_func(&si_shader_ctx
->radeon_bld
, params
, num_params
);
2369 radeon_llvm_shader_type(si_shader_ctx
->radeon_bld
.main_fn
, si_shader_ctx
->type
);
2371 for (i
= 0; i
<= last_sgpr
; ++i
) {
2372 LLVMValueRef P
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, i
);
2374 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2375 * to move load. Inputs are constant so this is fine. */
2376 if (i
<= last_array_pointer
)
2377 LLVMAddAttribute(P
, LLVMByValAttribute
);
2379 LLVMAddAttribute(P
, LLVMInRegAttribute
);
2382 if (bld_base
->info
&&
2383 (bld_base
->info
->opcode_count
[TGSI_OPCODE_DDX
] > 0 ||
2384 bld_base
->info
->opcode_count
[TGSI_OPCODE_DDY
] > 0))
2385 si_shader_ctx
->ddxy_lds
=
2386 LLVMAddGlobalInAddressSpace(gallivm
->module
,
2387 LLVMArrayType(i32
, 64),
2392 static void preload_constants(struct si_shader_context
*si_shader_ctx
)
2394 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2395 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2396 const struct tgsi_shader_info
* info
= bld_base
->info
;
2398 LLVMValueRef ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_CONST
);
2400 for (buf
= 0; buf
< SI_NUM_CONST_BUFFERS
; buf
++) {
2401 unsigned i
, num_const
= info
->const_file_max
[buf
] + 1;
2406 /* Allocate space for the constant values */
2407 si_shader_ctx
->constants
[buf
] = CALLOC(num_const
* 4, sizeof(LLVMValueRef
));
2409 /* Load the resource descriptor */
2410 si_shader_ctx
->const_resource
[buf
] =
2411 build_indexed_load(si_shader_ctx
, ptr
, lp_build_const_int32(gallivm
, buf
));
2413 /* Load the constants, we rely on the code sinking to do the rest */
2414 for (i
= 0; i
< num_const
* 4; ++i
) {
2415 si_shader_ctx
->constants
[buf
][i
] =
2416 load_const(gallivm
->builder
,
2417 si_shader_ctx
->const_resource
[buf
],
2418 lp_build_const_int32(gallivm
, i
* 4),
2419 bld_base
->base
.elem_type
);
2424 static void preload_samplers(struct si_shader_context
*si_shader_ctx
)
2426 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2427 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2428 const struct tgsi_shader_info
* info
= bld_base
->info
;
2430 unsigned i
, num_samplers
= info
->file_max
[TGSI_FILE_SAMPLER
] + 1;
2432 LLVMValueRef res_ptr
, samp_ptr
;
2433 LLVMValueRef offset
;
2435 if (num_samplers
== 0)
2438 res_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_RESOURCE
);
2439 samp_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
, SI_PARAM_SAMPLER
);
2441 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2442 for (i
= 0; i
< num_samplers
; ++i
) {
2444 offset
= lp_build_const_int32(gallivm
, i
);
2445 si_shader_ctx
->resources
[i
] = build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
2448 offset
= lp_build_const_int32(gallivm
, i
);
2449 si_shader_ctx
->samplers
[i
] = build_indexed_load(si_shader_ctx
, samp_ptr
, offset
);
2451 /* FMASK resource */
2452 if (info
->is_msaa_sampler
[i
]) {
2453 offset
= lp_build_const_int32(gallivm
, SI_FMASK_TEX_OFFSET
+ i
);
2454 si_shader_ctx
->resources
[SI_FMASK_TEX_OFFSET
+ i
] =
2455 build_indexed_load(si_shader_ctx
, res_ptr
, offset
);
2460 static void preload_streamout_buffers(struct si_shader_context
*si_shader_ctx
)
2462 struct lp_build_tgsi_context
* bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2463 struct gallivm_state
* gallivm
= bld_base
->base
.gallivm
;
2466 if (si_shader_ctx
->type
!= TGSI_PROCESSOR_VERTEX
||
2467 si_shader_ctx
->shader
->key
.vs
.as_es
||
2468 !si_shader_ctx
->shader
->selector
->so
.num_outputs
)
2471 LLVMValueRef buf_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2472 SI_PARAM_RW_BUFFERS
);
2474 /* Load the resources, we rely on the code sinking to do the rest */
2475 for (i
= 0; i
< 4; ++i
) {
2476 if (si_shader_ctx
->shader
->selector
->so
.stride
[i
]) {
2477 LLVMValueRef offset
= lp_build_const_int32(gallivm
,
2478 SI_SO_BUF_OFFSET
+ i
);
2480 si_shader_ctx
->so_buffers
[i
] = build_indexed_load(si_shader_ctx
, buf_ptr
, offset
);
2485 int si_compile_llvm(struct si_screen
*sscreen
, struct si_shader
*shader
,
2488 unsigned r
; /* llvm_compile result */
2491 struct radeon_shader_binary binary
;
2492 bool dump
= r600_can_dump_shader(&sscreen
->b
,
2493 shader
->selector
? shader
->selector
->tokens
: NULL
);
2494 const char * gpu_family
= r600_get_llvm_processor_name(sscreen
->b
.family
);
2497 /* Use LLVM to compile shader */
2498 memset(&binary
, 0, sizeof(binary
));
2499 r
= radeon_llvm_compile(mod
, &binary
, gpu_family
, dump
);
2501 /* Output binary dump if rscreen->debug_flags are set */
2502 if (dump
&& ! binary
.disassembled
) {
2503 fprintf(stderr
, "SI CODE:\n");
2504 for (i
= 0; i
< binary
.code_size
; i
+=4 ) {
2505 fprintf(stderr
, "%02x%02x%02x%02x\n", binary
.code
[i
+ 3],
2506 binary
.code
[i
+ 2], binary
.code
[i
+ 1],
2511 /* XXX: We may be able to emit some of these values directly rather than
2512 * extracting fields to be emitted later.
2514 /* Parse config data in compiled binary */
2515 for (i
= 0; i
< binary
.config_size
; i
+= 8) {
2516 unsigned reg
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
));
2517 unsigned value
= util_le32_to_cpu(*(uint32_t*)(binary
.config
+ i
+ 4));
2519 case R_00B028_SPI_SHADER_PGM_RSRC1_PS
:
2520 case R_00B128_SPI_SHADER_PGM_RSRC1_VS
:
2521 case R_00B228_SPI_SHADER_PGM_RSRC1_GS
:
2522 case R_00B848_COMPUTE_PGM_RSRC1
:
2523 shader
->num_sgprs
= (G_00B028_SGPRS(value
) + 1) * 8;
2524 shader
->num_vgprs
= (G_00B028_VGPRS(value
) + 1) * 4;
2526 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS
:
2527 shader
->lds_size
= G_00B02C_EXTRA_LDS_SIZE(value
);
2529 case R_00B84C_COMPUTE_PGM_RSRC2
:
2530 shader
->lds_size
= G_00B84C_LDS_SIZE(value
);
2532 case R_0286CC_SPI_PS_INPUT_ENA
:
2533 shader
->spi_ps_input_ena
= value
;
2535 case R_00B860_COMPUTE_TMPRING_SIZE
:
2536 /* WAVESIZE is in units of 256 dwords. */
2537 shader
->scratch_bytes_per_wave
=
2538 G_00B860_WAVESIZE(value
) * 256 * 4 * 1;
2541 fprintf(stderr
, "Warning: Compiler emitted unknown "
2542 "config register: 0x%x\n", reg
);
2547 /* copy new shader */
2548 code_size
= binary
.code_size
+ binary
.rodata_size
;
2549 r600_resource_reference(&shader
->bo
, NULL
);
2550 shader
->bo
= si_resource_create_custom(&sscreen
->b
.b
, PIPE_USAGE_IMMUTABLE
,
2552 if (shader
->bo
== NULL
) {
2556 ptr
= sscreen
->b
.ws
->buffer_map(shader
->bo
->cs_buf
, NULL
, PIPE_TRANSFER_WRITE
);
2557 util_memcpy_cpu_to_le32(ptr
, binary
.code
, binary
.code_size
);
2558 if (binary
.rodata_size
> 0) {
2559 ptr
+= binary
.code_size
;
2560 util_memcpy_cpu_to_le32(ptr
, binary
.rodata
, binary
.rodata_size
);
2563 sscreen
->b
.ws
->buffer_unmap(shader
->bo
->cs_buf
);
2566 free(binary
.config
);
2567 free(binary
.rodata
);
2572 /* Generate code for the hardware VS shader stage to go with a geometry shader */
2573 static int si_generate_gs_copy_shader(struct si_screen
*sscreen
,
2574 struct si_shader_context
*si_shader_ctx
,
2575 struct si_shader
*gs
, bool dump
)
2577 struct gallivm_state
*gallivm
= &si_shader_ctx
->radeon_bld
.gallivm
;
2578 struct lp_build_tgsi_context
*bld_base
= &si_shader_ctx
->radeon_bld
.soa
.bld_base
;
2579 struct lp_build_context
*base
= &bld_base
->base
;
2580 struct lp_build_context
*uint
= &bld_base
->uint_bld
;
2581 struct si_shader
*shader
= si_shader_ctx
->shader
;
2582 struct si_shader_output_values
*outputs
;
2583 struct tgsi_shader_info
*gsinfo
= &gs
->selector
->info
;
2584 LLVMValueRef t_list_ptr
, t_list
;
2585 LLVMValueRef args
[9];
2588 outputs
= MALLOC(gsinfo
->num_outputs
* sizeof(outputs
[0]));
2590 si_shader_ctx
->type
= TGSI_PROCESSOR_VERTEX
;
2591 shader
->is_gs_copy_shader
= true;
2593 radeon_llvm_context_init(&si_shader_ctx
->radeon_bld
);
2595 create_meta_data(si_shader_ctx
);
2596 create_function(si_shader_ctx
);
2597 preload_streamout_buffers(si_shader_ctx
);
2599 /* Load the GSVS ring resource descriptor */
2600 t_list_ptr
= LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2601 SI_PARAM_RW_BUFFERS
);
2602 t_list
= build_indexed_load(si_shader_ctx
, t_list_ptr
,
2603 lp_build_const_int32(gallivm
, SI_RING_GSVS
));
2606 args
[1] = lp_build_mul_imm(uint
,
2607 LLVMGetParam(si_shader_ctx
->radeon_bld
.main_fn
,
2608 si_shader_ctx
->param_vertex_id
),
2610 args
[3] = uint
->zero
;
2611 args
[4] = uint
->one
; /* OFFEN */
2612 args
[5] = uint
->zero
; /* IDXEN */
2613 args
[6] = uint
->one
; /* GLC */
2614 args
[7] = uint
->one
; /* SLC */
2615 args
[8] = uint
->zero
; /* TFE */
2617 /* Fetch vertex data from GSVS ring */
2618 for (i
= 0; i
< gsinfo
->num_outputs
; ++i
) {
2621 outputs
[i
].name
= gsinfo
->output_semantic_name
[i
];
2622 outputs
[i
].sid
= gsinfo
->output_semantic_index
[i
];
2624 for (chan
= 0; chan
< 4; chan
++) {
2625 args
[2] = lp_build_const_int32(gallivm
,
2627 gs
->selector
->gs_max_out_vertices
* 16 * 4);
2629 outputs
[i
].values
[chan
] =
2630 LLVMBuildBitCast(gallivm
->builder
,
2631 build_intrinsic(gallivm
->builder
,
2632 "llvm.SI.buffer.load.dword.i32.i32",
2633 LLVMInt32TypeInContext(gallivm
->context
),
2635 LLVMReadOnlyAttribute
| LLVMNoUnwindAttribute
),
2636 base
->elem_type
, "");
2640 si_llvm_export_vs(bld_base
, outputs
, gsinfo
->num_outputs
);
2642 radeon_llvm_finalize_module(&si_shader_ctx
->radeon_bld
);
2645 fprintf(stderr
, "Copy Vertex Shader for Geometry Shader:\n\n");
2647 r
= si_compile_llvm(sscreen
, si_shader_ctx
->shader
,
2648 bld_base
->base
.gallivm
->module
);
2650 radeon_llvm_dispose(&si_shader_ctx
->radeon_bld
);
2656 int si_shader_create(struct si_screen
*sscreen
, struct si_shader
*shader
)
2658 struct si_shader_selector
*sel
= shader
->selector
;
2659 struct si_shader_context si_shader_ctx
;
2660 struct lp_build_tgsi_context
* bld_base
;
2663 bool dump
= r600_can_dump_shader(&sscreen
->b
, sel
->tokens
);
2665 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2666 * conversion fails. */
2668 tgsi_dump(sel
->tokens
, 0);
2669 si_dump_streamout(&sel
->so
);
2672 assert(shader
->nparam
== 0);
2674 memset(&si_shader_ctx
, 0, sizeof(si_shader_ctx
));
2675 radeon_llvm_context_init(&si_shader_ctx
.radeon_bld
);
2676 bld_base
= &si_shader_ctx
.radeon_bld
.soa
.bld_base
;
2678 if (sel
->info
.uses_kill
)
2679 shader
->db_shader_control
|= S_02880C_KILL_ENABLE(1);
2681 shader
->uses_instanceid
= sel
->info
.uses_instanceid
;
2682 bld_base
->info
= &sel
->info
;
2683 bld_base
->emit_fetch_funcs
[TGSI_FILE_CONSTANT
] = fetch_constant
;
2685 bld_base
->op_actions
[TGSI_OPCODE_TEX
] = tex_action
;
2686 bld_base
->op_actions
[TGSI_OPCODE_TEX2
] = tex_action
;
2687 bld_base
->op_actions
[TGSI_OPCODE_TXB
] = tex_action
;
2688 bld_base
->op_actions
[TGSI_OPCODE_TXB2
] = tex_action
;
2689 bld_base
->op_actions
[TGSI_OPCODE_TXD
] = tex_action
;
2690 bld_base
->op_actions
[TGSI_OPCODE_TXF
] = tex_action
;
2691 bld_base
->op_actions
[TGSI_OPCODE_TXL
] = tex_action
;
2692 bld_base
->op_actions
[TGSI_OPCODE_TXL2
] = tex_action
;
2693 bld_base
->op_actions
[TGSI_OPCODE_TXP
] = tex_action
;
2694 bld_base
->op_actions
[TGSI_OPCODE_TXQ
] = txq_action
;
2695 bld_base
->op_actions
[TGSI_OPCODE_TG4
] = tex_action
;
2696 bld_base
->op_actions
[TGSI_OPCODE_LODQ
] = tex_action
;
2698 bld_base
->op_actions
[TGSI_OPCODE_DDX
].emit
= si_llvm_emit_ddxy
;
2699 bld_base
->op_actions
[TGSI_OPCODE_DDY
].emit
= si_llvm_emit_ddxy
;
2701 bld_base
->op_actions
[TGSI_OPCODE_EMIT
].emit
= si_llvm_emit_vertex
;
2702 bld_base
->op_actions
[TGSI_OPCODE_ENDPRIM
].emit
= si_llvm_emit_primitive
;
2704 si_shader_ctx
.radeon_bld
.load_system_value
= declare_system_value
;
2705 si_shader_ctx
.tokens
= sel
->tokens
;
2706 tgsi_parse_init(&si_shader_ctx
.parse
, si_shader_ctx
.tokens
);
2707 si_shader_ctx
.shader
= shader
;
2708 si_shader_ctx
.type
= si_shader_ctx
.parse
.FullHeader
.Processor
.Processor
;
2710 switch (si_shader_ctx
.type
) {
2711 case TGSI_PROCESSOR_VERTEX
:
2712 si_shader_ctx
.radeon_bld
.load_input
= declare_input_vs
;
2713 if (shader
->key
.vs
.as_es
) {
2714 bld_base
->emit_epilogue
= si_llvm_emit_es_epilogue
;
2716 bld_base
->emit_epilogue
= si_llvm_emit_vs_epilogue
;
2719 case TGSI_PROCESSOR_GEOMETRY
:
2720 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = fetch_input_gs
;
2721 bld_base
->emit_epilogue
= si_llvm_emit_gs_epilogue
;
2723 case TGSI_PROCESSOR_FRAGMENT
:
2724 si_shader_ctx
.radeon_bld
.load_input
= declare_input_fs
;
2725 bld_base
->emit_epilogue
= si_llvm_emit_fs_epilogue
;
2727 switch (sel
->info
.properties
[TGSI_PROPERTY_FS_DEPTH_LAYOUT
]) {
2728 case TGSI_FS_DEPTH_LAYOUT_GREATER
:
2729 shader
->db_shader_control
|=
2730 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z
);
2732 case TGSI_FS_DEPTH_LAYOUT_LESS
:
2733 shader
->db_shader_control
|=
2734 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z
);
2739 assert(!"Unsupported shader type");
2743 create_meta_data(&si_shader_ctx
);
2744 create_function(&si_shader_ctx
);
2745 preload_constants(&si_shader_ctx
);
2746 preload_samplers(&si_shader_ctx
);
2747 preload_streamout_buffers(&si_shader_ctx
);
2749 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
2750 si_shader_ctx
.gs_next_vertex
=
2751 lp_build_alloca(bld_base
->base
.gallivm
,
2752 bld_base
->uint_bld
.elem_type
, "");
2755 if (!lp_build_tgsi_llvm(bld_base
, sel
->tokens
)) {
2756 fprintf(stderr
, "Failed to translate shader from TGSI to LLVM\n");
2760 radeon_llvm_finalize_module(&si_shader_ctx
.radeon_bld
);
2762 mod
= bld_base
->base
.gallivm
->module
;
2763 r
= si_compile_llvm(sscreen
, shader
, mod
);
2765 fprintf(stderr
, "LLVM failed to compile shader\n");
2769 radeon_llvm_dispose(&si_shader_ctx
.radeon_bld
);
2771 if (si_shader_ctx
.type
== TGSI_PROCESSOR_GEOMETRY
) {
2772 shader
->gs_copy_shader
= CALLOC_STRUCT(si_shader
);
2773 shader
->gs_copy_shader
->selector
= shader
->selector
;
2774 shader
->gs_copy_shader
->key
= shader
->key
;
2775 si_shader_ctx
.shader
= shader
->gs_copy_shader
;
2776 if ((r
= si_generate_gs_copy_shader(sscreen
, &si_shader_ctx
,
2778 free(shader
->gs_copy_shader
);
2779 shader
->gs_copy_shader
= NULL
;
2784 tgsi_parse_free(&si_shader_ctx
.parse
);
2787 for (int i
= 0; i
< SI_NUM_CONST_BUFFERS
; i
++)
2788 FREE(si_shader_ctx
.constants
[i
]);
2793 void si_shader_destroy(struct pipe_context
*ctx
, struct si_shader
*shader
)
2795 if (shader
->gs_copy_shader
)
2796 si_shader_destroy(ctx
, shader
->gs_copy_shader
);
2798 r600_resource_reference(&shader
->bo
, NULL
);
2799 r600_resource_reference(&shader
->scratch_bo
, NULL
);