blob: 276c27e236d38d28b377e12e17620ea75acc21ff [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];
Marek Olšák55a9b772014-10-05 12:38:54 +020071 LLVMValueRef resources[SI_NUM_SAMPLER_VIEWS];
72 LLVMValueRef samplers[SI_NUM_SAMPLER_STATES];
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/**
Marek Olšákd7876082014-09-26 23:06:32 +0200185 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
186 * It's equivalent to doing a load from &base_ptr[index].
Tom Stellard467f5162012-05-16 15:15:35 -0400187 *
Marek Olšákd7876082014-09-26 23:06:32 +0200188 * \param base_ptr Where the array starts.
189 * \param index The element index into the array.
Tom Stellard467f5162012-05-16 15:15:35 -0400190 */
Marek Olšákd7876082014-09-26 23:06:32 +0200191static LLVMValueRef build_indexed_load(struct si_shader_context *si_shader_ctx,
192 LLVMValueRef base_ptr, LLVMValueRef index)
Tom Stellard467f5162012-05-16 15:15:35 -0400193{
Marek Olšákd7876082014-09-26 23:06:32 +0200194 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
195 struct gallivm_state *gallivm = bld_base->base.gallivm;
196 LLVMValueRef indices[2], pointer;
Tom Stellard467f5162012-05-16 15:15:35 -0400197
Marek Olšákd7876082014-09-26 23:06:32 +0200198 indices[0] = bld_base->uint_bld.zero;
199 indices[1] = index;
Christian König206f0592013-03-20 14:37:21 +0100200
Marek Olšákd7876082014-09-26 23:06:32 +0200201 pointer = LLVMBuildGEP(gallivm->builder, base_ptr, indices, 2, "");
202 return LLVMBuildLoad(gallivm->builder, pointer, "");
203}
204
205/**
206 * Do a load from &base_ptr[index], but also add a flag that it's loading
207 * a constant.
208 */
209static LLVMValueRef build_indexed_load_const(
210 struct si_shader_context * si_shader_ctx,
211 LLVMValueRef base_ptr, LLVMValueRef index)
212{
213 LLVMValueRef result = build_indexed_load(si_shader_ctx, base_ptr, index);
Christian König206f0592013-03-20 14:37:21 +0100214 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
215 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400216}
217
Marek Olšákf317ce52013-09-05 15:39:57 +0200218static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100219 struct radeon_llvm_context * radeon_bld,
220 unsigned divisor)
221{
Marek Olšák8d03d922013-09-01 23:59:06 +0200222 struct si_shader_context *si_shader_ctx =
223 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100224 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
225
Marek Olšák8d03d922013-09-01 23:59:06 +0200226 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
227 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100228 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
229 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
230
231 if (divisor > 1)
232 result = LLVMBuildUDiv(gallivm->builder, result,
233 lp_build_const_int32(gallivm, divisor), "");
234
235 return result;
236}
237
Tom Stellarda75c6162012-01-06 17:38:37 -0500238static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900239 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500240 unsigned input_index,
241 const struct tgsi_full_declaration *decl)
242{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900243 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
244 struct gallivm_state *gallivm = base->gallivm;
245 struct si_shader_context *si_shader_ctx =
246 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100247 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
248
249 unsigned chan;
250
Tom Stellarda75c6162012-01-06 17:38:37 -0500251 LLVMValueRef t_list_ptr;
252 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400253 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500254 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100255 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400256 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500257 LLVMTypeRef vec4_type;
258 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500259
Tom Stellard467f5162012-05-16 15:15:35 -0400260 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100261 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500262
Michel Dänzer51f89a02013-12-09 15:33:53 +0900263 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400264
Marek Olšákd7876082014-09-26 23:06:32 +0200265 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400266
267 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900268 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500269
Christian Königa0dca442013-03-22 15:59:22 +0100270 if (divisor) {
271 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200272 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200273 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100274 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200275 /* Load the buffer index for vertices. */
276 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
277 si_shader_ctx->param_vertex_id);
278 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
279 SI_PARAM_BASE_VERTEX);
280 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100281 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500282
283 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400284 args[0] = t_list;
285 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100286 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900287 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100288 "llvm.SI.vs.load.input", vec4_type, args, 3,
289 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500290
291 /* Break up the vec4 into individual components */
292 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900293 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500294 /* XXX: Use a helper function for this. There is one in
295 * tgsi_llvm.c. */
296 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900297 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500298 input, llvm_chan, "");
299 }
300}
301
Michel Dänzer404b29d2013-11-21 16:45:28 +0900302static LLVMValueRef fetch_input_gs(
303 struct lp_build_tgsi_context *bld_base,
304 const struct tgsi_full_src_register *reg,
305 enum tgsi_opcode_type type,
306 unsigned swizzle)
307{
308 struct lp_build_context *base = &bld_base->base;
309 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200310 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900311 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
312 struct gallivm_state *gallivm = base->gallivm;
313 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
314 LLVMValueRef vtx_offset;
315 LLVMValueRef t_list_ptr;
316 LLVMValueRef t_list;
317 LLVMValueRef args[9];
318 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200319 struct tgsi_shader_info *info = &shader->selector->info;
320 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
321 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900322
Marek Olšáke23fec12014-10-04 17:40:39 +0200323 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900324 if (swizzle == 0)
325 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
326 SI_PARAM_PRIMITIVE_ID);
327 else
328 return uint->zero;
329 }
330
Michel Dänzer404b29d2013-11-21 16:45:28 +0900331 if (!reg->Register.Dimension)
332 return NULL;
333
334 if (swizzle == ~0) {
335 LLVMValueRef values[TGSI_NUM_CHANNELS];
336 unsigned chan;
337 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
338 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
339 }
340 return lp_build_gather_values(bld_base->base.gallivm, values,
341 TGSI_NUM_CHANNELS);
342 }
343
344 /* Get the vertex offset parameter */
345 vtx_offset_param = reg->Dimension.Index;
346 if (vtx_offset_param < 2) {
347 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
348 } else {
349 assert(vtx_offset_param < 6);
350 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
351 }
352 vtx_offset = lp_build_mul_imm(uint,
353 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
354 vtx_offset_param),
355 4);
356
357 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +0900358 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
359 SI_PARAM_RW_BUFFERS);
Marek Olšákd7876082014-09-26 23:06:32 +0200360 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900361 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900362
363 args[0] = t_list;
364 args[1] = vtx_offset;
365 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200366 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200367 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900368 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900369 args[3] = uint->zero;
370 args[4] = uint->one; /* OFFEN */
371 args[5] = uint->zero; /* IDXEN */
372 args[6] = uint->one; /* GLC */
373 args[7] = uint->zero; /* SLC */
374 args[8] = uint->zero; /* TFE */
375
376 return LLVMBuildBitCast(gallivm->builder,
377 build_intrinsic(gallivm->builder,
378 "llvm.SI.buffer.load.dword.i32.i32",
379 i32, args, 9,
380 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
381 tgsi2llvmtype(bld_base, type), "");
382}
383
Tom Stellarda75c6162012-01-06 17:38:37 -0500384static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900385 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500386 unsigned input_index,
387 const struct tgsi_full_declaration *decl)
388{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900389 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
390 struct si_shader_context *si_shader_ctx =
391 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200392 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900393 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
394 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400395 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900396 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100397
398 LLVMValueRef interp_param;
399 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500400
401 /* This value is:
402 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
403 * quad begins a new primitive. Bit 0 always needs
404 * to be unset)
405 * [32:16] ParamOffset
406 *
407 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900408 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200409 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500410
Christian König0666ffd2013-03-05 15:07:39 +0100411 unsigned chan;
412
Tom Stellard0fb1e682012-09-06 16:18:11 -0400413 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
414 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400415 unsigned soa_index =
416 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900417 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100418 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100419
420 if (chan == 3)
421 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900422 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100423 LLVMBuildFDiv(gallivm->builder,
424 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900425 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100426 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400427 }
428 return;
429 }
430
Michel Dänzer97078b12012-09-25 12:41:31 +0200431 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900432 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Marek Olšáke827bb62014-10-16 16:20:26 +0200433 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900434 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
435 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200436 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900437 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200438 lp_build_const_float(gallivm, 1.0f);
439
440 return;
441 }
442
Marek Olšák8b057dd2014-10-04 22:15:07 +0200443 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200444 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200445 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200446
Francisco Jerez12799232012-04-30 18:27:52 +0200447 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500448 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100449 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500450 break;
451 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200452 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200453 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400454 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100455 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200456 else
Christian König0666ffd2013-03-05 15:07:39 +0100457 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200458 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200459 case TGSI_INTERPOLATE_COLOR:
460 if (si_shader_ctx->shader->key.ps.flatshade) {
461 interp_param = 0;
462 break;
463 }
464 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200465 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200466 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200467 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400468 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100469 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200470 else
Christian König0666ffd2013-03-05 15:07:39 +0100471 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500472 break;
473 default:
474 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
475 return;
476 }
477
Christian König0666ffd2013-03-05 15:07:39 +0100478 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
479
Tom Stellarda75c6162012-01-06 17:38:37 -0500480 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200481 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100482 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100483 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200484 LLVMValueRef face, is_face_positive;
485 LLVMValueRef back_attr_number =
486 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200487 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200488
Christian König0666ffd2013-03-05 15:07:39 +0100489 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
490
Michel Dänzer691f08d2012-09-06 18:03:38 +0200491 is_face_positive = LLVMBuildFCmp(gallivm->builder,
492 LLVMRealUGT, face,
493 lp_build_const_float(gallivm, 0.0f),
494 "");
495
Tom Stellarda75c6162012-01-06 17:38:37 -0500496 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100497 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200498 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
499 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
500 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
501 LLVMValueRef front, back;
502
503 args[0] = llvm_chan;
504 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900505 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100506 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100507 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200508
509 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900510 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100511 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100512 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200513
Michel Dänzer51f89a02013-12-09 15:33:53 +0900514 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200515 LLVMBuildSelect(gallivm->builder,
516 is_face_positive,
517 front,
518 back,
519 "");
520 }
521
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900522 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200523 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
524 LLVMValueRef args[4];
525
526 args[0] = uint->zero;
527 args[1] = attr_number;
528 args[2] = params;
529 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900530 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
531 build_intrinsic(gallivm->builder, intr_name,
532 input_type, args, args[3] ? 4 : 3,
533 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
534 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
535 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200536 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900537 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200538 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200539 } else {
540 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100541 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200542 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
543 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
544 args[0] = llvm_chan;
545 args[1] = attr_number;
546 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100547 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900548 radeon_bld->inputs[soa_index] =
549 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100550 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100551 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200552 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500553 }
554}
555
Marek Olšák5b06fc32014-05-06 18:12:40 +0200556static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
557{
558 struct gallivm_state *gallivm = &radeon_bld->gallivm;
559 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
560 SI_PARAM_ANCILLARY);
561 value = LLVMBuildLShr(gallivm->builder, value,
562 lp_build_const_int32(gallivm, 8), "");
563 value = LLVMBuildAnd(gallivm->builder, value,
564 lp_build_const_int32(gallivm, 0xf), "");
565 return value;
566}
567
Marek Olšákd7876082014-09-26 23:06:32 +0200568/**
569 * Load a dword from a constant buffer.
570 */
571static LLVMValueRef buffer_load_const(LLVMBuilderRef builder, LLVMValueRef resource,
572 LLVMValueRef offset, LLVMTypeRef return_type)
Marek Olšák250aa932014-05-06 14:10:47 +0200573{
574 LLVMValueRef args[2] = {resource, offset};
575
576 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
577 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
578}
579
Christian Könige4ed5872013-03-21 18:02:52 +0100580static void declare_system_value(
581 struct radeon_llvm_context * radeon_bld,
582 unsigned index,
583 const struct tgsi_full_declaration *decl)
584{
Marek Olšák8d03d922013-09-01 23:59:06 +0200585 struct si_shader_context *si_shader_ctx =
586 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200587 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
588 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100589 LLVMValueRef value = 0;
590
591 switch (decl->Semantic.Name) {
592 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200593 value = LLVMGetParam(radeon_bld->main_fn,
594 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100595 break;
596
597 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200598 value = LLVMGetParam(radeon_bld->main_fn,
599 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100600 break;
601
Marek Olšák5b06fc32014-05-06 18:12:40 +0200602 case TGSI_SEMANTIC_SAMPLEID:
603 value = get_sample_id(radeon_bld);
604 break;
605
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200606 case TGSI_SEMANTIC_SAMPLEPOS:
607 {
608 LLVMBuilderRef builder = gallivm->builder;
609 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200610 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200611 LLVMValueRef resource = build_indexed_load_const(si_shader_ctx, desc, buf_index);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200612
613 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
614 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
615 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
616
617 LLVMValueRef pos[4] = {
Marek Olšákd7876082014-09-26 23:06:32 +0200618 buffer_load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
619 buffer_load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200620 lp_build_const_float(gallivm, 0),
621 lp_build_const_float(gallivm, 0)
622 };
623 value = lp_build_gather_values(gallivm, pos, 4);
624 break;
625 }
626
Christian Könige4ed5872013-03-21 18:02:52 +0100627 default:
628 assert(!"unknown system value");
629 return;
630 }
631
632 radeon_bld->system_values[index] = value;
633}
634
Tom Stellarda75c6162012-01-06 17:38:37 -0500635static LLVMValueRef fetch_constant(
636 struct lp_build_tgsi_context * bld_base,
637 const struct tgsi_full_src_register *reg,
638 enum tgsi_opcode_type type,
639 unsigned swizzle)
640{
Christian König55fe5cc2013-03-04 16:30:06 +0100641 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500642 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100643 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200644 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500645
Christian König0f6cf2b2013-03-15 15:53:25 +0100646 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100647 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500648
Christian König8514f5a2013-02-04 17:46:42 +0100649 if (swizzle == LP_CHAN_ALL) {
650 unsigned chan;
651 LLVMValueRef values[4];
652 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
653 values[chan] = fetch_constant(bld_base, reg, type, chan);
654
655 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
656 }
657
Marek Olšák2fd42002013-10-25 11:45:47 +0200658 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100659 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100660
Marek Olšák2fd42002013-10-25 11:45:47 +0200661 if (!reg->Register.Indirect)
662 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
663
Christian König0f6cf2b2013-03-15 15:53:25 +0100664 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
665 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
666 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200667 addr = lp_build_add(&bld_base->uint_bld, addr,
668 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200669
Marek Olšákd7876082014-09-26 23:06:32 +0200670 result = buffer_load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
Marek Olšák250aa932014-05-06 14:10:47 +0200671 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500672
Christian Königf5298b02013-02-28 14:50:07 +0100673 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500674}
675
Michel Dänzer26c71392012-08-24 12:03:11 +0200676/* Initialize arguments for the shader export intrinsic */
677static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900678 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200679 unsigned target,
680 LLVMValueRef *args)
681{
682 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
683 struct lp_build_context *uint =
684 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
685 struct lp_build_context *base = &bld_base->base;
686 unsigned compressed = 0;
687 unsigned chan;
688
Michel Dänzerf402acd2012-08-22 18:15:36 +0200689 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
690 int cbuf = target - V_008DFC_SQ_EXP_MRT;
691
692 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100693 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100694
695 if (compressed)
696 si_shader_ctx->shader->spi_shader_col_format |=
697 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
698 else
699 si_shader_ctx->shader->spi_shader_col_format |=
700 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200701
702 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200703 }
704 }
705
706 if (compressed) {
707 /* Pixel shader needs to pack output values before export */
708 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900709 args[0] = values[2 * chan];
710 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200711 args[chan + 5] =
712 build_intrinsic(base->gallivm->builder,
713 "llvm.SI.packf16",
714 LLVMInt32TypeInContext(base->gallivm->context),
715 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100716 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100717 args[chan + 7] = args[chan + 5] =
718 LLVMBuildBitCast(base->gallivm->builder,
719 args[chan + 5],
720 LLVMFloatTypeInContext(base->gallivm->context),
721 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200722 }
723
724 /* Set COMPR flag */
725 args[4] = uint->one;
726 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900727 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200728 /* +5 because the first output value will be
729 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900730 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200731
732 /* Clear COMPR flag */
733 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200734 }
735
736 /* XXX: This controls which components of the output
737 * registers actually get exported. (e.g bit 0 means export
738 * X component, bit 1 means export Y component, etc.) I'm
739 * hard coding this to 0xf for now. In the future, we might
740 * want to do something else. */
741 args[0] = lp_build_const_int32(base->gallivm, 0xf);
742
743 /* Specify whether the EXEC mask represents the valid mask */
744 args[1] = uint->zero;
745
746 /* Specify whether this is the last export */
747 args[2] = uint->zero;
748
749 /* Specify the target we are exporting */
750 args[3] = lp_build_const_int32(base->gallivm, target);
751
Michel Dänzer26c71392012-08-24 12:03:11 +0200752 /* XXX: We probably need to keep track of the output
753 * values, so we know what we are passing to the next
754 * stage. */
755}
756
Michel Dänzer404b29d2013-11-21 16:45:28 +0900757/* Load from output pointers and initialize arguments for the shader export intrinsic */
758static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
759 LLVMValueRef *out_ptr,
760 unsigned target,
761 LLVMValueRef *args)
762{
763 struct gallivm_state *gallivm = bld_base->base.gallivm;
764 LLVMValueRef values[4];
765 int i;
766
767 for (i = 0; i < 4; i++)
768 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
769
770 si_llvm_init_export_args(bld_base, values, target, args);
771}
772
Michel Dänzer7708a862012-11-02 15:57:30 +0100773static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900774 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100775{
776 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
777 struct gallivm_state *gallivm = bld_base->base.gallivm;
778
Christian Königa0dca442013-03-22 15:59:22 +0100779 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400780 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
781 SI_PARAM_ALPHA_REF);
782
Michel Dänzer7708a862012-11-02 15:57:30 +0100783 LLVMValueRef alpha_pass =
784 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100785 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900786 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400787 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100788 LLVMValueRef arg =
789 lp_build_select(&bld_base->base,
790 alpha_pass,
791 lp_build_const_float(gallivm, 1.0f),
792 lp_build_const_float(gallivm, -1.0f));
793
794 build_intrinsic(gallivm->builder,
795 "llvm.AMDGPU.kill",
796 LLVMVoidTypeInContext(gallivm->context),
797 &arg, 1, 0);
798 } else {
799 build_intrinsic(gallivm->builder,
800 "llvm.AMDGPU.kilp",
801 LLVMVoidTypeInContext(gallivm->context),
802 NULL, 0, 0);
803 }
Marek Olšákadc57972014-09-19 17:07:07 +0200804
805 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100806}
807
Michel Dänzere3befbc2013-05-15 18:09:50 +0200808static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900809 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200810{
811 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200812 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200813 struct lp_build_context *base = &bld_base->base;
814 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200815 unsigned reg_index;
816 unsigned chan;
817 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200818 LLVMValueRef base_elt;
819 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200820 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200821 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200822
Michel Dänzere3befbc2013-05-15 18:09:50 +0200823 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200824 LLVMValueRef *args = pos[2 + reg_index];
825
Marek Olšák8c37c162014-09-16 18:40:07 +0200826 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200827
Michel Dänzere3befbc2013-05-15 18:09:50 +0200828 args[5] =
829 args[6] =
830 args[7] =
831 args[8] = lp_build_const_float(base->gallivm, 0.0f);
832
833 /* Compute dot products of position and user clip plane vectors */
834 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
835 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200836 args[1] = lp_build_const_int32(base->gallivm,
837 ((reg_index * 4 + chan) * 4 +
838 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200839 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200840 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200841 args[5 + chan] =
842 lp_build_add(base, args[5 + chan],
843 lp_build_mul(base, base_elt,
844 out_elts[const_chan]));
845 }
846 }
847
848 args[0] = lp_build_const_int32(base->gallivm, 0xf);
849 args[1] = uint->zero;
850 args[2] = uint->zero;
851 args[3] = lp_build_const_int32(base->gallivm,
852 V_008DFC_SQ_EXP_POS + 2 + reg_index);
853 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200854 }
855}
856
Marek Olšák8d03d922013-09-01 23:59:06 +0200857static void si_dump_streamout(struct pipe_stream_output_info *so)
858{
859 unsigned i;
860
861 if (so->num_outputs)
862 fprintf(stderr, "STREAMOUT\n");
863
864 for (i = 0; i < so->num_outputs; i++) {
865 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
866 so->output[i].start_component;
867 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
868 i, so->output[i].output_buffer,
869 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
870 so->output[i].register_index,
871 mask & 1 ? "x" : "",
872 mask & 2 ? "y" : "",
873 mask & 4 ? "z" : "",
874 mask & 8 ? "w" : "");
875 }
876}
877
878/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
879 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
880 * or v4i32 (num_channels=3,4). */
881static void build_tbuffer_store(struct si_shader_context *shader,
882 LLVMValueRef rsrc,
883 LLVMValueRef vdata,
884 unsigned num_channels,
885 LLVMValueRef vaddr,
886 LLVMValueRef soffset,
887 unsigned inst_offset,
888 unsigned dfmt,
889 unsigned nfmt,
890 unsigned offen,
891 unsigned idxen,
892 unsigned glc,
893 unsigned slc,
894 unsigned tfe)
895{
896 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
897 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
898 LLVMValueRef args[] = {
899 rsrc,
900 vdata,
901 LLVMConstInt(i32, num_channels, 0),
902 vaddr,
903 soffset,
904 LLVMConstInt(i32, inst_offset, 0),
905 LLVMConstInt(i32, dfmt, 0),
906 LLVMConstInt(i32, nfmt, 0),
907 LLVMConstInt(i32, offen, 0),
908 LLVMConstInt(i32, idxen, 0),
909 LLVMConstInt(i32, glc, 0),
910 LLVMConstInt(i32, slc, 0),
911 LLVMConstInt(i32, tfe, 0)
912 };
913
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900914 /* The instruction offset field has 12 bits */
915 assert(offen || inst_offset < (1 << 12));
916
Marek Olšák8d03d922013-09-01 23:59:06 +0200917 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
918 unsigned func = CLAMP(num_channels, 1, 3) - 1;
919 const char *types[] = {"i32", "v2i32", "v4i32"};
920 char name[256];
921 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
922
923 lp_build_intrinsic(gallivm->builder, name,
924 LLVMVoidTypeInContext(gallivm->context),
925 args, Elements(args));
926}
927
928static void build_streamout_store(struct si_shader_context *shader,
929 LLVMValueRef rsrc,
930 LLVMValueRef vdata,
931 unsigned num_channels,
932 LLVMValueRef vaddr,
933 LLVMValueRef soffset,
934 unsigned inst_offset)
935{
936 static unsigned dfmt[] = {
937 V_008F0C_BUF_DATA_FORMAT_32,
938 V_008F0C_BUF_DATA_FORMAT_32_32,
939 V_008F0C_BUF_DATA_FORMAT_32_32_32,
940 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
941 };
942 assert(num_channels >= 1 && num_channels <= 4);
943
944 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
945 inst_offset, dfmt[num_channels-1],
946 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
947}
948
949/* On SI, the vertex shader is responsible for writing streamout data
950 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900951static void si_llvm_emit_streamout(struct si_shader_context *shader,
952 struct si_shader_output_values *outputs,
953 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200954{
955 struct pipe_stream_output_info *so = &shader->shader->selector->so;
956 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
957 LLVMBuilderRef builder = gallivm->builder;
958 int i, j;
959 struct lp_build_if_state if_ctx;
960
961 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
962
963 LLVMValueRef so_param =
964 LLVMGetParam(shader->radeon_bld.main_fn,
965 shader->param_streamout_config);
966
967 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
968 LLVMValueRef so_vtx_count =
969 LLVMBuildAnd(builder,
970 LLVMBuildLShr(builder, so_param,
971 LLVMConstInt(i32, 16, 0), ""),
972 LLVMConstInt(i32, 127, 0), "");
973
974 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
975 NULL, 0, LLVMReadNoneAttribute);
976
977 /* can_emit = tid < so_vtx_count; */
978 LLVMValueRef can_emit =
979 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
980
981 /* Emit the streamout code conditionally. This actually avoids
982 * out-of-bounds buffer access. The hw tells us via the SGPR
983 * (so_vtx_count) which threads are allowed to emit streamout data. */
984 lp_build_if(&if_ctx, gallivm, can_emit);
985 {
986 /* The buffer offset is computed as follows:
987 * ByteOffset = streamout_offset[buffer_id]*4 +
988 * (streamout_write_index + thread_id)*stride[buffer_id] +
989 * attrib_offset
990 */
991
992 LLVMValueRef so_write_index =
993 LLVMGetParam(shader->radeon_bld.main_fn,
994 shader->param_streamout_write_index);
995
996 /* Compute (streamout_write_index + thread_id). */
997 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
998
999 /* Compute the write offset for each enabled buffer. */
1000 LLVMValueRef so_write_offset[4] = {};
1001 for (i = 0; i < 4; i++) {
1002 if (!so->stride[i])
1003 continue;
1004
1005 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1006 shader->param_streamout_offset[i]);
1007 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1008
1009 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1010 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1011 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1012 }
1013
Marek Olšák8d03d922013-09-01 23:59:06 +02001014 /* Write streamout data. */
1015 for (i = 0; i < so->num_outputs; i++) {
1016 unsigned buf_idx = so->output[i].output_buffer;
1017 unsigned reg = so->output[i].register_index;
1018 unsigned start = so->output[i].start_component;
1019 unsigned num_comps = so->output[i].num_components;
1020 LLVMValueRef out[4];
1021
1022 assert(num_comps && num_comps <= 4);
1023 if (!num_comps || num_comps > 4)
1024 continue;
1025
Marek Olšák558f7772014-10-04 22:37:23 +02001026 if (reg >= noutput)
1027 continue;
1028
Marek Olšák8d03d922013-09-01 23:59:06 +02001029 /* Load the output as int. */
1030 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001031 out[j] = LLVMBuildBitCast(builder,
1032 outputs[reg].values[start+j],
1033 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001034 }
1035
1036 /* Pack the output. */
1037 LLVMValueRef vdata = NULL;
1038
1039 switch (num_comps) {
1040 case 1: /* as i32 */
1041 vdata = out[0];
1042 break;
1043 case 2: /* as v2i32 */
1044 case 3: /* as v4i32 (aligned to 4) */
1045 case 4: /* as v4i32 */
1046 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1047 for (j = 0; j < num_comps; j++) {
1048 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1049 LLVMConstInt(i32, j, 0), "");
1050 }
1051 break;
1052 }
1053
1054 build_streamout_store(shader, shader->so_buffers[buf_idx],
1055 vdata, num_comps,
1056 so_write_offset[buf_idx],
1057 LLVMConstInt(i32, 0, 0),
1058 so->output[i].dst_offset*4);
1059 }
1060 }
1061 lp_build_endif(&if_ctx);
1062}
1063
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001064
Michel Dänzer404b29d2013-11-21 16:45:28 +09001065/* Generate export instructions for hardware VS shader stage */
1066static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1067 struct si_shader_output_values *outputs,
1068 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001069{
1070 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001071 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001072 struct lp_build_context * base = &bld_base->base;
1073 struct lp_build_context * uint =
1074 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001075 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001076 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001077 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001078 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001079 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001080 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001081 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001082 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001083
Michel Dänzer67e385b2014-01-08 17:48:21 +09001084 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1085 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001086 }
1087
Michel Dänzer404b29d2013-11-21 16:45:28 +09001088 for (i = 0; i < noutput; i++) {
1089 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001090 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001091
Michel Dänzer0afeea52013-05-02 14:53:17 +02001092handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001093 /* Select the correct target */
1094 switch(semantic_name) {
1095 case TGSI_SEMANTIC_PSIZE:
1096 shader->vs_out_misc_write = true;
1097 shader->vs_out_point_size = true;
1098 psize_value = outputs[i].values[0];
1099 continue;
1100 case TGSI_SEMANTIC_EDGEFLAG:
1101 shader->vs_out_misc_write = true;
1102 shader->vs_out_edgeflag = true;
1103 edgeflag_value = outputs[i].values[0];
1104 continue;
1105 case TGSI_SEMANTIC_LAYER:
1106 shader->vs_out_misc_write = true;
1107 shader->vs_out_layer = true;
1108 layer_value = outputs[i].values[0];
1109 continue;
1110 case TGSI_SEMANTIC_POSITION:
1111 target = V_008DFC_SQ_EXP_POS;
1112 break;
1113 case TGSI_SEMANTIC_COLOR:
1114 case TGSI_SEMANTIC_BCOLOR:
1115 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001116 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001117 param_count++;
1118 break;
1119 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001120 shader->clip_dist_write |=
Marek Olšáka9592cd2014-10-04 19:09:09 +02001121 0xf << (semantic_index * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001122 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1123 break;
1124 case TGSI_SEMANTIC_CLIPVERTEX:
1125 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1126 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001127 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001128 case TGSI_SEMANTIC_FOG:
1129 case TGSI_SEMANTIC_GENERIC:
1130 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001131 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001132 param_count++;
1133 break;
1134 default:
1135 target = 0;
1136 fprintf(stderr,
1137 "Warning: SI unhandled vs output type:%d\n",
1138 semantic_name);
1139 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001140
Michel Dänzer404b29d2013-11-21 16:45:28 +09001141 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001142
Michel Dänzer404b29d2013-11-21 16:45:28 +09001143 if (target >= V_008DFC_SQ_EXP_POS &&
1144 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1145 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1146 args, sizeof(args));
1147 } else {
1148 lp_build_intrinsic(base->gallivm->builder,
1149 "llvm.SI.export",
1150 LLVMVoidTypeInContext(base->gallivm->context),
1151 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001152 }
1153
1154 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1155 semantic_name = TGSI_SEMANTIC_GENERIC;
1156 goto handle_semantic;
1157 }
1158 }
1159
1160 /* We need to add the position output manually if it's missing. */
1161 if (!pos_args[0][0]) {
1162 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1163 pos_args[0][1] = uint->zero; /* EXEC mask */
1164 pos_args[0][2] = uint->zero; /* last export? */
1165 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1166 pos_args[0][4] = uint->zero; /* COMPR flag */
1167 pos_args[0][5] = base->zero; /* X */
1168 pos_args[0][6] = base->zero; /* Y */
1169 pos_args[0][7] = base->zero; /* Z */
1170 pos_args[0][8] = base->one; /* W */
1171 }
1172
1173 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1174 if (shader->vs_out_misc_write) {
1175 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1176 shader->vs_out_point_size |
1177 (shader->vs_out_edgeflag << 1) |
1178 (shader->vs_out_layer << 2));
1179 pos_args[1][1] = uint->zero; /* EXEC mask */
1180 pos_args[1][2] = uint->zero; /* last export? */
1181 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1182 pos_args[1][4] = uint->zero; /* COMPR flag */
1183 pos_args[1][5] = base->zero; /* X */
1184 pos_args[1][6] = base->zero; /* Y */
1185 pos_args[1][7] = base->zero; /* Z */
1186 pos_args[1][8] = base->zero; /* W */
1187
Michel Dänzer404b29d2013-11-21 16:45:28 +09001188 if (shader->vs_out_point_size)
1189 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001190
1191 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001192 /* The output is a float, but the hw expects an integer
1193 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001194 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1195 edgeflag_value,
1196 bld_base->uint_bld.elem_type, "");
1197 edgeflag_value = lp_build_min(&bld_base->int_bld,
1198 edgeflag_value,
1199 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001200
1201 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001202 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1203 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001204 base->elem_type, "");
1205 }
1206
Michel Dänzer404b29d2013-11-21 16:45:28 +09001207 if (shader->vs_out_layer)
1208 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001209 }
1210
1211 for (i = 0; i < 4; i++)
1212 if (pos_args[i][0])
1213 shader->nr_pos_exports++;
1214
1215 pos_idx = 0;
1216 for (i = 0; i < 4; i++) {
1217 if (!pos_args[i][0])
1218 continue;
1219
1220 /* Specify the target we are exporting */
1221 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1222
1223 if (pos_idx == shader->nr_pos_exports)
1224 /* Specify that this is the last export */
1225 pos_args[i][2] = uint->one;
1226
1227 lp_build_intrinsic(base->gallivm->builder,
1228 "llvm.SI.export",
1229 LLVMVoidTypeInContext(base->gallivm->context),
1230 pos_args[i], 9);
1231 }
1232}
1233
Michel Dänzer404b29d2013-11-21 16:45:28 +09001234static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1235{
1236 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1237 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001238 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001239 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001240 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001241 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1242 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001243 LLVMValueRef t_list_ptr;
1244 LLVMValueRef t_list;
1245 unsigned chan;
1246 int i;
1247
Michel Dänzer404b29d2013-11-21 16:45:28 +09001248 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001249 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1250 SI_PARAM_RW_BUFFERS);
Marek Olšákd7876082014-09-26 23:06:32 +02001251 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001252 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001253
Marek Olšák216cf862014-10-04 17:04:05 +02001254 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001255 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001256 si_shader_ctx->radeon_bld.soa.outputs[i];
1257 int param_index = get_param_index(info->output_semantic_name[i],
1258 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001259 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001260
Marek Olšáke29353f2014-09-17 22:17:02 +02001261 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001262 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001263
1264 for (chan = 0; chan < 4; chan++) {
1265 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001266 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1267
1268 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001269 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001270 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001271 V_008F0C_BUF_DATA_FORMAT_32,
1272 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001273 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001274 }
1275 }
1276}
1277
1278static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1279{
1280 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1281 struct gallivm_state *gallivm = bld_base->base.gallivm;
1282 LLVMValueRef args[2];
1283
1284 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1285 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1286 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1287 LLVMVoidTypeInContext(gallivm->context), args, 2,
1288 LLVMNoUnwindAttribute);
1289}
1290
1291static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1292{
1293 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1294 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001295 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001296 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001297 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001298
Marek Olšákec0d1682014-10-04 22:33:36 +02001299 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001300
Marek Olšákec0d1682014-10-04 22:33:36 +02001301 for (i = 0; i < info->num_outputs; i++) {
1302 outputs[i].name = info->output_semantic_name[i];
1303 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001304
Marek Olšákec0d1682014-10-04 22:33:36 +02001305 for (j = 0; j < 4; j++)
1306 outputs[i].values[j] =
1307 LLVMBuildLoad(gallivm->builder,
1308 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1309 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001310 }
1311
Marek Olšákec0d1682014-10-04 22:33:36 +02001312 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001313 FREE(outputs);
1314}
1315
Michel Dänzer51f89a02013-12-09 15:33:53 +09001316static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1317{
1318 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001319 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001320 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001321 struct lp_build_context * uint = &bld_base->uint_bld;
1322 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001323 LLVMValueRef args[9];
1324 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001325 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001326 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001327
Marek Olšák75e97e22014-10-04 23:13:50 +02001328 for (i = 0; i < info->num_outputs; i++) {
1329 unsigned semantic_name = info->output_semantic_name[i];
1330 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001331 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001332
Marek Olšák75e97e22014-10-04 23:13:50 +02001333 /* Select the correct target */
1334 switch (semantic_name) {
1335 case TGSI_SEMANTIC_POSITION:
1336 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001337 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001338 case TGSI_SEMANTIC_STENCIL:
1339 stencil_index = i;
1340 continue;
1341 case TGSI_SEMANTIC_SAMPLEMASK:
1342 samplemask_index = i;
1343 continue;
1344 case TGSI_SEMANTIC_COLOR:
1345 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1346 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1347 LLVMBuildStore(bld_base->base.gallivm->builder,
1348 bld_base->base.one,
1349 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001350
Marek Olšák75e97e22014-10-04 23:13:50 +02001351 if (semantic_index == 0 &&
1352 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1353 si_alpha_test(bld_base,
1354 si_shader_ctx->radeon_bld.soa.outputs[i]);
1355 break;
1356 default:
1357 target = 0;
1358 fprintf(stderr,
1359 "Warning: SI unhandled fs output type:%d\n",
1360 semantic_name);
1361 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001362
Marek Olšák75e97e22014-10-04 23:13:50 +02001363 si_llvm_init_export_args_load(bld_base,
1364 si_shader_ctx->radeon_bld.soa.outputs[i],
1365 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001366
Marek Olšák75e97e22014-10-04 23:13:50 +02001367 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1368 /* If there is an export instruction waiting to be emitted, do so now. */
1369 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001370 lp_build_intrinsic(base->gallivm->builder,
1371 "llvm.SI.export",
1372 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001373 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001374 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001375
1376 /* This instruction will be emitted at the end of the shader. */
1377 memcpy(last_args, args, sizeof(args));
1378
1379 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1380 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1381 semantic_index == 0 &&
1382 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1383 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1384 si_llvm_init_export_args_load(bld_base,
1385 si_shader_ctx->radeon_bld.soa.outputs[i],
1386 V_008DFC_SQ_EXP_MRT + c, args);
1387 lp_build_intrinsic(base->gallivm->builder,
1388 "llvm.SI.export",
1389 LLVMVoidTypeInContext(base->gallivm->context),
1390 args, 9);
1391 }
1392 }
1393 } else {
1394 lp_build_intrinsic(base->gallivm->builder,
1395 "llvm.SI.export",
1396 LLVMVoidTypeInContext(base->gallivm->context),
1397 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001398 }
1399 }
1400
Marek Olšákd0e8b652014-05-06 20:04:31 +02001401 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001402 LLVMValueRef out_ptr;
1403 unsigned mask = 0;
1404
1405 /* Specify the target we are exporting */
1406 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1407
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001408 args[5] = base->zero; /* R, depth */
1409 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1410 args[7] = base->zero; /* B, sample mask */
1411 args[8] = base->zero; /* A, alpha to mask */
1412
Michel Dänzer1a616c12012-11-13 17:35:09 +01001413 if (depth_index >= 0) {
1414 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1415 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1416 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001417 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001418 }
1419
1420 if (stencil_index >= 0) {
1421 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001422 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001423 /* Only setting the stencil component bit (0x2) here
1424 * breaks some stencil piglit tests
1425 */
1426 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001427 si_shader_ctx->shader->db_shader_control |=
1428 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001429 }
1430
Marek Olšákd0e8b652014-05-06 20:04:31 +02001431 if (samplemask_index >= 0) {
1432 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1433 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1434 mask |= 0xf; /* Set all components. */
1435 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1436 }
1437
1438 if (samplemask_index >= 0)
1439 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1440 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001441 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1442 else
1443 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1444
Michel Dänzer1a616c12012-11-13 17:35:09 +01001445 /* Specify which components to enable */
1446 args[0] = lp_build_const_int32(base->gallivm, mask);
1447
1448 args[1] =
1449 args[2] =
1450 args[4] = uint->zero;
1451
1452 if (last_args[0])
1453 lp_build_intrinsic(base->gallivm->builder,
1454 "llvm.SI.export",
1455 LLVMVoidTypeInContext(base->gallivm->context),
1456 args, 9);
1457 else
1458 memcpy(last_args, args, sizeof(args));
1459 }
1460
Michel Dänzer51f89a02013-12-09 15:33:53 +09001461 if (!last_args[0]) {
1462 /* Specify which components to enable */
1463 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001464
Michel Dänzer51f89a02013-12-09 15:33:53 +09001465 /* Specify the target we are exporting */
1466 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001467
Michel Dänzer51f89a02013-12-09 15:33:53 +09001468 /* Set COMPR flag to zero to export data as 32-bit */
1469 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001470
Michel Dänzer51f89a02013-12-09 15:33:53 +09001471 /* dummy bits */
1472 last_args[5]= uint->zero;
1473 last_args[6]= uint->zero;
1474 last_args[7]= uint->zero;
1475 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001476 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001477
1478 /* Specify whether the EXEC mask represents the valid mask */
1479 last_args[1] = uint->one;
1480
1481 /* Specify that this is the last export */
1482 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1483
1484 lp_build_intrinsic(base->gallivm->builder,
1485 "llvm.SI.export",
1486 LLVMVoidTypeInContext(base->gallivm->context),
1487 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001488}
1489
Marek Olšák4855acd2013-08-06 15:08:54 +02001490static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1491 struct lp_build_tgsi_context * bld_base,
1492 struct lp_build_emit_data * emit_data);
1493
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001494static bool tgsi_is_shadow_sampler(unsigned target)
1495{
1496 return target == TGSI_TEXTURE_SHADOW1D ||
1497 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1498 target == TGSI_TEXTURE_SHADOW2D ||
1499 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1500 target == TGSI_TEXTURE_SHADOWCUBE ||
1501 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1502 target == TGSI_TEXTURE_SHADOWRECT;
1503}
1504
Marek Olšák877bb522014-06-25 03:12:46 +02001505static const struct lp_build_tgsi_action tex_action;
1506
Tom Stellarda75c6162012-01-06 17:38:37 -05001507static void tex_fetch_args(
1508 struct lp_build_tgsi_context * bld_base,
1509 struct lp_build_emit_data * emit_data)
1510{
Christian König55fe5cc2013-03-04 16:30:06 +01001511 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001512 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001513 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001514 unsigned opcode = inst->Instruction.Opcode;
1515 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001516 LLVMValueRef coords[4];
1517 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001518 int ref_pos;
1519 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001520 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001521 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001522 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1523 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001524 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001525
1526 if (target == TGSI_TEXTURE_BUFFER) {
1527 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1528 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1529 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1530 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1531
Marek Olšák4f3f0432014-07-07 22:49:15 +02001532 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001533 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1534 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1535 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1536 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1537
1538 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1539 emit_data->args[0] = res;
1540 emit_data->args[1] = bld_base->uint_bld.zero;
1541 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1542 emit_data->arg_count = 3;
1543 return;
1544 }
Tom Stellard467f5162012-05-16 15:15:35 -04001545
Michel Dänzer120efee2013-01-25 12:10:11 +01001546 /* Fetch and project texture coordinates */
1547 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001548 for (chan = 0; chan < 3; chan++ ) {
1549 coords[chan] = lp_build_emit_fetch(bld_base,
1550 emit_data->inst, 0,
1551 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001552 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001553 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1554 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001555 coords[chan],
1556 coords[3]);
1557 }
1558
Michel Dänzer120efee2013-01-25 12:10:11 +01001559 if (opcode == TGSI_OPCODE_TXP)
1560 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001561
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001562 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001563 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001564 /* The offsets are six-bit signed integers packed like this:
1565 * X=[5:0], Y=[13:8], and Z=[21:16].
1566 */
1567 LLVMValueRef offset[3], pack;
1568
1569 assert(inst->Texture.NumOffsets == 1);
1570
1571 for (chan = 0; chan < 3; chan++) {
1572 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1573 emit_data->inst, 0, chan);
1574 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1575 lp_build_const_int32(gallivm, 0x3f), "");
1576 if (chan)
1577 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1578 lp_build_const_int32(gallivm, chan*8), "");
1579 }
1580
1581 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1582 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1583 address[count++] = pack;
1584 }
1585
Michel Dänzer120efee2013-01-25 12:10:11 +01001586 /* Pack LOD bias value */
1587 if (opcode == TGSI_OPCODE_TXB)
1588 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001589 if (opcode == TGSI_OPCODE_TXB2)
1590 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001591
Michel Dänzer120efee2013-01-25 12:10:11 +01001592 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001593 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001594 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1595 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1596 } else {
1597 assert(ref_pos >= 0);
1598 address[count++] = coords[ref_pos];
1599 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001600 }
1601
Marek Olšákb279f0142014-07-04 03:19:58 +02001602 if (target == TGSI_TEXTURE_CUBE ||
1603 target == TGSI_TEXTURE_CUBE_ARRAY ||
1604 target == TGSI_TEXTURE_SHADOWCUBE ||
1605 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1606 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1607
Michel Dänzera6b83c02013-02-21 16:10:55 +01001608 /* Pack user derivatives */
1609 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001610 int num_deriv_channels, param;
1611
1612 switch (target) {
1613 case TGSI_TEXTURE_3D:
1614 num_deriv_channels = 3;
1615 break;
1616 case TGSI_TEXTURE_2D:
1617 case TGSI_TEXTURE_SHADOW2D:
1618 case TGSI_TEXTURE_RECT:
1619 case TGSI_TEXTURE_SHADOWRECT:
1620 case TGSI_TEXTURE_2D_ARRAY:
1621 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1622 case TGSI_TEXTURE_CUBE:
1623 case TGSI_TEXTURE_SHADOWCUBE:
1624 case TGSI_TEXTURE_CUBE_ARRAY:
1625 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1626 num_deriv_channels = 2;
1627 break;
1628 case TGSI_TEXTURE_1D:
1629 case TGSI_TEXTURE_SHADOW1D:
1630 case TGSI_TEXTURE_1D_ARRAY:
1631 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1632 num_deriv_channels = 1;
1633 break;
1634 default:
1635 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001636 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001637
1638 for (param = 1; param <= 2; param++)
1639 for (chan = 0; chan < num_deriv_channels; chan++)
1640 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001641 }
1642
Michel Dänzer120efee2013-01-25 12:10:11 +01001643 /* Pack texture coordinates */
1644 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001645 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001646 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001647 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001648 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001649
Marek Olšákd2bd6342013-09-18 15:40:21 +02001650 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001651 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001652 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001653 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001654 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001655
1656 if (count > 16) {
1657 assert(!"Cannot handle more than 16 texture address parameters");
1658 count = 16;
1659 }
1660
1661 for (chan = 0; chan < count; chan++ ) {
1662 address[chan] = LLVMBuildBitCast(gallivm->builder,
1663 address[chan],
1664 LLVMInt32TypeInContext(gallivm->context),
1665 "");
1666 }
1667
Marek Olšák4855acd2013-08-06 15:08:54 +02001668 /* Adjust the sample index according to FMASK.
1669 *
1670 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1671 * which is the identity mapping. Each nibble says which physical sample
1672 * should be fetched to get that sample.
1673 *
1674 * For example, 0x11111100 means there are only 2 samples stored and
1675 * the second sample covers 3/4 of the pixel. When reading samples 0
1676 * and 1, return physical sample 0 (determined by the first two 0s
1677 * in FMASK), otherwise return physical sample 1.
1678 *
1679 * The sample index should be adjusted as follows:
1680 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1681 */
1682 if (target == TGSI_TEXTURE_2D_MSAA ||
1683 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1684 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1685 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001686 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001687 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001688 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001689
Marek Olšákd2bd6342013-09-18 15:40:21 +02001690 memcpy(txf_address, address, sizeof(txf_address));
1691
1692 if (target == TGSI_TEXTURE_2D_MSAA) {
1693 txf_address[2] = bld_base->uint_bld.zero;
1694 }
1695 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001696
1697 /* Pad to a power-of-two size. */
1698 while (txf_count < util_next_power_of_two(txf_count))
1699 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1700
1701 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001702 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1703 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1704 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001705 txf_emit_data.chan = 0;
1706 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001707 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001708 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001709 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001710 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001711 txf_emit_data.arg_count = 3;
1712
Marek Olšák877bb522014-06-25 03:12:46 +02001713 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001714
1715 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001716 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1717 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1718
1719 /* Apply the formula. */
1720 LLVMValueRef fmask =
1721 LLVMBuildExtractElement(gallivm->builder,
1722 txf_emit_data.output[0],
1723 uint_bld->zero, "");
1724
Marek Olšákd2bd6342013-09-18 15:40:21 +02001725 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001726
1727 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001728 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001729
1730 LLVMValueRef shifted_fmask =
1731 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1732
1733 LLVMValueRef final_sample =
1734 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1735
1736 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1737 * resource descriptor is 0 (invalid),
1738 */
1739 LLVMValueRef fmask_desc =
1740 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001741 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001742 LLVMVectorType(uint_bld->elem_type, 8), "");
1743
1744 LLVMValueRef fmask_word1 =
1745 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1746 uint_bld->one, "");
1747
1748 LLVMValueRef word1_is_nonzero =
1749 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1750 fmask_word1, uint_bld->zero, "");
1751
Marek Olšákd2bd6342013-09-18 15:40:21 +02001752 /* Replace the MSAA sample index. */
1753 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001754 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001755 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001756 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001757
Michel Dänzer36231112013-05-02 09:44:45 +02001758 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001759 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001760
1761 if (opcode == TGSI_OPCODE_TXF) {
1762 /* add tex offsets */
1763 if (inst->Texture.NumOffsets) {
1764 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1765 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1766 const struct tgsi_texture_offset * off = inst->TexOffsets;
1767
1768 assert(inst->Texture.NumOffsets == 1);
1769
Marek Olšákdefedc02013-09-18 15:36:38 +02001770 switch (target) {
1771 case TGSI_TEXTURE_3D:
1772 address[2] = lp_build_add(uint_bld, address[2],
1773 bld->immediates[off->Index][off->SwizzleZ]);
1774 /* fall through */
1775 case TGSI_TEXTURE_2D:
1776 case TGSI_TEXTURE_SHADOW2D:
1777 case TGSI_TEXTURE_RECT:
1778 case TGSI_TEXTURE_SHADOWRECT:
1779 case TGSI_TEXTURE_2D_ARRAY:
1780 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001781 address[1] =
1782 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001783 bld->immediates[off->Index][off->SwizzleY]);
1784 /* fall through */
1785 case TGSI_TEXTURE_1D:
1786 case TGSI_TEXTURE_SHADOW1D:
1787 case TGSI_TEXTURE_1D_ARRAY:
1788 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1789 address[0] =
1790 lp_build_add(uint_bld, address[0],
1791 bld->immediates[off->Index][off->SwizzleX]);
1792 break;
1793 /* texture offsets do not apply to other texture targets */
1794 }
Michel Dänzer36231112013-05-02 09:44:45 +02001795 }
1796
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001797 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1798 emit_data->arg_count = 3;
1799
Michel Dänzer36231112013-05-02 09:44:45 +02001800 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001801 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001802 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001803 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001804 opcode == TGSI_OPCODE_LODQ ||
1805 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001806 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1807 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1808 target == TGSI_TEXTURE_2D_ARRAY ||
1809 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1810 target == TGSI_TEXTURE_CUBE_ARRAY ||
1811 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1812 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001813 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001814
Marek Olšák57f3da92014-06-16 16:53:42 +02001815 if (opcode == TGSI_OPCODE_TG4) {
1816 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001817
Marek Olšák57f3da92014-06-16 16:53:42 +02001818 /* DMASK was repurposed for GATHER4. 4 components are always
1819 * returned and DMASK works like a swizzle - it selects
1820 * the component to fetch. The only valid DMASK values are
1821 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1822 * (red,red,red,red) etc.) The ISA document doesn't mention
1823 * this.
1824 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001825
Marek Olšák57f3da92014-06-16 16:53:42 +02001826 /* Get the component index from src1.x for Gather4. */
1827 if (!tgsi_is_shadow_sampler(target)) {
1828 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1829 LLVMValueRef comp_imm;
1830 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001831
Marek Olšák57f3da92014-06-16 16:53:42 +02001832 assert(src1.File == TGSI_FILE_IMMEDIATE);
1833
1834 comp_imm = imms[src1.Index][src1.SwizzleX];
1835 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1836 gather_comp = CLAMP(gather_comp, 0, 3);
1837 }
1838
1839 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001840 }
1841
Marek Olšák4855acd2013-08-06 15:08:54 +02001842 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001843 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001844 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1845 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1846 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1847 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1848 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1849 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1850 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1851
1852 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001853
1854 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001855 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001856 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001857 } else {
1858 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1859 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001860 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001861
1862 emit_data->dst_type = LLVMVectorType(
1863 LLVMFloatTypeInContext(gallivm->context),
1864 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001865 }
1866
Marek Olšák2484daa2014-04-22 21:23:29 +02001867 /* The fetch opcode has been converted to a 2D array fetch.
1868 * This simplifies the LLVM backend. */
1869 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1870 target = TGSI_TEXTURE_2D_ARRAY;
1871 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1872 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1873
Michel Dänzer120efee2013-01-25 12:10:11 +01001874 /* Pad to power of two vector */
1875 while (count < util_next_power_of_two(count))
1876 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1877
Christian Königccf3e8f2013-03-26 15:09:27 +01001878 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001879}
1880
Michel Dänzer07eddc42013-02-06 15:43:10 +01001881static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1882 struct lp_build_tgsi_context * bld_base,
1883 struct lp_build_emit_data * emit_data)
1884{
1885 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001886 unsigned opcode = emit_data->inst->Instruction.Opcode;
1887 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001888 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001889 bool has_offset = HAVE_LLVM >= 0x0305 ?
1890 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001891
Marek Olšák877bb522014-06-25 03:12:46 +02001892 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001893 emit_data->output[emit_data->chan] = build_intrinsic(
1894 base->gallivm->builder,
1895 "llvm.SI.vs.load.input", emit_data->dst_type,
1896 emit_data->args, emit_data->arg_count,
1897 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1898 return;
1899 }
1900
Marek Olšák877bb522014-06-25 03:12:46 +02001901 if (opcode == TGSI_OPCODE_TG4 ||
1902 opcode == TGSI_OPCODE_LODQ ||
1903 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1904 bool is_shadow = tgsi_is_shadow_sampler(target);
1905 const char *name = "llvm.SI.image.sample";
1906 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001907
Marek Olšák877bb522014-06-25 03:12:46 +02001908 switch (opcode) {
1909 case TGSI_OPCODE_TEX:
1910 case TGSI_OPCODE_TEX2:
1911 case TGSI_OPCODE_TXP:
1912 break;
1913 case TGSI_OPCODE_TXB:
1914 case TGSI_OPCODE_TXB2:
1915 infix = ".b";
1916 break;
1917 case TGSI_OPCODE_TXL:
1918 case TGSI_OPCODE_TXL2:
1919 infix = ".l";
1920 break;
1921 case TGSI_OPCODE_TXD:
1922 infix = ".d";
1923 break;
1924 case TGSI_OPCODE_TG4:
1925 name = "llvm.SI.gather4";
1926 break;
1927 case TGSI_OPCODE_LODQ:
1928 name = "llvm.SI.getlod";
1929 is_shadow = false;
1930 has_offset = false;
1931 break;
1932 default:
1933 assert(0);
1934 return;
1935 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001936
Marek Olšák877bb522014-06-25 03:12:46 +02001937 /* Add the type and suffixes .c, .o if needed. */
1938 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1939 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1940 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001941
Marek Olšák877bb522014-06-25 03:12:46 +02001942 emit_data->output[emit_data->chan] = build_intrinsic(
1943 base->gallivm->builder, intr_name, emit_data->dst_type,
1944 emit_data->args, emit_data->arg_count,
1945 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1946 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001947 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001948 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001949
Marek Olšák877bb522014-06-25 03:12:46 +02001950 switch (opcode) {
1951 case TGSI_OPCODE_TEX:
1952 case TGSI_OPCODE_TEX2:
1953 case TGSI_OPCODE_TXP:
1954 name = "llvm.SI.sample";
1955 break;
1956 case TGSI_OPCODE_TXB:
1957 case TGSI_OPCODE_TXB2:
1958 name = "llvm.SI.sampleb";
1959 break;
1960 case TGSI_OPCODE_TXD:
1961 name = "llvm.SI.sampled";
1962 break;
1963 case TGSI_OPCODE_TXF:
1964 name = "llvm.SI.imageload";
1965 break;
1966 case TGSI_OPCODE_TXL:
1967 case TGSI_OPCODE_TXL2:
1968 name = "llvm.SI.samplel";
1969 break;
1970 default:
1971 assert(0);
1972 return;
1973 }
1974
Marek Olšákd859bdb2014-07-14 19:40:14 +02001975 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1976 v16i8 = LLVMVectorType(i8, 16);
1977 v32i8 = LLVMVectorType(i8, 32);
1978
1979 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1980 emit_data->args[1], v32i8, "");
1981 if (opcode != TGSI_OPCODE_TXF) {
1982 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1983 emit_data->args[2], v16i8, "");
1984 }
1985
Marek Olšák877bb522014-06-25 03:12:46 +02001986 sprintf(intr_name, "%s.v%ui32", name,
1987 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1988
1989 emit_data->output[emit_data->chan] = build_intrinsic(
1990 base->gallivm->builder, intr_name, emit_data->dst_type,
1991 emit_data->args, emit_data->arg_count,
1992 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1993 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001994}
1995
Michel Dänzer0495adb2013-05-06 12:45:14 +02001996static void txq_fetch_args(
1997 struct lp_build_tgsi_context * bld_base,
1998 struct lp_build_emit_data * emit_data)
1999{
2000 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2001 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002002 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002003 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002004
Marek Olšák2484daa2014-04-22 21:23:29 +02002005 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002006 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2007 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2008
2009 /* Read the size from the buffer descriptor directly. */
2010 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2011 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2012 size = LLVMBuildExtractElement(gallivm->builder, size,
2013 lp_build_const_int32(gallivm, 2), "");
2014 emit_data->args[0] = size;
2015 return;
2016 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002017
2018 /* Mip level */
2019 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2020
2021 /* Resource */
2022 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2023
Marek Olšák2484daa2014-04-22 21:23:29 +02002024 /* Texture target */
2025 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2026 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2027 target = TGSI_TEXTURE_2D_ARRAY;
2028
Michel Dänzer0495adb2013-05-06 12:45:14 +02002029 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002030 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002031
2032 emit_data->arg_count = 3;
2033
2034 emit_data->dst_type = LLVMVectorType(
2035 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2036 4);
2037}
2038
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002039static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2040 struct lp_build_tgsi_context * bld_base,
2041 struct lp_build_emit_data * emit_data)
2042{
Marek Olšák2484daa2014-04-22 21:23:29 +02002043 unsigned target = emit_data->inst->Texture.Texture;
2044
2045 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002046 /* Just return the buffer size. */
2047 emit_data->output[emit_data->chan] = emit_data->args[0];
2048 return;
2049 }
2050
2051 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002052
2053 /* Divide the number of layers by 6 to get the number of cubes. */
2054 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2055 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2056 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2057 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2058 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2059
2060 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2061 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2062 z = LLVMBuildSDiv(builder, z, six, "");
2063
2064 emit_data->output[emit_data->chan] =
2065 LLVMBuildInsertElement(builder, v4, z, two, "");
2066 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002067}
2068
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002069static void si_llvm_emit_ddxy(
2070 const struct lp_build_tgsi_action * action,
2071 struct lp_build_tgsi_context * bld_base,
2072 struct lp_build_emit_data * emit_data)
2073{
2074 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2075 struct gallivm_state *gallivm = bld_base->base.gallivm;
2076 struct lp_build_context * base = &bld_base->base;
2077 const struct tgsi_full_instruction *inst = emit_data->inst;
2078 unsigned opcode = inst->Instruction.Opcode;
2079 LLVMValueRef indices[2];
2080 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2081 LLVMValueRef tl, trbl, result[4];
2082 LLVMTypeRef i32;
2083 unsigned swizzle[4];
2084 unsigned c;
2085
2086 i32 = LLVMInt32TypeInContext(gallivm->context);
2087
2088 indices[0] = bld_base->uint_bld.zero;
2089 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2090 NULL, 0, LLVMReadNoneAttribute);
2091 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2092 indices, 2, "");
2093
2094 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2095 lp_build_const_int32(gallivm, 0xfffffffc), "");
2096 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2097 indices, 2, "");
2098
2099 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2100 lp_build_const_int32(gallivm,
2101 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2102 "");
2103 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2104 indices, 2, "");
2105
2106 for (c = 0; c < 4; ++c) {
2107 unsigned i;
2108
2109 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2110 for (i = 0; i < c; ++i) {
2111 if (swizzle[i] == swizzle[c]) {
2112 result[c] = result[i];
2113 break;
2114 }
2115 }
2116 if (i != c)
2117 continue;
2118
2119 LLVMBuildStore(gallivm->builder,
2120 LLVMBuildBitCast(gallivm->builder,
2121 lp_build_emit_fetch(bld_base, inst, 0, c),
2122 i32, ""),
2123 store_ptr);
2124
2125 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2126 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2127
2128 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2129 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2130
2131 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2132 }
2133
2134 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2135}
2136
Michel Dänzer404b29d2013-11-21 16:45:28 +09002137/* Emit one vertex from the geometry shader */
2138static void si_llvm_emit_vertex(
2139 const struct lp_build_tgsi_action *action,
2140 struct lp_build_tgsi_context *bld_base,
2141 struct lp_build_emit_data *emit_data)
2142{
2143 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002144 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002145 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002146 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002147 struct gallivm_state *gallivm = bld_base->base.gallivm;
2148 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002149 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2150 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002151 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002152 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002153 LLVMValueRef t_list_ptr;
2154 LLVMValueRef t_list;
2155 LLVMValueRef args[2];
2156 unsigned chan;
2157 int i;
2158
2159 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002160 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2161 SI_PARAM_RW_BUFFERS);
Marek Olšákd7876082014-09-26 23:06:32 +02002162 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002163 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002164
Michel Dänzer404b29d2013-11-21 16:45:28 +09002165 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002166 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002167
2168 /* If this thread has already emitted the declared maximum number of
2169 * vertices, kill it: excessive vertex emissions are not supposed to
2170 * have any effect, and GS threads have no externally observable
2171 * effects other than emitting vertices.
2172 */
2173 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2174 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002175 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002176 kill = lp_build_select(&bld_base->base, can_emit,
2177 lp_build_const_float(gallivm, 1.0f),
2178 lp_build_const_float(gallivm, -1.0f));
2179 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2180 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2181
Marek Olšák02134cf2014-10-04 22:07:50 +02002182 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002183 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002184 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002185
2186 for (chan = 0; chan < 4; chan++) {
2187 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002188 LLVMValueRef voffset =
2189 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002190 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002191
2192 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2193 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002194
2195 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2196
2197 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2198 voffset, soffset, 0,
2199 V_008F0C_BUF_DATA_FORMAT_32,
2200 V_008F0C_BUF_NUM_FORMAT_UINT,
2201 1, 0, 1, 1, 0);
2202 }
2203 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002204 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2205 lp_build_const_int32(gallivm, 1));
2206 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002207
2208 /* Signal vertex emission */
2209 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2210 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2211 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2212 LLVMVoidTypeInContext(gallivm->context), args, 2,
2213 LLVMNoUnwindAttribute);
2214}
2215
2216/* Cut one primitive from the geometry shader */
2217static void si_llvm_emit_primitive(
2218 const struct lp_build_tgsi_action *action,
2219 struct lp_build_tgsi_context *bld_base,
2220 struct lp_build_emit_data *emit_data)
2221{
2222 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2223 struct gallivm_state *gallivm = bld_base->base.gallivm;
2224 LLVMValueRef args[2];
2225
2226 /* Signal primitive cut */
2227 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2228 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2229 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2230 LLVMVoidTypeInContext(gallivm->context), args, 2,
2231 LLVMNoUnwindAttribute);
2232}
2233
Tom Stellarda75c6162012-01-06 17:38:37 -05002234static const struct lp_build_tgsi_action tex_action = {
2235 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002236 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002237};
2238
Michel Dänzer0495adb2013-05-06 12:45:14 +02002239static const struct lp_build_tgsi_action txq_action = {
2240 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002241 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002242 .intr_name = "llvm.SI.resinfo"
2243};
2244
Christian König206f0592013-03-20 14:37:21 +01002245static void create_meta_data(struct si_shader_context *si_shader_ctx)
2246{
2247 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2248 LLVMValueRef args[3];
2249
2250 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2251 args[1] = 0;
2252 args[2] = lp_build_const_int32(gallivm, 1);
2253
2254 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2255}
2256
Marek Olšák4f3f0432014-07-07 22:49:15 +02002257static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2258{
2259 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2260 CONST_ADDR_SPACE);
2261}
2262
Christian König55fe5cc2013-03-04 16:30:06 +01002263static void create_function(struct si_shader_context *si_shader_ctx)
2264{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002265 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2266 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002267 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002268 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002269 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002270
Christian König55fe5cc2013-03-04 16:30:06 +01002271 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002272 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002273 f32 = LLVMFloatTypeInContext(gallivm->context);
2274 v2i32 = LLVMVectorType(i32, 2);
2275 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002276 v4i32 = LLVMVectorType(i32, 4);
2277 v8i32 = LLVMVectorType(i32, 8);
2278 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002279
Marek Olšákee2a8182014-07-07 23:27:19 +02002280 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002281 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002282 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2283 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002284 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002285
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002286 switch (si_shader_ctx->type) {
2287 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002288 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002289 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002290 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002291 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002292 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002293
Michel Dänzer404b29d2013-11-21 16:45:28 +09002294 if (shader->key.vs.as_es) {
2295 params[SI_PARAM_ES2GS_OFFSET] = i32;
2296 num_params++;
2297 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002298 if (shader->is_gs_copy_shader) {
2299 last_array_pointer = SI_PARAM_CONST;
2300 num_params = SI_PARAM_CONST+1;
2301 }
2302
Michel Dänzer404b29d2013-11-21 16:45:28 +09002303 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002305 /* Streamout SGPRs. */
2306 if (shader->selector->so.num_outputs) {
2307 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2308 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2309 }
2310 /* A streamout buffer offset is loaded if the stride is non-zero. */
2311 for (i = 0; i < 4; i++) {
2312 if (!shader->selector->so.stride[i])
2313 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002314
Michel Dänzer404b29d2013-11-21 16:45:28 +09002315 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2316 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002317 }
2318
2319 last_sgpr = num_params-1;
2320
2321 /* VGPRs */
2322 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2323 params[num_params++] = i32; /* unused*/
2324 params[num_params++] = i32; /* unused */
2325 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002326 break;
Christian König0666ffd2013-03-05 15:07:39 +01002327
Michel Dänzer404b29d2013-11-21 16:45:28 +09002328 case TGSI_PROCESSOR_GEOMETRY:
2329 params[SI_PARAM_GS2VS_OFFSET] = i32;
2330 params[SI_PARAM_GS_WAVE_ID] = i32;
2331 last_sgpr = SI_PARAM_GS_WAVE_ID;
2332
2333 /* VGPRs */
2334 params[SI_PARAM_VTX0_OFFSET] = i32;
2335 params[SI_PARAM_VTX1_OFFSET] = i32;
2336 params[SI_PARAM_PRIMITIVE_ID] = i32;
2337 params[SI_PARAM_VTX2_OFFSET] = i32;
2338 params[SI_PARAM_VTX3_OFFSET] = i32;
2339 params[SI_PARAM_VTX4_OFFSET] = i32;
2340 params[SI_PARAM_VTX5_OFFSET] = i32;
2341 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2342 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2343 break;
2344
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002345 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002346 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002347 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002348 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002349 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2350 params[SI_PARAM_PERSP_CENTER] = v2i32;
2351 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2352 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2353 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2354 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2355 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2356 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2357 params[SI_PARAM_POS_X_FLOAT] = f32;
2358 params[SI_PARAM_POS_Y_FLOAT] = f32;
2359 params[SI_PARAM_POS_Z_FLOAT] = f32;
2360 params[SI_PARAM_POS_W_FLOAT] = f32;
2361 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002362 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002363 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2364 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002365 num_params = SI_PARAM_POS_FIXED_PT+1;
2366 break;
2367
2368 default:
2369 assert(0 && "unimplemented shader");
2370 return;
Christian Königc4973212013-03-05 12:14:02 +01002371 }
Christian König55fe5cc2013-03-04 16:30:06 +01002372
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002373 assert(num_params <= Elements(params));
2374 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002375 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002376
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002377 for (i = 0; i <= last_sgpr; ++i) {
2378 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002379
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002380 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2381 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002382 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002383 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002384 else
2385 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002386 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002387
Michel Dänzer404b29d2013-11-21 16:45:28 +09002388 if (bld_base->info &&
2389 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2390 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002391 si_shader_ctx->ddxy_lds =
2392 LLVMAddGlobalInAddressSpace(gallivm->module,
2393 LLVMArrayType(i32, 64),
2394 "ddxy_lds",
2395 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002396}
Tom Stellarda75c6162012-01-06 17:38:37 -05002397
Christian König0f6cf2b2013-03-15 15:53:25 +01002398static void preload_constants(struct si_shader_context *si_shader_ctx)
2399{
2400 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2401 struct gallivm_state * gallivm = bld_base->base.gallivm;
2402 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002403 unsigned buf;
2404 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002405
Marek Olšákee2a8182014-07-07 23:27:19 +02002406 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002407 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002408
Marek Olšák2fd42002013-10-25 11:45:47 +02002409 if (num_const == 0)
2410 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002411
Marek Olšák2fd42002013-10-25 11:45:47 +02002412 /* Allocate space for the constant values */
2413 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002414
Marek Olšák2fd42002013-10-25 11:45:47 +02002415 /* Load the resource descriptor */
2416 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002417 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002418
Marek Olšák2fd42002013-10-25 11:45:47 +02002419 /* Load the constants, we rely on the code sinking to do the rest */
2420 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002421 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002422 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002423 si_shader_ctx->const_resource[buf],
2424 lp_build_const_int32(gallivm, i * 4),
2425 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002426 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002427 }
2428}
2429
Christian König1c100182013-03-17 16:02:42 +01002430static void preload_samplers(struct si_shader_context *si_shader_ctx)
2431{
2432 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2433 struct gallivm_state * gallivm = bld_base->base.gallivm;
2434 const struct tgsi_shader_info * info = bld_base->info;
2435
2436 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2437
2438 LLVMValueRef res_ptr, samp_ptr;
2439 LLVMValueRef offset;
2440
2441 if (num_samplers == 0)
2442 return;
2443
Christian König1c100182013-03-17 16:02:42 +01002444 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2445 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2446
2447 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2448 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002449 /* Resource */
2450 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002451 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002452
2453 /* Sampler */
2454 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002455 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002456
2457 /* FMASK resource */
2458 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002459 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2460 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002461 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002462 }
Christian König1c100182013-03-17 16:02:42 +01002463 }
2464}
2465
Marek Olšák8d03d922013-09-01 23:59:06 +02002466static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2467{
2468 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2469 struct gallivm_state * gallivm = bld_base->base.gallivm;
2470 unsigned i;
2471
Michel Dänzer67e385b2014-01-08 17:48:21 +09002472 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2473 si_shader_ctx->shader->key.vs.as_es ||
2474 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002475 return;
2476
2477 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002478 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002479
2480 /* Load the resources, we rely on the code sinking to do the rest */
2481 for (i = 0; i < 4; ++i) {
2482 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002483 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002484 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002485
Marek Olšákd7876082014-09-26 23:06:32 +02002486 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002487 }
2488 }
2489}
2490
Marek Olšák1abb1a92014-09-17 22:44:22 +02002491int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2492 LLVMModuleRef mod)
Tom Stellard302f53d2012-10-25 13:50:10 -04002493{
Darren Powellbc866902014-03-31 18:00:28 -04002494 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002495 unsigned i;
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002496 unsigned char *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002497 struct radeon_shader_binary binary;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002498 bool dump = r600_can_dump_shader(&sscreen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002499 shader->selector ? shader->selector->tokens : NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002500 const char * gpu_family = r600_get_llvm_processor_name(sscreen->b.family);
Tom Stellard9ba31052014-07-14 16:49:08 -04002501 unsigned code_size;
Darren Powellbc866902014-03-31 18:00:28 -04002502
2503 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002504 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002505 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2506
2507 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002508 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002509 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002510 for (i = 0; i < binary.code_size; i+=4 ) {
2511 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2512 binary.code[i + 2], binary.code[i + 1],
2513 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002514 }
2515 }
2516
Tom Stellardd50343d2013-04-04 16:21:06 -04002517 /* XXX: We may be able to emit some of these values directly rather than
2518 * extracting fields to be emitted later.
2519 */
Darren Powellbc866902014-03-31 18:00:28 -04002520 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002521 for (i = 0; i < binary.config_size; i+= 8) {
2522 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2523 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2524 switch (reg) {
2525 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2526 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2527 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2528 case R_00B848_COMPUTE_PGM_RSRC1:
2529 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2530 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2531 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002532 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2533 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2534 break;
2535 case R_00B84C_COMPUTE_PGM_RSRC2:
2536 shader->lds_size = G_00B84C_LDS_SIZE(value);
2537 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002538 case R_0286CC_SPI_PS_INPUT_ENA:
2539 shader->spi_ps_input_ena = value;
2540 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002541 case R_00B860_COMPUTE_TMPRING_SIZE:
2542 /* WAVESIZE is in units of 256 dwords. */
2543 shader->scratch_bytes_per_wave =
2544 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2545 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002546 default:
2547 fprintf(stderr, "Warning: Compiler emitted unknown "
2548 "config register: 0x%x\n", reg);
2549 break;
2550 }
2551 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002552
2553 /* copy new shader */
Tom Stellard9ba31052014-07-14 16:49:08 -04002554 code_size = binary.code_size + binary.rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002555 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002556 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002557 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002558 if (shader->bo == NULL) {
2559 return -ENOMEM;
2560 }
2561
Marek Olšák1abb1a92014-09-17 22:44:22 +02002562 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_WRITE);
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002563 util_memcpy_cpu_to_le32(ptr, binary.code, binary.code_size);
2564 if (binary.rodata_size > 0) {
2565 ptr += binary.code_size;
2566 util_memcpy_cpu_to_le32(ptr, binary.rodata, binary.rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002567 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002568
Marek Olšák1abb1a92014-09-17 22:44:22 +02002569 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002570
Tom Stellard7782d192013-04-04 09:57:13 -07002571 free(binary.code);
2572 free(binary.config);
Tom Stellard9ba31052014-07-14 16:49:08 -04002573 free(binary.rodata);
Tom Stellard302f53d2012-10-25 13:50:10 -04002574
Darren Powellbc866902014-03-31 18:00:28 -04002575 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002576}
2577
Michel Dänzer404b29d2013-11-21 16:45:28 +09002578/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002579static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002580 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002581 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002582{
2583 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2584 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2585 struct lp_build_context *base = &bld_base->base;
2586 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002587 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002588 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002589 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002590 LLVMValueRef t_list_ptr, t_list;
2591 LLVMValueRef args[9];
2592 int i, r;
2593
Marek Olšák02134cf2014-10-04 22:07:50 +02002594 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002595
2596 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002597 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002598
2599 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2600
2601 create_meta_data(si_shader_ctx);
2602 create_function(si_shader_ctx);
2603 preload_streamout_buffers(si_shader_ctx);
2604
2605 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002606 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2607 SI_PARAM_RW_BUFFERS);
Marek Olšákd7876082014-09-26 23:06:32 +02002608 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002609 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002610
2611 args[0] = t_list;
2612 args[1] = lp_build_mul_imm(uint,
2613 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2614 si_shader_ctx->param_vertex_id),
2615 4);
2616 args[3] = uint->zero;
2617 args[4] = uint->one; /* OFFEN */
2618 args[5] = uint->zero; /* IDXEN */
2619 args[6] = uint->one; /* GLC */
2620 args[7] = uint->one; /* SLC */
2621 args[8] = uint->zero; /* TFE */
2622
2623 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002624 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002625 unsigned chan;
2626
Marek Olšák02134cf2014-10-04 22:07:50 +02002627 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002628 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002629
2630 for (chan = 0; chan < 4; chan++) {
2631 args[2] = lp_build_const_int32(gallivm,
2632 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002633 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002634
2635 outputs[i].values[chan] =
2636 LLVMBuildBitCast(gallivm->builder,
2637 build_intrinsic(gallivm->builder,
2638 "llvm.SI.buffer.load.dword.i32.i32",
2639 LLVMInt32TypeInContext(gallivm->context),
2640 args, 9,
2641 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2642 base->elem_type, "");
2643 }
2644 }
2645
Marek Olšák02134cf2014-10-04 22:07:50 +02002646 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002647
2648 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2649
2650 if (dump)
2651 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2652
Marek Olšák1abb1a92014-09-17 22:44:22 +02002653 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002654 bld_base->base.gallivm->module);
2655
2656 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2657
2658 FREE(outputs);
2659 return r;
2660}
2661
Marek Olšák1abb1a92014-09-17 22:44:22 +02002662int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002663{
Marek Olšák2774abd2014-09-16 18:45:33 +02002664 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002665 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002666 struct lp_build_tgsi_context * bld_base;
2667 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002668 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002669 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002670
2671 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2672 * conversion fails. */
2673 if (dump) {
2674 tgsi_dump(sel->tokens, 0);
2675 si_dump_streamout(&sel->so);
2676 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002677
Marek Olšák8c37c162014-09-16 18:40:07 +02002678 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002679
Michel Dänzercfebaf92012-08-31 19:04:08 +02002680 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002681 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2682 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2683
Marek Olšák52335682014-09-30 15:12:09 +02002684 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002685 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2686
Marek Olšák52335682014-09-30 15:12:09 +02002687 shader->uses_instanceid = sel->info.uses_instanceid;
2688 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002689 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002690
2691 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002692 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002693 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2694 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002695 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002696 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2697 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2698 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002699 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002700 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002701 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2702 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002703
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002704 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2705 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002706
Michel Dänzer404b29d2013-11-21 16:45:28 +09002707 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2708 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2709
Christian Könige4ed5872013-03-21 18:02:52 +01002710 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002711 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002712 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2713 si_shader_ctx.shader = shader;
2714 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002715
Michel Dänzer51f89a02013-12-09 15:33:53 +09002716 switch (si_shader_ctx.type) {
2717 case TGSI_PROCESSOR_VERTEX:
2718 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002719 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002720 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2721 } else {
2722 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2723 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002724 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002725 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002726 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2727 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002728 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002729 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002730 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2731 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002732
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002733 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002734 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2735 shader->db_shader_control |=
2736 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2737 break;
2738 case TGSI_FS_DEPTH_LAYOUT_LESS:
2739 shader->db_shader_control |=
2740 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2741 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002742 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002743 break;
2744 default:
2745 assert(!"Unsupported shader type");
2746 return -1;
2747 }
2748
Christian König206f0592013-03-20 14:37:21 +01002749 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002750 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002751 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002752 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002753 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002754
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002755 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2756 si_shader_ctx.gs_next_vertex =
2757 lp_build_alloca(bld_base->base.gallivm,
2758 bld_base->uint_bld.elem_type, "");
2759 }
2760
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002761 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002762 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002763 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002764 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002765
2766 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2767
2768 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002769 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002770 if (r) {
2771 fprintf(stderr, "LLVM failed to compile shader\n");
2772 goto out;
2773 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002774
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002775 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002776
2777 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002778 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002779 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002780 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002781 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002782 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2783 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002784 free(shader->gs_copy_shader);
2785 shader->gs_copy_shader = NULL;
2786 goto out;
2787 }
2788 }
2789
Tom Stellarda75c6162012-01-06 17:38:37 -05002790 tgsi_parse_free(&si_shader_ctx.parse);
2791
Michel Dänzer404b29d2013-11-21 16:45:28 +09002792out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002793 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002794 FREE(si_shader_ctx.constants[i]);
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}