blob: 6f2fb7b99792f5a4e76aa5019cc9f176c062a4b7 [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) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900429 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Marek Olšáke827bb62014-10-16 16:20:26 +0200430 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900431 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
432 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200433 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900434 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200435 lp_build_const_float(gallivm, 1.0f);
436
437 return;
438 }
439
Marek Olšák8b057dd2014-10-04 22:15:07 +0200440 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200441 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200442 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200443
Francisco Jerez12799232012-04-30 18:27:52 +0200444 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500445 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100446 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500447 break;
448 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200449 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200450 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400451 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100452 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200453 else
Christian König0666ffd2013-03-05 15:07:39 +0100454 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200455 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200456 case TGSI_INTERPOLATE_COLOR:
457 if (si_shader_ctx->shader->key.ps.flatshade) {
458 interp_param = 0;
459 break;
460 }
461 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200462 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200463 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200464 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400465 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100466 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200467 else
Christian König0666ffd2013-03-05 15:07:39 +0100468 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500469 break;
470 default:
471 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
472 return;
473 }
474
Christian König0666ffd2013-03-05 15:07:39 +0100475 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
476
Tom Stellarda75c6162012-01-06 17:38:37 -0500477 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200478 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100479 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100480 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200481 LLVMValueRef face, is_face_positive;
482 LLVMValueRef back_attr_number =
483 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200484 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200485
Christian König0666ffd2013-03-05 15:07:39 +0100486 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
487
Michel Dänzer691f08d2012-09-06 18:03:38 +0200488 is_face_positive = LLVMBuildFCmp(gallivm->builder,
489 LLVMRealUGT, face,
490 lp_build_const_float(gallivm, 0.0f),
491 "");
492
Tom Stellarda75c6162012-01-06 17:38:37 -0500493 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100494 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200495 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
496 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
497 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
498 LLVMValueRef front, back;
499
500 args[0] = llvm_chan;
501 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900502 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100503 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100504 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200505
506 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900507 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100508 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100509 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200510
Michel Dänzer51f89a02013-12-09 15:33:53 +0900511 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200512 LLVMBuildSelect(gallivm->builder,
513 is_face_positive,
514 front,
515 back,
516 "");
517 }
518
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900519 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200520 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
521 LLVMValueRef args[4];
522
523 args[0] = uint->zero;
524 args[1] = attr_number;
525 args[2] = params;
526 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900527 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
528 build_intrinsic(gallivm->builder, intr_name,
529 input_type, args, args[3] ? 4 : 3,
530 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
531 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
532 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200533 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900534 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200535 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200536 } else {
537 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100538 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200539 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
540 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
541 args[0] = llvm_chan;
542 args[1] = attr_number;
543 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100544 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900545 radeon_bld->inputs[soa_index] =
546 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100547 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100548 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200549 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500550 }
551}
552
Marek Olšák5b06fc32014-05-06 18:12:40 +0200553static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
554{
555 struct gallivm_state *gallivm = &radeon_bld->gallivm;
556 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
557 SI_PARAM_ANCILLARY);
558 value = LLVMBuildLShr(gallivm->builder, value,
559 lp_build_const_int32(gallivm, 8), "");
560 value = LLVMBuildAnd(gallivm->builder, value,
561 lp_build_const_int32(gallivm, 0xf), "");
562 return value;
563}
564
Marek Olšák250aa932014-05-06 14:10:47 +0200565static LLVMValueRef load_const(LLVMBuilderRef builder, LLVMValueRef resource,
566 LLVMValueRef offset, LLVMTypeRef return_type)
567{
568 LLVMValueRef args[2] = {resource, offset};
569
570 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
571 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
572}
573
Christian Könige4ed5872013-03-21 18:02:52 +0100574static void declare_system_value(
575 struct radeon_llvm_context * radeon_bld,
576 unsigned index,
577 const struct tgsi_full_declaration *decl)
578{
Marek Olšák8d03d922013-09-01 23:59:06 +0200579 struct si_shader_context *si_shader_ctx =
580 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200581 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
582 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100583 LLVMValueRef value = 0;
584
585 switch (decl->Semantic.Name) {
586 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200587 value = LLVMGetParam(radeon_bld->main_fn,
588 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100589 break;
590
591 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200592 value = LLVMGetParam(radeon_bld->main_fn,
593 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100594 break;
595
Marek Olšák5b06fc32014-05-06 18:12:40 +0200596 case TGSI_SEMANTIC_SAMPLEID:
597 value = get_sample_id(radeon_bld);
598 break;
599
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200600 case TGSI_SEMANTIC_SAMPLEPOS:
601 {
602 LLVMBuilderRef builder = gallivm->builder;
603 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200604 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200605 LLVMValueRef resource = build_indexed_load(si_shader_ctx, desc, buf_index);
606
607 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
608 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
609 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
610
611 LLVMValueRef pos[4] = {
612 load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
613 load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
614 lp_build_const_float(gallivm, 0),
615 lp_build_const_float(gallivm, 0)
616 };
617 value = lp_build_gather_values(gallivm, pos, 4);
618 break;
619 }
620
Christian Könige4ed5872013-03-21 18:02:52 +0100621 default:
622 assert(!"unknown system value");
623 return;
624 }
625
626 radeon_bld->system_values[index] = value;
627}
628
Tom Stellarda75c6162012-01-06 17:38:37 -0500629static LLVMValueRef fetch_constant(
630 struct lp_build_tgsi_context * bld_base,
631 const struct tgsi_full_src_register *reg,
632 enum tgsi_opcode_type type,
633 unsigned swizzle)
634{
Christian König55fe5cc2013-03-04 16:30:06 +0100635 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500636 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100637 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200638 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500639
Christian König0f6cf2b2013-03-15 15:53:25 +0100640 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100641 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500642
Christian König8514f5a2013-02-04 17:46:42 +0100643 if (swizzle == LP_CHAN_ALL) {
644 unsigned chan;
645 LLVMValueRef values[4];
646 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
647 values[chan] = fetch_constant(bld_base, reg, type, chan);
648
649 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
650 }
651
Marek Olšák2fd42002013-10-25 11:45:47 +0200652 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100653 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100654
Marek Olšák2fd42002013-10-25 11:45:47 +0200655 if (!reg->Register.Indirect)
656 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
657
Christian König0f6cf2b2013-03-15 15:53:25 +0100658 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
659 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
660 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200661 addr = lp_build_add(&bld_base->uint_bld, addr,
662 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200663
Marek Olšák250aa932014-05-06 14:10:47 +0200664 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
665 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500666
Christian Königf5298b02013-02-28 14:50:07 +0100667 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500668}
669
Michel Dänzer26c71392012-08-24 12:03:11 +0200670/* Initialize arguments for the shader export intrinsic */
671static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900672 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200673 unsigned target,
674 LLVMValueRef *args)
675{
676 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
677 struct lp_build_context *uint =
678 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
679 struct lp_build_context *base = &bld_base->base;
680 unsigned compressed = 0;
681 unsigned chan;
682
Michel Dänzerf402acd2012-08-22 18:15:36 +0200683 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
684 int cbuf = target - V_008DFC_SQ_EXP_MRT;
685
686 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100687 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100688
689 if (compressed)
690 si_shader_ctx->shader->spi_shader_col_format |=
691 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
692 else
693 si_shader_ctx->shader->spi_shader_col_format |=
694 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200695
696 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200697 }
698 }
699
700 if (compressed) {
701 /* Pixel shader needs to pack output values before export */
702 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900703 args[0] = values[2 * chan];
704 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200705 args[chan + 5] =
706 build_intrinsic(base->gallivm->builder,
707 "llvm.SI.packf16",
708 LLVMInt32TypeInContext(base->gallivm->context),
709 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100710 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100711 args[chan + 7] = args[chan + 5] =
712 LLVMBuildBitCast(base->gallivm->builder,
713 args[chan + 5],
714 LLVMFloatTypeInContext(base->gallivm->context),
715 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200716 }
717
718 /* Set COMPR flag */
719 args[4] = uint->one;
720 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900721 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200722 /* +5 because the first output value will be
723 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900724 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200725
726 /* Clear COMPR flag */
727 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200728 }
729
730 /* XXX: This controls which components of the output
731 * registers actually get exported. (e.g bit 0 means export
732 * X component, bit 1 means export Y component, etc.) I'm
733 * hard coding this to 0xf for now. In the future, we might
734 * want to do something else. */
735 args[0] = lp_build_const_int32(base->gallivm, 0xf);
736
737 /* Specify whether the EXEC mask represents the valid mask */
738 args[1] = uint->zero;
739
740 /* Specify whether this is the last export */
741 args[2] = uint->zero;
742
743 /* Specify the target we are exporting */
744 args[3] = lp_build_const_int32(base->gallivm, target);
745
Michel Dänzer26c71392012-08-24 12:03:11 +0200746 /* XXX: We probably need to keep track of the output
747 * values, so we know what we are passing to the next
748 * stage. */
749}
750
Michel Dänzer404b29d2013-11-21 16:45:28 +0900751/* Load from output pointers and initialize arguments for the shader export intrinsic */
752static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
753 LLVMValueRef *out_ptr,
754 unsigned target,
755 LLVMValueRef *args)
756{
757 struct gallivm_state *gallivm = bld_base->base.gallivm;
758 LLVMValueRef values[4];
759 int i;
760
761 for (i = 0; i < 4; i++)
762 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
763
764 si_llvm_init_export_args(bld_base, values, target, args);
765}
766
Michel Dänzer7708a862012-11-02 15:57:30 +0100767static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900768 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100769{
770 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
771 struct gallivm_state *gallivm = bld_base->base.gallivm;
772
Christian Königa0dca442013-03-22 15:59:22 +0100773 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400774 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
775 SI_PARAM_ALPHA_REF);
776
Michel Dänzer7708a862012-11-02 15:57:30 +0100777 LLVMValueRef alpha_pass =
778 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100779 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900780 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400781 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100782 LLVMValueRef arg =
783 lp_build_select(&bld_base->base,
784 alpha_pass,
785 lp_build_const_float(gallivm, 1.0f),
786 lp_build_const_float(gallivm, -1.0f));
787
788 build_intrinsic(gallivm->builder,
789 "llvm.AMDGPU.kill",
790 LLVMVoidTypeInContext(gallivm->context),
791 &arg, 1, 0);
792 } else {
793 build_intrinsic(gallivm->builder,
794 "llvm.AMDGPU.kilp",
795 LLVMVoidTypeInContext(gallivm->context),
796 NULL, 0, 0);
797 }
Marek Olšákadc57972014-09-19 17:07:07 +0200798
799 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100800}
801
Michel Dänzere3befbc2013-05-15 18:09:50 +0200802static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900803 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200804{
805 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200806 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200807 struct lp_build_context *base = &bld_base->base;
808 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200809 unsigned reg_index;
810 unsigned chan;
811 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200812 LLVMValueRef base_elt;
813 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200814 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák2fd42002013-10-25 11:45:47 +0200815 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200816
Michel Dänzere3befbc2013-05-15 18:09:50 +0200817 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200818 LLVMValueRef *args = pos[2 + reg_index];
819
Marek Olšák8c37c162014-09-16 18:40:07 +0200820 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200821
Michel Dänzere3befbc2013-05-15 18:09:50 +0200822 args[5] =
823 args[6] =
824 args[7] =
825 args[8] = lp_build_const_float(base->gallivm, 0.0f);
826
827 /* Compute dot products of position and user clip plane vectors */
828 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
829 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200830 args[1] = lp_build_const_int32(base->gallivm,
831 ((reg_index * 4 + chan) * 4 +
832 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200833 base_elt = load_const(base->gallivm->builder, const_resource,
834 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200835 args[5 + chan] =
836 lp_build_add(base, args[5 + chan],
837 lp_build_mul(base, base_elt,
838 out_elts[const_chan]));
839 }
840 }
841
842 args[0] = lp_build_const_int32(base->gallivm, 0xf);
843 args[1] = uint->zero;
844 args[2] = uint->zero;
845 args[3] = lp_build_const_int32(base->gallivm,
846 V_008DFC_SQ_EXP_POS + 2 + reg_index);
847 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200848 }
849}
850
Marek Olšák8d03d922013-09-01 23:59:06 +0200851static void si_dump_streamout(struct pipe_stream_output_info *so)
852{
853 unsigned i;
854
855 if (so->num_outputs)
856 fprintf(stderr, "STREAMOUT\n");
857
858 for (i = 0; i < so->num_outputs; i++) {
859 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
860 so->output[i].start_component;
861 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
862 i, so->output[i].output_buffer,
863 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
864 so->output[i].register_index,
865 mask & 1 ? "x" : "",
866 mask & 2 ? "y" : "",
867 mask & 4 ? "z" : "",
868 mask & 8 ? "w" : "");
869 }
870}
871
872/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
873 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
874 * or v4i32 (num_channels=3,4). */
875static void build_tbuffer_store(struct si_shader_context *shader,
876 LLVMValueRef rsrc,
877 LLVMValueRef vdata,
878 unsigned num_channels,
879 LLVMValueRef vaddr,
880 LLVMValueRef soffset,
881 unsigned inst_offset,
882 unsigned dfmt,
883 unsigned nfmt,
884 unsigned offen,
885 unsigned idxen,
886 unsigned glc,
887 unsigned slc,
888 unsigned tfe)
889{
890 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
891 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
892 LLVMValueRef args[] = {
893 rsrc,
894 vdata,
895 LLVMConstInt(i32, num_channels, 0),
896 vaddr,
897 soffset,
898 LLVMConstInt(i32, inst_offset, 0),
899 LLVMConstInt(i32, dfmt, 0),
900 LLVMConstInt(i32, nfmt, 0),
901 LLVMConstInt(i32, offen, 0),
902 LLVMConstInt(i32, idxen, 0),
903 LLVMConstInt(i32, glc, 0),
904 LLVMConstInt(i32, slc, 0),
905 LLVMConstInt(i32, tfe, 0)
906 };
907
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900908 /* The instruction offset field has 12 bits */
909 assert(offen || inst_offset < (1 << 12));
910
Marek Olšák8d03d922013-09-01 23:59:06 +0200911 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
912 unsigned func = CLAMP(num_channels, 1, 3) - 1;
913 const char *types[] = {"i32", "v2i32", "v4i32"};
914 char name[256];
915 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
916
917 lp_build_intrinsic(gallivm->builder, name,
918 LLVMVoidTypeInContext(gallivm->context),
919 args, Elements(args));
920}
921
922static void build_streamout_store(struct si_shader_context *shader,
923 LLVMValueRef rsrc,
924 LLVMValueRef vdata,
925 unsigned num_channels,
926 LLVMValueRef vaddr,
927 LLVMValueRef soffset,
928 unsigned inst_offset)
929{
930 static unsigned dfmt[] = {
931 V_008F0C_BUF_DATA_FORMAT_32,
932 V_008F0C_BUF_DATA_FORMAT_32_32,
933 V_008F0C_BUF_DATA_FORMAT_32_32_32,
934 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
935 };
936 assert(num_channels >= 1 && num_channels <= 4);
937
938 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
939 inst_offset, dfmt[num_channels-1],
940 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
941}
942
943/* On SI, the vertex shader is responsible for writing streamout data
944 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900945static void si_llvm_emit_streamout(struct si_shader_context *shader,
946 struct si_shader_output_values *outputs,
947 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200948{
949 struct pipe_stream_output_info *so = &shader->shader->selector->so;
950 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
951 LLVMBuilderRef builder = gallivm->builder;
952 int i, j;
953 struct lp_build_if_state if_ctx;
954
955 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
956
957 LLVMValueRef so_param =
958 LLVMGetParam(shader->radeon_bld.main_fn,
959 shader->param_streamout_config);
960
961 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
962 LLVMValueRef so_vtx_count =
963 LLVMBuildAnd(builder,
964 LLVMBuildLShr(builder, so_param,
965 LLVMConstInt(i32, 16, 0), ""),
966 LLVMConstInt(i32, 127, 0), "");
967
968 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
969 NULL, 0, LLVMReadNoneAttribute);
970
971 /* can_emit = tid < so_vtx_count; */
972 LLVMValueRef can_emit =
973 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
974
975 /* Emit the streamout code conditionally. This actually avoids
976 * out-of-bounds buffer access. The hw tells us via the SGPR
977 * (so_vtx_count) which threads are allowed to emit streamout data. */
978 lp_build_if(&if_ctx, gallivm, can_emit);
979 {
980 /* The buffer offset is computed as follows:
981 * ByteOffset = streamout_offset[buffer_id]*4 +
982 * (streamout_write_index + thread_id)*stride[buffer_id] +
983 * attrib_offset
984 */
985
986 LLVMValueRef so_write_index =
987 LLVMGetParam(shader->radeon_bld.main_fn,
988 shader->param_streamout_write_index);
989
990 /* Compute (streamout_write_index + thread_id). */
991 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
992
993 /* Compute the write offset for each enabled buffer. */
994 LLVMValueRef so_write_offset[4] = {};
995 for (i = 0; i < 4; i++) {
996 if (!so->stride[i])
997 continue;
998
999 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1000 shader->param_streamout_offset[i]);
1001 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1002
1003 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1004 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1005 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1006 }
1007
Marek Olšák8d03d922013-09-01 23:59:06 +02001008 /* Write streamout data. */
1009 for (i = 0; i < so->num_outputs; i++) {
1010 unsigned buf_idx = so->output[i].output_buffer;
1011 unsigned reg = so->output[i].register_index;
1012 unsigned start = so->output[i].start_component;
1013 unsigned num_comps = so->output[i].num_components;
1014 LLVMValueRef out[4];
1015
1016 assert(num_comps && num_comps <= 4);
1017 if (!num_comps || num_comps > 4)
1018 continue;
1019
Marek Olšák558f7772014-10-04 22:37:23 +02001020 if (reg >= noutput)
1021 continue;
1022
Marek Olšák8d03d922013-09-01 23:59:06 +02001023 /* Load the output as int. */
1024 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001025 out[j] = LLVMBuildBitCast(builder,
1026 outputs[reg].values[start+j],
1027 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001028 }
1029
1030 /* Pack the output. */
1031 LLVMValueRef vdata = NULL;
1032
1033 switch (num_comps) {
1034 case 1: /* as i32 */
1035 vdata = out[0];
1036 break;
1037 case 2: /* as v2i32 */
1038 case 3: /* as v4i32 (aligned to 4) */
1039 case 4: /* as v4i32 */
1040 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1041 for (j = 0; j < num_comps; j++) {
1042 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1043 LLVMConstInt(i32, j, 0), "");
1044 }
1045 break;
1046 }
1047
1048 build_streamout_store(shader, shader->so_buffers[buf_idx],
1049 vdata, num_comps,
1050 so_write_offset[buf_idx],
1051 LLVMConstInt(i32, 0, 0),
1052 so->output[i].dst_offset*4);
1053 }
1054 }
1055 lp_build_endif(&if_ctx);
1056}
1057
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001058
Michel Dänzer404b29d2013-11-21 16:45:28 +09001059/* Generate export instructions for hardware VS shader stage */
1060static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1061 struct si_shader_output_values *outputs,
1062 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001063{
1064 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001065 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001066 struct lp_build_context * base = &bld_base->base;
1067 struct lp_build_context * uint =
1068 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001069 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001070 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001071 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001072 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001073 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001074 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001075 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001076 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001077
Michel Dänzer67e385b2014-01-08 17:48:21 +09001078 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1079 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001080 }
1081
Michel Dänzer404b29d2013-11-21 16:45:28 +09001082 for (i = 0; i < noutput; i++) {
1083 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001084 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001085
Michel Dänzer0afeea52013-05-02 14:53:17 +02001086handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001087 /* Select the correct target */
1088 switch(semantic_name) {
1089 case TGSI_SEMANTIC_PSIZE:
1090 shader->vs_out_misc_write = true;
1091 shader->vs_out_point_size = true;
1092 psize_value = outputs[i].values[0];
1093 continue;
1094 case TGSI_SEMANTIC_EDGEFLAG:
1095 shader->vs_out_misc_write = true;
1096 shader->vs_out_edgeflag = true;
1097 edgeflag_value = outputs[i].values[0];
1098 continue;
1099 case TGSI_SEMANTIC_LAYER:
1100 shader->vs_out_misc_write = true;
1101 shader->vs_out_layer = true;
1102 layer_value = outputs[i].values[0];
1103 continue;
1104 case TGSI_SEMANTIC_POSITION:
1105 target = V_008DFC_SQ_EXP_POS;
1106 break;
1107 case TGSI_SEMANTIC_COLOR:
1108 case TGSI_SEMANTIC_BCOLOR:
1109 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001110 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001111 param_count++;
1112 break;
1113 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001114 shader->clip_dist_write |=
Marek Olšáka9592cd2014-10-04 19:09:09 +02001115 0xf << (semantic_index * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001116 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1117 break;
1118 case TGSI_SEMANTIC_CLIPVERTEX:
1119 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1120 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001121 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001122 case TGSI_SEMANTIC_FOG:
1123 case TGSI_SEMANTIC_GENERIC:
1124 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001125 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001126 param_count++;
1127 break;
1128 default:
1129 target = 0;
1130 fprintf(stderr,
1131 "Warning: SI unhandled vs output type:%d\n",
1132 semantic_name);
1133 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001134
Michel Dänzer404b29d2013-11-21 16:45:28 +09001135 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001136
Michel Dänzer404b29d2013-11-21 16:45:28 +09001137 if (target >= V_008DFC_SQ_EXP_POS &&
1138 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1139 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1140 args, sizeof(args));
1141 } else {
1142 lp_build_intrinsic(base->gallivm->builder,
1143 "llvm.SI.export",
1144 LLVMVoidTypeInContext(base->gallivm->context),
1145 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001146 }
1147
1148 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1149 semantic_name = TGSI_SEMANTIC_GENERIC;
1150 goto handle_semantic;
1151 }
1152 }
1153
1154 /* We need to add the position output manually if it's missing. */
1155 if (!pos_args[0][0]) {
1156 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1157 pos_args[0][1] = uint->zero; /* EXEC mask */
1158 pos_args[0][2] = uint->zero; /* last export? */
1159 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1160 pos_args[0][4] = uint->zero; /* COMPR flag */
1161 pos_args[0][5] = base->zero; /* X */
1162 pos_args[0][6] = base->zero; /* Y */
1163 pos_args[0][7] = base->zero; /* Z */
1164 pos_args[0][8] = base->one; /* W */
1165 }
1166
1167 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1168 if (shader->vs_out_misc_write) {
1169 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1170 shader->vs_out_point_size |
1171 (shader->vs_out_edgeflag << 1) |
1172 (shader->vs_out_layer << 2));
1173 pos_args[1][1] = uint->zero; /* EXEC mask */
1174 pos_args[1][2] = uint->zero; /* last export? */
1175 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1176 pos_args[1][4] = uint->zero; /* COMPR flag */
1177 pos_args[1][5] = base->zero; /* X */
1178 pos_args[1][6] = base->zero; /* Y */
1179 pos_args[1][7] = base->zero; /* Z */
1180 pos_args[1][8] = base->zero; /* W */
1181
Michel Dänzer404b29d2013-11-21 16:45:28 +09001182 if (shader->vs_out_point_size)
1183 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001184
1185 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001186 /* The output is a float, but the hw expects an integer
1187 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001188 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1189 edgeflag_value,
1190 bld_base->uint_bld.elem_type, "");
1191 edgeflag_value = lp_build_min(&bld_base->int_bld,
1192 edgeflag_value,
1193 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001194
1195 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001196 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1197 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001198 base->elem_type, "");
1199 }
1200
Michel Dänzer404b29d2013-11-21 16:45:28 +09001201 if (shader->vs_out_layer)
1202 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001203 }
1204
1205 for (i = 0; i < 4; i++)
1206 if (pos_args[i][0])
1207 shader->nr_pos_exports++;
1208
1209 pos_idx = 0;
1210 for (i = 0; i < 4; i++) {
1211 if (!pos_args[i][0])
1212 continue;
1213
1214 /* Specify the target we are exporting */
1215 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1216
1217 if (pos_idx == shader->nr_pos_exports)
1218 /* Specify that this is the last export */
1219 pos_args[i][2] = uint->one;
1220
1221 lp_build_intrinsic(base->gallivm->builder,
1222 "llvm.SI.export",
1223 LLVMVoidTypeInContext(base->gallivm->context),
1224 pos_args[i], 9);
1225 }
1226}
1227
Michel Dänzer404b29d2013-11-21 16:45:28 +09001228static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1229{
1230 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1231 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001232 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001233 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001234 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001235 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1236 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001237 LLVMValueRef t_list_ptr;
1238 LLVMValueRef t_list;
1239 unsigned chan;
1240 int i;
1241
Michel Dänzer404b29d2013-11-21 16:45:28 +09001242 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001243 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1244 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001245 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001246 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001247
Marek Olšák216cf862014-10-04 17:04:05 +02001248 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001249 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001250 si_shader_ctx->radeon_bld.soa.outputs[i];
1251 int param_index = get_param_index(info->output_semantic_name[i],
1252 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001253 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001254
Marek Olšáke29353f2014-09-17 22:17:02 +02001255 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001256 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001257
1258 for (chan = 0; chan < 4; chan++) {
1259 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001260 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1261
1262 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001263 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001264 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001265 V_008F0C_BUF_DATA_FORMAT_32,
1266 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001267 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001268 }
1269 }
1270}
1271
1272static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1273{
1274 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1275 struct gallivm_state *gallivm = bld_base->base.gallivm;
1276 LLVMValueRef args[2];
1277
1278 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1279 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1280 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1281 LLVMVoidTypeInContext(gallivm->context), args, 2,
1282 LLVMNoUnwindAttribute);
1283}
1284
1285static void si_llvm_emit_vs_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;
Marek Olšákec0d1682014-10-04 22:33:36 +02001289 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001290 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001291 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001292
Marek Olšákec0d1682014-10-04 22:33:36 +02001293 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001294
Marek Olšákec0d1682014-10-04 22:33:36 +02001295 for (i = 0; i < info->num_outputs; i++) {
1296 outputs[i].name = info->output_semantic_name[i];
1297 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001298
Marek Olšákec0d1682014-10-04 22:33:36 +02001299 for (j = 0; j < 4; j++)
1300 outputs[i].values[j] =
1301 LLVMBuildLoad(gallivm->builder,
1302 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1303 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001304 }
1305
Marek Olšákec0d1682014-10-04 22:33:36 +02001306 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001307 FREE(outputs);
1308}
1309
Michel Dänzer51f89a02013-12-09 15:33:53 +09001310static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1311{
1312 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001313 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001314 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001315 struct lp_build_context * uint = &bld_base->uint_bld;
1316 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001317 LLVMValueRef args[9];
1318 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001319 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001320 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001321
Marek Olšák75e97e22014-10-04 23:13:50 +02001322 for (i = 0; i < info->num_outputs; i++) {
1323 unsigned semantic_name = info->output_semantic_name[i];
1324 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001325 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001326
Marek Olšák75e97e22014-10-04 23:13:50 +02001327 /* Select the correct target */
1328 switch (semantic_name) {
1329 case TGSI_SEMANTIC_POSITION:
1330 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001331 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001332 case TGSI_SEMANTIC_STENCIL:
1333 stencil_index = i;
1334 continue;
1335 case TGSI_SEMANTIC_SAMPLEMASK:
1336 samplemask_index = i;
1337 continue;
1338 case TGSI_SEMANTIC_COLOR:
1339 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1340 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1341 LLVMBuildStore(bld_base->base.gallivm->builder,
1342 bld_base->base.one,
1343 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001344
Marek Olšák75e97e22014-10-04 23:13:50 +02001345 if (semantic_index == 0 &&
1346 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1347 si_alpha_test(bld_base,
1348 si_shader_ctx->radeon_bld.soa.outputs[i]);
1349 break;
1350 default:
1351 target = 0;
1352 fprintf(stderr,
1353 "Warning: SI unhandled fs output type:%d\n",
1354 semantic_name);
1355 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001356
Marek Olšák75e97e22014-10-04 23:13:50 +02001357 si_llvm_init_export_args_load(bld_base,
1358 si_shader_ctx->radeon_bld.soa.outputs[i],
1359 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001360
Marek Olšák75e97e22014-10-04 23:13:50 +02001361 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1362 /* If there is an export instruction waiting to be emitted, do so now. */
1363 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001364 lp_build_intrinsic(base->gallivm->builder,
1365 "llvm.SI.export",
1366 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001367 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001368 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001369
1370 /* This instruction will be emitted at the end of the shader. */
1371 memcpy(last_args, args, sizeof(args));
1372
1373 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1374 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1375 semantic_index == 0 &&
1376 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1377 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1378 si_llvm_init_export_args_load(bld_base,
1379 si_shader_ctx->radeon_bld.soa.outputs[i],
1380 V_008DFC_SQ_EXP_MRT + c, args);
1381 lp_build_intrinsic(base->gallivm->builder,
1382 "llvm.SI.export",
1383 LLVMVoidTypeInContext(base->gallivm->context),
1384 args, 9);
1385 }
1386 }
1387 } else {
1388 lp_build_intrinsic(base->gallivm->builder,
1389 "llvm.SI.export",
1390 LLVMVoidTypeInContext(base->gallivm->context),
1391 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001392 }
1393 }
1394
Marek Olšákd0e8b652014-05-06 20:04:31 +02001395 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001396 LLVMValueRef out_ptr;
1397 unsigned mask = 0;
1398
1399 /* Specify the target we are exporting */
1400 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1401
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001402 args[5] = base->zero; /* R, depth */
1403 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1404 args[7] = base->zero; /* B, sample mask */
1405 args[8] = base->zero; /* A, alpha to mask */
1406
Michel Dänzer1a616c12012-11-13 17:35:09 +01001407 if (depth_index >= 0) {
1408 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1409 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1410 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001411 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001412 }
1413
1414 if (stencil_index >= 0) {
1415 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001416 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001417 /* Only setting the stencil component bit (0x2) here
1418 * breaks some stencil piglit tests
1419 */
1420 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001421 si_shader_ctx->shader->db_shader_control |=
1422 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001423 }
1424
Marek Olšákd0e8b652014-05-06 20:04:31 +02001425 if (samplemask_index >= 0) {
1426 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1427 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1428 mask |= 0xf; /* Set all components. */
1429 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1430 }
1431
1432 if (samplemask_index >= 0)
1433 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1434 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001435 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1436 else
1437 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1438
Michel Dänzer1a616c12012-11-13 17:35:09 +01001439 /* Specify which components to enable */
1440 args[0] = lp_build_const_int32(base->gallivm, mask);
1441
1442 args[1] =
1443 args[2] =
1444 args[4] = uint->zero;
1445
1446 if (last_args[0])
1447 lp_build_intrinsic(base->gallivm->builder,
1448 "llvm.SI.export",
1449 LLVMVoidTypeInContext(base->gallivm->context),
1450 args, 9);
1451 else
1452 memcpy(last_args, args, sizeof(args));
1453 }
1454
Michel Dänzer51f89a02013-12-09 15:33:53 +09001455 if (!last_args[0]) {
1456 /* Specify which components to enable */
1457 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001458
Michel Dänzer51f89a02013-12-09 15:33:53 +09001459 /* Specify the target we are exporting */
1460 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001461
Michel Dänzer51f89a02013-12-09 15:33:53 +09001462 /* Set COMPR flag to zero to export data as 32-bit */
1463 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001464
Michel Dänzer51f89a02013-12-09 15:33:53 +09001465 /* dummy bits */
1466 last_args[5]= uint->zero;
1467 last_args[6]= uint->zero;
1468 last_args[7]= uint->zero;
1469 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001470 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001471
1472 /* Specify whether the EXEC mask represents the valid mask */
1473 last_args[1] = uint->one;
1474
1475 /* Specify that this is the last export */
1476 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1477
1478 lp_build_intrinsic(base->gallivm->builder,
1479 "llvm.SI.export",
1480 LLVMVoidTypeInContext(base->gallivm->context),
1481 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001482}
1483
Marek Olšák4855acd2013-08-06 15:08:54 +02001484static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1485 struct lp_build_tgsi_context * bld_base,
1486 struct lp_build_emit_data * emit_data);
1487
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001488static bool tgsi_is_shadow_sampler(unsigned target)
1489{
1490 return target == TGSI_TEXTURE_SHADOW1D ||
1491 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1492 target == TGSI_TEXTURE_SHADOW2D ||
1493 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1494 target == TGSI_TEXTURE_SHADOWCUBE ||
1495 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1496 target == TGSI_TEXTURE_SHADOWRECT;
1497}
1498
Marek Olšák877bb522014-06-25 03:12:46 +02001499static const struct lp_build_tgsi_action tex_action;
1500
Tom Stellarda75c6162012-01-06 17:38:37 -05001501static void tex_fetch_args(
1502 struct lp_build_tgsi_context * bld_base,
1503 struct lp_build_emit_data * emit_data)
1504{
Christian König55fe5cc2013-03-04 16:30:06 +01001505 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001506 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001507 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001508 unsigned opcode = inst->Instruction.Opcode;
1509 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001510 LLVMValueRef coords[4];
1511 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001512 int ref_pos;
1513 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001514 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001515 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001516 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1517 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001518 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001519
1520 if (target == TGSI_TEXTURE_BUFFER) {
1521 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1522 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1523 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1524 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1525
Marek Olšák4f3f0432014-07-07 22:49:15 +02001526 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001527 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1528 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1529 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1530 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1531
1532 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1533 emit_data->args[0] = res;
1534 emit_data->args[1] = bld_base->uint_bld.zero;
1535 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1536 emit_data->arg_count = 3;
1537 return;
1538 }
Tom Stellard467f5162012-05-16 15:15:35 -04001539
Michel Dänzer120efee2013-01-25 12:10:11 +01001540 /* Fetch and project texture coordinates */
1541 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001542 for (chan = 0; chan < 3; chan++ ) {
1543 coords[chan] = lp_build_emit_fetch(bld_base,
1544 emit_data->inst, 0,
1545 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001546 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001547 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1548 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001549 coords[chan],
1550 coords[3]);
1551 }
1552
Michel Dänzer120efee2013-01-25 12:10:11 +01001553 if (opcode == TGSI_OPCODE_TXP)
1554 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001555
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001556 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001557 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001558 /* The offsets are six-bit signed integers packed like this:
1559 * X=[5:0], Y=[13:8], and Z=[21:16].
1560 */
1561 LLVMValueRef offset[3], pack;
1562
1563 assert(inst->Texture.NumOffsets == 1);
1564
1565 for (chan = 0; chan < 3; chan++) {
1566 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1567 emit_data->inst, 0, chan);
1568 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1569 lp_build_const_int32(gallivm, 0x3f), "");
1570 if (chan)
1571 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1572 lp_build_const_int32(gallivm, chan*8), "");
1573 }
1574
1575 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1576 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1577 address[count++] = pack;
1578 }
1579
Michel Dänzer120efee2013-01-25 12:10:11 +01001580 /* Pack LOD bias value */
1581 if (opcode == TGSI_OPCODE_TXB)
1582 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001583 if (opcode == TGSI_OPCODE_TXB2)
1584 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001585
Michel Dänzer120efee2013-01-25 12:10:11 +01001586 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001587 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001588 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1589 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1590 } else {
1591 assert(ref_pos >= 0);
1592 address[count++] = coords[ref_pos];
1593 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001594 }
1595
Marek Olšákb279f0142014-07-04 03:19:58 +02001596 if (target == TGSI_TEXTURE_CUBE ||
1597 target == TGSI_TEXTURE_CUBE_ARRAY ||
1598 target == TGSI_TEXTURE_SHADOWCUBE ||
1599 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1600 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1601
Michel Dänzera6b83c02013-02-21 16:10:55 +01001602 /* Pack user derivatives */
1603 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001604 int num_deriv_channels, param;
1605
1606 switch (target) {
1607 case TGSI_TEXTURE_3D:
1608 num_deriv_channels = 3;
1609 break;
1610 case TGSI_TEXTURE_2D:
1611 case TGSI_TEXTURE_SHADOW2D:
1612 case TGSI_TEXTURE_RECT:
1613 case TGSI_TEXTURE_SHADOWRECT:
1614 case TGSI_TEXTURE_2D_ARRAY:
1615 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1616 case TGSI_TEXTURE_CUBE:
1617 case TGSI_TEXTURE_SHADOWCUBE:
1618 case TGSI_TEXTURE_CUBE_ARRAY:
1619 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1620 num_deriv_channels = 2;
1621 break;
1622 case TGSI_TEXTURE_1D:
1623 case TGSI_TEXTURE_SHADOW1D:
1624 case TGSI_TEXTURE_1D_ARRAY:
1625 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1626 num_deriv_channels = 1;
1627 break;
1628 default:
1629 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001630 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001631
1632 for (param = 1; param <= 2; param++)
1633 for (chan = 0; chan < num_deriv_channels; chan++)
1634 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001635 }
1636
Michel Dänzer120efee2013-01-25 12:10:11 +01001637 /* Pack texture coordinates */
1638 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001639 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001640 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001641 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001642 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001643
Marek Olšákd2bd6342013-09-18 15:40:21 +02001644 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001645 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001646 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001647 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001648 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001649
1650 if (count > 16) {
1651 assert(!"Cannot handle more than 16 texture address parameters");
1652 count = 16;
1653 }
1654
1655 for (chan = 0; chan < count; chan++ ) {
1656 address[chan] = LLVMBuildBitCast(gallivm->builder,
1657 address[chan],
1658 LLVMInt32TypeInContext(gallivm->context),
1659 "");
1660 }
1661
Marek Olšák4855acd2013-08-06 15:08:54 +02001662 /* Adjust the sample index according to FMASK.
1663 *
1664 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1665 * which is the identity mapping. Each nibble says which physical sample
1666 * should be fetched to get that sample.
1667 *
1668 * For example, 0x11111100 means there are only 2 samples stored and
1669 * the second sample covers 3/4 of the pixel. When reading samples 0
1670 * and 1, return physical sample 0 (determined by the first two 0s
1671 * in FMASK), otherwise return physical sample 1.
1672 *
1673 * The sample index should be adjusted as follows:
1674 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1675 */
1676 if (target == TGSI_TEXTURE_2D_MSAA ||
1677 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1678 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1679 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001680 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001681 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001682 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001683
Marek Olšákd2bd6342013-09-18 15:40:21 +02001684 memcpy(txf_address, address, sizeof(txf_address));
1685
1686 if (target == TGSI_TEXTURE_2D_MSAA) {
1687 txf_address[2] = bld_base->uint_bld.zero;
1688 }
1689 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001690
1691 /* Pad to a power-of-two size. */
1692 while (txf_count < util_next_power_of_two(txf_count))
1693 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1694
1695 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001696 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1697 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1698 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001699 txf_emit_data.chan = 0;
1700 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001701 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001702 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001703 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001704 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001705 txf_emit_data.arg_count = 3;
1706
Marek Olšák877bb522014-06-25 03:12:46 +02001707 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001708
1709 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001710 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1711 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1712
1713 /* Apply the formula. */
1714 LLVMValueRef fmask =
1715 LLVMBuildExtractElement(gallivm->builder,
1716 txf_emit_data.output[0],
1717 uint_bld->zero, "");
1718
Marek Olšákd2bd6342013-09-18 15:40:21 +02001719 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001720
1721 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001722 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001723
1724 LLVMValueRef shifted_fmask =
1725 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1726
1727 LLVMValueRef final_sample =
1728 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1729
1730 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1731 * resource descriptor is 0 (invalid),
1732 */
1733 LLVMValueRef fmask_desc =
1734 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001735 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001736 LLVMVectorType(uint_bld->elem_type, 8), "");
1737
1738 LLVMValueRef fmask_word1 =
1739 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1740 uint_bld->one, "");
1741
1742 LLVMValueRef word1_is_nonzero =
1743 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1744 fmask_word1, uint_bld->zero, "");
1745
Marek Olšákd2bd6342013-09-18 15:40:21 +02001746 /* Replace the MSAA sample index. */
1747 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001748 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001749 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001750 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001751
Michel Dänzer36231112013-05-02 09:44:45 +02001752 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001753 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001754
1755 if (opcode == TGSI_OPCODE_TXF) {
1756 /* add tex offsets */
1757 if (inst->Texture.NumOffsets) {
1758 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1759 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1760 const struct tgsi_texture_offset * off = inst->TexOffsets;
1761
1762 assert(inst->Texture.NumOffsets == 1);
1763
Marek Olšákdefedc02013-09-18 15:36:38 +02001764 switch (target) {
1765 case TGSI_TEXTURE_3D:
1766 address[2] = lp_build_add(uint_bld, address[2],
1767 bld->immediates[off->Index][off->SwizzleZ]);
1768 /* fall through */
1769 case TGSI_TEXTURE_2D:
1770 case TGSI_TEXTURE_SHADOW2D:
1771 case TGSI_TEXTURE_RECT:
1772 case TGSI_TEXTURE_SHADOWRECT:
1773 case TGSI_TEXTURE_2D_ARRAY:
1774 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001775 address[1] =
1776 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001777 bld->immediates[off->Index][off->SwizzleY]);
1778 /* fall through */
1779 case TGSI_TEXTURE_1D:
1780 case TGSI_TEXTURE_SHADOW1D:
1781 case TGSI_TEXTURE_1D_ARRAY:
1782 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1783 address[0] =
1784 lp_build_add(uint_bld, address[0],
1785 bld->immediates[off->Index][off->SwizzleX]);
1786 break;
1787 /* texture offsets do not apply to other texture targets */
1788 }
Michel Dänzer36231112013-05-02 09:44:45 +02001789 }
1790
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001791 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1792 emit_data->arg_count = 3;
1793
Michel Dänzer36231112013-05-02 09:44:45 +02001794 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001795 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001796 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001797 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001798 opcode == TGSI_OPCODE_LODQ ||
1799 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001800 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1801 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1802 target == TGSI_TEXTURE_2D_ARRAY ||
1803 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1804 target == TGSI_TEXTURE_CUBE_ARRAY ||
1805 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1806 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001807 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001808
Marek Olšák57f3da92014-06-16 16:53:42 +02001809 if (opcode == TGSI_OPCODE_TG4) {
1810 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001811
Marek Olšák57f3da92014-06-16 16:53:42 +02001812 /* DMASK was repurposed for GATHER4. 4 components are always
1813 * returned and DMASK works like a swizzle - it selects
1814 * the component to fetch. The only valid DMASK values are
1815 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1816 * (red,red,red,red) etc.) The ISA document doesn't mention
1817 * this.
1818 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001819
Marek Olšák57f3da92014-06-16 16:53:42 +02001820 /* Get the component index from src1.x for Gather4. */
1821 if (!tgsi_is_shadow_sampler(target)) {
1822 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1823 LLVMValueRef comp_imm;
1824 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001825
Marek Olšák57f3da92014-06-16 16:53:42 +02001826 assert(src1.File == TGSI_FILE_IMMEDIATE);
1827
1828 comp_imm = imms[src1.Index][src1.SwizzleX];
1829 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1830 gather_comp = CLAMP(gather_comp, 0, 3);
1831 }
1832
1833 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001834 }
1835
Marek Olšák4855acd2013-08-06 15:08:54 +02001836 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001837 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001838 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1839 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1840 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1841 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1842 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1843 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1844 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1845
1846 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001847
1848 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001849 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001850 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001851 } else {
1852 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1853 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001854 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001855
1856 emit_data->dst_type = LLVMVectorType(
1857 LLVMFloatTypeInContext(gallivm->context),
1858 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001859 }
1860
Marek Olšák2484daa2014-04-22 21:23:29 +02001861 /* The fetch opcode has been converted to a 2D array fetch.
1862 * This simplifies the LLVM backend. */
1863 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1864 target = TGSI_TEXTURE_2D_ARRAY;
1865 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1866 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1867
Michel Dänzer120efee2013-01-25 12:10:11 +01001868 /* Pad to power of two vector */
1869 while (count < util_next_power_of_two(count))
1870 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1871
Christian Königccf3e8f2013-03-26 15:09:27 +01001872 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001873}
1874
Michel Dänzer07eddc42013-02-06 15:43:10 +01001875static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1876 struct lp_build_tgsi_context * bld_base,
1877 struct lp_build_emit_data * emit_data)
1878{
1879 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001880 unsigned opcode = emit_data->inst->Instruction.Opcode;
1881 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001882 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001883 bool has_offset = HAVE_LLVM >= 0x0305 ?
1884 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001885
Marek Olšák877bb522014-06-25 03:12:46 +02001886 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001887 emit_data->output[emit_data->chan] = build_intrinsic(
1888 base->gallivm->builder,
1889 "llvm.SI.vs.load.input", emit_data->dst_type,
1890 emit_data->args, emit_data->arg_count,
1891 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1892 return;
1893 }
1894
Marek Olšák877bb522014-06-25 03:12:46 +02001895 if (opcode == TGSI_OPCODE_TG4 ||
1896 opcode == TGSI_OPCODE_LODQ ||
1897 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1898 bool is_shadow = tgsi_is_shadow_sampler(target);
1899 const char *name = "llvm.SI.image.sample";
1900 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001901
Marek Olšák877bb522014-06-25 03:12:46 +02001902 switch (opcode) {
1903 case TGSI_OPCODE_TEX:
1904 case TGSI_OPCODE_TEX2:
1905 case TGSI_OPCODE_TXP:
1906 break;
1907 case TGSI_OPCODE_TXB:
1908 case TGSI_OPCODE_TXB2:
1909 infix = ".b";
1910 break;
1911 case TGSI_OPCODE_TXL:
1912 case TGSI_OPCODE_TXL2:
1913 infix = ".l";
1914 break;
1915 case TGSI_OPCODE_TXD:
1916 infix = ".d";
1917 break;
1918 case TGSI_OPCODE_TG4:
1919 name = "llvm.SI.gather4";
1920 break;
1921 case TGSI_OPCODE_LODQ:
1922 name = "llvm.SI.getlod";
1923 is_shadow = false;
1924 has_offset = false;
1925 break;
1926 default:
1927 assert(0);
1928 return;
1929 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001930
Marek Olšák877bb522014-06-25 03:12:46 +02001931 /* Add the type and suffixes .c, .o if needed. */
1932 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1933 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1934 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001935
Marek Olšák877bb522014-06-25 03:12:46 +02001936 emit_data->output[emit_data->chan] = build_intrinsic(
1937 base->gallivm->builder, intr_name, emit_data->dst_type,
1938 emit_data->args, emit_data->arg_count,
1939 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1940 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001941 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001942 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001943
Marek Olšák877bb522014-06-25 03:12:46 +02001944 switch (opcode) {
1945 case TGSI_OPCODE_TEX:
1946 case TGSI_OPCODE_TEX2:
1947 case TGSI_OPCODE_TXP:
1948 name = "llvm.SI.sample";
1949 break;
1950 case TGSI_OPCODE_TXB:
1951 case TGSI_OPCODE_TXB2:
1952 name = "llvm.SI.sampleb";
1953 break;
1954 case TGSI_OPCODE_TXD:
1955 name = "llvm.SI.sampled";
1956 break;
1957 case TGSI_OPCODE_TXF:
1958 name = "llvm.SI.imageload";
1959 break;
1960 case TGSI_OPCODE_TXL:
1961 case TGSI_OPCODE_TXL2:
1962 name = "llvm.SI.samplel";
1963 break;
1964 default:
1965 assert(0);
1966 return;
1967 }
1968
Marek Olšákd859bdb2014-07-14 19:40:14 +02001969 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1970 v16i8 = LLVMVectorType(i8, 16);
1971 v32i8 = LLVMVectorType(i8, 32);
1972
1973 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1974 emit_data->args[1], v32i8, "");
1975 if (opcode != TGSI_OPCODE_TXF) {
1976 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1977 emit_data->args[2], v16i8, "");
1978 }
1979
Marek Olšák877bb522014-06-25 03:12:46 +02001980 sprintf(intr_name, "%s.v%ui32", name,
1981 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1982
1983 emit_data->output[emit_data->chan] = build_intrinsic(
1984 base->gallivm->builder, intr_name, emit_data->dst_type,
1985 emit_data->args, emit_data->arg_count,
1986 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1987 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001988}
1989
Michel Dänzer0495adb2013-05-06 12:45:14 +02001990static void txq_fetch_args(
1991 struct lp_build_tgsi_context * bld_base,
1992 struct lp_build_emit_data * emit_data)
1993{
1994 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1995 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001996 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02001997 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001998
Marek Olšák2484daa2014-04-22 21:23:29 +02001999 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002000 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2001 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2002
2003 /* Read the size from the buffer descriptor directly. */
2004 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2005 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2006 size = LLVMBuildExtractElement(gallivm->builder, size,
2007 lp_build_const_int32(gallivm, 2), "");
2008 emit_data->args[0] = size;
2009 return;
2010 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002011
2012 /* Mip level */
2013 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2014
2015 /* Resource */
2016 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2017
Marek Olšák2484daa2014-04-22 21:23:29 +02002018 /* Texture target */
2019 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2020 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2021 target = TGSI_TEXTURE_2D_ARRAY;
2022
Michel Dänzer0495adb2013-05-06 12:45:14 +02002023 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002024 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002025
2026 emit_data->arg_count = 3;
2027
2028 emit_data->dst_type = LLVMVectorType(
2029 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2030 4);
2031}
2032
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002033static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2034 struct lp_build_tgsi_context * bld_base,
2035 struct lp_build_emit_data * emit_data)
2036{
Marek Olšák2484daa2014-04-22 21:23:29 +02002037 unsigned target = emit_data->inst->Texture.Texture;
2038
2039 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002040 /* Just return the buffer size. */
2041 emit_data->output[emit_data->chan] = emit_data->args[0];
2042 return;
2043 }
2044
2045 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002046
2047 /* Divide the number of layers by 6 to get the number of cubes. */
2048 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2049 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2050 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2051 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2052 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2053
2054 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2055 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2056 z = LLVMBuildSDiv(builder, z, six, "");
2057
2058 emit_data->output[emit_data->chan] =
2059 LLVMBuildInsertElement(builder, v4, z, two, "");
2060 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002061}
2062
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002063static void si_llvm_emit_ddxy(
2064 const struct lp_build_tgsi_action * action,
2065 struct lp_build_tgsi_context * bld_base,
2066 struct lp_build_emit_data * emit_data)
2067{
2068 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2069 struct gallivm_state *gallivm = bld_base->base.gallivm;
2070 struct lp_build_context * base = &bld_base->base;
2071 const struct tgsi_full_instruction *inst = emit_data->inst;
2072 unsigned opcode = inst->Instruction.Opcode;
2073 LLVMValueRef indices[2];
2074 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2075 LLVMValueRef tl, trbl, result[4];
2076 LLVMTypeRef i32;
2077 unsigned swizzle[4];
2078 unsigned c;
2079
2080 i32 = LLVMInt32TypeInContext(gallivm->context);
2081
2082 indices[0] = bld_base->uint_bld.zero;
2083 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2084 NULL, 0, LLVMReadNoneAttribute);
2085 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2086 indices, 2, "");
2087
2088 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2089 lp_build_const_int32(gallivm, 0xfffffffc), "");
2090 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2091 indices, 2, "");
2092
2093 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2094 lp_build_const_int32(gallivm,
2095 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2096 "");
2097 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2098 indices, 2, "");
2099
2100 for (c = 0; c < 4; ++c) {
2101 unsigned i;
2102
2103 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2104 for (i = 0; i < c; ++i) {
2105 if (swizzle[i] == swizzle[c]) {
2106 result[c] = result[i];
2107 break;
2108 }
2109 }
2110 if (i != c)
2111 continue;
2112
2113 LLVMBuildStore(gallivm->builder,
2114 LLVMBuildBitCast(gallivm->builder,
2115 lp_build_emit_fetch(bld_base, inst, 0, c),
2116 i32, ""),
2117 store_ptr);
2118
2119 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2120 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2121
2122 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2123 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2124
2125 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2126 }
2127
2128 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2129}
2130
Michel Dänzer404b29d2013-11-21 16:45:28 +09002131/* Emit one vertex from the geometry shader */
2132static void si_llvm_emit_vertex(
2133 const struct lp_build_tgsi_action *action,
2134 struct lp_build_tgsi_context *bld_base,
2135 struct lp_build_emit_data *emit_data)
2136{
2137 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002138 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002139 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002140 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002141 struct gallivm_state *gallivm = bld_base->base.gallivm;
2142 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002143 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2144 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002145 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002146 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002147 LLVMValueRef t_list_ptr;
2148 LLVMValueRef t_list;
2149 LLVMValueRef args[2];
2150 unsigned chan;
2151 int i;
2152
2153 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002154 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2155 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002156 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002157 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002158
Michel Dänzer404b29d2013-11-21 16:45:28 +09002159 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002160 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002161
2162 /* If this thread has already emitted the declared maximum number of
2163 * vertices, kill it: excessive vertex emissions are not supposed to
2164 * have any effect, and GS threads have no externally observable
2165 * effects other than emitting vertices.
2166 */
2167 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2168 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002169 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002170 kill = lp_build_select(&bld_base->base, can_emit,
2171 lp_build_const_float(gallivm, 1.0f),
2172 lp_build_const_float(gallivm, -1.0f));
2173 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2174 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2175
Marek Olšák02134cf2014-10-04 22:07:50 +02002176 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002177 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002178 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002179
2180 for (chan = 0; chan < 4; chan++) {
2181 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002182 LLVMValueRef voffset =
2183 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002184 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002185
2186 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2187 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002188
2189 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2190
2191 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2192 voffset, soffset, 0,
2193 V_008F0C_BUF_DATA_FORMAT_32,
2194 V_008F0C_BUF_NUM_FORMAT_UINT,
2195 1, 0, 1, 1, 0);
2196 }
2197 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002198 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2199 lp_build_const_int32(gallivm, 1));
2200 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002201
2202 /* Signal vertex emission */
2203 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2204 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2205 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2206 LLVMVoidTypeInContext(gallivm->context), args, 2,
2207 LLVMNoUnwindAttribute);
2208}
2209
2210/* Cut one primitive from the geometry shader */
2211static void si_llvm_emit_primitive(
2212 const struct lp_build_tgsi_action *action,
2213 struct lp_build_tgsi_context *bld_base,
2214 struct lp_build_emit_data *emit_data)
2215{
2216 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2217 struct gallivm_state *gallivm = bld_base->base.gallivm;
2218 LLVMValueRef args[2];
2219
2220 /* Signal primitive cut */
2221 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2222 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2223 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2224 LLVMVoidTypeInContext(gallivm->context), args, 2,
2225 LLVMNoUnwindAttribute);
2226}
2227
Tom Stellarda75c6162012-01-06 17:38:37 -05002228static const struct lp_build_tgsi_action tex_action = {
2229 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002230 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002231};
2232
Michel Dänzer0495adb2013-05-06 12:45:14 +02002233static const struct lp_build_tgsi_action txq_action = {
2234 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002235 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002236 .intr_name = "llvm.SI.resinfo"
2237};
2238
Christian König206f0592013-03-20 14:37:21 +01002239static void create_meta_data(struct si_shader_context *si_shader_ctx)
2240{
2241 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2242 LLVMValueRef args[3];
2243
2244 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2245 args[1] = 0;
2246 args[2] = lp_build_const_int32(gallivm, 1);
2247
2248 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2249}
2250
Marek Olšák4f3f0432014-07-07 22:49:15 +02002251static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2252{
2253 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2254 CONST_ADDR_SPACE);
2255}
2256
Christian König55fe5cc2013-03-04 16:30:06 +01002257static void create_function(struct si_shader_context *si_shader_ctx)
2258{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002259 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2260 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002261 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002262 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002263 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002264
Christian König55fe5cc2013-03-04 16:30:06 +01002265 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002266 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002267 f32 = LLVMFloatTypeInContext(gallivm->context);
2268 v2i32 = LLVMVectorType(i32, 2);
2269 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002270 v4i32 = LLVMVectorType(i32, 4);
2271 v8i32 = LLVMVectorType(i32, 8);
2272 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002273
Marek Olšákee2a8182014-07-07 23:27:19 +02002274 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002275 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002276 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2277 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002278 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002279
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002280 switch (si_shader_ctx->type) {
2281 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002282 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002283 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002284 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002285 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002286 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002287
Michel Dänzer404b29d2013-11-21 16:45:28 +09002288 if (shader->key.vs.as_es) {
2289 params[SI_PARAM_ES2GS_OFFSET] = i32;
2290 num_params++;
2291 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002292 if (shader->is_gs_copy_shader) {
2293 last_array_pointer = SI_PARAM_CONST;
2294 num_params = SI_PARAM_CONST+1;
2295 }
2296
Michel Dänzer404b29d2013-11-21 16:45:28 +09002297 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002298
Michel Dänzer404b29d2013-11-21 16:45:28 +09002299 /* Streamout SGPRs. */
2300 if (shader->selector->so.num_outputs) {
2301 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2302 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2303 }
2304 /* A streamout buffer offset is loaded if the stride is non-zero. */
2305 for (i = 0; i < 4; i++) {
2306 if (!shader->selector->so.stride[i])
2307 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002308
Michel Dänzer404b29d2013-11-21 16:45:28 +09002309 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2310 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002311 }
2312
2313 last_sgpr = num_params-1;
2314
2315 /* VGPRs */
2316 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2317 params[num_params++] = i32; /* unused*/
2318 params[num_params++] = i32; /* unused */
2319 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002320 break;
Christian König0666ffd2013-03-05 15:07:39 +01002321
Michel Dänzer404b29d2013-11-21 16:45:28 +09002322 case TGSI_PROCESSOR_GEOMETRY:
2323 params[SI_PARAM_GS2VS_OFFSET] = i32;
2324 params[SI_PARAM_GS_WAVE_ID] = i32;
2325 last_sgpr = SI_PARAM_GS_WAVE_ID;
2326
2327 /* VGPRs */
2328 params[SI_PARAM_VTX0_OFFSET] = i32;
2329 params[SI_PARAM_VTX1_OFFSET] = i32;
2330 params[SI_PARAM_PRIMITIVE_ID] = i32;
2331 params[SI_PARAM_VTX2_OFFSET] = i32;
2332 params[SI_PARAM_VTX3_OFFSET] = i32;
2333 params[SI_PARAM_VTX4_OFFSET] = i32;
2334 params[SI_PARAM_VTX5_OFFSET] = i32;
2335 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2336 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2337 break;
2338
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002339 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002340 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002341 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002342 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002343 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2344 params[SI_PARAM_PERSP_CENTER] = v2i32;
2345 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2346 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2347 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2348 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2349 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2350 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2351 params[SI_PARAM_POS_X_FLOAT] = f32;
2352 params[SI_PARAM_POS_Y_FLOAT] = f32;
2353 params[SI_PARAM_POS_Z_FLOAT] = f32;
2354 params[SI_PARAM_POS_W_FLOAT] = f32;
2355 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002356 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002357 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2358 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002359 num_params = SI_PARAM_POS_FIXED_PT+1;
2360 break;
2361
2362 default:
2363 assert(0 && "unimplemented shader");
2364 return;
Christian Königc4973212013-03-05 12:14:02 +01002365 }
Christian König55fe5cc2013-03-04 16:30:06 +01002366
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002367 assert(num_params <= Elements(params));
2368 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002369 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002370
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002371 for (i = 0; i <= last_sgpr; ++i) {
2372 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002373
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002374 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2375 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002376 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002377 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002378 else
2379 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002380 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002381
Michel Dänzer404b29d2013-11-21 16:45:28 +09002382 if (bld_base->info &&
2383 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2384 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002385 si_shader_ctx->ddxy_lds =
2386 LLVMAddGlobalInAddressSpace(gallivm->module,
2387 LLVMArrayType(i32, 64),
2388 "ddxy_lds",
2389 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002390}
Tom Stellarda75c6162012-01-06 17:38:37 -05002391
Christian König0f6cf2b2013-03-15 15:53:25 +01002392static void preload_constants(struct si_shader_context *si_shader_ctx)
2393{
2394 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2395 struct gallivm_state * gallivm = bld_base->base.gallivm;
2396 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002397 unsigned buf;
2398 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002399
Marek Olšákee2a8182014-07-07 23:27:19 +02002400 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002401 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002402
Marek Olšák2fd42002013-10-25 11:45:47 +02002403 if (num_const == 0)
2404 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002405
Marek Olšák2fd42002013-10-25 11:45:47 +02002406 /* Allocate space for the constant values */
2407 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002408
Marek Olšák2fd42002013-10-25 11:45:47 +02002409 /* Load the resource descriptor */
2410 si_shader_ctx->const_resource[buf] =
2411 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002412
Marek Olšák2fd42002013-10-25 11:45:47 +02002413 /* Load the constants, we rely on the code sinking to do the rest */
2414 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002415 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002416 load_const(gallivm->builder,
2417 si_shader_ctx->const_resource[buf],
2418 lp_build_const_int32(gallivm, i * 4),
2419 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002420 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002421 }
2422}
2423
Christian König1c100182013-03-17 16:02:42 +01002424static void preload_samplers(struct si_shader_context *si_shader_ctx)
2425{
2426 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2427 struct gallivm_state * gallivm = bld_base->base.gallivm;
2428 const struct tgsi_shader_info * info = bld_base->info;
2429
2430 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2431
2432 LLVMValueRef res_ptr, samp_ptr;
2433 LLVMValueRef offset;
2434
2435 if (num_samplers == 0)
2436 return;
2437
2438 /* Allocate space for the values */
Marek Olšákee2a8182014-07-07 23:27:19 +02002439 si_shader_ctx->resources = CALLOC(SI_NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002440 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2441
2442 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2443 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2444
2445 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2446 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002447 /* Resource */
2448 offset = lp_build_const_int32(gallivm, i);
2449 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2450
2451 /* Sampler */
2452 offset = lp_build_const_int32(gallivm, i);
2453 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002454
2455 /* FMASK resource */
2456 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002457 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2458 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšák4855acd2013-08-06 15:08:54 +02002459 build_indexed_load(si_shader_ctx, res_ptr, offset);
2460 }
Christian König1c100182013-03-17 16:02:42 +01002461 }
2462}
2463
Marek Olšák8d03d922013-09-01 23:59:06 +02002464static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2465{
2466 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2467 struct gallivm_state * gallivm = bld_base->base.gallivm;
2468 unsigned i;
2469
Michel Dänzer67e385b2014-01-08 17:48:21 +09002470 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2471 si_shader_ctx->shader->key.vs.as_es ||
2472 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002473 return;
2474
2475 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002476 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002477
2478 /* Load the resources, we rely on the code sinking to do the rest */
2479 for (i = 0; i < 4; ++i) {
2480 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002481 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002482 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002483
2484 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2485 }
2486 }
2487}
2488
Marek Olšák1abb1a92014-09-17 22:44:22 +02002489int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2490 LLVMModuleRef mod)
Tom Stellard302f53d2012-10-25 13:50:10 -04002491{
Darren Powellbc866902014-03-31 18:00:28 -04002492 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002493 unsigned i;
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002494 unsigned char *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002495 struct radeon_shader_binary binary;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002496 bool dump = r600_can_dump_shader(&sscreen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002497 shader->selector ? shader->selector->tokens : NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002498 const char * gpu_family = r600_get_llvm_processor_name(sscreen->b.family);
Tom Stellard9ba31052014-07-14 16:49:08 -04002499 unsigned code_size;
Darren Powellbc866902014-03-31 18:00:28 -04002500
2501 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002502 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002503 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2504
2505 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002506 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002507 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002508 for (i = 0; i < binary.code_size; i+=4 ) {
2509 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2510 binary.code[i + 2], binary.code[i + 1],
2511 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002512 }
2513 }
2514
Tom Stellardd50343d2013-04-04 16:21:06 -04002515 /* XXX: We may be able to emit some of these values directly rather than
2516 * extracting fields to be emitted later.
2517 */
Darren Powellbc866902014-03-31 18:00:28 -04002518 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002519 for (i = 0; i < binary.config_size; i+= 8) {
2520 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2521 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2522 switch (reg) {
2523 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2524 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2525 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2526 case R_00B848_COMPUTE_PGM_RSRC1:
2527 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2528 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2529 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002530 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2531 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2532 break;
2533 case R_00B84C_COMPUTE_PGM_RSRC2:
2534 shader->lds_size = G_00B84C_LDS_SIZE(value);
2535 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002536 case R_0286CC_SPI_PS_INPUT_ENA:
2537 shader->spi_ps_input_ena = value;
2538 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002539 case R_00B860_COMPUTE_TMPRING_SIZE:
2540 /* WAVESIZE is in units of 256 dwords. */
2541 shader->scratch_bytes_per_wave =
2542 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2543 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002544 default:
2545 fprintf(stderr, "Warning: Compiler emitted unknown "
2546 "config register: 0x%x\n", reg);
2547 break;
2548 }
2549 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002550
2551 /* copy new shader */
Tom Stellard9ba31052014-07-14 16:49:08 -04002552 code_size = binary.code_size + binary.rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002553 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002554 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002555 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002556 if (shader->bo == NULL) {
2557 return -ENOMEM;
2558 }
2559
Marek Olšák1abb1a92014-09-17 22:44:22 +02002560 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_WRITE);
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002561 util_memcpy_cpu_to_le32(ptr, binary.code, binary.code_size);
2562 if (binary.rodata_size > 0) {
2563 ptr += binary.code_size;
2564 util_memcpy_cpu_to_le32(ptr, binary.rodata, binary.rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002565 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002566
Marek Olšák1abb1a92014-09-17 22:44:22 +02002567 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002568
Tom Stellard7782d192013-04-04 09:57:13 -07002569 free(binary.code);
2570 free(binary.config);
Tom Stellard9ba31052014-07-14 16:49:08 -04002571 free(binary.rodata);
Tom Stellard302f53d2012-10-25 13:50:10 -04002572
Darren Powellbc866902014-03-31 18:00:28 -04002573 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002574}
2575
Michel Dänzer404b29d2013-11-21 16:45:28 +09002576/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002577static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002578 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002579 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002580{
2581 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2582 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2583 struct lp_build_context *base = &bld_base->base;
2584 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002585 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002586 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002587 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002588 LLVMValueRef t_list_ptr, t_list;
2589 LLVMValueRef args[9];
2590 int i, r;
2591
Marek Olšák02134cf2014-10-04 22:07:50 +02002592 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002593
2594 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002595 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002596
2597 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2598
2599 create_meta_data(si_shader_ctx);
2600 create_function(si_shader_ctx);
2601 preload_streamout_buffers(si_shader_ctx);
2602
2603 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002604 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2605 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002606 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002607 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002608
2609 args[0] = t_list;
2610 args[1] = lp_build_mul_imm(uint,
2611 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2612 si_shader_ctx->param_vertex_id),
2613 4);
2614 args[3] = uint->zero;
2615 args[4] = uint->one; /* OFFEN */
2616 args[5] = uint->zero; /* IDXEN */
2617 args[6] = uint->one; /* GLC */
2618 args[7] = uint->one; /* SLC */
2619 args[8] = uint->zero; /* TFE */
2620
2621 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002622 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002623 unsigned chan;
2624
Marek Olšák02134cf2014-10-04 22:07:50 +02002625 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002626 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002627
2628 for (chan = 0; chan < 4; chan++) {
2629 args[2] = lp_build_const_int32(gallivm,
2630 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002631 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002632
2633 outputs[i].values[chan] =
2634 LLVMBuildBitCast(gallivm->builder,
2635 build_intrinsic(gallivm->builder,
2636 "llvm.SI.buffer.load.dword.i32.i32",
2637 LLVMInt32TypeInContext(gallivm->context),
2638 args, 9,
2639 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2640 base->elem_type, "");
2641 }
2642 }
2643
Marek Olšák02134cf2014-10-04 22:07:50 +02002644 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002645
2646 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2647
2648 if (dump)
2649 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2650
Marek Olšák1abb1a92014-09-17 22:44:22 +02002651 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002652 bld_base->base.gallivm->module);
2653
2654 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2655
2656 FREE(outputs);
2657 return r;
2658}
2659
Marek Olšák1abb1a92014-09-17 22:44:22 +02002660int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002661{
Marek Olšák2774abd2014-09-16 18:45:33 +02002662 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002663 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002664 struct lp_build_tgsi_context * bld_base;
2665 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002666 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002667 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002668
2669 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2670 * conversion fails. */
2671 if (dump) {
2672 tgsi_dump(sel->tokens, 0);
2673 si_dump_streamout(&sel->so);
2674 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002675
Marek Olšák8c37c162014-09-16 18:40:07 +02002676 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002677
Michel Dänzercfebaf92012-08-31 19:04:08 +02002678 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002679 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2680 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2681
Marek Olšák52335682014-09-30 15:12:09 +02002682 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002683 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2684
Marek Olšák52335682014-09-30 15:12:09 +02002685 shader->uses_instanceid = sel->info.uses_instanceid;
2686 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002687 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002688
2689 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002690 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002691 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2692 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002693 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002694 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2695 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2696 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002697 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002698 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002699 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2700 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002701
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002702 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2703 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002704
Michel Dänzer404b29d2013-11-21 16:45:28 +09002705 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2706 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2707
Christian Könige4ed5872013-03-21 18:02:52 +01002708 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002709 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002710 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2711 si_shader_ctx.shader = shader;
2712 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002713
Michel Dänzer51f89a02013-12-09 15:33:53 +09002714 switch (si_shader_ctx.type) {
2715 case TGSI_PROCESSOR_VERTEX:
2716 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002717 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002718 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2719 } else {
2720 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2721 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002722 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002723 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002724 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2725 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002726 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002727 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002728 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2729 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002730
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002731 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002732 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2733 shader->db_shader_control |=
2734 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2735 break;
2736 case TGSI_FS_DEPTH_LAYOUT_LESS:
2737 shader->db_shader_control |=
2738 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2739 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002740 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002741 break;
2742 default:
2743 assert(!"Unsupported shader type");
2744 return -1;
2745 }
2746
Christian König206f0592013-03-20 14:37:21 +01002747 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002748 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002749 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002750 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002751 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002752
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002753 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2754 si_shader_ctx.gs_next_vertex =
2755 lp_build_alloca(bld_base->base.gallivm,
2756 bld_base->uint_bld.elem_type, "");
2757 }
2758
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002759 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002760 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002761 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002762 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002763
2764 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2765
2766 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002767 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002768 if (r) {
2769 fprintf(stderr, "LLVM failed to compile shader\n");
2770 goto out;
2771 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002772
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002773 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002774
2775 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002776 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002777 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002778 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002779 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002780 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2781 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002782 free(shader->gs_copy_shader);
2783 shader->gs_copy_shader = NULL;
2784 goto out;
2785 }
2786 }
2787
Tom Stellarda75c6162012-01-06 17:38:37 -05002788 tgsi_parse_free(&si_shader_ctx.parse);
2789
Michel Dänzer404b29d2013-11-21 16:45:28 +09002790out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002791 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002792 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002793 FREE(si_shader_ctx.resources);
2794 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002795
Tom Stellard302f53d2012-10-25 13:50:10 -04002796 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002797}
2798
Marek Olšák2774abd2014-09-16 18:45:33 +02002799void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002800{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002801 if (shader->gs_copy_shader)
2802 si_shader_destroy(ctx, shader->gs_copy_shader);
2803
Marek Olšáka81c3e02013-08-14 01:04:39 +02002804 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002805 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002806}