blob: 2a1217946c1aceb97f72064d5e22d17f8e8a9565 [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;
52 unsigned index;
Michel Dänzer67e385b2014-01-08 17:48:21 +090053 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090054};
55
Tom Stellarda75c6162012-01-06 17:38:37 -050056struct si_shader_context
57{
58 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050059 struct tgsi_parse_context parse;
60 struct tgsi_token * tokens;
Marek Olšák8c37c162014-09-16 18:40:07 +020061 struct si_shader *shader;
Tom Stellarda75c6162012-01-06 17:38:37 -050062 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020063 int param_streamout_config;
64 int param_streamout_write_index;
65 int param_streamout_offset[4];
66 int param_vertex_id;
67 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010068 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020069 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020070 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020071 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Christian König1c100182013-03-17 16:02:42 +010072 LLVMValueRef *resources;
73 LLVMValueRef *samplers;
Marek Olšák8d03d922013-09-01 23:59:06 +020074 LLVMValueRef so_buffers[4];
Michel Dänzerf07a96d2014-01-08 18:45:10 +090075 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050076};
77
78static struct si_shader_context * si_shader_context(
79 struct lp_build_tgsi_context * bld_base)
80{
81 return (struct si_shader_context *)bld_base;
82}
83
84
85#define PERSPECTIVE_BASE 0
86#define LINEAR_BASE 9
87
88#define SAMPLE_OFFSET 0
89#define CENTER_OFFSET 2
90#define CENTROID_OFSET 4
91
92#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040093#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020094#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040095#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050096
Michel Dänzer404b29d2013-11-21 16:45:28 +090097
98#define SENDMSG_GS 2
99#define SENDMSG_GS_DONE 3
100
101#define SENDMSG_GS_OP_NOP (0 << 4)
102#define SENDMSG_GS_OP_CUT (1 << 4)
103#define SENDMSG_GS_OP_EMIT (2 << 4)
104#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
105
Marek Olšáke29353f2014-09-17 22:17:02 +0200106/**
107 * Returns a unique index for a semantic name and index. The index must be
108 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
109 * calculated.
110 */
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200111unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
Marek Olšáke29353f2014-09-17 22:17:02 +0200112{
113 switch (semantic_name) {
114 case TGSI_SEMANTIC_POSITION:
115 return 0;
116 case TGSI_SEMANTIC_PSIZE:
117 return 1;
118 case TGSI_SEMANTIC_CLIPDIST:
119 assert(index <= 1);
120 return 2 + index;
121 case TGSI_SEMANTIC_CLIPVERTEX:
122 return 4;
123 case TGSI_SEMANTIC_COLOR:
124 assert(index <= 1);
125 return 5 + index;
126 case TGSI_SEMANTIC_BCOLOR:
127 assert(index <= 1);
128 return 7 + index;
129 case TGSI_SEMANTIC_FOG:
130 return 9;
131 case TGSI_SEMANTIC_EDGEFLAG:
132 return 10;
133 case TGSI_SEMANTIC_GENERIC:
134 assert(index <= 63-11);
135 return 11 + index;
136 default:
137 assert(0);
138 return 63;
139 }
140}
141
142/**
143 * Given a semantic name and index of a parameter and a mask of used parameters
144 * (inputs or outputs), return the index of the parameter in the list of all
145 * used parameters.
146 *
147 * For example, assume this list of parameters:
148 * POSITION, PSIZE, GENERIC0, GENERIC2
149 * which has the mask:
150 * 11000000000101
151 * Then:
152 * querying POSITION returns 0,
153 * querying PSIZE returns 1,
154 * querying GENERIC0 returns 2,
155 * querying GENERIC2 returns 3.
156 *
157 * Which can be used as an offset to a parameter buffer in units of vec4s.
158 */
159static int get_param_index(unsigned semantic_name, unsigned index,
160 uint64_t mask)
161{
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200162 unsigned unique_index = si_shader_io_get_unique_index(semantic_name, index);
Marek Olšáke29353f2014-09-17 22:17:02 +0200163 int i, param_index = 0;
164
165 /* If not present... */
166 if (!((1llu << unique_index) & mask))
167 return -1;
168
169 for (i = 0; mask; i++) {
170 uint64_t bit = 1llu << i;
171
172 if (bit & mask) {
173 if (i == unique_index)
174 return param_index;
175
176 mask &= ~bit;
177 param_index++;
178 }
179 }
180
181 assert(!"unreachable");
182 return -1;
183}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900184
Tom Stellard467f5162012-05-16 15:15:35 -0400185/**
186 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
187 *
188 * @param offset The offset parameter specifies the number of
189 * elements to offset, not the number of bytes or dwords. An element is the
190 * the type pointed to by the base_ptr parameter (e.g. int is the element of
191 * an int* pointer)
192 *
193 * When LLVM lowers the load instruction, it will convert the element offset
194 * into a dword offset automatically.
195 *
196 */
197static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100198 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400199 LLVMValueRef base_ptr,
200 LLVMValueRef offset)
201{
Christian König206f0592013-03-20 14:37:21 +0100202 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400203
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200204 LLVMValueRef indices[2] = {
205 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
206 offset
207 };
Christian König206f0592013-03-20 14:37:21 +0100208 LLVMValueRef computed_ptr = LLVMBuildGEP(
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200209 base->gallivm->builder, base_ptr, indices, 2, "");
Christian König206f0592013-03-20 14:37:21 +0100210
211 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
212 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
213 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400214}
215
Marek Olšákf317ce52013-09-05 15:39:57 +0200216static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100217 struct radeon_llvm_context * radeon_bld,
218 unsigned divisor)
219{
Marek Olšák8d03d922013-09-01 23:59:06 +0200220 struct si_shader_context *si_shader_ctx =
221 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100222 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
223
Marek Olšák8d03d922013-09-01 23:59:06 +0200224 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
225 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100226 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
227 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
228
229 if (divisor > 1)
230 result = LLVMBuildUDiv(gallivm->builder, result,
231 lp_build_const_int32(gallivm, divisor), "");
232
233 return result;
234}
235
Tom Stellarda75c6162012-01-06 17:38:37 -0500236static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900237 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500238 unsigned input_index,
239 const struct tgsi_full_declaration *decl)
240{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900241 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
242 struct gallivm_state *gallivm = base->gallivm;
243 struct si_shader_context *si_shader_ctx =
244 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100245 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
246
247 unsigned chan;
248
Tom Stellarda75c6162012-01-06 17:38:37 -0500249 LLVMValueRef t_list_ptr;
250 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400251 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500252 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100253 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400254 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500255 LLVMTypeRef vec4_type;
256 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500257
Tom Stellard467f5162012-05-16 15:15:35 -0400258 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100259 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500260
Michel Dänzer51f89a02013-12-09 15:33:53 +0900261 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400262
Christian König206f0592013-03-20 14:37:21 +0100263 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400264
265 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900266 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500267
Christian Königa0dca442013-03-22 15:59:22 +0100268 if (divisor) {
269 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200270 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200271 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100272 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200273 /* Load the buffer index for vertices. */
274 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
275 si_shader_ctx->param_vertex_id);
276 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
277 SI_PARAM_BASE_VERTEX);
278 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100279 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500280
281 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400282 args[0] = t_list;
283 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100284 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900285 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100286 "llvm.SI.vs.load.input", vec4_type, args, 3,
287 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500288
289 /* Break up the vec4 into individual components */
290 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900291 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500292 /* XXX: Use a helper function for this. There is one in
293 * tgsi_llvm.c. */
294 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900295 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500296 input, llvm_chan, "");
297 }
298}
299
Michel Dänzer404b29d2013-11-21 16:45:28 +0900300static LLVMValueRef fetch_input_gs(
301 struct lp_build_tgsi_context *bld_base,
302 const struct tgsi_full_src_register *reg,
303 enum tgsi_opcode_type type,
304 unsigned swizzle)
305{
306 struct lp_build_context *base = &bld_base->base;
307 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200308 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900309 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
310 struct gallivm_state *gallivm = base->gallivm;
311 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
312 LLVMValueRef vtx_offset;
313 LLVMValueRef t_list_ptr;
314 LLVMValueRef t_list;
315 LLVMValueRef args[9];
316 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200317 struct tgsi_shader_info *info = &shader->selector->info;
318 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
319 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900320
Marek Olšáke23fec12014-10-04 17:40:39 +0200321 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900322 if (swizzle == 0)
323 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
324 SI_PARAM_PRIMITIVE_ID);
325 else
326 return uint->zero;
327 }
328
Michel Dänzer404b29d2013-11-21 16:45:28 +0900329 if (!reg->Register.Dimension)
330 return NULL;
331
332 if (swizzle == ~0) {
333 LLVMValueRef values[TGSI_NUM_CHANNELS];
334 unsigned chan;
335 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
336 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
337 }
338 return lp_build_gather_values(bld_base->base.gallivm, values,
339 TGSI_NUM_CHANNELS);
340 }
341
342 /* Get the vertex offset parameter */
343 vtx_offset_param = reg->Dimension.Index;
344 if (vtx_offset_param < 2) {
345 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
346 } else {
347 assert(vtx_offset_param < 6);
348 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
349 }
350 vtx_offset = lp_build_mul_imm(uint,
351 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
352 vtx_offset_param),
353 4);
354
355 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +0900356 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
357 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900358 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900359 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900360
361 args[0] = t_list;
362 args[1] = vtx_offset;
363 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200364 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200365 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900366 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900367 args[3] = uint->zero;
368 args[4] = uint->one; /* OFFEN */
369 args[5] = uint->zero; /* IDXEN */
370 args[6] = uint->one; /* GLC */
371 args[7] = uint->zero; /* SLC */
372 args[8] = uint->zero; /* TFE */
373
374 return LLVMBuildBitCast(gallivm->builder,
375 build_intrinsic(gallivm->builder,
376 "llvm.SI.buffer.load.dword.i32.i32",
377 i32, args, 9,
378 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
379 tgsi2llvmtype(bld_base, type), "");
380}
381
Tom Stellarda75c6162012-01-06 17:38:37 -0500382static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900383 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500384 unsigned input_index,
385 const struct tgsi_full_declaration *decl)
386{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900387 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
388 struct si_shader_context *si_shader_ctx =
389 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200390 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900391 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
392 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400393 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900394 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100395
396 LLVMValueRef interp_param;
397 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500398
399 /* This value is:
400 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
401 * quad begins a new primitive. Bit 0 always needs
402 * to be unset)
403 * [32:16] ParamOffset
404 *
405 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900406 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200407 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500408
Christian König0666ffd2013-03-05 15:07:39 +0100409 unsigned chan;
410
Tom Stellard0fb1e682012-09-06 16:18:11 -0400411 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
412 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400413 unsigned soa_index =
414 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900415 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100416 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100417
418 if (chan == 3)
419 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900420 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100421 LLVMBuildFDiv(gallivm->builder,
422 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900423 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100424 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400425 }
426 return;
427 }
428
Michel Dänzer97078b12012-09-25 12:41:31 +0200429 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
430 LLVMValueRef face, is_face_positive;
431
Christian König0666ffd2013-03-05 15:07:39 +0100432 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
433
Michel Dänzer97078b12012-09-25 12:41:31 +0200434 is_face_positive = LLVMBuildFCmp(gallivm->builder,
435 LLVMRealUGT, face,
436 lp_build_const_float(gallivm, 0.0f),
437 "");
438
Michel Dänzer51f89a02013-12-09 15:33:53 +0900439 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200440 LLVMBuildSelect(gallivm->builder,
441 is_face_positive,
442 lp_build_const_float(gallivm, 1.0f),
443 lp_build_const_float(gallivm, 0.0f),
444 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900445 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
446 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200447 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900448 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200449 lp_build_const_float(gallivm, 1.0f);
450
451 return;
452 }
453
Marek Olšák8b057dd2014-10-04 22:15:07 +0200454 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200455 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200456 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200457
Francisco Jerez12799232012-04-30 18:27:52 +0200458 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500459 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100460 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500461 break;
462 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200463 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200464 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400465 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100466 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200467 else
Christian König0666ffd2013-03-05 15:07:39 +0100468 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200469 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200470 case TGSI_INTERPOLATE_COLOR:
471 if (si_shader_ctx->shader->key.ps.flatshade) {
472 interp_param = 0;
473 break;
474 }
475 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200476 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200477 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200478 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400479 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100480 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200481 else
Christian König0666ffd2013-03-05 15:07:39 +0100482 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500483 break;
484 default:
485 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
486 return;
487 }
488
Christian König0666ffd2013-03-05 15:07:39 +0100489 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
490
Tom Stellarda75c6162012-01-06 17:38:37 -0500491 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200492 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100493 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100494 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200495 LLVMValueRef face, is_face_positive;
496 LLVMValueRef back_attr_number =
497 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200498 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200499
Christian König0666ffd2013-03-05 15:07:39 +0100500 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
501
Michel Dänzer691f08d2012-09-06 18:03:38 +0200502 is_face_positive = LLVMBuildFCmp(gallivm->builder,
503 LLVMRealUGT, face,
504 lp_build_const_float(gallivm, 0.0f),
505 "");
506
Tom Stellarda75c6162012-01-06 17:38:37 -0500507 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100508 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200509 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
510 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
511 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
512 LLVMValueRef front, back;
513
514 args[0] = llvm_chan;
515 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900516 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100517 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100518 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200519
520 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900521 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100522 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100523 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200524
Michel Dänzer51f89a02013-12-09 15:33:53 +0900525 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200526 LLVMBuildSelect(gallivm->builder,
527 is_face_positive,
528 front,
529 back,
530 "");
531 }
532
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900533 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200534 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
535 LLVMValueRef args[4];
536
537 args[0] = uint->zero;
538 args[1] = attr_number;
539 args[2] = params;
540 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900541 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
542 build_intrinsic(gallivm->builder, intr_name,
543 input_type, args, args[3] ? 4 : 3,
544 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
545 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
546 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200547 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900548 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200549 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200550 } else {
551 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100552 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200553 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
554 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
555 args[0] = llvm_chan;
556 args[1] = attr_number;
557 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100558 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900559 radeon_bld->inputs[soa_index] =
560 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100561 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100562 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200563 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500564 }
565}
566
Marek Olšák5b06fc32014-05-06 18:12:40 +0200567static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
568{
569 struct gallivm_state *gallivm = &radeon_bld->gallivm;
570 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
571 SI_PARAM_ANCILLARY);
572 value = LLVMBuildLShr(gallivm->builder, value,
573 lp_build_const_int32(gallivm, 8), "");
574 value = LLVMBuildAnd(gallivm->builder, value,
575 lp_build_const_int32(gallivm, 0xf), "");
576 return value;
577}
578
Marek Olšák250aa932014-05-06 14:10:47 +0200579static LLVMValueRef load_const(LLVMBuilderRef builder, LLVMValueRef resource,
580 LLVMValueRef offset, LLVMTypeRef return_type)
581{
582 LLVMValueRef args[2] = {resource, offset};
583
584 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
585 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
586}
587
Christian Könige4ed5872013-03-21 18:02:52 +0100588static void declare_system_value(
589 struct radeon_llvm_context * radeon_bld,
590 unsigned index,
591 const struct tgsi_full_declaration *decl)
592{
Marek Olšák8d03d922013-09-01 23:59:06 +0200593 struct si_shader_context *si_shader_ctx =
594 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200595 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
596 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100597 LLVMValueRef value = 0;
598
599 switch (decl->Semantic.Name) {
600 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200601 value = LLVMGetParam(radeon_bld->main_fn,
602 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100603 break;
604
605 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200606 value = LLVMGetParam(radeon_bld->main_fn,
607 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100608 break;
609
Marek Olšák5b06fc32014-05-06 18:12:40 +0200610 case TGSI_SEMANTIC_SAMPLEID:
611 value = get_sample_id(radeon_bld);
612 break;
613
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200614 case TGSI_SEMANTIC_SAMPLEPOS:
615 {
616 LLVMBuilderRef builder = gallivm->builder;
617 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200618 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200619 LLVMValueRef resource = build_indexed_load(si_shader_ctx, desc, buf_index);
620
621 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
622 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
623 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
624
625 LLVMValueRef pos[4] = {
626 load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
627 load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
628 lp_build_const_float(gallivm, 0),
629 lp_build_const_float(gallivm, 0)
630 };
631 value = lp_build_gather_values(gallivm, pos, 4);
632 break;
633 }
634
Christian Könige4ed5872013-03-21 18:02:52 +0100635 default:
636 assert(!"unknown system value");
637 return;
638 }
639
640 radeon_bld->system_values[index] = value;
641}
642
Tom Stellarda75c6162012-01-06 17:38:37 -0500643static LLVMValueRef fetch_constant(
644 struct lp_build_tgsi_context * bld_base,
645 const struct tgsi_full_src_register *reg,
646 enum tgsi_opcode_type type,
647 unsigned swizzle)
648{
Christian König55fe5cc2013-03-04 16:30:06 +0100649 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500650 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100651 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200652 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500653
Christian König0f6cf2b2013-03-15 15:53:25 +0100654 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100655 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500656
Christian König8514f5a2013-02-04 17:46:42 +0100657 if (swizzle == LP_CHAN_ALL) {
658 unsigned chan;
659 LLVMValueRef values[4];
660 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
661 values[chan] = fetch_constant(bld_base, reg, type, chan);
662
663 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
664 }
665
Marek Olšák2fd42002013-10-25 11:45:47 +0200666 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100667 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100668
Marek Olšák2fd42002013-10-25 11:45:47 +0200669 if (!reg->Register.Indirect)
670 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
671
Christian König0f6cf2b2013-03-15 15:53:25 +0100672 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
673 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
674 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200675 addr = lp_build_add(&bld_base->uint_bld, addr,
676 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200677
Marek Olšák250aa932014-05-06 14:10:47 +0200678 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
679 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500680
Christian Königf5298b02013-02-28 14:50:07 +0100681 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500682}
683
Michel Dänzer26c71392012-08-24 12:03:11 +0200684/* Initialize arguments for the shader export intrinsic */
685static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900686 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200687 unsigned target,
688 LLVMValueRef *args)
689{
690 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
691 struct lp_build_context *uint =
692 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
693 struct lp_build_context *base = &bld_base->base;
694 unsigned compressed = 0;
695 unsigned chan;
696
Michel Dänzerf402acd2012-08-22 18:15:36 +0200697 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
698 int cbuf = target - V_008DFC_SQ_EXP_MRT;
699
700 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100701 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100702
703 if (compressed)
704 si_shader_ctx->shader->spi_shader_col_format |=
705 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
706 else
707 si_shader_ctx->shader->spi_shader_col_format |=
708 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200709
710 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200711 }
712 }
713
714 if (compressed) {
715 /* Pixel shader needs to pack output values before export */
716 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900717 args[0] = values[2 * chan];
718 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200719 args[chan + 5] =
720 build_intrinsic(base->gallivm->builder,
721 "llvm.SI.packf16",
722 LLVMInt32TypeInContext(base->gallivm->context),
723 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100724 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100725 args[chan + 7] = args[chan + 5] =
726 LLVMBuildBitCast(base->gallivm->builder,
727 args[chan + 5],
728 LLVMFloatTypeInContext(base->gallivm->context),
729 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200730 }
731
732 /* Set COMPR flag */
733 args[4] = uint->one;
734 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900735 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200736 /* +5 because the first output value will be
737 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900738 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200739
740 /* Clear COMPR flag */
741 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200742 }
743
744 /* XXX: This controls which components of the output
745 * registers actually get exported. (e.g bit 0 means export
746 * X component, bit 1 means export Y component, etc.) I'm
747 * hard coding this to 0xf for now. In the future, we might
748 * want to do something else. */
749 args[0] = lp_build_const_int32(base->gallivm, 0xf);
750
751 /* Specify whether the EXEC mask represents the valid mask */
752 args[1] = uint->zero;
753
754 /* Specify whether this is the last export */
755 args[2] = uint->zero;
756
757 /* Specify the target we are exporting */
758 args[3] = lp_build_const_int32(base->gallivm, target);
759
Michel Dänzer26c71392012-08-24 12:03:11 +0200760 /* XXX: We probably need to keep track of the output
761 * values, so we know what we are passing to the next
762 * stage. */
763}
764
Michel Dänzer404b29d2013-11-21 16:45:28 +0900765/* Load from output pointers and initialize arguments for the shader export intrinsic */
766static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
767 LLVMValueRef *out_ptr,
768 unsigned target,
769 LLVMValueRef *args)
770{
771 struct gallivm_state *gallivm = bld_base->base.gallivm;
772 LLVMValueRef values[4];
773 int i;
774
775 for (i = 0; i < 4; i++)
776 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
777
778 si_llvm_init_export_args(bld_base, values, target, args);
779}
780
Michel Dänzer7708a862012-11-02 15:57:30 +0100781static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900782 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100783{
784 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
785 struct gallivm_state *gallivm = bld_base->base.gallivm;
786
Christian Königa0dca442013-03-22 15:59:22 +0100787 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400788 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
789 SI_PARAM_ALPHA_REF);
790
Michel Dänzer7708a862012-11-02 15:57:30 +0100791 LLVMValueRef alpha_pass =
792 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100793 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900794 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400795 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100796 LLVMValueRef arg =
797 lp_build_select(&bld_base->base,
798 alpha_pass,
799 lp_build_const_float(gallivm, 1.0f),
800 lp_build_const_float(gallivm, -1.0f));
801
802 build_intrinsic(gallivm->builder,
803 "llvm.AMDGPU.kill",
804 LLVMVoidTypeInContext(gallivm->context),
805 &arg, 1, 0);
806 } else {
807 build_intrinsic(gallivm->builder,
808 "llvm.AMDGPU.kilp",
809 LLVMVoidTypeInContext(gallivm->context),
810 NULL, 0, 0);
811 }
Marek Olšákadc57972014-09-19 17:07:07 +0200812
813 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100814}
815
Michel Dänzere3befbc2013-05-15 18:09:50 +0200816static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900817 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200818{
819 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200820 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200821 struct lp_build_context *base = &bld_base->base;
822 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200823 unsigned reg_index;
824 unsigned chan;
825 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200826 LLVMValueRef base_elt;
827 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200828 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák2fd42002013-10-25 11:45:47 +0200829 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200830
Michel Dänzere3befbc2013-05-15 18:09:50 +0200831 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200832 LLVMValueRef *args = pos[2 + reg_index];
833
Marek Olšák8c37c162014-09-16 18:40:07 +0200834 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200835
Michel Dänzere3befbc2013-05-15 18:09:50 +0200836 args[5] =
837 args[6] =
838 args[7] =
839 args[8] = lp_build_const_float(base->gallivm, 0.0f);
840
841 /* Compute dot products of position and user clip plane vectors */
842 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
843 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200844 args[1] = lp_build_const_int32(base->gallivm,
845 ((reg_index * 4 + chan) * 4 +
846 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200847 base_elt = load_const(base->gallivm->builder, const_resource,
848 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200849 args[5 + chan] =
850 lp_build_add(base, args[5 + chan],
851 lp_build_mul(base, base_elt,
852 out_elts[const_chan]));
853 }
854 }
855
856 args[0] = lp_build_const_int32(base->gallivm, 0xf);
857 args[1] = uint->zero;
858 args[2] = uint->zero;
859 args[3] = lp_build_const_int32(base->gallivm,
860 V_008DFC_SQ_EXP_POS + 2 + reg_index);
861 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200862 }
863}
864
Marek Olšák8d03d922013-09-01 23:59:06 +0200865static void si_dump_streamout(struct pipe_stream_output_info *so)
866{
867 unsigned i;
868
869 if (so->num_outputs)
870 fprintf(stderr, "STREAMOUT\n");
871
872 for (i = 0; i < so->num_outputs; i++) {
873 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
874 so->output[i].start_component;
875 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
876 i, so->output[i].output_buffer,
877 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
878 so->output[i].register_index,
879 mask & 1 ? "x" : "",
880 mask & 2 ? "y" : "",
881 mask & 4 ? "z" : "",
882 mask & 8 ? "w" : "");
883 }
884}
885
886/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
887 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
888 * or v4i32 (num_channels=3,4). */
889static void build_tbuffer_store(struct si_shader_context *shader,
890 LLVMValueRef rsrc,
891 LLVMValueRef vdata,
892 unsigned num_channels,
893 LLVMValueRef vaddr,
894 LLVMValueRef soffset,
895 unsigned inst_offset,
896 unsigned dfmt,
897 unsigned nfmt,
898 unsigned offen,
899 unsigned idxen,
900 unsigned glc,
901 unsigned slc,
902 unsigned tfe)
903{
904 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
905 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
906 LLVMValueRef args[] = {
907 rsrc,
908 vdata,
909 LLVMConstInt(i32, num_channels, 0),
910 vaddr,
911 soffset,
912 LLVMConstInt(i32, inst_offset, 0),
913 LLVMConstInt(i32, dfmt, 0),
914 LLVMConstInt(i32, nfmt, 0),
915 LLVMConstInt(i32, offen, 0),
916 LLVMConstInt(i32, idxen, 0),
917 LLVMConstInt(i32, glc, 0),
918 LLVMConstInt(i32, slc, 0),
919 LLVMConstInt(i32, tfe, 0)
920 };
921
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900922 /* The instruction offset field has 12 bits */
923 assert(offen || inst_offset < (1 << 12));
924
Marek Olšák8d03d922013-09-01 23:59:06 +0200925 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
926 unsigned func = CLAMP(num_channels, 1, 3) - 1;
927 const char *types[] = {"i32", "v2i32", "v4i32"};
928 char name[256];
929 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
930
931 lp_build_intrinsic(gallivm->builder, name,
932 LLVMVoidTypeInContext(gallivm->context),
933 args, Elements(args));
934}
935
936static void build_streamout_store(struct si_shader_context *shader,
937 LLVMValueRef rsrc,
938 LLVMValueRef vdata,
939 unsigned num_channels,
940 LLVMValueRef vaddr,
941 LLVMValueRef soffset,
942 unsigned inst_offset)
943{
944 static unsigned dfmt[] = {
945 V_008F0C_BUF_DATA_FORMAT_32,
946 V_008F0C_BUF_DATA_FORMAT_32_32,
947 V_008F0C_BUF_DATA_FORMAT_32_32_32,
948 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
949 };
950 assert(num_channels >= 1 && num_channels <= 4);
951
952 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
953 inst_offset, dfmt[num_channels-1],
954 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
955}
956
957/* On SI, the vertex shader is responsible for writing streamout data
958 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900959static void si_llvm_emit_streamout(struct si_shader_context *shader,
960 struct si_shader_output_values *outputs,
961 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200962{
963 struct pipe_stream_output_info *so = &shader->shader->selector->so;
964 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
965 LLVMBuilderRef builder = gallivm->builder;
966 int i, j;
967 struct lp_build_if_state if_ctx;
968
969 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
970
971 LLVMValueRef so_param =
972 LLVMGetParam(shader->radeon_bld.main_fn,
973 shader->param_streamout_config);
974
975 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
976 LLVMValueRef so_vtx_count =
977 LLVMBuildAnd(builder,
978 LLVMBuildLShr(builder, so_param,
979 LLVMConstInt(i32, 16, 0), ""),
980 LLVMConstInt(i32, 127, 0), "");
981
982 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
983 NULL, 0, LLVMReadNoneAttribute);
984
985 /* can_emit = tid < so_vtx_count; */
986 LLVMValueRef can_emit =
987 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
988
989 /* Emit the streamout code conditionally. This actually avoids
990 * out-of-bounds buffer access. The hw tells us via the SGPR
991 * (so_vtx_count) which threads are allowed to emit streamout data. */
992 lp_build_if(&if_ctx, gallivm, can_emit);
993 {
994 /* The buffer offset is computed as follows:
995 * ByteOffset = streamout_offset[buffer_id]*4 +
996 * (streamout_write_index + thread_id)*stride[buffer_id] +
997 * attrib_offset
998 */
999
1000 LLVMValueRef so_write_index =
1001 LLVMGetParam(shader->radeon_bld.main_fn,
1002 shader->param_streamout_write_index);
1003
1004 /* Compute (streamout_write_index + thread_id). */
1005 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1006
1007 /* Compute the write offset for each enabled buffer. */
1008 LLVMValueRef so_write_offset[4] = {};
1009 for (i = 0; i < 4; i++) {
1010 if (!so->stride[i])
1011 continue;
1012
1013 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1014 shader->param_streamout_offset[i]);
1015 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1016
1017 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1018 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1019 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1020 }
1021
Marek Olšák8d03d922013-09-01 23:59:06 +02001022 /* Write streamout data. */
1023 for (i = 0; i < so->num_outputs; i++) {
1024 unsigned buf_idx = so->output[i].output_buffer;
1025 unsigned reg = so->output[i].register_index;
1026 unsigned start = so->output[i].start_component;
1027 unsigned num_comps = so->output[i].num_components;
1028 LLVMValueRef out[4];
1029
1030 assert(num_comps && num_comps <= 4);
1031 if (!num_comps || num_comps > 4)
1032 continue;
1033
1034 /* Load the output as int. */
1035 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001036 unsigned outidx = 0;
1037
1038 while (outidx < noutput && outputs[outidx].index != reg)
1039 outidx++;
1040
1041 if (outidx < noutput)
1042 out[j] = LLVMBuildBitCast(builder,
1043 outputs[outidx].values[start+j],
1044 i32, "");
1045 else
1046 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +02001047 }
1048
Michel Dänzer67e385b2014-01-08 17:48:21 +09001049 if (!out[0])
1050 continue;
1051
Marek Olšák8d03d922013-09-01 23:59:06 +02001052 /* Pack the output. */
1053 LLVMValueRef vdata = NULL;
1054
1055 switch (num_comps) {
1056 case 1: /* as i32 */
1057 vdata = out[0];
1058 break;
1059 case 2: /* as v2i32 */
1060 case 3: /* as v4i32 (aligned to 4) */
1061 case 4: /* as v4i32 */
1062 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1063 for (j = 0; j < num_comps; j++) {
1064 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1065 LLVMConstInt(i32, j, 0), "");
1066 }
1067 break;
1068 }
1069
1070 build_streamout_store(shader, shader->so_buffers[buf_idx],
1071 vdata, num_comps,
1072 so_write_offset[buf_idx],
1073 LLVMConstInt(i32, 0, 0),
1074 so->output[i].dst_offset*4);
1075 }
1076 }
1077 lp_build_endif(&if_ctx);
1078}
1079
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001080
Michel Dänzer404b29d2013-11-21 16:45:28 +09001081/* Generate export instructions for hardware VS shader stage */
1082static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1083 struct si_shader_output_values *outputs,
1084 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001085{
1086 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001087 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001088 struct lp_build_context * base = &bld_base->base;
1089 struct lp_build_context * uint =
1090 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001091 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001092 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001093 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001094 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001095 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001096 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001097 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001098 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001099
Michel Dänzer67e385b2014-01-08 17:48:21 +09001100 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1101 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001102 }
1103
Michel Dänzer404b29d2013-11-21 16:45:28 +09001104 for (i = 0; i < noutput; i++) {
1105 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001106 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001107
Michel Dänzer0afeea52013-05-02 14:53:17 +02001108handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001109 /* Select the correct target */
1110 switch(semantic_name) {
1111 case TGSI_SEMANTIC_PSIZE:
1112 shader->vs_out_misc_write = true;
1113 shader->vs_out_point_size = true;
1114 psize_value = outputs[i].values[0];
1115 continue;
1116 case TGSI_SEMANTIC_EDGEFLAG:
1117 shader->vs_out_misc_write = true;
1118 shader->vs_out_edgeflag = true;
1119 edgeflag_value = outputs[i].values[0];
1120 continue;
1121 case TGSI_SEMANTIC_LAYER:
1122 shader->vs_out_misc_write = true;
1123 shader->vs_out_layer = true;
1124 layer_value = outputs[i].values[0];
1125 continue;
1126 case TGSI_SEMANTIC_POSITION:
1127 target = V_008DFC_SQ_EXP_POS;
1128 break;
1129 case TGSI_SEMANTIC_COLOR:
1130 case TGSI_SEMANTIC_BCOLOR:
1131 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001132 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001133 param_count++;
1134 break;
1135 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001136 shader->clip_dist_write |=
Marek Olšáka9592cd2014-10-04 19:09:09 +02001137 0xf << (semantic_index * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001138 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1139 break;
1140 case TGSI_SEMANTIC_CLIPVERTEX:
1141 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1142 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001143 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001144 case TGSI_SEMANTIC_FOG:
1145 case TGSI_SEMANTIC_GENERIC:
1146 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001147 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001148 param_count++;
1149 break;
1150 default:
1151 target = 0;
1152 fprintf(stderr,
1153 "Warning: SI unhandled vs output type:%d\n",
1154 semantic_name);
1155 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001156
Michel Dänzer404b29d2013-11-21 16:45:28 +09001157 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001158
Michel Dänzer404b29d2013-11-21 16:45:28 +09001159 if (target >= V_008DFC_SQ_EXP_POS &&
1160 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1161 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1162 args, sizeof(args));
1163 } else {
1164 lp_build_intrinsic(base->gallivm->builder,
1165 "llvm.SI.export",
1166 LLVMVoidTypeInContext(base->gallivm->context),
1167 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001168 }
1169
1170 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1171 semantic_name = TGSI_SEMANTIC_GENERIC;
1172 goto handle_semantic;
1173 }
1174 }
1175
1176 /* We need to add the position output manually if it's missing. */
1177 if (!pos_args[0][0]) {
1178 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1179 pos_args[0][1] = uint->zero; /* EXEC mask */
1180 pos_args[0][2] = uint->zero; /* last export? */
1181 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1182 pos_args[0][4] = uint->zero; /* COMPR flag */
1183 pos_args[0][5] = base->zero; /* X */
1184 pos_args[0][6] = base->zero; /* Y */
1185 pos_args[0][7] = base->zero; /* Z */
1186 pos_args[0][8] = base->one; /* W */
1187 }
1188
1189 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1190 if (shader->vs_out_misc_write) {
1191 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1192 shader->vs_out_point_size |
1193 (shader->vs_out_edgeflag << 1) |
1194 (shader->vs_out_layer << 2));
1195 pos_args[1][1] = uint->zero; /* EXEC mask */
1196 pos_args[1][2] = uint->zero; /* last export? */
1197 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1198 pos_args[1][4] = uint->zero; /* COMPR flag */
1199 pos_args[1][5] = base->zero; /* X */
1200 pos_args[1][6] = base->zero; /* Y */
1201 pos_args[1][7] = base->zero; /* Z */
1202 pos_args[1][8] = base->zero; /* W */
1203
Michel Dänzer404b29d2013-11-21 16:45:28 +09001204 if (shader->vs_out_point_size)
1205 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001206
1207 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001208 /* The output is a float, but the hw expects an integer
1209 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001210 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1211 edgeflag_value,
1212 bld_base->uint_bld.elem_type, "");
1213 edgeflag_value = lp_build_min(&bld_base->int_bld,
1214 edgeflag_value,
1215 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001216
1217 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001218 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1219 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001220 base->elem_type, "");
1221 }
1222
Michel Dänzer404b29d2013-11-21 16:45:28 +09001223 if (shader->vs_out_layer)
1224 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001225 }
1226
1227 for (i = 0; i < 4; i++)
1228 if (pos_args[i][0])
1229 shader->nr_pos_exports++;
1230
1231 pos_idx = 0;
1232 for (i = 0; i < 4; i++) {
1233 if (!pos_args[i][0])
1234 continue;
1235
1236 /* Specify the target we are exporting */
1237 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1238
1239 if (pos_idx == shader->nr_pos_exports)
1240 /* Specify that this is the last export */
1241 pos_args[i][2] = uint->one;
1242
1243 lp_build_intrinsic(base->gallivm->builder,
1244 "llvm.SI.export",
1245 LLVMVoidTypeInContext(base->gallivm->context),
1246 pos_args[i], 9);
1247 }
1248}
1249
Michel Dänzer404b29d2013-11-21 16:45:28 +09001250static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1251{
1252 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1253 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001254 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001255 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001256 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001257 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1258 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001259 LLVMValueRef t_list_ptr;
1260 LLVMValueRef t_list;
1261 unsigned chan;
1262 int i;
1263
Michel Dänzer404b29d2013-11-21 16:45:28 +09001264 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001265 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1266 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001267 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001268 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001269
Marek Olšák216cf862014-10-04 17:04:05 +02001270 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001271 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001272 si_shader_ctx->radeon_bld.soa.outputs[i];
1273 int param_index = get_param_index(info->output_semantic_name[i],
1274 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001275 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001276
Marek Olšáke29353f2014-09-17 22:17:02 +02001277 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001278 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001279
1280 for (chan = 0; chan < 4; chan++) {
1281 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001282 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1283
1284 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001285 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001286 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001287 V_008F0C_BUF_DATA_FORMAT_32,
1288 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001289 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001290 }
1291 }
1292}
1293
1294static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1295{
1296 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1297 struct gallivm_state *gallivm = bld_base->base.gallivm;
1298 LLVMValueRef args[2];
1299
1300 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1301 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1302 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1303 LLVMVoidTypeInContext(gallivm->context), args, 2,
1304 LLVMNoUnwindAttribute);
1305}
1306
1307static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1308{
1309 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1310 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšákec0d1682014-10-04 22:33:36 +02001311 struct tgsi_shader_info *info = &si_shader_ctx->shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001312 struct si_shader_output_values *outputs = NULL;
Marek Olšákec0d1682014-10-04 22:33:36 +02001313 int i,j;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001314
Marek Olšákec0d1682014-10-04 22:33:36 +02001315 outputs = MALLOC(info->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001316
Marek Olšákec0d1682014-10-04 22:33:36 +02001317 for (i = 0; i < info->num_outputs; i++) {
1318 outputs[i].name = info->output_semantic_name[i];
1319 outputs[i].sid = info->output_semantic_index[i];
1320 outputs[i].index = i;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001321
Marek Olšákec0d1682014-10-04 22:33:36 +02001322 for (j = 0; j < 4; j++)
1323 outputs[i].values[j] =
1324 LLVMBuildLoad(gallivm->builder,
1325 si_shader_ctx->radeon_bld.soa.outputs[i][j],
1326 "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001327 }
1328
Marek Olšákec0d1682014-10-04 22:33:36 +02001329 si_llvm_export_vs(bld_base, outputs, info->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001330 FREE(outputs);
1331}
1332
Michel Dänzer51f89a02013-12-09 15:33:53 +09001333static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1334{
1335 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001336 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001337 struct lp_build_context * base = &bld_base->base;
1338 struct lp_build_context * uint =
1339 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1340 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1341 LLVMValueRef args[9];
1342 LLVMValueRef last_args[9] = { 0 };
1343 unsigned semantic_name;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001344 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001345
1346 while (!tgsi_parse_end_of_tokens(parse)) {
1347 struct tgsi_full_declaration *d =
1348 &parse->FullToken.FullDeclaration;
1349 unsigned target;
1350 unsigned index;
1351
1352 tgsi_parse_token(parse);
1353
Michel Dänzer51f89a02013-12-09 15:33:53 +09001354 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1355 continue;
1356
Michel Dänzer51f89a02013-12-09 15:33:53 +09001357 semantic_name = d->Semantic.Name;
1358 for (index = d->Range.First; index <= d->Range.Last; index++) {
1359 /* Select the correct target */
1360 switch(semantic_name) {
1361 case TGSI_SEMANTIC_POSITION:
1362 depth_index = index;
1363 continue;
1364 case TGSI_SEMANTIC_STENCIL:
1365 stencil_index = index;
1366 continue;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001367 case TGSI_SEMANTIC_SAMPLEMASK:
1368 samplemask_index = index;
1369 continue;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001370 case TGSI_SEMANTIC_COLOR:
1371 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1372 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001373 LLVMBuildStore(bld_base->base.gallivm->builder,
1374 bld_base->base.one,
1375 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1376
Michel Dänzer51f89a02013-12-09 15:33:53 +09001377 if (d->Semantic.Index == 0 &&
1378 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001379 si_alpha_test(bld_base,
1380 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001381 break;
1382 default:
1383 target = 0;
1384 fprintf(stderr,
1385 "Warning: SI unhandled fs output type:%d\n",
1386 semantic_name);
1387 }
1388
Michel Dänzer404b29d2013-11-21 16:45:28 +09001389 si_llvm_init_export_args_load(bld_base,
1390 si_shader_ctx->radeon_bld.soa.outputs[index],
1391 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001392
1393 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001394 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001395 if (last_args[0]) {
1396 lp_build_intrinsic(base->gallivm->builder,
1397 "llvm.SI.export",
1398 LLVMVoidTypeInContext(base->gallivm->context),
1399 last_args, 9);
1400 }
1401
Marek Olšák0eb528a2013-12-04 13:24:22 +01001402 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001403 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001404
1405 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02001406 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
Marek Olšák7a645c52014-10-04 22:09:16 +02001407 d->Semantic.Index == 0 &&
Marek Olšák34e82002014-10-04 20:44:23 +02001408 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1409 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001410 si_llvm_init_export_args_load(bld_base,
1411 si_shader_ctx->radeon_bld.soa.outputs[index],
1412 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001413 lp_build_intrinsic(base->gallivm->builder,
1414 "llvm.SI.export",
1415 LLVMVoidTypeInContext(base->gallivm->context),
1416 args, 9);
1417 }
1418 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001419 } else {
1420 lp_build_intrinsic(base->gallivm->builder,
1421 "llvm.SI.export",
1422 LLVMVoidTypeInContext(base->gallivm->context),
1423 args, 9);
1424 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001425 }
1426 }
1427
Marek Olšákd0e8b652014-05-06 20:04:31 +02001428 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001429 LLVMValueRef out_ptr;
1430 unsigned mask = 0;
1431
1432 /* Specify the target we are exporting */
1433 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1434
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001435 args[5] = base->zero; /* R, depth */
1436 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1437 args[7] = base->zero; /* B, sample mask */
1438 args[8] = base->zero; /* A, alpha to mask */
1439
Michel Dänzer1a616c12012-11-13 17:35:09 +01001440 if (depth_index >= 0) {
1441 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1442 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1443 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001444 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001445 }
1446
1447 if (stencil_index >= 0) {
1448 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001449 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001450 /* Only setting the stencil component bit (0x2) here
1451 * breaks some stencil piglit tests
1452 */
1453 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001454 si_shader_ctx->shader->db_shader_control |=
1455 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001456 }
1457
Marek Olšákd0e8b652014-05-06 20:04:31 +02001458 if (samplemask_index >= 0) {
1459 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1460 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1461 mask |= 0xf; /* Set all components. */
1462 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1463 }
1464
1465 if (samplemask_index >= 0)
1466 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1467 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001468 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1469 else
1470 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1471
Michel Dänzer1a616c12012-11-13 17:35:09 +01001472 /* Specify which components to enable */
1473 args[0] = lp_build_const_int32(base->gallivm, mask);
1474
1475 args[1] =
1476 args[2] =
1477 args[4] = uint->zero;
1478
1479 if (last_args[0])
1480 lp_build_intrinsic(base->gallivm->builder,
1481 "llvm.SI.export",
1482 LLVMVoidTypeInContext(base->gallivm->context),
1483 args, 9);
1484 else
1485 memcpy(last_args, args, sizeof(args));
1486 }
1487
Michel Dänzer51f89a02013-12-09 15:33:53 +09001488 if (!last_args[0]) {
1489 /* Specify which components to enable */
1490 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001491
Michel Dänzer51f89a02013-12-09 15:33:53 +09001492 /* Specify the target we are exporting */
1493 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001494
Michel Dänzer51f89a02013-12-09 15:33:53 +09001495 /* Set COMPR flag to zero to export data as 32-bit */
1496 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001497
Michel Dänzer51f89a02013-12-09 15:33:53 +09001498 /* dummy bits */
1499 last_args[5]= uint->zero;
1500 last_args[6]= uint->zero;
1501 last_args[7]= uint->zero;
1502 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001503 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001504
1505 /* Specify whether the EXEC mask represents the valid mask */
1506 last_args[1] = uint->one;
1507
1508 /* Specify that this is the last export */
1509 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1510
1511 lp_build_intrinsic(base->gallivm->builder,
1512 "llvm.SI.export",
1513 LLVMVoidTypeInContext(base->gallivm->context),
1514 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001515}
1516
Marek Olšák4855acd2013-08-06 15:08:54 +02001517static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1518 struct lp_build_tgsi_context * bld_base,
1519 struct lp_build_emit_data * emit_data);
1520
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001521static bool tgsi_is_shadow_sampler(unsigned target)
1522{
1523 return target == TGSI_TEXTURE_SHADOW1D ||
1524 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1525 target == TGSI_TEXTURE_SHADOW2D ||
1526 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1527 target == TGSI_TEXTURE_SHADOWCUBE ||
1528 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1529 target == TGSI_TEXTURE_SHADOWRECT;
1530}
1531
Marek Olšák877bb522014-06-25 03:12:46 +02001532static const struct lp_build_tgsi_action tex_action;
1533
Tom Stellarda75c6162012-01-06 17:38:37 -05001534static void tex_fetch_args(
1535 struct lp_build_tgsi_context * bld_base,
1536 struct lp_build_emit_data * emit_data)
1537{
Christian König55fe5cc2013-03-04 16:30:06 +01001538 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001539 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001540 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001541 unsigned opcode = inst->Instruction.Opcode;
1542 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001543 LLVMValueRef coords[4];
1544 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001545 int ref_pos;
1546 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001547 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001548 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001549 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1550 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001551 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001552
1553 if (target == TGSI_TEXTURE_BUFFER) {
1554 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1555 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1556 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1557 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1558
Marek Olšák4f3f0432014-07-07 22:49:15 +02001559 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001560 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1561 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1562 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1563 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1564
1565 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1566 emit_data->args[0] = res;
1567 emit_data->args[1] = bld_base->uint_bld.zero;
1568 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1569 emit_data->arg_count = 3;
1570 return;
1571 }
Tom Stellard467f5162012-05-16 15:15:35 -04001572
Michel Dänzer120efee2013-01-25 12:10:11 +01001573 /* Fetch and project texture coordinates */
1574 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001575 for (chan = 0; chan < 3; chan++ ) {
1576 coords[chan] = lp_build_emit_fetch(bld_base,
1577 emit_data->inst, 0,
1578 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001579 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001580 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1581 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001582 coords[chan],
1583 coords[3]);
1584 }
1585
Michel Dänzer120efee2013-01-25 12:10:11 +01001586 if (opcode == TGSI_OPCODE_TXP)
1587 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001588
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001589 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001590 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001591 /* The offsets are six-bit signed integers packed like this:
1592 * X=[5:0], Y=[13:8], and Z=[21:16].
1593 */
1594 LLVMValueRef offset[3], pack;
1595
1596 assert(inst->Texture.NumOffsets == 1);
1597
1598 for (chan = 0; chan < 3; chan++) {
1599 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1600 emit_data->inst, 0, chan);
1601 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1602 lp_build_const_int32(gallivm, 0x3f), "");
1603 if (chan)
1604 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1605 lp_build_const_int32(gallivm, chan*8), "");
1606 }
1607
1608 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1609 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1610 address[count++] = pack;
1611 }
1612
Michel Dänzer120efee2013-01-25 12:10:11 +01001613 /* Pack LOD bias value */
1614 if (opcode == TGSI_OPCODE_TXB)
1615 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001616 if (opcode == TGSI_OPCODE_TXB2)
1617 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001618
Michel Dänzer120efee2013-01-25 12:10:11 +01001619 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001620 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001621 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1622 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1623 } else {
1624 assert(ref_pos >= 0);
1625 address[count++] = coords[ref_pos];
1626 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001627 }
1628
Marek Olšákb279f0142014-07-04 03:19:58 +02001629 if (target == TGSI_TEXTURE_CUBE ||
1630 target == TGSI_TEXTURE_CUBE_ARRAY ||
1631 target == TGSI_TEXTURE_SHADOWCUBE ||
1632 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1633 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1634
Michel Dänzera6b83c02013-02-21 16:10:55 +01001635 /* Pack user derivatives */
1636 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001637 int num_deriv_channels, param;
1638
1639 switch (target) {
1640 case TGSI_TEXTURE_3D:
1641 num_deriv_channels = 3;
1642 break;
1643 case TGSI_TEXTURE_2D:
1644 case TGSI_TEXTURE_SHADOW2D:
1645 case TGSI_TEXTURE_RECT:
1646 case TGSI_TEXTURE_SHADOWRECT:
1647 case TGSI_TEXTURE_2D_ARRAY:
1648 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1649 case TGSI_TEXTURE_CUBE:
1650 case TGSI_TEXTURE_SHADOWCUBE:
1651 case TGSI_TEXTURE_CUBE_ARRAY:
1652 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1653 num_deriv_channels = 2;
1654 break;
1655 case TGSI_TEXTURE_1D:
1656 case TGSI_TEXTURE_SHADOW1D:
1657 case TGSI_TEXTURE_1D_ARRAY:
1658 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1659 num_deriv_channels = 1;
1660 break;
1661 default:
1662 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001663 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001664
1665 for (param = 1; param <= 2; param++)
1666 for (chan = 0; chan < num_deriv_channels; chan++)
1667 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001668 }
1669
Michel Dänzer120efee2013-01-25 12:10:11 +01001670 /* Pack texture coordinates */
1671 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001672 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001673 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001674 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001675 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001676
Marek Olšákd2bd6342013-09-18 15:40:21 +02001677 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001678 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001679 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001680 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001681 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001682
1683 if (count > 16) {
1684 assert(!"Cannot handle more than 16 texture address parameters");
1685 count = 16;
1686 }
1687
1688 for (chan = 0; chan < count; chan++ ) {
1689 address[chan] = LLVMBuildBitCast(gallivm->builder,
1690 address[chan],
1691 LLVMInt32TypeInContext(gallivm->context),
1692 "");
1693 }
1694
Marek Olšák4855acd2013-08-06 15:08:54 +02001695 /* Adjust the sample index according to FMASK.
1696 *
1697 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1698 * which is the identity mapping. Each nibble says which physical sample
1699 * should be fetched to get that sample.
1700 *
1701 * For example, 0x11111100 means there are only 2 samples stored and
1702 * the second sample covers 3/4 of the pixel. When reading samples 0
1703 * and 1, return physical sample 0 (determined by the first two 0s
1704 * in FMASK), otherwise return physical sample 1.
1705 *
1706 * The sample index should be adjusted as follows:
1707 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1708 */
1709 if (target == TGSI_TEXTURE_2D_MSAA ||
1710 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1711 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1712 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001713 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001714 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001715 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001716
Marek Olšákd2bd6342013-09-18 15:40:21 +02001717 memcpy(txf_address, address, sizeof(txf_address));
1718
1719 if (target == TGSI_TEXTURE_2D_MSAA) {
1720 txf_address[2] = bld_base->uint_bld.zero;
1721 }
1722 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001723
1724 /* Pad to a power-of-two size. */
1725 while (txf_count < util_next_power_of_two(txf_count))
1726 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1727
1728 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001729 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1730 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1731 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001732 txf_emit_data.chan = 0;
1733 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001734 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001735 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001736 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001737 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001738 txf_emit_data.arg_count = 3;
1739
Marek Olšák877bb522014-06-25 03:12:46 +02001740 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001741
1742 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001743 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1744 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1745
1746 /* Apply the formula. */
1747 LLVMValueRef fmask =
1748 LLVMBuildExtractElement(gallivm->builder,
1749 txf_emit_data.output[0],
1750 uint_bld->zero, "");
1751
Marek Olšákd2bd6342013-09-18 15:40:21 +02001752 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001753
1754 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001755 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001756
1757 LLVMValueRef shifted_fmask =
1758 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1759
1760 LLVMValueRef final_sample =
1761 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1762
1763 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1764 * resource descriptor is 0 (invalid),
1765 */
1766 LLVMValueRef fmask_desc =
1767 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001768 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001769 LLVMVectorType(uint_bld->elem_type, 8), "");
1770
1771 LLVMValueRef fmask_word1 =
1772 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1773 uint_bld->one, "");
1774
1775 LLVMValueRef word1_is_nonzero =
1776 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1777 fmask_word1, uint_bld->zero, "");
1778
Marek Olšákd2bd6342013-09-18 15:40:21 +02001779 /* Replace the MSAA sample index. */
1780 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001781 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001782 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001783 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001784
Michel Dänzer36231112013-05-02 09:44:45 +02001785 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001786 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001787
1788 if (opcode == TGSI_OPCODE_TXF) {
1789 /* add tex offsets */
1790 if (inst->Texture.NumOffsets) {
1791 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1792 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1793 const struct tgsi_texture_offset * off = inst->TexOffsets;
1794
1795 assert(inst->Texture.NumOffsets == 1);
1796
Marek Olšákdefedc02013-09-18 15:36:38 +02001797 switch (target) {
1798 case TGSI_TEXTURE_3D:
1799 address[2] = lp_build_add(uint_bld, address[2],
1800 bld->immediates[off->Index][off->SwizzleZ]);
1801 /* fall through */
1802 case TGSI_TEXTURE_2D:
1803 case TGSI_TEXTURE_SHADOW2D:
1804 case TGSI_TEXTURE_RECT:
1805 case TGSI_TEXTURE_SHADOWRECT:
1806 case TGSI_TEXTURE_2D_ARRAY:
1807 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001808 address[1] =
1809 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001810 bld->immediates[off->Index][off->SwizzleY]);
1811 /* fall through */
1812 case TGSI_TEXTURE_1D:
1813 case TGSI_TEXTURE_SHADOW1D:
1814 case TGSI_TEXTURE_1D_ARRAY:
1815 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1816 address[0] =
1817 lp_build_add(uint_bld, address[0],
1818 bld->immediates[off->Index][off->SwizzleX]);
1819 break;
1820 /* texture offsets do not apply to other texture targets */
1821 }
Michel Dänzer36231112013-05-02 09:44:45 +02001822 }
1823
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001824 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1825 emit_data->arg_count = 3;
1826
Michel Dänzer36231112013-05-02 09:44:45 +02001827 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001828 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001829 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001830 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001831 opcode == TGSI_OPCODE_LODQ ||
1832 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001833 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1834 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1835 target == TGSI_TEXTURE_2D_ARRAY ||
1836 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1837 target == TGSI_TEXTURE_CUBE_ARRAY ||
1838 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1839 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001840 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001841
Marek Olšák57f3da92014-06-16 16:53:42 +02001842 if (opcode == TGSI_OPCODE_TG4) {
1843 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001844
Marek Olšák57f3da92014-06-16 16:53:42 +02001845 /* DMASK was repurposed for GATHER4. 4 components are always
1846 * returned and DMASK works like a swizzle - it selects
1847 * the component to fetch. The only valid DMASK values are
1848 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1849 * (red,red,red,red) etc.) The ISA document doesn't mention
1850 * this.
1851 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001852
Marek Olšák57f3da92014-06-16 16:53:42 +02001853 /* Get the component index from src1.x for Gather4. */
1854 if (!tgsi_is_shadow_sampler(target)) {
1855 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1856 LLVMValueRef comp_imm;
1857 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001858
Marek Olšák57f3da92014-06-16 16:53:42 +02001859 assert(src1.File == TGSI_FILE_IMMEDIATE);
1860
1861 comp_imm = imms[src1.Index][src1.SwizzleX];
1862 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1863 gather_comp = CLAMP(gather_comp, 0, 3);
1864 }
1865
1866 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001867 }
1868
Marek Olšák4855acd2013-08-06 15:08:54 +02001869 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001870 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001871 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1872 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1873 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1874 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1875 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1876 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1877 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1878
1879 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001880
1881 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001882 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001883 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001884 } else {
1885 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1886 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001887 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001888
1889 emit_data->dst_type = LLVMVectorType(
1890 LLVMFloatTypeInContext(gallivm->context),
1891 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001892 }
1893
Marek Olšák2484daa2014-04-22 21:23:29 +02001894 /* The fetch opcode has been converted to a 2D array fetch.
1895 * This simplifies the LLVM backend. */
1896 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1897 target = TGSI_TEXTURE_2D_ARRAY;
1898 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1899 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1900
Michel Dänzer120efee2013-01-25 12:10:11 +01001901 /* Pad to power of two vector */
1902 while (count < util_next_power_of_two(count))
1903 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1904
Christian Königccf3e8f2013-03-26 15:09:27 +01001905 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001906}
1907
Michel Dänzer07eddc42013-02-06 15:43:10 +01001908static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1909 struct lp_build_tgsi_context * bld_base,
1910 struct lp_build_emit_data * emit_data)
1911{
1912 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001913 unsigned opcode = emit_data->inst->Instruction.Opcode;
1914 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001915 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001916 bool has_offset = HAVE_LLVM >= 0x0305 ?
1917 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001918
Marek Olšák877bb522014-06-25 03:12:46 +02001919 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001920 emit_data->output[emit_data->chan] = build_intrinsic(
1921 base->gallivm->builder,
1922 "llvm.SI.vs.load.input", emit_data->dst_type,
1923 emit_data->args, emit_data->arg_count,
1924 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1925 return;
1926 }
1927
Marek Olšák877bb522014-06-25 03:12:46 +02001928 if (opcode == TGSI_OPCODE_TG4 ||
1929 opcode == TGSI_OPCODE_LODQ ||
1930 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1931 bool is_shadow = tgsi_is_shadow_sampler(target);
1932 const char *name = "llvm.SI.image.sample";
1933 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001934
Marek Olšák877bb522014-06-25 03:12:46 +02001935 switch (opcode) {
1936 case TGSI_OPCODE_TEX:
1937 case TGSI_OPCODE_TEX2:
1938 case TGSI_OPCODE_TXP:
1939 break;
1940 case TGSI_OPCODE_TXB:
1941 case TGSI_OPCODE_TXB2:
1942 infix = ".b";
1943 break;
1944 case TGSI_OPCODE_TXL:
1945 case TGSI_OPCODE_TXL2:
1946 infix = ".l";
1947 break;
1948 case TGSI_OPCODE_TXD:
1949 infix = ".d";
1950 break;
1951 case TGSI_OPCODE_TG4:
1952 name = "llvm.SI.gather4";
1953 break;
1954 case TGSI_OPCODE_LODQ:
1955 name = "llvm.SI.getlod";
1956 is_shadow = false;
1957 has_offset = false;
1958 break;
1959 default:
1960 assert(0);
1961 return;
1962 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001963
Marek Olšák877bb522014-06-25 03:12:46 +02001964 /* Add the type and suffixes .c, .o if needed. */
1965 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1966 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1967 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001968
Marek Olšák877bb522014-06-25 03:12:46 +02001969 emit_data->output[emit_data->chan] = build_intrinsic(
1970 base->gallivm->builder, intr_name, emit_data->dst_type,
1971 emit_data->args, emit_data->arg_count,
1972 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1973 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001974 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001975 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001976
Marek Olšák877bb522014-06-25 03:12:46 +02001977 switch (opcode) {
1978 case TGSI_OPCODE_TEX:
1979 case TGSI_OPCODE_TEX2:
1980 case TGSI_OPCODE_TXP:
1981 name = "llvm.SI.sample";
1982 break;
1983 case TGSI_OPCODE_TXB:
1984 case TGSI_OPCODE_TXB2:
1985 name = "llvm.SI.sampleb";
1986 break;
1987 case TGSI_OPCODE_TXD:
1988 name = "llvm.SI.sampled";
1989 break;
1990 case TGSI_OPCODE_TXF:
1991 name = "llvm.SI.imageload";
1992 break;
1993 case TGSI_OPCODE_TXL:
1994 case TGSI_OPCODE_TXL2:
1995 name = "llvm.SI.samplel";
1996 break;
1997 default:
1998 assert(0);
1999 return;
2000 }
2001
Marek Olšákd859bdb2014-07-14 19:40:14 +02002002 i8 = LLVMInt8TypeInContext(base->gallivm->context);
2003 v16i8 = LLVMVectorType(i8, 16);
2004 v32i8 = LLVMVectorType(i8, 32);
2005
2006 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
2007 emit_data->args[1], v32i8, "");
2008 if (opcode != TGSI_OPCODE_TXF) {
2009 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
2010 emit_data->args[2], v16i8, "");
2011 }
2012
Marek Olšák877bb522014-06-25 03:12:46 +02002013 sprintf(intr_name, "%s.v%ui32", name,
2014 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
2015
2016 emit_data->output[emit_data->chan] = build_intrinsic(
2017 base->gallivm->builder, intr_name, emit_data->dst_type,
2018 emit_data->args, emit_data->arg_count,
2019 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2020 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002021}
2022
Michel Dänzer0495adb2013-05-06 12:45:14 +02002023static void txq_fetch_args(
2024 struct lp_build_tgsi_context * bld_base,
2025 struct lp_build_emit_data * emit_data)
2026{
2027 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2028 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002029 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002030 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002031
Marek Olšák2484daa2014-04-22 21:23:29 +02002032 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002033 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2034 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2035
2036 /* Read the size from the buffer descriptor directly. */
2037 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2038 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2039 size = LLVMBuildExtractElement(gallivm->builder, size,
2040 lp_build_const_int32(gallivm, 2), "");
2041 emit_data->args[0] = size;
2042 return;
2043 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002044
2045 /* Mip level */
2046 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2047
2048 /* Resource */
2049 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2050
Marek Olšák2484daa2014-04-22 21:23:29 +02002051 /* Texture target */
2052 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2053 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2054 target = TGSI_TEXTURE_2D_ARRAY;
2055
Michel Dänzer0495adb2013-05-06 12:45:14 +02002056 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002057 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002058
2059 emit_data->arg_count = 3;
2060
2061 emit_data->dst_type = LLVMVectorType(
2062 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2063 4);
2064}
2065
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002066static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2067 struct lp_build_tgsi_context * bld_base,
2068 struct lp_build_emit_data * emit_data)
2069{
Marek Olšák2484daa2014-04-22 21:23:29 +02002070 unsigned target = emit_data->inst->Texture.Texture;
2071
2072 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002073 /* Just return the buffer size. */
2074 emit_data->output[emit_data->chan] = emit_data->args[0];
2075 return;
2076 }
2077
2078 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002079
2080 /* Divide the number of layers by 6 to get the number of cubes. */
2081 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2082 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2083 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2084 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2085 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2086
2087 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2088 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2089 z = LLVMBuildSDiv(builder, z, six, "");
2090
2091 emit_data->output[emit_data->chan] =
2092 LLVMBuildInsertElement(builder, v4, z, two, "");
2093 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002094}
2095
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002096static void si_llvm_emit_ddxy(
2097 const struct lp_build_tgsi_action * action,
2098 struct lp_build_tgsi_context * bld_base,
2099 struct lp_build_emit_data * emit_data)
2100{
2101 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2102 struct gallivm_state *gallivm = bld_base->base.gallivm;
2103 struct lp_build_context * base = &bld_base->base;
2104 const struct tgsi_full_instruction *inst = emit_data->inst;
2105 unsigned opcode = inst->Instruction.Opcode;
2106 LLVMValueRef indices[2];
2107 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2108 LLVMValueRef tl, trbl, result[4];
2109 LLVMTypeRef i32;
2110 unsigned swizzle[4];
2111 unsigned c;
2112
2113 i32 = LLVMInt32TypeInContext(gallivm->context);
2114
2115 indices[0] = bld_base->uint_bld.zero;
2116 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2117 NULL, 0, LLVMReadNoneAttribute);
2118 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2119 indices, 2, "");
2120
2121 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2122 lp_build_const_int32(gallivm, 0xfffffffc), "");
2123 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2124 indices, 2, "");
2125
2126 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2127 lp_build_const_int32(gallivm,
2128 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2129 "");
2130 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2131 indices, 2, "");
2132
2133 for (c = 0; c < 4; ++c) {
2134 unsigned i;
2135
2136 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2137 for (i = 0; i < c; ++i) {
2138 if (swizzle[i] == swizzle[c]) {
2139 result[c] = result[i];
2140 break;
2141 }
2142 }
2143 if (i != c)
2144 continue;
2145
2146 LLVMBuildStore(gallivm->builder,
2147 LLVMBuildBitCast(gallivm->builder,
2148 lp_build_emit_fetch(bld_base, inst, 0, c),
2149 i32, ""),
2150 store_ptr);
2151
2152 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2153 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2154
2155 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2156 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2157
2158 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2159 }
2160
2161 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2162}
2163
Michel Dänzer404b29d2013-11-21 16:45:28 +09002164/* Emit one vertex from the geometry shader */
2165static void si_llvm_emit_vertex(
2166 const struct lp_build_tgsi_action *action,
2167 struct lp_build_tgsi_context *bld_base,
2168 struct lp_build_emit_data *emit_data)
2169{
2170 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002171 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002172 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002173 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002174 struct gallivm_state *gallivm = bld_base->base.gallivm;
2175 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002176 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2177 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002178 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002179 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002180 LLVMValueRef t_list_ptr;
2181 LLVMValueRef t_list;
2182 LLVMValueRef args[2];
2183 unsigned chan;
2184 int i;
2185
2186 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002187 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2188 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002189 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002190 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002191
Michel Dänzer404b29d2013-11-21 16:45:28 +09002192 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002193 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002194
2195 /* If this thread has already emitted the declared maximum number of
2196 * vertices, kill it: excessive vertex emissions are not supposed to
2197 * have any effect, and GS threads have no externally observable
2198 * effects other than emitting vertices.
2199 */
2200 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2201 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002202 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002203 kill = lp_build_select(&bld_base->base, can_emit,
2204 lp_build_const_float(gallivm, 1.0f),
2205 lp_build_const_float(gallivm, -1.0f));
2206 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2207 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2208
Marek Olšák02134cf2014-10-04 22:07:50 +02002209 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002210 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002211 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002212
2213 for (chan = 0; chan < 4; chan++) {
2214 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002215 LLVMValueRef voffset =
2216 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002217 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002218
2219 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2220 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002221
2222 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2223
2224 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2225 voffset, soffset, 0,
2226 V_008F0C_BUF_DATA_FORMAT_32,
2227 V_008F0C_BUF_NUM_FORMAT_UINT,
2228 1, 0, 1, 1, 0);
2229 }
2230 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002231 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2232 lp_build_const_int32(gallivm, 1));
2233 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002234
2235 /* Signal vertex emission */
2236 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2237 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2238 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2239 LLVMVoidTypeInContext(gallivm->context), args, 2,
2240 LLVMNoUnwindAttribute);
2241}
2242
2243/* Cut one primitive from the geometry shader */
2244static void si_llvm_emit_primitive(
2245 const struct lp_build_tgsi_action *action,
2246 struct lp_build_tgsi_context *bld_base,
2247 struct lp_build_emit_data *emit_data)
2248{
2249 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2250 struct gallivm_state *gallivm = bld_base->base.gallivm;
2251 LLVMValueRef args[2];
2252
2253 /* Signal primitive cut */
2254 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2255 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2256 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2257 LLVMVoidTypeInContext(gallivm->context), args, 2,
2258 LLVMNoUnwindAttribute);
2259}
2260
Tom Stellarda75c6162012-01-06 17:38:37 -05002261static const struct lp_build_tgsi_action tex_action = {
2262 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002263 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002264};
2265
Michel Dänzer0495adb2013-05-06 12:45:14 +02002266static const struct lp_build_tgsi_action txq_action = {
2267 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002268 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002269 .intr_name = "llvm.SI.resinfo"
2270};
2271
Christian König206f0592013-03-20 14:37:21 +01002272static void create_meta_data(struct si_shader_context *si_shader_ctx)
2273{
2274 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2275 LLVMValueRef args[3];
2276
2277 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2278 args[1] = 0;
2279 args[2] = lp_build_const_int32(gallivm, 1);
2280
2281 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2282}
2283
Marek Olšák4f3f0432014-07-07 22:49:15 +02002284static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2285{
2286 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2287 CONST_ADDR_SPACE);
2288}
2289
Christian König55fe5cc2013-03-04 16:30:06 +01002290static void create_function(struct si_shader_context *si_shader_ctx)
2291{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002292 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2293 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002294 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002295 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002296 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002297
Christian König55fe5cc2013-03-04 16:30:06 +01002298 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002299 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002300 f32 = LLVMFloatTypeInContext(gallivm->context);
2301 v2i32 = LLVMVectorType(i32, 2);
2302 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002303 v4i32 = LLVMVectorType(i32, 4);
2304 v8i32 = LLVMVectorType(i32, 8);
2305 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002306
Marek Olšákee2a8182014-07-07 23:27:19 +02002307 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002308 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002309 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2310 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002311 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002312
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002313 switch (si_shader_ctx->type) {
2314 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002315 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002316 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002317 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002318 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002319 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002320
Michel Dänzer404b29d2013-11-21 16:45:28 +09002321 if (shader->key.vs.as_es) {
2322 params[SI_PARAM_ES2GS_OFFSET] = i32;
2323 num_params++;
2324 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002325 if (shader->is_gs_copy_shader) {
2326 last_array_pointer = SI_PARAM_CONST;
2327 num_params = SI_PARAM_CONST+1;
2328 }
2329
Michel Dänzer404b29d2013-11-21 16:45:28 +09002330 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002331
Michel Dänzer404b29d2013-11-21 16:45:28 +09002332 /* Streamout SGPRs. */
2333 if (shader->selector->so.num_outputs) {
2334 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2335 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2336 }
2337 /* A streamout buffer offset is loaded if the stride is non-zero. */
2338 for (i = 0; i < 4; i++) {
2339 if (!shader->selector->so.stride[i])
2340 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002341
Michel Dänzer404b29d2013-11-21 16:45:28 +09002342 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2343 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002344 }
2345
2346 last_sgpr = num_params-1;
2347
2348 /* VGPRs */
2349 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2350 params[num_params++] = i32; /* unused*/
2351 params[num_params++] = i32; /* unused */
2352 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002353 break;
Christian König0666ffd2013-03-05 15:07:39 +01002354
Michel Dänzer404b29d2013-11-21 16:45:28 +09002355 case TGSI_PROCESSOR_GEOMETRY:
2356 params[SI_PARAM_GS2VS_OFFSET] = i32;
2357 params[SI_PARAM_GS_WAVE_ID] = i32;
2358 last_sgpr = SI_PARAM_GS_WAVE_ID;
2359
2360 /* VGPRs */
2361 params[SI_PARAM_VTX0_OFFSET] = i32;
2362 params[SI_PARAM_VTX1_OFFSET] = i32;
2363 params[SI_PARAM_PRIMITIVE_ID] = i32;
2364 params[SI_PARAM_VTX2_OFFSET] = i32;
2365 params[SI_PARAM_VTX3_OFFSET] = i32;
2366 params[SI_PARAM_VTX4_OFFSET] = i32;
2367 params[SI_PARAM_VTX5_OFFSET] = i32;
2368 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2369 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2370 break;
2371
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002372 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002373 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002374 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002375 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002376 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2377 params[SI_PARAM_PERSP_CENTER] = v2i32;
2378 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2379 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2380 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2381 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2382 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2383 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2384 params[SI_PARAM_POS_X_FLOAT] = f32;
2385 params[SI_PARAM_POS_Y_FLOAT] = f32;
2386 params[SI_PARAM_POS_Z_FLOAT] = f32;
2387 params[SI_PARAM_POS_W_FLOAT] = f32;
2388 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002389 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002390 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2391 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002392 num_params = SI_PARAM_POS_FIXED_PT+1;
2393 break;
2394
2395 default:
2396 assert(0 && "unimplemented shader");
2397 return;
Christian Königc4973212013-03-05 12:14:02 +01002398 }
Christian König55fe5cc2013-03-04 16:30:06 +01002399
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002400 assert(num_params <= Elements(params));
2401 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002402 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002403
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002404 for (i = 0; i <= last_sgpr; ++i) {
2405 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002406
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002407 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2408 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002409 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002410 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002411 else
2412 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002413 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002414
Michel Dänzer404b29d2013-11-21 16:45:28 +09002415 if (bld_base->info &&
2416 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2417 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002418 si_shader_ctx->ddxy_lds =
2419 LLVMAddGlobalInAddressSpace(gallivm->module,
2420 LLVMArrayType(i32, 64),
2421 "ddxy_lds",
2422 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002423}
Tom Stellarda75c6162012-01-06 17:38:37 -05002424
Christian König0f6cf2b2013-03-15 15:53:25 +01002425static void preload_constants(struct si_shader_context *si_shader_ctx)
2426{
2427 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2428 struct gallivm_state * gallivm = bld_base->base.gallivm;
2429 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002430 unsigned buf;
2431 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002432
Marek Olšákee2a8182014-07-07 23:27:19 +02002433 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002434 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002435
Marek Olšák2fd42002013-10-25 11:45:47 +02002436 if (num_const == 0)
2437 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002438
Marek Olšák2fd42002013-10-25 11:45:47 +02002439 /* Allocate space for the constant values */
2440 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002441
Marek Olšák2fd42002013-10-25 11:45:47 +02002442 /* Load the resource descriptor */
2443 si_shader_ctx->const_resource[buf] =
2444 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002445
Marek Olšák2fd42002013-10-25 11:45:47 +02002446 /* Load the constants, we rely on the code sinking to do the rest */
2447 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002448 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002449 load_const(gallivm->builder,
2450 si_shader_ctx->const_resource[buf],
2451 lp_build_const_int32(gallivm, i * 4),
2452 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002453 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002454 }
2455}
2456
Christian König1c100182013-03-17 16:02:42 +01002457static void preload_samplers(struct si_shader_context *si_shader_ctx)
2458{
2459 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2460 struct gallivm_state * gallivm = bld_base->base.gallivm;
2461 const struct tgsi_shader_info * info = bld_base->info;
2462
2463 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2464
2465 LLVMValueRef res_ptr, samp_ptr;
2466 LLVMValueRef offset;
2467
2468 if (num_samplers == 0)
2469 return;
2470
2471 /* Allocate space for the values */
Marek Olšákee2a8182014-07-07 23:27:19 +02002472 si_shader_ctx->resources = CALLOC(SI_NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002473 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2474
2475 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2476 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2477
2478 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2479 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002480 /* Resource */
2481 offset = lp_build_const_int32(gallivm, i);
2482 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2483
2484 /* Sampler */
2485 offset = lp_build_const_int32(gallivm, i);
2486 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002487
2488 /* FMASK resource */
2489 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002490 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2491 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšák4855acd2013-08-06 15:08:54 +02002492 build_indexed_load(si_shader_ctx, res_ptr, offset);
2493 }
Christian König1c100182013-03-17 16:02:42 +01002494 }
2495}
2496
Marek Olšák8d03d922013-09-01 23:59:06 +02002497static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2498{
2499 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2500 struct gallivm_state * gallivm = bld_base->base.gallivm;
2501 unsigned i;
2502
Michel Dänzer67e385b2014-01-08 17:48:21 +09002503 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2504 si_shader_ctx->shader->key.vs.as_es ||
2505 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002506 return;
2507
2508 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002509 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002510
2511 /* Load the resources, we rely on the code sinking to do the rest */
2512 for (i = 0; i < 4; ++i) {
2513 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002514 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002515 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002516
2517 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2518 }
2519 }
2520}
2521
Marek Olšák1abb1a92014-09-17 22:44:22 +02002522int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2523 LLVMModuleRef mod)
Tom Stellard302f53d2012-10-25 13:50:10 -04002524{
Darren Powellbc866902014-03-31 18:00:28 -04002525 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002526 unsigned i;
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002527 unsigned char *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002528 struct radeon_shader_binary binary;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002529 bool dump = r600_can_dump_shader(&sscreen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002530 shader->selector ? shader->selector->tokens : NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002531 const char * gpu_family = r600_get_llvm_processor_name(sscreen->b.family);
Tom Stellard9ba31052014-07-14 16:49:08 -04002532 unsigned code_size;
Darren Powellbc866902014-03-31 18:00:28 -04002533
2534 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002535 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002536 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2537
2538 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002539 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002540 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002541 for (i = 0; i < binary.code_size; i+=4 ) {
2542 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2543 binary.code[i + 2], binary.code[i + 1],
2544 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002545 }
2546 }
2547
Tom Stellardd50343d2013-04-04 16:21:06 -04002548 /* XXX: We may be able to emit some of these values directly rather than
2549 * extracting fields to be emitted later.
2550 */
Darren Powellbc866902014-03-31 18:00:28 -04002551 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002552 for (i = 0; i < binary.config_size; i+= 8) {
2553 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2554 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2555 switch (reg) {
2556 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2557 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2558 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2559 case R_00B848_COMPUTE_PGM_RSRC1:
2560 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2561 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2562 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002563 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2564 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2565 break;
2566 case R_00B84C_COMPUTE_PGM_RSRC2:
2567 shader->lds_size = G_00B84C_LDS_SIZE(value);
2568 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002569 case R_0286CC_SPI_PS_INPUT_ENA:
2570 shader->spi_ps_input_ena = value;
2571 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002572 case R_00B860_COMPUTE_TMPRING_SIZE:
2573 /* WAVESIZE is in units of 256 dwords. */
2574 shader->scratch_bytes_per_wave =
2575 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2576 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002577 default:
2578 fprintf(stderr, "Warning: Compiler emitted unknown "
2579 "config register: 0x%x\n", reg);
2580 break;
2581 }
2582 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002583
2584 /* copy new shader */
Tom Stellard9ba31052014-07-14 16:49:08 -04002585 code_size = binary.code_size + binary.rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002586 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002587 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002588 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002589 if (shader->bo == NULL) {
2590 return -ENOMEM;
2591 }
2592
Marek Olšák1abb1a92014-09-17 22:44:22 +02002593 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_WRITE);
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002594 util_memcpy_cpu_to_le32(ptr, binary.code, binary.code_size);
2595 if (binary.rodata_size > 0) {
2596 ptr += binary.code_size;
2597 util_memcpy_cpu_to_le32(ptr, binary.rodata, binary.rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002598 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002599
Marek Olšák1abb1a92014-09-17 22:44:22 +02002600 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002601
Tom Stellard7782d192013-04-04 09:57:13 -07002602 free(binary.code);
2603 free(binary.config);
Tom Stellard9ba31052014-07-14 16:49:08 -04002604 free(binary.rodata);
Tom Stellard302f53d2012-10-25 13:50:10 -04002605
Darren Powellbc866902014-03-31 18:00:28 -04002606 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002607}
2608
Michel Dänzer404b29d2013-11-21 16:45:28 +09002609/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002610static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002611 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002612 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002613{
2614 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2615 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2616 struct lp_build_context *base = &bld_base->base;
2617 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002618 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002619 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002620 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002621 LLVMValueRef t_list_ptr, t_list;
2622 LLVMValueRef args[9];
2623 int i, r;
2624
Marek Olšák02134cf2014-10-04 22:07:50 +02002625 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002626
2627 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002628 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002629
2630 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2631
2632 create_meta_data(si_shader_ctx);
2633 create_function(si_shader_ctx);
2634 preload_streamout_buffers(si_shader_ctx);
2635
2636 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002637 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2638 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002639 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002640 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002641
2642 args[0] = t_list;
2643 args[1] = lp_build_mul_imm(uint,
2644 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2645 si_shader_ctx->param_vertex_id),
2646 4);
2647 args[3] = uint->zero;
2648 args[4] = uint->one; /* OFFEN */
2649 args[5] = uint->zero; /* IDXEN */
2650 args[6] = uint->one; /* GLC */
2651 args[7] = uint->one; /* SLC */
2652 args[8] = uint->zero; /* TFE */
2653
2654 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002655 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002656 unsigned chan;
2657
Marek Olšák02134cf2014-10-04 22:07:50 +02002658 outputs[i].name = gsinfo->output_semantic_name[i];
2659 outputs[i].index = i;
2660 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002661
2662 for (chan = 0; chan < 4; chan++) {
2663 args[2] = lp_build_const_int32(gallivm,
2664 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002665 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002666
2667 outputs[i].values[chan] =
2668 LLVMBuildBitCast(gallivm->builder,
2669 build_intrinsic(gallivm->builder,
2670 "llvm.SI.buffer.load.dword.i32.i32",
2671 LLVMInt32TypeInContext(gallivm->context),
2672 args, 9,
2673 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2674 base->elem_type, "");
2675 }
2676 }
2677
Marek Olšák02134cf2014-10-04 22:07:50 +02002678 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002679
2680 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2681
2682 if (dump)
2683 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2684
Marek Olšák1abb1a92014-09-17 22:44:22 +02002685 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002686 bld_base->base.gallivm->module);
2687
2688 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2689
2690 FREE(outputs);
2691 return r;
2692}
2693
Marek Olšák1abb1a92014-09-17 22:44:22 +02002694int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002695{
Marek Olšák2774abd2014-09-16 18:45:33 +02002696 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002697 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002698 struct lp_build_tgsi_context * bld_base;
2699 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002700 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002701 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002702
2703 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2704 * conversion fails. */
2705 if (dump) {
2706 tgsi_dump(sel->tokens, 0);
2707 si_dump_streamout(&sel->so);
2708 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002709
Marek Olšák8c37c162014-09-16 18:40:07 +02002710 assert(shader->nparam == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002711
Michel Dänzercfebaf92012-08-31 19:04:08 +02002712 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002713 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2714 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2715
Marek Olšák52335682014-09-30 15:12:09 +02002716 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002717 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2718
Marek Olšák52335682014-09-30 15:12:09 +02002719 shader->uses_instanceid = sel->info.uses_instanceid;
2720 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002721 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002722
2723 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002724 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002725 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2726 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002727 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002728 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2729 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2730 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002731 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002732 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002733 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2734 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002735
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002736 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2737 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002738
Michel Dänzer404b29d2013-11-21 16:45:28 +09002739 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2740 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2741
Christian Könige4ed5872013-03-21 18:02:52 +01002742 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002743 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002744 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2745 si_shader_ctx.shader = shader;
2746 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002747
Michel Dänzer51f89a02013-12-09 15:33:53 +09002748 switch (si_shader_ctx.type) {
2749 case TGSI_PROCESSOR_VERTEX:
2750 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002751 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002752 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2753 } else {
2754 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2755 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002756 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002757 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002758 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2759 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002760 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002761 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002762 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2763 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002764
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002765 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002766 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2767 shader->db_shader_control |=
2768 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2769 break;
2770 case TGSI_FS_DEPTH_LAYOUT_LESS:
2771 shader->db_shader_control |=
2772 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2773 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002774 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002775 break;
2776 default:
2777 assert(!"Unsupported shader type");
2778 return -1;
2779 }
2780
Christian König206f0592013-03-20 14:37:21 +01002781 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002782 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002783 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002784 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002785 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002786
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002787 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2788 si_shader_ctx.gs_next_vertex =
2789 lp_build_alloca(bld_base->base.gallivm,
2790 bld_base->uint_bld.elem_type, "");
2791 }
2792
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002793 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002794 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002795 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002796 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002797
2798 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2799
2800 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002801 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002802 if (r) {
2803 fprintf(stderr, "LLVM failed to compile shader\n");
2804 goto out;
2805 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002806
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002807 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002808
2809 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002810 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002811 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002812 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002813 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002814 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2815 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002816 free(shader->gs_copy_shader);
2817 shader->gs_copy_shader = NULL;
2818 goto out;
2819 }
2820 }
2821
Tom Stellarda75c6162012-01-06 17:38:37 -05002822 tgsi_parse_free(&si_shader_ctx.parse);
2823
Michel Dänzer404b29d2013-11-21 16:45:28 +09002824out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002825 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002826 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002827 FREE(si_shader_ctx.resources);
2828 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002829
Tom Stellard302f53d2012-10-25 13:50:10 -04002830 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002831}
2832
Marek Olšák2774abd2014-09-16 18:45:33 +02002833void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002834{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002835 if (shader->gs_copy_shader)
2836 si_shader_destroy(ctx, shader->gs_copy_shader);
2837
Marek Olšáka81c3e02013-08-14 01:04:39 +02002838 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002839 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002840}