blob: 8696d03ca922b68d22dc1c1cae4d691581e01ec3 [file] [log] [blame]
Christian Königce40e472012-08-02 12:14:59 +02001/*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 *
23 * Authors:
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
27 */
28
Tom Stellarda75c6162012-01-06 17:38:37 -050029#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020030#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050031#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010032#include "gallivm/lp_bld_logic.h"
Christian König5e616cf2013-03-07 11:58:56 +010033#include "gallivm/lp_bld_arit.h"
Marek Olšák8d03d922013-09-01 23:59:06 +020034#include "gallivm/lp_bld_flow.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050035#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040036#include "radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010037#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010039#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_dump.h"
41
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010042#include "si_pipe.h"
43#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050044#include "sid.h"
45
Tom Stellarda75c6162012-01-06 17:38:37 -050046#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050047
Michel Dänzer404b29d2013-11-21 16:45:28 +090048struct si_shader_output_values
49{
50 LLVMValueRef values[4];
51 unsigned name;
52 unsigned index;
53 unsigned usage;
54};
55
Tom Stellarda75c6162012-01-06 17:38:37 -050056struct si_shader_context
57{
58 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050059 struct tgsi_parse_context parse;
60 struct tgsi_token * tokens;
61 struct si_pipe_shader *shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +090062 struct si_shader *gs_for_vs;
Tom Stellarda75c6162012-01-06 17:38:37 -050063 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020064 int param_streamout_config;
65 int param_streamout_write_index;
66 int param_streamout_offset[4];
67 int param_vertex_id;
68 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010069 LLVMValueRef const_md;
Marek Olšák2fd42002013-10-25 11:45:47 +020070 LLVMValueRef const_resource[NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020071#if HAVE_LLVM >= 0x0304
72 LLVMValueRef ddxy_lds;
73#endif
Marek Olšák2fd42002013-10-25 11:45:47 +020074 LLVMValueRef *constants[NUM_CONST_BUFFERS];
Christian König1c100182013-03-17 16:02:42 +010075 LLVMValueRef *resources;
76 LLVMValueRef *samplers;
Marek Olšák8d03d922013-09-01 23:59:06 +020077 LLVMValueRef so_buffers[4];
Michel Dänzerf07a96d2014-01-08 18:45:10 +090078 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050079};
80
81static struct si_shader_context * si_shader_context(
82 struct lp_build_tgsi_context * bld_base)
83{
84 return (struct si_shader_context *)bld_base;
85}
86
87
88#define PERSPECTIVE_BASE 0
89#define LINEAR_BASE 9
90
91#define SAMPLE_OFFSET 0
92#define CENTER_OFFSET 2
93#define CENTROID_OFSET 4
94
95#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040096#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020097#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040098#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050099
Michel Dänzer404b29d2013-11-21 16:45:28 +0900100
101#define SENDMSG_GS 2
102#define SENDMSG_GS_DONE 3
103
104#define SENDMSG_GS_OP_NOP (0 << 4)
105#define SENDMSG_GS_OP_CUT (1 << 4)
106#define SENDMSG_GS_OP_EMIT (2 << 4)
107#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
108
109
Tom Stellard467f5162012-05-16 15:15:35 -0400110/**
111 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
112 *
113 * @param offset The offset parameter specifies the number of
114 * elements to offset, not the number of bytes or dwords. An element is the
115 * the type pointed to by the base_ptr parameter (e.g. int is the element of
116 * an int* pointer)
117 *
118 * When LLVM lowers the load instruction, it will convert the element offset
119 * into a dword offset automatically.
120 *
121 */
122static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100123 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400124 LLVMValueRef base_ptr,
125 LLVMValueRef offset)
126{
Christian König206f0592013-03-20 14:37:21 +0100127 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400128
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200129 LLVMValueRef indices[2] = {
130 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
131 offset
132 };
Christian König206f0592013-03-20 14:37:21 +0100133 LLVMValueRef computed_ptr = LLVMBuildGEP(
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200134 base->gallivm->builder, base_ptr, indices, 2, "");
Christian König206f0592013-03-20 14:37:21 +0100135
136 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
137 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
138 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400139}
140
Marek Olšákf317ce52013-09-05 15:39:57 +0200141static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100142 struct radeon_llvm_context * radeon_bld,
143 unsigned divisor)
144{
Marek Olšák8d03d922013-09-01 23:59:06 +0200145 struct si_shader_context *si_shader_ctx =
146 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100147 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
148
Marek Olšák8d03d922013-09-01 23:59:06 +0200149 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
150 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100151 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
152 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
153
154 if (divisor > 1)
155 result = LLVMBuildUDiv(gallivm->builder, result,
156 lp_build_const_int32(gallivm, divisor), "");
157
158 return result;
159}
160
Tom Stellarda75c6162012-01-06 17:38:37 -0500161static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900162 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500163 unsigned input_index,
164 const struct tgsi_full_declaration *decl)
165{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900166 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
167 struct gallivm_state *gallivm = base->gallivm;
168 struct si_shader_context *si_shader_ctx =
169 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100170 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
171
172 unsigned chan;
173
Tom Stellarda75c6162012-01-06 17:38:37 -0500174 LLVMValueRef t_list_ptr;
175 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400176 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500177 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100178 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400179 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500180 LLVMTypeRef vec4_type;
181 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500182
Tom Stellard467f5162012-05-16 15:15:35 -0400183 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100184 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500185
Michel Dänzer51f89a02013-12-09 15:33:53 +0900186 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400187
Christian König206f0592013-03-20 14:37:21 +0100188 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400189
190 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900191 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500192
Christian Königa0dca442013-03-22 15:59:22 +0100193 if (divisor) {
194 /* Build index from instance ID, start instance and divisor */
195 si_shader_ctx->shader->shader.uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200196 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100197 } else {
198 /* Load the buffer index, which is always stored in VGPR0
199 * for Vertex Shaders */
Marek Olšák8d03d922013-09-01 23:59:06 +0200200 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
201 si_shader_ctx->param_vertex_id);
Christian Königa0dca442013-03-22 15:59:22 +0100202 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500203
204 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400205 args[0] = t_list;
206 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100207 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900208 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100209 "llvm.SI.vs.load.input", vec4_type, args, 3,
210 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500211
212 /* Break up the vec4 into individual components */
213 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900214 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500215 /* XXX: Use a helper function for this. There is one in
216 * tgsi_llvm.c. */
217 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900218 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500219 input, llvm_chan, "");
220 }
221}
222
Michel Dänzer404b29d2013-11-21 16:45:28 +0900223static void declare_input_gs(
224 struct radeon_llvm_context *radeon_bld,
225 unsigned input_index,
226 const struct tgsi_full_declaration *decl)
227{
228 /* Nothing to do, inputs are handled in fetch_input_gs() below */
229}
230
231static LLVMValueRef fetch_input_gs(
232 struct lp_build_tgsi_context *bld_base,
233 const struct tgsi_full_src_register *reg,
234 enum tgsi_opcode_type type,
235 unsigned swizzle)
236{
237 struct lp_build_context *base = &bld_base->base;
238 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
239 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
240 struct gallivm_state *gallivm = base->gallivm;
241 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
242 LLVMValueRef vtx_offset;
243 LLVMValueRef t_list_ptr;
244 LLVMValueRef t_list;
245 LLVMValueRef args[9];
246 unsigned vtx_offset_param;
247
248 if (!reg->Register.Dimension)
249 return NULL;
250
251 if (swizzle == ~0) {
252 LLVMValueRef values[TGSI_NUM_CHANNELS];
253 unsigned chan;
254 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
255 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
256 }
257 return lp_build_gather_values(bld_base->base.gallivm, values,
258 TGSI_NUM_CHANNELS);
259 }
260
261 /* Get the vertex offset parameter */
262 vtx_offset_param = reg->Dimension.Index;
263 if (vtx_offset_param < 2) {
264 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
265 } else {
266 assert(vtx_offset_param < 6);
267 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
268 }
269 vtx_offset = lp_build_mul_imm(uint,
270 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
271 vtx_offset_param),
272 4);
273
274 /* Load the ESGS ring resource descriptor */
275 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
276 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
277 lp_build_const_int32(gallivm,
278 NUM_PIPE_CONST_BUFFERS + 1));
279
280 args[0] = t_list;
281 args[1] = vtx_offset;
282 args[2] = lp_build_const_int32(gallivm,
283 ((reg->Register.Index * 4) + swizzle) * 256);
284 args[3] = uint->zero;
285 args[4] = uint->one; /* OFFEN */
286 args[5] = uint->zero; /* IDXEN */
287 args[6] = uint->one; /* GLC */
288 args[7] = uint->zero; /* SLC */
289 args[8] = uint->zero; /* TFE */
290
291 return LLVMBuildBitCast(gallivm->builder,
292 build_intrinsic(gallivm->builder,
293 "llvm.SI.buffer.load.dword.i32.i32",
294 i32, args, 9,
295 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
296 tgsi2llvmtype(bld_base, type), "");
297}
298
Tom Stellarda75c6162012-01-06 17:38:37 -0500299static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900300 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500301 unsigned input_index,
302 const struct tgsi_full_declaration *decl)
303{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900304 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
305 struct si_shader_context *si_shader_ctx =
306 si_shader_context(&radeon_bld->soa.bld_base);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200307 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900308 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
309 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400310 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900311 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100312
313 LLVMValueRef interp_param;
314 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500315
316 /* This value is:
317 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
318 * quad begins a new primitive. Bit 0 always needs
319 * to be unset)
320 * [32:16] ParamOffset
321 *
322 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900323 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200324 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500325
Christian König0666ffd2013-03-05 15:07:39 +0100326 unsigned chan;
327
Tom Stellard0fb1e682012-09-06 16:18:11 -0400328 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
329 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400330 unsigned soa_index =
331 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900332 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100333 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100334
335 if (chan == 3)
336 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900337 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100338 LLVMBuildFDiv(gallivm->builder,
339 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900340 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100341 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400342 }
343 return;
344 }
345
Michel Dänzer97078b12012-09-25 12:41:31 +0200346 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
347 LLVMValueRef face, is_face_positive;
348
Christian König0666ffd2013-03-05 15:07:39 +0100349 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
350
Michel Dänzer97078b12012-09-25 12:41:31 +0200351 is_face_positive = LLVMBuildFCmp(gallivm->builder,
352 LLVMRealUGT, face,
353 lp_build_const_float(gallivm, 0.0f),
354 "");
355
Michel Dänzer51f89a02013-12-09 15:33:53 +0900356 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200357 LLVMBuildSelect(gallivm->builder,
358 is_face_positive,
359 lp_build_const_float(gallivm, 1.0f),
360 lp_build_const_float(gallivm, 0.0f),
361 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900362 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
363 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200364 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900365 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200366 lp_build_const_float(gallivm, 1.0f);
367
368 return;
369 }
370
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200371 shader->input[input_index].param_offset = shader->ninterp++;
372 attr_number = lp_build_const_int32(gallivm,
373 shader->input[input_index].param_offset);
374
Francisco Jerez12799232012-04-30 18:27:52 +0200375 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500376 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100377 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100378 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200379 } else {
380 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100381 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200382 else
Christian König0666ffd2013-03-05 15:07:39 +0100383 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200384 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500385 break;
386 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100387 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500388 break;
389 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200390 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100391 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200392 else
Christian König0666ffd2013-03-05 15:07:39 +0100393 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200394 break;
395 case TGSI_INTERPOLATE_PERSPECTIVE:
396 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100397 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200398 else
Christian König0666ffd2013-03-05 15:07:39 +0100399 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500400 break;
401 default:
402 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
403 return;
404 }
405
Christian König0666ffd2013-03-05 15:07:39 +0100406 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
407
Tom Stellarda75c6162012-01-06 17:38:37 -0500408 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200409 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100410 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100411 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200412 LLVMValueRef face, is_face_positive;
413 LLVMValueRef back_attr_number =
414 lp_build_const_int32(gallivm,
415 shader->input[input_index].param_offset + 1);
416
Christian König0666ffd2013-03-05 15:07:39 +0100417 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
418
Michel Dänzer691f08d2012-09-06 18:03:38 +0200419 is_face_positive = LLVMBuildFCmp(gallivm->builder,
420 LLVMRealUGT, face,
421 lp_build_const_float(gallivm, 0.0f),
422 "");
423
Tom Stellarda75c6162012-01-06 17:38:37 -0500424 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100425 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200426 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
427 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
428 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
429 LLVMValueRef front, back;
430
431 args[0] = llvm_chan;
432 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900433 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100434 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100435 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200436
437 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900438 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100439 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100440 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200441
Michel Dänzer51f89a02013-12-09 15:33:53 +0900442 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200443 LLVMBuildSelect(gallivm->builder,
444 is_face_positive,
445 front,
446 back,
447 "");
448 }
449
450 shader->ninterp++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200451 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
452 LLVMValueRef args[4];
453
454 args[0] = uint->zero;
455 args[1] = attr_number;
456 args[2] = params;
457 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900458 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
459 build_intrinsic(gallivm->builder, intr_name,
460 input_type, args, args[3] ? 4 : 3,
461 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
462 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
463 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200464 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900465 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200466 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200467 } else {
468 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100469 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200470 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
471 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
472 args[0] = llvm_chan;
473 args[1] = attr_number;
474 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100475 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900476 radeon_bld->inputs[soa_index] =
477 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100478 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100479 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200480 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500481 }
482}
483
Christian Könige4ed5872013-03-21 18:02:52 +0100484static void declare_system_value(
485 struct radeon_llvm_context * radeon_bld,
486 unsigned index,
487 const struct tgsi_full_declaration *decl)
488{
Marek Olšák8d03d922013-09-01 23:59:06 +0200489 struct si_shader_context *si_shader_ctx =
490 si_shader_context(&radeon_bld->soa.bld_base);
Christian Könige4ed5872013-03-21 18:02:52 +0100491 LLVMValueRef value = 0;
492
493 switch (decl->Semantic.Name) {
494 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200495 value = LLVMGetParam(radeon_bld->main_fn,
496 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100497 break;
498
499 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200500 value = LLVMGetParam(radeon_bld->main_fn,
501 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100502 break;
503
504 default:
505 assert(!"unknown system value");
506 return;
507 }
508
509 radeon_bld->system_values[index] = value;
510}
511
Tom Stellarda75c6162012-01-06 17:38:37 -0500512static LLVMValueRef fetch_constant(
513 struct lp_build_tgsi_context * bld_base,
514 const struct tgsi_full_src_register *reg,
515 enum tgsi_opcode_type type,
516 unsigned swizzle)
517{
Christian König55fe5cc2013-03-04 16:30:06 +0100518 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500519 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100520 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200521 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500522
Christian Königf5298b02013-02-28 14:50:07 +0100523 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100524 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100525 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500526
Christian König8514f5a2013-02-04 17:46:42 +0100527 if (swizzle == LP_CHAN_ALL) {
528 unsigned chan;
529 LLVMValueRef values[4];
530 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
531 values[chan] = fetch_constant(bld_base, reg, type, chan);
532
533 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
534 }
535
Marek Olšák2fd42002013-10-25 11:45:47 +0200536 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100537 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100538
Marek Olšák2fd42002013-10-25 11:45:47 +0200539 if (!reg->Register.Indirect)
540 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
541
542 args[0] = si_shader_ctx->const_resource[buf];
Christian König0f6cf2b2013-03-15 15:53:25 +0100543 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
544 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
545 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
546 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
547 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200548
Christian Königf5298b02013-02-28 14:50:07 +0100549 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100550 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500551
Christian Königf5298b02013-02-28 14:50:07 +0100552 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500553}
554
Michel Dänzer26c71392012-08-24 12:03:11 +0200555/* Initialize arguments for the shader export intrinsic */
556static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900557 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200558 unsigned target,
559 LLVMValueRef *args)
560{
561 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
562 struct lp_build_context *uint =
563 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
564 struct lp_build_context *base = &bld_base->base;
565 unsigned compressed = 0;
566 unsigned chan;
567
Michel Dänzerf402acd2012-08-22 18:15:36 +0200568 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
569 int cbuf = target - V_008DFC_SQ_EXP_MRT;
570
571 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100572 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100573
574 if (compressed)
575 si_shader_ctx->shader->spi_shader_col_format |=
576 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
577 else
578 si_shader_ctx->shader->spi_shader_col_format |=
579 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200580
581 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200582 }
583 }
584
585 if (compressed) {
586 /* Pixel shader needs to pack output values before export */
587 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900588 args[0] = values[2 * chan];
589 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200590 args[chan + 5] =
591 build_intrinsic(base->gallivm->builder,
592 "llvm.SI.packf16",
593 LLVMInt32TypeInContext(base->gallivm->context),
594 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100595 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100596 args[chan + 7] = args[chan + 5] =
597 LLVMBuildBitCast(base->gallivm->builder,
598 args[chan + 5],
599 LLVMFloatTypeInContext(base->gallivm->context),
600 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200601 }
602
603 /* Set COMPR flag */
604 args[4] = uint->one;
605 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900606 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200607 /* +5 because the first output value will be
608 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900609 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200610
611 /* Clear COMPR flag */
612 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200613 }
614
615 /* XXX: This controls which components of the output
616 * registers actually get exported. (e.g bit 0 means export
617 * X component, bit 1 means export Y component, etc.) I'm
618 * hard coding this to 0xf for now. In the future, we might
619 * want to do something else. */
620 args[0] = lp_build_const_int32(base->gallivm, 0xf);
621
622 /* Specify whether the EXEC mask represents the valid mask */
623 args[1] = uint->zero;
624
625 /* Specify whether this is the last export */
626 args[2] = uint->zero;
627
628 /* Specify the target we are exporting */
629 args[3] = lp_build_const_int32(base->gallivm, target);
630
Michel Dänzer26c71392012-08-24 12:03:11 +0200631 /* XXX: We probably need to keep track of the output
632 * values, so we know what we are passing to the next
633 * stage. */
634}
635
Michel Dänzer404b29d2013-11-21 16:45:28 +0900636/* Load from output pointers and initialize arguments for the shader export intrinsic */
637static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
638 LLVMValueRef *out_ptr,
639 unsigned target,
640 LLVMValueRef *args)
641{
642 struct gallivm_state *gallivm = bld_base->base.gallivm;
643 LLVMValueRef values[4];
644 int i;
645
646 for (i = 0; i < 4; i++)
647 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
648
649 si_llvm_init_export_args(bld_base, values, target, args);
650}
651
Michel Dänzer7708a862012-11-02 15:57:30 +0100652static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900653 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100654{
655 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
656 struct gallivm_state *gallivm = bld_base->base.gallivm;
657
Christian Königa0dca442013-03-22 15:59:22 +0100658 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400659 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
660 SI_PARAM_ALPHA_REF);
661
Michel Dänzer7708a862012-11-02 15:57:30 +0100662 LLVMValueRef alpha_pass =
663 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100664 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900665 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400666 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100667 LLVMValueRef arg =
668 lp_build_select(&bld_base->base,
669 alpha_pass,
670 lp_build_const_float(gallivm, 1.0f),
671 lp_build_const_float(gallivm, -1.0f));
672
673 build_intrinsic(gallivm->builder,
674 "llvm.AMDGPU.kill",
675 LLVMVoidTypeInContext(gallivm->context),
676 &arg, 1, 0);
677 } else {
678 build_intrinsic(gallivm->builder,
679 "llvm.AMDGPU.kilp",
680 LLVMVoidTypeInContext(gallivm->context),
681 NULL, 0, 0);
682 }
683}
684
Michel Dänzere3befbc2013-05-15 18:09:50 +0200685static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900686 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200687{
688 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200689 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200690 struct lp_build_context *base = &bld_base->base;
691 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200692 unsigned reg_index;
693 unsigned chan;
694 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200695 LLVMValueRef base_elt;
696 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšák2fd42002013-10-25 11:45:47 +0200697 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, NUM_PIPE_CONST_BUFFERS);
698 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200699
Michel Dänzere3befbc2013-05-15 18:09:50 +0200700 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200701 LLVMValueRef *args = pos[2 + reg_index];
702
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200703 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
704 continue;
705
706 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
707
Michel Dänzere3befbc2013-05-15 18:09:50 +0200708 args[5] =
709 args[6] =
710 args[7] =
711 args[8] = lp_build_const_float(base->gallivm, 0.0f);
712
713 /* Compute dot products of position and user clip plane vectors */
714 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
715 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
716 args[0] = const_resource;
717 args[1] = lp_build_const_int32(base->gallivm,
718 ((reg_index * 4 + chan) * 4 +
719 const_chan) * 4);
720 base_elt = build_intrinsic(base->gallivm->builder,
721 "llvm.SI.load.const",
722 base->elem_type,
723 args, 2,
724 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
725 args[5 + chan] =
726 lp_build_add(base, args[5 + chan],
727 lp_build_mul(base, base_elt,
728 out_elts[const_chan]));
729 }
730 }
731
732 args[0] = lp_build_const_int32(base->gallivm, 0xf);
733 args[1] = uint->zero;
734 args[2] = uint->zero;
735 args[3] = lp_build_const_int32(base->gallivm,
736 V_008DFC_SQ_EXP_POS + 2 + reg_index);
737 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200738 }
739}
740
Marek Olšák8d03d922013-09-01 23:59:06 +0200741static void si_dump_streamout(struct pipe_stream_output_info *so)
742{
743 unsigned i;
744
745 if (so->num_outputs)
746 fprintf(stderr, "STREAMOUT\n");
747
748 for (i = 0; i < so->num_outputs; i++) {
749 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
750 so->output[i].start_component;
751 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
752 i, so->output[i].output_buffer,
753 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
754 so->output[i].register_index,
755 mask & 1 ? "x" : "",
756 mask & 2 ? "y" : "",
757 mask & 4 ? "z" : "",
758 mask & 8 ? "w" : "");
759 }
760}
761
762/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
763 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
764 * or v4i32 (num_channels=3,4). */
765static void build_tbuffer_store(struct si_shader_context *shader,
766 LLVMValueRef rsrc,
767 LLVMValueRef vdata,
768 unsigned num_channels,
769 LLVMValueRef vaddr,
770 LLVMValueRef soffset,
771 unsigned inst_offset,
772 unsigned dfmt,
773 unsigned nfmt,
774 unsigned offen,
775 unsigned idxen,
776 unsigned glc,
777 unsigned slc,
778 unsigned tfe)
779{
780 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
781 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
782 LLVMValueRef args[] = {
783 rsrc,
784 vdata,
785 LLVMConstInt(i32, num_channels, 0),
786 vaddr,
787 soffset,
788 LLVMConstInt(i32, inst_offset, 0),
789 LLVMConstInt(i32, dfmt, 0),
790 LLVMConstInt(i32, nfmt, 0),
791 LLVMConstInt(i32, offen, 0),
792 LLVMConstInt(i32, idxen, 0),
793 LLVMConstInt(i32, glc, 0),
794 LLVMConstInt(i32, slc, 0),
795 LLVMConstInt(i32, tfe, 0)
796 };
797
798 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
799 unsigned func = CLAMP(num_channels, 1, 3) - 1;
800 const char *types[] = {"i32", "v2i32", "v4i32"};
801 char name[256];
802 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
803
804 lp_build_intrinsic(gallivm->builder, name,
805 LLVMVoidTypeInContext(gallivm->context),
806 args, Elements(args));
807}
808
809static void build_streamout_store(struct si_shader_context *shader,
810 LLVMValueRef rsrc,
811 LLVMValueRef vdata,
812 unsigned num_channels,
813 LLVMValueRef vaddr,
814 LLVMValueRef soffset,
815 unsigned inst_offset)
816{
817 static unsigned dfmt[] = {
818 V_008F0C_BUF_DATA_FORMAT_32,
819 V_008F0C_BUF_DATA_FORMAT_32_32,
820 V_008F0C_BUF_DATA_FORMAT_32_32_32,
821 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
822 };
823 assert(num_channels >= 1 && num_channels <= 4);
824
825 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
826 inst_offset, dfmt[num_channels-1],
827 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
828}
829
830/* On SI, the vertex shader is responsible for writing streamout data
831 * to buffers. */
832static void si_llvm_emit_streamout(struct si_shader_context *shader)
833{
834 struct pipe_stream_output_info *so = &shader->shader->selector->so;
835 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
836 LLVMBuilderRef builder = gallivm->builder;
837 int i, j;
838 struct lp_build_if_state if_ctx;
839
840 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
841
842 LLVMValueRef so_param =
843 LLVMGetParam(shader->radeon_bld.main_fn,
844 shader->param_streamout_config);
845
846 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
847 LLVMValueRef so_vtx_count =
848 LLVMBuildAnd(builder,
849 LLVMBuildLShr(builder, so_param,
850 LLVMConstInt(i32, 16, 0), ""),
851 LLVMConstInt(i32, 127, 0), "");
852
853 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
854 NULL, 0, LLVMReadNoneAttribute);
855
856 /* can_emit = tid < so_vtx_count; */
857 LLVMValueRef can_emit =
858 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
859
860 /* Emit the streamout code conditionally. This actually avoids
861 * out-of-bounds buffer access. The hw tells us via the SGPR
862 * (so_vtx_count) which threads are allowed to emit streamout data. */
863 lp_build_if(&if_ctx, gallivm, can_emit);
864 {
865 /* The buffer offset is computed as follows:
866 * ByteOffset = streamout_offset[buffer_id]*4 +
867 * (streamout_write_index + thread_id)*stride[buffer_id] +
868 * attrib_offset
869 */
870
871 LLVMValueRef so_write_index =
872 LLVMGetParam(shader->radeon_bld.main_fn,
873 shader->param_streamout_write_index);
874
875 /* Compute (streamout_write_index + thread_id). */
876 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
877
878 /* Compute the write offset for each enabled buffer. */
879 LLVMValueRef so_write_offset[4] = {};
880 for (i = 0; i < 4; i++) {
881 if (!so->stride[i])
882 continue;
883
884 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
885 shader->param_streamout_offset[i]);
886 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
887
888 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
889 LLVMConstInt(i32, so->stride[i]*4, 0), "");
890 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
891 }
892
893 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS] = shader->radeon_bld.soa.outputs;
894
895 /* Write streamout data. */
896 for (i = 0; i < so->num_outputs; i++) {
897 unsigned buf_idx = so->output[i].output_buffer;
898 unsigned reg = so->output[i].register_index;
899 unsigned start = so->output[i].start_component;
900 unsigned num_comps = so->output[i].num_components;
901 LLVMValueRef out[4];
902
903 assert(num_comps && num_comps <= 4);
904 if (!num_comps || num_comps > 4)
905 continue;
906
907 /* Load the output as int. */
908 for (j = 0; j < num_comps; j++) {
909 out[j] = LLVMBuildLoad(builder, outputs[reg][start+j], "");
910 out[j] = LLVMBuildBitCast(builder, out[j], i32, "");
911 }
912
913 /* Pack the output. */
914 LLVMValueRef vdata = NULL;
915
916 switch (num_comps) {
917 case 1: /* as i32 */
918 vdata = out[0];
919 break;
920 case 2: /* as v2i32 */
921 case 3: /* as v4i32 (aligned to 4) */
922 case 4: /* as v4i32 */
923 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
924 for (j = 0; j < num_comps; j++) {
925 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
926 LLVMConstInt(i32, j, 0), "");
927 }
928 break;
929 }
930
931 build_streamout_store(shader, shader->so_buffers[buf_idx],
932 vdata, num_comps,
933 so_write_offset[buf_idx],
934 LLVMConstInt(i32, 0, 0),
935 so->output[i].dst_offset*4);
936 }
937 }
938 lp_build_endif(&if_ctx);
939}
940
Michel Dänzer7435d9f2013-12-04 13:37:07 +0900941
Michel Dänzer51f89a02013-12-09 15:33:53 +0900942static int si_store_shader_io_attribs(struct si_shader *shader,
943 struct tgsi_full_declaration *d)
944{
945 int i = -1;
946
947 switch (d->Declaration.File) {
948 case TGSI_FILE_INPUT:
949 i = shader->ninput++;
950 assert(i < Elements(shader->input));
951 shader->input[i].name = d->Semantic.Name;
952 shader->input[i].sid = d->Semantic.Index;
953 shader->input[i].interpolate = d->Interp.Interpolate;
954 shader->input[i].centroid = d->Interp.Centroid;
955 return -1;
956
957 case TGSI_FILE_OUTPUT:
958 i = shader->noutput++;
959 assert(i < Elements(shader->output));
960 shader->output[i].name = d->Semantic.Name;
961 shader->output[i].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900962 shader->output[i].index = d->Range.First;
963 shader->output[i].usage = d->Declaration.UsageMask;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900964 break;
965 }
966
967 return i;
968}
969
Michel Dänzer404b29d2013-11-21 16:45:28 +0900970/* Generate export instructions for hardware VS shader stage */
971static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
972 struct si_shader_output_values *outputs,
973 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -0500974{
975 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200976 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500977 struct lp_build_context * base = &bld_base->base;
978 struct lp_build_context * uint =
979 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100980 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +0200981 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +0900982 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
983 unsigned semantic_name, semantic_index, semantic_usage;
984 unsigned target;
Christian König35088152012-08-01 22:35:24 +0200985 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900986 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +0200987 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -0500988
Marek Olšák8d03d922013-09-01 23:59:06 +0200989 if (si_shader_ctx->shader->selector->so.num_outputs) {
990 si_llvm_emit_streamout(si_shader_ctx);
991 }
992
Michel Dänzer404b29d2013-11-21 16:45:28 +0900993 for (i = 0; i < noutput; i++) {
994 semantic_name = outputs[i].name;
995 semantic_index = outputs[i].index;
996 semantic_usage = outputs[i].usage;
Tom Stellarda75c6162012-01-06 17:38:37 -0500997
Michel Dänzer0afeea52013-05-02 14:53:17 +0200998handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +0900999 /* Select the correct target */
1000 switch(semantic_name) {
1001 case TGSI_SEMANTIC_PSIZE:
1002 shader->vs_out_misc_write = true;
1003 shader->vs_out_point_size = true;
1004 psize_value = outputs[i].values[0];
1005 continue;
1006 case TGSI_SEMANTIC_EDGEFLAG:
1007 shader->vs_out_misc_write = true;
1008 shader->vs_out_edgeflag = true;
1009 edgeflag_value = outputs[i].values[0];
1010 continue;
1011 case TGSI_SEMANTIC_LAYER:
1012 shader->vs_out_misc_write = true;
1013 shader->vs_out_layer = true;
1014 layer_value = outputs[i].values[0];
1015 continue;
1016 case TGSI_SEMANTIC_POSITION:
1017 target = V_008DFC_SQ_EXP_POS;
1018 break;
1019 case TGSI_SEMANTIC_COLOR:
1020 case TGSI_SEMANTIC_BCOLOR:
1021 target = V_008DFC_SQ_EXP_PARAM + param_count;
1022 shader->output[i].param_offset = param_count;
1023 param_count++;
1024 break;
1025 case TGSI_SEMANTIC_CLIPDIST:
1026 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
1027 (1 << semantic_index)))
Marek Olšák053606d2013-11-19 22:07:30 +01001028 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001029 shader->clip_dist_write |=
1030 semantic_usage << (semantic_index << 2);
1031 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1032 break;
1033 case TGSI_SEMANTIC_CLIPVERTEX:
1034 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1035 continue;
1036 case TGSI_SEMANTIC_FOG:
1037 case TGSI_SEMANTIC_GENERIC:
1038 target = V_008DFC_SQ_EXP_PARAM + param_count;
1039 shader->output[i].param_offset = param_count;
1040 param_count++;
1041 break;
1042 default:
1043 target = 0;
1044 fprintf(stderr,
1045 "Warning: SI unhandled vs output type:%d\n",
1046 semantic_name);
1047 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001048
Michel Dänzer404b29d2013-11-21 16:45:28 +09001049 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001050
Michel Dänzer404b29d2013-11-21 16:45:28 +09001051 if (target >= V_008DFC_SQ_EXP_POS &&
1052 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1053 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1054 args, sizeof(args));
1055 } else {
1056 lp_build_intrinsic(base->gallivm->builder,
1057 "llvm.SI.export",
1058 LLVMVoidTypeInContext(base->gallivm->context),
1059 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001060 }
1061
1062 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1063 semantic_name = TGSI_SEMANTIC_GENERIC;
1064 goto handle_semantic;
1065 }
1066 }
1067
1068 /* We need to add the position output manually if it's missing. */
1069 if (!pos_args[0][0]) {
1070 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1071 pos_args[0][1] = uint->zero; /* EXEC mask */
1072 pos_args[0][2] = uint->zero; /* last export? */
1073 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1074 pos_args[0][4] = uint->zero; /* COMPR flag */
1075 pos_args[0][5] = base->zero; /* X */
1076 pos_args[0][6] = base->zero; /* Y */
1077 pos_args[0][7] = base->zero; /* Z */
1078 pos_args[0][8] = base->one; /* W */
1079 }
1080
1081 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1082 if (shader->vs_out_misc_write) {
1083 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1084 shader->vs_out_point_size |
1085 (shader->vs_out_edgeflag << 1) |
1086 (shader->vs_out_layer << 2));
1087 pos_args[1][1] = uint->zero; /* EXEC mask */
1088 pos_args[1][2] = uint->zero; /* last export? */
1089 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1090 pos_args[1][4] = uint->zero; /* COMPR flag */
1091 pos_args[1][5] = base->zero; /* X */
1092 pos_args[1][6] = base->zero; /* Y */
1093 pos_args[1][7] = base->zero; /* Z */
1094 pos_args[1][8] = base->zero; /* W */
1095
Michel Dänzer404b29d2013-11-21 16:45:28 +09001096 if (shader->vs_out_point_size)
1097 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001098
1099 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001100 /* The output is a float, but the hw expects an integer
1101 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001102 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1103 edgeflag_value,
1104 bld_base->uint_bld.elem_type, "");
1105 edgeflag_value = lp_build_min(&bld_base->int_bld,
1106 edgeflag_value,
1107 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001108
1109 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001110 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1111 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001112 base->elem_type, "");
1113 }
1114
Michel Dänzer404b29d2013-11-21 16:45:28 +09001115 if (shader->vs_out_layer)
1116 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001117 }
1118
1119 for (i = 0; i < 4; i++)
1120 if (pos_args[i][0])
1121 shader->nr_pos_exports++;
1122
1123 pos_idx = 0;
1124 for (i = 0; i < 4; i++) {
1125 if (!pos_args[i][0])
1126 continue;
1127
1128 /* Specify the target we are exporting */
1129 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1130
1131 if (pos_idx == shader->nr_pos_exports)
1132 /* Specify that this is the last export */
1133 pos_args[i][2] = uint->one;
1134
1135 lp_build_intrinsic(base->gallivm->builder,
1136 "llvm.SI.export",
1137 LLVMVoidTypeInContext(base->gallivm->context),
1138 pos_args[i], 9);
1139 }
1140}
1141
Michel Dänzer404b29d2013-11-21 16:45:28 +09001142static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1143{
1144 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1145 struct gallivm_state *gallivm = bld_base->base.gallivm;
1146 struct si_pipe_shader *shader = si_shader_ctx->shader;
1147 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1148 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1149 LLVMValueRef t_list_ptr;
1150 LLVMValueRef t_list;
1151 unsigned chan;
1152 int i;
1153
1154 while (!tgsi_parse_end_of_tokens(parse)) {
1155 struct tgsi_full_declaration *d =
1156 &parse->FullToken.FullDeclaration;
1157
1158 tgsi_parse_token(parse);
1159
1160 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1161 continue;
1162
1163 si_store_shader_io_attribs(&shader->shader, d);
1164 }
1165
1166 /* Load the ESGS ring resource descriptor */
1167 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1168 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
1169 lp_build_const_int32(gallivm,
1170 NUM_PIPE_CONST_BUFFERS + 1));
1171
1172 for (i = 0; i < shader->shader.noutput; i++) {
1173 LLVMValueRef *out_ptr =
1174 si_shader_ctx->radeon_bld.soa.outputs[shader->shader.output[i].index];
1175
1176 for (chan = 0; chan < 4; chan++) {
1177 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
1178 LLVMValueRef voffset =
1179 lp_build_const_int32(gallivm, (4 * i + chan) * 4);
1180 LLVMValueRef soffset =
1181 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1182 SI_PARAM_ES2GS_OFFSET);
1183
1184 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1185
1186 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
1187 voffset, soffset, 0,
1188 V_008F0C_BUF_DATA_FORMAT_32,
1189 V_008F0C_BUF_NUM_FORMAT_UINT,
1190 1, 0, 1, 1, 0);
1191 }
1192 }
1193}
1194
1195static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1196{
1197 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1198 struct gallivm_state *gallivm = bld_base->base.gallivm;
1199 LLVMValueRef args[2];
1200
1201 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1202 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1203 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1204 LLVMVoidTypeInContext(gallivm->context), args, 2,
1205 LLVMNoUnwindAttribute);
1206}
1207
1208static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1209{
1210 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1211 struct gallivm_state *gallivm = bld_base->base.gallivm;
1212 struct si_pipe_shader *shader = si_shader_ctx->shader;
1213 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1214 struct si_shader_output_values *outputs = NULL;
1215 unsigned noutput = 0;
1216 int i;
1217
1218 while (!tgsi_parse_end_of_tokens(parse)) {
1219 struct tgsi_full_declaration *d =
1220 &parse->FullToken.FullDeclaration;
1221 unsigned index;
1222
1223 tgsi_parse_token(parse);
1224
1225 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1226 continue;
1227
1228 i = si_store_shader_io_attribs(&shader->shader, d);
1229 if (i < 0)
1230 continue;
1231
1232 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1233 (noutput + 1) * sizeof(outputs[0]));
1234 for (index = d->Range.First; index <= d->Range.Last; index++) {
1235 outputs[noutput].name = d->Semantic.Name;
1236 outputs[noutput].index = d->Semantic.Index;
1237 outputs[noutput].usage = d->Declaration.UsageMask;
1238
1239 for (i = 0; i < 4; i++)
1240 outputs[noutput].values[i] =
1241 LLVMBuildLoad(gallivm->builder,
1242 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1243 "");
1244 }
1245 noutput++;
1246 }
1247
1248 si_llvm_export_vs(bld_base, outputs, noutput);
1249 FREE(outputs);
1250}
1251
Michel Dänzer51f89a02013-12-09 15:33:53 +09001252static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1253{
1254 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
1255 struct si_shader * shader = &si_shader_ctx->shader->shader;
1256 struct lp_build_context * base = &bld_base->base;
1257 struct lp_build_context * uint =
1258 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1259 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1260 LLVMValueRef args[9];
1261 LLVMValueRef last_args[9] = { 0 };
1262 unsigned semantic_name;
1263 int depth_index = -1, stencil_index = -1;
1264 int i;
1265
1266 while (!tgsi_parse_end_of_tokens(parse)) {
1267 struct tgsi_full_declaration *d =
1268 &parse->FullToken.FullDeclaration;
1269 unsigned target;
1270 unsigned index;
1271
1272 tgsi_parse_token(parse);
1273
1274 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
1275 parse->FullToken.FullProperty.Property.PropertyName ==
1276 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
1277 shader->fs_write_all = TRUE;
1278
1279 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1280 continue;
1281
1282 i = si_store_shader_io_attribs(shader, d);
1283 if (i < 0)
1284 continue;
1285
1286 semantic_name = d->Semantic.Name;
1287 for (index = d->Range.First; index <= d->Range.Last; index++) {
1288 /* Select the correct target */
1289 switch(semantic_name) {
1290 case TGSI_SEMANTIC_POSITION:
1291 depth_index = index;
1292 continue;
1293 case TGSI_SEMANTIC_STENCIL:
1294 stencil_index = index;
1295 continue;
1296 case TGSI_SEMANTIC_COLOR:
1297 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1298 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001299 LLVMBuildStore(bld_base->base.gallivm->builder,
1300 bld_base->base.one,
1301 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1302
Michel Dänzer51f89a02013-12-09 15:33:53 +09001303 if (d->Semantic.Index == 0 &&
1304 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001305 si_alpha_test(bld_base,
1306 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001307 break;
1308 default:
1309 target = 0;
1310 fprintf(stderr,
1311 "Warning: SI unhandled fs output type:%d\n",
1312 semantic_name);
1313 }
1314
Michel Dänzer404b29d2013-11-21 16:45:28 +09001315 si_llvm_init_export_args_load(bld_base,
1316 si_shader_ctx->radeon_bld.soa.outputs[index],
1317 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001318
1319 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001320 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001321 if (last_args[0]) {
1322 lp_build_intrinsic(base->gallivm->builder,
1323 "llvm.SI.export",
1324 LLVMVoidTypeInContext(base->gallivm->context),
1325 last_args, 9);
1326 }
1327
Marek Olšák0eb528a2013-12-04 13:24:22 +01001328 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001329 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001330
1331 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1332 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1333 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1334 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001335 si_llvm_init_export_args_load(bld_base,
1336 si_shader_ctx->radeon_bld.soa.outputs[index],
1337 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001338 lp_build_intrinsic(base->gallivm->builder,
1339 "llvm.SI.export",
1340 LLVMVoidTypeInContext(base->gallivm->context),
1341 args, 9);
1342 }
1343 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001344 } else {
1345 lp_build_intrinsic(base->gallivm->builder,
1346 "llvm.SI.export",
1347 LLVMVoidTypeInContext(base->gallivm->context),
1348 args, 9);
1349 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001350 }
1351 }
1352
Michel Dänzer1a616c12012-11-13 17:35:09 +01001353 if (depth_index >= 0 || stencil_index >= 0) {
1354 LLVMValueRef out_ptr;
1355 unsigned mask = 0;
1356
1357 /* Specify the target we are exporting */
1358 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1359
1360 if (depth_index >= 0) {
1361 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1362 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1363 mask |= 0x1;
1364
1365 if (stencil_index < 0) {
1366 args[6] =
1367 args[7] =
1368 args[8] = args[5];
1369 }
1370 }
1371
1372 if (stencil_index >= 0) {
1373 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
1374 args[7] =
1375 args[8] =
1376 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001377 /* Only setting the stencil component bit (0x2) here
1378 * breaks some stencil piglit tests
1379 */
1380 mask |= 0x3;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001381
1382 if (depth_index < 0)
1383 args[5] = args[6];
1384 }
1385
1386 /* Specify which components to enable */
1387 args[0] = lp_build_const_int32(base->gallivm, mask);
1388
1389 args[1] =
1390 args[2] =
1391 args[4] = uint->zero;
1392
1393 if (last_args[0])
1394 lp_build_intrinsic(base->gallivm->builder,
1395 "llvm.SI.export",
1396 LLVMVoidTypeInContext(base->gallivm->context),
1397 args, 9);
1398 else
1399 memcpy(last_args, args, sizeof(args));
1400 }
1401
Michel Dänzer51f89a02013-12-09 15:33:53 +09001402 if (!last_args[0]) {
1403 /* Specify which components to enable */
1404 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001405
Michel Dänzer51f89a02013-12-09 15:33:53 +09001406 /* Specify the target we are exporting */
1407 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001408
Michel Dänzer51f89a02013-12-09 15:33:53 +09001409 /* Set COMPR flag to zero to export data as 32-bit */
1410 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001411
Michel Dänzer51f89a02013-12-09 15:33:53 +09001412 /* dummy bits */
1413 last_args[5]= uint->zero;
1414 last_args[6]= uint->zero;
1415 last_args[7]= uint->zero;
1416 last_args[8]= uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001417
Michel Dänzer51f89a02013-12-09 15:33:53 +09001418 si_shader_ctx->shader->spi_shader_col_format |=
1419 V_028714_SPI_SHADER_32_ABGR;
1420 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001421 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001422
1423 /* Specify whether the EXEC mask represents the valid mask */
1424 last_args[1] = uint->one;
1425
1426 /* Specify that this is the last export */
1427 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1428
1429 lp_build_intrinsic(base->gallivm->builder,
1430 "llvm.SI.export",
1431 LLVMVoidTypeInContext(base->gallivm->context),
1432 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001433}
1434
Marek Olšák4855acd2013-08-06 15:08:54 +02001435static const struct lp_build_tgsi_action txf_action;
1436
1437static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1438 struct lp_build_tgsi_context * bld_base,
1439 struct lp_build_emit_data * emit_data);
1440
Tom Stellarda75c6162012-01-06 17:38:37 -05001441static void tex_fetch_args(
1442 struct lp_build_tgsi_context * bld_base,
1443 struct lp_build_emit_data * emit_data)
1444{
Christian König55fe5cc2013-03-04 16:30:06 +01001445 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001446 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001447 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001448 unsigned opcode = inst->Instruction.Opcode;
1449 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001450 LLVMValueRef coords[4];
1451 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001452 int ref_pos;
1453 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001454 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001455 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001456 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1457 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1458
1459 if (target == TGSI_TEXTURE_BUFFER) {
1460 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1461 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1462 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1463 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1464
1465 /* Truncate v32i8 to v16i8. */
1466 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1467 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1468 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1469 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1470
1471 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1472 emit_data->args[0] = res;
1473 emit_data->args[1] = bld_base->uint_bld.zero;
1474 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1475 emit_data->arg_count = 3;
1476 return;
1477 }
Tom Stellard467f5162012-05-16 15:15:35 -04001478
Michel Dänzer120efee2013-01-25 12:10:11 +01001479 /* Fetch and project texture coordinates */
1480 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001481 for (chan = 0; chan < 3; chan++ ) {
1482 coords[chan] = lp_build_emit_fetch(bld_base,
1483 emit_data->inst, 0,
1484 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001485 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001486 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1487 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001488 coords[chan],
1489 coords[3]);
1490 }
1491
Michel Dänzer120efee2013-01-25 12:10:11 +01001492 if (opcode == TGSI_OPCODE_TXP)
1493 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001494
Michel Dänzer120efee2013-01-25 12:10:11 +01001495 /* Pack LOD bias value */
1496 if (opcode == TGSI_OPCODE_TXB)
1497 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001498
Michel Dänzer0495adb2013-05-06 12:45:14 +02001499 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001500 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001501
1502 /* Pack depth comparison value */
1503 switch (target) {
1504 case TGSI_TEXTURE_SHADOW1D:
1505 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1506 case TGSI_TEXTURE_SHADOW2D:
1507 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001508 case TGSI_TEXTURE_SHADOWCUBE:
1509 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001510 assert(ref_pos >= 0);
1511 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001512 break;
1513 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1514 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001515 }
1516
Michel Dänzera6b83c02013-02-21 16:10:55 +01001517 /* Pack user derivatives */
1518 if (opcode == TGSI_OPCODE_TXD) {
1519 for (chan = 0; chan < 2; chan++) {
1520 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1521 if (num_coords > 1)
1522 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1523 }
1524 }
1525
Michel Dänzer120efee2013-01-25 12:10:11 +01001526 /* Pack texture coordinates */
1527 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001528 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001529 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001530 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001531 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001532
Marek Olšákd2bd6342013-09-18 15:40:21 +02001533 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001534 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001535 address[count++] = coords[3];
1536
1537 if (count > 16) {
1538 assert(!"Cannot handle more than 16 texture address parameters");
1539 count = 16;
1540 }
1541
1542 for (chan = 0; chan < count; chan++ ) {
1543 address[chan] = LLVMBuildBitCast(gallivm->builder,
1544 address[chan],
1545 LLVMInt32TypeInContext(gallivm->context),
1546 "");
1547 }
1548
Marek Olšák4855acd2013-08-06 15:08:54 +02001549 /* Adjust the sample index according to FMASK.
1550 *
1551 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1552 * which is the identity mapping. Each nibble says which physical sample
1553 * should be fetched to get that sample.
1554 *
1555 * For example, 0x11111100 means there are only 2 samples stored and
1556 * the second sample covers 3/4 of the pixel. When reading samples 0
1557 * and 1, return physical sample 0 (determined by the first two 0s
1558 * in FMASK), otherwise return physical sample 1.
1559 *
1560 * The sample index should be adjusted as follows:
1561 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1562 */
1563 if (target == TGSI_TEXTURE_2D_MSAA ||
1564 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1565 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1566 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001567 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001568 unsigned txf_count = count;
1569
Marek Olšákd2bd6342013-09-18 15:40:21 +02001570 memcpy(txf_address, address, sizeof(txf_address));
1571
1572 if (target == TGSI_TEXTURE_2D_MSAA) {
1573 txf_address[2] = bld_base->uint_bld.zero;
1574 }
1575 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001576
1577 /* Pad to a power-of-two size. */
1578 while (txf_count < util_next_power_of_two(txf_count))
1579 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1580
1581 /* Read FMASK using TXF. */
1582 txf_emit_data.chan = 0;
1583 txf_emit_data.dst_type = LLVMVectorType(
1584 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1585 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1586 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
Marek Olšákd2bd6342013-09-18 15:40:21 +02001587 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1588 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
Marek Olšák4855acd2013-08-06 15:08:54 +02001589 txf_emit_data.arg_count = 3;
1590
1591 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1592
1593 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001594 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1595 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1596
1597 /* Apply the formula. */
1598 LLVMValueRef fmask =
1599 LLVMBuildExtractElement(gallivm->builder,
1600 txf_emit_data.output[0],
1601 uint_bld->zero, "");
1602
Marek Olšákd2bd6342013-09-18 15:40:21 +02001603 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001604
1605 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001606 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001607
1608 LLVMValueRef shifted_fmask =
1609 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1610
1611 LLVMValueRef final_sample =
1612 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1613
1614 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1615 * resource descriptor is 0 (invalid),
1616 */
1617 LLVMValueRef fmask_desc =
1618 LLVMBuildBitCast(gallivm->builder,
1619 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1620 LLVMVectorType(uint_bld->elem_type, 8), "");
1621
1622 LLVMValueRef fmask_word1 =
1623 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1624 uint_bld->one, "");
1625
1626 LLVMValueRef word1_is_nonzero =
1627 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1628 fmask_word1, uint_bld->zero, "");
1629
Marek Olšákd2bd6342013-09-18 15:40:21 +02001630 /* Replace the MSAA sample index. */
1631 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001632 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001633 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001634 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001635
Michel Dänzer36231112013-05-02 09:44:45 +02001636 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001637 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001638
1639 if (opcode == TGSI_OPCODE_TXF) {
1640 /* add tex offsets */
1641 if (inst->Texture.NumOffsets) {
1642 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1643 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1644 const struct tgsi_texture_offset * off = inst->TexOffsets;
1645
1646 assert(inst->Texture.NumOffsets == 1);
1647
Marek Olšákdefedc02013-09-18 15:36:38 +02001648 switch (target) {
1649 case TGSI_TEXTURE_3D:
1650 address[2] = lp_build_add(uint_bld, address[2],
1651 bld->immediates[off->Index][off->SwizzleZ]);
1652 /* fall through */
1653 case TGSI_TEXTURE_2D:
1654 case TGSI_TEXTURE_SHADOW2D:
1655 case TGSI_TEXTURE_RECT:
1656 case TGSI_TEXTURE_SHADOWRECT:
1657 case TGSI_TEXTURE_2D_ARRAY:
1658 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001659 address[1] =
1660 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001661 bld->immediates[off->Index][off->SwizzleY]);
1662 /* fall through */
1663 case TGSI_TEXTURE_1D:
1664 case TGSI_TEXTURE_SHADOW1D:
1665 case TGSI_TEXTURE_1D_ARRAY:
1666 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1667 address[0] =
1668 lp_build_add(uint_bld, address[0],
1669 bld->immediates[off->Index][off->SwizzleX]);
1670 break;
1671 /* texture offsets do not apply to other texture targets */
1672 }
Michel Dänzer36231112013-05-02 09:44:45 +02001673 }
1674
1675 emit_data->dst_type = LLVMVectorType(
1676 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1677 4);
1678
1679 emit_data->arg_count = 3;
1680 } else {
1681 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001682 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001683
1684 emit_data->dst_type = LLVMVectorType(
1685 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1686 4);
1687
1688 emit_data->arg_count = 4;
1689 }
1690
1691 /* Dimensions */
1692 emit_data->args[emit_data->arg_count - 1] =
1693 lp_build_const_int32(bld_base->base.gallivm, target);
1694
Michel Dänzer120efee2013-01-25 12:10:11 +01001695 /* Pad to power of two vector */
1696 while (count < util_next_power_of_two(count))
1697 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1698
Christian Königccf3e8f2013-03-26 15:09:27 +01001699 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001700}
1701
Michel Dänzer07eddc42013-02-06 15:43:10 +01001702static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1703 struct lp_build_tgsi_context * bld_base,
1704 struct lp_build_emit_data * emit_data)
1705{
1706 struct lp_build_context * base = &bld_base->base;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001707 char intr_name[127];
Michel Dänzer07eddc42013-02-06 15:43:10 +01001708
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001709 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1710 emit_data->output[emit_data->chan] = build_intrinsic(
1711 base->gallivm->builder,
1712 "llvm.SI.vs.load.input", emit_data->dst_type,
1713 emit_data->args, emit_data->arg_count,
1714 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1715 return;
1716 }
1717
Michel Dänzer07eddc42013-02-06 15:43:10 +01001718 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001719 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001720
Christian König44e32242013-03-20 12:10:35 +01001721 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001722 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001723 emit_data->args, emit_data->arg_count,
1724 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001725}
1726
Michel Dänzer0495adb2013-05-06 12:45:14 +02001727static void txq_fetch_args(
1728 struct lp_build_tgsi_context * bld_base,
1729 struct lp_build_emit_data * emit_data)
1730{
1731 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1732 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001733 struct gallivm_state *gallivm = bld_base->base.gallivm;
1734
1735 if (inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1736 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1737 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1738
1739 /* Read the size from the buffer descriptor directly. */
1740 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1741 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1742 size = LLVMBuildExtractElement(gallivm->builder, size,
1743 lp_build_const_int32(gallivm, 2), "");
1744 emit_data->args[0] = size;
1745 return;
1746 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02001747
1748 /* Mip level */
1749 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1750
1751 /* Resource */
1752 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1753
1754 /* Dimensions */
1755 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1756 inst->Texture.Texture);
1757
1758 emit_data->arg_count = 3;
1759
1760 emit_data->dst_type = LLVMVectorType(
1761 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1762 4);
1763}
1764
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001765static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
1766 struct lp_build_tgsi_context * bld_base,
1767 struct lp_build_emit_data * emit_data)
1768{
1769 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1770 /* Just return the buffer size. */
1771 emit_data->output[emit_data->chan] = emit_data->args[0];
1772 return;
1773 }
1774
1775 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
1776}
1777
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001778#if HAVE_LLVM >= 0x0304
1779
1780static void si_llvm_emit_ddxy(
1781 const struct lp_build_tgsi_action * action,
1782 struct lp_build_tgsi_context * bld_base,
1783 struct lp_build_emit_data * emit_data)
1784{
1785 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1786 struct gallivm_state *gallivm = bld_base->base.gallivm;
1787 struct lp_build_context * base = &bld_base->base;
1788 const struct tgsi_full_instruction *inst = emit_data->inst;
1789 unsigned opcode = inst->Instruction.Opcode;
1790 LLVMValueRef indices[2];
1791 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1792 LLVMValueRef tl, trbl, result[4];
1793 LLVMTypeRef i32;
1794 unsigned swizzle[4];
1795 unsigned c;
1796
1797 i32 = LLVMInt32TypeInContext(gallivm->context);
1798
1799 indices[0] = bld_base->uint_bld.zero;
1800 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1801 NULL, 0, LLVMReadNoneAttribute);
1802 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1803 indices, 2, "");
1804
1805 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1806 lp_build_const_int32(gallivm, 0xfffffffc), "");
1807 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1808 indices, 2, "");
1809
1810 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1811 lp_build_const_int32(gallivm,
1812 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1813 "");
1814 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1815 indices, 2, "");
1816
1817 for (c = 0; c < 4; ++c) {
1818 unsigned i;
1819
1820 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1821 for (i = 0; i < c; ++i) {
1822 if (swizzle[i] == swizzle[c]) {
1823 result[c] = result[i];
1824 break;
1825 }
1826 }
1827 if (i != c)
1828 continue;
1829
1830 LLVMBuildStore(gallivm->builder,
1831 LLVMBuildBitCast(gallivm->builder,
1832 lp_build_emit_fetch(bld_base, inst, 0, c),
1833 i32, ""),
1834 store_ptr);
1835
1836 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1837 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1838
1839 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1840 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1841
1842 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1843 }
1844
1845 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1846}
1847
1848#endif /* HAVE_LLVM >= 0x0304 */
1849
Michel Dänzer404b29d2013-11-21 16:45:28 +09001850/* Emit one vertex from the geometry shader */
1851static void si_llvm_emit_vertex(
1852 const struct lp_build_tgsi_action *action,
1853 struct lp_build_tgsi_context *bld_base,
1854 struct lp_build_emit_data *emit_data)
1855{
1856 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001857 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001858 struct si_shader *shader = &si_shader_ctx->shader->shader;
1859 struct gallivm_state *gallivm = bld_base->base.gallivm;
1860 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001861 LLVMValueRef gs_next_vertex;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001862 LLVMValueRef t_list_ptr;
1863 LLVMValueRef t_list;
1864 LLVMValueRef args[2];
1865 unsigned chan;
1866 int i;
1867
1868 /* Load the GSVS ring resource descriptor */
1869 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1870 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
1871 lp_build_const_int32(gallivm,
1872 NUM_PIPE_CONST_BUFFERS + 2));
1873
1874 if (shader->noutput == 0) {
1875 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1876
1877 while (!tgsi_parse_end_of_tokens(parse)) {
1878 tgsi_parse_token(parse);
1879
1880 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION)
1881 si_store_shader_io_attribs(shader,
1882 &parse->FullToken.FullDeclaration);
1883 }
1884 }
1885
1886 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001887 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001888 for (i = 0; i < shader->noutput; i++) {
1889 LLVMValueRef *out_ptr =
1890 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
1891
1892 for (chan = 0; chan < 4; chan++) {
1893 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001894 LLVMValueRef soffset =
1895 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1896 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001897 LLVMValueRef voffset =
1898 lp_build_const_int32(gallivm, (i * 4 + chan) *
1899 shader->gs_max_out_vertices);
1900
1901 voffset = lp_build_add(uint, voffset, gs_next_vertex);
1902 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001903
1904 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1905
1906 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
1907 voffset, soffset, 0,
1908 V_008F0C_BUF_DATA_FORMAT_32,
1909 V_008F0C_BUF_NUM_FORMAT_UINT,
1910 1, 0, 1, 1, 0);
1911 }
1912 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001913 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
1914 lp_build_const_int32(gallivm, 1));
1915 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001916
1917 /* Signal vertex emission */
1918 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
1919 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1920 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1921 LLVMVoidTypeInContext(gallivm->context), args, 2,
1922 LLVMNoUnwindAttribute);
1923}
1924
1925/* Cut one primitive from the geometry shader */
1926static void si_llvm_emit_primitive(
1927 const struct lp_build_tgsi_action *action,
1928 struct lp_build_tgsi_context *bld_base,
1929 struct lp_build_emit_data *emit_data)
1930{
1931 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1932 struct gallivm_state *gallivm = bld_base->base.gallivm;
1933 LLVMValueRef args[2];
1934
1935 /* Signal primitive cut */
1936 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
1937 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1938 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1939 LLVMVoidTypeInContext(gallivm->context), args, 2,
1940 LLVMNoUnwindAttribute);
1941}
1942
Tom Stellarda75c6162012-01-06 17:38:37 -05001943static const struct lp_build_tgsi_action tex_action = {
1944 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001945 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001946 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001947};
1948
Michel Dänzer3e205132012-11-06 17:39:01 +01001949static const struct lp_build_tgsi_action txb_action = {
1950 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001951 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001952 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001953};
1954
Michel Dänzera6b83c02013-02-21 16:10:55 +01001955#if HAVE_LLVM >= 0x0304
1956static const struct lp_build_tgsi_action txd_action = {
1957 .fetch_args = tex_fetch_args,
1958 .emit = build_tex_intrinsic,
1959 .intr_name = "llvm.SI.sampled."
1960};
1961#endif
1962
Michel Dänzer36231112013-05-02 09:44:45 +02001963static const struct lp_build_tgsi_action txf_action = {
1964 .fetch_args = tex_fetch_args,
1965 .emit = build_tex_intrinsic,
1966 .intr_name = "llvm.SI.imageload."
1967};
1968
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001969static const struct lp_build_tgsi_action txl_action = {
1970 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001971 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001972 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001973};
1974
Michel Dänzer0495adb2013-05-06 12:45:14 +02001975static const struct lp_build_tgsi_action txq_action = {
1976 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001977 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02001978 .intr_name = "llvm.SI.resinfo"
1979};
1980
Christian König206f0592013-03-20 14:37:21 +01001981static void create_meta_data(struct si_shader_context *si_shader_ctx)
1982{
1983 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1984 LLVMValueRef args[3];
1985
1986 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1987 args[1] = 0;
1988 args[2] = lp_build_const_int32(gallivm, 1);
1989
1990 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1991}
1992
Christian König55fe5cc2013-03-04 16:30:06 +01001993static void create_function(struct si_shader_context *si_shader_ctx)
1994{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001995 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1996 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001997 struct si_pipe_shader *shader = si_shader_ctx->shader;
Vadim Girlin453ea2d2013-10-13 19:53:54 +04001998 LLVMTypeRef params[21], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001999 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002000
Christian König55fe5cc2013-03-04 16:30:06 +01002001 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002002 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002003 f32 = LLVMFloatTypeInContext(gallivm->context);
2004 v2i32 = LLVMVectorType(i32, 2);
2005 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01002006
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002007 params[SI_PARAM_CONST] = LLVMPointerType(
2008 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
2009 /* We assume at most 16 textures per program at the moment.
2010 * This need probably need to be changed to support bindless textures */
2011 params[SI_PARAM_SAMPLER] = LLVMPointerType(
2012 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
2013 params[SI_PARAM_RESOURCE] = LLVMPointerType(
2014 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002015
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002016 switch (si_shader_ctx->type) {
2017 case TGSI_PROCESSOR_VERTEX:
2018 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Marek Olšák2993cca2013-08-18 02:34:23 +02002019 params[SI_PARAM_SO_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01002020 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002021 num_params = SI_PARAM_START_INSTANCE+1;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002022 if (shader->key.vs.as_es) {
2023 params[SI_PARAM_ES2GS_OFFSET] = i32;
2024 num_params++;
2025 } else {
2026 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002027
Michel Dänzer404b29d2013-11-21 16:45:28 +09002028 /* Streamout SGPRs. */
2029 if (shader->selector->so.num_outputs) {
2030 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2031 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2032 }
2033 /* A streamout buffer offset is loaded if the stride is non-zero. */
2034 for (i = 0; i < 4; i++) {
2035 if (!shader->selector->so.stride[i])
2036 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002037
Michel Dänzer404b29d2013-11-21 16:45:28 +09002038 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2039 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002040 }
2041
2042 last_sgpr = num_params-1;
2043
2044 /* VGPRs */
2045 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2046 params[num_params++] = i32; /* unused*/
2047 params[num_params++] = i32; /* unused */
2048 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002049 break;
Christian König0666ffd2013-03-05 15:07:39 +01002050
Michel Dänzer404b29d2013-11-21 16:45:28 +09002051 case TGSI_PROCESSOR_GEOMETRY:
2052 params[SI_PARAM_GS2VS_OFFSET] = i32;
2053 params[SI_PARAM_GS_WAVE_ID] = i32;
2054 last_sgpr = SI_PARAM_GS_WAVE_ID;
2055
2056 /* VGPRs */
2057 params[SI_PARAM_VTX0_OFFSET] = i32;
2058 params[SI_PARAM_VTX1_OFFSET] = i32;
2059 params[SI_PARAM_PRIMITIVE_ID] = i32;
2060 params[SI_PARAM_VTX2_OFFSET] = i32;
2061 params[SI_PARAM_VTX3_OFFSET] = i32;
2062 params[SI_PARAM_VTX4_OFFSET] = i32;
2063 params[SI_PARAM_VTX5_OFFSET] = i32;
2064 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2065 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2066 break;
2067
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002068 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002069 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002070 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002071 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002072 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2073 params[SI_PARAM_PERSP_CENTER] = v2i32;
2074 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2075 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2076 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2077 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2078 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2079 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2080 params[SI_PARAM_POS_X_FLOAT] = f32;
2081 params[SI_PARAM_POS_Y_FLOAT] = f32;
2082 params[SI_PARAM_POS_Z_FLOAT] = f32;
2083 params[SI_PARAM_POS_W_FLOAT] = f32;
2084 params[SI_PARAM_FRONT_FACE] = f32;
2085 params[SI_PARAM_ANCILLARY] = f32;
2086 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2087 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002088 num_params = SI_PARAM_POS_FIXED_PT+1;
2089 break;
2090
2091 default:
2092 assert(0 && "unimplemented shader");
2093 return;
Christian Königc4973212013-03-05 12:14:02 +01002094 }
Christian König55fe5cc2013-03-04 16:30:06 +01002095
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002096 assert(num_params <= Elements(params));
2097 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002098 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002099
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002100 for (i = 0; i <= last_sgpr; ++i) {
2101 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002102 switch (i) {
2103 default:
2104 LLVMAddAttribute(P, LLVMInRegAttribute);
2105 break;
2106#if HAVE_LLVM >= 0x0304
2107 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2108 * to move load. Inputs are constant so this is fine. */
2109 case SI_PARAM_CONST:
2110 case SI_PARAM_SAMPLER:
2111 case SI_PARAM_RESOURCE:
2112 LLVMAddAttribute(P, LLVMByValAttribute);
2113 break;
2114#endif
2115 }
Christian Königcf9b31f2013-03-21 18:30:23 +01002116 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002117
2118#if HAVE_LLVM >= 0x0304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002119 if (bld_base->info &&
2120 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2121 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002122 si_shader_ctx->ddxy_lds =
2123 LLVMAddGlobalInAddressSpace(gallivm->module,
2124 LLVMArrayType(i32, 64),
2125 "ddxy_lds",
2126 LOCAL_ADDR_SPACE);
2127#endif
Christian König55fe5cc2013-03-04 16:30:06 +01002128}
Tom Stellarda75c6162012-01-06 17:38:37 -05002129
Christian König0f6cf2b2013-03-15 15:53:25 +01002130static void preload_constants(struct si_shader_context *si_shader_ctx)
2131{
2132 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2133 struct gallivm_state * gallivm = bld_base->base.gallivm;
2134 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002135 unsigned buf;
2136 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002137
Marek Olšák2fd42002013-10-25 11:45:47 +02002138 for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) {
2139 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002140
Marek Olšák2fd42002013-10-25 11:45:47 +02002141 if (num_const == 0)
2142 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002143
Marek Olšák2fd42002013-10-25 11:45:47 +02002144 /* Allocate space for the constant values */
2145 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002146
Marek Olšák2fd42002013-10-25 11:45:47 +02002147 /* Load the resource descriptor */
2148 si_shader_ctx->const_resource[buf] =
2149 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002150
Marek Olšák2fd42002013-10-25 11:45:47 +02002151 /* Load the constants, we rely on the code sinking to do the rest */
2152 for (i = 0; i < num_const * 4; ++i) {
2153 LLVMValueRef args[2] = {
2154 si_shader_ctx->const_resource[buf],
2155 lp_build_const_int32(gallivm, i * 4)
2156 };
2157 si_shader_ctx->constants[buf][i] =
2158 build_intrinsic(gallivm->builder, "llvm.SI.load.const",
2159 bld_base->base.elem_type, args, 2,
2160 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2161 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002162 }
2163}
2164
Christian König1c100182013-03-17 16:02:42 +01002165static void preload_samplers(struct si_shader_context *si_shader_ctx)
2166{
2167 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2168 struct gallivm_state * gallivm = bld_base->base.gallivm;
2169 const struct tgsi_shader_info * info = bld_base->info;
2170
2171 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2172
2173 LLVMValueRef res_ptr, samp_ptr;
2174 LLVMValueRef offset;
2175
2176 if (num_samplers == 0)
2177 return;
2178
2179 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02002180 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002181 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2182
2183 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2184 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2185
2186 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2187 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002188 /* Resource */
2189 offset = lp_build_const_int32(gallivm, i);
2190 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2191
2192 /* Sampler */
2193 offset = lp_build_const_int32(gallivm, i);
2194 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002195
2196 /* FMASK resource */
2197 if (info->is_msaa_sampler[i]) {
2198 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
2199 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
2200 build_indexed_load(si_shader_ctx, res_ptr, offset);
2201 }
Christian König1c100182013-03-17 16:02:42 +01002202 }
2203}
2204
Marek Olšák8d03d922013-09-01 23:59:06 +02002205static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2206{
2207 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2208 struct gallivm_state * gallivm = bld_base->base.gallivm;
2209 unsigned i;
2210
2211 if (!si_shader_ctx->shader->selector->so.num_outputs)
2212 return;
2213
2214 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2215 SI_PARAM_SO_BUFFER);
2216
2217 /* Load the resources, we rely on the code sinking to do the rest */
2218 for (i = 0; i < 4; ++i) {
2219 if (si_shader_ctx->shader->selector->so.stride[i]) {
2220 LLVMValueRef offset = lp_build_const_int32(gallivm, i);
2221
2222 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2223 }
2224 }
2225}
2226
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002227int si_compile_llvm(struct si_context *sctx, struct si_pipe_shader *shader,
Tom Stellard302f53d2012-10-25 13:50:10 -04002228 LLVMModuleRef mod)
2229{
Tom Stellard302f53d2012-10-25 13:50:10 -04002230 unsigned i;
2231 uint32_t *ptr;
Tom Stellard7782d192013-04-04 09:57:13 -07002232 struct radeon_llvm_binary binary;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002233 bool dump = r600_can_dump_shader(&sctx->screen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002234 shader->selector ? shader->selector->tokens : NULL);
Tom Stellard7782d192013-04-04 09:57:13 -07002235 memset(&binary, 0, sizeof(binary));
2236 radeon_llvm_compile(mod, &binary,
Marek Olšák65dc5882014-01-22 02:42:20 +01002237 r600_get_llvm_processor_name(sctx->screen->b.family), dump);
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002238 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002239 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002240 for (i = 0; i < binary.code_size; i+=4 ) {
2241 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2242 binary.code[i + 2], binary.code[i + 1],
2243 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002244 }
2245 }
2246
Tom Stellardd50343d2013-04-04 16:21:06 -04002247 /* XXX: We may be able to emit some of these values directly rather than
2248 * extracting fields to be emitted later.
2249 */
2250 for (i = 0; i < binary.config_size; i+= 8) {
2251 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2252 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2253 switch (reg) {
2254 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2255 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2256 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2257 case R_00B848_COMPUTE_PGM_RSRC1:
2258 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2259 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2260 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002261 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2262 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2263 break;
2264 case R_00B84C_COMPUTE_PGM_RSRC2:
2265 shader->lds_size = G_00B84C_LDS_SIZE(value);
2266 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002267 case R_0286CC_SPI_PS_INPUT_ENA:
2268 shader->spi_ps_input_ena = value;
2269 break;
2270 default:
2271 fprintf(stderr, "Warning: Compiler emitted unknown "
2272 "config register: 0x%x\n", reg);
2273 break;
2274 }
2275 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002276
2277 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02002278 r600_resource_reference(&shader->bo, NULL);
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002279 shader->bo = si_resource_create_custom(sctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04002280 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002281 if (shader->bo == NULL) {
2282 return -ENOMEM;
2283 }
2284
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002285 ptr = (uint32_t*)sctx->b.ws->buffer_map(shader->bo->cs_buf, sctx->b.rings.gfx.cs, PIPE_TRANSFER_WRITE);
Andreas Hartmetz0b57fc12014-01-11 15:56:47 +01002286 if (0 /*SI_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04002287 for (i = 0; i < binary.code_size / 4; ++i) {
2288 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04002289 }
2290 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04002291 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002292 }
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002293 sctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002294
Tom Stellard7782d192013-04-04 09:57:13 -07002295 free(binary.code);
2296 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04002297
2298 return 0;
2299}
2300
Michel Dänzer404b29d2013-11-21 16:45:28 +09002301/* Generate code for the hardware VS shader stage to go with a geometry shader */
2302static int si_generate_gs_copy_shader(struct si_context *sctx,
2303 struct si_shader_context *si_shader_ctx,
2304 bool dump)
2305{
2306 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2307 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2308 struct lp_build_context *base = &bld_base->base;
2309 struct lp_build_context *uint = &bld_base->uint_bld;
2310 struct si_shader *gs = &si_shader_ctx->shader->selector->current->shader;
2311 struct si_shader_output_values *outputs;
2312 LLVMValueRef t_list_ptr, t_list;
2313 LLVMValueRef args[9];
2314 int i, r;
2315
2316 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2317
2318 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
2319 si_shader_ctx->gs_for_vs = gs;
2320
2321 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2322
2323 create_meta_data(si_shader_ctx);
2324 create_function(si_shader_ctx);
2325 preload_streamout_buffers(si_shader_ctx);
2326
2327 /* Load the GSVS ring resource descriptor */
2328 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
2329 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
2330 lp_build_const_int32(gallivm,
2331 NUM_PIPE_CONST_BUFFERS + 1));
2332
2333 args[0] = t_list;
2334 args[1] = lp_build_mul_imm(uint,
2335 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2336 si_shader_ctx->param_vertex_id),
2337 4);
2338 args[3] = uint->zero;
2339 args[4] = uint->one; /* OFFEN */
2340 args[5] = uint->zero; /* IDXEN */
2341 args[6] = uint->one; /* GLC */
2342 args[7] = uint->one; /* SLC */
2343 args[8] = uint->zero; /* TFE */
2344
2345 /* Fetch vertex data from GSVS ring */
2346 for (i = 0; i < gs->noutput; ++i) {
2347 struct si_shader_output *out = gs->output + i;
2348 unsigned chan;
2349
2350 outputs[i].name = out->name;
2351 outputs[i].index = out->index;
2352 outputs[i].usage = out->usage;
2353
2354 for (chan = 0; chan < 4; chan++) {
2355 args[2] = lp_build_const_int32(gallivm,
2356 (i * 4 + chan) *
2357 gs->gs_max_out_vertices * 16 * 4);
2358
2359 outputs[i].values[chan] =
2360 LLVMBuildBitCast(gallivm->builder,
2361 build_intrinsic(gallivm->builder,
2362 "llvm.SI.buffer.load.dword.i32.i32",
2363 LLVMInt32TypeInContext(gallivm->context),
2364 args, 9,
2365 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2366 base->elem_type, "");
2367 }
2368 }
2369
2370 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2371
2372 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2373
2374 if (dump)
2375 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2376
2377 r = si_compile_llvm(sctx, si_shader_ctx->shader,
2378 bld_base->base.gallivm->module);
2379
2380 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2381
2382 FREE(outputs);
2383 return r;
2384}
2385
Tom Stellarda75c6162012-01-06 17:38:37 -05002386int si_pipe_shader_create(
2387 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01002388 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002389{
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002390 struct si_context *sctx = (struct si_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002391 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002392 struct si_shader_context si_shader_ctx;
2393 struct tgsi_shader_info shader_info;
2394 struct lp_build_tgsi_context * bld_base;
2395 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002396 int r = 0;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002397 bool dump = r600_can_dump_shader(&sctx->screen->b, shader->selector->tokens);
Tom Stellarda75c6162012-01-06 17:38:37 -05002398
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002399 assert(shader->shader.noutput == 0);
2400 assert(shader->shader.ninterp == 0);
2401 assert(shader->shader.ninput == 0);
2402
Michel Dänzercfebaf92012-08-31 19:04:08 +02002403 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002404 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2405 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2406
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002407 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002408
Michel Dänzere44dfd42012-11-07 17:33:08 +01002409 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01002410 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002411 bld_base->info = &shader_info;
2412 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002413
2414 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01002415 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01002416#if HAVE_LLVM >= 0x0304
2417 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
2418#endif
Michel Dänzer36231112013-05-02 09:44:45 +02002419 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002420 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002421 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002422 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002423
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002424#if HAVE_LLVM >= 0x0304
2425 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2426 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
2427#endif
2428
Michel Dänzer404b29d2013-11-21 16:45:28 +09002429 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2430 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2431
Christian Könige4ed5872013-03-21 18:02:52 +01002432 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002433 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002434 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2435 si_shader_ctx.shader = shader;
2436 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002437
Michel Dänzer51f89a02013-12-09 15:33:53 +09002438 switch (si_shader_ctx.type) {
2439 case TGSI_PROCESSOR_VERTEX:
2440 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002441 if (shader->key.vs.as_es) {
2442 si_shader_ctx.gs_for_vs = &sctx->gs_shader->current->shader;
2443 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2444 } else {
2445 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2446 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002447 break;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002448 case TGSI_PROCESSOR_GEOMETRY: {
2449 int i;
2450
2451 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2452 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2453 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
2454
2455 for (i = 0; i < shader_info.num_properties; i++) {
2456 switch (shader_info.properties[i].name) {
2457 case TGSI_PROPERTY_GS_INPUT_PRIM:
2458 shader->shader.gs_input_prim = shader_info.properties[i].data[0];
2459 break;
2460 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
2461 shader->shader.gs_output_prim = shader_info.properties[i].data[0];
2462 break;
2463 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
2464 shader->shader.gs_max_out_vertices = shader_info.properties[i].data[0];
2465 break;
2466 }
2467 }
2468 break;
2469 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002470 case TGSI_PROCESSOR_FRAGMENT:
2471 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2472 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
2473 break;
2474 default:
2475 assert(!"Unsupported shader type");
2476 return -1;
2477 }
2478
Christian König206f0592013-03-20 14:37:21 +01002479 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002480 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002481 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002482 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002483 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002484
Tom Stellard185fc9a2012-07-12 10:40:47 -04002485 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2486 * conversion fails. */
2487 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002488 tgsi_dump(sel->tokens, 0);
Marek Olšák8d03d922013-09-01 23:59:06 +02002489 si_dump_streamout(&sel->so);
Tom Stellard185fc9a2012-07-12 10:40:47 -04002490 }
2491
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002492 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2493 si_shader_ctx.gs_next_vertex =
2494 lp_build_alloca(bld_base->base.gallivm,
2495 bld_base->uint_bld.elem_type, "");
2496 }
2497
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002498 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002499 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002500 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002501 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002502
2503 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2504
2505 mod = bld_base->base.gallivm->module;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002506 r = si_compile_llvm(sctx, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002507 if (r) {
2508 fprintf(stderr, "LLVM failed to compile shader\n");
2509 goto out;
2510 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002511
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002512 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002513
2514 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2515 shader->gs_copy_shader = CALLOC_STRUCT(si_pipe_shader);
2516 shader->gs_copy_shader->selector = shader->selector;
2517 si_shader_ctx.shader = shader->gs_copy_shader;
2518 if ((r = si_generate_gs_copy_shader(sctx, &si_shader_ctx, dump))) {
2519 free(shader->gs_copy_shader);
2520 shader->gs_copy_shader = NULL;
2521 goto out;
2522 }
2523 }
2524
Tom Stellarda75c6162012-01-06 17:38:37 -05002525 tgsi_parse_free(&si_shader_ctx.parse);
2526
Michel Dänzer404b29d2013-11-21 16:45:28 +09002527out:
Marek Olšák2fd42002013-10-25 11:45:47 +02002528 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2529 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002530 FREE(si_shader_ctx.resources);
2531 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002532
Tom Stellard302f53d2012-10-25 13:50:10 -04002533 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002534}
2535
2536void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
2537{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002538 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002539}