blob: 7922928043f48339a178b3184b11e400195f874b [file] [log] [blame]
Tom Stellarda75c6162012-01-06 17:38:37 -05001
Christian Königce40e472012-08-02 12:14:59 +02002/*
3 * Copyright 2012 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Tom Stellard <thomas.stellard@amd.com>
26 * Michel Dänzer <michel.daenzer@amd.com>
27 * Christian König <christian.koenig@amd.com>
28 */
29
Tom Stellarda75c6162012-01-06 17:38:37 -050030#include "gallivm/lp_bld_tgsi_action.h"
31#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020032#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050033#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010034#include "gallivm/lp_bld_logic.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050035#include "gallivm/lp_bld_tgsi.h"
36#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040037#include "radeon_llvm_emit.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "tgsi/tgsi_info.h"
39#include "tgsi/tgsi_parse.h"
40#include "tgsi/tgsi_scan.h"
41#include "tgsi/tgsi_dump.h"
42
43#include "radeonsi_pipe.h"
44#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020045#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050046#include "sid.h"
47
48#include <assert.h>
49#include <errno.h>
50#include <stdio.h>
51
Tom Stellarda75c6162012-01-06 17:38:37 -050052struct si_shader_context
53{
54 struct radeon_llvm_context radeon_bld;
55 struct r600_context *rctx;
56 struct tgsi_parse_context parse;
57 struct tgsi_token * tokens;
58 struct si_pipe_shader *shader;
Michel Dänzer44ef0332012-10-05 16:59:10 +020059 struct si_shader_key key;
Tom Stellarda75c6162012-01-06 17:38:37 -050060 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Michel Dänzercfebaf92012-08-31 19:04:08 +020061 unsigned ninput_emitted;
Tom Stellarda75c6162012-01-06 17:38:37 -050062/* struct list_head inputs; */
63/* unsigned * input_mappings *//* From TGSI to SI hw */
64/* struct tgsi_shader_info info;*/
65};
66
67static struct si_shader_context * si_shader_context(
68 struct lp_build_tgsi_context * bld_base)
69{
70 return (struct si_shader_context *)bld_base;
71}
72
73
74#define PERSPECTIVE_BASE 0
75#define LINEAR_BASE 9
76
77#define SAMPLE_OFFSET 0
78#define CENTER_OFFSET 2
79#define CENTROID_OFSET 4
80
81#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040082#define CONST_ADDR_SPACE 2
Tom Stellard89ece082012-05-29 11:36:29 -040083#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050084
85enum sgpr_type {
Tom Stellard467f5162012-05-16 15:15:35 -040086 SGPR_CONST_PTR_F32,
87 SGPR_CONST_PTR_V4I32,
88 SGPR_CONST_PTR_V8I32,
Tom Stellarda75c6162012-01-06 17:38:37 -050089 SGPR_I32,
Tom Stellard467f5162012-05-16 15:15:35 -040090 SGPR_I64
Tom Stellarda75c6162012-01-06 17:38:37 -050091};
92
Tom Stellard467f5162012-05-16 15:15:35 -040093/**
94 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
95 *
96 * @param offset The offset parameter specifies the number of
97 * elements to offset, not the number of bytes or dwords. An element is the
98 * the type pointed to by the base_ptr parameter (e.g. int is the element of
99 * an int* pointer)
100 *
101 * When LLVM lowers the load instruction, it will convert the element offset
102 * into a dword offset automatically.
103 *
104 */
105static LLVMValueRef build_indexed_load(
106 struct gallivm_state * gallivm,
107 LLVMValueRef base_ptr,
108 LLVMValueRef offset)
109{
110 LLVMValueRef computed_ptr = LLVMBuildGEP(
111 gallivm->builder, base_ptr, &offset, 1, "");
112
113 return LLVMBuildLoad(gallivm->builder, computed_ptr, "");
114}
115
Tom Stellard89ece082012-05-29 11:36:29 -0400116/**
117 * Load a value stored in one of the user SGPRs
118 *
119 * @param sgpr This is the sgpr to load the value from. If you need to load a
120 * value that is stored in consecutive SGPR registers (e.g. a 64-bit pointer),
121 * then you should pass the index of the first SGPR that holds the value. For
122 * example, if you want to load a pointer that is stored in SGPRs 2 and 3, then
123 * use pass 2 for the sgpr parameter.
124 *
125 * The value of the sgpr parameter must also be aligned to the width of the type
126 * being loaded, so that the sgpr parameter is divisible by the dword width of the
127 * type. For example, if the value being loaded is two dwords wide, then the sgpr
128 * parameter must be divisible by two.
Tom Stellard467f5162012-05-16 15:15:35 -0400129 */
Tom Stellarda75c6162012-01-06 17:38:37 -0500130static LLVMValueRef use_sgpr(
131 struct gallivm_state * gallivm,
132 enum sgpr_type type,
133 unsigned sgpr)
134{
135 LLVMValueRef sgpr_index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500136 LLVMTypeRef ret_type;
Tom Stellard89ece082012-05-29 11:36:29 -0400137 LLVMValueRef ptr;
Tom Stellarda75c6162012-01-06 17:38:37 -0500138
139 sgpr_index = lp_build_const_int32(gallivm, sgpr);
140
Tom Stellard467f5162012-05-16 15:15:35 -0400141 switch (type) {
142 case SGPR_CONST_PTR_F32:
Tom Stellard89ece082012-05-29 11:36:29 -0400143 assert(sgpr % 2 == 0);
Tom Stellard467f5162012-05-16 15:15:35 -0400144 ret_type = LLVMFloatTypeInContext(gallivm->context);
145 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400146 break;
147
Tom Stellard467f5162012-05-16 15:15:35 -0400148 case SGPR_I32:
Tom Stellarda75c6162012-01-06 17:38:37 -0500149 ret_type = LLVMInt32TypeInContext(gallivm->context);
Tom Stellard89ece082012-05-29 11:36:29 -0400150 break;
151
Tom Stellard467f5162012-05-16 15:15:35 -0400152 case SGPR_I64:
Tom Stellard89ece082012-05-29 11:36:29 -0400153 assert(sgpr % 2 == 0);
Tom Stellard467f5162012-05-16 15:15:35 -0400154 ret_type= LLVMInt64TypeInContext(gallivm->context);
Tom Stellard89ece082012-05-29 11:36:29 -0400155 break;
156
Tom Stellard467f5162012-05-16 15:15:35 -0400157 case SGPR_CONST_PTR_V4I32:
Tom Stellard89ece082012-05-29 11:36:29 -0400158 assert(sgpr % 2 == 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500159 ret_type = LLVMInt32TypeInContext(gallivm->context);
160 ret_type = LLVMVectorType(ret_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400161 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400162 break;
163
Tom Stellard467f5162012-05-16 15:15:35 -0400164 case SGPR_CONST_PTR_V8I32:
Tom Stellard89ece082012-05-29 11:36:29 -0400165 assert(sgpr % 2 == 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500166 ret_type = LLVMInt32TypeInContext(gallivm->context);
167 ret_type = LLVMVectorType(ret_type, 8);
Tom Stellard467f5162012-05-16 15:15:35 -0400168 ret_type = LLVMPointerType(ret_type, CONST_ADDR_SPACE);
Tom Stellard89ece082012-05-29 11:36:29 -0400169 break;
170
Tom Stellarda75c6162012-01-06 17:38:37 -0500171 default:
172 assert(!"Unsupported SGPR type in use_sgpr()");
173 return NULL;
174 }
Tom Stellard89ece082012-05-29 11:36:29 -0400175
176 ret_type = LLVMPointerType(ret_type, USER_SGPR_ADDR_SPACE);
177 ptr = LLVMBuildIntToPtr(gallivm->builder, sgpr_index, ret_type, "");
178 return LLVMBuildLoad(gallivm->builder, ptr, "");
Tom Stellarda75c6162012-01-06 17:38:37 -0500179}
180
181static void declare_input_vs(
182 struct si_shader_context * si_shader_ctx,
183 unsigned input_index,
184 const struct tgsi_full_declaration *decl)
185{
186 LLVMValueRef t_list_ptr;
187 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400188 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500189 LLVMValueRef attribute_offset;
190 LLVMValueRef buffer_index_reg;
Tom Stellard467f5162012-05-16 15:15:35 -0400191 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500192 LLVMTypeRef vec4_type;
193 LLVMValueRef input;
194 struct lp_build_context * uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
195 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Christian Königb15e3ae2012-07-25 11:22:59 +0200196 //struct pipe_vertex_element *velem = &rctx->vertex_elements->elements[input_index];
Tom Stellarda75c6162012-01-06 17:38:37 -0500197 unsigned chan;
198
Tom Stellard467f5162012-05-16 15:15:35 -0400199 /* Load the T list */
Christian König22ae0622012-09-26 20:42:23 +0200200 t_list_ptr = use_sgpr(base->gallivm, SGPR_CONST_PTR_V4I32, SI_SGPR_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500201
Christian Königb15e3ae2012-07-25 11:22:59 +0200202 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400203
204 t_list = build_indexed_load(base->gallivm, t_list_ptr, t_offset);
205
206 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200207 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500208
209 /* Load the buffer index is always, which is always stored in VGPR0
210 * for Vertex Shaders */
Tom Stellard40c41fe2012-07-25 19:17:16 +0000211 buffer_index_reg = build_intrinsic(base->gallivm->builder,
212 "llvm.SI.vs.load.buffer.index", uint->elem_type, NULL, 0,
213 LLVMReadNoneAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500214
215 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400216 args[0] = t_list;
217 args[1] = attribute_offset;
218 args[2] = buffer_index_reg;
Tom Stellarda75c6162012-01-06 17:38:37 -0500219 input = lp_build_intrinsic(base->gallivm->builder,
Tom Stellard467f5162012-05-16 15:15:35 -0400220 "llvm.SI.vs.load.input", vec4_type, args, 3);
Tom Stellarda75c6162012-01-06 17:38:37 -0500221
222 /* Break up the vec4 into individual components */
223 for (chan = 0; chan < 4; chan++) {
224 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
225 /* XXX: Use a helper function for this. There is one in
226 * tgsi_llvm.c. */
227 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
228 LLVMBuildExtractElement(base->gallivm->builder,
229 input, llvm_chan, "");
230 }
231}
232
233static void declare_input_fs(
234 struct si_shader_context * si_shader_ctx,
235 unsigned input_index,
236 const struct tgsi_full_declaration *decl)
237{
238 const char * intr_name;
239 unsigned chan;
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200240 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500241 struct lp_build_context * base =
242 &si_shader_ctx->radeon_bld.soa.bld_base.base;
243 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400244 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Tom Stellarda75c6162012-01-06 17:38:37 -0500245
246 /* This value is:
247 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
248 * quad begins a new primitive. Bit 0 always needs
249 * to be unset)
250 * [32:16] ParamOffset
251 *
252 */
Christian König22ae0622012-09-26 20:42:23 +0200253 LLVMValueRef params = use_sgpr(base->gallivm, SGPR_I32, SI_PS_NUM_USER_SGPR);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200254 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500255
Tom Stellard0fb1e682012-09-06 16:18:11 -0400256 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
257 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
258 LLVMValueRef args[1];
259 unsigned soa_index =
260 radeon_llvm_reg_index_soa(input_index, chan);
261 args[0] = lp_build_const_int32(gallivm, chan);
262 si_shader_ctx->radeon_bld.inputs[soa_index] =
263 build_intrinsic(base->gallivm->builder,
264 "llvm.SI.fs.read.pos", input_type,
265 args, 1, LLVMReadNoneAttribute);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100266
267 if (chan == 3)
268 /* RCP for fragcoord.w */
269 si_shader_ctx->radeon_bld.inputs[soa_index] =
270 LLVMBuildFDiv(gallivm->builder,
271 lp_build_const_float(gallivm, 1.0f),
272 si_shader_ctx->radeon_bld.inputs[soa_index],
273 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400274 }
275 return;
276 }
277
Michel Dänzer97078b12012-09-25 12:41:31 +0200278 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
279 LLVMValueRef face, is_face_positive;
280
281 face = build_intrinsic(gallivm->builder,
282 "llvm.SI.fs.read.face",
283 input_type,
284 NULL, 0, LLVMReadNoneAttribute);
285 is_face_positive = LLVMBuildFCmp(gallivm->builder,
286 LLVMRealUGT, face,
287 lp_build_const_float(gallivm, 0.0f),
288 "");
289
290 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
291 LLVMBuildSelect(gallivm->builder,
292 is_face_positive,
293 lp_build_const_float(gallivm, 1.0f),
294 lp_build_const_float(gallivm, 0.0f),
295 "");
296 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
297 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
298 lp_build_const_float(gallivm, 0.0f);
299 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
300 lp_build_const_float(gallivm, 1.0f);
301
302 return;
303 }
304
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200305 shader->input[input_index].param_offset = shader->ninterp++;
306 attr_number = lp_build_const_int32(gallivm,
307 shader->input[input_index].param_offset);
308
Tom Stellarda75c6162012-01-06 17:38:37 -0500309 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200310 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500311 case TGSI_INTERPOLATE_COLOR:
Michel Dänzer18272c92013-02-13 12:54:13 +0100312 if (si_shader_ctx->key.flatshade) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500313 intr_name = "llvm.SI.fs.interp.constant";
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200314 } else {
315 if (decl->Interp.Centroid)
316 intr_name = "llvm.SI.fs.interp.persp.centroid";
317 else
318 intr_name = "llvm.SI.fs.interp.persp.center";
319 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500320 break;
321 case TGSI_INTERPOLATE_CONSTANT:
322 intr_name = "llvm.SI.fs.interp.constant";
323 break;
324 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200325 if (decl->Interp.Centroid)
326 intr_name = "llvm.SI.fs.interp.linear.centroid";
327 else
328 intr_name = "llvm.SI.fs.interp.linear.center";
329 break;
330 case TGSI_INTERPOLATE_PERSPECTIVE:
331 if (decl->Interp.Centroid)
332 intr_name = "llvm.SI.fs.interp.persp.centroid";
333 else
334 intr_name = "llvm.SI.fs.interp.persp.center";
Tom Stellarda75c6162012-01-06 17:38:37 -0500335 break;
336 default:
337 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
338 return;
339 }
340
Michel Dänzercfebaf92012-08-31 19:04:08 +0200341 if (!si_shader_ctx->ninput_emitted++) {
342 /* Enable whole quad mode */
343 lp_build_intrinsic(gallivm->builder,
344 "llvm.SI.wqm",
345 LLVMVoidTypeInContext(gallivm->context),
346 NULL, 0);
347 }
348
Tom Stellarda75c6162012-01-06 17:38:37 -0500349 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200350 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
351 si_shader_ctx->key.color_two_side) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500352 LLVMValueRef args[3];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200353 LLVMValueRef face, is_face_positive;
354 LLVMValueRef back_attr_number =
355 lp_build_const_int32(gallivm,
356 shader->input[input_index].param_offset + 1);
357
358 face = build_intrinsic(gallivm->builder,
359 "llvm.SI.fs.read.face",
360 input_type,
361 NULL, 0, LLVMReadNoneAttribute);
362 is_face_positive = LLVMBuildFCmp(gallivm->builder,
363 LLVMRealUGT, face,
364 lp_build_const_float(gallivm, 0.0f),
365 "");
366
Tom Stellarda75c6162012-01-06 17:38:37 -0500367 args[2] = params;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200368 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
369 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
370 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
371 LLVMValueRef front, back;
372
373 args[0] = llvm_chan;
374 args[1] = attr_number;
375 front = build_intrinsic(base->gallivm->builder, intr_name,
376 input_type, args, 3, LLVMReadOnlyAttribute);
377
378 args[1] = back_attr_number;
379 back = build_intrinsic(base->gallivm->builder, intr_name,
380 input_type, args, 3, LLVMReadOnlyAttribute);
381
382 si_shader_ctx->radeon_bld.inputs[soa_index] =
383 LLVMBuildSelect(gallivm->builder,
384 is_face_positive,
385 front,
386 back,
387 "");
388 }
389
390 shader->ninterp++;
391 } else {
392 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
393 LLVMValueRef args[3];
394 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
395 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
396 args[0] = llvm_chan;
397 args[1] = attr_number;
398 args[2] = params;
399 si_shader_ctx->radeon_bld.inputs[soa_index] =
400 build_intrinsic(base->gallivm->builder, intr_name,
401 input_type, args, 3, LLVMReadOnlyAttribute);
402 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500403 }
404}
405
406static void declare_input(
407 struct radeon_llvm_context * radeon_bld,
408 unsigned input_index,
409 const struct tgsi_full_declaration *decl)
410{
411 struct si_shader_context * si_shader_ctx =
412 si_shader_context(&radeon_bld->soa.bld_base);
413 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
414 declare_input_vs(si_shader_ctx, input_index, decl);
415 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
416 declare_input_fs(si_shader_ctx, input_index, decl);
417 } else {
418 fprintf(stderr, "Warning: Unsupported shader type,\n");
419 }
420}
421
422static LLVMValueRef fetch_constant(
423 struct lp_build_tgsi_context * bld_base,
424 const struct tgsi_full_src_register *reg,
425 enum tgsi_opcode_type type,
426 unsigned swizzle)
427{
428 struct lp_build_context * base = &bld_base->base;
Christian König88a4fd82012-08-29 10:48:01 +0200429 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500430
431 LLVMValueRef const_ptr;
432 LLVMValueRef offset;
Tom Stellard022b5432012-07-25 08:23:52 -0400433 LLVMValueRef load;
Tom Stellarda75c6162012-01-06 17:38:37 -0500434
Christian König8514f5a2013-02-04 17:46:42 +0100435 if (swizzle == LP_CHAN_ALL) {
436 unsigned chan;
437 LLVMValueRef values[4];
438 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
439 values[chan] = fetch_constant(bld_base, reg, type, chan);
440
441 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
442 }
443
Christian Könige7723b52012-08-24 12:55:34 +0200444 /* currently not supported */
445 if (reg->Register.Indirect) {
446 assert(0);
447 load = lp_build_const_int32(base->gallivm, 0);
448 return bitcast(bld_base, type, load);
449 }
450
Christian König22ae0622012-09-26 20:42:23 +0200451 const_ptr = use_sgpr(base->gallivm, SGPR_CONST_PTR_F32, SI_SGPR_CONST);
Tom Stellarda75c6162012-01-06 17:38:37 -0500452
453 /* XXX: This assumes that the constant buffer is not packed, so
454 * CONST[0].x will have an offset of 0 and CONST[1].x will have an
455 * offset of 4. */
Christian König88a4fd82012-08-29 10:48:01 +0200456 idx = (reg->Register.Index * 4) + swizzle;
Christian König88a4fd82012-08-29 10:48:01 +0200457 offset = lp_build_const_int32(base->gallivm, idx);
Tom Stellarda75c6162012-01-06 17:38:37 -0500458
Tom Stellard022b5432012-07-25 08:23:52 -0400459 load = build_indexed_load(base->gallivm, const_ptr, offset);
460 return bitcast(bld_base, type, load);
Tom Stellarda75c6162012-01-06 17:38:37 -0500461}
462
Michel Dänzer26c71392012-08-24 12:03:11 +0200463/* Initialize arguments for the shader export intrinsic */
464static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
465 struct tgsi_full_declaration *d,
466 unsigned index,
467 unsigned target,
468 LLVMValueRef *args)
469{
470 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
471 struct lp_build_context *uint =
472 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
473 struct lp_build_context *base = &bld_base->base;
474 unsigned compressed = 0;
475 unsigned chan;
476
Michel Dänzerf402acd2012-08-22 18:15:36 +0200477 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
478 int cbuf = target - V_008DFC_SQ_EXP_MRT;
479
480 if (cbuf >= 0 && cbuf < 8) {
481 struct r600_context *rctx = si_shader_ctx->rctx;
Michel Dänzer44ef0332012-10-05 16:59:10 +0200482 compressed = (si_shader_ctx->key.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100483
484 if (compressed)
485 si_shader_ctx->shader->spi_shader_col_format |=
486 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
487 else
488 si_shader_ctx->shader->spi_shader_col_format |=
489 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200490 }
491 }
492
493 if (compressed) {
494 /* Pixel shader needs to pack output values before export */
495 for (chan = 0; chan < 2; chan++ ) {
496 LLVMValueRef *out_ptr =
497 si_shader_ctx->radeon_bld.soa.outputs[index];
498 args[0] = LLVMBuildLoad(base->gallivm->builder,
499 out_ptr[2 * chan], "");
500 args[1] = LLVMBuildLoad(base->gallivm->builder,
501 out_ptr[2 * chan + 1], "");
502 args[chan + 5] =
503 build_intrinsic(base->gallivm->builder,
504 "llvm.SI.packf16",
505 LLVMInt32TypeInContext(base->gallivm->context),
506 args, 2,
507 LLVMReadNoneAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100508 args[chan + 7] = args[chan + 5] =
509 LLVMBuildBitCast(base->gallivm->builder,
510 args[chan + 5],
511 LLVMFloatTypeInContext(base->gallivm->context),
512 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200513 }
514
515 /* Set COMPR flag */
516 args[4] = uint->one;
517 } else {
518 for (chan = 0; chan < 4; chan++ ) {
519 LLVMValueRef out_ptr =
520 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
521 /* +5 because the first output value will be
522 * the 6th argument to the intrinsic. */
523 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
524 out_ptr, "");
525 }
526
527 /* Clear COMPR flag */
528 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200529 }
530
531 /* XXX: This controls which components of the output
532 * registers actually get exported. (e.g bit 0 means export
533 * X component, bit 1 means export Y component, etc.) I'm
534 * hard coding this to 0xf for now. In the future, we might
535 * want to do something else. */
536 args[0] = lp_build_const_int32(base->gallivm, 0xf);
537
538 /* Specify whether the EXEC mask represents the valid mask */
539 args[1] = uint->zero;
540
541 /* Specify whether this is the last export */
542 args[2] = uint->zero;
543
544 /* Specify the target we are exporting */
545 args[3] = lp_build_const_int32(base->gallivm, target);
546
Michel Dänzer26c71392012-08-24 12:03:11 +0200547 /* XXX: We probably need to keep track of the output
548 * values, so we know what we are passing to the next
549 * stage. */
550}
551
Tom Stellardcc571a32012-09-07 09:12:51 -0400552static void si_llvm_emit_prologue(struct lp_build_tgsi_context *bld_base)
553{
554 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
555 struct gallivm_state *gallivm = bld_base->base.gallivm;
556 lp_build_intrinsic_unary(gallivm->builder,
557 "llvm.AMDGPU.shader.type",
558 LLVMVoidTypeInContext(gallivm->context),
559 lp_build_const_int32(gallivm, si_shader_ctx->type));
560}
561
562
Michel Dänzer7708a862012-11-02 15:57:30 +0100563static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
564 unsigned index)
565{
566 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
567 struct gallivm_state *gallivm = bld_base->base.gallivm;
568
569 if (si_shader_ctx->key.alpha_func != PIPE_FUNC_NEVER) {
570 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
571 LLVMValueRef alpha_pass =
572 lp_build_cmp(&bld_base->base,
573 si_shader_ctx->key.alpha_func,
574 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
575 lp_build_const_float(gallivm, si_shader_ctx->key.alpha_ref));
576 LLVMValueRef arg =
577 lp_build_select(&bld_base->base,
578 alpha_pass,
579 lp_build_const_float(gallivm, 1.0f),
580 lp_build_const_float(gallivm, -1.0f));
581
582 build_intrinsic(gallivm->builder,
583 "llvm.AMDGPU.kill",
584 LLVMVoidTypeInContext(gallivm->context),
585 &arg, 1, 0);
586 } else {
587 build_intrinsic(gallivm->builder,
588 "llvm.AMDGPU.kilp",
589 LLVMVoidTypeInContext(gallivm->context),
590 NULL, 0, 0);
591 }
592}
593
Tom Stellarda75c6162012-01-06 17:38:37 -0500594/* XXX: This is partially implemented for VS only at this point. It is not complete */
595static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
596{
597 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200598 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500599 struct lp_build_context * base = &bld_base->base;
600 struct lp_build_context * uint =
601 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
602 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100603 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500604 LLVMValueRef last_args[9] = { 0 };
Christian König35088152012-08-01 22:35:24 +0200605 unsigned color_count = 0;
606 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100607 int depth_index = -1, stencil_index = -1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500608
609 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500610 struct tgsi_full_declaration *d =
611 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500612 unsigned target;
613 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500614 int i;
615
616 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100617
618 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
619 parse->FullToken.FullProperty.Property.PropertyName ==
620 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
621 shader->fs_write_all = TRUE;
622
Tom Stellarda75c6162012-01-06 17:38:37 -0500623 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
624 continue;
625
626 switch (d->Declaration.File) {
627 case TGSI_FILE_INPUT:
628 i = shader->ninput++;
629 shader->input[i].name = d->Semantic.Name;
630 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200631 shader->input[i].interpolate = d->Interp.Interpolate;
632 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200633 continue;
634
Tom Stellarda75c6162012-01-06 17:38:37 -0500635 case TGSI_FILE_OUTPUT:
636 i = shader->noutput++;
637 shader->output[i].name = d->Semantic.Name;
638 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200639 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500640 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500641
Christian König35088152012-08-01 22:35:24 +0200642 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500643 continue;
Christian König35088152012-08-01 22:35:24 +0200644 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500645
646 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500647 /* Select the correct target */
648 switch(d->Semantic.Name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400649 case TGSI_SEMANTIC_PSIZE:
Tom Stellarda75c6162012-01-06 17:38:37 -0500650 target = V_008DFC_SQ_EXP_POS;
651 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100652 case TGSI_SEMANTIC_POSITION:
653 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
654 target = V_008DFC_SQ_EXP_POS;
655 break;
656 } else {
657 depth_index = index;
658 continue;
659 }
660 case TGSI_SEMANTIC_STENCIL:
661 stencil_index = index;
662 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500663 case TGSI_SEMANTIC_COLOR:
664 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200665 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500666 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200667 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500668 param_count++;
669 } else {
670 target = V_008DFC_SQ_EXP_MRT + color_count;
Michel Dänzer7708a862012-11-02 15:57:30 +0100671 if (color_count == 0 &&
672 si_shader_ctx->key.alpha_func != PIPE_FUNC_ALWAYS)
673 si_alpha_test(bld_base, index);
674
Tom Stellarda75c6162012-01-06 17:38:37 -0500675 color_count++;
676 }
677 break;
Michel Dänzer30b30372012-09-06 17:53:04 +0200678 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500679 case TGSI_SEMANTIC_GENERIC:
680 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200681 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500682 param_count++;
683 break;
684 default:
685 target = 0;
686 fprintf(stderr,
687 "Warning: SI unhandled output type:%d\n",
688 d->Semantic.Name);
689 }
690
Michel Dänzer26c71392012-08-24 12:03:11 +0200691 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500692
693 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ?
694 (d->Semantic.Name == TGSI_SEMANTIC_POSITION) :
695 (d->Semantic.Name == TGSI_SEMANTIC_COLOR)) {
696 if (last_args[0]) {
697 lp_build_intrinsic(base->gallivm->builder,
698 "llvm.SI.export",
699 LLVMVoidTypeInContext(base->gallivm->context),
700 last_args, 9);
701 }
702
703 memcpy(last_args, args, sizeof(args));
704 } else {
705 lp_build_intrinsic(base->gallivm->builder,
706 "llvm.SI.export",
707 LLVMVoidTypeInContext(base->gallivm->context),
708 args, 9);
709 }
710
711 }
712 }
713
Michel Dänzer1a616c12012-11-13 17:35:09 +0100714 if (depth_index >= 0 || stencil_index >= 0) {
715 LLVMValueRef out_ptr;
716 unsigned mask = 0;
717
718 /* Specify the target we are exporting */
719 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
720
721 if (depth_index >= 0) {
722 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
723 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
724 mask |= 0x1;
725
726 if (stencil_index < 0) {
727 args[6] =
728 args[7] =
729 args[8] = args[5];
730 }
731 }
732
733 if (stencil_index >= 0) {
734 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
735 args[7] =
736 args[8] =
737 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
738 mask |= 0x2;
739
740 if (depth_index < 0)
741 args[5] = args[6];
742 }
743
744 /* Specify which components to enable */
745 args[0] = lp_build_const_int32(base->gallivm, mask);
746
747 args[1] =
748 args[2] =
749 args[4] = uint->zero;
750
751 if (last_args[0])
752 lp_build_intrinsic(base->gallivm->builder,
753 "llvm.SI.export",
754 LLVMVoidTypeInContext(base->gallivm->context),
755 args, 9);
756 else
757 memcpy(last_args, args, sizeof(args));
758 }
759
Christian Königf18fd252012-07-25 21:58:46 +0200760 if (!last_args[0]) {
761 assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
762
763 /* Specify which components to enable */
764 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
765
766 /* Specify the target we are exporting */
767 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
768
769 /* Set COMPR flag to zero to export data as 32-bit */
770 last_args[4] = uint->zero;
771
772 /* dummy bits */
773 last_args[5]= uint->zero;
774 last_args[6]= uint->zero;
775 last_args[7]= uint->zero;
776 last_args[8]= uint->zero;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100777
778 si_shader_ctx->shader->spi_shader_col_format |=
779 V_028714_SPI_SHADER_32_ABGR;
Christian Königf18fd252012-07-25 21:58:46 +0200780 }
781
Tom Stellarda75c6162012-01-06 17:38:37 -0500782 /* Specify whether the EXEC mask represents the valid mask */
783 last_args[1] = lp_build_const_int32(base->gallivm,
784 si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
785
Michel Dänzerc8402702013-02-12 18:37:22 +0100786 if (shader->fs_write_all && shader->nr_cbufs > 1) {
787 int i;
788
789 /* Specify that this is not yet the last export */
790 last_args[2] = lp_build_const_int32(base->gallivm, 0);
791
792 for (i = 1; i < shader->nr_cbufs; i++) {
793 /* Specify the target we are exporting */
794 last_args[3] = lp_build_const_int32(base->gallivm,
795 V_008DFC_SQ_EXP_MRT + i);
796
797 lp_build_intrinsic(base->gallivm->builder,
798 "llvm.SI.export",
799 LLVMVoidTypeInContext(base->gallivm->context),
800 last_args, 9);
801
802 si_shader_ctx->shader->spi_shader_col_format |=
803 si_shader_ctx->shader->spi_shader_col_format << 4;
804 }
805
806 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
807 }
808
Tom Stellarda75c6162012-01-06 17:38:37 -0500809 /* Specify that this is the last export */
810 last_args[2] = lp_build_const_int32(base->gallivm, 1);
811
812 lp_build_intrinsic(base->gallivm->builder,
813 "llvm.SI.export",
814 LLVMVoidTypeInContext(base->gallivm->context),
815 last_args, 9);
816
817/* XXX: Look up what this function does */
818/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
819}
820
821static void tex_fetch_args(
822 struct lp_build_tgsi_context * bld_base,
823 struct lp_build_emit_data * emit_data)
824{
Michel Dänzere5fb7342013-01-24 18:54:51 +0100825 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200826 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100827 unsigned opcode = inst->Instruction.Opcode;
828 unsigned target = inst->Texture.Texture;
Tom Stellard467f5162012-05-16 15:15:35 -0400829 LLVMValueRef ptr;
830 LLVMValueRef offset;
Michel Dänzer120efee2013-01-25 12:10:11 +0100831 LLVMValueRef coords[4];
832 LLVMValueRef address[16];
833 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100834 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400835
Tom Stellarda75c6162012-01-06 17:38:37 -0500836 /* WriteMask */
Christian König250b7fd2012-08-01 23:18:14 +0200837 /* XXX: should be optimized using emit_data->inst->Dst[0].Register.WriteMask*/
838 emit_data->args[0] = lp_build_const_int32(bld_base->base.gallivm, 0xf);
Tom Stellarda75c6162012-01-06 17:38:37 -0500839
Michel Dänzer120efee2013-01-25 12:10:11 +0100840 /* Fetch and project texture coordinates */
841 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100842 for (chan = 0; chan < 3; chan++ ) {
843 coords[chan] = lp_build_emit_fetch(bld_base,
844 emit_data->inst, 0,
845 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100846 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200847 coords[chan] = lp_build_emit_llvm_binary(bld_base,
848 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100849 coords[chan],
850 coords[3]);
851 }
852
Michel Dänzer120efee2013-01-25 12:10:11 +0100853 if (opcode == TGSI_OPCODE_TXP)
854 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500855
Michel Dänzer120efee2013-01-25 12:10:11 +0100856 /* Pack LOD bias value */
857 if (opcode == TGSI_OPCODE_TXB)
858 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400859
Michel Dänzer120efee2013-01-25 12:10:11 +0100860 if ((target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE) &&
861 opcode != TGSI_OPCODE_TXQ)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100862 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100863
864 /* Pack depth comparison value */
865 switch (target) {
866 case TGSI_TEXTURE_SHADOW1D:
867 case TGSI_TEXTURE_SHADOW1D_ARRAY:
868 case TGSI_TEXTURE_SHADOW2D:
869 case TGSI_TEXTURE_SHADOWRECT:
870 address[count++] = coords[2];
871 break;
872 case TGSI_TEXTURE_SHADOWCUBE:
873 case TGSI_TEXTURE_SHADOW2D_ARRAY:
874 address[count++] = coords[3];
875 break;
876 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
877 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +0100878 }
879
Michel Dänzer120efee2013-01-25 12:10:11 +0100880 /* Pack texture coordinates */
881 address[count++] = coords[0];
882 switch (target) {
883 case TGSI_TEXTURE_2D:
884 case TGSI_TEXTURE_2D_ARRAY:
885 case TGSI_TEXTURE_3D:
886 case TGSI_TEXTURE_CUBE:
887 case TGSI_TEXTURE_RECT:
888 case TGSI_TEXTURE_SHADOW2D:
889 case TGSI_TEXTURE_SHADOWRECT:
890 case TGSI_TEXTURE_SHADOW2D_ARRAY:
891 case TGSI_TEXTURE_SHADOWCUBE:
892 case TGSI_TEXTURE_2D_MSAA:
893 case TGSI_TEXTURE_2D_ARRAY_MSAA:
894 case TGSI_TEXTURE_CUBE_ARRAY:
895 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
896 address[count++] = coords[1];
897 }
898 switch (target) {
899 case TGSI_TEXTURE_3D:
900 case TGSI_TEXTURE_CUBE:
901 case TGSI_TEXTURE_SHADOWCUBE:
902 case TGSI_TEXTURE_CUBE_ARRAY:
903 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
904 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +0100905 }
906
Michel Dänzer120efee2013-01-25 12:10:11 +0100907 /* Pack array slice */
908 switch (target) {
909 case TGSI_TEXTURE_1D_ARRAY:
910 address[count++] = coords[1];
911 }
912 switch (target) {
913 case TGSI_TEXTURE_2D_ARRAY:
914 case TGSI_TEXTURE_2D_ARRAY_MSAA:
915 case TGSI_TEXTURE_SHADOW2D_ARRAY:
916 address[count++] = coords[2];
917 }
918 switch (target) {
919 case TGSI_TEXTURE_CUBE_ARRAY:
920 case TGSI_TEXTURE_SHADOW1D_ARRAY:
921 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
922 address[count++] = coords[3];
923 }
924
925 /* Pack LOD */
926 if (opcode == TGSI_OPCODE_TXL)
927 address[count++] = coords[3];
928
929 if (count > 16) {
930 assert(!"Cannot handle more than 16 texture address parameters");
931 count = 16;
932 }
933
934 for (chan = 0; chan < count; chan++ ) {
935 address[chan] = LLVMBuildBitCast(gallivm->builder,
936 address[chan],
937 LLVMInt32TypeInContext(gallivm->context),
938 "");
939 }
940
941 /* Pad to power of two vector */
942 while (count < util_next_power_of_two(count))
943 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
944
Michel Dänzer120efee2013-01-25 12:10:11 +0100945 emit_data->args[1] = lp_build_gather_values(gallivm, address, count);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100946
Tom Stellarda75c6162012-01-06 17:38:37 -0500947 /* Resource */
Christian König22ae0622012-09-26 20:42:23 +0200948 ptr = use_sgpr(bld_base->base.gallivm, SGPR_CONST_PTR_V8I32, SI_SGPR_RESOURCE);
Tom Stellard467f5162012-05-16 15:15:35 -0400949 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200950 emit_data->inst->Src[1].Register.Index);
Tom Stellard467f5162012-05-16 15:15:35 -0400951 emit_data->args[2] = build_indexed_load(bld_base->base.gallivm,
952 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500953
954 /* Sampler */
Christian König22ae0622012-09-26 20:42:23 +0200955 ptr = use_sgpr(bld_base->base.gallivm, SGPR_CONST_PTR_V4I32, SI_SGPR_SAMPLER);
Tom Stellard467f5162012-05-16 15:15:35 -0400956 offset = lp_build_const_int32(bld_base->base.gallivm,
Christian König92b96a82012-08-01 15:20:07 +0200957 emit_data->inst->Src[1].Register.Index);
Tom Stellard467f5162012-05-16 15:15:35 -0400958 emit_data->args[3] = build_indexed_load(bld_base->base.gallivm,
959 ptr, offset);
Tom Stellarda75c6162012-01-06 17:38:37 -0500960
961 /* Dimensions */
Michel Dänzer120efee2013-01-25 12:10:11 +0100962 emit_data->args[4] = lp_build_const_int32(bld_base->base.gallivm, target);
Tom Stellarda75c6162012-01-06 17:38:37 -0500963
Michel Dänzer6eb0d3d2012-11-30 11:38:24 +0100964 emit_data->arg_count = 5;
Tom Stellarda75c6162012-01-06 17:38:37 -0500965 /* XXX: To optimize, we could use a float or v2f32, if the last bits of
966 * the writemask are clear */
967 emit_data->dst_type = LLVMVectorType(
968 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
969 4);
970}
971
Michel Dänzer07eddc42013-02-06 15:43:10 +0100972static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
973 struct lp_build_tgsi_context * bld_base,
974 struct lp_build_emit_data * emit_data)
975{
976 struct lp_build_context * base = &bld_base->base;
977 char intr_name[23];
978
979 sprintf(intr_name, "%sv%ui32", action->intr_name,
980 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[1])));
981
982 emit_data->output[emit_data->chan] = lp_build_intrinsic(
983 base->gallivm->builder, intr_name, emit_data->dst_type,
984 emit_data->args, emit_data->arg_count);
985}
986
Tom Stellarda75c6162012-01-06 17:38:37 -0500987static const struct lp_build_tgsi_action tex_action = {
988 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +0100989 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100990 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -0500991};
992
Michel Dänzer3e205132012-11-06 17:39:01 +0100993static const struct lp_build_tgsi_action txb_action = {
994 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +0100995 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100996 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +0100997};
998
Michel Dänzer56ae9be2012-11-06 17:41:50 +0100999static const struct lp_build_tgsi_action txl_action = {
1000 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001001 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001002 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001003};
1004
Tom Stellarda75c6162012-01-06 17:38:37 -05001005
1006int si_pipe_shader_create(
1007 struct pipe_context *ctx,
Michel Dänzer44ef0332012-10-05 16:59:10 +02001008 struct si_pipe_shader *shader,
1009 struct si_shader_key key)
Tom Stellarda75c6162012-01-06 17:38:37 -05001010{
1011 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001012 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05001013 struct si_shader_context si_shader_ctx;
1014 struct tgsi_shader_info shader_info;
1015 struct lp_build_tgsi_context * bld_base;
1016 LLVMModuleRef mod;
1017 unsigned char * inst_bytes;
1018 unsigned inst_byte_count;
1019 unsigned i;
Christian Königd51b9b72012-07-24 18:50:49 +02001020 uint32_t *ptr;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001021 bool dump;
1022
1023 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -05001024
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001025 assert(shader->shader.noutput == 0);
1026 assert(shader->shader.ninterp == 0);
1027 assert(shader->shader.ninput == 0);
1028
Michel Dänzercfebaf92012-08-31 19:04:08 +02001029 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001030 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1031 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1032
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001033 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzer35f0dc22013-01-22 17:08:24 +01001034 if (shader_info.indirect_files != 0) {
1035 fprintf(stderr, "Indirect addressing not fully handled yet\n");
1036 return -ENOSYS;
1037 }
1038
Michel Dänzere44dfd42012-11-07 17:33:08 +01001039 shader->shader.uses_kill = shader_info.uses_kill;
Tom Stellarda75c6162012-01-06 17:38:37 -05001040 bld_base->info = &shader_info;
1041 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellardcc571a32012-09-07 09:12:51 -04001042 bld_base->emit_prologue = si_llvm_emit_prologue;
Tom Stellarda75c6162012-01-06 17:38:37 -05001043 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1044
1045 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001046 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001047 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001048 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001049
1050 si_shader_ctx.radeon_bld.load_input = declare_input;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001051 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001052 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1053 si_shader_ctx.shader = shader;
Michel Dänzer44ef0332012-10-05 16:59:10 +02001054 si_shader_ctx.key = key;
Tom Stellarda75c6162012-01-06 17:38:37 -05001055 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
1056 si_shader_ctx.rctx = rctx;
1057
Christian König835098a2012-07-17 21:28:10 +02001058 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001059
Tom Stellard185fc9a2012-07-12 10:40:47 -04001060 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1061 * conversion fails. */
1062 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001063 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001064 }
1065
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001066 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001067 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
1068 return -EINVAL;
1069 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001070
1071 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1072
1073 mod = bld_base->base.gallivm->module;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001074 if (dump) {
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001075 LLVMDumpModule(mod);
1076 }
1077 radeon_llvm_compile(mod, &inst_bytes, &inst_byte_count, "SI", dump);
1078 if (dump) {
1079 fprintf(stderr, "SI CODE:\n");
1080 for (i = 0; i < inst_byte_count; i+=4 ) {
1081 fprintf(stderr, "%02x%02x%02x%02x\n", inst_bytes[i + 3],
1082 inst_bytes[i + 2], inst_bytes[i + 1],
1083 inst_bytes[i]);
1084 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001085 }
1086
1087 shader->num_sgprs = util_le32_to_cpu(*(uint32_t*)inst_bytes);
1088 shader->num_vgprs = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 4));
1089 shader->spi_ps_input_ena = util_le32_to_cpu(*(uint32_t*)(inst_bytes + 8));
1090
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001091 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001092 tgsi_parse_free(&si_shader_ctx.parse);
1093
1094 /* copy new shader */
Christian Königd51b9b72012-07-24 18:50:49 +02001095 si_resource_reference(&shader->bo, NULL);
1096 shader->bo = si_resource_create_custom(ctx->screen, PIPE_USAGE_IMMUTABLE,
1097 inst_byte_count - 12);
Tom Stellarda75c6162012-01-06 17:38:37 -05001098 if (shader->bo == NULL) {
Christian Königd51b9b72012-07-24 18:50:49 +02001099 return -ENOMEM;
Tom Stellarda75c6162012-01-06 17:38:37 -05001100 }
1101
Christian Königd51b9b72012-07-24 18:50:49 +02001102 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
1103 if (0 /*R600_BIG_ENDIAN*/) {
1104 for (i = 0; i < (inst_byte_count-12)/4; ++i) {
1105 ptr[i] = util_bswap32(*(uint32_t*)(inst_bytes+12 + i*4));
1106 }
1107 } else {
1108 memcpy(ptr, inst_bytes + 12, inst_byte_count - 12);
1109 }
1110 rctx->ws->buffer_unmap(shader->bo->cs_buf);
1111
Tom Stellarda75c6162012-01-06 17:38:37 -05001112 free(inst_bytes);
1113
1114 return 0;
1115}
1116
1117void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1118{
Christian Königfe412872012-07-24 18:47:19 +02001119 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001120}