blob: e70a3181bfeec746b42232d3b66adc4d863c5641 [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
Marek Olšákca90cde2015-02-28 00:30:26 +0100646 case TGSI_SEMANTIC_SAMPLEMASK:
647 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_SAMPLE_COVERAGE);
648 break;
649
Christian Könige4ed5872013-03-21 18:02:52 +0100650 default:
651 assert(!"unknown system value");
652 return;
653 }
654
655 radeon_bld->system_values[index] = value;
656}
657
Tom Stellarda75c6162012-01-06 17:38:37 -0500658static LLVMValueRef fetch_constant(
659 struct lp_build_tgsi_context * bld_base,
660 const struct tgsi_full_src_register *reg,
661 enum tgsi_opcode_type type,
662 unsigned swizzle)
663{
Christian König55fe5cc2013-03-04 16:30:06 +0100664 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500665 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100666 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200667 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500668
Christian König0f6cf2b2013-03-15 15:53:25 +0100669 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100670 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500671
Christian König8514f5a2013-02-04 17:46:42 +0100672 if (swizzle == LP_CHAN_ALL) {
673 unsigned chan;
674 LLVMValueRef values[4];
675 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
676 values[chan] = fetch_constant(bld_base, reg, type, chan);
677
678 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
679 }
680
Marek Olšák2fd42002013-10-25 11:45:47 +0200681 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100682 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100683
Marek Olšák2fd42002013-10-25 11:45:47 +0200684 if (!reg->Register.Indirect)
685 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
686
Christian König0f6cf2b2013-03-15 15:53:25 +0100687 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
688 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
689 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200690 addr = lp_build_add(&bld_base->uint_bld, addr,
691 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200692
Marek Olšákd7876082014-09-26 23:06:32 +0200693 result = buffer_load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
Marek Olšák250aa932014-05-06 14:10:47 +0200694 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500695
Christian Königf5298b02013-02-28 14:50:07 +0100696 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500697}
698
Michel Dänzer26c71392012-08-24 12:03:11 +0200699/* Initialize arguments for the shader export intrinsic */
700static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900701 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200702 unsigned target,
703 LLVMValueRef *args)
704{
705 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
706 struct lp_build_context *uint =
707 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
708 struct lp_build_context *base = &bld_base->base;
709 unsigned compressed = 0;
710 unsigned chan;
711
Michel Dänzerf402acd2012-08-22 18:15:36 +0200712 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
713 int cbuf = target - V_008DFC_SQ_EXP_MRT;
714
715 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100716 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100717
718 if (compressed)
719 si_shader_ctx->shader->spi_shader_col_format |=
720 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
721 else
722 si_shader_ctx->shader->spi_shader_col_format |=
723 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200724
725 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200726 }
727 }
728
729 if (compressed) {
730 /* Pixel shader needs to pack output values before export */
731 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900732 args[0] = values[2 * chan];
733 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200734 args[chan + 5] =
735 build_intrinsic(base->gallivm->builder,
736 "llvm.SI.packf16",
737 LLVMInt32TypeInContext(base->gallivm->context),
738 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100739 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100740 args[chan + 7] = args[chan + 5] =
741 LLVMBuildBitCast(base->gallivm->builder,
742 args[chan + 5],
743 LLVMFloatTypeInContext(base->gallivm->context),
744 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200745 }
746
747 /* Set COMPR flag */
748 args[4] = uint->one;
749 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900750 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200751 /* +5 because the first output value will be
752 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900753 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200754
755 /* Clear COMPR flag */
756 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200757 }
758
759 /* XXX: This controls which components of the output
760 * registers actually get exported. (e.g bit 0 means export
761 * X component, bit 1 means export Y component, etc.) I'm
762 * hard coding this to 0xf for now. In the future, we might
763 * want to do something else. */
764 args[0] = lp_build_const_int32(base->gallivm, 0xf);
765
766 /* Specify whether the EXEC mask represents the valid mask */
767 args[1] = uint->zero;
768
769 /* Specify whether this is the last export */
770 args[2] = uint->zero;
771
772 /* Specify the target we are exporting */
773 args[3] = lp_build_const_int32(base->gallivm, target);
774
Michel Dänzer26c71392012-08-24 12:03:11 +0200775 /* XXX: We probably need to keep track of the output
776 * values, so we know what we are passing to the next
777 * stage. */
778}
779
Michel Dänzer404b29d2013-11-21 16:45:28 +0900780/* Load from output pointers and initialize arguments for the shader export intrinsic */
781static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
782 LLVMValueRef *out_ptr,
783 unsigned target,
784 LLVMValueRef *args)
785{
786 struct gallivm_state *gallivm = bld_base->base.gallivm;
787 LLVMValueRef values[4];
788 int i;
789
790 for (i = 0; i < 4; i++)
791 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
792
793 si_llvm_init_export_args(bld_base, values, target, args);
794}
795
Michel Dänzer7708a862012-11-02 15:57:30 +0100796static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900797 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100798{
799 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
800 struct gallivm_state *gallivm = bld_base->base.gallivm;
801
Christian Königa0dca442013-03-22 15:59:22 +0100802 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400803 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
804 SI_PARAM_ALPHA_REF);
805
Michel Dänzer7708a862012-11-02 15:57:30 +0100806 LLVMValueRef alpha_pass =
807 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100808 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900809 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400810 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100811 LLVMValueRef arg =
812 lp_build_select(&bld_base->base,
813 alpha_pass,
814 lp_build_const_float(gallivm, 1.0f),
815 lp_build_const_float(gallivm, -1.0f));
816
817 build_intrinsic(gallivm->builder,
818 "llvm.AMDGPU.kill",
819 LLVMVoidTypeInContext(gallivm->context),
820 &arg, 1, 0);
821 } else {
822 build_intrinsic(gallivm->builder,
823 "llvm.AMDGPU.kilp",
824 LLVMVoidTypeInContext(gallivm->context),
825 NULL, 0, 0);
826 }
Marek Olšákadc57972014-09-19 17:07:07 +0200827
828 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100829}
830
Michel Dänzere3befbc2013-05-15 18:09:50 +0200831static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900832 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200833{
834 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
835 struct lp_build_context *base = &bld_base->base;
836 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200837 unsigned reg_index;
838 unsigned chan;
839 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200840 LLVMValueRef base_elt;
841 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200842 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200843 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200844
Michel Dänzere3befbc2013-05-15 18:09:50 +0200845 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200846 LLVMValueRef *args = pos[2 + reg_index];
847
Michel Dänzere3befbc2013-05-15 18:09:50 +0200848 args[5] =
849 args[6] =
850 args[7] =
851 args[8] = lp_build_const_float(base->gallivm, 0.0f);
852
853 /* Compute dot products of position and user clip plane vectors */
854 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
855 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200856 args[1] = lp_build_const_int32(base->gallivm,
857 ((reg_index * 4 + chan) * 4 +
858 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200859 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200860 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200861 args[5 + chan] =
862 lp_build_add(base, args[5 + chan],
863 lp_build_mul(base, base_elt,
864 out_elts[const_chan]));
865 }
866 }
867
868 args[0] = lp_build_const_int32(base->gallivm, 0xf);
869 args[1] = uint->zero;
870 args[2] = uint->zero;
871 args[3] = lp_build_const_int32(base->gallivm,
872 V_008DFC_SQ_EXP_POS + 2 + reg_index);
873 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200874 }
875}
876
Marek Olšák8d03d922013-09-01 23:59:06 +0200877static void si_dump_streamout(struct pipe_stream_output_info *so)
878{
879 unsigned i;
880
881 if (so->num_outputs)
882 fprintf(stderr, "STREAMOUT\n");
883
884 for (i = 0; i < so->num_outputs; i++) {
885 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
886 so->output[i].start_component;
887 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
888 i, so->output[i].output_buffer,
889 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
890 so->output[i].register_index,
891 mask & 1 ? "x" : "",
892 mask & 2 ? "y" : "",
893 mask & 4 ? "z" : "",
894 mask & 8 ? "w" : "");
895 }
896}
897
898/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
899 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
900 * or v4i32 (num_channels=3,4). */
901static void build_tbuffer_store(struct si_shader_context *shader,
902 LLVMValueRef rsrc,
903 LLVMValueRef vdata,
904 unsigned num_channels,
905 LLVMValueRef vaddr,
906 LLVMValueRef soffset,
907 unsigned inst_offset,
908 unsigned dfmt,
909 unsigned nfmt,
910 unsigned offen,
911 unsigned idxen,
912 unsigned glc,
913 unsigned slc,
914 unsigned tfe)
915{
916 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
917 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
918 LLVMValueRef args[] = {
919 rsrc,
920 vdata,
921 LLVMConstInt(i32, num_channels, 0),
922 vaddr,
923 soffset,
924 LLVMConstInt(i32, inst_offset, 0),
925 LLVMConstInt(i32, dfmt, 0),
926 LLVMConstInt(i32, nfmt, 0),
927 LLVMConstInt(i32, offen, 0),
928 LLVMConstInt(i32, idxen, 0),
929 LLVMConstInt(i32, glc, 0),
930 LLVMConstInt(i32, slc, 0),
931 LLVMConstInt(i32, tfe, 0)
932 };
933
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900934 /* The instruction offset field has 12 bits */
935 assert(offen || inst_offset < (1 << 12));
936
Marek Olšák8d03d922013-09-01 23:59:06 +0200937 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
938 unsigned func = CLAMP(num_channels, 1, 3) - 1;
939 const char *types[] = {"i32", "v2i32", "v4i32"};
940 char name[256];
941 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
942
943 lp_build_intrinsic(gallivm->builder, name,
944 LLVMVoidTypeInContext(gallivm->context),
945 args, Elements(args));
946}
947
948static void build_streamout_store(struct si_shader_context *shader,
949 LLVMValueRef rsrc,
950 LLVMValueRef vdata,
951 unsigned num_channels,
952 LLVMValueRef vaddr,
953 LLVMValueRef soffset,
954 unsigned inst_offset)
955{
956 static unsigned dfmt[] = {
957 V_008F0C_BUF_DATA_FORMAT_32,
958 V_008F0C_BUF_DATA_FORMAT_32_32,
959 V_008F0C_BUF_DATA_FORMAT_32_32_32,
960 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
961 };
962 assert(num_channels >= 1 && num_channels <= 4);
963
964 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
965 inst_offset, dfmt[num_channels-1],
966 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
967}
968
969/* On SI, the vertex shader is responsible for writing streamout data
970 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900971static void si_llvm_emit_streamout(struct si_shader_context *shader,
972 struct si_shader_output_values *outputs,
973 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200974{
975 struct pipe_stream_output_info *so = &shader->shader->selector->so;
976 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
977 LLVMBuilderRef builder = gallivm->builder;
978 int i, j;
979 struct lp_build_if_state if_ctx;
980
981 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
982
983 LLVMValueRef so_param =
984 LLVMGetParam(shader->radeon_bld.main_fn,
985 shader->param_streamout_config);
986
987 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
988 LLVMValueRef so_vtx_count =
989 LLVMBuildAnd(builder,
990 LLVMBuildLShr(builder, so_param,
991 LLVMConstInt(i32, 16, 0), ""),
992 LLVMConstInt(i32, 127, 0), "");
993
994 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
995 NULL, 0, LLVMReadNoneAttribute);
996
997 /* can_emit = tid < so_vtx_count; */
998 LLVMValueRef can_emit =
999 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
1000
1001 /* Emit the streamout code conditionally. This actually avoids
1002 * out-of-bounds buffer access. The hw tells us via the SGPR
1003 * (so_vtx_count) which threads are allowed to emit streamout data. */
1004 lp_build_if(&if_ctx, gallivm, can_emit);
1005 {
1006 /* The buffer offset is computed as follows:
1007 * ByteOffset = streamout_offset[buffer_id]*4 +
1008 * (streamout_write_index + thread_id)*stride[buffer_id] +
1009 * attrib_offset
1010 */
1011
1012 LLVMValueRef so_write_index =
1013 LLVMGetParam(shader->radeon_bld.main_fn,
1014 shader->param_streamout_write_index);
1015
1016 /* Compute (streamout_write_index + thread_id). */
1017 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1018
1019 /* Compute the write offset for each enabled buffer. */
1020 LLVMValueRef so_write_offset[4] = {};
1021 for (i = 0; i < 4; i++) {
1022 if (!so->stride[i])
1023 continue;
1024
1025 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1026 shader->param_streamout_offset[i]);
1027 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1028
1029 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1030 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1031 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1032 }
1033
Marek Olšák8d03d922013-09-01 23:59:06 +02001034 /* Write streamout data. */
1035 for (i = 0; i < so->num_outputs; i++) {
1036 unsigned buf_idx = so->output[i].output_buffer;
1037 unsigned reg = so->output[i].register_index;
1038 unsigned start = so->output[i].start_component;
1039 unsigned num_comps = so->output[i].num_components;
1040 LLVMValueRef out[4];
1041
1042 assert(num_comps && num_comps <= 4);
1043 if (!num_comps || num_comps > 4)
1044 continue;
1045
Marek Olšák558f7772014-10-04 22:37:23 +02001046 if (reg >= noutput)
1047 continue;
1048
Marek Olšák8d03d922013-09-01 23:59:06 +02001049 /* Load the output as int. */
1050 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001051 out[j] = LLVMBuildBitCast(builder,
1052 outputs[reg].values[start+j],
1053 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001054 }
1055
1056 /* Pack the output. */
1057 LLVMValueRef vdata = NULL;
1058
1059 switch (num_comps) {
1060 case 1: /* as i32 */
1061 vdata = out[0];
1062 break;
1063 case 2: /* as v2i32 */
1064 case 3: /* as v4i32 (aligned to 4) */
1065 case 4: /* as v4i32 */
1066 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1067 for (j = 0; j < num_comps; j++) {
1068 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1069 LLVMConstInt(i32, j, 0), "");
1070 }
1071 break;
1072 }
1073
1074 build_streamout_store(shader, shader->so_buffers[buf_idx],
1075 vdata, num_comps,
1076 so_write_offset[buf_idx],
1077 LLVMConstInt(i32, 0, 0),
1078 so->output[i].dst_offset*4);
1079 }
1080 }
1081 lp_build_endif(&if_ctx);
1082}
1083
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001084
Michel Dänzer404b29d2013-11-21 16:45:28 +09001085/* Generate export instructions for hardware VS shader stage */
1086static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1087 struct si_shader_output_values *outputs,
1088 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001089{
1090 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001091 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001092 struct lp_build_context * base = &bld_base->base;
1093 struct lp_build_context * uint =
1094 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001095 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001096 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001097 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001098 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001099 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001100 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001101 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001102 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001103
Michel Dänzer67e385b2014-01-08 17:48:21 +09001104 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1105 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001106 }
1107
Michel Dänzer404b29d2013-11-21 16:45:28 +09001108 for (i = 0; i < noutput; i++) {
1109 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001110 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001111
Michel Dänzer0afeea52013-05-02 14:53:17 +02001112handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001113 /* Select the correct target */
1114 switch(semantic_name) {
1115 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001116 psize_value = outputs[i].values[0];
1117 continue;
1118 case TGSI_SEMANTIC_EDGEFLAG:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001119 edgeflag_value = outputs[i].values[0];
1120 continue;
1121 case TGSI_SEMANTIC_LAYER:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001122 layer_value = outputs[i].values[0];
1123 continue;
1124 case TGSI_SEMANTIC_POSITION:
1125 target = V_008DFC_SQ_EXP_POS;
1126 break;
1127 case TGSI_SEMANTIC_COLOR:
1128 case TGSI_SEMANTIC_BCOLOR:
1129 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001130 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001131 param_count++;
1132 break;
1133 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001134 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1135 break;
1136 case TGSI_SEMANTIC_CLIPVERTEX:
1137 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1138 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001139 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001140 case TGSI_SEMANTIC_FOG:
1141 case TGSI_SEMANTIC_GENERIC:
1142 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001143 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001144 param_count++;
1145 break;
1146 default:
1147 target = 0;
1148 fprintf(stderr,
1149 "Warning: SI unhandled vs output type:%d\n",
1150 semantic_name);
1151 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001152
Michel Dänzer404b29d2013-11-21 16:45:28 +09001153 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001154
Michel Dänzer404b29d2013-11-21 16:45:28 +09001155 if (target >= V_008DFC_SQ_EXP_POS &&
1156 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1157 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1158 args, sizeof(args));
1159 } else {
1160 lp_build_intrinsic(base->gallivm->builder,
1161 "llvm.SI.export",
1162 LLVMVoidTypeInContext(base->gallivm->context),
1163 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001164 }
1165
1166 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1167 semantic_name = TGSI_SEMANTIC_GENERIC;
1168 goto handle_semantic;
1169 }
1170 }
1171
1172 /* We need to add the position output manually if it's missing. */
1173 if (!pos_args[0][0]) {
1174 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1175 pos_args[0][1] = uint->zero; /* EXEC mask */
1176 pos_args[0][2] = uint->zero; /* last export? */
1177 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1178 pos_args[0][4] = uint->zero; /* COMPR flag */
1179 pos_args[0][5] = base->zero; /* X */
1180 pos_args[0][6] = base->zero; /* Y */
1181 pos_args[0][7] = base->zero; /* Z */
1182 pos_args[0][8] = base->one; /* W */
1183 }
1184
1185 /* Write the misc vector (point size, edgeflag, layer, viewport). */
Marek Olšák16153472014-12-07 19:30:08 +01001186 if (shader->selector->info.writes_psize ||
1187 shader->selector->info.writes_edgeflag ||
1188 shader->selector->info.writes_layer) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001189 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
Marek Olšák16153472014-12-07 19:30:08 +01001190 shader->selector->info.writes_psize |
1191 (shader->selector->info.writes_edgeflag << 1) |
1192 (shader->selector->info.writes_layer << 2));
Michel Dänzer51f89a02013-12-09 15:33:53 +09001193 pos_args[1][1] = uint->zero; /* EXEC mask */
1194 pos_args[1][2] = uint->zero; /* last export? */
1195 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1196 pos_args[1][4] = uint->zero; /* COMPR flag */
1197 pos_args[1][5] = base->zero; /* X */
1198 pos_args[1][6] = base->zero; /* Y */
1199 pos_args[1][7] = base->zero; /* Z */
1200 pos_args[1][8] = base->zero; /* W */
1201
Marek Olšák16153472014-12-07 19:30:08 +01001202 if (shader->selector->info.writes_psize)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001203 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001204
Marek Olšák16153472014-12-07 19:30:08 +01001205 if (shader->selector->info.writes_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001206 /* The output is a float, but the hw expects an integer
1207 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001208 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1209 edgeflag_value,
1210 bld_base->uint_bld.elem_type, "");
1211 edgeflag_value = lp_build_min(&bld_base->int_bld,
1212 edgeflag_value,
1213 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001214
1215 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001216 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1217 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001218 base->elem_type, "");
1219 }
1220
Marek Olšák16153472014-12-07 19:30:08 +01001221 if (shader->selector->info.writes_layer)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001222 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001223 }
1224
1225 for (i = 0; i < 4; i++)
1226 if (pos_args[i][0])
1227 shader->nr_pos_exports++;
1228
1229 pos_idx = 0;
1230 for (i = 0; i < 4; i++) {
1231 if (!pos_args[i][0])
1232 continue;
1233
1234 /* Specify the target we are exporting */
1235 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1236
1237 if (pos_idx == shader->nr_pos_exports)
1238 /* Specify that this is the last export */
1239 pos_args[i][2] = uint->one;
1240
1241 lp_build_intrinsic(base->gallivm->builder,
1242 "llvm.SI.export",
1243 LLVMVoidTypeInContext(base->gallivm->context),
1244 pos_args[i], 9);
1245 }
1246}
1247
Michel Dänzer404b29d2013-11-21 16:45:28 +09001248static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1249{
1250 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1251 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001252 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001253 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001254 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001255 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1256 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001257 unsigned chan;
1258 int i;
1259
Marek Olšák216cf862014-10-04 17:04:05 +02001260 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001261 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001262 si_shader_ctx->radeon_bld.soa.outputs[i];
1263 int param_index = get_param_index(info->output_semantic_name[i],
1264 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001265 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001266
Marek Olšáke29353f2014-09-17 22:17:02 +02001267 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001268 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001269
1270 for (chan = 0; chan < 4; chan++) {
1271 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001272 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1273
Marek Olšákfc3b3352014-10-05 13:33:40 +02001274 build_tbuffer_store(si_shader_ctx,
1275 si_shader_ctx->esgs_ring,
1276 out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001277 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001278 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001279 V_008F0C_BUF_DATA_FORMAT_32,
1280 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001281 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001282 }
1283 }
1284}
1285
1286static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1287{
1288 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1289 struct gallivm_state *gallivm = bld_base->base.gallivm;
1290 LLVMValueRef args[2];
1291
1292 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1293 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1294 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1295 LLVMVoidTypeInContext(gallivm->context), args, 2,
1296 LLVMNoUnwindAttribute);
1297}
1298
1299static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1300{
1301 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1302 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001303 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001304 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001305 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001306
Marek Olšákec0d1682014-10-04 22:33:36 +02001307 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001308
Marek Olšákec0d1682014-10-04 22:33:36 +02001309 for (i = 0; i < info->num_outputs; i++) {
1310 outputs[i].name = info->output_semantic_name[i];
1311 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001312
Marek Olšákec0d1682014-10-04 22:33:36 +02001313 for (j = 0; j < 4; j++)
1314 outputs[i].values[j] =
1315 LLVMBuildLoad(gallivm->builder,
1316 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1317 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001318 }
1319
Marek Olšákec0d1682014-10-04 22:33:36 +02001320 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001321 FREE(outputs);
1322}
1323
Michel Dänzer51f89a02013-12-09 15:33:53 +09001324static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1325{
1326 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001327 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001328 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001329 struct lp_build_context * uint = &bld_base->uint_bld;
1330 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001331 LLVMValueRef args[9];
1332 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001333 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001334 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001335
Marek Olšák75e97e22014-10-04 23:13:50 +02001336 for (i = 0; i < info->num_outputs; i++) {
1337 unsigned semantic_name = info->output_semantic_name[i];
1338 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001339 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001340
Marek Olšák75e97e22014-10-04 23:13:50 +02001341 /* Select the correct target */
1342 switch (semantic_name) {
1343 case TGSI_SEMANTIC_POSITION:
1344 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001345 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001346 case TGSI_SEMANTIC_STENCIL:
1347 stencil_index = i;
1348 continue;
1349 case TGSI_SEMANTIC_SAMPLEMASK:
1350 samplemask_index = i;
1351 continue;
1352 case TGSI_SEMANTIC_COLOR:
1353 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1354 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1355 LLVMBuildStore(bld_base->base.gallivm->builder,
1356 bld_base->base.one,
1357 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001358
Marek Olšák75e97e22014-10-04 23:13:50 +02001359 if (semantic_index == 0 &&
1360 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1361 si_alpha_test(bld_base,
1362 si_shader_ctx->radeon_bld.soa.outputs[i]);
1363 break;
1364 default:
1365 target = 0;
1366 fprintf(stderr,
1367 "Warning: SI unhandled fs output type:%d\n",
1368 semantic_name);
1369 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001370
Marek Olšák75e97e22014-10-04 23:13:50 +02001371 si_llvm_init_export_args_load(bld_base,
1372 si_shader_ctx->radeon_bld.soa.outputs[i],
1373 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001374
Marek Olšák75e97e22014-10-04 23:13:50 +02001375 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1376 /* If there is an export instruction waiting to be emitted, do so now. */
1377 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001378 lp_build_intrinsic(base->gallivm->builder,
1379 "llvm.SI.export",
1380 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001381 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001382 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001383
1384 /* This instruction will be emitted at the end of the shader. */
1385 memcpy(last_args, args, sizeof(args));
1386
1387 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1388 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1389 semantic_index == 0 &&
1390 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1391 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1392 si_llvm_init_export_args_load(bld_base,
1393 si_shader_ctx->radeon_bld.soa.outputs[i],
1394 V_008DFC_SQ_EXP_MRT + c, args);
1395 lp_build_intrinsic(base->gallivm->builder,
1396 "llvm.SI.export",
1397 LLVMVoidTypeInContext(base->gallivm->context),
1398 args, 9);
1399 }
1400 }
1401 } else {
1402 lp_build_intrinsic(base->gallivm->builder,
1403 "llvm.SI.export",
1404 LLVMVoidTypeInContext(base->gallivm->context),
1405 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001406 }
1407 }
1408
Marek Olšákd0e8b652014-05-06 20:04:31 +02001409 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001410 LLVMValueRef out_ptr;
1411 unsigned mask = 0;
1412
1413 /* Specify the target we are exporting */
1414 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1415
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001416 args[5] = base->zero; /* R, depth */
1417 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1418 args[7] = base->zero; /* B, sample mask */
1419 args[8] = base->zero; /* A, alpha to mask */
1420
Michel Dänzer1a616c12012-11-13 17:35:09 +01001421 if (depth_index >= 0) {
1422 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1423 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1424 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001425 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001426 }
1427
1428 if (stencil_index >= 0) {
1429 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001430 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001431 mask |= 0x2;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001432 si_shader_ctx->shader->db_shader_control |=
1433 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001434 }
1435
Marek Olšákd0e8b652014-05-06 20:04:31 +02001436 if (samplemask_index >= 0) {
1437 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1438 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001439 mask |= 0x4;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001440 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1441 }
1442
Marek Olšák368b0a72014-12-28 21:51:35 +01001443 /* SI (except OLAND) has a bug that it only looks
1444 * at the X writemask component. */
1445 if (si_shader_ctx->screen->b.chip_class == SI &&
1446 si_shader_ctx->screen->b.family != CHIP_OLAND)
1447 mask |= 0x1;
1448
Marek Olšákd0e8b652014-05-06 20:04:31 +02001449 if (samplemask_index >= 0)
1450 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1451 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001452 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1453 else
1454 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1455
Michel Dänzer1a616c12012-11-13 17:35:09 +01001456 /* Specify which components to enable */
1457 args[0] = lp_build_const_int32(base->gallivm, mask);
1458
1459 args[1] =
1460 args[2] =
1461 args[4] = uint->zero;
1462
1463 if (last_args[0])
1464 lp_build_intrinsic(base->gallivm->builder,
1465 "llvm.SI.export",
1466 LLVMVoidTypeInContext(base->gallivm->context),
1467 args, 9);
1468 else
1469 memcpy(last_args, args, sizeof(args));
1470 }
1471
Michel Dänzer51f89a02013-12-09 15:33:53 +09001472 if (!last_args[0]) {
1473 /* Specify which components to enable */
1474 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001475
Michel Dänzer51f89a02013-12-09 15:33:53 +09001476 /* Specify the target we are exporting */
1477 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001478
Michel Dänzer51f89a02013-12-09 15:33:53 +09001479 /* Set COMPR flag to zero to export data as 32-bit */
1480 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001481
Michel Dänzer51f89a02013-12-09 15:33:53 +09001482 /* dummy bits */
1483 last_args[5]= uint->zero;
1484 last_args[6]= uint->zero;
1485 last_args[7]= uint->zero;
1486 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001487 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001488
1489 /* Specify whether the EXEC mask represents the valid mask */
1490 last_args[1] = uint->one;
1491
1492 /* Specify that this is the last export */
1493 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1494
1495 lp_build_intrinsic(base->gallivm->builder,
1496 "llvm.SI.export",
1497 LLVMVoidTypeInContext(base->gallivm->context),
1498 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001499}
1500
Marek Olšák4855acd2013-08-06 15:08:54 +02001501static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1502 struct lp_build_tgsi_context * bld_base,
1503 struct lp_build_emit_data * emit_data);
1504
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001505static bool tgsi_is_shadow_sampler(unsigned target)
1506{
1507 return target == TGSI_TEXTURE_SHADOW1D ||
1508 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1509 target == TGSI_TEXTURE_SHADOW2D ||
1510 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1511 target == TGSI_TEXTURE_SHADOWCUBE ||
1512 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1513 target == TGSI_TEXTURE_SHADOWRECT;
1514}
1515
Marek Olšák877bb522014-06-25 03:12:46 +02001516static const struct lp_build_tgsi_action tex_action;
1517
Tom Stellarda75c6162012-01-06 17:38:37 -05001518static void tex_fetch_args(
1519 struct lp_build_tgsi_context * bld_base,
1520 struct lp_build_emit_data * emit_data)
1521{
Christian König55fe5cc2013-03-04 16:30:06 +01001522 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001523 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001524 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001525 unsigned opcode = inst->Instruction.Opcode;
1526 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001527 LLVMValueRef coords[4];
1528 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001529 int ref_pos;
1530 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001531 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001532 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001533 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1534 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001535 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001536
1537 if (target == TGSI_TEXTURE_BUFFER) {
1538 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1539 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1540 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1541 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1542
Marek Olšák4f3f0432014-07-07 22:49:15 +02001543 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001544 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1545 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
Marek Olšákb142dd22015-02-01 16:58:08 +01001546 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.one, "");
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001547 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1548
1549 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1550 emit_data->args[0] = res;
1551 emit_data->args[1] = bld_base->uint_bld.zero;
1552 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1553 emit_data->arg_count = 3;
1554 return;
1555 }
Tom Stellard467f5162012-05-16 15:15:35 -04001556
Michel Dänzer120efee2013-01-25 12:10:11 +01001557 /* Fetch and project texture coordinates */
1558 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001559 for (chan = 0; chan < 3; chan++ ) {
1560 coords[chan] = lp_build_emit_fetch(bld_base,
1561 emit_data->inst, 0,
1562 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001563 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001564 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1565 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001566 coords[chan],
1567 coords[3]);
1568 }
1569
Michel Dänzer120efee2013-01-25 12:10:11 +01001570 if (opcode == TGSI_OPCODE_TXP)
1571 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001572
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001573 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001574 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001575 /* The offsets are six-bit signed integers packed like this:
1576 * X=[5:0], Y=[13:8], and Z=[21:16].
1577 */
1578 LLVMValueRef offset[3], pack;
1579
1580 assert(inst->Texture.NumOffsets == 1);
1581
1582 for (chan = 0; chan < 3; chan++) {
1583 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1584 emit_data->inst, 0, chan);
1585 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1586 lp_build_const_int32(gallivm, 0x3f), "");
1587 if (chan)
1588 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1589 lp_build_const_int32(gallivm, chan*8), "");
1590 }
1591
1592 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1593 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1594 address[count++] = pack;
1595 }
1596
Michel Dänzer120efee2013-01-25 12:10:11 +01001597 /* Pack LOD bias value */
1598 if (opcode == TGSI_OPCODE_TXB)
1599 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001600 if (opcode == TGSI_OPCODE_TXB2)
1601 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001602
Michel Dänzer120efee2013-01-25 12:10:11 +01001603 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001604 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001605 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1606 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1607 } else {
1608 assert(ref_pos >= 0);
1609 address[count++] = coords[ref_pos];
1610 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001611 }
1612
Marek Olšákb279f0142014-07-04 03:19:58 +02001613 if (target == TGSI_TEXTURE_CUBE ||
1614 target == TGSI_TEXTURE_CUBE_ARRAY ||
1615 target == TGSI_TEXTURE_SHADOWCUBE ||
1616 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1617 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1618
Michel Dänzera6b83c02013-02-21 16:10:55 +01001619 /* Pack user derivatives */
1620 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001621 int num_deriv_channels, param;
1622
1623 switch (target) {
1624 case TGSI_TEXTURE_3D:
1625 num_deriv_channels = 3;
1626 break;
1627 case TGSI_TEXTURE_2D:
1628 case TGSI_TEXTURE_SHADOW2D:
1629 case TGSI_TEXTURE_RECT:
1630 case TGSI_TEXTURE_SHADOWRECT:
1631 case TGSI_TEXTURE_2D_ARRAY:
1632 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1633 case TGSI_TEXTURE_CUBE:
1634 case TGSI_TEXTURE_SHADOWCUBE:
1635 case TGSI_TEXTURE_CUBE_ARRAY:
1636 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1637 num_deriv_channels = 2;
1638 break;
1639 case TGSI_TEXTURE_1D:
1640 case TGSI_TEXTURE_SHADOW1D:
1641 case TGSI_TEXTURE_1D_ARRAY:
1642 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1643 num_deriv_channels = 1;
1644 break;
1645 default:
1646 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001647 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001648
1649 for (param = 1; param <= 2; param++)
1650 for (chan = 0; chan < num_deriv_channels; chan++)
1651 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001652 }
1653
Michel Dänzer120efee2013-01-25 12:10:11 +01001654 /* Pack texture coordinates */
1655 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001656 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001657 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001658 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001659 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001660
Marek Olšákd2bd6342013-09-18 15:40:21 +02001661 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001662 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001663 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001664 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001665 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001666
1667 if (count > 16) {
1668 assert(!"Cannot handle more than 16 texture address parameters");
1669 count = 16;
1670 }
1671
1672 for (chan = 0; chan < count; chan++ ) {
1673 address[chan] = LLVMBuildBitCast(gallivm->builder,
1674 address[chan],
1675 LLVMInt32TypeInContext(gallivm->context),
1676 "");
1677 }
1678
Marek Olšák4855acd2013-08-06 15:08:54 +02001679 /* Adjust the sample index according to FMASK.
1680 *
1681 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1682 * which is the identity mapping. Each nibble says which physical sample
1683 * should be fetched to get that sample.
1684 *
1685 * For example, 0x11111100 means there are only 2 samples stored and
1686 * the second sample covers 3/4 of the pixel. When reading samples 0
1687 * and 1, return physical sample 0 (determined by the first two 0s
1688 * in FMASK), otherwise return physical sample 1.
1689 *
1690 * The sample index should be adjusted as follows:
1691 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1692 */
1693 if (target == TGSI_TEXTURE_2D_MSAA ||
1694 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1695 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1696 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001697 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001698 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001699 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001700
Marek Olšákd2bd6342013-09-18 15:40:21 +02001701 memcpy(txf_address, address, sizeof(txf_address));
1702
1703 if (target == TGSI_TEXTURE_2D_MSAA) {
1704 txf_address[2] = bld_base->uint_bld.zero;
1705 }
1706 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001707
1708 /* Pad to a power-of-two size. */
1709 while (txf_count < util_next_power_of_two(txf_count))
1710 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1711
1712 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001713 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1714 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1715 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001716 txf_emit_data.chan = 0;
1717 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001718 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001719 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001720 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001721 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001722 txf_emit_data.arg_count = 3;
1723
Marek Olšák877bb522014-06-25 03:12:46 +02001724 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001725
1726 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001727 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1728 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1729
1730 /* Apply the formula. */
1731 LLVMValueRef fmask =
1732 LLVMBuildExtractElement(gallivm->builder,
1733 txf_emit_data.output[0],
1734 uint_bld->zero, "");
1735
Marek Olšákd2bd6342013-09-18 15:40:21 +02001736 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001737
1738 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001739 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001740
1741 LLVMValueRef shifted_fmask =
1742 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1743
1744 LLVMValueRef final_sample =
1745 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1746
1747 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1748 * resource descriptor is 0 (invalid),
1749 */
1750 LLVMValueRef fmask_desc =
1751 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001752 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001753 LLVMVectorType(uint_bld->elem_type, 8), "");
1754
1755 LLVMValueRef fmask_word1 =
1756 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1757 uint_bld->one, "");
1758
1759 LLVMValueRef word1_is_nonzero =
1760 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1761 fmask_word1, uint_bld->zero, "");
1762
Marek Olšákd2bd6342013-09-18 15:40:21 +02001763 /* Replace the MSAA sample index. */
1764 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001765 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001766 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001767 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001768
Michel Dänzer36231112013-05-02 09:44:45 +02001769 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001770 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001771
1772 if (opcode == TGSI_OPCODE_TXF) {
1773 /* add tex offsets */
1774 if (inst->Texture.NumOffsets) {
1775 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1776 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1777 const struct tgsi_texture_offset * off = inst->TexOffsets;
1778
1779 assert(inst->Texture.NumOffsets == 1);
1780
Marek Olšákdefedc02013-09-18 15:36:38 +02001781 switch (target) {
1782 case TGSI_TEXTURE_3D:
1783 address[2] = lp_build_add(uint_bld, address[2],
1784 bld->immediates[off->Index][off->SwizzleZ]);
1785 /* fall through */
1786 case TGSI_TEXTURE_2D:
1787 case TGSI_TEXTURE_SHADOW2D:
1788 case TGSI_TEXTURE_RECT:
1789 case TGSI_TEXTURE_SHADOWRECT:
1790 case TGSI_TEXTURE_2D_ARRAY:
1791 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001792 address[1] =
1793 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001794 bld->immediates[off->Index][off->SwizzleY]);
1795 /* fall through */
1796 case TGSI_TEXTURE_1D:
1797 case TGSI_TEXTURE_SHADOW1D:
1798 case TGSI_TEXTURE_1D_ARRAY:
1799 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1800 address[0] =
1801 lp_build_add(uint_bld, address[0],
1802 bld->immediates[off->Index][off->SwizzleX]);
1803 break;
1804 /* texture offsets do not apply to other texture targets */
1805 }
Michel Dänzer36231112013-05-02 09:44:45 +02001806 }
1807
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001808 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1809 emit_data->arg_count = 3;
1810
Michel Dänzer36231112013-05-02 09:44:45 +02001811 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001812 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001813 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001814 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001815 opcode == TGSI_OPCODE_LODQ ||
1816 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001817 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1818 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1819 target == TGSI_TEXTURE_2D_ARRAY ||
1820 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1821 target == TGSI_TEXTURE_CUBE_ARRAY ||
1822 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1823 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001824 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001825
Marek Olšák57f3da92014-06-16 16:53:42 +02001826 if (opcode == TGSI_OPCODE_TG4) {
1827 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001828
Marek Olšák57f3da92014-06-16 16:53:42 +02001829 /* DMASK was repurposed for GATHER4. 4 components are always
1830 * returned and DMASK works like a swizzle - it selects
1831 * the component to fetch. The only valid DMASK values are
1832 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1833 * (red,red,red,red) etc.) The ISA document doesn't mention
1834 * this.
1835 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001836
Marek Olšák57f3da92014-06-16 16:53:42 +02001837 /* Get the component index from src1.x for Gather4. */
1838 if (!tgsi_is_shadow_sampler(target)) {
1839 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1840 LLVMValueRef comp_imm;
1841 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001842
Marek Olšák57f3da92014-06-16 16:53:42 +02001843 assert(src1.File == TGSI_FILE_IMMEDIATE);
1844
1845 comp_imm = imms[src1.Index][src1.SwizzleX];
1846 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1847 gather_comp = CLAMP(gather_comp, 0, 3);
1848 }
1849
1850 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001851 }
1852
Marek Olšák4855acd2013-08-06 15:08:54 +02001853 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001854 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001855 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1856 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1857 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1858 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1859 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1860 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1861 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1862
1863 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001864
1865 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001866 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001867 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001868 } else {
1869 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1870 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001871 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001872
1873 emit_data->dst_type = LLVMVectorType(
1874 LLVMFloatTypeInContext(gallivm->context),
1875 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001876 }
1877
Marek Olšák2484daa2014-04-22 21:23:29 +02001878 /* The fetch opcode has been converted to a 2D array fetch.
1879 * This simplifies the LLVM backend. */
1880 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1881 target = TGSI_TEXTURE_2D_ARRAY;
1882 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1883 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1884
Michel Dänzer120efee2013-01-25 12:10:11 +01001885 /* Pad to power of two vector */
1886 while (count < util_next_power_of_two(count))
1887 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1888
Christian Königccf3e8f2013-03-26 15:09:27 +01001889 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001890}
1891
Michel Dänzer07eddc42013-02-06 15:43:10 +01001892static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1893 struct lp_build_tgsi_context * bld_base,
1894 struct lp_build_emit_data * emit_data)
1895{
1896 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001897 unsigned opcode = emit_data->inst->Instruction.Opcode;
1898 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001899 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001900 bool has_offset = HAVE_LLVM >= 0x0305 ?
1901 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001902
Marek Olšák877bb522014-06-25 03:12:46 +02001903 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001904 emit_data->output[emit_data->chan] = build_intrinsic(
1905 base->gallivm->builder,
1906 "llvm.SI.vs.load.input", emit_data->dst_type,
1907 emit_data->args, emit_data->arg_count,
1908 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1909 return;
1910 }
1911
Marek Olšák877bb522014-06-25 03:12:46 +02001912 if (opcode == TGSI_OPCODE_TG4 ||
1913 opcode == TGSI_OPCODE_LODQ ||
1914 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1915 bool is_shadow = tgsi_is_shadow_sampler(target);
1916 const char *name = "llvm.SI.image.sample";
1917 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001918
Marek Olšák877bb522014-06-25 03:12:46 +02001919 switch (opcode) {
1920 case TGSI_OPCODE_TEX:
1921 case TGSI_OPCODE_TEX2:
1922 case TGSI_OPCODE_TXP:
1923 break;
1924 case TGSI_OPCODE_TXB:
1925 case TGSI_OPCODE_TXB2:
1926 infix = ".b";
1927 break;
1928 case TGSI_OPCODE_TXL:
1929 case TGSI_OPCODE_TXL2:
1930 infix = ".l";
1931 break;
1932 case TGSI_OPCODE_TXD:
1933 infix = ".d";
1934 break;
1935 case TGSI_OPCODE_TG4:
1936 name = "llvm.SI.gather4";
1937 break;
1938 case TGSI_OPCODE_LODQ:
1939 name = "llvm.SI.getlod";
1940 is_shadow = false;
1941 has_offset = false;
1942 break;
1943 default:
1944 assert(0);
1945 return;
1946 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001947
Marek Olšák877bb522014-06-25 03:12:46 +02001948 /* Add the type and suffixes .c, .o if needed. */
1949 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1950 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1951 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001952
Marek Olšák877bb522014-06-25 03:12:46 +02001953 emit_data->output[emit_data->chan] = build_intrinsic(
1954 base->gallivm->builder, intr_name, emit_data->dst_type,
1955 emit_data->args, emit_data->arg_count,
1956 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1957 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001958 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001959 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001960
Marek Olšák877bb522014-06-25 03:12:46 +02001961 switch (opcode) {
1962 case TGSI_OPCODE_TEX:
1963 case TGSI_OPCODE_TEX2:
1964 case TGSI_OPCODE_TXP:
1965 name = "llvm.SI.sample";
1966 break;
1967 case TGSI_OPCODE_TXB:
1968 case TGSI_OPCODE_TXB2:
1969 name = "llvm.SI.sampleb";
1970 break;
1971 case TGSI_OPCODE_TXD:
1972 name = "llvm.SI.sampled";
1973 break;
1974 case TGSI_OPCODE_TXF:
1975 name = "llvm.SI.imageload";
1976 break;
1977 case TGSI_OPCODE_TXL:
1978 case TGSI_OPCODE_TXL2:
1979 name = "llvm.SI.samplel";
1980 break;
1981 default:
1982 assert(0);
1983 return;
1984 }
1985
Marek Olšákd859bdb2014-07-14 19:40:14 +02001986 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1987 v16i8 = LLVMVectorType(i8, 16);
1988 v32i8 = LLVMVectorType(i8, 32);
1989
1990 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1991 emit_data->args[1], v32i8, "");
1992 if (opcode != TGSI_OPCODE_TXF) {
1993 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1994 emit_data->args[2], v16i8, "");
1995 }
1996
Marek Olšák877bb522014-06-25 03:12:46 +02001997 sprintf(intr_name, "%s.v%ui32", name,
1998 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1999
2000 emit_data->output[emit_data->chan] = build_intrinsic(
2001 base->gallivm->builder, intr_name, emit_data->dst_type,
2002 emit_data->args, emit_data->arg_count,
2003 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2004 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002005}
2006
Michel Dänzer0495adb2013-05-06 12:45:14 +02002007static void txq_fetch_args(
2008 struct lp_build_tgsi_context * bld_base,
2009 struct lp_build_emit_data * emit_data)
2010{
2011 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2012 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002013 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002014 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002015
Marek Olšák2484daa2014-04-22 21:23:29 +02002016 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002017 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2018 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2019
2020 /* Read the size from the buffer descriptor directly. */
2021 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2022 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2023 size = LLVMBuildExtractElement(gallivm->builder, size,
Marek Olšákb142dd22015-02-01 16:58:08 +01002024 lp_build_const_int32(gallivm, 6), "");
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002025 emit_data->args[0] = size;
2026 return;
2027 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002028
2029 /* Mip level */
2030 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2031
2032 /* Resource */
2033 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2034
Marek Olšák2484daa2014-04-22 21:23:29 +02002035 /* Texture target */
2036 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2037 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2038 target = TGSI_TEXTURE_2D_ARRAY;
2039
Michel Dänzer0495adb2013-05-06 12:45:14 +02002040 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002041 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002042
2043 emit_data->arg_count = 3;
2044
2045 emit_data->dst_type = LLVMVectorType(
2046 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2047 4);
2048}
2049
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002050static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2051 struct lp_build_tgsi_context * bld_base,
2052 struct lp_build_emit_data * emit_data)
2053{
Marek Olšák2484daa2014-04-22 21:23:29 +02002054 unsigned target = emit_data->inst->Texture.Texture;
2055
2056 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002057 /* Just return the buffer size. */
2058 emit_data->output[emit_data->chan] = emit_data->args[0];
2059 return;
2060 }
2061
2062 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002063
2064 /* Divide the number of layers by 6 to get the number of cubes. */
2065 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2066 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2067 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2068 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2069 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2070
2071 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2072 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2073 z = LLVMBuildSDiv(builder, z, six, "");
2074
2075 emit_data->output[emit_data->chan] =
2076 LLVMBuildInsertElement(builder, v4, z, two, "");
2077 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002078}
2079
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002080static void si_llvm_emit_ddxy(
2081 const struct lp_build_tgsi_action * action,
2082 struct lp_build_tgsi_context * bld_base,
2083 struct lp_build_emit_data * emit_data)
2084{
2085 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2086 struct gallivm_state *gallivm = bld_base->base.gallivm;
2087 struct lp_build_context * base = &bld_base->base;
2088 const struct tgsi_full_instruction *inst = emit_data->inst;
2089 unsigned opcode = inst->Instruction.Opcode;
2090 LLVMValueRef indices[2];
2091 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2092 LLVMValueRef tl, trbl, result[4];
2093 LLVMTypeRef i32;
2094 unsigned swizzle[4];
2095 unsigned c;
2096
2097 i32 = LLVMInt32TypeInContext(gallivm->context);
2098
2099 indices[0] = bld_base->uint_bld.zero;
2100 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2101 NULL, 0, LLVMReadNoneAttribute);
2102 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2103 indices, 2, "");
2104
2105 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2106 lp_build_const_int32(gallivm, 0xfffffffc), "");
2107 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2108 indices, 2, "");
2109
2110 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2111 lp_build_const_int32(gallivm,
2112 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2113 "");
2114 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2115 indices, 2, "");
2116
2117 for (c = 0; c < 4; ++c) {
2118 unsigned i;
2119
2120 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2121 for (i = 0; i < c; ++i) {
2122 if (swizzle[i] == swizzle[c]) {
2123 result[c] = result[i];
2124 break;
2125 }
2126 }
2127 if (i != c)
2128 continue;
2129
2130 LLVMBuildStore(gallivm->builder,
2131 LLVMBuildBitCast(gallivm->builder,
2132 lp_build_emit_fetch(bld_base, inst, 0, c),
2133 i32, ""),
2134 store_ptr);
2135
2136 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2137 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2138
2139 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2140 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2141
2142 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2143 }
2144
2145 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2146}
2147
Michel Dänzer404b29d2013-11-21 16:45:28 +09002148/* Emit one vertex from the geometry shader */
2149static void si_llvm_emit_vertex(
2150 const struct lp_build_tgsi_action *action,
2151 struct lp_build_tgsi_context *bld_base,
2152 struct lp_build_emit_data *emit_data)
2153{
2154 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002155 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002156 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002157 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002158 struct gallivm_state *gallivm = bld_base->base.gallivm;
2159 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002160 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2161 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002162 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002163 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002164 LLVMValueRef args[2];
2165 unsigned chan;
2166 int i;
2167
Michel Dänzer404b29d2013-11-21 16:45:28 +09002168 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002169 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002170
2171 /* If this thread has already emitted the declared maximum number of
2172 * vertices, kill it: excessive vertex emissions are not supposed to
2173 * have any effect, and GS threads have no externally observable
2174 * effects other than emitting vertices.
2175 */
2176 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2177 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002178 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002179 kill = lp_build_select(&bld_base->base, can_emit,
2180 lp_build_const_float(gallivm, 1.0f),
2181 lp_build_const_float(gallivm, -1.0f));
2182 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2183 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2184
Marek Olšák02134cf2014-10-04 22:07:50 +02002185 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002186 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002187 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002188
2189 for (chan = 0; chan < 4; chan++) {
2190 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002191 LLVMValueRef voffset =
2192 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002193 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002194
2195 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2196 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002197
2198 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2199
Marek Olšákfc3b3352014-10-05 13:33:40 +02002200 build_tbuffer_store(si_shader_ctx,
2201 si_shader_ctx->gsvs_ring,
2202 out_val, 1,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002203 voffset, soffset, 0,
2204 V_008F0C_BUF_DATA_FORMAT_32,
2205 V_008F0C_BUF_NUM_FORMAT_UINT,
2206 1, 0, 1, 1, 0);
2207 }
2208 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002209 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2210 lp_build_const_int32(gallivm, 1));
2211 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002212
2213 /* Signal vertex emission */
2214 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2215 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2216 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2217 LLVMVoidTypeInContext(gallivm->context), args, 2,
2218 LLVMNoUnwindAttribute);
2219}
2220
2221/* Cut one primitive from the geometry shader */
2222static void si_llvm_emit_primitive(
2223 const struct lp_build_tgsi_action *action,
2224 struct lp_build_tgsi_context *bld_base,
2225 struct lp_build_emit_data *emit_data)
2226{
2227 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2228 struct gallivm_state *gallivm = bld_base->base.gallivm;
2229 LLVMValueRef args[2];
2230
2231 /* Signal primitive cut */
2232 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2233 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2234 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2235 LLVMVoidTypeInContext(gallivm->context), args, 2,
2236 LLVMNoUnwindAttribute);
2237}
2238
Tom Stellarda75c6162012-01-06 17:38:37 -05002239static const struct lp_build_tgsi_action tex_action = {
2240 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002241 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002242};
2243
Michel Dänzer0495adb2013-05-06 12:45:14 +02002244static const struct lp_build_tgsi_action txq_action = {
2245 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002246 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002247 .intr_name = "llvm.SI.resinfo"
2248};
2249
Christian König206f0592013-03-20 14:37:21 +01002250static void create_meta_data(struct si_shader_context *si_shader_ctx)
2251{
2252 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2253 LLVMValueRef args[3];
2254
2255 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2256 args[1] = 0;
2257 args[2] = lp_build_const_int32(gallivm, 1);
2258
2259 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2260}
2261
Marek Olšák4f3f0432014-07-07 22:49:15 +02002262static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2263{
2264 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2265 CONST_ADDR_SPACE);
2266}
2267
Christian König55fe5cc2013-03-04 16:30:06 +01002268static void create_function(struct si_shader_context *si_shader_ctx)
2269{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002270 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2271 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002272 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002273 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002274 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002275
Christian König55fe5cc2013-03-04 16:30:06 +01002276 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002277 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002278 f32 = LLVMFloatTypeInContext(gallivm->context);
2279 v2i32 = LLVMVectorType(i32, 2);
2280 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002281 v4i32 = LLVMVectorType(i32, 4);
2282 v8i32 = LLVMVectorType(i32, 8);
2283 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002284
Marek Olšákee2a8182014-07-07 23:27:19 +02002285 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002286 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002287 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2288 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002289 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002290
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002291 switch (si_shader_ctx->type) {
2292 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002293 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002294 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002295 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002296 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002297 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002298
Michel Dänzer404b29d2013-11-21 16:45:28 +09002299 if (shader->key.vs.as_es) {
2300 params[SI_PARAM_ES2GS_OFFSET] = i32;
2301 num_params++;
2302 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002303 if (shader->is_gs_copy_shader) {
2304 last_array_pointer = SI_PARAM_CONST;
2305 num_params = SI_PARAM_CONST+1;
2306 }
2307
Michel Dänzer404b29d2013-11-21 16:45:28 +09002308 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002309
Michel Dänzer404b29d2013-11-21 16:45:28 +09002310 /* Streamout SGPRs. */
2311 if (shader->selector->so.num_outputs) {
2312 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2313 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2314 }
2315 /* A streamout buffer offset is loaded if the stride is non-zero. */
2316 for (i = 0; i < 4; i++) {
2317 if (!shader->selector->so.stride[i])
2318 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002319
Michel Dänzer404b29d2013-11-21 16:45:28 +09002320 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2321 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002322 }
2323
2324 last_sgpr = num_params-1;
2325
2326 /* VGPRs */
2327 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2328 params[num_params++] = i32; /* unused*/
2329 params[num_params++] = i32; /* unused */
2330 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002331 break;
Christian König0666ffd2013-03-05 15:07:39 +01002332
Michel Dänzer404b29d2013-11-21 16:45:28 +09002333 case TGSI_PROCESSOR_GEOMETRY:
2334 params[SI_PARAM_GS2VS_OFFSET] = i32;
2335 params[SI_PARAM_GS_WAVE_ID] = i32;
2336 last_sgpr = SI_PARAM_GS_WAVE_ID;
2337
2338 /* VGPRs */
2339 params[SI_PARAM_VTX0_OFFSET] = i32;
2340 params[SI_PARAM_VTX1_OFFSET] = i32;
2341 params[SI_PARAM_PRIMITIVE_ID] = i32;
2342 params[SI_PARAM_VTX2_OFFSET] = i32;
2343 params[SI_PARAM_VTX3_OFFSET] = i32;
2344 params[SI_PARAM_VTX4_OFFSET] = i32;
2345 params[SI_PARAM_VTX5_OFFSET] = i32;
2346 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2347 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2348 break;
2349
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002350 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002351 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002352 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002353 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002354 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2355 params[SI_PARAM_PERSP_CENTER] = v2i32;
2356 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2357 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2358 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2359 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2360 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2361 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2362 params[SI_PARAM_POS_X_FLOAT] = f32;
2363 params[SI_PARAM_POS_Y_FLOAT] = f32;
2364 params[SI_PARAM_POS_Z_FLOAT] = f32;
2365 params[SI_PARAM_POS_W_FLOAT] = f32;
2366 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002367 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002368 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2369 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002370 num_params = SI_PARAM_POS_FIXED_PT+1;
2371 break;
2372
2373 default:
2374 assert(0 && "unimplemented shader");
2375 return;
Christian Königc4973212013-03-05 12:14:02 +01002376 }
Christian König55fe5cc2013-03-04 16:30:06 +01002377
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002378 assert(num_params <= Elements(params));
2379 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002380 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002381
Marek Olšák1829f9c2015-01-04 17:08:57 +01002382 if (shader->dx10_clamp_mode)
2383 LLVMAddTargetDependentFunctionAttr(si_shader_ctx->radeon_bld.main_fn,
2384 "enable-no-nans-fp-math", "true");
2385
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002386 for (i = 0; i <= last_sgpr; ++i) {
2387 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002388
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002389 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2390 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002391 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002392 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002393 else
2394 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002395 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002396
Michel Dänzer404b29d2013-11-21 16:45:28 +09002397 if (bld_base->info &&
2398 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2399 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002400 si_shader_ctx->ddxy_lds =
2401 LLVMAddGlobalInAddressSpace(gallivm->module,
2402 LLVMArrayType(i32, 64),
2403 "ddxy_lds",
2404 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002405}
Tom Stellarda75c6162012-01-06 17:38:37 -05002406
Christian König0f6cf2b2013-03-15 15:53:25 +01002407static void preload_constants(struct si_shader_context *si_shader_ctx)
2408{
2409 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2410 struct gallivm_state * gallivm = bld_base->base.gallivm;
2411 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002412 unsigned buf;
2413 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002414
Marek Olšákee2a8182014-07-07 23:27:19 +02002415 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002416 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002417
Marek Olšák2fd42002013-10-25 11:45:47 +02002418 if (num_const == 0)
2419 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002420
Marek Olšák2fd42002013-10-25 11:45:47 +02002421 /* Allocate space for the constant values */
2422 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002423
Marek Olšák2fd42002013-10-25 11:45:47 +02002424 /* Load the resource descriptor */
2425 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002426 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002427
Marek Olšák2fd42002013-10-25 11:45:47 +02002428 /* Load the constants, we rely on the code sinking to do the rest */
2429 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002430 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002431 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002432 si_shader_ctx->const_resource[buf],
2433 lp_build_const_int32(gallivm, i * 4),
2434 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002435 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002436 }
2437}
2438
Christian König1c100182013-03-17 16:02:42 +01002439static void preload_samplers(struct si_shader_context *si_shader_ctx)
2440{
2441 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2442 struct gallivm_state * gallivm = bld_base->base.gallivm;
2443 const struct tgsi_shader_info * info = bld_base->info;
2444
2445 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2446
2447 LLVMValueRef res_ptr, samp_ptr;
2448 LLVMValueRef offset;
2449
2450 if (num_samplers == 0)
2451 return;
2452
Christian König1c100182013-03-17 16:02:42 +01002453 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2454 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2455
2456 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2457 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002458 /* Resource */
2459 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002460 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002461
2462 /* Sampler */
2463 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002464 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002465
2466 /* FMASK resource */
2467 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002468 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2469 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002470 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002471 }
Christian König1c100182013-03-17 16:02:42 +01002472 }
2473}
2474
Marek Olšák8d03d922013-09-01 23:59:06 +02002475static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2476{
2477 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2478 struct gallivm_state * gallivm = bld_base->base.gallivm;
2479 unsigned i;
2480
Michel Dänzer67e385b2014-01-08 17:48:21 +09002481 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2482 si_shader_ctx->shader->key.vs.as_es ||
2483 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002484 return;
2485
2486 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002487 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002488
2489 /* Load the resources, we rely on the code sinking to do the rest */
2490 for (i = 0; i < 4; ++i) {
2491 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002492 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002493 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002494
Marek Olšákd7876082014-09-26 23:06:32 +02002495 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002496 }
2497 }
2498}
2499
Marek Olšákfc3b3352014-10-05 13:33:40 +02002500/**
2501 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
2502 * for later use.
2503 */
2504static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
2505{
2506 struct gallivm_state *gallivm =
2507 si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2508
2509 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2510 SI_PARAM_RW_BUFFERS);
2511
2512 if ((si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
2513 si_shader_ctx->shader->key.vs.as_es) ||
2514 si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY) {
2515 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_ESGS);
2516
2517 si_shader_ctx->esgs_ring =
2518 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2519 }
2520
2521 if (si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY ||
2522 si_shader_ctx->shader->is_gs_copy_shader) {
2523 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_GSVS);
2524
2525 si_shader_ctx->gsvs_ring =
2526 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2527 }
2528}
2529
Tom Stellard2397a722014-12-10 09:13:59 -05002530void si_shader_binary_read_config(const struct si_screen *sscreen,
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002531 struct si_shader *shader,
2532 unsigned symbol_offset)
Tom Stellard302f53d2012-10-25 13:50:10 -04002533{
Tom Stellard302f53d2012-10-25 13:50:10 -04002534 unsigned i;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002535 const unsigned char *config =
Tom Stellard2397a722014-12-10 09:13:59 -05002536 radeon_shader_binary_config_start(&shader->binary,
2537 symbol_offset);
Tom Stellard302f53d2012-10-25 13:50:10 -04002538
Tom Stellardd50343d2013-04-04 16:21:06 -04002539 /* XXX: We may be able to emit some of these values directly rather than
2540 * extracting fields to be emitted later.
2541 */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002542
Tom Stellard2397a722014-12-10 09:13:59 -05002543 for (i = 0; i < shader->binary.config_size_per_symbol; i+= 8) {
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002544 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
2545 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
Tom Stellardd50343d2013-04-04 16:21:06 -04002546 switch (reg) {
2547 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2548 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2549 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2550 case R_00B848_COMPUTE_PGM_RSRC1:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002551 shader->num_sgprs = MAX2(shader->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
2552 shader->num_vgprs = MAX2(shader->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
Tom Stellardbbfa1c32015-03-06 14:53:00 +00002553 shader->float_mode = G_00B028_FLOAT_MODE(value);
Tom Stellardd50343d2013-04-04 16:21:06 -04002554 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002555 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002556 shader->lds_size = MAX2(shader->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002557 break;
2558 case R_00B84C_COMPUTE_PGM_RSRC2:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002559 shader->lds_size = MAX2(shader->lds_size, G_00B84C_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002560 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002561 case R_0286CC_SPI_PS_INPUT_ENA:
2562 shader->spi_ps_input_ena = value;
2563 break;
Tom Stellard2397a722014-12-10 09:13:59 -05002564 case R_0286E8_SPI_TMPRING_SIZE:
Tom Stellardb0f78032014-07-18 14:45:18 -04002565 case R_00B860_COMPUTE_TMPRING_SIZE:
2566 /* WAVESIZE is in units of 256 dwords. */
2567 shader->scratch_bytes_per_wave =
2568 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2569 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002570 default:
2571 fprintf(stderr, "Warning: Compiler emitted unknown "
2572 "config register: 0x%x\n", reg);
2573 break;
2574 }
2575 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002576}
2577
Tom Stellard2397a722014-12-10 09:13:59 -05002578void si_shader_apply_scratch_relocs(struct si_context *sctx,
2579 struct si_shader *shader,
2580 uint64_t scratch_va)
2581{
2582 unsigned i;
2583 uint32_t scratch_rsrc_dword0 = scratch_va & 0xffffffff;
2584 uint32_t scratch_rsrc_dword1 =
2585 S_008F04_BASE_ADDRESS_HI(scratch_va >> 32)
2586 | S_008F04_STRIDE(shader->scratch_bytes_per_wave / 64);
2587
2588 for (i = 0 ; i < shader->binary.reloc_count; i++) {
2589 const struct radeon_shader_reloc *reloc =
2590 &shader->binary.relocs[i];
2591 if (!strcmp(scratch_rsrc_dword0_symbol, reloc->name)) {
2592 util_memcpy_cpu_to_le32(shader->binary.code + reloc->offset,
2593 &scratch_rsrc_dword0, 4);
2594 } else if (!strcmp(scratch_rsrc_dword1_symbol, reloc->name)) {
2595 util_memcpy_cpu_to_le32(shader->binary.code + reloc->offset,
2596 &scratch_rsrc_dword1, 4);
2597 }
2598 }
2599}
2600
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002601int si_shader_binary_read(struct si_screen *sscreen,
2602 struct si_shader *shader,
2603 const struct radeon_shader_binary *binary)
2604{
2605
2606 unsigned i;
2607 unsigned code_size;
2608 unsigned char *ptr;
2609 bool dump = r600_can_dump_shader(&sscreen->b,
2610 shader->selector ? shader->selector->tokens : NULL);
2611
Tom Stellard51b43c52015-02-26 21:27:27 -05002612 si_shader_binary_read_config(sscreen, shader, 0);
2613
2614 if (dump) {
2615 if (!binary->disassembled) {
2616 fprintf(stderr, "SI CODE:\n");
2617 for (i = 0; i < binary->code_size; i+=4 ) {
2618 fprintf(stderr, "@0x%x: %02x%02x%02x%02x\n", i, binary->code[i + 3],
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002619 binary->code[i + 2], binary->code[i + 1],
2620 binary->code[i]);
Tom Stellard51b43c52015-02-26 21:27:27 -05002621 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002622 }
Tom Stellard51b43c52015-02-26 21:27:27 -05002623 fprintf(stderr, "SGPRS: %d\nVGPRS: %d\nCode Size: %d bytes\nLDS: %d blocks\n"
2624 "Scratch: %d bytes per wave\n",
2625 shader->num_sgprs, shader->num_vgprs, binary->code_size,
2626 shader->lds_size, shader->scratch_bytes_per_wave);
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002627 }
2628
Tom Stellard302f53d2012-10-25 13:50:10 -04002629 /* copy new shader */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002630 code_size = binary->code_size + binary->rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002631 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002632 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002633 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002634 if (shader->bo == NULL) {
2635 return -ENOMEM;
2636 }
2637
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002638
2639 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_READ_WRITE);
2640 util_memcpy_cpu_to_le32(ptr, binary->code, binary->code_size);
2641 if (binary->rodata_size > 0) {
2642 ptr += binary->code_size;
2643 util_memcpy_cpu_to_le32(ptr, binary->rodata, binary->rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002644 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002645
Marek Olšák1abb1a92014-09-17 22:44:22 +02002646 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002647
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002648 return 0;
2649}
Tom Stellard302f53d2012-10-25 13:50:10 -04002650
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002651int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2652 LLVMModuleRef mod)
2653{
2654 int r = 0;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002655 bool dump = r600_can_dump_shader(&sscreen->b,
2656 shader->selector ? shader->selector->tokens : NULL);
Tom Stellard2397a722014-12-10 09:13:59 -05002657 r = radeon_llvm_compile(mod, &shader->binary,
Tom Stellard761e36b2014-10-15 12:24:30 -04002658 r600_get_llvm_processor_name(sscreen->b.family), dump, sscreen->tm);
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002659
2660 if (r) {
2661 return r;
2662 }
Tom Stellard2397a722014-12-10 09:13:59 -05002663 r = si_shader_binary_read(sscreen, shader, &shader->binary);
2664
2665 FREE(shader->binary.config);
2666 FREE(shader->binary.rodata);
2667 FREE(shader->binary.global_symbol_offsets);
2668 if (shader->scratch_bytes_per_wave == 0) {
2669 FREE(shader->binary.code);
2670 FREE(shader->binary.relocs);
2671 memset(&shader->binary, 0, sizeof(shader->binary));
2672 }
Darren Powellbc866902014-03-31 18:00:28 -04002673 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002674}
2675
Michel Dänzer404b29d2013-11-21 16:45:28 +09002676/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002677static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002678 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002679 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002680{
2681 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2682 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2683 struct lp_build_context *base = &bld_base->base;
2684 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002685 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002686 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002687 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002688 LLVMValueRef args[9];
2689 int i, r;
2690
Marek Olšák02134cf2014-10-04 22:07:50 +02002691 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002692
2693 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002694 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002695
2696 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2697
2698 create_meta_data(si_shader_ctx);
2699 create_function(si_shader_ctx);
2700 preload_streamout_buffers(si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002701 preload_ring_buffers(si_shader_ctx);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002702
Marek Olšákfc3b3352014-10-05 13:33:40 +02002703 args[0] = si_shader_ctx->gsvs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002704 args[1] = lp_build_mul_imm(uint,
2705 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2706 si_shader_ctx->param_vertex_id),
2707 4);
2708 args[3] = uint->zero;
2709 args[4] = uint->one; /* OFFEN */
2710 args[5] = uint->zero; /* IDXEN */
2711 args[6] = uint->one; /* GLC */
2712 args[7] = uint->one; /* SLC */
2713 args[8] = uint->zero; /* TFE */
2714
2715 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002716 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002717 unsigned chan;
2718
Marek Olšák02134cf2014-10-04 22:07:50 +02002719 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002720 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002721
2722 for (chan = 0; chan < 4; chan++) {
2723 args[2] = lp_build_const_int32(gallivm,
2724 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002725 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002726
2727 outputs[i].values[chan] =
2728 LLVMBuildBitCast(gallivm->builder,
2729 build_intrinsic(gallivm->builder,
2730 "llvm.SI.buffer.load.dword.i32.i32",
2731 LLVMInt32TypeInContext(gallivm->context),
2732 args, 9,
2733 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2734 base->elem_type, "");
2735 }
2736 }
2737
Marek Olšák02134cf2014-10-04 22:07:50 +02002738 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002739
2740 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2741
2742 if (dump)
2743 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2744
Marek Olšák1abb1a92014-09-17 22:44:22 +02002745 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002746 bld_base->base.gallivm->module);
2747
2748 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2749
2750 FREE(outputs);
2751 return r;
2752}
2753
Marek Olšákffd701e2015-02-22 14:58:30 +01002754static void si_dump_key(unsigned shader, union si_shader_key *key)
2755{
2756 int i;
2757
2758 fprintf(stderr, "SHADER KEY\n");
2759
2760 switch (shader) {
2761 case PIPE_SHADER_VERTEX:
2762 fprintf(stderr, " instance_divisors = {");
2763 for (i = 0; i < Elements(key->vs.instance_divisors); i++)
2764 fprintf(stderr, !i ? "%u" : ", %u",
2765 key->vs.instance_divisors[i]);
2766 fprintf(stderr, "}\n");
2767
2768 if (key->vs.as_es)
2769 fprintf(stderr, " gs_used_inputs = 0x%"PRIx64"\n",
2770 key->vs.gs_used_inputs);
2771 fprintf(stderr, " as_es = %u\n", key->vs.as_es);
2772 break;
2773
2774 case PIPE_SHADER_GEOMETRY:
2775 break;
2776
2777 case PIPE_SHADER_FRAGMENT:
2778 fprintf(stderr, " export_16bpc = 0x%X\n", key->ps.export_16bpc);
2779 fprintf(stderr, " last_cbuf = %u\n", key->ps.last_cbuf);
2780 fprintf(stderr, " color_two_side = %u\n", key->ps.color_two_side);
2781 fprintf(stderr, " alpha_func = %u\n", key->ps.alpha_func);
2782 fprintf(stderr, " alpha_to_one = %u\n", key->ps.alpha_to_one);
2783 fprintf(stderr, " poly_stipple = %u\n", key->ps.poly_stipple);
2784 break;
2785
2786 default:
2787 assert(0);
2788 }
2789}
2790
Marek Olšák1abb1a92014-09-17 22:44:22 +02002791int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002792{
Marek Olšák2774abd2014-09-16 18:45:33 +02002793 struct si_shader_selector *sel = shader->selector;
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002794 struct tgsi_token *tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002795 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002796 struct lp_build_tgsi_context * bld_base;
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002797 struct tgsi_shader_info stipple_shader_info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002798 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002799 int r = 0;
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002800 bool poly_stipple = sel->type == PIPE_SHADER_FRAGMENT &&
2801 shader->key.ps.poly_stipple;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002802 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002803
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002804 if (poly_stipple) {
2805 tokens = util_pstipple_create_fragment_shader(tokens, NULL,
2806 SI_POLY_STIPPLE_SAMPLER);
2807 tgsi_scan_shader(tokens, &stipple_shader_info);
2808 }
2809
Michel Dänzere1df0d42014-01-15 12:31:07 +09002810 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2811 * conversion fails. */
2812 if (dump) {
Marek Olšákffd701e2015-02-22 14:58:30 +01002813 si_dump_key(sel->type, &shader->key);
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002814 tgsi_dump(tokens, 0);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002815 si_dump_streamout(&sel->so);
2816 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002817
Marek Olšák8c37c162014-09-16 18:40:07 +02002818 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002819
Michel Dänzercfebaf92012-08-31 19:04:08 +02002820 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002821 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2822 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2823
Marek Olšák1829f9c2015-01-04 17:08:57 +01002824 if (sel->type != PIPE_SHADER_COMPUTE)
2825 shader->dx10_clamp_mode = true;
2826
Marek Olšák52335682014-09-30 15:12:09 +02002827 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002828 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2829
Marek Olšák52335682014-09-30 15:12:09 +02002830 shader->uses_instanceid = sel->info.uses_instanceid;
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002831 bld_base->info = poly_stipple ? &stipple_shader_info : &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002832 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002833
2834 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002835 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002836 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2837 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002838 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002839 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2840 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2841 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002842 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002843 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002844 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2845 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002846
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002847 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2848 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002849
Michel Dänzer404b29d2013-11-21 16:45:28 +09002850 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2851 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2852
Marek Olšák645b4712014-11-20 22:16:09 +01002853 if (HAVE_LLVM >= 0x0306) {
2854 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
2855 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
2856 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
2857 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
2858 }
2859
Christian Könige4ed5872013-03-21 18:02:52 +01002860 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Tom Stellarda75c6162012-01-06 17:38:37 -05002861 si_shader_ctx.shader = shader;
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002862 si_shader_ctx.type = tgsi_get_processor_type(tokens);
Marek Olšák368b0a72014-12-28 21:51:35 +01002863 si_shader_ctx.screen = sscreen;
Tom Stellarda75c6162012-01-06 17:38:37 -05002864
Michel Dänzer51f89a02013-12-09 15:33:53 +09002865 switch (si_shader_ctx.type) {
2866 case TGSI_PROCESSOR_VERTEX:
2867 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002868 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002869 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2870 } else {
2871 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2872 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002873 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002874 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002875 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2876 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002877 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002878 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002879 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2880 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002881
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002882 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002883 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2884 shader->db_shader_control |=
2885 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2886 break;
2887 case TGSI_FS_DEPTH_LAYOUT_LESS:
2888 shader->db_shader_control |=
2889 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2890 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002891 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002892 break;
2893 default:
2894 assert(!"Unsupported shader type");
2895 return -1;
2896 }
2897
Christian König206f0592013-03-20 14:37:21 +01002898 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002899 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002900 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002901 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002902 preload_streamout_buffers(&si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002903 preload_ring_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002904
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002905 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2906 si_shader_ctx.gs_next_vertex =
2907 lp_build_alloca(bld_base->base.gallivm,
2908 bld_base->uint_bld.elem_type, "");
2909 }
2910
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002911 if (!lp_build_tgsi_llvm(bld_base, tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002912 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002913 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002914 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002915
2916 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2917
2918 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002919 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002920 if (r) {
2921 fprintf(stderr, "LLVM failed to compile shader\n");
2922 goto out;
2923 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002924
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002925 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002926
2927 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002928 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002929 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002930 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002931 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002932 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2933 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002934 free(shader->gs_copy_shader);
2935 shader->gs_copy_shader = NULL;
2936 goto out;
2937 }
2938 }
2939
Michel Dänzer404b29d2013-11-21 16:45:28 +09002940out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002941 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002942 FREE(si_shader_ctx.constants[i]);
Marek Olšák6c5af1d2015-01-31 21:43:37 +01002943 if (poly_stipple)
2944 tgsi_free_tokens(tokens);
Tom Stellard302f53d2012-10-25 13:50:10 -04002945 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002946}
2947
Marek Olšák2774abd2014-09-16 18:45:33 +02002948void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002949{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002950 if (shader->gs_copy_shader)
2951 si_shader_destroy(ctx, shader->gs_copy_shader);
2952
Tom Stellard5dcd97f2015-01-27 16:35:21 +00002953 if (shader->scratch_bo)
2954 r600_resource_reference(&shader->scratch_bo, NULL);
2955
Marek Olšáka81c3e02013-08-14 01:04:39 +02002956 r600_resource_reference(&shader->bo, NULL);
Tom Stellard2397a722014-12-10 09:13:59 -05002957
2958 FREE(shader->binary.code);
2959 FREE(shader->binary.relocs);
Tom Stellarda75c6162012-01-06 17:38:37 -05002960}