blob: 0ef58a7310e7fd5be8898d22ce7734df84689fd7 [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 Stellard2397a722014-12-10 09:13:59 -050035#include "radeon/r600_cs.h"
Emil Velikova1312632014-08-16 17:58:25 +010036#include "radeon/radeon_llvm.h"
Tom Stellard1f4e48d2014-09-25 18:11:24 -070037#include "radeon/radeon_elf_util.h"
Emil Velikova1312632014-08-16 17:58:25 +010038#include "radeon/radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010039#include "util/u_memory.h"
Marek Olšák6c5af1d2015-01-31 21:43:37 +010040#include "util/u_pstipple.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050041#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010042#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050043#include "tgsi/tgsi_dump.h"
44
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010045#include "si_pipe.h"
46#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050047#include "sid.h"
48
Tom Stellarda75c6162012-01-06 17:38:37 -050049#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050050
Tom Stellard2397a722014-12-10 09:13:59 -050051static const char *scratch_rsrc_dword0_symbol =
52 "SCRATCH_RSRC_DWORD0";
53
54static const char *scratch_rsrc_dword1_symbol =
55 "SCRATCH_RSRC_DWORD1";
56
Michel Dänzer404b29d2013-11-21 16:45:28 +090057struct si_shader_output_values
58{
59 LLVMValueRef values[4];
60 unsigned name;
Michel Dänzer67e385b2014-01-08 17:48:21 +090061 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090062};
63
Tom Stellarda75c6162012-01-06 17:38:37 -050064struct si_shader_context
65{
66 struct radeon_llvm_context radeon_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +020067 struct si_shader *shader;
Marek Olšák368b0a72014-12-28 21:51:35 +010068 struct si_screen *screen;
Tom Stellarda75c6162012-01-06 17:38:37 -050069 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020070 int param_streamout_config;
71 int param_streamout_write_index;
72 int param_streamout_offset[4];
73 int param_vertex_id;
74 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010075 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020076 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020077 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020078 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Marek Olšák55a9b772014-10-05 12:38:54 +020079 LLVMValueRef resources[SI_NUM_SAMPLER_VIEWS];
80 LLVMValueRef samplers[SI_NUM_SAMPLER_STATES];
Marek Olšák8d03d922013-09-01 23:59:06 +020081 LLVMValueRef so_buffers[4];
Marek Olšákfc3b3352014-10-05 13:33:40 +020082 LLVMValueRef esgs_ring;
83 LLVMValueRef gsvs_ring;
Michel Dänzerf07a96d2014-01-08 18:45:10 +090084 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050085};
86
87static struct si_shader_context * si_shader_context(
88 struct lp_build_tgsi_context * bld_base)
89{
90 return (struct si_shader_context *)bld_base;
91}
92
93
94#define PERSPECTIVE_BASE 0
95#define LINEAR_BASE 9
96
97#define SAMPLE_OFFSET 0
98#define CENTER_OFFSET 2
99#define CENTROID_OFSET 4
100
101#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -0400102#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +0200103#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -0400104#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -0500105
Michel Dänzer404b29d2013-11-21 16:45:28 +0900106
107#define SENDMSG_GS 2
108#define SENDMSG_GS_DONE 3
109
110#define SENDMSG_GS_OP_NOP (0 << 4)
111#define SENDMSG_GS_OP_CUT (1 << 4)
112#define SENDMSG_GS_OP_EMIT (2 << 4)
113#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
114
Marek Olšáke29353f2014-09-17 22:17:02 +0200115/**
116 * Returns a unique index for a semantic name and index. The index must be
117 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
118 * calculated.
119 */
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200120unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
Marek Olšáke29353f2014-09-17 22:17:02 +0200121{
122 switch (semantic_name) {
123 case TGSI_SEMANTIC_POSITION:
124 return 0;
125 case TGSI_SEMANTIC_PSIZE:
126 return 1;
127 case TGSI_SEMANTIC_CLIPDIST:
128 assert(index <= 1);
129 return 2 + index;
130 case TGSI_SEMANTIC_CLIPVERTEX:
131 return 4;
132 case TGSI_SEMANTIC_COLOR:
133 assert(index <= 1);
134 return 5 + index;
135 case TGSI_SEMANTIC_BCOLOR:
136 assert(index <= 1);
137 return 7 + index;
138 case TGSI_SEMANTIC_FOG:
139 return 9;
140 case TGSI_SEMANTIC_EDGEFLAG:
141 return 10;
142 case TGSI_SEMANTIC_GENERIC:
143 assert(index <= 63-11);
144 return 11 + index;
145 default:
146 assert(0);
147 return 63;
148 }
149}
150
151/**
152 * Given a semantic name and index of a parameter and a mask of used parameters
153 * (inputs or outputs), return the index of the parameter in the list of all
154 * used parameters.
155 *
156 * For example, assume this list of parameters:
157 * POSITION, PSIZE, GENERIC0, GENERIC2
158 * which has the mask:
159 * 11000000000101
160 * Then:
161 * querying POSITION returns 0,
162 * querying PSIZE returns 1,
163 * querying GENERIC0 returns 2,
164 * querying GENERIC2 returns 3.
165 *
166 * Which can be used as an offset to a parameter buffer in units of vec4s.
167 */
168static int get_param_index(unsigned semantic_name, unsigned index,
169 uint64_t mask)
170{
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200171 unsigned unique_index = si_shader_io_get_unique_index(semantic_name, index);
Marek Olšáke29353f2014-09-17 22:17:02 +0200172 int i, param_index = 0;
173
174 /* If not present... */
175 if (!((1llu << unique_index) & mask))
176 return -1;
177
178 for (i = 0; mask; i++) {
179 uint64_t bit = 1llu << i;
180
181 if (bit & mask) {
182 if (i == unique_index)
183 return param_index;
184
185 mask &= ~bit;
186 param_index++;
187 }
188 }
189
190 assert(!"unreachable");
191 return -1;
192}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900193
Tom Stellard467f5162012-05-16 15:15:35 -0400194/**
Marek Olšákd7876082014-09-26 23:06:32 +0200195 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
196 * It's equivalent to doing a load from &base_ptr[index].
Tom Stellard467f5162012-05-16 15:15:35 -0400197 *
Marek Olšákd7876082014-09-26 23:06:32 +0200198 * \param base_ptr Where the array starts.
199 * \param index The element index into the array.
Tom Stellard467f5162012-05-16 15:15:35 -0400200 */
Marek Olšákd7876082014-09-26 23:06:32 +0200201static LLVMValueRef build_indexed_load(struct si_shader_context *si_shader_ctx,
202 LLVMValueRef base_ptr, LLVMValueRef index)
Tom Stellard467f5162012-05-16 15:15:35 -0400203{
Marek Olšákd7876082014-09-26 23:06:32 +0200204 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
205 struct gallivm_state *gallivm = bld_base->base.gallivm;
206 LLVMValueRef indices[2], pointer;
Tom Stellard467f5162012-05-16 15:15:35 -0400207
Marek Olšákd7876082014-09-26 23:06:32 +0200208 indices[0] = bld_base->uint_bld.zero;
209 indices[1] = index;
Christian König206f0592013-03-20 14:37:21 +0100210
Marek Olšákd7876082014-09-26 23:06:32 +0200211 pointer = LLVMBuildGEP(gallivm->builder, base_ptr, indices, 2, "");
212 return LLVMBuildLoad(gallivm->builder, pointer, "");
213}
214
215/**
216 * Do a load from &base_ptr[index], but also add a flag that it's loading
217 * a constant.
218 */
219static LLVMValueRef build_indexed_load_const(
220 struct si_shader_context * si_shader_ctx,
221 LLVMValueRef base_ptr, LLVMValueRef index)
222{
223 LLVMValueRef result = build_indexed_load(si_shader_ctx, base_ptr, index);
Christian König206f0592013-03-20 14:37:21 +0100224 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
225 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400226}
227
Marek Olšákf317ce52013-09-05 15:39:57 +0200228static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100229 struct radeon_llvm_context * radeon_bld,
230 unsigned divisor)
231{
Marek Olšák8d03d922013-09-01 23:59:06 +0200232 struct si_shader_context *si_shader_ctx =
233 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100234 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
235
Marek Olšák8d03d922013-09-01 23:59:06 +0200236 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
237 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100238
Marek Olšák50908a82015-01-31 19:00:44 +0100239 /* The division must be done before START_INSTANCE is added. */
Christian Königa0dca442013-03-22 15:59:22 +0100240 if (divisor > 1)
241 result = LLVMBuildUDiv(gallivm->builder, result,
242 lp_build_const_int32(gallivm, divisor), "");
243
Marek Olšák50908a82015-01-31 19:00:44 +0100244 return LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
245 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
Christian Königa0dca442013-03-22 15:59:22 +0100246}
247
Tom Stellarda75c6162012-01-06 17:38:37 -0500248static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900249 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500250 unsigned input_index,
251 const struct tgsi_full_declaration *decl)
252{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900253 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
254 struct gallivm_state *gallivm = base->gallivm;
255 struct si_shader_context *si_shader_ctx =
256 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100257 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
258
259 unsigned chan;
260
Tom Stellarda75c6162012-01-06 17:38:37 -0500261 LLVMValueRef t_list_ptr;
262 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400263 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500264 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100265 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400266 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500267 LLVMTypeRef vec4_type;
268 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500269
Tom Stellard467f5162012-05-16 15:15:35 -0400270 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100271 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500272
Michel Dänzer51f89a02013-12-09 15:33:53 +0900273 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400274
Marek Olšákd7876082014-09-26 23:06:32 +0200275 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400276
277 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900278 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500279
Christian Königa0dca442013-03-22 15:59:22 +0100280 if (divisor) {
281 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200282 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200283 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100284 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200285 /* Load the buffer index for vertices. */
286 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
287 si_shader_ctx->param_vertex_id);
288 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
289 SI_PARAM_BASE_VERTEX);
290 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100291 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500292
293 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400294 args[0] = t_list;
295 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100296 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900297 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100298 "llvm.SI.vs.load.input", vec4_type, args, 3,
299 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500300
301 /* Break up the vec4 into individual components */
302 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900303 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500304 /* XXX: Use a helper function for this. There is one in
305 * tgsi_llvm.c. */
306 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900307 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500308 input, llvm_chan, "");
309 }
310}
311
Michel Dänzer404b29d2013-11-21 16:45:28 +0900312static LLVMValueRef fetch_input_gs(
313 struct lp_build_tgsi_context *bld_base,
314 const struct tgsi_full_src_register *reg,
315 enum tgsi_opcode_type type,
316 unsigned swizzle)
317{
318 struct lp_build_context *base = &bld_base->base;
319 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200320 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900321 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
322 struct gallivm_state *gallivm = base->gallivm;
323 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
324 LLVMValueRef vtx_offset;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900325 LLVMValueRef args[9];
326 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200327 struct tgsi_shader_info *info = &shader->selector->info;
328 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
329 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900330
Marek Olšáke23fec12014-10-04 17:40:39 +0200331 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900332 if (swizzle == 0)
333 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
334 SI_PARAM_PRIMITIVE_ID);
335 else
336 return uint->zero;
337 }
338
Michel Dänzer404b29d2013-11-21 16:45:28 +0900339 if (!reg->Register.Dimension)
340 return NULL;
341
342 if (swizzle == ~0) {
343 LLVMValueRef values[TGSI_NUM_CHANNELS];
344 unsigned chan;
345 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
346 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
347 }
348 return lp_build_gather_values(bld_base->base.gallivm, values,
349 TGSI_NUM_CHANNELS);
350 }
351
352 /* Get the vertex offset parameter */
353 vtx_offset_param = reg->Dimension.Index;
354 if (vtx_offset_param < 2) {
355 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
356 } else {
357 assert(vtx_offset_param < 6);
358 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
359 }
360 vtx_offset = lp_build_mul_imm(uint,
361 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
362 vtx_offset_param),
363 4);
364
Marek Olšákfc3b3352014-10-05 13:33:40 +0200365 args[0] = si_shader_ctx->esgs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900366 args[1] = vtx_offset;
367 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200368 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200369 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900370 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900371 args[3] = uint->zero;
372 args[4] = uint->one; /* OFFEN */
373 args[5] = uint->zero; /* IDXEN */
374 args[6] = uint->one; /* GLC */
375 args[7] = uint->zero; /* SLC */
376 args[8] = uint->zero; /* TFE */
377
378 return LLVMBuildBitCast(gallivm->builder,
379 build_intrinsic(gallivm->builder,
380 "llvm.SI.buffer.load.dword.i32.i32",
381 i32, args, 9,
382 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
383 tgsi2llvmtype(bld_base, type), "");
384}
385
Tom Stellarda75c6162012-01-06 17:38:37 -0500386static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900387 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500388 unsigned input_index,
389 const struct tgsi_full_declaration *decl)
390{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900391 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
392 struct si_shader_context *si_shader_ctx =
393 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200394 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900395 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
396 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400397 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900398 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100399
400 LLVMValueRef interp_param;
401 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500402
403 /* This value is:
404 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
405 * quad begins a new primitive. Bit 0 always needs
406 * to be unset)
407 * [32:16] ParamOffset
408 *
409 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900410 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200411 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500412
Christian König0666ffd2013-03-05 15:07:39 +0100413 unsigned chan;
414
Tom Stellard0fb1e682012-09-06 16:18:11 -0400415 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
416 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400417 unsigned soa_index =
418 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900419 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100420 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100421
422 if (chan == 3)
423 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900424 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100425 LLVMBuildFDiv(gallivm->builder,
426 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900427 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100428 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400429 }
430 return;
431 }
432
Michel Dänzer97078b12012-09-25 12:41:31 +0200433 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900434 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Marek Olšáke827bb62014-10-16 16:20:26 +0200435 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900436 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
437 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200438 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900439 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200440 lp_build_const_float(gallivm, 1.0f);
441
442 return;
443 }
444
Marek Olšák8b057dd2014-10-04 22:15:07 +0200445 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200446 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200447 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200448
Francisco Jerez12799232012-04-30 18:27:52 +0200449 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500450 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100451 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500452 break;
453 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200454 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200455 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400456 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100457 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200458 else
Christian König0666ffd2013-03-05 15:07:39 +0100459 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200460 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200461 case TGSI_INTERPOLATE_COLOR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200462 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200463 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200464 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400465 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100466 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200467 else
Christian König0666ffd2013-03-05 15:07:39 +0100468 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500469 break;
470 default:
471 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
472 return;
473 }
474
Marek Olšák13de9472015-01-04 20:09:51 +0100475 /* fs.constant returns the param from the middle vertex, so it's not
476 * really useful for flat shading. It's meant to be used for custom
477 * interpolation (but the intrinsic can't fetch from the other two
478 * vertices).
479 *
480 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
481 * to do the right thing. The only reason we use fs.constant is that
482 * fs.interp cannot be used on integers, because they can be equal
483 * to NaN.
484 */
Christian König0666ffd2013-03-05 15:07:39 +0100485 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
486
Michel Dänzer691f08d2012-09-06 18:03:38 +0200487 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100488 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100489 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200490 LLVMValueRef face, is_face_positive;
491 LLVMValueRef back_attr_number =
492 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200493 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200494
Christian König0666ffd2013-03-05 15:07:39 +0100495 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
496
Michel Dänzer691f08d2012-09-06 18:03:38 +0200497 is_face_positive = LLVMBuildFCmp(gallivm->builder,
Marek Olšákd1d2af22015-01-04 20:45:35 +0100498 LLVMRealOGT, face,
Michel Dänzer691f08d2012-09-06 18:03:38 +0200499 lp_build_const_float(gallivm, 0.0f),
500 "");
501
Tom Stellarda75c6162012-01-06 17:38:37 -0500502 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100503 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200504 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
505 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
506 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
507 LLVMValueRef front, back;
508
509 args[0] = llvm_chan;
510 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900511 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100512 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100513 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200514
515 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900516 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100517 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100518 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200519
Michel Dänzer51f89a02013-12-09 15:33:53 +0900520 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200521 LLVMBuildSelect(gallivm->builder,
522 is_face_positive,
523 front,
524 back,
525 "");
526 }
527
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900528 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200529 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
530 LLVMValueRef args[4];
531
532 args[0] = uint->zero;
533 args[1] = attr_number;
534 args[2] = params;
535 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900536 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
537 build_intrinsic(gallivm->builder, intr_name,
538 input_type, args, args[3] ? 4 : 3,
539 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
540 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
541 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200542 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900543 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200544 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200545 } else {
546 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100547 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200548 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
549 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
550 args[0] = llvm_chan;
551 args[1] = attr_number;
552 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100553 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900554 radeon_bld->inputs[soa_index] =
555 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100556 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100557 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200558 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500559 }
560}
561
Marek Olšák5b06fc32014-05-06 18:12:40 +0200562static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
563{
564 struct gallivm_state *gallivm = &radeon_bld->gallivm;
565 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
566 SI_PARAM_ANCILLARY);
567 value = LLVMBuildLShr(gallivm->builder, value,
568 lp_build_const_int32(gallivm, 8), "");
569 value = LLVMBuildAnd(gallivm->builder, value,
570 lp_build_const_int32(gallivm, 0xf), "");
571 return value;
572}
573
Marek Olšákd7876082014-09-26 23:06:32 +0200574/**
575 * Load a dword from a constant buffer.
576 */
577static LLVMValueRef buffer_load_const(LLVMBuilderRef builder, LLVMValueRef resource,
578 LLVMValueRef offset, LLVMTypeRef return_type)
Marek Olšák250aa932014-05-06 14:10:47 +0200579{
580 LLVMValueRef args[2] = {resource, offset};
581
582 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
583 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
584}
585
Christian Könige4ed5872013-03-21 18:02:52 +0100586static void declare_system_value(
587 struct radeon_llvm_context * radeon_bld,
588 unsigned index,
589 const struct tgsi_full_declaration *decl)
590{
Marek Olšák8d03d922013-09-01 23:59:06 +0200591 struct si_shader_context *si_shader_ctx =
592 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200593 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
594 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100595 LLVMValueRef value = 0;
596
597 switch (decl->Semantic.Name) {
598 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200599 value = LLVMGetParam(radeon_bld->main_fn,
600 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100601 break;
602
603 case TGSI_SEMANTIC_VERTEXID:
Marek Olšákd7c6f392015-01-04 14:41:49 +0100604 value = LLVMBuildAdd(gallivm->builder,
605 LLVMGetParam(radeon_bld->main_fn,
606 si_shader_ctx->param_vertex_id),
607 LLVMGetParam(radeon_bld->main_fn,
608 SI_PARAM_BASE_VERTEX), "");
Christian Könige4ed5872013-03-21 18:02:52 +0100609 break;
610
Marek Olšáke3d4bdd2015-01-04 14:51:01 +0100611 case TGSI_SEMANTIC_VERTEXID_NOBASE:
612 value = LLVMGetParam(radeon_bld->main_fn,
613 si_shader_ctx->param_vertex_id);
614 break;
615
616 case TGSI_SEMANTIC_BASEVERTEX:
617 value = LLVMGetParam(radeon_bld->main_fn,
618 SI_PARAM_BASE_VERTEX);
619 break;
620
Marek Olšák5b06fc32014-05-06 18:12:40 +0200621 case TGSI_SEMANTIC_SAMPLEID:
622 value = get_sample_id(radeon_bld);
623 break;
624
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200625 case TGSI_SEMANTIC_SAMPLEPOS:
626 {
627 LLVMBuilderRef builder = gallivm->builder;
628 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200629 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200630 LLVMValueRef resource = build_indexed_load_const(si_shader_ctx, desc, buf_index);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200631
632 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
633 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
634 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
635
636 LLVMValueRef pos[4] = {
Marek Olšákd7876082014-09-26 23:06:32 +0200637 buffer_load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
638 buffer_load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200639 lp_build_const_float(gallivm, 0),
640 lp_build_const_float(gallivm, 0)
641 };
642 value = lp_build_gather_values(gallivm, pos, 4);
643 break;
644 }
645
Christian Könige4ed5872013-03-21 18:02:52 +0100646 default:
647 assert(!"unknown system value");
648 return;
649 }
650
651 radeon_bld->system_values[index] = value;
652}
653
Tom Stellarda75c6162012-01-06 17:38:37 -0500654static LLVMValueRef fetch_constant(
655 struct lp_build_tgsi_context * bld_base,
656 const struct tgsi_full_src_register *reg,
657 enum tgsi_opcode_type type,
658 unsigned swizzle)
659{
Christian König55fe5cc2013-03-04 16:30:06 +0100660 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500661 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100662 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200663 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500664
Christian König0f6cf2b2013-03-15 15:53:25 +0100665 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100666 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500667
Christian König8514f5a2013-02-04 17:46:42 +0100668 if (swizzle == LP_CHAN_ALL) {
669 unsigned chan;
670 LLVMValueRef values[4];
671 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
672 values[chan] = fetch_constant(bld_base, reg, type, chan);
673
674 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
675 }
676
Marek Olšák2fd42002013-10-25 11:45:47 +0200677 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100678 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100679
Marek Olšák2fd42002013-10-25 11:45:47 +0200680 if (!reg->Register.Indirect)
681 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
682
Christian König0f6cf2b2013-03-15 15:53:25 +0100683 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
684 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
685 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200686 addr = lp_build_add(&bld_base->uint_bld, addr,
687 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200688
Marek Olšákd7876082014-09-26 23:06:32 +0200689 result = buffer_load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
Marek Olšák250aa932014-05-06 14:10:47 +0200690 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500691
Christian Königf5298b02013-02-28 14:50:07 +0100692 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500693}
694
Michel Dänzer26c71392012-08-24 12:03:11 +0200695/* Initialize arguments for the shader export intrinsic */
696static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900697 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200698 unsigned target,
699 LLVMValueRef *args)
700{
701 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
702 struct lp_build_context *uint =
703 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
704 struct lp_build_context *base = &bld_base->base;
705 unsigned compressed = 0;
706 unsigned chan;
707
Michel Dänzerf402acd2012-08-22 18:15:36 +0200708 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
709 int cbuf = target - V_008DFC_SQ_EXP_MRT;
710
711 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100712 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100713
714 if (compressed)
715 si_shader_ctx->shader->spi_shader_col_format |=
716 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
717 else
718 si_shader_ctx->shader->spi_shader_col_format |=
719 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200720
721 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200722 }
723 }
724
725 if (compressed) {
726 /* Pixel shader needs to pack output values before export */
727 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900728 args[0] = values[2 * chan];
729 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200730 args[chan + 5] =
731 build_intrinsic(base->gallivm->builder,
732 "llvm.SI.packf16",
733 LLVMInt32TypeInContext(base->gallivm->context),
734 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100735 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100736 args[chan + 7] = args[chan + 5] =
737 LLVMBuildBitCast(base->gallivm->builder,
738 args[chan + 5],
739 LLVMFloatTypeInContext(base->gallivm->context),
740 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200741 }
742
743 /* Set COMPR flag */
744 args[4] = uint->one;
745 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900746 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200747 /* +5 because the first output value will be
748 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900749 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200750
751 /* Clear COMPR flag */
752 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200753 }
754
755 /* XXX: This controls which components of the output
756 * registers actually get exported. (e.g bit 0 means export
757 * X component, bit 1 means export Y component, etc.) I'm
758 * hard coding this to 0xf for now. In the future, we might
759 * want to do something else. */
760 args[0] = lp_build_const_int32(base->gallivm, 0xf);
761
762 /* Specify whether the EXEC mask represents the valid mask */
763 args[1] = uint->zero;
764
765 /* Specify whether this is the last export */
766 args[2] = uint->zero;
767
768 /* Specify the target we are exporting */
769 args[3] = lp_build_const_int32(base->gallivm, target);
770
Michel Dänzer26c71392012-08-24 12:03:11 +0200771 /* XXX: We probably need to keep track of the output
772 * values, so we know what we are passing to the next
773 * stage. */
774}
775
Michel Dänzer404b29d2013-11-21 16:45:28 +0900776/* Load from output pointers and initialize arguments for the shader export intrinsic */
777static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
778 LLVMValueRef *out_ptr,
779 unsigned target,
780 LLVMValueRef *args)
781{
782 struct gallivm_state *gallivm = bld_base->base.gallivm;
783 LLVMValueRef values[4];
784 int i;
785
786 for (i = 0; i < 4; i++)
787 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
788
789 si_llvm_init_export_args(bld_base, values, target, args);
790}
791
Michel Dänzer7708a862012-11-02 15:57:30 +0100792static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900793 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100794{
795 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
796 struct gallivm_state *gallivm = bld_base->base.gallivm;
797
Christian Königa0dca442013-03-22 15:59:22 +0100798 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400799 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
800 SI_PARAM_ALPHA_REF);
801
Michel Dänzer7708a862012-11-02 15:57:30 +0100802 LLVMValueRef alpha_pass =
803 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100804 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900805 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400806 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100807 LLVMValueRef arg =
808 lp_build_select(&bld_base->base,
809 alpha_pass,
810 lp_build_const_float(gallivm, 1.0f),
811 lp_build_const_float(gallivm, -1.0f));
812
813 build_intrinsic(gallivm->builder,
814 "llvm.AMDGPU.kill",
815 LLVMVoidTypeInContext(gallivm->context),
816 &arg, 1, 0);
817 } else {
818 build_intrinsic(gallivm->builder,
819 "llvm.AMDGPU.kilp",
820 LLVMVoidTypeInContext(gallivm->context),
821 NULL, 0, 0);
822 }
Marek Olšákadc57972014-09-19 17:07:07 +0200823
824 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100825}
826
Michel Dänzere3befbc2013-05-15 18:09:50 +0200827static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900828 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200829{
830 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
831 struct lp_build_context *base = &bld_base->base;
832 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200833 unsigned reg_index;
834 unsigned chan;
835 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200836 LLVMValueRef base_elt;
837 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200838 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200839 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200840
Michel Dänzere3befbc2013-05-15 18:09:50 +0200841 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200842 LLVMValueRef *args = pos[2 + reg_index];
843
Michel Dänzere3befbc2013-05-15 18:09:50 +0200844 args[5] =
845 args[6] =
846 args[7] =
847 args[8] = lp_build_const_float(base->gallivm, 0.0f);
848
849 /* Compute dot products of position and user clip plane vectors */
850 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
851 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200852 args[1] = lp_build_const_int32(base->gallivm,
853 ((reg_index * 4 + chan) * 4 +
854 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200855 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200856 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200857 args[5 + chan] =
858 lp_build_add(base, args[5 + chan],
859 lp_build_mul(base, base_elt,
860 out_elts[const_chan]));
861 }
862 }
863
864 args[0] = lp_build_const_int32(base->gallivm, 0xf);
865 args[1] = uint->zero;
866 args[2] = uint->zero;
867 args[3] = lp_build_const_int32(base->gallivm,
868 V_008DFC_SQ_EXP_POS + 2 + reg_index);
869 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200870 }
871}
872
Marek Olšák8d03d922013-09-01 23:59:06 +0200873static void si_dump_streamout(struct pipe_stream_output_info *so)
874{
875 unsigned i;
876
877 if (so->num_outputs)
878 fprintf(stderr, "STREAMOUT\n");
879
880 for (i = 0; i < so->num_outputs; i++) {
881 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
882 so->output[i].start_component;
883 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
884 i, so->output[i].output_buffer,
885 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
886 so->output[i].register_index,
887 mask & 1 ? "x" : "",
888 mask & 2 ? "y" : "",
889 mask & 4 ? "z" : "",
890 mask & 8 ? "w" : "");
891 }
892}
893
894/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
895 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
896 * or v4i32 (num_channels=3,4). */
897static void build_tbuffer_store(struct si_shader_context *shader,
898 LLVMValueRef rsrc,
899 LLVMValueRef vdata,
900 unsigned num_channels,
901 LLVMValueRef vaddr,
902 LLVMValueRef soffset,
903 unsigned inst_offset,
904 unsigned dfmt,
905 unsigned nfmt,
906 unsigned offen,
907 unsigned idxen,
908 unsigned glc,
909 unsigned slc,
910 unsigned tfe)
911{
912 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
913 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
914 LLVMValueRef args[] = {
915 rsrc,
916 vdata,
917 LLVMConstInt(i32, num_channels, 0),
918 vaddr,
919 soffset,
920 LLVMConstInt(i32, inst_offset, 0),
921 LLVMConstInt(i32, dfmt, 0),
922 LLVMConstInt(i32, nfmt, 0),
923 LLVMConstInt(i32, offen, 0),
924 LLVMConstInt(i32, idxen, 0),
925 LLVMConstInt(i32, glc, 0),
926 LLVMConstInt(i32, slc, 0),
927 LLVMConstInt(i32, tfe, 0)
928 };
929
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900930 /* The instruction offset field has 12 bits */
931 assert(offen || inst_offset < (1 << 12));
932
Marek Olšák8d03d922013-09-01 23:59:06 +0200933 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
934 unsigned func = CLAMP(num_channels, 1, 3) - 1;
935 const char *types[] = {"i32", "v2i32", "v4i32"};
936 char name[256];
937 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
938
939 lp_build_intrinsic(gallivm->builder, name,
940 LLVMVoidTypeInContext(gallivm->context),
941 args, Elements(args));
942}
943
944static void build_streamout_store(struct si_shader_context *shader,
945 LLVMValueRef rsrc,
946 LLVMValueRef vdata,
947 unsigned num_channels,
948 LLVMValueRef vaddr,
949 LLVMValueRef soffset,
950 unsigned inst_offset)
951{
952 static unsigned dfmt[] = {
953 V_008F0C_BUF_DATA_FORMAT_32,
954 V_008F0C_BUF_DATA_FORMAT_32_32,
955 V_008F0C_BUF_DATA_FORMAT_32_32_32,
956 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
957 };
958 assert(num_channels >= 1 && num_channels <= 4);
959
960 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
961 inst_offset, dfmt[num_channels-1],
962 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
963}
964
965/* On SI, the vertex shader is responsible for writing streamout data
966 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900967static void si_llvm_emit_streamout(struct si_shader_context *shader,
968 struct si_shader_output_values *outputs,
969 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200970{
971 struct pipe_stream_output_info *so = &shader->shader->selector->so;
972 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
973 LLVMBuilderRef builder = gallivm->builder;
974 int i, j;
975 struct lp_build_if_state if_ctx;
976
977 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
978
979 LLVMValueRef so_param =
980 LLVMGetParam(shader->radeon_bld.main_fn,
981 shader->param_streamout_config);
982
983 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
984 LLVMValueRef so_vtx_count =
985 LLVMBuildAnd(builder,
986 LLVMBuildLShr(builder, so_param,
987 LLVMConstInt(i32, 16, 0), ""),
988 LLVMConstInt(i32, 127, 0), "");
989
990 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
991 NULL, 0, LLVMReadNoneAttribute);
992
993 /* can_emit = tid < so_vtx_count; */
994 LLVMValueRef can_emit =
995 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
996
997 /* Emit the streamout code conditionally. This actually avoids
998 * out-of-bounds buffer access. The hw tells us via the SGPR
999 * (so_vtx_count) which threads are allowed to emit streamout data. */
1000 lp_build_if(&if_ctx, gallivm, can_emit);
1001 {
1002 /* The buffer offset is computed as follows:
1003 * ByteOffset = streamout_offset[buffer_id]*4 +
1004 * (streamout_write_index + thread_id)*stride[buffer_id] +
1005 * attrib_offset
1006 */
1007
1008 LLVMValueRef so_write_index =
1009 LLVMGetParam(shader->radeon_bld.main_fn,
1010 shader->param_streamout_write_index);
1011
1012 /* Compute (streamout_write_index + thread_id). */
1013 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1014
1015 /* Compute the write offset for each enabled buffer. */
1016 LLVMValueRef so_write_offset[4] = {};
1017 for (i = 0; i < 4; i++) {
1018 if (!so->stride[i])
1019 continue;
1020
1021 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1022 shader->param_streamout_offset[i]);
1023 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1024
1025 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1026 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1027 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1028 }
1029
Marek Olšák8d03d922013-09-01 23:59:06 +02001030 /* Write streamout data. */
1031 for (i = 0; i < so->num_outputs; i++) {
1032 unsigned buf_idx = so->output[i].output_buffer;
1033 unsigned reg = so->output[i].register_index;
1034 unsigned start = so->output[i].start_component;
1035 unsigned num_comps = so->output[i].num_components;
1036 LLVMValueRef out[4];
1037
1038 assert(num_comps && num_comps <= 4);
1039 if (!num_comps || num_comps > 4)
1040 continue;
1041
Marek Olšák558f7772014-10-04 22:37:23 +02001042 if (reg >= noutput)
1043 continue;
1044
Marek Olšák8d03d922013-09-01 23:59:06 +02001045 /* Load the output as int. */
1046 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001047 out[j] = LLVMBuildBitCast(builder,
1048 outputs[reg].values[start+j],
1049 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001050 }
1051
1052 /* Pack the output. */
1053 LLVMValueRef vdata = NULL;
1054
1055 switch (num_comps) {
1056 case 1: /* as i32 */
1057 vdata = out[0];
1058 break;
1059 case 2: /* as v2i32 */
1060 case 3: /* as v4i32 (aligned to 4) */
1061 case 4: /* as v4i32 */
1062 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1063 for (j = 0; j < num_comps; j++) {
1064 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1065 LLVMConstInt(i32, j, 0), "");
1066 }
1067 break;
1068 }
1069
1070 build_streamout_store(shader, shader->so_buffers[buf_idx],
1071 vdata, num_comps,
1072 so_write_offset[buf_idx],
1073 LLVMConstInt(i32, 0, 0),
1074 so->output[i].dst_offset*4);
1075 }
1076 }
1077 lp_build_endif(&if_ctx);
1078}
1079
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001080
Michel Dänzer404b29d2013-11-21 16:45:28 +09001081/* Generate export instructions for hardware VS shader stage */
1082static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1083 struct si_shader_output_values *outputs,
1084 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001085{
1086 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001087 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001088 struct lp_build_context * base = &bld_base->base;
1089 struct lp_build_context * uint =
1090 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001091 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001092 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001093 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001094 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001095 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001096 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001097 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001098 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001099
Michel Dänzer67e385b2014-01-08 17:48:21 +09001100 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1101 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001102 }
1103
Michel Dänzer404b29d2013-11-21 16:45:28 +09001104 for (i = 0; i < noutput; i++) {
1105 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001106 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001107
Michel Dänzer0afeea52013-05-02 14:53:17 +02001108handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001109 /* Select the correct target */
1110 switch(semantic_name) {
1111 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001112 psize_value = outputs[i].values[0];
1113 continue;
1114 case TGSI_SEMANTIC_EDGEFLAG:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001115 edgeflag_value = outputs[i].values[0];
1116 continue;
1117 case TGSI_SEMANTIC_LAYER:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001118 layer_value = outputs[i].values[0];
1119 continue;
1120 case TGSI_SEMANTIC_POSITION:
1121 target = V_008DFC_SQ_EXP_POS;
1122 break;
1123 case TGSI_SEMANTIC_COLOR:
1124 case TGSI_SEMANTIC_BCOLOR:
1125 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001126 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001127 param_count++;
1128 break;
1129 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001130 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1131 break;
1132 case TGSI_SEMANTIC_CLIPVERTEX:
1133 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1134 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001135 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001136 case TGSI_SEMANTIC_FOG:
1137 case TGSI_SEMANTIC_GENERIC:
1138 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001139 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001140 param_count++;
1141 break;
1142 default:
1143 target = 0;
1144 fprintf(stderr,
1145 "Warning: SI unhandled vs output type:%d\n",
1146 semantic_name);
1147 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001148
Michel Dänzer404b29d2013-11-21 16:45:28 +09001149 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001150
Michel Dänzer404b29d2013-11-21 16:45:28 +09001151 if (target >= V_008DFC_SQ_EXP_POS &&
1152 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1153 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1154 args, sizeof(args));
1155 } else {
1156 lp_build_intrinsic(base->gallivm->builder,
1157 "llvm.SI.export",
1158 LLVMVoidTypeInContext(base->gallivm->context),
1159 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001160 }
1161
1162 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1163 semantic_name = TGSI_SEMANTIC_GENERIC;
1164 goto handle_semantic;
1165 }
1166 }
1167
1168 /* We need to add the position output manually if it's missing. */
1169 if (!pos_args[0][0]) {
1170 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1171 pos_args[0][1] = uint->zero; /* EXEC mask */
1172 pos_args[0][2] = uint->zero; /* last export? */
1173 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1174 pos_args[0][4] = uint->zero; /* COMPR flag */
1175 pos_args[0][5] = base->zero; /* X */
1176 pos_args[0][6] = base->zero; /* Y */
1177 pos_args[0][7] = base->zero; /* Z */
1178 pos_args[0][8] = base->one; /* W */
1179 }
1180
1181 /* Write the misc vector (point size, edgeflag, layer, viewport). */
Marek Olšák16153472014-12-07 19:30:08 +01001182 if (shader->selector->info.writes_psize ||
1183 shader->selector->info.writes_edgeflag ||
1184 shader->selector->info.writes_layer) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001185 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
Marek Olšák16153472014-12-07 19:30:08 +01001186 shader->selector->info.writes_psize |
1187 (shader->selector->info.writes_edgeflag << 1) |
1188 (shader->selector->info.writes_layer << 2));
Michel Dänzer51f89a02013-12-09 15:33:53 +09001189 pos_args[1][1] = uint->zero; /* EXEC mask */
1190 pos_args[1][2] = uint->zero; /* last export? */
1191 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1192 pos_args[1][4] = uint->zero; /* COMPR flag */
1193 pos_args[1][5] = base->zero; /* X */
1194 pos_args[1][6] = base->zero; /* Y */
1195 pos_args[1][7] = base->zero; /* Z */
1196 pos_args[1][8] = base->zero; /* W */
1197
Marek Olšák16153472014-12-07 19:30:08 +01001198 if (shader->selector->info.writes_psize)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001199 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001200
Marek Olšák16153472014-12-07 19:30:08 +01001201 if (shader->selector->info.writes_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001202 /* The output is a float, but the hw expects an integer
1203 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001204 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1205 edgeflag_value,
1206 bld_base->uint_bld.elem_type, "");
1207 edgeflag_value = lp_build_min(&bld_base->int_bld,
1208 edgeflag_value,
1209 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001210
1211 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001212 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1213 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001214 base->elem_type, "");
1215 }
1216
Marek Olšák16153472014-12-07 19:30:08 +01001217 if (shader->selector->info.writes_layer)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001218 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001219 }
1220
1221 for (i = 0; i < 4; i++)
1222 if (pos_args[i][0])
1223 shader->nr_pos_exports++;
1224
1225 pos_idx = 0;
1226 for (i = 0; i < 4; i++) {
1227 if (!pos_args[i][0])
1228 continue;
1229
1230 /* Specify the target we are exporting */
1231 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1232
1233 if (pos_idx == shader->nr_pos_exports)
1234 /* Specify that this is the last export */
1235 pos_args[i][2] = uint->one;
1236
1237 lp_build_intrinsic(base->gallivm->builder,
1238 "llvm.SI.export",
1239 LLVMVoidTypeInContext(base->gallivm->context),
1240 pos_args[i], 9);
1241 }
1242}
1243
Michel Dänzer404b29d2013-11-21 16:45:28 +09001244static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1245{
1246 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1247 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001248 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001249 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001250 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001251 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1252 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001253 unsigned chan;
1254 int i;
1255
Marek Olšák216cf862014-10-04 17:04:05 +02001256 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001257 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001258 si_shader_ctx->radeon_bld.soa.outputs[i];
1259 int param_index = get_param_index(info->output_semantic_name[i],
1260 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001261 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001262
Marek Olšáke29353f2014-09-17 22:17:02 +02001263 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001264 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001265
1266 for (chan = 0; chan < 4; chan++) {
1267 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001268 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1269
Marek Olšákfc3b3352014-10-05 13:33:40 +02001270 build_tbuffer_store(si_shader_ctx,
1271 si_shader_ctx->esgs_ring,
1272 out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001273 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001274 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001275 V_008F0C_BUF_DATA_FORMAT_32,
1276 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001277 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001278 }
1279 }
1280}
1281
1282static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1283{
1284 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1285 struct gallivm_state *gallivm = bld_base->base.gallivm;
1286 LLVMValueRef args[2];
1287
1288 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1289 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1290 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1291 LLVMVoidTypeInContext(gallivm->context), args, 2,
1292 LLVMNoUnwindAttribute);
1293}
1294
1295static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1296{
1297 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1298 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001299 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001300 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001301 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001302
Marek Olšákec0d1682014-10-04 22:33:36 +02001303 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001304
Marek Olšákec0d1682014-10-04 22:33:36 +02001305 for (i = 0; i < info->num_outputs; i++) {
1306 outputs[i].name = info->output_semantic_name[i];
1307 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001308
Marek Olšákec0d1682014-10-04 22:33:36 +02001309 for (j = 0; j < 4; j++)
1310 outputs[i].values[j] =
1311 LLVMBuildLoad(gallivm->builder,
1312 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1313 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001314 }
1315
Marek Olšákec0d1682014-10-04 22:33:36 +02001316 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001317 FREE(outputs);
1318}
1319
Michel Dänzer51f89a02013-12-09 15:33:53 +09001320static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1321{
1322 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001323 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001324 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001325 struct lp_build_context * uint = &bld_base->uint_bld;
1326 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001327 LLVMValueRef args[9];
1328 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001329 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001330 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001331
Marek Olšák75e97e22014-10-04 23:13:50 +02001332 for (i = 0; i < info->num_outputs; i++) {
1333 unsigned semantic_name = info->output_semantic_name[i];
1334 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001335 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001336
Marek Olšák75e97e22014-10-04 23:13:50 +02001337 /* Select the correct target */
1338 switch (semantic_name) {
1339 case TGSI_SEMANTIC_POSITION:
1340 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001341 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001342 case TGSI_SEMANTIC_STENCIL:
1343 stencil_index = i;
1344 continue;
1345 case TGSI_SEMANTIC_SAMPLEMASK:
1346 samplemask_index = i;
1347 continue;
1348 case TGSI_SEMANTIC_COLOR:
1349 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1350 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1351 LLVMBuildStore(bld_base->base.gallivm->builder,
1352 bld_base->base.one,
1353 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001354
Marek Olšák75e97e22014-10-04 23:13:50 +02001355 if (semantic_index == 0 &&
1356 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1357 si_alpha_test(bld_base,
1358 si_shader_ctx->radeon_bld.soa.outputs[i]);
1359 break;
1360 default:
1361 target = 0;
1362 fprintf(stderr,
1363 "Warning: SI unhandled fs output type:%d\n",
1364 semantic_name);
1365 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001366
Marek Olšák75e97e22014-10-04 23:13:50 +02001367 si_llvm_init_export_args_load(bld_base,
1368 si_shader_ctx->radeon_bld.soa.outputs[i],
1369 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001370
Marek Olšák75e97e22014-10-04 23:13:50 +02001371 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1372 /* If there is an export instruction waiting to be emitted, do so now. */
1373 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001374 lp_build_intrinsic(base->gallivm->builder,
1375 "llvm.SI.export",
1376 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001377 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001378 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001379
1380 /* This instruction will be emitted at the end of the shader. */
1381 memcpy(last_args, args, sizeof(args));
1382
1383 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1384 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1385 semantic_index == 0 &&
1386 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1387 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1388 si_llvm_init_export_args_load(bld_base,
1389 si_shader_ctx->radeon_bld.soa.outputs[i],
1390 V_008DFC_SQ_EXP_MRT + c, args);
1391 lp_build_intrinsic(base->gallivm->builder,
1392 "llvm.SI.export",
1393 LLVMVoidTypeInContext(base->gallivm->context),
1394 args, 9);
1395 }
1396 }
1397 } else {
1398 lp_build_intrinsic(base->gallivm->builder,
1399 "llvm.SI.export",
1400 LLVMVoidTypeInContext(base->gallivm->context),
1401 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001402 }
1403 }
1404
Marek Olšákd0e8b652014-05-06 20:04:31 +02001405 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001406 LLVMValueRef out_ptr;
1407 unsigned mask = 0;
1408
1409 /* Specify the target we are exporting */
1410 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1411
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001412 args[5] = base->zero; /* R, depth */
1413 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1414 args[7] = base->zero; /* B, sample mask */
1415 args[8] = base->zero; /* A, alpha to mask */
1416
Michel Dänzer1a616c12012-11-13 17:35:09 +01001417 if (depth_index >= 0) {
1418 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1419 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1420 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001421 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001422 }
1423
1424 if (stencil_index >= 0) {
1425 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001426 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001427 mask |= 0x2;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001428 si_shader_ctx->shader->db_shader_control |=
1429 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001430 }
1431
Marek Olšákd0e8b652014-05-06 20:04:31 +02001432 if (samplemask_index >= 0) {
1433 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1434 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001435 mask |= 0x4;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001436 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1437 }
1438
Marek Olšák368b0a72014-12-28 21:51:35 +01001439 /* SI (except OLAND) has a bug that it only looks
1440 * at the X writemask component. */
1441 if (si_shader_ctx->screen->b.chip_class == SI &&
1442 si_shader_ctx->screen->b.family != CHIP_OLAND)
1443 mask |= 0x1;
1444
Marek Olšákd0e8b652014-05-06 20:04:31 +02001445 if (samplemask_index >= 0)
1446 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1447 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001448 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1449 else
1450 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1451
Michel Dänzer1a616c12012-11-13 17:35:09 +01001452 /* Specify which components to enable */
1453 args[0] = lp_build_const_int32(base->gallivm, mask);
1454
1455 args[1] =
1456 args[2] =
1457 args[4] = uint->zero;
1458
1459 if (last_args[0])
1460 lp_build_intrinsic(base->gallivm->builder,
1461 "llvm.SI.export",
1462 LLVMVoidTypeInContext(base->gallivm->context),
1463 args, 9);
1464 else
1465 memcpy(last_args, args, sizeof(args));
1466 }
1467
Michel Dänzer51f89a02013-12-09 15:33:53 +09001468 if (!last_args[0]) {
1469 /* Specify which components to enable */
1470 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001471
Michel Dänzer51f89a02013-12-09 15:33:53 +09001472 /* Specify the target we are exporting */
1473 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001474
Michel Dänzer51f89a02013-12-09 15:33:53 +09001475 /* Set COMPR flag to zero to export data as 32-bit */
1476 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001477
Michel Dänzer51f89a02013-12-09 15:33:53 +09001478 /* dummy bits */
1479 last_args[5]= uint->zero;
1480 last_args[6]= uint->zero;
1481 last_args[7]= uint->zero;
1482 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001483 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001484
1485 /* Specify whether the EXEC mask represents the valid mask */
1486 last_args[1] = uint->one;
1487
1488 /* Specify that this is the last export */
1489 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1490
1491 lp_build_intrinsic(base->gallivm->builder,
1492 "llvm.SI.export",
1493 LLVMVoidTypeInContext(base->gallivm->context),
1494 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001495}
1496
Marek Olšák4855acd2013-08-06 15:08:54 +02001497static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1498 struct lp_build_tgsi_context * bld_base,
1499 struct lp_build_emit_data * emit_data);
1500
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001501static bool tgsi_is_shadow_sampler(unsigned target)
1502{
1503 return target == TGSI_TEXTURE_SHADOW1D ||
1504 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1505 target == TGSI_TEXTURE_SHADOW2D ||
1506 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1507 target == TGSI_TEXTURE_SHADOWCUBE ||
1508 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1509 target == TGSI_TEXTURE_SHADOWRECT;
1510}
1511
Marek Olšák877bb522014-06-25 03:12:46 +02001512static const struct lp_build_tgsi_action tex_action;
1513
Tom Stellarda75c6162012-01-06 17:38:37 -05001514static void tex_fetch_args(
1515 struct lp_build_tgsi_context * bld_base,
1516 struct lp_build_emit_data * emit_data)
1517{
Christian König55fe5cc2013-03-04 16:30:06 +01001518 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001519 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001520 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001521 unsigned opcode = inst->Instruction.Opcode;
1522 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001523 LLVMValueRef coords[4];
1524 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001525 int ref_pos;
1526 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001527 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001528 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001529 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1530 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001531 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001532
1533 if (target == TGSI_TEXTURE_BUFFER) {
1534 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1535 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1536 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1537 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1538
Marek Olšák4f3f0432014-07-07 22:49:15 +02001539 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001540 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1541 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
Marek Olšákb142dd22015-02-01 16:58:08 +01001542 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.one, "");
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001543 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1544
1545 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1546 emit_data->args[0] = res;
1547 emit_data->args[1] = bld_base->uint_bld.zero;
1548 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1549 emit_data->arg_count = 3;
1550 return;
1551 }
Tom Stellard467f5162012-05-16 15:15:35 -04001552
Michel Dänzer120efee2013-01-25 12:10:11 +01001553 /* Fetch and project texture coordinates */
1554 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001555 for (chan = 0; chan < 3; chan++ ) {
1556 coords[chan] = lp_build_emit_fetch(bld_base,
1557 emit_data->inst, 0,
1558 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001559 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001560 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1561 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001562 coords[chan],
1563 coords[3]);
1564 }
1565
Michel Dänzer120efee2013-01-25 12:10:11 +01001566 if (opcode == TGSI_OPCODE_TXP)
1567 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001568
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001569 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001570 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001571 /* The offsets are six-bit signed integers packed like this:
1572 * X=[5:0], Y=[13:8], and Z=[21:16].
1573 */
1574 LLVMValueRef offset[3], pack;
1575
1576 assert(inst->Texture.NumOffsets == 1);
1577
1578 for (chan = 0; chan < 3; chan++) {
1579 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1580 emit_data->inst, 0, chan);
1581 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1582 lp_build_const_int32(gallivm, 0x3f), "");
1583 if (chan)
1584 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1585 lp_build_const_int32(gallivm, chan*8), "");
1586 }
1587
1588 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1589 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1590 address[count++] = pack;
1591 }
1592
Michel Dänzer120efee2013-01-25 12:10:11 +01001593 /* Pack LOD bias value */
1594 if (opcode == TGSI_OPCODE_TXB)
1595 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001596 if (opcode == TGSI_OPCODE_TXB2)
1597 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001598
Michel Dänzer120efee2013-01-25 12:10:11 +01001599 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001600 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001601 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1602 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1603 } else {
1604 assert(ref_pos >= 0);
1605 address[count++] = coords[ref_pos];
1606 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001607 }
1608
Marek Olšákb279f0142014-07-04 03:19:58 +02001609 if (target == TGSI_TEXTURE_CUBE ||
1610 target == TGSI_TEXTURE_CUBE_ARRAY ||
1611 target == TGSI_TEXTURE_SHADOWCUBE ||
1612 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1613 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1614
Michel Dänzera6b83c02013-02-21 16:10:55 +01001615 /* Pack user derivatives */
1616 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001617 int num_deriv_channels, param;
1618
1619 switch (target) {
1620 case TGSI_TEXTURE_3D:
1621 num_deriv_channels = 3;
1622 break;
1623 case TGSI_TEXTURE_2D:
1624 case TGSI_TEXTURE_SHADOW2D:
1625 case TGSI_TEXTURE_RECT:
1626 case TGSI_TEXTURE_SHADOWRECT:
1627 case TGSI_TEXTURE_2D_ARRAY:
1628 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1629 case TGSI_TEXTURE_CUBE:
1630 case TGSI_TEXTURE_SHADOWCUBE:
1631 case TGSI_TEXTURE_CUBE_ARRAY:
1632 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1633 num_deriv_channels = 2;
1634 break;
1635 case TGSI_TEXTURE_1D:
1636 case TGSI_TEXTURE_SHADOW1D:
1637 case TGSI_TEXTURE_1D_ARRAY:
1638 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1639 num_deriv_channels = 1;
1640 break;
1641 default:
1642 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001643 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001644
1645 for (param = 1; param <= 2; param++)
1646 for (chan = 0; chan < num_deriv_channels; chan++)
1647 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001648 }
1649
Michel Dänzer120efee2013-01-25 12:10:11 +01001650 /* Pack texture coordinates */
1651 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001652 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001653 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001654 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001655 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001656
Marek Olšákd2bd6342013-09-18 15:40:21 +02001657 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001658 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001659 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001660 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001661 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001662
1663 if (count > 16) {
1664 assert(!"Cannot handle more than 16 texture address parameters");
1665 count = 16;
1666 }
1667
1668 for (chan = 0; chan < count; chan++ ) {
1669 address[chan] = LLVMBuildBitCast(gallivm->builder,
1670 address[chan],
1671 LLVMInt32TypeInContext(gallivm->context),
1672 "");
1673 }
1674
Marek Olšák4855acd2013-08-06 15:08:54 +02001675 /* Adjust the sample index according to FMASK.
1676 *
1677 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1678 * which is the identity mapping. Each nibble says which physical sample
1679 * should be fetched to get that sample.
1680 *
1681 * For example, 0x11111100 means there are only 2 samples stored and
1682 * the second sample covers 3/4 of the pixel. When reading samples 0
1683 * and 1, return physical sample 0 (determined by the first two 0s
1684 * in FMASK), otherwise return physical sample 1.
1685 *
1686 * The sample index should be adjusted as follows:
1687 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1688 */
1689 if (target == TGSI_TEXTURE_2D_MSAA ||
1690 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1691 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1692 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001693 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001694 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001695 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001696
Marek Olšákd2bd6342013-09-18 15:40:21 +02001697 memcpy(txf_address, address, sizeof(txf_address));
1698
1699 if (target == TGSI_TEXTURE_2D_MSAA) {
1700 txf_address[2] = bld_base->uint_bld.zero;
1701 }
1702 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001703
1704 /* Pad to a power-of-two size. */
1705 while (txf_count < util_next_power_of_two(txf_count))
1706 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1707
1708 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001709 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1710 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1711 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001712 txf_emit_data.chan = 0;
1713 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001714 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001715 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001716 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001717 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001718 txf_emit_data.arg_count = 3;
1719
Marek Olšák877bb522014-06-25 03:12:46 +02001720 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001721
1722 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001723 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1724 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1725
1726 /* Apply the formula. */
1727 LLVMValueRef fmask =
1728 LLVMBuildExtractElement(gallivm->builder,
1729 txf_emit_data.output[0],
1730 uint_bld->zero, "");
1731
Marek Olšákd2bd6342013-09-18 15:40:21 +02001732 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001733
1734 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001735 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001736
1737 LLVMValueRef shifted_fmask =
1738 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1739
1740 LLVMValueRef final_sample =
1741 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1742
1743 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1744 * resource descriptor is 0 (invalid),
1745 */
1746 LLVMValueRef fmask_desc =
1747 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001748 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001749 LLVMVectorType(uint_bld->elem_type, 8), "");
1750
1751 LLVMValueRef fmask_word1 =
1752 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1753 uint_bld->one, "");
1754
1755 LLVMValueRef word1_is_nonzero =
1756 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1757 fmask_word1, uint_bld->zero, "");
1758
Marek Olšákd2bd6342013-09-18 15:40:21 +02001759 /* Replace the MSAA sample index. */
1760 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001761 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001762 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001763 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001764
Michel Dänzer36231112013-05-02 09:44:45 +02001765 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001766 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001767
1768 if (opcode == TGSI_OPCODE_TXF) {
1769 /* add tex offsets */
1770 if (inst->Texture.NumOffsets) {
1771 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1772 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1773 const struct tgsi_texture_offset * off = inst->TexOffsets;
1774
1775 assert(inst->Texture.NumOffsets == 1);
1776
Marek Olšákdefedc02013-09-18 15:36:38 +02001777 switch (target) {
1778 case TGSI_TEXTURE_3D:
1779 address[2] = lp_build_add(uint_bld, address[2],
1780 bld->immediates[off->Index][off->SwizzleZ]);
1781 /* fall through */
1782 case TGSI_TEXTURE_2D:
1783 case TGSI_TEXTURE_SHADOW2D:
1784 case TGSI_TEXTURE_RECT:
1785 case TGSI_TEXTURE_SHADOWRECT:
1786 case TGSI_TEXTURE_2D_ARRAY:
1787 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001788 address[1] =
1789 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001790 bld->immediates[off->Index][off->SwizzleY]);
1791 /* fall through */
1792 case TGSI_TEXTURE_1D:
1793 case TGSI_TEXTURE_SHADOW1D:
1794 case TGSI_TEXTURE_1D_ARRAY:
1795 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1796 address[0] =
1797 lp_build_add(uint_bld, address[0],
1798 bld->immediates[off->Index][off->SwizzleX]);
1799 break;
1800 /* texture offsets do not apply to other texture targets */
1801 }
Michel Dänzer36231112013-05-02 09:44:45 +02001802 }
1803
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001804 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1805 emit_data->arg_count = 3;
1806
Michel Dänzer36231112013-05-02 09:44:45 +02001807 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001808 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001809 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001810 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001811 opcode == TGSI_OPCODE_LODQ ||
1812 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001813 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1814 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1815 target == TGSI_TEXTURE_2D_ARRAY ||
1816 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1817 target == TGSI_TEXTURE_CUBE_ARRAY ||
1818 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1819 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001820 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001821
Marek Olšák57f3da92014-06-16 16:53:42 +02001822 if (opcode == TGSI_OPCODE_TG4) {
1823 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001824
Marek Olšák57f3da92014-06-16 16:53:42 +02001825 /* DMASK was repurposed for GATHER4. 4 components are always
1826 * returned and DMASK works like a swizzle - it selects
1827 * the component to fetch. The only valid DMASK values are
1828 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1829 * (red,red,red,red) etc.) The ISA document doesn't mention
1830 * this.
1831 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001832
Marek Olšák57f3da92014-06-16 16:53:42 +02001833 /* Get the component index from src1.x for Gather4. */
1834 if (!tgsi_is_shadow_sampler(target)) {
1835 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1836 LLVMValueRef comp_imm;
1837 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001838
Marek Olšák57f3da92014-06-16 16:53:42 +02001839 assert(src1.File == TGSI_FILE_IMMEDIATE);
1840
1841 comp_imm = imms[src1.Index][src1.SwizzleX];
1842 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1843 gather_comp = CLAMP(gather_comp, 0, 3);
1844 }
1845
1846 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001847 }
1848
Marek Olšák4855acd2013-08-06 15:08:54 +02001849 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001850 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001851 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1852 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1853 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1854 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1855 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1856 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1857 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1858
1859 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001860
1861 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001862 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001863 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001864 } else {
1865 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1866 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001867 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001868
1869 emit_data->dst_type = LLVMVectorType(
1870 LLVMFloatTypeInContext(gallivm->context),
1871 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001872 }
1873
Marek Olšák2484daa2014-04-22 21:23:29 +02001874 /* The fetch opcode has been converted to a 2D array fetch.
1875 * This simplifies the LLVM backend. */
1876 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1877 target = TGSI_TEXTURE_2D_ARRAY;
1878 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1879 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1880
Michel Dänzer120efee2013-01-25 12:10:11 +01001881 /* Pad to power of two vector */
1882 while (count < util_next_power_of_two(count))
1883 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1884
Christian Königccf3e8f2013-03-26 15:09:27 +01001885 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001886}
1887
Michel Dänzer07eddc42013-02-06 15:43:10 +01001888static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1889 struct lp_build_tgsi_context * bld_base,
1890 struct lp_build_emit_data * emit_data)
1891{
1892 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001893 unsigned opcode = emit_data->inst->Instruction.Opcode;
1894 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001895 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001896 bool has_offset = HAVE_LLVM >= 0x0305 ?
1897 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001898
Marek Olšák877bb522014-06-25 03:12:46 +02001899 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001900 emit_data->output[emit_data->chan] = build_intrinsic(
1901 base->gallivm->builder,
1902 "llvm.SI.vs.load.input", emit_data->dst_type,
1903 emit_data->args, emit_data->arg_count,
1904 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1905 return;
1906 }
1907
Marek Olšák877bb522014-06-25 03:12:46 +02001908 if (opcode == TGSI_OPCODE_TG4 ||
1909 opcode == TGSI_OPCODE_LODQ ||
1910 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1911 bool is_shadow = tgsi_is_shadow_sampler(target);
1912 const char *name = "llvm.SI.image.sample";
1913 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001914
Marek Olšák877bb522014-06-25 03:12:46 +02001915 switch (opcode) {
1916 case TGSI_OPCODE_TEX:
1917 case TGSI_OPCODE_TEX2:
1918 case TGSI_OPCODE_TXP:
1919 break;
1920 case TGSI_OPCODE_TXB:
1921 case TGSI_OPCODE_TXB2:
1922 infix = ".b";
1923 break;
1924 case TGSI_OPCODE_TXL:
1925 case TGSI_OPCODE_TXL2:
1926 infix = ".l";
1927 break;
1928 case TGSI_OPCODE_TXD:
1929 infix = ".d";
1930 break;
1931 case TGSI_OPCODE_TG4:
1932 name = "llvm.SI.gather4";
1933 break;
1934 case TGSI_OPCODE_LODQ:
1935 name = "llvm.SI.getlod";
1936 is_shadow = false;
1937 has_offset = false;
1938 break;
1939 default:
1940 assert(0);
1941 return;
1942 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001943
Marek Olšák877bb522014-06-25 03:12:46 +02001944 /* Add the type and suffixes .c, .o if needed. */
1945 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1946 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1947 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001948
Marek Olšák877bb522014-06-25 03:12:46 +02001949 emit_data->output[emit_data->chan] = build_intrinsic(
1950 base->gallivm->builder, intr_name, emit_data->dst_type,
1951 emit_data->args, emit_data->arg_count,
1952 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1953 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001954 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001955 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001956
Marek Olšák877bb522014-06-25 03:12:46 +02001957 switch (opcode) {
1958 case TGSI_OPCODE_TEX:
1959 case TGSI_OPCODE_TEX2:
1960 case TGSI_OPCODE_TXP:
1961 name = "llvm.SI.sample";
1962 break;
1963 case TGSI_OPCODE_TXB:
1964 case TGSI_OPCODE_TXB2:
1965 name = "llvm.SI.sampleb";
1966 break;
1967 case TGSI_OPCODE_TXD:
1968 name = "llvm.SI.sampled";
1969 break;
1970 case TGSI_OPCODE_TXF:
1971 name = "llvm.SI.imageload";
1972 break;
1973 case TGSI_OPCODE_TXL:
1974 case TGSI_OPCODE_TXL2:
1975 name = "llvm.SI.samplel";
1976 break;
1977 default:
1978 assert(0);
1979 return;
1980 }
1981
Marek Olšákd859bdb2014-07-14 19:40:14 +02001982 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1983 v16i8 = LLVMVectorType(i8, 16);
1984 v32i8 = LLVMVectorType(i8, 32);
1985
1986 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1987 emit_data->args[1], v32i8, "");
1988 if (opcode != TGSI_OPCODE_TXF) {
1989 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1990 emit_data->args[2], v16i8, "");
1991 }
1992
Marek Olšák877bb522014-06-25 03:12:46 +02001993 sprintf(intr_name, "%s.v%ui32", name,
1994 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1995
1996 emit_data->output[emit_data->chan] = build_intrinsic(
1997 base->gallivm->builder, intr_name, emit_data->dst_type,
1998 emit_data->args, emit_data->arg_count,
1999 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2000 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002001}
2002
Michel Dänzer0495adb2013-05-06 12:45:14 +02002003static void txq_fetch_args(
2004 struct lp_build_tgsi_context * bld_base,
2005 struct lp_build_emit_data * emit_data)
2006{
2007 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2008 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002009 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002010 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002011
Marek Olšák2484daa2014-04-22 21:23:29 +02002012 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002013 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2014 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2015
2016 /* Read the size from the buffer descriptor directly. */
2017 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2018 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2019 size = LLVMBuildExtractElement(gallivm->builder, size,
Marek Olšákb142dd22015-02-01 16:58:08 +01002020 lp_build_const_int32(gallivm, 6), "");
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002021 emit_data->args[0] = size;
2022 return;
2023 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002024
2025 /* Mip level */
2026 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2027
2028 /* Resource */
2029 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2030
Marek Olšák2484daa2014-04-22 21:23:29 +02002031 /* Texture target */
2032 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2033 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2034 target = TGSI_TEXTURE_2D_ARRAY;
2035
Michel Dänzer0495adb2013-05-06 12:45:14 +02002036 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002037 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002038
2039 emit_data->arg_count = 3;
2040
2041 emit_data->dst_type = LLVMVectorType(
2042 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2043 4);
2044}
2045
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002046static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2047 struct lp_build_tgsi_context * bld_base,
2048 struct lp_build_emit_data * emit_data)
2049{
Marek Olšák2484daa2014-04-22 21:23:29 +02002050 unsigned target = emit_data->inst->Texture.Texture;
2051
2052 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002053 /* Just return the buffer size. */
2054 emit_data->output[emit_data->chan] = emit_data->args[0];
2055 return;
2056 }
2057
2058 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002059
2060 /* Divide the number of layers by 6 to get the number of cubes. */
2061 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2062 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2063 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2064 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2065 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2066
2067 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2068 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2069 z = LLVMBuildSDiv(builder, z, six, "");
2070
2071 emit_data->output[emit_data->chan] =
2072 LLVMBuildInsertElement(builder, v4, z, two, "");
2073 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002074}
2075
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002076static void si_llvm_emit_ddxy(
2077 const struct lp_build_tgsi_action * action,
2078 struct lp_build_tgsi_context * bld_base,
2079 struct lp_build_emit_data * emit_data)
2080{
2081 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2082 struct gallivm_state *gallivm = bld_base->base.gallivm;
2083 struct lp_build_context * base = &bld_base->base;
2084 const struct tgsi_full_instruction *inst = emit_data->inst;
2085 unsigned opcode = inst->Instruction.Opcode;
2086 LLVMValueRef indices[2];
2087 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2088 LLVMValueRef tl, trbl, result[4];
2089 LLVMTypeRef i32;
2090 unsigned swizzle[4];
2091 unsigned c;
2092
2093 i32 = LLVMInt32TypeInContext(gallivm->context);
2094
2095 indices[0] = bld_base->uint_bld.zero;
2096 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2097 NULL, 0, LLVMReadNoneAttribute);
2098 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2099 indices, 2, "");
2100
2101 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2102 lp_build_const_int32(gallivm, 0xfffffffc), "");
2103 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2104 indices, 2, "");
2105
2106 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2107 lp_build_const_int32(gallivm,
2108 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2109 "");
2110 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2111 indices, 2, "");
2112
2113 for (c = 0; c < 4; ++c) {
2114 unsigned i;
2115
2116 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2117 for (i = 0; i < c; ++i) {
2118 if (swizzle[i] == swizzle[c]) {
2119 result[c] = result[i];
2120 break;
2121 }
2122 }
2123 if (i != c)
2124 continue;
2125
2126 LLVMBuildStore(gallivm->builder,
2127 LLVMBuildBitCast(gallivm->builder,
2128 lp_build_emit_fetch(bld_base, inst, 0, c),
2129 i32, ""),
2130 store_ptr);
2131
2132 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2133 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2134
2135 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2136 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2137
2138 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2139 }
2140
2141 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2142}
2143
Michel Dänzer404b29d2013-11-21 16:45:28 +09002144/* Emit one vertex from the geometry shader */
2145static void si_llvm_emit_vertex(
2146 const struct lp_build_tgsi_action *action,
2147 struct lp_build_tgsi_context *bld_base,
2148 struct lp_build_emit_data *emit_data)
2149{
2150 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002151 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002152 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002153 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002154 struct gallivm_state *gallivm = bld_base->base.gallivm;
2155 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002156 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2157 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002158 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002159 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002160 LLVMValueRef args[2];
2161 unsigned chan;
2162 int i;
2163
Michel Dänzer404b29d2013-11-21 16:45:28 +09002164 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002165 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002166
2167 /* If this thread has already emitted the declared maximum number of
2168 * vertices, kill it: excessive vertex emissions are not supposed to
2169 * have any effect, and GS threads have no externally observable
2170 * effects other than emitting vertices.
2171 */
2172 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2173 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002174 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002175 kill = lp_build_select(&bld_base->base, can_emit,
2176 lp_build_const_float(gallivm, 1.0f),
2177 lp_build_const_float(gallivm, -1.0f));
2178 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2179 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2180
Marek Olšák02134cf2014-10-04 22:07:50 +02002181 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002182 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002183 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002184
2185 for (chan = 0; chan < 4; chan++) {
2186 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002187 LLVMValueRef voffset =
2188 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002189 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002190
2191 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2192 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002193
2194 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2195
Marek Olšákfc3b3352014-10-05 13:33:40 +02002196 build_tbuffer_store(si_shader_ctx,
2197 si_shader_ctx->gsvs_ring,
2198 out_val, 1,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002199 voffset, soffset, 0,
2200 V_008F0C_BUF_DATA_FORMAT_32,
2201 V_008F0C_BUF_NUM_FORMAT_UINT,
2202 1, 0, 1, 1, 0);
2203 }
2204 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002205 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2206 lp_build_const_int32(gallivm, 1));
2207 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002208
2209 /* Signal vertex emission */
2210 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2211 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2212 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2213 LLVMVoidTypeInContext(gallivm->context), args, 2,
2214 LLVMNoUnwindAttribute);
2215}
2216
2217/* Cut one primitive from the geometry shader */
2218static void si_llvm_emit_primitive(
2219 const struct lp_build_tgsi_action *action,
2220 struct lp_build_tgsi_context *bld_base,
2221 struct lp_build_emit_data *emit_data)
2222{
2223 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2224 struct gallivm_state *gallivm = bld_base->base.gallivm;
2225 LLVMValueRef args[2];
2226
2227 /* Signal primitive cut */
2228 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2229 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2230 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2231 LLVMVoidTypeInContext(gallivm->context), args, 2,
2232 LLVMNoUnwindAttribute);
2233}
2234
Tom Stellarda75c6162012-01-06 17:38:37 -05002235static const struct lp_build_tgsi_action tex_action = {
2236 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002237 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002238};
2239
Michel Dänzer0495adb2013-05-06 12:45:14 +02002240static const struct lp_build_tgsi_action txq_action = {
2241 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002242 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002243 .intr_name = "llvm.SI.resinfo"
2244};
2245
Christian König206f0592013-03-20 14:37:21 +01002246static void create_meta_data(struct si_shader_context *si_shader_ctx)
2247{
2248 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2249 LLVMValueRef args[3];
2250
2251 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2252 args[1] = 0;
2253 args[2] = lp_build_const_int32(gallivm, 1);
2254
2255 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2256}
2257
Marek Olšák4f3f0432014-07-07 22:49:15 +02002258static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2259{
2260 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2261 CONST_ADDR_SPACE);
2262}
2263
Christian König55fe5cc2013-03-04 16:30:06 +01002264static void create_function(struct si_shader_context *si_shader_ctx)
2265{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002266 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2267 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002268 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002269 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002270 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002271
Christian König55fe5cc2013-03-04 16:30:06 +01002272 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002273 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002274 f32 = LLVMFloatTypeInContext(gallivm->context);
2275 v2i32 = LLVMVectorType(i32, 2);
2276 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002277 v4i32 = LLVMVectorType(i32, 4);
2278 v8i32 = LLVMVectorType(i32, 8);
2279 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002280
Marek Olšákee2a8182014-07-07 23:27:19 +02002281 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002282 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002283 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2284 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002285 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002286
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002287 switch (si_shader_ctx->type) {
2288 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002289 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002290 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002291 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002292 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002293 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002294
Michel Dänzer404b29d2013-11-21 16:45:28 +09002295 if (shader->key.vs.as_es) {
2296 params[SI_PARAM_ES2GS_OFFSET] = i32;
2297 num_params++;
2298 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002299 if (shader->is_gs_copy_shader) {
2300 last_array_pointer = SI_PARAM_CONST;
2301 num_params = SI_PARAM_CONST+1;
2302 }
2303
Michel Dänzer404b29d2013-11-21 16:45:28 +09002304 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002305
Michel Dänzer404b29d2013-11-21 16:45:28 +09002306 /* Streamout SGPRs. */
2307 if (shader->selector->so.num_outputs) {
2308 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2309 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2310 }
2311 /* A streamout buffer offset is loaded if the stride is non-zero. */
2312 for (i = 0; i < 4; i++) {
2313 if (!shader->selector->so.stride[i])
2314 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002315
Michel Dänzer404b29d2013-11-21 16:45:28 +09002316 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2317 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002318 }
2319
2320 last_sgpr = num_params-1;
2321
2322 /* VGPRs */
2323 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2324 params[num_params++] = i32; /* unused*/
2325 params[num_params++] = i32; /* unused */
2326 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002327 break;
Christian König0666ffd2013-03-05 15:07:39 +01002328
Michel Dänzer404b29d2013-11-21 16:45:28 +09002329 case TGSI_PROCESSOR_GEOMETRY:
2330 params[SI_PARAM_GS2VS_OFFSET] = i32;
2331 params[SI_PARAM_GS_WAVE_ID] = i32;
2332 last_sgpr = SI_PARAM_GS_WAVE_ID;
2333
2334 /* VGPRs */
2335 params[SI_PARAM_VTX0_OFFSET] = i32;
2336 params[SI_PARAM_VTX1_OFFSET] = i32;
2337 params[SI_PARAM_PRIMITIVE_ID] = i32;
2338 params[SI_PARAM_VTX2_OFFSET] = i32;
2339 params[SI_PARAM_VTX3_OFFSET] = i32;
2340 params[SI_PARAM_VTX4_OFFSET] = i32;
2341 params[SI_PARAM_VTX5_OFFSET] = i32;
2342 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2343 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2344 break;
2345
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002346 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002347 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002348 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002349 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002350 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2351 params[SI_PARAM_PERSP_CENTER] = v2i32;
2352 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2353 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2354 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2355 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2356 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2357 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2358 params[SI_PARAM_POS_X_FLOAT] = f32;
2359 params[SI_PARAM_POS_Y_FLOAT] = f32;
2360 params[SI_PARAM_POS_Z_FLOAT] = f32;
2361 params[SI_PARAM_POS_W_FLOAT] = f32;
2362 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002363 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002364 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2365 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002366 num_params = SI_PARAM_POS_FIXED_PT+1;
2367 break;
2368
2369 default:
2370 assert(0 && "unimplemented shader");
2371 return;
Christian Königc4973212013-03-05 12:14:02 +01002372 }
Christian König55fe5cc2013-03-04 16:30:06 +01002373
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002374 assert(num_params <= Elements(params));
2375 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002376 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002377
Marek Olšák1829f9c2015-01-04 17:08:57 +01002378 if (shader->dx10_clamp_mode)
2379 LLVMAddTargetDependentFunctionAttr(si_shader_ctx->radeon_bld.main_fn,
2380 "enable-no-nans-fp-math", "true");
2381
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002382 for (i = 0; i <= last_sgpr; ++i) {
2383 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002384
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002385 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2386 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002387 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002388 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002389 else
2390 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002391 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002392
Michel Dänzer404b29d2013-11-21 16:45:28 +09002393 if (bld_base->info &&
2394 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2395 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002396 si_shader_ctx->ddxy_lds =
2397 LLVMAddGlobalInAddressSpace(gallivm->module,
2398 LLVMArrayType(i32, 64),
2399 "ddxy_lds",
2400 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002401}
Tom Stellarda75c6162012-01-06 17:38:37 -05002402
Christian König0f6cf2b2013-03-15 15:53:25 +01002403static void preload_constants(struct si_shader_context *si_shader_ctx)
2404{
2405 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2406 struct gallivm_state * gallivm = bld_base->base.gallivm;
2407 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002408 unsigned buf;
2409 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002410
Marek Olšákee2a8182014-07-07 23:27:19 +02002411 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002412 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002413
Marek Olšák2fd42002013-10-25 11:45:47 +02002414 if (num_const == 0)
2415 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002416
Marek Olšák2fd42002013-10-25 11:45:47 +02002417 /* Allocate space for the constant values */
2418 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002419
Marek Olšák2fd42002013-10-25 11:45:47 +02002420 /* Load the resource descriptor */
2421 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002422 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002423
Marek Olšák2fd42002013-10-25 11:45:47 +02002424 /* Load the constants, we rely on the code sinking to do the rest */
2425 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002426 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002427 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002428 si_shader_ctx->const_resource[buf],
2429 lp_build_const_int32(gallivm, i * 4),
2430 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002431 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002432 }
2433}
2434
Christian König1c100182013-03-17 16:02:42 +01002435static void preload_samplers(struct si_shader_context *si_shader_ctx)
2436{
2437 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2438 struct gallivm_state * gallivm = bld_base->base.gallivm;
2439 const struct tgsi_shader_info * info = bld_base->info;
2440
2441 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2442
2443 LLVMValueRef res_ptr, samp_ptr;
2444 LLVMValueRef offset;
2445
2446 if (num_samplers == 0)
2447 return;
2448
Christian König1c100182013-03-17 16:02:42 +01002449 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2450 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2451
2452 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2453 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002454 /* Resource */
2455 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002456 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002457
2458 /* Sampler */
2459 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002460 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002461
2462 /* FMASK resource */
2463 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002464 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2465 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002466 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002467 }
Christian König1c100182013-03-17 16:02:42 +01002468 }
2469}
2470
Marek Olšák8d03d922013-09-01 23:59:06 +02002471static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2472{
2473 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2474 struct gallivm_state * gallivm = bld_base->base.gallivm;
2475 unsigned i;
2476
Michel Dänzer67e385b2014-01-08 17:48:21 +09002477 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2478 si_shader_ctx->shader->key.vs.as_es ||
2479 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002480 return;
2481
2482 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002483 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002484
2485 /* Load the resources, we rely on the code sinking to do the rest */
2486 for (i = 0; i < 4; ++i) {
2487 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002488 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002489 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002490
Marek Olšákd7876082014-09-26 23:06:32 +02002491 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002492 }
2493 }
2494}
2495
Marek Olšákfc3b3352014-10-05 13:33:40 +02002496/**
2497 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
2498 * for later use.
2499 */
2500static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
2501{
2502 struct gallivm_state *gallivm =
2503 si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2504
2505 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2506 SI_PARAM_RW_BUFFERS);
2507
2508 if ((si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
2509 si_shader_ctx->shader->key.vs.as_es) ||
2510 si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY) {
2511 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_ESGS);
2512
2513 si_shader_ctx->esgs_ring =
2514 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2515 }
2516
2517 if (si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY ||
2518 si_shader_ctx->shader->is_gs_copy_shader) {
2519 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_GSVS);
2520
2521 si_shader_ctx->gsvs_ring =
2522 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2523 }
2524}
2525
Tom Stellard2397a722014-12-10 09:13:59 -05002526void si_shader_binary_read_config(const struct si_screen *sscreen,
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002527 struct si_shader *shader,
2528 unsigned symbol_offset)
Tom Stellard302f53d2012-10-25 13:50:10 -04002529{
Tom Stellard302f53d2012-10-25 13:50:10 -04002530 unsigned i;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002531 const unsigned char *config =
Tom Stellard2397a722014-12-10 09:13:59 -05002532 radeon_shader_binary_config_start(&shader->binary,
2533 symbol_offset);
Tom Stellard302f53d2012-10-25 13:50:10 -04002534
Tom Stellardd50343d2013-04-04 16:21:06 -04002535 /* XXX: We may be able to emit some of these values directly rather than
2536 * extracting fields to be emitted later.
2537 */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002538
Tom Stellard2397a722014-12-10 09:13:59 -05002539 for (i = 0; i < shader->binary.config_size_per_symbol; i+= 8) {
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002540 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
2541 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
Tom Stellardd50343d2013-04-04 16:21:06 -04002542 switch (reg) {
2543 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2544 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2545 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2546 case R_00B848_COMPUTE_PGM_RSRC1:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002547 shader->num_sgprs = MAX2(shader->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
2548 shader->num_vgprs = MAX2(shader->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
Tom Stellardbbfa1c32015-03-06 14:53:00 +00002549 shader->float_mode = G_00B028_FLOAT_MODE(value);
Tom Stellardd50343d2013-04-04 16:21:06 -04002550 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002551 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002552 shader->lds_size = MAX2(shader->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002553 break;
2554 case R_00B84C_COMPUTE_PGM_RSRC2:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002555 shader->lds_size = MAX2(shader->lds_size, G_00B84C_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002556 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002557 case R_0286CC_SPI_PS_INPUT_ENA:
2558 shader->spi_ps_input_ena = value;
2559 break;
Tom Stellard2397a722014-12-10 09:13:59 -05002560 case R_0286E8_SPI_TMPRING_SIZE:
Tom Stellardb0f78032014-07-18 14:45:18 -04002561 case R_00B860_COMPUTE_TMPRING_SIZE:
2562 /* WAVESIZE is in units of 256 dwords. */
2563 shader->scratch_bytes_per_wave =
2564 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2565 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002566 default:
2567 fprintf(stderr, "Warning: Compiler emitted unknown "
2568 "config register: 0x%x\n", reg);
2569 break;
2570 }
2571 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002572}
2573
Tom Stellard2397a722014-12-10 09:13:59 -05002574void si_shader_apply_scratch_relocs(struct si_context *sctx,
2575 struct si_shader *shader,
2576 uint64_t scratch_va)
2577{
2578 unsigned i;
2579 uint32_t scratch_rsrc_dword0 = scratch_va & 0xffffffff;
2580 uint32_t scratch_rsrc_dword1 =
2581 S_008F04_BASE_ADDRESS_HI(scratch_va >> 32)
2582 | S_008F04_STRIDE(shader->scratch_bytes_per_wave / 64);
2583
2584 for (i = 0 ; i < shader->binary.reloc_count; i++) {
2585 const struct radeon_shader_reloc *reloc =
2586 &shader->binary.relocs[i];
2587 if (!strcmp(scratch_rsrc_dword0_symbol, reloc->name)) {
2588 util_memcpy_cpu_to_le32(shader->binary.code + reloc->offset,
2589 &scratch_rsrc_dword0, 4);
2590 } else if (!strcmp(scratch_rsrc_dword1_symbol, reloc->name)) {
2591 util_memcpy_cpu_to_le32(shader->binary.code + reloc->offset,
2592 &scratch_rsrc_dword1, 4);
2593 }
2594 }
2595}
2596
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002597int si_shader_binary_read(struct si_screen *sscreen,
2598 struct si_shader *shader,
2599 const struct radeon_shader_binary *binary)
2600{
2601
2602 unsigned i;
2603 unsigned code_size;
2604 unsigned char *ptr;
2605 bool dump = r600_can_dump_shader(&sscreen->b,
2606 shader->selector ? shader->selector->tokens : NULL);
2607
Tom Stellard51b43c52015-02-26 21:27:27 -05002608 si_shader_binary_read_config(sscreen, shader, 0);
2609
2610 if (dump) {
2611 if (!binary->disassembled) {
2612 fprintf(stderr, "SI CODE:\n");
2613 for (i = 0; i < binary->code_size; i+=4 ) {
2614 fprintf(stderr, "@0x%x: %02x%02x%02x%02x\n", i, binary->code[i + 3],
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002615 binary->code[i + 2], binary->code[i + 1],
2616 binary->code[i]);
Tom Stellard51b43c52015-02-26 21:27:27 -05002617 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002618 }
Tom Stellard51b43c52015-02-26 21:27:27 -05002619 fprintf(stderr, "SGPRS: %d\nVGPRS: %d\nCode Size: %d bytes\nLDS: %d blocks\n"
2620 "Scratch: %d bytes per wave\n",
2621 shader->num_sgprs, shader->num_vgprs, binary->code_size,
2622 shader->lds_size, shader->scratch_bytes_per_wave);
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002623 }
2624
Tom Stellard302f53d2012-10-25 13:50:10 -04002625 /* copy new shader */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002626 code_size = binary->code_size + binary->rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002627 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002628 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002629 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002630 if (shader->bo == NULL) {
2631 return -ENOMEM;
2632 }
2633
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002634
2635 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_READ_WRITE);
2636 util_memcpy_cpu_to_le32(ptr, binary->code, binary->code_size);
2637 if (binary->rodata_size > 0) {
2638 ptr += binary->code_size;
2639 util_memcpy_cpu_to_le32(ptr, binary->rodata, binary->rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002640 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002641
Marek Olšák1abb1a92014-09-17 22:44:22 +02002642 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002643
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002644 return 0;
2645}
Tom Stellard302f53d2012-10-25 13:50:10 -04002646
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002647int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2648 LLVMModuleRef mod)
2649{
2650 int r = 0;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002651 bool dump = r600_can_dump_shader(&sscreen->b,
2652 shader->selector ? shader->selector->tokens : NULL);
Tom Stellard2397a722014-12-10 09:13:59 -05002653 r = radeon_llvm_compile(mod, &shader->binary,
Tom Stellard761e36b2014-10-15 12:24:30 -04002654 r600_get_llvm_processor_name(sscreen->b.family), dump, sscreen->tm);
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002655
2656 if (r) {
2657 return r;
2658 }
Tom Stellard2397a722014-12-10 09:13:59 -05002659 r = si_shader_binary_read(sscreen, shader, &shader->binary);
2660
2661 FREE(shader->binary.config);
2662 FREE(shader->binary.rodata);
2663 FREE(shader->binary.global_symbol_offsets);
2664 if (shader->scratch_bytes_per_wave == 0) {
2665 FREE(shader->binary.code);
2666 FREE(shader->binary.relocs);
2667 memset(&shader->binary, 0, sizeof(shader->binary));
2668 }
Darren Powellbc866902014-03-31 18:00:28 -04002669 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002670}
2671
Michel Dänzer404b29d2013-11-21 16:45:28 +09002672/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002673static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002674 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002675 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002676{
2677 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2678 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2679 struct lp_build_context *base = &bld_base->base;
2680 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002681 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002682 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002683 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002684 LLVMValueRef args[9];
2685 int i, r;
2686
Marek Olšák02134cf2014-10-04 22:07:50 +02002687 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002688
2689 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002690 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002691
2692 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2693
2694 create_meta_data(si_shader_ctx);
2695 create_function(si_shader_ctx);
2696 preload_streamout_buffers(si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002697 preload_ring_buffers(si_shader_ctx);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002698
Marek Olšákfc3b3352014-10-05 13:33:40 +02002699 args[0] = si_shader_ctx->gsvs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002700 args[1] = lp_build_mul_imm(uint,
2701 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2702 si_shader_ctx->param_vertex_id),
2703 4);
2704 args[3] = uint->zero;
2705 args[4] = uint->one; /* OFFEN */
2706 args[5] = uint->zero; /* IDXEN */
2707 args[6] = uint->one; /* GLC */
2708 args[7] = uint->one; /* SLC */
2709 args[8] = uint->zero; /* TFE */
2710
2711 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002712 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002713 unsigned chan;
2714
Marek Olšák02134cf2014-10-04 22:07:50 +02002715 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002716 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002717
2718 for (chan = 0; chan < 4; chan++) {
2719 args[2] = lp_build_const_int32(gallivm,
2720 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002721 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002722
2723 outputs[i].values[chan] =
2724 LLVMBuildBitCast(gallivm->builder,
2725 build_intrinsic(gallivm->builder,
2726 "llvm.SI.buffer.load.dword.i32.i32",
2727 LLVMInt32TypeInContext(gallivm->context),
2728 args, 9,
2729 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2730 base->elem_type, "");
2731 }
2732 }
2733
Marek Olšák02134cf2014-10-04 22:07:50 +02002734 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002735
2736 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2737
2738 if (dump)
2739 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2740
Marek Olšák1abb1a92014-09-17 22:44:22 +02002741 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002742 bld_base->base.gallivm->module);
2743
2744 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2745
2746 FREE(outputs);
2747 return r;
2748}
2749
Marek Olšákffd701e2015-02-22 14:58:30 +01002750static void si_dump_key(unsigned shader, union si_shader_key *key)
2751{
2752 int i;
2753
2754 fprintf(stderr, "SHADER KEY\n");
2755
2756 switch (shader) {
2757 case PIPE_SHADER_VERTEX:
2758 fprintf(stderr, " instance_divisors = {");
2759 for (i = 0; i < Elements(key->vs.instance_divisors); i++)
2760 fprintf(stderr, !i ? "%u" : ", %u",
2761 key->vs.instance_divisors[i]);
2762 fprintf(stderr, "}\n");
2763
2764 if (key->vs.as_es)
2765 fprintf(stderr, " gs_used_inputs = 0x%"PRIx64"\n",
2766 key->vs.gs_used_inputs);
2767 fprintf(stderr, " as_es = %u\n", key->vs.as_es);
2768 break;
2769
2770 case PIPE_SHADER_GEOMETRY:
2771 break;
2772
2773 case PIPE_SHADER_FRAGMENT:
2774 fprintf(stderr, " export_16bpc = 0x%X\n", key->ps.export_16bpc);
2775 fprintf(stderr, " last_cbuf = %u\n", key->ps.last_cbuf);
2776 fprintf(stderr, " color_two_side = %u\n", key->ps.color_two_side);
2777 fprintf(stderr, " alpha_func = %u\n", key->ps.alpha_func);
2778 fprintf(stderr, " alpha_to_one = %u\n", key->ps.alpha_to_one);
2779 fprintf(stderr, " poly_stipple = %u\n", key->ps.poly_stipple);
2780 break;
2781
2782 default:
2783 assert(0);
2784 }
2785}
2786
Marek Olšák1abb1a92014-09-17 22:44:22 +02002787int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002788{
Marek Olšák2774abd2014-09-16 18:45:33 +02002789 struct si_shader_selector *sel = shader->selector;
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002790 struct tgsi_token *tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002791 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002792 struct lp_build_tgsi_context * bld_base;
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002793 struct tgsi_shader_info stipple_shader_info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002794 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002795 int r = 0;
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002796 bool poly_stipple = sel->type == PIPE_SHADER_FRAGMENT &&
2797 shader->key.ps.poly_stipple;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002798 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002799
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002800 if (poly_stipple) {
2801 tokens = util_pstipple_create_fragment_shader(tokens, NULL,
2802 SI_POLY_STIPPLE_SAMPLER);
2803 tgsi_scan_shader(tokens, &stipple_shader_info);
2804 }
2805
Michel Dänzere1df0d42014-01-15 12:31:07 +09002806 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2807 * conversion fails. */
2808 if (dump) {
Marek Olšákffd701e2015-02-22 14:58:30 +01002809 si_dump_key(sel->type, &shader->key);
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002810 tgsi_dump(tokens, 0);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002811 si_dump_streamout(&sel->so);
2812 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002813
Marek Olšák8c37c162014-09-16 18:40:07 +02002814 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002815
Michel Dänzercfebaf92012-08-31 19:04:08 +02002816 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002817 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2818 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2819
Marek Olšák1829f9c2015-01-04 17:08:57 +01002820 if (sel->type != PIPE_SHADER_COMPUTE)
2821 shader->dx10_clamp_mode = true;
2822
Marek Olšák52335682014-09-30 15:12:09 +02002823 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002824 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2825
Marek Olšák52335682014-09-30 15:12:09 +02002826 shader->uses_instanceid = sel->info.uses_instanceid;
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002827 bld_base->info = poly_stipple ? &stipple_shader_info : &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002828 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002829
2830 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002831 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002832 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2833 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002834 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002835 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2836 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2837 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002838 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002839 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002840 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2841 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002842
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002843 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2844 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002845
Michel Dänzer404b29d2013-11-21 16:45:28 +09002846 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2847 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2848
Marek Olšák645b4712014-11-20 22:16:09 +01002849 if (HAVE_LLVM >= 0x0306) {
2850 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
2851 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
2852 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
2853 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
2854 }
2855
Christian Könige4ed5872013-03-21 18:02:52 +01002856 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Tom Stellarda75c6162012-01-06 17:38:37 -05002857 si_shader_ctx.shader = shader;
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002858 si_shader_ctx.type = tgsi_get_processor_type(tokens);
Marek Olšák368b0a72014-12-28 21:51:35 +01002859 si_shader_ctx.screen = sscreen;
Tom Stellarda75c6162012-01-06 17:38:37 -05002860
Michel Dänzer51f89a02013-12-09 15:33:53 +09002861 switch (si_shader_ctx.type) {
2862 case TGSI_PROCESSOR_VERTEX:
2863 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002864 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002865 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2866 } else {
2867 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2868 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002869 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002870 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002871 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2872 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002873 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002874 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002875 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2876 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002877
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002878 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002879 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2880 shader->db_shader_control |=
2881 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2882 break;
2883 case TGSI_FS_DEPTH_LAYOUT_LESS:
2884 shader->db_shader_control |=
2885 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2886 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002887 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002888 break;
2889 default:
2890 assert(!"Unsupported shader type");
2891 return -1;
2892 }
2893
Christian König206f0592013-03-20 14:37:21 +01002894 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002895 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002896 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002897 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002898 preload_streamout_buffers(&si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002899 preload_ring_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002900
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002901 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2902 si_shader_ctx.gs_next_vertex =
2903 lp_build_alloca(bld_base->base.gallivm,
2904 bld_base->uint_bld.elem_type, "");
2905 }
2906
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002907 if (!lp_build_tgsi_llvm(bld_base, tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002908 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002909 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002910 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002911
2912 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2913
2914 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002915 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002916 if (r) {
2917 fprintf(stderr, "LLVM failed to compile shader\n");
2918 goto out;
2919 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002920
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002921 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002922
2923 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002924 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002925 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002926 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002927 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002928 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2929 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002930 free(shader->gs_copy_shader);
2931 shader->gs_copy_shader = NULL;
2932 goto out;
2933 }
2934 }
2935
Michel Dänzer404b29d2013-11-21 16:45:28 +09002936out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002937 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002938 FREE(si_shader_ctx.constants[i]);
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002939 if (poly_stipple)
2940 tgsi_free_tokens(tokens);
Tom Stellard302f53d2012-10-25 13:50:10 -04002941 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002942}
2943
Marek Olšák2774abd2014-09-16 18:45:33 +02002944void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002945{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002946 if (shader->gs_copy_shader)
2947 si_shader_destroy(ctx, shader->gs_copy_shader);
2948
Tom Stellard5dcd97f2015-01-27 16:35:21 +00002949 if (shader->scratch_bo)
2950 r600_resource_reference(&shader->scratch_bo, NULL);
2951
Marek Olšáka81c3e02013-08-14 01:04:39 +02002952 r600_resource_reference(&shader->bo, NULL);
Tom Stellard2397a722014-12-10 09:13:59 -05002953
2954 FREE(shader->binary.code);
2955 FREE(shader->binary.relocs);
Tom Stellarda75c6162012-01-06 17:38:37 -05002956}