blob: 8fc895a62b67cccf7e9e524ac6851e071ca48697 [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
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900236static int si_store_shader_io_attribs(struct si_shader *shader,
237 const struct tgsi_full_declaration *d)
238{
239 int i = -1;
240
241 switch (d->Declaration.File) {
242 case TGSI_FILE_INPUT:
243 i = shader->ninput++;
244 assert(i < Elements(shader->input));
245 shader->input[i].name = d->Semantic.Name;
246 shader->input[i].sid = d->Semantic.Index;
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900247 shader->input[i].index = d->Range.First;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900248 shader->input[i].interpolate = d->Interp.Interpolate;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900249 return -1;
250
251 case TGSI_FILE_OUTPUT:
252 i = shader->noutput++;
253 assert(i < Elements(shader->output));
254 shader->output[i].name = d->Semantic.Name;
255 shader->output[i].sid = d->Semantic.Index;
256 shader->output[i].index = d->Range.First;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900257 break;
258 }
259
260 return i;
261}
262
Tom Stellarda75c6162012-01-06 17:38:37 -0500263static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900264 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500265 unsigned input_index,
266 const struct tgsi_full_declaration *decl)
267{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900268 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
269 struct gallivm_state *gallivm = base->gallivm;
270 struct si_shader_context *si_shader_ctx =
271 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100272 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
273
274 unsigned chan;
275
Tom Stellarda75c6162012-01-06 17:38:37 -0500276 LLVMValueRef t_list_ptr;
277 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400278 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500279 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100280 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400281 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500282 LLVMTypeRef vec4_type;
283 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500284
Tom Stellard467f5162012-05-16 15:15:35 -0400285 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100286 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500287
Michel Dänzer51f89a02013-12-09 15:33:53 +0900288 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400289
Christian König206f0592013-03-20 14:37:21 +0100290 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400291
292 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900293 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500294
Christian Königa0dca442013-03-22 15:59:22 +0100295 if (divisor) {
296 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200297 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200298 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100299 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200300 /* Load the buffer index for vertices. */
301 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
302 si_shader_ctx->param_vertex_id);
303 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
304 SI_PARAM_BASE_VERTEX);
305 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100306 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500307
308 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400309 args[0] = t_list;
310 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100311 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900312 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100313 "llvm.SI.vs.load.input", vec4_type, args, 3,
314 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500315
316 /* Break up the vec4 into individual components */
317 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900318 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500319 /* XXX: Use a helper function for this. There is one in
320 * tgsi_llvm.c. */
321 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900322 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500323 input, llvm_chan, "");
324 }
325}
326
Michel Dänzer404b29d2013-11-21 16:45:28 +0900327static void declare_input_gs(
328 struct radeon_llvm_context *radeon_bld,
329 unsigned input_index,
330 const struct tgsi_full_declaration *decl)
331{
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900332 struct si_shader_context *si_shader_ctx =
333 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200334 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900335
336 si_store_shader_io_attribs(shader, decl);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900337}
338
339static LLVMValueRef fetch_input_gs(
340 struct lp_build_tgsi_context *bld_base,
341 const struct tgsi_full_src_register *reg,
342 enum tgsi_opcode_type type,
343 unsigned swizzle)
344{
345 struct lp_build_context *base = &bld_base->base;
346 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200347 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900348 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
349 struct gallivm_state *gallivm = base->gallivm;
350 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
351 LLVMValueRef vtx_offset;
352 LLVMValueRef t_list_ptr;
353 LLVMValueRef t_list;
354 LLVMValueRef args[9];
355 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200356 struct tgsi_shader_info *info = &shader->selector->info;
357 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
358 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900359
Marek Olšáke23fec12014-10-04 17:40:39 +0200360 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900361 if (swizzle == 0)
362 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
363 SI_PARAM_PRIMITIVE_ID);
364 else
365 return uint->zero;
366 }
367
Michel Dänzer404b29d2013-11-21 16:45:28 +0900368 if (!reg->Register.Dimension)
369 return NULL;
370
371 if (swizzle == ~0) {
372 LLVMValueRef values[TGSI_NUM_CHANNELS];
373 unsigned chan;
374 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
375 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
376 }
377 return lp_build_gather_values(bld_base->base.gallivm, values,
378 TGSI_NUM_CHANNELS);
379 }
380
381 /* Get the vertex offset parameter */
382 vtx_offset_param = reg->Dimension.Index;
383 if (vtx_offset_param < 2) {
384 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
385 } else {
386 assert(vtx_offset_param < 6);
387 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
388 }
389 vtx_offset = lp_build_mul_imm(uint,
390 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
391 vtx_offset_param),
392 4);
393
394 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +0900395 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
396 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900397 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900398 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900399
400 args[0] = t_list;
401 args[1] = vtx_offset;
402 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200403 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200404 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900405 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900406 args[3] = uint->zero;
407 args[4] = uint->one; /* OFFEN */
408 args[5] = uint->zero; /* IDXEN */
409 args[6] = uint->one; /* GLC */
410 args[7] = uint->zero; /* SLC */
411 args[8] = uint->zero; /* TFE */
412
413 return LLVMBuildBitCast(gallivm->builder,
414 build_intrinsic(gallivm->builder,
415 "llvm.SI.buffer.load.dword.i32.i32",
416 i32, args, 9,
417 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
418 tgsi2llvmtype(bld_base, type), "");
419}
420
Tom Stellarda75c6162012-01-06 17:38:37 -0500421static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900422 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500423 unsigned input_index,
424 const struct tgsi_full_declaration *decl)
425{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900426 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
427 struct si_shader_context *si_shader_ctx =
428 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200429 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900430 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
431 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400432 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900433 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100434
435 LLVMValueRef interp_param;
436 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500437
438 /* This value is:
439 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
440 * quad begins a new primitive. Bit 0 always needs
441 * to be unset)
442 * [32:16] ParamOffset
443 *
444 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900445 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200446 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500447
Christian König0666ffd2013-03-05 15:07:39 +0100448 unsigned chan;
449
Tom Stellard0fb1e682012-09-06 16:18:11 -0400450 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
451 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400452 unsigned soa_index =
453 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900454 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100455 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100456
457 if (chan == 3)
458 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900459 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100460 LLVMBuildFDiv(gallivm->builder,
461 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900462 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100463 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400464 }
465 return;
466 }
467
Michel Dänzer97078b12012-09-25 12:41:31 +0200468 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
469 LLVMValueRef face, is_face_positive;
470
Christian König0666ffd2013-03-05 15:07:39 +0100471 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
472
Michel Dänzer97078b12012-09-25 12:41:31 +0200473 is_face_positive = LLVMBuildFCmp(gallivm->builder,
474 LLVMRealUGT, face,
475 lp_build_const_float(gallivm, 0.0f),
476 "");
477
Michel Dänzer51f89a02013-12-09 15:33:53 +0900478 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200479 LLVMBuildSelect(gallivm->builder,
480 is_face_positive,
481 lp_build_const_float(gallivm, 1.0f),
482 lp_build_const_float(gallivm, 0.0f),
483 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900484 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
485 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200486 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900487 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200488 lp_build_const_float(gallivm, 1.0f);
489
490 return;
491 }
492
Marek Olšák8b057dd2014-10-04 22:15:07 +0200493 shader->ps_input_param_offset[input_index] = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200494 attr_number = lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200495 shader->ps_input_param_offset[input_index]);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200496
Francisco Jerez12799232012-04-30 18:27:52 +0200497 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500498 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100499 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500500 break;
501 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200502 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200503 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400504 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100505 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200506 else
Christian König0666ffd2013-03-05 15:07:39 +0100507 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200508 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200509 case TGSI_INTERPOLATE_COLOR:
510 if (si_shader_ctx->shader->key.ps.flatshade) {
511 interp_param = 0;
512 break;
513 }
514 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200515 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200516 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200517 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400518 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100519 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200520 else
Christian König0666ffd2013-03-05 15:07:39 +0100521 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500522 break;
523 default:
524 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
525 return;
526 }
527
Christian König0666ffd2013-03-05 15:07:39 +0100528 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
529
Tom Stellarda75c6162012-01-06 17:38:37 -0500530 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200531 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100532 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100533 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200534 LLVMValueRef face, is_face_positive;
535 LLVMValueRef back_attr_number =
536 lp_build_const_int32(gallivm,
Marek Olšák8b057dd2014-10-04 22:15:07 +0200537 shader->ps_input_param_offset[input_index] + 1);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200538
Christian König0666ffd2013-03-05 15:07:39 +0100539 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
540
Michel Dänzer691f08d2012-09-06 18:03:38 +0200541 is_face_positive = LLVMBuildFCmp(gallivm->builder,
542 LLVMRealUGT, face,
543 lp_build_const_float(gallivm, 0.0f),
544 "");
545
Tom Stellarda75c6162012-01-06 17:38:37 -0500546 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100547 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200548 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
549 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
550 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
551 LLVMValueRef front, back;
552
553 args[0] = llvm_chan;
554 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900555 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100556 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100557 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200558
559 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900560 back = 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
Michel Dänzer51f89a02013-12-09 15:33:53 +0900564 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200565 LLVMBuildSelect(gallivm->builder,
566 is_face_positive,
567 front,
568 back,
569 "");
570 }
571
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900572 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200573 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
574 LLVMValueRef args[4];
575
576 args[0] = uint->zero;
577 args[1] = attr_number;
578 args[2] = params;
579 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900580 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
581 build_intrinsic(gallivm->builder, intr_name,
582 input_type, args, args[3] ? 4 : 3,
583 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
584 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
585 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200586 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900587 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200588 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200589 } else {
590 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100591 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200592 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
593 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
594 args[0] = llvm_chan;
595 args[1] = attr_number;
596 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100597 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900598 radeon_bld->inputs[soa_index] =
599 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100600 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100601 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200602 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500603 }
604}
605
Marek Olšák5b06fc32014-05-06 18:12:40 +0200606static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
607{
608 struct gallivm_state *gallivm = &radeon_bld->gallivm;
609 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
610 SI_PARAM_ANCILLARY);
611 value = LLVMBuildLShr(gallivm->builder, value,
612 lp_build_const_int32(gallivm, 8), "");
613 value = LLVMBuildAnd(gallivm->builder, value,
614 lp_build_const_int32(gallivm, 0xf), "");
615 return value;
616}
617
Marek Olšák250aa932014-05-06 14:10:47 +0200618static LLVMValueRef load_const(LLVMBuilderRef builder, LLVMValueRef resource,
619 LLVMValueRef offset, LLVMTypeRef return_type)
620{
621 LLVMValueRef args[2] = {resource, offset};
622
623 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
624 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
625}
626
Christian Könige4ed5872013-03-21 18:02:52 +0100627static void declare_system_value(
628 struct radeon_llvm_context * radeon_bld,
629 unsigned index,
630 const struct tgsi_full_declaration *decl)
631{
Marek Olšák8d03d922013-09-01 23:59:06 +0200632 struct si_shader_context *si_shader_ctx =
633 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200634 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
635 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100636 LLVMValueRef value = 0;
637
638 switch (decl->Semantic.Name) {
639 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200640 value = LLVMGetParam(radeon_bld->main_fn,
641 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100642 break;
643
644 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200645 value = LLVMGetParam(radeon_bld->main_fn,
646 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100647 break;
648
Marek Olšák5b06fc32014-05-06 18:12:40 +0200649 case TGSI_SEMANTIC_SAMPLEID:
650 value = get_sample_id(radeon_bld);
651 break;
652
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200653 case TGSI_SEMANTIC_SAMPLEPOS:
654 {
655 LLVMBuilderRef builder = gallivm->builder;
656 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200657 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200658 LLVMValueRef resource = build_indexed_load(si_shader_ctx, desc, buf_index);
659
660 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
661 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
662 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
663
664 LLVMValueRef pos[4] = {
665 load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
666 load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
667 lp_build_const_float(gallivm, 0),
668 lp_build_const_float(gallivm, 0)
669 };
670 value = lp_build_gather_values(gallivm, pos, 4);
671 break;
672 }
673
Christian Könige4ed5872013-03-21 18:02:52 +0100674 default:
675 assert(!"unknown system value");
676 return;
677 }
678
679 radeon_bld->system_values[index] = value;
680}
681
Tom Stellarda75c6162012-01-06 17:38:37 -0500682static LLVMValueRef fetch_constant(
683 struct lp_build_tgsi_context * bld_base,
684 const struct tgsi_full_src_register *reg,
685 enum tgsi_opcode_type type,
686 unsigned swizzle)
687{
Christian König55fe5cc2013-03-04 16:30:06 +0100688 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500689 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100690 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200691 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500692
Christian König0f6cf2b2013-03-15 15:53:25 +0100693 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100694 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500695
Christian König8514f5a2013-02-04 17:46:42 +0100696 if (swizzle == LP_CHAN_ALL) {
697 unsigned chan;
698 LLVMValueRef values[4];
699 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
700 values[chan] = fetch_constant(bld_base, reg, type, chan);
701
702 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
703 }
704
Marek Olšák2fd42002013-10-25 11:45:47 +0200705 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100706 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100707
Marek Olšák2fd42002013-10-25 11:45:47 +0200708 if (!reg->Register.Indirect)
709 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
710
Christian König0f6cf2b2013-03-15 15:53:25 +0100711 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
712 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
713 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200714 addr = lp_build_add(&bld_base->uint_bld, addr,
715 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200716
Marek Olšák250aa932014-05-06 14:10:47 +0200717 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
718 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500719
Christian Königf5298b02013-02-28 14:50:07 +0100720 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500721}
722
Michel Dänzer26c71392012-08-24 12:03:11 +0200723/* Initialize arguments for the shader export intrinsic */
724static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900725 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200726 unsigned target,
727 LLVMValueRef *args)
728{
729 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
730 struct lp_build_context *uint =
731 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
732 struct lp_build_context *base = &bld_base->base;
733 unsigned compressed = 0;
734 unsigned chan;
735
Michel Dänzerf402acd2012-08-22 18:15:36 +0200736 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
737 int cbuf = target - V_008DFC_SQ_EXP_MRT;
738
739 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100740 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100741
742 if (compressed)
743 si_shader_ctx->shader->spi_shader_col_format |=
744 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
745 else
746 si_shader_ctx->shader->spi_shader_col_format |=
747 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200748
749 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200750 }
751 }
752
753 if (compressed) {
754 /* Pixel shader needs to pack output values before export */
755 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900756 args[0] = values[2 * chan];
757 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200758 args[chan + 5] =
759 build_intrinsic(base->gallivm->builder,
760 "llvm.SI.packf16",
761 LLVMInt32TypeInContext(base->gallivm->context),
762 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100763 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100764 args[chan + 7] = args[chan + 5] =
765 LLVMBuildBitCast(base->gallivm->builder,
766 args[chan + 5],
767 LLVMFloatTypeInContext(base->gallivm->context),
768 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200769 }
770
771 /* Set COMPR flag */
772 args[4] = uint->one;
773 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900774 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200775 /* +5 because the first output value will be
776 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900777 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200778
779 /* Clear COMPR flag */
780 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200781 }
782
783 /* XXX: This controls which components of the output
784 * registers actually get exported. (e.g bit 0 means export
785 * X component, bit 1 means export Y component, etc.) I'm
786 * hard coding this to 0xf for now. In the future, we might
787 * want to do something else. */
788 args[0] = lp_build_const_int32(base->gallivm, 0xf);
789
790 /* Specify whether the EXEC mask represents the valid mask */
791 args[1] = uint->zero;
792
793 /* Specify whether this is the last export */
794 args[2] = uint->zero;
795
796 /* Specify the target we are exporting */
797 args[3] = lp_build_const_int32(base->gallivm, target);
798
Michel Dänzer26c71392012-08-24 12:03:11 +0200799 /* XXX: We probably need to keep track of the output
800 * values, so we know what we are passing to the next
801 * stage. */
802}
803
Michel Dänzer404b29d2013-11-21 16:45:28 +0900804/* Load from output pointers and initialize arguments for the shader export intrinsic */
805static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
806 LLVMValueRef *out_ptr,
807 unsigned target,
808 LLVMValueRef *args)
809{
810 struct gallivm_state *gallivm = bld_base->base.gallivm;
811 LLVMValueRef values[4];
812 int i;
813
814 for (i = 0; i < 4; i++)
815 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
816
817 si_llvm_init_export_args(bld_base, values, target, args);
818}
819
Michel Dänzer7708a862012-11-02 15:57:30 +0100820static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900821 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100822{
823 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
824 struct gallivm_state *gallivm = bld_base->base.gallivm;
825
Christian Königa0dca442013-03-22 15:59:22 +0100826 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400827 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
828 SI_PARAM_ALPHA_REF);
829
Michel Dänzer7708a862012-11-02 15:57:30 +0100830 LLVMValueRef alpha_pass =
831 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100832 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900833 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400834 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100835 LLVMValueRef arg =
836 lp_build_select(&bld_base->base,
837 alpha_pass,
838 lp_build_const_float(gallivm, 1.0f),
839 lp_build_const_float(gallivm, -1.0f));
840
841 build_intrinsic(gallivm->builder,
842 "llvm.AMDGPU.kill",
843 LLVMVoidTypeInContext(gallivm->context),
844 &arg, 1, 0);
845 } else {
846 build_intrinsic(gallivm->builder,
847 "llvm.AMDGPU.kilp",
848 LLVMVoidTypeInContext(gallivm->context),
849 NULL, 0, 0);
850 }
Marek Olšákadc57972014-09-19 17:07:07 +0200851
852 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100853}
854
Michel Dänzere3befbc2013-05-15 18:09:50 +0200855static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900856 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200857{
858 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200859 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200860 struct lp_build_context *base = &bld_base->base;
861 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200862 unsigned reg_index;
863 unsigned chan;
864 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200865 LLVMValueRef base_elt;
866 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200867 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák2fd42002013-10-25 11:45:47 +0200868 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200869
Michel Dänzere3befbc2013-05-15 18:09:50 +0200870 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200871 LLVMValueRef *args = pos[2 + reg_index];
872
Marek Olšák8c37c162014-09-16 18:40:07 +0200873 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200874
Michel Dänzere3befbc2013-05-15 18:09:50 +0200875 args[5] =
876 args[6] =
877 args[7] =
878 args[8] = lp_build_const_float(base->gallivm, 0.0f);
879
880 /* Compute dot products of position and user clip plane vectors */
881 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
882 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200883 args[1] = lp_build_const_int32(base->gallivm,
884 ((reg_index * 4 + chan) * 4 +
885 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200886 base_elt = load_const(base->gallivm->builder, const_resource,
887 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200888 args[5 + chan] =
889 lp_build_add(base, args[5 + chan],
890 lp_build_mul(base, base_elt,
891 out_elts[const_chan]));
892 }
893 }
894
895 args[0] = lp_build_const_int32(base->gallivm, 0xf);
896 args[1] = uint->zero;
897 args[2] = uint->zero;
898 args[3] = lp_build_const_int32(base->gallivm,
899 V_008DFC_SQ_EXP_POS + 2 + reg_index);
900 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200901 }
902}
903
Marek Olšák8d03d922013-09-01 23:59:06 +0200904static void si_dump_streamout(struct pipe_stream_output_info *so)
905{
906 unsigned i;
907
908 if (so->num_outputs)
909 fprintf(stderr, "STREAMOUT\n");
910
911 for (i = 0; i < so->num_outputs; i++) {
912 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
913 so->output[i].start_component;
914 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
915 i, so->output[i].output_buffer,
916 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
917 so->output[i].register_index,
918 mask & 1 ? "x" : "",
919 mask & 2 ? "y" : "",
920 mask & 4 ? "z" : "",
921 mask & 8 ? "w" : "");
922 }
923}
924
925/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
926 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
927 * or v4i32 (num_channels=3,4). */
928static void build_tbuffer_store(struct si_shader_context *shader,
929 LLVMValueRef rsrc,
930 LLVMValueRef vdata,
931 unsigned num_channels,
932 LLVMValueRef vaddr,
933 LLVMValueRef soffset,
934 unsigned inst_offset,
935 unsigned dfmt,
936 unsigned nfmt,
937 unsigned offen,
938 unsigned idxen,
939 unsigned glc,
940 unsigned slc,
941 unsigned tfe)
942{
943 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
944 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
945 LLVMValueRef args[] = {
946 rsrc,
947 vdata,
948 LLVMConstInt(i32, num_channels, 0),
949 vaddr,
950 soffset,
951 LLVMConstInt(i32, inst_offset, 0),
952 LLVMConstInt(i32, dfmt, 0),
953 LLVMConstInt(i32, nfmt, 0),
954 LLVMConstInt(i32, offen, 0),
955 LLVMConstInt(i32, idxen, 0),
956 LLVMConstInt(i32, glc, 0),
957 LLVMConstInt(i32, slc, 0),
958 LLVMConstInt(i32, tfe, 0)
959 };
960
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900961 /* The instruction offset field has 12 bits */
962 assert(offen || inst_offset < (1 << 12));
963
Marek Olšák8d03d922013-09-01 23:59:06 +0200964 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
965 unsigned func = CLAMP(num_channels, 1, 3) - 1;
966 const char *types[] = {"i32", "v2i32", "v4i32"};
967 char name[256];
968 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
969
970 lp_build_intrinsic(gallivm->builder, name,
971 LLVMVoidTypeInContext(gallivm->context),
972 args, Elements(args));
973}
974
975static void build_streamout_store(struct si_shader_context *shader,
976 LLVMValueRef rsrc,
977 LLVMValueRef vdata,
978 unsigned num_channels,
979 LLVMValueRef vaddr,
980 LLVMValueRef soffset,
981 unsigned inst_offset)
982{
983 static unsigned dfmt[] = {
984 V_008F0C_BUF_DATA_FORMAT_32,
985 V_008F0C_BUF_DATA_FORMAT_32_32,
986 V_008F0C_BUF_DATA_FORMAT_32_32_32,
987 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
988 };
989 assert(num_channels >= 1 && num_channels <= 4);
990
991 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
992 inst_offset, dfmt[num_channels-1],
993 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
994}
995
996/* On SI, the vertex shader is responsible for writing streamout data
997 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900998static void si_llvm_emit_streamout(struct si_shader_context *shader,
999 struct si_shader_output_values *outputs,
1000 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +02001001{
1002 struct pipe_stream_output_info *so = &shader->shader->selector->so;
1003 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
1004 LLVMBuilderRef builder = gallivm->builder;
1005 int i, j;
1006 struct lp_build_if_state if_ctx;
1007
1008 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1009
1010 LLVMValueRef so_param =
1011 LLVMGetParam(shader->radeon_bld.main_fn,
1012 shader->param_streamout_config);
1013
1014 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
1015 LLVMValueRef so_vtx_count =
1016 LLVMBuildAnd(builder,
1017 LLVMBuildLShr(builder, so_param,
1018 LLVMConstInt(i32, 16, 0), ""),
1019 LLVMConstInt(i32, 127, 0), "");
1020
1021 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
1022 NULL, 0, LLVMReadNoneAttribute);
1023
1024 /* can_emit = tid < so_vtx_count; */
1025 LLVMValueRef can_emit =
1026 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
1027
1028 /* Emit the streamout code conditionally. This actually avoids
1029 * out-of-bounds buffer access. The hw tells us via the SGPR
1030 * (so_vtx_count) which threads are allowed to emit streamout data. */
1031 lp_build_if(&if_ctx, gallivm, can_emit);
1032 {
1033 /* The buffer offset is computed as follows:
1034 * ByteOffset = streamout_offset[buffer_id]*4 +
1035 * (streamout_write_index + thread_id)*stride[buffer_id] +
1036 * attrib_offset
1037 */
1038
1039 LLVMValueRef so_write_index =
1040 LLVMGetParam(shader->radeon_bld.main_fn,
1041 shader->param_streamout_write_index);
1042
1043 /* Compute (streamout_write_index + thread_id). */
1044 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1045
1046 /* Compute the write offset for each enabled buffer. */
1047 LLVMValueRef so_write_offset[4] = {};
1048 for (i = 0; i < 4; i++) {
1049 if (!so->stride[i])
1050 continue;
1051
1052 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1053 shader->param_streamout_offset[i]);
1054 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1055
1056 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1057 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1058 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1059 }
1060
Marek Olšák8d03d922013-09-01 23:59:06 +02001061 /* Write streamout data. */
1062 for (i = 0; i < so->num_outputs; i++) {
1063 unsigned buf_idx = so->output[i].output_buffer;
1064 unsigned reg = so->output[i].register_index;
1065 unsigned start = so->output[i].start_component;
1066 unsigned num_comps = so->output[i].num_components;
1067 LLVMValueRef out[4];
1068
1069 assert(num_comps && num_comps <= 4);
1070 if (!num_comps || num_comps > 4)
1071 continue;
1072
1073 /* Load the output as int. */
1074 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001075 unsigned outidx = 0;
1076
1077 while (outidx < noutput && outputs[outidx].index != reg)
1078 outidx++;
1079
1080 if (outidx < noutput)
1081 out[j] = LLVMBuildBitCast(builder,
1082 outputs[outidx].values[start+j],
1083 i32, "");
1084 else
1085 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +02001086 }
1087
Michel Dänzer67e385b2014-01-08 17:48:21 +09001088 if (!out[0])
1089 continue;
1090
Marek Olšák8d03d922013-09-01 23:59:06 +02001091 /* Pack the output. */
1092 LLVMValueRef vdata = NULL;
1093
1094 switch (num_comps) {
1095 case 1: /* as i32 */
1096 vdata = out[0];
1097 break;
1098 case 2: /* as v2i32 */
1099 case 3: /* as v4i32 (aligned to 4) */
1100 case 4: /* as v4i32 */
1101 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1102 for (j = 0; j < num_comps; j++) {
1103 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1104 LLVMConstInt(i32, j, 0), "");
1105 }
1106 break;
1107 }
1108
1109 build_streamout_store(shader, shader->so_buffers[buf_idx],
1110 vdata, num_comps,
1111 so_write_offset[buf_idx],
1112 LLVMConstInt(i32, 0, 0),
1113 so->output[i].dst_offset*4);
1114 }
1115 }
1116 lp_build_endif(&if_ctx);
1117}
1118
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001119
Michel Dänzer404b29d2013-11-21 16:45:28 +09001120/* Generate export instructions for hardware VS shader stage */
1121static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1122 struct si_shader_output_values *outputs,
1123 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001124{
1125 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001126 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001127 struct lp_build_context * base = &bld_base->base;
1128 struct lp_build_context * uint =
1129 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001130 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001131 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001132 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001133 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001134 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001135 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001136 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001137 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001138
Michel Dänzer67e385b2014-01-08 17:48:21 +09001139 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1140 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001141 }
1142
Michel Dänzer404b29d2013-11-21 16:45:28 +09001143 for (i = 0; i < noutput; i++) {
1144 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001145 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001146
Michel Dänzer0afeea52013-05-02 14:53:17 +02001147handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001148 /* Select the correct target */
1149 switch(semantic_name) {
1150 case TGSI_SEMANTIC_PSIZE:
1151 shader->vs_out_misc_write = true;
1152 shader->vs_out_point_size = true;
1153 psize_value = outputs[i].values[0];
1154 continue;
1155 case TGSI_SEMANTIC_EDGEFLAG:
1156 shader->vs_out_misc_write = true;
1157 shader->vs_out_edgeflag = true;
1158 edgeflag_value = outputs[i].values[0];
1159 continue;
1160 case TGSI_SEMANTIC_LAYER:
1161 shader->vs_out_misc_write = true;
1162 shader->vs_out_layer = true;
1163 layer_value = outputs[i].values[0];
1164 continue;
1165 case TGSI_SEMANTIC_POSITION:
1166 target = V_008DFC_SQ_EXP_POS;
1167 break;
1168 case TGSI_SEMANTIC_COLOR:
1169 case TGSI_SEMANTIC_BCOLOR:
1170 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001171 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001172 param_count++;
1173 break;
1174 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001175 shader->clip_dist_write |=
Marek Olšáka9592cd2014-10-04 19:09:09 +02001176 0xf << (semantic_index * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001177 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1178 break;
1179 case TGSI_SEMANTIC_CLIPVERTEX:
1180 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1181 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001182 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001183 case TGSI_SEMANTIC_FOG:
1184 case TGSI_SEMANTIC_GENERIC:
1185 target = V_008DFC_SQ_EXP_PARAM + param_count;
Marek Olšák8b057dd2014-10-04 22:15:07 +02001186 shader->vs_output_param_offset[i] = param_count;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001187 param_count++;
1188 break;
1189 default:
1190 target = 0;
1191 fprintf(stderr,
1192 "Warning: SI unhandled vs output type:%d\n",
1193 semantic_name);
1194 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001195
Michel Dänzer404b29d2013-11-21 16:45:28 +09001196 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001197
Michel Dänzer404b29d2013-11-21 16:45:28 +09001198 if (target >= V_008DFC_SQ_EXP_POS &&
1199 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1200 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1201 args, sizeof(args));
1202 } else {
1203 lp_build_intrinsic(base->gallivm->builder,
1204 "llvm.SI.export",
1205 LLVMVoidTypeInContext(base->gallivm->context),
1206 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001207 }
1208
1209 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1210 semantic_name = TGSI_SEMANTIC_GENERIC;
1211 goto handle_semantic;
1212 }
1213 }
1214
1215 /* We need to add the position output manually if it's missing. */
1216 if (!pos_args[0][0]) {
1217 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1218 pos_args[0][1] = uint->zero; /* EXEC mask */
1219 pos_args[0][2] = uint->zero; /* last export? */
1220 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1221 pos_args[0][4] = uint->zero; /* COMPR flag */
1222 pos_args[0][5] = base->zero; /* X */
1223 pos_args[0][6] = base->zero; /* Y */
1224 pos_args[0][7] = base->zero; /* Z */
1225 pos_args[0][8] = base->one; /* W */
1226 }
1227
1228 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1229 if (shader->vs_out_misc_write) {
1230 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1231 shader->vs_out_point_size |
1232 (shader->vs_out_edgeflag << 1) |
1233 (shader->vs_out_layer << 2));
1234 pos_args[1][1] = uint->zero; /* EXEC mask */
1235 pos_args[1][2] = uint->zero; /* last export? */
1236 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1237 pos_args[1][4] = uint->zero; /* COMPR flag */
1238 pos_args[1][5] = base->zero; /* X */
1239 pos_args[1][6] = base->zero; /* Y */
1240 pos_args[1][7] = base->zero; /* Z */
1241 pos_args[1][8] = base->zero; /* W */
1242
Michel Dänzer404b29d2013-11-21 16:45:28 +09001243 if (shader->vs_out_point_size)
1244 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001245
1246 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001247 /* The output is a float, but the hw expects an integer
1248 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001249 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1250 edgeflag_value,
1251 bld_base->uint_bld.elem_type, "");
1252 edgeflag_value = lp_build_min(&bld_base->int_bld,
1253 edgeflag_value,
1254 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001255
1256 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001257 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1258 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001259 base->elem_type, "");
1260 }
1261
Michel Dänzer404b29d2013-11-21 16:45:28 +09001262 if (shader->vs_out_layer)
1263 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001264 }
1265
1266 for (i = 0; i < 4; i++)
1267 if (pos_args[i][0])
1268 shader->nr_pos_exports++;
1269
1270 pos_idx = 0;
1271 for (i = 0; i < 4; i++) {
1272 if (!pos_args[i][0])
1273 continue;
1274
1275 /* Specify the target we are exporting */
1276 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1277
1278 if (pos_idx == shader->nr_pos_exports)
1279 /* Specify that this is the last export */
1280 pos_args[i][2] = uint->one;
1281
1282 lp_build_intrinsic(base->gallivm->builder,
1283 "llvm.SI.export",
1284 LLVMVoidTypeInContext(base->gallivm->context),
1285 pos_args[i], 9);
1286 }
1287}
1288
Michel Dänzer404b29d2013-11-21 16:45:28 +09001289static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1290{
1291 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1292 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001293 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001294 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001295 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001296 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1297 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001298 LLVMValueRef t_list_ptr;
1299 LLVMValueRef t_list;
1300 unsigned chan;
1301 int i;
1302
Michel Dänzer404b29d2013-11-21 16:45:28 +09001303 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001304 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1305 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001306 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001307 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001308
Marek Olšák216cf862014-10-04 17:04:05 +02001309 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001310 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001311 si_shader_ctx->radeon_bld.soa.outputs[i];
1312 int param_index = get_param_index(info->output_semantic_name[i],
1313 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001314 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001315
Marek Olšáke29353f2014-09-17 22:17:02 +02001316 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001317 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001318
1319 for (chan = 0; chan < 4; chan++) {
1320 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001321 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1322
1323 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001324 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001325 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001326 V_008F0C_BUF_DATA_FORMAT_32,
1327 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001328 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001329 }
1330 }
1331}
1332
1333static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1334{
1335 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1336 struct gallivm_state *gallivm = bld_base->base.gallivm;
1337 LLVMValueRef args[2];
1338
1339 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1340 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1341 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1342 LLVMVoidTypeInContext(gallivm->context), args, 2,
1343 LLVMNoUnwindAttribute);
1344}
1345
1346static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1347{
1348 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1349 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001350 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001351 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1352 struct si_shader_output_values *outputs = NULL;
1353 unsigned noutput = 0;
1354 int i;
1355
1356 while (!tgsi_parse_end_of_tokens(parse)) {
1357 struct tgsi_full_declaration *d =
1358 &parse->FullToken.FullDeclaration;
1359 unsigned index;
1360
1361 tgsi_parse_token(parse);
1362
1363 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1364 continue;
1365
Marek Olšák8c37c162014-09-16 18:40:07 +02001366 i = si_store_shader_io_attribs(shader, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001367 if (i < 0)
1368 continue;
1369
1370 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1371 (noutput + 1) * sizeof(outputs[0]));
1372 for (index = d->Range.First; index <= d->Range.Last; index++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001373 outputs[noutput].index = index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001374 outputs[noutput].name = d->Semantic.Name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001375 outputs[noutput].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001376
1377 for (i = 0; i < 4; i++)
1378 outputs[noutput].values[i] =
1379 LLVMBuildLoad(gallivm->builder,
1380 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1381 "");
1382 }
1383 noutput++;
1384 }
1385
1386 si_llvm_export_vs(bld_base, outputs, noutput);
1387 FREE(outputs);
1388}
1389
Michel Dänzer51f89a02013-12-09 15:33:53 +09001390static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1391{
1392 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001393 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001394 struct lp_build_context * base = &bld_base->base;
1395 struct lp_build_context * uint =
1396 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1397 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1398 LLVMValueRef args[9];
1399 LLVMValueRef last_args[9] = { 0 };
1400 unsigned semantic_name;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001401 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001402 int i;
1403
1404 while (!tgsi_parse_end_of_tokens(parse)) {
1405 struct tgsi_full_declaration *d =
1406 &parse->FullToken.FullDeclaration;
1407 unsigned target;
1408 unsigned index;
1409
1410 tgsi_parse_token(parse);
1411
Michel Dänzer51f89a02013-12-09 15:33:53 +09001412 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1413 continue;
1414
1415 i = si_store_shader_io_attribs(shader, d);
1416 if (i < 0)
1417 continue;
1418
1419 semantic_name = d->Semantic.Name;
1420 for (index = d->Range.First; index <= d->Range.Last; index++) {
1421 /* Select the correct target */
1422 switch(semantic_name) {
1423 case TGSI_SEMANTIC_POSITION:
1424 depth_index = index;
1425 continue;
1426 case TGSI_SEMANTIC_STENCIL:
1427 stencil_index = index;
1428 continue;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001429 case TGSI_SEMANTIC_SAMPLEMASK:
1430 samplemask_index = index;
1431 continue;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001432 case TGSI_SEMANTIC_COLOR:
1433 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1434 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001435 LLVMBuildStore(bld_base->base.gallivm->builder,
1436 bld_base->base.one,
1437 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1438
Michel Dänzer51f89a02013-12-09 15:33:53 +09001439 if (d->Semantic.Index == 0 &&
1440 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001441 si_alpha_test(bld_base,
1442 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001443 break;
1444 default:
1445 target = 0;
1446 fprintf(stderr,
1447 "Warning: SI unhandled fs output type:%d\n",
1448 semantic_name);
1449 }
1450
Michel Dänzer404b29d2013-11-21 16:45:28 +09001451 si_llvm_init_export_args_load(bld_base,
1452 si_shader_ctx->radeon_bld.soa.outputs[index],
1453 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001454
1455 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001456 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001457 if (last_args[0]) {
1458 lp_build_intrinsic(base->gallivm->builder,
1459 "llvm.SI.export",
1460 LLVMVoidTypeInContext(base->gallivm->context),
1461 last_args, 9);
1462 }
1463
Marek Olšák0eb528a2013-12-04 13:24:22 +01001464 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001465 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001466
1467 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02001468 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
Marek Olšák7a645c52014-10-04 22:09:16 +02001469 d->Semantic.Index == 0 &&
Marek Olšák34e82002014-10-04 20:44:23 +02001470 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1471 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001472 si_llvm_init_export_args_load(bld_base,
1473 si_shader_ctx->radeon_bld.soa.outputs[index],
1474 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001475 lp_build_intrinsic(base->gallivm->builder,
1476 "llvm.SI.export",
1477 LLVMVoidTypeInContext(base->gallivm->context),
1478 args, 9);
1479 }
1480 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001481 } else {
1482 lp_build_intrinsic(base->gallivm->builder,
1483 "llvm.SI.export",
1484 LLVMVoidTypeInContext(base->gallivm->context),
1485 args, 9);
1486 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001487 }
1488 }
1489
Marek Olšákd0e8b652014-05-06 20:04:31 +02001490 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001491 LLVMValueRef out_ptr;
1492 unsigned mask = 0;
1493
1494 /* Specify the target we are exporting */
1495 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1496
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001497 args[5] = base->zero; /* R, depth */
1498 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1499 args[7] = base->zero; /* B, sample mask */
1500 args[8] = base->zero; /* A, alpha to mask */
1501
Michel Dänzer1a616c12012-11-13 17:35:09 +01001502 if (depth_index >= 0) {
1503 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1504 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1505 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001506 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001507 }
1508
1509 if (stencil_index >= 0) {
1510 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001511 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001512 /* Only setting the stencil component bit (0x2) here
1513 * breaks some stencil piglit tests
1514 */
1515 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001516 si_shader_ctx->shader->db_shader_control |=
1517 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001518 }
1519
Marek Olšákd0e8b652014-05-06 20:04:31 +02001520 if (samplemask_index >= 0) {
1521 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1522 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1523 mask |= 0xf; /* Set all components. */
1524 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1525 }
1526
1527 if (samplemask_index >= 0)
1528 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1529 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001530 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1531 else
1532 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1533
Michel Dänzer1a616c12012-11-13 17:35:09 +01001534 /* Specify which components to enable */
1535 args[0] = lp_build_const_int32(base->gallivm, mask);
1536
1537 args[1] =
1538 args[2] =
1539 args[4] = uint->zero;
1540
1541 if (last_args[0])
1542 lp_build_intrinsic(base->gallivm->builder,
1543 "llvm.SI.export",
1544 LLVMVoidTypeInContext(base->gallivm->context),
1545 args, 9);
1546 else
1547 memcpy(last_args, args, sizeof(args));
1548 }
1549
Michel Dänzer51f89a02013-12-09 15:33:53 +09001550 if (!last_args[0]) {
1551 /* Specify which components to enable */
1552 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001553
Michel Dänzer51f89a02013-12-09 15:33:53 +09001554 /* Specify the target we are exporting */
1555 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001556
Michel Dänzer51f89a02013-12-09 15:33:53 +09001557 /* Set COMPR flag to zero to export data as 32-bit */
1558 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001559
Michel Dänzer51f89a02013-12-09 15:33:53 +09001560 /* dummy bits */
1561 last_args[5]= uint->zero;
1562 last_args[6]= uint->zero;
1563 last_args[7]= uint->zero;
1564 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001565 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001566
1567 /* Specify whether the EXEC mask represents the valid mask */
1568 last_args[1] = uint->one;
1569
1570 /* Specify that this is the last export */
1571 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1572
1573 lp_build_intrinsic(base->gallivm->builder,
1574 "llvm.SI.export",
1575 LLVMVoidTypeInContext(base->gallivm->context),
1576 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001577}
1578
Marek Olšák4855acd2013-08-06 15:08:54 +02001579static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1580 struct lp_build_tgsi_context * bld_base,
1581 struct lp_build_emit_data * emit_data);
1582
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001583static bool tgsi_is_shadow_sampler(unsigned target)
1584{
1585 return target == TGSI_TEXTURE_SHADOW1D ||
1586 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1587 target == TGSI_TEXTURE_SHADOW2D ||
1588 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1589 target == TGSI_TEXTURE_SHADOWCUBE ||
1590 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1591 target == TGSI_TEXTURE_SHADOWRECT;
1592}
1593
Marek Olšák877bb522014-06-25 03:12:46 +02001594static const struct lp_build_tgsi_action tex_action;
1595
Tom Stellarda75c6162012-01-06 17:38:37 -05001596static void tex_fetch_args(
1597 struct lp_build_tgsi_context * bld_base,
1598 struct lp_build_emit_data * emit_data)
1599{
Christian König55fe5cc2013-03-04 16:30:06 +01001600 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001601 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001602 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001603 unsigned opcode = inst->Instruction.Opcode;
1604 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001605 LLVMValueRef coords[4];
1606 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001607 int ref_pos;
1608 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001609 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001610 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001611 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1612 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001613 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001614
1615 if (target == TGSI_TEXTURE_BUFFER) {
1616 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1617 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1618 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1619 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1620
Marek Olšák4f3f0432014-07-07 22:49:15 +02001621 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001622 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1623 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1624 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1625 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1626
1627 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1628 emit_data->args[0] = res;
1629 emit_data->args[1] = bld_base->uint_bld.zero;
1630 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1631 emit_data->arg_count = 3;
1632 return;
1633 }
Tom Stellard467f5162012-05-16 15:15:35 -04001634
Michel Dänzer120efee2013-01-25 12:10:11 +01001635 /* Fetch and project texture coordinates */
1636 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001637 for (chan = 0; chan < 3; chan++ ) {
1638 coords[chan] = lp_build_emit_fetch(bld_base,
1639 emit_data->inst, 0,
1640 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001641 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001642 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1643 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001644 coords[chan],
1645 coords[3]);
1646 }
1647
Michel Dänzer120efee2013-01-25 12:10:11 +01001648 if (opcode == TGSI_OPCODE_TXP)
1649 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001650
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001651 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001652 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001653 /* The offsets are six-bit signed integers packed like this:
1654 * X=[5:0], Y=[13:8], and Z=[21:16].
1655 */
1656 LLVMValueRef offset[3], pack;
1657
1658 assert(inst->Texture.NumOffsets == 1);
1659
1660 for (chan = 0; chan < 3; chan++) {
1661 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1662 emit_data->inst, 0, chan);
1663 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1664 lp_build_const_int32(gallivm, 0x3f), "");
1665 if (chan)
1666 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1667 lp_build_const_int32(gallivm, chan*8), "");
1668 }
1669
1670 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1671 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1672 address[count++] = pack;
1673 }
1674
Michel Dänzer120efee2013-01-25 12:10:11 +01001675 /* Pack LOD bias value */
1676 if (opcode == TGSI_OPCODE_TXB)
1677 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001678 if (opcode == TGSI_OPCODE_TXB2)
1679 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001680
Michel Dänzer120efee2013-01-25 12:10:11 +01001681 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001682 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001683 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1684 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1685 } else {
1686 assert(ref_pos >= 0);
1687 address[count++] = coords[ref_pos];
1688 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001689 }
1690
Marek Olšákb279f0142014-07-04 03:19:58 +02001691 if (target == TGSI_TEXTURE_CUBE ||
1692 target == TGSI_TEXTURE_CUBE_ARRAY ||
1693 target == TGSI_TEXTURE_SHADOWCUBE ||
1694 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1695 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1696
Michel Dänzera6b83c02013-02-21 16:10:55 +01001697 /* Pack user derivatives */
1698 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001699 int num_deriv_channels, param;
1700
1701 switch (target) {
1702 case TGSI_TEXTURE_3D:
1703 num_deriv_channels = 3;
1704 break;
1705 case TGSI_TEXTURE_2D:
1706 case TGSI_TEXTURE_SHADOW2D:
1707 case TGSI_TEXTURE_RECT:
1708 case TGSI_TEXTURE_SHADOWRECT:
1709 case TGSI_TEXTURE_2D_ARRAY:
1710 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1711 case TGSI_TEXTURE_CUBE:
1712 case TGSI_TEXTURE_SHADOWCUBE:
1713 case TGSI_TEXTURE_CUBE_ARRAY:
1714 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1715 num_deriv_channels = 2;
1716 break;
1717 case TGSI_TEXTURE_1D:
1718 case TGSI_TEXTURE_SHADOW1D:
1719 case TGSI_TEXTURE_1D_ARRAY:
1720 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1721 num_deriv_channels = 1;
1722 break;
1723 default:
1724 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001725 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001726
1727 for (param = 1; param <= 2; param++)
1728 for (chan = 0; chan < num_deriv_channels; chan++)
1729 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001730 }
1731
Michel Dänzer120efee2013-01-25 12:10:11 +01001732 /* Pack texture coordinates */
1733 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001734 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001735 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001736 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001737 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001738
Marek Olšákd2bd6342013-09-18 15:40:21 +02001739 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001740 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001741 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001742 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001743 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001744
1745 if (count > 16) {
1746 assert(!"Cannot handle more than 16 texture address parameters");
1747 count = 16;
1748 }
1749
1750 for (chan = 0; chan < count; chan++ ) {
1751 address[chan] = LLVMBuildBitCast(gallivm->builder,
1752 address[chan],
1753 LLVMInt32TypeInContext(gallivm->context),
1754 "");
1755 }
1756
Marek Olšák4855acd2013-08-06 15:08:54 +02001757 /* Adjust the sample index according to FMASK.
1758 *
1759 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1760 * which is the identity mapping. Each nibble says which physical sample
1761 * should be fetched to get that sample.
1762 *
1763 * For example, 0x11111100 means there are only 2 samples stored and
1764 * the second sample covers 3/4 of the pixel. When reading samples 0
1765 * and 1, return physical sample 0 (determined by the first two 0s
1766 * in FMASK), otherwise return physical sample 1.
1767 *
1768 * The sample index should be adjusted as follows:
1769 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1770 */
1771 if (target == TGSI_TEXTURE_2D_MSAA ||
1772 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1773 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1774 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001775 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001776 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001777 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001778
Marek Olšákd2bd6342013-09-18 15:40:21 +02001779 memcpy(txf_address, address, sizeof(txf_address));
1780
1781 if (target == TGSI_TEXTURE_2D_MSAA) {
1782 txf_address[2] = bld_base->uint_bld.zero;
1783 }
1784 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001785
1786 /* Pad to a power-of-two size. */
1787 while (txf_count < util_next_power_of_two(txf_count))
1788 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1789
1790 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001791 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1792 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1793 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001794 txf_emit_data.chan = 0;
1795 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001796 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001797 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001798 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001799 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001800 txf_emit_data.arg_count = 3;
1801
Marek Olšák877bb522014-06-25 03:12:46 +02001802 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001803
1804 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001805 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1806 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1807
1808 /* Apply the formula. */
1809 LLVMValueRef fmask =
1810 LLVMBuildExtractElement(gallivm->builder,
1811 txf_emit_data.output[0],
1812 uint_bld->zero, "");
1813
Marek Olšákd2bd6342013-09-18 15:40:21 +02001814 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001815
1816 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001817 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001818
1819 LLVMValueRef shifted_fmask =
1820 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1821
1822 LLVMValueRef final_sample =
1823 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1824
1825 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1826 * resource descriptor is 0 (invalid),
1827 */
1828 LLVMValueRef fmask_desc =
1829 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001830 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001831 LLVMVectorType(uint_bld->elem_type, 8), "");
1832
1833 LLVMValueRef fmask_word1 =
1834 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1835 uint_bld->one, "");
1836
1837 LLVMValueRef word1_is_nonzero =
1838 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1839 fmask_word1, uint_bld->zero, "");
1840
Marek Olšákd2bd6342013-09-18 15:40:21 +02001841 /* Replace the MSAA sample index. */
1842 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001843 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001844 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001845 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001846
Michel Dänzer36231112013-05-02 09:44:45 +02001847 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001848 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001849
1850 if (opcode == TGSI_OPCODE_TXF) {
1851 /* add tex offsets */
1852 if (inst->Texture.NumOffsets) {
1853 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1854 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1855 const struct tgsi_texture_offset * off = inst->TexOffsets;
1856
1857 assert(inst->Texture.NumOffsets == 1);
1858
Marek Olšákdefedc02013-09-18 15:36:38 +02001859 switch (target) {
1860 case TGSI_TEXTURE_3D:
1861 address[2] = lp_build_add(uint_bld, address[2],
1862 bld->immediates[off->Index][off->SwizzleZ]);
1863 /* fall through */
1864 case TGSI_TEXTURE_2D:
1865 case TGSI_TEXTURE_SHADOW2D:
1866 case TGSI_TEXTURE_RECT:
1867 case TGSI_TEXTURE_SHADOWRECT:
1868 case TGSI_TEXTURE_2D_ARRAY:
1869 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001870 address[1] =
1871 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001872 bld->immediates[off->Index][off->SwizzleY]);
1873 /* fall through */
1874 case TGSI_TEXTURE_1D:
1875 case TGSI_TEXTURE_SHADOW1D:
1876 case TGSI_TEXTURE_1D_ARRAY:
1877 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1878 address[0] =
1879 lp_build_add(uint_bld, address[0],
1880 bld->immediates[off->Index][off->SwizzleX]);
1881 break;
1882 /* texture offsets do not apply to other texture targets */
1883 }
Michel Dänzer36231112013-05-02 09:44:45 +02001884 }
1885
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001886 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1887 emit_data->arg_count = 3;
1888
Michel Dänzer36231112013-05-02 09:44:45 +02001889 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001890 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001891 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001892 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001893 opcode == TGSI_OPCODE_LODQ ||
1894 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001895 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1896 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1897 target == TGSI_TEXTURE_2D_ARRAY ||
1898 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1899 target == TGSI_TEXTURE_CUBE_ARRAY ||
1900 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1901 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001902 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001903
Marek Olšák57f3da92014-06-16 16:53:42 +02001904 if (opcode == TGSI_OPCODE_TG4) {
1905 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001906
Marek Olšák57f3da92014-06-16 16:53:42 +02001907 /* DMASK was repurposed for GATHER4. 4 components are always
1908 * returned and DMASK works like a swizzle - it selects
1909 * the component to fetch. The only valid DMASK values are
1910 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1911 * (red,red,red,red) etc.) The ISA document doesn't mention
1912 * this.
1913 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001914
Marek Olšák57f3da92014-06-16 16:53:42 +02001915 /* Get the component index from src1.x for Gather4. */
1916 if (!tgsi_is_shadow_sampler(target)) {
1917 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1918 LLVMValueRef comp_imm;
1919 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001920
Marek Olšák57f3da92014-06-16 16:53:42 +02001921 assert(src1.File == TGSI_FILE_IMMEDIATE);
1922
1923 comp_imm = imms[src1.Index][src1.SwizzleX];
1924 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1925 gather_comp = CLAMP(gather_comp, 0, 3);
1926 }
1927
1928 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001929 }
1930
Marek Olšák4855acd2013-08-06 15:08:54 +02001931 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001932 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001933 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1934 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1935 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1936 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1937 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1938 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1939 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1940
1941 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001942
1943 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001944 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001945 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001946 } else {
1947 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1948 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001949 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001950
1951 emit_data->dst_type = LLVMVectorType(
1952 LLVMFloatTypeInContext(gallivm->context),
1953 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001954 }
1955
Marek Olšák2484daa2014-04-22 21:23:29 +02001956 /* The fetch opcode has been converted to a 2D array fetch.
1957 * This simplifies the LLVM backend. */
1958 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1959 target = TGSI_TEXTURE_2D_ARRAY;
1960 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1961 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1962
Michel Dänzer120efee2013-01-25 12:10:11 +01001963 /* Pad to power of two vector */
1964 while (count < util_next_power_of_two(count))
1965 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1966
Christian Königccf3e8f2013-03-26 15:09:27 +01001967 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001968}
1969
Michel Dänzer07eddc42013-02-06 15:43:10 +01001970static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1971 struct lp_build_tgsi_context * bld_base,
1972 struct lp_build_emit_data * emit_data)
1973{
1974 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001975 unsigned opcode = emit_data->inst->Instruction.Opcode;
1976 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001977 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001978 bool has_offset = HAVE_LLVM >= 0x0305 ?
1979 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001980
Marek Olšák877bb522014-06-25 03:12:46 +02001981 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001982 emit_data->output[emit_data->chan] = build_intrinsic(
1983 base->gallivm->builder,
1984 "llvm.SI.vs.load.input", emit_data->dst_type,
1985 emit_data->args, emit_data->arg_count,
1986 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1987 return;
1988 }
1989
Marek Olšák877bb522014-06-25 03:12:46 +02001990 if (opcode == TGSI_OPCODE_TG4 ||
1991 opcode == TGSI_OPCODE_LODQ ||
1992 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1993 bool is_shadow = tgsi_is_shadow_sampler(target);
1994 const char *name = "llvm.SI.image.sample";
1995 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001996
Marek Olšák877bb522014-06-25 03:12:46 +02001997 switch (opcode) {
1998 case TGSI_OPCODE_TEX:
1999 case TGSI_OPCODE_TEX2:
2000 case TGSI_OPCODE_TXP:
2001 break;
2002 case TGSI_OPCODE_TXB:
2003 case TGSI_OPCODE_TXB2:
2004 infix = ".b";
2005 break;
2006 case TGSI_OPCODE_TXL:
2007 case TGSI_OPCODE_TXL2:
2008 infix = ".l";
2009 break;
2010 case TGSI_OPCODE_TXD:
2011 infix = ".d";
2012 break;
2013 case TGSI_OPCODE_TG4:
2014 name = "llvm.SI.gather4";
2015 break;
2016 case TGSI_OPCODE_LODQ:
2017 name = "llvm.SI.getlod";
2018 is_shadow = false;
2019 has_offset = false;
2020 break;
2021 default:
2022 assert(0);
2023 return;
2024 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01002025
Marek Olšák877bb522014-06-25 03:12:46 +02002026 /* Add the type and suffixes .c, .o if needed. */
2027 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
2028 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
2029 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002030
Marek Olšák877bb522014-06-25 03:12:46 +02002031 emit_data->output[emit_data->chan] = build_intrinsic(
2032 base->gallivm->builder, intr_name, emit_data->dst_type,
2033 emit_data->args, emit_data->arg_count,
2034 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2035 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02002036 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02002037 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002038
Marek Olšák877bb522014-06-25 03:12:46 +02002039 switch (opcode) {
2040 case TGSI_OPCODE_TEX:
2041 case TGSI_OPCODE_TEX2:
2042 case TGSI_OPCODE_TXP:
2043 name = "llvm.SI.sample";
2044 break;
2045 case TGSI_OPCODE_TXB:
2046 case TGSI_OPCODE_TXB2:
2047 name = "llvm.SI.sampleb";
2048 break;
2049 case TGSI_OPCODE_TXD:
2050 name = "llvm.SI.sampled";
2051 break;
2052 case TGSI_OPCODE_TXF:
2053 name = "llvm.SI.imageload";
2054 break;
2055 case TGSI_OPCODE_TXL:
2056 case TGSI_OPCODE_TXL2:
2057 name = "llvm.SI.samplel";
2058 break;
2059 default:
2060 assert(0);
2061 return;
2062 }
2063
Marek Olšákd859bdb2014-07-14 19:40:14 +02002064 i8 = LLVMInt8TypeInContext(base->gallivm->context);
2065 v16i8 = LLVMVectorType(i8, 16);
2066 v32i8 = LLVMVectorType(i8, 32);
2067
2068 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
2069 emit_data->args[1], v32i8, "");
2070 if (opcode != TGSI_OPCODE_TXF) {
2071 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
2072 emit_data->args[2], v16i8, "");
2073 }
2074
Marek Olšák877bb522014-06-25 03:12:46 +02002075 sprintf(intr_name, "%s.v%ui32", name,
2076 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
2077
2078 emit_data->output[emit_data->chan] = build_intrinsic(
2079 base->gallivm->builder, intr_name, emit_data->dst_type,
2080 emit_data->args, emit_data->arg_count,
2081 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2082 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002083}
2084
Michel Dänzer0495adb2013-05-06 12:45:14 +02002085static void txq_fetch_args(
2086 struct lp_build_tgsi_context * bld_base,
2087 struct lp_build_emit_data * emit_data)
2088{
2089 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2090 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002091 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002092 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002093
Marek Olšák2484daa2014-04-22 21:23:29 +02002094 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002095 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2096 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2097
2098 /* Read the size from the buffer descriptor directly. */
2099 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2100 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2101 size = LLVMBuildExtractElement(gallivm->builder, size,
2102 lp_build_const_int32(gallivm, 2), "");
2103 emit_data->args[0] = size;
2104 return;
2105 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002106
2107 /* Mip level */
2108 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2109
2110 /* Resource */
2111 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2112
Marek Olšák2484daa2014-04-22 21:23:29 +02002113 /* Texture target */
2114 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2115 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2116 target = TGSI_TEXTURE_2D_ARRAY;
2117
Michel Dänzer0495adb2013-05-06 12:45:14 +02002118 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002119 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002120
2121 emit_data->arg_count = 3;
2122
2123 emit_data->dst_type = LLVMVectorType(
2124 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2125 4);
2126}
2127
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002128static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2129 struct lp_build_tgsi_context * bld_base,
2130 struct lp_build_emit_data * emit_data)
2131{
Marek Olšák2484daa2014-04-22 21:23:29 +02002132 unsigned target = emit_data->inst->Texture.Texture;
2133
2134 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002135 /* Just return the buffer size. */
2136 emit_data->output[emit_data->chan] = emit_data->args[0];
2137 return;
2138 }
2139
2140 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002141
2142 /* Divide the number of layers by 6 to get the number of cubes. */
2143 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2144 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2145 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2146 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2147 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2148
2149 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2150 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2151 z = LLVMBuildSDiv(builder, z, six, "");
2152
2153 emit_data->output[emit_data->chan] =
2154 LLVMBuildInsertElement(builder, v4, z, two, "");
2155 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002156}
2157
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002158static void si_llvm_emit_ddxy(
2159 const struct lp_build_tgsi_action * action,
2160 struct lp_build_tgsi_context * bld_base,
2161 struct lp_build_emit_data * emit_data)
2162{
2163 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2164 struct gallivm_state *gallivm = bld_base->base.gallivm;
2165 struct lp_build_context * base = &bld_base->base;
2166 const struct tgsi_full_instruction *inst = emit_data->inst;
2167 unsigned opcode = inst->Instruction.Opcode;
2168 LLVMValueRef indices[2];
2169 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2170 LLVMValueRef tl, trbl, result[4];
2171 LLVMTypeRef i32;
2172 unsigned swizzle[4];
2173 unsigned c;
2174
2175 i32 = LLVMInt32TypeInContext(gallivm->context);
2176
2177 indices[0] = bld_base->uint_bld.zero;
2178 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2179 NULL, 0, LLVMReadNoneAttribute);
2180 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2181 indices, 2, "");
2182
2183 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2184 lp_build_const_int32(gallivm, 0xfffffffc), "");
2185 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2186 indices, 2, "");
2187
2188 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2189 lp_build_const_int32(gallivm,
2190 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2191 "");
2192 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2193 indices, 2, "");
2194
2195 for (c = 0; c < 4; ++c) {
2196 unsigned i;
2197
2198 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2199 for (i = 0; i < c; ++i) {
2200 if (swizzle[i] == swizzle[c]) {
2201 result[c] = result[i];
2202 break;
2203 }
2204 }
2205 if (i != c)
2206 continue;
2207
2208 LLVMBuildStore(gallivm->builder,
2209 LLVMBuildBitCast(gallivm->builder,
2210 lp_build_emit_fetch(bld_base, inst, 0, c),
2211 i32, ""),
2212 store_ptr);
2213
2214 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2215 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2216
2217 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2218 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2219
2220 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2221 }
2222
2223 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2224}
2225
Michel Dänzer404b29d2013-11-21 16:45:28 +09002226/* Emit one vertex from the geometry shader */
2227static void si_llvm_emit_vertex(
2228 const struct lp_build_tgsi_action *action,
2229 struct lp_build_tgsi_context *bld_base,
2230 struct lp_build_emit_data *emit_data)
2231{
2232 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002233 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002234 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák02134cf2014-10-04 22:07:50 +02002235 struct tgsi_shader_info *info = &shader->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002236 struct gallivm_state *gallivm = bld_base->base.gallivm;
2237 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002238 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2239 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002240 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002241 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002242 LLVMValueRef t_list_ptr;
2243 LLVMValueRef t_list;
2244 LLVMValueRef args[2];
2245 unsigned chan;
2246 int i;
2247
2248 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002249 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2250 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002251 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002252 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002253
2254 if (shader->noutput == 0) {
2255 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
2256
2257 while (!tgsi_parse_end_of_tokens(parse)) {
2258 tgsi_parse_token(parse);
2259
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002260 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
2261 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
2262
2263 if (d->Declaration.File == TGSI_FILE_OUTPUT)
2264 si_store_shader_io_attribs(shader, d);
2265 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09002266 }
2267 }
2268
2269 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002270 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002271
2272 /* If this thread has already emitted the declared maximum number of
2273 * vertices, kill it: excessive vertex emissions are not supposed to
2274 * have any effect, and GS threads have no externally observable
2275 * effects other than emitting vertices.
2276 */
2277 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2278 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002279 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002280 kill = lp_build_select(&bld_base->base, can_emit,
2281 lp_build_const_float(gallivm, 1.0f),
2282 lp_build_const_float(gallivm, -1.0f));
2283 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2284 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2285
Marek Olšák02134cf2014-10-04 22:07:50 +02002286 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002287 LLVMValueRef *out_ptr =
Marek Olšák02134cf2014-10-04 22:07:50 +02002288 si_shader_ctx->radeon_bld.soa.outputs[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002289
2290 for (chan = 0; chan < 4; chan++) {
2291 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002292 LLVMValueRef voffset =
2293 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002294 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002295
2296 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2297 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002298
2299 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2300
2301 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2302 voffset, soffset, 0,
2303 V_008F0C_BUF_DATA_FORMAT_32,
2304 V_008F0C_BUF_NUM_FORMAT_UINT,
2305 1, 0, 1, 1, 0);
2306 }
2307 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002308 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2309 lp_build_const_int32(gallivm, 1));
2310 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002311
2312 /* Signal vertex emission */
2313 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2314 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2315 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2316 LLVMVoidTypeInContext(gallivm->context), args, 2,
2317 LLVMNoUnwindAttribute);
2318}
2319
2320/* Cut one primitive from the geometry shader */
2321static void si_llvm_emit_primitive(
2322 const struct lp_build_tgsi_action *action,
2323 struct lp_build_tgsi_context *bld_base,
2324 struct lp_build_emit_data *emit_data)
2325{
2326 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2327 struct gallivm_state *gallivm = bld_base->base.gallivm;
2328 LLVMValueRef args[2];
2329
2330 /* Signal primitive cut */
2331 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2332 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2333 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2334 LLVMVoidTypeInContext(gallivm->context), args, 2,
2335 LLVMNoUnwindAttribute);
2336}
2337
Tom Stellarda75c6162012-01-06 17:38:37 -05002338static const struct lp_build_tgsi_action tex_action = {
2339 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002340 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002341};
2342
Michel Dänzer0495adb2013-05-06 12:45:14 +02002343static const struct lp_build_tgsi_action txq_action = {
2344 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002345 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002346 .intr_name = "llvm.SI.resinfo"
2347};
2348
Christian König206f0592013-03-20 14:37:21 +01002349static void create_meta_data(struct si_shader_context *si_shader_ctx)
2350{
2351 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2352 LLVMValueRef args[3];
2353
2354 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2355 args[1] = 0;
2356 args[2] = lp_build_const_int32(gallivm, 1);
2357
2358 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2359}
2360
Marek Olšák4f3f0432014-07-07 22:49:15 +02002361static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2362{
2363 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2364 CONST_ADDR_SPACE);
2365}
2366
Christian König55fe5cc2013-03-04 16:30:06 +01002367static void create_function(struct si_shader_context *si_shader_ctx)
2368{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002369 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2370 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002371 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002372 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002373 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002374
Christian König55fe5cc2013-03-04 16:30:06 +01002375 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002376 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002377 f32 = LLVMFloatTypeInContext(gallivm->context);
2378 v2i32 = LLVMVectorType(i32, 2);
2379 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002380 v4i32 = LLVMVectorType(i32, 4);
2381 v8i32 = LLVMVectorType(i32, 8);
2382 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002383
Marek Olšákee2a8182014-07-07 23:27:19 +02002384 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002385 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002386 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2387 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002388 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002389
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002390 switch (si_shader_ctx->type) {
2391 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002392 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002393 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002394 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002395 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002396 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002397
Michel Dänzer404b29d2013-11-21 16:45:28 +09002398 if (shader->key.vs.as_es) {
2399 params[SI_PARAM_ES2GS_OFFSET] = i32;
2400 num_params++;
2401 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002402 if (shader->is_gs_copy_shader) {
2403 last_array_pointer = SI_PARAM_CONST;
2404 num_params = SI_PARAM_CONST+1;
2405 }
2406
Michel Dänzer404b29d2013-11-21 16:45:28 +09002407 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002408
Michel Dänzer404b29d2013-11-21 16:45:28 +09002409 /* Streamout SGPRs. */
2410 if (shader->selector->so.num_outputs) {
2411 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2412 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2413 }
2414 /* A streamout buffer offset is loaded if the stride is non-zero. */
2415 for (i = 0; i < 4; i++) {
2416 if (!shader->selector->so.stride[i])
2417 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002418
Michel Dänzer404b29d2013-11-21 16:45:28 +09002419 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2420 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002421 }
2422
2423 last_sgpr = num_params-1;
2424
2425 /* VGPRs */
2426 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2427 params[num_params++] = i32; /* unused*/
2428 params[num_params++] = i32; /* unused */
2429 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002430 break;
Christian König0666ffd2013-03-05 15:07:39 +01002431
Michel Dänzer404b29d2013-11-21 16:45:28 +09002432 case TGSI_PROCESSOR_GEOMETRY:
2433 params[SI_PARAM_GS2VS_OFFSET] = i32;
2434 params[SI_PARAM_GS_WAVE_ID] = i32;
2435 last_sgpr = SI_PARAM_GS_WAVE_ID;
2436
2437 /* VGPRs */
2438 params[SI_PARAM_VTX0_OFFSET] = i32;
2439 params[SI_PARAM_VTX1_OFFSET] = i32;
2440 params[SI_PARAM_PRIMITIVE_ID] = i32;
2441 params[SI_PARAM_VTX2_OFFSET] = i32;
2442 params[SI_PARAM_VTX3_OFFSET] = i32;
2443 params[SI_PARAM_VTX4_OFFSET] = i32;
2444 params[SI_PARAM_VTX5_OFFSET] = i32;
2445 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2446 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2447 break;
2448
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002449 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002450 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002451 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002452 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002453 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2454 params[SI_PARAM_PERSP_CENTER] = v2i32;
2455 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2456 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2457 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2458 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2459 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2460 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2461 params[SI_PARAM_POS_X_FLOAT] = f32;
2462 params[SI_PARAM_POS_Y_FLOAT] = f32;
2463 params[SI_PARAM_POS_Z_FLOAT] = f32;
2464 params[SI_PARAM_POS_W_FLOAT] = f32;
2465 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002466 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002467 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2468 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002469 num_params = SI_PARAM_POS_FIXED_PT+1;
2470 break;
2471
2472 default:
2473 assert(0 && "unimplemented shader");
2474 return;
Christian Königc4973212013-03-05 12:14:02 +01002475 }
Christian König55fe5cc2013-03-04 16:30:06 +01002476
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002477 assert(num_params <= Elements(params));
2478 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002479 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002480
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002481 for (i = 0; i <= last_sgpr; ++i) {
2482 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002483
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002484 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2485 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002486 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002487 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002488 else
2489 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002490 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002491
Michel Dänzer404b29d2013-11-21 16:45:28 +09002492 if (bld_base->info &&
2493 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2494 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002495 si_shader_ctx->ddxy_lds =
2496 LLVMAddGlobalInAddressSpace(gallivm->module,
2497 LLVMArrayType(i32, 64),
2498 "ddxy_lds",
2499 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002500}
Tom Stellarda75c6162012-01-06 17:38:37 -05002501
Christian König0f6cf2b2013-03-15 15:53:25 +01002502static void preload_constants(struct si_shader_context *si_shader_ctx)
2503{
2504 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2505 struct gallivm_state * gallivm = bld_base->base.gallivm;
2506 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002507 unsigned buf;
2508 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002509
Marek Olšákee2a8182014-07-07 23:27:19 +02002510 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002511 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002512
Marek Olšák2fd42002013-10-25 11:45:47 +02002513 if (num_const == 0)
2514 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002515
Marek Olšák2fd42002013-10-25 11:45:47 +02002516 /* Allocate space for the constant values */
2517 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002518
Marek Olšák2fd42002013-10-25 11:45:47 +02002519 /* Load the resource descriptor */
2520 si_shader_ctx->const_resource[buf] =
2521 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002522
Marek Olšák2fd42002013-10-25 11:45:47 +02002523 /* Load the constants, we rely on the code sinking to do the rest */
2524 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002525 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002526 load_const(gallivm->builder,
2527 si_shader_ctx->const_resource[buf],
2528 lp_build_const_int32(gallivm, i * 4),
2529 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002530 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002531 }
2532}
2533
Christian König1c100182013-03-17 16:02:42 +01002534static void preload_samplers(struct si_shader_context *si_shader_ctx)
2535{
2536 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2537 struct gallivm_state * gallivm = bld_base->base.gallivm;
2538 const struct tgsi_shader_info * info = bld_base->info;
2539
2540 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2541
2542 LLVMValueRef res_ptr, samp_ptr;
2543 LLVMValueRef offset;
2544
2545 if (num_samplers == 0)
2546 return;
2547
2548 /* Allocate space for the values */
Marek Olšákee2a8182014-07-07 23:27:19 +02002549 si_shader_ctx->resources = CALLOC(SI_NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002550 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2551
2552 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2553 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2554
2555 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2556 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002557 /* Resource */
2558 offset = lp_build_const_int32(gallivm, i);
2559 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2560
2561 /* Sampler */
2562 offset = lp_build_const_int32(gallivm, i);
2563 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002564
2565 /* FMASK resource */
2566 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002567 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2568 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšák4855acd2013-08-06 15:08:54 +02002569 build_indexed_load(si_shader_ctx, res_ptr, offset);
2570 }
Christian König1c100182013-03-17 16:02:42 +01002571 }
2572}
2573
Marek Olšák8d03d922013-09-01 23:59:06 +02002574static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2575{
2576 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2577 struct gallivm_state * gallivm = bld_base->base.gallivm;
2578 unsigned i;
2579
Michel Dänzer67e385b2014-01-08 17:48:21 +09002580 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2581 si_shader_ctx->shader->key.vs.as_es ||
2582 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002583 return;
2584
2585 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002586 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002587
2588 /* Load the resources, we rely on the code sinking to do the rest */
2589 for (i = 0; i < 4; ++i) {
2590 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002591 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002592 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002593
2594 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2595 }
2596 }
2597}
2598
Marek Olšák1abb1a92014-09-17 22:44:22 +02002599int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2600 LLVMModuleRef mod)
Tom Stellard302f53d2012-10-25 13:50:10 -04002601{
Darren Powellbc866902014-03-31 18:00:28 -04002602 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002603 unsigned i;
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002604 unsigned char *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002605 struct radeon_shader_binary binary;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002606 bool dump = r600_can_dump_shader(&sscreen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002607 shader->selector ? shader->selector->tokens : NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002608 const char * gpu_family = r600_get_llvm_processor_name(sscreen->b.family);
Tom Stellard9ba31052014-07-14 16:49:08 -04002609 unsigned code_size;
Darren Powellbc866902014-03-31 18:00:28 -04002610
2611 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002612 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002613 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2614
2615 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002616 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002617 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002618 for (i = 0; i < binary.code_size; i+=4 ) {
2619 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2620 binary.code[i + 2], binary.code[i + 1],
2621 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002622 }
2623 }
2624
Tom Stellardd50343d2013-04-04 16:21:06 -04002625 /* XXX: We may be able to emit some of these values directly rather than
2626 * extracting fields to be emitted later.
2627 */
Darren Powellbc866902014-03-31 18:00:28 -04002628 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002629 for (i = 0; i < binary.config_size; i+= 8) {
2630 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2631 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2632 switch (reg) {
2633 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2634 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2635 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2636 case R_00B848_COMPUTE_PGM_RSRC1:
2637 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2638 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2639 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002640 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2641 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2642 break;
2643 case R_00B84C_COMPUTE_PGM_RSRC2:
2644 shader->lds_size = G_00B84C_LDS_SIZE(value);
2645 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002646 case R_0286CC_SPI_PS_INPUT_ENA:
2647 shader->spi_ps_input_ena = value;
2648 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002649 case R_00B860_COMPUTE_TMPRING_SIZE:
2650 /* WAVESIZE is in units of 256 dwords. */
2651 shader->scratch_bytes_per_wave =
2652 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2653 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002654 default:
2655 fprintf(stderr, "Warning: Compiler emitted unknown "
2656 "config register: 0x%x\n", reg);
2657 break;
2658 }
2659 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002660
2661 /* copy new shader */
Tom Stellard9ba31052014-07-14 16:49:08 -04002662 code_size = binary.code_size + binary.rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002663 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002664 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002665 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002666 if (shader->bo == NULL) {
2667 return -ENOMEM;
2668 }
2669
Marek Olšák1abb1a92014-09-17 22:44:22 +02002670 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_WRITE);
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002671 util_memcpy_cpu_to_le32(ptr, binary.code, binary.code_size);
2672 if (binary.rodata_size > 0) {
2673 ptr += binary.code_size;
2674 util_memcpy_cpu_to_le32(ptr, binary.rodata, binary.rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002675 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002676
Marek Olšák1abb1a92014-09-17 22:44:22 +02002677 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002678
Tom Stellard7782d192013-04-04 09:57:13 -07002679 free(binary.code);
2680 free(binary.config);
Tom Stellard9ba31052014-07-14 16:49:08 -04002681 free(binary.rodata);
Tom Stellard302f53d2012-10-25 13:50:10 -04002682
Darren Powellbc866902014-03-31 18:00:28 -04002683 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002684}
2685
Michel Dänzer404b29d2013-11-21 16:45:28 +09002686/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002687static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002688 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002689 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002690{
2691 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2692 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2693 struct lp_build_context *base = &bld_base->base;
2694 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002695 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002696 struct si_shader_output_values *outputs;
Marek Olšák02134cf2014-10-04 22:07:50 +02002697 struct tgsi_shader_info *gsinfo = &gs->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002698 LLVMValueRef t_list_ptr, t_list;
2699 LLVMValueRef args[9];
2700 int i, r;
2701
Marek Olšák02134cf2014-10-04 22:07:50 +02002702 outputs = MALLOC(gsinfo->num_outputs * sizeof(outputs[0]));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002703
2704 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002705 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002706
2707 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2708
2709 create_meta_data(si_shader_ctx);
2710 create_function(si_shader_ctx);
2711 preload_streamout_buffers(si_shader_ctx);
2712
2713 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002714 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2715 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002716 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002717 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002718
2719 args[0] = t_list;
2720 args[1] = lp_build_mul_imm(uint,
2721 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2722 si_shader_ctx->param_vertex_id),
2723 4);
2724 args[3] = uint->zero;
2725 args[4] = uint->one; /* OFFEN */
2726 args[5] = uint->zero; /* IDXEN */
2727 args[6] = uint->one; /* GLC */
2728 args[7] = uint->one; /* SLC */
2729 args[8] = uint->zero; /* TFE */
2730
2731 /* Fetch vertex data from GSVS ring */
Marek Olšák02134cf2014-10-04 22:07:50 +02002732 for (i = 0; i < gsinfo->num_outputs; ++i) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002733 unsigned chan;
2734
Marek Olšák02134cf2014-10-04 22:07:50 +02002735 outputs[i].name = gsinfo->output_semantic_name[i];
2736 outputs[i].index = i;
2737 outputs[i].sid = gsinfo->output_semantic_index[i];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002738
2739 for (chan = 0; chan < 4; chan++) {
2740 args[2] = lp_build_const_int32(gallivm,
2741 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002742 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002743
2744 outputs[i].values[chan] =
2745 LLVMBuildBitCast(gallivm->builder,
2746 build_intrinsic(gallivm->builder,
2747 "llvm.SI.buffer.load.dword.i32.i32",
2748 LLVMInt32TypeInContext(gallivm->context),
2749 args, 9,
2750 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2751 base->elem_type, "");
2752 }
2753 }
2754
Marek Olšák02134cf2014-10-04 22:07:50 +02002755 si_llvm_export_vs(bld_base, outputs, gsinfo->num_outputs);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002756
2757 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2758
2759 if (dump)
2760 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2761
Marek Olšák1abb1a92014-09-17 22:44:22 +02002762 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002763 bld_base->base.gallivm->module);
2764
2765 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2766
2767 FREE(outputs);
2768 return r;
2769}
2770
Marek Olšák1abb1a92014-09-17 22:44:22 +02002771int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002772{
Marek Olšák2774abd2014-09-16 18:45:33 +02002773 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002774 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002775 struct lp_build_tgsi_context * bld_base;
2776 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002777 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002778 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002779
2780 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2781 * conversion fails. */
2782 if (dump) {
2783 tgsi_dump(sel->tokens, 0);
2784 si_dump_streamout(&sel->so);
2785 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002786
Marek Olšák8c37c162014-09-16 18:40:07 +02002787 assert(shader->noutput == 0);
2788 assert(shader->nparam == 0);
2789 assert(shader->ninput == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002790
Michel Dänzercfebaf92012-08-31 19:04:08 +02002791 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002792 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2793 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2794
Marek Olšák52335682014-09-30 15:12:09 +02002795 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002796 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2797
Marek Olšák52335682014-09-30 15:12:09 +02002798 shader->uses_instanceid = sel->info.uses_instanceid;
2799 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002800 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002801
2802 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002803 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002804 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2805 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002806 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002807 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2808 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2809 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002810 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002811 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002812 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2813 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002814
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002815 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2816 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002817
Michel Dänzer404b29d2013-11-21 16:45:28 +09002818 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2819 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2820
Christian Könige4ed5872013-03-21 18:02:52 +01002821 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002822 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002823 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2824 si_shader_ctx.shader = shader;
2825 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002826
Michel Dänzer51f89a02013-12-09 15:33:53 +09002827 switch (si_shader_ctx.type) {
2828 case TGSI_PROCESSOR_VERTEX:
2829 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002830 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002831 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2832 } else {
2833 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2834 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002835 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002836 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002837 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2838 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2839 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002840 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002841 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002842 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2843 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002844
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002845 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002846 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2847 shader->db_shader_control |=
2848 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2849 break;
2850 case TGSI_FS_DEPTH_LAYOUT_LESS:
2851 shader->db_shader_control |=
2852 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2853 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002854 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002855 break;
2856 default:
2857 assert(!"Unsupported shader type");
2858 return -1;
2859 }
2860
Christian König206f0592013-03-20 14:37:21 +01002861 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002862 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002863 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002864 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002865 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002866
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002867 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2868 si_shader_ctx.gs_next_vertex =
2869 lp_build_alloca(bld_base->base.gallivm,
2870 bld_base->uint_bld.elem_type, "");
2871 }
2872
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002873 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002874 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002875 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002876 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002877
2878 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2879
2880 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002881 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002882 if (r) {
2883 fprintf(stderr, "LLVM failed to compile shader\n");
2884 goto out;
2885 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002886
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002887 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002888
2889 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002890 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002891 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002892 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002893 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002894 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2895 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002896 free(shader->gs_copy_shader);
2897 shader->gs_copy_shader = NULL;
2898 goto out;
2899 }
2900 }
2901
Tom Stellarda75c6162012-01-06 17:38:37 -05002902 tgsi_parse_free(&si_shader_ctx.parse);
2903
Michel Dänzer404b29d2013-11-21 16:45:28 +09002904out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002905 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002906 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002907 FREE(si_shader_ctx.resources);
2908 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002909
Tom Stellard302f53d2012-10-25 13:50:10 -04002910 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002911}
2912
Marek Olšák2774abd2014-09-16 18:45:33 +02002913void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002914{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002915 if (shader->gs_copy_shader)
2916 si_shader_destroy(ctx, shader->gs_copy_shader);
2917
Marek Olšáka81c3e02013-08-14 01:04:39 +02002918 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002919 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002920}