blob: aec5c2ea2227d811183c0eaf085d650bc4159dce [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"
34#include "gallivm/lp_bld_tgsi.h"
35#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040036#include "radeon_llvm_emit.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050037#include "tgsi/tgsi_info.h"
38#include "tgsi/tgsi_parse.h"
39#include "tgsi/tgsi_scan.h"
40#include "tgsi/tgsi_dump.h"
41
42#include "radeonsi_pipe.h"
43#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020044#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050045#include "sid.h"
46
47#include <assert.h>
48#include <errno.h>
49#include <stdio.h>
50
Tom Stellarda75c6162012-01-06 17:38:37 -050051struct si_shader_context
52{
53 struct radeon_llvm_context radeon_bld;
54 struct r600_context *rctx;
55 struct tgsi_parse_context parse;
56 struct tgsi_token * tokens;
57 struct si_pipe_shader *shader;
Michel Dänzer44ef0332012-10-05 16:59:10 +020058 struct si_shader_key key;
Tom Stellarda75c6162012-01-06 17:38:37 -050059 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Michel Dänzercfebaf92012-08-31 19:04:08 +020060 unsigned ninput_emitted;
Tom Stellarda75c6162012-01-06 17:38:37 -050061/* struct list_head inputs; */
62/* unsigned * input_mappings *//* From TGSI to SI hw */
63/* struct tgsi_shader_info info;*/
64};
65
66static struct si_shader_context * si_shader_context(
67 struct lp_build_tgsi_context * bld_base)
68{
69 return (struct si_shader_context *)bld_base;
70}
71
72
73#define PERSPECTIVE_BASE 0
74#define LINEAR_BASE 9
75
76#define SAMPLE_OFFSET 0
77#define CENTER_OFFSET 2
78#define CENTROID_OFSET 4
79
80#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040081#define CONST_ADDR_SPACE 2
Tom Stellard89ece082012-05-29 11:36:29 -040082#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050083
84enum sgpr_type {
Tom Stellard467f5162012-05-16 15:15:35 -040085 SGPR_CONST_PTR_F32,
86 SGPR_CONST_PTR_V4I32,
87 SGPR_CONST_PTR_V8I32,
Tom Stellarda75c6162012-01-06 17:38:37 -050088 SGPR_I32,
Tom Stellard467f5162012-05-16 15:15:35 -040089 SGPR_I64
Tom Stellarda75c6162012-01-06 17:38:37 -050090};
91
Tom Stellard467f5162012-05-16 15:15:35 -040092/**
93 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
94 *
95 * @param offset The offset parameter specifies the number of
96 * elements to offset, not the number of bytes or dwords. An element is the
97 * the type pointed to by the base_ptr parameter (e.g. int is the element of
98 * an int* pointer)
99 *
100 * When LLVM lowers the load instruction, it will convert the element offset
101 * into a dword offset automatically.
102 *
103 */
104static LLVMValueRef build_indexed_load(
105 struct gallivm_state * gallivm,
106 LLVMValueRef base_ptr,
107 LLVMValueRef offset)
108{
109 LLVMValueRef computed_ptr = LLVMBuildGEP(
110 gallivm->builder, base_ptr, &offset, 1, "");
111
112 return LLVMBuildLoad(gallivm->builder, computed_ptr, "");
113}
114
Tom Stellard89ece082012-05-29 11:36:29 -0400115/**
116 * Load a value stored in one of the user SGPRs
117 *
118 * @param sgpr This is the sgpr to load the value from. If you need to load a
119 * value that is stored in consecutive SGPR registers (e.g. a 64-bit pointer),
120 * then you should pass the index of the first SGPR that holds the value. For
121 * example, if you want to load a pointer that is stored in SGPRs 2 and 3, then
122 * use pass 2 for the sgpr parameter.
123 *
124 * The value of the sgpr parameter must also be aligned to the width of the type
125 * being loaded, so that the sgpr parameter is divisible by the dword width of the
126 * type. For example, if the value being loaded is two dwords wide, then the sgpr
127 * parameter must be divisible by two.
Tom Stellard467f5162012-05-16 15:15:35 -0400128 */
Tom Stellarda75c6162012-01-06 17:38:37 -0500129static LLVMValueRef use_sgpr(
130 struct gallivm_state * gallivm,
131 enum sgpr_type type,
132 unsigned sgpr)
133{
134 LLVMValueRef sgpr_index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500135 LLVMTypeRef ret_type;
Tom Stellard89ece082012-05-29 11:36:29 -0400136 LLVMValueRef ptr;
Tom Stellarda75c6162012-01-06 17:38:37 -0500137
138 sgpr_index = lp_build_const_int32(gallivm, sgpr);
139
Tom Stellard467f5162012-05-16 15:15:35 -0400140 switch (type) {
141 case SGPR_CONST_PTR_F32:
Tom Stellard89ece082012-05-29 11:36:29 -0400142 assert(sgpr % 2 == 0);
Tom Stellard467f5162012-05-16 15:15:35 -0400143 ret_type = LLVMFloatTypeInContext(gallivm->context);
144 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400145 break;
146
Tom Stellard467f5162012-05-16 15:15:35 -0400147 case SGPR_I32:
Tom Stellarda75c6162012-01-06 17:38:37 -0500148 ret_type = LLVMInt32TypeInContext(gallivm->context);
Tom Stellard89ece082012-05-29 11:36:29 -0400149 break;
150
Tom Stellard467f5162012-05-16 15:15:35 -0400151 case SGPR_I64:
Tom Stellard89ece082012-05-29 11:36:29 -0400152 assert(sgpr % 2 == 0);
Tom Stellard467f5162012-05-16 15:15:35 -0400153 ret_type= LLVMInt64TypeInContext(gallivm->context);
Tom Stellard89ece082012-05-29 11:36:29 -0400154 break;
155
Tom Stellard467f5162012-05-16 15:15:35 -0400156 case SGPR_CONST_PTR_V4I32:
Tom Stellard89ece082012-05-29 11:36:29 -0400157 assert(sgpr % 2 == 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500158 ret_type = LLVMInt32TypeInContext(gallivm->context);
159 ret_type = LLVMVectorType(ret_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400160 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400161 break;
162
Tom Stellard467f5162012-05-16 15:15:35 -0400163 case SGPR_CONST_PTR_V8I32:
Tom Stellard89ece082012-05-29 11:36:29 -0400164 assert(sgpr % 2 == 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500165 ret_type = LLVMInt32TypeInContext(gallivm->context);
166 ret_type = LLVMVectorType(ret_type, 8);
Tom Stellard467f5162012-05-16 15:15:35 -0400167 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400168 break;
169
Tom Stellarda75c6162012-01-06 17:38:37 -0500170 default:
171 assert(!"Unsupported SGPR type in use_sgpr()");
172 return NULL;
173 }
Tom Stellard89ece082012-05-29 11:36:29 -0400174
175 ret_type = LLVMPointerType(ret_type, USER_SGPR_ADDR_SPACE);
176 ptr = LLVMBuildIntToPtr(gallivm->builder, sgpr_index, ret_type, "");
177 return LLVMBuildLoad(gallivm->builder, ptr, "");
Tom Stellarda75c6162012-01-06 17:38:37 -0500178}
179
180static void declare_input_vs(
181 struct si_shader_context * si_shader_ctx,
182 unsigned input_index,
183 const struct tgsi_full_declaration *decl)
184{
185 LLVMValueRef t_list_ptr;
186 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400187 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500188 LLVMValueRef attribute_offset;
189 LLVMValueRef buffer_index_reg;
Tom Stellard467f5162012-05-16 15:15:35 -0400190 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500191 LLVMTypeRef vec4_type;
192 LLVMValueRef input;
193 struct lp_build_context * uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
194 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Christian Königb15e3ae2012-07-25 11:22:59 +0200195 //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index];
Tom Stellarda75c6162012-01-06 17:38:37 -0500196 unsigned chan;
197
Tom Stellard467f5162012-05-16 15:15:35 -0400198 /* Load the T list */
Christian König22ae0622012-09-26 20:42:23 +0200199 t_list_ptr = use_sgpr(base->gallivm, SGPR_CONST_PTR_V4I32, SI_SGPR_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500200
Christian Königb15e3ae2012-07-25 11:22:59 +0200201 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400202
203 t_list = build_indexed_load(base->gallivm, t_list_ptr, t_offset);
204
205 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200206 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500207
208 /* Load the buffer index is always, which is always stored in VGPR0
209 * for Vertex Shaders */
Tom Stellard40c41fe2012-07-25 19:17:16 +0000210 buffer_index_reg = build_intrinsic(base->gallivm->builder,
211 "llvm.SI.vs.load.buffer.index", uint->elem_type, NULL, 0,
212 LLVMReadNoneAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500213
214 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400215 args[0] = t_list;
216 args[1] = attribute_offset;
217 args[2] = buffer_index_reg;
Tom Stellarda75c6162012-01-06 17:38:37 -0500218 input = lp_build_intrinsic(base->gallivm->builder,
Tom Stellard467f5162012-05-16 15:15:35 -0400219 "llvm.SI.vs.load.input", vec4_type, args, 3);
Tom Stellarda75c6162012-01-06 17:38:37 -0500220
221 /* Break up the vec4 into individual components */
222 for (chan = 0; chan < 4; chan++) {
223 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
224 /* XXX: Use a helper function for this. There is one in
225 * tgsi_llvm.c. */
226 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
227 LLVMBuildExtractElement(base->gallivm->builder,
228 input, llvm_chan, "");
229 }
230}
231
232static void declare_input_fs(
233 struct si_shader_context * si_shader_ctx,
234 unsigned input_index,
235 const struct tgsi_full_declaration *decl)
236{
237 const char * intr_name;
238 unsigned chan;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200239 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500240 struct lp_build_context * base =
241 &si_shader_ctx->radeon_bld.soa.bld_base.base;
242 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400243 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Tom Stellarda75c6162012-01-06 17:38:37 -0500244
245 /* This value is:
246 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
247 * quad begins a new primitive. Bit 0 always needs
248 * to be unset)
249 * [32:16] ParamOffset
250 *
251 */
Christian König22ae0622012-09-26 20:42:23 +0200252 LLVMValueRef params = use_sgpr(base->gallivm, SGPR_I32, SI_PS_NUM_USER_SGPR);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200253 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500254
Tom Stellard0fb1e682012-09-06 16:18:11 -0400255 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
256 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
257 LLVMValueRef args[1];
258 unsigned soa_index =
259 radeon_llvm_reg_index_soa(input_index, chan);
260 args[0] = lp_build_const_int32(gallivm, chan);
261 si_shader_ctx->radeon_bld.inputs[soa_index] =
262 build_intrinsic(base->gallivm->builder,
263 "llvm.SI.fs.read.pos", input_type,
264 args, 1, LLVMReadNoneAttribute);
265 }
266 return;
267 }
268
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200269 shader->input[input_index].param_offset = shader->ninterp++;
270 attr_number = lp_build_const_int32(gallivm,
271 shader->input[input_index].param_offset);
272
Tom Stellarda75c6162012-01-06 17:38:37 -0500273 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200274 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500275 case TGSI_INTERPOLATE_COLOR:
Michel Dänzer90c6eac2012-06-08 17:15:21 +0200276 /* XXX: Flat shading hangs the GPU */
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200277 if (si_shader_ctx->rctx->queued.named.rasterizer &&
278 si_shader_ctx->rctx->queued.named.rasterizer->flatshade) {
Michel Dänzer90c6eac2012-06-08 17:15:21 +0200279#if 0
Tom Stellarda75c6162012-01-06 17:38:37 -0500280 intr_name = "llvm.SI.fs.interp.constant";
Michel Dänzer90c6eac2012-06-08 17:15:21 +0200281#else
282 intr_name = "llvm.SI.fs.interp.linear.center";
283#endif
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200284 } else {
285 if (decl->Interp.Centroid)
286 intr_name = "llvm.SI.fs.interp.persp.centroid";
287 else
288 intr_name = "llvm.SI.fs.interp.persp.center";
289 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500290 break;
291 case TGSI_INTERPOLATE_CONSTANT:
Michel Dänzer90c6eac2012-06-08 17:15:21 +0200292 /* XXX: Flat shading hangs the GPU */
293#if 0
Tom Stellarda75c6162012-01-06 17:38:37 -0500294 intr_name = "llvm.SI.fs.interp.constant";
295 break;
Michel Dänzer90c6eac2012-06-08 17:15:21 +0200296#endif
Tom Stellarda75c6162012-01-06 17:38:37 -0500297 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200298 if (decl->Interp.Centroid)
299 intr_name = "llvm.SI.fs.interp.linear.centroid";
300 else
301 intr_name = "llvm.SI.fs.interp.linear.center";
302 break;
303 case TGSI_INTERPOLATE_PERSPECTIVE:
304 if (decl->Interp.Centroid)
305 intr_name = "llvm.SI.fs.interp.persp.centroid";
306 else
307 intr_name = "llvm.SI.fs.interp.persp.center";
Tom Stellarda75c6162012-01-06 17:38:37 -0500308 break;
309 default:
310 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
311 return;
312 }
313
Michel Dänzercfebaf92012-08-31 19:04:08 +0200314 if (!si_shader_ctx->ninput_emitted++) {
315 /* Enable whole quad mode */
316 lp_build_intrinsic(gallivm->builder,
317 "llvm.SI.wqm",
318 LLVMVoidTypeInContext(gallivm->context),
319 NULL, 0);
320 }
321
Tom Stellarda75c6162012-01-06 17:38:37 -0500322 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200323 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
324 si_shader_ctx->key.color_two_side) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500325 LLVMValueRef args[3];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200326 LLVMValueRef face, is_face_positive;
327 LLVMValueRef back_attr_number =
328 lp_build_const_int32(gallivm,
329 shader->input[input_index].param_offset + 1);
330
331 face = build_intrinsic(gallivm->builder,
332 "llvm.SI.fs.read.face",
333 input_type,
334 NULL, 0, LLVMReadNoneAttribute);
335 is_face_positive = LLVMBuildFCmp(gallivm->builder,
336 LLVMRealUGT, face,
337 lp_build_const_float(gallivm, 0.0f),
338 "");
339
Tom Stellarda75c6162012-01-06 17:38:37 -0500340 args[2] = params;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200341 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
342 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
343 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
344 LLVMValueRef front, back;
345
346 args[0] = llvm_chan;
347 args[1] = attr_number;
348 front = build_intrinsic(base->gallivm->builder, intr_name,
349 input_type, args, 3, LLVMReadOnlyAttribute);
350
351 args[1] = back_attr_number;
352 back = build_intrinsic(base->gallivm->builder, intr_name,
353 input_type, args, 3, LLVMReadOnlyAttribute);
354
355 si_shader_ctx->radeon_bld.inputs[soa_index] =
356 LLVMBuildSelect(gallivm->builder,
357 is_face_positive,
358 front,
359 back,
360 "");
361 }
362
363 shader->ninterp++;
364 } else {
365 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
366 LLVMValueRef args[3];
367 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
368 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
369 args[0] = llvm_chan;
370 args[1] = attr_number;
371 args[2] = params;
372 si_shader_ctx->radeon_bld.inputs[soa_index] =
373 build_intrinsic(base->gallivm->builder, intr_name,
374 input_type, args, 3, LLVMReadOnlyAttribute);
375 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500376 }
377}
378
379static void declare_input(
380 struct radeon_llvm_context * radeon_bld,
381 unsigned input_index,
382 const struct tgsi_full_declaration *decl)
383{
384 struct si_shader_context * si_shader_ctx =
385 si_shader_context(&radeon_bld->soa.bld_base);
386 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
387 declare_input_vs(si_shader_ctx, input_index, decl);
388 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
389 declare_input_fs(si_shader_ctx, input_index, decl);
390 } else {
391 fprintf(stderr, "Warning: Unsupported shader type,\n");
392 }
393}
394
395static LLVMValueRef fetch_constant(
396 struct lp_build_tgsi_context * bld_base,
397 const struct tgsi_full_src_register *reg,
398 enum tgsi_opcode_type type,
399 unsigned swizzle)
400{
401 struct lp_build_context * base = &bld_base->base;
Christian König88a4fd82012-08-29 10:48:01 +0200402 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500403
404 LLVMValueRef const_ptr;
405 LLVMValueRef offset;
Tom Stellard022b5432012-07-25 08:23:52 -0400406 LLVMValueRef load;
Tom Stellarda75c6162012-01-06 17:38:37 -0500407
Christian Könige7723b52012-08-24 12:55:34 +0200408 /* currently not supported */
409 if (reg->Register.Indirect) {
410 assert(0);
411 load = lp_build_const_int32(base->gallivm, 0);
412 return bitcast(bld_base, type, load);
413 }
414
Christian König22ae0622012-09-26 20:42:23 +0200415 const_ptr = use_sgpr(base->gallivm, SGPR_CONST_PTR_F32, SI_SGPR_CONST);
Tom Stellarda75c6162012-01-06 17:38:37 -0500416
417 /* XXX: This assumes that the constant buffer is not packed, so
418 * CONST[0].x will have an offset of 0 and CONST[1].x will have an
419 * offset of 4. */
Christian König88a4fd82012-08-29 10:48:01 +0200420 idx = (reg->Register.Index * 4) + swizzle;
421
422 /* index loads above 255 are currently not supported */
423 if (idx > 255) {
424 assert(0);
425 idx = 0;
426 }
427 offset = lp_build_const_int32(base->gallivm, idx);
Tom Stellarda75c6162012-01-06 17:38:37 -0500428
Tom Stellard022b5432012-07-25 08:23:52 -0400429 load = build_indexed_load(base->gallivm, const_ptr, offset);
430 return bitcast(bld_base, type, load);
Tom Stellarda75c6162012-01-06 17:38:37 -0500431}
432
Michel Dänzer26c71392012-08-24 12:03:11 +0200433/* Initialize arguments for the shader export intrinsic */
434static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
435 struct tgsi_full_declaration *d,
436 unsigned index,
437 unsigned target,
438 LLVMValueRef *args)
439{
440 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
441 struct lp_build_context *uint =
442 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
443 struct lp_build_context *base = &bld_base->base;
444 unsigned compressed = 0;
445 unsigned chan;
446
Michel Dänzerf402acd2012-08-22 18:15:36 +0200447 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
448 int cbuf = target - V_008DFC_SQ_EXP_MRT;
449
450 if (cbuf >= 0 && cbuf < 8) {
451 struct r600_context *rctx = si_shader_ctx->rctx;
Michel Dänzer44ef0332012-10-05 16:59:10 +0200452 compressed = (si_shader_ctx->key.export_16bpc >> cbuf) & 0x1;
Michel Dänzerf402acd2012-08-22 18:15:36 +0200453 }
454 }
455
456 if (compressed) {
457 /* Pixel shader needs to pack output values before export */
458 for (chan = 0; chan < 2; chan++ ) {
459 LLVMValueRef *out_ptr =
460 si_shader_ctx->radeon_bld.soa.outputs[index];
461 args[0] = LLVMBuildLoad(base->gallivm->builder,
462 out_ptr[2 * chan], "");
463 args[1] = LLVMBuildLoad(base->gallivm->builder,
464 out_ptr[2 * chan + 1], "");
465 args[chan + 5] =
466 build_intrinsic(base->gallivm->builder,
467 "llvm.SI.packf16",
468 LLVMInt32TypeInContext(base->gallivm->context),
469 args, 2,
470 LLVMReadNoneAttribute);
471 args[chan + 7] = args[chan + 5];
472 }
473
474 /* Set COMPR flag */
475 args[4] = uint->one;
476 } else {
477 for (chan = 0; chan < 4; chan++ ) {
478 LLVMValueRef out_ptr =
479 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
480 /* +5 because the first output value will be
481 * the 6th argument to the intrinsic. */
482 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
483 out_ptr, "");
484 }
485
486 /* Clear COMPR flag */
487 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200488 }
489
490 /* XXX: This controls which components of the output
491 * registers actually get exported. (e.g bit 0 means export
492 * X component, bit 1 means export Y component, etc.) I'm
493 * hard coding this to 0xf for now. In the future, we might
494 * want to do something else. */
495 args[0] = lp_build_const_int32(base->gallivm, 0xf);
496
497 /* Specify whether the EXEC mask represents the valid mask */
498 args[1] = uint->zero;
499
500 /* Specify whether this is the last export */
501 args[2] = uint->zero;
502
503 /* Specify the target we are exporting */
504 args[3] = lp_build_const_int32(base->gallivm, target);
505
Michel Dänzer26c71392012-08-24 12:03:11 +0200506 /* XXX: We probably need to keep track of the output
507 * values, so we know what we are passing to the next
508 * stage. */
509}
510
Tom Stellardcc571a32012-09-07 09:12:51 -0400511static void si_llvm_emit_prologue(struct lp_build_tgsi_context *bld_base)
512{
513 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
514 struct gallivm_state *gallivm = bld_base->base.gallivm;
515 lp_build_intrinsic_unary(gallivm->builder,
516 "llvm.AMDGPU.shader.type",
517 LLVMVoidTypeInContext(gallivm->context),
518 lp_build_const_int32(gallivm, si_shader_ctx->type));
519}
520
521
Tom Stellarda75c6162012-01-06 17:38:37 -0500522/* XXX: This is partially implemented for VS only at this point. It is not complete */
523static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
524{
525 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200526 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500527 struct lp_build_context * base = &bld_base->base;
528 struct lp_build_context * uint =
529 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
530 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
531 LLVMValueRef last_args[9] = { 0 };
Christian König35088152012-08-01 22:35:24 +0200532 unsigned color_count = 0;
533 unsigned param_count = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500534
535 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500536 struct tgsi_full_declaration *d =
537 &parse->FullToken.FullDeclaration;
538 LLVMValueRef args[9];
539 unsigned target;
540 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500541 int i;
542
543 tgsi_parse_token(parse);
544 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
545 continue;
546
547 switch (d->Declaration.File) {
548 case TGSI_FILE_INPUT:
549 i = shader->ninput++;
550 shader->input[i].name = d->Semantic.Name;
551 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200552 shader->input[i].interpolate = d->Interp.Interpolate;
553 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200554 continue;
555
Tom Stellarda75c6162012-01-06 17:38:37 -0500556 case TGSI_FILE_OUTPUT:
557 i = shader->noutput++;
558 shader->output[i].name = d->Semantic.Name;
559 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200560 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500561 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500562
Christian König35088152012-08-01 22:35:24 +0200563 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500564 continue;
Christian König35088152012-08-01 22:35:24 +0200565 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500566
567 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500568 /* Select the correct target */
569 switch(d->Semantic.Name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400570 case TGSI_SEMANTIC_PSIZE:
Tom Stellarda75c6162012-01-06 17:38:37 -0500571 case TGSI_SEMANTIC_POSITION:
572 target = V_008DFC_SQ_EXP_POS;
573 break;
574 case TGSI_SEMANTIC_COLOR:
575 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200576 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500577 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200578 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500579 param_count++;
580 } else {
581 target = V_008DFC_SQ_EXP_MRT + color_count;
582 color_count++;
583 }
584 break;
Michel Dänzer30b30372012-09-06 17:53:04 +0200585 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500586 case TGSI_SEMANTIC_GENERIC:
587 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200588 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500589 param_count++;
590 break;
591 default:
592 target = 0;
593 fprintf(stderr,
594 "Warning: SI unhandled output type:%d\n",
595 d->Semantic.Name);
596 }
597
Michel Dänzer26c71392012-08-24 12:03:11 +0200598 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500599
600 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ?
601 (d->Semantic.Name == TGSI_SEMANTIC_POSITION) :
602 (d->Semantic.Name == TGSI_SEMANTIC_COLOR)) {
603 if (last_args[0]) {
604 lp_build_intrinsic(base->gallivm->builder,
605 "llvm.SI.export",
606 LLVMVoidTypeInContext(base->gallivm->context),
607 last_args, 9);
608 }
609
610 memcpy(last_args, args, sizeof(args));
611 } else {
612 lp_build_intrinsic(base->gallivm->builder,
613 "llvm.SI.export",
614 LLVMVoidTypeInContext(base->gallivm->context),
615 args, 9);
616 }
617
618 }
619 }
620
Christian Königf18fd252012-07-25 21:58:46 +0200621 if (!last_args[0]) {
622 assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
623
624 /* Specify which components to enable */
625 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
626
627 /* Specify the target we are exporting */
628 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
629
630 /* Set COMPR flag to zero to export data as 32-bit */
631 last_args[4] = uint->zero;
632
633 /* dummy bits */
634 last_args[5]= uint->zero;
635 last_args[6]= uint->zero;
636 last_args[7]= uint->zero;
637 last_args[8]= uint->zero;
638 }
639
Tom Stellarda75c6162012-01-06 17:38:37 -0500640 /* Specify whether the EXEC mask represents the valid mask */
641 last_args[1] = lp_build_const_int32(base->gallivm,
642 si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
643
644 /* Specify that this is the last export */
645 last_args[2] = lp_build_const_int32(base->gallivm, 1);
646
647 lp_build_intrinsic(base->gallivm->builder,
648 "llvm.SI.export",
649 LLVMVoidTypeInContext(base->gallivm->context),
650 last_args, 9);
651
652/* XXX: Look up what this function does */
653/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
654}
655
656static void tex_fetch_args(
657 struct lp_build_tgsi_context * bld_base,
658 struct lp_build_emit_data * emit_data)
659{
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200660 const struct tgsi_full_instruction * inst = emit_data->inst;
Tom Stellard467f5162012-05-16 15:15:35 -0400661 LLVMValueRef ptr;
662 LLVMValueRef offset;
663
Tom Stellarda75c6162012-01-06 17:38:37 -0500664 /* WriteMask */
Christian König250b7fd2012-08-01 23:18:14 +0200665 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
666 emit_data->args[0] = lp_build_const_int32(bld_base->base.gallivm, 0xf);
Tom Stellarda75c6162012-01-06 17:38:37 -0500667
668 /* Coordinates */
669 /* XXX: Not all sample instructions need 4 address arguments. */
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200670 if (inst->Instruction.Opcode == TGSI_OPCODE_TXP) {
671 LLVMValueRef src_w;
672 unsigned chan;
673 LLVMValueRef coords[4];
674
675 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
676 src_w = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
677
678 for (chan = 0; chan < 3; chan++ ) {
679 LLVMValueRef arg = lp_build_emit_fetch(bld_base,
680 emit_data->inst, 0, chan);
681 coords[chan] = lp_build_emit_llvm_binary(bld_base,
682 TGSI_OPCODE_DIV,
683 arg, src_w);
684 }
685 coords[3] = bld_base->base.one;
686 emit_data->args[1] = lp_build_gather_values(bld_base->base.gallivm,
687 coords, 4);
688 } else
689 emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst,
690 0, LP_CHAN_ALL);
Tom Stellarda75c6162012-01-06 17:38:37 -0500691
692 /* Resource */
Christian König22ae0622012-09-26 20:42:23 +0200693 ptr = use_sgpr(bld_base->base.gallivm, SGPR_CONST_PTR_V8I32, SI_SGPR_RESOURCE);
Tom Stellard467f5162012-05-16 15:15:35 -0400694 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200695 emit_data->inst->Src[1].Register.Index);
Tom Stellard467f5162012-05-16 15:15:35 -0400696 emit_data->args[2] = build_indexed_load(bld_base->base.gallivm,
697 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500698
699 /* Sampler */
Christian König22ae0622012-09-26 20:42:23 +0200700 ptr = use_sgpr(bld_base->base.gallivm, SGPR_CONST_PTR_V4I32, SI_SGPR_SAMPLER);
Tom Stellard467f5162012-05-16 15:15:35 -0400701 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200702 emit_data->inst->Src[1].Register.Index);
Tom Stellard467f5162012-05-16 15:15:35 -0400703 emit_data->args[3] = build_indexed_load(bld_base->base.gallivm,
704 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500705
706 /* Dimensions */
707 /* XXX: We might want to pass this information to the shader at some. */
708/* emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm,
709 emit_data->inst->Texture.Texture);
710*/
711
Tom Stellard467f5162012-05-16 15:15:35 -0400712 emit_data->arg_count = 4;
Tom Stellarda75c6162012-01-06 17:38:37 -0500713 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
714 * the writemask are clear */
715 emit_data->dst_type = LLVMVectorType(
716 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
717 4);
718}
719
720static const struct lp_build_tgsi_action tex_action = {
721 .fetch_args = tex_fetch_args,
722 .emit = lp_build_tgsi_intrinsic,
723 .intr_name = "llvm.SI.sample"
724};
725
726
727int si_pipe_shader_create(
728 struct pipe_context *ctx,
Michel Dänzer44ef0332012-10-05 16:59:10 +0200729 struct si_pipe_shader *shader,
730 struct si_shader_key key)
Tom Stellarda75c6162012-01-06 17:38:37 -0500731{
732 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200733 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -0500734 struct si_shader_context si_shader_ctx;
735 struct tgsi_shader_info shader_info;
736 struct lp_build_tgsi_context * bld_base;
737 LLVMModuleRef mod;
738 unsigned char * inst_bytes;
739 unsigned inst_byte_count;
740 unsigned i;
Christian Königd51b9b72012-07-24 18:50:49 +0200741 uint32_t *ptr;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200742 bool dump;
743
744 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -0500745
Michel Dänzer82e38ac2012-09-27 16:39:26 +0200746 assert(shader->shader.noutput == 0);
747 assert(shader->shader.ninterp == 0);
748 assert(shader->shader.ninput == 0);
749
Michel Dänzercfebaf92012-08-31 19:04:08 +0200750 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -0500751 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
752 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
753
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200754 tgsi_scan_shader(sel->tokens, &shader_info);
Tom Stellarda75c6162012-01-06 17:38:37 -0500755 bld_base->info = &shader_info;
756 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellardcc571a32012-09-07 09:12:51 -0400757 bld_base->emit_prologue = si_llvm_emit_prologue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500758 bld_base->emit_epilogue = si_llvm_emit_epilogue;
759
760 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200761 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -0500762
763 si_shader_ctx.radeon_bld.load_input = declare_input;
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200764 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -0500765 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
766 si_shader_ctx.shader = shader;
Michel Dänzer44ef0332012-10-05 16:59:10 +0200767 si_shader_ctx.key = key;
Tom Stellarda75c6162012-01-06 17:38:37 -0500768 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
769 si_shader_ctx.rctx = rctx;
770
Christian König835098a2012-07-17 21:28:10 +0200771 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -0500772
Tom Stellard185fc9a2012-07-12 10:40:47 -0400773 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
774 * conversion fails. */
775 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200776 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -0400777 }
778
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200779 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +0200780 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
781 return -EINVAL;
782 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500783
784 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
785
786 mod = bld_base->base.gallivm->module;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200787 if (dump) {
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200788 LLVMDumpModule(mod);
789 }
790 radeon_llvm_compile(mod, &inst_bytes, &inst_byte_count, "SI", dump);
791 if (dump) {
792 fprintf(stderr, "SI CODE:\n");
793 for (i = 0; i < inst_byte_count; i+=4 ) {
794 fprintf(stderr, "%02x%02x%02x%02x\n", inst_bytes[i + 3],
795 inst_bytes[i + 2], inst_bytes[i + 1],
796 inst_bytes[i]);
797 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500798 }
799
800 shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)inst_bytes);
801 shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 4));
802 shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 8));
803
Michel Dänzer4b64fa22012-08-15 18:22:46 +0200804 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -0500805 tgsi_parse_free(&si_shader_ctx.parse);
806
807 /* copy new shader */
Christian Königd51b9b72012-07-24 18:50:49 +0200808 si_resource_reference(&shader->bo, NULL);
809 shader->bo = si_resource_create_custom(ctx->screen, PIPE_USAGE_IMMUTABLE,
810 inst_byte_count - 12);
Tom Stellarda75c6162012-01-06 17:38:37 -0500811 if (shader->bo == NULL) {
Christian Königd51b9b72012-07-24 18:50:49 +0200812 return -ENOMEM;
Tom Stellarda75c6162012-01-06 17:38:37 -0500813 }
814
Christian Königd51b9b72012-07-24 18:50:49 +0200815 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
816 if (0 /*R600_BIG_ENDIAN*/) {
817 for (i = 0; i < (inst_byte_count-12)/4; ++i) {
818 ptr[i] = util_bswap32(*(uint32_t*)(inst_bytes+12 + i*4));
819 }
820 } else {
821 memcpy(ptr, inst_bytes + 12, inst_byte_count - 12);
822 }
823 rctx->ws->buffer_unmap(shader->bo->cs_buf);
824
Tom Stellarda75c6162012-01-06 17:38:37 -0500825 free(inst_bytes);
826
827 return 0;
828}
829
830void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
831{
Christian Königfe412872012-07-24 18:47:19 +0200832 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -0500833}