blob: 5b95c11580fa1a9e15b79c47964e7c75b21c245e [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;
Michel Dänzer67e385b2014-01-08 17:48:21 +090053 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090054 unsigned usage;
55};
56
Tom Stellarda75c6162012-01-06 17:38:37 -050057struct si_shader_context
58{
59 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050060 struct tgsi_parse_context parse;
61 struct tgsi_token * tokens;
62 struct si_pipe_shader *shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +090063 struct si_shader *gs_for_vs;
Tom Stellarda75c6162012-01-06 17:38:37 -050064 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020065 int param_streamout_config;
66 int param_streamout_write_index;
67 int param_streamout_offset[4];
68 int param_vertex_id;
69 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010070 LLVMValueRef const_md;
Marek Olšák2fd42002013-10-25 11:45:47 +020071 LLVMValueRef const_resource[NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020072#if HAVE_LLVM >= 0x0304
73 LLVMValueRef ddxy_lds;
74#endif
Marek Olšák2fd42002013-10-25 11:45:47 +020075 LLVMValueRef *constants[NUM_CONST_BUFFERS];
Christian König1c100182013-03-17 16:02:42 +010076 LLVMValueRef *resources;
77 LLVMValueRef *samplers;
Marek Olšák8d03d922013-09-01 23:59:06 +020078 LLVMValueRef so_buffers[4];
Michel Dänzerf07a96d2014-01-08 18:45:10 +090079 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050080};
81
82static struct si_shader_context * si_shader_context(
83 struct lp_build_tgsi_context * bld_base)
84{
85 return (struct si_shader_context *)bld_base;
86}
87
88
89#define PERSPECTIVE_BASE 0
90#define LINEAR_BASE 9
91
92#define SAMPLE_OFFSET 0
93#define CENTER_OFFSET 2
94#define CENTROID_OFSET 4
95
96#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040097#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020098#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040099#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -0500100
Michel Dänzer404b29d2013-11-21 16:45:28 +0900101
102#define SENDMSG_GS 2
103#define SENDMSG_GS_DONE 3
104
105#define SENDMSG_GS_OP_NOP (0 << 4)
106#define SENDMSG_GS_OP_CUT (1 << 4)
107#define SENDMSG_GS_OP_EMIT (2 << 4)
108#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
109
110
Tom Stellard467f5162012-05-16 15:15:35 -0400111/**
112 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
113 *
114 * @param offset The offset parameter specifies the number of
115 * elements to offset, not the number of bytes or dwords. An element is the
116 * the type pointed to by the base_ptr parameter (e.g. int is the element of
117 * an int* pointer)
118 *
119 * When LLVM lowers the load instruction, it will convert the element offset
120 * into a dword offset automatically.
121 *
122 */
123static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100124 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400125 LLVMValueRef base_ptr,
126 LLVMValueRef offset)
127{
Christian König206f0592013-03-20 14:37:21 +0100128 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400129
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200130 LLVMValueRef indices[2] = {
131 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
132 offset
133 };
Christian König206f0592013-03-20 14:37:21 +0100134 LLVMValueRef computed_ptr = LLVMBuildGEP(
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200135 base->gallivm->builder, base_ptr, indices, 2, "");
Christian König206f0592013-03-20 14:37:21 +0100136
137 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
138 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
139 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400140}
141
Marek Olšákf317ce52013-09-05 15:39:57 +0200142static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100143 struct radeon_llvm_context * radeon_bld,
144 unsigned divisor)
145{
Marek Olšák8d03d922013-09-01 23:59:06 +0200146 struct si_shader_context *si_shader_ctx =
147 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100148 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
149
Marek Olšák8d03d922013-09-01 23:59:06 +0200150 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
151 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100152 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
153 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
154
155 if (divisor > 1)
156 result = LLVMBuildUDiv(gallivm->builder, result,
157 lp_build_const_int32(gallivm, divisor), "");
158
159 return result;
160}
161
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900162static int si_store_shader_io_attribs(struct si_shader *shader,
163 const struct tgsi_full_declaration *d)
164{
165 int i = -1;
166
167 switch (d->Declaration.File) {
168 case TGSI_FILE_INPUT:
169 i = shader->ninput++;
170 assert(i < Elements(shader->input));
171 shader->input[i].name = d->Semantic.Name;
172 shader->input[i].sid = d->Semantic.Index;
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900173 shader->input[i].index = d->Range.First;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900174 shader->input[i].interpolate = d->Interp.Interpolate;
175 shader->input[i].centroid = d->Interp.Centroid;
176 return -1;
177
178 case TGSI_FILE_OUTPUT:
179 i = shader->noutput++;
180 assert(i < Elements(shader->output));
181 shader->output[i].name = d->Semantic.Name;
182 shader->output[i].sid = d->Semantic.Index;
183 shader->output[i].index = d->Range.First;
184 shader->output[i].usage = d->Declaration.UsageMask;
185 break;
186 }
187
188 return i;
189}
190
Tom Stellarda75c6162012-01-06 17:38:37 -0500191static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900192 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500193 unsigned input_index,
194 const struct tgsi_full_declaration *decl)
195{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900196 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
197 struct gallivm_state *gallivm = base->gallivm;
198 struct si_shader_context *si_shader_ctx =
199 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100200 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
201
202 unsigned chan;
203
Tom Stellarda75c6162012-01-06 17:38:37 -0500204 LLVMValueRef t_list_ptr;
205 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400206 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500207 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100208 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400209 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500210 LLVMTypeRef vec4_type;
211 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500212
Tom Stellard467f5162012-05-16 15:15:35 -0400213 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100214 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500215
Michel Dänzer51f89a02013-12-09 15:33:53 +0900216 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400217
Christian König206f0592013-03-20 14:37:21 +0100218 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400219
220 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900221 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500222
Christian Königa0dca442013-03-22 15:59:22 +0100223 if (divisor) {
224 /* Build index from instance ID, start instance and divisor */
225 si_shader_ctx->shader->shader.uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200226 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100227 } else {
228 /* Load the buffer index, which is always stored in VGPR0
229 * for Vertex Shaders */
Marek Olšák8d03d922013-09-01 23:59:06 +0200230 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
231 si_shader_ctx->param_vertex_id);
Christian Königa0dca442013-03-22 15:59:22 +0100232 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500233
234 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400235 args[0] = t_list;
236 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100237 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900238 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100239 "llvm.SI.vs.load.input", vec4_type, args, 3,
240 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500241
242 /* Break up the vec4 into individual components */
243 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900244 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500245 /* XXX: Use a helper function for this. There is one in
246 * tgsi_llvm.c. */
247 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900248 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500249 input, llvm_chan, "");
250 }
251}
252
Michel Dänzer404b29d2013-11-21 16:45:28 +0900253static void declare_input_gs(
254 struct radeon_llvm_context *radeon_bld,
255 unsigned input_index,
256 const struct tgsi_full_declaration *decl)
257{
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900258 struct si_shader_context *si_shader_ctx =
259 si_shader_context(&radeon_bld->soa.bld_base);
260 struct si_shader *shader = &si_shader_ctx->shader->shader;
261
262 si_store_shader_io_attribs(shader, decl);
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900263
264 if (decl->Semantic.Name != TGSI_SEMANTIC_PRIMID)
265 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900266}
267
268static LLVMValueRef fetch_input_gs(
269 struct lp_build_tgsi_context *bld_base,
270 const struct tgsi_full_src_register *reg,
271 enum tgsi_opcode_type type,
272 unsigned swizzle)
273{
274 struct lp_build_context *base = &bld_base->base;
275 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900276 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900277 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
278 struct gallivm_state *gallivm = base->gallivm;
279 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
280 LLVMValueRef vtx_offset;
281 LLVMValueRef t_list_ptr;
282 LLVMValueRef t_list;
283 LLVMValueRef args[9];
284 unsigned vtx_offset_param;
285
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900286 if (swizzle != ~0 &&
287 shader->input[reg->Register.Index].name == TGSI_SEMANTIC_PRIMID) {
288 if (swizzle == 0)
289 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
290 SI_PARAM_PRIMITIVE_ID);
291 else
292 return uint->zero;
293 }
294
Michel Dänzer404b29d2013-11-21 16:45:28 +0900295 if (!reg->Register.Dimension)
296 return NULL;
297
298 if (swizzle == ~0) {
299 LLVMValueRef values[TGSI_NUM_CHANNELS];
300 unsigned chan;
301 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
302 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
303 }
304 return lp_build_gather_values(bld_base->base.gallivm, values,
305 TGSI_NUM_CHANNELS);
306 }
307
308 /* Get the vertex offset parameter */
309 vtx_offset_param = reg->Dimension.Index;
310 if (vtx_offset_param < 2) {
311 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
312 } else {
313 assert(vtx_offset_param < 6);
314 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
315 }
316 vtx_offset = lp_build_mul_imm(uint,
317 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
318 vtx_offset_param),
319 4);
320
321 /* Load the ESGS ring resource descriptor */
322 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
323 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900324 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900325
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
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900845 /* The instruction offset field has 12 bits */
846 assert(offen || inst_offset < (1 << 12));
847
Marek Olšák8d03d922013-09-01 23:59:06 +0200848 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
849 unsigned func = CLAMP(num_channels, 1, 3) - 1;
850 const char *types[] = {"i32", "v2i32", "v4i32"};
851 char name[256];
852 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
853
854 lp_build_intrinsic(gallivm->builder, name,
855 LLVMVoidTypeInContext(gallivm->context),
856 args, Elements(args));
857}
858
859static void build_streamout_store(struct si_shader_context *shader,
860 LLVMValueRef rsrc,
861 LLVMValueRef vdata,
862 unsigned num_channels,
863 LLVMValueRef vaddr,
864 LLVMValueRef soffset,
865 unsigned inst_offset)
866{
867 static unsigned dfmt[] = {
868 V_008F0C_BUF_DATA_FORMAT_32,
869 V_008F0C_BUF_DATA_FORMAT_32_32,
870 V_008F0C_BUF_DATA_FORMAT_32_32_32,
871 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
872 };
873 assert(num_channels >= 1 && num_channels <= 4);
874
875 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
876 inst_offset, dfmt[num_channels-1],
877 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
878}
879
880/* On SI, the vertex shader is responsible for writing streamout data
881 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900882static void si_llvm_emit_streamout(struct si_shader_context *shader,
883 struct si_shader_output_values *outputs,
884 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200885{
886 struct pipe_stream_output_info *so = &shader->shader->selector->so;
887 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
888 LLVMBuilderRef builder = gallivm->builder;
889 int i, j;
890 struct lp_build_if_state if_ctx;
891
892 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
893
894 LLVMValueRef so_param =
895 LLVMGetParam(shader->radeon_bld.main_fn,
896 shader->param_streamout_config);
897
898 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
899 LLVMValueRef so_vtx_count =
900 LLVMBuildAnd(builder,
901 LLVMBuildLShr(builder, so_param,
902 LLVMConstInt(i32, 16, 0), ""),
903 LLVMConstInt(i32, 127, 0), "");
904
905 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
906 NULL, 0, LLVMReadNoneAttribute);
907
908 /* can_emit = tid < so_vtx_count; */
909 LLVMValueRef can_emit =
910 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
911
912 /* Emit the streamout code conditionally. This actually avoids
913 * out-of-bounds buffer access. The hw tells us via the SGPR
914 * (so_vtx_count) which threads are allowed to emit streamout data. */
915 lp_build_if(&if_ctx, gallivm, can_emit);
916 {
917 /* The buffer offset is computed as follows:
918 * ByteOffset = streamout_offset[buffer_id]*4 +
919 * (streamout_write_index + thread_id)*stride[buffer_id] +
920 * attrib_offset
921 */
922
923 LLVMValueRef so_write_index =
924 LLVMGetParam(shader->radeon_bld.main_fn,
925 shader->param_streamout_write_index);
926
927 /* Compute (streamout_write_index + thread_id). */
928 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
929
930 /* Compute the write offset for each enabled buffer. */
931 LLVMValueRef so_write_offset[4] = {};
932 for (i = 0; i < 4; i++) {
933 if (!so->stride[i])
934 continue;
935
936 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
937 shader->param_streamout_offset[i]);
938 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
939
940 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
941 LLVMConstInt(i32, so->stride[i]*4, 0), "");
942 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
943 }
944
Marek Olšák8d03d922013-09-01 23:59:06 +0200945 /* Write streamout data. */
946 for (i = 0; i < so->num_outputs; i++) {
947 unsigned buf_idx = so->output[i].output_buffer;
948 unsigned reg = so->output[i].register_index;
949 unsigned start = so->output[i].start_component;
950 unsigned num_comps = so->output[i].num_components;
951 LLVMValueRef out[4];
952
953 assert(num_comps && num_comps <= 4);
954 if (!num_comps || num_comps > 4)
955 continue;
956
957 /* Load the output as int. */
958 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +0900959 unsigned outidx = 0;
960
961 while (outidx < noutput && outputs[outidx].index != reg)
962 outidx++;
963
964 if (outidx < noutput)
965 out[j] = LLVMBuildBitCast(builder,
966 outputs[outidx].values[start+j],
967 i32, "");
968 else
969 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +0200970 }
971
Michel Dänzer67e385b2014-01-08 17:48:21 +0900972 if (!out[0])
973 continue;
974
Marek Olšák8d03d922013-09-01 23:59:06 +0200975 /* Pack the output. */
976 LLVMValueRef vdata = NULL;
977
978 switch (num_comps) {
979 case 1: /* as i32 */
980 vdata = out[0];
981 break;
982 case 2: /* as v2i32 */
983 case 3: /* as v4i32 (aligned to 4) */
984 case 4: /* as v4i32 */
985 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
986 for (j = 0; j < num_comps; j++) {
987 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
988 LLVMConstInt(i32, j, 0), "");
989 }
990 break;
991 }
992
993 build_streamout_store(shader, shader->so_buffers[buf_idx],
994 vdata, num_comps,
995 so_write_offset[buf_idx],
996 LLVMConstInt(i32, 0, 0),
997 so->output[i].dst_offset*4);
998 }
999 }
1000 lp_build_endif(&if_ctx);
1001}
1002
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001003
Michel Dänzer404b29d2013-11-21 16:45:28 +09001004/* Generate export instructions for hardware VS shader stage */
1005static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1006 struct si_shader_output_values *outputs,
1007 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001008{
1009 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +02001010 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001011 struct lp_build_context * base = &bld_base->base;
1012 struct lp_build_context * uint =
1013 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001014 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001015 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001016 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
1017 unsigned semantic_name, semantic_index, semantic_usage;
1018 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001019 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001020 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001021 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001022
Michel Dänzer67e385b2014-01-08 17:48:21 +09001023 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1024 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001025 }
1026
Michel Dänzer404b29d2013-11-21 16:45:28 +09001027 for (i = 0; i < noutput; i++) {
1028 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001029 semantic_index = outputs[i].sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001030 semantic_usage = outputs[i].usage;
Tom Stellarda75c6162012-01-06 17:38:37 -05001031
Michel Dänzer0afeea52013-05-02 14:53:17 +02001032handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001033 /* Select the correct target */
1034 switch(semantic_name) {
1035 case TGSI_SEMANTIC_PSIZE:
1036 shader->vs_out_misc_write = true;
1037 shader->vs_out_point_size = true;
1038 psize_value = outputs[i].values[0];
1039 continue;
1040 case TGSI_SEMANTIC_EDGEFLAG:
1041 shader->vs_out_misc_write = true;
1042 shader->vs_out_edgeflag = true;
1043 edgeflag_value = outputs[i].values[0];
1044 continue;
1045 case TGSI_SEMANTIC_LAYER:
1046 shader->vs_out_misc_write = true;
1047 shader->vs_out_layer = true;
1048 layer_value = outputs[i].values[0];
1049 continue;
1050 case TGSI_SEMANTIC_POSITION:
1051 target = V_008DFC_SQ_EXP_POS;
1052 break;
1053 case TGSI_SEMANTIC_COLOR:
1054 case TGSI_SEMANTIC_BCOLOR:
1055 target = V_008DFC_SQ_EXP_PARAM + param_count;
1056 shader->output[i].param_offset = param_count;
1057 param_count++;
1058 break;
1059 case TGSI_SEMANTIC_CLIPDIST:
1060 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
1061 (1 << semantic_index)))
Marek Olšák053606d2013-11-19 22:07:30 +01001062 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001063 shader->clip_dist_write |=
1064 semantic_usage << (semantic_index << 2);
1065 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1066 break;
1067 case TGSI_SEMANTIC_CLIPVERTEX:
1068 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1069 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001070 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001071 case TGSI_SEMANTIC_FOG:
1072 case TGSI_SEMANTIC_GENERIC:
1073 target = V_008DFC_SQ_EXP_PARAM + param_count;
1074 shader->output[i].param_offset = param_count;
1075 param_count++;
1076 break;
1077 default:
1078 target = 0;
1079 fprintf(stderr,
1080 "Warning: SI unhandled vs output type:%d\n",
1081 semantic_name);
1082 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001083
Michel Dänzer404b29d2013-11-21 16:45:28 +09001084 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001085
Michel Dänzer404b29d2013-11-21 16:45:28 +09001086 if (target >= V_008DFC_SQ_EXP_POS &&
1087 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1088 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1089 args, sizeof(args));
1090 } else {
1091 lp_build_intrinsic(base->gallivm->builder,
1092 "llvm.SI.export",
1093 LLVMVoidTypeInContext(base->gallivm->context),
1094 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001095 }
1096
1097 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1098 semantic_name = TGSI_SEMANTIC_GENERIC;
1099 goto handle_semantic;
1100 }
1101 }
1102
1103 /* We need to add the position output manually if it's missing. */
1104 if (!pos_args[0][0]) {
1105 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1106 pos_args[0][1] = uint->zero; /* EXEC mask */
1107 pos_args[0][2] = uint->zero; /* last export? */
1108 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1109 pos_args[0][4] = uint->zero; /* COMPR flag */
1110 pos_args[0][5] = base->zero; /* X */
1111 pos_args[0][6] = base->zero; /* Y */
1112 pos_args[0][7] = base->zero; /* Z */
1113 pos_args[0][8] = base->one; /* W */
1114 }
1115
1116 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1117 if (shader->vs_out_misc_write) {
1118 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1119 shader->vs_out_point_size |
1120 (shader->vs_out_edgeflag << 1) |
1121 (shader->vs_out_layer << 2));
1122 pos_args[1][1] = uint->zero; /* EXEC mask */
1123 pos_args[1][2] = uint->zero; /* last export? */
1124 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1125 pos_args[1][4] = uint->zero; /* COMPR flag */
1126 pos_args[1][5] = base->zero; /* X */
1127 pos_args[1][6] = base->zero; /* Y */
1128 pos_args[1][7] = base->zero; /* Z */
1129 pos_args[1][8] = base->zero; /* W */
1130
Michel Dänzer404b29d2013-11-21 16:45:28 +09001131 if (shader->vs_out_point_size)
1132 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001133
1134 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001135 /* The output is a float, but the hw expects an integer
1136 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001137 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1138 edgeflag_value,
1139 bld_base->uint_bld.elem_type, "");
1140 edgeflag_value = lp_build_min(&bld_base->int_bld,
1141 edgeflag_value,
1142 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001143
1144 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001145 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1146 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001147 base->elem_type, "");
1148 }
1149
Michel Dänzer404b29d2013-11-21 16:45:28 +09001150 if (shader->vs_out_layer)
1151 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001152 }
1153
1154 for (i = 0; i < 4; i++)
1155 if (pos_args[i][0])
1156 shader->nr_pos_exports++;
1157
1158 pos_idx = 0;
1159 for (i = 0; i < 4; i++) {
1160 if (!pos_args[i][0])
1161 continue;
1162
1163 /* Specify the target we are exporting */
1164 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1165
1166 if (pos_idx == shader->nr_pos_exports)
1167 /* Specify that this is the last export */
1168 pos_args[i][2] = uint->one;
1169
1170 lp_build_intrinsic(base->gallivm->builder,
1171 "llvm.SI.export",
1172 LLVMVoidTypeInContext(base->gallivm->context),
1173 pos_args[i], 9);
1174 }
1175}
1176
Michel Dänzer404b29d2013-11-21 16:45:28 +09001177static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1178{
1179 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1180 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzere884c562014-01-15 15:24:14 +09001181 struct si_shader *es = &si_shader_ctx->shader->shader;
1182 struct si_shader *gs = si_shader_ctx->gs_for_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001183 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1184 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001185 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1186 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001187 LLVMValueRef t_list_ptr;
1188 LLVMValueRef t_list;
1189 unsigned chan;
1190 int i;
1191
1192 while (!tgsi_parse_end_of_tokens(parse)) {
1193 struct tgsi_full_declaration *d =
1194 &parse->FullToken.FullDeclaration;
1195
1196 tgsi_parse_token(parse);
1197
1198 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1199 continue;
1200
Michel Dänzere884c562014-01-15 15:24:14 +09001201 si_store_shader_io_attribs(es, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001202 }
1203
1204 /* Load the ESGS ring resource descriptor */
1205 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1206 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001207 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001208
Michel Dänzere884c562014-01-15 15:24:14 +09001209 for (i = 0; i < es->noutput; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001210 LLVMValueRef *out_ptr =
Michel Dänzere884c562014-01-15 15:24:14 +09001211 si_shader_ctx->radeon_bld.soa.outputs[es->output[i].index];
1212 int j;
1213
1214 for (j = 0; j < gs->ninput; j++) {
1215 if (gs->input[j].name == es->output[i].name &&
1216 gs->input[j].sid == es->output[i].sid)
1217 break;
1218 }
1219 if (j == gs->ninput)
1220 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001221
1222 for (chan = 0; chan < 4; chan++) {
1223 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001224 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1225
1226 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001227 LLVMGetUndef(i32), soffset,
1228 (4 * gs->input[j].param_offset + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001229 V_008F0C_BUF_DATA_FORMAT_32,
1230 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001231 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001232 }
1233 }
1234}
1235
1236static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1237{
1238 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1239 struct gallivm_state *gallivm = bld_base->base.gallivm;
1240 LLVMValueRef args[2];
1241
1242 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1243 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1244 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1245 LLVMVoidTypeInContext(gallivm->context), args, 2,
1246 LLVMNoUnwindAttribute);
1247}
1248
1249static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1250{
1251 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1252 struct gallivm_state *gallivm = bld_base->base.gallivm;
1253 struct si_pipe_shader *shader = si_shader_ctx->shader;
1254 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1255 struct si_shader_output_values *outputs = NULL;
1256 unsigned noutput = 0;
1257 int i;
1258
1259 while (!tgsi_parse_end_of_tokens(parse)) {
1260 struct tgsi_full_declaration *d =
1261 &parse->FullToken.FullDeclaration;
1262 unsigned index;
1263
1264 tgsi_parse_token(parse);
1265
1266 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1267 continue;
1268
1269 i = si_store_shader_io_attribs(&shader->shader, d);
1270 if (i < 0)
1271 continue;
1272
1273 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1274 (noutput + 1) * sizeof(outputs[0]));
1275 for (index = d->Range.First; index <= d->Range.Last; index++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001276 outputs[noutput].index = index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001277 outputs[noutput].name = d->Semantic.Name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001278 outputs[noutput].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001279 outputs[noutput].usage = d->Declaration.UsageMask;
1280
1281 for (i = 0; i < 4; i++)
1282 outputs[noutput].values[i] =
1283 LLVMBuildLoad(gallivm->builder,
1284 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1285 "");
1286 }
1287 noutput++;
1288 }
1289
1290 si_llvm_export_vs(bld_base, outputs, noutput);
1291 FREE(outputs);
1292}
1293
Michel Dänzer51f89a02013-12-09 15:33:53 +09001294static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1295{
1296 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
1297 struct si_shader * shader = &si_shader_ctx->shader->shader;
1298 struct lp_build_context * base = &bld_base->base;
1299 struct lp_build_context * uint =
1300 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1301 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1302 LLVMValueRef args[9];
1303 LLVMValueRef last_args[9] = { 0 };
1304 unsigned semantic_name;
1305 int depth_index = -1, stencil_index = -1;
1306 int i;
1307
1308 while (!tgsi_parse_end_of_tokens(parse)) {
1309 struct tgsi_full_declaration *d =
1310 &parse->FullToken.FullDeclaration;
1311 unsigned target;
1312 unsigned index;
1313
1314 tgsi_parse_token(parse);
1315
1316 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
1317 parse->FullToken.FullProperty.Property.PropertyName ==
1318 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
1319 shader->fs_write_all = TRUE;
1320
1321 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1322 continue;
1323
1324 i = si_store_shader_io_attribs(shader, d);
1325 if (i < 0)
1326 continue;
1327
1328 semantic_name = d->Semantic.Name;
1329 for (index = d->Range.First; index <= d->Range.Last; index++) {
1330 /* Select the correct target */
1331 switch(semantic_name) {
1332 case TGSI_SEMANTIC_POSITION:
1333 depth_index = index;
1334 continue;
1335 case TGSI_SEMANTIC_STENCIL:
1336 stencil_index = index;
1337 continue;
1338 case TGSI_SEMANTIC_COLOR:
1339 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1340 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001341 LLVMBuildStore(bld_base->base.gallivm->builder,
1342 bld_base->base.one,
1343 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1344
Michel Dänzer51f89a02013-12-09 15:33:53 +09001345 if (d->Semantic.Index == 0 &&
1346 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001347 si_alpha_test(bld_base,
1348 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001349 break;
1350 default:
1351 target = 0;
1352 fprintf(stderr,
1353 "Warning: SI unhandled fs output type:%d\n",
1354 semantic_name);
1355 }
1356
Michel Dänzer404b29d2013-11-21 16:45:28 +09001357 si_llvm_init_export_args_load(bld_base,
1358 si_shader_ctx->radeon_bld.soa.outputs[index],
1359 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001360
1361 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001362 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001363 if (last_args[0]) {
1364 lp_build_intrinsic(base->gallivm->builder,
1365 "llvm.SI.export",
1366 LLVMVoidTypeInContext(base->gallivm->context),
1367 last_args, 9);
1368 }
1369
Marek Olšák0eb528a2013-12-04 13:24:22 +01001370 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001371 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001372
1373 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1374 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1375 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1376 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001377 si_llvm_init_export_args_load(bld_base,
1378 si_shader_ctx->radeon_bld.soa.outputs[index],
1379 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001380 lp_build_intrinsic(base->gallivm->builder,
1381 "llvm.SI.export",
1382 LLVMVoidTypeInContext(base->gallivm->context),
1383 args, 9);
1384 }
1385 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001386 } else {
1387 lp_build_intrinsic(base->gallivm->builder,
1388 "llvm.SI.export",
1389 LLVMVoidTypeInContext(base->gallivm->context),
1390 args, 9);
1391 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001392 }
1393 }
1394
Michel Dänzer1a616c12012-11-13 17:35:09 +01001395 if (depth_index >= 0 || stencil_index >= 0) {
1396 LLVMValueRef out_ptr;
1397 unsigned mask = 0;
1398
1399 /* Specify the target we are exporting */
1400 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1401
1402 if (depth_index >= 0) {
1403 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1404 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1405 mask |= 0x1;
1406
1407 if (stencil_index < 0) {
1408 args[6] =
1409 args[7] =
1410 args[8] = args[5];
1411 }
1412 }
1413
1414 if (stencil_index >= 0) {
1415 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
1416 args[7] =
1417 args[8] =
1418 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001419 /* Only setting the stencil component bit (0x2) here
1420 * breaks some stencil piglit tests
1421 */
1422 mask |= 0x3;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001423
1424 if (depth_index < 0)
1425 args[5] = args[6];
1426 }
1427
1428 /* Specify which components to enable */
1429 args[0] = lp_build_const_int32(base->gallivm, mask);
1430
1431 args[1] =
1432 args[2] =
1433 args[4] = uint->zero;
1434
1435 if (last_args[0])
1436 lp_build_intrinsic(base->gallivm->builder,
1437 "llvm.SI.export",
1438 LLVMVoidTypeInContext(base->gallivm->context),
1439 args, 9);
1440 else
1441 memcpy(last_args, args, sizeof(args));
1442 }
1443
Michel Dänzer51f89a02013-12-09 15:33:53 +09001444 if (!last_args[0]) {
1445 /* Specify which components to enable */
1446 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001447
Michel Dänzer51f89a02013-12-09 15:33:53 +09001448 /* Specify the target we are exporting */
1449 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001450
Michel Dänzer51f89a02013-12-09 15:33:53 +09001451 /* Set COMPR flag to zero to export data as 32-bit */
1452 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001453
Michel Dänzer51f89a02013-12-09 15:33:53 +09001454 /* dummy bits */
1455 last_args[5]= uint->zero;
1456 last_args[6]= uint->zero;
1457 last_args[7]= uint->zero;
1458 last_args[8]= uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001459
Michel Dänzer51f89a02013-12-09 15:33:53 +09001460 si_shader_ctx->shader->spi_shader_col_format |=
1461 V_028714_SPI_SHADER_32_ABGR;
1462 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001463 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001464
1465 /* Specify whether the EXEC mask represents the valid mask */
1466 last_args[1] = uint->one;
1467
1468 /* Specify that this is the last export */
1469 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1470
1471 lp_build_intrinsic(base->gallivm->builder,
1472 "llvm.SI.export",
1473 LLVMVoidTypeInContext(base->gallivm->context),
1474 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001475}
1476
Marek Olšák4855acd2013-08-06 15:08:54 +02001477static const struct lp_build_tgsi_action txf_action;
1478
1479static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1480 struct lp_build_tgsi_context * bld_base,
1481 struct lp_build_emit_data * emit_data);
1482
Tom Stellarda75c6162012-01-06 17:38:37 -05001483static void tex_fetch_args(
1484 struct lp_build_tgsi_context * bld_base,
1485 struct lp_build_emit_data * emit_data)
1486{
Christian König55fe5cc2013-03-04 16:30:06 +01001487 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001488 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001489 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001490 unsigned opcode = inst->Instruction.Opcode;
1491 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001492 LLVMValueRef coords[4];
1493 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001494 int ref_pos;
1495 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001496 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001497 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001498 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1499 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1500
1501 if (target == TGSI_TEXTURE_BUFFER) {
1502 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1503 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1504 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1505 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1506
1507 /* Truncate v32i8 to v16i8. */
1508 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1509 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1510 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1511 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1512
1513 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1514 emit_data->args[0] = res;
1515 emit_data->args[1] = bld_base->uint_bld.zero;
1516 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1517 emit_data->arg_count = 3;
1518 return;
1519 }
Tom Stellard467f5162012-05-16 15:15:35 -04001520
Michel Dänzer120efee2013-01-25 12:10:11 +01001521 /* Fetch and project texture coordinates */
1522 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001523 for (chan = 0; chan < 3; chan++ ) {
1524 coords[chan] = lp_build_emit_fetch(bld_base,
1525 emit_data->inst, 0,
1526 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001527 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001528 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1529 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001530 coords[chan],
1531 coords[3]);
1532 }
1533
Michel Dänzer120efee2013-01-25 12:10:11 +01001534 if (opcode == TGSI_OPCODE_TXP)
1535 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001536
Michel Dänzer120efee2013-01-25 12:10:11 +01001537 /* Pack LOD bias value */
1538 if (opcode == TGSI_OPCODE_TXB)
1539 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001540
Michel Dänzer0495adb2013-05-06 12:45:14 +02001541 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001542 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001543
1544 /* Pack depth comparison value */
1545 switch (target) {
1546 case TGSI_TEXTURE_SHADOW1D:
1547 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1548 case TGSI_TEXTURE_SHADOW2D:
1549 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001550 case TGSI_TEXTURE_SHADOWCUBE:
1551 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001552 assert(ref_pos >= 0);
1553 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001554 break;
1555 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1556 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001557 }
1558
Michel Dänzera6b83c02013-02-21 16:10:55 +01001559 /* Pack user derivatives */
1560 if (opcode == TGSI_OPCODE_TXD) {
1561 for (chan = 0; chan < 2; chan++) {
1562 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1563 if (num_coords > 1)
1564 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1565 }
1566 }
1567
Michel Dänzer120efee2013-01-25 12:10:11 +01001568 /* Pack texture coordinates */
1569 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001570 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001571 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001572 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001573 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001574
Marek Olšákd2bd6342013-09-18 15:40:21 +02001575 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001576 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001577 address[count++] = coords[3];
1578
1579 if (count > 16) {
1580 assert(!"Cannot handle more than 16 texture address parameters");
1581 count = 16;
1582 }
1583
1584 for (chan = 0; chan < count; chan++ ) {
1585 address[chan] = LLVMBuildBitCast(gallivm->builder,
1586 address[chan],
1587 LLVMInt32TypeInContext(gallivm->context),
1588 "");
1589 }
1590
Marek Olšák4855acd2013-08-06 15:08:54 +02001591 /* Adjust the sample index according to FMASK.
1592 *
1593 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1594 * which is the identity mapping. Each nibble says which physical sample
1595 * should be fetched to get that sample.
1596 *
1597 * For example, 0x11111100 means there are only 2 samples stored and
1598 * the second sample covers 3/4 of the pixel. When reading samples 0
1599 * and 1, return physical sample 0 (determined by the first two 0s
1600 * in FMASK), otherwise return physical sample 1.
1601 *
1602 * The sample index should be adjusted as follows:
1603 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1604 */
1605 if (target == TGSI_TEXTURE_2D_MSAA ||
1606 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1607 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1608 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001609 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001610 unsigned txf_count = count;
1611
Marek Olšákd2bd6342013-09-18 15:40:21 +02001612 memcpy(txf_address, address, sizeof(txf_address));
1613
1614 if (target == TGSI_TEXTURE_2D_MSAA) {
1615 txf_address[2] = bld_base->uint_bld.zero;
1616 }
1617 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001618
1619 /* Pad to a power-of-two size. */
1620 while (txf_count < util_next_power_of_two(txf_count))
1621 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1622
1623 /* Read FMASK using TXF. */
1624 txf_emit_data.chan = 0;
1625 txf_emit_data.dst_type = LLVMVectorType(
1626 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1627 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1628 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
Marek Olšákd2bd6342013-09-18 15:40:21 +02001629 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1630 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
Marek Olšák4855acd2013-08-06 15:08:54 +02001631 txf_emit_data.arg_count = 3;
1632
1633 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1634
1635 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001636 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1637 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1638
1639 /* Apply the formula. */
1640 LLVMValueRef fmask =
1641 LLVMBuildExtractElement(gallivm->builder,
1642 txf_emit_data.output[0],
1643 uint_bld->zero, "");
1644
Marek Olšákd2bd6342013-09-18 15:40:21 +02001645 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001646
1647 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001648 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001649
1650 LLVMValueRef shifted_fmask =
1651 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1652
1653 LLVMValueRef final_sample =
1654 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1655
1656 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1657 * resource descriptor is 0 (invalid),
1658 */
1659 LLVMValueRef fmask_desc =
1660 LLVMBuildBitCast(gallivm->builder,
1661 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1662 LLVMVectorType(uint_bld->elem_type, 8), "");
1663
1664 LLVMValueRef fmask_word1 =
1665 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1666 uint_bld->one, "");
1667
1668 LLVMValueRef word1_is_nonzero =
1669 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1670 fmask_word1, uint_bld->zero, "");
1671
Marek Olšákd2bd6342013-09-18 15:40:21 +02001672 /* Replace the MSAA sample index. */
1673 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001674 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001675 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001676 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001677
Michel Dänzer36231112013-05-02 09:44:45 +02001678 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001679 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001680
1681 if (opcode == TGSI_OPCODE_TXF) {
1682 /* add tex offsets */
1683 if (inst->Texture.NumOffsets) {
1684 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1685 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1686 const struct tgsi_texture_offset * off = inst->TexOffsets;
1687
1688 assert(inst->Texture.NumOffsets == 1);
1689
Marek Olšákdefedc02013-09-18 15:36:38 +02001690 switch (target) {
1691 case TGSI_TEXTURE_3D:
1692 address[2] = lp_build_add(uint_bld, address[2],
1693 bld->immediates[off->Index][off->SwizzleZ]);
1694 /* fall through */
1695 case TGSI_TEXTURE_2D:
1696 case TGSI_TEXTURE_SHADOW2D:
1697 case TGSI_TEXTURE_RECT:
1698 case TGSI_TEXTURE_SHADOWRECT:
1699 case TGSI_TEXTURE_2D_ARRAY:
1700 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001701 address[1] =
1702 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001703 bld->immediates[off->Index][off->SwizzleY]);
1704 /* fall through */
1705 case TGSI_TEXTURE_1D:
1706 case TGSI_TEXTURE_SHADOW1D:
1707 case TGSI_TEXTURE_1D_ARRAY:
1708 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1709 address[0] =
1710 lp_build_add(uint_bld, address[0],
1711 bld->immediates[off->Index][off->SwizzleX]);
1712 break;
1713 /* texture offsets do not apply to other texture targets */
1714 }
Michel Dänzer36231112013-05-02 09:44:45 +02001715 }
1716
1717 emit_data->dst_type = LLVMVectorType(
1718 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1719 4);
1720
1721 emit_data->arg_count = 3;
1722 } else {
1723 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001724 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001725
1726 emit_data->dst_type = LLVMVectorType(
1727 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1728 4);
1729
1730 emit_data->arg_count = 4;
1731 }
1732
1733 /* Dimensions */
1734 emit_data->args[emit_data->arg_count - 1] =
1735 lp_build_const_int32(bld_base->base.gallivm, target);
1736
Michel Dänzer120efee2013-01-25 12:10:11 +01001737 /* Pad to power of two vector */
1738 while (count < util_next_power_of_two(count))
1739 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1740
Christian Königccf3e8f2013-03-26 15:09:27 +01001741 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001742}
1743
Michel Dänzer07eddc42013-02-06 15:43:10 +01001744static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1745 struct lp_build_tgsi_context * bld_base,
1746 struct lp_build_emit_data * emit_data)
1747{
1748 struct lp_build_context * base = &bld_base->base;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001749 char intr_name[127];
Michel Dänzer07eddc42013-02-06 15:43:10 +01001750
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001751 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1752 emit_data->output[emit_data->chan] = build_intrinsic(
1753 base->gallivm->builder,
1754 "llvm.SI.vs.load.input", emit_data->dst_type,
1755 emit_data->args, emit_data->arg_count,
1756 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1757 return;
1758 }
1759
Michel Dänzer07eddc42013-02-06 15:43:10 +01001760 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001761 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001762
Christian König44e32242013-03-20 12:10:35 +01001763 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001764 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001765 emit_data->args, emit_data->arg_count,
1766 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001767}
1768
Michel Dänzer0495adb2013-05-06 12:45:14 +02001769static void txq_fetch_args(
1770 struct lp_build_tgsi_context * bld_base,
1771 struct lp_build_emit_data * emit_data)
1772{
1773 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1774 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001775 struct gallivm_state *gallivm = bld_base->base.gallivm;
1776
1777 if (inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1778 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1779 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1780
1781 /* Read the size from the buffer descriptor directly. */
1782 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1783 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1784 size = LLVMBuildExtractElement(gallivm->builder, size,
1785 lp_build_const_int32(gallivm, 2), "");
1786 emit_data->args[0] = size;
1787 return;
1788 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02001789
1790 /* Mip level */
1791 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1792
1793 /* Resource */
1794 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1795
1796 /* Dimensions */
1797 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1798 inst->Texture.Texture);
1799
1800 emit_data->arg_count = 3;
1801
1802 emit_data->dst_type = LLVMVectorType(
1803 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1804 4);
1805}
1806
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001807static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
1808 struct lp_build_tgsi_context * bld_base,
1809 struct lp_build_emit_data * emit_data)
1810{
1811 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1812 /* Just return the buffer size. */
1813 emit_data->output[emit_data->chan] = emit_data->args[0];
1814 return;
1815 }
1816
1817 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
1818}
1819
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001820#if HAVE_LLVM >= 0x0304
1821
1822static void si_llvm_emit_ddxy(
1823 const struct lp_build_tgsi_action * action,
1824 struct lp_build_tgsi_context * bld_base,
1825 struct lp_build_emit_data * emit_data)
1826{
1827 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1828 struct gallivm_state *gallivm = bld_base->base.gallivm;
1829 struct lp_build_context * base = &bld_base->base;
1830 const struct tgsi_full_instruction *inst = emit_data->inst;
1831 unsigned opcode = inst->Instruction.Opcode;
1832 LLVMValueRef indices[2];
1833 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1834 LLVMValueRef tl, trbl, result[4];
1835 LLVMTypeRef i32;
1836 unsigned swizzle[4];
1837 unsigned c;
1838
1839 i32 = LLVMInt32TypeInContext(gallivm->context);
1840
1841 indices[0] = bld_base->uint_bld.zero;
1842 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1843 NULL, 0, LLVMReadNoneAttribute);
1844 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1845 indices, 2, "");
1846
1847 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1848 lp_build_const_int32(gallivm, 0xfffffffc), "");
1849 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1850 indices, 2, "");
1851
1852 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1853 lp_build_const_int32(gallivm,
1854 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1855 "");
1856 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1857 indices, 2, "");
1858
1859 for (c = 0; c < 4; ++c) {
1860 unsigned i;
1861
1862 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1863 for (i = 0; i < c; ++i) {
1864 if (swizzle[i] == swizzle[c]) {
1865 result[c] = result[i];
1866 break;
1867 }
1868 }
1869 if (i != c)
1870 continue;
1871
1872 LLVMBuildStore(gallivm->builder,
1873 LLVMBuildBitCast(gallivm->builder,
1874 lp_build_emit_fetch(bld_base, inst, 0, c),
1875 i32, ""),
1876 store_ptr);
1877
1878 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1879 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1880
1881 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1882 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1883
1884 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1885 }
1886
1887 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1888}
1889
1890#endif /* HAVE_LLVM >= 0x0304 */
1891
Michel Dänzer404b29d2013-11-21 16:45:28 +09001892/* Emit one vertex from the geometry shader */
1893static void si_llvm_emit_vertex(
1894 const struct lp_build_tgsi_action *action,
1895 struct lp_build_tgsi_context *bld_base,
1896 struct lp_build_emit_data *emit_data)
1897{
1898 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001899 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001900 struct si_shader *shader = &si_shader_ctx->shader->shader;
1901 struct gallivm_state *gallivm = bld_base->base.gallivm;
1902 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001903 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1904 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001905 LLVMValueRef gs_next_vertex;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001906 LLVMValueRef t_list_ptr;
1907 LLVMValueRef t_list;
1908 LLVMValueRef args[2];
1909 unsigned chan;
1910 int i;
1911
1912 /* Load the GSVS ring resource descriptor */
1913 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1914 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001915 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001916
1917 if (shader->noutput == 0) {
1918 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1919
1920 while (!tgsi_parse_end_of_tokens(parse)) {
1921 tgsi_parse_token(parse);
1922
Michel Dänzer7c7d7382014-01-09 15:33:34 +09001923 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
1924 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
1925
1926 if (d->Declaration.File == TGSI_FILE_OUTPUT)
1927 si_store_shader_io_attribs(shader, d);
1928 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001929 }
1930 }
1931
1932 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001933 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001934 for (i = 0; i < shader->noutput; i++) {
1935 LLVMValueRef *out_ptr =
1936 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
1937
1938 for (chan = 0; chan < 4; chan++) {
1939 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001940 LLVMValueRef voffset =
1941 lp_build_const_int32(gallivm, (i * 4 + chan) *
1942 shader->gs_max_out_vertices);
1943
1944 voffset = lp_build_add(uint, voffset, gs_next_vertex);
1945 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001946
1947 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1948
1949 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
1950 voffset, soffset, 0,
1951 V_008F0C_BUF_DATA_FORMAT_32,
1952 V_008F0C_BUF_NUM_FORMAT_UINT,
1953 1, 0, 1, 1, 0);
1954 }
1955 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001956 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
1957 lp_build_const_int32(gallivm, 1));
1958 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001959
1960 /* Signal vertex emission */
1961 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
1962 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1963 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1964 LLVMVoidTypeInContext(gallivm->context), args, 2,
1965 LLVMNoUnwindAttribute);
1966}
1967
1968/* Cut one primitive from the geometry shader */
1969static void si_llvm_emit_primitive(
1970 const struct lp_build_tgsi_action *action,
1971 struct lp_build_tgsi_context *bld_base,
1972 struct lp_build_emit_data *emit_data)
1973{
1974 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1975 struct gallivm_state *gallivm = bld_base->base.gallivm;
1976 LLVMValueRef args[2];
1977
1978 /* Signal primitive cut */
1979 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
1980 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1981 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1982 LLVMVoidTypeInContext(gallivm->context), args, 2,
1983 LLVMNoUnwindAttribute);
1984}
1985
Tom Stellarda75c6162012-01-06 17:38:37 -05001986static const struct lp_build_tgsi_action tex_action = {
1987 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001988 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001989 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001990};
1991
Michel Dänzer3e205132012-11-06 17:39:01 +01001992static const struct lp_build_tgsi_action txb_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.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001996};
1997
Michel Dänzera6b83c02013-02-21 16:10:55 +01001998#if HAVE_LLVM >= 0x0304
1999static const struct lp_build_tgsi_action txd_action = {
2000 .fetch_args = tex_fetch_args,
2001 .emit = build_tex_intrinsic,
2002 .intr_name = "llvm.SI.sampled."
2003};
2004#endif
2005
Michel Dänzer36231112013-05-02 09:44:45 +02002006static const struct lp_build_tgsi_action txf_action = {
2007 .fetch_args = tex_fetch_args,
2008 .emit = build_tex_intrinsic,
2009 .intr_name = "llvm.SI.imageload."
2010};
2011
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002012static const struct lp_build_tgsi_action txl_action = {
2013 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002014 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002015 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002016};
2017
Michel Dänzer0495adb2013-05-06 12:45:14 +02002018static const struct lp_build_tgsi_action txq_action = {
2019 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002020 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002021 .intr_name = "llvm.SI.resinfo"
2022};
2023
Christian König206f0592013-03-20 14:37:21 +01002024static void create_meta_data(struct si_shader_context *si_shader_ctx)
2025{
2026 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2027 LLVMValueRef args[3];
2028
2029 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2030 args[1] = 0;
2031 args[2] = lp_build_const_int32(gallivm, 1);
2032
2033 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2034}
2035
Christian König55fe5cc2013-03-04 16:30:06 +01002036static void create_function(struct si_shader_context *si_shader_ctx)
2037{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002038 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2039 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002040 struct si_pipe_shader *shader = si_shader_ctx->shader;
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002041 LLVMTypeRef params[21], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002042 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002043
Christian König55fe5cc2013-03-04 16:30:06 +01002044 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002045 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002046 f32 = LLVMFloatTypeInContext(gallivm->context);
2047 v2i32 = LLVMVectorType(i32, 2);
2048 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01002049
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002050 params[SI_PARAM_CONST] = LLVMPointerType(
2051 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
2052 /* We assume at most 16 textures per program at the moment.
2053 * This need probably need to be changed to support bindless textures */
2054 params[SI_PARAM_SAMPLER] = LLVMPointerType(
2055 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
2056 params[SI_PARAM_RESOURCE] = LLVMPointerType(
2057 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002058
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002059 switch (si_shader_ctx->type) {
2060 case TGSI_PROCESSOR_VERTEX:
2061 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Marek Olšák2993cca2013-08-18 02:34:23 +02002062 params[SI_PARAM_SO_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01002063 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002064 num_params = SI_PARAM_START_INSTANCE+1;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002065 if (shader->key.vs.as_es) {
2066 params[SI_PARAM_ES2GS_OFFSET] = i32;
2067 num_params++;
2068 } else {
2069 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002070
Michel Dänzer404b29d2013-11-21 16:45:28 +09002071 /* Streamout SGPRs. */
2072 if (shader->selector->so.num_outputs) {
2073 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2074 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2075 }
2076 /* A streamout buffer offset is loaded if the stride is non-zero. */
2077 for (i = 0; i < 4; i++) {
2078 if (!shader->selector->so.stride[i])
2079 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002080
Michel Dänzer404b29d2013-11-21 16:45:28 +09002081 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2082 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002083 }
2084
2085 last_sgpr = num_params-1;
2086
2087 /* VGPRs */
2088 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2089 params[num_params++] = i32; /* unused*/
2090 params[num_params++] = i32; /* unused */
2091 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002092 break;
Christian König0666ffd2013-03-05 15:07:39 +01002093
Michel Dänzer404b29d2013-11-21 16:45:28 +09002094 case TGSI_PROCESSOR_GEOMETRY:
2095 params[SI_PARAM_GS2VS_OFFSET] = i32;
2096 params[SI_PARAM_GS_WAVE_ID] = i32;
2097 last_sgpr = SI_PARAM_GS_WAVE_ID;
2098
2099 /* VGPRs */
2100 params[SI_PARAM_VTX0_OFFSET] = i32;
2101 params[SI_PARAM_VTX1_OFFSET] = i32;
2102 params[SI_PARAM_PRIMITIVE_ID] = i32;
2103 params[SI_PARAM_VTX2_OFFSET] = i32;
2104 params[SI_PARAM_VTX3_OFFSET] = i32;
2105 params[SI_PARAM_VTX4_OFFSET] = i32;
2106 params[SI_PARAM_VTX5_OFFSET] = i32;
2107 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2108 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2109 break;
2110
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002111 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002112 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002113 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002114 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002115 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2116 params[SI_PARAM_PERSP_CENTER] = v2i32;
2117 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2118 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2119 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2120 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2121 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2122 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2123 params[SI_PARAM_POS_X_FLOAT] = f32;
2124 params[SI_PARAM_POS_Y_FLOAT] = f32;
2125 params[SI_PARAM_POS_Z_FLOAT] = f32;
2126 params[SI_PARAM_POS_W_FLOAT] = f32;
2127 params[SI_PARAM_FRONT_FACE] = f32;
2128 params[SI_PARAM_ANCILLARY] = f32;
2129 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2130 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002131 num_params = SI_PARAM_POS_FIXED_PT+1;
2132 break;
2133
2134 default:
2135 assert(0 && "unimplemented shader");
2136 return;
Christian Königc4973212013-03-05 12:14:02 +01002137 }
Christian König55fe5cc2013-03-04 16:30:06 +01002138
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002139 assert(num_params <= Elements(params));
2140 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002141 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002142
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002143 for (i = 0; i <= last_sgpr; ++i) {
2144 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002145 switch (i) {
2146 default:
2147 LLVMAddAttribute(P, LLVMInRegAttribute);
2148 break;
2149#if HAVE_LLVM >= 0x0304
2150 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2151 * to move load. Inputs are constant so this is fine. */
2152 case SI_PARAM_CONST:
2153 case SI_PARAM_SAMPLER:
2154 case SI_PARAM_RESOURCE:
2155 LLVMAddAttribute(P, LLVMByValAttribute);
2156 break;
2157#endif
2158 }
Christian Königcf9b31f2013-03-21 18:30:23 +01002159 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002160
2161#if HAVE_LLVM >= 0x0304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002162 if (bld_base->info &&
2163 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2164 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002165 si_shader_ctx->ddxy_lds =
2166 LLVMAddGlobalInAddressSpace(gallivm->module,
2167 LLVMArrayType(i32, 64),
2168 "ddxy_lds",
2169 LOCAL_ADDR_SPACE);
2170#endif
Christian König55fe5cc2013-03-04 16:30:06 +01002171}
Tom Stellarda75c6162012-01-06 17:38:37 -05002172
Christian König0f6cf2b2013-03-15 15:53:25 +01002173static void preload_constants(struct si_shader_context *si_shader_ctx)
2174{
2175 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2176 struct gallivm_state * gallivm = bld_base->base.gallivm;
2177 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002178 unsigned buf;
2179 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002180
Marek Olšák2fd42002013-10-25 11:45:47 +02002181 for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) {
2182 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002183
Marek Olšák2fd42002013-10-25 11:45:47 +02002184 if (num_const == 0)
2185 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002186
Marek Olšák2fd42002013-10-25 11:45:47 +02002187 /* Allocate space for the constant values */
2188 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002189
Marek Olšák2fd42002013-10-25 11:45:47 +02002190 /* Load the resource descriptor */
2191 si_shader_ctx->const_resource[buf] =
2192 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002193
Marek Olšák2fd42002013-10-25 11:45:47 +02002194 /* Load the constants, we rely on the code sinking to do the rest */
2195 for (i = 0; i < num_const * 4; ++i) {
2196 LLVMValueRef args[2] = {
2197 si_shader_ctx->const_resource[buf],
2198 lp_build_const_int32(gallivm, i * 4)
2199 };
2200 si_shader_ctx->constants[buf][i] =
2201 build_intrinsic(gallivm->builder, "llvm.SI.load.const",
2202 bld_base->base.elem_type, args, 2,
2203 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2204 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002205 }
2206}
2207
Christian König1c100182013-03-17 16:02:42 +01002208static void preload_samplers(struct si_shader_context *si_shader_ctx)
2209{
2210 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2211 struct gallivm_state * gallivm = bld_base->base.gallivm;
2212 const struct tgsi_shader_info * info = bld_base->info;
2213
2214 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2215
2216 LLVMValueRef res_ptr, samp_ptr;
2217 LLVMValueRef offset;
2218
2219 if (num_samplers == 0)
2220 return;
2221
2222 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02002223 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002224 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2225
2226 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2227 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2228
2229 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2230 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002231 /* Resource */
2232 offset = lp_build_const_int32(gallivm, i);
2233 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2234
2235 /* Sampler */
2236 offset = lp_build_const_int32(gallivm, i);
2237 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002238
2239 /* FMASK resource */
2240 if (info->is_msaa_sampler[i]) {
2241 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
2242 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
2243 build_indexed_load(si_shader_ctx, res_ptr, offset);
2244 }
Christian König1c100182013-03-17 16:02:42 +01002245 }
2246}
2247
Marek Olšák8d03d922013-09-01 23:59:06 +02002248static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2249{
2250 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2251 struct gallivm_state * gallivm = bld_base->base.gallivm;
2252 unsigned i;
2253
Michel Dänzer67e385b2014-01-08 17:48:21 +09002254 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2255 si_shader_ctx->shader->key.vs.as_es ||
2256 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002257 return;
2258
2259 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2260 SI_PARAM_SO_BUFFER);
2261
2262 /* Load the resources, we rely on the code sinking to do the rest */
2263 for (i = 0; i < 4; ++i) {
2264 if (si_shader_ctx->shader->selector->so.stride[i]) {
2265 LLVMValueRef offset = lp_build_const_int32(gallivm, i);
2266
2267 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2268 }
2269 }
2270}
2271
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002272int si_compile_llvm(struct si_context *sctx, struct si_pipe_shader *shader,
Tom Stellard302f53d2012-10-25 13:50:10 -04002273 LLVMModuleRef mod)
2274{
Tom Stellard302f53d2012-10-25 13:50:10 -04002275 unsigned i;
2276 uint32_t *ptr;
Tom Stellard7782d192013-04-04 09:57:13 -07002277 struct radeon_llvm_binary binary;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002278 bool dump = r600_can_dump_shader(&sctx->screen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002279 shader->selector ? shader->selector->tokens : NULL);
Tom Stellard7782d192013-04-04 09:57:13 -07002280 memset(&binary, 0, sizeof(binary));
2281 radeon_llvm_compile(mod, &binary,
Marek Olšák65dc5882014-01-22 02:42:20 +01002282 r600_get_llvm_processor_name(sctx->screen->b.family), dump);
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002283 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002284 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002285 for (i = 0; i < binary.code_size; i+=4 ) {
2286 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2287 binary.code[i + 2], binary.code[i + 1],
2288 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002289 }
2290 }
2291
Tom Stellardd50343d2013-04-04 16:21:06 -04002292 /* XXX: We may be able to emit some of these values directly rather than
2293 * extracting fields to be emitted later.
2294 */
2295 for (i = 0; i < binary.config_size; i+= 8) {
2296 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2297 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2298 switch (reg) {
2299 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2300 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2301 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2302 case R_00B848_COMPUTE_PGM_RSRC1:
2303 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2304 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2305 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002306 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2307 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2308 break;
2309 case R_00B84C_COMPUTE_PGM_RSRC2:
2310 shader->lds_size = G_00B84C_LDS_SIZE(value);
2311 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002312 case R_0286CC_SPI_PS_INPUT_ENA:
2313 shader->spi_ps_input_ena = value;
2314 break;
2315 default:
2316 fprintf(stderr, "Warning: Compiler emitted unknown "
2317 "config register: 0x%x\n", reg);
2318 break;
2319 }
2320 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002321
2322 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02002323 r600_resource_reference(&shader->bo, NULL);
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002324 shader->bo = si_resource_create_custom(sctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04002325 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002326 if (shader->bo == NULL) {
2327 return -ENOMEM;
2328 }
2329
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002330 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 +01002331 if (0 /*SI_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04002332 for (i = 0; i < binary.code_size / 4; ++i) {
2333 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04002334 }
2335 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04002336 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002337 }
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002338 sctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002339
Tom Stellard7782d192013-04-04 09:57:13 -07002340 free(binary.code);
2341 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04002342
2343 return 0;
2344}
2345
Michel Dänzer404b29d2013-11-21 16:45:28 +09002346/* Generate code for the hardware VS shader stage to go with a geometry shader */
2347static int si_generate_gs_copy_shader(struct si_context *sctx,
2348 struct si_shader_context *si_shader_ctx,
2349 bool dump)
2350{
2351 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2352 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2353 struct lp_build_context *base = &bld_base->base;
2354 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002355 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002356 struct si_shader *gs = &si_shader_ctx->shader->selector->current->shader;
2357 struct si_shader_output_values *outputs;
2358 LLVMValueRef t_list_ptr, t_list;
2359 LLVMValueRef args[9];
2360 int i, r;
2361
2362 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2363
2364 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
2365 si_shader_ctx->gs_for_vs = gs;
2366
2367 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2368
2369 create_meta_data(si_shader_ctx);
2370 create_function(si_shader_ctx);
2371 preload_streamout_buffers(si_shader_ctx);
2372
2373 /* Load the GSVS ring resource descriptor */
2374 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
2375 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002376 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002377
2378 args[0] = t_list;
2379 args[1] = lp_build_mul_imm(uint,
2380 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2381 si_shader_ctx->param_vertex_id),
2382 4);
2383 args[3] = uint->zero;
2384 args[4] = uint->one; /* OFFEN */
2385 args[5] = uint->zero; /* IDXEN */
2386 args[6] = uint->one; /* GLC */
2387 args[7] = uint->one; /* SLC */
2388 args[8] = uint->zero; /* TFE */
2389
2390 /* Fetch vertex data from GSVS ring */
2391 for (i = 0; i < gs->noutput; ++i) {
2392 struct si_shader_output *out = gs->output + i;
2393 unsigned chan;
2394
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002395 shader->output[i] = *out;
2396
Michel Dänzer404b29d2013-11-21 16:45:28 +09002397 outputs[i].name = out->name;
2398 outputs[i].index = out->index;
Michel Dänzer67e385b2014-01-08 17:48:21 +09002399 outputs[i].sid = out->sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002400 outputs[i].usage = out->usage;
2401
2402 for (chan = 0; chan < 4; chan++) {
2403 args[2] = lp_build_const_int32(gallivm,
2404 (i * 4 + chan) *
2405 gs->gs_max_out_vertices * 16 * 4);
2406
2407 outputs[i].values[chan] =
2408 LLVMBuildBitCast(gallivm->builder,
2409 build_intrinsic(gallivm->builder,
2410 "llvm.SI.buffer.load.dword.i32.i32",
2411 LLVMInt32TypeInContext(gallivm->context),
2412 args, 9,
2413 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2414 base->elem_type, "");
2415 }
2416 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002417 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002418
2419 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2420
2421 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2422
2423 if (dump)
2424 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2425
2426 r = si_compile_llvm(sctx, si_shader_ctx->shader,
2427 bld_base->base.gallivm->module);
2428
2429 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2430
2431 FREE(outputs);
2432 return r;
2433}
2434
Tom Stellarda75c6162012-01-06 17:38:37 -05002435int si_pipe_shader_create(
2436 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01002437 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002438{
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002439 struct si_context *sctx = (struct si_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002440 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002441 struct si_shader_context si_shader_ctx;
2442 struct tgsi_shader_info shader_info;
2443 struct lp_build_tgsi_context * bld_base;
2444 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002445 int r = 0;
Michel Dänzere1df0d42014-01-15 12:31:07 +09002446 bool dump = r600_can_dump_shader(&sctx->screen->b, sel->tokens);
2447
2448 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2449 * conversion fails. */
2450 if (dump) {
2451 tgsi_dump(sel->tokens, 0);
2452 si_dump_streamout(&sel->so);
2453 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002454
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002455 assert(shader->shader.noutput == 0);
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002456 assert(shader->shader.nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002457 assert(shader->shader.ninput == 0);
2458
Michel Dänzercfebaf92012-08-31 19:04:08 +02002459 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002460 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2461 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2462
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002463 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002464
Michel Dänzere44dfd42012-11-07 17:33:08 +01002465 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01002466 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002467 bld_base->info = &shader_info;
2468 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002469
2470 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01002471 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01002472#if HAVE_LLVM >= 0x0304
2473 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
2474#endif
Michel Dänzer36231112013-05-02 09:44:45 +02002475 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002476 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002477 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002478 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002479
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002480#if HAVE_LLVM >= 0x0304
2481 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2482 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
2483#endif
2484
Michel Dänzer404b29d2013-11-21 16:45:28 +09002485 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2486 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2487
Christian Könige4ed5872013-03-21 18:02:52 +01002488 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002489 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002490 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2491 si_shader_ctx.shader = shader;
2492 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002493
Michel Dänzer51f89a02013-12-09 15:33:53 +09002494 switch (si_shader_ctx.type) {
2495 case TGSI_PROCESSOR_VERTEX:
2496 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002497 if (shader->key.vs.as_es) {
2498 si_shader_ctx.gs_for_vs = &sctx->gs_shader->current->shader;
2499 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2500 } else {
2501 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2502 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002503 break;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002504 case TGSI_PROCESSOR_GEOMETRY: {
2505 int i;
2506
2507 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2508 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2509 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
2510
2511 for (i = 0; i < shader_info.num_properties; i++) {
2512 switch (shader_info.properties[i].name) {
2513 case TGSI_PROPERTY_GS_INPUT_PRIM:
2514 shader->shader.gs_input_prim = shader_info.properties[i].data[0];
2515 break;
2516 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
2517 shader->shader.gs_output_prim = shader_info.properties[i].data[0];
2518 break;
2519 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
2520 shader->shader.gs_max_out_vertices = shader_info.properties[i].data[0];
2521 break;
2522 }
2523 }
2524 break;
2525 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002526 case TGSI_PROCESSOR_FRAGMENT:
2527 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2528 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
2529 break;
2530 default:
2531 assert(!"Unsupported shader type");
2532 return -1;
2533 }
2534
Christian König206f0592013-03-20 14:37:21 +01002535 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002536 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002537 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002538 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002539 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002540
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002541 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2542 si_shader_ctx.gs_next_vertex =
2543 lp_build_alloca(bld_base->base.gallivm,
2544 bld_base->uint_bld.elem_type, "");
2545 }
2546
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002547 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002548 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002549 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002550 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002551
2552 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2553
2554 mod = bld_base->base.gallivm->module;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002555 r = si_compile_llvm(sctx, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002556 if (r) {
2557 fprintf(stderr, "LLVM failed to compile shader\n");
2558 goto out;
2559 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002560
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002561 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002562
2563 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2564 shader->gs_copy_shader = CALLOC_STRUCT(si_pipe_shader);
2565 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002566 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002567 si_shader_ctx.shader = shader->gs_copy_shader;
2568 if ((r = si_generate_gs_copy_shader(sctx, &si_shader_ctx, dump))) {
2569 free(shader->gs_copy_shader);
2570 shader->gs_copy_shader = NULL;
2571 goto out;
2572 }
2573 }
2574
Tom Stellarda75c6162012-01-06 17:38:37 -05002575 tgsi_parse_free(&si_shader_ctx.parse);
2576
Michel Dänzer404b29d2013-11-21 16:45:28 +09002577out:
Marek Olšák2fd42002013-10-25 11:45:47 +02002578 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2579 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002580 FREE(si_shader_ctx.resources);
2581 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002582
Tom Stellard302f53d2012-10-25 13:50:10 -04002583 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002584}
2585
2586void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
2587{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002588 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002589}