blob: 00dfb3c783c87122fc9e51c90201f11780164c7c [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
1407 if (depth_index >= 0) {
1408 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1409 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1410 mask |= 0x1;
1411
1412 if (stencil_index < 0) {
1413 args[6] =
1414 args[7] =
1415 args[8] = args[5];
1416 }
1417 }
1418
1419 if (stencil_index >= 0) {
1420 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
1421 args[7] =
1422 args[8] =
1423 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001424 /* Only setting the stencil component bit (0x2) here
1425 * breaks some stencil piglit tests
1426 */
1427 mask |= 0x3;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001428
1429 if (depth_index < 0)
1430 args[5] = args[6];
1431 }
1432
1433 /* Specify which components to enable */
1434 args[0] = lp_build_const_int32(base->gallivm, mask);
1435
1436 args[1] =
1437 args[2] =
1438 args[4] = uint->zero;
1439
1440 if (last_args[0])
1441 lp_build_intrinsic(base->gallivm->builder,
1442 "llvm.SI.export",
1443 LLVMVoidTypeInContext(base->gallivm->context),
1444 args, 9);
1445 else
1446 memcpy(last_args, args, sizeof(args));
1447 }
1448
Michel Dänzer51f89a02013-12-09 15:33:53 +09001449 if (!last_args[0]) {
1450 /* Specify which components to enable */
1451 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001452
Michel Dänzer51f89a02013-12-09 15:33:53 +09001453 /* Specify the target we are exporting */
1454 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001455
Michel Dänzer51f89a02013-12-09 15:33:53 +09001456 /* Set COMPR flag to zero to export data as 32-bit */
1457 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001458
Michel Dänzer51f89a02013-12-09 15:33:53 +09001459 /* dummy bits */
1460 last_args[5]= uint->zero;
1461 last_args[6]= uint->zero;
1462 last_args[7]= uint->zero;
1463 last_args[8]= uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001464
Michel Dänzer51f89a02013-12-09 15:33:53 +09001465 si_shader_ctx->shader->spi_shader_col_format |=
1466 V_028714_SPI_SHADER_32_ABGR;
1467 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001468 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001469
1470 /* Specify whether the EXEC mask represents the valid mask */
1471 last_args[1] = uint->one;
1472
1473 /* Specify that this is the last export */
1474 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1475
1476 lp_build_intrinsic(base->gallivm->builder,
1477 "llvm.SI.export",
1478 LLVMVoidTypeInContext(base->gallivm->context),
1479 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001480}
1481
Marek Olšák4855acd2013-08-06 15:08:54 +02001482static const struct lp_build_tgsi_action txf_action;
1483
1484static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1485 struct lp_build_tgsi_context * bld_base,
1486 struct lp_build_emit_data * emit_data);
1487
Tom Stellarda75c6162012-01-06 17:38:37 -05001488static void tex_fetch_args(
1489 struct lp_build_tgsi_context * bld_base,
1490 struct lp_build_emit_data * emit_data)
1491{
Christian König55fe5cc2013-03-04 16:30:06 +01001492 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001493 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001494 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001495 unsigned opcode = inst->Instruction.Opcode;
1496 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001497 LLVMValueRef coords[4];
1498 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001499 int ref_pos;
1500 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001501 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001502 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001503 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1504 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1505
1506 if (target == TGSI_TEXTURE_BUFFER) {
1507 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1508 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1509 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1510 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1511
1512 /* Truncate v32i8 to v16i8. */
1513 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1514 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1515 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1516 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1517
1518 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1519 emit_data->args[0] = res;
1520 emit_data->args[1] = bld_base->uint_bld.zero;
1521 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1522 emit_data->arg_count = 3;
1523 return;
1524 }
Tom Stellard467f5162012-05-16 15:15:35 -04001525
Michel Dänzer120efee2013-01-25 12:10:11 +01001526 /* Fetch and project texture coordinates */
1527 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001528 for (chan = 0; chan < 3; chan++ ) {
1529 coords[chan] = lp_build_emit_fetch(bld_base,
1530 emit_data->inst, 0,
1531 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001532 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001533 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1534 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001535 coords[chan],
1536 coords[3]);
1537 }
1538
Michel Dänzer120efee2013-01-25 12:10:11 +01001539 if (opcode == TGSI_OPCODE_TXP)
1540 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001541
Michel Dänzer120efee2013-01-25 12:10:11 +01001542 /* Pack LOD bias value */
1543 if (opcode == TGSI_OPCODE_TXB)
1544 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001545 if (opcode == TGSI_OPCODE_TXB2)
1546 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001547
Marek Olšák2484daa2014-04-22 21:23:29 +02001548 if (target == TGSI_TEXTURE_CUBE ||
1549 target == TGSI_TEXTURE_CUBE_ARRAY ||
1550 target == TGSI_TEXTURE_SHADOWCUBE ||
1551 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001552 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001553
1554 /* Pack depth comparison value */
1555 switch (target) {
1556 case TGSI_TEXTURE_SHADOW1D:
1557 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1558 case TGSI_TEXTURE_SHADOW2D:
1559 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001560 case TGSI_TEXTURE_SHADOWCUBE:
1561 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001562 assert(ref_pos >= 0);
1563 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001564 break;
1565 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1566 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001567 }
1568
Michel Dänzera6b83c02013-02-21 16:10:55 +01001569 /* Pack user derivatives */
1570 if (opcode == TGSI_OPCODE_TXD) {
1571 for (chan = 0; chan < 2; chan++) {
1572 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1573 if (num_coords > 1)
1574 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1575 }
1576 }
1577
Michel Dänzer120efee2013-01-25 12:10:11 +01001578 /* Pack texture coordinates */
1579 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001580 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001581 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001582 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001583 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001584
Marek Olšákd2bd6342013-09-18 15:40:21 +02001585 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001586 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001587 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001588 if (opcode == TGSI_OPCODE_TXL2)
1589 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001590
1591 if (count > 16) {
1592 assert(!"Cannot handle more than 16 texture address parameters");
1593 count = 16;
1594 }
1595
1596 for (chan = 0; chan < count; chan++ ) {
1597 address[chan] = LLVMBuildBitCast(gallivm->builder,
1598 address[chan],
1599 LLVMInt32TypeInContext(gallivm->context),
1600 "");
1601 }
1602
Marek Olšák4855acd2013-08-06 15:08:54 +02001603 /* Adjust the sample index according to FMASK.
1604 *
1605 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1606 * which is the identity mapping. Each nibble says which physical sample
1607 * should be fetched to get that sample.
1608 *
1609 * For example, 0x11111100 means there are only 2 samples stored and
1610 * the second sample covers 3/4 of the pixel. When reading samples 0
1611 * and 1, return physical sample 0 (determined by the first two 0s
1612 * in FMASK), otherwise return physical sample 1.
1613 *
1614 * The sample index should be adjusted as follows:
1615 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1616 */
1617 if (target == TGSI_TEXTURE_2D_MSAA ||
1618 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1619 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1620 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001621 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001622 unsigned txf_count = count;
1623
Marek Olšákd2bd6342013-09-18 15:40:21 +02001624 memcpy(txf_address, address, sizeof(txf_address));
1625
1626 if (target == TGSI_TEXTURE_2D_MSAA) {
1627 txf_address[2] = bld_base->uint_bld.zero;
1628 }
1629 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001630
1631 /* Pad to a power-of-two size. */
1632 while (txf_count < util_next_power_of_two(txf_count))
1633 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1634
1635 /* Read FMASK using TXF. */
1636 txf_emit_data.chan = 0;
1637 txf_emit_data.dst_type = LLVMVectorType(
1638 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1639 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1640 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
Marek Olšákd2bd6342013-09-18 15:40:21 +02001641 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1642 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
Marek Olšák4855acd2013-08-06 15:08:54 +02001643 txf_emit_data.arg_count = 3;
1644
1645 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1646
1647 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001648 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1649 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1650
1651 /* Apply the formula. */
1652 LLVMValueRef fmask =
1653 LLVMBuildExtractElement(gallivm->builder,
1654 txf_emit_data.output[0],
1655 uint_bld->zero, "");
1656
Marek Olšákd2bd6342013-09-18 15:40:21 +02001657 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001658
1659 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001660 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001661
1662 LLVMValueRef shifted_fmask =
1663 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1664
1665 LLVMValueRef final_sample =
1666 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1667
1668 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1669 * resource descriptor is 0 (invalid),
1670 */
1671 LLVMValueRef fmask_desc =
1672 LLVMBuildBitCast(gallivm->builder,
1673 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1674 LLVMVectorType(uint_bld->elem_type, 8), "");
1675
1676 LLVMValueRef fmask_word1 =
1677 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1678 uint_bld->one, "");
1679
1680 LLVMValueRef word1_is_nonzero =
1681 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1682 fmask_word1, uint_bld->zero, "");
1683
Marek Olšákd2bd6342013-09-18 15:40:21 +02001684 /* Replace the MSAA sample index. */
1685 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001686 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001687 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001688 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001689
Michel Dänzer36231112013-05-02 09:44:45 +02001690 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001691 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001692
1693 if (opcode == TGSI_OPCODE_TXF) {
1694 /* add tex offsets */
1695 if (inst->Texture.NumOffsets) {
1696 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1697 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1698 const struct tgsi_texture_offset * off = inst->TexOffsets;
1699
1700 assert(inst->Texture.NumOffsets == 1);
1701
Marek Olšákdefedc02013-09-18 15:36:38 +02001702 switch (target) {
1703 case TGSI_TEXTURE_3D:
1704 address[2] = lp_build_add(uint_bld, address[2],
1705 bld->immediates[off->Index][off->SwizzleZ]);
1706 /* fall through */
1707 case TGSI_TEXTURE_2D:
1708 case TGSI_TEXTURE_SHADOW2D:
1709 case TGSI_TEXTURE_RECT:
1710 case TGSI_TEXTURE_SHADOWRECT:
1711 case TGSI_TEXTURE_2D_ARRAY:
1712 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001713 address[1] =
1714 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001715 bld->immediates[off->Index][off->SwizzleY]);
1716 /* fall through */
1717 case TGSI_TEXTURE_1D:
1718 case TGSI_TEXTURE_SHADOW1D:
1719 case TGSI_TEXTURE_1D_ARRAY:
1720 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1721 address[0] =
1722 lp_build_add(uint_bld, address[0],
1723 bld->immediates[off->Index][off->SwizzleX]);
1724 break;
1725 /* texture offsets do not apply to other texture targets */
1726 }
Michel Dänzer36231112013-05-02 09:44:45 +02001727 }
1728
1729 emit_data->dst_type = LLVMVectorType(
1730 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1731 4);
1732
1733 emit_data->arg_count = 3;
1734 } else {
1735 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001736 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001737
1738 emit_data->dst_type = LLVMVectorType(
1739 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1740 4);
1741
1742 emit_data->arg_count = 4;
1743 }
1744
Marek Olšák2484daa2014-04-22 21:23:29 +02001745 /* The fetch opcode has been converted to a 2D array fetch.
1746 * This simplifies the LLVM backend. */
1747 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1748 target = TGSI_TEXTURE_2D_ARRAY;
1749 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1750 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1751
Michel Dänzer36231112013-05-02 09:44:45 +02001752 /* Dimensions */
1753 emit_data->args[emit_data->arg_count - 1] =
1754 lp_build_const_int32(bld_base->base.gallivm, target);
1755
Michel Dänzer120efee2013-01-25 12:10:11 +01001756 /* Pad to power of two vector */
1757 while (count < util_next_power_of_two(count))
1758 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1759
Christian Königccf3e8f2013-03-26 15:09:27 +01001760 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001761}
1762
Michel Dänzer07eddc42013-02-06 15:43:10 +01001763static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1764 struct lp_build_tgsi_context * bld_base,
1765 struct lp_build_emit_data * emit_data)
1766{
1767 struct lp_build_context * base = &bld_base->base;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001768 char intr_name[127];
Michel Dänzer07eddc42013-02-06 15:43:10 +01001769
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001770 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1771 emit_data->output[emit_data->chan] = build_intrinsic(
1772 base->gallivm->builder,
1773 "llvm.SI.vs.load.input", emit_data->dst_type,
1774 emit_data->args, emit_data->arg_count,
1775 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1776 return;
1777 }
1778
Michel Dänzer07eddc42013-02-06 15:43:10 +01001779 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001780 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001781
Christian König44e32242013-03-20 12:10:35 +01001782 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001783 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001784 emit_data->args, emit_data->arg_count,
1785 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001786}
1787
Michel Dänzer0495adb2013-05-06 12:45:14 +02001788static void txq_fetch_args(
1789 struct lp_build_tgsi_context * bld_base,
1790 struct lp_build_emit_data * emit_data)
1791{
1792 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1793 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001794 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02001795 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001796
Marek Olšák2484daa2014-04-22 21:23:29 +02001797 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001798 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1799 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1800
1801 /* Read the size from the buffer descriptor directly. */
1802 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1803 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1804 size = LLVMBuildExtractElement(gallivm->builder, size,
1805 lp_build_const_int32(gallivm, 2), "");
1806 emit_data->args[0] = size;
1807 return;
1808 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02001809
1810 /* Mip level */
1811 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1812
1813 /* Resource */
1814 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1815
Marek Olšák2484daa2014-04-22 21:23:29 +02001816 /* Texture target */
1817 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
1818 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1819 target = TGSI_TEXTURE_2D_ARRAY;
1820
Michel Dänzer0495adb2013-05-06 12:45:14 +02001821 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1822 inst->Texture.Texture);
1823
1824 emit_data->arg_count = 3;
1825
1826 emit_data->dst_type = LLVMVectorType(
1827 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1828 4);
1829}
1830
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001831static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
1832 struct lp_build_tgsi_context * bld_base,
1833 struct lp_build_emit_data * emit_data)
1834{
Marek Olšák2484daa2014-04-22 21:23:29 +02001835 unsigned target = emit_data->inst->Texture.Texture;
1836
1837 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001838 /* Just return the buffer size. */
1839 emit_data->output[emit_data->chan] = emit_data->args[0];
1840 return;
1841 }
1842
1843 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02001844
1845 /* Divide the number of layers by 6 to get the number of cubes. */
1846 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
1847 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1848 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1849 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
1850 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
1851
1852 LLVMValueRef v4 = emit_data->output[emit_data->chan];
1853 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
1854 z = LLVMBuildSDiv(builder, z, six, "");
1855
1856 emit_data->output[emit_data->chan] =
1857 LLVMBuildInsertElement(builder, v4, z, two, "");
1858 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001859}
1860
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001861#if HAVE_LLVM >= 0x0304
1862
1863static void si_llvm_emit_ddxy(
1864 const struct lp_build_tgsi_action * action,
1865 struct lp_build_tgsi_context * bld_base,
1866 struct lp_build_emit_data * emit_data)
1867{
1868 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1869 struct gallivm_state *gallivm = bld_base->base.gallivm;
1870 struct lp_build_context * base = &bld_base->base;
1871 const struct tgsi_full_instruction *inst = emit_data->inst;
1872 unsigned opcode = inst->Instruction.Opcode;
1873 LLVMValueRef indices[2];
1874 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1875 LLVMValueRef tl, trbl, result[4];
1876 LLVMTypeRef i32;
1877 unsigned swizzle[4];
1878 unsigned c;
1879
1880 i32 = LLVMInt32TypeInContext(gallivm->context);
1881
1882 indices[0] = bld_base->uint_bld.zero;
1883 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1884 NULL, 0, LLVMReadNoneAttribute);
1885 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1886 indices, 2, "");
1887
1888 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1889 lp_build_const_int32(gallivm, 0xfffffffc), "");
1890 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1891 indices, 2, "");
1892
1893 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1894 lp_build_const_int32(gallivm,
1895 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1896 "");
1897 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1898 indices, 2, "");
1899
1900 for (c = 0; c < 4; ++c) {
1901 unsigned i;
1902
1903 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1904 for (i = 0; i < c; ++i) {
1905 if (swizzle[i] == swizzle[c]) {
1906 result[c] = result[i];
1907 break;
1908 }
1909 }
1910 if (i != c)
1911 continue;
1912
1913 LLVMBuildStore(gallivm->builder,
1914 LLVMBuildBitCast(gallivm->builder,
1915 lp_build_emit_fetch(bld_base, inst, 0, c),
1916 i32, ""),
1917 store_ptr);
1918
1919 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1920 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1921
1922 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1923 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1924
1925 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1926 }
1927
1928 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1929}
1930
1931#endif /* HAVE_LLVM >= 0x0304 */
1932
Michel Dänzer404b29d2013-11-21 16:45:28 +09001933/* Emit one vertex from the geometry shader */
1934static void si_llvm_emit_vertex(
1935 const struct lp_build_tgsi_action *action,
1936 struct lp_build_tgsi_context *bld_base,
1937 struct lp_build_emit_data *emit_data)
1938{
1939 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001940 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001941 struct si_shader *shader = &si_shader_ctx->shader->shader;
1942 struct gallivm_state *gallivm = bld_base->base.gallivm;
1943 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001944 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1945 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001946 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09001947 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001948 LLVMValueRef t_list_ptr;
1949 LLVMValueRef t_list;
1950 LLVMValueRef args[2];
1951 unsigned chan;
1952 int i;
1953
1954 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001955 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1956 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001957 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001958 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001959
1960 if (shader->noutput == 0) {
1961 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1962
1963 while (!tgsi_parse_end_of_tokens(parse)) {
1964 tgsi_parse_token(parse);
1965
Michel Dänzer7c7d7382014-01-09 15:33:34 +09001966 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
1967 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
1968
1969 if (d->Declaration.File == TGSI_FILE_OUTPUT)
1970 si_store_shader_io_attribs(shader, d);
1971 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001972 }
1973 }
1974
1975 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001976 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09001977
1978 /* If this thread has already emitted the declared maximum number of
1979 * vertices, kill it: excessive vertex emissions are not supposed to
1980 * have any effect, and GS threads have no externally observable
1981 * effects other than emitting vertices.
1982 */
1983 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
1984 lp_build_const_int32(gallivm,
1985 shader->gs_max_out_vertices), "");
1986 kill = lp_build_select(&bld_base->base, can_emit,
1987 lp_build_const_float(gallivm, 1.0f),
1988 lp_build_const_float(gallivm, -1.0f));
1989 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
1990 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
1991
Michel Dänzer404b29d2013-11-21 16:45:28 +09001992 for (i = 0; i < shader->noutput; i++) {
1993 LLVMValueRef *out_ptr =
1994 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
1995
1996 for (chan = 0; chan < 4; chan++) {
1997 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001998 LLVMValueRef voffset =
1999 lp_build_const_int32(gallivm, (i * 4 + chan) *
2000 shader->gs_max_out_vertices);
2001
2002 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2003 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002004
2005 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2006
2007 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2008 voffset, soffset, 0,
2009 V_008F0C_BUF_DATA_FORMAT_32,
2010 V_008F0C_BUF_NUM_FORMAT_UINT,
2011 1, 0, 1, 1, 0);
2012 }
2013 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002014 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2015 lp_build_const_int32(gallivm, 1));
2016 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002017
2018 /* Signal vertex emission */
2019 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2020 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2021 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2022 LLVMVoidTypeInContext(gallivm->context), args, 2,
2023 LLVMNoUnwindAttribute);
2024}
2025
2026/* Cut one primitive from the geometry shader */
2027static void si_llvm_emit_primitive(
2028 const struct lp_build_tgsi_action *action,
2029 struct lp_build_tgsi_context *bld_base,
2030 struct lp_build_emit_data *emit_data)
2031{
2032 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2033 struct gallivm_state *gallivm = bld_base->base.gallivm;
2034 LLVMValueRef args[2];
2035
2036 /* Signal primitive cut */
2037 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2038 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2039 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2040 LLVMVoidTypeInContext(gallivm->context), args, 2,
2041 LLVMNoUnwindAttribute);
2042}
2043
Tom Stellarda75c6162012-01-06 17:38:37 -05002044static const struct lp_build_tgsi_action tex_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.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05002048};
2049
Michel Dänzer3e205132012-11-06 17:39:01 +01002050static const struct lp_build_tgsi_action txb_action = {
2051 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002052 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002053 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01002054};
2055
Michel Dänzera6b83c02013-02-21 16:10:55 +01002056#if HAVE_LLVM >= 0x0304
2057static const struct lp_build_tgsi_action txd_action = {
2058 .fetch_args = tex_fetch_args,
2059 .emit = build_tex_intrinsic,
2060 .intr_name = "llvm.SI.sampled."
2061};
2062#endif
2063
Michel Dänzer36231112013-05-02 09:44:45 +02002064static const struct lp_build_tgsi_action txf_action = {
2065 .fetch_args = tex_fetch_args,
2066 .emit = build_tex_intrinsic,
2067 .intr_name = "llvm.SI.imageload."
2068};
2069
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002070static const struct lp_build_tgsi_action txl_action = {
2071 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002072 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002073 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002074};
2075
Michel Dänzer0495adb2013-05-06 12:45:14 +02002076static const struct lp_build_tgsi_action txq_action = {
2077 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002078 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002079 .intr_name = "llvm.SI.resinfo"
2080};
2081
Christian König206f0592013-03-20 14:37:21 +01002082static void create_meta_data(struct si_shader_context *si_shader_ctx)
2083{
2084 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2085 LLVMValueRef args[3];
2086
2087 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2088 args[1] = 0;
2089 args[2] = lp_build_const_int32(gallivm, 1);
2090
2091 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2092}
2093
Christian König55fe5cc2013-03-04 16:30:06 +01002094static void create_function(struct si_shader_context *si_shader_ctx)
2095{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002096 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2097 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002098 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzerf8e16012014-01-28 15:39:30 +09002099 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002100 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002101
Christian König55fe5cc2013-03-04 16:30:06 +01002102 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002103 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002104 f32 = LLVMFloatTypeInContext(gallivm->context);
2105 v2i32 = LLVMVectorType(i32, 2);
2106 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01002107
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002108 params[SI_PARAM_CONST] = LLVMPointerType(
2109 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
Michel Dänzerf8e16012014-01-28 15:39:30 +09002110 params[SI_PARAM_RW_BUFFERS] = params[SI_PARAM_CONST];
2111
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002112 /* We assume at most 16 textures per program at the moment.
2113 * This need probably need to be changed to support bindless textures */
2114 params[SI_PARAM_SAMPLER] = LLVMPointerType(
2115 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
2116 params[SI_PARAM_RESOURCE] = LLVMPointerType(
2117 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002118
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002119 switch (si_shader_ctx->type) {
2120 case TGSI_PROCESSOR_VERTEX:
2121 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01002122 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002123 num_params = SI_PARAM_START_INSTANCE+1;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002124 if (shader->key.vs.as_es) {
2125 params[SI_PARAM_ES2GS_OFFSET] = i32;
2126 num_params++;
2127 } else {
2128 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002129
Michel Dänzer404b29d2013-11-21 16:45:28 +09002130 /* Streamout SGPRs. */
2131 if (shader->selector->so.num_outputs) {
2132 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2133 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2134 }
2135 /* A streamout buffer offset is loaded if the stride is non-zero. */
2136 for (i = 0; i < 4; i++) {
2137 if (!shader->selector->so.stride[i])
2138 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002139
Michel Dänzer404b29d2013-11-21 16:45:28 +09002140 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2141 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002142 }
2143
2144 last_sgpr = num_params-1;
2145
2146 /* VGPRs */
2147 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2148 params[num_params++] = i32; /* unused*/
2149 params[num_params++] = i32; /* unused */
2150 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002151 break;
Christian König0666ffd2013-03-05 15:07:39 +01002152
Michel Dänzer404b29d2013-11-21 16:45:28 +09002153 case TGSI_PROCESSOR_GEOMETRY:
2154 params[SI_PARAM_GS2VS_OFFSET] = i32;
2155 params[SI_PARAM_GS_WAVE_ID] = i32;
2156 last_sgpr = SI_PARAM_GS_WAVE_ID;
2157
2158 /* VGPRs */
2159 params[SI_PARAM_VTX0_OFFSET] = i32;
2160 params[SI_PARAM_VTX1_OFFSET] = i32;
2161 params[SI_PARAM_PRIMITIVE_ID] = i32;
2162 params[SI_PARAM_VTX2_OFFSET] = i32;
2163 params[SI_PARAM_VTX3_OFFSET] = i32;
2164 params[SI_PARAM_VTX4_OFFSET] = i32;
2165 params[SI_PARAM_VTX5_OFFSET] = i32;
2166 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2167 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2168 break;
2169
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002170 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002171 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002172 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002173 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002174 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2175 params[SI_PARAM_PERSP_CENTER] = v2i32;
2176 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2177 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2178 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2179 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2180 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2181 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2182 params[SI_PARAM_POS_X_FLOAT] = f32;
2183 params[SI_PARAM_POS_Y_FLOAT] = f32;
2184 params[SI_PARAM_POS_Z_FLOAT] = f32;
2185 params[SI_PARAM_POS_W_FLOAT] = f32;
2186 params[SI_PARAM_FRONT_FACE] = f32;
2187 params[SI_PARAM_ANCILLARY] = f32;
2188 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2189 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002190 num_params = SI_PARAM_POS_FIXED_PT+1;
2191 break;
2192
2193 default:
2194 assert(0 && "unimplemented shader");
2195 return;
Christian Königc4973212013-03-05 12:14:02 +01002196 }
Christian König55fe5cc2013-03-04 16:30:06 +01002197
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002198 assert(num_params <= Elements(params));
2199 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002200 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002201
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002202 for (i = 0; i <= last_sgpr; ++i) {
2203 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002204 switch (i) {
2205 default:
2206 LLVMAddAttribute(P, LLVMInRegAttribute);
2207 break;
2208#if HAVE_LLVM >= 0x0304
2209 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2210 * to move load. Inputs are constant so this is fine. */
2211 case SI_PARAM_CONST:
2212 case SI_PARAM_SAMPLER:
2213 case SI_PARAM_RESOURCE:
2214 LLVMAddAttribute(P, LLVMByValAttribute);
2215 break;
2216#endif
2217 }
Christian Königcf9b31f2013-03-21 18:30:23 +01002218 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002219
2220#if HAVE_LLVM >= 0x0304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002221 if (bld_base->info &&
2222 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2223 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002224 si_shader_ctx->ddxy_lds =
2225 LLVMAddGlobalInAddressSpace(gallivm->module,
2226 LLVMArrayType(i32, 64),
2227 "ddxy_lds",
2228 LOCAL_ADDR_SPACE);
2229#endif
Christian König55fe5cc2013-03-04 16:30:06 +01002230}
Tom Stellarda75c6162012-01-06 17:38:37 -05002231
Christian König0f6cf2b2013-03-15 15:53:25 +01002232static void preload_constants(struct si_shader_context *si_shader_ctx)
2233{
2234 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2235 struct gallivm_state * gallivm = bld_base->base.gallivm;
2236 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002237 unsigned buf;
2238 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002239
Marek Olšák2fd42002013-10-25 11:45:47 +02002240 for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) {
2241 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002242
Marek Olšák2fd42002013-10-25 11:45:47 +02002243 if (num_const == 0)
2244 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002245
Marek Olšák2fd42002013-10-25 11:45:47 +02002246 /* Allocate space for the constant values */
2247 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002248
Marek Olšák2fd42002013-10-25 11:45:47 +02002249 /* Load the resource descriptor */
2250 si_shader_ctx->const_resource[buf] =
2251 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002252
Marek Olšák2fd42002013-10-25 11:45:47 +02002253 /* Load the constants, we rely on the code sinking to do the rest */
2254 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002255 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002256 load_const(gallivm->builder,
2257 si_shader_ctx->const_resource[buf],
2258 lp_build_const_int32(gallivm, i * 4),
2259 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002260 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002261 }
2262}
2263
Christian König1c100182013-03-17 16:02:42 +01002264static void preload_samplers(struct si_shader_context *si_shader_ctx)
2265{
2266 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2267 struct gallivm_state * gallivm = bld_base->base.gallivm;
2268 const struct tgsi_shader_info * info = bld_base->info;
2269
2270 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2271
2272 LLVMValueRef res_ptr, samp_ptr;
2273 LLVMValueRef offset;
2274
2275 if (num_samplers == 0)
2276 return;
2277
2278 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02002279 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002280 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2281
2282 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2283 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2284
2285 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2286 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002287 /* Resource */
2288 offset = lp_build_const_int32(gallivm, i);
2289 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2290
2291 /* Sampler */
2292 offset = lp_build_const_int32(gallivm, i);
2293 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002294
2295 /* FMASK resource */
2296 if (info->is_msaa_sampler[i]) {
2297 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
2298 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
2299 build_indexed_load(si_shader_ctx, res_ptr, offset);
2300 }
Christian König1c100182013-03-17 16:02:42 +01002301 }
2302}
2303
Marek Olšák8d03d922013-09-01 23:59:06 +02002304static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2305{
2306 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2307 struct gallivm_state * gallivm = bld_base->base.gallivm;
2308 unsigned i;
2309
Michel Dänzer67e385b2014-01-08 17:48:21 +09002310 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2311 si_shader_ctx->shader->key.vs.as_es ||
2312 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002313 return;
2314
2315 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002316 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002317
2318 /* Load the resources, we rely on the code sinking to do the rest */
2319 for (i = 0; i < 4; ++i) {
2320 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002321 LLVMValueRef offset = lp_build_const_int32(gallivm,
2322 SI_RW_SO + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002323
2324 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2325 }
2326 }
2327}
2328
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002329int si_compile_llvm(struct si_context *sctx, struct si_pipe_shader *shader,
Tom Stellard302f53d2012-10-25 13:50:10 -04002330 LLVMModuleRef mod)
2331{
Darren Powellbc866902014-03-31 18:00:28 -04002332 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002333 unsigned i;
2334 uint32_t *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002335 struct radeon_shader_binary binary;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002336 bool dump = r600_can_dump_shader(&sctx->screen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002337 shader->selector ? shader->selector->tokens : NULL);
Darren Powellbc866902014-03-31 18:00:28 -04002338 const char * gpu_family = r600_get_llvm_processor_name(sctx->screen->b.family);
2339
2340 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002341 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002342 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2343
2344 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002345 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002346 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002347 for (i = 0; i < binary.code_size; i+=4 ) {
2348 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2349 binary.code[i + 2], binary.code[i + 1],
2350 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002351 }
2352 }
2353
Tom Stellardd50343d2013-04-04 16:21:06 -04002354 /* XXX: We may be able to emit some of these values directly rather than
2355 * extracting fields to be emitted later.
2356 */
Darren Powellbc866902014-03-31 18:00:28 -04002357 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002358 for (i = 0; i < binary.config_size; i+= 8) {
2359 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2360 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2361 switch (reg) {
2362 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2363 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2364 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2365 case R_00B848_COMPUTE_PGM_RSRC1:
2366 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2367 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2368 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002369 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2370 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2371 break;
2372 case R_00B84C_COMPUTE_PGM_RSRC2:
2373 shader->lds_size = G_00B84C_LDS_SIZE(value);
2374 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002375 case R_0286CC_SPI_PS_INPUT_ENA:
2376 shader->spi_ps_input_ena = value;
2377 break;
2378 default:
2379 fprintf(stderr, "Warning: Compiler emitted unknown "
2380 "config register: 0x%x\n", reg);
2381 break;
2382 }
2383 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002384
2385 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02002386 r600_resource_reference(&shader->bo, NULL);
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002387 shader->bo = si_resource_create_custom(sctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04002388 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002389 if (shader->bo == NULL) {
2390 return -ENOMEM;
2391 }
2392
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002393 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 -08002394 if (SI_BIG_ENDIAN) {
Tom Stellardd50343d2013-04-04 16:21:06 -04002395 for (i = 0; i < binary.code_size / 4; ++i) {
Tom Stellard195ee102014-02-20 09:03:53 -08002396 ptr[i] = util_cpu_to_le32((*(uint32_t*)(binary.code + i*4)));
Tom Stellard302f53d2012-10-25 13:50:10 -04002397 }
2398 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04002399 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002400 }
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002401 sctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002402
Tom Stellard7782d192013-04-04 09:57:13 -07002403 free(binary.code);
2404 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04002405
Darren Powellbc866902014-03-31 18:00:28 -04002406 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002407}
2408
Michel Dänzer404b29d2013-11-21 16:45:28 +09002409/* Generate code for the hardware VS shader stage to go with a geometry shader */
2410static int si_generate_gs_copy_shader(struct si_context *sctx,
2411 struct si_shader_context *si_shader_ctx,
2412 bool dump)
2413{
2414 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2415 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2416 struct lp_build_context *base = &bld_base->base;
2417 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002418 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002419 struct si_shader *gs = &si_shader_ctx->shader->selector->current->shader;
2420 struct si_shader_output_values *outputs;
2421 LLVMValueRef t_list_ptr, t_list;
2422 LLVMValueRef args[9];
2423 int i, r;
2424
2425 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2426
2427 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
2428 si_shader_ctx->gs_for_vs = gs;
2429
2430 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2431
2432 create_meta_data(si_shader_ctx);
2433 create_function(si_shader_ctx);
2434 preload_streamout_buffers(si_shader_ctx);
2435
2436 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002437 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2438 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002439 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002440 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002441
2442 args[0] = t_list;
2443 args[1] = lp_build_mul_imm(uint,
2444 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2445 si_shader_ctx->param_vertex_id),
2446 4);
2447 args[3] = uint->zero;
2448 args[4] = uint->one; /* OFFEN */
2449 args[5] = uint->zero; /* IDXEN */
2450 args[6] = uint->one; /* GLC */
2451 args[7] = uint->one; /* SLC */
2452 args[8] = uint->zero; /* TFE */
2453
2454 /* Fetch vertex data from GSVS ring */
2455 for (i = 0; i < gs->noutput; ++i) {
2456 struct si_shader_output *out = gs->output + i;
2457 unsigned chan;
2458
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002459 shader->output[i] = *out;
2460
Michel Dänzer404b29d2013-11-21 16:45:28 +09002461 outputs[i].name = out->name;
2462 outputs[i].index = out->index;
Michel Dänzer67e385b2014-01-08 17:48:21 +09002463 outputs[i].sid = out->sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002464 outputs[i].usage = out->usage;
2465
2466 for (chan = 0; chan < 4; chan++) {
2467 args[2] = lp_build_const_int32(gallivm,
2468 (i * 4 + chan) *
2469 gs->gs_max_out_vertices * 16 * 4);
2470
2471 outputs[i].values[chan] =
2472 LLVMBuildBitCast(gallivm->builder,
2473 build_intrinsic(gallivm->builder,
2474 "llvm.SI.buffer.load.dword.i32.i32",
2475 LLVMInt32TypeInContext(gallivm->context),
2476 args, 9,
2477 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2478 base->elem_type, "");
2479 }
2480 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002481 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002482
2483 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2484
2485 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2486
2487 if (dump)
2488 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2489
2490 r = si_compile_llvm(sctx, si_shader_ctx->shader,
2491 bld_base->base.gallivm->module);
2492
2493 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2494
2495 FREE(outputs);
2496 return r;
2497}
2498
Tom Stellarda75c6162012-01-06 17:38:37 -05002499int si_pipe_shader_create(
2500 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01002501 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002502{
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002503 struct si_context *sctx = (struct si_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002504 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002505 struct si_shader_context si_shader_ctx;
2506 struct tgsi_shader_info shader_info;
2507 struct lp_build_tgsi_context * bld_base;
2508 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002509 int r = 0;
Michel Dänzere1df0d42014-01-15 12:31:07 +09002510 bool dump = r600_can_dump_shader(&sctx->screen->b, sel->tokens);
2511
2512 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2513 * conversion fails. */
2514 if (dump) {
2515 tgsi_dump(sel->tokens, 0);
2516 si_dump_streamout(&sel->so);
2517 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002518
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002519 assert(shader->shader.noutput == 0);
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002520 assert(shader->shader.nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002521 assert(shader->shader.ninput == 0);
2522
Michel Dänzercfebaf92012-08-31 19:04:08 +02002523 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002524 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2525 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2526
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002527 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002528
Michel Dänzere44dfd42012-11-07 17:33:08 +01002529 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01002530 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002531 bld_base->info = &shader_info;
2532 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002533
2534 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002535 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01002536 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002537 bld_base->op_actions[TGSI_OPCODE_TXB2] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01002538#if HAVE_LLVM >= 0x0304
2539 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
2540#endif
Michel Dänzer36231112013-05-02 09:44:45 +02002541 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002542 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002543 bld_base->op_actions[TGSI_OPCODE_TXL2] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002544 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002545 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002546
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002547#if HAVE_LLVM >= 0x0304
2548 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2549 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
2550#endif
2551
Michel Dänzer404b29d2013-11-21 16:45:28 +09002552 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2553 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2554
Christian Könige4ed5872013-03-21 18:02:52 +01002555 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002556 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002557 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2558 si_shader_ctx.shader = shader;
2559 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002560
Michel Dänzer51f89a02013-12-09 15:33:53 +09002561 switch (si_shader_ctx.type) {
2562 case TGSI_PROCESSOR_VERTEX:
2563 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002564 if (shader->key.vs.as_es) {
2565 si_shader_ctx.gs_for_vs = &sctx->gs_shader->current->shader;
2566 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2567 } else {
2568 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2569 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002570 break;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002571 case TGSI_PROCESSOR_GEOMETRY: {
2572 int i;
2573
2574 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2575 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2576 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
2577
2578 for (i = 0; i < shader_info.num_properties; i++) {
2579 switch (shader_info.properties[i].name) {
2580 case TGSI_PROPERTY_GS_INPUT_PRIM:
2581 shader->shader.gs_input_prim = shader_info.properties[i].data[0];
2582 break;
2583 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
2584 shader->shader.gs_output_prim = shader_info.properties[i].data[0];
2585 break;
2586 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
2587 shader->shader.gs_max_out_vertices = shader_info.properties[i].data[0];
2588 break;
2589 }
2590 }
2591 break;
2592 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002593 case TGSI_PROCESSOR_FRAGMENT:
2594 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2595 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
2596 break;
2597 default:
2598 assert(!"Unsupported shader type");
2599 return -1;
2600 }
2601
Christian König206f0592013-03-20 14:37:21 +01002602 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002603 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002604 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002605 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002606 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002607
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002608 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2609 si_shader_ctx.gs_next_vertex =
2610 lp_build_alloca(bld_base->base.gallivm,
2611 bld_base->uint_bld.elem_type, "");
2612 }
2613
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002614 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002615 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002616 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002617 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002618
2619 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2620
2621 mod = bld_base->base.gallivm->module;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002622 r = si_compile_llvm(sctx, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002623 if (r) {
2624 fprintf(stderr, "LLVM failed to compile shader\n");
2625 goto out;
2626 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002627
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002628 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002629
2630 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2631 shader->gs_copy_shader = CALLOC_STRUCT(si_pipe_shader);
2632 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002633 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002634 si_shader_ctx.shader = shader->gs_copy_shader;
2635 if ((r = si_generate_gs_copy_shader(sctx, &si_shader_ctx, dump))) {
2636 free(shader->gs_copy_shader);
2637 shader->gs_copy_shader = NULL;
2638 goto out;
2639 }
2640 }
2641
Tom Stellarda75c6162012-01-06 17:38:37 -05002642 tgsi_parse_free(&si_shader_ctx.parse);
2643
Michel Dänzer404b29d2013-11-21 16:45:28 +09002644out:
Marek Olšák2fd42002013-10-25 11:45:47 +02002645 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2646 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002647 FREE(si_shader_ctx.resources);
2648 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002649
Tom Stellard302f53d2012-10-25 13:50:10 -04002650 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002651}
2652
2653void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
2654{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002655 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002656}