blob: cf28860a35f6cb8360d264ba8fbd485354cf1c67 [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:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200456 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200457 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200458 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400459 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100460 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200461 else
Christian König0666ffd2013-03-05 15:07:39 +0100462 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500463 break;
464 default:
465 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
466 return;
467 }
468
Marek Olšák13de9472015-01-04 20:09:51 +0100469 /* fs.constant returns the param from the middle vertex, so it's not
470 * really useful for flat shading. It's meant to be used for custom
471 * interpolation (but the intrinsic can't fetch from the other two
472 * vertices).
473 *
474 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
475 * to do the right thing. The only reason we use fs.constant is that
476 * fs.interp cannot be used on integers, because they can be equal
477 * to NaN.
478 */
Christian König0666ffd2013-03-05 15:07:39 +0100479 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
480
Michel Dänzer691f08d2012-09-06 18:03:38 +0200481 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100482 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100483 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200484 LLVMValueRef face, is_face_positive;
485 LLVMValueRef back_attr_number =
486 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200487 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200488
Christian König0666ffd2013-03-05 15:07:39 +0100489 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
490
Michel Dänzer691f08d2012-09-06 18:03:38 +0200491 is_face_positive = LLVMBuildFCmp(gallivm->builder,
Marek Olšákd1d2af22015-01-04 20:45:35 +0100492 LLVMRealOGT, face,
Michel Dänzer691f08d2012-09-06 18:03:38 +0200493 lp_build_const_float(gallivm, 0.0f),
494 "");
495
Tom Stellarda75c6162012-01-06 17:38:37 -0500496 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100497 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200498 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
499 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
500 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
501 LLVMValueRef front, back;
502
503 args[0] = llvm_chan;
504 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900505 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100506 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100507 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200508
509 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900510 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100511 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100512 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200513
Michel Dänzer51f89a02013-12-09 15:33:53 +0900514 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200515 LLVMBuildSelect(gallivm->builder,
516 is_face_positive,
517 front,
518 back,
519 "");
520 }
521
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900522 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200523 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
524 LLVMValueRef args[4];
525
526 args[0] = uint->zero;
527 args[1] = attr_number;
528 args[2] = params;
529 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900530 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
531 build_intrinsic(gallivm->builder, intr_name,
532 input_type, args, args[3] ? 4 : 3,
533 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
534 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
535 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200536 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900537 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200538 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200539 } else {
540 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100541 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200542 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
543 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
544 args[0] = llvm_chan;
545 args[1] = attr_number;
546 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100547 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900548 radeon_bld->inputs[soa_index] =
549 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100550 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100551 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200552 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500553 }
554}
555
Marek Olšák5b06fc32014-05-06 18:12:40 +0200556static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
557{
558 struct gallivm_state *gallivm = &radeon_bld->gallivm;
559 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
560 SI_PARAM_ANCILLARY);
561 value = LLVMBuildLShr(gallivm->builder, value,
562 lp_build_const_int32(gallivm, 8), "");
563 value = LLVMBuildAnd(gallivm->builder, value,
564 lp_build_const_int32(gallivm, 0xf), "");
565 return value;
566}
567
Marek Olšákd7876082014-09-26 23:06:32 +0200568/**
569 * Load a dword from a constant buffer.
570 */
571static LLVMValueRef buffer_load_const(LLVMBuilderRef builder, LLVMValueRef resource,
572 LLVMValueRef offset, LLVMTypeRef return_type)
Marek Olšák250aa932014-05-06 14:10:47 +0200573{
574 LLVMValueRef args[2] = {resource, offset};
575
576 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
577 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
578}
579
Christian Könige4ed5872013-03-21 18:02:52 +0100580static void declare_system_value(
581 struct radeon_llvm_context * radeon_bld,
582 unsigned index,
583 const struct tgsi_full_declaration *decl)
584{
Marek Olšák8d03d922013-09-01 23:59:06 +0200585 struct si_shader_context *si_shader_ctx =
586 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200587 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
588 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100589 LLVMValueRef value = 0;
590
591 switch (decl->Semantic.Name) {
592 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200593 value = LLVMGetParam(radeon_bld->main_fn,
594 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100595 break;
596
597 case TGSI_SEMANTIC_VERTEXID:
Marek Olšákd7c6f392015-01-04 14:41:49 +0100598 value = LLVMBuildAdd(gallivm->builder,
599 LLVMGetParam(radeon_bld->main_fn,
600 si_shader_ctx->param_vertex_id),
601 LLVMGetParam(radeon_bld->main_fn,
602 SI_PARAM_BASE_VERTEX), "");
Christian Könige4ed5872013-03-21 18:02:52 +0100603 break;
604
Marek Olšáke3d4bdd2015-01-04 14:51:01 +0100605 case TGSI_SEMANTIC_VERTEXID_NOBASE:
606 value = LLVMGetParam(radeon_bld->main_fn,
607 si_shader_ctx->param_vertex_id);
608 break;
609
610 case TGSI_SEMANTIC_BASEVERTEX:
611 value = LLVMGetParam(radeon_bld->main_fn,
612 SI_PARAM_BASE_VERTEX);
613 break;
614
Marek Olšák5b06fc32014-05-06 18:12:40 +0200615 case TGSI_SEMANTIC_SAMPLEID:
616 value = get_sample_id(radeon_bld);
617 break;
618
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200619 case TGSI_SEMANTIC_SAMPLEPOS:
620 {
621 LLVMBuilderRef builder = gallivm->builder;
622 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200623 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200624 LLVMValueRef resource = build_indexed_load_const(si_shader_ctx, desc, buf_index);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200625
626 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
627 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
628 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
629
630 LLVMValueRef pos[4] = {
Marek Olšákd7876082014-09-26 23:06:32 +0200631 buffer_load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
632 buffer_load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200633 lp_build_const_float(gallivm, 0),
634 lp_build_const_float(gallivm, 0)
635 };
636 value = lp_build_gather_values(gallivm, pos, 4);
637 break;
638 }
639
Christian Könige4ed5872013-03-21 18:02:52 +0100640 default:
641 assert(!"unknown system value");
642 return;
643 }
644
645 radeon_bld->system_values[index] = value;
646}
647
Tom Stellarda75c6162012-01-06 17:38:37 -0500648static LLVMValueRef fetch_constant(
649 struct lp_build_tgsi_context * bld_base,
650 const struct tgsi_full_src_register *reg,
651 enum tgsi_opcode_type type,
652 unsigned swizzle)
653{
Christian König55fe5cc2013-03-04 16:30:06 +0100654 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500655 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100656 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200657 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500658
Christian König0f6cf2b2013-03-15 15:53:25 +0100659 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100660 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500661
Christian König8514f5a2013-02-04 17:46:42 +0100662 if (swizzle == LP_CHAN_ALL) {
663 unsigned chan;
664 LLVMValueRef values[4];
665 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
666 values[chan] = fetch_constant(bld_base, reg, type, chan);
667
668 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
669 }
670
Marek Olšák2fd42002013-10-25 11:45:47 +0200671 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100672 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100673
Marek Olšák2fd42002013-10-25 11:45:47 +0200674 if (!reg->Register.Indirect)
675 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
676
Christian König0f6cf2b2013-03-15 15:53:25 +0100677 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
678 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
679 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200680 addr = lp_build_add(&bld_base->uint_bld, addr,
681 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200682
Marek Olšákd7876082014-09-26 23:06:32 +0200683 result = buffer_load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
Marek Olšák250aa932014-05-06 14:10:47 +0200684 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500685
Christian Königf5298b02013-02-28 14:50:07 +0100686 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500687}
688
Michel Dänzer26c71392012-08-24 12:03:11 +0200689/* Initialize arguments for the shader export intrinsic */
690static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900691 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200692 unsigned target,
693 LLVMValueRef *args)
694{
695 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
696 struct lp_build_context *uint =
697 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
698 struct lp_build_context *base = &bld_base->base;
699 unsigned compressed = 0;
700 unsigned chan;
701
Michel Dänzerf402acd2012-08-22 18:15:36 +0200702 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
703 int cbuf = target - V_008DFC_SQ_EXP_MRT;
704
705 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100706 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100707
708 if (compressed)
709 si_shader_ctx->shader->spi_shader_col_format |=
710 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
711 else
712 si_shader_ctx->shader->spi_shader_col_format |=
713 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200714
715 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200716 }
717 }
718
719 if (compressed) {
720 /* Pixel shader needs to pack output values before export */
721 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900722 args[0] = values[2 * chan];
723 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200724 args[chan + 5] =
725 build_intrinsic(base->gallivm->builder,
726 "llvm.SI.packf16",
727 LLVMInt32TypeInContext(base->gallivm->context),
728 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100729 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100730 args[chan + 7] = args[chan + 5] =
731 LLVMBuildBitCast(base->gallivm->builder,
732 args[chan + 5],
733 LLVMFloatTypeInContext(base->gallivm->context),
734 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200735 }
736
737 /* Set COMPR flag */
738 args[4] = uint->one;
739 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900740 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200741 /* +5 because the first output value will be
742 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900743 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200744
745 /* Clear COMPR flag */
746 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200747 }
748
749 /* XXX: This controls which components of the output
750 * registers actually get exported. (e.g bit 0 means export
751 * X component, bit 1 means export Y component, etc.) I'm
752 * hard coding this to 0xf for now. In the future, we might
753 * want to do something else. */
754 args[0] = lp_build_const_int32(base->gallivm, 0xf);
755
756 /* Specify whether the EXEC mask represents the valid mask */
757 args[1] = uint->zero;
758
759 /* Specify whether this is the last export */
760 args[2] = uint->zero;
761
762 /* Specify the target we are exporting */
763 args[3] = lp_build_const_int32(base->gallivm, target);
764
Michel Dänzer26c71392012-08-24 12:03:11 +0200765 /* XXX: We probably need to keep track of the output
766 * values, so we know what we are passing to the next
767 * stage. */
768}
769
Michel Dänzer404b29d2013-11-21 16:45:28 +0900770/* Load from output pointers and initialize arguments for the shader export intrinsic */
771static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
772 LLVMValueRef *out_ptr,
773 unsigned target,
774 LLVMValueRef *args)
775{
776 struct gallivm_state *gallivm = bld_base->base.gallivm;
777 LLVMValueRef values[4];
778 int i;
779
780 for (i = 0; i < 4; i++)
781 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
782
783 si_llvm_init_export_args(bld_base, values, target, args);
784}
785
Michel Dänzer7708a862012-11-02 15:57:30 +0100786static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900787 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100788{
789 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
790 struct gallivm_state *gallivm = bld_base->base.gallivm;
791
Christian Königa0dca442013-03-22 15:59:22 +0100792 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400793 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
794 SI_PARAM_ALPHA_REF);
795
Michel Dänzer7708a862012-11-02 15:57:30 +0100796 LLVMValueRef alpha_pass =
797 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100798 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900799 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400800 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100801 LLVMValueRef arg =
802 lp_build_select(&bld_base->base,
803 alpha_pass,
804 lp_build_const_float(gallivm, 1.0f),
805 lp_build_const_float(gallivm, -1.0f));
806
807 build_intrinsic(gallivm->builder,
808 "llvm.AMDGPU.kill",
809 LLVMVoidTypeInContext(gallivm->context),
810 &arg, 1, 0);
811 } else {
812 build_intrinsic(gallivm->builder,
813 "llvm.AMDGPU.kilp",
814 LLVMVoidTypeInContext(gallivm->context),
815 NULL, 0, 0);
816 }
Marek Olšákadc57972014-09-19 17:07:07 +0200817
818 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100819}
820
Michel Dänzere3befbc2013-05-15 18:09:50 +0200821static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900822 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200823{
824 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
825 struct lp_build_context *base = &bld_base->base;
826 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200827 unsigned reg_index;
828 unsigned chan;
829 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200830 LLVMValueRef base_elt;
831 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200832 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200833 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200834
Michel Dänzere3befbc2013-05-15 18:09:50 +0200835 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200836 LLVMValueRef *args = pos[2 + reg_index];
837
Michel Dänzere3befbc2013-05-15 18:09:50 +0200838 args[5] =
839 args[6] =
840 args[7] =
841 args[8] = lp_build_const_float(base->gallivm, 0.0f);
842
843 /* Compute dot products of position and user clip plane vectors */
844 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
845 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200846 args[1] = lp_build_const_int32(base->gallivm,
847 ((reg_index * 4 + chan) * 4 +
848 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200849 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200850 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200851 args[5 + chan] =
852 lp_build_add(base, args[5 + chan],
853 lp_build_mul(base, base_elt,
854 out_elts[const_chan]));
855 }
856 }
857
858 args[0] = lp_build_const_int32(base->gallivm, 0xf);
859 args[1] = uint->zero;
860 args[2] = uint->zero;
861 args[3] = lp_build_const_int32(base->gallivm,
862 V_008DFC_SQ_EXP_POS + 2 + reg_index);
863 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200864 }
865}
866
Marek Olšák8d03d922013-09-01 23:59:06 +0200867static void si_dump_streamout(struct pipe_stream_output_info *so)
868{
869 unsigned i;
870
871 if (so->num_outputs)
872 fprintf(stderr, "STREAMOUT\n");
873
874 for (i = 0; i < so->num_outputs; i++) {
875 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
876 so->output[i].start_component;
877 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
878 i, so->output[i].output_buffer,
879 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
880 so->output[i].register_index,
881 mask & 1 ? "x" : "",
882 mask & 2 ? "y" : "",
883 mask & 4 ? "z" : "",
884 mask & 8 ? "w" : "");
885 }
886}
887
888/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
889 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
890 * or v4i32 (num_channels=3,4). */
891static void build_tbuffer_store(struct si_shader_context *shader,
892 LLVMValueRef rsrc,
893 LLVMValueRef vdata,
894 unsigned num_channels,
895 LLVMValueRef vaddr,
896 LLVMValueRef soffset,
897 unsigned inst_offset,
898 unsigned dfmt,
899 unsigned nfmt,
900 unsigned offen,
901 unsigned idxen,
902 unsigned glc,
903 unsigned slc,
904 unsigned tfe)
905{
906 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
907 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
908 LLVMValueRef args[] = {
909 rsrc,
910 vdata,
911 LLVMConstInt(i32, num_channels, 0),
912 vaddr,
913 soffset,
914 LLVMConstInt(i32, inst_offset, 0),
915 LLVMConstInt(i32, dfmt, 0),
916 LLVMConstInt(i32, nfmt, 0),
917 LLVMConstInt(i32, offen, 0),
918 LLVMConstInt(i32, idxen, 0),
919 LLVMConstInt(i32, glc, 0),
920 LLVMConstInt(i32, slc, 0),
921 LLVMConstInt(i32, tfe, 0)
922 };
923
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900924 /* The instruction offset field has 12 bits */
925 assert(offen || inst_offset < (1 << 12));
926
Marek Olšák8d03d922013-09-01 23:59:06 +0200927 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
928 unsigned func = CLAMP(num_channels, 1, 3) - 1;
929 const char *types[] = {"i32", "v2i32", "v4i32"};
930 char name[256];
931 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
932
933 lp_build_intrinsic(gallivm->builder, name,
934 LLVMVoidTypeInContext(gallivm->context),
935 args, Elements(args));
936}
937
938static void build_streamout_store(struct si_shader_context *shader,
939 LLVMValueRef rsrc,
940 LLVMValueRef vdata,
941 unsigned num_channels,
942 LLVMValueRef vaddr,
943 LLVMValueRef soffset,
944 unsigned inst_offset)
945{
946 static unsigned dfmt[] = {
947 V_008F0C_BUF_DATA_FORMAT_32,
948 V_008F0C_BUF_DATA_FORMAT_32_32,
949 V_008F0C_BUF_DATA_FORMAT_32_32_32,
950 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
951 };
952 assert(num_channels >= 1 && num_channels <= 4);
953
954 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
955 inst_offset, dfmt[num_channels-1],
956 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
957}
958
959/* On SI, the vertex shader is responsible for writing streamout data
960 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900961static void si_llvm_emit_streamout(struct si_shader_context *shader,
962 struct si_shader_output_values *outputs,
963 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200964{
965 struct pipe_stream_output_info *so = &shader->shader->selector->so;
966 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
967 LLVMBuilderRef builder = gallivm->builder;
968 int i, j;
969 struct lp_build_if_state if_ctx;
970
971 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
972
973 LLVMValueRef so_param =
974 LLVMGetParam(shader->radeon_bld.main_fn,
975 shader->param_streamout_config);
976
977 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
978 LLVMValueRef so_vtx_count =
979 LLVMBuildAnd(builder,
980 LLVMBuildLShr(builder, so_param,
981 LLVMConstInt(i32, 16, 0), ""),
982 LLVMConstInt(i32, 127, 0), "");
983
984 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
985 NULL, 0, LLVMReadNoneAttribute);
986
987 /* can_emit = tid < so_vtx_count; */
988 LLVMValueRef can_emit =
989 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
990
991 /* Emit the streamout code conditionally. This actually avoids
992 * out-of-bounds buffer access. The hw tells us via the SGPR
993 * (so_vtx_count) which threads are allowed to emit streamout data. */
994 lp_build_if(&if_ctx, gallivm, can_emit);
995 {
996 /* The buffer offset is computed as follows:
997 * ByteOffset = streamout_offset[buffer_id]*4 +
998 * (streamout_write_index + thread_id)*stride[buffer_id] +
999 * attrib_offset
1000 */
1001
1002 LLVMValueRef so_write_index =
1003 LLVMGetParam(shader->radeon_bld.main_fn,
1004 shader->param_streamout_write_index);
1005
1006 /* Compute (streamout_write_index + thread_id). */
1007 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1008
1009 /* Compute the write offset for each enabled buffer. */
1010 LLVMValueRef so_write_offset[4] = {};
1011 for (i = 0; i < 4; i++) {
1012 if (!so->stride[i])
1013 continue;
1014
1015 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1016 shader->param_streamout_offset[i]);
1017 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1018
1019 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1020 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1021 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1022 }
1023
Marek Olšák8d03d922013-09-01 23:59:06 +02001024 /* Write streamout data. */
1025 for (i = 0; i < so->num_outputs; i++) {
1026 unsigned buf_idx = so->output[i].output_buffer;
1027 unsigned reg = so->output[i].register_index;
1028 unsigned start = so->output[i].start_component;
1029 unsigned num_comps = so->output[i].num_components;
1030 LLVMValueRef out[4];
1031
1032 assert(num_comps && num_comps <= 4);
1033 if (!num_comps || num_comps > 4)
1034 continue;
1035
Marek Olšák558f7772014-10-04 22:37:23 +02001036 if (reg >= noutput)
1037 continue;
1038
Marek Olšák8d03d922013-09-01 23:59:06 +02001039 /* Load the output as int. */
1040 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001041 out[j] = LLVMBuildBitCast(builder,
1042 outputs[reg].values[start+j],
1043 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001044 }
1045
1046 /* Pack the output. */
1047 LLVMValueRef vdata = NULL;
1048
1049 switch (num_comps) {
1050 case 1: /* as i32 */
1051 vdata = out[0];
1052 break;
1053 case 2: /* as v2i32 */
1054 case 3: /* as v4i32 (aligned to 4) */
1055 case 4: /* as v4i32 */
1056 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1057 for (j = 0; j < num_comps; j++) {
1058 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1059 LLVMConstInt(i32, j, 0), "");
1060 }
1061 break;
1062 }
1063
1064 build_streamout_store(shader, shader->so_buffers[buf_idx],
1065 vdata, num_comps,
1066 so_write_offset[buf_idx],
1067 LLVMConstInt(i32, 0, 0),
1068 so->output[i].dst_offset*4);
1069 }
1070 }
1071 lp_build_endif(&if_ctx);
1072}
1073
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001074
Michel Dänzer404b29d2013-11-21 16:45:28 +09001075/* Generate export instructions for hardware VS shader stage */
1076static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1077 struct si_shader_output_values *outputs,
1078 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001079{
1080 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001081 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001082 struct lp_build_context * base = &bld_base->base;
1083 struct lp_build_context * uint =
1084 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001085 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001086 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001087 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001088 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001089 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001090 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001091 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001092 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001093
Michel Dänzer67e385b2014-01-08 17:48:21 +09001094 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1095 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001096 }
1097
Michel Dänzer404b29d2013-11-21 16:45:28 +09001098 for (i = 0; i < noutput; i++) {
1099 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001100 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001101
Michel Dänzer0afeea52013-05-02 14:53:17 +02001102handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001103 /* Select the correct target */
1104 switch(semantic_name) {
1105 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001106 psize_value = outputs[i].values[0];
1107 continue;
1108 case TGSI_SEMANTIC_EDGEFLAG:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001109 edgeflag_value = outputs[i].values[0];
1110 continue;
1111 case TGSI_SEMANTIC_LAYER:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001112 layer_value = outputs[i].values[0];
1113 continue;
1114 case TGSI_SEMANTIC_POSITION:
1115 target = V_008DFC_SQ_EXP_POS;
1116 break;
1117 case TGSI_SEMANTIC_COLOR:
1118 case TGSI_SEMANTIC_BCOLOR:
1119 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001120 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001121 param_count++;
1122 break;
1123 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001124 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1125 break;
1126 case TGSI_SEMANTIC_CLIPVERTEX:
1127 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1128 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001129 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001130 case TGSI_SEMANTIC_FOG:
1131 case TGSI_SEMANTIC_GENERIC:
1132 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001133 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001134 param_count++;
1135 break;
1136 default:
1137 target = 0;
1138 fprintf(stderr,
1139 "Warning: SI unhandled vs output type:%d\n",
1140 semantic_name);
1141 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001142
Michel Dänzer404b29d2013-11-21 16:45:28 +09001143 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001144
Michel Dänzer404b29d2013-11-21 16:45:28 +09001145 if (target >= V_008DFC_SQ_EXP_POS &&
1146 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1147 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1148 args, sizeof(args));
1149 } else {
1150 lp_build_intrinsic(base->gallivm->builder,
1151 "llvm.SI.export",
1152 LLVMVoidTypeInContext(base->gallivm->context),
1153 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001154 }
1155
1156 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1157 semantic_name = TGSI_SEMANTIC_GENERIC;
1158 goto handle_semantic;
1159 }
1160 }
1161
1162 /* We need to add the position output manually if it's missing. */
1163 if (!pos_args[0][0]) {
1164 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1165 pos_args[0][1] = uint->zero; /* EXEC mask */
1166 pos_args[0][2] = uint->zero; /* last export? */
1167 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1168 pos_args[0][4] = uint->zero; /* COMPR flag */
1169 pos_args[0][5] = base->zero; /* X */
1170 pos_args[0][6] = base->zero; /* Y */
1171 pos_args[0][7] = base->zero; /* Z */
1172 pos_args[0][8] = base->one; /* W */
1173 }
1174
1175 /* Write the misc vector (point size, edgeflag, layer, viewport). */
Marek Olšák16153472014-12-07 19:30:08 +01001176 if (shader->selector->info.writes_psize ||
1177 shader->selector->info.writes_edgeflag ||
1178 shader->selector->info.writes_layer) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001179 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
Marek Olšák16153472014-12-07 19:30:08 +01001180 shader->selector->info.writes_psize |
1181 (shader->selector->info.writes_edgeflag << 1) |
1182 (shader->selector->info.writes_layer << 2));
Michel Dänzer51f89a02013-12-09 15:33:53 +09001183 pos_args[1][1] = uint->zero; /* EXEC mask */
1184 pos_args[1][2] = uint->zero; /* last export? */
1185 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1186 pos_args[1][4] = uint->zero; /* COMPR flag */
1187 pos_args[1][5] = base->zero; /* X */
1188 pos_args[1][6] = base->zero; /* Y */
1189 pos_args[1][7] = base->zero; /* Z */
1190 pos_args[1][8] = base->zero; /* W */
1191
Marek Olšák16153472014-12-07 19:30:08 +01001192 if (shader->selector->info.writes_psize)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001193 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001194
Marek Olšák16153472014-12-07 19:30:08 +01001195 if (shader->selector->info.writes_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001196 /* The output is a float, but the hw expects an integer
1197 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001198 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1199 edgeflag_value,
1200 bld_base->uint_bld.elem_type, "");
1201 edgeflag_value = lp_build_min(&bld_base->int_bld,
1202 edgeflag_value,
1203 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001204
1205 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001206 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1207 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001208 base->elem_type, "");
1209 }
1210
Marek Olšák16153472014-12-07 19:30:08 +01001211 if (shader->selector->info.writes_layer)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001212 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001213 }
1214
1215 for (i = 0; i < 4; i++)
1216 if (pos_args[i][0])
1217 shader->nr_pos_exports++;
1218
1219 pos_idx = 0;
1220 for (i = 0; i < 4; i++) {
1221 if (!pos_args[i][0])
1222 continue;
1223
1224 /* Specify the target we are exporting */
1225 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1226
1227 if (pos_idx == shader->nr_pos_exports)
1228 /* Specify that this is the last export */
1229 pos_args[i][2] = uint->one;
1230
1231 lp_build_intrinsic(base->gallivm->builder,
1232 "llvm.SI.export",
1233 LLVMVoidTypeInContext(base->gallivm->context),
1234 pos_args[i], 9);
1235 }
1236}
1237
Michel Dänzer404b29d2013-11-21 16:45:28 +09001238static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1239{
1240 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1241 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001242 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001243 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001244 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001245 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1246 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001247 unsigned chan;
1248 int i;
1249
Marek Olšák216cf862014-10-04 17:04:05 +02001250 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001251 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001252 si_shader_ctx->radeon_bld.soa.outputs[i];
1253 int param_index = get_param_index(info->output_semantic_name[i],
1254 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001255 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001256
Marek Olšáke29353f2014-09-17 22:17:02 +02001257 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001258 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001259
1260 for (chan = 0; chan < 4; chan++) {
1261 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001262 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1263
Marek Olšákfc3b3352014-10-05 13:33:40 +02001264 build_tbuffer_store(si_shader_ctx,
1265 si_shader_ctx->esgs_ring,
1266 out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001267 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001268 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001269 V_008F0C_BUF_DATA_FORMAT_32,
1270 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001271 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001272 }
1273 }
1274}
1275
1276static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1277{
1278 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1279 struct gallivm_state *gallivm = bld_base->base.gallivm;
1280 LLVMValueRef args[2];
1281
1282 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1283 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1284 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1285 LLVMVoidTypeInContext(gallivm->context), args, 2,
1286 LLVMNoUnwindAttribute);
1287}
1288
1289static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1290{
1291 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1292 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001293 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001294 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001295 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001296
Marek Olšákec0d1682014-10-04 22:33:36 +02001297 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001298
Marek Olšákec0d1682014-10-04 22:33:36 +02001299 for (i = 0; i < info->num_outputs; i++) {
1300 outputs[i].name = info->output_semantic_name[i];
1301 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001302
Marek Olšákec0d1682014-10-04 22:33:36 +02001303 for (j = 0; j < 4; j++)
1304 outputs[i].values[j] =
1305 LLVMBuildLoad(gallivm->builder,
1306 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1307 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001308 }
1309
Marek Olšákec0d1682014-10-04 22:33:36 +02001310 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001311 FREE(outputs);
1312}
1313
Michel Dänzer51f89a02013-12-09 15:33:53 +09001314static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1315{
1316 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001317 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001318 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001319 struct lp_build_context * uint = &bld_base->uint_bld;
1320 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001321 LLVMValueRef args[9];
1322 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001323 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001324 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001325
Marek Olšák75e97e22014-10-04 23:13:50 +02001326 for (i = 0; i < info->num_outputs; i++) {
1327 unsigned semantic_name = info->output_semantic_name[i];
1328 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001329 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001330
Marek Olšák75e97e22014-10-04 23:13:50 +02001331 /* Select the correct target */
1332 switch (semantic_name) {
1333 case TGSI_SEMANTIC_POSITION:
1334 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001335 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001336 case TGSI_SEMANTIC_STENCIL:
1337 stencil_index = i;
1338 continue;
1339 case TGSI_SEMANTIC_SAMPLEMASK:
1340 samplemask_index = i;
1341 continue;
1342 case TGSI_SEMANTIC_COLOR:
1343 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1344 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1345 LLVMBuildStore(bld_base->base.gallivm->builder,
1346 bld_base->base.one,
1347 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001348
Marek Olšák75e97e22014-10-04 23:13:50 +02001349 if (semantic_index == 0 &&
1350 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1351 si_alpha_test(bld_base,
1352 si_shader_ctx->radeon_bld.soa.outputs[i]);
1353 break;
1354 default:
1355 target = 0;
1356 fprintf(stderr,
1357 "Warning: SI unhandled fs output type:%d\n",
1358 semantic_name);
1359 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001360
Marek Olšák75e97e22014-10-04 23:13:50 +02001361 si_llvm_init_export_args_load(bld_base,
1362 si_shader_ctx->radeon_bld.soa.outputs[i],
1363 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001364
Marek Olšák75e97e22014-10-04 23:13:50 +02001365 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1366 /* If there is an export instruction waiting to be emitted, do so now. */
1367 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001368 lp_build_intrinsic(base->gallivm->builder,
1369 "llvm.SI.export",
1370 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001371 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001372 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001373
1374 /* This instruction will be emitted at the end of the shader. */
1375 memcpy(last_args, args, sizeof(args));
1376
1377 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1378 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1379 semantic_index == 0 &&
1380 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1381 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1382 si_llvm_init_export_args_load(bld_base,
1383 si_shader_ctx->radeon_bld.soa.outputs[i],
1384 V_008DFC_SQ_EXP_MRT + c, args);
1385 lp_build_intrinsic(base->gallivm->builder,
1386 "llvm.SI.export",
1387 LLVMVoidTypeInContext(base->gallivm->context),
1388 args, 9);
1389 }
1390 }
1391 } else {
1392 lp_build_intrinsic(base->gallivm->builder,
1393 "llvm.SI.export",
1394 LLVMVoidTypeInContext(base->gallivm->context),
1395 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001396 }
1397 }
1398
Marek Olšákd0e8b652014-05-06 20:04:31 +02001399 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001400 LLVMValueRef out_ptr;
1401 unsigned mask = 0;
1402
1403 /* Specify the target we are exporting */
1404 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1405
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001406 args[5] = base->zero; /* R, depth */
1407 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1408 args[7] = base->zero; /* B, sample mask */
1409 args[8] = base->zero; /* A, alpha to mask */
1410
Michel Dänzer1a616c12012-11-13 17:35:09 +01001411 if (depth_index >= 0) {
1412 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1413 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1414 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001415 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001416 }
1417
1418 if (stencil_index >= 0) {
1419 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001420 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001421 mask |= 0x2;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001422 si_shader_ctx->shader->db_shader_control |=
1423 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001424 }
1425
Marek Olšákd0e8b652014-05-06 20:04:31 +02001426 if (samplemask_index >= 0) {
1427 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1428 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001429 mask |= 0x4;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001430 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1431 }
1432
Marek Olšák368b0a72014-12-28 21:51:35 +01001433 /* SI (except OLAND) has a bug that it only looks
1434 * at the X writemask component. */
1435 if (si_shader_ctx->screen->b.chip_class == SI &&
1436 si_shader_ctx->screen->b.family != CHIP_OLAND)
1437 mask |= 0x1;
1438
Marek Olšákd0e8b652014-05-06 20:04:31 +02001439 if (samplemask_index >= 0)
1440 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1441 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001442 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1443 else
1444 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1445
Michel Dänzer1a616c12012-11-13 17:35:09 +01001446 /* Specify which components to enable */
1447 args[0] = lp_build_const_int32(base->gallivm, mask);
1448
1449 args[1] =
1450 args[2] =
1451 args[4] = uint->zero;
1452
1453 if (last_args[0])
1454 lp_build_intrinsic(base->gallivm->builder,
1455 "llvm.SI.export",
1456 LLVMVoidTypeInContext(base->gallivm->context),
1457 args, 9);
1458 else
1459 memcpy(last_args, args, sizeof(args));
1460 }
1461
Michel Dänzer51f89a02013-12-09 15:33:53 +09001462 if (!last_args[0]) {
1463 /* Specify which components to enable */
1464 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001465
Michel Dänzer51f89a02013-12-09 15:33:53 +09001466 /* Specify the target we are exporting */
1467 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001468
Michel Dänzer51f89a02013-12-09 15:33:53 +09001469 /* Set COMPR flag to zero to export data as 32-bit */
1470 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001471
Michel Dänzer51f89a02013-12-09 15:33:53 +09001472 /* dummy bits */
1473 last_args[5]= uint->zero;
1474 last_args[6]= uint->zero;
1475 last_args[7]= uint->zero;
1476 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001477 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001478
1479 /* Specify whether the EXEC mask represents the valid mask */
1480 last_args[1] = uint->one;
1481
1482 /* Specify that this is the last export */
1483 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1484
1485 lp_build_intrinsic(base->gallivm->builder,
1486 "llvm.SI.export",
1487 LLVMVoidTypeInContext(base->gallivm->context),
1488 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001489}
1490
Marek Olšák4855acd2013-08-06 15:08:54 +02001491static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1492 struct lp_build_tgsi_context * bld_base,
1493 struct lp_build_emit_data * emit_data);
1494
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001495static bool tgsi_is_shadow_sampler(unsigned target)
1496{
1497 return target == TGSI_TEXTURE_SHADOW1D ||
1498 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1499 target == TGSI_TEXTURE_SHADOW2D ||
1500 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1501 target == TGSI_TEXTURE_SHADOWCUBE ||
1502 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1503 target == TGSI_TEXTURE_SHADOWRECT;
1504}
1505
Marek Olšák877bb522014-06-25 03:12:46 +02001506static const struct lp_build_tgsi_action tex_action;
1507
Tom Stellarda75c6162012-01-06 17:38:37 -05001508static void tex_fetch_args(
1509 struct lp_build_tgsi_context * bld_base,
1510 struct lp_build_emit_data * emit_data)
1511{
Christian König55fe5cc2013-03-04 16:30:06 +01001512 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001513 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001514 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001515 unsigned opcode = inst->Instruction.Opcode;
1516 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001517 LLVMValueRef coords[4];
1518 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001519 int ref_pos;
1520 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001521 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001522 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001523 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1524 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001525 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001526
1527 if (target == TGSI_TEXTURE_BUFFER) {
1528 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1529 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1530 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1531 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1532
Marek Olšák4f3f0432014-07-07 22:49:15 +02001533 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001534 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1535 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1536 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1537 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1538
1539 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1540 emit_data->args[0] = res;
1541 emit_data->args[1] = bld_base->uint_bld.zero;
1542 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1543 emit_data->arg_count = 3;
1544 return;
1545 }
Tom Stellard467f5162012-05-16 15:15:35 -04001546
Michel Dänzer120efee2013-01-25 12:10:11 +01001547 /* Fetch and project texture coordinates */
1548 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001549 for (chan = 0; chan < 3; chan++ ) {
1550 coords[chan] = lp_build_emit_fetch(bld_base,
1551 emit_data->inst, 0,
1552 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001553 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001554 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1555 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001556 coords[chan],
1557 coords[3]);
1558 }
1559
Michel Dänzer120efee2013-01-25 12:10:11 +01001560 if (opcode == TGSI_OPCODE_TXP)
1561 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001562
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001563 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001564 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001565 /* The offsets are six-bit signed integers packed like this:
1566 * X=[5:0], Y=[13:8], and Z=[21:16].
1567 */
1568 LLVMValueRef offset[3], pack;
1569
1570 assert(inst->Texture.NumOffsets == 1);
1571
1572 for (chan = 0; chan < 3; chan++) {
1573 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1574 emit_data->inst, 0, chan);
1575 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1576 lp_build_const_int32(gallivm, 0x3f), "");
1577 if (chan)
1578 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1579 lp_build_const_int32(gallivm, chan*8), "");
1580 }
1581
1582 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1583 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1584 address[count++] = pack;
1585 }
1586
Michel Dänzer120efee2013-01-25 12:10:11 +01001587 /* Pack LOD bias value */
1588 if (opcode == TGSI_OPCODE_TXB)
1589 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001590 if (opcode == TGSI_OPCODE_TXB2)
1591 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001592
Michel Dänzer120efee2013-01-25 12:10:11 +01001593 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001594 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001595 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1596 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1597 } else {
1598 assert(ref_pos >= 0);
1599 address[count++] = coords[ref_pos];
1600 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001601 }
1602
Marek Olšákb279f0142014-07-04 03:19:58 +02001603 if (target == TGSI_TEXTURE_CUBE ||
1604 target == TGSI_TEXTURE_CUBE_ARRAY ||
1605 target == TGSI_TEXTURE_SHADOWCUBE ||
1606 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1607 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1608
Michel Dänzera6b83c02013-02-21 16:10:55 +01001609 /* Pack user derivatives */
1610 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001611 int num_deriv_channels, param;
1612
1613 switch (target) {
1614 case TGSI_TEXTURE_3D:
1615 num_deriv_channels = 3;
1616 break;
1617 case TGSI_TEXTURE_2D:
1618 case TGSI_TEXTURE_SHADOW2D:
1619 case TGSI_TEXTURE_RECT:
1620 case TGSI_TEXTURE_SHADOWRECT:
1621 case TGSI_TEXTURE_2D_ARRAY:
1622 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1623 case TGSI_TEXTURE_CUBE:
1624 case TGSI_TEXTURE_SHADOWCUBE:
1625 case TGSI_TEXTURE_CUBE_ARRAY:
1626 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1627 num_deriv_channels = 2;
1628 break;
1629 case TGSI_TEXTURE_1D:
1630 case TGSI_TEXTURE_SHADOW1D:
1631 case TGSI_TEXTURE_1D_ARRAY:
1632 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1633 num_deriv_channels = 1;
1634 break;
1635 default:
1636 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001637 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001638
1639 for (param = 1; param <= 2; param++)
1640 for (chan = 0; chan < num_deriv_channels; chan++)
1641 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001642 }
1643
Michel Dänzer120efee2013-01-25 12:10:11 +01001644 /* Pack texture coordinates */
1645 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001646 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001647 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001648 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001649 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001650
Marek Olšákd2bd6342013-09-18 15:40:21 +02001651 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001652 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001653 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001654 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001655 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001656
1657 if (count > 16) {
1658 assert(!"Cannot handle more than 16 texture address parameters");
1659 count = 16;
1660 }
1661
1662 for (chan = 0; chan < count; chan++ ) {
1663 address[chan] = LLVMBuildBitCast(gallivm->builder,
1664 address[chan],
1665 LLVMInt32TypeInContext(gallivm->context),
1666 "");
1667 }
1668
Marek Olšák4855acd2013-08-06 15:08:54 +02001669 /* Adjust the sample index according to FMASK.
1670 *
1671 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1672 * which is the identity mapping. Each nibble says which physical sample
1673 * should be fetched to get that sample.
1674 *
1675 * For example, 0x11111100 means there are only 2 samples stored and
1676 * the second sample covers 3/4 of the pixel. When reading samples 0
1677 * and 1, return physical sample 0 (determined by the first two 0s
1678 * in FMASK), otherwise return physical sample 1.
1679 *
1680 * The sample index should be adjusted as follows:
1681 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1682 */
1683 if (target == TGSI_TEXTURE_2D_MSAA ||
1684 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1685 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1686 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001687 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001688 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001689 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001690
Marek Olšákd2bd6342013-09-18 15:40:21 +02001691 memcpy(txf_address, address, sizeof(txf_address));
1692
1693 if (target == TGSI_TEXTURE_2D_MSAA) {
1694 txf_address[2] = bld_base->uint_bld.zero;
1695 }
1696 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001697
1698 /* Pad to a power-of-two size. */
1699 while (txf_count < util_next_power_of_two(txf_count))
1700 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1701
1702 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001703 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1704 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1705 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001706 txf_emit_data.chan = 0;
1707 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001708 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001709 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001710 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001711 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001712 txf_emit_data.arg_count = 3;
1713
Marek Olšák877bb522014-06-25 03:12:46 +02001714 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001715
1716 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001717 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1718 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1719
1720 /* Apply the formula. */
1721 LLVMValueRef fmask =
1722 LLVMBuildExtractElement(gallivm->builder,
1723 txf_emit_data.output[0],
1724 uint_bld->zero, "");
1725
Marek Olšákd2bd6342013-09-18 15:40:21 +02001726 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001727
1728 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001729 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001730
1731 LLVMValueRef shifted_fmask =
1732 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1733
1734 LLVMValueRef final_sample =
1735 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1736
1737 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1738 * resource descriptor is 0 (invalid),
1739 */
1740 LLVMValueRef fmask_desc =
1741 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001742 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001743 LLVMVectorType(uint_bld->elem_type, 8), "");
1744
1745 LLVMValueRef fmask_word1 =
1746 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1747 uint_bld->one, "");
1748
1749 LLVMValueRef word1_is_nonzero =
1750 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1751 fmask_word1, uint_bld->zero, "");
1752
Marek Olšákd2bd6342013-09-18 15:40:21 +02001753 /* Replace the MSAA sample index. */
1754 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001755 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001756 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001757 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001758
Michel Dänzer36231112013-05-02 09:44:45 +02001759 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001760 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001761
1762 if (opcode == TGSI_OPCODE_TXF) {
1763 /* add tex offsets */
1764 if (inst->Texture.NumOffsets) {
1765 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1766 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1767 const struct tgsi_texture_offset * off = inst->TexOffsets;
1768
1769 assert(inst->Texture.NumOffsets == 1);
1770
Marek Olšákdefedc02013-09-18 15:36:38 +02001771 switch (target) {
1772 case TGSI_TEXTURE_3D:
1773 address[2] = lp_build_add(uint_bld, address[2],
1774 bld->immediates[off->Index][off->SwizzleZ]);
1775 /* fall through */
1776 case TGSI_TEXTURE_2D:
1777 case TGSI_TEXTURE_SHADOW2D:
1778 case TGSI_TEXTURE_RECT:
1779 case TGSI_TEXTURE_SHADOWRECT:
1780 case TGSI_TEXTURE_2D_ARRAY:
1781 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001782 address[1] =
1783 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001784 bld->immediates[off->Index][off->SwizzleY]);
1785 /* fall through */
1786 case TGSI_TEXTURE_1D:
1787 case TGSI_TEXTURE_SHADOW1D:
1788 case TGSI_TEXTURE_1D_ARRAY:
1789 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1790 address[0] =
1791 lp_build_add(uint_bld, address[0],
1792 bld->immediates[off->Index][off->SwizzleX]);
1793 break;
1794 /* texture offsets do not apply to other texture targets */
1795 }
Michel Dänzer36231112013-05-02 09:44:45 +02001796 }
1797
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001798 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1799 emit_data->arg_count = 3;
1800
Michel Dänzer36231112013-05-02 09:44:45 +02001801 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001802 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001803 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001804 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001805 opcode == TGSI_OPCODE_LODQ ||
1806 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001807 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1808 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1809 target == TGSI_TEXTURE_2D_ARRAY ||
1810 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1811 target == TGSI_TEXTURE_CUBE_ARRAY ||
1812 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1813 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001814 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001815
Marek Olšák57f3da92014-06-16 16:53:42 +02001816 if (opcode == TGSI_OPCODE_TG4) {
1817 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001818
Marek Olšák57f3da92014-06-16 16:53:42 +02001819 /* DMASK was repurposed for GATHER4. 4 components are always
1820 * returned and DMASK works like a swizzle - it selects
1821 * the component to fetch. The only valid DMASK values are
1822 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1823 * (red,red,red,red) etc.) The ISA document doesn't mention
1824 * this.
1825 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001826
Marek Olšák57f3da92014-06-16 16:53:42 +02001827 /* Get the component index from src1.x for Gather4. */
1828 if (!tgsi_is_shadow_sampler(target)) {
1829 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1830 LLVMValueRef comp_imm;
1831 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001832
Marek Olšák57f3da92014-06-16 16:53:42 +02001833 assert(src1.File == TGSI_FILE_IMMEDIATE);
1834
1835 comp_imm = imms[src1.Index][src1.SwizzleX];
1836 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1837 gather_comp = CLAMP(gather_comp, 0, 3);
1838 }
1839
1840 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001841 }
1842
Marek Olšák4855acd2013-08-06 15:08:54 +02001843 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001844 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001845 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1846 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1847 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1848 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1849 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1850 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1851 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1852
1853 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001854
1855 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001856 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001857 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001858 } else {
1859 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1860 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001861 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001862
1863 emit_data->dst_type = LLVMVectorType(
1864 LLVMFloatTypeInContext(gallivm->context),
1865 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001866 }
1867
Marek Olšák2484daa2014-04-22 21:23:29 +02001868 /* The fetch opcode has been converted to a 2D array fetch.
1869 * This simplifies the LLVM backend. */
1870 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1871 target = TGSI_TEXTURE_2D_ARRAY;
1872 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1873 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1874
Michel Dänzer120efee2013-01-25 12:10:11 +01001875 /* Pad to power of two vector */
1876 while (count < util_next_power_of_two(count))
1877 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1878
Christian Königccf3e8f2013-03-26 15:09:27 +01001879 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001880}
1881
Michel Dänzer07eddc42013-02-06 15:43:10 +01001882static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1883 struct lp_build_tgsi_context * bld_base,
1884 struct lp_build_emit_data * emit_data)
1885{
1886 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001887 unsigned opcode = emit_data->inst->Instruction.Opcode;
1888 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001889 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001890 bool has_offset = HAVE_LLVM >= 0x0305 ?
1891 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001892
Marek Olšák877bb522014-06-25 03:12:46 +02001893 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001894 emit_data->output[emit_data->chan] = build_intrinsic(
1895 base->gallivm->builder,
1896 "llvm.SI.vs.load.input", emit_data->dst_type,
1897 emit_data->args, emit_data->arg_count,
1898 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1899 return;
1900 }
1901
Marek Olšák877bb522014-06-25 03:12:46 +02001902 if (opcode == TGSI_OPCODE_TG4 ||
1903 opcode == TGSI_OPCODE_LODQ ||
1904 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1905 bool is_shadow = tgsi_is_shadow_sampler(target);
1906 const char *name = "llvm.SI.image.sample";
1907 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001908
Marek Olšák877bb522014-06-25 03:12:46 +02001909 switch (opcode) {
1910 case TGSI_OPCODE_TEX:
1911 case TGSI_OPCODE_TEX2:
1912 case TGSI_OPCODE_TXP:
1913 break;
1914 case TGSI_OPCODE_TXB:
1915 case TGSI_OPCODE_TXB2:
1916 infix = ".b";
1917 break;
1918 case TGSI_OPCODE_TXL:
1919 case TGSI_OPCODE_TXL2:
1920 infix = ".l";
1921 break;
1922 case TGSI_OPCODE_TXD:
1923 infix = ".d";
1924 break;
1925 case TGSI_OPCODE_TG4:
1926 name = "llvm.SI.gather4";
1927 break;
1928 case TGSI_OPCODE_LODQ:
1929 name = "llvm.SI.getlod";
1930 is_shadow = false;
1931 has_offset = false;
1932 break;
1933 default:
1934 assert(0);
1935 return;
1936 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001937
Marek Olšák877bb522014-06-25 03:12:46 +02001938 /* Add the type and suffixes .c, .o if needed. */
1939 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1940 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1941 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001942
Marek Olšák877bb522014-06-25 03:12:46 +02001943 emit_data->output[emit_data->chan] = build_intrinsic(
1944 base->gallivm->builder, intr_name, emit_data->dst_type,
1945 emit_data->args, emit_data->arg_count,
1946 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1947 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001948 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001949 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001950
Marek Olšák877bb522014-06-25 03:12:46 +02001951 switch (opcode) {
1952 case TGSI_OPCODE_TEX:
1953 case TGSI_OPCODE_TEX2:
1954 case TGSI_OPCODE_TXP:
1955 name = "llvm.SI.sample";
1956 break;
1957 case TGSI_OPCODE_TXB:
1958 case TGSI_OPCODE_TXB2:
1959 name = "llvm.SI.sampleb";
1960 break;
1961 case TGSI_OPCODE_TXD:
1962 name = "llvm.SI.sampled";
1963 break;
1964 case TGSI_OPCODE_TXF:
1965 name = "llvm.SI.imageload";
1966 break;
1967 case TGSI_OPCODE_TXL:
1968 case TGSI_OPCODE_TXL2:
1969 name = "llvm.SI.samplel";
1970 break;
1971 default:
1972 assert(0);
1973 return;
1974 }
1975
Marek Olšákd859bdb2014-07-14 19:40:14 +02001976 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1977 v16i8 = LLVMVectorType(i8, 16);
1978 v32i8 = LLVMVectorType(i8, 32);
1979
1980 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1981 emit_data->args[1], v32i8, "");
1982 if (opcode != TGSI_OPCODE_TXF) {
1983 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1984 emit_data->args[2], v16i8, "");
1985 }
1986
Marek Olšák877bb522014-06-25 03:12:46 +02001987 sprintf(intr_name, "%s.v%ui32", name,
1988 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1989
1990 emit_data->output[emit_data->chan] = build_intrinsic(
1991 base->gallivm->builder, intr_name, emit_data->dst_type,
1992 emit_data->args, emit_data->arg_count,
1993 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1994 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001995}
1996
Michel Dänzer0495adb2013-05-06 12:45:14 +02001997static void txq_fetch_args(
1998 struct lp_build_tgsi_context * bld_base,
1999 struct lp_build_emit_data * emit_data)
2000{
2001 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2002 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002003 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002004 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002005
Marek Olšák2484daa2014-04-22 21:23:29 +02002006 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002007 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2008 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2009
2010 /* Read the size from the buffer descriptor directly. */
2011 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2012 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2013 size = LLVMBuildExtractElement(gallivm->builder, size,
2014 lp_build_const_int32(gallivm, 2), "");
2015 emit_data->args[0] = size;
2016 return;
2017 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002018
2019 /* Mip level */
2020 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2021
2022 /* Resource */
2023 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2024
Marek Olšák2484daa2014-04-22 21:23:29 +02002025 /* Texture target */
2026 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2027 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2028 target = TGSI_TEXTURE_2D_ARRAY;
2029
Michel Dänzer0495adb2013-05-06 12:45:14 +02002030 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002031 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002032
2033 emit_data->arg_count = 3;
2034
2035 emit_data->dst_type = LLVMVectorType(
2036 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2037 4);
2038}
2039
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002040static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2041 struct lp_build_tgsi_context * bld_base,
2042 struct lp_build_emit_data * emit_data)
2043{
Marek Olšák2484daa2014-04-22 21:23:29 +02002044 unsigned target = emit_data->inst->Texture.Texture;
2045
2046 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002047 /* Just return the buffer size. */
2048 emit_data->output[emit_data->chan] = emit_data->args[0];
2049 return;
2050 }
2051
2052 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002053
2054 /* Divide the number of layers by 6 to get the number of cubes. */
2055 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2056 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2057 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2058 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2059 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2060
2061 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2062 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2063 z = LLVMBuildSDiv(builder, z, six, "");
2064
2065 emit_data->output[emit_data->chan] =
2066 LLVMBuildInsertElement(builder, v4, z, two, "");
2067 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002068}
2069
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002070static void si_llvm_emit_ddxy(
2071 const struct lp_build_tgsi_action * action,
2072 struct lp_build_tgsi_context * bld_base,
2073 struct lp_build_emit_data * emit_data)
2074{
2075 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2076 struct gallivm_state *gallivm = bld_base->base.gallivm;
2077 struct lp_build_context * base = &bld_base->base;
2078 const struct tgsi_full_instruction *inst = emit_data->inst;
2079 unsigned opcode = inst->Instruction.Opcode;
2080 LLVMValueRef indices[2];
2081 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2082 LLVMValueRef tl, trbl, result[4];
2083 LLVMTypeRef i32;
2084 unsigned swizzle[4];
2085 unsigned c;
2086
2087 i32 = LLVMInt32TypeInContext(gallivm->context);
2088
2089 indices[0] = bld_base->uint_bld.zero;
2090 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2091 NULL, 0, LLVMReadNoneAttribute);
2092 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2093 indices, 2, "");
2094
2095 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2096 lp_build_const_int32(gallivm, 0xfffffffc), "");
2097 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2098 indices, 2, "");
2099
2100 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2101 lp_build_const_int32(gallivm,
2102 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2103 "");
2104 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2105 indices, 2, "");
2106
2107 for (c = 0; c < 4; ++c) {
2108 unsigned i;
2109
2110 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2111 for (i = 0; i < c; ++i) {
2112 if (swizzle[i] == swizzle[c]) {
2113 result[c] = result[i];
2114 break;
2115 }
2116 }
2117 if (i != c)
2118 continue;
2119
2120 LLVMBuildStore(gallivm->builder,
2121 LLVMBuildBitCast(gallivm->builder,
2122 lp_build_emit_fetch(bld_base, inst, 0, c),
2123 i32, ""),
2124 store_ptr);
2125
2126 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2127 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2128
2129 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2130 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2131
2132 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2133 }
2134
2135 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2136}
2137
Michel Dänzer404b29d2013-11-21 16:45:28 +09002138/* Emit one vertex from the geometry shader */
2139static void si_llvm_emit_vertex(
2140 const struct lp_build_tgsi_action *action,
2141 struct lp_build_tgsi_context *bld_base,
2142 struct lp_build_emit_data *emit_data)
2143{
2144 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002145 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002146 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002147 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002148 struct gallivm_state *gallivm = bld_base->base.gallivm;
2149 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002150 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2151 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002152 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002153 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002154 LLVMValueRef args[2];
2155 unsigned chan;
2156 int i;
2157
Michel Dänzer404b29d2013-11-21 16:45:28 +09002158 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002159 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002160
2161 /* If this thread has already emitted the declared maximum number of
2162 * vertices, kill it: excessive vertex emissions are not supposed to
2163 * have any effect, and GS threads have no externally observable
2164 * effects other than emitting vertices.
2165 */
2166 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2167 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002168 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002169 kill = lp_build_select(&bld_base->base, can_emit,
2170 lp_build_const_float(gallivm, 1.0f),
2171 lp_build_const_float(gallivm, -1.0f));
2172 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2173 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2174
Marek Olšák02134cf2014-10-04 22:07:50 +02002175 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002176 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002177 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002178
2179 for (chan = 0; chan < 4; chan++) {
2180 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002181 LLVMValueRef voffset =
2182 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002183 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002184
2185 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2186 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002187
2188 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2189
Marek Olšákfc3b3352014-10-05 13:33:40 +02002190 build_tbuffer_store(si_shader_ctx,
2191 si_shader_ctx->gsvs_ring,
2192 out_val, 1,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002193 voffset, soffset, 0,
2194 V_008F0C_BUF_DATA_FORMAT_32,
2195 V_008F0C_BUF_NUM_FORMAT_UINT,
2196 1, 0, 1, 1, 0);
2197 }
2198 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002199 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2200 lp_build_const_int32(gallivm, 1));
2201 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002202
2203 /* Signal vertex emission */
2204 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2205 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2206 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2207 LLVMVoidTypeInContext(gallivm->context), args, 2,
2208 LLVMNoUnwindAttribute);
2209}
2210
2211/* Cut one primitive from the geometry shader */
2212static void si_llvm_emit_primitive(
2213 const struct lp_build_tgsi_action *action,
2214 struct lp_build_tgsi_context *bld_base,
2215 struct lp_build_emit_data *emit_data)
2216{
2217 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2218 struct gallivm_state *gallivm = bld_base->base.gallivm;
2219 LLVMValueRef args[2];
2220
2221 /* Signal primitive cut */
2222 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2223 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2224 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2225 LLVMVoidTypeInContext(gallivm->context), args, 2,
2226 LLVMNoUnwindAttribute);
2227}
2228
Tom Stellarda75c6162012-01-06 17:38:37 -05002229static const struct lp_build_tgsi_action tex_action = {
2230 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002231 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002232};
2233
Michel Dänzer0495adb2013-05-06 12:45:14 +02002234static const struct lp_build_tgsi_action txq_action = {
2235 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002236 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002237 .intr_name = "llvm.SI.resinfo"
2238};
2239
Christian König206f0592013-03-20 14:37:21 +01002240static void create_meta_data(struct si_shader_context *si_shader_ctx)
2241{
2242 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2243 LLVMValueRef args[3];
2244
2245 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2246 args[1] = 0;
2247 args[2] = lp_build_const_int32(gallivm, 1);
2248
2249 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2250}
2251
Marek Olšák4f3f0432014-07-07 22:49:15 +02002252static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2253{
2254 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2255 CONST_ADDR_SPACE);
2256}
2257
Christian König55fe5cc2013-03-04 16:30:06 +01002258static void create_function(struct si_shader_context *si_shader_ctx)
2259{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002260 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2261 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002262 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002263 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002264 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002265
Christian König55fe5cc2013-03-04 16:30:06 +01002266 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002267 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002268 f32 = LLVMFloatTypeInContext(gallivm->context);
2269 v2i32 = LLVMVectorType(i32, 2);
2270 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002271 v4i32 = LLVMVectorType(i32, 4);
2272 v8i32 = LLVMVectorType(i32, 8);
2273 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002274
Marek Olšákee2a8182014-07-07 23:27:19 +02002275 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002276 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002277 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2278 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002279 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002280
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002281 switch (si_shader_ctx->type) {
2282 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002283 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002284 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002285 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002286 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002287 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002288
Michel Dänzer404b29d2013-11-21 16:45:28 +09002289 if (shader->key.vs.as_es) {
2290 params[SI_PARAM_ES2GS_OFFSET] = i32;
2291 num_params++;
2292 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002293 if (shader->is_gs_copy_shader) {
2294 last_array_pointer = SI_PARAM_CONST;
2295 num_params = SI_PARAM_CONST+1;
2296 }
2297
Michel Dänzer404b29d2013-11-21 16:45:28 +09002298 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002299
Michel Dänzer404b29d2013-11-21 16:45:28 +09002300 /* Streamout SGPRs. */
2301 if (shader->selector->so.num_outputs) {
2302 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2303 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2304 }
2305 /* A streamout buffer offset is loaded if the stride is non-zero. */
2306 for (i = 0; i < 4; i++) {
2307 if (!shader->selector->so.stride[i])
2308 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002309
Michel Dänzer404b29d2013-11-21 16:45:28 +09002310 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2311 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002312 }
2313
2314 last_sgpr = num_params-1;
2315
2316 /* VGPRs */
2317 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2318 params[num_params++] = i32; /* unused*/
2319 params[num_params++] = i32; /* unused */
2320 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002321 break;
Christian König0666ffd2013-03-05 15:07:39 +01002322
Michel Dänzer404b29d2013-11-21 16:45:28 +09002323 case TGSI_PROCESSOR_GEOMETRY:
2324 params[SI_PARAM_GS2VS_OFFSET] = i32;
2325 params[SI_PARAM_GS_WAVE_ID] = i32;
2326 last_sgpr = SI_PARAM_GS_WAVE_ID;
2327
2328 /* VGPRs */
2329 params[SI_PARAM_VTX0_OFFSET] = i32;
2330 params[SI_PARAM_VTX1_OFFSET] = i32;
2331 params[SI_PARAM_PRIMITIVE_ID] = i32;
2332 params[SI_PARAM_VTX2_OFFSET] = i32;
2333 params[SI_PARAM_VTX3_OFFSET] = i32;
2334 params[SI_PARAM_VTX4_OFFSET] = i32;
2335 params[SI_PARAM_VTX5_OFFSET] = i32;
2336 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2337 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2338 break;
2339
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002340 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002341 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002342 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002343 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002344 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2345 params[SI_PARAM_PERSP_CENTER] = v2i32;
2346 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2347 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2348 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2349 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2350 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2351 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2352 params[SI_PARAM_POS_X_FLOAT] = f32;
2353 params[SI_PARAM_POS_Y_FLOAT] = f32;
2354 params[SI_PARAM_POS_Z_FLOAT] = f32;
2355 params[SI_PARAM_POS_W_FLOAT] = f32;
2356 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002357 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002358 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2359 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002360 num_params = SI_PARAM_POS_FIXED_PT+1;
2361 break;
2362
2363 default:
2364 assert(0 && "unimplemented shader");
2365 return;
Christian Königc4973212013-03-05 12:14:02 +01002366 }
Christian König55fe5cc2013-03-04 16:30:06 +01002367
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002368 assert(num_params <= Elements(params));
2369 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002370 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002371
Marek Olšák1829f9c2015-01-04 17:08:57 +01002372 if (shader->dx10_clamp_mode)
2373 LLVMAddTargetDependentFunctionAttr(si_shader_ctx->radeon_bld.main_fn,
2374 "enable-no-nans-fp-math", "true");
2375
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002376 for (i = 0; i <= last_sgpr; ++i) {
2377 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002378
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002379 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2380 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002381 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002382 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002383 else
2384 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002385 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002386
Michel Dänzer404b29d2013-11-21 16:45:28 +09002387 if (bld_base->info &&
2388 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2389 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002390 si_shader_ctx->ddxy_lds =
2391 LLVMAddGlobalInAddressSpace(gallivm->module,
2392 LLVMArrayType(i32, 64),
2393 "ddxy_lds",
2394 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002395}
Tom Stellarda75c6162012-01-06 17:38:37 -05002396
Christian König0f6cf2b2013-03-15 15:53:25 +01002397static void preload_constants(struct si_shader_context *si_shader_ctx)
2398{
2399 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2400 struct gallivm_state * gallivm = bld_base->base.gallivm;
2401 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002402 unsigned buf;
2403 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002404
Marek Olšákee2a8182014-07-07 23:27:19 +02002405 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002406 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002407
Marek Olšák2fd42002013-10-25 11:45:47 +02002408 if (num_const == 0)
2409 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002410
Marek Olšák2fd42002013-10-25 11:45:47 +02002411 /* Allocate space for the constant values */
2412 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002413
Marek Olšák2fd42002013-10-25 11:45:47 +02002414 /* Load the resource descriptor */
2415 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002416 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002417
Marek Olšák2fd42002013-10-25 11:45:47 +02002418 /* Load the constants, we rely on the code sinking to do the rest */
2419 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002420 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002421 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002422 si_shader_ctx->const_resource[buf],
2423 lp_build_const_int32(gallivm, i * 4),
2424 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002425 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002426 }
2427}
2428
Christian König1c100182013-03-17 16:02:42 +01002429static void preload_samplers(struct si_shader_context *si_shader_ctx)
2430{
2431 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2432 struct gallivm_state * gallivm = bld_base->base.gallivm;
2433 const struct tgsi_shader_info * info = bld_base->info;
2434
2435 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2436
2437 LLVMValueRef res_ptr, samp_ptr;
2438 LLVMValueRef offset;
2439
2440 if (num_samplers == 0)
2441 return;
2442
Christian König1c100182013-03-17 16:02:42 +01002443 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2444 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2445
2446 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2447 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002448 /* Resource */
2449 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002450 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002451
2452 /* Sampler */
2453 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002454 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002455
2456 /* FMASK resource */
2457 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002458 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2459 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002460 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002461 }
Christian König1c100182013-03-17 16:02:42 +01002462 }
2463}
2464
Marek Olšák8d03d922013-09-01 23:59:06 +02002465static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2466{
2467 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2468 struct gallivm_state * gallivm = bld_base->base.gallivm;
2469 unsigned i;
2470
Michel Dänzer67e385b2014-01-08 17:48:21 +09002471 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2472 si_shader_ctx->shader->key.vs.as_es ||
2473 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002474 return;
2475
2476 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002477 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002478
2479 /* Load the resources, we rely on the code sinking to do the rest */
2480 for (i = 0; i < 4; ++i) {
2481 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002482 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002483 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002484
Marek Olšákd7876082014-09-26 23:06:32 +02002485 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002486 }
2487 }
2488}
2489
Marek Olšákfc3b3352014-10-05 13:33:40 +02002490/**
2491 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
2492 * for later use.
2493 */
2494static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
2495{
2496 struct gallivm_state *gallivm =
2497 si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2498
2499 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2500 SI_PARAM_RW_BUFFERS);
2501
2502 if ((si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
2503 si_shader_ctx->shader->key.vs.as_es) ||
2504 si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY) {
2505 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_ESGS);
2506
2507 si_shader_ctx->esgs_ring =
2508 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2509 }
2510
2511 if (si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY ||
2512 si_shader_ctx->shader->is_gs_copy_shader) {
2513 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_GSVS);
2514
2515 si_shader_ctx->gsvs_ring =
2516 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2517 }
2518}
2519
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002520void si_shader_binary_read_config(const struct radeon_shader_binary *binary,
2521 struct si_shader *shader,
2522 unsigned symbol_offset)
Tom Stellard302f53d2012-10-25 13:50:10 -04002523{
Tom Stellard302f53d2012-10-25 13:50:10 -04002524 unsigned i;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002525 const unsigned char *config =
2526 radeon_shader_binary_config_start(binary, symbol_offset);
Tom Stellard302f53d2012-10-25 13:50:10 -04002527
Tom Stellardd50343d2013-04-04 16:21:06 -04002528 /* XXX: We may be able to emit some of these values directly rather than
2529 * extracting fields to be emitted later.
2530 */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002531
2532 for (i = 0; i < binary->config_size_per_symbol; i+= 8) {
2533 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
2534 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
Tom Stellardd50343d2013-04-04 16:21:06 -04002535 switch (reg) {
2536 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2537 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2538 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2539 case R_00B848_COMPUTE_PGM_RSRC1:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002540 shader->num_sgprs = MAX2(shader->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
2541 shader->num_vgprs = MAX2(shader->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
Tom Stellardd50343d2013-04-04 16:21:06 -04002542 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002543 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002544 shader->lds_size = MAX2(shader->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002545 break;
2546 case R_00B84C_COMPUTE_PGM_RSRC2:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002547 shader->lds_size = MAX2(shader->lds_size, G_00B84C_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002548 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002549 case R_0286CC_SPI_PS_INPUT_ENA:
2550 shader->spi_ps_input_ena = value;
2551 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002552 case R_00B860_COMPUTE_TMPRING_SIZE:
2553 /* WAVESIZE is in units of 256 dwords. */
2554 shader->scratch_bytes_per_wave =
2555 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2556 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002557 default:
2558 fprintf(stderr, "Warning: Compiler emitted unknown "
2559 "config register: 0x%x\n", reg);
2560 break;
2561 }
2562 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002563}
2564
2565int si_shader_binary_read(struct si_screen *sscreen,
2566 struct si_shader *shader,
2567 const struct radeon_shader_binary *binary)
2568{
2569
2570 unsigned i;
2571 unsigned code_size;
2572 unsigned char *ptr;
2573 bool dump = r600_can_dump_shader(&sscreen->b,
2574 shader->selector ? shader->selector->tokens : NULL);
2575
2576 if (dump && !binary->disassembled) {
2577 fprintf(stderr, "SI CODE:\n");
2578 for (i = 0; i < binary->code_size; i+=4 ) {
2579 fprintf(stderr, "@0x%x: %02x%02x%02x%02x\n", i, binary->code[i + 3],
2580 binary->code[i + 2], binary->code[i + 1],
2581 binary->code[i]);
2582 }
2583 }
2584
2585 si_shader_binary_read_config(binary, shader, 0);
Tom Stellard302f53d2012-10-25 13:50:10 -04002586
2587 /* copy new shader */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002588 code_size = binary->code_size + binary->rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002589 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002590 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002591 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002592 if (shader->bo == NULL) {
2593 return -ENOMEM;
2594 }
2595
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002596
2597 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_READ_WRITE);
2598 util_memcpy_cpu_to_le32(ptr, binary->code, binary->code_size);
2599 if (binary->rodata_size > 0) {
2600 ptr += binary->code_size;
2601 util_memcpy_cpu_to_le32(ptr, binary->rodata, binary->rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002602 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002603
Marek Olšák1abb1a92014-09-17 22:44:22 +02002604 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002605
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002606 return 0;
2607}
Tom Stellard302f53d2012-10-25 13:50:10 -04002608
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002609int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2610 LLVMModuleRef mod)
2611{
2612 int r = 0;
2613 struct radeon_shader_binary binary;
2614 bool dump = r600_can_dump_shader(&sscreen->b,
2615 shader->selector ? shader->selector->tokens : NULL);
2616 memset(&binary, 0, sizeof(binary));
2617 r = radeon_llvm_compile(mod, &binary,
Tom Stellard761e36b2014-10-15 12:24:30 -04002618 r600_get_llvm_processor_name(sscreen->b.family), dump, sscreen->tm);
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002619
2620 if (r) {
2621 return r;
2622 }
2623 r = si_shader_binary_read(sscreen, shader, &binary);
2624 FREE(binary.code);
2625 FREE(binary.config);
2626 FREE(binary.rodata);
Darren Powellbc866902014-03-31 18:00:28 -04002627 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002628}
2629
Michel Dänzer404b29d2013-11-21 16:45:28 +09002630/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002631static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002632 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002633 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002634{
2635 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2636 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2637 struct lp_build_context *base = &bld_base->base;
2638 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002639 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002640 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002641 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002642 LLVMValueRef args[9];
2643 int i, r;
2644
Marek Olšák02134cf2014-10-04 22:07:50 +02002645 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002646
2647 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002648 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002649
2650 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2651
2652 create_meta_data(si_shader_ctx);
2653 create_function(si_shader_ctx);
2654 preload_streamout_buffers(si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002655 preload_ring_buffers(si_shader_ctx);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002656
Marek Olšákfc3b3352014-10-05 13:33:40 +02002657 args[0] = si_shader_ctx->gsvs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002658 args[1] = lp_build_mul_imm(uint,
2659 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2660 si_shader_ctx->param_vertex_id),
2661 4);
2662 args[3] = uint->zero;
2663 args[4] = uint->one; /* OFFEN */
2664 args[5] = uint->zero; /* IDXEN */
2665 args[6] = uint->one; /* GLC */
2666 args[7] = uint->one; /* SLC */
2667 args[8] = uint->zero; /* TFE */
2668
2669 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002670 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002671 unsigned chan;
2672
Marek Olšák02134cf2014-10-04 22:07:50 +02002673 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002674 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002675
2676 for (chan = 0; chan < 4; chan++) {
2677 args[2] = lp_build_const_int32(gallivm,
2678 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002679 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002680
2681 outputs[i].values[chan] =
2682 LLVMBuildBitCast(gallivm->builder,
2683 build_intrinsic(gallivm->builder,
2684 "llvm.SI.buffer.load.dword.i32.i32",
2685 LLVMInt32TypeInContext(gallivm->context),
2686 args, 9,
2687 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2688 base->elem_type, "");
2689 }
2690 }
2691
Marek Olšák02134cf2014-10-04 22:07:50 +02002692 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002693
2694 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2695
2696 if (dump)
2697 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2698
Marek Olšák1abb1a92014-09-17 22:44:22 +02002699 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002700 bld_base->base.gallivm->module);
2701
2702 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2703
2704 FREE(outputs);
2705 return r;
2706}
2707
Marek Olšák1abb1a92014-09-17 22:44:22 +02002708int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002709{
Marek Olšák2774abd2014-09-16 18:45:33 +02002710 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002711 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002712 struct lp_build_tgsi_context * bld_base;
2713 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002714 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002715 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002716
2717 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2718 * conversion fails. */
2719 if (dump) {
2720 tgsi_dump(sel->tokens, 0);
2721 si_dump_streamout(&sel->so);
2722 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002723
Marek Olšák8c37c162014-09-16 18:40:07 +02002724 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002725
Michel Dänzercfebaf92012-08-31 19:04:08 +02002726 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002727 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2728 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2729
Marek Olšák1829f9c2015-01-04 17:08:57 +01002730 if (sel->type != PIPE_SHADER_COMPUTE)
2731 shader->dx10_clamp_mode = true;
2732
Marek Olšák52335682014-09-30 15:12:09 +02002733 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002734 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2735
Marek Olšák52335682014-09-30 15:12:09 +02002736 shader->uses_instanceid = sel->info.uses_instanceid;
2737 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002738 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002739
2740 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002741 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002742 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2743 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002744 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002745 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2746 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2747 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002748 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002749 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002750 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2751 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002752
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002753 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2754 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002755
Michel Dänzer404b29d2013-11-21 16:45:28 +09002756 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2757 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2758
Marek Olšák645b4712014-11-20 22:16:09 +01002759 if (HAVE_LLVM >= 0x0306) {
2760 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
2761 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
2762 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
2763 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
2764 }
2765
Christian Könige4ed5872013-03-21 18:02:52 +01002766 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002767 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002768 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2769 si_shader_ctx.shader = shader;
2770 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Marek Olšák368b0a72014-12-28 21:51:35 +01002771 si_shader_ctx.screen = sscreen;
Tom Stellarda75c6162012-01-06 17:38:37 -05002772
Michel Dänzer51f89a02013-12-09 15:33:53 +09002773 switch (si_shader_ctx.type) {
2774 case TGSI_PROCESSOR_VERTEX:
2775 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002776 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002777 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2778 } else {
2779 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2780 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002781 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002782 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002783 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2784 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002785 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002786 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002787 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2788 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002789
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002790 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002791 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2792 shader->db_shader_control |=
2793 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2794 break;
2795 case TGSI_FS_DEPTH_LAYOUT_LESS:
2796 shader->db_shader_control |=
2797 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2798 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002799 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002800 break;
2801 default:
2802 assert(!"Unsupported shader type");
2803 return -1;
2804 }
2805
Christian König206f0592013-03-20 14:37:21 +01002806 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002807 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002808 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002809 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002810 preload_streamout_buffers(&si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002811 preload_ring_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002812
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002813 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2814 si_shader_ctx.gs_next_vertex =
2815 lp_build_alloca(bld_base->base.gallivm,
2816 bld_base->uint_bld.elem_type, "");
2817 }
2818
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002819 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002820 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002821 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002822 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002823
2824 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2825
2826 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002827 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002828 if (r) {
2829 fprintf(stderr, "LLVM failed to compile shader\n");
2830 goto out;
2831 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002832
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002833 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002834
2835 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002836 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002837 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002838 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002839 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002840 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2841 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002842 free(shader->gs_copy_shader);
2843 shader->gs_copy_shader = NULL;
2844 goto out;
2845 }
2846 }
2847
Tom Stellarda75c6162012-01-06 17:38:37 -05002848 tgsi_parse_free(&si_shader_ctx.parse);
2849
Michel Dänzer404b29d2013-11-21 16:45:28 +09002850out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002851 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002852 FREE(si_shader_ctx.constants[i]);
Tom Stellarda75c6162012-01-06 17:38:37 -05002853
Tom Stellard302f53d2012-10-25 13:50:10 -04002854 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002855}
2856
Marek Olšák2774abd2014-09-16 18:45:33 +02002857void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002858{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002859 if (shader->gs_copy_shader)
2860 si_shader_destroy(ctx, shader->gs_copy_shader);
2861
Marek Olšáka81c3e02013-08-14 01:04:39 +02002862 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002863 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002864}