blob: 169449054add6e7aa4f6bedcc5c1229212e89d61 [file] [log] [blame]
Christian Königce40e472012-08-02 12:14:59 +02001/*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
27 */
28
Tom Stellarda75c6162012-01-06 17:38:37 -050029#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020030#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050031#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010032#include "gallivm/lp_bld_logic.h"
Christian König5e616cf2013-03-07 11:58:56 +010033#include "gallivm/lp_bld_arit.h"
Marek Olšák8d03d922013-09-01 23:59:06 +020034#include "gallivm/lp_bld_flow.h"
Emil Velikova1312632014-08-16 17:58:25 +010035#include "radeon/radeon_llvm.h"
Tom Stellard1f4e48d2014-09-25 18:11:24 -070036#include "radeon/radeon_elf_util.h"
Emil Velikova1312632014-08-16 17:58:25 +010037#include "radeon/radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010038#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050039#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010040#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050041#include "tgsi/tgsi_dump.h"
42
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010043#include "si_pipe.h"
44#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050045#include "sid.h"
46
Tom Stellarda75c6162012-01-06 17:38:37 -050047#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050048
Michel Dänzer404b29d2013-11-21 16:45:28 +090049struct si_shader_output_values
50{
51 LLVMValueRef values[4];
52 unsigned name;
Michel Dänzer67e385b2014-01-08 17:48:21 +090053 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090054};
55
Tom Stellarda75c6162012-01-06 17:38:37 -050056struct si_shader_context
57{
58 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050059 struct tgsi_parse_context parse;
60 struct tgsi_token * tokens;
Marek Olšák8c37c162014-09-16 18:40:07 +020061 struct si_shader *shader;
Marek Olšák368b0a72014-12-28 21:51:35 +010062 struct si_screen *screen;
Tom Stellarda75c6162012-01-06 17:38:37 -050063 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020064 int param_streamout_config;
65 int param_streamout_write_index;
66 int param_streamout_offset[4];
67 int param_vertex_id;
68 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010069 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020070 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020071 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020072 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Marek Olšák55a9b772014-10-05 12:38:54 +020073 LLVMValueRef resources[SI_NUM_SAMPLER_VIEWS];
74 LLVMValueRef samplers[SI_NUM_SAMPLER_STATES];
Marek Olšák8d03d922013-09-01 23:59:06 +020075 LLVMValueRef so_buffers[4];
Marek Olšákfc3b3352014-10-05 13:33:40 +020076 LLVMValueRef esgs_ring;
77 LLVMValueRef gsvs_ring;
Michel Dänzerf07a96d2014-01-08 18:45:10 +090078 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050079};
80
81static struct si_shader_context * si_shader_context(
82 struct lp_build_tgsi_context * bld_base)
83{
84 return (struct si_shader_context *)bld_base;
85}
86
87
88#define PERSPECTIVE_BASE 0
89#define LINEAR_BASE 9
90
91#define SAMPLE_OFFSET 0
92#define CENTER_OFFSET 2
93#define CENTROID_OFSET 4
94
95#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040096#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020097#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040098#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050099
Michel Dänzer404b29d2013-11-21 16:45:28 +0900100
101#define SENDMSG_GS 2
102#define SENDMSG_GS_DONE 3
103
104#define SENDMSG_GS_OP_NOP (0 << 4)
105#define SENDMSG_GS_OP_CUT (1 << 4)
106#define SENDMSG_GS_OP_EMIT (2 << 4)
107#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
108
Marek Olšáke29353f2014-09-17 22:17:02 +0200109/**
110 * Returns a unique index for a semantic name and index. The index must be
111 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
112 * calculated.
113 */
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200114unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
Marek Olšáke29353f2014-09-17 22:17:02 +0200115{
116 switch (semantic_name) {
117 case TGSI_SEMANTIC_POSITION:
118 return 0;
119 case TGSI_SEMANTIC_PSIZE:
120 return 1;
121 case TGSI_SEMANTIC_CLIPDIST:
122 assert(index <= 1);
123 return 2 + index;
124 case TGSI_SEMANTIC_CLIPVERTEX:
125 return 4;
126 case TGSI_SEMANTIC_COLOR:
127 assert(index <= 1);
128 return 5 + index;
129 case TGSI_SEMANTIC_BCOLOR:
130 assert(index <= 1);
131 return 7 + index;
132 case TGSI_SEMANTIC_FOG:
133 return 9;
134 case TGSI_SEMANTIC_EDGEFLAG:
135 return 10;
136 case TGSI_SEMANTIC_GENERIC:
137 assert(index <= 63-11);
138 return 11 + index;
139 default:
140 assert(0);
141 return 63;
142 }
143}
144
145/**
146 * Given a semantic name and index of a parameter and a mask of used parameters
147 * (inputs or outputs), return the index of the parameter in the list of all
148 * used parameters.
149 *
150 * For example, assume this list of parameters:
151 * POSITION, PSIZE, GENERIC0, GENERIC2
152 * which has the mask:
153 * 11000000000101
154 * Then:
155 * querying POSITION returns 0,
156 * querying PSIZE returns 1,
157 * querying GENERIC0 returns 2,
158 * querying GENERIC2 returns 3.
159 *
160 * Which can be used as an offset to a parameter buffer in units of vec4s.
161 */
162static int get_param_index(unsigned semantic_name, unsigned index,
163 uint64_t mask)
164{
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200165 unsigned unique_index = si_shader_io_get_unique_index(semantic_name, index);
Marek Olšáke29353f2014-09-17 22:17:02 +0200166 int i, param_index = 0;
167
168 /* If not present... */
169 if (!((1llu << unique_index) & mask))
170 return -1;
171
172 for (i = 0; mask; i++) {
173 uint64_t bit = 1llu << i;
174
175 if (bit & mask) {
176 if (i == unique_index)
177 return param_index;
178
179 mask &= ~bit;
180 param_index++;
181 }
182 }
183
184 assert(!"unreachable");
185 return -1;
186}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900187
Tom Stellard467f5162012-05-16 15:15:35 -0400188/**
Marek Olšákd7876082014-09-26 23:06:32 +0200189 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
190 * It's equivalent to doing a load from &base_ptr[index].
Tom Stellard467f5162012-05-16 15:15:35 -0400191 *
Marek Olšákd7876082014-09-26 23:06:32 +0200192 * \param base_ptr Where the array starts.
193 * \param index The element index into the array.
Tom Stellard467f5162012-05-16 15:15:35 -0400194 */
Marek Olšákd7876082014-09-26 23:06:32 +0200195static LLVMValueRef build_indexed_load(struct si_shader_context *si_shader_ctx,
196 LLVMValueRef base_ptr, LLVMValueRef index)
Tom Stellard467f5162012-05-16 15:15:35 -0400197{
Marek Olšákd7876082014-09-26 23:06:32 +0200198 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
199 struct gallivm_state *gallivm = bld_base->base.gallivm;
200 LLVMValueRef indices[2], pointer;
Tom Stellard467f5162012-05-16 15:15:35 -0400201
Marek Olšákd7876082014-09-26 23:06:32 +0200202 indices[0] = bld_base->uint_bld.zero;
203 indices[1] = index;
Christian König206f0592013-03-20 14:37:21 +0100204
Marek Olšákd7876082014-09-26 23:06:32 +0200205 pointer = LLVMBuildGEP(gallivm->builder, base_ptr, indices, 2, "");
206 return LLVMBuildLoad(gallivm->builder, pointer, "");
207}
208
209/**
210 * Do a load from &base_ptr[index], but also add a flag that it's loading
211 * a constant.
212 */
213static LLVMValueRef build_indexed_load_const(
214 struct si_shader_context * si_shader_ctx,
215 LLVMValueRef base_ptr, LLVMValueRef index)
216{
217 LLVMValueRef result = build_indexed_load(si_shader_ctx, base_ptr, index);
Christian König206f0592013-03-20 14:37:21 +0100218 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
219 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400220}
221
Marek Olšákf317ce52013-09-05 15:39:57 +0200222static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100223 struct radeon_llvm_context * radeon_bld,
224 unsigned divisor)
225{
Marek Olšák8d03d922013-09-01 23:59:06 +0200226 struct si_shader_context *si_shader_ctx =
227 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100228 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
229
Marek Olšák8d03d922013-09-01 23:59:06 +0200230 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
231 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100232 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
233 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
234
235 if (divisor > 1)
236 result = LLVMBuildUDiv(gallivm->builder, result,
237 lp_build_const_int32(gallivm, divisor), "");
238
239 return result;
240}
241
Tom Stellarda75c6162012-01-06 17:38:37 -0500242static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900243 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500244 unsigned input_index,
245 const struct tgsi_full_declaration *decl)
246{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900247 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
248 struct gallivm_state *gallivm = base->gallivm;
249 struct si_shader_context *si_shader_ctx =
250 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100251 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
252
253 unsigned chan;
254
Tom Stellarda75c6162012-01-06 17:38:37 -0500255 LLVMValueRef t_list_ptr;
256 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400257 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500258 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100259 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400260 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500261 LLVMTypeRef vec4_type;
262 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500263
Tom Stellard467f5162012-05-16 15:15:35 -0400264 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100265 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500266
Michel Dänzer51f89a02013-12-09 15:33:53 +0900267 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400268
Marek Olšákd7876082014-09-26 23:06:32 +0200269 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400270
271 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900272 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500273
Christian Königa0dca442013-03-22 15:59:22 +0100274 if (divisor) {
275 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200276 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200277 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100278 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200279 /* Load the buffer index for vertices. */
280 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
281 si_shader_ctx->param_vertex_id);
282 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
283 SI_PARAM_BASE_VERTEX);
284 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100285 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500286
287 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400288 args[0] = t_list;
289 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100290 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900291 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100292 "llvm.SI.vs.load.input", vec4_type, args, 3,
293 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500294
295 /* Break up the vec4 into individual components */
296 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900297 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500298 /* XXX: Use a helper function for this. There is one in
299 * tgsi_llvm.c. */
300 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900301 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500302 input, llvm_chan, "");
303 }
304}
305
Michel Dänzer404b29d2013-11-21 16:45:28 +0900306static LLVMValueRef fetch_input_gs(
307 struct lp_build_tgsi_context *bld_base,
308 const struct tgsi_full_src_register *reg,
309 enum tgsi_opcode_type type,
310 unsigned swizzle)
311{
312 struct lp_build_context *base = &bld_base->base;
313 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200314 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900315 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
316 struct gallivm_state *gallivm = base->gallivm;
317 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
318 LLVMValueRef vtx_offset;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900319 LLVMValueRef args[9];
320 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200321 struct tgsi_shader_info *info = &shader->selector->info;
322 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
323 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900324
Marek Olšáke23fec12014-10-04 17:40:39 +0200325 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900326 if (swizzle == 0)
327 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
328 SI_PARAM_PRIMITIVE_ID);
329 else
330 return uint->zero;
331 }
332
Michel Dänzer404b29d2013-11-21 16:45:28 +0900333 if (!reg->Register.Dimension)
334 return NULL;
335
336 if (swizzle == ~0) {
337 LLVMValueRef values[TGSI_NUM_CHANNELS];
338 unsigned chan;
339 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
340 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
341 }
342 return lp_build_gather_values(bld_base->base.gallivm, values,
343 TGSI_NUM_CHANNELS);
344 }
345
346 /* Get the vertex offset parameter */
347 vtx_offset_param = reg->Dimension.Index;
348 if (vtx_offset_param < 2) {
349 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
350 } else {
351 assert(vtx_offset_param < 6);
352 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
353 }
354 vtx_offset = lp_build_mul_imm(uint,
355 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
356 vtx_offset_param),
357 4);
358
Marek Olšákfc3b3352014-10-05 13:33:40 +0200359 args[0] = si_shader_ctx->esgs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900360 args[1] = vtx_offset;
361 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200362 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200363 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900364 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900365 args[3] = uint->zero;
366 args[4] = uint->one; /* OFFEN */
367 args[5] = uint->zero; /* IDXEN */
368 args[6] = uint->one; /* GLC */
369 args[7] = uint->zero; /* SLC */
370 args[8] = uint->zero; /* TFE */
371
372 return LLVMBuildBitCast(gallivm->builder,
373 build_intrinsic(gallivm->builder,
374 "llvm.SI.buffer.load.dword.i32.i32",
375 i32, args, 9,
376 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
377 tgsi2llvmtype(bld_base, type), "");
378}
379
Tom Stellarda75c6162012-01-06 17:38:37 -0500380static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900381 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500382 unsigned input_index,
383 const struct tgsi_full_declaration *decl)
384{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900385 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
386 struct si_shader_context *si_shader_ctx =
387 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200388 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900389 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
390 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400391 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900392 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100393
394 LLVMValueRef interp_param;
395 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500396
397 /* This value is:
398 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
399 * quad begins a new primitive. Bit 0 always needs
400 * to be unset)
401 * [32:16] ParamOffset
402 *
403 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900404 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200405 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500406
Christian König0666ffd2013-03-05 15:07:39 +0100407 unsigned chan;
408
Tom Stellard0fb1e682012-09-06 16:18:11 -0400409 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
410 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400411 unsigned soa_index =
412 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900413 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100414 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100415
416 if (chan == 3)
417 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900418 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100419 LLVMBuildFDiv(gallivm->builder,
420 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900421 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100422 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400423 }
424 return;
425 }
426
Michel Dänzer97078b12012-09-25 12:41:31 +0200427 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900428 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Marek Olšáke827bb62014-10-16 16:20:26 +0200429 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900430 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
431 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200432 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900433 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200434 lp_build_const_float(gallivm, 1.0f);
435
436 return;
437 }
438
Marek Olšák8b057dd2014-10-04 22:15:07 +0200439 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200440 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200441 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200442
Francisco Jerez12799232012-04-30 18:27:52 +0200443 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500444 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100445 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500446 break;
447 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200448 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200449 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400450 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100451 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200452 else
Christian König0666ffd2013-03-05 15:07:39 +0100453 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200454 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200455 case TGSI_INTERPOLATE_COLOR:
456 if (si_shader_ctx->shader->key.ps.flatshade) {
457 interp_param = 0;
458 break;
459 }
460 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200461 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200462 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200463 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400464 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100465 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200466 else
Christian König0666ffd2013-03-05 15:07:39 +0100467 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500468 break;
469 default:
470 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
471 return;
472 }
473
Christian König0666ffd2013-03-05 15:07:39 +0100474 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
475
Tom Stellarda75c6162012-01-06 17:38:37 -0500476 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200477 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100478 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100479 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200480 LLVMValueRef face, is_face_positive;
481 LLVMValueRef back_attr_number =
482 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200483 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200484
Christian König0666ffd2013-03-05 15:07:39 +0100485 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
486
Michel Dänzer691f08d2012-09-06 18:03:38 +0200487 is_face_positive = LLVMBuildFCmp(gallivm->builder,
Marek Olšákd1d2af22015-01-04 20:45:35 +0100488 LLVMRealOGT, face,
Michel Dänzer691f08d2012-09-06 18:03:38 +0200489 lp_build_const_float(gallivm, 0.0f),
490 "");
491
Tom Stellarda75c6162012-01-06 17:38:37 -0500492 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100493 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200494 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
495 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
496 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
497 LLVMValueRef front, back;
498
499 args[0] = llvm_chan;
500 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900501 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100502 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100503 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200504
505 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900506 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100507 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100508 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200509
Michel Dänzer51f89a02013-12-09 15:33:53 +0900510 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200511 LLVMBuildSelect(gallivm->builder,
512 is_face_positive,
513 front,
514 back,
515 "");
516 }
517
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900518 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200519 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
520 LLVMValueRef args[4];
521
522 args[0] = uint->zero;
523 args[1] = attr_number;
524 args[2] = params;
525 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900526 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
527 build_intrinsic(gallivm->builder, intr_name,
528 input_type, args, args[3] ? 4 : 3,
529 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
530 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
531 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200532 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900533 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200534 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200535 } else {
536 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100537 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200538 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
539 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
540 args[0] = llvm_chan;
541 args[1] = attr_number;
542 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100543 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900544 radeon_bld->inputs[soa_index] =
545 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100546 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100547 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200548 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500549 }
550}
551
Marek Olšák5b06fc32014-05-06 18:12:40 +0200552static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
553{
554 struct gallivm_state *gallivm = &radeon_bld->gallivm;
555 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
556 SI_PARAM_ANCILLARY);
557 value = LLVMBuildLShr(gallivm->builder, value,
558 lp_build_const_int32(gallivm, 8), "");
559 value = LLVMBuildAnd(gallivm->builder, value,
560 lp_build_const_int32(gallivm, 0xf), "");
561 return value;
562}
563
Marek Olšákd7876082014-09-26 23:06:32 +0200564/**
565 * Load a dword from a constant buffer.
566 */
567static LLVMValueRef buffer_load_const(LLVMBuilderRef builder, LLVMValueRef resource,
568 LLVMValueRef offset, LLVMTypeRef return_type)
Marek Olšák250aa932014-05-06 14:10:47 +0200569{
570 LLVMValueRef args[2] = {resource, offset};
571
572 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
573 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
574}
575
Christian Könige4ed5872013-03-21 18:02:52 +0100576static void declare_system_value(
577 struct radeon_llvm_context * radeon_bld,
578 unsigned index,
579 const struct tgsi_full_declaration *decl)
580{
Marek Olšák8d03d922013-09-01 23:59:06 +0200581 struct si_shader_context *si_shader_ctx =
582 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200583 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
584 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100585 LLVMValueRef value = 0;
586
587 switch (decl->Semantic.Name) {
588 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200589 value = LLVMGetParam(radeon_bld->main_fn,
590 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100591 break;
592
593 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200594 value = LLVMGetParam(radeon_bld->main_fn,
595 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100596 break;
597
Marek Olšák5b06fc32014-05-06 18:12:40 +0200598 case TGSI_SEMANTIC_SAMPLEID:
599 value = get_sample_id(radeon_bld);
600 break;
601
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200602 case TGSI_SEMANTIC_SAMPLEPOS:
603 {
604 LLVMBuilderRef builder = gallivm->builder;
605 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200606 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200607 LLVMValueRef resource = build_indexed_load_const(si_shader_ctx, desc, buf_index);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200608
609 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
610 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
611 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
612
613 LLVMValueRef pos[4] = {
Marek Olšákd7876082014-09-26 23:06:32 +0200614 buffer_load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
615 buffer_load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200616 lp_build_const_float(gallivm, 0),
617 lp_build_const_float(gallivm, 0)
618 };
619 value = lp_build_gather_values(gallivm, pos, 4);
620 break;
621 }
622
Christian Könige4ed5872013-03-21 18:02:52 +0100623 default:
624 assert(!"unknown system value");
625 return;
626 }
627
628 radeon_bld->system_values[index] = value;
629}
630
Tom Stellarda75c6162012-01-06 17:38:37 -0500631static LLVMValueRef fetch_constant(
632 struct lp_build_tgsi_context * bld_base,
633 const struct tgsi_full_src_register *reg,
634 enum tgsi_opcode_type type,
635 unsigned swizzle)
636{
Christian König55fe5cc2013-03-04 16:30:06 +0100637 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500638 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100639 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200640 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500641
Christian König0f6cf2b2013-03-15 15:53:25 +0100642 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100643 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500644
Christian König8514f5a2013-02-04 17:46:42 +0100645 if (swizzle == LP_CHAN_ALL) {
646 unsigned chan;
647 LLVMValueRef values[4];
648 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
649 values[chan] = fetch_constant(bld_base, reg, type, chan);
650
651 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
652 }
653
Marek Olšák2fd42002013-10-25 11:45:47 +0200654 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100655 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100656
Marek Olšák2fd42002013-10-25 11:45:47 +0200657 if (!reg->Register.Indirect)
658 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
659
Christian König0f6cf2b2013-03-15 15:53:25 +0100660 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
661 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
662 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200663 addr = lp_build_add(&bld_base->uint_bld, addr,
664 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200665
Marek Olšákd7876082014-09-26 23:06:32 +0200666 result = buffer_load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
Marek Olšák250aa932014-05-06 14:10:47 +0200667 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500668
Christian Königf5298b02013-02-28 14:50:07 +0100669 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500670}
671
Michel Dänzer26c71392012-08-24 12:03:11 +0200672/* Initialize arguments for the shader export intrinsic */
673static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900674 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200675 unsigned target,
676 LLVMValueRef *args)
677{
678 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
679 struct lp_build_context *uint =
680 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
681 struct lp_build_context *base = &bld_base->base;
682 unsigned compressed = 0;
683 unsigned chan;
684
Michel Dänzerf402acd2012-08-22 18:15:36 +0200685 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
686 int cbuf = target - V_008DFC_SQ_EXP_MRT;
687
688 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100689 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100690
691 if (compressed)
692 si_shader_ctx->shader->spi_shader_col_format |=
693 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
694 else
695 si_shader_ctx->shader->spi_shader_col_format |=
696 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200697
698 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200699 }
700 }
701
702 if (compressed) {
703 /* Pixel shader needs to pack output values before export */
704 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900705 args[0] = values[2 * chan];
706 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200707 args[chan + 5] =
708 build_intrinsic(base->gallivm->builder,
709 "llvm.SI.packf16",
710 LLVMInt32TypeInContext(base->gallivm->context),
711 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100712 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100713 args[chan + 7] = args[chan + 5] =
714 LLVMBuildBitCast(base->gallivm->builder,
715 args[chan + 5],
716 LLVMFloatTypeInContext(base->gallivm->context),
717 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200718 }
719
720 /* Set COMPR flag */
721 args[4] = uint->one;
722 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900723 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200724 /* +5 because the first output value will be
725 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900726 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200727
728 /* Clear COMPR flag */
729 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200730 }
731
732 /* XXX: This controls which components of the output
733 * registers actually get exported. (e.g bit 0 means export
734 * X component, bit 1 means export Y component, etc.) I'm
735 * hard coding this to 0xf for now. In the future, we might
736 * want to do something else. */
737 args[0] = lp_build_const_int32(base->gallivm, 0xf);
738
739 /* Specify whether the EXEC mask represents the valid mask */
740 args[1] = uint->zero;
741
742 /* Specify whether this is the last export */
743 args[2] = uint->zero;
744
745 /* Specify the target we are exporting */
746 args[3] = lp_build_const_int32(base->gallivm, target);
747
Michel Dänzer26c71392012-08-24 12:03:11 +0200748 /* XXX: We probably need to keep track of the output
749 * values, so we know what we are passing to the next
750 * stage. */
751}
752
Michel Dänzer404b29d2013-11-21 16:45:28 +0900753/* Load from output pointers and initialize arguments for the shader export intrinsic */
754static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
755 LLVMValueRef *out_ptr,
756 unsigned target,
757 LLVMValueRef *args)
758{
759 struct gallivm_state *gallivm = bld_base->base.gallivm;
760 LLVMValueRef values[4];
761 int i;
762
763 for (i = 0; i < 4; i++)
764 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
765
766 si_llvm_init_export_args(bld_base, values, target, args);
767}
768
Michel Dänzer7708a862012-11-02 15:57:30 +0100769static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900770 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100771{
772 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
773 struct gallivm_state *gallivm = bld_base->base.gallivm;
774
Christian Königa0dca442013-03-22 15:59:22 +0100775 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400776 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
777 SI_PARAM_ALPHA_REF);
778
Michel Dänzer7708a862012-11-02 15:57:30 +0100779 LLVMValueRef alpha_pass =
780 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100781 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900782 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400783 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100784 LLVMValueRef arg =
785 lp_build_select(&bld_base->base,
786 alpha_pass,
787 lp_build_const_float(gallivm, 1.0f),
788 lp_build_const_float(gallivm, -1.0f));
789
790 build_intrinsic(gallivm->builder,
791 "llvm.AMDGPU.kill",
792 LLVMVoidTypeInContext(gallivm->context),
793 &arg, 1, 0);
794 } else {
795 build_intrinsic(gallivm->builder,
796 "llvm.AMDGPU.kilp",
797 LLVMVoidTypeInContext(gallivm->context),
798 NULL, 0, 0);
799 }
Marek Olšákadc57972014-09-19 17:07:07 +0200800
801 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100802}
803
Michel Dänzere3befbc2013-05-15 18:09:50 +0200804static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900805 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200806{
807 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
808 struct lp_build_context *base = &bld_base->base;
809 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200810 unsigned reg_index;
811 unsigned chan;
812 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200813 LLVMValueRef base_elt;
814 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200815 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200816 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200817
Michel Dänzere3befbc2013-05-15 18:09:50 +0200818 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200819 LLVMValueRef *args = pos[2 + reg_index];
820
Michel Dänzere3befbc2013-05-15 18:09:50 +0200821 args[5] =
822 args[6] =
823 args[7] =
824 args[8] = lp_build_const_float(base->gallivm, 0.0f);
825
826 /* Compute dot products of position and user clip plane vectors */
827 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
828 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200829 args[1] = lp_build_const_int32(base->gallivm,
830 ((reg_index * 4 + chan) * 4 +
831 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200832 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200833 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200834 args[5 + chan] =
835 lp_build_add(base, args[5 + chan],
836 lp_build_mul(base, base_elt,
837 out_elts[const_chan]));
838 }
839 }
840
841 args[0] = lp_build_const_int32(base->gallivm, 0xf);
842 args[1] = uint->zero;
843 args[2] = uint->zero;
844 args[3] = lp_build_const_int32(base->gallivm,
845 V_008DFC_SQ_EXP_POS + 2 + reg_index);
846 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200847 }
848}
849
Marek Olšák8d03d922013-09-01 23:59:06 +0200850static void si_dump_streamout(struct pipe_stream_output_info *so)
851{
852 unsigned i;
853
854 if (so->num_outputs)
855 fprintf(stderr, "STREAMOUT\n");
856
857 for (i = 0; i < so->num_outputs; i++) {
858 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
859 so->output[i].start_component;
860 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
861 i, so->output[i].output_buffer,
862 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
863 so->output[i].register_index,
864 mask & 1 ? "x" : "",
865 mask & 2 ? "y" : "",
866 mask & 4 ? "z" : "",
867 mask & 8 ? "w" : "");
868 }
869}
870
871/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
872 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
873 * or v4i32 (num_channels=3,4). */
874static void build_tbuffer_store(struct si_shader_context *shader,
875 LLVMValueRef rsrc,
876 LLVMValueRef vdata,
877 unsigned num_channels,
878 LLVMValueRef vaddr,
879 LLVMValueRef soffset,
880 unsigned inst_offset,
881 unsigned dfmt,
882 unsigned nfmt,
883 unsigned offen,
884 unsigned idxen,
885 unsigned glc,
886 unsigned slc,
887 unsigned tfe)
888{
889 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
890 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
891 LLVMValueRef args[] = {
892 rsrc,
893 vdata,
894 LLVMConstInt(i32, num_channels, 0),
895 vaddr,
896 soffset,
897 LLVMConstInt(i32, inst_offset, 0),
898 LLVMConstInt(i32, dfmt, 0),
899 LLVMConstInt(i32, nfmt, 0),
900 LLVMConstInt(i32, offen, 0),
901 LLVMConstInt(i32, idxen, 0),
902 LLVMConstInt(i32, glc, 0),
903 LLVMConstInt(i32, slc, 0),
904 LLVMConstInt(i32, tfe, 0)
905 };
906
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900907 /* The instruction offset field has 12 bits */
908 assert(offen || inst_offset < (1 << 12));
909
Marek Olšák8d03d922013-09-01 23:59:06 +0200910 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
911 unsigned func = CLAMP(num_channels, 1, 3) - 1;
912 const char *types[] = {"i32", "v2i32", "v4i32"};
913 char name[256];
914 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
915
916 lp_build_intrinsic(gallivm->builder, name,
917 LLVMVoidTypeInContext(gallivm->context),
918 args, Elements(args));
919}
920
921static void build_streamout_store(struct si_shader_context *shader,
922 LLVMValueRef rsrc,
923 LLVMValueRef vdata,
924 unsigned num_channels,
925 LLVMValueRef vaddr,
926 LLVMValueRef soffset,
927 unsigned inst_offset)
928{
929 static unsigned dfmt[] = {
930 V_008F0C_BUF_DATA_FORMAT_32,
931 V_008F0C_BUF_DATA_FORMAT_32_32,
932 V_008F0C_BUF_DATA_FORMAT_32_32_32,
933 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
934 };
935 assert(num_channels >= 1 && num_channels <= 4);
936
937 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
938 inst_offset, dfmt[num_channels-1],
939 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
940}
941
942/* On SI, the vertex shader is responsible for writing streamout data
943 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900944static void si_llvm_emit_streamout(struct si_shader_context *shader,
945 struct si_shader_output_values *outputs,
946 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200947{
948 struct pipe_stream_output_info *so = &shader->shader->selector->so;
949 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
950 LLVMBuilderRef builder = gallivm->builder;
951 int i, j;
952 struct lp_build_if_state if_ctx;
953
954 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
955
956 LLVMValueRef so_param =
957 LLVMGetParam(shader->radeon_bld.main_fn,
958 shader->param_streamout_config);
959
960 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
961 LLVMValueRef so_vtx_count =
962 LLVMBuildAnd(builder,
963 LLVMBuildLShr(builder, so_param,
964 LLVMConstInt(i32, 16, 0), ""),
965 LLVMConstInt(i32, 127, 0), "");
966
967 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
968 NULL, 0, LLVMReadNoneAttribute);
969
970 /* can_emit = tid < so_vtx_count; */
971 LLVMValueRef can_emit =
972 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
973
974 /* Emit the streamout code conditionally. This actually avoids
975 * out-of-bounds buffer access. The hw tells us via the SGPR
976 * (so_vtx_count) which threads are allowed to emit streamout data. */
977 lp_build_if(&if_ctx, gallivm, can_emit);
978 {
979 /* The buffer offset is computed as follows:
980 * ByteOffset = streamout_offset[buffer_id]*4 +
981 * (streamout_write_index + thread_id)*stride[buffer_id] +
982 * attrib_offset
983 */
984
985 LLVMValueRef so_write_index =
986 LLVMGetParam(shader->radeon_bld.main_fn,
987 shader->param_streamout_write_index);
988
989 /* Compute (streamout_write_index + thread_id). */
990 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
991
992 /* Compute the write offset for each enabled buffer. */
993 LLVMValueRef so_write_offset[4] = {};
994 for (i = 0; i < 4; i++) {
995 if (!so->stride[i])
996 continue;
997
998 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
999 shader->param_streamout_offset[i]);
1000 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1001
1002 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1003 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1004 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1005 }
1006
Marek Olšák8d03d922013-09-01 23:59:06 +02001007 /* Write streamout data. */
1008 for (i = 0; i < so->num_outputs; i++) {
1009 unsigned buf_idx = so->output[i].output_buffer;
1010 unsigned reg = so->output[i].register_index;
1011 unsigned start = so->output[i].start_component;
1012 unsigned num_comps = so->output[i].num_components;
1013 LLVMValueRef out[4];
1014
1015 assert(num_comps && num_comps <= 4);
1016 if (!num_comps || num_comps > 4)
1017 continue;
1018
Marek Olšák558f7772014-10-04 22:37:23 +02001019 if (reg >= noutput)
1020 continue;
1021
Marek Olšák8d03d922013-09-01 23:59:06 +02001022 /* Load the output as int. */
1023 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001024 out[j] = LLVMBuildBitCast(builder,
1025 outputs[reg].values[start+j],
1026 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001027 }
1028
1029 /* Pack the output. */
1030 LLVMValueRef vdata = NULL;
1031
1032 switch (num_comps) {
1033 case 1: /* as i32 */
1034 vdata = out[0];
1035 break;
1036 case 2: /* as v2i32 */
1037 case 3: /* as v4i32 (aligned to 4) */
1038 case 4: /* as v4i32 */
1039 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1040 for (j = 0; j < num_comps; j++) {
1041 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1042 LLVMConstInt(i32, j, 0), "");
1043 }
1044 break;
1045 }
1046
1047 build_streamout_store(shader, shader->so_buffers[buf_idx],
1048 vdata, num_comps,
1049 so_write_offset[buf_idx],
1050 LLVMConstInt(i32, 0, 0),
1051 so->output[i].dst_offset*4);
1052 }
1053 }
1054 lp_build_endif(&if_ctx);
1055}
1056
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001057
Michel Dänzer404b29d2013-11-21 16:45:28 +09001058/* Generate export instructions for hardware VS shader stage */
1059static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1060 struct si_shader_output_values *outputs,
1061 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001062{
1063 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001064 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001065 struct lp_build_context * base = &bld_base->base;
1066 struct lp_build_context * uint =
1067 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001068 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001069 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001070 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001071 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001072 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001073 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001074 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001075 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001076
Michel Dänzer67e385b2014-01-08 17:48:21 +09001077 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1078 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001079 }
1080
Michel Dänzer404b29d2013-11-21 16:45:28 +09001081 for (i = 0; i < noutput; i++) {
1082 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001083 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001084
Michel Dänzer0afeea52013-05-02 14:53:17 +02001085handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001086 /* Select the correct target */
1087 switch(semantic_name) {
1088 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001089 psize_value = outputs[i].values[0];
1090 continue;
1091 case TGSI_SEMANTIC_EDGEFLAG:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001092 edgeflag_value = outputs[i].values[0];
1093 continue;
1094 case TGSI_SEMANTIC_LAYER:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001095 layer_value = outputs[i].values[0];
1096 continue;
1097 case TGSI_SEMANTIC_POSITION:
1098 target = V_008DFC_SQ_EXP_POS;
1099 break;
1100 case TGSI_SEMANTIC_COLOR:
1101 case TGSI_SEMANTIC_BCOLOR:
1102 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001103 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001104 param_count++;
1105 break;
1106 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001107 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1108 break;
1109 case TGSI_SEMANTIC_CLIPVERTEX:
1110 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1111 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001112 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001113 case TGSI_SEMANTIC_FOG:
1114 case TGSI_SEMANTIC_GENERIC:
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 default:
1120 target = 0;
1121 fprintf(stderr,
1122 "Warning: SI unhandled vs output type:%d\n",
1123 semantic_name);
1124 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001125
Michel Dänzer404b29d2013-11-21 16:45:28 +09001126 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001127
Michel Dänzer404b29d2013-11-21 16:45:28 +09001128 if (target >= V_008DFC_SQ_EXP_POS &&
1129 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1130 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1131 args, sizeof(args));
1132 } else {
1133 lp_build_intrinsic(base->gallivm->builder,
1134 "llvm.SI.export",
1135 LLVMVoidTypeInContext(base->gallivm->context),
1136 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001137 }
1138
1139 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1140 semantic_name = TGSI_SEMANTIC_GENERIC;
1141 goto handle_semantic;
1142 }
1143 }
1144
1145 /* We need to add the position output manually if it's missing. */
1146 if (!pos_args[0][0]) {
1147 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1148 pos_args[0][1] = uint->zero; /* EXEC mask */
1149 pos_args[0][2] = uint->zero; /* last export? */
1150 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1151 pos_args[0][4] = uint->zero; /* COMPR flag */
1152 pos_args[0][5] = base->zero; /* X */
1153 pos_args[0][6] = base->zero; /* Y */
1154 pos_args[0][7] = base->zero; /* Z */
1155 pos_args[0][8] = base->one; /* W */
1156 }
1157
1158 /* Write the misc vector (point size, edgeflag, layer, viewport). */
Marek Olšák16153472014-12-07 19:30:08 +01001159 if (shader->selector->info.writes_psize ||
1160 shader->selector->info.writes_edgeflag ||
1161 shader->selector->info.writes_layer) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001162 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
Marek Olšák16153472014-12-07 19:30:08 +01001163 shader->selector->info.writes_psize |
1164 (shader->selector->info.writes_edgeflag << 1) |
1165 (shader->selector->info.writes_layer << 2));
Michel Dänzer51f89a02013-12-09 15:33:53 +09001166 pos_args[1][1] = uint->zero; /* EXEC mask */
1167 pos_args[1][2] = uint->zero; /* last export? */
1168 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1169 pos_args[1][4] = uint->zero; /* COMPR flag */
1170 pos_args[1][5] = base->zero; /* X */
1171 pos_args[1][6] = base->zero; /* Y */
1172 pos_args[1][7] = base->zero; /* Z */
1173 pos_args[1][8] = base->zero; /* W */
1174
Marek Olšák16153472014-12-07 19:30:08 +01001175 if (shader->selector->info.writes_psize)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001176 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001177
Marek Olšák16153472014-12-07 19:30:08 +01001178 if (shader->selector->info.writes_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001179 /* The output is a float, but the hw expects an integer
1180 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001181 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1182 edgeflag_value,
1183 bld_base->uint_bld.elem_type, "");
1184 edgeflag_value = lp_build_min(&bld_base->int_bld,
1185 edgeflag_value,
1186 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001187
1188 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001189 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1190 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001191 base->elem_type, "");
1192 }
1193
Marek Olšák16153472014-12-07 19:30:08 +01001194 if (shader->selector->info.writes_layer)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001195 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001196 }
1197
1198 for (i = 0; i < 4; i++)
1199 if (pos_args[i][0])
1200 shader->nr_pos_exports++;
1201
1202 pos_idx = 0;
1203 for (i = 0; i < 4; i++) {
1204 if (!pos_args[i][0])
1205 continue;
1206
1207 /* Specify the target we are exporting */
1208 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1209
1210 if (pos_idx == shader->nr_pos_exports)
1211 /* Specify that this is the last export */
1212 pos_args[i][2] = uint->one;
1213
1214 lp_build_intrinsic(base->gallivm->builder,
1215 "llvm.SI.export",
1216 LLVMVoidTypeInContext(base->gallivm->context),
1217 pos_args[i], 9);
1218 }
1219}
1220
Michel Dänzer404b29d2013-11-21 16:45:28 +09001221static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1222{
1223 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1224 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001225 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001226 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001227 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001228 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1229 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001230 unsigned chan;
1231 int i;
1232
Marek Olšák216cf862014-10-04 17:04:05 +02001233 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001234 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001235 si_shader_ctx->radeon_bld.soa.outputs[i];
1236 int param_index = get_param_index(info->output_semantic_name[i],
1237 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001238 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001239
Marek Olšáke29353f2014-09-17 22:17:02 +02001240 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001241 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001242
1243 for (chan = 0; chan < 4; chan++) {
1244 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001245 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1246
Marek Olšákfc3b3352014-10-05 13:33:40 +02001247 build_tbuffer_store(si_shader_ctx,
1248 si_shader_ctx->esgs_ring,
1249 out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001250 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001251 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001252 V_008F0C_BUF_DATA_FORMAT_32,
1253 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001254 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001255 }
1256 }
1257}
1258
1259static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1260{
1261 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1262 struct gallivm_state *gallivm = bld_base->base.gallivm;
1263 LLVMValueRef args[2];
1264
1265 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1266 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1267 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1268 LLVMVoidTypeInContext(gallivm->context), args, 2,
1269 LLVMNoUnwindAttribute);
1270}
1271
1272static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1273{
1274 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1275 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001276 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001277 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001278 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001279
Marek Olšákec0d1682014-10-04 22:33:36 +02001280 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001281
Marek Olšákec0d1682014-10-04 22:33:36 +02001282 for (i = 0; i < info->num_outputs; i++) {
1283 outputs[i].name = info->output_semantic_name[i];
1284 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001285
Marek Olšákec0d1682014-10-04 22:33:36 +02001286 for (j = 0; j < 4; j++)
1287 outputs[i].values[j] =
1288 LLVMBuildLoad(gallivm->builder,
1289 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1290 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001291 }
1292
Marek Olšákec0d1682014-10-04 22:33:36 +02001293 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001294 FREE(outputs);
1295}
1296
Michel Dänzer51f89a02013-12-09 15:33:53 +09001297static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1298{
1299 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001300 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001301 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001302 struct lp_build_context * uint = &bld_base->uint_bld;
1303 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001304 LLVMValueRef args[9];
1305 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001306 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001307 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001308
Marek Olšák75e97e22014-10-04 23:13:50 +02001309 for (i = 0; i < info->num_outputs; i++) {
1310 unsigned semantic_name = info->output_semantic_name[i];
1311 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001312 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001313
Marek Olšák75e97e22014-10-04 23:13:50 +02001314 /* Select the correct target */
1315 switch (semantic_name) {
1316 case TGSI_SEMANTIC_POSITION:
1317 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001318 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001319 case TGSI_SEMANTIC_STENCIL:
1320 stencil_index = i;
1321 continue;
1322 case TGSI_SEMANTIC_SAMPLEMASK:
1323 samplemask_index = i;
1324 continue;
1325 case TGSI_SEMANTIC_COLOR:
1326 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1327 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1328 LLVMBuildStore(bld_base->base.gallivm->builder,
1329 bld_base->base.one,
1330 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001331
Marek Olšák75e97e22014-10-04 23:13:50 +02001332 if (semantic_index == 0 &&
1333 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1334 si_alpha_test(bld_base,
1335 si_shader_ctx->radeon_bld.soa.outputs[i]);
1336 break;
1337 default:
1338 target = 0;
1339 fprintf(stderr,
1340 "Warning: SI unhandled fs output type:%d\n",
1341 semantic_name);
1342 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001343
Marek Olšák75e97e22014-10-04 23:13:50 +02001344 si_llvm_init_export_args_load(bld_base,
1345 si_shader_ctx->radeon_bld.soa.outputs[i],
1346 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001347
Marek Olšák75e97e22014-10-04 23:13:50 +02001348 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1349 /* If there is an export instruction waiting to be emitted, do so now. */
1350 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001351 lp_build_intrinsic(base->gallivm->builder,
1352 "llvm.SI.export",
1353 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001354 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001355 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001356
1357 /* This instruction will be emitted at the end of the shader. */
1358 memcpy(last_args, args, sizeof(args));
1359
1360 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1361 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1362 semantic_index == 0 &&
1363 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1364 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1365 si_llvm_init_export_args_load(bld_base,
1366 si_shader_ctx->radeon_bld.soa.outputs[i],
1367 V_008DFC_SQ_EXP_MRT + c, args);
1368 lp_build_intrinsic(base->gallivm->builder,
1369 "llvm.SI.export",
1370 LLVMVoidTypeInContext(base->gallivm->context),
1371 args, 9);
1372 }
1373 }
1374 } else {
1375 lp_build_intrinsic(base->gallivm->builder,
1376 "llvm.SI.export",
1377 LLVMVoidTypeInContext(base->gallivm->context),
1378 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001379 }
1380 }
1381
Marek Olšákd0e8b652014-05-06 20:04:31 +02001382 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001383 LLVMValueRef out_ptr;
1384 unsigned mask = 0;
1385
1386 /* Specify the target we are exporting */
1387 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1388
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001389 args[5] = base->zero; /* R, depth */
1390 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1391 args[7] = base->zero; /* B, sample mask */
1392 args[8] = base->zero; /* A, alpha to mask */
1393
Michel Dänzer1a616c12012-11-13 17:35:09 +01001394 if (depth_index >= 0) {
1395 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1396 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1397 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001398 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001399 }
1400
1401 if (stencil_index >= 0) {
1402 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001403 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001404 mask |= 0x2;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001405 si_shader_ctx->shader->db_shader_control |=
1406 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001407 }
1408
Marek Olšákd0e8b652014-05-06 20:04:31 +02001409 if (samplemask_index >= 0) {
1410 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1411 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001412 mask |= 0x4;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001413 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1414 }
1415
Marek Olšák368b0a72014-12-28 21:51:35 +01001416 /* SI (except OLAND) has a bug that it only looks
1417 * at the X writemask component. */
1418 if (si_shader_ctx->screen->b.chip_class == SI &&
1419 si_shader_ctx->screen->b.family != CHIP_OLAND)
1420 mask |= 0x1;
1421
Marek Olšákd0e8b652014-05-06 20:04:31 +02001422 if (samplemask_index >= 0)
1423 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1424 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001425 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1426 else
1427 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1428
Michel Dänzer1a616c12012-11-13 17:35:09 +01001429 /* Specify which components to enable */
1430 args[0] = lp_build_const_int32(base->gallivm, mask);
1431
1432 args[1] =
1433 args[2] =
1434 args[4] = uint->zero;
1435
1436 if (last_args[0])
1437 lp_build_intrinsic(base->gallivm->builder,
1438 "llvm.SI.export",
1439 LLVMVoidTypeInContext(base->gallivm->context),
1440 args, 9);
1441 else
1442 memcpy(last_args, args, sizeof(args));
1443 }
1444
Michel Dänzer51f89a02013-12-09 15:33:53 +09001445 if (!last_args[0]) {
1446 /* Specify which components to enable */
1447 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001448
Michel Dänzer51f89a02013-12-09 15:33:53 +09001449 /* Specify the target we are exporting */
1450 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001451
Michel Dänzer51f89a02013-12-09 15:33:53 +09001452 /* Set COMPR flag to zero to export data as 32-bit */
1453 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001454
Michel Dänzer51f89a02013-12-09 15:33:53 +09001455 /* dummy bits */
1456 last_args[5]= uint->zero;
1457 last_args[6]= uint->zero;
1458 last_args[7]= uint->zero;
1459 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001460 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001461
1462 /* Specify whether the EXEC mask represents the valid mask */
1463 last_args[1] = uint->one;
1464
1465 /* Specify that this is the last export */
1466 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1467
1468 lp_build_intrinsic(base->gallivm->builder,
1469 "llvm.SI.export",
1470 LLVMVoidTypeInContext(base->gallivm->context),
1471 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001472}
1473
Marek Olšák4855acd2013-08-06 15:08:54 +02001474static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1475 struct lp_build_tgsi_context * bld_base,
1476 struct lp_build_emit_data * emit_data);
1477
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001478static bool tgsi_is_shadow_sampler(unsigned target)
1479{
1480 return target == TGSI_TEXTURE_SHADOW1D ||
1481 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1482 target == TGSI_TEXTURE_SHADOW2D ||
1483 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1484 target == TGSI_TEXTURE_SHADOWCUBE ||
1485 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1486 target == TGSI_TEXTURE_SHADOWRECT;
1487}
1488
Marek Olšák877bb522014-06-25 03:12:46 +02001489static const struct lp_build_tgsi_action tex_action;
1490
Tom Stellarda75c6162012-01-06 17:38:37 -05001491static void tex_fetch_args(
1492 struct lp_build_tgsi_context * bld_base,
1493 struct lp_build_emit_data * emit_data)
1494{
Christian König55fe5cc2013-03-04 16:30:06 +01001495 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001496 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001497 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001498 unsigned opcode = inst->Instruction.Opcode;
1499 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001500 LLVMValueRef coords[4];
1501 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001502 int ref_pos;
1503 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001504 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001505 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001506 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1507 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001508 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001509
1510 if (target == TGSI_TEXTURE_BUFFER) {
1511 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1512 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1513 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1514 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1515
Marek Olšák4f3f0432014-07-07 22:49:15 +02001516 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001517 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1518 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1519 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1520 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1521
1522 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1523 emit_data->args[0] = res;
1524 emit_data->args[1] = bld_base->uint_bld.zero;
1525 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1526 emit_data->arg_count = 3;
1527 return;
1528 }
Tom Stellard467f5162012-05-16 15:15:35 -04001529
Michel Dänzer120efee2013-01-25 12:10:11 +01001530 /* Fetch and project texture coordinates */
1531 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001532 for (chan = 0; chan < 3; chan++ ) {
1533 coords[chan] = lp_build_emit_fetch(bld_base,
1534 emit_data->inst, 0,
1535 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001536 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001537 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1538 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001539 coords[chan],
1540 coords[3]);
1541 }
1542
Michel Dänzer120efee2013-01-25 12:10:11 +01001543 if (opcode == TGSI_OPCODE_TXP)
1544 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001545
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001546 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001547 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001548 /* The offsets are six-bit signed integers packed like this:
1549 * X=[5:0], Y=[13:8], and Z=[21:16].
1550 */
1551 LLVMValueRef offset[3], pack;
1552
1553 assert(inst->Texture.NumOffsets == 1);
1554
1555 for (chan = 0; chan < 3; chan++) {
1556 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1557 emit_data->inst, 0, chan);
1558 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1559 lp_build_const_int32(gallivm, 0x3f), "");
1560 if (chan)
1561 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1562 lp_build_const_int32(gallivm, chan*8), "");
1563 }
1564
1565 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1566 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1567 address[count++] = pack;
1568 }
1569
Michel Dänzer120efee2013-01-25 12:10:11 +01001570 /* Pack LOD bias value */
1571 if (opcode == TGSI_OPCODE_TXB)
1572 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001573 if (opcode == TGSI_OPCODE_TXB2)
1574 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001575
Michel Dänzer120efee2013-01-25 12:10:11 +01001576 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001577 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001578 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1579 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1580 } else {
1581 assert(ref_pos >= 0);
1582 address[count++] = coords[ref_pos];
1583 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001584 }
1585
Marek Olšákb279f0142014-07-04 03:19:58 +02001586 if (target == TGSI_TEXTURE_CUBE ||
1587 target == TGSI_TEXTURE_CUBE_ARRAY ||
1588 target == TGSI_TEXTURE_SHADOWCUBE ||
1589 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1590 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1591
Michel Dänzera6b83c02013-02-21 16:10:55 +01001592 /* Pack user derivatives */
1593 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001594 int num_deriv_channels, param;
1595
1596 switch (target) {
1597 case TGSI_TEXTURE_3D:
1598 num_deriv_channels = 3;
1599 break;
1600 case TGSI_TEXTURE_2D:
1601 case TGSI_TEXTURE_SHADOW2D:
1602 case TGSI_TEXTURE_RECT:
1603 case TGSI_TEXTURE_SHADOWRECT:
1604 case TGSI_TEXTURE_2D_ARRAY:
1605 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1606 case TGSI_TEXTURE_CUBE:
1607 case TGSI_TEXTURE_SHADOWCUBE:
1608 case TGSI_TEXTURE_CUBE_ARRAY:
1609 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1610 num_deriv_channels = 2;
1611 break;
1612 case TGSI_TEXTURE_1D:
1613 case TGSI_TEXTURE_SHADOW1D:
1614 case TGSI_TEXTURE_1D_ARRAY:
1615 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1616 num_deriv_channels = 1;
1617 break;
1618 default:
1619 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001620 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001621
1622 for (param = 1; param <= 2; param++)
1623 for (chan = 0; chan < num_deriv_channels; chan++)
1624 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001625 }
1626
Michel Dänzer120efee2013-01-25 12:10:11 +01001627 /* Pack texture coordinates */
1628 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001629 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001630 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001631 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001632 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001633
Marek Olšákd2bd6342013-09-18 15:40:21 +02001634 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001635 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001636 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001637 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001638 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001639
1640 if (count > 16) {
1641 assert(!"Cannot handle more than 16 texture address parameters");
1642 count = 16;
1643 }
1644
1645 for (chan = 0; chan < count; chan++ ) {
1646 address[chan] = LLVMBuildBitCast(gallivm->builder,
1647 address[chan],
1648 LLVMInt32TypeInContext(gallivm->context),
1649 "");
1650 }
1651
Marek Olšák4855acd2013-08-06 15:08:54 +02001652 /* Adjust the sample index according to FMASK.
1653 *
1654 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1655 * which is the identity mapping. Each nibble says which physical sample
1656 * should be fetched to get that sample.
1657 *
1658 * For example, 0x11111100 means there are only 2 samples stored and
1659 * the second sample covers 3/4 of the pixel. When reading samples 0
1660 * and 1, return physical sample 0 (determined by the first two 0s
1661 * in FMASK), otherwise return physical sample 1.
1662 *
1663 * The sample index should be adjusted as follows:
1664 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1665 */
1666 if (target == TGSI_TEXTURE_2D_MSAA ||
1667 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1668 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1669 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001670 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001671 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001672 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001673
Marek Olšákd2bd6342013-09-18 15:40:21 +02001674 memcpy(txf_address, address, sizeof(txf_address));
1675
1676 if (target == TGSI_TEXTURE_2D_MSAA) {
1677 txf_address[2] = bld_base->uint_bld.zero;
1678 }
1679 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001680
1681 /* Pad to a power-of-two size. */
1682 while (txf_count < util_next_power_of_two(txf_count))
1683 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1684
1685 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001686 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1687 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1688 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001689 txf_emit_data.chan = 0;
1690 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001691 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001692 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001693 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001694 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001695 txf_emit_data.arg_count = 3;
1696
Marek Olšák877bb522014-06-25 03:12:46 +02001697 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001698
1699 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001700 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1701 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1702
1703 /* Apply the formula. */
1704 LLVMValueRef fmask =
1705 LLVMBuildExtractElement(gallivm->builder,
1706 txf_emit_data.output[0],
1707 uint_bld->zero, "");
1708
Marek Olšákd2bd6342013-09-18 15:40:21 +02001709 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001710
1711 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001712 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001713
1714 LLVMValueRef shifted_fmask =
1715 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1716
1717 LLVMValueRef final_sample =
1718 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1719
1720 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1721 * resource descriptor is 0 (invalid),
1722 */
1723 LLVMValueRef fmask_desc =
1724 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001725 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001726 LLVMVectorType(uint_bld->elem_type, 8), "");
1727
1728 LLVMValueRef fmask_word1 =
1729 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1730 uint_bld->one, "");
1731
1732 LLVMValueRef word1_is_nonzero =
1733 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1734 fmask_word1, uint_bld->zero, "");
1735
Marek Olšákd2bd6342013-09-18 15:40:21 +02001736 /* Replace the MSAA sample index. */
1737 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001738 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001739 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001740 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001741
Michel Dänzer36231112013-05-02 09:44:45 +02001742 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001743 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001744
1745 if (opcode == TGSI_OPCODE_TXF) {
1746 /* add tex offsets */
1747 if (inst->Texture.NumOffsets) {
1748 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1749 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1750 const struct tgsi_texture_offset * off = inst->TexOffsets;
1751
1752 assert(inst->Texture.NumOffsets == 1);
1753
Marek Olšákdefedc02013-09-18 15:36:38 +02001754 switch (target) {
1755 case TGSI_TEXTURE_3D:
1756 address[2] = lp_build_add(uint_bld, address[2],
1757 bld->immediates[off->Index][off->SwizzleZ]);
1758 /* fall through */
1759 case TGSI_TEXTURE_2D:
1760 case TGSI_TEXTURE_SHADOW2D:
1761 case TGSI_TEXTURE_RECT:
1762 case TGSI_TEXTURE_SHADOWRECT:
1763 case TGSI_TEXTURE_2D_ARRAY:
1764 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001765 address[1] =
1766 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001767 bld->immediates[off->Index][off->SwizzleY]);
1768 /* fall through */
1769 case TGSI_TEXTURE_1D:
1770 case TGSI_TEXTURE_SHADOW1D:
1771 case TGSI_TEXTURE_1D_ARRAY:
1772 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1773 address[0] =
1774 lp_build_add(uint_bld, address[0],
1775 bld->immediates[off->Index][off->SwizzleX]);
1776 break;
1777 /* texture offsets do not apply to other texture targets */
1778 }
Michel Dänzer36231112013-05-02 09:44:45 +02001779 }
1780
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001781 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1782 emit_data->arg_count = 3;
1783
Michel Dänzer36231112013-05-02 09:44:45 +02001784 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001785 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001786 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001787 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001788 opcode == TGSI_OPCODE_LODQ ||
1789 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001790 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1791 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1792 target == TGSI_TEXTURE_2D_ARRAY ||
1793 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1794 target == TGSI_TEXTURE_CUBE_ARRAY ||
1795 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1796 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001797 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001798
Marek Olšák57f3da92014-06-16 16:53:42 +02001799 if (opcode == TGSI_OPCODE_TG4) {
1800 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001801
Marek Olšák57f3da92014-06-16 16:53:42 +02001802 /* DMASK was repurposed for GATHER4. 4 components are always
1803 * returned and DMASK works like a swizzle - it selects
1804 * the component to fetch. The only valid DMASK values are
1805 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1806 * (red,red,red,red) etc.) The ISA document doesn't mention
1807 * this.
1808 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001809
Marek Olšák57f3da92014-06-16 16:53:42 +02001810 /* Get the component index from src1.x for Gather4. */
1811 if (!tgsi_is_shadow_sampler(target)) {
1812 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1813 LLVMValueRef comp_imm;
1814 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001815
Marek Olšák57f3da92014-06-16 16:53:42 +02001816 assert(src1.File == TGSI_FILE_IMMEDIATE);
1817
1818 comp_imm = imms[src1.Index][src1.SwizzleX];
1819 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1820 gather_comp = CLAMP(gather_comp, 0, 3);
1821 }
1822
1823 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001824 }
1825
Marek Olšák4855acd2013-08-06 15:08:54 +02001826 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001827 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001828 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1829 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1830 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1831 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1832 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1833 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1834 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1835
1836 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001837
1838 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001839 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001840 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001841 } else {
1842 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1843 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001844 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001845
1846 emit_data->dst_type = LLVMVectorType(
1847 LLVMFloatTypeInContext(gallivm->context),
1848 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001849 }
1850
Marek Olšák2484daa2014-04-22 21:23:29 +02001851 /* The fetch opcode has been converted to a 2D array fetch.
1852 * This simplifies the LLVM backend. */
1853 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1854 target = TGSI_TEXTURE_2D_ARRAY;
1855 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1856 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1857
Michel Dänzer120efee2013-01-25 12:10:11 +01001858 /* Pad to power of two vector */
1859 while (count < util_next_power_of_two(count))
1860 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1861
Christian Königccf3e8f2013-03-26 15:09:27 +01001862 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001863}
1864
Michel Dänzer07eddc42013-02-06 15:43:10 +01001865static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1866 struct lp_build_tgsi_context * bld_base,
1867 struct lp_build_emit_data * emit_data)
1868{
1869 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001870 unsigned opcode = emit_data->inst->Instruction.Opcode;
1871 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001872 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001873 bool has_offset = HAVE_LLVM >= 0x0305 ?
1874 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001875
Marek Olšák877bb522014-06-25 03:12:46 +02001876 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001877 emit_data->output[emit_data->chan] = build_intrinsic(
1878 base->gallivm->builder,
1879 "llvm.SI.vs.load.input", emit_data->dst_type,
1880 emit_data->args, emit_data->arg_count,
1881 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1882 return;
1883 }
1884
Marek Olšák877bb522014-06-25 03:12:46 +02001885 if (opcode == TGSI_OPCODE_TG4 ||
1886 opcode == TGSI_OPCODE_LODQ ||
1887 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1888 bool is_shadow = tgsi_is_shadow_sampler(target);
1889 const char *name = "llvm.SI.image.sample";
1890 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001891
Marek Olšák877bb522014-06-25 03:12:46 +02001892 switch (opcode) {
1893 case TGSI_OPCODE_TEX:
1894 case TGSI_OPCODE_TEX2:
1895 case TGSI_OPCODE_TXP:
1896 break;
1897 case TGSI_OPCODE_TXB:
1898 case TGSI_OPCODE_TXB2:
1899 infix = ".b";
1900 break;
1901 case TGSI_OPCODE_TXL:
1902 case TGSI_OPCODE_TXL2:
1903 infix = ".l";
1904 break;
1905 case TGSI_OPCODE_TXD:
1906 infix = ".d";
1907 break;
1908 case TGSI_OPCODE_TG4:
1909 name = "llvm.SI.gather4";
1910 break;
1911 case TGSI_OPCODE_LODQ:
1912 name = "llvm.SI.getlod";
1913 is_shadow = false;
1914 has_offset = false;
1915 break;
1916 default:
1917 assert(0);
1918 return;
1919 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001920
Marek Olšák877bb522014-06-25 03:12:46 +02001921 /* Add the type and suffixes .c, .o if needed. */
1922 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1923 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1924 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001925
Marek Olšák877bb522014-06-25 03:12:46 +02001926 emit_data->output[emit_data->chan] = build_intrinsic(
1927 base->gallivm->builder, intr_name, emit_data->dst_type,
1928 emit_data->args, emit_data->arg_count,
1929 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1930 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001931 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001932 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001933
Marek Olšák877bb522014-06-25 03:12:46 +02001934 switch (opcode) {
1935 case TGSI_OPCODE_TEX:
1936 case TGSI_OPCODE_TEX2:
1937 case TGSI_OPCODE_TXP:
1938 name = "llvm.SI.sample";
1939 break;
1940 case TGSI_OPCODE_TXB:
1941 case TGSI_OPCODE_TXB2:
1942 name = "llvm.SI.sampleb";
1943 break;
1944 case TGSI_OPCODE_TXD:
1945 name = "llvm.SI.sampled";
1946 break;
1947 case TGSI_OPCODE_TXF:
1948 name = "llvm.SI.imageload";
1949 break;
1950 case TGSI_OPCODE_TXL:
1951 case TGSI_OPCODE_TXL2:
1952 name = "llvm.SI.samplel";
1953 break;
1954 default:
1955 assert(0);
1956 return;
1957 }
1958
Marek Olšákd859bdb2014-07-14 19:40:14 +02001959 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1960 v16i8 = LLVMVectorType(i8, 16);
1961 v32i8 = LLVMVectorType(i8, 32);
1962
1963 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1964 emit_data->args[1], v32i8, "");
1965 if (opcode != TGSI_OPCODE_TXF) {
1966 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1967 emit_data->args[2], v16i8, "");
1968 }
1969
Marek Olšák877bb522014-06-25 03:12:46 +02001970 sprintf(intr_name, "%s.v%ui32", name,
1971 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1972
1973 emit_data->output[emit_data->chan] = build_intrinsic(
1974 base->gallivm->builder, intr_name, emit_data->dst_type,
1975 emit_data->args, emit_data->arg_count,
1976 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1977 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001978}
1979
Michel Dänzer0495adb2013-05-06 12:45:14 +02001980static void txq_fetch_args(
1981 struct lp_build_tgsi_context * bld_base,
1982 struct lp_build_emit_data * emit_data)
1983{
1984 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1985 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001986 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02001987 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001988
Marek Olšák2484daa2014-04-22 21:23:29 +02001989 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001990 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1991 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1992
1993 /* Read the size from the buffer descriptor directly. */
1994 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1995 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1996 size = LLVMBuildExtractElement(gallivm->builder, size,
1997 lp_build_const_int32(gallivm, 2), "");
1998 emit_data->args[0] = size;
1999 return;
2000 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002001
2002 /* Mip level */
2003 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2004
2005 /* Resource */
2006 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2007
Marek Olšák2484daa2014-04-22 21:23:29 +02002008 /* Texture target */
2009 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2010 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2011 target = TGSI_TEXTURE_2D_ARRAY;
2012
Michel Dänzer0495adb2013-05-06 12:45:14 +02002013 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002014 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002015
2016 emit_data->arg_count = 3;
2017
2018 emit_data->dst_type = LLVMVectorType(
2019 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2020 4);
2021}
2022
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002023static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2024 struct lp_build_tgsi_context * bld_base,
2025 struct lp_build_emit_data * emit_data)
2026{
Marek Olšák2484daa2014-04-22 21:23:29 +02002027 unsigned target = emit_data->inst->Texture.Texture;
2028
2029 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002030 /* Just return the buffer size. */
2031 emit_data->output[emit_data->chan] = emit_data->args[0];
2032 return;
2033 }
2034
2035 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002036
2037 /* Divide the number of layers by 6 to get the number of cubes. */
2038 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2039 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2040 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2041 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2042 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2043
2044 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2045 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2046 z = LLVMBuildSDiv(builder, z, six, "");
2047
2048 emit_data->output[emit_data->chan] =
2049 LLVMBuildInsertElement(builder, v4, z, two, "");
2050 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002051}
2052
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002053static void si_llvm_emit_ddxy(
2054 const struct lp_build_tgsi_action * action,
2055 struct lp_build_tgsi_context * bld_base,
2056 struct lp_build_emit_data * emit_data)
2057{
2058 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2059 struct gallivm_state *gallivm = bld_base->base.gallivm;
2060 struct lp_build_context * base = &bld_base->base;
2061 const struct tgsi_full_instruction *inst = emit_data->inst;
2062 unsigned opcode = inst->Instruction.Opcode;
2063 LLVMValueRef indices[2];
2064 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2065 LLVMValueRef tl, trbl, result[4];
2066 LLVMTypeRef i32;
2067 unsigned swizzle[4];
2068 unsigned c;
2069
2070 i32 = LLVMInt32TypeInContext(gallivm->context);
2071
2072 indices[0] = bld_base->uint_bld.zero;
2073 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2074 NULL, 0, LLVMReadNoneAttribute);
2075 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2076 indices, 2, "");
2077
2078 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2079 lp_build_const_int32(gallivm, 0xfffffffc), "");
2080 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2081 indices, 2, "");
2082
2083 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2084 lp_build_const_int32(gallivm,
2085 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2086 "");
2087 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2088 indices, 2, "");
2089
2090 for (c = 0; c < 4; ++c) {
2091 unsigned i;
2092
2093 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2094 for (i = 0; i < c; ++i) {
2095 if (swizzle[i] == swizzle[c]) {
2096 result[c] = result[i];
2097 break;
2098 }
2099 }
2100 if (i != c)
2101 continue;
2102
2103 LLVMBuildStore(gallivm->builder,
2104 LLVMBuildBitCast(gallivm->builder,
2105 lp_build_emit_fetch(bld_base, inst, 0, c),
2106 i32, ""),
2107 store_ptr);
2108
2109 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2110 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2111
2112 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2113 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2114
2115 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2116 }
2117
2118 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2119}
2120
Michel Dänzer404b29d2013-11-21 16:45:28 +09002121/* Emit one vertex from the geometry shader */
2122static void si_llvm_emit_vertex(
2123 const struct lp_build_tgsi_action *action,
2124 struct lp_build_tgsi_context *bld_base,
2125 struct lp_build_emit_data *emit_data)
2126{
2127 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002128 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002129 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002130 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002131 struct gallivm_state *gallivm = bld_base->base.gallivm;
2132 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002133 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2134 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002135 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002136 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002137 LLVMValueRef args[2];
2138 unsigned chan;
2139 int i;
2140
Michel Dänzer404b29d2013-11-21 16:45:28 +09002141 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002142 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002143
2144 /* If this thread has already emitted the declared maximum number of
2145 * vertices, kill it: excessive vertex emissions are not supposed to
2146 * have any effect, and GS threads have no externally observable
2147 * effects other than emitting vertices.
2148 */
2149 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2150 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002151 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002152 kill = lp_build_select(&bld_base->base, can_emit,
2153 lp_build_const_float(gallivm, 1.0f),
2154 lp_build_const_float(gallivm, -1.0f));
2155 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2156 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2157
Marek Olšák02134cf2014-10-04 22:07:50 +02002158 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002159 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002160 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002161
2162 for (chan = 0; chan < 4; chan++) {
2163 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002164 LLVMValueRef voffset =
2165 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002166 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002167
2168 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2169 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002170
2171 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2172
Marek Olšákfc3b3352014-10-05 13:33:40 +02002173 build_tbuffer_store(si_shader_ctx,
2174 si_shader_ctx->gsvs_ring,
2175 out_val, 1,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002176 voffset, soffset, 0,
2177 V_008F0C_BUF_DATA_FORMAT_32,
2178 V_008F0C_BUF_NUM_FORMAT_UINT,
2179 1, 0, 1, 1, 0);
2180 }
2181 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002182 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2183 lp_build_const_int32(gallivm, 1));
2184 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002185
2186 /* Signal vertex emission */
2187 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2188 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2189 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2190 LLVMVoidTypeInContext(gallivm->context), args, 2,
2191 LLVMNoUnwindAttribute);
2192}
2193
2194/* Cut one primitive from the geometry shader */
2195static void si_llvm_emit_primitive(
2196 const struct lp_build_tgsi_action *action,
2197 struct lp_build_tgsi_context *bld_base,
2198 struct lp_build_emit_data *emit_data)
2199{
2200 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2201 struct gallivm_state *gallivm = bld_base->base.gallivm;
2202 LLVMValueRef args[2];
2203
2204 /* Signal primitive cut */
2205 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2206 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2207 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2208 LLVMVoidTypeInContext(gallivm->context), args, 2,
2209 LLVMNoUnwindAttribute);
2210}
2211
Tom Stellarda75c6162012-01-06 17:38:37 -05002212static const struct lp_build_tgsi_action tex_action = {
2213 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002214 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002215};
2216
Michel Dänzer0495adb2013-05-06 12:45:14 +02002217static const struct lp_build_tgsi_action txq_action = {
2218 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002219 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002220 .intr_name = "llvm.SI.resinfo"
2221};
2222
Christian König206f0592013-03-20 14:37:21 +01002223static void create_meta_data(struct si_shader_context *si_shader_ctx)
2224{
2225 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2226 LLVMValueRef args[3];
2227
2228 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2229 args[1] = 0;
2230 args[2] = lp_build_const_int32(gallivm, 1);
2231
2232 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2233}
2234
Marek Olšák4f3f0432014-07-07 22:49:15 +02002235static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2236{
2237 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2238 CONST_ADDR_SPACE);
2239}
2240
Christian König55fe5cc2013-03-04 16:30:06 +01002241static void create_function(struct si_shader_context *si_shader_ctx)
2242{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002243 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2244 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002245 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002246 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002247 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002248
Christian König55fe5cc2013-03-04 16:30:06 +01002249 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002250 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002251 f32 = LLVMFloatTypeInContext(gallivm->context);
2252 v2i32 = LLVMVectorType(i32, 2);
2253 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002254 v4i32 = LLVMVectorType(i32, 4);
2255 v8i32 = LLVMVectorType(i32, 8);
2256 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002257
Marek Olšákee2a8182014-07-07 23:27:19 +02002258 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002259 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002260 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2261 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002262 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002263
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002264 switch (si_shader_ctx->type) {
2265 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002266 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002267 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002268 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002269 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002270 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002271
Michel Dänzer404b29d2013-11-21 16:45:28 +09002272 if (shader->key.vs.as_es) {
2273 params[SI_PARAM_ES2GS_OFFSET] = i32;
2274 num_params++;
2275 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002276 if (shader->is_gs_copy_shader) {
2277 last_array_pointer = SI_PARAM_CONST;
2278 num_params = SI_PARAM_CONST+1;
2279 }
2280
Michel Dänzer404b29d2013-11-21 16:45:28 +09002281 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002282
Michel Dänzer404b29d2013-11-21 16:45:28 +09002283 /* Streamout SGPRs. */
2284 if (shader->selector->so.num_outputs) {
2285 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2286 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2287 }
2288 /* A streamout buffer offset is loaded if the stride is non-zero. */
2289 for (i = 0; i < 4; i++) {
2290 if (!shader->selector->so.stride[i])
2291 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002292
Michel Dänzer404b29d2013-11-21 16:45:28 +09002293 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2294 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002295 }
2296
2297 last_sgpr = num_params-1;
2298
2299 /* VGPRs */
2300 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2301 params[num_params++] = i32; /* unused*/
2302 params[num_params++] = i32; /* unused */
2303 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002304 break;
Christian König0666ffd2013-03-05 15:07:39 +01002305
Michel Dänzer404b29d2013-11-21 16:45:28 +09002306 case TGSI_PROCESSOR_GEOMETRY:
2307 params[SI_PARAM_GS2VS_OFFSET] = i32;
2308 params[SI_PARAM_GS_WAVE_ID] = i32;
2309 last_sgpr = SI_PARAM_GS_WAVE_ID;
2310
2311 /* VGPRs */
2312 params[SI_PARAM_VTX0_OFFSET] = i32;
2313 params[SI_PARAM_VTX1_OFFSET] = i32;
2314 params[SI_PARAM_PRIMITIVE_ID] = i32;
2315 params[SI_PARAM_VTX2_OFFSET] = i32;
2316 params[SI_PARAM_VTX3_OFFSET] = i32;
2317 params[SI_PARAM_VTX4_OFFSET] = i32;
2318 params[SI_PARAM_VTX5_OFFSET] = i32;
2319 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2320 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2321 break;
2322
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002323 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002324 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002325 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002326 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002327 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2328 params[SI_PARAM_PERSP_CENTER] = v2i32;
2329 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2330 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2331 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2332 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2333 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2334 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2335 params[SI_PARAM_POS_X_FLOAT] = f32;
2336 params[SI_PARAM_POS_Y_FLOAT] = f32;
2337 params[SI_PARAM_POS_Z_FLOAT] = f32;
2338 params[SI_PARAM_POS_W_FLOAT] = f32;
2339 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002340 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002341 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2342 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002343 num_params = SI_PARAM_POS_FIXED_PT+1;
2344 break;
2345
2346 default:
2347 assert(0 && "unimplemented shader");
2348 return;
Christian Königc4973212013-03-05 12:14:02 +01002349 }
Christian König55fe5cc2013-03-04 16:30:06 +01002350
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002351 assert(num_params <= Elements(params));
2352 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002353 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002354
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002355 for (i = 0; i <= last_sgpr; ++i) {
2356 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002357
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002358 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2359 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002360 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002361 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002362 else
2363 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002364 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002365
Michel Dänzer404b29d2013-11-21 16:45:28 +09002366 if (bld_base->info &&
2367 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2368 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002369 si_shader_ctx->ddxy_lds =
2370 LLVMAddGlobalInAddressSpace(gallivm->module,
2371 LLVMArrayType(i32, 64),
2372 "ddxy_lds",
2373 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002374}
Tom Stellarda75c6162012-01-06 17:38:37 -05002375
Christian König0f6cf2b2013-03-15 15:53:25 +01002376static void preload_constants(struct si_shader_context *si_shader_ctx)
2377{
2378 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2379 struct gallivm_state * gallivm = bld_base->base.gallivm;
2380 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002381 unsigned buf;
2382 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002383
Marek Olšákee2a8182014-07-07 23:27:19 +02002384 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002385 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002386
Marek Olšák2fd42002013-10-25 11:45:47 +02002387 if (num_const == 0)
2388 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002389
Marek Olšák2fd42002013-10-25 11:45:47 +02002390 /* Allocate space for the constant values */
2391 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002392
Marek Olšák2fd42002013-10-25 11:45:47 +02002393 /* Load the resource descriptor */
2394 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002395 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002396
Marek Olšák2fd42002013-10-25 11:45:47 +02002397 /* Load the constants, we rely on the code sinking to do the rest */
2398 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002399 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002400 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002401 si_shader_ctx->const_resource[buf],
2402 lp_build_const_int32(gallivm, i * 4),
2403 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002404 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002405 }
2406}
2407
Christian König1c100182013-03-17 16:02:42 +01002408static void preload_samplers(struct si_shader_context *si_shader_ctx)
2409{
2410 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2411 struct gallivm_state * gallivm = bld_base->base.gallivm;
2412 const struct tgsi_shader_info * info = bld_base->info;
2413
2414 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2415
2416 LLVMValueRef res_ptr, samp_ptr;
2417 LLVMValueRef offset;
2418
2419 if (num_samplers == 0)
2420 return;
2421
Christian König1c100182013-03-17 16:02:42 +01002422 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2423 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2424
2425 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2426 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002427 /* Resource */
2428 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002429 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002430
2431 /* Sampler */
2432 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002433 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002434
2435 /* FMASK resource */
2436 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002437 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2438 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002439 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002440 }
Christian König1c100182013-03-17 16:02:42 +01002441 }
2442}
2443
Marek Olšák8d03d922013-09-01 23:59:06 +02002444static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2445{
2446 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2447 struct gallivm_state * gallivm = bld_base->base.gallivm;
2448 unsigned i;
2449
Michel Dänzer67e385b2014-01-08 17:48:21 +09002450 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2451 si_shader_ctx->shader->key.vs.as_es ||
2452 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002453 return;
2454
2455 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002456 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002457
2458 /* Load the resources, we rely on the code sinking to do the rest */
2459 for (i = 0; i < 4; ++i) {
2460 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002461 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002462 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002463
Marek Olšákd7876082014-09-26 23:06:32 +02002464 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002465 }
2466 }
2467}
2468
Marek Olšákfc3b3352014-10-05 13:33:40 +02002469/**
2470 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
2471 * for later use.
2472 */
2473static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
2474{
2475 struct gallivm_state *gallivm =
2476 si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2477
2478 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2479 SI_PARAM_RW_BUFFERS);
2480
2481 if ((si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
2482 si_shader_ctx->shader->key.vs.as_es) ||
2483 si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY) {
2484 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_ESGS);
2485
2486 si_shader_ctx->esgs_ring =
2487 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2488 }
2489
2490 if (si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY ||
2491 si_shader_ctx->shader->is_gs_copy_shader) {
2492 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_GSVS);
2493
2494 si_shader_ctx->gsvs_ring =
2495 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2496 }
2497}
2498
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002499void si_shader_binary_read_config(const struct radeon_shader_binary *binary,
2500 struct si_shader *shader,
2501 unsigned symbol_offset)
Tom Stellard302f53d2012-10-25 13:50:10 -04002502{
Tom Stellard302f53d2012-10-25 13:50:10 -04002503 unsigned i;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002504 const unsigned char *config =
2505 radeon_shader_binary_config_start(binary, symbol_offset);
Tom Stellard302f53d2012-10-25 13:50:10 -04002506
Tom Stellardd50343d2013-04-04 16:21:06 -04002507 /* XXX: We may be able to emit some of these values directly rather than
2508 * extracting fields to be emitted later.
2509 */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002510
2511 for (i = 0; i < binary->config_size_per_symbol; i+= 8) {
2512 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
2513 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
Tom Stellardd50343d2013-04-04 16:21:06 -04002514 switch (reg) {
2515 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2516 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2517 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2518 case R_00B848_COMPUTE_PGM_RSRC1:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002519 shader->num_sgprs = MAX2(shader->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
2520 shader->num_vgprs = MAX2(shader->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
Tom Stellardd50343d2013-04-04 16:21:06 -04002521 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002522 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002523 shader->lds_size = MAX2(shader->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002524 break;
2525 case R_00B84C_COMPUTE_PGM_RSRC2:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002526 shader->lds_size = MAX2(shader->lds_size, G_00B84C_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002527 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002528 case R_0286CC_SPI_PS_INPUT_ENA:
2529 shader->spi_ps_input_ena = value;
2530 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002531 case R_00B860_COMPUTE_TMPRING_SIZE:
2532 /* WAVESIZE is in units of 256 dwords. */
2533 shader->scratch_bytes_per_wave =
2534 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2535 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002536 default:
2537 fprintf(stderr, "Warning: Compiler emitted unknown "
2538 "config register: 0x%x\n", reg);
2539 break;
2540 }
2541 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002542}
2543
2544int si_shader_binary_read(struct si_screen *sscreen,
2545 struct si_shader *shader,
2546 const struct radeon_shader_binary *binary)
2547{
2548
2549 unsigned i;
2550 unsigned code_size;
2551 unsigned char *ptr;
2552 bool dump = r600_can_dump_shader(&sscreen->b,
2553 shader->selector ? shader->selector->tokens : NULL);
2554
2555 if (dump && !binary->disassembled) {
2556 fprintf(stderr, "SI CODE:\n");
2557 for (i = 0; i < binary->code_size; i+=4 ) {
2558 fprintf(stderr, "@0x%x: %02x%02x%02x%02x\n", i, binary->code[i + 3],
2559 binary->code[i + 2], binary->code[i + 1],
2560 binary->code[i]);
2561 }
2562 }
2563
2564 si_shader_binary_read_config(binary, shader, 0);
Tom Stellard302f53d2012-10-25 13:50:10 -04002565
2566 /* copy new shader */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002567 code_size = binary->code_size + binary->rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002568 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002569 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002570 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002571 if (shader->bo == NULL) {
2572 return -ENOMEM;
2573 }
2574
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002575
2576 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_READ_WRITE);
2577 util_memcpy_cpu_to_le32(ptr, binary->code, binary->code_size);
2578 if (binary->rodata_size > 0) {
2579 ptr += binary->code_size;
2580 util_memcpy_cpu_to_le32(ptr, binary->rodata, binary->rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002581 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002582
Marek Olšák1abb1a92014-09-17 22:44:22 +02002583 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002584
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002585 return 0;
2586}
Tom Stellard302f53d2012-10-25 13:50:10 -04002587
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002588int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2589 LLVMModuleRef mod)
2590{
2591 int r = 0;
2592 struct radeon_shader_binary binary;
2593 bool dump = r600_can_dump_shader(&sscreen->b,
2594 shader->selector ? shader->selector->tokens : NULL);
2595 memset(&binary, 0, sizeof(binary));
2596 r = radeon_llvm_compile(mod, &binary,
Tom Stellard761e36b2014-10-15 12:24:30 -04002597 r600_get_llvm_processor_name(sscreen->b.family), dump, sscreen->tm);
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002598
2599 if (r) {
2600 return r;
2601 }
2602 r = si_shader_binary_read(sscreen, shader, &binary);
2603 FREE(binary.code);
2604 FREE(binary.config);
2605 FREE(binary.rodata);
Darren Powellbc866902014-03-31 18:00:28 -04002606 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002607}
2608
Michel Dänzer404b29d2013-11-21 16:45:28 +09002609/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002610static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002611 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002612 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002613{
2614 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2615 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2616 struct lp_build_context *base = &bld_base->base;
2617 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002618 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002619 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002620 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002621 LLVMValueRef args[9];
2622 int i, r;
2623
Marek Olšák02134cf2014-10-04 22:07:50 +02002624 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002625
2626 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002627 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002628
2629 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2630
2631 create_meta_data(si_shader_ctx);
2632 create_function(si_shader_ctx);
2633 preload_streamout_buffers(si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002634 preload_ring_buffers(si_shader_ctx);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002635
Marek Olšákfc3b3352014-10-05 13:33:40 +02002636 args[0] = si_shader_ctx->gsvs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002637 args[1] = lp_build_mul_imm(uint,
2638 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2639 si_shader_ctx->param_vertex_id),
2640 4);
2641 args[3] = uint->zero;
2642 args[4] = uint->one; /* OFFEN */
2643 args[5] = uint->zero; /* IDXEN */
2644 args[6] = uint->one; /* GLC */
2645 args[7] = uint->one; /* SLC */
2646 args[8] = uint->zero; /* TFE */
2647
2648 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002649 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002650 unsigned chan;
2651
Marek Olšák02134cf2014-10-04 22:07:50 +02002652 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002653 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002654
2655 for (chan = 0; chan < 4; chan++) {
2656 args[2] = lp_build_const_int32(gallivm,
2657 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002658 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002659
2660 outputs[i].values[chan] =
2661 LLVMBuildBitCast(gallivm->builder,
2662 build_intrinsic(gallivm->builder,
2663 "llvm.SI.buffer.load.dword.i32.i32",
2664 LLVMInt32TypeInContext(gallivm->context),
2665 args, 9,
2666 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2667 base->elem_type, "");
2668 }
2669 }
2670
Marek Olšák02134cf2014-10-04 22:07:50 +02002671 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002672
2673 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2674
2675 if (dump)
2676 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2677
Marek Olšák1abb1a92014-09-17 22:44:22 +02002678 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002679 bld_base->base.gallivm->module);
2680
2681 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2682
2683 FREE(outputs);
2684 return r;
2685}
2686
Marek Olšák1abb1a92014-09-17 22:44:22 +02002687int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002688{
Marek Olšák2774abd2014-09-16 18:45:33 +02002689 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002690 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002691 struct lp_build_tgsi_context * bld_base;
2692 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002693 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002694 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002695
2696 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2697 * conversion fails. */
2698 if (dump) {
2699 tgsi_dump(sel->tokens, 0);
2700 si_dump_streamout(&sel->so);
2701 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002702
Marek Olšák8c37c162014-09-16 18:40:07 +02002703 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002704
Michel Dänzercfebaf92012-08-31 19:04:08 +02002705 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002706 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2707 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2708
Marek Olšák52335682014-09-30 15:12:09 +02002709 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002710 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2711
Marek Olšák52335682014-09-30 15:12:09 +02002712 shader->uses_instanceid = sel->info.uses_instanceid;
2713 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002714 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002715
2716 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002717 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002718 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2719 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002720 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002721 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2722 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2723 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002724 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002725 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002726 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2727 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002728
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002729 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2730 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002731
Michel Dänzer404b29d2013-11-21 16:45:28 +09002732 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2733 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2734
Marek Olšák645b4712014-11-20 22:16:09 +01002735 if (HAVE_LLVM >= 0x0306) {
2736 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
2737 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
2738 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
2739 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
2740 }
2741
Christian Könige4ed5872013-03-21 18:02:52 +01002742 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002743 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002744 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2745 si_shader_ctx.shader = shader;
2746 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Marek Olšák368b0a72014-12-28 21:51:35 +01002747 si_shader_ctx.screen = sscreen;
Tom Stellarda75c6162012-01-06 17:38:37 -05002748
Michel Dänzer51f89a02013-12-09 15:33:53 +09002749 switch (si_shader_ctx.type) {
2750 case TGSI_PROCESSOR_VERTEX:
2751 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002752 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002753 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2754 } else {
2755 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2756 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002757 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002758 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002759 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2760 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002761 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002762 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002763 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2764 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002765
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002766 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002767 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2768 shader->db_shader_control |=
2769 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2770 break;
2771 case TGSI_FS_DEPTH_LAYOUT_LESS:
2772 shader->db_shader_control |=
2773 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2774 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002775 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002776 break;
2777 default:
2778 assert(!"Unsupported shader type");
2779 return -1;
2780 }
2781
Christian König206f0592013-03-20 14:37:21 +01002782 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002783 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002784 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002785 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002786 preload_streamout_buffers(&si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002787 preload_ring_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002788
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002789 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2790 si_shader_ctx.gs_next_vertex =
2791 lp_build_alloca(bld_base->base.gallivm,
2792 bld_base->uint_bld.elem_type, "");
2793 }
2794
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002795 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002796 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002797 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002798 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002799
2800 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2801
2802 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002803 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002804 if (r) {
2805 fprintf(stderr, "LLVM failed to compile shader\n");
2806 goto out;
2807 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002808
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002809 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002810
2811 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002812 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002813 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002814 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002815 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002816 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2817 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002818 free(shader->gs_copy_shader);
2819 shader->gs_copy_shader = NULL;
2820 goto out;
2821 }
2822 }
2823
Tom Stellarda75c6162012-01-06 17:38:37 -05002824 tgsi_parse_free(&si_shader_ctx.parse);
2825
Michel Dänzer404b29d2013-11-21 16:45:28 +09002826out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002827 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002828 FREE(si_shader_ctx.constants[i]);
Tom Stellarda75c6162012-01-06 17:38:37 -05002829
Tom Stellard302f53d2012-10-25 13:50:10 -04002830 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002831}
2832
Marek Olšák2774abd2014-09-16 18:45:33 +02002833void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002834{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002835 if (shader->gs_copy_shader)
2836 si_shader_destroy(ctx, shader->gs_copy_shader);
2837
Marek Olšáka81c3e02013-08-14 01:04:39 +02002838 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002839 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002840}