blob: 2fc1632da2bf56266910302ed8a7ab0c2f90ec9c [file] [log] [blame]
Christian Königce40e472012-08-02 12:14:59 +02001/*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
27 */
28
Tom Stellarda75c6162012-01-06 17:38:37 -050029#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020030#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050031#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010032#include "gallivm/lp_bld_logic.h"
Christian König5e616cf2013-03-07 11:58:56 +010033#include "gallivm/lp_bld_arit.h"
Marek Olšák8d03d922013-09-01 23:59:06 +020034#include "gallivm/lp_bld_flow.h"
Emil Velikova1312632014-08-16 17:58:25 +010035#include "radeon/radeon_llvm.h"
36#include "radeon/radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010037#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010039#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_dump.h"
41
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010042#include "si_pipe.h"
43#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050044#include "sid.h"
45
Tom Stellarda75c6162012-01-06 17:38:37 -050046#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050047
Michel Dänzer404b29d2013-11-21 16:45:28 +090048struct si_shader_output_values
49{
50 LLVMValueRef values[4];
51 unsigned name;
52 unsigned index;
Michel Dänzer67e385b2014-01-08 17:48:21 +090053 unsigned sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +090054 unsigned usage;
55};
56
Tom Stellarda75c6162012-01-06 17:38:37 -050057struct si_shader_context
58{
59 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050060 struct tgsi_parse_context parse;
61 struct tgsi_token * tokens;
Marek Olšák8c37c162014-09-16 18:40:07 +020062 struct si_shader *shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +090063 struct si_shader *gs_for_vs;
Tom Stellarda75c6162012-01-06 17:38:37 -050064 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020065 int param_streamout_config;
66 int param_streamout_write_index;
67 int param_streamout_offset[4];
68 int param_vertex_id;
69 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010070 LLVMValueRef const_md;
Marek Olšákee2a8182014-07-07 23:27:19 +020071 LLVMValueRef const_resource[SI_NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020072 LLVMValueRef ddxy_lds;
Marek Olšákee2a8182014-07-07 23:27:19 +020073 LLVMValueRef *constants[SI_NUM_CONST_BUFFERS];
Christian König1c100182013-03-17 16:02:42 +010074 LLVMValueRef *resources;
75 LLVMValueRef *samplers;
Marek Olšák8d03d922013-09-01 23:59:06 +020076 LLVMValueRef so_buffers[4];
Michel Dänzerf07a96d2014-01-08 18:45:10 +090077 LLVMValueRef gs_next_vertex;
Tom Stellarda75c6162012-01-06 17:38:37 -050078};
79
80static struct si_shader_context * si_shader_context(
81 struct lp_build_tgsi_context * bld_base)
82{
83 return (struct si_shader_context *)bld_base;
84}
85
86
87#define PERSPECTIVE_BASE 0
88#define LINEAR_BASE 9
89
90#define SAMPLE_OFFSET 0
91#define CENTER_OFFSET 2
92#define CENTROID_OFSET 4
93
94#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040095#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020096#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040097#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050098
Michel Dänzer404b29d2013-11-21 16:45:28 +090099
100#define SENDMSG_GS 2
101#define SENDMSG_GS_DONE 3
102
103#define SENDMSG_GS_OP_NOP (0 << 4)
104#define SENDMSG_GS_OP_CUT (1 << 4)
105#define SENDMSG_GS_OP_EMIT (2 << 4)
106#define SENDMSG_GS_OP_EMIT_CUT (3 << 4)
107
108
Tom Stellard467f5162012-05-16 15:15:35 -0400109/**
110 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
111 *
112 * @param offset The offset parameter specifies the number of
113 * elements to offset, not the number of bytes or dwords. An element is the
114 * the type pointed to by the base_ptr parameter (e.g. int is the element of
115 * an int* pointer)
116 *
117 * When LLVM lowers the load instruction, it will convert the element offset
118 * into a dword offset automatically.
119 *
120 */
121static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100122 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400123 LLVMValueRef base_ptr,
124 LLVMValueRef offset)
125{
Christian König206f0592013-03-20 14:37:21 +0100126 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400127
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200128 LLVMValueRef indices[2] = {
129 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
130 offset
131 };
Christian König206f0592013-03-20 14:37:21 +0100132 LLVMValueRef computed_ptr = LLVMBuildGEP(
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200133 base->gallivm->builder, base_ptr, indices, 2, "");
Christian König206f0592013-03-20 14:37:21 +0100134
135 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
136 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
137 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400138}
139
Marek Olšákf317ce52013-09-05 15:39:57 +0200140static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100141 struct radeon_llvm_context * radeon_bld,
142 unsigned divisor)
143{
Marek Olšák8d03d922013-09-01 23:59:06 +0200144 struct si_shader_context *si_shader_ctx =
145 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100146 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
147
Marek Olšák8d03d922013-09-01 23:59:06 +0200148 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
149 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100150 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
151 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
152
153 if (divisor > 1)
154 result = LLVMBuildUDiv(gallivm->builder, result,
155 lp_build_const_int32(gallivm, divisor), "");
156
157 return result;
158}
159
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900160static int si_store_shader_io_attribs(struct si_shader *shader,
161 const struct tgsi_full_declaration *d)
162{
163 int i = -1;
164
165 switch (d->Declaration.File) {
166 case TGSI_FILE_INPUT:
167 i = shader->ninput++;
168 assert(i < Elements(shader->input));
169 shader->input[i].name = d->Semantic.Name;
170 shader->input[i].sid = d->Semantic.Index;
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900171 shader->input[i].index = d->Range.First;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900172 shader->input[i].interpolate = d->Interp.Interpolate;
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400173 shader->input[i].centroid = d->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900174 return -1;
175
176 case TGSI_FILE_OUTPUT:
177 i = shader->noutput++;
178 assert(i < Elements(shader->output));
179 shader->output[i].name = d->Semantic.Name;
180 shader->output[i].sid = d->Semantic.Index;
181 shader->output[i].index = d->Range.First;
182 shader->output[i].usage = d->Declaration.UsageMask;
183 break;
184 }
185
186 return i;
187}
188
Tom Stellarda75c6162012-01-06 17:38:37 -0500189static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900190 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500191 unsigned input_index,
192 const struct tgsi_full_declaration *decl)
193{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900194 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
195 struct gallivm_state *gallivm = base->gallivm;
196 struct si_shader_context *si_shader_ctx =
197 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100198 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
199
200 unsigned chan;
201
Tom Stellarda75c6162012-01-06 17:38:37 -0500202 LLVMValueRef t_list_ptr;
203 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400204 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500205 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100206 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400207 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500208 LLVMTypeRef vec4_type;
209 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500210
Tom Stellard467f5162012-05-16 15:15:35 -0400211 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100212 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500213
Michel Dänzer51f89a02013-12-09 15:33:53 +0900214 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400215
Christian König206f0592013-03-20 14:37:21 +0100216 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400217
218 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900219 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500220
Christian Königa0dca442013-03-22 15:59:22 +0100221 if (divisor) {
222 /* Build index from instance ID, start instance and divisor */
Marek Olšák8c37c162014-09-16 18:40:07 +0200223 si_shader_ctx->shader->uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200224 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100225 } else {
Marek Olšák09056b32014-04-23 16:15:36 +0200226 /* Load the buffer index for vertices. */
227 LLVMValueRef vertex_id = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
228 si_shader_ctx->param_vertex_id);
229 LLVMValueRef base_vertex = LLVMGetParam(radeon_bld->main_fn,
230 SI_PARAM_BASE_VERTEX);
231 buffer_index = LLVMBuildAdd(gallivm->builder, base_vertex, vertex_id, "");
Christian Königa0dca442013-03-22 15:59:22 +0100232 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500233
234 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400235 args[0] = t_list;
236 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100237 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900238 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100239 "llvm.SI.vs.load.input", vec4_type, args, 3,
240 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500241
242 /* Break up the vec4 into individual components */
243 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900244 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500245 /* XXX: Use a helper function for this. There is one in
246 * tgsi_llvm.c. */
247 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900248 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500249 input, llvm_chan, "");
250 }
251}
252
Michel Dänzer404b29d2013-11-21 16:45:28 +0900253static void declare_input_gs(
254 struct radeon_llvm_context *radeon_bld,
255 unsigned input_index,
256 const struct tgsi_full_declaration *decl)
257{
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900258 struct si_shader_context *si_shader_ctx =
259 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200260 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900261
262 si_store_shader_io_attribs(shader, decl);
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900263
264 if (decl->Semantic.Name != TGSI_SEMANTIC_PRIMID)
265 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900266}
267
268static LLVMValueRef fetch_input_gs(
269 struct lp_build_tgsi_context *bld_base,
270 const struct tgsi_full_src_register *reg,
271 enum tgsi_opcode_type type,
272 unsigned swizzle)
273{
274 struct lp_build_context *base = &bld_base->base;
275 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200276 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +0900277 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
278 struct gallivm_state *gallivm = base->gallivm;
279 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
280 LLVMValueRef vtx_offset;
281 LLVMValueRef t_list_ptr;
282 LLVMValueRef t_list;
283 LLVMValueRef args[9];
284 unsigned vtx_offset_param;
285
Michel Dänzerd8b3d802014-01-09 12:55:26 +0900286 if (swizzle != ~0 &&
287 shader->input[reg->Register.Index].name == TGSI_SEMANTIC_PRIMID) {
288 if (swizzle == 0)
289 return LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
290 SI_PARAM_PRIMITIVE_ID);
291 else
292 return uint->zero;
293 }
294
Michel Dänzer404b29d2013-11-21 16:45:28 +0900295 if (!reg->Register.Dimension)
296 return NULL;
297
298 if (swizzle == ~0) {
299 LLVMValueRef values[TGSI_NUM_CHANNELS];
300 unsigned chan;
301 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
302 values[chan] = fetch_input_gs(bld_base, reg, type, chan);
303 }
304 return lp_build_gather_values(bld_base->base.gallivm, values,
305 TGSI_NUM_CHANNELS);
306 }
307
308 /* Get the vertex offset parameter */
309 vtx_offset_param = reg->Dimension.Index;
310 if (vtx_offset_param < 2) {
311 vtx_offset_param += SI_PARAM_VTX0_OFFSET;
312 } else {
313 assert(vtx_offset_param < 6);
314 vtx_offset_param += SI_PARAM_VTX2_OFFSET - 2;
315 }
316 vtx_offset = lp_build_mul_imm(uint,
317 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
318 vtx_offset_param),
319 4);
320
321 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +0900322 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
323 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900324 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +0900325 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +0900326
327 args[0] = t_list;
328 args[1] = vtx_offset;
329 args[2] = lp_build_const_int32(gallivm,
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900330 ((shader->input[reg->Register.Index].param_offset * 4) +
331 swizzle) * 256);
Michel Dänzer404b29d2013-11-21 16:45:28 +0900332 args[3] = uint->zero;
333 args[4] = uint->one; /* OFFEN */
334 args[5] = uint->zero; /* IDXEN */
335 args[6] = uint->one; /* GLC */
336 args[7] = uint->zero; /* SLC */
337 args[8] = uint->zero; /* TFE */
338
339 return LLVMBuildBitCast(gallivm->builder,
340 build_intrinsic(gallivm->builder,
341 "llvm.SI.buffer.load.dword.i32.i32",
342 i32, args, 9,
343 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
344 tgsi2llvmtype(bld_base, type), "");
345}
346
Tom Stellarda75c6162012-01-06 17:38:37 -0500347static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900348 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500349 unsigned input_index,
350 const struct tgsi_full_declaration *decl)
351{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900352 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
353 struct si_shader_context *si_shader_ctx =
354 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200355 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900356 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
357 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400358 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900359 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100360
361 LLVMValueRef interp_param;
362 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500363
364 /* This value is:
365 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
366 * quad begins a new primitive. Bit 0 always needs
367 * to be unset)
368 * [32:16] ParamOffset
369 *
370 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900371 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200372 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500373
Christian König0666ffd2013-03-05 15:07:39 +0100374 unsigned chan;
375
Tom Stellard0fb1e682012-09-06 16:18:11 -0400376 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
377 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400378 unsigned soa_index =
379 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900380 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100381 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100382
383 if (chan == 3)
384 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900385 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100386 LLVMBuildFDiv(gallivm->builder,
387 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900388 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100389 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400390 }
391 return;
392 }
393
Michel Dänzer97078b12012-09-25 12:41:31 +0200394 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
395 LLVMValueRef face, is_face_positive;
396
Christian König0666ffd2013-03-05 15:07:39 +0100397 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
398
Michel Dänzer97078b12012-09-25 12:41:31 +0200399 is_face_positive = LLVMBuildFCmp(gallivm->builder,
400 LLVMRealUGT, face,
401 lp_build_const_float(gallivm, 0.0f),
402 "");
403
Michel Dänzer51f89a02013-12-09 15:33:53 +0900404 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200405 LLVMBuildSelect(gallivm->builder,
406 is_face_positive,
407 lp_build_const_float(gallivm, 1.0f),
408 lp_build_const_float(gallivm, 0.0f),
409 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900410 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
411 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200412 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900413 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200414 lp_build_const_float(gallivm, 1.0f);
415
416 return;
417 }
418
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900419 shader->input[input_index].param_offset = shader->nparam++;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200420 attr_number = lp_build_const_int32(gallivm,
421 shader->input[input_index].param_offset);
422
Francisco Jerez12799232012-04-30 18:27:52 +0200423 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500424 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100425 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500426 break;
427 case TGSI_INTERPOLATE_LINEAR:
Marek Olšák99df1202014-05-06 19:10:52 +0200428 if (si_shader_ctx->shader->key.ps.interp_at_sample)
429 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400430 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100431 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200432 else
Christian König0666ffd2013-03-05 15:07:39 +0100433 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200434 break;
Marek Olšák99df1202014-05-06 19:10:52 +0200435 case TGSI_INTERPOLATE_COLOR:
436 if (si_shader_ctx->shader->key.ps.flatshade) {
437 interp_param = 0;
438 break;
439 }
440 /* fall through to perspective */
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200441 case TGSI_INTERPOLATE_PERSPECTIVE:
Marek Olšák99df1202014-05-06 19:10:52 +0200442 if (si_shader_ctx->shader->key.ps.interp_at_sample)
443 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_SAMPLE);
Ilia Mirkin4c97ed42014-07-01 20:54:01 -0400444 else if (decl->Interp.Location == TGSI_INTERPOLATE_LOC_CENTROID)
Christian König0666ffd2013-03-05 15:07:39 +0100445 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200446 else
Christian König0666ffd2013-03-05 15:07:39 +0100447 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500448 break;
449 default:
450 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
451 return;
452 }
453
Christian König0666ffd2013-03-05 15:07:39 +0100454 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
455
Tom Stellarda75c6162012-01-06 17:38:37 -0500456 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200457 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100458 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100459 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200460 LLVMValueRef face, is_face_positive;
461 LLVMValueRef back_attr_number =
462 lp_build_const_int32(gallivm,
463 shader->input[input_index].param_offset + 1);
464
Christian König0666ffd2013-03-05 15:07:39 +0100465 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
466
Michel Dänzer691f08d2012-09-06 18:03:38 +0200467 is_face_positive = LLVMBuildFCmp(gallivm->builder,
468 LLVMRealUGT, face,
469 lp_build_const_float(gallivm, 0.0f),
470 "");
471
Tom Stellarda75c6162012-01-06 17:38:37 -0500472 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100473 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200474 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
475 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
476 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
477 LLVMValueRef front, back;
478
479 args[0] = llvm_chan;
480 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900481 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100482 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100483 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200484
485 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900486 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100487 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100488 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200489
Michel Dänzer51f89a02013-12-09 15:33:53 +0900490 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200491 LLVMBuildSelect(gallivm->builder,
492 is_face_positive,
493 front,
494 back,
495 "");
496 }
497
Michel Dänzer7c7d7382014-01-09 15:33:34 +0900498 shader->nparam++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200499 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
500 LLVMValueRef args[4];
501
502 args[0] = uint->zero;
503 args[1] = attr_number;
504 args[2] = params;
505 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900506 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
507 build_intrinsic(gallivm->builder, intr_name,
508 input_type, args, args[3] ? 4 : 3,
509 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
510 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
511 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200512 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900513 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200514 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200515 } else {
516 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100517 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200518 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
519 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
520 args[0] = llvm_chan;
521 args[1] = attr_number;
522 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100523 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900524 radeon_bld->inputs[soa_index] =
525 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100526 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100527 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200528 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500529 }
530}
531
Marek Olšák5b06fc32014-05-06 18:12:40 +0200532static LLVMValueRef get_sample_id(struct radeon_llvm_context *radeon_bld)
533{
534 struct gallivm_state *gallivm = &radeon_bld->gallivm;
535 LLVMValueRef value = LLVMGetParam(radeon_bld->main_fn,
536 SI_PARAM_ANCILLARY);
537 value = LLVMBuildLShr(gallivm->builder, value,
538 lp_build_const_int32(gallivm, 8), "");
539 value = LLVMBuildAnd(gallivm->builder, value,
540 lp_build_const_int32(gallivm, 0xf), "");
541 return value;
542}
543
Marek Olšák250aa932014-05-06 14:10:47 +0200544static LLVMValueRef load_const(LLVMBuilderRef builder, LLVMValueRef resource,
545 LLVMValueRef offset, LLVMTypeRef return_type)
546{
547 LLVMValueRef args[2] = {resource, offset};
548
549 return build_intrinsic(builder, "llvm.SI.load.const", return_type, args, 2,
550 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
551}
552
Christian Könige4ed5872013-03-21 18:02:52 +0100553static void declare_system_value(
554 struct radeon_llvm_context * radeon_bld,
555 unsigned index,
556 const struct tgsi_full_declaration *decl)
557{
Marek Olšák8d03d922013-09-01 23:59:06 +0200558 struct si_shader_context *si_shader_ctx =
559 si_shader_context(&radeon_bld->soa.bld_base);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200560 struct lp_build_context *uint_bld = &radeon_bld->soa.bld_base.uint_bld;
561 struct gallivm_state *gallivm = &radeon_bld->gallivm;
Christian Könige4ed5872013-03-21 18:02:52 +0100562 LLVMValueRef value = 0;
563
564 switch (decl->Semantic.Name) {
565 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200566 value = LLVMGetParam(radeon_bld->main_fn,
567 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100568 break;
569
570 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200571 value = LLVMGetParam(radeon_bld->main_fn,
572 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100573 break;
574
Marek Olšák5b06fc32014-05-06 18:12:40 +0200575 case TGSI_SEMANTIC_SAMPLEID:
576 value = get_sample_id(radeon_bld);
577 break;
578
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200579 case TGSI_SEMANTIC_SAMPLEPOS:
580 {
581 LLVMBuilderRef builder = gallivm->builder;
582 LLVMValueRef desc = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200583 LLVMValueRef buf_index = lp_build_const_int32(gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák99d9d7c2014-05-06 18:20:58 +0200584 LLVMValueRef resource = build_indexed_load(si_shader_ctx, desc, buf_index);
585
586 /* offset = sample_id * 8 (8 = 2 floats containing samplepos.xy) */
587 LLVMValueRef offset0 = lp_build_mul_imm(uint_bld, get_sample_id(radeon_bld), 8);
588 LLVMValueRef offset1 = LLVMBuildAdd(builder, offset0, lp_build_const_int32(gallivm, 4), "");
589
590 LLVMValueRef pos[4] = {
591 load_const(builder, resource, offset0, radeon_bld->soa.bld_base.base.elem_type),
592 load_const(builder, resource, offset1, radeon_bld->soa.bld_base.base.elem_type),
593 lp_build_const_float(gallivm, 0),
594 lp_build_const_float(gallivm, 0)
595 };
596 value = lp_build_gather_values(gallivm, pos, 4);
597 break;
598 }
599
Christian Könige4ed5872013-03-21 18:02:52 +0100600 default:
601 assert(!"unknown system value");
602 return;
603 }
604
605 radeon_bld->system_values[index] = value;
606}
607
Tom Stellarda75c6162012-01-06 17:38:37 -0500608static LLVMValueRef fetch_constant(
609 struct lp_build_tgsi_context * bld_base,
610 const struct tgsi_full_src_register *reg,
611 enum tgsi_opcode_type type,
612 unsigned swizzle)
613{
Christian König55fe5cc2013-03-04 16:30:06 +0100614 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500615 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100616 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200617 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500618
Christian König0f6cf2b2013-03-15 15:53:25 +0100619 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100620 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500621
Christian König8514f5a2013-02-04 17:46:42 +0100622 if (swizzle == LP_CHAN_ALL) {
623 unsigned chan;
624 LLVMValueRef values[4];
625 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
626 values[chan] = fetch_constant(bld_base, reg, type, chan);
627
628 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
629 }
630
Marek Olšák2fd42002013-10-25 11:45:47 +0200631 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100632 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100633
Marek Olšák2fd42002013-10-25 11:45:47 +0200634 if (!reg->Register.Indirect)
635 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
636
Christian König0f6cf2b2013-03-15 15:53:25 +0100637 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
638 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
639 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
Marek Olšák250aa932014-05-06 14:10:47 +0200640 addr = lp_build_add(&bld_base->uint_bld, addr,
641 lp_build_const_int32(base->gallivm, idx * 4));
Christian Könige7723b52012-08-24 12:55:34 +0200642
Marek Olšák250aa932014-05-06 14:10:47 +0200643 result = load_const(base->gallivm->builder, si_shader_ctx->const_resource[buf],
644 addr, base->elem_type);
Tom Stellarda75c6162012-01-06 17:38:37 -0500645
Christian Königf5298b02013-02-28 14:50:07 +0100646 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500647}
648
Michel Dänzer26c71392012-08-24 12:03:11 +0200649/* Initialize arguments for the shader export intrinsic */
650static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900651 LLVMValueRef *values,
Michel Dänzer26c71392012-08-24 12:03:11 +0200652 unsigned target,
653 LLVMValueRef *args)
654{
655 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
656 struct lp_build_context *uint =
657 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
658 struct lp_build_context *base = &bld_base->base;
659 unsigned compressed = 0;
660 unsigned chan;
661
Michel Dänzerf402acd2012-08-22 18:15:36 +0200662 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
663 int cbuf = target - V_008DFC_SQ_EXP_MRT;
664
665 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100666 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100667
668 if (compressed)
669 si_shader_ctx->shader->spi_shader_col_format |=
670 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
671 else
672 si_shader_ctx->shader->spi_shader_col_format |=
673 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200674
675 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200676 }
677 }
678
679 if (compressed) {
680 /* Pixel shader needs to pack output values before export */
681 for (chan = 0; chan < 2; chan++ ) {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900682 args[0] = values[2 * chan];
683 args[1] = values[2 * chan + 1];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200684 args[chan + 5] =
685 build_intrinsic(base->gallivm->builder,
686 "llvm.SI.packf16",
687 LLVMInt32TypeInContext(base->gallivm->context),
688 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100689 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100690 args[chan + 7] = args[chan + 5] =
691 LLVMBuildBitCast(base->gallivm->builder,
692 args[chan + 5],
693 LLVMFloatTypeInContext(base->gallivm->context),
694 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200695 }
696
697 /* Set COMPR flag */
698 args[4] = uint->one;
699 } else {
Michel Dänzer404b29d2013-11-21 16:45:28 +0900700 for (chan = 0; chan < 4; chan++ )
Michel Dänzerf402acd2012-08-22 18:15:36 +0200701 /* +5 because the first output value will be
702 * the 6th argument to the intrinsic. */
Michel Dänzer404b29d2013-11-21 16:45:28 +0900703 args[chan + 5] = values[chan];
Michel Dänzerf402acd2012-08-22 18:15:36 +0200704
705 /* Clear COMPR flag */
706 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200707 }
708
709 /* XXX: This controls which components of the output
710 * registers actually get exported. (e.g bit 0 means export
711 * X component, bit 1 means export Y component, etc.) I'm
712 * hard coding this to 0xf for now. In the future, we might
713 * want to do something else. */
714 args[0] = lp_build_const_int32(base->gallivm, 0xf);
715
716 /* Specify whether the EXEC mask represents the valid mask */
717 args[1] = uint->zero;
718
719 /* Specify whether this is the last export */
720 args[2] = uint->zero;
721
722 /* Specify the target we are exporting */
723 args[3] = lp_build_const_int32(base->gallivm, target);
724
Michel Dänzer26c71392012-08-24 12:03:11 +0200725 /* XXX: We probably need to keep track of the output
726 * values, so we know what we are passing to the next
727 * stage. */
728}
729
Michel Dänzer404b29d2013-11-21 16:45:28 +0900730/* Load from output pointers and initialize arguments for the shader export intrinsic */
731static void si_llvm_init_export_args_load(struct lp_build_tgsi_context *bld_base,
732 LLVMValueRef *out_ptr,
733 unsigned target,
734 LLVMValueRef *args)
735{
736 struct gallivm_state *gallivm = bld_base->base.gallivm;
737 LLVMValueRef values[4];
738 int i;
739
740 for (i = 0; i < 4; i++)
741 values[i] = LLVMBuildLoad(gallivm->builder, out_ptr[i], "");
742
743 si_llvm_init_export_args(bld_base, values, target, args);
744}
745
Michel Dänzer7708a862012-11-02 15:57:30 +0100746static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900747 LLVMValueRef *out_ptr)
Michel Dänzer7708a862012-11-02 15:57:30 +0100748{
749 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
750 struct gallivm_state *gallivm = bld_base->base.gallivm;
751
Christian Königa0dca442013-03-22 15:59:22 +0100752 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400753 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
754 SI_PARAM_ALPHA_REF);
755
Michel Dänzer7708a862012-11-02 15:57:30 +0100756 LLVMValueRef alpha_pass =
757 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100758 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900759 LLVMBuildLoad(gallivm->builder, out_ptr[3], ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400760 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100761 LLVMValueRef arg =
762 lp_build_select(&bld_base->base,
763 alpha_pass,
764 lp_build_const_float(gallivm, 1.0f),
765 lp_build_const_float(gallivm, -1.0f));
766
767 build_intrinsic(gallivm->builder,
768 "llvm.AMDGPU.kill",
769 LLVMVoidTypeInContext(gallivm->context),
770 &arg, 1, 0);
771 } else {
772 build_intrinsic(gallivm->builder,
773 "llvm.AMDGPU.kilp",
774 LLVMVoidTypeInContext(gallivm->context),
775 NULL, 0, 0);
776 }
Marek Olšákadc57972014-09-19 17:07:07 +0200777
778 si_shader_ctx->shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
Michel Dänzer7708a862012-11-02 15:57:30 +0100779}
780
Michel Dänzere3befbc2013-05-15 18:09:50 +0200781static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzer404b29d2013-11-21 16:45:28 +0900782 LLVMValueRef (*pos)[9], LLVMValueRef *out_elts)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200783{
784 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +0200785 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200786 struct lp_build_context *base = &bld_base->base;
787 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200788 unsigned reg_index;
789 unsigned chan;
790 unsigned const_chan;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200791 LLVMValueRef base_elt;
792 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšákee2a8182014-07-07 23:27:19 +0200793 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, SI_DRIVER_STATE_CONST_BUF);
Marek Olšák2fd42002013-10-25 11:45:47 +0200794 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200795
Michel Dänzere3befbc2013-05-15 18:09:50 +0200796 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200797 LLVMValueRef *args = pos[2 + reg_index];
798
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200799 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
800 continue;
801
Marek Olšák8c37c162014-09-16 18:40:07 +0200802 shader->clip_dist_write |= 0xf << (4 * reg_index);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200803
Michel Dänzere3befbc2013-05-15 18:09:50 +0200804 args[5] =
805 args[6] =
806 args[7] =
807 args[8] = lp_build_const_float(base->gallivm, 0.0f);
808
809 /* Compute dot products of position and user clip plane vectors */
810 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
811 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
Michel Dänzere3befbc2013-05-15 18:09:50 +0200812 args[1] = lp_build_const_int32(base->gallivm,
813 ((reg_index * 4 + chan) * 4 +
814 const_chan) * 4);
Marek Olšák250aa932014-05-06 14:10:47 +0200815 base_elt = load_const(base->gallivm->builder, const_resource,
816 args[1], base->elem_type);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200817 args[5 + chan] =
818 lp_build_add(base, args[5 + chan],
819 lp_build_mul(base, base_elt,
820 out_elts[const_chan]));
821 }
822 }
823
824 args[0] = lp_build_const_int32(base->gallivm, 0xf);
825 args[1] = uint->zero;
826 args[2] = uint->zero;
827 args[3] = lp_build_const_int32(base->gallivm,
828 V_008DFC_SQ_EXP_POS + 2 + reg_index);
829 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200830 }
831}
832
Marek Olšák8d03d922013-09-01 23:59:06 +0200833static void si_dump_streamout(struct pipe_stream_output_info *so)
834{
835 unsigned i;
836
837 if (so->num_outputs)
838 fprintf(stderr, "STREAMOUT\n");
839
840 for (i = 0; i < so->num_outputs; i++) {
841 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
842 so->output[i].start_component;
843 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
844 i, so->output[i].output_buffer,
845 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
846 so->output[i].register_index,
847 mask & 1 ? "x" : "",
848 mask & 2 ? "y" : "",
849 mask & 4 ? "z" : "",
850 mask & 8 ? "w" : "");
851 }
852}
853
854/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
855 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
856 * or v4i32 (num_channels=3,4). */
857static void build_tbuffer_store(struct si_shader_context *shader,
858 LLVMValueRef rsrc,
859 LLVMValueRef vdata,
860 unsigned num_channels,
861 LLVMValueRef vaddr,
862 LLVMValueRef soffset,
863 unsigned inst_offset,
864 unsigned dfmt,
865 unsigned nfmt,
866 unsigned offen,
867 unsigned idxen,
868 unsigned glc,
869 unsigned slc,
870 unsigned tfe)
871{
872 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
873 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
874 LLVMValueRef args[] = {
875 rsrc,
876 vdata,
877 LLVMConstInt(i32, num_channels, 0),
878 vaddr,
879 soffset,
880 LLVMConstInt(i32, inst_offset, 0),
881 LLVMConstInt(i32, dfmt, 0),
882 LLVMConstInt(i32, nfmt, 0),
883 LLVMConstInt(i32, offen, 0),
884 LLVMConstInt(i32, idxen, 0),
885 LLVMConstInt(i32, glc, 0),
886 LLVMConstInt(i32, slc, 0),
887 LLVMConstInt(i32, tfe, 0)
888 };
889
Michel Dänzerdb9d6af2014-01-24 16:46:27 +0900890 /* The instruction offset field has 12 bits */
891 assert(offen || inst_offset < (1 << 12));
892
Marek Olšák8d03d922013-09-01 23:59:06 +0200893 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
894 unsigned func = CLAMP(num_channels, 1, 3) - 1;
895 const char *types[] = {"i32", "v2i32", "v4i32"};
896 char name[256];
897 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
898
899 lp_build_intrinsic(gallivm->builder, name,
900 LLVMVoidTypeInContext(gallivm->context),
901 args, Elements(args));
902}
903
904static void build_streamout_store(struct si_shader_context *shader,
905 LLVMValueRef rsrc,
906 LLVMValueRef vdata,
907 unsigned num_channels,
908 LLVMValueRef vaddr,
909 LLVMValueRef soffset,
910 unsigned inst_offset)
911{
912 static unsigned dfmt[] = {
913 V_008F0C_BUF_DATA_FORMAT_32,
914 V_008F0C_BUF_DATA_FORMAT_32_32,
915 V_008F0C_BUF_DATA_FORMAT_32_32_32,
916 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
917 };
918 assert(num_channels >= 1 && num_channels <= 4);
919
920 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
921 inst_offset, dfmt[num_channels-1],
922 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
923}
924
925/* On SI, the vertex shader is responsible for writing streamout data
926 * to buffers. */
Michel Dänzer67e385b2014-01-08 17:48:21 +0900927static void si_llvm_emit_streamout(struct si_shader_context *shader,
928 struct si_shader_output_values *outputs,
929 unsigned noutput)
Marek Olšák8d03d922013-09-01 23:59:06 +0200930{
931 struct pipe_stream_output_info *so = &shader->shader->selector->so;
932 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
933 LLVMBuilderRef builder = gallivm->builder;
934 int i, j;
935 struct lp_build_if_state if_ctx;
936
937 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
938
939 LLVMValueRef so_param =
940 LLVMGetParam(shader->radeon_bld.main_fn,
941 shader->param_streamout_config);
942
943 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
944 LLVMValueRef so_vtx_count =
945 LLVMBuildAnd(builder,
946 LLVMBuildLShr(builder, so_param,
947 LLVMConstInt(i32, 16, 0), ""),
948 LLVMConstInt(i32, 127, 0), "");
949
950 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
951 NULL, 0, LLVMReadNoneAttribute);
952
953 /* can_emit = tid < so_vtx_count; */
954 LLVMValueRef can_emit =
955 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
956
957 /* Emit the streamout code conditionally. This actually avoids
958 * out-of-bounds buffer access. The hw tells us via the SGPR
959 * (so_vtx_count) which threads are allowed to emit streamout data. */
960 lp_build_if(&if_ctx, gallivm, can_emit);
961 {
962 /* The buffer offset is computed as follows:
963 * ByteOffset = streamout_offset[buffer_id]*4 +
964 * (streamout_write_index + thread_id)*stride[buffer_id] +
965 * attrib_offset
966 */
967
968 LLVMValueRef so_write_index =
969 LLVMGetParam(shader->radeon_bld.main_fn,
970 shader->param_streamout_write_index);
971
972 /* Compute (streamout_write_index + thread_id). */
973 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
974
975 /* Compute the write offset for each enabled buffer. */
976 LLVMValueRef so_write_offset[4] = {};
977 for (i = 0; i < 4; i++) {
978 if (!so->stride[i])
979 continue;
980
981 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
982 shader->param_streamout_offset[i]);
983 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
984
985 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
986 LLVMConstInt(i32, so->stride[i]*4, 0), "");
987 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
988 }
989
Marek Olšák8d03d922013-09-01 23:59:06 +0200990 /* Write streamout data. */
991 for (i = 0; i < so->num_outputs; i++) {
992 unsigned buf_idx = so->output[i].output_buffer;
993 unsigned reg = so->output[i].register_index;
994 unsigned start = so->output[i].start_component;
995 unsigned num_comps = so->output[i].num_components;
996 LLVMValueRef out[4];
997
998 assert(num_comps && num_comps <= 4);
999 if (!num_comps || num_comps > 4)
1000 continue;
1001
1002 /* Load the output as int. */
1003 for (j = 0; j < num_comps; j++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001004 unsigned outidx = 0;
1005
1006 while (outidx < noutput && outputs[outidx].index != reg)
1007 outidx++;
1008
1009 if (outidx < noutput)
1010 out[j] = LLVMBuildBitCast(builder,
1011 outputs[outidx].values[start+j],
1012 i32, "");
1013 else
1014 out[j] = NULL;
Marek Olšák8d03d922013-09-01 23:59:06 +02001015 }
1016
Michel Dänzer67e385b2014-01-08 17:48:21 +09001017 if (!out[0])
1018 continue;
1019
Marek Olšák8d03d922013-09-01 23:59:06 +02001020 /* Pack the output. */
1021 LLVMValueRef vdata = NULL;
1022
1023 switch (num_comps) {
1024 case 1: /* as i32 */
1025 vdata = out[0];
1026 break;
1027 case 2: /* as v2i32 */
1028 case 3: /* as v4i32 (aligned to 4) */
1029 case 4: /* as v4i32 */
1030 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
1031 for (j = 0; j < num_comps; j++) {
1032 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
1033 LLVMConstInt(i32, j, 0), "");
1034 }
1035 break;
1036 }
1037
1038 build_streamout_store(shader, shader->so_buffers[buf_idx],
1039 vdata, num_comps,
1040 so_write_offset[buf_idx],
1041 LLVMConstInt(i32, 0, 0),
1042 so->output[i].dst_offset*4);
1043 }
1044 }
1045 lp_build_endif(&if_ctx);
1046}
1047
Michel Dänzer7435d9f2013-12-04 13:37:07 +09001048
Michel Dänzer404b29d2013-11-21 16:45:28 +09001049/* Generate export instructions for hardware VS shader stage */
1050static void si_llvm_export_vs(struct lp_build_tgsi_context *bld_base,
1051 struct si_shader_output_values *outputs,
1052 unsigned noutput)
Tom Stellarda75c6162012-01-06 17:38:37 -05001053{
1054 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001055 struct si_shader * shader = si_shader_ctx->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -05001056 struct lp_build_context * base = &bld_base->base;
1057 struct lp_build_context * uint =
1058 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001059 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +02001060 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer404b29d2013-11-21 16:45:28 +09001061 LLVMValueRef psize_value = NULL, edgeflag_value = NULL, layer_value = NULL;
1062 unsigned semantic_name, semantic_index, semantic_usage;
1063 unsigned target;
Christian König35088152012-08-01 22:35:24 +02001064 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001065 unsigned pos_idx;
Michel Dänzerb00269a2013-08-07 18:14:16 +02001066 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -05001067
Michel Dänzer67e385b2014-01-08 17:48:21 +09001068 if (outputs && si_shader_ctx->shader->selector->so.num_outputs) {
1069 si_llvm_emit_streamout(si_shader_ctx, outputs, noutput);
Marek Olšák8d03d922013-09-01 23:59:06 +02001070 }
1071
Michel Dänzer404b29d2013-11-21 16:45:28 +09001072 for (i = 0; i < noutput; i++) {
1073 semantic_name = outputs[i].name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001074 semantic_index = outputs[i].sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001075 semantic_usage = outputs[i].usage;
Tom Stellarda75c6162012-01-06 17:38:37 -05001076
Michel Dänzer0afeea52013-05-02 14:53:17 +02001077handle_semantic:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001078 /* Select the correct target */
1079 switch(semantic_name) {
1080 case TGSI_SEMANTIC_PSIZE:
1081 shader->vs_out_misc_write = true;
1082 shader->vs_out_point_size = true;
1083 psize_value = outputs[i].values[0];
1084 continue;
1085 case TGSI_SEMANTIC_EDGEFLAG:
1086 shader->vs_out_misc_write = true;
1087 shader->vs_out_edgeflag = true;
1088 edgeflag_value = outputs[i].values[0];
1089 continue;
1090 case TGSI_SEMANTIC_LAYER:
1091 shader->vs_out_misc_write = true;
1092 shader->vs_out_layer = true;
1093 layer_value = outputs[i].values[0];
1094 continue;
1095 case TGSI_SEMANTIC_POSITION:
1096 target = V_008DFC_SQ_EXP_POS;
1097 break;
1098 case TGSI_SEMANTIC_COLOR:
1099 case TGSI_SEMANTIC_BCOLOR:
1100 target = V_008DFC_SQ_EXP_PARAM + param_count;
1101 shader->output[i].param_offset = param_count;
1102 param_count++;
1103 break;
1104 case TGSI_SEMANTIC_CLIPDIST:
1105 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
1106 (1 << semantic_index)))
Marek Olšák053606d2013-11-19 22:07:30 +01001107 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001108 shader->clip_dist_write |=
1109 semantic_usage << (semantic_index << 2);
1110 target = V_008DFC_SQ_EXP_POS + 2 + semantic_index;
1111 break;
1112 case TGSI_SEMANTIC_CLIPVERTEX:
1113 si_llvm_emit_clipvertex(bld_base, pos_args, outputs[i].values);
1114 continue;
Michel Dänzerd8b3d802014-01-09 12:55:26 +09001115 case TGSI_SEMANTIC_PRIMID:
Michel Dänzer404b29d2013-11-21 16:45:28 +09001116 case TGSI_SEMANTIC_FOG:
1117 case TGSI_SEMANTIC_GENERIC:
1118 target = V_008DFC_SQ_EXP_PARAM + param_count;
1119 shader->output[i].param_offset = param_count;
1120 param_count++;
1121 break;
1122 default:
1123 target = 0;
1124 fprintf(stderr,
1125 "Warning: SI unhandled vs output type:%d\n",
1126 semantic_name);
1127 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001128
Michel Dänzer404b29d2013-11-21 16:45:28 +09001129 si_llvm_init_export_args(bld_base, outputs[i].values, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -05001130
Michel Dänzer404b29d2013-11-21 16:45:28 +09001131 if (target >= V_008DFC_SQ_EXP_POS &&
1132 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1133 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1134 args, sizeof(args));
1135 } else {
1136 lp_build_intrinsic(base->gallivm->builder,
1137 "llvm.SI.export",
1138 LLVMVoidTypeInContext(base->gallivm->context),
1139 args, 9);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001140 }
1141
1142 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1143 semantic_name = TGSI_SEMANTIC_GENERIC;
1144 goto handle_semantic;
1145 }
1146 }
1147
1148 /* We need to add the position output manually if it's missing. */
1149 if (!pos_args[0][0]) {
1150 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
1151 pos_args[0][1] = uint->zero; /* EXEC mask */
1152 pos_args[0][2] = uint->zero; /* last export? */
1153 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
1154 pos_args[0][4] = uint->zero; /* COMPR flag */
1155 pos_args[0][5] = base->zero; /* X */
1156 pos_args[0][6] = base->zero; /* Y */
1157 pos_args[0][7] = base->zero; /* Z */
1158 pos_args[0][8] = base->one; /* W */
1159 }
1160
1161 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1162 if (shader->vs_out_misc_write) {
1163 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1164 shader->vs_out_point_size |
1165 (shader->vs_out_edgeflag << 1) |
1166 (shader->vs_out_layer << 2));
1167 pos_args[1][1] = uint->zero; /* EXEC mask */
1168 pos_args[1][2] = uint->zero; /* last export? */
1169 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1170 pos_args[1][4] = uint->zero; /* COMPR flag */
1171 pos_args[1][5] = base->zero; /* X */
1172 pos_args[1][6] = base->zero; /* Y */
1173 pos_args[1][7] = base->zero; /* Z */
1174 pos_args[1][8] = base->zero; /* W */
1175
Michel Dänzer404b29d2013-11-21 16:45:28 +09001176 if (shader->vs_out_point_size)
1177 pos_args[1][5] = psize_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001178
1179 if (shader->vs_out_edgeflag) {
Michel Dänzer51f89a02013-12-09 15:33:53 +09001180 /* The output is a float, but the hw expects an integer
1181 * with the first bit containing the edge flag. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001182 edgeflag_value = LLVMBuildFPToUI(base->gallivm->builder,
1183 edgeflag_value,
1184 bld_base->uint_bld.elem_type, "");
1185 edgeflag_value = lp_build_min(&bld_base->int_bld,
1186 edgeflag_value,
1187 bld_base->int_bld.one);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001188
1189 /* The LLVM intrinsic expects a float. */
Michel Dänzer404b29d2013-11-21 16:45:28 +09001190 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder,
1191 edgeflag_value,
Michel Dänzer51f89a02013-12-09 15:33:53 +09001192 base->elem_type, "");
1193 }
1194
Michel Dänzer404b29d2013-11-21 16:45:28 +09001195 if (shader->vs_out_layer)
1196 pos_args[1][7] = layer_value;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001197 }
1198
1199 for (i = 0; i < 4; i++)
1200 if (pos_args[i][0])
1201 shader->nr_pos_exports++;
1202
1203 pos_idx = 0;
1204 for (i = 0; i < 4; i++) {
1205 if (!pos_args[i][0])
1206 continue;
1207
1208 /* Specify the target we are exporting */
1209 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1210
1211 if (pos_idx == shader->nr_pos_exports)
1212 /* Specify that this is the last export */
1213 pos_args[i][2] = uint->one;
1214
1215 lp_build_intrinsic(base->gallivm->builder,
1216 "llvm.SI.export",
1217 LLVMVoidTypeInContext(base->gallivm->context),
1218 pos_args[i], 9);
1219 }
1220}
1221
Michel Dänzer404b29d2013-11-21 16:45:28 +09001222static void si_llvm_emit_es_epilogue(struct lp_build_tgsi_context * bld_base)
1223{
1224 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1225 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02001226 struct si_shader *es = si_shader_ctx->shader;
Michel Dänzere884c562014-01-15 15:24:14 +09001227 struct si_shader *gs = si_shader_ctx->gs_for_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001228 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1229 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001230 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1231 SI_PARAM_ES2GS_OFFSET);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001232 LLVMValueRef t_list_ptr;
1233 LLVMValueRef t_list;
1234 unsigned chan;
1235 int i;
1236
1237 while (!tgsi_parse_end_of_tokens(parse)) {
1238 struct tgsi_full_declaration *d =
1239 &parse->FullToken.FullDeclaration;
1240
1241 tgsi_parse_token(parse);
1242
1243 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1244 continue;
1245
Michel Dänzere884c562014-01-15 15:24:14 +09001246 si_store_shader_io_attribs(es, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001247 }
1248
1249 /* Load the ESGS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09001250 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1251 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001252 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09001253 lp_build_const_int32(gallivm, SI_RING_ESGS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09001254
Michel Dänzere884c562014-01-15 15:24:14 +09001255 for (i = 0; i < es->noutput; i++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001256 LLVMValueRef *out_ptr =
Michel Dänzere884c562014-01-15 15:24:14 +09001257 si_shader_ctx->radeon_bld.soa.outputs[es->output[i].index];
1258 int j;
1259
1260 for (j = 0; j < gs->ninput; j++) {
1261 if (gs->input[j].name == es->output[i].name &&
1262 gs->input[j].sid == es->output[i].sid)
1263 break;
1264 }
1265 if (j == gs->ninput)
1266 continue;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001267
1268 for (chan = 0; chan < 4; chan++) {
1269 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzer404b29d2013-11-21 16:45:28 +09001270 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
1271
1272 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001273 LLVMGetUndef(i32), soffset,
1274 (4 * gs->input[j].param_offset + chan) * 4,
Michel Dänzer404b29d2013-11-21 16:45:28 +09001275 V_008F0C_BUF_DATA_FORMAT_32,
1276 V_008F0C_BUF_NUM_FORMAT_UINT,
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09001277 0, 0, 1, 1, 0);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001278 }
1279 }
1280}
1281
1282static void si_llvm_emit_gs_epilogue(struct lp_build_tgsi_context *bld_base)
1283{
1284 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1285 struct gallivm_state *gallivm = bld_base->base.gallivm;
1286 LLVMValueRef args[2];
1287
1288 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_NOP | SENDMSG_GS_DONE);
1289 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
1290 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
1291 LLVMVoidTypeInContext(gallivm->context), args, 2,
1292 LLVMNoUnwindAttribute);
1293}
1294
1295static void si_llvm_emit_vs_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 *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001300 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1301 struct si_shader_output_values *outputs = NULL;
1302 unsigned noutput = 0;
1303 int i;
1304
1305 while (!tgsi_parse_end_of_tokens(parse)) {
1306 struct tgsi_full_declaration *d =
1307 &parse->FullToken.FullDeclaration;
1308 unsigned index;
1309
1310 tgsi_parse_token(parse);
1311
1312 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1313 continue;
1314
Marek Olšák8c37c162014-09-16 18:40:07 +02001315 i = si_store_shader_io_attribs(shader, d);
Michel Dänzer404b29d2013-11-21 16:45:28 +09001316 if (i < 0)
1317 continue;
1318
1319 outputs = REALLOC(outputs, noutput * sizeof(outputs[0]),
1320 (noutput + 1) * sizeof(outputs[0]));
1321 for (index = d->Range.First; index <= d->Range.Last; index++) {
Michel Dänzer67e385b2014-01-08 17:48:21 +09001322 outputs[noutput].index = index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001323 outputs[noutput].name = d->Semantic.Name;
Michel Dänzer67e385b2014-01-08 17:48:21 +09001324 outputs[noutput].sid = d->Semantic.Index;
Michel Dänzer404b29d2013-11-21 16:45:28 +09001325 outputs[noutput].usage = d->Declaration.UsageMask;
1326
1327 for (i = 0; i < 4; i++)
1328 outputs[noutput].values[i] =
1329 LLVMBuildLoad(gallivm->builder,
1330 si_shader_ctx->radeon_bld.soa.outputs[index][i],
1331 "");
1332 }
1333 noutput++;
1334 }
1335
1336 si_llvm_export_vs(bld_base, outputs, noutput);
1337 FREE(outputs);
1338}
1339
Michel Dänzer51f89a02013-12-09 15:33:53 +09001340static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1341{
1342 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Marek Olšák8c37c162014-09-16 18:40:07 +02001343 struct si_shader * shader = si_shader_ctx->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001344 struct lp_build_context * base = &bld_base->base;
1345 struct lp_build_context * uint =
1346 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1347 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1348 LLVMValueRef args[9];
1349 LLVMValueRef last_args[9] = { 0 };
1350 unsigned semantic_name;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001351 int depth_index = -1, stencil_index = -1, samplemask_index = -1;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001352 int i;
1353
1354 while (!tgsi_parse_end_of_tokens(parse)) {
1355 struct tgsi_full_declaration *d =
1356 &parse->FullToken.FullDeclaration;
1357 unsigned target;
1358 unsigned index;
1359
1360 tgsi_parse_token(parse);
1361
1362 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
1363 parse->FullToken.FullProperty.Property.PropertyName ==
1364 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
1365 shader->fs_write_all = TRUE;
1366
1367 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1368 continue;
1369
1370 i = si_store_shader_io_attribs(shader, d);
1371 if (i < 0)
1372 continue;
1373
1374 semantic_name = d->Semantic.Name;
1375 for (index = d->Range.First; index <= d->Range.Last; index++) {
1376 /* Select the correct target */
1377 switch(semantic_name) {
1378 case TGSI_SEMANTIC_POSITION:
1379 depth_index = index;
1380 continue;
1381 case TGSI_SEMANTIC_STENCIL:
1382 stencil_index = index;
1383 continue;
Marek Olšákd0e8b652014-05-06 20:04:31 +02001384 case TGSI_SEMANTIC_SAMPLEMASK:
1385 samplemask_index = index;
1386 continue;
Michel Dänzer51f89a02013-12-09 15:33:53 +09001387 case TGSI_SEMANTIC_COLOR:
1388 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1389 if (si_shader_ctx->shader->key.ps.alpha_to_one)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001390 LLVMBuildStore(bld_base->base.gallivm->builder,
1391 bld_base->base.one,
1392 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
1393
Michel Dänzer51f89a02013-12-09 15:33:53 +09001394 if (d->Semantic.Index == 0 &&
1395 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer404b29d2013-11-21 16:45:28 +09001396 si_alpha_test(bld_base,
1397 si_shader_ctx->radeon_bld.soa.outputs[index]);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001398 break;
1399 default:
1400 target = 0;
1401 fprintf(stderr,
1402 "Warning: SI unhandled fs output type:%d\n",
1403 semantic_name);
1404 }
1405
Michel Dänzer404b29d2013-11-21 16:45:28 +09001406 si_llvm_init_export_args_load(bld_base,
1407 si_shader_ctx->radeon_bld.soa.outputs[index],
1408 target, args);
Michel Dänzer51f89a02013-12-09 15:33:53 +09001409
1410 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001411 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001412 if (last_args[0]) {
1413 lp_build_intrinsic(base->gallivm->builder,
1414 "llvm.SI.export",
1415 LLVMVoidTypeInContext(base->gallivm->context),
1416 last_args, 9);
1417 }
1418
Marek Olšák0eb528a2013-12-04 13:24:22 +01001419 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001420 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001421
1422 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1423 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1424 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1425 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
Michel Dänzer404b29d2013-11-21 16:45:28 +09001426 si_llvm_init_export_args_load(bld_base,
1427 si_shader_ctx->radeon_bld.soa.outputs[index],
1428 V_008DFC_SQ_EXP_MRT + c, args);
Marek Olšák0eb528a2013-12-04 13:24:22 +01001429 lp_build_intrinsic(base->gallivm->builder,
1430 "llvm.SI.export",
1431 LLVMVoidTypeInContext(base->gallivm->context),
1432 args, 9);
1433 }
1434 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001435 } else {
1436 lp_build_intrinsic(base->gallivm->builder,
1437 "llvm.SI.export",
1438 LLVMVoidTypeInContext(base->gallivm->context),
1439 args, 9);
1440 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001441 }
1442 }
1443
Marek Olšákd0e8b652014-05-06 20:04:31 +02001444 if (depth_index >= 0 || stencil_index >= 0 || samplemask_index >= 0) {
Michel Dänzer1a616c12012-11-13 17:35:09 +01001445 LLVMValueRef out_ptr;
1446 unsigned mask = 0;
1447
1448 /* Specify the target we are exporting */
1449 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1450
Marek Olšák9baaa5d2014-05-06 19:55:48 +02001451 args[5] = base->zero; /* R, depth */
1452 args[6] = base->zero; /* G, stencil test value[0:7], stencil op value[8:15] */
1453 args[7] = base->zero; /* B, sample mask */
1454 args[8] = base->zero; /* A, alpha to mask */
1455
Michel Dänzer1a616c12012-11-13 17:35:09 +01001456 if (depth_index >= 0) {
1457 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1458 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1459 mask |= 0x1;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001460 si_shader_ctx->shader->db_shader_control |= S_02880C_Z_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001461 }
1462
1463 if (stencil_index >= 0) {
1464 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
Michel Dänzer1a616c12012-11-13 17:35:09 +01001465 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001466 /* Only setting the stencil component bit (0x2) here
1467 * breaks some stencil piglit tests
1468 */
1469 mask |= 0x3;
Marek Olšákd9e102b2014-05-06 19:59:53 +02001470 si_shader_ctx->shader->db_shader_control |=
1471 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(1);
Michel Dänzer1a616c12012-11-13 17:35:09 +01001472 }
1473
Marek Olšákd0e8b652014-05-06 20:04:31 +02001474 if (samplemask_index >= 0) {
1475 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[samplemask_index][0];
1476 args[7] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1477 mask |= 0xf; /* Set all components. */
1478 si_shader_ctx->shader->db_shader_control |= S_02880C_MASK_EXPORT_ENABLE(1);
1479 }
1480
1481 if (samplemask_index >= 0)
1482 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_ABGR;
1483 else if (stencil_index >= 0)
Marek Olšákd9e102b2014-05-06 19:59:53 +02001484 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_GR;
1485 else
1486 si_shader_ctx->shader->spi_shader_z_format = V_028710_SPI_SHADER_32_R;
1487
Michel Dänzer1a616c12012-11-13 17:35:09 +01001488 /* Specify which components to enable */
1489 args[0] = lp_build_const_int32(base->gallivm, mask);
1490
1491 args[1] =
1492 args[2] =
1493 args[4] = uint->zero;
1494
1495 if (last_args[0])
1496 lp_build_intrinsic(base->gallivm->builder,
1497 "llvm.SI.export",
1498 LLVMVoidTypeInContext(base->gallivm->context),
1499 args, 9);
1500 else
1501 memcpy(last_args, args, sizeof(args));
1502 }
1503
Michel Dänzer51f89a02013-12-09 15:33:53 +09001504 if (!last_args[0]) {
1505 /* Specify which components to enable */
1506 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001507
Michel Dänzer51f89a02013-12-09 15:33:53 +09001508 /* Specify the target we are exporting */
1509 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001510
Michel Dänzer51f89a02013-12-09 15:33:53 +09001511 /* Set COMPR flag to zero to export data as 32-bit */
1512 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001513
Michel Dänzer51f89a02013-12-09 15:33:53 +09001514 /* dummy bits */
1515 last_args[5]= uint->zero;
1516 last_args[6]= uint->zero;
1517 last_args[7]= uint->zero;
1518 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001519 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001520
1521 /* Specify whether the EXEC mask represents the valid mask */
1522 last_args[1] = uint->one;
1523
1524 /* Specify that this is the last export */
1525 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1526
1527 lp_build_intrinsic(base->gallivm->builder,
1528 "llvm.SI.export",
1529 LLVMVoidTypeInContext(base->gallivm->context),
1530 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001531}
1532
Marek Olšák4855acd2013-08-06 15:08:54 +02001533static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1534 struct lp_build_tgsi_context * bld_base,
1535 struct lp_build_emit_data * emit_data);
1536
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001537static bool tgsi_is_shadow_sampler(unsigned target)
1538{
1539 return target == TGSI_TEXTURE_SHADOW1D ||
1540 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1541 target == TGSI_TEXTURE_SHADOW2D ||
1542 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1543 target == TGSI_TEXTURE_SHADOWCUBE ||
1544 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY ||
1545 target == TGSI_TEXTURE_SHADOWRECT;
1546}
1547
Marek Olšák877bb522014-06-25 03:12:46 +02001548static const struct lp_build_tgsi_action tex_action;
1549
Tom Stellarda75c6162012-01-06 17:38:37 -05001550static void tex_fetch_args(
1551 struct lp_build_tgsi_context * bld_base,
1552 struct lp_build_emit_data * emit_data)
1553{
Christian König55fe5cc2013-03-04 16:30:06 +01001554 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001555 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001556 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001557 unsigned opcode = inst->Instruction.Opcode;
1558 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001559 LLVMValueRef coords[4];
1560 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001561 int ref_pos;
1562 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001563 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001564 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001565 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1566 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
Marek Olšák877bb522014-06-25 03:12:46 +02001567 bool has_offset = HAVE_LLVM >= 0x0305 ? inst->Texture.NumOffsets > 0 : false;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001568
1569 if (target == TGSI_TEXTURE_BUFFER) {
1570 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1571 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1572 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1573 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1574
Marek Olšák4f3f0432014-07-07 22:49:15 +02001575 /* Bitcast and truncate v8i32 to v16i8. */
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001576 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1577 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1578 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1579 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1580
1581 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1582 emit_data->args[0] = res;
1583 emit_data->args[1] = bld_base->uint_bld.zero;
1584 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1585 emit_data->arg_count = 3;
1586 return;
1587 }
Tom Stellard467f5162012-05-16 15:15:35 -04001588
Michel Dänzer120efee2013-01-25 12:10:11 +01001589 /* Fetch and project texture coordinates */
1590 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001591 for (chan = 0; chan < 3; chan++ ) {
1592 coords[chan] = lp_build_emit_fetch(bld_base,
1593 emit_data->inst, 0,
1594 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001595 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001596 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1597 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001598 coords[chan],
1599 coords[3]);
1600 }
1601
Michel Dänzer120efee2013-01-25 12:10:11 +01001602 if (opcode == TGSI_OPCODE_TXP)
1603 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001604
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001605 /* Pack offsets. */
Marek Olšák877bb522014-06-25 03:12:46 +02001606 if (has_offset && opcode != TGSI_OPCODE_TXF) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001607 /* The offsets are six-bit signed integers packed like this:
1608 * X=[5:0], Y=[13:8], and Z=[21:16].
1609 */
1610 LLVMValueRef offset[3], pack;
1611
1612 assert(inst->Texture.NumOffsets == 1);
1613
1614 for (chan = 0; chan < 3; chan++) {
1615 offset[chan] = lp_build_emit_fetch_texoffset(bld_base,
1616 emit_data->inst, 0, chan);
1617 offset[chan] = LLVMBuildAnd(gallivm->builder, offset[chan],
1618 lp_build_const_int32(gallivm, 0x3f), "");
1619 if (chan)
1620 offset[chan] = LLVMBuildShl(gallivm->builder, offset[chan],
1621 lp_build_const_int32(gallivm, chan*8), "");
1622 }
1623
1624 pack = LLVMBuildOr(gallivm->builder, offset[0], offset[1], "");
1625 pack = LLVMBuildOr(gallivm->builder, pack, offset[2], "");
1626 address[count++] = pack;
1627 }
1628
Michel Dänzer120efee2013-01-25 12:10:11 +01001629 /* Pack LOD bias value */
1630 if (opcode == TGSI_OPCODE_TXB)
1631 address[count++] = coords[3];
Marek Olšák2484daa2014-04-22 21:23:29 +02001632 if (opcode == TGSI_OPCODE_TXB2)
1633 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001634
Michel Dänzer120efee2013-01-25 12:10:11 +01001635 /* Pack depth comparison value */
Marek Olšák57f3da92014-06-16 16:53:42 +02001636 if (tgsi_is_shadow_sampler(target) && opcode != TGSI_OPCODE_LODQ) {
Marek Olšák6818e112014-06-06 03:04:21 +02001637 if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
1638 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
1639 } else {
1640 assert(ref_pos >= 0);
1641 address[count++] = coords[ref_pos];
1642 }
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001643 }
1644
Marek Olšákb279f0142014-07-04 03:19:58 +02001645 if (target == TGSI_TEXTURE_CUBE ||
1646 target == TGSI_TEXTURE_CUBE_ARRAY ||
1647 target == TGSI_TEXTURE_SHADOWCUBE ||
1648 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1649 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
1650
Michel Dänzera6b83c02013-02-21 16:10:55 +01001651 /* Pack user derivatives */
1652 if (opcode == TGSI_OPCODE_TXD) {
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001653 int num_deriv_channels, param;
1654
1655 switch (target) {
1656 case TGSI_TEXTURE_3D:
1657 num_deriv_channels = 3;
1658 break;
1659 case TGSI_TEXTURE_2D:
1660 case TGSI_TEXTURE_SHADOW2D:
1661 case TGSI_TEXTURE_RECT:
1662 case TGSI_TEXTURE_SHADOWRECT:
1663 case TGSI_TEXTURE_2D_ARRAY:
1664 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1665 case TGSI_TEXTURE_CUBE:
1666 case TGSI_TEXTURE_SHADOWCUBE:
1667 case TGSI_TEXTURE_CUBE_ARRAY:
1668 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1669 num_deriv_channels = 2;
1670 break;
1671 case TGSI_TEXTURE_1D:
1672 case TGSI_TEXTURE_SHADOW1D:
1673 case TGSI_TEXTURE_1D_ARRAY:
1674 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1675 num_deriv_channels = 1;
1676 break;
1677 default:
1678 assert(0); /* no other targets are valid here */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001679 }
Marek Olšák04aa2bd2014-07-02 03:55:47 +02001680
1681 for (param = 1; param <= 2; param++)
1682 for (chan = 0; chan < num_deriv_channels; chan++)
1683 address[count++] = lp_build_emit_fetch(bld_base, inst, param, chan);
Michel Dänzera6b83c02013-02-21 16:10:55 +01001684 }
1685
Michel Dänzer120efee2013-01-25 12:10:11 +01001686 /* Pack texture coordinates */
1687 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001688 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001689 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001690 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001691 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001692
Marek Olšákd2bd6342013-09-18 15:40:21 +02001693 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001694 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001695 address[count++] = coords[3];
Marek Olšák6818e112014-06-06 03:04:21 +02001696 else if (opcode == TGSI_OPCODE_TXL2)
Marek Olšák2484daa2014-04-22 21:23:29 +02001697 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzer120efee2013-01-25 12:10:11 +01001698
1699 if (count > 16) {
1700 assert(!"Cannot handle more than 16 texture address parameters");
1701 count = 16;
1702 }
1703
1704 for (chan = 0; chan < count; chan++ ) {
1705 address[chan] = LLVMBuildBitCast(gallivm->builder,
1706 address[chan],
1707 LLVMInt32TypeInContext(gallivm->context),
1708 "");
1709 }
1710
Marek Olšák4855acd2013-08-06 15:08:54 +02001711 /* Adjust the sample index according to FMASK.
1712 *
1713 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1714 * which is the identity mapping. Each nibble says which physical sample
1715 * should be fetched to get that sample.
1716 *
1717 * For example, 0x11111100 means there are only 2 samples stored and
1718 * the second sample covers 3/4 of the pixel. When reading samples 0
1719 * and 1, return physical sample 0 (determined by the first two 0s
1720 * in FMASK), otherwise return physical sample 1.
1721 *
1722 * The sample index should be adjusted as follows:
1723 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1724 */
1725 if (target == TGSI_TEXTURE_2D_MSAA ||
1726 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1727 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1728 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001729 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001730 unsigned txf_count = count;
Marek Olšák877bb522014-06-25 03:12:46 +02001731 struct tgsi_full_instruction inst = {};
Marek Olšák4855acd2013-08-06 15:08:54 +02001732
Marek Olšákd2bd6342013-09-18 15:40:21 +02001733 memcpy(txf_address, address, sizeof(txf_address));
1734
1735 if (target == TGSI_TEXTURE_2D_MSAA) {
1736 txf_address[2] = bld_base->uint_bld.zero;
1737 }
1738 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001739
1740 /* Pad to a power-of-two size. */
1741 while (txf_count < util_next_power_of_two(txf_count))
1742 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1743
1744 /* Read FMASK using TXF. */
Marek Olšák877bb522014-06-25 03:12:46 +02001745 inst.Instruction.Opcode = TGSI_OPCODE_TXF;
1746 inst.Texture.Texture = target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY;
1747 txf_emit_data.inst = &inst;
Marek Olšák4855acd2013-08-06 15:08:54 +02001748 txf_emit_data.chan = 0;
1749 txf_emit_data.dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001750 LLVMInt32TypeInContext(gallivm->context), 4);
Marek Olšák4855acd2013-08-06 15:08:54 +02001751 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
Marek Olšákee2a8182014-07-07 23:27:19 +02001752 txf_emit_data.args[1] = si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index];
Marek Olšák877bb522014-06-25 03:12:46 +02001753 txf_emit_data.args[2] = lp_build_const_int32(gallivm, inst.Texture.Texture);
Marek Olšák4855acd2013-08-06 15:08:54 +02001754 txf_emit_data.arg_count = 3;
1755
Marek Olšák877bb522014-06-25 03:12:46 +02001756 build_tex_intrinsic(&tex_action, bld_base, &txf_emit_data);
Marek Olšák4855acd2013-08-06 15:08:54 +02001757
1758 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001759 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1760 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1761
1762 /* Apply the formula. */
1763 LLVMValueRef fmask =
1764 LLVMBuildExtractElement(gallivm->builder,
1765 txf_emit_data.output[0],
1766 uint_bld->zero, "");
1767
Marek Olšákd2bd6342013-09-18 15:40:21 +02001768 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001769
1770 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001771 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001772
1773 LLVMValueRef shifted_fmask =
1774 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1775
1776 LLVMValueRef final_sample =
1777 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1778
1779 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1780 * resource descriptor is 0 (invalid),
1781 */
1782 LLVMValueRef fmask_desc =
1783 LLVMBuildBitCast(gallivm->builder,
Marek Olšákee2a8182014-07-07 23:27:19 +02001784 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + sampler_index],
Marek Olšák4855acd2013-08-06 15:08:54 +02001785 LLVMVectorType(uint_bld->elem_type, 8), "");
1786
1787 LLVMValueRef fmask_word1 =
1788 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1789 uint_bld->one, "");
1790
1791 LLVMValueRef word1_is_nonzero =
1792 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1793 fmask_word1, uint_bld->zero, "");
1794
Marek Olšákd2bd6342013-09-18 15:40:21 +02001795 /* Replace the MSAA sample index. */
1796 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001797 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001798 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001799 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001800
Michel Dänzer36231112013-05-02 09:44:45 +02001801 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001802 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001803
1804 if (opcode == TGSI_OPCODE_TXF) {
1805 /* add tex offsets */
1806 if (inst->Texture.NumOffsets) {
1807 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1808 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1809 const struct tgsi_texture_offset * off = inst->TexOffsets;
1810
1811 assert(inst->Texture.NumOffsets == 1);
1812
Marek Olšákdefedc02013-09-18 15:36:38 +02001813 switch (target) {
1814 case TGSI_TEXTURE_3D:
1815 address[2] = lp_build_add(uint_bld, address[2],
1816 bld->immediates[off->Index][off->SwizzleZ]);
1817 /* fall through */
1818 case TGSI_TEXTURE_2D:
1819 case TGSI_TEXTURE_SHADOW2D:
1820 case TGSI_TEXTURE_RECT:
1821 case TGSI_TEXTURE_SHADOWRECT:
1822 case TGSI_TEXTURE_2D_ARRAY:
1823 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001824 address[1] =
1825 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001826 bld->immediates[off->Index][off->SwizzleY]);
1827 /* fall through */
1828 case TGSI_TEXTURE_1D:
1829 case TGSI_TEXTURE_SHADOW1D:
1830 case TGSI_TEXTURE_1D_ARRAY:
1831 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1832 address[0] =
1833 lp_build_add(uint_bld, address[0],
1834 bld->immediates[off->Index][off->SwizzleX]);
1835 break;
1836 /* texture offsets do not apply to other texture targets */
1837 }
Michel Dänzer36231112013-05-02 09:44:45 +02001838 }
1839
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001840 emit_data->args[2] = lp_build_const_int32(gallivm, target);
1841 emit_data->arg_count = 3;
1842
Michel Dänzer36231112013-05-02 09:44:45 +02001843 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001844 LLVMInt32TypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001845 4);
Marek Olšák57f3da92014-06-16 16:53:42 +02001846 } else if (opcode == TGSI_OPCODE_TG4 ||
Marek Olšák877bb522014-06-25 03:12:46 +02001847 opcode == TGSI_OPCODE_LODQ ||
1848 has_offset) {
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001849 unsigned is_array = target == TGSI_TEXTURE_1D_ARRAY ||
1850 target == TGSI_TEXTURE_SHADOW1D_ARRAY ||
1851 target == TGSI_TEXTURE_2D_ARRAY ||
1852 target == TGSI_TEXTURE_SHADOW2D_ARRAY ||
1853 target == TGSI_TEXTURE_CUBE_ARRAY ||
1854 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY;
1855 unsigned is_rect = target == TGSI_TEXTURE_RECT;
Marek Olšák57f3da92014-06-16 16:53:42 +02001856 unsigned dmask = 0xf;
Michel Dänzer36231112013-05-02 09:44:45 +02001857
Marek Olšák57f3da92014-06-16 16:53:42 +02001858 if (opcode == TGSI_OPCODE_TG4) {
1859 unsigned gather_comp = 0;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001860
Marek Olšák57f3da92014-06-16 16:53:42 +02001861 /* DMASK was repurposed for GATHER4. 4 components are always
1862 * returned and DMASK works like a swizzle - it selects
1863 * the component to fetch. The only valid DMASK values are
1864 * 1=red, 2=green, 4=blue, 8=alpha. (e.g. 1 returns
1865 * (red,red,red,red) etc.) The ISA document doesn't mention
1866 * this.
1867 */
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001868
Marek Olšák57f3da92014-06-16 16:53:42 +02001869 /* Get the component index from src1.x for Gather4. */
1870 if (!tgsi_is_shadow_sampler(target)) {
1871 LLVMValueRef (*imms)[4] = lp_soa_context(bld_base)->immediates;
1872 LLVMValueRef comp_imm;
1873 struct tgsi_src_register src1 = inst->Src[1].Register;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001874
Marek Olšák57f3da92014-06-16 16:53:42 +02001875 assert(src1.File == TGSI_FILE_IMMEDIATE);
1876
1877 comp_imm = imms[src1.Index][src1.SwizzleX];
1878 gather_comp = LLVMConstIntGetZExtValue(comp_imm);
1879 gather_comp = CLAMP(gather_comp, 0, 3);
1880 }
1881
1882 dmask = 1 << gather_comp;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001883 }
1884
Marek Olšák4855acd2013-08-06 15:08:54 +02001885 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Marek Olšák57f3da92014-06-16 16:53:42 +02001886 emit_data->args[3] = lp_build_const_int32(gallivm, dmask);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001887 emit_data->args[4] = lp_build_const_int32(gallivm, is_rect); /* unorm */
1888 emit_data->args[5] = lp_build_const_int32(gallivm, 0); /* r128 */
1889 emit_data->args[6] = lp_build_const_int32(gallivm, is_array); /* da */
1890 emit_data->args[7] = lp_build_const_int32(gallivm, 0); /* glc */
1891 emit_data->args[8] = lp_build_const_int32(gallivm, 0); /* slc */
1892 emit_data->args[9] = lp_build_const_int32(gallivm, 0); /* tfe */
1893 emit_data->args[10] = lp_build_const_int32(gallivm, 0); /* lwe */
1894
1895 emit_data->arg_count = 11;
Michel Dänzer36231112013-05-02 09:44:45 +02001896
1897 emit_data->dst_type = LLVMVectorType(
Marek Olšák6818e112014-06-06 03:04:21 +02001898 LLVMFloatTypeInContext(gallivm->context),
Michel Dänzer36231112013-05-02 09:44:45 +02001899 4);
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001900 } else {
1901 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
1902 emit_data->args[3] = lp_build_const_int32(gallivm, target);
Michel Dänzer36231112013-05-02 09:44:45 +02001903 emit_data->arg_count = 4;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001904
1905 emit_data->dst_type = LLVMVectorType(
1906 LLVMFloatTypeInContext(gallivm->context),
1907 4);
Michel Dänzer36231112013-05-02 09:44:45 +02001908 }
1909
Marek Olšák2484daa2014-04-22 21:23:29 +02001910 /* The fetch opcode has been converted to a 2D array fetch.
1911 * This simplifies the LLVM backend. */
1912 if (target == TGSI_TEXTURE_CUBE_ARRAY)
1913 target = TGSI_TEXTURE_2D_ARRAY;
1914 else if (target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
1915 target = TGSI_TEXTURE_SHADOW2D_ARRAY;
1916
Michel Dänzer120efee2013-01-25 12:10:11 +01001917 /* Pad to power of two vector */
1918 while (count < util_next_power_of_two(count))
1919 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1920
Christian Königccf3e8f2013-03-26 15:09:27 +01001921 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001922}
1923
Michel Dänzer07eddc42013-02-06 15:43:10 +01001924static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1925 struct lp_build_tgsi_context * bld_base,
1926 struct lp_build_emit_data * emit_data)
1927{
1928 struct lp_build_context * base = &bld_base->base;
Marek Olšák877bb522014-06-25 03:12:46 +02001929 unsigned opcode = emit_data->inst->Instruction.Opcode;
1930 unsigned target = emit_data->inst->Texture.Texture;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001931 char intr_name[127];
Marek Olšák877bb522014-06-25 03:12:46 +02001932 bool has_offset = HAVE_LLVM >= 0x0305 ?
1933 emit_data->inst->Texture.NumOffsets > 0 : false;
Michel Dänzer07eddc42013-02-06 15:43:10 +01001934
Marek Olšák877bb522014-06-25 03:12:46 +02001935 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001936 emit_data->output[emit_data->chan] = build_intrinsic(
1937 base->gallivm->builder,
1938 "llvm.SI.vs.load.input", emit_data->dst_type,
1939 emit_data->args, emit_data->arg_count,
1940 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1941 return;
1942 }
1943
Marek Olšák877bb522014-06-25 03:12:46 +02001944 if (opcode == TGSI_OPCODE_TG4 ||
1945 opcode == TGSI_OPCODE_LODQ ||
1946 (opcode != TGSI_OPCODE_TXF && has_offset)) {
1947 bool is_shadow = tgsi_is_shadow_sampler(target);
1948 const char *name = "llvm.SI.image.sample";
1949 const char *infix = "";
Michel Dänzer07eddc42013-02-06 15:43:10 +01001950
Marek Olšák877bb522014-06-25 03:12:46 +02001951 switch (opcode) {
1952 case TGSI_OPCODE_TEX:
1953 case TGSI_OPCODE_TEX2:
1954 case TGSI_OPCODE_TXP:
1955 break;
1956 case TGSI_OPCODE_TXB:
1957 case TGSI_OPCODE_TXB2:
1958 infix = ".b";
1959 break;
1960 case TGSI_OPCODE_TXL:
1961 case TGSI_OPCODE_TXL2:
1962 infix = ".l";
1963 break;
1964 case TGSI_OPCODE_TXD:
1965 infix = ".d";
1966 break;
1967 case TGSI_OPCODE_TG4:
1968 name = "llvm.SI.gather4";
1969 break;
1970 case TGSI_OPCODE_LODQ:
1971 name = "llvm.SI.getlod";
1972 is_shadow = false;
1973 has_offset = false;
1974 break;
1975 default:
1976 assert(0);
1977 return;
1978 }
Michel Dänzer07eddc42013-02-06 15:43:10 +01001979
Marek Olšák877bb522014-06-25 03:12:46 +02001980 /* Add the type and suffixes .c, .o if needed. */
1981 sprintf(intr_name, "%s%s%s%s.v%ui32", name,
1982 is_shadow ? ".c" : "", infix, has_offset ? ".o" : "",
1983 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001984
Marek Olšák877bb522014-06-25 03:12:46 +02001985 emit_data->output[emit_data->chan] = build_intrinsic(
1986 base->gallivm->builder, intr_name, emit_data->dst_type,
1987 emit_data->args, emit_data->arg_count,
1988 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1989 } else {
Marek Olšákd859bdb2014-07-14 19:40:14 +02001990 LLVMTypeRef i8, v16i8, v32i8;
Marek Olšák877bb522014-06-25 03:12:46 +02001991 const char *name;
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02001992
Marek Olšák877bb522014-06-25 03:12:46 +02001993 switch (opcode) {
1994 case TGSI_OPCODE_TEX:
1995 case TGSI_OPCODE_TEX2:
1996 case TGSI_OPCODE_TXP:
1997 name = "llvm.SI.sample";
1998 break;
1999 case TGSI_OPCODE_TXB:
2000 case TGSI_OPCODE_TXB2:
2001 name = "llvm.SI.sampleb";
2002 break;
2003 case TGSI_OPCODE_TXD:
2004 name = "llvm.SI.sampled";
2005 break;
2006 case TGSI_OPCODE_TXF:
2007 name = "llvm.SI.imageload";
2008 break;
2009 case TGSI_OPCODE_TXL:
2010 case TGSI_OPCODE_TXL2:
2011 name = "llvm.SI.samplel";
2012 break;
2013 default:
2014 assert(0);
2015 return;
2016 }
2017
Marek Olšákd859bdb2014-07-14 19:40:14 +02002018 i8 = LLVMInt8TypeInContext(base->gallivm->context);
2019 v16i8 = LLVMVectorType(i8, 16);
2020 v32i8 = LLVMVectorType(i8, 32);
2021
2022 emit_data->args[1] = LLVMBuildBitCast(base->gallivm->builder,
2023 emit_data->args[1], v32i8, "");
2024 if (opcode != TGSI_OPCODE_TXF) {
2025 emit_data->args[2] = LLVMBuildBitCast(base->gallivm->builder,
2026 emit_data->args[2], v16i8, "");
2027 }
2028
Marek Olšák877bb522014-06-25 03:12:46 +02002029 sprintf(intr_name, "%s.v%ui32", name,
2030 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
2031
2032 emit_data->output[emit_data->chan] = build_intrinsic(
2033 base->gallivm->builder, intr_name, emit_data->dst_type,
2034 emit_data->args, emit_data->arg_count,
2035 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
2036 }
Marek Olšákc7b5a5c2014-06-06 03:00:18 +02002037}
2038
Michel Dänzer0495adb2013-05-06 12:45:14 +02002039static void txq_fetch_args(
2040 struct lp_build_tgsi_context * bld_base,
2041 struct lp_build_emit_data * emit_data)
2042{
2043 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2044 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002045 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák2484daa2014-04-22 21:23:29 +02002046 unsigned target = inst->Texture.Texture;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002047
Marek Olšák2484daa2014-04-22 21:23:29 +02002048 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002049 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
2050 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
2051
2052 /* Read the size from the buffer descriptor directly. */
2053 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
2054 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
2055 size = LLVMBuildExtractElement(gallivm->builder, size,
2056 lp_build_const_int32(gallivm, 2), "");
2057 emit_data->args[0] = size;
2058 return;
2059 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02002060
2061 /* Mip level */
2062 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
2063
2064 /* Resource */
2065 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
2066
Marek Olšák2484daa2014-04-22 21:23:29 +02002067 /* Texture target */
2068 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2069 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY)
2070 target = TGSI_TEXTURE_2D_ARRAY;
2071
Michel Dänzer0495adb2013-05-06 12:45:14 +02002072 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
Marek Olšák6818e112014-06-06 03:04:21 +02002073 target);
Michel Dänzer0495adb2013-05-06 12:45:14 +02002074
2075 emit_data->arg_count = 3;
2076
2077 emit_data->dst_type = LLVMVectorType(
2078 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
2079 4);
2080}
2081
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002082static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
2083 struct lp_build_tgsi_context * bld_base,
2084 struct lp_build_emit_data * emit_data)
2085{
Marek Olšák2484daa2014-04-22 21:23:29 +02002086 unsigned target = emit_data->inst->Texture.Texture;
2087
2088 if (target == TGSI_TEXTURE_BUFFER) {
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002089 /* Just return the buffer size. */
2090 emit_data->output[emit_data->chan] = emit_data->args[0];
2091 return;
2092 }
2093
2094 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
Marek Olšák2484daa2014-04-22 21:23:29 +02002095
2096 /* Divide the number of layers by 6 to get the number of cubes. */
2097 if (target == TGSI_TEXTURE_CUBE_ARRAY ||
2098 target == TGSI_TEXTURE_SHADOWCUBE_ARRAY) {
2099 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2100 LLVMValueRef two = lp_build_const_int32(bld_base->base.gallivm, 2);
2101 LLVMValueRef six = lp_build_const_int32(bld_base->base.gallivm, 6);
2102
2103 LLVMValueRef v4 = emit_data->output[emit_data->chan];
2104 LLVMValueRef z = LLVMBuildExtractElement(builder, v4, two, "");
2105 z = LLVMBuildSDiv(builder, z, six, "");
2106
2107 emit_data->output[emit_data->chan] =
2108 LLVMBuildInsertElement(builder, v4, z, two, "");
2109 }
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002110}
2111
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002112static void si_llvm_emit_ddxy(
2113 const struct lp_build_tgsi_action * action,
2114 struct lp_build_tgsi_context * bld_base,
2115 struct lp_build_emit_data * emit_data)
2116{
2117 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2118 struct gallivm_state *gallivm = bld_base->base.gallivm;
2119 struct lp_build_context * base = &bld_base->base;
2120 const struct tgsi_full_instruction *inst = emit_data->inst;
2121 unsigned opcode = inst->Instruction.Opcode;
2122 LLVMValueRef indices[2];
2123 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
2124 LLVMValueRef tl, trbl, result[4];
2125 LLVMTypeRef i32;
2126 unsigned swizzle[4];
2127 unsigned c;
2128
2129 i32 = LLVMInt32TypeInContext(gallivm->context);
2130
2131 indices[0] = bld_base->uint_bld.zero;
2132 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
2133 NULL, 0, LLVMReadNoneAttribute);
2134 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2135 indices, 2, "");
2136
2137 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
2138 lp_build_const_int32(gallivm, 0xfffffffc), "");
2139 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2140 indices, 2, "");
2141
2142 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
2143 lp_build_const_int32(gallivm,
2144 opcode == TGSI_OPCODE_DDX ? 1 : 2),
2145 "");
2146 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
2147 indices, 2, "");
2148
2149 for (c = 0; c < 4; ++c) {
2150 unsigned i;
2151
2152 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
2153 for (i = 0; i < c; ++i) {
2154 if (swizzle[i] == swizzle[c]) {
2155 result[c] = result[i];
2156 break;
2157 }
2158 }
2159 if (i != c)
2160 continue;
2161
2162 LLVMBuildStore(gallivm->builder,
2163 LLVMBuildBitCast(gallivm->builder,
2164 lp_build_emit_fetch(bld_base, inst, 0, c),
2165 i32, ""),
2166 store_ptr);
2167
2168 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
2169 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
2170
2171 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
2172 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
2173
2174 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
2175 }
2176
2177 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
2178}
2179
Michel Dänzer404b29d2013-11-21 16:45:28 +09002180/* Emit one vertex from the geometry shader */
2181static void si_llvm_emit_vertex(
2182 const struct lp_build_tgsi_action *action,
2183 struct lp_build_tgsi_context *bld_base,
2184 struct lp_build_emit_data *emit_data)
2185{
2186 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002187 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002188 struct si_shader *shader = si_shader_ctx->shader;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002189 struct gallivm_state *gallivm = bld_base->base.gallivm;
2190 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
Michel Dänzerdb9d6af2014-01-24 16:46:27 +09002191 LLVMValueRef soffset = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2192 SI_PARAM_GS2VS_OFFSET);
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002193 LLVMValueRef gs_next_vertex;
Michel Daenzer59936a42014-02-13 15:37:11 +09002194 LLVMValueRef can_emit, kill;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002195 LLVMValueRef t_list_ptr;
2196 LLVMValueRef t_list;
2197 LLVMValueRef args[2];
2198 unsigned chan;
2199 int i;
2200
2201 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002202 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2203 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002204 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002205 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002206
2207 if (shader->noutput == 0) {
2208 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
2209
2210 while (!tgsi_parse_end_of_tokens(parse)) {
2211 tgsi_parse_token(parse);
2212
Michel Dänzer7c7d7382014-01-09 15:33:34 +09002213 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_DECLARATION) {
2214 struct tgsi_full_declaration *d = &parse->FullToken.FullDeclaration;
2215
2216 if (d->Declaration.File == TGSI_FILE_OUTPUT)
2217 si_store_shader_io_attribs(shader, d);
2218 }
Michel Dänzer404b29d2013-11-21 16:45:28 +09002219 }
2220 }
2221
2222 /* Write vertex attribute values to GSVS ring */
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002223 gs_next_vertex = LLVMBuildLoad(gallivm->builder, si_shader_ctx->gs_next_vertex, "");
Michel Daenzer59936a42014-02-13 15:37:11 +09002224
2225 /* If this thread has already emitted the declared maximum number of
2226 * vertices, kill it: excessive vertex emissions are not supposed to
2227 * have any effect, and GS threads have no externally observable
2228 * effects other than emitting vertices.
2229 */
2230 can_emit = LLVMBuildICmp(gallivm->builder, LLVMIntULE, gs_next_vertex,
2231 lp_build_const_int32(gallivm,
2232 shader->gs_max_out_vertices), "");
2233 kill = lp_build_select(&bld_base->base, can_emit,
2234 lp_build_const_float(gallivm, 1.0f),
2235 lp_build_const_float(gallivm, -1.0f));
2236 build_intrinsic(gallivm->builder, "llvm.AMDGPU.kill",
2237 LLVMVoidTypeInContext(gallivm->context), &kill, 1, 0);
2238
Michel Dänzer404b29d2013-11-21 16:45:28 +09002239 for (i = 0; i < shader->noutput; i++) {
2240 LLVMValueRef *out_ptr =
2241 si_shader_ctx->radeon_bld.soa.outputs[shader->output[i].index];
2242
2243 for (chan = 0; chan < 4; chan++) {
2244 LLVMValueRef out_val = LLVMBuildLoad(gallivm->builder, out_ptr[chan], "");
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002245 LLVMValueRef voffset =
2246 lp_build_const_int32(gallivm, (i * 4 + chan) *
2247 shader->gs_max_out_vertices);
2248
2249 voffset = lp_build_add(uint, voffset, gs_next_vertex);
2250 voffset = lp_build_mul_imm(uint, voffset, 4);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002251
2252 out_val = LLVMBuildBitCast(gallivm->builder, out_val, i32, "");
2253
2254 build_tbuffer_store(si_shader_ctx, t_list, out_val, 1,
2255 voffset, soffset, 0,
2256 V_008F0C_BUF_DATA_FORMAT_32,
2257 V_008F0C_BUF_NUM_FORMAT_UINT,
2258 1, 0, 1, 1, 0);
2259 }
2260 }
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002261 gs_next_vertex = lp_build_add(uint, gs_next_vertex,
2262 lp_build_const_int32(gallivm, 1));
2263 LLVMBuildStore(gallivm->builder, gs_next_vertex, si_shader_ctx->gs_next_vertex);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002264
2265 /* Signal vertex emission */
2266 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_EMIT | SENDMSG_GS);
2267 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2268 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2269 LLVMVoidTypeInContext(gallivm->context), args, 2,
2270 LLVMNoUnwindAttribute);
2271}
2272
2273/* Cut one primitive from the geometry shader */
2274static void si_llvm_emit_primitive(
2275 const struct lp_build_tgsi_action *action,
2276 struct lp_build_tgsi_context *bld_base,
2277 struct lp_build_emit_data *emit_data)
2278{
2279 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
2280 struct gallivm_state *gallivm = bld_base->base.gallivm;
2281 LLVMValueRef args[2];
2282
2283 /* Signal primitive cut */
2284 args[0] = lp_build_const_int32(gallivm, SENDMSG_GS_OP_CUT | SENDMSG_GS);
2285 args[1] = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_GS_WAVE_ID);
2286 build_intrinsic(gallivm->builder, "llvm.SI.sendmsg",
2287 LLVMVoidTypeInContext(gallivm->context), args, 2,
2288 LLVMNoUnwindAttribute);
2289}
2290
Tom Stellarda75c6162012-01-06 17:38:37 -05002291static const struct lp_build_tgsi_action tex_action = {
2292 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01002293 .emit = build_tex_intrinsic,
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002294};
2295
Michel Dänzer0495adb2013-05-06 12:45:14 +02002296static const struct lp_build_tgsi_action txq_action = {
2297 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01002298 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02002299 .intr_name = "llvm.SI.resinfo"
2300};
2301
Christian König206f0592013-03-20 14:37:21 +01002302static void create_meta_data(struct si_shader_context *si_shader_ctx)
2303{
2304 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
2305 LLVMValueRef args[3];
2306
2307 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
2308 args[1] = 0;
2309 args[2] = lp_build_const_int32(gallivm, 1);
2310
2311 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
2312}
2313
Marek Olšák4f3f0432014-07-07 22:49:15 +02002314static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
2315{
2316 return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
2317 CONST_ADDR_SPACE);
2318}
2319
Christian König55fe5cc2013-03-04 16:30:06 +01002320static void create_function(struct si_shader_context *si_shader_ctx)
2321{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002322 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2323 struct gallivm_state *gallivm = bld_base->base.gallivm;
Marek Olšák8c37c162014-09-16 18:40:07 +02002324 struct si_shader *shader = si_shader_ctx->shader;
Marek Olšák4f3f0432014-07-07 22:49:15 +02002325 LLVMTypeRef params[SI_NUM_PARAMS], f32, i8, i32, v2i32, v3i32, v16i8, v4i32, v8i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002326 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01002327
Christian König55fe5cc2013-03-04 16:30:06 +01002328 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01002329 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01002330 f32 = LLVMFloatTypeInContext(gallivm->context);
2331 v2i32 = LLVMVectorType(i32, 2);
2332 v3i32 = LLVMVectorType(i32, 3);
Marek Olšák4f3f0432014-07-07 22:49:15 +02002333 v4i32 = LLVMVectorType(i32, 4);
2334 v8i32 = LLVMVectorType(i32, 8);
2335 v16i8 = LLVMVectorType(i8, 16);
Christian Königc4973212013-03-05 12:14:02 +01002336
Marek Olšákee2a8182014-07-07 23:27:19 +02002337 params[SI_PARAM_CONST] = const_array(v16i8, SI_NUM_CONST_BUFFERS);
2338 params[SI_PARAM_RW_BUFFERS] = const_array(v16i8, SI_NUM_RW_BUFFERS);
2339 params[SI_PARAM_SAMPLER] = const_array(v4i32, SI_NUM_SAMPLER_STATES);
2340 params[SI_PARAM_RESOURCE] = const_array(v8i32, SI_NUM_SAMPLER_VIEWS);
Christian König55fe5cc2013-03-04 16:30:06 +01002341
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002342 switch (si_shader_ctx->type) {
2343 case TGSI_PROCESSOR_VERTEX:
Marek Olšákee2a8182014-07-07 23:27:19 +02002344 params[SI_PARAM_VERTEX_BUFFER] = const_array(v16i8, SI_NUM_VERTEX_BUFFERS);
Marek Olšák09056b32014-04-23 16:15:36 +02002345 params[SI_PARAM_BASE_VERTEX] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01002346 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02002347 num_params = SI_PARAM_START_INSTANCE+1;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002348 if (shader->key.vs.as_es) {
2349 params[SI_PARAM_ES2GS_OFFSET] = i32;
2350 num_params++;
2351 } else {
2352 /* The locations of the other parameters are assigned dynamically. */
Marek Olšák8d03d922013-09-01 23:59:06 +02002353
Michel Dänzer404b29d2013-11-21 16:45:28 +09002354 /* Streamout SGPRs. */
2355 if (shader->selector->so.num_outputs) {
2356 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
2357 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
2358 }
2359 /* A streamout buffer offset is loaded if the stride is non-zero. */
2360 for (i = 0; i < 4; i++) {
2361 if (!shader->selector->so.stride[i])
2362 continue;
Marek Olšák8d03d922013-09-01 23:59:06 +02002363
Michel Dänzer404b29d2013-11-21 16:45:28 +09002364 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
2365 }
Marek Olšák8d03d922013-09-01 23:59:06 +02002366 }
2367
2368 last_sgpr = num_params-1;
2369
2370 /* VGPRs */
2371 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
2372 params[num_params++] = i32; /* unused*/
2373 params[num_params++] = i32; /* unused */
2374 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002375 break;
Christian König0666ffd2013-03-05 15:07:39 +01002376
Michel Dänzer404b29d2013-11-21 16:45:28 +09002377 case TGSI_PROCESSOR_GEOMETRY:
2378 params[SI_PARAM_GS2VS_OFFSET] = i32;
2379 params[SI_PARAM_GS_WAVE_ID] = i32;
2380 last_sgpr = SI_PARAM_GS_WAVE_ID;
2381
2382 /* VGPRs */
2383 params[SI_PARAM_VTX0_OFFSET] = i32;
2384 params[SI_PARAM_VTX1_OFFSET] = i32;
2385 params[SI_PARAM_PRIMITIVE_ID] = i32;
2386 params[SI_PARAM_VTX2_OFFSET] = i32;
2387 params[SI_PARAM_VTX3_OFFSET] = i32;
2388 params[SI_PARAM_VTX4_OFFSET] = i32;
2389 params[SI_PARAM_VTX5_OFFSET] = i32;
2390 params[SI_PARAM_GS_INSTANCE_ID] = i32;
2391 num_params = SI_PARAM_GS_INSTANCE_ID+1;
2392 break;
2393
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002394 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04002395 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01002396 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002397 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01002398 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
2399 params[SI_PARAM_PERSP_CENTER] = v2i32;
2400 params[SI_PARAM_PERSP_CENTROID] = v2i32;
2401 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
2402 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
2403 params[SI_PARAM_LINEAR_CENTER] = v2i32;
2404 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
2405 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
2406 params[SI_PARAM_POS_X_FLOAT] = f32;
2407 params[SI_PARAM_POS_Y_FLOAT] = f32;
2408 params[SI_PARAM_POS_Z_FLOAT] = f32;
2409 params[SI_PARAM_POS_W_FLOAT] = f32;
2410 params[SI_PARAM_FRONT_FACE] = f32;
Marek Olšák5b06fc32014-05-06 18:12:40 +02002411 params[SI_PARAM_ANCILLARY] = i32;
Christian König0666ffd2013-03-05 15:07:39 +01002412 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
2413 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002414 num_params = SI_PARAM_POS_FIXED_PT+1;
2415 break;
2416
2417 default:
2418 assert(0 && "unimplemented shader");
2419 return;
Christian Königc4973212013-03-05 12:14:02 +01002420 }
Christian König55fe5cc2013-03-04 16:30:06 +01002421
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002422 assert(num_params <= Elements(params));
2423 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01002424 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01002425
Marek Olšák13a1a8b2013-08-18 01:57:40 +02002426 for (i = 0; i <= last_sgpr; ++i) {
2427 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002428 switch (i) {
2429 default:
2430 LLVMAddAttribute(P, LLVMInRegAttribute);
2431 break;
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002432 /* We tell llvm that array inputs are passed by value to allow Sinking pass
2433 * to move load. Inputs are constant so this is fine. */
2434 case SI_PARAM_CONST:
2435 case SI_PARAM_SAMPLER:
2436 case SI_PARAM_RESOURCE:
2437 LLVMAddAttribute(P, LLVMByValAttribute);
2438 break;
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02002439 }
Christian Königcf9b31f2013-03-21 18:30:23 +01002440 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002441
Michel Dänzer404b29d2013-11-21 16:45:28 +09002442 if (bld_base->info &&
2443 (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
2444 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0))
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002445 si_shader_ctx->ddxy_lds =
2446 LLVMAddGlobalInAddressSpace(gallivm->module,
2447 LLVMArrayType(i32, 64),
2448 "ddxy_lds",
2449 LOCAL_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01002450}
Tom Stellarda75c6162012-01-06 17:38:37 -05002451
Christian König0f6cf2b2013-03-15 15:53:25 +01002452static void preload_constants(struct si_shader_context *si_shader_ctx)
2453{
2454 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2455 struct gallivm_state * gallivm = bld_base->base.gallivm;
2456 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02002457 unsigned buf;
2458 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01002459
Marek Olšákee2a8182014-07-07 23:27:19 +02002460 for (buf = 0; buf < SI_NUM_CONST_BUFFERS; buf++) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002461 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01002462
Marek Olšák2fd42002013-10-25 11:45:47 +02002463 if (num_const == 0)
2464 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01002465
Marek Olšák2fd42002013-10-25 11:45:47 +02002466 /* Allocate space for the constant values */
2467 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01002468
Marek Olšák2fd42002013-10-25 11:45:47 +02002469 /* Load the resource descriptor */
2470 si_shader_ctx->const_resource[buf] =
2471 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01002472
Marek Olšák2fd42002013-10-25 11:45:47 +02002473 /* Load the constants, we rely on the code sinking to do the rest */
2474 for (i = 0; i < num_const * 4; ++i) {
Marek Olšák2fd42002013-10-25 11:45:47 +02002475 si_shader_ctx->constants[buf][i] =
Marek Olšák250aa932014-05-06 14:10:47 +02002476 load_const(gallivm->builder,
2477 si_shader_ctx->const_resource[buf],
2478 lp_build_const_int32(gallivm, i * 4),
2479 bld_base->base.elem_type);
Marek Olšák2fd42002013-10-25 11:45:47 +02002480 }
Christian König0f6cf2b2013-03-15 15:53:25 +01002481 }
2482}
2483
Christian König1c100182013-03-17 16:02:42 +01002484static void preload_samplers(struct si_shader_context *si_shader_ctx)
2485{
2486 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2487 struct gallivm_state * gallivm = bld_base->base.gallivm;
2488 const struct tgsi_shader_info * info = bld_base->info;
2489
2490 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
2491
2492 LLVMValueRef res_ptr, samp_ptr;
2493 LLVMValueRef offset;
2494
2495 if (num_samplers == 0)
2496 return;
2497
2498 /* Allocate space for the values */
Marek Olšákee2a8182014-07-07 23:27:19 +02002499 si_shader_ctx->resources = CALLOC(SI_NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01002500 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
2501
2502 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
2503 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
2504
2505 /* Load the resources and samplers, we rely on the code sinking to do the rest */
2506 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01002507 /* Resource */
2508 offset = lp_build_const_int32(gallivm, i);
2509 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
2510
2511 /* Sampler */
2512 offset = lp_build_const_int32(gallivm, i);
2513 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02002514
2515 /* FMASK resource */
2516 if (info->is_msaa_sampler[i]) {
Marek Olšákee2a8182014-07-07 23:27:19 +02002517 offset = lp_build_const_int32(gallivm, SI_FMASK_TEX_OFFSET + i);
2518 si_shader_ctx->resources[SI_FMASK_TEX_OFFSET + i] =
Marek Olšák4855acd2013-08-06 15:08:54 +02002519 build_indexed_load(si_shader_ctx, res_ptr, offset);
2520 }
Christian König1c100182013-03-17 16:02:42 +01002521 }
2522}
2523
Marek Olšák8d03d922013-09-01 23:59:06 +02002524static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
2525{
2526 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2527 struct gallivm_state * gallivm = bld_base->base.gallivm;
2528 unsigned i;
2529
Michel Dänzer67e385b2014-01-08 17:48:21 +09002530 if (si_shader_ctx->type != TGSI_PROCESSOR_VERTEX ||
2531 si_shader_ctx->shader->key.vs.as_es ||
2532 !si_shader_ctx->shader->selector->so.num_outputs)
Marek Olšák8d03d922013-09-01 23:59:06 +02002533 return;
2534
2535 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
Michel Dänzerf8e16012014-01-28 15:39:30 +09002536 SI_PARAM_RW_BUFFERS);
Marek Olšák8d03d922013-09-01 23:59:06 +02002537
2538 /* Load the resources, we rely on the code sinking to do the rest */
2539 for (i = 0; i < 4; ++i) {
2540 if (si_shader_ctx->shader->selector->so.stride[i]) {
Michel Dänzerf8e16012014-01-28 15:39:30 +09002541 LLVMValueRef offset = lp_build_const_int32(gallivm,
Marek Olšákee2a8182014-07-07 23:27:19 +02002542 SI_SO_BUF_OFFSET + i);
Marek Olšák8d03d922013-09-01 23:59:06 +02002543
2544 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
2545 }
2546 }
2547}
2548
Marek Olšák8c37c162014-09-16 18:40:07 +02002549int si_compile_llvm(struct si_context *sctx, struct si_shader *shader,
Tom Stellard302f53d2012-10-25 13:50:10 -04002550 LLVMModuleRef mod)
2551{
Darren Powellbc866902014-03-31 18:00:28 -04002552 unsigned r; /* llvm_compile result */
Tom Stellard302f53d2012-10-25 13:50:10 -04002553 unsigned i;
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002554 unsigned char *ptr;
Tom Stellard1f4a9fc2014-02-03 13:50:09 -05002555 struct radeon_shader_binary binary;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002556 bool dump = r600_can_dump_shader(&sctx->screen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04002557 shader->selector ? shader->selector->tokens : NULL);
Darren Powellbc866902014-03-31 18:00:28 -04002558 const char * gpu_family = r600_get_llvm_processor_name(sctx->screen->b.family);
Tom Stellard9ba31052014-07-14 16:49:08 -04002559 unsigned code_size;
Darren Powellbc866902014-03-31 18:00:28 -04002560
2561 /* Use LLVM to compile shader */
Tom Stellard7782d192013-04-04 09:57:13 -07002562 memset(&binary, 0, sizeof(binary));
Darren Powellbc866902014-03-31 18:00:28 -04002563 r = radeon_llvm_compile(mod, &binary, gpu_family, dump);
2564
2565 /* Output binary dump if rscreen->debug_flags are set */
Jay Cornwalld7d539a2013-10-10 20:06:48 -05002566 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04002567 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07002568 for (i = 0; i < binary.code_size; i+=4 ) {
2569 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
2570 binary.code[i + 2], binary.code[i + 1],
2571 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04002572 }
2573 }
2574
Tom Stellardd50343d2013-04-04 16:21:06 -04002575 /* XXX: We may be able to emit some of these values directly rather than
2576 * extracting fields to be emitted later.
2577 */
Darren Powellbc866902014-03-31 18:00:28 -04002578 /* Parse config data in compiled binary */
Tom Stellardd50343d2013-04-04 16:21:06 -04002579 for (i = 0; i < binary.config_size; i+= 8) {
2580 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
2581 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
2582 switch (reg) {
2583 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
2584 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
2585 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
2586 case R_00B848_COMPUTE_PGM_RSRC1:
2587 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
2588 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
2589 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002590 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
2591 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
2592 break;
2593 case R_00B84C_COMPUTE_PGM_RSRC2:
2594 shader->lds_size = G_00B84C_LDS_SIZE(value);
2595 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002596 case R_0286CC_SPI_PS_INPUT_ENA:
2597 shader->spi_ps_input_ena = value;
2598 break;
Tom Stellardb0f78032014-07-18 14:45:18 -04002599 case R_00B860_COMPUTE_TMPRING_SIZE:
2600 /* WAVESIZE is in units of 256 dwords. */
2601 shader->scratch_bytes_per_wave =
2602 G_00B860_WAVESIZE(value) * 256 * 4 * 1;
2603 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04002604 default:
2605 fprintf(stderr, "Warning: Compiler emitted unknown "
2606 "config register: 0x%x\n", reg);
2607 break;
2608 }
2609 }
Tom Stellard302f53d2012-10-25 13:50:10 -04002610
2611 /* copy new shader */
Tom Stellard9ba31052014-07-14 16:49:08 -04002612 code_size = binary.code_size + binary.rodata_size;
Marek Olšáka81c3e02013-08-14 01:04:39 +02002613 r600_resource_reference(&shader->bo, NULL);
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002614 shader->bo = si_resource_create_custom(sctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellard9ba31052014-07-14 16:49:08 -04002615 code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002616 if (shader->bo == NULL) {
2617 return -ENOMEM;
2618 }
2619
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002620 ptr = sctx->b.ws->buffer_map(shader->bo->cs_buf, sctx->b.rings.gfx.cs, PIPE_TRANSFER_WRITE);
2621 util_memcpy_cpu_to_le32(ptr, binary.code, binary.code_size);
2622 if (binary.rodata_size > 0) {
2623 ptr += binary.code_size;
2624 util_memcpy_cpu_to_le32(ptr, binary.rodata, binary.rodata_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04002625 }
Tom Stellard6f0c1f22014-07-18 15:10:52 -04002626
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002627 sctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04002628
Tom Stellard7782d192013-04-04 09:57:13 -07002629 free(binary.code);
2630 free(binary.config);
Tom Stellard9ba31052014-07-14 16:49:08 -04002631 free(binary.rodata);
Tom Stellard302f53d2012-10-25 13:50:10 -04002632
Darren Powellbc866902014-03-31 18:00:28 -04002633 return r;
Tom Stellard302f53d2012-10-25 13:50:10 -04002634}
2635
Michel Dänzer404b29d2013-11-21 16:45:28 +09002636/* Generate code for the hardware VS shader stage to go with a geometry shader */
2637static int si_generate_gs_copy_shader(struct si_context *sctx,
2638 struct si_shader_context *si_shader_ctx,
2639 bool dump)
2640{
2641 struct gallivm_state *gallivm = &si_shader_ctx->radeon_bld.gallivm;
2642 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
2643 struct lp_build_context *base = &bld_base->base;
2644 struct lp_build_context *uint = &bld_base->uint_bld;
Marek Olšák8c37c162014-09-16 18:40:07 +02002645 struct si_shader *shader = si_shader_ctx->shader;
2646 struct si_shader *gs = si_shader_ctx->shader->selector->current;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002647 struct si_shader_output_values *outputs;
2648 LLVMValueRef t_list_ptr, t_list;
2649 LLVMValueRef args[9];
2650 int i, r;
2651
2652 outputs = MALLOC(gs->noutput * sizeof(outputs[0]));
2653
2654 si_shader_ctx->type = TGSI_PROCESSOR_VERTEX;
2655 si_shader_ctx->gs_for_vs = gs;
2656
2657 radeon_llvm_context_init(&si_shader_ctx->radeon_bld);
2658
2659 create_meta_data(si_shader_ctx);
2660 create_function(si_shader_ctx);
2661 preload_streamout_buffers(si_shader_ctx);
2662
2663 /* Load the GSVS ring resource descriptor */
Michel Dänzerf8e16012014-01-28 15:39:30 +09002664 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2665 SI_PARAM_RW_BUFFERS);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002666 t_list = build_indexed_load(si_shader_ctx, t_list_ptr,
Michel Dänzerb4e14932014-01-22 17:32:09 +09002667 lp_build_const_int32(gallivm, SI_RING_GSVS));
Michel Dänzer404b29d2013-11-21 16:45:28 +09002668
2669 args[0] = t_list;
2670 args[1] = lp_build_mul_imm(uint,
2671 LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
2672 si_shader_ctx->param_vertex_id),
2673 4);
2674 args[3] = uint->zero;
2675 args[4] = uint->one; /* OFFEN */
2676 args[5] = uint->zero; /* IDXEN */
2677 args[6] = uint->one; /* GLC */
2678 args[7] = uint->one; /* SLC */
2679 args[8] = uint->zero; /* TFE */
2680
2681 /* Fetch vertex data from GSVS ring */
2682 for (i = 0; i < gs->noutput; ++i) {
2683 struct si_shader_output *out = gs->output + i;
2684 unsigned chan;
2685
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002686 shader->output[i] = *out;
2687
Michel Dänzer404b29d2013-11-21 16:45:28 +09002688 outputs[i].name = out->name;
2689 outputs[i].index = out->index;
Michel Dänzer67e385b2014-01-08 17:48:21 +09002690 outputs[i].sid = out->sid;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002691 outputs[i].usage = out->usage;
2692
2693 for (chan = 0; chan < 4; chan++) {
2694 args[2] = lp_build_const_int32(gallivm,
2695 (i * 4 + chan) *
2696 gs->gs_max_out_vertices * 16 * 4);
2697
2698 outputs[i].values[chan] =
2699 LLVMBuildBitCast(gallivm->builder,
2700 build_intrinsic(gallivm->builder,
2701 "llvm.SI.buffer.load.dword.i32.i32",
2702 LLVMInt32TypeInContext(gallivm->context),
2703 args, 9,
2704 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute),
2705 base->elem_type, "");
2706 }
2707 }
Michel Dänzer8afde9f2014-01-09 16:10:49 +09002708 shader->noutput = gs->noutput;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002709
2710 si_llvm_export_vs(bld_base, outputs, gs->noutput);
2711
2712 radeon_llvm_finalize_module(&si_shader_ctx->radeon_bld);
2713
2714 if (dump)
2715 fprintf(stderr, "Copy Vertex Shader for Geometry Shader:\n\n");
2716
2717 r = si_compile_llvm(sctx, si_shader_ctx->shader,
2718 bld_base->base.gallivm->module);
2719
2720 radeon_llvm_dispose(&si_shader_ctx->radeon_bld);
2721
2722 FREE(outputs);
2723 return r;
2724}
2725
Marek Olšák2774abd2014-09-16 18:45:33 +02002726int si_shader_create(
Tom Stellarda75c6162012-01-06 17:38:37 -05002727 struct pipe_context *ctx,
Marek Olšák8c37c162014-09-16 18:40:07 +02002728 struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002729{
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002730 struct si_context *sctx = (struct si_context*)ctx;
Marek Olšák2774abd2014-09-16 18:45:33 +02002731 struct si_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002732 struct si_shader_context si_shader_ctx;
2733 struct tgsi_shader_info shader_info;
2734 struct lp_build_tgsi_context * bld_base;
2735 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002736 int r = 0;
Michel Dänzere1df0d42014-01-15 12:31:07 +09002737 bool dump = r600_can_dump_shader(&sctx->screen->b, sel->tokens);
2738
2739 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2740 * conversion fails. */
2741 if (dump) {
2742 tgsi_dump(sel->tokens, 0);
2743 si_dump_streamout(&sel->so);
2744 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002745
Marek Olšák8c37c162014-09-16 18:40:07 +02002746 assert(shader->noutput == 0);
2747 assert(shader->nparam == 0);
2748 assert(shader->ninput == 0);
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002749
Michel Dänzercfebaf92012-08-31 19:04:08 +02002750 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002751 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2752 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2753
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002754 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002755
Marek Olšákadc57972014-09-19 17:07:07 +02002756 if (shader_info.uses_kill)
2757 shader->db_shader_control |= S_02880C_KILL_ENABLE(1);
2758
Marek Olšák8c37c162014-09-16 18:40:07 +02002759 shader->uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002760 bld_base->info = &shader_info;
2761 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002762
2763 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Marek Olšák2484daa2014-04-22 21:23:29 +02002764 bld_base->op_actions[TGSI_OPCODE_TEX2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002765 bld_base->op_actions[TGSI_OPCODE_TXB] = tex_action;
2766 bld_base->op_actions[TGSI_OPCODE_TXB2] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002767 bld_base->op_actions[TGSI_OPCODE_TXD] = tex_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002768 bld_base->op_actions[TGSI_OPCODE_TXF] = tex_action;
2769 bld_base->op_actions[TGSI_OPCODE_TXL] = tex_action;
2770 bld_base->op_actions[TGSI_OPCODE_TXL2] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002771 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002772 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Marek Olšák877bb522014-06-25 03:12:46 +02002773 bld_base->op_actions[TGSI_OPCODE_TG4] = tex_action;
2774 bld_base->op_actions[TGSI_OPCODE_LODQ] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002775
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002776 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2777 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002778
Michel Dänzer404b29d2013-11-21 16:45:28 +09002779 bld_base->op_actions[TGSI_OPCODE_EMIT].emit = si_llvm_emit_vertex;
2780 bld_base->op_actions[TGSI_OPCODE_ENDPRIM].emit = si_llvm_emit_primitive;
2781
Christian Könige4ed5872013-03-21 18:02:52 +01002782 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002783 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002784 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2785 si_shader_ctx.shader = shader;
2786 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002787
Michel Dänzer51f89a02013-12-09 15:33:53 +09002788 switch (si_shader_ctx.type) {
2789 case TGSI_PROCESSOR_VERTEX:
2790 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002791 if (shader->key.vs.as_es) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002792 si_shader_ctx.gs_for_vs = sctx->gs_shader->current;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002793 bld_base->emit_epilogue = si_llvm_emit_es_epilogue;
2794 } else {
2795 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2796 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002797 break;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002798 case TGSI_PROCESSOR_GEOMETRY: {
2799 int i;
2800
2801 si_shader_ctx.radeon_bld.load_input = declare_input_gs;
2802 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = fetch_input_gs;
2803 bld_base->emit_epilogue = si_llvm_emit_gs_epilogue;
2804
2805 for (i = 0; i < shader_info.num_properties; i++) {
2806 switch (shader_info.properties[i].name) {
2807 case TGSI_PROPERTY_GS_INPUT_PRIM:
Marek Olšák8c37c162014-09-16 18:40:07 +02002808 shader->gs_input_prim = shader_info.properties[i].data[0];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002809 break;
2810 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
Marek Olšák8c37c162014-09-16 18:40:07 +02002811 shader->gs_output_prim = shader_info.properties[i].data[0];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002812 break;
2813 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
Marek Olšák8c37c162014-09-16 18:40:07 +02002814 shader->gs_max_out_vertices = shader_info.properties[i].data[0];
Michel Dänzer404b29d2013-11-21 16:45:28 +09002815 break;
2816 }
2817 }
2818 break;
2819 }
Marek Olšák6a2b3832014-06-14 17:58:30 +02002820 case TGSI_PROCESSOR_FRAGMENT: {
2821 int i;
2822
Michel Dänzer51f89a02013-12-09 15:33:53 +09002823 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2824 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002825
2826 for (i = 0; i < shader_info.num_properties; i++) {
2827 switch (shader_info.properties[i].name) {
2828 case TGSI_PROPERTY_FS_DEPTH_LAYOUT:
2829 switch (shader_info.properties[i].data[0]) {
2830 case TGSI_FS_DEPTH_LAYOUT_GREATER:
Marek Olšáka34c9f72014-09-19 16:19:44 +02002831 shader->db_shader_control |=
2832 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
Marek Olšák6a2b3832014-06-14 17:58:30 +02002833 break;
2834 case TGSI_FS_DEPTH_LAYOUT_LESS:
Marek Olšáka34c9f72014-09-19 16:19:44 +02002835 shader->db_shader_control |=
2836 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
Marek Olšák6a2b3832014-06-14 17:58:30 +02002837 break;
2838 }
2839 break;
2840 }
2841 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002842 break;
Marek Olšák6a2b3832014-06-14 17:58:30 +02002843 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09002844 default:
2845 assert(!"Unsupported shader type");
2846 return -1;
2847 }
2848
Christian König206f0592013-03-20 14:37:21 +01002849 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002850 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002851 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002852 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002853 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002854
Michel Dänzerf07a96d2014-01-08 18:45:10 +09002855 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
2856 si_shader_ctx.gs_next_vertex =
2857 lp_build_alloca(bld_base->base.gallivm,
2858 bld_base->uint_bld.elem_type, "");
2859 }
2860
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002861 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002862 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Michel Dänzer404b29d2013-11-21 16:45:28 +09002863 goto out;
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002864 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002865
2866 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2867
2868 mod = bld_base->base.gallivm->module;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002869 r = si_compile_llvm(sctx, shader, mod);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002870 if (r) {
2871 fprintf(stderr, "LLVM failed to compile shader\n");
2872 goto out;
2873 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002874
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002875 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002876
2877 if (si_shader_ctx.type == TGSI_PROCESSOR_GEOMETRY) {
Marek Olšák8c37c162014-09-16 18:40:07 +02002878 shader->gs_copy_shader = CALLOC_STRUCT(si_shader);
Michel Dänzer404b29d2013-11-21 16:45:28 +09002879 shader->gs_copy_shader->selector = shader->selector;
Michel Dänzer7b19c392014-01-09 18:18:26 +09002880 shader->gs_copy_shader->key = shader->key;
Michel Dänzer404b29d2013-11-21 16:45:28 +09002881 si_shader_ctx.shader = shader->gs_copy_shader;
2882 if ((r = si_generate_gs_copy_shader(sctx, &si_shader_ctx, dump))) {
2883 free(shader->gs_copy_shader);
2884 shader->gs_copy_shader = NULL;
2885 goto out;
2886 }
2887 }
2888
Tom Stellarda75c6162012-01-06 17:38:37 -05002889 tgsi_parse_free(&si_shader_ctx.parse);
2890
Michel Dänzer404b29d2013-11-21 16:45:28 +09002891out:
Marek Olšákee2a8182014-07-07 23:27:19 +02002892 for (int i = 0; i < SI_NUM_CONST_BUFFERS; i++)
Marek Olšák2fd42002013-10-25 11:45:47 +02002893 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002894 FREE(si_shader_ctx.resources);
2895 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002896
Tom Stellard302f53d2012-10-25 13:50:10 -04002897 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002898}
2899
Marek Olšák2774abd2014-09-16 18:45:33 +02002900void si_shader_destroy(struct pipe_context *ctx, struct si_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05002901{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002902 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002903}