blob: 05125289a1f4c3d4030fc6180208f04eb20c8196 [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"
43#include "tgsi/tgsi_dump.h"
44
45#include "radeonsi_pipe.h"
46#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020047#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050048#include "sid.h"
49
50#include <assert.h>
51#include <errno.h>
52#include <stdio.h>
53
Tom Stellarda75c6162012-01-06 17:38:37 -050054struct si_shader_context
55{
56 struct radeon_llvm_context radeon_bld;
57 struct r600_context *rctx;
58 struct tgsi_parse_context parse;
59 struct tgsi_token * tokens;
60 struct si_pipe_shader *shader;
Michel Dänzer44ef0332012-10-05 16:59:10 +020061 struct si_shader_key key;
Tom Stellarda75c6162012-01-06 17:38:37 -050062 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Christian König206f0592013-03-20 14:37:21 +010063 LLVMValueRef const_md;
Christian König0f6cf2b2013-03-15 15:53:25 +010064 LLVMValueRef const_resource;
65 LLVMValueRef *constants;
Christian König1c100182013-03-17 16:02:42 +010066 LLVMValueRef *resources;
67 LLVMValueRef *samplers;
Tom Stellarda75c6162012-01-06 17:38:37 -050068};
69
70static struct si_shader_context * si_shader_context(
71 struct lp_build_tgsi_context * bld_base)
72{
73 return (struct si_shader_context *)bld_base;
74}
75
76
77#define PERSPECTIVE_BASE 0
78#define LINEAR_BASE 9
79
80#define SAMPLE_OFFSET 0
81#define CENTER_OFFSET 2
82#define CENTROID_OFSET 4
83
84#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040085#define CONST_ADDR_SPACE 2
Tom Stellard89ece082012-05-29 11:36:29 -040086#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050087
Tom Stellard467f5162012-05-16 15:15:35 -040088/**
89 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
90 *
91 * @param offset The offset parameter specifies the number of
92 * elements to offset, not the number of bytes or dwords. An element is the
93 * the type pointed to by the base_ptr parameter (e.g. int is the element of
94 * an int* pointer)
95 *
96 * When LLVM lowers the load instruction, it will convert the element offset
97 * into a dword offset automatically.
98 *
99 */
100static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100101 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400102 LLVMValueRef base_ptr,
103 LLVMValueRef offset)
104{
Christian König206f0592013-03-20 14:37:21 +0100105 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400106
Christian König206f0592013-03-20 14:37:21 +0100107 LLVMValueRef computed_ptr = LLVMBuildGEP(
108 base->gallivm->builder, base_ptr, &offset, 1, "");
109
110 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
111 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
112 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400113}
114
Tom Stellarda75c6162012-01-06 17:38:37 -0500115static void declare_input_vs(
116 struct si_shader_context * si_shader_ctx,
117 unsigned input_index,
118 const struct tgsi_full_declaration *decl)
119{
120 LLVMValueRef t_list_ptr;
121 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400122 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500123 LLVMValueRef attribute_offset;
124 LLVMValueRef buffer_index_reg;
Tom Stellard467f5162012-05-16 15:15:35 -0400125 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500126 LLVMTypeRef vec4_type;
127 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500128 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Christian Königb15e3ae2012-07-25 11:22:59 +0200129 //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index];
Tom Stellarda75c6162012-01-06 17:38:37 -0500130 unsigned chan;
131
Tom Stellard467f5162012-05-16 15:15:35 -0400132 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100133 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500134
Christian Königb15e3ae2012-07-25 11:22:59 +0200135 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400136
Christian König206f0592013-03-20 14:37:21 +0100137 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400138
139 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200140 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500141
Christian Königc4973212013-03-05 12:14:02 +0100142 /* Load the buffer index, which is always stored in VGPR0
Tom Stellarda75c6162012-01-06 17:38:37 -0500143 * for Vertex Shaders */
Christian Könige4ed5872013-03-21 18:02:52 +0100144 buffer_index_reg = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_ID);
Tom Stellarda75c6162012-01-06 17:38:37 -0500145
146 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400147 args[0] = t_list;
148 args[1] = attribute_offset;
149 args[2] = buffer_index_reg;
Christian König44e32242013-03-20 12:10:35 +0100150 input = build_intrinsic(base->gallivm->builder,
151 "llvm.SI.vs.load.input", vec4_type, args, 3,
152 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500153
154 /* Break up the vec4 into individual components */
155 for (chan = 0; chan < 4; chan++) {
156 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
157 /* XXX: Use a helper function for this. There is one in
158 * tgsi_llvm.c. */
159 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
160 LLVMBuildExtractElement(base->gallivm->builder,
161 input, llvm_chan, "");
162 }
163}
164
165static void declare_input_fs(
166 struct si_shader_context * si_shader_ctx,
167 unsigned input_index,
168 const struct tgsi_full_declaration *decl)
169{
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200170 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500171 struct lp_build_context * base =
172 &si_shader_ctx->radeon_bld.soa.bld_base.base;
173 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400174 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100175 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
176
177 LLVMValueRef interp_param;
178 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500179
180 /* This value is:
181 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
182 * quad begins a new primitive. Bit 0 always needs
183 * to be unset)
184 * [32:16] ParamOffset
185 *
186 */
Christian König55fe5cc2013-03-04 16:30:06 +0100187 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200188 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500189
Christian König0666ffd2013-03-05 15:07:39 +0100190 unsigned chan;
191
Tom Stellard0fb1e682012-09-06 16:18:11 -0400192 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
193 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400194 unsigned soa_index =
195 radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellard0fb1e682012-09-06 16:18:11 -0400196 si_shader_ctx->radeon_bld.inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100197 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100198
199 if (chan == 3)
200 /* RCP for fragcoord.w */
201 si_shader_ctx->radeon_bld.inputs[soa_index] =
202 LLVMBuildFDiv(gallivm->builder,
203 lp_build_const_float(gallivm, 1.0f),
204 si_shader_ctx->radeon_bld.inputs[soa_index],
205 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400206 }
207 return;
208 }
209
Michel Dänzer97078b12012-09-25 12:41:31 +0200210 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
211 LLVMValueRef face, is_face_positive;
212
Christian König0666ffd2013-03-05 15:07:39 +0100213 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
214
Michel Dänzer97078b12012-09-25 12:41:31 +0200215 is_face_positive = LLVMBuildFCmp(gallivm->builder,
216 LLVMRealUGT, face,
217 lp_build_const_float(gallivm, 0.0f),
218 "");
219
220 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
221 LLVMBuildSelect(gallivm->builder,
222 is_face_positive,
223 lp_build_const_float(gallivm, 1.0f),
224 lp_build_const_float(gallivm, 0.0f),
225 "");
226 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
227 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
228 lp_build_const_float(gallivm, 0.0f);
229 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
230 lp_build_const_float(gallivm, 1.0f);
231
232 return;
233 }
234
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200235 shader->input[input_index].param_offset = shader->ninterp++;
236 attr_number = lp_build_const_int32(gallivm,
237 shader->input[input_index].param_offset);
238
Tom Stellarda75c6162012-01-06 17:38:37 -0500239 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200240 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500241 case TGSI_INTERPOLATE_COLOR:
Michel Dänzer18272c92013-02-13 12:54:13 +0100242 if (si_shader_ctx->key.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100243 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200244 } else {
245 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100246 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200247 else
Christian König0666ffd2013-03-05 15:07:39 +0100248 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200249 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500250 break;
251 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100252 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500253 break;
254 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200255 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100256 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200257 else
Christian König0666ffd2013-03-05 15:07:39 +0100258 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200259 break;
260 case TGSI_INTERPOLATE_PERSPECTIVE:
261 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100262 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200263 else
Christian König0666ffd2013-03-05 15:07:39 +0100264 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500265 break;
266 default:
267 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
268 return;
269 }
270
Christian König0666ffd2013-03-05 15:07:39 +0100271 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
272
Tom Stellarda75c6162012-01-06 17:38:37 -0500273 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200274 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
275 si_shader_ctx->key.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100276 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200277 LLVMValueRef face, is_face_positive;
278 LLVMValueRef back_attr_number =
279 lp_build_const_int32(gallivm,
280 shader->input[input_index].param_offset + 1);
281
Christian König0666ffd2013-03-05 15:07:39 +0100282 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
283
Michel Dänzer691f08d2012-09-06 18:03:38 +0200284 is_face_positive = LLVMBuildFCmp(gallivm->builder,
285 LLVMRealUGT, face,
286 lp_build_const_float(gallivm, 0.0f),
287 "");
288
Tom Stellarda75c6162012-01-06 17:38:37 -0500289 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100290 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200291 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
292 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
293 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
294 LLVMValueRef front, back;
295
296 args[0] = llvm_chan;
297 args[1] = attr_number;
298 front = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100299 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100300 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200301
302 args[1] = back_attr_number;
303 back = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100304 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100305 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200306
307 si_shader_ctx->radeon_bld.inputs[soa_index] =
308 LLVMBuildSelect(gallivm->builder,
309 is_face_positive,
310 front,
311 back,
312 "");
313 }
314
315 shader->ninterp++;
316 } else {
317 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100318 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200319 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
320 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
321 args[0] = llvm_chan;
322 args[1] = attr_number;
323 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100324 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200325 si_shader_ctx->radeon_bld.inputs[soa_index] =
326 build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100327 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100328 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200329 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500330 }
331}
332
333static void declare_input(
334 struct radeon_llvm_context * radeon_bld,
335 unsigned input_index,
336 const struct tgsi_full_declaration *decl)
337{
338 struct si_shader_context * si_shader_ctx =
339 si_shader_context(&radeon_bld->soa.bld_base);
340 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
341 declare_input_vs(si_shader_ctx, input_index, decl);
342 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
343 declare_input_fs(si_shader_ctx, input_index, decl);
344 } else {
345 fprintf(stderr, "Warning: Unsupported shader type,\n");
346 }
347}
348
Christian Könige4ed5872013-03-21 18:02:52 +0100349static void declare_system_value(
350 struct radeon_llvm_context * radeon_bld,
351 unsigned index,
352 const struct tgsi_full_declaration *decl)
353{
Christian Königcf9b31f2013-03-21 18:30:23 +0100354 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
355
Christian Könige4ed5872013-03-21 18:02:52 +0100356 LLVMValueRef value = 0;
357
358 switch (decl->Semantic.Name) {
359 case TGSI_SEMANTIC_INSTANCEID:
360 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_INSTANCE_ID);
Christian Königcf9b31f2013-03-21 18:30:23 +0100361 value = LLVMBuildAdd(gallivm->builder, value,
362 LLVMGetParam(radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
Christian Könige4ed5872013-03-21 18:02:52 +0100363 break;
364
365 case TGSI_SEMANTIC_VERTEXID:
366 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_VERTEX_ID);
367 break;
368
369 default:
370 assert(!"unknown system value");
371 return;
372 }
373
374 radeon_bld->system_values[index] = value;
375}
376
Tom Stellarda75c6162012-01-06 17:38:37 -0500377static LLVMValueRef fetch_constant(
378 struct lp_build_tgsi_context * bld_base,
379 const struct tgsi_full_src_register *reg,
380 enum tgsi_opcode_type type,
381 unsigned swizzle)
382{
Christian König55fe5cc2013-03-04 16:30:06 +0100383 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500384 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100385 const struct tgsi_ind_register *ireg = &reg->Indirect;
386 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500387
Christian Königf5298b02013-02-28 14:50:07 +0100388 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100389 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100390 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500391
Christian König8514f5a2013-02-04 17:46:42 +0100392 if (swizzle == LP_CHAN_ALL) {
393 unsigned chan;
394 LLVMValueRef values[4];
395 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
396 values[chan] = fetch_constant(bld_base, reg, type, chan);
397
398 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
399 }
400
Christian König0f6cf2b2013-03-15 15:53:25 +0100401 idx = reg->Register.Index * 4 + swizzle;
402 if (!reg->Register.Indirect)
403 return bitcast(bld_base, type, si_shader_ctx->constants[idx]);
Christian Königf5298b02013-02-28 14:50:07 +0100404
Christian König0f6cf2b2013-03-15 15:53:25 +0100405 args[0] = si_shader_ctx->const_resource;
406 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
407 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
408 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
409 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
410 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200411
Christian Königf5298b02013-02-28 14:50:07 +0100412 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100413 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500414
Christian Königf5298b02013-02-28 14:50:07 +0100415 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500416}
417
Michel Dänzer26c71392012-08-24 12:03:11 +0200418/* Initialize arguments for the shader export intrinsic */
419static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
420 struct tgsi_full_declaration *d,
421 unsigned index,
422 unsigned target,
423 LLVMValueRef *args)
424{
425 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
426 struct lp_build_context *uint =
427 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
428 struct lp_build_context *base = &bld_base->base;
429 unsigned compressed = 0;
430 unsigned chan;
431
Michel Dänzerf402acd2012-08-22 18:15:36 +0200432 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
433 int cbuf = target - V_008DFC_SQ_EXP_MRT;
434
435 if (cbuf >= 0 && cbuf < 8) {
Michel Dänzer44ef0332012-10-05 16:59:10 +0200436 compressed = (si_shader_ctx->key.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100437
438 if (compressed)
439 si_shader_ctx->shader->spi_shader_col_format |=
440 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
441 else
442 si_shader_ctx->shader->spi_shader_col_format |=
443 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200444 }
445 }
446
447 if (compressed) {
448 /* Pixel shader needs to pack output values before export */
449 for (chan = 0; chan < 2; chan++ ) {
450 LLVMValueRef *out_ptr =
451 si_shader_ctx->radeon_bld.soa.outputs[index];
452 args[0] = LLVMBuildLoad(base->gallivm->builder,
453 out_ptr[2 * chan], "");
454 args[1] = LLVMBuildLoad(base->gallivm->builder,
455 out_ptr[2 * chan + 1], "");
456 args[chan + 5] =
457 build_intrinsic(base->gallivm->builder,
458 "llvm.SI.packf16",
459 LLVMInt32TypeInContext(base->gallivm->context),
460 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100461 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100462 args[chan + 7] = args[chan + 5] =
463 LLVMBuildBitCast(base->gallivm->builder,
464 args[chan + 5],
465 LLVMFloatTypeInContext(base->gallivm->context),
466 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200467 }
468
469 /* Set COMPR flag */
470 args[4] = uint->one;
471 } else {
472 for (chan = 0; chan < 4; chan++ ) {
473 LLVMValueRef out_ptr =
474 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
475 /* +5 because the first output value will be
476 * the 6th argument to the intrinsic. */
477 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
478 out_ptr, "");
479 }
480
481 /* Clear COMPR flag */
482 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200483 }
484
485 /* XXX: This controls which components of the output
486 * registers actually get exported. (e.g bit 0 means export
487 * X component, bit 1 means export Y component, etc.) I'm
488 * hard coding this to 0xf for now. In the future, we might
489 * want to do something else. */
490 args[0] = lp_build_const_int32(base->gallivm, 0xf);
491
492 /* Specify whether the EXEC mask represents the valid mask */
493 args[1] = uint->zero;
494
495 /* Specify whether this is the last export */
496 args[2] = uint->zero;
497
498 /* Specify the target we are exporting */
499 args[3] = lp_build_const_int32(base->gallivm, target);
500
Michel Dänzer26c71392012-08-24 12:03:11 +0200501 /* XXX: We probably need to keep track of the output
502 * values, so we know what we are passing to the next
503 * stage. */
504}
505
Michel Dänzer7708a862012-11-02 15:57:30 +0100506static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
507 unsigned index)
508{
509 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
510 struct gallivm_state *gallivm = bld_base->base.gallivm;
511
512 if (si_shader_ctx->key.alpha_func != PIPE_FUNC_NEVER) {
513 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
514 LLVMValueRef alpha_pass =
515 lp_build_cmp(&bld_base->base,
516 si_shader_ctx->key.alpha_func,
517 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
518 lp_build_const_float(gallivm, si_shader_ctx->key.alpha_ref));
519 LLVMValueRef arg =
520 lp_build_select(&bld_base->base,
521 alpha_pass,
522 lp_build_const_float(gallivm, 1.0f),
523 lp_build_const_float(gallivm, -1.0f));
524
525 build_intrinsic(gallivm->builder,
526 "llvm.AMDGPU.kill",
527 LLVMVoidTypeInContext(gallivm->context),
528 &arg, 1, 0);
529 } else {
530 build_intrinsic(gallivm->builder,
531 "llvm.AMDGPU.kilp",
532 LLVMVoidTypeInContext(gallivm->context),
533 NULL, 0, 0);
534 }
535}
536
Tom Stellarda75c6162012-01-06 17:38:37 -0500537/* XXX: This is partially implemented for VS only at this point. It is not complete */
538static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
539{
540 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200541 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500542 struct lp_build_context * base = &bld_base->base;
543 struct lp_build_context * uint =
544 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
545 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100546 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500547 LLVMValueRef last_args[9] = { 0 };
Christian König35088152012-08-01 22:35:24 +0200548 unsigned color_count = 0;
549 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100550 int depth_index = -1, stencil_index = -1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500551
552 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500553 struct tgsi_full_declaration *d =
554 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500555 unsigned target;
556 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500557 int i;
558
559 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100560
561 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
562 parse->FullToken.FullProperty.Property.PropertyName ==
563 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
564 shader->fs_write_all = TRUE;
565
Tom Stellarda75c6162012-01-06 17:38:37 -0500566 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
567 continue;
568
569 switch (d->Declaration.File) {
570 case TGSI_FILE_INPUT:
571 i = shader->ninput++;
572 shader->input[i].name = d->Semantic.Name;
573 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200574 shader->input[i].interpolate = d->Interp.Interpolate;
575 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200576 continue;
577
Tom Stellarda75c6162012-01-06 17:38:37 -0500578 case TGSI_FILE_OUTPUT:
579 i = shader->noutput++;
580 shader->output[i].name = d->Semantic.Name;
581 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200582 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500583 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500584
Christian König35088152012-08-01 22:35:24 +0200585 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500586 continue;
Christian König35088152012-08-01 22:35:24 +0200587 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500588
589 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500590 /* Select the correct target */
591 switch(d->Semantic.Name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400592 case TGSI_SEMANTIC_PSIZE:
Tom Stellarda75c6162012-01-06 17:38:37 -0500593 target = V_008DFC_SQ_EXP_POS;
594 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100595 case TGSI_SEMANTIC_POSITION:
596 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
597 target = V_008DFC_SQ_EXP_POS;
598 break;
599 } else {
600 depth_index = index;
601 continue;
602 }
603 case TGSI_SEMANTIC_STENCIL:
604 stencil_index = index;
605 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500606 case TGSI_SEMANTIC_COLOR:
607 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200608 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500609 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200610 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500611 param_count++;
612 } else {
613 target = V_008DFC_SQ_EXP_MRT + color_count;
Michel Dänzer7708a862012-11-02 15:57:30 +0100614 if (color_count == 0 &&
615 si_shader_ctx->key.alpha_func != PIPE_FUNC_ALWAYS)
616 si_alpha_test(bld_base, index);
617
Tom Stellarda75c6162012-01-06 17:38:37 -0500618 color_count++;
619 }
620 break;
Michel Dänzer30b30372012-09-06 17:53:04 +0200621 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500622 case TGSI_SEMANTIC_GENERIC:
623 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200624 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500625 param_count++;
626 break;
627 default:
628 target = 0;
629 fprintf(stderr,
630 "Warning: SI unhandled output type:%d\n",
631 d->Semantic.Name);
632 }
633
Michel Dänzer26c71392012-08-24 12:03:11 +0200634 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500635
636 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ?
637 (d->Semantic.Name == TGSI_SEMANTIC_POSITION) :
638 (d->Semantic.Name == TGSI_SEMANTIC_COLOR)) {
639 if (last_args[0]) {
640 lp_build_intrinsic(base->gallivm->builder,
641 "llvm.SI.export",
642 LLVMVoidTypeInContext(base->gallivm->context),
643 last_args, 9);
644 }
645
646 memcpy(last_args, args, sizeof(args));
647 } else {
648 lp_build_intrinsic(base->gallivm->builder,
649 "llvm.SI.export",
650 LLVMVoidTypeInContext(base->gallivm->context),
651 args, 9);
652 }
653
654 }
655 }
656
Michel Dänzer1a616c12012-11-13 17:35:09 +0100657 if (depth_index >= 0 || stencil_index >= 0) {
658 LLVMValueRef out_ptr;
659 unsigned mask = 0;
660
661 /* Specify the target we are exporting */
662 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
663
664 if (depth_index >= 0) {
665 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
666 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
667 mask |= 0x1;
668
669 if (stencil_index < 0) {
670 args[6] =
671 args[7] =
672 args[8] = args[5];
673 }
674 }
675
676 if (stencil_index >= 0) {
677 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
678 args[7] =
679 args[8] =
680 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
681 mask |= 0x2;
682
683 if (depth_index < 0)
684 args[5] = args[6];
685 }
686
687 /* Specify which components to enable */
688 args[0] = lp_build_const_int32(base->gallivm, mask);
689
690 args[1] =
691 args[2] =
692 args[4] = uint->zero;
693
694 if (last_args[0])
695 lp_build_intrinsic(base->gallivm->builder,
696 "llvm.SI.export",
697 LLVMVoidTypeInContext(base->gallivm->context),
698 args, 9);
699 else
700 memcpy(last_args, args, sizeof(args));
701 }
702
Christian Königf18fd252012-07-25 21:58:46 +0200703 if (!last_args[0]) {
704 assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
705
706 /* Specify which components to enable */
707 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
708
709 /* Specify the target we are exporting */
710 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
711
712 /* Set COMPR flag to zero to export data as 32-bit */
713 last_args[4] = uint->zero;
714
715 /* dummy bits */
716 last_args[5]= uint->zero;
717 last_args[6]= uint->zero;
718 last_args[7]= uint->zero;
719 last_args[8]= uint->zero;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100720
721 si_shader_ctx->shader->spi_shader_col_format |=
722 V_028714_SPI_SHADER_32_ABGR;
Christian Königf18fd252012-07-25 21:58:46 +0200723 }
724
Tom Stellarda75c6162012-01-06 17:38:37 -0500725 /* Specify whether the EXEC mask represents the valid mask */
726 last_args[1] = lp_build_const_int32(base->gallivm,
727 si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
728
Michel Dänzerc8402702013-02-12 18:37:22 +0100729 if (shader->fs_write_all && shader->nr_cbufs > 1) {
730 int i;
731
732 /* Specify that this is not yet the last export */
733 last_args[2] = lp_build_const_int32(base->gallivm, 0);
734
735 for (i = 1; i < shader->nr_cbufs; i++) {
736 /* Specify the target we are exporting */
737 last_args[3] = lp_build_const_int32(base->gallivm,
738 V_008DFC_SQ_EXP_MRT + i);
739
740 lp_build_intrinsic(base->gallivm->builder,
741 "llvm.SI.export",
742 LLVMVoidTypeInContext(base->gallivm->context),
743 last_args, 9);
744
745 si_shader_ctx->shader->spi_shader_col_format |=
746 si_shader_ctx->shader->spi_shader_col_format << 4;
747 }
748
749 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
750 }
751
Tom Stellarda75c6162012-01-06 17:38:37 -0500752 /* Specify that this is the last export */
753 last_args[2] = lp_build_const_int32(base->gallivm, 1);
754
755 lp_build_intrinsic(base->gallivm->builder,
756 "llvm.SI.export",
757 LLVMVoidTypeInContext(base->gallivm->context),
758 last_args, 9);
759
760/* XXX: Look up what this function does */
761/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
762}
763
764static void tex_fetch_args(
765 struct lp_build_tgsi_context * bld_base,
766 struct lp_build_emit_data * emit_data)
767{
Christian König55fe5cc2013-03-04 16:30:06 +0100768 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100769 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200770 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100771 unsigned opcode = inst->Instruction.Opcode;
772 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +0100773 LLVMValueRef coords[4];
774 LLVMValueRef address[16];
775 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100776 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400777
Tom Stellarda75c6162012-01-06 17:38:37 -0500778 /* WriteMask */
Christian König250b7fd2012-08-01 23:18:14 +0200779 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
780 emit_data->args[0] = lp_build_const_int32(bld_base->base.gallivm, 0xf);
Tom Stellarda75c6162012-01-06 17:38:37 -0500781
Michel Dänzer120efee2013-01-25 12:10:11 +0100782 /* Fetch and project texture coordinates */
783 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100784 for (chan = 0; chan < 3; chan++ ) {
785 coords[chan] = lp_build_emit_fetch(bld_base,
786 emit_data->inst, 0,
787 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100788 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200789 coords[chan] = lp_build_emit_llvm_binary(bld_base,
790 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100791 coords[chan],
792 coords[3]);
793 }
794
Michel Dänzer120efee2013-01-25 12:10:11 +0100795 if (opcode == TGSI_OPCODE_TXP)
796 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500797
Michel Dänzer120efee2013-01-25 12:10:11 +0100798 /* Pack LOD bias value */
799 if (opcode == TGSI_OPCODE_TXB)
800 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400801
Michel Dänzer120efee2013-01-25 12:10:11 +0100802 if ((target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE) &&
803 opcode != TGSI_OPCODE_TXQ)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100804 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100805
806 /* Pack depth comparison value */
807 switch (target) {
808 case TGSI_TEXTURE_SHADOW1D:
809 case TGSI_TEXTURE_SHADOW1D_ARRAY:
810 case TGSI_TEXTURE_SHADOW2D:
811 case TGSI_TEXTURE_SHADOWRECT:
812 address[count++] = coords[2];
813 break;
814 case TGSI_TEXTURE_SHADOWCUBE:
815 case TGSI_TEXTURE_SHADOW2D_ARRAY:
816 address[count++] = coords[3];
817 break;
818 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
819 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +0100820 }
821
Michel Dänzer120efee2013-01-25 12:10:11 +0100822 /* Pack texture coordinates */
823 address[count++] = coords[0];
824 switch (target) {
825 case TGSI_TEXTURE_2D:
826 case TGSI_TEXTURE_2D_ARRAY:
827 case TGSI_TEXTURE_3D:
828 case TGSI_TEXTURE_CUBE:
829 case TGSI_TEXTURE_RECT:
830 case TGSI_TEXTURE_SHADOW2D:
831 case TGSI_TEXTURE_SHADOWRECT:
832 case TGSI_TEXTURE_SHADOW2D_ARRAY:
833 case TGSI_TEXTURE_SHADOWCUBE:
834 case TGSI_TEXTURE_2D_MSAA:
835 case TGSI_TEXTURE_2D_ARRAY_MSAA:
836 case TGSI_TEXTURE_CUBE_ARRAY:
837 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
838 address[count++] = coords[1];
839 }
840 switch (target) {
841 case TGSI_TEXTURE_3D:
842 case TGSI_TEXTURE_CUBE:
843 case TGSI_TEXTURE_SHADOWCUBE:
844 case TGSI_TEXTURE_CUBE_ARRAY:
845 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
846 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +0100847 }
848
Michel Dänzer120efee2013-01-25 12:10:11 +0100849 /* Pack array slice */
850 switch (target) {
851 case TGSI_TEXTURE_1D_ARRAY:
852 address[count++] = coords[1];
853 }
854 switch (target) {
855 case TGSI_TEXTURE_2D_ARRAY:
856 case TGSI_TEXTURE_2D_ARRAY_MSAA:
857 case TGSI_TEXTURE_SHADOW2D_ARRAY:
858 address[count++] = coords[2];
859 }
860 switch (target) {
861 case TGSI_TEXTURE_CUBE_ARRAY:
862 case TGSI_TEXTURE_SHADOW1D_ARRAY:
863 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
864 address[count++] = coords[3];
865 }
866
867 /* Pack LOD */
868 if (opcode == TGSI_OPCODE_TXL)
869 address[count++] = coords[3];
870
871 if (count > 16) {
872 assert(!"Cannot handle more than 16 texture address parameters");
873 count = 16;
874 }
875
876 for (chan = 0; chan < count; chan++ ) {
877 address[chan] = LLVMBuildBitCast(gallivm->builder,
878 address[chan],
879 LLVMInt32TypeInContext(gallivm->context),
880 "");
881 }
882
883 /* Pad to power of two vector */
884 while (count < util_next_power_of_two(count))
885 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
886
Michel Dänzer120efee2013-01-25 12:10:11 +0100887 emit_data->args[1] = lp_build_gather_values(gallivm, address, count);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100888
Tom Stellarda75c6162012-01-06 17:38:37 -0500889 /* Resource */
Christian König1c100182013-03-17 16:02:42 +0100890 emit_data->args[2] = si_shader_ctx->resources[emit_data->inst->Src[1].Register.Index];
Tom Stellarda75c6162012-01-06 17:38:37 -0500891
892 /* Sampler */
Christian König1c100182013-03-17 16:02:42 +0100893 emit_data->args[3] = si_shader_ctx->samplers[emit_data->inst->Src[1].Register.Index];
Tom Stellarda75c6162012-01-06 17:38:37 -0500894
895 /* Dimensions */
Michel Dänzer120efee2013-01-25 12:10:11 +0100896 emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm, target);
Tom Stellarda75c6162012-01-06 17:38:37 -0500897
Michel Dänzer6eb0d3d2012-11-30 11:38:24 +0100898 emit_data->arg_count = 5;
Tom Stellarda75c6162012-01-06 17:38:37 -0500899 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
900 * the writemask are clear */
901 emit_data->dst_type = LLVMVectorType(
902 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
903 4);
904}
905
Michel Dänzer07eddc42013-02-06 15:43:10 +0100906static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
907 struct lp_build_tgsi_context * bld_base,
908 struct lp_build_emit_data * emit_data)
909{
910 struct lp_build_context * base = &bld_base->base;
911 char intr_name[23];
912
913 sprintf(intr_name, "%sv%ui32", action->intr_name,
914 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[1])));
915
Christian König44e32242013-03-20 12:10:35 +0100916 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +0100917 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +0100918 emit_data->args, emit_data->arg_count,
919 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +0100920}
921
Tom Stellarda75c6162012-01-06 17:38:37 -0500922static const struct lp_build_tgsi_action tex_action = {
923 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +0100924 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100925 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -0500926};
927
Michel Dänzer3e205132012-11-06 17:39:01 +0100928static const struct lp_build_tgsi_action txb_action = {
929 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +0100930 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100931 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +0100932};
933
Michel Dänzer56ae9be2012-11-06 17:41:50 +0100934static const struct lp_build_tgsi_action txl_action = {
935 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +0100936 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100937 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +0100938};
939
Christian König206f0592013-03-20 14:37:21 +0100940static void create_meta_data(struct si_shader_context *si_shader_ctx)
941{
942 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
943 LLVMValueRef args[3];
944
945 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
946 args[1] = 0;
947 args[2] = lp_build_const_int32(gallivm, 1);
948
949 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
950}
951
Christian König55fe5cc2013-03-04 16:30:06 +0100952static void create_function(struct si_shader_context *si_shader_ctx)
953{
954 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
Christian König0666ffd2013-03-05 15:07:39 +0100955 LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
Christian König55fe5cc2013-03-04 16:30:06 +0100956 unsigned i;
957
Christian König55fe5cc2013-03-04 16:30:06 +0100958 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +0100959 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100960 f32 = LLVMFloatTypeInContext(gallivm->context);
961 v2i32 = LLVMVectorType(i32, 2);
962 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +0100963
Christian Königf5298b02013-02-28 14:50:07 +0100964 params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
965 params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
Christian König55fe5cc2013-03-04 16:30:06 +0100966 params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
967
Christian Königc4973212013-03-05 12:14:02 +0100968 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Christian König55fe5cc2013-03-04 16:30:06 +0100969 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER];
Christian Königcf9b31f2013-03-21 18:30:23 +0100970 params[SI_PARAM_START_INSTANCE] = i32;
Christian Könige4ed5872013-03-21 18:02:52 +0100971 params[SI_PARAM_VERTEX_ID] = i32;
972 params[SI_PARAM_DUMMY_0] = i32;
973 params[SI_PARAM_DUMMY_1] = i32;
974 params[SI_PARAM_INSTANCE_ID] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +0100975 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 9);
Christian König0666ffd2013-03-05 15:07:39 +0100976
Christian Königc4973212013-03-05 12:14:02 +0100977 } else {
Christian König0666ffd2013-03-05 15:07:39 +0100978 params[SI_PARAM_PRIM_MASK] = i32;
979 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
980 params[SI_PARAM_PERSP_CENTER] = v2i32;
981 params[SI_PARAM_PERSP_CENTROID] = v2i32;
982 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
983 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
984 params[SI_PARAM_LINEAR_CENTER] = v2i32;
985 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
986 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
987 params[SI_PARAM_POS_X_FLOAT] = f32;
988 params[SI_PARAM_POS_Y_FLOAT] = f32;
989 params[SI_PARAM_POS_Z_FLOAT] = f32;
990 params[SI_PARAM_POS_W_FLOAT] = f32;
991 params[SI_PARAM_FRONT_FACE] = f32;
992 params[SI_PARAM_ANCILLARY] = f32;
993 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
994 params[SI_PARAM_POS_FIXED_PT] = f32;
995 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20);
Christian Königc4973212013-03-05 12:14:02 +0100996 }
Christian König55fe5cc2013-03-04 16:30:06 +0100997
998 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
999 for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) {
1000 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
1001 LLVMAddAttribute(P, LLVMInRegAttribute);
1002 }
Christian Königcf9b31f2013-03-21 18:30:23 +01001003
1004 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
1005 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1006 SI_PARAM_START_INSTANCE);
1007 LLVMAddAttribute(P, LLVMInRegAttribute);
1008 }
Christian König55fe5cc2013-03-04 16:30:06 +01001009}
Tom Stellarda75c6162012-01-06 17:38:37 -05001010
Christian König0f6cf2b2013-03-15 15:53:25 +01001011static void preload_constants(struct si_shader_context *si_shader_ctx)
1012{
1013 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1014 struct gallivm_state * gallivm = bld_base->base.gallivm;
1015 const struct tgsi_shader_info * info = bld_base->info;
1016
1017 unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1;
1018
1019 LLVMValueRef ptr;
1020
1021 if (num_const == 0)
1022 return;
1023
1024 /* Allocate space for the constant values */
1025 si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef));
1026
1027 /* Load the resource descriptor */
1028 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1029 si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
1030
1031 /* Load the constants, we rely on the code sinking to do the rest */
1032 for (i = 0; i < num_const * 4; ++i) {
1033 LLVMValueRef args[2] = {
1034 si_shader_ctx->const_resource,
1035 lp_build_const_int32(gallivm, i * 4)
1036 };
1037 si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1038 bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1039 }
1040}
1041
Christian König1c100182013-03-17 16:02:42 +01001042static void preload_samplers(struct si_shader_context *si_shader_ctx)
1043{
1044 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1045 struct gallivm_state * gallivm = bld_base->base.gallivm;
1046 const struct tgsi_shader_info * info = bld_base->info;
1047
1048 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1049
1050 LLVMValueRef res_ptr, samp_ptr;
1051 LLVMValueRef offset;
1052
1053 if (num_samplers == 0)
1054 return;
1055
1056 /* Allocate space for the values */
1057 si_shader_ctx->resources = CALLOC(num_samplers, sizeof(LLVMValueRef));
1058 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1059
1060 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1061 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1062
1063 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1064 for (i = 0; i < num_samplers; ++i) {
1065
1066 /* Resource */
1067 offset = lp_build_const_int32(gallivm, i);
1068 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1069
1070 /* Sampler */
1071 offset = lp_build_const_int32(gallivm, i);
1072 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
1073 }
1074}
1075
Tom Stellarda75c6162012-01-06 17:38:37 -05001076int si_pipe_shader_create(
1077 struct pipe_context *ctx,
Michel Dänzer44ef0332012-10-05 16:59:10 +02001078 struct si_pipe_shader *shader,
1079 struct si_shader_key key)
Tom Stellarda75c6162012-01-06 17:38:37 -05001080{
1081 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001082 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05001083 struct si_shader_context si_shader_ctx;
1084 struct tgsi_shader_info shader_info;
1085 struct lp_build_tgsi_context * bld_base;
1086 LLVMModuleRef mod;
1087 unsigned char * inst_bytes;
1088 unsigned inst_byte_count;
1089 unsigned i;
Christian Königd51b9b72012-07-24 18:50:49 +02001090 uint32_t *ptr;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001091 bool dump;
1092
1093 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -05001094
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001095 assert(shader->shader.noutput == 0);
1096 assert(shader->shader.ninterp == 0);
1097 assert(shader->shader.ninput == 0);
1098
Michel Dänzercfebaf92012-08-31 19:04:08 +02001099 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001100 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1101 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1102
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001103 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzere44dfd42012-11-07 17:33:08 +01001104 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01001105 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001106 bld_base->info = &shader_info;
1107 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001108 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1109
1110 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001111 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001112 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001113 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001114
1115 si_shader_ctx.radeon_bld.load_input = declare_input;
Christian Könige4ed5872013-03-21 18:02:52 +01001116 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001117 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001118 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1119 si_shader_ctx.shader = shader;
Michel Dänzer44ef0332012-10-05 16:59:10 +02001120 si_shader_ctx.key = key;
Tom Stellarda75c6162012-01-06 17:38:37 -05001121 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
1122 si_shader_ctx.rctx = rctx;
1123
Christian König206f0592013-03-20 14:37:21 +01001124 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01001125 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01001126 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01001127 preload_samplers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001128
Christian König835098a2012-07-17 21:28:10 +02001129 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001130
Tom Stellard185fc9a2012-07-12 10:40:47 -04001131 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1132 * conversion fails. */
1133 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001134 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001135 }
1136
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001137 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001138 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Christian König0f6cf2b2013-03-15 15:53:25 +01001139 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001140 FREE(si_shader_ctx.resources);
1141 FREE(si_shader_ctx.samplers);
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001142 return -EINVAL;
1143 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001144
1145 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1146
1147 mod = bld_base->base.gallivm->module;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001148 if (dump) {
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001149 LLVMDumpModule(mod);
1150 }
1151 radeon_llvm_compile(mod, &inst_bytes, &inst_byte_count, "SI", dump);
1152 if (dump) {
1153 fprintf(stderr, "SI CODE:\n");
1154 for (i = 0; i < inst_byte_count; i+=4 ) {
1155 fprintf(stderr, "%02x%02x%02x%02x\n", inst_bytes[i + 3],
1156 inst_bytes[i + 2], inst_bytes[i + 1],
1157 inst_bytes[i]);
1158 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001159 }
1160
1161 shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)inst_bytes);
1162 shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 4));
1163 shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 8));
1164
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001165 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001166 tgsi_parse_free(&si_shader_ctx.parse);
1167
1168 /* copy new shader */
Christian Königd51b9b72012-07-24 18:50:49 +02001169 si_resource_reference(&shader->bo, NULL);
1170 shader->bo = si_resource_create_custom(ctx->screen, PIPE_USAGE_IMMUTABLE,
1171 inst_byte_count - 12);
Tom Stellarda75c6162012-01-06 17:38:37 -05001172 if (shader->bo == NULL) {
Christian König0f6cf2b2013-03-15 15:53:25 +01001173 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001174 FREE(si_shader_ctx.resources);
1175 FREE(si_shader_ctx.samplers);
Christian Königd51b9b72012-07-24 18:50:49 +02001176 return -ENOMEM;
Tom Stellarda75c6162012-01-06 17:38:37 -05001177 }
1178
Christian Königd51b9b72012-07-24 18:50:49 +02001179 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
1180 if (0 /*R600_BIG_ENDIAN*/) {
1181 for (i = 0; i < (inst_byte_count-12)/4; ++i) {
1182 ptr[i] = util_bswap32(*(uint32_t*)(inst_bytes+12 + i*4));
1183 }
1184 } else {
1185 memcpy(ptr, inst_bytes + 12, inst_byte_count - 12);
1186 }
1187 rctx->ws->buffer_unmap(shader->bo->cs_buf);
1188
Christian König0f6cf2b2013-03-15 15:53:25 +01001189 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001190 FREE(si_shader_ctx.resources);
1191 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05001192 free(inst_bytes);
1193
1194 return 0;
1195}
1196
1197void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1198{
Christian Königfe412872012-07-24 18:47:19 +02001199 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001200}