blob: 54270cdb7334112b0fb0c11179cf869e15423095 [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
Christian Könige4ed5872013-03-21 18:02:52 +0100532static void declare_system_value(
533 struct radeon_llvm_context * radeon_bld,
534 unsigned index,
535 const struct tgsi_full_declaration *decl)
536{
Marek Olšák8d03d922013-09-01 23:59:06 +0200537 struct si_shader_context *si_shader_ctx =
538 si_shader_context(&radeon_bld->soa.bld_base);
Christian Könige4ed5872013-03-21 18:02:52 +0100539 LLVMValueRef value = 0;
540
541 switch (decl->Semantic.Name) {
542 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200543 value = LLVMGetParam(radeon_bld->main_fn,
544 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100545 break;
546
547 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200548 value = LLVMGetParam(radeon_bld->main_fn,
549 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100550 break;
551
552 default:
553 assert(!"unknown system value");
554 return;
555 }
556
557 radeon_bld->system_values[index] = value;
558}
559
Tom Stellarda75c6162012-01-06 17:38:37 -0500560static LLVMValueRef fetch_constant(
561 struct lp_build_tgsi_context * bld_base,
562 const struct tgsi_full_src_register *reg,
563 enum tgsi_opcode_type type,
564 unsigned swizzle)
565{
Christian König55fe5cc2013-03-04 16:30:06 +0100566 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500567 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100568 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200569 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500570
Christian Königf5298b02013-02-28 14:50:07 +0100571 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100572 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100573 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500574
Christian König8514f5a2013-02-04 17:46:42 +0100575 if (swizzle == LP_CHAN_ALL) {
576 unsigned chan;
577 LLVMValueRef values[4];
578 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
579 values[chan] = fetch_constant(bld_base, reg, type, chan);
580
581 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
582 }
583
Marek Olšák2fd42002013-10-25 11:45:47 +0200584 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100585 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100586
Marek Olšák2fd42002013-10-25 11:45:47 +0200587 if (!reg->Register.Indirect)
588 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
589
590 args[0] = si_shader_ctx->const_resource[buf];
Christian König0f6cf2b2013-03-15 15:53:25 +0100591 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
592 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
593 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
594 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
595 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200596
Christian Königf5298b02013-02-28 14:50:07 +0100597 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100598 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500599
Christian Königf5298b02013-02-28 14:50:07 +0100600 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500601}
602
Michel Dänzer26c71392012-08-24 12:03:11 +0200603/* Initialize arguments for the shader export intrinsic */
604static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900605 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200606 unsigned target,
607 LLVMValueRef *args)
608{
609 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
610 struct lp_build_context *uint =
611 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
612 struct lp_build_context *base = &bld_base->base;
613 unsigned compressed = 0;
614 unsigned chan;
615
Michel Dänzerf402acd2012-08-22 18:15:36 +0200616 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
617 int cbuf = target - V_008DFC_SQ_EXP_MRT;
618
619 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100620 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100621
622 if (compressed)
623 si_shader_ctx->shader->spi_shader_col_format |=
624 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
625 else
626 si_shader_ctx->shader->spi_shader_col_format |=
627 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200628
629 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200630 }
631 }
632
633 if (compressed) {
634 /* Pixel shader needs to pack output values before export */
635 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900636 args[0] = values[2 * chan];
637 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200638 args[chan + 5] =
639 build_intrinsic(base->gallivm->builder,
640 "llvm.SI.packf16",
641 LLVMInt32TypeInContext(base->gallivm->context),
642 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100643 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100644 args[chan + 7] = args[chan + 5] =
645 LLVMBuildBitCast(base->gallivm->builder,
646 args[chan + 5],
647 LLVMFloatTypeInContext(base->gallivm->context),
648 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200649 }
650
651 /* Set COMPR flag */
652 args[4] = uint->one;
653 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900654 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200655 /* +5 because the first output value will be
656 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900657 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200658
659 /* Clear COMPR flag */
660 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200661 }
662
663 /* XXX: This controls which components of the output
664 * registers actually get exported. (e.g bit 0 means export
665 * X component, bit 1 means export Y component, etc.) I'm
666 * hard coding this to 0xf for now. In the future, we might
667 * want to do something else. */
668 args[0] = lp_build_const_int32(base->gallivm, 0xf);
669
670 /* Specify whether the EXEC mask represents the valid mask */
671 args[1] = uint->zero;
672
673 /* Specify whether this is the last export */
674 args[2] = uint->zero;
675
676 /* Specify the target we are exporting */
677 args[3] = lp_build_const_int32(base->gallivm, target);
678
Michel Dänzer26c71392012-08-24 12:03:11 +0200679 /* XXX: We probably need to keep track of the output
680 * values, so we know what we are passing to the next
681 * stage. */
682}
683
Michel Dänzer404b29d2013-11-21 16:45:28 +0900684/* Load from output pointers and initialize arguments for the shader export intrinsic */
685static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
686 LLVMValueRef *out_ptr,
687 unsigned target,
688 LLVMValueRef *args)
689{
690 struct gallivm_state *gallivm = bld_base->base.gallivm;
691 LLVMValueRef values[4];
692 int i;
693
694 for (i = 0; i < 4; i++)
695 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
696
697 si_llvm_init_export_args(bld_base, values, target, args);
698}
699
Michel Dänzer7708a862012-11-02 15:57:30 +0100700static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900701 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100702{
703 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
704 struct gallivm_state *gallivm = bld_base->base.gallivm;
705
Christian Königa0dca442013-03-22 15:59:22 +0100706 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400707 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
708 SI_PARAM_ALPHA_REF);
709
Michel Dänzer7708a862012-11-02 15:57:30 +0100710 LLVMValueRef alpha_pass =
711 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100712 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900713 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400714 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100715 LLVMValueRef arg =
716 lp_build_select(&bld_base->base,
717 alpha_pass,
718 lp_build_const_float(gallivm, 1.0f),
719 lp_build_const_float(gallivm, -1.0f));
720
721 build_intrinsic(gallivm->builder,
722 "llvm.AMDGPU.kill",
723 LLVMVoidTypeInContext(gallivm->context),
724 &arg, 1, 0);
725 } else {
726 build_intrinsic(gallivm->builder,
727 "llvm.AMDGPU.kilp",
728 LLVMVoidTypeInContext(gallivm->context),
729 NULL, 0, 0);
730 }
731}
732
Michel Dänzere3befbc2013-05-15 18:09:50 +0200733static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900734 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200735{
736 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200737 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200738 struct lp_build_context *base = &bld_base->base;
739 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200740 unsigned reg_index;
741 unsigned chan;
742 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200743 LLVMValueRef base_elt;
744 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšák2fd42002013-10-25 11:45:47 +0200745 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, NUM_PIPE_CONST_BUFFERS);
746 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200747
Michel Dänzere3befbc2013-05-15 18:09:50 +0200748 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200749 LLVMValueRef *args = pos[2 + reg_index];
750
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200751 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
752 continue;
753
754 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
755
Michel Dänzere3befbc2013-05-15 18:09:50 +0200756 args[5] =
757 args[6] =
758 args[7] =
759 args[8] = lp_build_const_float(base->gallivm, 0.0f);
760
761 /* Compute dot products of position and user clip plane vectors */
762 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
763 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
764 args[0] = const_resource;
765 args[1] = lp_build_const_int32(base->gallivm,
766 ((reg_index * 4 + chan) * 4 +
767 const_chan) * 4);
768 base_elt = build_intrinsic(base->gallivm->builder,
769 "llvm.SI.load.const",
770 base->elem_type,
771 args, 2,
772 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
773 args[5 + chan] =
774 lp_build_add(base, args[5 + chan],
775 lp_build_mul(base, base_elt,
776 out_elts[const_chan]));
777 }
778 }
779
780 args[0] = lp_build_const_int32(base->gallivm, 0xf);
781 args[1] = uint->zero;
782 args[2] = uint->zero;
783 args[3] = lp_build_const_int32(base->gallivm,
784 V_008DFC_SQ_EXP_POS + 2 + reg_index);
785 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200786 }
787}
788
Marek Olšák8d03d922013-09-01 23:59:06 +0200789static void si_dump_streamout(struct pipe_stream_output_info *so)
790{
791 unsigned i;
792
793 if (so->num_outputs)
794 fprintf(stderr, "STREAMOUT\n");
795
796 for (i = 0; i < so->num_outputs; i++) {
797 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
798 so->output[i].start_component;
799 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
800 i, so->output[i].output_buffer,
801 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
802 so->output[i].register_index,
803 mask & 1 ? "x" : "",
804 mask & 2 ? "y" : "",
805 mask & 4 ? "z" : "",
806 mask & 8 ? "w" : "");
807 }
808}
809
810/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
811 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
812 * or v4i32 (num_channels=3,4). */
813static void build_tbuffer_store(struct si_shader_context *shader,
814 LLVMValueRef rsrc,
815 LLVMValueRef vdata,
816 unsigned num_channels,
817 LLVMValueRef vaddr,
818 LLVMValueRef soffset,
819 unsigned inst_offset,
820 unsigned dfmt,
821 unsigned nfmt,
822 unsigned offen,
823 unsigned idxen,
824 unsigned glc,
825 unsigned slc,
826 unsigned tfe)
827{
828 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
829 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
830 LLVMValueRef args[] = {
831 rsrc,
832 vdata,
833 LLVMConstInt(i32, num_channels, 0),
834 vaddr,
835 soffset,
836 LLVMConstInt(i32, inst_offset, 0),
837 LLVMConstInt(i32, dfmt, 0),
838 LLVMConstInt(i32, nfmt, 0),
839 LLVMConstInt(i32, offen, 0),
840 LLVMConstInt(i32, idxen, 0),
841 LLVMConstInt(i32, glc, 0),
842 LLVMConstInt(i32, slc, 0),
843 LLVMConstInt(i32, tfe, 0)
844 };
845
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900846 /* The instruction offset field has 12 bits */
847 assert(offen || inst_offset < (1 << 12));
848
Marek Olšák8d03d922013-09-01 23:59:06 +0200849 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
850 unsigned func = CLAMP(num_channels, 1, 3) - 1;
851 const char *types[] = {"i32", "v2i32", "v4i32"};
852 char name[256];
853 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
854
855 lp_build_intrinsic(gallivm->builder, name,
856 LLVMVoidTypeInContext(gallivm->context),
857 args, Elements(args));
858}
859
860static void build_streamout_store(struct si_shader_context *shader,
861 LLVMValueRef rsrc,
862 LLVMValueRef vdata,
863 unsigned num_channels,
864 LLVMValueRef vaddr,
865 LLVMValueRef soffset,
866 unsigned inst_offset)
867{
868 static unsigned dfmt[] = {
869 V_008F0C_BUF_DATA_FORMAT_32,
870 V_008F0C_BUF_DATA_FORMAT_32_32,
871 V_008F0C_BUF_DATA_FORMAT_32_32_32,
872 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
873 };
874 assert(num_channels >= 1 && num_channels <= 4);
875
876 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
877 inst_offset, dfmt[num_channels-1],
878 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
879}
880
881/* On SI, the vertex shader is responsible for writing streamout data
882 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900883static void si_llvm_emit_streamout(struct si_shader_context *shader,
884 struct si_shader_output_values *outputs,
885 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200886{
887 struct pipe_stream_output_info *so = &shader->shader->selector->so;
888 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
889 LLVMBuilderRef builder = gallivm->builder;
890 int i, j;
891 struct lp_build_if_state if_ctx;
892
893 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
894
895 LLVMValueRef so_param =
896 LLVMGetParam(shader->radeon_bld.main_fn,
897 shader->param_streamout_config);
898
899 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
900 LLVMValueRef so_vtx_count =
901 LLVMBuildAnd(builder,
902 LLVMBuildLShr(builder, so_param,
903 LLVMConstInt(i32, 16, 0), ""),
904 LLVMConstInt(i32, 127, 0), "");
905
906 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
907 NULL, 0, LLVMReadNoneAttribute);
908
909 /* can_emit = tid < so_vtx_count; */
910 LLVMValueRef can_emit =
911 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
912
913 /* Emit the streamout code conditionally. This actually avoids
914 * out-of-bounds buffer access. The hw tells us via the SGPR
915 * (so_vtx_count) which threads are allowed to emit streamout data. */
916 lp_build_if(&if_ctx, gallivm, can_emit);
917 {
918 /* The buffer offset is computed as follows:
919 * ByteOffset = streamout_offset[buffer_id]*4 +
920 * (streamout_write_index + thread_id)*stride[buffer_id] +
921 * attrib_offset
922 */
923
924 LLVMValueRef so_write_index =
925 LLVMGetParam(shader->radeon_bld.main_fn,
926 shader->param_streamout_write_index);
927
928 /* Compute (streamout_write_index + thread_id). */
929 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
930
931 /* Compute the write offset for each enabled buffer. */
932 LLVMValueRef so_write_offset[4] = {};
933 for (i = 0; i < 4; i++) {
934 if (!so->stride[i])
935 continue;
936
937 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
938 shader->param_streamout_offset[i]);
939 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
940
941 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
942 LLVMConstInt(i32, so->stride[i]*4, 0), "");
943 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
944 }
945
Marek Olšák8d03d922013-09-01 23:59:06 +0200946 /* Write streamout data. */
947 for (i = 0; i < so->num_outputs; i++) {
948 unsigned buf_idx = so->output[i].output_buffer;
949 unsigned reg = so->output[i].register_index;
950 unsigned start = so->output[i].start_component;
951 unsigned num_comps = so->output[i].num_components;
952 LLVMValueRef out[4];
953
954 assert(num_comps && num_comps <= 4);
955 if (!num_comps || num_comps > 4)
956 continue;
957
958 /* Load the output as int. */
959 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +0900960 unsigned outidx = 0;
961
962 while (outidx < noutput && outputs[outidx].index != reg)
963 outidx++;
964
965 if (outidx < noutput)
966 out[j] = LLVMBuildBitCast(builder,
967 outputs[outidx].values[start+j],
968 i32, "");
969 else
970 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +0200971 }
972
Michel Dänzer67e385b2014-01-08 17:48:21 +0900973 if (!out[0])
974 continue;
975
Marek Olšák8d03d922013-09-01 23:59:06 +0200976 /* Pack the output. */
977 LLVMValueRef vdata = NULL;
978
979 switch (num_comps) {
980 case 1: /* as i32 */
981 vdata = out[0];
982 break;
983 case 2: /* as v2i32 */
984 case 3: /* as v4i32 (aligned to 4) */
985 case 4: /* as v4i32 */
986 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
987 for (j = 0; j < num_comps; j++) {
988 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
989 LLVMConstInt(i32, j, 0), "");
990 }
991 break;
992 }
993
994 build_streamout_store(shader, shader->so_buffers[buf_idx],
995 vdata, num_comps,
996 so_write_offset[buf_idx],
997 LLVMConstInt(i32, 0, 0),
998 so->output[i].dst_offset*4);
999 }
1000 }
1001 lp_build_endif(&if_ctx);
1002}
1003
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001004
Michel Dänzer404b29d2013-11-21 16:45:28 +09001005/* Generate export instructions for hardware VS shader stage */
1006static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1007 struct si_shader_output_values *outputs,
1008 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001009{
1010 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +02001011 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001012 struct lp_build_context * base = &bld_base->base;
1013 struct lp_build_context * uint =
1014 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001015 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001016 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001017 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
1018 unsigned semantic_name, semantic_index, semantic_usage;
1019 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001020 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001021 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001022 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001023
Michel Dänzer67e385b2014-01-08 17:48:21 +09001024 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1025 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001026 }
1027
Michel Dänzer404b29d2013-11-21 16:45:28 +09001028 for (i = 0; i < noutput; i++) {
1029 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001030 semantic_index = outputs[i].sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001031 semantic_usage = outputs[i].usage;
Tom Stellarda75c6162012-01-06 17:38:37 -05001032
Michel Dänzer0afeea52013-05-02 14:53:17 +02001033handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001034 /* Select the correct target */
1035 switch(semantic_name) {
1036 case TGSI_SEMANTIC_PSIZE:
1037 shader->vs_out_misc_write = true;
1038 shader->vs_out_point_size = true;
1039 psize_value = outputs[i].values[0];
1040 continue;
1041 case TGSI_SEMANTIC_EDGEFLAG:
1042 shader->vs_out_misc_write = true;
1043 shader->vs_out_edgeflag = true;
1044 edgeflag_value = outputs[i].values[0];
1045 continue;
1046 case TGSI_SEMANTIC_LAYER:
1047 shader->vs_out_misc_write = true;
1048 shader->vs_out_layer = true;
1049 layer_value = outputs[i].values[0];
1050 continue;
1051 case TGSI_SEMANTIC_POSITION:
1052 target = V_008DFC_SQ_EXP_POS;
1053 break;
1054 case TGSI_SEMANTIC_COLOR:
1055 case TGSI_SEMANTIC_BCOLOR:
1056 target = V_008DFC_SQ_EXP_PARAM + param_count;
1057 shader->output[i].param_offset = param_count;
1058 param_count++;
1059 break;
1060 case TGSI_SEMANTIC_CLIPDIST:
1061 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
1062 (1 << semantic_index)))
Marek Olšák053606d2013-11-19 22:07:30 +01001063 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001064 shader->clip_dist_write |=
1065 semantic_usage << (semantic_index << 2);
1066 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1067 break;
1068 case TGSI_SEMANTIC_CLIPVERTEX:
1069 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1070 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001071 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001072 case TGSI_SEMANTIC_FOG:
1073 case TGSI_SEMANTIC_GENERIC:
1074 target = V_008DFC_SQ_EXP_PARAM + param_count;
1075 shader->output[i].param_offset = param_count;
1076 param_count++;
1077 break;
1078 default:
1079 target = 0;
1080 fprintf(stderr,
1081 "Warning: SI unhandled vs output type:%d\n",
1082 semantic_name);
1083 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001084
Michel Dänzer404b29d2013-11-21 16:45:28 +09001085 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001086
Michel Dänzer404b29d2013-11-21 16:45:28 +09001087 if (target >= V_008DFC_SQ_EXP_POS &&
1088 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1089 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1090 args, sizeof(args));
1091 } else {
1092 lp_build_intrinsic(base->gallivm->builder,
1093 "llvm.SI.export",
1094 LLVMVoidTypeInContext(base->gallivm->context),
1095 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001096 }
1097
1098 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1099 semantic_name = TGSI_SEMANTIC_GENERIC;
1100 goto handle_semantic;
1101 }
1102 }
1103
1104 /* We need to add the position output manually if it's missing. */
1105 if (!pos_args[0][0]) {
1106 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1107 pos_args[0][1] = uint->zero; /* EXEC mask */
1108 pos_args[0][2] = uint->zero; /* last export? */
1109 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1110 pos_args[0][4] = uint->zero; /* COMPR flag */
1111 pos_args[0][5] = base->zero; /* X */
1112 pos_args[0][6] = base->zero; /* Y */
1113 pos_args[0][7] = base->zero; /* Z */
1114 pos_args[0][8] = base->one; /* W */
1115 }
1116
1117 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1118 if (shader->vs_out_misc_write) {
1119 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1120 shader->vs_out_point_size |
1121 (shader->vs_out_edgeflag << 1) |
1122 (shader->vs_out_layer << 2));
1123 pos_args[1][1] = uint->zero; /* EXEC mask */
1124 pos_args[1][2] = uint->zero; /* last export? */
1125 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1126 pos_args[1][4] = uint->zero; /* COMPR flag */
1127 pos_args[1][5] = base->zero; /* X */
1128 pos_args[1][6] = base->zero; /* Y */
1129 pos_args[1][7] = base->zero; /* Z */
1130 pos_args[1][8] = base->zero; /* W */
1131
Michel Dänzer404b29d2013-11-21 16:45:28 +09001132 if (shader->vs_out_point_size)
1133 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001134
1135 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001136 /* The output is a float, but the hw expects an integer
1137 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001138 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1139 edgeflag_value,
1140 bld_base->uint_bld.elem_type, "");
1141 edgeflag_value = lp_build_min(&bld_base->int_bld,
1142 edgeflag_value,
1143 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001144
1145 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001146 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1147 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001148 base->elem_type, "");
1149 }
1150
Michel Dänzer404b29d2013-11-21 16:45:28 +09001151 if (shader->vs_out_layer)
1152 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001153 }
1154
1155 for (i = 0; i < 4; i++)
1156 if (pos_args[i][0])
1157 shader->nr_pos_exports++;
1158
1159 pos_idx = 0;
1160 for (i = 0; i < 4; i++) {
1161 if (!pos_args[i][0])
1162 continue;
1163
1164 /* Specify the target we are exporting */
1165 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1166
1167 if (pos_idx == shader->nr_pos_exports)
1168 /* Specify that this is the last export */
1169 pos_args[i][2] = uint->one;
1170
1171 lp_build_intrinsic(base->gallivm->builder,
1172 "llvm.SI.export",
1173 LLVMVoidTypeInContext(base->gallivm->context),
1174 pos_args[i], 9);
1175 }
1176}
1177
Michel Dänzer404b29d2013-11-21 16:45:28 +09001178static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1179{
1180 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1181 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzere884c562014-01-15 15:24:14 +09001182 struct si_shader *es = &si_shader_ctx->shader->shader;
1183 struct si_shader *gs = si_shader_ctx->gs_for_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001184 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1185 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001186 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1187 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001188 LLVMValueRef t_list_ptr;
1189 LLVMValueRef t_list;
1190 unsigned chan;
1191 int i;
1192
1193 while (!tgsi_parse_end_of_tokens(parse)) {
1194 struct tgsi_full_declaration *d =
1195 &parse->FullToken.FullDeclaration;
1196
1197 tgsi_parse_token(parse);
1198
1199 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1200 continue;
1201
Michel Dänzere884c562014-01-15 15:24:14 +09001202 si_store_shader_io_attribs(es, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001203 }
1204
1205 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001206 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1207 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001208 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001209 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001210
Michel Dänzere884c562014-01-15 15:24:14 +09001211 for (i = 0; i < es->noutput; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001212 LLVMValueRef *out_ptr =
Michel Dänzere884c562014-01-15 15:24:14 +09001213 si_shader_ctx->radeon_bld.soa.outputs[es->output[i].index];
1214 int j;
1215
1216 for (j = 0; j < gs->ninput; j++) {
1217 if (gs->input[j].name == es->output[i].name &&
1218 gs->input[j].sid == es->output[i].sid)
1219 break;
1220 }
1221 if (j == gs->ninput)
1222 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001223
1224 for (chan = 0; chan < 4; chan++) {
1225 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001226 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1227
1228 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001229 LLVMGetUndef(i32), soffset,
1230 (4 * gs->input[j].param_offset + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001231 V_008F0C_BUF_DATA_FORMAT_32,
1232 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001233 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001234 }
1235 }
1236}
1237
1238static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1239{
1240 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1241 struct gallivm_state *gallivm = bld_base->base.gallivm;
1242 LLVMValueRef args[2];
1243
1244 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1245 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1246 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1247 LLVMVoidTypeInContext(gallivm->context), args, 2,
1248 LLVMNoUnwindAttribute);
1249}
1250
1251static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1252{
1253 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1254 struct gallivm_state *gallivm = bld_base->base.gallivm;
1255 struct si_pipe_shader *shader = si_shader_ctx->shader;
1256 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1257 struct si_shader_output_values *outputs = NULL;
1258 unsigned noutput = 0;
1259 int i;
1260
1261 while (!tgsi_parse_end_of_tokens(parse)) {
1262 struct tgsi_full_declaration *d =
1263 &parse->FullToken.FullDeclaration;
1264 unsigned index;
1265
1266 tgsi_parse_token(parse);
1267
1268 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1269 continue;
1270
1271 i = si_store_shader_io_attribs(&shader->shader, d);
1272 if (i < 0)
1273 continue;
1274
1275 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1276 (noutput + 1) * sizeof(outputs[0]));
1277 for (index = d->Range.First; index <= d->Range.Last; index++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001278 outputs[noutput].index = index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001279 outputs[noutput].name = d->Semantic.Name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001280 outputs[noutput].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001281 outputs[noutput].usage = d->Declaration.UsageMask;
1282
1283 for (i = 0; i < 4; i++)
1284 outputs[noutput].values[i] =
1285 LLVMBuildLoad(gallivm->builder,
1286 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1287 "");
1288 }
1289 noutput++;
1290 }
1291
1292 si_llvm_export_vs(bld_base, outputs, noutput);
1293 FREE(outputs);
1294}
1295
Michel Dänzer51f89a02013-12-09 15:33:53 +09001296static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1297{
1298 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
1299 struct si_shader * shader = &si_shader_ctx->shader->shader;
1300 struct lp_build_context * base = &bld_base->base;
1301 struct lp_build_context * uint =
1302 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1303 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1304 LLVMValueRef args[9];
1305 LLVMValueRef last_args[9] = { 0 };
1306 unsigned semantic_name;
1307 int depth_index = -1, stencil_index = -1;
1308 int i;
1309
1310 while (!tgsi_parse_end_of_tokens(parse)) {
1311 struct tgsi_full_declaration *d =
1312 &parse->FullToken.FullDeclaration;
1313 unsigned target;
1314 unsigned index;
1315
1316 tgsi_parse_token(parse);
1317
1318 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
1319 parse->FullToken.FullProperty.Property.PropertyName ==
1320 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
1321 shader->fs_write_all = TRUE;
1322
1323 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1324 continue;
1325
1326 i = si_store_shader_io_attribs(shader, d);
1327 if (i < 0)
1328 continue;
1329
1330 semantic_name = d->Semantic.Name;
1331 for (index = d->Range.First; index <= d->Range.Last; index++) {
1332 /* Select the correct target */
1333 switch(semantic_name) {
1334 case TGSI_SEMANTIC_POSITION:
1335 depth_index = index;
1336 continue;
1337 case TGSI_SEMANTIC_STENCIL:
1338 stencil_index = index;
1339 continue;
1340 case TGSI_SEMANTIC_COLOR:
1341 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1342 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001343 LLVMBuildStore(bld_base->base.gallivm->builder,
1344 bld_base->base.one,
1345 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1346
Michel Dänzer51f89a02013-12-09 15:33:53 +09001347 if (d->Semantic.Index == 0 &&
1348 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001349 si_alpha_test(bld_base,
1350 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001351 break;
1352 default:
1353 target = 0;
1354 fprintf(stderr,
1355 "Warning: SI unhandled fs output type:%d\n",
1356 semantic_name);
1357 }
1358
Michel Dänzer404b29d2013-11-21 16:45:28 +09001359 si_llvm_init_export_args_load(bld_base,
1360 si_shader_ctx->radeon_bld.soa.outputs[index],
1361 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001362
1363 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001364 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001365 if (last_args[0]) {
1366 lp_build_intrinsic(base->gallivm->builder,
1367 "llvm.SI.export",
1368 LLVMVoidTypeInContext(base->gallivm->context),
1369 last_args, 9);
1370 }
1371
Marek Olšák0eb528a2013-12-04 13:24:22 +01001372 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001373 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001374
1375 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1376 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1377 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1378 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001379 si_llvm_init_export_args_load(bld_base,
1380 si_shader_ctx->radeon_bld.soa.outputs[index],
1381 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001382 lp_build_intrinsic(base->gallivm->builder,
1383 "llvm.SI.export",
1384 LLVMVoidTypeInContext(base->gallivm->context),
1385 args, 9);
1386 }
1387 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001388 } else {
1389 lp_build_intrinsic(base->gallivm->builder,
1390 "llvm.SI.export",
1391 LLVMVoidTypeInContext(base->gallivm->context),
1392 args, 9);
1393 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001394 }
1395 }
1396
Michel Dänzer1a616c12012-11-13 17:35:09 +01001397 if (depth_index >= 0 || stencil_index >= 0) {
1398 LLVMValueRef out_ptr;
1399 unsigned mask = 0;
1400
1401 /* Specify the target we are exporting */
1402 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1403
1404 if (depth_index >= 0) {
1405 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1406 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1407 mask |= 0x1;
1408
1409 if (stencil_index < 0) {
1410 args[6] =
1411 args[7] =
1412 args[8] = args[5];
1413 }
1414 }
1415
1416 if (stencil_index >= 0) {
1417 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
1418 args[7] =
1419 args[8] =
1420 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001421 /* Only setting the stencil component bit (0x2) here
1422 * breaks some stencil piglit tests
1423 */
1424 mask |= 0x3;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001425
1426 if (depth_index < 0)
1427 args[5] = args[6];
1428 }
1429
1430 /* Specify which components to enable */
1431 args[0] = lp_build_const_int32(base->gallivm, mask);
1432
1433 args[1] =
1434 args[2] =
1435 args[4] = uint->zero;
1436
1437 if (last_args[0])
1438 lp_build_intrinsic(base->gallivm->builder,
1439 "llvm.SI.export",
1440 LLVMVoidTypeInContext(base->gallivm->context),
1441 args, 9);
1442 else
1443 memcpy(last_args, args, sizeof(args));
1444 }
1445
Michel Dänzer51f89a02013-12-09 15:33:53 +09001446 if (!last_args[0]) {
1447 /* Specify which components to enable */
1448 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001449
Michel Dänzer51f89a02013-12-09 15:33:53 +09001450 /* Specify the target we are exporting */
1451 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001452
Michel Dänzer51f89a02013-12-09 15:33:53 +09001453 /* Set COMPR flag to zero to export data as 32-bit */
1454 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001455
Michel Dänzer51f89a02013-12-09 15:33:53 +09001456 /* dummy bits */
1457 last_args[5]= uint->zero;
1458 last_args[6]= uint->zero;
1459 last_args[7]= uint->zero;
1460 last_args[8]= uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001461
Michel Dänzer51f89a02013-12-09 15:33:53 +09001462 si_shader_ctx->shader->spi_shader_col_format |=
1463 V_028714_SPI_SHADER_32_ABGR;
1464 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001465 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001466
1467 /* Specify whether the EXEC mask represents the valid mask */
1468 last_args[1] = uint->one;
1469
1470 /* Specify that this is the last export */
1471 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1472
1473 lp_build_intrinsic(base->gallivm->builder,
1474 "llvm.SI.export",
1475 LLVMVoidTypeInContext(base->gallivm->context),
1476 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001477}
1478
Marek Olšák4855acd2013-08-06 15:08:54 +02001479static const struct lp_build_tgsi_action txf_action;
1480
1481static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1482 struct lp_build_tgsi_context * bld_base,
1483 struct lp_build_emit_data * emit_data);
1484
Tom Stellarda75c6162012-01-06 17:38:37 -05001485static void tex_fetch_args(
1486 struct lp_build_tgsi_context * bld_base,
1487 struct lp_build_emit_data * emit_data)
1488{
Christian König55fe5cc2013-03-04 16:30:06 +01001489 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001490 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001491 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001492 unsigned opcode = inst->Instruction.Opcode;
1493 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001494 LLVMValueRef coords[4];
1495 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001496 int ref_pos;
1497 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001498 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001499 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001500 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1501 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1502
1503 if (target == TGSI_TEXTURE_BUFFER) {
1504 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1505 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1506 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1507 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1508
1509 /* Truncate v32i8 to v16i8. */
1510 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1511 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1512 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1513 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1514
1515 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1516 emit_data->args[0] = res;
1517 emit_data->args[1] = bld_base->uint_bld.zero;
1518 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1519 emit_data->arg_count = 3;
1520 return;
1521 }
Tom Stellard467f5162012-05-16 15:15:35 -04001522
Michel Dänzer120efee2013-01-25 12:10:11 +01001523 /* Fetch and project texture coordinates */
1524 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001525 for (chan = 0; chan < 3; chan++ ) {
1526 coords[chan] = lp_build_emit_fetch(bld_base,
1527 emit_data->inst, 0,
1528 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001529 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001530 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1531 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001532 coords[chan],
1533 coords[3]);
1534 }
1535
Michel Dänzer120efee2013-01-25 12:10:11 +01001536 if (opcode == TGSI_OPCODE_TXP)
1537 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001538
Michel Dänzer120efee2013-01-25 12:10:11 +01001539 /* Pack LOD bias value */
1540 if (opcode == TGSI_OPCODE_TXB)
1541 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001542
Michel Dänzer0495adb2013-05-06 12:45:14 +02001543 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001544 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001545
1546 /* Pack depth comparison value */
1547 switch (target) {
1548 case TGSI_TEXTURE_SHADOW1D:
1549 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1550 case TGSI_TEXTURE_SHADOW2D:
1551 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001552 case TGSI_TEXTURE_SHADOWCUBE:
1553 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001554 assert(ref_pos >= 0);
1555 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001556 break;
1557 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1558 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001559 }
1560
Michel Dänzera6b83c02013-02-21 16:10:55 +01001561 /* Pack user derivatives */
1562 if (opcode == TGSI_OPCODE_TXD) {
1563 for (chan = 0; chan < 2; chan++) {
1564 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1565 if (num_coords > 1)
1566 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1567 }
1568 }
1569
Michel Dänzer120efee2013-01-25 12:10:11 +01001570 /* Pack texture coordinates */
1571 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001572 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001573 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001574 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001575 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001576
Marek Olšákd2bd6342013-09-18 15:40:21 +02001577 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001578 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001579 address[count++] = coords[3];
1580
1581 if (count > 16) {
1582 assert(!"Cannot handle more than 16 texture address parameters");
1583 count = 16;
1584 }
1585
1586 for (chan = 0; chan < count; chan++ ) {
1587 address[chan] = LLVMBuildBitCast(gallivm->builder,
1588 address[chan],
1589 LLVMInt32TypeInContext(gallivm->context),
1590 "");
1591 }
1592
Marek Olšák4855acd2013-08-06 15:08:54 +02001593 /* Adjust the sample index according to FMASK.
1594 *
1595 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1596 * which is the identity mapping. Each nibble says which physical sample
1597 * should be fetched to get that sample.
1598 *
1599 * For example, 0x11111100 means there are only 2 samples stored and
1600 * the second sample covers 3/4 of the pixel. When reading samples 0
1601 * and 1, return physical sample 0 (determined by the first two 0s
1602 * in FMASK), otherwise return physical sample 1.
1603 *
1604 * The sample index should be adjusted as follows:
1605 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1606 */
1607 if (target == TGSI_TEXTURE_2D_MSAA ||
1608 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1609 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1610 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001611 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001612 unsigned txf_count = count;
1613
Marek Olšákd2bd6342013-09-18 15:40:21 +02001614 memcpy(txf_address, address, sizeof(txf_address));
1615
1616 if (target == TGSI_TEXTURE_2D_MSAA) {
1617 txf_address[2] = bld_base->uint_bld.zero;
1618 }
1619 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001620
1621 /* Pad to a power-of-two size. */
1622 while (txf_count < util_next_power_of_two(txf_count))
1623 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1624
1625 /* Read FMASK using TXF. */
1626 txf_emit_data.chan = 0;
1627 txf_emit_data.dst_type = LLVMVectorType(
1628 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1629 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1630 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
Marek Olšákd2bd6342013-09-18 15:40:21 +02001631 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1632 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
Marek Olšák4855acd2013-08-06 15:08:54 +02001633 txf_emit_data.arg_count = 3;
1634
1635 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1636
1637 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001638 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1639 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1640
1641 /* Apply the formula. */
1642 LLVMValueRef fmask =
1643 LLVMBuildExtractElement(gallivm->builder,
1644 txf_emit_data.output[0],
1645 uint_bld->zero, "");
1646
Marek Olšákd2bd6342013-09-18 15:40:21 +02001647 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001648
1649 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001650 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001651
1652 LLVMValueRef shifted_fmask =
1653 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1654
1655 LLVMValueRef final_sample =
1656 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1657
1658 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1659 * resource descriptor is 0 (invalid),
1660 */
1661 LLVMValueRef fmask_desc =
1662 LLVMBuildBitCast(gallivm->builder,
1663 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1664 LLVMVectorType(uint_bld->elem_type, 8), "");
1665
1666 LLVMValueRef fmask_word1 =
1667 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1668 uint_bld->one, "");
1669
1670 LLVMValueRef word1_is_nonzero =
1671 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1672 fmask_word1, uint_bld->zero, "");
1673
Marek Olšákd2bd6342013-09-18 15:40:21 +02001674 /* Replace the MSAA sample index. */
1675 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001676 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001677 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001678 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001679
Michel Dänzer36231112013-05-02 09:44:45 +02001680 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001681 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001682
1683 if (opcode == TGSI_OPCODE_TXF) {
1684 /* add tex offsets */
1685 if (inst->Texture.NumOffsets) {
1686 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1687 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1688 const struct tgsi_texture_offset * off = inst->TexOffsets;
1689
1690 assert(inst->Texture.NumOffsets == 1);
1691
Marek Olšákdefedc02013-09-18 15:36:38 +02001692 switch (target) {
1693 case TGSI_TEXTURE_3D:
1694 address[2] = lp_build_add(uint_bld, address[2],
1695 bld->immediates[off->Index][off->SwizzleZ]);
1696 /* fall through */
1697 case TGSI_TEXTURE_2D:
1698 case TGSI_TEXTURE_SHADOW2D:
1699 case TGSI_TEXTURE_RECT:
1700 case TGSI_TEXTURE_SHADOWRECT:
1701 case TGSI_TEXTURE_2D_ARRAY:
1702 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001703 address[1] =
1704 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001705 bld->immediates[off->Index][off->SwizzleY]);
1706 /* fall through */
1707 case TGSI_TEXTURE_1D:
1708 case TGSI_TEXTURE_SHADOW1D:
1709 case TGSI_TEXTURE_1D_ARRAY:
1710 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1711 address[0] =
1712 lp_build_add(uint_bld, address[0],
1713 bld->immediates[off->Index][off->SwizzleX]);
1714 break;
1715 /* texture offsets do not apply to other texture targets */
1716 }
Michel Dänzer36231112013-05-02 09:44:45 +02001717 }
1718
1719 emit_data->dst_type = LLVMVectorType(
1720 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1721 4);
1722
1723 emit_data->arg_count = 3;
1724 } else {
1725 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001726 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001727
1728 emit_data->dst_type = LLVMVectorType(
1729 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1730 4);
1731
1732 emit_data->arg_count = 4;
1733 }
1734
1735 /* Dimensions */
1736 emit_data->args[emit_data->arg_count - 1] =
1737 lp_build_const_int32(bld_base->base.gallivm, target);
1738
Michel Dänzer120efee2013-01-25 12:10:11 +01001739 /* Pad to power of two vector */
1740 while (count < util_next_power_of_two(count))
1741 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1742
Christian Königccf3e8f2013-03-26 15:09:27 +01001743 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001744}
1745
Michel Dänzer07eddc42013-02-06 15:43:10 +01001746static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1747 struct lp_build_tgsi_context * bld_base,
1748 struct lp_build_emit_data * emit_data)
1749{
1750 struct lp_build_context * base = &bld_base->base;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001751 char intr_name[127];
Michel Dänzer07eddc42013-02-06 15:43:10 +01001752
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001753 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1754 emit_data->output[emit_data->chan] = build_intrinsic(
1755 base->gallivm->builder,
1756 "llvm.SI.vs.load.input", emit_data->dst_type,
1757 emit_data->args, emit_data->arg_count,
1758 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1759 return;
1760 }
1761
Michel Dänzer07eddc42013-02-06 15:43:10 +01001762 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001763 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001764
Christian König44e32242013-03-20 12:10:35 +01001765 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001766 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001767 emit_data->args, emit_data->arg_count,
1768 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001769}
1770
Michel Dänzer0495adb2013-05-06 12:45:14 +02001771static void txq_fetch_args(
1772 struct lp_build_tgsi_context * bld_base,
1773 struct lp_build_emit_data * emit_data)
1774{
1775 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1776 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001777 struct gallivm_state *gallivm = bld_base->base.gallivm;
1778
1779 if (inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1780 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1781 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1782
1783 /* Read the size from the buffer descriptor directly. */
1784 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1785 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1786 size = LLVMBuildExtractElement(gallivm->builder, size,
1787 lp_build_const_int32(gallivm, 2), "");
1788 emit_data->args[0] = size;
1789 return;
1790 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02001791
1792 /* Mip level */
1793 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1794
1795 /* Resource */
1796 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1797
1798 /* Dimensions */
1799 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1800 inst->Texture.Texture);
1801
1802 emit_data->arg_count = 3;
1803
1804 emit_data->dst_type = LLVMVectorType(
1805 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1806 4);
1807}
1808
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001809static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
1810 struct lp_build_tgsi_context * bld_base,
1811 struct lp_build_emit_data * emit_data)
1812{
1813 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1814 /* Just return the buffer size. */
1815 emit_data->output[emit_data->chan] = emit_data->args[0];
1816 return;
1817 }
1818
1819 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
1820}
1821
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001822#if HAVE_LLVM >= 0x0304
1823
1824static void si_llvm_emit_ddxy(
1825 const struct lp_build_tgsi_action * action,
1826 struct lp_build_tgsi_context * bld_base,
1827 struct lp_build_emit_data * emit_data)
1828{
1829 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1830 struct gallivm_state *gallivm = bld_base->base.gallivm;
1831 struct lp_build_context * base = &bld_base->base;
1832 const struct tgsi_full_instruction *inst = emit_data->inst;
1833 unsigned opcode = inst->Instruction.Opcode;
1834 LLVMValueRef indices[2];
1835 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1836 LLVMValueRef tl, trbl, result[4];
1837 LLVMTypeRef i32;
1838 unsigned swizzle[4];
1839 unsigned c;
1840
1841 i32 = LLVMInt32TypeInContext(gallivm->context);
1842
1843 indices[0] = bld_base->uint_bld.zero;
1844 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1845 NULL, 0, LLVMReadNoneAttribute);
1846 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1847 indices, 2, "");
1848
1849 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1850 lp_build_const_int32(gallivm, 0xfffffffc), "");
1851 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1852 indices, 2, "");
1853
1854 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1855 lp_build_const_int32(gallivm,
1856 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1857 "");
1858 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1859 indices, 2, "");
1860
1861 for (c = 0; c < 4; ++c) {
1862 unsigned i;
1863
1864 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1865 for (i = 0; i < c; ++i) {
1866 if (swizzle[i] == swizzle[c]) {
1867 result[c] = result[i];
1868 break;
1869 }
1870 }
1871 if (i != c)
1872 continue;
1873
1874 LLVMBuildStore(gallivm->builder,
1875 LLVMBuildBitCast(gallivm->builder,
1876 lp_build_emit_fetch(bld_base, inst, 0, c),
1877 i32, ""),
1878 store_ptr);
1879
1880 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1881 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1882
1883 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1884 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1885
1886 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1887 }
1888
1889 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1890}
1891
1892#endif /* HAVE_LLVM >= 0x0304 */
1893
Michel Dänzer404b29d2013-11-21 16:45:28 +09001894/* Emit one vertex from the geometry shader */
1895static void si_llvm_emit_vertex(
1896 const struct lp_build_tgsi_action *action,
1897 struct lp_build_tgsi_context *bld_base,
1898 struct lp_build_emit_data *emit_data)
1899{
1900 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001901 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001902 struct si_shader *shader = &si_shader_ctx->shader->shader;
1903 struct gallivm_state *gallivm = bld_base->base.gallivm;
1904 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001905 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1906 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001907 LLVMValueRef gs_next_vertex;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001908 LLVMValueRef t_list_ptr;
1909 LLVMValueRef t_list;
1910 LLVMValueRef args[2];
1911 unsigned chan;
1912 int i;
1913
1914 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001915 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1916 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001917 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001918 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001919
1920 if (shader->noutput == 0) {
1921 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1922
1923 while (!tgsi_parse_end_of_tokens(parse)) {
1924 tgsi_parse_token(parse);
1925
Michel Dänzer7c7d7382014-01-09 15:33:34 +09001926 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
1927 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
1928
1929 if (d->Declaration.File == TGSI_FILE_OUTPUT)
1930 si_store_shader_io_attribs(shader, d);
1931 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001932 }
1933 }
1934
1935 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001936 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001937 for (i = 0; i < shader->noutput; i++) {
1938 LLVMValueRef *out_ptr =
1939 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
1940
1941 for (chan = 0; chan < 4; chan++) {
1942 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001943 LLVMValueRef voffset =
1944 lp_build_const_int32(gallivm, (i * 4 + chan) *
1945 shader->gs_max_out_vertices);
1946
1947 voffset = lp_build_add(uint, voffset, gs_next_vertex);
1948 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001949
1950 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1951
1952 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
1953 voffset, soffset, 0,
1954 V_008F0C_BUF_DATA_FORMAT_32,
1955 V_008F0C_BUF_NUM_FORMAT_UINT,
1956 1, 0, 1, 1, 0);
1957 }
1958 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09001959 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
1960 lp_build_const_int32(gallivm, 1));
1961 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001962
1963 /* Signal vertex emission */
1964 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
1965 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1966 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1967 LLVMVoidTypeInContext(gallivm->context), args, 2,
1968 LLVMNoUnwindAttribute);
1969}
1970
1971/* Cut one primitive from the geometry shader */
1972static void si_llvm_emit_primitive(
1973 const struct lp_build_tgsi_action *action,
1974 struct lp_build_tgsi_context *bld_base,
1975 struct lp_build_emit_data *emit_data)
1976{
1977 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1978 struct gallivm_state *gallivm = bld_base->base.gallivm;
1979 LLVMValueRef args[2];
1980
1981 /* Signal primitive cut */
1982 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
1983 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1984 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1985 LLVMVoidTypeInContext(gallivm->context), args, 2,
1986 LLVMNoUnwindAttribute);
1987}
1988
Tom Stellarda75c6162012-01-06 17:38:37 -05001989static const struct lp_build_tgsi_action tex_action = {
1990 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001991 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001992 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001993};
1994
Michel Dänzer3e205132012-11-06 17:39:01 +01001995static const struct lp_build_tgsi_action txb_action = {
1996 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001997 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001998 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001999};
2000
Michel Dänzera6b83c02013-02-21 16:10:55 +01002001#if HAVE_LLVM >= 0x0304
2002static const struct lp_build_tgsi_action txd_action = {
2003 .fetch_args = tex_fetch_args,
2004 .emit = build_tex_intrinsic,
2005 .intr_name = "llvm.SI.sampled."
2006};
2007#endif
2008
Michel Dänzer36231112013-05-02 09:44:45 +02002009static const struct lp_build_tgsi_action txf_action = {
2010 .fetch_args = tex_fetch_args,
2011 .emit = build_tex_intrinsic,
2012 .intr_name = "llvm.SI.imageload."
2013};
2014
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002015static const struct lp_build_tgsi_action txl_action = {
2016 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002017 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01002018 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002019};
2020
Michel Dänzer0495adb2013-05-06 12:45:14 +02002021static const struct lp_build_tgsi_action txq_action = {
2022 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002023 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002024 .intr_name = "llvm.SI.resinfo"
2025};
2026
Christian König206f0592013-03-20 14:37:21 +01002027static void create_meta_data(struct si_shader_context *si_shader_ctx)
2028{
2029 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2030 LLVMValueRef args[3];
2031
2032 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2033 args[1] = 0;
2034 args[2] = lp_build_const_int32(gallivm, 1);
2035
2036 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2037}
2038
Christian König55fe5cc2013-03-04 16:30:06 +01002039static void create_function(struct si_shader_context *si_shader_ctx)
2040{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002041 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2042 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002043 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzerf8e16012014-01-28 15:39:30 +09002044 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002045 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002046
Christian König55fe5cc2013-03-04 16:30:06 +01002047 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002048 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002049 f32 = LLVMFloatTypeInContext(gallivm->context);
2050 v2i32 = LLVMVectorType(i32, 2);
2051 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01002052
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002053 params[SI_PARAM_CONST] = LLVMPointerType(
2054 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
Michel Dänzerf8e16012014-01-28 15:39:30 +09002055 params[SI_PARAM_RW_BUFFERS] = params[SI_PARAM_CONST];
2056
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002057 /* We assume at most 16 textures per program at the moment.
2058 * This need probably need to be changed to support bindless textures */
2059 params[SI_PARAM_SAMPLER] = LLVMPointerType(
2060 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
2061 params[SI_PARAM_RESOURCE] = LLVMPointerType(
2062 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002063
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002064 switch (si_shader_ctx->type) {
2065 case TGSI_PROCESSOR_VERTEX:
2066 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01002067 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002068 num_params = SI_PARAM_START_INSTANCE+1;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002069 if (shader->key.vs.as_es) {
2070 params[SI_PARAM_ES2GS_OFFSET] = i32;
2071 num_params++;
2072 } else {
2073 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002074
Michel Dänzer404b29d2013-11-21 16:45:28 +09002075 /* Streamout SGPRs. */
2076 if (shader->selector->so.num_outputs) {
2077 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2078 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2079 }
2080 /* A streamout buffer offset is loaded if the stride is non-zero. */
2081 for (i = 0; i < 4; i++) {
2082 if (!shader->selector->so.stride[i])
2083 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002084
Michel Dänzer404b29d2013-11-21 16:45:28 +09002085 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2086 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002087 }
2088
2089 last_sgpr = num_params-1;
2090
2091 /* VGPRs */
2092 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2093 params[num_params++] = i32; /* unused*/
2094 params[num_params++] = i32; /* unused */
2095 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002096 break;
Christian König0666ffd2013-03-05 15:07:39 +01002097
Michel Dänzer404b29d2013-11-21 16:45:28 +09002098 case TGSI_PROCESSOR_GEOMETRY:
2099 params[SI_PARAM_GS2VS_OFFSET] = i32;
2100 params[SI_PARAM_GS_WAVE_ID] = i32;
2101 last_sgpr = SI_PARAM_GS_WAVE_ID;
2102
2103 /* VGPRs */
2104 params[SI_PARAM_VTX0_OFFSET] = i32;
2105 params[SI_PARAM_VTX1_OFFSET] = i32;
2106 params[SI_PARAM_PRIMITIVE_ID] = i32;
2107 params[SI_PARAM_VTX2_OFFSET] = i32;
2108 params[SI_PARAM_VTX3_OFFSET] = i32;
2109 params[SI_PARAM_VTX4_OFFSET] = i32;
2110 params[SI_PARAM_VTX5_OFFSET] = i32;
2111 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2112 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2113 break;
2114
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002115 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002116 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002117 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002118 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002119 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2120 params[SI_PARAM_PERSP_CENTER] = v2i32;
2121 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2122 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2123 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2124 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2125 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2126 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2127 params[SI_PARAM_POS_X_FLOAT] = f32;
2128 params[SI_PARAM_POS_Y_FLOAT] = f32;
2129 params[SI_PARAM_POS_Z_FLOAT] = f32;
2130 params[SI_PARAM_POS_W_FLOAT] = f32;
2131 params[SI_PARAM_FRONT_FACE] = f32;
2132 params[SI_PARAM_ANCILLARY] = f32;
2133 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2134 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002135 num_params = SI_PARAM_POS_FIXED_PT+1;
2136 break;
2137
2138 default:
2139 assert(0 && "unimplemented shader");
2140 return;
Christian Königc4973212013-03-05 12:14:02 +01002141 }
Christian König55fe5cc2013-03-04 16:30:06 +01002142
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002143 assert(num_params <= Elements(params));
2144 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002145 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002146
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002147 for (i = 0; i <= last_sgpr; ++i) {
2148 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002149 switch (i) {
2150 default:
2151 LLVMAddAttribute(P, LLVMInRegAttribute);
2152 break;
2153#if HAVE_LLVM >= 0x0304
2154 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2155 * to move load. Inputs are constant so this is fine. */
2156 case SI_PARAM_CONST:
2157 case SI_PARAM_SAMPLER:
2158 case SI_PARAM_RESOURCE:
2159 LLVMAddAttribute(P, LLVMByValAttribute);
2160 break;
2161#endif
2162 }
Christian Königcf9b31f2013-03-21 18:30:23 +01002163 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002164
2165#if HAVE_LLVM >= 0x0304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002166 if (bld_base->info &&
2167 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2168 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002169 si_shader_ctx->ddxy_lds =
2170 LLVMAddGlobalInAddressSpace(gallivm->module,
2171 LLVMArrayType(i32, 64),
2172 "ddxy_lds",
2173 LOCAL_ADDR_SPACE);
2174#endif
Christian König55fe5cc2013-03-04 16:30:06 +01002175}
Tom Stellarda75c6162012-01-06 17:38:37 -05002176
Christian König0f6cf2b2013-03-15 15:53:25 +01002177static void preload_constants(struct si_shader_context *si_shader_ctx)
2178{
2179 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2180 struct gallivm_state * gallivm = bld_base->base.gallivm;
2181 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002182 unsigned buf;
2183 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002184
Marek Olšák2fd42002013-10-25 11:45:47 +02002185 for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) {
2186 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002187
Marek Olšák2fd42002013-10-25 11:45:47 +02002188 if (num_const == 0)
2189 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002190
Marek Olšák2fd42002013-10-25 11:45:47 +02002191 /* Allocate space for the constant values */
2192 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002193
Marek Olšák2fd42002013-10-25 11:45:47 +02002194 /* Load the resource descriptor */
2195 si_shader_ctx->const_resource[buf] =
2196 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002197
Marek Olšák2fd42002013-10-25 11:45:47 +02002198 /* Load the constants, we rely on the code sinking to do the rest */
2199 for (i = 0; i < num_const * 4; ++i) {
2200 LLVMValueRef args[2] = {
2201 si_shader_ctx->const_resource[buf],
2202 lp_build_const_int32(gallivm, i * 4)
2203 };
2204 si_shader_ctx->constants[buf][i] =
2205 build_intrinsic(gallivm->builder, "llvm.SI.load.const",
2206 bld_base->base.elem_type, args, 2,
2207 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2208 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002209 }
2210}
2211
Christian König1c100182013-03-17 16:02:42 +01002212static void preload_samplers(struct si_shader_context *si_shader_ctx)
2213{
2214 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2215 struct gallivm_state * gallivm = bld_base->base.gallivm;
2216 const struct tgsi_shader_info * info = bld_base->info;
2217
2218 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2219
2220 LLVMValueRef res_ptr, samp_ptr;
2221 LLVMValueRef offset;
2222
2223 if (num_samplers == 0)
2224 return;
2225
2226 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02002227 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002228 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2229
2230 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2231 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2232
2233 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2234 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002235 /* Resource */
2236 offset = lp_build_const_int32(gallivm, i);
2237 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2238
2239 /* Sampler */
2240 offset = lp_build_const_int32(gallivm, i);
2241 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002242
2243 /* FMASK resource */
2244 if (info->is_msaa_sampler[i]) {
2245 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
2246 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
2247 build_indexed_load(si_shader_ctx, res_ptr, offset);
2248 }
Christian König1c100182013-03-17 16:02:42 +01002249 }
2250}
2251
Marek Olšák8d03d922013-09-01 23:59:06 +02002252static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2253{
2254 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2255 struct gallivm_state * gallivm = bld_base->base.gallivm;
2256 unsigned i;
2257
Michel Dänzer67e385b2014-01-08 17:48:21 +09002258 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2259 si_shader_ctx->shader->key.vs.as_es ||
2260 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002261 return;
2262
2263 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002264 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002265
2266 /* Load the resources, we rely on the code sinking to do the rest */
2267 for (i = 0; i < 4; ++i) {
2268 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002269 LLVMValueRef offset = lp_build_const_int32(gallivm,
2270 SI_RW_SO + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002271
2272 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2273 }
2274 }
2275}
2276
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002277int si_compile_llvm(struct si_context *sctx, struct si_pipe_shader *shader,
Tom Stellard302f53d2012-10-25 13:50:10 -04002278 LLVMModuleRef mod)
2279{
Tom Stellard302f53d2012-10-25 13:50:10 -04002280 unsigned i;
2281 uint32_t *ptr;
Tom Stellard7782d192013-04-04 09:57:13 -07002282 struct radeon_llvm_binary binary;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002283 bool dump = r600_can_dump_shader(&sctx->screen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002284 shader->selector ? shader->selector->tokens : NULL);
Tom Stellard7782d192013-04-04 09:57:13 -07002285 memset(&binary, 0, sizeof(binary));
2286 radeon_llvm_compile(mod, &binary,
Marek Olšák65dc5882014-01-22 02:42:20 +01002287 r600_get_llvm_processor_name(sctx->screen->b.family), dump);
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002288 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002289 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002290 for (i = 0; i < binary.code_size; i+=4 ) {
2291 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2292 binary.code[i + 2], binary.code[i + 1],
2293 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002294 }
2295 }
2296
Tom Stellardd50343d2013-04-04 16:21:06 -04002297 /* XXX: We may be able to emit some of these values directly rather than
2298 * extracting fields to be emitted later.
2299 */
2300 for (i = 0; i < binary.config_size; i+= 8) {
2301 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2302 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2303 switch (reg) {
2304 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2305 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2306 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2307 case R_00B848_COMPUTE_PGM_RSRC1:
2308 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2309 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2310 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002311 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2312 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2313 break;
2314 case R_00B84C_COMPUTE_PGM_RSRC2:
2315 shader->lds_size = G_00B84C_LDS_SIZE(value);
2316 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002317 case R_0286CC_SPI_PS_INPUT_ENA:
2318 shader->spi_ps_input_ena = value;
2319 break;
2320 default:
2321 fprintf(stderr, "Warning: Compiler emitted unknown "
2322 "config register: 0x%x\n", reg);
2323 break;
2324 }
2325 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002326
2327 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02002328 r600_resource_reference(&shader->bo, NULL);
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002329 shader->bo = si_resource_create_custom(sctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04002330 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002331 if (shader->bo == NULL) {
2332 return -ENOMEM;
2333 }
2334
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002335 ptr = (uint32_t*)sctx->b.ws->buffer_map(shader->bo->cs_buf, sctx->b.rings.gfx.cs, PIPE_TRANSFER_WRITE);
Andreas Hartmetz0b57fc12014-01-11 15:56:47 +01002336 if (0 /*SI_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04002337 for (i = 0; i < binary.code_size / 4; ++i) {
2338 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04002339 }
2340 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04002341 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002342 }
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002343 sctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002344
Tom Stellard7782d192013-04-04 09:57:13 -07002345 free(binary.code);
2346 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04002347
2348 return 0;
2349}
2350
Michel Dänzer404b29d2013-11-21 16:45:28 +09002351/* Generate code for the hardware VS shader stage to go with a geometry shader */
2352static int si_generate_gs_copy_shader(struct si_context *sctx,
2353 struct si_shader_context *si_shader_ctx,
2354 bool dump)
2355{
2356 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2357 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2358 struct lp_build_context *base = &bld_base->base;
2359 struct lp_build_context *uint = &bld_base->uint_bld;
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002360 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002361 struct si_shader *gs = &si_shader_ctx->shader->selector->current->shader;
2362 struct si_shader_output_values *outputs;
2363 LLVMValueRef t_list_ptr, t_list;
2364 LLVMValueRef args[9];
2365 int i, r;
2366
2367 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2368
2369 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
2370 si_shader_ctx->gs_for_vs = gs;
2371
2372 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2373
2374 create_meta_data(si_shader_ctx);
2375 create_function(si_shader_ctx);
2376 preload_streamout_buffers(si_shader_ctx);
2377
2378 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002379 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2380 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002381 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002382 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002383
2384 args[0] = t_list;
2385 args[1] = lp_build_mul_imm(uint,
2386 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2387 si_shader_ctx->param_vertex_id),
2388 4);
2389 args[3] = uint->zero;
2390 args[4] = uint->one; /* OFFEN */
2391 args[5] = uint->zero; /* IDXEN */
2392 args[6] = uint->one; /* GLC */
2393 args[7] = uint->one; /* SLC */
2394 args[8] = uint->zero; /* TFE */
2395
2396 /* Fetch vertex data from GSVS ring */
2397 for (i = 0; i < gs->noutput; ++i) {
2398 struct si_shader_output *out = gs->output + i;
2399 unsigned chan;
2400
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002401 shader->output[i] = *out;
2402
Michel Dänzer404b29d2013-11-21 16:45:28 +09002403 outputs[i].name = out->name;
2404 outputs[i].index = out->index;
Michel Dänzer67e385b2014-01-08 17:48:21 +09002405 outputs[i].sid = out->sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002406 outputs[i].usage = out->usage;
2407
2408 for (chan = 0; chan < 4; chan++) {
2409 args[2] = lp_build_const_int32(gallivm,
2410 (i * 4 + chan) *
2411 gs->gs_max_out_vertices * 16 * 4);
2412
2413 outputs[i].values[chan] =
2414 LLVMBuildBitCast(gallivm->builder,
2415 build_intrinsic(gallivm->builder,
2416 "llvm.SI.buffer.load.dword.i32.i32",
2417 LLVMInt32TypeInContext(gallivm->context),
2418 args, 9,
2419 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2420 base->elem_type, "");
2421 }
2422 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002423 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002424
2425 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2426
2427 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2428
2429 if (dump)
2430 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2431
2432 r = si_compile_llvm(sctx, si_shader_ctx->shader,
2433 bld_base->base.gallivm->module);
2434
2435 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2436
2437 FREE(outputs);
2438 return r;
2439}
2440
Tom Stellarda75c6162012-01-06 17:38:37 -05002441int si_pipe_shader_create(
2442 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01002443 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002444{
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002445 struct si_context *sctx = (struct si_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002446 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002447 struct si_shader_context si_shader_ctx;
2448 struct tgsi_shader_info shader_info;
2449 struct lp_build_tgsi_context * bld_base;
2450 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002451 int r = 0;
Michel Dänzere1df0d42014-01-15 12:31:07 +09002452 bool dump = r600_can_dump_shader(&sctx->screen->b, sel->tokens);
2453
2454 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2455 * conversion fails. */
2456 if (dump) {
2457 tgsi_dump(sel->tokens, 0);
2458 si_dump_streamout(&sel->so);
2459 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002460
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002461 assert(shader->shader.noutput == 0);
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002462 assert(shader->shader.nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002463 assert(shader->shader.ninput == 0);
2464
Michel Dänzercfebaf92012-08-31 19:04:08 +02002465 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002466 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2467 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2468
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002469 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002470
Michel Dänzere44dfd42012-11-07 17:33:08 +01002471 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01002472 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002473 bld_base->info = &shader_info;
2474 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002475
2476 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01002477 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01002478#if HAVE_LLVM >= 0x0304
2479 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
2480#endif
Michel Dänzer36231112013-05-02 09:44:45 +02002481 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002482 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002483 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002484 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002485
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002486#if HAVE_LLVM >= 0x0304
2487 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2488 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
2489#endif
2490
Michel Dänzer404b29d2013-11-21 16:45:28 +09002491 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2492 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2493
Christian Könige4ed5872013-03-21 18:02:52 +01002494 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002495 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002496 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2497 si_shader_ctx.shader = shader;
2498 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002499
Michel Dänzer51f89a02013-12-09 15:33:53 +09002500 switch (si_shader_ctx.type) {
2501 case TGSI_PROCESSOR_VERTEX:
2502 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002503 if (shader->key.vs.as_es) {
2504 si_shader_ctx.gs_for_vs = &sctx->gs_shader->current->shader;
2505 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2506 } else {
2507 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2508 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002509 break;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002510 case TGSI_PROCESSOR_GEOMETRY: {
2511 int i;
2512
2513 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2514 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2515 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
2516
2517 for (i = 0; i < shader_info.num_properties; i++) {
2518 switch (shader_info.properties[i].name) {
2519 case TGSI_PROPERTY_GS_INPUT_PRIM:
2520 shader->shader.gs_input_prim = shader_info.properties[i].data[0];
2521 break;
2522 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
2523 shader->shader.gs_output_prim = shader_info.properties[i].data[0];
2524 break;
2525 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
2526 shader->shader.gs_max_out_vertices = shader_info.properties[i].data[0];
2527 break;
2528 }
2529 }
2530 break;
2531 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002532 case TGSI_PROCESSOR_FRAGMENT:
2533 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2534 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
2535 break;
2536 default:
2537 assert(!"Unsupported shader type");
2538 return -1;
2539 }
2540
Christian König206f0592013-03-20 14:37:21 +01002541 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002542 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002543 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002544 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002545 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002546
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002547 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2548 si_shader_ctx.gs_next_vertex =
2549 lp_build_alloca(bld_base->base.gallivm,
2550 bld_base->uint_bld.elem_type, "");
2551 }
2552
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002553 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002554 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002555 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002556 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002557
2558 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2559
2560 mod = bld_base->base.gallivm->module;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002561 r = si_compile_llvm(sctx, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002562 if (r) {
2563 fprintf(stderr, "LLVM failed to compile shader\n");
2564 goto out;
2565 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002566
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002567 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002568
2569 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2570 shader->gs_copy_shader = CALLOC_STRUCT(si_pipe_shader);
2571 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002572 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002573 si_shader_ctx.shader = shader->gs_copy_shader;
2574 if ((r = si_generate_gs_copy_shader(sctx, &si_shader_ctx, dump))) {
2575 free(shader->gs_copy_shader);
2576 shader->gs_copy_shader = NULL;
2577 goto out;
2578 }
2579 }
2580
Tom Stellarda75c6162012-01-06 17:38:37 -05002581 tgsi_parse_free(&si_shader_ctx.parse);
2582
Michel Dänzer404b29d2013-11-21 16:45:28 +09002583out:
Marek Olšák2fd42002013-10-25 11:45:47 +02002584 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2585 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002586 FREE(si_shader_ctx.resources);
2587 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002588
Tom Stellard302f53d2012-10-25 13:50:10 -04002589 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002590}
2591
2592void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
2593{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002594 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002595}