blob: d021caaa6c424011927d589ce5ebcf125975da20 [file] [log] [blame]
Christian Königce40e472012-08-02 12:14:59 +02001/*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
27 */
28
Tom Stellarda75c6162012-01-06 17:38:37 -050029#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020030#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050031#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010032#include "gallivm/lp_bld_logic.h"
Christian König5e616cf2013-03-07 11:58:56 +010033#include "gallivm/lp_bld_arit.h"
Marek Olšák8d03d922013-09-01 23:59:06 +020034#include "gallivm/lp_bld_flow.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050035#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040036#include "radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010037#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010039#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_dump.h"
41
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010042#include "si_pipe.h"
43#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050044#include "sid.h"
45
Tom Stellarda75c6162012-01-06 17:38:37 -050046#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050047
Michel Dänzer404b29d2013-11-21 16:45:28 +090048struct si_shader_output_values
49{
50 LLVMValueRef values[4];
51 unsigned name;
52 unsigned index;
Michel Dänzer67e385b2014-01-08 17:48:21 +090053 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090054 unsigned usage;
55};
56
Tom Stellarda75c6162012-01-06 17:38:37 -050057struct si_shader_context
58{
59 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050060 struct tgsi_parse_context parse;
61 struct tgsi_token * tokens;
62 struct si_pipe_shader *shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +090063 struct si_shader *gs_for_vs;
Tom Stellarda75c6162012-01-06 17:38:37 -050064 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020065 int param_streamout_config;
66 int param_streamout_write_index;
67 int param_streamout_offset[4];
68 int param_vertex_id;
69 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010070 LLVMValueRef const_md;
Marek Olšák2fd42002013-10-25 11:45:47 +020071 LLVMValueRef const_resource[NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020072#if HAVE_LLVM >= 0x0304
73 LLVMValueRef ddxy_lds;
74#endif
Marek Olšák2fd42002013-10-25 11:45:47 +020075 LLVMValueRef *constants[NUM_CONST_BUFFERS];
Christian König1c100182013-03-17 16:02:42 +010076 LLVMValueRef *resources;
77 LLVMValueRef *samplers;
Marek Olšák8d03d922013-09-01 23:59:06 +020078 LLVMValueRef so_buffers[4];
Michel Dänzerf07a96d2014-01-08 18:45:10 +090079 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050080};
81
82static struct si_shader_context * si_shader_context(
83 struct lp_build_tgsi_context * bld_base)
84{
85 return (struct si_shader_context *)bld_base;
86}
87
88
89#define PERSPECTIVE_BASE 0
90#define LINEAR_BASE 9
91
92#define SAMPLE_OFFSET 0
93#define CENTER_OFFSET 2
94#define CENTROID_OFSET 4
95
96#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040097#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020098#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040099#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -0500100
Michel Dänzer404b29d2013-11-21 16:45:28 +0900101
102#define SENDMSG_GS 2
103#define SENDMSG_GS_DONE 3
104
105#define SENDMSG_GS_OP_NOP (0 << 4)
106#define SENDMSG_GS_OP_CUT (1 << 4)
107#define SENDMSG_GS_OP_EMIT (2 << 4)
108#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
109
110
Tom Stellard467f5162012-05-16 15:15:35 -0400111/**
112 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
113 *
114 * @param offset The offset parameter specifies the number of
115 * elements to offset, not the number of bytes or dwords. An element is the
116 * the type pointed to by the base_ptr parameter (e.g. int is the element of
117 * an int* pointer)
118 *
119 * When LLVM lowers the load instruction, it will convert the element offset
120 * into a dword offset automatically.
121 *
122 */
123static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100124 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400125 LLVMValueRef base_ptr,
126 LLVMValueRef offset)
127{
Christian König206f0592013-03-20 14:37:21 +0100128 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400129
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200130 LLVMValueRef indices[2] = {
131 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
132 offset
133 };
Christian König206f0592013-03-20 14:37:21 +0100134 LLVMValueRef computed_ptr = LLVMBuildGEP(
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200135 base->gallivm->builder, base_ptr, indices, 2, "");
Christian König206f0592013-03-20 14:37:21 +0100136
137 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
138 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
139 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400140}
141
Marek Olšákf317ce52013-09-05 15:39:57 +0200142static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100143 struct radeon_llvm_context * radeon_bld,
144 unsigned divisor)
145{
Marek Olšák8d03d922013-09-01 23:59:06 +0200146 struct si_shader_context *si_shader_ctx =
147 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100148 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
149
Marek Olšák8d03d922013-09-01 23:59:06 +0200150 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
151 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100152 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
153 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
154
155 if (divisor > 1)
156 result = LLVMBuildUDiv(gallivm->builder, result,
157 lp_build_const_int32(gallivm, divisor), "");
158
159 return result;
160}
161
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900162static int si_store_shader_io_attribs(struct si_shader *shader,
163 const struct tgsi_full_declaration *d)
164{
165 int i = -1;
166
167 switch (d->Declaration.File) {
168 case TGSI_FILE_INPUT:
169 i = shader->ninput++;
170 assert(i < Elements(shader->input));
171 shader->input[i].name = d->Semantic.Name;
172 shader->input[i].sid = d->Semantic.Index;
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900173 shader->input[i].index = d->Range.First;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900174 shader->input[i].interpolate = d->Interp.Interpolate;
175 shader->input[i].centroid = d->Interp.Centroid;
176 return -1;
177
178 case TGSI_FILE_OUTPUT:
179 i = shader->noutput++;
180 assert(i < Elements(shader->output));
181 shader->output[i].name = d->Semantic.Name;
182 shader->output[i].sid = d->Semantic.Index;
183 shader->output[i].index = d->Range.First;
184 shader->output[i].usage = d->Declaration.UsageMask;
185 break;
186 }
187
188 return i;
189}
190
Tom Stellarda75c6162012-01-06 17:38:37 -0500191static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900192 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500193 unsigned input_index,
194 const struct tgsi_full_declaration *decl)
195{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900196 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
197 struct gallivm_state *gallivm = base->gallivm;
198 struct si_shader_context *si_shader_ctx =
199 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100200 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
201
202 unsigned chan;
203
Tom Stellarda75c6162012-01-06 17:38:37 -0500204 LLVMValueRef t_list_ptr;
205 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400206 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500207 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100208 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400209 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500210 LLVMTypeRef vec4_type;
211 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500212
Tom Stellard467f5162012-05-16 15:15:35 -0400213 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100214 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500215
Michel Dänzer51f89a02013-12-09 15:33:53 +0900216 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400217
Christian König206f0592013-03-20 14:37:21 +0100218 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400219
220 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900221 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500222
Christian Königa0dca442013-03-22 15:59:22 +0100223 if (divisor) {
224 /* Build index from instance ID, start instance and divisor */
225 si_shader_ctx->shader->shader.uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200226 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100227 } else {
228 /* Load the buffer index, which is always stored in VGPR0
229 * for Vertex Shaders */
Marek Olšák8d03d922013-09-01 23:59:06 +0200230 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
231 si_shader_ctx->param_vertex_id);
Christian Königa0dca442013-03-22 15:59:22 +0100232 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500233
234 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400235 args[0] = t_list;
236 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100237 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900238 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100239 "llvm.SI.vs.load.input", vec4_type, args, 3,
240 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500241
242 /* Break up the vec4 into individual components */
243 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900244 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500245 /* XXX: Use a helper function for this. There is one in
246 * tgsi_llvm.c. */
247 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900248 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500249 input, llvm_chan, "");
250 }
251}
252
Michel Dänzer404b29d2013-11-21 16:45:28 +0900253static void declare_input_gs(
254 struct radeon_llvm_context *radeon_bld,
255 unsigned input_index,
256 const struct tgsi_full_declaration *decl)
257{
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900258 struct si_shader_context *si_shader_ctx =
259 si_shader_context(&radeon_bld->soa.bld_base);
260 struct si_shader *shader = &si_shader_ctx->shader->shader;
261
262 si_store_shader_io_attribs(shader, decl);
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900263
264 if (decl->Semantic.Name != TGSI_SEMANTIC_PRIMID)
265 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900266}
267
268static LLVMValueRef fetch_input_gs(
269 struct lp_build_tgsi_context *bld_base,
270 const struct tgsi_full_src_register *reg,
271 enum tgsi_opcode_type type,
272 unsigned swizzle)
273{
274 struct lp_build_context *base = &bld_base->base;
275 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900276 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900277 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
278 struct gallivm_state *gallivm = base->gallivm;
279 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
280 LLVMValueRef vtx_offset;
281 LLVMValueRef t_list_ptr;
282 LLVMValueRef t_list;
283 LLVMValueRef args[9];
284 unsigned vtx_offset_param;
285
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900286 if (swizzle != ~0 &&
287 shader->input[reg->Register.Index].name == TGSI_SEMANTIC_PRIMID) {
288 if (swizzle == 0)
289 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
290 SI_PARAM_PRIMITIVE_ID);
291 else
292 return uint->zero;
293 }
294
Michel Dänzer404b29d2013-11-21 16:45:28 +0900295 if (!reg->Register.Dimension)
296 return NULL;
297
298 if (swizzle == ~0) {
299 LLVMValueRef values[TGSI_NUM_CHANNELS];
300 unsigned chan;
301 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
302 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
303 }
304 return lp_build_gather_values(bld_base->base.gallivm, values,
305 TGSI_NUM_CHANNELS);
306 }
307
308 /* Get the vertex offset parameter */
309 vtx_offset_param = reg->Dimension.Index;
310 if (vtx_offset_param < 2) {
311 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
312 } else {
313 assert(vtx_offset_param < 6);
314 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
315 }
316 vtx_offset = lp_build_mul_imm(uint,
317 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
318 vtx_offset_param),
319 4);
320
321 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +0900322 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
323 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900324 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900325 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900326
327 args[0] = t_list;
328 args[1] = vtx_offset;
329 args[2] = lp_build_const_int32(gallivm,
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900330 ((shader->input[reg->Register.Index].param_offset * 4) +
331 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900332 args[3] = uint->zero;
333 args[4] = uint->one; /* OFFEN */
334 args[5] = uint->zero; /* IDXEN */
335 args[6] = uint->one; /* GLC */
336 args[7] = uint->zero; /* SLC */
337 args[8] = uint->zero; /* TFE */
338
339 return LLVMBuildBitCast(gallivm->builder,
340 build_intrinsic(gallivm->builder,
341 "llvm.SI.buffer.load.dword.i32.i32",
342 i32, args, 9,
343 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
344 tgsi2llvmtype(bld_base, type), "");
345}
346
Tom Stellarda75c6162012-01-06 17:38:37 -0500347static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900348 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500349 unsigned input_index,
350 const struct tgsi_full_declaration *decl)
351{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900352 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
353 struct si_shader_context *si_shader_ctx =
354 si_shader_context(&radeon_bld->soa.bld_base);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200355 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900356 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
357 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400358 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900359 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100360
361 LLVMValueRef interp_param;
362 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500363
364 /* This value is:
365 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
366 * quad begins a new primitive. Bit 0 always needs
367 * to be unset)
368 * [32:16] ParamOffset
369 *
370 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900371 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200372 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500373
Christian König0666ffd2013-03-05 15:07:39 +0100374 unsigned chan;
375
Tom Stellard0fb1e682012-09-06 16:18:11 -0400376 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
377 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400378 unsigned soa_index =
379 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900380 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100381 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100382
383 if (chan == 3)
384 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900385 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100386 LLVMBuildFDiv(gallivm->builder,
387 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900388 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100389 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400390 }
391 return;
392 }
393
Michel Dänzer97078b12012-09-25 12:41:31 +0200394 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
395 LLVMValueRef face, is_face_positive;
396
Christian König0666ffd2013-03-05 15:07:39 +0100397 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
398
Michel Dänzer97078b12012-09-25 12:41:31 +0200399 is_face_positive = LLVMBuildFCmp(gallivm->builder,
400 LLVMRealUGT, face,
401 lp_build_const_float(gallivm, 0.0f),
402 "");
403
Michel Dänzer51f89a02013-12-09 15:33:53 +0900404 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200405 LLVMBuildSelect(gallivm->builder,
406 is_face_positive,
407 lp_build_const_float(gallivm, 1.0f),
408 lp_build_const_float(gallivm, 0.0f),
409 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900410 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
411 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200412 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900413 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200414 lp_build_const_float(gallivm, 1.0f);
415
416 return;
417 }
418
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900419 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200420 attr_number = lp_build_const_int32(gallivm,
421 shader->input[input_index].param_offset);
422
Francisco Jerez12799232012-04-30 18:27:52 +0200423 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500424 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100425 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100426 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200427 } else {
428 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100429 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200430 else
Christian König0666ffd2013-03-05 15:07:39 +0100431 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200432 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500433 break;
434 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100435 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500436 break;
437 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200438 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100439 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200440 else
Christian König0666ffd2013-03-05 15:07:39 +0100441 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200442 break;
443 case TGSI_INTERPOLATE_PERSPECTIVE:
444 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100445 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200446 else
Christian König0666ffd2013-03-05 15:07:39 +0100447 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500448 break;
449 default:
450 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
451 return;
452 }
453
Christian König0666ffd2013-03-05 15:07:39 +0100454 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
455
Tom Stellarda75c6162012-01-06 17:38:37 -0500456 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200457 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100458 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100459 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200460 LLVMValueRef face, is_face_positive;
461 LLVMValueRef back_attr_number =
462 lp_build_const_int32(gallivm,
463 shader->input[input_index].param_offset + 1);
464
Christian König0666ffd2013-03-05 15:07:39 +0100465 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
466
Michel Dänzer691f08d2012-09-06 18:03:38 +0200467 is_face_positive = LLVMBuildFCmp(gallivm->builder,
468 LLVMRealUGT, face,
469 lp_build_const_float(gallivm, 0.0f),
470 "");
471
Tom Stellarda75c6162012-01-06 17:38:37 -0500472 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100473 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200474 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
475 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
476 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
477 LLVMValueRef front, back;
478
479 args[0] = llvm_chan;
480 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900481 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100482 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100483 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200484
485 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900486 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100487 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100488 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200489
Michel Dänzer51f89a02013-12-09 15:33:53 +0900490 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200491 LLVMBuildSelect(gallivm->builder,
492 is_face_positive,
493 front,
494 back,
495 "");
496 }
497
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900498 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200499 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
500 LLVMValueRef args[4];
501
502 args[0] = uint->zero;
503 args[1] = attr_number;
504 args[2] = params;
505 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900506 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
507 build_intrinsic(gallivm->builder, intr_name,
508 input_type, args, args[3] ? 4 : 3,
509 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
510 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
511 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200512 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900513 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200514 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200515 } else {
516 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100517 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200518 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
519 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
520 args[0] = llvm_chan;
521 args[1] = attr_number;
522 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100523 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900524 radeon_bld->inputs[soa_index] =
525 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100526 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100527 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200528 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500529 }
530}
531
Marek Olšá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);
Christian Könige4ed5872013-03-21 18:02:52 +0100560 LLVMValueRef value = 0;
561
562 switch (decl->Semantic.Name) {
563 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200564 value = LLVMGetParam(radeon_bld->main_fn,
565 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100566 break;
567
568 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200569 value = LLVMGetParam(radeon_bld->main_fn,
570 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100571 break;
572
Marek Olšák5b06fc32014-05-06 18:12:40 +0200573 case TGSI_SEMANTIC_SAMPLEID:
574 value = get_sample_id(radeon_bld);
575 break;
576
Christian Könige4ed5872013-03-21 18:02:52 +0100577 default:
578 assert(!"unknown system value");
579 return;
580 }
581
582 radeon_bld->system_values[index] = value;
583}
584
Tom Stellarda75c6162012-01-06 17:38:37 -0500585static LLVMValueRef fetch_constant(
586 struct lp_build_tgsi_context * bld_base,
587 const struct tgsi_full_src_register *reg,
588 enum tgsi_opcode_type type,
589 unsigned swizzle)
590{
Christian König55fe5cc2013-03-04 16:30:06 +0100591 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500592 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100593 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200594 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500595
Christian König0f6cf2b2013-03-15 15:53:25 +0100596 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100597 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500598
Christian König8514f5a2013-02-04 17:46:42 +0100599 if (swizzle == LP_CHAN_ALL) {
600 unsigned chan;
601 LLVMValueRef values[4];
602 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
603 values[chan] = fetch_constant(bld_base, reg, type, chan);
604
605 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
606 }
607
Marek Olšák2fd42002013-10-25 11:45:47 +0200608 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100609 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100610
Marek Olšák2fd42002013-10-25 11:45:47 +0200611 if (!reg->Register.Indirect)
612 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
613
Christian König0f6cf2b2013-03-15 15:53:25 +0100614 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
615 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
616 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200617 addr = lp_build_add(&bld_base->uint_bld, addr,
618 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200619
Marek Olšák250aa932014-05-06 14:10:47 +0200620 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
621 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500622
Christian Königf5298b02013-02-28 14:50:07 +0100623 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500624}
625
Michel Dänzer26c71392012-08-24 12:03:11 +0200626/* Initialize arguments for the shader export intrinsic */
627static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900628 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200629 unsigned target,
630 LLVMValueRef *args)
631{
632 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
633 struct lp_build_context *uint =
634 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
635 struct lp_build_context *base = &bld_base->base;
636 unsigned compressed = 0;
637 unsigned chan;
638
Michel Dänzerf402acd2012-08-22 18:15:36 +0200639 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
640 int cbuf = target - V_008DFC_SQ_EXP_MRT;
641
642 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100643 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100644
645 if (compressed)
646 si_shader_ctx->shader->spi_shader_col_format |=
647 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
648 else
649 si_shader_ctx->shader->spi_shader_col_format |=
650 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200651
652 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200653 }
654 }
655
656 if (compressed) {
657 /* Pixel shader needs to pack output values before export */
658 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900659 args[0] = values[2 * chan];
660 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200661 args[chan + 5] =
662 build_intrinsic(base->gallivm->builder,
663 "llvm.SI.packf16",
664 LLVMInt32TypeInContext(base->gallivm->context),
665 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100666 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100667 args[chan + 7] = args[chan + 5] =
668 LLVMBuildBitCast(base->gallivm->builder,
669 args[chan + 5],
670 LLVMFloatTypeInContext(base->gallivm->context),
671 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200672 }
673
674 /* Set COMPR flag */
675 args[4] = uint->one;
676 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900677 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200678 /* +5 because the first output value will be
679 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900680 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200681
682 /* Clear COMPR flag */
683 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200684 }
685
686 /* XXX: This controls which components of the output
687 * registers actually get exported. (e.g bit 0 means export
688 * X component, bit 1 means export Y component, etc.) I'm
689 * hard coding this to 0xf for now. In the future, we might
690 * want to do something else. */
691 args[0] = lp_build_const_int32(base->gallivm, 0xf);
692
693 /* Specify whether the EXEC mask represents the valid mask */
694 args[1] = uint->zero;
695
696 /* Specify whether this is the last export */
697 args[2] = uint->zero;
698
699 /* Specify the target we are exporting */
700 args[3] = lp_build_const_int32(base->gallivm, target);
701
Michel Dänzer26c71392012-08-24 12:03:11 +0200702 /* XXX: We probably need to keep track of the output
703 * values, so we know what we are passing to the next
704 * stage. */
705}
706
Michel Dänzer404b29d2013-11-21 16:45:28 +0900707/* Load from output pointers and initialize arguments for the shader export intrinsic */
708static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
709 LLVMValueRef *out_ptr,
710 unsigned target,
711 LLVMValueRef *args)
712{
713 struct gallivm_state *gallivm = bld_base->base.gallivm;
714 LLVMValueRef values[4];
715 int i;
716
717 for (i = 0; i < 4; i++)
718 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
719
720 si_llvm_init_export_args(bld_base, values, target, args);
721}
722
Michel Dänzer7708a862012-11-02 15:57:30 +0100723static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900724 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100725{
726 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
727 struct gallivm_state *gallivm = bld_base->base.gallivm;
728
Christian Königa0dca442013-03-22 15:59:22 +0100729 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400730 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
731 SI_PARAM_ALPHA_REF);
732
Michel Dänzer7708a862012-11-02 15:57:30 +0100733 LLVMValueRef alpha_pass =
734 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100735 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900736 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400737 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100738 LLVMValueRef arg =
739 lp_build_select(&bld_base->base,
740 alpha_pass,
741 lp_build_const_float(gallivm, 1.0f),
742 lp_build_const_float(gallivm, -1.0f));
743
744 build_intrinsic(gallivm->builder,
745 "llvm.AMDGPU.kill",
746 LLVMVoidTypeInContext(gallivm->context),
747 &arg, 1, 0);
748 } else {
749 build_intrinsic(gallivm->builder,
750 "llvm.AMDGPU.kilp",
751 LLVMVoidTypeInContext(gallivm->context),
752 NULL, 0, 0);
753 }
754}
755
Michel Dänzere3befbc2013-05-15 18:09:50 +0200756static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900757 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200758{
759 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200760 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200761 struct lp_build_context *base = &bld_base->base;
762 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200763 unsigned reg_index;
764 unsigned chan;
765 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200766 LLVMValueRef base_elt;
767 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšák2fd42002013-10-25 11:45:47 +0200768 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, NUM_PIPE_CONST_BUFFERS);
769 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200770
Michel Dänzere3befbc2013-05-15 18:09:50 +0200771 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200772 LLVMValueRef *args = pos[2 + reg_index];
773
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200774 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
775 continue;
776
777 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
778
Michel Dänzere3befbc2013-05-15 18:09:50 +0200779 args[5] =
780 args[6] =
781 args[7] =
782 args[8] = lp_build_const_float(base->gallivm, 0.0f);
783
784 /* Compute dot products of position and user clip plane vectors */
785 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
786 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200787 args[1] = lp_build_const_int32(base->gallivm,
788 ((reg_index * 4 + chan) * 4 +
789 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200790 base_elt = load_const(base->gallivm->builder, const_resource,
791 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200792 args[5 + chan] =
793 lp_build_add(base, args[5 + chan],
794 lp_build_mul(base, base_elt,
795 out_elts[const_chan]));
796 }
797 }
798
799 args[0] = lp_build_const_int32(base->gallivm, 0xf);
800 args[1] = uint->zero;
801 args[2] = uint->zero;
802 args[3] = lp_build_const_int32(base->gallivm,
803 V_008DFC_SQ_EXP_POS + 2 + reg_index);
804 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200805 }
806}
807
Marek Olšák8d03d922013-09-01 23:59:06 +0200808static void si_dump_streamout(struct pipe_stream_output_info *so)
809{
810 unsigned i;
811
812 if (so->num_outputs)
813 fprintf(stderr, "STREAMOUT\n");
814
815 for (i = 0; i < so->num_outputs; i++) {
816 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
817 so->output[i].start_component;
818 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
819 i, so->output[i].output_buffer,
820 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
821 so->output[i].register_index,
822 mask & 1 ? "x" : "",
823 mask & 2 ? "y" : "",
824 mask & 4 ? "z" : "",
825 mask & 8 ? "w" : "");
826 }
827}
828
829/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
830 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
831 * or v4i32 (num_channels=3,4). */
832static void build_tbuffer_store(struct si_shader_context *shader,
833 LLVMValueRef rsrc,
834 LLVMValueRef vdata,
835 unsigned num_channels,
836 LLVMValueRef vaddr,
837 LLVMValueRef soffset,
838 unsigned inst_offset,
839 unsigned dfmt,
840 unsigned nfmt,
841 unsigned offen,
842 unsigned idxen,
843 unsigned glc,
844 unsigned slc,
845 unsigned tfe)
846{
847 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
848 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
849 LLVMValueRef args[] = {
850 rsrc,
851 vdata,
852 LLVMConstInt(i32, num_channels, 0),
853 vaddr,
854 soffset,
855 LLVMConstInt(i32, inst_offset, 0),
856 LLVMConstInt(i32, dfmt, 0),
857 LLVMConstInt(i32, nfmt, 0),
858 LLVMConstInt(i32, offen, 0),
859 LLVMConstInt(i32, idxen, 0),
860 LLVMConstInt(i32, glc, 0),
861 LLVMConstInt(i32, slc, 0),
862 LLVMConstInt(i32, tfe, 0)
863 };
864
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900865 /* The instruction offset field has 12 bits */
866 assert(offen || inst_offset < (1 << 12));
867
Marek Olšák8d03d922013-09-01 23:59:06 +0200868 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
869 unsigned func = CLAMP(num_channels, 1, 3) - 1;
870 const char *types[] = {"i32", "v2i32", "v4i32"};
871 char name[256];
872 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
873
874 lp_build_intrinsic(gallivm->builder, name,
875 LLVMVoidTypeInContext(gallivm->context),
876 args, Elements(args));
877}
878
879static void build_streamout_store(struct si_shader_context *shader,
880 LLVMValueRef rsrc,
881 LLVMValueRef vdata,
882 unsigned num_channels,
883 LLVMValueRef vaddr,
884 LLVMValueRef soffset,
885 unsigned inst_offset)
886{
887 static unsigned dfmt[] = {
888 V_008F0C_BUF_DATA_FORMAT_32,
889 V_008F0C_BUF_DATA_FORMAT_32_32,
890 V_008F0C_BUF_DATA_FORMAT_32_32_32,
891 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
892 };
893 assert(num_channels >= 1 && num_channels <= 4);
894
895 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
896 inst_offset, dfmt[num_channels-1],
897 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
898}
899
900/* On SI, the vertex shader is responsible for writing streamout data
901 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900902static void si_llvm_emit_streamout(struct si_shader_context *shader,
903 struct si_shader_output_values *outputs,
904 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200905{
906 struct pipe_stream_output_info *so = &shader->shader->selector->so;
907 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
908 LLVMBuilderRef builder = gallivm->builder;
909 int i, j;
910 struct lp_build_if_state if_ctx;
911
912 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
913
914 LLVMValueRef so_param =
915 LLVMGetParam(shader->radeon_bld.main_fn,
916 shader->param_streamout_config);
917
918 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
919 LLVMValueRef so_vtx_count =
920 LLVMBuildAnd(builder,
921 LLVMBuildLShr(builder, so_param,
922 LLVMConstInt(i32, 16, 0), ""),
923 LLVMConstInt(i32, 127, 0), "");
924
925 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
926 NULL, 0, LLVMReadNoneAttribute);
927
928 /* can_emit = tid < so_vtx_count; */
929 LLVMValueRef can_emit =
930 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
931
932 /* Emit the streamout code conditionally. This actually avoids
933 * out-of-bounds buffer access. The hw tells us via the SGPR
934 * (so_vtx_count) which threads are allowed to emit streamout data. */
935 lp_build_if(&if_ctx, gallivm, can_emit);
936 {
937 /* The buffer offset is computed as follows:
938 * ByteOffset = streamout_offset[buffer_id]*4 +
939 * (streamout_write_index + thread_id)*stride[buffer_id] +
940 * attrib_offset
941 */
942
943 LLVMValueRef so_write_index =
944 LLVMGetParam(shader->radeon_bld.main_fn,
945 shader->param_streamout_write_index);
946
947 /* Compute (streamout_write_index + thread_id). */
948 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
949
950 /* Compute the write offset for each enabled buffer. */
951 LLVMValueRef so_write_offset[4] = {};
952 for (i = 0; i < 4; i++) {
953 if (!so->stride[i])
954 continue;
955
956 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
957 shader->param_streamout_offset[i]);
958 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
959
960 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
961 LLVMConstInt(i32, so->stride[i]*4, 0), "");
962 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
963 }
964
Marek Olšák8d03d922013-09-01 23:59:06 +0200965 /* Write streamout data. */
966 for (i = 0; i < so->num_outputs; i++) {
967 unsigned buf_idx = so->output[i].output_buffer;
968 unsigned reg = so->output[i].register_index;
969 unsigned start = so->output[i].start_component;
970 unsigned num_comps = so->output[i].num_components;
971 LLVMValueRef out[4];
972
973 assert(num_comps && num_comps <= 4);
974 if (!num_comps || num_comps > 4)
975 continue;
976
977 /* Load the output as int. */
978 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +0900979 unsigned outidx = 0;
980
981 while (outidx < noutput && outputs[outidx].index != reg)
982 outidx++;
983
984 if (outidx < noutput)
985 out[j] = LLVMBuildBitCast(builder,
986 outputs[outidx].values[start+j],
987 i32, "");
988 else
989 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +0200990 }
991
Michel Dänzer67e385b2014-01-08 17:48:21 +0900992 if (!out[0])
993 continue;
994
Marek Olšák8d03d922013-09-01 23:59:06 +0200995 /* Pack the output. */
996 LLVMValueRef vdata = NULL;
997
998 switch (num_comps) {
999 case 1: /* as i32 */
1000 vdata = out[0];
1001 break;
1002 case 2: /* as v2i32 */
1003 case 3: /* as v4i32 (aligned to 4) */
1004 case 4: /* as v4i32 */
1005 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1006 for (j = 0; j < num_comps; j++) {
1007 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1008 LLVMConstInt(i32, j, 0), "");
1009 }
1010 break;
1011 }
1012
1013 build_streamout_store(shader, shader->so_buffers[buf_idx],
1014 vdata, num_comps,
1015 so_write_offset[buf_idx],
1016 LLVMConstInt(i32, 0, 0),
1017 so->output[i].dst_offset*4);
1018 }
1019 }
1020 lp_build_endif(&if_ctx);
1021}
1022
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001023
Michel Dänzer404b29d2013-11-21 16:45:28 +09001024/* Generate export instructions for hardware VS shader stage */
1025static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1026 struct si_shader_output_values *outputs,
1027 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001028{
1029 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +02001030 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001031 struct lp_build_context * base = &bld_base->base;
1032 struct lp_build_context * uint =
1033 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001034 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001035 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001036 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
1037 unsigned semantic_name, semantic_index, semantic_usage;
1038 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001039 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001040 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001041 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001042
Michel Dänzer67e385b2014-01-08 17:48:21 +09001043 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1044 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001045 }
1046
Michel Dänzer404b29d2013-11-21 16:45:28 +09001047 for (i = 0; i < noutput; i++) {
1048 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001049 semantic_index = outputs[i].sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001050 semantic_usage = outputs[i].usage;
Tom Stellarda75c6162012-01-06 17:38:37 -05001051
Michel Dänzer0afeea52013-05-02 14:53:17 +02001052handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001053 /* Select the correct target */
1054 switch(semantic_name) {
1055 case TGSI_SEMANTIC_PSIZE:
1056 shader->vs_out_misc_write = true;
1057 shader->vs_out_point_size = true;
1058 psize_value = outputs[i].values[0];
1059 continue;
1060 case TGSI_SEMANTIC_EDGEFLAG:
1061 shader->vs_out_misc_write = true;
1062 shader->vs_out_edgeflag = true;
1063 edgeflag_value = outputs[i].values[0];
1064 continue;
1065 case TGSI_SEMANTIC_LAYER:
1066 shader->vs_out_misc_write = true;
1067 shader->vs_out_layer = true;
1068 layer_value = outputs[i].values[0];
1069 continue;
1070 case TGSI_SEMANTIC_POSITION:
1071 target = V_008DFC_SQ_EXP_POS;
1072 break;
1073 case TGSI_SEMANTIC_COLOR:
1074 case TGSI_SEMANTIC_BCOLOR:
1075 target = V_008DFC_SQ_EXP_PARAM + param_count;
1076 shader->output[i].param_offset = param_count;
1077 param_count++;
1078 break;
1079 case TGSI_SEMANTIC_CLIPDIST:
1080 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
1081 (1 << semantic_index)))
Marek Olšák053606d2013-11-19 22:07:30 +01001082 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001083 shader->clip_dist_write |=
1084 semantic_usage << (semantic_index << 2);
1085 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1086 break;
1087 case TGSI_SEMANTIC_CLIPVERTEX:
1088 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1089 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001090 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001091 case TGSI_SEMANTIC_FOG:
1092 case TGSI_SEMANTIC_GENERIC:
1093 target = V_008DFC_SQ_EXP_PARAM + param_count;
1094 shader->output[i].param_offset = param_count;
1095 param_count++;
1096 break;
1097 default:
1098 target = 0;
1099 fprintf(stderr,
1100 "Warning: SI unhandled vs output type:%d\n",
1101 semantic_name);
1102 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001103
Michel Dänzer404b29d2013-11-21 16:45:28 +09001104 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001105
Michel Dänzer404b29d2013-11-21 16:45:28 +09001106 if (target >= V_008DFC_SQ_EXP_POS &&
1107 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1108 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1109 args, sizeof(args));
1110 } else {
1111 lp_build_intrinsic(base->gallivm->builder,
1112 "llvm.SI.export",
1113 LLVMVoidTypeInContext(base->gallivm->context),
1114 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001115 }
1116
1117 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1118 semantic_name = TGSI_SEMANTIC_GENERIC;
1119 goto handle_semantic;
1120 }
1121 }
1122
1123 /* We need to add the position output manually if it's missing. */
1124 if (!pos_args[0][0]) {
1125 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1126 pos_args[0][1] = uint->zero; /* EXEC mask */
1127 pos_args[0][2] = uint->zero; /* last export? */
1128 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1129 pos_args[0][4] = uint->zero; /* COMPR flag */
1130 pos_args[0][5] = base->zero; /* X */
1131 pos_args[0][6] = base->zero; /* Y */
1132 pos_args[0][7] = base->zero; /* Z */
1133 pos_args[0][8] = base->one; /* W */
1134 }
1135
1136 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1137 if (shader->vs_out_misc_write) {
1138 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1139 shader->vs_out_point_size |
1140 (shader->vs_out_edgeflag << 1) |
1141 (shader->vs_out_layer << 2));
1142 pos_args[1][1] = uint->zero; /* EXEC mask */
1143 pos_args[1][2] = uint->zero; /* last export? */
1144 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1145 pos_args[1][4] = uint->zero; /* COMPR flag */
1146 pos_args[1][5] = base->zero; /* X */
1147 pos_args[1][6] = base->zero; /* Y */
1148 pos_args[1][7] = base->zero; /* Z */
1149 pos_args[1][8] = base->zero; /* W */
1150
Michel Dänzer404b29d2013-11-21 16:45:28 +09001151 if (shader->vs_out_point_size)
1152 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001153
1154 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001155 /* The output is a float, but the hw expects an integer
1156 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001157 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1158 edgeflag_value,
1159 bld_base->uint_bld.elem_type, "");
1160 edgeflag_value = lp_build_min(&bld_base->int_bld,
1161 edgeflag_value,
1162 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001163
1164 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001165 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1166 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001167 base->elem_type, "");
1168 }
1169
Michel Dänzer404b29d2013-11-21 16:45:28 +09001170 if (shader->vs_out_layer)
1171 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001172 }
1173
1174 for (i = 0; i < 4; i++)
1175 if (pos_args[i][0])
1176 shader->nr_pos_exports++;
1177
1178 pos_idx = 0;
1179 for (i = 0; i < 4; i++) {
1180 if (!pos_args[i][0])
1181 continue;
1182
1183 /* Specify the target we are exporting */
1184 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1185
1186 if (pos_idx == shader->nr_pos_exports)
1187 /* Specify that this is the last export */
1188 pos_args[i][2] = uint->one;
1189
1190 lp_build_intrinsic(base->gallivm->builder,
1191 "llvm.SI.export",
1192 LLVMVoidTypeInContext(base->gallivm->context),
1193 pos_args[i], 9);
1194 }
1195}
1196
Michel Dänzer404b29d2013-11-21 16:45:28 +09001197static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1198{
1199 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1200 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzere884c562014-01-15 15:24:14 +09001201 struct si_shader *es = &si_shader_ctx->shader->shader;
1202 struct si_shader *gs = si_shader_ctx->gs_for_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001203 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1204 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001205 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1206 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001207 LLVMValueRef t_list_ptr;
1208 LLVMValueRef t_list;
1209 unsigned chan;
1210 int i;
1211
1212 while (!tgsi_parse_end_of_tokens(parse)) {
1213 struct tgsi_full_declaration *d =
1214 &parse->FullToken.FullDeclaration;
1215
1216 tgsi_parse_token(parse);
1217
1218 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1219 continue;
1220
Michel Dänzere884c562014-01-15 15:24:14 +09001221 si_store_shader_io_attribs(es, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001222 }
1223
1224 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001225 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1226 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001227 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001228 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001229
Michel Dänzere884c562014-01-15 15:24:14 +09001230 for (i = 0; i < es->noutput; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001231 LLVMValueRef *out_ptr =
Michel Dänzere884c562014-01-15 15:24:14 +09001232 si_shader_ctx->radeon_bld.soa.outputs[es->output[i].index];
1233 int j;
1234
1235 for (j = 0; j < gs->ninput; j++) {
1236 if (gs->input[j].name == es->output[i].name &&
1237 gs->input[j].sid == es->output[i].sid)
1238 break;
1239 }
1240 if (j == gs->ninput)
1241 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001242
1243 for (chan = 0; chan < 4; chan++) {
1244 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001245 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1246
1247 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001248 LLVMGetUndef(i32), soffset,
1249 (4 * gs->input[j].param_offset + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001250 V_008F0C_BUF_DATA_FORMAT_32,
1251 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001252 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001253 }
1254 }
1255}
1256
1257static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1258{
1259 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1260 struct gallivm_state *gallivm = bld_base->base.gallivm;
1261 LLVMValueRef args[2];
1262
1263 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1264 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1265 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1266 LLVMVoidTypeInContext(gallivm->context), args, 2,
1267 LLVMNoUnwindAttribute);
1268}
1269
1270static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1271{
1272 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1273 struct gallivm_state *gallivm = bld_base->base.gallivm;
1274 struct si_pipe_shader *shader = si_shader_ctx->shader;
1275 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1276 struct si_shader_output_values *outputs = NULL;
1277 unsigned noutput = 0;
1278 int i;
1279
1280 while (!tgsi_parse_end_of_tokens(parse)) {
1281 struct tgsi_full_declaration *d =
1282 &parse->FullToken.FullDeclaration;
1283 unsigned index;
1284
1285 tgsi_parse_token(parse);
1286
1287 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1288 continue;
1289
1290 i = si_store_shader_io_attribs(&shader->shader, d);
1291 if (i < 0)
1292 continue;
1293
1294 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1295 (noutput + 1) * sizeof(outputs[0]));
1296 for (index = d->Range.First; index <= d->Range.Last; index++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001297 outputs[noutput].index = index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001298 outputs[noutput].name = d->Semantic.Name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001299 outputs[noutput].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001300 outputs[noutput].usage = d->Declaration.UsageMask;
1301
1302 for (i = 0; i < 4; i++)
1303 outputs[noutput].values[i] =
1304 LLVMBuildLoad(gallivm->builder,
1305 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1306 "");
1307 }
1308 noutput++;
1309 }
1310
1311 si_llvm_export_vs(bld_base, outputs, noutput);
1312 FREE(outputs);
1313}
1314
Michel Dänzer51f89a02013-12-09 15:33:53 +09001315static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1316{
1317 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
1318 struct si_shader * shader = &si_shader_ctx->shader->shader;
1319 struct lp_build_context * base = &bld_base->base;
1320 struct lp_build_context * uint =
1321 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1322 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1323 LLVMValueRef args[9];
1324 LLVMValueRef last_args[9] = { 0 };
1325 unsigned semantic_name;
1326 int depth_index = -1, stencil_index = -1;
1327 int i;
1328
1329 while (!tgsi_parse_end_of_tokens(parse)) {
1330 struct tgsi_full_declaration *d =
1331 &parse->FullToken.FullDeclaration;
1332 unsigned target;
1333 unsigned index;
1334
1335 tgsi_parse_token(parse);
1336
1337 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
1338 parse->FullToken.FullProperty.Property.PropertyName ==
1339 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
1340 shader->fs_write_all = TRUE;
1341
1342 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1343 continue;
1344
1345 i = si_store_shader_io_attribs(shader, d);
1346 if (i < 0)
1347 continue;
1348
1349 semantic_name = d->Semantic.Name;
1350 for (index = d->Range.First; index <= d->Range.Last; index++) {
1351 /* Select the correct target */
1352 switch(semantic_name) {
1353 case TGSI_SEMANTIC_POSITION:
1354 depth_index = index;
1355 continue;
1356 case TGSI_SEMANTIC_STENCIL:
1357 stencil_index = index;
1358 continue;
1359 case TGSI_SEMANTIC_COLOR:
1360 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1361 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001362 LLVMBuildStore(bld_base->base.gallivm->builder,
1363 bld_base->base.one,
1364 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1365
Michel Dänzer51f89a02013-12-09 15:33:53 +09001366 if (d->Semantic.Index == 0 &&
1367 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001368 si_alpha_test(bld_base,
1369 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001370 break;
1371 default:
1372 target = 0;
1373 fprintf(stderr,
1374 "Warning: SI unhandled fs output type:%d\n",
1375 semantic_name);
1376 }
1377
Michel Dänzer404b29d2013-11-21 16:45:28 +09001378 si_llvm_init_export_args_load(bld_base,
1379 si_shader_ctx->radeon_bld.soa.outputs[index],
1380 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001381
1382 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001383 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001384 if (last_args[0]) {
1385 lp_build_intrinsic(base->gallivm->builder,
1386 "llvm.SI.export",
1387 LLVMVoidTypeInContext(base->gallivm->context),
1388 last_args, 9);
1389 }
1390
Marek Olšák0eb528a2013-12-04 13:24:22 +01001391 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001392 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001393
1394 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1395 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1396 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1397 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001398 si_llvm_init_export_args_load(bld_base,
1399 si_shader_ctx->radeon_bld.soa.outputs[index],
1400 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001401 lp_build_intrinsic(base->gallivm->builder,
1402 "llvm.SI.export",
1403 LLVMVoidTypeInContext(base->gallivm->context),
1404 args, 9);
1405 }
1406 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001407 } else {
1408 lp_build_intrinsic(base->gallivm->builder,
1409 "llvm.SI.export",
1410 LLVMVoidTypeInContext(base->gallivm->context),
1411 args, 9);
1412 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001413 }
1414 }
1415
Michel Dänzer1a616c12012-11-13 17:35:09 +01001416 if (depth_index >= 0 || stencil_index >= 0) {
1417 LLVMValueRef out_ptr;
1418 unsigned mask = 0;
1419
1420 /* Specify the target we are exporting */
1421 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1422
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001423 args[5] = base->zero; /* R, depth */
1424 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1425 args[7] = base->zero; /* B, sample mask */
1426 args[8] = base->zero; /* A, alpha to mask */
1427
Michel Dänzer1a616c12012-11-13 17:35:09 +01001428 if (depth_index >= 0) {
1429 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1430 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1431 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001432 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001433 }
1434
1435 if (stencil_index >= 0) {
1436 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001437 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001438 /* Only setting the stencil component bit (0x2) here
1439 * breaks some stencil piglit tests
1440 */
1441 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001442 si_shader_ctx->shader->db_shader_control |=
1443 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001444 }
1445
Marek Olšákd9e102b2014-05-06 19:59:53 +02001446 if (stencil_index >= 0)
1447 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1448 else
1449 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1450
Michel Dänzer1a616c12012-11-13 17:35:09 +01001451 /* Specify which components to enable */
1452 args[0] = lp_build_const_int32(base->gallivm, mask);
1453
1454 args[1] =
1455 args[2] =
1456 args[4] = uint->zero;
1457
1458 if (last_args[0])
1459 lp_build_intrinsic(base->gallivm->builder,
1460 "llvm.SI.export",
1461 LLVMVoidTypeInContext(base->gallivm->context),
1462 args, 9);
1463 else
1464 memcpy(last_args, args, sizeof(args));
1465 }
1466
Michel Dänzer51f89a02013-12-09 15:33:53 +09001467 if (!last_args[0]) {
1468 /* Specify which components to enable */
1469 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001470
Michel Dänzer51f89a02013-12-09 15:33:53 +09001471 /* Specify the target we are exporting */
1472 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001473
Michel Dänzer51f89a02013-12-09 15:33:53 +09001474 /* Set COMPR flag to zero to export data as 32-bit */
1475 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001476
Michel Dänzer51f89a02013-12-09 15:33:53 +09001477 /* dummy bits */
1478 last_args[5]= uint->zero;
1479 last_args[6]= uint->zero;
1480 last_args[7]= uint->zero;
1481 last_args[8]= uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001482
Michel Dänzer51f89a02013-12-09 15:33:53 +09001483 si_shader_ctx->shader->spi_shader_col_format |=
1484 V_028714_SPI_SHADER_32_ABGR;
1485 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001486 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001487
1488 /* Specify whether the EXEC mask represents the valid mask */
1489 last_args[1] = uint->one;
1490
1491 /* Specify that this is the last export */
1492 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1493
1494 lp_build_intrinsic(base->gallivm->builder,
1495 "llvm.SI.export",
1496 LLVMVoidTypeInContext(base->gallivm->context),
1497 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001498}
1499
Marek Olšák4855acd2013-08-06 15:08:54 +02001500static const struct lp_build_tgsi_action txf_action;
1501
1502static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1503 struct lp_build_tgsi_context * bld_base,
1504 struct lp_build_emit_data * emit_data);
1505
Tom Stellarda75c6162012-01-06 17:38:37 -05001506static void tex_fetch_args(
1507 struct lp_build_tgsi_context * bld_base,
1508 struct lp_build_emit_data * emit_data)
1509{
Christian König55fe5cc2013-03-04 16:30:06 +01001510 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001511 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001512 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001513 unsigned opcode = inst->Instruction.Opcode;
1514 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001515 LLVMValueRef coords[4];
1516 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001517 int ref_pos;
1518 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001519 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001520 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001521 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1522 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1523
1524 if (target == TGSI_TEXTURE_BUFFER) {
1525 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1526 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1527 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1528 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1529
1530 /* Truncate v32i8 to v16i8. */
1531 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1532 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1533 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1534 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1535
1536 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1537 emit_data->args[0] = res;
1538 emit_data->args[1] = bld_base->uint_bld.zero;
1539 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1540 emit_data->arg_count = 3;
1541 return;
1542 }
Tom Stellard467f5162012-05-16 15:15:35 -04001543
Michel Dänzer120efee2013-01-25 12:10:11 +01001544 /* Fetch and project texture coordinates */
1545 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001546 for (chan = 0; chan < 3; chan++ ) {
1547 coords[chan] = lp_build_emit_fetch(bld_base,
1548 emit_data->inst, 0,
1549 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001550 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001551 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1552 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001553 coords[chan],
1554 coords[3]);
1555 }
1556
Michel Dänzer120efee2013-01-25 12:10:11 +01001557 if (opcode == TGSI_OPCODE_TXP)
1558 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001559
Michel Dänzer120efee2013-01-25 12:10:11 +01001560 /* Pack LOD bias value */
1561 if (opcode == TGSI_OPCODE_TXB)
1562 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001563 if (opcode == TGSI_OPCODE_TXB2)
1564 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001565
Marek Olšák2484daa2014-04-22 21:23:29 +02001566 if (target == TGSI_TEXTURE_CUBE ||
1567 target == TGSI_TEXTURE_CUBE_ARRAY ||
1568 target == TGSI_TEXTURE_SHADOWCUBE ||
1569 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001570 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001571
1572 /* Pack depth comparison value */
1573 switch (target) {
1574 case TGSI_TEXTURE_SHADOW1D:
1575 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1576 case TGSI_TEXTURE_SHADOW2D:
1577 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001578 case TGSI_TEXTURE_SHADOWCUBE:
1579 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001580 assert(ref_pos >= 0);
1581 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001582 break;
1583 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1584 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001585 }
1586
Michel Dänzera6b83c02013-02-21 16:10:55 +01001587 /* Pack user derivatives */
1588 if (opcode == TGSI_OPCODE_TXD) {
1589 for (chan = 0; chan < 2; chan++) {
1590 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1591 if (num_coords > 1)
1592 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1593 }
1594 }
1595
Michel Dänzer120efee2013-01-25 12:10:11 +01001596 /* Pack texture coordinates */
1597 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001598 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001599 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001600 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001601 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001602
Marek Olšákd2bd6342013-09-18 15:40:21 +02001603 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001604 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001605 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001606 if (opcode == TGSI_OPCODE_TXL2)
1607 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001608
1609 if (count > 16) {
1610 assert(!"Cannot handle more than 16 texture address parameters");
1611 count = 16;
1612 }
1613
1614 for (chan = 0; chan < count; chan++ ) {
1615 address[chan] = LLVMBuildBitCast(gallivm->builder,
1616 address[chan],
1617 LLVMInt32TypeInContext(gallivm->context),
1618 "");
1619 }
1620
Marek Olšák4855acd2013-08-06 15:08:54 +02001621 /* Adjust the sample index according to FMASK.
1622 *
1623 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1624 * which is the identity mapping. Each nibble says which physical sample
1625 * should be fetched to get that sample.
1626 *
1627 * For example, 0x11111100 means there are only 2 samples stored and
1628 * the second sample covers 3/4 of the pixel. When reading samples 0
1629 * and 1, return physical sample 0 (determined by the first two 0s
1630 * in FMASK), otherwise return physical sample 1.
1631 *
1632 * The sample index should be adjusted as follows:
1633 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1634 */
1635 if (target == TGSI_TEXTURE_2D_MSAA ||
1636 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1637 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1638 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001639 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001640 unsigned txf_count = count;
1641
Marek Olšákd2bd6342013-09-18 15:40:21 +02001642 memcpy(txf_address, address, sizeof(txf_address));
1643
1644 if (target == TGSI_TEXTURE_2D_MSAA) {
1645 txf_address[2] = bld_base->uint_bld.zero;
1646 }
1647 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001648
1649 /* Pad to a power-of-two size. */
1650 while (txf_count < util_next_power_of_two(txf_count))
1651 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1652
1653 /* Read FMASK using TXF. */
1654 txf_emit_data.chan = 0;
1655 txf_emit_data.dst_type = LLVMVectorType(
1656 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1657 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1658 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
Marek Olšákd2bd6342013-09-18 15:40:21 +02001659 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1660 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
Marek Olšák4855acd2013-08-06 15:08:54 +02001661 txf_emit_data.arg_count = 3;
1662
1663 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1664
1665 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001666 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1667 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1668
1669 /* Apply the formula. */
1670 LLVMValueRef fmask =
1671 LLVMBuildExtractElement(gallivm->builder,
1672 txf_emit_data.output[0],
1673 uint_bld->zero, "");
1674
Marek Olšákd2bd6342013-09-18 15:40:21 +02001675 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001676
1677 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001678 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001679
1680 LLVMValueRef shifted_fmask =
1681 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1682
1683 LLVMValueRef final_sample =
1684 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1685
1686 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1687 * resource descriptor is 0 (invalid),
1688 */
1689 LLVMValueRef fmask_desc =
1690 LLVMBuildBitCast(gallivm->builder,
1691 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1692 LLVMVectorType(uint_bld->elem_type, 8), "");
1693
1694 LLVMValueRef fmask_word1 =
1695 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1696 uint_bld->one, "");
1697
1698 LLVMValueRef word1_is_nonzero =
1699 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1700 fmask_word1, uint_bld->zero, "");
1701
Marek Olšákd2bd6342013-09-18 15:40:21 +02001702 /* Replace the MSAA sample index. */
1703 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001704 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001705 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001706 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001707
Michel Dänzer36231112013-05-02 09:44:45 +02001708 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001709 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001710
1711 if (opcode == TGSI_OPCODE_TXF) {
1712 /* add tex offsets */
1713 if (inst->Texture.NumOffsets) {
1714 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1715 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1716 const struct tgsi_texture_offset * off = inst->TexOffsets;
1717
1718 assert(inst->Texture.NumOffsets == 1);
1719
Marek Olšákdefedc02013-09-18 15:36:38 +02001720 switch (target) {
1721 case TGSI_TEXTURE_3D:
1722 address[2] = lp_build_add(uint_bld, address[2],
1723 bld->immediates[off->Index][off->SwizzleZ]);
1724 /* fall through */
1725 case TGSI_TEXTURE_2D:
1726 case TGSI_TEXTURE_SHADOW2D:
1727 case TGSI_TEXTURE_RECT:
1728 case TGSI_TEXTURE_SHADOWRECT:
1729 case TGSI_TEXTURE_2D_ARRAY:
1730 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001731 address[1] =
1732 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001733 bld->immediates[off->Index][off->SwizzleY]);
1734 /* fall through */
1735 case TGSI_TEXTURE_1D:
1736 case TGSI_TEXTURE_SHADOW1D:
1737 case TGSI_TEXTURE_1D_ARRAY:
1738 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1739 address[0] =
1740 lp_build_add(uint_bld, address[0],
1741 bld->immediates[off->Index][off->SwizzleX]);
1742 break;
1743 /* texture offsets do not apply to other texture targets */
1744 }
Michel Dänzer36231112013-05-02 09:44:45 +02001745 }
1746
1747 emit_data->dst_type = LLVMVectorType(
1748 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1749 4);
1750
1751 emit_data->arg_count = 3;
1752 } else {
1753 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001754 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001755
1756 emit_data->dst_type = LLVMVectorType(
1757 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1758 4);
1759
1760 emit_data->arg_count = 4;
1761 }
1762
Marek Olšák2484daa2014-04-22 21:23:29 +02001763 /* The fetch opcode has been converted to a 2D array fetch.
1764 * This simplifies the LLVM backend. */
1765 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1766 target = TGSI_TEXTURE_2D_ARRAY;
1767 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1768 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1769
Michel Dänzer36231112013-05-02 09:44:45 +02001770 /* Dimensions */
1771 emit_data->args[emit_data->arg_count - 1] =
1772 lp_build_const_int32(bld_base->base.gallivm, target);
1773
Michel Dänzer120efee2013-01-25 12:10:11 +01001774 /* Pad to power of two vector */
1775 while (count < util_next_power_of_two(count))
1776 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1777
Christian Königccf3e8f2013-03-26 15:09:27 +01001778 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001779}
1780
Michel Dänzer07eddc42013-02-06 15:43:10 +01001781static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1782 struct lp_build_tgsi_context * bld_base,
1783 struct lp_build_emit_data * emit_data)
1784{
1785 struct lp_build_context * base = &bld_base->base;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001786 char intr_name[127];
Michel Dänzer07eddc42013-02-06 15:43:10 +01001787
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001788 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1789 emit_data->output[emit_data->chan] = build_intrinsic(
1790 base->gallivm->builder,
1791 "llvm.SI.vs.load.input", emit_data->dst_type,
1792 emit_data->args, emit_data->arg_count,
1793 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1794 return;
1795 }
1796
Michel Dänzer07eddc42013-02-06 15:43:10 +01001797 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001798 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001799
Christian König44e32242013-03-20 12:10:35 +01001800 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001801 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001802 emit_data->args, emit_data->arg_count,
1803 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001804}
1805
Michel Dänzer0495adb2013-05-06 12:45:14 +02001806static void txq_fetch_args(
1807 struct lp_build_tgsi_context * bld_base,
1808 struct lp_build_emit_data * emit_data)
1809{
1810 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1811 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001812 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02001813 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001814
Marek Olšák2484daa2014-04-22 21:23:29 +02001815 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001816 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1817 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1818
1819 /* Read the size from the buffer descriptor directly. */
1820 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1821 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1822 size = LLVMBuildExtractElement(gallivm->builder, size,
1823 lp_build_const_int32(gallivm, 2), "");
1824 emit_data->args[0] = size;
1825 return;
1826 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02001827
1828 /* Mip level */
1829 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1830
1831 /* Resource */
1832 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1833
Marek Olšák2484daa2014-04-22 21:23:29 +02001834 /* Texture target */
1835 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
1836 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1837 target = TGSI_TEXTURE_2D_ARRAY;
1838
Michel Dänzer0495adb2013-05-06 12:45:14 +02001839 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1840 inst->Texture.Texture);
1841
1842 emit_data->arg_count = 3;
1843
1844 emit_data->dst_type = LLVMVectorType(
1845 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1846 4);
1847}
1848
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001849static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
1850 struct lp_build_tgsi_context * bld_base,
1851 struct lp_build_emit_data * emit_data)
1852{
Marek Olšák2484daa2014-04-22 21:23:29 +02001853 unsigned target = emit_data->inst->Texture.Texture;
1854
1855 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001856 /* Just return the buffer size. */
1857 emit_data->output[emit_data->chan] = emit_data->args[0];
1858 return;
1859 }
1860
1861 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02001862
1863 /* Divide the number of layers by 6 to get the number of cubes. */
1864 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
1865 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1866 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1867 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
1868 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
1869
1870 LLVMValueRef v4 = emit_data->output[emit_data->chan];
1871 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
1872 z = LLVMBuildSDiv(builder, z, six, "");
1873
1874 emit_data->output[emit_data->chan] =
1875 LLVMBuildInsertElement(builder, v4, z, two, "");
1876 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001877}
1878
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001879#if HAVE_LLVM >= 0x0304
1880
1881static void si_llvm_emit_ddxy(
1882 const struct lp_build_tgsi_action * action,
1883 struct lp_build_tgsi_context * bld_base,
1884 struct lp_build_emit_data * emit_data)
1885{
1886 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1887 struct gallivm_state *gallivm = bld_base->base.gallivm;
1888 struct lp_build_context * base = &bld_base->base;
1889 const struct tgsi_full_instruction *inst = emit_data->inst;
1890 unsigned opcode = inst->Instruction.Opcode;
1891 LLVMValueRef indices[2];
1892 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1893 LLVMValueRef tl, trbl, result[4];
1894 LLVMTypeRef i32;
1895 unsigned swizzle[4];
1896 unsigned c;
1897
1898 i32 = LLVMInt32TypeInContext(gallivm->context);
1899
1900 indices[0] = bld_base->uint_bld.zero;
1901 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1902 NULL, 0, LLVMReadNoneAttribute);
1903 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1904 indices, 2, "");
1905
1906 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1907 lp_build_const_int32(gallivm, 0xfffffffc), "");
1908 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1909 indices, 2, "");
1910
1911 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1912 lp_build_const_int32(gallivm,
1913 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1914 "");
1915 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1916 indices, 2, "");
1917
1918 for (c = 0; c < 4; ++c) {
1919 unsigned i;
1920
1921 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1922 for (i = 0; i < c; ++i) {
1923 if (swizzle[i] == swizzle[c]) {
1924 result[c] = result[i];
1925 break;
1926 }
1927 }
1928 if (i != c)
1929 continue;
1930
1931 LLVMBuildStore(gallivm->builder,
1932 LLVMBuildBitCast(gallivm->builder,
1933 lp_build_emit_fetch(bld_base, inst, 0, c),
1934 i32, ""),
1935 store_ptr);
1936
1937 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1938 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1939
1940 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1941 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1942
1943 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1944 }
1945
1946 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1947}
1948
1949#endif /* HAVE_LLVM >= 0x0304 */
1950
Michel Dänzer404b29d2013-11-21 16:45:28 +09001951/* Emit one vertex from the geometry shader */
1952static void si_llvm_emit_vertex(
1953 const struct lp_build_tgsi_action *action,
1954 struct lp_build_tgsi_context *bld_base,
1955 struct lp_build_emit_data *emit_data)
1956{
1957 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001958 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001959 struct si_shader *shader = &si_shader_ctx->shader->shader;
1960 struct gallivm_state *gallivm = bld_base->base.gallivm;
1961 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001962 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1963 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001964 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09001965 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001966 LLVMValueRef t_list_ptr;
1967 LLVMValueRef t_list;
1968 LLVMValueRef args[2];
1969 unsigned chan;
1970 int i;
1971
1972 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001973 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1974 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001975 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001976 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001977
1978 if (shader->noutput == 0) {
1979 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1980
1981 while (!tgsi_parse_end_of_tokens(parse)) {
1982 tgsi_parse_token(parse);
1983
Michel Dänzer7c7d7382014-01-09 15:33:34 +09001984 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
1985 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
1986
1987 if (d->Declaration.File == TGSI_FILE_OUTPUT)
1988 si_store_shader_io_attribs(shader, d);
1989 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001990 }
1991 }
1992
1993 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001994 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09001995
1996 /* If this thread has already emitted the declared maximum number of
1997 * vertices, kill it: excessive vertex emissions are not supposed to
1998 * have any effect, and GS threads have no externally observable
1999 * effects other than emitting vertices.
2000 */
2001 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2002 lp_build_const_int32(gallivm,
2003 shader->gs_max_out_vertices), "");
2004 kill = lp_build_select(&bld_base->base, can_emit,
2005 lp_build_const_float(gallivm, 1.0f),
2006 lp_build_const_float(gallivm, -1.0f));
2007 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2008 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2009
Michel Dänzer404b29d2013-11-21 16:45:28 +09002010 for (i = 0; i < shader->noutput; i++) {
2011 LLVMValueRef *out_ptr =
2012 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
2013
2014 for (chan = 0; chan < 4; chan++) {
2015 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002016 LLVMValueRef voffset =
2017 lp_build_const_int32(gallivm, (i * 4 + chan) *
2018 shader->gs_max_out_vertices);
2019
2020 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2021 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002022
2023 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2024
2025 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2026 voffset, soffset, 0,
2027 V_008F0C_BUF_DATA_FORMAT_32,
2028 V_008F0C_BUF_NUM_FORMAT_UINT,
2029 1, 0, 1, 1, 0);
2030 }
2031 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002032 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2033 lp_build_const_int32(gallivm, 1));
2034 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002035
2036 /* Signal vertex emission */
2037 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2038 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2039 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2040 LLVMVoidTypeInContext(gallivm->context), args, 2,
2041 LLVMNoUnwindAttribute);
2042}
2043
2044/* Cut one primitive from the geometry shader */
2045static void si_llvm_emit_primitive(
2046 const struct lp_build_tgsi_action *action,
2047 struct lp_build_tgsi_context *bld_base,
2048 struct lp_build_emit_data *emit_data)
2049{
2050 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2051 struct gallivm_state *gallivm = bld_base->base.gallivm;
2052 LLVMValueRef args[2];
2053
2054 /* Signal primitive cut */
2055 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2056 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2057 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2058 LLVMVoidTypeInContext(gallivm->context), args, 2,
2059 LLVMNoUnwindAttribute);
2060}
2061
Tom Stellarda75c6162012-01-06 17:38:37 -05002062static const struct lp_build_tgsi_action tex_action = {
2063 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002064 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002065 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05002066};
2067
Michel Dänzer3e205132012-11-06 17:39:01 +01002068static const struct lp_build_tgsi_action txb_action = {
2069 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002070 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002071 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01002072};
2073
Michel Dänzera6b83c02013-02-21 16:10:55 +01002074#if HAVE_LLVM >= 0x0304
2075static const struct lp_build_tgsi_action txd_action = {
2076 .fetch_args = tex_fetch_args,
2077 .emit = build_tex_intrinsic,
2078 .intr_name = "llvm.SI.sampled."
2079};
2080#endif
2081
Michel Dänzer36231112013-05-02 09:44:45 +02002082static const struct lp_build_tgsi_action txf_action = {
2083 .fetch_args = tex_fetch_args,
2084 .emit = build_tex_intrinsic,
2085 .intr_name = "llvm.SI.imageload."
2086};
2087
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002088static const struct lp_build_tgsi_action txl_action = {
2089 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002090 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002091 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002092};
2093
Michel Dänzer0495adb2013-05-06 12:45:14 +02002094static const struct lp_build_tgsi_action txq_action = {
2095 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002096 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002097 .intr_name = "llvm.SI.resinfo"
2098};
2099
Christian König206f0592013-03-20 14:37:21 +01002100static void create_meta_data(struct si_shader_context *si_shader_ctx)
2101{
2102 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2103 LLVMValueRef args[3];
2104
2105 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2106 args[1] = 0;
2107 args[2] = lp_build_const_int32(gallivm, 1);
2108
2109 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2110}
2111
Christian König55fe5cc2013-03-04 16:30:06 +01002112static void create_function(struct si_shader_context *si_shader_ctx)
2113{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002114 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2115 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002116 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzerf8e16012014-01-28 15:39:30 +09002117 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002118 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002119
Christian König55fe5cc2013-03-04 16:30:06 +01002120 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002121 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002122 f32 = LLVMFloatTypeInContext(gallivm->context);
2123 v2i32 = LLVMVectorType(i32, 2);
2124 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01002125
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002126 params[SI_PARAM_CONST] = LLVMPointerType(
2127 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
Michel Dänzerf8e16012014-01-28 15:39:30 +09002128 params[SI_PARAM_RW_BUFFERS] = params[SI_PARAM_CONST];
2129
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002130 /* We assume at most 16 textures per program at the moment.
2131 * This need probably need to be changed to support bindless textures */
2132 params[SI_PARAM_SAMPLER] = LLVMPointerType(
2133 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
2134 params[SI_PARAM_RESOURCE] = LLVMPointerType(
2135 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002136
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002137 switch (si_shader_ctx->type) {
2138 case TGSI_PROCESSOR_VERTEX:
2139 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01002140 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002141 num_params = SI_PARAM_START_INSTANCE+1;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002142 if (shader->key.vs.as_es) {
2143 params[SI_PARAM_ES2GS_OFFSET] = i32;
2144 num_params++;
2145 } else {
2146 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002147
Michel Dänzer404b29d2013-11-21 16:45:28 +09002148 /* Streamout SGPRs. */
2149 if (shader->selector->so.num_outputs) {
2150 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2151 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2152 }
2153 /* A streamout buffer offset is loaded if the stride is non-zero. */
2154 for (i = 0; i < 4; i++) {
2155 if (!shader->selector->so.stride[i])
2156 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002157
Michel Dänzer404b29d2013-11-21 16:45:28 +09002158 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2159 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002160 }
2161
2162 last_sgpr = num_params-1;
2163
2164 /* VGPRs */
2165 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2166 params[num_params++] = i32; /* unused*/
2167 params[num_params++] = i32; /* unused */
2168 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002169 break;
Christian König0666ffd2013-03-05 15:07:39 +01002170
Michel Dänzer404b29d2013-11-21 16:45:28 +09002171 case TGSI_PROCESSOR_GEOMETRY:
2172 params[SI_PARAM_GS2VS_OFFSET] = i32;
2173 params[SI_PARAM_GS_WAVE_ID] = i32;
2174 last_sgpr = SI_PARAM_GS_WAVE_ID;
2175
2176 /* VGPRs */
2177 params[SI_PARAM_VTX0_OFFSET] = i32;
2178 params[SI_PARAM_VTX1_OFFSET] = i32;
2179 params[SI_PARAM_PRIMITIVE_ID] = i32;
2180 params[SI_PARAM_VTX2_OFFSET] = i32;
2181 params[SI_PARAM_VTX3_OFFSET] = i32;
2182 params[SI_PARAM_VTX4_OFFSET] = i32;
2183 params[SI_PARAM_VTX5_OFFSET] = i32;
2184 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2185 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2186 break;
2187
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002188 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002189 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002190 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002191 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002192 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2193 params[SI_PARAM_PERSP_CENTER] = v2i32;
2194 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2195 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2196 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2197 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2198 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2199 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2200 params[SI_PARAM_POS_X_FLOAT] = f32;
2201 params[SI_PARAM_POS_Y_FLOAT] = f32;
2202 params[SI_PARAM_POS_Z_FLOAT] = f32;
2203 params[SI_PARAM_POS_W_FLOAT] = f32;
2204 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002205 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002206 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2207 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002208 num_params = SI_PARAM_POS_FIXED_PT+1;
2209 break;
2210
2211 default:
2212 assert(0 && "unimplemented shader");
2213 return;
Christian Königc4973212013-03-05 12:14:02 +01002214 }
Christian König55fe5cc2013-03-04 16:30:06 +01002215
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002216 assert(num_params <= Elements(params));
2217 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002218 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002219
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002220 for (i = 0; i <= last_sgpr; ++i) {
2221 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002222 switch (i) {
2223 default:
2224 LLVMAddAttribute(P, LLVMInRegAttribute);
2225 break;
2226#if HAVE_LLVM >= 0x0304
2227 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2228 * to move load. Inputs are constant so this is fine. */
2229 case SI_PARAM_CONST:
2230 case SI_PARAM_SAMPLER:
2231 case SI_PARAM_RESOURCE:
2232 LLVMAddAttribute(P, LLVMByValAttribute);
2233 break;
2234#endif
2235 }
Christian Königcf9b31f2013-03-21 18:30:23 +01002236 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002237
2238#if HAVE_LLVM >= 0x0304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002239 if (bld_base->info &&
2240 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2241 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002242 si_shader_ctx->ddxy_lds =
2243 LLVMAddGlobalInAddressSpace(gallivm->module,
2244 LLVMArrayType(i32, 64),
2245 "ddxy_lds",
2246 LOCAL_ADDR_SPACE);
2247#endif
Christian König55fe5cc2013-03-04 16:30:06 +01002248}
Tom Stellarda75c6162012-01-06 17:38:37 -05002249
Christian König0f6cf2b2013-03-15 15:53:25 +01002250static void preload_constants(struct si_shader_context *si_shader_ctx)
2251{
2252 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2253 struct gallivm_state * gallivm = bld_base->base.gallivm;
2254 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002255 unsigned buf;
2256 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002257
Marek Olšák2fd42002013-10-25 11:45:47 +02002258 for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) {
2259 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002260
Marek Olšák2fd42002013-10-25 11:45:47 +02002261 if (num_const == 0)
2262 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002263
Marek Olšák2fd42002013-10-25 11:45:47 +02002264 /* Allocate space for the constant values */
2265 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002266
Marek Olšák2fd42002013-10-25 11:45:47 +02002267 /* Load the resource descriptor */
2268 si_shader_ctx->const_resource[buf] =
2269 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002270
Marek Olšák2fd42002013-10-25 11:45:47 +02002271 /* Load the constants, we rely on the code sinking to do the rest */
2272 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002273 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002274 load_const(gallivm->builder,
2275 si_shader_ctx->const_resource[buf],
2276 lp_build_const_int32(gallivm, i * 4),
2277 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002278 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002279 }
2280}
2281
Christian König1c100182013-03-17 16:02:42 +01002282static void preload_samplers(struct si_shader_context *si_shader_ctx)
2283{
2284 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2285 struct gallivm_state * gallivm = bld_base->base.gallivm;
2286 const struct tgsi_shader_info * info = bld_base->info;
2287
2288 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2289
2290 LLVMValueRef res_ptr, samp_ptr;
2291 LLVMValueRef offset;
2292
2293 if (num_samplers == 0)
2294 return;
2295
2296 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02002297 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002298 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2299
2300 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2301 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2302
2303 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2304 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002305 /* Resource */
2306 offset = lp_build_const_int32(gallivm, i);
2307 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2308
2309 /* Sampler */
2310 offset = lp_build_const_int32(gallivm, i);
2311 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002312
2313 /* FMASK resource */
2314 if (info->is_msaa_sampler[i]) {
2315 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
2316 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
2317 build_indexed_load(si_shader_ctx, res_ptr, offset);
2318 }
Christian König1c100182013-03-17 16:02:42 +01002319 }
2320}
2321
Marek Olšák8d03d922013-09-01 23:59:06 +02002322static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2323{
2324 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2325 struct gallivm_state * gallivm = bld_base->base.gallivm;
2326 unsigned i;
2327
Michel Dänzer67e385b2014-01-08 17:48:21 +09002328 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2329 si_shader_ctx->shader->key.vs.as_es ||
2330 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002331 return;
2332
2333 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002334 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002335
2336 /* Load the resources, we rely on the code sinking to do the rest */
2337 for (i = 0; i < 4; ++i) {
2338 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002339 LLVMValueRef offset = lp_build_const_int32(gallivm,
2340 SI_RW_SO + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002341
2342 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2343 }
2344 }
2345}
2346
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002347int si_compile_llvm(struct si_context *sctx, struct si_pipe_shader *shader,
Tom Stellard302f53d2012-10-25 13:50:10 -04002348 LLVMModuleRef mod)
2349{
Darren Powellbc866902014-03-31 18:00:28 -04002350 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002351 unsigned i;
2352 uint32_t *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002353 struct radeon_shader_binary binary;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002354 bool dump = r600_can_dump_shader(&sctx->screen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002355 shader->selector ? shader->selector->tokens : NULL);
Darren Powellbc866902014-03-31 18:00:28 -04002356 const char * gpu_family = r600_get_llvm_processor_name(sctx->screen->b.family);
2357
2358 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002359 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002360 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2361
2362 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002363 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002364 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002365 for (i = 0; i < binary.code_size; i+=4 ) {
2366 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2367 binary.code[i + 2], binary.code[i + 1],
2368 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002369 }
2370 }
2371
Tom Stellardd50343d2013-04-04 16:21:06 -04002372 /* XXX: We may be able to emit some of these values directly rather than
2373 * extracting fields to be emitted later.
2374 */
Darren Powellbc866902014-03-31 18:00:28 -04002375 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002376 for (i = 0; i < binary.config_size; i+= 8) {
2377 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2378 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2379 switch (reg) {
2380 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2381 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2382 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2383 case R_00B848_COMPUTE_PGM_RSRC1:
2384 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2385 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2386 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002387 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2388 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2389 break;
2390 case R_00B84C_COMPUTE_PGM_RSRC2:
2391 shader->lds_size = G_00B84C_LDS_SIZE(value);
2392 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002393 case R_0286CC_SPI_PS_INPUT_ENA:
2394 shader->spi_ps_input_ena = value;
2395 break;
2396 default:
2397 fprintf(stderr, "Warning: Compiler emitted unknown "
2398 "config register: 0x%x\n", reg);
2399 break;
2400 }
2401 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002402
2403 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02002404 r600_resource_reference(&shader->bo, NULL);
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002405 shader->bo = si_resource_create_custom(sctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04002406 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002407 if (shader->bo == NULL) {
2408 return -ENOMEM;
2409 }
2410
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002411 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 -08002412 if (SI_BIG_ENDIAN) {
Tom Stellardd50343d2013-04-04 16:21:06 -04002413 for (i = 0; i < binary.code_size / 4; ++i) {
Tom Stellard195ee102014-02-20 09:03:53 -08002414 ptr[i] = util_cpu_to_le32((*(uint32_t*)(binary.code + i*4)));
Tom Stellard302f53d2012-10-25 13:50:10 -04002415 }
2416 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04002417 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002418 }
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002419 sctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002420
Tom Stellard7782d192013-04-04 09:57:13 -07002421 free(binary.code);
2422 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04002423
Darren Powellbc866902014-03-31 18:00:28 -04002424 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002425}
2426
Michel Dänzer404b29d2013-11-21 16:45:28 +09002427/* Generate code for the hardware VS shader stage to go with a geometry shader */
2428static int si_generate_gs_copy_shader(struct si_context *sctx,
2429 struct si_shader_context *si_shader_ctx,
2430 bool dump)
2431{
2432 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2433 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2434 struct lp_build_context *base = &bld_base->base;
2435 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002436 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002437 struct si_shader *gs = &si_shader_ctx->shader->selector->current->shader;
2438 struct si_shader_output_values *outputs;
2439 LLVMValueRef t_list_ptr, t_list;
2440 LLVMValueRef args[9];
2441 int i, r;
2442
2443 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2444
2445 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
2446 si_shader_ctx->gs_for_vs = gs;
2447
2448 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2449
2450 create_meta_data(si_shader_ctx);
2451 create_function(si_shader_ctx);
2452 preload_streamout_buffers(si_shader_ctx);
2453
2454 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002455 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2456 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002457 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002458 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002459
2460 args[0] = t_list;
2461 args[1] = lp_build_mul_imm(uint,
2462 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2463 si_shader_ctx->param_vertex_id),
2464 4);
2465 args[3] = uint->zero;
2466 args[4] = uint->one; /* OFFEN */
2467 args[5] = uint->zero; /* IDXEN */
2468 args[6] = uint->one; /* GLC */
2469 args[7] = uint->one; /* SLC */
2470 args[8] = uint->zero; /* TFE */
2471
2472 /* Fetch vertex data from GSVS ring */
2473 for (i = 0; i < gs->noutput; ++i) {
2474 struct si_shader_output *out = gs->output + i;
2475 unsigned chan;
2476
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002477 shader->output[i] = *out;
2478
Michel Dänzer404b29d2013-11-21 16:45:28 +09002479 outputs[i].name = out->name;
2480 outputs[i].index = out->index;
Michel Dänzer67e385b2014-01-08 17:48:21 +09002481 outputs[i].sid = out->sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002482 outputs[i].usage = out->usage;
2483
2484 for (chan = 0; chan < 4; chan++) {
2485 args[2] = lp_build_const_int32(gallivm,
2486 (i * 4 + chan) *
2487 gs->gs_max_out_vertices * 16 * 4);
2488
2489 outputs[i].values[chan] =
2490 LLVMBuildBitCast(gallivm->builder,
2491 build_intrinsic(gallivm->builder,
2492 "llvm.SI.buffer.load.dword.i32.i32",
2493 LLVMInt32TypeInContext(gallivm->context),
2494 args, 9,
2495 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2496 base->elem_type, "");
2497 }
2498 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002499 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002500
2501 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2502
2503 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2504
2505 if (dump)
2506 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2507
2508 r = si_compile_llvm(sctx, si_shader_ctx->shader,
2509 bld_base->base.gallivm->module);
2510
2511 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2512
2513 FREE(outputs);
2514 return r;
2515}
2516
Tom Stellarda75c6162012-01-06 17:38:37 -05002517int si_pipe_shader_create(
2518 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01002519 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002520{
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002521 struct si_context *sctx = (struct si_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002522 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002523 struct si_shader_context si_shader_ctx;
2524 struct tgsi_shader_info shader_info;
2525 struct lp_build_tgsi_context * bld_base;
2526 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002527 int r = 0;
Michel Dänzere1df0d42014-01-15 12:31:07 +09002528 bool dump = r600_can_dump_shader(&sctx->screen->b, sel->tokens);
2529
2530 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2531 * conversion fails. */
2532 if (dump) {
2533 tgsi_dump(sel->tokens, 0);
2534 si_dump_streamout(&sel->so);
2535 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002536
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002537 assert(shader->shader.noutput == 0);
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002538 assert(shader->shader.nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002539 assert(shader->shader.ninput == 0);
2540
Michel Dänzercfebaf92012-08-31 19:04:08 +02002541 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002542 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2543 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2544
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002545 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002546
Michel Dänzere44dfd42012-11-07 17:33:08 +01002547 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01002548 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002549 bld_base->info = &shader_info;
2550 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002551
2552 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002553 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01002554 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002555 bld_base->op_actions[TGSI_OPCODE_TXB2] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01002556#if HAVE_LLVM >= 0x0304
2557 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
2558#endif
Michel Dänzer36231112013-05-02 09:44:45 +02002559 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002560 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002561 bld_base->op_actions[TGSI_OPCODE_TXL2] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002562 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002563 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002564
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002565#if HAVE_LLVM >= 0x0304
2566 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2567 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
2568#endif
2569
Michel Dänzer404b29d2013-11-21 16:45:28 +09002570 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2571 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2572
Christian Könige4ed5872013-03-21 18:02:52 +01002573 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002574 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002575 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2576 si_shader_ctx.shader = shader;
2577 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002578
Michel Dänzer51f89a02013-12-09 15:33:53 +09002579 switch (si_shader_ctx.type) {
2580 case TGSI_PROCESSOR_VERTEX:
2581 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002582 if (shader->key.vs.as_es) {
2583 si_shader_ctx.gs_for_vs = &sctx->gs_shader->current->shader;
2584 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2585 } else {
2586 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2587 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002588 break;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002589 case TGSI_PROCESSOR_GEOMETRY: {
2590 int i;
2591
2592 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2593 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2594 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
2595
2596 for (i = 0; i < shader_info.num_properties; i++) {
2597 switch (shader_info.properties[i].name) {
2598 case TGSI_PROPERTY_GS_INPUT_PRIM:
2599 shader->shader.gs_input_prim = shader_info.properties[i].data[0];
2600 break;
2601 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
2602 shader->shader.gs_output_prim = shader_info.properties[i].data[0];
2603 break;
2604 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
2605 shader->shader.gs_max_out_vertices = shader_info.properties[i].data[0];
2606 break;
2607 }
2608 }
2609 break;
2610 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002611 case TGSI_PROCESSOR_FRAGMENT:
2612 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2613 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
2614 break;
2615 default:
2616 assert(!"Unsupported shader type");
2617 return -1;
2618 }
2619
Christian König206f0592013-03-20 14:37:21 +01002620 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002621 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002622 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002623 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002624 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002625
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002626 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2627 si_shader_ctx.gs_next_vertex =
2628 lp_build_alloca(bld_base->base.gallivm,
2629 bld_base->uint_bld.elem_type, "");
2630 }
2631
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002632 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002633 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002634 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002635 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002636
2637 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2638
2639 mod = bld_base->base.gallivm->module;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002640 r = si_compile_llvm(sctx, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002641 if (r) {
2642 fprintf(stderr, "LLVM failed to compile shader\n");
2643 goto out;
2644 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002645
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002646 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002647
2648 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2649 shader->gs_copy_shader = CALLOC_STRUCT(si_pipe_shader);
2650 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002651 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002652 si_shader_ctx.shader = shader->gs_copy_shader;
2653 if ((r = si_generate_gs_copy_shader(sctx, &si_shader_ctx, dump))) {
2654 free(shader->gs_copy_shader);
2655 shader->gs_copy_shader = NULL;
2656 goto out;
2657 }
2658 }
2659
Tom Stellarda75c6162012-01-06 17:38:37 -05002660 tgsi_parse_free(&si_shader_ctx.parse);
2661
Michel Dänzer404b29d2013-11-21 16:45:28 +09002662out:
Marek Olšák2fd42002013-10-25 11:45:47 +02002663 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2664 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002665 FREE(si_shader_ctx.resources);
2666 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002667
Tom Stellard302f53d2012-10-25 13:50:10 -04002668 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002669}
2670
2671void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
2672{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002673 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002674}