blob: 3e62858c08d47311761dae6e3a38f9c34b38541d [file] [log] [blame]
Tom Stellarda75c6162012-01-06 17:38:37 -05001
Christian Königce40e472012-08-02 12:14:59 +02002/*
3 * Copyright 2012 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Tom Stellard <thomas.stellard@amd.com>
26 * Michel Dänzer <michel.daenzer@amd.com>
27 * Christian König <christian.koenig@amd.com>
28 */
29
Tom Stellarda75c6162012-01-06 17:38:37 -050030#include "gallivm/lp_bld_tgsi_action.h"
31#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020032#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050033#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010034#include "gallivm/lp_bld_logic.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050035#include "gallivm/lp_bld_tgsi.h"
Christian König5e616cf2013-03-07 11:58:56 +010036#include "gallivm/lp_bld_arit.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050037#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040038#include "radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010039#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_info.h"
41#include "tgsi/tgsi_parse.h"
42#include "tgsi/tgsi_scan.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010043#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050044#include "tgsi/tgsi_dump.h"
45
46#include "radeonsi_pipe.h"
47#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020048#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050049#include "sid.h"
50
51#include <assert.h>
52#include <errno.h>
53#include <stdio.h>
54
Tom Stellarda75c6162012-01-06 17:38:37 -050055struct si_shader_context
56{
57 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050058 struct tgsi_parse_context parse;
59 struct tgsi_token * tokens;
60 struct si_pipe_shader *shader;
61 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Christian König206f0592013-03-20 14:37:21 +010062 LLVMValueRef const_md;
Christian König0f6cf2b2013-03-15 15:53:25 +010063 LLVMValueRef const_resource;
Michel Dänzera06ee5a2013-06-19 18:14:01 +020064#if HAVE_LLVM >= 0x0304
65 LLVMValueRef ddxy_lds;
66#endif
Christian König0f6cf2b2013-03-15 15:53:25 +010067 LLVMValueRef *constants;
Christian König1c100182013-03-17 16:02:42 +010068 LLVMValueRef *resources;
69 LLVMValueRef *samplers;
Tom Stellarda75c6162012-01-06 17:38:37 -050070};
71
72static struct si_shader_context * si_shader_context(
73 struct lp_build_tgsi_context * bld_base)
74{
75 return (struct si_shader_context *)bld_base;
76}
77
78
79#define PERSPECTIVE_BASE 0
80#define LINEAR_BASE 9
81
82#define SAMPLE_OFFSET 0
83#define CENTER_OFFSET 2
84#define CENTROID_OFSET 4
85
86#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040087#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020088#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040089#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050090
Tom Stellard467f5162012-05-16 15:15:35 -040091/**
92 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
93 *
94 * @param offset The offset parameter specifies the number of
95 * elements to offset, not the number of bytes or dwords. An element is the
96 * the type pointed to by the base_ptr parameter (e.g. int is the element of
97 * an int* pointer)
98 *
99 * When LLVM lowers the load instruction, it will convert the element offset
100 * into a dword offset automatically.
101 *
102 */
103static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100104 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400105 LLVMValueRef base_ptr,
106 LLVMValueRef offset)
107{
Christian König206f0592013-03-20 14:37:21 +0100108 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400109
Christian König206f0592013-03-20 14:37:21 +0100110 LLVMValueRef computed_ptr = LLVMBuildGEP(
111 base->gallivm->builder, base_ptr, &offset, 1, "");
112
113 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
114 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
115 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400116}
117
Christian Königa0dca442013-03-22 15:59:22 +0100118static LLVMValueRef get_instance_index(
119 struct radeon_llvm_context * radeon_bld,
120 unsigned divisor)
121{
122 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
123
124 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_INSTANCE_ID);
125 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
126 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
127
128 if (divisor > 1)
129 result = LLVMBuildUDiv(gallivm->builder, result,
130 lp_build_const_int32(gallivm, divisor), "");
131
132 return result;
133}
134
Tom Stellarda75c6162012-01-06 17:38:37 -0500135static void declare_input_vs(
136 struct si_shader_context * si_shader_ctx,
137 unsigned input_index,
138 const struct tgsi_full_declaration *decl)
139{
Christian Königa0dca442013-03-22 15:59:22 +0100140 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
141 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
142
143 unsigned chan;
144
Tom Stellarda75c6162012-01-06 17:38:37 -0500145 LLVMValueRef t_list_ptr;
146 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400147 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500148 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100149 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400150 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500151 LLVMTypeRef vec4_type;
152 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500153
Tom Stellard467f5162012-05-16 15:15:35 -0400154 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100155 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500156
Christian Königb15e3ae2012-07-25 11:22:59 +0200157 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400158
Christian König206f0592013-03-20 14:37:21 +0100159 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400160
161 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200162 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500163
Christian Königa0dca442013-03-22 15:59:22 +0100164 if (divisor) {
165 /* Build index from instance ID, start instance and divisor */
166 si_shader_ctx->shader->shader.uses_instanceid = true;
167 buffer_index = get_instance_index(&si_shader_ctx->radeon_bld, divisor);
168 } else {
169 /* Load the buffer index, which is always stored in VGPR0
170 * for Vertex Shaders */
171 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_ID);
172 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500173
174 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400175 args[0] = t_list;
176 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100177 args[2] = buffer_index;
Christian König44e32242013-03-20 12:10:35 +0100178 input = build_intrinsic(base->gallivm->builder,
179 "llvm.SI.vs.load.input", vec4_type, args, 3,
180 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500181
182 /* Break up the vec4 into individual components */
183 for (chan = 0; chan < 4; chan++) {
184 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
185 /* XXX: Use a helper function for this. There is one in
186 * tgsi_llvm.c. */
187 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
188 LLVMBuildExtractElement(base->gallivm->builder,
189 input, llvm_chan, "");
190 }
191}
192
193static void declare_input_fs(
194 struct si_shader_context * si_shader_ctx,
195 unsigned input_index,
196 const struct tgsi_full_declaration *decl)
197{
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200198 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500199 struct lp_build_context * base =
200 &si_shader_ctx->radeon_bld.soa.bld_base.base;
201 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400202 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100203 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
204
205 LLVMValueRef interp_param;
206 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500207
208 /* This value is:
209 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
210 * quad begins a new primitive. Bit 0 always needs
211 * to be unset)
212 * [32:16] ParamOffset
213 *
214 */
Christian König55fe5cc2013-03-04 16:30:06 +0100215 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200216 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500217
Christian König0666ffd2013-03-05 15:07:39 +0100218 unsigned chan;
219
Tom Stellard0fb1e682012-09-06 16:18:11 -0400220 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
221 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400222 unsigned soa_index =
223 radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellard0fb1e682012-09-06 16:18:11 -0400224 si_shader_ctx->radeon_bld.inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100225 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100226
227 if (chan == 3)
228 /* RCP for fragcoord.w */
229 si_shader_ctx->radeon_bld.inputs[soa_index] =
230 LLVMBuildFDiv(gallivm->builder,
231 lp_build_const_float(gallivm, 1.0f),
232 si_shader_ctx->radeon_bld.inputs[soa_index],
233 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400234 }
235 return;
236 }
237
Michel Dänzer97078b12012-09-25 12:41:31 +0200238 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
239 LLVMValueRef face, is_face_positive;
240
Christian König0666ffd2013-03-05 15:07:39 +0100241 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
242
Michel Dänzer97078b12012-09-25 12:41:31 +0200243 is_face_positive = LLVMBuildFCmp(gallivm->builder,
244 LLVMRealUGT, face,
245 lp_build_const_float(gallivm, 0.0f),
246 "");
247
248 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
249 LLVMBuildSelect(gallivm->builder,
250 is_face_positive,
251 lp_build_const_float(gallivm, 1.0f),
252 lp_build_const_float(gallivm, 0.0f),
253 "");
254 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
255 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
256 lp_build_const_float(gallivm, 0.0f);
257 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
258 lp_build_const_float(gallivm, 1.0f);
259
260 return;
261 }
262
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200263 shader->input[input_index].param_offset = shader->ninterp++;
264 attr_number = lp_build_const_int32(gallivm,
265 shader->input[input_index].param_offset);
266
Tom Stellarda75c6162012-01-06 17:38:37 -0500267 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200268 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500269 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100270 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100271 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200272 } else {
273 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100274 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200275 else
Christian König0666ffd2013-03-05 15:07:39 +0100276 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200277 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500278 break;
279 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100280 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500281 break;
282 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200283 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100284 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200285 else
Christian König0666ffd2013-03-05 15:07:39 +0100286 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200287 break;
288 case TGSI_INTERPOLATE_PERSPECTIVE:
289 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100290 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200291 else
Christian König0666ffd2013-03-05 15:07:39 +0100292 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500293 break;
294 default:
295 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
296 return;
297 }
298
Christian König0666ffd2013-03-05 15:07:39 +0100299 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
300
Tom Stellarda75c6162012-01-06 17:38:37 -0500301 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200302 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100303 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100304 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200305 LLVMValueRef face, is_face_positive;
306 LLVMValueRef back_attr_number =
307 lp_build_const_int32(gallivm,
308 shader->input[input_index].param_offset + 1);
309
Christian König0666ffd2013-03-05 15:07:39 +0100310 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
311
Michel Dänzer691f08d2012-09-06 18:03:38 +0200312 is_face_positive = LLVMBuildFCmp(gallivm->builder,
313 LLVMRealUGT, face,
314 lp_build_const_float(gallivm, 0.0f),
315 "");
316
Tom Stellarda75c6162012-01-06 17:38:37 -0500317 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100318 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200319 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
320 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
321 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
322 LLVMValueRef front, back;
323
324 args[0] = llvm_chan;
325 args[1] = attr_number;
326 front = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100327 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100328 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200329
330 args[1] = back_attr_number;
331 back = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100332 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100333 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200334
335 si_shader_ctx->radeon_bld.inputs[soa_index] =
336 LLVMBuildSelect(gallivm->builder,
337 is_face_positive,
338 front,
339 back,
340 "");
341 }
342
343 shader->ninterp++;
344 } else {
345 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100346 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200347 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
348 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
349 args[0] = llvm_chan;
350 args[1] = attr_number;
351 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100352 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200353 si_shader_ctx->radeon_bld.inputs[soa_index] =
354 build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100355 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100356 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200357 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500358 }
359}
360
361static void declare_input(
362 struct radeon_llvm_context * radeon_bld,
363 unsigned input_index,
364 const struct tgsi_full_declaration *decl)
365{
366 struct si_shader_context * si_shader_ctx =
367 si_shader_context(&radeon_bld->soa.bld_base);
368 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
369 declare_input_vs(si_shader_ctx, input_index, decl);
370 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
371 declare_input_fs(si_shader_ctx, input_index, decl);
372 } else {
373 fprintf(stderr, "Warning: Unsupported shader type,\n");
374 }
375}
376
Christian Könige4ed5872013-03-21 18:02:52 +0100377static void declare_system_value(
378 struct radeon_llvm_context * radeon_bld,
379 unsigned index,
380 const struct tgsi_full_declaration *decl)
381{
Christian Königcf9b31f2013-03-21 18:30:23 +0100382
Christian Könige4ed5872013-03-21 18:02:52 +0100383 LLVMValueRef value = 0;
384
385 switch (decl->Semantic.Name) {
386 case TGSI_SEMANTIC_INSTANCEID:
Christian Königa0dca442013-03-22 15:59:22 +0100387 value = get_instance_index(radeon_bld, 1);
Christian Könige4ed5872013-03-21 18:02:52 +0100388 break;
389
390 case TGSI_SEMANTIC_VERTEXID:
391 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_VERTEX_ID);
392 break;
393
394 default:
395 assert(!"unknown system value");
396 return;
397 }
398
399 radeon_bld->system_values[index] = value;
400}
401
Tom Stellarda75c6162012-01-06 17:38:37 -0500402static LLVMValueRef fetch_constant(
403 struct lp_build_tgsi_context * bld_base,
404 const struct tgsi_full_src_register *reg,
405 enum tgsi_opcode_type type,
406 unsigned swizzle)
407{
Christian König55fe5cc2013-03-04 16:30:06 +0100408 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500409 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100410 const struct tgsi_ind_register *ireg = &reg->Indirect;
411 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500412
Christian Königf5298b02013-02-28 14:50:07 +0100413 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100414 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100415 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500416
Christian König8514f5a2013-02-04 17:46:42 +0100417 if (swizzle == LP_CHAN_ALL) {
418 unsigned chan;
419 LLVMValueRef values[4];
420 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
421 values[chan] = fetch_constant(bld_base, reg, type, chan);
422
423 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
424 }
425
Christian König0f6cf2b2013-03-15 15:53:25 +0100426 idx = reg->Register.Index * 4 + swizzle;
427 if (!reg->Register.Indirect)
428 return bitcast(bld_base, type, si_shader_ctx->constants[idx]);
Christian Königf5298b02013-02-28 14:50:07 +0100429
Christian König0f6cf2b2013-03-15 15:53:25 +0100430 args[0] = si_shader_ctx->const_resource;
431 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
432 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
433 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
434 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
435 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200436
Christian Königf5298b02013-02-28 14:50:07 +0100437 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100438 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500439
Christian Königf5298b02013-02-28 14:50:07 +0100440 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500441}
442
Michel Dänzer26c71392012-08-24 12:03:11 +0200443/* Initialize arguments for the shader export intrinsic */
444static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
445 struct tgsi_full_declaration *d,
446 unsigned index,
447 unsigned target,
448 LLVMValueRef *args)
449{
450 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
451 struct lp_build_context *uint =
452 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
453 struct lp_build_context *base = &bld_base->base;
454 unsigned compressed = 0;
455 unsigned chan;
456
Michel Dänzerf402acd2012-08-22 18:15:36 +0200457 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
458 int cbuf = target - V_008DFC_SQ_EXP_MRT;
459
460 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100461 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100462
463 if (compressed)
464 si_shader_ctx->shader->spi_shader_col_format |=
465 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
466 else
467 si_shader_ctx->shader->spi_shader_col_format |=
468 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200469
470 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200471 }
472 }
473
474 if (compressed) {
475 /* Pixel shader needs to pack output values before export */
476 for (chan = 0; chan < 2; chan++ ) {
477 LLVMValueRef *out_ptr =
478 si_shader_ctx->radeon_bld.soa.outputs[index];
479 args[0] = LLVMBuildLoad(base->gallivm->builder,
480 out_ptr[2 * chan], "");
481 args[1] = LLVMBuildLoad(base->gallivm->builder,
482 out_ptr[2 * chan + 1], "");
483 args[chan + 5] =
484 build_intrinsic(base->gallivm->builder,
485 "llvm.SI.packf16",
486 LLVMInt32TypeInContext(base->gallivm->context),
487 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100488 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100489 args[chan + 7] = args[chan + 5] =
490 LLVMBuildBitCast(base->gallivm->builder,
491 args[chan + 5],
492 LLVMFloatTypeInContext(base->gallivm->context),
493 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200494 }
495
496 /* Set COMPR flag */
497 args[4] = uint->one;
498 } else {
499 for (chan = 0; chan < 4; chan++ ) {
500 LLVMValueRef out_ptr =
501 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
502 /* +5 because the first output value will be
503 * the 6th argument to the intrinsic. */
504 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
505 out_ptr, "");
506 }
507
508 /* Clear COMPR flag */
509 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200510 }
511
512 /* XXX: This controls which components of the output
513 * registers actually get exported. (e.g bit 0 means export
514 * X component, bit 1 means export Y component, etc.) I'm
515 * hard coding this to 0xf for now. In the future, we might
516 * want to do something else. */
517 args[0] = lp_build_const_int32(base->gallivm, 0xf);
518
519 /* Specify whether the EXEC mask represents the valid mask */
520 args[1] = uint->zero;
521
522 /* Specify whether this is the last export */
523 args[2] = uint->zero;
524
525 /* Specify the target we are exporting */
526 args[3] = lp_build_const_int32(base->gallivm, target);
527
Michel Dänzer26c71392012-08-24 12:03:11 +0200528 /* XXX: We probably need to keep track of the output
529 * values, so we know what we are passing to the next
530 * stage. */
531}
532
Michel Dänzer7708a862012-11-02 15:57:30 +0100533static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
534 unsigned index)
535{
536 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
537 struct gallivm_state *gallivm = bld_base->base.gallivm;
538
Christian Königa0dca442013-03-22 15:59:22 +0100539 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Michel Dänzer7708a862012-11-02 15:57:30 +0100540 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
541 LLVMValueRef alpha_pass =
542 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100543 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer7708a862012-11-02 15:57:30 +0100544 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
Christian Königa0dca442013-03-22 15:59:22 +0100545 lp_build_const_float(gallivm, si_shader_ctx->shader->key.ps.alpha_ref));
Michel Dänzer7708a862012-11-02 15:57:30 +0100546 LLVMValueRef arg =
547 lp_build_select(&bld_base->base,
548 alpha_pass,
549 lp_build_const_float(gallivm, 1.0f),
550 lp_build_const_float(gallivm, -1.0f));
551
552 build_intrinsic(gallivm->builder,
553 "llvm.AMDGPU.kill",
554 LLVMVoidTypeInContext(gallivm->context),
555 &arg, 1, 0);
556 } else {
557 build_intrinsic(gallivm->builder,
558 "llvm.AMDGPU.kilp",
559 LLVMVoidTypeInContext(gallivm->context),
560 NULL, 0, 0);
561 }
562}
563
Marek Olšák6d4755a2013-07-30 22:29:29 +0200564static void si_alpha_to_one(struct lp_build_tgsi_context *bld_base,
565 unsigned index)
566{
567 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
568
569 /* set alpha to one */
570 LLVMBuildStore(bld_base->base.gallivm->builder,
571 bld_base->base.one,
572 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
573}
574
Michel Dänzere3befbc2013-05-15 18:09:50 +0200575static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzerb00269a2013-08-07 18:14:16 +0200576 LLVMValueRef (*pos)[9], unsigned index)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200577{
578 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200579 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200580 struct lp_build_context *base = &bld_base->base;
581 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200582 unsigned reg_index;
583 unsigned chan;
584 unsigned const_chan;
585 LLVMValueRef out_elts[4];
586 LLVMValueRef base_elt;
587 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
588 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, uint->one);
589
590 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
591 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
592 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
593 }
594
595 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200596 LLVMValueRef *args = pos[2 + reg_index];
597
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200598 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
599 continue;
600
601 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
602
Michel Dänzere3befbc2013-05-15 18:09:50 +0200603 args[5] =
604 args[6] =
605 args[7] =
606 args[8] = lp_build_const_float(base->gallivm, 0.0f);
607
608 /* Compute dot products of position and user clip plane vectors */
609 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
610 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
611 args[0] = const_resource;
612 args[1] = lp_build_const_int32(base->gallivm,
613 ((reg_index * 4 + chan) * 4 +
614 const_chan) * 4);
615 base_elt = build_intrinsic(base->gallivm->builder,
616 "llvm.SI.load.const",
617 base->elem_type,
618 args, 2,
619 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
620 args[5 + chan] =
621 lp_build_add(base, args[5 + chan],
622 lp_build_mul(base, base_elt,
623 out_elts[const_chan]));
624 }
625 }
626
627 args[0] = lp_build_const_int32(base->gallivm, 0xf);
628 args[1] = uint->zero;
629 args[2] = uint->zero;
630 args[3] = lp_build_const_int32(base->gallivm,
631 V_008DFC_SQ_EXP_POS + 2 + reg_index);
632 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200633 }
634}
635
Tom Stellarda75c6162012-01-06 17:38:37 -0500636/* XXX: This is partially implemented for VS only at this point. It is not complete */
637static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
638{
639 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200640 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500641 struct lp_build_context * base = &bld_base->base;
642 struct lp_build_context * uint =
643 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
644 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100645 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500646 LLVMValueRef last_args[9] = { 0 };
Michel Dänzerb00269a2013-08-07 18:14:16 +0200647 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer0afeea52013-05-02 14:53:17 +0200648 unsigned semantic_name;
Christian König35088152012-08-01 22:35:24 +0200649 unsigned color_count = 0;
650 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100651 int depth_index = -1, stencil_index = -1;
Michel Dänzerb00269a2013-08-07 18:14:16 +0200652 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -0500653
654 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500655 struct tgsi_full_declaration *d =
656 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500657 unsigned target;
658 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500659
660 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100661
662 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
663 parse->FullToken.FullProperty.Property.PropertyName ==
664 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
665 shader->fs_write_all = TRUE;
666
Tom Stellarda75c6162012-01-06 17:38:37 -0500667 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
668 continue;
669
670 switch (d->Declaration.File) {
671 case TGSI_FILE_INPUT:
672 i = shader->ninput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200673 assert(i < Elements(shader->input));
Tom Stellarda75c6162012-01-06 17:38:37 -0500674 shader->input[i].name = d->Semantic.Name;
675 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200676 shader->input[i].interpolate = d->Interp.Interpolate;
677 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200678 continue;
679
Tom Stellarda75c6162012-01-06 17:38:37 -0500680 case TGSI_FILE_OUTPUT:
681 i = shader->noutput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200682 assert(i < Elements(shader->output));
Tom Stellarda75c6162012-01-06 17:38:37 -0500683 shader->output[i].name = d->Semantic.Name;
684 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200685 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500686 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500687
Christian König35088152012-08-01 22:35:24 +0200688 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500689 continue;
Christian König35088152012-08-01 22:35:24 +0200690 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500691
Michel Dänzer0afeea52013-05-02 14:53:17 +0200692 semantic_name = d->Semantic.Name;
693handle_semantic:
Tom Stellarda75c6162012-01-06 17:38:37 -0500694 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500695 /* Select the correct target */
Michel Dänzer0afeea52013-05-02 14:53:17 +0200696 switch(semantic_name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400697 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer4730dea2013-05-03 17:59:34 +0200698 shader->vs_out_misc_write = 1;
699 shader->vs_out_point_size = 1;
700 target = V_008DFC_SQ_EXP_POS + 1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500701 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100702 case TGSI_SEMANTIC_POSITION:
703 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
704 target = V_008DFC_SQ_EXP_POS;
705 break;
706 } else {
707 depth_index = index;
708 continue;
709 }
710 case TGSI_SEMANTIC_STENCIL:
711 stencil_index = index;
712 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500713 case TGSI_SEMANTIC_COLOR:
714 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200715 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500716 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200717 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500718 param_count++;
719 } else {
720 target = V_008DFC_SQ_EXP_MRT + color_count;
Marek Olšák6d4755a2013-07-30 22:29:29 +0200721 if (si_shader_ctx->shader->key.ps.alpha_to_one) {
722 si_alpha_to_one(bld_base, index);
723 }
Michel Dänzer7708a862012-11-02 15:57:30 +0100724 if (color_count == 0 &&
Christian Königa0dca442013-03-22 15:59:22 +0100725 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer7708a862012-11-02 15:57:30 +0100726 si_alpha_test(bld_base, index);
727
Tom Stellarda75c6162012-01-06 17:38:37 -0500728 color_count++;
729 }
730 break;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200731 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200732 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
733 (1 << d->Semantic.Index)))
734 continue;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200735 shader->clip_dist_write |=
736 d->Declaration.UsageMask << (d->Semantic.Index << 2);
737 target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index;
738 break;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200739 case TGSI_SEMANTIC_CLIPVERTEX:
Michel Dänzerb00269a2013-08-07 18:14:16 +0200740 si_llvm_emit_clipvertex(bld_base, pos_args, index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200741 continue;
Michel Dänzer30b30372012-09-06 17:53:04 +0200742 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500743 case TGSI_SEMANTIC_GENERIC:
744 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200745 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500746 param_count++;
747 break;
748 default:
749 target = 0;
750 fprintf(stderr,
751 "Warning: SI unhandled output type:%d\n",
Michel Dänzer0afeea52013-05-02 14:53:17 +0200752 semantic_name);
Tom Stellarda75c6162012-01-06 17:38:37 -0500753 }
754
Michel Dänzer26c71392012-08-24 12:03:11 +0200755 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500756
Michel Dänzerb00269a2013-08-07 18:14:16 +0200757 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
758 target >= V_008DFC_SQ_EXP_POS &&
759 target <= (V_008DFC_SQ_EXP_POS + 3)) {
760 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
761 args, sizeof(args));
762 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT &&
763 semantic_name == TGSI_SEMANTIC_COLOR) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500764 if (last_args[0]) {
765 lp_build_intrinsic(base->gallivm->builder,
766 "llvm.SI.export",
767 LLVMVoidTypeInContext(base->gallivm->context),
768 last_args, 9);
769 }
770
771 memcpy(last_args, args, sizeof(args));
772 } else {
773 lp_build_intrinsic(base->gallivm->builder,
774 "llvm.SI.export",
775 LLVMVoidTypeInContext(base->gallivm->context),
776 args, 9);
777 }
778
779 }
Michel Dänzer0afeea52013-05-02 14:53:17 +0200780
781 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
782 semantic_name = TGSI_SEMANTIC_GENERIC;
783 goto handle_semantic;
784 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500785 }
786
Michel Dänzer1a616c12012-11-13 17:35:09 +0100787 if (depth_index >= 0 || stencil_index >= 0) {
788 LLVMValueRef out_ptr;
789 unsigned mask = 0;
790
791 /* Specify the target we are exporting */
792 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
793
794 if (depth_index >= 0) {
795 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
796 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
797 mask |= 0x1;
798
799 if (stencil_index < 0) {
800 args[6] =
801 args[7] =
802 args[8] = args[5];
803 }
804 }
805
806 if (stencil_index >= 0) {
807 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
808 args[7] =
809 args[8] =
810 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
811 mask |= 0x2;
812
813 if (depth_index < 0)
814 args[5] = args[6];
815 }
816
817 /* Specify which components to enable */
818 args[0] = lp_build_const_int32(base->gallivm, mask);
819
820 args[1] =
821 args[2] =
822 args[4] = uint->zero;
823
824 if (last_args[0])
825 lp_build_intrinsic(base->gallivm->builder,
826 "llvm.SI.export",
827 LLVMVoidTypeInContext(base->gallivm->context),
828 args, 9);
829 else
830 memcpy(last_args, args, sizeof(args));
831 }
832
Michel Dänzerb00269a2013-08-07 18:14:16 +0200833 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
834 unsigned pos_idx = 0;
Christian Königf18fd252012-07-25 21:58:46 +0200835
Michel Dänzerb00269a2013-08-07 18:14:16 +0200836 for (i = 0; i < 4; i++)
837 if (pos_args[i][0])
838 shader->nr_pos_exports++;
Christian Königf18fd252012-07-25 21:58:46 +0200839
Michel Dänzerb00269a2013-08-07 18:14:16 +0200840 for (i = 0; i < 4; i++) {
841 if (!pos_args[i][0])
842 continue;
Christian Königf18fd252012-07-25 21:58:46 +0200843
Michel Dänzerc8402702013-02-12 18:37:22 +0100844 /* Specify the target we are exporting */
Michel Dänzerb00269a2013-08-07 18:14:16 +0200845 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
846
847 if (pos_idx == shader->nr_pos_exports)
848 /* Specify that this is the last export */
849 pos_args[i][2] = uint->one;
Michel Dänzerc8402702013-02-12 18:37:22 +0100850
851 lp_build_intrinsic(base->gallivm->builder,
852 "llvm.SI.export",
853 LLVMVoidTypeInContext(base->gallivm->context),
Michel Dänzerb00269a2013-08-07 18:14:16 +0200854 pos_args[i], 9);
855 }
856 } else {
857 if (!last_args[0]) {
858 /* Specify which components to enable */
859 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
860
861 /* Specify the target we are exporting */
862 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
863
864 /* Set COMPR flag to zero to export data as 32-bit */
865 last_args[4] = uint->zero;
866
867 /* dummy bits */
868 last_args[5]= uint->zero;
869 last_args[6]= uint->zero;
870 last_args[7]= uint->zero;
871 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +0100872
873 si_shader_ctx->shader->spi_shader_col_format |=
Michel Dänzerb00269a2013-08-07 18:14:16 +0200874 V_028714_SPI_SHADER_32_ABGR;
875 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +0100876 }
877
Michel Dänzerb00269a2013-08-07 18:14:16 +0200878 /* Specify whether the EXEC mask represents the valid mask */
879 last_args[1] = uint->one;
880
881 if (shader->fs_write_all && shader->nr_cbufs > 1) {
882 int i;
883
884 /* Specify that this is not yet the last export */
885 last_args[2] = lp_build_const_int32(base->gallivm, 0);
886
887 for (i = 1; i < shader->nr_cbufs; i++) {
888 /* Specify the target we are exporting */
889 last_args[3] = lp_build_const_int32(base->gallivm,
890 V_008DFC_SQ_EXP_MRT + i);
891
892 lp_build_intrinsic(base->gallivm->builder,
893 "llvm.SI.export",
894 LLVMVoidTypeInContext(base->gallivm->context),
895 last_args, 9);
896
897 si_shader_ctx->shader->spi_shader_col_format |=
898 si_shader_ctx->shader->spi_shader_col_format << 4;
899 si_shader_ctx->shader->cb_shader_mask |=
900 si_shader_ctx->shader->cb_shader_mask << 4;
901 }
902
903 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
904 }
905
906 /* Specify that this is the last export */
907 last_args[2] = lp_build_const_int32(base->gallivm, 1);
908
909 lp_build_intrinsic(base->gallivm->builder,
910 "llvm.SI.export",
911 LLVMVoidTypeInContext(base->gallivm->context),
912 last_args, 9);
Michel Dänzerc8402702013-02-12 18:37:22 +0100913 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500914/* XXX: Look up what this function does */
915/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
916}
917
Marek Olšák4855acd2013-08-06 15:08:54 +0200918static const struct lp_build_tgsi_action txf_action;
919
920static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
921 struct lp_build_tgsi_context * bld_base,
922 struct lp_build_emit_data * emit_data);
923
Tom Stellarda75c6162012-01-06 17:38:37 -0500924static void tex_fetch_args(
925 struct lp_build_tgsi_context * bld_base,
926 struct lp_build_emit_data * emit_data)
927{
Christian König55fe5cc2013-03-04 16:30:06 +0100928 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100929 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200930 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100931 unsigned opcode = inst->Instruction.Opcode;
932 unsigned target = inst->Texture.Texture;
Marek Olšák4855acd2013-08-06 15:08:54 +0200933 unsigned sampler_src, sampler_index;
Michel Dänzer120efee2013-01-25 12:10:11 +0100934 LLVMValueRef coords[4];
935 LLVMValueRef address[16];
Marek Olšák4855acd2013-08-06 15:08:54 +0200936 LLVMValueRef sample_index_rewrite = NULL;
937 LLVMValueRef sample_chan = NULL;
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100938 int ref_pos;
939 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +0100940 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100941 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400942
Michel Dänzer120efee2013-01-25 12:10:11 +0100943 /* Fetch and project texture coordinates */
944 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100945 for (chan = 0; chan < 3; chan++ ) {
946 coords[chan] = lp_build_emit_fetch(bld_base,
947 emit_data->inst, 0,
948 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100949 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200950 coords[chan] = lp_build_emit_llvm_binary(bld_base,
951 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100952 coords[chan],
953 coords[3]);
954 }
955
Michel Dänzer120efee2013-01-25 12:10:11 +0100956 if (opcode == TGSI_OPCODE_TXP)
957 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500958
Michel Dänzer120efee2013-01-25 12:10:11 +0100959 /* Pack LOD bias value */
960 if (opcode == TGSI_OPCODE_TXB)
961 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400962
Michel Dänzer0495adb2013-05-06 12:45:14 +0200963 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100964 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100965
966 /* Pack depth comparison value */
967 switch (target) {
968 case TGSI_TEXTURE_SHADOW1D:
969 case TGSI_TEXTURE_SHADOW1D_ARRAY:
970 case TGSI_TEXTURE_SHADOW2D:
971 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +0100972 case TGSI_TEXTURE_SHADOWCUBE:
973 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100974 assert(ref_pos >= 0);
975 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +0100976 break;
977 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
978 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +0100979 }
980
Michel Dänzera6b83c02013-02-21 16:10:55 +0100981 /* Pack user derivatives */
982 if (opcode == TGSI_OPCODE_TXD) {
983 for (chan = 0; chan < 2; chan++) {
984 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
985 if (num_coords > 1)
986 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
987 }
988 }
989
Michel Dänzer120efee2013-01-25 12:10:11 +0100990 /* Pack texture coordinates */
991 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100992 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +0100993 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100994 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +0100995 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +0100996
Michel Dänzer120efee2013-01-25 12:10:11 +0100997 /* Pack array slice */
998 switch (target) {
999 case TGSI_TEXTURE_1D_ARRAY:
1000 address[count++] = coords[1];
1001 }
1002 switch (target) {
1003 case TGSI_TEXTURE_2D_ARRAY:
1004 case TGSI_TEXTURE_2D_ARRAY_MSAA:
1005 case TGSI_TEXTURE_SHADOW2D_ARRAY:
1006 address[count++] = coords[2];
1007 }
1008 switch (target) {
1009 case TGSI_TEXTURE_CUBE_ARRAY:
1010 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1011 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1012 address[count++] = coords[3];
1013 }
1014
1015 /* 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}