blob: 80dd773c10cf4f4fa59b71a7606e5d6172dfee38 [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"
Marek Olšák8d03d922013-09-01 23:59:06 +020037#include "gallivm/lp_bld_flow.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040039#include "radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010040#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050041#include "tgsi/tgsi_info.h"
42#include "tgsi/tgsi_parse.h"
43#include "tgsi/tgsi_scan.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010044#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050045#include "tgsi/tgsi_dump.h"
46
47#include "radeonsi_pipe.h"
48#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020049#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050050#include "sid.h"
51
52#include <assert.h>
53#include <errno.h>
54#include <stdio.h>
55
Tom Stellarda75c6162012-01-06 17:38:37 -050056struct si_shader_context
57{
58 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050059 struct tgsi_parse_context parse;
60 struct tgsi_token * tokens;
61 struct si_pipe_shader *shader;
62 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020063 int param_streamout_config;
64 int param_streamout_write_index;
65 int param_streamout_offset[4];
66 int param_vertex_id;
67 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010068 LLVMValueRef const_md;
Christian König0f6cf2b2013-03-15 15:53:25 +010069 LLVMValueRef const_resource;
Michel Dänzera06ee5a2013-06-19 18:14:01 +020070#if HAVE_LLVM >= 0x0304
71 LLVMValueRef ddxy_lds;
72#endif
Christian König0f6cf2b2013-03-15 15:53:25 +010073 LLVMValueRef *constants;
Christian König1c100182013-03-17 16:02:42 +010074 LLVMValueRef *resources;
75 LLVMValueRef *samplers;
Marek Olšák8d03d922013-09-01 23:59:06 +020076 LLVMValueRef so_buffers[4];
Tom Stellarda75c6162012-01-06 17:38:37 -050077};
78
79static struct si_shader_context * si_shader_context(
80 struct lp_build_tgsi_context * bld_base)
81{
82 return (struct si_shader_context *)bld_base;
83}
84
85
86#define PERSPECTIVE_BASE 0
87#define LINEAR_BASE 9
88
89#define SAMPLE_OFFSET 0
90#define CENTER_OFFSET 2
91#define CENTROID_OFSET 4
92
93#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040094#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020095#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040096#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050097
Tom Stellard467f5162012-05-16 15:15:35 -040098/**
99 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
100 *
101 * @param offset The offset parameter specifies the number of
102 * elements to offset, not the number of bytes or dwords. An element is the
103 * the type pointed to by the base_ptr parameter (e.g. int is the element of
104 * an int* pointer)
105 *
106 * When LLVM lowers the load instruction, it will convert the element offset
107 * into a dword offset automatically.
108 *
109 */
110static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100111 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400112 LLVMValueRef base_ptr,
113 LLVMValueRef offset)
114{
Christian König206f0592013-03-20 14:37:21 +0100115 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400116
Christian König206f0592013-03-20 14:37:21 +0100117 LLVMValueRef computed_ptr = LLVMBuildGEP(
118 base->gallivm->builder, base_ptr, &offset, 1, "");
119
120 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
121 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
122 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400123}
124
Christian Königa0dca442013-03-22 15:59:22 +0100125static LLVMValueRef get_instance_index(
126 struct radeon_llvm_context * radeon_bld,
127 unsigned divisor)
128{
Marek Olšák8d03d922013-09-01 23:59:06 +0200129 struct si_shader_context *si_shader_ctx =
130 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100131 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
132
Marek Olšák8d03d922013-09-01 23:59:06 +0200133 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
134 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100135 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
136 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
137
138 if (divisor > 1)
139 result = LLVMBuildUDiv(gallivm->builder, result,
140 lp_build_const_int32(gallivm, divisor), "");
141
142 return result;
143}
144
Tom Stellarda75c6162012-01-06 17:38:37 -0500145static void declare_input_vs(
146 struct si_shader_context * si_shader_ctx,
147 unsigned input_index,
148 const struct tgsi_full_declaration *decl)
149{
Christian Königa0dca442013-03-22 15:59:22 +0100150 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
151 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
152
153 unsigned chan;
154
Tom Stellarda75c6162012-01-06 17:38:37 -0500155 LLVMValueRef t_list_ptr;
156 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400157 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500158 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100159 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400160 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500161 LLVMTypeRef vec4_type;
162 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500163
Tom Stellard467f5162012-05-16 15:15:35 -0400164 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100165 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500166
Christian Königb15e3ae2012-07-25 11:22:59 +0200167 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400168
Christian König206f0592013-03-20 14:37:21 +0100169 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400170
171 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200172 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500173
Christian Königa0dca442013-03-22 15:59:22 +0100174 if (divisor) {
175 /* Build index from instance ID, start instance and divisor */
176 si_shader_ctx->shader->shader.uses_instanceid = true;
177 buffer_index = get_instance_index(&si_shader_ctx->radeon_bld, divisor);
178 } else {
179 /* Load the buffer index, which is always stored in VGPR0
180 * for Vertex Shaders */
Marek Olšák8d03d922013-09-01 23:59:06 +0200181 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
182 si_shader_ctx->param_vertex_id);
Christian Königa0dca442013-03-22 15:59:22 +0100183 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500184
185 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400186 args[0] = t_list;
187 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100188 args[2] = buffer_index;
Christian König44e32242013-03-20 12:10:35 +0100189 input = build_intrinsic(base->gallivm->builder,
190 "llvm.SI.vs.load.input", vec4_type, args, 3,
191 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500192
193 /* Break up the vec4 into individual components */
194 for (chan = 0; chan < 4; chan++) {
195 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
196 /* XXX: Use a helper function for this. There is one in
197 * tgsi_llvm.c. */
198 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
199 LLVMBuildExtractElement(base->gallivm->builder,
200 input, llvm_chan, "");
201 }
202}
203
204static void declare_input_fs(
205 struct si_shader_context * si_shader_ctx,
206 unsigned input_index,
207 const struct tgsi_full_declaration *decl)
208{
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200209 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500210 struct lp_build_context * base =
211 &si_shader_ctx->radeon_bld.soa.bld_base.base;
Michel Dänzer237cb072013-08-21 18:00:35 +0200212 struct lp_build_context *uint =
213 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -0500214 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400215 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100216 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
217
218 LLVMValueRef interp_param;
219 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500220
221 /* This value is:
222 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
223 * quad begins a new primitive. Bit 0 always needs
224 * to be unset)
225 * [32:16] ParamOffset
226 *
227 */
Christian König55fe5cc2013-03-04 16:30:06 +0100228 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200229 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500230
Christian König0666ffd2013-03-05 15:07:39 +0100231 unsigned chan;
232
Tom Stellard0fb1e682012-09-06 16:18:11 -0400233 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
234 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400235 unsigned soa_index =
236 radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellard0fb1e682012-09-06 16:18:11 -0400237 si_shader_ctx->radeon_bld.inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100238 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100239
240 if (chan == 3)
241 /* RCP for fragcoord.w */
242 si_shader_ctx->radeon_bld.inputs[soa_index] =
243 LLVMBuildFDiv(gallivm->builder,
244 lp_build_const_float(gallivm, 1.0f),
245 si_shader_ctx->radeon_bld.inputs[soa_index],
246 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400247 }
248 return;
249 }
250
Michel Dänzer97078b12012-09-25 12:41:31 +0200251 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
252 LLVMValueRef face, is_face_positive;
253
Christian König0666ffd2013-03-05 15:07:39 +0100254 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
255
Michel Dänzer97078b12012-09-25 12:41:31 +0200256 is_face_positive = LLVMBuildFCmp(gallivm->builder,
257 LLVMRealUGT, face,
258 lp_build_const_float(gallivm, 0.0f),
259 "");
260
261 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
262 LLVMBuildSelect(gallivm->builder,
263 is_face_positive,
264 lp_build_const_float(gallivm, 1.0f),
265 lp_build_const_float(gallivm, 0.0f),
266 "");
267 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
268 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
269 lp_build_const_float(gallivm, 0.0f);
270 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
271 lp_build_const_float(gallivm, 1.0f);
272
273 return;
274 }
275
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200276 shader->input[input_index].param_offset = shader->ninterp++;
277 attr_number = lp_build_const_int32(gallivm,
278 shader->input[input_index].param_offset);
279
Tom Stellarda75c6162012-01-06 17:38:37 -0500280 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200281 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500282 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100283 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100284 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200285 } else {
286 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100287 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200288 else
Christian König0666ffd2013-03-05 15:07:39 +0100289 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200290 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500291 break;
292 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100293 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500294 break;
295 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200296 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100297 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200298 else
Christian König0666ffd2013-03-05 15:07:39 +0100299 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200300 break;
301 case TGSI_INTERPOLATE_PERSPECTIVE:
302 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100303 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200304 else
Christian König0666ffd2013-03-05 15:07:39 +0100305 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500306 break;
307 default:
308 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
309 return;
310 }
311
Christian König0666ffd2013-03-05 15:07:39 +0100312 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
313
Tom Stellarda75c6162012-01-06 17:38:37 -0500314 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200315 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100316 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100317 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200318 LLVMValueRef face, is_face_positive;
319 LLVMValueRef back_attr_number =
320 lp_build_const_int32(gallivm,
321 shader->input[input_index].param_offset + 1);
322
Christian König0666ffd2013-03-05 15:07:39 +0100323 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
324
Michel Dänzer691f08d2012-09-06 18:03:38 +0200325 is_face_positive = LLVMBuildFCmp(gallivm->builder,
326 LLVMRealUGT, face,
327 lp_build_const_float(gallivm, 0.0f),
328 "");
329
Tom Stellarda75c6162012-01-06 17:38:37 -0500330 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100331 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200332 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
333 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
334 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
335 LLVMValueRef front, back;
336
337 args[0] = llvm_chan;
338 args[1] = attr_number;
339 front = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100340 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100341 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200342
343 args[1] = back_attr_number;
344 back = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100345 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100346 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200347
348 si_shader_ctx->radeon_bld.inputs[soa_index] =
349 LLVMBuildSelect(gallivm->builder,
350 is_face_positive,
351 front,
352 back,
353 "");
354 }
355
356 shader->ninterp++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200357 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
358 LLVMValueRef args[4];
359
360 args[0] = uint->zero;
361 args[1] = attr_number;
362 args[2] = params;
363 args[3] = interp_param;
364 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
365 build_intrinsic(base->gallivm->builder, intr_name,
366 input_type, args, args[3] ? 4 : 3,
367 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
368 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
369 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
370 lp_build_const_float(gallivm, 0.0f);
371 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
372 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200373 } else {
374 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100375 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200376 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
377 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
378 args[0] = llvm_chan;
379 args[1] = attr_number;
380 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100381 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200382 si_shader_ctx->radeon_bld.inputs[soa_index] =
383 build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100384 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100385 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200386 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500387 }
388}
389
390static void declare_input(
391 struct radeon_llvm_context * radeon_bld,
392 unsigned input_index,
393 const struct tgsi_full_declaration *decl)
394{
395 struct si_shader_context * si_shader_ctx =
396 si_shader_context(&radeon_bld->soa.bld_base);
397 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
398 declare_input_vs(si_shader_ctx, input_index, decl);
399 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
400 declare_input_fs(si_shader_ctx, input_index, decl);
401 } else {
402 fprintf(stderr, "Warning: Unsupported shader type,\n");
403 }
404}
405
Christian Könige4ed5872013-03-21 18:02:52 +0100406static void declare_system_value(
407 struct radeon_llvm_context * radeon_bld,
408 unsigned index,
409 const struct tgsi_full_declaration *decl)
410{
Marek Olšák8d03d922013-09-01 23:59:06 +0200411 struct si_shader_context *si_shader_ctx =
412 si_shader_context(&radeon_bld->soa.bld_base);
Christian Könige4ed5872013-03-21 18:02:52 +0100413 LLVMValueRef value = 0;
414
415 switch (decl->Semantic.Name) {
416 case TGSI_SEMANTIC_INSTANCEID:
Christian Königa0dca442013-03-22 15:59:22 +0100417 value = get_instance_index(radeon_bld, 1);
Christian Könige4ed5872013-03-21 18:02:52 +0100418 break;
419
420 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200421 value = LLVMGetParam(radeon_bld->main_fn,
422 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100423 break;
424
425 default:
426 assert(!"unknown system value");
427 return;
428 }
429
430 radeon_bld->system_values[index] = value;
431}
432
Tom Stellarda75c6162012-01-06 17:38:37 -0500433static LLVMValueRef fetch_constant(
434 struct lp_build_tgsi_context * bld_base,
435 const struct tgsi_full_src_register *reg,
436 enum tgsi_opcode_type type,
437 unsigned swizzle)
438{
Christian König55fe5cc2013-03-04 16:30:06 +0100439 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500440 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100441 const struct tgsi_ind_register *ireg = &reg->Indirect;
442 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500443
Christian Königf5298b02013-02-28 14:50:07 +0100444 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100445 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100446 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500447
Christian König8514f5a2013-02-04 17:46:42 +0100448 if (swizzle == LP_CHAN_ALL) {
449 unsigned chan;
450 LLVMValueRef values[4];
451 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
452 values[chan] = fetch_constant(bld_base, reg, type, chan);
453
454 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
455 }
456
Christian König0f6cf2b2013-03-15 15:53:25 +0100457 idx = reg->Register.Index * 4 + swizzle;
458 if (!reg->Register.Indirect)
459 return bitcast(bld_base, type, si_shader_ctx->constants[idx]);
Christian Königf5298b02013-02-28 14:50:07 +0100460
Christian König0f6cf2b2013-03-15 15:53:25 +0100461 args[0] = si_shader_ctx->const_resource;
462 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
463 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
464 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
465 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
466 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200467
Christian Königf5298b02013-02-28 14:50:07 +0100468 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100469 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500470
Christian Königf5298b02013-02-28 14:50:07 +0100471 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500472}
473
Michel Dänzer26c71392012-08-24 12:03:11 +0200474/* Initialize arguments for the shader export intrinsic */
475static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
476 struct tgsi_full_declaration *d,
477 unsigned index,
478 unsigned target,
479 LLVMValueRef *args)
480{
481 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
482 struct lp_build_context *uint =
483 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
484 struct lp_build_context *base = &bld_base->base;
485 unsigned compressed = 0;
486 unsigned chan;
487
Michel Dänzerf402acd2012-08-22 18:15:36 +0200488 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
489 int cbuf = target - V_008DFC_SQ_EXP_MRT;
490
491 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100492 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100493
494 if (compressed)
495 si_shader_ctx->shader->spi_shader_col_format |=
496 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
497 else
498 si_shader_ctx->shader->spi_shader_col_format |=
499 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200500
501 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200502 }
503 }
504
505 if (compressed) {
506 /* Pixel shader needs to pack output values before export */
507 for (chan = 0; chan < 2; chan++ ) {
508 LLVMValueRef *out_ptr =
509 si_shader_ctx->radeon_bld.soa.outputs[index];
510 args[0] = LLVMBuildLoad(base->gallivm->builder,
511 out_ptr[2 * chan], "");
512 args[1] = LLVMBuildLoad(base->gallivm->builder,
513 out_ptr[2 * chan + 1], "");
514 args[chan + 5] =
515 build_intrinsic(base->gallivm->builder,
516 "llvm.SI.packf16",
517 LLVMInt32TypeInContext(base->gallivm->context),
518 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100519 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100520 args[chan + 7] = args[chan + 5] =
521 LLVMBuildBitCast(base->gallivm->builder,
522 args[chan + 5],
523 LLVMFloatTypeInContext(base->gallivm->context),
524 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200525 }
526
527 /* Set COMPR flag */
528 args[4] = uint->one;
529 } else {
530 for (chan = 0; chan < 4; chan++ ) {
531 LLVMValueRef out_ptr =
532 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
533 /* +5 because the first output value will be
534 * the 6th argument to the intrinsic. */
535 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
536 out_ptr, "");
537 }
538
539 /* Clear COMPR flag */
540 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200541 }
542
543 /* XXX: This controls which components of the output
544 * registers actually get exported. (e.g bit 0 means export
545 * X component, bit 1 means export Y component, etc.) I'm
546 * hard coding this to 0xf for now. In the future, we might
547 * want to do something else. */
548 args[0] = lp_build_const_int32(base->gallivm, 0xf);
549
550 /* Specify whether the EXEC mask represents the valid mask */
551 args[1] = uint->zero;
552
553 /* Specify whether this is the last export */
554 args[2] = uint->zero;
555
556 /* Specify the target we are exporting */
557 args[3] = lp_build_const_int32(base->gallivm, target);
558
Michel Dänzer26c71392012-08-24 12:03:11 +0200559 /* XXX: We probably need to keep track of the output
560 * values, so we know what we are passing to the next
561 * stage. */
562}
563
Michel Dänzer7708a862012-11-02 15:57:30 +0100564static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
565 unsigned index)
566{
567 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
568 struct gallivm_state *gallivm = bld_base->base.gallivm;
569
Christian Königa0dca442013-03-22 15:59:22 +0100570 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Michel Dänzer7708a862012-11-02 15:57:30 +0100571 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
572 LLVMValueRef alpha_pass =
573 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100574 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer7708a862012-11-02 15:57:30 +0100575 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
Christian Königa0dca442013-03-22 15:59:22 +0100576 lp_build_const_float(gallivm, si_shader_ctx->shader->key.ps.alpha_ref));
Michel Dänzer7708a862012-11-02 15:57:30 +0100577 LLVMValueRef arg =
578 lp_build_select(&bld_base->base,
579 alpha_pass,
580 lp_build_const_float(gallivm, 1.0f),
581 lp_build_const_float(gallivm, -1.0f));
582
583 build_intrinsic(gallivm->builder,
584 "llvm.AMDGPU.kill",
585 LLVMVoidTypeInContext(gallivm->context),
586 &arg, 1, 0);
587 } else {
588 build_intrinsic(gallivm->builder,
589 "llvm.AMDGPU.kilp",
590 LLVMVoidTypeInContext(gallivm->context),
591 NULL, 0, 0);
592 }
593}
594
Marek Olšák6d4755a2013-07-30 22:29:29 +0200595static void si_alpha_to_one(struct lp_build_tgsi_context *bld_base,
596 unsigned index)
597{
598 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
599
600 /* set alpha to one */
601 LLVMBuildStore(bld_base->base.gallivm->builder,
602 bld_base->base.one,
603 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
604}
605
Michel Dänzere3befbc2013-05-15 18:09:50 +0200606static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzerb00269a2013-08-07 18:14:16 +0200607 LLVMValueRef (*pos)[9], unsigned index)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200608{
609 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200610 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200611 struct lp_build_context *base = &bld_base->base;
612 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200613 unsigned reg_index;
614 unsigned chan;
615 unsigned const_chan;
616 LLVMValueRef out_elts[4];
617 LLVMValueRef base_elt;
618 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
619 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, uint->one);
620
621 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
622 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
623 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
624 }
625
626 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200627 LLVMValueRef *args = pos[2 + reg_index];
628
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200629 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
630 continue;
631
632 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
633
Michel Dänzere3befbc2013-05-15 18:09:50 +0200634 args[5] =
635 args[6] =
636 args[7] =
637 args[8] = lp_build_const_float(base->gallivm, 0.0f);
638
639 /* Compute dot products of position and user clip plane vectors */
640 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
641 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
642 args[0] = const_resource;
643 args[1] = lp_build_const_int32(base->gallivm,
644 ((reg_index * 4 + chan) * 4 +
645 const_chan) * 4);
646 base_elt = build_intrinsic(base->gallivm->builder,
647 "llvm.SI.load.const",
648 base->elem_type,
649 args, 2,
650 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
651 args[5 + chan] =
652 lp_build_add(base, args[5 + chan],
653 lp_build_mul(base, base_elt,
654 out_elts[const_chan]));
655 }
656 }
657
658 args[0] = lp_build_const_int32(base->gallivm, 0xf);
659 args[1] = uint->zero;
660 args[2] = uint->zero;
661 args[3] = lp_build_const_int32(base->gallivm,
662 V_008DFC_SQ_EXP_POS + 2 + reg_index);
663 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200664 }
665}
666
Marek Olšák8d03d922013-09-01 23:59:06 +0200667static void si_dump_streamout(struct pipe_stream_output_info *so)
668{
669 unsigned i;
670
671 if (so->num_outputs)
672 fprintf(stderr, "STREAMOUT\n");
673
674 for (i = 0; i < so->num_outputs; i++) {
675 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
676 so->output[i].start_component;
677 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
678 i, so->output[i].output_buffer,
679 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
680 so->output[i].register_index,
681 mask & 1 ? "x" : "",
682 mask & 2 ? "y" : "",
683 mask & 4 ? "z" : "",
684 mask & 8 ? "w" : "");
685 }
686}
687
688/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
689 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
690 * or v4i32 (num_channels=3,4). */
691static void build_tbuffer_store(struct si_shader_context *shader,
692 LLVMValueRef rsrc,
693 LLVMValueRef vdata,
694 unsigned num_channels,
695 LLVMValueRef vaddr,
696 LLVMValueRef soffset,
697 unsigned inst_offset,
698 unsigned dfmt,
699 unsigned nfmt,
700 unsigned offen,
701 unsigned idxen,
702 unsigned glc,
703 unsigned slc,
704 unsigned tfe)
705{
706 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
707 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
708 LLVMValueRef args[] = {
709 rsrc,
710 vdata,
711 LLVMConstInt(i32, num_channels, 0),
712 vaddr,
713 soffset,
714 LLVMConstInt(i32, inst_offset, 0),
715 LLVMConstInt(i32, dfmt, 0),
716 LLVMConstInt(i32, nfmt, 0),
717 LLVMConstInt(i32, offen, 0),
718 LLVMConstInt(i32, idxen, 0),
719 LLVMConstInt(i32, glc, 0),
720 LLVMConstInt(i32, slc, 0),
721 LLVMConstInt(i32, tfe, 0)
722 };
723
724 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
725 unsigned func = CLAMP(num_channels, 1, 3) - 1;
726 const char *types[] = {"i32", "v2i32", "v4i32"};
727 char name[256];
728 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
729
730 lp_build_intrinsic(gallivm->builder, name,
731 LLVMVoidTypeInContext(gallivm->context),
732 args, Elements(args));
733}
734
735static void build_streamout_store(struct si_shader_context *shader,
736 LLVMValueRef rsrc,
737 LLVMValueRef vdata,
738 unsigned num_channels,
739 LLVMValueRef vaddr,
740 LLVMValueRef soffset,
741 unsigned inst_offset)
742{
743 static unsigned dfmt[] = {
744 V_008F0C_BUF_DATA_FORMAT_32,
745 V_008F0C_BUF_DATA_FORMAT_32_32,
746 V_008F0C_BUF_DATA_FORMAT_32_32_32,
747 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
748 };
749 assert(num_channels >= 1 && num_channels <= 4);
750
751 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
752 inst_offset, dfmt[num_channels-1],
753 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
754}
755
756/* On SI, the vertex shader is responsible for writing streamout data
757 * to buffers. */
758static void si_llvm_emit_streamout(struct si_shader_context *shader)
759{
760 struct pipe_stream_output_info *so = &shader->shader->selector->so;
761 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
762 LLVMBuilderRef builder = gallivm->builder;
763 int i, j;
764 struct lp_build_if_state if_ctx;
765
766 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
767
768 LLVMValueRef so_param =
769 LLVMGetParam(shader->radeon_bld.main_fn,
770 shader->param_streamout_config);
771
772 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
773 LLVMValueRef so_vtx_count =
774 LLVMBuildAnd(builder,
775 LLVMBuildLShr(builder, so_param,
776 LLVMConstInt(i32, 16, 0), ""),
777 LLVMConstInt(i32, 127, 0), "");
778
779 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
780 NULL, 0, LLVMReadNoneAttribute);
781
782 /* can_emit = tid < so_vtx_count; */
783 LLVMValueRef can_emit =
784 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
785
786 /* Emit the streamout code conditionally. This actually avoids
787 * out-of-bounds buffer access. The hw tells us via the SGPR
788 * (so_vtx_count) which threads are allowed to emit streamout data. */
789 lp_build_if(&if_ctx, gallivm, can_emit);
790 {
791 /* The buffer offset is computed as follows:
792 * ByteOffset = streamout_offset[buffer_id]*4 +
793 * (streamout_write_index + thread_id)*stride[buffer_id] +
794 * attrib_offset
795 */
796
797 LLVMValueRef so_write_index =
798 LLVMGetParam(shader->radeon_bld.main_fn,
799 shader->param_streamout_write_index);
800
801 /* Compute (streamout_write_index + thread_id). */
802 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
803
804 /* Compute the write offset for each enabled buffer. */
805 LLVMValueRef so_write_offset[4] = {};
806 for (i = 0; i < 4; i++) {
807 if (!so->stride[i])
808 continue;
809
810 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
811 shader->param_streamout_offset[i]);
812 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
813
814 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
815 LLVMConstInt(i32, so->stride[i]*4, 0), "");
816 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
817 }
818
819 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS] = shader->radeon_bld.soa.outputs;
820
821 /* Write streamout data. */
822 for (i = 0; i < so->num_outputs; i++) {
823 unsigned buf_idx = so->output[i].output_buffer;
824 unsigned reg = so->output[i].register_index;
825 unsigned start = so->output[i].start_component;
826 unsigned num_comps = so->output[i].num_components;
827 LLVMValueRef out[4];
828
829 assert(num_comps && num_comps <= 4);
830 if (!num_comps || num_comps > 4)
831 continue;
832
833 /* Load the output as int. */
834 for (j = 0; j < num_comps; j++) {
835 out[j] = LLVMBuildLoad(builder, outputs[reg][start+j], "");
836 out[j] = LLVMBuildBitCast(builder, out[j], i32, "");
837 }
838
839 /* Pack the output. */
840 LLVMValueRef vdata = NULL;
841
842 switch (num_comps) {
843 case 1: /* as i32 */
844 vdata = out[0];
845 break;
846 case 2: /* as v2i32 */
847 case 3: /* as v4i32 (aligned to 4) */
848 case 4: /* as v4i32 */
849 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
850 for (j = 0; j < num_comps; j++) {
851 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
852 LLVMConstInt(i32, j, 0), "");
853 }
854 break;
855 }
856
857 build_streamout_store(shader, shader->so_buffers[buf_idx],
858 vdata, num_comps,
859 so_write_offset[buf_idx],
860 LLVMConstInt(i32, 0, 0),
861 so->output[i].dst_offset*4);
862 }
863 }
864 lp_build_endif(&if_ctx);
865}
866
Tom Stellarda75c6162012-01-06 17:38:37 -0500867/* XXX: This is partially implemented for VS only at this point. It is not complete */
868static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
869{
870 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200871 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500872 struct lp_build_context * base = &bld_base->base;
873 struct lp_build_context * uint =
874 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
875 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100876 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500877 LLVMValueRef last_args[9] = { 0 };
Michel Dänzerb00269a2013-08-07 18:14:16 +0200878 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer0afeea52013-05-02 14:53:17 +0200879 unsigned semantic_name;
Christian König35088152012-08-01 22:35:24 +0200880 unsigned color_count = 0;
881 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100882 int depth_index = -1, stencil_index = -1;
Michel Dänzerb00269a2013-08-07 18:14:16 +0200883 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -0500884
Marek Olšák8d03d922013-09-01 23:59:06 +0200885 if (si_shader_ctx->shader->selector->so.num_outputs) {
886 si_llvm_emit_streamout(si_shader_ctx);
887 }
888
Tom Stellarda75c6162012-01-06 17:38:37 -0500889 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500890 struct tgsi_full_declaration *d =
891 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500892 unsigned target;
893 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500894
895 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100896
897 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
898 parse->FullToken.FullProperty.Property.PropertyName ==
899 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
900 shader->fs_write_all = TRUE;
901
Tom Stellarda75c6162012-01-06 17:38:37 -0500902 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
903 continue;
904
905 switch (d->Declaration.File) {
906 case TGSI_FILE_INPUT:
907 i = shader->ninput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200908 assert(i < Elements(shader->input));
Tom Stellarda75c6162012-01-06 17:38:37 -0500909 shader->input[i].name = d->Semantic.Name;
910 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200911 shader->input[i].interpolate = d->Interp.Interpolate;
912 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200913 continue;
914
Tom Stellarda75c6162012-01-06 17:38:37 -0500915 case TGSI_FILE_OUTPUT:
916 i = shader->noutput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200917 assert(i < Elements(shader->output));
Tom Stellarda75c6162012-01-06 17:38:37 -0500918 shader->output[i].name = d->Semantic.Name;
919 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200920 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500921 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500922
Christian König35088152012-08-01 22:35:24 +0200923 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500924 continue;
Christian König35088152012-08-01 22:35:24 +0200925 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500926
Michel Dänzer0afeea52013-05-02 14:53:17 +0200927 semantic_name = d->Semantic.Name;
928handle_semantic:
Tom Stellarda75c6162012-01-06 17:38:37 -0500929 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500930 /* Select the correct target */
Michel Dänzer0afeea52013-05-02 14:53:17 +0200931 switch(semantic_name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400932 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer4730dea2013-05-03 17:59:34 +0200933 shader->vs_out_misc_write = 1;
934 shader->vs_out_point_size = 1;
935 target = V_008DFC_SQ_EXP_POS + 1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500936 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100937 case TGSI_SEMANTIC_POSITION:
938 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
939 target = V_008DFC_SQ_EXP_POS;
940 break;
941 } else {
942 depth_index = index;
943 continue;
944 }
945 case TGSI_SEMANTIC_STENCIL:
946 stencil_index = index;
947 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500948 case TGSI_SEMANTIC_COLOR:
949 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200950 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500951 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200952 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500953 param_count++;
954 } else {
955 target = V_008DFC_SQ_EXP_MRT + color_count;
Marek Olšák6d4755a2013-07-30 22:29:29 +0200956 if (si_shader_ctx->shader->key.ps.alpha_to_one) {
957 si_alpha_to_one(bld_base, index);
958 }
Michel Dänzer7708a862012-11-02 15:57:30 +0100959 if (color_count == 0 &&
Christian Königa0dca442013-03-22 15:59:22 +0100960 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer7708a862012-11-02 15:57:30 +0100961 si_alpha_test(bld_base, index);
962
Tom Stellarda75c6162012-01-06 17:38:37 -0500963 color_count++;
964 }
965 break;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200966 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200967 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
968 (1 << d->Semantic.Index)))
969 continue;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200970 shader->clip_dist_write |=
971 d->Declaration.UsageMask << (d->Semantic.Index << 2);
972 target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index;
973 break;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200974 case TGSI_SEMANTIC_CLIPVERTEX:
Michel Dänzerb00269a2013-08-07 18:14:16 +0200975 si_llvm_emit_clipvertex(bld_base, pos_args, index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200976 continue;
Michel Dänzer30b30372012-09-06 17:53:04 +0200977 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500978 case TGSI_SEMANTIC_GENERIC:
979 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200980 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500981 param_count++;
982 break;
983 default:
984 target = 0;
985 fprintf(stderr,
986 "Warning: SI unhandled output type:%d\n",
Michel Dänzer0afeea52013-05-02 14:53:17 +0200987 semantic_name);
Tom Stellarda75c6162012-01-06 17:38:37 -0500988 }
989
Michel Dänzer26c71392012-08-24 12:03:11 +0200990 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500991
Michel Dänzerb00269a2013-08-07 18:14:16 +0200992 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX &&
993 target >= V_008DFC_SQ_EXP_POS &&
994 target <= (V_008DFC_SQ_EXP_POS + 3)) {
995 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
996 args, sizeof(args));
997 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT &&
998 semantic_name == TGSI_SEMANTIC_COLOR) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500999 if (last_args[0]) {
1000 lp_build_intrinsic(base->gallivm->builder,
1001 "llvm.SI.export",
1002 LLVMVoidTypeInContext(base->gallivm->context),
1003 last_args, 9);
1004 }
1005
1006 memcpy(last_args, args, sizeof(args));
1007 } else {
1008 lp_build_intrinsic(base->gallivm->builder,
1009 "llvm.SI.export",
1010 LLVMVoidTypeInContext(base->gallivm->context),
1011 args, 9);
1012 }
1013
1014 }
Michel Dänzer0afeea52013-05-02 14:53:17 +02001015
1016 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
1017 semantic_name = TGSI_SEMANTIC_GENERIC;
1018 goto handle_semantic;
1019 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001020 }
1021
Michel Dänzer1a616c12012-11-13 17:35:09 +01001022 if (depth_index >= 0 || stencil_index >= 0) {
1023 LLVMValueRef out_ptr;
1024 unsigned mask = 0;
1025
1026 /* Specify the target we are exporting */
1027 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1028
1029 if (depth_index >= 0) {
1030 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1031 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1032 mask |= 0x1;
1033
1034 if (stencil_index < 0) {
1035 args[6] =
1036 args[7] =
1037 args[8] = args[5];
1038 }
1039 }
1040
1041 if (stencil_index >= 0) {
1042 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
1043 args[7] =
1044 args[8] =
1045 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001046 /* Only setting the stencil component bit (0x2) here
1047 * breaks some stencil piglit tests
1048 */
1049 mask |= 0x3;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001050
1051 if (depth_index < 0)
1052 args[5] = args[6];
1053 }
1054
1055 /* Specify which components to enable */
1056 args[0] = lp_build_const_int32(base->gallivm, mask);
1057
1058 args[1] =
1059 args[2] =
1060 args[4] = uint->zero;
1061
1062 if (last_args[0])
1063 lp_build_intrinsic(base->gallivm->builder,
1064 "llvm.SI.export",
1065 LLVMVoidTypeInContext(base->gallivm->context),
1066 args, 9);
1067 else
1068 memcpy(last_args, args, sizeof(args));
1069 }
1070
Michel Dänzerb00269a2013-08-07 18:14:16 +02001071 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
1072 unsigned pos_idx = 0;
Christian Königf18fd252012-07-25 21:58:46 +02001073
Michel Dänzerb00269a2013-08-07 18:14:16 +02001074 for (i = 0; i < 4; i++)
1075 if (pos_args[i][0])
1076 shader->nr_pos_exports++;
Christian Königf18fd252012-07-25 21:58:46 +02001077
Michel Dänzerb00269a2013-08-07 18:14:16 +02001078 for (i = 0; i < 4; i++) {
1079 if (!pos_args[i][0])
1080 continue;
Christian Königf18fd252012-07-25 21:58:46 +02001081
Michel Dänzerc8402702013-02-12 18:37:22 +01001082 /* Specify the target we are exporting */
Michel Dänzerb00269a2013-08-07 18:14:16 +02001083 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1084
1085 if (pos_idx == shader->nr_pos_exports)
1086 /* Specify that this is the last export */
1087 pos_args[i][2] = uint->one;
Michel Dänzerc8402702013-02-12 18:37:22 +01001088
1089 lp_build_intrinsic(base->gallivm->builder,
1090 "llvm.SI.export",
1091 LLVMVoidTypeInContext(base->gallivm->context),
Michel Dänzerb00269a2013-08-07 18:14:16 +02001092 pos_args[i], 9);
1093 }
1094 } else {
1095 if (!last_args[0]) {
1096 /* Specify which components to enable */
1097 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
1098
1099 /* Specify the target we are exporting */
1100 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
1101
1102 /* Set COMPR flag to zero to export data as 32-bit */
1103 last_args[4] = uint->zero;
1104
1105 /* dummy bits */
1106 last_args[5]= uint->zero;
1107 last_args[6]= uint->zero;
1108 last_args[7]= uint->zero;
1109 last_args[8]= uint->zero;
Michel Dänzerc8402702013-02-12 18:37:22 +01001110
1111 si_shader_ctx->shader->spi_shader_col_format |=
Michel Dänzerb00269a2013-08-07 18:14:16 +02001112 V_028714_SPI_SHADER_32_ABGR;
1113 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001114 }
1115
Michel Dänzerb00269a2013-08-07 18:14:16 +02001116 /* Specify whether the EXEC mask represents the valid mask */
1117 last_args[1] = uint->one;
1118
1119 if (shader->fs_write_all && shader->nr_cbufs > 1) {
1120 int i;
1121
1122 /* Specify that this is not yet the last export */
1123 last_args[2] = lp_build_const_int32(base->gallivm, 0);
1124
1125 for (i = 1; i < shader->nr_cbufs; i++) {
1126 /* Specify the target we are exporting */
1127 last_args[3] = lp_build_const_int32(base->gallivm,
1128 V_008DFC_SQ_EXP_MRT + i);
1129
1130 lp_build_intrinsic(base->gallivm->builder,
1131 "llvm.SI.export",
1132 LLVMVoidTypeInContext(base->gallivm->context),
1133 last_args, 9);
1134
1135 si_shader_ctx->shader->spi_shader_col_format |=
1136 si_shader_ctx->shader->spi_shader_col_format << 4;
1137 si_shader_ctx->shader->cb_shader_mask |=
1138 si_shader_ctx->shader->cb_shader_mask << 4;
1139 }
1140
1141 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
1142 }
1143
1144 /* Specify that this is the last export */
1145 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1146
1147 lp_build_intrinsic(base->gallivm->builder,
1148 "llvm.SI.export",
1149 LLVMVoidTypeInContext(base->gallivm->context),
1150 last_args, 9);
Michel Dänzerc8402702013-02-12 18:37:22 +01001151 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001152/* XXX: Look up what this function does */
1153/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
1154}
1155
Marek Olšák4855acd2013-08-06 15:08:54 +02001156static const struct lp_build_tgsi_action txf_action;
1157
1158static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1159 struct lp_build_tgsi_context * bld_base,
1160 struct lp_build_emit_data * emit_data);
1161
Tom Stellarda75c6162012-01-06 17:38:37 -05001162static void tex_fetch_args(
1163 struct lp_build_tgsi_context * bld_base,
1164 struct lp_build_emit_data * emit_data)
1165{
Christian König55fe5cc2013-03-04 16:30:06 +01001166 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001167 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001168 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001169 unsigned opcode = inst->Instruction.Opcode;
1170 unsigned target = inst->Texture.Texture;
Marek Olšák4855acd2013-08-06 15:08:54 +02001171 unsigned sampler_src, sampler_index;
Michel Dänzer120efee2013-01-25 12:10:11 +01001172 LLVMValueRef coords[4];
1173 LLVMValueRef address[16];
Marek Olšák4855acd2013-08-06 15:08:54 +02001174 LLVMValueRef sample_index_rewrite = NULL;
1175 LLVMValueRef sample_chan = NULL;
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001176 int ref_pos;
1177 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001178 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001179 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -04001180
Michel Dänzer120efee2013-01-25 12:10:11 +01001181 /* Fetch and project texture coordinates */
1182 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001183 for (chan = 0; chan < 3; chan++ ) {
1184 coords[chan] = lp_build_emit_fetch(bld_base,
1185 emit_data->inst, 0,
1186 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001187 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001188 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1189 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001190 coords[chan],
1191 coords[3]);
1192 }
1193
Michel Dänzer120efee2013-01-25 12:10:11 +01001194 if (opcode == TGSI_OPCODE_TXP)
1195 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001196
Michel Dänzer120efee2013-01-25 12:10:11 +01001197 /* Pack LOD bias value */
1198 if (opcode == TGSI_OPCODE_TXB)
1199 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001200
Michel Dänzer0495adb2013-05-06 12:45:14 +02001201 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001202 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001203
1204 /* Pack depth comparison value */
1205 switch (target) {
1206 case TGSI_TEXTURE_SHADOW1D:
1207 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1208 case TGSI_TEXTURE_SHADOW2D:
1209 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001210 case TGSI_TEXTURE_SHADOWCUBE:
1211 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001212 assert(ref_pos >= 0);
1213 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001214 break;
1215 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1216 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001217 }
1218
Michel Dänzera6b83c02013-02-21 16:10:55 +01001219 /* Pack user derivatives */
1220 if (opcode == TGSI_OPCODE_TXD) {
1221 for (chan = 0; chan < 2; chan++) {
1222 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1223 if (num_coords > 1)
1224 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1225 }
1226 }
1227
Michel Dänzer120efee2013-01-25 12:10:11 +01001228 /* Pack texture coordinates */
1229 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001230 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001231 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001232 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001233 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001234
Michel Dänzer120efee2013-01-25 12:10:11 +01001235 /* Pack LOD */
Michel Dänzer36231112013-05-02 09:44:45 +02001236 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001237 address[count++] = coords[3];
1238
1239 if (count > 16) {
1240 assert(!"Cannot handle more than 16 texture address parameters");
1241 count = 16;
1242 }
1243
1244 for (chan = 0; chan < count; chan++ ) {
1245 address[chan] = LLVMBuildBitCast(gallivm->builder,
1246 address[chan],
1247 LLVMInt32TypeInContext(gallivm->context),
1248 "");
1249 }
1250
Michel Dänzera6b83c02013-02-21 16:10:55 +01001251 sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
Marek Olšák4855acd2013-08-06 15:08:54 +02001252 sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1253
1254 /* Adjust the sample index according to FMASK.
1255 *
1256 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1257 * which is the identity mapping. Each nibble says which physical sample
1258 * should be fetched to get that sample.
1259 *
1260 * For example, 0x11111100 means there are only 2 samples stored and
1261 * the second sample covers 3/4 of the pixel. When reading samples 0
1262 * and 1, return physical sample 0 (determined by the first two 0s
1263 * in FMASK), otherwise return physical sample 1.
1264 *
1265 * The sample index should be adjusted as follows:
1266 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1267 */
1268 if (target == TGSI_TEXTURE_2D_MSAA ||
1269 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1270 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1271 struct lp_build_emit_data txf_emit_data = *emit_data;
1272 LLVMValueRef txf_address[16];
1273 unsigned txf_count = count;
1274
1275 memcpy(txf_address, address, sizeof(address));
1276
1277 /* Pad to a power-of-two size. */
1278 while (txf_count < util_next_power_of_two(txf_count))
1279 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1280
1281 /* Read FMASK using TXF. */
1282 txf_emit_data.chan = 0;
1283 txf_emit_data.dst_type = LLVMVectorType(
1284 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1285 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1286 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
1287 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm, target);
1288 txf_emit_data.arg_count = 3;
1289
1290 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1291
1292 /* Initialize some constants. */
1293 if (target == TGSI_TEXTURE_2D_MSAA) {
1294 sample_chan = LLVMConstInt(uint_bld->elem_type, 2, 0);
1295 } else {
1296 sample_chan = LLVMConstInt(uint_bld->elem_type, 3, 0);
1297 }
1298
1299 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1300 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1301
1302 /* Apply the formula. */
1303 LLVMValueRef fmask =
1304 LLVMBuildExtractElement(gallivm->builder,
1305 txf_emit_data.output[0],
1306 uint_bld->zero, "");
1307
1308 LLVMValueRef sample_index =
1309 LLVMBuildExtractElement(gallivm->builder,
1310 txf_emit_data.args[0],
1311 sample_chan, "");
1312
1313 LLVMValueRef sample_index4 =
1314 LLVMBuildMul(gallivm->builder, sample_index, four, "");
1315
1316 LLVMValueRef shifted_fmask =
1317 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1318
1319 LLVMValueRef final_sample =
1320 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1321
1322 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1323 * resource descriptor is 0 (invalid),
1324 */
1325 LLVMValueRef fmask_desc =
1326 LLVMBuildBitCast(gallivm->builder,
1327 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1328 LLVMVectorType(uint_bld->elem_type, 8), "");
1329
1330 LLVMValueRef fmask_word1 =
1331 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1332 uint_bld->one, "");
1333
1334 LLVMValueRef word1_is_nonzero =
1335 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1336 fmask_word1, uint_bld->zero, "");
1337
1338 sample_index_rewrite =
1339 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
1340 final_sample, sample_index, "");
1341 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001342
Michel Dänzer36231112013-05-02 09:44:45 +02001343 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001344 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001345
1346 if (opcode == TGSI_OPCODE_TXF) {
1347 /* add tex offsets */
1348 if (inst->Texture.NumOffsets) {
1349 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1350 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1351 const struct tgsi_texture_offset * off = inst->TexOffsets;
1352
1353 assert(inst->Texture.NumOffsets == 1);
1354
1355 address[0] =
1356 lp_build_add(uint_bld, address[0],
1357 bld->immediates[off->Index][off->SwizzleX]);
1358 if (num_coords > 1)
1359 address[1] =
1360 lp_build_add(uint_bld, address[1],
1361 bld->immediates[off->Index][off->SwizzleY]);
1362 if (num_coords > 2)
1363 address[2] =
1364 lp_build_add(uint_bld, address[2],
1365 bld->immediates[off->Index][off->SwizzleZ]);
1366 }
1367
1368 emit_data->dst_type = LLVMVectorType(
1369 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1370 4);
1371
1372 emit_data->arg_count = 3;
1373 } else {
1374 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001375 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001376
1377 emit_data->dst_type = LLVMVectorType(
1378 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1379 4);
1380
1381 emit_data->arg_count = 4;
1382 }
1383
1384 /* Dimensions */
1385 emit_data->args[emit_data->arg_count - 1] =
1386 lp_build_const_int32(bld_base->base.gallivm, target);
1387
Michel Dänzer120efee2013-01-25 12:10:11 +01001388 /* Pad to power of two vector */
1389 while (count < util_next_power_of_two(count))
1390 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1391
Christian Königccf3e8f2013-03-26 15:09:27 +01001392 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Marek Olšák4855acd2013-08-06 15:08:54 +02001393
1394 /* Replace the MSAA sample index if needed. */
1395 if (sample_index_rewrite) {
1396 emit_data->args[0] =
1397 LLVMBuildInsertElement(gallivm->builder, emit_data->args[0],
1398 sample_index_rewrite, sample_chan, "");
1399 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001400}
1401
Michel Dänzer07eddc42013-02-06 15:43:10 +01001402static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1403 struct lp_build_tgsi_context * bld_base,
1404 struct lp_build_emit_data * emit_data)
1405{
1406 struct lp_build_context * base = &bld_base->base;
1407 char intr_name[23];
1408
1409 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001410 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001411
Christian König44e32242013-03-20 12:10:35 +01001412 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001413 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001414 emit_data->args, emit_data->arg_count,
1415 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001416}
1417
Michel Dänzer0495adb2013-05-06 12:45:14 +02001418static void txq_fetch_args(
1419 struct lp_build_tgsi_context * bld_base,
1420 struct lp_build_emit_data * emit_data)
1421{
1422 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1423 const struct tgsi_full_instruction *inst = emit_data->inst;
1424
1425 /* Mip level */
1426 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1427
1428 /* Resource */
1429 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1430
1431 /* Dimensions */
1432 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1433 inst->Texture.Texture);
1434
1435 emit_data->arg_count = 3;
1436
1437 emit_data->dst_type = LLVMVectorType(
1438 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1439 4);
1440}
1441
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001442#if HAVE_LLVM >= 0x0304
1443
1444static void si_llvm_emit_ddxy(
1445 const struct lp_build_tgsi_action * action,
1446 struct lp_build_tgsi_context * bld_base,
1447 struct lp_build_emit_data * emit_data)
1448{
1449 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1450 struct gallivm_state *gallivm = bld_base->base.gallivm;
1451 struct lp_build_context * base = &bld_base->base;
1452 const struct tgsi_full_instruction *inst = emit_data->inst;
1453 unsigned opcode = inst->Instruction.Opcode;
1454 LLVMValueRef indices[2];
1455 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1456 LLVMValueRef tl, trbl, result[4];
1457 LLVMTypeRef i32;
1458 unsigned swizzle[4];
1459 unsigned c;
1460
1461 i32 = LLVMInt32TypeInContext(gallivm->context);
1462
1463 indices[0] = bld_base->uint_bld.zero;
1464 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1465 NULL, 0, LLVMReadNoneAttribute);
1466 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1467 indices, 2, "");
1468
1469 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1470 lp_build_const_int32(gallivm, 0xfffffffc), "");
1471 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1472 indices, 2, "");
1473
1474 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1475 lp_build_const_int32(gallivm,
1476 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1477 "");
1478 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1479 indices, 2, "");
1480
1481 for (c = 0; c < 4; ++c) {
1482 unsigned i;
1483
1484 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1485 for (i = 0; i < c; ++i) {
1486 if (swizzle[i] == swizzle[c]) {
1487 result[c] = result[i];
1488 break;
1489 }
1490 }
1491 if (i != c)
1492 continue;
1493
1494 LLVMBuildStore(gallivm->builder,
1495 LLVMBuildBitCast(gallivm->builder,
1496 lp_build_emit_fetch(bld_base, inst, 0, c),
1497 i32, ""),
1498 store_ptr);
1499
1500 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1501 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1502
1503 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1504 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1505
1506 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1507 }
1508
1509 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1510}
1511
1512#endif /* HAVE_LLVM >= 0x0304 */
1513
Tom Stellarda75c6162012-01-06 17:38:37 -05001514static const struct lp_build_tgsi_action tex_action = {
1515 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001516 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001517 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001518};
1519
Michel Dänzer3e205132012-11-06 17:39:01 +01001520static const struct lp_build_tgsi_action txb_action = {
1521 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001522 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001523 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001524};
1525
Michel Dänzera6b83c02013-02-21 16:10:55 +01001526#if HAVE_LLVM >= 0x0304
1527static const struct lp_build_tgsi_action txd_action = {
1528 .fetch_args = tex_fetch_args,
1529 .emit = build_tex_intrinsic,
1530 .intr_name = "llvm.SI.sampled."
1531};
1532#endif
1533
Michel Dänzer36231112013-05-02 09:44:45 +02001534static const struct lp_build_tgsi_action txf_action = {
1535 .fetch_args = tex_fetch_args,
1536 .emit = build_tex_intrinsic,
1537 .intr_name = "llvm.SI.imageload."
1538};
1539
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001540static const struct lp_build_tgsi_action txl_action = {
1541 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001542 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001543 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001544};
1545
Michel Dänzer0495adb2013-05-06 12:45:14 +02001546static const struct lp_build_tgsi_action txq_action = {
1547 .fetch_args = txq_fetch_args,
1548 .emit = build_tgsi_intrinsic_nomem,
1549 .intr_name = "llvm.SI.resinfo"
1550};
1551
Christian König206f0592013-03-20 14:37:21 +01001552static void create_meta_data(struct si_shader_context *si_shader_ctx)
1553{
1554 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1555 LLVMValueRef args[3];
1556
1557 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1558 args[1] = 0;
1559 args[2] = lp_build_const_int32(gallivm, 1);
1560
1561 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1562}
1563
Christian König55fe5cc2013-03-04 16:30:06 +01001564static void create_function(struct si_shader_context *si_shader_ctx)
1565{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001566 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1567 struct gallivm_state *gallivm = bld_base->base.gallivm;
Christian König0666ffd2013-03-05 15:07:39 +01001568 LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001569 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01001570
Christian König55fe5cc2013-03-04 16:30:06 +01001571 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01001572 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01001573 f32 = LLVMFloatTypeInContext(gallivm->context);
1574 v2i32 = LLVMVectorType(i32, 2);
1575 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01001576
Christian Königf5298b02013-02-28 14:50:07 +01001577 params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
1578 params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
Christian König55fe5cc2013-03-04 16:30:06 +01001579 params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
1580
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001581 switch (si_shader_ctx->type) {
1582 case TGSI_PROCESSOR_VERTEX:
1583 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Marek Olšák2993cca2013-08-18 02:34:23 +02001584 params[SI_PARAM_SO_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01001585 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02001586 num_params = SI_PARAM_START_INSTANCE+1;
1587
1588 /* The locations of the other parameters are assigned dynamically. */
1589
1590 /* Streamout SGPRs. */
1591 if (si_shader_ctx->shader->selector->so.num_outputs) {
1592 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
1593 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
1594 }
1595 /* A streamout buffer offset is loaded if the stride is non-zero. */
1596 for (i = 0; i < 4; i++) {
1597 if (!si_shader_ctx->shader->selector->so.stride[i])
1598 continue;
1599
1600 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
1601 }
1602
1603 last_sgpr = num_params-1;
1604
1605 /* VGPRs */
1606 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
1607 params[num_params++] = i32; /* unused*/
1608 params[num_params++] = i32; /* unused */
1609 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001610 break;
Christian König0666ffd2013-03-05 15:07:39 +01001611
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001612 case TGSI_PROCESSOR_FRAGMENT:
Christian König0666ffd2013-03-05 15:07:39 +01001613 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001614 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01001615 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
1616 params[SI_PARAM_PERSP_CENTER] = v2i32;
1617 params[SI_PARAM_PERSP_CENTROID] = v2i32;
1618 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
1619 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
1620 params[SI_PARAM_LINEAR_CENTER] = v2i32;
1621 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
1622 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
1623 params[SI_PARAM_POS_X_FLOAT] = f32;
1624 params[SI_PARAM_POS_Y_FLOAT] = f32;
1625 params[SI_PARAM_POS_Z_FLOAT] = f32;
1626 params[SI_PARAM_POS_W_FLOAT] = f32;
1627 params[SI_PARAM_FRONT_FACE] = f32;
1628 params[SI_PARAM_ANCILLARY] = f32;
1629 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
1630 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001631 num_params = SI_PARAM_POS_FIXED_PT+1;
1632 break;
1633
1634 default:
1635 assert(0 && "unimplemented shader");
1636 return;
Christian Königc4973212013-03-05 12:14:02 +01001637 }
Christian König55fe5cc2013-03-04 16:30:06 +01001638
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001639 assert(num_params <= Elements(params));
1640 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01001641 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01001642
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001643 for (i = 0; i <= last_sgpr; ++i) {
1644 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Christian Königcf9b31f2013-03-21 18:30:23 +01001645 LLVMAddAttribute(P, LLVMInRegAttribute);
1646 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001647
1648#if HAVE_LLVM >= 0x0304
1649 if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
1650 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0)
1651 si_shader_ctx->ddxy_lds =
1652 LLVMAddGlobalInAddressSpace(gallivm->module,
1653 LLVMArrayType(i32, 64),
1654 "ddxy_lds",
1655 LOCAL_ADDR_SPACE);
1656#endif
Christian König55fe5cc2013-03-04 16:30:06 +01001657}
Tom Stellarda75c6162012-01-06 17:38:37 -05001658
Christian König0f6cf2b2013-03-15 15:53:25 +01001659static void preload_constants(struct si_shader_context *si_shader_ctx)
1660{
1661 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1662 struct gallivm_state * gallivm = bld_base->base.gallivm;
1663 const struct tgsi_shader_info * info = bld_base->info;
1664
1665 unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1;
1666
1667 LLVMValueRef ptr;
1668
1669 if (num_const == 0)
1670 return;
1671
1672 /* Allocate space for the constant values */
1673 si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef));
1674
1675 /* Load the resource descriptor */
1676 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1677 si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
1678
1679 /* Load the constants, we rely on the code sinking to do the rest */
1680 for (i = 0; i < num_const * 4; ++i) {
1681 LLVMValueRef args[2] = {
1682 si_shader_ctx->const_resource,
1683 lp_build_const_int32(gallivm, i * 4)
1684 };
1685 si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1686 bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1687 }
1688}
1689
Christian König1c100182013-03-17 16:02:42 +01001690static void preload_samplers(struct si_shader_context *si_shader_ctx)
1691{
1692 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1693 struct gallivm_state * gallivm = bld_base->base.gallivm;
1694 const struct tgsi_shader_info * info = bld_base->info;
1695
1696 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1697
1698 LLVMValueRef res_ptr, samp_ptr;
1699 LLVMValueRef offset;
1700
1701 if (num_samplers == 0)
1702 return;
1703
1704 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02001705 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01001706 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1707
1708 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1709 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1710
1711 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1712 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01001713 /* Resource */
1714 offset = lp_build_const_int32(gallivm, i);
1715 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1716
1717 /* Sampler */
1718 offset = lp_build_const_int32(gallivm, i);
1719 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02001720
1721 /* FMASK resource */
1722 if (info->is_msaa_sampler[i]) {
1723 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
1724 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
1725 build_indexed_load(si_shader_ctx, res_ptr, offset);
1726 }
Christian König1c100182013-03-17 16:02:42 +01001727 }
1728}
1729
Marek Olšák8d03d922013-09-01 23:59:06 +02001730static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
1731{
1732 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1733 struct gallivm_state * gallivm = bld_base->base.gallivm;
1734 unsigned i;
1735
1736 if (!si_shader_ctx->shader->selector->so.num_outputs)
1737 return;
1738
1739 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1740 SI_PARAM_SO_BUFFER);
1741
1742 /* Load the resources, we rely on the code sinking to do the rest */
1743 for (i = 0; i < 4; ++i) {
1744 if (si_shader_ctx->shader->selector->so.stride[i]) {
1745 LLVMValueRef offset = lp_build_const_int32(gallivm, i);
1746
1747 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
1748 }
1749 }
1750}
1751
Tom Stellard302f53d2012-10-25 13:50:10 -04001752int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader,
1753 LLVMModuleRef mod)
1754{
Tom Stellard302f53d2012-10-25 13:50:10 -04001755 unsigned i;
1756 uint32_t *ptr;
1757 bool dump;
Tom Stellard7782d192013-04-04 09:57:13 -07001758 struct radeon_llvm_binary binary;
Tom Stellard302f53d2012-10-25 13:50:10 -04001759
1760 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
1761
Tom Stellard7782d192013-04-04 09:57:13 -07001762 memset(&binary, 0, sizeof(binary));
1763 radeon_llvm_compile(mod, &binary,
Marek Olšáka81c3e02013-08-14 01:04:39 +02001764 r600_get_llvm_processor_name(rctx->screen->b.family), dump);
Tom Stellard302f53d2012-10-25 13:50:10 -04001765 if (dump) {
1766 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07001767 for (i = 0; i < binary.code_size; i+=4 ) {
1768 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
1769 binary.code[i + 2], binary.code[i + 1],
1770 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04001771 }
1772 }
1773
Tom Stellardd50343d2013-04-04 16:21:06 -04001774 /* XXX: We may be able to emit some of these values directly rather than
1775 * extracting fields to be emitted later.
1776 */
1777 for (i = 0; i < binary.config_size; i+= 8) {
1778 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
1779 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
1780 switch (reg) {
1781 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
1782 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
1783 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
1784 case R_00B848_COMPUTE_PGM_RSRC1:
1785 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
1786 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
1787 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001788 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
1789 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
1790 break;
1791 case R_00B84C_COMPUTE_PGM_RSRC2:
1792 shader->lds_size = G_00B84C_LDS_SIZE(value);
1793 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04001794 case R_0286CC_SPI_PS_INPUT_ENA:
1795 shader->spi_ps_input_ena = value;
1796 break;
1797 default:
1798 fprintf(stderr, "Warning: Compiler emitted unknown "
1799 "config register: 0x%x\n", reg);
1800 break;
1801 }
1802 }
Tom Stellard302f53d2012-10-25 13:50:10 -04001803
1804 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02001805 r600_resource_reference(&shader->bo, NULL);
1806 shader->bo = r600_resource_create_custom(rctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04001807 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001808 if (shader->bo == NULL) {
1809 return -ENOMEM;
1810 }
1811
Marek Olšáka81c3e02013-08-14 01:04:39 +02001812 ptr = (uint32_t*)rctx->b.ws->buffer_map(shader->bo->cs_buf, rctx->b.rings.gfx.cs, PIPE_TRANSFER_WRITE);
Tom Stellard302f53d2012-10-25 13:50:10 -04001813 if (0 /*R600_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04001814 for (i = 0; i < binary.code_size / 4; ++i) {
1815 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04001816 }
1817 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04001818 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001819 }
Marek Olšáka81c3e02013-08-14 01:04:39 +02001820 rctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04001821
Tom Stellard7782d192013-04-04 09:57:13 -07001822 free(binary.code);
1823 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04001824
1825 return 0;
1826}
1827
Tom Stellarda75c6162012-01-06 17:38:37 -05001828int si_pipe_shader_create(
1829 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01001830 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05001831{
1832 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001833 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05001834 struct si_shader_context si_shader_ctx;
1835 struct tgsi_shader_info shader_info;
1836 struct lp_build_tgsi_context * bld_base;
1837 LLVMModuleRef mod;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001838 bool dump;
Tom Stellard302f53d2012-10-25 13:50:10 -04001839 int r = 0;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001840
1841 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -05001842
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001843 assert(shader->shader.noutput == 0);
1844 assert(shader->shader.ninterp == 0);
1845 assert(shader->shader.ninput == 0);
1846
Michel Dänzercfebaf92012-08-31 19:04:08 +02001847 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001848 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1849 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1850
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001851 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001852
Michel Dänzere44dfd42012-11-07 17:33:08 +01001853 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01001854 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001855 bld_base->info = &shader_info;
1856 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001857 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1858
1859 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001860 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01001861#if HAVE_LLVM >= 0x0304
1862 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
1863#endif
Michel Dänzer36231112013-05-02 09:44:45 +02001864 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001865 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001866 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02001867 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001868
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001869#if HAVE_LLVM >= 0x0304
1870 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
1871 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
1872#endif
1873
Tom Stellarda75c6162012-01-06 17:38:37 -05001874 si_shader_ctx.radeon_bld.load_input = declare_input;
Christian Könige4ed5872013-03-21 18:02:52 +01001875 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001876 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001877 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1878 si_shader_ctx.shader = shader;
1879 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05001880
Christian König206f0592013-03-20 14:37:21 +01001881 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01001882 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01001883 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01001884 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02001885 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001886
Christian König835098a2012-07-17 21:28:10 +02001887 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001888
Tom Stellard185fc9a2012-07-12 10:40:47 -04001889 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1890 * conversion fails. */
1891 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001892 tgsi_dump(sel->tokens, 0);
Marek Olšák8d03d922013-09-01 23:59:06 +02001893 si_dump_streamout(&sel->so);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001894 }
1895
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001896 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001897 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Christian König0f6cf2b2013-03-15 15:53:25 +01001898 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001899 FREE(si_shader_ctx.resources);
1900 FREE(si_shader_ctx.samplers);
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001901 return -EINVAL;
1902 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001903
1904 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1905
1906 mod = bld_base->base.gallivm->module;
Tom Stellard302f53d2012-10-25 13:50:10 -04001907 r = si_compile_llvm(rctx, shader, mod);
Tom Stellarda75c6162012-01-06 17:38:37 -05001908
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001909 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001910 tgsi_parse_free(&si_shader_ctx.parse);
1911
Christian König0f6cf2b2013-03-15 15:53:25 +01001912 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001913 FREE(si_shader_ctx.resources);
1914 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05001915
Tom Stellard302f53d2012-10-25 13:50:10 -04001916 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05001917}
1918
1919void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1920{
Marek Olšáka81c3e02013-08-14 01:04:39 +02001921 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001922}