blob: f65e69210cff09519b86775345d1c9d73b55bc32 [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;
58 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Michel Dänzercfebaf92012-08-31 19:04:08 +020059 unsigned ninput_emitted;
Tom Stellarda75c6162012-01-06 17:38:37 -050060/* struct list_head inputs; */
61/* unsigned * input_mappings *//* From TGSI to SI hw */
62/* struct tgsi_shader_info info;*/
63};
64
65static struct si_shader_context * si_shader_context(
66 struct lp_build_tgsi_context * bld_base)
67{
68 return (struct si_shader_context *)bld_base;
69}
70
71
72#define PERSPECTIVE_BASE 0
73#define LINEAR_BASE 9
74
75#define SAMPLE_OFFSET 0
76#define CENTER_OFFSET 2
77#define CENTROID_OFSET 4
78
79#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040080#define CONST_ADDR_SPACE 2
Tom Stellard89ece082012-05-29 11:36:29 -040081#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050082
83enum sgpr_type {
Tom Stellard467f5162012-05-16 15:15:35 -040084 SGPR_CONST_PTR_F32,
85 SGPR_CONST_PTR_V4I32,
86 SGPR_CONST_PTR_V8I32,
Tom Stellarda75c6162012-01-06 17:38:37 -050087 SGPR_I32,
Tom Stellard467f5162012-05-16 15:15:35 -040088 SGPR_I64
Tom Stellarda75c6162012-01-06 17:38:37 -050089};
90
Tom Stellard467f5162012-05-16 15:15:35 -040091/**
92 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
93 *
94 * @param offset The offset parameter specifies the number of
95 * elements to offset, not the number of bytes or dwords. An element is the
96 * the type pointed to by the base_ptr parameter (e.g. int is the element of
97 * an int* pointer)
98 *
99 * When LLVM lowers the load instruction, it will convert the element offset
100 * into a dword offset automatically.
101 *
102 */
103static LLVMValueRef build_indexed_load(
104 struct gallivm_state * gallivm,
105 LLVMValueRef base_ptr,
106 LLVMValueRef offset)
107{
108 LLVMValueRef computed_ptr = LLVMBuildGEP(
109 gallivm->builder, base_ptr, &offset, 1, "");
110
111 return LLVMBuildLoad(gallivm->builder, computed_ptr, "");
112}
113
Tom Stellard89ece082012-05-29 11:36:29 -0400114/**
115 * Load a value stored in one of the user SGPRs
116 *
117 * @param sgpr This is the sgpr to load the value from. If you need to load a
118 * value that is stored in consecutive SGPR registers (e.g. a 64-bit pointer),
119 * then you should pass the index of the first SGPR that holds the value. For
120 * example, if you want to load a pointer that is stored in SGPRs 2 and 3, then
121 * use pass 2 for the sgpr parameter.
122 *
123 * The value of the sgpr parameter must also be aligned to the width of the type
124 * being loaded, so that the sgpr parameter is divisible by the dword width of the
125 * type. For example, if the value being loaded is two dwords wide, then the sgpr
126 * parameter must be divisible by two.
Tom Stellard467f5162012-05-16 15:15:35 -0400127 */
Tom Stellarda75c6162012-01-06 17:38:37 -0500128static LLVMValueRef use_sgpr(
129 struct gallivm_state * gallivm,
130 enum sgpr_type type,
131 unsigned sgpr)
132{
133 LLVMValueRef sgpr_index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500134 LLVMTypeRef ret_type;
Tom Stellard89ece082012-05-29 11:36:29 -0400135 LLVMValueRef ptr;
Tom Stellarda75c6162012-01-06 17:38:37 -0500136
137 sgpr_index = lp_build_const_int32(gallivm, sgpr);
138
Tom Stellard467f5162012-05-16 15:15:35 -0400139 switch (type) {
140 case SGPR_CONST_PTR_F32:
Tom Stellard89ece082012-05-29 11:36:29 -0400141 assert(sgpr % 2 == 0);
Tom Stellard467f5162012-05-16 15:15:35 -0400142 ret_type = LLVMFloatTypeInContext(gallivm->context);
143 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400144 break;
145
Tom Stellard467f5162012-05-16 15:15:35 -0400146 case SGPR_I32:
Tom Stellarda75c6162012-01-06 17:38:37 -0500147 ret_type = LLVMInt32TypeInContext(gallivm->context);
Tom Stellard89ece082012-05-29 11:36:29 -0400148 break;
149
Tom Stellard467f5162012-05-16 15:15:35 -0400150 case SGPR_I64:
Tom Stellard89ece082012-05-29 11:36:29 -0400151 assert(sgpr % 2 == 0);
Tom Stellard467f5162012-05-16 15:15:35 -0400152 ret_type= LLVMInt64TypeInContext(gallivm->context);
Tom Stellard89ece082012-05-29 11:36:29 -0400153 break;
154
Tom Stellard467f5162012-05-16 15:15:35 -0400155 case SGPR_CONST_PTR_V4I32:
Tom Stellard89ece082012-05-29 11:36:29 -0400156 assert(sgpr % 2 == 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500157 ret_type = LLVMInt32TypeInContext(gallivm->context);
158 ret_type = LLVMVectorType(ret_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400159 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400160 break;
161
Tom Stellard467f5162012-05-16 15:15:35 -0400162 case SGPR_CONST_PTR_V8I32:
Tom Stellard89ece082012-05-29 11:36:29 -0400163 assert(sgpr % 2 == 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500164 ret_type = LLVMInt32TypeInContext(gallivm->context);
165 ret_type = LLVMVectorType(ret_type, 8);
Tom Stellard467f5162012-05-16 15:15:35 -0400166 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400167 break;
168
Tom Stellarda75c6162012-01-06 17:38:37 -0500169 default:
170 assert(!"Unsupported SGPR type in use_sgpr()");
171 return NULL;
172 }
Tom Stellard89ece082012-05-29 11:36:29 -0400173
174 ret_type = LLVMPointerType(ret_type, USER_SGPR_ADDR_SPACE);
175 ptr = LLVMBuildIntToPtr(gallivm->builder, sgpr_index, ret_type, "");
176 return LLVMBuildLoad(gallivm->builder, ptr, "");
Tom Stellarda75c6162012-01-06 17:38:37 -0500177}
178
179static void declare_input_vs(
180 struct si_shader_context * si_shader_ctx,
181 unsigned input_index,
182 const struct tgsi_full_declaration *decl)
183{
184 LLVMValueRef t_list_ptr;
185 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400186 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500187 LLVMValueRef attribute_offset;
188 LLVMValueRef buffer_index_reg;
Tom Stellard467f5162012-05-16 15:15:35 -0400189 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500190 LLVMTypeRef vec4_type;
191 LLVMValueRef input;
192 struct lp_build_context * uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
193 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
194 struct r600_context *rctx = si_shader_ctx->rctx;
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) ? */
323 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
324 LLVMValueRef args[3];
325 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
326 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500327 args[0] = llvm_chan;
328 args[1] = attr_number;
329 args[2] = params;
330 si_shader_ctx->radeon_bld.inputs[soa_index] =
Tom Stellardcf4ac692012-08-29 09:58:28 -0400331 build_intrinsic(base->gallivm->builder, intr_name,
332 input_type, args, 3, LLVMReadOnlyAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500333 }
334}
335
336static void declare_input(
337 struct radeon_llvm_context * radeon_bld,
338 unsigned input_index,
339 const struct tgsi_full_declaration *decl)
340{
341 struct si_shader_context * si_shader_ctx =
342 si_shader_context(&radeon_bld->soa.bld_base);
343 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
344 declare_input_vs(si_shader_ctx, input_index, decl);
345 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
346 declare_input_fs(si_shader_ctx, input_index, decl);
347 } else {
348 fprintf(stderr, "Warning: Unsupported shader type,\n");
349 }
350}
351
352static LLVMValueRef fetch_constant(
353 struct lp_build_tgsi_context * bld_base,
354 const struct tgsi_full_src_register *reg,
355 enum tgsi_opcode_type type,
356 unsigned swizzle)
357{
358 struct lp_build_context * base = &bld_base->base;
Christian König88a4fd82012-08-29 10:48:01 +0200359 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500360
361 LLVMValueRef const_ptr;
362 LLVMValueRef offset;
Tom Stellard022b5432012-07-25 08:23:52 -0400363 LLVMValueRef load;
Tom Stellarda75c6162012-01-06 17:38:37 -0500364
Christian Könige7723b52012-08-24 12:55:34 +0200365 /* currently not supported */
366 if (reg->Register.Indirect) {
367 assert(0);
368 load = lp_build_const_int32(base->gallivm, 0);
369 return bitcast(bld_base, type, load);
370 }
371
Christian König22ae0622012-09-26 20:42:23 +0200372 const_ptr = use_sgpr(base->gallivm, SGPR_CONST_PTR_F32, SI_SGPR_CONST);
Tom Stellarda75c6162012-01-06 17:38:37 -0500373
374 /* XXX: This assumes that the constant buffer is not packed, so
375 * CONST[0].x will have an offset of 0 and CONST[1].x will have an
376 * offset of 4. */
Christian König88a4fd82012-08-29 10:48:01 +0200377 idx = (reg->Register.Index * 4) + swizzle;
378
379 /* index loads above 255 are currently not supported */
380 if (idx > 255) {
381 assert(0);
382 idx = 0;
383 }
384 offset = lp_build_const_int32(base->gallivm, idx);
Tom Stellarda75c6162012-01-06 17:38:37 -0500385
Tom Stellard022b5432012-07-25 08:23:52 -0400386 load = build_indexed_load(base->gallivm, const_ptr, offset);
387 return bitcast(bld_base, type, load);
Tom Stellarda75c6162012-01-06 17:38:37 -0500388}
389
Michel Dänzer26c71392012-08-24 12:03:11 +0200390/* Initialize arguments for the shader export intrinsic */
391static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
392 struct tgsi_full_declaration *d,
393 unsigned index,
394 unsigned target,
395 LLVMValueRef *args)
396{
397 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
398 struct lp_build_context *uint =
399 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
400 struct lp_build_context *base = &bld_base->base;
401 unsigned compressed = 0;
402 unsigned chan;
403
Michel Dänzerf402acd2012-08-22 18:15:36 +0200404 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
405 int cbuf = target - V_008DFC_SQ_EXP_MRT;
406
407 if (cbuf >= 0 && cbuf < 8) {
408 struct r600_context *rctx = si_shader_ctx->rctx;
409 compressed = (rctx->export_16bpc >> cbuf) & 0x1;
410 }
411 }
412
413 if (compressed) {
414 /* Pixel shader needs to pack output values before export */
415 for (chan = 0; chan < 2; chan++ ) {
416 LLVMValueRef *out_ptr =
417 si_shader_ctx->radeon_bld.soa.outputs[index];
418 args[0] = LLVMBuildLoad(base->gallivm->builder,
419 out_ptr[2 * chan], "");
420 args[1] = LLVMBuildLoad(base->gallivm->builder,
421 out_ptr[2 * chan + 1], "");
422 args[chan + 5] =
423 build_intrinsic(base->gallivm->builder,
424 "llvm.SI.packf16",
425 LLVMInt32TypeInContext(base->gallivm->context),
426 args, 2,
427 LLVMReadNoneAttribute);
428 args[chan + 7] = args[chan + 5];
429 }
430
431 /* Set COMPR flag */
432 args[4] = uint->one;
433 } else {
434 for (chan = 0; chan < 4; chan++ ) {
435 LLVMValueRef out_ptr =
436 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
437 /* +5 because the first output value will be
438 * the 6th argument to the intrinsic. */
439 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
440 out_ptr, "");
441 }
442
443 /* Clear COMPR flag */
444 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200445 }
446
447 /* XXX: This controls which components of the output
448 * registers actually get exported. (e.g bit 0 means export
449 * X component, bit 1 means export Y component, etc.) I'm
450 * hard coding this to 0xf for now. In the future, we might
451 * want to do something else. */
452 args[0] = lp_build_const_int32(base->gallivm, 0xf);
453
454 /* Specify whether the EXEC mask represents the valid mask */
455 args[1] = uint->zero;
456
457 /* Specify whether this is the last export */
458 args[2] = uint->zero;
459
460 /* Specify the target we are exporting */
461 args[3] = lp_build_const_int32(base->gallivm, target);
462
Michel Dänzer26c71392012-08-24 12:03:11 +0200463 /* XXX: We probably need to keep track of the output
464 * values, so we know what we are passing to the next
465 * stage. */
466}
467
Tom Stellardcc571a32012-09-07 09:12:51 -0400468static void si_llvm_emit_prologue(struct lp_build_tgsi_context *bld_base)
469{
470 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
471 struct gallivm_state *gallivm = bld_base->base.gallivm;
472 lp_build_intrinsic_unary(gallivm->builder,
473 "llvm.AMDGPU.shader.type",
474 LLVMVoidTypeInContext(gallivm->context),
475 lp_build_const_int32(gallivm, si_shader_ctx->type));
476}
477
478
Tom Stellarda75c6162012-01-06 17:38:37 -0500479/* XXX: This is partially implemented for VS only at this point. It is not complete */
480static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
481{
482 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200483 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500484 struct lp_build_context * base = &bld_base->base;
485 struct lp_build_context * uint =
486 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
487 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
488 LLVMValueRef last_args[9] = { 0 };
Christian König35088152012-08-01 22:35:24 +0200489 unsigned color_count = 0;
490 unsigned param_count = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500491
492 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500493 struct tgsi_full_declaration *d =
494 &parse->FullToken.FullDeclaration;
495 LLVMValueRef args[9];
496 unsigned target;
497 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500498 int i;
499
500 tgsi_parse_token(parse);
501 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
502 continue;
503
504 switch (d->Declaration.File) {
505 case TGSI_FILE_INPUT:
506 i = shader->ninput++;
507 shader->input[i].name = d->Semantic.Name;
508 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200509 shader->input[i].interpolate = d->Interp.Interpolate;
510 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200511 continue;
512
Tom Stellarda75c6162012-01-06 17:38:37 -0500513 case TGSI_FILE_OUTPUT:
514 i = shader->noutput++;
515 shader->output[i].name = d->Semantic.Name;
516 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200517 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500518 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500519
Christian König35088152012-08-01 22:35:24 +0200520 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500521 continue;
Christian König35088152012-08-01 22:35:24 +0200522 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500523
524 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500525 /* Select the correct target */
526 switch(d->Semantic.Name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400527 case TGSI_SEMANTIC_PSIZE:
Tom Stellarda75c6162012-01-06 17:38:37 -0500528 case TGSI_SEMANTIC_POSITION:
529 target = V_008DFC_SQ_EXP_POS;
530 break;
531 case TGSI_SEMANTIC_COLOR:
532 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
533 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200534 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500535 param_count++;
536 } else {
537 target = V_008DFC_SQ_EXP_MRT + color_count;
538 color_count++;
539 }
540 break;
Michel Dänzer30b30372012-09-06 17:53:04 +0200541 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500542 case TGSI_SEMANTIC_GENERIC:
543 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200544 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500545 param_count++;
546 break;
547 default:
548 target = 0;
549 fprintf(stderr,
550 "Warning: SI unhandled output type:%d\n",
551 d->Semantic.Name);
552 }
553
Michel Dänzer26c71392012-08-24 12:03:11 +0200554 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500555
556 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ?
557 (d->Semantic.Name == TGSI_SEMANTIC_POSITION) :
558 (d->Semantic.Name == TGSI_SEMANTIC_COLOR)) {
559 if (last_args[0]) {
560 lp_build_intrinsic(base->gallivm->builder,
561 "llvm.SI.export",
562 LLVMVoidTypeInContext(base->gallivm->context),
563 last_args, 9);
564 }
565
566 memcpy(last_args, args, sizeof(args));
567 } else {
568 lp_build_intrinsic(base->gallivm->builder,
569 "llvm.SI.export",
570 LLVMVoidTypeInContext(base->gallivm->context),
571 args, 9);
572 }
573
574 }
575 }
576
Christian Königf18fd252012-07-25 21:58:46 +0200577 if (!last_args[0]) {
578 assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
579
580 /* Specify which components to enable */
581 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
582
583 /* Specify the target we are exporting */
584 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
585
586 /* Set COMPR flag to zero to export data as 32-bit */
587 last_args[4] = uint->zero;
588
589 /* dummy bits */
590 last_args[5]= uint->zero;
591 last_args[6]= uint->zero;
592 last_args[7]= uint->zero;
593 last_args[8]= uint->zero;
594 }
595
Tom Stellarda75c6162012-01-06 17:38:37 -0500596 /* Specify whether the EXEC mask represents the valid mask */
597 last_args[1] = lp_build_const_int32(base->gallivm,
598 si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
599
600 /* Specify that this is the last export */
601 last_args[2] = lp_build_const_int32(base->gallivm, 1);
602
603 lp_build_intrinsic(base->gallivm->builder,
604 "llvm.SI.export",
605 LLVMVoidTypeInContext(base->gallivm->context),
606 last_args, 9);
607
608/* XXX: Look up what this function does */
609/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
610}
611
612static void tex_fetch_args(
613 struct lp_build_tgsi_context * bld_base,
614 struct lp_build_emit_data * emit_data)
615{
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200616 const struct tgsi_full_instruction * inst = emit_data->inst;
Tom Stellard467f5162012-05-16 15:15:35 -0400617 LLVMValueRef ptr;
618 LLVMValueRef offset;
619
Tom Stellarda75c6162012-01-06 17:38:37 -0500620 /* WriteMask */
Christian König250b7fd2012-08-01 23:18:14 +0200621 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
622 emit_data->args[0] = lp_build_const_int32(bld_base->base.gallivm, 0xf);
Tom Stellarda75c6162012-01-06 17:38:37 -0500623
624 /* Coordinates */
625 /* XXX: Not all sample instructions need 4 address arguments. */
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200626 if (inst->Instruction.Opcode == TGSI_OPCODE_TXP) {
627 LLVMValueRef src_w;
628 unsigned chan;
629 LLVMValueRef coords[4];
630
631 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
632 src_w = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
633
634 for (chan = 0; chan < 3; chan++ ) {
635 LLVMValueRef arg = lp_build_emit_fetch(bld_base,
636 emit_data->inst, 0, chan);
637 coords[chan] = lp_build_emit_llvm_binary(bld_base,
638 TGSI_OPCODE_DIV,
639 arg, src_w);
640 }
641 coords[3] = bld_base->base.one;
642 emit_data->args[1] = lp_build_gather_values(bld_base->base.gallivm,
643 coords, 4);
644 } else
645 emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst,
646 0, LP_CHAN_ALL);
Tom Stellarda75c6162012-01-06 17:38:37 -0500647
648 /* Resource */
Christian König22ae0622012-09-26 20:42:23 +0200649 ptr = use_sgpr(bld_base->base.gallivm, SGPR_CONST_PTR_V8I32, SI_SGPR_RESOURCE);
Tom Stellard467f5162012-05-16 15:15:35 -0400650 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200651 emit_data->inst->Src[1].Register.Index);
Tom Stellard467f5162012-05-16 15:15:35 -0400652 emit_data->args[2] = build_indexed_load(bld_base->base.gallivm,
653 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500654
655 /* Sampler */
Christian König22ae0622012-09-26 20:42:23 +0200656 ptr = use_sgpr(bld_base->base.gallivm, SGPR_CONST_PTR_V4I32, SI_SGPR_SAMPLER);
Tom Stellard467f5162012-05-16 15:15:35 -0400657 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200658 emit_data->inst->Src[1].Register.Index);
Tom Stellard467f5162012-05-16 15:15:35 -0400659 emit_data->args[3] = build_indexed_load(bld_base->base.gallivm,
660 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500661
662 /* Dimensions */
663 /* XXX: We might want to pass this information to the shader at some. */
664/* emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm,
665 emit_data->inst->Texture.Texture);
666*/
667
Tom Stellard467f5162012-05-16 15:15:35 -0400668 emit_data->arg_count = 4;
Tom Stellarda75c6162012-01-06 17:38:37 -0500669 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
670 * the writemask are clear */
671 emit_data->dst_type = LLVMVectorType(
672 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
673 4);
674}
675
676static const struct lp_build_tgsi_action tex_action = {
677 .fetch_args = tex_fetch_args,
678 .emit = lp_build_tgsi_intrinsic,
679 .intr_name = "llvm.SI.sample"
680};
681
682
683int si_pipe_shader_create(
684 struct pipe_context *ctx,
685 struct si_pipe_shader *shader)
686{
687 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200688 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -0500689 struct si_shader_context si_shader_ctx;
690 struct tgsi_shader_info shader_info;
691 struct lp_build_tgsi_context * bld_base;
692 LLVMModuleRef mod;
693 unsigned char * inst_bytes;
694 unsigned inst_byte_count;
695 unsigned i;
Christian Königd51b9b72012-07-24 18:50:49 +0200696 uint32_t *ptr;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200697 bool dump;
698
699 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -0500700
Michel Dänzer82e38ac2012-09-27 16:39:26 +0200701 assert(shader->shader.noutput == 0);
702 assert(shader->shader.ninterp == 0);
703 assert(shader->shader.ninput == 0);
704
Michel Dänzercfebaf92012-08-31 19:04:08 +0200705 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -0500706 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
707 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
708
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200709 tgsi_scan_shader(sel->tokens, &shader_info);
Tom Stellarda75c6162012-01-06 17:38:37 -0500710 bld_base->info = &shader_info;
711 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellardcc571a32012-09-07 09:12:51 -0400712 bld_base->emit_prologue = si_llvm_emit_prologue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500713 bld_base->emit_epilogue = si_llvm_emit_epilogue;
714
715 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200716 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -0500717
718 si_shader_ctx.radeon_bld.load_input = declare_input;
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200719 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -0500720 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
721 si_shader_ctx.shader = shader;
722 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
723 si_shader_ctx.rctx = rctx;
724
Christian König835098a2012-07-17 21:28:10 +0200725 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -0500726
Tom Stellard185fc9a2012-07-12 10:40:47 -0400727 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
728 * conversion fails. */
729 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200730 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -0400731 }
732
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200733 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +0200734 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
735 return -EINVAL;
736 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500737
738 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
739
740 mod = bld_base->base.gallivm->module;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200741 if (dump) {
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200742 LLVMDumpModule(mod);
743 }
744 radeon_llvm_compile(mod, &inst_bytes, &inst_byte_count, "SI", dump);
745 if (dump) {
746 fprintf(stderr, "SI CODE:\n");
747 for (i = 0; i < inst_byte_count; i+=4 ) {
748 fprintf(stderr, "%02x%02x%02x%02x\n", inst_bytes[i + 3],
749 inst_bytes[i + 2], inst_bytes[i + 1],
750 inst_bytes[i]);
751 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500752 }
753
754 shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)inst_bytes);
755 shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 4));
756 shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 8));
757
Michel Dänzer4b64fa22012-08-15 18:22:46 +0200758 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -0500759 tgsi_parse_free(&si_shader_ctx.parse);
760
761 /* copy new shader */
Christian Königd51b9b72012-07-24 18:50:49 +0200762 si_resource_reference(&shader->bo, NULL);
763 shader->bo = si_resource_create_custom(ctx->screen, PIPE_USAGE_IMMUTABLE,
764 inst_byte_count - 12);
Tom Stellarda75c6162012-01-06 17:38:37 -0500765 if (shader->bo == NULL) {
Christian Königd51b9b72012-07-24 18:50:49 +0200766 return -ENOMEM;
Tom Stellarda75c6162012-01-06 17:38:37 -0500767 }
768
Christian Königd51b9b72012-07-24 18:50:49 +0200769 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
770 if (0 /*R600_BIG_ENDIAN*/) {
771 for (i = 0; i < (inst_byte_count-12)/4; ++i) {
772 ptr[i] = util_bswap32(*(uint32_t*)(inst_bytes+12 + i*4));
773 }
774 } else {
775 memcpy(ptr, inst_bytes + 12, inst_byte_count - 12);
776 }
777 rctx->ws->buffer_unmap(shader->bo->cs_buf);
778
Tom Stellarda75c6162012-01-06 17:38:37 -0500779 free(inst_bytes);
780
781 return 0;
782}
783
784void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
785{
Christian Königfe412872012-07-24 18:47:19 +0200786 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -0500787}