blob: 2c1dac928a2be88c006ac03c1123649c71165846 [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;
Marek Olšák8c37c162014-09-16 18:40:07 +020066 struct si_shader *shader;
Marek Olšák368b0a72014-12-28 21:51:35 +010067 struct si_screen *screen;
Tom Stellarda75c6162012-01-06 17:38:37 -050068 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020069 int param_streamout_config;
70 int param_streamout_write_index;
71 int param_streamout_offset[4];
72 int param_vertex_id;
73 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010074 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020075 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020076 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020077 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Marek Olšák55a9b772014-10-05 12:38:54 +020078 LLVMValueRef resources[SI_NUM_SAMPLER_VIEWS];
79 LLVMValueRef samplers[SI_NUM_SAMPLER_STATES];
Marek Olšák8d03d922013-09-01 23:59:06 +020080 LLVMValueRef so_buffers[4];
Marek Olšákfc3b3352014-10-05 13:33:40 +020081 LLVMValueRef esgs_ring;
82 LLVMValueRef gsvs_ring;
Michel Dänzerf07a96d2014-01-08 18:45:10 +090083 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050084};
85
86static struct si_shader_context * si_shader_context(
87 struct lp_build_tgsi_context * bld_base)
88{
89 return (struct si_shader_context *)bld_base;
90}
91
92
93#define PERSPECTIVE_BASE 0
94#define LINEAR_BASE 9
95
96#define SAMPLE_OFFSET 0
97#define CENTER_OFFSET 2
98#define CENTROID_OFSET 4
99
100#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -0400101#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +0200102#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -0400103#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -0500104
Michel Dänzer404b29d2013-11-21 16:45:28 +0900105
106#define SENDMSG_GS 2
107#define SENDMSG_GS_DONE 3
108
109#define SENDMSG_GS_OP_NOP (0 << 4)
110#define SENDMSG_GS_OP_CUT (1 << 4)
111#define SENDMSG_GS_OP_EMIT (2 << 4)
112#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
113
Marek Olšáke29353f2014-09-17 22:17:02 +0200114/**
115 * Returns a unique index for a semantic name and index. The index must be
116 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
117 * calculated.
118 */
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200119unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
Marek Olšáke29353f2014-09-17 22:17:02 +0200120{
121 switch (semantic_name) {
122 case TGSI_SEMANTIC_POSITION:
123 return 0;
124 case TGSI_SEMANTIC_PSIZE:
125 return 1;
126 case TGSI_SEMANTIC_CLIPDIST:
127 assert(index <= 1);
128 return 2 + index;
129 case TGSI_SEMANTIC_CLIPVERTEX:
130 return 4;
131 case TGSI_SEMANTIC_COLOR:
132 assert(index <= 1);
133 return 5 + index;
134 case TGSI_SEMANTIC_BCOLOR:
135 assert(index <= 1);
136 return 7 + index;
137 case TGSI_SEMANTIC_FOG:
138 return 9;
139 case TGSI_SEMANTIC_EDGEFLAG:
140 return 10;
141 case TGSI_SEMANTIC_GENERIC:
142 assert(index <= 63-11);
143 return 11 + index;
144 default:
145 assert(0);
146 return 63;
147 }
148}
149
150/**
151 * Given a semantic name and index of a parameter and a mask of used parameters
152 * (inputs or outputs), return the index of the parameter in the list of all
153 * used parameters.
154 *
155 * For example, assume this list of parameters:
156 * POSITION, PSIZE, GENERIC0, GENERIC2
157 * which has the mask:
158 * 11000000000101
159 * Then:
160 * querying POSITION returns 0,
161 * querying PSIZE returns 1,
162 * querying GENERIC0 returns 2,
163 * querying GENERIC2 returns 3.
164 *
165 * Which can be used as an offset to a parameter buffer in units of vec4s.
166 */
167static int get_param_index(unsigned semantic_name, unsigned index,
168 uint64_t mask)
169{
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200170 unsigned unique_index = si_shader_io_get_unique_index(semantic_name, index);
Marek Olšáke29353f2014-09-17 22:17:02 +0200171 int i, param_index = 0;
172
173 /* If not present... */
174 if (!((1llu << unique_index) & mask))
175 return -1;
176
177 for (i = 0; mask; i++) {
178 uint64_t bit = 1llu << i;
179
180 if (bit & mask) {
181 if (i == unique_index)
182 return param_index;
183
184 mask &= ~bit;
185 param_index++;
186 }
187 }
188
189 assert(!"unreachable");
190 return -1;
191}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900192
Tom Stellard467f5162012-05-16 15:15:35 -0400193/**
Marek Olšákd7876082014-09-26 23:06:32 +0200194 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
195 * It's equivalent to doing a load from &base_ptr[index].
Tom Stellard467f5162012-05-16 15:15:35 -0400196 *
Marek Olšákd7876082014-09-26 23:06:32 +0200197 * \param base_ptr Where the array starts.
198 * \param index The element index into the array.
Tom Stellard467f5162012-05-16 15:15:35 -0400199 */
Marek Olšákd7876082014-09-26 23:06:32 +0200200static LLVMValueRef build_indexed_load(struct si_shader_context *si_shader_ctx,
201 LLVMValueRef base_ptr, LLVMValueRef index)
Tom Stellard467f5162012-05-16 15:15:35 -0400202{
Marek Olšákd7876082014-09-26 23:06:32 +0200203 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
204 struct gallivm_state *gallivm = bld_base->base.gallivm;
205 LLVMValueRef indices[2], pointer;
Tom Stellard467f5162012-05-16 15:15:35 -0400206
Marek Olšákd7876082014-09-26 23:06:32 +0200207 indices[0] = bld_base->uint_bld.zero;
208 indices[1] = index;
Christian König206f0592013-03-20 14:37:21 +0100209
Marek Olšákd7876082014-09-26 23:06:32 +0200210 pointer = LLVMBuildGEP(gallivm->builder, base_ptr, indices, 2, "");
211 return LLVMBuildLoad(gallivm->builder, pointer, "");
212}
213
214/**
215 * Do a load from &base_ptr[index], but also add a flag that it's loading
216 * a constant.
217 */
218static LLVMValueRef build_indexed_load_const(
219 struct si_shader_context * si_shader_ctx,
220 LLVMValueRef base_ptr, LLVMValueRef index)
221{
222 LLVMValueRef result = build_indexed_load(si_shader_ctx, base_ptr, index);
Christian König206f0592013-03-20 14:37:21 +0100223 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
224 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400225}
226
Marek Olšákf317ce52013-09-05 15:39:57 +0200227static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100228 struct radeon_llvm_context * radeon_bld,
229 unsigned divisor)
230{
Marek Olšák8d03d922013-09-01 23:59:06 +0200231 struct si_shader_context *si_shader_ctx =
232 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100233 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
234
Marek Olšák8d03d922013-09-01 23:59:06 +0200235 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
236 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100237
Marek Olšák50908a82015-01-31 19:00:44 +0100238 /* The division must be done before START_INSTANCE is added. */
Christian Königa0dca442013-03-22 15:59:22 +0100239 if (divisor > 1)
240 result = LLVMBuildUDiv(gallivm->builder, result,
241 lp_build_const_int32(gallivm, divisor), "");
242
Marek Olšák50908a82015-01-31 19:00:44 +0100243 return LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
244 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
Christian Königa0dca442013-03-22 15:59:22 +0100245}
246
Tom Stellarda75c6162012-01-06 17:38:37 -0500247static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900248 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500249 unsigned input_index,
250 const struct tgsi_full_declaration *decl)
251{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900252 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
253 struct gallivm_state *gallivm = base->gallivm;
254 struct si_shader_context *si_shader_ctx =
255 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100256 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
257
258 unsigned chan;
259
Tom Stellarda75c6162012-01-06 17:38:37 -0500260 LLVMValueRef t_list_ptr;
261 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400262 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500263 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100264 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400265 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500266 LLVMTypeRef vec4_type;
267 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500268
Tom Stellard467f5162012-05-16 15:15:35 -0400269 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100270 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500271
Michel Dänzer51f89a02013-12-09 15:33:53 +0900272 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400273
Marek Olšákd7876082014-09-26 23:06:32 +0200274 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400275
276 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900277 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500278
Christian Königa0dca442013-03-22 15:59:22 +0100279 if (divisor) {
280 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200281 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200282 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100283 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200284 /* Load the buffer index for vertices. */
285 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
286 si_shader_ctx->param_vertex_id);
287 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
288 SI_PARAM_BASE_VERTEX);
289 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100290 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500291
292 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400293 args[0] = t_list;
294 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100295 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900296 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100297 "llvm.SI.vs.load.input", vec4_type, args, 3,
298 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500299
300 /* Break up the vec4 into individual components */
301 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900302 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500303 /* XXX: Use a helper function for this. There is one in
304 * tgsi_llvm.c. */
305 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900306 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500307 input, llvm_chan, "");
308 }
309}
310
Michel Dänzer404b29d2013-11-21 16:45:28 +0900311static LLVMValueRef fetch_input_gs(
312 struct lp_build_tgsi_context *bld_base,
313 const struct tgsi_full_src_register *reg,
314 enum tgsi_opcode_type type,
315 unsigned swizzle)
316{
317 struct lp_build_context *base = &bld_base->base;
318 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200319 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900320 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
321 struct gallivm_state *gallivm = base->gallivm;
322 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
323 LLVMValueRef vtx_offset;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900324 LLVMValueRef args[9];
325 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200326 struct tgsi_shader_info *info = &shader->selector->info;
327 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
328 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900329
Marek Olšáke23fec12014-10-04 17:40:39 +0200330 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900331 if (swizzle == 0)
332 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
333 SI_PARAM_PRIMITIVE_ID);
334 else
335 return uint->zero;
336 }
337
Michel Dänzer404b29d2013-11-21 16:45:28 +0900338 if (!reg->Register.Dimension)
339 return NULL;
340
341 if (swizzle == ~0) {
342 LLVMValueRef values[TGSI_NUM_CHANNELS];
343 unsigned chan;
344 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
345 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
346 }
347 return lp_build_gather_values(bld_base->base.gallivm, values,
348 TGSI_NUM_CHANNELS);
349 }
350
351 /* Get the vertex offset parameter */
352 vtx_offset_param = reg->Dimension.Index;
353 if (vtx_offset_param < 2) {
354 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
355 } else {
356 assert(vtx_offset_param < 6);
357 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
358 }
359 vtx_offset = lp_build_mul_imm(uint,
360 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
361 vtx_offset_param),
362 4);
363
Marek Olšákfc3b3352014-10-05 13:33:40 +0200364 args[0] = si_shader_ctx->esgs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900365 args[1] = vtx_offset;
366 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200367 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200368 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900369 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900370 args[3] = uint->zero;
371 args[4] = uint->one; /* OFFEN */
372 args[5] = uint->zero; /* IDXEN */
373 args[6] = uint->one; /* GLC */
374 args[7] = uint->zero; /* SLC */
375 args[8] = uint->zero; /* TFE */
376
377 return LLVMBuildBitCast(gallivm->builder,
378 build_intrinsic(gallivm->builder,
379 "llvm.SI.buffer.load.dword.i32.i32",
380 i32, args, 9,
381 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
382 tgsi2llvmtype(bld_base, type), "");
383}
384
Tom Stellarda75c6162012-01-06 17:38:37 -0500385static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900386 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500387 unsigned input_index,
388 const struct tgsi_full_declaration *decl)
389{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900390 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
391 struct si_shader_context *si_shader_ctx =
392 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200393 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900394 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
395 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400396 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900397 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100398
399 LLVMValueRef interp_param;
400 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500401
402 /* This value is:
403 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
404 * quad begins a new primitive. Bit 0 always needs
405 * to be unset)
406 * [32:16] ParamOffset
407 *
408 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900409 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200410 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500411
Christian König0666ffd2013-03-05 15:07:39 +0100412 unsigned chan;
413
Tom Stellard0fb1e682012-09-06 16:18:11 -0400414 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
415 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400416 unsigned soa_index =
417 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900418 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100419 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100420
421 if (chan == 3)
422 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900423 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100424 LLVMBuildFDiv(gallivm->builder,
425 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900426 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100427 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400428 }
429 return;
430 }
431
Michel Dänzer97078b12012-09-25 12:41:31 +0200432 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900433 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Marek Olšáke827bb62014-10-16 16:20:26 +0200434 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900435 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
436 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200437 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900438 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200439 lp_build_const_float(gallivm, 1.0f);
440
441 return;
442 }
443
Marek Olšák8b057dd2014-10-04 22:15:07 +0200444 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200445 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200446 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200447
Francisco Jerez12799232012-04-30 18:27:52 +0200448 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500449 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100450 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500451 break;
452 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200453 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200454 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400455 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100456 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200457 else
Christian König0666ffd2013-03-05 15:07:39 +0100458 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200459 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200460 case TGSI_INTERPOLATE_COLOR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200461 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200462 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200463 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400464 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100465 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200466 else
Christian König0666ffd2013-03-05 15:07:39 +0100467 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500468 break;
469 default:
470 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
471 return;
472 }
473
Marek Olšák13de9472015-01-04 20:09:51 +0100474 /* fs.constant returns the param from the middle vertex, so it's not
475 * really useful for flat shading. It's meant to be used for custom
476 * interpolation (but the intrinsic can't fetch from the other two
477 * vertices).
478 *
479 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
480 * to do the right thing. The only reason we use fs.constant is that
481 * fs.interp cannot be used on integers, because they can be equal
482 * to NaN.
483 */
Christian König0666ffd2013-03-05 15:07:39 +0100484 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
485
Michel Dänzer691f08d2012-09-06 18:03:38 +0200486 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100487 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100488 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200489 LLVMValueRef face, is_face_positive;
490 LLVMValueRef back_attr_number =
491 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200492 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200493
Christian König0666ffd2013-03-05 15:07:39 +0100494 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
495
Michel Dänzer691f08d2012-09-06 18:03:38 +0200496 is_face_positive = LLVMBuildFCmp(gallivm->builder,
Marek Olšákd1d2af22015-01-04 20:45:35 +0100497 LLVMRealOGT, face,
Michel Dänzer691f08d2012-09-06 18:03:38 +0200498 lp_build_const_float(gallivm, 0.0f),
499 "");
500
Tom Stellarda75c6162012-01-06 17:38:37 -0500501 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100502 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200503 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
504 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
505 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
506 LLVMValueRef front, back;
507
508 args[0] = llvm_chan;
509 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900510 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100511 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100512 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200513
514 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900515 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100516 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100517 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200518
Michel Dänzer51f89a02013-12-09 15:33:53 +0900519 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200520 LLVMBuildSelect(gallivm->builder,
521 is_face_positive,
522 front,
523 back,
524 "");
525 }
526
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900527 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200528 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
529 LLVMValueRef args[4];
530
531 args[0] = uint->zero;
532 args[1] = attr_number;
533 args[2] = params;
534 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900535 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
536 build_intrinsic(gallivm->builder, intr_name,
537 input_type, args, args[3] ? 4 : 3,
538 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
539 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
540 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200541 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900542 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200543 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200544 } else {
545 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100546 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200547 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
548 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
549 args[0] = llvm_chan;
550 args[1] = attr_number;
551 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100552 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900553 radeon_bld->inputs[soa_index] =
554 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100555 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100556 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200557 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500558 }
559}
560
Marek Olšák5b06fc32014-05-06 18:12:40 +0200561static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
562{
563 struct gallivm_state *gallivm = &radeon_bld->gallivm;
564 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
565 SI_PARAM_ANCILLARY);
566 value = LLVMBuildLShr(gallivm->builder, value,
567 lp_build_const_int32(gallivm, 8), "");
568 value = LLVMBuildAnd(gallivm->builder, value,
569 lp_build_const_int32(gallivm, 0xf), "");
570 return value;
571}
572
Marek Olšákd7876082014-09-26 23:06:32 +0200573/**
574 * Load a dword from a constant buffer.
575 */
576static LLVMValueRef buffer_load_const(LLVMBuilderRef builder, LLVMValueRef resource,
577 LLVMValueRef offset, LLVMTypeRef return_type)
Marek Olšák250aa932014-05-06 14:10:47 +0200578{
579 LLVMValueRef args[2] = {resource, offset};
580
581 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
582 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
583}
584
Christian Könige4ed5872013-03-21 18:02:52 +0100585static void declare_system_value(
586 struct radeon_llvm_context * radeon_bld,
587 unsigned index,
588 const struct tgsi_full_declaration *decl)
589{
Marek Olšák8d03d922013-09-01 23:59:06 +0200590 struct si_shader_context *si_shader_ctx =
591 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200592 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
593 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100594 LLVMValueRef value = 0;
595
596 switch (decl->Semantic.Name) {
597 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200598 value = LLVMGetParam(radeon_bld->main_fn,
599 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100600 break;
601
602 case TGSI_SEMANTIC_VERTEXID:
Marek Olšákd7c6f392015-01-04 14:41:49 +0100603 value = LLVMBuildAdd(gallivm->builder,
604 LLVMGetParam(radeon_bld->main_fn,
605 si_shader_ctx->param_vertex_id),
606 LLVMGetParam(radeon_bld->main_fn,
607 SI_PARAM_BASE_VERTEX), "");
Christian Könige4ed5872013-03-21 18:02:52 +0100608 break;
609
Marek Olšáke3d4bdd2015-01-04 14:51:01 +0100610 case TGSI_SEMANTIC_VERTEXID_NOBASE:
611 value = LLVMGetParam(radeon_bld->main_fn,
612 si_shader_ctx->param_vertex_id);
613 break;
614
615 case TGSI_SEMANTIC_BASEVERTEX:
616 value = LLVMGetParam(radeon_bld->main_fn,
617 SI_PARAM_BASE_VERTEX);
618 break;
619
Marek Olšák5b06fc32014-05-06 18:12:40 +0200620 case TGSI_SEMANTIC_SAMPLEID:
621 value = get_sample_id(radeon_bld);
622 break;
623
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200624 case TGSI_SEMANTIC_SAMPLEPOS:
625 {
626 LLVMBuilderRef builder = gallivm->builder;
627 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200628 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200629 LLVMValueRef resource = build_indexed_load_const(si_shader_ctx, desc, buf_index);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200630
631 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
632 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
633 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
634
635 LLVMValueRef pos[4] = {
Marek Olšákd7876082014-09-26 23:06:32 +0200636 buffer_load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
637 buffer_load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200638 lp_build_const_float(gallivm, 0),
639 lp_build_const_float(gallivm, 0)
640 };
641 value = lp_build_gather_values(gallivm, pos, 4);
642 break;
643 }
644
Christian Könige4ed5872013-03-21 18:02:52 +0100645 default:
646 assert(!"unknown system value");
647 return;
648 }
649
650 radeon_bld->system_values[index] = value;
651}
652
Tom Stellarda75c6162012-01-06 17:38:37 -0500653static LLVMValueRef fetch_constant(
654 struct lp_build_tgsi_context * bld_base,
655 const struct tgsi_full_src_register *reg,
656 enum tgsi_opcode_type type,
657 unsigned swizzle)
658{
Christian König55fe5cc2013-03-04 16:30:06 +0100659 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500660 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100661 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200662 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500663
Christian König0f6cf2b2013-03-15 15:53:25 +0100664 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100665 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500666
Christian König8514f5a2013-02-04 17:46:42 +0100667 if (swizzle == LP_CHAN_ALL) {
668 unsigned chan;
669 LLVMValueRef values[4];
670 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
671 values[chan] = fetch_constant(bld_base, reg, type, chan);
672
673 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
674 }
675
Marek Olšák2fd42002013-10-25 11:45:47 +0200676 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100677 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100678
Marek Olšák2fd42002013-10-25 11:45:47 +0200679 if (!reg->Register.Indirect)
680 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
681
Christian König0f6cf2b2013-03-15 15:53:25 +0100682 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
683 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
684 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200685 addr = lp_build_add(&bld_base->uint_bld, addr,
686 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200687
Marek Olšákd7876082014-09-26 23:06:32 +0200688 result = buffer_load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
Marek Olšák250aa932014-05-06 14:10:47 +0200689 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500690
Christian Königf5298b02013-02-28 14:50:07 +0100691 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500692}
693
Michel Dänzer26c71392012-08-24 12:03:11 +0200694/* Initialize arguments for the shader export intrinsic */
695static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900696 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200697 unsigned target,
698 LLVMValueRef *args)
699{
700 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
701 struct lp_build_context *uint =
702 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
703 struct lp_build_context *base = &bld_base->base;
704 unsigned compressed = 0;
705 unsigned chan;
706
Michel Dänzerf402acd2012-08-22 18:15:36 +0200707 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
708 int cbuf = target - V_008DFC_SQ_EXP_MRT;
709
710 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100711 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100712
713 if (compressed)
714 si_shader_ctx->shader->spi_shader_col_format |=
715 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
716 else
717 si_shader_ctx->shader->spi_shader_col_format |=
718 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200719
720 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200721 }
722 }
723
724 if (compressed) {
725 /* Pixel shader needs to pack output values before export */
726 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900727 args[0] = values[2 * chan];
728 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200729 args[chan + 5] =
730 build_intrinsic(base->gallivm->builder,
731 "llvm.SI.packf16",
732 LLVMInt32TypeInContext(base->gallivm->context),
733 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100734 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100735 args[chan + 7] = args[chan + 5] =
736 LLVMBuildBitCast(base->gallivm->builder,
737 args[chan + 5],
738 LLVMFloatTypeInContext(base->gallivm->context),
739 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200740 }
741
742 /* Set COMPR flag */
743 args[4] = uint->one;
744 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900745 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200746 /* +5 because the first output value will be
747 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900748 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200749
750 /* Clear COMPR flag */
751 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200752 }
753
754 /* XXX: This controls which components of the output
755 * registers actually get exported. (e.g bit 0 means export
756 * X component, bit 1 means export Y component, etc.) I'm
757 * hard coding this to 0xf for now. In the future, we might
758 * want to do something else. */
759 args[0] = lp_build_const_int32(base->gallivm, 0xf);
760
761 /* Specify whether the EXEC mask represents the valid mask */
762 args[1] = uint->zero;
763
764 /* Specify whether this is the last export */
765 args[2] = uint->zero;
766
767 /* Specify the target we are exporting */
768 args[3] = lp_build_const_int32(base->gallivm, target);
769
Michel Dänzer26c71392012-08-24 12:03:11 +0200770 /* XXX: We probably need to keep track of the output
771 * values, so we know what we are passing to the next
772 * stage. */
773}
774
Michel Dänzer404b29d2013-11-21 16:45:28 +0900775/* Load from output pointers and initialize arguments for the shader export intrinsic */
776static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
777 LLVMValueRef *out_ptr,
778 unsigned target,
779 LLVMValueRef *args)
780{
781 struct gallivm_state *gallivm = bld_base->base.gallivm;
782 LLVMValueRef values[4];
783 int i;
784
785 for (i = 0; i < 4; i++)
786 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
787
788 si_llvm_init_export_args(bld_base, values, target, args);
789}
790
Michel Dänzer7708a862012-11-02 15:57:30 +0100791static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900792 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100793{
794 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
795 struct gallivm_state *gallivm = bld_base->base.gallivm;
796
Christian Königa0dca442013-03-22 15:59:22 +0100797 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400798 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
799 SI_PARAM_ALPHA_REF);
800
Michel Dänzer7708a862012-11-02 15:57:30 +0100801 LLVMValueRef alpha_pass =
802 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100803 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900804 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400805 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100806 LLVMValueRef arg =
807 lp_build_select(&bld_base->base,
808 alpha_pass,
809 lp_build_const_float(gallivm, 1.0f),
810 lp_build_const_float(gallivm, -1.0f));
811
812 build_intrinsic(gallivm->builder,
813 "llvm.AMDGPU.kill",
814 LLVMVoidTypeInContext(gallivm->context),
815 &arg, 1, 0);
816 } else {
817 build_intrinsic(gallivm->builder,
818 "llvm.AMDGPU.kilp",
819 LLVMVoidTypeInContext(gallivm->context),
820 NULL, 0, 0);
821 }
Marek Olšákadc57972014-09-19 17:07:07 +0200822
823 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100824}
825
Michel Dänzere3befbc2013-05-15 18:09:50 +0200826static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900827 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200828{
829 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
830 struct lp_build_context *base = &bld_base->base;
831 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200832 unsigned reg_index;
833 unsigned chan;
834 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200835 LLVMValueRef base_elt;
836 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200837 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200838 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200839
Michel Dänzere3befbc2013-05-15 18:09:50 +0200840 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200841 LLVMValueRef *args = pos[2 + reg_index];
842
Michel Dänzere3befbc2013-05-15 18:09:50 +0200843 args[5] =
844 args[6] =
845 args[7] =
846 args[8] = lp_build_const_float(base->gallivm, 0.0f);
847
848 /* Compute dot products of position and user clip plane vectors */
849 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
850 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200851 args[1] = lp_build_const_int32(base->gallivm,
852 ((reg_index * 4 + chan) * 4 +
853 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200854 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200855 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200856 args[5 + chan] =
857 lp_build_add(base, args[5 + chan],
858 lp_build_mul(base, base_elt,
859 out_elts[const_chan]));
860 }
861 }
862
863 args[0] = lp_build_const_int32(base->gallivm, 0xf);
864 args[1] = uint->zero;
865 args[2] = uint->zero;
866 args[3] = lp_build_const_int32(base->gallivm,
867 V_008DFC_SQ_EXP_POS + 2 + reg_index);
868 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200869 }
870}
871
Marek Olšák8d03d922013-09-01 23:59:06 +0200872static void si_dump_streamout(struct pipe_stream_output_info *so)
873{
874 unsigned i;
875
876 if (so->num_outputs)
877 fprintf(stderr, "STREAMOUT\n");
878
879 for (i = 0; i < so->num_outputs; i++) {
880 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
881 so->output[i].start_component;
882 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
883 i, so->output[i].output_buffer,
884 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
885 so->output[i].register_index,
886 mask & 1 ? "x" : "",
887 mask & 2 ? "y" : "",
888 mask & 4 ? "z" : "",
889 mask & 8 ? "w" : "");
890 }
891}
892
893/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
894 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
895 * or v4i32 (num_channels=3,4). */
896static void build_tbuffer_store(struct si_shader_context *shader,
897 LLVMValueRef rsrc,
898 LLVMValueRef vdata,
899 unsigned num_channels,
900 LLVMValueRef vaddr,
901 LLVMValueRef soffset,
902 unsigned inst_offset,
903 unsigned dfmt,
904 unsigned nfmt,
905 unsigned offen,
906 unsigned idxen,
907 unsigned glc,
908 unsigned slc,
909 unsigned tfe)
910{
911 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
912 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
913 LLVMValueRef args[] = {
914 rsrc,
915 vdata,
916 LLVMConstInt(i32, num_channels, 0),
917 vaddr,
918 soffset,
919 LLVMConstInt(i32, inst_offset, 0),
920 LLVMConstInt(i32, dfmt, 0),
921 LLVMConstInt(i32, nfmt, 0),
922 LLVMConstInt(i32, offen, 0),
923 LLVMConstInt(i32, idxen, 0),
924 LLVMConstInt(i32, glc, 0),
925 LLVMConstInt(i32, slc, 0),
926 LLVMConstInt(i32, tfe, 0)
927 };
928
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900929 /* The instruction offset field has 12 bits */
930 assert(offen || inst_offset < (1 << 12));
931
Marek Olšák8d03d922013-09-01 23:59:06 +0200932 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
933 unsigned func = CLAMP(num_channels, 1, 3) - 1;
934 const char *types[] = {"i32", "v2i32", "v4i32"};
935 char name[256];
936 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
937
938 lp_build_intrinsic(gallivm->builder, name,
939 LLVMVoidTypeInContext(gallivm->context),
940 args, Elements(args));
941}
942
943static void build_streamout_store(struct si_shader_context *shader,
944 LLVMValueRef rsrc,
945 LLVMValueRef vdata,
946 unsigned num_channels,
947 LLVMValueRef vaddr,
948 LLVMValueRef soffset,
949 unsigned inst_offset)
950{
951 static unsigned dfmt[] = {
952 V_008F0C_BUF_DATA_FORMAT_32,
953 V_008F0C_BUF_DATA_FORMAT_32_32,
954 V_008F0C_BUF_DATA_FORMAT_32_32_32,
955 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
956 };
957 assert(num_channels >= 1 && num_channels <= 4);
958
959 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
960 inst_offset, dfmt[num_channels-1],
961 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
962}
963
964/* On SI, the vertex shader is responsible for writing streamout data
965 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900966static void si_llvm_emit_streamout(struct si_shader_context *shader,
967 struct si_shader_output_values *outputs,
968 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200969{
970 struct pipe_stream_output_info *so = &shader->shader->selector->so;
971 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
972 LLVMBuilderRef builder = gallivm->builder;
973 int i, j;
974 struct lp_build_if_state if_ctx;
975
976 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
977
978 LLVMValueRef so_param =
979 LLVMGetParam(shader->radeon_bld.main_fn,
980 shader->param_streamout_config);
981
982 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
983 LLVMValueRef so_vtx_count =
984 LLVMBuildAnd(builder,
985 LLVMBuildLShr(builder, so_param,
986 LLVMConstInt(i32, 16, 0), ""),
987 LLVMConstInt(i32, 127, 0), "");
988
989 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
990 NULL, 0, LLVMReadNoneAttribute);
991
992 /* can_emit = tid < so_vtx_count; */
993 LLVMValueRef can_emit =
994 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
995
996 /* Emit the streamout code conditionally. This actually avoids
997 * out-of-bounds buffer access. The hw tells us via the SGPR
998 * (so_vtx_count) which threads are allowed to emit streamout data. */
999 lp_build_if(&if_ctx, gallivm, can_emit);
1000 {
1001 /* The buffer offset is computed as follows:
1002 * ByteOffset = streamout_offset[buffer_id]*4 +
1003 * (streamout_write_index + thread_id)*stride[buffer_id] +
1004 * attrib_offset
1005 */
1006
1007 LLVMValueRef so_write_index =
1008 LLVMGetParam(shader->radeon_bld.main_fn,
1009 shader->param_streamout_write_index);
1010
1011 /* Compute (streamout_write_index + thread_id). */
1012 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1013
1014 /* Compute the write offset for each enabled buffer. */
1015 LLVMValueRef so_write_offset[4] = {};
1016 for (i = 0; i < 4; i++) {
1017 if (!so->stride[i])
1018 continue;
1019
1020 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1021 shader->param_streamout_offset[i]);
1022 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1023
1024 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1025 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1026 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1027 }
1028
Marek Olšák8d03d922013-09-01 23:59:06 +02001029 /* Write streamout data. */
1030 for (i = 0; i < so->num_outputs; i++) {
1031 unsigned buf_idx = so->output[i].output_buffer;
1032 unsigned reg = so->output[i].register_index;
1033 unsigned start = so->output[i].start_component;
1034 unsigned num_comps = so->output[i].num_components;
1035 LLVMValueRef out[4];
1036
1037 assert(num_comps && num_comps <= 4);
1038 if (!num_comps || num_comps > 4)
1039 continue;
1040
Marek Olšák558f7772014-10-04 22:37:23 +02001041 if (reg >= noutput)
1042 continue;
1043
Marek Olšák8d03d922013-09-01 23:59:06 +02001044 /* Load the output as int. */
1045 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001046 out[j] = LLVMBuildBitCast(builder,
1047 outputs[reg].values[start+j],
1048 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001049 }
1050
1051 /* Pack the output. */
1052 LLVMValueRef vdata = NULL;
1053
1054 switch (num_comps) {
1055 case 1: /* as i32 */
1056 vdata = out[0];
1057 break;
1058 case 2: /* as v2i32 */
1059 case 3: /* as v4i32 (aligned to 4) */
1060 case 4: /* as v4i32 */
1061 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1062 for (j = 0; j < num_comps; j++) {
1063 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1064 LLVMConstInt(i32, j, 0), "");
1065 }
1066 break;
1067 }
1068
1069 build_streamout_store(shader, shader->so_buffers[buf_idx],
1070 vdata, num_comps,
1071 so_write_offset[buf_idx],
1072 LLVMConstInt(i32, 0, 0),
1073 so->output[i].dst_offset*4);
1074 }
1075 }
1076 lp_build_endif(&if_ctx);
1077}
1078
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001079
Michel Dänzer404b29d2013-11-21 16:45:28 +09001080/* Generate export instructions for hardware VS shader stage */
1081static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1082 struct si_shader_output_values *outputs,
1083 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001084{
1085 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001086 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001087 struct lp_build_context * base = &bld_base->base;
1088 struct lp_build_context * uint =
1089 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001090 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001091 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001092 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001093 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001094 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001095 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001096 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001097 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001098
Michel Dänzer67e385b2014-01-08 17:48:21 +09001099 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1100 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001101 }
1102
Michel Dänzer404b29d2013-11-21 16:45:28 +09001103 for (i = 0; i < noutput; i++) {
1104 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001105 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001106
Michel Dänzer0afeea52013-05-02 14:53:17 +02001107handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001108 /* Select the correct target */
1109 switch(semantic_name) {
1110 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001111 psize_value = outputs[i].values[0];
1112 continue;
1113 case TGSI_SEMANTIC_EDGEFLAG:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001114 edgeflag_value = outputs[i].values[0];
1115 continue;
1116 case TGSI_SEMANTIC_LAYER:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001117 layer_value = outputs[i].values[0];
1118 continue;
1119 case TGSI_SEMANTIC_POSITION:
1120 target = V_008DFC_SQ_EXP_POS;
1121 break;
1122 case TGSI_SEMANTIC_COLOR:
1123 case TGSI_SEMANTIC_BCOLOR:
1124 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001125 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001126 param_count++;
1127 break;
1128 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001129 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1130 break;
1131 case TGSI_SEMANTIC_CLIPVERTEX:
1132 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1133 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001134 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001135 case TGSI_SEMANTIC_FOG:
1136 case TGSI_SEMANTIC_GENERIC:
1137 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001138 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001139 param_count++;
1140 break;
1141 default:
1142 target = 0;
1143 fprintf(stderr,
1144 "Warning: SI unhandled vs output type:%d\n",
1145 semantic_name);
1146 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001147
Michel Dänzer404b29d2013-11-21 16:45:28 +09001148 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001149
Michel Dänzer404b29d2013-11-21 16:45:28 +09001150 if (target >= V_008DFC_SQ_EXP_POS &&
1151 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1152 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1153 args, sizeof(args));
1154 } else {
1155 lp_build_intrinsic(base->gallivm->builder,
1156 "llvm.SI.export",
1157 LLVMVoidTypeInContext(base->gallivm->context),
1158 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001159 }
1160
1161 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1162 semantic_name = TGSI_SEMANTIC_GENERIC;
1163 goto handle_semantic;
1164 }
1165 }
1166
1167 /* We need to add the position output manually if it's missing. */
1168 if (!pos_args[0][0]) {
1169 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1170 pos_args[0][1] = uint->zero; /* EXEC mask */
1171 pos_args[0][2] = uint->zero; /* last export? */
1172 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1173 pos_args[0][4] = uint->zero; /* COMPR flag */
1174 pos_args[0][5] = base->zero; /* X */
1175 pos_args[0][6] = base->zero; /* Y */
1176 pos_args[0][7] = base->zero; /* Z */
1177 pos_args[0][8] = base->one; /* W */
1178 }
1179
1180 /* Write the misc vector (point size, edgeflag, layer, viewport). */
Marek Olšák16153472014-12-07 19:30:08 +01001181 if (shader->selector->info.writes_psize ||
1182 shader->selector->info.writes_edgeflag ||
1183 shader->selector->info.writes_layer) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001184 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
Marek Olšák16153472014-12-07 19:30:08 +01001185 shader->selector->info.writes_psize |
1186 (shader->selector->info.writes_edgeflag << 1) |
1187 (shader->selector->info.writes_layer << 2));
Michel Dänzer51f89a02013-12-09 15:33:53 +09001188 pos_args[1][1] = uint->zero; /* EXEC mask */
1189 pos_args[1][2] = uint->zero; /* last export? */
1190 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1191 pos_args[1][4] = uint->zero; /* COMPR flag */
1192 pos_args[1][5] = base->zero; /* X */
1193 pos_args[1][6] = base->zero; /* Y */
1194 pos_args[1][7] = base->zero; /* Z */
1195 pos_args[1][8] = base->zero; /* W */
1196
Marek Olšák16153472014-12-07 19:30:08 +01001197 if (shader->selector->info.writes_psize)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001198 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001199
Marek Olšák16153472014-12-07 19:30:08 +01001200 if (shader->selector->info.writes_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001201 /* The output is a float, but the hw expects an integer
1202 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001203 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1204 edgeflag_value,
1205 bld_base->uint_bld.elem_type, "");
1206 edgeflag_value = lp_build_min(&bld_base->int_bld,
1207 edgeflag_value,
1208 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001209
1210 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001211 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1212 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001213 base->elem_type, "");
1214 }
1215
Marek Olšák16153472014-12-07 19:30:08 +01001216 if (shader->selector->info.writes_layer)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001217 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001218 }
1219
1220 for (i = 0; i < 4; i++)
1221 if (pos_args[i][0])
1222 shader->nr_pos_exports++;
1223
1224 pos_idx = 0;
1225 for (i = 0; i < 4; i++) {
1226 if (!pos_args[i][0])
1227 continue;
1228
1229 /* Specify the target we are exporting */
1230 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1231
1232 if (pos_idx == shader->nr_pos_exports)
1233 /* Specify that this is the last export */
1234 pos_args[i][2] = uint->one;
1235
1236 lp_build_intrinsic(base->gallivm->builder,
1237 "llvm.SI.export",
1238 LLVMVoidTypeInContext(base->gallivm->context),
1239 pos_args[i], 9);
1240 }
1241}
1242
Michel Dänzer404b29d2013-11-21 16:45:28 +09001243static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1244{
1245 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1246 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001247 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001248 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001249 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001250 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1251 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001252 unsigned chan;
1253 int i;
1254
Marek Olšák216cf862014-10-04 17:04:05 +02001255 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001256 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001257 si_shader_ctx->radeon_bld.soa.outputs[i];
1258 int param_index = get_param_index(info->output_semantic_name[i],
1259 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001260 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001261
Marek Olšáke29353f2014-09-17 22:17:02 +02001262 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001263 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001264
1265 for (chan = 0; chan < 4; chan++) {
1266 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001267 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1268
Marek Olšákfc3b3352014-10-05 13:33:40 +02001269 build_tbuffer_store(si_shader_ctx,
1270 si_shader_ctx->esgs_ring,
1271 out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001272 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001273 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001274 V_008F0C_BUF_DATA_FORMAT_32,
1275 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001276 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001277 }
1278 }
1279}
1280
1281static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1282{
1283 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1284 struct gallivm_state *gallivm = bld_base->base.gallivm;
1285 LLVMValueRef args[2];
1286
1287 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1288 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1289 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1290 LLVMVoidTypeInContext(gallivm->context), args, 2,
1291 LLVMNoUnwindAttribute);
1292}
1293
1294static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1295{
1296 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1297 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001298 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001299 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001300 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001301
Marek Olšákec0d1682014-10-04 22:33:36 +02001302 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001303
Marek Olšákec0d1682014-10-04 22:33:36 +02001304 for (i = 0; i < info->num_outputs; i++) {
1305 outputs[i].name = info->output_semantic_name[i];
1306 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001307
Marek Olšákec0d1682014-10-04 22:33:36 +02001308 for (j = 0; j < 4; j++)
1309 outputs[i].values[j] =
1310 LLVMBuildLoad(gallivm->builder,
1311 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1312 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001313 }
1314
Marek Olšákec0d1682014-10-04 22:33:36 +02001315 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001316 FREE(outputs);
1317}
1318
Michel Dänzer51f89a02013-12-09 15:33:53 +09001319static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1320{
1321 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001322 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001323 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001324 struct lp_build_context * uint = &bld_base->uint_bld;
1325 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001326 LLVMValueRef args[9];
1327 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001328 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001329 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001330
Marek Olšák75e97e22014-10-04 23:13:50 +02001331 for (i = 0; i < info->num_outputs; i++) {
1332 unsigned semantic_name = info->output_semantic_name[i];
1333 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001334 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001335
Marek Olšák75e97e22014-10-04 23:13:50 +02001336 /* Select the correct target */
1337 switch (semantic_name) {
1338 case TGSI_SEMANTIC_POSITION:
1339 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001340 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001341 case TGSI_SEMANTIC_STENCIL:
1342 stencil_index = i;
1343 continue;
1344 case TGSI_SEMANTIC_SAMPLEMASK:
1345 samplemask_index = i;
1346 continue;
1347 case TGSI_SEMANTIC_COLOR:
1348 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1349 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1350 LLVMBuildStore(bld_base->base.gallivm->builder,
1351 bld_base->base.one,
1352 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001353
Marek Olšák75e97e22014-10-04 23:13:50 +02001354 if (semantic_index == 0 &&
1355 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1356 si_alpha_test(bld_base,
1357 si_shader_ctx->radeon_bld.soa.outputs[i]);
1358 break;
1359 default:
1360 target = 0;
1361 fprintf(stderr,
1362 "Warning: SI unhandled fs output type:%d\n",
1363 semantic_name);
1364 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001365
Marek Olšák75e97e22014-10-04 23:13:50 +02001366 si_llvm_init_export_args_load(bld_base,
1367 si_shader_ctx->radeon_bld.soa.outputs[i],
1368 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001369
Marek Olšák75e97e22014-10-04 23:13:50 +02001370 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1371 /* If there is an export instruction waiting to be emitted, do so now. */
1372 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001373 lp_build_intrinsic(base->gallivm->builder,
1374 "llvm.SI.export",
1375 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001376 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001377 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001378
1379 /* This instruction will be emitted at the end of the shader. */
1380 memcpy(last_args, args, sizeof(args));
1381
1382 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1383 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1384 semantic_index == 0 &&
1385 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1386 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1387 si_llvm_init_export_args_load(bld_base,
1388 si_shader_ctx->radeon_bld.soa.outputs[i],
1389 V_008DFC_SQ_EXP_MRT + c, args);
1390 lp_build_intrinsic(base->gallivm->builder,
1391 "llvm.SI.export",
1392 LLVMVoidTypeInContext(base->gallivm->context),
1393 args, 9);
1394 }
1395 }
1396 } else {
1397 lp_build_intrinsic(base->gallivm->builder,
1398 "llvm.SI.export",
1399 LLVMVoidTypeInContext(base->gallivm->context),
1400 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001401 }
1402 }
1403
Marek Olšákd0e8b652014-05-06 20:04:31 +02001404 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001405 LLVMValueRef out_ptr;
1406 unsigned mask = 0;
1407
1408 /* Specify the target we are exporting */
1409 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1410
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001411 args[5] = base->zero; /* R, depth */
1412 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1413 args[7] = base->zero; /* B, sample mask */
1414 args[8] = base->zero; /* A, alpha to mask */
1415
Michel Dänzer1a616c12012-11-13 17:35:09 +01001416 if (depth_index >= 0) {
1417 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1418 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1419 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001420 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001421 }
1422
1423 if (stencil_index >= 0) {
1424 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001425 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001426 mask |= 0x2;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001427 si_shader_ctx->shader->db_shader_control |=
1428 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001429 }
1430
Marek Olšákd0e8b652014-05-06 20:04:31 +02001431 if (samplemask_index >= 0) {
1432 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1433 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Marek Olšák368b0a72014-12-28 21:51:35 +01001434 mask |= 0x4;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001435 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1436 }
1437
Marek Olšák368b0a72014-12-28 21:51:35 +01001438 /* SI (except OLAND) has a bug that it only looks
1439 * at the X writemask component. */
1440 if (si_shader_ctx->screen->b.chip_class == SI &&
1441 si_shader_ctx->screen->b.family != CHIP_OLAND)
1442 mask |= 0x1;
1443
Marek Olšákd0e8b652014-05-06 20:04:31 +02001444 if (samplemask_index >= 0)
1445 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1446 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001447 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1448 else
1449 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1450
Michel Dänzer1a616c12012-11-13 17:35:09 +01001451 /* Specify which components to enable */
1452 args[0] = lp_build_const_int32(base->gallivm, mask);
1453
1454 args[1] =
1455 args[2] =
1456 args[4] = uint->zero;
1457
1458 if (last_args[0])
1459 lp_build_intrinsic(base->gallivm->builder,
1460 "llvm.SI.export",
1461 LLVMVoidTypeInContext(base->gallivm->context),
1462 args, 9);
1463 else
1464 memcpy(last_args, args, sizeof(args));
1465 }
1466
Michel Dänzer51f89a02013-12-09 15:33:53 +09001467 if (!last_args[0]) {
1468 /* Specify which components to enable */
1469 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001470
Michel Dänzer51f89a02013-12-09 15:33:53 +09001471 /* Specify the target we are exporting */
1472 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001473
Michel Dänzer51f89a02013-12-09 15:33:53 +09001474 /* Set COMPR flag to zero to export data as 32-bit */
1475 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001476
Michel Dänzer51f89a02013-12-09 15:33:53 +09001477 /* dummy bits */
1478 last_args[5]= uint->zero;
1479 last_args[6]= uint->zero;
1480 last_args[7]= uint->zero;
1481 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001482 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001483
1484 /* Specify whether the EXEC mask represents the valid mask */
1485 last_args[1] = uint->one;
1486
1487 /* Specify that this is the last export */
1488 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1489
1490 lp_build_intrinsic(base->gallivm->builder,
1491 "llvm.SI.export",
1492 LLVMVoidTypeInContext(base->gallivm->context),
1493 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001494}
1495
Marek Olšák4855acd2013-08-06 15:08:54 +02001496static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1497 struct lp_build_tgsi_context * bld_base,
1498 struct lp_build_emit_data * emit_data);
1499
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001500static bool tgsi_is_shadow_sampler(unsigned target)
1501{
1502 return target == TGSI_TEXTURE_SHADOW1D ||
1503 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1504 target == TGSI_TEXTURE_SHADOW2D ||
1505 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1506 target == TGSI_TEXTURE_SHADOWCUBE ||
1507 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1508 target == TGSI_TEXTURE_SHADOWRECT;
1509}
1510
Marek Olšák877bb522014-06-25 03:12:46 +02001511static const struct lp_build_tgsi_action tex_action;
1512
Tom Stellarda75c6162012-01-06 17:38:37 -05001513static void tex_fetch_args(
1514 struct lp_build_tgsi_context * bld_base,
1515 struct lp_build_emit_data * emit_data)
1516{
Christian König55fe5cc2013-03-04 16:30:06 +01001517 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001518 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001519 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001520 unsigned opcode = inst->Instruction.Opcode;
1521 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001522 LLVMValueRef coords[4];
1523 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001524 int ref_pos;
1525 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001526 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001527 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001528 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1529 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001530 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001531
1532 if (target == TGSI_TEXTURE_BUFFER) {
1533 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1534 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1535 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1536 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1537
Marek Olšák4f3f0432014-07-07 22:49:15 +02001538 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001539 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1540 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
Marek Olšákb142dd22015-02-01 16:58:08 +01001541 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.one, "");
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001542 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1543
1544 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1545 emit_data->args[0] = res;
1546 emit_data->args[1] = bld_base->uint_bld.zero;
1547 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1548 emit_data->arg_count = 3;
1549 return;
1550 }
Tom Stellard467f5162012-05-16 15:15:35 -04001551
Michel Dänzer120efee2013-01-25 12:10:11 +01001552 /* Fetch and project texture coordinates */
1553 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001554 for (chan = 0; chan < 3; chan++ ) {
1555 coords[chan] = lp_build_emit_fetch(bld_base,
1556 emit_data->inst, 0,
1557 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001558 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001559 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1560 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001561 coords[chan],
1562 coords[3]);
1563 }
1564
Michel Dänzer120efee2013-01-25 12:10:11 +01001565 if (opcode == TGSI_OPCODE_TXP)
1566 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001567
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001568 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001569 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001570 /* The offsets are six-bit signed integers packed like this:
1571 * X=[5:0], Y=[13:8], and Z=[21:16].
1572 */
1573 LLVMValueRef offset[3], pack;
1574
1575 assert(inst->Texture.NumOffsets == 1);
1576
1577 for (chan = 0; chan < 3; chan++) {
1578 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1579 emit_data->inst, 0, chan);
1580 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1581 lp_build_const_int32(gallivm, 0x3f), "");
1582 if (chan)
1583 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1584 lp_build_const_int32(gallivm, chan*8), "");
1585 }
1586
1587 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1588 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1589 address[count++] = pack;
1590 }
1591
Michel Dänzer120efee2013-01-25 12:10:11 +01001592 /* Pack LOD bias value */
1593 if (opcode == TGSI_OPCODE_TXB)
1594 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001595 if (opcode == TGSI_OPCODE_TXB2)
1596 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001597
Michel Dänzer120efee2013-01-25 12:10:11 +01001598 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001599 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001600 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1601 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1602 } else {
1603 assert(ref_pos >= 0);
1604 address[count++] = coords[ref_pos];
1605 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001606 }
1607
Marek Olšákb279f0142014-07-04 03:19:58 +02001608 if (target == TGSI_TEXTURE_CUBE ||
1609 target == TGSI_TEXTURE_CUBE_ARRAY ||
1610 target == TGSI_TEXTURE_SHADOWCUBE ||
1611 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1612 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1613
Michel Dänzera6b83c02013-02-21 16:10:55 +01001614 /* Pack user derivatives */
1615 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001616 int num_deriv_channels, param;
1617
1618 switch (target) {
1619 case TGSI_TEXTURE_3D:
1620 num_deriv_channels = 3;
1621 break;
1622 case TGSI_TEXTURE_2D:
1623 case TGSI_TEXTURE_SHADOW2D:
1624 case TGSI_TEXTURE_RECT:
1625 case TGSI_TEXTURE_SHADOWRECT:
1626 case TGSI_TEXTURE_2D_ARRAY:
1627 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1628 case TGSI_TEXTURE_CUBE:
1629 case TGSI_TEXTURE_SHADOWCUBE:
1630 case TGSI_TEXTURE_CUBE_ARRAY:
1631 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1632 num_deriv_channels = 2;
1633 break;
1634 case TGSI_TEXTURE_1D:
1635 case TGSI_TEXTURE_SHADOW1D:
1636 case TGSI_TEXTURE_1D_ARRAY:
1637 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1638 num_deriv_channels = 1;
1639 break;
1640 default:
1641 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001642 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001643
1644 for (param = 1; param <= 2; param++)
1645 for (chan = 0; chan < num_deriv_channels; chan++)
1646 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001647 }
1648
Michel Dänzer120efee2013-01-25 12:10:11 +01001649 /* Pack texture coordinates */
1650 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001651 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001652 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001653 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001654 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001655
Marek Olšákd2bd6342013-09-18 15:40:21 +02001656 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001657 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001658 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001659 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001660 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001661
1662 if (count > 16) {
1663 assert(!"Cannot handle more than 16 texture address parameters");
1664 count = 16;
1665 }
1666
1667 for (chan = 0; chan < count; chan++ ) {
1668 address[chan] = LLVMBuildBitCast(gallivm->builder,
1669 address[chan],
1670 LLVMInt32TypeInContext(gallivm->context),
1671 "");
1672 }
1673
Marek Olšák4855acd2013-08-06 15:08:54 +02001674 /* Adjust the sample index according to FMASK.
1675 *
1676 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1677 * which is the identity mapping. Each nibble says which physical sample
1678 * should be fetched to get that sample.
1679 *
1680 * For example, 0x11111100 means there are only 2 samples stored and
1681 * the second sample covers 3/4 of the pixel. When reading samples 0
1682 * and 1, return physical sample 0 (determined by the first two 0s
1683 * in FMASK), otherwise return physical sample 1.
1684 *
1685 * The sample index should be adjusted as follows:
1686 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1687 */
1688 if (target == TGSI_TEXTURE_2D_MSAA ||
1689 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1690 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1691 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001692 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001693 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001694 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001695
Marek Olšákd2bd6342013-09-18 15:40:21 +02001696 memcpy(txf_address, address, sizeof(txf_address));
1697
1698 if (target == TGSI_TEXTURE_2D_MSAA) {
1699 txf_address[2] = bld_base->uint_bld.zero;
1700 }
1701 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001702
1703 /* Pad to a power-of-two size. */
1704 while (txf_count < util_next_power_of_two(txf_count))
1705 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1706
1707 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001708 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1709 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1710 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001711 txf_emit_data.chan = 0;
1712 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001713 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001714 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001715 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001716 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001717 txf_emit_data.arg_count = 3;
1718
Marek Olšák877bb522014-06-25 03:12:46 +02001719 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001720
1721 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001722 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1723 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1724
1725 /* Apply the formula. */
1726 LLVMValueRef fmask =
1727 LLVMBuildExtractElement(gallivm->builder,
1728 txf_emit_data.output[0],
1729 uint_bld->zero, "");
1730
Marek Olšákd2bd6342013-09-18 15:40:21 +02001731 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001732
1733 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001734 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001735
1736 LLVMValueRef shifted_fmask =
1737 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1738
1739 LLVMValueRef final_sample =
1740 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1741
1742 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1743 * resource descriptor is 0 (invalid),
1744 */
1745 LLVMValueRef fmask_desc =
1746 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001747 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001748 LLVMVectorType(uint_bld->elem_type, 8), "");
1749
1750 LLVMValueRef fmask_word1 =
1751 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1752 uint_bld->one, "");
1753
1754 LLVMValueRef word1_is_nonzero =
1755 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1756 fmask_word1, uint_bld->zero, "");
1757
Marek Olšákd2bd6342013-09-18 15:40:21 +02001758 /* Replace the MSAA sample index. */
1759 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001760 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001761 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001762 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001763
Michel Dänzer36231112013-05-02 09:44:45 +02001764 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001765 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001766
1767 if (opcode == TGSI_OPCODE_TXF) {
1768 /* add tex offsets */
1769 if (inst->Texture.NumOffsets) {
1770 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1771 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1772 const struct tgsi_texture_offset * off = inst->TexOffsets;
1773
1774 assert(inst->Texture.NumOffsets == 1);
1775
Marek Olšákdefedc02013-09-18 15:36:38 +02001776 switch (target) {
1777 case TGSI_TEXTURE_3D:
1778 address[2] = lp_build_add(uint_bld, address[2],
1779 bld->immediates[off->Index][off->SwizzleZ]);
1780 /* fall through */
1781 case TGSI_TEXTURE_2D:
1782 case TGSI_TEXTURE_SHADOW2D:
1783 case TGSI_TEXTURE_RECT:
1784 case TGSI_TEXTURE_SHADOWRECT:
1785 case TGSI_TEXTURE_2D_ARRAY:
1786 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001787 address[1] =
1788 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001789 bld->immediates[off->Index][off->SwizzleY]);
1790 /* fall through */
1791 case TGSI_TEXTURE_1D:
1792 case TGSI_TEXTURE_SHADOW1D:
1793 case TGSI_TEXTURE_1D_ARRAY:
1794 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1795 address[0] =
1796 lp_build_add(uint_bld, address[0],
1797 bld->immediates[off->Index][off->SwizzleX]);
1798 break;
1799 /* texture offsets do not apply to other texture targets */
1800 }
Michel Dänzer36231112013-05-02 09:44:45 +02001801 }
1802
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001803 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1804 emit_data->arg_count = 3;
1805
Michel Dänzer36231112013-05-02 09:44:45 +02001806 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001807 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001808 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001809 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001810 opcode == TGSI_OPCODE_LODQ ||
1811 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001812 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1813 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1814 target == TGSI_TEXTURE_2D_ARRAY ||
1815 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1816 target == TGSI_TEXTURE_CUBE_ARRAY ||
1817 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1818 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001819 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001820
Marek Olšák57f3da92014-06-16 16:53:42 +02001821 if (opcode == TGSI_OPCODE_TG4) {
1822 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001823
Marek Olšák57f3da92014-06-16 16:53:42 +02001824 /* DMASK was repurposed for GATHER4. 4 components are always
1825 * returned and DMASK works like a swizzle - it selects
1826 * the component to fetch. The only valid DMASK values are
1827 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1828 * (red,red,red,red) etc.) The ISA document doesn't mention
1829 * this.
1830 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001831
Marek Olšák57f3da92014-06-16 16:53:42 +02001832 /* Get the component index from src1.x for Gather4. */
1833 if (!tgsi_is_shadow_sampler(target)) {
1834 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1835 LLVMValueRef comp_imm;
1836 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001837
Marek Olšák57f3da92014-06-16 16:53:42 +02001838 assert(src1.File == TGSI_FILE_IMMEDIATE);
1839
1840 comp_imm = imms[src1.Index][src1.SwizzleX];
1841 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1842 gather_comp = CLAMP(gather_comp, 0, 3);
1843 }
1844
1845 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001846 }
1847
Marek Olšák4855acd2013-08-06 15:08:54 +02001848 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001849 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001850 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1851 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1852 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1853 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1854 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1855 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1856 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1857
1858 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001859
1860 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001861 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001862 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001863 } else {
1864 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1865 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001866 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001867
1868 emit_data->dst_type = LLVMVectorType(
1869 LLVMFloatTypeInContext(gallivm->context),
1870 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001871 }
1872
Marek Olšák2484daa2014-04-22 21:23:29 +02001873 /* The fetch opcode has been converted to a 2D array fetch.
1874 * This simplifies the LLVM backend. */
1875 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1876 target = TGSI_TEXTURE_2D_ARRAY;
1877 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1878 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1879
Michel Dänzer120efee2013-01-25 12:10:11 +01001880 /* Pad to power of two vector */
1881 while (count < util_next_power_of_two(count))
1882 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1883
Christian Königccf3e8f2013-03-26 15:09:27 +01001884 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001885}
1886
Michel Dänzer07eddc42013-02-06 15:43:10 +01001887static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1888 struct lp_build_tgsi_context * bld_base,
1889 struct lp_build_emit_data * emit_data)
1890{
1891 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001892 unsigned opcode = emit_data->inst->Instruction.Opcode;
1893 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001894 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001895 bool has_offset = HAVE_LLVM >= 0x0305 ?
1896 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001897
Marek Olšák877bb522014-06-25 03:12:46 +02001898 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001899 emit_data->output[emit_data->chan] = build_intrinsic(
1900 base->gallivm->builder,
1901 "llvm.SI.vs.load.input", emit_data->dst_type,
1902 emit_data->args, emit_data->arg_count,
1903 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1904 return;
1905 }
1906
Marek Olšák877bb522014-06-25 03:12:46 +02001907 if (opcode == TGSI_OPCODE_TG4 ||
1908 opcode == TGSI_OPCODE_LODQ ||
1909 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1910 bool is_shadow = tgsi_is_shadow_sampler(target);
1911 const char *name = "llvm.SI.image.sample";
1912 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001913
Marek Olšák877bb522014-06-25 03:12:46 +02001914 switch (opcode) {
1915 case TGSI_OPCODE_TEX:
1916 case TGSI_OPCODE_TEX2:
1917 case TGSI_OPCODE_TXP:
1918 break;
1919 case TGSI_OPCODE_TXB:
1920 case TGSI_OPCODE_TXB2:
1921 infix = ".b";
1922 break;
1923 case TGSI_OPCODE_TXL:
1924 case TGSI_OPCODE_TXL2:
1925 infix = ".l";
1926 break;
1927 case TGSI_OPCODE_TXD:
1928 infix = ".d";
1929 break;
1930 case TGSI_OPCODE_TG4:
1931 name = "llvm.SI.gather4";
1932 break;
1933 case TGSI_OPCODE_LODQ:
1934 name = "llvm.SI.getlod";
1935 is_shadow = false;
1936 has_offset = false;
1937 break;
1938 default:
1939 assert(0);
1940 return;
1941 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001942
Marek Olšák877bb522014-06-25 03:12:46 +02001943 /* Add the type and suffixes .c, .o if needed. */
1944 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1945 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1946 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001947
Marek Olšák877bb522014-06-25 03:12:46 +02001948 emit_data->output[emit_data->chan] = build_intrinsic(
1949 base->gallivm->builder, intr_name, emit_data->dst_type,
1950 emit_data->args, emit_data->arg_count,
1951 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1952 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001953 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001954 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001955
Marek Olšák877bb522014-06-25 03:12:46 +02001956 switch (opcode) {
1957 case TGSI_OPCODE_TEX:
1958 case TGSI_OPCODE_TEX2:
1959 case TGSI_OPCODE_TXP:
1960 name = "llvm.SI.sample";
1961 break;
1962 case TGSI_OPCODE_TXB:
1963 case TGSI_OPCODE_TXB2:
1964 name = "llvm.SI.sampleb";
1965 break;
1966 case TGSI_OPCODE_TXD:
1967 name = "llvm.SI.sampled";
1968 break;
1969 case TGSI_OPCODE_TXF:
1970 name = "llvm.SI.imageload";
1971 break;
1972 case TGSI_OPCODE_TXL:
1973 case TGSI_OPCODE_TXL2:
1974 name = "llvm.SI.samplel";
1975 break;
1976 default:
1977 assert(0);
1978 return;
1979 }
1980
Marek Olšákd859bdb2014-07-14 19:40:14 +02001981 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1982 v16i8 = LLVMVectorType(i8, 16);
1983 v32i8 = LLVMVectorType(i8, 32);
1984
1985 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1986 emit_data->args[1], v32i8, "");
1987 if (opcode != TGSI_OPCODE_TXF) {
1988 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1989 emit_data->args[2], v16i8, "");
1990 }
1991
Marek Olšák877bb522014-06-25 03:12:46 +02001992 sprintf(intr_name, "%s.v%ui32", name,
1993 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1994
1995 emit_data->output[emit_data->chan] = build_intrinsic(
1996 base->gallivm->builder, intr_name, emit_data->dst_type,
1997 emit_data->args, emit_data->arg_count,
1998 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1999 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002000}
2001
Michel Dänzer0495adb2013-05-06 12:45:14 +02002002static void txq_fetch_args(
2003 struct lp_build_tgsi_context * bld_base,
2004 struct lp_build_emit_data * emit_data)
2005{
2006 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2007 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002008 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002009 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002010
Marek Olšák2484daa2014-04-22 21:23:29 +02002011 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002012 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2013 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2014
2015 /* Read the size from the buffer descriptor directly. */
2016 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2017 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2018 size = LLVMBuildExtractElement(gallivm->builder, size,
Marek Olšákb142dd22015-02-01 16:58:08 +01002019 lp_build_const_int32(gallivm, 6), "");
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002020 emit_data->args[0] = size;
2021 return;
2022 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002023
2024 /* Mip level */
2025 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2026
2027 /* Resource */
2028 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2029
Marek Olšák2484daa2014-04-22 21:23:29 +02002030 /* Texture target */
2031 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2032 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2033 target = TGSI_TEXTURE_2D_ARRAY;
2034
Michel Dänzer0495adb2013-05-06 12:45:14 +02002035 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002036 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002037
2038 emit_data->arg_count = 3;
2039
2040 emit_data->dst_type = LLVMVectorType(
2041 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2042 4);
2043}
2044
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002045static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2046 struct lp_build_tgsi_context * bld_base,
2047 struct lp_build_emit_data * emit_data)
2048{
Marek Olšák2484daa2014-04-22 21:23:29 +02002049 unsigned target = emit_data->inst->Texture.Texture;
2050
2051 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002052 /* Just return the buffer size. */
2053 emit_data->output[emit_data->chan] = emit_data->args[0];
2054 return;
2055 }
2056
2057 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002058
2059 /* Divide the number of layers by 6 to get the number of cubes. */
2060 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2061 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2062 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2063 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2064 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2065
2066 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2067 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2068 z = LLVMBuildSDiv(builder, z, six, "");
2069
2070 emit_data->output[emit_data->chan] =
2071 LLVMBuildInsertElement(builder, v4, z, two, "");
2072 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002073}
2074
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002075static void si_llvm_emit_ddxy(
2076 const struct lp_build_tgsi_action * action,
2077 struct lp_build_tgsi_context * bld_base,
2078 struct lp_build_emit_data * emit_data)
2079{
2080 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2081 struct gallivm_state *gallivm = bld_base->base.gallivm;
2082 struct lp_build_context * base = &bld_base->base;
2083 const struct tgsi_full_instruction *inst = emit_data->inst;
2084 unsigned opcode = inst->Instruction.Opcode;
2085 LLVMValueRef indices[2];
2086 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2087 LLVMValueRef tl, trbl, result[4];
2088 LLVMTypeRef i32;
2089 unsigned swizzle[4];
2090 unsigned c;
2091
2092 i32 = LLVMInt32TypeInContext(gallivm->context);
2093
2094 indices[0] = bld_base->uint_bld.zero;
2095 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2096 NULL, 0, LLVMReadNoneAttribute);
2097 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2098 indices, 2, "");
2099
2100 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2101 lp_build_const_int32(gallivm, 0xfffffffc), "");
2102 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2103 indices, 2, "");
2104
2105 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2106 lp_build_const_int32(gallivm,
2107 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2108 "");
2109 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2110 indices, 2, "");
2111
2112 for (c = 0; c < 4; ++c) {
2113 unsigned i;
2114
2115 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2116 for (i = 0; i < c; ++i) {
2117 if (swizzle[i] == swizzle[c]) {
2118 result[c] = result[i];
2119 break;
2120 }
2121 }
2122 if (i != c)
2123 continue;
2124
2125 LLVMBuildStore(gallivm->builder,
2126 LLVMBuildBitCast(gallivm->builder,
2127 lp_build_emit_fetch(bld_base, inst, 0, c),
2128 i32, ""),
2129 store_ptr);
2130
2131 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2132 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2133
2134 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2135 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2136
2137 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2138 }
2139
2140 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2141}
2142
Michel Dänzer404b29d2013-11-21 16:45:28 +09002143/* Emit one vertex from the geometry shader */
2144static void si_llvm_emit_vertex(
2145 const struct lp_build_tgsi_action *action,
2146 struct lp_build_tgsi_context *bld_base,
2147 struct lp_build_emit_data *emit_data)
2148{
2149 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002150 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002151 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002152 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002153 struct gallivm_state *gallivm = bld_base->base.gallivm;
2154 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002155 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2156 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002157 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002158 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002159 LLVMValueRef args[2];
2160 unsigned chan;
2161 int i;
2162
Michel Dänzer404b29d2013-11-21 16:45:28 +09002163 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002164 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002165
2166 /* If this thread has already emitted the declared maximum number of
2167 * vertices, kill it: excessive vertex emissions are not supposed to
2168 * have any effect, and GS threads have no externally observable
2169 * effects other than emitting vertices.
2170 */
2171 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2172 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002173 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002174 kill = lp_build_select(&bld_base->base, can_emit,
2175 lp_build_const_float(gallivm, 1.0f),
2176 lp_build_const_float(gallivm, -1.0f));
2177 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2178 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2179
Marek Olšák02134cf2014-10-04 22:07:50 +02002180 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002181 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002182 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002183
2184 for (chan = 0; chan < 4; chan++) {
2185 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002186 LLVMValueRef voffset =
2187 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002188 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002189
2190 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2191 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002192
2193 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2194
Marek Olšákfc3b3352014-10-05 13:33:40 +02002195 build_tbuffer_store(si_shader_ctx,
2196 si_shader_ctx->gsvs_ring,
2197 out_val, 1,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002198 voffset, soffset, 0,
2199 V_008F0C_BUF_DATA_FORMAT_32,
2200 V_008F0C_BUF_NUM_FORMAT_UINT,
2201 1, 0, 1, 1, 0);
2202 }
2203 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002204 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2205 lp_build_const_int32(gallivm, 1));
2206 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002207
2208 /* Signal vertex emission */
2209 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2210 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2211 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2212 LLVMVoidTypeInContext(gallivm->context), args, 2,
2213 LLVMNoUnwindAttribute);
2214}
2215
2216/* Cut one primitive from the geometry shader */
2217static void si_llvm_emit_primitive(
2218 const struct lp_build_tgsi_action *action,
2219 struct lp_build_tgsi_context *bld_base,
2220 struct lp_build_emit_data *emit_data)
2221{
2222 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2223 struct gallivm_state *gallivm = bld_base->base.gallivm;
2224 LLVMValueRef args[2];
2225
2226 /* Signal primitive cut */
2227 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2228 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2229 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2230 LLVMVoidTypeInContext(gallivm->context), args, 2,
2231 LLVMNoUnwindAttribute);
2232}
2233
Tom Stellarda75c6162012-01-06 17:38:37 -05002234static const struct lp_build_tgsi_action tex_action = {
2235 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002236 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002237};
2238
Michel Dänzer0495adb2013-05-06 12:45:14 +02002239static const struct lp_build_tgsi_action txq_action = {
2240 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002241 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002242 .intr_name = "llvm.SI.resinfo"
2243};
2244
Christian König206f0592013-03-20 14:37:21 +01002245static void create_meta_data(struct si_shader_context *si_shader_ctx)
2246{
2247 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2248 LLVMValueRef args[3];
2249
2250 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2251 args[1] = 0;
2252 args[2] = lp_build_const_int32(gallivm, 1);
2253
2254 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2255}
2256
Marek Olšák4f3f0432014-07-07 22:49:15 +02002257static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2258{
2259 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2260 CONST_ADDR_SPACE);
2261}
2262
Christian König55fe5cc2013-03-04 16:30:06 +01002263static void create_function(struct si_shader_context *si_shader_ctx)
2264{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002265 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2266 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002267 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002268 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002269 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002270
Christian König55fe5cc2013-03-04 16:30:06 +01002271 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002272 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002273 f32 = LLVMFloatTypeInContext(gallivm->context);
2274 v2i32 = LLVMVectorType(i32, 2);
2275 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002276 v4i32 = LLVMVectorType(i32, 4);
2277 v8i32 = LLVMVectorType(i32, 8);
2278 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002279
Marek Olšákee2a8182014-07-07 23:27:19 +02002280 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002281 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002282 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2283 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002284 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002285
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002286 switch (si_shader_ctx->type) {
2287 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002288 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002289 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002290 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002291 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002292 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002293
Michel Dänzer404b29d2013-11-21 16:45:28 +09002294 if (shader->key.vs.as_es) {
2295 params[SI_PARAM_ES2GS_OFFSET] = i32;
2296 num_params++;
2297 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002298 if (shader->is_gs_copy_shader) {
2299 last_array_pointer = SI_PARAM_CONST;
2300 num_params = SI_PARAM_CONST+1;
2301 }
2302
Michel Dänzer404b29d2013-11-21 16:45:28 +09002303 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002304
Michel Dänzer404b29d2013-11-21 16:45:28 +09002305 /* Streamout SGPRs. */
2306 if (shader->selector->so.num_outputs) {
2307 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2308 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2309 }
2310 /* A streamout buffer offset is loaded if the stride is non-zero. */
2311 for (i = 0; i < 4; i++) {
2312 if (!shader->selector->so.stride[i])
2313 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002314
Michel Dänzer404b29d2013-11-21 16:45:28 +09002315 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2316 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002317 }
2318
2319 last_sgpr = num_params-1;
2320
2321 /* VGPRs */
2322 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2323 params[num_params++] = i32; /* unused*/
2324 params[num_params++] = i32; /* unused */
2325 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002326 break;
Christian König0666ffd2013-03-05 15:07:39 +01002327
Michel Dänzer404b29d2013-11-21 16:45:28 +09002328 case TGSI_PROCESSOR_GEOMETRY:
2329 params[SI_PARAM_GS2VS_OFFSET] = i32;
2330 params[SI_PARAM_GS_WAVE_ID] = i32;
2331 last_sgpr = SI_PARAM_GS_WAVE_ID;
2332
2333 /* VGPRs */
2334 params[SI_PARAM_VTX0_OFFSET] = i32;
2335 params[SI_PARAM_VTX1_OFFSET] = i32;
2336 params[SI_PARAM_PRIMITIVE_ID] = i32;
2337 params[SI_PARAM_VTX2_OFFSET] = i32;
2338 params[SI_PARAM_VTX3_OFFSET] = i32;
2339 params[SI_PARAM_VTX4_OFFSET] = i32;
2340 params[SI_PARAM_VTX5_OFFSET] = i32;
2341 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2342 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2343 break;
2344
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002345 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002346 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002347 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002348 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002349 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2350 params[SI_PARAM_PERSP_CENTER] = v2i32;
2351 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2352 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2353 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2354 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2355 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2356 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2357 params[SI_PARAM_POS_X_FLOAT] = f32;
2358 params[SI_PARAM_POS_Y_FLOAT] = f32;
2359 params[SI_PARAM_POS_Z_FLOAT] = f32;
2360 params[SI_PARAM_POS_W_FLOAT] = f32;
2361 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002362 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002363 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2364 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002365 num_params = SI_PARAM_POS_FIXED_PT+1;
2366 break;
2367
2368 default:
2369 assert(0 && "unimplemented shader");
2370 return;
Christian Königc4973212013-03-05 12:14:02 +01002371 }
Christian König55fe5cc2013-03-04 16:30:06 +01002372
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002373 assert(num_params <= Elements(params));
2374 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002375 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002376
Marek Olšák1829f9c2015-01-04 17:08:57 +01002377 if (shader->dx10_clamp_mode)
2378 LLVMAddTargetDependentFunctionAttr(si_shader_ctx->radeon_bld.main_fn,
2379 "enable-no-nans-fp-math", "true");
2380
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002381 for (i = 0; i <= last_sgpr; ++i) {
2382 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002383
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002384 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2385 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002386 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002387 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002388 else
2389 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002390 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002391
Michel Dänzer404b29d2013-11-21 16:45:28 +09002392 if (bld_base->info &&
2393 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2394 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002395 si_shader_ctx->ddxy_lds =
2396 LLVMAddGlobalInAddressSpace(gallivm->module,
2397 LLVMArrayType(i32, 64),
2398 "ddxy_lds",
2399 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002400}
Tom Stellarda75c6162012-01-06 17:38:37 -05002401
Christian König0f6cf2b2013-03-15 15:53:25 +01002402static void preload_constants(struct si_shader_context *si_shader_ctx)
2403{
2404 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2405 struct gallivm_state * gallivm = bld_base->base.gallivm;
2406 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002407 unsigned buf;
2408 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002409
Marek Olšákee2a8182014-07-07 23:27:19 +02002410 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002411 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002412
Marek Olšák2fd42002013-10-25 11:45:47 +02002413 if (num_const == 0)
2414 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002415
Marek Olšák2fd42002013-10-25 11:45:47 +02002416 /* Allocate space for the constant values */
2417 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002418
Marek Olšák2fd42002013-10-25 11:45:47 +02002419 /* Load the resource descriptor */
2420 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002421 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002422
Marek Olšák2fd42002013-10-25 11:45:47 +02002423 /* Load the constants, we rely on the code sinking to do the rest */
2424 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002425 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002426 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002427 si_shader_ctx->const_resource[buf],
2428 lp_build_const_int32(gallivm, i * 4),
2429 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002430 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002431 }
2432}
2433
Christian König1c100182013-03-17 16:02:42 +01002434static void preload_samplers(struct si_shader_context *si_shader_ctx)
2435{
2436 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2437 struct gallivm_state * gallivm = bld_base->base.gallivm;
2438 const struct tgsi_shader_info * info = bld_base->info;
2439
2440 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2441
2442 LLVMValueRef res_ptr, samp_ptr;
2443 LLVMValueRef offset;
2444
2445 if (num_samplers == 0)
2446 return;
2447
Christian König1c100182013-03-17 16:02:42 +01002448 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2449 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2450
2451 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2452 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002453 /* Resource */
2454 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002455 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002456
2457 /* Sampler */
2458 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002459 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002460
2461 /* FMASK resource */
2462 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002463 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2464 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002465 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002466 }
Christian König1c100182013-03-17 16:02:42 +01002467 }
2468}
2469
Marek Olšák8d03d922013-09-01 23:59:06 +02002470static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2471{
2472 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2473 struct gallivm_state * gallivm = bld_base->base.gallivm;
2474 unsigned i;
2475
Michel Dänzer67e385b2014-01-08 17:48:21 +09002476 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2477 si_shader_ctx->shader->key.vs.as_es ||
2478 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002479 return;
2480
2481 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002482 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002483
2484 /* Load the resources, we rely on the code sinking to do the rest */
2485 for (i = 0; i < 4; ++i) {
2486 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002487 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002488 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002489
Marek Olšákd7876082014-09-26 23:06:32 +02002490 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002491 }
2492 }
2493}
2494
Marek Olšákfc3b3352014-10-05 13:33:40 +02002495/**
2496 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
2497 * for later use.
2498 */
2499static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
2500{
2501 struct gallivm_state *gallivm =
2502 si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2503
2504 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2505 SI_PARAM_RW_BUFFERS);
2506
2507 if ((si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
2508 si_shader_ctx->shader->key.vs.as_es) ||
2509 si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY) {
2510 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_ESGS);
2511
2512 si_shader_ctx->esgs_ring =
2513 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2514 }
2515
2516 if (si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY ||
2517 si_shader_ctx->shader->is_gs_copy_shader) {
2518 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_GSVS);
2519
2520 si_shader_ctx->gsvs_ring =
2521 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2522 }
2523}
2524
Tom Stellard2397a722014-12-10 09:13:59 -05002525void si_shader_binary_read_config(const struct si_screen *sscreen,
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002526 struct si_shader *shader,
2527 unsigned symbol_offset)
Tom Stellard302f53d2012-10-25 13:50:10 -04002528{
Tom Stellard302f53d2012-10-25 13:50:10 -04002529 unsigned i;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002530 const unsigned char *config =
Tom Stellard2397a722014-12-10 09:13:59 -05002531 radeon_shader_binary_config_start(&shader->binary,
2532 symbol_offset);
Tom Stellard302f53d2012-10-25 13:50:10 -04002533
Tom Stellardd50343d2013-04-04 16:21:06 -04002534 /* XXX: We may be able to emit some of these values directly rather than
2535 * extracting fields to be emitted later.
2536 */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002537
Tom Stellard2397a722014-12-10 09:13:59 -05002538 for (i = 0; i < shader->binary.config_size_per_symbol; i+= 8) {
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002539 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
2540 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
Tom Stellardd50343d2013-04-04 16:21:06 -04002541 switch (reg) {
2542 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2543 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2544 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2545 case R_00B848_COMPUTE_PGM_RSRC1:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002546 shader->num_sgprs = MAX2(shader->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
2547 shader->num_vgprs = MAX2(shader->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
Tom Stellardd50343d2013-04-04 16:21:06 -04002548 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002549 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002550 shader->lds_size = MAX2(shader->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002551 break;
2552 case R_00B84C_COMPUTE_PGM_RSRC2:
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002553 shader->lds_size = MAX2(shader->lds_size, G_00B84C_LDS_SIZE(value));
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002554 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002555 case R_0286CC_SPI_PS_INPUT_ENA:
2556 shader->spi_ps_input_ena = value;
2557 break;
Tom Stellard2397a722014-12-10 09:13:59 -05002558 case R_0286E8_SPI_TMPRING_SIZE:
Tom Stellardb0f78032014-07-18 14:45:18 -04002559 case R_00B860_COMPUTE_TMPRING_SIZE:
2560 /* WAVESIZE is in units of 256 dwords. */
2561 shader->scratch_bytes_per_wave =
2562 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2563 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002564 default:
2565 fprintf(stderr, "Warning: Compiler emitted unknown "
2566 "config register: 0x%x\n", reg);
2567 break;
2568 }
2569 }
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002570}
2571
Tom Stellard2397a722014-12-10 09:13:59 -05002572void si_shader_apply_scratch_relocs(struct si_context *sctx,
2573 struct si_shader *shader,
2574 uint64_t scratch_va)
2575{
2576 unsigned i;
2577 uint32_t scratch_rsrc_dword0 = scratch_va & 0xffffffff;
2578 uint32_t scratch_rsrc_dword1 =
2579 S_008F04_BASE_ADDRESS_HI(scratch_va >> 32)
2580 | S_008F04_STRIDE(shader->scratch_bytes_per_wave / 64);
2581
2582 for (i = 0 ; i < shader->binary.reloc_count; i++) {
2583 const struct radeon_shader_reloc *reloc =
2584 &shader->binary.relocs[i];
2585 if (!strcmp(scratch_rsrc_dword0_symbol, reloc->name)) {
2586 util_memcpy_cpu_to_le32(shader->binary.code + reloc->offset,
2587 &scratch_rsrc_dword0, 4);
2588 } else if (!strcmp(scratch_rsrc_dword1_symbol, reloc->name)) {
2589 util_memcpy_cpu_to_le32(shader->binary.code + reloc->offset,
2590 &scratch_rsrc_dword1, 4);
2591 }
2592 }
2593}
2594
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002595int si_shader_binary_read(struct si_screen *sscreen,
2596 struct si_shader *shader,
2597 const struct radeon_shader_binary *binary)
2598{
2599
2600 unsigned i;
2601 unsigned code_size;
2602 unsigned char *ptr;
2603 bool dump = r600_can_dump_shader(&sscreen->b,
2604 shader->selector ? shader->selector->tokens : NULL);
2605
2606 if (dump && !binary->disassembled) {
2607 fprintf(stderr, "SI CODE:\n");
2608 for (i = 0; i < binary->code_size; i+=4 ) {
2609 fprintf(stderr, "@0x%x: %02x%02x%02x%02x\n", i, binary->code[i + 3],
2610 binary->code[i + 2], binary->code[i + 1],
2611 binary->code[i]);
2612 }
2613 }
2614
Tom Stellard2397a722014-12-10 09:13:59 -05002615 si_shader_binary_read_config(sscreen, shader, 0);
Tom Stellard302f53d2012-10-25 13:50:10 -04002616
2617 /* copy new shader */
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002618 code_size = binary->code_size + binary->rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002619 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002620 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002621 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002622 if (shader->bo == NULL) {
2623 return -ENOMEM;
2624 }
2625
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002626
2627 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_READ_WRITE);
2628 util_memcpy_cpu_to_le32(ptr, binary->code, binary->code_size);
2629 if (binary->rodata_size > 0) {
2630 ptr += binary->code_size;
2631 util_memcpy_cpu_to_le32(ptr, binary->rodata, binary->rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002632 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002633
Marek Olšák1abb1a92014-09-17 22:44:22 +02002634 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002635
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002636 return 0;
2637}
Tom Stellard302f53d2012-10-25 13:50:10 -04002638
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002639int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2640 LLVMModuleRef mod)
2641{
2642 int r = 0;
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002643 bool dump = r600_can_dump_shader(&sscreen->b,
2644 shader->selector ? shader->selector->tokens : NULL);
Tom Stellard2397a722014-12-10 09:13:59 -05002645 r = radeon_llvm_compile(mod, &shader->binary,
Tom Stellard761e36b2014-10-15 12:24:30 -04002646 r600_get_llvm_processor_name(sscreen->b.family), dump, sscreen->tm);
Tom Stellard1f4e48d2014-09-25 18:11:24 -07002647
2648 if (r) {
2649 return r;
2650 }
Tom Stellard2397a722014-12-10 09:13:59 -05002651 r = si_shader_binary_read(sscreen, shader, &shader->binary);
2652
2653 FREE(shader->binary.config);
2654 FREE(shader->binary.rodata);
2655 FREE(shader->binary.global_symbol_offsets);
2656 if (shader->scratch_bytes_per_wave == 0) {
2657 FREE(shader->binary.code);
2658 FREE(shader->binary.relocs);
2659 memset(&shader->binary, 0, sizeof(shader->binary));
2660 }
Darren Powellbc866902014-03-31 18:00:28 -04002661 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002662}
2663
Michel Dänzer404b29d2013-11-21 16:45:28 +09002664/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002665static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002666 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002667 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002668{
2669 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2670 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2671 struct lp_build_context *base = &bld_base->base;
2672 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002673 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002674 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002675 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002676 LLVMValueRef args[9];
2677 int i, r;
2678
Marek Olšák02134cf2014-10-04 22:07:50 +02002679 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002680
2681 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002682 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002683
2684 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2685
2686 create_meta_data(si_shader_ctx);
2687 create_function(si_shader_ctx);
2688 preload_streamout_buffers(si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002689 preload_ring_buffers(si_shader_ctx);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002690
Marek Olšákfc3b3352014-10-05 13:33:40 +02002691 args[0] = si_shader_ctx->gsvs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002692 args[1] = lp_build_mul_imm(uint,
2693 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2694 si_shader_ctx->param_vertex_id),
2695 4);
2696 args[3] = uint->zero;
2697 args[4] = uint->one; /* OFFEN */
2698 args[5] = uint->zero; /* IDXEN */
2699 args[6] = uint->one; /* GLC */
2700 args[7] = uint->one; /* SLC */
2701 args[8] = uint->zero; /* TFE */
2702
2703 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002704 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002705 unsigned chan;
2706
Marek Olšák02134cf2014-10-04 22:07:50 +02002707 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002708 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002709
2710 for (chan = 0; chan < 4; chan++) {
2711 args[2] = lp_build_const_int32(gallivm,
2712 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002713 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002714
2715 outputs[i].values[chan] =
2716 LLVMBuildBitCast(gallivm->builder,
2717 build_intrinsic(gallivm->builder,
2718 "llvm.SI.buffer.load.dword.i32.i32",
2719 LLVMInt32TypeInContext(gallivm->context),
2720 args, 9,
2721 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2722 base->elem_type, "");
2723 }
2724 }
2725
Marek Olšák02134cf2014-10-04 22:07:50 +02002726 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002727
2728 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2729
2730 if (dump)
2731 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2732
Marek Olšák1abb1a92014-09-17 22:44:22 +02002733 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002734 bld_base->base.gallivm->module);
2735
2736 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2737
2738 FREE(outputs);
2739 return r;
2740}
2741
Marek Olšák1abb1a92014-09-17 22:44:22 +02002742int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002743{
Marek Olšák2774abd2014-09-16 18:45:33 +02002744 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002745 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002746 struct lp_build_tgsi_context * bld_base;
2747 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002748 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002749 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002750
2751 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2752 * conversion fails. */
2753 if (dump) {
2754 tgsi_dump(sel->tokens, 0);
2755 si_dump_streamout(&sel->so);
2756 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002757
Marek Olšák8c37c162014-09-16 18:40:07 +02002758 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002759
Michel Dänzercfebaf92012-08-31 19:04:08 +02002760 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002761 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2762 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2763
Marek Olšák1829f9c2015-01-04 17:08:57 +01002764 if (sel->type != PIPE_SHADER_COMPUTE)
2765 shader->dx10_clamp_mode = true;
2766
Marek Olšák52335682014-09-30 15:12:09 +02002767 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002768 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2769
Marek Olšák52335682014-09-30 15:12:09 +02002770 shader->uses_instanceid = sel->info.uses_instanceid;
2771 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002772 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002773
2774 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002775 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002776 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2777 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002778 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002779 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2780 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2781 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002782 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002783 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002784 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2785 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002786
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002787 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2788 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002789
Michel Dänzer404b29d2013-11-21 16:45:28 +09002790 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2791 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2792
Marek Olšák645b4712014-11-20 22:16:09 +01002793 if (HAVE_LLVM >= 0x0306) {
2794 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
2795 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
2796 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
2797 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
2798 }
2799
Christian Könige4ed5872013-03-21 18:02:52 +01002800 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Tom Stellarda75c6162012-01-06 17:38:37 -05002801 si_shader_ctx.shader = shader;
Marek Olšákafe1e6a2015-01-31 17:31:23 +01002802 si_shader_ctx.type = tgsi_get_processor_type(sel->tokens);
Marek Olšák368b0a72014-12-28 21:51:35 +01002803 si_shader_ctx.screen = sscreen;
Tom Stellarda75c6162012-01-06 17:38:37 -05002804
Michel Dänzer51f89a02013-12-09 15:33:53 +09002805 switch (si_shader_ctx.type) {
2806 case TGSI_PROCESSOR_VERTEX:
2807 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002808 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002809 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2810 } else {
2811 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2812 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002813 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002814 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002815 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2816 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002817 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002818 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002819 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2820 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002821
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002822 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002823 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2824 shader->db_shader_control |=
2825 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2826 break;
2827 case TGSI_FS_DEPTH_LAYOUT_LESS:
2828 shader->db_shader_control |=
2829 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2830 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002831 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002832 break;
2833 default:
2834 assert(!"Unsupported shader type");
2835 return -1;
2836 }
2837
Christian König206f0592013-03-20 14:37:21 +01002838 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002839 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002840 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002841 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002842 preload_streamout_buffers(&si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002843 preload_ring_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002844
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002845 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2846 si_shader_ctx.gs_next_vertex =
2847 lp_build_alloca(bld_base->base.gallivm,
2848 bld_base->uint_bld.elem_type, "");
2849 }
2850
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002851 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002852 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002853 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002854 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002855
2856 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2857
2858 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002859 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002860 if (r) {
2861 fprintf(stderr, "LLVM failed to compile shader\n");
2862 goto out;
2863 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002864
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002865 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002866
2867 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002868 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002869 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002870 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002871 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002872 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2873 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002874 free(shader->gs_copy_shader);
2875 shader->gs_copy_shader = NULL;
2876 goto out;
2877 }
2878 }
2879
Michel Dänzer404b29d2013-11-21 16:45:28 +09002880out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002881 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002882 FREE(si_shader_ctx.constants[i]);
Tom Stellarda75c6162012-01-06 17:38:37 -05002883
Tom Stellard302f53d2012-10-25 13:50:10 -04002884 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002885}
2886
Marek Olšák2774abd2014-09-16 18:45:33 +02002887void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002888{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002889 if (shader->gs_copy_shader)
2890 si_shader_destroy(ctx, shader->gs_copy_shader);
2891
Tom Stellard5dcd97f2015-01-27 16:35:21 +00002892 if (shader->scratch_bo)
2893 r600_resource_reference(&shader->scratch_bo, NULL);
2894
Marek Olšáka81c3e02013-08-14 01:04:39 +02002895 r600_resource_reference(&shader->bo, NULL);
Tom Stellard2397a722014-12-10 09:13:59 -05002896
2897 FREE(shader->binary.code);
2898 FREE(shader->binary.relocs);
Tom Stellarda75c6162012-01-06 17:38:37 -05002899}