blob: 7f3d38b3643d5f855606667210a92582d6d80597 [file] [log] [blame]
Tom Stellarda75c6162012-01-06 17:38:37 -05001
Christian Königce40e472012-08-02 12:14:59 +02002/*
3 * Copyright 2012 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Tom Stellard <thomas.stellard@amd.com>
26 * Michel Dänzer <michel.daenzer@amd.com>
27 * Christian König <christian.koenig@amd.com>
28 */
29
Tom Stellarda75c6162012-01-06 17:38:37 -050030#include "gallivm/lp_bld_tgsi_action.h"
31#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020032#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050033#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010034#include "gallivm/lp_bld_logic.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050035#include "gallivm/lp_bld_tgsi.h"
Christian König5e616cf2013-03-07 11:58:56 +010036#include "gallivm/lp_bld_arit.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050037#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040038#include "radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010039#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_info.h"
41#include "tgsi/tgsi_parse.h"
42#include "tgsi/tgsi_scan.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010043#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050044#include "tgsi/tgsi_dump.h"
45
46#include "radeonsi_pipe.h"
47#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020048#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050049#include "sid.h"
50
51#include <assert.h>
52#include <errno.h>
53#include <stdio.h>
54
Tom Stellarda75c6162012-01-06 17:38:37 -050055struct si_shader_context
56{
57 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050058 struct tgsi_parse_context parse;
59 struct tgsi_token * tokens;
60 struct si_pipe_shader *shader;
61 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Christian König206f0592013-03-20 14:37:21 +010062 LLVMValueRef const_md;
Christian König0f6cf2b2013-03-15 15:53:25 +010063 LLVMValueRef const_resource;
64 LLVMValueRef *constants;
Christian König1c100182013-03-17 16:02:42 +010065 LLVMValueRef *resources;
66 LLVMValueRef *samplers;
Tom Stellarda75c6162012-01-06 17:38:37 -050067};
68
69static struct si_shader_context * si_shader_context(
70 struct lp_build_tgsi_context * bld_base)
71{
72 return (struct si_shader_context *)bld_base;
73}
74
75
76#define PERSPECTIVE_BASE 0
77#define LINEAR_BASE 9
78
79#define SAMPLE_OFFSET 0
80#define CENTER_OFFSET 2
81#define CENTROID_OFSET 4
82
83#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040084#define CONST_ADDR_SPACE 2
Tom Stellard89ece082012-05-29 11:36:29 -040085#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050086
Tom Stellard467f5162012-05-16 15:15:35 -040087/**
88 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
89 *
90 * @param offset The offset parameter specifies the number of
91 * elements to offset, not the number of bytes or dwords. An element is the
92 * the type pointed to by the base_ptr parameter (e.g. int is the element of
93 * an int* pointer)
94 *
95 * When LLVM lowers the load instruction, it will convert the element offset
96 * into a dword offset automatically.
97 *
98 */
99static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100100 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400101 LLVMValueRef base_ptr,
102 LLVMValueRef offset)
103{
Christian König206f0592013-03-20 14:37:21 +0100104 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400105
Christian König206f0592013-03-20 14:37:21 +0100106 LLVMValueRef computed_ptr = LLVMBuildGEP(
107 base->gallivm->builder, base_ptr, &offset, 1, "");
108
109 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
110 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
111 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400112}
113
Christian Königa0dca442013-03-22 15:59:22 +0100114static LLVMValueRef get_instance_index(
115 struct radeon_llvm_context * radeon_bld,
116 unsigned divisor)
117{
118 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
119
120 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_INSTANCE_ID);
121 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
122 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
123
124 if (divisor > 1)
125 result = LLVMBuildUDiv(gallivm->builder, result,
126 lp_build_const_int32(gallivm, divisor), "");
127
128 return result;
129}
130
Tom Stellarda75c6162012-01-06 17:38:37 -0500131static void declare_input_vs(
132 struct si_shader_context * si_shader_ctx,
133 unsigned input_index,
134 const struct tgsi_full_declaration *decl)
135{
Christian Königa0dca442013-03-22 15:59:22 +0100136 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
137 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
138
139 unsigned chan;
140
Tom Stellarda75c6162012-01-06 17:38:37 -0500141 LLVMValueRef t_list_ptr;
142 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400143 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500144 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100145 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400146 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500147 LLVMTypeRef vec4_type;
148 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500149
Tom Stellard467f5162012-05-16 15:15:35 -0400150 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100151 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500152
Christian Königb15e3ae2012-07-25 11:22:59 +0200153 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400154
Christian König206f0592013-03-20 14:37:21 +0100155 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400156
157 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200158 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500159
Christian Königa0dca442013-03-22 15:59:22 +0100160 if (divisor) {
161 /* Build index from instance ID, start instance and divisor */
162 si_shader_ctx->shader->shader.uses_instanceid = true;
163 buffer_index = get_instance_index(&si_shader_ctx->radeon_bld, divisor);
164 } else {
165 /* Load the buffer index, which is always stored in VGPR0
166 * for Vertex Shaders */
167 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_ID);
168 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500169
170 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400171 args[0] = t_list;
172 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100173 args[2] = buffer_index;
Christian König44e32242013-03-20 12:10:35 +0100174 input = build_intrinsic(base->gallivm->builder,
175 "llvm.SI.vs.load.input", vec4_type, args, 3,
176 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500177
178 /* Break up the vec4 into individual components */
179 for (chan = 0; chan < 4; chan++) {
180 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
181 /* XXX: Use a helper function for this. There is one in
182 * tgsi_llvm.c. */
183 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
184 LLVMBuildExtractElement(base->gallivm->builder,
185 input, llvm_chan, "");
186 }
187}
188
189static void declare_input_fs(
190 struct si_shader_context * si_shader_ctx,
191 unsigned input_index,
192 const struct tgsi_full_declaration *decl)
193{
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200194 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500195 struct lp_build_context * base =
196 &si_shader_ctx->radeon_bld.soa.bld_base.base;
197 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400198 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100199 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
200
201 LLVMValueRef interp_param;
202 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500203
204 /* This value is:
205 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
206 * quad begins a new primitive. Bit 0 always needs
207 * to be unset)
208 * [32:16] ParamOffset
209 *
210 */
Christian König55fe5cc2013-03-04 16:30:06 +0100211 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200212 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500213
Christian König0666ffd2013-03-05 15:07:39 +0100214 unsigned chan;
215
Tom Stellard0fb1e682012-09-06 16:18:11 -0400216 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
217 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400218 unsigned soa_index =
219 radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellard0fb1e682012-09-06 16:18:11 -0400220 si_shader_ctx->radeon_bld.inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100221 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100222
223 if (chan == 3)
224 /* RCP for fragcoord.w */
225 si_shader_ctx->radeon_bld.inputs[soa_index] =
226 LLVMBuildFDiv(gallivm->builder,
227 lp_build_const_float(gallivm, 1.0f),
228 si_shader_ctx->radeon_bld.inputs[soa_index],
229 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400230 }
231 return;
232 }
233
Michel Dänzer97078b12012-09-25 12:41:31 +0200234 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
235 LLVMValueRef face, is_face_positive;
236
Christian König0666ffd2013-03-05 15:07:39 +0100237 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
238
Michel Dänzer97078b12012-09-25 12:41:31 +0200239 is_face_positive = LLVMBuildFCmp(gallivm->builder,
240 LLVMRealUGT, face,
241 lp_build_const_float(gallivm, 0.0f),
242 "");
243
244 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
245 LLVMBuildSelect(gallivm->builder,
246 is_face_positive,
247 lp_build_const_float(gallivm, 1.0f),
248 lp_build_const_float(gallivm, 0.0f),
249 "");
250 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
251 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
252 lp_build_const_float(gallivm, 0.0f);
253 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
254 lp_build_const_float(gallivm, 1.0f);
255
256 return;
257 }
258
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200259 shader->input[input_index].param_offset = shader->ninterp++;
260 attr_number = lp_build_const_int32(gallivm,
261 shader->input[input_index].param_offset);
262
Tom Stellarda75c6162012-01-06 17:38:37 -0500263 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200264 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500265 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100266 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100267 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200268 } else {
269 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100270 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200271 else
Christian König0666ffd2013-03-05 15:07:39 +0100272 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200273 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500274 break;
275 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100276 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500277 break;
278 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200279 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100280 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200281 else
Christian König0666ffd2013-03-05 15:07:39 +0100282 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200283 break;
284 case TGSI_INTERPOLATE_PERSPECTIVE:
285 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100286 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200287 else
Christian König0666ffd2013-03-05 15:07:39 +0100288 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500289 break;
290 default:
291 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
292 return;
293 }
294
Christian König0666ffd2013-03-05 15:07:39 +0100295 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
296
Tom Stellarda75c6162012-01-06 17:38:37 -0500297 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200298 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100299 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100300 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200301 LLVMValueRef face, is_face_positive;
302 LLVMValueRef back_attr_number =
303 lp_build_const_int32(gallivm,
304 shader->input[input_index].param_offset + 1);
305
Christian König0666ffd2013-03-05 15:07:39 +0100306 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
307
Michel Dänzer691f08d2012-09-06 18:03:38 +0200308 is_face_positive = LLVMBuildFCmp(gallivm->builder,
309 LLVMRealUGT, face,
310 lp_build_const_float(gallivm, 0.0f),
311 "");
312
Tom Stellarda75c6162012-01-06 17:38:37 -0500313 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100314 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200315 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
316 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
317 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
318 LLVMValueRef front, back;
319
320 args[0] = llvm_chan;
321 args[1] = attr_number;
322 front = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100323 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100324 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200325
326 args[1] = back_attr_number;
327 back = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100328 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100329 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200330
331 si_shader_ctx->radeon_bld.inputs[soa_index] =
332 LLVMBuildSelect(gallivm->builder,
333 is_face_positive,
334 front,
335 back,
336 "");
337 }
338
339 shader->ninterp++;
340 } else {
341 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100342 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200343 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
344 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
345 args[0] = llvm_chan;
346 args[1] = attr_number;
347 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100348 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200349 si_shader_ctx->radeon_bld.inputs[soa_index] =
350 build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100351 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100352 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200353 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500354 }
355}
356
357static void declare_input(
358 struct radeon_llvm_context * radeon_bld,
359 unsigned input_index,
360 const struct tgsi_full_declaration *decl)
361{
362 struct si_shader_context * si_shader_ctx =
363 si_shader_context(&radeon_bld->soa.bld_base);
364 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
365 declare_input_vs(si_shader_ctx, input_index, decl);
366 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
367 declare_input_fs(si_shader_ctx, input_index, decl);
368 } else {
369 fprintf(stderr, "Warning: Unsupported shader type,\n");
370 }
371}
372
Christian Könige4ed5872013-03-21 18:02:52 +0100373static void declare_system_value(
374 struct radeon_llvm_context * radeon_bld,
375 unsigned index,
376 const struct tgsi_full_declaration *decl)
377{
Christian Königcf9b31f2013-03-21 18:30:23 +0100378
Christian Könige4ed5872013-03-21 18:02:52 +0100379 LLVMValueRef value = 0;
380
381 switch (decl->Semantic.Name) {
382 case TGSI_SEMANTIC_INSTANCEID:
Christian Königa0dca442013-03-22 15:59:22 +0100383 value = get_instance_index(radeon_bld, 1);
Christian Könige4ed5872013-03-21 18:02:52 +0100384 break;
385
386 case TGSI_SEMANTIC_VERTEXID:
387 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_VERTEX_ID);
388 break;
389
390 default:
391 assert(!"unknown system value");
392 return;
393 }
394
395 radeon_bld->system_values[index] = value;
396}
397
Tom Stellarda75c6162012-01-06 17:38:37 -0500398static LLVMValueRef fetch_constant(
399 struct lp_build_tgsi_context * bld_base,
400 const struct tgsi_full_src_register *reg,
401 enum tgsi_opcode_type type,
402 unsigned swizzle)
403{
Christian König55fe5cc2013-03-04 16:30:06 +0100404 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500405 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100406 const struct tgsi_ind_register *ireg = &reg->Indirect;
407 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500408
Christian Königf5298b02013-02-28 14:50:07 +0100409 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100410 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100411 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500412
Christian König8514f5a2013-02-04 17:46:42 +0100413 if (swizzle == LP_CHAN_ALL) {
414 unsigned chan;
415 LLVMValueRef values[4];
416 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
417 values[chan] = fetch_constant(bld_base, reg, type, chan);
418
419 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
420 }
421
Christian König0f6cf2b2013-03-15 15:53:25 +0100422 idx = reg->Register.Index * 4 + swizzle;
423 if (!reg->Register.Indirect)
424 return bitcast(bld_base, type, si_shader_ctx->constants[idx]);
Christian Königf5298b02013-02-28 14:50:07 +0100425
Christian König0f6cf2b2013-03-15 15:53:25 +0100426 args[0] = si_shader_ctx->const_resource;
427 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
428 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
429 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
430 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
431 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200432
Christian Königf5298b02013-02-28 14:50:07 +0100433 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100434 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500435
Christian Königf5298b02013-02-28 14:50:07 +0100436 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500437}
438
Michel Dänzer26c71392012-08-24 12:03:11 +0200439/* Initialize arguments for the shader export intrinsic */
440static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
441 struct tgsi_full_declaration *d,
442 unsigned index,
443 unsigned target,
444 LLVMValueRef *args)
445{
446 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
447 struct lp_build_context *uint =
448 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
449 struct lp_build_context *base = &bld_base->base;
450 unsigned compressed = 0;
451 unsigned chan;
452
Michel Dänzerf402acd2012-08-22 18:15:36 +0200453 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
454 int cbuf = target - V_008DFC_SQ_EXP_MRT;
455
456 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100457 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100458
459 if (compressed)
460 si_shader_ctx->shader->spi_shader_col_format |=
461 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
462 else
463 si_shader_ctx->shader->spi_shader_col_format |=
464 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200465
466 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200467 }
468 }
469
470 if (compressed) {
471 /* Pixel shader needs to pack output values before export */
472 for (chan = 0; chan < 2; chan++ ) {
473 LLVMValueRef *out_ptr =
474 si_shader_ctx->radeon_bld.soa.outputs[index];
475 args[0] = LLVMBuildLoad(base->gallivm->builder,
476 out_ptr[2 * chan], "");
477 args[1] = LLVMBuildLoad(base->gallivm->builder,
478 out_ptr[2 * chan + 1], "");
479 args[chan + 5] =
480 build_intrinsic(base->gallivm->builder,
481 "llvm.SI.packf16",
482 LLVMInt32TypeInContext(base->gallivm->context),
483 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100484 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100485 args[chan + 7] = args[chan + 5] =
486 LLVMBuildBitCast(base->gallivm->builder,
487 args[chan + 5],
488 LLVMFloatTypeInContext(base->gallivm->context),
489 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200490 }
491
492 /* Set COMPR flag */
493 args[4] = uint->one;
494 } else {
495 for (chan = 0; chan < 4; chan++ ) {
496 LLVMValueRef out_ptr =
497 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
498 /* +5 because the first output value will be
499 * the 6th argument to the intrinsic. */
500 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
501 out_ptr, "");
502 }
503
504 /* Clear COMPR flag */
505 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200506 }
507
508 /* XXX: This controls which components of the output
509 * registers actually get exported. (e.g bit 0 means export
510 * X component, bit 1 means export Y component, etc.) I'm
511 * hard coding this to 0xf for now. In the future, we might
512 * want to do something else. */
513 args[0] = lp_build_const_int32(base->gallivm, 0xf);
514
515 /* Specify whether the EXEC mask represents the valid mask */
516 args[1] = uint->zero;
517
518 /* Specify whether this is the last export */
519 args[2] = uint->zero;
520
521 /* Specify the target we are exporting */
522 args[3] = lp_build_const_int32(base->gallivm, target);
523
Michel Dänzer26c71392012-08-24 12:03:11 +0200524 /* XXX: We probably need to keep track of the output
525 * values, so we know what we are passing to the next
526 * stage. */
527}
528
Michel Dänzer7708a862012-11-02 15:57:30 +0100529static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
530 unsigned index)
531{
532 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
533 struct gallivm_state *gallivm = bld_base->base.gallivm;
534
Christian Königa0dca442013-03-22 15:59:22 +0100535 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Michel Dänzer7708a862012-11-02 15:57:30 +0100536 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
537 LLVMValueRef alpha_pass =
538 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100539 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer7708a862012-11-02 15:57:30 +0100540 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
Christian Königa0dca442013-03-22 15:59:22 +0100541 lp_build_const_float(gallivm, si_shader_ctx->shader->key.ps.alpha_ref));
Michel Dänzer7708a862012-11-02 15:57:30 +0100542 LLVMValueRef arg =
543 lp_build_select(&bld_base->base,
544 alpha_pass,
545 lp_build_const_float(gallivm, 1.0f),
546 lp_build_const_float(gallivm, -1.0f));
547
548 build_intrinsic(gallivm->builder,
549 "llvm.AMDGPU.kill",
550 LLVMVoidTypeInContext(gallivm->context),
551 &arg, 1, 0);
552 } else {
553 build_intrinsic(gallivm->builder,
554 "llvm.AMDGPU.kilp",
555 LLVMVoidTypeInContext(gallivm->context),
556 NULL, 0, 0);
557 }
558}
559
Michel Dänzere3befbc2013-05-15 18:09:50 +0200560static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
561 unsigned index)
562{
563 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
564 struct lp_build_context *base = &bld_base->base;
565 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
566 LLVMValueRef args[9];
567 unsigned reg_index;
568 unsigned chan;
569 unsigned const_chan;
570 LLVMValueRef out_elts[4];
571 LLVMValueRef base_elt;
572 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
573 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, uint->one);
574
575 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
576 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
577 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
578 }
579
580 for (reg_index = 0; reg_index < 2; reg_index ++) {
581 args[5] =
582 args[6] =
583 args[7] =
584 args[8] = lp_build_const_float(base->gallivm, 0.0f);
585
586 /* Compute dot products of position and user clip plane vectors */
587 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
588 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
589 args[0] = const_resource;
590 args[1] = lp_build_const_int32(base->gallivm,
591 ((reg_index * 4 + chan) * 4 +
592 const_chan) * 4);
593 base_elt = build_intrinsic(base->gallivm->builder,
594 "llvm.SI.load.const",
595 base->elem_type,
596 args, 2,
597 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
598 args[5 + chan] =
599 lp_build_add(base, args[5 + chan],
600 lp_build_mul(base, base_elt,
601 out_elts[const_chan]));
602 }
603 }
604
605 args[0] = lp_build_const_int32(base->gallivm, 0xf);
606 args[1] = uint->zero;
607 args[2] = uint->zero;
608 args[3] = lp_build_const_int32(base->gallivm,
609 V_008DFC_SQ_EXP_POS + 2 + reg_index);
610 args[4] = uint->zero;
611 lp_build_intrinsic(base->gallivm->builder,
612 "llvm.SI.export",
613 LLVMVoidTypeInContext(base->gallivm->context),
614 args, 9);
615 }
616}
617
Tom Stellarda75c6162012-01-06 17:38:37 -0500618/* XXX: This is partially implemented for VS only at this point. It is not complete */
619static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
620{
621 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200622 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500623 struct lp_build_context * base = &bld_base->base;
624 struct lp_build_context * uint =
625 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
626 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100627 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500628 LLVMValueRef last_args[9] = { 0 };
Michel Dänzer0afeea52013-05-02 14:53:17 +0200629 unsigned semantic_name;
Christian König35088152012-08-01 22:35:24 +0200630 unsigned color_count = 0;
631 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100632 int depth_index = -1, stencil_index = -1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500633
634 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500635 struct tgsi_full_declaration *d =
636 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500637 unsigned target;
638 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500639 int i;
640
641 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100642
643 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
644 parse->FullToken.FullProperty.Property.PropertyName ==
645 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
646 shader->fs_write_all = TRUE;
647
Tom Stellarda75c6162012-01-06 17:38:37 -0500648 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
649 continue;
650
651 switch (d->Declaration.File) {
652 case TGSI_FILE_INPUT:
653 i = shader->ninput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200654 assert(i < Elements(shader->input));
Tom Stellarda75c6162012-01-06 17:38:37 -0500655 shader->input[i].name = d->Semantic.Name;
656 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200657 shader->input[i].interpolate = d->Interp.Interpolate;
658 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200659 continue;
660
Tom Stellarda75c6162012-01-06 17:38:37 -0500661 case TGSI_FILE_OUTPUT:
662 i = shader->noutput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200663 assert(i < Elements(shader->output));
Tom Stellarda75c6162012-01-06 17:38:37 -0500664 shader->output[i].name = d->Semantic.Name;
665 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200666 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500667 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500668
Christian König35088152012-08-01 22:35:24 +0200669 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500670 continue;
Christian König35088152012-08-01 22:35:24 +0200671 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500672
Michel Dänzer0afeea52013-05-02 14:53:17 +0200673 semantic_name = d->Semantic.Name;
674handle_semantic:
Tom Stellarda75c6162012-01-06 17:38:37 -0500675 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500676 /* Select the correct target */
Michel Dänzer0afeea52013-05-02 14:53:17 +0200677 switch(semantic_name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400678 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer4730dea2013-05-03 17:59:34 +0200679 shader->vs_out_misc_write = 1;
680 shader->vs_out_point_size = 1;
681 target = V_008DFC_SQ_EXP_POS + 1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500682 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100683 case TGSI_SEMANTIC_POSITION:
684 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
685 target = V_008DFC_SQ_EXP_POS;
686 break;
687 } else {
688 depth_index = index;
689 continue;
690 }
691 case TGSI_SEMANTIC_STENCIL:
692 stencil_index = index;
693 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500694 case TGSI_SEMANTIC_COLOR:
695 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200696 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500697 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200698 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500699 param_count++;
700 } else {
701 target = V_008DFC_SQ_EXP_MRT + color_count;
Michel Dänzer7708a862012-11-02 15:57:30 +0100702 if (color_count == 0 &&
Christian Königa0dca442013-03-22 15:59:22 +0100703 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer7708a862012-11-02 15:57:30 +0100704 si_alpha_test(bld_base, index);
705
Tom Stellarda75c6162012-01-06 17:38:37 -0500706 color_count++;
707 }
708 break;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200709 case TGSI_SEMANTIC_CLIPDIST:
710 shader->clip_dist_write |=
711 d->Declaration.UsageMask << (d->Semantic.Index << 2);
712 target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index;
713 break;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200714 case TGSI_SEMANTIC_CLIPVERTEX:
715 si_llvm_emit_clipvertex(bld_base, index);
716 shader->clip_dist_write = 0xFF;
717 continue;
Michel Dänzer30b30372012-09-06 17:53:04 +0200718 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500719 case TGSI_SEMANTIC_GENERIC:
720 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200721 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500722 param_count++;
723 break;
724 default:
725 target = 0;
726 fprintf(stderr,
727 "Warning: SI unhandled output type:%d\n",
Michel Dänzer0afeea52013-05-02 14:53:17 +0200728 semantic_name);
Tom Stellarda75c6162012-01-06 17:38:37 -0500729 }
730
Michel Dänzer26c71392012-08-24 12:03:11 +0200731 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500732
733 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ?
Michel Dänzer0afeea52013-05-02 14:53:17 +0200734 (semantic_name == TGSI_SEMANTIC_POSITION) :
735 (semantic_name == TGSI_SEMANTIC_COLOR)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500736 if (last_args[0]) {
737 lp_build_intrinsic(base->gallivm->builder,
738 "llvm.SI.export",
739 LLVMVoidTypeInContext(base->gallivm->context),
740 last_args, 9);
741 }
742
743 memcpy(last_args, args, sizeof(args));
744 } else {
745 lp_build_intrinsic(base->gallivm->builder,
746 "llvm.SI.export",
747 LLVMVoidTypeInContext(base->gallivm->context),
748 args, 9);
749 }
750
751 }
Michel Dänzer0afeea52013-05-02 14:53:17 +0200752
753 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
754 semantic_name = TGSI_SEMANTIC_GENERIC;
755 goto handle_semantic;
756 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500757 }
758
Michel Dänzer1a616c12012-11-13 17:35:09 +0100759 if (depth_index >= 0 || stencil_index >= 0) {
760 LLVMValueRef out_ptr;
761 unsigned mask = 0;
762
763 /* Specify the target we are exporting */
764 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
765
766 if (depth_index >= 0) {
767 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
768 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
769 mask |= 0x1;
770
771 if (stencil_index < 0) {
772 args[6] =
773 args[7] =
774 args[8] = args[5];
775 }
776 }
777
778 if (stencil_index >= 0) {
779 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
780 args[7] =
781 args[8] =
782 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
783 mask |= 0x2;
784
785 if (depth_index < 0)
786 args[5] = args[6];
787 }
788
789 /* Specify which components to enable */
790 args[0] = lp_build_const_int32(base->gallivm, mask);
791
792 args[1] =
793 args[2] =
794 args[4] = uint->zero;
795
796 if (last_args[0])
797 lp_build_intrinsic(base->gallivm->builder,
798 "llvm.SI.export",
799 LLVMVoidTypeInContext(base->gallivm->context),
800 args, 9);
801 else
802 memcpy(last_args, args, sizeof(args));
803 }
804
Christian Königf18fd252012-07-25 21:58:46 +0200805 if (!last_args[0]) {
806 assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
807
808 /* Specify which components to enable */
809 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
810
811 /* Specify the target we are exporting */
812 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
813
814 /* Set COMPR flag to zero to export data as 32-bit */
815 last_args[4] = uint->zero;
816
817 /* dummy bits */
818 last_args[5]= uint->zero;
819 last_args[6]= uint->zero;
820 last_args[7]= uint->zero;
821 last_args[8]= uint->zero;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100822
823 si_shader_ctx->shader->spi_shader_col_format |=
824 V_028714_SPI_SHADER_32_ABGR;
Michel Dänzere369f402013-04-30 16:34:10 +0200825 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Christian Königf18fd252012-07-25 21:58:46 +0200826 }
827
Tom Stellarda75c6162012-01-06 17:38:37 -0500828 /* Specify whether the EXEC mask represents the valid mask */
829 last_args[1] = lp_build_const_int32(base->gallivm,
830 si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
831
Michel Dänzerc8402702013-02-12 18:37:22 +0100832 if (shader->fs_write_all && shader->nr_cbufs > 1) {
833 int i;
834
835 /* Specify that this is not yet the last export */
836 last_args[2] = lp_build_const_int32(base->gallivm, 0);
837
838 for (i = 1; i < shader->nr_cbufs; i++) {
839 /* Specify the target we are exporting */
840 last_args[3] = lp_build_const_int32(base->gallivm,
841 V_008DFC_SQ_EXP_MRT + i);
842
843 lp_build_intrinsic(base->gallivm->builder,
844 "llvm.SI.export",
845 LLVMVoidTypeInContext(base->gallivm->context),
846 last_args, 9);
847
848 si_shader_ctx->shader->spi_shader_col_format |=
849 si_shader_ctx->shader->spi_shader_col_format << 4;
Michel Dänzere369f402013-04-30 16:34:10 +0200850 si_shader_ctx->shader->cb_shader_mask |=
851 si_shader_ctx->shader->cb_shader_mask << 4;
Michel Dänzerc8402702013-02-12 18:37:22 +0100852 }
853
854 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
855 }
856
Tom Stellarda75c6162012-01-06 17:38:37 -0500857 /* Specify that this is the last export */
858 last_args[2] = lp_build_const_int32(base->gallivm, 1);
859
860 lp_build_intrinsic(base->gallivm->builder,
861 "llvm.SI.export",
862 LLVMVoidTypeInContext(base->gallivm->context),
863 last_args, 9);
864
865/* XXX: Look up what this function does */
866/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
867}
868
869static void tex_fetch_args(
870 struct lp_build_tgsi_context * bld_base,
871 struct lp_build_emit_data * emit_data)
872{
Christian König55fe5cc2013-03-04 16:30:06 +0100873 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100874 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200875 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100876 unsigned opcode = inst->Instruction.Opcode;
877 unsigned target = inst->Texture.Texture;
Michel Dänzera6b83c02013-02-21 16:10:55 +0100878 unsigned sampler_src;
Michel Dänzer120efee2013-01-25 12:10:11 +0100879 LLVMValueRef coords[4];
880 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100881 int ref_pos;
882 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +0100883 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100884 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400885
Michel Dänzer120efee2013-01-25 12:10:11 +0100886 /* Fetch and project texture coordinates */
887 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100888 for (chan = 0; chan < 3; chan++ ) {
889 coords[chan] = lp_build_emit_fetch(bld_base,
890 emit_data->inst, 0,
891 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100892 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200893 coords[chan] = lp_build_emit_llvm_binary(bld_base,
894 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100895 coords[chan],
896 coords[3]);
897 }
898
Michel Dänzer120efee2013-01-25 12:10:11 +0100899 if (opcode == TGSI_OPCODE_TXP)
900 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500901
Michel Dänzer120efee2013-01-25 12:10:11 +0100902 /* Pack LOD bias value */
903 if (opcode == TGSI_OPCODE_TXB)
904 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400905
Michel Dänzer0495adb2013-05-06 12:45:14 +0200906 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100907 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100908
909 /* Pack depth comparison value */
910 switch (target) {
911 case TGSI_TEXTURE_SHADOW1D:
912 case TGSI_TEXTURE_SHADOW1D_ARRAY:
913 case TGSI_TEXTURE_SHADOW2D:
914 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +0100915 case TGSI_TEXTURE_SHADOWCUBE:
916 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100917 assert(ref_pos >= 0);
918 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +0100919 break;
920 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
921 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +0100922 }
923
Michel Dänzera6b83c02013-02-21 16:10:55 +0100924 /* Pack user derivatives */
925 if (opcode == TGSI_OPCODE_TXD) {
926 for (chan = 0; chan < 2; chan++) {
927 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
928 if (num_coords > 1)
929 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
930 }
931 }
932
Michel Dänzer120efee2013-01-25 12:10:11 +0100933 /* Pack texture coordinates */
934 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100935 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +0100936 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +0100937 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +0100938 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +0100939
Michel Dänzer120efee2013-01-25 12:10:11 +0100940 /* Pack array slice */
941 switch (target) {
942 case TGSI_TEXTURE_1D_ARRAY:
943 address[count++] = coords[1];
944 }
945 switch (target) {
946 case TGSI_TEXTURE_2D_ARRAY:
947 case TGSI_TEXTURE_2D_ARRAY_MSAA:
948 case TGSI_TEXTURE_SHADOW2D_ARRAY:
949 address[count++] = coords[2];
950 }
951 switch (target) {
952 case TGSI_TEXTURE_CUBE_ARRAY:
953 case TGSI_TEXTURE_SHADOW1D_ARRAY:
954 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
955 address[count++] = coords[3];
956 }
957
958 /* Pack LOD */
Michel Dänzer36231112013-05-02 09:44:45 +0200959 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +0100960 address[count++] = coords[3];
961
962 if (count > 16) {
963 assert(!"Cannot handle more than 16 texture address parameters");
964 count = 16;
965 }
966
967 for (chan = 0; chan < count; chan++ ) {
968 address[chan] = LLVMBuildBitCast(gallivm->builder,
969 address[chan],
970 LLVMInt32TypeInContext(gallivm->context),
971 "");
972 }
973
Michel Dänzera6b83c02013-02-21 16:10:55 +0100974 sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
975
Michel Dänzer36231112013-05-02 09:44:45 +0200976 /* Resource */
Michel Dänzera6b83c02013-02-21 16:10:55 +0100977 emit_data->args[1] = si_shader_ctx->resources[emit_data->inst->Src[sampler_src].Register.Index];
Michel Dänzer36231112013-05-02 09:44:45 +0200978
979 if (opcode == TGSI_OPCODE_TXF) {
980 /* add tex offsets */
981 if (inst->Texture.NumOffsets) {
982 struct lp_build_context *uint_bld = &bld_base->uint_bld;
983 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
984 const struct tgsi_texture_offset * off = inst->TexOffsets;
985
986 assert(inst->Texture.NumOffsets == 1);
987
988 address[0] =
989 lp_build_add(uint_bld, address[0],
990 bld->immediates[off->Index][off->SwizzleX]);
991 if (num_coords > 1)
992 address[1] =
993 lp_build_add(uint_bld, address[1],
994 bld->immediates[off->Index][off->SwizzleY]);
995 if (num_coords > 2)
996 address[2] =
997 lp_build_add(uint_bld, address[2],
998 bld->immediates[off->Index][off->SwizzleZ]);
999 }
1000
1001 emit_data->dst_type = LLVMVectorType(
1002 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1003 4);
1004
1005 emit_data->arg_count = 3;
1006 } else {
1007 /* Sampler */
Michel Dänzera6b83c02013-02-21 16:10:55 +01001008 emit_data->args[2] = si_shader_ctx->samplers[emit_data->inst->Src[sampler_src].Register.Index];
Michel Dänzer36231112013-05-02 09:44:45 +02001009
1010 emit_data->dst_type = LLVMVectorType(
1011 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1012 4);
1013
1014 emit_data->arg_count = 4;
1015 }
1016
1017 /* Dimensions */
1018 emit_data->args[emit_data->arg_count - 1] =
1019 lp_build_const_int32(bld_base->base.gallivm, target);
1020
Michel Dänzer120efee2013-01-25 12:10:11 +01001021 /* Pad to power of two vector */
1022 while (count < util_next_power_of_two(count))
1023 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1024
Christian Königccf3e8f2013-03-26 15:09:27 +01001025 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001026}
1027
Michel Dänzer07eddc42013-02-06 15:43:10 +01001028static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1029 struct lp_build_tgsi_context * bld_base,
1030 struct lp_build_emit_data * emit_data)
1031{
1032 struct lp_build_context * base = &bld_base->base;
1033 char intr_name[23];
1034
1035 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001036 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001037
Christian König44e32242013-03-20 12:10:35 +01001038 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001039 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001040 emit_data->args, emit_data->arg_count,
1041 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001042}
1043
Michel Dänzer0495adb2013-05-06 12:45:14 +02001044static void txq_fetch_args(
1045 struct lp_build_tgsi_context * bld_base,
1046 struct lp_build_emit_data * emit_data)
1047{
1048 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1049 const struct tgsi_full_instruction *inst = emit_data->inst;
1050
1051 /* Mip level */
1052 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1053
1054 /* Resource */
1055 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1056
1057 /* Dimensions */
1058 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1059 inst->Texture.Texture);
1060
1061 emit_data->arg_count = 3;
1062
1063 emit_data->dst_type = LLVMVectorType(
1064 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1065 4);
1066}
1067
Tom Stellarda75c6162012-01-06 17:38:37 -05001068static const struct lp_build_tgsi_action tex_action = {
1069 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001070 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001071 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001072};
1073
Michel Dänzer3e205132012-11-06 17:39:01 +01001074static const struct lp_build_tgsi_action txb_action = {
1075 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001076 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001077 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001078};
1079
Michel Dänzera6b83c02013-02-21 16:10:55 +01001080#if HAVE_LLVM >= 0x0304
1081static const struct lp_build_tgsi_action txd_action = {
1082 .fetch_args = tex_fetch_args,
1083 .emit = build_tex_intrinsic,
1084 .intr_name = "llvm.SI.sampled."
1085};
1086#endif
1087
Michel Dänzer36231112013-05-02 09:44:45 +02001088static const struct lp_build_tgsi_action txf_action = {
1089 .fetch_args = tex_fetch_args,
1090 .emit = build_tex_intrinsic,
1091 .intr_name = "llvm.SI.imageload."
1092};
1093
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001094static const struct lp_build_tgsi_action txl_action = {
1095 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001096 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001097 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001098};
1099
Michel Dänzer0495adb2013-05-06 12:45:14 +02001100static const struct lp_build_tgsi_action txq_action = {
1101 .fetch_args = txq_fetch_args,
1102 .emit = build_tgsi_intrinsic_nomem,
1103 .intr_name = "llvm.SI.resinfo"
1104};
1105
Christian König206f0592013-03-20 14:37:21 +01001106static void create_meta_data(struct si_shader_context *si_shader_ctx)
1107{
1108 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1109 LLVMValueRef args[3];
1110
1111 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1112 args[1] = 0;
1113 args[2] = lp_build_const_int32(gallivm, 1);
1114
1115 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1116}
1117
Christian König55fe5cc2013-03-04 16:30:06 +01001118static void create_function(struct si_shader_context *si_shader_ctx)
1119{
1120 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
Christian König0666ffd2013-03-05 15:07:39 +01001121 LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
Christian König55fe5cc2013-03-04 16:30:06 +01001122 unsigned i;
1123
Christian König55fe5cc2013-03-04 16:30:06 +01001124 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01001125 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01001126 f32 = LLVMFloatTypeInContext(gallivm->context);
1127 v2i32 = LLVMVectorType(i32, 2);
1128 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01001129
Christian Königf5298b02013-02-28 14:50:07 +01001130 params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
1131 params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
Christian König55fe5cc2013-03-04 16:30:06 +01001132 params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
1133
Christian Königc4973212013-03-05 12:14:02 +01001134 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Christian König55fe5cc2013-03-04 16:30:06 +01001135 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER];
Christian Königcf9b31f2013-03-21 18:30:23 +01001136 params[SI_PARAM_START_INSTANCE] = i32;
Christian Könige4ed5872013-03-21 18:02:52 +01001137 params[SI_PARAM_VERTEX_ID] = i32;
1138 params[SI_PARAM_DUMMY_0] = i32;
1139 params[SI_PARAM_DUMMY_1] = i32;
1140 params[SI_PARAM_INSTANCE_ID] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01001141 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 9);
Christian König0666ffd2013-03-05 15:07:39 +01001142
Christian Königc4973212013-03-05 12:14:02 +01001143 } else {
Christian König0666ffd2013-03-05 15:07:39 +01001144 params[SI_PARAM_PRIM_MASK] = i32;
1145 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
1146 params[SI_PARAM_PERSP_CENTER] = v2i32;
1147 params[SI_PARAM_PERSP_CENTROID] = v2i32;
1148 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
1149 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
1150 params[SI_PARAM_LINEAR_CENTER] = v2i32;
1151 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
1152 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
1153 params[SI_PARAM_POS_X_FLOAT] = f32;
1154 params[SI_PARAM_POS_Y_FLOAT] = f32;
1155 params[SI_PARAM_POS_Z_FLOAT] = f32;
1156 params[SI_PARAM_POS_W_FLOAT] = f32;
1157 params[SI_PARAM_FRONT_FACE] = f32;
1158 params[SI_PARAM_ANCILLARY] = f32;
1159 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
1160 params[SI_PARAM_POS_FIXED_PT] = f32;
1161 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20);
Christian Königc4973212013-03-05 12:14:02 +01001162 }
Christian König55fe5cc2013-03-04 16:30:06 +01001163
1164 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
1165 for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) {
1166 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
1167 LLVMAddAttribute(P, LLVMInRegAttribute);
1168 }
Christian Königcf9b31f2013-03-21 18:30:23 +01001169
1170 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
1171 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1172 SI_PARAM_START_INSTANCE);
1173 LLVMAddAttribute(P, LLVMInRegAttribute);
1174 }
Christian König55fe5cc2013-03-04 16:30:06 +01001175}
Tom Stellarda75c6162012-01-06 17:38:37 -05001176
Christian König0f6cf2b2013-03-15 15:53:25 +01001177static void preload_constants(struct si_shader_context *si_shader_ctx)
1178{
1179 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1180 struct gallivm_state * gallivm = bld_base->base.gallivm;
1181 const struct tgsi_shader_info * info = bld_base->info;
1182
1183 unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1;
1184
1185 LLVMValueRef ptr;
1186
1187 if (num_const == 0)
1188 return;
1189
1190 /* Allocate space for the constant values */
1191 si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef));
1192
1193 /* Load the resource descriptor */
1194 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1195 si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
1196
1197 /* Load the constants, we rely on the code sinking to do the rest */
1198 for (i = 0; i < num_const * 4; ++i) {
1199 LLVMValueRef args[2] = {
1200 si_shader_ctx->const_resource,
1201 lp_build_const_int32(gallivm, i * 4)
1202 };
1203 si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1204 bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1205 }
1206}
1207
Christian König1c100182013-03-17 16:02:42 +01001208static void preload_samplers(struct si_shader_context *si_shader_ctx)
1209{
1210 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1211 struct gallivm_state * gallivm = bld_base->base.gallivm;
1212 const struct tgsi_shader_info * info = bld_base->info;
1213
1214 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1215
1216 LLVMValueRef res_ptr, samp_ptr;
1217 LLVMValueRef offset;
1218
1219 if (num_samplers == 0)
1220 return;
1221
1222 /* Allocate space for the values */
1223 si_shader_ctx->resources = CALLOC(num_samplers, sizeof(LLVMValueRef));
1224 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1225
1226 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1227 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1228
1229 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1230 for (i = 0; i < num_samplers; ++i) {
1231
1232 /* Resource */
1233 offset = lp_build_const_int32(gallivm, i);
1234 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1235
1236 /* Sampler */
1237 offset = lp_build_const_int32(gallivm, i);
1238 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
1239 }
1240}
1241
Tom Stellard302f53d2012-10-25 13:50:10 -04001242int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader,
1243 LLVMModuleRef mod)
1244{
Tom Stellard302f53d2012-10-25 13:50:10 -04001245 unsigned i;
1246 uint32_t *ptr;
1247 bool dump;
Tom Stellard7782d192013-04-04 09:57:13 -07001248 struct radeon_llvm_binary binary;
Tom Stellard302f53d2012-10-25 13:50:10 -04001249
1250 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
1251
Tom Stellard7782d192013-04-04 09:57:13 -07001252 memset(&binary, 0, sizeof(binary));
1253 radeon_llvm_compile(mod, &binary,
1254 r600_get_llvm_processor_name(rctx->screen->family), dump);
Tom Stellard302f53d2012-10-25 13:50:10 -04001255 if (dump) {
1256 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07001257 for (i = 0; i < binary.code_size; i+=4 ) {
1258 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
1259 binary.code[i + 2], binary.code[i + 1],
1260 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04001261 }
1262 }
1263
Tom Stellardd50343d2013-04-04 16:21:06 -04001264 /* XXX: We may be able to emit some of these values directly rather than
1265 * extracting fields to be emitted later.
1266 */
1267 for (i = 0; i < binary.config_size; i+= 8) {
1268 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
1269 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
1270 switch (reg) {
1271 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
1272 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
1273 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
1274 case R_00B848_COMPUTE_PGM_RSRC1:
1275 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
1276 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
1277 break;
1278 case R_0286CC_SPI_PS_INPUT_ENA:
1279 shader->spi_ps_input_ena = value;
1280 break;
1281 default:
1282 fprintf(stderr, "Warning: Compiler emitted unknown "
1283 "config register: 0x%x\n", reg);
1284 break;
1285 }
1286 }
Tom Stellard302f53d2012-10-25 13:50:10 -04001287
1288 /* copy new shader */
1289 si_resource_reference(&shader->bo, NULL);
1290 shader->bo = si_resource_create_custom(rctx->context.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04001291 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001292 if (shader->bo == NULL) {
1293 return -ENOMEM;
1294 }
1295
1296 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
1297 if (0 /*R600_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04001298 for (i = 0; i < binary.code_size / 4; ++i) {
1299 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04001300 }
1301 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04001302 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001303 }
1304 rctx->ws->buffer_unmap(shader->bo->cs_buf);
1305
Tom Stellard7782d192013-04-04 09:57:13 -07001306 free(binary.code);
1307 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04001308
1309 return 0;
1310}
1311
Tom Stellarda75c6162012-01-06 17:38:37 -05001312int si_pipe_shader_create(
1313 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01001314 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05001315{
1316 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001317 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05001318 struct si_shader_context si_shader_ctx;
1319 struct tgsi_shader_info shader_info;
1320 struct lp_build_tgsi_context * bld_base;
1321 LLVMModuleRef mod;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001322 bool dump;
Tom Stellard302f53d2012-10-25 13:50:10 -04001323 int r = 0;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001324
1325 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -05001326
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001327 assert(shader->shader.noutput == 0);
1328 assert(shader->shader.ninterp == 0);
1329 assert(shader->shader.ninput == 0);
1330
Michel Dänzercfebaf92012-08-31 19:04:08 +02001331 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001332 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1333 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1334
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001335 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzere44dfd42012-11-07 17:33:08 +01001336 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01001337 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001338 bld_base->info = &shader_info;
1339 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001340 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1341
1342 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001343 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01001344#if HAVE_LLVM >= 0x0304
1345 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
1346#endif
Michel Dänzer36231112013-05-02 09:44:45 +02001347 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001348 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001349 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02001350 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001351
1352 si_shader_ctx.radeon_bld.load_input = declare_input;
Christian Könige4ed5872013-03-21 18:02:52 +01001353 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001354 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001355 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1356 si_shader_ctx.shader = shader;
1357 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05001358
Christian König206f0592013-03-20 14:37:21 +01001359 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01001360 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01001361 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01001362 preload_samplers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001363
Christian König835098a2012-07-17 21:28:10 +02001364 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001365
Tom Stellard185fc9a2012-07-12 10:40:47 -04001366 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1367 * conversion fails. */
1368 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001369 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001370 }
1371
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001372 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001373 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Christian König0f6cf2b2013-03-15 15:53:25 +01001374 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001375 FREE(si_shader_ctx.resources);
1376 FREE(si_shader_ctx.samplers);
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001377 return -EINVAL;
1378 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001379
1380 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1381
1382 mod = bld_base->base.gallivm->module;
Tom Stellard302f53d2012-10-25 13:50:10 -04001383 r = si_compile_llvm(rctx, shader, mod);
Tom Stellarda75c6162012-01-06 17:38:37 -05001384
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001385 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001386 tgsi_parse_free(&si_shader_ctx.parse);
1387
Christian König0f6cf2b2013-03-15 15:53:25 +01001388 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001389 FREE(si_shader_ctx.resources);
1390 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05001391
Tom Stellard302f53d2012-10-25 13:50:10 -04001392 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05001393}
1394
1395void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1396{
Christian Königfe412872012-07-24 18:47:19 +02001397 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001398}