blob: e21cb76d9f970d4c128e2d654c3b675a8b3425dd [file] [log] [blame]
Christian Königce40e472012-08-02 12:14:59 +02001/*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
27 */
28
Tom Stellarda75c6162012-01-06 17:38:37 -050029#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020030#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050031#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010032#include "gallivm/lp_bld_logic.h"
Christian König5e616cf2013-03-07 11:58:56 +010033#include "gallivm/lp_bld_arit.h"
Marek Olšák8d03d922013-09-01 23:59:06 +020034#include "gallivm/lp_bld_flow.h"
Emil Velikova1312632014-08-16 17:58:25 +010035#include "radeon/radeon_llvm.h"
36#include "radeon/radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010037#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010039#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_dump.h"
41
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010042#include "si_pipe.h"
43#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050044#include "sid.h"
45
Tom Stellarda75c6162012-01-06 17:38:37 -050046#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050047
Michel Dänzer404b29d2013-11-21 16:45:28 +090048struct si_shader_output_values
49{
50 LLVMValueRef values[4];
51 unsigned name;
52 unsigned index;
Michel Dänzer67e385b2014-01-08 17:48:21 +090053 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090054};
55
Tom Stellarda75c6162012-01-06 17:38:37 -050056struct si_shader_context
57{
58 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050059 struct tgsi_parse_context parse;
60 struct tgsi_token * tokens;
Marek Olšák8c37c162014-09-16 18:40:07 +020061 struct si_shader *shader;
Tom Stellarda75c6162012-01-06 17:38:37 -050062 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020063 int param_streamout_config;
64 int param_streamout_write_index;
65 int param_streamout_offset[4];
66 int param_vertex_id;
67 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010068 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020069 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020070 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020071 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Christian König1c100182013-03-17 16:02:42 +010072 LLVMValueRef *resources;
73 LLVMValueRef *samplers;
Marek Olšák8d03d922013-09-01 23:59:06 +020074 LLVMValueRef so_buffers[4];
Michel Dänzerf07a96d2014-01-08 18:45:10 +090075 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050076};
77
78static struct si_shader_context * si_shader_context(
79 struct lp_build_tgsi_context * bld_base)
80{
81 return (struct si_shader_context *)bld_base;
82}
83
84
85#define PERSPECTIVE_BASE 0
86#define LINEAR_BASE 9
87
88#define SAMPLE_OFFSET 0
89#define CENTER_OFFSET 2
90#define CENTROID_OFSET 4
91
92#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040093#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020094#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040095#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050096
Michel Dänzer404b29d2013-11-21 16:45:28 +090097
98#define SENDMSG_GS 2
99#define SENDMSG_GS_DONE 3
100
101#define SENDMSG_GS_OP_NOP (0 << 4)
102#define SENDMSG_GS_OP_CUT (1 << 4)
103#define SENDMSG_GS_OP_EMIT (2 << 4)
104#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
105
Marek Olšáke29353f2014-09-17 22:17:02 +0200106/**
107 * Returns a unique index for a semantic name and index. The index must be
108 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
109 * calculated.
110 */
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200111unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index)
Marek Olšáke29353f2014-09-17 22:17:02 +0200112{
113 switch (semantic_name) {
114 case TGSI_SEMANTIC_POSITION:
115 return 0;
116 case TGSI_SEMANTIC_PSIZE:
117 return 1;
118 case TGSI_SEMANTIC_CLIPDIST:
119 assert(index <= 1);
120 return 2 + index;
121 case TGSI_SEMANTIC_CLIPVERTEX:
122 return 4;
123 case TGSI_SEMANTIC_COLOR:
124 assert(index <= 1);
125 return 5 + index;
126 case TGSI_SEMANTIC_BCOLOR:
127 assert(index <= 1);
128 return 7 + index;
129 case TGSI_SEMANTIC_FOG:
130 return 9;
131 case TGSI_SEMANTIC_EDGEFLAG:
132 return 10;
133 case TGSI_SEMANTIC_GENERIC:
134 assert(index <= 63-11);
135 return 11 + index;
136 default:
137 assert(0);
138 return 63;
139 }
140}
141
142/**
143 * Given a semantic name and index of a parameter and a mask of used parameters
144 * (inputs or outputs), return the index of the parameter in the list of all
145 * used parameters.
146 *
147 * For example, assume this list of parameters:
148 * POSITION, PSIZE, GENERIC0, GENERIC2
149 * which has the mask:
150 * 11000000000101
151 * Then:
152 * querying POSITION returns 0,
153 * querying PSIZE returns 1,
154 * querying GENERIC0 returns 2,
155 * querying GENERIC2 returns 3.
156 *
157 * Which can be used as an offset to a parameter buffer in units of vec4s.
158 */
159static int get_param_index(unsigned semantic_name, unsigned index,
160 uint64_t mask)
161{
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200162 unsigned unique_index = si_shader_io_get_unique_index(semantic_name, index);
Marek Olšáke29353f2014-09-17 22:17:02 +0200163 int i, param_index = 0;
164
165 /* If not present... */
166 if (!((1llu << unique_index) & mask))
167 return -1;
168
169 for (i = 0; mask; i++) {
170 uint64_t bit = 1llu << i;
171
172 if (bit & mask) {
173 if (i == unique_index)
174 return param_index;
175
176 mask &= ~bit;
177 param_index++;
178 }
179 }
180
181 assert(!"unreachable");
182 return -1;
183}
Michel Dänzer404b29d2013-11-21 16:45:28 +0900184
Tom Stellard467f5162012-05-16 15:15:35 -0400185/**
186 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
187 *
188 * @param offset The offset parameter specifies the number of
189 * elements to offset, not the number of bytes or dwords. An element is the
190 * the type pointed to by the base_ptr parameter (e.g. int is the element of
191 * an int* pointer)
192 *
193 * When LLVM lowers the load instruction, it will convert the element offset
194 * into a dword offset automatically.
195 *
196 */
197static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100198 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400199 LLVMValueRef base_ptr,
200 LLVMValueRef offset)
201{
Christian König206f0592013-03-20 14:37:21 +0100202 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400203
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200204 LLVMValueRef indices[2] = {
205 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
206 offset
207 };
Christian König206f0592013-03-20 14:37:21 +0100208 LLVMValueRef computed_ptr = LLVMBuildGEP(
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200209 base->gallivm->builder, base_ptr, indices, 2, "");
Christian König206f0592013-03-20 14:37:21 +0100210
211 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
212 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
213 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400214}
215
Marek Olšákf317ce52013-09-05 15:39:57 +0200216static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100217 struct radeon_llvm_context * radeon_bld,
218 unsigned divisor)
219{
Marek Olšák8d03d922013-09-01 23:59:06 +0200220 struct si_shader_context *si_shader_ctx =
221 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100222 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
223
Marek Olšák8d03d922013-09-01 23:59:06 +0200224 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
225 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100226 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
227 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
228
229 if (divisor > 1)
230 result = LLVMBuildUDiv(gallivm->builder, result,
231 lp_build_const_int32(gallivm, divisor), "");
232
233 return result;
234}
235
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900236static int si_store_shader_io_attribs(struct si_shader *shader,
237 const struct tgsi_full_declaration *d)
238{
239 int i = -1;
240
241 switch (d->Declaration.File) {
242 case TGSI_FILE_INPUT:
243 i = shader->ninput++;
244 assert(i < Elements(shader->input));
245 shader->input[i].name = d->Semantic.Name;
246 shader->input[i].sid = d->Semantic.Index;
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900247 shader->input[i].index = d->Range.First;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900248 shader->input[i].interpolate = d->Interp.Interpolate;
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400249 shader->input[i].centroid = d->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900250 return -1;
251
252 case TGSI_FILE_OUTPUT:
253 i = shader->noutput++;
254 assert(i < Elements(shader->output));
255 shader->output[i].name = d->Semantic.Name;
256 shader->output[i].sid = d->Semantic.Index;
257 shader->output[i].index = d->Range.First;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900258 break;
259 }
260
261 return i;
262}
263
Tom Stellarda75c6162012-01-06 17:38:37 -0500264static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900265 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500266 unsigned input_index,
267 const struct tgsi_full_declaration *decl)
268{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900269 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
270 struct gallivm_state *gallivm = base->gallivm;
271 struct si_shader_context *si_shader_ctx =
272 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100273 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
274
275 unsigned chan;
276
Tom Stellarda75c6162012-01-06 17:38:37 -0500277 LLVMValueRef t_list_ptr;
278 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400279 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500280 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100281 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400282 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500283 LLVMTypeRef vec4_type;
284 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500285
Tom Stellard467f5162012-05-16 15:15:35 -0400286 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100287 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500288
Michel Dänzer51f89a02013-12-09 15:33:53 +0900289 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400290
Christian König206f0592013-03-20 14:37:21 +0100291 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400292
293 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900294 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500295
Christian Königa0dca442013-03-22 15:59:22 +0100296 if (divisor) {
297 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200298 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200299 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100300 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200301 /* Load the buffer index for vertices. */
302 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
303 si_shader_ctx->param_vertex_id);
304 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
305 SI_PARAM_BASE_VERTEX);
306 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100307 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500308
309 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400310 args[0] = t_list;
311 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100312 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900313 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100314 "llvm.SI.vs.load.input", vec4_type, args, 3,
315 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500316
317 /* Break up the vec4 into individual components */
318 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900319 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500320 /* XXX: Use a helper function for this. There is one in
321 * tgsi_llvm.c. */
322 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900323 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500324 input, llvm_chan, "");
325 }
326}
327
Michel Dänzer404b29d2013-11-21 16:45:28 +0900328static void declare_input_gs(
329 struct radeon_llvm_context *radeon_bld,
330 unsigned input_index,
331 const struct tgsi_full_declaration *decl)
332{
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900333 struct si_shader_context *si_shader_ctx =
334 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200335 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900336
337 si_store_shader_io_attribs(shader, decl);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900338}
339
340static LLVMValueRef fetch_input_gs(
341 struct lp_build_tgsi_context *bld_base,
342 const struct tgsi_full_src_register *reg,
343 enum tgsi_opcode_type type,
344 unsigned swizzle)
345{
346 struct lp_build_context *base = &bld_base->base;
347 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200348 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900349 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
350 struct gallivm_state *gallivm = base->gallivm;
351 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
352 LLVMValueRef vtx_offset;
353 LLVMValueRef t_list_ptr;
354 LLVMValueRef t_list;
355 LLVMValueRef args[9];
356 unsigned vtx_offset_param;
Marek Olšáke29353f2014-09-17 22:17:02 +0200357 struct si_shader_input *input = &shader->input[reg->Register.Index];
Michel Dänzer404b29d2013-11-21 16:45:28 +0900358
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900359 if (swizzle != ~0 &&
360 shader->input[reg->Register.Index].name == TGSI_SEMANTIC_PRIMID) {
361 if (swizzle == 0)
362 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
363 SI_PARAM_PRIMITIVE_ID);
364 else
365 return uint->zero;
366 }
367
Michel Dänzer404b29d2013-11-21 16:45:28 +0900368 if (!reg->Register.Dimension)
369 return NULL;
370
371 if (swizzle == ~0) {
372 LLVMValueRef values[TGSI_NUM_CHANNELS];
373 unsigned chan;
374 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
375 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
376 }
377 return lp_build_gather_values(bld_base->base.gallivm, values,
378 TGSI_NUM_CHANNELS);
379 }
380
381 /* Get the vertex offset parameter */
382 vtx_offset_param = reg->Dimension.Index;
383 if (vtx_offset_param < 2) {
384 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
385 } else {
386 assert(vtx_offset_param < 6);
387 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
388 }
389 vtx_offset = lp_build_mul_imm(uint,
390 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
391 vtx_offset_param),
392 4);
393
394 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +0900395 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
396 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900397 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900398 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900399
400 args[0] = t_list;
401 args[1] = vtx_offset;
402 args[2] = lp_build_const_int32(gallivm,
Marek Olšáke29353f2014-09-17 22:17:02 +0200403 (get_param_index(input->name, input->sid,
Marek Olšák0a2d6f02014-09-30 16:55:36 +0200404 shader->selector->gs_used_inputs) * 4 +
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900405 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900406 args[3] = uint->zero;
407 args[4] = uint->one; /* OFFEN */
408 args[5] = uint->zero; /* IDXEN */
409 args[6] = uint->one; /* GLC */
410 args[7] = uint->zero; /* SLC */
411 args[8] = uint->zero; /* TFE */
412
413 return LLVMBuildBitCast(gallivm->builder,
414 build_intrinsic(gallivm->builder,
415 "llvm.SI.buffer.load.dword.i32.i32",
416 i32, args, 9,
417 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
418 tgsi2llvmtype(bld_base, type), "");
419}
420
Tom Stellarda75c6162012-01-06 17:38:37 -0500421static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900422 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500423 unsigned input_index,
424 const struct tgsi_full_declaration *decl)
425{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900426 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
427 struct si_shader_context *si_shader_ctx =
428 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200429 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900430 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
431 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400432 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900433 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100434
435 LLVMValueRef interp_param;
436 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500437
438 /* This value is:
439 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
440 * quad begins a new primitive. Bit 0 always needs
441 * to be unset)
442 * [32:16] ParamOffset
443 *
444 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900445 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200446 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500447
Christian König0666ffd2013-03-05 15:07:39 +0100448 unsigned chan;
449
Tom Stellard0fb1e682012-09-06 16:18:11 -0400450 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
451 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400452 unsigned soa_index =
453 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900454 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100455 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100456
457 if (chan == 3)
458 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900459 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100460 LLVMBuildFDiv(gallivm->builder,
461 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900462 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100463 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400464 }
465 return;
466 }
467
Michel Dänzer97078b12012-09-25 12:41:31 +0200468 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
469 LLVMValueRef face, is_face_positive;
470
Christian König0666ffd2013-03-05 15:07:39 +0100471 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
472
Michel Dänzer97078b12012-09-25 12:41:31 +0200473 is_face_positive = LLVMBuildFCmp(gallivm->builder,
474 LLVMRealUGT, face,
475 lp_build_const_float(gallivm, 0.0f),
476 "");
477
Michel Dänzer51f89a02013-12-09 15:33:53 +0900478 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200479 LLVMBuildSelect(gallivm->builder,
480 is_face_positive,
481 lp_build_const_float(gallivm, 1.0f),
482 lp_build_const_float(gallivm, 0.0f),
483 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900484 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
485 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200486 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900487 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200488 lp_build_const_float(gallivm, 1.0f);
489
490 return;
491 }
492
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900493 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200494 attr_number = lp_build_const_int32(gallivm,
495 shader->input[input_index].param_offset);
496
Francisco Jerez12799232012-04-30 18:27:52 +0200497 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500498 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100499 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500500 break;
501 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák10e386f2014-09-30 17:09:13 +0200502 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200503 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400504 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100505 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200506 else
Christian König0666ffd2013-03-05 15:07:39 +0100507 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200508 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200509 case TGSI_INTERPOLATE_COLOR:
510 if (si_shader_ctx->shader->key.ps.flatshade) {
511 interp_param = 0;
512 break;
513 }
514 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200515 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák10e386f2014-09-30 17:09:13 +0200516 if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_SAMPLE)
Marek Olšák99df1202014-05-06 19:10:52 +0200517 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400518 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100519 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200520 else
Christian König0666ffd2013-03-05 15:07:39 +0100521 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500522 break;
523 default:
524 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
525 return;
526 }
527
Christian König0666ffd2013-03-05 15:07:39 +0100528 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
529
Tom Stellarda75c6162012-01-06 17:38:37 -0500530 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200531 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100532 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100533 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200534 LLVMValueRef face, is_face_positive;
535 LLVMValueRef back_attr_number =
536 lp_build_const_int32(gallivm,
537 shader->input[input_index].param_offset + 1);
538
Christian König0666ffd2013-03-05 15:07:39 +0100539 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
540
Michel Dänzer691f08d2012-09-06 18:03:38 +0200541 is_face_positive = LLVMBuildFCmp(gallivm->builder,
542 LLVMRealUGT, face,
543 lp_build_const_float(gallivm, 0.0f),
544 "");
545
Tom Stellarda75c6162012-01-06 17:38:37 -0500546 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100547 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200548 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
549 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
550 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
551 LLVMValueRef front, back;
552
553 args[0] = llvm_chan;
554 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900555 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100556 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100557 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200558
559 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900560 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100561 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100562 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200563
Michel Dänzer51f89a02013-12-09 15:33:53 +0900564 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200565 LLVMBuildSelect(gallivm->builder,
566 is_face_positive,
567 front,
568 back,
569 "");
570 }
571
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900572 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200573 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
574 LLVMValueRef args[4];
575
576 args[0] = uint->zero;
577 args[1] = attr_number;
578 args[2] = params;
579 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900580 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
581 build_intrinsic(gallivm->builder, intr_name,
582 input_type, args, args[3] ? 4 : 3,
583 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
584 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
585 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200586 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900587 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200588 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200589 } else {
590 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100591 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200592 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
593 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
594 args[0] = llvm_chan;
595 args[1] = attr_number;
596 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100597 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900598 radeon_bld->inputs[soa_index] =
599 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100600 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100601 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200602 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500603 }
604}
605
Marek Olšák5b06fc32014-05-06 18:12:40 +0200606static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
607{
608 struct gallivm_state *gallivm = &radeon_bld->gallivm;
609 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
610 SI_PARAM_ANCILLARY);
611 value = LLVMBuildLShr(gallivm->builder, value,
612 lp_build_const_int32(gallivm, 8), "");
613 value = LLVMBuildAnd(gallivm->builder, value,
614 lp_build_const_int32(gallivm, 0xf), "");
615 return value;
616}
617
Marek Olšák250aa932014-05-06 14:10:47 +0200618static LLVMValueRef load_const(LLVMBuilderRef builder, LLVMValueRef resource,
619 LLVMValueRef offset, LLVMTypeRef return_type)
620{
621 LLVMValueRef args[2] = {resource, offset};
622
623 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
624 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
625}
626
Christian Könige4ed5872013-03-21 18:02:52 +0100627static void declare_system_value(
628 struct radeon_llvm_context * radeon_bld,
629 unsigned index,
630 const struct tgsi_full_declaration *decl)
631{
Marek Olšák8d03d922013-09-01 23:59:06 +0200632 struct si_shader_context *si_shader_ctx =
633 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200634 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
635 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100636 LLVMValueRef value = 0;
637
638 switch (decl->Semantic.Name) {
639 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200640 value = LLVMGetParam(radeon_bld->main_fn,
641 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100642 break;
643
644 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200645 value = LLVMGetParam(radeon_bld->main_fn,
646 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100647 break;
648
Marek Olšák5b06fc32014-05-06 18:12:40 +0200649 case TGSI_SEMANTIC_SAMPLEID:
650 value = get_sample_id(radeon_bld);
651 break;
652
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200653 case TGSI_SEMANTIC_SAMPLEPOS:
654 {
655 LLVMBuilderRef builder = gallivm->builder;
656 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200657 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200658 LLVMValueRef resource = build_indexed_load(si_shader_ctx, desc, buf_index);
659
660 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
661 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
662 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
663
664 LLVMValueRef pos[4] = {
665 load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
666 load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
667 lp_build_const_float(gallivm, 0),
668 lp_build_const_float(gallivm, 0)
669 };
670 value = lp_build_gather_values(gallivm, pos, 4);
671 break;
672 }
673
Christian Könige4ed5872013-03-21 18:02:52 +0100674 default:
675 assert(!"unknown system value");
676 return;
677 }
678
679 radeon_bld->system_values[index] = value;
680}
681
Tom Stellarda75c6162012-01-06 17:38:37 -0500682static LLVMValueRef fetch_constant(
683 struct lp_build_tgsi_context * bld_base,
684 const struct tgsi_full_src_register *reg,
685 enum tgsi_opcode_type type,
686 unsigned swizzle)
687{
Christian König55fe5cc2013-03-04 16:30:06 +0100688 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500689 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100690 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200691 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500692
Christian König0f6cf2b2013-03-15 15:53:25 +0100693 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100694 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500695
Christian König8514f5a2013-02-04 17:46:42 +0100696 if (swizzle == LP_CHAN_ALL) {
697 unsigned chan;
698 LLVMValueRef values[4];
699 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
700 values[chan] = fetch_constant(bld_base, reg, type, chan);
701
702 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
703 }
704
Marek Olšák2fd42002013-10-25 11:45:47 +0200705 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100706 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100707
Marek Olšák2fd42002013-10-25 11:45:47 +0200708 if (!reg->Register.Indirect)
709 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
710
Christian König0f6cf2b2013-03-15 15:53:25 +0100711 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
712 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
713 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200714 addr = lp_build_add(&bld_base->uint_bld, addr,
715 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200716
Marek Olšák250aa932014-05-06 14:10:47 +0200717 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
718 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500719
Christian Königf5298b02013-02-28 14:50:07 +0100720 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500721}
722
Michel Dänzer26c71392012-08-24 12:03:11 +0200723/* Initialize arguments for the shader export intrinsic */
724static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900725 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200726 unsigned target,
727 LLVMValueRef *args)
728{
729 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
730 struct lp_build_context *uint =
731 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
732 struct lp_build_context *base = &bld_base->base;
733 unsigned compressed = 0;
734 unsigned chan;
735
Michel Dänzerf402acd2012-08-22 18:15:36 +0200736 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
737 int cbuf = target - V_008DFC_SQ_EXP_MRT;
738
739 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100740 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100741
742 if (compressed)
743 si_shader_ctx->shader->spi_shader_col_format |=
744 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
745 else
746 si_shader_ctx->shader->spi_shader_col_format |=
747 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200748
749 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200750 }
751 }
752
753 if (compressed) {
754 /* Pixel shader needs to pack output values before export */
755 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900756 args[0] = values[2 * chan];
757 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200758 args[chan + 5] =
759 build_intrinsic(base->gallivm->builder,
760 "llvm.SI.packf16",
761 LLVMInt32TypeInContext(base->gallivm->context),
762 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100763 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100764 args[chan + 7] = args[chan + 5] =
765 LLVMBuildBitCast(base->gallivm->builder,
766 args[chan + 5],
767 LLVMFloatTypeInContext(base->gallivm->context),
768 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200769 }
770
771 /* Set COMPR flag */
772 args[4] = uint->one;
773 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900774 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200775 /* +5 because the first output value will be
776 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900777 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200778
779 /* Clear COMPR flag */
780 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200781 }
782
783 /* XXX: This controls which components of the output
784 * registers actually get exported. (e.g bit 0 means export
785 * X component, bit 1 means export Y component, etc.) I'm
786 * hard coding this to 0xf for now. In the future, we might
787 * want to do something else. */
788 args[0] = lp_build_const_int32(base->gallivm, 0xf);
789
790 /* Specify whether the EXEC mask represents the valid mask */
791 args[1] = uint->zero;
792
793 /* Specify whether this is the last export */
794 args[2] = uint->zero;
795
796 /* Specify the target we are exporting */
797 args[3] = lp_build_const_int32(base->gallivm, target);
798
Michel Dänzer26c71392012-08-24 12:03:11 +0200799 /* XXX: We probably need to keep track of the output
800 * values, so we know what we are passing to the next
801 * stage. */
802}
803
Michel Dänzer404b29d2013-11-21 16:45:28 +0900804/* Load from output pointers and initialize arguments for the shader export intrinsic */
805static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
806 LLVMValueRef *out_ptr,
807 unsigned target,
808 LLVMValueRef *args)
809{
810 struct gallivm_state *gallivm = bld_base->base.gallivm;
811 LLVMValueRef values[4];
812 int i;
813
814 for (i = 0; i < 4; i++)
815 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
816
817 si_llvm_init_export_args(bld_base, values, target, args);
818}
819
Michel Dänzer7708a862012-11-02 15:57:30 +0100820static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900821 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100822{
823 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
824 struct gallivm_state *gallivm = bld_base->base.gallivm;
825
Christian Königa0dca442013-03-22 15:59:22 +0100826 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400827 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
828 SI_PARAM_ALPHA_REF);
829
Michel Dänzer7708a862012-11-02 15:57:30 +0100830 LLVMValueRef alpha_pass =
831 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100832 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900833 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400834 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100835 LLVMValueRef arg =
836 lp_build_select(&bld_base->base,
837 alpha_pass,
838 lp_build_const_float(gallivm, 1.0f),
839 lp_build_const_float(gallivm, -1.0f));
840
841 build_intrinsic(gallivm->builder,
842 "llvm.AMDGPU.kill",
843 LLVMVoidTypeInContext(gallivm->context),
844 &arg, 1, 0);
845 } else {
846 build_intrinsic(gallivm->builder,
847 "llvm.AMDGPU.kilp",
848 LLVMVoidTypeInContext(gallivm->context),
849 NULL, 0, 0);
850 }
Marek Olšákadc57972014-09-19 17:07:07 +0200851
852 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100853}
854
Michel Dänzere3befbc2013-05-15 18:09:50 +0200855static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900856 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200857{
858 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200859 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200860 struct lp_build_context *base = &bld_base->base;
861 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200862 unsigned reg_index;
863 unsigned chan;
864 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200865 LLVMValueRef base_elt;
866 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200867 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák2fd42002013-10-25 11:45:47 +0200868 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200869
Michel Dänzere3befbc2013-05-15 18:09:50 +0200870 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200871 LLVMValueRef *args = pos[2 + reg_index];
872
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200873 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
874 continue;
875
Marek Olšák8c37c162014-09-16 18:40:07 +0200876 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200877
Michel Dänzere3befbc2013-05-15 18:09:50 +0200878 args[5] =
879 args[6] =
880 args[7] =
881 args[8] = lp_build_const_float(base->gallivm, 0.0f);
882
883 /* Compute dot products of position and user clip plane vectors */
884 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
885 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200886 args[1] = lp_build_const_int32(base->gallivm,
887 ((reg_index * 4 + chan) * 4 +
888 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200889 base_elt = load_const(base->gallivm->builder, const_resource,
890 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200891 args[5 + chan] =
892 lp_build_add(base, args[5 + chan],
893 lp_build_mul(base, base_elt,
894 out_elts[const_chan]));
895 }
896 }
897
898 args[0] = lp_build_const_int32(base->gallivm, 0xf);
899 args[1] = uint->zero;
900 args[2] = uint->zero;
901 args[3] = lp_build_const_int32(base->gallivm,
902 V_008DFC_SQ_EXP_POS + 2 + reg_index);
903 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200904 }
905}
906
Marek Olšák8d03d922013-09-01 23:59:06 +0200907static void si_dump_streamout(struct pipe_stream_output_info *so)
908{
909 unsigned i;
910
911 if (so->num_outputs)
912 fprintf(stderr, "STREAMOUT\n");
913
914 for (i = 0; i < so->num_outputs; i++) {
915 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
916 so->output[i].start_component;
917 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
918 i, so->output[i].output_buffer,
919 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
920 so->output[i].register_index,
921 mask & 1 ? "x" : "",
922 mask & 2 ? "y" : "",
923 mask & 4 ? "z" : "",
924 mask & 8 ? "w" : "");
925 }
926}
927
928/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
929 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
930 * or v4i32 (num_channels=3,4). */
931static void build_tbuffer_store(struct si_shader_context *shader,
932 LLVMValueRef rsrc,
933 LLVMValueRef vdata,
934 unsigned num_channels,
935 LLVMValueRef vaddr,
936 LLVMValueRef soffset,
937 unsigned inst_offset,
938 unsigned dfmt,
939 unsigned nfmt,
940 unsigned offen,
941 unsigned idxen,
942 unsigned glc,
943 unsigned slc,
944 unsigned tfe)
945{
946 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
947 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
948 LLVMValueRef args[] = {
949 rsrc,
950 vdata,
951 LLVMConstInt(i32, num_channels, 0),
952 vaddr,
953 soffset,
954 LLVMConstInt(i32, inst_offset, 0),
955 LLVMConstInt(i32, dfmt, 0),
956 LLVMConstInt(i32, nfmt, 0),
957 LLVMConstInt(i32, offen, 0),
958 LLVMConstInt(i32, idxen, 0),
959 LLVMConstInt(i32, glc, 0),
960 LLVMConstInt(i32, slc, 0),
961 LLVMConstInt(i32, tfe, 0)
962 };
963
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900964 /* The instruction offset field has 12 bits */
965 assert(offen || inst_offset < (1 << 12));
966
Marek Olšák8d03d922013-09-01 23:59:06 +0200967 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
968 unsigned func = CLAMP(num_channels, 1, 3) - 1;
969 const char *types[] = {"i32", "v2i32", "v4i32"};
970 char name[256];
971 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
972
973 lp_build_intrinsic(gallivm->builder, name,
974 LLVMVoidTypeInContext(gallivm->context),
975 args, Elements(args));
976}
977
978static void build_streamout_store(struct si_shader_context *shader,
979 LLVMValueRef rsrc,
980 LLVMValueRef vdata,
981 unsigned num_channels,
982 LLVMValueRef vaddr,
983 LLVMValueRef soffset,
984 unsigned inst_offset)
985{
986 static unsigned dfmt[] = {
987 V_008F0C_BUF_DATA_FORMAT_32,
988 V_008F0C_BUF_DATA_FORMAT_32_32,
989 V_008F0C_BUF_DATA_FORMAT_32_32_32,
990 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
991 };
992 assert(num_channels >= 1 && num_channels <= 4);
993
994 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
995 inst_offset, dfmt[num_channels-1],
996 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
997}
998
999/* On SI, the vertex shader is responsible for writing streamout data
1000 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +09001001static void si_llvm_emit_streamout(struct si_shader_context *shader,
1002 struct si_shader_output_values *outputs,
1003 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +02001004{
1005 struct pipe_stream_output_info *so = &shader->shader->selector->so;
1006 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
1007 LLVMBuilderRef builder = gallivm->builder;
1008 int i, j;
1009 struct lp_build_if_state if_ctx;
1010
1011 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1012
1013 LLVMValueRef so_param =
1014 LLVMGetParam(shader->radeon_bld.main_fn,
1015 shader->param_streamout_config);
1016
1017 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
1018 LLVMValueRef so_vtx_count =
1019 LLVMBuildAnd(builder,
1020 LLVMBuildLShr(builder, so_param,
1021 LLVMConstInt(i32, 16, 0), ""),
1022 LLVMConstInt(i32, 127, 0), "");
1023
1024 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
1025 NULL, 0, LLVMReadNoneAttribute);
1026
1027 /* can_emit = tid < so_vtx_count; */
1028 LLVMValueRef can_emit =
1029 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
1030
1031 /* Emit the streamout code conditionally. This actually avoids
1032 * out-of-bounds buffer access. The hw tells us via the SGPR
1033 * (so_vtx_count) which threads are allowed to emit streamout data. */
1034 lp_build_if(&if_ctx, gallivm, can_emit);
1035 {
1036 /* The buffer offset is computed as follows:
1037 * ByteOffset = streamout_offset[buffer_id]*4 +
1038 * (streamout_write_index + thread_id)*stride[buffer_id] +
1039 * attrib_offset
1040 */
1041
1042 LLVMValueRef so_write_index =
1043 LLVMGetParam(shader->radeon_bld.main_fn,
1044 shader->param_streamout_write_index);
1045
1046 /* Compute (streamout_write_index + thread_id). */
1047 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
1048
1049 /* Compute the write offset for each enabled buffer. */
1050 LLVMValueRef so_write_offset[4] = {};
1051 for (i = 0; i < 4; i++) {
1052 if (!so->stride[i])
1053 continue;
1054
1055 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
1056 shader->param_streamout_offset[i]);
1057 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
1058
1059 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
1060 LLVMConstInt(i32, so->stride[i]*4, 0), "");
1061 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
1062 }
1063
Marek Olšák8d03d922013-09-01 23:59:06 +02001064 /* Write streamout data. */
1065 for (i = 0; i < so->num_outputs; i++) {
1066 unsigned buf_idx = so->output[i].output_buffer;
1067 unsigned reg = so->output[i].register_index;
1068 unsigned start = so->output[i].start_component;
1069 unsigned num_comps = so->output[i].num_components;
1070 LLVMValueRef out[4];
1071
1072 assert(num_comps && num_comps <= 4);
1073 if (!num_comps || num_comps > 4)
1074 continue;
1075
1076 /* Load the output as int. */
1077 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001078 unsigned outidx = 0;
1079
1080 while (outidx < noutput && outputs[outidx].index != reg)
1081 outidx++;
1082
1083 if (outidx < noutput)
1084 out[j] = LLVMBuildBitCast(builder,
1085 outputs[outidx].values[start+j],
1086 i32, "");
1087 else
1088 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +02001089 }
1090
Michel Dänzer67e385b2014-01-08 17:48:21 +09001091 if (!out[0])
1092 continue;
1093
Marek Olšák8d03d922013-09-01 23:59:06 +02001094 /* Pack the output. */
1095 LLVMValueRef vdata = NULL;
1096
1097 switch (num_comps) {
1098 case 1: /* as i32 */
1099 vdata = out[0];
1100 break;
1101 case 2: /* as v2i32 */
1102 case 3: /* as v4i32 (aligned to 4) */
1103 case 4: /* as v4i32 */
1104 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1105 for (j = 0; j < num_comps; j++) {
1106 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1107 LLVMConstInt(i32, j, 0), "");
1108 }
1109 break;
1110 }
1111
1112 build_streamout_store(shader, shader->so_buffers[buf_idx],
1113 vdata, num_comps,
1114 so_write_offset[buf_idx],
1115 LLVMConstInt(i32, 0, 0),
1116 so->output[i].dst_offset*4);
1117 }
1118 }
1119 lp_build_endif(&if_ctx);
1120}
1121
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001122
Michel Dänzer404b29d2013-11-21 16:45:28 +09001123/* Generate export instructions for hardware VS shader stage */
1124static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1125 struct si_shader_output_values *outputs,
1126 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001127{
1128 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001129 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001130 struct lp_build_context * base = &bld_base->base;
1131 struct lp_build_context * uint =
1132 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001133 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001134 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001135 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
Marek Olšáka9592cd2014-10-04 19:09:09 +02001136 unsigned semantic_name, semantic_index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001137 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001138 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001139 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001140 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001141
Michel Dänzer67e385b2014-01-08 17:48:21 +09001142 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1143 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001144 }
1145
Michel Dänzer404b29d2013-11-21 16:45:28 +09001146 for (i = 0; i < noutput; i++) {
1147 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001148 semantic_index = outputs[i].sid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001149
Michel Dänzer0afeea52013-05-02 14:53:17 +02001150handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001151 /* Select the correct target */
1152 switch(semantic_name) {
1153 case TGSI_SEMANTIC_PSIZE:
1154 shader->vs_out_misc_write = true;
1155 shader->vs_out_point_size = true;
1156 psize_value = outputs[i].values[0];
1157 continue;
1158 case TGSI_SEMANTIC_EDGEFLAG:
1159 shader->vs_out_misc_write = true;
1160 shader->vs_out_edgeflag = true;
1161 edgeflag_value = outputs[i].values[0];
1162 continue;
1163 case TGSI_SEMANTIC_LAYER:
1164 shader->vs_out_misc_write = true;
1165 shader->vs_out_layer = true;
1166 layer_value = outputs[i].values[0];
1167 continue;
1168 case TGSI_SEMANTIC_POSITION:
1169 target = V_008DFC_SQ_EXP_POS;
1170 break;
1171 case TGSI_SEMANTIC_COLOR:
1172 case TGSI_SEMANTIC_BCOLOR:
1173 target = V_008DFC_SQ_EXP_PARAM + param_count;
1174 shader->output[i].param_offset = param_count;
1175 param_count++;
1176 break;
1177 case TGSI_SEMANTIC_CLIPDIST:
1178 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
1179 (1 << semantic_index)))
Marek Olšák053606d2013-11-19 22:07:30 +01001180 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001181 shader->clip_dist_write |=
Marek Olšáka9592cd2014-10-04 19:09:09 +02001182 0xf << (semantic_index * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001183 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1184 break;
1185 case TGSI_SEMANTIC_CLIPVERTEX:
1186 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1187 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001188 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001189 case TGSI_SEMANTIC_FOG:
1190 case TGSI_SEMANTIC_GENERIC:
1191 target = V_008DFC_SQ_EXP_PARAM + param_count;
1192 shader->output[i].param_offset = param_count;
1193 param_count++;
1194 break;
1195 default:
1196 target = 0;
1197 fprintf(stderr,
1198 "Warning: SI unhandled vs output type:%d\n",
1199 semantic_name);
1200 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001201
Michel Dänzer404b29d2013-11-21 16:45:28 +09001202 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001203
Michel Dänzer404b29d2013-11-21 16:45:28 +09001204 if (target >= V_008DFC_SQ_EXP_POS &&
1205 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1206 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1207 args, sizeof(args));
1208 } else {
1209 lp_build_intrinsic(base->gallivm->builder,
1210 "llvm.SI.export",
1211 LLVMVoidTypeInContext(base->gallivm->context),
1212 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001213 }
1214
1215 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1216 semantic_name = TGSI_SEMANTIC_GENERIC;
1217 goto handle_semantic;
1218 }
1219 }
1220
1221 /* We need to add the position output manually if it's missing. */
1222 if (!pos_args[0][0]) {
1223 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1224 pos_args[0][1] = uint->zero; /* EXEC mask */
1225 pos_args[0][2] = uint->zero; /* last export? */
1226 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1227 pos_args[0][4] = uint->zero; /* COMPR flag */
1228 pos_args[0][5] = base->zero; /* X */
1229 pos_args[0][6] = base->zero; /* Y */
1230 pos_args[0][7] = base->zero; /* Z */
1231 pos_args[0][8] = base->one; /* W */
1232 }
1233
1234 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1235 if (shader->vs_out_misc_write) {
1236 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1237 shader->vs_out_point_size |
1238 (shader->vs_out_edgeflag << 1) |
1239 (shader->vs_out_layer << 2));
1240 pos_args[1][1] = uint->zero; /* EXEC mask */
1241 pos_args[1][2] = uint->zero; /* last export? */
1242 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1243 pos_args[1][4] = uint->zero; /* COMPR flag */
1244 pos_args[1][5] = base->zero; /* X */
1245 pos_args[1][6] = base->zero; /* Y */
1246 pos_args[1][7] = base->zero; /* Z */
1247 pos_args[1][8] = base->zero; /* W */
1248
Michel Dänzer404b29d2013-11-21 16:45:28 +09001249 if (shader->vs_out_point_size)
1250 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001251
1252 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001253 /* The output is a float, but the hw expects an integer
1254 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001255 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1256 edgeflag_value,
1257 bld_base->uint_bld.elem_type, "");
1258 edgeflag_value = lp_build_min(&bld_base->int_bld,
1259 edgeflag_value,
1260 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001261
1262 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001263 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1264 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001265 base->elem_type, "");
1266 }
1267
Michel Dänzer404b29d2013-11-21 16:45:28 +09001268 if (shader->vs_out_layer)
1269 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001270 }
1271
1272 for (i = 0; i < 4; i++)
1273 if (pos_args[i][0])
1274 shader->nr_pos_exports++;
1275
1276 pos_idx = 0;
1277 for (i = 0; i < 4; i++) {
1278 if (!pos_args[i][0])
1279 continue;
1280
1281 /* Specify the target we are exporting */
1282 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1283
1284 if (pos_idx == shader->nr_pos_exports)
1285 /* Specify that this is the last export */
1286 pos_args[i][2] = uint->one;
1287
1288 lp_build_intrinsic(base->gallivm->builder,
1289 "llvm.SI.export",
1290 LLVMVoidTypeInContext(base->gallivm->context),
1291 pos_args[i], 9);
1292 }
1293}
1294
Michel Dänzer404b29d2013-11-21 16:45:28 +09001295static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1296{
1297 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1298 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001299 struct si_shader *es = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001300 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1301 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001302 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1303 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001304 LLVMValueRef t_list_ptr;
1305 LLVMValueRef t_list;
1306 unsigned chan;
1307 int i;
1308
1309 while (!tgsi_parse_end_of_tokens(parse)) {
1310 struct tgsi_full_declaration *d =
1311 &parse->FullToken.FullDeclaration;
1312
1313 tgsi_parse_token(parse);
1314
1315 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1316 continue;
1317
Michel Dänzere884c562014-01-15 15:24:14 +09001318 si_store_shader_io_attribs(es, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001319 }
1320
1321 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001322 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1323 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001324 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001325 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001326
Michel Dänzere884c562014-01-15 15:24:14 +09001327 for (i = 0; i < es->noutput; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001328 LLVMValueRef *out_ptr =
Michel Dänzere884c562014-01-15 15:24:14 +09001329 si_shader_ctx->radeon_bld.soa.outputs[es->output[i].index];
Marek Olšáke29353f2014-09-17 22:17:02 +02001330 int param_index = get_param_index(es->output[i].name,
1331 es->output[i].sid,
1332 es->key.vs.gs_used_inputs);
Michel Dänzere884c562014-01-15 15:24:14 +09001333
Marek Olšáke29353f2014-09-17 22:17:02 +02001334 if (param_index < 0)
Michel Dänzere884c562014-01-15 15:24:14 +09001335 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001336
1337 for (chan = 0; chan < 4; chan++) {
1338 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001339 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1340
1341 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001342 LLVMGetUndef(i32), soffset,
Marek Olšáke29353f2014-09-17 22:17:02 +02001343 (4 * param_index + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001344 V_008F0C_BUF_DATA_FORMAT_32,
1345 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001346 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001347 }
1348 }
1349}
1350
1351static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1352{
1353 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1354 struct gallivm_state *gallivm = bld_base->base.gallivm;
1355 LLVMValueRef args[2];
1356
1357 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1358 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1359 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1360 LLVMVoidTypeInContext(gallivm->context), args, 2,
1361 LLVMNoUnwindAttribute);
1362}
1363
1364static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
1365{
1366 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1367 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001368 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001369 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1370 struct si_shader_output_values *outputs = NULL;
1371 unsigned noutput = 0;
1372 int i;
1373
1374 while (!tgsi_parse_end_of_tokens(parse)) {
1375 struct tgsi_full_declaration *d =
1376 &parse->FullToken.FullDeclaration;
1377 unsigned index;
1378
1379 tgsi_parse_token(parse);
1380
1381 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1382 continue;
1383
Marek Olšák8c37c162014-09-16 18:40:07 +02001384 i = si_store_shader_io_attribs(shader, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001385 if (i < 0)
1386 continue;
1387
1388 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1389 (noutput + 1) * sizeof(outputs[0]));
1390 for (index = d->Range.First; index <= d->Range.Last; index++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001391 outputs[noutput].index = index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001392 outputs[noutput].name = d->Semantic.Name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001393 outputs[noutput].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001394
1395 for (i = 0; i < 4; i++)
1396 outputs[noutput].values[i] =
1397 LLVMBuildLoad(gallivm->builder,
1398 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1399 "");
1400 }
1401 noutput++;
1402 }
1403
1404 si_llvm_export_vs(bld_base, outputs, noutput);
1405 FREE(outputs);
1406}
1407
Michel Dänzer51f89a02013-12-09 15:33:53 +09001408static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1409{
1410 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001411 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001412 struct lp_build_context * base = &bld_base->base;
1413 struct lp_build_context * uint =
1414 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1415 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1416 LLVMValueRef args[9];
1417 LLVMValueRef last_args[9] = { 0 };
1418 unsigned semantic_name;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001419 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001420 int i;
1421
1422 while (!tgsi_parse_end_of_tokens(parse)) {
1423 struct tgsi_full_declaration *d =
1424 &parse->FullToken.FullDeclaration;
1425 unsigned target;
1426 unsigned index;
1427
1428 tgsi_parse_token(parse);
1429
Michel Dänzer51f89a02013-12-09 15:33:53 +09001430 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1431 continue;
1432
1433 i = si_store_shader_io_attribs(shader, d);
1434 if (i < 0)
1435 continue;
1436
1437 semantic_name = d->Semantic.Name;
1438 for (index = d->Range.First; index <= d->Range.Last; index++) {
1439 /* Select the correct target */
1440 switch(semantic_name) {
1441 case TGSI_SEMANTIC_POSITION:
1442 depth_index = index;
1443 continue;
1444 case TGSI_SEMANTIC_STENCIL:
1445 stencil_index = index;
1446 continue;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001447 case TGSI_SEMANTIC_SAMPLEMASK:
1448 samplemask_index = index;
1449 continue;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001450 case TGSI_SEMANTIC_COLOR:
1451 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1452 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001453 LLVMBuildStore(bld_base->base.gallivm->builder,
1454 bld_base->base.one,
1455 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1456
Michel Dänzer51f89a02013-12-09 15:33:53 +09001457 if (d->Semantic.Index == 0 &&
1458 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001459 si_alpha_test(bld_base,
1460 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001461 break;
1462 default:
1463 target = 0;
1464 fprintf(stderr,
1465 "Warning: SI unhandled fs output type:%d\n",
1466 semantic_name);
1467 }
1468
Michel Dänzer404b29d2013-11-21 16:45:28 +09001469 si_llvm_init_export_args_load(bld_base,
1470 si_shader_ctx->radeon_bld.soa.outputs[index],
1471 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001472
1473 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001474 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001475 if (last_args[0]) {
1476 lp_build_intrinsic(base->gallivm->builder,
1477 "llvm.SI.export",
1478 LLVMVoidTypeInContext(base->gallivm->context),
1479 last_args, 9);
1480 }
1481
Marek Olšák0eb528a2013-12-04 13:24:22 +01001482 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001483 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001484
1485 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02001486 if (shader->selector->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
Marek Olšák88605842014-09-30 15:56:14 +02001487 shader->output[i].sid == 0 &&
Marek Olšák0eb528a2013-12-04 13:24:22 +01001488 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1489 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001490 si_llvm_init_export_args_load(bld_base,
1491 si_shader_ctx->radeon_bld.soa.outputs[index],
1492 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001493 lp_build_intrinsic(base->gallivm->builder,
1494 "llvm.SI.export",
1495 LLVMVoidTypeInContext(base->gallivm->context),
1496 args, 9);
1497 }
1498 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001499 } else {
1500 lp_build_intrinsic(base->gallivm->builder,
1501 "llvm.SI.export",
1502 LLVMVoidTypeInContext(base->gallivm->context),
1503 args, 9);
1504 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001505 }
1506 }
1507
Marek Olšákd0e8b652014-05-06 20:04:31 +02001508 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001509 LLVMValueRef out_ptr;
1510 unsigned mask = 0;
1511
1512 /* Specify the target we are exporting */
1513 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1514
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001515 args[5] = base->zero; /* R, depth */
1516 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1517 args[7] = base->zero; /* B, sample mask */
1518 args[8] = base->zero; /* A, alpha to mask */
1519
Michel Dänzer1a616c12012-11-13 17:35:09 +01001520 if (depth_index >= 0) {
1521 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1522 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1523 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001524 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001525 }
1526
1527 if (stencil_index >= 0) {
1528 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001529 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001530 /* Only setting the stencil component bit (0x2) here
1531 * breaks some stencil piglit tests
1532 */
1533 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001534 si_shader_ctx->shader->db_shader_control |=
1535 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001536 }
1537
Marek Olšákd0e8b652014-05-06 20:04:31 +02001538 if (samplemask_index >= 0) {
1539 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1540 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1541 mask |= 0xf; /* Set all components. */
1542 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1543 }
1544
1545 if (samplemask_index >= 0)
1546 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1547 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001548 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1549 else
1550 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1551
Michel Dänzer1a616c12012-11-13 17:35:09 +01001552 /* Specify which components to enable */
1553 args[0] = lp_build_const_int32(base->gallivm, mask);
1554
1555 args[1] =
1556 args[2] =
1557 args[4] = uint->zero;
1558
1559 if (last_args[0])
1560 lp_build_intrinsic(base->gallivm->builder,
1561 "llvm.SI.export",
1562 LLVMVoidTypeInContext(base->gallivm->context),
1563 args, 9);
1564 else
1565 memcpy(last_args, args, sizeof(args));
1566 }
1567
Michel Dänzer51f89a02013-12-09 15:33:53 +09001568 if (!last_args[0]) {
1569 /* Specify which components to enable */
1570 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001571
Michel Dänzer51f89a02013-12-09 15:33:53 +09001572 /* Specify the target we are exporting */
1573 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001574
Michel Dänzer51f89a02013-12-09 15:33:53 +09001575 /* Set COMPR flag to zero to export data as 32-bit */
1576 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001577
Michel Dänzer51f89a02013-12-09 15:33:53 +09001578 /* dummy bits */
1579 last_args[5]= uint->zero;
1580 last_args[6]= uint->zero;
1581 last_args[7]= uint->zero;
1582 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001583 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001584
1585 /* Specify whether the EXEC mask represents the valid mask */
1586 last_args[1] = uint->one;
1587
1588 /* Specify that this is the last export */
1589 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1590
1591 lp_build_intrinsic(base->gallivm->builder,
1592 "llvm.SI.export",
1593 LLVMVoidTypeInContext(base->gallivm->context),
1594 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001595}
1596
Marek Olšák4855acd2013-08-06 15:08:54 +02001597static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1598 struct lp_build_tgsi_context * bld_base,
1599 struct lp_build_emit_data * emit_data);
1600
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001601static bool tgsi_is_shadow_sampler(unsigned target)
1602{
1603 return target == TGSI_TEXTURE_SHADOW1D ||
1604 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1605 target == TGSI_TEXTURE_SHADOW2D ||
1606 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1607 target == TGSI_TEXTURE_SHADOWCUBE ||
1608 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1609 target == TGSI_TEXTURE_SHADOWRECT;
1610}
1611
Marek Olšák877bb522014-06-25 03:12:46 +02001612static const struct lp_build_tgsi_action tex_action;
1613
Tom Stellarda75c6162012-01-06 17:38:37 -05001614static void tex_fetch_args(
1615 struct lp_build_tgsi_context * bld_base,
1616 struct lp_build_emit_data * emit_data)
1617{
Christian König55fe5cc2013-03-04 16:30:06 +01001618 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001619 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001620 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001621 unsigned opcode = inst->Instruction.Opcode;
1622 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001623 LLVMValueRef coords[4];
1624 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001625 int ref_pos;
1626 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001627 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001628 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001629 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1630 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001631 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001632
1633 if (target == TGSI_TEXTURE_BUFFER) {
1634 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1635 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1636 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1637 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1638
Marek Olšák4f3f0432014-07-07 22:49:15 +02001639 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001640 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1641 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1642 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1643 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1644
1645 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1646 emit_data->args[0] = res;
1647 emit_data->args[1] = bld_base->uint_bld.zero;
1648 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1649 emit_data->arg_count = 3;
1650 return;
1651 }
Tom Stellard467f5162012-05-16 15:15:35 -04001652
Michel Dänzer120efee2013-01-25 12:10:11 +01001653 /* Fetch and project texture coordinates */
1654 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001655 for (chan = 0; chan < 3; chan++ ) {
1656 coords[chan] = lp_build_emit_fetch(bld_base,
1657 emit_data->inst, 0,
1658 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001659 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001660 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1661 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001662 coords[chan],
1663 coords[3]);
1664 }
1665
Michel Dänzer120efee2013-01-25 12:10:11 +01001666 if (opcode == TGSI_OPCODE_TXP)
1667 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001668
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001669 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001670 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001671 /* The offsets are six-bit signed integers packed like this:
1672 * X=[5:0], Y=[13:8], and Z=[21:16].
1673 */
1674 LLVMValueRef offset[3], pack;
1675
1676 assert(inst->Texture.NumOffsets == 1);
1677
1678 for (chan = 0; chan < 3; chan++) {
1679 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1680 emit_data->inst, 0, chan);
1681 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1682 lp_build_const_int32(gallivm, 0x3f), "");
1683 if (chan)
1684 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1685 lp_build_const_int32(gallivm, chan*8), "");
1686 }
1687
1688 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1689 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1690 address[count++] = pack;
1691 }
1692
Michel Dänzer120efee2013-01-25 12:10:11 +01001693 /* Pack LOD bias value */
1694 if (opcode == TGSI_OPCODE_TXB)
1695 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001696 if (opcode == TGSI_OPCODE_TXB2)
1697 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001698
Michel Dänzer120efee2013-01-25 12:10:11 +01001699 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001700 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001701 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1702 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1703 } else {
1704 assert(ref_pos >= 0);
1705 address[count++] = coords[ref_pos];
1706 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001707 }
1708
Marek Olšákb279f0142014-07-04 03:19:58 +02001709 if (target == TGSI_TEXTURE_CUBE ||
1710 target == TGSI_TEXTURE_CUBE_ARRAY ||
1711 target == TGSI_TEXTURE_SHADOWCUBE ||
1712 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1713 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1714
Michel Dänzera6b83c02013-02-21 16:10:55 +01001715 /* Pack user derivatives */
1716 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001717 int num_deriv_channels, param;
1718
1719 switch (target) {
1720 case TGSI_TEXTURE_3D:
1721 num_deriv_channels = 3;
1722 break;
1723 case TGSI_TEXTURE_2D:
1724 case TGSI_TEXTURE_SHADOW2D:
1725 case TGSI_TEXTURE_RECT:
1726 case TGSI_TEXTURE_SHADOWRECT:
1727 case TGSI_TEXTURE_2D_ARRAY:
1728 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1729 case TGSI_TEXTURE_CUBE:
1730 case TGSI_TEXTURE_SHADOWCUBE:
1731 case TGSI_TEXTURE_CUBE_ARRAY:
1732 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1733 num_deriv_channels = 2;
1734 break;
1735 case TGSI_TEXTURE_1D:
1736 case TGSI_TEXTURE_SHADOW1D:
1737 case TGSI_TEXTURE_1D_ARRAY:
1738 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1739 num_deriv_channels = 1;
1740 break;
1741 default:
1742 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001743 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001744
1745 for (param = 1; param <= 2; param++)
1746 for (chan = 0; chan < num_deriv_channels; chan++)
1747 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001748 }
1749
Michel Dänzer120efee2013-01-25 12:10:11 +01001750 /* Pack texture coordinates */
1751 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001752 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001753 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001754 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001755 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001756
Marek Olšákd2bd6342013-09-18 15:40:21 +02001757 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001758 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001759 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001760 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001761 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001762
1763 if (count > 16) {
1764 assert(!"Cannot handle more than 16 texture address parameters");
1765 count = 16;
1766 }
1767
1768 for (chan = 0; chan < count; chan++ ) {
1769 address[chan] = LLVMBuildBitCast(gallivm->builder,
1770 address[chan],
1771 LLVMInt32TypeInContext(gallivm->context),
1772 "");
1773 }
1774
Marek Olšák4855acd2013-08-06 15:08:54 +02001775 /* Adjust the sample index according to FMASK.
1776 *
1777 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1778 * which is the identity mapping. Each nibble says which physical sample
1779 * should be fetched to get that sample.
1780 *
1781 * For example, 0x11111100 means there are only 2 samples stored and
1782 * the second sample covers 3/4 of the pixel. When reading samples 0
1783 * and 1, return physical sample 0 (determined by the first two 0s
1784 * in FMASK), otherwise return physical sample 1.
1785 *
1786 * The sample index should be adjusted as follows:
1787 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1788 */
1789 if (target == TGSI_TEXTURE_2D_MSAA ||
1790 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1791 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1792 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001793 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001794 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001795 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001796
Marek Olšákd2bd6342013-09-18 15:40:21 +02001797 memcpy(txf_address, address, sizeof(txf_address));
1798
1799 if (target == TGSI_TEXTURE_2D_MSAA) {
1800 txf_address[2] = bld_base->uint_bld.zero;
1801 }
1802 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001803
1804 /* Pad to a power-of-two size. */
1805 while (txf_count < util_next_power_of_two(txf_count))
1806 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1807
1808 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001809 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1810 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1811 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001812 txf_emit_data.chan = 0;
1813 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001814 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001815 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001816 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001817 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001818 txf_emit_data.arg_count = 3;
1819
Marek Olšák877bb522014-06-25 03:12:46 +02001820 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001821
1822 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001823 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1824 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1825
1826 /* Apply the formula. */
1827 LLVMValueRef fmask =
1828 LLVMBuildExtractElement(gallivm->builder,
1829 txf_emit_data.output[0],
1830 uint_bld->zero, "");
1831
Marek Olšákd2bd6342013-09-18 15:40:21 +02001832 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001833
1834 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001835 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001836
1837 LLVMValueRef shifted_fmask =
1838 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1839
1840 LLVMValueRef final_sample =
1841 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1842
1843 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1844 * resource descriptor is 0 (invalid),
1845 */
1846 LLVMValueRef fmask_desc =
1847 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001848 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001849 LLVMVectorType(uint_bld->elem_type, 8), "");
1850
1851 LLVMValueRef fmask_word1 =
1852 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1853 uint_bld->one, "");
1854
1855 LLVMValueRef word1_is_nonzero =
1856 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1857 fmask_word1, uint_bld->zero, "");
1858
Marek Olšákd2bd6342013-09-18 15:40:21 +02001859 /* Replace the MSAA sample index. */
1860 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001861 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001862 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001863 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001864
Michel Dänzer36231112013-05-02 09:44:45 +02001865 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001866 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001867
1868 if (opcode == TGSI_OPCODE_TXF) {
1869 /* add tex offsets */
1870 if (inst->Texture.NumOffsets) {
1871 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1872 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1873 const struct tgsi_texture_offset * off = inst->TexOffsets;
1874
1875 assert(inst->Texture.NumOffsets == 1);
1876
Marek Olšákdefedc02013-09-18 15:36:38 +02001877 switch (target) {
1878 case TGSI_TEXTURE_3D:
1879 address[2] = lp_build_add(uint_bld, address[2],
1880 bld->immediates[off->Index][off->SwizzleZ]);
1881 /* fall through */
1882 case TGSI_TEXTURE_2D:
1883 case TGSI_TEXTURE_SHADOW2D:
1884 case TGSI_TEXTURE_RECT:
1885 case TGSI_TEXTURE_SHADOWRECT:
1886 case TGSI_TEXTURE_2D_ARRAY:
1887 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001888 address[1] =
1889 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001890 bld->immediates[off->Index][off->SwizzleY]);
1891 /* fall through */
1892 case TGSI_TEXTURE_1D:
1893 case TGSI_TEXTURE_SHADOW1D:
1894 case TGSI_TEXTURE_1D_ARRAY:
1895 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1896 address[0] =
1897 lp_build_add(uint_bld, address[0],
1898 bld->immediates[off->Index][off->SwizzleX]);
1899 break;
1900 /* texture offsets do not apply to other texture targets */
1901 }
Michel Dänzer36231112013-05-02 09:44:45 +02001902 }
1903
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001904 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1905 emit_data->arg_count = 3;
1906
Michel Dänzer36231112013-05-02 09:44:45 +02001907 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001908 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001909 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001910 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001911 opcode == TGSI_OPCODE_LODQ ||
1912 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001913 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1914 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1915 target == TGSI_TEXTURE_2D_ARRAY ||
1916 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1917 target == TGSI_TEXTURE_CUBE_ARRAY ||
1918 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1919 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001920 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001921
Marek Olšák57f3da92014-06-16 16:53:42 +02001922 if (opcode == TGSI_OPCODE_TG4) {
1923 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001924
Marek Olšák57f3da92014-06-16 16:53:42 +02001925 /* DMASK was repurposed for GATHER4. 4 components are always
1926 * returned and DMASK works like a swizzle - it selects
1927 * the component to fetch. The only valid DMASK values are
1928 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1929 * (red,red,red,red) etc.) The ISA document doesn't mention
1930 * this.
1931 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001932
Marek Olšák57f3da92014-06-16 16:53:42 +02001933 /* Get the component index from src1.x for Gather4. */
1934 if (!tgsi_is_shadow_sampler(target)) {
1935 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1936 LLVMValueRef comp_imm;
1937 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001938
Marek Olšák57f3da92014-06-16 16:53:42 +02001939 assert(src1.File == TGSI_FILE_IMMEDIATE);
1940
1941 comp_imm = imms[src1.Index][src1.SwizzleX];
1942 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1943 gather_comp = CLAMP(gather_comp, 0, 3);
1944 }
1945
1946 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001947 }
1948
Marek Olšák4855acd2013-08-06 15:08:54 +02001949 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001950 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001951 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1952 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1953 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1954 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1955 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1956 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1957 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1958
1959 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001960
1961 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001962 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001963 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001964 } else {
1965 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1966 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001967 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001968
1969 emit_data->dst_type = LLVMVectorType(
1970 LLVMFloatTypeInContext(gallivm->context),
1971 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001972 }
1973
Marek Olšák2484daa2014-04-22 21:23:29 +02001974 /* The fetch opcode has been converted to a 2D array fetch.
1975 * This simplifies the LLVM backend. */
1976 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1977 target = TGSI_TEXTURE_2D_ARRAY;
1978 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1979 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1980
Michel Dänzer120efee2013-01-25 12:10:11 +01001981 /* Pad to power of two vector */
1982 while (count < util_next_power_of_two(count))
1983 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1984
Christian Königccf3e8f2013-03-26 15:09:27 +01001985 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001986}
1987
Michel Dänzer07eddc42013-02-06 15:43:10 +01001988static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1989 struct lp_build_tgsi_context * bld_base,
1990 struct lp_build_emit_data * emit_data)
1991{
1992 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001993 unsigned opcode = emit_data->inst->Instruction.Opcode;
1994 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001995 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001996 bool has_offset = HAVE_LLVM >= 0x0305 ?
1997 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001998
Marek Olšák877bb522014-06-25 03:12:46 +02001999 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002000 emit_data->output[emit_data->chan] = build_intrinsic(
2001 base->gallivm->builder,
2002 "llvm.SI.vs.load.input", emit_data->dst_type,
2003 emit_data->args, emit_data->arg_count,
2004 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2005 return;
2006 }
2007
Marek Olšák877bb522014-06-25 03:12:46 +02002008 if (opcode == TGSI_OPCODE_TG4 ||
2009 opcode == TGSI_OPCODE_LODQ ||
2010 (opcode != TGSI_OPCODE_TXF && has_offset)) {
2011 bool is_shadow = tgsi_is_shadow_sampler(target);
2012 const char *name = "llvm.SI.image.sample";
2013 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01002014
Marek Olšák877bb522014-06-25 03:12:46 +02002015 switch (opcode) {
2016 case TGSI_OPCODE_TEX:
2017 case TGSI_OPCODE_TEX2:
2018 case TGSI_OPCODE_TXP:
2019 break;
2020 case TGSI_OPCODE_TXB:
2021 case TGSI_OPCODE_TXB2:
2022 infix = ".b";
2023 break;
2024 case TGSI_OPCODE_TXL:
2025 case TGSI_OPCODE_TXL2:
2026 infix = ".l";
2027 break;
2028 case TGSI_OPCODE_TXD:
2029 infix = ".d";
2030 break;
2031 case TGSI_OPCODE_TG4:
2032 name = "llvm.SI.gather4";
2033 break;
2034 case TGSI_OPCODE_LODQ:
2035 name = "llvm.SI.getlod";
2036 is_shadow = false;
2037 has_offset = false;
2038 break;
2039 default:
2040 assert(0);
2041 return;
2042 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01002043
Marek Olšák877bb522014-06-25 03:12:46 +02002044 /* Add the type and suffixes .c, .o if needed. */
2045 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
2046 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
2047 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002048
Marek Olšák877bb522014-06-25 03:12:46 +02002049 emit_data->output[emit_data->chan] = build_intrinsic(
2050 base->gallivm->builder, intr_name, emit_data->dst_type,
2051 emit_data->args, emit_data->arg_count,
2052 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2053 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02002054 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02002055 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002056
Marek Olšák877bb522014-06-25 03:12:46 +02002057 switch (opcode) {
2058 case TGSI_OPCODE_TEX:
2059 case TGSI_OPCODE_TEX2:
2060 case TGSI_OPCODE_TXP:
2061 name = "llvm.SI.sample";
2062 break;
2063 case TGSI_OPCODE_TXB:
2064 case TGSI_OPCODE_TXB2:
2065 name = "llvm.SI.sampleb";
2066 break;
2067 case TGSI_OPCODE_TXD:
2068 name = "llvm.SI.sampled";
2069 break;
2070 case TGSI_OPCODE_TXF:
2071 name = "llvm.SI.imageload";
2072 break;
2073 case TGSI_OPCODE_TXL:
2074 case TGSI_OPCODE_TXL2:
2075 name = "llvm.SI.samplel";
2076 break;
2077 default:
2078 assert(0);
2079 return;
2080 }
2081
Marek Olšákd859bdb2014-07-14 19:40:14 +02002082 i8 = LLVMInt8TypeInContext(base->gallivm->context);
2083 v16i8 = LLVMVectorType(i8, 16);
2084 v32i8 = LLVMVectorType(i8, 32);
2085
2086 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
2087 emit_data->args[1], v32i8, "");
2088 if (opcode != TGSI_OPCODE_TXF) {
2089 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
2090 emit_data->args[2], v16i8, "");
2091 }
2092
Marek Olšák877bb522014-06-25 03:12:46 +02002093 sprintf(intr_name, "%s.v%ui32", name,
2094 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
2095
2096 emit_data->output[emit_data->chan] = build_intrinsic(
2097 base->gallivm->builder, intr_name, emit_data->dst_type,
2098 emit_data->args, emit_data->arg_count,
2099 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2100 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002101}
2102
Michel Dänzer0495adb2013-05-06 12:45:14 +02002103static void txq_fetch_args(
2104 struct lp_build_tgsi_context * bld_base,
2105 struct lp_build_emit_data * emit_data)
2106{
2107 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2108 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002109 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002110 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002111
Marek Olšák2484daa2014-04-22 21:23:29 +02002112 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002113 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2114 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2115
2116 /* Read the size from the buffer descriptor directly. */
2117 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2118 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2119 size = LLVMBuildExtractElement(gallivm->builder, size,
2120 lp_build_const_int32(gallivm, 2), "");
2121 emit_data->args[0] = size;
2122 return;
2123 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002124
2125 /* Mip level */
2126 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2127
2128 /* Resource */
2129 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2130
Marek Olšák2484daa2014-04-22 21:23:29 +02002131 /* Texture target */
2132 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2133 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2134 target = TGSI_TEXTURE_2D_ARRAY;
2135
Michel Dänzer0495adb2013-05-06 12:45:14 +02002136 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002137 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002138
2139 emit_data->arg_count = 3;
2140
2141 emit_data->dst_type = LLVMVectorType(
2142 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2143 4);
2144}
2145
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002146static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2147 struct lp_build_tgsi_context * bld_base,
2148 struct lp_build_emit_data * emit_data)
2149{
Marek Olšák2484daa2014-04-22 21:23:29 +02002150 unsigned target = emit_data->inst->Texture.Texture;
2151
2152 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002153 /* Just return the buffer size. */
2154 emit_data->output[emit_data->chan] = emit_data->args[0];
2155 return;
2156 }
2157
2158 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002159
2160 /* Divide the number of layers by 6 to get the number of cubes. */
2161 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2162 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2163 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2164 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2165 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2166
2167 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2168 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2169 z = LLVMBuildSDiv(builder, z, six, "");
2170
2171 emit_data->output[emit_data->chan] =
2172 LLVMBuildInsertElement(builder, v4, z, two, "");
2173 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002174}
2175
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002176static void si_llvm_emit_ddxy(
2177 const struct lp_build_tgsi_action * action,
2178 struct lp_build_tgsi_context * bld_base,
2179 struct lp_build_emit_data * emit_data)
2180{
2181 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2182 struct gallivm_state *gallivm = bld_base->base.gallivm;
2183 struct lp_build_context * base = &bld_base->base;
2184 const struct tgsi_full_instruction *inst = emit_data->inst;
2185 unsigned opcode = inst->Instruction.Opcode;
2186 LLVMValueRef indices[2];
2187 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2188 LLVMValueRef tl, trbl, result[4];
2189 LLVMTypeRef i32;
2190 unsigned swizzle[4];
2191 unsigned c;
2192
2193 i32 = LLVMInt32TypeInContext(gallivm->context);
2194
2195 indices[0] = bld_base->uint_bld.zero;
2196 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2197 NULL, 0, LLVMReadNoneAttribute);
2198 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2199 indices, 2, "");
2200
2201 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2202 lp_build_const_int32(gallivm, 0xfffffffc), "");
2203 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2204 indices, 2, "");
2205
2206 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2207 lp_build_const_int32(gallivm,
2208 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2209 "");
2210 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2211 indices, 2, "");
2212
2213 for (c = 0; c < 4; ++c) {
2214 unsigned i;
2215
2216 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2217 for (i = 0; i < c; ++i) {
2218 if (swizzle[i] == swizzle[c]) {
2219 result[c] = result[i];
2220 break;
2221 }
2222 }
2223 if (i != c)
2224 continue;
2225
2226 LLVMBuildStore(gallivm->builder,
2227 LLVMBuildBitCast(gallivm->builder,
2228 lp_build_emit_fetch(bld_base, inst, 0, c),
2229 i32, ""),
2230 store_ptr);
2231
2232 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2233 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2234
2235 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2236 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2237
2238 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2239 }
2240
2241 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2242}
2243
Michel Dänzer404b29d2013-11-21 16:45:28 +09002244/* Emit one vertex from the geometry shader */
2245static void si_llvm_emit_vertex(
2246 const struct lp_build_tgsi_action *action,
2247 struct lp_build_tgsi_context *bld_base,
2248 struct lp_build_emit_data *emit_data)
2249{
2250 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002251 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002252 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002253 struct gallivm_state *gallivm = bld_base->base.gallivm;
2254 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002255 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2256 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002257 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002258 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002259 LLVMValueRef t_list_ptr;
2260 LLVMValueRef t_list;
2261 LLVMValueRef args[2];
2262 unsigned chan;
2263 int i;
2264
2265 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002266 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2267 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002268 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002269 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002270
2271 if (shader->noutput == 0) {
2272 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
2273
2274 while (!tgsi_parse_end_of_tokens(parse)) {
2275 tgsi_parse_token(parse);
2276
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002277 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
2278 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
2279
2280 if (d->Declaration.File == TGSI_FILE_OUTPUT)
2281 si_store_shader_io_attribs(shader, d);
2282 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09002283 }
2284 }
2285
2286 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002287 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002288
2289 /* If this thread has already emitted the declared maximum number of
2290 * vertices, kill it: excessive vertex emissions are not supposed to
2291 * have any effect, and GS threads have no externally observable
2292 * effects other than emitting vertices.
2293 */
2294 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2295 lp_build_const_int32(gallivm,
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002296 shader->selector->gs_max_out_vertices), "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002297 kill = lp_build_select(&bld_base->base, can_emit,
2298 lp_build_const_float(gallivm, 1.0f),
2299 lp_build_const_float(gallivm, -1.0f));
2300 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2301 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2302
Michel Dänzer404b29d2013-11-21 16:45:28 +09002303 for (i = 0; i < shader->noutput; i++) {
2304 LLVMValueRef *out_ptr =
2305 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
2306
2307 for (chan = 0; chan < 4; chan++) {
2308 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002309 LLVMValueRef voffset =
2310 lp_build_const_int32(gallivm, (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002311 shader->selector->gs_max_out_vertices);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002312
2313 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2314 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002315
2316 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2317
2318 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2319 voffset, soffset, 0,
2320 V_008F0C_BUF_DATA_FORMAT_32,
2321 V_008F0C_BUF_NUM_FORMAT_UINT,
2322 1, 0, 1, 1, 0);
2323 }
2324 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002325 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2326 lp_build_const_int32(gallivm, 1));
2327 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002328
2329 /* Signal vertex emission */
2330 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2331 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2332 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2333 LLVMVoidTypeInContext(gallivm->context), args, 2,
2334 LLVMNoUnwindAttribute);
2335}
2336
2337/* Cut one primitive from the geometry shader */
2338static void si_llvm_emit_primitive(
2339 const struct lp_build_tgsi_action *action,
2340 struct lp_build_tgsi_context *bld_base,
2341 struct lp_build_emit_data *emit_data)
2342{
2343 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2344 struct gallivm_state *gallivm = bld_base->base.gallivm;
2345 LLVMValueRef args[2];
2346
2347 /* Signal primitive cut */
2348 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2349 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2350 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2351 LLVMVoidTypeInContext(gallivm->context), args, 2,
2352 LLVMNoUnwindAttribute);
2353}
2354
Tom Stellarda75c6162012-01-06 17:38:37 -05002355static const struct lp_build_tgsi_action tex_action = {
2356 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002357 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002358};
2359
Michel Dänzer0495adb2013-05-06 12:45:14 +02002360static const struct lp_build_tgsi_action txq_action = {
2361 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002362 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002363 .intr_name = "llvm.SI.resinfo"
2364};
2365
Christian König206f0592013-03-20 14:37:21 +01002366static void create_meta_data(struct si_shader_context *si_shader_ctx)
2367{
2368 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2369 LLVMValueRef args[3];
2370
2371 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2372 args[1] = 0;
2373 args[2] = lp_build_const_int32(gallivm, 1);
2374
2375 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2376}
2377
Marek Olšák4f3f0432014-07-07 22:49:15 +02002378static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2379{
2380 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2381 CONST_ADDR_SPACE);
2382}
2383
Christian König55fe5cc2013-03-04 16:30:06 +01002384static void create_function(struct si_shader_context *si_shader_ctx)
2385{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002386 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2387 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002388 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002389 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšákaeb05f02014-09-30 18:13:06 +02002390 unsigned i, last_array_pointer, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002391
Christian König55fe5cc2013-03-04 16:30:06 +01002392 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002393 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002394 f32 = LLVMFloatTypeInContext(gallivm->context);
2395 v2i32 = LLVMVectorType(i32, 2);
2396 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002397 v4i32 = LLVMVectorType(i32, 4);
2398 v8i32 = LLVMVectorType(i32, 8);
2399 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002400
Marek Olšákee2a8182014-07-07 23:27:19 +02002401 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002402 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
Marek Olšákee2a8182014-07-07 23:27:19 +02002403 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2404 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002405 last_array_pointer = SI_PARAM_RESOURCE;
Christian König55fe5cc2013-03-04 16:30:06 +01002406
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002407 switch (si_shader_ctx->type) {
2408 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002409 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002410 last_array_pointer = SI_PARAM_VERTEX_BUFFER;
Marek Olšák09056b32014-04-23 16:15:36 +02002411 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002412 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002413 num_params = SI_PARAM_START_INSTANCE+1;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002414
Michel Dänzer404b29d2013-11-21 16:45:28 +09002415 if (shader->key.vs.as_es) {
2416 params[SI_PARAM_ES2GS_OFFSET] = i32;
2417 num_params++;
2418 } else {
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002419 if (shader->is_gs_copy_shader) {
2420 last_array_pointer = SI_PARAM_CONST;
2421 num_params = SI_PARAM_CONST+1;
2422 }
2423
Michel Dänzer404b29d2013-11-21 16:45:28 +09002424 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002425
Michel Dänzer404b29d2013-11-21 16:45:28 +09002426 /* Streamout SGPRs. */
2427 if (shader->selector->so.num_outputs) {
2428 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2429 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2430 }
2431 /* A streamout buffer offset is loaded if the stride is non-zero. */
2432 for (i = 0; i < 4; i++) {
2433 if (!shader->selector->so.stride[i])
2434 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002435
Michel Dänzer404b29d2013-11-21 16:45:28 +09002436 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2437 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002438 }
2439
2440 last_sgpr = num_params-1;
2441
2442 /* VGPRs */
2443 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2444 params[num_params++] = i32; /* unused*/
2445 params[num_params++] = i32; /* unused */
2446 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002447 break;
Christian König0666ffd2013-03-05 15:07:39 +01002448
Michel Dänzer404b29d2013-11-21 16:45:28 +09002449 case TGSI_PROCESSOR_GEOMETRY:
2450 params[SI_PARAM_GS2VS_OFFSET] = i32;
2451 params[SI_PARAM_GS_WAVE_ID] = i32;
2452 last_sgpr = SI_PARAM_GS_WAVE_ID;
2453
2454 /* VGPRs */
2455 params[SI_PARAM_VTX0_OFFSET] = i32;
2456 params[SI_PARAM_VTX1_OFFSET] = i32;
2457 params[SI_PARAM_PRIMITIVE_ID] = i32;
2458 params[SI_PARAM_VTX2_OFFSET] = i32;
2459 params[SI_PARAM_VTX3_OFFSET] = i32;
2460 params[SI_PARAM_VTX4_OFFSET] = i32;
2461 params[SI_PARAM_VTX5_OFFSET] = i32;
2462 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2463 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2464 break;
2465
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002466 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002467 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002468 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002469 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002470 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2471 params[SI_PARAM_PERSP_CENTER] = v2i32;
2472 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2473 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2474 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2475 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2476 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2477 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2478 params[SI_PARAM_POS_X_FLOAT] = f32;
2479 params[SI_PARAM_POS_Y_FLOAT] = f32;
2480 params[SI_PARAM_POS_Z_FLOAT] = f32;
2481 params[SI_PARAM_POS_W_FLOAT] = f32;
2482 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002483 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002484 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2485 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002486 num_params = SI_PARAM_POS_FIXED_PT+1;
2487 break;
2488
2489 default:
2490 assert(0 && "unimplemented shader");
2491 return;
Christian Königc4973212013-03-05 12:14:02 +01002492 }
Christian König55fe5cc2013-03-04 16:30:06 +01002493
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002494 assert(num_params <= Elements(params));
2495 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002496 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002497
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002498 for (i = 0; i <= last_sgpr; ++i) {
2499 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002500
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002501 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2502 * to move load. Inputs are constant so this is fine. */
Marek Olšákaeb05f02014-09-30 18:13:06 +02002503 if (i <= last_array_pointer)
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002504 LLVMAddAttribute(P, LLVMByValAttribute);
Marek Olšákaeb05f02014-09-30 18:13:06 +02002505 else
2506 LLVMAddAttribute(P, LLVMInRegAttribute);
Christian Königcf9b31f2013-03-21 18:30:23 +01002507 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002508
Michel Dänzer404b29d2013-11-21 16:45:28 +09002509 if (bld_base->info &&
2510 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2511 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002512 si_shader_ctx->ddxy_lds =
2513 LLVMAddGlobalInAddressSpace(gallivm->module,
2514 LLVMArrayType(i32, 64),
2515 "ddxy_lds",
2516 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002517}
Tom Stellarda75c6162012-01-06 17:38:37 -05002518
Christian König0f6cf2b2013-03-15 15:53:25 +01002519static void preload_constants(struct si_shader_context *si_shader_ctx)
2520{
2521 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2522 struct gallivm_state * gallivm = bld_base->base.gallivm;
2523 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002524 unsigned buf;
2525 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002526
Marek Olšákee2a8182014-07-07 23:27:19 +02002527 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002528 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002529
Marek Olšák2fd42002013-10-25 11:45:47 +02002530 if (num_const == 0)
2531 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002532
Marek Olšák2fd42002013-10-25 11:45:47 +02002533 /* Allocate space for the constant values */
2534 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002535
Marek Olšák2fd42002013-10-25 11:45:47 +02002536 /* Load the resource descriptor */
2537 si_shader_ctx->const_resource[buf] =
2538 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002539
Marek Olšák2fd42002013-10-25 11:45:47 +02002540 /* Load the constants, we rely on the code sinking to do the rest */
2541 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002542 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002543 load_const(gallivm->builder,
2544 si_shader_ctx->const_resource[buf],
2545 lp_build_const_int32(gallivm, i * 4),
2546 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002547 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002548 }
2549}
2550
Christian König1c100182013-03-17 16:02:42 +01002551static void preload_samplers(struct si_shader_context *si_shader_ctx)
2552{
2553 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2554 struct gallivm_state * gallivm = bld_base->base.gallivm;
2555 const struct tgsi_shader_info * info = bld_base->info;
2556
2557 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2558
2559 LLVMValueRef res_ptr, samp_ptr;
2560 LLVMValueRef offset;
2561
2562 if (num_samplers == 0)
2563 return;
2564
2565 /* Allocate space for the values */
Marek Olšákee2a8182014-07-07 23:27:19 +02002566 si_shader_ctx->resources = CALLOC(SI_NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002567 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2568
2569 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2570 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2571
2572 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2573 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002574 /* Resource */
2575 offset = lp_build_const_int32(gallivm, i);
2576 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2577
2578 /* Sampler */
2579 offset = lp_build_const_int32(gallivm, i);
2580 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002581
2582 /* FMASK resource */
2583 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002584 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2585 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšák4855acd2013-08-06 15:08:54 +02002586 build_indexed_load(si_shader_ctx, res_ptr, offset);
2587 }
Christian König1c100182013-03-17 16:02:42 +01002588 }
2589}
2590
Marek Olšák8d03d922013-09-01 23:59:06 +02002591static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2592{
2593 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2594 struct gallivm_state * gallivm = bld_base->base.gallivm;
2595 unsigned i;
2596
Michel Dänzer67e385b2014-01-08 17:48:21 +09002597 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2598 si_shader_ctx->shader->key.vs.as_es ||
2599 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002600 return;
2601
2602 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002603 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002604
2605 /* Load the resources, we rely on the code sinking to do the rest */
2606 for (i = 0; i < 4; ++i) {
2607 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002608 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002609 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002610
2611 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2612 }
2613 }
2614}
2615
Marek Olšák1abb1a92014-09-17 22:44:22 +02002616int si_compile_llvm(struct si_screen *sscreen, struct si_shader *shader,
2617 LLVMModuleRef mod)
Tom Stellard302f53d2012-10-25 13:50:10 -04002618{
Darren Powellbc866902014-03-31 18:00:28 -04002619 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002620 unsigned i;
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002621 unsigned char *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002622 struct radeon_shader_binary binary;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002623 bool dump = r600_can_dump_shader(&sscreen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002624 shader->selector ? shader->selector->tokens : NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002625 const char * gpu_family = r600_get_llvm_processor_name(sscreen->b.family);
Tom Stellard9ba31052014-07-14 16:49:08 -04002626 unsigned code_size;
Darren Powellbc866902014-03-31 18:00:28 -04002627
2628 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002629 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002630 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2631
2632 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002633 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002634 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002635 for (i = 0; i < binary.code_size; i+=4 ) {
2636 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2637 binary.code[i + 2], binary.code[i + 1],
2638 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002639 }
2640 }
2641
Tom Stellardd50343d2013-04-04 16:21:06 -04002642 /* XXX: We may be able to emit some of these values directly rather than
2643 * extracting fields to be emitted later.
2644 */
Darren Powellbc866902014-03-31 18:00:28 -04002645 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002646 for (i = 0; i < binary.config_size; i+= 8) {
2647 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2648 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2649 switch (reg) {
2650 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2651 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2652 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2653 case R_00B848_COMPUTE_PGM_RSRC1:
2654 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2655 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2656 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002657 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2658 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2659 break;
2660 case R_00B84C_COMPUTE_PGM_RSRC2:
2661 shader->lds_size = G_00B84C_LDS_SIZE(value);
2662 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002663 case R_0286CC_SPI_PS_INPUT_ENA:
2664 shader->spi_ps_input_ena = value;
2665 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002666 case R_00B860_COMPUTE_TMPRING_SIZE:
2667 /* WAVESIZE is in units of 256 dwords. */
2668 shader->scratch_bytes_per_wave =
2669 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2670 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002671 default:
2672 fprintf(stderr, "Warning: Compiler emitted unknown "
2673 "config register: 0x%x\n", reg);
2674 break;
2675 }
2676 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002677
2678 /* copy new shader */
Tom Stellard9ba31052014-07-14 16:49:08 -04002679 code_size = binary.code_size + binary.rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002680 r600_resource_reference(&shader->bo, NULL);
Marek Olšák1abb1a92014-09-17 22:44:22 +02002681 shader->bo = si_resource_create_custom(&sscreen->b.b, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002682 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002683 if (shader->bo == NULL) {
2684 return -ENOMEM;
2685 }
2686
Marek Olšák1abb1a92014-09-17 22:44:22 +02002687 ptr = sscreen->b.ws->buffer_map(shader->bo->cs_buf, NULL, PIPE_TRANSFER_WRITE);
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002688 util_memcpy_cpu_to_le32(ptr, binary.code, binary.code_size);
2689 if (binary.rodata_size > 0) {
2690 ptr += binary.code_size;
2691 util_memcpy_cpu_to_le32(ptr, binary.rodata, binary.rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002692 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002693
Marek Olšák1abb1a92014-09-17 22:44:22 +02002694 sscreen->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002695
Tom Stellard7782d192013-04-04 09:57:13 -07002696 free(binary.code);
2697 free(binary.config);
Tom Stellard9ba31052014-07-14 16:49:08 -04002698 free(binary.rodata);
Tom Stellard302f53d2012-10-25 13:50:10 -04002699
Darren Powellbc866902014-03-31 18:00:28 -04002700 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002701}
2702
Michel Dänzer404b29d2013-11-21 16:45:28 +09002703/* Generate code for the hardware VS shader stage to go with a geometry shader */
Marek Olšák1abb1a92014-09-17 22:44:22 +02002704static int si_generate_gs_copy_shader(struct si_screen *sscreen,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002705 struct si_shader_context *si_shader_ctx,
Marek Olšák68d36c02014-09-30 18:15:17 +02002706 struct si_shader *gs, bool dump)
Michel Dänzer404b29d2013-11-21 16:45:28 +09002707{
2708 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2709 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2710 struct lp_build_context *base = &bld_base->base;
2711 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002712 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002713 struct si_shader_output_values *outputs;
2714 LLVMValueRef t_list_ptr, t_list;
2715 LLVMValueRef args[9];
2716 int i, r;
2717
2718 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2719
2720 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
Marek Olšák1f6c0b52014-09-23 19:42:28 +02002721 shader->is_gs_copy_shader = true;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002722
2723 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2724
2725 create_meta_data(si_shader_ctx);
2726 create_function(si_shader_ctx);
2727 preload_streamout_buffers(si_shader_ctx);
2728
2729 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002730 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2731 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002732 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002733 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002734
2735 args[0] = t_list;
2736 args[1] = lp_build_mul_imm(uint,
2737 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2738 si_shader_ctx->param_vertex_id),
2739 4);
2740 args[3] = uint->zero;
2741 args[4] = uint->one; /* OFFEN */
2742 args[5] = uint->zero; /* IDXEN */
2743 args[6] = uint->one; /* GLC */
2744 args[7] = uint->one; /* SLC */
2745 args[8] = uint->zero; /* TFE */
2746
2747 /* Fetch vertex data from GSVS ring */
2748 for (i = 0; i < gs->noutput; ++i) {
2749 struct si_shader_output *out = gs->output + i;
2750 unsigned chan;
2751
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002752 shader->output[i] = *out;
2753
Michel Dänzer404b29d2013-11-21 16:45:28 +09002754 outputs[i].name = out->name;
2755 outputs[i].index = out->index;
Michel Dänzer67e385b2014-01-08 17:48:21 +09002756 outputs[i].sid = out->sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002757
2758 for (chan = 0; chan < 4; chan++) {
2759 args[2] = lp_build_const_int32(gallivm,
2760 (i * 4 + chan) *
Marek Olšák0a2d6f02014-09-30 16:55:36 +02002761 gs->selector->gs_max_out_vertices * 16 * 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002762
2763 outputs[i].values[chan] =
2764 LLVMBuildBitCast(gallivm->builder,
2765 build_intrinsic(gallivm->builder,
2766 "llvm.SI.buffer.load.dword.i32.i32",
2767 LLVMInt32TypeInContext(gallivm->context),
2768 args, 9,
2769 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2770 base->elem_type, "");
2771 }
2772 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002773 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002774
2775 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2776
2777 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2778
2779 if (dump)
2780 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2781
Marek Olšák1abb1a92014-09-17 22:44:22 +02002782 r = si_compile_llvm(sscreen, si_shader_ctx->shader,
Michel Dänzer404b29d2013-11-21 16:45:28 +09002783 bld_base->base.gallivm->module);
2784
2785 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2786
2787 FREE(outputs);
2788 return r;
2789}
2790
Marek Olšák1abb1a92014-09-17 22:44:22 +02002791int si_shader_create(struct si_screen *sscreen, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002792{
Marek Olšák2774abd2014-09-16 18:45:33 +02002793 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002794 struct si_shader_context si_shader_ctx;
Tom Stellarda75c6162012-01-06 17:38:37 -05002795 struct lp_build_tgsi_context * bld_base;
2796 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002797 int r = 0;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002798 bool dump = r600_can_dump_shader(&sscreen->b, sel->tokens);
Michel Dänzere1df0d42014-01-15 12:31:07 +09002799
2800 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2801 * conversion fails. */
2802 if (dump) {
2803 tgsi_dump(sel->tokens, 0);
2804 si_dump_streamout(&sel->so);
2805 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002806
Marek Olšák8c37c162014-09-16 18:40:07 +02002807 assert(shader->noutput == 0);
2808 assert(shader->nparam == 0);
2809 assert(shader->ninput == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002810
Michel Dänzercfebaf92012-08-31 19:04:08 +02002811 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002812 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2813 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2814
Marek Olšák52335682014-09-30 15:12:09 +02002815 if (sel->info.uses_kill)
Marek Olšákadc57972014-09-19 17:07:07 +02002816 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2817
Marek Olšák52335682014-09-30 15:12:09 +02002818 shader->uses_instanceid = sel->info.uses_instanceid;
2819 bld_base->info = &sel->info;
Tom Stellarda75c6162012-01-06 17:38:37 -05002820 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002821
2822 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002823 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002824 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2825 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002826 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002827 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2828 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2829 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002830 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002831 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002832 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2833 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002834
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002835 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2836 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002837
Michel Dänzer404b29d2013-11-21 16:45:28 +09002838 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2839 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2840
Christian Könige4ed5872013-03-21 18:02:52 +01002841 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002842 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002843 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2844 si_shader_ctx.shader = shader;
2845 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002846
Michel Dänzer51f89a02013-12-09 15:33:53 +09002847 switch (si_shader_ctx.type) {
2848 case TGSI_PROCESSOR_VERTEX:
2849 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002850 if (shader->key.vs.as_es) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002851 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2852 } else {
2853 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2854 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002855 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002856 case TGSI_PROCESSOR_GEOMETRY:
Michel Dänzer404b29d2013-11-21 16:45:28 +09002857 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2858 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2859 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002860 break;
Marek Olšák8908fae2014-09-30 15:48:22 +02002861 case TGSI_PROCESSOR_FRAGMENT:
Michel Dänzer51f89a02013-12-09 15:33:53 +09002862 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2863 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002864
Marek Olšák0c4bc1e2014-10-02 16:36:51 +02002865 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
Marek Olšák8908fae2014-09-30 15:48:22 +02002866 case TGSI_FS_DEPTH_LAYOUT_GREATER:
2867 shader->db_shader_control |=
2868 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
2869 break;
2870 case TGSI_FS_DEPTH_LAYOUT_LESS:
2871 shader->db_shader_control |=
2872 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
2873 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002874 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002875 break;
2876 default:
2877 assert(!"Unsupported shader type");
2878 return -1;
2879 }
2880
Christian König206f0592013-03-20 14:37:21 +01002881 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002882 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002883 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002884 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002885 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002886
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002887 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2888 si_shader_ctx.gs_next_vertex =
2889 lp_build_alloca(bld_base->base.gallivm,
2890 bld_base->uint_bld.elem_type, "");
2891 }
2892
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002893 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002894 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002895 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002896 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002897
2898 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2899
2900 mod = bld_base->base.gallivm->module;
Marek Olšák1abb1a92014-09-17 22:44:22 +02002901 r = si_compile_llvm(sscreen, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002902 if (r) {
2903 fprintf(stderr, "LLVM failed to compile shader\n");
2904 goto out;
2905 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002906
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002907 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002908
2909 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002910 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002911 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002912 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002913 si_shader_ctx.shader = shader->gs_copy_shader;
Marek Olšák68d36c02014-09-30 18:15:17 +02002914 if ((r = si_generate_gs_copy_shader(sscreen, &si_shader_ctx,
2915 shader, dump))) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09002916 free(shader->gs_copy_shader);
2917 shader->gs_copy_shader = NULL;
2918 goto out;
2919 }
2920 }
2921
Tom Stellarda75c6162012-01-06 17:38:37 -05002922 tgsi_parse_free(&si_shader_ctx.parse);
2923
Michel Dänzer404b29d2013-11-21 16:45:28 +09002924out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002925 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002926 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002927 FREE(si_shader_ctx.resources);
2928 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002929
Tom Stellard302f53d2012-10-25 13:50:10 -04002930 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002931}
2932
Marek Olšák2774abd2014-09-16 18:45:33 +02002933void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002934{
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002935 if (shader->gs_copy_shader)
2936 si_shader_destroy(ctx, shader->gs_copy_shader);
2937
Marek Olšáka81c3e02013-08-14 01:04:39 +02002938 r600_resource_reference(&shader->bo, NULL);
Marek Olšákdc05a9e2014-09-18 23:48:04 +02002939 r600_resource_reference(&shader->scratch_bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002940}