blob: 09d7849a373e28cf003f9b789f89196d320605b2 [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,
Marek Olšákd1d2af22015-01-04 20:45:35 +0100487 LLVMRealOGT, face,
Michel Dänzer691f08d2012-09-06 18:03:38 +0200488 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);
807 struct lp_build_context *base = &bld_base->base;
808 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200809 unsigned reg_index;
810 unsigned chan;
811 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200812 LLVMValueRef base_elt;
813 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200814 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200815 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200816
Michel Dänzere3befbc2013-05-15 18:09:50 +0200817 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200818 LLVMValueRef *args = pos[2 + reg_index];
819
Michel Dänzere3befbc2013-05-15 18:09:50 +0200820 args[5] =
821 args[6] =
822 args[7] =
823 args[8] = lp_build_const_float(base->gallivm, 0.0f);
824
825 /* Compute dot products of position and user clip plane vectors */
826 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
827 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200828 args[1] = lp_build_const_int32(base->gallivm,
829 ((reg_index * 4 + chan) * 4 +
830 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200831 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200832 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200833 args[5 + chan] =
834 lp_build_add(base, args[5 + chan],
835 lp_build_mul(base, base_elt,
836 out_elts[const_chan]));
837 }
838 }
839
840 args[0] = lp_build_const_int32(base->gallivm, 0xf);
841 args[1] = uint->zero;
842 args[2] = uint->zero;
843 args[3] = lp_build_const_int32(base->gallivm,
844 V_008DFC_SQ_EXP_POS + 2 + reg_index);
845 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200846 }
847}
848
Marek Olšák8d03d922013-09-01 23:59:06 +0200849static void si_dump_streamout(struct pipe_stream_output_info *so)
850{
851 unsigned i;
852
853 if (so->num_outputs)
854 fprintf(stderr, "STREAMOUT\n");
855
856 for (i = 0; i < so->num_outputs; i++) {
857 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
858 so->output[i].start_component;
859 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
860 i, so->output[i].output_buffer,
861 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
862 so->output[i].register_index,
863 mask & 1 ? "x" : "",
864 mask & 2 ? "y" : "",
865 mask & 4 ? "z" : "",
866 mask & 8 ? "w" : "");
867 }
868}
869
870/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
871 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
872 * or v4i32 (num_channels=3,4). */
873static void build_tbuffer_store(struct si_shader_context *shader,
874 LLVMValueRef rsrc,
875 LLVMValueRef vdata,
876 unsigned num_channels,
877 LLVMValueRef vaddr,
878 LLVMValueRef soffset,
879 unsigned inst_offset,
880 unsigned dfmt,
881 unsigned nfmt,
882 unsigned offen,
883 unsigned idxen,
884 unsigned glc,
885 unsigned slc,
886 unsigned tfe)
887{
888 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
889 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
890 LLVMValueRef args[] = {
891 rsrc,
892 vdata,
893 LLVMConstInt(i32, num_channels, 0),
894 vaddr,
895 soffset,
896 LLVMConstInt(i32, inst_offset, 0),
897 LLVMConstInt(i32, dfmt, 0),
898 LLVMConstInt(i32, nfmt, 0),
899 LLVMConstInt(i32, offen, 0),
900 LLVMConstInt(i32, idxen, 0),
901 LLVMConstInt(i32, glc, 0),
902 LLVMConstInt(i32, slc, 0),
903 LLVMConstInt(i32, tfe, 0)
904 };
905
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900906 /* The instruction offset field has 12 bits */
907 assert(offen || inst_offset < (1 << 12));
908
Marek Olšák8d03d922013-09-01 23:59:06 +0200909 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
910 unsigned func = CLAMP(num_channels, 1, 3) - 1;
911 const char *types[] = {"i32", "v2i32", "v4i32"};
912 char name[256];
913 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
914
915 lp_build_intrinsic(gallivm->builder, name,
916 LLVMVoidTypeInContext(gallivm->context),
917 args, Elements(args));
918}
919
920static void build_streamout_store(struct si_shader_context *shader,
921 LLVMValueRef rsrc,
922 LLVMValueRef vdata,
923 unsigned num_channels,
924 LLVMValueRef vaddr,
925 LLVMValueRef soffset,
926 unsigned inst_offset)
927{
928 static unsigned dfmt[] = {
929 V_008F0C_BUF_DATA_FORMAT_32,
930 V_008F0C_BUF_DATA_FORMAT_32_32,
931 V_008F0C_BUF_DATA_FORMAT_32_32_32,
932 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
933 };
934 assert(num_channels >= 1 && num_channels <= 4);
935
936 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
937 inst_offset, dfmt[num_channels-1],
938 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
939}
940
941/* On SI, the vertex shader is responsible for writing streamout data
942 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900943static void si_llvm_emit_streamout(struct si_shader_context *shader,
944 struct si_shader_output_values *outputs,
945 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200946{
947 struct pipe_stream_output_info *so = &shader->shader->selector->so;
948 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
949 LLVMBuilderRef builder = gallivm->builder;
950 int i, j;
951 struct lp_build_if_state if_ctx;
952
953 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
954
955 LLVMValueRef so_param =
956 LLVMGetParam(shader->radeon_bld.main_fn,
957 shader->param_streamout_config);
958
959 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
960 LLVMValueRef so_vtx_count =
961 LLVMBuildAnd(builder,
962 LLVMBuildLShr(builder, so_param,
963 LLVMConstInt(i32, 16, 0), ""),
964 LLVMConstInt(i32, 127, 0), "");
965
966 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
967 NULL, 0, LLVMReadNoneAttribute);
968
969 /* can_emit = tid < so_vtx_count; */
970 LLVMValueRef can_emit =
971 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
972
973 /* Emit the streamout code conditionally. This actually avoids
974 * out-of-bounds buffer access. The hw tells us via the SGPR
975 * (so_vtx_count) which threads are allowed to emit streamout data. */
976 lp_build_if(&if_ctx, gallivm, can_emit);
977 {
978 /* The buffer offset is computed as follows:
979 * ByteOffset = streamout_offset[buffer_id]*4 +
980 * (streamout_write_index + thread_id)*stride[buffer_id] +
981 * attrib_offset
982 */
983
984 LLVMValueRef so_write_index =
985 LLVMGetParam(shader->radeon_bld.main_fn,
986 shader->param_streamout_write_index);
987
988 /* Compute (streamout_write_index + thread_id). */
989 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
990
991 /* Compute the write offset for each enabled buffer. */
992 LLVMValueRef so_write_offset[4] = {};
993 for (i = 0; i < 4; i++) {
994 if (!so->stride[i])
995 continue;
996
997 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
998 shader->param_streamout_offset[i]);
999 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1000
1001 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1002 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1003 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1004 }
1005
Marek Olšák8d03d922013-09-01 23:59:06 +02001006 /* Write streamout data. */
1007 for (i = 0; i < so->num_outputs; i++) {
1008 unsigned buf_idx = so->output[i].output_buffer;
1009 unsigned reg = so->output[i].register_index;
1010 unsigned start = so->output[i].start_component;
1011 unsigned num_comps = so->output[i].num_components;
1012 LLVMValueRef out[4];
1013
1014 assert(num_comps && num_comps <= 4);
1015 if (!num_comps || num_comps > 4)
1016 continue;
1017
Marek Olšák558f7772014-10-04 22:37:23 +02001018 if (reg >= noutput)
1019 continue;
1020
Marek Olšák8d03d922013-09-01 23:59:06 +02001021 /* Load the output as int. */
1022 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001023 out[j] = LLVMBuildBitCast(builder,
1024 outputs[reg].values[start+j],
1025 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001026 }
1027
1028 /* Pack the output. */
1029 LLVMValueRef vdata = NULL;
1030
1031 switch (num_comps) {
1032 case 1: /* as i32 */
1033 vdata = out[0];
1034 break;
1035 case 2: /* as v2i32 */
1036 case 3: /* as v4i32 (aligned to 4) */
1037 case 4: /* as v4i32 */
1038 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1039 for (j = 0; j < num_comps; j++) {
1040 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1041 LLVMConstInt(i32, j, 0), "");
1042 }
1043 break;
1044 }
1045
1046 build_streamout_store(shader, shader->so_buffers[buf_idx],
1047 vdata, num_comps,
1048 so_write_offset[buf_idx],
1049 LLVMConstInt(i32, 0, 0),
1050 so->output[i].dst_offset*4);
1051 }
1052 }
1053 lp_build_endif(&if_ctx);
1054}
1055
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001056
Michel Dänzer404b29d2013-11-21 16:45:28 +09001057/* Generate export instructions for hardware VS shader stage */
1058static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1059 struct si_shader_output_values *outputs,
1060 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001061{
1062 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001063 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001064 struct lp_build_context * base = &bld_base->base;
1065 struct lp_build_context * uint =
1066 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001067 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001068 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001069 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001070 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001071 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001072 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001073 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001074 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001075
Michel Dänzer67e385b2014-01-08 17:48:21 +09001076 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1077 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001078 }
1079
Michel Dänzer404b29d2013-11-21 16:45:28 +09001080 for (i = 0; i < noutput; i++) {
1081 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001082 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001083
Michel Dänzer0afeea52013-05-02 14:53:17 +02001084handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001085 /* Select the correct target */
1086 switch(semantic_name) {
1087 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001088 psize_value = outputs[i].values[0];
1089 continue;
1090 case TGSI_SEMANTIC_EDGEFLAG:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001091 edgeflag_value = outputs[i].values[0];
1092 continue;
1093 case TGSI_SEMANTIC_LAYER:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001094 layer_value = outputs[i].values[0];
1095 continue;
1096 case TGSI_SEMANTIC_POSITION:
1097 target = V_008DFC_SQ_EXP_POS;
1098 break;
1099 case TGSI_SEMANTIC_COLOR:
1100 case TGSI_SEMANTIC_BCOLOR:
1101 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001102 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001103 param_count++;
1104 break;
1105 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001106 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1107 break;
1108 case TGSI_SEMANTIC_CLIPVERTEX:
1109 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1110 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001111 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001112 case TGSI_SEMANTIC_FOG:
1113 case TGSI_SEMANTIC_GENERIC:
1114 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001115 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001116 param_count++;
1117 break;
1118 default:
1119 target = 0;
1120 fprintf(stderr,
1121 "Warning: SI unhandled vs output type:%d\n",
1122 semantic_name);
1123 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001124
Michel Dänzer404b29d2013-11-21 16:45:28 +09001125 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001126
Michel Dänzer404b29d2013-11-21 16:45:28 +09001127 if (target >= V_008DFC_SQ_EXP_POS &&
1128 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1129 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1130 args, sizeof(args));
1131 } else {
1132 lp_build_intrinsic(base->gallivm->builder,
1133 "llvm.SI.export",
1134 LLVMVoidTypeInContext(base->gallivm->context),
1135 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001136 }
1137
1138 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1139 semantic_name = TGSI_SEMANTIC_GENERIC;
1140 goto handle_semantic;
1141 }
1142 }
1143
1144 /* We need to add the position output manually if it's missing. */
1145 if (!pos_args[0][0]) {
1146 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1147 pos_args[0][1] = uint->zero; /* EXEC mask */
1148 pos_args[0][2] = uint->zero; /* last export? */
1149 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1150 pos_args[0][4] = uint->zero; /* COMPR flag */
1151 pos_args[0][5] = base->zero; /* X */
1152 pos_args[0][6] = base->zero; /* Y */
1153 pos_args[0][7] = base->zero; /* Z */
1154 pos_args[0][8] = base->one; /* W */
1155 }
1156
1157 /* Write the misc vector (point size, edgeflag, layer, viewport). */
Marek Olšák16153472014-12-07 19:30:08 +01001158 if (shader->selector->info.writes_psize ||
1159 shader->selector->info.writes_edgeflag ||
1160 shader->selector->info.writes_layer) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001161 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
Marek Olšák16153472014-12-07 19:30:08 +01001162 shader->selector->info.writes_psize |
1163 (shader->selector->info.writes_edgeflag << 1) |
1164 (shader->selector->info.writes_layer << 2));
Michel Dänzer51f89a02013-12-09 15:33:53 +09001165 pos_args[1][1] = uint->zero; /* EXEC mask */
1166 pos_args[1][2] = uint->zero; /* last export? */
1167 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1168 pos_args[1][4] = uint->zero; /* COMPR flag */
1169 pos_args[1][5] = base->zero; /* X */
1170 pos_args[1][6] = base->zero; /* Y */
1171 pos_args[1][7] = base->zero; /* Z */
1172 pos_args[1][8] = base->zero; /* W */
1173
Marek Olšák16153472014-12-07 19:30:08 +01001174 if (shader->selector->info.writes_psize)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001175 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001176
Marek Olšák16153472014-12-07 19:30:08 +01001177 if (shader->selector->info.writes_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001178 /* The output is a float, but the hw expects an integer
1179 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001180 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1181 edgeflag_value,
1182 bld_base->uint_bld.elem_type, "");
1183 edgeflag_value = lp_build_min(&bld_base->int_bld,
1184 edgeflag_value,
1185 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001186
1187 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001188 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1189 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001190 base->elem_type, "");
1191 }
1192
Marek Olšák16153472014-12-07 19:30:08 +01001193 if (shader->selector->info.writes_layer)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001194 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001195 }
1196
1197 for (i = 0; i < 4; i++)
1198 if (pos_args[i][0])
1199 shader->nr_pos_exports++;
1200
1201 pos_idx = 0;
1202 for (i = 0; i < 4; i++) {
1203 if (!pos_args[i][0])
1204 continue;
1205
1206 /* Specify the target we are exporting */
1207 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1208
1209 if (pos_idx == shader->nr_pos_exports)
1210 /* Specify that this is the last export */
1211 pos_args[i][2] = uint->one;
1212
1213 lp_build_intrinsic(base->gallivm->builder,
1214 "llvm.SI.export",
1215 LLVMVoidTypeInContext(base->gallivm->context),
1216 pos_args[i], 9);
1217 }
1218}
1219
Michel Dänzer404b29d2013-11-21 16:45:28 +09001220static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1221{
1222 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1223 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001224 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001225 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001226 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001227 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1228 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001229 unsigned chan;
1230 int i;
1231
Marek Olšák216cf862014-10-04 17:04:05 +02001232 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001233 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001234 si_shader_ctx->radeon_bld.soa.outputs[i];
1235 int param_index = get_param_index(info->output_semantic_name[i],
1236 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001237 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001238
Marek Olšáke29353f2014-09-17 22:17:02 +02001239 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001240 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001241
1242 for (chan = 0; chan < 4; chan++) {
1243 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001244 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1245
Marek Olšákfc3b3352014-10-05 13:33:40 +02001246 build_tbuffer_store(si_shader_ctx,
1247 si_shader_ctx->esgs_ring,
1248 out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001249 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001250 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001251 V_008F0C_BUF_DATA_FORMAT_32,
1252 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001253 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001254 }
1255 }
1256}
1257
1258static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1259{
1260 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1261 struct gallivm_state *gallivm = bld_base->base.gallivm;
1262 LLVMValueRef args[2];
1263
1264 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1265 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1266 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1267 LLVMVoidTypeInContext(gallivm->context), args, 2,
1268 LLVMNoUnwindAttribute);
1269}
1270
1271static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1272{
1273 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1274 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001275 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001276 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001277 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001278
Marek Olšákec0d1682014-10-04 22:33:36 +02001279 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001280
Marek Olšákec0d1682014-10-04 22:33:36 +02001281 for (i = 0; i < info->num_outputs; i++) {
1282 outputs[i].name = info->output_semantic_name[i];
1283 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001284
Marek Olšákec0d1682014-10-04 22:33:36 +02001285 for (j = 0; j < 4; j++)
1286 outputs[i].values[j] =
1287 LLVMBuildLoad(gallivm->builder,
1288 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1289 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001290 }
1291
Marek Olšákec0d1682014-10-04 22:33:36 +02001292 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001293 FREE(outputs);
1294}
1295
Michel Dänzer51f89a02013-12-09 15:33:53 +09001296static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1297{
1298 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001299 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001300 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001301 struct lp_build_context * uint = &bld_base->uint_bld;
1302 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001303 LLVMValueRef args[9];
1304 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001305 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001306 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001307
Marek Olšák75e97e22014-10-04 23:13:50 +02001308 for (i = 0; i < info->num_outputs; i++) {
1309 unsigned semantic_name = info->output_semantic_name[i];
1310 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001311 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001312
Marek Olšák75e97e22014-10-04 23:13:50 +02001313 /* Select the correct target */
1314 switch (semantic_name) {
1315 case TGSI_SEMANTIC_POSITION:
1316 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001317 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001318 case TGSI_SEMANTIC_STENCIL:
1319 stencil_index = i;
1320 continue;
1321 case TGSI_SEMANTIC_SAMPLEMASK:
1322 samplemask_index = i;
1323 continue;
1324 case TGSI_SEMANTIC_COLOR:
1325 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1326 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1327 LLVMBuildStore(bld_base->base.gallivm->builder,
1328 bld_base->base.one,
1329 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001330
Marek Olšák75e97e22014-10-04 23:13:50 +02001331 if (semantic_index == 0 &&
1332 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1333 si_alpha_test(bld_base,
1334 si_shader_ctx->radeon_bld.soa.outputs[i]);
1335 break;
1336 default:
1337 target = 0;
1338 fprintf(stderr,
1339 "Warning: SI unhandled fs output type:%d\n",
1340 semantic_name);
1341 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001342
Marek Olšák75e97e22014-10-04 23:13:50 +02001343 si_llvm_init_export_args_load(bld_base,
1344 si_shader_ctx->radeon_bld.soa.outputs[i],
1345 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001346
Marek Olšák75e97e22014-10-04 23:13:50 +02001347 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1348 /* If there is an export instruction waiting to be emitted, do so now. */
1349 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001350 lp_build_intrinsic(base->gallivm->builder,
1351 "llvm.SI.export",
1352 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001353 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001354 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001355
1356 /* This instruction will be emitted at the end of the shader. */
1357 memcpy(last_args, args, sizeof(args));
1358
1359 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1360 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1361 semantic_index == 0 &&
1362 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1363 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1364 si_llvm_init_export_args_load(bld_base,
1365 si_shader_ctx->radeon_bld.soa.outputs[i],
1366 V_008DFC_SQ_EXP_MRT + c, args);
1367 lp_build_intrinsic(base->gallivm->builder,
1368 "llvm.SI.export",
1369 LLVMVoidTypeInContext(base->gallivm->context),
1370 args, 9);
1371 }
1372 }
1373 } else {
1374 lp_build_intrinsic(base->gallivm->builder,
1375 "llvm.SI.export",
1376 LLVMVoidTypeInContext(base->gallivm->context),
1377 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001378 }
1379 }
1380
Marek Olšákd0e8b652014-05-06 20:04:31 +02001381 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001382 LLVMValueRef out_ptr;
1383 unsigned mask = 0;
1384
1385 /* Specify the target we are exporting */
1386 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1387
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001388 args[5] = base->zero; /* R, depth */
1389 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1390 args[7] = base->zero; /* B, sample mask */
1391 args[8] = base->zero; /* A, alpha to mask */
1392
Michel Dänzer1a616c12012-11-13 17:35:09 +01001393 if (depth_index >= 0) {
1394 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1395 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1396 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001397 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001398 }
1399
1400 if (stencil_index >= 0) {
1401 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001402 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001403 /* Only setting the stencil component bit (0x2) here
1404 * breaks some stencil piglit tests
1405 */
1406 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001407 si_shader_ctx->shader->db_shader_control |=
1408 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001409 }
1410
Marek Olšákd0e8b652014-05-06 20:04:31 +02001411 if (samplemask_index >= 0) {
1412 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1413 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1414 mask |= 0xf; /* Set all components. */
1415 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1416 }
1417
1418 if (samplemask_index >= 0)
1419 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1420 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001421 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1422 else
1423 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1424
Michel Dänzer1a616c12012-11-13 17:35:09 +01001425 /* Specify which components to enable */
1426 args[0] = lp_build_const_int32(base->gallivm, mask);
1427
1428 args[1] =
1429 args[2] =
1430 args[4] = uint->zero;
1431
1432 if (last_args[0])
1433 lp_build_intrinsic(base->gallivm->builder,
1434 "llvm.SI.export",
1435 LLVMVoidTypeInContext(base->gallivm->context),
1436 args, 9);
1437 else
1438 memcpy(last_args, args, sizeof(args));
1439 }
1440
Michel Dänzer51f89a02013-12-09 15:33:53 +09001441 if (!last_args[0]) {
1442 /* Specify which components to enable */
1443 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001444
Michel Dänzer51f89a02013-12-09 15:33:53 +09001445 /* Specify the target we are exporting */
1446 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001447
Michel Dänzer51f89a02013-12-09 15:33:53 +09001448 /* Set COMPR flag to zero to export data as 32-bit */
1449 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001450
Michel Dänzer51f89a02013-12-09 15:33:53 +09001451 /* dummy bits */
1452 last_args[5]= uint->zero;
1453 last_args[6]= uint->zero;
1454 last_args[7]= uint->zero;
1455 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001456 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001457
1458 /* Specify whether the EXEC mask represents the valid mask */
1459 last_args[1] = uint->one;
1460
1461 /* Specify that this is the last export */
1462 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1463
1464 lp_build_intrinsic(base->gallivm->builder,
1465 "llvm.SI.export",
1466 LLVMVoidTypeInContext(base->gallivm->context),
1467 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001468}
1469
Marek Olšák4855acd2013-08-06 15:08:54 +02001470static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1471 struct lp_build_tgsi_context * bld_base,
1472 struct lp_build_emit_data * emit_data);
1473
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001474static bool tgsi_is_shadow_sampler(unsigned target)
1475{
1476 return target == TGSI_TEXTURE_SHADOW1D ||
1477 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1478 target == TGSI_TEXTURE_SHADOW2D ||
1479 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1480 target == TGSI_TEXTURE_SHADOWCUBE ||
1481 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1482 target == TGSI_TEXTURE_SHADOWRECT;
1483}
1484
Marek Olšák877bb522014-06-25 03:12:46 +02001485static const struct lp_build_tgsi_action tex_action;
1486
Tom Stellarda75c6162012-01-06 17:38:37 -05001487static void tex_fetch_args(
1488 struct lp_build_tgsi_context * bld_base,
1489 struct lp_build_emit_data * emit_data)
1490{
Christian König55fe5cc2013-03-04 16:30:06 +01001491 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001492 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001493 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001494 unsigned opcode = inst->Instruction.Opcode;
1495 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001496 LLVMValueRef coords[4];
1497 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001498 int ref_pos;
1499 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001500 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001501 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001502 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1503 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001504 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001505
1506 if (target == TGSI_TEXTURE_BUFFER) {
1507 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1508 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1509 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1510 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1511
Marek Olšák4f3f0432014-07-07 22:49:15 +02001512 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001513 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1514 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1515 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1516 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1517
1518 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1519 emit_data->args[0] = res;
1520 emit_data->args[1] = bld_base->uint_bld.zero;
1521 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1522 emit_data->arg_count = 3;
1523 return;
1524 }
Tom Stellard467f5162012-05-16 15:15:35 -04001525
Michel Dänzer120efee2013-01-25 12:10:11 +01001526 /* Fetch and project texture coordinates */
1527 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001528 for (chan = 0; chan < 3; chan++ ) {
1529 coords[chan] = lp_build_emit_fetch(bld_base,
1530 emit_data->inst, 0,
1531 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001532 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001533 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1534 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001535 coords[chan],
1536 coords[3]);
1537 }
1538
Michel Dänzer120efee2013-01-25 12:10:11 +01001539 if (opcode == TGSI_OPCODE_TXP)
1540 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001541
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001542 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001543 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001544 /* The offsets are six-bit signed integers packed like this:
1545 * X=[5:0], Y=[13:8], and Z=[21:16].
1546 */
1547 LLVMValueRef offset[3], pack;
1548
1549 assert(inst->Texture.NumOffsets == 1);
1550
1551 for (chan = 0; chan < 3; chan++) {
1552 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1553 emit_data->inst, 0, chan);
1554 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1555 lp_build_const_int32(gallivm, 0x3f), "");
1556 if (chan)
1557 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1558 lp_build_const_int32(gallivm, chan*8), "");
1559 }
1560
1561 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1562 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1563 address[count++] = pack;
1564 }
1565
Michel Dänzer120efee2013-01-25 12:10:11 +01001566 /* Pack LOD bias value */
1567 if (opcode == TGSI_OPCODE_TXB)
1568 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001569 if (opcode == TGSI_OPCODE_TXB2)
1570 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001571
Michel Dänzer120efee2013-01-25 12:10:11 +01001572 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001573 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001574 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1575 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1576 } else {
1577 assert(ref_pos >= 0);
1578 address[count++] = coords[ref_pos];
1579 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001580 }
1581
Marek Olšákb279f0142014-07-04 03:19:58 +02001582 if (target == TGSI_TEXTURE_CUBE ||
1583 target == TGSI_TEXTURE_CUBE_ARRAY ||
1584 target == TGSI_TEXTURE_SHADOWCUBE ||
1585 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1586 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1587
Michel Dänzera6b83c02013-02-21 16:10:55 +01001588 /* Pack user derivatives */
1589 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001590 int num_deriv_channels, param;
1591
1592 switch (target) {
1593 case TGSI_TEXTURE_3D:
1594 num_deriv_channels = 3;
1595 break;
1596 case TGSI_TEXTURE_2D:
1597 case TGSI_TEXTURE_SHADOW2D:
1598 case TGSI_TEXTURE_RECT:
1599 case TGSI_TEXTURE_SHADOWRECT:
1600 case TGSI_TEXTURE_2D_ARRAY:
1601 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1602 case TGSI_TEXTURE_CUBE:
1603 case TGSI_TEXTURE_SHADOWCUBE:
1604 case TGSI_TEXTURE_CUBE_ARRAY:
1605 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1606 num_deriv_channels = 2;
1607 break;
1608 case TGSI_TEXTURE_1D:
1609 case TGSI_TEXTURE_SHADOW1D:
1610 case TGSI_TEXTURE_1D_ARRAY:
1611 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1612 num_deriv_channels = 1;
1613 break;
1614 default:
1615 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001616 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001617
1618 for (param = 1; param <= 2; param++)
1619 for (chan = 0; chan < num_deriv_channels; chan++)
1620 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001621 }
1622
Michel Dänzer120efee2013-01-25 12:10:11 +01001623 /* Pack texture coordinates */
1624 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001625 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001626 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001627 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001628 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001629
Marek Olšákd2bd6342013-09-18 15:40:21 +02001630 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001631 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001632 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001633 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001634 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001635
1636 if (count > 16) {
1637 assert(!"Cannot handle more than 16 texture address parameters");
1638 count = 16;
1639 }
1640
1641 for (chan = 0; chan < count; chan++ ) {
1642 address[chan] = LLVMBuildBitCast(gallivm->builder,
1643 address[chan],
1644 LLVMInt32TypeInContext(gallivm->context),
1645 "");
1646 }
1647
Marek Olšák4855acd2013-08-06 15:08:54 +02001648 /* Adjust the sample index according to FMASK.
1649 *
1650 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1651 * which is the identity mapping. Each nibble says which physical sample
1652 * should be fetched to get that sample.
1653 *
1654 * For example, 0x11111100 means there are only 2 samples stored and
1655 * the second sample covers 3/4 of the pixel. When reading samples 0
1656 * and 1, return physical sample 0 (determined by the first two 0s
1657 * in FMASK), otherwise return physical sample 1.
1658 *
1659 * The sample index should be adjusted as follows:
1660 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1661 */
1662 if (target == TGSI_TEXTURE_2D_MSAA ||
1663 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1664 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1665 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001666 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001667 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001668 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001669
Marek Olšákd2bd6342013-09-18 15:40:21 +02001670 memcpy(txf_address, address, sizeof(txf_address));
1671
1672 if (target == TGSI_TEXTURE_2D_MSAA) {
1673 txf_address[2] = bld_base->uint_bld.zero;
1674 }
1675 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001676
1677 /* Pad to a power-of-two size. */
1678 while (txf_count < util_next_power_of_two(txf_count))
1679 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1680
1681 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001682 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1683 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1684 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001685 txf_emit_data.chan = 0;
1686 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001687 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001688 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001689 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001690 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001691 txf_emit_data.arg_count = 3;
1692
Marek Olšák877bb522014-06-25 03:12:46 +02001693 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001694
1695 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001696 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1697 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1698
1699 /* Apply the formula. */
1700 LLVMValueRef fmask =
1701 LLVMBuildExtractElement(gallivm->builder,
1702 txf_emit_data.output[0],
1703 uint_bld->zero, "");
1704
Marek Olšákd2bd6342013-09-18 15:40:21 +02001705 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001706
1707 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001708 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001709
1710 LLVMValueRef shifted_fmask =
1711 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1712
1713 LLVMValueRef final_sample =
1714 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1715
1716 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1717 * resource descriptor is 0 (invalid),
1718 */
1719 LLVMValueRef fmask_desc =
1720 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001721 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001722 LLVMVectorType(uint_bld->elem_type, 8), "");
1723
1724 LLVMValueRef fmask_word1 =
1725 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1726 uint_bld->one, "");
1727
1728 LLVMValueRef word1_is_nonzero =
1729 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1730 fmask_word1, uint_bld->zero, "");
1731
Marek Olšákd2bd6342013-09-18 15:40:21 +02001732 /* Replace the MSAA sample index. */
1733 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001734 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001735 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001736 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001737
Michel Dänzer36231112013-05-02 09:44:45 +02001738 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001739 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001740
1741 if (opcode == TGSI_OPCODE_TXF) {
1742 /* add tex offsets */
1743 if (inst->Texture.NumOffsets) {
1744 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1745 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1746 const struct tgsi_texture_offset * off = inst->TexOffsets;
1747
1748 assert(inst->Texture.NumOffsets == 1);
1749
Marek Olšákdefedc02013-09-18 15:36:38 +02001750 switch (target) {
1751 case TGSI_TEXTURE_3D:
1752 address[2] = lp_build_add(uint_bld, address[2],
1753 bld->immediates[off->Index][off->SwizzleZ]);
1754 /* fall through */
1755 case TGSI_TEXTURE_2D:
1756 case TGSI_TEXTURE_SHADOW2D:
1757 case TGSI_TEXTURE_RECT:
1758 case TGSI_TEXTURE_SHADOWRECT:
1759 case TGSI_TEXTURE_2D_ARRAY:
1760 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001761 address[1] =
1762 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001763 bld->immediates[off->Index][off->SwizzleY]);
1764 /* fall through */
1765 case TGSI_TEXTURE_1D:
1766 case TGSI_TEXTURE_SHADOW1D:
1767 case TGSI_TEXTURE_1D_ARRAY:
1768 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1769 address[0] =
1770 lp_build_add(uint_bld, address[0],
1771 bld->immediates[off->Index][off->SwizzleX]);
1772 break;
1773 /* texture offsets do not apply to other texture targets */
1774 }
Michel Dänzer36231112013-05-02 09:44:45 +02001775 }
1776
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001777 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1778 emit_data->arg_count = 3;
1779
Michel Dänzer36231112013-05-02 09:44:45 +02001780 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001781 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001782 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001783 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001784 opcode == TGSI_OPCODE_LODQ ||
1785 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001786 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1787 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1788 target == TGSI_TEXTURE_2D_ARRAY ||
1789 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1790 target == TGSI_TEXTURE_CUBE_ARRAY ||
1791 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1792 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001793 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001794
Marek Olšák57f3da92014-06-16 16:53:42 +02001795 if (opcode == TGSI_OPCODE_TG4) {
1796 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001797
Marek Olšák57f3da92014-06-16 16:53:42 +02001798 /* DMASK was repurposed for GATHER4. 4 components are always
1799 * returned and DMASK works like a swizzle - it selects
1800 * the component to fetch. The only valid DMASK values are
1801 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1802 * (red,red,red,red) etc.) The ISA document doesn't mention
1803 * this.
1804 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001805
Marek Olšák57f3da92014-06-16 16:53:42 +02001806 /* Get the component index from src1.x for Gather4. */
1807 if (!tgsi_is_shadow_sampler(target)) {
1808 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1809 LLVMValueRef comp_imm;
1810 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001811
Marek Olšák57f3da92014-06-16 16:53:42 +02001812 assert(src1.File == TGSI_FILE_IMMEDIATE);
1813
1814 comp_imm = imms[src1.Index][src1.SwizzleX];
1815 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1816 gather_comp = CLAMP(gather_comp, 0, 3);
1817 }
1818
1819 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001820 }
1821
Marek Olšák4855acd2013-08-06 15:08:54 +02001822 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001823 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001824 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1825 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1826 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1827 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1828 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1829 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1830 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1831
1832 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001833
1834 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001835 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001836 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001837 } else {
1838 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1839 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001840 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001841
1842 emit_data->dst_type = LLVMVectorType(
1843 LLVMFloatTypeInContext(gallivm->context),
1844 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001845 }
1846
Marek Olšák2484daa2014-04-22 21:23:29 +02001847 /* The fetch opcode has been converted to a 2D array fetch.
1848 * This simplifies the LLVM backend. */
1849 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1850 target = TGSI_TEXTURE_2D_ARRAY;
1851 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1852 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1853
Michel Dänzer120efee2013-01-25 12:10:11 +01001854 /* Pad to power of two vector */
1855 while (count < util_next_power_of_two(count))
1856 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1857
Christian Königccf3e8f2013-03-26 15:09:27 +01001858 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001859}
1860
Michel Dänzer07eddc42013-02-06 15:43:10 +01001861static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1862 struct lp_build_tgsi_context * bld_base,
1863 struct lp_build_emit_data * emit_data)
1864{
1865 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001866 unsigned opcode = emit_data->inst->Instruction.Opcode;
1867 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001868 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001869 bool has_offset = HAVE_LLVM >= 0x0305 ?
1870 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001871
Marek Olšák877bb522014-06-25 03:12:46 +02001872 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001873 emit_data->output[emit_data->chan] = build_intrinsic(
1874 base->gallivm->builder,
1875 "llvm.SI.vs.load.input", emit_data->dst_type,
1876 emit_data->args, emit_data->arg_count,
1877 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1878 return;
1879 }
1880
Marek Olšák877bb522014-06-25 03:12:46 +02001881 if (opcode == TGSI_OPCODE_TG4 ||
1882 opcode == TGSI_OPCODE_LODQ ||
1883 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1884 bool is_shadow = tgsi_is_shadow_sampler(target);
1885 const char *name = "llvm.SI.image.sample";
1886 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001887
Marek Olšák877bb522014-06-25 03:12:46 +02001888 switch (opcode) {
1889 case TGSI_OPCODE_TEX:
1890 case TGSI_OPCODE_TEX2:
1891 case TGSI_OPCODE_TXP:
1892 break;
1893 case TGSI_OPCODE_TXB:
1894 case TGSI_OPCODE_TXB2:
1895 infix = ".b";
1896 break;
1897 case TGSI_OPCODE_TXL:
1898 case TGSI_OPCODE_TXL2:
1899 infix = ".l";
1900 break;
1901 case TGSI_OPCODE_TXD:
1902 infix = ".d";
1903 break;
1904 case TGSI_OPCODE_TG4:
1905 name = "llvm.SI.gather4";
1906 break;
1907 case TGSI_OPCODE_LODQ:
1908 name = "llvm.SI.getlod";
1909 is_shadow = false;
1910 has_offset = false;
1911 break;
1912 default:
1913 assert(0);
1914 return;
1915 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001916
Marek Olšák877bb522014-06-25 03:12:46 +02001917 /* Add the type and suffixes .c, .o if needed. */
1918 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1919 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1920 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001921
Marek Olšák877bb522014-06-25 03:12:46 +02001922 emit_data->output[emit_data->chan] = build_intrinsic(
1923 base->gallivm->builder, intr_name, emit_data->dst_type,
1924 emit_data->args, emit_data->arg_count,
1925 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1926 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001927 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001928 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001929
Marek Olšák877bb522014-06-25 03:12:46 +02001930 switch (opcode) {
1931 case TGSI_OPCODE_TEX:
1932 case TGSI_OPCODE_TEX2:
1933 case TGSI_OPCODE_TXP:
1934 name = "llvm.SI.sample";
1935 break;
1936 case TGSI_OPCODE_TXB:
1937 case TGSI_OPCODE_TXB2:
1938 name = "llvm.SI.sampleb";
1939 break;
1940 case TGSI_OPCODE_TXD:
1941 name = "llvm.SI.sampled";
1942 break;
1943 case TGSI_OPCODE_TXF:
1944 name = "llvm.SI.imageload";
1945 break;
1946 case TGSI_OPCODE_TXL:
1947 case TGSI_OPCODE_TXL2:
1948 name = "llvm.SI.samplel";
1949 break;
1950 default:
1951 assert(0);
1952 return;
1953 }
1954
Marek Olšákd859bdb2014-07-14 19:40:14 +02001955 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1956 v16i8 = LLVMVectorType(i8, 16);
1957 v32i8 = LLVMVectorType(i8, 32);
1958
1959 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1960 emit_data->args[1], v32i8, "");
1961 if (opcode != TGSI_OPCODE_TXF) {
1962 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1963 emit_data->args[2], v16i8, "");
1964 }
1965
Marek Olšák877bb522014-06-25 03:12:46 +02001966 sprintf(intr_name, "%s.v%ui32", name,
1967 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1968
1969 emit_data->output[emit_data->chan] = build_intrinsic(
1970 base->gallivm->builder, intr_name, emit_data->dst_type,
1971 emit_data->args, emit_data->arg_count,
1972 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1973 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001974}
1975
Michel Dänzer0495adb2013-05-06 12:45:14 +02001976static void txq_fetch_args(
1977 struct lp_build_tgsi_context * bld_base,
1978 struct lp_build_emit_data * emit_data)
1979{
1980 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1981 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001982 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02001983 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001984
Marek Olšák2484daa2014-04-22 21:23:29 +02001985 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001986 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1987 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1988
1989 /* Read the size from the buffer descriptor directly. */
1990 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1991 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1992 size = LLVMBuildExtractElement(gallivm->builder, size,
1993 lp_build_const_int32(gallivm, 2), "");
1994 emit_data->args[0] = size;
1995 return;
1996 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02001997
1998 /* Mip level */
1999 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2000
2001 /* Resource */
2002 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2003
Marek Olšák2484daa2014-04-22 21:23:29 +02002004 /* Texture target */
2005 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2006 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2007 target = TGSI_TEXTURE_2D_ARRAY;
2008
Michel Dänzer0495adb2013-05-06 12:45:14 +02002009 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002010 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002011
2012 emit_data->arg_count = 3;
2013
2014 emit_data->dst_type = LLVMVectorType(
2015 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2016 4);
2017}
2018
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002019static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2020 struct lp_build_tgsi_context * bld_base,
2021 struct lp_build_emit_data * emit_data)
2022{
Marek Olšák2484daa2014-04-22 21:23:29 +02002023 unsigned target = emit_data->inst->Texture.Texture;
2024
2025 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002026 /* Just return the buffer size. */
2027 emit_data->output[emit_data->chan] = emit_data->args[0];
2028 return;
2029 }
2030
2031 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002032
2033 /* Divide the number of layers by 6 to get the number of cubes. */
2034 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2035 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2036 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2037 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2038 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2039
2040 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2041 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2042 z = LLVMBuildSDiv(builder, z, six, "");
2043
2044 emit_data->output[emit_data->chan] =
2045 LLVMBuildInsertElement(builder, v4, z, two, "");
2046 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002047}
2048
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002049static void si_llvm_emit_ddxy(
2050 const struct lp_build_tgsi_action * action,
2051 struct lp_build_tgsi_context * bld_base,
2052 struct lp_build_emit_data * emit_data)
2053{
2054 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2055 struct gallivm_state *gallivm = bld_base->base.gallivm;
2056 struct lp_build_context * base = &bld_base->base;
2057 const struct tgsi_full_instruction *inst = emit_data->inst;
2058 unsigned opcode = inst->Instruction.Opcode;
2059 LLVMValueRef indices[2];
2060 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2061 LLVMValueRef tl, trbl, result[4];
2062 LLVMTypeRef i32;
2063 unsigned swizzle[4];
2064 unsigned c;
2065
2066 i32 = LLVMInt32TypeInContext(gallivm->context);
2067
2068 indices[0] = bld_base->uint_bld.zero;
2069 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2070 NULL, 0, LLVMReadNoneAttribute);
2071 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2072 indices, 2, "");
2073
2074 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2075 lp_build_const_int32(gallivm, 0xfffffffc), "");
2076 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2077 indices, 2, "");
2078
2079 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2080 lp_build_const_int32(gallivm,
2081 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2082 "");
2083 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2084 indices, 2, "");
2085
2086 for (c = 0; c < 4; ++c) {
2087 unsigned i;
2088
2089 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2090 for (i = 0; i < c; ++i) {
2091 if (swizzle[i] == swizzle[c]) {
2092 result[c] = result[i];
2093 break;
2094 }
2095 }
2096 if (i != c)
2097 continue;
2098
2099 LLVMBuildStore(gallivm->builder,
2100 LLVMBuildBitCast(gallivm->builder,
2101 lp_build_emit_fetch(bld_base, inst, 0, c),
2102 i32, ""),
2103 store_ptr);
2104
2105 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2106 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2107
2108 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2109 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2110
2111 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2112 }
2113
2114 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2115}
2116
Michel Dänzer404b29d2013-11-21 16:45:28 +09002117/* Emit one vertex from the geometry shader */
2118static void si_llvm_emit_vertex(
2119 const struct lp_build_tgsi_action *action,
2120 struct lp_build_tgsi_context *bld_base,
2121 struct lp_build_emit_data *emit_data)
2122{
2123 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002124 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002125 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002126 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002127 struct gallivm_state *gallivm = bld_base->base.gallivm;
2128 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002129 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2130 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002131 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002132 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002133 LLVMValueRef args[2];
2134 unsigned chan;
2135 int i;
2136
Michel Dänzer404b29d2013-11-21 16:45:28 +09002137 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002138 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002139
2140 /* If this thread has already emitted the declared maximum number of
2141 * vertices, kill it: excessive vertex emissions are not supposed to
2142 * have any effect, and GS threads have no externally observable
2143 * effects other than emitting vertices.
2144 */
2145 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2146 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002147 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002148 kill = lp_build_select(&bld_base->base, can_emit,
2149 lp_build_const_float(gallivm, 1.0f),
2150 lp_build_const_float(gallivm, -1.0f));
2151 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2152 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2153
Marek Olšák02134cf2014-10-04 22:07:50 +02002154 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002155 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002156 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002157
2158 for (chan = 0; chan < 4; chan++) {
2159 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002160 LLVMValueRef voffset =
2161 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002162 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002163
2164 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2165 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002166
2167 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2168
Marek Olšákfc3b3352014-10-05 13:33:40 +02002169 build_tbuffer_store(si_shader_ctx,
2170 si_shader_ctx->gsvs_ring,
2171 out_val, 1,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002172 voffset, soffset, 0,
2173 V_008F0C_BUF_DATA_FORMAT_32,
2174 V_008F0C_BUF_NUM_FORMAT_UINT,
2175 1, 0, 1, 1, 0);
2176 }
2177 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002178 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2179 lp_build_const_int32(gallivm, 1));
2180 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002181
2182 /* Signal vertex emission */
2183 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2184 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2185 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2186 LLVMVoidTypeInContext(gallivm->context), args, 2,
2187 LLVMNoUnwindAttribute);
2188}
2189
2190/* Cut one primitive from the geometry shader */
2191static void si_llvm_emit_primitive(
2192 const struct lp_build_tgsi_action *action,
2193 struct lp_build_tgsi_context *bld_base,
2194 struct lp_build_emit_data *emit_data)
2195{
2196 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2197 struct gallivm_state *gallivm = bld_base->base.gallivm;
2198 LLVMValueRef args[2];
2199
2200 /* Signal primitive cut */
2201 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2202 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2203 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2204 LLVMVoidTypeInContext(gallivm->context), args, 2,
2205 LLVMNoUnwindAttribute);
2206}
2207
Tom Stellarda75c6162012-01-06 17:38:37 -05002208static const struct lp_build_tgsi_action tex_action = {
2209 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002210 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002211};
2212
Michel Dänzer0495adb2013-05-06 12:45:14 +02002213static const struct lp_build_tgsi_action txq_action = {
2214 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002215 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002216 .intr_name = "llvm.SI.resinfo"
2217};
2218
Christian König206f0592013-03-20 14:37:21 +01002219static void create_meta_data(struct si_shader_context *si_shader_ctx)
2220{
2221 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2222 LLVMValueRef args[3];
2223
2224 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2225 args[1] = 0;
2226 args[2] = lp_build_const_int32(gallivm, 1);
2227
2228 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2229}
2230
Marek Olšák4f3f0432014-07-07 22:49:15 +02002231static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2232{
2233 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2234 CONST_ADDR_SPACE);
2235}
2236
Christian König55fe5cc2013-03-04 16:30:06 +01002237static void create_function(struct si_shader_context *si_shader_ctx)
2238{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002239 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2240 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002241 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002242 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002243 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002244
Christian König55fe5cc2013-03-04 16:30:06 +01002245 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002246 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002247 f32 = LLVMFloatTypeInContext(gallivm->context);
2248 v2i32 = LLVMVectorType(i32, 2);
2249 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002250 v4i32 = LLVMVectorType(i32, 4);
2251 v8i32 = LLVMVectorType(i32, 8);
2252 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002253
Marek Olšákee2a8182014-07-07 23:27:19 +02002254 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002255 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002256 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2257 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002258 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002259
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002260 switch (si_shader_ctx->type) {
2261 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002262 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002263 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002264 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002265 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002266 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002267
Michel Dänzer404b29d2013-11-21 16:45:28 +09002268 if (shader->key.vs.as_es) {
2269 params[SI_PARAM_ES2GS_OFFSET] = i32;
2270 num_params++;
2271 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002272 if (shader->is_gs_copy_shader) {
2273 last_array_pointer = SI_PARAM_CONST;
2274 num_params = SI_PARAM_CONST+1;
2275 }
2276
Michel Dänzer404b29d2013-11-21 16:45:28 +09002277 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002278
Michel Dänzer404b29d2013-11-21 16:45:28 +09002279 /* Streamout SGPRs. */
2280 if (shader->selector->so.num_outputs) {
2281 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2282 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2283 }
2284 /* A streamout buffer offset is loaded if the stride is non-zero. */
2285 for (i = 0; i < 4; i++) {
2286 if (!shader->selector->so.stride[i])
2287 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002288
Michel Dänzer404b29d2013-11-21 16:45:28 +09002289 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2290 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002291 }
2292
2293 last_sgpr = num_params-1;
2294
2295 /* VGPRs */
2296 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2297 params[num_params++] = i32; /* unused*/
2298 params[num_params++] = i32; /* unused */
2299 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002300 break;
Christian König0666ffd2013-03-05 15:07:39 +01002301
Michel Dänzer404b29d2013-11-21 16:45:28 +09002302 case TGSI_PROCESSOR_GEOMETRY:
2303 params[SI_PARAM_GS2VS_OFFSET] = i32;
2304 params[SI_PARAM_GS_WAVE_ID] = i32;
2305 last_sgpr = SI_PARAM_GS_WAVE_ID;
2306
2307 /* VGPRs */
2308 params[SI_PARAM_VTX0_OFFSET] = i32;
2309 params[SI_PARAM_VTX1_OFFSET] = i32;
2310 params[SI_PARAM_PRIMITIVE_ID] = i32;
2311 params[SI_PARAM_VTX2_OFFSET] = i32;
2312 params[SI_PARAM_VTX3_OFFSET] = i32;
2313 params[SI_PARAM_VTX4_OFFSET] = i32;
2314 params[SI_PARAM_VTX5_OFFSET] = i32;
2315 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2316 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2317 break;
2318
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002319 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002320 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002321 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002322 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002323 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2324 params[SI_PARAM_PERSP_CENTER] = v2i32;
2325 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2326 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2327 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2328 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2329 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2330 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2331 params[SI_PARAM_POS_X_FLOAT] = f32;
2332 params[SI_PARAM_POS_Y_FLOAT] = f32;
2333 params[SI_PARAM_POS_Z_FLOAT] = f32;
2334 params[SI_PARAM_POS_W_FLOAT] = f32;
2335 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002336 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002337 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2338 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002339 num_params = SI_PARAM_POS_FIXED_PT+1;
2340 break;
2341
2342 default:
2343 assert(0 && "unimplemented shader");
2344 return;
Christian Königc4973212013-03-05 12:14:02 +01002345 }
Christian König55fe5cc2013-03-04 16:30:06 +01002346
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002347 assert(num_params <= Elements(params));
2348 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002349 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002350
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002351 for (i = 0; i <= last_sgpr; ++i) {
2352 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002353
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002354 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2355 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002356 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002357 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002358 else
2359 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002360 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002361
Michel Dänzer404b29d2013-11-21 16:45:28 +09002362 if (bld_base->info &&
2363 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2364 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002365 si_shader_ctx->ddxy_lds =
2366 LLVMAddGlobalInAddressSpace(gallivm->module,
2367 LLVMArrayType(i32, 64),
2368 "ddxy_lds",
2369 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002370}
Tom Stellarda75c6162012-01-06 17:38:37 -05002371
Christian König0f6cf2b2013-03-15 15:53:25 +01002372static void preload_constants(struct si_shader_context *si_shader_ctx)
2373{
2374 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2375 struct gallivm_state * gallivm = bld_base->base.gallivm;
2376 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002377 unsigned buf;
2378 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002379
Marek Olšákee2a8182014-07-07 23:27:19 +02002380 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002381 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002382
Marek Olšák2fd42002013-10-25 11:45:47 +02002383 if (num_const == 0)
2384 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002385
Marek Olšák2fd42002013-10-25 11:45:47 +02002386 /* Allocate space for the constant values */
2387 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002388
Marek Olšák2fd42002013-10-25 11:45:47 +02002389 /* Load the resource descriptor */
2390 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002391 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002392
Marek Olšák2fd42002013-10-25 11:45:47 +02002393 /* Load the constants, we rely on the code sinking to do the rest */
2394 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002395 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002396 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002397 si_shader_ctx->const_resource[buf],
2398 lp_build_const_int32(gallivm, i * 4),
2399 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002400 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002401 }
2402}
2403
Christian König1c100182013-03-17 16:02:42 +01002404static void preload_samplers(struct si_shader_context *si_shader_ctx)
2405{
2406 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2407 struct gallivm_state * gallivm = bld_base->base.gallivm;
2408 const struct tgsi_shader_info * info = bld_base->info;
2409
2410 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2411
2412 LLVMValueRef res_ptr, samp_ptr;
2413 LLVMValueRef offset;
2414
2415 if (num_samplers == 0)
2416 return;
2417
Christian König1c100182013-03-17 16:02:42 +01002418 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2419 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2420
2421 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2422 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002423 /* Resource */
2424 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002425 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002426
2427 /* Sampler */
2428 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002429 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002430
2431 /* FMASK resource */
2432 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002433 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2434 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002435 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002436 }
Christian König1c100182013-03-17 16:02:42 +01002437 }
2438}
2439
Marek Olšák8d03d922013-09-01 23:59:06 +02002440static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2441{
2442 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2443 struct gallivm_state * gallivm = bld_base->base.gallivm;
2444 unsigned i;
2445
Michel Dänzer67e385b2014-01-08 17:48:21 +09002446 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2447 si_shader_ctx->shader->key.vs.as_es ||
2448 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002449 return;
2450
2451 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002452 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002453
2454 /* Load the resources, we rely on the code sinking to do the rest */
2455 for (i = 0; i < 4; ++i) {
2456 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002457 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002458 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002459
Marek Olšákd7876082014-09-26 23:06:32 +02002460 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002461 }
2462 }
2463}
2464
Marek Olšákfc3b3352014-10-05 13:33:40 +02002465/**
2466 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
2467 * for later use.
2468 */
2469static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
2470{
2471 struct gallivm_state *gallivm =
2472 si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2473
2474 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2475 SI_PARAM_RW_BUFFERS);
2476
2477 if ((si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
2478 si_shader_ctx->shader->key.vs.as_es) ||
2479 si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY) {
2480 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_ESGS);
2481
2482 si_shader_ctx->esgs_ring =
2483 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2484 }
2485
2486 if (si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY ||
2487 si_shader_ctx->shader->is_gs_copy_shader) {
2488 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_GSVS);
2489
2490 si_shader_ctx->gsvs_ring =
2491 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2492 }
2493}
2494
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002495void si_shader_binary_read_config(const struct radeon_shader_binary *binary,
2496 struct si_shader *shader,
2497 unsigned symbol_offset)
Tom Stellard302f53d2012-10-25 13:50:10 -04002498{
Tom Stellard302f53d2012-10-25 13:50:10 -04002499 unsigned i;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002500 const unsigned char *config =
2501 radeon_shader_binary_config_start(binary, symbol_offset);
Tom Stellard302f53d2012-10-25 13:50:10 -04002502
Tom Stellardd50343d2013-04-04 16:21:06 -04002503 /* XXX: We may be able to emit some of these values directly rather than
2504 * extracting fields to be emitted later.
2505 */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002506
2507 for (i = 0; i < binary->config_size_per_symbol; i+= 8) {
2508 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
2509 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
Tom Stellardd50343d2013-04-04 16:21:06 -04002510 switch (reg) {
2511 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2512 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2513 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2514 case R_00B848_COMPUTE_PGM_RSRC1:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002515 shader->num_sgprs = MAX2(shader->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
2516 shader->num_vgprs = MAX2(shader->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
Tom Stellardd50343d2013-04-04 16:21:06 -04002517 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002518 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002519 shader->lds_size = MAX2(shader->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002520 break;
2521 case R_00B84C_COMPUTE_PGM_RSRC2:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002522 shader->lds_size = MAX2(shader->lds_size, G_00B84C_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002523 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002524 case R_0286CC_SPI_PS_INPUT_ENA:
2525 shader->spi_ps_input_ena = value;
2526 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002527 case R_00B860_COMPUTE_TMPRING_SIZE:
2528 /* WAVESIZE is in units of 256 dwords. */
2529 shader->scratch_bytes_per_wave =
2530 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2531 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002532 default:
2533 fprintf(stderr, "Warning: Compiler emitted unknown "
2534 "config register: 0x%x\n", reg);
2535 break;
2536 }
2537 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002538}
2539
2540int si_shader_binary_read(struct si_screen *sscreen,
2541 struct si_shader *shader,
2542 const struct radeon_shader_binary *binary)
2543{
2544
2545 unsigned i;
2546 unsigned code_size;
2547 unsigned char *ptr;
2548 bool dump = r600_can_dump_shader(&sscreen->b,
2549 shader->selector ? shader->selector->tokens : NULL);
2550
2551 if (dump && !binary->disassembled) {
2552 fprintf(stderr, "SI CODE:\n");
2553 for (i = 0; i < binary->code_size; i+=4 ) {
2554 fprintf(stderr, "@0x%x: %02x%02x%02x%02x\n", i, binary->code[i + 3],
2555 binary->code[i + 2], binary->code[i + 1],
2556 binary->code[i]);
2557 }
2558 }
2559
2560 si_shader_binary_read_config(binary, shader, 0);
Tom Stellard302f53d2012-10-25 13:50:10 -04002561
2562 /* copy new shader */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002563 code_size = binary->code_size + binary->rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002564 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002565 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002566 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002567 if (shader->bo == NULL) {
2568 return -ENOMEM;
2569 }
2570
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002571
2572 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_READ_WRITE);
2573 util_memcpy_cpu_to_le32(ptr, binary->code, binary->code_size);
2574 if (binary->rodata_size > 0) {
2575 ptr += binary->code_size;
2576 util_memcpy_cpu_to_le32(ptr, binary->rodata, binary->rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002577 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002578
Marek Olšák1abb1a92014-09-17 22:44:22 +02002579 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002580
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002581 return 0;
2582}
Tom Stellard302f53d2012-10-25 13:50:10 -04002583
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002584int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2585 LLVMModuleRef mod)
2586{
2587 int r = 0;
2588 struct radeon_shader_binary binary;
2589 bool dump = r600_can_dump_shader(&sscreen->b,
2590 shader->selector ? shader->selector->tokens : NULL);
2591 memset(&binary, 0, sizeof(binary));
2592 r = radeon_llvm_compile(mod, &binary,
Tom Stellard761e36b2014-10-15 12:24:30 -04002593 r600_get_llvm_processor_name(sscreen->b.family), dump, sscreen->tm);
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002594
2595 if (r) {
2596 return r;
2597 }
2598 r = si_shader_binary_read(sscreen, shader, &binary);
2599 FREE(binary.code);
2600 FREE(binary.config);
2601 FREE(binary.rodata);
Darren Powellbc866902014-03-31 18:00:28 -04002602 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002603}
2604
Michel Dänzer404b29d2013-11-21 16:45:28 +09002605/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002606static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002607 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002608 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002609{
2610 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2611 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2612 struct lp_build_context *base = &bld_base->base;
2613 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002614 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002615 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002616 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002617 LLVMValueRef args[9];
2618 int i, r;
2619
Marek Olšák02134cf2014-10-04 22:07:50 +02002620 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002621
2622 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002623 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002624
2625 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2626
2627 create_meta_data(si_shader_ctx);
2628 create_function(si_shader_ctx);
2629 preload_streamout_buffers(si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002630 preload_ring_buffers(si_shader_ctx);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002631
Marek Olšákfc3b3352014-10-05 13:33:40 +02002632 args[0] = si_shader_ctx->gsvs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002633 args[1] = lp_build_mul_imm(uint,
2634 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2635 si_shader_ctx->param_vertex_id),
2636 4);
2637 args[3] = uint->zero;
2638 args[4] = uint->one; /* OFFEN */
2639 args[5] = uint->zero; /* IDXEN */
2640 args[6] = uint->one; /* GLC */
2641 args[7] = uint->one; /* SLC */
2642 args[8] = uint->zero; /* TFE */
2643
2644 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002645 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002646 unsigned chan;
2647
Marek Olšák02134cf2014-10-04 22:07:50 +02002648 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002649 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002650
2651 for (chan = 0; chan < 4; chan++) {
2652 args[2] = lp_build_const_int32(gallivm,
2653 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002654 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002655
2656 outputs[i].values[chan] =
2657 LLVMBuildBitCast(gallivm->builder,
2658 build_intrinsic(gallivm->builder,
2659 "llvm.SI.buffer.load.dword.i32.i32",
2660 LLVMInt32TypeInContext(gallivm->context),
2661 args, 9,
2662 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2663 base->elem_type, "");
2664 }
2665 }
2666
Marek Olšák02134cf2014-10-04 22:07:50 +02002667 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002668
2669 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2670
2671 if (dump)
2672 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2673
Marek Olšák1abb1a92014-09-17 22:44:22 +02002674 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002675 bld_base->base.gallivm->module);
2676
2677 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2678
2679 FREE(outputs);
2680 return r;
2681}
2682
Marek Olšák1abb1a92014-09-17 22:44:22 +02002683int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002684{
Marek Olšák2774abd2014-09-16 18:45:33 +02002685 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002686 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002687 struct lp_build_tgsi_context * bld_base;
2688 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002689 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002690 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002691
2692 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2693 * conversion fails. */
2694 if (dump) {
2695 tgsi_dump(sel->tokens, 0);
2696 si_dump_streamout(&sel->so);
2697 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002698
Marek Olšák8c37c162014-09-16 18:40:07 +02002699 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002700
Michel Dänzercfebaf92012-08-31 19:04:08 +02002701 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002702 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2703 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2704
Marek Olšák52335682014-09-30 15:12:09 +02002705 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002706 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2707
Marek Olšák52335682014-09-30 15:12:09 +02002708 shader->uses_instanceid = sel->info.uses_instanceid;
2709 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002710 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002711
2712 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002713 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002714 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2715 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002716 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002717 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2718 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2719 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002720 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002721 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002722 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2723 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002724
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002725 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2726 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002727
Michel Dänzer404b29d2013-11-21 16:45:28 +09002728 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2729 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2730
Marek Olšák645b4712014-11-20 22:16:09 +01002731 if (HAVE_LLVM >= 0x0306) {
2732 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
2733 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
2734 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
2735 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
2736 }
2737
Christian Könige4ed5872013-03-21 18:02:52 +01002738 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002739 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002740 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2741 si_shader_ctx.shader = shader;
2742 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002743
Michel Dänzer51f89a02013-12-09 15:33:53 +09002744 switch (si_shader_ctx.type) {
2745 case TGSI_PROCESSOR_VERTEX:
2746 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002747 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002748 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2749 } else {
2750 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2751 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002752 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002753 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002754 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2755 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002756 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002757 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002758 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2759 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002760
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002761 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002762 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2763 shader->db_shader_control |=
2764 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2765 break;
2766 case TGSI_FS_DEPTH_LAYOUT_LESS:
2767 shader->db_shader_control |=
2768 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2769 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002770 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002771 break;
2772 default:
2773 assert(!"Unsupported shader type");
2774 return -1;
2775 }
2776
Christian König206f0592013-03-20 14:37:21 +01002777 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002778 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002779 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002780 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002781 preload_streamout_buffers(&si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002782 preload_ring_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002783
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002784 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2785 si_shader_ctx.gs_next_vertex =
2786 lp_build_alloca(bld_base->base.gallivm,
2787 bld_base->uint_bld.elem_type, "");
2788 }
2789
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002790 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002791 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002792 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002793 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002794
2795 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2796
2797 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002798 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002799 if (r) {
2800 fprintf(stderr, "LLVM failed to compile shader\n");
2801 goto out;
2802 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002803
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002804 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002805
2806 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002807 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002808 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002809 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002810 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002811 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2812 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002813 free(shader->gs_copy_shader);
2814 shader->gs_copy_shader = NULL;
2815 goto out;
2816 }
2817 }
2818
Tom Stellarda75c6162012-01-06 17:38:37 -05002819 tgsi_parse_free(&si_shader_ctx.parse);
2820
Michel Dänzer404b29d2013-11-21 16:45:28 +09002821out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002822 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002823 FREE(si_shader_ctx.constants[i]);
Tom Stellarda75c6162012-01-06 17:38:37 -05002824
Tom Stellard302f53d2012-10-25 13:50:10 -04002825 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002826}
2827
Marek Olšák2774abd2014-09-16 18:45:33 +02002828void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002829{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002830 if (shader->gs_copy_shader)
2831 si_shader_destroy(ctx, shader->gs_copy_shader);
2832
Marek Olšáka81c3e02013-08-14 01:04:39 +02002833 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002834 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002835}