blob: 062e8338fcfa49debbe88a62657d80ce694b8b9a [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"
Tom Stellarda75c6162012-01-06 17:38:37 -050039#include "tgsi/tgsi_info.h"
40#include "tgsi/tgsi_parse.h"
41#include "tgsi/tgsi_scan.h"
42#include "tgsi/tgsi_dump.h"
43
44#include "radeonsi_pipe.h"
45#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020046#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050047#include "sid.h"
48
49#include <assert.h>
50#include <errno.h>
51#include <stdio.h>
52
Tom Stellarda75c6162012-01-06 17:38:37 -050053struct si_shader_context
54{
55 struct radeon_llvm_context radeon_bld;
56 struct r600_context *rctx;
57 struct tgsi_parse_context parse;
58 struct tgsi_token * tokens;
59 struct si_pipe_shader *shader;
Michel Dänzer44ef0332012-10-05 16:59:10 +020060 struct si_shader_key key;
Tom Stellarda75c6162012-01-06 17:38:37 -050061 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Christian König206f0592013-03-20 14:37:21 +010062 LLVMValueRef const_md;
Tom Stellarda75c6162012-01-06 17:38:37 -050063/* struct list_head inputs; */
64/* unsigned * input_mappings *//* From TGSI to SI hw */
65/* struct tgsi_shader_info info;*/
66};
67
68static struct si_shader_context * si_shader_context(
69 struct lp_build_tgsi_context * bld_base)
70{
71 return (struct si_shader_context *)bld_base;
72}
73
74
75#define PERSPECTIVE_BASE 0
76#define LINEAR_BASE 9
77
78#define SAMPLE_OFFSET 0
79#define CENTER_OFFSET 2
80#define CENTROID_OFSET 4
81
82#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040083#define CONST_ADDR_SPACE 2
Tom Stellard89ece082012-05-29 11:36:29 -040084#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050085
Tom Stellard467f5162012-05-16 15:15:35 -040086/**
87 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
88 *
89 * @param offset The offset parameter specifies the number of
90 * elements to offset, not the number of bytes or dwords. An element is the
91 * the type pointed to by the base_ptr parameter (e.g. int is the element of
92 * an int* pointer)
93 *
94 * When LLVM lowers the load instruction, it will convert the element offset
95 * into a dword offset automatically.
96 *
97 */
98static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +010099 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400100 LLVMValueRef base_ptr,
101 LLVMValueRef offset)
102{
Christian König206f0592013-03-20 14:37:21 +0100103 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400104
Christian König206f0592013-03-20 14:37:21 +0100105 LLVMValueRef computed_ptr = LLVMBuildGEP(
106 base->gallivm->builder, base_ptr, &offset, 1, "");
107
108 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
109 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
110 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400111}
112
Tom Stellarda75c6162012-01-06 17:38:37 -0500113static void declare_input_vs(
114 struct si_shader_context * si_shader_ctx,
115 unsigned input_index,
116 const struct tgsi_full_declaration *decl)
117{
118 LLVMValueRef t_list_ptr;
119 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400120 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500121 LLVMValueRef attribute_offset;
122 LLVMValueRef buffer_index_reg;
Tom Stellard467f5162012-05-16 15:15:35 -0400123 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500124 LLVMTypeRef vec4_type;
125 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500126 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Christian Königb15e3ae2012-07-25 11:22:59 +0200127 //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index];
Tom Stellarda75c6162012-01-06 17:38:37 -0500128 unsigned chan;
129
Tom Stellard467f5162012-05-16 15:15:35 -0400130 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100131 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500132
Christian Königb15e3ae2012-07-25 11:22:59 +0200133 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400134
Christian König206f0592013-03-20 14:37:21 +0100135 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400136
137 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200138 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500139
Christian Königc4973212013-03-05 12:14:02 +0100140 /* Load the buffer index, which is always stored in VGPR0
Tom Stellarda75c6162012-01-06 17:38:37 -0500141 * for Vertex Shaders */
Christian Königc4973212013-03-05 12:14:02 +0100142 buffer_index_reg = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_INDEX);
Tom Stellarda75c6162012-01-06 17:38:37 -0500143
144 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400145 args[0] = t_list;
146 args[1] = attribute_offset;
147 args[2] = buffer_index_reg;
Christian König44e32242013-03-20 12:10:35 +0100148 input = build_intrinsic(base->gallivm->builder,
149 "llvm.SI.vs.load.input", vec4_type, args, 3,
150 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500151
152 /* Break up the vec4 into individual components */
153 for (chan = 0; chan < 4; chan++) {
154 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
155 /* XXX: Use a helper function for this. There is one in
156 * tgsi_llvm.c. */
157 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
158 LLVMBuildExtractElement(base->gallivm->builder,
159 input, llvm_chan, "");
160 }
161}
162
163static void declare_input_fs(
164 struct si_shader_context * si_shader_ctx,
165 unsigned input_index,
166 const struct tgsi_full_declaration *decl)
167{
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200168 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500169 struct lp_build_context * base =
170 &si_shader_ctx->radeon_bld.soa.bld_base.base;
171 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400172 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100173 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
174
175 LLVMValueRef interp_param;
176 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500177
178 /* This value is:
179 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
180 * quad begins a new primitive. Bit 0 always needs
181 * to be unset)
182 * [32:16] ParamOffset
183 *
184 */
Christian König55fe5cc2013-03-04 16:30:06 +0100185 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200186 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500187
Christian König0666ffd2013-03-05 15:07:39 +0100188 unsigned chan;
189
Tom Stellard0fb1e682012-09-06 16:18:11 -0400190 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
191 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400192 unsigned soa_index =
193 radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellard0fb1e682012-09-06 16:18:11 -0400194 si_shader_ctx->radeon_bld.inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100195 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100196
197 if (chan == 3)
198 /* RCP for fragcoord.w */
199 si_shader_ctx->radeon_bld.inputs[soa_index] =
200 LLVMBuildFDiv(gallivm->builder,
201 lp_build_const_float(gallivm, 1.0f),
202 si_shader_ctx->radeon_bld.inputs[soa_index],
203 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400204 }
205 return;
206 }
207
Michel Dänzer97078b12012-09-25 12:41:31 +0200208 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
209 LLVMValueRef face, is_face_positive;
210
Christian König0666ffd2013-03-05 15:07:39 +0100211 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
212
Michel Dänzer97078b12012-09-25 12:41:31 +0200213 is_face_positive = LLVMBuildFCmp(gallivm->builder,
214 LLVMRealUGT, face,
215 lp_build_const_float(gallivm, 0.0f),
216 "");
217
218 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
219 LLVMBuildSelect(gallivm->builder,
220 is_face_positive,
221 lp_build_const_float(gallivm, 1.0f),
222 lp_build_const_float(gallivm, 0.0f),
223 "");
224 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
225 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
226 lp_build_const_float(gallivm, 0.0f);
227 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
228 lp_build_const_float(gallivm, 1.0f);
229
230 return;
231 }
232
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200233 shader->input[input_index].param_offset = shader->ninterp++;
234 attr_number = lp_build_const_int32(gallivm,
235 shader->input[input_index].param_offset);
236
Tom Stellarda75c6162012-01-06 17:38:37 -0500237 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200238 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500239 case TGSI_INTERPOLATE_COLOR:
Michel Dänzer18272c92013-02-13 12:54:13 +0100240 if (si_shader_ctx->key.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100241 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200242 } else {
243 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100244 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200245 else
Christian König0666ffd2013-03-05 15:07:39 +0100246 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200247 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500248 break;
249 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100250 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500251 break;
252 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200253 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100254 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_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_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200257 break;
258 case TGSI_INTERPOLATE_PERSPECTIVE:
259 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100260 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200261 else
Christian König0666ffd2013-03-05 15:07:39 +0100262 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500263 break;
264 default:
265 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
266 return;
267 }
268
Christian König0666ffd2013-03-05 15:07:39 +0100269 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
270
Tom Stellarda75c6162012-01-06 17:38:37 -0500271 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200272 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
273 si_shader_ctx->key.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100274 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200275 LLVMValueRef face, is_face_positive;
276 LLVMValueRef back_attr_number =
277 lp_build_const_int32(gallivm,
278 shader->input[input_index].param_offset + 1);
279
Christian König0666ffd2013-03-05 15:07:39 +0100280 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
281
Michel Dänzer691f08d2012-09-06 18:03:38 +0200282 is_face_positive = LLVMBuildFCmp(gallivm->builder,
283 LLVMRealUGT, face,
284 lp_build_const_float(gallivm, 0.0f),
285 "");
286
Tom Stellarda75c6162012-01-06 17:38:37 -0500287 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100288 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200289 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
290 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
291 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
292 LLVMValueRef front, back;
293
294 args[0] = llvm_chan;
295 args[1] = attr_number;
296 front = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100297 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100298 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200299
300 args[1] = back_attr_number;
301 back = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100302 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100303 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200304
305 si_shader_ctx->radeon_bld.inputs[soa_index] =
306 LLVMBuildSelect(gallivm->builder,
307 is_face_positive,
308 front,
309 back,
310 "");
311 }
312
313 shader->ninterp++;
314 } else {
315 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100316 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200317 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
318 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
319 args[0] = llvm_chan;
320 args[1] = attr_number;
321 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100322 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200323 si_shader_ctx->radeon_bld.inputs[soa_index] =
324 build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100325 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100326 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200327 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500328 }
329}
330
331static void declare_input(
332 struct radeon_llvm_context * radeon_bld,
333 unsigned input_index,
334 const struct tgsi_full_declaration *decl)
335{
336 struct si_shader_context * si_shader_ctx =
337 si_shader_context(&radeon_bld->soa.bld_base);
338 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
339 declare_input_vs(si_shader_ctx, input_index, decl);
340 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
341 declare_input_fs(si_shader_ctx, input_index, decl);
342 } else {
343 fprintf(stderr, "Warning: Unsupported shader type,\n");
344 }
345}
346
347static LLVMValueRef fetch_constant(
348 struct lp_build_tgsi_context * bld_base,
349 const struct tgsi_full_src_register *reg,
350 enum tgsi_opcode_type type,
351 unsigned swizzle)
352{
Christian König55fe5cc2013-03-04 16:30:06 +0100353 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500354 struct lp_build_context * base = &bld_base->base;
355
Christian Königf5298b02013-02-28 14:50:07 +0100356 LLVMValueRef ptr;
357 LLVMValueRef args[2];
358 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500359
Christian König8514f5a2013-02-04 17:46:42 +0100360 if (swizzle == LP_CHAN_ALL) {
361 unsigned chan;
362 LLVMValueRef values[4];
363 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
364 values[chan] = fetch_constant(bld_base, reg, type, chan);
365
366 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
367 }
368
Christian Königf5298b02013-02-28 14:50:07 +0100369 /* Load the resource descriptor */
370 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König206f0592013-03-20 14:37:21 +0100371 args[0] = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
Christian Königf5298b02013-02-28 14:50:07 +0100372
Christian König5e616cf2013-03-07 11:58:56 +0100373 args[1] = lp_build_const_int32(base->gallivm, (reg->Register.Index * 4 + swizzle) * 4);
Christian Könige7723b52012-08-24 12:55:34 +0200374 if (reg->Register.Indirect) {
Christian König5e616cf2013-03-07 11:58:56 +0100375 const struct tgsi_ind_register *ireg = &reg->Indirect;
376 LLVMValueRef addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
377 LLVMValueRef idx = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
378 idx = lp_build_mul_imm(&bld_base->uint_bld, idx, 16);
379 args[1] = lp_build_add(&bld_base->uint_bld, idx, args[1]);
380 }
Christian Könige7723b52012-08-24 12:55:34 +0200381
Christian Königf5298b02013-02-28 14:50:07 +0100382 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100383 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500384
Christian Königf5298b02013-02-28 14:50:07 +0100385 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500386}
387
Michel Dänzer26c71392012-08-24 12:03:11 +0200388/* Initialize arguments for the shader export intrinsic */
389static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
390 struct tgsi_full_declaration *d,
391 unsigned index,
392 unsigned target,
393 LLVMValueRef *args)
394{
395 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
396 struct lp_build_context *uint =
397 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
398 struct lp_build_context *base = &bld_base->base;
399 unsigned compressed = 0;
400 unsigned chan;
401
Michel Dänzerf402acd2012-08-22 18:15:36 +0200402 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
403 int cbuf = target - V_008DFC_SQ_EXP_MRT;
404
405 if (cbuf >= 0 && cbuf < 8) {
Michel Dänzer44ef0332012-10-05 16:59:10 +0200406 compressed = (si_shader_ctx->key.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100407
408 if (compressed)
409 si_shader_ctx->shader->spi_shader_col_format |=
410 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
411 else
412 si_shader_ctx->shader->spi_shader_col_format |=
413 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200414 }
415 }
416
417 if (compressed) {
418 /* Pixel shader needs to pack output values before export */
419 for (chan = 0; chan < 2; chan++ ) {
420 LLVMValueRef *out_ptr =
421 si_shader_ctx->radeon_bld.soa.outputs[index];
422 args[0] = LLVMBuildLoad(base->gallivm->builder,
423 out_ptr[2 * chan], "");
424 args[1] = LLVMBuildLoad(base->gallivm->builder,
425 out_ptr[2 * chan + 1], "");
426 args[chan + 5] =
427 build_intrinsic(base->gallivm->builder,
428 "llvm.SI.packf16",
429 LLVMInt32TypeInContext(base->gallivm->context),
430 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100431 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100432 args[chan + 7] = args[chan + 5] =
433 LLVMBuildBitCast(base->gallivm->builder,
434 args[chan + 5],
435 LLVMFloatTypeInContext(base->gallivm->context),
436 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200437 }
438
439 /* Set COMPR flag */
440 args[4] = uint->one;
441 } else {
442 for (chan = 0; chan < 4; chan++ ) {
443 LLVMValueRef out_ptr =
444 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
445 /* +5 because the first output value will be
446 * the 6th argument to the intrinsic. */
447 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
448 out_ptr, "");
449 }
450
451 /* Clear COMPR flag */
452 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200453 }
454
455 /* XXX: This controls which components of the output
456 * registers actually get exported. (e.g bit 0 means export
457 * X component, bit 1 means export Y component, etc.) I'm
458 * hard coding this to 0xf for now. In the future, we might
459 * want to do something else. */
460 args[0] = lp_build_const_int32(base->gallivm, 0xf);
461
462 /* Specify whether the EXEC mask represents the valid mask */
463 args[1] = uint->zero;
464
465 /* Specify whether this is the last export */
466 args[2] = uint->zero;
467
468 /* Specify the target we are exporting */
469 args[3] = lp_build_const_int32(base->gallivm, target);
470
Michel Dänzer26c71392012-08-24 12:03:11 +0200471 /* XXX: We probably need to keep track of the output
472 * values, so we know what we are passing to the next
473 * stage. */
474}
475
Michel Dänzer7708a862012-11-02 15:57:30 +0100476static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
477 unsigned index)
478{
479 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
480 struct gallivm_state *gallivm = bld_base->base.gallivm;
481
482 if (si_shader_ctx->key.alpha_func != PIPE_FUNC_NEVER) {
483 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
484 LLVMValueRef alpha_pass =
485 lp_build_cmp(&bld_base->base,
486 si_shader_ctx->key.alpha_func,
487 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
488 lp_build_const_float(gallivm, si_shader_ctx->key.alpha_ref));
489 LLVMValueRef arg =
490 lp_build_select(&bld_base->base,
491 alpha_pass,
492 lp_build_const_float(gallivm, 1.0f),
493 lp_build_const_float(gallivm, -1.0f));
494
495 build_intrinsic(gallivm->builder,
496 "llvm.AMDGPU.kill",
497 LLVMVoidTypeInContext(gallivm->context),
498 &arg, 1, 0);
499 } else {
500 build_intrinsic(gallivm->builder,
501 "llvm.AMDGPU.kilp",
502 LLVMVoidTypeInContext(gallivm->context),
503 NULL, 0, 0);
504 }
505}
506
Tom Stellarda75c6162012-01-06 17:38:37 -0500507/* XXX: This is partially implemented for VS only at this point. It is not complete */
508static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
509{
510 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200511 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500512 struct lp_build_context * base = &bld_base->base;
513 struct lp_build_context * uint =
514 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
515 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100516 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500517 LLVMValueRef last_args[9] = { 0 };
Christian König35088152012-08-01 22:35:24 +0200518 unsigned color_count = 0;
519 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100520 int depth_index = -1, stencil_index = -1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500521
522 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500523 struct tgsi_full_declaration *d =
524 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500525 unsigned target;
526 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500527 int i;
528
529 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100530
531 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
532 parse->FullToken.FullProperty.Property.PropertyName ==
533 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
534 shader->fs_write_all = TRUE;
535
Tom Stellarda75c6162012-01-06 17:38:37 -0500536 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
537 continue;
538
539 switch (d->Declaration.File) {
540 case TGSI_FILE_INPUT:
541 i = shader->ninput++;
542 shader->input[i].name = d->Semantic.Name;
543 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200544 shader->input[i].interpolate = d->Interp.Interpolate;
545 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200546 continue;
547
Tom Stellarda75c6162012-01-06 17:38:37 -0500548 case TGSI_FILE_OUTPUT:
549 i = shader->noutput++;
550 shader->output[i].name = d->Semantic.Name;
551 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200552 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500553 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500554
Christian König35088152012-08-01 22:35:24 +0200555 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500556 continue;
Christian König35088152012-08-01 22:35:24 +0200557 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500558
559 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500560 /* Select the correct target */
561 switch(d->Semantic.Name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400562 case TGSI_SEMANTIC_PSIZE:
Tom Stellarda75c6162012-01-06 17:38:37 -0500563 target = V_008DFC_SQ_EXP_POS;
564 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100565 case TGSI_SEMANTIC_POSITION:
566 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
567 target = V_008DFC_SQ_EXP_POS;
568 break;
569 } else {
570 depth_index = index;
571 continue;
572 }
573 case TGSI_SEMANTIC_STENCIL:
574 stencil_index = index;
575 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500576 case TGSI_SEMANTIC_COLOR:
577 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200578 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500579 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200580 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500581 param_count++;
582 } else {
583 target = V_008DFC_SQ_EXP_MRT + color_count;
Michel Dänzer7708a862012-11-02 15:57:30 +0100584 if (color_count == 0 &&
585 si_shader_ctx->key.alpha_func != PIPE_FUNC_ALWAYS)
586 si_alpha_test(bld_base, index);
587
Tom Stellarda75c6162012-01-06 17:38:37 -0500588 color_count++;
589 }
590 break;
Michel Dänzer30b30372012-09-06 17:53:04 +0200591 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500592 case TGSI_SEMANTIC_GENERIC:
593 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200594 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500595 param_count++;
596 break;
597 default:
598 target = 0;
599 fprintf(stderr,
600 "Warning: SI unhandled output type:%d\n",
601 d->Semantic.Name);
602 }
603
Michel Dänzer26c71392012-08-24 12:03:11 +0200604 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500605
606 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ?
607 (d->Semantic.Name == TGSI_SEMANTIC_POSITION) :
608 (d->Semantic.Name == TGSI_SEMANTIC_COLOR)) {
609 if (last_args[0]) {
610 lp_build_intrinsic(base->gallivm->builder,
611 "llvm.SI.export",
612 LLVMVoidTypeInContext(base->gallivm->context),
613 last_args, 9);
614 }
615
616 memcpy(last_args, args, sizeof(args));
617 } else {
618 lp_build_intrinsic(base->gallivm->builder,
619 "llvm.SI.export",
620 LLVMVoidTypeInContext(base->gallivm->context),
621 args, 9);
622 }
623
624 }
625 }
626
Michel Dänzer1a616c12012-11-13 17:35:09 +0100627 if (depth_index >= 0 || stencil_index >= 0) {
628 LLVMValueRef out_ptr;
629 unsigned mask = 0;
630
631 /* Specify the target we are exporting */
632 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
633
634 if (depth_index >= 0) {
635 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
636 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
637 mask |= 0x1;
638
639 if (stencil_index < 0) {
640 args[6] =
641 args[7] =
642 args[8] = args[5];
643 }
644 }
645
646 if (stencil_index >= 0) {
647 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
648 args[7] =
649 args[8] =
650 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
651 mask |= 0x2;
652
653 if (depth_index < 0)
654 args[5] = args[6];
655 }
656
657 /* Specify which components to enable */
658 args[0] = lp_build_const_int32(base->gallivm, mask);
659
660 args[1] =
661 args[2] =
662 args[4] = uint->zero;
663
664 if (last_args[0])
665 lp_build_intrinsic(base->gallivm->builder,
666 "llvm.SI.export",
667 LLVMVoidTypeInContext(base->gallivm->context),
668 args, 9);
669 else
670 memcpy(last_args, args, sizeof(args));
671 }
672
Christian Königf18fd252012-07-25 21:58:46 +0200673 if (!last_args[0]) {
674 assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
675
676 /* Specify which components to enable */
677 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
678
679 /* Specify the target we are exporting */
680 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
681
682 /* Set COMPR flag to zero to export data as 32-bit */
683 last_args[4] = uint->zero;
684
685 /* dummy bits */
686 last_args[5]= uint->zero;
687 last_args[6]= uint->zero;
688 last_args[7]= uint->zero;
689 last_args[8]= uint->zero;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100690
691 si_shader_ctx->shader->spi_shader_col_format |=
692 V_028714_SPI_SHADER_32_ABGR;
Christian Königf18fd252012-07-25 21:58:46 +0200693 }
694
Tom Stellarda75c6162012-01-06 17:38:37 -0500695 /* Specify whether the EXEC mask represents the valid mask */
696 last_args[1] = lp_build_const_int32(base->gallivm,
697 si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
698
Michel Dänzerc8402702013-02-12 18:37:22 +0100699 if (shader->fs_write_all && shader->nr_cbufs > 1) {
700 int i;
701
702 /* Specify that this is not yet the last export */
703 last_args[2] = lp_build_const_int32(base->gallivm, 0);
704
705 for (i = 1; i < shader->nr_cbufs; i++) {
706 /* Specify the target we are exporting */
707 last_args[3] = lp_build_const_int32(base->gallivm,
708 V_008DFC_SQ_EXP_MRT + i);
709
710 lp_build_intrinsic(base->gallivm->builder,
711 "llvm.SI.export",
712 LLVMVoidTypeInContext(base->gallivm->context),
713 last_args, 9);
714
715 si_shader_ctx->shader->spi_shader_col_format |=
716 si_shader_ctx->shader->spi_shader_col_format << 4;
717 }
718
719 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
720 }
721
Tom Stellarda75c6162012-01-06 17:38:37 -0500722 /* Specify that this is the last export */
723 last_args[2] = lp_build_const_int32(base->gallivm, 1);
724
725 lp_build_intrinsic(base->gallivm->builder,
726 "llvm.SI.export",
727 LLVMVoidTypeInContext(base->gallivm->context),
728 last_args, 9);
729
730/* XXX: Look up what this function does */
731/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
732}
733
734static void tex_fetch_args(
735 struct lp_build_tgsi_context * bld_base,
736 struct lp_build_emit_data * emit_data)
737{
Christian König55fe5cc2013-03-04 16:30:06 +0100738 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100739 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200740 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100741 unsigned opcode = inst->Instruction.Opcode;
742 unsigned target = inst->Texture.Texture;
Tom Stellard467f5162012-05-16 15:15:35 -0400743 LLVMValueRef ptr;
744 LLVMValueRef offset;
Michel Dänzer120efee2013-01-25 12:10:11 +0100745 LLVMValueRef coords[4];
746 LLVMValueRef address[16];
747 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100748 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400749
Tom Stellarda75c6162012-01-06 17:38:37 -0500750 /* WriteMask */
Christian König250b7fd2012-08-01 23:18:14 +0200751 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
752 emit_data->args[0] = lp_build_const_int32(bld_base->base.gallivm, 0xf);
Tom Stellarda75c6162012-01-06 17:38:37 -0500753
Michel Dänzer120efee2013-01-25 12:10:11 +0100754 /* Fetch and project texture coordinates */
755 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100756 for (chan = 0; chan < 3; chan++ ) {
757 coords[chan] = lp_build_emit_fetch(bld_base,
758 emit_data->inst, 0,
759 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100760 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200761 coords[chan] = lp_build_emit_llvm_binary(bld_base,
762 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100763 coords[chan],
764 coords[3]);
765 }
766
Michel Dänzer120efee2013-01-25 12:10:11 +0100767 if (opcode == TGSI_OPCODE_TXP)
768 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500769
Michel Dänzer120efee2013-01-25 12:10:11 +0100770 /* Pack LOD bias value */
771 if (opcode == TGSI_OPCODE_TXB)
772 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400773
Michel Dänzer120efee2013-01-25 12:10:11 +0100774 if ((target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE) &&
775 opcode != TGSI_OPCODE_TXQ)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100776 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100777
778 /* Pack depth comparison value */
779 switch (target) {
780 case TGSI_TEXTURE_SHADOW1D:
781 case TGSI_TEXTURE_SHADOW1D_ARRAY:
782 case TGSI_TEXTURE_SHADOW2D:
783 case TGSI_TEXTURE_SHADOWRECT:
784 address[count++] = coords[2];
785 break;
786 case TGSI_TEXTURE_SHADOWCUBE:
787 case TGSI_TEXTURE_SHADOW2D_ARRAY:
788 address[count++] = coords[3];
789 break;
790 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
791 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +0100792 }
793
Michel Dänzer120efee2013-01-25 12:10:11 +0100794 /* Pack texture coordinates */
795 address[count++] = coords[0];
796 switch (target) {
797 case TGSI_TEXTURE_2D:
798 case TGSI_TEXTURE_2D_ARRAY:
799 case TGSI_TEXTURE_3D:
800 case TGSI_TEXTURE_CUBE:
801 case TGSI_TEXTURE_RECT:
802 case TGSI_TEXTURE_SHADOW2D:
803 case TGSI_TEXTURE_SHADOWRECT:
804 case TGSI_TEXTURE_SHADOW2D_ARRAY:
805 case TGSI_TEXTURE_SHADOWCUBE:
806 case TGSI_TEXTURE_2D_MSAA:
807 case TGSI_TEXTURE_2D_ARRAY_MSAA:
808 case TGSI_TEXTURE_CUBE_ARRAY:
809 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
810 address[count++] = coords[1];
811 }
812 switch (target) {
813 case TGSI_TEXTURE_3D:
814 case TGSI_TEXTURE_CUBE:
815 case TGSI_TEXTURE_SHADOWCUBE:
816 case TGSI_TEXTURE_CUBE_ARRAY:
817 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
818 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +0100819 }
820
Michel Dänzer120efee2013-01-25 12:10:11 +0100821 /* Pack array slice */
822 switch (target) {
823 case TGSI_TEXTURE_1D_ARRAY:
824 address[count++] = coords[1];
825 }
826 switch (target) {
827 case TGSI_TEXTURE_2D_ARRAY:
828 case TGSI_TEXTURE_2D_ARRAY_MSAA:
829 case TGSI_TEXTURE_SHADOW2D_ARRAY:
830 address[count++] = coords[2];
831 }
832 switch (target) {
833 case TGSI_TEXTURE_CUBE_ARRAY:
834 case TGSI_TEXTURE_SHADOW1D_ARRAY:
835 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
836 address[count++] = coords[3];
837 }
838
839 /* Pack LOD */
840 if (opcode == TGSI_OPCODE_TXL)
841 address[count++] = coords[3];
842
843 if (count > 16) {
844 assert(!"Cannot handle more than 16 texture address parameters");
845 count = 16;
846 }
847
848 for (chan = 0; chan < count; chan++ ) {
849 address[chan] = LLVMBuildBitCast(gallivm->builder,
850 address[chan],
851 LLVMInt32TypeInContext(gallivm->context),
852 "");
853 }
854
855 /* Pad to power of two vector */
856 while (count < util_next_power_of_two(count))
857 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
858
Michel Dänzer120efee2013-01-25 12:10:11 +0100859 emit_data->args[1] = lp_build_gather_values(gallivm, address, count);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100860
Tom Stellarda75c6162012-01-06 17:38:37 -0500861 /* Resource */
Christian König55fe5cc2013-03-04 16:30:06 +0100862 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
Tom Stellard467f5162012-05-16 15:15:35 -0400863 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200864 emit_data->inst->Src[1].Register.Index);
Christian König206f0592013-03-20 14:37:21 +0100865 emit_data->args[2] = build_indexed_load(si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400866 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500867
868 /* Sampler */
Christian König55fe5cc2013-03-04 16:30:06 +0100869 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
Tom Stellard467f5162012-05-16 15:15:35 -0400870 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200871 emit_data->inst->Src[1].Register.Index);
Christian König206f0592013-03-20 14:37:21 +0100872 emit_data->args[3] = build_indexed_load(si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400873 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500874
875 /* Dimensions */
Michel Dänzer120efee2013-01-25 12:10:11 +0100876 emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm, target);
Tom Stellarda75c6162012-01-06 17:38:37 -0500877
Michel Dänzer6eb0d3d2012-11-30 11:38:24 +0100878 emit_data->arg_count = 5;
Tom Stellarda75c6162012-01-06 17:38:37 -0500879 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
880 * the writemask are clear */
881 emit_data->dst_type = LLVMVectorType(
882 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
883 4);
884}
885
Michel Dänzer07eddc42013-02-06 15:43:10 +0100886static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
887 struct lp_build_tgsi_context * bld_base,
888 struct lp_build_emit_data * emit_data)
889{
890 struct lp_build_context * base = &bld_base->base;
891 char intr_name[23];
892
893 sprintf(intr_name, "%sv%ui32", action->intr_name,
894 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[1])));
895
Christian König44e32242013-03-20 12:10:35 +0100896 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +0100897 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +0100898 emit_data->args, emit_data->arg_count,
899 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +0100900}
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önig206f0592013-03-20 14:37:21 +0100920static void create_meta_data(struct si_shader_context *si_shader_ctx)
921{
922 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
923 LLVMValueRef args[3];
924
925 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
926 args[1] = 0;
927 args[2] = lp_build_const_int32(gallivm, 1);
928
929 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
930}
931
Christian König55fe5cc2013-03-04 16:30:06 +0100932static void create_function(struct si_shader_context *si_shader_ctx)
933{
934 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
Christian König0666ffd2013-03-05 15:07:39 +0100935 LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
Christian König55fe5cc2013-03-04 16:30:06 +0100936 unsigned i;
937
Christian König55fe5cc2013-03-04 16:30:06 +0100938 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +0100939 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100940 f32 = LLVMFloatTypeInContext(gallivm->context);
941 v2i32 = LLVMVectorType(i32, 2);
942 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +0100943
Christian Königf5298b02013-02-28 14:50:07 +0100944 params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
945 params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
Christian König55fe5cc2013-03-04 16:30:06 +0100946 params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
947
Christian Königc4973212013-03-05 12:14:02 +0100948 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Christian König55fe5cc2013-03-04 16:30:06 +0100949 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER];
Christian Königc4973212013-03-05 12:14:02 +0100950 params[SI_PARAM_VERTEX_INDEX] = i32;
951 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 5);
Christian König0666ffd2013-03-05 15:07:39 +0100952
Christian Königc4973212013-03-05 12:14:02 +0100953 } else {
Christian König0666ffd2013-03-05 15:07:39 +0100954 params[SI_PARAM_PRIM_MASK] = i32;
955 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
956 params[SI_PARAM_PERSP_CENTER] = v2i32;
957 params[SI_PARAM_PERSP_CENTROID] = v2i32;
958 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
959 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
960 params[SI_PARAM_LINEAR_CENTER] = v2i32;
961 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
962 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
963 params[SI_PARAM_POS_X_FLOAT] = f32;
964 params[SI_PARAM_POS_Y_FLOAT] = f32;
965 params[SI_PARAM_POS_Z_FLOAT] = f32;
966 params[SI_PARAM_POS_W_FLOAT] = f32;
967 params[SI_PARAM_FRONT_FACE] = f32;
968 params[SI_PARAM_ANCILLARY] = f32;
969 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
970 params[SI_PARAM_POS_FIXED_PT] = f32;
971 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20);
Christian Königc4973212013-03-05 12:14:02 +0100972 }
Christian König55fe5cc2013-03-04 16:30:06 +0100973
974 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
975 for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) {
976 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
977 LLVMAddAttribute(P, LLVMInRegAttribute);
978 }
979}
Tom Stellarda75c6162012-01-06 17:38:37 -0500980
981int si_pipe_shader_create(
982 struct pipe_context *ctx,
Michel Dänzer44ef0332012-10-05 16:59:10 +0200983 struct si_pipe_shader *shader,
984 struct si_shader_key key)
Tom Stellarda75c6162012-01-06 17:38:37 -0500985{
986 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200987 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -0500988 struct si_shader_context si_shader_ctx;
989 struct tgsi_shader_info shader_info;
990 struct lp_build_tgsi_context * bld_base;
991 LLVMModuleRef mod;
992 unsigned char * inst_bytes;
993 unsigned inst_byte_count;
994 unsigned i;
Christian Königd51b9b72012-07-24 18:50:49 +0200995 uint32_t *ptr;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200996 bool dump;
997
998 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -0500999
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001000 assert(shader->shader.noutput == 0);
1001 assert(shader->shader.ninterp == 0);
1002 assert(shader->shader.ninput == 0);
1003
Michel Dänzercfebaf92012-08-31 19:04:08 +02001004 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001005 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1006 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1007
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001008 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzere44dfd42012-11-07 17:33:08 +01001009 shader->shader.uses_kill = shader_info.uses_kill;
Tom Stellarda75c6162012-01-06 17:38:37 -05001010 bld_base->info = &shader_info;
1011 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001012 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1013
1014 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001015 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001016 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001017 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001018
1019 si_shader_ctx.radeon_bld.load_input = declare_input;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001020 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001021 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1022 si_shader_ctx.shader = shader;
Michel Dänzer44ef0332012-10-05 16:59:10 +02001023 si_shader_ctx.key = key;
Tom Stellarda75c6162012-01-06 17:38:37 -05001024 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
1025 si_shader_ctx.rctx = rctx;
1026
Christian König206f0592013-03-20 14:37:21 +01001027 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01001028 create_function(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001029
Christian König835098a2012-07-17 21:28:10 +02001030 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001031
Tom Stellard185fc9a2012-07-12 10:40:47 -04001032 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1033 * conversion fails. */
1034 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001035 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001036 }
1037
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001038 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001039 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
1040 return -EINVAL;
1041 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001042
1043 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1044
1045 mod = bld_base->base.gallivm->module;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001046 if (dump) {
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001047 LLVMDumpModule(mod);
1048 }
1049 radeon_llvm_compile(mod, &inst_bytes, &inst_byte_count, "SI", dump);
1050 if (dump) {
1051 fprintf(stderr, "SI CODE:\n");
1052 for (i = 0; i < inst_byte_count; i+=4 ) {
1053 fprintf(stderr, "%02x%02x%02x%02x\n", inst_bytes[i + 3],
1054 inst_bytes[i + 2], inst_bytes[i + 1],
1055 inst_bytes[i]);
1056 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001057 }
1058
1059 shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)inst_bytes);
1060 shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 4));
1061 shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 8));
1062
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001063 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001064 tgsi_parse_free(&si_shader_ctx.parse);
1065
1066 /* copy new shader */
Christian Königd51b9b72012-07-24 18:50:49 +02001067 si_resource_reference(&shader->bo, NULL);
1068 shader->bo = si_resource_create_custom(ctx->screen, PIPE_USAGE_IMMUTABLE,
1069 inst_byte_count - 12);
Tom Stellarda75c6162012-01-06 17:38:37 -05001070 if (shader->bo == NULL) {
Christian Königd51b9b72012-07-24 18:50:49 +02001071 return -ENOMEM;
Tom Stellarda75c6162012-01-06 17:38:37 -05001072 }
1073
Christian Königd51b9b72012-07-24 18:50:49 +02001074 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
1075 if (0 /*R600_BIG_ENDIAN*/) {
1076 for (i = 0; i < (inst_byte_count-12)/4; ++i) {
1077 ptr[i] = util_bswap32(*(uint32_t*)(inst_bytes+12 + i*4));
1078 }
1079 } else {
1080 memcpy(ptr, inst_bytes + 12, inst_byte_count - 12);
1081 }
1082 rctx->ws->buffer_unmap(shader->bo->cs_buf);
1083
Tom Stellarda75c6162012-01-06 17:38:37 -05001084 free(inst_bytes);
1085
1086 return 0;
1087}
1088
1089void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1090{
Christian Königfe412872012-07-24 18:47:19 +02001091 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001092}