blob: 4b1279cc26b70d73f7fdfab94ce1722264312749 [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"
Tom Stellard2397a722014-12-10 09:13:59 -050035#include "radeon/r600_cs.h"
Emil Velikova1312632014-08-16 17:58:25 +010036#include "radeon/radeon_llvm.h"
Tom Stellard1f4e48d2014-09-25 18:11:24 -070037#include "radeon/radeon_elf_util.h"
Emil Velikova1312632014-08-16 17:58:25 +010038#include "radeon/radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010039#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010041#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050042#include "tgsi/tgsi_dump.h"
43
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010044#include "si_pipe.h"
45#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050046#include "sid.h"
47
Tom Stellarda75c6162012-01-06 17:38:37 -050048#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050049
Tom Stellard2397a722014-12-10 09:13:59 -050050static const char *scratch_rsrc_dword0_symbol =
51 "SCRATCH_RSRC_DWORD0";
52
53static const char *scratch_rsrc_dword1_symbol =
54 "SCRATCH_RSRC_DWORD1";
55
Michel Dänzer404b29d2013-11-21 16:45:28 +090056struct si_shader_output_values
57{
58 LLVMValueRef values[4];
59 unsigned name;
Michel Dänzer67e385b2014-01-08 17:48:21 +090060 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090061};
62
Tom Stellarda75c6162012-01-06 17:38:37 -050063struct si_shader_context
64{
65 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050066 struct tgsi_parse_context parse;
67 struct tgsi_token * tokens;
Marek Olšák8c37c162014-09-16 18:40:07 +020068 struct si_shader *shader;
Marek Olšák368b0a72014-12-28 21:51:35 +010069 struct si_screen *screen;
Tom Stellarda75c6162012-01-06 17:38:37 -050070 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020071 int param_streamout_config;
72 int param_streamout_write_index;
73 int param_streamout_offset[4];
74 int param_vertex_id;
75 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010076 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020077 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020078 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020079 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Marek Olšák55a9b772014-10-05 12:38:54 +020080 LLVMValueRef resources[SI_NUM_SAMPLER_VIEWS];
81 LLVMValueRef samplers[SI_NUM_SAMPLER_STATES];
Marek Olšák8d03d922013-09-01 23:59:06 +020082 LLVMValueRef so_buffers[4];
Marek Olšákfc3b3352014-10-05 13:33:40 +020083 LLVMValueRef esgs_ring;
84 LLVMValueRef gsvs_ring;
Michel Dänzerf07a96d2014-01-08 18:45:10 +090085 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050086};
87
88static struct si_shader_context * si_shader_context(
89 struct lp_build_tgsi_context * bld_base)
90{
91 return (struct si_shader_context *)bld_base;
92}
93
94
95#define PERSPECTIVE_BASE 0
96#define LINEAR_BASE 9
97
98#define SAMPLE_OFFSET 0
99#define CENTER_OFFSET 2
100#define CENTROID_OFSET 4
101
102#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -0400103#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +0200104#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -0400105#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -0500106
Michel Dänzer404b29d2013-11-21 16:45:28 +0900107
108#define SENDMSG_GS 2
109#define SENDMSG_GS_DONE 3
110
111#define SENDMSG_GS_OP_NOP (0 << 4)
112#define SENDMSG_GS_OP_CUT (1 << 4)
113#define SENDMSG_GS_OP_EMIT (2 << 4)
114#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
115
Marek Olšáke29353f2014-09-17 22:17:02 +0200116/**
117 * Returns a unique index for a semantic name and index. The index must be
118 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
119 * calculated.
120 */
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200121unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
Marek Olšáke29353f2014-09-17 22:17:02 +0200122{
123 switch (semantic_name) {
124 case TGSI_SEMANTIC_POSITION:
125 return 0;
126 case TGSI_SEMANTIC_PSIZE:
127 return 1;
128 case TGSI_SEMANTIC_CLIPDIST:
129 assert(index <= 1);
130 return 2 + index;
131 case TGSI_SEMANTIC_CLIPVERTEX:
132 return 4;
133 case TGSI_SEMANTIC_COLOR:
134 assert(index <= 1);
135 return 5 + index;
136 case TGSI_SEMANTIC_BCOLOR:
137 assert(index <= 1);
138 return 7 + index;
139 case TGSI_SEMANTIC_FOG:
140 return 9;
141 case TGSI_SEMANTIC_EDGEFLAG:
142 return 10;
143 case TGSI_SEMANTIC_GENERIC:
144 assert(index <= 63-11);
145 return 11 + index;
146 default:
147 assert(0);
148 return 63;
149 }
150}
151
152/**
153 * Given a semantic name and index of a parameter and a mask of used parameters
154 * (inputs or outputs), return the index of the parameter in the list of all
155 * used parameters.
156 *
157 * For example, assume this list of parameters:
158 * POSITION, PSIZE, GENERIC0, GENERIC2
159 * which has the mask:
160 * 11000000000101
161 * Then:
162 * querying POSITION returns 0,
163 * querying PSIZE returns 1,
164 * querying GENERIC0 returns 2,
165 * querying GENERIC2 returns 3.
166 *
167 * Which can be used as an offset to a parameter buffer in units of vec4s.
168 */
169static int get_param_index(unsigned semantic_name, unsigned index,
170 uint64_t mask)
171{
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200172 unsigned unique_index = si_shader_io_get_unique_index(semantic_name, index);
Marek Olšáke29353f2014-09-17 22:17:02 +0200173 int i, param_index = 0;
174
175 /* If not present... */
176 if (!((1llu << unique_index) & mask))
177 return -1;
178
179 for (i = 0; mask; i++) {
180 uint64_t bit = 1llu << i;
181
182 if (bit & mask) {
183 if (i == unique_index)
184 return param_index;
185
186 mask &= ~bit;
187 param_index++;
188 }
189 }
190
191 assert(!"unreachable");
192 return -1;
193}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900194
Tom Stellard467f5162012-05-16 15:15:35 -0400195/**
Marek Olšákd7876082014-09-26 23:06:32 +0200196 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
197 * It's equivalent to doing a load from &base_ptr[index].
Tom Stellard467f5162012-05-16 15:15:35 -0400198 *
Marek Olšákd7876082014-09-26 23:06:32 +0200199 * \param base_ptr Where the array starts.
200 * \param index The element index into the array.
Tom Stellard467f5162012-05-16 15:15:35 -0400201 */
Marek Olšákd7876082014-09-26 23:06:32 +0200202static LLVMValueRef build_indexed_load(struct si_shader_context *si_shader_ctx,
203 LLVMValueRef base_ptr, LLVMValueRef index)
Tom Stellard467f5162012-05-16 15:15:35 -0400204{
Marek Olšákd7876082014-09-26 23:06:32 +0200205 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
206 struct gallivm_state *gallivm = bld_base->base.gallivm;
207 LLVMValueRef indices[2], pointer;
Tom Stellard467f5162012-05-16 15:15:35 -0400208
Marek Olšákd7876082014-09-26 23:06:32 +0200209 indices[0] = bld_base->uint_bld.zero;
210 indices[1] = index;
Christian König206f0592013-03-20 14:37:21 +0100211
Marek Olšákd7876082014-09-26 23:06:32 +0200212 pointer = LLVMBuildGEP(gallivm->builder, base_ptr, indices, 2, "");
213 return LLVMBuildLoad(gallivm->builder, pointer, "");
214}
215
216/**
217 * Do a load from &base_ptr[index], but also add a flag that it's loading
218 * a constant.
219 */
220static LLVMValueRef build_indexed_load_const(
221 struct si_shader_context * si_shader_ctx,
222 LLVMValueRef base_ptr, LLVMValueRef index)
223{
224 LLVMValueRef result = build_indexed_load(si_shader_ctx, base_ptr, index);
Christian König206f0592013-03-20 14:37:21 +0100225 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
226 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400227}
228
Marek Olšákf317ce52013-09-05 15:39:57 +0200229static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100230 struct radeon_llvm_context * radeon_bld,
231 unsigned divisor)
232{
Marek Olšák8d03d922013-09-01 23:59:06 +0200233 struct si_shader_context *si_shader_ctx =
234 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100235 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
236
Marek Olšák8d03d922013-09-01 23:59:06 +0200237 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
238 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100239
Marek Olšák50908a82015-01-31 19:00:44 +0100240 /* The division must be done before START_INSTANCE is added. */
Christian Königa0dca442013-03-22 15:59:22 +0100241 if (divisor > 1)
242 result = LLVMBuildUDiv(gallivm->builder, result,
243 lp_build_const_int32(gallivm, divisor), "");
244
Marek Olšák50908a82015-01-31 19:00:44 +0100245 return LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
246 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
Christian Königa0dca442013-03-22 15:59:22 +0100247}
248
Tom Stellarda75c6162012-01-06 17:38:37 -0500249static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900250 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500251 unsigned input_index,
252 const struct tgsi_full_declaration *decl)
253{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900254 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
255 struct gallivm_state *gallivm = base->gallivm;
256 struct si_shader_context *si_shader_ctx =
257 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100258 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
259
260 unsigned chan;
261
Tom Stellarda75c6162012-01-06 17:38:37 -0500262 LLVMValueRef t_list_ptr;
263 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400264 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500265 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100266 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400267 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500268 LLVMTypeRef vec4_type;
269 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500270
Tom Stellard467f5162012-05-16 15:15:35 -0400271 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100272 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500273
Michel Dänzer51f89a02013-12-09 15:33:53 +0900274 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400275
Marek Olšákd7876082014-09-26 23:06:32 +0200276 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400277
278 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900279 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500280
Christian Königa0dca442013-03-22 15:59:22 +0100281 if (divisor) {
282 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200283 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200284 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100285 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200286 /* Load the buffer index for vertices. */
287 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
288 si_shader_ctx->param_vertex_id);
289 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
290 SI_PARAM_BASE_VERTEX);
291 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100292 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500293
294 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400295 args[0] = t_list;
296 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100297 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900298 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100299 "llvm.SI.vs.load.input", vec4_type, args, 3,
300 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500301
302 /* Break up the vec4 into individual components */
303 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900304 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500305 /* XXX: Use a helper function for this. There is one in
306 * tgsi_llvm.c. */
307 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900308 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500309 input, llvm_chan, "");
310 }
311}
312
Michel Dänzer404b29d2013-11-21 16:45:28 +0900313static LLVMValueRef fetch_input_gs(
314 struct lp_build_tgsi_context *bld_base,
315 const struct tgsi_full_src_register *reg,
316 enum tgsi_opcode_type type,
317 unsigned swizzle)
318{
319 struct lp_build_context *base = &bld_base->base;
320 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200321 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900322 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
323 struct gallivm_state *gallivm = base->gallivm;
324 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
325 LLVMValueRef vtx_offset;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900326 LLVMValueRef args[9];
327 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200328 struct tgsi_shader_info *info = &shader->selector->info;
329 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
330 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900331
Marek Olšáke23fec12014-10-04 17:40:39 +0200332 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900333 if (swizzle == 0)
334 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
335 SI_PARAM_PRIMITIVE_ID);
336 else
337 return uint->zero;
338 }
339
Michel Dänzer404b29d2013-11-21 16:45:28 +0900340 if (!reg->Register.Dimension)
341 return NULL;
342
343 if (swizzle == ~0) {
344 LLVMValueRef values[TGSI_NUM_CHANNELS];
345 unsigned chan;
346 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
347 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
348 }
349 return lp_build_gather_values(bld_base->base.gallivm, values,
350 TGSI_NUM_CHANNELS);
351 }
352
353 /* Get the vertex offset parameter */
354 vtx_offset_param = reg->Dimension.Index;
355 if (vtx_offset_param < 2) {
356 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
357 } else {
358 assert(vtx_offset_param < 6);
359 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
360 }
361 vtx_offset = lp_build_mul_imm(uint,
362 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
363 vtx_offset_param),
364 4);
365
Marek Olšákfc3b3352014-10-05 13:33:40 +0200366 args[0] = si_shader_ctx->esgs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900367 args[1] = vtx_offset;
368 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200369 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200370 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900371 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900372 args[3] = uint->zero;
373 args[4] = uint->one; /* OFFEN */
374 args[5] = uint->zero; /* IDXEN */
375 args[6] = uint->one; /* GLC */
376 args[7] = uint->zero; /* SLC */
377 args[8] = uint->zero; /* TFE */
378
379 return LLVMBuildBitCast(gallivm->builder,
380 build_intrinsic(gallivm->builder,
381 "llvm.SI.buffer.load.dword.i32.i32",
382 i32, args, 9,
383 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
384 tgsi2llvmtype(bld_base, type), "");
385}
386
Tom Stellarda75c6162012-01-06 17:38:37 -0500387static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900388 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500389 unsigned input_index,
390 const struct tgsi_full_declaration *decl)
391{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900392 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
393 struct si_shader_context *si_shader_ctx =
394 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200395 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900396 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
397 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400398 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900399 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100400
401 LLVMValueRef interp_param;
402 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500403
404 /* This value is:
405 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
406 * quad begins a new primitive. Bit 0 always needs
407 * to be unset)
408 * [32:16] ParamOffset
409 *
410 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900411 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200412 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500413
Christian König0666ffd2013-03-05 15:07:39 +0100414 unsigned chan;
415
Tom Stellard0fb1e682012-09-06 16:18:11 -0400416 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
417 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400418 unsigned soa_index =
419 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900420 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100421 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100422
423 if (chan == 3)
424 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900425 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100426 LLVMBuildFDiv(gallivm->builder,
427 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900428 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100429 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400430 }
431 return;
432 }
433
Michel Dänzer97078b12012-09-25 12:41:31 +0200434 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900435 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Marek Olšáke827bb62014-10-16 16:20:26 +0200436 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900437 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
438 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200439 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900440 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200441 lp_build_const_float(gallivm, 1.0f);
442
443 return;
444 }
445
Marek Olšák8b057dd2014-10-04 22:15:07 +0200446 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200447 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200448 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200449
Francisco Jerez12799232012-04-30 18:27:52 +0200450 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500451 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100452 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500453 break;
454 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200455 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200456 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400457 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100458 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200459 else
Christian König0666ffd2013-03-05 15:07:39 +0100460 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200461 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200462 case TGSI_INTERPOLATE_COLOR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200463 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200464 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200465 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400466 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100467 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200468 else
Christian König0666ffd2013-03-05 15:07:39 +0100469 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500470 break;
471 default:
472 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
473 return;
474 }
475
Marek Olšák13de9472015-01-04 20:09:51 +0100476 /* fs.constant returns the param from the middle vertex, so it's not
477 * really useful for flat shading. It's meant to be used for custom
478 * interpolation (but the intrinsic can't fetch from the other two
479 * vertices).
480 *
481 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
482 * to do the right thing. The only reason we use fs.constant is that
483 * fs.interp cannot be used on integers, because they can be equal
484 * to NaN.
485 */
Christian König0666ffd2013-03-05 15:07:39 +0100486 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
487
Michel Dänzer691f08d2012-09-06 18:03:38 +0200488 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100489 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100490 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200491 LLVMValueRef face, is_face_positive;
492 LLVMValueRef back_attr_number =
493 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200494 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200495
Christian König0666ffd2013-03-05 15:07:39 +0100496 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
497
Michel Dänzer691f08d2012-09-06 18:03:38 +0200498 is_face_positive = LLVMBuildFCmp(gallivm->builder,
Marek Olšákd1d2af22015-01-04 20:45:35 +0100499 LLVMRealOGT, face,
Michel Dänzer691f08d2012-09-06 18:03:38 +0200500 lp_build_const_float(gallivm, 0.0f),
501 "");
502
Tom Stellarda75c6162012-01-06 17:38:37 -0500503 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100504 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200505 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
506 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
507 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
508 LLVMValueRef front, back;
509
510 args[0] = llvm_chan;
511 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900512 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100513 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100514 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200515
516 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900517 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100518 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100519 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200520
Michel Dänzer51f89a02013-12-09 15:33:53 +0900521 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200522 LLVMBuildSelect(gallivm->builder,
523 is_face_positive,
524 front,
525 back,
526 "");
527 }
528
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900529 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200530 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
531 LLVMValueRef args[4];
532
533 args[0] = uint->zero;
534 args[1] = attr_number;
535 args[2] = params;
536 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900537 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
538 build_intrinsic(gallivm->builder, intr_name,
539 input_type, args, args[3] ? 4 : 3,
540 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
541 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
542 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200543 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900544 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200545 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200546 } else {
547 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100548 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200549 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
550 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
551 args[0] = llvm_chan;
552 args[1] = attr_number;
553 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100554 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900555 radeon_bld->inputs[soa_index] =
556 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100557 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100558 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200559 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500560 }
561}
562
Marek Olšák5b06fc32014-05-06 18:12:40 +0200563static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
564{
565 struct gallivm_state *gallivm = &radeon_bld->gallivm;
566 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
567 SI_PARAM_ANCILLARY);
568 value = LLVMBuildLShr(gallivm->builder, value,
569 lp_build_const_int32(gallivm, 8), "");
570 value = LLVMBuildAnd(gallivm->builder, value,
571 lp_build_const_int32(gallivm, 0xf), "");
572 return value;
573}
574
Marek Olšákd7876082014-09-26 23:06:32 +0200575/**
576 * Load a dword from a constant buffer.
577 */
578static LLVMValueRef buffer_load_const(LLVMBuilderRef builder, LLVMValueRef resource,
579 LLVMValueRef offset, LLVMTypeRef return_type)
Marek Olšák250aa932014-05-06 14:10:47 +0200580{
581 LLVMValueRef args[2] = {resource, offset};
582
583 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
584 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
585}
586
Christian Könige4ed5872013-03-21 18:02:52 +0100587static void declare_system_value(
588 struct radeon_llvm_context * radeon_bld,
589 unsigned index,
590 const struct tgsi_full_declaration *decl)
591{
Marek Olšák8d03d922013-09-01 23:59:06 +0200592 struct si_shader_context *si_shader_ctx =
593 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200594 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
595 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100596 LLVMValueRef value = 0;
597
598 switch (decl->Semantic.Name) {
599 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200600 value = LLVMGetParam(radeon_bld->main_fn,
601 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100602 break;
603
604 case TGSI_SEMANTIC_VERTEXID:
Marek Olšákd7c6f392015-01-04 14:41:49 +0100605 value = LLVMBuildAdd(gallivm->builder,
606 LLVMGetParam(radeon_bld->main_fn,
607 si_shader_ctx->param_vertex_id),
608 LLVMGetParam(radeon_bld->main_fn,
609 SI_PARAM_BASE_VERTEX), "");
Christian Könige4ed5872013-03-21 18:02:52 +0100610 break;
611
Marek Olšáke3d4bdd2015-01-04 14:51:01 +0100612 case TGSI_SEMANTIC_VERTEXID_NOBASE:
613 value = LLVMGetParam(radeon_bld->main_fn,
614 si_shader_ctx->param_vertex_id);
615 break;
616
617 case TGSI_SEMANTIC_BASEVERTEX:
618 value = LLVMGetParam(radeon_bld->main_fn,
619 SI_PARAM_BASE_VERTEX);
620 break;
621
Marek Olšák5b06fc32014-05-06 18:12:40 +0200622 case TGSI_SEMANTIC_SAMPLEID:
623 value = get_sample_id(radeon_bld);
624 break;
625
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200626 case TGSI_SEMANTIC_SAMPLEPOS:
627 {
628 LLVMBuilderRef builder = gallivm->builder;
629 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200630 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200631 LLVMValueRef resource = build_indexed_load_const(si_shader_ctx, desc, buf_index);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200632
633 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
634 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
635 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
636
637 LLVMValueRef pos[4] = {
Marek Olšákd7876082014-09-26 23:06:32 +0200638 buffer_load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
639 buffer_load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200640 lp_build_const_float(gallivm, 0),
641 lp_build_const_float(gallivm, 0)
642 };
643 value = lp_build_gather_values(gallivm, pos, 4);
644 break;
645 }
646
Christian Könige4ed5872013-03-21 18:02:52 +0100647 default:
648 assert(!"unknown system value");
649 return;
650 }
651
652 radeon_bld->system_values[index] = value;
653}
654
Tom Stellarda75c6162012-01-06 17:38:37 -0500655static LLVMValueRef fetch_constant(
656 struct lp_build_tgsi_context * bld_base,
657 const struct tgsi_full_src_register *reg,
658 enum tgsi_opcode_type type,
659 unsigned swizzle)
660{
Christian König55fe5cc2013-03-04 16:30:06 +0100661 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500662 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100663 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200664 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500665
Christian König0f6cf2b2013-03-15 15:53:25 +0100666 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100667 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500668
Christian König8514f5a2013-02-04 17:46:42 +0100669 if (swizzle == LP_CHAN_ALL) {
670 unsigned chan;
671 LLVMValueRef values[4];
672 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
673 values[chan] = fetch_constant(bld_base, reg, type, chan);
674
675 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
676 }
677
Marek Olšák2fd42002013-10-25 11:45:47 +0200678 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100679 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100680
Marek Olšák2fd42002013-10-25 11:45:47 +0200681 if (!reg->Register.Indirect)
682 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
683
Christian König0f6cf2b2013-03-15 15:53:25 +0100684 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
685 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
686 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200687 addr = lp_build_add(&bld_base->uint_bld, addr,
688 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200689
Marek Olšákd7876082014-09-26 23:06:32 +0200690 result = buffer_load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
Marek Olšák250aa932014-05-06 14:10:47 +0200691 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500692
Christian Königf5298b02013-02-28 14:50:07 +0100693 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500694}
695
Michel Dänzer26c71392012-08-24 12:03:11 +0200696/* Initialize arguments for the shader export intrinsic */
697static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900698 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200699 unsigned target,
700 LLVMValueRef *args)
701{
702 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
703 struct lp_build_context *uint =
704 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
705 struct lp_build_context *base = &bld_base->base;
706 unsigned compressed = 0;
707 unsigned chan;
708
Michel Dänzerf402acd2012-08-22 18:15:36 +0200709 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
710 int cbuf = target - V_008DFC_SQ_EXP_MRT;
711
712 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100713 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100714
715 if (compressed)
716 si_shader_ctx->shader->spi_shader_col_format |=
717 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
718 else
719 si_shader_ctx->shader->spi_shader_col_format |=
720 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200721
722 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200723 }
724 }
725
726 if (compressed) {
727 /* Pixel shader needs to pack output values before export */
728 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900729 args[0] = values[2 * chan];
730 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200731 args[chan + 5] =
732 build_intrinsic(base->gallivm->builder,
733 "llvm.SI.packf16",
734 LLVMInt32TypeInContext(base->gallivm->context),
735 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100736 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100737 args[chan + 7] = args[chan + 5] =
738 LLVMBuildBitCast(base->gallivm->builder,
739 args[chan + 5],
740 LLVMFloatTypeInContext(base->gallivm->context),
741 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200742 }
743
744 /* Set COMPR flag */
745 args[4] = uint->one;
746 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900747 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200748 /* +5 because the first output value will be
749 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900750 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200751
752 /* Clear COMPR flag */
753 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200754 }
755
756 /* XXX: This controls which components of the output
757 * registers actually get exported. (e.g bit 0 means export
758 * X component, bit 1 means export Y component, etc.) I'm
759 * hard coding this to 0xf for now. In the future, we might
760 * want to do something else. */
761 args[0] = lp_build_const_int32(base->gallivm, 0xf);
762
763 /* Specify whether the EXEC mask represents the valid mask */
764 args[1] = uint->zero;
765
766 /* Specify whether this is the last export */
767 args[2] = uint->zero;
768
769 /* Specify the target we are exporting */
770 args[3] = lp_build_const_int32(base->gallivm, target);
771
Michel Dänzer26c71392012-08-24 12:03:11 +0200772 /* XXX: We probably need to keep track of the output
773 * values, so we know what we are passing to the next
774 * stage. */
775}
776
Michel Dänzer404b29d2013-11-21 16:45:28 +0900777/* Load from output pointers and initialize arguments for the shader export intrinsic */
778static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
779 LLVMValueRef *out_ptr,
780 unsigned target,
781 LLVMValueRef *args)
782{
783 struct gallivm_state *gallivm = bld_base->base.gallivm;
784 LLVMValueRef values[4];
785 int i;
786
787 for (i = 0; i < 4; i++)
788 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
789
790 si_llvm_init_export_args(bld_base, values, target, args);
791}
792
Michel Dänzer7708a862012-11-02 15:57:30 +0100793static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900794 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100795{
796 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
797 struct gallivm_state *gallivm = bld_base->base.gallivm;
798
Christian Königa0dca442013-03-22 15:59:22 +0100799 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400800 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
801 SI_PARAM_ALPHA_REF);
802
Michel Dänzer7708a862012-11-02 15:57:30 +0100803 LLVMValueRef alpha_pass =
804 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100805 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900806 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400807 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100808 LLVMValueRef arg =
809 lp_build_select(&bld_base->base,
810 alpha_pass,
811 lp_build_const_float(gallivm, 1.0f),
812 lp_build_const_float(gallivm, -1.0f));
813
814 build_intrinsic(gallivm->builder,
815 "llvm.AMDGPU.kill",
816 LLVMVoidTypeInContext(gallivm->context),
817 &arg, 1, 0);
818 } else {
819 build_intrinsic(gallivm->builder,
820 "llvm.AMDGPU.kilp",
821 LLVMVoidTypeInContext(gallivm->context),
822 NULL, 0, 0);
823 }
Marek Olšákadc57972014-09-19 17:07:07 +0200824
825 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100826}
827
Michel Dänzere3befbc2013-05-15 18:09:50 +0200828static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900829 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200830{
831 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
832 struct lp_build_context *base = &bld_base->base;
833 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200834 unsigned reg_index;
835 unsigned chan;
836 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200837 LLVMValueRef base_elt;
838 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200839 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200840 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200841
Michel Dänzere3befbc2013-05-15 18:09:50 +0200842 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200843 LLVMValueRef *args = pos[2 + reg_index];
844
Michel Dänzere3befbc2013-05-15 18:09:50 +0200845 args[5] =
846 args[6] =
847 args[7] =
848 args[8] = lp_build_const_float(base->gallivm, 0.0f);
849
850 /* Compute dot products of position and user clip plane vectors */
851 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
852 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200853 args[1] = lp_build_const_int32(base->gallivm,
854 ((reg_index * 4 + chan) * 4 +
855 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200856 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200857 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200858 args[5 + chan] =
859 lp_build_add(base, args[5 + chan],
860 lp_build_mul(base, base_elt,
861 out_elts[const_chan]));
862 }
863 }
864
865 args[0] = lp_build_const_int32(base->gallivm, 0xf);
866 args[1] = uint->zero;
867 args[2] = uint->zero;
868 args[3] = lp_build_const_int32(base->gallivm,
869 V_008DFC_SQ_EXP_POS + 2 + reg_index);
870 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200871 }
872}
873
Marek Olšák8d03d922013-09-01 23:59:06 +0200874static void si_dump_streamout(struct pipe_stream_output_info *so)
875{
876 unsigned i;
877
878 if (so->num_outputs)
879 fprintf(stderr, "STREAMOUT\n");
880
881 for (i = 0; i < so->num_outputs; i++) {
882 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
883 so->output[i].start_component;
884 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
885 i, so->output[i].output_buffer,
886 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
887 so->output[i].register_index,
888 mask & 1 ? "x" : "",
889 mask & 2 ? "y" : "",
890 mask & 4 ? "z" : "",
891 mask & 8 ? "w" : "");
892 }
893}
894
895/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
896 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
897 * or v4i32 (num_channels=3,4). */
898static void build_tbuffer_store(struct si_shader_context *shader,
899 LLVMValueRef rsrc,
900 LLVMValueRef vdata,
901 unsigned num_channels,
902 LLVMValueRef vaddr,
903 LLVMValueRef soffset,
904 unsigned inst_offset,
905 unsigned dfmt,
906 unsigned nfmt,
907 unsigned offen,
908 unsigned idxen,
909 unsigned glc,
910 unsigned slc,
911 unsigned tfe)
912{
913 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
914 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
915 LLVMValueRef args[] = {
916 rsrc,
917 vdata,
918 LLVMConstInt(i32, num_channels, 0),
919 vaddr,
920 soffset,
921 LLVMConstInt(i32, inst_offset, 0),
922 LLVMConstInt(i32, dfmt, 0),
923 LLVMConstInt(i32, nfmt, 0),
924 LLVMConstInt(i32, offen, 0),
925 LLVMConstInt(i32, idxen, 0),
926 LLVMConstInt(i32, glc, 0),
927 LLVMConstInt(i32, slc, 0),
928 LLVMConstInt(i32, tfe, 0)
929 };
930
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900931 /* The instruction offset field has 12 bits */
932 assert(offen || inst_offset < (1 << 12));
933
Marek Olšák8d03d922013-09-01 23:59:06 +0200934 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
935 unsigned func = CLAMP(num_channels, 1, 3) - 1;
936 const char *types[] = {"i32", "v2i32", "v4i32"};
937 char name[256];
938 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
939
940 lp_build_intrinsic(gallivm->builder, name,
941 LLVMVoidTypeInContext(gallivm->context),
942 args, Elements(args));
943}
944
945static void build_streamout_store(struct si_shader_context *shader,
946 LLVMValueRef rsrc,
947 LLVMValueRef vdata,
948 unsigned num_channels,
949 LLVMValueRef vaddr,
950 LLVMValueRef soffset,
951 unsigned inst_offset)
952{
953 static unsigned dfmt[] = {
954 V_008F0C_BUF_DATA_FORMAT_32,
955 V_008F0C_BUF_DATA_FORMAT_32_32,
956 V_008F0C_BUF_DATA_FORMAT_32_32_32,
957 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
958 };
959 assert(num_channels >= 1 && num_channels <= 4);
960
961 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
962 inst_offset, dfmt[num_channels-1],
963 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
964}
965
966/* On SI, the vertex shader is responsible for writing streamout data
967 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900968static void si_llvm_emit_streamout(struct si_shader_context *shader,
969 struct si_shader_output_values *outputs,
970 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200971{
972 struct pipe_stream_output_info *so = &shader->shader->selector->so;
973 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
974 LLVMBuilderRef builder = gallivm->builder;
975 int i, j;
976 struct lp_build_if_state if_ctx;
977
978 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
979
980 LLVMValueRef so_param =
981 LLVMGetParam(shader->radeon_bld.main_fn,
982 shader->param_streamout_config);
983
984 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
985 LLVMValueRef so_vtx_count =
986 LLVMBuildAnd(builder,
987 LLVMBuildLShr(builder, so_param,
988 LLVMConstInt(i32, 16, 0), ""),
989 LLVMConstInt(i32, 127, 0), "");
990
991 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
992 NULL, 0, LLVMReadNoneAttribute);
993
994 /* can_emit = tid < so_vtx_count; */
995 LLVMValueRef can_emit =
996 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
997
998 /* Emit the streamout code conditionally. This actually avoids
999 * out-of-bounds buffer access. The hw tells us via the SGPR
1000 * (so_vtx_count) which threads are allowed to emit streamout data. */
1001 lp_build_if(&if_ctx, gallivm, can_emit);
1002 {
1003 /* The buffer offset is computed as follows:
1004 * ByteOffset = streamout_offset[buffer_id]*4 +
1005 * (streamout_write_index + thread_id)*stride[buffer_id] +
1006 * attrib_offset
1007 */
1008
1009 LLVMValueRef so_write_index =
1010 LLVMGetParam(shader->radeon_bld.main_fn,
1011 shader->param_streamout_write_index);
1012
1013 /* Compute (streamout_write_index + thread_id). */
1014 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1015
1016 /* Compute the write offset for each enabled buffer. */
1017 LLVMValueRef so_write_offset[4] = {};
1018 for (i = 0; i < 4; i++) {
1019 if (!so->stride[i])
1020 continue;
1021
1022 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1023 shader->param_streamout_offset[i]);
1024 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1025
1026 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1027 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1028 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1029 }
1030
Marek Olšák8d03d922013-09-01 23:59:06 +02001031 /* Write streamout data. */
1032 for (i = 0; i < so->num_outputs; i++) {
1033 unsigned buf_idx = so->output[i].output_buffer;
1034 unsigned reg = so->output[i].register_index;
1035 unsigned start = so->output[i].start_component;
1036 unsigned num_comps = so->output[i].num_components;
1037 LLVMValueRef out[4];
1038
1039 assert(num_comps && num_comps <= 4);
1040 if (!num_comps || num_comps > 4)
1041 continue;
1042
Marek Olšák558f7772014-10-04 22:37:23 +02001043 if (reg >= noutput)
1044 continue;
1045
Marek Olšák8d03d922013-09-01 23:59:06 +02001046 /* Load the output as int. */
1047 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001048 out[j] = LLVMBuildBitCast(builder,
1049 outputs[reg].values[start+j],
1050 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001051 }
1052
1053 /* Pack the output. */
1054 LLVMValueRef vdata = NULL;
1055
1056 switch (num_comps) {
1057 case 1: /* as i32 */
1058 vdata = out[0];
1059 break;
1060 case 2: /* as v2i32 */
1061 case 3: /* as v4i32 (aligned to 4) */
1062 case 4: /* as v4i32 */
1063 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1064 for (j = 0; j < num_comps; j++) {
1065 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1066 LLVMConstInt(i32, j, 0), "");
1067 }
1068 break;
1069 }
1070
1071 build_streamout_store(shader, shader->so_buffers[buf_idx],
1072 vdata, num_comps,
1073 so_write_offset[buf_idx],
1074 LLVMConstInt(i32, 0, 0),
1075 so->output[i].dst_offset*4);
1076 }
1077 }
1078 lp_build_endif(&if_ctx);
1079}
1080
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001081
Michel Dänzer404b29d2013-11-21 16:45:28 +09001082/* Generate export instructions for hardware VS shader stage */
1083static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1084 struct si_shader_output_values *outputs,
1085 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001086{
1087 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001088 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001089 struct lp_build_context * base = &bld_base->base;
1090 struct lp_build_context * uint =
1091 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001092 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001093 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001094 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001095 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001096 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001097 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001098 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001099 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001100
Michel Dänzer67e385b2014-01-08 17:48:21 +09001101 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1102 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001103 }
1104
Michel Dänzer404b29d2013-11-21 16:45:28 +09001105 for (i = 0; i < noutput; i++) {
1106 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001107 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001108
Michel Dänzer0afeea52013-05-02 14:53:17 +02001109handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001110 /* Select the correct target */
1111 switch(semantic_name) {
1112 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001113 psize_value = outputs[i].values[0];
1114 continue;
1115 case TGSI_SEMANTIC_EDGEFLAG:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001116 edgeflag_value = outputs[i].values[0];
1117 continue;
1118 case TGSI_SEMANTIC_LAYER:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001119 layer_value = outputs[i].values[0];
1120 continue;
1121 case TGSI_SEMANTIC_POSITION:
1122 target = V_008DFC_SQ_EXP_POS;
1123 break;
1124 case TGSI_SEMANTIC_COLOR:
1125 case TGSI_SEMANTIC_BCOLOR:
1126 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001127 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001128 param_count++;
1129 break;
1130 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001131 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1132 break;
1133 case TGSI_SEMANTIC_CLIPVERTEX:
1134 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1135 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001136 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001137 case TGSI_SEMANTIC_FOG:
1138 case TGSI_SEMANTIC_GENERIC:
1139 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001140 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001141 param_count++;
1142 break;
1143 default:
1144 target = 0;
1145 fprintf(stderr,
1146 "Warning: SI unhandled vs output type:%d\n",
1147 semantic_name);
1148 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001149
Michel Dänzer404b29d2013-11-21 16:45:28 +09001150 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001151
Michel Dänzer404b29d2013-11-21 16:45:28 +09001152 if (target >= V_008DFC_SQ_EXP_POS &&
1153 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1154 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1155 args, sizeof(args));
1156 } else {
1157 lp_build_intrinsic(base->gallivm->builder,
1158 "llvm.SI.export",
1159 LLVMVoidTypeInContext(base->gallivm->context),
1160 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001161 }
1162
1163 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1164 semantic_name = TGSI_SEMANTIC_GENERIC;
1165 goto handle_semantic;
1166 }
1167 }
1168
1169 /* We need to add the position output manually if it's missing. */
1170 if (!pos_args[0][0]) {
1171 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1172 pos_args[0][1] = uint->zero; /* EXEC mask */
1173 pos_args[0][2] = uint->zero; /* last export? */
1174 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1175 pos_args[0][4] = uint->zero; /* COMPR flag */
1176 pos_args[0][5] = base->zero; /* X */
1177 pos_args[0][6] = base->zero; /* Y */
1178 pos_args[0][7] = base->zero; /* Z */
1179 pos_args[0][8] = base->one; /* W */
1180 }
1181
1182 /* Write the misc vector (point size, edgeflag, layer, viewport). */
Marek Olšák16153472014-12-07 19:30:08 +01001183 if (shader->selector->info.writes_psize ||
1184 shader->selector->info.writes_edgeflag ||
1185 shader->selector->info.writes_layer) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001186 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
Marek Olšák16153472014-12-07 19:30:08 +01001187 shader->selector->info.writes_psize |
1188 (shader->selector->info.writes_edgeflag << 1) |
1189 (shader->selector->info.writes_layer << 2));
Michel Dänzer51f89a02013-12-09 15:33:53 +09001190 pos_args[1][1] = uint->zero; /* EXEC mask */
1191 pos_args[1][2] = uint->zero; /* last export? */
1192 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1193 pos_args[1][4] = uint->zero; /* COMPR flag */
1194 pos_args[1][5] = base->zero; /* X */
1195 pos_args[1][6] = base->zero; /* Y */
1196 pos_args[1][7] = base->zero; /* Z */
1197 pos_args[1][8] = base->zero; /* W */
1198
Marek Olšák16153472014-12-07 19:30:08 +01001199 if (shader->selector->info.writes_psize)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001200 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001201
Marek Olšák16153472014-12-07 19:30:08 +01001202 if (shader->selector->info.writes_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001203 /* The output is a float, but the hw expects an integer
1204 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001205 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1206 edgeflag_value,
1207 bld_base->uint_bld.elem_type, "");
1208 edgeflag_value = lp_build_min(&bld_base->int_bld,
1209 edgeflag_value,
1210 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001211
1212 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001213 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1214 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001215 base->elem_type, "");
1216 }
1217
Marek Olšák16153472014-12-07 19:30:08 +01001218 if (shader->selector->info.writes_layer)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001219 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001220 }
1221
1222 for (i = 0; i < 4; i++)
1223 if (pos_args[i][0])
1224 shader->nr_pos_exports++;
1225
1226 pos_idx = 0;
1227 for (i = 0; i < 4; i++) {
1228 if (!pos_args[i][0])
1229 continue;
1230
1231 /* Specify the target we are exporting */
1232 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1233
1234 if (pos_idx == shader->nr_pos_exports)
1235 /* Specify that this is the last export */
1236 pos_args[i][2] = uint->one;
1237
1238 lp_build_intrinsic(base->gallivm->builder,
1239 "llvm.SI.export",
1240 LLVMVoidTypeInContext(base->gallivm->context),
1241 pos_args[i], 9);
1242 }
1243}
1244
Michel Dänzer404b29d2013-11-21 16:45:28 +09001245static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1246{
1247 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1248 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001249 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001250 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001251 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001252 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1253 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001254 unsigned chan;
1255 int i;
1256
Marek Olšák216cf862014-10-04 17:04:05 +02001257 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001258 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001259 si_shader_ctx->radeon_bld.soa.outputs[i];
1260 int param_index = get_param_index(info->output_semantic_name[i],
1261 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001262 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001263
Marek Olšáke29353f2014-09-17 22:17:02 +02001264 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001265 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001266
1267 for (chan = 0; chan < 4; chan++) {
1268 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001269 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1270
Marek Olšákfc3b3352014-10-05 13:33:40 +02001271 build_tbuffer_store(si_shader_ctx,
1272 si_shader_ctx->esgs_ring,
1273 out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001274 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001275 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001276 V_008F0C_BUF_DATA_FORMAT_32,
1277 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001278 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001279 }
1280 }
1281}
1282
1283static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1284{
1285 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1286 struct gallivm_state *gallivm = bld_base->base.gallivm;
1287 LLVMValueRef args[2];
1288
1289 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1290 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1291 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1292 LLVMVoidTypeInContext(gallivm->context), args, 2,
1293 LLVMNoUnwindAttribute);
1294}
1295
1296static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1297{
1298 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1299 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001300 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001301 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001302 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001303
Marek Olšákec0d1682014-10-04 22:33:36 +02001304 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001305
Marek Olšákec0d1682014-10-04 22:33:36 +02001306 for (i = 0; i < info->num_outputs; i++) {
1307 outputs[i].name = info->output_semantic_name[i];
1308 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001309
Marek Olšákec0d1682014-10-04 22:33:36 +02001310 for (j = 0; j < 4; j++)
1311 outputs[i].values[j] =
1312 LLVMBuildLoad(gallivm->builder,
1313 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1314 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001315 }
1316
Marek Olšákec0d1682014-10-04 22:33:36 +02001317 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001318 FREE(outputs);
1319}
1320
Michel Dänzer51f89a02013-12-09 15:33:53 +09001321static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1322{
1323 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001324 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001325 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001326 struct lp_build_context * uint = &bld_base->uint_bld;
1327 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001328 LLVMValueRef args[9];
1329 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001330 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001331 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001332
Marek Olšák75e97e22014-10-04 23:13:50 +02001333 for (i = 0; i < info->num_outputs; i++) {
1334 unsigned semantic_name = info->output_semantic_name[i];
1335 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001336 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001337
Marek Olšák75e97e22014-10-04 23:13:50 +02001338 /* Select the correct target */
1339 switch (semantic_name) {
1340 case TGSI_SEMANTIC_POSITION:
1341 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001342 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001343 case TGSI_SEMANTIC_STENCIL:
1344 stencil_index = i;
1345 continue;
1346 case TGSI_SEMANTIC_SAMPLEMASK:
1347 samplemask_index = i;
1348 continue;
1349 case TGSI_SEMANTIC_COLOR:
1350 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1351 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1352 LLVMBuildStore(bld_base->base.gallivm->builder,
1353 bld_base->base.one,
1354 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001355
Marek Olšák75e97e22014-10-04 23:13:50 +02001356 if (semantic_index == 0 &&
1357 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1358 si_alpha_test(bld_base,
1359 si_shader_ctx->radeon_bld.soa.outputs[i]);
1360 break;
1361 default:
1362 target = 0;
1363 fprintf(stderr,
1364 "Warning: SI unhandled fs output type:%d\n",
1365 semantic_name);
1366 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001367
Marek Olšák75e97e22014-10-04 23:13:50 +02001368 si_llvm_init_export_args_load(bld_base,
1369 si_shader_ctx->radeon_bld.soa.outputs[i],
1370 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001371
Marek Olšák75e97e22014-10-04 23:13:50 +02001372 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1373 /* If there is an export instruction waiting to be emitted, do so now. */
1374 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001375 lp_build_intrinsic(base->gallivm->builder,
1376 "llvm.SI.export",
1377 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001378 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001379 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001380
1381 /* This instruction will be emitted at the end of the shader. */
1382 memcpy(last_args, args, sizeof(args));
1383
1384 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1385 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1386 semantic_index == 0 &&
1387 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1388 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1389 si_llvm_init_export_args_load(bld_base,
1390 si_shader_ctx->radeon_bld.soa.outputs[i],
1391 V_008DFC_SQ_EXP_MRT + c, args);
1392 lp_build_intrinsic(base->gallivm->builder,
1393 "llvm.SI.export",
1394 LLVMVoidTypeInContext(base->gallivm->context),
1395 args, 9);
1396 }
1397 }
1398 } else {
1399 lp_build_intrinsic(base->gallivm->builder,
1400 "llvm.SI.export",
1401 LLVMVoidTypeInContext(base->gallivm->context),
1402 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001403 }
1404 }
1405
Marek Olšákd0e8b652014-05-06 20:04:31 +02001406 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001407 LLVMValueRef out_ptr;
1408 unsigned mask = 0;
1409
1410 /* Specify the target we are exporting */
1411 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1412
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001413 args[5] = base->zero; /* R, depth */
1414 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1415 args[7] = base->zero; /* B, sample mask */
1416 args[8] = base->zero; /* A, alpha to mask */
1417
Michel Dänzer1a616c12012-11-13 17:35:09 +01001418 if (depth_index >= 0) {
1419 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1420 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1421 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001422 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001423 }
1424
1425 if (stencil_index >= 0) {
1426 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001427 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001428 mask |= 0x2;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001429 si_shader_ctx->shader->db_shader_control |=
1430 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001431 }
1432
Marek Olšákd0e8b652014-05-06 20:04:31 +02001433 if (samplemask_index >= 0) {
1434 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1435 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001436 mask |= 0x4;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001437 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1438 }
1439
Marek Olšák368b0a72014-12-28 21:51:35 +01001440 /* SI (except OLAND) has a bug that it only looks
1441 * at the X writemask component. */
1442 if (si_shader_ctx->screen->b.chip_class == SI &&
1443 si_shader_ctx->screen->b.family != CHIP_OLAND)
1444 mask |= 0x1;
1445
Marek Olšákd0e8b652014-05-06 20:04:31 +02001446 if (samplemask_index >= 0)
1447 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1448 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001449 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1450 else
1451 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1452
Michel Dänzer1a616c12012-11-13 17:35:09 +01001453 /* Specify which components to enable */
1454 args[0] = lp_build_const_int32(base->gallivm, mask);
1455
1456 args[1] =
1457 args[2] =
1458 args[4] = uint->zero;
1459
1460 if (last_args[0])
1461 lp_build_intrinsic(base->gallivm->builder,
1462 "llvm.SI.export",
1463 LLVMVoidTypeInContext(base->gallivm->context),
1464 args, 9);
1465 else
1466 memcpy(last_args, args, sizeof(args));
1467 }
1468
Michel Dänzer51f89a02013-12-09 15:33:53 +09001469 if (!last_args[0]) {
1470 /* Specify which components to enable */
1471 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001472
Michel Dänzer51f89a02013-12-09 15:33:53 +09001473 /* Specify the target we are exporting */
1474 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001475
Michel Dänzer51f89a02013-12-09 15:33:53 +09001476 /* Set COMPR flag to zero to export data as 32-bit */
1477 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001478
Michel Dänzer51f89a02013-12-09 15:33:53 +09001479 /* dummy bits */
1480 last_args[5]= uint->zero;
1481 last_args[6]= uint->zero;
1482 last_args[7]= uint->zero;
1483 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001484 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001485
1486 /* Specify whether the EXEC mask represents the valid mask */
1487 last_args[1] = uint->one;
1488
1489 /* Specify that this is the last export */
1490 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1491
1492 lp_build_intrinsic(base->gallivm->builder,
1493 "llvm.SI.export",
1494 LLVMVoidTypeInContext(base->gallivm->context),
1495 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001496}
1497
Marek Olšák4855acd2013-08-06 15:08:54 +02001498static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1499 struct lp_build_tgsi_context * bld_base,
1500 struct lp_build_emit_data * emit_data);
1501
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001502static bool tgsi_is_shadow_sampler(unsigned target)
1503{
1504 return target == TGSI_TEXTURE_SHADOW1D ||
1505 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1506 target == TGSI_TEXTURE_SHADOW2D ||
1507 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1508 target == TGSI_TEXTURE_SHADOWCUBE ||
1509 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1510 target == TGSI_TEXTURE_SHADOWRECT;
1511}
1512
Marek Olšák877bb522014-06-25 03:12:46 +02001513static const struct lp_build_tgsi_action tex_action;
1514
Tom Stellarda75c6162012-01-06 17:38:37 -05001515static void tex_fetch_args(
1516 struct lp_build_tgsi_context * bld_base,
1517 struct lp_build_emit_data * emit_data)
1518{
Christian König55fe5cc2013-03-04 16:30:06 +01001519 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001520 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001521 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001522 unsigned opcode = inst->Instruction.Opcode;
1523 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001524 LLVMValueRef coords[4];
1525 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001526 int ref_pos;
1527 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001528 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001529 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001530 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1531 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001532 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001533
1534 if (target == TGSI_TEXTURE_BUFFER) {
1535 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1536 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1537 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1538 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1539
Marek Olšák4f3f0432014-07-07 22:49:15 +02001540 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001541 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1542 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1543 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1544 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1545
1546 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1547 emit_data->args[0] = res;
1548 emit_data->args[1] = bld_base->uint_bld.zero;
1549 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1550 emit_data->arg_count = 3;
1551 return;
1552 }
Tom Stellard467f5162012-05-16 15:15:35 -04001553
Michel Dänzer120efee2013-01-25 12:10:11 +01001554 /* Fetch and project texture coordinates */
1555 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001556 for (chan = 0; chan < 3; chan++ ) {
1557 coords[chan] = lp_build_emit_fetch(bld_base,
1558 emit_data->inst, 0,
1559 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001560 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001561 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1562 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001563 coords[chan],
1564 coords[3]);
1565 }
1566
Michel Dänzer120efee2013-01-25 12:10:11 +01001567 if (opcode == TGSI_OPCODE_TXP)
1568 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001569
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001570 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001571 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001572 /* The offsets are six-bit signed integers packed like this:
1573 * X=[5:0], Y=[13:8], and Z=[21:16].
1574 */
1575 LLVMValueRef offset[3], pack;
1576
1577 assert(inst->Texture.NumOffsets == 1);
1578
1579 for (chan = 0; chan < 3; chan++) {
1580 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1581 emit_data->inst, 0, chan);
1582 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1583 lp_build_const_int32(gallivm, 0x3f), "");
1584 if (chan)
1585 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1586 lp_build_const_int32(gallivm, chan*8), "");
1587 }
1588
1589 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1590 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1591 address[count++] = pack;
1592 }
1593
Michel Dänzer120efee2013-01-25 12:10:11 +01001594 /* Pack LOD bias value */
1595 if (opcode == TGSI_OPCODE_TXB)
1596 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001597 if (opcode == TGSI_OPCODE_TXB2)
1598 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001599
Michel Dänzer120efee2013-01-25 12:10:11 +01001600 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001601 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001602 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1603 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1604 } else {
1605 assert(ref_pos >= 0);
1606 address[count++] = coords[ref_pos];
1607 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001608 }
1609
Marek Olšákb279f0142014-07-04 03:19:58 +02001610 if (target == TGSI_TEXTURE_CUBE ||
1611 target == TGSI_TEXTURE_CUBE_ARRAY ||
1612 target == TGSI_TEXTURE_SHADOWCUBE ||
1613 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1614 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1615
Michel Dänzera6b83c02013-02-21 16:10:55 +01001616 /* Pack user derivatives */
1617 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001618 int num_deriv_channels, param;
1619
1620 switch (target) {
1621 case TGSI_TEXTURE_3D:
1622 num_deriv_channels = 3;
1623 break;
1624 case TGSI_TEXTURE_2D:
1625 case TGSI_TEXTURE_SHADOW2D:
1626 case TGSI_TEXTURE_RECT:
1627 case TGSI_TEXTURE_SHADOWRECT:
1628 case TGSI_TEXTURE_2D_ARRAY:
1629 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1630 case TGSI_TEXTURE_CUBE:
1631 case TGSI_TEXTURE_SHADOWCUBE:
1632 case TGSI_TEXTURE_CUBE_ARRAY:
1633 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1634 num_deriv_channels = 2;
1635 break;
1636 case TGSI_TEXTURE_1D:
1637 case TGSI_TEXTURE_SHADOW1D:
1638 case TGSI_TEXTURE_1D_ARRAY:
1639 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1640 num_deriv_channels = 1;
1641 break;
1642 default:
1643 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001644 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001645
1646 for (param = 1; param <= 2; param++)
1647 for (chan = 0; chan < num_deriv_channels; chan++)
1648 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001649 }
1650
Michel Dänzer120efee2013-01-25 12:10:11 +01001651 /* Pack texture coordinates */
1652 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001653 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001654 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001655 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001656 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001657
Marek Olšákd2bd6342013-09-18 15:40:21 +02001658 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001659 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001660 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001661 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001662 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001663
1664 if (count > 16) {
1665 assert(!"Cannot handle more than 16 texture address parameters");
1666 count = 16;
1667 }
1668
1669 for (chan = 0; chan < count; chan++ ) {
1670 address[chan] = LLVMBuildBitCast(gallivm->builder,
1671 address[chan],
1672 LLVMInt32TypeInContext(gallivm->context),
1673 "");
1674 }
1675
Marek Olšák4855acd2013-08-06 15:08:54 +02001676 /* Adjust the sample index according to FMASK.
1677 *
1678 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1679 * which is the identity mapping. Each nibble says which physical sample
1680 * should be fetched to get that sample.
1681 *
1682 * For example, 0x11111100 means there are only 2 samples stored and
1683 * the second sample covers 3/4 of the pixel. When reading samples 0
1684 * and 1, return physical sample 0 (determined by the first two 0s
1685 * in FMASK), otherwise return physical sample 1.
1686 *
1687 * The sample index should be adjusted as follows:
1688 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1689 */
1690 if (target == TGSI_TEXTURE_2D_MSAA ||
1691 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1692 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1693 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001694 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001695 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001696 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001697
Marek Olšákd2bd6342013-09-18 15:40:21 +02001698 memcpy(txf_address, address, sizeof(txf_address));
1699
1700 if (target == TGSI_TEXTURE_2D_MSAA) {
1701 txf_address[2] = bld_base->uint_bld.zero;
1702 }
1703 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001704
1705 /* Pad to a power-of-two size. */
1706 while (txf_count < util_next_power_of_two(txf_count))
1707 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1708
1709 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001710 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1711 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1712 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001713 txf_emit_data.chan = 0;
1714 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001715 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001716 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001717 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001718 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001719 txf_emit_data.arg_count = 3;
1720
Marek Olšák877bb522014-06-25 03:12:46 +02001721 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001722
1723 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001724 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1725 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1726
1727 /* Apply the formula. */
1728 LLVMValueRef fmask =
1729 LLVMBuildExtractElement(gallivm->builder,
1730 txf_emit_data.output[0],
1731 uint_bld->zero, "");
1732
Marek Olšákd2bd6342013-09-18 15:40:21 +02001733 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001734
1735 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001736 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001737
1738 LLVMValueRef shifted_fmask =
1739 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1740
1741 LLVMValueRef final_sample =
1742 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1743
1744 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1745 * resource descriptor is 0 (invalid),
1746 */
1747 LLVMValueRef fmask_desc =
1748 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001749 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001750 LLVMVectorType(uint_bld->elem_type, 8), "");
1751
1752 LLVMValueRef fmask_word1 =
1753 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1754 uint_bld->one, "");
1755
1756 LLVMValueRef word1_is_nonzero =
1757 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1758 fmask_word1, uint_bld->zero, "");
1759
Marek Olšákd2bd6342013-09-18 15:40:21 +02001760 /* Replace the MSAA sample index. */
1761 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001762 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001763 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001764 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001765
Michel Dänzer36231112013-05-02 09:44:45 +02001766 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001767 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001768
1769 if (opcode == TGSI_OPCODE_TXF) {
1770 /* add tex offsets */
1771 if (inst->Texture.NumOffsets) {
1772 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1773 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1774 const struct tgsi_texture_offset * off = inst->TexOffsets;
1775
1776 assert(inst->Texture.NumOffsets == 1);
1777
Marek Olšákdefedc02013-09-18 15:36:38 +02001778 switch (target) {
1779 case TGSI_TEXTURE_3D:
1780 address[2] = lp_build_add(uint_bld, address[2],
1781 bld->immediates[off->Index][off->SwizzleZ]);
1782 /* fall through */
1783 case TGSI_TEXTURE_2D:
1784 case TGSI_TEXTURE_SHADOW2D:
1785 case TGSI_TEXTURE_RECT:
1786 case TGSI_TEXTURE_SHADOWRECT:
1787 case TGSI_TEXTURE_2D_ARRAY:
1788 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001789 address[1] =
1790 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001791 bld->immediates[off->Index][off->SwizzleY]);
1792 /* fall through */
1793 case TGSI_TEXTURE_1D:
1794 case TGSI_TEXTURE_SHADOW1D:
1795 case TGSI_TEXTURE_1D_ARRAY:
1796 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1797 address[0] =
1798 lp_build_add(uint_bld, address[0],
1799 bld->immediates[off->Index][off->SwizzleX]);
1800 break;
1801 /* texture offsets do not apply to other texture targets */
1802 }
Michel Dänzer36231112013-05-02 09:44:45 +02001803 }
1804
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001805 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1806 emit_data->arg_count = 3;
1807
Michel Dänzer36231112013-05-02 09:44:45 +02001808 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001809 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001810 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001811 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001812 opcode == TGSI_OPCODE_LODQ ||
1813 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001814 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1815 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1816 target == TGSI_TEXTURE_2D_ARRAY ||
1817 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1818 target == TGSI_TEXTURE_CUBE_ARRAY ||
1819 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1820 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001821 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001822
Marek Olšák57f3da92014-06-16 16:53:42 +02001823 if (opcode == TGSI_OPCODE_TG4) {
1824 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001825
Marek Olšák57f3da92014-06-16 16:53:42 +02001826 /* DMASK was repurposed for GATHER4. 4 components are always
1827 * returned and DMASK works like a swizzle - it selects
1828 * the component to fetch. The only valid DMASK values are
1829 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1830 * (red,red,red,red) etc.) The ISA document doesn't mention
1831 * this.
1832 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001833
Marek Olšák57f3da92014-06-16 16:53:42 +02001834 /* Get the component index from src1.x for Gather4. */
1835 if (!tgsi_is_shadow_sampler(target)) {
1836 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1837 LLVMValueRef comp_imm;
1838 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001839
Marek Olšák57f3da92014-06-16 16:53:42 +02001840 assert(src1.File == TGSI_FILE_IMMEDIATE);
1841
1842 comp_imm = imms[src1.Index][src1.SwizzleX];
1843 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1844 gather_comp = CLAMP(gather_comp, 0, 3);
1845 }
1846
1847 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001848 }
1849
Marek Olšák4855acd2013-08-06 15:08:54 +02001850 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001851 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001852 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1853 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1854 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1855 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1856 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1857 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1858 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1859
1860 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001861
1862 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001863 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001864 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001865 } else {
1866 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1867 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001868 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001869
1870 emit_data->dst_type = LLVMVectorType(
1871 LLVMFloatTypeInContext(gallivm->context),
1872 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001873 }
1874
Marek Olšák2484daa2014-04-22 21:23:29 +02001875 /* The fetch opcode has been converted to a 2D array fetch.
1876 * This simplifies the LLVM backend. */
1877 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1878 target = TGSI_TEXTURE_2D_ARRAY;
1879 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1880 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1881
Michel Dänzer120efee2013-01-25 12:10:11 +01001882 /* Pad to power of two vector */
1883 while (count < util_next_power_of_two(count))
1884 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1885
Christian Königccf3e8f2013-03-26 15:09:27 +01001886 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001887}
1888
Michel Dänzer07eddc42013-02-06 15:43:10 +01001889static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1890 struct lp_build_tgsi_context * bld_base,
1891 struct lp_build_emit_data * emit_data)
1892{
1893 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001894 unsigned opcode = emit_data->inst->Instruction.Opcode;
1895 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001896 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001897 bool has_offset = HAVE_LLVM >= 0x0305 ?
1898 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001899
Marek Olšák877bb522014-06-25 03:12:46 +02001900 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001901 emit_data->output[emit_data->chan] = build_intrinsic(
1902 base->gallivm->builder,
1903 "llvm.SI.vs.load.input", emit_data->dst_type,
1904 emit_data->args, emit_data->arg_count,
1905 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1906 return;
1907 }
1908
Marek Olšák877bb522014-06-25 03:12:46 +02001909 if (opcode == TGSI_OPCODE_TG4 ||
1910 opcode == TGSI_OPCODE_LODQ ||
1911 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1912 bool is_shadow = tgsi_is_shadow_sampler(target);
1913 const char *name = "llvm.SI.image.sample";
1914 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001915
Marek Olšák877bb522014-06-25 03:12:46 +02001916 switch (opcode) {
1917 case TGSI_OPCODE_TEX:
1918 case TGSI_OPCODE_TEX2:
1919 case TGSI_OPCODE_TXP:
1920 break;
1921 case TGSI_OPCODE_TXB:
1922 case TGSI_OPCODE_TXB2:
1923 infix = ".b";
1924 break;
1925 case TGSI_OPCODE_TXL:
1926 case TGSI_OPCODE_TXL2:
1927 infix = ".l";
1928 break;
1929 case TGSI_OPCODE_TXD:
1930 infix = ".d";
1931 break;
1932 case TGSI_OPCODE_TG4:
1933 name = "llvm.SI.gather4";
1934 break;
1935 case TGSI_OPCODE_LODQ:
1936 name = "llvm.SI.getlod";
1937 is_shadow = false;
1938 has_offset = false;
1939 break;
1940 default:
1941 assert(0);
1942 return;
1943 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001944
Marek Olšák877bb522014-06-25 03:12:46 +02001945 /* Add the type and suffixes .c, .o if needed. */
1946 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1947 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1948 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001949
Marek Olšák877bb522014-06-25 03:12:46 +02001950 emit_data->output[emit_data->chan] = build_intrinsic(
1951 base->gallivm->builder, intr_name, emit_data->dst_type,
1952 emit_data->args, emit_data->arg_count,
1953 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1954 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001955 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001956 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001957
Marek Olšák877bb522014-06-25 03:12:46 +02001958 switch (opcode) {
1959 case TGSI_OPCODE_TEX:
1960 case TGSI_OPCODE_TEX2:
1961 case TGSI_OPCODE_TXP:
1962 name = "llvm.SI.sample";
1963 break;
1964 case TGSI_OPCODE_TXB:
1965 case TGSI_OPCODE_TXB2:
1966 name = "llvm.SI.sampleb";
1967 break;
1968 case TGSI_OPCODE_TXD:
1969 name = "llvm.SI.sampled";
1970 break;
1971 case TGSI_OPCODE_TXF:
1972 name = "llvm.SI.imageload";
1973 break;
1974 case TGSI_OPCODE_TXL:
1975 case TGSI_OPCODE_TXL2:
1976 name = "llvm.SI.samplel";
1977 break;
1978 default:
1979 assert(0);
1980 return;
1981 }
1982
Marek Olšákd859bdb2014-07-14 19:40:14 +02001983 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1984 v16i8 = LLVMVectorType(i8, 16);
1985 v32i8 = LLVMVectorType(i8, 32);
1986
1987 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1988 emit_data->args[1], v32i8, "");
1989 if (opcode != TGSI_OPCODE_TXF) {
1990 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1991 emit_data->args[2], v16i8, "");
1992 }
1993
Marek Olšák877bb522014-06-25 03:12:46 +02001994 sprintf(intr_name, "%s.v%ui32", name,
1995 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1996
1997 emit_data->output[emit_data->chan] = build_intrinsic(
1998 base->gallivm->builder, intr_name, emit_data->dst_type,
1999 emit_data->args, emit_data->arg_count,
2000 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2001 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002002}
2003
Michel Dänzer0495adb2013-05-06 12:45:14 +02002004static void txq_fetch_args(
2005 struct lp_build_tgsi_context * bld_base,
2006 struct lp_build_emit_data * emit_data)
2007{
2008 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2009 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002010 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002011 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002012
Marek Olšák2484daa2014-04-22 21:23:29 +02002013 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002014 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2015 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2016
2017 /* Read the size from the buffer descriptor directly. */
2018 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2019 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2020 size = LLVMBuildExtractElement(gallivm->builder, size,
2021 lp_build_const_int32(gallivm, 2), "");
2022 emit_data->args[0] = size;
2023 return;
2024 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002025
2026 /* Mip level */
2027 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2028
2029 /* Resource */
2030 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2031
Marek Olšák2484daa2014-04-22 21:23:29 +02002032 /* Texture target */
2033 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2034 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2035 target = TGSI_TEXTURE_2D_ARRAY;
2036
Michel Dänzer0495adb2013-05-06 12:45:14 +02002037 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002038 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002039
2040 emit_data->arg_count = 3;
2041
2042 emit_data->dst_type = LLVMVectorType(
2043 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2044 4);
2045}
2046
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002047static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2048 struct lp_build_tgsi_context * bld_base,
2049 struct lp_build_emit_data * emit_data)
2050{
Marek Olšák2484daa2014-04-22 21:23:29 +02002051 unsigned target = emit_data->inst->Texture.Texture;
2052
2053 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002054 /* Just return the buffer size. */
2055 emit_data->output[emit_data->chan] = emit_data->args[0];
2056 return;
2057 }
2058
2059 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002060
2061 /* Divide the number of layers by 6 to get the number of cubes. */
2062 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2063 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2064 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2065 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2066 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2067
2068 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2069 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2070 z = LLVMBuildSDiv(builder, z, six, "");
2071
2072 emit_data->output[emit_data->chan] =
2073 LLVMBuildInsertElement(builder, v4, z, two, "");
2074 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002075}
2076
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002077static void si_llvm_emit_ddxy(
2078 const struct lp_build_tgsi_action * action,
2079 struct lp_build_tgsi_context * bld_base,
2080 struct lp_build_emit_data * emit_data)
2081{
2082 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2083 struct gallivm_state *gallivm = bld_base->base.gallivm;
2084 struct lp_build_context * base = &bld_base->base;
2085 const struct tgsi_full_instruction *inst = emit_data->inst;
2086 unsigned opcode = inst->Instruction.Opcode;
2087 LLVMValueRef indices[2];
2088 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2089 LLVMValueRef tl, trbl, result[4];
2090 LLVMTypeRef i32;
2091 unsigned swizzle[4];
2092 unsigned c;
2093
2094 i32 = LLVMInt32TypeInContext(gallivm->context);
2095
2096 indices[0] = bld_base->uint_bld.zero;
2097 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2098 NULL, 0, LLVMReadNoneAttribute);
2099 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2100 indices, 2, "");
2101
2102 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2103 lp_build_const_int32(gallivm, 0xfffffffc), "");
2104 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2105 indices, 2, "");
2106
2107 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2108 lp_build_const_int32(gallivm,
2109 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2110 "");
2111 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2112 indices, 2, "");
2113
2114 for (c = 0; c < 4; ++c) {
2115 unsigned i;
2116
2117 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2118 for (i = 0; i < c; ++i) {
2119 if (swizzle[i] == swizzle[c]) {
2120 result[c] = result[i];
2121 break;
2122 }
2123 }
2124 if (i != c)
2125 continue;
2126
2127 LLVMBuildStore(gallivm->builder,
2128 LLVMBuildBitCast(gallivm->builder,
2129 lp_build_emit_fetch(bld_base, inst, 0, c),
2130 i32, ""),
2131 store_ptr);
2132
2133 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2134 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2135
2136 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2137 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2138
2139 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2140 }
2141
2142 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2143}
2144
Michel Dänzer404b29d2013-11-21 16:45:28 +09002145/* Emit one vertex from the geometry shader */
2146static void si_llvm_emit_vertex(
2147 const struct lp_build_tgsi_action *action,
2148 struct lp_build_tgsi_context *bld_base,
2149 struct lp_build_emit_data *emit_data)
2150{
2151 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002152 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002153 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002154 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002155 struct gallivm_state *gallivm = bld_base->base.gallivm;
2156 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002157 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2158 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002159 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002160 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002161 LLVMValueRef args[2];
2162 unsigned chan;
2163 int i;
2164
Michel Dänzer404b29d2013-11-21 16:45:28 +09002165 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002166 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002167
2168 /* If this thread has already emitted the declared maximum number of
2169 * vertices, kill it: excessive vertex emissions are not supposed to
2170 * have any effect, and GS threads have no externally observable
2171 * effects other than emitting vertices.
2172 */
2173 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2174 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002175 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002176 kill = lp_build_select(&bld_base->base, can_emit,
2177 lp_build_const_float(gallivm, 1.0f),
2178 lp_build_const_float(gallivm, -1.0f));
2179 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2180 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2181
Marek Olšák02134cf2014-10-04 22:07:50 +02002182 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002183 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002184 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002185
2186 for (chan = 0; chan < 4; chan++) {
2187 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002188 LLVMValueRef voffset =
2189 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002190 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002191
2192 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2193 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002194
2195 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2196
Marek Olšákfc3b3352014-10-05 13:33:40 +02002197 build_tbuffer_store(si_shader_ctx,
2198 si_shader_ctx->gsvs_ring,
2199 out_val, 1,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002200 voffset, soffset, 0,
2201 V_008F0C_BUF_DATA_FORMAT_32,
2202 V_008F0C_BUF_NUM_FORMAT_UINT,
2203 1, 0, 1, 1, 0);
2204 }
2205 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002206 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2207 lp_build_const_int32(gallivm, 1));
2208 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002209
2210 /* Signal vertex emission */
2211 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2212 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2213 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2214 LLVMVoidTypeInContext(gallivm->context), args, 2,
2215 LLVMNoUnwindAttribute);
2216}
2217
2218/* Cut one primitive from the geometry shader */
2219static void si_llvm_emit_primitive(
2220 const struct lp_build_tgsi_action *action,
2221 struct lp_build_tgsi_context *bld_base,
2222 struct lp_build_emit_data *emit_data)
2223{
2224 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2225 struct gallivm_state *gallivm = bld_base->base.gallivm;
2226 LLVMValueRef args[2];
2227
2228 /* Signal primitive cut */
2229 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2230 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2231 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2232 LLVMVoidTypeInContext(gallivm->context), args, 2,
2233 LLVMNoUnwindAttribute);
2234}
2235
Tom Stellarda75c6162012-01-06 17:38:37 -05002236static const struct lp_build_tgsi_action tex_action = {
2237 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002238 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002239};
2240
Michel Dänzer0495adb2013-05-06 12:45:14 +02002241static const struct lp_build_tgsi_action txq_action = {
2242 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002243 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002244 .intr_name = "llvm.SI.resinfo"
2245};
2246
Christian König206f0592013-03-20 14:37:21 +01002247static void create_meta_data(struct si_shader_context *si_shader_ctx)
2248{
2249 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2250 LLVMValueRef args[3];
2251
2252 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2253 args[1] = 0;
2254 args[2] = lp_build_const_int32(gallivm, 1);
2255
2256 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2257}
2258
Marek Olšák4f3f0432014-07-07 22:49:15 +02002259static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2260{
2261 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2262 CONST_ADDR_SPACE);
2263}
2264
Christian König55fe5cc2013-03-04 16:30:06 +01002265static void create_function(struct si_shader_context *si_shader_ctx)
2266{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002267 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2268 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002269 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002270 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002271 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002272
Christian König55fe5cc2013-03-04 16:30:06 +01002273 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002274 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002275 f32 = LLVMFloatTypeInContext(gallivm->context);
2276 v2i32 = LLVMVectorType(i32, 2);
2277 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002278 v4i32 = LLVMVectorType(i32, 4);
2279 v8i32 = LLVMVectorType(i32, 8);
2280 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002281
Marek Olšákee2a8182014-07-07 23:27:19 +02002282 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002283 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002284 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2285 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002286 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002287
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002288 switch (si_shader_ctx->type) {
2289 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002290 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002291 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002292 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002293 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002294 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002295
Michel Dänzer404b29d2013-11-21 16:45:28 +09002296 if (shader->key.vs.as_es) {
2297 params[SI_PARAM_ES2GS_OFFSET] = i32;
2298 num_params++;
2299 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002300 if (shader->is_gs_copy_shader) {
2301 last_array_pointer = SI_PARAM_CONST;
2302 num_params = SI_PARAM_CONST+1;
2303 }
2304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002305 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002306
Michel Dänzer404b29d2013-11-21 16:45:28 +09002307 /* Streamout SGPRs. */
2308 if (shader->selector->so.num_outputs) {
2309 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2310 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2311 }
2312 /* A streamout buffer offset is loaded if the stride is non-zero. */
2313 for (i = 0; i < 4; i++) {
2314 if (!shader->selector->so.stride[i])
2315 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002316
Michel Dänzer404b29d2013-11-21 16:45:28 +09002317 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2318 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002319 }
2320
2321 last_sgpr = num_params-1;
2322
2323 /* VGPRs */
2324 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2325 params[num_params++] = i32; /* unused*/
2326 params[num_params++] = i32; /* unused */
2327 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002328 break;
Christian König0666ffd2013-03-05 15:07:39 +01002329
Michel Dänzer404b29d2013-11-21 16:45:28 +09002330 case TGSI_PROCESSOR_GEOMETRY:
2331 params[SI_PARAM_GS2VS_OFFSET] = i32;
2332 params[SI_PARAM_GS_WAVE_ID] = i32;
2333 last_sgpr = SI_PARAM_GS_WAVE_ID;
2334
2335 /* VGPRs */
2336 params[SI_PARAM_VTX0_OFFSET] = i32;
2337 params[SI_PARAM_VTX1_OFFSET] = i32;
2338 params[SI_PARAM_PRIMITIVE_ID] = i32;
2339 params[SI_PARAM_VTX2_OFFSET] = i32;
2340 params[SI_PARAM_VTX3_OFFSET] = i32;
2341 params[SI_PARAM_VTX4_OFFSET] = i32;
2342 params[SI_PARAM_VTX5_OFFSET] = i32;
2343 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2344 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2345 break;
2346
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002347 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002348 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002349 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002350 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002351 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2352 params[SI_PARAM_PERSP_CENTER] = v2i32;
2353 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2354 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2355 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2356 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2357 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2358 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2359 params[SI_PARAM_POS_X_FLOAT] = f32;
2360 params[SI_PARAM_POS_Y_FLOAT] = f32;
2361 params[SI_PARAM_POS_Z_FLOAT] = f32;
2362 params[SI_PARAM_POS_W_FLOAT] = f32;
2363 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002364 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002365 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2366 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002367 num_params = SI_PARAM_POS_FIXED_PT+1;
2368 break;
2369
2370 default:
2371 assert(0 && "unimplemented shader");
2372 return;
Christian Königc4973212013-03-05 12:14:02 +01002373 }
Christian König55fe5cc2013-03-04 16:30:06 +01002374
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002375 assert(num_params <= Elements(params));
2376 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002377 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002378
Marek Olšák1829f9c2015-01-04 17:08:57 +01002379 if (shader->dx10_clamp_mode)
2380 LLVMAddTargetDependentFunctionAttr(si_shader_ctx->radeon_bld.main_fn,
2381 "enable-no-nans-fp-math", "true");
2382
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002383 for (i = 0; i <= last_sgpr; ++i) {
2384 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002385
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002386 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2387 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002388 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002389 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002390 else
2391 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002392 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002393
Michel Dänzer404b29d2013-11-21 16:45:28 +09002394 if (bld_base->info &&
2395 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2396 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002397 si_shader_ctx->ddxy_lds =
2398 LLVMAddGlobalInAddressSpace(gallivm->module,
2399 LLVMArrayType(i32, 64),
2400 "ddxy_lds",
2401 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002402}
Tom Stellarda75c6162012-01-06 17:38:37 -05002403
Christian König0f6cf2b2013-03-15 15:53:25 +01002404static void preload_constants(struct si_shader_context *si_shader_ctx)
2405{
2406 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2407 struct gallivm_state * gallivm = bld_base->base.gallivm;
2408 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002409 unsigned buf;
2410 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002411
Marek Olšákee2a8182014-07-07 23:27:19 +02002412 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002413 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002414
Marek Olšák2fd42002013-10-25 11:45:47 +02002415 if (num_const == 0)
2416 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002417
Marek Olšák2fd42002013-10-25 11:45:47 +02002418 /* Allocate space for the constant values */
2419 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002420
Marek Olšák2fd42002013-10-25 11:45:47 +02002421 /* Load the resource descriptor */
2422 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002423 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002424
Marek Olšák2fd42002013-10-25 11:45:47 +02002425 /* Load the constants, we rely on the code sinking to do the rest */
2426 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002427 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002428 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002429 si_shader_ctx->const_resource[buf],
2430 lp_build_const_int32(gallivm, i * 4),
2431 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002432 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002433 }
2434}
2435
Christian König1c100182013-03-17 16:02:42 +01002436static void preload_samplers(struct si_shader_context *si_shader_ctx)
2437{
2438 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2439 struct gallivm_state * gallivm = bld_base->base.gallivm;
2440 const struct tgsi_shader_info * info = bld_base->info;
2441
2442 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2443
2444 LLVMValueRef res_ptr, samp_ptr;
2445 LLVMValueRef offset;
2446
2447 if (num_samplers == 0)
2448 return;
2449
Christian König1c100182013-03-17 16:02:42 +01002450 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2451 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2452
2453 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2454 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002455 /* Resource */
2456 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002457 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002458
2459 /* Sampler */
2460 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002461 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002462
2463 /* FMASK resource */
2464 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002465 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2466 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002467 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002468 }
Christian König1c100182013-03-17 16:02:42 +01002469 }
2470}
2471
Marek Olšák8d03d922013-09-01 23:59:06 +02002472static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2473{
2474 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2475 struct gallivm_state * gallivm = bld_base->base.gallivm;
2476 unsigned i;
2477
Michel Dänzer67e385b2014-01-08 17:48:21 +09002478 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2479 si_shader_ctx->shader->key.vs.as_es ||
2480 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002481 return;
2482
2483 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002484 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002485
2486 /* Load the resources, we rely on the code sinking to do the rest */
2487 for (i = 0; i < 4; ++i) {
2488 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002489 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002490 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002491
Marek Olšákd7876082014-09-26 23:06:32 +02002492 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002493 }
2494 }
2495}
2496
Marek Olšákfc3b3352014-10-05 13:33:40 +02002497/**
2498 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
2499 * for later use.
2500 */
2501static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
2502{
2503 struct gallivm_state *gallivm =
2504 si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2505
2506 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2507 SI_PARAM_RW_BUFFERS);
2508
2509 if ((si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
2510 si_shader_ctx->shader->key.vs.as_es) ||
2511 si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY) {
2512 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_ESGS);
2513
2514 si_shader_ctx->esgs_ring =
2515 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2516 }
2517
2518 if (si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY ||
2519 si_shader_ctx->shader->is_gs_copy_shader) {
2520 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_GSVS);
2521
2522 si_shader_ctx->gsvs_ring =
2523 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2524 }
2525}
2526
Tom Stellard2397a722014-12-10 09:13:59 -05002527void si_shader_binary_read_config(const struct si_screen *sscreen,
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002528 struct si_shader *shader,
2529 unsigned symbol_offset)
Tom Stellard302f53d2012-10-25 13:50:10 -04002530{
Tom Stellard302f53d2012-10-25 13:50:10 -04002531 unsigned i;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002532 const unsigned char *config =
Tom Stellard2397a722014-12-10 09:13:59 -05002533 radeon_shader_binary_config_start(&shader->binary,
2534 symbol_offset);
Tom Stellard302f53d2012-10-25 13:50:10 -04002535
Tom Stellardd50343d2013-04-04 16:21:06 -04002536 /* XXX: We may be able to emit some of these values directly rather than
2537 * extracting fields to be emitted later.
2538 */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002539
Tom Stellard2397a722014-12-10 09:13:59 -05002540 for (i = 0; i < shader->binary.config_size_per_symbol; i+= 8) {
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002541 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
2542 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
Tom Stellardd50343d2013-04-04 16:21:06 -04002543 switch (reg) {
2544 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2545 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2546 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2547 case R_00B848_COMPUTE_PGM_RSRC1:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002548 shader->num_sgprs = MAX2(shader->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
2549 shader->num_vgprs = MAX2(shader->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
Tom Stellardd50343d2013-04-04 16:21:06 -04002550 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002551 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002552 shader->lds_size = MAX2(shader->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002553 break;
2554 case R_00B84C_COMPUTE_PGM_RSRC2:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002555 shader->lds_size = MAX2(shader->lds_size, G_00B84C_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002556 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002557 case R_0286CC_SPI_PS_INPUT_ENA:
2558 shader->spi_ps_input_ena = value;
2559 break;
Tom Stellard2397a722014-12-10 09:13:59 -05002560 case R_0286E8_SPI_TMPRING_SIZE:
Tom Stellardb0f78032014-07-18 14:45:18 -04002561 case R_00B860_COMPUTE_TMPRING_SIZE:
2562 /* WAVESIZE is in units of 256 dwords. */
2563 shader->scratch_bytes_per_wave =
2564 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2565 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002566 default:
2567 fprintf(stderr, "Warning: Compiler emitted unknown "
2568 "config register: 0x%x\n", reg);
2569 break;
2570 }
2571 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002572}
2573
Tom Stellard2397a722014-12-10 09:13:59 -05002574void si_shader_apply_scratch_relocs(struct si_context *sctx,
2575 struct si_shader *shader,
2576 uint64_t scratch_va)
2577{
2578 unsigned i;
2579 uint32_t scratch_rsrc_dword0 = scratch_va & 0xffffffff;
2580 uint32_t scratch_rsrc_dword1 =
2581 S_008F04_BASE_ADDRESS_HI(scratch_va >> 32)
2582 | S_008F04_STRIDE(shader->scratch_bytes_per_wave / 64);
2583
2584 for (i = 0 ; i < shader->binary.reloc_count; i++) {
2585 const struct radeon_shader_reloc *reloc =
2586 &shader->binary.relocs[i];
2587 if (!strcmp(scratch_rsrc_dword0_symbol, reloc->name)) {
2588 util_memcpy_cpu_to_le32(shader->binary.code + reloc->offset,
2589 &scratch_rsrc_dword0, 4);
2590 } else if (!strcmp(scratch_rsrc_dword1_symbol, reloc->name)) {
2591 util_memcpy_cpu_to_le32(shader->binary.code + reloc->offset,
2592 &scratch_rsrc_dword1, 4);
2593 }
2594 }
2595}
2596
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002597int si_shader_binary_read(struct si_screen *sscreen,
2598 struct si_shader *shader,
2599 const struct radeon_shader_binary *binary)
2600{
2601
2602 unsigned i;
2603 unsigned code_size;
2604 unsigned char *ptr;
2605 bool dump = r600_can_dump_shader(&sscreen->b,
2606 shader->selector ? shader->selector->tokens : NULL);
2607
2608 if (dump && !binary->disassembled) {
2609 fprintf(stderr, "SI CODE:\n");
2610 for (i = 0; i < binary->code_size; i+=4 ) {
2611 fprintf(stderr, "@0x%x: %02x%02x%02x%02x\n", i, binary->code[i + 3],
2612 binary->code[i + 2], binary->code[i + 1],
2613 binary->code[i]);
2614 }
2615 }
2616
Tom Stellard2397a722014-12-10 09:13:59 -05002617 si_shader_binary_read_config(sscreen, shader, 0);
Tom Stellard302f53d2012-10-25 13:50:10 -04002618
2619 /* copy new shader */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002620 code_size = binary->code_size + binary->rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002621 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002622 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002623 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002624 if (shader->bo == NULL) {
2625 return -ENOMEM;
2626 }
2627
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002628
2629 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_READ_WRITE);
2630 util_memcpy_cpu_to_le32(ptr, binary->code, binary->code_size);
2631 if (binary->rodata_size > 0) {
2632 ptr += binary->code_size;
2633 util_memcpy_cpu_to_le32(ptr, binary->rodata, binary->rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002634 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002635
Marek Olšák1abb1a92014-09-17 22:44:22 +02002636 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002637
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002638 return 0;
2639}
Tom Stellard302f53d2012-10-25 13:50:10 -04002640
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002641int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2642 LLVMModuleRef mod)
2643{
2644 int r = 0;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002645 bool dump = r600_can_dump_shader(&sscreen->b,
2646 shader->selector ? shader->selector->tokens : NULL);
Tom Stellard2397a722014-12-10 09:13:59 -05002647 r = radeon_llvm_compile(mod, &shader->binary,
Tom Stellard761e36b2014-10-15 12:24:30 -04002648 r600_get_llvm_processor_name(sscreen->b.family), dump, sscreen->tm);
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002649
2650 if (r) {
2651 return r;
2652 }
Tom Stellard2397a722014-12-10 09:13:59 -05002653 r = si_shader_binary_read(sscreen, shader, &shader->binary);
2654
2655 FREE(shader->binary.config);
2656 FREE(shader->binary.rodata);
2657 FREE(shader->binary.global_symbol_offsets);
2658 if (shader->scratch_bytes_per_wave == 0) {
2659 FREE(shader->binary.code);
2660 FREE(shader->binary.relocs);
2661 memset(&shader->binary, 0, sizeof(shader->binary));
2662 }
Darren Powellbc866902014-03-31 18:00:28 -04002663 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002664}
2665
Michel Dänzer404b29d2013-11-21 16:45:28 +09002666/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002667static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002668 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002669 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002670{
2671 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2672 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2673 struct lp_build_context *base = &bld_base->base;
2674 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002675 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002676 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002677 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002678 LLVMValueRef args[9];
2679 int i, r;
2680
Marek Olšák02134cf2014-10-04 22:07:50 +02002681 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002682
2683 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002684 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002685
2686 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2687
2688 create_meta_data(si_shader_ctx);
2689 create_function(si_shader_ctx);
2690 preload_streamout_buffers(si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002691 preload_ring_buffers(si_shader_ctx);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002692
Marek Olšákfc3b3352014-10-05 13:33:40 +02002693 args[0] = si_shader_ctx->gsvs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002694 args[1] = lp_build_mul_imm(uint,
2695 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2696 si_shader_ctx->param_vertex_id),
2697 4);
2698 args[3] = uint->zero;
2699 args[4] = uint->one; /* OFFEN */
2700 args[5] = uint->zero; /* IDXEN */
2701 args[6] = uint->one; /* GLC */
2702 args[7] = uint->one; /* SLC */
2703 args[8] = uint->zero; /* TFE */
2704
2705 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002706 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002707 unsigned chan;
2708
Marek Olšák02134cf2014-10-04 22:07:50 +02002709 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002710 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002711
2712 for (chan = 0; chan < 4; chan++) {
2713 args[2] = lp_build_const_int32(gallivm,
2714 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002715 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002716
2717 outputs[i].values[chan] =
2718 LLVMBuildBitCast(gallivm->builder,
2719 build_intrinsic(gallivm->builder,
2720 "llvm.SI.buffer.load.dword.i32.i32",
2721 LLVMInt32TypeInContext(gallivm->context),
2722 args, 9,
2723 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2724 base->elem_type, "");
2725 }
2726 }
2727
Marek Olšák02134cf2014-10-04 22:07:50 +02002728 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002729
2730 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2731
2732 if (dump)
2733 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2734
Marek Olšák1abb1a92014-09-17 22:44:22 +02002735 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002736 bld_base->base.gallivm->module);
2737
2738 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2739
2740 FREE(outputs);
2741 return r;
2742}
2743
Marek Olšák1abb1a92014-09-17 22:44:22 +02002744int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002745{
Marek Olšák2774abd2014-09-16 18:45:33 +02002746 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002747 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002748 struct lp_build_tgsi_context * bld_base;
2749 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002750 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002751 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002752
2753 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2754 * conversion fails. */
2755 if (dump) {
2756 tgsi_dump(sel->tokens, 0);
2757 si_dump_streamout(&sel->so);
2758 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002759
Marek Olšák8c37c162014-09-16 18:40:07 +02002760 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002761
Michel Dänzercfebaf92012-08-31 19:04:08 +02002762 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002763 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2764 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2765
Marek Olšák1829f9c2015-01-04 17:08:57 +01002766 if (sel->type != PIPE_SHADER_COMPUTE)
2767 shader->dx10_clamp_mode = true;
2768
Marek Olšák52335682014-09-30 15:12:09 +02002769 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002770 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2771
Marek Olšák52335682014-09-30 15:12:09 +02002772 shader->uses_instanceid = sel->info.uses_instanceid;
2773 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002774 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002775
2776 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002777 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002778 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2779 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002780 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002781 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2782 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2783 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002784 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002785 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002786 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2787 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002788
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002789 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2790 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002791
Michel Dänzer404b29d2013-11-21 16:45:28 +09002792 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2793 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2794
Marek Olšák645b4712014-11-20 22:16:09 +01002795 if (HAVE_LLVM >= 0x0306) {
2796 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
2797 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
2798 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
2799 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
2800 }
2801
Christian Könige4ed5872013-03-21 18:02:52 +01002802 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002803 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002804 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2805 si_shader_ctx.shader = shader;
2806 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Marek Olšák368b0a72014-12-28 21:51:35 +01002807 si_shader_ctx.screen = sscreen;
Tom Stellarda75c6162012-01-06 17:38:37 -05002808
Michel Dänzer51f89a02013-12-09 15:33:53 +09002809 switch (si_shader_ctx.type) {
2810 case TGSI_PROCESSOR_VERTEX:
2811 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002812 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002813 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2814 } else {
2815 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2816 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002817 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002818 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002819 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2820 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002821 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002822 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002823 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2824 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002825
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002826 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002827 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2828 shader->db_shader_control |=
2829 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2830 break;
2831 case TGSI_FS_DEPTH_LAYOUT_LESS:
2832 shader->db_shader_control |=
2833 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2834 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002835 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002836 break;
2837 default:
2838 assert(!"Unsupported shader type");
2839 return -1;
2840 }
2841
Christian König206f0592013-03-20 14:37:21 +01002842 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002843 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002844 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002845 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002846 preload_streamout_buffers(&si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002847 preload_ring_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002848
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002849 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2850 si_shader_ctx.gs_next_vertex =
2851 lp_build_alloca(bld_base->base.gallivm,
2852 bld_base->uint_bld.elem_type, "");
2853 }
2854
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002855 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002856 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002857 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002858 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002859
2860 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2861
2862 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002863 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002864 if (r) {
2865 fprintf(stderr, "LLVM failed to compile shader\n");
2866 goto out;
2867 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002868
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002869 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002870
2871 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002872 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002873 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002874 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002875 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002876 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2877 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002878 free(shader->gs_copy_shader);
2879 shader->gs_copy_shader = NULL;
2880 goto out;
2881 }
2882 }
2883
Tom Stellarda75c6162012-01-06 17:38:37 -05002884 tgsi_parse_free(&si_shader_ctx.parse);
2885
Michel Dänzer404b29d2013-11-21 16:45:28 +09002886out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002887 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002888 FREE(si_shader_ctx.constants[i]);
Tom Stellarda75c6162012-01-06 17:38:37 -05002889
Tom Stellard302f53d2012-10-25 13:50:10 -04002890 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002891}
2892
Marek Olšák2774abd2014-09-16 18:45:33 +02002893void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002894{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002895 if (shader->gs_copy_shader)
2896 si_shader_destroy(ctx, shader->gs_copy_shader);
2897
Tom Stellard5dcd97f2015-01-27 16:35:21 +00002898 if (shader->scratch_bo)
2899 r600_resource_reference(&shader->scratch_bo, NULL);
2900
Marek Olšáka81c3e02013-08-14 01:04:39 +02002901 r600_resource_reference(&shader->bo, NULL);
Tom Stellard2397a722014-12-10 09:13:59 -05002902
2903 FREE(shader->binary.code);
2904 FREE(shader->binary.relocs);
Tom Stellarda75c6162012-01-06 17:38:37 -05002905}