blob: dd9581dd95af16e5d643e182c7a7d22ffc3f4245 [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,
Michel Dänzerb00269a2013-08-07 18:14:16 +0200565 LLVMValueRef (*pos)[9], unsigned index)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200566{
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;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200570 unsigned reg_index;
571 unsigned chan;
572 unsigned const_chan;
573 LLVMValueRef out_elts[4];
574 LLVMValueRef base_elt;
575 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
576 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, uint->one);
577
578 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
579 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
580 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
581 }
582
583 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200584 LLVMValueRef *args = pos[2 + reg_index];
585
Michel Dänzere3befbc2013-05-15 18:09:50 +0200586 args[5] =
587 args[6] =
588 args[7] =
589 args[8] = lp_build_const_float(base->gallivm, 0.0f);
590
591 /* Compute dot products of position and user clip plane vectors */
592 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
593 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
594 args[0] = const_resource;
595 args[1] = lp_build_const_int32(base->gallivm,
596 ((reg_index * 4 + chan) * 4 +
597 const_chan) * 4);
598 base_elt = build_intrinsic(base->gallivm->builder,
599 "llvm.SI.load.const",
600 base->elem_type,
601 args, 2,
602 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
603 args[5 + chan] =
604 lp_build_add(base, args[5 + chan],
605 lp_build_mul(base, base_elt,
606 out_elts[const_chan]));
607 }
608 }
609
610 args[0] = lp_build_const_int32(base->gallivm, 0xf);
611 args[1] = uint->zero;
612 args[2] = uint->zero;
613 args[3] = lp_build_const_int32(base->gallivm,
614 V_008DFC_SQ_EXP_POS + 2 + reg_index);
615 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200616 }
617}
618
Tom Stellarda75c6162012-01-06 17:38:37 -0500619/* XXX: This is partially implemented for VS only at this point. It is not complete */
620static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
621{
622 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200623 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500624 struct lp_build_context * base = &bld_base->base;
625 struct lp_build_context * uint =
626 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
627 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100628 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500629 LLVMValueRef last_args[9] = { 0 };
Michel Dänzerb00269a2013-08-07 18:14:16 +0200630 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer0afeea52013-05-02 14:53:17 +0200631 unsigned semantic_name;
Christian König35088152012-08-01 22:35:24 +0200632 unsigned color_count = 0;
633 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100634 int depth_index = -1, stencil_index = -1;
Michel Dänzerb00269a2013-08-07 18:14:16 +0200635 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -0500636
637 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500638 struct tgsi_full_declaration *d =
639 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500640 unsigned target;
641 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500642
643 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100644
645 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
646 parse->FullToken.FullProperty.Property.PropertyName ==
647 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
648 shader->fs_write_all = TRUE;
649
Tom Stellarda75c6162012-01-06 17:38:37 -0500650 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
651 continue;
652
653 switch (d->Declaration.File) {
654 case TGSI_FILE_INPUT:
655 i = shader->ninput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200656 assert(i < Elements(shader->input));
Tom Stellarda75c6162012-01-06 17:38:37 -0500657 shader->input[i].name = d->Semantic.Name;
658 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200659 shader->input[i].interpolate = d->Interp.Interpolate;
660 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200661 continue;
662
Tom Stellarda75c6162012-01-06 17:38:37 -0500663 case TGSI_FILE_OUTPUT:
664 i = shader->noutput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200665 assert(i < Elements(shader->output));
Tom Stellarda75c6162012-01-06 17:38:37 -0500666 shader->output[i].name = d->Semantic.Name;
667 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200668 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500669 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500670
Christian König35088152012-08-01 22:35:24 +0200671 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500672 continue;
Christian König35088152012-08-01 22:35:24 +0200673 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500674
Michel Dänzer0afeea52013-05-02 14:53:17 +0200675 semantic_name = d->Semantic.Name;
676handle_semantic:
Tom Stellarda75c6162012-01-06 17:38:37 -0500677 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500678 /* Select the correct target */
Michel Dänzer0afeea52013-05-02 14:53:17 +0200679 switch(semantic_name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400680 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer4730dea2013-05-03 17:59:34 +0200681 shader->vs_out_misc_write = 1;
682 shader->vs_out_point_size = 1;
683 target = V_008DFC_SQ_EXP_POS + 1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500684 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100685 case TGSI_SEMANTIC_POSITION:
686 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
687 target = V_008DFC_SQ_EXP_POS;
688 break;
689 } else {
690 depth_index = index;
691 continue;
692 }
693 case TGSI_SEMANTIC_STENCIL:
694 stencil_index = index;
695 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500696 case TGSI_SEMANTIC_COLOR:
697 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200698 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500699 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200700 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500701 param_count++;
702 } else {
703 target = V_008DFC_SQ_EXP_MRT + color_count;
Michel Dänzer7708a862012-11-02 15:57:30 +0100704 if (color_count == 0 &&
Christian Königa0dca442013-03-22 15:59:22 +0100705 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer7708a862012-11-02 15:57:30 +0100706 si_alpha_test(bld_base, index);
707
Tom Stellarda75c6162012-01-06 17:38:37 -0500708 color_count++;
709 }
710 break;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200711 case TGSI_SEMANTIC_CLIPDIST:
712 shader->clip_dist_write |=
713 d->Declaration.UsageMask << (d->Semantic.Index << 2);
714 target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index;
715 break;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200716 case TGSI_SEMANTIC_CLIPVERTEX:
Michel Dänzerb00269a2013-08-07 18:14:16 +0200717 si_llvm_emit_clipvertex(bld_base, pos_args, index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200718 shader->clip_dist_write = 0xFF;
719 continue;
Michel Dänzer30b30372012-09-06 17:53:04 +0200720 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500721 case TGSI_SEMANTIC_GENERIC:
722 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200723 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500724 param_count++;
725 break;
726 default:
727 target = 0;
728 fprintf(stderr,
729 "Warning: SI unhandled output type:%d\n",
Michel Dänzer0afeea52013-05-02 14:53:17 +0200730 semantic_name);
Tom Stellarda75c6162012-01-06 17:38:37 -0500731 }
732
Michel Dänzer26c71392012-08-24 12:03:11 +0200733 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500734
Michel Dänzerb00269a2013-08-07 18:14:16 +0200735 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
736 target >= V_008DFC_SQ_EXP_POS &&
737 target <= (V_008DFC_SQ_EXP_POS + 3)) {
738 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
739 args, sizeof(args));
740 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT &&
741 semantic_name == TGSI_SEMANTIC_COLOR) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500742 if (last_args[0]) {
743 lp_build_intrinsic(base->gallivm->builder,
744 "llvm.SI.export",
745 LLVMVoidTypeInContext(base->gallivm->context),
746 last_args, 9);
747 }
748
749 memcpy(last_args, args, sizeof(args));
750 } else {
751 lp_build_intrinsic(base->gallivm->builder,
752 "llvm.SI.export",
753 LLVMVoidTypeInContext(base->gallivm->context),
754 args, 9);
755 }
756
757 }
Michel Dänzer0afeea52013-05-02 14:53:17 +0200758
759 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
760 semantic_name = TGSI_SEMANTIC_GENERIC;
761 goto handle_semantic;
762 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500763 }
764
Michel Dänzer1a616c12012-11-13 17:35:09 +0100765 if (depth_index >= 0 || stencil_index >= 0) {
766 LLVMValueRef out_ptr;
767 unsigned mask = 0;
768
769 /* Specify the target we are exporting */
770 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
771
772 if (depth_index >= 0) {
773 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
774 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
775 mask |= 0x1;
776
777 if (stencil_index < 0) {
778 args[6] =
779 args[7] =
780 args[8] = args[5];
781 }
782 }
783
784 if (stencil_index >= 0) {
785 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
786 args[7] =
787 args[8] =
788 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
789 mask |= 0x2;
790
791 if (depth_index < 0)
792 args[5] = args[6];
793 }
794
795 /* Specify which components to enable */
796 args[0] = lp_build_const_int32(base->gallivm, mask);
797
798 args[1] =
799 args[2] =
800 args[4] = uint->zero;
801
802 if (last_args[0])
803 lp_build_intrinsic(base->gallivm->builder,
804 "llvm.SI.export",
805 LLVMVoidTypeInContext(base->gallivm->context),
806 args, 9);
807 else
808 memcpy(last_args, args, sizeof(args));
809 }
810
Michel Dänzerb00269a2013-08-07 18:14:16 +0200811 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
812 unsigned pos_idx = 0;
Christian Königf18fd252012-07-25 21:58:46 +0200813
Michel Dänzerb00269a2013-08-07 18:14:16 +0200814 for (i = 0; i < 4; i++)
815 if (pos_args[i][0])
816 shader->nr_pos_exports++;
Christian Königf18fd252012-07-25 21:58:46 +0200817
Michel Dänzerb00269a2013-08-07 18:14:16 +0200818 for (i = 0; i < 4; i++) {
819 if (!pos_args[i][0])
820 continue;
Christian Königf18fd252012-07-25 21:58:46 +0200821
Michel Dänzerc8402702013-02-12 18:37:22 +0100822 /* Specify the target we are exporting */
Michel Dänzerb00269a2013-08-07 18:14:16 +0200823 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
824
825 if (pos_idx == shader->nr_pos_exports)
826 /* Specify that this is the last export */
827 pos_args[i][2] = uint->one;
Michel Dänzerc8402702013-02-12 18:37:22 +0100828
829 lp_build_intrinsic(base->gallivm->builder,
830 "llvm.SI.export",
831 LLVMVoidTypeInContext(base->gallivm->context),
Michel Dänzerb00269a2013-08-07 18:14:16 +0200832 pos_args[i], 9);
833 }
834 } else {
835 if (!last_args[0]) {
836 /* Specify which components to enable */
837 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
838
839 /* Specify the target we are exporting */
840 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
841
842 /* Set COMPR flag to zero to export data as 32-bit */
843 last_args[4] = uint->zero;
844
845 /* dummy bits */
846 last_args[5]= uint->zero;
847 last_args[6]= uint->zero;
848 last_args[7]= uint->zero;
849 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +0100850
851 si_shader_ctx->shader->spi_shader_col_format |=
Michel Dänzerb00269a2013-08-07 18:14:16 +0200852 V_028714_SPI_SHADER_32_ABGR;
853 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +0100854 }
855
Michel Dänzerb00269a2013-08-07 18:14:16 +0200856 /* Specify whether the EXEC mask represents the valid mask */
857 last_args[1] = uint->one;
858
859 if (shader->fs_write_all && shader->nr_cbufs > 1) {
860 int i;
861
862 /* Specify that this is not yet the last export */
863 last_args[2] = lp_build_const_int32(base->gallivm, 0);
864
865 for (i = 1; i < shader->nr_cbufs; i++) {
866 /* Specify the target we are exporting */
867 last_args[3] = lp_build_const_int32(base->gallivm,
868 V_008DFC_SQ_EXP_MRT + i);
869
870 lp_build_intrinsic(base->gallivm->builder,
871 "llvm.SI.export",
872 LLVMVoidTypeInContext(base->gallivm->context),
873 last_args, 9);
874
875 si_shader_ctx->shader->spi_shader_col_format |=
876 si_shader_ctx->shader->spi_shader_col_format << 4;
877 si_shader_ctx->shader->cb_shader_mask |=
878 si_shader_ctx->shader->cb_shader_mask << 4;
879 }
880
881 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
882 }
883
884 /* Specify that this is the last export */
885 last_args[2] = lp_build_const_int32(base->gallivm, 1);
886
887 lp_build_intrinsic(base->gallivm->builder,
888 "llvm.SI.export",
889 LLVMVoidTypeInContext(base->gallivm->context),
890 last_args, 9);
Michel Dänzerc8402702013-02-12 18:37:22 +0100891 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500892/* XXX: Look up what this function does */
893/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
894}
895
896static void tex_fetch_args(
897 struct lp_build_tgsi_context * bld_base,
898 struct lp_build_emit_data * emit_data)
899{
Christian König55fe5cc2013-03-04 16:30:06 +0100900 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100901 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200902 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100903 unsigned opcode = inst->Instruction.Opcode;
904 unsigned target = inst->Texture.Texture;
Michel Dänzera6b83c02013-02-21 16:10:55 +0100905 unsigned sampler_src;
Michel Dänzer120efee2013-01-25 12:10:11 +0100906 LLVMValueRef coords[4];
907 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100908 int ref_pos;
909 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +0100910 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100911 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400912
Michel Dänzer120efee2013-01-25 12:10:11 +0100913 /* Fetch and project texture coordinates */
914 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100915 for (chan = 0; chan < 3; chan++ ) {
916 coords[chan] = lp_build_emit_fetch(bld_base,
917 emit_data->inst, 0,
918 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100919 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200920 coords[chan] = lp_build_emit_llvm_binary(bld_base,
921 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100922 coords[chan],
923 coords[3]);
924 }
925
Michel Dänzer120efee2013-01-25 12:10:11 +0100926 if (opcode == TGSI_OPCODE_TXP)
927 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500928
Michel Dänzer120efee2013-01-25 12:10:11 +0100929 /* Pack LOD bias value */
930 if (opcode == TGSI_OPCODE_TXB)
931 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400932
Michel Dänzer0495adb2013-05-06 12:45:14 +0200933 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100934 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100935
936 /* Pack depth comparison value */
937 switch (target) {
938 case TGSI_TEXTURE_SHADOW1D:
939 case TGSI_TEXTURE_SHADOW1D_ARRAY:
940 case TGSI_TEXTURE_SHADOW2D:
941 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +0100942 case TGSI_TEXTURE_SHADOWCUBE:
943 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100944 assert(ref_pos >= 0);
945 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +0100946 break;
947 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
948 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +0100949 }
950
Michel Dänzera6b83c02013-02-21 16:10:55 +0100951 /* Pack user derivatives */
952 if (opcode == TGSI_OPCODE_TXD) {
953 for (chan = 0; chan < 2; chan++) {
954 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
955 if (num_coords > 1)
956 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
957 }
958 }
959
Michel Dänzer120efee2013-01-25 12:10:11 +0100960 /* Pack texture coordinates */
961 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100962 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +0100963 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100964 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +0100965 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +0100966
Michel Dänzer120efee2013-01-25 12:10:11 +0100967 /* Pack array slice */
968 switch (target) {
969 case TGSI_TEXTURE_1D_ARRAY:
970 address[count++] = coords[1];
971 }
972 switch (target) {
973 case TGSI_TEXTURE_2D_ARRAY:
974 case TGSI_TEXTURE_2D_ARRAY_MSAA:
975 case TGSI_TEXTURE_SHADOW2D_ARRAY:
976 address[count++] = coords[2];
977 }
978 switch (target) {
979 case TGSI_TEXTURE_CUBE_ARRAY:
980 case TGSI_TEXTURE_SHADOW1D_ARRAY:
981 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
982 address[count++] = coords[3];
983 }
984
985 /* Pack LOD */
Michel Dänzer36231112013-05-02 09:44:45 +0200986 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +0100987 address[count++] = coords[3];
988
989 if (count > 16) {
990 assert(!"Cannot handle more than 16 texture address parameters");
991 count = 16;
992 }
993
994 for (chan = 0; chan < count; chan++ ) {
995 address[chan] = LLVMBuildBitCast(gallivm->builder,
996 address[chan],
997 LLVMInt32TypeInContext(gallivm->context),
998 "");
999 }
1000
Michel Dänzera6b83c02013-02-21 16:10:55 +01001001 sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1002
Michel Dänzer36231112013-05-02 09:44:45 +02001003 /* Resource */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001004 emit_data->args[1] = si_shader_ctx->resources[emit_data->inst->Src[sampler_src].Register.Index];
Michel Dänzer36231112013-05-02 09:44:45 +02001005
1006 if (opcode == TGSI_OPCODE_TXF) {
1007 /* add tex offsets */
1008 if (inst->Texture.NumOffsets) {
1009 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1010 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1011 const struct tgsi_texture_offset * off = inst->TexOffsets;
1012
1013 assert(inst->Texture.NumOffsets == 1);
1014
1015 address[0] =
1016 lp_build_add(uint_bld, address[0],
1017 bld->immediates[off->Index][off->SwizzleX]);
1018 if (num_coords > 1)
1019 address[1] =
1020 lp_build_add(uint_bld, address[1],
1021 bld->immediates[off->Index][off->SwizzleY]);
1022 if (num_coords > 2)
1023 address[2] =
1024 lp_build_add(uint_bld, address[2],
1025 bld->immediates[off->Index][off->SwizzleZ]);
1026 }
1027
1028 emit_data->dst_type = LLVMVectorType(
1029 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1030 4);
1031
1032 emit_data->arg_count = 3;
1033 } else {
1034 /* Sampler */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001035 emit_data->args[2] = si_shader_ctx->samplers[emit_data->inst->Src[sampler_src].Register.Index];
Michel Dänzer36231112013-05-02 09:44:45 +02001036
1037 emit_data->dst_type = LLVMVectorType(
1038 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1039 4);
1040
1041 emit_data->arg_count = 4;
1042 }
1043
1044 /* Dimensions */
1045 emit_data->args[emit_data->arg_count - 1] =
1046 lp_build_const_int32(bld_base->base.gallivm, target);
1047
Michel Dänzer120efee2013-01-25 12:10:11 +01001048 /* Pad to power of two vector */
1049 while (count < util_next_power_of_two(count))
1050 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1051
Christian Königccf3e8f2013-03-26 15:09:27 +01001052 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001053}
1054
Michel Dänzer07eddc42013-02-06 15:43:10 +01001055static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1056 struct lp_build_tgsi_context * bld_base,
1057 struct lp_build_emit_data * emit_data)
1058{
1059 struct lp_build_context * base = &bld_base->base;
1060 char intr_name[23];
1061
1062 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001063 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001064
Christian König44e32242013-03-20 12:10:35 +01001065 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001066 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001067 emit_data->args, emit_data->arg_count,
1068 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001069}
1070
Michel Dänzer0495adb2013-05-06 12:45:14 +02001071static void txq_fetch_args(
1072 struct lp_build_tgsi_context * bld_base,
1073 struct lp_build_emit_data * emit_data)
1074{
1075 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1076 const struct tgsi_full_instruction *inst = emit_data->inst;
1077
1078 /* Mip level */
1079 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1080
1081 /* Resource */
1082 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1083
1084 /* Dimensions */
1085 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1086 inst->Texture.Texture);
1087
1088 emit_data->arg_count = 3;
1089
1090 emit_data->dst_type = LLVMVectorType(
1091 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1092 4);
1093}
1094
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001095#if HAVE_LLVM >= 0x0304
1096
1097static void si_llvm_emit_ddxy(
1098 const struct lp_build_tgsi_action * action,
1099 struct lp_build_tgsi_context * bld_base,
1100 struct lp_build_emit_data * emit_data)
1101{
1102 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1103 struct gallivm_state *gallivm = bld_base->base.gallivm;
1104 struct lp_build_context * base = &bld_base->base;
1105 const struct tgsi_full_instruction *inst = emit_data->inst;
1106 unsigned opcode = inst->Instruction.Opcode;
1107 LLVMValueRef indices[2];
1108 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1109 LLVMValueRef tl, trbl, result[4];
1110 LLVMTypeRef i32;
1111 unsigned swizzle[4];
1112 unsigned c;
1113
1114 i32 = LLVMInt32TypeInContext(gallivm->context);
1115
1116 indices[0] = bld_base->uint_bld.zero;
1117 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1118 NULL, 0, LLVMReadNoneAttribute);
1119 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1120 indices, 2, "");
1121
1122 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1123 lp_build_const_int32(gallivm, 0xfffffffc), "");
1124 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1125 indices, 2, "");
1126
1127 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1128 lp_build_const_int32(gallivm,
1129 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1130 "");
1131 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1132 indices, 2, "");
1133
1134 for (c = 0; c < 4; ++c) {
1135 unsigned i;
1136
1137 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1138 for (i = 0; i < c; ++i) {
1139 if (swizzle[i] == swizzle[c]) {
1140 result[c] = result[i];
1141 break;
1142 }
1143 }
1144 if (i != c)
1145 continue;
1146
1147 LLVMBuildStore(gallivm->builder,
1148 LLVMBuildBitCast(gallivm->builder,
1149 lp_build_emit_fetch(bld_base, inst, 0, c),
1150 i32, ""),
1151 store_ptr);
1152
1153 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1154 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1155
1156 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1157 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1158
1159 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1160 }
1161
1162 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1163}
1164
1165#endif /* HAVE_LLVM >= 0x0304 */
1166
Tom Stellarda75c6162012-01-06 17:38:37 -05001167static const struct lp_build_tgsi_action tex_action = {
1168 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001169 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001170 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001171};
1172
Michel Dänzer3e205132012-11-06 17:39:01 +01001173static const struct lp_build_tgsi_action txb_action = {
1174 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001175 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001176 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001177};
1178
Michel Dänzera6b83c02013-02-21 16:10:55 +01001179#if HAVE_LLVM >= 0x0304
1180static const struct lp_build_tgsi_action txd_action = {
1181 .fetch_args = tex_fetch_args,
1182 .emit = build_tex_intrinsic,
1183 .intr_name = "llvm.SI.sampled."
1184};
1185#endif
1186
Michel Dänzer36231112013-05-02 09:44:45 +02001187static const struct lp_build_tgsi_action txf_action = {
1188 .fetch_args = tex_fetch_args,
1189 .emit = build_tex_intrinsic,
1190 .intr_name = "llvm.SI.imageload."
1191};
1192
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001193static const struct lp_build_tgsi_action txl_action = {
1194 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001195 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001196 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001197};
1198
Michel Dänzer0495adb2013-05-06 12:45:14 +02001199static const struct lp_build_tgsi_action txq_action = {
1200 .fetch_args = txq_fetch_args,
1201 .emit = build_tgsi_intrinsic_nomem,
1202 .intr_name = "llvm.SI.resinfo"
1203};
1204
Christian König206f0592013-03-20 14:37:21 +01001205static void create_meta_data(struct si_shader_context *si_shader_ctx)
1206{
1207 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1208 LLVMValueRef args[3];
1209
1210 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1211 args[1] = 0;
1212 args[2] = lp_build_const_int32(gallivm, 1);
1213
1214 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1215}
1216
Christian König55fe5cc2013-03-04 16:30:06 +01001217static void create_function(struct si_shader_context *si_shader_ctx)
1218{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001219 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1220 struct gallivm_state *gallivm = bld_base->base.gallivm;
Christian König0666ffd2013-03-05 15:07:39 +01001221 LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
Christian König55fe5cc2013-03-04 16:30:06 +01001222 unsigned i;
1223
Christian König55fe5cc2013-03-04 16:30:06 +01001224 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01001225 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01001226 f32 = LLVMFloatTypeInContext(gallivm->context);
1227 v2i32 = LLVMVectorType(i32, 2);
1228 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01001229
Christian Königf5298b02013-02-28 14:50:07 +01001230 params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
1231 params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
Christian König55fe5cc2013-03-04 16:30:06 +01001232 params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
1233
Christian Königc4973212013-03-05 12:14:02 +01001234 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Christian König55fe5cc2013-03-04 16:30:06 +01001235 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER];
Christian Königcf9b31f2013-03-21 18:30:23 +01001236 params[SI_PARAM_START_INSTANCE] = i32;
Christian Könige4ed5872013-03-21 18:02:52 +01001237 params[SI_PARAM_VERTEX_ID] = i32;
1238 params[SI_PARAM_DUMMY_0] = i32;
1239 params[SI_PARAM_DUMMY_1] = i32;
1240 params[SI_PARAM_INSTANCE_ID] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01001241 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 9);
Christian König0666ffd2013-03-05 15:07:39 +01001242
Christian Königc4973212013-03-05 12:14:02 +01001243 } else {
Christian König0666ffd2013-03-05 15:07:39 +01001244 params[SI_PARAM_PRIM_MASK] = i32;
1245 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
1246 params[SI_PARAM_PERSP_CENTER] = v2i32;
1247 params[SI_PARAM_PERSP_CENTROID] = v2i32;
1248 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
1249 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
1250 params[SI_PARAM_LINEAR_CENTER] = v2i32;
1251 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
1252 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
1253 params[SI_PARAM_POS_X_FLOAT] = f32;
1254 params[SI_PARAM_POS_Y_FLOAT] = f32;
1255 params[SI_PARAM_POS_Z_FLOAT] = f32;
1256 params[SI_PARAM_POS_W_FLOAT] = f32;
1257 params[SI_PARAM_FRONT_FACE] = f32;
1258 params[SI_PARAM_ANCILLARY] = f32;
1259 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
1260 params[SI_PARAM_POS_FIXED_PT] = f32;
1261 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20);
Christian Königc4973212013-03-05 12:14:02 +01001262 }
Christian König55fe5cc2013-03-04 16:30:06 +01001263
1264 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
1265 for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) {
1266 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
1267 LLVMAddAttribute(P, LLVMInRegAttribute);
1268 }
Christian Königcf9b31f2013-03-21 18:30:23 +01001269
1270 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
1271 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1272 SI_PARAM_START_INSTANCE);
1273 LLVMAddAttribute(P, LLVMInRegAttribute);
1274 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001275
1276#if HAVE_LLVM >= 0x0304
1277 if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
1278 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0)
1279 si_shader_ctx->ddxy_lds =
1280 LLVMAddGlobalInAddressSpace(gallivm->module,
1281 LLVMArrayType(i32, 64),
1282 "ddxy_lds",
1283 LOCAL_ADDR_SPACE);
1284#endif
Christian König55fe5cc2013-03-04 16:30:06 +01001285}
Tom Stellarda75c6162012-01-06 17:38:37 -05001286
Christian König0f6cf2b2013-03-15 15:53:25 +01001287static void preload_constants(struct si_shader_context *si_shader_ctx)
1288{
1289 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1290 struct gallivm_state * gallivm = bld_base->base.gallivm;
1291 const struct tgsi_shader_info * info = bld_base->info;
1292
1293 unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1;
1294
1295 LLVMValueRef ptr;
1296
1297 if (num_const == 0)
1298 return;
1299
1300 /* Allocate space for the constant values */
1301 si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef));
1302
1303 /* Load the resource descriptor */
1304 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1305 si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
1306
1307 /* Load the constants, we rely on the code sinking to do the rest */
1308 for (i = 0; i < num_const * 4; ++i) {
1309 LLVMValueRef args[2] = {
1310 si_shader_ctx->const_resource,
1311 lp_build_const_int32(gallivm, i * 4)
1312 };
1313 si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1314 bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1315 }
1316}
1317
Christian König1c100182013-03-17 16:02:42 +01001318static void preload_samplers(struct si_shader_context *si_shader_ctx)
1319{
1320 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1321 struct gallivm_state * gallivm = bld_base->base.gallivm;
1322 const struct tgsi_shader_info * info = bld_base->info;
1323
1324 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1325
1326 LLVMValueRef res_ptr, samp_ptr;
1327 LLVMValueRef offset;
1328
1329 if (num_samplers == 0)
1330 return;
1331
1332 /* Allocate space for the values */
1333 si_shader_ctx->resources = CALLOC(num_samplers, sizeof(LLVMValueRef));
1334 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1335
1336 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1337 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1338
1339 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1340 for (i = 0; i < num_samplers; ++i) {
1341
1342 /* Resource */
1343 offset = lp_build_const_int32(gallivm, i);
1344 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1345
1346 /* Sampler */
1347 offset = lp_build_const_int32(gallivm, i);
1348 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
1349 }
1350}
1351
Tom Stellard302f53d2012-10-25 13:50:10 -04001352int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader,
1353 LLVMModuleRef mod)
1354{
Tom Stellard302f53d2012-10-25 13:50:10 -04001355 unsigned i;
1356 uint32_t *ptr;
1357 bool dump;
Tom Stellard7782d192013-04-04 09:57:13 -07001358 struct radeon_llvm_binary binary;
Tom Stellard302f53d2012-10-25 13:50:10 -04001359
1360 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
1361
Tom Stellard7782d192013-04-04 09:57:13 -07001362 memset(&binary, 0, sizeof(binary));
1363 radeon_llvm_compile(mod, &binary,
1364 r600_get_llvm_processor_name(rctx->screen->family), dump);
Tom Stellard302f53d2012-10-25 13:50:10 -04001365 if (dump) {
1366 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07001367 for (i = 0; i < binary.code_size; i+=4 ) {
1368 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
1369 binary.code[i + 2], binary.code[i + 1],
1370 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04001371 }
1372 }
1373
Tom Stellardd50343d2013-04-04 16:21:06 -04001374 /* XXX: We may be able to emit some of these values directly rather than
1375 * extracting fields to be emitted later.
1376 */
1377 for (i = 0; i < binary.config_size; i+= 8) {
1378 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
1379 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
1380 switch (reg) {
1381 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
1382 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
1383 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
1384 case R_00B848_COMPUTE_PGM_RSRC1:
1385 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
1386 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
1387 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001388 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
1389 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
1390 break;
1391 case R_00B84C_COMPUTE_PGM_RSRC2:
1392 shader->lds_size = G_00B84C_LDS_SIZE(value);
1393 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04001394 case R_0286CC_SPI_PS_INPUT_ENA:
1395 shader->spi_ps_input_ena = value;
1396 break;
1397 default:
1398 fprintf(stderr, "Warning: Compiler emitted unknown "
1399 "config register: 0x%x\n", reg);
1400 break;
1401 }
1402 }
Tom Stellard302f53d2012-10-25 13:50:10 -04001403
1404 /* copy new shader */
1405 si_resource_reference(&shader->bo, NULL);
1406 shader->bo = si_resource_create_custom(rctx->context.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04001407 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001408 if (shader->bo == NULL) {
1409 return -ENOMEM;
1410 }
1411
1412 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
1413 if (0 /*R600_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04001414 for (i = 0; i < binary.code_size / 4; ++i) {
1415 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04001416 }
1417 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04001418 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001419 }
1420 rctx->ws->buffer_unmap(shader->bo->cs_buf);
1421
Tom Stellard7782d192013-04-04 09:57:13 -07001422 free(binary.code);
1423 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04001424
1425 return 0;
1426}
1427
Tom Stellarda75c6162012-01-06 17:38:37 -05001428int si_pipe_shader_create(
1429 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01001430 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05001431{
1432 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001433 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05001434 struct si_shader_context si_shader_ctx;
1435 struct tgsi_shader_info shader_info;
1436 struct lp_build_tgsi_context * bld_base;
1437 LLVMModuleRef mod;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001438 bool dump;
Tom Stellard302f53d2012-10-25 13:50:10 -04001439 int r = 0;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001440
1441 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -05001442
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001443 assert(shader->shader.noutput == 0);
1444 assert(shader->shader.ninterp == 0);
1445 assert(shader->shader.ninput == 0);
1446
Michel Dänzercfebaf92012-08-31 19:04:08 +02001447 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001448 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1449 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1450
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001451 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001452
Michel Dänzere44dfd42012-11-07 17:33:08 +01001453 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01001454 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001455 bld_base->info = &shader_info;
1456 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001457 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1458
1459 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001460 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01001461#if HAVE_LLVM >= 0x0304
1462 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
1463#endif
Michel Dänzer36231112013-05-02 09:44:45 +02001464 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001465 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001466 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02001467 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001468
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001469#if HAVE_LLVM >= 0x0304
1470 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
1471 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
1472#endif
1473
Tom Stellarda75c6162012-01-06 17:38:37 -05001474 si_shader_ctx.radeon_bld.load_input = declare_input;
Christian Könige4ed5872013-03-21 18:02:52 +01001475 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001476 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001477 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1478 si_shader_ctx.shader = shader;
1479 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05001480
Christian König206f0592013-03-20 14:37:21 +01001481 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01001482 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01001483 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01001484 preload_samplers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001485
Christian König835098a2012-07-17 21:28:10 +02001486 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001487
Tom Stellard185fc9a2012-07-12 10:40:47 -04001488 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1489 * conversion fails. */
1490 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001491 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001492 }
1493
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001494 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001495 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Christian König0f6cf2b2013-03-15 15:53:25 +01001496 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001497 FREE(si_shader_ctx.resources);
1498 FREE(si_shader_ctx.samplers);
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001499 return -EINVAL;
1500 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001501
1502 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1503
1504 mod = bld_base->base.gallivm->module;
Tom Stellard302f53d2012-10-25 13:50:10 -04001505 r = si_compile_llvm(rctx, shader, mod);
Tom Stellarda75c6162012-01-06 17:38:37 -05001506
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001507 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001508 tgsi_parse_free(&si_shader_ctx.parse);
1509
Christian König0f6cf2b2013-03-15 15:53:25 +01001510 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001511 FREE(si_shader_ctx.resources);
1512 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05001513
Tom Stellard302f53d2012-10-25 13:50:10 -04001514 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05001515}
1516
1517void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1518{
Christian Königfe412872012-07-24 18:47:19 +02001519 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001520}