blob: 53f4a9a416658bf2bdd788c524e7524be3dfac56 [file] [log] [blame]
Christian Königce40e472012-08-02 12:14:59 +02001/*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Tom Stellard <thomas.stellard@amd.com>
25 * Michel Dänzer <michel.daenzer@amd.com>
26 * Christian König <christian.koenig@amd.com>
27 */
28
Tom Stellarda75c6162012-01-06 17:38:37 -050029#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020030#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050031#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010032#include "gallivm/lp_bld_logic.h"
Christian König5e616cf2013-03-07 11:58:56 +010033#include "gallivm/lp_bld_arit.h"
Marek Olšák8d03d922013-09-01 23:59:06 +020034#include "gallivm/lp_bld_flow.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050035#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040036#include "radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010037#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050038#include "tgsi/tgsi_parse.h"
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +010039#include "tgsi/tgsi_util.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_dump.h"
41
Andreas Hartmetz786af2f2014-01-04 18:44:33 +010042#include "si_pipe.h"
43#include "si_shader.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050044#include "sid.h"
45
Tom Stellarda75c6162012-01-06 17:38:37 -050046#include <errno.h>
Tom Stellarda75c6162012-01-06 17:38:37 -050047
Tom Stellarda75c6162012-01-06 17:38:37 -050048struct si_shader_context
49{
50 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050051 struct tgsi_parse_context parse;
52 struct tgsi_token * tokens;
53 struct si_pipe_shader *shader;
54 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Marek Olšák8d03d922013-09-01 23:59:06 +020055 int param_streamout_config;
56 int param_streamout_write_index;
57 int param_streamout_offset[4];
58 int param_vertex_id;
59 int param_instance_id;
Christian König206f0592013-03-20 14:37:21 +010060 LLVMValueRef const_md;
Marek Olšák2fd42002013-10-25 11:45:47 +020061 LLVMValueRef const_resource[NUM_CONST_BUFFERS];
Michel Dänzera06ee5a2013-06-19 18:14:01 +020062#if HAVE_LLVM >= 0x0304
63 LLVMValueRef ddxy_lds;
64#endif
Marek Olšák2fd42002013-10-25 11:45:47 +020065 LLVMValueRef *constants[NUM_CONST_BUFFERS];
Christian König1c100182013-03-17 16:02:42 +010066 LLVMValueRef *resources;
67 LLVMValueRef *samplers;
Marek Olšák8d03d922013-09-01 23:59:06 +020068 LLVMValueRef so_buffers[4];
Tom Stellarda75c6162012-01-06 17:38:37 -050069};
70
71static struct si_shader_context * si_shader_context(
72 struct lp_build_tgsi_context * bld_base)
73{
74 return (struct si_shader_context *)bld_base;
75}
76
77
78#define PERSPECTIVE_BASE 0
79#define LINEAR_BASE 9
80
81#define SAMPLE_OFFSET 0
82#define CENTER_OFFSET 2
83#define CENTROID_OFSET 4
84
85#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040086#define CONST_ADDR_SPACE 2
Michel Dänzera06ee5a2013-06-19 18:14:01 +020087#define LOCAL_ADDR_SPACE 3
Tom Stellard89ece082012-05-29 11:36:29 -040088#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050089
Tom Stellard467f5162012-05-16 15:15:35 -040090/**
91 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
92 *
93 * @param offset The offset parameter specifies the number of
94 * elements to offset, not the number of bytes or dwords. An element is the
95 * the type pointed to by the base_ptr parameter (e.g. int is the element of
96 * an int* pointer)
97 *
98 * When LLVM lowers the load instruction, it will convert the element offset
99 * into a dword offset automatically.
100 *
101 */
102static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +0100103 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400104 LLVMValueRef base_ptr,
105 LLVMValueRef offset)
106{
Christian König206f0592013-03-20 14:37:21 +0100107 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400108
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200109 LLVMValueRef indices[2] = {
110 LLVMConstInt(LLVMInt64TypeInContext(base->gallivm->context), 0, false),
111 offset
112 };
Christian König206f0592013-03-20 14:37:21 +0100113 LLVMValueRef computed_ptr = LLVMBuildGEP(
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +0200114 base->gallivm->builder, base_ptr, indices, 2, "");
Christian König206f0592013-03-20 14:37:21 +0100115
116 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
117 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
118 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400119}
120
Marek Olšákf317ce52013-09-05 15:39:57 +0200121static LLVMValueRef get_instance_index_for_fetch(
Christian Königa0dca442013-03-22 15:59:22 +0100122 struct radeon_llvm_context * radeon_bld,
123 unsigned divisor)
124{
Marek Olšák8d03d922013-09-01 23:59:06 +0200125 struct si_shader_context *si_shader_ctx =
126 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100127 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
128
Marek Olšák8d03d922013-09-01 23:59:06 +0200129 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn,
130 si_shader_ctx->param_instance_id);
Christian Königa0dca442013-03-22 15:59:22 +0100131 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
132 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
133
134 if (divisor > 1)
135 result = LLVMBuildUDiv(gallivm->builder, result,
136 lp_build_const_int32(gallivm, divisor), "");
137
138 return result;
139}
140
Tom Stellarda75c6162012-01-06 17:38:37 -0500141static void declare_input_vs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900142 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500143 unsigned input_index,
144 const struct tgsi_full_declaration *decl)
145{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900146 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
147 struct gallivm_state *gallivm = base->gallivm;
148 struct si_shader_context *si_shader_ctx =
149 si_shader_context(&radeon_bld->soa.bld_base);
Christian Königa0dca442013-03-22 15:59:22 +0100150 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
151
152 unsigned chan;
153
Tom Stellarda75c6162012-01-06 17:38:37 -0500154 LLVMValueRef t_list_ptr;
155 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400156 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500157 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100158 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400159 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500160 LLVMTypeRef vec4_type;
161 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500162
Tom Stellard467f5162012-05-16 15:15:35 -0400163 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100164 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500165
Michel Dänzer51f89a02013-12-09 15:33:53 +0900166 t_offset = lp_build_const_int32(gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400167
Christian König206f0592013-03-20 14:37:21 +0100168 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400169
170 /* Build the attribute offset */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900171 attribute_offset = lp_build_const_int32(gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500172
Christian Königa0dca442013-03-22 15:59:22 +0100173 if (divisor) {
174 /* Build index from instance ID, start instance and divisor */
175 si_shader_ctx->shader->shader.uses_instanceid = true;
Marek Olšákf317ce52013-09-05 15:39:57 +0200176 buffer_index = get_instance_index_for_fetch(&si_shader_ctx->radeon_bld, divisor);
Christian Königa0dca442013-03-22 15:59:22 +0100177 } else {
178 /* Load the buffer index, which is always stored in VGPR0
179 * for Vertex Shaders */
Marek Olšák8d03d922013-09-01 23:59:06 +0200180 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
181 si_shader_ctx->param_vertex_id);
Christian Königa0dca442013-03-22 15:59:22 +0100182 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500183
184 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400185 args[0] = t_list;
186 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100187 args[2] = buffer_index;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900188 input = build_intrinsic(gallivm->builder,
Christian König44e32242013-03-20 12:10:35 +0100189 "llvm.SI.vs.load.input", vec4_type, args, 3,
190 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500191
192 /* Break up the vec4 into individual components */
193 for (chan = 0; chan < 4; chan++) {
Michel Dänzer51f89a02013-12-09 15:33:53 +0900194 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
Tom Stellarda75c6162012-01-06 17:38:37 -0500195 /* XXX: Use a helper function for this. There is one in
196 * tgsi_llvm.c. */
197 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
Michel Dänzer51f89a02013-12-09 15:33:53 +0900198 LLVMBuildExtractElement(gallivm->builder,
Tom Stellarda75c6162012-01-06 17:38:37 -0500199 input, llvm_chan, "");
200 }
201}
202
203static void declare_input_fs(
Michel Dänzer51f89a02013-12-09 15:33:53 +0900204 struct radeon_llvm_context *radeon_bld,
Tom Stellarda75c6162012-01-06 17:38:37 -0500205 unsigned input_index,
206 const struct tgsi_full_declaration *decl)
207{
Michel Dänzer51f89a02013-12-09 15:33:53 +0900208 struct lp_build_context *base = &radeon_bld->soa.bld_base.base;
209 struct si_shader_context *si_shader_ctx =
210 si_shader_context(&radeon_bld->soa.bld_base);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200211 struct si_shader *shader = &si_shader_ctx->shader->shader;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900212 struct lp_build_context *uint = &radeon_bld->soa.bld_base.uint_bld;
213 struct gallivm_state *gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400214 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900215 LLVMValueRef main_fn = radeon_bld->main_fn;
Christian König0666ffd2013-03-05 15:07:39 +0100216
217 LLVMValueRef interp_param;
218 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500219
220 /* This value is:
221 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
222 * quad begins a new primitive. Bit 0 always needs
223 * to be unset)
224 * [32:16] ParamOffset
225 *
226 */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900227 LLVMValueRef params = LLVMGetParam(main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200228 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500229
Christian König0666ffd2013-03-05 15:07:39 +0100230 unsigned chan;
231
Tom Stellard0fb1e682012-09-06 16:18:11 -0400232 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
233 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400234 unsigned soa_index =
235 radeon_llvm_reg_index_soa(input_index, chan);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900236 radeon_bld->inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100237 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100238
239 if (chan == 3)
240 /* RCP for fragcoord.w */
Michel Dänzer51f89a02013-12-09 15:33:53 +0900241 radeon_bld->inputs[soa_index] =
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100242 LLVMBuildFDiv(gallivm->builder,
243 lp_build_const_float(gallivm, 1.0f),
Michel Dänzer51f89a02013-12-09 15:33:53 +0900244 radeon_bld->inputs[soa_index],
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100245 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400246 }
247 return;
248 }
249
Michel Dänzer97078b12012-09-25 12:41:31 +0200250 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
251 LLVMValueRef face, is_face_positive;
252
Christian König0666ffd2013-03-05 15:07:39 +0100253 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
254
Michel Dänzer97078b12012-09-25 12:41:31 +0200255 is_face_positive = LLVMBuildFCmp(gallivm->builder,
256 LLVMRealUGT, face,
257 lp_build_const_float(gallivm, 0.0f),
258 "");
259
Michel Dänzer51f89a02013-12-09 15:33:53 +0900260 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200261 LLVMBuildSelect(gallivm->builder,
262 is_face_positive,
263 lp_build_const_float(gallivm, 1.0f),
264 lp_build_const_float(gallivm, 0.0f),
265 "");
Michel Dänzer51f89a02013-12-09 15:33:53 +0900266 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
267 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200268 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900269 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer97078b12012-09-25 12:41:31 +0200270 lp_build_const_float(gallivm, 1.0f);
271
272 return;
273 }
274
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200275 shader->input[input_index].param_offset = shader->ninterp++;
276 attr_number = lp_build_const_int32(gallivm,
277 shader->input[input_index].param_offset);
278
Francisco Jerez12799232012-04-30 18:27:52 +0200279 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500280 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100281 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100282 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200283 } else {
284 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100285 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200286 else
Christian König0666ffd2013-03-05 15:07:39 +0100287 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200288 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500289 break;
290 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100291 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500292 break;
293 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200294 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100295 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200296 else
Christian König0666ffd2013-03-05 15:07:39 +0100297 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200298 break;
299 case TGSI_INTERPOLATE_PERSPECTIVE:
300 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100301 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200302 else
Christian König0666ffd2013-03-05 15:07:39 +0100303 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500304 break;
305 default:
306 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
307 return;
308 }
309
Christian König0666ffd2013-03-05 15:07:39 +0100310 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
311
Tom Stellarda75c6162012-01-06 17:38:37 -0500312 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200313 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100314 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100315 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200316 LLVMValueRef face, is_face_positive;
317 LLVMValueRef back_attr_number =
318 lp_build_const_int32(gallivm,
319 shader->input[input_index].param_offset + 1);
320
Christian König0666ffd2013-03-05 15:07:39 +0100321 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
322
Michel Dänzer691f08d2012-09-06 18:03:38 +0200323 is_face_positive = LLVMBuildFCmp(gallivm->builder,
324 LLVMRealUGT, face,
325 lp_build_const_float(gallivm, 0.0f),
326 "");
327
Tom Stellarda75c6162012-01-06 17:38:37 -0500328 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100329 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200330 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
331 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
332 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
333 LLVMValueRef front, back;
334
335 args[0] = llvm_chan;
336 args[1] = attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900337 front = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100338 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100339 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200340
341 args[1] = back_attr_number;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900342 back = build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100343 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100344 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200345
Michel Dänzer51f89a02013-12-09 15:33:53 +0900346 radeon_bld->inputs[soa_index] =
Michel Dänzer691f08d2012-09-06 18:03:38 +0200347 LLVMBuildSelect(gallivm->builder,
348 is_face_positive,
349 front,
350 back,
351 "");
352 }
353
354 shader->ninterp++;
Michel Dänzer237cb072013-08-21 18:00:35 +0200355 } else if (decl->Semantic.Name == TGSI_SEMANTIC_FOG) {
356 LLVMValueRef args[4];
357
358 args[0] = uint->zero;
359 args[1] = attr_number;
360 args[2] = params;
361 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900362 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
363 build_intrinsic(gallivm->builder, intr_name,
364 input_type, args, args[3] ? 4 : 3,
365 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
366 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
367 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200368 lp_build_const_float(gallivm, 0.0f);
Michel Dänzer51f89a02013-12-09 15:33:53 +0900369 radeon_bld->inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
Michel Dänzer237cb072013-08-21 18:00:35 +0200370 lp_build_const_float(gallivm, 1.0f);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200371 } else {
372 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100373 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200374 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
375 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
376 args[0] = llvm_chan;
377 args[1] = attr_number;
378 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100379 args[3] = interp_param;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900380 radeon_bld->inputs[soa_index] =
381 build_intrinsic(gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100382 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100383 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200384 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500385 }
386}
387
Christian Könige4ed5872013-03-21 18:02:52 +0100388static void declare_system_value(
389 struct radeon_llvm_context * radeon_bld,
390 unsigned index,
391 const struct tgsi_full_declaration *decl)
392{
Marek Olšák8d03d922013-09-01 23:59:06 +0200393 struct si_shader_context *si_shader_ctx =
394 si_shader_context(&radeon_bld->soa.bld_base);
Christian Könige4ed5872013-03-21 18:02:52 +0100395 LLVMValueRef value = 0;
396
397 switch (decl->Semantic.Name) {
398 case TGSI_SEMANTIC_INSTANCEID:
Marek Olšákf317ce52013-09-05 15:39:57 +0200399 value = LLVMGetParam(radeon_bld->main_fn,
400 si_shader_ctx->param_instance_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100401 break;
402
403 case TGSI_SEMANTIC_VERTEXID:
Marek Olšák8d03d922013-09-01 23:59:06 +0200404 value = LLVMGetParam(radeon_bld->main_fn,
405 si_shader_ctx->param_vertex_id);
Christian Könige4ed5872013-03-21 18:02:52 +0100406 break;
407
408 default:
409 assert(!"unknown system value");
410 return;
411 }
412
413 radeon_bld->system_values[index] = value;
414}
415
Tom Stellarda75c6162012-01-06 17:38:37 -0500416static LLVMValueRef fetch_constant(
417 struct lp_build_tgsi_context * bld_base,
418 const struct tgsi_full_src_register *reg,
419 enum tgsi_opcode_type type,
420 unsigned swizzle)
421{
Christian König55fe5cc2013-03-04 16:30:06 +0100422 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500423 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100424 const struct tgsi_ind_register *ireg = &reg->Indirect;
Marek Olšák2fd42002013-10-25 11:45:47 +0200425 unsigned buf, idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500426
Christian Königf5298b02013-02-28 14:50:07 +0100427 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100428 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100429 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500430
Christian König8514f5a2013-02-04 17:46:42 +0100431 if (swizzle == LP_CHAN_ALL) {
432 unsigned chan;
433 LLVMValueRef values[4];
434 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
435 values[chan] = fetch_constant(bld_base, reg, type, chan);
436
437 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
438 }
439
Marek Olšák2fd42002013-10-25 11:45:47 +0200440 buf = reg->Register.Dimension ? reg->Dimension.Index : 0;
Christian König0f6cf2b2013-03-15 15:53:25 +0100441 idx = reg->Register.Index * 4 + swizzle;
Christian Königf5298b02013-02-28 14:50:07 +0100442
Marek Olšák2fd42002013-10-25 11:45:47 +0200443 if (!reg->Register.Indirect)
444 return bitcast(bld_base, type, si_shader_ctx->constants[buf][idx]);
445
446 args[0] = si_shader_ctx->const_resource[buf];
Christian König0f6cf2b2013-03-15 15:53:25 +0100447 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
448 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
449 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
450 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
451 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200452
Christian Königf5298b02013-02-28 14:50:07 +0100453 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100454 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500455
Christian Königf5298b02013-02-28 14:50:07 +0100456 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500457}
458
Michel Dänzer26c71392012-08-24 12:03:11 +0200459/* Initialize arguments for the shader export intrinsic */
460static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
461 struct tgsi_full_declaration *d,
462 unsigned index,
463 unsigned target,
464 LLVMValueRef *args)
465{
466 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
467 struct lp_build_context *uint =
468 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
469 struct lp_build_context *base = &bld_base->base;
470 unsigned compressed = 0;
471 unsigned chan;
472
Michel Dänzerf402acd2012-08-22 18:15:36 +0200473 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
474 int cbuf = target - V_008DFC_SQ_EXP_MRT;
475
476 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100477 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100478
479 if (compressed)
480 si_shader_ctx->shader->spi_shader_col_format |=
481 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
482 else
483 si_shader_ctx->shader->spi_shader_col_format |=
484 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200485
486 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200487 }
488 }
489
490 if (compressed) {
491 /* Pixel shader needs to pack output values before export */
492 for (chan = 0; chan < 2; chan++ ) {
493 LLVMValueRef *out_ptr =
494 si_shader_ctx->radeon_bld.soa.outputs[index];
495 args[0] = LLVMBuildLoad(base->gallivm->builder,
496 out_ptr[2 * chan], "");
497 args[1] = LLVMBuildLoad(base->gallivm->builder,
498 out_ptr[2 * chan + 1], "");
499 args[chan + 5] =
500 build_intrinsic(base->gallivm->builder,
501 "llvm.SI.packf16",
502 LLVMInt32TypeInContext(base->gallivm->context),
503 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100504 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100505 args[chan + 7] = args[chan + 5] =
506 LLVMBuildBitCast(base->gallivm->builder,
507 args[chan + 5],
508 LLVMFloatTypeInContext(base->gallivm->context),
509 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200510 }
511
512 /* Set COMPR flag */
513 args[4] = uint->one;
514 } else {
515 for (chan = 0; chan < 4; chan++ ) {
516 LLVMValueRef out_ptr =
517 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
518 /* +5 because the first output value will be
519 * the 6th argument to the intrinsic. */
520 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
521 out_ptr, "");
522 }
523
524 /* Clear COMPR flag */
525 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200526 }
527
528 /* XXX: This controls which components of the output
529 * registers actually get exported. (e.g bit 0 means export
530 * X component, bit 1 means export Y component, etc.) I'm
531 * hard coding this to 0xf for now. In the future, we might
532 * want to do something else. */
533 args[0] = lp_build_const_int32(base->gallivm, 0xf);
534
535 /* Specify whether the EXEC mask represents the valid mask */
536 args[1] = uint->zero;
537
538 /* Specify whether this is the last export */
539 args[2] = uint->zero;
540
541 /* Specify the target we are exporting */
542 args[3] = lp_build_const_int32(base->gallivm, target);
543
Michel Dänzer26c71392012-08-24 12:03:11 +0200544 /* XXX: We probably need to keep track of the output
545 * values, so we know what we are passing to the next
546 * stage. */
547}
548
Michel Dänzer7708a862012-11-02 15:57:30 +0100549static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
550 unsigned index)
551{
552 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
553 struct gallivm_state *gallivm = bld_base->base.gallivm;
554
Christian Königa0dca442013-03-22 15:59:22 +0100555 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Michel Dänzer7708a862012-11-02 15:57:30 +0100556 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400557 LLVMValueRef alpha_ref = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
558 SI_PARAM_ALPHA_REF);
559
Michel Dänzer7708a862012-11-02 15:57:30 +0100560 LLVMValueRef alpha_pass =
561 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100562 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer7708a862012-11-02 15:57:30 +0100563 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
Vadim Girlin453ea2d2013-10-13 19:53:54 +0400564 alpha_ref);
Michel Dänzer7708a862012-11-02 15:57:30 +0100565 LLVMValueRef arg =
566 lp_build_select(&bld_base->base,
567 alpha_pass,
568 lp_build_const_float(gallivm, 1.0f),
569 lp_build_const_float(gallivm, -1.0f));
570
571 build_intrinsic(gallivm->builder,
572 "llvm.AMDGPU.kill",
573 LLVMVoidTypeInContext(gallivm->context),
574 &arg, 1, 0);
575 } else {
576 build_intrinsic(gallivm->builder,
577 "llvm.AMDGPU.kilp",
578 LLVMVoidTypeInContext(gallivm->context),
579 NULL, 0, 0);
580 }
581}
582
Marek Olšák6d4755a2013-07-30 22:29:29 +0200583static void si_alpha_to_one(struct lp_build_tgsi_context *bld_base,
584 unsigned index)
585{
586 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
587
588 /* set alpha to one */
589 LLVMBuildStore(bld_base->base.gallivm->builder,
590 bld_base->base.one,
591 si_shader_ctx->radeon_bld.soa.outputs[index][3]);
592}
593
Michel Dänzere3befbc2013-05-15 18:09:50 +0200594static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
Michel Dänzerb00269a2013-08-07 18:14:16 +0200595 LLVMValueRef (*pos)[9], unsigned index)
Michel Dänzere3befbc2013-05-15 18:09:50 +0200596{
597 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200598 struct si_pipe_shader *shader = si_shader_ctx->shader;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200599 struct lp_build_context *base = &bld_base->base;
600 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200601 unsigned reg_index;
602 unsigned chan;
603 unsigned const_chan;
604 LLVMValueRef out_elts[4];
605 LLVMValueRef base_elt;
606 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Marek Olšák2fd42002013-10-25 11:45:47 +0200607 LLVMValueRef constbuf_index = lp_build_const_int32(base->gallivm, NUM_PIPE_CONST_BUFFERS);
608 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, constbuf_index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200609
610 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
611 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
612 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
613 }
614
615 for (reg_index = 0; reg_index < 2; reg_index ++) {
Michel Dänzerb00269a2013-08-07 18:14:16 +0200616 LLVMValueRef *args = pos[2 + reg_index];
617
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200618 if (!(shader->key.vs.ucps_enabled & (1 << reg_index)))
619 continue;
620
621 shader->shader.clip_dist_write |= 0xf << (4 * reg_index);
622
Michel Dänzere3befbc2013-05-15 18:09:50 +0200623 args[5] =
624 args[6] =
625 args[7] =
626 args[8] = lp_build_const_float(base->gallivm, 0.0f);
627
628 /* Compute dot products of position and user clip plane vectors */
629 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
630 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
631 args[0] = const_resource;
632 args[1] = lp_build_const_int32(base->gallivm,
633 ((reg_index * 4 + chan) * 4 +
634 const_chan) * 4);
635 base_elt = build_intrinsic(base->gallivm->builder,
636 "llvm.SI.load.const",
637 base->elem_type,
638 args, 2,
639 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
640 args[5 + chan] =
641 lp_build_add(base, args[5 + chan],
642 lp_build_mul(base, base_elt,
643 out_elts[const_chan]));
644 }
645 }
646
647 args[0] = lp_build_const_int32(base->gallivm, 0xf);
648 args[1] = uint->zero;
649 args[2] = uint->zero;
650 args[3] = lp_build_const_int32(base->gallivm,
651 V_008DFC_SQ_EXP_POS + 2 + reg_index);
652 args[4] = uint->zero;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200653 }
654}
655
Marek Olšák8d03d922013-09-01 23:59:06 +0200656static void si_dump_streamout(struct pipe_stream_output_info *so)
657{
658 unsigned i;
659
660 if (so->num_outputs)
661 fprintf(stderr, "STREAMOUT\n");
662
663 for (i = 0; i < so->num_outputs; i++) {
664 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
665 so->output[i].start_component;
666 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
667 i, so->output[i].output_buffer,
668 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
669 so->output[i].register_index,
670 mask & 1 ? "x" : "",
671 mask & 2 ? "y" : "",
672 mask & 4 ? "z" : "",
673 mask & 8 ? "w" : "");
674 }
675}
676
677/* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
678 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
679 * or v4i32 (num_channels=3,4). */
680static void build_tbuffer_store(struct si_shader_context *shader,
681 LLVMValueRef rsrc,
682 LLVMValueRef vdata,
683 unsigned num_channels,
684 LLVMValueRef vaddr,
685 LLVMValueRef soffset,
686 unsigned inst_offset,
687 unsigned dfmt,
688 unsigned nfmt,
689 unsigned offen,
690 unsigned idxen,
691 unsigned glc,
692 unsigned slc,
693 unsigned tfe)
694{
695 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
696 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
697 LLVMValueRef args[] = {
698 rsrc,
699 vdata,
700 LLVMConstInt(i32, num_channels, 0),
701 vaddr,
702 soffset,
703 LLVMConstInt(i32, inst_offset, 0),
704 LLVMConstInt(i32, dfmt, 0),
705 LLVMConstInt(i32, nfmt, 0),
706 LLVMConstInt(i32, offen, 0),
707 LLVMConstInt(i32, idxen, 0),
708 LLVMConstInt(i32, glc, 0),
709 LLVMConstInt(i32, slc, 0),
710 LLVMConstInt(i32, tfe, 0)
711 };
712
713 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
714 unsigned func = CLAMP(num_channels, 1, 3) - 1;
715 const char *types[] = {"i32", "v2i32", "v4i32"};
716 char name[256];
717 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
718
719 lp_build_intrinsic(gallivm->builder, name,
720 LLVMVoidTypeInContext(gallivm->context),
721 args, Elements(args));
722}
723
724static void build_streamout_store(struct si_shader_context *shader,
725 LLVMValueRef rsrc,
726 LLVMValueRef vdata,
727 unsigned num_channels,
728 LLVMValueRef vaddr,
729 LLVMValueRef soffset,
730 unsigned inst_offset)
731{
732 static unsigned dfmt[] = {
733 V_008F0C_BUF_DATA_FORMAT_32,
734 V_008F0C_BUF_DATA_FORMAT_32_32,
735 V_008F0C_BUF_DATA_FORMAT_32_32_32,
736 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
737 };
738 assert(num_channels >= 1 && num_channels <= 4);
739
740 build_tbuffer_store(shader, rsrc, vdata, num_channels, vaddr, soffset,
741 inst_offset, dfmt[num_channels-1],
742 V_008F0C_BUF_NUM_FORMAT_UINT, 1, 0, 1, 1, 0);
743}
744
745/* On SI, the vertex shader is responsible for writing streamout data
746 * to buffers. */
747static void si_llvm_emit_streamout(struct si_shader_context *shader)
748{
749 struct pipe_stream_output_info *so = &shader->shader->selector->so;
750 struct gallivm_state *gallivm = &shader->radeon_bld.gallivm;
751 LLVMBuilderRef builder = gallivm->builder;
752 int i, j;
753 struct lp_build_if_state if_ctx;
754
755 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
756
757 LLVMValueRef so_param =
758 LLVMGetParam(shader->radeon_bld.main_fn,
759 shader->param_streamout_config);
760
761 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
762 LLVMValueRef so_vtx_count =
763 LLVMBuildAnd(builder,
764 LLVMBuildLShr(builder, so_param,
765 LLVMConstInt(i32, 16, 0), ""),
766 LLVMConstInt(i32, 127, 0), "");
767
768 LLVMValueRef tid = build_intrinsic(builder, "llvm.SI.tid", i32,
769 NULL, 0, LLVMReadNoneAttribute);
770
771 /* can_emit = tid < so_vtx_count; */
772 LLVMValueRef can_emit =
773 LLVMBuildICmp(builder, LLVMIntULT, tid, so_vtx_count, "");
774
775 /* Emit the streamout code conditionally. This actually avoids
776 * out-of-bounds buffer access. The hw tells us via the SGPR
777 * (so_vtx_count) which threads are allowed to emit streamout data. */
778 lp_build_if(&if_ctx, gallivm, can_emit);
779 {
780 /* The buffer offset is computed as follows:
781 * ByteOffset = streamout_offset[buffer_id]*4 +
782 * (streamout_write_index + thread_id)*stride[buffer_id] +
783 * attrib_offset
784 */
785
786 LLVMValueRef so_write_index =
787 LLVMGetParam(shader->radeon_bld.main_fn,
788 shader->param_streamout_write_index);
789
790 /* Compute (streamout_write_index + thread_id). */
791 so_write_index = LLVMBuildAdd(builder, so_write_index, tid, "");
792
793 /* Compute the write offset for each enabled buffer. */
794 LLVMValueRef so_write_offset[4] = {};
795 for (i = 0; i < 4; i++) {
796 if (!so->stride[i])
797 continue;
798
799 LLVMValueRef so_offset = LLVMGetParam(shader->radeon_bld.main_fn,
800 shader->param_streamout_offset[i]);
801 so_offset = LLVMBuildMul(builder, so_offset, LLVMConstInt(i32, 4, 0), "");
802
803 so_write_offset[i] = LLVMBuildMul(builder, so_write_index,
804 LLVMConstInt(i32, so->stride[i]*4, 0), "");
805 so_write_offset[i] = LLVMBuildAdd(builder, so_write_offset[i], so_offset, "");
806 }
807
808 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS] = shader->radeon_bld.soa.outputs;
809
810 /* Write streamout data. */
811 for (i = 0; i < so->num_outputs; i++) {
812 unsigned buf_idx = so->output[i].output_buffer;
813 unsigned reg = so->output[i].register_index;
814 unsigned start = so->output[i].start_component;
815 unsigned num_comps = so->output[i].num_components;
816 LLVMValueRef out[4];
817
818 assert(num_comps && num_comps <= 4);
819 if (!num_comps || num_comps > 4)
820 continue;
821
822 /* Load the output as int. */
823 for (j = 0; j < num_comps; j++) {
824 out[j] = LLVMBuildLoad(builder, outputs[reg][start+j], "");
825 out[j] = LLVMBuildBitCast(builder, out[j], i32, "");
826 }
827
828 /* Pack the output. */
829 LLVMValueRef vdata = NULL;
830
831 switch (num_comps) {
832 case 1: /* as i32 */
833 vdata = out[0];
834 break;
835 case 2: /* as v2i32 */
836 case 3: /* as v4i32 (aligned to 4) */
837 case 4: /* as v4i32 */
838 vdata = LLVMGetUndef(LLVMVectorType(i32, util_next_power_of_two(num_comps)));
839 for (j = 0; j < num_comps; j++) {
840 vdata = LLVMBuildInsertElement(builder, vdata, out[j],
841 LLVMConstInt(i32, j, 0), "");
842 }
843 break;
844 }
845
846 build_streamout_store(shader, shader->so_buffers[buf_idx],
847 vdata, num_comps,
848 so_write_offset[buf_idx],
849 LLVMConstInt(i32, 0, 0),
850 so->output[i].dst_offset*4);
851 }
852 }
853 lp_build_endif(&if_ctx);
854}
855
Michel Dänzer7435d9f2013-12-04 13:37:07 +0900856
Michel Dänzer51f89a02013-12-09 15:33:53 +0900857static int si_store_shader_io_attribs(struct si_shader *shader,
858 struct tgsi_full_declaration *d)
859{
860 int i = -1;
861
862 switch (d->Declaration.File) {
863 case TGSI_FILE_INPUT:
864 i = shader->ninput++;
865 assert(i < Elements(shader->input));
866 shader->input[i].name = d->Semantic.Name;
867 shader->input[i].sid = d->Semantic.Index;
868 shader->input[i].interpolate = d->Interp.Interpolate;
869 shader->input[i].centroid = d->Interp.Centroid;
870 return -1;
871
872 case TGSI_FILE_OUTPUT:
873 i = shader->noutput++;
874 assert(i < Elements(shader->output));
875 shader->output[i].name = d->Semantic.Name;
876 shader->output[i].sid = d->Semantic.Index;
877 shader->output[i].interpolate = d->Interp.Interpolate;
878 break;
879 }
880
881 return i;
882}
883
884static void si_llvm_emit_vs_epilogue(struct lp_build_tgsi_context * bld_base)
Tom Stellarda75c6162012-01-06 17:38:37 -0500885{
886 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200887 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500888 struct lp_build_context * base = &bld_base->base;
889 struct lp_build_context * uint =
890 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
891 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100892 LLVMValueRef args[9];
Michel Dänzerb00269a2013-08-07 18:14:16 +0200893 LLVMValueRef pos_args[4][9] = { { 0 } };
Michel Dänzer0afeea52013-05-02 14:53:17 +0200894 unsigned semantic_name;
Christian König35088152012-08-01 22:35:24 +0200895 unsigned param_count = 0;
Michel Dänzer51f89a02013-12-09 15:33:53 +0900896 unsigned pos_idx;
897 int psize_index = -1, edgeflag_index = -1, layer_index = -1;
Michel Dänzerb00269a2013-08-07 18:14:16 +0200898 int i;
Tom Stellarda75c6162012-01-06 17:38:37 -0500899
Marek Olšák8d03d922013-09-01 23:59:06 +0200900 if (si_shader_ctx->shader->selector->so.num_outputs) {
901 si_llvm_emit_streamout(si_shader_ctx);
902 }
903
Tom Stellarda75c6162012-01-06 17:38:37 -0500904 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500905 struct tgsi_full_declaration *d =
906 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500907 unsigned target;
908 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500909
910 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100911
Tom Stellarda75c6162012-01-06 17:38:37 -0500912 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
913 continue;
914
Michel Dänzer51f89a02013-12-09 15:33:53 +0900915 i = si_store_shader_io_attribs(shader, d);
916 if (i < 0)
Christian König35088152012-08-01 22:35:24 +0200917 continue;
918
Michel Dänzer0afeea52013-05-02 14:53:17 +0200919 semantic_name = d->Semantic.Name;
920handle_semantic:
Tom Stellarda75c6162012-01-06 17:38:37 -0500921 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500922 /* Select the correct target */
Michel Dänzer0afeea52013-05-02 14:53:17 +0200923 switch(semantic_name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400924 case TGSI_SEMANTIC_PSIZE:
Marek Olšák053606d2013-11-19 22:07:30 +0100925 shader->vs_out_misc_write = true;
926 shader->vs_out_point_size = true;
927 psize_index = index;
928 continue;
929 case TGSI_SEMANTIC_EDGEFLAG:
930 shader->vs_out_misc_write = true;
931 shader->vs_out_edgeflag = true;
932 edgeflag_index = index;
933 continue;
Marek Olšákd52791a2013-11-21 15:21:38 +0100934 case TGSI_SEMANTIC_LAYER:
935 shader->vs_out_misc_write = true;
936 shader->vs_out_layer = true;
937 layer_index = index;
938 continue;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100939 case TGSI_SEMANTIC_POSITION:
Michel Dänzer51f89a02013-12-09 15:33:53 +0900940 target = V_008DFC_SQ_EXP_POS;
941 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500942 case TGSI_SEMANTIC_COLOR:
Michel Dänzer691f08d2012-09-06 18:03:38 +0200943 case TGSI_SEMANTIC_BCOLOR:
Michel Dänzer51f89a02013-12-09 15:33:53 +0900944 target = V_008DFC_SQ_EXP_PARAM + param_count;
945 shader->output[i].param_offset = param_count;
946 param_count++;
Tom Stellarda75c6162012-01-06 17:38:37 -0500947 break;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200948 case TGSI_SEMANTIC_CLIPDIST:
Michel Dänzer2f98dc22013-08-08 16:58:00 +0200949 if (!(si_shader_ctx->shader->key.vs.ucps_enabled &
950 (1 << d->Semantic.Index)))
951 continue;
Michel Dänzer0afeea52013-05-02 14:53:17 +0200952 shader->clip_dist_write |=
953 d->Declaration.UsageMask << (d->Semantic.Index << 2);
954 target = V_008DFC_SQ_EXP_POS + 2 + d->Semantic.Index;
955 break;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200956 case TGSI_SEMANTIC_CLIPVERTEX:
Michel Dänzerb00269a2013-08-07 18:14:16 +0200957 si_llvm_emit_clipvertex(bld_base, pos_args, index);
Michel Dänzere3befbc2013-05-15 18:09:50 +0200958 continue;
Michel Dänzer30b30372012-09-06 17:53:04 +0200959 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500960 case TGSI_SEMANTIC_GENERIC:
961 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200962 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500963 param_count++;
964 break;
965 default:
966 target = 0;
967 fprintf(stderr,
Michel Dänzer51f89a02013-12-09 15:33:53 +0900968 "Warning: SI unhandled vs output type:%d\n",
Michel Dänzer0afeea52013-05-02 14:53:17 +0200969 semantic_name);
Tom Stellarda75c6162012-01-06 17:38:37 -0500970 }
971
Michel Dänzer26c71392012-08-24 12:03:11 +0200972 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500973
Michel Dänzer51f89a02013-12-09 15:33:53 +0900974 if (target >= V_008DFC_SQ_EXP_POS &&
Michel Dänzerb00269a2013-08-07 18:14:16 +0200975 target <= (V_008DFC_SQ_EXP_POS + 3)) {
976 memcpy(pos_args[target - V_008DFC_SQ_EXP_POS],
977 args, sizeof(args));
Michel Dänzer51f89a02013-12-09 15:33:53 +0900978 } else {
979 lp_build_intrinsic(base->gallivm->builder,
980 "llvm.SI.export",
981 LLVMVoidTypeInContext(base->gallivm->context),
982 args, 9);
983 }
984 }
985
986 if (semantic_name == TGSI_SEMANTIC_CLIPDIST) {
987 semantic_name = TGSI_SEMANTIC_GENERIC;
988 goto handle_semantic;
989 }
990 }
991
992 /* We need to add the position output manually if it's missing. */
993 if (!pos_args[0][0]) {
994 pos_args[0][0] = lp_build_const_int32(base->gallivm, 0xf); /* writemask */
995 pos_args[0][1] = uint->zero; /* EXEC mask */
996 pos_args[0][2] = uint->zero; /* last export? */
997 pos_args[0][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS);
998 pos_args[0][4] = uint->zero; /* COMPR flag */
999 pos_args[0][5] = base->zero; /* X */
1000 pos_args[0][6] = base->zero; /* Y */
1001 pos_args[0][7] = base->zero; /* Z */
1002 pos_args[0][8] = base->one; /* W */
1003 }
1004
1005 /* Write the misc vector (point size, edgeflag, layer, viewport). */
1006 if (shader->vs_out_misc_write) {
1007 pos_args[1][0] = lp_build_const_int32(base->gallivm, /* writemask */
1008 shader->vs_out_point_size |
1009 (shader->vs_out_edgeflag << 1) |
1010 (shader->vs_out_layer << 2));
1011 pos_args[1][1] = uint->zero; /* EXEC mask */
1012 pos_args[1][2] = uint->zero; /* last export? */
1013 pos_args[1][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + 1);
1014 pos_args[1][4] = uint->zero; /* COMPR flag */
1015 pos_args[1][5] = base->zero; /* X */
1016 pos_args[1][6] = base->zero; /* Y */
1017 pos_args[1][7] = base->zero; /* Z */
1018 pos_args[1][8] = base->zero; /* W */
1019
1020 if (shader->vs_out_point_size) {
1021 pos_args[1][5] = LLVMBuildLoad(base->gallivm->builder,
1022 si_shader_ctx->radeon_bld.soa.outputs[psize_index][0], "");
1023 }
1024
1025 if (shader->vs_out_edgeflag) {
1026 LLVMValueRef output = LLVMBuildLoad(base->gallivm->builder,
1027 si_shader_ctx->radeon_bld.soa.outputs[edgeflag_index][0], "");
1028
1029 /* The output is a float, but the hw expects an integer
1030 * with the first bit containing the edge flag. */
1031 output = LLVMBuildFPToUI(base->gallivm->builder, output,
1032 bld_base->uint_bld.elem_type, "");
1033
1034 output = lp_build_min(&bld_base->int_bld, output, bld_base->int_bld.one);
1035
1036 /* The LLVM intrinsic expects a float. */
1037 pos_args[1][6] = LLVMBuildBitCast(base->gallivm->builder, output,
1038 base->elem_type, "");
1039 }
1040
1041 if (shader->vs_out_layer) {
1042 pos_args[1][7] = LLVMBuildLoad(base->gallivm->builder,
1043 si_shader_ctx->radeon_bld.soa.outputs[layer_index][0], "");
1044 }
1045 }
1046
1047 for (i = 0; i < 4; i++)
1048 if (pos_args[i][0])
1049 shader->nr_pos_exports++;
1050
1051 pos_idx = 0;
1052 for (i = 0; i < 4; i++) {
1053 if (!pos_args[i][0])
1054 continue;
1055
1056 /* Specify the target we are exporting */
1057 pos_args[i][3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_POS + pos_idx++);
1058
1059 if (pos_idx == shader->nr_pos_exports)
1060 /* Specify that this is the last export */
1061 pos_args[i][2] = uint->one;
1062
1063 lp_build_intrinsic(base->gallivm->builder,
1064 "llvm.SI.export",
1065 LLVMVoidTypeInContext(base->gallivm->context),
1066 pos_args[i], 9);
1067 }
1068}
1069
1070static void si_llvm_emit_fs_epilogue(struct lp_build_tgsi_context * bld_base)
1071{
1072 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
1073 struct si_shader * shader = &si_shader_ctx->shader->shader;
1074 struct lp_build_context * base = &bld_base->base;
1075 struct lp_build_context * uint =
1076 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
1077 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
1078 LLVMValueRef args[9];
1079 LLVMValueRef last_args[9] = { 0 };
1080 unsigned semantic_name;
1081 int depth_index = -1, stencil_index = -1;
1082 int i;
1083
1084 while (!tgsi_parse_end_of_tokens(parse)) {
1085 struct tgsi_full_declaration *d =
1086 &parse->FullToken.FullDeclaration;
1087 unsigned target;
1088 unsigned index;
1089
1090 tgsi_parse_token(parse);
1091
1092 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
1093 parse->FullToken.FullProperty.Property.PropertyName ==
1094 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
1095 shader->fs_write_all = TRUE;
1096
1097 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
1098 continue;
1099
1100 i = si_store_shader_io_attribs(shader, d);
1101 if (i < 0)
1102 continue;
1103
1104 semantic_name = d->Semantic.Name;
1105 for (index = d->Range.First; index <= d->Range.Last; index++) {
1106 /* Select the correct target */
1107 switch(semantic_name) {
1108 case TGSI_SEMANTIC_POSITION:
1109 depth_index = index;
1110 continue;
1111 case TGSI_SEMANTIC_STENCIL:
1112 stencil_index = index;
1113 continue;
1114 case TGSI_SEMANTIC_COLOR:
1115 target = V_008DFC_SQ_EXP_MRT + d->Semantic.Index;
1116 if (si_shader_ctx->shader->key.ps.alpha_to_one)
1117 si_alpha_to_one(bld_base, index);
1118 if (d->Semantic.Index == 0 &&
1119 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
1120 si_alpha_test(bld_base, index);
1121 break;
1122 default:
1123 target = 0;
1124 fprintf(stderr,
1125 "Warning: SI unhandled fs output type:%d\n",
1126 semantic_name);
1127 }
1128
1129 si_llvm_init_export_args(bld_base, d, index, target, args);
1130
1131 if (semantic_name == TGSI_SEMANTIC_COLOR) {
Marek Olšák0eb528a2013-12-04 13:24:22 +01001132 /* If there is an export instruction waiting to be emitted, do so now. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001133 if (last_args[0]) {
1134 lp_build_intrinsic(base->gallivm->builder,
1135 "llvm.SI.export",
1136 LLVMVoidTypeInContext(base->gallivm->context),
1137 last_args, 9);
1138 }
1139
Marek Olšák0eb528a2013-12-04 13:24:22 +01001140 /* This instruction will be emitted at the end of the shader. */
Tom Stellarda75c6162012-01-06 17:38:37 -05001141 memcpy(last_args, args, sizeof(args));
Marek Olšák0eb528a2013-12-04 13:24:22 +01001142
1143 /* Handle FS_COLOR0_WRITES_ALL_CBUFS. */
1144 if (shader->fs_write_all && shader->output[i].sid == 0 &&
1145 si_shader_ctx->shader->key.ps.nr_cbufs > 1) {
1146 for (int c = 1; c < si_shader_ctx->shader->key.ps.nr_cbufs; c++) {
1147 si_llvm_init_export_args(bld_base, d, index,
1148 V_008DFC_SQ_EXP_MRT + c, args);
1149 lp_build_intrinsic(base->gallivm->builder,
1150 "llvm.SI.export",
1151 LLVMVoidTypeInContext(base->gallivm->context),
1152 args, 9);
1153 }
1154 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001155 } else {
1156 lp_build_intrinsic(base->gallivm->builder,
1157 "llvm.SI.export",
1158 LLVMVoidTypeInContext(base->gallivm->context),
1159 args, 9);
1160 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001161 }
1162 }
1163
Michel Dänzer1a616c12012-11-13 17:35:09 +01001164 if (depth_index >= 0 || stencil_index >= 0) {
1165 LLVMValueRef out_ptr;
1166 unsigned mask = 0;
1167
1168 /* Specify the target we are exporting */
1169 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
1170
1171 if (depth_index >= 0) {
1172 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
1173 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
1174 mask |= 0x1;
1175
1176 if (stencil_index < 0) {
1177 args[6] =
1178 args[7] =
1179 args[8] = args[5];
1180 }
1181 }
1182
1183 if (stencil_index >= 0) {
1184 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
1185 args[7] =
1186 args[8] =
1187 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
Michel Dänzer46fd81e2013-08-23 14:55:45 +02001188 /* Only setting the stencil component bit (0x2) here
1189 * breaks some stencil piglit tests
1190 */
1191 mask |= 0x3;
Michel Dänzer1a616c12012-11-13 17:35:09 +01001192
1193 if (depth_index < 0)
1194 args[5] = args[6];
1195 }
1196
1197 /* Specify which components to enable */
1198 args[0] = lp_build_const_int32(base->gallivm, mask);
1199
1200 args[1] =
1201 args[2] =
1202 args[4] = uint->zero;
1203
1204 if (last_args[0])
1205 lp_build_intrinsic(base->gallivm->builder,
1206 "llvm.SI.export",
1207 LLVMVoidTypeInContext(base->gallivm->context),
1208 args, 9);
1209 else
1210 memcpy(last_args, args, sizeof(args));
1211 }
1212
Michel Dänzer51f89a02013-12-09 15:33:53 +09001213 if (!last_args[0]) {
1214 /* Specify which components to enable */
1215 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
Christian Königf18fd252012-07-25 21:58:46 +02001216
Michel Dänzer51f89a02013-12-09 15:33:53 +09001217 /* Specify the target we are exporting */
1218 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
Marek Olšák48784f32013-10-23 16:10:38 +02001219
Michel Dänzer51f89a02013-12-09 15:33:53 +09001220 /* Set COMPR flag to zero to export data as 32-bit */
1221 last_args[4] = uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001222
Michel Dänzer51f89a02013-12-09 15:33:53 +09001223 /* dummy bits */
1224 last_args[5]= uint->zero;
1225 last_args[6]= uint->zero;
1226 last_args[7]= uint->zero;
1227 last_args[8]= uint->zero;
Marek Olšák053606d2013-11-19 22:07:30 +01001228
Michel Dänzer51f89a02013-12-09 15:33:53 +09001229 si_shader_ctx->shader->spi_shader_col_format |=
1230 V_028714_SPI_SHADER_32_ABGR;
1231 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Michel Dänzerc8402702013-02-12 18:37:22 +01001232 }
Michel Dänzer51f89a02013-12-09 15:33:53 +09001233
1234 /* Specify whether the EXEC mask represents the valid mask */
1235 last_args[1] = uint->one;
1236
1237 /* Specify that this is the last export */
1238 last_args[2] = lp_build_const_int32(base->gallivm, 1);
1239
1240 lp_build_intrinsic(base->gallivm->builder,
1241 "llvm.SI.export",
1242 LLVMVoidTypeInContext(base->gallivm->context),
1243 last_args, 9);
Tom Stellarda75c6162012-01-06 17:38:37 -05001244}
1245
Marek Olšák4855acd2013-08-06 15:08:54 +02001246static const struct lp_build_tgsi_action txf_action;
1247
1248static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1249 struct lp_build_tgsi_context * bld_base,
1250 struct lp_build_emit_data * emit_data);
1251
Tom Stellarda75c6162012-01-06 17:38:37 -05001252static void tex_fetch_args(
1253 struct lp_build_tgsi_context * bld_base,
1254 struct lp_build_emit_data * emit_data)
1255{
Christian König55fe5cc2013-03-04 16:30:06 +01001256 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001257 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001258 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +01001259 unsigned opcode = inst->Instruction.Opcode;
1260 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +01001261 LLVMValueRef coords[4];
1262 LLVMValueRef address[16];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001263 int ref_pos;
1264 unsigned num_coords = tgsi_util_get_texture_coord_dim(target, &ref_pos);
Michel Dänzer120efee2013-01-25 12:10:11 +01001265 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +01001266 unsigned chan;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001267 unsigned sampler_src = emit_data->inst->Instruction.NumSrcRegs - 1;
1268 unsigned sampler_index = emit_data->inst->Src[sampler_src].Register.Index;
1269
1270 if (target == TGSI_TEXTURE_BUFFER) {
1271 LLVMTypeRef i128 = LLVMIntTypeInContext(gallivm->context, 128);
1272 LLVMTypeRef v2i128 = LLVMVectorType(i128, 2);
1273 LLVMTypeRef i8 = LLVMInt8TypeInContext(gallivm->context);
1274 LLVMTypeRef v16i8 = LLVMVectorType(i8, 16);
1275
1276 /* Truncate v32i8 to v16i8. */
1277 LLVMValueRef res = si_shader_ctx->resources[sampler_index];
1278 res = LLVMBuildBitCast(gallivm->builder, res, v2i128, "");
1279 res = LLVMBuildExtractElement(gallivm->builder, res, bld_base->uint_bld.zero, "");
1280 res = LLVMBuildBitCast(gallivm->builder, res, v16i8, "");
1281
1282 emit_data->dst_type = LLVMVectorType(bld_base->base.elem_type, 4);
1283 emit_data->args[0] = res;
1284 emit_data->args[1] = bld_base->uint_bld.zero;
1285 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
1286 emit_data->arg_count = 3;
1287 return;
1288 }
Tom Stellard467f5162012-05-16 15:15:35 -04001289
Michel Dänzer120efee2013-01-25 12:10:11 +01001290 /* Fetch and project texture coordinates */
1291 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +01001292 for (chan = 0; chan < 3; chan++ ) {
1293 coords[chan] = lp_build_emit_fetch(bld_base,
1294 emit_data->inst, 0,
1295 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +01001296 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001297 coords[chan] = lp_build_emit_llvm_binary(bld_base,
1298 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001299 coords[chan],
1300 coords[3]);
1301 }
1302
Michel Dänzer120efee2013-01-25 12:10:11 +01001303 if (opcode == TGSI_OPCODE_TXP)
1304 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -05001305
Michel Dänzer120efee2013-01-25 12:10:11 +01001306 /* Pack LOD bias value */
1307 if (opcode == TGSI_OPCODE_TXB)
1308 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +04001309
Michel Dänzer0495adb2013-05-06 12:45:14 +02001310 if (target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE)
Michel Dänzere5fb7342013-01-24 18:54:51 +01001311 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +01001312
1313 /* Pack depth comparison value */
1314 switch (target) {
1315 case TGSI_TEXTURE_SHADOW1D:
1316 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1317 case TGSI_TEXTURE_SHADOW2D:
1318 case TGSI_TEXTURE_SHADOWRECT:
Michel Dänzer120efee2013-01-25 12:10:11 +01001319 case TGSI_TEXTURE_SHADOWCUBE:
1320 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001321 assert(ref_pos >= 0);
1322 address[count++] = coords[ref_pos];
Michel Dänzer120efee2013-01-25 12:10:11 +01001323 break;
1324 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
1325 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +01001326 }
1327
Michel Dänzera6b83c02013-02-21 16:10:55 +01001328 /* Pack user derivatives */
1329 if (opcode == TGSI_OPCODE_TXD) {
1330 for (chan = 0; chan < 2; chan++) {
1331 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, chan);
1332 if (num_coords > 1)
1333 address[count++] = lp_build_emit_fetch(bld_base, inst, 2, chan);
1334 }
1335 }
1336
Michel Dänzer120efee2013-01-25 12:10:11 +01001337 /* Pack texture coordinates */
1338 address[count++] = coords[0];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001339 if (num_coords > 1)
Michel Dänzer120efee2013-01-25 12:10:11 +01001340 address[count++] = coords[1];
Michel Dänzerbeaa5eb2013-05-24 13:23:26 +01001341 if (num_coords > 2)
Michel Dänzer120efee2013-01-25 12:10:11 +01001342 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +01001343
Marek Olšákd2bd6342013-09-18 15:40:21 +02001344 /* Pack LOD or sample index */
Michel Dänzer36231112013-05-02 09:44:45 +02001345 if (opcode == TGSI_OPCODE_TXL || opcode == TGSI_OPCODE_TXF)
Michel Dänzer120efee2013-01-25 12:10:11 +01001346 address[count++] = coords[3];
1347
1348 if (count > 16) {
1349 assert(!"Cannot handle more than 16 texture address parameters");
1350 count = 16;
1351 }
1352
1353 for (chan = 0; chan < count; chan++ ) {
1354 address[chan] = LLVMBuildBitCast(gallivm->builder,
1355 address[chan],
1356 LLVMInt32TypeInContext(gallivm->context),
1357 "");
1358 }
1359
Marek Olšák4855acd2013-08-06 15:08:54 +02001360 /* Adjust the sample index according to FMASK.
1361 *
1362 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
1363 * which is the identity mapping. Each nibble says which physical sample
1364 * should be fetched to get that sample.
1365 *
1366 * For example, 0x11111100 means there are only 2 samples stored and
1367 * the second sample covers 3/4 of the pixel. When reading samples 0
1368 * and 1, return physical sample 0 (determined by the first two 0s
1369 * in FMASK), otherwise return physical sample 1.
1370 *
1371 * The sample index should be adjusted as follows:
1372 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
1373 */
1374 if (target == TGSI_TEXTURE_2D_MSAA ||
1375 target == TGSI_TEXTURE_2D_ARRAY_MSAA) {
1376 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1377 struct lp_build_emit_data txf_emit_data = *emit_data;
Marek Olšákd2bd6342013-09-18 15:40:21 +02001378 LLVMValueRef txf_address[4];
Marek Olšák4855acd2013-08-06 15:08:54 +02001379 unsigned txf_count = count;
1380
Marek Olšákd2bd6342013-09-18 15:40:21 +02001381 memcpy(txf_address, address, sizeof(txf_address));
1382
1383 if (target == TGSI_TEXTURE_2D_MSAA) {
1384 txf_address[2] = bld_base->uint_bld.zero;
1385 }
1386 txf_address[3] = bld_base->uint_bld.zero;
Marek Olšák4855acd2013-08-06 15:08:54 +02001387
1388 /* Pad to a power-of-two size. */
1389 while (txf_count < util_next_power_of_two(txf_count))
1390 txf_address[txf_count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1391
1392 /* Read FMASK using TXF. */
1393 txf_emit_data.chan = 0;
1394 txf_emit_data.dst_type = LLVMVectorType(
1395 LLVMInt32TypeInContext(bld_base->base.gallivm->context), 4);
1396 txf_emit_data.args[0] = lp_build_gather_values(gallivm, txf_address, txf_count);
1397 txf_emit_data.args[1] = si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index];
Marek Olšákd2bd6342013-09-18 15:40:21 +02001398 txf_emit_data.args[2] = lp_build_const_int32(bld_base->base.gallivm,
1399 target == TGSI_TEXTURE_2D_MSAA ? TGSI_TEXTURE_2D : TGSI_TEXTURE_2D_ARRAY);
Marek Olšák4855acd2013-08-06 15:08:54 +02001400 txf_emit_data.arg_count = 3;
1401
1402 build_tex_intrinsic(&txf_action, bld_base, &txf_emit_data);
1403
1404 /* Initialize some constants. */
Marek Olšák4855acd2013-08-06 15:08:54 +02001405 LLVMValueRef four = LLVMConstInt(uint_bld->elem_type, 4, 0);
1406 LLVMValueRef F = LLVMConstInt(uint_bld->elem_type, 0xF, 0);
1407
1408 /* Apply the formula. */
1409 LLVMValueRef fmask =
1410 LLVMBuildExtractElement(gallivm->builder,
1411 txf_emit_data.output[0],
1412 uint_bld->zero, "");
1413
Marek Olšákd2bd6342013-09-18 15:40:21 +02001414 unsigned sample_chan = target == TGSI_TEXTURE_2D_MSAA ? 2 : 3;
Marek Olšák4855acd2013-08-06 15:08:54 +02001415
1416 LLVMValueRef sample_index4 =
Marek Olšákd2bd6342013-09-18 15:40:21 +02001417 LLVMBuildMul(gallivm->builder, address[sample_chan], four, "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001418
1419 LLVMValueRef shifted_fmask =
1420 LLVMBuildLShr(gallivm->builder, fmask, sample_index4, "");
1421
1422 LLVMValueRef final_sample =
1423 LLVMBuildAnd(gallivm->builder, shifted_fmask, F, "");
1424
1425 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
1426 * resource descriptor is 0 (invalid),
1427 */
1428 LLVMValueRef fmask_desc =
1429 LLVMBuildBitCast(gallivm->builder,
1430 si_shader_ctx->resources[FMASK_TEX_OFFSET + sampler_index],
1431 LLVMVectorType(uint_bld->elem_type, 8), "");
1432
1433 LLVMValueRef fmask_word1 =
1434 LLVMBuildExtractElement(gallivm->builder, fmask_desc,
1435 uint_bld->one, "");
1436
1437 LLVMValueRef word1_is_nonzero =
1438 LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1439 fmask_word1, uint_bld->zero, "");
1440
Marek Olšákd2bd6342013-09-18 15:40:21 +02001441 /* Replace the MSAA sample index. */
1442 address[sample_chan] =
Marek Olšák4855acd2013-08-06 15:08:54 +02001443 LLVMBuildSelect(gallivm->builder, word1_is_nonzero,
Marek Olšákd2bd6342013-09-18 15:40:21 +02001444 final_sample, address[sample_chan], "");
Marek Olšák4855acd2013-08-06 15:08:54 +02001445 }
Michel Dänzera6b83c02013-02-21 16:10:55 +01001446
Michel Dänzer36231112013-05-02 09:44:45 +02001447 /* Resource */
Marek Olšák4855acd2013-08-06 15:08:54 +02001448 emit_data->args[1] = si_shader_ctx->resources[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001449
1450 if (opcode == TGSI_OPCODE_TXF) {
1451 /* add tex offsets */
1452 if (inst->Texture.NumOffsets) {
1453 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1454 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
1455 const struct tgsi_texture_offset * off = inst->TexOffsets;
1456
1457 assert(inst->Texture.NumOffsets == 1);
1458
Marek Olšákdefedc02013-09-18 15:36:38 +02001459 switch (target) {
1460 case TGSI_TEXTURE_3D:
1461 address[2] = lp_build_add(uint_bld, address[2],
1462 bld->immediates[off->Index][off->SwizzleZ]);
1463 /* fall through */
1464 case TGSI_TEXTURE_2D:
1465 case TGSI_TEXTURE_SHADOW2D:
1466 case TGSI_TEXTURE_RECT:
1467 case TGSI_TEXTURE_SHADOWRECT:
1468 case TGSI_TEXTURE_2D_ARRAY:
1469 case TGSI_TEXTURE_SHADOW2D_ARRAY:
Michel Dänzer36231112013-05-02 09:44:45 +02001470 address[1] =
1471 lp_build_add(uint_bld, address[1],
Marek Olšákdefedc02013-09-18 15:36:38 +02001472 bld->immediates[off->Index][off->SwizzleY]);
1473 /* fall through */
1474 case TGSI_TEXTURE_1D:
1475 case TGSI_TEXTURE_SHADOW1D:
1476 case TGSI_TEXTURE_1D_ARRAY:
1477 case TGSI_TEXTURE_SHADOW1D_ARRAY:
1478 address[0] =
1479 lp_build_add(uint_bld, address[0],
1480 bld->immediates[off->Index][off->SwizzleX]);
1481 break;
1482 /* texture offsets do not apply to other texture targets */
1483 }
Michel Dänzer36231112013-05-02 09:44:45 +02001484 }
1485
1486 emit_data->dst_type = LLVMVectorType(
1487 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1488 4);
1489
1490 emit_data->arg_count = 3;
1491 } else {
1492 /* Sampler */
Marek Olšák4855acd2013-08-06 15:08:54 +02001493 emit_data->args[2] = si_shader_ctx->samplers[sampler_index];
Michel Dänzer36231112013-05-02 09:44:45 +02001494
1495 emit_data->dst_type = LLVMVectorType(
1496 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
1497 4);
1498
1499 emit_data->arg_count = 4;
1500 }
1501
1502 /* Dimensions */
1503 emit_data->args[emit_data->arg_count - 1] =
1504 lp_build_const_int32(bld_base->base.gallivm, target);
1505
Michel Dänzer120efee2013-01-25 12:10:11 +01001506 /* Pad to power of two vector */
1507 while (count < util_next_power_of_two(count))
1508 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
1509
Christian Königccf3e8f2013-03-26 15:09:27 +01001510 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Tom Stellarda75c6162012-01-06 17:38:37 -05001511}
1512
Michel Dänzer07eddc42013-02-06 15:43:10 +01001513static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
1514 struct lp_build_tgsi_context * bld_base,
1515 struct lp_build_emit_data * emit_data)
1516{
1517 struct lp_build_context * base = &bld_base->base;
Kai Wasserbächbbb77fc2013-10-27 19:36:07 +01001518 char intr_name[127];
Michel Dänzer07eddc42013-02-06 15:43:10 +01001519
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001520 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1521 emit_data->output[emit_data->chan] = build_intrinsic(
1522 base->gallivm->builder,
1523 "llvm.SI.vs.load.input", emit_data->dst_type,
1524 emit_data->args, emit_data->arg_count,
1525 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1526 return;
1527 }
1528
Michel Dänzer07eddc42013-02-06 15:43:10 +01001529 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001530 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001531
Christian König44e32242013-03-20 12:10:35 +01001532 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001533 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001534 emit_data->args, emit_data->arg_count,
1535 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001536}
1537
Michel Dänzer0495adb2013-05-06 12:45:14 +02001538static void txq_fetch_args(
1539 struct lp_build_tgsi_context * bld_base,
1540 struct lp_build_emit_data * emit_data)
1541{
1542 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1543 const struct tgsi_full_instruction *inst = emit_data->inst;
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001544 struct gallivm_state *gallivm = bld_base->base.gallivm;
1545
1546 if (inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1547 LLVMTypeRef i32 = LLVMInt32TypeInContext(gallivm->context);
1548 LLVMTypeRef v8i32 = LLVMVectorType(i32, 8);
1549
1550 /* Read the size from the buffer descriptor directly. */
1551 LLVMValueRef size = si_shader_ctx->resources[inst->Src[1].Register.Index];
1552 size = LLVMBuildBitCast(gallivm->builder, size, v8i32, "");
1553 size = LLVMBuildExtractElement(gallivm->builder, size,
1554 lp_build_const_int32(gallivm, 2), "");
1555 emit_data->args[0] = size;
1556 return;
1557 }
Michel Dänzer0495adb2013-05-06 12:45:14 +02001558
1559 /* Mip level */
1560 emit_data->args[0] = lp_build_emit_fetch(bld_base, inst, 0, TGSI_CHAN_X);
1561
1562 /* Resource */
1563 emit_data->args[1] = si_shader_ctx->resources[inst->Src[1].Register.Index];
1564
1565 /* Dimensions */
1566 emit_data->args[2] = lp_build_const_int32(bld_base->base.gallivm,
1567 inst->Texture.Texture);
1568
1569 emit_data->arg_count = 3;
1570
1571 emit_data->dst_type = LLVMVectorType(
1572 LLVMInt32TypeInContext(bld_base->base.gallivm->context),
1573 4);
1574}
1575
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001576static void build_txq_intrinsic(const struct lp_build_tgsi_action * action,
1577 struct lp_build_tgsi_context * bld_base,
1578 struct lp_build_emit_data * emit_data)
1579{
1580 if (emit_data->inst->Texture.Texture == TGSI_TEXTURE_BUFFER) {
1581 /* Just return the buffer size. */
1582 emit_data->output[emit_data->chan] = emit_data->args[0];
1583 return;
1584 }
1585
1586 build_tgsi_intrinsic_nomem(action, bld_base, emit_data);
1587}
1588
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001589#if HAVE_LLVM >= 0x0304
1590
1591static void si_llvm_emit_ddxy(
1592 const struct lp_build_tgsi_action * action,
1593 struct lp_build_tgsi_context * bld_base,
1594 struct lp_build_emit_data * emit_data)
1595{
1596 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
1597 struct gallivm_state *gallivm = bld_base->base.gallivm;
1598 struct lp_build_context * base = &bld_base->base;
1599 const struct tgsi_full_instruction *inst = emit_data->inst;
1600 unsigned opcode = inst->Instruction.Opcode;
1601 LLVMValueRef indices[2];
1602 LLVMValueRef store_ptr, load_ptr0, load_ptr1;
1603 LLVMValueRef tl, trbl, result[4];
1604 LLVMTypeRef i32;
1605 unsigned swizzle[4];
1606 unsigned c;
1607
1608 i32 = LLVMInt32TypeInContext(gallivm->context);
1609
1610 indices[0] = bld_base->uint_bld.zero;
1611 indices[1] = build_intrinsic(gallivm->builder, "llvm.SI.tid", i32,
1612 NULL, 0, LLVMReadNoneAttribute);
1613 store_ptr = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1614 indices, 2, "");
1615
1616 indices[1] = LLVMBuildAnd(gallivm->builder, indices[1],
1617 lp_build_const_int32(gallivm, 0xfffffffc), "");
1618 load_ptr0 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1619 indices, 2, "");
1620
1621 indices[1] = LLVMBuildAdd(gallivm->builder, indices[1],
1622 lp_build_const_int32(gallivm,
1623 opcode == TGSI_OPCODE_DDX ? 1 : 2),
1624 "");
1625 load_ptr1 = LLVMBuildGEP(gallivm->builder, si_shader_ctx->ddxy_lds,
1626 indices, 2, "");
1627
1628 for (c = 0; c < 4; ++c) {
1629 unsigned i;
1630
1631 swizzle[c] = tgsi_util_get_full_src_register_swizzle(&inst->Src[0], c);
1632 for (i = 0; i < c; ++i) {
1633 if (swizzle[i] == swizzle[c]) {
1634 result[c] = result[i];
1635 break;
1636 }
1637 }
1638 if (i != c)
1639 continue;
1640
1641 LLVMBuildStore(gallivm->builder,
1642 LLVMBuildBitCast(gallivm->builder,
1643 lp_build_emit_fetch(bld_base, inst, 0, c),
1644 i32, ""),
1645 store_ptr);
1646
1647 tl = LLVMBuildLoad(gallivm->builder, load_ptr0, "");
1648 tl = LLVMBuildBitCast(gallivm->builder, tl, base->elem_type, "");
1649
1650 trbl = LLVMBuildLoad(gallivm->builder, load_ptr1, "");
1651 trbl = LLVMBuildBitCast(gallivm->builder, trbl, base->elem_type, "");
1652
1653 result[c] = LLVMBuildFSub(gallivm->builder, trbl, tl, "");
1654 }
1655
1656 emit_data->output[0] = lp_build_gather_values(gallivm, result, 4);
1657}
1658
1659#endif /* HAVE_LLVM >= 0x0304 */
1660
Tom Stellarda75c6162012-01-06 17:38:37 -05001661static const struct lp_build_tgsi_action tex_action = {
1662 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001663 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001664 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001665};
1666
Michel Dänzer3e205132012-11-06 17:39:01 +01001667static const struct lp_build_tgsi_action txb_action = {
1668 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001669 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001670 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001671};
1672
Michel Dänzera6b83c02013-02-21 16:10:55 +01001673#if HAVE_LLVM >= 0x0304
1674static const struct lp_build_tgsi_action txd_action = {
1675 .fetch_args = tex_fetch_args,
1676 .emit = build_tex_intrinsic,
1677 .intr_name = "llvm.SI.sampled."
1678};
1679#endif
1680
Michel Dänzer36231112013-05-02 09:44:45 +02001681static const struct lp_build_tgsi_action txf_action = {
1682 .fetch_args = tex_fetch_args,
1683 .emit = build_tex_intrinsic,
1684 .intr_name = "llvm.SI.imageload."
1685};
1686
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001687static const struct lp_build_tgsi_action txl_action = {
1688 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001689 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001690 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001691};
1692
Michel Dänzer0495adb2013-05-06 12:45:14 +02001693static const struct lp_build_tgsi_action txq_action = {
1694 .fetch_args = txq_fetch_args,
Marek Olšákdbeedbb2013-10-31 15:08:49 +01001695 .emit = build_txq_intrinsic,
Michel Dänzer0495adb2013-05-06 12:45:14 +02001696 .intr_name = "llvm.SI.resinfo"
1697};
1698
Christian König206f0592013-03-20 14:37:21 +01001699static void create_meta_data(struct si_shader_context *si_shader_ctx)
1700{
1701 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1702 LLVMValueRef args[3];
1703
1704 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1705 args[1] = 0;
1706 args[2] = lp_build_const_int32(gallivm, 1);
1707
1708 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1709}
1710
Christian König55fe5cc2013-03-04 16:30:06 +01001711static void create_function(struct si_shader_context *si_shader_ctx)
1712{
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001713 struct lp_build_tgsi_context *bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1714 struct gallivm_state *gallivm = bld_base->base.gallivm;
Vadim Girlin453ea2d2013-10-13 19:53:54 +04001715 LLVMTypeRef params[21], f32, i8, i32, v2i32, v3i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001716 unsigned i, last_sgpr, num_params;
Christian König55fe5cc2013-03-04 16:30:06 +01001717
Christian König55fe5cc2013-03-04 16:30:06 +01001718 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01001719 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01001720 f32 = LLVMFloatTypeInContext(gallivm->context);
1721 v2i32 = LLVMVectorType(i32, 2);
1722 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01001723
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02001724 params[SI_PARAM_CONST] = LLVMPointerType(
1725 LLVMArrayType(LLVMVectorType(i8, 16), NUM_CONST_BUFFERS), CONST_ADDR_SPACE);
1726 /* We assume at most 16 textures per program at the moment.
1727 * This need probably need to be changed to support bindless textures */
1728 params[SI_PARAM_SAMPLER] = LLVMPointerType(
1729 LLVMArrayType(LLVMVectorType(i8, 16), NUM_SAMPLER_VIEWS), CONST_ADDR_SPACE);
1730 params[SI_PARAM_RESOURCE] = LLVMPointerType(
1731 LLVMArrayType(LLVMVectorType(i8, 32), NUM_SAMPLER_STATES), CONST_ADDR_SPACE);
Christian König55fe5cc2013-03-04 16:30:06 +01001732
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001733 switch (si_shader_ctx->type) {
1734 case TGSI_PROCESSOR_VERTEX:
1735 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_CONST];
Marek Olšák2993cca2013-08-18 02:34:23 +02001736 params[SI_PARAM_SO_BUFFER] = params[SI_PARAM_CONST];
Christian Königcf9b31f2013-03-21 18:30:23 +01001737 params[SI_PARAM_START_INSTANCE] = i32;
Marek Olšák8d03d922013-09-01 23:59:06 +02001738 num_params = SI_PARAM_START_INSTANCE+1;
1739
1740 /* The locations of the other parameters are assigned dynamically. */
1741
1742 /* Streamout SGPRs. */
1743 if (si_shader_ctx->shader->selector->so.num_outputs) {
1744 params[si_shader_ctx->param_streamout_config = num_params++] = i32;
1745 params[si_shader_ctx->param_streamout_write_index = num_params++] = i32;
1746 }
1747 /* A streamout buffer offset is loaded if the stride is non-zero. */
1748 for (i = 0; i < 4; i++) {
1749 if (!si_shader_ctx->shader->selector->so.stride[i])
1750 continue;
1751
1752 params[si_shader_ctx->param_streamout_offset[i] = num_params++] = i32;
1753 }
1754
1755 last_sgpr = num_params-1;
1756
1757 /* VGPRs */
1758 params[si_shader_ctx->param_vertex_id = num_params++] = i32;
1759 params[num_params++] = i32; /* unused*/
1760 params[num_params++] = i32; /* unused */
1761 params[si_shader_ctx->param_instance_id = num_params++] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001762 break;
Christian König0666ffd2013-03-05 15:07:39 +01001763
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001764 case TGSI_PROCESSOR_FRAGMENT:
Vadim Girlin453ea2d2013-10-13 19:53:54 +04001765 params[SI_PARAM_ALPHA_REF] = f32;
Christian König0666ffd2013-03-05 15:07:39 +01001766 params[SI_PARAM_PRIM_MASK] = i32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001767 last_sgpr = SI_PARAM_PRIM_MASK;
Christian König0666ffd2013-03-05 15:07:39 +01001768 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
1769 params[SI_PARAM_PERSP_CENTER] = v2i32;
1770 params[SI_PARAM_PERSP_CENTROID] = v2i32;
1771 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
1772 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
1773 params[SI_PARAM_LINEAR_CENTER] = v2i32;
1774 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
1775 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
1776 params[SI_PARAM_POS_X_FLOAT] = f32;
1777 params[SI_PARAM_POS_Y_FLOAT] = f32;
1778 params[SI_PARAM_POS_Z_FLOAT] = f32;
1779 params[SI_PARAM_POS_W_FLOAT] = f32;
1780 params[SI_PARAM_FRONT_FACE] = f32;
1781 params[SI_PARAM_ANCILLARY] = f32;
1782 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
1783 params[SI_PARAM_POS_FIXED_PT] = f32;
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001784 num_params = SI_PARAM_POS_FIXED_PT+1;
1785 break;
1786
1787 default:
1788 assert(0 && "unimplemented shader");
1789 return;
Christian Königc4973212013-03-05 12:14:02 +01001790 }
Christian König55fe5cc2013-03-04 16:30:06 +01001791
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001792 assert(num_params <= Elements(params));
1793 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, num_params);
Christian König55fe5cc2013-03-04 16:30:06 +01001794 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
Christian Königcf9b31f2013-03-21 18:30:23 +01001795
Marek Olšák13a1a8b2013-08-18 01:57:40 +02001796 for (i = 0; i <= last_sgpr; ++i) {
1797 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
Vincent Lejeune6e51c2a2013-10-05 16:04:48 +02001798 switch (i) {
1799 default:
1800 LLVMAddAttribute(P, LLVMInRegAttribute);
1801 break;
1802#if HAVE_LLVM >= 0x0304
1803 /* We tell llvm that array inputs are passed by value to allow Sinking pass
1804 * to move load. Inputs are constant so this is fine. */
1805 case SI_PARAM_CONST:
1806 case SI_PARAM_SAMPLER:
1807 case SI_PARAM_RESOURCE:
1808 LLVMAddAttribute(P, LLVMByValAttribute);
1809 break;
1810#endif
1811 }
Christian Königcf9b31f2013-03-21 18:30:23 +01001812 }
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001813
1814#if HAVE_LLVM >= 0x0304
1815 if (bld_base->info->opcode_count[TGSI_OPCODE_DDX] > 0 ||
1816 bld_base->info->opcode_count[TGSI_OPCODE_DDY] > 0)
1817 si_shader_ctx->ddxy_lds =
1818 LLVMAddGlobalInAddressSpace(gallivm->module,
1819 LLVMArrayType(i32, 64),
1820 "ddxy_lds",
1821 LOCAL_ADDR_SPACE);
1822#endif
Christian König55fe5cc2013-03-04 16:30:06 +01001823}
Tom Stellarda75c6162012-01-06 17:38:37 -05001824
Christian König0f6cf2b2013-03-15 15:53:25 +01001825static void preload_constants(struct si_shader_context *si_shader_ctx)
1826{
1827 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1828 struct gallivm_state * gallivm = bld_base->base.gallivm;
1829 const struct tgsi_shader_info * info = bld_base->info;
Marek Olšák2fd42002013-10-25 11:45:47 +02001830 unsigned buf;
1831 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
Christian König0f6cf2b2013-03-15 15:53:25 +01001832
Marek Olšák2fd42002013-10-25 11:45:47 +02001833 for (buf = 0; buf < NUM_CONST_BUFFERS; buf++) {
1834 unsigned i, num_const = info->const_file_max[buf] + 1;
Christian König0f6cf2b2013-03-15 15:53:25 +01001835
Marek Olšák2fd42002013-10-25 11:45:47 +02001836 if (num_const == 0)
1837 continue;
Christian König0f6cf2b2013-03-15 15:53:25 +01001838
Marek Olšák2fd42002013-10-25 11:45:47 +02001839 /* Allocate space for the constant values */
1840 si_shader_ctx->constants[buf] = CALLOC(num_const * 4, sizeof(LLVMValueRef));
Christian König0f6cf2b2013-03-15 15:53:25 +01001841
Marek Olšák2fd42002013-10-25 11:45:47 +02001842 /* Load the resource descriptor */
1843 si_shader_ctx->const_resource[buf] =
1844 build_indexed_load(si_shader_ctx, ptr, lp_build_const_int32(gallivm, buf));
Christian König0f6cf2b2013-03-15 15:53:25 +01001845
Marek Olšák2fd42002013-10-25 11:45:47 +02001846 /* Load the constants, we rely on the code sinking to do the rest */
1847 for (i = 0; i < num_const * 4; ++i) {
1848 LLVMValueRef args[2] = {
1849 si_shader_ctx->const_resource[buf],
1850 lp_build_const_int32(gallivm, i * 4)
1851 };
1852 si_shader_ctx->constants[buf][i] =
1853 build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1854 bld_base->base.elem_type, args, 2,
1855 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1856 }
Christian König0f6cf2b2013-03-15 15:53:25 +01001857 }
1858}
1859
Christian König1c100182013-03-17 16:02:42 +01001860static void preload_samplers(struct si_shader_context *si_shader_ctx)
1861{
1862 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1863 struct gallivm_state * gallivm = bld_base->base.gallivm;
1864 const struct tgsi_shader_info * info = bld_base->info;
1865
1866 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1867
1868 LLVMValueRef res_ptr, samp_ptr;
1869 LLVMValueRef offset;
1870
1871 if (num_samplers == 0)
1872 return;
1873
1874 /* Allocate space for the values */
Marek Olšák4855acd2013-08-06 15:08:54 +02001875 si_shader_ctx->resources = CALLOC(NUM_SAMPLER_VIEWS, sizeof(LLVMValueRef));
Christian König1c100182013-03-17 16:02:42 +01001876 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1877
1878 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1879 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1880
1881 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1882 for (i = 0; i < num_samplers; ++i) {
Christian König1c100182013-03-17 16:02:42 +01001883 /* Resource */
1884 offset = lp_build_const_int32(gallivm, i);
1885 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1886
1887 /* Sampler */
1888 offset = lp_build_const_int32(gallivm, i);
1889 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
Marek Olšák4855acd2013-08-06 15:08:54 +02001890
1891 /* FMASK resource */
1892 if (info->is_msaa_sampler[i]) {
1893 offset = lp_build_const_int32(gallivm, FMASK_TEX_OFFSET + i);
1894 si_shader_ctx->resources[FMASK_TEX_OFFSET + i] =
1895 build_indexed_load(si_shader_ctx, res_ptr, offset);
1896 }
Christian König1c100182013-03-17 16:02:42 +01001897 }
1898}
1899
Marek Olšák8d03d922013-09-01 23:59:06 +02001900static void preload_streamout_buffers(struct si_shader_context *si_shader_ctx)
1901{
1902 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1903 struct gallivm_state * gallivm = bld_base->base.gallivm;
1904 unsigned i;
1905
1906 if (!si_shader_ctx->shader->selector->so.num_outputs)
1907 return;
1908
1909 LLVMValueRef buf_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1910 SI_PARAM_SO_BUFFER);
1911
1912 /* Load the resources, we rely on the code sinking to do the rest */
1913 for (i = 0; i < 4; ++i) {
1914 if (si_shader_ctx->shader->selector->so.stride[i]) {
1915 LLVMValueRef offset = lp_build_const_int32(gallivm, i);
1916
1917 si_shader_ctx->so_buffers[i] = build_indexed_load(si_shader_ctx, buf_ptr, offset);
1918 }
1919 }
1920}
1921
Andreas Hartmetz8662e662014-01-11 16:00:50 +01001922int si_compile_llvm(struct si_context *sctx, struct si_pipe_shader *shader,
Tom Stellard302f53d2012-10-25 13:50:10 -04001923 LLVMModuleRef mod)
1924{
Tom Stellard302f53d2012-10-25 13:50:10 -04001925 unsigned i;
1926 uint32_t *ptr;
Tom Stellard7782d192013-04-04 09:57:13 -07001927 struct radeon_llvm_binary binary;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01001928 bool dump = r600_can_dump_shader(&sctx->screen->b,
Tom Stellardb2805162013-10-03 17:39:59 -04001929 shader->selector ? shader->selector->tokens : NULL);
Tom Stellard7782d192013-04-04 09:57:13 -07001930 memset(&binary, 0, sizeof(binary));
1931 radeon_llvm_compile(mod, &binary,
Marek Olšák65dc5882014-01-22 02:42:20 +01001932 r600_get_llvm_processor_name(sctx->screen->b.family), dump);
Jay Cornwalld7d539a2013-10-10 20:06:48 -05001933 if (dump && ! binary.disassembled) {
Tom Stellard302f53d2012-10-25 13:50:10 -04001934 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07001935 for (i = 0; i < binary.code_size; i+=4 ) {
1936 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
1937 binary.code[i + 2], binary.code[i + 1],
1938 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04001939 }
1940 }
1941
Tom Stellardd50343d2013-04-04 16:21:06 -04001942 /* XXX: We may be able to emit some of these values directly rather than
1943 * extracting fields to be emitted later.
1944 */
1945 for (i = 0; i < binary.config_size; i+= 8) {
1946 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
1947 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
1948 switch (reg) {
1949 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
1950 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
1951 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
1952 case R_00B848_COMPUTE_PGM_RSRC1:
1953 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
1954 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
1955 break;
Michel Dänzera06ee5a2013-06-19 18:14:01 +02001956 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
1957 shader->lds_size = G_00B02C_EXTRA_LDS_SIZE(value);
1958 break;
1959 case R_00B84C_COMPUTE_PGM_RSRC2:
1960 shader->lds_size = G_00B84C_LDS_SIZE(value);
1961 break;
Tom Stellardd50343d2013-04-04 16:21:06 -04001962 case R_0286CC_SPI_PS_INPUT_ENA:
1963 shader->spi_ps_input_ena = value;
1964 break;
1965 default:
1966 fprintf(stderr, "Warning: Compiler emitted unknown "
1967 "config register: 0x%x\n", reg);
1968 break;
1969 }
1970 }
Tom Stellard302f53d2012-10-25 13:50:10 -04001971
1972 /* copy new shader */
Marek Olšáka81c3e02013-08-14 01:04:39 +02001973 r600_resource_reference(&shader->bo, NULL);
Andreas Hartmetz8662e662014-01-11 16:00:50 +01001974 shader->bo = si_resource_create_custom(sctx->b.b.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04001975 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001976 if (shader->bo == NULL) {
1977 return -ENOMEM;
1978 }
1979
Andreas Hartmetz8662e662014-01-11 16:00:50 +01001980 ptr = (uint32_t*)sctx->b.ws->buffer_map(shader->bo->cs_buf, sctx->b.rings.gfx.cs, PIPE_TRANSFER_WRITE);
Andreas Hartmetz0b57fc12014-01-11 15:56:47 +01001981 if (0 /*SI_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04001982 for (i = 0; i < binary.code_size / 4; ++i) {
1983 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04001984 }
1985 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04001986 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001987 }
Andreas Hartmetz8662e662014-01-11 16:00:50 +01001988 sctx->b.ws->buffer_unmap(shader->bo->cs_buf);
Tom Stellard302f53d2012-10-25 13:50:10 -04001989
Tom Stellard7782d192013-04-04 09:57:13 -07001990 free(binary.code);
1991 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04001992
1993 return 0;
1994}
1995
Tom Stellarda75c6162012-01-06 17:38:37 -05001996int si_pipe_shader_create(
1997 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01001998 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05001999{
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002000 struct si_context *sctx = (struct si_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002001 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05002002 struct si_shader_context si_shader_ctx;
2003 struct tgsi_shader_info shader_info;
2004 struct lp_build_tgsi_context * bld_base;
2005 LLVMModuleRef mod;
Tom Stellard302f53d2012-10-25 13:50:10 -04002006 int r = 0;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002007 bool dump = r600_can_dump_shader(&sctx->screen->b, shader->selector->tokens);
Tom Stellarda75c6162012-01-06 17:38:37 -05002008
Michel Dänzer82e38ac2012-09-27 16:39:26 +02002009 assert(shader->shader.noutput == 0);
2010 assert(shader->shader.ninterp == 0);
2011 assert(shader->shader.ninput == 0);
2012
Michel Dänzercfebaf92012-08-31 19:04:08 +02002013 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05002014 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
2015 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
2016
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002017 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002018
Michel Dänzere44dfd42012-11-07 17:33:08 +01002019 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01002020 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05002021 bld_base->info = &shader_info;
2022 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05002023
2024 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01002025 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzera6b83c02013-02-21 16:10:55 +01002026#if HAVE_LLVM >= 0x0304
2027 bld_base->op_actions[TGSI_OPCODE_TXD] = txd_action;
2028#endif
Michel Dänzer36231112013-05-02 09:44:45 +02002029 bld_base->op_actions[TGSI_OPCODE_TXF] = txf_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01002030 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02002031 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Michel Dänzer0495adb2013-05-06 12:45:14 +02002032 bld_base->op_actions[TGSI_OPCODE_TXQ] = txq_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05002033
Michel Dänzera06ee5a2013-06-19 18:14:01 +02002034#if HAVE_LLVM >= 0x0304
2035 bld_base->op_actions[TGSI_OPCODE_DDX].emit = si_llvm_emit_ddxy;
2036 bld_base->op_actions[TGSI_OPCODE_DDY].emit = si_llvm_emit_ddxy;
2037#endif
2038
Christian Könige4ed5872013-03-21 18:02:52 +01002039 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002040 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05002041 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
2042 si_shader_ctx.shader = shader;
2043 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05002044
Michel Dänzer51f89a02013-12-09 15:33:53 +09002045 switch (si_shader_ctx.type) {
2046 case TGSI_PROCESSOR_VERTEX:
2047 si_shader_ctx.radeon_bld.load_input = declare_input_vs;
2048 bld_base->emit_epilogue = si_llvm_emit_vs_epilogue;
2049 break;
2050 case TGSI_PROCESSOR_FRAGMENT:
2051 si_shader_ctx.radeon_bld.load_input = declare_input_fs;
2052 bld_base->emit_epilogue = si_llvm_emit_fs_epilogue;
2053 break;
2054 default:
2055 assert(!"Unsupported shader type");
2056 return -1;
2057 }
2058
Christian König206f0592013-03-20 14:37:21 +01002059 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01002060 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01002061 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01002062 preload_samplers(&si_shader_ctx);
Marek Olšák8d03d922013-09-01 23:59:06 +02002063 preload_streamout_buffers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01002064
Tom Stellard185fc9a2012-07-12 10:40:47 -04002065 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
2066 * conversion fails. */
2067 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002068 tgsi_dump(sel->tokens, 0);
Marek Olšák8d03d922013-09-01 23:59:06 +02002069 si_dump_streamout(&sel->so);
Tom Stellard185fc9a2012-07-12 10:40:47 -04002070 }
2071
Michel Dänzerd1e40b32012-08-23 17:10:37 +02002072 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002073 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Marek Olšák2fd42002013-10-25 11:45:47 +02002074 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2075 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002076 FREE(si_shader_ctx.resources);
2077 FREE(si_shader_ctx.samplers);
Michel Dänzer82cd9c02012-08-08 15:35:42 +02002078 return -EINVAL;
2079 }
Tom Stellarda75c6162012-01-06 17:38:37 -05002080
2081 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
2082
2083 mod = bld_base->base.gallivm->module;
Andreas Hartmetz8662e662014-01-11 16:00:50 +01002084 r = si_compile_llvm(sctx, shader, mod);
Tom Stellarda75c6162012-01-06 17:38:37 -05002085
Michel Dänzer4b64fa22012-08-15 18:22:46 +02002086 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05002087 tgsi_parse_free(&si_shader_ctx.parse);
2088
Marek Olšák2fd42002013-10-25 11:45:47 +02002089 for (int i = 0; i < NUM_CONST_BUFFERS; i++)
2090 FREE(si_shader_ctx.constants[i]);
Christian König1c100182013-03-17 16:02:42 +01002091 FREE(si_shader_ctx.resources);
2092 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05002093
Tom Stellard302f53d2012-10-25 13:50:10 -04002094 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05002095}
2096
2097void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
2098{
Marek Olšáka81c3e02013-08-14 01:04:39 +02002099 r600_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05002100}