blob: 692d2a9967ef28abf6037c556be0304f3774a89b [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;
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400249 shader->input[i].centroid = d->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900250 return -1;
251
252 case TGSI_FILE_OUTPUT:
253 i = shader->noutput++;
254 assert(i < Elements(shader->output));
255 shader->output[i].name = d->Semantic.Name;
256 shader->output[i].sid = d->Semantic.Index;
257 shader->output[i].index = d->Range.First;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900258 break;
259 }
260
261 return i;
262}
263
Tom Stellarda75c6162012-01-06 17:38:37 -0500264static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900265 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500266 unsigned input_index,
267 const struct tgsi_full_declaration *decl)
268{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900269 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
270 struct gallivm_state *gallivm = base->gallivm;
271 struct si_shader_context *si_shader_ctx =
272 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100273 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
274
275 unsigned chan;
276
Tom Stellarda75c6162012-01-06 17:38:37 -0500277 LLVMValueRef t_list_ptr;
278 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400279 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500280 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100281 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400282 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500283 LLVMTypeRef vec4_type;
284 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500285
Tom Stellard467f5162012-05-16 15:15:35 -0400286 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100287 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500288
Michel Dänzer51f89a02013-12-09 15:33:53 +0900289 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400290
Christian König206f0592013-03-20 14:37:21 +0100291 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400292
293 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900294 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500295
Christian Königa0dca442013-03-22 15:59:22 +0100296 if (divisor) {
297 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200298 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200299 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100300 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200301 /* Load the buffer index for vertices. */
302 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
303 si_shader_ctx->param_vertex_id);
304 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
305 SI_PARAM_BASE_VERTEX);
306 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100307 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500308
309 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400310 args[0] = t_list;
311 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100312 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900313 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100314 "llvm.SI.vs.load.input", vec4_type, args, 3,
315 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500316
317 /* Break up the vec4 into individual components */
318 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900319 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500320 /* XXX: Use a helper function for this. There is one in
321 * tgsi_llvm.c. */
322 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900323 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500324 input, llvm_chan, "");
325 }
326}
327
Michel Dänzer404b29d2013-11-21 16:45:28 +0900328static void declare_input_gs(
329 struct radeon_llvm_context *radeon_bld,
330 unsigned input_index,
331 const struct tgsi_full_declaration *decl)
332{
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900333 struct si_shader_context *si_shader_ctx =
334 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200335 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900336
337 si_store_shader_io_attribs(shader, decl);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900338}
339
340static LLVMValueRef fetch_input_gs(
341 struct lp_build_tgsi_context *bld_base,
342 const struct tgsi_full_src_register *reg,
343 enum tgsi_opcode_type type,
344 unsigned swizzle)
345{
346 struct lp_build_context *base = &bld_base->base;
347 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200348 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900349 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
350 struct gallivm_state *gallivm = base->gallivm;
351 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
352 LLVMValueRef vtx_offset;
353 LLVMValueRef t_list_ptr;
354 LLVMValueRef t_list;
355 LLVMValueRef args[9];
356 unsigned vtx_offset_param;
Marek Olšáke23fec12014-10-04 17:40:39 +0200357 struct tgsi_shader_info *info = &shader->selector->info;
358 unsigned semantic_name = info->input_semantic_name[reg->Register.Index];
359 unsigned semantic_index = info->input_semantic_index[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900360
Marek Olšáke23fec12014-10-04 17:40:39 +0200361 if (swizzle != ~0 && semantic_name == TGSI_SEMANTIC_PRIMID) {
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900362 if (swizzle == 0)
363 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
364 SI_PARAM_PRIMITIVE_ID);
365 else
366 return uint->zero;
367 }
368
Michel Dänzer404b29d2013-11-21 16:45:28 +0900369 if (!reg->Register.Dimension)
370 return NULL;
371
372 if (swizzle == ~0) {
373 LLVMValueRef values[TGSI_NUM_CHANNELS];
374 unsigned chan;
375 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
376 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
377 }
378 return lp_build_gather_values(bld_base->base.gallivm, values,
379 TGSI_NUM_CHANNELS);
380 }
381
382 /* Get the vertex offset parameter */
383 vtx_offset_param = reg->Dimension.Index;
384 if (vtx_offset_param < 2) {
385 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
386 } else {
387 assert(vtx_offset_param < 6);
388 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
389 }
390 vtx_offset = lp_build_mul_imm(uint,
391 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
392 vtx_offset_param),
393 4);
394
395 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +0900396 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
397 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900398 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900399 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900400
401 args[0] = t_list;
402 args[1] = vtx_offset;
403 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke23fec12014-10-04 17:40:39 +0200404 (get_param_index(semantic_name, semantic_index,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200405 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900406 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900407 args[3] = uint->zero;
408 args[4] = uint->one; /* OFFEN */
409 args[5] = uint->zero; /* IDXEN */
410 args[6] = uint->one; /* GLC */
411 args[7] = uint->zero; /* SLC */
412 args[8] = uint->zero; /* TFE */
413
414 return LLVMBuildBitCast(gallivm->builder,
415 build_intrinsic(gallivm->builder,
416 "llvm.SI.buffer.load.dword.i32.i32",
417 i32, args, 9,
418 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
419 tgsi2llvmtype(bld_base, type), "");
420}
421
Tom Stellarda75c6162012-01-06 17:38:37 -0500422static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900423 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500424 unsigned input_index,
425 const struct tgsi_full_declaration *decl)
426{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900427 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
428 struct si_shader_context *si_shader_ctx =
429 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200430 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900431 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
432 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400433 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900434 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100435
436 LLVMValueRef interp_param;
437 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500438
439 /* This value is:
440 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
441 * quad begins a new primitive. Bit 0 always needs
442 * to be unset)
443 * [32:16] ParamOffset
444 *
445 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900446 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200447 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500448
Christian König0666ffd2013-03-05 15:07:39 +0100449 unsigned chan;
450
Tom Stellard0fb1e682012-09-06 16:18:11 -0400451 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
452 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400453 unsigned soa_index =
454 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900455 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100456 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100457
458 if (chan == 3)
459 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900460 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100461 LLVMBuildFDiv(gallivm->builder,
462 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900463 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100464 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400465 }
466 return;
467 }
468
Michel Dänzer97078b12012-09-25 12:41:31 +0200469 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
470 LLVMValueRef face, is_face_positive;
471
Christian König0666ffd2013-03-05 15:07:39 +0100472 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
473
Michel Dänzer97078b12012-09-25 12:41:31 +0200474 is_face_positive = LLVMBuildFCmp(gallivm->builder,
475 LLVMRealUGT, face,
476 lp_build_const_float(gallivm, 0.0f),
477 "");
478
Michel Dänzer51f89a02013-12-09 15:33:53 +0900479 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200480 LLVMBuildSelect(gallivm->builder,
481 is_face_positive,
482 lp_build_const_float(gallivm, 1.0f),
483 lp_build_const_float(gallivm, 0.0f),
484 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900485 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
486 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200487 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900488 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200489 lp_build_const_float(gallivm, 1.0f);
490
491 return;
492 }
493
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900494 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200495 attr_number = lp_build_const_int32(gallivm,
496 shader->input[input_index].param_offset);
497
Francisco Jerez12799232012-04-30 18:27:52 +0200498 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500499 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100500 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500501 break;
502 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200503 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200504 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400505 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100506 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200507 else
Christian König0666ffd2013-03-05 15:07:39 +0100508 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200509 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200510 case TGSI_INTERPOLATE_COLOR:
511 if (si_shader_ctx->shader->key.ps.flatshade) {
512 interp_param = 0;
513 break;
514 }
515 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200516 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200517 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200518 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400519 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100520 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200521 else
Christian König0666ffd2013-03-05 15:07:39 +0100522 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500523 break;
524 default:
525 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
526 return;
527 }
528
Christian König0666ffd2013-03-05 15:07:39 +0100529 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
530
Tom Stellarda75c6162012-01-06 17:38:37 -0500531 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200532 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100533 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100534 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200535 LLVMValueRef face, is_face_positive;
536 LLVMValueRef back_attr_number =
537 lp_build_const_int32(gallivm,
538 shader->input[input_index].param_offset + 1);
539
Christian König0666ffd2013-03-05 15:07:39 +0100540 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
541
Michel Dänzer691f08d2012-09-06 18:03:38 +0200542 is_face_positive = LLVMBuildFCmp(gallivm->builder,
543 LLVMRealUGT, face,
544 lp_build_const_float(gallivm, 0.0f),
545 "");
546
Tom Stellarda75c6162012-01-06 17:38:37 -0500547 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100548 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200549 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
550 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
551 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
552 LLVMValueRef front, back;
553
554 args[0] = llvm_chan;
555 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900556 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100557 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100558 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200559
560 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900561 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100562 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100563 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200564
Michel Dänzer51f89a02013-12-09 15:33:53 +0900565 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200566 LLVMBuildSelect(gallivm->builder,
567 is_face_positive,
568 front,
569 back,
570 "");
571 }
572
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900573 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200574 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
575 LLVMValueRef args[4];
576
577 args[0] = uint->zero;
578 args[1] = attr_number;
579 args[2] = params;
580 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900581 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
582 build_intrinsic(gallivm->builder, intr_name,
583 input_type, args, args[3] ? 4 : 3,
584 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
585 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
586 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200587 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900588 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200589 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200590 } else {
591 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100592 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200593 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
594 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
595 args[0] = llvm_chan;
596 args[1] = attr_number;
597 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100598 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900599 radeon_bld->inputs[soa_index] =
600 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100601 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100602 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200603 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500604 }
605}
606
Marek Olšák5b06fc32014-05-06 18:12:40 +0200607static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
608{
609 struct gallivm_state *gallivm = &radeon_bld->gallivm;
610 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
611 SI_PARAM_ANCILLARY);
612 value = LLVMBuildLShr(gallivm->builder, value,
613 lp_build_const_int32(gallivm, 8), "");
614 value = LLVMBuildAnd(gallivm->builder, value,
615 lp_build_const_int32(gallivm, 0xf), "");
616 return value;
617}
618
Marek Olšák250aa932014-05-06 14:10:47 +0200619static LLVMValueRef load_const(LLVMBuilderRef builder, LLVMValueRef resource,
620 LLVMValueRef offset, LLVMTypeRef return_type)
621{
622 LLVMValueRef args[2] = {resource, offset};
623
624 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
625 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
626}
627
Christian Könige4ed5872013-03-21 18:02:52 +0100628static void declare_system_value(
629 struct radeon_llvm_context * radeon_bld,
630 unsigned index,
631 const struct tgsi_full_declaration *decl)
632{
Marek Olšák8d03d922013-09-01 23:59:06 +0200633 struct si_shader_context *si_shader_ctx =
634 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200635 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
636 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100637 LLVMValueRef value = 0;
638
639 switch (decl->Semantic.Name) {
640 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200641 value = LLVMGetParam(radeon_bld->main_fn,
642 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100643 break;
644
645 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200646 value = LLVMGetParam(radeon_bld->main_fn,
647 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100648 break;
649
Marek Olšák5b06fc32014-05-06 18:12:40 +0200650 case TGSI_SEMANTIC_SAMPLEID:
651 value = get_sample_id(radeon_bld);
652 break;
653
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200654 case TGSI_SEMANTIC_SAMPLEPOS:
655 {
656 LLVMBuilderRef builder = gallivm->builder;
657 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200658 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200659 LLVMValueRef resource = build_indexed_load(si_shader_ctx, desc, buf_index);
660
661 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
662 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
663 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
664
665 LLVMValueRef pos[4] = {
666 load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
667 load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
668 lp_build_const_float(gallivm, 0),
669 lp_build_const_float(gallivm, 0)
670 };
671 value = lp_build_gather_values(gallivm, pos, 4);
672 break;
673 }
674
Christian Könige4ed5872013-03-21 18:02:52 +0100675 default:
676 assert(!"unknown system value");
677 return;
678 }
679
680 radeon_bld->system_values[index] = value;
681}
682
Tom Stellarda75c6162012-01-06 17:38:37 -0500683static LLVMValueRef fetch_constant(
684 struct lp_build_tgsi_context * bld_base,
685 const struct tgsi_full_src_register *reg,
686 enum tgsi_opcode_type type,
687 unsigned swizzle)
688{
Christian König55fe5cc2013-03-04 16:30:06 +0100689 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500690 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100691 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200692 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500693
Christian König0f6cf2b2013-03-15 15:53:25 +0100694 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100695 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500696
Christian König8514f5a2013-02-04 17:46:42 +0100697 if (swizzle == LP_CHAN_ALL) {
698 unsigned chan;
699 LLVMValueRef values[4];
700 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
701 values[chan] = fetch_constant(bld_base, reg, type, chan);
702
703 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
704 }
705
Marek Olšák2fd42002013-10-25 11:45:47 +0200706 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100707 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100708
Marek Olšák2fd42002013-10-25 11:45:47 +0200709 if (!reg->Register.Indirect)
710 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
711
Christian König0f6cf2b2013-03-15 15:53:25 +0100712 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
713 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
714 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200715 addr = lp_build_add(&bld_base->uint_bld, addr,
716 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200717
Marek Olšák250aa932014-05-06 14:10:47 +0200718 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
719 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500720
Christian Königf5298b02013-02-28 14:50:07 +0100721 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500722}
723
Michel Dänzer26c71392012-08-24 12:03:11 +0200724/* Initialize arguments for the shader export intrinsic */
725static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900726 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200727 unsigned target,
728 LLVMValueRef *args)
729{
730 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
731 struct lp_build_context *uint =
732 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
733 struct lp_build_context *base = &bld_base->base;
734 unsigned compressed = 0;
735 unsigned chan;
736
Michel Dänzerf402acd2012-08-22 18:15:36 +0200737 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
738 int cbuf = target - V_008DFC_SQ_EXP_MRT;
739
740 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100741 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100742
743 if (compressed)
744 si_shader_ctx->shader->spi_shader_col_format |=
745 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
746 else
747 si_shader_ctx->shader->spi_shader_col_format |=
748 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200749
750 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200751 }
752 }
753
754 if (compressed) {
755 /* Pixel shader needs to pack output values before export */
756 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900757 args[0] = values[2 * chan];
758 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200759 args[chan + 5] =
760 build_intrinsic(base->gallivm->builder,
761 "llvm.SI.packf16",
762 LLVMInt32TypeInContext(base->gallivm->context),
763 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100764 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100765 args[chan + 7] = args[chan + 5] =
766 LLVMBuildBitCast(base->gallivm->builder,
767 args[chan + 5],
768 LLVMFloatTypeInContext(base->gallivm->context),
769 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200770 }
771
772 /* Set COMPR flag */
773 args[4] = uint->one;
774 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900775 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200776 /* +5 because the first output value will be
777 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900778 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200779
780 /* Clear COMPR flag */
781 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200782 }
783
784 /* XXX: This controls which components of the output
785 * registers actually get exported. (e.g bit 0 means export
786 * X component, bit 1 means export Y component, etc.) I'm
787 * hard coding this to 0xf for now. In the future, we might
788 * want to do something else. */
789 args[0] = lp_build_const_int32(base->gallivm, 0xf);
790
791 /* Specify whether the EXEC mask represents the valid mask */
792 args[1] = uint->zero;
793
794 /* Specify whether this is the last export */
795 args[2] = uint->zero;
796
797 /* Specify the target we are exporting */
798 args[3] = lp_build_const_int32(base->gallivm, target);
799
Michel Dänzer26c71392012-08-24 12:03:11 +0200800 /* XXX: We probably need to keep track of the output
801 * values, so we know what we are passing to the next
802 * stage. */
803}
804
Michel Dänzer404b29d2013-11-21 16:45:28 +0900805/* Load from output pointers and initialize arguments for the shader export intrinsic */
806static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
807 LLVMValueRef *out_ptr,
808 unsigned target,
809 LLVMValueRef *args)
810{
811 struct gallivm_state *gallivm = bld_base->base.gallivm;
812 LLVMValueRef values[4];
813 int i;
814
815 for (i = 0; i < 4; i++)
816 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
817
818 si_llvm_init_export_args(bld_base, values, target, args);
819}
820
Michel Dänzer7708a862012-11-02 15:57:30 +0100821static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900822 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100823{
824 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
825 struct gallivm_state *gallivm = bld_base->base.gallivm;
826
Christian Königa0dca442013-03-22 15:59:22 +0100827 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400828 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
829 SI_PARAM_ALPHA_REF);
830
Michel Dänzer7708a862012-11-02 15:57:30 +0100831 LLVMValueRef alpha_pass =
832 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100833 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900834 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400835 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100836 LLVMValueRef arg =
837 lp_build_select(&bld_base->base,
838 alpha_pass,
839 lp_build_const_float(gallivm, 1.0f),
840 lp_build_const_float(gallivm, -1.0f));
841
842 build_intrinsic(gallivm->builder,
843 "llvm.AMDGPU.kill",
844 LLVMVoidTypeInContext(gallivm->context),
845 &arg, 1, 0);
846 } else {
847 build_intrinsic(gallivm->builder,
848 "llvm.AMDGPU.kilp",
849 LLVMVoidTypeInContext(gallivm->context),
850 NULL, 0, 0);
851 }
Marek Olšákadc57972014-09-19 17:07:07 +0200852
853 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100854}
855
Michel Dänzere3befbc2013-05-15 18:09:50 +0200856static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900857 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200858{
859 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200860 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200861 struct lp_build_context *base = &bld_base->base;
862 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200863 unsigned reg_index;
864 unsigned chan;
865 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200866 LLVMValueRef base_elt;
867 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200868 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák2fd42002013-10-25 11:45:47 +0200869 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200870
Michel Dänzere3befbc2013-05-15 18:09:50 +0200871 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200872 LLVMValueRef *args = pos[2 + reg_index];
873
Marek Olšák8c37c162014-09-16 18:40:07 +0200874 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200875
Michel Dänzere3befbc2013-05-15 18:09:50 +0200876 args[5] =
877 args[6] =
878 args[7] =
879 args[8] = lp_build_const_float(base->gallivm, 0.0f);
880
881 /* Compute dot products of position and user clip plane vectors */
882 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
883 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200884 args[1] = lp_build_const_int32(base->gallivm,
885 ((reg_index * 4 + chan) * 4 +
886 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200887 base_elt = load_const(base->gallivm->builder, const_resource,
888 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200889 args[5 + chan] =
890 lp_build_add(base, args[5 + chan],
891 lp_build_mul(base, base_elt,
892 out_elts[const_chan]));
893 }
894 }
895
896 args[0] = lp_build_const_int32(base->gallivm, 0xf);
897 args[1] = uint->zero;
898 args[2] = uint->zero;
899 args[3] = lp_build_const_int32(base->gallivm,
900 V_008DFC_SQ_EXP_POS + 2 + reg_index);
901 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200902 }
903}
904
Marek Olšák8d03d922013-09-01 23:59:06 +0200905static void si_dump_streamout(struct pipe_stream_output_info *so)
906{
907 unsigned i;
908
909 if (so->num_outputs)
910 fprintf(stderr, "STREAMOUT\n");
911
912 for (i = 0; i < so->num_outputs; i++) {
913 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
914 so->output[i].start_component;
915 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
916 i, so->output[i].output_buffer,
917 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
918 so->output[i].register_index,
919 mask & 1 ? "x" : "",
920 mask & 2 ? "y" : "",
921 mask & 4 ? "z" : "",
922 mask & 8 ? "w" : "");
923 }
924}
925
926/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
927 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
928 * or v4i32 (num_channels=3,4). */
929static void build_tbuffer_store(struct si_shader_context *shader,
930 LLVMValueRef rsrc,
931 LLVMValueRef vdata,
932 unsigned num_channels,
933 LLVMValueRef vaddr,
934 LLVMValueRef soffset,
935 unsigned inst_offset,
936 unsigned dfmt,
937 unsigned nfmt,
938 unsigned offen,
939 unsigned idxen,
940 unsigned glc,
941 unsigned slc,
942 unsigned tfe)
943{
944 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
945 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
946 LLVMValueRef args[] = {
947 rsrc,
948 vdata,
949 LLVMConstInt(i32, num_channels, 0),
950 vaddr,
951 soffset,
952 LLVMConstInt(i32, inst_offset, 0),
953 LLVMConstInt(i32, dfmt, 0),
954 LLVMConstInt(i32, nfmt, 0),
955 LLVMConstInt(i32, offen, 0),
956 LLVMConstInt(i32, idxen, 0),
957 LLVMConstInt(i32, glc, 0),
958 LLVMConstInt(i32, slc, 0),
959 LLVMConstInt(i32, tfe, 0)
960 };
961
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900962 /* The instruction offset field has 12 bits */
963 assert(offen || inst_offset < (1 << 12));
964
Marek Olšák8d03d922013-09-01 23:59:06 +0200965 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
966 unsigned func = CLAMP(num_channels, 1, 3) - 1;
967 const char *types[] = {"i32", "v2i32", "v4i32"};
968 char name[256];
969 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
970
971 lp_build_intrinsic(gallivm->builder, name,
972 LLVMVoidTypeInContext(gallivm->context),
973 args, Elements(args));
974}
975
976static void build_streamout_store(struct si_shader_context *shader,
977 LLVMValueRef rsrc,
978 LLVMValueRef vdata,
979 unsigned num_channels,
980 LLVMValueRef vaddr,
981 LLVMValueRef soffset,
982 unsigned inst_offset)
983{
984 static unsigned dfmt[] = {
985 V_008F0C_BUF_DATA_FORMAT_32,
986 V_008F0C_BUF_DATA_FORMAT_32_32,
987 V_008F0C_BUF_DATA_FORMAT_32_32_32,
988 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
989 };
990 assert(num_channels >= 1 && num_channels <= 4);
991
992 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
993 inst_offset, dfmt[num_channels-1],
994 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
995}
996
997/* On SI, the vertex shader is responsible for writing streamout data
998 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900999static void si_llvm_emit_streamout(struct si_shader_context *shader,
1000 struct si_shader_output_values *outputs,
1001 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +02001002{
1003 struct pipe_stream_output_info *so = &shader->shader->selector->so;
1004 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
1005 LLVMBuilderRef builder = gallivm->builder;
1006 int i, j;
1007 struct lp_build_if_state if_ctx;
1008
1009 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1010
1011 LLVMValueRef so_param =
1012 LLVMGetParam(shader->radeon_bld.main_fn,
1013 shader->param_streamout_config);
1014
1015 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
1016 LLVMValueRef so_vtx_count =
1017 LLVMBuildAnd(builder,
1018 LLVMBuildLShr(builder, so_param,
1019 LLVMConstInt(i32, 16, 0), ""),
1020 LLVMConstInt(i32, 127, 0), "");
1021
1022 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
1023 NULL, 0, LLVMReadNoneAttribute);
1024
1025 /* can_emit = tid < so_vtx_count; */
1026 LLVMValueRef can_emit =
1027 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
1028
1029 /* Emit the streamout code conditionally. This actually avoids
1030 * out-of-bounds buffer access. The hw tells us via the SGPR
1031 * (so_vtx_count) which threads are allowed to emit streamout data. */
1032 lp_build_if(&if_ctx, gallivm, can_emit);
1033 {
1034 /* The buffer offset is computed as follows:
1035 * ByteOffset = streamout_offset[buffer_id]*4 +
1036 * (streamout_write_index + thread_id)*stride[buffer_id] +
1037 * attrib_offset
1038 */
1039
1040 LLVMValueRef so_write_index =
1041 LLVMGetParam(shader->radeon_bld.main_fn,
1042 shader->param_streamout_write_index);
1043
1044 /* Compute (streamout_write_index + thread_id). */
1045 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1046
1047 /* Compute the write offset for each enabled buffer. */
1048 LLVMValueRef so_write_offset[4] = {};
1049 for (i = 0; i < 4; i++) {
1050 if (!so->stride[i])
1051 continue;
1052
1053 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1054 shader->param_streamout_offset[i]);
1055 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1056
1057 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1058 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1059 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1060 }
1061
Marek Olšák8d03d922013-09-01 23:59:06 +02001062 /* Write streamout data. */
1063 for (i = 0; i < so->num_outputs; i++) {
1064 unsigned buf_idx = so->output[i].output_buffer;
1065 unsigned reg = so->output[i].register_index;
1066 unsigned start = so->output[i].start_component;
1067 unsigned num_comps = so->output[i].num_components;
1068 LLVMValueRef out[4];
1069
1070 assert(num_comps && num_comps <= 4);
1071 if (!num_comps || num_comps > 4)
1072 continue;
1073
1074 /* Load the output as int. */
1075 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001076 unsigned outidx = 0;
1077
1078 while (outidx < noutput && outputs[outidx].index != reg)
1079 outidx++;
1080
1081 if (outidx < noutput)
1082 out[j] = LLVMBuildBitCast(builder,
1083 outputs[outidx].values[start+j],
1084 i32, "");
1085 else
1086 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +02001087 }
1088
Michel Dänzer67e385b2014-01-08 17:48:21 +09001089 if (!out[0])
1090 continue;
1091
Marek Olšák8d03d922013-09-01 23:59:06 +02001092 /* Pack the output. */
1093 LLVMValueRef vdata = NULL;
1094
1095 switch (num_comps) {
1096 case 1: /* as i32 */
1097 vdata = out[0];
1098 break;
1099 case 2: /* as v2i32 */
1100 case 3: /* as v4i32 (aligned to 4) */
1101 case 4: /* as v4i32 */
1102 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1103 for (j = 0; j < num_comps; j++) {
1104 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1105 LLVMConstInt(i32, j, 0), "");
1106 }
1107 break;
1108 }
1109
1110 build_streamout_store(shader, shader->so_buffers[buf_idx],
1111 vdata, num_comps,
1112 so_write_offset[buf_idx],
1113 LLVMConstInt(i32, 0, 0),
1114 so->output[i].dst_offset*4);
1115 }
1116 }
1117 lp_build_endif(&if_ctx);
1118}
1119
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001120
Michel Dänzer404b29d2013-11-21 16:45:28 +09001121/* Generate export instructions for hardware VS shader stage */
1122static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1123 struct si_shader_output_values *outputs,
1124 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001125{
1126 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001127 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001128 struct lp_build_context * base = &bld_base->base;
1129 struct lp_build_context * uint =
1130 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001131 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001132 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001133 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001134 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001135 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001136 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001137 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001138 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001139
Michel Dänzer67e385b2014-01-08 17:48:21 +09001140 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1141 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001142 }
1143
Michel Dänzer404b29d2013-11-21 16:45:28 +09001144 for (i = 0; i < noutput; i++) {
1145 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001146 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001147
Michel Dänzer0afeea52013-05-02 14:53:17 +02001148handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001149 /* Select the correct target */
1150 switch(semantic_name) {
1151 case TGSI_SEMANTIC_PSIZE:
1152 shader->vs_out_misc_write = true;
1153 shader->vs_out_point_size = true;
1154 psize_value = outputs[i].values[0];
1155 continue;
1156 case TGSI_SEMANTIC_EDGEFLAG:
1157 shader->vs_out_misc_write = true;
1158 shader->vs_out_edgeflag = true;
1159 edgeflag_value = outputs[i].values[0];
1160 continue;
1161 case TGSI_SEMANTIC_LAYER:
1162 shader->vs_out_misc_write = true;
1163 shader->vs_out_layer = true;
1164 layer_value = outputs[i].values[0];
1165 continue;
1166 case TGSI_SEMANTIC_POSITION:
1167 target = V_008DFC_SQ_EXP_POS;
1168 break;
1169 case TGSI_SEMANTIC_COLOR:
1170 case TGSI_SEMANTIC_BCOLOR:
1171 target = V_008DFC_SQ_EXP_PARAM + param_count;
1172 shader->output[i].param_offset = param_count;
1173 param_count++;
1174 break;
1175 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001176 shader->clip_dist_write |=
Marek Olšáka9592cd2014-10-04 19:09:09 +02001177 0xf << (semantic_index * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001178 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1179 break;
1180 case TGSI_SEMANTIC_CLIPVERTEX:
1181 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1182 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001183 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001184 case TGSI_SEMANTIC_FOG:
1185 case TGSI_SEMANTIC_GENERIC:
1186 target = V_008DFC_SQ_EXP_PARAM + param_count;
1187 shader->output[i].param_offset = param_count;
1188 param_count++;
1189 break;
1190 default:
1191 target = 0;
1192 fprintf(stderr,
1193 "Warning: SI unhandled vs output type:%d\n",
1194 semantic_name);
1195 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001196
Michel Dänzer404b29d2013-11-21 16:45:28 +09001197 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001198
Michel Dänzer404b29d2013-11-21 16:45:28 +09001199 if (target >= V_008DFC_SQ_EXP_POS &&
1200 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1201 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1202 args, sizeof(args));
1203 } else {
1204 lp_build_intrinsic(base->gallivm->builder,
1205 "llvm.SI.export",
1206 LLVMVoidTypeInContext(base->gallivm->context),
1207 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001208 }
1209
1210 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1211 semantic_name = TGSI_SEMANTIC_GENERIC;
1212 goto handle_semantic;
1213 }
1214 }
1215
1216 /* We need to add the position output manually if it's missing. */
1217 if (!pos_args[0][0]) {
1218 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1219 pos_args[0][1] = uint->zero; /* EXEC mask */
1220 pos_args[0][2] = uint->zero; /* last export? */
1221 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1222 pos_args[0][4] = uint->zero; /* COMPR flag */
1223 pos_args[0][5] = base->zero; /* X */
1224 pos_args[0][6] = base->zero; /* Y */
1225 pos_args[0][7] = base->zero; /* Z */
1226 pos_args[0][8] = base->one; /* W */
1227 }
1228
1229 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1230 if (shader->vs_out_misc_write) {
1231 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1232 shader->vs_out_point_size |
1233 (shader->vs_out_edgeflag << 1) |
1234 (shader->vs_out_layer << 2));
1235 pos_args[1][1] = uint->zero; /* EXEC mask */
1236 pos_args[1][2] = uint->zero; /* last export? */
1237 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1238 pos_args[1][4] = uint->zero; /* COMPR flag */
1239 pos_args[1][5] = base->zero; /* X */
1240 pos_args[1][6] = base->zero; /* Y */
1241 pos_args[1][7] = base->zero; /* Z */
1242 pos_args[1][8] = base->zero; /* W */
1243
Michel Dänzer404b29d2013-11-21 16:45:28 +09001244 if (shader->vs_out_point_size)
1245 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001246
1247 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001248 /* The output is a float, but the hw expects an integer
1249 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001250 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1251 edgeflag_value,
1252 bld_base->uint_bld.elem_type, "");
1253 edgeflag_value = lp_build_min(&bld_base->int_bld,
1254 edgeflag_value,
1255 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001256
1257 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001258 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1259 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001260 base->elem_type, "");
1261 }
1262
Michel Dänzer404b29d2013-11-21 16:45:28 +09001263 if (shader->vs_out_layer)
1264 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001265 }
1266
1267 for (i = 0; i < 4; i++)
1268 if (pos_args[i][0])
1269 shader->nr_pos_exports++;
1270
1271 pos_idx = 0;
1272 for (i = 0; i < 4; i++) {
1273 if (!pos_args[i][0])
1274 continue;
1275
1276 /* Specify the target we are exporting */
1277 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1278
1279 if (pos_idx == shader->nr_pos_exports)
1280 /* Specify that this is the last export */
1281 pos_args[i][2] = uint->one;
1282
1283 lp_build_intrinsic(base->gallivm->builder,
1284 "llvm.SI.export",
1285 LLVMVoidTypeInContext(base->gallivm->context),
1286 pos_args[i], 9);
1287 }
1288}
1289
Michel Dänzer404b29d2013-11-21 16:45:28 +09001290static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1291{
1292 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1293 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001294 struct si_shader *es = si_shader_ctx->shader;
Marek Olšák216cf862014-10-04 17:04:05 +02001295 struct tgsi_shader_info *info = &es->selector->info;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001296 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001297 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1298 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001299 LLVMValueRef t_list_ptr;
1300 LLVMValueRef t_list;
1301 unsigned chan;
1302 int i;
1303
Michel Dänzer404b29d2013-11-21 16:45:28 +09001304 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001305 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1306 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001307 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001308 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001309
Marek Olšák216cf862014-10-04 17:04:05 +02001310 for (i = 0; i < info->num_outputs; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001311 LLVMValueRef *out_ptr =
Marek Olšák216cf862014-10-04 17:04:05 +02001312 si_shader_ctx->radeon_bld.soa.outputs[i];
1313 int param_index = get_param_index(info->output_semantic_name[i],
1314 info->output_semantic_index[i],
Marek Olšáke29353f2014-09-17 22:17:02 +02001315 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001316
Marek Olšáke29353f2014-09-17 22:17:02 +02001317 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001318 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001319
1320 for (chan = 0; chan < 4; chan++) {
1321 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001322 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1323
1324 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001325 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001326 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001327 V_008F0C_BUF_DATA_FORMAT_32,
1328 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001329 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001330 }
1331 }
1332}
1333
1334static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1335{
1336 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1337 struct gallivm_state *gallivm = bld_base->base.gallivm;
1338 LLVMValueRef args[2];
1339
1340 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1341 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1342 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1343 LLVMVoidTypeInContext(gallivm->context), args, 2,
1344 LLVMNoUnwindAttribute);
1345}
1346
1347static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1348{
1349 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1350 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001351 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001352 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1353 struct si_shader_output_values *outputs = NULL;
1354 unsigned noutput = 0;
1355 int i;
1356
1357 while (!tgsi_parse_end_of_tokens(parse)) {
1358 struct tgsi_full_declaration *d =
1359 &parse->FullToken.FullDeclaration;
1360 unsigned index;
1361
1362 tgsi_parse_token(parse);
1363
1364 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1365 continue;
1366
Marek Olšák8c37c162014-09-16 18:40:07 +02001367 i = si_store_shader_io_attribs(shader, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001368 if (i < 0)
1369 continue;
1370
1371 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1372 (noutput + 1) * sizeof(outputs[0]));
1373 for (index = d->Range.First; index <= d->Range.Last; index++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001374 outputs[noutput].index = index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001375 outputs[noutput].name = d->Semantic.Name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001376 outputs[noutput].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001377
1378 for (i = 0; i < 4; i++)
1379 outputs[noutput].values[i] =
1380 LLVMBuildLoad(gallivm->builder,
1381 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1382 "");
1383 }
1384 noutput++;
1385 }
1386
1387 si_llvm_export_vs(bld_base, outputs, noutput);
1388 FREE(outputs);
1389}
1390
Michel Dänzer51f89a02013-12-09 15:33:53 +09001391static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1392{
1393 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001394 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001395 struct lp_build_context * base = &bld_base->base;
1396 struct lp_build_context * uint =
1397 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1398 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1399 LLVMValueRef args[9];
1400 LLVMValueRef last_args[9] = { 0 };
1401 unsigned semantic_name;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001402 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001403 int i;
1404
1405 while (!tgsi_parse_end_of_tokens(parse)) {
1406 struct tgsi_full_declaration *d =
1407 &parse->FullToken.FullDeclaration;
1408 unsigned target;
1409 unsigned index;
1410
1411 tgsi_parse_token(parse);
1412
Michel Dänzer51f89a02013-12-09 15:33:53 +09001413 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1414 continue;
1415
1416 i = si_store_shader_io_attribs(shader, d);
1417 if (i < 0)
1418 continue;
1419
1420 semantic_name = d->Semantic.Name;
1421 for (index = d->Range.First; index <= d->Range.Last; index++) {
1422 /* Select the correct target */
1423 switch(semantic_name) {
1424 case TGSI_SEMANTIC_POSITION:
1425 depth_index = index;
1426 continue;
1427 case TGSI_SEMANTIC_STENCIL:
1428 stencil_index = index;
1429 continue;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001430 case TGSI_SEMANTIC_SAMPLEMASK:
1431 samplemask_index = index;
1432 continue;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001433 case TGSI_SEMANTIC_COLOR:
1434 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1435 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001436 LLVMBuildStore(bld_base->base.gallivm->builder,
1437 bld_base->base.one,
1438 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1439
Michel Dänzer51f89a02013-12-09 15:33:53 +09001440 if (d->Semantic.Index == 0 &&
1441 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001442 si_alpha_test(bld_base,
1443 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001444 break;
1445 default:
1446 target = 0;
1447 fprintf(stderr,
1448 "Warning: SI unhandled fs output type:%d\n",
1449 semantic_name);
1450 }
1451
Michel Dänzer404b29d2013-11-21 16:45:28 +09001452 si_llvm_init_export_args_load(bld_base,
1453 si_shader_ctx->radeon_bld.soa.outputs[index],
1454 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001455
1456 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001457 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001458 if (last_args[0]) {
1459 lp_build_intrinsic(base->gallivm->builder,
1460 "llvm.SI.export",
1461 LLVMVoidTypeInContext(base->gallivm->context),
1462 last_args, 9);
1463 }
1464
Marek Olšák0eb528a2013-12-04 13:24:22 +01001465 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001466 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001467
1468 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02001469 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
Marek Olšák7a645c52014-10-04 22:09:16 +02001470 d->Semantic.Index == 0 &&
Marek Olšák34e82002014-10-04 20:44:23 +02001471 si_shader_ctx->shader->key.ps.last_cbuf > 0) {
1472 for (int c = 1; c <= si_shader_ctx->shader->key.ps.last_cbuf; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001473 si_llvm_init_export_args_load(bld_base,
1474 si_shader_ctx->radeon_bld.soa.outputs[index],
1475 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001476 lp_build_intrinsic(base->gallivm->builder,
1477 "llvm.SI.export",
1478 LLVMVoidTypeInContext(base->gallivm->context),
1479 args, 9);
1480 }
1481 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001482 } else {
1483 lp_build_intrinsic(base->gallivm->builder,
1484 "llvm.SI.export",
1485 LLVMVoidTypeInContext(base->gallivm->context),
1486 args, 9);
1487 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001488 }
1489 }
1490
Marek Olšákd0e8b652014-05-06 20:04:31 +02001491 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001492 LLVMValueRef out_ptr;
1493 unsigned mask = 0;
1494
1495 /* Specify the target we are exporting */
1496 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1497
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001498 args[5] = base->zero; /* R, depth */
1499 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1500 args[7] = base->zero; /* B, sample mask */
1501 args[8] = base->zero; /* A, alpha to mask */
1502
Michel Dänzer1a616c12012-11-13 17:35:09 +01001503 if (depth_index >= 0) {
1504 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1505 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1506 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001507 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001508 }
1509
1510 if (stencil_index >= 0) {
1511 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001512 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001513 /* Only setting the stencil component bit (0x2) here
1514 * breaks some stencil piglit tests
1515 */
1516 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001517 si_shader_ctx->shader->db_shader_control |=
1518 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001519 }
1520
Marek Olšákd0e8b652014-05-06 20:04:31 +02001521 if (samplemask_index >= 0) {
1522 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1523 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1524 mask |= 0xf; /* Set all components. */
1525 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1526 }
1527
1528 if (samplemask_index >= 0)
1529 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1530 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001531 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1532 else
1533 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1534
Michel Dänzer1a616c12012-11-13 17:35:09 +01001535 /* Specify which components to enable */
1536 args[0] = lp_build_const_int32(base->gallivm, mask);
1537
1538 args[1] =
1539 args[2] =
1540 args[4] = uint->zero;
1541
1542 if (last_args[0])
1543 lp_build_intrinsic(base->gallivm->builder,
1544 "llvm.SI.export",
1545 LLVMVoidTypeInContext(base->gallivm->context),
1546 args, 9);
1547 else
1548 memcpy(last_args, args, sizeof(args));
1549 }
1550
Michel Dänzer51f89a02013-12-09 15:33:53 +09001551 if (!last_args[0]) {
1552 /* Specify which components to enable */
1553 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001554
Michel Dänzer51f89a02013-12-09 15:33:53 +09001555 /* Specify the target we are exporting */
1556 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001557
Michel Dänzer51f89a02013-12-09 15:33:53 +09001558 /* Set COMPR flag to zero to export data as 32-bit */
1559 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001560
Michel Dänzer51f89a02013-12-09 15:33:53 +09001561 /* dummy bits */
1562 last_args[5]= uint->zero;
1563 last_args[6]= uint->zero;
1564 last_args[7]= uint->zero;
1565 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001566 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001567
1568 /* Specify whether the EXEC mask represents the valid mask */
1569 last_args[1] = uint->one;
1570
1571 /* Specify that this is the last export */
1572 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1573
1574 lp_build_intrinsic(base->gallivm->builder,
1575 "llvm.SI.export",
1576 LLVMVoidTypeInContext(base->gallivm->context),
1577 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001578}
1579
Marek Olšák4855acd2013-08-06 15:08:54 +02001580static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1581 struct lp_build_tgsi_context * bld_base,
1582 struct lp_build_emit_data * emit_data);
1583
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001584static bool tgsi_is_shadow_sampler(unsigned target)
1585{
1586 return target == TGSI_TEXTURE_SHADOW1D ||
1587 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1588 target == TGSI_TEXTURE_SHADOW2D ||
1589 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1590 target == TGSI_TEXTURE_SHADOWCUBE ||
1591 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1592 target == TGSI_TEXTURE_SHADOWRECT;
1593}
1594
Marek Olšák877bb522014-06-25 03:12:46 +02001595static const struct lp_build_tgsi_action tex_action;
1596
Tom Stellarda75c6162012-01-06 17:38:37 -05001597static void tex_fetch_args(
1598 struct lp_build_tgsi_context * bld_base,
1599 struct lp_build_emit_data * emit_data)
1600{
Christian König55fe5cc2013-03-04 16:30:06 +01001601 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001602 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001603 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001604 unsigned opcode = inst->Instruction.Opcode;
1605 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001606 LLVMValueRef coords[4];
1607 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001608 int ref_pos;
1609 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001610 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001611 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001612 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1613 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001614 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001615
1616 if (target == TGSI_TEXTURE_BUFFER) {
1617 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1618 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1619 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1620 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1621
Marek Olšák4f3f0432014-07-07 22:49:15 +02001622 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001623 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1624 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1625 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1626 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1627
1628 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1629 emit_data->args[0] = res;
1630 emit_data->args[1] = bld_base->uint_bld.zero;
1631 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1632 emit_data->arg_count = 3;
1633 return;
1634 }
Tom Stellard467f5162012-05-16 15:15:35 -04001635
Michel Dänzer120efee2013-01-25 12:10:11 +01001636 /* Fetch and project texture coordinates */
1637 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001638 for (chan = 0; chan < 3; chan++ ) {
1639 coords[chan] = lp_build_emit_fetch(bld_base,
1640 emit_data->inst, 0,
1641 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001642 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001643 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1644 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001645 coords[chan],
1646 coords[3]);
1647 }
1648
Michel Dänzer120efee2013-01-25 12:10:11 +01001649 if (opcode == TGSI_OPCODE_TXP)
1650 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001651
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001652 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001653 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001654 /* The offsets are six-bit signed integers packed like this:
1655 * X=[5:0], Y=[13:8], and Z=[21:16].
1656 */
1657 LLVMValueRef offset[3], pack;
1658
1659 assert(inst->Texture.NumOffsets == 1);
1660
1661 for (chan = 0; chan < 3; chan++) {
1662 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1663 emit_data->inst, 0, chan);
1664 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1665 lp_build_const_int32(gallivm, 0x3f), "");
1666 if (chan)
1667 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1668 lp_build_const_int32(gallivm, chan*8), "");
1669 }
1670
1671 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1672 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1673 address[count++] = pack;
1674 }
1675
Michel Dänzer120efee2013-01-25 12:10:11 +01001676 /* Pack LOD bias value */
1677 if (opcode == TGSI_OPCODE_TXB)
1678 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001679 if (opcode == TGSI_OPCODE_TXB2)
1680 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001681
Michel Dänzer120efee2013-01-25 12:10:11 +01001682 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001683 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001684 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1685 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1686 } else {
1687 assert(ref_pos >= 0);
1688 address[count++] = coords[ref_pos];
1689 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001690 }
1691
Marek Olšákb279f0142014-07-04 03:19:58 +02001692 if (target == TGSI_TEXTURE_CUBE ||
1693 target == TGSI_TEXTURE_CUBE_ARRAY ||
1694 target == TGSI_TEXTURE_SHADOWCUBE ||
1695 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1696 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1697
Michel Dänzera6b83c02013-02-21 16:10:55 +01001698 /* Pack user derivatives */
1699 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001700 int num_deriv_channels, param;
1701
1702 switch (target) {
1703 case TGSI_TEXTURE_3D:
1704 num_deriv_channels = 3;
1705 break;
1706 case TGSI_TEXTURE_2D:
1707 case TGSI_TEXTURE_SHADOW2D:
1708 case TGSI_TEXTURE_RECT:
1709 case TGSI_TEXTURE_SHADOWRECT:
1710 case TGSI_TEXTURE_2D_ARRAY:
1711 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1712 case TGSI_TEXTURE_CUBE:
1713 case TGSI_TEXTURE_SHADOWCUBE:
1714 case TGSI_TEXTURE_CUBE_ARRAY:
1715 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1716 num_deriv_channels = 2;
1717 break;
1718 case TGSI_TEXTURE_1D:
1719 case TGSI_TEXTURE_SHADOW1D:
1720 case TGSI_TEXTURE_1D_ARRAY:
1721 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1722 num_deriv_channels = 1;
1723 break;
1724 default:
1725 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001726 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001727
1728 for (param = 1; param <= 2; param++)
1729 for (chan = 0; chan < num_deriv_channels; chan++)
1730 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001731 }
1732
Michel Dänzer120efee2013-01-25 12:10:11 +01001733 /* Pack texture coordinates */
1734 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001735 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001736 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001737 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001738 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001739
Marek Olšákd2bd6342013-09-18 15:40:21 +02001740 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001741 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001742 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001743 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001744 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001745
1746 if (count > 16) {
1747 assert(!"Cannot handle more than 16 texture address parameters");
1748 count = 16;
1749 }
1750
1751 for (chan = 0; chan < count; chan++ ) {
1752 address[chan] = LLVMBuildBitCast(gallivm->builder,
1753 address[chan],
1754 LLVMInt32TypeInContext(gallivm->context),
1755 "");
1756 }
1757
Marek Olšák4855acd2013-08-06 15:08:54 +02001758 /* Adjust the sample index according to FMASK.
1759 *
1760 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1761 * which is the identity mapping. Each nibble says which physical sample
1762 * should be fetched to get that sample.
1763 *
1764 * For example, 0x11111100 means there are only 2 samples stored and
1765 * the second sample covers 3/4 of the pixel. When reading samples 0
1766 * and 1, return physical sample 0 (determined by the first two 0s
1767 * in FMASK), otherwise return physical sample 1.
1768 *
1769 * The sample index should be adjusted as follows:
1770 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1771 */
1772 if (target == TGSI_TEXTURE_2D_MSAA ||
1773 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1774 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1775 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001776 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001777 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001778 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001779
Marek Olšákd2bd6342013-09-18 15:40:21 +02001780 memcpy(txf_address, address, sizeof(txf_address));
1781
1782 if (target == TGSI_TEXTURE_2D_MSAA) {
1783 txf_address[2] = bld_base->uint_bld.zero;
1784 }
1785 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001786
1787 /* Pad to a power-of-two size. */
1788 while (txf_count < util_next_power_of_two(txf_count))
1789 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1790
1791 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001792 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1793 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1794 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001795 txf_emit_data.chan = 0;
1796 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001797 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001798 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001799 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001800 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001801 txf_emit_data.arg_count = 3;
1802
Marek Olšák877bb522014-06-25 03:12:46 +02001803 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001804
1805 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001806 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1807 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1808
1809 /* Apply the formula. */
1810 LLVMValueRef fmask =
1811 LLVMBuildExtractElement(gallivm->builder,
1812 txf_emit_data.output[0],
1813 uint_bld->zero, "");
1814
Marek Olšákd2bd6342013-09-18 15:40:21 +02001815 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001816
1817 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001818 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001819
1820 LLVMValueRef shifted_fmask =
1821 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1822
1823 LLVMValueRef final_sample =
1824 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1825
1826 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1827 * resource descriptor is 0 (invalid),
1828 */
1829 LLVMValueRef fmask_desc =
1830 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001831 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001832 LLVMVectorType(uint_bld->elem_type, 8), "");
1833
1834 LLVMValueRef fmask_word1 =
1835 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1836 uint_bld->one, "");
1837
1838 LLVMValueRef word1_is_nonzero =
1839 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1840 fmask_word1, uint_bld->zero, "");
1841
Marek Olšákd2bd6342013-09-18 15:40:21 +02001842 /* Replace the MSAA sample index. */
1843 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001844 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001845 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001846 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001847
Michel Dänzer36231112013-05-02 09:44:45 +02001848 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001849 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001850
1851 if (opcode == TGSI_OPCODE_TXF) {
1852 /* add tex offsets */
1853 if (inst->Texture.NumOffsets) {
1854 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1855 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1856 const struct tgsi_texture_offset * off = inst->TexOffsets;
1857
1858 assert(inst->Texture.NumOffsets == 1);
1859
Marek Olšákdefedc02013-09-18 15:36:38 +02001860 switch (target) {
1861 case TGSI_TEXTURE_3D:
1862 address[2] = lp_build_add(uint_bld, address[2],
1863 bld->immediates[off->Index][off->SwizzleZ]);
1864 /* fall through */
1865 case TGSI_TEXTURE_2D:
1866 case TGSI_TEXTURE_SHADOW2D:
1867 case TGSI_TEXTURE_RECT:
1868 case TGSI_TEXTURE_SHADOWRECT:
1869 case TGSI_TEXTURE_2D_ARRAY:
1870 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001871 address[1] =
1872 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001873 bld->immediates[off->Index][off->SwizzleY]);
1874 /* fall through */
1875 case TGSI_TEXTURE_1D:
1876 case TGSI_TEXTURE_SHADOW1D:
1877 case TGSI_TEXTURE_1D_ARRAY:
1878 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1879 address[0] =
1880 lp_build_add(uint_bld, address[0],
1881 bld->immediates[off->Index][off->SwizzleX]);
1882 break;
1883 /* texture offsets do not apply to other texture targets */
1884 }
Michel Dänzer36231112013-05-02 09:44:45 +02001885 }
1886
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001887 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1888 emit_data->arg_count = 3;
1889
Michel Dänzer36231112013-05-02 09:44:45 +02001890 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001891 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001892 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001893 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001894 opcode == TGSI_OPCODE_LODQ ||
1895 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001896 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1897 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1898 target == TGSI_TEXTURE_2D_ARRAY ||
1899 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1900 target == TGSI_TEXTURE_CUBE_ARRAY ||
1901 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1902 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001903 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001904
Marek Olšák57f3da92014-06-16 16:53:42 +02001905 if (opcode == TGSI_OPCODE_TG4) {
1906 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001907
Marek Olšák57f3da92014-06-16 16:53:42 +02001908 /* DMASK was repurposed for GATHER4. 4 components are always
1909 * returned and DMASK works like a swizzle - it selects
1910 * the component to fetch. The only valid DMASK values are
1911 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1912 * (red,red,red,red) etc.) The ISA document doesn't mention
1913 * this.
1914 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001915
Marek Olšák57f3da92014-06-16 16:53:42 +02001916 /* Get the component index from src1.x for Gather4. */
1917 if (!tgsi_is_shadow_sampler(target)) {
1918 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1919 LLVMValueRef comp_imm;
1920 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001921
Marek Olšák57f3da92014-06-16 16:53:42 +02001922 assert(src1.File == TGSI_FILE_IMMEDIATE);
1923
1924 comp_imm = imms[src1.Index][src1.SwizzleX];
1925 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1926 gather_comp = CLAMP(gather_comp, 0, 3);
1927 }
1928
1929 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001930 }
1931
Marek Olšák4855acd2013-08-06 15:08:54 +02001932 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001933 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001934 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1935 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1936 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1937 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1938 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1939 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1940 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1941
1942 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001943
1944 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001945 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001946 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001947 } else {
1948 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1949 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001950 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001951
1952 emit_data->dst_type = LLVMVectorType(
1953 LLVMFloatTypeInContext(gallivm->context),
1954 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001955 }
1956
Marek Olšák2484daa2014-04-22 21:23:29 +02001957 /* The fetch opcode has been converted to a 2D array fetch.
1958 * This simplifies the LLVM backend. */
1959 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1960 target = TGSI_TEXTURE_2D_ARRAY;
1961 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1962 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1963
Michel Dänzer120efee2013-01-25 12:10:11 +01001964 /* Pad to power of two vector */
1965 while (count < util_next_power_of_two(count))
1966 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1967
Christian Königccf3e8f2013-03-26 15:09:27 +01001968 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001969}
1970
Michel Dänzer07eddc42013-02-06 15:43:10 +01001971static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1972 struct lp_build_tgsi_context * bld_base,
1973 struct lp_build_emit_data * emit_data)
1974{
1975 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001976 unsigned opcode = emit_data->inst->Instruction.Opcode;
1977 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001978 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001979 bool has_offset = HAVE_LLVM >= 0x0305 ?
1980 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001981
Marek Olšák877bb522014-06-25 03:12:46 +02001982 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001983 emit_data->output[emit_data->chan] = build_intrinsic(
1984 base->gallivm->builder,
1985 "llvm.SI.vs.load.input", emit_data->dst_type,
1986 emit_data->args, emit_data->arg_count,
1987 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1988 return;
1989 }
1990
Marek Olšák877bb522014-06-25 03:12:46 +02001991 if (opcode == TGSI_OPCODE_TG4 ||
1992 opcode == TGSI_OPCODE_LODQ ||
1993 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1994 bool is_shadow = tgsi_is_shadow_sampler(target);
1995 const char *name = "llvm.SI.image.sample";
1996 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001997
Marek Olšák877bb522014-06-25 03:12:46 +02001998 switch (opcode) {
1999 case TGSI_OPCODE_TEX:
2000 case TGSI_OPCODE_TEX2:
2001 case TGSI_OPCODE_TXP:
2002 break;
2003 case TGSI_OPCODE_TXB:
2004 case TGSI_OPCODE_TXB2:
2005 infix = ".b";
2006 break;
2007 case TGSI_OPCODE_TXL:
2008 case TGSI_OPCODE_TXL2:
2009 infix = ".l";
2010 break;
2011 case TGSI_OPCODE_TXD:
2012 infix = ".d";
2013 break;
2014 case TGSI_OPCODE_TG4:
2015 name = "llvm.SI.gather4";
2016 break;
2017 case TGSI_OPCODE_LODQ:
2018 name = "llvm.SI.getlod";
2019 is_shadow = false;
2020 has_offset = false;
2021 break;
2022 default:
2023 assert(0);
2024 return;
2025 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01002026
Marek Olšák877bb522014-06-25 03:12:46 +02002027 /* Add the type and suffixes .c, .o if needed. */
2028 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
2029 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
2030 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002031
Marek Olšák877bb522014-06-25 03:12:46 +02002032 emit_data->output[emit_data->chan] = build_intrinsic(
2033 base->gallivm->builder, intr_name, emit_data->dst_type,
2034 emit_data->args, emit_data->arg_count,
2035 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2036 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02002037 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02002038 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002039
Marek Olšák877bb522014-06-25 03:12:46 +02002040 switch (opcode) {
2041 case TGSI_OPCODE_TEX:
2042 case TGSI_OPCODE_TEX2:
2043 case TGSI_OPCODE_TXP:
2044 name = "llvm.SI.sample";
2045 break;
2046 case TGSI_OPCODE_TXB:
2047 case TGSI_OPCODE_TXB2:
2048 name = "llvm.SI.sampleb";
2049 break;
2050 case TGSI_OPCODE_TXD:
2051 name = "llvm.SI.sampled";
2052 break;
2053 case TGSI_OPCODE_TXF:
2054 name = "llvm.SI.imageload";
2055 break;
2056 case TGSI_OPCODE_TXL:
2057 case TGSI_OPCODE_TXL2:
2058 name = "llvm.SI.samplel";
2059 break;
2060 default:
2061 assert(0);
2062 return;
2063 }
2064
Marek Olšákd859bdb2014-07-14 19:40:14 +02002065 i8 = LLVMInt8TypeInContext(base->gallivm->context);
2066 v16i8 = LLVMVectorType(i8, 16);
2067 v32i8 = LLVMVectorType(i8, 32);
2068
2069 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
2070 emit_data->args[1], v32i8, "");
2071 if (opcode != TGSI_OPCODE_TXF) {
2072 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
2073 emit_data->args[2], v16i8, "");
2074 }
2075
Marek Olšák877bb522014-06-25 03:12:46 +02002076 sprintf(intr_name, "%s.v%ui32", name,
2077 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
2078
2079 emit_data->output[emit_data->chan] = build_intrinsic(
2080 base->gallivm->builder, intr_name, emit_data->dst_type,
2081 emit_data->args, emit_data->arg_count,
2082 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2083 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002084}
2085
Michel Dänzer0495adb2013-05-06 12:45:14 +02002086static void txq_fetch_args(
2087 struct lp_build_tgsi_context * bld_base,
2088 struct lp_build_emit_data * emit_data)
2089{
2090 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2091 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002092 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002093 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002094
Marek Olšák2484daa2014-04-22 21:23:29 +02002095 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002096 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2097 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2098
2099 /* Read the size from the buffer descriptor directly. */
2100 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2101 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2102 size = LLVMBuildExtractElement(gallivm->builder, size,
2103 lp_build_const_int32(gallivm, 2), "");
2104 emit_data->args[0] = size;
2105 return;
2106 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002107
2108 /* Mip level */
2109 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2110
2111 /* Resource */
2112 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2113
Marek Olšák2484daa2014-04-22 21:23:29 +02002114 /* Texture target */
2115 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2116 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2117 target = TGSI_TEXTURE_2D_ARRAY;
2118
Michel Dänzer0495adb2013-05-06 12:45:14 +02002119 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002120 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002121
2122 emit_data->arg_count = 3;
2123
2124 emit_data->dst_type = LLVMVectorType(
2125 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2126 4);
2127}
2128
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002129static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2130 struct lp_build_tgsi_context * bld_base,
2131 struct lp_build_emit_data * emit_data)
2132{
Marek Olšák2484daa2014-04-22 21:23:29 +02002133 unsigned target = emit_data->inst->Texture.Texture;
2134
2135 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002136 /* Just return the buffer size. */
2137 emit_data->output[emit_data->chan] = emit_data->args[0];
2138 return;
2139 }
2140
2141 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002142
2143 /* Divide the number of layers by 6 to get the number of cubes. */
2144 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2145 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2146 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2147 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2148 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2149
2150 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2151 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2152 z = LLVMBuildSDiv(builder, z, six, "");
2153
2154 emit_data->output[emit_data->chan] =
2155 LLVMBuildInsertElement(builder, v4, z, two, "");
2156 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002157}
2158
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002159static void si_llvm_emit_ddxy(
2160 const struct lp_build_tgsi_action * action,
2161 struct lp_build_tgsi_context * bld_base,
2162 struct lp_build_emit_data * emit_data)
2163{
2164 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2165 struct gallivm_state *gallivm = bld_base->base.gallivm;
2166 struct lp_build_context * base = &bld_base->base;
2167 const struct tgsi_full_instruction *inst = emit_data->inst;
2168 unsigned opcode = inst->Instruction.Opcode;
2169 LLVMValueRef indices[2];
2170 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2171 LLVMValueRef tl, trbl, result[4];
2172 LLVMTypeRef i32;
2173 unsigned swizzle[4];
2174 unsigned c;
2175
2176 i32 = LLVMInt32TypeInContext(gallivm->context);
2177
2178 indices[0] = bld_base->uint_bld.zero;
2179 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2180 NULL, 0, LLVMReadNoneAttribute);
2181 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2182 indices, 2, "");
2183
2184 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2185 lp_build_const_int32(gallivm, 0xfffffffc), "");
2186 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2187 indices, 2, "");
2188
2189 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2190 lp_build_const_int32(gallivm,
2191 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2192 "");
2193 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2194 indices, 2, "");
2195
2196 for (c = 0; c < 4; ++c) {
2197 unsigned i;
2198
2199 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2200 for (i = 0; i < c; ++i) {
2201 if (swizzle[i] == swizzle[c]) {
2202 result[c] = result[i];
2203 break;
2204 }
2205 }
2206 if (i != c)
2207 continue;
2208
2209 LLVMBuildStore(gallivm->builder,
2210 LLVMBuildBitCast(gallivm->builder,
2211 lp_build_emit_fetch(bld_base, inst, 0, c),
2212 i32, ""),
2213 store_ptr);
2214
2215 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2216 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2217
2218 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2219 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2220
2221 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2222 }
2223
2224 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2225}
2226
Michel Dänzer404b29d2013-11-21 16:45:28 +09002227/* Emit one vertex from the geometry shader */
2228static void si_llvm_emit_vertex(
2229 const struct lp_build_tgsi_action *action,
2230 struct lp_build_tgsi_context *bld_base,
2231 struct lp_build_emit_data *emit_data)
2232{
2233 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002234 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002235 struct si_shader *shader = si_shader_ctx->shader;
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
Michel Dänzer404b29d2013-11-21 16:45:28 +09002286 for (i = 0; i < shader->noutput; i++) {
2287 LLVMValueRef *out_ptr =
2288 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
2289
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;
2697 LLVMValueRef t_list_ptr, t_list;
2698 LLVMValueRef args[9];
2699 int i, r;
2700
2701 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2702
2703 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002704 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002705
2706 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2707
2708 create_meta_data(si_shader_ctx);
2709 create_function(si_shader_ctx);
2710 preload_streamout_buffers(si_shader_ctx);
2711
2712 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002713 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2714 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002715 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002716 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002717
2718 args[0] = t_list;
2719 args[1] = lp_build_mul_imm(uint,
2720 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2721 si_shader_ctx->param_vertex_id),
2722 4);
2723 args[3] = uint->zero;
2724 args[4] = uint->one; /* OFFEN */
2725 args[5] = uint->zero; /* IDXEN */
2726 args[6] = uint->one; /* GLC */
2727 args[7] = uint->one; /* SLC */
2728 args[8] = uint->zero; /* TFE */
2729
2730 /* Fetch vertex data from GSVS ring */
2731 for (i = 0; i < gs->noutput; ++i) {
2732 struct si_shader_output *out = gs->output + i;
2733 unsigned chan;
2734
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002735 shader->output[i] = *out;
2736
Michel Dänzer404b29d2013-11-21 16:45:28 +09002737 outputs[i].name = out->name;
2738 outputs[i].index = out->index;
Michel Dänzer67e385b2014-01-08 17:48:21 +09002739 outputs[i].sid = out->sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002740
2741 for (chan = 0; chan < 4; chan++) {
2742 args[2] = lp_build_const_int32(gallivm,
2743 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002744 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002745
2746 outputs[i].values[chan] =
2747 LLVMBuildBitCast(gallivm->builder,
2748 build_intrinsic(gallivm->builder,
2749 "llvm.SI.buffer.load.dword.i32.i32",
2750 LLVMInt32TypeInContext(gallivm->context),
2751 args, 9,
2752 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2753 base->elem_type, "");
2754 }
2755 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002756 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002757
2758 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2759
2760 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2761
2762 if (dump)
2763 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2764
Marek Olšák1abb1a92014-09-17 22:44:22 +02002765 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002766 bld_base->base.gallivm->module);
2767
2768 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2769
2770 FREE(outputs);
2771 return r;
2772}
2773
Marek Olšák1abb1a92014-09-17 22:44:22 +02002774int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002775{
Marek Olšák2774abd2014-09-16 18:45:33 +02002776 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002777 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002778 struct lp_build_tgsi_context * bld_base;
2779 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002780 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002781 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002782
2783 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2784 * conversion fails. */
2785 if (dump) {
2786 tgsi_dump(sel->tokens, 0);
2787 si_dump_streamout(&sel->so);
2788 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002789
Marek Olšák8c37c162014-09-16 18:40:07 +02002790 assert(shader->noutput == 0);
2791 assert(shader->nparam == 0);
2792 assert(shader->ninput == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002793
Michel Dänzercfebaf92012-08-31 19:04:08 +02002794 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002795 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2796 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2797
Marek Olšák52335682014-09-30 15:12:09 +02002798 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002799 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2800
Marek Olšák52335682014-09-30 15:12:09 +02002801 shader->uses_instanceid = sel->info.uses_instanceid;
2802 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002803 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002804
2805 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002806 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002807 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2808 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002809 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002810 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2811 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2812 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002813 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002814 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002815 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2816 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002817
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002818 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2819 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002820
Michel Dänzer404b29d2013-11-21 16:45:28 +09002821 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2822 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2823
Christian Könige4ed5872013-03-21 18:02:52 +01002824 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002825 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002826 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2827 si_shader_ctx.shader = shader;
2828 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002829
Michel Dänzer51f89a02013-12-09 15:33:53 +09002830 switch (si_shader_ctx.type) {
2831 case TGSI_PROCESSOR_VERTEX:
2832 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002833 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002834 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2835 } else {
2836 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2837 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002838 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002839 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002840 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2841 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2842 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002843 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002844 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002845 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2846 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002847
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002848 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002849 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2850 shader->db_shader_control |=
2851 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2852 break;
2853 case TGSI_FS_DEPTH_LAYOUT_LESS:
2854 shader->db_shader_control |=
2855 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2856 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002857 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002858 break;
2859 default:
2860 assert(!"Unsupported shader type");
2861 return -1;
2862 }
2863
Christian König206f0592013-03-20 14:37:21 +01002864 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002865 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002866 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002867 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002868 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002869
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002870 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2871 si_shader_ctx.gs_next_vertex =
2872 lp_build_alloca(bld_base->base.gallivm,
2873 bld_base->uint_bld.elem_type, "");
2874 }
2875
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002876 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002877 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002878 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002879 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002880
2881 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2882
2883 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002884 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002885 if (r) {
2886 fprintf(stderr, "LLVM failed to compile shader\n");
2887 goto out;
2888 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002889
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002890 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002891
2892 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002893 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002894 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002895 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002896 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002897 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2898 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002899 free(shader->gs_copy_shader);
2900 shader->gs_copy_shader = NULL;
2901 goto out;
2902 }
2903 }
2904
Tom Stellarda75c6162012-01-06 17:38:37 -05002905 tgsi_parse_free(&si_shader_ctx.parse);
2906
Michel Dänzer404b29d2013-11-21 16:45:28 +09002907out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002908 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002909 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002910 FREE(si_shader_ctx.resources);
2911 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002912
Tom Stellard302f53d2012-10-25 13:50:10 -04002913 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002914}
2915
Marek Olšák2774abd2014-09-16 18:45:33 +02002916void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002917{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002918 if (shader->gs_copy_shader)
2919 si_shader_destroy(ctx, shader->gs_copy_shader);
2920
Marek Olšáka81c3e02013-08-14 01:04:39 +02002921 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002922 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002923}