blob: a1dec1757cb8bf2557f6cef182965e3f9517debf [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, "");
829 mask |= 0x2;
830
831 if (depth_index < 0)
832 args[5] = args[6];
833 }
834
835 /* Specify which components to enable */
836 args[0] = lp_build_const_int32(base->gallivm, mask);
837
838 args[1] =
839 args[2] =
840 args[4] = uint->zero;
841
842 if (last_args[0])
843 lp_build_intrinsic(base->gallivm->builder,
844 "llvm.SI.export",
845 LLVMVoidTypeInContext(base->gallivm->context),
846 args, 9);
847 else
848 memcpy(last_args, args, sizeof(args));
849 }
850
Michel Dänzerb00269a2013-08-07 18:14:16 +0200851 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
852 unsigned pos_idx = 0;
Christian Königf18fd252012-07-25 21:58:46 +0200853
Michel Dänzerb00269a2013-08-07 18:14:16 +0200854 for (i = 0; i < 4; i++)
855 if (pos_args[i][0])
856 shader->nr_pos_exports++;
Christian Königf18fd252012-07-25 21:58:46 +0200857
Michel Dänzerb00269a2013-08-07 18:14:16 +0200858 for (i = 0; i < 4; i++) {
859 if (!pos_args[i][0])
860 continue;
Christian Königf18fd252012-07-25 21:58:46 +0200861
Michel Dänzerc8402702013-02-12 18:37:22 +0100862 /* Specify the target we are exporting */
Michel Dänzerb00269a2013-08-07 18:14:16 +0200863 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
864
865 if (pos_idx == shader->nr_pos_exports)
866 /* Specify that this is the last export */
867 pos_args[i][2] = uint->one;
Michel Dänzerc8402702013-02-12 18:37:22 +0100868
869 lp_build_intrinsic(base->gallivm->builder,
870 "llvm.SI.export",
871 LLVMVoidTypeInContext(base->gallivm->context),
Michel Dänzerb00269a2013-08-07 18:14:16 +0200872 pos_args[i], 9);
873 }
874 } else {
875 if (!last_args[0]) {
876 /* Specify which components to enable */
877 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
878
879 /* Specify the target we are exporting */
880 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
881
882 /* Set COMPR flag to zero to export data as 32-bit */
883 last_args[4] = uint->zero;
884
885 /* dummy bits */
886 last_args[5]= uint->zero;
887 last_args[6]= uint->zero;
888 last_args[7]= uint->zero;
889 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +0100890
891 si_shader_ctx->shader->spi_shader_col_format |=
Michel Dänzerb00269a2013-08-07 18:14:16 +0200892 V_028714_SPI_SHADER_32_ABGR;
893 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +0100894 }
895
Michel Dänzerb00269a2013-08-07 18:14:16 +0200896 /* Specify whether the EXEC mask represents the valid mask */
897 last_args[1] = uint->one;
898
899 if (shader->fs_write_all && shader->nr_cbufs > 1) {
900 int i;
901
902 /* Specify that this is not yet the last export */
903 last_args[2] = lp_build_const_int32(base->gallivm, 0);
904
905 for (i = 1; i < shader->nr_cbufs; i++) {
906 /* Specify the target we are exporting */
907 last_args[3] = lp_build_const_int32(base->gallivm,
908 V_008DFC_SQ_EXP_MRT + i);
909
910 lp_build_intrinsic(base->gallivm->builder,
911 "llvm.SI.export",
912 LLVMVoidTypeInContext(base->gallivm->context),
913 last_args, 9);
914
915 si_shader_ctx->shader->spi_shader_col_format |=
916 si_shader_ctx->shader->spi_shader_col_format << 4;
917 si_shader_ctx->shader->cb_shader_mask |=
918 si_shader_ctx->shader->cb_shader_mask << 4;
919 }
920
921 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
922 }
923
924 /* Specify that this is the last export */
925 last_args[2] = lp_build_const_int32(base->gallivm, 1);
926
927 lp_build_intrinsic(base->gallivm->builder,
928 "llvm.SI.export",
929 LLVMVoidTypeInContext(base->gallivm->context),
930 last_args, 9);
Michel Dänzerc8402702013-02-12 18:37:22 +0100931 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500932/* XXX: Look up what this function does */
933/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
934}
935
Marek Olšák4855acd2013-08-06 15:08:54 +0200936static const struct lp_build_tgsi_action txf_action;
937
938static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
939 struct lp_build_tgsi_context * bld_base,
940 struct lp_build_emit_data * emit_data);
941
Tom Stellarda75c6162012-01-06 17:38:37 -0500942static void tex_fetch_args(
943 struct lp_build_tgsi_context * bld_base,
944 struct lp_build_emit_data * emit_data)
945{
Christian König55fe5cc2013-03-04 16:30:06 +0100946 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100947 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200948 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100949 unsigned opcode = inst->Instruction.Opcode;
950 unsigned target = inst->Texture.Texture;
Marek Olšák4855acd2013-08-06 15:08:54 +0200951 unsigned sampler_src, sampler_index;
Michel Dänzer120efee2013-01-25 12:10:11 +0100952 LLVMValueRef coords[4];
953 LLVMValueRef address[16];
Marek Olšák4855acd2013-08-06 15:08:54 +0200954 LLVMValueRef sample_index_rewrite = NULL;
955 LLVMValueRef sample_chan = NULL;
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100956 int ref_pos;
957 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +0100958 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100959 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400960
Michel Dänzer120efee2013-01-25 12:10:11 +0100961 /* Fetch and project texture coordinates */
962 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100963 for (chan = 0; chan < 3; chan++ ) {
964 coords[chan] = lp_build_emit_fetch(bld_base,
965 emit_data->inst, 0,
966 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100967 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200968 coords[chan] = lp_build_emit_llvm_binary(bld_base,
969 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100970 coords[chan],
971 coords[3]);
972 }
973
Michel Dänzer120efee2013-01-25 12:10:11 +0100974 if (opcode == TGSI_OPCODE_TXP)
975 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500976
Michel Dänzer120efee2013-01-25 12:10:11 +0100977 /* Pack LOD bias value */
978 if (opcode == TGSI_OPCODE_TXB)
979 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400980
Michel Dänzer0495adb2013-05-06 12:45:14 +0200981 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100982 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100983
984 /* Pack depth comparison value */
985 switch (target) {
986 case TGSI_TEXTURE_SHADOW1D:
987 case TGSI_TEXTURE_SHADOW1D_ARRAY:
988 case TGSI_TEXTURE_SHADOW2D:
989 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +0100990 case TGSI_TEXTURE_SHADOWCUBE:
991 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100992 assert(ref_pos >= 0);
993 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +0100994 break;
995 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
996 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +0100997 }
998
Michel Dänzera6b83c02013-02-21 16:10:55 +0100999 /* Pack user derivatives */
1000 if (opcode == TGSI_OPCODE_TXD) {
1001 for (chan = 0; chan < 2; chan++) {
1002 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1003 if (num_coords > 1)
1004 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1005 }
1006 }
1007
Michel Dänzer120efee2013-01-25 12:10:11 +01001008 /* Pack texture coordinates */
1009 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001010 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001011 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001012 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001013 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001014
Michel Dänzer120efee2013-01-25 12:10:11 +01001015 /* Pack LOD */
Michel Dänzer36231112013-05-02 09:44:45 +02001016 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001017 address[count++] = coords[3];
1018
1019 if (count > 16) {
1020 assert(!"Cannot handle more than 16 texture address parameters");
1021 count = 16;
1022 }
1023
1024 for (chan = 0; chan < count; chan++ ) {
1025 address[chan] = LLVMBuildBitCast(gallivm->builder,
1026 address[chan],
1027 LLVMInt32TypeInContext(gallivm->context),
1028 "");
1029 }
1030
Michel Dänzera6b83c02013-02-21 16:10:55 +01001031 sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
Marek Olšák4855acd2013-08-06 15:08:54 +02001032 sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1033
1034 /* Adjust the sample index according to FMASK.
1035 *
1036 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1037 * which is the identity mapping. Each nibble says which physical sample
1038 * should be fetched to get that sample.
1039 *
1040 * For example, 0x11111100 means there are only 2 samples stored and
1041 * the second sample covers 3/4 of the pixel. When reading samples 0
1042 * and 1, return physical sample 0 (determined by the first two 0s
1043 * in FMASK), otherwise return physical sample 1.
1044 *
1045 * The sample index should be adjusted as follows:
1046 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1047 */
1048 if (target == TGSI_TEXTURE_2D_MSAA ||
1049 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1050 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1051 struct lp_build_emit_data txf_emit_data = *emit_data;
1052 LLVMValueRef txf_address[16];
1053 unsigned txf_count = count;
1054
1055 memcpy(txf_address, address, sizeof(address));
1056
1057 /* Pad to a power-of-two size. */
1058 while (txf_count < util_next_power_of_two(txf_count))
1059 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1060
1061 /* Read FMASK using TXF. */
1062 txf_emit_data.chan = 0;
1063 txf_emit_data.dst_type = LLVMVectorType(
1064 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1065 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1066 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
1067 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm, target);
1068 txf_emit_data.arg_count = 3;
1069
1070 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1071
1072 /* Initialize some constants. */
1073 if (target == TGSI_TEXTURE_2D_MSAA) {
1074 sample_chan = LLVMConstInt(uint_bld->elem_type, 2, 0);
1075 } else {
1076 sample_chan = LLVMConstInt(uint_bld->elem_type, 3, 0);
1077 }
1078
1079 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1080 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1081
1082 /* Apply the formula. */
1083 LLVMValueRef fmask =
1084 LLVMBuildExtractElement(gallivm->builder,
1085 txf_emit_data.output[0],
1086 uint_bld->zero, "");
1087
1088 LLVMValueRef sample_index =
1089 LLVMBuildExtractElement(gallivm->builder,
1090 txf_emit_data.args[0],
1091 sample_chan, "");
1092
1093 LLVMValueRef sample_index4 =
1094 LLVMBuildMul(gallivm->builder, sample_index, four, "");
1095
1096 LLVMValueRef shifted_fmask =
1097 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1098
1099 LLVMValueRef final_sample =
1100 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1101
1102 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1103 * resource descriptor is 0 (invalid),
1104 */
1105 LLVMValueRef fmask_desc =
1106 LLVMBuildBitCast(gallivm->builder,
1107 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1108 LLVMVectorType(uint_bld->elem_type, 8), "");
1109
1110 LLVMValueRef fmask_word1 =
1111 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1112 uint_bld->one, "");
1113
1114 LLVMValueRef word1_is_nonzero =
1115 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1116 fmask_word1, uint_bld->zero, "");
1117
1118 sample_index_rewrite =
1119 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
1120 final_sample, sample_index, "");
1121 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001122
Michel Dänzer36231112013-05-02 09:44:45 +02001123 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001124 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001125
1126 if (opcode == TGSI_OPCODE_TXF) {
1127 /* add tex offsets */
1128 if (inst->Texture.NumOffsets) {
1129 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1130 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1131 const struct tgsi_texture_offset * off = inst->TexOffsets;
1132
1133 assert(inst->Texture.NumOffsets == 1);
1134
1135 address[0] =
1136 lp_build_add(uint_bld, address[0],
1137 bld->immediates[off->Index][off->SwizzleX]);
1138 if (num_coords > 1)
1139 address[1] =
1140 lp_build_add(uint_bld, address[1],
1141 bld->immediates[off->Index][off->SwizzleY]);
1142 if (num_coords > 2)
1143 address[2] =
1144 lp_build_add(uint_bld, address[2],
1145 bld->immediates[off->Index][off->SwizzleZ]);
1146 }
1147
1148 emit_data->dst_type = LLVMVectorType(
1149 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1150 4);
1151
1152 emit_data->arg_count = 3;
1153 } else {
1154 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001155 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001156
1157 emit_data->dst_type = LLVMVectorType(
1158 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1159 4);
1160
1161 emit_data->arg_count = 4;
1162 }
1163
1164 /* Dimensions */
1165 emit_data->args[emit_data->arg_count - 1] =
1166 lp_build_const_int32(bld_base->base.gallivm, target);
1167
Michel Dänzer120efee2013-01-25 12:10:11 +01001168 /* Pad to power of two vector */
1169 while (count < util_next_power_of_two(count))
1170 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1171
Christian Königccf3e8f2013-03-26 15:09:27 +01001172 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Marek Olšák4855acd2013-08-06 15:08:54 +02001173
1174 /* Replace the MSAA sample index if needed. */
1175 if (sample_index_rewrite) {
1176 emit_data->args[0] =
1177 LLVMBuildInsertElement(gallivm->builder, emit_data->args[0],
1178 sample_index_rewrite, sample_chan, "");
1179 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001180}
1181
Michel Dänzer07eddc42013-02-06 15:43:10 +01001182static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1183 struct lp_build_tgsi_context * bld_base,
1184 struct lp_build_emit_data * emit_data)
1185{
1186 struct lp_build_context * base = &bld_base->base;
1187 char intr_name[23];
1188
1189 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001190 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001191
Christian König44e32242013-03-20 12:10:35 +01001192 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001193 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001194 emit_data->args, emit_data->arg_count,
1195 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001196}
1197
Michel Dänzer0495adb2013-05-06 12:45:14 +02001198static void txq_fetch_args(
1199 struct lp_build_tgsi_context * bld_base,
1200 struct lp_build_emit_data * emit_data)
1201{
1202 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1203 const struct tgsi_full_instruction *inst = emit_data->inst;
1204
1205 /* Mip level */
1206 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1207
1208 /* Resource */
1209 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1210
1211 /* Dimensions */
1212 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1213 inst->Texture.Texture);
1214
1215 emit_data->arg_count = 3;
1216
1217 emit_data->dst_type = LLVMVectorType(
1218 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1219 4);
1220}
1221
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001222#if HAVE_LLVM >= 0x0304
1223
1224static void si_llvm_emit_ddxy(
1225 const struct lp_build_tgsi_action * action,
1226 struct lp_build_tgsi_context * bld_base,
1227 struct lp_build_emit_data * emit_data)
1228{
1229 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1230 struct gallivm_state *gallivm = bld_base->base.gallivm;
1231 struct lp_build_context * base = &bld_base->base;
1232 const struct tgsi_full_instruction *inst = emit_data->inst;
1233 unsigned opcode = inst->Instruction.Opcode;
1234 LLVMValueRef indices[2];
1235 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1236 LLVMValueRef tl, trbl, result[4];
1237 LLVMTypeRef i32;
1238 unsigned swizzle[4];
1239 unsigned c;
1240
1241 i32 = LLVMInt32TypeInContext(gallivm->context);
1242
1243 indices[0] = bld_base->uint_bld.zero;
1244 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1245 NULL, 0, LLVMReadNoneAttribute);
1246 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1247 indices, 2, "");
1248
1249 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1250 lp_build_const_int32(gallivm, 0xfffffffc), "");
1251 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1252 indices, 2, "");
1253
1254 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1255 lp_build_const_int32(gallivm,
1256 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1257 "");
1258 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1259 indices, 2, "");
1260
1261 for (c = 0; c < 4; ++c) {
1262 unsigned i;
1263
1264 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1265 for (i = 0; i < c; ++i) {
1266 if (swizzle[i] == swizzle[c]) {
1267 result[c] = result[i];
1268 break;
1269 }
1270 }
1271 if (i != c)
1272 continue;
1273
1274 LLVMBuildStore(gallivm->builder,
1275 LLVMBuildBitCast(gallivm->builder,
1276 lp_build_emit_fetch(bld_base, inst, 0, c),
1277 i32, ""),
1278 store_ptr);
1279
1280 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1281 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1282
1283 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1284 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1285
1286 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1287 }
1288
1289 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1290}
1291
1292#endif /* HAVE_LLVM >= 0x0304 */
1293
Tom Stellarda75c6162012-01-06 17:38:37 -05001294static const struct lp_build_tgsi_action tex_action = {
1295 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001296 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001297 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001298};
1299
Michel Dänzer3e205132012-11-06 17:39:01 +01001300static const struct lp_build_tgsi_action txb_action = {
1301 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001302 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001303 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001304};
1305
Michel Dänzera6b83c02013-02-21 16:10:55 +01001306#if HAVE_LLVM >= 0x0304
1307static const struct lp_build_tgsi_action txd_action = {
1308 .fetch_args = tex_fetch_args,
1309 .emit = build_tex_intrinsic,
1310 .intr_name = "llvm.SI.sampled."
1311};
1312#endif
1313
Michel Dänzer36231112013-05-02 09:44:45 +02001314static const struct lp_build_tgsi_action txf_action = {
1315 .fetch_args = tex_fetch_args,
1316 .emit = build_tex_intrinsic,
1317 .intr_name = "llvm.SI.imageload."
1318};
1319
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001320static const struct lp_build_tgsi_action txl_action = {
1321 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001322 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001323 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001324};
1325
Michel Dänzer0495adb2013-05-06 12:45:14 +02001326static const struct lp_build_tgsi_action txq_action = {
1327 .fetch_args = txq_fetch_args,
1328 .emit = build_tgsi_intrinsic_nomem,
1329 .intr_name = "llvm.SI.resinfo"
1330};
1331
Christian König206f0592013-03-20 14:37:21 +01001332static void create_meta_data(struct si_shader_context *si_shader_ctx)
1333{
1334 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1335 LLVMValueRef args[3];
1336
1337 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1338 args[1] = 0;
1339 args[2] = lp_build_const_int32(gallivm, 1);
1340
1341 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1342}
1343
Christian König55fe5cc2013-03-04 16:30:06 +01001344static void create_function(struct si_shader_context *si_shader_ctx)
1345{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001346 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1347 struct gallivm_state *gallivm = bld_base->base.gallivm;
Christian König0666ffd2013-03-05 15:07:39 +01001348 LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
Christian König55fe5cc2013-03-04 16:30:06 +01001349 unsigned i;
1350
Christian König55fe5cc2013-03-04 16:30:06 +01001351 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01001352 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01001353 f32 = LLVMFloatTypeInContext(gallivm->context);
1354 v2i32 = LLVMVectorType(i32, 2);
1355 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01001356
Christian Königf5298b02013-02-28 14:50:07 +01001357 params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
1358 params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
Christian König55fe5cc2013-03-04 16:30:06 +01001359 params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
1360
Christian Königc4973212013-03-05 12:14:02 +01001361 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Christian König55fe5cc2013-03-04 16:30:06 +01001362 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER];
Christian Königcf9b31f2013-03-21 18:30:23 +01001363 params[SI_PARAM_START_INSTANCE] = i32;
Christian Könige4ed5872013-03-21 18:02:52 +01001364 params[SI_PARAM_VERTEX_ID] = i32;
1365 params[SI_PARAM_DUMMY_0] = i32;
1366 params[SI_PARAM_DUMMY_1] = i32;
1367 params[SI_PARAM_INSTANCE_ID] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01001368 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 9);
Christian König0666ffd2013-03-05 15:07:39 +01001369
Christian Königc4973212013-03-05 12:14:02 +01001370 } else {
Christian König0666ffd2013-03-05 15:07:39 +01001371 params[SI_PARAM_PRIM_MASK] = i32;
1372 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
1373 params[SI_PARAM_PERSP_CENTER] = v2i32;
1374 params[SI_PARAM_PERSP_CENTROID] = v2i32;
1375 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
1376 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
1377 params[SI_PARAM_LINEAR_CENTER] = v2i32;
1378 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
1379 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
1380 params[SI_PARAM_POS_X_FLOAT] = f32;
1381 params[SI_PARAM_POS_Y_FLOAT] = f32;
1382 params[SI_PARAM_POS_Z_FLOAT] = f32;
1383 params[SI_PARAM_POS_W_FLOAT] = f32;
1384 params[SI_PARAM_FRONT_FACE] = f32;
1385 params[SI_PARAM_ANCILLARY] = f32;
1386 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
1387 params[SI_PARAM_POS_FIXED_PT] = f32;
1388 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20);
Christian Königc4973212013-03-05 12:14:02 +01001389 }
Christian König55fe5cc2013-03-04 16:30:06 +01001390
1391 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
1392 for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) {
1393 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
1394 LLVMAddAttribute(P, LLVMInRegAttribute);
1395 }
Christian Königcf9b31f2013-03-21 18:30:23 +01001396
1397 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
1398 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1399 SI_PARAM_START_INSTANCE);
1400 LLVMAddAttribute(P, LLVMInRegAttribute);
1401 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001402
1403#if HAVE_LLVM >= 0x0304
1404 if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
1405 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0)
1406 si_shader_ctx->ddxy_lds =
1407 LLVMAddGlobalInAddressSpace(gallivm->module,
1408 LLVMArrayType(i32, 64),
1409 "ddxy_lds",
1410 LOCAL_ADDR_SPACE);
1411#endif
Christian König55fe5cc2013-03-04 16:30:06 +01001412}
Tom Stellarda75c6162012-01-06 17:38:37 -05001413
Christian König0f6cf2b2013-03-15 15:53:25 +01001414static void preload_constants(struct si_shader_context *si_shader_ctx)
1415{
1416 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1417 struct gallivm_state * gallivm = bld_base->base.gallivm;
1418 const struct tgsi_shader_info * info = bld_base->info;
1419
1420 unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1;
1421
1422 LLVMValueRef ptr;
1423
1424 if (num_const == 0)
1425 return;
1426
1427 /* Allocate space for the constant values */
1428 si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef));
1429
1430 /* Load the resource descriptor */
1431 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1432 si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
1433
1434 /* Load the constants, we rely on the code sinking to do the rest */
1435 for (i = 0; i < num_const * 4; ++i) {
1436 LLVMValueRef args[2] = {
1437 si_shader_ctx->const_resource,
1438 lp_build_const_int32(gallivm, i * 4)
1439 };
1440 si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1441 bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1442 }
1443}
1444
Christian König1c100182013-03-17 16:02:42 +01001445static void preload_samplers(struct si_shader_context *si_shader_ctx)
1446{
1447 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1448 struct gallivm_state * gallivm = bld_base->base.gallivm;
1449 const struct tgsi_shader_info * info = bld_base->info;
1450
1451 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1452
1453 LLVMValueRef res_ptr, samp_ptr;
1454 LLVMValueRef offset;
1455
1456 if (num_samplers == 0)
1457 return;
1458
1459 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02001460 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01001461 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1462
1463 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1464 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1465
1466 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1467 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01001468 /* Resource */
1469 offset = lp_build_const_int32(gallivm, i);
1470 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1471
1472 /* Sampler */
1473 offset = lp_build_const_int32(gallivm, i);
1474 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02001475
1476 /* FMASK resource */
1477 if (info->is_msaa_sampler[i]) {
1478 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
1479 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
1480 build_indexed_load(si_shader_ctx, res_ptr, offset);
1481 }
Christian König1c100182013-03-17 16:02:42 +01001482 }
1483}
1484
Tom Stellard302f53d2012-10-25 13:50:10 -04001485int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader,
1486 LLVMModuleRef mod)
1487{
Tom Stellard302f53d2012-10-25 13:50:10 -04001488 unsigned i;
1489 uint32_t *ptr;
1490 bool dump;
Tom Stellard7782d192013-04-04 09:57:13 -07001491 struct radeon_llvm_binary binary;
Tom Stellard302f53d2012-10-25 13:50:10 -04001492
1493 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
1494
Tom Stellard7782d192013-04-04 09:57:13 -07001495 memset(&binary, 0, sizeof(binary));
1496 radeon_llvm_compile(mod, &binary,
1497 r600_get_llvm_processor_name(rctx->screen->family), dump);
Tom Stellard302f53d2012-10-25 13:50:10 -04001498 if (dump) {
1499 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07001500 for (i = 0; i < binary.code_size; i+=4 ) {
1501 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
1502 binary.code[i + 2], binary.code[i + 1],
1503 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04001504 }
1505 }
1506
Tom Stellardd50343d2013-04-04 16:21:06 -04001507 /* XXX: We may be able to emit some of these values directly rather than
1508 * extracting fields to be emitted later.
1509 */
1510 for (i = 0; i < binary.config_size; i+= 8) {
1511 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
1512 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
1513 switch (reg) {
1514 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
1515 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
1516 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
1517 case R_00B848_COMPUTE_PGM_RSRC1:
1518 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
1519 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
1520 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001521 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
1522 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
1523 break;
1524 case R_00B84C_COMPUTE_PGM_RSRC2:
1525 shader->lds_size = G_00B84C_LDS_SIZE(value);
1526 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04001527 case R_0286CC_SPI_PS_INPUT_ENA:
1528 shader->spi_ps_input_ena = value;
1529 break;
1530 default:
1531 fprintf(stderr, "Warning: Compiler emitted unknown "
1532 "config register: 0x%x\n", reg);
1533 break;
1534 }
1535 }
Tom Stellard302f53d2012-10-25 13:50:10 -04001536
1537 /* copy new shader */
1538 si_resource_reference(&shader->bo, NULL);
1539 shader->bo = si_resource_create_custom(rctx->context.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04001540 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001541 if (shader->bo == NULL) {
1542 return -ENOMEM;
1543 }
1544
1545 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
1546 if (0 /*R600_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04001547 for (i = 0; i < binary.code_size / 4; ++i) {
1548 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04001549 }
1550 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04001551 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001552 }
1553 rctx->ws->buffer_unmap(shader->bo->cs_buf);
1554
Tom Stellard7782d192013-04-04 09:57:13 -07001555 free(binary.code);
1556 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04001557
1558 return 0;
1559}
1560
Tom Stellarda75c6162012-01-06 17:38:37 -05001561int si_pipe_shader_create(
1562 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01001563 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05001564{
1565 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001566 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05001567 struct si_shader_context si_shader_ctx;
1568 struct tgsi_shader_info shader_info;
1569 struct lp_build_tgsi_context * bld_base;
1570 LLVMModuleRef mod;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001571 bool dump;
Tom Stellard302f53d2012-10-25 13:50:10 -04001572 int r = 0;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001573
1574 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -05001575
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001576 assert(shader->shader.noutput == 0);
1577 assert(shader->shader.ninterp == 0);
1578 assert(shader->shader.ninput == 0);
1579
Michel Dänzercfebaf92012-08-31 19:04:08 +02001580 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001581 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1582 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1583
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001584 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001585
Michel Dänzere44dfd42012-11-07 17:33:08 +01001586 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01001587 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001588 bld_base->info = &shader_info;
1589 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001590 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1591
1592 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001593 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01001594#if HAVE_LLVM >= 0x0304
1595 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
1596#endif
Michel Dänzer36231112013-05-02 09:44:45 +02001597 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001598 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001599 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02001600 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001601
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001602#if HAVE_LLVM >= 0x0304
1603 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
1604 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
1605#endif
1606
Tom Stellarda75c6162012-01-06 17:38:37 -05001607 si_shader_ctx.radeon_bld.load_input = declare_input;
Christian Könige4ed5872013-03-21 18:02:52 +01001608 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001609 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001610 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1611 si_shader_ctx.shader = shader;
1612 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05001613
Christian König206f0592013-03-20 14:37:21 +01001614 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01001615 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01001616 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01001617 preload_samplers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001618
Christian König835098a2012-07-17 21:28:10 +02001619 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001620
Tom Stellard185fc9a2012-07-12 10:40:47 -04001621 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1622 * conversion fails. */
1623 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001624 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001625 }
1626
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001627 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001628 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Christian König0f6cf2b2013-03-15 15:53:25 +01001629 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001630 FREE(si_shader_ctx.resources);
1631 FREE(si_shader_ctx.samplers);
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001632 return -EINVAL;
1633 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001634
1635 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1636
1637 mod = bld_base->base.gallivm->module;
Tom Stellard302f53d2012-10-25 13:50:10 -04001638 r = si_compile_llvm(rctx, shader, mod);
Tom Stellarda75c6162012-01-06 17:38:37 -05001639
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001640 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001641 tgsi_parse_free(&si_shader_ctx.parse);
1642
Christian König0f6cf2b2013-03-15 15:53:25 +01001643 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001644 FREE(si_shader_ctx.resources);
1645 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05001646
Tom Stellard302f53d2012-10-25 13:50:10 -04001647 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05001648}
1649
1650void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1651{
Christian Königfe412872012-07-24 18:47:19 +02001652 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001653}