blob: 40a2f906414e5084c9a90b809757e37e1dbed223 [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;
Tom Stellarda75c6162012-01-06 17:38:37 -050062 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020063 int param_streamout_config;
64 int param_streamout_write_index;
65 int param_streamout_offset[4];
66 int param_vertex_id;
67 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010068 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020069 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020070 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020071 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Marek Olšák55a9b772014-10-05 12:38:54 +020072 LLVMValueRef resources[SI_NUM_SAMPLER_VIEWS];
73 LLVMValueRef samplers[SI_NUM_SAMPLER_STATES];
Marek Olšák8d03d922013-09-01 23:59:06 +020074 LLVMValueRef so_buffers[4];
Marek Olšákfc3b3352014-10-05 13:33:40 +020075 LLVMValueRef esgs_ring;
76 LLVMValueRef gsvs_ring;
Michel Dänzerf07a96d2014-01-08 18:45:10 +090077 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050078};
79
80static struct si_shader_context * si_shader_context(
81 struct lp_build_tgsi_context * bld_base)
82{
83 return (struct si_shader_context *)bld_base;
84}
85
86
87#define PERSPECTIVE_BASE 0
88#define LINEAR_BASE 9
89
90#define SAMPLE_OFFSET 0
91#define CENTER_OFFSET 2
92#define CENTROID_OFSET 4
93
94#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040095#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020096#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040097#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050098
Michel Dänzer404b29d2013-11-21 16:45:28 +090099
100#define SENDMSG_GS 2
101#define SENDMSG_GS_DONE 3
102
103#define SENDMSG_GS_OP_NOP (0 << 4)
104#define SENDMSG_GS_OP_CUT (1 << 4)
105#define SENDMSG_GS_OP_EMIT (2 << 4)
106#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
107
Marek Olšáke29353f2014-09-17 22:17:02 +0200108/**
109 * Returns a unique index for a semantic name and index. The index must be
110 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
111 * calculated.
112 */
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200113unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
Marek Olšáke29353f2014-09-17 22:17:02 +0200114{
115 switch (semantic_name) {
116 case TGSI_SEMANTIC_POSITION:
117 return 0;
118 case TGSI_SEMANTIC_PSIZE:
119 return 1;
120 case TGSI_SEMANTIC_CLIPDIST:
121 assert(index <= 1);
122 return 2 + index;
123 case TGSI_SEMANTIC_CLIPVERTEX:
124 return 4;
125 case TGSI_SEMANTIC_COLOR:
126 assert(index <= 1);
127 return 5 + index;
128 case TGSI_SEMANTIC_BCOLOR:
129 assert(index <= 1);
130 return 7 + index;
131 case TGSI_SEMANTIC_FOG:
132 return 9;
133 case TGSI_SEMANTIC_EDGEFLAG:
134 return 10;
135 case TGSI_SEMANTIC_GENERIC:
136 assert(index <= 63-11);
137 return 11 + index;
138 default:
139 assert(0);
140 return 63;
141 }
142}
143
144/**
145 * Given a semantic name and index of a parameter and a mask of used parameters
146 * (inputs or outputs), return the index of the parameter in the list of all
147 * used parameters.
148 *
149 * For example, assume this list of parameters:
150 * POSITION, PSIZE, GENERIC0, GENERIC2
151 * which has the mask:
152 * 11000000000101
153 * Then:
154 * querying POSITION returns 0,
155 * querying PSIZE returns 1,
156 * querying GENERIC0 returns 2,
157 * querying GENERIC2 returns 3.
158 *
159 * Which can be used as an offset to a parameter buffer in units of vec4s.
160 */
161static int get_param_index(unsigned semantic_name, unsigned index,
162 uint64_t mask)
163{
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200164 unsigned unique_index = si_shader_io_get_unique_index(semantic_name, index);
Marek Olšáke29353f2014-09-17 22:17:02 +0200165 int i, param_index = 0;
166
167 /* If not present... */
168 if (!((1llu << unique_index) & mask))
169 return -1;
170
171 for (i = 0; mask; i++) {
172 uint64_t bit = 1llu << i;
173
174 if (bit & mask) {
175 if (i == unique_index)
176 return param_index;
177
178 mask &= ~bit;
179 param_index++;
180 }
181 }
182
183 assert(!"unreachable");
184 return -1;
185}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900186
Tom Stellard467f5162012-05-16 15:15:35 -0400187/**
Marek Olšákd7876082014-09-26 23:06:32 +0200188 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
189 * It's equivalent to doing a load from &base_ptr[index].
Tom Stellard467f5162012-05-16 15:15:35 -0400190 *
Marek Olšákd7876082014-09-26 23:06:32 +0200191 * \param base_ptr Where the array starts.
192 * \param index The element index into the array.
Tom Stellard467f5162012-05-16 15:15:35 -0400193 */
Marek Olšákd7876082014-09-26 23:06:32 +0200194static LLVMValueRef build_indexed_load(struct si_shader_context *si_shader_ctx,
195 LLVMValueRef base_ptr, LLVMValueRef index)
Tom Stellard467f5162012-05-16 15:15:35 -0400196{
Marek Olšákd7876082014-09-26 23:06:32 +0200197 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
198 struct gallivm_state *gallivm = bld_base->base.gallivm;
199 LLVMValueRef indices[2], pointer;
Tom Stellard467f5162012-05-16 15:15:35 -0400200
Marek Olšákd7876082014-09-26 23:06:32 +0200201 indices[0] = bld_base->uint_bld.zero;
202 indices[1] = index;
Christian König206f0592013-03-20 14:37:21 +0100203
Marek Olšákd7876082014-09-26 23:06:32 +0200204 pointer = LLVMBuildGEP(gallivm->builder, base_ptr, indices, 2, "");
205 return LLVMBuildLoad(gallivm->builder, pointer, "");
206}
207
208/**
209 * Do a load from &base_ptr[index], but also add a flag that it's loading
210 * a constant.
211 */
212static LLVMValueRef build_indexed_load_const(
213 struct si_shader_context * si_shader_ctx,
214 LLVMValueRef base_ptr, LLVMValueRef index)
215{
216 LLVMValueRef result = build_indexed_load(si_shader_ctx, base_ptr, index);
Christian König206f0592013-03-20 14:37:21 +0100217 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
218 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400219}
220
Marek Olšákf317ce52013-09-05 15:39:57 +0200221static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100222 struct radeon_llvm_context * radeon_bld,
223 unsigned divisor)
224{
Marek Olšák8d03d922013-09-01 23:59:06 +0200225 struct si_shader_context *si_shader_ctx =
226 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100227 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
228
Marek Olšák8d03d922013-09-01 23:59:06 +0200229 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
230 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100231 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
232 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
233
234 if (divisor > 1)
235 result = LLVMBuildUDiv(gallivm->builder, result,
236 lp_build_const_int32(gallivm, divisor), "");
237
238 return result;
239}
240
Tom Stellarda75c6162012-01-06 17:38:37 -0500241static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900242 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500243 unsigned input_index,
244 const struct tgsi_full_declaration *decl)
245{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900246 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
247 struct gallivm_state *gallivm = base->gallivm;
248 struct si_shader_context *si_shader_ctx =
249 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100250 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
251
252 unsigned chan;
253
Tom Stellarda75c6162012-01-06 17:38:37 -0500254 LLVMValueRef t_list_ptr;
255 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400256 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500257 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100258 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400259 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500260 LLVMTypeRef vec4_type;
261 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500262
Tom Stellard467f5162012-05-16 15:15:35 -0400263 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100264 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500265
Michel Dänzer51f89a02013-12-09 15:33:53 +0900266 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400267
Marek Olšákd7876082014-09-26 23:06:32 +0200268 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400269
270 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900271 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500272
Christian Königa0dca442013-03-22 15:59:22 +0100273 if (divisor) {
274 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200275 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200276 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100277 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200278 /* Load the buffer index for vertices. */
279 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
280 si_shader_ctx->param_vertex_id);
281 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
282 SI_PARAM_BASE_VERTEX);
283 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100284 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500285
286 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400287 args[0] = t_list;
288 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100289 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900290 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100291 "llvm.SI.vs.load.input", vec4_type, args, 3,
292 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500293
294 /* Break up the vec4 into individual components */
295 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900296 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500297 /* XXX: Use a helper function for this. There is one in
298 * tgsi_llvm.c. */
299 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900300 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500301 input, llvm_chan, "");
302 }
303}
304
Michel Dänzer404b29d2013-11-21 16:45:28 +0900305static LLVMValueRef fetch_input_gs(
306 struct lp_build_tgsi_context *bld_base,
307 const struct tgsi_full_src_register *reg,
308 enum tgsi_opcode_type type,
309 unsigned swizzle)
310{
311 struct lp_build_context *base = &bld_base->base;
312 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200313 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900314 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
315 struct gallivm_state *gallivm = base->gallivm;
316 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
317 LLVMValueRef vtx_offset;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900318 LLVMValueRef args[9];
319 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200320 struct tgsi_shader_info *info = &shader->selector->info;
321 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
322 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900323
Marek Olšáke23fec12014-10-04 17:40:39 +0200324 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900325 if (swizzle == 0)
326 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
327 SI_PARAM_PRIMITIVE_ID);
328 else
329 return uint->zero;
330 }
331
Michel Dänzer404b29d2013-11-21 16:45:28 +0900332 if (!reg->Register.Dimension)
333 return NULL;
334
335 if (swizzle == ~0) {
336 LLVMValueRef values[TGSI_NUM_CHANNELS];
337 unsigned chan;
338 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
339 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
340 }
341 return lp_build_gather_values(bld_base->base.gallivm, values,
342 TGSI_NUM_CHANNELS);
343 }
344
345 /* Get the vertex offset parameter */
346 vtx_offset_param = reg->Dimension.Index;
347 if (vtx_offset_param < 2) {
348 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
349 } else {
350 assert(vtx_offset_param < 6);
351 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
352 }
353 vtx_offset = lp_build_mul_imm(uint,
354 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
355 vtx_offset_param),
356 4);
357
Marek Olšákfc3b3352014-10-05 13:33:40 +0200358 args[0] = si_shader_ctx->esgs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900359 args[1] = vtx_offset;
360 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200361 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200362 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900363 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900364 args[3] = uint->zero;
365 args[4] = uint->one; /* OFFEN */
366 args[5] = uint->zero; /* IDXEN */
367 args[6] = uint->one; /* GLC */
368 args[7] = uint->zero; /* SLC */
369 args[8] = uint->zero; /* TFE */
370
371 return LLVMBuildBitCast(gallivm->builder,
372 build_intrinsic(gallivm->builder,
373 "llvm.SI.buffer.load.dword.i32.i32",
374 i32, args, 9,
375 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
376 tgsi2llvmtype(bld_base, type), "");
377}
378
Tom Stellarda75c6162012-01-06 17:38:37 -0500379static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900380 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500381 unsigned input_index,
382 const struct tgsi_full_declaration *decl)
383{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900384 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
385 struct si_shader_context *si_shader_ctx =
386 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200387 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900388 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
389 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400390 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900391 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100392
393 LLVMValueRef interp_param;
394 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500395
396 /* This value is:
397 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
398 * quad begins a new primitive. Bit 0 always needs
399 * to be unset)
400 * [32:16] ParamOffset
401 *
402 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900403 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200404 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500405
Christian König0666ffd2013-03-05 15:07:39 +0100406 unsigned chan;
407
Tom Stellard0fb1e682012-09-06 16:18:11 -0400408 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
409 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400410 unsigned soa_index =
411 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900412 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100413 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100414
415 if (chan == 3)
416 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900417 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100418 LLVMBuildFDiv(gallivm->builder,
419 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900420 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100421 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400422 }
423 return;
424 }
425
Michel Dänzer97078b12012-09-25 12:41:31 +0200426 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900427 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Marek Olšáke827bb62014-10-16 16:20:26 +0200428 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900429 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
430 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200431 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900432 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200433 lp_build_const_float(gallivm, 1.0f);
434
435 return;
436 }
437
Marek Olšák8b057dd2014-10-04 22:15:07 +0200438 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200439 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200440 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200441
Francisco Jerez12799232012-04-30 18:27:52 +0200442 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500443 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100444 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500445 break;
446 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200447 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200448 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400449 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100450 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200451 else
Christian König0666ffd2013-03-05 15:07:39 +0100452 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200453 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200454 case TGSI_INTERPOLATE_COLOR:
455 if (si_shader_ctx->shader->key.ps.flatshade) {
456 interp_param = 0;
457 break;
458 }
459 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200460 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200461 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200462 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400463 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100464 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200465 else
Christian König0666ffd2013-03-05 15:07:39 +0100466 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500467 break;
468 default:
469 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
470 return;
471 }
472
Christian König0666ffd2013-03-05 15:07:39 +0100473 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
474
Tom Stellarda75c6162012-01-06 17:38:37 -0500475 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200476 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100477 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100478 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200479 LLVMValueRef face, is_face_positive;
480 LLVMValueRef back_attr_number =
481 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200482 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200483
Christian König0666ffd2013-03-05 15:07:39 +0100484 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
485
Michel Dänzer691f08d2012-09-06 18:03:38 +0200486 is_face_positive = LLVMBuildFCmp(gallivm->builder,
487 LLVMRealUGT, face,
488 lp_build_const_float(gallivm, 0.0f),
489 "");
490
Tom Stellarda75c6162012-01-06 17:38:37 -0500491 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100492 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200493 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
494 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
495 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
496 LLVMValueRef front, back;
497
498 args[0] = llvm_chan;
499 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900500 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100501 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100502 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200503
504 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900505 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100506 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100507 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200508
Michel Dänzer51f89a02013-12-09 15:33:53 +0900509 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200510 LLVMBuildSelect(gallivm->builder,
511 is_face_positive,
512 front,
513 back,
514 "");
515 }
516
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900517 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200518 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
519 LLVMValueRef args[4];
520
521 args[0] = uint->zero;
522 args[1] = attr_number;
523 args[2] = params;
524 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900525 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
526 build_intrinsic(gallivm->builder, intr_name,
527 input_type, args, args[3] ? 4 : 3,
528 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
529 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
530 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200531 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900532 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200533 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200534 } else {
535 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100536 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200537 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
538 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
539 args[0] = llvm_chan;
540 args[1] = attr_number;
541 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100542 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900543 radeon_bld->inputs[soa_index] =
544 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100545 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100546 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200547 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500548 }
549}
550
Marek Olšák5b06fc32014-05-06 18:12:40 +0200551static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
552{
553 struct gallivm_state *gallivm = &radeon_bld->gallivm;
554 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
555 SI_PARAM_ANCILLARY);
556 value = LLVMBuildLShr(gallivm->builder, value,
557 lp_build_const_int32(gallivm, 8), "");
558 value = LLVMBuildAnd(gallivm->builder, value,
559 lp_build_const_int32(gallivm, 0xf), "");
560 return value;
561}
562
Marek Olšákd7876082014-09-26 23:06:32 +0200563/**
564 * Load a dword from a constant buffer.
565 */
566static LLVMValueRef buffer_load_const(LLVMBuilderRef builder, LLVMValueRef resource,
567 LLVMValueRef offset, LLVMTypeRef return_type)
Marek Olšák250aa932014-05-06 14:10:47 +0200568{
569 LLVMValueRef args[2] = {resource, offset};
570
571 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
572 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
573}
574
Christian Könige4ed5872013-03-21 18:02:52 +0100575static void declare_system_value(
576 struct radeon_llvm_context * radeon_bld,
577 unsigned index,
578 const struct tgsi_full_declaration *decl)
579{
Marek Olšák8d03d922013-09-01 23:59:06 +0200580 struct si_shader_context *si_shader_ctx =
581 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200582 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
583 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100584 LLVMValueRef value = 0;
585
586 switch (decl->Semantic.Name) {
587 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200588 value = LLVMGetParam(radeon_bld->main_fn,
589 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100590 break;
591
592 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200593 value = LLVMGetParam(radeon_bld->main_fn,
594 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100595 break;
596
Marek Olšák5b06fc32014-05-06 18:12:40 +0200597 case TGSI_SEMANTIC_SAMPLEID:
598 value = get_sample_id(radeon_bld);
599 break;
600
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200601 case TGSI_SEMANTIC_SAMPLEPOS:
602 {
603 LLVMBuilderRef builder = gallivm->builder;
604 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200605 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200606 LLVMValueRef resource = build_indexed_load_const(si_shader_ctx, desc, buf_index);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200607
608 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
609 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
610 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
611
612 LLVMValueRef pos[4] = {
Marek Olšákd7876082014-09-26 23:06:32 +0200613 buffer_load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
614 buffer_load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200615 lp_build_const_float(gallivm, 0),
616 lp_build_const_float(gallivm, 0)
617 };
618 value = lp_build_gather_values(gallivm, pos, 4);
619 break;
620 }
621
Christian Könige4ed5872013-03-21 18:02:52 +0100622 default:
623 assert(!"unknown system value");
624 return;
625 }
626
627 radeon_bld->system_values[index] = value;
628}
629
Tom Stellarda75c6162012-01-06 17:38:37 -0500630static LLVMValueRef fetch_constant(
631 struct lp_build_tgsi_context * bld_base,
632 const struct tgsi_full_src_register *reg,
633 enum tgsi_opcode_type type,
634 unsigned swizzle)
635{
Christian König55fe5cc2013-03-04 16:30:06 +0100636 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500637 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100638 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200639 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500640
Christian König0f6cf2b2013-03-15 15:53:25 +0100641 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100642 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500643
Christian König8514f5a2013-02-04 17:46:42 +0100644 if (swizzle == LP_CHAN_ALL) {
645 unsigned chan;
646 LLVMValueRef values[4];
647 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
648 values[chan] = fetch_constant(bld_base, reg, type, chan);
649
650 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
651 }
652
Marek Olšák2fd42002013-10-25 11:45:47 +0200653 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100654 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100655
Marek Olšák2fd42002013-10-25 11:45:47 +0200656 if (!reg->Register.Indirect)
657 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
658
Christian König0f6cf2b2013-03-15 15:53:25 +0100659 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
660 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
661 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200662 addr = lp_build_add(&bld_base->uint_bld, addr,
663 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200664
Marek Olšákd7876082014-09-26 23:06:32 +0200665 result = buffer_load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
Marek Olšák250aa932014-05-06 14:10:47 +0200666 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500667
Christian Königf5298b02013-02-28 14:50:07 +0100668 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500669}
670
Michel Dänzer26c71392012-08-24 12:03:11 +0200671/* Initialize arguments for the shader export intrinsic */
672static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900673 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200674 unsigned target,
675 LLVMValueRef *args)
676{
677 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
678 struct lp_build_context *uint =
679 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
680 struct lp_build_context *base = &bld_base->base;
681 unsigned compressed = 0;
682 unsigned chan;
683
Michel Dänzerf402acd2012-08-22 18:15:36 +0200684 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
685 int cbuf = target - V_008DFC_SQ_EXP_MRT;
686
687 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100688 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100689
690 if (compressed)
691 si_shader_ctx->shader->spi_shader_col_format |=
692 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
693 else
694 si_shader_ctx->shader->spi_shader_col_format |=
695 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200696
697 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200698 }
699 }
700
701 if (compressed) {
702 /* Pixel shader needs to pack output values before export */
703 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900704 args[0] = values[2 * chan];
705 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200706 args[chan + 5] =
707 build_intrinsic(base->gallivm->builder,
708 "llvm.SI.packf16",
709 LLVMInt32TypeInContext(base->gallivm->context),
710 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100711 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100712 args[chan + 7] = args[chan + 5] =
713 LLVMBuildBitCast(base->gallivm->builder,
714 args[chan + 5],
715 LLVMFloatTypeInContext(base->gallivm->context),
716 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200717 }
718
719 /* Set COMPR flag */
720 args[4] = uint->one;
721 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900722 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200723 /* +5 because the first output value will be
724 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900725 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200726
727 /* Clear COMPR flag */
728 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200729 }
730
731 /* XXX: This controls which components of the output
732 * registers actually get exported. (e.g bit 0 means export
733 * X component, bit 1 means export Y component, etc.) I'm
734 * hard coding this to 0xf for now. In the future, we might
735 * want to do something else. */
736 args[0] = lp_build_const_int32(base->gallivm, 0xf);
737
738 /* Specify whether the EXEC mask represents the valid mask */
739 args[1] = uint->zero;
740
741 /* Specify whether this is the last export */
742 args[2] = uint->zero;
743
744 /* Specify the target we are exporting */
745 args[3] = lp_build_const_int32(base->gallivm, target);
746
Michel Dänzer26c71392012-08-24 12:03:11 +0200747 /* XXX: We probably need to keep track of the output
748 * values, so we know what we are passing to the next
749 * stage. */
750}
751
Michel Dänzer404b29d2013-11-21 16:45:28 +0900752/* Load from output pointers and initialize arguments for the shader export intrinsic */
753static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
754 LLVMValueRef *out_ptr,
755 unsigned target,
756 LLVMValueRef *args)
757{
758 struct gallivm_state *gallivm = bld_base->base.gallivm;
759 LLVMValueRef values[4];
760 int i;
761
762 for (i = 0; i < 4; i++)
763 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
764
765 si_llvm_init_export_args(bld_base, values, target, args);
766}
767
Michel Dänzer7708a862012-11-02 15:57:30 +0100768static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900769 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100770{
771 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
772 struct gallivm_state *gallivm = bld_base->base.gallivm;
773
Christian Königa0dca442013-03-22 15:59:22 +0100774 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400775 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
776 SI_PARAM_ALPHA_REF);
777
Michel Dänzer7708a862012-11-02 15:57:30 +0100778 LLVMValueRef alpha_pass =
779 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100780 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900781 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400782 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100783 LLVMValueRef arg =
784 lp_build_select(&bld_base->base,
785 alpha_pass,
786 lp_build_const_float(gallivm, 1.0f),
787 lp_build_const_float(gallivm, -1.0f));
788
789 build_intrinsic(gallivm->builder,
790 "llvm.AMDGPU.kill",
791 LLVMVoidTypeInContext(gallivm->context),
792 &arg, 1, 0);
793 } else {
794 build_intrinsic(gallivm->builder,
795 "llvm.AMDGPU.kilp",
796 LLVMVoidTypeInContext(gallivm->context),
797 NULL, 0, 0);
798 }
Marek Olšákadc57972014-09-19 17:07:07 +0200799
800 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100801}
802
Michel Dänzere3befbc2013-05-15 18:09:50 +0200803static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900804 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200805{
806 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200807 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200808 struct lp_build_context *base = &bld_base->base;
809 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200810 unsigned reg_index;
811 unsigned chan;
812 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200813 LLVMValueRef base_elt;
814 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200815 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200816 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200817
Michel Dänzere3befbc2013-05-15 18:09:50 +0200818 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200819 LLVMValueRef *args = pos[2 + reg_index];
820
Marek Olšák8c37c162014-09-16 18:40:07 +0200821 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200822
Michel Dänzere3befbc2013-05-15 18:09:50 +0200823 args[5] =
824 args[6] =
825 args[7] =
826 args[8] = lp_build_const_float(base->gallivm, 0.0f);
827
828 /* Compute dot products of position and user clip plane vectors */
829 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
830 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200831 args[1] = lp_build_const_int32(base->gallivm,
832 ((reg_index * 4 + chan) * 4 +
833 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200834 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200835 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200836 args[5 + chan] =
837 lp_build_add(base, args[5 + chan],
838 lp_build_mul(base, base_elt,
839 out_elts[const_chan]));
840 }
841 }
842
843 args[0] = lp_build_const_int32(base->gallivm, 0xf);
844 args[1] = uint->zero;
845 args[2] = uint->zero;
846 args[3] = lp_build_const_int32(base->gallivm,
847 V_008DFC_SQ_EXP_POS + 2 + reg_index);
848 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200849 }
850}
851
Marek Olšák8d03d922013-09-01 23:59:06 +0200852static void si_dump_streamout(struct pipe_stream_output_info *so)
853{
854 unsigned i;
855
856 if (so->num_outputs)
857 fprintf(stderr, "STREAMOUT\n");
858
859 for (i = 0; i < so->num_outputs; i++) {
860 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
861 so->output[i].start_component;
862 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
863 i, so->output[i].output_buffer,
864 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
865 so->output[i].register_index,
866 mask & 1 ? "x" : "",
867 mask & 2 ? "y" : "",
868 mask & 4 ? "z" : "",
869 mask & 8 ? "w" : "");
870 }
871}
872
873/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
874 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
875 * or v4i32 (num_channels=3,4). */
876static void build_tbuffer_store(struct si_shader_context *shader,
877 LLVMValueRef rsrc,
878 LLVMValueRef vdata,
879 unsigned num_channels,
880 LLVMValueRef vaddr,
881 LLVMValueRef soffset,
882 unsigned inst_offset,
883 unsigned dfmt,
884 unsigned nfmt,
885 unsigned offen,
886 unsigned idxen,
887 unsigned glc,
888 unsigned slc,
889 unsigned tfe)
890{
891 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
892 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
893 LLVMValueRef args[] = {
894 rsrc,
895 vdata,
896 LLVMConstInt(i32, num_channels, 0),
897 vaddr,
898 soffset,
899 LLVMConstInt(i32, inst_offset, 0),
900 LLVMConstInt(i32, dfmt, 0),
901 LLVMConstInt(i32, nfmt, 0),
902 LLVMConstInt(i32, offen, 0),
903 LLVMConstInt(i32, idxen, 0),
904 LLVMConstInt(i32, glc, 0),
905 LLVMConstInt(i32, slc, 0),
906 LLVMConstInt(i32, tfe, 0)
907 };
908
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900909 /* The instruction offset field has 12 bits */
910 assert(offen || inst_offset < (1 << 12));
911
Marek Olšák8d03d922013-09-01 23:59:06 +0200912 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
913 unsigned func = CLAMP(num_channels, 1, 3) - 1;
914 const char *types[] = {"i32", "v2i32", "v4i32"};
915 char name[256];
916 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
917
918 lp_build_intrinsic(gallivm->builder, name,
919 LLVMVoidTypeInContext(gallivm->context),
920 args, Elements(args));
921}
922
923static void build_streamout_store(struct si_shader_context *shader,
924 LLVMValueRef rsrc,
925 LLVMValueRef vdata,
926 unsigned num_channels,
927 LLVMValueRef vaddr,
928 LLVMValueRef soffset,
929 unsigned inst_offset)
930{
931 static unsigned dfmt[] = {
932 V_008F0C_BUF_DATA_FORMAT_32,
933 V_008F0C_BUF_DATA_FORMAT_32_32,
934 V_008F0C_BUF_DATA_FORMAT_32_32_32,
935 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
936 };
937 assert(num_channels >= 1 && num_channels <= 4);
938
939 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
940 inst_offset, dfmt[num_channels-1],
941 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
942}
943
944/* On SI, the vertex shader is responsible for writing streamout data
945 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900946static void si_llvm_emit_streamout(struct si_shader_context *shader,
947 struct si_shader_output_values *outputs,
948 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200949{
950 struct pipe_stream_output_info *so = &shader->shader->selector->so;
951 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
952 LLVMBuilderRef builder = gallivm->builder;
953 int i, j;
954 struct lp_build_if_state if_ctx;
955
956 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
957
958 LLVMValueRef so_param =
959 LLVMGetParam(shader->radeon_bld.main_fn,
960 shader->param_streamout_config);
961
962 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
963 LLVMValueRef so_vtx_count =
964 LLVMBuildAnd(builder,
965 LLVMBuildLShr(builder, so_param,
966 LLVMConstInt(i32, 16, 0), ""),
967 LLVMConstInt(i32, 127, 0), "");
968
969 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
970 NULL, 0, LLVMReadNoneAttribute);
971
972 /* can_emit = tid < so_vtx_count; */
973 LLVMValueRef can_emit =
974 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
975
976 /* Emit the streamout code conditionally. This actually avoids
977 * out-of-bounds buffer access. The hw tells us via the SGPR
978 * (so_vtx_count) which threads are allowed to emit streamout data. */
979 lp_build_if(&if_ctx, gallivm, can_emit);
980 {
981 /* The buffer offset is computed as follows:
982 * ByteOffset = streamout_offset[buffer_id]*4 +
983 * (streamout_write_index + thread_id)*stride[buffer_id] +
984 * attrib_offset
985 */
986
987 LLVMValueRef so_write_index =
988 LLVMGetParam(shader->radeon_bld.main_fn,
989 shader->param_streamout_write_index);
990
991 /* Compute (streamout_write_index + thread_id). */
992 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
993
994 /* Compute the write offset for each enabled buffer. */
995 LLVMValueRef so_write_offset[4] = {};
996 for (i = 0; i < 4; i++) {
997 if (!so->stride[i])
998 continue;
999
1000 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1001 shader->param_streamout_offset[i]);
1002 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1003
1004 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1005 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1006 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1007 }
1008
Marek Olšák8d03d922013-09-01 23:59:06 +02001009 /* Write streamout data. */
1010 for (i = 0; i < so->num_outputs; i++) {
1011 unsigned buf_idx = so->output[i].output_buffer;
1012 unsigned reg = so->output[i].register_index;
1013 unsigned start = so->output[i].start_component;
1014 unsigned num_comps = so->output[i].num_components;
1015 LLVMValueRef out[4];
1016
1017 assert(num_comps && num_comps <= 4);
1018 if (!num_comps || num_comps > 4)
1019 continue;
1020
Marek Olšák558f7772014-10-04 22:37:23 +02001021 if (reg >= noutput)
1022 continue;
1023
Marek Olšák8d03d922013-09-01 23:59:06 +02001024 /* Load the output as int. */
1025 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001026 out[j] = LLVMBuildBitCast(builder,
1027 outputs[reg].values[start+j],
1028 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001029 }
1030
1031 /* Pack the output. */
1032 LLVMValueRef vdata = NULL;
1033
1034 switch (num_comps) {
1035 case 1: /* as i32 */
1036 vdata = out[0];
1037 break;
1038 case 2: /* as v2i32 */
1039 case 3: /* as v4i32 (aligned to 4) */
1040 case 4: /* as v4i32 */
1041 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1042 for (j = 0; j < num_comps; j++) {
1043 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1044 LLVMConstInt(i32, j, 0), "");
1045 }
1046 break;
1047 }
1048
1049 build_streamout_store(shader, shader->so_buffers[buf_idx],
1050 vdata, num_comps,
1051 so_write_offset[buf_idx],
1052 LLVMConstInt(i32, 0, 0),
1053 so->output[i].dst_offset*4);
1054 }
1055 }
1056 lp_build_endif(&if_ctx);
1057}
1058
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001059
Michel Dänzer404b29d2013-11-21 16:45:28 +09001060/* Generate export instructions for hardware VS shader stage */
1061static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1062 struct si_shader_output_values *outputs,
1063 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001064{
1065 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001066 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001067 struct lp_build_context * base = &bld_base->base;
1068 struct lp_build_context * uint =
1069 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001070 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001071 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001072 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001073 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001074 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001075 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001076 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001077 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001078
Michel Dänzer67e385b2014-01-08 17:48:21 +09001079 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1080 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001081 }
1082
Michel Dänzer404b29d2013-11-21 16:45:28 +09001083 for (i = 0; i < noutput; i++) {
1084 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001085 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001086
Michel Dänzer0afeea52013-05-02 14:53:17 +02001087handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001088 /* Select the correct target */
1089 switch(semantic_name) {
1090 case TGSI_SEMANTIC_PSIZE:
1091 shader->vs_out_misc_write = true;
1092 shader->vs_out_point_size = true;
1093 psize_value = outputs[i].values[0];
1094 continue;
1095 case TGSI_SEMANTIC_EDGEFLAG:
1096 shader->vs_out_misc_write = true;
1097 shader->vs_out_edgeflag = true;
1098 edgeflag_value = outputs[i].values[0];
1099 continue;
1100 case TGSI_SEMANTIC_LAYER:
1101 shader->vs_out_misc_write = true;
1102 shader->vs_out_layer = true;
1103 layer_value = outputs[i].values[0];
1104 continue;
1105 case TGSI_SEMANTIC_POSITION:
1106 target = V_008DFC_SQ_EXP_POS;
1107 break;
1108 case TGSI_SEMANTIC_COLOR:
1109 case TGSI_SEMANTIC_BCOLOR:
1110 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001111 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001112 param_count++;
1113 break;
1114 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001115 shader->clip_dist_write |=
Marek Olšáka9592cd2014-10-04 19:09:09 +02001116 0xf << (semantic_index * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001117 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1118 break;
1119 case TGSI_SEMANTIC_CLIPVERTEX:
1120 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1121 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001122 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001123 case TGSI_SEMANTIC_FOG:
1124 case TGSI_SEMANTIC_GENERIC:
1125 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001126 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001127 param_count++;
1128 break;
1129 default:
1130 target = 0;
1131 fprintf(stderr,
1132 "Warning: SI unhandled vs output type:%d\n",
1133 semantic_name);
1134 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001135
Michel Dänzer404b29d2013-11-21 16:45:28 +09001136 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001137
Michel Dänzer404b29d2013-11-21 16:45:28 +09001138 if (target >= V_008DFC_SQ_EXP_POS &&
1139 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1140 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1141 args, sizeof(args));
1142 } else {
1143 lp_build_intrinsic(base->gallivm->builder,
1144 "llvm.SI.export",
1145 LLVMVoidTypeInContext(base->gallivm->context),
1146 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001147 }
1148
1149 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1150 semantic_name = TGSI_SEMANTIC_GENERIC;
1151 goto handle_semantic;
1152 }
1153 }
1154
1155 /* We need to add the position output manually if it's missing. */
1156 if (!pos_args[0][0]) {
1157 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1158 pos_args[0][1] = uint->zero; /* EXEC mask */
1159 pos_args[0][2] = uint->zero; /* last export? */
1160 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1161 pos_args[0][4] = uint->zero; /* COMPR flag */
1162 pos_args[0][5] = base->zero; /* X */
1163 pos_args[0][6] = base->zero; /* Y */
1164 pos_args[0][7] = base->zero; /* Z */
1165 pos_args[0][8] = base->one; /* W */
1166 }
1167
1168 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1169 if (shader->vs_out_misc_write) {
1170 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1171 shader->vs_out_point_size |
1172 (shader->vs_out_edgeflag << 1) |
1173 (shader->vs_out_layer << 2));
1174 pos_args[1][1] = uint->zero; /* EXEC mask */
1175 pos_args[1][2] = uint->zero; /* last export? */
1176 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1177 pos_args[1][4] = uint->zero; /* COMPR flag */
1178 pos_args[1][5] = base->zero; /* X */
1179 pos_args[1][6] = base->zero; /* Y */
1180 pos_args[1][7] = base->zero; /* Z */
1181 pos_args[1][8] = base->zero; /* W */
1182
Michel Dänzer404b29d2013-11-21 16:45:28 +09001183 if (shader->vs_out_point_size)
1184 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001185
1186 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001187 /* The output is a float, but the hw expects an integer
1188 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001189 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1190 edgeflag_value,
1191 bld_base->uint_bld.elem_type, "");
1192 edgeflag_value = lp_build_min(&bld_base->int_bld,
1193 edgeflag_value,
1194 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001195
1196 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001197 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1198 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001199 base->elem_type, "");
1200 }
1201
Michel Dänzer404b29d2013-11-21 16:45:28 +09001202 if (shader->vs_out_layer)
1203 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001204 }
1205
1206 for (i = 0; i < 4; i++)
1207 if (pos_args[i][0])
1208 shader->nr_pos_exports++;
1209
1210 pos_idx = 0;
1211 for (i = 0; i < 4; i++) {
1212 if (!pos_args[i][0])
1213 continue;
1214
1215 /* Specify the target we are exporting */
1216 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1217
1218 if (pos_idx == shader->nr_pos_exports)
1219 /* Specify that this is the last export */
1220 pos_args[i][2] = uint->one;
1221
1222 lp_build_intrinsic(base->gallivm->builder,
1223 "llvm.SI.export",
1224 LLVMVoidTypeInContext(base->gallivm->context),
1225 pos_args[i], 9);
1226 }
1227}
1228
Michel Dänzer404b29d2013-11-21 16:45:28 +09001229static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1230{
1231 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1232 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001233 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001234 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001235 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001236 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1237 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001238 unsigned chan;
1239 int i;
1240
Marek Olšák216cf862014-10-04 17:04:05 +02001241 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001242 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001243 si_shader_ctx->radeon_bld.soa.outputs[i];
1244 int param_index = get_param_index(info->output_semantic_name[i],
1245 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001246 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001247
Marek Olšáke29353f2014-09-17 22:17:02 +02001248 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001249 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001250
1251 for (chan = 0; chan < 4; chan++) {
1252 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001253 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1254
Marek Olšákfc3b3352014-10-05 13:33:40 +02001255 build_tbuffer_store(si_shader_ctx,
1256 si_shader_ctx->esgs_ring,
1257 out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001258 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001259 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001260 V_008F0C_BUF_DATA_FORMAT_32,
1261 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001262 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001263 }
1264 }
1265}
1266
1267static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1268{
1269 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1270 struct gallivm_state *gallivm = bld_base->base.gallivm;
1271 LLVMValueRef args[2];
1272
1273 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1274 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1275 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1276 LLVMVoidTypeInContext(gallivm->context), args, 2,
1277 LLVMNoUnwindAttribute);
1278}
1279
1280static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1281{
1282 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1283 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001284 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001285 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001286 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001287
Marek Olšákec0d1682014-10-04 22:33:36 +02001288 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001289
Marek Olšákec0d1682014-10-04 22:33:36 +02001290 for (i = 0; i < info->num_outputs; i++) {
1291 outputs[i].name = info->output_semantic_name[i];
1292 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001293
Marek Olšákec0d1682014-10-04 22:33:36 +02001294 for (j = 0; j < 4; j++)
1295 outputs[i].values[j] =
1296 LLVMBuildLoad(gallivm->builder,
1297 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1298 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001299 }
1300
Marek Olšákec0d1682014-10-04 22:33:36 +02001301 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001302 FREE(outputs);
1303}
1304
Michel Dänzer51f89a02013-12-09 15:33:53 +09001305static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1306{
1307 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001308 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001309 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001310 struct lp_build_context * uint = &bld_base->uint_bld;
1311 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001312 LLVMValueRef args[9];
1313 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001314 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001315 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001316
Marek Olšák75e97e22014-10-04 23:13:50 +02001317 for (i = 0; i < info->num_outputs; i++) {
1318 unsigned semantic_name = info->output_semantic_name[i];
1319 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001320 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001321
Marek Olšák75e97e22014-10-04 23:13:50 +02001322 /* Select the correct target */
1323 switch (semantic_name) {
1324 case TGSI_SEMANTIC_POSITION:
1325 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001326 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001327 case TGSI_SEMANTIC_STENCIL:
1328 stencil_index = i;
1329 continue;
1330 case TGSI_SEMANTIC_SAMPLEMASK:
1331 samplemask_index = i;
1332 continue;
1333 case TGSI_SEMANTIC_COLOR:
1334 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1335 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1336 LLVMBuildStore(bld_base->base.gallivm->builder,
1337 bld_base->base.one,
1338 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001339
Marek Olšák75e97e22014-10-04 23:13:50 +02001340 if (semantic_index == 0 &&
1341 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1342 si_alpha_test(bld_base,
1343 si_shader_ctx->radeon_bld.soa.outputs[i]);
1344 break;
1345 default:
1346 target = 0;
1347 fprintf(stderr,
1348 "Warning: SI unhandled fs output type:%d\n",
1349 semantic_name);
1350 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001351
Marek Olšák75e97e22014-10-04 23:13:50 +02001352 si_llvm_init_export_args_load(bld_base,
1353 si_shader_ctx->radeon_bld.soa.outputs[i],
1354 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001355
Marek Olšák75e97e22014-10-04 23:13:50 +02001356 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1357 /* If there is an export instruction waiting to be emitted, do so now. */
1358 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001359 lp_build_intrinsic(base->gallivm->builder,
1360 "llvm.SI.export",
1361 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001362 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001363 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001364
1365 /* This instruction will be emitted at the end of the shader. */
1366 memcpy(last_args, args, sizeof(args));
1367
1368 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1369 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1370 semantic_index == 0 &&
1371 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1372 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1373 si_llvm_init_export_args_load(bld_base,
1374 si_shader_ctx->radeon_bld.soa.outputs[i],
1375 V_008DFC_SQ_EXP_MRT + c, args);
1376 lp_build_intrinsic(base->gallivm->builder,
1377 "llvm.SI.export",
1378 LLVMVoidTypeInContext(base->gallivm->context),
1379 args, 9);
1380 }
1381 }
1382 } else {
1383 lp_build_intrinsic(base->gallivm->builder,
1384 "llvm.SI.export",
1385 LLVMVoidTypeInContext(base->gallivm->context),
1386 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001387 }
1388 }
1389
Marek Olšákd0e8b652014-05-06 20:04:31 +02001390 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001391 LLVMValueRef out_ptr;
1392 unsigned mask = 0;
1393
1394 /* Specify the target we are exporting */
1395 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1396
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001397 args[5] = base->zero; /* R, depth */
1398 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1399 args[7] = base->zero; /* B, sample mask */
1400 args[8] = base->zero; /* A, alpha to mask */
1401
Michel Dänzer1a616c12012-11-13 17:35:09 +01001402 if (depth_index >= 0) {
1403 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1404 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1405 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001406 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001407 }
1408
1409 if (stencil_index >= 0) {
1410 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001411 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001412 /* Only setting the stencil component bit (0x2) here
1413 * breaks some stencil piglit tests
1414 */
1415 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001416 si_shader_ctx->shader->db_shader_control |=
1417 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001418 }
1419
Marek Olšákd0e8b652014-05-06 20:04:31 +02001420 if (samplemask_index >= 0) {
1421 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1422 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1423 mask |= 0xf; /* Set all components. */
1424 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1425 }
1426
1427 if (samplemask_index >= 0)
1428 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1429 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001430 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1431 else
1432 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1433
Michel Dänzer1a616c12012-11-13 17:35:09 +01001434 /* Specify which components to enable */
1435 args[0] = lp_build_const_int32(base->gallivm, mask);
1436
1437 args[1] =
1438 args[2] =
1439 args[4] = uint->zero;
1440
1441 if (last_args[0])
1442 lp_build_intrinsic(base->gallivm->builder,
1443 "llvm.SI.export",
1444 LLVMVoidTypeInContext(base->gallivm->context),
1445 args, 9);
1446 else
1447 memcpy(last_args, args, sizeof(args));
1448 }
1449
Michel Dänzer51f89a02013-12-09 15:33:53 +09001450 if (!last_args[0]) {
1451 /* Specify which components to enable */
1452 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001453
Michel Dänzer51f89a02013-12-09 15:33:53 +09001454 /* Specify the target we are exporting */
1455 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001456
Michel Dänzer51f89a02013-12-09 15:33:53 +09001457 /* Set COMPR flag to zero to export data as 32-bit */
1458 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001459
Michel Dänzer51f89a02013-12-09 15:33:53 +09001460 /* dummy bits */
1461 last_args[5]= uint->zero;
1462 last_args[6]= uint->zero;
1463 last_args[7]= uint->zero;
1464 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001465 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001466
1467 /* Specify whether the EXEC mask represents the valid mask */
1468 last_args[1] = uint->one;
1469
1470 /* Specify that this is the last export */
1471 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1472
1473 lp_build_intrinsic(base->gallivm->builder,
1474 "llvm.SI.export",
1475 LLVMVoidTypeInContext(base->gallivm->context),
1476 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001477}
1478
Marek Olšák4855acd2013-08-06 15:08:54 +02001479static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1480 struct lp_build_tgsi_context * bld_base,
1481 struct lp_build_emit_data * emit_data);
1482
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001483static bool tgsi_is_shadow_sampler(unsigned target)
1484{
1485 return target == TGSI_TEXTURE_SHADOW1D ||
1486 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1487 target == TGSI_TEXTURE_SHADOW2D ||
1488 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1489 target == TGSI_TEXTURE_SHADOWCUBE ||
1490 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1491 target == TGSI_TEXTURE_SHADOWRECT;
1492}
1493
Marek Olšák877bb522014-06-25 03:12:46 +02001494static const struct lp_build_tgsi_action tex_action;
1495
Tom Stellarda75c6162012-01-06 17:38:37 -05001496static void tex_fetch_args(
1497 struct lp_build_tgsi_context * bld_base,
1498 struct lp_build_emit_data * emit_data)
1499{
Christian König55fe5cc2013-03-04 16:30:06 +01001500 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001501 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001502 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001503 unsigned opcode = inst->Instruction.Opcode;
1504 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001505 LLVMValueRef coords[4];
1506 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001507 int ref_pos;
1508 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001509 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001510 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001511 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1512 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001513 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001514
1515 if (target == TGSI_TEXTURE_BUFFER) {
1516 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1517 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1518 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1519 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1520
Marek Olšák4f3f0432014-07-07 22:49:15 +02001521 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001522 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1523 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1524 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1525 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1526
1527 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1528 emit_data->args[0] = res;
1529 emit_data->args[1] = bld_base->uint_bld.zero;
1530 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1531 emit_data->arg_count = 3;
1532 return;
1533 }
Tom Stellard467f5162012-05-16 15:15:35 -04001534
Michel Dänzer120efee2013-01-25 12:10:11 +01001535 /* Fetch and project texture coordinates */
1536 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001537 for (chan = 0; chan < 3; chan++ ) {
1538 coords[chan] = lp_build_emit_fetch(bld_base,
1539 emit_data->inst, 0,
1540 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001541 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001542 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1543 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001544 coords[chan],
1545 coords[3]);
1546 }
1547
Michel Dänzer120efee2013-01-25 12:10:11 +01001548 if (opcode == TGSI_OPCODE_TXP)
1549 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001550
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001551 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001552 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001553 /* The offsets are six-bit signed integers packed like this:
1554 * X=[5:0], Y=[13:8], and Z=[21:16].
1555 */
1556 LLVMValueRef offset[3], pack;
1557
1558 assert(inst->Texture.NumOffsets == 1);
1559
1560 for (chan = 0; chan < 3; chan++) {
1561 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1562 emit_data->inst, 0, chan);
1563 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1564 lp_build_const_int32(gallivm, 0x3f), "");
1565 if (chan)
1566 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1567 lp_build_const_int32(gallivm, chan*8), "");
1568 }
1569
1570 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1571 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1572 address[count++] = pack;
1573 }
1574
Michel Dänzer120efee2013-01-25 12:10:11 +01001575 /* Pack LOD bias value */
1576 if (opcode == TGSI_OPCODE_TXB)
1577 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001578 if (opcode == TGSI_OPCODE_TXB2)
1579 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001580
Michel Dänzer120efee2013-01-25 12:10:11 +01001581 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001582 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001583 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1584 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1585 } else {
1586 assert(ref_pos >= 0);
1587 address[count++] = coords[ref_pos];
1588 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001589 }
1590
Marek Olšákb279f0142014-07-04 03:19:58 +02001591 if (target == TGSI_TEXTURE_CUBE ||
1592 target == TGSI_TEXTURE_CUBE_ARRAY ||
1593 target == TGSI_TEXTURE_SHADOWCUBE ||
1594 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1595 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1596
Michel Dänzera6b83c02013-02-21 16:10:55 +01001597 /* Pack user derivatives */
1598 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001599 int num_deriv_channels, param;
1600
1601 switch (target) {
1602 case TGSI_TEXTURE_3D:
1603 num_deriv_channels = 3;
1604 break;
1605 case TGSI_TEXTURE_2D:
1606 case TGSI_TEXTURE_SHADOW2D:
1607 case TGSI_TEXTURE_RECT:
1608 case TGSI_TEXTURE_SHADOWRECT:
1609 case TGSI_TEXTURE_2D_ARRAY:
1610 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1611 case TGSI_TEXTURE_CUBE:
1612 case TGSI_TEXTURE_SHADOWCUBE:
1613 case TGSI_TEXTURE_CUBE_ARRAY:
1614 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1615 num_deriv_channels = 2;
1616 break;
1617 case TGSI_TEXTURE_1D:
1618 case TGSI_TEXTURE_SHADOW1D:
1619 case TGSI_TEXTURE_1D_ARRAY:
1620 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1621 num_deriv_channels = 1;
1622 break;
1623 default:
1624 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001625 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001626
1627 for (param = 1; param <= 2; param++)
1628 for (chan = 0; chan < num_deriv_channels; chan++)
1629 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001630 }
1631
Michel Dänzer120efee2013-01-25 12:10:11 +01001632 /* Pack texture coordinates */
1633 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001634 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001635 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001636 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001637 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001638
Marek Olšákd2bd6342013-09-18 15:40:21 +02001639 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001640 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001641 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001642 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001643 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001644
1645 if (count > 16) {
1646 assert(!"Cannot handle more than 16 texture address parameters");
1647 count = 16;
1648 }
1649
1650 for (chan = 0; chan < count; chan++ ) {
1651 address[chan] = LLVMBuildBitCast(gallivm->builder,
1652 address[chan],
1653 LLVMInt32TypeInContext(gallivm->context),
1654 "");
1655 }
1656
Marek Olšák4855acd2013-08-06 15:08:54 +02001657 /* Adjust the sample index according to FMASK.
1658 *
1659 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1660 * which is the identity mapping. Each nibble says which physical sample
1661 * should be fetched to get that sample.
1662 *
1663 * For example, 0x11111100 means there are only 2 samples stored and
1664 * the second sample covers 3/4 of the pixel. When reading samples 0
1665 * and 1, return physical sample 0 (determined by the first two 0s
1666 * in FMASK), otherwise return physical sample 1.
1667 *
1668 * The sample index should be adjusted as follows:
1669 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1670 */
1671 if (target == TGSI_TEXTURE_2D_MSAA ||
1672 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1673 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1674 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001675 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001676 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001677 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001678
Marek Olšákd2bd6342013-09-18 15:40:21 +02001679 memcpy(txf_address, address, sizeof(txf_address));
1680
1681 if (target == TGSI_TEXTURE_2D_MSAA) {
1682 txf_address[2] = bld_base->uint_bld.zero;
1683 }
1684 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001685
1686 /* Pad to a power-of-two size. */
1687 while (txf_count < util_next_power_of_two(txf_count))
1688 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1689
1690 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001691 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1692 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1693 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001694 txf_emit_data.chan = 0;
1695 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001696 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001697 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001698 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001699 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001700 txf_emit_data.arg_count = 3;
1701
Marek Olšák877bb522014-06-25 03:12:46 +02001702 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001703
1704 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001705 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1706 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1707
1708 /* Apply the formula. */
1709 LLVMValueRef fmask =
1710 LLVMBuildExtractElement(gallivm->builder,
1711 txf_emit_data.output[0],
1712 uint_bld->zero, "");
1713
Marek Olšákd2bd6342013-09-18 15:40:21 +02001714 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001715
1716 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001717 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001718
1719 LLVMValueRef shifted_fmask =
1720 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1721
1722 LLVMValueRef final_sample =
1723 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1724
1725 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1726 * resource descriptor is 0 (invalid),
1727 */
1728 LLVMValueRef fmask_desc =
1729 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001730 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001731 LLVMVectorType(uint_bld->elem_type, 8), "");
1732
1733 LLVMValueRef fmask_word1 =
1734 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1735 uint_bld->one, "");
1736
1737 LLVMValueRef word1_is_nonzero =
1738 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1739 fmask_word1, uint_bld->zero, "");
1740
Marek Olšákd2bd6342013-09-18 15:40:21 +02001741 /* Replace the MSAA sample index. */
1742 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001743 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001744 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001745 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001746
Michel Dänzer36231112013-05-02 09:44:45 +02001747 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001748 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001749
1750 if (opcode == TGSI_OPCODE_TXF) {
1751 /* add tex offsets */
1752 if (inst->Texture.NumOffsets) {
1753 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1754 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1755 const struct tgsi_texture_offset * off = inst->TexOffsets;
1756
1757 assert(inst->Texture.NumOffsets == 1);
1758
Marek Olšákdefedc02013-09-18 15:36:38 +02001759 switch (target) {
1760 case TGSI_TEXTURE_3D:
1761 address[2] = lp_build_add(uint_bld, address[2],
1762 bld->immediates[off->Index][off->SwizzleZ]);
1763 /* fall through */
1764 case TGSI_TEXTURE_2D:
1765 case TGSI_TEXTURE_SHADOW2D:
1766 case TGSI_TEXTURE_RECT:
1767 case TGSI_TEXTURE_SHADOWRECT:
1768 case TGSI_TEXTURE_2D_ARRAY:
1769 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001770 address[1] =
1771 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001772 bld->immediates[off->Index][off->SwizzleY]);
1773 /* fall through */
1774 case TGSI_TEXTURE_1D:
1775 case TGSI_TEXTURE_SHADOW1D:
1776 case TGSI_TEXTURE_1D_ARRAY:
1777 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1778 address[0] =
1779 lp_build_add(uint_bld, address[0],
1780 bld->immediates[off->Index][off->SwizzleX]);
1781 break;
1782 /* texture offsets do not apply to other texture targets */
1783 }
Michel Dänzer36231112013-05-02 09:44:45 +02001784 }
1785
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001786 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1787 emit_data->arg_count = 3;
1788
Michel Dänzer36231112013-05-02 09:44:45 +02001789 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001790 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001791 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001792 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001793 opcode == TGSI_OPCODE_LODQ ||
1794 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001795 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1796 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1797 target == TGSI_TEXTURE_2D_ARRAY ||
1798 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1799 target == TGSI_TEXTURE_CUBE_ARRAY ||
1800 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1801 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001802 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001803
Marek Olšák57f3da92014-06-16 16:53:42 +02001804 if (opcode == TGSI_OPCODE_TG4) {
1805 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001806
Marek Olšák57f3da92014-06-16 16:53:42 +02001807 /* DMASK was repurposed for GATHER4. 4 components are always
1808 * returned and DMASK works like a swizzle - it selects
1809 * the component to fetch. The only valid DMASK values are
1810 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1811 * (red,red,red,red) etc.) The ISA document doesn't mention
1812 * this.
1813 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001814
Marek Olšák57f3da92014-06-16 16:53:42 +02001815 /* Get the component index from src1.x for Gather4. */
1816 if (!tgsi_is_shadow_sampler(target)) {
1817 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1818 LLVMValueRef comp_imm;
1819 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001820
Marek Olšák57f3da92014-06-16 16:53:42 +02001821 assert(src1.File == TGSI_FILE_IMMEDIATE);
1822
1823 comp_imm = imms[src1.Index][src1.SwizzleX];
1824 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1825 gather_comp = CLAMP(gather_comp, 0, 3);
1826 }
1827
1828 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001829 }
1830
Marek Olšák4855acd2013-08-06 15:08:54 +02001831 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001832 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001833 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1834 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1835 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1836 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1837 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1838 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1839 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1840
1841 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001842
1843 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001844 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001845 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001846 } else {
1847 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1848 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001849 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001850
1851 emit_data->dst_type = LLVMVectorType(
1852 LLVMFloatTypeInContext(gallivm->context),
1853 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001854 }
1855
Marek Olšák2484daa2014-04-22 21:23:29 +02001856 /* The fetch opcode has been converted to a 2D array fetch.
1857 * This simplifies the LLVM backend. */
1858 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1859 target = TGSI_TEXTURE_2D_ARRAY;
1860 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1861 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1862
Michel Dänzer120efee2013-01-25 12:10:11 +01001863 /* Pad to power of two vector */
1864 while (count < util_next_power_of_two(count))
1865 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1866
Christian Königccf3e8f2013-03-26 15:09:27 +01001867 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001868}
1869
Michel Dänzer07eddc42013-02-06 15:43:10 +01001870static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1871 struct lp_build_tgsi_context * bld_base,
1872 struct lp_build_emit_data * emit_data)
1873{
1874 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001875 unsigned opcode = emit_data->inst->Instruction.Opcode;
1876 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001877 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001878 bool has_offset = HAVE_LLVM >= 0x0305 ?
1879 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001880
Marek Olšák877bb522014-06-25 03:12:46 +02001881 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001882 emit_data->output[emit_data->chan] = build_intrinsic(
1883 base->gallivm->builder,
1884 "llvm.SI.vs.load.input", emit_data->dst_type,
1885 emit_data->args, emit_data->arg_count,
1886 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1887 return;
1888 }
1889
Marek Olšák877bb522014-06-25 03:12:46 +02001890 if (opcode == TGSI_OPCODE_TG4 ||
1891 opcode == TGSI_OPCODE_LODQ ||
1892 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1893 bool is_shadow = tgsi_is_shadow_sampler(target);
1894 const char *name = "llvm.SI.image.sample";
1895 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001896
Marek Olšák877bb522014-06-25 03:12:46 +02001897 switch (opcode) {
1898 case TGSI_OPCODE_TEX:
1899 case TGSI_OPCODE_TEX2:
1900 case TGSI_OPCODE_TXP:
1901 break;
1902 case TGSI_OPCODE_TXB:
1903 case TGSI_OPCODE_TXB2:
1904 infix = ".b";
1905 break;
1906 case TGSI_OPCODE_TXL:
1907 case TGSI_OPCODE_TXL2:
1908 infix = ".l";
1909 break;
1910 case TGSI_OPCODE_TXD:
1911 infix = ".d";
1912 break;
1913 case TGSI_OPCODE_TG4:
1914 name = "llvm.SI.gather4";
1915 break;
1916 case TGSI_OPCODE_LODQ:
1917 name = "llvm.SI.getlod";
1918 is_shadow = false;
1919 has_offset = false;
1920 break;
1921 default:
1922 assert(0);
1923 return;
1924 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001925
Marek Olšák877bb522014-06-25 03:12:46 +02001926 /* Add the type and suffixes .c, .o if needed. */
1927 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1928 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1929 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001930
Marek Olšák877bb522014-06-25 03:12:46 +02001931 emit_data->output[emit_data->chan] = build_intrinsic(
1932 base->gallivm->builder, intr_name, emit_data->dst_type,
1933 emit_data->args, emit_data->arg_count,
1934 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1935 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001936 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001937 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001938
Marek Olšák877bb522014-06-25 03:12:46 +02001939 switch (opcode) {
1940 case TGSI_OPCODE_TEX:
1941 case TGSI_OPCODE_TEX2:
1942 case TGSI_OPCODE_TXP:
1943 name = "llvm.SI.sample";
1944 break;
1945 case TGSI_OPCODE_TXB:
1946 case TGSI_OPCODE_TXB2:
1947 name = "llvm.SI.sampleb";
1948 break;
1949 case TGSI_OPCODE_TXD:
1950 name = "llvm.SI.sampled";
1951 break;
1952 case TGSI_OPCODE_TXF:
1953 name = "llvm.SI.imageload";
1954 break;
1955 case TGSI_OPCODE_TXL:
1956 case TGSI_OPCODE_TXL2:
1957 name = "llvm.SI.samplel";
1958 break;
1959 default:
1960 assert(0);
1961 return;
1962 }
1963
Marek Olšákd859bdb2014-07-14 19:40:14 +02001964 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1965 v16i8 = LLVMVectorType(i8, 16);
1966 v32i8 = LLVMVectorType(i8, 32);
1967
1968 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1969 emit_data->args[1], v32i8, "");
1970 if (opcode != TGSI_OPCODE_TXF) {
1971 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1972 emit_data->args[2], v16i8, "");
1973 }
1974
Marek Olšák877bb522014-06-25 03:12:46 +02001975 sprintf(intr_name, "%s.v%ui32", name,
1976 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1977
1978 emit_data->output[emit_data->chan] = build_intrinsic(
1979 base->gallivm->builder, intr_name, emit_data->dst_type,
1980 emit_data->args, emit_data->arg_count,
1981 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1982 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001983}
1984
Michel Dänzer0495adb2013-05-06 12:45:14 +02001985static void txq_fetch_args(
1986 struct lp_build_tgsi_context * bld_base,
1987 struct lp_build_emit_data * emit_data)
1988{
1989 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1990 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001991 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02001992 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001993
Marek Olšák2484daa2014-04-22 21:23:29 +02001994 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001995 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1996 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1997
1998 /* Read the size from the buffer descriptor directly. */
1999 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2000 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2001 size = LLVMBuildExtractElement(gallivm->builder, size,
2002 lp_build_const_int32(gallivm, 2), "");
2003 emit_data->args[0] = size;
2004 return;
2005 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002006
2007 /* Mip level */
2008 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2009
2010 /* Resource */
2011 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2012
Marek Olšák2484daa2014-04-22 21:23:29 +02002013 /* Texture target */
2014 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2015 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2016 target = TGSI_TEXTURE_2D_ARRAY;
2017
Michel Dänzer0495adb2013-05-06 12:45:14 +02002018 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002019 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002020
2021 emit_data->arg_count = 3;
2022
2023 emit_data->dst_type = LLVMVectorType(
2024 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2025 4);
2026}
2027
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002028static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2029 struct lp_build_tgsi_context * bld_base,
2030 struct lp_build_emit_data * emit_data)
2031{
Marek Olšák2484daa2014-04-22 21:23:29 +02002032 unsigned target = emit_data->inst->Texture.Texture;
2033
2034 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002035 /* Just return the buffer size. */
2036 emit_data->output[emit_data->chan] = emit_data->args[0];
2037 return;
2038 }
2039
2040 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002041
2042 /* Divide the number of layers by 6 to get the number of cubes. */
2043 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2044 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2045 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2046 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2047 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2048
2049 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2050 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2051 z = LLVMBuildSDiv(builder, z, six, "");
2052
2053 emit_data->output[emit_data->chan] =
2054 LLVMBuildInsertElement(builder, v4, z, two, "");
2055 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002056}
2057
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002058static void si_llvm_emit_ddxy(
2059 const struct lp_build_tgsi_action * action,
2060 struct lp_build_tgsi_context * bld_base,
2061 struct lp_build_emit_data * emit_data)
2062{
2063 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2064 struct gallivm_state *gallivm = bld_base->base.gallivm;
2065 struct lp_build_context * base = &bld_base->base;
2066 const struct tgsi_full_instruction *inst = emit_data->inst;
2067 unsigned opcode = inst->Instruction.Opcode;
2068 LLVMValueRef indices[2];
2069 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2070 LLVMValueRef tl, trbl, result[4];
2071 LLVMTypeRef i32;
2072 unsigned swizzle[4];
2073 unsigned c;
2074
2075 i32 = LLVMInt32TypeInContext(gallivm->context);
2076
2077 indices[0] = bld_base->uint_bld.zero;
2078 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2079 NULL, 0, LLVMReadNoneAttribute);
2080 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2081 indices, 2, "");
2082
2083 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2084 lp_build_const_int32(gallivm, 0xfffffffc), "");
2085 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2086 indices, 2, "");
2087
2088 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2089 lp_build_const_int32(gallivm,
2090 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2091 "");
2092 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2093 indices, 2, "");
2094
2095 for (c = 0; c < 4; ++c) {
2096 unsigned i;
2097
2098 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2099 for (i = 0; i < c; ++i) {
2100 if (swizzle[i] == swizzle[c]) {
2101 result[c] = result[i];
2102 break;
2103 }
2104 }
2105 if (i != c)
2106 continue;
2107
2108 LLVMBuildStore(gallivm->builder,
2109 LLVMBuildBitCast(gallivm->builder,
2110 lp_build_emit_fetch(bld_base, inst, 0, c),
2111 i32, ""),
2112 store_ptr);
2113
2114 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2115 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2116
2117 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2118 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2119
2120 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2121 }
2122
2123 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2124}
2125
Michel Dänzer404b29d2013-11-21 16:45:28 +09002126/* Emit one vertex from the geometry shader */
2127static void si_llvm_emit_vertex(
2128 const struct lp_build_tgsi_action *action,
2129 struct lp_build_tgsi_context *bld_base,
2130 struct lp_build_emit_data *emit_data)
2131{
2132 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002133 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002134 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002135 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002136 struct gallivm_state *gallivm = bld_base->base.gallivm;
2137 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002138 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2139 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002140 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002141 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002142 LLVMValueRef args[2];
2143 unsigned chan;
2144 int i;
2145
Michel Dänzer404b29d2013-11-21 16:45:28 +09002146 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002147 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002148
2149 /* If this thread has already emitted the declared maximum number of
2150 * vertices, kill it: excessive vertex emissions are not supposed to
2151 * have any effect, and GS threads have no externally observable
2152 * effects other than emitting vertices.
2153 */
2154 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2155 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002156 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002157 kill = lp_build_select(&bld_base->base, can_emit,
2158 lp_build_const_float(gallivm, 1.0f),
2159 lp_build_const_float(gallivm, -1.0f));
2160 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2161 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2162
Marek Olšák02134cf2014-10-04 22:07:50 +02002163 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002164 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002165 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002166
2167 for (chan = 0; chan < 4; chan++) {
2168 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002169 LLVMValueRef voffset =
2170 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002171 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002172
2173 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2174 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002175
2176 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2177
Marek Olšákfc3b3352014-10-05 13:33:40 +02002178 build_tbuffer_store(si_shader_ctx,
2179 si_shader_ctx->gsvs_ring,
2180 out_val, 1,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002181 voffset, soffset, 0,
2182 V_008F0C_BUF_DATA_FORMAT_32,
2183 V_008F0C_BUF_NUM_FORMAT_UINT,
2184 1, 0, 1, 1, 0);
2185 }
2186 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002187 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2188 lp_build_const_int32(gallivm, 1));
2189 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002190
2191 /* Signal vertex emission */
2192 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2193 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2194 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2195 LLVMVoidTypeInContext(gallivm->context), args, 2,
2196 LLVMNoUnwindAttribute);
2197}
2198
2199/* Cut one primitive from the geometry shader */
2200static void si_llvm_emit_primitive(
2201 const struct lp_build_tgsi_action *action,
2202 struct lp_build_tgsi_context *bld_base,
2203 struct lp_build_emit_data *emit_data)
2204{
2205 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2206 struct gallivm_state *gallivm = bld_base->base.gallivm;
2207 LLVMValueRef args[2];
2208
2209 /* Signal primitive cut */
2210 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2211 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2212 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2213 LLVMVoidTypeInContext(gallivm->context), args, 2,
2214 LLVMNoUnwindAttribute);
2215}
2216
Tom Stellarda75c6162012-01-06 17:38:37 -05002217static const struct lp_build_tgsi_action tex_action = {
2218 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002219 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002220};
2221
Michel Dänzer0495adb2013-05-06 12:45:14 +02002222static const struct lp_build_tgsi_action txq_action = {
2223 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002224 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002225 .intr_name = "llvm.SI.resinfo"
2226};
2227
Christian König206f0592013-03-20 14:37:21 +01002228static void create_meta_data(struct si_shader_context *si_shader_ctx)
2229{
2230 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2231 LLVMValueRef args[3];
2232
2233 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2234 args[1] = 0;
2235 args[2] = lp_build_const_int32(gallivm, 1);
2236
2237 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2238}
2239
Marek Olšák4f3f0432014-07-07 22:49:15 +02002240static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2241{
2242 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2243 CONST_ADDR_SPACE);
2244}
2245
Christian König55fe5cc2013-03-04 16:30:06 +01002246static void create_function(struct si_shader_context *si_shader_ctx)
2247{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002248 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2249 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002250 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002251 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002252 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002253
Christian König55fe5cc2013-03-04 16:30:06 +01002254 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002255 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002256 f32 = LLVMFloatTypeInContext(gallivm->context);
2257 v2i32 = LLVMVectorType(i32, 2);
2258 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002259 v4i32 = LLVMVectorType(i32, 4);
2260 v8i32 = LLVMVectorType(i32, 8);
2261 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002262
Marek Olšákee2a8182014-07-07 23:27:19 +02002263 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002264 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002265 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2266 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002267 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002268
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002269 switch (si_shader_ctx->type) {
2270 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002271 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002272 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002273 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002274 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002275 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002276
Michel Dänzer404b29d2013-11-21 16:45:28 +09002277 if (shader->key.vs.as_es) {
2278 params[SI_PARAM_ES2GS_OFFSET] = i32;
2279 num_params++;
2280 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002281 if (shader->is_gs_copy_shader) {
2282 last_array_pointer = SI_PARAM_CONST;
2283 num_params = SI_PARAM_CONST+1;
2284 }
2285
Michel Dänzer404b29d2013-11-21 16:45:28 +09002286 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002287
Michel Dänzer404b29d2013-11-21 16:45:28 +09002288 /* Streamout SGPRs. */
2289 if (shader->selector->so.num_outputs) {
2290 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2291 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2292 }
2293 /* A streamout buffer offset is loaded if the stride is non-zero. */
2294 for (i = 0; i < 4; i++) {
2295 if (!shader->selector->so.stride[i])
2296 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002297
Michel Dänzer404b29d2013-11-21 16:45:28 +09002298 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2299 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002300 }
2301
2302 last_sgpr = num_params-1;
2303
2304 /* VGPRs */
2305 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2306 params[num_params++] = i32; /* unused*/
2307 params[num_params++] = i32; /* unused */
2308 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002309 break;
Christian König0666ffd2013-03-05 15:07:39 +01002310
Michel Dänzer404b29d2013-11-21 16:45:28 +09002311 case TGSI_PROCESSOR_GEOMETRY:
2312 params[SI_PARAM_GS2VS_OFFSET] = i32;
2313 params[SI_PARAM_GS_WAVE_ID] = i32;
2314 last_sgpr = SI_PARAM_GS_WAVE_ID;
2315
2316 /* VGPRs */
2317 params[SI_PARAM_VTX0_OFFSET] = i32;
2318 params[SI_PARAM_VTX1_OFFSET] = i32;
2319 params[SI_PARAM_PRIMITIVE_ID] = i32;
2320 params[SI_PARAM_VTX2_OFFSET] = i32;
2321 params[SI_PARAM_VTX3_OFFSET] = i32;
2322 params[SI_PARAM_VTX4_OFFSET] = i32;
2323 params[SI_PARAM_VTX5_OFFSET] = i32;
2324 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2325 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2326 break;
2327
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002328 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002329 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002330 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002331 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002332 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2333 params[SI_PARAM_PERSP_CENTER] = v2i32;
2334 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2335 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2336 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2337 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2338 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2339 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2340 params[SI_PARAM_POS_X_FLOAT] = f32;
2341 params[SI_PARAM_POS_Y_FLOAT] = f32;
2342 params[SI_PARAM_POS_Z_FLOAT] = f32;
2343 params[SI_PARAM_POS_W_FLOAT] = f32;
2344 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002345 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002346 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2347 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002348 num_params = SI_PARAM_POS_FIXED_PT+1;
2349 break;
2350
2351 default:
2352 assert(0 && "unimplemented shader");
2353 return;
Christian Königc4973212013-03-05 12:14:02 +01002354 }
Christian König55fe5cc2013-03-04 16:30:06 +01002355
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002356 assert(num_params <= Elements(params));
2357 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002358 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002359
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002360 for (i = 0; i <= last_sgpr; ++i) {
2361 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002362
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002363 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2364 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002365 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002366 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002367 else
2368 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002369 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002370
Michel Dänzer404b29d2013-11-21 16:45:28 +09002371 if (bld_base->info &&
2372 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2373 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002374 si_shader_ctx->ddxy_lds =
2375 LLVMAddGlobalInAddressSpace(gallivm->module,
2376 LLVMArrayType(i32, 64),
2377 "ddxy_lds",
2378 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002379}
Tom Stellarda75c6162012-01-06 17:38:37 -05002380
Christian König0f6cf2b2013-03-15 15:53:25 +01002381static void preload_constants(struct si_shader_context *si_shader_ctx)
2382{
2383 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2384 struct gallivm_state * gallivm = bld_base->base.gallivm;
2385 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002386 unsigned buf;
2387 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002388
Marek Olšákee2a8182014-07-07 23:27:19 +02002389 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002390 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002391
Marek Olšák2fd42002013-10-25 11:45:47 +02002392 if (num_const == 0)
2393 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002394
Marek Olšák2fd42002013-10-25 11:45:47 +02002395 /* Allocate space for the constant values */
2396 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002397
Marek Olšák2fd42002013-10-25 11:45:47 +02002398 /* Load the resource descriptor */
2399 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002400 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002401
Marek Olšák2fd42002013-10-25 11:45:47 +02002402 /* Load the constants, we rely on the code sinking to do the rest */
2403 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002404 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002405 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002406 si_shader_ctx->const_resource[buf],
2407 lp_build_const_int32(gallivm, i * 4),
2408 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002409 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002410 }
2411}
2412
Christian König1c100182013-03-17 16:02:42 +01002413static void preload_samplers(struct si_shader_context *si_shader_ctx)
2414{
2415 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2416 struct gallivm_state * gallivm = bld_base->base.gallivm;
2417 const struct tgsi_shader_info * info = bld_base->info;
2418
2419 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2420
2421 LLVMValueRef res_ptr, samp_ptr;
2422 LLVMValueRef offset;
2423
2424 if (num_samplers == 0)
2425 return;
2426
Christian König1c100182013-03-17 16:02:42 +01002427 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2428 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2429
2430 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2431 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002432 /* Resource */
2433 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002434 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002435
2436 /* Sampler */
2437 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002438 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002439
2440 /* FMASK resource */
2441 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002442 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2443 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002444 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002445 }
Christian König1c100182013-03-17 16:02:42 +01002446 }
2447}
2448
Marek Olšák8d03d922013-09-01 23:59:06 +02002449static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2450{
2451 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2452 struct gallivm_state * gallivm = bld_base->base.gallivm;
2453 unsigned i;
2454
Michel Dänzer67e385b2014-01-08 17:48:21 +09002455 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2456 si_shader_ctx->shader->key.vs.as_es ||
2457 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002458 return;
2459
2460 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002461 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002462
2463 /* Load the resources, we rely on the code sinking to do the rest */
2464 for (i = 0; i < 4; ++i) {
2465 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002466 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002467 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002468
Marek Olšákd7876082014-09-26 23:06:32 +02002469 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002470 }
2471 }
2472}
2473
Marek Olšákfc3b3352014-10-05 13:33:40 +02002474/**
2475 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
2476 * for later use.
2477 */
2478static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
2479{
2480 struct gallivm_state *gallivm =
2481 si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2482
2483 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2484 SI_PARAM_RW_BUFFERS);
2485
2486 if ((si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
2487 si_shader_ctx->shader->key.vs.as_es) ||
2488 si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY) {
2489 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_ESGS);
2490
2491 si_shader_ctx->esgs_ring =
2492 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2493 }
2494
2495 if (si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY ||
2496 si_shader_ctx->shader->is_gs_copy_shader) {
2497 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_GSVS);
2498
2499 si_shader_ctx->gsvs_ring =
2500 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2501 }
2502}
2503
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002504void si_shader_binary_read_config(const struct radeon_shader_binary *binary,
2505 struct si_shader *shader,
2506 unsigned symbol_offset)
Tom Stellard302f53d2012-10-25 13:50:10 -04002507{
Tom Stellard302f53d2012-10-25 13:50:10 -04002508 unsigned i;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002509 const unsigned char *config =
2510 radeon_shader_binary_config_start(binary, symbol_offset);
Tom Stellard302f53d2012-10-25 13:50:10 -04002511
Tom Stellardd50343d2013-04-04 16:21:06 -04002512 /* XXX: We may be able to emit some of these values directly rather than
2513 * extracting fields to be emitted later.
2514 */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002515
2516 for (i = 0; i < binary->config_size_per_symbol; i+= 8) {
2517 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
2518 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
Tom Stellardd50343d2013-04-04 16:21:06 -04002519 switch (reg) {
2520 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2521 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2522 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2523 case R_00B848_COMPUTE_PGM_RSRC1:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002524 shader->num_sgprs = MAX2(shader->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
2525 shader->num_vgprs = MAX2(shader->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
Tom Stellardd50343d2013-04-04 16:21:06 -04002526 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002527 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002528 shader->lds_size = MAX2(shader->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002529 break;
2530 case R_00B84C_COMPUTE_PGM_RSRC2:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002531 shader->lds_size = MAX2(shader->lds_size, G_00B84C_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002532 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002533 case R_0286CC_SPI_PS_INPUT_ENA:
2534 shader->spi_ps_input_ena = value;
2535 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002536 case R_00B860_COMPUTE_TMPRING_SIZE:
2537 /* WAVESIZE is in units of 256 dwords. */
2538 shader->scratch_bytes_per_wave =
2539 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2540 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002541 default:
2542 fprintf(stderr, "Warning: Compiler emitted unknown "
2543 "config register: 0x%x\n", reg);
2544 break;
2545 }
2546 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002547}
2548
2549int si_shader_binary_read(struct si_screen *sscreen,
2550 struct si_shader *shader,
2551 const struct radeon_shader_binary *binary)
2552{
2553
2554 unsigned i;
2555 unsigned code_size;
2556 unsigned char *ptr;
2557 bool dump = r600_can_dump_shader(&sscreen->b,
2558 shader->selector ? shader->selector->tokens : NULL);
2559
2560 if (dump && !binary->disassembled) {
2561 fprintf(stderr, "SI CODE:\n");
2562 for (i = 0; i < binary->code_size; i+=4 ) {
2563 fprintf(stderr, "@0x%x: %02x%02x%02x%02x\n", i, binary->code[i + 3],
2564 binary->code[i + 2], binary->code[i + 1],
2565 binary->code[i]);
2566 }
2567 }
2568
2569 si_shader_binary_read_config(binary, shader, 0);
Tom Stellard302f53d2012-10-25 13:50:10 -04002570
2571 /* copy new shader */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002572 code_size = binary->code_size + binary->rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002573 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002574 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002575 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002576 if (shader->bo == NULL) {
2577 return -ENOMEM;
2578 }
2579
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002580
2581 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_READ_WRITE);
2582 util_memcpy_cpu_to_le32(ptr, binary->code, binary->code_size);
2583 if (binary->rodata_size > 0) {
2584 ptr += binary->code_size;
2585 util_memcpy_cpu_to_le32(ptr, binary->rodata, binary->rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002586 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002587
Marek Olšák1abb1a92014-09-17 22:44:22 +02002588 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002589
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002590 return 0;
2591}
Tom Stellard302f53d2012-10-25 13:50:10 -04002592
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002593int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2594 LLVMModuleRef mod)
2595{
2596 int r = 0;
2597 struct radeon_shader_binary binary;
2598 bool dump = r600_can_dump_shader(&sscreen->b,
2599 shader->selector ? shader->selector->tokens : NULL);
2600 memset(&binary, 0, sizeof(binary));
2601 r = radeon_llvm_compile(mod, &binary,
2602 r600_get_llvm_processor_name(sscreen->b.family), dump);
2603
2604 if (r) {
2605 return r;
2606 }
2607 r = si_shader_binary_read(sscreen, shader, &binary);
2608 FREE(binary.code);
2609 FREE(binary.config);
2610 FREE(binary.rodata);
Darren Powellbc866902014-03-31 18:00:28 -04002611 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002612}
2613
Michel Dänzer404b29d2013-11-21 16:45:28 +09002614/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002615static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002616 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002617 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002618{
2619 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2620 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2621 struct lp_build_context *base = &bld_base->base;
2622 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002623 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002624 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002625 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002626 LLVMValueRef args[9];
2627 int i, r;
2628
Marek Olšák02134cf2014-10-04 22:07:50 +02002629 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002630
2631 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002632 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002633
2634 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2635
2636 create_meta_data(si_shader_ctx);
2637 create_function(si_shader_ctx);
2638 preload_streamout_buffers(si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002639 preload_ring_buffers(si_shader_ctx);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002640
Marek Olšákfc3b3352014-10-05 13:33:40 +02002641 args[0] = si_shader_ctx->gsvs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002642 args[1] = lp_build_mul_imm(uint,
2643 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2644 si_shader_ctx->param_vertex_id),
2645 4);
2646 args[3] = uint->zero;
2647 args[4] = uint->one; /* OFFEN */
2648 args[5] = uint->zero; /* IDXEN */
2649 args[6] = uint->one; /* GLC */
2650 args[7] = uint->one; /* SLC */
2651 args[8] = uint->zero; /* TFE */
2652
2653 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002654 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002655 unsigned chan;
2656
Marek Olšák02134cf2014-10-04 22:07:50 +02002657 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002658 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002659
2660 for (chan = 0; chan < 4; chan++) {
2661 args[2] = lp_build_const_int32(gallivm,
2662 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002663 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002664
2665 outputs[i].values[chan] =
2666 LLVMBuildBitCast(gallivm->builder,
2667 build_intrinsic(gallivm->builder,
2668 "llvm.SI.buffer.load.dword.i32.i32",
2669 LLVMInt32TypeInContext(gallivm->context),
2670 args, 9,
2671 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2672 base->elem_type, "");
2673 }
2674 }
2675
Marek Olšák02134cf2014-10-04 22:07:50 +02002676 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002677
2678 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2679
2680 if (dump)
2681 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2682
Marek Olšák1abb1a92014-09-17 22:44:22 +02002683 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002684 bld_base->base.gallivm->module);
2685
2686 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2687
2688 FREE(outputs);
2689 return r;
2690}
2691
Marek Olšák1abb1a92014-09-17 22:44:22 +02002692int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002693{
Marek Olšák2774abd2014-09-16 18:45:33 +02002694 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002695 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002696 struct lp_build_tgsi_context * bld_base;
2697 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002698 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002699 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002700
2701 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2702 * conversion fails. */
2703 if (dump) {
2704 tgsi_dump(sel->tokens, 0);
2705 si_dump_streamout(&sel->so);
2706 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002707
Marek Olšák8c37c162014-09-16 18:40:07 +02002708 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002709
Michel Dänzercfebaf92012-08-31 19:04:08 +02002710 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002711 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2712 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2713
Marek Olšák52335682014-09-30 15:12:09 +02002714 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002715 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2716
Marek Olšák52335682014-09-30 15:12:09 +02002717 shader->uses_instanceid = sel->info.uses_instanceid;
2718 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002719 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002720
2721 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002722 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002723 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2724 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002725 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002726 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2727 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2728 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002729 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002730 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002731 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2732 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002733
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002734 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2735 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002736
Michel Dänzer404b29d2013-11-21 16:45:28 +09002737 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2738 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2739
Christian Könige4ed5872013-03-21 18:02:52 +01002740 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002741 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002742 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2743 si_shader_ctx.shader = shader;
2744 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002745
Michel Dänzer51f89a02013-12-09 15:33:53 +09002746 switch (si_shader_ctx.type) {
2747 case TGSI_PROCESSOR_VERTEX:
2748 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002749 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002750 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2751 } else {
2752 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2753 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002754 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002755 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002756 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2757 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002758 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002759 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002760 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2761 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002762
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002763 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002764 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2765 shader->db_shader_control |=
2766 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2767 break;
2768 case TGSI_FS_DEPTH_LAYOUT_LESS:
2769 shader->db_shader_control |=
2770 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2771 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002772 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002773 break;
2774 default:
2775 assert(!"Unsupported shader type");
2776 return -1;
2777 }
2778
Christian König206f0592013-03-20 14:37:21 +01002779 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002780 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002781 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002782 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002783 preload_streamout_buffers(&si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002784 preload_ring_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002785
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002786 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2787 si_shader_ctx.gs_next_vertex =
2788 lp_build_alloca(bld_base->base.gallivm,
2789 bld_base->uint_bld.elem_type, "");
2790 }
2791
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002792 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002793 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002794 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002795 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002796
2797 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2798
2799 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002800 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002801 if (r) {
2802 fprintf(stderr, "LLVM failed to compile shader\n");
2803 goto out;
2804 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002805
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002806 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002807
2808 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002809 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002810 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002811 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002812 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002813 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2814 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002815 free(shader->gs_copy_shader);
2816 shader->gs_copy_shader = NULL;
2817 goto out;
2818 }
2819 }
2820
Tom Stellarda75c6162012-01-06 17:38:37 -05002821 tgsi_parse_free(&si_shader_ctx.parse);
2822
Michel Dänzer404b29d2013-11-21 16:45:28 +09002823out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002824 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002825 FREE(si_shader_ctx.constants[i]);
Tom Stellarda75c6162012-01-06 17:38:37 -05002826
Tom Stellard302f53d2012-10-25 13:50:10 -04002827 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002828}
2829
Marek Olšák2774abd2014-09-16 18:45:33 +02002830void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002831{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002832 if (shader->gs_copy_shader)
2833 si_shader_destroy(ctx, shader->gs_copy_shader);
2834
Marek Olšáka81c3e02013-08-14 01:04:39 +02002835 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002836 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002837}