blob: e93708bf96438ddab475624d2a001676cb978551 [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"
Emil Velikova1312632014-08-16 17:58:25 +010035#include "radeon/radeon_llvm.h"
Tom Stellard1f4e48d2014-09-25 18:11:24 -070036#include "radeon/radeon_elf_util.h"
Emil Velikova1312632014-08-16 17:58:25 +010037#include "radeon/radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010038#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050039#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010040#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050041#include "tgsi/tgsi_dump.h"
42
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010043#include "si_pipe.h"
44#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050045#include "sid.h"
46
Tom Stellarda75c6162012-01-06 17:38:37 -050047#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050048
Michel Dänzer404b29d2013-11-21 16:45:28 +090049struct si_shader_output_values
50{
51 LLVMValueRef values[4];
52 unsigned name;
Michel Dänzer67e385b2014-01-08 17:48:21 +090053 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090054};
55
Tom Stellarda75c6162012-01-06 17:38:37 -050056struct si_shader_context
57{
58 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050059 struct tgsi_parse_context parse;
60 struct tgsi_token * tokens;
Marek Olšák8c37c162014-09-16 18:40:07 +020061 struct si_shader *shader;
Marek Olšák368b0a72014-12-28 21:51:35 +010062 struct si_screen *screen;
Tom Stellarda75c6162012-01-06 17:38:37 -050063 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020064 int param_streamout_config;
65 int param_streamout_write_index;
66 int param_streamout_offset[4];
67 int param_vertex_id;
68 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010069 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020070 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020071 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020072 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Marek Olšák55a9b772014-10-05 12:38:54 +020073 LLVMValueRef resources[SI_NUM_SAMPLER_VIEWS];
74 LLVMValueRef samplers[SI_NUM_SAMPLER_STATES];
Marek Olšák8d03d922013-09-01 23:59:06 +020075 LLVMValueRef so_buffers[4];
Marek Olšákfc3b3352014-10-05 13:33:40 +020076 LLVMValueRef esgs_ring;
77 LLVMValueRef gsvs_ring;
Michel Dänzerf07a96d2014-01-08 18:45:10 +090078 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050079};
80
81static struct si_shader_context * si_shader_context(
82 struct lp_build_tgsi_context * bld_base)
83{
84 return (struct si_shader_context *)bld_base;
85}
86
87
88#define PERSPECTIVE_BASE 0
89#define LINEAR_BASE 9
90
91#define SAMPLE_OFFSET 0
92#define CENTER_OFFSET 2
93#define CENTROID_OFSET 4
94
95#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040096#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020097#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040098#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050099
Michel Dänzer404b29d2013-11-21 16:45:28 +0900100
101#define SENDMSG_GS 2
102#define SENDMSG_GS_DONE 3
103
104#define SENDMSG_GS_OP_NOP (0 << 4)
105#define SENDMSG_GS_OP_CUT (1 << 4)
106#define SENDMSG_GS_OP_EMIT (2 << 4)
107#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
108
Marek Olšáke29353f2014-09-17 22:17:02 +0200109/**
110 * Returns a unique index for a semantic name and index. The index must be
111 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
112 * calculated.
113 */
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200114unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
Marek Olšáke29353f2014-09-17 22:17:02 +0200115{
116 switch (semantic_name) {
117 case TGSI_SEMANTIC_POSITION:
118 return 0;
119 case TGSI_SEMANTIC_PSIZE:
120 return 1;
121 case TGSI_SEMANTIC_CLIPDIST:
122 assert(index <= 1);
123 return 2 + index;
124 case TGSI_SEMANTIC_CLIPVERTEX:
125 return 4;
126 case TGSI_SEMANTIC_COLOR:
127 assert(index <= 1);
128 return 5 + index;
129 case TGSI_SEMANTIC_BCOLOR:
130 assert(index <= 1);
131 return 7 + index;
132 case TGSI_SEMANTIC_FOG:
133 return 9;
134 case TGSI_SEMANTIC_EDGEFLAG:
135 return 10;
136 case TGSI_SEMANTIC_GENERIC:
137 assert(index <= 63-11);
138 return 11 + index;
139 default:
140 assert(0);
141 return 63;
142 }
143}
144
145/**
146 * Given a semantic name and index of a parameter and a mask of used parameters
147 * (inputs or outputs), return the index of the parameter in the list of all
148 * used parameters.
149 *
150 * For example, assume this list of parameters:
151 * POSITION, PSIZE, GENERIC0, GENERIC2
152 * which has the mask:
153 * 11000000000101
154 * Then:
155 * querying POSITION returns 0,
156 * querying PSIZE returns 1,
157 * querying GENERIC0 returns 2,
158 * querying GENERIC2 returns 3.
159 *
160 * Which can be used as an offset to a parameter buffer in units of vec4s.
161 */
162static int get_param_index(unsigned semantic_name, unsigned index,
163 uint64_t mask)
164{
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200165 unsigned unique_index = si_shader_io_get_unique_index(semantic_name, index);
Marek Olšáke29353f2014-09-17 22:17:02 +0200166 int i, param_index = 0;
167
168 /* If not present... */
169 if (!((1llu << unique_index) & mask))
170 return -1;
171
172 for (i = 0; mask; i++) {
173 uint64_t bit = 1llu << i;
174
175 if (bit & mask) {
176 if (i == unique_index)
177 return param_index;
178
179 mask &= ~bit;
180 param_index++;
181 }
182 }
183
184 assert(!"unreachable");
185 return -1;
186}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900187
Tom Stellard467f5162012-05-16 15:15:35 -0400188/**
Marek Olšákd7876082014-09-26 23:06:32 +0200189 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
190 * It's equivalent to doing a load from &base_ptr[index].
Tom Stellard467f5162012-05-16 15:15:35 -0400191 *
Marek Olšákd7876082014-09-26 23:06:32 +0200192 * \param base_ptr Where the array starts.
193 * \param index The element index into the array.
Tom Stellard467f5162012-05-16 15:15:35 -0400194 */
Marek Olšákd7876082014-09-26 23:06:32 +0200195static LLVMValueRef build_indexed_load(struct si_shader_context *si_shader_ctx,
196 LLVMValueRef base_ptr, LLVMValueRef index)
Tom Stellard467f5162012-05-16 15:15:35 -0400197{
Marek Olšákd7876082014-09-26 23:06:32 +0200198 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
199 struct gallivm_state *gallivm = bld_base->base.gallivm;
200 LLVMValueRef indices[2], pointer;
Tom Stellard467f5162012-05-16 15:15:35 -0400201
Marek Olšákd7876082014-09-26 23:06:32 +0200202 indices[0] = bld_base->uint_bld.zero;
203 indices[1] = index;
Christian König206f0592013-03-20 14:37:21 +0100204
Marek Olšákd7876082014-09-26 23:06:32 +0200205 pointer = LLVMBuildGEP(gallivm->builder, base_ptr, indices, 2, "");
206 return LLVMBuildLoad(gallivm->builder, pointer, "");
207}
208
209/**
210 * Do a load from &base_ptr[index], but also add a flag that it's loading
211 * a constant.
212 */
213static LLVMValueRef build_indexed_load_const(
214 struct si_shader_context * si_shader_ctx,
215 LLVMValueRef base_ptr, LLVMValueRef index)
216{
217 LLVMValueRef result = build_indexed_load(si_shader_ctx, base_ptr, index);
Christian König206f0592013-03-20 14:37:21 +0100218 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
219 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400220}
221
Marek Olšákf317ce52013-09-05 15:39:57 +0200222static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100223 struct radeon_llvm_context * radeon_bld,
224 unsigned divisor)
225{
Marek Olšák8d03d922013-09-01 23:59:06 +0200226 struct si_shader_context *si_shader_ctx =
227 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100228 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
229
Marek Olšák8d03d922013-09-01 23:59:06 +0200230 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
231 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100232 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
233 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
234
235 if (divisor > 1)
236 result = LLVMBuildUDiv(gallivm->builder, result,
237 lp_build_const_int32(gallivm, divisor), "");
238
239 return result;
240}
241
Tom Stellarda75c6162012-01-06 17:38:37 -0500242static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900243 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500244 unsigned input_index,
245 const struct tgsi_full_declaration *decl)
246{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900247 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
248 struct gallivm_state *gallivm = base->gallivm;
249 struct si_shader_context *si_shader_ctx =
250 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100251 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
252
253 unsigned chan;
254
Tom Stellarda75c6162012-01-06 17:38:37 -0500255 LLVMValueRef t_list_ptr;
256 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400257 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500258 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100259 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400260 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500261 LLVMTypeRef vec4_type;
262 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500263
Tom Stellard467f5162012-05-16 15:15:35 -0400264 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100265 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500266
Michel Dänzer51f89a02013-12-09 15:33:53 +0900267 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400268
Marek Olšákd7876082014-09-26 23:06:32 +0200269 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400270
271 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900272 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500273
Christian Königa0dca442013-03-22 15:59:22 +0100274 if (divisor) {
275 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200276 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200277 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100278 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200279 /* Load the buffer index for vertices. */
280 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
281 si_shader_ctx->param_vertex_id);
282 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
283 SI_PARAM_BASE_VERTEX);
284 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100285 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500286
287 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400288 args[0] = t_list;
289 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100290 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900291 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100292 "llvm.SI.vs.load.input", vec4_type, args, 3,
293 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500294
295 /* Break up the vec4 into individual components */
296 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900297 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500298 /* XXX: Use a helper function for this. There is one in
299 * tgsi_llvm.c. */
300 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900301 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500302 input, llvm_chan, "");
303 }
304}
305
Michel Dänzer404b29d2013-11-21 16:45:28 +0900306static LLVMValueRef fetch_input_gs(
307 struct lp_build_tgsi_context *bld_base,
308 const struct tgsi_full_src_register *reg,
309 enum tgsi_opcode_type type,
310 unsigned swizzle)
311{
312 struct lp_build_context *base = &bld_base->base;
313 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200314 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900315 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
316 struct gallivm_state *gallivm = base->gallivm;
317 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
318 LLVMValueRef vtx_offset;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900319 LLVMValueRef args[9];
320 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200321 struct tgsi_shader_info *info = &shader->selector->info;
322 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
323 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900324
Marek Olšáke23fec12014-10-04 17:40:39 +0200325 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900326 if (swizzle == 0)
327 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
328 SI_PARAM_PRIMITIVE_ID);
329 else
330 return uint->zero;
331 }
332
Michel Dänzer404b29d2013-11-21 16:45:28 +0900333 if (!reg->Register.Dimension)
334 return NULL;
335
336 if (swizzle == ~0) {
337 LLVMValueRef values[TGSI_NUM_CHANNELS];
338 unsigned chan;
339 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
340 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
341 }
342 return lp_build_gather_values(bld_base->base.gallivm, values,
343 TGSI_NUM_CHANNELS);
344 }
345
346 /* Get the vertex offset parameter */
347 vtx_offset_param = reg->Dimension.Index;
348 if (vtx_offset_param < 2) {
349 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
350 } else {
351 assert(vtx_offset_param < 6);
352 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
353 }
354 vtx_offset = lp_build_mul_imm(uint,
355 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
356 vtx_offset_param),
357 4);
358
Marek Olšákfc3b3352014-10-05 13:33:40 +0200359 args[0] = si_shader_ctx->esgs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900360 args[1] = vtx_offset;
361 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200362 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200363 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900364 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900365 args[3] = uint->zero;
366 args[4] = uint->one; /* OFFEN */
367 args[5] = uint->zero; /* IDXEN */
368 args[6] = uint->one; /* GLC */
369 args[7] = uint->zero; /* SLC */
370 args[8] = uint->zero; /* TFE */
371
372 return LLVMBuildBitCast(gallivm->builder,
373 build_intrinsic(gallivm->builder,
374 "llvm.SI.buffer.load.dword.i32.i32",
375 i32, args, 9,
376 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
377 tgsi2llvmtype(bld_base, type), "");
378}
379
Tom Stellarda75c6162012-01-06 17:38:37 -0500380static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900381 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500382 unsigned input_index,
383 const struct tgsi_full_declaration *decl)
384{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900385 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
386 struct si_shader_context *si_shader_ctx =
387 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200388 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900389 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
390 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400391 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900392 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100393
394 LLVMValueRef interp_param;
395 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500396
397 /* This value is:
398 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
399 * quad begins a new primitive. Bit 0 always needs
400 * to be unset)
401 * [32:16] ParamOffset
402 *
403 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900404 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200405 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500406
Christian König0666ffd2013-03-05 15:07:39 +0100407 unsigned chan;
408
Tom Stellard0fb1e682012-09-06 16:18:11 -0400409 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
410 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400411 unsigned soa_index =
412 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900413 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100414 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100415
416 if (chan == 3)
417 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900418 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100419 LLVMBuildFDiv(gallivm->builder,
420 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900421 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100422 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400423 }
424 return;
425 }
426
Michel Dänzer97078b12012-09-25 12:41:31 +0200427 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900428 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Marek Olšáke827bb62014-10-16 16:20:26 +0200429 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900430 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
431 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200432 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900433 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200434 lp_build_const_float(gallivm, 1.0f);
435
436 return;
437 }
438
Marek Olšák8b057dd2014-10-04 22:15:07 +0200439 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200440 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200441 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200442
Francisco Jerez12799232012-04-30 18:27:52 +0200443 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500444 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100445 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500446 break;
447 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200448 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200449 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400450 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100451 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200452 else
Christian König0666ffd2013-03-05 15:07:39 +0100453 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200454 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200455 case TGSI_INTERPOLATE_COLOR:
456 if (si_shader_ctx->shader->key.ps.flatshade) {
457 interp_param = 0;
458 break;
459 }
460 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200461 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200462 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200463 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400464 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100465 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200466 else
Christian König0666ffd2013-03-05 15:07:39 +0100467 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500468 break;
469 default:
470 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
471 return;
472 }
473
Christian König0666ffd2013-03-05 15:07:39 +0100474 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
475
Tom Stellarda75c6162012-01-06 17:38:37 -0500476 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200477 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100478 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100479 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200480 LLVMValueRef face, is_face_positive;
481 LLVMValueRef back_attr_number =
482 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200483 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200484
Christian König0666ffd2013-03-05 15:07:39 +0100485 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
486
Michel Dänzer691f08d2012-09-06 18:03:38 +0200487 is_face_positive = LLVMBuildFCmp(gallivm->builder,
Marek Olšákd1d2af22015-01-04 20:45:35 +0100488 LLVMRealOGT, face,
Michel Dänzer691f08d2012-09-06 18:03:38 +0200489 lp_build_const_float(gallivm, 0.0f),
490 "");
491
Tom Stellarda75c6162012-01-06 17:38:37 -0500492 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100493 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200494 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
495 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
496 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
497 LLVMValueRef front, back;
498
499 args[0] = llvm_chan;
500 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900501 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100502 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100503 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200504
505 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900506 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100507 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100508 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200509
Michel Dänzer51f89a02013-12-09 15:33:53 +0900510 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200511 LLVMBuildSelect(gallivm->builder,
512 is_face_positive,
513 front,
514 back,
515 "");
516 }
517
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900518 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200519 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
520 LLVMValueRef args[4];
521
522 args[0] = uint->zero;
523 args[1] = attr_number;
524 args[2] = params;
525 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900526 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
527 build_intrinsic(gallivm->builder, intr_name,
528 input_type, args, args[3] ? 4 : 3,
529 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
530 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
531 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200532 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900533 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200534 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200535 } else {
536 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100537 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200538 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
539 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
540 args[0] = llvm_chan;
541 args[1] = attr_number;
542 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100543 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900544 radeon_bld->inputs[soa_index] =
545 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100546 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100547 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200548 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500549 }
550}
551
Marek Olšák5b06fc32014-05-06 18:12:40 +0200552static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
553{
554 struct gallivm_state *gallivm = &radeon_bld->gallivm;
555 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
556 SI_PARAM_ANCILLARY);
557 value = LLVMBuildLShr(gallivm->builder, value,
558 lp_build_const_int32(gallivm, 8), "");
559 value = LLVMBuildAnd(gallivm->builder, value,
560 lp_build_const_int32(gallivm, 0xf), "");
561 return value;
562}
563
Marek Olšákd7876082014-09-26 23:06:32 +0200564/**
565 * Load a dword from a constant buffer.
566 */
567static LLVMValueRef buffer_load_const(LLVMBuilderRef builder, LLVMValueRef resource,
568 LLVMValueRef offset, LLVMTypeRef return_type)
Marek Olšák250aa932014-05-06 14:10:47 +0200569{
570 LLVMValueRef args[2] = {resource, offset};
571
572 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
573 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
574}
575
Christian Könige4ed5872013-03-21 18:02:52 +0100576static void declare_system_value(
577 struct radeon_llvm_context * radeon_bld,
578 unsigned index,
579 const struct tgsi_full_declaration *decl)
580{
Marek Olšák8d03d922013-09-01 23:59:06 +0200581 struct si_shader_context *si_shader_ctx =
582 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200583 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
584 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100585 LLVMValueRef value = 0;
586
587 switch (decl->Semantic.Name) {
588 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200589 value = LLVMGetParam(radeon_bld->main_fn,
590 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100591 break;
592
593 case TGSI_SEMANTIC_VERTEXID:
Marek Olšákd7c6f392015-01-04 14:41:49 +0100594 value = LLVMBuildAdd(gallivm->builder,
595 LLVMGetParam(radeon_bld->main_fn,
596 si_shader_ctx->param_vertex_id),
597 LLVMGetParam(radeon_bld->main_fn,
598 SI_PARAM_BASE_VERTEX), "");
Christian Könige4ed5872013-03-21 18:02:52 +0100599 break;
600
Marek Olšák5b06fc32014-05-06 18:12:40 +0200601 case TGSI_SEMANTIC_SAMPLEID:
602 value = get_sample_id(radeon_bld);
603 break;
604
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200605 case TGSI_SEMANTIC_SAMPLEPOS:
606 {
607 LLVMBuilderRef builder = gallivm->builder;
608 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200609 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200610 LLVMValueRef resource = build_indexed_load_const(si_shader_ctx, desc, buf_index);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200611
612 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
613 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
614 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
615
616 LLVMValueRef pos[4] = {
Marek Olšákd7876082014-09-26 23:06:32 +0200617 buffer_load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
618 buffer_load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200619 lp_build_const_float(gallivm, 0),
620 lp_build_const_float(gallivm, 0)
621 };
622 value = lp_build_gather_values(gallivm, pos, 4);
623 break;
624 }
625
Christian Könige4ed5872013-03-21 18:02:52 +0100626 default:
627 assert(!"unknown system value");
628 return;
629 }
630
631 radeon_bld->system_values[index] = value;
632}
633
Tom Stellarda75c6162012-01-06 17:38:37 -0500634static LLVMValueRef fetch_constant(
635 struct lp_build_tgsi_context * bld_base,
636 const struct tgsi_full_src_register *reg,
637 enum tgsi_opcode_type type,
638 unsigned swizzle)
639{
Christian König55fe5cc2013-03-04 16:30:06 +0100640 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500641 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100642 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200643 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500644
Christian König0f6cf2b2013-03-15 15:53:25 +0100645 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100646 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500647
Christian König8514f5a2013-02-04 17:46:42 +0100648 if (swizzle == LP_CHAN_ALL) {
649 unsigned chan;
650 LLVMValueRef values[4];
651 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
652 values[chan] = fetch_constant(bld_base, reg, type, chan);
653
654 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
655 }
656
Marek Olšák2fd42002013-10-25 11:45:47 +0200657 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100658 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100659
Marek Olšák2fd42002013-10-25 11:45:47 +0200660 if (!reg->Register.Indirect)
661 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
662
Christian König0f6cf2b2013-03-15 15:53:25 +0100663 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
664 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
665 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200666 addr = lp_build_add(&bld_base->uint_bld, addr,
667 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200668
Marek Olšákd7876082014-09-26 23:06:32 +0200669 result = buffer_load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
Marek Olšák250aa932014-05-06 14:10:47 +0200670 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500671
Christian Königf5298b02013-02-28 14:50:07 +0100672 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500673}
674
Michel Dänzer26c71392012-08-24 12:03:11 +0200675/* Initialize arguments for the shader export intrinsic */
676static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900677 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200678 unsigned target,
679 LLVMValueRef *args)
680{
681 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
682 struct lp_build_context *uint =
683 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
684 struct lp_build_context *base = &bld_base->base;
685 unsigned compressed = 0;
686 unsigned chan;
687
Michel Dänzerf402acd2012-08-22 18:15:36 +0200688 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
689 int cbuf = target - V_008DFC_SQ_EXP_MRT;
690
691 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100692 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100693
694 if (compressed)
695 si_shader_ctx->shader->spi_shader_col_format |=
696 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
697 else
698 si_shader_ctx->shader->spi_shader_col_format |=
699 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200700
701 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200702 }
703 }
704
705 if (compressed) {
706 /* Pixel shader needs to pack output values before export */
707 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900708 args[0] = values[2 * chan];
709 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200710 args[chan + 5] =
711 build_intrinsic(base->gallivm->builder,
712 "llvm.SI.packf16",
713 LLVMInt32TypeInContext(base->gallivm->context),
714 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100715 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100716 args[chan + 7] = args[chan + 5] =
717 LLVMBuildBitCast(base->gallivm->builder,
718 args[chan + 5],
719 LLVMFloatTypeInContext(base->gallivm->context),
720 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200721 }
722
723 /* Set COMPR flag */
724 args[4] = uint->one;
725 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900726 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200727 /* +5 because the first output value will be
728 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900729 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200730
731 /* Clear COMPR flag */
732 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200733 }
734
735 /* XXX: This controls which components of the output
736 * registers actually get exported. (e.g bit 0 means export
737 * X component, bit 1 means export Y component, etc.) I'm
738 * hard coding this to 0xf for now. In the future, we might
739 * want to do something else. */
740 args[0] = lp_build_const_int32(base->gallivm, 0xf);
741
742 /* Specify whether the EXEC mask represents the valid mask */
743 args[1] = uint->zero;
744
745 /* Specify whether this is the last export */
746 args[2] = uint->zero;
747
748 /* Specify the target we are exporting */
749 args[3] = lp_build_const_int32(base->gallivm, target);
750
Michel Dänzer26c71392012-08-24 12:03:11 +0200751 /* XXX: We probably need to keep track of the output
752 * values, so we know what we are passing to the next
753 * stage. */
754}
755
Michel Dänzer404b29d2013-11-21 16:45:28 +0900756/* Load from output pointers and initialize arguments for the shader export intrinsic */
757static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
758 LLVMValueRef *out_ptr,
759 unsigned target,
760 LLVMValueRef *args)
761{
762 struct gallivm_state *gallivm = bld_base->base.gallivm;
763 LLVMValueRef values[4];
764 int i;
765
766 for (i = 0; i < 4; i++)
767 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
768
769 si_llvm_init_export_args(bld_base, values, target, args);
770}
771
Michel Dänzer7708a862012-11-02 15:57:30 +0100772static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900773 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100774{
775 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
776 struct gallivm_state *gallivm = bld_base->base.gallivm;
777
Christian Königa0dca442013-03-22 15:59:22 +0100778 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400779 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
780 SI_PARAM_ALPHA_REF);
781
Michel Dänzer7708a862012-11-02 15:57:30 +0100782 LLVMValueRef alpha_pass =
783 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100784 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900785 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400786 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100787 LLVMValueRef arg =
788 lp_build_select(&bld_base->base,
789 alpha_pass,
790 lp_build_const_float(gallivm, 1.0f),
791 lp_build_const_float(gallivm, -1.0f));
792
793 build_intrinsic(gallivm->builder,
794 "llvm.AMDGPU.kill",
795 LLVMVoidTypeInContext(gallivm->context),
796 &arg, 1, 0);
797 } else {
798 build_intrinsic(gallivm->builder,
799 "llvm.AMDGPU.kilp",
800 LLVMVoidTypeInContext(gallivm->context),
801 NULL, 0, 0);
802 }
Marek Olšákadc57972014-09-19 17:07:07 +0200803
804 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100805}
806
Michel Dänzere3befbc2013-05-15 18:09:50 +0200807static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900808 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200809{
810 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
811 struct lp_build_context *base = &bld_base->base;
812 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200813 unsigned reg_index;
814 unsigned chan;
815 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200816 LLVMValueRef base_elt;
817 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200818 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200819 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200820
Michel Dänzere3befbc2013-05-15 18:09:50 +0200821 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200822 LLVMValueRef *args = pos[2 + reg_index];
823
Michel Dänzere3befbc2013-05-15 18:09:50 +0200824 args[5] =
825 args[6] =
826 args[7] =
827 args[8] = lp_build_const_float(base->gallivm, 0.0f);
828
829 /* Compute dot products of position and user clip plane vectors */
830 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
831 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200832 args[1] = lp_build_const_int32(base->gallivm,
833 ((reg_index * 4 + chan) * 4 +
834 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200835 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200836 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200837 args[5 + chan] =
838 lp_build_add(base, args[5 + chan],
839 lp_build_mul(base, base_elt,
840 out_elts[const_chan]));
841 }
842 }
843
844 args[0] = lp_build_const_int32(base->gallivm, 0xf);
845 args[1] = uint->zero;
846 args[2] = uint->zero;
847 args[3] = lp_build_const_int32(base->gallivm,
848 V_008DFC_SQ_EXP_POS + 2 + reg_index);
849 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200850 }
851}
852
Marek Olšák8d03d922013-09-01 23:59:06 +0200853static void si_dump_streamout(struct pipe_stream_output_info *so)
854{
855 unsigned i;
856
857 if (so->num_outputs)
858 fprintf(stderr, "STREAMOUT\n");
859
860 for (i = 0; i < so->num_outputs; i++) {
861 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
862 so->output[i].start_component;
863 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
864 i, so->output[i].output_buffer,
865 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
866 so->output[i].register_index,
867 mask & 1 ? "x" : "",
868 mask & 2 ? "y" : "",
869 mask & 4 ? "z" : "",
870 mask & 8 ? "w" : "");
871 }
872}
873
874/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
875 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
876 * or v4i32 (num_channels=3,4). */
877static void build_tbuffer_store(struct si_shader_context *shader,
878 LLVMValueRef rsrc,
879 LLVMValueRef vdata,
880 unsigned num_channels,
881 LLVMValueRef vaddr,
882 LLVMValueRef soffset,
883 unsigned inst_offset,
884 unsigned dfmt,
885 unsigned nfmt,
886 unsigned offen,
887 unsigned idxen,
888 unsigned glc,
889 unsigned slc,
890 unsigned tfe)
891{
892 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
893 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
894 LLVMValueRef args[] = {
895 rsrc,
896 vdata,
897 LLVMConstInt(i32, num_channels, 0),
898 vaddr,
899 soffset,
900 LLVMConstInt(i32, inst_offset, 0),
901 LLVMConstInt(i32, dfmt, 0),
902 LLVMConstInt(i32, nfmt, 0),
903 LLVMConstInt(i32, offen, 0),
904 LLVMConstInt(i32, idxen, 0),
905 LLVMConstInt(i32, glc, 0),
906 LLVMConstInt(i32, slc, 0),
907 LLVMConstInt(i32, tfe, 0)
908 };
909
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900910 /* The instruction offset field has 12 bits */
911 assert(offen || inst_offset < (1 << 12));
912
Marek Olšák8d03d922013-09-01 23:59:06 +0200913 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
914 unsigned func = CLAMP(num_channels, 1, 3) - 1;
915 const char *types[] = {"i32", "v2i32", "v4i32"};
916 char name[256];
917 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
918
919 lp_build_intrinsic(gallivm->builder, name,
920 LLVMVoidTypeInContext(gallivm->context),
921 args, Elements(args));
922}
923
924static void build_streamout_store(struct si_shader_context *shader,
925 LLVMValueRef rsrc,
926 LLVMValueRef vdata,
927 unsigned num_channels,
928 LLVMValueRef vaddr,
929 LLVMValueRef soffset,
930 unsigned inst_offset)
931{
932 static unsigned dfmt[] = {
933 V_008F0C_BUF_DATA_FORMAT_32,
934 V_008F0C_BUF_DATA_FORMAT_32_32,
935 V_008F0C_BUF_DATA_FORMAT_32_32_32,
936 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
937 };
938 assert(num_channels >= 1 && num_channels <= 4);
939
940 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
941 inst_offset, dfmt[num_channels-1],
942 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
943}
944
945/* On SI, the vertex shader is responsible for writing streamout data
946 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900947static void si_llvm_emit_streamout(struct si_shader_context *shader,
948 struct si_shader_output_values *outputs,
949 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200950{
951 struct pipe_stream_output_info *so = &shader->shader->selector->so;
952 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
953 LLVMBuilderRef builder = gallivm->builder;
954 int i, j;
955 struct lp_build_if_state if_ctx;
956
957 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
958
959 LLVMValueRef so_param =
960 LLVMGetParam(shader->radeon_bld.main_fn,
961 shader->param_streamout_config);
962
963 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
964 LLVMValueRef so_vtx_count =
965 LLVMBuildAnd(builder,
966 LLVMBuildLShr(builder, so_param,
967 LLVMConstInt(i32, 16, 0), ""),
968 LLVMConstInt(i32, 127, 0), "");
969
970 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
971 NULL, 0, LLVMReadNoneAttribute);
972
973 /* can_emit = tid < so_vtx_count; */
974 LLVMValueRef can_emit =
975 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
976
977 /* Emit the streamout code conditionally. This actually avoids
978 * out-of-bounds buffer access. The hw tells us via the SGPR
979 * (so_vtx_count) which threads are allowed to emit streamout data. */
980 lp_build_if(&if_ctx, gallivm, can_emit);
981 {
982 /* The buffer offset is computed as follows:
983 * ByteOffset = streamout_offset[buffer_id]*4 +
984 * (streamout_write_index + thread_id)*stride[buffer_id] +
985 * attrib_offset
986 */
987
988 LLVMValueRef so_write_index =
989 LLVMGetParam(shader->radeon_bld.main_fn,
990 shader->param_streamout_write_index);
991
992 /* Compute (streamout_write_index + thread_id). */
993 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
994
995 /* Compute the write offset for each enabled buffer. */
996 LLVMValueRef so_write_offset[4] = {};
997 for (i = 0; i < 4; i++) {
998 if (!so->stride[i])
999 continue;
1000
1001 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1002 shader->param_streamout_offset[i]);
1003 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1004
1005 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1006 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1007 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1008 }
1009
Marek Olšák8d03d922013-09-01 23:59:06 +02001010 /* Write streamout data. */
1011 for (i = 0; i < so->num_outputs; i++) {
1012 unsigned buf_idx = so->output[i].output_buffer;
1013 unsigned reg = so->output[i].register_index;
1014 unsigned start = so->output[i].start_component;
1015 unsigned num_comps = so->output[i].num_components;
1016 LLVMValueRef out[4];
1017
1018 assert(num_comps && num_comps <= 4);
1019 if (!num_comps || num_comps > 4)
1020 continue;
1021
Marek Olšák558f7772014-10-04 22:37:23 +02001022 if (reg >= noutput)
1023 continue;
1024
Marek Olšák8d03d922013-09-01 23:59:06 +02001025 /* Load the output as int. */
1026 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001027 out[j] = LLVMBuildBitCast(builder,
1028 outputs[reg].values[start+j],
1029 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001030 }
1031
1032 /* Pack the output. */
1033 LLVMValueRef vdata = NULL;
1034
1035 switch (num_comps) {
1036 case 1: /* as i32 */
1037 vdata = out[0];
1038 break;
1039 case 2: /* as v2i32 */
1040 case 3: /* as v4i32 (aligned to 4) */
1041 case 4: /* as v4i32 */
1042 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1043 for (j = 0; j < num_comps; j++) {
1044 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1045 LLVMConstInt(i32, j, 0), "");
1046 }
1047 break;
1048 }
1049
1050 build_streamout_store(shader, shader->so_buffers[buf_idx],
1051 vdata, num_comps,
1052 so_write_offset[buf_idx],
1053 LLVMConstInt(i32, 0, 0),
1054 so->output[i].dst_offset*4);
1055 }
1056 }
1057 lp_build_endif(&if_ctx);
1058}
1059
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001060
Michel Dänzer404b29d2013-11-21 16:45:28 +09001061/* Generate export instructions for hardware VS shader stage */
1062static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1063 struct si_shader_output_values *outputs,
1064 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001065{
1066 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001067 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001068 struct lp_build_context * base = &bld_base->base;
1069 struct lp_build_context * uint =
1070 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001071 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001072 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001073 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001074 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001075 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001076 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001077 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001078 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001079
Michel Dänzer67e385b2014-01-08 17:48:21 +09001080 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1081 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001082 }
1083
Michel Dänzer404b29d2013-11-21 16:45:28 +09001084 for (i = 0; i < noutput; i++) {
1085 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001086 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001087
Michel Dänzer0afeea52013-05-02 14:53:17 +02001088handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001089 /* Select the correct target */
1090 switch(semantic_name) {
1091 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001092 psize_value = outputs[i].values[0];
1093 continue;
1094 case TGSI_SEMANTIC_EDGEFLAG:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001095 edgeflag_value = outputs[i].values[0];
1096 continue;
1097 case TGSI_SEMANTIC_LAYER:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001098 layer_value = outputs[i].values[0];
1099 continue;
1100 case TGSI_SEMANTIC_POSITION:
1101 target = V_008DFC_SQ_EXP_POS;
1102 break;
1103 case TGSI_SEMANTIC_COLOR:
1104 case TGSI_SEMANTIC_BCOLOR:
1105 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001106 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001107 param_count++;
1108 break;
1109 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001110 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1111 break;
1112 case TGSI_SEMANTIC_CLIPVERTEX:
1113 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1114 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001115 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001116 case TGSI_SEMANTIC_FOG:
1117 case TGSI_SEMANTIC_GENERIC:
1118 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001119 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001120 param_count++;
1121 break;
1122 default:
1123 target = 0;
1124 fprintf(stderr,
1125 "Warning: SI unhandled vs output type:%d\n",
1126 semantic_name);
1127 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001128
Michel Dänzer404b29d2013-11-21 16:45:28 +09001129 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001130
Michel Dänzer404b29d2013-11-21 16:45:28 +09001131 if (target >= V_008DFC_SQ_EXP_POS &&
1132 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1133 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1134 args, sizeof(args));
1135 } else {
1136 lp_build_intrinsic(base->gallivm->builder,
1137 "llvm.SI.export",
1138 LLVMVoidTypeInContext(base->gallivm->context),
1139 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001140 }
1141
1142 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1143 semantic_name = TGSI_SEMANTIC_GENERIC;
1144 goto handle_semantic;
1145 }
1146 }
1147
1148 /* We need to add the position output manually if it's missing. */
1149 if (!pos_args[0][0]) {
1150 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1151 pos_args[0][1] = uint->zero; /* EXEC mask */
1152 pos_args[0][2] = uint->zero; /* last export? */
1153 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1154 pos_args[0][4] = uint->zero; /* COMPR flag */
1155 pos_args[0][5] = base->zero; /* X */
1156 pos_args[0][6] = base->zero; /* Y */
1157 pos_args[0][7] = base->zero; /* Z */
1158 pos_args[0][8] = base->one; /* W */
1159 }
1160
1161 /* Write the misc vector (point size, edgeflag, layer, viewport). */
Marek Olšák16153472014-12-07 19:30:08 +01001162 if (shader->selector->info.writes_psize ||
1163 shader->selector->info.writes_edgeflag ||
1164 shader->selector->info.writes_layer) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001165 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
Marek Olšák16153472014-12-07 19:30:08 +01001166 shader->selector->info.writes_psize |
1167 (shader->selector->info.writes_edgeflag << 1) |
1168 (shader->selector->info.writes_layer << 2));
Michel Dänzer51f89a02013-12-09 15:33:53 +09001169 pos_args[1][1] = uint->zero; /* EXEC mask */
1170 pos_args[1][2] = uint->zero; /* last export? */
1171 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1172 pos_args[1][4] = uint->zero; /* COMPR flag */
1173 pos_args[1][5] = base->zero; /* X */
1174 pos_args[1][6] = base->zero; /* Y */
1175 pos_args[1][7] = base->zero; /* Z */
1176 pos_args[1][8] = base->zero; /* W */
1177
Marek Olšák16153472014-12-07 19:30:08 +01001178 if (shader->selector->info.writes_psize)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001179 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001180
Marek Olšák16153472014-12-07 19:30:08 +01001181 if (shader->selector->info.writes_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001182 /* The output is a float, but the hw expects an integer
1183 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001184 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1185 edgeflag_value,
1186 bld_base->uint_bld.elem_type, "");
1187 edgeflag_value = lp_build_min(&bld_base->int_bld,
1188 edgeflag_value,
1189 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001190
1191 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001192 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1193 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001194 base->elem_type, "");
1195 }
1196
Marek Olšák16153472014-12-07 19:30:08 +01001197 if (shader->selector->info.writes_layer)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001198 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001199 }
1200
1201 for (i = 0; i < 4; i++)
1202 if (pos_args[i][0])
1203 shader->nr_pos_exports++;
1204
1205 pos_idx = 0;
1206 for (i = 0; i < 4; i++) {
1207 if (!pos_args[i][0])
1208 continue;
1209
1210 /* Specify the target we are exporting */
1211 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1212
1213 if (pos_idx == shader->nr_pos_exports)
1214 /* Specify that this is the last export */
1215 pos_args[i][2] = uint->one;
1216
1217 lp_build_intrinsic(base->gallivm->builder,
1218 "llvm.SI.export",
1219 LLVMVoidTypeInContext(base->gallivm->context),
1220 pos_args[i], 9);
1221 }
1222}
1223
Michel Dänzer404b29d2013-11-21 16:45:28 +09001224static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1225{
1226 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1227 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001228 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001229 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001230 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001231 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1232 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001233 unsigned chan;
1234 int i;
1235
Marek Olšák216cf862014-10-04 17:04:05 +02001236 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001237 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001238 si_shader_ctx->radeon_bld.soa.outputs[i];
1239 int param_index = get_param_index(info->output_semantic_name[i],
1240 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001241 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001242
Marek Olšáke29353f2014-09-17 22:17:02 +02001243 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001244 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001245
1246 for (chan = 0; chan < 4; chan++) {
1247 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001248 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1249
Marek Olšákfc3b3352014-10-05 13:33:40 +02001250 build_tbuffer_store(si_shader_ctx,
1251 si_shader_ctx->esgs_ring,
1252 out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001253 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001254 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001255 V_008F0C_BUF_DATA_FORMAT_32,
1256 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001257 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001258 }
1259 }
1260}
1261
1262static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1263{
1264 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1265 struct gallivm_state *gallivm = bld_base->base.gallivm;
1266 LLVMValueRef args[2];
1267
1268 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1269 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1270 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1271 LLVMVoidTypeInContext(gallivm->context), args, 2,
1272 LLVMNoUnwindAttribute);
1273}
1274
1275static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1276{
1277 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1278 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001279 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001280 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001281 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001282
Marek Olšákec0d1682014-10-04 22:33:36 +02001283 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001284
Marek Olšákec0d1682014-10-04 22:33:36 +02001285 for (i = 0; i < info->num_outputs; i++) {
1286 outputs[i].name = info->output_semantic_name[i];
1287 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001288
Marek Olšákec0d1682014-10-04 22:33:36 +02001289 for (j = 0; j < 4; j++)
1290 outputs[i].values[j] =
1291 LLVMBuildLoad(gallivm->builder,
1292 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1293 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001294 }
1295
Marek Olšákec0d1682014-10-04 22:33:36 +02001296 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001297 FREE(outputs);
1298}
1299
Michel Dänzer51f89a02013-12-09 15:33:53 +09001300static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1301{
1302 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001303 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001304 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001305 struct lp_build_context * uint = &bld_base->uint_bld;
1306 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001307 LLVMValueRef args[9];
1308 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001309 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001310 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001311
Marek Olšák75e97e22014-10-04 23:13:50 +02001312 for (i = 0; i < info->num_outputs; i++) {
1313 unsigned semantic_name = info->output_semantic_name[i];
1314 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001315 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001316
Marek Olšák75e97e22014-10-04 23:13:50 +02001317 /* Select the correct target */
1318 switch (semantic_name) {
1319 case TGSI_SEMANTIC_POSITION:
1320 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001321 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001322 case TGSI_SEMANTIC_STENCIL:
1323 stencil_index = i;
1324 continue;
1325 case TGSI_SEMANTIC_SAMPLEMASK:
1326 samplemask_index = i;
1327 continue;
1328 case TGSI_SEMANTIC_COLOR:
1329 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1330 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1331 LLVMBuildStore(bld_base->base.gallivm->builder,
1332 bld_base->base.one,
1333 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001334
Marek Olšák75e97e22014-10-04 23:13:50 +02001335 if (semantic_index == 0 &&
1336 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1337 si_alpha_test(bld_base,
1338 si_shader_ctx->radeon_bld.soa.outputs[i]);
1339 break;
1340 default:
1341 target = 0;
1342 fprintf(stderr,
1343 "Warning: SI unhandled fs output type:%d\n",
1344 semantic_name);
1345 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001346
Marek Olšák75e97e22014-10-04 23:13:50 +02001347 si_llvm_init_export_args_load(bld_base,
1348 si_shader_ctx->radeon_bld.soa.outputs[i],
1349 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001350
Marek Olšák75e97e22014-10-04 23:13:50 +02001351 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1352 /* If there is an export instruction waiting to be emitted, do so now. */
1353 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001354 lp_build_intrinsic(base->gallivm->builder,
1355 "llvm.SI.export",
1356 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001357 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001358 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001359
1360 /* This instruction will be emitted at the end of the shader. */
1361 memcpy(last_args, args, sizeof(args));
1362
1363 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1364 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1365 semantic_index == 0 &&
1366 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1367 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1368 si_llvm_init_export_args_load(bld_base,
1369 si_shader_ctx->radeon_bld.soa.outputs[i],
1370 V_008DFC_SQ_EXP_MRT + c, args);
1371 lp_build_intrinsic(base->gallivm->builder,
1372 "llvm.SI.export",
1373 LLVMVoidTypeInContext(base->gallivm->context),
1374 args, 9);
1375 }
1376 }
1377 } else {
1378 lp_build_intrinsic(base->gallivm->builder,
1379 "llvm.SI.export",
1380 LLVMVoidTypeInContext(base->gallivm->context),
1381 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001382 }
1383 }
1384
Marek Olšákd0e8b652014-05-06 20:04:31 +02001385 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001386 LLVMValueRef out_ptr;
1387 unsigned mask = 0;
1388
1389 /* Specify the target we are exporting */
1390 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1391
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001392 args[5] = base->zero; /* R, depth */
1393 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1394 args[7] = base->zero; /* B, sample mask */
1395 args[8] = base->zero; /* A, alpha to mask */
1396
Michel Dänzer1a616c12012-11-13 17:35:09 +01001397 if (depth_index >= 0) {
1398 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1399 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1400 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001401 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001402 }
1403
1404 if (stencil_index >= 0) {
1405 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001406 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001407 mask |= 0x2;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001408 si_shader_ctx->shader->db_shader_control |=
1409 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001410 }
1411
Marek Olšákd0e8b652014-05-06 20:04:31 +02001412 if (samplemask_index >= 0) {
1413 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1414 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001415 mask |= 0x4;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001416 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1417 }
1418
Marek Olšák368b0a72014-12-28 21:51:35 +01001419 /* SI (except OLAND) has a bug that it only looks
1420 * at the X writemask component. */
1421 if (si_shader_ctx->screen->b.chip_class == SI &&
1422 si_shader_ctx->screen->b.family != CHIP_OLAND)
1423 mask |= 0x1;
1424
Marek Olšákd0e8b652014-05-06 20:04:31 +02001425 if (samplemask_index >= 0)
1426 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1427 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001428 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1429 else
1430 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1431
Michel Dänzer1a616c12012-11-13 17:35:09 +01001432 /* Specify which components to enable */
1433 args[0] = lp_build_const_int32(base->gallivm, mask);
1434
1435 args[1] =
1436 args[2] =
1437 args[4] = uint->zero;
1438
1439 if (last_args[0])
1440 lp_build_intrinsic(base->gallivm->builder,
1441 "llvm.SI.export",
1442 LLVMVoidTypeInContext(base->gallivm->context),
1443 args, 9);
1444 else
1445 memcpy(last_args, args, sizeof(args));
1446 }
1447
Michel Dänzer51f89a02013-12-09 15:33:53 +09001448 if (!last_args[0]) {
1449 /* Specify which components to enable */
1450 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001451
Michel Dänzer51f89a02013-12-09 15:33:53 +09001452 /* Specify the target we are exporting */
1453 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001454
Michel Dänzer51f89a02013-12-09 15:33:53 +09001455 /* Set COMPR flag to zero to export data as 32-bit */
1456 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001457
Michel Dänzer51f89a02013-12-09 15:33:53 +09001458 /* dummy bits */
1459 last_args[5]= uint->zero;
1460 last_args[6]= uint->zero;
1461 last_args[7]= uint->zero;
1462 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001463 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001464
1465 /* Specify whether the EXEC mask represents the valid mask */
1466 last_args[1] = uint->one;
1467
1468 /* Specify that this is the last export */
1469 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1470
1471 lp_build_intrinsic(base->gallivm->builder,
1472 "llvm.SI.export",
1473 LLVMVoidTypeInContext(base->gallivm->context),
1474 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001475}
1476
Marek Olšák4855acd2013-08-06 15:08:54 +02001477static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1478 struct lp_build_tgsi_context * bld_base,
1479 struct lp_build_emit_data * emit_data);
1480
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001481static bool tgsi_is_shadow_sampler(unsigned target)
1482{
1483 return target == TGSI_TEXTURE_SHADOW1D ||
1484 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1485 target == TGSI_TEXTURE_SHADOW2D ||
1486 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1487 target == TGSI_TEXTURE_SHADOWCUBE ||
1488 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1489 target == TGSI_TEXTURE_SHADOWRECT;
1490}
1491
Marek Olšák877bb522014-06-25 03:12:46 +02001492static const struct lp_build_tgsi_action tex_action;
1493
Tom Stellarda75c6162012-01-06 17:38:37 -05001494static void tex_fetch_args(
1495 struct lp_build_tgsi_context * bld_base,
1496 struct lp_build_emit_data * emit_data)
1497{
Christian König55fe5cc2013-03-04 16:30:06 +01001498 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001499 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001500 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001501 unsigned opcode = inst->Instruction.Opcode;
1502 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001503 LLVMValueRef coords[4];
1504 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001505 int ref_pos;
1506 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001507 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001508 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001509 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1510 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001511 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001512
1513 if (target == TGSI_TEXTURE_BUFFER) {
1514 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1515 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1516 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1517 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1518
Marek Olšák4f3f0432014-07-07 22:49:15 +02001519 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001520 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1521 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1522 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1523 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1524
1525 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1526 emit_data->args[0] = res;
1527 emit_data->args[1] = bld_base->uint_bld.zero;
1528 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1529 emit_data->arg_count = 3;
1530 return;
1531 }
Tom Stellard467f5162012-05-16 15:15:35 -04001532
Michel Dänzer120efee2013-01-25 12:10:11 +01001533 /* Fetch and project texture coordinates */
1534 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001535 for (chan = 0; chan < 3; chan++ ) {
1536 coords[chan] = lp_build_emit_fetch(bld_base,
1537 emit_data->inst, 0,
1538 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001539 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001540 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1541 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001542 coords[chan],
1543 coords[3]);
1544 }
1545
Michel Dänzer120efee2013-01-25 12:10:11 +01001546 if (opcode == TGSI_OPCODE_TXP)
1547 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001548
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001549 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001550 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001551 /* The offsets are six-bit signed integers packed like this:
1552 * X=[5:0], Y=[13:8], and Z=[21:16].
1553 */
1554 LLVMValueRef offset[3], pack;
1555
1556 assert(inst->Texture.NumOffsets == 1);
1557
1558 for (chan = 0; chan < 3; chan++) {
1559 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1560 emit_data->inst, 0, chan);
1561 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1562 lp_build_const_int32(gallivm, 0x3f), "");
1563 if (chan)
1564 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1565 lp_build_const_int32(gallivm, chan*8), "");
1566 }
1567
1568 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1569 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1570 address[count++] = pack;
1571 }
1572
Michel Dänzer120efee2013-01-25 12:10:11 +01001573 /* Pack LOD bias value */
1574 if (opcode == TGSI_OPCODE_TXB)
1575 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001576 if (opcode == TGSI_OPCODE_TXB2)
1577 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001578
Michel Dänzer120efee2013-01-25 12:10:11 +01001579 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001580 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001581 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1582 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1583 } else {
1584 assert(ref_pos >= 0);
1585 address[count++] = coords[ref_pos];
1586 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001587 }
1588
Marek Olšákb279f0142014-07-04 03:19:58 +02001589 if (target == TGSI_TEXTURE_CUBE ||
1590 target == TGSI_TEXTURE_CUBE_ARRAY ||
1591 target == TGSI_TEXTURE_SHADOWCUBE ||
1592 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1593 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1594
Michel Dänzera6b83c02013-02-21 16:10:55 +01001595 /* Pack user derivatives */
1596 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001597 int num_deriv_channels, param;
1598
1599 switch (target) {
1600 case TGSI_TEXTURE_3D:
1601 num_deriv_channels = 3;
1602 break;
1603 case TGSI_TEXTURE_2D:
1604 case TGSI_TEXTURE_SHADOW2D:
1605 case TGSI_TEXTURE_RECT:
1606 case TGSI_TEXTURE_SHADOWRECT:
1607 case TGSI_TEXTURE_2D_ARRAY:
1608 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1609 case TGSI_TEXTURE_CUBE:
1610 case TGSI_TEXTURE_SHADOWCUBE:
1611 case TGSI_TEXTURE_CUBE_ARRAY:
1612 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1613 num_deriv_channels = 2;
1614 break;
1615 case TGSI_TEXTURE_1D:
1616 case TGSI_TEXTURE_SHADOW1D:
1617 case TGSI_TEXTURE_1D_ARRAY:
1618 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1619 num_deriv_channels = 1;
1620 break;
1621 default:
1622 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001623 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001624
1625 for (param = 1; param <= 2; param++)
1626 for (chan = 0; chan < num_deriv_channels; chan++)
1627 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001628 }
1629
Michel Dänzer120efee2013-01-25 12:10:11 +01001630 /* Pack texture coordinates */
1631 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001632 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001633 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001634 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001635 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001636
Marek Olšákd2bd6342013-09-18 15:40:21 +02001637 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001638 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001639 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001640 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001641 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001642
1643 if (count > 16) {
1644 assert(!"Cannot handle more than 16 texture address parameters");
1645 count = 16;
1646 }
1647
1648 for (chan = 0; chan < count; chan++ ) {
1649 address[chan] = LLVMBuildBitCast(gallivm->builder,
1650 address[chan],
1651 LLVMInt32TypeInContext(gallivm->context),
1652 "");
1653 }
1654
Marek Olšák4855acd2013-08-06 15:08:54 +02001655 /* Adjust the sample index according to FMASK.
1656 *
1657 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1658 * which is the identity mapping. Each nibble says which physical sample
1659 * should be fetched to get that sample.
1660 *
1661 * For example, 0x11111100 means there are only 2 samples stored and
1662 * the second sample covers 3/4 of the pixel. When reading samples 0
1663 * and 1, return physical sample 0 (determined by the first two 0s
1664 * in FMASK), otherwise return physical sample 1.
1665 *
1666 * The sample index should be adjusted as follows:
1667 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1668 */
1669 if (target == TGSI_TEXTURE_2D_MSAA ||
1670 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1671 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1672 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001673 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001674 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001675 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001676
Marek Olšákd2bd6342013-09-18 15:40:21 +02001677 memcpy(txf_address, address, sizeof(txf_address));
1678
1679 if (target == TGSI_TEXTURE_2D_MSAA) {
1680 txf_address[2] = bld_base->uint_bld.zero;
1681 }
1682 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001683
1684 /* Pad to a power-of-two size. */
1685 while (txf_count < util_next_power_of_two(txf_count))
1686 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1687
1688 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001689 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1690 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1691 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001692 txf_emit_data.chan = 0;
1693 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001694 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001695 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001696 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001697 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001698 txf_emit_data.arg_count = 3;
1699
Marek Olšák877bb522014-06-25 03:12:46 +02001700 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001701
1702 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001703 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1704 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1705
1706 /* Apply the formula. */
1707 LLVMValueRef fmask =
1708 LLVMBuildExtractElement(gallivm->builder,
1709 txf_emit_data.output[0],
1710 uint_bld->zero, "");
1711
Marek Olšákd2bd6342013-09-18 15:40:21 +02001712 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001713
1714 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001715 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001716
1717 LLVMValueRef shifted_fmask =
1718 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1719
1720 LLVMValueRef final_sample =
1721 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1722
1723 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1724 * resource descriptor is 0 (invalid),
1725 */
1726 LLVMValueRef fmask_desc =
1727 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001728 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001729 LLVMVectorType(uint_bld->elem_type, 8), "");
1730
1731 LLVMValueRef fmask_word1 =
1732 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1733 uint_bld->one, "");
1734
1735 LLVMValueRef word1_is_nonzero =
1736 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1737 fmask_word1, uint_bld->zero, "");
1738
Marek Olšákd2bd6342013-09-18 15:40:21 +02001739 /* Replace the MSAA sample index. */
1740 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001741 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001742 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001743 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001744
Michel Dänzer36231112013-05-02 09:44:45 +02001745 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001746 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001747
1748 if (opcode == TGSI_OPCODE_TXF) {
1749 /* add tex offsets */
1750 if (inst->Texture.NumOffsets) {
1751 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1752 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1753 const struct tgsi_texture_offset * off = inst->TexOffsets;
1754
1755 assert(inst->Texture.NumOffsets == 1);
1756
Marek Olšákdefedc02013-09-18 15:36:38 +02001757 switch (target) {
1758 case TGSI_TEXTURE_3D:
1759 address[2] = lp_build_add(uint_bld, address[2],
1760 bld->immediates[off->Index][off->SwizzleZ]);
1761 /* fall through */
1762 case TGSI_TEXTURE_2D:
1763 case TGSI_TEXTURE_SHADOW2D:
1764 case TGSI_TEXTURE_RECT:
1765 case TGSI_TEXTURE_SHADOWRECT:
1766 case TGSI_TEXTURE_2D_ARRAY:
1767 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001768 address[1] =
1769 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001770 bld->immediates[off->Index][off->SwizzleY]);
1771 /* fall through */
1772 case TGSI_TEXTURE_1D:
1773 case TGSI_TEXTURE_SHADOW1D:
1774 case TGSI_TEXTURE_1D_ARRAY:
1775 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1776 address[0] =
1777 lp_build_add(uint_bld, address[0],
1778 bld->immediates[off->Index][off->SwizzleX]);
1779 break;
1780 /* texture offsets do not apply to other texture targets */
1781 }
Michel Dänzer36231112013-05-02 09:44:45 +02001782 }
1783
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001784 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1785 emit_data->arg_count = 3;
1786
Michel Dänzer36231112013-05-02 09:44:45 +02001787 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001788 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001789 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001790 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001791 opcode == TGSI_OPCODE_LODQ ||
1792 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001793 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1794 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1795 target == TGSI_TEXTURE_2D_ARRAY ||
1796 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1797 target == TGSI_TEXTURE_CUBE_ARRAY ||
1798 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1799 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001800 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001801
Marek Olšák57f3da92014-06-16 16:53:42 +02001802 if (opcode == TGSI_OPCODE_TG4) {
1803 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001804
Marek Olšák57f3da92014-06-16 16:53:42 +02001805 /* DMASK was repurposed for GATHER4. 4 components are always
1806 * returned and DMASK works like a swizzle - it selects
1807 * the component to fetch. The only valid DMASK values are
1808 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1809 * (red,red,red,red) etc.) The ISA document doesn't mention
1810 * this.
1811 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001812
Marek Olšák57f3da92014-06-16 16:53:42 +02001813 /* Get the component index from src1.x for Gather4. */
1814 if (!tgsi_is_shadow_sampler(target)) {
1815 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1816 LLVMValueRef comp_imm;
1817 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001818
Marek Olšák57f3da92014-06-16 16:53:42 +02001819 assert(src1.File == TGSI_FILE_IMMEDIATE);
1820
1821 comp_imm = imms[src1.Index][src1.SwizzleX];
1822 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1823 gather_comp = CLAMP(gather_comp, 0, 3);
1824 }
1825
1826 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001827 }
1828
Marek Olšák4855acd2013-08-06 15:08:54 +02001829 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001830 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001831 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1832 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1833 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1834 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1835 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1836 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1837 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1838
1839 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001840
1841 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001842 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001843 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001844 } else {
1845 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1846 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001847 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001848
1849 emit_data->dst_type = LLVMVectorType(
1850 LLVMFloatTypeInContext(gallivm->context),
1851 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001852 }
1853
Marek Olšák2484daa2014-04-22 21:23:29 +02001854 /* The fetch opcode has been converted to a 2D array fetch.
1855 * This simplifies the LLVM backend. */
1856 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1857 target = TGSI_TEXTURE_2D_ARRAY;
1858 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1859 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1860
Michel Dänzer120efee2013-01-25 12:10:11 +01001861 /* Pad to power of two vector */
1862 while (count < util_next_power_of_two(count))
1863 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1864
Christian Königccf3e8f2013-03-26 15:09:27 +01001865 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001866}
1867
Michel Dänzer07eddc42013-02-06 15:43:10 +01001868static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1869 struct lp_build_tgsi_context * bld_base,
1870 struct lp_build_emit_data * emit_data)
1871{
1872 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001873 unsigned opcode = emit_data->inst->Instruction.Opcode;
1874 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001875 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001876 bool has_offset = HAVE_LLVM >= 0x0305 ?
1877 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001878
Marek Olšák877bb522014-06-25 03:12:46 +02001879 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001880 emit_data->output[emit_data->chan] = build_intrinsic(
1881 base->gallivm->builder,
1882 "llvm.SI.vs.load.input", emit_data->dst_type,
1883 emit_data->args, emit_data->arg_count,
1884 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1885 return;
1886 }
1887
Marek Olšák877bb522014-06-25 03:12:46 +02001888 if (opcode == TGSI_OPCODE_TG4 ||
1889 opcode == TGSI_OPCODE_LODQ ||
1890 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1891 bool is_shadow = tgsi_is_shadow_sampler(target);
1892 const char *name = "llvm.SI.image.sample";
1893 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001894
Marek Olšák877bb522014-06-25 03:12:46 +02001895 switch (opcode) {
1896 case TGSI_OPCODE_TEX:
1897 case TGSI_OPCODE_TEX2:
1898 case TGSI_OPCODE_TXP:
1899 break;
1900 case TGSI_OPCODE_TXB:
1901 case TGSI_OPCODE_TXB2:
1902 infix = ".b";
1903 break;
1904 case TGSI_OPCODE_TXL:
1905 case TGSI_OPCODE_TXL2:
1906 infix = ".l";
1907 break;
1908 case TGSI_OPCODE_TXD:
1909 infix = ".d";
1910 break;
1911 case TGSI_OPCODE_TG4:
1912 name = "llvm.SI.gather4";
1913 break;
1914 case TGSI_OPCODE_LODQ:
1915 name = "llvm.SI.getlod";
1916 is_shadow = false;
1917 has_offset = false;
1918 break;
1919 default:
1920 assert(0);
1921 return;
1922 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001923
Marek Olšák877bb522014-06-25 03:12:46 +02001924 /* Add the type and suffixes .c, .o if needed. */
1925 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1926 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1927 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001928
Marek Olšák877bb522014-06-25 03:12:46 +02001929 emit_data->output[emit_data->chan] = build_intrinsic(
1930 base->gallivm->builder, intr_name, emit_data->dst_type,
1931 emit_data->args, emit_data->arg_count,
1932 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1933 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001934 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001935 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001936
Marek Olšák877bb522014-06-25 03:12:46 +02001937 switch (opcode) {
1938 case TGSI_OPCODE_TEX:
1939 case TGSI_OPCODE_TEX2:
1940 case TGSI_OPCODE_TXP:
1941 name = "llvm.SI.sample";
1942 break;
1943 case TGSI_OPCODE_TXB:
1944 case TGSI_OPCODE_TXB2:
1945 name = "llvm.SI.sampleb";
1946 break;
1947 case TGSI_OPCODE_TXD:
1948 name = "llvm.SI.sampled";
1949 break;
1950 case TGSI_OPCODE_TXF:
1951 name = "llvm.SI.imageload";
1952 break;
1953 case TGSI_OPCODE_TXL:
1954 case TGSI_OPCODE_TXL2:
1955 name = "llvm.SI.samplel";
1956 break;
1957 default:
1958 assert(0);
1959 return;
1960 }
1961
Marek Olšákd859bdb2014-07-14 19:40:14 +02001962 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1963 v16i8 = LLVMVectorType(i8, 16);
1964 v32i8 = LLVMVectorType(i8, 32);
1965
1966 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1967 emit_data->args[1], v32i8, "");
1968 if (opcode != TGSI_OPCODE_TXF) {
1969 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1970 emit_data->args[2], v16i8, "");
1971 }
1972
Marek Olšák877bb522014-06-25 03:12:46 +02001973 sprintf(intr_name, "%s.v%ui32", name,
1974 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1975
1976 emit_data->output[emit_data->chan] = build_intrinsic(
1977 base->gallivm->builder, intr_name, emit_data->dst_type,
1978 emit_data->args, emit_data->arg_count,
1979 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1980 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001981}
1982
Michel Dänzer0495adb2013-05-06 12:45:14 +02001983static void txq_fetch_args(
1984 struct lp_build_tgsi_context * bld_base,
1985 struct lp_build_emit_data * emit_data)
1986{
1987 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1988 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001989 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02001990 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001991
Marek Olšák2484daa2014-04-22 21:23:29 +02001992 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001993 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1994 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1995
1996 /* Read the size from the buffer descriptor directly. */
1997 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1998 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1999 size = LLVMBuildExtractElement(gallivm->builder, size,
2000 lp_build_const_int32(gallivm, 2), "");
2001 emit_data->args[0] = size;
2002 return;
2003 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002004
2005 /* Mip level */
2006 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2007
2008 /* Resource */
2009 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2010
Marek Olšák2484daa2014-04-22 21:23:29 +02002011 /* Texture target */
2012 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2013 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2014 target = TGSI_TEXTURE_2D_ARRAY;
2015
Michel Dänzer0495adb2013-05-06 12:45:14 +02002016 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002017 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002018
2019 emit_data->arg_count = 3;
2020
2021 emit_data->dst_type = LLVMVectorType(
2022 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2023 4);
2024}
2025
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002026static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2027 struct lp_build_tgsi_context * bld_base,
2028 struct lp_build_emit_data * emit_data)
2029{
Marek Olšák2484daa2014-04-22 21:23:29 +02002030 unsigned target = emit_data->inst->Texture.Texture;
2031
2032 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002033 /* Just return the buffer size. */
2034 emit_data->output[emit_data->chan] = emit_data->args[0];
2035 return;
2036 }
2037
2038 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002039
2040 /* Divide the number of layers by 6 to get the number of cubes. */
2041 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2042 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2043 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2044 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2045 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2046
2047 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2048 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2049 z = LLVMBuildSDiv(builder, z, six, "");
2050
2051 emit_data->output[emit_data->chan] =
2052 LLVMBuildInsertElement(builder, v4, z, two, "");
2053 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002054}
2055
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002056static void si_llvm_emit_ddxy(
2057 const struct lp_build_tgsi_action * action,
2058 struct lp_build_tgsi_context * bld_base,
2059 struct lp_build_emit_data * emit_data)
2060{
2061 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2062 struct gallivm_state *gallivm = bld_base->base.gallivm;
2063 struct lp_build_context * base = &bld_base->base;
2064 const struct tgsi_full_instruction *inst = emit_data->inst;
2065 unsigned opcode = inst->Instruction.Opcode;
2066 LLVMValueRef indices[2];
2067 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2068 LLVMValueRef tl, trbl, result[4];
2069 LLVMTypeRef i32;
2070 unsigned swizzle[4];
2071 unsigned c;
2072
2073 i32 = LLVMInt32TypeInContext(gallivm->context);
2074
2075 indices[0] = bld_base->uint_bld.zero;
2076 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2077 NULL, 0, LLVMReadNoneAttribute);
2078 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2079 indices, 2, "");
2080
2081 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2082 lp_build_const_int32(gallivm, 0xfffffffc), "");
2083 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2084 indices, 2, "");
2085
2086 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2087 lp_build_const_int32(gallivm,
2088 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2089 "");
2090 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2091 indices, 2, "");
2092
2093 for (c = 0; c < 4; ++c) {
2094 unsigned i;
2095
2096 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2097 for (i = 0; i < c; ++i) {
2098 if (swizzle[i] == swizzle[c]) {
2099 result[c] = result[i];
2100 break;
2101 }
2102 }
2103 if (i != c)
2104 continue;
2105
2106 LLVMBuildStore(gallivm->builder,
2107 LLVMBuildBitCast(gallivm->builder,
2108 lp_build_emit_fetch(bld_base, inst, 0, c),
2109 i32, ""),
2110 store_ptr);
2111
2112 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2113 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2114
2115 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2116 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2117
2118 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2119 }
2120
2121 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2122}
2123
Michel Dänzer404b29d2013-11-21 16:45:28 +09002124/* Emit one vertex from the geometry shader */
2125static void si_llvm_emit_vertex(
2126 const struct lp_build_tgsi_action *action,
2127 struct lp_build_tgsi_context *bld_base,
2128 struct lp_build_emit_data *emit_data)
2129{
2130 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002131 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002132 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002133 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002134 struct gallivm_state *gallivm = bld_base->base.gallivm;
2135 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002136 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2137 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002138 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002139 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002140 LLVMValueRef args[2];
2141 unsigned chan;
2142 int i;
2143
Michel Dänzer404b29d2013-11-21 16:45:28 +09002144 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002145 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002146
2147 /* If this thread has already emitted the declared maximum number of
2148 * vertices, kill it: excessive vertex emissions are not supposed to
2149 * have any effect, and GS threads have no externally observable
2150 * effects other than emitting vertices.
2151 */
2152 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2153 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002154 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002155 kill = lp_build_select(&bld_base->base, can_emit,
2156 lp_build_const_float(gallivm, 1.0f),
2157 lp_build_const_float(gallivm, -1.0f));
2158 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2159 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2160
Marek Olšák02134cf2014-10-04 22:07:50 +02002161 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002162 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002163 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002164
2165 for (chan = 0; chan < 4; chan++) {
2166 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002167 LLVMValueRef voffset =
2168 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002169 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002170
2171 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2172 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002173
2174 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2175
Marek Olšákfc3b3352014-10-05 13:33:40 +02002176 build_tbuffer_store(si_shader_ctx,
2177 si_shader_ctx->gsvs_ring,
2178 out_val, 1,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002179 voffset, soffset, 0,
2180 V_008F0C_BUF_DATA_FORMAT_32,
2181 V_008F0C_BUF_NUM_FORMAT_UINT,
2182 1, 0, 1, 1, 0);
2183 }
2184 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002185 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2186 lp_build_const_int32(gallivm, 1));
2187 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002188
2189 /* Signal vertex emission */
2190 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2191 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2192 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2193 LLVMVoidTypeInContext(gallivm->context), args, 2,
2194 LLVMNoUnwindAttribute);
2195}
2196
2197/* Cut one primitive from the geometry shader */
2198static void si_llvm_emit_primitive(
2199 const struct lp_build_tgsi_action *action,
2200 struct lp_build_tgsi_context *bld_base,
2201 struct lp_build_emit_data *emit_data)
2202{
2203 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2204 struct gallivm_state *gallivm = bld_base->base.gallivm;
2205 LLVMValueRef args[2];
2206
2207 /* Signal primitive cut */
2208 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2209 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2210 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2211 LLVMVoidTypeInContext(gallivm->context), args, 2,
2212 LLVMNoUnwindAttribute);
2213}
2214
Tom Stellarda75c6162012-01-06 17:38:37 -05002215static const struct lp_build_tgsi_action tex_action = {
2216 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002217 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002218};
2219
Michel Dänzer0495adb2013-05-06 12:45:14 +02002220static const struct lp_build_tgsi_action txq_action = {
2221 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002222 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002223 .intr_name = "llvm.SI.resinfo"
2224};
2225
Christian König206f0592013-03-20 14:37:21 +01002226static void create_meta_data(struct si_shader_context *si_shader_ctx)
2227{
2228 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2229 LLVMValueRef args[3];
2230
2231 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2232 args[1] = 0;
2233 args[2] = lp_build_const_int32(gallivm, 1);
2234
2235 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2236}
2237
Marek Olšák4f3f0432014-07-07 22:49:15 +02002238static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2239{
2240 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2241 CONST_ADDR_SPACE);
2242}
2243
Christian König55fe5cc2013-03-04 16:30:06 +01002244static void create_function(struct si_shader_context *si_shader_ctx)
2245{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002246 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2247 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002248 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002249 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002250 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002251
Christian König55fe5cc2013-03-04 16:30:06 +01002252 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002253 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002254 f32 = LLVMFloatTypeInContext(gallivm->context);
2255 v2i32 = LLVMVectorType(i32, 2);
2256 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002257 v4i32 = LLVMVectorType(i32, 4);
2258 v8i32 = LLVMVectorType(i32, 8);
2259 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002260
Marek Olšákee2a8182014-07-07 23:27:19 +02002261 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002262 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002263 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2264 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002265 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002266
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002267 switch (si_shader_ctx->type) {
2268 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002269 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002270 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002271 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002272 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002273 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002274
Michel Dänzer404b29d2013-11-21 16:45:28 +09002275 if (shader->key.vs.as_es) {
2276 params[SI_PARAM_ES2GS_OFFSET] = i32;
2277 num_params++;
2278 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002279 if (shader->is_gs_copy_shader) {
2280 last_array_pointer = SI_PARAM_CONST;
2281 num_params = SI_PARAM_CONST+1;
2282 }
2283
Michel Dänzer404b29d2013-11-21 16:45:28 +09002284 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002285
Michel Dänzer404b29d2013-11-21 16:45:28 +09002286 /* Streamout SGPRs. */
2287 if (shader->selector->so.num_outputs) {
2288 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2289 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2290 }
2291 /* A streamout buffer offset is loaded if the stride is non-zero. */
2292 for (i = 0; i < 4; i++) {
2293 if (!shader->selector->so.stride[i])
2294 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002295
Michel Dänzer404b29d2013-11-21 16:45:28 +09002296 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2297 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002298 }
2299
2300 last_sgpr = num_params-1;
2301
2302 /* VGPRs */
2303 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2304 params[num_params++] = i32; /* unused*/
2305 params[num_params++] = i32; /* unused */
2306 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002307 break;
Christian König0666ffd2013-03-05 15:07:39 +01002308
Michel Dänzer404b29d2013-11-21 16:45:28 +09002309 case TGSI_PROCESSOR_GEOMETRY:
2310 params[SI_PARAM_GS2VS_OFFSET] = i32;
2311 params[SI_PARAM_GS_WAVE_ID] = i32;
2312 last_sgpr = SI_PARAM_GS_WAVE_ID;
2313
2314 /* VGPRs */
2315 params[SI_PARAM_VTX0_OFFSET] = i32;
2316 params[SI_PARAM_VTX1_OFFSET] = i32;
2317 params[SI_PARAM_PRIMITIVE_ID] = i32;
2318 params[SI_PARAM_VTX2_OFFSET] = i32;
2319 params[SI_PARAM_VTX3_OFFSET] = i32;
2320 params[SI_PARAM_VTX4_OFFSET] = i32;
2321 params[SI_PARAM_VTX5_OFFSET] = i32;
2322 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2323 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2324 break;
2325
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002326 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002327 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002328 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002329 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002330 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2331 params[SI_PARAM_PERSP_CENTER] = v2i32;
2332 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2333 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2334 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2335 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2336 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2337 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2338 params[SI_PARAM_POS_X_FLOAT] = f32;
2339 params[SI_PARAM_POS_Y_FLOAT] = f32;
2340 params[SI_PARAM_POS_Z_FLOAT] = f32;
2341 params[SI_PARAM_POS_W_FLOAT] = f32;
2342 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002343 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002344 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2345 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002346 num_params = SI_PARAM_POS_FIXED_PT+1;
2347 break;
2348
2349 default:
2350 assert(0 && "unimplemented shader");
2351 return;
Christian Königc4973212013-03-05 12:14:02 +01002352 }
Christian König55fe5cc2013-03-04 16:30:06 +01002353
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002354 assert(num_params <= Elements(params));
2355 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002356 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002357
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002358 for (i = 0; i <= last_sgpr; ++i) {
2359 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002360
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002361 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2362 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002363 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002364 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002365 else
2366 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002367 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002368
Michel Dänzer404b29d2013-11-21 16:45:28 +09002369 if (bld_base->info &&
2370 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2371 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002372 si_shader_ctx->ddxy_lds =
2373 LLVMAddGlobalInAddressSpace(gallivm->module,
2374 LLVMArrayType(i32, 64),
2375 "ddxy_lds",
2376 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002377}
Tom Stellarda75c6162012-01-06 17:38:37 -05002378
Christian König0f6cf2b2013-03-15 15:53:25 +01002379static void preload_constants(struct si_shader_context *si_shader_ctx)
2380{
2381 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2382 struct gallivm_state * gallivm = bld_base->base.gallivm;
2383 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002384 unsigned buf;
2385 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002386
Marek Olšákee2a8182014-07-07 23:27:19 +02002387 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002388 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002389
Marek Olšák2fd42002013-10-25 11:45:47 +02002390 if (num_const == 0)
2391 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002392
Marek Olšák2fd42002013-10-25 11:45:47 +02002393 /* Allocate space for the constant values */
2394 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002395
Marek Olšák2fd42002013-10-25 11:45:47 +02002396 /* Load the resource descriptor */
2397 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002398 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002399
Marek Olšák2fd42002013-10-25 11:45:47 +02002400 /* Load the constants, we rely on the code sinking to do the rest */
2401 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002402 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002403 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002404 si_shader_ctx->const_resource[buf],
2405 lp_build_const_int32(gallivm, i * 4),
2406 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002407 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002408 }
2409}
2410
Christian König1c100182013-03-17 16:02:42 +01002411static void preload_samplers(struct si_shader_context *si_shader_ctx)
2412{
2413 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2414 struct gallivm_state * gallivm = bld_base->base.gallivm;
2415 const struct tgsi_shader_info * info = bld_base->info;
2416
2417 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2418
2419 LLVMValueRef res_ptr, samp_ptr;
2420 LLVMValueRef offset;
2421
2422 if (num_samplers == 0)
2423 return;
2424
Christian König1c100182013-03-17 16:02:42 +01002425 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2426 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2427
2428 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2429 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002430 /* Resource */
2431 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002432 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002433
2434 /* Sampler */
2435 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002436 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002437
2438 /* FMASK resource */
2439 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002440 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2441 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002442 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002443 }
Christian König1c100182013-03-17 16:02:42 +01002444 }
2445}
2446
Marek Olšák8d03d922013-09-01 23:59:06 +02002447static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2448{
2449 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2450 struct gallivm_state * gallivm = bld_base->base.gallivm;
2451 unsigned i;
2452
Michel Dänzer67e385b2014-01-08 17:48:21 +09002453 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2454 si_shader_ctx->shader->key.vs.as_es ||
2455 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002456 return;
2457
2458 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002459 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002460
2461 /* Load the resources, we rely on the code sinking to do the rest */
2462 for (i = 0; i < 4; ++i) {
2463 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002464 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002465 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002466
Marek Olšákd7876082014-09-26 23:06:32 +02002467 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002468 }
2469 }
2470}
2471
Marek Olšákfc3b3352014-10-05 13:33:40 +02002472/**
2473 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
2474 * for later use.
2475 */
2476static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
2477{
2478 struct gallivm_state *gallivm =
2479 si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2480
2481 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2482 SI_PARAM_RW_BUFFERS);
2483
2484 if ((si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
2485 si_shader_ctx->shader->key.vs.as_es) ||
2486 si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY) {
2487 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_ESGS);
2488
2489 si_shader_ctx->esgs_ring =
2490 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2491 }
2492
2493 if (si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY ||
2494 si_shader_ctx->shader->is_gs_copy_shader) {
2495 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_GSVS);
2496
2497 si_shader_ctx->gsvs_ring =
2498 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2499 }
2500}
2501
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002502void si_shader_binary_read_config(const struct radeon_shader_binary *binary,
2503 struct si_shader *shader,
2504 unsigned symbol_offset)
Tom Stellard302f53d2012-10-25 13:50:10 -04002505{
Tom Stellard302f53d2012-10-25 13:50:10 -04002506 unsigned i;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002507 const unsigned char *config =
2508 radeon_shader_binary_config_start(binary, symbol_offset);
Tom Stellard302f53d2012-10-25 13:50:10 -04002509
Tom Stellardd50343d2013-04-04 16:21:06 -04002510 /* XXX: We may be able to emit some of these values directly rather than
2511 * extracting fields to be emitted later.
2512 */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002513
2514 for (i = 0; i < binary->config_size_per_symbol; i+= 8) {
2515 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
2516 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
Tom Stellardd50343d2013-04-04 16:21:06 -04002517 switch (reg) {
2518 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2519 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2520 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2521 case R_00B848_COMPUTE_PGM_RSRC1:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002522 shader->num_sgprs = MAX2(shader->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
2523 shader->num_vgprs = MAX2(shader->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
Tom Stellardd50343d2013-04-04 16:21:06 -04002524 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002525 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002526 shader->lds_size = MAX2(shader->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002527 break;
2528 case R_00B84C_COMPUTE_PGM_RSRC2:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002529 shader->lds_size = MAX2(shader->lds_size, G_00B84C_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002530 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002531 case R_0286CC_SPI_PS_INPUT_ENA:
2532 shader->spi_ps_input_ena = value;
2533 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002534 case R_00B860_COMPUTE_TMPRING_SIZE:
2535 /* WAVESIZE is in units of 256 dwords. */
2536 shader->scratch_bytes_per_wave =
2537 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2538 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002539 default:
2540 fprintf(stderr, "Warning: Compiler emitted unknown "
2541 "config register: 0x%x\n", reg);
2542 break;
2543 }
2544 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002545}
2546
2547int si_shader_binary_read(struct si_screen *sscreen,
2548 struct si_shader *shader,
2549 const struct radeon_shader_binary *binary)
2550{
2551
2552 unsigned i;
2553 unsigned code_size;
2554 unsigned char *ptr;
2555 bool dump = r600_can_dump_shader(&sscreen->b,
2556 shader->selector ? shader->selector->tokens : NULL);
2557
2558 if (dump && !binary->disassembled) {
2559 fprintf(stderr, "SI CODE:\n");
2560 for (i = 0; i < binary->code_size; i+=4 ) {
2561 fprintf(stderr, "@0x%x: %02x%02x%02x%02x\n", i, binary->code[i + 3],
2562 binary->code[i + 2], binary->code[i + 1],
2563 binary->code[i]);
2564 }
2565 }
2566
2567 si_shader_binary_read_config(binary, shader, 0);
Tom Stellard302f53d2012-10-25 13:50:10 -04002568
2569 /* copy new shader */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002570 code_size = binary->code_size + binary->rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002571 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002572 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002573 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002574 if (shader->bo == NULL) {
2575 return -ENOMEM;
2576 }
2577
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002578
2579 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_READ_WRITE);
2580 util_memcpy_cpu_to_le32(ptr, binary->code, binary->code_size);
2581 if (binary->rodata_size > 0) {
2582 ptr += binary->code_size;
2583 util_memcpy_cpu_to_le32(ptr, binary->rodata, binary->rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002584 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002585
Marek Olšák1abb1a92014-09-17 22:44:22 +02002586 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002587
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002588 return 0;
2589}
Tom Stellard302f53d2012-10-25 13:50:10 -04002590
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002591int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2592 LLVMModuleRef mod)
2593{
2594 int r = 0;
2595 struct radeon_shader_binary binary;
2596 bool dump = r600_can_dump_shader(&sscreen->b,
2597 shader->selector ? shader->selector->tokens : NULL);
2598 memset(&binary, 0, sizeof(binary));
2599 r = radeon_llvm_compile(mod, &binary,
Tom Stellard761e36b2014-10-15 12:24:30 -04002600 r600_get_llvm_processor_name(sscreen->b.family), dump, sscreen->tm);
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002601
2602 if (r) {
2603 return r;
2604 }
2605 r = si_shader_binary_read(sscreen, shader, &binary);
2606 FREE(binary.code);
2607 FREE(binary.config);
2608 FREE(binary.rodata);
Darren Powellbc866902014-03-31 18:00:28 -04002609 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002610}
2611
Michel Dänzer404b29d2013-11-21 16:45:28 +09002612/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002613static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002614 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002615 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002616{
2617 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2618 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2619 struct lp_build_context *base = &bld_base->base;
2620 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002621 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002622 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002623 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002624 LLVMValueRef args[9];
2625 int i, r;
2626
Marek Olšák02134cf2014-10-04 22:07:50 +02002627 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002628
2629 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002630 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002631
2632 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2633
2634 create_meta_data(si_shader_ctx);
2635 create_function(si_shader_ctx);
2636 preload_streamout_buffers(si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002637 preload_ring_buffers(si_shader_ctx);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002638
Marek Olšákfc3b3352014-10-05 13:33:40 +02002639 args[0] = si_shader_ctx->gsvs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002640 args[1] = lp_build_mul_imm(uint,
2641 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2642 si_shader_ctx->param_vertex_id),
2643 4);
2644 args[3] = uint->zero;
2645 args[4] = uint->one; /* OFFEN */
2646 args[5] = uint->zero; /* IDXEN */
2647 args[6] = uint->one; /* GLC */
2648 args[7] = uint->one; /* SLC */
2649 args[8] = uint->zero; /* TFE */
2650
2651 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002652 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002653 unsigned chan;
2654
Marek Olšák02134cf2014-10-04 22:07:50 +02002655 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002656 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002657
2658 for (chan = 0; chan < 4; chan++) {
2659 args[2] = lp_build_const_int32(gallivm,
2660 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002661 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002662
2663 outputs[i].values[chan] =
2664 LLVMBuildBitCast(gallivm->builder,
2665 build_intrinsic(gallivm->builder,
2666 "llvm.SI.buffer.load.dword.i32.i32",
2667 LLVMInt32TypeInContext(gallivm->context),
2668 args, 9,
2669 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2670 base->elem_type, "");
2671 }
2672 }
2673
Marek Olšák02134cf2014-10-04 22:07:50 +02002674 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002675
2676 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2677
2678 if (dump)
2679 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2680
Marek Olšák1abb1a92014-09-17 22:44:22 +02002681 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002682 bld_base->base.gallivm->module);
2683
2684 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2685
2686 FREE(outputs);
2687 return r;
2688}
2689
Marek Olšák1abb1a92014-09-17 22:44:22 +02002690int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002691{
Marek Olšák2774abd2014-09-16 18:45:33 +02002692 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002693 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002694 struct lp_build_tgsi_context * bld_base;
2695 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002696 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002697 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002698
2699 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2700 * conversion fails. */
2701 if (dump) {
2702 tgsi_dump(sel->tokens, 0);
2703 si_dump_streamout(&sel->so);
2704 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002705
Marek Olšák8c37c162014-09-16 18:40:07 +02002706 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002707
Michel Dänzercfebaf92012-08-31 19:04:08 +02002708 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002709 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2710 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2711
Marek Olšák52335682014-09-30 15:12:09 +02002712 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002713 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2714
Marek Olšák52335682014-09-30 15:12:09 +02002715 shader->uses_instanceid = sel->info.uses_instanceid;
2716 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002717 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002718
2719 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002720 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002721 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2722 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002723 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002724 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2725 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2726 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002727 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002728 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002729 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2730 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002731
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002732 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2733 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002734
Michel Dänzer404b29d2013-11-21 16:45:28 +09002735 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2736 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2737
Marek Olšák645b4712014-11-20 22:16:09 +01002738 if (HAVE_LLVM >= 0x0306) {
2739 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
2740 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
2741 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
2742 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
2743 }
2744
Christian Könige4ed5872013-03-21 18:02:52 +01002745 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002746 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002747 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2748 si_shader_ctx.shader = shader;
2749 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Marek Olšák368b0a72014-12-28 21:51:35 +01002750 si_shader_ctx.screen = sscreen;
Tom Stellarda75c6162012-01-06 17:38:37 -05002751
Michel Dänzer51f89a02013-12-09 15:33:53 +09002752 switch (si_shader_ctx.type) {
2753 case TGSI_PROCESSOR_VERTEX:
2754 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002755 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002756 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2757 } else {
2758 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2759 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002760 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002761 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002762 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2763 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002764 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002765 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002766 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2767 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002768
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002769 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002770 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2771 shader->db_shader_control |=
2772 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2773 break;
2774 case TGSI_FS_DEPTH_LAYOUT_LESS:
2775 shader->db_shader_control |=
2776 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2777 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002778 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002779 break;
2780 default:
2781 assert(!"Unsupported shader type");
2782 return -1;
2783 }
2784
Christian König206f0592013-03-20 14:37:21 +01002785 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002786 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002787 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002788 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002789 preload_streamout_buffers(&si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002790 preload_ring_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002791
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002792 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2793 si_shader_ctx.gs_next_vertex =
2794 lp_build_alloca(bld_base->base.gallivm,
2795 bld_base->uint_bld.elem_type, "");
2796 }
2797
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002798 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002799 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002800 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002801 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002802
2803 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2804
2805 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002806 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002807 if (r) {
2808 fprintf(stderr, "LLVM failed to compile shader\n");
2809 goto out;
2810 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002811
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002812 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002813
2814 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002815 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002816 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002817 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002818 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002819 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2820 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002821 free(shader->gs_copy_shader);
2822 shader->gs_copy_shader = NULL;
2823 goto out;
2824 }
2825 }
2826
Tom Stellarda75c6162012-01-06 17:38:37 -05002827 tgsi_parse_free(&si_shader_ctx.parse);
2828
Michel Dänzer404b29d2013-11-21 16:45:28 +09002829out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002830 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002831 FREE(si_shader_ctx.constants[i]);
Tom Stellarda75c6162012-01-06 17:38:37 -05002832
Tom Stellard302f53d2012-10-25 13:50:10 -04002833 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002834}
2835
Marek Olšák2774abd2014-09-16 18:45:33 +02002836void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002837{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002838 if (shader->gs_copy_shader)
2839 si_shader_destroy(ctx, shader->gs_copy_shader);
2840
Marek Olšáka81c3e02013-08-14 01:04:39 +02002841 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002842 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002843}