blob: fee62624c6f6ad6ae6e06752c12583240f0bd62d [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"
Tom Stellarda75c6162012-01-06 17:38:37 -050037#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040038#include "radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010039#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_info.h"
41#include "tgsi/tgsi_parse.h"
42#include "tgsi/tgsi_scan.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010043#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050044#include "tgsi/tgsi_dump.h"
45
46#include "radeonsi_pipe.h"
47#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020048#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050049#include "sid.h"
50
51#include <assert.h>
52#include <errno.h>
53#include <stdio.h>
54
Tom Stellarda75c6162012-01-06 17:38:37 -050055struct si_shader_context
56{
57 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050058 struct tgsi_parse_context parse;
59 struct tgsi_token * tokens;
60 struct si_pipe_shader *shader;
61 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Christian König206f0592013-03-20 14:37:21 +010062 LLVMValueRef const_md;
Christian König0f6cf2b2013-03-15 15:53:25 +010063 LLVMValueRef const_resource;
Michel Dänzera06ee5a2013-06-19 18:14:01 +020064#if HAVE_LLVM >= 0x0304
65 LLVMValueRef ddxy_lds;
66#endif
Christian König0f6cf2b2013-03-15 15:53:25 +010067 LLVMValueRef *constants;
Christian König1c100182013-03-17 16:02:42 +010068 LLVMValueRef *resources;
69 LLVMValueRef *samplers;
Tom Stellarda75c6162012-01-06 17:38:37 -050070};
71
72static struct si_shader_context * si_shader_context(
73 struct lp_build_tgsi_context * bld_base)
74{
75 return (struct si_shader_context *)bld_base;
76}
77
78
79#define PERSPECTIVE_BASE 0
80#define LINEAR_BASE 9
81
82#define SAMPLE_OFFSET 0
83#define CENTER_OFFSET 2
84#define CENTROID_OFSET 4
85
86#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040087#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020088#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040089#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050090
Tom Stellard467f5162012-05-16 15:15:35 -040091/**
92 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
93 *
94 * @param offset The offset parameter specifies the number of
95 * elements to offset, not the number of bytes or dwords. An element is the
96 * the type pointed to by the base_ptr parameter (e.g. int is the element of
97 * an int* pointer)
98 *
99 * When LLVM lowers the load instruction, it will convert the element offset
100 * into a dword offset automatically.
101 *
102 */
103static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100104 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400105 LLVMValueRef base_ptr,
106 LLVMValueRef offset)
107{
Christian König206f0592013-03-20 14:37:21 +0100108 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400109
Christian König206f0592013-03-20 14:37:21 +0100110 LLVMValueRef computed_ptr = LLVMBuildGEP(
111 base->gallivm->builder, base_ptr, &offset, 1, "");
112
113 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
114 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
115 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400116}
117
Christian Königa0dca442013-03-22 15:59:22 +0100118static LLVMValueRef get_instance_index(
119 struct radeon_llvm_context * radeon_bld,
120 unsigned divisor)
121{
122 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
123
124 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_INSTANCE_ID);
125 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
126 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
127
128 if (divisor > 1)
129 result = LLVMBuildUDiv(gallivm->builder, result,
130 lp_build_const_int32(gallivm, divisor), "");
131
132 return result;
133}
134
Tom Stellarda75c6162012-01-06 17:38:37 -0500135static void declare_input_vs(
136 struct si_shader_context * si_shader_ctx,
137 unsigned input_index,
138 const struct tgsi_full_declaration *decl)
139{
Christian Königa0dca442013-03-22 15:59:22 +0100140 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
141 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
142
143 unsigned chan;
144
Tom Stellarda75c6162012-01-06 17:38:37 -0500145 LLVMValueRef t_list_ptr;
146 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400147 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500148 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100149 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400150 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500151 LLVMTypeRef vec4_type;
152 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500153
Tom Stellard467f5162012-05-16 15:15:35 -0400154 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100155 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500156
Christian Königb15e3ae2012-07-25 11:22:59 +0200157 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400158
Christian König206f0592013-03-20 14:37:21 +0100159 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400160
161 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200162 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500163
Christian Königa0dca442013-03-22 15:59:22 +0100164 if (divisor) {
165 /* Build index from instance ID, start instance and divisor */
166 si_shader_ctx->shader->shader.uses_instanceid = true;
167 buffer_index = get_instance_index(&si_shader_ctx->radeon_bld, divisor);
168 } else {
169 /* Load the buffer index, which is always stored in VGPR0
170 * for Vertex Shaders */
171 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_ID);
172 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500173
174 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400175 args[0] = t_list;
176 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100177 args[2] = buffer_index;
Christian König44e32242013-03-20 12:10:35 +0100178 input = build_intrinsic(base->gallivm->builder,
179 "llvm.SI.vs.load.input", vec4_type, args, 3,
180 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500181
182 /* Break up the vec4 into individual components */
183 for (chan = 0; chan < 4; chan++) {
184 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
185 /* XXX: Use a helper function for this. There is one in
186 * tgsi_llvm.c. */
187 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
188 LLVMBuildExtractElement(base->gallivm->builder,
189 input, llvm_chan, "");
190 }
191}
192
193static void declare_input_fs(
194 struct si_shader_context * si_shader_ctx,
195 unsigned input_index,
196 const struct tgsi_full_declaration *decl)
197{
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200198 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500199 struct lp_build_context * base =
200 &si_shader_ctx->radeon_bld.soa.bld_base.base;
201 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400202 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100203 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
204
205 LLVMValueRef interp_param;
206 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500207
208 /* This value is:
209 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
210 * quad begins a new primitive. Bit 0 always needs
211 * to be unset)
212 * [32:16] ParamOffset
213 *
214 */
Christian König55fe5cc2013-03-04 16:30:06 +0100215 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200216 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500217
Christian König0666ffd2013-03-05 15:07:39 +0100218 unsigned chan;
219
Tom Stellard0fb1e682012-09-06 16:18:11 -0400220 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
221 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400222 unsigned soa_index =
223 radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellard0fb1e682012-09-06 16:18:11 -0400224 si_shader_ctx->radeon_bld.inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100225 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100226
227 if (chan == 3)
228 /* RCP for fragcoord.w */
229 si_shader_ctx->radeon_bld.inputs[soa_index] =
230 LLVMBuildFDiv(gallivm->builder,
231 lp_build_const_float(gallivm, 1.0f),
232 si_shader_ctx->radeon_bld.inputs[soa_index],
233 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400234 }
235 return;
236 }
237
Michel Dänzer97078b12012-09-25 12:41:31 +0200238 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
239 LLVMValueRef face, is_face_positive;
240
Christian König0666ffd2013-03-05 15:07:39 +0100241 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
242
Michel Dänzer97078b12012-09-25 12:41:31 +0200243 is_face_positive = LLVMBuildFCmp(gallivm->builder,
244 LLVMRealUGT, face,
245 lp_build_const_float(gallivm, 0.0f),
246 "");
247
248 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
249 LLVMBuildSelect(gallivm->builder,
250 is_face_positive,
251 lp_build_const_float(gallivm, 1.0f),
252 lp_build_const_float(gallivm, 0.0f),
253 "");
254 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
255 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
256 lp_build_const_float(gallivm, 0.0f);
257 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
258 lp_build_const_float(gallivm, 1.0f);
259
260 return;
261 }
262
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200263 shader->input[input_index].param_offset = shader->ninterp++;
264 attr_number = lp_build_const_int32(gallivm,
265 shader->input[input_index].param_offset);
266
Tom Stellarda75c6162012-01-06 17:38:37 -0500267 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200268 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500269 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100270 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100271 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200272 } else {
273 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100274 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200275 else
Christian König0666ffd2013-03-05 15:07:39 +0100276 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200277 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500278 break;
279 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100280 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500281 break;
282 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200283 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100284 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200285 else
Christian König0666ffd2013-03-05 15:07:39 +0100286 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200287 break;
288 case TGSI_INTERPOLATE_PERSPECTIVE:
289 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100290 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200291 else
Christian König0666ffd2013-03-05 15:07:39 +0100292 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500293 break;
294 default:
295 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
296 return;
297 }
298
Christian König0666ffd2013-03-05 15:07:39 +0100299 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
300
Tom Stellarda75c6162012-01-06 17:38:37 -0500301 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200302 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100303 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100304 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200305 LLVMValueRef face, is_face_positive;
306 LLVMValueRef back_attr_number =
307 lp_build_const_int32(gallivm,
308 shader->input[input_index].param_offset + 1);
309
Christian König0666ffd2013-03-05 15:07:39 +0100310 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
311
Michel Dänzer691f08d2012-09-06 18:03:38 +0200312 is_face_positive = LLVMBuildFCmp(gallivm->builder,
313 LLVMRealUGT, face,
314 lp_build_const_float(gallivm, 0.0f),
315 "");
316
Tom Stellarda75c6162012-01-06 17:38:37 -0500317 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100318 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200319 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
320 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
321 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
322 LLVMValueRef front, back;
323
324 args[0] = llvm_chan;
325 args[1] = attr_number;
326 front = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100327 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100328 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200329
330 args[1] = back_attr_number;
331 back = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100332 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100333 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200334
335 si_shader_ctx->radeon_bld.inputs[soa_index] =
336 LLVMBuildSelect(gallivm->builder,
337 is_face_positive,
338 front,
339 back,
340 "");
341 }
342
343 shader->ninterp++;
344 } else {
345 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100346 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200347 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
348 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
349 args[0] = llvm_chan;
350 args[1] = attr_number;
351 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100352 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200353 si_shader_ctx->radeon_bld.inputs[soa_index] =
354 build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100355 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100356 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200357 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500358 }
359}
360
361static void declare_input(
362 struct radeon_llvm_context * radeon_bld,
363 unsigned input_index,
364 const struct tgsi_full_declaration *decl)
365{
366 struct si_shader_context * si_shader_ctx =
367 si_shader_context(&radeon_bld->soa.bld_base);
368 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
369 declare_input_vs(si_shader_ctx, input_index, decl);
370 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
371 declare_input_fs(si_shader_ctx, input_index, decl);
372 } else {
373 fprintf(stderr, "Warning: Unsupported shader type,\n");
374 }
375}
376
Christian Könige4ed5872013-03-21 18:02:52 +0100377static void declare_system_value(
378 struct radeon_llvm_context * radeon_bld,
379 unsigned index,
380 const struct tgsi_full_declaration *decl)
381{
Christian Königcf9b31f2013-03-21 18:30:23 +0100382
Christian Könige4ed5872013-03-21 18:02:52 +0100383 LLVMValueRef value = 0;
384
385 switch (decl->Semantic.Name) {
386 case TGSI_SEMANTIC_INSTANCEID:
Christian Königa0dca442013-03-22 15:59:22 +0100387 value = get_instance_index(radeon_bld, 1);
Christian Könige4ed5872013-03-21 18:02:52 +0100388 break;
389
390 case TGSI_SEMANTIC_VERTEXID:
391 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_VERTEX_ID);
392 break;
393
394 default:
395 assert(!"unknown system value");
396 return;
397 }
398
399 radeon_bld->system_values[index] = value;
400}
401
Tom Stellarda75c6162012-01-06 17:38:37 -0500402static LLVMValueRef fetch_constant(
403 struct lp_build_tgsi_context * bld_base,
404 const struct tgsi_full_src_register *reg,
405 enum tgsi_opcode_type type,
406 unsigned swizzle)
407{
Christian König55fe5cc2013-03-04 16:30:06 +0100408 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500409 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100410 const struct tgsi_ind_register *ireg = &reg->Indirect;
411 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500412
Christian Königf5298b02013-02-28 14:50:07 +0100413 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100414 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100415 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500416
Christian König8514f5a2013-02-04 17:46:42 +0100417 if (swizzle == LP_CHAN_ALL) {
418 unsigned chan;
419 LLVMValueRef values[4];
420 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
421 values[chan] = fetch_constant(bld_base, reg, type, chan);
422
423 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
424 }
425
Christian König0f6cf2b2013-03-15 15:53:25 +0100426 idx = reg->Register.Index * 4 + swizzle;
427 if (!reg->Register.Indirect)
428 return bitcast(bld_base, type, si_shader_ctx->constants[idx]);
Christian Königf5298b02013-02-28 14:50:07 +0100429
Christian König0f6cf2b2013-03-15 15:53:25 +0100430 args[0] = si_shader_ctx->const_resource;
431 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
432 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
433 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
434 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
435 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200436
Christian Königf5298b02013-02-28 14:50:07 +0100437 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100438 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500439
Christian Königf5298b02013-02-28 14:50:07 +0100440 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500441}
442
Michel Dänzer26c71392012-08-24 12:03:11 +0200443/* Initialize arguments for the shader export intrinsic */
444static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
445 struct tgsi_full_declaration *d,
446 unsigned index,
447 unsigned target,
448 LLVMValueRef *args)
449{
450 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
451 struct lp_build_context *uint =
452 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
453 struct lp_build_context *base = &bld_base->base;
454 unsigned compressed = 0;
455 unsigned chan;
456
Michel Dänzerf402acd2012-08-22 18:15:36 +0200457 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
458 int cbuf = target - V_008DFC_SQ_EXP_MRT;
459
460 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100461 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100462
463 if (compressed)
464 si_shader_ctx->shader->spi_shader_col_format |=
465 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
466 else
467 si_shader_ctx->shader->spi_shader_col_format |=
468 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200469
470 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200471 }
472 }
473
474 if (compressed) {
475 /* Pixel shader needs to pack output values before export */
476 for (chan = 0; chan < 2; chan++ ) {
477 LLVMValueRef *out_ptr =
478 si_shader_ctx->radeon_bld.soa.outputs[index];
479 args[0] = LLVMBuildLoad(base->gallivm->builder,
480 out_ptr[2 * chan], "");
481 args[1] = LLVMBuildLoad(base->gallivm->builder,
482 out_ptr[2 * chan + 1], "");
483 args[chan + 5] =
484 build_intrinsic(base->gallivm->builder,
485 "llvm.SI.packf16",
486 LLVMInt32TypeInContext(base->gallivm->context),
487 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100488 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100489 args[chan + 7] = args[chan + 5] =
490 LLVMBuildBitCast(base->gallivm->builder,
491 args[chan + 5],
492 LLVMFloatTypeInContext(base->gallivm->context),
493 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200494 }
495
496 /* Set COMPR flag */
497 args[4] = uint->one;
498 } else {
499 for (chan = 0; chan < 4; chan++ ) {
500 LLVMValueRef out_ptr =
501 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
502 /* +5 because the first output value will be
503 * the 6th argument to the intrinsic. */
504 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
505 out_ptr, "");
506 }
507
508 /* Clear COMPR flag */
509 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200510 }
511
512 /* XXX: This controls which components of the output
513 * registers actually get exported. (e.g bit 0 means export
514 * X component, bit 1 means export Y component, etc.) I'm
515 * hard coding this to 0xf for now. In the future, we might
516 * want to do something else. */
517 args[0] = lp_build_const_int32(base->gallivm, 0xf);
518
519 /* Specify whether the EXEC mask represents the valid mask */
520 args[1] = uint->zero;
521
522 /* Specify whether this is the last export */
523 args[2] = uint->zero;
524
525 /* Specify the target we are exporting */
526 args[3] = lp_build_const_int32(base->gallivm, target);
527
Michel Dänzer26c71392012-08-24 12:03:11 +0200528 /* XXX: We probably need to keep track of the output
529 * values, so we know what we are passing to the next
530 * stage. */
531}
532
Michel Dänzer7708a862012-11-02 15:57:30 +0100533static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
534 unsigned index)
535{
536 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
537 struct gallivm_state *gallivm = bld_base->base.gallivm;
538
Christian Königa0dca442013-03-22 15:59:22 +0100539 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Michel Dänzer7708a862012-11-02 15:57:30 +0100540 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
541 LLVMValueRef alpha_pass =
542 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100543 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer7708a862012-11-02 15:57:30 +0100544 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
Christian Königa0dca442013-03-22 15:59:22 +0100545 lp_build_const_float(gallivm, si_shader_ctx->shader->key.ps.alpha_ref));
Michel Dänzer7708a862012-11-02 15:57:30 +0100546 LLVMValueRef arg =
547 lp_build_select(&bld_base->base,
548 alpha_pass,
549 lp_build_const_float(gallivm, 1.0f),
550 lp_build_const_float(gallivm, -1.0f));
551
552 build_intrinsic(gallivm->builder,
553 "llvm.AMDGPU.kill",
554 LLVMVoidTypeInContext(gallivm->context),
555 &arg, 1, 0);
556 } else {
557 build_intrinsic(gallivm->builder,
558 "llvm.AMDGPU.kilp",
559 LLVMVoidTypeInContext(gallivm->context),
560 NULL, 0, 0);
561 }
562}
563
Michel Dänzere3befbc2013-05-15 18:09:50 +0200564static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
565 unsigned index)
566{
567 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
568 struct lp_build_context *base = &bld_base->base;
569 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
570 LLVMValueRef args[9];
571 unsigned reg_index;
572 unsigned chan;
573 unsigned const_chan;
574 LLVMValueRef out_elts[4];
575 LLVMValueRef base_elt;
576 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
577 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, uint->one);
578
579 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
580 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
581 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
582 }
583
584 for (reg_index = 0; reg_index < 2; reg_index ++) {
585 args[5] =
586 args[6] =
587 args[7] =
588 args[8] = lp_build_const_float(base->gallivm, 0.0f);
589
590 /* Compute dot products of position and user clip plane vectors */
591 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
592 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
593 args[0] = const_resource;
594 args[1] = lp_build_const_int32(base->gallivm,
595 ((reg_index * 4 + chan) * 4 +
596 const_chan) * 4);
597 base_elt = build_intrinsic(base->gallivm->builder,
598 "llvm.SI.load.const",
599 base->elem_type,
600 args, 2,
601 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
602 args[5 + chan] =
603 lp_build_add(base, args[5 + chan],
604 lp_build_mul(base, base_elt,
605 out_elts[const_chan]));
606 }
607 }
608
609 args[0] = lp_build_const_int32(base->gallivm, 0xf);
610 args[1] = uint->zero;
611 args[2] = uint->zero;
612 args[3] = lp_build_const_int32(base->gallivm,
613 V_008DFC_SQ_EXP_POS + 2 + reg_index);
614 args[4] = uint->zero;
615 lp_build_intrinsic(base->gallivm->builder,
616 "llvm.SI.export",
617 LLVMVoidTypeInContext(base->gallivm->context),
618 args, 9);
619 }
620}
621
Tom Stellarda75c6162012-01-06 17:38:37 -0500622/* XXX: This is partially implemented for VS only at this point. It is not complete */
623static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
624{
625 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200626 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500627 struct lp_build_context * base = &bld_base->base;
628 struct lp_build_context * uint =
629 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
630 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100631 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500632 LLVMValueRef last_args[9] = { 0 };
Michel Dänzer0afeea52013-05-02 14:53:17 +0200633 unsigned semantic_name;
Christian König35088152012-08-01 22:35:24 +0200634 unsigned color_count = 0;
635 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100636 int depth_index = -1, stencil_index = -1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500637
638 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500639 struct tgsi_full_declaration *d =
640 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500641 unsigned target;
642 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500643 int i;
644
645 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100646
647 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
648 parse->FullToken.FullProperty.Property.PropertyName ==
649 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
650 shader->fs_write_all = TRUE;
651
Tom Stellarda75c6162012-01-06 17:38:37 -0500652 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
653 continue;
654
655 switch (d->Declaration.File) {
656 case TGSI_FILE_INPUT:
657 i = shader->ninput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200658 assert(i < Elements(shader->input));
Tom Stellarda75c6162012-01-06 17:38:37 -0500659 shader->input[i].name = d->Semantic.Name;
660 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200661 shader->input[i].interpolate = d->Interp.Interpolate;
662 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200663 continue;
664
Tom Stellarda75c6162012-01-06 17:38:37 -0500665 case TGSI_FILE_OUTPUT:
666 i = shader->noutput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200667 assert(i < Elements(shader->output));
Tom Stellarda75c6162012-01-06 17:38:37 -0500668 shader->output[i].name = d->Semantic.Name;
669 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200670 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500671 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500672
Christian König35088152012-08-01 22:35:24 +0200673 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500674 continue;
Christian König35088152012-08-01 22:35:24 +0200675 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500676
Michel Dänzer0afeea52013-05-02 14:53:17 +0200677 semantic_name = d->Semantic.Name;
678handle_semantic:
Tom Stellarda75c6162012-01-06 17:38:37 -0500679 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500680 /* Select the correct target */
Michel Dänzer0afeea52013-05-02 14:53:17 +0200681 switch(semantic_name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400682 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer4730dea2013-05-03 17:59:34 +0200683 shader->vs_out_misc_write = 1;
684 shader->vs_out_point_size = 1;
685 target = V_008DFC_SQ_EXP_POS + 1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500686 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100687 case TGSI_SEMANTIC_POSITION:
688 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
689 target = V_008DFC_SQ_EXP_POS;
690 break;
691 } else {
692 depth_index = index;
693 continue;
694 }
695 case TGSI_SEMANTIC_STENCIL:
696 stencil_index = index;
697 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500698 case TGSI_SEMANTIC_COLOR:
699 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200700 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500701 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200702 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500703 param_count++;
704 } else {
705 target = V_008DFC_SQ_EXP_MRT + color_count;
Michel Dänzer7708a862012-11-02 15:57:30 +0100706 if (color_count == 0 &&
Christian Königa0dca442013-03-22 15:59:22 +0100707 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer7708a862012-11-02 15:57:30 +0100708 si_alpha_test(bld_base, index);
709
Tom Stellarda75c6162012-01-06 17:38:37 -0500710 color_count++;
711 }
712 break;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200713 case TGSI_SEMANTIC_CLIPDIST:
714 shader->clip_dist_write |=
715 d->Declaration.UsageMask << (d->Semantic.Index << 2);
716 target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index;
717 break;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200718 case TGSI_SEMANTIC_CLIPVERTEX:
719 si_llvm_emit_clipvertex(bld_base, index);
720 shader->clip_dist_write = 0xFF;
721 continue;
Michel Dänzer30b30372012-09-06 17:53:04 +0200722 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500723 case TGSI_SEMANTIC_GENERIC:
724 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200725 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500726 param_count++;
727 break;
728 default:
729 target = 0;
730 fprintf(stderr,
731 "Warning: SI unhandled output type:%d\n",
Michel Dänzer0afeea52013-05-02 14:53:17 +0200732 semantic_name);
Tom Stellarda75c6162012-01-06 17:38:37 -0500733 }
734
Michel Dänzer26c71392012-08-24 12:03:11 +0200735 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500736
737 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ?
Michel Dänzer0afeea52013-05-02 14:53:17 +0200738 (semantic_name == TGSI_SEMANTIC_POSITION) :
739 (semantic_name == TGSI_SEMANTIC_COLOR)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500740 if (last_args[0]) {
741 lp_build_intrinsic(base->gallivm->builder,
742 "llvm.SI.export",
743 LLVMVoidTypeInContext(base->gallivm->context),
744 last_args, 9);
745 }
746
747 memcpy(last_args, args, sizeof(args));
748 } else {
749 lp_build_intrinsic(base->gallivm->builder,
750 "llvm.SI.export",
751 LLVMVoidTypeInContext(base->gallivm->context),
752 args, 9);
753 }
754
755 }
Michel Dänzer0afeea52013-05-02 14:53:17 +0200756
757 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
758 semantic_name = TGSI_SEMANTIC_GENERIC;
759 goto handle_semantic;
760 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500761 }
762
Michel Dänzer1a616c12012-11-13 17:35:09 +0100763 if (depth_index >= 0 || stencil_index >= 0) {
764 LLVMValueRef out_ptr;
765 unsigned mask = 0;
766
767 /* Specify the target we are exporting */
768 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
769
770 if (depth_index >= 0) {
771 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
772 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
773 mask |= 0x1;
774
775 if (stencil_index < 0) {
776 args[6] =
777 args[7] =
778 args[8] = args[5];
779 }
780 }
781
782 if (stencil_index >= 0) {
783 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
784 args[7] =
785 args[8] =
786 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
787 mask |= 0x2;
788
789 if (depth_index < 0)
790 args[5] = args[6];
791 }
792
793 /* Specify which components to enable */
794 args[0] = lp_build_const_int32(base->gallivm, mask);
795
796 args[1] =
797 args[2] =
798 args[4] = uint->zero;
799
800 if (last_args[0])
801 lp_build_intrinsic(base->gallivm->builder,
802 "llvm.SI.export",
803 LLVMVoidTypeInContext(base->gallivm->context),
804 args, 9);
805 else
806 memcpy(last_args, args, sizeof(args));
807 }
808
Christian Königf18fd252012-07-25 21:58:46 +0200809 if (!last_args[0]) {
810 assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
811
812 /* Specify which components to enable */
813 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
814
815 /* Specify the target we are exporting */
816 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
817
818 /* Set COMPR flag to zero to export data as 32-bit */
819 last_args[4] = uint->zero;
820
821 /* dummy bits */
822 last_args[5]= uint->zero;
823 last_args[6]= uint->zero;
824 last_args[7]= uint->zero;
825 last_args[8]= uint->zero;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100826
827 si_shader_ctx->shader->spi_shader_col_format |=
828 V_028714_SPI_SHADER_32_ABGR;
Michel Dänzere369f402013-04-30 16:34:10 +0200829 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Christian Königf18fd252012-07-25 21:58:46 +0200830 }
831
Tom Stellarda75c6162012-01-06 17:38:37 -0500832 /* Specify whether the EXEC mask represents the valid mask */
833 last_args[1] = lp_build_const_int32(base->gallivm,
834 si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
835
Michel Dänzerc8402702013-02-12 18:37:22 +0100836 if (shader->fs_write_all && shader->nr_cbufs > 1) {
837 int i;
838
839 /* Specify that this is not yet the last export */
840 last_args[2] = lp_build_const_int32(base->gallivm, 0);
841
842 for (i = 1; i < shader->nr_cbufs; i++) {
843 /* Specify the target we are exporting */
844 last_args[3] = lp_build_const_int32(base->gallivm,
845 V_008DFC_SQ_EXP_MRT + i);
846
847 lp_build_intrinsic(base->gallivm->builder,
848 "llvm.SI.export",
849 LLVMVoidTypeInContext(base->gallivm->context),
850 last_args, 9);
851
852 si_shader_ctx->shader->spi_shader_col_format |=
853 si_shader_ctx->shader->spi_shader_col_format << 4;
Michel Dänzere369f402013-04-30 16:34:10 +0200854 si_shader_ctx->shader->cb_shader_mask |=
855 si_shader_ctx->shader->cb_shader_mask << 4;
Michel Dänzerc8402702013-02-12 18:37:22 +0100856 }
857
858 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
859 }
860
Tom Stellarda75c6162012-01-06 17:38:37 -0500861 /* Specify that this is the last export */
862 last_args[2] = lp_build_const_int32(base->gallivm, 1);
863
864 lp_build_intrinsic(base->gallivm->builder,
865 "llvm.SI.export",
866 LLVMVoidTypeInContext(base->gallivm->context),
867 last_args, 9);
868
869/* XXX: Look up what this function does */
870/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
871}
872
873static void tex_fetch_args(
874 struct lp_build_tgsi_context * bld_base,
875 struct lp_build_emit_data * emit_data)
876{
Christian König55fe5cc2013-03-04 16:30:06 +0100877 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100878 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200879 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100880 unsigned opcode = inst->Instruction.Opcode;
881 unsigned target = inst->Texture.Texture;
Michel Dänzera6b83c02013-02-21 16:10:55 +0100882 unsigned sampler_src;
Michel Dänzer120efee2013-01-25 12:10:11 +0100883 LLVMValueRef coords[4];
884 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100885 int ref_pos;
886 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +0100887 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100888 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400889
Michel Dänzer120efee2013-01-25 12:10:11 +0100890 /* Fetch and project texture coordinates */
891 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100892 for (chan = 0; chan < 3; chan++ ) {
893 coords[chan] = lp_build_emit_fetch(bld_base,
894 emit_data->inst, 0,
895 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100896 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200897 coords[chan] = lp_build_emit_llvm_binary(bld_base,
898 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100899 coords[chan],
900 coords[3]);
901 }
902
Michel Dänzer120efee2013-01-25 12:10:11 +0100903 if (opcode == TGSI_OPCODE_TXP)
904 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500905
Michel Dänzer120efee2013-01-25 12:10:11 +0100906 /* Pack LOD bias value */
907 if (opcode == TGSI_OPCODE_TXB)
908 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400909
Michel Dänzer0495adb2013-05-06 12:45:14 +0200910 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100911 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100912
913 /* Pack depth comparison value */
914 switch (target) {
915 case TGSI_TEXTURE_SHADOW1D:
916 case TGSI_TEXTURE_SHADOW1D_ARRAY:
917 case TGSI_TEXTURE_SHADOW2D:
918 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +0100919 case TGSI_TEXTURE_SHADOWCUBE:
920 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100921 assert(ref_pos >= 0);
922 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +0100923 break;
924 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
925 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +0100926 }
927
Michel Dänzera6b83c02013-02-21 16:10:55 +0100928 /* Pack user derivatives */
929 if (opcode == TGSI_OPCODE_TXD) {
930 for (chan = 0; chan < 2; chan++) {
931 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
932 if (num_coords > 1)
933 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
934 }
935 }
936
Michel Dänzer120efee2013-01-25 12:10:11 +0100937 /* Pack texture coordinates */
938 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100939 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +0100940 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100941 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +0100942 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +0100943
Michel Dänzer120efee2013-01-25 12:10:11 +0100944 /* Pack array slice */
945 switch (target) {
946 case TGSI_TEXTURE_1D_ARRAY:
947 address[count++] = coords[1];
948 }
949 switch (target) {
950 case TGSI_TEXTURE_2D_ARRAY:
951 case TGSI_TEXTURE_2D_ARRAY_MSAA:
952 case TGSI_TEXTURE_SHADOW2D_ARRAY:
953 address[count++] = coords[2];
954 }
955 switch (target) {
956 case TGSI_TEXTURE_CUBE_ARRAY:
957 case TGSI_TEXTURE_SHADOW1D_ARRAY:
958 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
959 address[count++] = coords[3];
960 }
961
962 /* Pack LOD */
Michel Dänzer36231112013-05-02 09:44:45 +0200963 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +0100964 address[count++] = coords[3];
965
966 if (count > 16) {
967 assert(!"Cannot handle more than 16 texture address parameters");
968 count = 16;
969 }
970
971 for (chan = 0; chan < count; chan++ ) {
972 address[chan] = LLVMBuildBitCast(gallivm->builder,
973 address[chan],
974 LLVMInt32TypeInContext(gallivm->context),
975 "");
976 }
977
Michel Dänzera6b83c02013-02-21 16:10:55 +0100978 sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
979
Michel Dänzer36231112013-05-02 09:44:45 +0200980 /* Resource */
Michel Dänzera6b83c02013-02-21 16:10:55 +0100981 emit_data->args[1] = si_shader_ctx->resources[emit_data->inst->Src[sampler_src].Register.Index];
Michel Dänzer36231112013-05-02 09:44:45 +0200982
983 if (opcode == TGSI_OPCODE_TXF) {
984 /* add tex offsets */
985 if (inst->Texture.NumOffsets) {
986 struct lp_build_context *uint_bld = &bld_base->uint_bld;
987 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
988 const struct tgsi_texture_offset * off = inst->TexOffsets;
989
990 assert(inst->Texture.NumOffsets == 1);
991
992 address[0] =
993 lp_build_add(uint_bld, address[0],
994 bld->immediates[off->Index][off->SwizzleX]);
995 if (num_coords > 1)
996 address[1] =
997 lp_build_add(uint_bld, address[1],
998 bld->immediates[off->Index][off->SwizzleY]);
999 if (num_coords > 2)
1000 address[2] =
1001 lp_build_add(uint_bld, address[2],
1002 bld->immediates[off->Index][off->SwizzleZ]);
1003 }
1004
1005 emit_data->dst_type = LLVMVectorType(
1006 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1007 4);
1008
1009 emit_data->arg_count = 3;
1010 } else {
1011 /* Sampler */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001012 emit_data->args[2] = si_shader_ctx->samplers[emit_data->inst->Src[sampler_src].Register.Index];
Michel Dänzer36231112013-05-02 09:44:45 +02001013
1014 emit_data->dst_type = LLVMVectorType(
1015 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1016 4);
1017
1018 emit_data->arg_count = 4;
1019 }
1020
1021 /* Dimensions */
1022 emit_data->args[emit_data->arg_count - 1] =
1023 lp_build_const_int32(bld_base->base.gallivm, target);
1024
Michel Dänzer120efee2013-01-25 12:10:11 +01001025 /* Pad to power of two vector */
1026 while (count < util_next_power_of_two(count))
1027 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1028
Christian Königccf3e8f2013-03-26 15:09:27 +01001029 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001030}
1031
Michel Dänzer07eddc42013-02-06 15:43:10 +01001032static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1033 struct lp_build_tgsi_context * bld_base,
1034 struct lp_build_emit_data * emit_data)
1035{
1036 struct lp_build_context * base = &bld_base->base;
1037 char intr_name[23];
1038
1039 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001040 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001041
Christian König44e32242013-03-20 12:10:35 +01001042 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001043 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001044 emit_data->args, emit_data->arg_count,
1045 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001046}
1047
Michel Dänzer0495adb2013-05-06 12:45:14 +02001048static void txq_fetch_args(
1049 struct lp_build_tgsi_context * bld_base,
1050 struct lp_build_emit_data * emit_data)
1051{
1052 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1053 const struct tgsi_full_instruction *inst = emit_data->inst;
1054
1055 /* Mip level */
1056 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1057
1058 /* Resource */
1059 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1060
1061 /* Dimensions */
1062 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1063 inst->Texture.Texture);
1064
1065 emit_data->arg_count = 3;
1066
1067 emit_data->dst_type = LLVMVectorType(
1068 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1069 4);
1070}
1071
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001072#if HAVE_LLVM >= 0x0304
1073
1074static void si_llvm_emit_ddxy(
1075 const struct lp_build_tgsi_action * action,
1076 struct lp_build_tgsi_context * bld_base,
1077 struct lp_build_emit_data * emit_data)
1078{
1079 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1080 struct gallivm_state *gallivm = bld_base->base.gallivm;
1081 struct lp_build_context * base = &bld_base->base;
1082 const struct tgsi_full_instruction *inst = emit_data->inst;
1083 unsigned opcode = inst->Instruction.Opcode;
1084 LLVMValueRef indices[2];
1085 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1086 LLVMValueRef tl, trbl, result[4];
1087 LLVMTypeRef i32;
1088 unsigned swizzle[4];
1089 unsigned c;
1090
1091 i32 = LLVMInt32TypeInContext(gallivm->context);
1092
1093 indices[0] = bld_base->uint_bld.zero;
1094 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1095 NULL, 0, LLVMReadNoneAttribute);
1096 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1097 indices, 2, "");
1098
1099 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1100 lp_build_const_int32(gallivm, 0xfffffffc), "");
1101 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1102 indices, 2, "");
1103
1104 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1105 lp_build_const_int32(gallivm,
1106 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1107 "");
1108 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1109 indices, 2, "");
1110
1111 for (c = 0; c < 4; ++c) {
1112 unsigned i;
1113
1114 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1115 for (i = 0; i < c; ++i) {
1116 if (swizzle[i] == swizzle[c]) {
1117 result[c] = result[i];
1118 break;
1119 }
1120 }
1121 if (i != c)
1122 continue;
1123
1124 LLVMBuildStore(gallivm->builder,
1125 LLVMBuildBitCast(gallivm->builder,
1126 lp_build_emit_fetch(bld_base, inst, 0, c),
1127 i32, ""),
1128 store_ptr);
1129
1130 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1131 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1132
1133 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1134 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1135
1136 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1137 }
1138
1139 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1140}
1141
1142#endif /* HAVE_LLVM >= 0x0304 */
1143
Tom Stellarda75c6162012-01-06 17:38:37 -05001144static const struct lp_build_tgsi_action tex_action = {
1145 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001146 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001147 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001148};
1149
Michel Dänzer3e205132012-11-06 17:39:01 +01001150static const struct lp_build_tgsi_action txb_action = {
1151 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001152 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001153 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001154};
1155
Michel Dänzera6b83c02013-02-21 16:10:55 +01001156#if HAVE_LLVM >= 0x0304
1157static const struct lp_build_tgsi_action txd_action = {
1158 .fetch_args = tex_fetch_args,
1159 .emit = build_tex_intrinsic,
1160 .intr_name = "llvm.SI.sampled."
1161};
1162#endif
1163
Michel Dänzer36231112013-05-02 09:44:45 +02001164static const struct lp_build_tgsi_action txf_action = {
1165 .fetch_args = tex_fetch_args,
1166 .emit = build_tex_intrinsic,
1167 .intr_name = "llvm.SI.imageload."
1168};
1169
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001170static const struct lp_build_tgsi_action txl_action = {
1171 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001172 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001173 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001174};
1175
Michel Dänzer0495adb2013-05-06 12:45:14 +02001176static const struct lp_build_tgsi_action txq_action = {
1177 .fetch_args = txq_fetch_args,
1178 .emit = build_tgsi_intrinsic_nomem,
1179 .intr_name = "llvm.SI.resinfo"
1180};
1181
Christian König206f0592013-03-20 14:37:21 +01001182static void create_meta_data(struct si_shader_context *si_shader_ctx)
1183{
1184 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1185 LLVMValueRef args[3];
1186
1187 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1188 args[1] = 0;
1189 args[2] = lp_build_const_int32(gallivm, 1);
1190
1191 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1192}
1193
Christian König55fe5cc2013-03-04 16:30:06 +01001194static void create_function(struct si_shader_context *si_shader_ctx)
1195{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001196 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1197 struct gallivm_state *gallivm = bld_base->base.gallivm;
Christian König0666ffd2013-03-05 15:07:39 +01001198 LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
Christian König55fe5cc2013-03-04 16:30:06 +01001199 unsigned i;
1200
Christian König55fe5cc2013-03-04 16:30:06 +01001201 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01001202 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01001203 f32 = LLVMFloatTypeInContext(gallivm->context);
1204 v2i32 = LLVMVectorType(i32, 2);
1205 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01001206
Christian Königf5298b02013-02-28 14:50:07 +01001207 params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
1208 params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
Christian König55fe5cc2013-03-04 16:30:06 +01001209 params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
1210
Christian Königc4973212013-03-05 12:14:02 +01001211 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Christian König55fe5cc2013-03-04 16:30:06 +01001212 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER];
Christian Königcf9b31f2013-03-21 18:30:23 +01001213 params[SI_PARAM_START_INSTANCE] = i32;
Christian Könige4ed5872013-03-21 18:02:52 +01001214 params[SI_PARAM_VERTEX_ID] = i32;
1215 params[SI_PARAM_DUMMY_0] = i32;
1216 params[SI_PARAM_DUMMY_1] = i32;
1217 params[SI_PARAM_INSTANCE_ID] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01001218 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 9);
Christian König0666ffd2013-03-05 15:07:39 +01001219
Christian Königc4973212013-03-05 12:14:02 +01001220 } else {
Christian König0666ffd2013-03-05 15:07:39 +01001221 params[SI_PARAM_PRIM_MASK] = i32;
1222 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
1223 params[SI_PARAM_PERSP_CENTER] = v2i32;
1224 params[SI_PARAM_PERSP_CENTROID] = v2i32;
1225 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
1226 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
1227 params[SI_PARAM_LINEAR_CENTER] = v2i32;
1228 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
1229 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
1230 params[SI_PARAM_POS_X_FLOAT] = f32;
1231 params[SI_PARAM_POS_Y_FLOAT] = f32;
1232 params[SI_PARAM_POS_Z_FLOAT] = f32;
1233 params[SI_PARAM_POS_W_FLOAT] = f32;
1234 params[SI_PARAM_FRONT_FACE] = f32;
1235 params[SI_PARAM_ANCILLARY] = f32;
1236 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
1237 params[SI_PARAM_POS_FIXED_PT] = f32;
1238 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20);
Christian Königc4973212013-03-05 12:14:02 +01001239 }
Christian König55fe5cc2013-03-04 16:30:06 +01001240
1241 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
1242 for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) {
1243 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
1244 LLVMAddAttribute(P, LLVMInRegAttribute);
1245 }
Christian Königcf9b31f2013-03-21 18:30:23 +01001246
1247 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
1248 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1249 SI_PARAM_START_INSTANCE);
1250 LLVMAddAttribute(P, LLVMInRegAttribute);
1251 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001252
1253#if HAVE_LLVM >= 0x0304
1254 if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
1255 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0)
1256 si_shader_ctx->ddxy_lds =
1257 LLVMAddGlobalInAddressSpace(gallivm->module,
1258 LLVMArrayType(i32, 64),
1259 "ddxy_lds",
1260 LOCAL_ADDR_SPACE);
1261#endif
Christian König55fe5cc2013-03-04 16:30:06 +01001262}
Tom Stellarda75c6162012-01-06 17:38:37 -05001263
Christian König0f6cf2b2013-03-15 15:53:25 +01001264static void preload_constants(struct si_shader_context *si_shader_ctx)
1265{
1266 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1267 struct gallivm_state * gallivm = bld_base->base.gallivm;
1268 const struct tgsi_shader_info * info = bld_base->info;
1269
1270 unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1;
1271
1272 LLVMValueRef ptr;
1273
1274 if (num_const == 0)
1275 return;
1276
1277 /* Allocate space for the constant values */
1278 si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef));
1279
1280 /* Load the resource descriptor */
1281 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1282 si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
1283
1284 /* Load the constants, we rely on the code sinking to do the rest */
1285 for (i = 0; i < num_const * 4; ++i) {
1286 LLVMValueRef args[2] = {
1287 si_shader_ctx->const_resource,
1288 lp_build_const_int32(gallivm, i * 4)
1289 };
1290 si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1291 bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1292 }
1293}
1294
Christian König1c100182013-03-17 16:02:42 +01001295static void preload_samplers(struct si_shader_context *si_shader_ctx)
1296{
1297 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1298 struct gallivm_state * gallivm = bld_base->base.gallivm;
1299 const struct tgsi_shader_info * info = bld_base->info;
1300
1301 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1302
1303 LLVMValueRef res_ptr, samp_ptr;
1304 LLVMValueRef offset;
1305
1306 if (num_samplers == 0)
1307 return;
1308
1309 /* Allocate space for the values */
1310 si_shader_ctx->resources = CALLOC(num_samplers, sizeof(LLVMValueRef));
1311 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1312
1313 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1314 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1315
1316 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1317 for (i = 0; i < num_samplers; ++i) {
1318
1319 /* Resource */
1320 offset = lp_build_const_int32(gallivm, i);
1321 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1322
1323 /* Sampler */
1324 offset = lp_build_const_int32(gallivm, i);
1325 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
1326 }
1327}
1328
Tom Stellard302f53d2012-10-25 13:50:10 -04001329int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader,
1330 LLVMModuleRef mod)
1331{
Tom Stellard302f53d2012-10-25 13:50:10 -04001332 unsigned i;
1333 uint32_t *ptr;
1334 bool dump;
Tom Stellard7782d192013-04-04 09:57:13 -07001335 struct radeon_llvm_binary binary;
Tom Stellard302f53d2012-10-25 13:50:10 -04001336
1337 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
1338
Tom Stellard7782d192013-04-04 09:57:13 -07001339 memset(&binary, 0, sizeof(binary));
1340 radeon_llvm_compile(mod, &binary,
1341 r600_get_llvm_processor_name(rctx->screen->family), dump);
Tom Stellard302f53d2012-10-25 13:50:10 -04001342 if (dump) {
1343 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07001344 for (i = 0; i < binary.code_size; i+=4 ) {
1345 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
1346 binary.code[i + 2], binary.code[i + 1],
1347 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04001348 }
1349 }
1350
Tom Stellardd50343d2013-04-04 16:21:06 -04001351 /* XXX: We may be able to emit some of these values directly rather than
1352 * extracting fields to be emitted later.
1353 */
1354 for (i = 0; i < binary.config_size; i+= 8) {
1355 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
1356 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
1357 switch (reg) {
1358 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
1359 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
1360 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
1361 case R_00B848_COMPUTE_PGM_RSRC1:
1362 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
1363 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
1364 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001365 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
1366 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
1367 break;
1368 case R_00B84C_COMPUTE_PGM_RSRC2:
1369 shader->lds_size = G_00B84C_LDS_SIZE(value);
1370 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04001371 case R_0286CC_SPI_PS_INPUT_ENA:
1372 shader->spi_ps_input_ena = value;
1373 break;
1374 default:
1375 fprintf(stderr, "Warning: Compiler emitted unknown "
1376 "config register: 0x%x\n", reg);
1377 break;
1378 }
1379 }
Tom Stellard302f53d2012-10-25 13:50:10 -04001380
1381 /* copy new shader */
1382 si_resource_reference(&shader->bo, NULL);
1383 shader->bo = si_resource_create_custom(rctx->context.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04001384 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001385 if (shader->bo == NULL) {
1386 return -ENOMEM;
1387 }
1388
1389 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
1390 if (0 /*R600_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04001391 for (i = 0; i < binary.code_size / 4; ++i) {
1392 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04001393 }
1394 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04001395 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001396 }
1397 rctx->ws->buffer_unmap(shader->bo->cs_buf);
1398
Tom Stellard7782d192013-04-04 09:57:13 -07001399 free(binary.code);
1400 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04001401
1402 return 0;
1403}
1404
Tom Stellarda75c6162012-01-06 17:38:37 -05001405int si_pipe_shader_create(
1406 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01001407 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05001408{
1409 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001410 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05001411 struct si_shader_context si_shader_ctx;
1412 struct tgsi_shader_info shader_info;
1413 struct lp_build_tgsi_context * bld_base;
1414 LLVMModuleRef mod;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001415 bool dump;
Tom Stellard302f53d2012-10-25 13:50:10 -04001416 int r = 0;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001417
1418 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -05001419
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001420 assert(shader->shader.noutput == 0);
1421 assert(shader->shader.ninterp == 0);
1422 assert(shader->shader.ninput == 0);
1423
Michel Dänzercfebaf92012-08-31 19:04:08 +02001424 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001425 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1426 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1427
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001428 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001429
Michel Dänzere44dfd42012-11-07 17:33:08 +01001430 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01001431 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001432 bld_base->info = &shader_info;
1433 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001434 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1435
1436 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001437 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01001438#if HAVE_LLVM >= 0x0304
1439 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
1440#endif
Michel Dänzer36231112013-05-02 09:44:45 +02001441 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001442 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001443 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02001444 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001445
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001446#if HAVE_LLVM >= 0x0304
1447 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
1448 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
1449#endif
1450
Tom Stellarda75c6162012-01-06 17:38:37 -05001451 si_shader_ctx.radeon_bld.load_input = declare_input;
Christian Könige4ed5872013-03-21 18:02:52 +01001452 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001453 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001454 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1455 si_shader_ctx.shader = shader;
1456 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05001457
Christian König206f0592013-03-20 14:37:21 +01001458 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01001459 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01001460 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01001461 preload_samplers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001462
Christian König835098a2012-07-17 21:28:10 +02001463 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001464
Tom Stellard185fc9a2012-07-12 10:40:47 -04001465 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1466 * conversion fails. */
1467 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001468 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001469 }
1470
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001471 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001472 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Christian König0f6cf2b2013-03-15 15:53:25 +01001473 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001474 FREE(si_shader_ctx.resources);
1475 FREE(si_shader_ctx.samplers);
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001476 return -EINVAL;
1477 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001478
1479 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1480
1481 mod = bld_base->base.gallivm->module;
Tom Stellard302f53d2012-10-25 13:50:10 -04001482 r = si_compile_llvm(rctx, shader, mod);
Tom Stellarda75c6162012-01-06 17:38:37 -05001483
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001484 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001485 tgsi_parse_free(&si_shader_ctx.parse);
1486
Christian König0f6cf2b2013-03-15 15:53:25 +01001487 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001488 FREE(si_shader_ctx.resources);
1489 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05001490
Tom Stellard302f53d2012-10-25 13:50:10 -04001491 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05001492}
1493
1494void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1495{
Christian Königfe412872012-07-24 18:47:19 +02001496 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001497}