blob: 726188524300905d9fc2de218dd9aa33c22ec8cb [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 unsigned usage;
55};
56
Tom Stellarda75c6162012-01-06 17:38:37 -050057struct si_shader_context
58{
59 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050060 struct tgsi_parse_context parse;
61 struct tgsi_token * tokens;
Marek Olšák8c37c162014-09-16 18:40:07 +020062 struct si_shader *shader;
Tom Stellarda75c6162012-01-06 17:38:37 -050063 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020064 int param_streamout_config;
65 int param_streamout_write_index;
66 int param_streamout_offset[4];
67 int param_vertex_id;
68 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010069 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020070 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020071 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020072 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Christian König1c100182013-03-17 16:02:42 +010073 LLVMValueRef *resources;
74 LLVMValueRef *samplers;
Marek Olšák8d03d922013-09-01 23:59:06 +020075 LLVMValueRef so_buffers[4];
Michel Dänzerf07a96d2014-01-08 18:45:10 +090076 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050077};
78
79static struct si_shader_context * si_shader_context(
80 struct lp_build_tgsi_context * bld_base)
81{
82 return (struct si_shader_context *)bld_base;
83}
84
85
86#define PERSPECTIVE_BASE 0
87#define LINEAR_BASE 9
88
89#define SAMPLE_OFFSET 0
90#define CENTER_OFFSET 2
91#define CENTROID_OFSET 4
92
93#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040094#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020095#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040096#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050097
Michel Dänzer404b29d2013-11-21 16:45:28 +090098
99#define SENDMSG_GS 2
100#define SENDMSG_GS_DONE 3
101
102#define SENDMSG_GS_OP_NOP (0 << 4)
103#define SENDMSG_GS_OP_CUT (1 << 4)
104#define SENDMSG_GS_OP_EMIT (2 << 4)
105#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
106
Marek Olšáke29353f2014-09-17 22:17:02 +0200107/**
108 * Returns a unique index for a semantic name and index. The index must be
109 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
110 * calculated.
111 */
112static unsigned get_unique_index(unsigned semantic_name, unsigned index)
113{
114 switch (semantic_name) {
115 case TGSI_SEMANTIC_POSITION:
116 return 0;
117 case TGSI_SEMANTIC_PSIZE:
118 return 1;
119 case TGSI_SEMANTIC_CLIPDIST:
120 assert(index <= 1);
121 return 2 + index;
122 case TGSI_SEMANTIC_CLIPVERTEX:
123 return 4;
124 case TGSI_SEMANTIC_COLOR:
125 assert(index <= 1);
126 return 5 + index;
127 case TGSI_SEMANTIC_BCOLOR:
128 assert(index <= 1);
129 return 7 + index;
130 case TGSI_SEMANTIC_FOG:
131 return 9;
132 case TGSI_SEMANTIC_EDGEFLAG:
133 return 10;
134 case TGSI_SEMANTIC_GENERIC:
135 assert(index <= 63-11);
136 return 11 + index;
137 default:
138 assert(0);
139 return 63;
140 }
141}
142
143/**
144 * Given a semantic name and index of a parameter and a mask of used parameters
145 * (inputs or outputs), return the index of the parameter in the list of all
146 * used parameters.
147 *
148 * For example, assume this list of parameters:
149 * POSITION, PSIZE, GENERIC0, GENERIC2
150 * which has the mask:
151 * 11000000000101
152 * Then:
153 * querying POSITION returns 0,
154 * querying PSIZE returns 1,
155 * querying GENERIC0 returns 2,
156 * querying GENERIC2 returns 3.
157 *
158 * Which can be used as an offset to a parameter buffer in units of vec4s.
159 */
160static int get_param_index(unsigned semantic_name, unsigned index,
161 uint64_t mask)
162{
163 unsigned unique_index = get_unique_index(semantic_name, index);
164 int i, param_index = 0;
165
166 /* If not present... */
167 if (!((1llu << unique_index) & mask))
168 return -1;
169
170 for (i = 0; mask; i++) {
171 uint64_t bit = 1llu << i;
172
173 if (bit & mask) {
174 if (i == unique_index)
175 return param_index;
176
177 mask &= ~bit;
178 param_index++;
179 }
180 }
181
182 assert(!"unreachable");
183 return -1;
184}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900185
Tom Stellard467f5162012-05-16 15:15:35 -0400186/**
187 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
188 *
189 * @param offset The offset parameter specifies the number of
190 * elements to offset, not the number of bytes or dwords. An element is the
191 * the type pointed to by the base_ptr parameter (e.g. int is the element of
192 * an int* pointer)
193 *
194 * When LLVM lowers the load instruction, it will convert the element offset
195 * into a dword offset automatically.
196 *
197 */
198static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100199 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400200 LLVMValueRef base_ptr,
201 LLVMValueRef offset)
202{
Christian König206f0592013-03-20 14:37:21 +0100203 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400204
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200205 LLVMValueRef indices[2] = {
206 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
207 offset
208 };
Christian König206f0592013-03-20 14:37:21 +0100209 LLVMValueRef computed_ptr = LLVMBuildGEP(
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200210 base->gallivm->builder, base_ptr, indices, 2, "");
Christian König206f0592013-03-20 14:37:21 +0100211
212 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
213 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
214 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400215}
216
Marek Olšákf317ce52013-09-05 15:39:57 +0200217static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100218 struct radeon_llvm_context * radeon_bld,
219 unsigned divisor)
220{
Marek Olšák8d03d922013-09-01 23:59:06 +0200221 struct si_shader_context *si_shader_ctx =
222 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100223 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
224
Marek Olšák8d03d922013-09-01 23:59:06 +0200225 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
226 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100227 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
228 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
229
230 if (divisor > 1)
231 result = LLVMBuildUDiv(gallivm->builder, result,
232 lp_build_const_int32(gallivm, divisor), "");
233
234 return result;
235}
236
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900237static int si_store_shader_io_attribs(struct si_shader *shader,
238 const struct tgsi_full_declaration *d)
239{
240 int i = -1;
241
242 switch (d->Declaration.File) {
243 case TGSI_FILE_INPUT:
244 i = shader->ninput++;
245 assert(i < Elements(shader->input));
246 shader->input[i].name = d->Semantic.Name;
247 shader->input[i].sid = d->Semantic.Index;
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900248 shader->input[i].index = d->Range.First;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900249 shader->input[i].interpolate = d->Interp.Interpolate;
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400250 shader->input[i].centroid = d->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900251 return -1;
252
253 case TGSI_FILE_OUTPUT:
254 i = shader->noutput++;
255 assert(i < Elements(shader->output));
256 shader->output[i].name = d->Semantic.Name;
257 shader->output[i].sid = d->Semantic.Index;
258 shader->output[i].index = d->Range.First;
259 shader->output[i].usage = d->Declaration.UsageMask;
260 break;
261 }
262
263 return i;
264}
265
Tom Stellarda75c6162012-01-06 17:38:37 -0500266static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900267 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500268 unsigned input_index,
269 const struct tgsi_full_declaration *decl)
270{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900271 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
272 struct gallivm_state *gallivm = base->gallivm;
273 struct si_shader_context *si_shader_ctx =
274 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100275 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
276
277 unsigned chan;
278
Tom Stellarda75c6162012-01-06 17:38:37 -0500279 LLVMValueRef t_list_ptr;
280 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400281 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500282 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100283 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400284 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500285 LLVMTypeRef vec4_type;
286 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500287
Tom Stellard467f5162012-05-16 15:15:35 -0400288 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100289 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500290
Michel Dänzer51f89a02013-12-09 15:33:53 +0900291 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400292
Christian König206f0592013-03-20 14:37:21 +0100293 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400294
295 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900296 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500297
Christian Königa0dca442013-03-22 15:59:22 +0100298 if (divisor) {
299 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200300 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200301 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100302 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200303 /* Load the buffer index for vertices. */
304 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
305 si_shader_ctx->param_vertex_id);
306 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
307 SI_PARAM_BASE_VERTEX);
308 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100309 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500310
311 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400312 args[0] = t_list;
313 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100314 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900315 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100316 "llvm.SI.vs.load.input", vec4_type, args, 3,
317 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500318
319 /* Break up the vec4 into individual components */
320 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900321 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500322 /* XXX: Use a helper function for this. There is one in
323 * tgsi_llvm.c. */
324 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900325 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500326 input, llvm_chan, "");
327 }
328}
329
Michel Dänzer404b29d2013-11-21 16:45:28 +0900330static void declare_input_gs(
331 struct radeon_llvm_context *radeon_bld,
332 unsigned input_index,
333 const struct tgsi_full_declaration *decl)
334{
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900335 struct si_shader_context *si_shader_ctx =
336 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200337 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900338
339 si_store_shader_io_attribs(shader, decl);
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900340
Marek Olšáke29353f2014-09-17 22:17:02 +0200341 if (decl->Semantic.Name != TGSI_SEMANTIC_PRIMID) {
342 shader->gs_used_inputs |=
343 1llu << get_unique_index(decl->Semantic.Name,
344 decl->Semantic.Index);
345 shader->nparam++;
346 }
Michel Dänzer404b29d2013-11-21 16:45:28 +0900347}
348
349static LLVMValueRef fetch_input_gs(
350 struct lp_build_tgsi_context *bld_base,
351 const struct tgsi_full_src_register *reg,
352 enum tgsi_opcode_type type,
353 unsigned swizzle)
354{
355 struct lp_build_context *base = &bld_base->base;
356 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200357 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900358 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
359 struct gallivm_state *gallivm = base->gallivm;
360 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
361 LLVMValueRef vtx_offset;
362 LLVMValueRef t_list_ptr;
363 LLVMValueRef t_list;
364 LLVMValueRef args[9];
365 unsigned vtx_offset_param;
Marek Olšáke29353f2014-09-17 22:17:02 +0200366 struct si_shader_input *input = &shader->input[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900367
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900368 if (swizzle != ~0 &&
369 shader->input[reg->Register.Index].name == TGSI_SEMANTIC_PRIMID) {
370 if (swizzle == 0)
371 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
372 SI_PARAM_PRIMITIVE_ID);
373 else
374 return uint->zero;
375 }
376
Michel Dänzer404b29d2013-11-21 16:45:28 +0900377 if (!reg->Register.Dimension)
378 return NULL;
379
380 if (swizzle == ~0) {
381 LLVMValueRef values[TGSI_NUM_CHANNELS];
382 unsigned chan;
383 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
384 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
385 }
386 return lp_build_gather_values(bld_base->base.gallivm, values,
387 TGSI_NUM_CHANNELS);
388 }
389
390 /* Get the vertex offset parameter */
391 vtx_offset_param = reg->Dimension.Index;
392 if (vtx_offset_param < 2) {
393 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
394 } else {
395 assert(vtx_offset_param < 6);
396 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
397 }
398 vtx_offset = lp_build_mul_imm(uint,
399 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
400 vtx_offset_param),
401 4);
402
403 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +0900404 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
405 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900406 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900407 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900408
409 args[0] = t_list;
410 args[1] = vtx_offset;
411 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke29353f2014-09-17 22:17:02 +0200412 (get_param_index(input->name, input->sid,
413 shader->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900414 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900415 args[3] = uint->zero;
416 args[4] = uint->one; /* OFFEN */
417 args[5] = uint->zero; /* IDXEN */
418 args[6] = uint->one; /* GLC */
419 args[7] = uint->zero; /* SLC */
420 args[8] = uint->zero; /* TFE */
421
422 return LLVMBuildBitCast(gallivm->builder,
423 build_intrinsic(gallivm->builder,
424 "llvm.SI.buffer.load.dword.i32.i32",
425 i32, args, 9,
426 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
427 tgsi2llvmtype(bld_base, type), "");
428}
429
Tom Stellarda75c6162012-01-06 17:38:37 -0500430static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900431 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500432 unsigned input_index,
433 const struct tgsi_full_declaration *decl)
434{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900435 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
436 struct si_shader_context *si_shader_ctx =
437 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200438 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900439 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
440 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400441 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900442 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100443
444 LLVMValueRef interp_param;
445 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500446
447 /* This value is:
448 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
449 * quad begins a new primitive. Bit 0 always needs
450 * to be unset)
451 * [32:16] ParamOffset
452 *
453 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900454 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200455 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500456
Christian König0666ffd2013-03-05 15:07:39 +0100457 unsigned chan;
458
Tom Stellard0fb1e682012-09-06 16:18:11 -0400459 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
460 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400461 unsigned soa_index =
462 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900463 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100464 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100465
466 if (chan == 3)
467 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900468 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100469 LLVMBuildFDiv(gallivm->builder,
470 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900471 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100472 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400473 }
474 return;
475 }
476
Michel Dänzer97078b12012-09-25 12:41:31 +0200477 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
478 LLVMValueRef face, is_face_positive;
479
Christian König0666ffd2013-03-05 15:07:39 +0100480 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
481
Michel Dänzer97078b12012-09-25 12:41:31 +0200482 is_face_positive = LLVMBuildFCmp(gallivm->builder,
483 LLVMRealUGT, face,
484 lp_build_const_float(gallivm, 0.0f),
485 "");
486
Michel Dänzer51f89a02013-12-09 15:33:53 +0900487 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200488 LLVMBuildSelect(gallivm->builder,
489 is_face_positive,
490 lp_build_const_float(gallivm, 1.0f),
491 lp_build_const_float(gallivm, 0.0f),
492 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900493 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
494 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200495 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900496 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200497 lp_build_const_float(gallivm, 1.0f);
498
499 return;
500 }
501
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900502 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200503 attr_number = lp_build_const_int32(gallivm,
504 shader->input[input_index].param_offset);
505
Francisco Jerez12799232012-04-30 18:27:52 +0200506 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500507 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100508 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500509 break;
510 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák99df1202014-05-06 19:10:52 +0200511 if (si_shader_ctx->shader->key.ps.interp_at_sample)
512 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400513 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100514 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200515 else
Christian König0666ffd2013-03-05 15:07:39 +0100516 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200517 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200518 case TGSI_INTERPOLATE_COLOR:
519 if (si_shader_ctx->shader->key.ps.flatshade) {
520 interp_param = 0;
521 break;
522 }
523 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200524 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák99df1202014-05-06 19:10:52 +0200525 if (si_shader_ctx->shader->key.ps.interp_at_sample)
526 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400527 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100528 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200529 else
Christian König0666ffd2013-03-05 15:07:39 +0100530 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500531 break;
532 default:
533 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
534 return;
535 }
536
Christian König0666ffd2013-03-05 15:07:39 +0100537 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
538
Tom Stellarda75c6162012-01-06 17:38:37 -0500539 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200540 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100541 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100542 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200543 LLVMValueRef face, is_face_positive;
544 LLVMValueRef back_attr_number =
545 lp_build_const_int32(gallivm,
546 shader->input[input_index].param_offset + 1);
547
Christian König0666ffd2013-03-05 15:07:39 +0100548 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
549
Michel Dänzer691f08d2012-09-06 18:03:38 +0200550 is_face_positive = LLVMBuildFCmp(gallivm->builder,
551 LLVMRealUGT, face,
552 lp_build_const_float(gallivm, 0.0f),
553 "");
554
Tom Stellarda75c6162012-01-06 17:38:37 -0500555 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100556 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200557 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
558 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
559 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
560 LLVMValueRef front, back;
561
562 args[0] = llvm_chan;
563 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900564 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100565 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100566 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200567
568 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900569 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100570 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100571 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200572
Michel Dänzer51f89a02013-12-09 15:33:53 +0900573 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200574 LLVMBuildSelect(gallivm->builder,
575 is_face_positive,
576 front,
577 back,
578 "");
579 }
580
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900581 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200582 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
583 LLVMValueRef args[4];
584
585 args[0] = uint->zero;
586 args[1] = attr_number;
587 args[2] = params;
588 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900589 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
590 build_intrinsic(gallivm->builder, intr_name,
591 input_type, args, args[3] ? 4 : 3,
592 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
593 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
594 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200595 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900596 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200597 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200598 } else {
599 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100600 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200601 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
602 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
603 args[0] = llvm_chan;
604 args[1] = attr_number;
605 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100606 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900607 radeon_bld->inputs[soa_index] =
608 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100609 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100610 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200611 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500612 }
613}
614
Marek Olšák5b06fc32014-05-06 18:12:40 +0200615static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
616{
617 struct gallivm_state *gallivm = &radeon_bld->gallivm;
618 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
619 SI_PARAM_ANCILLARY);
620 value = LLVMBuildLShr(gallivm->builder, value,
621 lp_build_const_int32(gallivm, 8), "");
622 value = LLVMBuildAnd(gallivm->builder, value,
623 lp_build_const_int32(gallivm, 0xf), "");
624 return value;
625}
626
Marek Olšák250aa932014-05-06 14:10:47 +0200627static LLVMValueRef load_const(LLVMBuilderRef builder, LLVMValueRef resource,
628 LLVMValueRef offset, LLVMTypeRef return_type)
629{
630 LLVMValueRef args[2] = {resource, offset};
631
632 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
633 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
634}
635
Christian Könige4ed5872013-03-21 18:02:52 +0100636static void declare_system_value(
637 struct radeon_llvm_context * radeon_bld,
638 unsigned index,
639 const struct tgsi_full_declaration *decl)
640{
Marek Olšák8d03d922013-09-01 23:59:06 +0200641 struct si_shader_context *si_shader_ctx =
642 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200643 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
644 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100645 LLVMValueRef value = 0;
646
647 switch (decl->Semantic.Name) {
648 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200649 value = LLVMGetParam(radeon_bld->main_fn,
650 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100651 break;
652
653 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200654 value = LLVMGetParam(radeon_bld->main_fn,
655 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100656 break;
657
Marek Olšák5b06fc32014-05-06 18:12:40 +0200658 case TGSI_SEMANTIC_SAMPLEID:
659 value = get_sample_id(radeon_bld);
660 break;
661
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200662 case TGSI_SEMANTIC_SAMPLEPOS:
663 {
664 LLVMBuilderRef builder = gallivm->builder;
665 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200666 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200667 LLVMValueRef resource = build_indexed_load(si_shader_ctx, desc, buf_index);
668
669 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
670 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
671 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
672
673 LLVMValueRef pos[4] = {
674 load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
675 load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
676 lp_build_const_float(gallivm, 0),
677 lp_build_const_float(gallivm, 0)
678 };
679 value = lp_build_gather_values(gallivm, pos, 4);
680 break;
681 }
682
Christian Könige4ed5872013-03-21 18:02:52 +0100683 default:
684 assert(!"unknown system value");
685 return;
686 }
687
688 radeon_bld->system_values[index] = value;
689}
690
Tom Stellarda75c6162012-01-06 17:38:37 -0500691static LLVMValueRef fetch_constant(
692 struct lp_build_tgsi_context * bld_base,
693 const struct tgsi_full_src_register *reg,
694 enum tgsi_opcode_type type,
695 unsigned swizzle)
696{
Christian König55fe5cc2013-03-04 16:30:06 +0100697 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500698 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100699 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200700 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500701
Christian König0f6cf2b2013-03-15 15:53:25 +0100702 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100703 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500704
Christian König8514f5a2013-02-04 17:46:42 +0100705 if (swizzle == LP_CHAN_ALL) {
706 unsigned chan;
707 LLVMValueRef values[4];
708 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
709 values[chan] = fetch_constant(bld_base, reg, type, chan);
710
711 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
712 }
713
Marek Olšák2fd42002013-10-25 11:45:47 +0200714 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100715 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100716
Marek Olšák2fd42002013-10-25 11:45:47 +0200717 if (!reg->Register.Indirect)
718 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
719
Christian König0f6cf2b2013-03-15 15:53:25 +0100720 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
721 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
722 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200723 addr = lp_build_add(&bld_base->uint_bld, addr,
724 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200725
Marek Olšák250aa932014-05-06 14:10:47 +0200726 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
727 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500728
Christian Königf5298b02013-02-28 14:50:07 +0100729 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500730}
731
Michel Dänzer26c71392012-08-24 12:03:11 +0200732/* Initialize arguments for the shader export intrinsic */
733static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900734 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200735 unsigned target,
736 LLVMValueRef *args)
737{
738 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
739 struct lp_build_context *uint =
740 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
741 struct lp_build_context *base = &bld_base->base;
742 unsigned compressed = 0;
743 unsigned chan;
744
Michel Dänzerf402acd2012-08-22 18:15:36 +0200745 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
746 int cbuf = target - V_008DFC_SQ_EXP_MRT;
747
748 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100749 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100750
751 if (compressed)
752 si_shader_ctx->shader->spi_shader_col_format |=
753 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
754 else
755 si_shader_ctx->shader->spi_shader_col_format |=
756 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200757
758 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200759 }
760 }
761
762 if (compressed) {
763 /* Pixel shader needs to pack output values before export */
764 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900765 args[0] = values[2 * chan];
766 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200767 args[chan + 5] =
768 build_intrinsic(base->gallivm->builder,
769 "llvm.SI.packf16",
770 LLVMInt32TypeInContext(base->gallivm->context),
771 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100772 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100773 args[chan + 7] = args[chan + 5] =
774 LLVMBuildBitCast(base->gallivm->builder,
775 args[chan + 5],
776 LLVMFloatTypeInContext(base->gallivm->context),
777 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200778 }
779
780 /* Set COMPR flag */
781 args[4] = uint->one;
782 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900783 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200784 /* +5 because the first output value will be
785 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900786 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200787
788 /* Clear COMPR flag */
789 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200790 }
791
792 /* XXX: This controls which components of the output
793 * registers actually get exported. (e.g bit 0 means export
794 * X component, bit 1 means export Y component, etc.) I'm
795 * hard coding this to 0xf for now. In the future, we might
796 * want to do something else. */
797 args[0] = lp_build_const_int32(base->gallivm, 0xf);
798
799 /* Specify whether the EXEC mask represents the valid mask */
800 args[1] = uint->zero;
801
802 /* Specify whether this is the last export */
803 args[2] = uint->zero;
804
805 /* Specify the target we are exporting */
806 args[3] = lp_build_const_int32(base->gallivm, target);
807
Michel Dänzer26c71392012-08-24 12:03:11 +0200808 /* XXX: We probably need to keep track of the output
809 * values, so we know what we are passing to the next
810 * stage. */
811}
812
Michel Dänzer404b29d2013-11-21 16:45:28 +0900813/* Load from output pointers and initialize arguments for the shader export intrinsic */
814static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
815 LLVMValueRef *out_ptr,
816 unsigned target,
817 LLVMValueRef *args)
818{
819 struct gallivm_state *gallivm = bld_base->base.gallivm;
820 LLVMValueRef values[4];
821 int i;
822
823 for (i = 0; i < 4; i++)
824 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
825
826 si_llvm_init_export_args(bld_base, values, target, args);
827}
828
Michel Dänzer7708a862012-11-02 15:57:30 +0100829static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900830 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100831{
832 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
833 struct gallivm_state *gallivm = bld_base->base.gallivm;
834
Christian Königa0dca442013-03-22 15:59:22 +0100835 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400836 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
837 SI_PARAM_ALPHA_REF);
838
Michel Dänzer7708a862012-11-02 15:57:30 +0100839 LLVMValueRef alpha_pass =
840 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100841 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900842 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400843 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100844 LLVMValueRef arg =
845 lp_build_select(&bld_base->base,
846 alpha_pass,
847 lp_build_const_float(gallivm, 1.0f),
848 lp_build_const_float(gallivm, -1.0f));
849
850 build_intrinsic(gallivm->builder,
851 "llvm.AMDGPU.kill",
852 LLVMVoidTypeInContext(gallivm->context),
853 &arg, 1, 0);
854 } else {
855 build_intrinsic(gallivm->builder,
856 "llvm.AMDGPU.kilp",
857 LLVMVoidTypeInContext(gallivm->context),
858 NULL, 0, 0);
859 }
Marek Olšákadc57972014-09-19 17:07:07 +0200860
861 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100862}
863
Michel Dänzere3befbc2013-05-15 18:09:50 +0200864static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900865 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200866{
867 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200868 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200869 struct lp_build_context *base = &bld_base->base;
870 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200871 unsigned reg_index;
872 unsigned chan;
873 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200874 LLVMValueRef base_elt;
875 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200876 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák2fd42002013-10-25 11:45:47 +0200877 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200878
Michel Dänzere3befbc2013-05-15 18:09:50 +0200879 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200880 LLVMValueRef *args = pos[2 + reg_index];
881
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200882 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
883 continue;
884
Marek Olšák8c37c162014-09-16 18:40:07 +0200885 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200886
Michel Dänzere3befbc2013-05-15 18:09:50 +0200887 args[5] =
888 args[6] =
889 args[7] =
890 args[8] = lp_build_const_float(base->gallivm, 0.0f);
891
892 /* Compute dot products of position and user clip plane vectors */
893 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
894 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200895 args[1] = lp_build_const_int32(base->gallivm,
896 ((reg_index * 4 + chan) * 4 +
897 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200898 base_elt = load_const(base->gallivm->builder, const_resource,
899 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200900 args[5 + chan] =
901 lp_build_add(base, args[5 + chan],
902 lp_build_mul(base, base_elt,
903 out_elts[const_chan]));
904 }
905 }
906
907 args[0] = lp_build_const_int32(base->gallivm, 0xf);
908 args[1] = uint->zero;
909 args[2] = uint->zero;
910 args[3] = lp_build_const_int32(base->gallivm,
911 V_008DFC_SQ_EXP_POS + 2 + reg_index);
912 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200913 }
914}
915
Marek Olšák8d03d922013-09-01 23:59:06 +0200916static void si_dump_streamout(struct pipe_stream_output_info *so)
917{
918 unsigned i;
919
920 if (so->num_outputs)
921 fprintf(stderr, "STREAMOUT\n");
922
923 for (i = 0; i < so->num_outputs; i++) {
924 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
925 so->output[i].start_component;
926 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
927 i, so->output[i].output_buffer,
928 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
929 so->output[i].register_index,
930 mask & 1 ? "x" : "",
931 mask & 2 ? "y" : "",
932 mask & 4 ? "z" : "",
933 mask & 8 ? "w" : "");
934 }
935}
936
937/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
938 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
939 * or v4i32 (num_channels=3,4). */
940static void build_tbuffer_store(struct si_shader_context *shader,
941 LLVMValueRef rsrc,
942 LLVMValueRef vdata,
943 unsigned num_channels,
944 LLVMValueRef vaddr,
945 LLVMValueRef soffset,
946 unsigned inst_offset,
947 unsigned dfmt,
948 unsigned nfmt,
949 unsigned offen,
950 unsigned idxen,
951 unsigned glc,
952 unsigned slc,
953 unsigned tfe)
954{
955 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
956 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
957 LLVMValueRef args[] = {
958 rsrc,
959 vdata,
960 LLVMConstInt(i32, num_channels, 0),
961 vaddr,
962 soffset,
963 LLVMConstInt(i32, inst_offset, 0),
964 LLVMConstInt(i32, dfmt, 0),
965 LLVMConstInt(i32, nfmt, 0),
966 LLVMConstInt(i32, offen, 0),
967 LLVMConstInt(i32, idxen, 0),
968 LLVMConstInt(i32, glc, 0),
969 LLVMConstInt(i32, slc, 0),
970 LLVMConstInt(i32, tfe, 0)
971 };
972
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900973 /* The instruction offset field has 12 bits */
974 assert(offen || inst_offset < (1 << 12));
975
Marek Olšák8d03d922013-09-01 23:59:06 +0200976 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
977 unsigned func = CLAMP(num_channels, 1, 3) - 1;
978 const char *types[] = {"i32", "v2i32", "v4i32"};
979 char name[256];
980 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
981
982 lp_build_intrinsic(gallivm->builder, name,
983 LLVMVoidTypeInContext(gallivm->context),
984 args, Elements(args));
985}
986
987static void build_streamout_store(struct si_shader_context *shader,
988 LLVMValueRef rsrc,
989 LLVMValueRef vdata,
990 unsigned num_channels,
991 LLVMValueRef vaddr,
992 LLVMValueRef soffset,
993 unsigned inst_offset)
994{
995 static unsigned dfmt[] = {
996 V_008F0C_BUF_DATA_FORMAT_32,
997 V_008F0C_BUF_DATA_FORMAT_32_32,
998 V_008F0C_BUF_DATA_FORMAT_32_32_32,
999 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1000 };
1001 assert(num_channels >= 1 && num_channels <= 4);
1002
1003 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
1004 inst_offset, dfmt[num_channels-1],
1005 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
1006}
1007
1008/* On SI, the vertex shader is responsible for writing streamout data
1009 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +09001010static void si_llvm_emit_streamout(struct si_shader_context *shader,
1011 struct si_shader_output_values *outputs,
1012 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +02001013{
1014 struct pipe_stream_output_info *so = &shader->shader->selector->so;
1015 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
1016 LLVMBuilderRef builder = gallivm->builder;
1017 int i, j;
1018 struct lp_build_if_state if_ctx;
1019
1020 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1021
1022 LLVMValueRef so_param =
1023 LLVMGetParam(shader->radeon_bld.main_fn,
1024 shader->param_streamout_config);
1025
1026 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
1027 LLVMValueRef so_vtx_count =
1028 LLVMBuildAnd(builder,
1029 LLVMBuildLShr(builder, so_param,
1030 LLVMConstInt(i32, 16, 0), ""),
1031 LLVMConstInt(i32, 127, 0), "");
1032
1033 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
1034 NULL, 0, LLVMReadNoneAttribute);
1035
1036 /* can_emit = tid < so_vtx_count; */
1037 LLVMValueRef can_emit =
1038 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
1039
1040 /* Emit the streamout code conditionally. This actually avoids
1041 * out-of-bounds buffer access. The hw tells us via the SGPR
1042 * (so_vtx_count) which threads are allowed to emit streamout data. */
1043 lp_build_if(&if_ctx, gallivm, can_emit);
1044 {
1045 /* The buffer offset is computed as follows:
1046 * ByteOffset = streamout_offset[buffer_id]*4 +
1047 * (streamout_write_index + thread_id)*stride[buffer_id] +
1048 * attrib_offset
1049 */
1050
1051 LLVMValueRef so_write_index =
1052 LLVMGetParam(shader->radeon_bld.main_fn,
1053 shader->param_streamout_write_index);
1054
1055 /* Compute (streamout_write_index + thread_id). */
1056 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1057
1058 /* Compute the write offset for each enabled buffer. */
1059 LLVMValueRef so_write_offset[4] = {};
1060 for (i = 0; i < 4; i++) {
1061 if (!so->stride[i])
1062 continue;
1063
1064 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1065 shader->param_streamout_offset[i]);
1066 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1067
1068 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1069 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1070 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1071 }
1072
Marek Olšák8d03d922013-09-01 23:59:06 +02001073 /* Write streamout data. */
1074 for (i = 0; i < so->num_outputs; i++) {
1075 unsigned buf_idx = so->output[i].output_buffer;
1076 unsigned reg = so->output[i].register_index;
1077 unsigned start = so->output[i].start_component;
1078 unsigned num_comps = so->output[i].num_components;
1079 LLVMValueRef out[4];
1080
1081 assert(num_comps && num_comps <= 4);
1082 if (!num_comps || num_comps > 4)
1083 continue;
1084
1085 /* Load the output as int. */
1086 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001087 unsigned outidx = 0;
1088
1089 while (outidx < noutput && outputs[outidx].index != reg)
1090 outidx++;
1091
1092 if (outidx < noutput)
1093 out[j] = LLVMBuildBitCast(builder,
1094 outputs[outidx].values[start+j],
1095 i32, "");
1096 else
1097 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +02001098 }
1099
Michel Dänzer67e385b2014-01-08 17:48:21 +09001100 if (!out[0])
1101 continue;
1102
Marek Olšák8d03d922013-09-01 23:59:06 +02001103 /* Pack the output. */
1104 LLVMValueRef vdata = NULL;
1105
1106 switch (num_comps) {
1107 case 1: /* as i32 */
1108 vdata = out[0];
1109 break;
1110 case 2: /* as v2i32 */
1111 case 3: /* as v4i32 (aligned to 4) */
1112 case 4: /* as v4i32 */
1113 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1114 for (j = 0; j < num_comps; j++) {
1115 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1116 LLVMConstInt(i32, j, 0), "");
1117 }
1118 break;
1119 }
1120
1121 build_streamout_store(shader, shader->so_buffers[buf_idx],
1122 vdata, num_comps,
1123 so_write_offset[buf_idx],
1124 LLVMConstInt(i32, 0, 0),
1125 so->output[i].dst_offset*4);
1126 }
1127 }
1128 lp_build_endif(&if_ctx);
1129}
1130
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001131
Michel Dänzer404b29d2013-11-21 16:45:28 +09001132/* Generate export instructions for hardware VS shader stage */
1133static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1134 struct si_shader_output_values *outputs,
1135 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001136{
1137 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001138 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001139 struct lp_build_context * base = &bld_base->base;
1140 struct lp_build_context * uint =
1141 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001142 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001143 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001144 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
1145 unsigned semantic_name, semantic_index, semantic_usage;
1146 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001147 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001148 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001149 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001150
Michel Dänzer67e385b2014-01-08 17:48:21 +09001151 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1152 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001153 }
1154
Michel Dänzer404b29d2013-11-21 16:45:28 +09001155 for (i = 0; i < noutput; i++) {
1156 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001157 semantic_index = outputs[i].sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001158 semantic_usage = outputs[i].usage;
Tom Stellarda75c6162012-01-06 17:38:37 -05001159
Michel Dänzer0afeea52013-05-02 14:53:17 +02001160handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001161 /* Select the correct target */
1162 switch(semantic_name) {
1163 case TGSI_SEMANTIC_PSIZE:
1164 shader->vs_out_misc_write = true;
1165 shader->vs_out_point_size = true;
1166 psize_value = outputs[i].values[0];
1167 continue;
1168 case TGSI_SEMANTIC_EDGEFLAG:
1169 shader->vs_out_misc_write = true;
1170 shader->vs_out_edgeflag = true;
1171 edgeflag_value = outputs[i].values[0];
1172 continue;
1173 case TGSI_SEMANTIC_LAYER:
1174 shader->vs_out_misc_write = true;
1175 shader->vs_out_layer = true;
1176 layer_value = outputs[i].values[0];
1177 continue;
1178 case TGSI_SEMANTIC_POSITION:
1179 target = V_008DFC_SQ_EXP_POS;
1180 break;
1181 case TGSI_SEMANTIC_COLOR:
1182 case TGSI_SEMANTIC_BCOLOR:
1183 target = V_008DFC_SQ_EXP_PARAM + param_count;
1184 shader->output[i].param_offset = param_count;
1185 param_count++;
1186 break;
1187 case TGSI_SEMANTIC_CLIPDIST:
1188 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
1189 (1 << semantic_index)))
Marek Olšák053606d2013-11-19 22:07:30 +01001190 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001191 shader->clip_dist_write |=
1192 semantic_usage << (semantic_index << 2);
1193 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1194 break;
1195 case TGSI_SEMANTIC_CLIPVERTEX:
1196 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1197 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001198 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001199 case TGSI_SEMANTIC_FOG:
1200 case TGSI_SEMANTIC_GENERIC:
1201 target = V_008DFC_SQ_EXP_PARAM + param_count;
1202 shader->output[i].param_offset = param_count;
1203 param_count++;
1204 break;
1205 default:
1206 target = 0;
1207 fprintf(stderr,
1208 "Warning: SI unhandled vs output type:%d\n",
1209 semantic_name);
1210 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001211
Michel Dänzer404b29d2013-11-21 16:45:28 +09001212 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001213
Michel Dänzer404b29d2013-11-21 16:45:28 +09001214 if (target >= V_008DFC_SQ_EXP_POS &&
1215 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1216 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1217 args, sizeof(args));
1218 } else {
1219 lp_build_intrinsic(base->gallivm->builder,
1220 "llvm.SI.export",
1221 LLVMVoidTypeInContext(base->gallivm->context),
1222 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001223 }
1224
1225 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1226 semantic_name = TGSI_SEMANTIC_GENERIC;
1227 goto handle_semantic;
1228 }
1229 }
1230
1231 /* We need to add the position output manually if it's missing. */
1232 if (!pos_args[0][0]) {
1233 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1234 pos_args[0][1] = uint->zero; /* EXEC mask */
1235 pos_args[0][2] = uint->zero; /* last export? */
1236 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1237 pos_args[0][4] = uint->zero; /* COMPR flag */
1238 pos_args[0][5] = base->zero; /* X */
1239 pos_args[0][6] = base->zero; /* Y */
1240 pos_args[0][7] = base->zero; /* Z */
1241 pos_args[0][8] = base->one; /* W */
1242 }
1243
1244 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1245 if (shader->vs_out_misc_write) {
1246 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1247 shader->vs_out_point_size |
1248 (shader->vs_out_edgeflag << 1) |
1249 (shader->vs_out_layer << 2));
1250 pos_args[1][1] = uint->zero; /* EXEC mask */
1251 pos_args[1][2] = uint->zero; /* last export? */
1252 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1253 pos_args[1][4] = uint->zero; /* COMPR flag */
1254 pos_args[1][5] = base->zero; /* X */
1255 pos_args[1][6] = base->zero; /* Y */
1256 pos_args[1][7] = base->zero; /* Z */
1257 pos_args[1][8] = base->zero; /* W */
1258
Michel Dänzer404b29d2013-11-21 16:45:28 +09001259 if (shader->vs_out_point_size)
1260 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001261
1262 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001263 /* The output is a float, but the hw expects an integer
1264 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001265 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1266 edgeflag_value,
1267 bld_base->uint_bld.elem_type, "");
1268 edgeflag_value = lp_build_min(&bld_base->int_bld,
1269 edgeflag_value,
1270 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001271
1272 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001273 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1274 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001275 base->elem_type, "");
1276 }
1277
Michel Dänzer404b29d2013-11-21 16:45:28 +09001278 if (shader->vs_out_layer)
1279 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001280 }
1281
1282 for (i = 0; i < 4; i++)
1283 if (pos_args[i][0])
1284 shader->nr_pos_exports++;
1285
1286 pos_idx = 0;
1287 for (i = 0; i < 4; i++) {
1288 if (!pos_args[i][0])
1289 continue;
1290
1291 /* Specify the target we are exporting */
1292 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1293
1294 if (pos_idx == shader->nr_pos_exports)
1295 /* Specify that this is the last export */
1296 pos_args[i][2] = uint->one;
1297
1298 lp_build_intrinsic(base->gallivm->builder,
1299 "llvm.SI.export",
1300 LLVMVoidTypeInContext(base->gallivm->context),
1301 pos_args[i], 9);
1302 }
1303}
1304
Michel Dänzer404b29d2013-11-21 16:45:28 +09001305static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1306{
1307 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1308 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001309 struct si_shader *es = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001310 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1311 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001312 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1313 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001314 LLVMValueRef t_list_ptr;
1315 LLVMValueRef t_list;
1316 unsigned chan;
1317 int i;
1318
1319 while (!tgsi_parse_end_of_tokens(parse)) {
1320 struct tgsi_full_declaration *d =
1321 &parse->FullToken.FullDeclaration;
1322
1323 tgsi_parse_token(parse);
1324
1325 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1326 continue;
1327
Michel Dänzere884c562014-01-15 15:24:14 +09001328 si_store_shader_io_attribs(es, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001329 }
1330
1331 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001332 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1333 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001334 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001335 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001336
Michel Dänzere884c562014-01-15 15:24:14 +09001337 for (i = 0; i < es->noutput; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001338 LLVMValueRef *out_ptr =
Michel Dänzere884c562014-01-15 15:24:14 +09001339 si_shader_ctx->radeon_bld.soa.outputs[es->output[i].index];
Marek Olšáke29353f2014-09-17 22:17:02 +02001340 int param_index = get_param_index(es->output[i].name,
1341 es->output[i].sid,
1342 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001343
Marek Olšáke29353f2014-09-17 22:17:02 +02001344 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001345 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001346
1347 for (chan = 0; chan < 4; chan++) {
1348 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001349 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1350
1351 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001352 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001353 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001354 V_008F0C_BUF_DATA_FORMAT_32,
1355 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001356 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001357 }
1358 }
1359}
1360
1361static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1362{
1363 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1364 struct gallivm_state *gallivm = bld_base->base.gallivm;
1365 LLVMValueRef args[2];
1366
1367 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1368 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1369 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1370 LLVMVoidTypeInContext(gallivm->context), args, 2,
1371 LLVMNoUnwindAttribute);
1372}
1373
1374static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1375{
1376 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1377 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001378 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001379 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1380 struct si_shader_output_values *outputs = NULL;
1381 unsigned noutput = 0;
1382 int i;
1383
1384 while (!tgsi_parse_end_of_tokens(parse)) {
1385 struct tgsi_full_declaration *d =
1386 &parse->FullToken.FullDeclaration;
1387 unsigned index;
1388
1389 tgsi_parse_token(parse);
1390
1391 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1392 continue;
1393
Marek Olšák8c37c162014-09-16 18:40:07 +02001394 i = si_store_shader_io_attribs(shader, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001395 if (i < 0)
1396 continue;
1397
1398 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1399 (noutput + 1) * sizeof(outputs[0]));
1400 for (index = d->Range.First; index <= d->Range.Last; index++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001401 outputs[noutput].index = index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001402 outputs[noutput].name = d->Semantic.Name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001403 outputs[noutput].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001404 outputs[noutput].usage = d->Declaration.UsageMask;
1405
1406 for (i = 0; i < 4; i++)
1407 outputs[noutput].values[i] =
1408 LLVMBuildLoad(gallivm->builder,
1409 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1410 "");
1411 }
1412 noutput++;
1413 }
1414
1415 si_llvm_export_vs(bld_base, outputs, noutput);
1416 FREE(outputs);
1417}
1418
Michel Dänzer51f89a02013-12-09 15:33:53 +09001419static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1420{
1421 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001422 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001423 struct lp_build_context * base = &bld_base->base;
1424 struct lp_build_context * uint =
1425 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1426 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1427 LLVMValueRef args[9];
1428 LLVMValueRef last_args[9] = { 0 };
1429 unsigned semantic_name;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001430 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001431 int i;
1432
1433 while (!tgsi_parse_end_of_tokens(parse)) {
1434 struct tgsi_full_declaration *d =
1435 &parse->FullToken.FullDeclaration;
1436 unsigned target;
1437 unsigned index;
1438
1439 tgsi_parse_token(parse);
1440
1441 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
1442 parse->FullToken.FullProperty.Property.PropertyName ==
1443 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
1444 shader->fs_write_all = TRUE;
1445
1446 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1447 continue;
1448
1449 i = si_store_shader_io_attribs(shader, d);
1450 if (i < 0)
1451 continue;
1452
1453 semantic_name = d->Semantic.Name;
1454 for (index = d->Range.First; index <= d->Range.Last; index++) {
1455 /* Select the correct target */
1456 switch(semantic_name) {
1457 case TGSI_SEMANTIC_POSITION:
1458 depth_index = index;
1459 continue;
1460 case TGSI_SEMANTIC_STENCIL:
1461 stencil_index = index;
1462 continue;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001463 case TGSI_SEMANTIC_SAMPLEMASK:
1464 samplemask_index = index;
1465 continue;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001466 case TGSI_SEMANTIC_COLOR:
1467 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1468 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001469 LLVMBuildStore(bld_base->base.gallivm->builder,
1470 bld_base->base.one,
1471 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1472
Michel Dänzer51f89a02013-12-09 15:33:53 +09001473 if (d->Semantic.Index == 0 &&
1474 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001475 si_alpha_test(bld_base,
1476 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001477 break;
1478 default:
1479 target = 0;
1480 fprintf(stderr,
1481 "Warning: SI unhandled fs output type:%d\n",
1482 semantic_name);
1483 }
1484
Michel Dänzer404b29d2013-11-21 16:45:28 +09001485 si_llvm_init_export_args_load(bld_base,
1486 si_shader_ctx->radeon_bld.soa.outputs[index],
1487 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001488
1489 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001490 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001491 if (last_args[0]) {
1492 lp_build_intrinsic(base->gallivm->builder,
1493 "llvm.SI.export",
1494 LLVMVoidTypeInContext(base->gallivm->context),
1495 last_args, 9);
1496 }
1497
Marek Olšák0eb528a2013-12-04 13:24:22 +01001498 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001499 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001500
1501 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1502 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1503 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1504 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001505 si_llvm_init_export_args_load(bld_base,
1506 si_shader_ctx->radeon_bld.soa.outputs[index],
1507 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001508 lp_build_intrinsic(base->gallivm->builder,
1509 "llvm.SI.export",
1510 LLVMVoidTypeInContext(base->gallivm->context),
1511 args, 9);
1512 }
1513 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001514 } else {
1515 lp_build_intrinsic(base->gallivm->builder,
1516 "llvm.SI.export",
1517 LLVMVoidTypeInContext(base->gallivm->context),
1518 args, 9);
1519 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001520 }
1521 }
1522
Marek Olšákd0e8b652014-05-06 20:04:31 +02001523 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001524 LLVMValueRef out_ptr;
1525 unsigned mask = 0;
1526
1527 /* Specify the target we are exporting */
1528 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1529
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001530 args[5] = base->zero; /* R, depth */
1531 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1532 args[7] = base->zero; /* B, sample mask */
1533 args[8] = base->zero; /* A, alpha to mask */
1534
Michel Dänzer1a616c12012-11-13 17:35:09 +01001535 if (depth_index >= 0) {
1536 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1537 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1538 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001539 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001540 }
1541
1542 if (stencil_index >= 0) {
1543 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001544 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001545 /* Only setting the stencil component bit (0x2) here
1546 * breaks some stencil piglit tests
1547 */
1548 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001549 si_shader_ctx->shader->db_shader_control |=
1550 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001551 }
1552
Marek Olšákd0e8b652014-05-06 20:04:31 +02001553 if (samplemask_index >= 0) {
1554 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1555 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1556 mask |= 0xf; /* Set all components. */
1557 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1558 }
1559
1560 if (samplemask_index >= 0)
1561 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1562 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001563 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1564 else
1565 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1566
Michel Dänzer1a616c12012-11-13 17:35:09 +01001567 /* Specify which components to enable */
1568 args[0] = lp_build_const_int32(base->gallivm, mask);
1569
1570 args[1] =
1571 args[2] =
1572 args[4] = uint->zero;
1573
1574 if (last_args[0])
1575 lp_build_intrinsic(base->gallivm->builder,
1576 "llvm.SI.export",
1577 LLVMVoidTypeInContext(base->gallivm->context),
1578 args, 9);
1579 else
1580 memcpy(last_args, args, sizeof(args));
1581 }
1582
Michel Dänzer51f89a02013-12-09 15:33:53 +09001583 if (!last_args[0]) {
1584 /* Specify which components to enable */
1585 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001586
Michel Dänzer51f89a02013-12-09 15:33:53 +09001587 /* Specify the target we are exporting */
1588 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001589
Michel Dänzer51f89a02013-12-09 15:33:53 +09001590 /* Set COMPR flag to zero to export data as 32-bit */
1591 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001592
Michel Dänzer51f89a02013-12-09 15:33:53 +09001593 /* dummy bits */
1594 last_args[5]= uint->zero;
1595 last_args[6]= uint->zero;
1596 last_args[7]= uint->zero;
1597 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001598 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001599
1600 /* Specify whether the EXEC mask represents the valid mask */
1601 last_args[1] = uint->one;
1602
1603 /* Specify that this is the last export */
1604 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1605
1606 lp_build_intrinsic(base->gallivm->builder,
1607 "llvm.SI.export",
1608 LLVMVoidTypeInContext(base->gallivm->context),
1609 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001610}
1611
Marek Olšák4855acd2013-08-06 15:08:54 +02001612static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1613 struct lp_build_tgsi_context * bld_base,
1614 struct lp_build_emit_data * emit_data);
1615
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001616static bool tgsi_is_shadow_sampler(unsigned target)
1617{
1618 return target == TGSI_TEXTURE_SHADOW1D ||
1619 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1620 target == TGSI_TEXTURE_SHADOW2D ||
1621 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1622 target == TGSI_TEXTURE_SHADOWCUBE ||
1623 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1624 target == TGSI_TEXTURE_SHADOWRECT;
1625}
1626
Marek Olšák877bb522014-06-25 03:12:46 +02001627static const struct lp_build_tgsi_action tex_action;
1628
Tom Stellarda75c6162012-01-06 17:38:37 -05001629static void tex_fetch_args(
1630 struct lp_build_tgsi_context * bld_base,
1631 struct lp_build_emit_data * emit_data)
1632{
Christian König55fe5cc2013-03-04 16:30:06 +01001633 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001634 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001635 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001636 unsigned opcode = inst->Instruction.Opcode;
1637 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001638 LLVMValueRef coords[4];
1639 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001640 int ref_pos;
1641 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001642 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001643 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001644 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1645 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001646 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001647
1648 if (target == TGSI_TEXTURE_BUFFER) {
1649 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1650 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1651 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1652 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1653
Marek Olšák4f3f0432014-07-07 22:49:15 +02001654 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001655 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1656 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1657 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1658 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1659
1660 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1661 emit_data->args[0] = res;
1662 emit_data->args[1] = bld_base->uint_bld.zero;
1663 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1664 emit_data->arg_count = 3;
1665 return;
1666 }
Tom Stellard467f5162012-05-16 15:15:35 -04001667
Michel Dänzer120efee2013-01-25 12:10:11 +01001668 /* Fetch and project texture coordinates */
1669 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001670 for (chan = 0; chan < 3; chan++ ) {
1671 coords[chan] = lp_build_emit_fetch(bld_base,
1672 emit_data->inst, 0,
1673 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001674 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001675 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1676 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001677 coords[chan],
1678 coords[3]);
1679 }
1680
Michel Dänzer120efee2013-01-25 12:10:11 +01001681 if (opcode == TGSI_OPCODE_TXP)
1682 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001683
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001684 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001685 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001686 /* The offsets are six-bit signed integers packed like this:
1687 * X=[5:0], Y=[13:8], and Z=[21:16].
1688 */
1689 LLVMValueRef offset[3], pack;
1690
1691 assert(inst->Texture.NumOffsets == 1);
1692
1693 for (chan = 0; chan < 3; chan++) {
1694 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1695 emit_data->inst, 0, chan);
1696 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1697 lp_build_const_int32(gallivm, 0x3f), "");
1698 if (chan)
1699 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1700 lp_build_const_int32(gallivm, chan*8), "");
1701 }
1702
1703 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1704 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1705 address[count++] = pack;
1706 }
1707
Michel Dänzer120efee2013-01-25 12:10:11 +01001708 /* Pack LOD bias value */
1709 if (opcode == TGSI_OPCODE_TXB)
1710 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001711 if (opcode == TGSI_OPCODE_TXB2)
1712 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001713
Michel Dänzer120efee2013-01-25 12:10:11 +01001714 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001715 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001716 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1717 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1718 } else {
1719 assert(ref_pos >= 0);
1720 address[count++] = coords[ref_pos];
1721 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001722 }
1723
Marek Olšákb279f0142014-07-04 03:19:58 +02001724 if (target == TGSI_TEXTURE_CUBE ||
1725 target == TGSI_TEXTURE_CUBE_ARRAY ||
1726 target == TGSI_TEXTURE_SHADOWCUBE ||
1727 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1728 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1729
Michel Dänzera6b83c02013-02-21 16:10:55 +01001730 /* Pack user derivatives */
1731 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001732 int num_deriv_channels, param;
1733
1734 switch (target) {
1735 case TGSI_TEXTURE_3D:
1736 num_deriv_channels = 3;
1737 break;
1738 case TGSI_TEXTURE_2D:
1739 case TGSI_TEXTURE_SHADOW2D:
1740 case TGSI_TEXTURE_RECT:
1741 case TGSI_TEXTURE_SHADOWRECT:
1742 case TGSI_TEXTURE_2D_ARRAY:
1743 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1744 case TGSI_TEXTURE_CUBE:
1745 case TGSI_TEXTURE_SHADOWCUBE:
1746 case TGSI_TEXTURE_CUBE_ARRAY:
1747 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1748 num_deriv_channels = 2;
1749 break;
1750 case TGSI_TEXTURE_1D:
1751 case TGSI_TEXTURE_SHADOW1D:
1752 case TGSI_TEXTURE_1D_ARRAY:
1753 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1754 num_deriv_channels = 1;
1755 break;
1756 default:
1757 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001758 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001759
1760 for (param = 1; param <= 2; param++)
1761 for (chan = 0; chan < num_deriv_channels; chan++)
1762 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001763 }
1764
Michel Dänzer120efee2013-01-25 12:10:11 +01001765 /* Pack texture coordinates */
1766 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001767 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001768 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001769 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001770 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001771
Marek Olšákd2bd6342013-09-18 15:40:21 +02001772 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001773 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001774 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001775 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001776 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001777
1778 if (count > 16) {
1779 assert(!"Cannot handle more than 16 texture address parameters");
1780 count = 16;
1781 }
1782
1783 for (chan = 0; chan < count; chan++ ) {
1784 address[chan] = LLVMBuildBitCast(gallivm->builder,
1785 address[chan],
1786 LLVMInt32TypeInContext(gallivm->context),
1787 "");
1788 }
1789
Marek Olšák4855acd2013-08-06 15:08:54 +02001790 /* Adjust the sample index according to FMASK.
1791 *
1792 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1793 * which is the identity mapping. Each nibble says which physical sample
1794 * should be fetched to get that sample.
1795 *
1796 * For example, 0x11111100 means there are only 2 samples stored and
1797 * the second sample covers 3/4 of the pixel. When reading samples 0
1798 * and 1, return physical sample 0 (determined by the first two 0s
1799 * in FMASK), otherwise return physical sample 1.
1800 *
1801 * The sample index should be adjusted as follows:
1802 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1803 */
1804 if (target == TGSI_TEXTURE_2D_MSAA ||
1805 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1806 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1807 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001808 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001809 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001810 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001811
Marek Olšákd2bd6342013-09-18 15:40:21 +02001812 memcpy(txf_address, address, sizeof(txf_address));
1813
1814 if (target == TGSI_TEXTURE_2D_MSAA) {
1815 txf_address[2] = bld_base->uint_bld.zero;
1816 }
1817 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001818
1819 /* Pad to a power-of-two size. */
1820 while (txf_count < util_next_power_of_two(txf_count))
1821 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1822
1823 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001824 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1825 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1826 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001827 txf_emit_data.chan = 0;
1828 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001829 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001830 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001831 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001832 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001833 txf_emit_data.arg_count = 3;
1834
Marek Olšák877bb522014-06-25 03:12:46 +02001835 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001836
1837 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001838 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1839 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1840
1841 /* Apply the formula. */
1842 LLVMValueRef fmask =
1843 LLVMBuildExtractElement(gallivm->builder,
1844 txf_emit_data.output[0],
1845 uint_bld->zero, "");
1846
Marek Olšákd2bd6342013-09-18 15:40:21 +02001847 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001848
1849 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001850 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001851
1852 LLVMValueRef shifted_fmask =
1853 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1854
1855 LLVMValueRef final_sample =
1856 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1857
1858 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1859 * resource descriptor is 0 (invalid),
1860 */
1861 LLVMValueRef fmask_desc =
1862 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001863 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001864 LLVMVectorType(uint_bld->elem_type, 8), "");
1865
1866 LLVMValueRef fmask_word1 =
1867 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1868 uint_bld->one, "");
1869
1870 LLVMValueRef word1_is_nonzero =
1871 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1872 fmask_word1, uint_bld->zero, "");
1873
Marek Olšákd2bd6342013-09-18 15:40:21 +02001874 /* Replace the MSAA sample index. */
1875 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001876 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001877 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001878 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001879
Michel Dänzer36231112013-05-02 09:44:45 +02001880 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001881 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001882
1883 if (opcode == TGSI_OPCODE_TXF) {
1884 /* add tex offsets */
1885 if (inst->Texture.NumOffsets) {
1886 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1887 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1888 const struct tgsi_texture_offset * off = inst->TexOffsets;
1889
1890 assert(inst->Texture.NumOffsets == 1);
1891
Marek Olšákdefedc02013-09-18 15:36:38 +02001892 switch (target) {
1893 case TGSI_TEXTURE_3D:
1894 address[2] = lp_build_add(uint_bld, address[2],
1895 bld->immediates[off->Index][off->SwizzleZ]);
1896 /* fall through */
1897 case TGSI_TEXTURE_2D:
1898 case TGSI_TEXTURE_SHADOW2D:
1899 case TGSI_TEXTURE_RECT:
1900 case TGSI_TEXTURE_SHADOWRECT:
1901 case TGSI_TEXTURE_2D_ARRAY:
1902 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001903 address[1] =
1904 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001905 bld->immediates[off->Index][off->SwizzleY]);
1906 /* fall through */
1907 case TGSI_TEXTURE_1D:
1908 case TGSI_TEXTURE_SHADOW1D:
1909 case TGSI_TEXTURE_1D_ARRAY:
1910 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1911 address[0] =
1912 lp_build_add(uint_bld, address[0],
1913 bld->immediates[off->Index][off->SwizzleX]);
1914 break;
1915 /* texture offsets do not apply to other texture targets */
1916 }
Michel Dänzer36231112013-05-02 09:44:45 +02001917 }
1918
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001919 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1920 emit_data->arg_count = 3;
1921
Michel Dänzer36231112013-05-02 09:44:45 +02001922 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001923 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001924 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001925 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001926 opcode == TGSI_OPCODE_LODQ ||
1927 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001928 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1929 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1930 target == TGSI_TEXTURE_2D_ARRAY ||
1931 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1932 target == TGSI_TEXTURE_CUBE_ARRAY ||
1933 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1934 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001935 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001936
Marek Olšák57f3da92014-06-16 16:53:42 +02001937 if (opcode == TGSI_OPCODE_TG4) {
1938 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001939
Marek Olšák57f3da92014-06-16 16:53:42 +02001940 /* DMASK was repurposed for GATHER4. 4 components are always
1941 * returned and DMASK works like a swizzle - it selects
1942 * the component to fetch. The only valid DMASK values are
1943 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1944 * (red,red,red,red) etc.) The ISA document doesn't mention
1945 * this.
1946 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001947
Marek Olšák57f3da92014-06-16 16:53:42 +02001948 /* Get the component index from src1.x for Gather4. */
1949 if (!tgsi_is_shadow_sampler(target)) {
1950 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1951 LLVMValueRef comp_imm;
1952 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001953
Marek Olšák57f3da92014-06-16 16:53:42 +02001954 assert(src1.File == TGSI_FILE_IMMEDIATE);
1955
1956 comp_imm = imms[src1.Index][src1.SwizzleX];
1957 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1958 gather_comp = CLAMP(gather_comp, 0, 3);
1959 }
1960
1961 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001962 }
1963
Marek Olšák4855acd2013-08-06 15:08:54 +02001964 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001965 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001966 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1967 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1968 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1969 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1970 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1971 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1972 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1973
1974 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001975
1976 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001977 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001978 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001979 } else {
1980 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1981 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001982 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001983
1984 emit_data->dst_type = LLVMVectorType(
1985 LLVMFloatTypeInContext(gallivm->context),
1986 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001987 }
1988
Marek Olšák2484daa2014-04-22 21:23:29 +02001989 /* The fetch opcode has been converted to a 2D array fetch.
1990 * This simplifies the LLVM backend. */
1991 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1992 target = TGSI_TEXTURE_2D_ARRAY;
1993 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1994 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1995
Michel Dänzer120efee2013-01-25 12:10:11 +01001996 /* Pad to power of two vector */
1997 while (count < util_next_power_of_two(count))
1998 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1999
Christian Königccf3e8f2013-03-26 15:09:27 +01002000 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05002001}
2002
Michel Dänzer07eddc42013-02-06 15:43:10 +01002003static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
2004 struct lp_build_tgsi_context * bld_base,
2005 struct lp_build_emit_data * emit_data)
2006{
2007 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02002008 unsigned opcode = emit_data->inst->Instruction.Opcode;
2009 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01002010 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02002011 bool has_offset = HAVE_LLVM >= 0x0305 ?
2012 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01002013
Marek Olšák877bb522014-06-25 03:12:46 +02002014 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002015 emit_data->output[emit_data->chan] = build_intrinsic(
2016 base->gallivm->builder,
2017 "llvm.SI.vs.load.input", emit_data->dst_type,
2018 emit_data->args, emit_data->arg_count,
2019 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2020 return;
2021 }
2022
Marek Olšák877bb522014-06-25 03:12:46 +02002023 if (opcode == TGSI_OPCODE_TG4 ||
2024 opcode == TGSI_OPCODE_LODQ ||
2025 (opcode != TGSI_OPCODE_TXF && has_offset)) {
2026 bool is_shadow = tgsi_is_shadow_sampler(target);
2027 const char *name = "llvm.SI.image.sample";
2028 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01002029
Marek Olšák877bb522014-06-25 03:12:46 +02002030 switch (opcode) {
2031 case TGSI_OPCODE_TEX:
2032 case TGSI_OPCODE_TEX2:
2033 case TGSI_OPCODE_TXP:
2034 break;
2035 case TGSI_OPCODE_TXB:
2036 case TGSI_OPCODE_TXB2:
2037 infix = ".b";
2038 break;
2039 case TGSI_OPCODE_TXL:
2040 case TGSI_OPCODE_TXL2:
2041 infix = ".l";
2042 break;
2043 case TGSI_OPCODE_TXD:
2044 infix = ".d";
2045 break;
2046 case TGSI_OPCODE_TG4:
2047 name = "llvm.SI.gather4";
2048 break;
2049 case TGSI_OPCODE_LODQ:
2050 name = "llvm.SI.getlod";
2051 is_shadow = false;
2052 has_offset = false;
2053 break;
2054 default:
2055 assert(0);
2056 return;
2057 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01002058
Marek Olšák877bb522014-06-25 03:12:46 +02002059 /* Add the type and suffixes .c, .o if needed. */
2060 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
2061 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
2062 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002063
Marek Olšák877bb522014-06-25 03:12:46 +02002064 emit_data->output[emit_data->chan] = build_intrinsic(
2065 base->gallivm->builder, intr_name, emit_data->dst_type,
2066 emit_data->args, emit_data->arg_count,
2067 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2068 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02002069 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02002070 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002071
Marek Olšák877bb522014-06-25 03:12:46 +02002072 switch (opcode) {
2073 case TGSI_OPCODE_TEX:
2074 case TGSI_OPCODE_TEX2:
2075 case TGSI_OPCODE_TXP:
2076 name = "llvm.SI.sample";
2077 break;
2078 case TGSI_OPCODE_TXB:
2079 case TGSI_OPCODE_TXB2:
2080 name = "llvm.SI.sampleb";
2081 break;
2082 case TGSI_OPCODE_TXD:
2083 name = "llvm.SI.sampled";
2084 break;
2085 case TGSI_OPCODE_TXF:
2086 name = "llvm.SI.imageload";
2087 break;
2088 case TGSI_OPCODE_TXL:
2089 case TGSI_OPCODE_TXL2:
2090 name = "llvm.SI.samplel";
2091 break;
2092 default:
2093 assert(0);
2094 return;
2095 }
2096
Marek Olšákd859bdb2014-07-14 19:40:14 +02002097 i8 = LLVMInt8TypeInContext(base->gallivm->context);
2098 v16i8 = LLVMVectorType(i8, 16);
2099 v32i8 = LLVMVectorType(i8, 32);
2100
2101 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
2102 emit_data->args[1], v32i8, "");
2103 if (opcode != TGSI_OPCODE_TXF) {
2104 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
2105 emit_data->args[2], v16i8, "");
2106 }
2107
Marek Olšák877bb522014-06-25 03:12:46 +02002108 sprintf(intr_name, "%s.v%ui32", name,
2109 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
2110
2111 emit_data->output[emit_data->chan] = build_intrinsic(
2112 base->gallivm->builder, intr_name, emit_data->dst_type,
2113 emit_data->args, emit_data->arg_count,
2114 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2115 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002116}
2117
Michel Dänzer0495adb2013-05-06 12:45:14 +02002118static void txq_fetch_args(
2119 struct lp_build_tgsi_context * bld_base,
2120 struct lp_build_emit_data * emit_data)
2121{
2122 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2123 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002124 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002125 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002126
Marek Olšák2484daa2014-04-22 21:23:29 +02002127 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002128 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2129 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2130
2131 /* Read the size from the buffer descriptor directly. */
2132 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2133 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2134 size = LLVMBuildExtractElement(gallivm->builder, size,
2135 lp_build_const_int32(gallivm, 2), "");
2136 emit_data->args[0] = size;
2137 return;
2138 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002139
2140 /* Mip level */
2141 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2142
2143 /* Resource */
2144 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2145
Marek Olšák2484daa2014-04-22 21:23:29 +02002146 /* Texture target */
2147 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2148 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2149 target = TGSI_TEXTURE_2D_ARRAY;
2150
Michel Dänzer0495adb2013-05-06 12:45:14 +02002151 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002152 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002153
2154 emit_data->arg_count = 3;
2155
2156 emit_data->dst_type = LLVMVectorType(
2157 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2158 4);
2159}
2160
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002161static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2162 struct lp_build_tgsi_context * bld_base,
2163 struct lp_build_emit_data * emit_data)
2164{
Marek Olšák2484daa2014-04-22 21:23:29 +02002165 unsigned target = emit_data->inst->Texture.Texture;
2166
2167 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002168 /* Just return the buffer size. */
2169 emit_data->output[emit_data->chan] = emit_data->args[0];
2170 return;
2171 }
2172
2173 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002174
2175 /* Divide the number of layers by 6 to get the number of cubes. */
2176 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2177 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2178 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2179 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2180 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2181
2182 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2183 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2184 z = LLVMBuildSDiv(builder, z, six, "");
2185
2186 emit_data->output[emit_data->chan] =
2187 LLVMBuildInsertElement(builder, v4, z, two, "");
2188 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002189}
2190
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002191static void si_llvm_emit_ddxy(
2192 const struct lp_build_tgsi_action * action,
2193 struct lp_build_tgsi_context * bld_base,
2194 struct lp_build_emit_data * emit_data)
2195{
2196 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2197 struct gallivm_state *gallivm = bld_base->base.gallivm;
2198 struct lp_build_context * base = &bld_base->base;
2199 const struct tgsi_full_instruction *inst = emit_data->inst;
2200 unsigned opcode = inst->Instruction.Opcode;
2201 LLVMValueRef indices[2];
2202 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2203 LLVMValueRef tl, trbl, result[4];
2204 LLVMTypeRef i32;
2205 unsigned swizzle[4];
2206 unsigned c;
2207
2208 i32 = LLVMInt32TypeInContext(gallivm->context);
2209
2210 indices[0] = bld_base->uint_bld.zero;
2211 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2212 NULL, 0, LLVMReadNoneAttribute);
2213 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2214 indices, 2, "");
2215
2216 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2217 lp_build_const_int32(gallivm, 0xfffffffc), "");
2218 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2219 indices, 2, "");
2220
2221 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2222 lp_build_const_int32(gallivm,
2223 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2224 "");
2225 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2226 indices, 2, "");
2227
2228 for (c = 0; c < 4; ++c) {
2229 unsigned i;
2230
2231 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2232 for (i = 0; i < c; ++i) {
2233 if (swizzle[i] == swizzle[c]) {
2234 result[c] = result[i];
2235 break;
2236 }
2237 }
2238 if (i != c)
2239 continue;
2240
2241 LLVMBuildStore(gallivm->builder,
2242 LLVMBuildBitCast(gallivm->builder,
2243 lp_build_emit_fetch(bld_base, inst, 0, c),
2244 i32, ""),
2245 store_ptr);
2246
2247 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2248 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2249
2250 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2251 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2252
2253 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2254 }
2255
2256 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2257}
2258
Michel Dänzer404b29d2013-11-21 16:45:28 +09002259/* Emit one vertex from the geometry shader */
2260static void si_llvm_emit_vertex(
2261 const struct lp_build_tgsi_action *action,
2262 struct lp_build_tgsi_context *bld_base,
2263 struct lp_build_emit_data *emit_data)
2264{
2265 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002266 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002267 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002268 struct gallivm_state *gallivm = bld_base->base.gallivm;
2269 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002270 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2271 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002272 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002273 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002274 LLVMValueRef t_list_ptr;
2275 LLVMValueRef t_list;
2276 LLVMValueRef args[2];
2277 unsigned chan;
2278 int i;
2279
2280 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002281 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2282 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002283 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002284 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002285
2286 if (shader->noutput == 0) {
2287 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
2288
2289 while (!tgsi_parse_end_of_tokens(parse)) {
2290 tgsi_parse_token(parse);
2291
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002292 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
2293 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
2294
2295 if (d->Declaration.File == TGSI_FILE_OUTPUT)
2296 si_store_shader_io_attribs(shader, d);
2297 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09002298 }
2299 }
2300
2301 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002302 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002303
2304 /* If this thread has already emitted the declared maximum number of
2305 * vertices, kill it: excessive vertex emissions are not supposed to
2306 * have any effect, and GS threads have no externally observable
2307 * effects other than emitting vertices.
2308 */
2309 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2310 lp_build_const_int32(gallivm,
2311 shader->gs_max_out_vertices), "");
2312 kill = lp_build_select(&bld_base->base, can_emit,
2313 lp_build_const_float(gallivm, 1.0f),
2314 lp_build_const_float(gallivm, -1.0f));
2315 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2316 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2317
Michel Dänzer404b29d2013-11-21 16:45:28 +09002318 for (i = 0; i < shader->noutput; i++) {
2319 LLVMValueRef *out_ptr =
2320 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
2321
2322 for (chan = 0; chan < 4; chan++) {
2323 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002324 LLVMValueRef voffset =
2325 lp_build_const_int32(gallivm, (i * 4 + chan) *
2326 shader->gs_max_out_vertices);
2327
2328 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2329 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002330
2331 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2332
2333 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2334 voffset, soffset, 0,
2335 V_008F0C_BUF_DATA_FORMAT_32,
2336 V_008F0C_BUF_NUM_FORMAT_UINT,
2337 1, 0, 1, 1, 0);
2338 }
2339 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002340 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2341 lp_build_const_int32(gallivm, 1));
2342 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002343
2344 /* Signal vertex emission */
2345 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2346 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2347 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2348 LLVMVoidTypeInContext(gallivm->context), args, 2,
2349 LLVMNoUnwindAttribute);
2350}
2351
2352/* Cut one primitive from the geometry shader */
2353static void si_llvm_emit_primitive(
2354 const struct lp_build_tgsi_action *action,
2355 struct lp_build_tgsi_context *bld_base,
2356 struct lp_build_emit_data *emit_data)
2357{
2358 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2359 struct gallivm_state *gallivm = bld_base->base.gallivm;
2360 LLVMValueRef args[2];
2361
2362 /* Signal primitive cut */
2363 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2364 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2365 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2366 LLVMVoidTypeInContext(gallivm->context), args, 2,
2367 LLVMNoUnwindAttribute);
2368}
2369
Tom Stellarda75c6162012-01-06 17:38:37 -05002370static const struct lp_build_tgsi_action tex_action = {
2371 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002372 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002373};
2374
Michel Dänzer0495adb2013-05-06 12:45:14 +02002375static const struct lp_build_tgsi_action txq_action = {
2376 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002377 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002378 .intr_name = "llvm.SI.resinfo"
2379};
2380
Christian König206f0592013-03-20 14:37:21 +01002381static void create_meta_data(struct si_shader_context *si_shader_ctx)
2382{
2383 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2384 LLVMValueRef args[3];
2385
2386 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2387 args[1] = 0;
2388 args[2] = lp_build_const_int32(gallivm, 1);
2389
2390 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2391}
2392
Marek Olšák4f3f0432014-07-07 22:49:15 +02002393static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2394{
2395 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2396 CONST_ADDR_SPACE);
2397}
2398
Christian König55fe5cc2013-03-04 16:30:06 +01002399static void create_function(struct si_shader_context *si_shader_ctx)
2400{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002401 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2402 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002403 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002404 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002405 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002406
Christian König55fe5cc2013-03-04 16:30:06 +01002407 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002408 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002409 f32 = LLVMFloatTypeInContext(gallivm->context);
2410 v2i32 = LLVMVectorType(i32, 2);
2411 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002412 v4i32 = LLVMVectorType(i32, 4);
2413 v8i32 = LLVMVectorType(i32, 8);
2414 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002415
Marek Olšákee2a8182014-07-07 23:27:19 +02002416 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
2417 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
2418 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2419 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Christian König55fe5cc2013-03-04 16:30:06 +01002420
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002421 switch (si_shader_ctx->type) {
2422 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002423 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšák09056b32014-04-23 16:15:36 +02002424 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002425 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002426 num_params = SI_PARAM_START_INSTANCE+1;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002427 if (shader->key.vs.as_es) {
2428 params[SI_PARAM_ES2GS_OFFSET] = i32;
2429 num_params++;
2430 } else {
2431 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002432
Michel Dänzer404b29d2013-11-21 16:45:28 +09002433 /* Streamout SGPRs. */
2434 if (shader->selector->so.num_outputs) {
2435 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2436 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2437 }
2438 /* A streamout buffer offset is loaded if the stride is non-zero. */
2439 for (i = 0; i < 4; i++) {
2440 if (!shader->selector->so.stride[i])
2441 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002442
Michel Dänzer404b29d2013-11-21 16:45:28 +09002443 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2444 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002445 }
2446
2447 last_sgpr = num_params-1;
2448
2449 /* VGPRs */
2450 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2451 params[num_params++] = i32; /* unused*/
2452 params[num_params++] = i32; /* unused */
2453 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002454 break;
Christian König0666ffd2013-03-05 15:07:39 +01002455
Michel Dänzer404b29d2013-11-21 16:45:28 +09002456 case TGSI_PROCESSOR_GEOMETRY:
2457 params[SI_PARAM_GS2VS_OFFSET] = i32;
2458 params[SI_PARAM_GS_WAVE_ID] = i32;
2459 last_sgpr = SI_PARAM_GS_WAVE_ID;
2460
2461 /* VGPRs */
2462 params[SI_PARAM_VTX0_OFFSET] = i32;
2463 params[SI_PARAM_VTX1_OFFSET] = i32;
2464 params[SI_PARAM_PRIMITIVE_ID] = i32;
2465 params[SI_PARAM_VTX2_OFFSET] = i32;
2466 params[SI_PARAM_VTX3_OFFSET] = i32;
2467 params[SI_PARAM_VTX4_OFFSET] = i32;
2468 params[SI_PARAM_VTX5_OFFSET] = i32;
2469 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2470 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2471 break;
2472
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002473 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002474 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002475 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002476 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002477 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2478 params[SI_PARAM_PERSP_CENTER] = v2i32;
2479 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2480 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2481 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2482 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2483 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2484 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2485 params[SI_PARAM_POS_X_FLOAT] = f32;
2486 params[SI_PARAM_POS_Y_FLOAT] = f32;
2487 params[SI_PARAM_POS_Z_FLOAT] = f32;
2488 params[SI_PARAM_POS_W_FLOAT] = f32;
2489 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002490 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002491 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2492 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002493 num_params = SI_PARAM_POS_FIXED_PT+1;
2494 break;
2495
2496 default:
2497 assert(0 && "unimplemented shader");
2498 return;
Christian Königc4973212013-03-05 12:14:02 +01002499 }
Christian König55fe5cc2013-03-04 16:30:06 +01002500
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002501 assert(num_params <= Elements(params));
2502 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002503 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002504
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002505 for (i = 0; i <= last_sgpr; ++i) {
2506 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002507 switch (i) {
2508 default:
2509 LLVMAddAttribute(P, LLVMInRegAttribute);
2510 break;
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002511 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2512 * to move load. Inputs are constant so this is fine. */
2513 case SI_PARAM_CONST:
2514 case SI_PARAM_SAMPLER:
2515 case SI_PARAM_RESOURCE:
2516 LLVMAddAttribute(P, LLVMByValAttribute);
2517 break;
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002518 }
Christian Königcf9b31f2013-03-21 18:30:23 +01002519 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002520
Michel Dänzer404b29d2013-11-21 16:45:28 +09002521 if (bld_base->info &&
2522 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2523 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002524 si_shader_ctx->ddxy_lds =
2525 LLVMAddGlobalInAddressSpace(gallivm->module,
2526 LLVMArrayType(i32, 64),
2527 "ddxy_lds",
2528 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002529}
Tom Stellarda75c6162012-01-06 17:38:37 -05002530
Christian König0f6cf2b2013-03-15 15:53:25 +01002531static void preload_constants(struct si_shader_context *si_shader_ctx)
2532{
2533 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2534 struct gallivm_state * gallivm = bld_base->base.gallivm;
2535 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002536 unsigned buf;
2537 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002538
Marek Olšákee2a8182014-07-07 23:27:19 +02002539 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002540 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002541
Marek Olšák2fd42002013-10-25 11:45:47 +02002542 if (num_const == 0)
2543 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002544
Marek Olšák2fd42002013-10-25 11:45:47 +02002545 /* Allocate space for the constant values */
2546 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002547
Marek Olšák2fd42002013-10-25 11:45:47 +02002548 /* Load the resource descriptor */
2549 si_shader_ctx->const_resource[buf] =
2550 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002551
Marek Olšák2fd42002013-10-25 11:45:47 +02002552 /* Load the constants, we rely on the code sinking to do the rest */
2553 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002554 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002555 load_const(gallivm->builder,
2556 si_shader_ctx->const_resource[buf],
2557 lp_build_const_int32(gallivm, i * 4),
2558 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002559 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002560 }
2561}
2562
Christian König1c100182013-03-17 16:02:42 +01002563static void preload_samplers(struct si_shader_context *si_shader_ctx)
2564{
2565 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2566 struct gallivm_state * gallivm = bld_base->base.gallivm;
2567 const struct tgsi_shader_info * info = bld_base->info;
2568
2569 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2570
2571 LLVMValueRef res_ptr, samp_ptr;
2572 LLVMValueRef offset;
2573
2574 if (num_samplers == 0)
2575 return;
2576
2577 /* Allocate space for the values */
Marek Olšákee2a8182014-07-07 23:27:19 +02002578 si_shader_ctx->resources = CALLOC(SI_NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002579 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2580
2581 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2582 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2583
2584 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2585 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002586 /* Resource */
2587 offset = lp_build_const_int32(gallivm, i);
2588 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2589
2590 /* Sampler */
2591 offset = lp_build_const_int32(gallivm, i);
2592 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002593
2594 /* FMASK resource */
2595 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002596 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2597 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšák4855acd2013-08-06 15:08:54 +02002598 build_indexed_load(si_shader_ctx, res_ptr, offset);
2599 }
Christian König1c100182013-03-17 16:02:42 +01002600 }
2601}
2602
Marek Olšák8d03d922013-09-01 23:59:06 +02002603static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2604{
2605 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2606 struct gallivm_state * gallivm = bld_base->base.gallivm;
2607 unsigned i;
2608
Michel Dänzer67e385b2014-01-08 17:48:21 +09002609 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2610 si_shader_ctx->shader->key.vs.as_es ||
2611 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002612 return;
2613
2614 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002615 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002616
2617 /* Load the resources, we rely on the code sinking to do the rest */
2618 for (i = 0; i < 4; ++i) {
2619 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002620 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002621 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002622
2623 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2624 }
2625 }
2626}
2627
Marek Olšák1abb1a92014-09-17 22:44:22 +02002628int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2629 LLVMModuleRef mod)
Tom Stellard302f53d2012-10-25 13:50:10 -04002630{
Darren Powellbc866902014-03-31 18:00:28 -04002631 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002632 unsigned i;
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002633 unsigned char *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002634 struct radeon_shader_binary binary;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002635 bool dump = r600_can_dump_shader(&sscreen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002636 shader->selector ? shader->selector->tokens : NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002637 const char * gpu_family = r600_get_llvm_processor_name(sscreen->b.family);
Tom Stellard9ba31052014-07-14 16:49:08 -04002638 unsigned code_size;
Darren Powellbc866902014-03-31 18:00:28 -04002639
2640 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002641 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002642 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2643
2644 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002645 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002646 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002647 for (i = 0; i < binary.code_size; i+=4 ) {
2648 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2649 binary.code[i + 2], binary.code[i + 1],
2650 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002651 }
2652 }
2653
Tom Stellardd50343d2013-04-04 16:21:06 -04002654 /* XXX: We may be able to emit some of these values directly rather than
2655 * extracting fields to be emitted later.
2656 */
Darren Powellbc866902014-03-31 18:00:28 -04002657 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002658 for (i = 0; i < binary.config_size; i+= 8) {
2659 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2660 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2661 switch (reg) {
2662 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2663 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2664 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2665 case R_00B848_COMPUTE_PGM_RSRC1:
2666 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2667 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2668 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002669 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2670 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2671 break;
2672 case R_00B84C_COMPUTE_PGM_RSRC2:
2673 shader->lds_size = G_00B84C_LDS_SIZE(value);
2674 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002675 case R_0286CC_SPI_PS_INPUT_ENA:
2676 shader->spi_ps_input_ena = value;
2677 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002678 case R_00B860_COMPUTE_TMPRING_SIZE:
2679 /* WAVESIZE is in units of 256 dwords. */
2680 shader->scratch_bytes_per_wave =
2681 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2682 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002683 default:
2684 fprintf(stderr, "Warning: Compiler emitted unknown "
2685 "config register: 0x%x\n", reg);
2686 break;
2687 }
2688 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002689
2690 /* copy new shader */
Tom Stellard9ba31052014-07-14 16:49:08 -04002691 code_size = binary.code_size + binary.rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002692 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002693 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002694 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002695 if (shader->bo == NULL) {
2696 return -ENOMEM;
2697 }
2698
Marek Olšák1abb1a92014-09-17 22:44:22 +02002699 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_WRITE);
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002700 util_memcpy_cpu_to_le32(ptr, binary.code, binary.code_size);
2701 if (binary.rodata_size > 0) {
2702 ptr += binary.code_size;
2703 util_memcpy_cpu_to_le32(ptr, binary.rodata, binary.rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002704 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002705
Marek Olšák1abb1a92014-09-17 22:44:22 +02002706 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002707
Tom Stellard7782d192013-04-04 09:57:13 -07002708 free(binary.code);
2709 free(binary.config);
Tom Stellard9ba31052014-07-14 16:49:08 -04002710 free(binary.rodata);
Tom Stellard302f53d2012-10-25 13:50:10 -04002711
Darren Powellbc866902014-03-31 18:00:28 -04002712 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002713}
2714
Michel Dänzer404b29d2013-11-21 16:45:28 +09002715/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002716static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002717 struct si_shader_context *si_shader_ctx,
2718 bool dump)
2719{
2720 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2721 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2722 struct lp_build_context *base = &bld_base->base;
2723 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002724 struct si_shader *shader = si_shader_ctx->shader;
2725 struct si_shader *gs = si_shader_ctx->shader->selector->current;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002726 struct si_shader_output_values *outputs;
2727 LLVMValueRef t_list_ptr, t_list;
2728 LLVMValueRef args[9];
2729 int i, r;
2730
2731 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2732
2733 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002734
2735 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2736
2737 create_meta_data(si_shader_ctx);
2738 create_function(si_shader_ctx);
2739 preload_streamout_buffers(si_shader_ctx);
2740
2741 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002742 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2743 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002744 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002745 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002746
2747 args[0] = t_list;
2748 args[1] = lp_build_mul_imm(uint,
2749 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2750 si_shader_ctx->param_vertex_id),
2751 4);
2752 args[3] = uint->zero;
2753 args[4] = uint->one; /* OFFEN */
2754 args[5] = uint->zero; /* IDXEN */
2755 args[6] = uint->one; /* GLC */
2756 args[7] = uint->one; /* SLC */
2757 args[8] = uint->zero; /* TFE */
2758
2759 /* Fetch vertex data from GSVS ring */
2760 for (i = 0; i < gs->noutput; ++i) {
2761 struct si_shader_output *out = gs->output + i;
2762 unsigned chan;
2763
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002764 shader->output[i] = *out;
2765
Michel Dänzer404b29d2013-11-21 16:45:28 +09002766 outputs[i].name = out->name;
2767 outputs[i].index = out->index;
Michel Dänzer67e385b2014-01-08 17:48:21 +09002768 outputs[i].sid = out->sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002769 outputs[i].usage = out->usage;
2770
2771 for (chan = 0; chan < 4; chan++) {
2772 args[2] = lp_build_const_int32(gallivm,
2773 (i * 4 + chan) *
2774 gs->gs_max_out_vertices * 16 * 4);
2775
2776 outputs[i].values[chan] =
2777 LLVMBuildBitCast(gallivm->builder,
2778 build_intrinsic(gallivm->builder,
2779 "llvm.SI.buffer.load.dword.i32.i32",
2780 LLVMInt32TypeInContext(gallivm->context),
2781 args, 9,
2782 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2783 base->elem_type, "");
2784 }
2785 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002786 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002787
2788 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2789
2790 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2791
2792 if (dump)
2793 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2794
Marek Olšák1abb1a92014-09-17 22:44:22 +02002795 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002796 bld_base->base.gallivm->module);
2797
2798 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2799
2800 FREE(outputs);
2801 return r;
2802}
2803
Marek Olšák1abb1a92014-09-17 22:44:22 +02002804int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002805{
Marek Olšák2774abd2014-09-16 18:45:33 +02002806 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002807 struct si_shader_context si_shader_ctx;
2808 struct tgsi_shader_info shader_info;
2809 struct lp_build_tgsi_context * bld_base;
2810 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002811 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002812 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002813
2814 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2815 * conversion fails. */
2816 if (dump) {
2817 tgsi_dump(sel->tokens, 0);
2818 si_dump_streamout(&sel->so);
2819 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002820
Marek Olšák8c37c162014-09-16 18:40:07 +02002821 assert(shader->noutput == 0);
2822 assert(shader->nparam == 0);
2823 assert(shader->ninput == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002824
Michel Dänzercfebaf92012-08-31 19:04:08 +02002825 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002826 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2827 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2828
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002829 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002830
Marek Olšákadc57972014-09-19 17:07:07 +02002831 if (shader_info.uses_kill)
2832 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2833
Marek Olšák8c37c162014-09-16 18:40:07 +02002834 shader->uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002835 bld_base->info = &shader_info;
2836 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002837
2838 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002839 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002840 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2841 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002842 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002843 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2844 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2845 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002846 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002847 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002848 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2849 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002850
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002851 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2852 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002853
Michel Dänzer404b29d2013-11-21 16:45:28 +09002854 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2855 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2856
Christian Könige4ed5872013-03-21 18:02:52 +01002857 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002858 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002859 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2860 si_shader_ctx.shader = shader;
2861 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002862
Michel Dänzer51f89a02013-12-09 15:33:53 +09002863 switch (si_shader_ctx.type) {
2864 case TGSI_PROCESSOR_VERTEX:
2865 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002866 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002867 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2868 } else {
2869 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2870 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002871 break;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002872 case TGSI_PROCESSOR_GEOMETRY: {
2873 int i;
2874
2875 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2876 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2877 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
2878
2879 for (i = 0; i < shader_info.num_properties; i++) {
2880 switch (shader_info.properties[i].name) {
2881 case TGSI_PROPERTY_GS_INPUT_PRIM:
Marek Olšák8c37c162014-09-16 18:40:07 +02002882 shader->gs_input_prim = shader_info.properties[i].data[0];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002883 break;
2884 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
Marek Olšák8c37c162014-09-16 18:40:07 +02002885 shader->gs_output_prim = shader_info.properties[i].data[0];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002886 break;
2887 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
Marek Olšák8c37c162014-09-16 18:40:07 +02002888 shader->gs_max_out_vertices = shader_info.properties[i].data[0];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002889 break;
2890 }
2891 }
2892 break;
2893 }
Marek Olšák6a2b3832014-06-14 17:58:30 +02002894 case TGSI_PROCESSOR_FRAGMENT: {
2895 int i;
2896
Michel Dänzer51f89a02013-12-09 15:33:53 +09002897 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2898 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002899
2900 for (i = 0; i < shader_info.num_properties; i++) {
2901 switch (shader_info.properties[i].name) {
2902 case TGSI_PROPERTY_FS_DEPTH_LAYOUT:
2903 switch (shader_info.properties[i].data[0]) {
2904 case TGSI_FS_DEPTH_LAYOUT_GREATER:
Marek Olšáka34c9f72014-09-19 16:19:44 +02002905 shader->db_shader_control |=
2906 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
Marek Olšák6a2b3832014-06-14 17:58:30 +02002907 break;
2908 case TGSI_FS_DEPTH_LAYOUT_LESS:
Marek Olšáka34c9f72014-09-19 16:19:44 +02002909 shader->db_shader_control |=
2910 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
Marek Olšák6a2b3832014-06-14 17:58:30 +02002911 break;
2912 }
2913 break;
2914 }
2915 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002916 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002917 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002918 default:
2919 assert(!"Unsupported shader type");
2920 return -1;
2921 }
2922
Christian König206f0592013-03-20 14:37:21 +01002923 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002924 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002925 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002926 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002927 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002928
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002929 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2930 si_shader_ctx.gs_next_vertex =
2931 lp_build_alloca(bld_base->base.gallivm,
2932 bld_base->uint_bld.elem_type, "");
2933 }
2934
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002935 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002936 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002937 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002938 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002939
2940 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2941
2942 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002943 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002944 if (r) {
2945 fprintf(stderr, "LLVM failed to compile shader\n");
2946 goto out;
2947 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002948
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002949 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002950
2951 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002952 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002953 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002954 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002955 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002956 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002957 free(shader->gs_copy_shader);
2958 shader->gs_copy_shader = NULL;
2959 goto out;
2960 }
2961 }
2962
Tom Stellarda75c6162012-01-06 17:38:37 -05002963 tgsi_parse_free(&si_shader_ctx.parse);
2964
Michel Dänzer404b29d2013-11-21 16:45:28 +09002965out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002966 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002967 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002968 FREE(si_shader_ctx.resources);
2969 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002970
Tom Stellard302f53d2012-10-25 13:50:10 -04002971 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002972}
2973
Marek Olšák2774abd2014-09-16 18:45:33 +02002974void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002975{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002976 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002977}