blob: c54041433bfd58d527fbbb707d58da87f277da28 [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_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100425 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500426 break;
427 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák99df1202014-05-06 19:10:52 +0200428 if (si_shader_ctx->shader->key.ps.interp_at_sample)
429 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
430 else if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100431 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200432 else
Christian König0666ffd2013-03-05 15:07:39 +0100433 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200434 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200435 case TGSI_INTERPOLATE_COLOR:
436 if (si_shader_ctx->shader->key.ps.flatshade) {
437 interp_param = 0;
438 break;
439 }
440 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200441 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák99df1202014-05-06 19:10:52 +0200442 if (si_shader_ctx->shader->key.ps.interp_at_sample)
443 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
444 else 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šák5b06fc32014-05-06 18:12:40 +0200532static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
533{
534 struct gallivm_state *gallivm = &radeon_bld->gallivm;
535 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
536 SI_PARAM_ANCILLARY);
537 value = LLVMBuildLShr(gallivm->builder, value,
538 lp_build_const_int32(gallivm, 8), "");
539 value = LLVMBuildAnd(gallivm->builder, value,
540 lp_build_const_int32(gallivm, 0xf), "");
541 return value;
542}
543
Marek Olšák250aa932014-05-06 14:10:47 +0200544static LLVMValueRef load_const(LLVMBuilderRef builder, LLVMValueRef resource,
545 LLVMValueRef offset, LLVMTypeRef return_type)
546{
547 LLVMValueRef args[2] = {resource, offset};
548
549 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
550 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
551}
552
Christian Könige4ed5872013-03-21 18:02:52 +0100553static void declare_system_value(
554 struct radeon_llvm_context * radeon_bld,
555 unsigned index,
556 const struct tgsi_full_declaration *decl)
557{
Marek Olšák8d03d922013-09-01 23:59:06 +0200558 struct si_shader_context *si_shader_ctx =
559 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200560 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
561 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100562 LLVMValueRef value = 0;
563
564 switch (decl->Semantic.Name) {
565 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200566 value = LLVMGetParam(radeon_bld->main_fn,
567 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100568 break;
569
570 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200571 value = LLVMGetParam(radeon_bld->main_fn,
572 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100573 break;
574
Marek Olšák5b06fc32014-05-06 18:12:40 +0200575 case TGSI_SEMANTIC_SAMPLEID:
576 value = get_sample_id(radeon_bld);
577 break;
578
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200579 case TGSI_SEMANTIC_SAMPLEPOS:
580 {
581 LLVMBuilderRef builder = gallivm->builder;
582 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
583 LLVMValueRef buf_index = lp_build_const_int32(gallivm, NUM_PIPE_CONST_BUFFERS);
584 LLVMValueRef resource = build_indexed_load(si_shader_ctx, desc, buf_index);
585
586 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
587 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
588 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
589
590 LLVMValueRef pos[4] = {
591 load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
592 load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
593 lp_build_const_float(gallivm, 0),
594 lp_build_const_float(gallivm, 0)
595 };
596 value = lp_build_gather_values(gallivm, pos, 4);
597 break;
598 }
599
Christian Könige4ed5872013-03-21 18:02:52 +0100600 default:
601 assert(!"unknown system value");
602 return;
603 }
604
605 radeon_bld->system_values[index] = value;
606}
607
Tom Stellarda75c6162012-01-06 17:38:37 -0500608static LLVMValueRef fetch_constant(
609 struct lp_build_tgsi_context * bld_base,
610 const struct tgsi_full_src_register *reg,
611 enum tgsi_opcode_type type,
612 unsigned swizzle)
613{
Christian König55fe5cc2013-03-04 16:30:06 +0100614 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500615 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100616 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200617 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500618
Christian König0f6cf2b2013-03-15 15:53:25 +0100619 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100620 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500621
Christian König8514f5a2013-02-04 17:46:42 +0100622 if (swizzle == LP_CHAN_ALL) {
623 unsigned chan;
624 LLVMValueRef values[4];
625 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
626 values[chan] = fetch_constant(bld_base, reg, type, chan);
627
628 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
629 }
630
Marek Olšák2fd42002013-10-25 11:45:47 +0200631 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100632 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100633
Marek Olšák2fd42002013-10-25 11:45:47 +0200634 if (!reg->Register.Indirect)
635 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
636
Christian König0f6cf2b2013-03-15 15:53:25 +0100637 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
638 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
639 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200640 addr = lp_build_add(&bld_base->uint_bld, addr,
641 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200642
Marek Olšák250aa932014-05-06 14:10:47 +0200643 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
644 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500645
Christian Königf5298b02013-02-28 14:50:07 +0100646 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500647}
648
Michel Dänzer26c71392012-08-24 12:03:11 +0200649/* Initialize arguments for the shader export intrinsic */
650static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900651 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200652 unsigned target,
653 LLVMValueRef *args)
654{
655 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
656 struct lp_build_context *uint =
657 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
658 struct lp_build_context *base = &bld_base->base;
659 unsigned compressed = 0;
660 unsigned chan;
661
Michel Dänzerf402acd2012-08-22 18:15:36 +0200662 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
663 int cbuf = target - V_008DFC_SQ_EXP_MRT;
664
665 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100666 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100667
668 if (compressed)
669 si_shader_ctx->shader->spi_shader_col_format |=
670 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
671 else
672 si_shader_ctx->shader->spi_shader_col_format |=
673 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200674
675 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200676 }
677 }
678
679 if (compressed) {
680 /* Pixel shader needs to pack output values before export */
681 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900682 args[0] = values[2 * chan];
683 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200684 args[chan + 5] =
685 build_intrinsic(base->gallivm->builder,
686 "llvm.SI.packf16",
687 LLVMInt32TypeInContext(base->gallivm->context),
688 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100689 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100690 args[chan + 7] = args[chan + 5] =
691 LLVMBuildBitCast(base->gallivm->builder,
692 args[chan + 5],
693 LLVMFloatTypeInContext(base->gallivm->context),
694 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200695 }
696
697 /* Set COMPR flag */
698 args[4] = uint->one;
699 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900700 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200701 /* +5 because the first output value will be
702 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900703 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200704
705 /* Clear COMPR flag */
706 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200707 }
708
709 /* XXX: This controls which components of the output
710 * registers actually get exported. (e.g bit 0 means export
711 * X component, bit 1 means export Y component, etc.) I'm
712 * hard coding this to 0xf for now. In the future, we might
713 * want to do something else. */
714 args[0] = lp_build_const_int32(base->gallivm, 0xf);
715
716 /* Specify whether the EXEC mask represents the valid mask */
717 args[1] = uint->zero;
718
719 /* Specify whether this is the last export */
720 args[2] = uint->zero;
721
722 /* Specify the target we are exporting */
723 args[3] = lp_build_const_int32(base->gallivm, target);
724
Michel Dänzer26c71392012-08-24 12:03:11 +0200725 /* XXX: We probably need to keep track of the output
726 * values, so we know what we are passing to the next
727 * stage. */
728}
729
Michel Dänzer404b29d2013-11-21 16:45:28 +0900730/* Load from output pointers and initialize arguments for the shader export intrinsic */
731static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
732 LLVMValueRef *out_ptr,
733 unsigned target,
734 LLVMValueRef *args)
735{
736 struct gallivm_state *gallivm = bld_base->base.gallivm;
737 LLVMValueRef values[4];
738 int i;
739
740 for (i = 0; i < 4; i++)
741 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
742
743 si_llvm_init_export_args(bld_base, values, target, args);
744}
745
Michel Dänzer7708a862012-11-02 15:57:30 +0100746static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900747 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100748{
749 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
750 struct gallivm_state *gallivm = bld_base->base.gallivm;
751
Christian Königa0dca442013-03-22 15:59:22 +0100752 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400753 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
754 SI_PARAM_ALPHA_REF);
755
Michel Dänzer7708a862012-11-02 15:57:30 +0100756 LLVMValueRef alpha_pass =
757 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100758 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900759 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400760 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100761 LLVMValueRef arg =
762 lp_build_select(&bld_base->base,
763 alpha_pass,
764 lp_build_const_float(gallivm, 1.0f),
765 lp_build_const_float(gallivm, -1.0f));
766
767 build_intrinsic(gallivm->builder,
768 "llvm.AMDGPU.kill",
769 LLVMVoidTypeInContext(gallivm->context),
770 &arg, 1, 0);
771 } else {
772 build_intrinsic(gallivm->builder,
773 "llvm.AMDGPU.kilp",
774 LLVMVoidTypeInContext(gallivm->context),
775 NULL, 0, 0);
776 }
777}
778
Michel Dänzere3befbc2013-05-15 18:09:50 +0200779static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900780 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200781{
782 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200783 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200784 struct lp_build_context *base = &bld_base->base;
785 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200786 unsigned reg_index;
787 unsigned chan;
788 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200789 LLVMValueRef base_elt;
790 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšák2fd42002013-10-25 11:45:47 +0200791 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, NUM_PIPE_CONST_BUFFERS);
792 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200793
Michel Dänzere3befbc2013-05-15 18:09:50 +0200794 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200795 LLVMValueRef *args = pos[2 + reg_index];
796
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200797 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
798 continue;
799
800 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
801
Michel Dänzere3befbc2013-05-15 18:09:50 +0200802 args[5] =
803 args[6] =
804 args[7] =
805 args[8] = lp_build_const_float(base->gallivm, 0.0f);
806
807 /* Compute dot products of position and user clip plane vectors */
808 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
809 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200810 args[1] = lp_build_const_int32(base->gallivm,
811 ((reg_index * 4 + chan) * 4 +
812 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200813 base_elt = load_const(base->gallivm->builder, const_resource,
814 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200815 args[5 + chan] =
816 lp_build_add(base, args[5 + chan],
817 lp_build_mul(base, base_elt,
818 out_elts[const_chan]));
819 }
820 }
821
822 args[0] = lp_build_const_int32(base->gallivm, 0xf);
823 args[1] = uint->zero;
824 args[2] = uint->zero;
825 args[3] = lp_build_const_int32(base->gallivm,
826 V_008DFC_SQ_EXP_POS + 2 + reg_index);
827 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200828 }
829}
830
Marek Olšák8d03d922013-09-01 23:59:06 +0200831static void si_dump_streamout(struct pipe_stream_output_info *so)
832{
833 unsigned i;
834
835 if (so->num_outputs)
836 fprintf(stderr, "STREAMOUT\n");
837
838 for (i = 0; i < so->num_outputs; i++) {
839 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
840 so->output[i].start_component;
841 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
842 i, so->output[i].output_buffer,
843 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
844 so->output[i].register_index,
845 mask & 1 ? "x" : "",
846 mask & 2 ? "y" : "",
847 mask & 4 ? "z" : "",
848 mask & 8 ? "w" : "");
849 }
850}
851
852/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
853 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
854 * or v4i32 (num_channels=3,4). */
855static void build_tbuffer_store(struct si_shader_context *shader,
856 LLVMValueRef rsrc,
857 LLVMValueRef vdata,
858 unsigned num_channels,
859 LLVMValueRef vaddr,
860 LLVMValueRef soffset,
861 unsigned inst_offset,
862 unsigned dfmt,
863 unsigned nfmt,
864 unsigned offen,
865 unsigned idxen,
866 unsigned glc,
867 unsigned slc,
868 unsigned tfe)
869{
870 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
871 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
872 LLVMValueRef args[] = {
873 rsrc,
874 vdata,
875 LLVMConstInt(i32, num_channels, 0),
876 vaddr,
877 soffset,
878 LLVMConstInt(i32, inst_offset, 0),
879 LLVMConstInt(i32, dfmt, 0),
880 LLVMConstInt(i32, nfmt, 0),
881 LLVMConstInt(i32, offen, 0),
882 LLVMConstInt(i32, idxen, 0),
883 LLVMConstInt(i32, glc, 0),
884 LLVMConstInt(i32, slc, 0),
885 LLVMConstInt(i32, tfe, 0)
886 };
887
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900888 /* The instruction offset field has 12 bits */
889 assert(offen || inst_offset < (1 << 12));
890
Marek Olšák8d03d922013-09-01 23:59:06 +0200891 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
892 unsigned func = CLAMP(num_channels, 1, 3) - 1;
893 const char *types[] = {"i32", "v2i32", "v4i32"};
894 char name[256];
895 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
896
897 lp_build_intrinsic(gallivm->builder, name,
898 LLVMVoidTypeInContext(gallivm->context),
899 args, Elements(args));
900}
901
902static void build_streamout_store(struct si_shader_context *shader,
903 LLVMValueRef rsrc,
904 LLVMValueRef vdata,
905 unsigned num_channels,
906 LLVMValueRef vaddr,
907 LLVMValueRef soffset,
908 unsigned inst_offset)
909{
910 static unsigned dfmt[] = {
911 V_008F0C_BUF_DATA_FORMAT_32,
912 V_008F0C_BUF_DATA_FORMAT_32_32,
913 V_008F0C_BUF_DATA_FORMAT_32_32_32,
914 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
915 };
916 assert(num_channels >= 1 && num_channels <= 4);
917
918 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
919 inst_offset, dfmt[num_channels-1],
920 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
921}
922
923/* On SI, the vertex shader is responsible for writing streamout data
924 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900925static void si_llvm_emit_streamout(struct si_shader_context *shader,
926 struct si_shader_output_values *outputs,
927 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200928{
929 struct pipe_stream_output_info *so = &shader->shader->selector->so;
930 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
931 LLVMBuilderRef builder = gallivm->builder;
932 int i, j;
933 struct lp_build_if_state if_ctx;
934
935 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
936
937 LLVMValueRef so_param =
938 LLVMGetParam(shader->radeon_bld.main_fn,
939 shader->param_streamout_config);
940
941 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
942 LLVMValueRef so_vtx_count =
943 LLVMBuildAnd(builder,
944 LLVMBuildLShr(builder, so_param,
945 LLVMConstInt(i32, 16, 0), ""),
946 LLVMConstInt(i32, 127, 0), "");
947
948 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
949 NULL, 0, LLVMReadNoneAttribute);
950
951 /* can_emit = tid < so_vtx_count; */
952 LLVMValueRef can_emit =
953 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
954
955 /* Emit the streamout code conditionally. This actually avoids
956 * out-of-bounds buffer access. The hw tells us via the SGPR
957 * (so_vtx_count) which threads are allowed to emit streamout data. */
958 lp_build_if(&if_ctx, gallivm, can_emit);
959 {
960 /* The buffer offset is computed as follows:
961 * ByteOffset = streamout_offset[buffer_id]*4 +
962 * (streamout_write_index + thread_id)*stride[buffer_id] +
963 * attrib_offset
964 */
965
966 LLVMValueRef so_write_index =
967 LLVMGetParam(shader->radeon_bld.main_fn,
968 shader->param_streamout_write_index);
969
970 /* Compute (streamout_write_index + thread_id). */
971 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
972
973 /* Compute the write offset for each enabled buffer. */
974 LLVMValueRef so_write_offset[4] = {};
975 for (i = 0; i < 4; i++) {
976 if (!so->stride[i])
977 continue;
978
979 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
980 shader->param_streamout_offset[i]);
981 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
982
983 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
984 LLVMConstInt(i32, so->stride[i]*4, 0), "");
985 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
986 }
987
Marek Olšák8d03d922013-09-01 23:59:06 +0200988 /* Write streamout data. */
989 for (i = 0; i < so->num_outputs; i++) {
990 unsigned buf_idx = so->output[i].output_buffer;
991 unsigned reg = so->output[i].register_index;
992 unsigned start = so->output[i].start_component;
993 unsigned num_comps = so->output[i].num_components;
994 LLVMValueRef out[4];
995
996 assert(num_comps && num_comps <= 4);
997 if (!num_comps || num_comps > 4)
998 continue;
999
1000 /* Load the output as int. */
1001 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001002 unsigned outidx = 0;
1003
1004 while (outidx < noutput && outputs[outidx].index != reg)
1005 outidx++;
1006
1007 if (outidx < noutput)
1008 out[j] = LLVMBuildBitCast(builder,
1009 outputs[outidx].values[start+j],
1010 i32, "");
1011 else
1012 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +02001013 }
1014
Michel Dänzer67e385b2014-01-08 17:48:21 +09001015 if (!out[0])
1016 continue;
1017
Marek Olšák8d03d922013-09-01 23:59:06 +02001018 /* Pack the output. */
1019 LLVMValueRef vdata = NULL;
1020
1021 switch (num_comps) {
1022 case 1: /* as i32 */
1023 vdata = out[0];
1024 break;
1025 case 2: /* as v2i32 */
1026 case 3: /* as v4i32 (aligned to 4) */
1027 case 4: /* as v4i32 */
1028 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1029 for (j = 0; j < num_comps; j++) {
1030 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1031 LLVMConstInt(i32, j, 0), "");
1032 }
1033 break;
1034 }
1035
1036 build_streamout_store(shader, shader->so_buffers[buf_idx],
1037 vdata, num_comps,
1038 so_write_offset[buf_idx],
1039 LLVMConstInt(i32, 0, 0),
1040 so->output[i].dst_offset*4);
1041 }
1042 }
1043 lp_build_endif(&if_ctx);
1044}
1045
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001046
Michel Dänzer404b29d2013-11-21 16:45:28 +09001047/* Generate export instructions for hardware VS shader stage */
1048static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1049 struct si_shader_output_values *outputs,
1050 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001051{
1052 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +02001053 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001054 struct lp_build_context * base = &bld_base->base;
1055 struct lp_build_context * uint =
1056 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001057 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001058 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001059 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
1060 unsigned semantic_name, semantic_index, semantic_usage;
1061 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001062 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001063 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001064 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001065
Michel Dänzer67e385b2014-01-08 17:48:21 +09001066 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1067 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001068 }
1069
Michel Dänzer404b29d2013-11-21 16:45:28 +09001070 for (i = 0; i < noutput; i++) {
1071 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001072 semantic_index = outputs[i].sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001073 semantic_usage = outputs[i].usage;
Tom Stellarda75c6162012-01-06 17:38:37 -05001074
Michel Dänzer0afeea52013-05-02 14:53:17 +02001075handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001076 /* Select the correct target */
1077 switch(semantic_name) {
1078 case TGSI_SEMANTIC_PSIZE:
1079 shader->vs_out_misc_write = true;
1080 shader->vs_out_point_size = true;
1081 psize_value = outputs[i].values[0];
1082 continue;
1083 case TGSI_SEMANTIC_EDGEFLAG:
1084 shader->vs_out_misc_write = true;
1085 shader->vs_out_edgeflag = true;
1086 edgeflag_value = outputs[i].values[0];
1087 continue;
1088 case TGSI_SEMANTIC_LAYER:
1089 shader->vs_out_misc_write = true;
1090 shader->vs_out_layer = true;
1091 layer_value = outputs[i].values[0];
1092 continue;
1093 case TGSI_SEMANTIC_POSITION:
1094 target = V_008DFC_SQ_EXP_POS;
1095 break;
1096 case TGSI_SEMANTIC_COLOR:
1097 case TGSI_SEMANTIC_BCOLOR:
1098 target = V_008DFC_SQ_EXP_PARAM + param_count;
1099 shader->output[i].param_offset = param_count;
1100 param_count++;
1101 break;
1102 case TGSI_SEMANTIC_CLIPDIST:
1103 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
1104 (1 << semantic_index)))
Marek Olšák053606d2013-11-19 22:07:30 +01001105 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001106 shader->clip_dist_write |=
1107 semantic_usage << (semantic_index << 2);
1108 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1109 break;
1110 case TGSI_SEMANTIC_CLIPVERTEX:
1111 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1112 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001113 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001114 case TGSI_SEMANTIC_FOG:
1115 case TGSI_SEMANTIC_GENERIC:
1116 target = V_008DFC_SQ_EXP_PARAM + param_count;
1117 shader->output[i].param_offset = param_count;
1118 param_count++;
1119 break;
1120 default:
1121 target = 0;
1122 fprintf(stderr,
1123 "Warning: SI unhandled vs output type:%d\n",
1124 semantic_name);
1125 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001126
Michel Dänzer404b29d2013-11-21 16:45:28 +09001127 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001128
Michel Dänzer404b29d2013-11-21 16:45:28 +09001129 if (target >= V_008DFC_SQ_EXP_POS &&
1130 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1131 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1132 args, sizeof(args));
1133 } else {
1134 lp_build_intrinsic(base->gallivm->builder,
1135 "llvm.SI.export",
1136 LLVMVoidTypeInContext(base->gallivm->context),
1137 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001138 }
1139
1140 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1141 semantic_name = TGSI_SEMANTIC_GENERIC;
1142 goto handle_semantic;
1143 }
1144 }
1145
1146 /* We need to add the position output manually if it's missing. */
1147 if (!pos_args[0][0]) {
1148 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1149 pos_args[0][1] = uint->zero; /* EXEC mask */
1150 pos_args[0][2] = uint->zero; /* last export? */
1151 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1152 pos_args[0][4] = uint->zero; /* COMPR flag */
1153 pos_args[0][5] = base->zero; /* X */
1154 pos_args[0][6] = base->zero; /* Y */
1155 pos_args[0][7] = base->zero; /* Z */
1156 pos_args[0][8] = base->one; /* W */
1157 }
1158
1159 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1160 if (shader->vs_out_misc_write) {
1161 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1162 shader->vs_out_point_size |
1163 (shader->vs_out_edgeflag << 1) |
1164 (shader->vs_out_layer << 2));
1165 pos_args[1][1] = uint->zero; /* EXEC mask */
1166 pos_args[1][2] = uint->zero; /* last export? */
1167 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1168 pos_args[1][4] = uint->zero; /* COMPR flag */
1169 pos_args[1][5] = base->zero; /* X */
1170 pos_args[1][6] = base->zero; /* Y */
1171 pos_args[1][7] = base->zero; /* Z */
1172 pos_args[1][8] = base->zero; /* W */
1173
Michel Dänzer404b29d2013-11-21 16:45:28 +09001174 if (shader->vs_out_point_size)
1175 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001176
1177 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001178 /* The output is a float, but the hw expects an integer
1179 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001180 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1181 edgeflag_value,
1182 bld_base->uint_bld.elem_type, "");
1183 edgeflag_value = lp_build_min(&bld_base->int_bld,
1184 edgeflag_value,
1185 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001186
1187 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001188 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1189 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001190 base->elem_type, "");
1191 }
1192
Michel Dänzer404b29d2013-11-21 16:45:28 +09001193 if (shader->vs_out_layer)
1194 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001195 }
1196
1197 for (i = 0; i < 4; i++)
1198 if (pos_args[i][0])
1199 shader->nr_pos_exports++;
1200
1201 pos_idx = 0;
1202 for (i = 0; i < 4; i++) {
1203 if (!pos_args[i][0])
1204 continue;
1205
1206 /* Specify the target we are exporting */
1207 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1208
1209 if (pos_idx == shader->nr_pos_exports)
1210 /* Specify that this is the last export */
1211 pos_args[i][2] = uint->one;
1212
1213 lp_build_intrinsic(base->gallivm->builder,
1214 "llvm.SI.export",
1215 LLVMVoidTypeInContext(base->gallivm->context),
1216 pos_args[i], 9);
1217 }
1218}
1219
Michel Dänzer404b29d2013-11-21 16:45:28 +09001220static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1221{
1222 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1223 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzere884c562014-01-15 15:24:14 +09001224 struct si_shader *es = &si_shader_ctx->shader->shader;
1225 struct si_shader *gs = si_shader_ctx->gs_for_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001226 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1227 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001228 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1229 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001230 LLVMValueRef t_list_ptr;
1231 LLVMValueRef t_list;
1232 unsigned chan;
1233 int i;
1234
1235 while (!tgsi_parse_end_of_tokens(parse)) {
1236 struct tgsi_full_declaration *d =
1237 &parse->FullToken.FullDeclaration;
1238
1239 tgsi_parse_token(parse);
1240
1241 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1242 continue;
1243
Michel Dänzere884c562014-01-15 15:24:14 +09001244 si_store_shader_io_attribs(es, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001245 }
1246
1247 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001248 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1249 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001250 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001251 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001252
Michel Dänzere884c562014-01-15 15:24:14 +09001253 for (i = 0; i < es->noutput; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001254 LLVMValueRef *out_ptr =
Michel Dänzere884c562014-01-15 15:24:14 +09001255 si_shader_ctx->radeon_bld.soa.outputs[es->output[i].index];
1256 int j;
1257
1258 for (j = 0; j < gs->ninput; j++) {
1259 if (gs->input[j].name == es->output[i].name &&
1260 gs->input[j].sid == es->output[i].sid)
1261 break;
1262 }
1263 if (j == gs->ninput)
1264 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001265
1266 for (chan = 0; chan < 4; chan++) {
1267 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001268 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1269
1270 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001271 LLVMGetUndef(i32), soffset,
1272 (4 * gs->input[j].param_offset + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001273 V_008F0C_BUF_DATA_FORMAT_32,
1274 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001275 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001276 }
1277 }
1278}
1279
1280static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1281{
1282 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1283 struct gallivm_state *gallivm = bld_base->base.gallivm;
1284 LLVMValueRef args[2];
1285
1286 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1287 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1288 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1289 LLVMVoidTypeInContext(gallivm->context), args, 2,
1290 LLVMNoUnwindAttribute);
1291}
1292
1293static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1294{
1295 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1296 struct gallivm_state *gallivm = bld_base->base.gallivm;
1297 struct si_pipe_shader *shader = si_shader_ctx->shader;
1298 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1299 struct si_shader_output_values *outputs = NULL;
1300 unsigned noutput = 0;
1301 int i;
1302
1303 while (!tgsi_parse_end_of_tokens(parse)) {
1304 struct tgsi_full_declaration *d =
1305 &parse->FullToken.FullDeclaration;
1306 unsigned index;
1307
1308 tgsi_parse_token(parse);
1309
1310 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1311 continue;
1312
1313 i = si_store_shader_io_attribs(&shader->shader, d);
1314 if (i < 0)
1315 continue;
1316
1317 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1318 (noutput + 1) * sizeof(outputs[0]));
1319 for (index = d->Range.First; index <= d->Range.Last; index++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001320 outputs[noutput].index = index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001321 outputs[noutput].name = d->Semantic.Name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001322 outputs[noutput].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001323 outputs[noutput].usage = d->Declaration.UsageMask;
1324
1325 for (i = 0; i < 4; i++)
1326 outputs[noutput].values[i] =
1327 LLVMBuildLoad(gallivm->builder,
1328 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1329 "");
1330 }
1331 noutput++;
1332 }
1333
1334 si_llvm_export_vs(bld_base, outputs, noutput);
1335 FREE(outputs);
1336}
1337
Michel Dänzer51f89a02013-12-09 15:33:53 +09001338static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1339{
1340 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
1341 struct si_shader * shader = &si_shader_ctx->shader->shader;
1342 struct lp_build_context * base = &bld_base->base;
1343 struct lp_build_context * uint =
1344 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1345 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1346 LLVMValueRef args[9];
1347 LLVMValueRef last_args[9] = { 0 };
1348 unsigned semantic_name;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001349 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001350 int i;
1351
1352 while (!tgsi_parse_end_of_tokens(parse)) {
1353 struct tgsi_full_declaration *d =
1354 &parse->FullToken.FullDeclaration;
1355 unsigned target;
1356 unsigned index;
1357
1358 tgsi_parse_token(parse);
1359
1360 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
1361 parse->FullToken.FullProperty.Property.PropertyName ==
1362 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
1363 shader->fs_write_all = TRUE;
1364
1365 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1366 continue;
1367
1368 i = si_store_shader_io_attribs(shader, d);
1369 if (i < 0)
1370 continue;
1371
1372 semantic_name = d->Semantic.Name;
1373 for (index = d->Range.First; index <= d->Range.Last; index++) {
1374 /* Select the correct target */
1375 switch(semantic_name) {
1376 case TGSI_SEMANTIC_POSITION:
1377 depth_index = index;
1378 continue;
1379 case TGSI_SEMANTIC_STENCIL:
1380 stencil_index = index;
1381 continue;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001382 case TGSI_SEMANTIC_SAMPLEMASK:
1383 samplemask_index = index;
1384 continue;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001385 case TGSI_SEMANTIC_COLOR:
1386 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1387 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001388 LLVMBuildStore(bld_base->base.gallivm->builder,
1389 bld_base->base.one,
1390 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1391
Michel Dänzer51f89a02013-12-09 15:33:53 +09001392 if (d->Semantic.Index == 0 &&
1393 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001394 si_alpha_test(bld_base,
1395 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001396 break;
1397 default:
1398 target = 0;
1399 fprintf(stderr,
1400 "Warning: SI unhandled fs output type:%d\n",
1401 semantic_name);
1402 }
1403
Michel Dänzer404b29d2013-11-21 16:45:28 +09001404 si_llvm_init_export_args_load(bld_base,
1405 si_shader_ctx->radeon_bld.soa.outputs[index],
1406 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001407
1408 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001409 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001410 if (last_args[0]) {
1411 lp_build_intrinsic(base->gallivm->builder,
1412 "llvm.SI.export",
1413 LLVMVoidTypeInContext(base->gallivm->context),
1414 last_args, 9);
1415 }
1416
Marek Olšák0eb528a2013-12-04 13:24:22 +01001417 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001418 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001419
1420 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1421 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1422 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1423 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001424 si_llvm_init_export_args_load(bld_base,
1425 si_shader_ctx->radeon_bld.soa.outputs[index],
1426 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001427 lp_build_intrinsic(base->gallivm->builder,
1428 "llvm.SI.export",
1429 LLVMVoidTypeInContext(base->gallivm->context),
1430 args, 9);
1431 }
1432 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001433 } else {
1434 lp_build_intrinsic(base->gallivm->builder,
1435 "llvm.SI.export",
1436 LLVMVoidTypeInContext(base->gallivm->context),
1437 args, 9);
1438 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001439 }
1440 }
1441
Marek Olšákd0e8b652014-05-06 20:04:31 +02001442 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001443 LLVMValueRef out_ptr;
1444 unsigned mask = 0;
1445
1446 /* Specify the target we are exporting */
1447 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1448
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001449 args[5] = base->zero; /* R, depth */
1450 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1451 args[7] = base->zero; /* B, sample mask */
1452 args[8] = base->zero; /* A, alpha to mask */
1453
Michel Dänzer1a616c12012-11-13 17:35:09 +01001454 if (depth_index >= 0) {
1455 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1456 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1457 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001458 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001459 }
1460
1461 if (stencil_index >= 0) {
1462 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001463 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001464 /* Only setting the stencil component bit (0x2) here
1465 * breaks some stencil piglit tests
1466 */
1467 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001468 si_shader_ctx->shader->db_shader_control |=
1469 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001470 }
1471
Marek Olšákd0e8b652014-05-06 20:04:31 +02001472 if (samplemask_index >= 0) {
1473 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1474 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1475 mask |= 0xf; /* Set all components. */
1476 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1477 }
1478
1479 if (samplemask_index >= 0)
1480 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1481 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001482 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1483 else
1484 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1485
Michel Dänzer1a616c12012-11-13 17:35:09 +01001486 /* Specify which components to enable */
1487 args[0] = lp_build_const_int32(base->gallivm, mask);
1488
1489 args[1] =
1490 args[2] =
1491 args[4] = uint->zero;
1492
1493 if (last_args[0])
1494 lp_build_intrinsic(base->gallivm->builder,
1495 "llvm.SI.export",
1496 LLVMVoidTypeInContext(base->gallivm->context),
1497 args, 9);
1498 else
1499 memcpy(last_args, args, sizeof(args));
1500 }
1501
Michel Dänzer51f89a02013-12-09 15:33:53 +09001502 if (!last_args[0]) {
1503 /* Specify which components to enable */
1504 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001505
Michel Dänzer51f89a02013-12-09 15:33:53 +09001506 /* Specify the target we are exporting */
1507 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001508
Michel Dänzer51f89a02013-12-09 15:33:53 +09001509 /* Set COMPR flag to zero to export data as 32-bit */
1510 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001511
Michel Dänzer51f89a02013-12-09 15:33:53 +09001512 /* dummy bits */
1513 last_args[5]= uint->zero;
1514 last_args[6]= uint->zero;
1515 last_args[7]= uint->zero;
1516 last_args[8]= uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001517
Michel Dänzer51f89a02013-12-09 15:33:53 +09001518 si_shader_ctx->shader->spi_shader_col_format |=
1519 V_028714_SPI_SHADER_32_ABGR;
1520 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001521 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001522
1523 /* Specify whether the EXEC mask represents the valid mask */
1524 last_args[1] = uint->one;
1525
1526 /* Specify that this is the last export */
1527 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1528
1529 lp_build_intrinsic(base->gallivm->builder,
1530 "llvm.SI.export",
1531 LLVMVoidTypeInContext(base->gallivm->context),
1532 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001533}
1534
Marek Olšák4855acd2013-08-06 15:08:54 +02001535static const struct lp_build_tgsi_action txf_action;
1536
1537static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1538 struct lp_build_tgsi_context * bld_base,
1539 struct lp_build_emit_data * emit_data);
1540
Tom Stellarda75c6162012-01-06 17:38:37 -05001541static void tex_fetch_args(
1542 struct lp_build_tgsi_context * bld_base,
1543 struct lp_build_emit_data * emit_data)
1544{
Christian König55fe5cc2013-03-04 16:30:06 +01001545 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001546 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001547 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001548 unsigned opcode = inst->Instruction.Opcode;
1549 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001550 LLVMValueRef coords[4];
1551 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001552 int ref_pos;
1553 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001554 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001555 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001556 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1557 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1558
1559 if (target == TGSI_TEXTURE_BUFFER) {
1560 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1561 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1562 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1563 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1564
1565 /* Truncate v32i8 to v16i8. */
1566 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1567 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1568 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1569 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1570
1571 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1572 emit_data->args[0] = res;
1573 emit_data->args[1] = bld_base->uint_bld.zero;
1574 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1575 emit_data->arg_count = 3;
1576 return;
1577 }
Tom Stellard467f5162012-05-16 15:15:35 -04001578
Michel Dänzer120efee2013-01-25 12:10:11 +01001579 /* Fetch and project texture coordinates */
1580 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001581 for (chan = 0; chan < 3; chan++ ) {
1582 coords[chan] = lp_build_emit_fetch(bld_base,
1583 emit_data->inst, 0,
1584 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001585 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001586 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1587 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001588 coords[chan],
1589 coords[3]);
1590 }
1591
Michel Dänzer120efee2013-01-25 12:10:11 +01001592 if (opcode == TGSI_OPCODE_TXP)
1593 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001594
Michel Dänzer120efee2013-01-25 12:10:11 +01001595 /* Pack LOD bias value */
1596 if (opcode == TGSI_OPCODE_TXB)
1597 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001598 if (opcode == TGSI_OPCODE_TXB2)
1599 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001600
Marek Olšák2484daa2014-04-22 21:23:29 +02001601 if (target == TGSI_TEXTURE_CUBE ||
1602 target == TGSI_TEXTURE_CUBE_ARRAY ||
1603 target == TGSI_TEXTURE_SHADOWCUBE ||
1604 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001605 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001606
1607 /* Pack depth comparison value */
1608 switch (target) {
1609 case TGSI_TEXTURE_SHADOW1D:
1610 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1611 case TGSI_TEXTURE_SHADOW2D:
1612 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001613 case TGSI_TEXTURE_SHADOWCUBE:
1614 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001615 assert(ref_pos >= 0);
1616 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001617 break;
1618 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1619 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001620 }
1621
Michel Dänzera6b83c02013-02-21 16:10:55 +01001622 /* Pack user derivatives */
1623 if (opcode == TGSI_OPCODE_TXD) {
1624 for (chan = 0; chan < 2; chan++) {
1625 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1626 if (num_coords > 1)
1627 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1628 }
1629 }
1630
Michel Dänzer120efee2013-01-25 12:10:11 +01001631 /* Pack texture coordinates */
1632 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001633 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001634 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001635 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001636 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001637
Marek Olšákd2bd6342013-09-18 15:40:21 +02001638 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001639 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001640 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001641 if (opcode == TGSI_OPCODE_TXL2)
1642 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001643
1644 if (count > 16) {
1645 assert(!"Cannot handle more than 16 texture address parameters");
1646 count = 16;
1647 }
1648
1649 for (chan = 0; chan < count; chan++ ) {
1650 address[chan] = LLVMBuildBitCast(gallivm->builder,
1651 address[chan],
1652 LLVMInt32TypeInContext(gallivm->context),
1653 "");
1654 }
1655
Marek Olšák4855acd2013-08-06 15:08:54 +02001656 /* Adjust the sample index according to FMASK.
1657 *
1658 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1659 * which is the identity mapping. Each nibble says which physical sample
1660 * should be fetched to get that sample.
1661 *
1662 * For example, 0x11111100 means there are only 2 samples stored and
1663 * the second sample covers 3/4 of the pixel. When reading samples 0
1664 * and 1, return physical sample 0 (determined by the first two 0s
1665 * in FMASK), otherwise return physical sample 1.
1666 *
1667 * The sample index should be adjusted as follows:
1668 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1669 */
1670 if (target == TGSI_TEXTURE_2D_MSAA ||
1671 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1672 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1673 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001674 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001675 unsigned txf_count = count;
1676
Marek Olšákd2bd6342013-09-18 15:40:21 +02001677 memcpy(txf_address, address, sizeof(txf_address));
1678
1679 if (target == TGSI_TEXTURE_2D_MSAA) {
1680 txf_address[2] = bld_base->uint_bld.zero;
1681 }
1682 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001683
1684 /* Pad to a power-of-two size. */
1685 while (txf_count < util_next_power_of_two(txf_count))
1686 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1687
1688 /* Read FMASK using TXF. */
1689 txf_emit_data.chan = 0;
1690 txf_emit_data.dst_type = LLVMVectorType(
1691 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1692 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1693 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
Marek Olšákd2bd6342013-09-18 15:40:21 +02001694 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1695 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
Marek Olšák4855acd2013-08-06 15:08:54 +02001696 txf_emit_data.arg_count = 3;
1697
1698 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1699
1700 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001701 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1702 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1703
1704 /* Apply the formula. */
1705 LLVMValueRef fmask =
1706 LLVMBuildExtractElement(gallivm->builder,
1707 txf_emit_data.output[0],
1708 uint_bld->zero, "");
1709
Marek Olšákd2bd6342013-09-18 15:40:21 +02001710 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001711
1712 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001713 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001714
1715 LLVMValueRef shifted_fmask =
1716 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1717
1718 LLVMValueRef final_sample =
1719 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1720
1721 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1722 * resource descriptor is 0 (invalid),
1723 */
1724 LLVMValueRef fmask_desc =
1725 LLVMBuildBitCast(gallivm->builder,
1726 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1727 LLVMVectorType(uint_bld->elem_type, 8), "");
1728
1729 LLVMValueRef fmask_word1 =
1730 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1731 uint_bld->one, "");
1732
1733 LLVMValueRef word1_is_nonzero =
1734 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1735 fmask_word1, uint_bld->zero, "");
1736
Marek Olšákd2bd6342013-09-18 15:40:21 +02001737 /* Replace the MSAA sample index. */
1738 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001739 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001740 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001741 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001742
Michel Dänzer36231112013-05-02 09:44:45 +02001743 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001744 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001745
1746 if (opcode == TGSI_OPCODE_TXF) {
1747 /* add tex offsets */
1748 if (inst->Texture.NumOffsets) {
1749 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1750 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1751 const struct tgsi_texture_offset * off = inst->TexOffsets;
1752
1753 assert(inst->Texture.NumOffsets == 1);
1754
Marek Olšákdefedc02013-09-18 15:36:38 +02001755 switch (target) {
1756 case TGSI_TEXTURE_3D:
1757 address[2] = lp_build_add(uint_bld, address[2],
1758 bld->immediates[off->Index][off->SwizzleZ]);
1759 /* fall through */
1760 case TGSI_TEXTURE_2D:
1761 case TGSI_TEXTURE_SHADOW2D:
1762 case TGSI_TEXTURE_RECT:
1763 case TGSI_TEXTURE_SHADOWRECT:
1764 case TGSI_TEXTURE_2D_ARRAY:
1765 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001766 address[1] =
1767 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001768 bld->immediates[off->Index][off->SwizzleY]);
1769 /* fall through */
1770 case TGSI_TEXTURE_1D:
1771 case TGSI_TEXTURE_SHADOW1D:
1772 case TGSI_TEXTURE_1D_ARRAY:
1773 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1774 address[0] =
1775 lp_build_add(uint_bld, address[0],
1776 bld->immediates[off->Index][off->SwizzleX]);
1777 break;
1778 /* texture offsets do not apply to other texture targets */
1779 }
Michel Dänzer36231112013-05-02 09:44:45 +02001780 }
1781
1782 emit_data->dst_type = LLVMVectorType(
1783 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1784 4);
1785
1786 emit_data->arg_count = 3;
1787 } else {
1788 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001789 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001790
1791 emit_data->dst_type = LLVMVectorType(
1792 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1793 4);
1794
1795 emit_data->arg_count = 4;
1796 }
1797
Marek Olšák2484daa2014-04-22 21:23:29 +02001798 /* The fetch opcode has been converted to a 2D array fetch.
1799 * This simplifies the LLVM backend. */
1800 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1801 target = TGSI_TEXTURE_2D_ARRAY;
1802 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1803 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1804
Michel Dänzer36231112013-05-02 09:44:45 +02001805 /* Dimensions */
1806 emit_data->args[emit_data->arg_count - 1] =
1807 lp_build_const_int32(bld_base->base.gallivm, target);
1808
Michel Dänzer120efee2013-01-25 12:10:11 +01001809 /* Pad to power of two vector */
1810 while (count < util_next_power_of_two(count))
1811 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1812
Christian Königccf3e8f2013-03-26 15:09:27 +01001813 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001814}
1815
Michel Dänzer07eddc42013-02-06 15:43:10 +01001816static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1817 struct lp_build_tgsi_context * bld_base,
1818 struct lp_build_emit_data * emit_data)
1819{
1820 struct lp_build_context * base = &bld_base->base;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001821 char intr_name[127];
Michel Dänzer07eddc42013-02-06 15:43:10 +01001822
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001823 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1824 emit_data->output[emit_data->chan] = build_intrinsic(
1825 base->gallivm->builder,
1826 "llvm.SI.vs.load.input", emit_data->dst_type,
1827 emit_data->args, emit_data->arg_count,
1828 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1829 return;
1830 }
1831
Michel Dänzer07eddc42013-02-06 15:43:10 +01001832 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001833 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001834
Christian König44e32242013-03-20 12:10:35 +01001835 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001836 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001837 emit_data->args, emit_data->arg_count,
1838 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001839}
1840
Michel Dänzer0495adb2013-05-06 12:45:14 +02001841static void txq_fetch_args(
1842 struct lp_build_tgsi_context * bld_base,
1843 struct lp_build_emit_data * emit_data)
1844{
1845 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1846 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001847 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02001848 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001849
Marek Olšák2484daa2014-04-22 21:23:29 +02001850 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001851 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1852 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1853
1854 /* Read the size from the buffer descriptor directly. */
1855 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1856 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1857 size = LLVMBuildExtractElement(gallivm->builder, size,
1858 lp_build_const_int32(gallivm, 2), "");
1859 emit_data->args[0] = size;
1860 return;
1861 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02001862
1863 /* Mip level */
1864 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1865
1866 /* Resource */
1867 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1868
Marek Olšák2484daa2014-04-22 21:23:29 +02001869 /* Texture target */
1870 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
1871 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1872 target = TGSI_TEXTURE_2D_ARRAY;
1873
Michel Dänzer0495adb2013-05-06 12:45:14 +02001874 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1875 inst->Texture.Texture);
1876
1877 emit_data->arg_count = 3;
1878
1879 emit_data->dst_type = LLVMVectorType(
1880 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1881 4);
1882}
1883
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001884static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
1885 struct lp_build_tgsi_context * bld_base,
1886 struct lp_build_emit_data * emit_data)
1887{
Marek Olšák2484daa2014-04-22 21:23:29 +02001888 unsigned target = emit_data->inst->Texture.Texture;
1889
1890 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001891 /* Just return the buffer size. */
1892 emit_data->output[emit_data->chan] = emit_data->args[0];
1893 return;
1894 }
1895
1896 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02001897
1898 /* Divide the number of layers by 6 to get the number of cubes. */
1899 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
1900 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1901 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1902 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
1903 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
1904
1905 LLVMValueRef v4 = emit_data->output[emit_data->chan];
1906 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
1907 z = LLVMBuildSDiv(builder, z, six, "");
1908
1909 emit_data->output[emit_data->chan] =
1910 LLVMBuildInsertElement(builder, v4, z, two, "");
1911 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001912}
1913
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001914#if HAVE_LLVM >= 0x0304
1915
1916static void si_llvm_emit_ddxy(
1917 const struct lp_build_tgsi_action * action,
1918 struct lp_build_tgsi_context * bld_base,
1919 struct lp_build_emit_data * emit_data)
1920{
1921 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1922 struct gallivm_state *gallivm = bld_base->base.gallivm;
1923 struct lp_build_context * base = &bld_base->base;
1924 const struct tgsi_full_instruction *inst = emit_data->inst;
1925 unsigned opcode = inst->Instruction.Opcode;
1926 LLVMValueRef indices[2];
1927 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1928 LLVMValueRef tl, trbl, result[4];
1929 LLVMTypeRef i32;
1930 unsigned swizzle[4];
1931 unsigned c;
1932
1933 i32 = LLVMInt32TypeInContext(gallivm->context);
1934
1935 indices[0] = bld_base->uint_bld.zero;
1936 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1937 NULL, 0, LLVMReadNoneAttribute);
1938 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1939 indices, 2, "");
1940
1941 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1942 lp_build_const_int32(gallivm, 0xfffffffc), "");
1943 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1944 indices, 2, "");
1945
1946 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1947 lp_build_const_int32(gallivm,
1948 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1949 "");
1950 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1951 indices, 2, "");
1952
1953 for (c = 0; c < 4; ++c) {
1954 unsigned i;
1955
1956 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1957 for (i = 0; i < c; ++i) {
1958 if (swizzle[i] == swizzle[c]) {
1959 result[c] = result[i];
1960 break;
1961 }
1962 }
1963 if (i != c)
1964 continue;
1965
1966 LLVMBuildStore(gallivm->builder,
1967 LLVMBuildBitCast(gallivm->builder,
1968 lp_build_emit_fetch(bld_base, inst, 0, c),
1969 i32, ""),
1970 store_ptr);
1971
1972 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1973 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1974
1975 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1976 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1977
1978 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1979 }
1980
1981 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1982}
1983
1984#endif /* HAVE_LLVM >= 0x0304 */
1985
Michel Dänzer404b29d2013-11-21 16:45:28 +09001986/* Emit one vertex from the geometry shader */
1987static void si_llvm_emit_vertex(
1988 const struct lp_build_tgsi_action *action,
1989 struct lp_build_tgsi_context *bld_base,
1990 struct lp_build_emit_data *emit_data)
1991{
1992 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001993 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001994 struct si_shader *shader = &si_shader_ctx->shader->shader;
1995 struct gallivm_state *gallivm = bld_base->base.gallivm;
1996 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001997 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1998 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001999 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002000 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002001 LLVMValueRef t_list_ptr;
2002 LLVMValueRef t_list;
2003 LLVMValueRef args[2];
2004 unsigned chan;
2005 int i;
2006
2007 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002008 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2009 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002010 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002011 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002012
2013 if (shader->noutput == 0) {
2014 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
2015
2016 while (!tgsi_parse_end_of_tokens(parse)) {
2017 tgsi_parse_token(parse);
2018
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002019 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
2020 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
2021
2022 if (d->Declaration.File == TGSI_FILE_OUTPUT)
2023 si_store_shader_io_attribs(shader, d);
2024 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09002025 }
2026 }
2027
2028 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002029 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002030
2031 /* If this thread has already emitted the declared maximum number of
2032 * vertices, kill it: excessive vertex emissions are not supposed to
2033 * have any effect, and GS threads have no externally observable
2034 * effects other than emitting vertices.
2035 */
2036 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2037 lp_build_const_int32(gallivm,
2038 shader->gs_max_out_vertices), "");
2039 kill = lp_build_select(&bld_base->base, can_emit,
2040 lp_build_const_float(gallivm, 1.0f),
2041 lp_build_const_float(gallivm, -1.0f));
2042 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2043 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2044
Michel Dänzer404b29d2013-11-21 16:45:28 +09002045 for (i = 0; i < shader->noutput; i++) {
2046 LLVMValueRef *out_ptr =
2047 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
2048
2049 for (chan = 0; chan < 4; chan++) {
2050 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002051 LLVMValueRef voffset =
2052 lp_build_const_int32(gallivm, (i * 4 + chan) *
2053 shader->gs_max_out_vertices);
2054
2055 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2056 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002057
2058 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2059
2060 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2061 voffset, soffset, 0,
2062 V_008F0C_BUF_DATA_FORMAT_32,
2063 V_008F0C_BUF_NUM_FORMAT_UINT,
2064 1, 0, 1, 1, 0);
2065 }
2066 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002067 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2068 lp_build_const_int32(gallivm, 1));
2069 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002070
2071 /* Signal vertex emission */
2072 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2073 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2074 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2075 LLVMVoidTypeInContext(gallivm->context), args, 2,
2076 LLVMNoUnwindAttribute);
2077}
2078
2079/* Cut one primitive from the geometry shader */
2080static void si_llvm_emit_primitive(
2081 const struct lp_build_tgsi_action *action,
2082 struct lp_build_tgsi_context *bld_base,
2083 struct lp_build_emit_data *emit_data)
2084{
2085 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2086 struct gallivm_state *gallivm = bld_base->base.gallivm;
2087 LLVMValueRef args[2];
2088
2089 /* Signal primitive cut */
2090 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2091 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2092 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2093 LLVMVoidTypeInContext(gallivm->context), args, 2,
2094 LLVMNoUnwindAttribute);
2095}
2096
Tom Stellarda75c6162012-01-06 17:38:37 -05002097static const struct lp_build_tgsi_action tex_action = {
2098 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002099 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002100 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05002101};
2102
Michel Dänzer3e205132012-11-06 17:39:01 +01002103static const struct lp_build_tgsi_action txb_action = {
2104 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002105 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002106 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01002107};
2108
Michel Dänzera6b83c02013-02-21 16:10:55 +01002109#if HAVE_LLVM >= 0x0304
2110static const struct lp_build_tgsi_action txd_action = {
2111 .fetch_args = tex_fetch_args,
2112 .emit = build_tex_intrinsic,
2113 .intr_name = "llvm.SI.sampled."
2114};
2115#endif
2116
Michel Dänzer36231112013-05-02 09:44:45 +02002117static const struct lp_build_tgsi_action txf_action = {
2118 .fetch_args = tex_fetch_args,
2119 .emit = build_tex_intrinsic,
2120 .intr_name = "llvm.SI.imageload."
2121};
2122
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002123static const struct lp_build_tgsi_action txl_action = {
2124 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002125 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002126 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002127};
2128
Michel Dänzer0495adb2013-05-06 12:45:14 +02002129static const struct lp_build_tgsi_action txq_action = {
2130 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002131 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002132 .intr_name = "llvm.SI.resinfo"
2133};
2134
Christian König206f0592013-03-20 14:37:21 +01002135static void create_meta_data(struct si_shader_context *si_shader_ctx)
2136{
2137 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2138 LLVMValueRef args[3];
2139
2140 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2141 args[1] = 0;
2142 args[2] = lp_build_const_int32(gallivm, 1);
2143
2144 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2145}
2146
Christian König55fe5cc2013-03-04 16:30:06 +01002147static void create_function(struct si_shader_context *si_shader_ctx)
2148{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002149 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2150 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002151 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzerf8e16012014-01-28 15:39:30 +09002152 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002153 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002154
Christian König55fe5cc2013-03-04 16:30:06 +01002155 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002156 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002157 f32 = LLVMFloatTypeInContext(gallivm->context);
2158 v2i32 = LLVMVectorType(i32, 2);
2159 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01002160
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002161 params[SI_PARAM_CONST] = LLVMPointerType(
2162 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
Michel Dänzerf8e16012014-01-28 15:39:30 +09002163 params[SI_PARAM_RW_BUFFERS] = params[SI_PARAM_CONST];
2164
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002165 /* We assume at most 16 textures per program at the moment.
2166 * This need probably need to be changed to support bindless textures */
2167 params[SI_PARAM_SAMPLER] = LLVMPointerType(
2168 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
2169 params[SI_PARAM_RESOURCE] = LLVMPointerType(
2170 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002171
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002172 switch (si_shader_ctx->type) {
2173 case TGSI_PROCESSOR_VERTEX:
2174 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01002175 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002176 num_params = SI_PARAM_START_INSTANCE+1;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002177 if (shader->key.vs.as_es) {
2178 params[SI_PARAM_ES2GS_OFFSET] = i32;
2179 num_params++;
2180 } else {
2181 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002182
Michel Dänzer404b29d2013-11-21 16:45:28 +09002183 /* Streamout SGPRs. */
2184 if (shader->selector->so.num_outputs) {
2185 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2186 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2187 }
2188 /* A streamout buffer offset is loaded if the stride is non-zero. */
2189 for (i = 0; i < 4; i++) {
2190 if (!shader->selector->so.stride[i])
2191 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002192
Michel Dänzer404b29d2013-11-21 16:45:28 +09002193 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2194 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002195 }
2196
2197 last_sgpr = num_params-1;
2198
2199 /* VGPRs */
2200 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2201 params[num_params++] = i32; /* unused*/
2202 params[num_params++] = i32; /* unused */
2203 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002204 break;
Christian König0666ffd2013-03-05 15:07:39 +01002205
Michel Dänzer404b29d2013-11-21 16:45:28 +09002206 case TGSI_PROCESSOR_GEOMETRY:
2207 params[SI_PARAM_GS2VS_OFFSET] = i32;
2208 params[SI_PARAM_GS_WAVE_ID] = i32;
2209 last_sgpr = SI_PARAM_GS_WAVE_ID;
2210
2211 /* VGPRs */
2212 params[SI_PARAM_VTX0_OFFSET] = i32;
2213 params[SI_PARAM_VTX1_OFFSET] = i32;
2214 params[SI_PARAM_PRIMITIVE_ID] = i32;
2215 params[SI_PARAM_VTX2_OFFSET] = i32;
2216 params[SI_PARAM_VTX3_OFFSET] = i32;
2217 params[SI_PARAM_VTX4_OFFSET] = i32;
2218 params[SI_PARAM_VTX5_OFFSET] = i32;
2219 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2220 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2221 break;
2222
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002223 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002224 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002225 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002226 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002227 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2228 params[SI_PARAM_PERSP_CENTER] = v2i32;
2229 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2230 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2231 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2232 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2233 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2234 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2235 params[SI_PARAM_POS_X_FLOAT] = f32;
2236 params[SI_PARAM_POS_Y_FLOAT] = f32;
2237 params[SI_PARAM_POS_Z_FLOAT] = f32;
2238 params[SI_PARAM_POS_W_FLOAT] = f32;
2239 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002240 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002241 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2242 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002243 num_params = SI_PARAM_POS_FIXED_PT+1;
2244 break;
2245
2246 default:
2247 assert(0 && "unimplemented shader");
2248 return;
Christian Königc4973212013-03-05 12:14:02 +01002249 }
Christian König55fe5cc2013-03-04 16:30:06 +01002250
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002251 assert(num_params <= Elements(params));
2252 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002253 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002254
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002255 for (i = 0; i <= last_sgpr; ++i) {
2256 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002257 switch (i) {
2258 default:
2259 LLVMAddAttribute(P, LLVMInRegAttribute);
2260 break;
2261#if HAVE_LLVM >= 0x0304
2262 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2263 * to move load. Inputs are constant so this is fine. */
2264 case SI_PARAM_CONST:
2265 case SI_PARAM_SAMPLER:
2266 case SI_PARAM_RESOURCE:
2267 LLVMAddAttribute(P, LLVMByValAttribute);
2268 break;
2269#endif
2270 }
Christian Königcf9b31f2013-03-21 18:30:23 +01002271 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002272
2273#if HAVE_LLVM >= 0x0304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002274 if (bld_base->info &&
2275 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2276 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002277 si_shader_ctx->ddxy_lds =
2278 LLVMAddGlobalInAddressSpace(gallivm->module,
2279 LLVMArrayType(i32, 64),
2280 "ddxy_lds",
2281 LOCAL_ADDR_SPACE);
2282#endif
Christian König55fe5cc2013-03-04 16:30:06 +01002283}
Tom Stellarda75c6162012-01-06 17:38:37 -05002284
Christian König0f6cf2b2013-03-15 15:53:25 +01002285static void preload_constants(struct si_shader_context *si_shader_ctx)
2286{
2287 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2288 struct gallivm_state * gallivm = bld_base->base.gallivm;
2289 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002290 unsigned buf;
2291 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002292
Marek Olšák2fd42002013-10-25 11:45:47 +02002293 for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) {
2294 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002295
Marek Olšák2fd42002013-10-25 11:45:47 +02002296 if (num_const == 0)
2297 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002298
Marek Olšák2fd42002013-10-25 11:45:47 +02002299 /* Allocate space for the constant values */
2300 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002301
Marek Olšák2fd42002013-10-25 11:45:47 +02002302 /* Load the resource descriptor */
2303 si_shader_ctx->const_resource[buf] =
2304 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002305
Marek Olšák2fd42002013-10-25 11:45:47 +02002306 /* Load the constants, we rely on the code sinking to do the rest */
2307 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002308 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002309 load_const(gallivm->builder,
2310 si_shader_ctx->const_resource[buf],
2311 lp_build_const_int32(gallivm, i * 4),
2312 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002313 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002314 }
2315}
2316
Christian König1c100182013-03-17 16:02:42 +01002317static void preload_samplers(struct si_shader_context *si_shader_ctx)
2318{
2319 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2320 struct gallivm_state * gallivm = bld_base->base.gallivm;
2321 const struct tgsi_shader_info * info = bld_base->info;
2322
2323 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2324
2325 LLVMValueRef res_ptr, samp_ptr;
2326 LLVMValueRef offset;
2327
2328 if (num_samplers == 0)
2329 return;
2330
2331 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02002332 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002333 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2334
2335 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2336 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2337
2338 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2339 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002340 /* Resource */
2341 offset = lp_build_const_int32(gallivm, i);
2342 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2343
2344 /* Sampler */
2345 offset = lp_build_const_int32(gallivm, i);
2346 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002347
2348 /* FMASK resource */
2349 if (info->is_msaa_sampler[i]) {
2350 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
2351 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
2352 build_indexed_load(si_shader_ctx, res_ptr, offset);
2353 }
Christian König1c100182013-03-17 16:02:42 +01002354 }
2355}
2356
Marek Olšák8d03d922013-09-01 23:59:06 +02002357static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2358{
2359 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2360 struct gallivm_state * gallivm = bld_base->base.gallivm;
2361 unsigned i;
2362
Michel Dänzer67e385b2014-01-08 17:48:21 +09002363 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2364 si_shader_ctx->shader->key.vs.as_es ||
2365 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002366 return;
2367
2368 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002369 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002370
2371 /* Load the resources, we rely on the code sinking to do the rest */
2372 for (i = 0; i < 4; ++i) {
2373 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002374 LLVMValueRef offset = lp_build_const_int32(gallivm,
2375 SI_RW_SO + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002376
2377 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2378 }
2379 }
2380}
2381
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002382int si_compile_llvm(struct si_context *sctx, struct si_pipe_shader *shader,
Tom Stellard302f53d2012-10-25 13:50:10 -04002383 LLVMModuleRef mod)
2384{
Darren Powellbc866902014-03-31 18:00:28 -04002385 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002386 unsigned i;
2387 uint32_t *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002388 struct radeon_shader_binary binary;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002389 bool dump = r600_can_dump_shader(&sctx->screen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002390 shader->selector ? shader->selector->tokens : NULL);
Darren Powellbc866902014-03-31 18:00:28 -04002391 const char * gpu_family = r600_get_llvm_processor_name(sctx->screen->b.family);
2392
2393 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002394 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002395 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2396
2397 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002398 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002399 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002400 for (i = 0; i < binary.code_size; i+=4 ) {
2401 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2402 binary.code[i + 2], binary.code[i + 1],
2403 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002404 }
2405 }
2406
Tom Stellardd50343d2013-04-04 16:21:06 -04002407 /* XXX: We may be able to emit some of these values directly rather than
2408 * extracting fields to be emitted later.
2409 */
Darren Powellbc866902014-03-31 18:00:28 -04002410 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002411 for (i = 0; i < binary.config_size; i+= 8) {
2412 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2413 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2414 switch (reg) {
2415 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2416 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2417 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2418 case R_00B848_COMPUTE_PGM_RSRC1:
2419 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2420 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2421 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002422 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2423 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2424 break;
2425 case R_00B84C_COMPUTE_PGM_RSRC2:
2426 shader->lds_size = G_00B84C_LDS_SIZE(value);
2427 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002428 case R_0286CC_SPI_PS_INPUT_ENA:
2429 shader->spi_ps_input_ena = value;
2430 break;
2431 default:
2432 fprintf(stderr, "Warning: Compiler emitted unknown "
2433 "config register: 0x%x\n", reg);
2434 break;
2435 }
2436 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002437
2438 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02002439 r600_resource_reference(&shader->bo, NULL);
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002440 shader->bo = si_resource_create_custom(sctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04002441 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002442 if (shader->bo == NULL) {
2443 return -ENOMEM;
2444 }
2445
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002446 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 -08002447 if (SI_BIG_ENDIAN) {
Tom Stellardd50343d2013-04-04 16:21:06 -04002448 for (i = 0; i < binary.code_size / 4; ++i) {
Tom Stellard195ee102014-02-20 09:03:53 -08002449 ptr[i] = util_cpu_to_le32((*(uint32_t*)(binary.code + i*4)));
Tom Stellard302f53d2012-10-25 13:50:10 -04002450 }
2451 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04002452 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002453 }
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002454 sctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002455
Tom Stellard7782d192013-04-04 09:57:13 -07002456 free(binary.code);
2457 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04002458
Darren Powellbc866902014-03-31 18:00:28 -04002459 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002460}
2461
Michel Dänzer404b29d2013-11-21 16:45:28 +09002462/* Generate code for the hardware VS shader stage to go with a geometry shader */
2463static int si_generate_gs_copy_shader(struct si_context *sctx,
2464 struct si_shader_context *si_shader_ctx,
2465 bool dump)
2466{
2467 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2468 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2469 struct lp_build_context *base = &bld_base->base;
2470 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002471 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002472 struct si_shader *gs = &si_shader_ctx->shader->selector->current->shader;
2473 struct si_shader_output_values *outputs;
2474 LLVMValueRef t_list_ptr, t_list;
2475 LLVMValueRef args[9];
2476 int i, r;
2477
2478 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2479
2480 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
2481 si_shader_ctx->gs_for_vs = gs;
2482
2483 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2484
2485 create_meta_data(si_shader_ctx);
2486 create_function(si_shader_ctx);
2487 preload_streamout_buffers(si_shader_ctx);
2488
2489 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002490 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2491 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002492 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002493 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002494
2495 args[0] = t_list;
2496 args[1] = lp_build_mul_imm(uint,
2497 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2498 si_shader_ctx->param_vertex_id),
2499 4);
2500 args[3] = uint->zero;
2501 args[4] = uint->one; /* OFFEN */
2502 args[5] = uint->zero; /* IDXEN */
2503 args[6] = uint->one; /* GLC */
2504 args[7] = uint->one; /* SLC */
2505 args[8] = uint->zero; /* TFE */
2506
2507 /* Fetch vertex data from GSVS ring */
2508 for (i = 0; i < gs->noutput; ++i) {
2509 struct si_shader_output *out = gs->output + i;
2510 unsigned chan;
2511
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002512 shader->output[i] = *out;
2513
Michel Dänzer404b29d2013-11-21 16:45:28 +09002514 outputs[i].name = out->name;
2515 outputs[i].index = out->index;
Michel Dänzer67e385b2014-01-08 17:48:21 +09002516 outputs[i].sid = out->sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002517 outputs[i].usage = out->usage;
2518
2519 for (chan = 0; chan < 4; chan++) {
2520 args[2] = lp_build_const_int32(gallivm,
2521 (i * 4 + chan) *
2522 gs->gs_max_out_vertices * 16 * 4);
2523
2524 outputs[i].values[chan] =
2525 LLVMBuildBitCast(gallivm->builder,
2526 build_intrinsic(gallivm->builder,
2527 "llvm.SI.buffer.load.dword.i32.i32",
2528 LLVMInt32TypeInContext(gallivm->context),
2529 args, 9,
2530 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2531 base->elem_type, "");
2532 }
2533 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002534 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002535
2536 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2537
2538 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2539
2540 if (dump)
2541 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2542
2543 r = si_compile_llvm(sctx, si_shader_ctx->shader,
2544 bld_base->base.gallivm->module);
2545
2546 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2547
2548 FREE(outputs);
2549 return r;
2550}
2551
Tom Stellarda75c6162012-01-06 17:38:37 -05002552int si_pipe_shader_create(
2553 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01002554 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002555{
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002556 struct si_context *sctx = (struct si_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002557 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002558 struct si_shader_context si_shader_ctx;
2559 struct tgsi_shader_info shader_info;
2560 struct lp_build_tgsi_context * bld_base;
2561 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002562 int r = 0;
Michel Dänzere1df0d42014-01-15 12:31:07 +09002563 bool dump = r600_can_dump_shader(&sctx->screen->b, sel->tokens);
2564
2565 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2566 * conversion fails. */
2567 if (dump) {
2568 tgsi_dump(sel->tokens, 0);
2569 si_dump_streamout(&sel->so);
2570 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002571
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002572 assert(shader->shader.noutput == 0);
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002573 assert(shader->shader.nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002574 assert(shader->shader.ninput == 0);
2575
Michel Dänzercfebaf92012-08-31 19:04:08 +02002576 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002577 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2578 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2579
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002580 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002581
Michel Dänzere44dfd42012-11-07 17:33:08 +01002582 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01002583 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002584 bld_base->info = &shader_info;
2585 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002586
2587 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002588 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01002589 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002590 bld_base->op_actions[TGSI_OPCODE_TXB2] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01002591#if HAVE_LLVM >= 0x0304
2592 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
2593#endif
Michel Dänzer36231112013-05-02 09:44:45 +02002594 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002595 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002596 bld_base->op_actions[TGSI_OPCODE_TXL2] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002597 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002598 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002599
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002600#if HAVE_LLVM >= 0x0304
2601 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2602 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
2603#endif
2604
Michel Dänzer404b29d2013-11-21 16:45:28 +09002605 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2606 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2607
Christian Könige4ed5872013-03-21 18:02:52 +01002608 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002609 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002610 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2611 si_shader_ctx.shader = shader;
2612 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002613
Michel Dänzer51f89a02013-12-09 15:33:53 +09002614 switch (si_shader_ctx.type) {
2615 case TGSI_PROCESSOR_VERTEX:
2616 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002617 if (shader->key.vs.as_es) {
2618 si_shader_ctx.gs_for_vs = &sctx->gs_shader->current->shader;
2619 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2620 } else {
2621 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2622 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002623 break;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002624 case TGSI_PROCESSOR_GEOMETRY: {
2625 int i;
2626
2627 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2628 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2629 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
2630
2631 for (i = 0; i < shader_info.num_properties; i++) {
2632 switch (shader_info.properties[i].name) {
2633 case TGSI_PROPERTY_GS_INPUT_PRIM:
2634 shader->shader.gs_input_prim = shader_info.properties[i].data[0];
2635 break;
2636 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
2637 shader->shader.gs_output_prim = shader_info.properties[i].data[0];
2638 break;
2639 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
2640 shader->shader.gs_max_out_vertices = shader_info.properties[i].data[0];
2641 break;
2642 }
2643 }
2644 break;
2645 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002646 case TGSI_PROCESSOR_FRAGMENT:
2647 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2648 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
2649 break;
2650 default:
2651 assert(!"Unsupported shader type");
2652 return -1;
2653 }
2654
Christian König206f0592013-03-20 14:37:21 +01002655 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002656 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002657 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002658 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002659 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002660
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002661 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2662 si_shader_ctx.gs_next_vertex =
2663 lp_build_alloca(bld_base->base.gallivm,
2664 bld_base->uint_bld.elem_type, "");
2665 }
2666
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002667 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002668 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002669 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002670 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002671
2672 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2673
2674 mod = bld_base->base.gallivm->module;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002675 r = si_compile_llvm(sctx, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002676 if (r) {
2677 fprintf(stderr, "LLVM failed to compile shader\n");
2678 goto out;
2679 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002680
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002681 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002682
2683 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2684 shader->gs_copy_shader = CALLOC_STRUCT(si_pipe_shader);
2685 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002686 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002687 si_shader_ctx.shader = shader->gs_copy_shader;
2688 if ((r = si_generate_gs_copy_shader(sctx, &si_shader_ctx, dump))) {
2689 free(shader->gs_copy_shader);
2690 shader->gs_copy_shader = NULL;
2691 goto out;
2692 }
2693 }
2694
Tom Stellarda75c6162012-01-06 17:38:37 -05002695 tgsi_parse_free(&si_shader_ctx.parse);
2696
Michel Dänzer404b29d2013-11-21 16:45:28 +09002697out:
Marek Olšák2fd42002013-10-25 11:45:47 +02002698 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2699 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002700 FREE(si_shader_ctx.resources);
2701 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002702
Tom Stellard302f53d2012-10-25 13:50:10 -04002703 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002704}
2705
2706void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
2707{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002708 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002709}