blob: 77915aefee7c60caffda8e05e4be6211b269aa53 [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;
Michel Dänzer237cb072013-08-21 18:00:35 +0200201 struct lp_build_context *uint =
202 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -0500203 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400204 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100205 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
206
207 LLVMValueRef interp_param;
208 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500209
210 /* This value is:
211 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
212 * quad begins a new primitive. Bit 0 always needs
213 * to be unset)
214 * [32:16] ParamOffset
215 *
216 */
Christian König55fe5cc2013-03-04 16:30:06 +0100217 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200218 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500219
Christian König0666ffd2013-03-05 15:07:39 +0100220 unsigned chan;
221
Tom Stellard0fb1e682012-09-06 16:18:11 -0400222 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
223 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400224 unsigned soa_index =
225 radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellard0fb1e682012-09-06 16:18:11 -0400226 si_shader_ctx->radeon_bld.inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100227 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100228
229 if (chan == 3)
230 /* RCP for fragcoord.w */
231 si_shader_ctx->radeon_bld.inputs[soa_index] =
232 LLVMBuildFDiv(gallivm->builder,
233 lp_build_const_float(gallivm, 1.0f),
234 si_shader_ctx->radeon_bld.inputs[soa_index],
235 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400236 }
237 return;
238 }
239
Michel Dänzer97078b12012-09-25 12:41:31 +0200240 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
241 LLVMValueRef face, is_face_positive;
242
Christian König0666ffd2013-03-05 15:07:39 +0100243 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
244
Michel Dänzer97078b12012-09-25 12:41:31 +0200245 is_face_positive = LLVMBuildFCmp(gallivm->builder,
246 LLVMRealUGT, face,
247 lp_build_const_float(gallivm, 0.0f),
248 "");
249
250 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
251 LLVMBuildSelect(gallivm->builder,
252 is_face_positive,
253 lp_build_const_float(gallivm, 1.0f),
254 lp_build_const_float(gallivm, 0.0f),
255 "");
256 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
257 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
258 lp_build_const_float(gallivm, 0.0f);
259 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
260 lp_build_const_float(gallivm, 1.0f);
261
262 return;
263 }
264
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200265 shader->input[input_index].param_offset = shader->ninterp++;
266 attr_number = lp_build_const_int32(gallivm,
267 shader->input[input_index].param_offset);
268
Tom Stellarda75c6162012-01-06 17:38:37 -0500269 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200270 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500271 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100272 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100273 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200274 } else {
275 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100276 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200277 else
Christian König0666ffd2013-03-05 15:07:39 +0100278 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200279 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500280 break;
281 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100282 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500283 break;
284 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200285 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100286 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200287 else
Christian König0666ffd2013-03-05 15:07:39 +0100288 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200289 break;
290 case TGSI_INTERPOLATE_PERSPECTIVE:
291 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100292 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200293 else
Christian König0666ffd2013-03-05 15:07:39 +0100294 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500295 break;
296 default:
297 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
298 return;
299 }
300
Christian König0666ffd2013-03-05 15:07:39 +0100301 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
302
Tom Stellarda75c6162012-01-06 17:38:37 -0500303 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200304 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100305 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100306 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200307 LLVMValueRef face, is_face_positive;
308 LLVMValueRef back_attr_number =
309 lp_build_const_int32(gallivm,
310 shader->input[input_index].param_offset + 1);
311
Christian König0666ffd2013-03-05 15:07:39 +0100312 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
313
Michel Dänzer691f08d2012-09-06 18:03:38 +0200314 is_face_positive = LLVMBuildFCmp(gallivm->builder,
315 LLVMRealUGT, face,
316 lp_build_const_float(gallivm, 0.0f),
317 "");
318
Tom Stellarda75c6162012-01-06 17:38:37 -0500319 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100320 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200321 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
322 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
323 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
324 LLVMValueRef front, back;
325
326 args[0] = llvm_chan;
327 args[1] = attr_number;
328 front = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100329 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100330 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200331
332 args[1] = back_attr_number;
333 back = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100334 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100335 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200336
337 si_shader_ctx->radeon_bld.inputs[soa_index] =
338 LLVMBuildSelect(gallivm->builder,
339 is_face_positive,
340 front,
341 back,
342 "");
343 }
344
345 shader->ninterp++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200346 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
347 LLVMValueRef args[4];
348
349 args[0] = uint->zero;
350 args[1] = attr_number;
351 args[2] = params;
352 args[3] = interp_param;
353 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
354 build_intrinsic(base->gallivm->builder, intr_name,
355 input_type, args, args[3] ? 4 : 3,
356 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
357 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
358 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
359 lp_build_const_float(gallivm, 0.0f);
360 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
361 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200362 } else {
363 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100364 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200365 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
366 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
367 args[0] = llvm_chan;
368 args[1] = attr_number;
369 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100370 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200371 si_shader_ctx->radeon_bld.inputs[soa_index] =
372 build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100373 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100374 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200375 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500376 }
377}
378
379static void declare_input(
380 struct radeon_llvm_context * radeon_bld,
381 unsigned input_index,
382 const struct tgsi_full_declaration *decl)
383{
384 struct si_shader_context * si_shader_ctx =
385 si_shader_context(&radeon_bld->soa.bld_base);
386 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
387 declare_input_vs(si_shader_ctx, input_index, decl);
388 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
389 declare_input_fs(si_shader_ctx, input_index, decl);
390 } else {
391 fprintf(stderr, "Warning: Unsupported shader type,\n");
392 }
393}
394
Christian Könige4ed5872013-03-21 18:02:52 +0100395static void declare_system_value(
396 struct radeon_llvm_context * radeon_bld,
397 unsigned index,
398 const struct tgsi_full_declaration *decl)
399{
Christian Königcf9b31f2013-03-21 18:30:23 +0100400
Christian Könige4ed5872013-03-21 18:02:52 +0100401 LLVMValueRef value = 0;
402
403 switch (decl->Semantic.Name) {
404 case TGSI_SEMANTIC_INSTANCEID:
Christian Königa0dca442013-03-22 15:59:22 +0100405 value = get_instance_index(radeon_bld, 1);
Christian Könige4ed5872013-03-21 18:02:52 +0100406 break;
407
408 case TGSI_SEMANTIC_VERTEXID:
409 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_VERTEX_ID);
410 break;
411
412 default:
413 assert(!"unknown system value");
414 return;
415 }
416
417 radeon_bld->system_values[index] = value;
418}
419
Tom Stellarda75c6162012-01-06 17:38:37 -0500420static LLVMValueRef fetch_constant(
421 struct lp_build_tgsi_context * bld_base,
422 const struct tgsi_full_src_register *reg,
423 enum tgsi_opcode_type type,
424 unsigned swizzle)
425{
Christian König55fe5cc2013-03-04 16:30:06 +0100426 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500427 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100428 const struct tgsi_ind_register *ireg = &reg->Indirect;
429 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500430
Christian Königf5298b02013-02-28 14:50:07 +0100431 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100432 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100433 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500434
Christian König8514f5a2013-02-04 17:46:42 +0100435 if (swizzle == LP_CHAN_ALL) {
436 unsigned chan;
437 LLVMValueRef values[4];
438 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
439 values[chan] = fetch_constant(bld_base, reg, type, chan);
440
441 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
442 }
443
Christian König0f6cf2b2013-03-15 15:53:25 +0100444 idx = reg->Register.Index * 4 + swizzle;
445 if (!reg->Register.Indirect)
446 return bitcast(bld_base, type, si_shader_ctx->constants[idx]);
Christian Königf5298b02013-02-28 14:50:07 +0100447
Christian König0f6cf2b2013-03-15 15:53:25 +0100448 args[0] = si_shader_ctx->const_resource;
449 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
450 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
451 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
452 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
453 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200454
Christian Königf5298b02013-02-28 14:50:07 +0100455 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100456 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500457
Christian Königf5298b02013-02-28 14:50:07 +0100458 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500459}
460
Michel Dänzer26c71392012-08-24 12:03:11 +0200461/* Initialize arguments for the shader export intrinsic */
462static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
463 struct tgsi_full_declaration *d,
464 unsigned index,
465 unsigned target,
466 LLVMValueRef *args)
467{
468 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
469 struct lp_build_context *uint =
470 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
471 struct lp_build_context *base = &bld_base->base;
472 unsigned compressed = 0;
473 unsigned chan;
474
Michel Dänzerf402acd2012-08-22 18:15:36 +0200475 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
476 int cbuf = target - V_008DFC_SQ_EXP_MRT;
477
478 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100479 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100480
481 if (compressed)
482 si_shader_ctx->shader->spi_shader_col_format |=
483 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
484 else
485 si_shader_ctx->shader->spi_shader_col_format |=
486 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200487
488 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200489 }
490 }
491
492 if (compressed) {
493 /* Pixel shader needs to pack output values before export */
494 for (chan = 0; chan < 2; chan++ ) {
495 LLVMValueRef *out_ptr =
496 si_shader_ctx->radeon_bld.soa.outputs[index];
497 args[0] = LLVMBuildLoad(base->gallivm->builder,
498 out_ptr[2 * chan], "");
499 args[1] = LLVMBuildLoad(base->gallivm->builder,
500 out_ptr[2 * chan + 1], "");
501 args[chan + 5] =
502 build_intrinsic(base->gallivm->builder,
503 "llvm.SI.packf16",
504 LLVMInt32TypeInContext(base->gallivm->context),
505 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100506 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100507 args[chan + 7] = args[chan + 5] =
508 LLVMBuildBitCast(base->gallivm->builder,
509 args[chan + 5],
510 LLVMFloatTypeInContext(base->gallivm->context),
511 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200512 }
513
514 /* Set COMPR flag */
515 args[4] = uint->one;
516 } else {
517 for (chan = 0; chan < 4; chan++ ) {
518 LLVMValueRef out_ptr =
519 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
520 /* +5 because the first output value will be
521 * the 6th argument to the intrinsic. */
522 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
523 out_ptr, "");
524 }
525
526 /* Clear COMPR flag */
527 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200528 }
529
530 /* XXX: This controls which components of the output
531 * registers actually get exported. (e.g bit 0 means export
532 * X component, bit 1 means export Y component, etc.) I'm
533 * hard coding this to 0xf for now. In the future, we might
534 * want to do something else. */
535 args[0] = lp_build_const_int32(base->gallivm, 0xf);
536
537 /* Specify whether the EXEC mask represents the valid mask */
538 args[1] = uint->zero;
539
540 /* Specify whether this is the last export */
541 args[2] = uint->zero;
542
543 /* Specify the target we are exporting */
544 args[3] = lp_build_const_int32(base->gallivm, target);
545
Michel Dänzer26c71392012-08-24 12:03:11 +0200546 /* XXX: We probably need to keep track of the output
547 * values, so we know what we are passing to the next
548 * stage. */
549}
550
Michel Dänzer7708a862012-11-02 15:57:30 +0100551static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
552 unsigned index)
553{
554 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
555 struct gallivm_state *gallivm = bld_base->base.gallivm;
556
Christian Königa0dca442013-03-22 15:59:22 +0100557 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Michel Dänzer7708a862012-11-02 15:57:30 +0100558 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
559 LLVMValueRef alpha_pass =
560 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100561 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer7708a862012-11-02 15:57:30 +0100562 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
Christian Königa0dca442013-03-22 15:59:22 +0100563 lp_build_const_float(gallivm, si_shader_ctx->shader->key.ps.alpha_ref));
Michel Dänzer7708a862012-11-02 15:57:30 +0100564 LLVMValueRef arg =
565 lp_build_select(&bld_base->base,
566 alpha_pass,
567 lp_build_const_float(gallivm, 1.0f),
568 lp_build_const_float(gallivm, -1.0f));
569
570 build_intrinsic(gallivm->builder,
571 "llvm.AMDGPU.kill",
572 LLVMVoidTypeInContext(gallivm->context),
573 &arg, 1, 0);
574 } else {
575 build_intrinsic(gallivm->builder,
576 "llvm.AMDGPU.kilp",
577 LLVMVoidTypeInContext(gallivm->context),
578 NULL, 0, 0);
579 }
580}
581
Marek Olšák6d4755a2013-07-30 22:29:29 +0200582static void si_alpha_to_one(struct lp_build_tgsi_context *bld_base,
583 unsigned index)
584{
585 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
586
587 /* set alpha to one */
588 LLVMBuildStore(bld_base->base.gallivm->builder,
589 bld_base->base.one,
590 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
591}
592
Michel Dänzere3befbc2013-05-15 18:09:50 +0200593static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzerb00269a2013-08-07 18:14:16 +0200594 LLVMValueRef (*pos)[9], unsigned index)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200595{
596 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200597 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200598 struct lp_build_context *base = &bld_base->base;
599 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200600 unsigned reg_index;
601 unsigned chan;
602 unsigned const_chan;
603 LLVMValueRef out_elts[4];
604 LLVMValueRef base_elt;
605 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
606 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, uint->one);
607
608 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
609 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
610 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
611 }
612
613 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200614 LLVMValueRef *args = pos[2 + reg_index];
615
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200616 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
617 continue;
618
619 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
620
Michel Dänzere3befbc2013-05-15 18:09:50 +0200621 args[5] =
622 args[6] =
623 args[7] =
624 args[8] = lp_build_const_float(base->gallivm, 0.0f);
625
626 /* Compute dot products of position and user clip plane vectors */
627 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
628 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
629 args[0] = const_resource;
630 args[1] = lp_build_const_int32(base->gallivm,
631 ((reg_index * 4 + chan) * 4 +
632 const_chan) * 4);
633 base_elt = build_intrinsic(base->gallivm->builder,
634 "llvm.SI.load.const",
635 base->elem_type,
636 args, 2,
637 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
638 args[5 + chan] =
639 lp_build_add(base, args[5 + chan],
640 lp_build_mul(base, base_elt,
641 out_elts[const_chan]));
642 }
643 }
644
645 args[0] = lp_build_const_int32(base->gallivm, 0xf);
646 args[1] = uint->zero;
647 args[2] = uint->zero;
648 args[3] = lp_build_const_int32(base->gallivm,
649 V_008DFC_SQ_EXP_POS + 2 + reg_index);
650 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200651 }
652}
653
Tom Stellarda75c6162012-01-06 17:38:37 -0500654/* XXX: This is partially implemented for VS only at this point. It is not complete */
655static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
656{
657 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200658 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500659 struct lp_build_context * base = &bld_base->base;
660 struct lp_build_context * uint =
661 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
662 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100663 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500664 LLVMValueRef last_args[9] = { 0 };
Michel Dänzerb00269a2013-08-07 18:14:16 +0200665 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer0afeea52013-05-02 14:53:17 +0200666 unsigned semantic_name;
Christian König35088152012-08-01 22:35:24 +0200667 unsigned color_count = 0;
668 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100669 int depth_index = -1, stencil_index = -1;
Michel Dänzerb00269a2013-08-07 18:14:16 +0200670 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -0500671
672 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500673 struct tgsi_full_declaration *d =
674 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500675 unsigned target;
676 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500677
678 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100679
680 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
681 parse->FullToken.FullProperty.Property.PropertyName ==
682 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
683 shader->fs_write_all = TRUE;
684
Tom Stellarda75c6162012-01-06 17:38:37 -0500685 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
686 continue;
687
688 switch (d->Declaration.File) {
689 case TGSI_FILE_INPUT:
690 i = shader->ninput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200691 assert(i < Elements(shader->input));
Tom Stellarda75c6162012-01-06 17:38:37 -0500692 shader->input[i].name = d->Semantic.Name;
693 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200694 shader->input[i].interpolate = d->Interp.Interpolate;
695 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200696 continue;
697
Tom Stellarda75c6162012-01-06 17:38:37 -0500698 case TGSI_FILE_OUTPUT:
699 i = shader->noutput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200700 assert(i < Elements(shader->output));
Tom Stellarda75c6162012-01-06 17:38:37 -0500701 shader->output[i].name = d->Semantic.Name;
702 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200703 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500704 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500705
Christian König35088152012-08-01 22:35:24 +0200706 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500707 continue;
Christian König35088152012-08-01 22:35:24 +0200708 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500709
Michel Dänzer0afeea52013-05-02 14:53:17 +0200710 semantic_name = d->Semantic.Name;
711handle_semantic:
Tom Stellarda75c6162012-01-06 17:38:37 -0500712 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500713 /* Select the correct target */
Michel Dänzer0afeea52013-05-02 14:53:17 +0200714 switch(semantic_name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400715 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer4730dea2013-05-03 17:59:34 +0200716 shader->vs_out_misc_write = 1;
717 shader->vs_out_point_size = 1;
718 target = V_008DFC_SQ_EXP_POS + 1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500719 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100720 case TGSI_SEMANTIC_POSITION:
721 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
722 target = V_008DFC_SQ_EXP_POS;
723 break;
724 } else {
725 depth_index = index;
726 continue;
727 }
728 case TGSI_SEMANTIC_STENCIL:
729 stencil_index = index;
730 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500731 case TGSI_SEMANTIC_COLOR:
732 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200733 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500734 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200735 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500736 param_count++;
737 } else {
738 target = V_008DFC_SQ_EXP_MRT + color_count;
Marek Olšák6d4755a2013-07-30 22:29:29 +0200739 if (si_shader_ctx->shader->key.ps.alpha_to_one) {
740 si_alpha_to_one(bld_base, index);
741 }
Michel Dänzer7708a862012-11-02 15:57:30 +0100742 if (color_count == 0 &&
Christian Königa0dca442013-03-22 15:59:22 +0100743 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer7708a862012-11-02 15:57:30 +0100744 si_alpha_test(bld_base, index);
745
Tom Stellarda75c6162012-01-06 17:38:37 -0500746 color_count++;
747 }
748 break;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200749 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200750 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
751 (1 << d->Semantic.Index)))
752 continue;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200753 shader->clip_dist_write |=
754 d->Declaration.UsageMask << (d->Semantic.Index << 2);
755 target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index;
756 break;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200757 case TGSI_SEMANTIC_CLIPVERTEX:
Michel Dänzerb00269a2013-08-07 18:14:16 +0200758 si_llvm_emit_clipvertex(bld_base, pos_args, index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200759 continue;
Michel Dänzer30b30372012-09-06 17:53:04 +0200760 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500761 case TGSI_SEMANTIC_GENERIC:
762 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200763 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500764 param_count++;
765 break;
766 default:
767 target = 0;
768 fprintf(stderr,
769 "Warning: SI unhandled output type:%d\n",
Michel Dänzer0afeea52013-05-02 14:53:17 +0200770 semantic_name);
Tom Stellarda75c6162012-01-06 17:38:37 -0500771 }
772
Michel Dänzer26c71392012-08-24 12:03:11 +0200773 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500774
Michel Dänzerb00269a2013-08-07 18:14:16 +0200775 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
776 target >= V_008DFC_SQ_EXP_POS &&
777 target <= (V_008DFC_SQ_EXP_POS + 3)) {
778 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
779 args, sizeof(args));
780 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT &&
781 semantic_name == TGSI_SEMANTIC_COLOR) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500782 if (last_args[0]) {
783 lp_build_intrinsic(base->gallivm->builder,
784 "llvm.SI.export",
785 LLVMVoidTypeInContext(base->gallivm->context),
786 last_args, 9);
787 }
788
789 memcpy(last_args, args, sizeof(args));
790 } else {
791 lp_build_intrinsic(base->gallivm->builder,
792 "llvm.SI.export",
793 LLVMVoidTypeInContext(base->gallivm->context),
794 args, 9);
795 }
796
797 }
Michel Dänzer0afeea52013-05-02 14:53:17 +0200798
799 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
800 semantic_name = TGSI_SEMANTIC_GENERIC;
801 goto handle_semantic;
802 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500803 }
804
Michel Dänzer1a616c12012-11-13 17:35:09 +0100805 if (depth_index >= 0 || stencil_index >= 0) {
806 LLVMValueRef out_ptr;
807 unsigned mask = 0;
808
809 /* Specify the target we are exporting */
810 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
811
812 if (depth_index >= 0) {
813 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
814 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
815 mask |= 0x1;
816
817 if (stencil_index < 0) {
818 args[6] =
819 args[7] =
820 args[8] = args[5];
821 }
822 }
823
824 if (stencil_index >= 0) {
825 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
826 args[7] =
827 args[8] =
828 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +0200829 /* Only setting the stencil component bit (0x2) here
830 * breaks some stencil piglit tests
831 */
832 mask |= 0x3;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100833
834 if (depth_index < 0)
835 args[5] = args[6];
836 }
837
838 /* Specify which components to enable */
839 args[0] = lp_build_const_int32(base->gallivm, mask);
840
841 args[1] =
842 args[2] =
843 args[4] = uint->zero;
844
845 if (last_args[0])
846 lp_build_intrinsic(base->gallivm->builder,
847 "llvm.SI.export",
848 LLVMVoidTypeInContext(base->gallivm->context),
849 args, 9);
850 else
851 memcpy(last_args, args, sizeof(args));
852 }
853
Michel Dänzerb00269a2013-08-07 18:14:16 +0200854 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
855 unsigned pos_idx = 0;
Christian Königf18fd252012-07-25 21:58:46 +0200856
Michel Dänzerb00269a2013-08-07 18:14:16 +0200857 for (i = 0; i < 4; i++)
858 if (pos_args[i][0])
859 shader->nr_pos_exports++;
Christian Königf18fd252012-07-25 21:58:46 +0200860
Michel Dänzerb00269a2013-08-07 18:14:16 +0200861 for (i = 0; i < 4; i++) {
862 if (!pos_args[i][0])
863 continue;
Christian Königf18fd252012-07-25 21:58:46 +0200864
Michel Dänzerc8402702013-02-12 18:37:22 +0100865 /* Specify the target we are exporting */
Michel Dänzerb00269a2013-08-07 18:14:16 +0200866 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
867
868 if (pos_idx == shader->nr_pos_exports)
869 /* Specify that this is the last export */
870 pos_args[i][2] = uint->one;
Michel Dänzerc8402702013-02-12 18:37:22 +0100871
872 lp_build_intrinsic(base->gallivm->builder,
873 "llvm.SI.export",
874 LLVMVoidTypeInContext(base->gallivm->context),
Michel Dänzerb00269a2013-08-07 18:14:16 +0200875 pos_args[i], 9);
876 }
877 } else {
878 if (!last_args[0]) {
879 /* Specify which components to enable */
880 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
881
882 /* Specify the target we are exporting */
883 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
884
885 /* Set COMPR flag to zero to export data as 32-bit */
886 last_args[4] = uint->zero;
887
888 /* dummy bits */
889 last_args[5]= uint->zero;
890 last_args[6]= uint->zero;
891 last_args[7]= uint->zero;
892 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +0100893
894 si_shader_ctx->shader->spi_shader_col_format |=
Michel Dänzerb00269a2013-08-07 18:14:16 +0200895 V_028714_SPI_SHADER_32_ABGR;
896 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +0100897 }
898
Michel Dänzerb00269a2013-08-07 18:14:16 +0200899 /* Specify whether the EXEC mask represents the valid mask */
900 last_args[1] = uint->one;
901
902 if (shader->fs_write_all && shader->nr_cbufs > 1) {
903 int i;
904
905 /* Specify that this is not yet the last export */
906 last_args[2] = lp_build_const_int32(base->gallivm, 0);
907
908 for (i = 1; i < shader->nr_cbufs; i++) {
909 /* Specify the target we are exporting */
910 last_args[3] = lp_build_const_int32(base->gallivm,
911 V_008DFC_SQ_EXP_MRT + i);
912
913 lp_build_intrinsic(base->gallivm->builder,
914 "llvm.SI.export",
915 LLVMVoidTypeInContext(base->gallivm->context),
916 last_args, 9);
917
918 si_shader_ctx->shader->spi_shader_col_format |=
919 si_shader_ctx->shader->spi_shader_col_format << 4;
920 si_shader_ctx->shader->cb_shader_mask |=
921 si_shader_ctx->shader->cb_shader_mask << 4;
922 }
923
924 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
925 }
926
927 /* Specify that this is the last export */
928 last_args[2] = lp_build_const_int32(base->gallivm, 1);
929
930 lp_build_intrinsic(base->gallivm->builder,
931 "llvm.SI.export",
932 LLVMVoidTypeInContext(base->gallivm->context),
933 last_args, 9);
Michel Dänzerc8402702013-02-12 18:37:22 +0100934 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500935/* XXX: Look up what this function does */
936/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
937}
938
Marek Olšák4855acd2013-08-06 15:08:54 +0200939static const struct lp_build_tgsi_action txf_action;
940
941static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
942 struct lp_build_tgsi_context * bld_base,
943 struct lp_build_emit_data * emit_data);
944
Tom Stellarda75c6162012-01-06 17:38:37 -0500945static void tex_fetch_args(
946 struct lp_build_tgsi_context * bld_base,
947 struct lp_build_emit_data * emit_data)
948{
Christian König55fe5cc2013-03-04 16:30:06 +0100949 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100950 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200951 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100952 unsigned opcode = inst->Instruction.Opcode;
953 unsigned target = inst->Texture.Texture;
Marek Olšák4855acd2013-08-06 15:08:54 +0200954 unsigned sampler_src, sampler_index;
Michel Dänzer120efee2013-01-25 12:10:11 +0100955 LLVMValueRef coords[4];
956 LLVMValueRef address[16];
Marek Olšák4855acd2013-08-06 15:08:54 +0200957 LLVMValueRef sample_index_rewrite = NULL;
958 LLVMValueRef sample_chan = NULL;
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100959 int ref_pos;
960 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +0100961 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100962 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400963
Michel Dänzer120efee2013-01-25 12:10:11 +0100964 /* Fetch and project texture coordinates */
965 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100966 for (chan = 0; chan < 3; chan++ ) {
967 coords[chan] = lp_build_emit_fetch(bld_base,
968 emit_data->inst, 0,
969 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100970 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200971 coords[chan] = lp_build_emit_llvm_binary(bld_base,
972 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100973 coords[chan],
974 coords[3]);
975 }
976
Michel Dänzer120efee2013-01-25 12:10:11 +0100977 if (opcode == TGSI_OPCODE_TXP)
978 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500979
Michel Dänzer120efee2013-01-25 12:10:11 +0100980 /* Pack LOD bias value */
981 if (opcode == TGSI_OPCODE_TXB)
982 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400983
Michel Dänzer0495adb2013-05-06 12:45:14 +0200984 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100985 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100986
987 /* Pack depth comparison value */
988 switch (target) {
989 case TGSI_TEXTURE_SHADOW1D:
990 case TGSI_TEXTURE_SHADOW1D_ARRAY:
991 case TGSI_TEXTURE_SHADOW2D:
992 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +0100993 case TGSI_TEXTURE_SHADOWCUBE:
994 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100995 assert(ref_pos >= 0);
996 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +0100997 break;
998 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
999 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001000 }
1001
Michel Dänzera6b83c02013-02-21 16:10:55 +01001002 /* Pack user derivatives */
1003 if (opcode == TGSI_OPCODE_TXD) {
1004 for (chan = 0; chan < 2; chan++) {
1005 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1006 if (num_coords > 1)
1007 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1008 }
1009 }
1010
Michel Dänzer120efee2013-01-25 12:10:11 +01001011 /* Pack texture coordinates */
1012 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001013 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001014 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001015 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001016 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001017
Michel Dänzer120efee2013-01-25 12:10:11 +01001018 /* Pack LOD */
Michel Dänzer36231112013-05-02 09:44:45 +02001019 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001020 address[count++] = coords[3];
1021
1022 if (count > 16) {
1023 assert(!"Cannot handle more than 16 texture address parameters");
1024 count = 16;
1025 }
1026
1027 for (chan = 0; chan < count; chan++ ) {
1028 address[chan] = LLVMBuildBitCast(gallivm->builder,
1029 address[chan],
1030 LLVMInt32TypeInContext(gallivm->context),
1031 "");
1032 }
1033
Michel Dänzera6b83c02013-02-21 16:10:55 +01001034 sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
Marek Olšák4855acd2013-08-06 15:08:54 +02001035 sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1036
1037 /* Adjust the sample index according to FMASK.
1038 *
1039 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1040 * which is the identity mapping. Each nibble says which physical sample
1041 * should be fetched to get that sample.
1042 *
1043 * For example, 0x11111100 means there are only 2 samples stored and
1044 * the second sample covers 3/4 of the pixel. When reading samples 0
1045 * and 1, return physical sample 0 (determined by the first two 0s
1046 * in FMASK), otherwise return physical sample 1.
1047 *
1048 * The sample index should be adjusted as follows:
1049 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1050 */
1051 if (target == TGSI_TEXTURE_2D_MSAA ||
1052 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1053 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1054 struct lp_build_emit_data txf_emit_data = *emit_data;
1055 LLVMValueRef txf_address[16];
1056 unsigned txf_count = count;
1057
1058 memcpy(txf_address, address, sizeof(address));
1059
1060 /* Pad to a power-of-two size. */
1061 while (txf_count < util_next_power_of_two(txf_count))
1062 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1063
1064 /* Read FMASK using TXF. */
1065 txf_emit_data.chan = 0;
1066 txf_emit_data.dst_type = LLVMVectorType(
1067 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1068 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1069 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
1070 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm, target);
1071 txf_emit_data.arg_count = 3;
1072
1073 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1074
1075 /* Initialize some constants. */
1076 if (target == TGSI_TEXTURE_2D_MSAA) {
1077 sample_chan = LLVMConstInt(uint_bld->elem_type, 2, 0);
1078 } else {
1079 sample_chan = LLVMConstInt(uint_bld->elem_type, 3, 0);
1080 }
1081
1082 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1083 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1084
1085 /* Apply the formula. */
1086 LLVMValueRef fmask =
1087 LLVMBuildExtractElement(gallivm->builder,
1088 txf_emit_data.output[0],
1089 uint_bld->zero, "");
1090
1091 LLVMValueRef sample_index =
1092 LLVMBuildExtractElement(gallivm->builder,
1093 txf_emit_data.args[0],
1094 sample_chan, "");
1095
1096 LLVMValueRef sample_index4 =
1097 LLVMBuildMul(gallivm->builder, sample_index, four, "");
1098
1099 LLVMValueRef shifted_fmask =
1100 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1101
1102 LLVMValueRef final_sample =
1103 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1104
1105 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1106 * resource descriptor is 0 (invalid),
1107 */
1108 LLVMValueRef fmask_desc =
1109 LLVMBuildBitCast(gallivm->builder,
1110 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1111 LLVMVectorType(uint_bld->elem_type, 8), "");
1112
1113 LLVMValueRef fmask_word1 =
1114 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1115 uint_bld->one, "");
1116
1117 LLVMValueRef word1_is_nonzero =
1118 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1119 fmask_word1, uint_bld->zero, "");
1120
1121 sample_index_rewrite =
1122 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
1123 final_sample, sample_index, "");
1124 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001125
Michel Dänzer36231112013-05-02 09:44:45 +02001126 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001127 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001128
1129 if (opcode == TGSI_OPCODE_TXF) {
1130 /* add tex offsets */
1131 if (inst->Texture.NumOffsets) {
1132 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1133 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1134 const struct tgsi_texture_offset * off = inst->TexOffsets;
1135
1136 assert(inst->Texture.NumOffsets == 1);
1137
1138 address[0] =
1139 lp_build_add(uint_bld, address[0],
1140 bld->immediates[off->Index][off->SwizzleX]);
1141 if (num_coords > 1)
1142 address[1] =
1143 lp_build_add(uint_bld, address[1],
1144 bld->immediates[off->Index][off->SwizzleY]);
1145 if (num_coords > 2)
1146 address[2] =
1147 lp_build_add(uint_bld, address[2],
1148 bld->immediates[off->Index][off->SwizzleZ]);
1149 }
1150
1151 emit_data->dst_type = LLVMVectorType(
1152 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1153 4);
1154
1155 emit_data->arg_count = 3;
1156 } else {
1157 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001158 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001159
1160 emit_data->dst_type = LLVMVectorType(
1161 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1162 4);
1163
1164 emit_data->arg_count = 4;
1165 }
1166
1167 /* Dimensions */
1168 emit_data->args[emit_data->arg_count - 1] =
1169 lp_build_const_int32(bld_base->base.gallivm, target);
1170
Michel Dänzer120efee2013-01-25 12:10:11 +01001171 /* Pad to power of two vector */
1172 while (count < util_next_power_of_two(count))
1173 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1174
Christian Königccf3e8f2013-03-26 15:09:27 +01001175 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Marek Olšák4855acd2013-08-06 15:08:54 +02001176
1177 /* Replace the MSAA sample index if needed. */
1178 if (sample_index_rewrite) {
1179 emit_data->args[0] =
1180 LLVMBuildInsertElement(gallivm->builder, emit_data->args[0],
1181 sample_index_rewrite, sample_chan, "");
1182 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001183}
1184
Michel Dänzer07eddc42013-02-06 15:43:10 +01001185static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1186 struct lp_build_tgsi_context * bld_base,
1187 struct lp_build_emit_data * emit_data)
1188{
1189 struct lp_build_context * base = &bld_base->base;
1190 char intr_name[23];
1191
1192 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001193 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001194
Christian König44e32242013-03-20 12:10:35 +01001195 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001196 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001197 emit_data->args, emit_data->arg_count,
1198 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001199}
1200
Michel Dänzer0495adb2013-05-06 12:45:14 +02001201static void txq_fetch_args(
1202 struct lp_build_tgsi_context * bld_base,
1203 struct lp_build_emit_data * emit_data)
1204{
1205 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1206 const struct tgsi_full_instruction *inst = emit_data->inst;
1207
1208 /* Mip level */
1209 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1210
1211 /* Resource */
1212 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1213
1214 /* Dimensions */
1215 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1216 inst->Texture.Texture);
1217
1218 emit_data->arg_count = 3;
1219
1220 emit_data->dst_type = LLVMVectorType(
1221 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1222 4);
1223}
1224
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001225#if HAVE_LLVM >= 0x0304
1226
1227static void si_llvm_emit_ddxy(
1228 const struct lp_build_tgsi_action * action,
1229 struct lp_build_tgsi_context * bld_base,
1230 struct lp_build_emit_data * emit_data)
1231{
1232 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1233 struct gallivm_state *gallivm = bld_base->base.gallivm;
1234 struct lp_build_context * base = &bld_base->base;
1235 const struct tgsi_full_instruction *inst = emit_data->inst;
1236 unsigned opcode = inst->Instruction.Opcode;
1237 LLVMValueRef indices[2];
1238 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1239 LLVMValueRef tl, trbl, result[4];
1240 LLVMTypeRef i32;
1241 unsigned swizzle[4];
1242 unsigned c;
1243
1244 i32 = LLVMInt32TypeInContext(gallivm->context);
1245
1246 indices[0] = bld_base->uint_bld.zero;
1247 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1248 NULL, 0, LLVMReadNoneAttribute);
1249 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1250 indices, 2, "");
1251
1252 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1253 lp_build_const_int32(gallivm, 0xfffffffc), "");
1254 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1255 indices, 2, "");
1256
1257 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1258 lp_build_const_int32(gallivm,
1259 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1260 "");
1261 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1262 indices, 2, "");
1263
1264 for (c = 0; c < 4; ++c) {
1265 unsigned i;
1266
1267 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1268 for (i = 0; i < c; ++i) {
1269 if (swizzle[i] == swizzle[c]) {
1270 result[c] = result[i];
1271 break;
1272 }
1273 }
1274 if (i != c)
1275 continue;
1276
1277 LLVMBuildStore(gallivm->builder,
1278 LLVMBuildBitCast(gallivm->builder,
1279 lp_build_emit_fetch(bld_base, inst, 0, c),
1280 i32, ""),
1281 store_ptr);
1282
1283 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1284 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1285
1286 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1287 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1288
1289 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1290 }
1291
1292 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1293}
1294
1295#endif /* HAVE_LLVM >= 0x0304 */
1296
Tom Stellarda75c6162012-01-06 17:38:37 -05001297static const struct lp_build_tgsi_action tex_action = {
1298 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001299 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001300 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001301};
1302
Michel Dänzer3e205132012-11-06 17:39:01 +01001303static const struct lp_build_tgsi_action txb_action = {
1304 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001305 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001306 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001307};
1308
Michel Dänzera6b83c02013-02-21 16:10:55 +01001309#if HAVE_LLVM >= 0x0304
1310static const struct lp_build_tgsi_action txd_action = {
1311 .fetch_args = tex_fetch_args,
1312 .emit = build_tex_intrinsic,
1313 .intr_name = "llvm.SI.sampled."
1314};
1315#endif
1316
Michel Dänzer36231112013-05-02 09:44:45 +02001317static const struct lp_build_tgsi_action txf_action = {
1318 .fetch_args = tex_fetch_args,
1319 .emit = build_tex_intrinsic,
1320 .intr_name = "llvm.SI.imageload."
1321};
1322
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001323static const struct lp_build_tgsi_action txl_action = {
1324 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001325 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001326 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001327};
1328
Michel Dänzer0495adb2013-05-06 12:45:14 +02001329static const struct lp_build_tgsi_action txq_action = {
1330 .fetch_args = txq_fetch_args,
1331 .emit = build_tgsi_intrinsic_nomem,
1332 .intr_name = "llvm.SI.resinfo"
1333};
1334
Christian König206f0592013-03-20 14:37:21 +01001335static void create_meta_data(struct si_shader_context *si_shader_ctx)
1336{
1337 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1338 LLVMValueRef args[3];
1339
1340 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1341 args[1] = 0;
1342 args[2] = lp_build_const_int32(gallivm, 1);
1343
1344 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1345}
1346
Christian König55fe5cc2013-03-04 16:30:06 +01001347static void create_function(struct si_shader_context *si_shader_ctx)
1348{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001349 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1350 struct gallivm_state *gallivm = bld_base->base.gallivm;
Christian König0666ffd2013-03-05 15:07:39 +01001351 LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001352 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01001353
Christian König55fe5cc2013-03-04 16:30:06 +01001354 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01001355 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01001356 f32 = LLVMFloatTypeInContext(gallivm->context);
1357 v2i32 = LLVMVectorType(i32, 2);
1358 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01001359
Christian Königf5298b02013-02-28 14:50:07 +01001360 params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
1361 params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
Christian König55fe5cc2013-03-04 16:30:06 +01001362 params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
1363
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001364 switch (si_shader_ctx->type) {
1365 case TGSI_PROCESSOR_VERTEX:
1366 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01001367 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001368 last_sgpr = SI_PARAM_START_INSTANCE;
Christian Könige4ed5872013-03-21 18:02:52 +01001369 params[SI_PARAM_VERTEX_ID] = i32;
1370 params[SI_PARAM_DUMMY_0] = i32;
1371 params[SI_PARAM_DUMMY_1] = i32;
1372 params[SI_PARAM_INSTANCE_ID] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001373 num_params = SI_PARAM_INSTANCE_ID+1;
1374 break;
Christian König0666ffd2013-03-05 15:07:39 +01001375
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001376 case TGSI_PROCESSOR_FRAGMENT:
Christian König0666ffd2013-03-05 15:07:39 +01001377 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001378 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01001379 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
1380 params[SI_PARAM_PERSP_CENTER] = v2i32;
1381 params[SI_PARAM_PERSP_CENTROID] = v2i32;
1382 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
1383 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
1384 params[SI_PARAM_LINEAR_CENTER] = v2i32;
1385 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
1386 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
1387 params[SI_PARAM_POS_X_FLOAT] = f32;
1388 params[SI_PARAM_POS_Y_FLOAT] = f32;
1389 params[SI_PARAM_POS_Z_FLOAT] = f32;
1390 params[SI_PARAM_POS_W_FLOAT] = f32;
1391 params[SI_PARAM_FRONT_FACE] = f32;
1392 params[SI_PARAM_ANCILLARY] = f32;
1393 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
1394 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001395 num_params = SI_PARAM_POS_FIXED_PT+1;
1396 break;
1397
1398 default:
1399 assert(0 && "unimplemented shader");
1400 return;
Christian Königc4973212013-03-05 12:14:02 +01001401 }
Christian König55fe5cc2013-03-04 16:30:06 +01001402
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001403 assert(num_params <= Elements(params));
1404 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01001405 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01001406
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001407 for (i = 0; i <= last_sgpr; ++i) {
1408 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Christian Königcf9b31f2013-03-21 18:30:23 +01001409 LLVMAddAttribute(P, LLVMInRegAttribute);
1410 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001411
1412#if HAVE_LLVM >= 0x0304
1413 if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
1414 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0)
1415 si_shader_ctx->ddxy_lds =
1416 LLVMAddGlobalInAddressSpace(gallivm->module,
1417 LLVMArrayType(i32, 64),
1418 "ddxy_lds",
1419 LOCAL_ADDR_SPACE);
1420#endif
Christian König55fe5cc2013-03-04 16:30:06 +01001421}
Tom Stellarda75c6162012-01-06 17:38:37 -05001422
Christian König0f6cf2b2013-03-15 15:53:25 +01001423static void preload_constants(struct si_shader_context *si_shader_ctx)
1424{
1425 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1426 struct gallivm_state * gallivm = bld_base->base.gallivm;
1427 const struct tgsi_shader_info * info = bld_base->info;
1428
1429 unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1;
1430
1431 LLVMValueRef ptr;
1432
1433 if (num_const == 0)
1434 return;
1435
1436 /* Allocate space for the constant values */
1437 si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef));
1438
1439 /* Load the resource descriptor */
1440 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1441 si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
1442
1443 /* Load the constants, we rely on the code sinking to do the rest */
1444 for (i = 0; i < num_const * 4; ++i) {
1445 LLVMValueRef args[2] = {
1446 si_shader_ctx->const_resource,
1447 lp_build_const_int32(gallivm, i * 4)
1448 };
1449 si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1450 bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1451 }
1452}
1453
Christian König1c100182013-03-17 16:02:42 +01001454static void preload_samplers(struct si_shader_context *si_shader_ctx)
1455{
1456 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1457 struct gallivm_state * gallivm = bld_base->base.gallivm;
1458 const struct tgsi_shader_info * info = bld_base->info;
1459
1460 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1461
1462 LLVMValueRef res_ptr, samp_ptr;
1463 LLVMValueRef offset;
1464
1465 if (num_samplers == 0)
1466 return;
1467
1468 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02001469 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01001470 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1471
1472 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1473 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1474
1475 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1476 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01001477 /* Resource */
1478 offset = lp_build_const_int32(gallivm, i);
1479 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1480
1481 /* Sampler */
1482 offset = lp_build_const_int32(gallivm, i);
1483 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02001484
1485 /* FMASK resource */
1486 if (info->is_msaa_sampler[i]) {
1487 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
1488 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
1489 build_indexed_load(si_shader_ctx, res_ptr, offset);
1490 }
Christian König1c100182013-03-17 16:02:42 +01001491 }
1492}
1493
Tom Stellard302f53d2012-10-25 13:50:10 -04001494int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader,
1495 LLVMModuleRef mod)
1496{
Tom Stellard302f53d2012-10-25 13:50:10 -04001497 unsigned i;
1498 uint32_t *ptr;
1499 bool dump;
Tom Stellard7782d192013-04-04 09:57:13 -07001500 struct radeon_llvm_binary binary;
Tom Stellard302f53d2012-10-25 13:50:10 -04001501
1502 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
1503
Tom Stellard7782d192013-04-04 09:57:13 -07001504 memset(&binary, 0, sizeof(binary));
1505 radeon_llvm_compile(mod, &binary,
Marek Olšáka81c3e02013-08-14 01:04:39 +02001506 r600_get_llvm_processor_name(rctx->screen->b.family), dump);
Tom Stellard302f53d2012-10-25 13:50:10 -04001507 if (dump) {
1508 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07001509 for (i = 0; i < binary.code_size; i+=4 ) {
1510 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
1511 binary.code[i + 2], binary.code[i + 1],
1512 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04001513 }
1514 }
1515
Tom Stellardd50343d2013-04-04 16:21:06 -04001516 /* XXX: We may be able to emit some of these values directly rather than
1517 * extracting fields to be emitted later.
1518 */
1519 for (i = 0; i < binary.config_size; i+= 8) {
1520 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
1521 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
1522 switch (reg) {
1523 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
1524 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
1525 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
1526 case R_00B848_COMPUTE_PGM_RSRC1:
1527 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
1528 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
1529 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001530 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
1531 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
1532 break;
1533 case R_00B84C_COMPUTE_PGM_RSRC2:
1534 shader->lds_size = G_00B84C_LDS_SIZE(value);
1535 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04001536 case R_0286CC_SPI_PS_INPUT_ENA:
1537 shader->spi_ps_input_ena = value;
1538 break;
1539 default:
1540 fprintf(stderr, "Warning: Compiler emitted unknown "
1541 "config register: 0x%x\n", reg);
1542 break;
1543 }
1544 }
Tom Stellard302f53d2012-10-25 13:50:10 -04001545
1546 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02001547 r600_resource_reference(&shader->bo, NULL);
1548 shader->bo = r600_resource_create_custom(rctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04001549 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001550 if (shader->bo == NULL) {
1551 return -ENOMEM;
1552 }
1553
Marek Olšáka81c3e02013-08-14 01:04:39 +02001554 ptr = (uint32_t*)rctx->b.ws->buffer_map(shader->bo->cs_buf, rctx->b.rings.gfx.cs, PIPE_TRANSFER_WRITE);
Tom Stellard302f53d2012-10-25 13:50:10 -04001555 if (0 /*R600_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04001556 for (i = 0; i < binary.code_size / 4; ++i) {
1557 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04001558 }
1559 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04001560 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001561 }
Marek Olšáka81c3e02013-08-14 01:04:39 +02001562 rctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04001563
Tom Stellard7782d192013-04-04 09:57:13 -07001564 free(binary.code);
1565 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04001566
1567 return 0;
1568}
1569
Tom Stellarda75c6162012-01-06 17:38:37 -05001570int si_pipe_shader_create(
1571 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01001572 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05001573{
1574 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001575 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05001576 struct si_shader_context si_shader_ctx;
1577 struct tgsi_shader_info shader_info;
1578 struct lp_build_tgsi_context * bld_base;
1579 LLVMModuleRef mod;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001580 bool dump;
Tom Stellard302f53d2012-10-25 13:50:10 -04001581 int r = 0;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001582
1583 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -05001584
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001585 assert(shader->shader.noutput == 0);
1586 assert(shader->shader.ninterp == 0);
1587 assert(shader->shader.ninput == 0);
1588
Michel Dänzercfebaf92012-08-31 19:04:08 +02001589 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001590 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1591 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1592
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001593 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001594
Michel Dänzere44dfd42012-11-07 17:33:08 +01001595 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01001596 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001597 bld_base->info = &shader_info;
1598 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001599 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1600
1601 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001602 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01001603#if HAVE_LLVM >= 0x0304
1604 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
1605#endif
Michel Dänzer36231112013-05-02 09:44:45 +02001606 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001607 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001608 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02001609 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001610
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001611#if HAVE_LLVM >= 0x0304
1612 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
1613 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
1614#endif
1615
Tom Stellarda75c6162012-01-06 17:38:37 -05001616 si_shader_ctx.radeon_bld.load_input = declare_input;
Christian Könige4ed5872013-03-21 18:02:52 +01001617 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001618 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001619 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1620 si_shader_ctx.shader = shader;
1621 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05001622
Christian König206f0592013-03-20 14:37:21 +01001623 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01001624 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01001625 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01001626 preload_samplers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001627
Christian König835098a2012-07-17 21:28:10 +02001628 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001629
Tom Stellard185fc9a2012-07-12 10:40:47 -04001630 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1631 * conversion fails. */
1632 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001633 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001634 }
1635
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001636 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001637 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Christian König0f6cf2b2013-03-15 15:53:25 +01001638 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001639 FREE(si_shader_ctx.resources);
1640 FREE(si_shader_ctx.samplers);
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001641 return -EINVAL;
1642 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001643
1644 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1645
1646 mod = bld_base->base.gallivm->module;
Tom Stellard302f53d2012-10-25 13:50:10 -04001647 r = si_compile_llvm(rctx, shader, mod);
Tom Stellarda75c6162012-01-06 17:38:37 -05001648
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001649 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001650 tgsi_parse_free(&si_shader_ctx.parse);
1651
Christian König0f6cf2b2013-03-15 15:53:25 +01001652 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001653 FREE(si_shader_ctx.resources);
1654 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05001655
Tom Stellard302f53d2012-10-25 13:50:10 -04001656 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05001657}
1658
1659void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1660{
Marek Olšáka81c3e02013-08-14 01:04:39 +02001661 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001662}