blob: c40795349c7d167d6bed01831aad4578c0efdc9d [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"
36#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040037#include "radeon_llvm_emit.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "tgsi/tgsi_info.h"
39#include "tgsi/tgsi_parse.h"
40#include "tgsi/tgsi_scan.h"
41#include "tgsi/tgsi_dump.h"
42
43#include "radeonsi_pipe.h"
44#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020045#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050046#include "sid.h"
47
48#include <assert.h>
49#include <errno.h>
50#include <stdio.h>
51
Tom Stellarda75c6162012-01-06 17:38:37 -050052struct si_shader_context
53{
54 struct radeon_llvm_context radeon_bld;
55 struct r600_context *rctx;
56 struct tgsi_parse_context parse;
57 struct tgsi_token * tokens;
58 struct si_pipe_shader *shader;
Michel Dänzer44ef0332012-10-05 16:59:10 +020059 struct si_shader_key key;
Tom Stellarda75c6162012-01-06 17:38:37 -050060 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Michel Dänzercfebaf92012-08-31 19:04:08 +020061 unsigned ninput_emitted;
Tom Stellarda75c6162012-01-06 17:38:37 -050062/* struct list_head inputs; */
63/* unsigned * input_mappings *//* From TGSI to SI hw */
64/* struct tgsi_shader_info info;*/
65};
66
67static struct si_shader_context * si_shader_context(
68 struct lp_build_tgsi_context * bld_base)
69{
70 return (struct si_shader_context *)bld_base;
71}
72
73
74#define PERSPECTIVE_BASE 0
75#define LINEAR_BASE 9
76
77#define SAMPLE_OFFSET 0
78#define CENTER_OFFSET 2
79#define CENTROID_OFSET 4
80
81#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040082#define CONST_ADDR_SPACE 2
Tom Stellard89ece082012-05-29 11:36:29 -040083#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050084
Tom Stellard467f5162012-05-16 15:15:35 -040085/**
86 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
87 *
88 * @param offset The offset parameter specifies the number of
89 * elements to offset, not the number of bytes or dwords. An element is the
90 * the type pointed to by the base_ptr parameter (e.g. int is the element of
91 * an int* pointer)
92 *
93 * When LLVM lowers the load instruction, it will convert the element offset
94 * into a dword offset automatically.
95 *
96 */
97static LLVMValueRef build_indexed_load(
98 struct gallivm_state * gallivm,
99 LLVMValueRef base_ptr,
100 LLVMValueRef offset)
101{
102 LLVMValueRef computed_ptr = LLVMBuildGEP(
103 gallivm->builder, base_ptr, &offset, 1, "");
104
105 return LLVMBuildLoad(gallivm->builder, computed_ptr, "");
106}
107
Tom Stellarda75c6162012-01-06 17:38:37 -0500108static void declare_input_vs(
109 struct si_shader_context * si_shader_ctx,
110 unsigned input_index,
111 const struct tgsi_full_declaration *decl)
112{
113 LLVMValueRef t_list_ptr;
114 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400115 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500116 LLVMValueRef attribute_offset;
117 LLVMValueRef buffer_index_reg;
Tom Stellard467f5162012-05-16 15:15:35 -0400118 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500119 LLVMTypeRef vec4_type;
120 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500121 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Christian Königb15e3ae2012-07-25 11:22:59 +0200122 //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index];
Tom Stellarda75c6162012-01-06 17:38:37 -0500123 unsigned chan;
124
Tom Stellard467f5162012-05-16 15:15:35 -0400125 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100126 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500127
Christian Königb15e3ae2012-07-25 11:22:59 +0200128 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400129
130 t_list = build_indexed_load(base->gallivm, t_list_ptr, t_offset);
131
132 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200133 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500134
Christian Königc4973212013-03-05 12:14:02 +0100135 /* Load the buffer index, which is always stored in VGPR0
Tom Stellarda75c6162012-01-06 17:38:37 -0500136 * for Vertex Shaders */
Christian Königc4973212013-03-05 12:14:02 +0100137 buffer_index_reg = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_INDEX);
Tom Stellarda75c6162012-01-06 17:38:37 -0500138
139 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400140 args[0] = t_list;
141 args[1] = attribute_offset;
142 args[2] = buffer_index_reg;
Tom Stellarda75c6162012-01-06 17:38:37 -0500143 input = lp_build_intrinsic(base->gallivm->builder,
Tom Stellard467f5162012-05-16 15:15:35 -0400144 "llvm.SI.vs.load.input", vec4_type, args, 3);
Tom Stellarda75c6162012-01-06 17:38:37 -0500145
146 /* Break up the vec4 into individual components */
147 for (chan = 0; chan < 4; chan++) {
148 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
149 /* XXX: Use a helper function for this. There is one in
150 * tgsi_llvm.c. */
151 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
152 LLVMBuildExtractElement(base->gallivm->builder,
153 input, llvm_chan, "");
154 }
155}
156
157static void declare_input_fs(
158 struct si_shader_context * si_shader_ctx,
159 unsigned input_index,
160 const struct tgsi_full_declaration *decl)
161{
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200162 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500163 struct lp_build_context * base =
164 &si_shader_ctx->radeon_bld.soa.bld_base.base;
165 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400166 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100167 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
168
169 LLVMValueRef interp_param;
170 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500171
172 /* This value is:
173 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
174 * quad begins a new primitive. Bit 0 always needs
175 * to be unset)
176 * [32:16] ParamOffset
177 *
178 */
Christian König55fe5cc2013-03-04 16:30:06 +0100179 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200180 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500181
Christian König0666ffd2013-03-05 15:07:39 +0100182 unsigned chan;
183
Tom Stellard0fb1e682012-09-06 16:18:11 -0400184 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
185 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400186 unsigned soa_index =
187 radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellard0fb1e682012-09-06 16:18:11 -0400188 si_shader_ctx->radeon_bld.inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100189 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100190
191 if (chan == 3)
192 /* RCP for fragcoord.w */
193 si_shader_ctx->radeon_bld.inputs[soa_index] =
194 LLVMBuildFDiv(gallivm->builder,
195 lp_build_const_float(gallivm, 1.0f),
196 si_shader_ctx->radeon_bld.inputs[soa_index],
197 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400198 }
199 return;
200 }
201
Michel Dänzer97078b12012-09-25 12:41:31 +0200202 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
203 LLVMValueRef face, is_face_positive;
204
Christian König0666ffd2013-03-05 15:07:39 +0100205 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
206
Michel Dänzer97078b12012-09-25 12:41:31 +0200207 is_face_positive = LLVMBuildFCmp(gallivm->builder,
208 LLVMRealUGT, face,
209 lp_build_const_float(gallivm, 0.0f),
210 "");
211
212 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
213 LLVMBuildSelect(gallivm->builder,
214 is_face_positive,
215 lp_build_const_float(gallivm, 1.0f),
216 lp_build_const_float(gallivm, 0.0f),
217 "");
218 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
219 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
220 lp_build_const_float(gallivm, 0.0f);
221 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
222 lp_build_const_float(gallivm, 1.0f);
223
224 return;
225 }
226
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200227 shader->input[input_index].param_offset = shader->ninterp++;
228 attr_number = lp_build_const_int32(gallivm,
229 shader->input[input_index].param_offset);
230
Tom Stellarda75c6162012-01-06 17:38:37 -0500231 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200232 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500233 case TGSI_INTERPOLATE_COLOR:
Michel Dänzer18272c92013-02-13 12:54:13 +0100234 if (si_shader_ctx->key.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100235 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200236 } else {
237 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100238 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200239 else
Christian König0666ffd2013-03-05 15:07:39 +0100240 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200241 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500242 break;
243 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100244 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500245 break;
246 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200247 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100248 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200249 else
Christian König0666ffd2013-03-05 15:07:39 +0100250 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200251 break;
252 case TGSI_INTERPOLATE_PERSPECTIVE:
253 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100254 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200255 else
Christian König0666ffd2013-03-05 15:07:39 +0100256 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500257 break;
258 default:
259 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
260 return;
261 }
262
Michel Dänzercfebaf92012-08-31 19:04:08 +0200263 if (!si_shader_ctx->ninput_emitted++) {
264 /* Enable whole quad mode */
265 lp_build_intrinsic(gallivm->builder,
266 "llvm.SI.wqm",
267 LLVMVoidTypeInContext(gallivm->context),
268 NULL, 0);
269 }
270
Christian König0666ffd2013-03-05 15:07:39 +0100271 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
272
Tom Stellarda75c6162012-01-06 17:38:37 -0500273 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200274 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
275 si_shader_ctx->key.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100276 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200277 LLVMValueRef face, is_face_positive;
278 LLVMValueRef back_attr_number =
279 lp_build_const_int32(gallivm,
280 shader->input[input_index].param_offset + 1);
281
Christian König0666ffd2013-03-05 15:07:39 +0100282 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
283
Michel Dänzer691f08d2012-09-06 18:03:38 +0200284 is_face_positive = LLVMBuildFCmp(gallivm->builder,
285 LLVMRealUGT, face,
286 lp_build_const_float(gallivm, 0.0f),
287 "");
288
Tom Stellarda75c6162012-01-06 17:38:37 -0500289 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100290 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200291 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
292 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
293 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
294 LLVMValueRef front, back;
295
296 args[0] = llvm_chan;
297 args[1] = attr_number;
298 front = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100299 input_type, args, args[3] ? 4 : 3,
300 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200301
302 args[1] = back_attr_number;
303 back = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100304 input_type, args, args[3] ? 4 : 3,
305 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200306
307 si_shader_ctx->radeon_bld.inputs[soa_index] =
308 LLVMBuildSelect(gallivm->builder,
309 is_face_positive,
310 front,
311 back,
312 "");
313 }
314
315 shader->ninterp++;
316 } else {
317 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100318 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200319 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
320 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
321 args[0] = llvm_chan;
322 args[1] = attr_number;
323 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100324 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200325 si_shader_ctx->radeon_bld.inputs[soa_index] =
326 build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100327 input_type, args, args[3] ? 4 : 3,
328 LLVMReadOnlyAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200329 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500330 }
331}
332
333static void declare_input(
334 struct radeon_llvm_context * radeon_bld,
335 unsigned input_index,
336 const struct tgsi_full_declaration *decl)
337{
338 struct si_shader_context * si_shader_ctx =
339 si_shader_context(&radeon_bld->soa.bld_base);
340 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
341 declare_input_vs(si_shader_ctx, input_index, decl);
342 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
343 declare_input_fs(si_shader_ctx, input_index, decl);
344 } else {
345 fprintf(stderr, "Warning: Unsupported shader type,\n");
346 }
347}
348
349static LLVMValueRef fetch_constant(
350 struct lp_build_tgsi_context * bld_base,
351 const struct tgsi_full_src_register *reg,
352 enum tgsi_opcode_type type,
353 unsigned swizzle)
354{
Christian König55fe5cc2013-03-04 16:30:06 +0100355 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500356 struct lp_build_context * base = &bld_base->base;
357
Christian Königf5298b02013-02-28 14:50:07 +0100358 LLVMValueRef ptr;
359 LLVMValueRef args[2];
360 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500361
Christian König8514f5a2013-02-04 17:46:42 +0100362 if (swizzle == LP_CHAN_ALL) {
363 unsigned chan;
364 LLVMValueRef values[4];
365 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
366 values[chan] = fetch_constant(bld_base, reg, type, chan);
367
368 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
369 }
370
Christian Königf5298b02013-02-28 14:50:07 +0100371 /* Load the resource descriptor */
372 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
373 args[0] = build_indexed_load(base->gallivm, ptr, bld_base->uint_bld.zero);
374
Christian Könige7723b52012-08-24 12:55:34 +0200375 /* currently not supported */
376 if (reg->Register.Indirect) {
377 assert(0);
Christian Königf5298b02013-02-28 14:50:07 +0100378 result = lp_build_const_int32(base->gallivm, 0);
379 return bitcast(bld_base, type, result);
380 } else
381 args[1] = lp_build_const_int32(base->gallivm, (reg->Register.Index * 4 + swizzle) * 4);
Christian Könige7723b52012-08-24 12:55:34 +0200382
Christian Königf5298b02013-02-28 14:50:07 +0100383 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
384 args, 2, LLVMReadOnlyAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500385
Christian Königf5298b02013-02-28 14:50:07 +0100386 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500387}
388
Michel Dänzer26c71392012-08-24 12:03:11 +0200389/* Initialize arguments for the shader export intrinsic */
390static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
391 struct tgsi_full_declaration *d,
392 unsigned index,
393 unsigned target,
394 LLVMValueRef *args)
395{
396 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
397 struct lp_build_context *uint =
398 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
399 struct lp_build_context *base = &bld_base->base;
400 unsigned compressed = 0;
401 unsigned chan;
402
Michel Dänzerf402acd2012-08-22 18:15:36 +0200403 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
404 int cbuf = target - V_008DFC_SQ_EXP_MRT;
405
406 if (cbuf >= 0 && cbuf < 8) {
Michel Dänzer44ef0332012-10-05 16:59:10 +0200407 compressed = (si_shader_ctx->key.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100408
409 if (compressed)
410 si_shader_ctx->shader->spi_shader_col_format |=
411 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
412 else
413 si_shader_ctx->shader->spi_shader_col_format |=
414 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200415 }
416 }
417
418 if (compressed) {
419 /* Pixel shader needs to pack output values before export */
420 for (chan = 0; chan < 2; chan++ ) {
421 LLVMValueRef *out_ptr =
422 si_shader_ctx->radeon_bld.soa.outputs[index];
423 args[0] = LLVMBuildLoad(base->gallivm->builder,
424 out_ptr[2 * chan], "");
425 args[1] = LLVMBuildLoad(base->gallivm->builder,
426 out_ptr[2 * chan + 1], "");
427 args[chan + 5] =
428 build_intrinsic(base->gallivm->builder,
429 "llvm.SI.packf16",
430 LLVMInt32TypeInContext(base->gallivm->context),
431 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100432 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100433 args[chan + 7] = args[chan + 5] =
434 LLVMBuildBitCast(base->gallivm->builder,
435 args[chan + 5],
436 LLVMFloatTypeInContext(base->gallivm->context),
437 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200438 }
439
440 /* Set COMPR flag */
441 args[4] = uint->one;
442 } else {
443 for (chan = 0; chan < 4; chan++ ) {
444 LLVMValueRef out_ptr =
445 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
446 /* +5 because the first output value will be
447 * the 6th argument to the intrinsic. */
448 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
449 out_ptr, "");
450 }
451
452 /* Clear COMPR flag */
453 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200454 }
455
456 /* XXX: This controls which components of the output
457 * registers actually get exported. (e.g bit 0 means export
458 * X component, bit 1 means export Y component, etc.) I'm
459 * hard coding this to 0xf for now. In the future, we might
460 * want to do something else. */
461 args[0] = lp_build_const_int32(base->gallivm, 0xf);
462
463 /* Specify whether the EXEC mask represents the valid mask */
464 args[1] = uint->zero;
465
466 /* Specify whether this is the last export */
467 args[2] = uint->zero;
468
469 /* Specify the target we are exporting */
470 args[3] = lp_build_const_int32(base->gallivm, target);
471
Michel Dänzer26c71392012-08-24 12:03:11 +0200472 /* XXX: We probably need to keep track of the output
473 * values, so we know what we are passing to the next
474 * stage. */
475}
476
Michel Dänzer7708a862012-11-02 15:57:30 +0100477static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
478 unsigned index)
479{
480 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
481 struct gallivm_state *gallivm = bld_base->base.gallivm;
482
483 if (si_shader_ctx->key.alpha_func != PIPE_FUNC_NEVER) {
484 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
485 LLVMValueRef alpha_pass =
486 lp_build_cmp(&bld_base->base,
487 si_shader_ctx->key.alpha_func,
488 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
489 lp_build_const_float(gallivm, si_shader_ctx->key.alpha_ref));
490 LLVMValueRef arg =
491 lp_build_select(&bld_base->base,
492 alpha_pass,
493 lp_build_const_float(gallivm, 1.0f),
494 lp_build_const_float(gallivm, -1.0f));
495
496 build_intrinsic(gallivm->builder,
497 "llvm.AMDGPU.kill",
498 LLVMVoidTypeInContext(gallivm->context),
499 &arg, 1, 0);
500 } else {
501 build_intrinsic(gallivm->builder,
502 "llvm.AMDGPU.kilp",
503 LLVMVoidTypeInContext(gallivm->context),
504 NULL, 0, 0);
505 }
506}
507
Tom Stellarda75c6162012-01-06 17:38:37 -0500508/* XXX: This is partially implemented for VS only at this point. It is not complete */
509static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
510{
511 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200512 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500513 struct lp_build_context * base = &bld_base->base;
514 struct lp_build_context * uint =
515 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
516 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100517 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500518 LLVMValueRef last_args[9] = { 0 };
Christian König35088152012-08-01 22:35:24 +0200519 unsigned color_count = 0;
520 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100521 int depth_index = -1, stencil_index = -1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500522
523 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500524 struct tgsi_full_declaration *d =
525 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500526 unsigned target;
527 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500528 int i;
529
530 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100531
532 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
533 parse->FullToken.FullProperty.Property.PropertyName ==
534 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
535 shader->fs_write_all = TRUE;
536
Tom Stellarda75c6162012-01-06 17:38:37 -0500537 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
538 continue;
539
540 switch (d->Declaration.File) {
541 case TGSI_FILE_INPUT:
542 i = shader->ninput++;
543 shader->input[i].name = d->Semantic.Name;
544 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200545 shader->input[i].interpolate = d->Interp.Interpolate;
546 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200547 continue;
548
Tom Stellarda75c6162012-01-06 17:38:37 -0500549 case TGSI_FILE_OUTPUT:
550 i = shader->noutput++;
551 shader->output[i].name = d->Semantic.Name;
552 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200553 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500554 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500555
Christian König35088152012-08-01 22:35:24 +0200556 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500557 continue;
Christian König35088152012-08-01 22:35:24 +0200558 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500559
560 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500561 /* Select the correct target */
562 switch(d->Semantic.Name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400563 case TGSI_SEMANTIC_PSIZE:
Tom Stellarda75c6162012-01-06 17:38:37 -0500564 target = V_008DFC_SQ_EXP_POS;
565 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100566 case TGSI_SEMANTIC_POSITION:
567 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
568 target = V_008DFC_SQ_EXP_POS;
569 break;
570 } else {
571 depth_index = index;
572 continue;
573 }
574 case TGSI_SEMANTIC_STENCIL:
575 stencil_index = index;
576 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500577 case TGSI_SEMANTIC_COLOR:
578 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200579 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500580 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200581 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500582 param_count++;
583 } else {
584 target = V_008DFC_SQ_EXP_MRT + color_count;
Michel Dänzer7708a862012-11-02 15:57:30 +0100585 if (color_count == 0 &&
586 si_shader_ctx->key.alpha_func != PIPE_FUNC_ALWAYS)
587 si_alpha_test(bld_base, index);
588
Tom Stellarda75c6162012-01-06 17:38:37 -0500589 color_count++;
590 }
591 break;
Michel Dänzer30b30372012-09-06 17:53:04 +0200592 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500593 case TGSI_SEMANTIC_GENERIC:
594 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200595 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500596 param_count++;
597 break;
598 default:
599 target = 0;
600 fprintf(stderr,
601 "Warning: SI unhandled output type:%d\n",
602 d->Semantic.Name);
603 }
604
Michel Dänzer26c71392012-08-24 12:03:11 +0200605 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500606
607 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ?
608 (d->Semantic.Name == TGSI_SEMANTIC_POSITION) :
609 (d->Semantic.Name == TGSI_SEMANTIC_COLOR)) {
610 if (last_args[0]) {
611 lp_build_intrinsic(base->gallivm->builder,
612 "llvm.SI.export",
613 LLVMVoidTypeInContext(base->gallivm->context),
614 last_args, 9);
615 }
616
617 memcpy(last_args, args, sizeof(args));
618 } else {
619 lp_build_intrinsic(base->gallivm->builder,
620 "llvm.SI.export",
621 LLVMVoidTypeInContext(base->gallivm->context),
622 args, 9);
623 }
624
625 }
626 }
627
Michel Dänzer1a616c12012-11-13 17:35:09 +0100628 if (depth_index >= 0 || stencil_index >= 0) {
629 LLVMValueRef out_ptr;
630 unsigned mask = 0;
631
632 /* Specify the target we are exporting */
633 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
634
635 if (depth_index >= 0) {
636 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
637 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
638 mask |= 0x1;
639
640 if (stencil_index < 0) {
641 args[6] =
642 args[7] =
643 args[8] = args[5];
644 }
645 }
646
647 if (stencil_index >= 0) {
648 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
649 args[7] =
650 args[8] =
651 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
652 mask |= 0x2;
653
654 if (depth_index < 0)
655 args[5] = args[6];
656 }
657
658 /* Specify which components to enable */
659 args[0] = lp_build_const_int32(base->gallivm, mask);
660
661 args[1] =
662 args[2] =
663 args[4] = uint->zero;
664
665 if (last_args[0])
666 lp_build_intrinsic(base->gallivm->builder,
667 "llvm.SI.export",
668 LLVMVoidTypeInContext(base->gallivm->context),
669 args, 9);
670 else
671 memcpy(last_args, args, sizeof(args));
672 }
673
Christian Königf18fd252012-07-25 21:58:46 +0200674 if (!last_args[0]) {
675 assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
676
677 /* Specify which components to enable */
678 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
679
680 /* Specify the target we are exporting */
681 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
682
683 /* Set COMPR flag to zero to export data as 32-bit */
684 last_args[4] = uint->zero;
685
686 /* dummy bits */
687 last_args[5]= uint->zero;
688 last_args[6]= uint->zero;
689 last_args[7]= uint->zero;
690 last_args[8]= uint->zero;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100691
692 si_shader_ctx->shader->spi_shader_col_format |=
693 V_028714_SPI_SHADER_32_ABGR;
Christian Königf18fd252012-07-25 21:58:46 +0200694 }
695
Tom Stellarda75c6162012-01-06 17:38:37 -0500696 /* Specify whether the EXEC mask represents the valid mask */
697 last_args[1] = lp_build_const_int32(base->gallivm,
698 si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
699
Michel Dänzerc8402702013-02-12 18:37:22 +0100700 if (shader->fs_write_all && shader->nr_cbufs > 1) {
701 int i;
702
703 /* Specify that this is not yet the last export */
704 last_args[2] = lp_build_const_int32(base->gallivm, 0);
705
706 for (i = 1; i < shader->nr_cbufs; i++) {
707 /* Specify the target we are exporting */
708 last_args[3] = lp_build_const_int32(base->gallivm,
709 V_008DFC_SQ_EXP_MRT + i);
710
711 lp_build_intrinsic(base->gallivm->builder,
712 "llvm.SI.export",
713 LLVMVoidTypeInContext(base->gallivm->context),
714 last_args, 9);
715
716 si_shader_ctx->shader->spi_shader_col_format |=
717 si_shader_ctx->shader->spi_shader_col_format << 4;
718 }
719
720 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
721 }
722
Tom Stellarda75c6162012-01-06 17:38:37 -0500723 /* Specify that this is the last export */
724 last_args[2] = lp_build_const_int32(base->gallivm, 1);
725
726 lp_build_intrinsic(base->gallivm->builder,
727 "llvm.SI.export",
728 LLVMVoidTypeInContext(base->gallivm->context),
729 last_args, 9);
730
731/* XXX: Look up what this function does */
732/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
733}
734
735static void tex_fetch_args(
736 struct lp_build_tgsi_context * bld_base,
737 struct lp_build_emit_data * emit_data)
738{
Christian König55fe5cc2013-03-04 16:30:06 +0100739 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100740 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200741 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100742 unsigned opcode = inst->Instruction.Opcode;
743 unsigned target = inst->Texture.Texture;
Tom Stellard467f5162012-05-16 15:15:35 -0400744 LLVMValueRef ptr;
745 LLVMValueRef offset;
Michel Dänzer120efee2013-01-25 12:10:11 +0100746 LLVMValueRef coords[4];
747 LLVMValueRef address[16];
748 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100749 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400750
Tom Stellarda75c6162012-01-06 17:38:37 -0500751 /* WriteMask */
Christian König250b7fd2012-08-01 23:18:14 +0200752 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
753 emit_data->args[0] = lp_build_const_int32(bld_base->base.gallivm, 0xf);
Tom Stellarda75c6162012-01-06 17:38:37 -0500754
Michel Dänzer120efee2013-01-25 12:10:11 +0100755 /* Fetch and project texture coordinates */
756 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100757 for (chan = 0; chan < 3; chan++ ) {
758 coords[chan] = lp_build_emit_fetch(bld_base,
759 emit_data->inst, 0,
760 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100761 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200762 coords[chan] = lp_build_emit_llvm_binary(bld_base,
763 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100764 coords[chan],
765 coords[3]);
766 }
767
Michel Dänzer120efee2013-01-25 12:10:11 +0100768 if (opcode == TGSI_OPCODE_TXP)
769 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500770
Michel Dänzer120efee2013-01-25 12:10:11 +0100771 /* Pack LOD bias value */
772 if (opcode == TGSI_OPCODE_TXB)
773 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400774
Michel Dänzer120efee2013-01-25 12:10:11 +0100775 if ((target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE) &&
776 opcode != TGSI_OPCODE_TXQ)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100777 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100778
779 /* Pack depth comparison value */
780 switch (target) {
781 case TGSI_TEXTURE_SHADOW1D:
782 case TGSI_TEXTURE_SHADOW1D_ARRAY:
783 case TGSI_TEXTURE_SHADOW2D:
784 case TGSI_TEXTURE_SHADOWRECT:
785 address[count++] = coords[2];
786 break;
787 case TGSI_TEXTURE_SHADOWCUBE:
788 case TGSI_TEXTURE_SHADOW2D_ARRAY:
789 address[count++] = coords[3];
790 break;
791 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
792 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +0100793 }
794
Michel Dänzer120efee2013-01-25 12:10:11 +0100795 /* Pack texture coordinates */
796 address[count++] = coords[0];
797 switch (target) {
798 case TGSI_TEXTURE_2D:
799 case TGSI_TEXTURE_2D_ARRAY:
800 case TGSI_TEXTURE_3D:
801 case TGSI_TEXTURE_CUBE:
802 case TGSI_TEXTURE_RECT:
803 case TGSI_TEXTURE_SHADOW2D:
804 case TGSI_TEXTURE_SHADOWRECT:
805 case TGSI_TEXTURE_SHADOW2D_ARRAY:
806 case TGSI_TEXTURE_SHADOWCUBE:
807 case TGSI_TEXTURE_2D_MSAA:
808 case TGSI_TEXTURE_2D_ARRAY_MSAA:
809 case TGSI_TEXTURE_CUBE_ARRAY:
810 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
811 address[count++] = coords[1];
812 }
813 switch (target) {
814 case TGSI_TEXTURE_3D:
815 case TGSI_TEXTURE_CUBE:
816 case TGSI_TEXTURE_SHADOWCUBE:
817 case TGSI_TEXTURE_CUBE_ARRAY:
818 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
819 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +0100820 }
821
Michel Dänzer120efee2013-01-25 12:10:11 +0100822 /* Pack array slice */
823 switch (target) {
824 case TGSI_TEXTURE_1D_ARRAY:
825 address[count++] = coords[1];
826 }
827 switch (target) {
828 case TGSI_TEXTURE_2D_ARRAY:
829 case TGSI_TEXTURE_2D_ARRAY_MSAA:
830 case TGSI_TEXTURE_SHADOW2D_ARRAY:
831 address[count++] = coords[2];
832 }
833 switch (target) {
834 case TGSI_TEXTURE_CUBE_ARRAY:
835 case TGSI_TEXTURE_SHADOW1D_ARRAY:
836 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
837 address[count++] = coords[3];
838 }
839
840 /* Pack LOD */
841 if (opcode == TGSI_OPCODE_TXL)
842 address[count++] = coords[3];
843
844 if (count > 16) {
845 assert(!"Cannot handle more than 16 texture address parameters");
846 count = 16;
847 }
848
849 for (chan = 0; chan < count; chan++ ) {
850 address[chan] = LLVMBuildBitCast(gallivm->builder,
851 address[chan],
852 LLVMInt32TypeInContext(gallivm->context),
853 "");
854 }
855
856 /* Pad to power of two vector */
857 while (count < util_next_power_of_two(count))
858 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
859
Michel Dänzer120efee2013-01-25 12:10:11 +0100860 emit_data->args[1] = lp_build_gather_values(gallivm, address, count);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100861
Tom Stellarda75c6162012-01-06 17:38:37 -0500862 /* Resource */
Christian König55fe5cc2013-03-04 16:30:06 +0100863 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
Tom Stellard467f5162012-05-16 15:15:35 -0400864 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200865 emit_data->inst->Src[1].Register.Index);
Tom Stellard467f5162012-05-16 15:15:35 -0400866 emit_data->args[2] = build_indexed_load(bld_base->base.gallivm,
867 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500868
869 /* Sampler */
Christian König55fe5cc2013-03-04 16:30:06 +0100870 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
Tom Stellard467f5162012-05-16 15:15:35 -0400871 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200872 emit_data->inst->Src[1].Register.Index);
Tom Stellard467f5162012-05-16 15:15:35 -0400873 emit_data->args[3] = build_indexed_load(bld_base->base.gallivm,
874 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500875
876 /* Dimensions */
Michel Dänzer120efee2013-01-25 12:10:11 +0100877 emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm, target);
Tom Stellarda75c6162012-01-06 17:38:37 -0500878
Michel Dänzer6eb0d3d2012-11-30 11:38:24 +0100879 emit_data->arg_count = 5;
Tom Stellarda75c6162012-01-06 17:38:37 -0500880 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
881 * the writemask are clear */
882 emit_data->dst_type = LLVMVectorType(
883 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
884 4);
885}
886
Michel Dänzer07eddc42013-02-06 15:43:10 +0100887static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
888 struct lp_build_tgsi_context * bld_base,
889 struct lp_build_emit_data * emit_data)
890{
891 struct lp_build_context * base = &bld_base->base;
892 char intr_name[23];
893
894 sprintf(intr_name, "%sv%ui32", action->intr_name,
895 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[1])));
896
897 emit_data->output[emit_data->chan] = lp_build_intrinsic(
898 base->gallivm->builder, intr_name, emit_data->dst_type,
899 emit_data->args, emit_data->arg_count);
900}
901
Tom Stellarda75c6162012-01-06 17:38:37 -0500902static const struct lp_build_tgsi_action tex_action = {
903 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +0100904 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100905 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -0500906};
907
Michel Dänzer3e205132012-11-06 17:39:01 +0100908static const struct lp_build_tgsi_action txb_action = {
909 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +0100910 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100911 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +0100912};
913
Michel Dänzer56ae9be2012-11-06 17:41:50 +0100914static const struct lp_build_tgsi_action txl_action = {
915 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +0100916 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100917 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +0100918};
919
Christian König55fe5cc2013-03-04 16:30:06 +0100920static void create_function(struct si_shader_context *si_shader_ctx)
921{
922 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
Christian König0666ffd2013-03-05 15:07:39 +0100923 LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
Christian König55fe5cc2013-03-04 16:30:06 +0100924 unsigned i;
925
Christian König55fe5cc2013-03-04 16:30:06 +0100926 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +0100927 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100928 f32 = LLVMFloatTypeInContext(gallivm->context);
929 v2i32 = LLVMVectorType(i32, 2);
930 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +0100931
Christian Königf5298b02013-02-28 14:50:07 +0100932 params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
933 params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
Christian König55fe5cc2013-03-04 16:30:06 +0100934 params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
935
Christian Königc4973212013-03-05 12:14:02 +0100936 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Christian König55fe5cc2013-03-04 16:30:06 +0100937 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER];
Christian Königc4973212013-03-05 12:14:02 +0100938 params[SI_PARAM_VERTEX_INDEX] = i32;
939 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 5);
Christian König0666ffd2013-03-05 15:07:39 +0100940
Christian Königc4973212013-03-05 12:14:02 +0100941 } else {
Christian König0666ffd2013-03-05 15:07:39 +0100942 params[SI_PARAM_PRIM_MASK] = i32;
943 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
944 params[SI_PARAM_PERSP_CENTER] = v2i32;
945 params[SI_PARAM_PERSP_CENTROID] = v2i32;
946 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
947 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
948 params[SI_PARAM_LINEAR_CENTER] = v2i32;
949 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
950 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
951 params[SI_PARAM_POS_X_FLOAT] = f32;
952 params[SI_PARAM_POS_Y_FLOAT] = f32;
953 params[SI_PARAM_POS_Z_FLOAT] = f32;
954 params[SI_PARAM_POS_W_FLOAT] = f32;
955 params[SI_PARAM_FRONT_FACE] = f32;
956 params[SI_PARAM_ANCILLARY] = f32;
957 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
958 params[SI_PARAM_POS_FIXED_PT] = f32;
959 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20);
Christian Königc4973212013-03-05 12:14:02 +0100960 }
Christian König55fe5cc2013-03-04 16:30:06 +0100961
962 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
963 for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) {
964 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
965 LLVMAddAttribute(P, LLVMInRegAttribute);
966 }
967}
Tom Stellarda75c6162012-01-06 17:38:37 -0500968
969int si_pipe_shader_create(
970 struct pipe_context *ctx,
Michel Dänzer44ef0332012-10-05 16:59:10 +0200971 struct si_pipe_shader *shader,
972 struct si_shader_key key)
Tom Stellarda75c6162012-01-06 17:38:37 -0500973{
974 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200975 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -0500976 struct si_shader_context si_shader_ctx;
977 struct tgsi_shader_info shader_info;
978 struct lp_build_tgsi_context * bld_base;
979 LLVMModuleRef mod;
980 unsigned char * inst_bytes;
981 unsigned inst_byte_count;
982 unsigned i;
Christian Königd51b9b72012-07-24 18:50:49 +0200983 uint32_t *ptr;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200984 bool dump;
985
986 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -0500987
Michel Dänzer82e38ac2012-09-27 16:39:26 +0200988 assert(shader->shader.noutput == 0);
989 assert(shader->shader.ninterp == 0);
990 assert(shader->shader.ninput == 0);
991
Michel Dänzercfebaf92012-08-31 19:04:08 +0200992 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -0500993 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
994 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
995
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200996 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzer35f0dc22013-01-22 17:08:24 +0100997 if (shader_info.indirect_files != 0) {
998 fprintf(stderr, "Indirect addressing not fully handled yet\n");
999 return -ENOSYS;
1000 }
1001
Michel Dänzere44dfd42012-11-07 17:33:08 +01001002 shader->shader.uses_kill = shader_info.uses_kill;
Tom Stellarda75c6162012-01-06 17:38:37 -05001003 bld_base->info = &shader_info;
1004 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001005 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1006
1007 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001008 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001009 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001010 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001011
1012 si_shader_ctx.radeon_bld.load_input = declare_input;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001013 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001014 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1015 si_shader_ctx.shader = shader;
Michel Dänzer44ef0332012-10-05 16:59:10 +02001016 si_shader_ctx.key = key;
Tom Stellarda75c6162012-01-06 17:38:37 -05001017 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
1018 si_shader_ctx.rctx = rctx;
1019
Christian König55fe5cc2013-03-04 16:30:06 +01001020 create_function(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001021
Christian König835098a2012-07-17 21:28:10 +02001022 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001023
Tom Stellard185fc9a2012-07-12 10:40:47 -04001024 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1025 * conversion fails. */
1026 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001027 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001028 }
1029
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001030 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001031 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
1032 return -EINVAL;
1033 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001034
1035 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1036
1037 mod = bld_base->base.gallivm->module;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001038 if (dump) {
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001039 LLVMDumpModule(mod);
1040 }
1041 radeon_llvm_compile(mod, &inst_bytes, &inst_byte_count, "SI", dump);
1042 if (dump) {
1043 fprintf(stderr, "SI CODE:\n");
1044 for (i = 0; i < inst_byte_count; i+=4 ) {
1045 fprintf(stderr, "%02x%02x%02x%02x\n", inst_bytes[i + 3],
1046 inst_bytes[i + 2], inst_bytes[i + 1],
1047 inst_bytes[i]);
1048 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001049 }
1050
1051 shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)inst_bytes);
1052 shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 4));
1053 shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 8));
1054
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001055 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001056 tgsi_parse_free(&si_shader_ctx.parse);
1057
1058 /* copy new shader */
Christian Königd51b9b72012-07-24 18:50:49 +02001059 si_resource_reference(&shader->bo, NULL);
1060 shader->bo = si_resource_create_custom(ctx->screen, PIPE_USAGE_IMMUTABLE,
1061 inst_byte_count - 12);
Tom Stellarda75c6162012-01-06 17:38:37 -05001062 if (shader->bo == NULL) {
Christian Königd51b9b72012-07-24 18:50:49 +02001063 return -ENOMEM;
Tom Stellarda75c6162012-01-06 17:38:37 -05001064 }
1065
Christian Königd51b9b72012-07-24 18:50:49 +02001066 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
1067 if (0 /*R600_BIG_ENDIAN*/) {
1068 for (i = 0; i < (inst_byte_count-12)/4; ++i) {
1069 ptr[i] = util_bswap32(*(uint32_t*)(inst_bytes+12 + i*4));
1070 }
1071 } else {
1072 memcpy(ptr, inst_bytes + 12, inst_byte_count - 12);
1073 }
1074 rctx->ws->buffer_unmap(shader->bo->cs_buf);
1075
Tom Stellarda75c6162012-01-06 17:38:37 -05001076 free(inst_bytes);
1077
1078 return 0;
1079}
1080
1081void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1082{
Christian Königfe412872012-07-24 18:47:19 +02001083 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001084}