blob: 80ee325b0f2ad32f93b7ef99d2565dbe77493f24 [file] [log] [blame]
Tom Stellarda75c6162012-01-06 17:38:37 -05001
Christian Königce40e472012-08-02 12:14:59 +02002/*
3 * Copyright 2012 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Tom Stellard <thomas.stellard@amd.com>
26 * Michel Dänzer <michel.daenzer@amd.com>
27 * Christian König <christian.koenig@amd.com>
28 */
29
Tom Stellarda75c6162012-01-06 17:38:37 -050030#include "gallivm/lp_bld_tgsi_action.h"
31#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020032#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050033#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010034#include "gallivm/lp_bld_logic.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050035#include "gallivm/lp_bld_tgsi.h"
Christian König5e616cf2013-03-07 11:58:56 +010036#include "gallivm/lp_bld_arit.h"
Marek Olšák8d03d922013-09-01 23:59:06 +020037#include "gallivm/lp_bld_flow.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040039#include "radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010040#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050041#include "tgsi/tgsi_info.h"
42#include "tgsi/tgsi_parse.h"
43#include "tgsi/tgsi_scan.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010044#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050045#include "tgsi/tgsi_dump.h"
46
47#include "radeonsi_pipe.h"
48#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020049#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050050#include "sid.h"
51
52#include <assert.h>
53#include <errno.h>
54#include <stdio.h>
55
Tom Stellarda75c6162012-01-06 17:38:37 -050056struct si_shader_context
57{
58 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050059 struct tgsi_parse_context parse;
60 struct tgsi_token * tokens;
61 struct si_pipe_shader *shader;
62 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020063 int param_streamout_config;
64 int param_streamout_write_index;
65 int param_streamout_offset[4];
66 int param_vertex_id;
67 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010068 LLVMValueRef const_md;
Christian König0f6cf2b2013-03-15 15:53:25 +010069 LLVMValueRef const_resource;
Michel Dänzera06ee5a2013-06-19 18:14:01 +020070#if HAVE_LLVM >= 0x0304
71 LLVMValueRef ddxy_lds;
72#endif
Christian König0f6cf2b2013-03-15 15:53:25 +010073 LLVMValueRef *constants;
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];
Tom Stellarda75c6162012-01-06 17:38:37 -050077};
78
79static struct si_shader_context * si_shader_context(
80 struct lp_build_tgsi_context * bld_base)
81{
82 return (struct si_shader_context *)bld_base;
83}
84
85
86#define PERSPECTIVE_BASE 0
87#define LINEAR_BASE 9
88
89#define SAMPLE_OFFSET 0
90#define CENTER_OFFSET 2
91#define CENTROID_OFSET 4
92
93#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040094#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020095#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040096#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050097
Tom Stellard467f5162012-05-16 15:15:35 -040098/**
99 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
100 *
101 * @param offset The offset parameter specifies the number of
102 * elements to offset, not the number of bytes or dwords. An element is the
103 * the type pointed to by the base_ptr parameter (e.g. int is the element of
104 * an int* pointer)
105 *
106 * When LLVM lowers the load instruction, it will convert the element offset
107 * into a dword offset automatically.
108 *
109 */
110static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100111 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400112 LLVMValueRef base_ptr,
113 LLVMValueRef offset)
114{
Christian König206f0592013-03-20 14:37:21 +0100115 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400116
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200117 LLVMValueRef indices[2] = {
118 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
119 offset
120 };
Christian König206f0592013-03-20 14:37:21 +0100121 LLVMValueRef computed_ptr = LLVMBuildGEP(
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200122 base->gallivm->builder, base_ptr, indices, 2, "");
Christian König206f0592013-03-20 14:37:21 +0100123
124 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
125 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
126 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400127}
128
Marek Olšákf317ce52013-09-05 15:39:57 +0200129static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100130 struct radeon_llvm_context * radeon_bld,
131 unsigned divisor)
132{
Marek Olšák8d03d922013-09-01 23:59:06 +0200133 struct si_shader_context *si_shader_ctx =
134 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100135 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
136
Marek Olšák8d03d922013-09-01 23:59:06 +0200137 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
138 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100139 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
140 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
141
142 if (divisor > 1)
143 result = LLVMBuildUDiv(gallivm->builder, result,
144 lp_build_const_int32(gallivm, divisor), "");
145
146 return result;
147}
148
Tom Stellarda75c6162012-01-06 17:38:37 -0500149static void declare_input_vs(
150 struct si_shader_context * si_shader_ctx,
151 unsigned input_index,
152 const struct tgsi_full_declaration *decl)
153{
Christian Königa0dca442013-03-22 15:59:22 +0100154 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
155 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
156
157 unsigned chan;
158
Tom Stellarda75c6162012-01-06 17:38:37 -0500159 LLVMValueRef t_list_ptr;
160 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400161 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500162 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100163 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400164 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500165 LLVMTypeRef vec4_type;
166 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500167
Tom Stellard467f5162012-05-16 15:15:35 -0400168 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100169 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500170
Christian Königb15e3ae2012-07-25 11:22:59 +0200171 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400172
Christian König206f0592013-03-20 14:37:21 +0100173 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400174
175 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200176 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500177
Christian Königa0dca442013-03-22 15:59:22 +0100178 if (divisor) {
179 /* Build index from instance ID, start instance and divisor */
180 si_shader_ctx->shader->shader.uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200181 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100182 } else {
183 /* Load the buffer index, which is always stored in VGPR0
184 * for Vertex Shaders */
Marek Olšák8d03d922013-09-01 23:59:06 +0200185 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
186 si_shader_ctx->param_vertex_id);
Christian Königa0dca442013-03-22 15:59:22 +0100187 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500188
189 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400190 args[0] = t_list;
191 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100192 args[2] = buffer_index;
Christian König44e32242013-03-20 12:10:35 +0100193 input = build_intrinsic(base->gallivm->builder,
194 "llvm.SI.vs.load.input", vec4_type, args, 3,
195 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500196
197 /* Break up the vec4 into individual components */
198 for (chan = 0; chan < 4; chan++) {
199 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
200 /* XXX: Use a helper function for this. There is one in
201 * tgsi_llvm.c. */
202 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
203 LLVMBuildExtractElement(base->gallivm->builder,
204 input, llvm_chan, "");
205 }
206}
207
208static void declare_input_fs(
209 struct si_shader_context * si_shader_ctx,
210 unsigned input_index,
211 const struct tgsi_full_declaration *decl)
212{
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200213 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500214 struct lp_build_context * base =
215 &si_shader_ctx->radeon_bld.soa.bld_base.base;
Michel Dänzer237cb072013-08-21 18:00:35 +0200216 struct lp_build_context *uint =
217 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -0500218 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400219 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100220 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
221
222 LLVMValueRef interp_param;
223 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500224
225 /* This value is:
226 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
227 * quad begins a new primitive. Bit 0 always needs
228 * to be unset)
229 * [32:16] ParamOffset
230 *
231 */
Christian König55fe5cc2013-03-04 16:30:06 +0100232 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200233 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500234
Christian König0666ffd2013-03-05 15:07:39 +0100235 unsigned chan;
236
Tom Stellard0fb1e682012-09-06 16:18:11 -0400237 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
238 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400239 unsigned soa_index =
240 radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellard0fb1e682012-09-06 16:18:11 -0400241 si_shader_ctx->radeon_bld.inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100242 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100243
244 if (chan == 3)
245 /* RCP for fragcoord.w */
246 si_shader_ctx->radeon_bld.inputs[soa_index] =
247 LLVMBuildFDiv(gallivm->builder,
248 lp_build_const_float(gallivm, 1.0f),
249 si_shader_ctx->radeon_bld.inputs[soa_index],
250 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400251 }
252 return;
253 }
254
Michel Dänzer97078b12012-09-25 12:41:31 +0200255 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
256 LLVMValueRef face, is_face_positive;
257
Christian König0666ffd2013-03-05 15:07:39 +0100258 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
259
Michel Dänzer97078b12012-09-25 12:41:31 +0200260 is_face_positive = LLVMBuildFCmp(gallivm->builder,
261 LLVMRealUGT, face,
262 lp_build_const_float(gallivm, 0.0f),
263 "");
264
265 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
266 LLVMBuildSelect(gallivm->builder,
267 is_face_positive,
268 lp_build_const_float(gallivm, 1.0f),
269 lp_build_const_float(gallivm, 0.0f),
270 "");
271 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
272 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
273 lp_build_const_float(gallivm, 0.0f);
274 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
275 lp_build_const_float(gallivm, 1.0f);
276
277 return;
278 }
279
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200280 shader->input[input_index].param_offset = shader->ninterp++;
281 attr_number = lp_build_const_int32(gallivm,
282 shader->input[input_index].param_offset);
283
Tom Stellarda75c6162012-01-06 17:38:37 -0500284 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200285 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500286 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100287 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100288 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200289 } else {
290 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100291 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200292 else
Christian König0666ffd2013-03-05 15:07:39 +0100293 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200294 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500295 break;
296 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100297 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500298 break;
299 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200300 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100301 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200302 else
Christian König0666ffd2013-03-05 15:07:39 +0100303 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200304 break;
305 case TGSI_INTERPOLATE_PERSPECTIVE:
306 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100307 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200308 else
Christian König0666ffd2013-03-05 15:07:39 +0100309 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500310 break;
311 default:
312 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
313 return;
314 }
315
Christian König0666ffd2013-03-05 15:07:39 +0100316 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
317
Tom Stellarda75c6162012-01-06 17:38:37 -0500318 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200319 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100320 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100321 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200322 LLVMValueRef face, is_face_positive;
323 LLVMValueRef back_attr_number =
324 lp_build_const_int32(gallivm,
325 shader->input[input_index].param_offset + 1);
326
Christian König0666ffd2013-03-05 15:07:39 +0100327 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
328
Michel Dänzer691f08d2012-09-06 18:03:38 +0200329 is_face_positive = LLVMBuildFCmp(gallivm->builder,
330 LLVMRealUGT, face,
331 lp_build_const_float(gallivm, 0.0f),
332 "");
333
Tom Stellarda75c6162012-01-06 17:38:37 -0500334 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100335 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200336 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
337 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
338 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
339 LLVMValueRef front, back;
340
341 args[0] = llvm_chan;
342 args[1] = attr_number;
343 front = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100344 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100345 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200346
347 args[1] = back_attr_number;
348 back = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100349 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100350 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200351
352 si_shader_ctx->radeon_bld.inputs[soa_index] =
353 LLVMBuildSelect(gallivm->builder,
354 is_face_positive,
355 front,
356 back,
357 "");
358 }
359
360 shader->ninterp++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200361 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
362 LLVMValueRef args[4];
363
364 args[0] = uint->zero;
365 args[1] = attr_number;
366 args[2] = params;
367 args[3] = interp_param;
368 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
369 build_intrinsic(base->gallivm->builder, intr_name,
370 input_type, args, args[3] ? 4 : 3,
371 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
372 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
373 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
374 lp_build_const_float(gallivm, 0.0f);
375 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
376 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200377 } else {
378 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100379 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200380 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
381 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
382 args[0] = llvm_chan;
383 args[1] = attr_number;
384 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100385 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200386 si_shader_ctx->radeon_bld.inputs[soa_index] =
387 build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100388 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100389 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200390 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500391 }
392}
393
394static void declare_input(
395 struct radeon_llvm_context * radeon_bld,
396 unsigned input_index,
397 const struct tgsi_full_declaration *decl)
398{
399 struct si_shader_context * si_shader_ctx =
400 si_shader_context(&radeon_bld->soa.bld_base);
401 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
402 declare_input_vs(si_shader_ctx, input_index, decl);
403 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
404 declare_input_fs(si_shader_ctx, input_index, decl);
405 } else {
406 fprintf(stderr, "Warning: Unsupported shader type,\n");
407 }
408}
409
Christian Könige4ed5872013-03-21 18:02:52 +0100410static void declare_system_value(
411 struct radeon_llvm_context * radeon_bld,
412 unsigned index,
413 const struct tgsi_full_declaration *decl)
414{
Marek Olšák8d03d922013-09-01 23:59:06 +0200415 struct si_shader_context *si_shader_ctx =
416 si_shader_context(&radeon_bld->soa.bld_base);
Christian Könige4ed5872013-03-21 18:02:52 +0100417 LLVMValueRef value = 0;
418
419 switch (decl->Semantic.Name) {
420 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200421 value = LLVMGetParam(radeon_bld->main_fn,
422 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100423 break;
424
425 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200426 value = LLVMGetParam(radeon_bld->main_fn,
427 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100428 break;
429
430 default:
431 assert(!"unknown system value");
432 return;
433 }
434
435 radeon_bld->system_values[index] = value;
436}
437
Tom Stellarda75c6162012-01-06 17:38:37 -0500438static LLVMValueRef fetch_constant(
439 struct lp_build_tgsi_context * bld_base,
440 const struct tgsi_full_src_register *reg,
441 enum tgsi_opcode_type type,
442 unsigned swizzle)
443{
Christian König55fe5cc2013-03-04 16:30:06 +0100444 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500445 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100446 const struct tgsi_ind_register *ireg = &reg->Indirect;
447 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500448
Christian Königf5298b02013-02-28 14:50:07 +0100449 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100450 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100451 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500452
Christian König8514f5a2013-02-04 17:46:42 +0100453 if (swizzle == LP_CHAN_ALL) {
454 unsigned chan;
455 LLVMValueRef values[4];
456 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
457 values[chan] = fetch_constant(bld_base, reg, type, chan);
458
459 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
460 }
461
Christian König0f6cf2b2013-03-15 15:53:25 +0100462 idx = reg->Register.Index * 4 + swizzle;
463 if (!reg->Register.Indirect)
464 return bitcast(bld_base, type, si_shader_ctx->constants[idx]);
Christian Königf5298b02013-02-28 14:50:07 +0100465
Christian König0f6cf2b2013-03-15 15:53:25 +0100466 args[0] = si_shader_ctx->const_resource;
467 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
468 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
469 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
470 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
471 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200472
Christian Königf5298b02013-02-28 14:50:07 +0100473 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100474 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500475
Christian Königf5298b02013-02-28 14:50:07 +0100476 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500477}
478
Michel Dänzer26c71392012-08-24 12:03:11 +0200479/* Initialize arguments for the shader export intrinsic */
480static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
481 struct tgsi_full_declaration *d,
482 unsigned index,
483 unsigned target,
484 LLVMValueRef *args)
485{
486 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
487 struct lp_build_context *uint =
488 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
489 struct lp_build_context *base = &bld_base->base;
490 unsigned compressed = 0;
491 unsigned chan;
492
Michel Dänzerf402acd2012-08-22 18:15:36 +0200493 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
494 int cbuf = target - V_008DFC_SQ_EXP_MRT;
495
496 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100497 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100498
499 if (compressed)
500 si_shader_ctx->shader->spi_shader_col_format |=
501 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
502 else
503 si_shader_ctx->shader->spi_shader_col_format |=
504 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200505
506 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200507 }
508 }
509
510 if (compressed) {
511 /* Pixel shader needs to pack output values before export */
512 for (chan = 0; chan < 2; chan++ ) {
513 LLVMValueRef *out_ptr =
514 si_shader_ctx->radeon_bld.soa.outputs[index];
515 args[0] = LLVMBuildLoad(base->gallivm->builder,
516 out_ptr[2 * chan], "");
517 args[1] = LLVMBuildLoad(base->gallivm->builder,
518 out_ptr[2 * chan + 1], "");
519 args[chan + 5] =
520 build_intrinsic(base->gallivm->builder,
521 "llvm.SI.packf16",
522 LLVMInt32TypeInContext(base->gallivm->context),
523 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100524 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100525 args[chan + 7] = args[chan + 5] =
526 LLVMBuildBitCast(base->gallivm->builder,
527 args[chan + 5],
528 LLVMFloatTypeInContext(base->gallivm->context),
529 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200530 }
531
532 /* Set COMPR flag */
533 args[4] = uint->one;
534 } else {
535 for (chan = 0; chan < 4; chan++ ) {
536 LLVMValueRef out_ptr =
537 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
538 /* +5 because the first output value will be
539 * the 6th argument to the intrinsic. */
540 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
541 out_ptr, "");
542 }
543
544 /* Clear COMPR flag */
545 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200546 }
547
548 /* XXX: This controls which components of the output
549 * registers actually get exported. (e.g bit 0 means export
550 * X component, bit 1 means export Y component, etc.) I'm
551 * hard coding this to 0xf for now. In the future, we might
552 * want to do something else. */
553 args[0] = lp_build_const_int32(base->gallivm, 0xf);
554
555 /* Specify whether the EXEC mask represents the valid mask */
556 args[1] = uint->zero;
557
558 /* Specify whether this is the last export */
559 args[2] = uint->zero;
560
561 /* Specify the target we are exporting */
562 args[3] = lp_build_const_int32(base->gallivm, target);
563
Michel Dänzer26c71392012-08-24 12:03:11 +0200564 /* XXX: We probably need to keep track of the output
565 * values, so we know what we are passing to the next
566 * stage. */
567}
568
Michel Dänzer7708a862012-11-02 15:57:30 +0100569static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
570 unsigned index)
571{
572 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
573 struct gallivm_state *gallivm = bld_base->base.gallivm;
574
Christian Königa0dca442013-03-22 15:59:22 +0100575 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Michel Dänzer7708a862012-11-02 15:57:30 +0100576 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400577 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
578 SI_PARAM_ALPHA_REF);
579
Michel Dänzer7708a862012-11-02 15:57:30 +0100580 LLVMValueRef alpha_pass =
581 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100582 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer7708a862012-11-02 15:57:30 +0100583 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400584 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100585 LLVMValueRef arg =
586 lp_build_select(&bld_base->base,
587 alpha_pass,
588 lp_build_const_float(gallivm, 1.0f),
589 lp_build_const_float(gallivm, -1.0f));
590
591 build_intrinsic(gallivm->builder,
592 "llvm.AMDGPU.kill",
593 LLVMVoidTypeInContext(gallivm->context),
594 &arg, 1, 0);
595 } else {
596 build_intrinsic(gallivm->builder,
597 "llvm.AMDGPU.kilp",
598 LLVMVoidTypeInContext(gallivm->context),
599 NULL, 0, 0);
600 }
601}
602
Marek Olšák6d4755a2013-07-30 22:29:29 +0200603static void si_alpha_to_one(struct lp_build_tgsi_context *bld_base,
604 unsigned index)
605{
606 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
607
608 /* set alpha to one */
609 LLVMBuildStore(bld_base->base.gallivm->builder,
610 bld_base->base.one,
611 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
612}
613
Michel Dänzere3befbc2013-05-15 18:09:50 +0200614static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzerb00269a2013-08-07 18:14:16 +0200615 LLVMValueRef (*pos)[9], unsigned index)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200616{
617 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200618 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200619 struct lp_build_context *base = &bld_base->base;
620 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200621 unsigned reg_index;
622 unsigned chan;
623 unsigned const_chan;
624 LLVMValueRef out_elts[4];
625 LLVMValueRef base_elt;
626 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
627 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, uint->one);
628
629 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
630 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
631 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
632 }
633
634 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200635 LLVMValueRef *args = pos[2 + reg_index];
636
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200637 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
638 continue;
639
640 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
641
Michel Dänzere3befbc2013-05-15 18:09:50 +0200642 args[5] =
643 args[6] =
644 args[7] =
645 args[8] = lp_build_const_float(base->gallivm, 0.0f);
646
647 /* Compute dot products of position and user clip plane vectors */
648 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
649 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
650 args[0] = const_resource;
651 args[1] = lp_build_const_int32(base->gallivm,
652 ((reg_index * 4 + chan) * 4 +
653 const_chan) * 4);
654 base_elt = build_intrinsic(base->gallivm->builder,
655 "llvm.SI.load.const",
656 base->elem_type,
657 args, 2,
658 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
659 args[5 + chan] =
660 lp_build_add(base, args[5 + chan],
661 lp_build_mul(base, base_elt,
662 out_elts[const_chan]));
663 }
664 }
665
666 args[0] = lp_build_const_int32(base->gallivm, 0xf);
667 args[1] = uint->zero;
668 args[2] = uint->zero;
669 args[3] = lp_build_const_int32(base->gallivm,
670 V_008DFC_SQ_EXP_POS + 2 + reg_index);
671 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200672 }
673}
674
Marek Olšák8d03d922013-09-01 23:59:06 +0200675static void si_dump_streamout(struct pipe_stream_output_info *so)
676{
677 unsigned i;
678
679 if (so->num_outputs)
680 fprintf(stderr, "STREAMOUT\n");
681
682 for (i = 0; i < so->num_outputs; i++) {
683 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
684 so->output[i].start_component;
685 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
686 i, so->output[i].output_buffer,
687 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
688 so->output[i].register_index,
689 mask & 1 ? "x" : "",
690 mask & 2 ? "y" : "",
691 mask & 4 ? "z" : "",
692 mask & 8 ? "w" : "");
693 }
694}
695
696/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
697 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
698 * or v4i32 (num_channels=3,4). */
699static void build_tbuffer_store(struct si_shader_context *shader,
700 LLVMValueRef rsrc,
701 LLVMValueRef vdata,
702 unsigned num_channels,
703 LLVMValueRef vaddr,
704 LLVMValueRef soffset,
705 unsigned inst_offset,
706 unsigned dfmt,
707 unsigned nfmt,
708 unsigned offen,
709 unsigned idxen,
710 unsigned glc,
711 unsigned slc,
712 unsigned tfe)
713{
714 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
715 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
716 LLVMValueRef args[] = {
717 rsrc,
718 vdata,
719 LLVMConstInt(i32, num_channels, 0),
720 vaddr,
721 soffset,
722 LLVMConstInt(i32, inst_offset, 0),
723 LLVMConstInt(i32, dfmt, 0),
724 LLVMConstInt(i32, nfmt, 0),
725 LLVMConstInt(i32, offen, 0),
726 LLVMConstInt(i32, idxen, 0),
727 LLVMConstInt(i32, glc, 0),
728 LLVMConstInt(i32, slc, 0),
729 LLVMConstInt(i32, tfe, 0)
730 };
731
732 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
733 unsigned func = CLAMP(num_channels, 1, 3) - 1;
734 const char *types[] = {"i32", "v2i32", "v4i32"};
735 char name[256];
736 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
737
738 lp_build_intrinsic(gallivm->builder, name,
739 LLVMVoidTypeInContext(gallivm->context),
740 args, Elements(args));
741}
742
743static void build_streamout_store(struct si_shader_context *shader,
744 LLVMValueRef rsrc,
745 LLVMValueRef vdata,
746 unsigned num_channels,
747 LLVMValueRef vaddr,
748 LLVMValueRef soffset,
749 unsigned inst_offset)
750{
751 static unsigned dfmt[] = {
752 V_008F0C_BUF_DATA_FORMAT_32,
753 V_008F0C_BUF_DATA_FORMAT_32_32,
754 V_008F0C_BUF_DATA_FORMAT_32_32_32,
755 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
756 };
757 assert(num_channels >= 1 && num_channels <= 4);
758
759 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
760 inst_offset, dfmt[num_channels-1],
761 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
762}
763
764/* On SI, the vertex shader is responsible for writing streamout data
765 * to buffers. */
766static void si_llvm_emit_streamout(struct si_shader_context *shader)
767{
768 struct pipe_stream_output_info *so = &shader->shader->selector->so;
769 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
770 LLVMBuilderRef builder = gallivm->builder;
771 int i, j;
772 struct lp_build_if_state if_ctx;
773
774 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
775
776 LLVMValueRef so_param =
777 LLVMGetParam(shader->radeon_bld.main_fn,
778 shader->param_streamout_config);
779
780 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
781 LLVMValueRef so_vtx_count =
782 LLVMBuildAnd(builder,
783 LLVMBuildLShr(builder, so_param,
784 LLVMConstInt(i32, 16, 0), ""),
785 LLVMConstInt(i32, 127, 0), "");
786
787 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
788 NULL, 0, LLVMReadNoneAttribute);
789
790 /* can_emit = tid < so_vtx_count; */
791 LLVMValueRef can_emit =
792 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
793
794 /* Emit the streamout code conditionally. This actually avoids
795 * out-of-bounds buffer access. The hw tells us via the SGPR
796 * (so_vtx_count) which threads are allowed to emit streamout data. */
797 lp_build_if(&if_ctx, gallivm, can_emit);
798 {
799 /* The buffer offset is computed as follows:
800 * ByteOffset = streamout_offset[buffer_id]*4 +
801 * (streamout_write_index + thread_id)*stride[buffer_id] +
802 * attrib_offset
803 */
804
805 LLVMValueRef so_write_index =
806 LLVMGetParam(shader->radeon_bld.main_fn,
807 shader->param_streamout_write_index);
808
809 /* Compute (streamout_write_index + thread_id). */
810 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
811
812 /* Compute the write offset for each enabled buffer. */
813 LLVMValueRef so_write_offset[4] = {};
814 for (i = 0; i < 4; i++) {
815 if (!so->stride[i])
816 continue;
817
818 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
819 shader->param_streamout_offset[i]);
820 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
821
822 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
823 LLVMConstInt(i32, so->stride[i]*4, 0), "");
824 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
825 }
826
827 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS] = shader->radeon_bld.soa.outputs;
828
829 /* Write streamout data. */
830 for (i = 0; i < so->num_outputs; i++) {
831 unsigned buf_idx = so->output[i].output_buffer;
832 unsigned reg = so->output[i].register_index;
833 unsigned start = so->output[i].start_component;
834 unsigned num_comps = so->output[i].num_components;
835 LLVMValueRef out[4];
836
837 assert(num_comps && num_comps <= 4);
838 if (!num_comps || num_comps > 4)
839 continue;
840
841 /* Load the output as int. */
842 for (j = 0; j < num_comps; j++) {
843 out[j] = LLVMBuildLoad(builder, outputs[reg][start+j], "");
844 out[j] = LLVMBuildBitCast(builder, out[j], i32, "");
845 }
846
847 /* Pack the output. */
848 LLVMValueRef vdata = NULL;
849
850 switch (num_comps) {
851 case 1: /* as i32 */
852 vdata = out[0];
853 break;
854 case 2: /* as v2i32 */
855 case 3: /* as v4i32 (aligned to 4) */
856 case 4: /* as v4i32 */
857 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
858 for (j = 0; j < num_comps; j++) {
859 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
860 LLVMConstInt(i32, j, 0), "");
861 }
862 break;
863 }
864
865 build_streamout_store(shader, shader->so_buffers[buf_idx],
866 vdata, num_comps,
867 so_write_offset[buf_idx],
868 LLVMConstInt(i32, 0, 0),
869 so->output[i].dst_offset*4);
870 }
871 }
872 lp_build_endif(&if_ctx);
873}
874
Tom Stellarda75c6162012-01-06 17:38:37 -0500875/* XXX: This is partially implemented for VS only at this point. It is not complete */
876static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
877{
878 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200879 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500880 struct lp_build_context * base = &bld_base->base;
881 struct lp_build_context * uint =
882 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
883 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100884 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500885 LLVMValueRef last_args[9] = { 0 };
Michel Dänzerb00269a2013-08-07 18:14:16 +0200886 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer0afeea52013-05-02 14:53:17 +0200887 unsigned semantic_name;
Christian König35088152012-08-01 22:35:24 +0200888 unsigned color_count = 0;
889 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100890 int depth_index = -1, stencil_index = -1;
Michel Dänzerb00269a2013-08-07 18:14:16 +0200891 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -0500892
Marek Olšák8d03d922013-09-01 23:59:06 +0200893 if (si_shader_ctx->shader->selector->so.num_outputs) {
894 si_llvm_emit_streamout(si_shader_ctx);
895 }
896
Tom Stellarda75c6162012-01-06 17:38:37 -0500897 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500898 struct tgsi_full_declaration *d =
899 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500900 unsigned target;
901 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500902
903 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100904
905 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
906 parse->FullToken.FullProperty.Property.PropertyName ==
907 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
908 shader->fs_write_all = TRUE;
909
Tom Stellarda75c6162012-01-06 17:38:37 -0500910 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
911 continue;
912
913 switch (d->Declaration.File) {
914 case TGSI_FILE_INPUT:
915 i = shader->ninput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200916 assert(i < Elements(shader->input));
Tom Stellarda75c6162012-01-06 17:38:37 -0500917 shader->input[i].name = d->Semantic.Name;
918 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200919 shader->input[i].interpolate = d->Interp.Interpolate;
920 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200921 continue;
922
Tom Stellarda75c6162012-01-06 17:38:37 -0500923 case TGSI_FILE_OUTPUT:
924 i = shader->noutput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200925 assert(i < Elements(shader->output));
Tom Stellarda75c6162012-01-06 17:38:37 -0500926 shader->output[i].name = d->Semantic.Name;
927 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200928 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500929 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500930
Christian König35088152012-08-01 22:35:24 +0200931 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500932 continue;
Christian König35088152012-08-01 22:35:24 +0200933 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500934
Michel Dänzer0afeea52013-05-02 14:53:17 +0200935 semantic_name = d->Semantic.Name;
936handle_semantic:
Tom Stellarda75c6162012-01-06 17:38:37 -0500937 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500938 /* Select the correct target */
Michel Dänzer0afeea52013-05-02 14:53:17 +0200939 switch(semantic_name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400940 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer4730dea2013-05-03 17:59:34 +0200941 shader->vs_out_misc_write = 1;
942 shader->vs_out_point_size = 1;
943 target = V_008DFC_SQ_EXP_POS + 1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500944 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100945 case TGSI_SEMANTIC_POSITION:
946 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
947 target = V_008DFC_SQ_EXP_POS;
948 break;
949 } else {
950 depth_index = index;
951 continue;
952 }
953 case TGSI_SEMANTIC_STENCIL:
954 stencil_index = index;
955 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500956 case TGSI_SEMANTIC_COLOR:
957 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200958 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500959 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200960 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500961 param_count++;
962 } else {
963 target = V_008DFC_SQ_EXP_MRT + color_count;
Marek Olšák6d4755a2013-07-30 22:29:29 +0200964 if (si_shader_ctx->shader->key.ps.alpha_to_one) {
965 si_alpha_to_one(bld_base, index);
966 }
Michel Dänzer7708a862012-11-02 15:57:30 +0100967 if (color_count == 0 &&
Christian Königa0dca442013-03-22 15:59:22 +0100968 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer7708a862012-11-02 15:57:30 +0100969 si_alpha_test(bld_base, index);
970
Tom Stellarda75c6162012-01-06 17:38:37 -0500971 color_count++;
972 }
973 break;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200974 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200975 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
976 (1 << d->Semantic.Index)))
977 continue;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200978 shader->clip_dist_write |=
979 d->Declaration.UsageMask << (d->Semantic.Index << 2);
980 target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index;
981 break;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200982 case TGSI_SEMANTIC_CLIPVERTEX:
Michel Dänzerb00269a2013-08-07 18:14:16 +0200983 si_llvm_emit_clipvertex(bld_base, pos_args, index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200984 continue;
Michel Dänzer30b30372012-09-06 17:53:04 +0200985 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500986 case TGSI_SEMANTIC_GENERIC:
987 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200988 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500989 param_count++;
990 break;
991 default:
992 target = 0;
993 fprintf(stderr,
994 "Warning: SI unhandled output type:%d\n",
Michel Dänzer0afeea52013-05-02 14:53:17 +0200995 semantic_name);
Tom Stellarda75c6162012-01-06 17:38:37 -0500996 }
997
Michel Dänzer26c71392012-08-24 12:03:11 +0200998 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500999
Michel Dänzerb00269a2013-08-07 18:14:16 +02001000 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
1001 target >= V_008DFC_SQ_EXP_POS &&
1002 target <= (V_008DFC_SQ_EXP_POS + 3)) {
1003 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
1004 args, sizeof(args));
1005 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT &&
1006 semantic_name == TGSI_SEMANTIC_COLOR) {
Tom Stellarda75c6162012-01-06 17:38:37 -05001007 if (last_args[0]) {
1008 lp_build_intrinsic(base->gallivm->builder,
1009 "llvm.SI.export",
1010 LLVMVoidTypeInContext(base->gallivm->context),
1011 last_args, 9);
1012 }
1013
1014 memcpy(last_args, args, sizeof(args));
1015 } else {
1016 lp_build_intrinsic(base->gallivm->builder,
1017 "llvm.SI.export",
1018 LLVMVoidTypeInContext(base->gallivm->context),
1019 args, 9);
1020 }
1021
1022 }
Michel Dänzer0afeea52013-05-02 14:53:17 +02001023
1024 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1025 semantic_name = TGSI_SEMANTIC_GENERIC;
1026 goto handle_semantic;
1027 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001028 }
1029
Michel Dänzer1a616c12012-11-13 17:35:09 +01001030 if (depth_index >= 0 || stencil_index >= 0) {
1031 LLVMValueRef out_ptr;
1032 unsigned mask = 0;
1033
1034 /* Specify the target we are exporting */
1035 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1036
1037 if (depth_index >= 0) {
1038 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1039 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1040 mask |= 0x1;
1041
1042 if (stencil_index < 0) {
1043 args[6] =
1044 args[7] =
1045 args[8] = args[5];
1046 }
1047 }
1048
1049 if (stencil_index >= 0) {
1050 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
1051 args[7] =
1052 args[8] =
1053 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001054 /* Only setting the stencil component bit (0x2) here
1055 * breaks some stencil piglit tests
1056 */
1057 mask |= 0x3;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001058
1059 if (depth_index < 0)
1060 args[5] = args[6];
1061 }
1062
1063 /* Specify which components to enable */
1064 args[0] = lp_build_const_int32(base->gallivm, mask);
1065
1066 args[1] =
1067 args[2] =
1068 args[4] = uint->zero;
1069
1070 if (last_args[0])
1071 lp_build_intrinsic(base->gallivm->builder,
1072 "llvm.SI.export",
1073 LLVMVoidTypeInContext(base->gallivm->context),
1074 args, 9);
1075 else
1076 memcpy(last_args, args, sizeof(args));
1077 }
1078
Michel Dänzerb00269a2013-08-07 18:14:16 +02001079 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
1080 unsigned pos_idx = 0;
Christian Königf18fd252012-07-25 21:58:46 +02001081
Michel Dänzerb00269a2013-08-07 18:14:16 +02001082 for (i = 0; i < 4; i++)
1083 if (pos_args[i][0])
1084 shader->nr_pos_exports++;
Christian Königf18fd252012-07-25 21:58:46 +02001085
Michel Dänzerb00269a2013-08-07 18:14:16 +02001086 for (i = 0; i < 4; i++) {
1087 if (!pos_args[i][0])
1088 continue;
Christian Königf18fd252012-07-25 21:58:46 +02001089
Michel Dänzerc8402702013-02-12 18:37:22 +01001090 /* Specify the target we are exporting */
Michel Dänzerb00269a2013-08-07 18:14:16 +02001091 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1092
1093 if (pos_idx == shader->nr_pos_exports)
1094 /* Specify that this is the last export */
1095 pos_args[i][2] = uint->one;
Michel Dänzerc8402702013-02-12 18:37:22 +01001096
1097 lp_build_intrinsic(base->gallivm->builder,
1098 "llvm.SI.export",
1099 LLVMVoidTypeInContext(base->gallivm->context),
Michel Dänzerb00269a2013-08-07 18:14:16 +02001100 pos_args[i], 9);
1101 }
1102 } else {
1103 if (!last_args[0]) {
1104 /* Specify which components to enable */
1105 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
1106
1107 /* Specify the target we are exporting */
1108 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
1109
1110 /* Set COMPR flag to zero to export data as 32-bit */
1111 last_args[4] = uint->zero;
1112
1113 /* dummy bits */
1114 last_args[5]= uint->zero;
1115 last_args[6]= uint->zero;
1116 last_args[7]= uint->zero;
1117 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001118
1119 si_shader_ctx->shader->spi_shader_col_format |=
Michel Dänzerb00269a2013-08-07 18:14:16 +02001120 V_028714_SPI_SHADER_32_ABGR;
1121 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001122 }
1123
Michel Dänzerb00269a2013-08-07 18:14:16 +02001124 /* Specify whether the EXEC mask represents the valid mask */
1125 last_args[1] = uint->one;
1126
1127 if (shader->fs_write_all && shader->nr_cbufs > 1) {
1128 int i;
1129
1130 /* Specify that this is not yet the last export */
1131 last_args[2] = lp_build_const_int32(base->gallivm, 0);
1132
1133 for (i = 1; i < shader->nr_cbufs; i++) {
1134 /* Specify the target we are exporting */
1135 last_args[3] = lp_build_const_int32(base->gallivm,
1136 V_008DFC_SQ_EXP_MRT + i);
1137
1138 lp_build_intrinsic(base->gallivm->builder,
1139 "llvm.SI.export",
1140 LLVMVoidTypeInContext(base->gallivm->context),
1141 last_args, 9);
1142
1143 si_shader_ctx->shader->spi_shader_col_format |=
1144 si_shader_ctx->shader->spi_shader_col_format << 4;
1145 si_shader_ctx->shader->cb_shader_mask |=
1146 si_shader_ctx->shader->cb_shader_mask << 4;
1147 }
1148
1149 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
1150 }
1151
1152 /* Specify that this is the last export */
1153 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1154
1155 lp_build_intrinsic(base->gallivm->builder,
1156 "llvm.SI.export",
1157 LLVMVoidTypeInContext(base->gallivm->context),
1158 last_args, 9);
Michel Dänzerc8402702013-02-12 18:37:22 +01001159 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001160/* XXX: Look up what this function does */
1161/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
1162}
1163
Marek Olšák4855acd2013-08-06 15:08:54 +02001164static const struct lp_build_tgsi_action txf_action;
1165
1166static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1167 struct lp_build_tgsi_context * bld_base,
1168 struct lp_build_emit_data * emit_data);
1169
Tom Stellarda75c6162012-01-06 17:38:37 -05001170static void tex_fetch_args(
1171 struct lp_build_tgsi_context * bld_base,
1172 struct lp_build_emit_data * emit_data)
1173{
Christian König55fe5cc2013-03-04 16:30:06 +01001174 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001175 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001176 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001177 unsigned opcode = inst->Instruction.Opcode;
1178 unsigned target = inst->Texture.Texture;
Marek Olšák4855acd2013-08-06 15:08:54 +02001179 unsigned sampler_src, sampler_index;
Michel Dänzer120efee2013-01-25 12:10:11 +01001180 LLVMValueRef coords[4];
1181 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001182 int ref_pos;
1183 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001184 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001185 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -04001186
Michel Dänzer120efee2013-01-25 12:10:11 +01001187 /* Fetch and project texture coordinates */
1188 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001189 for (chan = 0; chan < 3; chan++ ) {
1190 coords[chan] = lp_build_emit_fetch(bld_base,
1191 emit_data->inst, 0,
1192 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001193 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001194 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1195 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001196 coords[chan],
1197 coords[3]);
1198 }
1199
Michel Dänzer120efee2013-01-25 12:10:11 +01001200 if (opcode == TGSI_OPCODE_TXP)
1201 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001202
Michel Dänzer120efee2013-01-25 12:10:11 +01001203 /* Pack LOD bias value */
1204 if (opcode == TGSI_OPCODE_TXB)
1205 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001206
Michel Dänzer0495adb2013-05-06 12:45:14 +02001207 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001208 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001209
1210 /* Pack depth comparison value */
1211 switch (target) {
1212 case TGSI_TEXTURE_SHADOW1D:
1213 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1214 case TGSI_TEXTURE_SHADOW2D:
1215 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001216 case TGSI_TEXTURE_SHADOWCUBE:
1217 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001218 assert(ref_pos >= 0);
1219 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001220 break;
1221 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1222 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001223 }
1224
Michel Dänzera6b83c02013-02-21 16:10:55 +01001225 /* Pack user derivatives */
1226 if (opcode == TGSI_OPCODE_TXD) {
1227 for (chan = 0; chan < 2; chan++) {
1228 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1229 if (num_coords > 1)
1230 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1231 }
1232 }
1233
Michel Dänzer120efee2013-01-25 12:10:11 +01001234 /* Pack texture coordinates */
1235 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001236 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001237 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001238 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001239 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001240
Marek Olšákd2bd6342013-09-18 15:40:21 +02001241 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001242 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001243 address[count++] = coords[3];
1244
1245 if (count > 16) {
1246 assert(!"Cannot handle more than 16 texture address parameters");
1247 count = 16;
1248 }
1249
1250 for (chan = 0; chan < count; chan++ ) {
1251 address[chan] = LLVMBuildBitCast(gallivm->builder,
1252 address[chan],
1253 LLVMInt32TypeInContext(gallivm->context),
1254 "");
1255 }
1256
Michel Dänzera6b83c02013-02-21 16:10:55 +01001257 sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
Marek Olšák4855acd2013-08-06 15:08:54 +02001258 sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1259
1260 /* Adjust the sample index according to FMASK.
1261 *
1262 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1263 * which is the identity mapping. Each nibble says which physical sample
1264 * should be fetched to get that sample.
1265 *
1266 * For example, 0x11111100 means there are only 2 samples stored and
1267 * the second sample covers 3/4 of the pixel. When reading samples 0
1268 * and 1, return physical sample 0 (determined by the first two 0s
1269 * in FMASK), otherwise return physical sample 1.
1270 *
1271 * The sample index should be adjusted as follows:
1272 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1273 */
1274 if (target == TGSI_TEXTURE_2D_MSAA ||
1275 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1276 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1277 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001278 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001279 unsigned txf_count = count;
1280
Marek Olšákd2bd6342013-09-18 15:40:21 +02001281 memcpy(txf_address, address, sizeof(txf_address));
1282
1283 if (target == TGSI_TEXTURE_2D_MSAA) {
1284 txf_address[2] = bld_base->uint_bld.zero;
1285 }
1286 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001287
1288 /* Pad to a power-of-two size. */
1289 while (txf_count < util_next_power_of_two(txf_count))
1290 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1291
1292 /* Read FMASK using TXF. */
1293 txf_emit_data.chan = 0;
1294 txf_emit_data.dst_type = LLVMVectorType(
1295 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1296 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1297 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
Marek Olšákd2bd6342013-09-18 15:40:21 +02001298 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1299 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
Marek Olšák4855acd2013-08-06 15:08:54 +02001300 txf_emit_data.arg_count = 3;
1301
1302 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1303
1304 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001305 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1306 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1307
1308 /* Apply the formula. */
1309 LLVMValueRef fmask =
1310 LLVMBuildExtractElement(gallivm->builder,
1311 txf_emit_data.output[0],
1312 uint_bld->zero, "");
1313
Marek Olšákd2bd6342013-09-18 15:40:21 +02001314 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001315
1316 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001317 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001318
1319 LLVMValueRef shifted_fmask =
1320 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1321
1322 LLVMValueRef final_sample =
1323 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1324
1325 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1326 * resource descriptor is 0 (invalid),
1327 */
1328 LLVMValueRef fmask_desc =
1329 LLVMBuildBitCast(gallivm->builder,
1330 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1331 LLVMVectorType(uint_bld->elem_type, 8), "");
1332
1333 LLVMValueRef fmask_word1 =
1334 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1335 uint_bld->one, "");
1336
1337 LLVMValueRef word1_is_nonzero =
1338 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1339 fmask_word1, uint_bld->zero, "");
1340
Marek Olšákd2bd6342013-09-18 15:40:21 +02001341 /* Replace the MSAA sample index. */
1342 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001343 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001344 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001345 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001346
Michel Dänzer36231112013-05-02 09:44:45 +02001347 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001348 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001349
1350 if (opcode == TGSI_OPCODE_TXF) {
1351 /* add tex offsets */
1352 if (inst->Texture.NumOffsets) {
1353 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1354 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1355 const struct tgsi_texture_offset * off = inst->TexOffsets;
1356
1357 assert(inst->Texture.NumOffsets == 1);
1358
Marek Olšákdefedc02013-09-18 15:36:38 +02001359 switch (target) {
1360 case TGSI_TEXTURE_3D:
1361 address[2] = lp_build_add(uint_bld, address[2],
1362 bld->immediates[off->Index][off->SwizzleZ]);
1363 /* fall through */
1364 case TGSI_TEXTURE_2D:
1365 case TGSI_TEXTURE_SHADOW2D:
1366 case TGSI_TEXTURE_RECT:
1367 case TGSI_TEXTURE_SHADOWRECT:
1368 case TGSI_TEXTURE_2D_ARRAY:
1369 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001370 address[1] =
1371 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001372 bld->immediates[off->Index][off->SwizzleY]);
1373 /* fall through */
1374 case TGSI_TEXTURE_1D:
1375 case TGSI_TEXTURE_SHADOW1D:
1376 case TGSI_TEXTURE_1D_ARRAY:
1377 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1378 address[0] =
1379 lp_build_add(uint_bld, address[0],
1380 bld->immediates[off->Index][off->SwizzleX]);
1381 break;
1382 /* texture offsets do not apply to other texture targets */
1383 }
Michel Dänzer36231112013-05-02 09:44:45 +02001384 }
1385
1386 emit_data->dst_type = LLVMVectorType(
1387 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1388 4);
1389
1390 emit_data->arg_count = 3;
1391 } else {
1392 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001393 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001394
1395 emit_data->dst_type = LLVMVectorType(
1396 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1397 4);
1398
1399 emit_data->arg_count = 4;
1400 }
1401
1402 /* Dimensions */
1403 emit_data->args[emit_data->arg_count - 1] =
1404 lp_build_const_int32(bld_base->base.gallivm, target);
1405
Michel Dänzer120efee2013-01-25 12:10:11 +01001406 /* Pad to power of two vector */
1407 while (count < util_next_power_of_two(count))
1408 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1409
Christian Königccf3e8f2013-03-26 15:09:27 +01001410 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001411}
1412
Michel Dänzer07eddc42013-02-06 15:43:10 +01001413static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1414 struct lp_build_tgsi_context * bld_base,
1415 struct lp_build_emit_data * emit_data)
1416{
1417 struct lp_build_context * base = &bld_base->base;
1418 char intr_name[23];
1419
1420 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001421 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001422
Christian König44e32242013-03-20 12:10:35 +01001423 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001424 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001425 emit_data->args, emit_data->arg_count,
1426 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001427}
1428
Michel Dänzer0495adb2013-05-06 12:45:14 +02001429static void txq_fetch_args(
1430 struct lp_build_tgsi_context * bld_base,
1431 struct lp_build_emit_data * emit_data)
1432{
1433 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1434 const struct tgsi_full_instruction *inst = emit_data->inst;
1435
1436 /* Mip level */
1437 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1438
1439 /* Resource */
1440 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1441
1442 /* Dimensions */
1443 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1444 inst->Texture.Texture);
1445
1446 emit_data->arg_count = 3;
1447
1448 emit_data->dst_type = LLVMVectorType(
1449 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1450 4);
1451}
1452
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001453#if HAVE_LLVM >= 0x0304
1454
1455static void si_llvm_emit_ddxy(
1456 const struct lp_build_tgsi_action * action,
1457 struct lp_build_tgsi_context * bld_base,
1458 struct lp_build_emit_data * emit_data)
1459{
1460 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1461 struct gallivm_state *gallivm = bld_base->base.gallivm;
1462 struct lp_build_context * base = &bld_base->base;
1463 const struct tgsi_full_instruction *inst = emit_data->inst;
1464 unsigned opcode = inst->Instruction.Opcode;
1465 LLVMValueRef indices[2];
1466 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1467 LLVMValueRef tl, trbl, result[4];
1468 LLVMTypeRef i32;
1469 unsigned swizzle[4];
1470 unsigned c;
1471
1472 i32 = LLVMInt32TypeInContext(gallivm->context);
1473
1474 indices[0] = bld_base->uint_bld.zero;
1475 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1476 NULL, 0, LLVMReadNoneAttribute);
1477 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1478 indices, 2, "");
1479
1480 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1481 lp_build_const_int32(gallivm, 0xfffffffc), "");
1482 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1483 indices, 2, "");
1484
1485 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1486 lp_build_const_int32(gallivm,
1487 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1488 "");
1489 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1490 indices, 2, "");
1491
1492 for (c = 0; c < 4; ++c) {
1493 unsigned i;
1494
1495 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1496 for (i = 0; i < c; ++i) {
1497 if (swizzle[i] == swizzle[c]) {
1498 result[c] = result[i];
1499 break;
1500 }
1501 }
1502 if (i != c)
1503 continue;
1504
1505 LLVMBuildStore(gallivm->builder,
1506 LLVMBuildBitCast(gallivm->builder,
1507 lp_build_emit_fetch(bld_base, inst, 0, c),
1508 i32, ""),
1509 store_ptr);
1510
1511 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1512 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1513
1514 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1515 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1516
1517 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1518 }
1519
1520 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1521}
1522
1523#endif /* HAVE_LLVM >= 0x0304 */
1524
Tom Stellarda75c6162012-01-06 17:38:37 -05001525static const struct lp_build_tgsi_action tex_action = {
1526 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001527 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001528 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001529};
1530
Michel Dänzer3e205132012-11-06 17:39:01 +01001531static const struct lp_build_tgsi_action txb_action = {
1532 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001533 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001534 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001535};
1536
Michel Dänzera6b83c02013-02-21 16:10:55 +01001537#if HAVE_LLVM >= 0x0304
1538static const struct lp_build_tgsi_action txd_action = {
1539 .fetch_args = tex_fetch_args,
1540 .emit = build_tex_intrinsic,
1541 .intr_name = "llvm.SI.sampled."
1542};
1543#endif
1544
Michel Dänzer36231112013-05-02 09:44:45 +02001545static const struct lp_build_tgsi_action txf_action = {
1546 .fetch_args = tex_fetch_args,
1547 .emit = build_tex_intrinsic,
1548 .intr_name = "llvm.SI.imageload."
1549};
1550
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001551static const struct lp_build_tgsi_action txl_action = {
1552 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001553 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001554 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001555};
1556
Michel Dänzer0495adb2013-05-06 12:45:14 +02001557static const struct lp_build_tgsi_action txq_action = {
1558 .fetch_args = txq_fetch_args,
1559 .emit = build_tgsi_intrinsic_nomem,
1560 .intr_name = "llvm.SI.resinfo"
1561};
1562
Christian König206f0592013-03-20 14:37:21 +01001563static void create_meta_data(struct si_shader_context *si_shader_ctx)
1564{
1565 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1566 LLVMValueRef args[3];
1567
1568 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1569 args[1] = 0;
1570 args[2] = lp_build_const_int32(gallivm, 1);
1571
1572 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1573}
1574
Christian König55fe5cc2013-03-04 16:30:06 +01001575static void create_function(struct si_shader_context *si_shader_ctx)
1576{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001577 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1578 struct gallivm_state *gallivm = bld_base->base.gallivm;
Vadim Girlin453ea2d2013-10-13 19:53:54 +04001579 LLVMTypeRef params[21], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001580 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01001581
Christian König55fe5cc2013-03-04 16:30:06 +01001582 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01001583 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01001584 f32 = LLVMFloatTypeInContext(gallivm->context);
1585 v2i32 = LLVMVectorType(i32, 2);
1586 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01001587
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02001588 params[SI_PARAM_CONST] = LLVMPointerType(
1589 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
1590 /* We assume at most 16 textures per program at the moment.
1591 * This need probably need to be changed to support bindless textures */
1592 params[SI_PARAM_SAMPLER] = LLVMPointerType(
1593 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
1594 params[SI_PARAM_RESOURCE] = LLVMPointerType(
1595 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01001596
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001597 switch (si_shader_ctx->type) {
1598 case TGSI_PROCESSOR_VERTEX:
1599 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Marek Olšák2993cca2013-08-18 02:34:23 +02001600 params[SI_PARAM_SO_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01001601 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02001602 num_params = SI_PARAM_START_INSTANCE+1;
1603
1604 /* The locations of the other parameters are assigned dynamically. */
1605
1606 /* Streamout SGPRs. */
1607 if (si_shader_ctx->shader->selector->so.num_outputs) {
1608 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
1609 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
1610 }
1611 /* A streamout buffer offset is loaded if the stride is non-zero. */
1612 for (i = 0; i < 4; i++) {
1613 if (!si_shader_ctx->shader->selector->so.stride[i])
1614 continue;
1615
1616 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
1617 }
1618
1619 last_sgpr = num_params-1;
1620
1621 /* VGPRs */
1622 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
1623 params[num_params++] = i32; /* unused*/
1624 params[num_params++] = i32; /* unused */
1625 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001626 break;
Christian König0666ffd2013-03-05 15:07:39 +01001627
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001628 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04001629 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01001630 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001631 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01001632 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
1633 params[SI_PARAM_PERSP_CENTER] = v2i32;
1634 params[SI_PARAM_PERSP_CENTROID] = v2i32;
1635 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
1636 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
1637 params[SI_PARAM_LINEAR_CENTER] = v2i32;
1638 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
1639 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
1640 params[SI_PARAM_POS_X_FLOAT] = f32;
1641 params[SI_PARAM_POS_Y_FLOAT] = f32;
1642 params[SI_PARAM_POS_Z_FLOAT] = f32;
1643 params[SI_PARAM_POS_W_FLOAT] = f32;
1644 params[SI_PARAM_FRONT_FACE] = f32;
1645 params[SI_PARAM_ANCILLARY] = f32;
1646 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
1647 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001648 num_params = SI_PARAM_POS_FIXED_PT+1;
1649 break;
1650
1651 default:
1652 assert(0 && "unimplemented shader");
1653 return;
Christian Königc4973212013-03-05 12:14:02 +01001654 }
Christian König55fe5cc2013-03-04 16:30:06 +01001655
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001656 assert(num_params <= Elements(params));
1657 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01001658 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01001659
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001660 for (i = 0; i <= last_sgpr; ++i) {
1661 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02001662 switch (i) {
1663 default:
1664 LLVMAddAttribute(P, LLVMInRegAttribute);
1665 break;
1666#if HAVE_LLVM >= 0x0304
1667 /* We tell llvm that array inputs are passed by value to allow Sinking pass
1668 * to move load. Inputs are constant so this is fine. */
1669 case SI_PARAM_CONST:
1670 case SI_PARAM_SAMPLER:
1671 case SI_PARAM_RESOURCE:
1672 LLVMAddAttribute(P, LLVMByValAttribute);
1673 break;
1674#endif
1675 }
Christian Königcf9b31f2013-03-21 18:30:23 +01001676 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001677
1678#if HAVE_LLVM >= 0x0304
1679 if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
1680 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0)
1681 si_shader_ctx->ddxy_lds =
1682 LLVMAddGlobalInAddressSpace(gallivm->module,
1683 LLVMArrayType(i32, 64),
1684 "ddxy_lds",
1685 LOCAL_ADDR_SPACE);
1686#endif
Christian König55fe5cc2013-03-04 16:30:06 +01001687}
Tom Stellarda75c6162012-01-06 17:38:37 -05001688
Christian König0f6cf2b2013-03-15 15:53:25 +01001689static void preload_constants(struct si_shader_context *si_shader_ctx)
1690{
1691 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1692 struct gallivm_state * gallivm = bld_base->base.gallivm;
1693 const struct tgsi_shader_info * info = bld_base->info;
1694
1695 unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1;
1696
1697 LLVMValueRef ptr;
1698
1699 if (num_const == 0)
1700 return;
1701
1702 /* Allocate space for the constant values */
1703 si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef));
1704
1705 /* Load the resource descriptor */
1706 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1707 si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
1708
1709 /* Load the constants, we rely on the code sinking to do the rest */
1710 for (i = 0; i < num_const * 4; ++i) {
1711 LLVMValueRef args[2] = {
1712 si_shader_ctx->const_resource,
1713 lp_build_const_int32(gallivm, i * 4)
1714 };
1715 si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1716 bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1717 }
1718}
1719
Christian König1c100182013-03-17 16:02:42 +01001720static void preload_samplers(struct si_shader_context *si_shader_ctx)
1721{
1722 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1723 struct gallivm_state * gallivm = bld_base->base.gallivm;
1724 const struct tgsi_shader_info * info = bld_base->info;
1725
1726 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1727
1728 LLVMValueRef res_ptr, samp_ptr;
1729 LLVMValueRef offset;
1730
1731 if (num_samplers == 0)
1732 return;
1733
1734 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02001735 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01001736 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1737
1738 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1739 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1740
1741 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1742 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01001743 /* Resource */
1744 offset = lp_build_const_int32(gallivm, i);
1745 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1746
1747 /* Sampler */
1748 offset = lp_build_const_int32(gallivm, i);
1749 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02001750
1751 /* FMASK resource */
1752 if (info->is_msaa_sampler[i]) {
1753 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
1754 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
1755 build_indexed_load(si_shader_ctx, res_ptr, offset);
1756 }
Christian König1c100182013-03-17 16:02:42 +01001757 }
1758}
1759
Marek Olšák8d03d922013-09-01 23:59:06 +02001760static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
1761{
1762 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1763 struct gallivm_state * gallivm = bld_base->base.gallivm;
1764 unsigned i;
1765
1766 if (!si_shader_ctx->shader->selector->so.num_outputs)
1767 return;
1768
1769 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1770 SI_PARAM_SO_BUFFER);
1771
1772 /* Load the resources, we rely on the code sinking to do the rest */
1773 for (i = 0; i < 4; ++i) {
1774 if (si_shader_ctx->shader->selector->so.stride[i]) {
1775 LLVMValueRef offset = lp_build_const_int32(gallivm, i);
1776
1777 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
1778 }
1779 }
1780}
1781
Tom Stellard302f53d2012-10-25 13:50:10 -04001782int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader,
1783 LLVMModuleRef mod)
1784{
Tom Stellard302f53d2012-10-25 13:50:10 -04001785 unsigned i;
1786 uint32_t *ptr;
Tom Stellard7782d192013-04-04 09:57:13 -07001787 struct radeon_llvm_binary binary;
Tom Stellardb2805162013-10-03 17:39:59 -04001788 bool dump = r600_can_dump_shader(&rctx->screen->b,
1789 shader->selector ? shader->selector->tokens : NULL);
Tom Stellard7782d192013-04-04 09:57:13 -07001790 memset(&binary, 0, sizeof(binary));
1791 radeon_llvm_compile(mod, &binary,
Marek Olšáka81c3e02013-08-14 01:04:39 +02001792 r600_get_llvm_processor_name(rctx->screen->b.family), dump);
Jay Cornwalld7d539a2013-10-10 20:06:48 -05001793 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04001794 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07001795 for (i = 0; i < binary.code_size; i+=4 ) {
1796 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
1797 binary.code[i + 2], binary.code[i + 1],
1798 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04001799 }
1800 }
1801
Tom Stellardd50343d2013-04-04 16:21:06 -04001802 /* XXX: We may be able to emit some of these values directly rather than
1803 * extracting fields to be emitted later.
1804 */
1805 for (i = 0; i < binary.config_size; i+= 8) {
1806 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
1807 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
1808 switch (reg) {
1809 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
1810 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
1811 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
1812 case R_00B848_COMPUTE_PGM_RSRC1:
1813 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
1814 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
1815 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001816 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
1817 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
1818 break;
1819 case R_00B84C_COMPUTE_PGM_RSRC2:
1820 shader->lds_size = G_00B84C_LDS_SIZE(value);
1821 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04001822 case R_0286CC_SPI_PS_INPUT_ENA:
1823 shader->spi_ps_input_ena = value;
1824 break;
1825 default:
1826 fprintf(stderr, "Warning: Compiler emitted unknown "
1827 "config register: 0x%x\n", reg);
1828 break;
1829 }
1830 }
Tom Stellard302f53d2012-10-25 13:50:10 -04001831
1832 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02001833 r600_resource_reference(&shader->bo, NULL);
1834 shader->bo = r600_resource_create_custom(rctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04001835 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001836 if (shader->bo == NULL) {
1837 return -ENOMEM;
1838 }
1839
Marek Olšáka81c3e02013-08-14 01:04:39 +02001840 ptr = (uint32_t*)rctx->b.ws->buffer_map(shader->bo->cs_buf, rctx->b.rings.gfx.cs, PIPE_TRANSFER_WRITE);
Tom Stellard302f53d2012-10-25 13:50:10 -04001841 if (0 /*R600_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04001842 for (i = 0; i < binary.code_size / 4; ++i) {
1843 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04001844 }
1845 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04001846 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001847 }
Marek Olšáka81c3e02013-08-14 01:04:39 +02001848 rctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04001849
Tom Stellard7782d192013-04-04 09:57:13 -07001850 free(binary.code);
1851 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04001852
1853 return 0;
1854}
1855
Tom Stellarda75c6162012-01-06 17:38:37 -05001856int si_pipe_shader_create(
1857 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01001858 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05001859{
1860 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001861 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05001862 struct si_shader_context si_shader_ctx;
1863 struct tgsi_shader_info shader_info;
1864 struct lp_build_tgsi_context * bld_base;
1865 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04001866 int r = 0;
Marek Olšák0cb9de12013-09-22 15:34:12 +02001867 bool dump = r600_can_dump_shader(&rctx->screen->b, shader->selector->tokens);
Tom Stellarda75c6162012-01-06 17:38:37 -05001868
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001869 assert(shader->shader.noutput == 0);
1870 assert(shader->shader.ninterp == 0);
1871 assert(shader->shader.ninput == 0);
1872
Michel Dänzercfebaf92012-08-31 19:04:08 +02001873 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001874 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1875 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1876
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001877 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001878
Michel Dänzere44dfd42012-11-07 17:33:08 +01001879 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01001880 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001881 bld_base->info = &shader_info;
1882 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001883 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1884
1885 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001886 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01001887#if HAVE_LLVM >= 0x0304
1888 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
1889#endif
Michel Dänzer36231112013-05-02 09:44:45 +02001890 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001891 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001892 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02001893 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001894
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001895#if HAVE_LLVM >= 0x0304
1896 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
1897 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
1898#endif
1899
Tom Stellarda75c6162012-01-06 17:38:37 -05001900 si_shader_ctx.radeon_bld.load_input = declare_input;
Christian Könige4ed5872013-03-21 18:02:52 +01001901 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001902 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001903 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1904 si_shader_ctx.shader = shader;
1905 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05001906
Christian König206f0592013-03-20 14:37:21 +01001907 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01001908 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01001909 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01001910 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02001911 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001912
Christian König835098a2012-07-17 21:28:10 +02001913 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001914
Tom Stellard185fc9a2012-07-12 10:40:47 -04001915 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1916 * conversion fails. */
1917 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001918 tgsi_dump(sel->tokens, 0);
Marek Olšák8d03d922013-09-01 23:59:06 +02001919 si_dump_streamout(&sel->so);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001920 }
1921
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001922 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001923 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Christian König0f6cf2b2013-03-15 15:53:25 +01001924 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001925 FREE(si_shader_ctx.resources);
1926 FREE(si_shader_ctx.samplers);
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001927 return -EINVAL;
1928 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001929
1930 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1931
1932 mod = bld_base->base.gallivm->module;
Tom Stellard302f53d2012-10-25 13:50:10 -04001933 r = si_compile_llvm(rctx, shader, mod);
Tom Stellarda75c6162012-01-06 17:38:37 -05001934
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001935 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001936 tgsi_parse_free(&si_shader_ctx.parse);
1937
Christian König0f6cf2b2013-03-15 15:53:25 +01001938 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001939 FREE(si_shader_ctx.resources);
1940 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05001941
Tom Stellard302f53d2012-10-25 13:50:10 -04001942 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05001943}
1944
1945void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1946{
Marek Olšáka81c3e02013-08-14 01:04:39 +02001947 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001948}