blob: 6b4f9e6d1e8dbf323bc41cbddf00a70f9656fc3a [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"
36#include "radeon/radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010037#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010039#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_dump.h"
41
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010042#include "si_pipe.h"
43#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050044#include "sid.h"
45
Tom Stellarda75c6162012-01-06 17:38:37 -050046#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050047
Michel Dänzer404b29d2013-11-21 16:45:28 +090048struct si_shader_output_values
49{
50 LLVMValueRef values[4];
51 unsigned name;
Michel Dänzer67e385b2014-01-08 17:48:21 +090052 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090053};
54
Tom Stellarda75c6162012-01-06 17:38:37 -050055struct si_shader_context
56{
57 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050058 struct tgsi_parse_context parse;
59 struct tgsi_token * tokens;
Marek Olšák8c37c162014-09-16 18:40:07 +020060 struct si_shader *shader;
Tom Stellarda75c6162012-01-06 17:38:37 -050061 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020062 int param_streamout_config;
63 int param_streamout_write_index;
64 int param_streamout_offset[4];
65 int param_vertex_id;
66 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010067 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020068 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020069 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020070 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Christian König1c100182013-03-17 16:02:42 +010071 LLVMValueRef *resources;
72 LLVMValueRef *samplers;
Marek Olšák8d03d922013-09-01 23:59:06 +020073 LLVMValueRef so_buffers[4];
Michel Dänzerf07a96d2014-01-08 18:45:10 +090074 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050075};
76
77static struct si_shader_context * si_shader_context(
78 struct lp_build_tgsi_context * bld_base)
79{
80 return (struct si_shader_context *)bld_base;
81}
82
83
84#define PERSPECTIVE_BASE 0
85#define LINEAR_BASE 9
86
87#define SAMPLE_OFFSET 0
88#define CENTER_OFFSET 2
89#define CENTROID_OFSET 4
90
91#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040092#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020093#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040094#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050095
Michel Dänzer404b29d2013-11-21 16:45:28 +090096
97#define SENDMSG_GS 2
98#define SENDMSG_GS_DONE 3
99
100#define SENDMSG_GS_OP_NOP (0 << 4)
101#define SENDMSG_GS_OP_CUT (1 << 4)
102#define SENDMSG_GS_OP_EMIT (2 << 4)
103#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
104
Marek Olšáke29353f2014-09-17 22:17:02 +0200105/**
106 * Returns a unique index for a semantic name and index. The index must be
107 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
108 * calculated.
109 */
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200110unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
Marek Olšáke29353f2014-09-17 22:17:02 +0200111{
112 switch (semantic_name) {
113 case TGSI_SEMANTIC_POSITION:
114 return 0;
115 case TGSI_SEMANTIC_PSIZE:
116 return 1;
117 case TGSI_SEMANTIC_CLIPDIST:
118 assert(index <= 1);
119 return 2 + index;
120 case TGSI_SEMANTIC_CLIPVERTEX:
121 return 4;
122 case TGSI_SEMANTIC_COLOR:
123 assert(index <= 1);
124 return 5 + index;
125 case TGSI_SEMANTIC_BCOLOR:
126 assert(index <= 1);
127 return 7 + index;
128 case TGSI_SEMANTIC_FOG:
129 return 9;
130 case TGSI_SEMANTIC_EDGEFLAG:
131 return 10;
132 case TGSI_SEMANTIC_GENERIC:
133 assert(index <= 63-11);
134 return 11 + index;
135 default:
136 assert(0);
137 return 63;
138 }
139}
140
141/**
142 * Given a semantic name and index of a parameter and a mask of used parameters
143 * (inputs or outputs), return the index of the parameter in the list of all
144 * used parameters.
145 *
146 * For example, assume this list of parameters:
147 * POSITION, PSIZE, GENERIC0, GENERIC2
148 * which has the mask:
149 * 11000000000101
150 * Then:
151 * querying POSITION returns 0,
152 * querying PSIZE returns 1,
153 * querying GENERIC0 returns 2,
154 * querying GENERIC2 returns 3.
155 *
156 * Which can be used as an offset to a parameter buffer in units of vec4s.
157 */
158static int get_param_index(unsigned semantic_name, unsigned index,
159 uint64_t mask)
160{
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200161 unsigned unique_index = si_shader_io_get_unique_index(semantic_name, index);
Marek Olšáke29353f2014-09-17 22:17:02 +0200162 int i, param_index = 0;
163
164 /* If not present... */
165 if (!((1llu << unique_index) & mask))
166 return -1;
167
168 for (i = 0; mask; i++) {
169 uint64_t bit = 1llu << i;
170
171 if (bit & mask) {
172 if (i == unique_index)
173 return param_index;
174
175 mask &= ~bit;
176 param_index++;
177 }
178 }
179
180 assert(!"unreachable");
181 return -1;
182}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900183
Tom Stellard467f5162012-05-16 15:15:35 -0400184/**
185 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
186 *
187 * @param offset The offset parameter specifies the number of
188 * elements to offset, not the number of bytes or dwords. An element is the
189 * the type pointed to by the base_ptr parameter (e.g. int is the element of
190 * an int* pointer)
191 *
192 * When LLVM lowers the load instruction, it will convert the element offset
193 * into a dword offset automatically.
194 *
195 */
196static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100197 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400198 LLVMValueRef base_ptr,
199 LLVMValueRef offset)
200{
Christian König206f0592013-03-20 14:37:21 +0100201 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400202
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200203 LLVMValueRef indices[2] = {
204 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
205 offset
206 };
Christian König206f0592013-03-20 14:37:21 +0100207 LLVMValueRef computed_ptr = LLVMBuildGEP(
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200208 base->gallivm->builder, base_ptr, indices, 2, "");
Christian König206f0592013-03-20 14:37:21 +0100209
210 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
211 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
212 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400213}
214
Marek Olšákf317ce52013-09-05 15:39:57 +0200215static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100216 struct radeon_llvm_context * radeon_bld,
217 unsigned divisor)
218{
Marek Olšák8d03d922013-09-01 23:59:06 +0200219 struct si_shader_context *si_shader_ctx =
220 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100221 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
222
Marek Olšák8d03d922013-09-01 23:59:06 +0200223 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
224 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100225 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
226 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
227
228 if (divisor > 1)
229 result = LLVMBuildUDiv(gallivm->builder, result,
230 lp_build_const_int32(gallivm, divisor), "");
231
232 return result;
233}
234
Tom Stellarda75c6162012-01-06 17:38:37 -0500235static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900236 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500237 unsigned input_index,
238 const struct tgsi_full_declaration *decl)
239{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900240 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
241 struct gallivm_state *gallivm = base->gallivm;
242 struct si_shader_context *si_shader_ctx =
243 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100244 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
245
246 unsigned chan;
247
Tom Stellarda75c6162012-01-06 17:38:37 -0500248 LLVMValueRef t_list_ptr;
249 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400250 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500251 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100252 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400253 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500254 LLVMTypeRef vec4_type;
255 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500256
Tom Stellard467f5162012-05-16 15:15:35 -0400257 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100258 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500259
Michel Dänzer51f89a02013-12-09 15:33:53 +0900260 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400261
Christian König206f0592013-03-20 14:37:21 +0100262 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400263
264 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900265 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500266
Christian Königa0dca442013-03-22 15:59:22 +0100267 if (divisor) {
268 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200269 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200270 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100271 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200272 /* Load the buffer index for vertices. */
273 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
274 si_shader_ctx->param_vertex_id);
275 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
276 SI_PARAM_BASE_VERTEX);
277 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100278 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500279
280 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400281 args[0] = t_list;
282 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100283 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900284 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100285 "llvm.SI.vs.load.input", vec4_type, args, 3,
286 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500287
288 /* Break up the vec4 into individual components */
289 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900290 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500291 /* XXX: Use a helper function for this. There is one in
292 * tgsi_llvm.c. */
293 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900294 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500295 input, llvm_chan, "");
296 }
297}
298
Michel Dänzer404b29d2013-11-21 16:45:28 +0900299static LLVMValueRef fetch_input_gs(
300 struct lp_build_tgsi_context *bld_base,
301 const struct tgsi_full_src_register *reg,
302 enum tgsi_opcode_type type,
303 unsigned swizzle)
304{
305 struct lp_build_context *base = &bld_base->base;
306 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200307 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900308 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
309 struct gallivm_state *gallivm = base->gallivm;
310 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
311 LLVMValueRef vtx_offset;
312 LLVMValueRef t_list_ptr;
313 LLVMValueRef t_list;
314 LLVMValueRef args[9];
315 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200316 struct tgsi_shader_info *info = &shader->selector->info;
317 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
318 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900319
Marek Olšáke23fec12014-10-04 17:40:39 +0200320 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900321 if (swizzle == 0)
322 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
323 SI_PARAM_PRIMITIVE_ID);
324 else
325 return uint->zero;
326 }
327
Michel Dänzer404b29d2013-11-21 16:45:28 +0900328 if (!reg->Register.Dimension)
329 return NULL;
330
331 if (swizzle == ~0) {
332 LLVMValueRef values[TGSI_NUM_CHANNELS];
333 unsigned chan;
334 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
335 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
336 }
337 return lp_build_gather_values(bld_base->base.gallivm, values,
338 TGSI_NUM_CHANNELS);
339 }
340
341 /* Get the vertex offset parameter */
342 vtx_offset_param = reg->Dimension.Index;
343 if (vtx_offset_param < 2) {
344 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
345 } else {
346 assert(vtx_offset_param < 6);
347 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
348 }
349 vtx_offset = lp_build_mul_imm(uint,
350 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
351 vtx_offset_param),
352 4);
353
354 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +0900355 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
356 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900357 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900358 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900359
360 args[0] = t_list;
361 args[1] = vtx_offset;
362 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200363 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200364 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900365 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900366 args[3] = uint->zero;
367 args[4] = uint->one; /* OFFEN */
368 args[5] = uint->zero; /* IDXEN */
369 args[6] = uint->one; /* GLC */
370 args[7] = uint->zero; /* SLC */
371 args[8] = uint->zero; /* TFE */
372
373 return LLVMBuildBitCast(gallivm->builder,
374 build_intrinsic(gallivm->builder,
375 "llvm.SI.buffer.load.dword.i32.i32",
376 i32, args, 9,
377 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
378 tgsi2llvmtype(bld_base, type), "");
379}
380
Tom Stellarda75c6162012-01-06 17:38:37 -0500381static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900382 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500383 unsigned input_index,
384 const struct tgsi_full_declaration *decl)
385{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900386 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
387 struct si_shader_context *si_shader_ctx =
388 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200389 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900390 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
391 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400392 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900393 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100394
395 LLVMValueRef interp_param;
396 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500397
398 /* This value is:
399 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
400 * quad begins a new primitive. Bit 0 always needs
401 * to be unset)
402 * [32:16] ParamOffset
403 *
404 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900405 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200406 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500407
Christian König0666ffd2013-03-05 15:07:39 +0100408 unsigned chan;
409
Tom Stellard0fb1e682012-09-06 16:18:11 -0400410 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
411 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400412 unsigned soa_index =
413 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900414 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100415 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100416
417 if (chan == 3)
418 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900419 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100420 LLVMBuildFDiv(gallivm->builder,
421 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900422 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100423 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400424 }
425 return;
426 }
427
Michel Dänzer97078b12012-09-25 12:41:31 +0200428 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
429 LLVMValueRef face, is_face_positive;
430
Christian König0666ffd2013-03-05 15:07:39 +0100431 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
432
Michel Dänzer97078b12012-09-25 12:41:31 +0200433 is_face_positive = LLVMBuildFCmp(gallivm->builder,
434 LLVMRealUGT, face,
435 lp_build_const_float(gallivm, 0.0f),
436 "");
437
Michel Dänzer51f89a02013-12-09 15:33:53 +0900438 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200439 LLVMBuildSelect(gallivm->builder,
440 is_face_positive,
441 lp_build_const_float(gallivm, 1.0f),
442 lp_build_const_float(gallivm, 0.0f),
443 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900444 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
445 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200446 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900447 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200448 lp_build_const_float(gallivm, 1.0f);
449
450 return;
451 }
452
Marek Olšák8b057dd2014-10-04 22:15:07 +0200453 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200454 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200455 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200456
Francisco Jerez12799232012-04-30 18:27:52 +0200457 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500458 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100459 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500460 break;
461 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200462 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200463 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400464 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100465 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200466 else
Christian König0666ffd2013-03-05 15:07:39 +0100467 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200468 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200469 case TGSI_INTERPOLATE_COLOR:
470 if (si_shader_ctx->shader->key.ps.flatshade) {
471 interp_param = 0;
472 break;
473 }
474 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200475 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200476 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200477 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400478 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100479 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200480 else
Christian König0666ffd2013-03-05 15:07:39 +0100481 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500482 break;
483 default:
484 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
485 return;
486 }
487
Christian König0666ffd2013-03-05 15:07:39 +0100488 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
489
Tom Stellarda75c6162012-01-06 17:38:37 -0500490 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200491 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100492 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100493 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200494 LLVMValueRef face, is_face_positive;
495 LLVMValueRef back_attr_number =
496 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200497 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200498
Christian König0666ffd2013-03-05 15:07:39 +0100499 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
500
Michel Dänzer691f08d2012-09-06 18:03:38 +0200501 is_face_positive = LLVMBuildFCmp(gallivm->builder,
502 LLVMRealUGT, face,
503 lp_build_const_float(gallivm, 0.0f),
504 "");
505
Tom Stellarda75c6162012-01-06 17:38:37 -0500506 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100507 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200508 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
509 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
510 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
511 LLVMValueRef front, back;
512
513 args[0] = llvm_chan;
514 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900515 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100516 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100517 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200518
519 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900520 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100521 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100522 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200523
Michel Dänzer51f89a02013-12-09 15:33:53 +0900524 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200525 LLVMBuildSelect(gallivm->builder,
526 is_face_positive,
527 front,
528 back,
529 "");
530 }
531
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900532 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200533 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
534 LLVMValueRef args[4];
535
536 args[0] = uint->zero;
537 args[1] = attr_number;
538 args[2] = params;
539 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900540 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
541 build_intrinsic(gallivm->builder, intr_name,
542 input_type, args, args[3] ? 4 : 3,
543 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
544 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
545 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200546 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900547 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200548 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200549 } else {
550 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100551 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200552 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
553 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
554 args[0] = llvm_chan;
555 args[1] = attr_number;
556 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100557 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900558 radeon_bld->inputs[soa_index] =
559 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100560 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100561 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200562 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500563 }
564}
565
Marek Olšák5b06fc32014-05-06 18:12:40 +0200566static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
567{
568 struct gallivm_state *gallivm = &radeon_bld->gallivm;
569 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
570 SI_PARAM_ANCILLARY);
571 value = LLVMBuildLShr(gallivm->builder, value,
572 lp_build_const_int32(gallivm, 8), "");
573 value = LLVMBuildAnd(gallivm->builder, value,
574 lp_build_const_int32(gallivm, 0xf), "");
575 return value;
576}
577
Marek Olšák250aa932014-05-06 14:10:47 +0200578static LLVMValueRef load_const(LLVMBuilderRef builder, LLVMValueRef resource,
579 LLVMValueRef offset, LLVMTypeRef return_type)
580{
581 LLVMValueRef args[2] = {resource, offset};
582
583 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
584 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
585}
586
Christian Könige4ed5872013-03-21 18:02:52 +0100587static void declare_system_value(
588 struct radeon_llvm_context * radeon_bld,
589 unsigned index,
590 const struct tgsi_full_declaration *decl)
591{
Marek Olšák8d03d922013-09-01 23:59:06 +0200592 struct si_shader_context *si_shader_ctx =
593 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200594 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
595 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100596 LLVMValueRef value = 0;
597
598 switch (decl->Semantic.Name) {
599 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200600 value = LLVMGetParam(radeon_bld->main_fn,
601 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100602 break;
603
604 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200605 value = LLVMGetParam(radeon_bld->main_fn,
606 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100607 break;
608
Marek Olšák5b06fc32014-05-06 18:12:40 +0200609 case TGSI_SEMANTIC_SAMPLEID:
610 value = get_sample_id(radeon_bld);
611 break;
612
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200613 case TGSI_SEMANTIC_SAMPLEPOS:
614 {
615 LLVMBuilderRef builder = gallivm->builder;
616 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200617 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200618 LLVMValueRef resource = build_indexed_load(si_shader_ctx, desc, buf_index);
619
620 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
621 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
622 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
623
624 LLVMValueRef pos[4] = {
625 load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
626 load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
627 lp_build_const_float(gallivm, 0),
628 lp_build_const_float(gallivm, 0)
629 };
630 value = lp_build_gather_values(gallivm, pos, 4);
631 break;
632 }
633
Christian Könige4ed5872013-03-21 18:02:52 +0100634 default:
635 assert(!"unknown system value");
636 return;
637 }
638
639 radeon_bld->system_values[index] = value;
640}
641
Tom Stellarda75c6162012-01-06 17:38:37 -0500642static LLVMValueRef fetch_constant(
643 struct lp_build_tgsi_context * bld_base,
644 const struct tgsi_full_src_register *reg,
645 enum tgsi_opcode_type type,
646 unsigned swizzle)
647{
Christian König55fe5cc2013-03-04 16:30:06 +0100648 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500649 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100650 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200651 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500652
Christian König0f6cf2b2013-03-15 15:53:25 +0100653 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100654 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500655
Christian König8514f5a2013-02-04 17:46:42 +0100656 if (swizzle == LP_CHAN_ALL) {
657 unsigned chan;
658 LLVMValueRef values[4];
659 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
660 values[chan] = fetch_constant(bld_base, reg, type, chan);
661
662 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
663 }
664
Marek Olšák2fd42002013-10-25 11:45:47 +0200665 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100666 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100667
Marek Olšák2fd42002013-10-25 11:45:47 +0200668 if (!reg->Register.Indirect)
669 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
670
Christian König0f6cf2b2013-03-15 15:53:25 +0100671 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
672 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
673 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200674 addr = lp_build_add(&bld_base->uint_bld, addr,
675 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200676
Marek Olšák250aa932014-05-06 14:10:47 +0200677 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
678 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500679
Christian Königf5298b02013-02-28 14:50:07 +0100680 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500681}
682
Michel Dänzer26c71392012-08-24 12:03:11 +0200683/* Initialize arguments for the shader export intrinsic */
684static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900685 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200686 unsigned target,
687 LLVMValueRef *args)
688{
689 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
690 struct lp_build_context *uint =
691 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
692 struct lp_build_context *base = &bld_base->base;
693 unsigned compressed = 0;
694 unsigned chan;
695
Michel Dänzerf402acd2012-08-22 18:15:36 +0200696 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
697 int cbuf = target - V_008DFC_SQ_EXP_MRT;
698
699 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100700 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100701
702 if (compressed)
703 si_shader_ctx->shader->spi_shader_col_format |=
704 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
705 else
706 si_shader_ctx->shader->spi_shader_col_format |=
707 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200708
709 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200710 }
711 }
712
713 if (compressed) {
714 /* Pixel shader needs to pack output values before export */
715 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900716 args[0] = values[2 * chan];
717 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200718 args[chan + 5] =
719 build_intrinsic(base->gallivm->builder,
720 "llvm.SI.packf16",
721 LLVMInt32TypeInContext(base->gallivm->context),
722 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100723 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100724 args[chan + 7] = args[chan + 5] =
725 LLVMBuildBitCast(base->gallivm->builder,
726 args[chan + 5],
727 LLVMFloatTypeInContext(base->gallivm->context),
728 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200729 }
730
731 /* Set COMPR flag */
732 args[4] = uint->one;
733 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900734 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200735 /* +5 because the first output value will be
736 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900737 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200738
739 /* Clear COMPR flag */
740 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200741 }
742
743 /* XXX: This controls which components of the output
744 * registers actually get exported. (e.g bit 0 means export
745 * X component, bit 1 means export Y component, etc.) I'm
746 * hard coding this to 0xf for now. In the future, we might
747 * want to do something else. */
748 args[0] = lp_build_const_int32(base->gallivm, 0xf);
749
750 /* Specify whether the EXEC mask represents the valid mask */
751 args[1] = uint->zero;
752
753 /* Specify whether this is the last export */
754 args[2] = uint->zero;
755
756 /* Specify the target we are exporting */
757 args[3] = lp_build_const_int32(base->gallivm, target);
758
Michel Dänzer26c71392012-08-24 12:03:11 +0200759 /* XXX: We probably need to keep track of the output
760 * values, so we know what we are passing to the next
761 * stage. */
762}
763
Michel Dänzer404b29d2013-11-21 16:45:28 +0900764/* Load from output pointers and initialize arguments for the shader export intrinsic */
765static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
766 LLVMValueRef *out_ptr,
767 unsigned target,
768 LLVMValueRef *args)
769{
770 struct gallivm_state *gallivm = bld_base->base.gallivm;
771 LLVMValueRef values[4];
772 int i;
773
774 for (i = 0; i < 4; i++)
775 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
776
777 si_llvm_init_export_args(bld_base, values, target, args);
778}
779
Michel Dänzer7708a862012-11-02 15:57:30 +0100780static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900781 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100782{
783 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
784 struct gallivm_state *gallivm = bld_base->base.gallivm;
785
Christian Königa0dca442013-03-22 15:59:22 +0100786 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400787 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
788 SI_PARAM_ALPHA_REF);
789
Michel Dänzer7708a862012-11-02 15:57:30 +0100790 LLVMValueRef alpha_pass =
791 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100792 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900793 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400794 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100795 LLVMValueRef arg =
796 lp_build_select(&bld_base->base,
797 alpha_pass,
798 lp_build_const_float(gallivm, 1.0f),
799 lp_build_const_float(gallivm, -1.0f));
800
801 build_intrinsic(gallivm->builder,
802 "llvm.AMDGPU.kill",
803 LLVMVoidTypeInContext(gallivm->context),
804 &arg, 1, 0);
805 } else {
806 build_intrinsic(gallivm->builder,
807 "llvm.AMDGPU.kilp",
808 LLVMVoidTypeInContext(gallivm->context),
809 NULL, 0, 0);
810 }
Marek Olšákadc57972014-09-19 17:07:07 +0200811
812 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100813}
814
Michel Dänzere3befbc2013-05-15 18:09:50 +0200815static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900816 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200817{
818 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200819 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200820 struct lp_build_context *base = &bld_base->base;
821 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200822 unsigned reg_index;
823 unsigned chan;
824 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200825 LLVMValueRef base_elt;
826 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200827 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák2fd42002013-10-25 11:45:47 +0200828 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200829
Michel Dänzere3befbc2013-05-15 18:09:50 +0200830 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200831 LLVMValueRef *args = pos[2 + reg_index];
832
Marek Olšák8c37c162014-09-16 18:40:07 +0200833 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200834
Michel Dänzere3befbc2013-05-15 18:09:50 +0200835 args[5] =
836 args[6] =
837 args[7] =
838 args[8] = lp_build_const_float(base->gallivm, 0.0f);
839
840 /* Compute dot products of position and user clip plane vectors */
841 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
842 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200843 args[1] = lp_build_const_int32(base->gallivm,
844 ((reg_index * 4 + chan) * 4 +
845 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200846 base_elt = load_const(base->gallivm->builder, const_resource,
847 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200848 args[5 + chan] =
849 lp_build_add(base, args[5 + chan],
850 lp_build_mul(base, base_elt,
851 out_elts[const_chan]));
852 }
853 }
854
855 args[0] = lp_build_const_int32(base->gallivm, 0xf);
856 args[1] = uint->zero;
857 args[2] = uint->zero;
858 args[3] = lp_build_const_int32(base->gallivm,
859 V_008DFC_SQ_EXP_POS + 2 + reg_index);
860 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200861 }
862}
863
Marek Olšák8d03d922013-09-01 23:59:06 +0200864static void si_dump_streamout(struct pipe_stream_output_info *so)
865{
866 unsigned i;
867
868 if (so->num_outputs)
869 fprintf(stderr, "STREAMOUT\n");
870
871 for (i = 0; i < so->num_outputs; i++) {
872 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
873 so->output[i].start_component;
874 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
875 i, so->output[i].output_buffer,
876 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
877 so->output[i].register_index,
878 mask & 1 ? "x" : "",
879 mask & 2 ? "y" : "",
880 mask & 4 ? "z" : "",
881 mask & 8 ? "w" : "");
882 }
883}
884
885/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
886 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
887 * or v4i32 (num_channels=3,4). */
888static void build_tbuffer_store(struct si_shader_context *shader,
889 LLVMValueRef rsrc,
890 LLVMValueRef vdata,
891 unsigned num_channels,
892 LLVMValueRef vaddr,
893 LLVMValueRef soffset,
894 unsigned inst_offset,
895 unsigned dfmt,
896 unsigned nfmt,
897 unsigned offen,
898 unsigned idxen,
899 unsigned glc,
900 unsigned slc,
901 unsigned tfe)
902{
903 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
904 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
905 LLVMValueRef args[] = {
906 rsrc,
907 vdata,
908 LLVMConstInt(i32, num_channels, 0),
909 vaddr,
910 soffset,
911 LLVMConstInt(i32, inst_offset, 0),
912 LLVMConstInt(i32, dfmt, 0),
913 LLVMConstInt(i32, nfmt, 0),
914 LLVMConstInt(i32, offen, 0),
915 LLVMConstInt(i32, idxen, 0),
916 LLVMConstInt(i32, glc, 0),
917 LLVMConstInt(i32, slc, 0),
918 LLVMConstInt(i32, tfe, 0)
919 };
920
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900921 /* The instruction offset field has 12 bits */
922 assert(offen || inst_offset < (1 << 12));
923
Marek Olšák8d03d922013-09-01 23:59:06 +0200924 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
925 unsigned func = CLAMP(num_channels, 1, 3) - 1;
926 const char *types[] = {"i32", "v2i32", "v4i32"};
927 char name[256];
928 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
929
930 lp_build_intrinsic(gallivm->builder, name,
931 LLVMVoidTypeInContext(gallivm->context),
932 args, Elements(args));
933}
934
935static void build_streamout_store(struct si_shader_context *shader,
936 LLVMValueRef rsrc,
937 LLVMValueRef vdata,
938 unsigned num_channels,
939 LLVMValueRef vaddr,
940 LLVMValueRef soffset,
941 unsigned inst_offset)
942{
943 static unsigned dfmt[] = {
944 V_008F0C_BUF_DATA_FORMAT_32,
945 V_008F0C_BUF_DATA_FORMAT_32_32,
946 V_008F0C_BUF_DATA_FORMAT_32_32_32,
947 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
948 };
949 assert(num_channels >= 1 && num_channels <= 4);
950
951 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
952 inst_offset, dfmt[num_channels-1],
953 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
954}
955
956/* On SI, the vertex shader is responsible for writing streamout data
957 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900958static void si_llvm_emit_streamout(struct si_shader_context *shader,
959 struct si_shader_output_values *outputs,
960 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200961{
962 struct pipe_stream_output_info *so = &shader->shader->selector->so;
963 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
964 LLVMBuilderRef builder = gallivm->builder;
965 int i, j;
966 struct lp_build_if_state if_ctx;
967
968 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
969
970 LLVMValueRef so_param =
971 LLVMGetParam(shader->radeon_bld.main_fn,
972 shader->param_streamout_config);
973
974 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
975 LLVMValueRef so_vtx_count =
976 LLVMBuildAnd(builder,
977 LLVMBuildLShr(builder, so_param,
978 LLVMConstInt(i32, 16, 0), ""),
979 LLVMConstInt(i32, 127, 0), "");
980
981 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
982 NULL, 0, LLVMReadNoneAttribute);
983
984 /* can_emit = tid < so_vtx_count; */
985 LLVMValueRef can_emit =
986 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
987
988 /* Emit the streamout code conditionally. This actually avoids
989 * out-of-bounds buffer access. The hw tells us via the SGPR
990 * (so_vtx_count) which threads are allowed to emit streamout data. */
991 lp_build_if(&if_ctx, gallivm, can_emit);
992 {
993 /* The buffer offset is computed as follows:
994 * ByteOffset = streamout_offset[buffer_id]*4 +
995 * (streamout_write_index + thread_id)*stride[buffer_id] +
996 * attrib_offset
997 */
998
999 LLVMValueRef so_write_index =
1000 LLVMGetParam(shader->radeon_bld.main_fn,
1001 shader->param_streamout_write_index);
1002
1003 /* Compute (streamout_write_index + thread_id). */
1004 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1005
1006 /* Compute the write offset for each enabled buffer. */
1007 LLVMValueRef so_write_offset[4] = {};
1008 for (i = 0; i < 4; i++) {
1009 if (!so->stride[i])
1010 continue;
1011
1012 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1013 shader->param_streamout_offset[i]);
1014 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1015
1016 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1017 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1018 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1019 }
1020
Marek Olšák8d03d922013-09-01 23:59:06 +02001021 /* Write streamout data. */
1022 for (i = 0; i < so->num_outputs; i++) {
1023 unsigned buf_idx = so->output[i].output_buffer;
1024 unsigned reg = so->output[i].register_index;
1025 unsigned start = so->output[i].start_component;
1026 unsigned num_comps = so->output[i].num_components;
1027 LLVMValueRef out[4];
1028
1029 assert(num_comps && num_comps <= 4);
1030 if (!num_comps || num_comps > 4)
1031 continue;
1032
Marek Olšák558f7772014-10-04 22:37:23 +02001033 if (reg >= noutput)
1034 continue;
1035
Marek Olšák8d03d922013-09-01 23:59:06 +02001036 /* Load the output as int. */
1037 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001038 out[j] = LLVMBuildBitCast(builder,
1039 outputs[reg].values[start+j],
1040 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001041 }
1042
1043 /* Pack the output. */
1044 LLVMValueRef vdata = NULL;
1045
1046 switch (num_comps) {
1047 case 1: /* as i32 */
1048 vdata = out[0];
1049 break;
1050 case 2: /* as v2i32 */
1051 case 3: /* as v4i32 (aligned to 4) */
1052 case 4: /* as v4i32 */
1053 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1054 for (j = 0; j < num_comps; j++) {
1055 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1056 LLVMConstInt(i32, j, 0), "");
1057 }
1058 break;
1059 }
1060
1061 build_streamout_store(shader, shader->so_buffers[buf_idx],
1062 vdata, num_comps,
1063 so_write_offset[buf_idx],
1064 LLVMConstInt(i32, 0, 0),
1065 so->output[i].dst_offset*4);
1066 }
1067 }
1068 lp_build_endif(&if_ctx);
1069}
1070
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001071
Michel Dänzer404b29d2013-11-21 16:45:28 +09001072/* Generate export instructions for hardware VS shader stage */
1073static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1074 struct si_shader_output_values *outputs,
1075 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001076{
1077 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001078 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001079 struct lp_build_context * base = &bld_base->base;
1080 struct lp_build_context * uint =
1081 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001082 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001083 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001084 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001085 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001086 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001087 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001088 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001089 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001090
Michel Dänzer67e385b2014-01-08 17:48:21 +09001091 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1092 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001093 }
1094
Michel Dänzer404b29d2013-11-21 16:45:28 +09001095 for (i = 0; i < noutput; i++) {
1096 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001097 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001098
Michel Dänzer0afeea52013-05-02 14:53:17 +02001099handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001100 /* Select the correct target */
1101 switch(semantic_name) {
1102 case TGSI_SEMANTIC_PSIZE:
1103 shader->vs_out_misc_write = true;
1104 shader->vs_out_point_size = true;
1105 psize_value = outputs[i].values[0];
1106 continue;
1107 case TGSI_SEMANTIC_EDGEFLAG:
1108 shader->vs_out_misc_write = true;
1109 shader->vs_out_edgeflag = true;
1110 edgeflag_value = outputs[i].values[0];
1111 continue;
1112 case TGSI_SEMANTIC_LAYER:
1113 shader->vs_out_misc_write = true;
1114 shader->vs_out_layer = true;
1115 layer_value = outputs[i].values[0];
1116 continue;
1117 case TGSI_SEMANTIC_POSITION:
1118 target = V_008DFC_SQ_EXP_POS;
1119 break;
1120 case TGSI_SEMANTIC_COLOR:
1121 case TGSI_SEMANTIC_BCOLOR:
1122 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001123 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001124 param_count++;
1125 break;
1126 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001127 shader->clip_dist_write |=
Marek Olšáka9592cd2014-10-04 19:09:09 +02001128 0xf << (semantic_index * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001129 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1130 break;
1131 case TGSI_SEMANTIC_CLIPVERTEX:
1132 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1133 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001134 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001135 case TGSI_SEMANTIC_FOG:
1136 case TGSI_SEMANTIC_GENERIC:
1137 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001138 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001139 param_count++;
1140 break;
1141 default:
1142 target = 0;
1143 fprintf(stderr,
1144 "Warning: SI unhandled vs output type:%d\n",
1145 semantic_name);
1146 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001147
Michel Dänzer404b29d2013-11-21 16:45:28 +09001148 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001149
Michel Dänzer404b29d2013-11-21 16:45:28 +09001150 if (target >= V_008DFC_SQ_EXP_POS &&
1151 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1152 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1153 args, sizeof(args));
1154 } else {
1155 lp_build_intrinsic(base->gallivm->builder,
1156 "llvm.SI.export",
1157 LLVMVoidTypeInContext(base->gallivm->context),
1158 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001159 }
1160
1161 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1162 semantic_name = TGSI_SEMANTIC_GENERIC;
1163 goto handle_semantic;
1164 }
1165 }
1166
1167 /* We need to add the position output manually if it's missing. */
1168 if (!pos_args[0][0]) {
1169 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1170 pos_args[0][1] = uint->zero; /* EXEC mask */
1171 pos_args[0][2] = uint->zero; /* last export? */
1172 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1173 pos_args[0][4] = uint->zero; /* COMPR flag */
1174 pos_args[0][5] = base->zero; /* X */
1175 pos_args[0][6] = base->zero; /* Y */
1176 pos_args[0][7] = base->zero; /* Z */
1177 pos_args[0][8] = base->one; /* W */
1178 }
1179
1180 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1181 if (shader->vs_out_misc_write) {
1182 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1183 shader->vs_out_point_size |
1184 (shader->vs_out_edgeflag << 1) |
1185 (shader->vs_out_layer << 2));
1186 pos_args[1][1] = uint->zero; /* EXEC mask */
1187 pos_args[1][2] = uint->zero; /* last export? */
1188 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1189 pos_args[1][4] = uint->zero; /* COMPR flag */
1190 pos_args[1][5] = base->zero; /* X */
1191 pos_args[1][6] = base->zero; /* Y */
1192 pos_args[1][7] = base->zero; /* Z */
1193 pos_args[1][8] = base->zero; /* W */
1194
Michel Dänzer404b29d2013-11-21 16:45:28 +09001195 if (shader->vs_out_point_size)
1196 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001197
1198 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001199 /* The output is a float, but the hw expects an integer
1200 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001201 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1202 edgeflag_value,
1203 bld_base->uint_bld.elem_type, "");
1204 edgeflag_value = lp_build_min(&bld_base->int_bld,
1205 edgeflag_value,
1206 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001207
1208 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001209 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1210 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001211 base->elem_type, "");
1212 }
1213
Michel Dänzer404b29d2013-11-21 16:45:28 +09001214 if (shader->vs_out_layer)
1215 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001216 }
1217
1218 for (i = 0; i < 4; i++)
1219 if (pos_args[i][0])
1220 shader->nr_pos_exports++;
1221
1222 pos_idx = 0;
1223 for (i = 0; i < 4; i++) {
1224 if (!pos_args[i][0])
1225 continue;
1226
1227 /* Specify the target we are exporting */
1228 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1229
1230 if (pos_idx == shader->nr_pos_exports)
1231 /* Specify that this is the last export */
1232 pos_args[i][2] = uint->one;
1233
1234 lp_build_intrinsic(base->gallivm->builder,
1235 "llvm.SI.export",
1236 LLVMVoidTypeInContext(base->gallivm->context),
1237 pos_args[i], 9);
1238 }
1239}
1240
Michel Dänzer404b29d2013-11-21 16:45:28 +09001241static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1242{
1243 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1244 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001245 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001246 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001247 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001248 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1249 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001250 LLVMValueRef t_list_ptr;
1251 LLVMValueRef t_list;
1252 unsigned chan;
1253 int i;
1254
Michel Dänzer404b29d2013-11-21 16:45:28 +09001255 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001256 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1257 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001258 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001259 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001260
Marek Olšák216cf862014-10-04 17:04:05 +02001261 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001262 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001263 si_shader_ctx->radeon_bld.soa.outputs[i];
1264 int param_index = get_param_index(info->output_semantic_name[i],
1265 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001266 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001267
Marek Olšáke29353f2014-09-17 22:17:02 +02001268 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001269 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001270
1271 for (chan = 0; chan < 4; chan++) {
1272 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001273 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1274
1275 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001276 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001277 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001278 V_008F0C_BUF_DATA_FORMAT_32,
1279 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001280 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001281 }
1282 }
1283}
1284
1285static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1286{
1287 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1288 struct gallivm_state *gallivm = bld_base->base.gallivm;
1289 LLVMValueRef args[2];
1290
1291 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1292 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1293 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1294 LLVMVoidTypeInContext(gallivm->context), args, 2,
1295 LLVMNoUnwindAttribute);
1296}
1297
1298static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1299{
1300 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1301 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001302 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001303 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001304 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001305
Marek Olšákec0d1682014-10-04 22:33:36 +02001306 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001307
Marek Olšákec0d1682014-10-04 22:33:36 +02001308 for (i = 0; i < info->num_outputs; i++) {
1309 outputs[i].name = info->output_semantic_name[i];
1310 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001311
Marek Olšákec0d1682014-10-04 22:33:36 +02001312 for (j = 0; j < 4; j++)
1313 outputs[i].values[j] =
1314 LLVMBuildLoad(gallivm->builder,
1315 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1316 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001317 }
1318
Marek Olšákec0d1682014-10-04 22:33:36 +02001319 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001320 FREE(outputs);
1321}
1322
Michel Dänzer51f89a02013-12-09 15:33:53 +09001323static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1324{
1325 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001326 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001327 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001328 struct lp_build_context * uint = &bld_base->uint_bld;
1329 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001330 LLVMValueRef args[9];
1331 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001332 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001333 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001334
Marek Olšák75e97e22014-10-04 23:13:50 +02001335 for (i = 0; i < info->num_outputs; i++) {
1336 unsigned semantic_name = info->output_semantic_name[i];
1337 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001338 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001339
Marek Olšák75e97e22014-10-04 23:13:50 +02001340 /* Select the correct target */
1341 switch (semantic_name) {
1342 case TGSI_SEMANTIC_POSITION:
1343 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001344 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001345 case TGSI_SEMANTIC_STENCIL:
1346 stencil_index = i;
1347 continue;
1348 case TGSI_SEMANTIC_SAMPLEMASK:
1349 samplemask_index = i;
1350 continue;
1351 case TGSI_SEMANTIC_COLOR:
1352 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1353 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1354 LLVMBuildStore(bld_base->base.gallivm->builder,
1355 bld_base->base.one,
1356 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001357
Marek Olšák75e97e22014-10-04 23:13:50 +02001358 if (semantic_index == 0 &&
1359 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1360 si_alpha_test(bld_base,
1361 si_shader_ctx->radeon_bld.soa.outputs[i]);
1362 break;
1363 default:
1364 target = 0;
1365 fprintf(stderr,
1366 "Warning: SI unhandled fs output type:%d\n",
1367 semantic_name);
1368 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001369
Marek Olšák75e97e22014-10-04 23:13:50 +02001370 si_llvm_init_export_args_load(bld_base,
1371 si_shader_ctx->radeon_bld.soa.outputs[i],
1372 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001373
Marek Olšák75e97e22014-10-04 23:13:50 +02001374 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1375 /* If there is an export instruction waiting to be emitted, do so now. */
1376 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001377 lp_build_intrinsic(base->gallivm->builder,
1378 "llvm.SI.export",
1379 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001380 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001381 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001382
1383 /* This instruction will be emitted at the end of the shader. */
1384 memcpy(last_args, args, sizeof(args));
1385
1386 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1387 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1388 semantic_index == 0 &&
1389 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1390 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1391 si_llvm_init_export_args_load(bld_base,
1392 si_shader_ctx->radeon_bld.soa.outputs[i],
1393 V_008DFC_SQ_EXP_MRT + c, args);
1394 lp_build_intrinsic(base->gallivm->builder,
1395 "llvm.SI.export",
1396 LLVMVoidTypeInContext(base->gallivm->context),
1397 args, 9);
1398 }
1399 }
1400 } else {
1401 lp_build_intrinsic(base->gallivm->builder,
1402 "llvm.SI.export",
1403 LLVMVoidTypeInContext(base->gallivm->context),
1404 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001405 }
1406 }
1407
Marek Olšákd0e8b652014-05-06 20:04:31 +02001408 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001409 LLVMValueRef out_ptr;
1410 unsigned mask = 0;
1411
1412 /* Specify the target we are exporting */
1413 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1414
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001415 args[5] = base->zero; /* R, depth */
1416 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1417 args[7] = base->zero; /* B, sample mask */
1418 args[8] = base->zero; /* A, alpha to mask */
1419
Michel Dänzer1a616c12012-11-13 17:35:09 +01001420 if (depth_index >= 0) {
1421 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1422 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1423 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001424 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001425 }
1426
1427 if (stencil_index >= 0) {
1428 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001429 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001430 /* Only setting the stencil component bit (0x2) here
1431 * breaks some stencil piglit tests
1432 */
1433 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001434 si_shader_ctx->shader->db_shader_control |=
1435 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001436 }
1437
Marek Olšákd0e8b652014-05-06 20:04:31 +02001438 if (samplemask_index >= 0) {
1439 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1440 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1441 mask |= 0xf; /* Set all components. */
1442 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1443 }
1444
1445 if (samplemask_index >= 0)
1446 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1447 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001448 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1449 else
1450 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1451
Michel Dänzer1a616c12012-11-13 17:35:09 +01001452 /* Specify which components to enable */
1453 args[0] = lp_build_const_int32(base->gallivm, mask);
1454
1455 args[1] =
1456 args[2] =
1457 args[4] = uint->zero;
1458
1459 if (last_args[0])
1460 lp_build_intrinsic(base->gallivm->builder,
1461 "llvm.SI.export",
1462 LLVMVoidTypeInContext(base->gallivm->context),
1463 args, 9);
1464 else
1465 memcpy(last_args, args, sizeof(args));
1466 }
1467
Michel Dänzer51f89a02013-12-09 15:33:53 +09001468 if (!last_args[0]) {
1469 /* Specify which components to enable */
1470 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001471
Michel Dänzer51f89a02013-12-09 15:33:53 +09001472 /* Specify the target we are exporting */
1473 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001474
Michel Dänzer51f89a02013-12-09 15:33:53 +09001475 /* Set COMPR flag to zero to export data as 32-bit */
1476 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001477
Michel Dänzer51f89a02013-12-09 15:33:53 +09001478 /* dummy bits */
1479 last_args[5]= uint->zero;
1480 last_args[6]= uint->zero;
1481 last_args[7]= uint->zero;
1482 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001483 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001484
1485 /* Specify whether the EXEC mask represents the valid mask */
1486 last_args[1] = uint->one;
1487
1488 /* Specify that this is the last export */
1489 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1490
1491 lp_build_intrinsic(base->gallivm->builder,
1492 "llvm.SI.export",
1493 LLVMVoidTypeInContext(base->gallivm->context),
1494 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001495}
1496
Marek Olšák4855acd2013-08-06 15:08:54 +02001497static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1498 struct lp_build_tgsi_context * bld_base,
1499 struct lp_build_emit_data * emit_data);
1500
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001501static bool tgsi_is_shadow_sampler(unsigned target)
1502{
1503 return target == TGSI_TEXTURE_SHADOW1D ||
1504 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1505 target == TGSI_TEXTURE_SHADOW2D ||
1506 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1507 target == TGSI_TEXTURE_SHADOWCUBE ||
1508 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1509 target == TGSI_TEXTURE_SHADOWRECT;
1510}
1511
Marek Olšák877bb522014-06-25 03:12:46 +02001512static const struct lp_build_tgsi_action tex_action;
1513
Tom Stellarda75c6162012-01-06 17:38:37 -05001514static void tex_fetch_args(
1515 struct lp_build_tgsi_context * bld_base,
1516 struct lp_build_emit_data * emit_data)
1517{
Christian König55fe5cc2013-03-04 16:30:06 +01001518 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001519 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001520 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001521 unsigned opcode = inst->Instruction.Opcode;
1522 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001523 LLVMValueRef coords[4];
1524 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001525 int ref_pos;
1526 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001527 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001528 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001529 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1530 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001531 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001532
1533 if (target == TGSI_TEXTURE_BUFFER) {
1534 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1535 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1536 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1537 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1538
Marek Olšák4f3f0432014-07-07 22:49:15 +02001539 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001540 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1541 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1542 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1543 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1544
1545 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1546 emit_data->args[0] = res;
1547 emit_data->args[1] = bld_base->uint_bld.zero;
1548 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1549 emit_data->arg_count = 3;
1550 return;
1551 }
Tom Stellard467f5162012-05-16 15:15:35 -04001552
Michel Dänzer120efee2013-01-25 12:10:11 +01001553 /* Fetch and project texture coordinates */
1554 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001555 for (chan = 0; chan < 3; chan++ ) {
1556 coords[chan] = lp_build_emit_fetch(bld_base,
1557 emit_data->inst, 0,
1558 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001559 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001560 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1561 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001562 coords[chan],
1563 coords[3]);
1564 }
1565
Michel Dänzer120efee2013-01-25 12:10:11 +01001566 if (opcode == TGSI_OPCODE_TXP)
1567 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001568
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001569 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001570 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001571 /* The offsets are six-bit signed integers packed like this:
1572 * X=[5:0], Y=[13:8], and Z=[21:16].
1573 */
1574 LLVMValueRef offset[3], pack;
1575
1576 assert(inst->Texture.NumOffsets == 1);
1577
1578 for (chan = 0; chan < 3; chan++) {
1579 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1580 emit_data->inst, 0, chan);
1581 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1582 lp_build_const_int32(gallivm, 0x3f), "");
1583 if (chan)
1584 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1585 lp_build_const_int32(gallivm, chan*8), "");
1586 }
1587
1588 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1589 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1590 address[count++] = pack;
1591 }
1592
Michel Dänzer120efee2013-01-25 12:10:11 +01001593 /* Pack LOD bias value */
1594 if (opcode == TGSI_OPCODE_TXB)
1595 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001596 if (opcode == TGSI_OPCODE_TXB2)
1597 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001598
Michel Dänzer120efee2013-01-25 12:10:11 +01001599 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001600 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001601 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1602 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1603 } else {
1604 assert(ref_pos >= 0);
1605 address[count++] = coords[ref_pos];
1606 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001607 }
1608
Marek Olšákb279f0142014-07-04 03:19:58 +02001609 if (target == TGSI_TEXTURE_CUBE ||
1610 target == TGSI_TEXTURE_CUBE_ARRAY ||
1611 target == TGSI_TEXTURE_SHADOWCUBE ||
1612 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1613 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1614
Michel Dänzera6b83c02013-02-21 16:10:55 +01001615 /* Pack user derivatives */
1616 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001617 int num_deriv_channels, param;
1618
1619 switch (target) {
1620 case TGSI_TEXTURE_3D:
1621 num_deriv_channels = 3;
1622 break;
1623 case TGSI_TEXTURE_2D:
1624 case TGSI_TEXTURE_SHADOW2D:
1625 case TGSI_TEXTURE_RECT:
1626 case TGSI_TEXTURE_SHADOWRECT:
1627 case TGSI_TEXTURE_2D_ARRAY:
1628 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1629 case TGSI_TEXTURE_CUBE:
1630 case TGSI_TEXTURE_SHADOWCUBE:
1631 case TGSI_TEXTURE_CUBE_ARRAY:
1632 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1633 num_deriv_channels = 2;
1634 break;
1635 case TGSI_TEXTURE_1D:
1636 case TGSI_TEXTURE_SHADOW1D:
1637 case TGSI_TEXTURE_1D_ARRAY:
1638 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1639 num_deriv_channels = 1;
1640 break;
1641 default:
1642 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001643 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001644
1645 for (param = 1; param <= 2; param++)
1646 for (chan = 0; chan < num_deriv_channels; chan++)
1647 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001648 }
1649
Michel Dänzer120efee2013-01-25 12:10:11 +01001650 /* Pack texture coordinates */
1651 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001652 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001653 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001654 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001655 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001656
Marek Olšákd2bd6342013-09-18 15:40:21 +02001657 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001658 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001659 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001660 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001661 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001662
1663 if (count > 16) {
1664 assert(!"Cannot handle more than 16 texture address parameters");
1665 count = 16;
1666 }
1667
1668 for (chan = 0; chan < count; chan++ ) {
1669 address[chan] = LLVMBuildBitCast(gallivm->builder,
1670 address[chan],
1671 LLVMInt32TypeInContext(gallivm->context),
1672 "");
1673 }
1674
Marek Olšák4855acd2013-08-06 15:08:54 +02001675 /* Adjust the sample index according to FMASK.
1676 *
1677 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1678 * which is the identity mapping. Each nibble says which physical sample
1679 * should be fetched to get that sample.
1680 *
1681 * For example, 0x11111100 means there are only 2 samples stored and
1682 * the second sample covers 3/4 of the pixel. When reading samples 0
1683 * and 1, return physical sample 0 (determined by the first two 0s
1684 * in FMASK), otherwise return physical sample 1.
1685 *
1686 * The sample index should be adjusted as follows:
1687 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1688 */
1689 if (target == TGSI_TEXTURE_2D_MSAA ||
1690 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1691 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1692 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001693 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001694 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001695 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001696
Marek Olšákd2bd6342013-09-18 15:40:21 +02001697 memcpy(txf_address, address, sizeof(txf_address));
1698
1699 if (target == TGSI_TEXTURE_2D_MSAA) {
1700 txf_address[2] = bld_base->uint_bld.zero;
1701 }
1702 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001703
1704 /* Pad to a power-of-two size. */
1705 while (txf_count < util_next_power_of_two(txf_count))
1706 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1707
1708 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001709 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1710 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1711 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001712 txf_emit_data.chan = 0;
1713 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001714 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001715 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001716 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001717 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001718 txf_emit_data.arg_count = 3;
1719
Marek Olšák877bb522014-06-25 03:12:46 +02001720 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001721
1722 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001723 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1724 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1725
1726 /* Apply the formula. */
1727 LLVMValueRef fmask =
1728 LLVMBuildExtractElement(gallivm->builder,
1729 txf_emit_data.output[0],
1730 uint_bld->zero, "");
1731
Marek Olšákd2bd6342013-09-18 15:40:21 +02001732 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001733
1734 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001735 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001736
1737 LLVMValueRef shifted_fmask =
1738 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1739
1740 LLVMValueRef final_sample =
1741 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1742
1743 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1744 * resource descriptor is 0 (invalid),
1745 */
1746 LLVMValueRef fmask_desc =
1747 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001748 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001749 LLVMVectorType(uint_bld->elem_type, 8), "");
1750
1751 LLVMValueRef fmask_word1 =
1752 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1753 uint_bld->one, "");
1754
1755 LLVMValueRef word1_is_nonzero =
1756 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1757 fmask_word1, uint_bld->zero, "");
1758
Marek Olšákd2bd6342013-09-18 15:40:21 +02001759 /* Replace the MSAA sample index. */
1760 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001761 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001762 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001763 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001764
Michel Dänzer36231112013-05-02 09:44:45 +02001765 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001766 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001767
1768 if (opcode == TGSI_OPCODE_TXF) {
1769 /* add tex offsets */
1770 if (inst->Texture.NumOffsets) {
1771 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1772 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1773 const struct tgsi_texture_offset * off = inst->TexOffsets;
1774
1775 assert(inst->Texture.NumOffsets == 1);
1776
Marek Olšákdefedc02013-09-18 15:36:38 +02001777 switch (target) {
1778 case TGSI_TEXTURE_3D:
1779 address[2] = lp_build_add(uint_bld, address[2],
1780 bld->immediates[off->Index][off->SwizzleZ]);
1781 /* fall through */
1782 case TGSI_TEXTURE_2D:
1783 case TGSI_TEXTURE_SHADOW2D:
1784 case TGSI_TEXTURE_RECT:
1785 case TGSI_TEXTURE_SHADOWRECT:
1786 case TGSI_TEXTURE_2D_ARRAY:
1787 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001788 address[1] =
1789 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001790 bld->immediates[off->Index][off->SwizzleY]);
1791 /* fall through */
1792 case TGSI_TEXTURE_1D:
1793 case TGSI_TEXTURE_SHADOW1D:
1794 case TGSI_TEXTURE_1D_ARRAY:
1795 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1796 address[0] =
1797 lp_build_add(uint_bld, address[0],
1798 bld->immediates[off->Index][off->SwizzleX]);
1799 break;
1800 /* texture offsets do not apply to other texture targets */
1801 }
Michel Dänzer36231112013-05-02 09:44:45 +02001802 }
1803
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001804 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1805 emit_data->arg_count = 3;
1806
Michel Dänzer36231112013-05-02 09:44:45 +02001807 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001808 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001809 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001810 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001811 opcode == TGSI_OPCODE_LODQ ||
1812 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001813 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1814 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1815 target == TGSI_TEXTURE_2D_ARRAY ||
1816 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1817 target == TGSI_TEXTURE_CUBE_ARRAY ||
1818 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1819 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001820 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001821
Marek Olšák57f3da92014-06-16 16:53:42 +02001822 if (opcode == TGSI_OPCODE_TG4) {
1823 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001824
Marek Olšák57f3da92014-06-16 16:53:42 +02001825 /* DMASK was repurposed for GATHER4. 4 components are always
1826 * returned and DMASK works like a swizzle - it selects
1827 * the component to fetch. The only valid DMASK values are
1828 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1829 * (red,red,red,red) etc.) The ISA document doesn't mention
1830 * this.
1831 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001832
Marek Olšák57f3da92014-06-16 16:53:42 +02001833 /* Get the component index from src1.x for Gather4. */
1834 if (!tgsi_is_shadow_sampler(target)) {
1835 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1836 LLVMValueRef comp_imm;
1837 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001838
Marek Olšák57f3da92014-06-16 16:53:42 +02001839 assert(src1.File == TGSI_FILE_IMMEDIATE);
1840
1841 comp_imm = imms[src1.Index][src1.SwizzleX];
1842 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1843 gather_comp = CLAMP(gather_comp, 0, 3);
1844 }
1845
1846 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001847 }
1848
Marek Olšák4855acd2013-08-06 15:08:54 +02001849 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001850 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001851 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1852 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1853 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1854 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1855 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1856 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1857 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1858
1859 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001860
1861 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001862 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001863 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001864 } else {
1865 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1866 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001867 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001868
1869 emit_data->dst_type = LLVMVectorType(
1870 LLVMFloatTypeInContext(gallivm->context),
1871 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001872 }
1873
Marek Olšák2484daa2014-04-22 21:23:29 +02001874 /* The fetch opcode has been converted to a 2D array fetch.
1875 * This simplifies the LLVM backend. */
1876 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1877 target = TGSI_TEXTURE_2D_ARRAY;
1878 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1879 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1880
Michel Dänzer120efee2013-01-25 12:10:11 +01001881 /* Pad to power of two vector */
1882 while (count < util_next_power_of_two(count))
1883 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1884
Christian Königccf3e8f2013-03-26 15:09:27 +01001885 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001886}
1887
Michel Dänzer07eddc42013-02-06 15:43:10 +01001888static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1889 struct lp_build_tgsi_context * bld_base,
1890 struct lp_build_emit_data * emit_data)
1891{
1892 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001893 unsigned opcode = emit_data->inst->Instruction.Opcode;
1894 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001895 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001896 bool has_offset = HAVE_LLVM >= 0x0305 ?
1897 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001898
Marek Olšák877bb522014-06-25 03:12:46 +02001899 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001900 emit_data->output[emit_data->chan] = build_intrinsic(
1901 base->gallivm->builder,
1902 "llvm.SI.vs.load.input", emit_data->dst_type,
1903 emit_data->args, emit_data->arg_count,
1904 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1905 return;
1906 }
1907
Marek Olšák877bb522014-06-25 03:12:46 +02001908 if (opcode == TGSI_OPCODE_TG4 ||
1909 opcode == TGSI_OPCODE_LODQ ||
1910 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1911 bool is_shadow = tgsi_is_shadow_sampler(target);
1912 const char *name = "llvm.SI.image.sample";
1913 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001914
Marek Olšák877bb522014-06-25 03:12:46 +02001915 switch (opcode) {
1916 case TGSI_OPCODE_TEX:
1917 case TGSI_OPCODE_TEX2:
1918 case TGSI_OPCODE_TXP:
1919 break;
1920 case TGSI_OPCODE_TXB:
1921 case TGSI_OPCODE_TXB2:
1922 infix = ".b";
1923 break;
1924 case TGSI_OPCODE_TXL:
1925 case TGSI_OPCODE_TXL2:
1926 infix = ".l";
1927 break;
1928 case TGSI_OPCODE_TXD:
1929 infix = ".d";
1930 break;
1931 case TGSI_OPCODE_TG4:
1932 name = "llvm.SI.gather4";
1933 break;
1934 case TGSI_OPCODE_LODQ:
1935 name = "llvm.SI.getlod";
1936 is_shadow = false;
1937 has_offset = false;
1938 break;
1939 default:
1940 assert(0);
1941 return;
1942 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001943
Marek Olšák877bb522014-06-25 03:12:46 +02001944 /* Add the type and suffixes .c, .o if needed. */
1945 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1946 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1947 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001948
Marek Olšák877bb522014-06-25 03:12:46 +02001949 emit_data->output[emit_data->chan] = build_intrinsic(
1950 base->gallivm->builder, intr_name, emit_data->dst_type,
1951 emit_data->args, emit_data->arg_count,
1952 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1953 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001954 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001955 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001956
Marek Olšák877bb522014-06-25 03:12:46 +02001957 switch (opcode) {
1958 case TGSI_OPCODE_TEX:
1959 case TGSI_OPCODE_TEX2:
1960 case TGSI_OPCODE_TXP:
1961 name = "llvm.SI.sample";
1962 break;
1963 case TGSI_OPCODE_TXB:
1964 case TGSI_OPCODE_TXB2:
1965 name = "llvm.SI.sampleb";
1966 break;
1967 case TGSI_OPCODE_TXD:
1968 name = "llvm.SI.sampled";
1969 break;
1970 case TGSI_OPCODE_TXF:
1971 name = "llvm.SI.imageload";
1972 break;
1973 case TGSI_OPCODE_TXL:
1974 case TGSI_OPCODE_TXL2:
1975 name = "llvm.SI.samplel";
1976 break;
1977 default:
1978 assert(0);
1979 return;
1980 }
1981
Marek Olšákd859bdb2014-07-14 19:40:14 +02001982 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1983 v16i8 = LLVMVectorType(i8, 16);
1984 v32i8 = LLVMVectorType(i8, 32);
1985
1986 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1987 emit_data->args[1], v32i8, "");
1988 if (opcode != TGSI_OPCODE_TXF) {
1989 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1990 emit_data->args[2], v16i8, "");
1991 }
1992
Marek Olšák877bb522014-06-25 03:12:46 +02001993 sprintf(intr_name, "%s.v%ui32", name,
1994 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1995
1996 emit_data->output[emit_data->chan] = build_intrinsic(
1997 base->gallivm->builder, intr_name, emit_data->dst_type,
1998 emit_data->args, emit_data->arg_count,
1999 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2000 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002001}
2002
Michel Dänzer0495adb2013-05-06 12:45:14 +02002003static void txq_fetch_args(
2004 struct lp_build_tgsi_context * bld_base,
2005 struct lp_build_emit_data * emit_data)
2006{
2007 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2008 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002009 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002010 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002011
Marek Olšák2484daa2014-04-22 21:23:29 +02002012 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002013 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2014 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2015
2016 /* Read the size from the buffer descriptor directly. */
2017 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2018 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2019 size = LLVMBuildExtractElement(gallivm->builder, size,
2020 lp_build_const_int32(gallivm, 2), "");
2021 emit_data->args[0] = size;
2022 return;
2023 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002024
2025 /* Mip level */
2026 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2027
2028 /* Resource */
2029 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2030
Marek Olšák2484daa2014-04-22 21:23:29 +02002031 /* Texture target */
2032 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2033 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2034 target = TGSI_TEXTURE_2D_ARRAY;
2035
Michel Dänzer0495adb2013-05-06 12:45:14 +02002036 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002037 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002038
2039 emit_data->arg_count = 3;
2040
2041 emit_data->dst_type = LLVMVectorType(
2042 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2043 4);
2044}
2045
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002046static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2047 struct lp_build_tgsi_context * bld_base,
2048 struct lp_build_emit_data * emit_data)
2049{
Marek Olšák2484daa2014-04-22 21:23:29 +02002050 unsigned target = emit_data->inst->Texture.Texture;
2051
2052 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002053 /* Just return the buffer size. */
2054 emit_data->output[emit_data->chan] = emit_data->args[0];
2055 return;
2056 }
2057
2058 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002059
2060 /* Divide the number of layers by 6 to get the number of cubes. */
2061 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2062 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2063 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2064 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2065 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2066
2067 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2068 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2069 z = LLVMBuildSDiv(builder, z, six, "");
2070
2071 emit_data->output[emit_data->chan] =
2072 LLVMBuildInsertElement(builder, v4, z, two, "");
2073 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002074}
2075
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002076static void si_llvm_emit_ddxy(
2077 const struct lp_build_tgsi_action * action,
2078 struct lp_build_tgsi_context * bld_base,
2079 struct lp_build_emit_data * emit_data)
2080{
2081 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2082 struct gallivm_state *gallivm = bld_base->base.gallivm;
2083 struct lp_build_context * base = &bld_base->base;
2084 const struct tgsi_full_instruction *inst = emit_data->inst;
2085 unsigned opcode = inst->Instruction.Opcode;
2086 LLVMValueRef indices[2];
2087 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2088 LLVMValueRef tl, trbl, result[4];
2089 LLVMTypeRef i32;
2090 unsigned swizzle[4];
2091 unsigned c;
2092
2093 i32 = LLVMInt32TypeInContext(gallivm->context);
2094
2095 indices[0] = bld_base->uint_bld.zero;
2096 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2097 NULL, 0, LLVMReadNoneAttribute);
2098 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2099 indices, 2, "");
2100
2101 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2102 lp_build_const_int32(gallivm, 0xfffffffc), "");
2103 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2104 indices, 2, "");
2105
2106 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2107 lp_build_const_int32(gallivm,
2108 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2109 "");
2110 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2111 indices, 2, "");
2112
2113 for (c = 0; c < 4; ++c) {
2114 unsigned i;
2115
2116 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2117 for (i = 0; i < c; ++i) {
2118 if (swizzle[i] == swizzle[c]) {
2119 result[c] = result[i];
2120 break;
2121 }
2122 }
2123 if (i != c)
2124 continue;
2125
2126 LLVMBuildStore(gallivm->builder,
2127 LLVMBuildBitCast(gallivm->builder,
2128 lp_build_emit_fetch(bld_base, inst, 0, c),
2129 i32, ""),
2130 store_ptr);
2131
2132 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2133 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2134
2135 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2136 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2137
2138 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2139 }
2140
2141 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2142}
2143
Michel Dänzer404b29d2013-11-21 16:45:28 +09002144/* Emit one vertex from the geometry shader */
2145static void si_llvm_emit_vertex(
2146 const struct lp_build_tgsi_action *action,
2147 struct lp_build_tgsi_context *bld_base,
2148 struct lp_build_emit_data *emit_data)
2149{
2150 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002151 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002152 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002153 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002154 struct gallivm_state *gallivm = bld_base->base.gallivm;
2155 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002156 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2157 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002158 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002159 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002160 LLVMValueRef t_list_ptr;
2161 LLVMValueRef t_list;
2162 LLVMValueRef args[2];
2163 unsigned chan;
2164 int i;
2165
2166 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002167 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2168 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002169 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002170 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002171
Michel Dänzer404b29d2013-11-21 16:45:28 +09002172 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002173 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002174
2175 /* If this thread has already emitted the declared maximum number of
2176 * vertices, kill it: excessive vertex emissions are not supposed to
2177 * have any effect, and GS threads have no externally observable
2178 * effects other than emitting vertices.
2179 */
2180 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2181 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002182 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002183 kill = lp_build_select(&bld_base->base, can_emit,
2184 lp_build_const_float(gallivm, 1.0f),
2185 lp_build_const_float(gallivm, -1.0f));
2186 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2187 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2188
Marek Olšák02134cf2014-10-04 22:07:50 +02002189 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002190 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002191 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002192
2193 for (chan = 0; chan < 4; chan++) {
2194 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002195 LLVMValueRef voffset =
2196 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002197 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002198
2199 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2200 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002201
2202 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2203
2204 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2205 voffset, soffset, 0,
2206 V_008F0C_BUF_DATA_FORMAT_32,
2207 V_008F0C_BUF_NUM_FORMAT_UINT,
2208 1, 0, 1, 1, 0);
2209 }
2210 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002211 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2212 lp_build_const_int32(gallivm, 1));
2213 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002214
2215 /* Signal vertex emission */
2216 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2217 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2218 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2219 LLVMVoidTypeInContext(gallivm->context), args, 2,
2220 LLVMNoUnwindAttribute);
2221}
2222
2223/* Cut one primitive from the geometry shader */
2224static void si_llvm_emit_primitive(
2225 const struct lp_build_tgsi_action *action,
2226 struct lp_build_tgsi_context *bld_base,
2227 struct lp_build_emit_data *emit_data)
2228{
2229 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2230 struct gallivm_state *gallivm = bld_base->base.gallivm;
2231 LLVMValueRef args[2];
2232
2233 /* Signal primitive cut */
2234 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2235 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2236 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2237 LLVMVoidTypeInContext(gallivm->context), args, 2,
2238 LLVMNoUnwindAttribute);
2239}
2240
Tom Stellarda75c6162012-01-06 17:38:37 -05002241static const struct lp_build_tgsi_action tex_action = {
2242 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002243 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002244};
2245
Michel Dänzer0495adb2013-05-06 12:45:14 +02002246static const struct lp_build_tgsi_action txq_action = {
2247 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002248 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002249 .intr_name = "llvm.SI.resinfo"
2250};
2251
Christian König206f0592013-03-20 14:37:21 +01002252static void create_meta_data(struct si_shader_context *si_shader_ctx)
2253{
2254 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2255 LLVMValueRef args[3];
2256
2257 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2258 args[1] = 0;
2259 args[2] = lp_build_const_int32(gallivm, 1);
2260
2261 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2262}
2263
Marek Olšák4f3f0432014-07-07 22:49:15 +02002264static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2265{
2266 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2267 CONST_ADDR_SPACE);
2268}
2269
Christian König55fe5cc2013-03-04 16:30:06 +01002270static void create_function(struct si_shader_context *si_shader_ctx)
2271{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002272 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2273 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002274 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002275 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002276 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002277
Christian König55fe5cc2013-03-04 16:30:06 +01002278 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002279 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002280 f32 = LLVMFloatTypeInContext(gallivm->context);
2281 v2i32 = LLVMVectorType(i32, 2);
2282 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002283 v4i32 = LLVMVectorType(i32, 4);
2284 v8i32 = LLVMVectorType(i32, 8);
2285 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002286
Marek Olšákee2a8182014-07-07 23:27:19 +02002287 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002288 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002289 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2290 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002291 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002292
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002293 switch (si_shader_ctx->type) {
2294 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002295 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002296 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002297 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002298 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002299 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002300
Michel Dänzer404b29d2013-11-21 16:45:28 +09002301 if (shader->key.vs.as_es) {
2302 params[SI_PARAM_ES2GS_OFFSET] = i32;
2303 num_params++;
2304 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002305 if (shader->is_gs_copy_shader) {
2306 last_array_pointer = SI_PARAM_CONST;
2307 num_params = SI_PARAM_CONST+1;
2308 }
2309
Michel Dänzer404b29d2013-11-21 16:45:28 +09002310 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002311
Michel Dänzer404b29d2013-11-21 16:45:28 +09002312 /* Streamout SGPRs. */
2313 if (shader->selector->so.num_outputs) {
2314 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2315 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2316 }
2317 /* A streamout buffer offset is loaded if the stride is non-zero. */
2318 for (i = 0; i < 4; i++) {
2319 if (!shader->selector->so.stride[i])
2320 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002321
Michel Dänzer404b29d2013-11-21 16:45:28 +09002322 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2323 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002324 }
2325
2326 last_sgpr = num_params-1;
2327
2328 /* VGPRs */
2329 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2330 params[num_params++] = i32; /* unused*/
2331 params[num_params++] = i32; /* unused */
2332 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002333 break;
Christian König0666ffd2013-03-05 15:07:39 +01002334
Michel Dänzer404b29d2013-11-21 16:45:28 +09002335 case TGSI_PROCESSOR_GEOMETRY:
2336 params[SI_PARAM_GS2VS_OFFSET] = i32;
2337 params[SI_PARAM_GS_WAVE_ID] = i32;
2338 last_sgpr = SI_PARAM_GS_WAVE_ID;
2339
2340 /* VGPRs */
2341 params[SI_PARAM_VTX0_OFFSET] = i32;
2342 params[SI_PARAM_VTX1_OFFSET] = i32;
2343 params[SI_PARAM_PRIMITIVE_ID] = i32;
2344 params[SI_PARAM_VTX2_OFFSET] = i32;
2345 params[SI_PARAM_VTX3_OFFSET] = i32;
2346 params[SI_PARAM_VTX4_OFFSET] = i32;
2347 params[SI_PARAM_VTX5_OFFSET] = i32;
2348 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2349 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2350 break;
2351
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002352 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002353 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002354 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002355 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002356 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2357 params[SI_PARAM_PERSP_CENTER] = v2i32;
2358 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2359 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2360 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2361 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2362 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2363 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2364 params[SI_PARAM_POS_X_FLOAT] = f32;
2365 params[SI_PARAM_POS_Y_FLOAT] = f32;
2366 params[SI_PARAM_POS_Z_FLOAT] = f32;
2367 params[SI_PARAM_POS_W_FLOAT] = f32;
2368 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002369 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002370 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2371 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002372 num_params = SI_PARAM_POS_FIXED_PT+1;
2373 break;
2374
2375 default:
2376 assert(0 && "unimplemented shader");
2377 return;
Christian Königc4973212013-03-05 12:14:02 +01002378 }
Christian König55fe5cc2013-03-04 16:30:06 +01002379
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002380 assert(num_params <= Elements(params));
2381 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002382 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002383
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002384 for (i = 0; i <= last_sgpr; ++i) {
2385 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002386
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002387 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2388 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002389 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002390 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002391 else
2392 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002393 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002394
Michel Dänzer404b29d2013-11-21 16:45:28 +09002395 if (bld_base->info &&
2396 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2397 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002398 si_shader_ctx->ddxy_lds =
2399 LLVMAddGlobalInAddressSpace(gallivm->module,
2400 LLVMArrayType(i32, 64),
2401 "ddxy_lds",
2402 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002403}
Tom Stellarda75c6162012-01-06 17:38:37 -05002404
Christian König0f6cf2b2013-03-15 15:53:25 +01002405static void preload_constants(struct si_shader_context *si_shader_ctx)
2406{
2407 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2408 struct gallivm_state * gallivm = bld_base->base.gallivm;
2409 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002410 unsigned buf;
2411 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002412
Marek Olšákee2a8182014-07-07 23:27:19 +02002413 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002414 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002415
Marek Olšák2fd42002013-10-25 11:45:47 +02002416 if (num_const == 0)
2417 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002418
Marek Olšák2fd42002013-10-25 11:45:47 +02002419 /* Allocate space for the constant values */
2420 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002421
Marek Olšák2fd42002013-10-25 11:45:47 +02002422 /* Load the resource descriptor */
2423 si_shader_ctx->const_resource[buf] =
2424 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002425
Marek Olšák2fd42002013-10-25 11:45:47 +02002426 /* Load the constants, we rely on the code sinking to do the rest */
2427 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002428 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002429 load_const(gallivm->builder,
2430 si_shader_ctx->const_resource[buf],
2431 lp_build_const_int32(gallivm, i * 4),
2432 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002433 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002434 }
2435}
2436
Christian König1c100182013-03-17 16:02:42 +01002437static void preload_samplers(struct si_shader_context *si_shader_ctx)
2438{
2439 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2440 struct gallivm_state * gallivm = bld_base->base.gallivm;
2441 const struct tgsi_shader_info * info = bld_base->info;
2442
2443 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2444
2445 LLVMValueRef res_ptr, samp_ptr;
2446 LLVMValueRef offset;
2447
2448 if (num_samplers == 0)
2449 return;
2450
2451 /* Allocate space for the values */
Marek Olšákee2a8182014-07-07 23:27:19 +02002452 si_shader_ctx->resources = CALLOC(SI_NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002453 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2454
2455 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2456 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2457
2458 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2459 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002460 /* Resource */
2461 offset = lp_build_const_int32(gallivm, i);
2462 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2463
2464 /* Sampler */
2465 offset = lp_build_const_int32(gallivm, i);
2466 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002467
2468 /* FMASK resource */
2469 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002470 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2471 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšák4855acd2013-08-06 15:08:54 +02002472 build_indexed_load(si_shader_ctx, res_ptr, offset);
2473 }
Christian König1c100182013-03-17 16:02:42 +01002474 }
2475}
2476
Marek Olšák8d03d922013-09-01 23:59:06 +02002477static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2478{
2479 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2480 struct gallivm_state * gallivm = bld_base->base.gallivm;
2481 unsigned i;
2482
Michel Dänzer67e385b2014-01-08 17:48:21 +09002483 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2484 si_shader_ctx->shader->key.vs.as_es ||
2485 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002486 return;
2487
2488 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002489 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002490
2491 /* Load the resources, we rely on the code sinking to do the rest */
2492 for (i = 0; i < 4; ++i) {
2493 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002494 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002495 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002496
2497 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2498 }
2499 }
2500}
2501
Marek Olšák1abb1a92014-09-17 22:44:22 +02002502int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2503 LLVMModuleRef mod)
Tom Stellard302f53d2012-10-25 13:50:10 -04002504{
Darren Powellbc866902014-03-31 18:00:28 -04002505 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002506 unsigned i;
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002507 unsigned char *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002508 struct radeon_shader_binary binary;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002509 bool dump = r600_can_dump_shader(&sscreen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002510 shader->selector ? shader->selector->tokens : NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002511 const char * gpu_family = r600_get_llvm_processor_name(sscreen->b.family);
Tom Stellard9ba31052014-07-14 16:49:08 -04002512 unsigned code_size;
Darren Powellbc866902014-03-31 18:00:28 -04002513
2514 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002515 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002516 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2517
2518 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002519 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002520 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002521 for (i = 0; i < binary.code_size; i+=4 ) {
2522 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2523 binary.code[i + 2], binary.code[i + 1],
2524 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002525 }
2526 }
2527
Tom Stellardd50343d2013-04-04 16:21:06 -04002528 /* XXX: We may be able to emit some of these values directly rather than
2529 * extracting fields to be emitted later.
2530 */
Darren Powellbc866902014-03-31 18:00:28 -04002531 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002532 for (i = 0; i < binary.config_size; i+= 8) {
2533 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2534 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2535 switch (reg) {
2536 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2537 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2538 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2539 case R_00B848_COMPUTE_PGM_RSRC1:
2540 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2541 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2542 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002543 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2544 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2545 break;
2546 case R_00B84C_COMPUTE_PGM_RSRC2:
2547 shader->lds_size = G_00B84C_LDS_SIZE(value);
2548 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002549 case R_0286CC_SPI_PS_INPUT_ENA:
2550 shader->spi_ps_input_ena = value;
2551 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002552 case R_00B860_COMPUTE_TMPRING_SIZE:
2553 /* WAVESIZE is in units of 256 dwords. */
2554 shader->scratch_bytes_per_wave =
2555 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2556 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002557 default:
2558 fprintf(stderr, "Warning: Compiler emitted unknown "
2559 "config register: 0x%x\n", reg);
2560 break;
2561 }
2562 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002563
2564 /* copy new shader */
Tom Stellard9ba31052014-07-14 16:49:08 -04002565 code_size = binary.code_size + binary.rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002566 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002567 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002568 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002569 if (shader->bo == NULL) {
2570 return -ENOMEM;
2571 }
2572
Marek Olšák1abb1a92014-09-17 22:44:22 +02002573 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_WRITE);
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002574 util_memcpy_cpu_to_le32(ptr, binary.code, binary.code_size);
2575 if (binary.rodata_size > 0) {
2576 ptr += binary.code_size;
2577 util_memcpy_cpu_to_le32(ptr, binary.rodata, binary.rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002578 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002579
Marek Olšák1abb1a92014-09-17 22:44:22 +02002580 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002581
Tom Stellard7782d192013-04-04 09:57:13 -07002582 free(binary.code);
2583 free(binary.config);
Tom Stellard9ba31052014-07-14 16:49:08 -04002584 free(binary.rodata);
Tom Stellard302f53d2012-10-25 13:50:10 -04002585
Darren Powellbc866902014-03-31 18:00:28 -04002586 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002587}
2588
Michel Dänzer404b29d2013-11-21 16:45:28 +09002589/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002590static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002591 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002592 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002593{
2594 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2595 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2596 struct lp_build_context *base = &bld_base->base;
2597 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002598 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002599 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002600 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002601 LLVMValueRef t_list_ptr, t_list;
2602 LLVMValueRef args[9];
2603 int i, r;
2604
Marek Olšák02134cf2014-10-04 22:07:50 +02002605 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002606
2607 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002608 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002609
2610 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2611
2612 create_meta_data(si_shader_ctx);
2613 create_function(si_shader_ctx);
2614 preload_streamout_buffers(si_shader_ctx);
2615
2616 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002617 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2618 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002619 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002620 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002621
2622 args[0] = t_list;
2623 args[1] = lp_build_mul_imm(uint,
2624 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2625 si_shader_ctx->param_vertex_id),
2626 4);
2627 args[3] = uint->zero;
2628 args[4] = uint->one; /* OFFEN */
2629 args[5] = uint->zero; /* IDXEN */
2630 args[6] = uint->one; /* GLC */
2631 args[7] = uint->one; /* SLC */
2632 args[8] = uint->zero; /* TFE */
2633
2634 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002635 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002636 unsigned chan;
2637
Marek Olšák02134cf2014-10-04 22:07:50 +02002638 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002639 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002640
2641 for (chan = 0; chan < 4; chan++) {
2642 args[2] = lp_build_const_int32(gallivm,
2643 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002644 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002645
2646 outputs[i].values[chan] =
2647 LLVMBuildBitCast(gallivm->builder,
2648 build_intrinsic(gallivm->builder,
2649 "llvm.SI.buffer.load.dword.i32.i32",
2650 LLVMInt32TypeInContext(gallivm->context),
2651 args, 9,
2652 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2653 base->elem_type, "");
2654 }
2655 }
2656
Marek Olšák02134cf2014-10-04 22:07:50 +02002657 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002658
2659 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2660
2661 if (dump)
2662 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2663
Marek Olšák1abb1a92014-09-17 22:44:22 +02002664 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002665 bld_base->base.gallivm->module);
2666
2667 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2668
2669 FREE(outputs);
2670 return r;
2671}
2672
Marek Olšák1abb1a92014-09-17 22:44:22 +02002673int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002674{
Marek Olšák2774abd2014-09-16 18:45:33 +02002675 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002676 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002677 struct lp_build_tgsi_context * bld_base;
2678 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002679 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002680 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002681
2682 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2683 * conversion fails. */
2684 if (dump) {
2685 tgsi_dump(sel->tokens, 0);
2686 si_dump_streamout(&sel->so);
2687 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002688
Marek Olšák8c37c162014-09-16 18:40:07 +02002689 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002690
Michel Dänzercfebaf92012-08-31 19:04:08 +02002691 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002692 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2693 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2694
Marek Olšák52335682014-09-30 15:12:09 +02002695 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002696 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2697
Marek Olšák52335682014-09-30 15:12:09 +02002698 shader->uses_instanceid = sel->info.uses_instanceid;
2699 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002700 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002701
2702 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002703 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002704 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2705 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002706 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002707 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2708 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2709 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002710 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002711 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002712 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2713 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002714
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002715 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2716 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002717
Michel Dänzer404b29d2013-11-21 16:45:28 +09002718 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2719 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2720
Christian Könige4ed5872013-03-21 18:02:52 +01002721 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002722 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002723 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2724 si_shader_ctx.shader = shader;
2725 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002726
Michel Dänzer51f89a02013-12-09 15:33:53 +09002727 switch (si_shader_ctx.type) {
2728 case TGSI_PROCESSOR_VERTEX:
2729 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002730 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002731 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2732 } else {
2733 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2734 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002735 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002736 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002737 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2738 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002739 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002740 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002741 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2742 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002743
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002744 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002745 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2746 shader->db_shader_control |=
2747 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2748 break;
2749 case TGSI_FS_DEPTH_LAYOUT_LESS:
2750 shader->db_shader_control |=
2751 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2752 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002753 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002754 break;
2755 default:
2756 assert(!"Unsupported shader type");
2757 return -1;
2758 }
2759
Christian König206f0592013-03-20 14:37:21 +01002760 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002761 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002762 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002763 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002764 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002765
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002766 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2767 si_shader_ctx.gs_next_vertex =
2768 lp_build_alloca(bld_base->base.gallivm,
2769 bld_base->uint_bld.elem_type, "");
2770 }
2771
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002772 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002773 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002774 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002775 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002776
2777 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2778
2779 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002780 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002781 if (r) {
2782 fprintf(stderr, "LLVM failed to compile shader\n");
2783 goto out;
2784 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002785
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002786 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002787
2788 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002789 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002790 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002791 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002792 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002793 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2794 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002795 free(shader->gs_copy_shader);
2796 shader->gs_copy_shader = NULL;
2797 goto out;
2798 }
2799 }
2800
Tom Stellarda75c6162012-01-06 17:38:37 -05002801 tgsi_parse_free(&si_shader_ctx.parse);
2802
Michel Dänzer404b29d2013-11-21 16:45:28 +09002803out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002804 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002805 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002806 FREE(si_shader_ctx.resources);
2807 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002808
Tom Stellard302f53d2012-10-25 13:50:10 -04002809 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002810}
2811
Marek Olšák2774abd2014-09-16 18:45:33 +02002812void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002813{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002814 if (shader->gs_copy_shader)
2815 si_shader_destroy(ctx, shader->gs_copy_shader);
2816
Marek Olšáka81c3e02013-08-14 01:04:39 +02002817 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002818 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002819}