blob: 7718e919555abafbf67d79a770ac64d814805201 [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
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900161static int si_store_shader_io_attribs(struct si_shader *shader,
162 const struct tgsi_full_declaration *d)
163{
164 int i = -1;
165
166 switch (d->Declaration.File) {
167 case TGSI_FILE_INPUT:
168 i = shader->ninput++;
169 assert(i < Elements(shader->input));
170 shader->input[i].name = d->Semantic.Name;
171 shader->input[i].sid = d->Semantic.Index;
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900172 shader->input[i].index = d->Range.First;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900173 shader->input[i].interpolate = d->Interp.Interpolate;
174 shader->input[i].centroid = d->Interp.Centroid;
175 return -1;
176
177 case TGSI_FILE_OUTPUT:
178 i = shader->noutput++;
179 assert(i < Elements(shader->output));
180 shader->output[i].name = d->Semantic.Name;
181 shader->output[i].sid = d->Semantic.Index;
182 shader->output[i].index = d->Range.First;
183 shader->output[i].usage = d->Declaration.UsageMask;
184 break;
185 }
186
187 return i;
188}
189
Tom Stellarda75c6162012-01-06 17:38:37 -0500190static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900191 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500192 unsigned input_index,
193 const struct tgsi_full_declaration *decl)
194{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900195 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
196 struct gallivm_state *gallivm = base->gallivm;
197 struct si_shader_context *si_shader_ctx =
198 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100199 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
200
201 unsigned chan;
202
Tom Stellarda75c6162012-01-06 17:38:37 -0500203 LLVMValueRef t_list_ptr;
204 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400205 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500206 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100207 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400208 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500209 LLVMTypeRef vec4_type;
210 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500211
Tom Stellard467f5162012-05-16 15:15:35 -0400212 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100213 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500214
Michel Dänzer51f89a02013-12-09 15:33:53 +0900215 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400216
Christian König206f0592013-03-20 14:37:21 +0100217 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400218
219 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900220 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500221
Christian Königa0dca442013-03-22 15:59:22 +0100222 if (divisor) {
223 /* Build index from instance ID, start instance and divisor */
224 si_shader_ctx->shader->shader.uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200225 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100226 } else {
227 /* Load the buffer index, which is always stored in VGPR0
228 * for Vertex Shaders */
Marek Olšák8d03d922013-09-01 23:59:06 +0200229 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
230 si_shader_ctx->param_vertex_id);
Christian Königa0dca442013-03-22 15:59:22 +0100231 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500232
233 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400234 args[0] = t_list;
235 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100236 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900237 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100238 "llvm.SI.vs.load.input", vec4_type, args, 3,
239 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500240
241 /* Break up the vec4 into individual components */
242 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900243 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500244 /* XXX: Use a helper function for this. There is one in
245 * tgsi_llvm.c. */
246 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900247 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500248 input, llvm_chan, "");
249 }
250}
251
Michel Dänzer404b29d2013-11-21 16:45:28 +0900252static void declare_input_gs(
253 struct radeon_llvm_context *radeon_bld,
254 unsigned input_index,
255 const struct tgsi_full_declaration *decl)
256{
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900257 struct si_shader_context *si_shader_ctx =
258 si_shader_context(&radeon_bld->soa.bld_base);
259 struct si_shader *shader = &si_shader_ctx->shader->shader;
260
261 si_store_shader_io_attribs(shader, decl);
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900262
263 if (decl->Semantic.Name != TGSI_SEMANTIC_PRIMID)
264 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900265}
266
267static LLVMValueRef fetch_input_gs(
268 struct lp_build_tgsi_context *bld_base,
269 const struct tgsi_full_src_register *reg,
270 enum tgsi_opcode_type type,
271 unsigned swizzle)
272{
273 struct lp_build_context *base = &bld_base->base;
274 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900275 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900276 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
277 struct gallivm_state *gallivm = base->gallivm;
278 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
279 LLVMValueRef vtx_offset;
280 LLVMValueRef t_list_ptr;
281 LLVMValueRef t_list;
282 LLVMValueRef args[9];
283 unsigned vtx_offset_param;
284
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900285 if (swizzle != ~0 &&
286 shader->input[reg->Register.Index].name == TGSI_SEMANTIC_PRIMID) {
287 if (swizzle == 0)
288 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
289 SI_PARAM_PRIMITIVE_ID);
290 else
291 return uint->zero;
292 }
293
Michel Dänzer404b29d2013-11-21 16:45:28 +0900294 if (!reg->Register.Dimension)
295 return NULL;
296
297 if (swizzle == ~0) {
298 LLVMValueRef values[TGSI_NUM_CHANNELS];
299 unsigned chan;
300 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
301 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
302 }
303 return lp_build_gather_values(bld_base->base.gallivm, values,
304 TGSI_NUM_CHANNELS);
305 }
306
307 /* Get the vertex offset parameter */
308 vtx_offset_param = reg->Dimension.Index;
309 if (vtx_offset_param < 2) {
310 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
311 } else {
312 assert(vtx_offset_param < 6);
313 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
314 }
315 vtx_offset = lp_build_mul_imm(uint,
316 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
317 vtx_offset_param),
318 4);
319
320 /* Load the ESGS ring resource descriptor */
321 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
322 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
323 lp_build_const_int32(gallivm,
324 NUM_PIPE_CONST_BUFFERS + 1));
325
326 args[0] = t_list;
327 args[1] = vtx_offset;
328 args[2] = lp_build_const_int32(gallivm,
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900329 ((shader->input[reg->Register.Index].param_offset * 4) +
330 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900331 args[3] = uint->zero;
332 args[4] = uint->one; /* OFFEN */
333 args[5] = uint->zero; /* IDXEN */
334 args[6] = uint->one; /* GLC */
335 args[7] = uint->zero; /* SLC */
336 args[8] = uint->zero; /* TFE */
337
338 return LLVMBuildBitCast(gallivm->builder,
339 build_intrinsic(gallivm->builder,
340 "llvm.SI.buffer.load.dword.i32.i32",
341 i32, args, 9,
342 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
343 tgsi2llvmtype(bld_base, type), "");
344}
345
Tom Stellarda75c6162012-01-06 17:38:37 -0500346static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900347 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500348 unsigned input_index,
349 const struct tgsi_full_declaration *decl)
350{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900351 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
352 struct si_shader_context *si_shader_ctx =
353 si_shader_context(&radeon_bld->soa.bld_base);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200354 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900355 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
356 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400357 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900358 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100359
360 LLVMValueRef interp_param;
361 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500362
363 /* This value is:
364 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
365 * quad begins a new primitive. Bit 0 always needs
366 * to be unset)
367 * [32:16] ParamOffset
368 *
369 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900370 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200371 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500372
Christian König0666ffd2013-03-05 15:07:39 +0100373 unsigned chan;
374
Tom Stellard0fb1e682012-09-06 16:18:11 -0400375 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
376 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400377 unsigned soa_index =
378 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900379 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100380 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100381
382 if (chan == 3)
383 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900384 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100385 LLVMBuildFDiv(gallivm->builder,
386 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900387 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100388 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400389 }
390 return;
391 }
392
Michel Dänzer97078b12012-09-25 12:41:31 +0200393 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
394 LLVMValueRef face, is_face_positive;
395
Christian König0666ffd2013-03-05 15:07:39 +0100396 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
397
Michel Dänzer97078b12012-09-25 12:41:31 +0200398 is_face_positive = LLVMBuildFCmp(gallivm->builder,
399 LLVMRealUGT, face,
400 lp_build_const_float(gallivm, 0.0f),
401 "");
402
Michel Dänzer51f89a02013-12-09 15:33:53 +0900403 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200404 LLVMBuildSelect(gallivm->builder,
405 is_face_positive,
406 lp_build_const_float(gallivm, 1.0f),
407 lp_build_const_float(gallivm, 0.0f),
408 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900409 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
410 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200411 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900412 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200413 lp_build_const_float(gallivm, 1.0f);
414
415 return;
416 }
417
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900418 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200419 attr_number = lp_build_const_int32(gallivm,
420 shader->input[input_index].param_offset);
421
Francisco Jerez12799232012-04-30 18:27:52 +0200422 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500423 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100424 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100425 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200426 } else {
427 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100428 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200429 else
Christian König0666ffd2013-03-05 15:07:39 +0100430 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200431 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500432 break;
433 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100434 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500435 break;
436 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200437 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100438 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200439 else
Christian König0666ffd2013-03-05 15:07:39 +0100440 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200441 break;
442 case TGSI_INTERPOLATE_PERSPECTIVE:
443 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100444 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200445 else
Christian König0666ffd2013-03-05 15:07:39 +0100446 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500447 break;
448 default:
449 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
450 return;
451 }
452
Christian König0666ffd2013-03-05 15:07:39 +0100453 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
454
Tom Stellarda75c6162012-01-06 17:38:37 -0500455 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200456 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100457 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100458 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200459 LLVMValueRef face, is_face_positive;
460 LLVMValueRef back_attr_number =
461 lp_build_const_int32(gallivm,
462 shader->input[input_index].param_offset + 1);
463
Christian König0666ffd2013-03-05 15:07:39 +0100464 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
465
Michel Dänzer691f08d2012-09-06 18:03:38 +0200466 is_face_positive = LLVMBuildFCmp(gallivm->builder,
467 LLVMRealUGT, face,
468 lp_build_const_float(gallivm, 0.0f),
469 "");
470
Tom Stellarda75c6162012-01-06 17:38:37 -0500471 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100472 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200473 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
474 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
475 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
476 LLVMValueRef front, back;
477
478 args[0] = llvm_chan;
479 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900480 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100481 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100482 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200483
484 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900485 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100486 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100487 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200488
Michel Dänzer51f89a02013-12-09 15:33:53 +0900489 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200490 LLVMBuildSelect(gallivm->builder,
491 is_face_positive,
492 front,
493 back,
494 "");
495 }
496
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900497 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200498 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
499 LLVMValueRef args[4];
500
501 args[0] = uint->zero;
502 args[1] = attr_number;
503 args[2] = params;
504 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900505 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
506 build_intrinsic(gallivm->builder, intr_name,
507 input_type, args, args[3] ? 4 : 3,
508 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
509 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
510 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200511 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900512 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200513 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200514 } else {
515 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100516 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200517 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
518 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
519 args[0] = llvm_chan;
520 args[1] = attr_number;
521 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100522 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900523 radeon_bld->inputs[soa_index] =
524 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100525 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100526 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200527 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500528 }
529}
530
Christian Könige4ed5872013-03-21 18:02:52 +0100531static void declare_system_value(
532 struct radeon_llvm_context * radeon_bld,
533 unsigned index,
534 const struct tgsi_full_declaration *decl)
535{
Marek Olšák8d03d922013-09-01 23:59:06 +0200536 struct si_shader_context *si_shader_ctx =
537 si_shader_context(&radeon_bld->soa.bld_base);
Christian Könige4ed5872013-03-21 18:02:52 +0100538 LLVMValueRef value = 0;
539
540 switch (decl->Semantic.Name) {
541 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200542 value = LLVMGetParam(radeon_bld->main_fn,
543 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100544 break;
545
546 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200547 value = LLVMGetParam(radeon_bld->main_fn,
548 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100549 break;
550
551 default:
552 assert(!"unknown system value");
553 return;
554 }
555
556 radeon_bld->system_values[index] = value;
557}
558
Tom Stellarda75c6162012-01-06 17:38:37 -0500559static LLVMValueRef fetch_constant(
560 struct lp_build_tgsi_context * bld_base,
561 const struct tgsi_full_src_register *reg,
562 enum tgsi_opcode_type type,
563 unsigned swizzle)
564{
Christian König55fe5cc2013-03-04 16:30:06 +0100565 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500566 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100567 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200568 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500569
Christian Königf5298b02013-02-28 14:50:07 +0100570 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100571 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100572 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500573
Christian König8514f5a2013-02-04 17:46:42 +0100574 if (swizzle == LP_CHAN_ALL) {
575 unsigned chan;
576 LLVMValueRef values[4];
577 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
578 values[chan] = fetch_constant(bld_base, reg, type, chan);
579
580 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
581 }
582
Marek Olšák2fd42002013-10-25 11:45:47 +0200583 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100584 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100585
Marek Olšák2fd42002013-10-25 11:45:47 +0200586 if (!reg->Register.Indirect)
587 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
588
589 args[0] = si_shader_ctx->const_resource[buf];
Christian König0f6cf2b2013-03-15 15:53:25 +0100590 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
591 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
592 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
593 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
594 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200595
Christian Königf5298b02013-02-28 14:50:07 +0100596 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100597 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500598
Christian Königf5298b02013-02-28 14:50:07 +0100599 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500600}
601
Michel Dänzer26c71392012-08-24 12:03:11 +0200602/* Initialize arguments for the shader export intrinsic */
603static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900604 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200605 unsigned target,
606 LLVMValueRef *args)
607{
608 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
609 struct lp_build_context *uint =
610 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
611 struct lp_build_context *base = &bld_base->base;
612 unsigned compressed = 0;
613 unsigned chan;
614
Michel Dänzerf402acd2012-08-22 18:15:36 +0200615 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
616 int cbuf = target - V_008DFC_SQ_EXP_MRT;
617
618 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100619 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100620
621 if (compressed)
622 si_shader_ctx->shader->spi_shader_col_format |=
623 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
624 else
625 si_shader_ctx->shader->spi_shader_col_format |=
626 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200627
628 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200629 }
630 }
631
632 if (compressed) {
633 /* Pixel shader needs to pack output values before export */
634 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900635 args[0] = values[2 * chan];
636 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200637 args[chan + 5] =
638 build_intrinsic(base->gallivm->builder,
639 "llvm.SI.packf16",
640 LLVMInt32TypeInContext(base->gallivm->context),
641 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100642 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100643 args[chan + 7] = args[chan + 5] =
644 LLVMBuildBitCast(base->gallivm->builder,
645 args[chan + 5],
646 LLVMFloatTypeInContext(base->gallivm->context),
647 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200648 }
649
650 /* Set COMPR flag */
651 args[4] = uint->one;
652 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900653 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200654 /* +5 because the first output value will be
655 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900656 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200657
658 /* Clear COMPR flag */
659 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200660 }
661
662 /* XXX: This controls which components of the output
663 * registers actually get exported. (e.g bit 0 means export
664 * X component, bit 1 means export Y component, etc.) I'm
665 * hard coding this to 0xf for now. In the future, we might
666 * want to do something else. */
667 args[0] = lp_build_const_int32(base->gallivm, 0xf);
668
669 /* Specify whether the EXEC mask represents the valid mask */
670 args[1] = uint->zero;
671
672 /* Specify whether this is the last export */
673 args[2] = uint->zero;
674
675 /* Specify the target we are exporting */
676 args[3] = lp_build_const_int32(base->gallivm, target);
677
Michel Dänzer26c71392012-08-24 12:03:11 +0200678 /* XXX: We probably need to keep track of the output
679 * values, so we know what we are passing to the next
680 * stage. */
681}
682
Michel Dänzer404b29d2013-11-21 16:45:28 +0900683/* Load from output pointers and initialize arguments for the shader export intrinsic */
684static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
685 LLVMValueRef *out_ptr,
686 unsigned target,
687 LLVMValueRef *args)
688{
689 struct gallivm_state *gallivm = bld_base->base.gallivm;
690 LLVMValueRef values[4];
691 int i;
692
693 for (i = 0; i < 4; i++)
694 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
695
696 si_llvm_init_export_args(bld_base, values, target, args);
697}
698
Michel Dänzer7708a862012-11-02 15:57:30 +0100699static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900700 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100701{
702 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
703 struct gallivm_state *gallivm = bld_base->base.gallivm;
704
Christian Königa0dca442013-03-22 15:59:22 +0100705 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400706 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
707 SI_PARAM_ALPHA_REF);
708
Michel Dänzer7708a862012-11-02 15:57:30 +0100709 LLVMValueRef alpha_pass =
710 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100711 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900712 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400713 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100714 LLVMValueRef arg =
715 lp_build_select(&bld_base->base,
716 alpha_pass,
717 lp_build_const_float(gallivm, 1.0f),
718 lp_build_const_float(gallivm, -1.0f));
719
720 build_intrinsic(gallivm->builder,
721 "llvm.AMDGPU.kill",
722 LLVMVoidTypeInContext(gallivm->context),
723 &arg, 1, 0);
724 } else {
725 build_intrinsic(gallivm->builder,
726 "llvm.AMDGPU.kilp",
727 LLVMVoidTypeInContext(gallivm->context),
728 NULL, 0, 0);
729 }
730}
731
Michel Dänzere3befbc2013-05-15 18:09:50 +0200732static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900733 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200734{
735 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200736 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200737 struct lp_build_context *base = &bld_base->base;
738 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200739 unsigned reg_index;
740 unsigned chan;
741 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200742 LLVMValueRef base_elt;
743 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšák2fd42002013-10-25 11:45:47 +0200744 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, NUM_PIPE_CONST_BUFFERS);
745 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200746
Michel Dänzere3befbc2013-05-15 18:09:50 +0200747 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200748 LLVMValueRef *args = pos[2 + reg_index];
749
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200750 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
751 continue;
752
753 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
754
Michel Dänzere3befbc2013-05-15 18:09:50 +0200755 args[5] =
756 args[6] =
757 args[7] =
758 args[8] = lp_build_const_float(base->gallivm, 0.0f);
759
760 /* Compute dot products of position and user clip plane vectors */
761 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
762 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
763 args[0] = const_resource;
764 args[1] = lp_build_const_int32(base->gallivm,
765 ((reg_index * 4 + chan) * 4 +
766 const_chan) * 4);
767 base_elt = build_intrinsic(base->gallivm->builder,
768 "llvm.SI.load.const",
769 base->elem_type,
770 args, 2,
771 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
772 args[5 + chan] =
773 lp_build_add(base, args[5 + chan],
774 lp_build_mul(base, base_elt,
775 out_elts[const_chan]));
776 }
777 }
778
779 args[0] = lp_build_const_int32(base->gallivm, 0xf);
780 args[1] = uint->zero;
781 args[2] = uint->zero;
782 args[3] = lp_build_const_int32(base->gallivm,
783 V_008DFC_SQ_EXP_POS + 2 + reg_index);
784 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200785 }
786}
787
Marek Olšák8d03d922013-09-01 23:59:06 +0200788static void si_dump_streamout(struct pipe_stream_output_info *so)
789{
790 unsigned i;
791
792 if (so->num_outputs)
793 fprintf(stderr, "STREAMOUT\n");
794
795 for (i = 0; i < so->num_outputs; i++) {
796 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
797 so->output[i].start_component;
798 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
799 i, so->output[i].output_buffer,
800 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
801 so->output[i].register_index,
802 mask & 1 ? "x" : "",
803 mask & 2 ? "y" : "",
804 mask & 4 ? "z" : "",
805 mask & 8 ? "w" : "");
806 }
807}
808
809/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
810 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
811 * or v4i32 (num_channels=3,4). */
812static void build_tbuffer_store(struct si_shader_context *shader,
813 LLVMValueRef rsrc,
814 LLVMValueRef vdata,
815 unsigned num_channels,
816 LLVMValueRef vaddr,
817 LLVMValueRef soffset,
818 unsigned inst_offset,
819 unsigned dfmt,
820 unsigned nfmt,
821 unsigned offen,
822 unsigned idxen,
823 unsigned glc,
824 unsigned slc,
825 unsigned tfe)
826{
827 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
828 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
829 LLVMValueRef args[] = {
830 rsrc,
831 vdata,
832 LLVMConstInt(i32, num_channels, 0),
833 vaddr,
834 soffset,
835 LLVMConstInt(i32, inst_offset, 0),
836 LLVMConstInt(i32, dfmt, 0),
837 LLVMConstInt(i32, nfmt, 0),
838 LLVMConstInt(i32, offen, 0),
839 LLVMConstInt(i32, idxen, 0),
840 LLVMConstInt(i32, glc, 0),
841 LLVMConstInt(i32, slc, 0),
842 LLVMConstInt(i32, tfe, 0)
843 };
844
845 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
846 unsigned func = CLAMP(num_channels, 1, 3) - 1;
847 const char *types[] = {"i32", "v2i32", "v4i32"};
848 char name[256];
849 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
850
851 lp_build_intrinsic(gallivm->builder, name,
852 LLVMVoidTypeInContext(gallivm->context),
853 args, Elements(args));
854}
855
856static void build_streamout_store(struct si_shader_context *shader,
857 LLVMValueRef rsrc,
858 LLVMValueRef vdata,
859 unsigned num_channels,
860 LLVMValueRef vaddr,
861 LLVMValueRef soffset,
862 unsigned inst_offset)
863{
864 static unsigned dfmt[] = {
865 V_008F0C_BUF_DATA_FORMAT_32,
866 V_008F0C_BUF_DATA_FORMAT_32_32,
867 V_008F0C_BUF_DATA_FORMAT_32_32_32,
868 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
869 };
870 assert(num_channels >= 1 && num_channels <= 4);
871
872 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
873 inst_offset, dfmt[num_channels-1],
874 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
875}
876
877/* On SI, the vertex shader is responsible for writing streamout data
878 * to buffers. */
879static void si_llvm_emit_streamout(struct si_shader_context *shader)
880{
881 struct pipe_stream_output_info *so = &shader->shader->selector->so;
882 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
883 LLVMBuilderRef builder = gallivm->builder;
884 int i, j;
885 struct lp_build_if_state if_ctx;
886
887 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
888
889 LLVMValueRef so_param =
890 LLVMGetParam(shader->radeon_bld.main_fn,
891 shader->param_streamout_config);
892
893 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
894 LLVMValueRef so_vtx_count =
895 LLVMBuildAnd(builder,
896 LLVMBuildLShr(builder, so_param,
897 LLVMConstInt(i32, 16, 0), ""),
898 LLVMConstInt(i32, 127, 0), "");
899
900 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
901 NULL, 0, LLVMReadNoneAttribute);
902
903 /* can_emit = tid < so_vtx_count; */
904 LLVMValueRef can_emit =
905 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
906
907 /* Emit the streamout code conditionally. This actually avoids
908 * out-of-bounds buffer access. The hw tells us via the SGPR
909 * (so_vtx_count) which threads are allowed to emit streamout data. */
910 lp_build_if(&if_ctx, gallivm, can_emit);
911 {
912 /* The buffer offset is computed as follows:
913 * ByteOffset = streamout_offset[buffer_id]*4 +
914 * (streamout_write_index + thread_id)*stride[buffer_id] +
915 * attrib_offset
916 */
917
918 LLVMValueRef so_write_index =
919 LLVMGetParam(shader->radeon_bld.main_fn,
920 shader->param_streamout_write_index);
921
922 /* Compute (streamout_write_index + thread_id). */
923 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
924
925 /* Compute the write offset for each enabled buffer. */
926 LLVMValueRef so_write_offset[4] = {};
927 for (i = 0; i < 4; i++) {
928 if (!so->stride[i])
929 continue;
930
931 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
932 shader->param_streamout_offset[i]);
933 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
934
935 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
936 LLVMConstInt(i32, so->stride[i]*4, 0), "");
937 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
938 }
939
940 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS] = shader->radeon_bld.soa.outputs;
941
942 /* Write streamout data. */
943 for (i = 0; i < so->num_outputs; i++) {
944 unsigned buf_idx = so->output[i].output_buffer;
945 unsigned reg = so->output[i].register_index;
946 unsigned start = so->output[i].start_component;
947 unsigned num_comps = so->output[i].num_components;
948 LLVMValueRef out[4];
949
950 assert(num_comps && num_comps <= 4);
951 if (!num_comps || num_comps > 4)
952 continue;
953
954 /* Load the output as int. */
955 for (j = 0; j < num_comps; j++) {
956 out[j] = LLVMBuildLoad(builder, outputs[reg][start+j], "");
957 out[j] = LLVMBuildBitCast(builder, out[j], i32, "");
958 }
959
960 /* Pack the output. */
961 LLVMValueRef vdata = NULL;
962
963 switch (num_comps) {
964 case 1: /* as i32 */
965 vdata = out[0];
966 break;
967 case 2: /* as v2i32 */
968 case 3: /* as v4i32 (aligned to 4) */
969 case 4: /* as v4i32 */
970 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
971 for (j = 0; j < num_comps; j++) {
972 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
973 LLVMConstInt(i32, j, 0), "");
974 }
975 break;
976 }
977
978 build_streamout_store(shader, shader->so_buffers[buf_idx],
979 vdata, num_comps,
980 so_write_offset[buf_idx],
981 LLVMConstInt(i32, 0, 0),
982 so->output[i].dst_offset*4);
983 }
984 }
985 lp_build_endif(&if_ctx);
986}
987
Michel Dänzer7435d9f2013-12-04 13:37:07 +0900988
Michel Dänzer404b29d2013-11-21 16:45:28 +0900989/* Generate export instructions for hardware VS shader stage */
990static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
991 struct si_shader_output_values *outputs,
992 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -0500993{
994 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200995 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500996 struct lp_build_context * base = &bld_base->base;
997 struct lp_build_context * uint =
998 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100999 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001000 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001001 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
1002 unsigned semantic_name, semantic_index, semantic_usage;
1003 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001004 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001005 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001006 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001007
Marek Olšák8d03d922013-09-01 23:59:06 +02001008 if (si_shader_ctx->shader->selector->so.num_outputs) {
1009 si_llvm_emit_streamout(si_shader_ctx);
1010 }
1011
Michel Dänzer404b29d2013-11-21 16:45:28 +09001012 for (i = 0; i < noutput; i++) {
1013 semantic_name = outputs[i].name;
1014 semantic_index = outputs[i].index;
1015 semantic_usage = outputs[i].usage;
Tom Stellarda75c6162012-01-06 17:38:37 -05001016
Michel Dänzer0afeea52013-05-02 14:53:17 +02001017handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001018 /* Select the correct target */
1019 switch(semantic_name) {
1020 case TGSI_SEMANTIC_PSIZE:
1021 shader->vs_out_misc_write = true;
1022 shader->vs_out_point_size = true;
1023 psize_value = outputs[i].values[0];
1024 continue;
1025 case TGSI_SEMANTIC_EDGEFLAG:
1026 shader->vs_out_misc_write = true;
1027 shader->vs_out_edgeflag = true;
1028 edgeflag_value = outputs[i].values[0];
1029 continue;
1030 case TGSI_SEMANTIC_LAYER:
1031 shader->vs_out_misc_write = true;
1032 shader->vs_out_layer = true;
1033 layer_value = outputs[i].values[0];
1034 continue;
1035 case TGSI_SEMANTIC_POSITION:
1036 target = V_008DFC_SQ_EXP_POS;
1037 break;
1038 case TGSI_SEMANTIC_COLOR:
1039 case TGSI_SEMANTIC_BCOLOR:
1040 target = V_008DFC_SQ_EXP_PARAM + param_count;
1041 shader->output[i].param_offset = param_count;
1042 param_count++;
1043 break;
1044 case TGSI_SEMANTIC_CLIPDIST:
1045 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
1046 (1 << semantic_index)))
Marek Olšák053606d2013-11-19 22:07:30 +01001047 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001048 shader->clip_dist_write |=
1049 semantic_usage << (semantic_index << 2);
1050 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1051 break;
1052 case TGSI_SEMANTIC_CLIPVERTEX:
1053 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1054 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001055 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001056 case TGSI_SEMANTIC_FOG:
1057 case TGSI_SEMANTIC_GENERIC:
1058 target = V_008DFC_SQ_EXP_PARAM + param_count;
1059 shader->output[i].param_offset = param_count;
1060 param_count++;
1061 break;
1062 default:
1063 target = 0;
1064 fprintf(stderr,
1065 "Warning: SI unhandled vs output type:%d\n",
1066 semantic_name);
1067 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001068
Michel Dänzer404b29d2013-11-21 16:45:28 +09001069 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001070
Michel Dänzer404b29d2013-11-21 16:45:28 +09001071 if (target >= V_008DFC_SQ_EXP_POS &&
1072 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1073 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1074 args, sizeof(args));
1075 } else {
1076 lp_build_intrinsic(base->gallivm->builder,
1077 "llvm.SI.export",
1078 LLVMVoidTypeInContext(base->gallivm->context),
1079 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001080 }
1081
1082 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1083 semantic_name = TGSI_SEMANTIC_GENERIC;
1084 goto handle_semantic;
1085 }
1086 }
1087
1088 /* We need to add the position output manually if it's missing. */
1089 if (!pos_args[0][0]) {
1090 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1091 pos_args[0][1] = uint->zero; /* EXEC mask */
1092 pos_args[0][2] = uint->zero; /* last export? */
1093 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1094 pos_args[0][4] = uint->zero; /* COMPR flag */
1095 pos_args[0][5] = base->zero; /* X */
1096 pos_args[0][6] = base->zero; /* Y */
1097 pos_args[0][7] = base->zero; /* Z */
1098 pos_args[0][8] = base->one; /* W */
1099 }
1100
1101 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1102 if (shader->vs_out_misc_write) {
1103 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1104 shader->vs_out_point_size |
1105 (shader->vs_out_edgeflag << 1) |
1106 (shader->vs_out_layer << 2));
1107 pos_args[1][1] = uint->zero; /* EXEC mask */
1108 pos_args[1][2] = uint->zero; /* last export? */
1109 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1110 pos_args[1][4] = uint->zero; /* COMPR flag */
1111 pos_args[1][5] = base->zero; /* X */
1112 pos_args[1][6] = base->zero; /* Y */
1113 pos_args[1][7] = base->zero; /* Z */
1114 pos_args[1][8] = base->zero; /* W */
1115
Michel Dänzer404b29d2013-11-21 16:45:28 +09001116 if (shader->vs_out_point_size)
1117 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001118
1119 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001120 /* The output is a float, but the hw expects an integer
1121 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001122 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1123 edgeflag_value,
1124 bld_base->uint_bld.elem_type, "");
1125 edgeflag_value = lp_build_min(&bld_base->int_bld,
1126 edgeflag_value,
1127 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001128
1129 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001130 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1131 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001132 base->elem_type, "");
1133 }
1134
Michel Dänzer404b29d2013-11-21 16:45:28 +09001135 if (shader->vs_out_layer)
1136 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001137 }
1138
1139 for (i = 0; i < 4; i++)
1140 if (pos_args[i][0])
1141 shader->nr_pos_exports++;
1142
1143 pos_idx = 0;
1144 for (i = 0; i < 4; i++) {
1145 if (!pos_args[i][0])
1146 continue;
1147
1148 /* Specify the target we are exporting */
1149 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1150
1151 if (pos_idx == shader->nr_pos_exports)
1152 /* Specify that this is the last export */
1153 pos_args[i][2] = uint->one;
1154
1155 lp_build_intrinsic(base->gallivm->builder,
1156 "llvm.SI.export",
1157 LLVMVoidTypeInContext(base->gallivm->context),
1158 pos_args[i], 9);
1159 }
1160}
1161
Michel Dänzer404b29d2013-11-21 16:45:28 +09001162static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1163{
1164 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1165 struct gallivm_state *gallivm = bld_base->base.gallivm;
1166 struct si_pipe_shader *shader = si_shader_ctx->shader;
1167 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1168 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1169 LLVMValueRef t_list_ptr;
1170 LLVMValueRef t_list;
1171 unsigned chan;
1172 int i;
1173
1174 while (!tgsi_parse_end_of_tokens(parse)) {
1175 struct tgsi_full_declaration *d =
1176 &parse->FullToken.FullDeclaration;
1177
1178 tgsi_parse_token(parse);
1179
1180 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1181 continue;
1182
1183 si_store_shader_io_attribs(&shader->shader, d);
1184 }
1185
1186 /* Load the ESGS ring resource descriptor */
1187 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1188 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
1189 lp_build_const_int32(gallivm,
1190 NUM_PIPE_CONST_BUFFERS + 1));
1191
1192 for (i = 0; i < shader->shader.noutput; i++) {
1193 LLVMValueRef *out_ptr =
1194 si_shader_ctx->radeon_bld.soa.outputs[shader->shader.output[i].index];
1195
1196 for (chan = 0; chan < 4; chan++) {
1197 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
1198 LLVMValueRef voffset =
1199 lp_build_const_int32(gallivm, (4 * i + chan) * 4);
1200 LLVMValueRef soffset =
1201 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1202 SI_PARAM_ES2GS_OFFSET);
1203
1204 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1205
1206 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
1207 voffset, soffset, 0,
1208 V_008F0C_BUF_DATA_FORMAT_32,
1209 V_008F0C_BUF_NUM_FORMAT_UINT,
1210 1, 0, 1, 1, 0);
1211 }
1212 }
1213}
1214
1215static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1216{
1217 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1218 struct gallivm_state *gallivm = bld_base->base.gallivm;
1219 LLVMValueRef args[2];
1220
1221 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1222 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1223 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1224 LLVMVoidTypeInContext(gallivm->context), args, 2,
1225 LLVMNoUnwindAttribute);
1226}
1227
1228static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1229{
1230 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1231 struct gallivm_state *gallivm = bld_base->base.gallivm;
1232 struct si_pipe_shader *shader = si_shader_ctx->shader;
1233 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1234 struct si_shader_output_values *outputs = NULL;
1235 unsigned noutput = 0;
1236 int i;
1237
1238 while (!tgsi_parse_end_of_tokens(parse)) {
1239 struct tgsi_full_declaration *d =
1240 &parse->FullToken.FullDeclaration;
1241 unsigned index;
1242
1243 tgsi_parse_token(parse);
1244
1245 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1246 continue;
1247
1248 i = si_store_shader_io_attribs(&shader->shader, d);
1249 if (i < 0)
1250 continue;
1251
1252 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1253 (noutput + 1) * sizeof(outputs[0]));
1254 for (index = d->Range.First; index <= d->Range.Last; index++) {
1255 outputs[noutput].name = d->Semantic.Name;
1256 outputs[noutput].index = d->Semantic.Index;
1257 outputs[noutput].usage = d->Declaration.UsageMask;
1258
1259 for (i = 0; i < 4; i++)
1260 outputs[noutput].values[i] =
1261 LLVMBuildLoad(gallivm->builder,
1262 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1263 "");
1264 }
1265 noutput++;
1266 }
1267
1268 si_llvm_export_vs(bld_base, outputs, noutput);
1269 FREE(outputs);
1270}
1271
Michel Dänzer51f89a02013-12-09 15:33:53 +09001272static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1273{
1274 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
1275 struct si_shader * shader = &si_shader_ctx->shader->shader;
1276 struct lp_build_context * base = &bld_base->base;
1277 struct lp_build_context * uint =
1278 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1279 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1280 LLVMValueRef args[9];
1281 LLVMValueRef last_args[9] = { 0 };
1282 unsigned semantic_name;
1283 int depth_index = -1, stencil_index = -1;
1284 int i;
1285
1286 while (!tgsi_parse_end_of_tokens(parse)) {
1287 struct tgsi_full_declaration *d =
1288 &parse->FullToken.FullDeclaration;
1289 unsigned target;
1290 unsigned index;
1291
1292 tgsi_parse_token(parse);
1293
1294 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
1295 parse->FullToken.FullProperty.Property.PropertyName ==
1296 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
1297 shader->fs_write_all = TRUE;
1298
1299 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1300 continue;
1301
1302 i = si_store_shader_io_attribs(shader, d);
1303 if (i < 0)
1304 continue;
1305
1306 semantic_name = d->Semantic.Name;
1307 for (index = d->Range.First; index <= d->Range.Last; index++) {
1308 /* Select the correct target */
1309 switch(semantic_name) {
1310 case TGSI_SEMANTIC_POSITION:
1311 depth_index = index;
1312 continue;
1313 case TGSI_SEMANTIC_STENCIL:
1314 stencil_index = index;
1315 continue;
1316 case TGSI_SEMANTIC_COLOR:
1317 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1318 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001319 LLVMBuildStore(bld_base->base.gallivm->builder,
1320 bld_base->base.one,
1321 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1322
Michel Dänzer51f89a02013-12-09 15:33:53 +09001323 if (d->Semantic.Index == 0 &&
1324 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001325 si_alpha_test(bld_base,
1326 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001327 break;
1328 default:
1329 target = 0;
1330 fprintf(stderr,
1331 "Warning: SI unhandled fs output type:%d\n",
1332 semantic_name);
1333 }
1334
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 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001338
1339 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001340 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001341 if (last_args[0]) {
1342 lp_build_intrinsic(base->gallivm->builder,
1343 "llvm.SI.export",
1344 LLVMVoidTypeInContext(base->gallivm->context),
1345 last_args, 9);
1346 }
1347
Marek Olšák0eb528a2013-12-04 13:24:22 +01001348 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001349 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001350
1351 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1352 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1353 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1354 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001355 si_llvm_init_export_args_load(bld_base,
1356 si_shader_ctx->radeon_bld.soa.outputs[index],
1357 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001358 lp_build_intrinsic(base->gallivm->builder,
1359 "llvm.SI.export",
1360 LLVMVoidTypeInContext(base->gallivm->context),
1361 args, 9);
1362 }
1363 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001364 } else {
1365 lp_build_intrinsic(base->gallivm->builder,
1366 "llvm.SI.export",
1367 LLVMVoidTypeInContext(base->gallivm->context),
1368 args, 9);
1369 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001370 }
1371 }
1372
Michel Dänzer1a616c12012-11-13 17:35:09 +01001373 if (depth_index >= 0 || stencil_index >= 0) {
1374 LLVMValueRef out_ptr;
1375 unsigned mask = 0;
1376
1377 /* Specify the target we are exporting */
1378 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1379
1380 if (depth_index >= 0) {
1381 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1382 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1383 mask |= 0x1;
1384
1385 if (stencil_index < 0) {
1386 args[6] =
1387 args[7] =
1388 args[8] = args[5];
1389 }
1390 }
1391
1392 if (stencil_index >= 0) {
1393 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
1394 args[7] =
1395 args[8] =
1396 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001397 /* Only setting the stencil component bit (0x2) here
1398 * breaks some stencil piglit tests
1399 */
1400 mask |= 0x3;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001401
1402 if (depth_index < 0)
1403 args[5] = args[6];
1404 }
1405
1406 /* Specify which components to enable */
1407 args[0] = lp_build_const_int32(base->gallivm, mask);
1408
1409 args[1] =
1410 args[2] =
1411 args[4] = uint->zero;
1412
1413 if (last_args[0])
1414 lp_build_intrinsic(base->gallivm->builder,
1415 "llvm.SI.export",
1416 LLVMVoidTypeInContext(base->gallivm->context),
1417 args, 9);
1418 else
1419 memcpy(last_args, args, sizeof(args));
1420 }
1421
Michel Dänzer51f89a02013-12-09 15:33:53 +09001422 if (!last_args[0]) {
1423 /* Specify which components to enable */
1424 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001425
Michel Dänzer51f89a02013-12-09 15:33:53 +09001426 /* Specify the target we are exporting */
1427 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001428
Michel Dänzer51f89a02013-12-09 15:33:53 +09001429 /* Set COMPR flag to zero to export data as 32-bit */
1430 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001431
Michel Dänzer51f89a02013-12-09 15:33:53 +09001432 /* dummy bits */
1433 last_args[5]= uint->zero;
1434 last_args[6]= uint->zero;
1435 last_args[7]= uint->zero;
1436 last_args[8]= uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001437
Michel Dänzer51f89a02013-12-09 15:33:53 +09001438 si_shader_ctx->shader->spi_shader_col_format |=
1439 V_028714_SPI_SHADER_32_ABGR;
1440 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001441 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001442
1443 /* Specify whether the EXEC mask represents the valid mask */
1444 last_args[1] = uint->one;
1445
1446 /* Specify that this is the last export */
1447 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1448
1449 lp_build_intrinsic(base->gallivm->builder,
1450 "llvm.SI.export",
1451 LLVMVoidTypeInContext(base->gallivm->context),
1452 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001453}
1454
Marek Olšák4855acd2013-08-06 15:08:54 +02001455static const struct lp_build_tgsi_action txf_action;
1456
1457static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1458 struct lp_build_tgsi_context * bld_base,
1459 struct lp_build_emit_data * emit_data);
1460
Tom Stellarda75c6162012-01-06 17:38:37 -05001461static void tex_fetch_args(
1462 struct lp_build_tgsi_context * bld_base,
1463 struct lp_build_emit_data * emit_data)
1464{
Christian König55fe5cc2013-03-04 16:30:06 +01001465 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001466 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001467 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001468 unsigned opcode = inst->Instruction.Opcode;
1469 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001470 LLVMValueRef coords[4];
1471 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001472 int ref_pos;
1473 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001474 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001475 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001476 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1477 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1478
1479 if (target == TGSI_TEXTURE_BUFFER) {
1480 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1481 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1482 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1483 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1484
1485 /* Truncate v32i8 to v16i8. */
1486 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1487 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1488 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1489 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1490
1491 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1492 emit_data->args[0] = res;
1493 emit_data->args[1] = bld_base->uint_bld.zero;
1494 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1495 emit_data->arg_count = 3;
1496 return;
1497 }
Tom Stellard467f5162012-05-16 15:15:35 -04001498
Michel Dänzer120efee2013-01-25 12:10:11 +01001499 /* Fetch and project texture coordinates */
1500 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001501 for (chan = 0; chan < 3; chan++ ) {
1502 coords[chan] = lp_build_emit_fetch(bld_base,
1503 emit_data->inst, 0,
1504 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001505 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001506 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1507 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001508 coords[chan],
1509 coords[3]);
1510 }
1511
Michel Dänzer120efee2013-01-25 12:10:11 +01001512 if (opcode == TGSI_OPCODE_TXP)
1513 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001514
Michel Dänzer120efee2013-01-25 12:10:11 +01001515 /* Pack LOD bias value */
1516 if (opcode == TGSI_OPCODE_TXB)
1517 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001518
Michel Dänzer0495adb2013-05-06 12:45:14 +02001519 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001520 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001521
1522 /* Pack depth comparison value */
1523 switch (target) {
1524 case TGSI_TEXTURE_SHADOW1D:
1525 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1526 case TGSI_TEXTURE_SHADOW2D:
1527 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001528 case TGSI_TEXTURE_SHADOWCUBE:
1529 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001530 assert(ref_pos >= 0);
1531 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001532 break;
1533 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1534 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001535 }
1536
Michel Dänzera6b83c02013-02-21 16:10:55 +01001537 /* Pack user derivatives */
1538 if (opcode == TGSI_OPCODE_TXD) {
1539 for (chan = 0; chan < 2; chan++) {
1540 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1541 if (num_coords > 1)
1542 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1543 }
1544 }
1545
Michel Dänzer120efee2013-01-25 12:10:11 +01001546 /* Pack texture coordinates */
1547 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001548 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001549 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001550 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001551 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001552
Marek Olšákd2bd6342013-09-18 15:40:21 +02001553 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001554 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001555 address[count++] = coords[3];
1556
1557 if (count > 16) {
1558 assert(!"Cannot handle more than 16 texture address parameters");
1559 count = 16;
1560 }
1561
1562 for (chan = 0; chan < count; chan++ ) {
1563 address[chan] = LLVMBuildBitCast(gallivm->builder,
1564 address[chan],
1565 LLVMInt32TypeInContext(gallivm->context),
1566 "");
1567 }
1568
Marek Olšák4855acd2013-08-06 15:08:54 +02001569 /* Adjust the sample index according to FMASK.
1570 *
1571 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1572 * which is the identity mapping. Each nibble says which physical sample
1573 * should be fetched to get that sample.
1574 *
1575 * For example, 0x11111100 means there are only 2 samples stored and
1576 * the second sample covers 3/4 of the pixel. When reading samples 0
1577 * and 1, return physical sample 0 (determined by the first two 0s
1578 * in FMASK), otherwise return physical sample 1.
1579 *
1580 * The sample index should be adjusted as follows:
1581 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1582 */
1583 if (target == TGSI_TEXTURE_2D_MSAA ||
1584 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1585 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1586 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001587 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001588 unsigned txf_count = count;
1589
Marek Olšákd2bd6342013-09-18 15:40:21 +02001590 memcpy(txf_address, address, sizeof(txf_address));
1591
1592 if (target == TGSI_TEXTURE_2D_MSAA) {
1593 txf_address[2] = bld_base->uint_bld.zero;
1594 }
1595 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001596
1597 /* Pad to a power-of-two size. */
1598 while (txf_count < util_next_power_of_two(txf_count))
1599 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1600
1601 /* Read FMASK using TXF. */
1602 txf_emit_data.chan = 0;
1603 txf_emit_data.dst_type = LLVMVectorType(
1604 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1605 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1606 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
Marek Olšákd2bd6342013-09-18 15:40:21 +02001607 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1608 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
Marek Olšák4855acd2013-08-06 15:08:54 +02001609 txf_emit_data.arg_count = 3;
1610
1611 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1612
1613 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001614 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1615 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1616
1617 /* Apply the formula. */
1618 LLVMValueRef fmask =
1619 LLVMBuildExtractElement(gallivm->builder,
1620 txf_emit_data.output[0],
1621 uint_bld->zero, "");
1622
Marek Olšákd2bd6342013-09-18 15:40:21 +02001623 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001624
1625 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001626 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001627
1628 LLVMValueRef shifted_fmask =
1629 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1630
1631 LLVMValueRef final_sample =
1632 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1633
1634 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1635 * resource descriptor is 0 (invalid),
1636 */
1637 LLVMValueRef fmask_desc =
1638 LLVMBuildBitCast(gallivm->builder,
1639 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1640 LLVMVectorType(uint_bld->elem_type, 8), "");
1641
1642 LLVMValueRef fmask_word1 =
1643 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1644 uint_bld->one, "");
1645
1646 LLVMValueRef word1_is_nonzero =
1647 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1648 fmask_word1, uint_bld->zero, "");
1649
Marek Olšákd2bd6342013-09-18 15:40:21 +02001650 /* Replace the MSAA sample index. */
1651 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001652 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001653 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001654 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001655
Michel Dänzer36231112013-05-02 09:44:45 +02001656 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001657 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001658
1659 if (opcode == TGSI_OPCODE_TXF) {
1660 /* add tex offsets */
1661 if (inst->Texture.NumOffsets) {
1662 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1663 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1664 const struct tgsi_texture_offset * off = inst->TexOffsets;
1665
1666 assert(inst->Texture.NumOffsets == 1);
1667
Marek Olšákdefedc02013-09-18 15:36:38 +02001668 switch (target) {
1669 case TGSI_TEXTURE_3D:
1670 address[2] = lp_build_add(uint_bld, address[2],
1671 bld->immediates[off->Index][off->SwizzleZ]);
1672 /* fall through */
1673 case TGSI_TEXTURE_2D:
1674 case TGSI_TEXTURE_SHADOW2D:
1675 case TGSI_TEXTURE_RECT:
1676 case TGSI_TEXTURE_SHADOWRECT:
1677 case TGSI_TEXTURE_2D_ARRAY:
1678 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001679 address[1] =
1680 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001681 bld->immediates[off->Index][off->SwizzleY]);
1682 /* fall through */
1683 case TGSI_TEXTURE_1D:
1684 case TGSI_TEXTURE_SHADOW1D:
1685 case TGSI_TEXTURE_1D_ARRAY:
1686 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1687 address[0] =
1688 lp_build_add(uint_bld, address[0],
1689 bld->immediates[off->Index][off->SwizzleX]);
1690 break;
1691 /* texture offsets do not apply to other texture targets */
1692 }
Michel Dänzer36231112013-05-02 09:44:45 +02001693 }
1694
1695 emit_data->dst_type = LLVMVectorType(
1696 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1697 4);
1698
1699 emit_data->arg_count = 3;
1700 } else {
1701 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001702 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001703
1704 emit_data->dst_type = LLVMVectorType(
1705 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1706 4);
1707
1708 emit_data->arg_count = 4;
1709 }
1710
1711 /* Dimensions */
1712 emit_data->args[emit_data->arg_count - 1] =
1713 lp_build_const_int32(bld_base->base.gallivm, target);
1714
Michel Dänzer120efee2013-01-25 12:10:11 +01001715 /* Pad to power of two vector */
1716 while (count < util_next_power_of_two(count))
1717 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1718
Christian Königccf3e8f2013-03-26 15:09:27 +01001719 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001720}
1721
Michel Dänzer07eddc42013-02-06 15:43:10 +01001722static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1723 struct lp_build_tgsi_context * bld_base,
1724 struct lp_build_emit_data * emit_data)
1725{
1726 struct lp_build_context * base = &bld_base->base;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001727 char intr_name[127];
Michel Dänzer07eddc42013-02-06 15:43:10 +01001728
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001729 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1730 emit_data->output[emit_data->chan] = build_intrinsic(
1731 base->gallivm->builder,
1732 "llvm.SI.vs.load.input", emit_data->dst_type,
1733 emit_data->args, emit_data->arg_count,
1734 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1735 return;
1736 }
1737
Michel Dänzer07eddc42013-02-06 15:43:10 +01001738 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001739 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001740
Christian König44e32242013-03-20 12:10:35 +01001741 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001742 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001743 emit_data->args, emit_data->arg_count,
1744 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001745}
1746
Michel Dänzer0495adb2013-05-06 12:45:14 +02001747static void txq_fetch_args(
1748 struct lp_build_tgsi_context * bld_base,
1749 struct lp_build_emit_data * emit_data)
1750{
1751 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1752 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001753 struct gallivm_state *gallivm = bld_base->base.gallivm;
1754
1755 if (inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1756 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1757 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1758
1759 /* Read the size from the buffer descriptor directly. */
1760 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1761 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1762 size = LLVMBuildExtractElement(gallivm->builder, size,
1763 lp_build_const_int32(gallivm, 2), "");
1764 emit_data->args[0] = size;
1765 return;
1766 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02001767
1768 /* Mip level */
1769 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1770
1771 /* Resource */
1772 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1773
1774 /* Dimensions */
1775 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1776 inst->Texture.Texture);
1777
1778 emit_data->arg_count = 3;
1779
1780 emit_data->dst_type = LLVMVectorType(
1781 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1782 4);
1783}
1784
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001785static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
1786 struct lp_build_tgsi_context * bld_base,
1787 struct lp_build_emit_data * emit_data)
1788{
1789 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1790 /* Just return the buffer size. */
1791 emit_data->output[emit_data->chan] = emit_data->args[0];
1792 return;
1793 }
1794
1795 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
1796}
1797
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001798#if HAVE_LLVM >= 0x0304
1799
1800static void si_llvm_emit_ddxy(
1801 const struct lp_build_tgsi_action * action,
1802 struct lp_build_tgsi_context * bld_base,
1803 struct lp_build_emit_data * emit_data)
1804{
1805 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1806 struct gallivm_state *gallivm = bld_base->base.gallivm;
1807 struct lp_build_context * base = &bld_base->base;
1808 const struct tgsi_full_instruction *inst = emit_data->inst;
1809 unsigned opcode = inst->Instruction.Opcode;
1810 LLVMValueRef indices[2];
1811 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1812 LLVMValueRef tl, trbl, result[4];
1813 LLVMTypeRef i32;
1814 unsigned swizzle[4];
1815 unsigned c;
1816
1817 i32 = LLVMInt32TypeInContext(gallivm->context);
1818
1819 indices[0] = bld_base->uint_bld.zero;
1820 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1821 NULL, 0, LLVMReadNoneAttribute);
1822 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1823 indices, 2, "");
1824
1825 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1826 lp_build_const_int32(gallivm, 0xfffffffc), "");
1827 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1828 indices, 2, "");
1829
1830 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1831 lp_build_const_int32(gallivm,
1832 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1833 "");
1834 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1835 indices, 2, "");
1836
1837 for (c = 0; c < 4; ++c) {
1838 unsigned i;
1839
1840 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1841 for (i = 0; i < c; ++i) {
1842 if (swizzle[i] == swizzle[c]) {
1843 result[c] = result[i];
1844 break;
1845 }
1846 }
1847 if (i != c)
1848 continue;
1849
1850 LLVMBuildStore(gallivm->builder,
1851 LLVMBuildBitCast(gallivm->builder,
1852 lp_build_emit_fetch(bld_base, inst, 0, c),
1853 i32, ""),
1854 store_ptr);
1855
1856 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1857 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1858
1859 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1860 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1861
1862 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1863 }
1864
1865 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1866}
1867
1868#endif /* HAVE_LLVM >= 0x0304 */
1869
Michel Dänzer404b29d2013-11-21 16:45:28 +09001870/* Emit one vertex from the geometry shader */
1871static void si_llvm_emit_vertex(
1872 const struct lp_build_tgsi_action *action,
1873 struct lp_build_tgsi_context *bld_base,
1874 struct lp_build_emit_data *emit_data)
1875{
1876 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001877 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001878 struct si_shader *shader = &si_shader_ctx->shader->shader;
1879 struct gallivm_state *gallivm = bld_base->base.gallivm;
1880 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001881 LLVMValueRef gs_next_vertex;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001882 LLVMValueRef t_list_ptr;
1883 LLVMValueRef t_list;
1884 LLVMValueRef args[2];
1885 unsigned chan;
1886 int i;
1887
1888 /* Load the GSVS ring resource descriptor */
1889 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1890 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
1891 lp_build_const_int32(gallivm,
1892 NUM_PIPE_CONST_BUFFERS + 2));
1893
1894 if (shader->noutput == 0) {
1895 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1896
1897 while (!tgsi_parse_end_of_tokens(parse)) {
1898 tgsi_parse_token(parse);
1899
Michel Dänzer7c7d7382014-01-09 15:33:34 +09001900 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
1901 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
1902
1903 if (d->Declaration.File == TGSI_FILE_OUTPUT)
1904 si_store_shader_io_attribs(shader, d);
1905 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001906 }
1907 }
1908
1909 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001910 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001911 for (i = 0; i < shader->noutput; i++) {
1912 LLVMValueRef *out_ptr =
1913 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
1914
1915 for (chan = 0; chan < 4; chan++) {
1916 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001917 LLVMValueRef soffset =
1918 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1919 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001920 LLVMValueRef voffset =
1921 lp_build_const_int32(gallivm, (i * 4 + chan) *
1922 shader->gs_max_out_vertices);
1923
1924 voffset = lp_build_add(uint, voffset, gs_next_vertex);
1925 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001926
1927 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1928
1929 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
1930 voffset, soffset, 0,
1931 V_008F0C_BUF_DATA_FORMAT_32,
1932 V_008F0C_BUF_NUM_FORMAT_UINT,
1933 1, 0, 1, 1, 0);
1934 }
1935 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001936 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
1937 lp_build_const_int32(gallivm, 1));
1938 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001939
1940 /* Signal vertex emission */
1941 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
1942 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1943 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1944 LLVMVoidTypeInContext(gallivm->context), args, 2,
1945 LLVMNoUnwindAttribute);
1946}
1947
1948/* Cut one primitive from the geometry shader */
1949static void si_llvm_emit_primitive(
1950 const struct lp_build_tgsi_action *action,
1951 struct lp_build_tgsi_context *bld_base,
1952 struct lp_build_emit_data *emit_data)
1953{
1954 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1955 struct gallivm_state *gallivm = bld_base->base.gallivm;
1956 LLVMValueRef args[2];
1957
1958 /* Signal primitive cut */
1959 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
1960 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1961 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1962 LLVMVoidTypeInContext(gallivm->context), args, 2,
1963 LLVMNoUnwindAttribute);
1964}
1965
Tom Stellarda75c6162012-01-06 17:38:37 -05001966static const struct lp_build_tgsi_action tex_action = {
1967 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001968 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001969 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001970};
1971
Michel Dänzer3e205132012-11-06 17:39:01 +01001972static const struct lp_build_tgsi_action txb_action = {
1973 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001974 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001975 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001976};
1977
Michel Dänzera6b83c02013-02-21 16:10:55 +01001978#if HAVE_LLVM >= 0x0304
1979static const struct lp_build_tgsi_action txd_action = {
1980 .fetch_args = tex_fetch_args,
1981 .emit = build_tex_intrinsic,
1982 .intr_name = "llvm.SI.sampled."
1983};
1984#endif
1985
Michel Dänzer36231112013-05-02 09:44:45 +02001986static const struct lp_build_tgsi_action txf_action = {
1987 .fetch_args = tex_fetch_args,
1988 .emit = build_tex_intrinsic,
1989 .intr_name = "llvm.SI.imageload."
1990};
1991
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001992static const struct lp_build_tgsi_action txl_action = {
1993 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001994 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001995 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001996};
1997
Michel Dänzer0495adb2013-05-06 12:45:14 +02001998static const struct lp_build_tgsi_action txq_action = {
1999 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002000 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002001 .intr_name = "llvm.SI.resinfo"
2002};
2003
Christian König206f0592013-03-20 14:37:21 +01002004static void create_meta_data(struct si_shader_context *si_shader_ctx)
2005{
2006 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2007 LLVMValueRef args[3];
2008
2009 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2010 args[1] = 0;
2011 args[2] = lp_build_const_int32(gallivm, 1);
2012
2013 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2014}
2015
Christian König55fe5cc2013-03-04 16:30:06 +01002016static void create_function(struct si_shader_context *si_shader_ctx)
2017{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002018 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2019 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002020 struct si_pipe_shader *shader = si_shader_ctx->shader;
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002021 LLVMTypeRef params[21], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002022 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002023
Christian König55fe5cc2013-03-04 16:30:06 +01002024 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002025 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002026 f32 = LLVMFloatTypeInContext(gallivm->context);
2027 v2i32 = LLVMVectorType(i32, 2);
2028 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01002029
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002030 params[SI_PARAM_CONST] = LLVMPointerType(
2031 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
2032 /* We assume at most 16 textures per program at the moment.
2033 * This need probably need to be changed to support bindless textures */
2034 params[SI_PARAM_SAMPLER] = LLVMPointerType(
2035 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
2036 params[SI_PARAM_RESOURCE] = LLVMPointerType(
2037 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002038
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002039 switch (si_shader_ctx->type) {
2040 case TGSI_PROCESSOR_VERTEX:
2041 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Marek Olšák2993cca2013-08-18 02:34:23 +02002042 params[SI_PARAM_SO_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01002043 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002044 num_params = SI_PARAM_START_INSTANCE+1;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002045 if (shader->key.vs.as_es) {
2046 params[SI_PARAM_ES2GS_OFFSET] = i32;
2047 num_params++;
2048 } else {
2049 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002050
Michel Dänzer404b29d2013-11-21 16:45:28 +09002051 /* Streamout SGPRs. */
2052 if (shader->selector->so.num_outputs) {
2053 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2054 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2055 }
2056 /* A streamout buffer offset is loaded if the stride is non-zero. */
2057 for (i = 0; i < 4; i++) {
2058 if (!shader->selector->so.stride[i])
2059 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002060
Michel Dänzer404b29d2013-11-21 16:45:28 +09002061 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2062 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002063 }
2064
2065 last_sgpr = num_params-1;
2066
2067 /* VGPRs */
2068 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2069 params[num_params++] = i32; /* unused*/
2070 params[num_params++] = i32; /* unused */
2071 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002072 break;
Christian König0666ffd2013-03-05 15:07:39 +01002073
Michel Dänzer404b29d2013-11-21 16:45:28 +09002074 case TGSI_PROCESSOR_GEOMETRY:
2075 params[SI_PARAM_GS2VS_OFFSET] = i32;
2076 params[SI_PARAM_GS_WAVE_ID] = i32;
2077 last_sgpr = SI_PARAM_GS_WAVE_ID;
2078
2079 /* VGPRs */
2080 params[SI_PARAM_VTX0_OFFSET] = i32;
2081 params[SI_PARAM_VTX1_OFFSET] = i32;
2082 params[SI_PARAM_PRIMITIVE_ID] = i32;
2083 params[SI_PARAM_VTX2_OFFSET] = i32;
2084 params[SI_PARAM_VTX3_OFFSET] = i32;
2085 params[SI_PARAM_VTX4_OFFSET] = i32;
2086 params[SI_PARAM_VTX5_OFFSET] = i32;
2087 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2088 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2089 break;
2090
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002091 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002092 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002093 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002094 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002095 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2096 params[SI_PARAM_PERSP_CENTER] = v2i32;
2097 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2098 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2099 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2100 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2101 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2102 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2103 params[SI_PARAM_POS_X_FLOAT] = f32;
2104 params[SI_PARAM_POS_Y_FLOAT] = f32;
2105 params[SI_PARAM_POS_Z_FLOAT] = f32;
2106 params[SI_PARAM_POS_W_FLOAT] = f32;
2107 params[SI_PARAM_FRONT_FACE] = f32;
2108 params[SI_PARAM_ANCILLARY] = f32;
2109 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2110 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002111 num_params = SI_PARAM_POS_FIXED_PT+1;
2112 break;
2113
2114 default:
2115 assert(0 && "unimplemented shader");
2116 return;
Christian Königc4973212013-03-05 12:14:02 +01002117 }
Christian König55fe5cc2013-03-04 16:30:06 +01002118
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002119 assert(num_params <= Elements(params));
2120 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002121 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002122
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002123 for (i = 0; i <= last_sgpr; ++i) {
2124 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002125 switch (i) {
2126 default:
2127 LLVMAddAttribute(P, LLVMInRegAttribute);
2128 break;
2129#if HAVE_LLVM >= 0x0304
2130 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2131 * to move load. Inputs are constant so this is fine. */
2132 case SI_PARAM_CONST:
2133 case SI_PARAM_SAMPLER:
2134 case SI_PARAM_RESOURCE:
2135 LLVMAddAttribute(P, LLVMByValAttribute);
2136 break;
2137#endif
2138 }
Christian Königcf9b31f2013-03-21 18:30:23 +01002139 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002140
2141#if HAVE_LLVM >= 0x0304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002142 if (bld_base->info &&
2143 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2144 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002145 si_shader_ctx->ddxy_lds =
2146 LLVMAddGlobalInAddressSpace(gallivm->module,
2147 LLVMArrayType(i32, 64),
2148 "ddxy_lds",
2149 LOCAL_ADDR_SPACE);
2150#endif
Christian König55fe5cc2013-03-04 16:30:06 +01002151}
Tom Stellarda75c6162012-01-06 17:38:37 -05002152
Christian König0f6cf2b2013-03-15 15:53:25 +01002153static void preload_constants(struct si_shader_context *si_shader_ctx)
2154{
2155 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2156 struct gallivm_state * gallivm = bld_base->base.gallivm;
2157 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002158 unsigned buf;
2159 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002160
Marek Olšák2fd42002013-10-25 11:45:47 +02002161 for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) {
2162 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002163
Marek Olšák2fd42002013-10-25 11:45:47 +02002164 if (num_const == 0)
2165 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002166
Marek Olšák2fd42002013-10-25 11:45:47 +02002167 /* Allocate space for the constant values */
2168 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002169
Marek Olšák2fd42002013-10-25 11:45:47 +02002170 /* Load the resource descriptor */
2171 si_shader_ctx->const_resource[buf] =
2172 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002173
Marek Olšák2fd42002013-10-25 11:45:47 +02002174 /* Load the constants, we rely on the code sinking to do the rest */
2175 for (i = 0; i < num_const * 4; ++i) {
2176 LLVMValueRef args[2] = {
2177 si_shader_ctx->const_resource[buf],
2178 lp_build_const_int32(gallivm, i * 4)
2179 };
2180 si_shader_ctx->constants[buf][i] =
2181 build_intrinsic(gallivm->builder, "llvm.SI.load.const",
2182 bld_base->base.elem_type, args, 2,
2183 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2184 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002185 }
2186}
2187
Christian König1c100182013-03-17 16:02:42 +01002188static void preload_samplers(struct si_shader_context *si_shader_ctx)
2189{
2190 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2191 struct gallivm_state * gallivm = bld_base->base.gallivm;
2192 const struct tgsi_shader_info * info = bld_base->info;
2193
2194 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2195
2196 LLVMValueRef res_ptr, samp_ptr;
2197 LLVMValueRef offset;
2198
2199 if (num_samplers == 0)
2200 return;
2201
2202 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02002203 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002204 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2205
2206 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2207 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2208
2209 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2210 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002211 /* Resource */
2212 offset = lp_build_const_int32(gallivm, i);
2213 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2214
2215 /* Sampler */
2216 offset = lp_build_const_int32(gallivm, i);
2217 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002218
2219 /* FMASK resource */
2220 if (info->is_msaa_sampler[i]) {
2221 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
2222 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
2223 build_indexed_load(si_shader_ctx, res_ptr, offset);
2224 }
Christian König1c100182013-03-17 16:02:42 +01002225 }
2226}
2227
Marek Olšák8d03d922013-09-01 23:59:06 +02002228static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2229{
2230 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2231 struct gallivm_state * gallivm = bld_base->base.gallivm;
2232 unsigned i;
2233
2234 if (!si_shader_ctx->shader->selector->so.num_outputs)
2235 return;
2236
2237 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2238 SI_PARAM_SO_BUFFER);
2239
2240 /* Load the resources, we rely on the code sinking to do the rest */
2241 for (i = 0; i < 4; ++i) {
2242 if (si_shader_ctx->shader->selector->so.stride[i]) {
2243 LLVMValueRef offset = lp_build_const_int32(gallivm, i);
2244
2245 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2246 }
2247 }
2248}
2249
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002250int si_compile_llvm(struct si_context *sctx, struct si_pipe_shader *shader,
Tom Stellard302f53d2012-10-25 13:50:10 -04002251 LLVMModuleRef mod)
2252{
Tom Stellard302f53d2012-10-25 13:50:10 -04002253 unsigned i;
2254 uint32_t *ptr;
Tom Stellard7782d192013-04-04 09:57:13 -07002255 struct radeon_llvm_binary binary;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002256 bool dump = r600_can_dump_shader(&sctx->screen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002257 shader->selector ? shader->selector->tokens : NULL);
Tom Stellard7782d192013-04-04 09:57:13 -07002258 memset(&binary, 0, sizeof(binary));
2259 radeon_llvm_compile(mod, &binary,
Marek Olšák65dc5882014-01-22 02:42:20 +01002260 r600_get_llvm_processor_name(sctx->screen->b.family), dump);
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002261 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002262 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002263 for (i = 0; i < binary.code_size; i+=4 ) {
2264 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2265 binary.code[i + 2], binary.code[i + 1],
2266 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002267 }
2268 }
2269
Tom Stellardd50343d2013-04-04 16:21:06 -04002270 /* XXX: We may be able to emit some of these values directly rather than
2271 * extracting fields to be emitted later.
2272 */
2273 for (i = 0; i < binary.config_size; i+= 8) {
2274 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2275 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2276 switch (reg) {
2277 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2278 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2279 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2280 case R_00B848_COMPUTE_PGM_RSRC1:
2281 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2282 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2283 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002284 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2285 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2286 break;
2287 case R_00B84C_COMPUTE_PGM_RSRC2:
2288 shader->lds_size = G_00B84C_LDS_SIZE(value);
2289 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002290 case R_0286CC_SPI_PS_INPUT_ENA:
2291 shader->spi_ps_input_ena = value;
2292 break;
2293 default:
2294 fprintf(stderr, "Warning: Compiler emitted unknown "
2295 "config register: 0x%x\n", reg);
2296 break;
2297 }
2298 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002299
2300 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02002301 r600_resource_reference(&shader->bo, NULL);
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002302 shader->bo = si_resource_create_custom(sctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04002303 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002304 if (shader->bo == NULL) {
2305 return -ENOMEM;
2306 }
2307
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002308 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 +01002309 if (0 /*SI_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04002310 for (i = 0; i < binary.code_size / 4; ++i) {
2311 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04002312 }
2313 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04002314 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002315 }
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002316 sctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002317
Tom Stellard7782d192013-04-04 09:57:13 -07002318 free(binary.code);
2319 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04002320
2321 return 0;
2322}
2323
Michel Dänzer404b29d2013-11-21 16:45:28 +09002324/* Generate code for the hardware VS shader stage to go with a geometry shader */
2325static int si_generate_gs_copy_shader(struct si_context *sctx,
2326 struct si_shader_context *si_shader_ctx,
2327 bool dump)
2328{
2329 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2330 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2331 struct lp_build_context *base = &bld_base->base;
2332 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002333 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002334 struct si_shader *gs = &si_shader_ctx->shader->selector->current->shader;
2335 struct si_shader_output_values *outputs;
2336 LLVMValueRef t_list_ptr, t_list;
2337 LLVMValueRef args[9];
2338 int i, r;
2339
2340 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2341
2342 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
2343 si_shader_ctx->gs_for_vs = gs;
2344
2345 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2346
2347 create_meta_data(si_shader_ctx);
2348 create_function(si_shader_ctx);
2349 preload_streamout_buffers(si_shader_ctx);
2350
2351 /* Load the GSVS ring resource descriptor */
2352 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
2353 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
2354 lp_build_const_int32(gallivm,
2355 NUM_PIPE_CONST_BUFFERS + 1));
2356
2357 args[0] = t_list;
2358 args[1] = lp_build_mul_imm(uint,
2359 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2360 si_shader_ctx->param_vertex_id),
2361 4);
2362 args[3] = uint->zero;
2363 args[4] = uint->one; /* OFFEN */
2364 args[5] = uint->zero; /* IDXEN */
2365 args[6] = uint->one; /* GLC */
2366 args[7] = uint->one; /* SLC */
2367 args[8] = uint->zero; /* TFE */
2368
2369 /* Fetch vertex data from GSVS ring */
2370 for (i = 0; i < gs->noutput; ++i) {
2371 struct si_shader_output *out = gs->output + i;
2372 unsigned chan;
2373
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002374 shader->output[i] = *out;
2375
Michel Dänzer404b29d2013-11-21 16:45:28 +09002376 outputs[i].name = out->name;
2377 outputs[i].index = out->index;
2378 outputs[i].usage = out->usage;
2379
2380 for (chan = 0; chan < 4; chan++) {
2381 args[2] = lp_build_const_int32(gallivm,
2382 (i * 4 + chan) *
2383 gs->gs_max_out_vertices * 16 * 4);
2384
2385 outputs[i].values[chan] =
2386 LLVMBuildBitCast(gallivm->builder,
2387 build_intrinsic(gallivm->builder,
2388 "llvm.SI.buffer.load.dword.i32.i32",
2389 LLVMInt32TypeInContext(gallivm->context),
2390 args, 9,
2391 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2392 base->elem_type, "");
2393 }
2394 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002395 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002396
2397 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2398
2399 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2400
2401 if (dump)
2402 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2403
2404 r = si_compile_llvm(sctx, si_shader_ctx->shader,
2405 bld_base->base.gallivm->module);
2406
2407 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2408
2409 FREE(outputs);
2410 return r;
2411}
2412
Tom Stellarda75c6162012-01-06 17:38:37 -05002413int si_pipe_shader_create(
2414 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01002415 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002416{
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002417 struct si_context *sctx = (struct si_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002418 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002419 struct si_shader_context si_shader_ctx;
2420 struct tgsi_shader_info shader_info;
2421 struct lp_build_tgsi_context * bld_base;
2422 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002423 int r = 0;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002424 bool dump = r600_can_dump_shader(&sctx->screen->b, shader->selector->tokens);
Tom Stellarda75c6162012-01-06 17:38:37 -05002425
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002426 assert(shader->shader.noutput == 0);
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002427 assert(shader->shader.nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002428 assert(shader->shader.ninput == 0);
2429
Michel Dänzercfebaf92012-08-31 19:04:08 +02002430 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002431 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2432 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2433
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002434 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002435
Michel Dänzere44dfd42012-11-07 17:33:08 +01002436 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01002437 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002438 bld_base->info = &shader_info;
2439 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002440
2441 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01002442 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01002443#if HAVE_LLVM >= 0x0304
2444 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
2445#endif
Michel Dänzer36231112013-05-02 09:44:45 +02002446 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002447 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002448 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002449 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002450
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002451#if HAVE_LLVM >= 0x0304
2452 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2453 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
2454#endif
2455
Michel Dänzer404b29d2013-11-21 16:45:28 +09002456 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2457 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2458
Christian Könige4ed5872013-03-21 18:02:52 +01002459 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002460 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002461 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2462 si_shader_ctx.shader = shader;
2463 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002464
Michel Dänzer51f89a02013-12-09 15:33:53 +09002465 switch (si_shader_ctx.type) {
2466 case TGSI_PROCESSOR_VERTEX:
2467 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002468 if (shader->key.vs.as_es) {
2469 si_shader_ctx.gs_for_vs = &sctx->gs_shader->current->shader;
2470 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2471 } else {
2472 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2473 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002474 break;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002475 case TGSI_PROCESSOR_GEOMETRY: {
2476 int i;
2477
2478 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2479 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2480 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
2481
2482 for (i = 0; i < shader_info.num_properties; i++) {
2483 switch (shader_info.properties[i].name) {
2484 case TGSI_PROPERTY_GS_INPUT_PRIM:
2485 shader->shader.gs_input_prim = shader_info.properties[i].data[0];
2486 break;
2487 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
2488 shader->shader.gs_output_prim = shader_info.properties[i].data[0];
2489 break;
2490 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
2491 shader->shader.gs_max_out_vertices = shader_info.properties[i].data[0];
2492 break;
2493 }
2494 }
2495 break;
2496 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002497 case TGSI_PROCESSOR_FRAGMENT:
2498 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2499 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
2500 break;
2501 default:
2502 assert(!"Unsupported shader type");
2503 return -1;
2504 }
2505
Christian König206f0592013-03-20 14:37:21 +01002506 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002507 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002508 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002509 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002510 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002511
Tom Stellard185fc9a2012-07-12 10:40:47 -04002512 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2513 * conversion fails. */
2514 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002515 tgsi_dump(sel->tokens, 0);
Marek Olšák8d03d922013-09-01 23:59:06 +02002516 si_dump_streamout(&sel->so);
Tom Stellard185fc9a2012-07-12 10:40:47 -04002517 }
2518
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002519 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2520 si_shader_ctx.gs_next_vertex =
2521 lp_build_alloca(bld_base->base.gallivm,
2522 bld_base->uint_bld.elem_type, "");
2523 }
2524
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002525 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002526 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002527 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002528 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002529
2530 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2531
2532 mod = bld_base->base.gallivm->module;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002533 r = si_compile_llvm(sctx, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002534 if (r) {
2535 fprintf(stderr, "LLVM failed to compile shader\n");
2536 goto out;
2537 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002538
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002539 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002540
2541 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2542 shader->gs_copy_shader = CALLOC_STRUCT(si_pipe_shader);
2543 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002544 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002545 si_shader_ctx.shader = shader->gs_copy_shader;
2546 if ((r = si_generate_gs_copy_shader(sctx, &si_shader_ctx, dump))) {
2547 free(shader->gs_copy_shader);
2548 shader->gs_copy_shader = NULL;
2549 goto out;
2550 }
2551 }
2552
Tom Stellarda75c6162012-01-06 17:38:37 -05002553 tgsi_parse_free(&si_shader_ctx.parse);
2554
Michel Dänzer404b29d2013-11-21 16:45:28 +09002555out:
Marek Olšák2fd42002013-10-25 11:45:47 +02002556 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2557 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002558 FREE(si_shader_ctx.resources);
2559 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002560
Tom Stellard302f53d2012-10-25 13:50:10 -04002561 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002562}
2563
2564void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
2565{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002566 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002567}