blob: 466d5658d5ace3e22835b5cff4773161d319c408 [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
51/*
52static ps_remap_inputs(
53 struct tgsi_llvm_context * tl_ctx,
54 unsigned tgsi_index,
55 unsigned tgsi_chan)
56{
57 :
58}
59
60struct si_input
61{
62 struct list_head head;
63 unsigned tgsi_index;
64 unsigned tgsi_chan;
65 unsigned order;
66};
67*/
68
69
70struct si_shader_context
71{
72 struct radeon_llvm_context radeon_bld;
73 struct r600_context *rctx;
74 struct tgsi_parse_context parse;
75 struct tgsi_token * tokens;
76 struct si_pipe_shader *shader;
77 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Michel Dänzercfebaf92012-08-31 19:04:08 +020078 unsigned ninput_emitted;
Tom Stellarda75c6162012-01-06 17:38:37 -050079/* struct list_head inputs; */
80/* unsigned * input_mappings *//* From TGSI to SI hw */
81/* struct tgsi_shader_info info;*/
82};
83
84static struct si_shader_context * si_shader_context(
85 struct lp_build_tgsi_context * bld_base)
86{
87 return (struct si_shader_context *)bld_base;
88}
89
90
91#define PERSPECTIVE_BASE 0
92#define LINEAR_BASE 9
93
94#define SAMPLE_OFFSET 0
95#define CENTER_OFFSET 2
96#define CENTROID_OFSET 4
97
98#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040099#define CONST_ADDR_SPACE 2
Tom Stellard89ece082012-05-29 11:36:29 -0400100#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -0500101
102enum sgpr_type {
Tom Stellard467f5162012-05-16 15:15:35 -0400103 SGPR_CONST_PTR_F32,
104 SGPR_CONST_PTR_V4I32,
105 SGPR_CONST_PTR_V8I32,
Tom Stellarda75c6162012-01-06 17:38:37 -0500106 SGPR_I32,
Tom Stellard467f5162012-05-16 15:15:35 -0400107 SGPR_I64
Tom Stellarda75c6162012-01-06 17:38:37 -0500108};
109
Tom Stellard467f5162012-05-16 15:15:35 -0400110/**
111 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
112 *
113 * @param offset The offset parameter specifies the number of
114 * elements to offset, not the number of bytes or dwords. An element is the
115 * the type pointed to by the base_ptr parameter (e.g. int is the element of
116 * an int* pointer)
117 *
118 * When LLVM lowers the load instruction, it will convert the element offset
119 * into a dword offset automatically.
120 *
121 */
122static LLVMValueRef build_indexed_load(
123 struct gallivm_state * gallivm,
124 LLVMValueRef base_ptr,
125 LLVMValueRef offset)
126{
127 LLVMValueRef computed_ptr = LLVMBuildGEP(
128 gallivm->builder, base_ptr, &offset, 1, "");
129
130 return LLVMBuildLoad(gallivm->builder, computed_ptr, "");
131}
132
Tom Stellard89ece082012-05-29 11:36:29 -0400133/**
134 * Load a value stored in one of the user SGPRs
135 *
136 * @param sgpr This is the sgpr to load the value from. If you need to load a
137 * value that is stored in consecutive SGPR registers (e.g. a 64-bit pointer),
138 * then you should pass the index of the first SGPR that holds the value. For
139 * example, if you want to load a pointer that is stored in SGPRs 2 and 3, then
140 * use pass 2 for the sgpr parameter.
141 *
142 * The value of the sgpr parameter must also be aligned to the width of the type
143 * being loaded, so that the sgpr parameter is divisible by the dword width of the
144 * type. For example, if the value being loaded is two dwords wide, then the sgpr
145 * parameter must be divisible by two.
Tom Stellard467f5162012-05-16 15:15:35 -0400146 */
Tom Stellarda75c6162012-01-06 17:38:37 -0500147static LLVMValueRef use_sgpr(
148 struct gallivm_state * gallivm,
149 enum sgpr_type type,
150 unsigned sgpr)
151{
152 LLVMValueRef sgpr_index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500153 LLVMTypeRef ret_type;
Tom Stellard89ece082012-05-29 11:36:29 -0400154 LLVMValueRef ptr;
Tom Stellarda75c6162012-01-06 17:38:37 -0500155
156 sgpr_index = lp_build_const_int32(gallivm, sgpr);
157
Tom Stellard467f5162012-05-16 15:15:35 -0400158 switch (type) {
159 case SGPR_CONST_PTR_F32:
Tom Stellard89ece082012-05-29 11:36:29 -0400160 assert(sgpr % 2 == 0);
Tom Stellard467f5162012-05-16 15:15:35 -0400161 ret_type = LLVMFloatTypeInContext(gallivm->context);
162 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400163 break;
164
Tom Stellard467f5162012-05-16 15:15:35 -0400165 case SGPR_I32:
Tom Stellarda75c6162012-01-06 17:38:37 -0500166 ret_type = LLVMInt32TypeInContext(gallivm->context);
Tom Stellard89ece082012-05-29 11:36:29 -0400167 break;
168
Tom Stellard467f5162012-05-16 15:15:35 -0400169 case SGPR_I64:
Tom Stellard89ece082012-05-29 11:36:29 -0400170 assert(sgpr % 2 == 0);
Tom Stellard467f5162012-05-16 15:15:35 -0400171 ret_type= LLVMInt64TypeInContext(gallivm->context);
Tom Stellard89ece082012-05-29 11:36:29 -0400172 break;
173
Tom Stellard467f5162012-05-16 15:15:35 -0400174 case SGPR_CONST_PTR_V4I32:
Tom Stellard89ece082012-05-29 11:36:29 -0400175 assert(sgpr % 2 == 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500176 ret_type = LLVMInt32TypeInContext(gallivm->context);
177 ret_type = LLVMVectorType(ret_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400178 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400179 break;
180
Tom Stellard467f5162012-05-16 15:15:35 -0400181 case SGPR_CONST_PTR_V8I32:
Tom Stellard89ece082012-05-29 11:36:29 -0400182 assert(sgpr % 2 == 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500183 ret_type = LLVMInt32TypeInContext(gallivm->context);
184 ret_type = LLVMVectorType(ret_type, 8);
Tom Stellard467f5162012-05-16 15:15:35 -0400185 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400186 break;
187
Tom Stellarda75c6162012-01-06 17:38:37 -0500188 default:
189 assert(!"Unsupported SGPR type in use_sgpr()");
190 return NULL;
191 }
Tom Stellard89ece082012-05-29 11:36:29 -0400192
193 ret_type = LLVMPointerType(ret_type, USER_SGPR_ADDR_SPACE);
194 ptr = LLVMBuildIntToPtr(gallivm->builder, sgpr_index, ret_type, "");
195 return LLVMBuildLoad(gallivm->builder, ptr, "");
Tom Stellarda75c6162012-01-06 17:38:37 -0500196}
197
198static void declare_input_vs(
199 struct si_shader_context * si_shader_ctx,
200 unsigned input_index,
201 const struct tgsi_full_declaration *decl)
202{
203 LLVMValueRef t_list_ptr;
204 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400205 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500206 LLVMValueRef attribute_offset;
207 LLVMValueRef buffer_index_reg;
Tom Stellard467f5162012-05-16 15:15:35 -0400208 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500209 LLVMTypeRef vec4_type;
210 LLVMValueRef input;
211 struct lp_build_context * uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
212 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
213 struct r600_context *rctx = si_shader_ctx->rctx;
Christian Königb15e3ae2012-07-25 11:22:59 +0200214 //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index];
Tom Stellarda75c6162012-01-06 17:38:37 -0500215 unsigned chan;
216
Tom Stellard467f5162012-05-16 15:15:35 -0400217 /* Load the T list */
Tom Stellarda75c6162012-01-06 17:38:37 -0500218 /* XXX: Communicate with the rest of the driver about which SGPR the T#
Michel Dänzer9918fbd2012-04-19 11:41:48 +0200219 * list pointer is going to be stored in. Hard code to SGPR[6:7] for
Tom Stellarda75c6162012-01-06 17:38:37 -0500220 * now */
Tom Stellard89ece082012-05-29 11:36:29 -0400221 t_list_ptr = use_sgpr(base->gallivm, SGPR_CONST_PTR_V4I32, 6);
Tom Stellarda75c6162012-01-06 17:38:37 -0500222
Christian Königb15e3ae2012-07-25 11:22:59 +0200223 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400224
225 t_list = build_indexed_load(base->gallivm, t_list_ptr, t_offset);
226
227 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200228 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500229
230 /* Load the buffer index is always, which is always stored in VGPR0
231 * for Vertex Shaders */
Tom Stellard40c41fe2012-07-25 19:17:16 +0000232 buffer_index_reg = build_intrinsic(base->gallivm->builder,
233 "llvm.SI.vs.load.buffer.index", uint->elem_type, NULL, 0,
234 LLVMReadNoneAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500235
236 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400237 args[0] = t_list;
238 args[1] = attribute_offset;
239 args[2] = buffer_index_reg;
Tom Stellarda75c6162012-01-06 17:38:37 -0500240 input = lp_build_intrinsic(base->gallivm->builder,
Tom Stellard467f5162012-05-16 15:15:35 -0400241 "llvm.SI.vs.load.input", vec4_type, args, 3);
Tom Stellarda75c6162012-01-06 17:38:37 -0500242
243 /* Break up the vec4 into individual components */
244 for (chan = 0; chan < 4; chan++) {
245 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
246 /* XXX: Use a helper function for this. There is one in
247 * tgsi_llvm.c. */
248 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
249 LLVMBuildExtractElement(base->gallivm->builder,
250 input, llvm_chan, "");
251 }
252}
253
254static void declare_input_fs(
255 struct si_shader_context * si_shader_ctx,
256 unsigned input_index,
257 const struct tgsi_full_declaration *decl)
258{
259 const char * intr_name;
260 unsigned chan;
261 struct lp_build_context * base =
262 &si_shader_ctx->radeon_bld.soa.bld_base.base;
263 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400264 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Tom Stellarda75c6162012-01-06 17:38:37 -0500265
266 /* This value is:
267 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
268 * quad begins a new primitive. Bit 0 always needs
269 * to be unset)
270 * [32:16] ParamOffset
271 *
272 */
Michel Dänzer9918fbd2012-04-19 11:41:48 +0200273 /* XXX: This register number must be identical to the S_00B02C_USER_SGPR
274 * register field value
275 */
Tom Stellarda75c6162012-01-06 17:38:37 -0500276 LLVMValueRef params = use_sgpr(base->gallivm, SGPR_I32, 6);
277
278
279 /* XXX: Is this the input_index? */
280 LLVMValueRef attr_number = lp_build_const_int32(gallivm, input_index);
281
Tom Stellard0fb1e682012-09-06 16:18:11 -0400282 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
283 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
284 LLVMValueRef args[1];
285 unsigned soa_index =
286 radeon_llvm_reg_index_soa(input_index, chan);
287 args[0] = lp_build_const_int32(gallivm, chan);
288 si_shader_ctx->radeon_bld.inputs[soa_index] =
289 build_intrinsic(base->gallivm->builder,
290 "llvm.SI.fs.read.pos", input_type,
291 args, 1, LLVMReadNoneAttribute);
292 }
293 return;
294 }
295
Tom Stellarda75c6162012-01-06 17:38:37 -0500296 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200297 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500298 case TGSI_INTERPOLATE_COLOR:
Michel Dänzer90c6eac2012-06-08 17:15:21 +0200299 /* XXX: Flat shading hangs the GPU */
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200300 if (si_shader_ctx->rctx->queued.named.rasterizer &&
301 si_shader_ctx->rctx->queued.named.rasterizer->flatshade) {
Michel Dänzer90c6eac2012-06-08 17:15:21 +0200302#if 0
Tom Stellarda75c6162012-01-06 17:38:37 -0500303 intr_name = "llvm.SI.fs.interp.constant";
Michel Dänzer90c6eac2012-06-08 17:15:21 +0200304#else
305 intr_name = "llvm.SI.fs.interp.linear.center";
306#endif
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200307 } else {
308 if (decl->Interp.Centroid)
309 intr_name = "llvm.SI.fs.interp.persp.centroid";
310 else
311 intr_name = "llvm.SI.fs.interp.persp.center";
312 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500313 break;
314 case TGSI_INTERPOLATE_CONSTANT:
Michel Dänzer90c6eac2012-06-08 17:15:21 +0200315 /* XXX: Flat shading hangs the GPU */
316#if 0
Tom Stellarda75c6162012-01-06 17:38:37 -0500317 intr_name = "llvm.SI.fs.interp.constant";
318 break;
Michel Dänzer90c6eac2012-06-08 17:15:21 +0200319#endif
Tom Stellarda75c6162012-01-06 17:38:37 -0500320 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200321 if (decl->Interp.Centroid)
322 intr_name = "llvm.SI.fs.interp.linear.centroid";
323 else
324 intr_name = "llvm.SI.fs.interp.linear.center";
325 break;
326 case TGSI_INTERPOLATE_PERSPECTIVE:
327 if (decl->Interp.Centroid)
328 intr_name = "llvm.SI.fs.interp.persp.centroid";
329 else
330 intr_name = "llvm.SI.fs.interp.persp.center";
Tom Stellarda75c6162012-01-06 17:38:37 -0500331 break;
332 default:
333 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
334 return;
335 }
336
Michel Dänzercfebaf92012-08-31 19:04:08 +0200337 if (!si_shader_ctx->ninput_emitted++) {
338 /* Enable whole quad mode */
339 lp_build_intrinsic(gallivm->builder,
340 "llvm.SI.wqm",
341 LLVMVoidTypeInContext(gallivm->context),
342 NULL, 0);
343 }
344
Tom Stellarda75c6162012-01-06 17:38:37 -0500345 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
346 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
347 LLVMValueRef args[3];
348 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
349 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500350 args[0] = llvm_chan;
351 args[1] = attr_number;
352 args[2] = params;
353 si_shader_ctx->radeon_bld.inputs[soa_index] =
Tom Stellardcf4ac692012-08-29 09:58:28 -0400354 build_intrinsic(base->gallivm->builder, intr_name,
355 input_type, args, 3, LLVMReadOnlyAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500356 }
357}
358
359static void declare_input(
360 struct radeon_llvm_context * radeon_bld,
361 unsigned input_index,
362 const struct tgsi_full_declaration *decl)
363{
364 struct si_shader_context * si_shader_ctx =
365 si_shader_context(&radeon_bld->soa.bld_base);
366 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
367 declare_input_vs(si_shader_ctx, input_index, decl);
368 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
369 declare_input_fs(si_shader_ctx, input_index, decl);
370 } else {
371 fprintf(stderr, "Warning: Unsupported shader type,\n");
372 }
373}
374
375static LLVMValueRef fetch_constant(
376 struct lp_build_tgsi_context * bld_base,
377 const struct tgsi_full_src_register *reg,
378 enum tgsi_opcode_type type,
379 unsigned swizzle)
380{
381 struct lp_build_context * base = &bld_base->base;
Christian König88a4fd82012-08-29 10:48:01 +0200382 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500383
384 LLVMValueRef const_ptr;
385 LLVMValueRef offset;
Tom Stellard022b5432012-07-25 08:23:52 -0400386 LLVMValueRef load;
Tom Stellarda75c6162012-01-06 17:38:37 -0500387
Christian Könige7723b52012-08-24 12:55:34 +0200388 /* currently not supported */
389 if (reg->Register.Indirect) {
390 assert(0);
391 load = lp_build_const_int32(base->gallivm, 0);
392 return bitcast(bld_base, type, load);
393 }
394
Tom Stellarda75c6162012-01-06 17:38:37 -0500395 /* XXX: Assume the pointer to the constant buffer is being stored in
Michel Dänzer9918fbd2012-04-19 11:41:48 +0200396 * SGPR[0:1] */
Tom Stellard467f5162012-05-16 15:15:35 -0400397 const_ptr = use_sgpr(base->gallivm, SGPR_CONST_PTR_F32, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500398
399 /* XXX: This assumes that the constant buffer is not packed, so
400 * CONST[0].x will have an offset of 0 and CONST[1].x will have an
401 * offset of 4. */
Christian König88a4fd82012-08-29 10:48:01 +0200402 idx = (reg->Register.Index * 4) + swizzle;
403
404 /* index loads above 255 are currently not supported */
405 if (idx > 255) {
406 assert(0);
407 idx = 0;
408 }
409 offset = lp_build_const_int32(base->gallivm, idx);
Tom Stellarda75c6162012-01-06 17:38:37 -0500410
Tom Stellard022b5432012-07-25 08:23:52 -0400411 load = build_indexed_load(base->gallivm, const_ptr, offset);
412 return bitcast(bld_base, type, load);
Tom Stellarda75c6162012-01-06 17:38:37 -0500413}
414
Michel Dänzer26c71392012-08-24 12:03:11 +0200415/* Initialize arguments for the shader export intrinsic */
416static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
417 struct tgsi_full_declaration *d,
418 unsigned index,
419 unsigned target,
420 LLVMValueRef *args)
421{
422 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
423 struct lp_build_context *uint =
424 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
425 struct lp_build_context *base = &bld_base->base;
426 unsigned compressed = 0;
427 unsigned chan;
428
Michel Dänzerf402acd2012-08-22 18:15:36 +0200429 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
430 int cbuf = target - V_008DFC_SQ_EXP_MRT;
431
432 if (cbuf >= 0 && cbuf < 8) {
433 struct r600_context *rctx = si_shader_ctx->rctx;
434 compressed = (rctx->export_16bpc >> cbuf) & 0x1;
435 }
436 }
437
438 if (compressed) {
439 /* Pixel shader needs to pack output values before export */
440 for (chan = 0; chan < 2; chan++ ) {
441 LLVMValueRef *out_ptr =
442 si_shader_ctx->radeon_bld.soa.outputs[index];
443 args[0] = LLVMBuildLoad(base->gallivm->builder,
444 out_ptr[2 * chan], "");
445 args[1] = LLVMBuildLoad(base->gallivm->builder,
446 out_ptr[2 * chan + 1], "");
447 args[chan + 5] =
448 build_intrinsic(base->gallivm->builder,
449 "llvm.SI.packf16",
450 LLVMInt32TypeInContext(base->gallivm->context),
451 args, 2,
452 LLVMReadNoneAttribute);
453 args[chan + 7] = args[chan + 5];
454 }
455
456 /* Set COMPR flag */
457 args[4] = uint->one;
458 } else {
459 for (chan = 0; chan < 4; chan++ ) {
460 LLVMValueRef out_ptr =
461 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
462 /* +5 because the first output value will be
463 * the 6th argument to the intrinsic. */
464 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
465 out_ptr, "");
466 }
467
468 /* Clear COMPR flag */
469 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200470 }
471
472 /* XXX: This controls which components of the output
473 * registers actually get exported. (e.g bit 0 means export
474 * X component, bit 1 means export Y component, etc.) I'm
475 * hard coding this to 0xf for now. In the future, we might
476 * want to do something else. */
477 args[0] = lp_build_const_int32(base->gallivm, 0xf);
478
479 /* Specify whether the EXEC mask represents the valid mask */
480 args[1] = uint->zero;
481
482 /* Specify whether this is the last export */
483 args[2] = uint->zero;
484
485 /* Specify the target we are exporting */
486 args[3] = lp_build_const_int32(base->gallivm, target);
487
Michel Dänzer26c71392012-08-24 12:03:11 +0200488 /* XXX: We probably need to keep track of the output
489 * values, so we know what we are passing to the next
490 * stage. */
491}
492
Tom Stellardcc571a32012-09-07 09:12:51 -0400493static void si_llvm_emit_prologue(struct lp_build_tgsi_context *bld_base)
494{
495 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
496 struct gallivm_state *gallivm = bld_base->base.gallivm;
497 lp_build_intrinsic_unary(gallivm->builder,
498 "llvm.AMDGPU.shader.type",
499 LLVMVoidTypeInContext(gallivm->context),
500 lp_build_const_int32(gallivm, si_shader_ctx->type));
501}
502
503
Tom Stellarda75c6162012-01-06 17:38:37 -0500504/* XXX: This is partially implemented for VS only at this point. It is not complete */
505static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
506{
507 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200508 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500509 struct lp_build_context * base = &bld_base->base;
510 struct lp_build_context * uint =
511 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
512 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
513 LLVMValueRef last_args[9] = { 0 };
Christian König35088152012-08-01 22:35:24 +0200514 unsigned color_count = 0;
515 unsigned param_count = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500516
517 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500518 struct tgsi_full_declaration *d =
519 &parse->FullToken.FullDeclaration;
520 LLVMValueRef args[9];
521 unsigned target;
522 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500523 int i;
524
525 tgsi_parse_token(parse);
526 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
527 continue;
528
529 switch (d->Declaration.File) {
530 case TGSI_FILE_INPUT:
531 i = shader->ninput++;
532 shader->input[i].name = d->Semantic.Name;
533 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200534 shader->input[i].interpolate = d->Interp.Interpolate;
535 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200536 continue;
537
Tom Stellarda75c6162012-01-06 17:38:37 -0500538 case TGSI_FILE_OUTPUT:
539 i = shader->noutput++;
540 shader->output[i].name = d->Semantic.Name;
541 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200542 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500543 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500544
Christian König35088152012-08-01 22:35:24 +0200545 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500546 continue;
Christian König35088152012-08-01 22:35:24 +0200547 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500548
549 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500550 /* Select the correct target */
551 switch(d->Semantic.Name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400552 case TGSI_SEMANTIC_PSIZE:
Tom Stellarda75c6162012-01-06 17:38:37 -0500553 case TGSI_SEMANTIC_POSITION:
554 target = V_008DFC_SQ_EXP_POS;
555 break;
556 case TGSI_SEMANTIC_COLOR:
557 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
558 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200559 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500560 param_count++;
561 } else {
562 target = V_008DFC_SQ_EXP_MRT + color_count;
563 color_count++;
564 }
565 break;
Michel Dänzer30b30372012-09-06 17:53:04 +0200566 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500567 case TGSI_SEMANTIC_GENERIC:
568 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200569 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500570 param_count++;
571 break;
572 default:
573 target = 0;
574 fprintf(stderr,
575 "Warning: SI unhandled output type:%d\n",
576 d->Semantic.Name);
577 }
578
Michel Dänzer26c71392012-08-24 12:03:11 +0200579 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500580
581 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ?
582 (d->Semantic.Name == TGSI_SEMANTIC_POSITION) :
583 (d->Semantic.Name == TGSI_SEMANTIC_COLOR)) {
584 if (last_args[0]) {
585 lp_build_intrinsic(base->gallivm->builder,
586 "llvm.SI.export",
587 LLVMVoidTypeInContext(base->gallivm->context),
588 last_args, 9);
589 }
590
591 memcpy(last_args, args, sizeof(args));
592 } else {
593 lp_build_intrinsic(base->gallivm->builder,
594 "llvm.SI.export",
595 LLVMVoidTypeInContext(base->gallivm->context),
596 args, 9);
597 }
598
599 }
600 }
601
Christian Königf18fd252012-07-25 21:58:46 +0200602 if (!last_args[0]) {
603 assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
604
605 /* Specify which components to enable */
606 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
607
608 /* Specify the target we are exporting */
609 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
610
611 /* Set COMPR flag to zero to export data as 32-bit */
612 last_args[4] = uint->zero;
613
614 /* dummy bits */
615 last_args[5]= uint->zero;
616 last_args[6]= uint->zero;
617 last_args[7]= uint->zero;
618 last_args[8]= uint->zero;
619 }
620
Tom Stellarda75c6162012-01-06 17:38:37 -0500621 /* Specify whether the EXEC mask represents the valid mask */
622 last_args[1] = lp_build_const_int32(base->gallivm,
623 si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
624
625 /* Specify that this is the last export */
626 last_args[2] = lp_build_const_int32(base->gallivm, 1);
627
628 lp_build_intrinsic(base->gallivm->builder,
629 "llvm.SI.export",
630 LLVMVoidTypeInContext(base->gallivm->context),
631 last_args, 9);
632
633/* XXX: Look up what this function does */
634/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
635}
636
637static void tex_fetch_args(
638 struct lp_build_tgsi_context * bld_base,
639 struct lp_build_emit_data * emit_data)
640{
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200641 const struct tgsi_full_instruction * inst = emit_data->inst;
Tom Stellard467f5162012-05-16 15:15:35 -0400642 LLVMValueRef ptr;
643 LLVMValueRef offset;
644
Tom Stellarda75c6162012-01-06 17:38:37 -0500645 /* WriteMask */
Christian König250b7fd2012-08-01 23:18:14 +0200646 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
647 emit_data->args[0] = lp_build_const_int32(bld_base->base.gallivm, 0xf);
Tom Stellarda75c6162012-01-06 17:38:37 -0500648
649 /* Coordinates */
650 /* XXX: Not all sample instructions need 4 address arguments. */
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200651 if (inst->Instruction.Opcode == TGSI_OPCODE_TXP) {
652 LLVMValueRef src_w;
653 unsigned chan;
654 LLVMValueRef coords[4];
655
656 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
657 src_w = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
658
659 for (chan = 0; chan < 3; chan++ ) {
660 LLVMValueRef arg = lp_build_emit_fetch(bld_base,
661 emit_data->inst, 0, chan);
662 coords[chan] = lp_build_emit_llvm_binary(bld_base,
663 TGSI_OPCODE_DIV,
664 arg, src_w);
665 }
666 coords[3] = bld_base->base.one;
667 emit_data->args[1] = lp_build_gather_values(bld_base->base.gallivm,
668 coords, 4);
669 } else
670 emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst,
671 0, LP_CHAN_ALL);
Tom Stellarda75c6162012-01-06 17:38:37 -0500672
673 /* Resource */
Tom Stellard89ece082012-05-29 11:36:29 -0400674 ptr = use_sgpr(bld_base->base.gallivm, SGPR_CONST_PTR_V8I32, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400675 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200676 emit_data->inst->Src[1].Register.Index);
Tom Stellard467f5162012-05-16 15:15:35 -0400677 emit_data->args[2] = build_indexed_load(bld_base->base.gallivm,
678 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500679
680 /* Sampler */
Tom Stellard89ece082012-05-29 11:36:29 -0400681 ptr = use_sgpr(bld_base->base.gallivm, SGPR_CONST_PTR_V4I32, 2);
Tom Stellard467f5162012-05-16 15:15:35 -0400682 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200683 emit_data->inst->Src[1].Register.Index);
Tom Stellard467f5162012-05-16 15:15:35 -0400684 emit_data->args[3] = build_indexed_load(bld_base->base.gallivm,
685 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500686
687 /* Dimensions */
688 /* XXX: We might want to pass this information to the shader at some. */
689/* emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm,
690 emit_data->inst->Texture.Texture);
691*/
692
Tom Stellard467f5162012-05-16 15:15:35 -0400693 emit_data->arg_count = 4;
Tom Stellarda75c6162012-01-06 17:38:37 -0500694 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
695 * the writemask are clear */
696 emit_data->dst_type = LLVMVectorType(
697 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
698 4);
699}
700
701static const struct lp_build_tgsi_action tex_action = {
702 .fetch_args = tex_fetch_args,
703 .emit = lp_build_tgsi_intrinsic,
704 .intr_name = "llvm.SI.sample"
705};
706
707
708int si_pipe_shader_create(
709 struct pipe_context *ctx,
710 struct si_pipe_shader *shader)
711{
712 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200713 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -0500714 struct si_shader_context si_shader_ctx;
715 struct tgsi_shader_info shader_info;
716 struct lp_build_tgsi_context * bld_base;
717 LLVMModuleRef mod;
718 unsigned char * inst_bytes;
719 unsigned inst_byte_count;
720 unsigned i;
Christian Königd51b9b72012-07-24 18:50:49 +0200721 uint32_t *ptr;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200722 bool dump;
723
724 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -0500725
Michel Dänzercfebaf92012-08-31 19:04:08 +0200726 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -0500727 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
728 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
729
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200730 tgsi_scan_shader(sel->tokens, &shader_info);
Tom Stellarda75c6162012-01-06 17:38:37 -0500731 bld_base->info = &shader_info;
732 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellardcc571a32012-09-07 09:12:51 -0400733 bld_base->emit_prologue = si_llvm_emit_prologue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500734 bld_base->emit_epilogue = si_llvm_emit_epilogue;
735
736 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200737 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -0500738
739 si_shader_ctx.radeon_bld.load_input = declare_input;
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200740 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -0500741 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
742 si_shader_ctx.shader = shader;
743 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
744 si_shader_ctx.rctx = rctx;
745
Christian König835098a2012-07-17 21:28:10 +0200746 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -0500747
Tom Stellard185fc9a2012-07-12 10:40:47 -0400748 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
749 * conversion fails. */
750 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200751 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -0400752 }
753
Michel Dänzerd1e40b32012-08-23 17:10:37 +0200754 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +0200755 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
756 return -EINVAL;
757 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500758
759 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
760
761 mod = bld_base->base.gallivm->module;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200762 if (dump) {
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +0200763 LLVMDumpModule(mod);
764 }
765 radeon_llvm_compile(mod, &inst_bytes, &inst_byte_count, "SI", dump);
766 if (dump) {
767 fprintf(stderr, "SI CODE:\n");
768 for (i = 0; i < inst_byte_count; i+=4 ) {
769 fprintf(stderr, "%02x%02x%02x%02x\n", inst_bytes[i + 3],
770 inst_bytes[i + 2], inst_bytes[i + 1],
771 inst_bytes[i]);
772 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500773 }
774
775 shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)inst_bytes);
776 shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 4));
777 shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 8));
778
Michel Dänzer4b64fa22012-08-15 18:22:46 +0200779 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -0500780 tgsi_parse_free(&si_shader_ctx.parse);
781
782 /* copy new shader */
Christian Königd51b9b72012-07-24 18:50:49 +0200783 si_resource_reference(&shader->bo, NULL);
784 shader->bo = si_resource_create_custom(ctx->screen, PIPE_USAGE_IMMUTABLE,
785 inst_byte_count - 12);
Tom Stellarda75c6162012-01-06 17:38:37 -0500786 if (shader->bo == NULL) {
Christian Königd51b9b72012-07-24 18:50:49 +0200787 return -ENOMEM;
Tom Stellarda75c6162012-01-06 17:38:37 -0500788 }
789
Christian Königd51b9b72012-07-24 18:50:49 +0200790 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
791 if (0 /*R600_BIG_ENDIAN*/) {
792 for (i = 0; i < (inst_byte_count-12)/4; ++i) {
793 ptr[i] = util_bswap32(*(uint32_t*)(inst_bytes+12 + i*4));
794 }
795 } else {
796 memcpy(ptr, inst_bytes + 12, inst_byte_count - 12);
797 }
798 rctx->ws->buffer_unmap(shader->bo->cs_buf);
799
Tom Stellarda75c6162012-01-06 17:38:37 -0500800 free(inst_bytes);
801
802 return 0;
803}
804
805void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
806{
Christian Königfe412872012-07-24 18:47:19 +0200807 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -0500808}