blob: 3ed4ad2d52e89ad658f2bd381eb579f8a471aaa1 [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 */
Michel Dänzerf8e16012014-01-28 15:39:30 +0900322 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
323 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900324 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900325 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900326
327 args[0] = t_list;
328 args[1] = vtx_offset;
329 args[2] = lp_build_const_int32(gallivm,
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900330 ((shader->input[reg->Register.Index].param_offset * 4) +
331 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900332 args[3] = uint->zero;
333 args[4] = uint->one; /* OFFEN */
334 args[5] = uint->zero; /* IDXEN */
335 args[6] = uint->one; /* GLC */
336 args[7] = uint->zero; /* SLC */
337 args[8] = uint->zero; /* TFE */
338
339 return LLVMBuildBitCast(gallivm->builder,
340 build_intrinsic(gallivm->builder,
341 "llvm.SI.buffer.load.dword.i32.i32",
342 i32, args, 9,
343 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
344 tgsi2llvmtype(bld_base, type), "");
345}
346
Tom Stellarda75c6162012-01-06 17:38:37 -0500347static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900348 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500349 unsigned input_index,
350 const struct tgsi_full_declaration *decl)
351{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900352 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
353 struct si_shader_context *si_shader_ctx =
354 si_shader_context(&radeon_bld->soa.bld_base);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200355 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900356 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
357 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400358 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900359 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100360
361 LLVMValueRef interp_param;
362 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500363
364 /* This value is:
365 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
366 * quad begins a new primitive. Bit 0 always needs
367 * to be unset)
368 * [32:16] ParamOffset
369 *
370 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900371 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200372 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500373
Christian König0666ffd2013-03-05 15:07:39 +0100374 unsigned chan;
375
Tom Stellard0fb1e682012-09-06 16:18:11 -0400376 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
377 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400378 unsigned soa_index =
379 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900380 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100381 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100382
383 if (chan == 3)
384 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900385 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100386 LLVMBuildFDiv(gallivm->builder,
387 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900388 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100389 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400390 }
391 return;
392 }
393
Michel Dänzer97078b12012-09-25 12:41:31 +0200394 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
395 LLVMValueRef face, is_face_positive;
396
Christian König0666ffd2013-03-05 15:07:39 +0100397 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
398
Michel Dänzer97078b12012-09-25 12:41:31 +0200399 is_face_positive = LLVMBuildFCmp(gallivm->builder,
400 LLVMRealUGT, face,
401 lp_build_const_float(gallivm, 0.0f),
402 "");
403
Michel Dänzer51f89a02013-12-09 15:33:53 +0900404 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200405 LLVMBuildSelect(gallivm->builder,
406 is_face_positive,
407 lp_build_const_float(gallivm, 1.0f),
408 lp_build_const_float(gallivm, 0.0f),
409 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900410 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
411 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200412 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900413 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200414 lp_build_const_float(gallivm, 1.0f);
415
416 return;
417 }
418
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900419 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200420 attr_number = lp_build_const_int32(gallivm,
421 shader->input[input_index].param_offset);
422
Francisco Jerez12799232012-04-30 18:27:52 +0200423 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500424 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100425 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100426 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200427 } else {
428 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100429 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200430 else
Christian König0666ffd2013-03-05 15:07:39 +0100431 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200432 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500433 break;
434 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100435 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500436 break;
437 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200438 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100439 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200440 else
Christian König0666ffd2013-03-05 15:07:39 +0100441 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200442 break;
443 case TGSI_INTERPOLATE_PERSPECTIVE:
444 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100445 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200446 else
Christian König0666ffd2013-03-05 15:07:39 +0100447 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500448 break;
449 default:
450 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
451 return;
452 }
453
Christian König0666ffd2013-03-05 15:07:39 +0100454 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
455
Tom Stellarda75c6162012-01-06 17:38:37 -0500456 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200457 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100458 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100459 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200460 LLVMValueRef face, is_face_positive;
461 LLVMValueRef back_attr_number =
462 lp_build_const_int32(gallivm,
463 shader->input[input_index].param_offset + 1);
464
Christian König0666ffd2013-03-05 15:07:39 +0100465 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
466
Michel Dänzer691f08d2012-09-06 18:03:38 +0200467 is_face_positive = LLVMBuildFCmp(gallivm->builder,
468 LLVMRealUGT, face,
469 lp_build_const_float(gallivm, 0.0f),
470 "");
471
Tom Stellarda75c6162012-01-06 17:38:37 -0500472 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100473 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200474 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
475 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
476 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
477 LLVMValueRef front, back;
478
479 args[0] = llvm_chan;
480 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900481 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100482 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100483 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200484
485 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900486 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100487 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100488 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200489
Michel Dänzer51f89a02013-12-09 15:33:53 +0900490 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200491 LLVMBuildSelect(gallivm->builder,
492 is_face_positive,
493 front,
494 back,
495 "");
496 }
497
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900498 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200499 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
500 LLVMValueRef args[4];
501
502 args[0] = uint->zero;
503 args[1] = attr_number;
504 args[2] = params;
505 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900506 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
507 build_intrinsic(gallivm->builder, intr_name,
508 input_type, args, args[3] ? 4 : 3,
509 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
510 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
511 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200512 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900513 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200514 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200515 } else {
516 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100517 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200518 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
519 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
520 args[0] = llvm_chan;
521 args[1] = attr_number;
522 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100523 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900524 radeon_bld->inputs[soa_index] =
525 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100526 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100527 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200528 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500529 }
530}
531
Marek Olšák250aa932014-05-06 14:10:47 +0200532static LLVMValueRef load_const(LLVMBuilderRef builder, LLVMValueRef resource,
533 LLVMValueRef offset, LLVMTypeRef return_type)
534{
535 LLVMValueRef args[2] = {resource, offset};
536
537 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
538 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
539}
540
Christian Könige4ed5872013-03-21 18:02:52 +0100541static void declare_system_value(
542 struct radeon_llvm_context * radeon_bld,
543 unsigned index,
544 const struct tgsi_full_declaration *decl)
545{
Marek Olšák8d03d922013-09-01 23:59:06 +0200546 struct si_shader_context *si_shader_ctx =
547 si_shader_context(&radeon_bld->soa.bld_base);
Christian Könige4ed5872013-03-21 18:02:52 +0100548 LLVMValueRef value = 0;
549
550 switch (decl->Semantic.Name) {
551 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200552 value = LLVMGetParam(radeon_bld->main_fn,
553 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100554 break;
555
556 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200557 value = LLVMGetParam(radeon_bld->main_fn,
558 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100559 break;
560
561 default:
562 assert(!"unknown system value");
563 return;
564 }
565
566 radeon_bld->system_values[index] = value;
567}
568
Tom Stellarda75c6162012-01-06 17:38:37 -0500569static LLVMValueRef fetch_constant(
570 struct lp_build_tgsi_context * bld_base,
571 const struct tgsi_full_src_register *reg,
572 enum tgsi_opcode_type type,
573 unsigned swizzle)
574{
Christian König55fe5cc2013-03-04 16:30:06 +0100575 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500576 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100577 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200578 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500579
Christian König0f6cf2b2013-03-15 15:53:25 +0100580 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100581 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500582
Christian König8514f5a2013-02-04 17:46:42 +0100583 if (swizzle == LP_CHAN_ALL) {
584 unsigned chan;
585 LLVMValueRef values[4];
586 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
587 values[chan] = fetch_constant(bld_base, reg, type, chan);
588
589 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
590 }
591
Marek Olšák2fd42002013-10-25 11:45:47 +0200592 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100593 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100594
Marek Olšák2fd42002013-10-25 11:45:47 +0200595 if (!reg->Register.Indirect)
596 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
597
Christian König0f6cf2b2013-03-15 15:53:25 +0100598 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
599 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
600 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200601 addr = lp_build_add(&bld_base->uint_bld, addr,
602 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200603
Marek Olšák250aa932014-05-06 14:10:47 +0200604 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
605 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500606
Christian Königf5298b02013-02-28 14:50:07 +0100607 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500608}
609
Michel Dänzer26c71392012-08-24 12:03:11 +0200610/* Initialize arguments for the shader export intrinsic */
611static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900612 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200613 unsigned target,
614 LLVMValueRef *args)
615{
616 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
617 struct lp_build_context *uint =
618 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
619 struct lp_build_context *base = &bld_base->base;
620 unsigned compressed = 0;
621 unsigned chan;
622
Michel Dänzerf402acd2012-08-22 18:15:36 +0200623 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
624 int cbuf = target - V_008DFC_SQ_EXP_MRT;
625
626 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100627 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100628
629 if (compressed)
630 si_shader_ctx->shader->spi_shader_col_format |=
631 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
632 else
633 si_shader_ctx->shader->spi_shader_col_format |=
634 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200635
636 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200637 }
638 }
639
640 if (compressed) {
641 /* Pixel shader needs to pack output values before export */
642 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900643 args[0] = values[2 * chan];
644 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200645 args[chan + 5] =
646 build_intrinsic(base->gallivm->builder,
647 "llvm.SI.packf16",
648 LLVMInt32TypeInContext(base->gallivm->context),
649 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100650 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100651 args[chan + 7] = args[chan + 5] =
652 LLVMBuildBitCast(base->gallivm->builder,
653 args[chan + 5],
654 LLVMFloatTypeInContext(base->gallivm->context),
655 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200656 }
657
658 /* Set COMPR flag */
659 args[4] = uint->one;
660 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900661 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200662 /* +5 because the first output value will be
663 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900664 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200665
666 /* Clear COMPR flag */
667 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200668 }
669
670 /* XXX: This controls which components of the output
671 * registers actually get exported. (e.g bit 0 means export
672 * X component, bit 1 means export Y component, etc.) I'm
673 * hard coding this to 0xf for now. In the future, we might
674 * want to do something else. */
675 args[0] = lp_build_const_int32(base->gallivm, 0xf);
676
677 /* Specify whether the EXEC mask represents the valid mask */
678 args[1] = uint->zero;
679
680 /* Specify whether this is the last export */
681 args[2] = uint->zero;
682
683 /* Specify the target we are exporting */
684 args[3] = lp_build_const_int32(base->gallivm, target);
685
Michel Dänzer26c71392012-08-24 12:03:11 +0200686 /* XXX: We probably need to keep track of the output
687 * values, so we know what we are passing to the next
688 * stage. */
689}
690
Michel Dänzer404b29d2013-11-21 16:45:28 +0900691/* Load from output pointers and initialize arguments for the shader export intrinsic */
692static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
693 LLVMValueRef *out_ptr,
694 unsigned target,
695 LLVMValueRef *args)
696{
697 struct gallivm_state *gallivm = bld_base->base.gallivm;
698 LLVMValueRef values[4];
699 int i;
700
701 for (i = 0; i < 4; i++)
702 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
703
704 si_llvm_init_export_args(bld_base, values, target, args);
705}
706
Michel Dänzer7708a862012-11-02 15:57:30 +0100707static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900708 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100709{
710 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
711 struct gallivm_state *gallivm = bld_base->base.gallivm;
712
Christian Königa0dca442013-03-22 15:59:22 +0100713 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400714 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
715 SI_PARAM_ALPHA_REF);
716
Michel Dänzer7708a862012-11-02 15:57:30 +0100717 LLVMValueRef alpha_pass =
718 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100719 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900720 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400721 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100722 LLVMValueRef arg =
723 lp_build_select(&bld_base->base,
724 alpha_pass,
725 lp_build_const_float(gallivm, 1.0f),
726 lp_build_const_float(gallivm, -1.0f));
727
728 build_intrinsic(gallivm->builder,
729 "llvm.AMDGPU.kill",
730 LLVMVoidTypeInContext(gallivm->context),
731 &arg, 1, 0);
732 } else {
733 build_intrinsic(gallivm->builder,
734 "llvm.AMDGPU.kilp",
735 LLVMVoidTypeInContext(gallivm->context),
736 NULL, 0, 0);
737 }
738}
739
Michel Dänzere3befbc2013-05-15 18:09:50 +0200740static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900741 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200742{
743 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200744 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200745 struct lp_build_context *base = &bld_base->base;
746 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200747 unsigned reg_index;
748 unsigned chan;
749 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200750 LLVMValueRef base_elt;
751 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšák2fd42002013-10-25 11:45:47 +0200752 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, NUM_PIPE_CONST_BUFFERS);
753 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200754
Michel Dänzere3befbc2013-05-15 18:09:50 +0200755 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200756 LLVMValueRef *args = pos[2 + reg_index];
757
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200758 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
759 continue;
760
761 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
762
Michel Dänzere3befbc2013-05-15 18:09:50 +0200763 args[5] =
764 args[6] =
765 args[7] =
766 args[8] = lp_build_const_float(base->gallivm, 0.0f);
767
768 /* Compute dot products of position and user clip plane vectors */
769 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
770 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200771 args[1] = lp_build_const_int32(base->gallivm,
772 ((reg_index * 4 + chan) * 4 +
773 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200774 base_elt = load_const(base->gallivm->builder, const_resource,
775 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200776 args[5 + chan] =
777 lp_build_add(base, args[5 + chan],
778 lp_build_mul(base, base_elt,
779 out_elts[const_chan]));
780 }
781 }
782
783 args[0] = lp_build_const_int32(base->gallivm, 0xf);
784 args[1] = uint->zero;
785 args[2] = uint->zero;
786 args[3] = lp_build_const_int32(base->gallivm,
787 V_008DFC_SQ_EXP_POS + 2 + reg_index);
788 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200789 }
790}
791
Marek Olšák8d03d922013-09-01 23:59:06 +0200792static void si_dump_streamout(struct pipe_stream_output_info *so)
793{
794 unsigned i;
795
796 if (so->num_outputs)
797 fprintf(stderr, "STREAMOUT\n");
798
799 for (i = 0; i < so->num_outputs; i++) {
800 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
801 so->output[i].start_component;
802 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
803 i, so->output[i].output_buffer,
804 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
805 so->output[i].register_index,
806 mask & 1 ? "x" : "",
807 mask & 2 ? "y" : "",
808 mask & 4 ? "z" : "",
809 mask & 8 ? "w" : "");
810 }
811}
812
813/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
814 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
815 * or v4i32 (num_channels=3,4). */
816static void build_tbuffer_store(struct si_shader_context *shader,
817 LLVMValueRef rsrc,
818 LLVMValueRef vdata,
819 unsigned num_channels,
820 LLVMValueRef vaddr,
821 LLVMValueRef soffset,
822 unsigned inst_offset,
823 unsigned dfmt,
824 unsigned nfmt,
825 unsigned offen,
826 unsigned idxen,
827 unsigned glc,
828 unsigned slc,
829 unsigned tfe)
830{
831 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
832 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
833 LLVMValueRef args[] = {
834 rsrc,
835 vdata,
836 LLVMConstInt(i32, num_channels, 0),
837 vaddr,
838 soffset,
839 LLVMConstInt(i32, inst_offset, 0),
840 LLVMConstInt(i32, dfmt, 0),
841 LLVMConstInt(i32, nfmt, 0),
842 LLVMConstInt(i32, offen, 0),
843 LLVMConstInt(i32, idxen, 0),
844 LLVMConstInt(i32, glc, 0),
845 LLVMConstInt(i32, slc, 0),
846 LLVMConstInt(i32, tfe, 0)
847 };
848
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900849 /* The instruction offset field has 12 bits */
850 assert(offen || inst_offset < (1 << 12));
851
Marek Olšák8d03d922013-09-01 23:59:06 +0200852 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
853 unsigned func = CLAMP(num_channels, 1, 3) - 1;
854 const char *types[] = {"i32", "v2i32", "v4i32"};
855 char name[256];
856 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
857
858 lp_build_intrinsic(gallivm->builder, name,
859 LLVMVoidTypeInContext(gallivm->context),
860 args, Elements(args));
861}
862
863static void build_streamout_store(struct si_shader_context *shader,
864 LLVMValueRef rsrc,
865 LLVMValueRef vdata,
866 unsigned num_channels,
867 LLVMValueRef vaddr,
868 LLVMValueRef soffset,
869 unsigned inst_offset)
870{
871 static unsigned dfmt[] = {
872 V_008F0C_BUF_DATA_FORMAT_32,
873 V_008F0C_BUF_DATA_FORMAT_32_32,
874 V_008F0C_BUF_DATA_FORMAT_32_32_32,
875 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
876 };
877 assert(num_channels >= 1 && num_channels <= 4);
878
879 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
880 inst_offset, dfmt[num_channels-1],
881 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
882}
883
884/* On SI, the vertex shader is responsible for writing streamout data
885 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900886static void si_llvm_emit_streamout(struct si_shader_context *shader,
887 struct si_shader_output_values *outputs,
888 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200889{
890 struct pipe_stream_output_info *so = &shader->shader->selector->so;
891 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
892 LLVMBuilderRef builder = gallivm->builder;
893 int i, j;
894 struct lp_build_if_state if_ctx;
895
896 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
897
898 LLVMValueRef so_param =
899 LLVMGetParam(shader->radeon_bld.main_fn,
900 shader->param_streamout_config);
901
902 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
903 LLVMValueRef so_vtx_count =
904 LLVMBuildAnd(builder,
905 LLVMBuildLShr(builder, so_param,
906 LLVMConstInt(i32, 16, 0), ""),
907 LLVMConstInt(i32, 127, 0), "");
908
909 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
910 NULL, 0, LLVMReadNoneAttribute);
911
912 /* can_emit = tid < so_vtx_count; */
913 LLVMValueRef can_emit =
914 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
915
916 /* Emit the streamout code conditionally. This actually avoids
917 * out-of-bounds buffer access. The hw tells us via the SGPR
918 * (so_vtx_count) which threads are allowed to emit streamout data. */
919 lp_build_if(&if_ctx, gallivm, can_emit);
920 {
921 /* The buffer offset is computed as follows:
922 * ByteOffset = streamout_offset[buffer_id]*4 +
923 * (streamout_write_index + thread_id)*stride[buffer_id] +
924 * attrib_offset
925 */
926
927 LLVMValueRef so_write_index =
928 LLVMGetParam(shader->radeon_bld.main_fn,
929 shader->param_streamout_write_index);
930
931 /* Compute (streamout_write_index + thread_id). */
932 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
933
934 /* Compute the write offset for each enabled buffer. */
935 LLVMValueRef so_write_offset[4] = {};
936 for (i = 0; i < 4; i++) {
937 if (!so->stride[i])
938 continue;
939
940 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
941 shader->param_streamout_offset[i]);
942 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
943
944 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
945 LLVMConstInt(i32, so->stride[i]*4, 0), "");
946 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
947 }
948
Marek Olšák8d03d922013-09-01 23:59:06 +0200949 /* Write streamout data. */
950 for (i = 0; i < so->num_outputs; i++) {
951 unsigned buf_idx = so->output[i].output_buffer;
952 unsigned reg = so->output[i].register_index;
953 unsigned start = so->output[i].start_component;
954 unsigned num_comps = so->output[i].num_components;
955 LLVMValueRef out[4];
956
957 assert(num_comps && num_comps <= 4);
958 if (!num_comps || num_comps > 4)
959 continue;
960
961 /* Load the output as int. */
962 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +0900963 unsigned outidx = 0;
964
965 while (outidx < noutput && outputs[outidx].index != reg)
966 outidx++;
967
968 if (outidx < noutput)
969 out[j] = LLVMBuildBitCast(builder,
970 outputs[outidx].values[start+j],
971 i32, "");
972 else
973 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +0200974 }
975
Michel Dänzer67e385b2014-01-08 17:48:21 +0900976 if (!out[0])
977 continue;
978
Marek Olšák8d03d922013-09-01 23:59:06 +0200979 /* Pack the output. */
980 LLVMValueRef vdata = NULL;
981
982 switch (num_comps) {
983 case 1: /* as i32 */
984 vdata = out[0];
985 break;
986 case 2: /* as v2i32 */
987 case 3: /* as v4i32 (aligned to 4) */
988 case 4: /* as v4i32 */
989 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
990 for (j = 0; j < num_comps; j++) {
991 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
992 LLVMConstInt(i32, j, 0), "");
993 }
994 break;
995 }
996
997 build_streamout_store(shader, shader->so_buffers[buf_idx],
998 vdata, num_comps,
999 so_write_offset[buf_idx],
1000 LLVMConstInt(i32, 0, 0),
1001 so->output[i].dst_offset*4);
1002 }
1003 }
1004 lp_build_endif(&if_ctx);
1005}
1006
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001007
Michel Dänzer404b29d2013-11-21 16:45:28 +09001008/* Generate export instructions for hardware VS shader stage */
1009static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1010 struct si_shader_output_values *outputs,
1011 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001012{
1013 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +02001014 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001015 struct lp_build_context * base = &bld_base->base;
1016 struct lp_build_context * uint =
1017 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001018 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001019 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001020 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
1021 unsigned semantic_name, semantic_index, semantic_usage;
1022 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001023 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001024 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001025 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001026
Michel Dänzer67e385b2014-01-08 17:48:21 +09001027 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1028 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001029 }
1030
Michel Dänzer404b29d2013-11-21 16:45:28 +09001031 for (i = 0; i < noutput; i++) {
1032 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001033 semantic_index = outputs[i].sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001034 semantic_usage = outputs[i].usage;
Tom Stellarda75c6162012-01-06 17:38:37 -05001035
Michel Dänzer0afeea52013-05-02 14:53:17 +02001036handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001037 /* Select the correct target */
1038 switch(semantic_name) {
1039 case TGSI_SEMANTIC_PSIZE:
1040 shader->vs_out_misc_write = true;
1041 shader->vs_out_point_size = true;
1042 psize_value = outputs[i].values[0];
1043 continue;
1044 case TGSI_SEMANTIC_EDGEFLAG:
1045 shader->vs_out_misc_write = true;
1046 shader->vs_out_edgeflag = true;
1047 edgeflag_value = outputs[i].values[0];
1048 continue;
1049 case TGSI_SEMANTIC_LAYER:
1050 shader->vs_out_misc_write = true;
1051 shader->vs_out_layer = true;
1052 layer_value = outputs[i].values[0];
1053 continue;
1054 case TGSI_SEMANTIC_POSITION:
1055 target = V_008DFC_SQ_EXP_POS;
1056 break;
1057 case TGSI_SEMANTIC_COLOR:
1058 case TGSI_SEMANTIC_BCOLOR:
1059 target = V_008DFC_SQ_EXP_PARAM + param_count;
1060 shader->output[i].param_offset = param_count;
1061 param_count++;
1062 break;
1063 case TGSI_SEMANTIC_CLIPDIST:
1064 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
1065 (1 << semantic_index)))
Marek Olšák053606d2013-11-19 22:07:30 +01001066 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001067 shader->clip_dist_write |=
1068 semantic_usage << (semantic_index << 2);
1069 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1070 break;
1071 case TGSI_SEMANTIC_CLIPVERTEX:
1072 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1073 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001074 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001075 case TGSI_SEMANTIC_FOG:
1076 case TGSI_SEMANTIC_GENERIC:
1077 target = V_008DFC_SQ_EXP_PARAM + param_count;
1078 shader->output[i].param_offset = param_count;
1079 param_count++;
1080 break;
1081 default:
1082 target = 0;
1083 fprintf(stderr,
1084 "Warning: SI unhandled vs output type:%d\n",
1085 semantic_name);
1086 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001087
Michel Dänzer404b29d2013-11-21 16:45:28 +09001088 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001089
Michel Dänzer404b29d2013-11-21 16:45:28 +09001090 if (target >= V_008DFC_SQ_EXP_POS &&
1091 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1092 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1093 args, sizeof(args));
1094 } else {
1095 lp_build_intrinsic(base->gallivm->builder,
1096 "llvm.SI.export",
1097 LLVMVoidTypeInContext(base->gallivm->context),
1098 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001099 }
1100
1101 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1102 semantic_name = TGSI_SEMANTIC_GENERIC;
1103 goto handle_semantic;
1104 }
1105 }
1106
1107 /* We need to add the position output manually if it's missing. */
1108 if (!pos_args[0][0]) {
1109 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1110 pos_args[0][1] = uint->zero; /* EXEC mask */
1111 pos_args[0][2] = uint->zero; /* last export? */
1112 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1113 pos_args[0][4] = uint->zero; /* COMPR flag */
1114 pos_args[0][5] = base->zero; /* X */
1115 pos_args[0][6] = base->zero; /* Y */
1116 pos_args[0][7] = base->zero; /* Z */
1117 pos_args[0][8] = base->one; /* W */
1118 }
1119
1120 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1121 if (shader->vs_out_misc_write) {
1122 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1123 shader->vs_out_point_size |
1124 (shader->vs_out_edgeflag << 1) |
1125 (shader->vs_out_layer << 2));
1126 pos_args[1][1] = uint->zero; /* EXEC mask */
1127 pos_args[1][2] = uint->zero; /* last export? */
1128 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1129 pos_args[1][4] = uint->zero; /* COMPR flag */
1130 pos_args[1][5] = base->zero; /* X */
1131 pos_args[1][6] = base->zero; /* Y */
1132 pos_args[1][7] = base->zero; /* Z */
1133 pos_args[1][8] = base->zero; /* W */
1134
Michel Dänzer404b29d2013-11-21 16:45:28 +09001135 if (shader->vs_out_point_size)
1136 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001137
1138 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001139 /* The output is a float, but the hw expects an integer
1140 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001141 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1142 edgeflag_value,
1143 bld_base->uint_bld.elem_type, "");
1144 edgeflag_value = lp_build_min(&bld_base->int_bld,
1145 edgeflag_value,
1146 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001147
1148 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001149 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1150 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001151 base->elem_type, "");
1152 }
1153
Michel Dänzer404b29d2013-11-21 16:45:28 +09001154 if (shader->vs_out_layer)
1155 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001156 }
1157
1158 for (i = 0; i < 4; i++)
1159 if (pos_args[i][0])
1160 shader->nr_pos_exports++;
1161
1162 pos_idx = 0;
1163 for (i = 0; i < 4; i++) {
1164 if (!pos_args[i][0])
1165 continue;
1166
1167 /* Specify the target we are exporting */
1168 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1169
1170 if (pos_idx == shader->nr_pos_exports)
1171 /* Specify that this is the last export */
1172 pos_args[i][2] = uint->one;
1173
1174 lp_build_intrinsic(base->gallivm->builder,
1175 "llvm.SI.export",
1176 LLVMVoidTypeInContext(base->gallivm->context),
1177 pos_args[i], 9);
1178 }
1179}
1180
Michel Dänzer404b29d2013-11-21 16:45:28 +09001181static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1182{
1183 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1184 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzere884c562014-01-15 15:24:14 +09001185 struct si_shader *es = &si_shader_ctx->shader->shader;
1186 struct si_shader *gs = si_shader_ctx->gs_for_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001187 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1188 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001189 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1190 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001191 LLVMValueRef t_list_ptr;
1192 LLVMValueRef t_list;
1193 unsigned chan;
1194 int i;
1195
1196 while (!tgsi_parse_end_of_tokens(parse)) {
1197 struct tgsi_full_declaration *d =
1198 &parse->FullToken.FullDeclaration;
1199
1200 tgsi_parse_token(parse);
1201
1202 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1203 continue;
1204
Michel Dänzere884c562014-01-15 15:24:14 +09001205 si_store_shader_io_attribs(es, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001206 }
1207
1208 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001209 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1210 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001211 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001212 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001213
Michel Dänzere884c562014-01-15 15:24:14 +09001214 for (i = 0; i < es->noutput; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001215 LLVMValueRef *out_ptr =
Michel Dänzere884c562014-01-15 15:24:14 +09001216 si_shader_ctx->radeon_bld.soa.outputs[es->output[i].index];
1217 int j;
1218
1219 for (j = 0; j < gs->ninput; j++) {
1220 if (gs->input[j].name == es->output[i].name &&
1221 gs->input[j].sid == es->output[i].sid)
1222 break;
1223 }
1224 if (j == gs->ninput)
1225 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001226
1227 for (chan = 0; chan < 4; chan++) {
1228 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001229 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1230
1231 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001232 LLVMGetUndef(i32), soffset,
1233 (4 * gs->input[j].param_offset + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001234 V_008F0C_BUF_DATA_FORMAT_32,
1235 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001236 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001237 }
1238 }
1239}
1240
1241static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1242{
1243 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1244 struct gallivm_state *gallivm = bld_base->base.gallivm;
1245 LLVMValueRef args[2];
1246
1247 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1248 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1249 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1250 LLVMVoidTypeInContext(gallivm->context), args, 2,
1251 LLVMNoUnwindAttribute);
1252}
1253
1254static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1255{
1256 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1257 struct gallivm_state *gallivm = bld_base->base.gallivm;
1258 struct si_pipe_shader *shader = si_shader_ctx->shader;
1259 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1260 struct si_shader_output_values *outputs = NULL;
1261 unsigned noutput = 0;
1262 int i;
1263
1264 while (!tgsi_parse_end_of_tokens(parse)) {
1265 struct tgsi_full_declaration *d =
1266 &parse->FullToken.FullDeclaration;
1267 unsigned index;
1268
1269 tgsi_parse_token(parse);
1270
1271 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1272 continue;
1273
1274 i = si_store_shader_io_attribs(&shader->shader, d);
1275 if (i < 0)
1276 continue;
1277
1278 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1279 (noutput + 1) * sizeof(outputs[0]));
1280 for (index = d->Range.First; index <= d->Range.Last; index++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001281 outputs[noutput].index = index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001282 outputs[noutput].name = d->Semantic.Name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001283 outputs[noutput].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001284 outputs[noutput].usage = d->Declaration.UsageMask;
1285
1286 for (i = 0; i < 4; i++)
1287 outputs[noutput].values[i] =
1288 LLVMBuildLoad(gallivm->builder,
1289 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1290 "");
1291 }
1292 noutput++;
1293 }
1294
1295 si_llvm_export_vs(bld_base, outputs, noutput);
1296 FREE(outputs);
1297}
1298
Michel Dänzer51f89a02013-12-09 15:33:53 +09001299static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1300{
1301 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
1302 struct si_shader * shader = &si_shader_ctx->shader->shader;
1303 struct lp_build_context * base = &bld_base->base;
1304 struct lp_build_context * uint =
1305 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1306 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1307 LLVMValueRef args[9];
1308 LLVMValueRef last_args[9] = { 0 };
1309 unsigned semantic_name;
1310 int depth_index = -1, stencil_index = -1;
1311 int i;
1312
1313 while (!tgsi_parse_end_of_tokens(parse)) {
1314 struct tgsi_full_declaration *d =
1315 &parse->FullToken.FullDeclaration;
1316 unsigned target;
1317 unsigned index;
1318
1319 tgsi_parse_token(parse);
1320
1321 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
1322 parse->FullToken.FullProperty.Property.PropertyName ==
1323 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
1324 shader->fs_write_all = TRUE;
1325
1326 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1327 continue;
1328
1329 i = si_store_shader_io_attribs(shader, d);
1330 if (i < 0)
1331 continue;
1332
1333 semantic_name = d->Semantic.Name;
1334 for (index = d->Range.First; index <= d->Range.Last; index++) {
1335 /* Select the correct target */
1336 switch(semantic_name) {
1337 case TGSI_SEMANTIC_POSITION:
1338 depth_index = index;
1339 continue;
1340 case TGSI_SEMANTIC_STENCIL:
1341 stencil_index = index;
1342 continue;
1343 case TGSI_SEMANTIC_COLOR:
1344 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1345 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001346 LLVMBuildStore(bld_base->base.gallivm->builder,
1347 bld_base->base.one,
1348 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1349
Michel Dänzer51f89a02013-12-09 15:33:53 +09001350 if (d->Semantic.Index == 0 &&
1351 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001352 si_alpha_test(bld_base,
1353 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001354 break;
1355 default:
1356 target = 0;
1357 fprintf(stderr,
1358 "Warning: SI unhandled fs output type:%d\n",
1359 semantic_name);
1360 }
1361
Michel Dänzer404b29d2013-11-21 16:45:28 +09001362 si_llvm_init_export_args_load(bld_base,
1363 si_shader_ctx->radeon_bld.soa.outputs[index],
1364 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001365
1366 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001367 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001368 if (last_args[0]) {
1369 lp_build_intrinsic(base->gallivm->builder,
1370 "llvm.SI.export",
1371 LLVMVoidTypeInContext(base->gallivm->context),
1372 last_args, 9);
1373 }
1374
Marek Olšák0eb528a2013-12-04 13:24:22 +01001375 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001376 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001377
1378 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1379 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1380 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1381 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001382 si_llvm_init_export_args_load(bld_base,
1383 si_shader_ctx->radeon_bld.soa.outputs[index],
1384 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001385 lp_build_intrinsic(base->gallivm->builder,
1386 "llvm.SI.export",
1387 LLVMVoidTypeInContext(base->gallivm->context),
1388 args, 9);
1389 }
1390 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001391 } else {
1392 lp_build_intrinsic(base->gallivm->builder,
1393 "llvm.SI.export",
1394 LLVMVoidTypeInContext(base->gallivm->context),
1395 args, 9);
1396 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001397 }
1398 }
1399
Michel Dänzer1a616c12012-11-13 17:35:09 +01001400 if (depth_index >= 0 || stencil_index >= 0) {
1401 LLVMValueRef out_ptr;
1402 unsigned mask = 0;
1403
1404 /* Specify the target we are exporting */
1405 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1406
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001407 args[5] = base->zero; /* R, depth */
1408 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1409 args[7] = base->zero; /* B, sample mask */
1410 args[8] = base->zero; /* A, alpha to mask */
1411
Michel Dänzer1a616c12012-11-13 17:35:09 +01001412 if (depth_index >= 0) {
1413 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1414 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1415 mask |= 0x1;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001416 }
1417
1418 if (stencil_index >= 0) {
1419 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001420 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001421 /* Only setting the stencil component bit (0x2) here
1422 * breaks some stencil piglit tests
1423 */
1424 mask |= 0x3;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001425 }
1426
1427 /* Specify which components to enable */
1428 args[0] = lp_build_const_int32(base->gallivm, mask);
1429
1430 args[1] =
1431 args[2] =
1432 args[4] = uint->zero;
1433
1434 if (last_args[0])
1435 lp_build_intrinsic(base->gallivm->builder,
1436 "llvm.SI.export",
1437 LLVMVoidTypeInContext(base->gallivm->context),
1438 args, 9);
1439 else
1440 memcpy(last_args, args, sizeof(args));
1441 }
1442
Michel Dänzer51f89a02013-12-09 15:33:53 +09001443 if (!last_args[0]) {
1444 /* Specify which components to enable */
1445 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001446
Michel Dänzer51f89a02013-12-09 15:33:53 +09001447 /* Specify the target we are exporting */
1448 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001449
Michel Dänzer51f89a02013-12-09 15:33:53 +09001450 /* Set COMPR flag to zero to export data as 32-bit */
1451 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001452
Michel Dänzer51f89a02013-12-09 15:33:53 +09001453 /* dummy bits */
1454 last_args[5]= uint->zero;
1455 last_args[6]= uint->zero;
1456 last_args[7]= uint->zero;
1457 last_args[8]= uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001458
Michel Dänzer51f89a02013-12-09 15:33:53 +09001459 si_shader_ctx->shader->spi_shader_col_format |=
1460 V_028714_SPI_SHADER_32_ABGR;
1461 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001462 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001463
1464 /* Specify whether the EXEC mask represents the valid mask */
1465 last_args[1] = uint->one;
1466
1467 /* Specify that this is the last export */
1468 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1469
1470 lp_build_intrinsic(base->gallivm->builder,
1471 "llvm.SI.export",
1472 LLVMVoidTypeInContext(base->gallivm->context),
1473 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001474}
1475
Marek Olšák4855acd2013-08-06 15:08:54 +02001476static const struct lp_build_tgsi_action txf_action;
1477
1478static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1479 struct lp_build_tgsi_context * bld_base,
1480 struct lp_build_emit_data * emit_data);
1481
Tom Stellarda75c6162012-01-06 17:38:37 -05001482static void tex_fetch_args(
1483 struct lp_build_tgsi_context * bld_base,
1484 struct lp_build_emit_data * emit_data)
1485{
Christian König55fe5cc2013-03-04 16:30:06 +01001486 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001487 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001488 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001489 unsigned opcode = inst->Instruction.Opcode;
1490 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001491 LLVMValueRef coords[4];
1492 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001493 int ref_pos;
1494 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001495 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001496 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001497 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1498 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1499
1500 if (target == TGSI_TEXTURE_BUFFER) {
1501 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1502 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1503 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1504 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1505
1506 /* Truncate v32i8 to v16i8. */
1507 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1508 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1509 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1510 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1511
1512 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1513 emit_data->args[0] = res;
1514 emit_data->args[1] = bld_base->uint_bld.zero;
1515 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1516 emit_data->arg_count = 3;
1517 return;
1518 }
Tom Stellard467f5162012-05-16 15:15:35 -04001519
Michel Dänzer120efee2013-01-25 12:10:11 +01001520 /* Fetch and project texture coordinates */
1521 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001522 for (chan = 0; chan < 3; chan++ ) {
1523 coords[chan] = lp_build_emit_fetch(bld_base,
1524 emit_data->inst, 0,
1525 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001526 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001527 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1528 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001529 coords[chan],
1530 coords[3]);
1531 }
1532
Michel Dänzer120efee2013-01-25 12:10:11 +01001533 if (opcode == TGSI_OPCODE_TXP)
1534 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001535
Michel Dänzer120efee2013-01-25 12:10:11 +01001536 /* Pack LOD bias value */
1537 if (opcode == TGSI_OPCODE_TXB)
1538 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001539 if (opcode == TGSI_OPCODE_TXB2)
1540 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001541
Marek Olšák2484daa2014-04-22 21:23:29 +02001542 if (target == TGSI_TEXTURE_CUBE ||
1543 target == TGSI_TEXTURE_CUBE_ARRAY ||
1544 target == TGSI_TEXTURE_SHADOWCUBE ||
1545 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001546 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001547
1548 /* Pack depth comparison value */
1549 switch (target) {
1550 case TGSI_TEXTURE_SHADOW1D:
1551 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1552 case TGSI_TEXTURE_SHADOW2D:
1553 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001554 case TGSI_TEXTURE_SHADOWCUBE:
1555 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001556 assert(ref_pos >= 0);
1557 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001558 break;
1559 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1560 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001561 }
1562
Michel Dänzera6b83c02013-02-21 16:10:55 +01001563 /* Pack user derivatives */
1564 if (opcode == TGSI_OPCODE_TXD) {
1565 for (chan = 0; chan < 2; chan++) {
1566 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1567 if (num_coords > 1)
1568 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1569 }
1570 }
1571
Michel Dänzer120efee2013-01-25 12:10:11 +01001572 /* Pack texture coordinates */
1573 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001574 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001575 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001576 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001577 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001578
Marek Olšákd2bd6342013-09-18 15:40:21 +02001579 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001580 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001581 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001582 if (opcode == TGSI_OPCODE_TXL2)
1583 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001584
1585 if (count > 16) {
1586 assert(!"Cannot handle more than 16 texture address parameters");
1587 count = 16;
1588 }
1589
1590 for (chan = 0; chan < count; chan++ ) {
1591 address[chan] = LLVMBuildBitCast(gallivm->builder,
1592 address[chan],
1593 LLVMInt32TypeInContext(gallivm->context),
1594 "");
1595 }
1596
Marek Olšák4855acd2013-08-06 15:08:54 +02001597 /* Adjust the sample index according to FMASK.
1598 *
1599 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1600 * which is the identity mapping. Each nibble says which physical sample
1601 * should be fetched to get that sample.
1602 *
1603 * For example, 0x11111100 means there are only 2 samples stored and
1604 * the second sample covers 3/4 of the pixel. When reading samples 0
1605 * and 1, return physical sample 0 (determined by the first two 0s
1606 * in FMASK), otherwise return physical sample 1.
1607 *
1608 * The sample index should be adjusted as follows:
1609 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1610 */
1611 if (target == TGSI_TEXTURE_2D_MSAA ||
1612 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1613 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1614 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001615 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001616 unsigned txf_count = count;
1617
Marek Olšákd2bd6342013-09-18 15:40:21 +02001618 memcpy(txf_address, address, sizeof(txf_address));
1619
1620 if (target == TGSI_TEXTURE_2D_MSAA) {
1621 txf_address[2] = bld_base->uint_bld.zero;
1622 }
1623 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001624
1625 /* Pad to a power-of-two size. */
1626 while (txf_count < util_next_power_of_two(txf_count))
1627 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1628
1629 /* Read FMASK using TXF. */
1630 txf_emit_data.chan = 0;
1631 txf_emit_data.dst_type = LLVMVectorType(
1632 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1633 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1634 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
Marek Olšákd2bd6342013-09-18 15:40:21 +02001635 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1636 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
Marek Olšák4855acd2013-08-06 15:08:54 +02001637 txf_emit_data.arg_count = 3;
1638
1639 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1640
1641 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001642 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1643 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1644
1645 /* Apply the formula. */
1646 LLVMValueRef fmask =
1647 LLVMBuildExtractElement(gallivm->builder,
1648 txf_emit_data.output[0],
1649 uint_bld->zero, "");
1650
Marek Olšákd2bd6342013-09-18 15:40:21 +02001651 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001652
1653 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001654 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001655
1656 LLVMValueRef shifted_fmask =
1657 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1658
1659 LLVMValueRef final_sample =
1660 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1661
1662 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1663 * resource descriptor is 0 (invalid),
1664 */
1665 LLVMValueRef fmask_desc =
1666 LLVMBuildBitCast(gallivm->builder,
1667 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1668 LLVMVectorType(uint_bld->elem_type, 8), "");
1669
1670 LLVMValueRef fmask_word1 =
1671 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1672 uint_bld->one, "");
1673
1674 LLVMValueRef word1_is_nonzero =
1675 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1676 fmask_word1, uint_bld->zero, "");
1677
Marek Olšákd2bd6342013-09-18 15:40:21 +02001678 /* Replace the MSAA sample index. */
1679 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001680 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001681 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001682 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001683
Michel Dänzer36231112013-05-02 09:44:45 +02001684 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001685 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001686
1687 if (opcode == TGSI_OPCODE_TXF) {
1688 /* add tex offsets */
1689 if (inst->Texture.NumOffsets) {
1690 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1691 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1692 const struct tgsi_texture_offset * off = inst->TexOffsets;
1693
1694 assert(inst->Texture.NumOffsets == 1);
1695
Marek Olšákdefedc02013-09-18 15:36:38 +02001696 switch (target) {
1697 case TGSI_TEXTURE_3D:
1698 address[2] = lp_build_add(uint_bld, address[2],
1699 bld->immediates[off->Index][off->SwizzleZ]);
1700 /* fall through */
1701 case TGSI_TEXTURE_2D:
1702 case TGSI_TEXTURE_SHADOW2D:
1703 case TGSI_TEXTURE_RECT:
1704 case TGSI_TEXTURE_SHADOWRECT:
1705 case TGSI_TEXTURE_2D_ARRAY:
1706 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001707 address[1] =
1708 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001709 bld->immediates[off->Index][off->SwizzleY]);
1710 /* fall through */
1711 case TGSI_TEXTURE_1D:
1712 case TGSI_TEXTURE_SHADOW1D:
1713 case TGSI_TEXTURE_1D_ARRAY:
1714 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1715 address[0] =
1716 lp_build_add(uint_bld, address[0],
1717 bld->immediates[off->Index][off->SwizzleX]);
1718 break;
1719 /* texture offsets do not apply to other texture targets */
1720 }
Michel Dänzer36231112013-05-02 09:44:45 +02001721 }
1722
1723 emit_data->dst_type = LLVMVectorType(
1724 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1725 4);
1726
1727 emit_data->arg_count = 3;
1728 } else {
1729 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001730 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001731
1732 emit_data->dst_type = LLVMVectorType(
1733 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1734 4);
1735
1736 emit_data->arg_count = 4;
1737 }
1738
Marek Olšák2484daa2014-04-22 21:23:29 +02001739 /* The fetch opcode has been converted to a 2D array fetch.
1740 * This simplifies the LLVM backend. */
1741 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1742 target = TGSI_TEXTURE_2D_ARRAY;
1743 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1744 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1745
Michel Dänzer36231112013-05-02 09:44:45 +02001746 /* Dimensions */
1747 emit_data->args[emit_data->arg_count - 1] =
1748 lp_build_const_int32(bld_base->base.gallivm, target);
1749
Michel Dänzer120efee2013-01-25 12:10:11 +01001750 /* Pad to power of two vector */
1751 while (count < util_next_power_of_two(count))
1752 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1753
Christian Königccf3e8f2013-03-26 15:09:27 +01001754 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001755}
1756
Michel Dänzer07eddc42013-02-06 15:43:10 +01001757static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1758 struct lp_build_tgsi_context * bld_base,
1759 struct lp_build_emit_data * emit_data)
1760{
1761 struct lp_build_context * base = &bld_base->base;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001762 char intr_name[127];
Michel Dänzer07eddc42013-02-06 15:43:10 +01001763
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001764 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1765 emit_data->output[emit_data->chan] = build_intrinsic(
1766 base->gallivm->builder,
1767 "llvm.SI.vs.load.input", emit_data->dst_type,
1768 emit_data->args, emit_data->arg_count,
1769 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1770 return;
1771 }
1772
Michel Dänzer07eddc42013-02-06 15:43:10 +01001773 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001774 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001775
Christian König44e32242013-03-20 12:10:35 +01001776 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001777 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001778 emit_data->args, emit_data->arg_count,
1779 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001780}
1781
Michel Dänzer0495adb2013-05-06 12:45:14 +02001782static void txq_fetch_args(
1783 struct lp_build_tgsi_context * bld_base,
1784 struct lp_build_emit_data * emit_data)
1785{
1786 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1787 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001788 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02001789 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001790
Marek Olšák2484daa2014-04-22 21:23:29 +02001791 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001792 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1793 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1794
1795 /* Read the size from the buffer descriptor directly. */
1796 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1797 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1798 size = LLVMBuildExtractElement(gallivm->builder, size,
1799 lp_build_const_int32(gallivm, 2), "");
1800 emit_data->args[0] = size;
1801 return;
1802 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02001803
1804 /* Mip level */
1805 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1806
1807 /* Resource */
1808 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1809
Marek Olšák2484daa2014-04-22 21:23:29 +02001810 /* Texture target */
1811 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
1812 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1813 target = TGSI_TEXTURE_2D_ARRAY;
1814
Michel Dänzer0495adb2013-05-06 12:45:14 +02001815 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1816 inst->Texture.Texture);
1817
1818 emit_data->arg_count = 3;
1819
1820 emit_data->dst_type = LLVMVectorType(
1821 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1822 4);
1823}
1824
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001825static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
1826 struct lp_build_tgsi_context * bld_base,
1827 struct lp_build_emit_data * emit_data)
1828{
Marek Olšák2484daa2014-04-22 21:23:29 +02001829 unsigned target = emit_data->inst->Texture.Texture;
1830
1831 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001832 /* Just return the buffer size. */
1833 emit_data->output[emit_data->chan] = emit_data->args[0];
1834 return;
1835 }
1836
1837 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02001838
1839 /* Divide the number of layers by 6 to get the number of cubes. */
1840 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
1841 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1842 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1843 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
1844 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
1845
1846 LLVMValueRef v4 = emit_data->output[emit_data->chan];
1847 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
1848 z = LLVMBuildSDiv(builder, z, six, "");
1849
1850 emit_data->output[emit_data->chan] =
1851 LLVMBuildInsertElement(builder, v4, z, two, "");
1852 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001853}
1854
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001855#if HAVE_LLVM >= 0x0304
1856
1857static void si_llvm_emit_ddxy(
1858 const struct lp_build_tgsi_action * action,
1859 struct lp_build_tgsi_context * bld_base,
1860 struct lp_build_emit_data * emit_data)
1861{
1862 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1863 struct gallivm_state *gallivm = bld_base->base.gallivm;
1864 struct lp_build_context * base = &bld_base->base;
1865 const struct tgsi_full_instruction *inst = emit_data->inst;
1866 unsigned opcode = inst->Instruction.Opcode;
1867 LLVMValueRef indices[2];
1868 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1869 LLVMValueRef tl, trbl, result[4];
1870 LLVMTypeRef i32;
1871 unsigned swizzle[4];
1872 unsigned c;
1873
1874 i32 = LLVMInt32TypeInContext(gallivm->context);
1875
1876 indices[0] = bld_base->uint_bld.zero;
1877 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1878 NULL, 0, LLVMReadNoneAttribute);
1879 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1880 indices, 2, "");
1881
1882 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1883 lp_build_const_int32(gallivm, 0xfffffffc), "");
1884 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1885 indices, 2, "");
1886
1887 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1888 lp_build_const_int32(gallivm,
1889 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1890 "");
1891 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1892 indices, 2, "");
1893
1894 for (c = 0; c < 4; ++c) {
1895 unsigned i;
1896
1897 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1898 for (i = 0; i < c; ++i) {
1899 if (swizzle[i] == swizzle[c]) {
1900 result[c] = result[i];
1901 break;
1902 }
1903 }
1904 if (i != c)
1905 continue;
1906
1907 LLVMBuildStore(gallivm->builder,
1908 LLVMBuildBitCast(gallivm->builder,
1909 lp_build_emit_fetch(bld_base, inst, 0, c),
1910 i32, ""),
1911 store_ptr);
1912
1913 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1914 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1915
1916 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1917 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1918
1919 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1920 }
1921
1922 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1923}
1924
1925#endif /* HAVE_LLVM >= 0x0304 */
1926
Michel Dänzer404b29d2013-11-21 16:45:28 +09001927/* Emit one vertex from the geometry shader */
1928static void si_llvm_emit_vertex(
1929 const struct lp_build_tgsi_action *action,
1930 struct lp_build_tgsi_context *bld_base,
1931 struct lp_build_emit_data *emit_data)
1932{
1933 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001934 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001935 struct si_shader *shader = &si_shader_ctx->shader->shader;
1936 struct gallivm_state *gallivm = bld_base->base.gallivm;
1937 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001938 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1939 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001940 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09001941 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001942 LLVMValueRef t_list_ptr;
1943 LLVMValueRef t_list;
1944 LLVMValueRef args[2];
1945 unsigned chan;
1946 int i;
1947
1948 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001949 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1950 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001951 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001952 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001953
1954 if (shader->noutput == 0) {
1955 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1956
1957 while (!tgsi_parse_end_of_tokens(parse)) {
1958 tgsi_parse_token(parse);
1959
Michel Dänzer7c7d7382014-01-09 15:33:34 +09001960 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
1961 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
1962
1963 if (d->Declaration.File == TGSI_FILE_OUTPUT)
1964 si_store_shader_io_attribs(shader, d);
1965 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001966 }
1967 }
1968
1969 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001970 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09001971
1972 /* If this thread has already emitted the declared maximum number of
1973 * vertices, kill it: excessive vertex emissions are not supposed to
1974 * have any effect, and GS threads have no externally observable
1975 * effects other than emitting vertices.
1976 */
1977 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
1978 lp_build_const_int32(gallivm,
1979 shader->gs_max_out_vertices), "");
1980 kill = lp_build_select(&bld_base->base, can_emit,
1981 lp_build_const_float(gallivm, 1.0f),
1982 lp_build_const_float(gallivm, -1.0f));
1983 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
1984 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
1985
Michel Dänzer404b29d2013-11-21 16:45:28 +09001986 for (i = 0; i < shader->noutput; i++) {
1987 LLVMValueRef *out_ptr =
1988 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
1989
1990 for (chan = 0; chan < 4; chan++) {
1991 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001992 LLVMValueRef voffset =
1993 lp_build_const_int32(gallivm, (i * 4 + chan) *
1994 shader->gs_max_out_vertices);
1995
1996 voffset = lp_build_add(uint, voffset, gs_next_vertex);
1997 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001998
1999 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2000
2001 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2002 voffset, soffset, 0,
2003 V_008F0C_BUF_DATA_FORMAT_32,
2004 V_008F0C_BUF_NUM_FORMAT_UINT,
2005 1, 0, 1, 1, 0);
2006 }
2007 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002008 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2009 lp_build_const_int32(gallivm, 1));
2010 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002011
2012 /* Signal vertex emission */
2013 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2014 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2015 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2016 LLVMVoidTypeInContext(gallivm->context), args, 2,
2017 LLVMNoUnwindAttribute);
2018}
2019
2020/* Cut one primitive from the geometry shader */
2021static void si_llvm_emit_primitive(
2022 const struct lp_build_tgsi_action *action,
2023 struct lp_build_tgsi_context *bld_base,
2024 struct lp_build_emit_data *emit_data)
2025{
2026 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2027 struct gallivm_state *gallivm = bld_base->base.gallivm;
2028 LLVMValueRef args[2];
2029
2030 /* Signal primitive cut */
2031 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2032 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2033 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2034 LLVMVoidTypeInContext(gallivm->context), args, 2,
2035 LLVMNoUnwindAttribute);
2036}
2037
Tom Stellarda75c6162012-01-06 17:38:37 -05002038static const struct lp_build_tgsi_action tex_action = {
2039 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002040 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002041 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05002042};
2043
Michel Dänzer3e205132012-11-06 17:39:01 +01002044static const struct lp_build_tgsi_action txb_action = {
2045 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002046 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002047 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01002048};
2049
Michel Dänzera6b83c02013-02-21 16:10:55 +01002050#if HAVE_LLVM >= 0x0304
2051static const struct lp_build_tgsi_action txd_action = {
2052 .fetch_args = tex_fetch_args,
2053 .emit = build_tex_intrinsic,
2054 .intr_name = "llvm.SI.sampled."
2055};
2056#endif
2057
Michel Dänzer36231112013-05-02 09:44:45 +02002058static const struct lp_build_tgsi_action txf_action = {
2059 .fetch_args = tex_fetch_args,
2060 .emit = build_tex_intrinsic,
2061 .intr_name = "llvm.SI.imageload."
2062};
2063
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002064static const struct lp_build_tgsi_action txl_action = {
2065 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002066 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002067 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002068};
2069
Michel Dänzer0495adb2013-05-06 12:45:14 +02002070static const struct lp_build_tgsi_action txq_action = {
2071 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002072 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002073 .intr_name = "llvm.SI.resinfo"
2074};
2075
Christian König206f0592013-03-20 14:37:21 +01002076static void create_meta_data(struct si_shader_context *si_shader_ctx)
2077{
2078 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2079 LLVMValueRef args[3];
2080
2081 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2082 args[1] = 0;
2083 args[2] = lp_build_const_int32(gallivm, 1);
2084
2085 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2086}
2087
Christian König55fe5cc2013-03-04 16:30:06 +01002088static void create_function(struct si_shader_context *si_shader_ctx)
2089{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002090 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2091 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002092 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzerf8e16012014-01-28 15:39:30 +09002093 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002094 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002095
Christian König55fe5cc2013-03-04 16:30:06 +01002096 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002097 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002098 f32 = LLVMFloatTypeInContext(gallivm->context);
2099 v2i32 = LLVMVectorType(i32, 2);
2100 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01002101
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002102 params[SI_PARAM_CONST] = LLVMPointerType(
2103 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
Michel Dänzerf8e16012014-01-28 15:39:30 +09002104 params[SI_PARAM_RW_BUFFERS] = params[SI_PARAM_CONST];
2105
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002106 /* We assume at most 16 textures per program at the moment.
2107 * This need probably need to be changed to support bindless textures */
2108 params[SI_PARAM_SAMPLER] = LLVMPointerType(
2109 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
2110 params[SI_PARAM_RESOURCE] = LLVMPointerType(
2111 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002112
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002113 switch (si_shader_ctx->type) {
2114 case TGSI_PROCESSOR_VERTEX:
2115 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01002116 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002117 num_params = SI_PARAM_START_INSTANCE+1;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002118 if (shader->key.vs.as_es) {
2119 params[SI_PARAM_ES2GS_OFFSET] = i32;
2120 num_params++;
2121 } else {
2122 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002123
Michel Dänzer404b29d2013-11-21 16:45:28 +09002124 /* Streamout SGPRs. */
2125 if (shader->selector->so.num_outputs) {
2126 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2127 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2128 }
2129 /* A streamout buffer offset is loaded if the stride is non-zero. */
2130 for (i = 0; i < 4; i++) {
2131 if (!shader->selector->so.stride[i])
2132 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002133
Michel Dänzer404b29d2013-11-21 16:45:28 +09002134 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2135 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002136 }
2137
2138 last_sgpr = num_params-1;
2139
2140 /* VGPRs */
2141 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2142 params[num_params++] = i32; /* unused*/
2143 params[num_params++] = i32; /* unused */
2144 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002145 break;
Christian König0666ffd2013-03-05 15:07:39 +01002146
Michel Dänzer404b29d2013-11-21 16:45:28 +09002147 case TGSI_PROCESSOR_GEOMETRY:
2148 params[SI_PARAM_GS2VS_OFFSET] = i32;
2149 params[SI_PARAM_GS_WAVE_ID] = i32;
2150 last_sgpr = SI_PARAM_GS_WAVE_ID;
2151
2152 /* VGPRs */
2153 params[SI_PARAM_VTX0_OFFSET] = i32;
2154 params[SI_PARAM_VTX1_OFFSET] = i32;
2155 params[SI_PARAM_PRIMITIVE_ID] = i32;
2156 params[SI_PARAM_VTX2_OFFSET] = i32;
2157 params[SI_PARAM_VTX3_OFFSET] = i32;
2158 params[SI_PARAM_VTX4_OFFSET] = i32;
2159 params[SI_PARAM_VTX5_OFFSET] = i32;
2160 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2161 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2162 break;
2163
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002164 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002165 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002166 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002167 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002168 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2169 params[SI_PARAM_PERSP_CENTER] = v2i32;
2170 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2171 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2172 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2173 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2174 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2175 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2176 params[SI_PARAM_POS_X_FLOAT] = f32;
2177 params[SI_PARAM_POS_Y_FLOAT] = f32;
2178 params[SI_PARAM_POS_Z_FLOAT] = f32;
2179 params[SI_PARAM_POS_W_FLOAT] = f32;
2180 params[SI_PARAM_FRONT_FACE] = f32;
2181 params[SI_PARAM_ANCILLARY] = f32;
2182 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2183 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002184 num_params = SI_PARAM_POS_FIXED_PT+1;
2185 break;
2186
2187 default:
2188 assert(0 && "unimplemented shader");
2189 return;
Christian Königc4973212013-03-05 12:14:02 +01002190 }
Christian König55fe5cc2013-03-04 16:30:06 +01002191
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002192 assert(num_params <= Elements(params));
2193 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002194 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002195
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002196 for (i = 0; i <= last_sgpr; ++i) {
2197 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002198 switch (i) {
2199 default:
2200 LLVMAddAttribute(P, LLVMInRegAttribute);
2201 break;
2202#if HAVE_LLVM >= 0x0304
2203 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2204 * to move load. Inputs are constant so this is fine. */
2205 case SI_PARAM_CONST:
2206 case SI_PARAM_SAMPLER:
2207 case SI_PARAM_RESOURCE:
2208 LLVMAddAttribute(P, LLVMByValAttribute);
2209 break;
2210#endif
2211 }
Christian Königcf9b31f2013-03-21 18:30:23 +01002212 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002213
2214#if HAVE_LLVM >= 0x0304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002215 if (bld_base->info &&
2216 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2217 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002218 si_shader_ctx->ddxy_lds =
2219 LLVMAddGlobalInAddressSpace(gallivm->module,
2220 LLVMArrayType(i32, 64),
2221 "ddxy_lds",
2222 LOCAL_ADDR_SPACE);
2223#endif
Christian König55fe5cc2013-03-04 16:30:06 +01002224}
Tom Stellarda75c6162012-01-06 17:38:37 -05002225
Christian König0f6cf2b2013-03-15 15:53:25 +01002226static void preload_constants(struct si_shader_context *si_shader_ctx)
2227{
2228 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2229 struct gallivm_state * gallivm = bld_base->base.gallivm;
2230 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002231 unsigned buf;
2232 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002233
Marek Olšák2fd42002013-10-25 11:45:47 +02002234 for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) {
2235 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002236
Marek Olšák2fd42002013-10-25 11:45:47 +02002237 if (num_const == 0)
2238 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002239
Marek Olšák2fd42002013-10-25 11:45:47 +02002240 /* Allocate space for the constant values */
2241 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002242
Marek Olšák2fd42002013-10-25 11:45:47 +02002243 /* Load the resource descriptor */
2244 si_shader_ctx->const_resource[buf] =
2245 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002246
Marek Olšák2fd42002013-10-25 11:45:47 +02002247 /* Load the constants, we rely on the code sinking to do the rest */
2248 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002249 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002250 load_const(gallivm->builder,
2251 si_shader_ctx->const_resource[buf],
2252 lp_build_const_int32(gallivm, i * 4),
2253 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002254 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002255 }
2256}
2257
Christian König1c100182013-03-17 16:02:42 +01002258static void preload_samplers(struct si_shader_context *si_shader_ctx)
2259{
2260 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2261 struct gallivm_state * gallivm = bld_base->base.gallivm;
2262 const struct tgsi_shader_info * info = bld_base->info;
2263
2264 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2265
2266 LLVMValueRef res_ptr, samp_ptr;
2267 LLVMValueRef offset;
2268
2269 if (num_samplers == 0)
2270 return;
2271
2272 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02002273 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002274 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2275
2276 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2277 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2278
2279 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2280 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002281 /* Resource */
2282 offset = lp_build_const_int32(gallivm, i);
2283 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2284
2285 /* Sampler */
2286 offset = lp_build_const_int32(gallivm, i);
2287 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002288
2289 /* FMASK resource */
2290 if (info->is_msaa_sampler[i]) {
2291 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
2292 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
2293 build_indexed_load(si_shader_ctx, res_ptr, offset);
2294 }
Christian König1c100182013-03-17 16:02:42 +01002295 }
2296}
2297
Marek Olšák8d03d922013-09-01 23:59:06 +02002298static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2299{
2300 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2301 struct gallivm_state * gallivm = bld_base->base.gallivm;
2302 unsigned i;
2303
Michel Dänzer67e385b2014-01-08 17:48:21 +09002304 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2305 si_shader_ctx->shader->key.vs.as_es ||
2306 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002307 return;
2308
2309 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002310 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002311
2312 /* Load the resources, we rely on the code sinking to do the rest */
2313 for (i = 0; i < 4; ++i) {
2314 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002315 LLVMValueRef offset = lp_build_const_int32(gallivm,
2316 SI_RW_SO + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002317
2318 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2319 }
2320 }
2321}
2322
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002323int si_compile_llvm(struct si_context *sctx, struct si_pipe_shader *shader,
Tom Stellard302f53d2012-10-25 13:50:10 -04002324 LLVMModuleRef mod)
2325{
Darren Powellbc866902014-03-31 18:00:28 -04002326 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002327 unsigned i;
2328 uint32_t *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002329 struct radeon_shader_binary binary;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002330 bool dump = r600_can_dump_shader(&sctx->screen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002331 shader->selector ? shader->selector->tokens : NULL);
Darren Powellbc866902014-03-31 18:00:28 -04002332 const char * gpu_family = r600_get_llvm_processor_name(sctx->screen->b.family);
2333
2334 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002335 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002336 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2337
2338 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002339 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002340 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002341 for (i = 0; i < binary.code_size; i+=4 ) {
2342 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2343 binary.code[i + 2], binary.code[i + 1],
2344 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002345 }
2346 }
2347
Tom Stellardd50343d2013-04-04 16:21:06 -04002348 /* XXX: We may be able to emit some of these values directly rather than
2349 * extracting fields to be emitted later.
2350 */
Darren Powellbc866902014-03-31 18:00:28 -04002351 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002352 for (i = 0; i < binary.config_size; i+= 8) {
2353 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2354 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2355 switch (reg) {
2356 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2357 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2358 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2359 case R_00B848_COMPUTE_PGM_RSRC1:
2360 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2361 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2362 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002363 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2364 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2365 break;
2366 case R_00B84C_COMPUTE_PGM_RSRC2:
2367 shader->lds_size = G_00B84C_LDS_SIZE(value);
2368 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002369 case R_0286CC_SPI_PS_INPUT_ENA:
2370 shader->spi_ps_input_ena = value;
2371 break;
2372 default:
2373 fprintf(stderr, "Warning: Compiler emitted unknown "
2374 "config register: 0x%x\n", reg);
2375 break;
2376 }
2377 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002378
2379 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02002380 r600_resource_reference(&shader->bo, NULL);
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002381 shader->bo = si_resource_create_custom(sctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04002382 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002383 if (shader->bo == NULL) {
2384 return -ENOMEM;
2385 }
2386
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002387 ptr = (uint32_t*)sctx->b.ws->buffer_map(shader->bo->cs_buf, sctx->b.rings.gfx.cs, PIPE_TRANSFER_WRITE);
Tom Stellardeac72362014-02-19 14:19:53 -08002388 if (SI_BIG_ENDIAN) {
Tom Stellardd50343d2013-04-04 16:21:06 -04002389 for (i = 0; i < binary.code_size / 4; ++i) {
Tom Stellard195ee102014-02-20 09:03:53 -08002390 ptr[i] = util_cpu_to_le32((*(uint32_t*)(binary.code + i*4)));
Tom Stellard302f53d2012-10-25 13:50:10 -04002391 }
2392 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04002393 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002394 }
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002395 sctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002396
Tom Stellard7782d192013-04-04 09:57:13 -07002397 free(binary.code);
2398 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04002399
Darren Powellbc866902014-03-31 18:00:28 -04002400 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002401}
2402
Michel Dänzer404b29d2013-11-21 16:45:28 +09002403/* Generate code for the hardware VS shader stage to go with a geometry shader */
2404static int si_generate_gs_copy_shader(struct si_context *sctx,
2405 struct si_shader_context *si_shader_ctx,
2406 bool dump)
2407{
2408 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2409 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2410 struct lp_build_context *base = &bld_base->base;
2411 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002412 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002413 struct si_shader *gs = &si_shader_ctx->shader->selector->current->shader;
2414 struct si_shader_output_values *outputs;
2415 LLVMValueRef t_list_ptr, t_list;
2416 LLVMValueRef args[9];
2417 int i, r;
2418
2419 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2420
2421 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
2422 si_shader_ctx->gs_for_vs = gs;
2423
2424 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2425
2426 create_meta_data(si_shader_ctx);
2427 create_function(si_shader_ctx);
2428 preload_streamout_buffers(si_shader_ctx);
2429
2430 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002431 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2432 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002433 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002434 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002435
2436 args[0] = t_list;
2437 args[1] = lp_build_mul_imm(uint,
2438 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2439 si_shader_ctx->param_vertex_id),
2440 4);
2441 args[3] = uint->zero;
2442 args[4] = uint->one; /* OFFEN */
2443 args[5] = uint->zero; /* IDXEN */
2444 args[6] = uint->one; /* GLC */
2445 args[7] = uint->one; /* SLC */
2446 args[8] = uint->zero; /* TFE */
2447
2448 /* Fetch vertex data from GSVS ring */
2449 for (i = 0; i < gs->noutput; ++i) {
2450 struct si_shader_output *out = gs->output + i;
2451 unsigned chan;
2452
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002453 shader->output[i] = *out;
2454
Michel Dänzer404b29d2013-11-21 16:45:28 +09002455 outputs[i].name = out->name;
2456 outputs[i].index = out->index;
Michel Dänzer67e385b2014-01-08 17:48:21 +09002457 outputs[i].sid = out->sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002458 outputs[i].usage = out->usage;
2459
2460 for (chan = 0; chan < 4; chan++) {
2461 args[2] = lp_build_const_int32(gallivm,
2462 (i * 4 + chan) *
2463 gs->gs_max_out_vertices * 16 * 4);
2464
2465 outputs[i].values[chan] =
2466 LLVMBuildBitCast(gallivm->builder,
2467 build_intrinsic(gallivm->builder,
2468 "llvm.SI.buffer.load.dword.i32.i32",
2469 LLVMInt32TypeInContext(gallivm->context),
2470 args, 9,
2471 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2472 base->elem_type, "");
2473 }
2474 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002475 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002476
2477 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2478
2479 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2480
2481 if (dump)
2482 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2483
2484 r = si_compile_llvm(sctx, si_shader_ctx->shader,
2485 bld_base->base.gallivm->module);
2486
2487 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2488
2489 FREE(outputs);
2490 return r;
2491}
2492
Tom Stellarda75c6162012-01-06 17:38:37 -05002493int si_pipe_shader_create(
2494 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01002495 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002496{
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002497 struct si_context *sctx = (struct si_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002498 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002499 struct si_shader_context si_shader_ctx;
2500 struct tgsi_shader_info shader_info;
2501 struct lp_build_tgsi_context * bld_base;
2502 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002503 int r = 0;
Michel Dänzere1df0d42014-01-15 12:31:07 +09002504 bool dump = r600_can_dump_shader(&sctx->screen->b, sel->tokens);
2505
2506 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2507 * conversion fails. */
2508 if (dump) {
2509 tgsi_dump(sel->tokens, 0);
2510 si_dump_streamout(&sel->so);
2511 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002512
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002513 assert(shader->shader.noutput == 0);
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002514 assert(shader->shader.nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002515 assert(shader->shader.ninput == 0);
2516
Michel Dänzercfebaf92012-08-31 19:04:08 +02002517 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002518 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2519 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2520
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002521 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002522
Michel Dänzere44dfd42012-11-07 17:33:08 +01002523 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01002524 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002525 bld_base->info = &shader_info;
2526 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002527
2528 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002529 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01002530 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002531 bld_base->op_actions[TGSI_OPCODE_TXB2] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01002532#if HAVE_LLVM >= 0x0304
2533 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
2534#endif
Michel Dänzer36231112013-05-02 09:44:45 +02002535 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002536 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002537 bld_base->op_actions[TGSI_OPCODE_TXL2] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002538 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002539 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002540
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002541#if HAVE_LLVM >= 0x0304
2542 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2543 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
2544#endif
2545
Michel Dänzer404b29d2013-11-21 16:45:28 +09002546 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2547 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2548
Christian Könige4ed5872013-03-21 18:02:52 +01002549 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002550 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002551 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2552 si_shader_ctx.shader = shader;
2553 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002554
Michel Dänzer51f89a02013-12-09 15:33:53 +09002555 switch (si_shader_ctx.type) {
2556 case TGSI_PROCESSOR_VERTEX:
2557 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002558 if (shader->key.vs.as_es) {
2559 si_shader_ctx.gs_for_vs = &sctx->gs_shader->current->shader;
2560 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2561 } else {
2562 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2563 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002564 break;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002565 case TGSI_PROCESSOR_GEOMETRY: {
2566 int i;
2567
2568 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2569 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2570 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
2571
2572 for (i = 0; i < shader_info.num_properties; i++) {
2573 switch (shader_info.properties[i].name) {
2574 case TGSI_PROPERTY_GS_INPUT_PRIM:
2575 shader->shader.gs_input_prim = shader_info.properties[i].data[0];
2576 break;
2577 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
2578 shader->shader.gs_output_prim = shader_info.properties[i].data[0];
2579 break;
2580 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
2581 shader->shader.gs_max_out_vertices = shader_info.properties[i].data[0];
2582 break;
2583 }
2584 }
2585 break;
2586 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002587 case TGSI_PROCESSOR_FRAGMENT:
2588 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2589 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
2590 break;
2591 default:
2592 assert(!"Unsupported shader type");
2593 return -1;
2594 }
2595
Christian König206f0592013-03-20 14:37:21 +01002596 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002597 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002598 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002599 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002600 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002601
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002602 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2603 si_shader_ctx.gs_next_vertex =
2604 lp_build_alloca(bld_base->base.gallivm,
2605 bld_base->uint_bld.elem_type, "");
2606 }
2607
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002608 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002609 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002610 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002611 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002612
2613 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2614
2615 mod = bld_base->base.gallivm->module;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002616 r = si_compile_llvm(sctx, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002617 if (r) {
2618 fprintf(stderr, "LLVM failed to compile shader\n");
2619 goto out;
2620 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002621
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002622 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002623
2624 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2625 shader->gs_copy_shader = CALLOC_STRUCT(si_pipe_shader);
2626 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002627 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002628 si_shader_ctx.shader = shader->gs_copy_shader;
2629 if ((r = si_generate_gs_copy_shader(sctx, &si_shader_ctx, dump))) {
2630 free(shader->gs_copy_shader);
2631 shader->gs_copy_shader = NULL;
2632 goto out;
2633 }
2634 }
2635
Tom Stellarda75c6162012-01-06 17:38:37 -05002636 tgsi_parse_free(&si_shader_ctx.parse);
2637
Michel Dänzer404b29d2013-11-21 16:45:28 +09002638out:
Marek Olšák2fd42002013-10-25 11:45:47 +02002639 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2640 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002641 FREE(si_shader_ctx.resources);
2642 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002643
Tom Stellard302f53d2012-10-25 13:50:10 -04002644 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002645}
2646
2647void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
2648{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002649 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002650}