blob: f8e9fbe7bde8dc8e31a7f65ca433cf7a2f6a510b [file] [log] [blame]
Christian Königce40e472012-08-02 12:14:59 +02001/*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
27 */
28
Tom Stellarda75c6162012-01-06 17:38:37 -050029#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020030#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050031#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010032#include "gallivm/lp_bld_logic.h"
Christian König5e616cf2013-03-07 11:58:56 +010033#include "gallivm/lp_bld_arit.h"
Marek Olšák8d03d922013-09-01 23:59:06 +020034#include "gallivm/lp_bld_flow.h"
Emil Velikova1312632014-08-16 17:58:25 +010035#include "radeon/radeon_llvm.h"
36#include "radeon/radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010037#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010039#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_dump.h"
41
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010042#include "si_pipe.h"
43#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050044#include "sid.h"
45
Tom Stellarda75c6162012-01-06 17:38:37 -050046#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050047
Michel Dänzer404b29d2013-11-21 16:45:28 +090048struct si_shader_output_values
49{
50 LLVMValueRef values[4];
51 unsigned name;
Michel Dänzer67e385b2014-01-08 17:48:21 +090052 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090053};
54
Tom Stellarda75c6162012-01-06 17:38:37 -050055struct si_shader_context
56{
57 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050058 struct tgsi_parse_context parse;
59 struct tgsi_token * tokens;
Marek Olšák8c37c162014-09-16 18:40:07 +020060 struct si_shader *shader;
Tom Stellarda75c6162012-01-06 17:38:37 -050061 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020062 int param_streamout_config;
63 int param_streamout_write_index;
64 int param_streamout_offset[4];
65 int param_vertex_id;
66 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010067 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020068 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020069 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020070 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Marek Olšák55a9b772014-10-05 12:38:54 +020071 LLVMValueRef resources[SI_NUM_SAMPLER_VIEWS];
72 LLVMValueRef samplers[SI_NUM_SAMPLER_STATES];
Marek Olšák8d03d922013-09-01 23:59:06 +020073 LLVMValueRef so_buffers[4];
Marek Olšákfc3b3352014-10-05 13:33:40 +020074 LLVMValueRef esgs_ring;
75 LLVMValueRef gsvs_ring;
Michel Dänzerf07a96d2014-01-08 18:45:10 +090076 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050077};
78
79static struct si_shader_context * si_shader_context(
80 struct lp_build_tgsi_context * bld_base)
81{
82 return (struct si_shader_context *)bld_base;
83}
84
85
86#define PERSPECTIVE_BASE 0
87#define LINEAR_BASE 9
88
89#define SAMPLE_OFFSET 0
90#define CENTER_OFFSET 2
91#define CENTROID_OFSET 4
92
93#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040094#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020095#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040096#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050097
Michel Dänzer404b29d2013-11-21 16:45:28 +090098
99#define SENDMSG_GS 2
100#define SENDMSG_GS_DONE 3
101
102#define SENDMSG_GS_OP_NOP (0 << 4)
103#define SENDMSG_GS_OP_CUT (1 << 4)
104#define SENDMSG_GS_OP_EMIT (2 << 4)
105#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
106
Marek Olšáke29353f2014-09-17 22:17:02 +0200107/**
108 * Returns a unique index for a semantic name and index. The index must be
109 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
110 * calculated.
111 */
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200112unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
Marek Olšáke29353f2014-09-17 22:17:02 +0200113{
114 switch (semantic_name) {
115 case TGSI_SEMANTIC_POSITION:
116 return 0;
117 case TGSI_SEMANTIC_PSIZE:
118 return 1;
119 case TGSI_SEMANTIC_CLIPDIST:
120 assert(index <= 1);
121 return 2 + index;
122 case TGSI_SEMANTIC_CLIPVERTEX:
123 return 4;
124 case TGSI_SEMANTIC_COLOR:
125 assert(index <= 1);
126 return 5 + index;
127 case TGSI_SEMANTIC_BCOLOR:
128 assert(index <= 1);
129 return 7 + index;
130 case TGSI_SEMANTIC_FOG:
131 return 9;
132 case TGSI_SEMANTIC_EDGEFLAG:
133 return 10;
134 case TGSI_SEMANTIC_GENERIC:
135 assert(index <= 63-11);
136 return 11 + index;
137 default:
138 assert(0);
139 return 63;
140 }
141}
142
143/**
144 * Given a semantic name and index of a parameter and a mask of used parameters
145 * (inputs or outputs), return the index of the parameter in the list of all
146 * used parameters.
147 *
148 * For example, assume this list of parameters:
149 * POSITION, PSIZE, GENERIC0, GENERIC2
150 * which has the mask:
151 * 11000000000101
152 * Then:
153 * querying POSITION returns 0,
154 * querying PSIZE returns 1,
155 * querying GENERIC0 returns 2,
156 * querying GENERIC2 returns 3.
157 *
158 * Which can be used as an offset to a parameter buffer in units of vec4s.
159 */
160static int get_param_index(unsigned semantic_name, unsigned index,
161 uint64_t mask)
162{
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200163 unsigned unique_index = si_shader_io_get_unique_index(semantic_name, index);
Marek Olšáke29353f2014-09-17 22:17:02 +0200164 int i, param_index = 0;
165
166 /* If not present... */
167 if (!((1llu << unique_index) & mask))
168 return -1;
169
170 for (i = 0; mask; i++) {
171 uint64_t bit = 1llu << i;
172
173 if (bit & mask) {
174 if (i == unique_index)
175 return param_index;
176
177 mask &= ~bit;
178 param_index++;
179 }
180 }
181
182 assert(!"unreachable");
183 return -1;
184}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900185
Tom Stellard467f5162012-05-16 15:15:35 -0400186/**
Marek Olšákd7876082014-09-26 23:06:32 +0200187 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
188 * It's equivalent to doing a load from &base_ptr[index].
Tom Stellard467f5162012-05-16 15:15:35 -0400189 *
Marek Olšákd7876082014-09-26 23:06:32 +0200190 * \param base_ptr Where the array starts.
191 * \param index The element index into the array.
Tom Stellard467f5162012-05-16 15:15:35 -0400192 */
Marek Olšákd7876082014-09-26 23:06:32 +0200193static LLVMValueRef build_indexed_load(struct si_shader_context *si_shader_ctx,
194 LLVMValueRef base_ptr, LLVMValueRef index)
Tom Stellard467f5162012-05-16 15:15:35 -0400195{
Marek Olšákd7876082014-09-26 23:06:32 +0200196 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
197 struct gallivm_state *gallivm = bld_base->base.gallivm;
198 LLVMValueRef indices[2], pointer;
Tom Stellard467f5162012-05-16 15:15:35 -0400199
Marek Olšákd7876082014-09-26 23:06:32 +0200200 indices[0] = bld_base->uint_bld.zero;
201 indices[1] = index;
Christian König206f0592013-03-20 14:37:21 +0100202
Marek Olšákd7876082014-09-26 23:06:32 +0200203 pointer = LLVMBuildGEP(gallivm->builder, base_ptr, indices, 2, "");
204 return LLVMBuildLoad(gallivm->builder, pointer, "");
205}
206
207/**
208 * Do a load from &base_ptr[index], but also add a flag that it's loading
209 * a constant.
210 */
211static LLVMValueRef build_indexed_load_const(
212 struct si_shader_context * si_shader_ctx,
213 LLVMValueRef base_ptr, LLVMValueRef index)
214{
215 LLVMValueRef result = build_indexed_load(si_shader_ctx, base_ptr, index);
Christian König206f0592013-03-20 14:37:21 +0100216 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
217 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400218}
219
Marek Olšákf317ce52013-09-05 15:39:57 +0200220static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100221 struct radeon_llvm_context * radeon_bld,
222 unsigned divisor)
223{
Marek Olšák8d03d922013-09-01 23:59:06 +0200224 struct si_shader_context *si_shader_ctx =
225 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100226 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
227
Marek Olšák8d03d922013-09-01 23:59:06 +0200228 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
229 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100230 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
231 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
232
233 if (divisor > 1)
234 result = LLVMBuildUDiv(gallivm->builder, result,
235 lp_build_const_int32(gallivm, divisor), "");
236
237 return result;
238}
239
Tom Stellarda75c6162012-01-06 17:38:37 -0500240static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900241 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500242 unsigned input_index,
243 const struct tgsi_full_declaration *decl)
244{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900245 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
246 struct gallivm_state *gallivm = base->gallivm;
247 struct si_shader_context *si_shader_ctx =
248 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100249 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
250
251 unsigned chan;
252
Tom Stellarda75c6162012-01-06 17:38:37 -0500253 LLVMValueRef t_list_ptr;
254 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400255 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500256 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100257 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400258 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500259 LLVMTypeRef vec4_type;
260 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500261
Tom Stellard467f5162012-05-16 15:15:35 -0400262 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100263 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500264
Michel Dänzer51f89a02013-12-09 15:33:53 +0900265 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400266
Marek Olšákd7876082014-09-26 23:06:32 +0200267 t_list = build_indexed_load_const(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400268
269 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900270 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500271
Christian Königa0dca442013-03-22 15:59:22 +0100272 if (divisor) {
273 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200274 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200275 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100276 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200277 /* Load the buffer index for vertices. */
278 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
279 si_shader_ctx->param_vertex_id);
280 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
281 SI_PARAM_BASE_VERTEX);
282 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100283 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500284
285 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400286 args[0] = t_list;
287 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100288 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900289 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100290 "llvm.SI.vs.load.input", vec4_type, args, 3,
291 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500292
293 /* Break up the vec4 into individual components */
294 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900295 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500296 /* XXX: Use a helper function for this. There is one in
297 * tgsi_llvm.c. */
298 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900299 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500300 input, llvm_chan, "");
301 }
302}
303
Michel Dänzer404b29d2013-11-21 16:45:28 +0900304static LLVMValueRef fetch_input_gs(
305 struct lp_build_tgsi_context *bld_base,
306 const struct tgsi_full_src_register *reg,
307 enum tgsi_opcode_type type,
308 unsigned swizzle)
309{
310 struct lp_build_context *base = &bld_base->base;
311 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200312 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900313 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
314 struct gallivm_state *gallivm = base->gallivm;
315 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
316 LLVMValueRef vtx_offset;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900317 LLVMValueRef args[9];
318 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200319 struct tgsi_shader_info *info = &shader->selector->info;
320 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
321 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900322
Marek Olšáke23fec12014-10-04 17:40:39 +0200323 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900324 if (swizzle == 0)
325 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
326 SI_PARAM_PRIMITIVE_ID);
327 else
328 return uint->zero;
329 }
330
Michel Dänzer404b29d2013-11-21 16:45:28 +0900331 if (!reg->Register.Dimension)
332 return NULL;
333
334 if (swizzle == ~0) {
335 LLVMValueRef values[TGSI_NUM_CHANNELS];
336 unsigned chan;
337 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
338 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
339 }
340 return lp_build_gather_values(bld_base->base.gallivm, values,
341 TGSI_NUM_CHANNELS);
342 }
343
344 /* Get the vertex offset parameter */
345 vtx_offset_param = reg->Dimension.Index;
346 if (vtx_offset_param < 2) {
347 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
348 } else {
349 assert(vtx_offset_param < 6);
350 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
351 }
352 vtx_offset = lp_build_mul_imm(uint,
353 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
354 vtx_offset_param),
355 4);
356
Marek Olšákfc3b3352014-10-05 13:33:40 +0200357 args[0] = si_shader_ctx->esgs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900358 args[1] = vtx_offset;
359 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200360 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200361 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900362 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900363 args[3] = uint->zero;
364 args[4] = uint->one; /* OFFEN */
365 args[5] = uint->zero; /* IDXEN */
366 args[6] = uint->one; /* GLC */
367 args[7] = uint->zero; /* SLC */
368 args[8] = uint->zero; /* TFE */
369
370 return LLVMBuildBitCast(gallivm->builder,
371 build_intrinsic(gallivm->builder,
372 "llvm.SI.buffer.load.dword.i32.i32",
373 i32, args, 9,
374 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
375 tgsi2llvmtype(bld_base, type), "");
376}
377
Tom Stellarda75c6162012-01-06 17:38:37 -0500378static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900379 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500380 unsigned input_index,
381 const struct tgsi_full_declaration *decl)
382{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900383 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
384 struct si_shader_context *si_shader_ctx =
385 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200386 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900387 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
388 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400389 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900390 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100391
392 LLVMValueRef interp_param;
393 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500394
395 /* This value is:
396 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
397 * quad begins a new primitive. Bit 0 always needs
398 * to be unset)
399 * [32:16] ParamOffset
400 *
401 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900402 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200403 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500404
Christian König0666ffd2013-03-05 15:07:39 +0100405 unsigned chan;
406
Tom Stellard0fb1e682012-09-06 16:18:11 -0400407 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
408 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400409 unsigned soa_index =
410 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900411 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100412 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100413
414 if (chan == 3)
415 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900416 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100417 LLVMBuildFDiv(gallivm->builder,
418 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900419 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100420 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400421 }
422 return;
423 }
424
Michel Dänzer97078b12012-09-25 12:41:31 +0200425 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900426 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Marek Olšáke827bb62014-10-16 16:20:26 +0200427 LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900428 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
429 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200430 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900431 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200432 lp_build_const_float(gallivm, 1.0f);
433
434 return;
435 }
436
Marek Olšák8b057dd2014-10-04 22:15:07 +0200437 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200438 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200439 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200440
Francisco Jerez12799232012-04-30 18:27:52 +0200441 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500442 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100443 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500444 break;
445 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200446 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200447 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400448 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100449 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200450 else
Christian König0666ffd2013-03-05 15:07:39 +0100451 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200452 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200453 case TGSI_INTERPOLATE_COLOR:
454 if (si_shader_ctx->shader->key.ps.flatshade) {
455 interp_param = 0;
456 break;
457 }
458 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200459 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200460 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200461 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400462 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100463 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200464 else
Christian König0666ffd2013-03-05 15:07:39 +0100465 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500466 break;
467 default:
468 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
469 return;
470 }
471
Christian König0666ffd2013-03-05 15:07:39 +0100472 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
473
Tom Stellarda75c6162012-01-06 17:38:37 -0500474 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200475 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100476 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100477 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200478 LLVMValueRef face, is_face_positive;
479 LLVMValueRef back_attr_number =
480 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200481 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200482
Christian König0666ffd2013-03-05 15:07:39 +0100483 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
484
Michel Dänzer691f08d2012-09-06 18:03:38 +0200485 is_face_positive = LLVMBuildFCmp(gallivm->builder,
486 LLVMRealUGT, face,
487 lp_build_const_float(gallivm, 0.0f),
488 "");
489
Tom Stellarda75c6162012-01-06 17:38:37 -0500490 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100491 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200492 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
493 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
494 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
495 LLVMValueRef front, back;
496
497 args[0] = llvm_chan;
498 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900499 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100500 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100501 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200502
503 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900504 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100505 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100506 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200507
Michel Dänzer51f89a02013-12-09 15:33:53 +0900508 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200509 LLVMBuildSelect(gallivm->builder,
510 is_face_positive,
511 front,
512 back,
513 "");
514 }
515
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900516 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200517 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
518 LLVMValueRef args[4];
519
520 args[0] = uint->zero;
521 args[1] = attr_number;
522 args[2] = params;
523 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900524 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
525 build_intrinsic(gallivm->builder, intr_name,
526 input_type, args, args[3] ? 4 : 3,
527 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
528 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
529 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200530 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900531 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200532 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200533 } else {
534 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100535 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200536 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
537 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
538 args[0] = llvm_chan;
539 args[1] = attr_number;
540 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100541 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900542 radeon_bld->inputs[soa_index] =
543 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100544 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100545 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200546 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500547 }
548}
549
Marek Olšák5b06fc32014-05-06 18:12:40 +0200550static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
551{
552 struct gallivm_state *gallivm = &radeon_bld->gallivm;
553 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
554 SI_PARAM_ANCILLARY);
555 value = LLVMBuildLShr(gallivm->builder, value,
556 lp_build_const_int32(gallivm, 8), "");
557 value = LLVMBuildAnd(gallivm->builder, value,
558 lp_build_const_int32(gallivm, 0xf), "");
559 return value;
560}
561
Marek Olšákd7876082014-09-26 23:06:32 +0200562/**
563 * Load a dword from a constant buffer.
564 */
565static LLVMValueRef buffer_load_const(LLVMBuilderRef builder, LLVMValueRef resource,
566 LLVMValueRef offset, LLVMTypeRef return_type)
Marek Olšák250aa932014-05-06 14:10:47 +0200567{
568 LLVMValueRef args[2] = {resource, offset};
569
570 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
571 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
572}
573
Christian Könige4ed5872013-03-21 18:02:52 +0100574static void declare_system_value(
575 struct radeon_llvm_context * radeon_bld,
576 unsigned index,
577 const struct tgsi_full_declaration *decl)
578{
Marek Olšák8d03d922013-09-01 23:59:06 +0200579 struct si_shader_context *si_shader_ctx =
580 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200581 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
582 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100583 LLVMValueRef value = 0;
584
585 switch (decl->Semantic.Name) {
586 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200587 value = LLVMGetParam(radeon_bld->main_fn,
588 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100589 break;
590
591 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200592 value = LLVMGetParam(radeon_bld->main_fn,
593 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100594 break;
595
Marek Olšák5b06fc32014-05-06 18:12:40 +0200596 case TGSI_SEMANTIC_SAMPLEID:
597 value = get_sample_id(radeon_bld);
598 break;
599
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200600 case TGSI_SEMANTIC_SAMPLEPOS:
601 {
602 LLVMBuilderRef builder = gallivm->builder;
603 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200604 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200605 LLVMValueRef resource = build_indexed_load_const(si_shader_ctx, desc, buf_index);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200606
607 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
608 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
609 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
610
611 LLVMValueRef pos[4] = {
Marek Olšákd7876082014-09-26 23:06:32 +0200612 buffer_load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
613 buffer_load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200614 lp_build_const_float(gallivm, 0),
615 lp_build_const_float(gallivm, 0)
616 };
617 value = lp_build_gather_values(gallivm, pos, 4);
618 break;
619 }
620
Christian Könige4ed5872013-03-21 18:02:52 +0100621 default:
622 assert(!"unknown system value");
623 return;
624 }
625
626 radeon_bld->system_values[index] = value;
627}
628
Tom Stellarda75c6162012-01-06 17:38:37 -0500629static LLVMValueRef fetch_constant(
630 struct lp_build_tgsi_context * bld_base,
631 const struct tgsi_full_src_register *reg,
632 enum tgsi_opcode_type type,
633 unsigned swizzle)
634{
Christian König55fe5cc2013-03-04 16:30:06 +0100635 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500636 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100637 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200638 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500639
Christian König0f6cf2b2013-03-15 15:53:25 +0100640 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100641 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500642
Christian König8514f5a2013-02-04 17:46:42 +0100643 if (swizzle == LP_CHAN_ALL) {
644 unsigned chan;
645 LLVMValueRef values[4];
646 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
647 values[chan] = fetch_constant(bld_base, reg, type, chan);
648
649 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
650 }
651
Marek Olšák2fd42002013-10-25 11:45:47 +0200652 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100653 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100654
Marek Olšák2fd42002013-10-25 11:45:47 +0200655 if (!reg->Register.Indirect)
656 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
657
Christian König0f6cf2b2013-03-15 15:53:25 +0100658 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
659 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
660 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200661 addr = lp_build_add(&bld_base->uint_bld, addr,
662 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200663
Marek Olšákd7876082014-09-26 23:06:32 +0200664 result = buffer_load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
Marek Olšák250aa932014-05-06 14:10:47 +0200665 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500666
Christian Königf5298b02013-02-28 14:50:07 +0100667 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500668}
669
Michel Dänzer26c71392012-08-24 12:03:11 +0200670/* Initialize arguments for the shader export intrinsic */
671static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900672 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200673 unsigned target,
674 LLVMValueRef *args)
675{
676 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
677 struct lp_build_context *uint =
678 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
679 struct lp_build_context *base = &bld_base->base;
680 unsigned compressed = 0;
681 unsigned chan;
682
Michel Dänzerf402acd2012-08-22 18:15:36 +0200683 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
684 int cbuf = target - V_008DFC_SQ_EXP_MRT;
685
686 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100687 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100688
689 if (compressed)
690 si_shader_ctx->shader->spi_shader_col_format |=
691 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
692 else
693 si_shader_ctx->shader->spi_shader_col_format |=
694 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200695
696 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200697 }
698 }
699
700 if (compressed) {
701 /* Pixel shader needs to pack output values before export */
702 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900703 args[0] = values[2 * chan];
704 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200705 args[chan + 5] =
706 build_intrinsic(base->gallivm->builder,
707 "llvm.SI.packf16",
708 LLVMInt32TypeInContext(base->gallivm->context),
709 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100710 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100711 args[chan + 7] = args[chan + 5] =
712 LLVMBuildBitCast(base->gallivm->builder,
713 args[chan + 5],
714 LLVMFloatTypeInContext(base->gallivm->context),
715 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200716 }
717
718 /* Set COMPR flag */
719 args[4] = uint->one;
720 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900721 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200722 /* +5 because the first output value will be
723 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900724 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200725
726 /* Clear COMPR flag */
727 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200728 }
729
730 /* XXX: This controls which components of the output
731 * registers actually get exported. (e.g bit 0 means export
732 * X component, bit 1 means export Y component, etc.) I'm
733 * hard coding this to 0xf for now. In the future, we might
734 * want to do something else. */
735 args[0] = lp_build_const_int32(base->gallivm, 0xf);
736
737 /* Specify whether the EXEC mask represents the valid mask */
738 args[1] = uint->zero;
739
740 /* Specify whether this is the last export */
741 args[2] = uint->zero;
742
743 /* Specify the target we are exporting */
744 args[3] = lp_build_const_int32(base->gallivm, target);
745
Michel Dänzer26c71392012-08-24 12:03:11 +0200746 /* XXX: We probably need to keep track of the output
747 * values, so we know what we are passing to the next
748 * stage. */
749}
750
Michel Dänzer404b29d2013-11-21 16:45:28 +0900751/* Load from output pointers and initialize arguments for the shader export intrinsic */
752static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
753 LLVMValueRef *out_ptr,
754 unsigned target,
755 LLVMValueRef *args)
756{
757 struct gallivm_state *gallivm = bld_base->base.gallivm;
758 LLVMValueRef values[4];
759 int i;
760
761 for (i = 0; i < 4; i++)
762 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
763
764 si_llvm_init_export_args(bld_base, values, target, args);
765}
766
Michel Dänzer7708a862012-11-02 15:57:30 +0100767static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900768 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100769{
770 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
771 struct gallivm_state *gallivm = bld_base->base.gallivm;
772
Christian Königa0dca442013-03-22 15:59:22 +0100773 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400774 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
775 SI_PARAM_ALPHA_REF);
776
Michel Dänzer7708a862012-11-02 15:57:30 +0100777 LLVMValueRef alpha_pass =
778 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100779 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900780 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400781 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100782 LLVMValueRef arg =
783 lp_build_select(&bld_base->base,
784 alpha_pass,
785 lp_build_const_float(gallivm, 1.0f),
786 lp_build_const_float(gallivm, -1.0f));
787
788 build_intrinsic(gallivm->builder,
789 "llvm.AMDGPU.kill",
790 LLVMVoidTypeInContext(gallivm->context),
791 &arg, 1, 0);
792 } else {
793 build_intrinsic(gallivm->builder,
794 "llvm.AMDGPU.kilp",
795 LLVMVoidTypeInContext(gallivm->context),
796 NULL, 0, 0);
797 }
Marek Olšákadc57972014-09-19 17:07:07 +0200798
799 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100800}
801
Michel Dänzere3befbc2013-05-15 18:09:50 +0200802static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900803 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200804{
805 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200806 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200807 struct lp_build_context *base = &bld_base->base;
808 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200809 unsigned reg_index;
810 unsigned chan;
811 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200812 LLVMValueRef base_elt;
813 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200814 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšákd7876082014-09-26 23:06:32 +0200815 LLVMValueRef const_resource = build_indexed_load_const(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200816
Michel Dänzere3befbc2013-05-15 18:09:50 +0200817 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200818 LLVMValueRef *args = pos[2 + reg_index];
819
Marek Olšák8c37c162014-09-16 18:40:07 +0200820 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200821
Michel Dänzere3befbc2013-05-15 18:09:50 +0200822 args[5] =
823 args[6] =
824 args[7] =
825 args[8] = lp_build_const_float(base->gallivm, 0.0f);
826
827 /* Compute dot products of position and user clip plane vectors */
828 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
829 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200830 args[1] = lp_build_const_int32(base->gallivm,
831 ((reg_index * 4 + chan) * 4 +
832 const_chan) * 4);
Marek Olšákd7876082014-09-26 23:06:32 +0200833 base_elt = buffer_load_const(base->gallivm->builder, const_resource,
Marek Olšák250aa932014-05-06 14:10:47 +0200834 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200835 args[5 + chan] =
836 lp_build_add(base, args[5 + chan],
837 lp_build_mul(base, base_elt,
838 out_elts[const_chan]));
839 }
840 }
841
842 args[0] = lp_build_const_int32(base->gallivm, 0xf);
843 args[1] = uint->zero;
844 args[2] = uint->zero;
845 args[3] = lp_build_const_int32(base->gallivm,
846 V_008DFC_SQ_EXP_POS + 2 + reg_index);
847 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200848 }
849}
850
Marek Olšák8d03d922013-09-01 23:59:06 +0200851static void si_dump_streamout(struct pipe_stream_output_info *so)
852{
853 unsigned i;
854
855 if (so->num_outputs)
856 fprintf(stderr, "STREAMOUT\n");
857
858 for (i = 0; i < so->num_outputs; i++) {
859 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
860 so->output[i].start_component;
861 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
862 i, so->output[i].output_buffer,
863 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
864 so->output[i].register_index,
865 mask & 1 ? "x" : "",
866 mask & 2 ? "y" : "",
867 mask & 4 ? "z" : "",
868 mask & 8 ? "w" : "");
869 }
870}
871
872/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
873 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
874 * or v4i32 (num_channels=3,4). */
875static void build_tbuffer_store(struct si_shader_context *shader,
876 LLVMValueRef rsrc,
877 LLVMValueRef vdata,
878 unsigned num_channels,
879 LLVMValueRef vaddr,
880 LLVMValueRef soffset,
881 unsigned inst_offset,
882 unsigned dfmt,
883 unsigned nfmt,
884 unsigned offen,
885 unsigned idxen,
886 unsigned glc,
887 unsigned slc,
888 unsigned tfe)
889{
890 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
891 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
892 LLVMValueRef args[] = {
893 rsrc,
894 vdata,
895 LLVMConstInt(i32, num_channels, 0),
896 vaddr,
897 soffset,
898 LLVMConstInt(i32, inst_offset, 0),
899 LLVMConstInt(i32, dfmt, 0),
900 LLVMConstInt(i32, nfmt, 0),
901 LLVMConstInt(i32, offen, 0),
902 LLVMConstInt(i32, idxen, 0),
903 LLVMConstInt(i32, glc, 0),
904 LLVMConstInt(i32, slc, 0),
905 LLVMConstInt(i32, tfe, 0)
906 };
907
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900908 /* The instruction offset field has 12 bits */
909 assert(offen || inst_offset < (1 << 12));
910
Marek Olšák8d03d922013-09-01 23:59:06 +0200911 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
912 unsigned func = CLAMP(num_channels, 1, 3) - 1;
913 const char *types[] = {"i32", "v2i32", "v4i32"};
914 char name[256];
915 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
916
917 lp_build_intrinsic(gallivm->builder, name,
918 LLVMVoidTypeInContext(gallivm->context),
919 args, Elements(args));
920}
921
922static void build_streamout_store(struct si_shader_context *shader,
923 LLVMValueRef rsrc,
924 LLVMValueRef vdata,
925 unsigned num_channels,
926 LLVMValueRef vaddr,
927 LLVMValueRef soffset,
928 unsigned inst_offset)
929{
930 static unsigned dfmt[] = {
931 V_008F0C_BUF_DATA_FORMAT_32,
932 V_008F0C_BUF_DATA_FORMAT_32_32,
933 V_008F0C_BUF_DATA_FORMAT_32_32_32,
934 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
935 };
936 assert(num_channels >= 1 && num_channels <= 4);
937
938 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
939 inst_offset, dfmt[num_channels-1],
940 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
941}
942
943/* On SI, the vertex shader is responsible for writing streamout data
944 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900945static void si_llvm_emit_streamout(struct si_shader_context *shader,
946 struct si_shader_output_values *outputs,
947 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200948{
949 struct pipe_stream_output_info *so = &shader->shader->selector->so;
950 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
951 LLVMBuilderRef builder = gallivm->builder;
952 int i, j;
953 struct lp_build_if_state if_ctx;
954
955 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
956
957 LLVMValueRef so_param =
958 LLVMGetParam(shader->radeon_bld.main_fn,
959 shader->param_streamout_config);
960
961 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
962 LLVMValueRef so_vtx_count =
963 LLVMBuildAnd(builder,
964 LLVMBuildLShr(builder, so_param,
965 LLVMConstInt(i32, 16, 0), ""),
966 LLVMConstInt(i32, 127, 0), "");
967
968 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
969 NULL, 0, LLVMReadNoneAttribute);
970
971 /* can_emit = tid < so_vtx_count; */
972 LLVMValueRef can_emit =
973 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
974
975 /* Emit the streamout code conditionally. This actually avoids
976 * out-of-bounds buffer access. The hw tells us via the SGPR
977 * (so_vtx_count) which threads are allowed to emit streamout data. */
978 lp_build_if(&if_ctx, gallivm, can_emit);
979 {
980 /* The buffer offset is computed as follows:
981 * ByteOffset = streamout_offset[buffer_id]*4 +
982 * (streamout_write_index + thread_id)*stride[buffer_id] +
983 * attrib_offset
984 */
985
986 LLVMValueRef so_write_index =
987 LLVMGetParam(shader->radeon_bld.main_fn,
988 shader->param_streamout_write_index);
989
990 /* Compute (streamout_write_index + thread_id). */
991 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
992
993 /* Compute the write offset for each enabled buffer. */
994 LLVMValueRef so_write_offset[4] = {};
995 for (i = 0; i < 4; i++) {
996 if (!so->stride[i])
997 continue;
998
999 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1000 shader->param_streamout_offset[i]);
1001 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1002
1003 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1004 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1005 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1006 }
1007
Marek Olšák8d03d922013-09-01 23:59:06 +02001008 /* Write streamout data. */
1009 for (i = 0; i < so->num_outputs; i++) {
1010 unsigned buf_idx = so->output[i].output_buffer;
1011 unsigned reg = so->output[i].register_index;
1012 unsigned start = so->output[i].start_component;
1013 unsigned num_comps = so->output[i].num_components;
1014 LLVMValueRef out[4];
1015
1016 assert(num_comps && num_comps <= 4);
1017 if (!num_comps || num_comps > 4)
1018 continue;
1019
Marek Olšák558f7772014-10-04 22:37:23 +02001020 if (reg >= noutput)
1021 continue;
1022
Marek Olšák8d03d922013-09-01 23:59:06 +02001023 /* Load the output as int. */
1024 for (j = 0; j < num_comps; j++) {
Marek Olšák558f7772014-10-04 22:37:23 +02001025 out[j] = LLVMBuildBitCast(builder,
1026 outputs[reg].values[start+j],
1027 i32, "");
Marek Olšák8d03d922013-09-01 23:59:06 +02001028 }
1029
1030 /* Pack the output. */
1031 LLVMValueRef vdata = NULL;
1032
1033 switch (num_comps) {
1034 case 1: /* as i32 */
1035 vdata = out[0];
1036 break;
1037 case 2: /* as v2i32 */
1038 case 3: /* as v4i32 (aligned to 4) */
1039 case 4: /* as v4i32 */
1040 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1041 for (j = 0; j < num_comps; j++) {
1042 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1043 LLVMConstInt(i32, j, 0), "");
1044 }
1045 break;
1046 }
1047
1048 build_streamout_store(shader, shader->so_buffers[buf_idx],
1049 vdata, num_comps,
1050 so_write_offset[buf_idx],
1051 LLVMConstInt(i32, 0, 0),
1052 so->output[i].dst_offset*4);
1053 }
1054 }
1055 lp_build_endif(&if_ctx);
1056}
1057
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001058
Michel Dänzer404b29d2013-11-21 16:45:28 +09001059/* Generate export instructions for hardware VS shader stage */
1060static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1061 struct si_shader_output_values *outputs,
1062 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001063{
1064 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001065 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001066 struct lp_build_context * base = &bld_base->base;
1067 struct lp_build_context * uint =
1068 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001069 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001070 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001071 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001072 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001073 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001074 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001075 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001076 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001077
Michel Dänzer67e385b2014-01-08 17:48:21 +09001078 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1079 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001080 }
1081
Michel Dänzer404b29d2013-11-21 16:45:28 +09001082 for (i = 0; i < noutput; i++) {
1083 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001084 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001085
Michel Dänzer0afeea52013-05-02 14:53:17 +02001086handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001087 /* Select the correct target */
1088 switch(semantic_name) {
1089 case TGSI_SEMANTIC_PSIZE:
1090 shader->vs_out_misc_write = true;
1091 shader->vs_out_point_size = true;
1092 psize_value = outputs[i].values[0];
1093 continue;
1094 case TGSI_SEMANTIC_EDGEFLAG:
1095 shader->vs_out_misc_write = true;
1096 shader->vs_out_edgeflag = true;
1097 edgeflag_value = outputs[i].values[0];
1098 continue;
1099 case TGSI_SEMANTIC_LAYER:
1100 shader->vs_out_misc_write = true;
1101 shader->vs_out_layer = true;
1102 layer_value = outputs[i].values[0];
1103 continue;
1104 case TGSI_SEMANTIC_POSITION:
1105 target = V_008DFC_SQ_EXP_POS;
1106 break;
1107 case TGSI_SEMANTIC_COLOR:
1108 case TGSI_SEMANTIC_BCOLOR:
1109 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001110 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001111 param_count++;
1112 break;
1113 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001114 shader->clip_dist_write |=
Marek Olšáka9592cd2014-10-04 19:09:09 +02001115 0xf << (semantic_index * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001116 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1117 break;
1118 case TGSI_SEMANTIC_CLIPVERTEX:
1119 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1120 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001121 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001122 case TGSI_SEMANTIC_FOG:
1123 case TGSI_SEMANTIC_GENERIC:
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 default:
1129 target = 0;
1130 fprintf(stderr,
1131 "Warning: SI unhandled vs output type:%d\n",
1132 semantic_name);
1133 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001134
Michel Dänzer404b29d2013-11-21 16:45:28 +09001135 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001136
Michel Dänzer404b29d2013-11-21 16:45:28 +09001137 if (target >= V_008DFC_SQ_EXP_POS &&
1138 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1139 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1140 args, sizeof(args));
1141 } else {
1142 lp_build_intrinsic(base->gallivm->builder,
1143 "llvm.SI.export",
1144 LLVMVoidTypeInContext(base->gallivm->context),
1145 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001146 }
1147
1148 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1149 semantic_name = TGSI_SEMANTIC_GENERIC;
1150 goto handle_semantic;
1151 }
1152 }
1153
1154 /* We need to add the position output manually if it's missing. */
1155 if (!pos_args[0][0]) {
1156 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1157 pos_args[0][1] = uint->zero; /* EXEC mask */
1158 pos_args[0][2] = uint->zero; /* last export? */
1159 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1160 pos_args[0][4] = uint->zero; /* COMPR flag */
1161 pos_args[0][5] = base->zero; /* X */
1162 pos_args[0][6] = base->zero; /* Y */
1163 pos_args[0][7] = base->zero; /* Z */
1164 pos_args[0][8] = base->one; /* W */
1165 }
1166
1167 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1168 if (shader->vs_out_misc_write) {
1169 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1170 shader->vs_out_point_size |
1171 (shader->vs_out_edgeflag << 1) |
1172 (shader->vs_out_layer << 2));
1173 pos_args[1][1] = uint->zero; /* EXEC mask */
1174 pos_args[1][2] = uint->zero; /* last export? */
1175 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1176 pos_args[1][4] = uint->zero; /* COMPR flag */
1177 pos_args[1][5] = base->zero; /* X */
1178 pos_args[1][6] = base->zero; /* Y */
1179 pos_args[1][7] = base->zero; /* Z */
1180 pos_args[1][8] = base->zero; /* W */
1181
Michel Dänzer404b29d2013-11-21 16:45:28 +09001182 if (shader->vs_out_point_size)
1183 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001184
1185 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001186 /* The output is a float, but the hw expects an integer
1187 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001188 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1189 edgeflag_value,
1190 bld_base->uint_bld.elem_type, "");
1191 edgeflag_value = lp_build_min(&bld_base->int_bld,
1192 edgeflag_value,
1193 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001194
1195 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001196 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1197 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001198 base->elem_type, "");
1199 }
1200
Michel Dänzer404b29d2013-11-21 16:45:28 +09001201 if (shader->vs_out_layer)
1202 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001203 }
1204
1205 for (i = 0; i < 4; i++)
1206 if (pos_args[i][0])
1207 shader->nr_pos_exports++;
1208
1209 pos_idx = 0;
1210 for (i = 0; i < 4; i++) {
1211 if (!pos_args[i][0])
1212 continue;
1213
1214 /* Specify the target we are exporting */
1215 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1216
1217 if (pos_idx == shader->nr_pos_exports)
1218 /* Specify that this is the last export */
1219 pos_args[i][2] = uint->one;
1220
1221 lp_build_intrinsic(base->gallivm->builder,
1222 "llvm.SI.export",
1223 LLVMVoidTypeInContext(base->gallivm->context),
1224 pos_args[i], 9);
1225 }
1226}
1227
Michel Dänzer404b29d2013-11-21 16:45:28 +09001228static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1229{
1230 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1231 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001232 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001233 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001234 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001235 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1236 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001237 unsigned chan;
1238 int i;
1239
Marek Olšák216cf862014-10-04 17:04:05 +02001240 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001241 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001242 si_shader_ctx->radeon_bld.soa.outputs[i];
1243 int param_index = get_param_index(info->output_semantic_name[i],
1244 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001245 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001246
Marek Olšáke29353f2014-09-17 22:17:02 +02001247 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001248 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001249
1250 for (chan = 0; chan < 4; chan++) {
1251 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001252 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1253
Marek Olšákfc3b3352014-10-05 13:33:40 +02001254 build_tbuffer_store(si_shader_ctx,
1255 si_shader_ctx->esgs_ring,
1256 out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001257 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001258 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001259 V_008F0C_BUF_DATA_FORMAT_32,
1260 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001261 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001262 }
1263 }
1264}
1265
1266static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1267{
1268 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1269 struct gallivm_state *gallivm = bld_base->base.gallivm;
1270 LLVMValueRef args[2];
1271
1272 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1273 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1274 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1275 LLVMVoidTypeInContext(gallivm->context), args, 2,
1276 LLVMNoUnwindAttribute);
1277}
1278
1279static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1280{
1281 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1282 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001283 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001284 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001285 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001286
Marek Olšákec0d1682014-10-04 22:33:36 +02001287 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001288
Marek Olšákec0d1682014-10-04 22:33:36 +02001289 for (i = 0; i < info->num_outputs; i++) {
1290 outputs[i].name = info->output_semantic_name[i];
1291 outputs[i].sid = info->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09001292
Marek Olšákec0d1682014-10-04 22:33:36 +02001293 for (j = 0; j < 4; j++)
1294 outputs[i].values[j] =
1295 LLVMBuildLoad(gallivm->builder,
1296 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1297 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001298 }
1299
Marek Olšákec0d1682014-10-04 22:33:36 +02001300 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001301 FREE(outputs);
1302}
1303
Michel Dänzer51f89a02013-12-09 15:33:53 +09001304static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1305{
1306 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001307 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001308 struct lp_build_context * base = &bld_base->base;
Marek Olšák75e97e22014-10-04 23:13:50 +02001309 struct lp_build_context * uint = &bld_base->uint_bld;
1310 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001311 LLVMValueRef args[9];
1312 LLVMValueRef last_args[9] = { 0 };
Marek Olšákd0e8b652014-05-06 20:04:31 +02001313 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Marek Olšák75e97e22014-10-04 23:13:50 +02001314 int i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001315
Marek Olšák75e97e22014-10-04 23:13:50 +02001316 for (i = 0; i < info->num_outputs; i++) {
1317 unsigned semantic_name = info->output_semantic_name[i];
1318 unsigned semantic_index = info->output_semantic_index[i];
Michel Dänzer51f89a02013-12-09 15:33:53 +09001319 unsigned target;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001320
Marek Olšák75e97e22014-10-04 23:13:50 +02001321 /* Select the correct target */
1322 switch (semantic_name) {
1323 case TGSI_SEMANTIC_POSITION:
1324 depth_index = i;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001325 continue;
Marek Olšák75e97e22014-10-04 23:13:50 +02001326 case TGSI_SEMANTIC_STENCIL:
1327 stencil_index = i;
1328 continue;
1329 case TGSI_SEMANTIC_SAMPLEMASK:
1330 samplemask_index = i;
1331 continue;
1332 case TGSI_SEMANTIC_COLOR:
1333 target = V_008DFC_SQ_EXP_MRT + semantic_index;
1334 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1335 LLVMBuildStore(bld_base->base.gallivm->builder,
1336 bld_base->base.one,
1337 si_shader_ctx->radeon_bld.soa.outputs[i][3]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001338
Marek Olšák75e97e22014-10-04 23:13:50 +02001339 if (semantic_index == 0 &&
1340 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1341 si_alpha_test(bld_base,
1342 si_shader_ctx->radeon_bld.soa.outputs[i]);
1343 break;
1344 default:
1345 target = 0;
1346 fprintf(stderr,
1347 "Warning: SI unhandled fs output type:%d\n",
1348 semantic_name);
1349 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09001350
Marek Olšák75e97e22014-10-04 23:13:50 +02001351 si_llvm_init_export_args_load(bld_base,
1352 si_shader_ctx->radeon_bld.soa.outputs[i],
1353 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001354
Marek Olšák75e97e22014-10-04 23:13:50 +02001355 if (semantic_name == TGSI_SEMANTIC_COLOR) {
1356 /* If there is an export instruction waiting to be emitted, do so now. */
1357 if (last_args[0]) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001358 lp_build_intrinsic(base->gallivm->builder,
1359 "llvm.SI.export",
1360 LLVMVoidTypeInContext(base->gallivm->context),
Marek Olšák75e97e22014-10-04 23:13:50 +02001361 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001362 }
Marek Olšák75e97e22014-10-04 23:13:50 +02001363
1364 /* This instruction will be emitted at the end of the shader. */
1365 memcpy(last_args, args, sizeof(args));
1366
1367 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1368 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1369 semantic_index == 0 &&
1370 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1371 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
1372 si_llvm_init_export_args_load(bld_base,
1373 si_shader_ctx->radeon_bld.soa.outputs[i],
1374 V_008DFC_SQ_EXP_MRT + c, args);
1375 lp_build_intrinsic(base->gallivm->builder,
1376 "llvm.SI.export",
1377 LLVMVoidTypeInContext(base->gallivm->context),
1378 args, 9);
1379 }
1380 }
1381 } else {
1382 lp_build_intrinsic(base->gallivm->builder,
1383 "llvm.SI.export",
1384 LLVMVoidTypeInContext(base->gallivm->context),
1385 args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001386 }
1387 }
1388
Marek Olšákd0e8b652014-05-06 20:04:31 +02001389 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001390 LLVMValueRef out_ptr;
1391 unsigned mask = 0;
1392
1393 /* Specify the target we are exporting */
1394 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1395
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001396 args[5] = base->zero; /* R, depth */
1397 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1398 args[7] = base->zero; /* B, sample mask */
1399 args[8] = base->zero; /* A, alpha to mask */
1400
Michel Dänzer1a616c12012-11-13 17:35:09 +01001401 if (depth_index >= 0) {
1402 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1403 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1404 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001405 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001406 }
1407
1408 if (stencil_index >= 0) {
1409 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001410 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001411 /* Only setting the stencil component bit (0x2) here
1412 * breaks some stencil piglit tests
1413 */
1414 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001415 si_shader_ctx->shader->db_shader_control |=
1416 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001417 }
1418
Marek Olšákd0e8b652014-05-06 20:04:31 +02001419 if (samplemask_index >= 0) {
1420 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1421 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1422 mask |= 0xf; /* Set all components. */
1423 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1424 }
1425
1426 if (samplemask_index >= 0)
1427 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1428 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001429 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1430 else
1431 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1432
Michel Dänzer1a616c12012-11-13 17:35:09 +01001433 /* Specify which components to enable */
1434 args[0] = lp_build_const_int32(base->gallivm, mask);
1435
1436 args[1] =
1437 args[2] =
1438 args[4] = uint->zero;
1439
1440 if (last_args[0])
1441 lp_build_intrinsic(base->gallivm->builder,
1442 "llvm.SI.export",
1443 LLVMVoidTypeInContext(base->gallivm->context),
1444 args, 9);
1445 else
1446 memcpy(last_args, args, sizeof(args));
1447 }
1448
Michel Dänzer51f89a02013-12-09 15:33:53 +09001449 if (!last_args[0]) {
1450 /* Specify which components to enable */
1451 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001452
Michel Dänzer51f89a02013-12-09 15:33:53 +09001453 /* Specify the target we are exporting */
1454 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001455
Michel Dänzer51f89a02013-12-09 15:33:53 +09001456 /* Set COMPR flag to zero to export data as 32-bit */
1457 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001458
Michel Dänzer51f89a02013-12-09 15:33:53 +09001459 /* dummy bits */
1460 last_args[5]= uint->zero;
1461 last_args[6]= uint->zero;
1462 last_args[7]= uint->zero;
1463 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001464 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001465
1466 /* Specify whether the EXEC mask represents the valid mask */
1467 last_args[1] = uint->one;
1468
1469 /* Specify that this is the last export */
1470 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1471
1472 lp_build_intrinsic(base->gallivm->builder,
1473 "llvm.SI.export",
1474 LLVMVoidTypeInContext(base->gallivm->context),
1475 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001476}
1477
Marek Olšák4855acd2013-08-06 15:08:54 +02001478static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1479 struct lp_build_tgsi_context * bld_base,
1480 struct lp_build_emit_data * emit_data);
1481
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001482static bool tgsi_is_shadow_sampler(unsigned target)
1483{
1484 return target == TGSI_TEXTURE_SHADOW1D ||
1485 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1486 target == TGSI_TEXTURE_SHADOW2D ||
1487 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1488 target == TGSI_TEXTURE_SHADOWCUBE ||
1489 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1490 target == TGSI_TEXTURE_SHADOWRECT;
1491}
1492
Marek Olšák877bb522014-06-25 03:12:46 +02001493static const struct lp_build_tgsi_action tex_action;
1494
Tom Stellarda75c6162012-01-06 17:38:37 -05001495static void tex_fetch_args(
1496 struct lp_build_tgsi_context * bld_base,
1497 struct lp_build_emit_data * emit_data)
1498{
Christian König55fe5cc2013-03-04 16:30:06 +01001499 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001500 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001501 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001502 unsigned opcode = inst->Instruction.Opcode;
1503 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001504 LLVMValueRef coords[4];
1505 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001506 int ref_pos;
1507 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001508 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001509 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001510 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1511 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001512 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001513
1514 if (target == TGSI_TEXTURE_BUFFER) {
1515 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1516 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1517 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1518 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1519
Marek Olšák4f3f0432014-07-07 22:49:15 +02001520 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001521 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1522 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1523 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1524 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1525
1526 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1527 emit_data->args[0] = res;
1528 emit_data->args[1] = bld_base->uint_bld.zero;
1529 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1530 emit_data->arg_count = 3;
1531 return;
1532 }
Tom Stellard467f5162012-05-16 15:15:35 -04001533
Michel Dänzer120efee2013-01-25 12:10:11 +01001534 /* Fetch and project texture coordinates */
1535 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001536 for (chan = 0; chan < 3; chan++ ) {
1537 coords[chan] = lp_build_emit_fetch(bld_base,
1538 emit_data->inst, 0,
1539 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001540 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001541 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1542 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001543 coords[chan],
1544 coords[3]);
1545 }
1546
Michel Dänzer120efee2013-01-25 12:10:11 +01001547 if (opcode == TGSI_OPCODE_TXP)
1548 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001549
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001550 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001551 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001552 /* The offsets are six-bit signed integers packed like this:
1553 * X=[5:0], Y=[13:8], and Z=[21:16].
1554 */
1555 LLVMValueRef offset[3], pack;
1556
1557 assert(inst->Texture.NumOffsets == 1);
1558
1559 for (chan = 0; chan < 3; chan++) {
1560 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1561 emit_data->inst, 0, chan);
1562 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1563 lp_build_const_int32(gallivm, 0x3f), "");
1564 if (chan)
1565 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1566 lp_build_const_int32(gallivm, chan*8), "");
1567 }
1568
1569 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1570 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1571 address[count++] = pack;
1572 }
1573
Michel Dänzer120efee2013-01-25 12:10:11 +01001574 /* Pack LOD bias value */
1575 if (opcode == TGSI_OPCODE_TXB)
1576 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001577 if (opcode == TGSI_OPCODE_TXB2)
1578 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001579
Michel Dänzer120efee2013-01-25 12:10:11 +01001580 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001581 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001582 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1583 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1584 } else {
1585 assert(ref_pos >= 0);
1586 address[count++] = coords[ref_pos];
1587 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001588 }
1589
Marek Olšákb279f0142014-07-04 03:19:58 +02001590 if (target == TGSI_TEXTURE_CUBE ||
1591 target == TGSI_TEXTURE_CUBE_ARRAY ||
1592 target == TGSI_TEXTURE_SHADOWCUBE ||
1593 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1594 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1595
Michel Dänzera6b83c02013-02-21 16:10:55 +01001596 /* Pack user derivatives */
1597 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001598 int num_deriv_channels, param;
1599
1600 switch (target) {
1601 case TGSI_TEXTURE_3D:
1602 num_deriv_channels = 3;
1603 break;
1604 case TGSI_TEXTURE_2D:
1605 case TGSI_TEXTURE_SHADOW2D:
1606 case TGSI_TEXTURE_RECT:
1607 case TGSI_TEXTURE_SHADOWRECT:
1608 case TGSI_TEXTURE_2D_ARRAY:
1609 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1610 case TGSI_TEXTURE_CUBE:
1611 case TGSI_TEXTURE_SHADOWCUBE:
1612 case TGSI_TEXTURE_CUBE_ARRAY:
1613 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1614 num_deriv_channels = 2;
1615 break;
1616 case TGSI_TEXTURE_1D:
1617 case TGSI_TEXTURE_SHADOW1D:
1618 case TGSI_TEXTURE_1D_ARRAY:
1619 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1620 num_deriv_channels = 1;
1621 break;
1622 default:
1623 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001624 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001625
1626 for (param = 1; param <= 2; param++)
1627 for (chan = 0; chan < num_deriv_channels; chan++)
1628 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001629 }
1630
Michel Dänzer120efee2013-01-25 12:10:11 +01001631 /* Pack texture coordinates */
1632 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001633 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001634 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001635 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001636 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001637
Marek Olšákd2bd6342013-09-18 15:40:21 +02001638 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001639 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001640 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001641 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001642 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001643
1644 if (count > 16) {
1645 assert(!"Cannot handle more than 16 texture address parameters");
1646 count = 16;
1647 }
1648
1649 for (chan = 0; chan < count; chan++ ) {
1650 address[chan] = LLVMBuildBitCast(gallivm->builder,
1651 address[chan],
1652 LLVMInt32TypeInContext(gallivm->context),
1653 "");
1654 }
1655
Marek Olšák4855acd2013-08-06 15:08:54 +02001656 /* Adjust the sample index according to FMASK.
1657 *
1658 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1659 * which is the identity mapping. Each nibble says which physical sample
1660 * should be fetched to get that sample.
1661 *
1662 * For example, 0x11111100 means there are only 2 samples stored and
1663 * the second sample covers 3/4 of the pixel. When reading samples 0
1664 * and 1, return physical sample 0 (determined by the first two 0s
1665 * in FMASK), otherwise return physical sample 1.
1666 *
1667 * The sample index should be adjusted as follows:
1668 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1669 */
1670 if (target == TGSI_TEXTURE_2D_MSAA ||
1671 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1672 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1673 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001674 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001675 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001676 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001677
Marek Olšákd2bd6342013-09-18 15:40:21 +02001678 memcpy(txf_address, address, sizeof(txf_address));
1679
1680 if (target == TGSI_TEXTURE_2D_MSAA) {
1681 txf_address[2] = bld_base->uint_bld.zero;
1682 }
1683 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001684
1685 /* Pad to a power-of-two size. */
1686 while (txf_count < util_next_power_of_two(txf_count))
1687 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1688
1689 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001690 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1691 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1692 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001693 txf_emit_data.chan = 0;
1694 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001695 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001696 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001697 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001698 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001699 txf_emit_data.arg_count = 3;
1700
Marek Olšák877bb522014-06-25 03:12:46 +02001701 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001702
1703 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001704 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1705 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1706
1707 /* Apply the formula. */
1708 LLVMValueRef fmask =
1709 LLVMBuildExtractElement(gallivm->builder,
1710 txf_emit_data.output[0],
1711 uint_bld->zero, "");
1712
Marek Olšákd2bd6342013-09-18 15:40:21 +02001713 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001714
1715 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001716 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001717
1718 LLVMValueRef shifted_fmask =
1719 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1720
1721 LLVMValueRef final_sample =
1722 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1723
1724 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1725 * resource descriptor is 0 (invalid),
1726 */
1727 LLVMValueRef fmask_desc =
1728 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001729 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001730 LLVMVectorType(uint_bld->elem_type, 8), "");
1731
1732 LLVMValueRef fmask_word1 =
1733 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1734 uint_bld->one, "");
1735
1736 LLVMValueRef word1_is_nonzero =
1737 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1738 fmask_word1, uint_bld->zero, "");
1739
Marek Olšákd2bd6342013-09-18 15:40:21 +02001740 /* Replace the MSAA sample index. */
1741 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001742 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001743 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001744 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001745
Michel Dänzer36231112013-05-02 09:44:45 +02001746 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001747 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001748
1749 if (opcode == TGSI_OPCODE_TXF) {
1750 /* add tex offsets */
1751 if (inst->Texture.NumOffsets) {
1752 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1753 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1754 const struct tgsi_texture_offset * off = inst->TexOffsets;
1755
1756 assert(inst->Texture.NumOffsets == 1);
1757
Marek Olšákdefedc02013-09-18 15:36:38 +02001758 switch (target) {
1759 case TGSI_TEXTURE_3D:
1760 address[2] = lp_build_add(uint_bld, address[2],
1761 bld->immediates[off->Index][off->SwizzleZ]);
1762 /* fall through */
1763 case TGSI_TEXTURE_2D:
1764 case TGSI_TEXTURE_SHADOW2D:
1765 case TGSI_TEXTURE_RECT:
1766 case TGSI_TEXTURE_SHADOWRECT:
1767 case TGSI_TEXTURE_2D_ARRAY:
1768 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001769 address[1] =
1770 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001771 bld->immediates[off->Index][off->SwizzleY]);
1772 /* fall through */
1773 case TGSI_TEXTURE_1D:
1774 case TGSI_TEXTURE_SHADOW1D:
1775 case TGSI_TEXTURE_1D_ARRAY:
1776 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1777 address[0] =
1778 lp_build_add(uint_bld, address[0],
1779 bld->immediates[off->Index][off->SwizzleX]);
1780 break;
1781 /* texture offsets do not apply to other texture targets */
1782 }
Michel Dänzer36231112013-05-02 09:44:45 +02001783 }
1784
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001785 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1786 emit_data->arg_count = 3;
1787
Michel Dänzer36231112013-05-02 09:44:45 +02001788 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001789 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001790 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001791 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001792 opcode == TGSI_OPCODE_LODQ ||
1793 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001794 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1795 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1796 target == TGSI_TEXTURE_2D_ARRAY ||
1797 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1798 target == TGSI_TEXTURE_CUBE_ARRAY ||
1799 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1800 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001801 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001802
Marek Olšák57f3da92014-06-16 16:53:42 +02001803 if (opcode == TGSI_OPCODE_TG4) {
1804 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001805
Marek Olšák57f3da92014-06-16 16:53:42 +02001806 /* DMASK was repurposed for GATHER4. 4 components are always
1807 * returned and DMASK works like a swizzle - it selects
1808 * the component to fetch. The only valid DMASK values are
1809 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1810 * (red,red,red,red) etc.) The ISA document doesn't mention
1811 * this.
1812 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001813
Marek Olšák57f3da92014-06-16 16:53:42 +02001814 /* Get the component index from src1.x for Gather4. */
1815 if (!tgsi_is_shadow_sampler(target)) {
1816 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1817 LLVMValueRef comp_imm;
1818 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001819
Marek Olšák57f3da92014-06-16 16:53:42 +02001820 assert(src1.File == TGSI_FILE_IMMEDIATE);
1821
1822 comp_imm = imms[src1.Index][src1.SwizzleX];
1823 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1824 gather_comp = CLAMP(gather_comp, 0, 3);
1825 }
1826
1827 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001828 }
1829
Marek Olšák4855acd2013-08-06 15:08:54 +02001830 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001831 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001832 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1833 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1834 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1835 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1836 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1837 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1838 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1839
1840 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001841
1842 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001843 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001844 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001845 } else {
1846 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1847 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001848 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001849
1850 emit_data->dst_type = LLVMVectorType(
1851 LLVMFloatTypeInContext(gallivm->context),
1852 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001853 }
1854
Marek Olšák2484daa2014-04-22 21:23:29 +02001855 /* The fetch opcode has been converted to a 2D array fetch.
1856 * This simplifies the LLVM backend. */
1857 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1858 target = TGSI_TEXTURE_2D_ARRAY;
1859 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1860 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1861
Michel Dänzer120efee2013-01-25 12:10:11 +01001862 /* Pad to power of two vector */
1863 while (count < util_next_power_of_two(count))
1864 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1865
Christian Königccf3e8f2013-03-26 15:09:27 +01001866 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001867}
1868
Michel Dänzer07eddc42013-02-06 15:43:10 +01001869static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1870 struct lp_build_tgsi_context * bld_base,
1871 struct lp_build_emit_data * emit_data)
1872{
1873 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001874 unsigned opcode = emit_data->inst->Instruction.Opcode;
1875 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001876 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001877 bool has_offset = HAVE_LLVM >= 0x0305 ?
1878 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001879
Marek Olšák877bb522014-06-25 03:12:46 +02001880 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001881 emit_data->output[emit_data->chan] = build_intrinsic(
1882 base->gallivm->builder,
1883 "llvm.SI.vs.load.input", emit_data->dst_type,
1884 emit_data->args, emit_data->arg_count,
1885 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1886 return;
1887 }
1888
Marek Olšák877bb522014-06-25 03:12:46 +02001889 if (opcode == TGSI_OPCODE_TG4 ||
1890 opcode == TGSI_OPCODE_LODQ ||
1891 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1892 bool is_shadow = tgsi_is_shadow_sampler(target);
1893 const char *name = "llvm.SI.image.sample";
1894 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001895
Marek Olšák877bb522014-06-25 03:12:46 +02001896 switch (opcode) {
1897 case TGSI_OPCODE_TEX:
1898 case TGSI_OPCODE_TEX2:
1899 case TGSI_OPCODE_TXP:
1900 break;
1901 case TGSI_OPCODE_TXB:
1902 case TGSI_OPCODE_TXB2:
1903 infix = ".b";
1904 break;
1905 case TGSI_OPCODE_TXL:
1906 case TGSI_OPCODE_TXL2:
1907 infix = ".l";
1908 break;
1909 case TGSI_OPCODE_TXD:
1910 infix = ".d";
1911 break;
1912 case TGSI_OPCODE_TG4:
1913 name = "llvm.SI.gather4";
1914 break;
1915 case TGSI_OPCODE_LODQ:
1916 name = "llvm.SI.getlod";
1917 is_shadow = false;
1918 has_offset = false;
1919 break;
1920 default:
1921 assert(0);
1922 return;
1923 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001924
Marek Olšák877bb522014-06-25 03:12:46 +02001925 /* Add the type and suffixes .c, .o if needed. */
1926 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1927 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1928 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001929
Marek Olšák877bb522014-06-25 03:12:46 +02001930 emit_data->output[emit_data->chan] = build_intrinsic(
1931 base->gallivm->builder, intr_name, emit_data->dst_type,
1932 emit_data->args, emit_data->arg_count,
1933 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1934 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001935 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001936 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001937
Marek Olšák877bb522014-06-25 03:12:46 +02001938 switch (opcode) {
1939 case TGSI_OPCODE_TEX:
1940 case TGSI_OPCODE_TEX2:
1941 case TGSI_OPCODE_TXP:
1942 name = "llvm.SI.sample";
1943 break;
1944 case TGSI_OPCODE_TXB:
1945 case TGSI_OPCODE_TXB2:
1946 name = "llvm.SI.sampleb";
1947 break;
1948 case TGSI_OPCODE_TXD:
1949 name = "llvm.SI.sampled";
1950 break;
1951 case TGSI_OPCODE_TXF:
1952 name = "llvm.SI.imageload";
1953 break;
1954 case TGSI_OPCODE_TXL:
1955 case TGSI_OPCODE_TXL2:
1956 name = "llvm.SI.samplel";
1957 break;
1958 default:
1959 assert(0);
1960 return;
1961 }
1962
Marek Olšákd859bdb2014-07-14 19:40:14 +02001963 i8 = LLVMInt8TypeInContext(base->gallivm->context);
1964 v16i8 = LLVMVectorType(i8, 16);
1965 v32i8 = LLVMVectorType(i8, 32);
1966
1967 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
1968 emit_data->args[1], v32i8, "");
1969 if (opcode != TGSI_OPCODE_TXF) {
1970 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
1971 emit_data->args[2], v16i8, "");
1972 }
1973
Marek Olšák877bb522014-06-25 03:12:46 +02001974 sprintf(intr_name, "%s.v%ui32", name,
1975 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
1976
1977 emit_data->output[emit_data->chan] = build_intrinsic(
1978 base->gallivm->builder, intr_name, emit_data->dst_type,
1979 emit_data->args, emit_data->arg_count,
1980 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1981 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001982}
1983
Michel Dänzer0495adb2013-05-06 12:45:14 +02001984static void txq_fetch_args(
1985 struct lp_build_tgsi_context * bld_base,
1986 struct lp_build_emit_data * emit_data)
1987{
1988 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1989 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001990 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02001991 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001992
Marek Olšák2484daa2014-04-22 21:23:29 +02001993 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001994 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1995 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1996
1997 /* Read the size from the buffer descriptor directly. */
1998 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1999 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2000 size = LLVMBuildExtractElement(gallivm->builder, size,
2001 lp_build_const_int32(gallivm, 2), "");
2002 emit_data->args[0] = size;
2003 return;
2004 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002005
2006 /* Mip level */
2007 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2008
2009 /* Resource */
2010 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2011
Marek Olšák2484daa2014-04-22 21:23:29 +02002012 /* Texture target */
2013 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2014 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2015 target = TGSI_TEXTURE_2D_ARRAY;
2016
Michel Dänzer0495adb2013-05-06 12:45:14 +02002017 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002018 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002019
2020 emit_data->arg_count = 3;
2021
2022 emit_data->dst_type = LLVMVectorType(
2023 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2024 4);
2025}
2026
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002027static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2028 struct lp_build_tgsi_context * bld_base,
2029 struct lp_build_emit_data * emit_data)
2030{
Marek Olšák2484daa2014-04-22 21:23:29 +02002031 unsigned target = emit_data->inst->Texture.Texture;
2032
2033 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002034 /* Just return the buffer size. */
2035 emit_data->output[emit_data->chan] = emit_data->args[0];
2036 return;
2037 }
2038
2039 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002040
2041 /* Divide the number of layers by 6 to get the number of cubes. */
2042 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2043 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2044 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2045 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2046 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2047
2048 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2049 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2050 z = LLVMBuildSDiv(builder, z, six, "");
2051
2052 emit_data->output[emit_data->chan] =
2053 LLVMBuildInsertElement(builder, v4, z, two, "");
2054 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002055}
2056
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002057static void si_llvm_emit_ddxy(
2058 const struct lp_build_tgsi_action * action,
2059 struct lp_build_tgsi_context * bld_base,
2060 struct lp_build_emit_data * emit_data)
2061{
2062 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2063 struct gallivm_state *gallivm = bld_base->base.gallivm;
2064 struct lp_build_context * base = &bld_base->base;
2065 const struct tgsi_full_instruction *inst = emit_data->inst;
2066 unsigned opcode = inst->Instruction.Opcode;
2067 LLVMValueRef indices[2];
2068 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2069 LLVMValueRef tl, trbl, result[4];
2070 LLVMTypeRef i32;
2071 unsigned swizzle[4];
2072 unsigned c;
2073
2074 i32 = LLVMInt32TypeInContext(gallivm->context);
2075
2076 indices[0] = bld_base->uint_bld.zero;
2077 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2078 NULL, 0, LLVMReadNoneAttribute);
2079 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2080 indices, 2, "");
2081
2082 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2083 lp_build_const_int32(gallivm, 0xfffffffc), "");
2084 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2085 indices, 2, "");
2086
2087 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2088 lp_build_const_int32(gallivm,
2089 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2090 "");
2091 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2092 indices, 2, "");
2093
2094 for (c = 0; c < 4; ++c) {
2095 unsigned i;
2096
2097 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2098 for (i = 0; i < c; ++i) {
2099 if (swizzle[i] == swizzle[c]) {
2100 result[c] = result[i];
2101 break;
2102 }
2103 }
2104 if (i != c)
2105 continue;
2106
2107 LLVMBuildStore(gallivm->builder,
2108 LLVMBuildBitCast(gallivm->builder,
2109 lp_build_emit_fetch(bld_base, inst, 0, c),
2110 i32, ""),
2111 store_ptr);
2112
2113 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2114 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2115
2116 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2117 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2118
2119 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2120 }
2121
2122 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2123}
2124
Michel Dänzer404b29d2013-11-21 16:45:28 +09002125/* Emit one vertex from the geometry shader */
2126static void si_llvm_emit_vertex(
2127 const struct lp_build_tgsi_action *action,
2128 struct lp_build_tgsi_context *bld_base,
2129 struct lp_build_emit_data *emit_data)
2130{
2131 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002132 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002133 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002134 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002135 struct gallivm_state *gallivm = bld_base->base.gallivm;
2136 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002137 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2138 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002139 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002140 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002141 LLVMValueRef args[2];
2142 unsigned chan;
2143 int i;
2144
Michel Dänzer404b29d2013-11-21 16:45:28 +09002145 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002146 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002147
2148 /* If this thread has already emitted the declared maximum number of
2149 * vertices, kill it: excessive vertex emissions are not supposed to
2150 * have any effect, and GS threads have no externally observable
2151 * effects other than emitting vertices.
2152 */
2153 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2154 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002155 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002156 kill = lp_build_select(&bld_base->base, can_emit,
2157 lp_build_const_float(gallivm, 1.0f),
2158 lp_build_const_float(gallivm, -1.0f));
2159 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2160 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2161
Marek Olšák02134cf2014-10-04 22:07:50 +02002162 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002163 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002164 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002165
2166 for (chan = 0; chan < 4; chan++) {
2167 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002168 LLVMValueRef voffset =
2169 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002170 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002171
2172 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2173 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002174
2175 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2176
Marek Olšákfc3b3352014-10-05 13:33:40 +02002177 build_tbuffer_store(si_shader_ctx,
2178 si_shader_ctx->gsvs_ring,
2179 out_val, 1,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002180 voffset, soffset, 0,
2181 V_008F0C_BUF_DATA_FORMAT_32,
2182 V_008F0C_BUF_NUM_FORMAT_UINT,
2183 1, 0, 1, 1, 0);
2184 }
2185 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002186 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2187 lp_build_const_int32(gallivm, 1));
2188 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002189
2190 /* Signal vertex emission */
2191 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2192 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2193 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2194 LLVMVoidTypeInContext(gallivm->context), args, 2,
2195 LLVMNoUnwindAttribute);
2196}
2197
2198/* Cut one primitive from the geometry shader */
2199static void si_llvm_emit_primitive(
2200 const struct lp_build_tgsi_action *action,
2201 struct lp_build_tgsi_context *bld_base,
2202 struct lp_build_emit_data *emit_data)
2203{
2204 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2205 struct gallivm_state *gallivm = bld_base->base.gallivm;
2206 LLVMValueRef args[2];
2207
2208 /* Signal primitive cut */
2209 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | 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
Tom Stellarda75c6162012-01-06 17:38:37 -05002216static const struct lp_build_tgsi_action tex_action = {
2217 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002218 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002219};
2220
Michel Dänzer0495adb2013-05-06 12:45:14 +02002221static const struct lp_build_tgsi_action txq_action = {
2222 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002223 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002224 .intr_name = "llvm.SI.resinfo"
2225};
2226
Christian König206f0592013-03-20 14:37:21 +01002227static void create_meta_data(struct si_shader_context *si_shader_ctx)
2228{
2229 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2230 LLVMValueRef args[3];
2231
2232 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2233 args[1] = 0;
2234 args[2] = lp_build_const_int32(gallivm, 1);
2235
2236 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2237}
2238
Marek Olšák4f3f0432014-07-07 22:49:15 +02002239static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2240{
2241 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2242 CONST_ADDR_SPACE);
2243}
2244
Christian König55fe5cc2013-03-04 16:30:06 +01002245static void create_function(struct si_shader_context *si_shader_ctx)
2246{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002247 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2248 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002249 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002250 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002251 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002252
Christian König55fe5cc2013-03-04 16:30:06 +01002253 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002254 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002255 f32 = LLVMFloatTypeInContext(gallivm->context);
2256 v2i32 = LLVMVectorType(i32, 2);
2257 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002258 v4i32 = LLVMVectorType(i32, 4);
2259 v8i32 = LLVMVectorType(i32, 8);
2260 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002261
Marek Olšákee2a8182014-07-07 23:27:19 +02002262 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002263 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002264 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2265 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002266 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002267
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002268 switch (si_shader_ctx->type) {
2269 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002270 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002271 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002272 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002273 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002274 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002275
Michel Dänzer404b29d2013-11-21 16:45:28 +09002276 if (shader->key.vs.as_es) {
2277 params[SI_PARAM_ES2GS_OFFSET] = i32;
2278 num_params++;
2279 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002280 if (shader->is_gs_copy_shader) {
2281 last_array_pointer = SI_PARAM_CONST;
2282 num_params = SI_PARAM_CONST+1;
2283 }
2284
Michel Dänzer404b29d2013-11-21 16:45:28 +09002285 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002286
Michel Dänzer404b29d2013-11-21 16:45:28 +09002287 /* Streamout SGPRs. */
2288 if (shader->selector->so.num_outputs) {
2289 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2290 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2291 }
2292 /* A streamout buffer offset is loaded if the stride is non-zero. */
2293 for (i = 0; i < 4; i++) {
2294 if (!shader->selector->so.stride[i])
2295 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002296
Michel Dänzer404b29d2013-11-21 16:45:28 +09002297 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2298 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002299 }
2300
2301 last_sgpr = num_params-1;
2302
2303 /* VGPRs */
2304 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2305 params[num_params++] = i32; /* unused*/
2306 params[num_params++] = i32; /* unused */
2307 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002308 break;
Christian König0666ffd2013-03-05 15:07:39 +01002309
Michel Dänzer404b29d2013-11-21 16:45:28 +09002310 case TGSI_PROCESSOR_GEOMETRY:
2311 params[SI_PARAM_GS2VS_OFFSET] = i32;
2312 params[SI_PARAM_GS_WAVE_ID] = i32;
2313 last_sgpr = SI_PARAM_GS_WAVE_ID;
2314
2315 /* VGPRs */
2316 params[SI_PARAM_VTX0_OFFSET] = i32;
2317 params[SI_PARAM_VTX1_OFFSET] = i32;
2318 params[SI_PARAM_PRIMITIVE_ID] = i32;
2319 params[SI_PARAM_VTX2_OFFSET] = i32;
2320 params[SI_PARAM_VTX3_OFFSET] = i32;
2321 params[SI_PARAM_VTX4_OFFSET] = i32;
2322 params[SI_PARAM_VTX5_OFFSET] = i32;
2323 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2324 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2325 break;
2326
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002327 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002328 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002329 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002330 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002331 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2332 params[SI_PARAM_PERSP_CENTER] = v2i32;
2333 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2334 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2335 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2336 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2337 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2338 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2339 params[SI_PARAM_POS_X_FLOAT] = f32;
2340 params[SI_PARAM_POS_Y_FLOAT] = f32;
2341 params[SI_PARAM_POS_Z_FLOAT] = f32;
2342 params[SI_PARAM_POS_W_FLOAT] = f32;
2343 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002344 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002345 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2346 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002347 num_params = SI_PARAM_POS_FIXED_PT+1;
2348 break;
2349
2350 default:
2351 assert(0 && "unimplemented shader");
2352 return;
Christian Königc4973212013-03-05 12:14:02 +01002353 }
Christian König55fe5cc2013-03-04 16:30:06 +01002354
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002355 assert(num_params <= Elements(params));
2356 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002357 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002358
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002359 for (i = 0; i <= last_sgpr; ++i) {
2360 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002361
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002362 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2363 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002364 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002365 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002366 else
2367 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002368 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002369
Michel Dänzer404b29d2013-11-21 16:45:28 +09002370 if (bld_base->info &&
2371 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2372 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002373 si_shader_ctx->ddxy_lds =
2374 LLVMAddGlobalInAddressSpace(gallivm->module,
2375 LLVMArrayType(i32, 64),
2376 "ddxy_lds",
2377 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002378}
Tom Stellarda75c6162012-01-06 17:38:37 -05002379
Christian König0f6cf2b2013-03-15 15:53:25 +01002380static void preload_constants(struct si_shader_context *si_shader_ctx)
2381{
2382 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2383 struct gallivm_state * gallivm = bld_base->base.gallivm;
2384 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002385 unsigned buf;
2386 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002387
Marek Olšákee2a8182014-07-07 23:27:19 +02002388 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002389 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002390
Marek Olšák2fd42002013-10-25 11:45:47 +02002391 if (num_const == 0)
2392 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002393
Marek Olšák2fd42002013-10-25 11:45:47 +02002394 /* Allocate space for the constant values */
2395 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002396
Marek Olšák2fd42002013-10-25 11:45:47 +02002397 /* Load the resource descriptor */
2398 si_shader_ctx->const_resource[buf] =
Marek Olšákd7876082014-09-26 23:06:32 +02002399 build_indexed_load_const(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002400
Marek Olšák2fd42002013-10-25 11:45:47 +02002401 /* Load the constants, we rely on the code sinking to do the rest */
2402 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002403 si_shader_ctx->constants[buf][i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002404 buffer_load_const(gallivm->builder,
Marek Olšák250aa932014-05-06 14:10:47 +02002405 si_shader_ctx->const_resource[buf],
2406 lp_build_const_int32(gallivm, i * 4),
2407 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002408 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002409 }
2410}
2411
Christian König1c100182013-03-17 16:02:42 +01002412static void preload_samplers(struct si_shader_context *si_shader_ctx)
2413{
2414 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2415 struct gallivm_state * gallivm = bld_base->base.gallivm;
2416 const struct tgsi_shader_info * info = bld_base->info;
2417
2418 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2419
2420 LLVMValueRef res_ptr, samp_ptr;
2421 LLVMValueRef offset;
2422
2423 if (num_samplers == 0)
2424 return;
2425
Christian König1c100182013-03-17 16:02:42 +01002426 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2427 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2428
2429 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2430 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002431 /* Resource */
2432 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002433 si_shader_ctx->resources[i] = build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Christian König1c100182013-03-17 16:02:42 +01002434
2435 /* Sampler */
2436 offset = lp_build_const_int32(gallivm, i);
Marek Olšákd7876082014-09-26 23:06:32 +02002437 si_shader_ctx->samplers[i] = build_indexed_load_const(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002438
2439 /* FMASK resource */
2440 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002441 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2442 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšákd7876082014-09-26 23:06:32 +02002443 build_indexed_load_const(si_shader_ctx, res_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002444 }
Christian König1c100182013-03-17 16:02:42 +01002445 }
2446}
2447
Marek Olšák8d03d922013-09-01 23:59:06 +02002448static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2449{
2450 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2451 struct gallivm_state * gallivm = bld_base->base.gallivm;
2452 unsigned i;
2453
Michel Dänzer67e385b2014-01-08 17:48:21 +09002454 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2455 si_shader_ctx->shader->key.vs.as_es ||
2456 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002457 return;
2458
2459 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002460 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002461
2462 /* Load the resources, we rely on the code sinking to do the rest */
2463 for (i = 0; i < 4; ++i) {
2464 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002465 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002466 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002467
Marek Olšákd7876082014-09-26 23:06:32 +02002468 si_shader_ctx->so_buffers[i] = build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
Marek Olšák8d03d922013-09-01 23:59:06 +02002469 }
2470 }
2471}
2472
Marek Olšákfc3b3352014-10-05 13:33:40 +02002473/**
2474 * Load ESGS and GSVS ring buffer resource descriptors and save the variables
2475 * for later use.
2476 */
2477static void preload_ring_buffers(struct si_shader_context *si_shader_ctx)
2478{
2479 struct gallivm_state *gallivm =
2480 si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2481
2482 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2483 SI_PARAM_RW_BUFFERS);
2484
2485 if ((si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
2486 si_shader_ctx->shader->key.vs.as_es) ||
2487 si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY) {
2488 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_ESGS);
2489
2490 si_shader_ctx->esgs_ring =
2491 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2492 }
2493
2494 if (si_shader_ctx->type == TGSI_PROCESSOR_GEOMETRY ||
2495 si_shader_ctx->shader->is_gs_copy_shader) {
2496 LLVMValueRef offset = lp_build_const_int32(gallivm, SI_RING_GSVS);
2497
2498 si_shader_ctx->gsvs_ring =
2499 build_indexed_load_const(si_shader_ctx, buf_ptr, offset);
2500 }
2501}
2502
Marek Olšák1abb1a92014-09-17 22:44:22 +02002503int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2504 LLVMModuleRef mod)
Tom Stellard302f53d2012-10-25 13:50:10 -04002505{
Darren Powellbc866902014-03-31 18:00:28 -04002506 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002507 unsigned i;
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002508 unsigned char *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002509 struct radeon_shader_binary binary;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002510 bool dump = r600_can_dump_shader(&sscreen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002511 shader->selector ? shader->selector->tokens : NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002512 const char * gpu_family = r600_get_llvm_processor_name(sscreen->b.family);
Tom Stellard9ba31052014-07-14 16:49:08 -04002513 unsigned code_size;
Darren Powellbc866902014-03-31 18:00:28 -04002514
2515 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002516 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002517 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2518
2519 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002520 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002521 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002522 for (i = 0; i < binary.code_size; i+=4 ) {
2523 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2524 binary.code[i + 2], binary.code[i + 1],
2525 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002526 }
2527 }
2528
Tom Stellardd50343d2013-04-04 16:21:06 -04002529 /* XXX: We may be able to emit some of these values directly rather than
2530 * extracting fields to be emitted later.
2531 */
Darren Powellbc866902014-03-31 18:00:28 -04002532 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002533 for (i = 0; i < binary.config_size; i+= 8) {
2534 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2535 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2536 switch (reg) {
2537 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2538 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2539 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2540 case R_00B848_COMPUTE_PGM_RSRC1:
2541 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2542 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2543 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002544 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2545 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2546 break;
2547 case R_00B84C_COMPUTE_PGM_RSRC2:
2548 shader->lds_size = G_00B84C_LDS_SIZE(value);
2549 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002550 case R_0286CC_SPI_PS_INPUT_ENA:
2551 shader->spi_ps_input_ena = value;
2552 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002553 case R_00B860_COMPUTE_TMPRING_SIZE:
2554 /* WAVESIZE is in units of 256 dwords. */
2555 shader->scratch_bytes_per_wave =
2556 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2557 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002558 default:
2559 fprintf(stderr, "Warning: Compiler emitted unknown "
2560 "config register: 0x%x\n", reg);
2561 break;
2562 }
2563 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002564
2565 /* copy new shader */
Tom Stellard9ba31052014-07-14 16:49:08 -04002566 code_size = binary.code_size + binary.rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002567 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002568 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002569 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002570 if (shader->bo == NULL) {
2571 return -ENOMEM;
2572 }
2573
Marek Olšák1abb1a92014-09-17 22:44:22 +02002574 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_WRITE);
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002575 util_memcpy_cpu_to_le32(ptr, binary.code, binary.code_size);
2576 if (binary.rodata_size > 0) {
2577 ptr += binary.code_size;
2578 util_memcpy_cpu_to_le32(ptr, binary.rodata, binary.rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002579 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002580
Marek Olšák1abb1a92014-09-17 22:44:22 +02002581 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002582
Tom Stellard7782d192013-04-04 09:57:13 -07002583 free(binary.code);
2584 free(binary.config);
Tom Stellard9ba31052014-07-14 16:49:08 -04002585 free(binary.rodata);
Tom Stellard302f53d2012-10-25 13:50:10 -04002586
Darren Powellbc866902014-03-31 18:00:28 -04002587 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002588}
2589
Michel Dänzer404b29d2013-11-21 16:45:28 +09002590/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002591static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002592 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002593 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002594{
2595 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2596 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2597 struct lp_build_context *base = &bld_base->base;
2598 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002599 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002600 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002601 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002602 LLVMValueRef args[9];
2603 int i, r;
2604
Marek Olšák02134cf2014-10-04 22:07:50 +02002605 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002606
2607 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002608 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002609
2610 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2611
2612 create_meta_data(si_shader_ctx);
2613 create_function(si_shader_ctx);
2614 preload_streamout_buffers(si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002615 preload_ring_buffers(si_shader_ctx);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002616
Marek Olšákfc3b3352014-10-05 13:33:40 +02002617 args[0] = si_shader_ctx->gsvs_ring;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002618 args[1] = lp_build_mul_imm(uint,
2619 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2620 si_shader_ctx->param_vertex_id),
2621 4);
2622 args[3] = uint->zero;
2623 args[4] = uint->one; /* OFFEN */
2624 args[5] = uint->zero; /* IDXEN */
2625 args[6] = uint->one; /* GLC */
2626 args[7] = uint->one; /* SLC */
2627 args[8] = uint->zero; /* TFE */
2628
2629 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002630 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002631 unsigned chan;
2632
Marek Olšák02134cf2014-10-04 22:07:50 +02002633 outputs[i].name = gsinfo->output_semantic_name[i];
Marek Olšák02134cf2014-10-04 22:07:50 +02002634 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002635
2636 for (chan = 0; chan < 4; chan++) {
2637 args[2] = lp_build_const_int32(gallivm,
2638 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002639 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002640
2641 outputs[i].values[chan] =
2642 LLVMBuildBitCast(gallivm->builder,
2643 build_intrinsic(gallivm->builder,
2644 "llvm.SI.buffer.load.dword.i32.i32",
2645 LLVMInt32TypeInContext(gallivm->context),
2646 args, 9,
2647 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2648 base->elem_type, "");
2649 }
2650 }
2651
Marek Olšák02134cf2014-10-04 22:07:50 +02002652 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002653
2654 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2655
2656 if (dump)
2657 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2658
Marek Olšák1abb1a92014-09-17 22:44:22 +02002659 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002660 bld_base->base.gallivm->module);
2661
2662 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2663
2664 FREE(outputs);
2665 return r;
2666}
2667
Marek Olšák1abb1a92014-09-17 22:44:22 +02002668int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002669{
Marek Olšák2774abd2014-09-16 18:45:33 +02002670 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002671 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002672 struct lp_build_tgsi_context * bld_base;
2673 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002674 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002675 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002676
2677 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2678 * conversion fails. */
2679 if (dump) {
2680 tgsi_dump(sel->tokens, 0);
2681 si_dump_streamout(&sel->so);
2682 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002683
Marek Olšák8c37c162014-09-16 18:40:07 +02002684 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002685
Michel Dänzercfebaf92012-08-31 19:04:08 +02002686 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002687 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2688 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2689
Marek Olšák52335682014-09-30 15:12:09 +02002690 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002691 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2692
Marek Olšák52335682014-09-30 15:12:09 +02002693 shader->uses_instanceid = sel->info.uses_instanceid;
2694 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002695 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002696
2697 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002698 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002699 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2700 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002701 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002702 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2703 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2704 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002705 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002706 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002707 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2708 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002709
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002710 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2711 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002712
Michel Dänzer404b29d2013-11-21 16:45:28 +09002713 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2714 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2715
Christian Könige4ed5872013-03-21 18:02:52 +01002716 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002717 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002718 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2719 si_shader_ctx.shader = shader;
2720 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002721
Michel Dänzer51f89a02013-12-09 15:33:53 +09002722 switch (si_shader_ctx.type) {
2723 case TGSI_PROCESSOR_VERTEX:
2724 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002725 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002726 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2727 } else {
2728 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2729 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002730 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002731 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002732 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2733 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002734 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002735 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002736 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2737 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002738
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002739 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002740 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2741 shader->db_shader_control |=
2742 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2743 break;
2744 case TGSI_FS_DEPTH_LAYOUT_LESS:
2745 shader->db_shader_control |=
2746 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2747 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002748 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002749 break;
2750 default:
2751 assert(!"Unsupported shader type");
2752 return -1;
2753 }
2754
Christian König206f0592013-03-20 14:37:21 +01002755 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002756 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002757 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002758 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002759 preload_streamout_buffers(&si_shader_ctx);
Marek Olšákfc3b3352014-10-05 13:33:40 +02002760 preload_ring_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002761
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002762 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2763 si_shader_ctx.gs_next_vertex =
2764 lp_build_alloca(bld_base->base.gallivm,
2765 bld_base->uint_bld.elem_type, "");
2766 }
2767
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002768 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002769 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002770 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002771 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002772
2773 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2774
2775 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002776 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002777 if (r) {
2778 fprintf(stderr, "LLVM failed to compile shader\n");
2779 goto out;
2780 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002781
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002782 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002783
2784 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002785 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002786 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002787 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002788 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002789 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2790 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002791 free(shader->gs_copy_shader);
2792 shader->gs_copy_shader = NULL;
2793 goto out;
2794 }
2795 }
2796
Tom Stellarda75c6162012-01-06 17:38:37 -05002797 tgsi_parse_free(&si_shader_ctx.parse);
2798
Michel Dänzer404b29d2013-11-21 16:45:28 +09002799out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002800 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002801 FREE(si_shader_ctx.constants[i]);
Tom Stellarda75c6162012-01-06 17:38:37 -05002802
Tom Stellard302f53d2012-10-25 13:50:10 -04002803 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002804}
2805
Marek Olšák2774abd2014-09-16 18:45:33 +02002806void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002807{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002808 if (shader->gs_copy_shader)
2809 si_shader_destroy(ctx, shader->gs_copy_shader);
2810
Marek Olšáka81c3e02013-08-14 01:04:39 +02002811 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002812 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002813}