blob: 3e023f8600d1ecff49250567561b59491ef6570f [file] [log] [blame]
Tom Stellarda75c6162012-01-06 17:38:37 -05001
Christian Königce40e472012-08-02 12:14:59 +02002/*
3 * Copyright 2012 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Tom Stellard <thomas.stellard@amd.com>
26 * Michel Dänzer <michel.daenzer@amd.com>
27 * Christian König <christian.koenig@amd.com>
28 */
29
Tom Stellarda75c6162012-01-06 17:38:37 -050030#include "gallivm/lp_bld_tgsi_action.h"
31#include "gallivm/lp_bld_const.h"
Michel Dänzerc2bae6b2012-08-02 17:19:22 +020032#include "gallivm/lp_bld_gather.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050033#include "gallivm/lp_bld_intr.h"
Michel Dänzer7708a862012-11-02 15:57:30 +010034#include "gallivm/lp_bld_logic.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050035#include "gallivm/lp_bld_tgsi.h"
Christian König5e616cf2013-03-07 11:58:56 +010036#include "gallivm/lp_bld_arit.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050037#include "radeon_llvm.h"
Tom Stellard509ddb02012-04-16 17:48:44 -040038#include "radeon_llvm_emit.h"
Christian König0f6cf2b2013-03-15 15:53:25 +010039#include "util/u_memory.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050040#include "tgsi/tgsi_info.h"
41#include "tgsi/tgsi_parse.h"
42#include "tgsi/tgsi_scan.h"
43#include "tgsi/tgsi_dump.h"
44
45#include "radeonsi_pipe.h"
46#include "radeonsi_shader.h"
Christian Königf67fae02012-07-17 23:43:00 +020047#include "si_state.h"
Tom Stellarda75c6162012-01-06 17:38:37 -050048#include "sid.h"
49
50#include <assert.h>
51#include <errno.h>
52#include <stdio.h>
53
Tom Stellarda75c6162012-01-06 17:38:37 -050054struct si_shader_context
55{
56 struct radeon_llvm_context radeon_bld;
Tom Stellarda75c6162012-01-06 17:38:37 -050057 struct tgsi_parse_context parse;
58 struct tgsi_token * tokens;
59 struct si_pipe_shader *shader;
60 unsigned type; /* TGSI_PROCESSOR_* specifies the type of shader. */
Christian König206f0592013-03-20 14:37:21 +010061 LLVMValueRef const_md;
Christian König0f6cf2b2013-03-15 15:53:25 +010062 LLVMValueRef const_resource;
63 LLVMValueRef *constants;
Christian König1c100182013-03-17 16:02:42 +010064 LLVMValueRef *resources;
65 LLVMValueRef *samplers;
Tom Stellarda75c6162012-01-06 17:38:37 -050066};
67
68static struct si_shader_context * si_shader_context(
69 struct lp_build_tgsi_context * bld_base)
70{
71 return (struct si_shader_context *)bld_base;
72}
73
74
75#define PERSPECTIVE_BASE 0
76#define LINEAR_BASE 9
77
78#define SAMPLE_OFFSET 0
79#define CENTER_OFFSET 2
80#define CENTROID_OFSET 4
81
82#define USE_SGPR_MAX_SUFFIX_LEN 5
Tom Stellard467f5162012-05-16 15:15:35 -040083#define CONST_ADDR_SPACE 2
Tom Stellard89ece082012-05-29 11:36:29 -040084#define USER_SGPR_ADDR_SPACE 8
Tom Stellarda75c6162012-01-06 17:38:37 -050085
Tom Stellard467f5162012-05-16 15:15:35 -040086/**
87 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad
88 *
89 * @param offset The offset parameter specifies the number of
90 * elements to offset, not the number of bytes or dwords. An element is the
91 * the type pointed to by the base_ptr parameter (e.g. int is the element of
92 * an int* pointer)
93 *
94 * When LLVM lowers the load instruction, it will convert the element offset
95 * into a dword offset automatically.
96 *
97 */
98static LLVMValueRef build_indexed_load(
Christian König206f0592013-03-20 14:37:21 +010099 struct si_shader_context * si_shader_ctx,
Tom Stellard467f5162012-05-16 15:15:35 -0400100 LLVMValueRef base_ptr,
101 LLVMValueRef offset)
102{
Christian König206f0592013-03-20 14:37:21 +0100103 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
Tom Stellard467f5162012-05-16 15:15:35 -0400104
Christian König206f0592013-03-20 14:37:21 +0100105 LLVMValueRef computed_ptr = LLVMBuildGEP(
106 base->gallivm->builder, base_ptr, &offset, 1, "");
107
108 LLVMValueRef result = LLVMBuildLoad(base->gallivm->builder, computed_ptr, "");
109 LLVMSetMetadata(result, 1, si_shader_ctx->const_md);
110 return result;
Tom Stellard467f5162012-05-16 15:15:35 -0400111}
112
Christian Königa0dca442013-03-22 15:59:22 +0100113static LLVMValueRef get_instance_index(
114 struct radeon_llvm_context * radeon_bld,
115 unsigned divisor)
116{
117 struct gallivm_state * gallivm = radeon_bld->soa.bld_base.base.gallivm;
118
119 LLVMValueRef result = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_INSTANCE_ID);
120 result = LLVMBuildAdd(gallivm->builder, result, LLVMGetParam(
121 radeon_bld->main_fn, SI_PARAM_START_INSTANCE), "");
122
123 if (divisor > 1)
124 result = LLVMBuildUDiv(gallivm->builder, result,
125 lp_build_const_int32(gallivm, divisor), "");
126
127 return result;
128}
129
Tom Stellarda75c6162012-01-06 17:38:37 -0500130static void declare_input_vs(
131 struct si_shader_context * si_shader_ctx,
132 unsigned input_index,
133 const struct tgsi_full_declaration *decl)
134{
Christian Königa0dca442013-03-22 15:59:22 +0100135 struct lp_build_context * base = &si_shader_ctx->radeon_bld.soa.bld_base.base;
136 unsigned divisor = si_shader_ctx->shader->key.vs.instance_divisors[input_index];
137
138 unsigned chan;
139
Tom Stellarda75c6162012-01-06 17:38:37 -0500140 LLVMValueRef t_list_ptr;
141 LLVMValueRef t_offset;
Tom Stellard467f5162012-05-16 15:15:35 -0400142 LLVMValueRef t_list;
Tom Stellarda75c6162012-01-06 17:38:37 -0500143 LLVMValueRef attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100144 LLVMValueRef buffer_index;
Tom Stellard467f5162012-05-16 15:15:35 -0400145 LLVMValueRef args[3];
Tom Stellarda75c6162012-01-06 17:38:37 -0500146 LLVMTypeRef vec4_type;
147 LLVMValueRef input;
Tom Stellarda75c6162012-01-06 17:38:37 -0500148
Tom Stellard467f5162012-05-16 15:15:35 -0400149 /* Load the T list */
Christian König55fe5cc2013-03-04 16:30:06 +0100150 t_list_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_BUFFER);
Tom Stellarda75c6162012-01-06 17:38:37 -0500151
Christian Königb15e3ae2012-07-25 11:22:59 +0200152 t_offset = lp_build_const_int32(base->gallivm, input_index);
Tom Stellard467f5162012-05-16 15:15:35 -0400153
Christian König206f0592013-03-20 14:37:21 +0100154 t_list = build_indexed_load(si_shader_ctx, t_list_ptr, t_offset);
Tom Stellard467f5162012-05-16 15:15:35 -0400155
156 /* Build the attribute offset */
Christian Königb15e3ae2012-07-25 11:22:59 +0200157 attribute_offset = lp_build_const_int32(base->gallivm, 0);
Tom Stellarda75c6162012-01-06 17:38:37 -0500158
Christian Königa0dca442013-03-22 15:59:22 +0100159 if (divisor) {
160 /* Build index from instance ID, start instance and divisor */
161 si_shader_ctx->shader->shader.uses_instanceid = true;
162 buffer_index = get_instance_index(&si_shader_ctx->radeon_bld, divisor);
163 } else {
164 /* Load the buffer index, which is always stored in VGPR0
165 * for Vertex Shaders */
166 buffer_index = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_VERTEX_ID);
167 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500168
169 vec4_type = LLVMVectorType(base->elem_type, 4);
Tom Stellard467f5162012-05-16 15:15:35 -0400170 args[0] = t_list;
171 args[1] = attribute_offset;
Christian Königa0dca442013-03-22 15:59:22 +0100172 args[2] = buffer_index;
Christian König44e32242013-03-20 12:10:35 +0100173 input = build_intrinsic(base->gallivm->builder,
174 "llvm.SI.vs.load.input", vec4_type, args, 3,
175 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500176
177 /* Break up the vec4 into individual components */
178 for (chan = 0; chan < 4; chan++) {
179 LLVMValueRef llvm_chan = lp_build_const_int32(base->gallivm, chan);
180 /* XXX: Use a helper function for this. There is one in
181 * tgsi_llvm.c. */
182 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, chan)] =
183 LLVMBuildExtractElement(base->gallivm->builder,
184 input, llvm_chan, "");
185 }
186}
187
188static void declare_input_fs(
189 struct si_shader_context * si_shader_ctx,
190 unsigned input_index,
191 const struct tgsi_full_declaration *decl)
192{
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200193 struct si_shader *shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500194 struct lp_build_context * base =
195 &si_shader_ctx->radeon_bld.soa.bld_base.base;
196 struct gallivm_state * gallivm = base->gallivm;
Tom Stellard0fb1e682012-09-06 16:18:11 -0400197 LLVMTypeRef input_type = LLVMFloatTypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +0100198 LLVMValueRef main_fn = si_shader_ctx->radeon_bld.main_fn;
199
200 LLVMValueRef interp_param;
201 const char * intr_name;
Tom Stellarda75c6162012-01-06 17:38:37 -0500202
203 /* This value is:
204 * [15:0] NewPrimMask (Bit mask for each quad. It is set it the
205 * quad begins a new primitive. Bit 0 always needs
206 * to be unset)
207 * [32:16] ParamOffset
208 *
209 */
Christian König55fe5cc2013-03-04 16:30:06 +0100210 LLVMValueRef params = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_PRIM_MASK);
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200211 LLVMValueRef attr_number;
Tom Stellarda75c6162012-01-06 17:38:37 -0500212
Christian König0666ffd2013-03-05 15:07:39 +0100213 unsigned chan;
214
Tom Stellard0fb1e682012-09-06 16:18:11 -0400215 if (decl->Semantic.Name == TGSI_SEMANTIC_POSITION) {
216 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Tom Stellard0fb1e682012-09-06 16:18:11 -0400217 unsigned soa_index =
218 radeon_llvm_reg_index_soa(input_index, chan);
Tom Stellard0fb1e682012-09-06 16:18:11 -0400219 si_shader_ctx->radeon_bld.inputs[soa_index] =
Christian König0666ffd2013-03-05 15:07:39 +0100220 LLVMGetParam(main_fn, SI_PARAM_POS_X_FLOAT + chan);
Michel Dänzer954bc4a2013-02-13 15:57:23 +0100221
222 if (chan == 3)
223 /* RCP for fragcoord.w */
224 si_shader_ctx->radeon_bld.inputs[soa_index] =
225 LLVMBuildFDiv(gallivm->builder,
226 lp_build_const_float(gallivm, 1.0f),
227 si_shader_ctx->radeon_bld.inputs[soa_index],
228 "");
Tom Stellard0fb1e682012-09-06 16:18:11 -0400229 }
230 return;
231 }
232
Michel Dänzer97078b12012-09-25 12:41:31 +0200233 if (decl->Semantic.Name == TGSI_SEMANTIC_FACE) {
234 LLVMValueRef face, is_face_positive;
235
Christian König0666ffd2013-03-05 15:07:39 +0100236 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
237
Michel Dänzer97078b12012-09-25 12:41:31 +0200238 is_face_positive = LLVMBuildFCmp(gallivm->builder,
239 LLVMRealUGT, face,
240 lp_build_const_float(gallivm, 0.0f),
241 "");
242
243 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 0)] =
244 LLVMBuildSelect(gallivm->builder,
245 is_face_positive,
246 lp_build_const_float(gallivm, 1.0f),
247 lp_build_const_float(gallivm, 0.0f),
248 "");
249 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 1)] =
250 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 2)] =
251 lp_build_const_float(gallivm, 0.0f);
252 si_shader_ctx->radeon_bld.inputs[radeon_llvm_reg_index_soa(input_index, 3)] =
253 lp_build_const_float(gallivm, 1.0f);
254
255 return;
256 }
257
Michel Dänzerc3db19e2012-09-27 20:01:33 +0200258 shader->input[input_index].param_offset = shader->ninterp++;
259 attr_number = lp_build_const_int32(gallivm,
260 shader->input[input_index].param_offset);
261
Tom Stellarda75c6162012-01-06 17:38:37 -0500262 /* XXX: Handle all possible interpolation modes */
Francisco Jerez12799232012-04-30 18:27:52 +0200263 switch (decl->Interp.Interpolate) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500264 case TGSI_INTERPOLATE_COLOR:
Christian Königa0dca442013-03-22 15:59:22 +0100265 if (si_shader_ctx->shader->key.ps.flatshade) {
Christian König0666ffd2013-03-05 15:07:39 +0100266 interp_param = 0;
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200267 } else {
268 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100269 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200270 else
Christian König0666ffd2013-03-05 15:07:39 +0100271 interp_param = LLVMGetParam(main_fn, SI_PARAM_PERSP_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200272 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500273 break;
274 case TGSI_INTERPOLATE_CONSTANT:
Christian König0666ffd2013-03-05 15:07:39 +0100275 interp_param = 0;
Tom Stellarda75c6162012-01-06 17:38:37 -0500276 break;
277 case TGSI_INTERPOLATE_LINEAR:
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200278 if (decl->Interp.Centroid)
Christian König0666ffd2013-03-05 15:07:39 +0100279 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTROID);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200280 else
Christian König0666ffd2013-03-05 15:07:39 +0100281 interp_param = LLVMGetParam(main_fn, SI_PARAM_LINEAR_CENTER);
Michel Dänzer1deb2be2012-05-14 16:26:19 +0200282 break;
283 case TGSI_INTERPOLATE_PERSPECTIVE:
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);
Tom Stellarda75c6162012-01-06 17:38:37 -0500288 break;
289 default:
290 fprintf(stderr, "Warning: Unhandled interpolation mode.\n");
291 return;
292 }
293
Christian König0666ffd2013-03-05 15:07:39 +0100294 intr_name = interp_param ? "llvm.SI.fs.interp" : "llvm.SI.fs.constant";
295
Tom Stellarda75c6162012-01-06 17:38:37 -0500296 /* XXX: Could there be more than TGSI_NUM_CHANNELS (4) ? */
Michel Dänzer691f08d2012-09-06 18:03:38 +0200297 if (decl->Semantic.Name == TGSI_SEMANTIC_COLOR &&
Christian Königa0dca442013-03-22 15:59:22 +0100298 si_shader_ctx->shader->key.ps.color_two_side) {
Christian König0666ffd2013-03-05 15:07:39 +0100299 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200300 LLVMValueRef face, is_face_positive;
301 LLVMValueRef back_attr_number =
302 lp_build_const_int32(gallivm,
303 shader->input[input_index].param_offset + 1);
304
Christian König0666ffd2013-03-05 15:07:39 +0100305 face = LLVMGetParam(main_fn, SI_PARAM_FRONT_FACE);
306
Michel Dänzer691f08d2012-09-06 18:03:38 +0200307 is_face_positive = LLVMBuildFCmp(gallivm->builder,
308 LLVMRealUGT, face,
309 lp_build_const_float(gallivm, 0.0f),
310 "");
311
Tom Stellarda75c6162012-01-06 17:38:37 -0500312 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100313 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200314 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
315 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
316 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
317 LLVMValueRef front, back;
318
319 args[0] = llvm_chan;
320 args[1] = attr_number;
321 front = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100322 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100323 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200324
325 args[1] = back_attr_number;
326 back = build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100327 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100328 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200329
330 si_shader_ctx->radeon_bld.inputs[soa_index] =
331 LLVMBuildSelect(gallivm->builder,
332 is_face_positive,
333 front,
334 back,
335 "");
336 }
337
338 shader->ninterp++;
339 } else {
340 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
Christian König0666ffd2013-03-05 15:07:39 +0100341 LLVMValueRef args[4];
Michel Dänzer691f08d2012-09-06 18:03:38 +0200342 LLVMValueRef llvm_chan = lp_build_const_int32(gallivm, chan);
343 unsigned soa_index = radeon_llvm_reg_index_soa(input_index, chan);
344 args[0] = llvm_chan;
345 args[1] = attr_number;
346 args[2] = params;
Christian König0666ffd2013-03-05 15:07:39 +0100347 args[3] = interp_param;
Michel Dänzer691f08d2012-09-06 18:03:38 +0200348 si_shader_ctx->radeon_bld.inputs[soa_index] =
349 build_intrinsic(base->gallivm->builder, intr_name,
Christian König0666ffd2013-03-05 15:07:39 +0100350 input_type, args, args[3] ? 4 : 3,
Christian König44e32242013-03-20 12:10:35 +0100351 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer691f08d2012-09-06 18:03:38 +0200352 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500353 }
354}
355
356static void declare_input(
357 struct radeon_llvm_context * radeon_bld,
358 unsigned input_index,
359 const struct tgsi_full_declaration *decl)
360{
361 struct si_shader_context * si_shader_ctx =
362 si_shader_context(&radeon_bld->soa.bld_base);
363 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
364 declare_input_vs(si_shader_ctx, input_index, decl);
365 } else if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
366 declare_input_fs(si_shader_ctx, input_index, decl);
367 } else {
368 fprintf(stderr, "Warning: Unsupported shader type,\n");
369 }
370}
371
Christian Könige4ed5872013-03-21 18:02:52 +0100372static void declare_system_value(
373 struct radeon_llvm_context * radeon_bld,
374 unsigned index,
375 const struct tgsi_full_declaration *decl)
376{
Christian Königcf9b31f2013-03-21 18:30:23 +0100377
Christian Könige4ed5872013-03-21 18:02:52 +0100378 LLVMValueRef value = 0;
379
380 switch (decl->Semantic.Name) {
381 case TGSI_SEMANTIC_INSTANCEID:
Christian Königa0dca442013-03-22 15:59:22 +0100382 value = get_instance_index(radeon_bld, 1);
Christian Könige4ed5872013-03-21 18:02:52 +0100383 break;
384
385 case TGSI_SEMANTIC_VERTEXID:
386 value = LLVMGetParam(radeon_bld->main_fn, SI_PARAM_VERTEX_ID);
387 break;
388
389 default:
390 assert(!"unknown system value");
391 return;
392 }
393
394 radeon_bld->system_values[index] = value;
395}
396
Tom Stellarda75c6162012-01-06 17:38:37 -0500397static LLVMValueRef fetch_constant(
398 struct lp_build_tgsi_context * bld_base,
399 const struct tgsi_full_src_register *reg,
400 enum tgsi_opcode_type type,
401 unsigned swizzle)
402{
Christian König55fe5cc2013-03-04 16:30:06 +0100403 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Tom Stellarda75c6162012-01-06 17:38:37 -0500404 struct lp_build_context * base = &bld_base->base;
Christian König0f6cf2b2013-03-15 15:53:25 +0100405 const struct tgsi_ind_register *ireg = &reg->Indirect;
406 unsigned idx;
Tom Stellarda75c6162012-01-06 17:38:37 -0500407
Christian Königf5298b02013-02-28 14:50:07 +0100408 LLVMValueRef args[2];
Christian König0f6cf2b2013-03-15 15:53:25 +0100409 LLVMValueRef addr;
Christian Königf5298b02013-02-28 14:50:07 +0100410 LLVMValueRef result;
Tom Stellarda75c6162012-01-06 17:38:37 -0500411
Christian König8514f5a2013-02-04 17:46:42 +0100412 if (swizzle == LP_CHAN_ALL) {
413 unsigned chan;
414 LLVMValueRef values[4];
415 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan)
416 values[chan] = fetch_constant(bld_base, reg, type, chan);
417
418 return lp_build_gather_values(bld_base->base.gallivm, values, 4);
419 }
420
Christian König0f6cf2b2013-03-15 15:53:25 +0100421 idx = reg->Register.Index * 4 + swizzle;
422 if (!reg->Register.Indirect)
423 return bitcast(bld_base, type, si_shader_ctx->constants[idx]);
Christian Königf5298b02013-02-28 14:50:07 +0100424
Christian König0f6cf2b2013-03-15 15:53:25 +0100425 args[0] = si_shader_ctx->const_resource;
426 args[1] = lp_build_const_int32(base->gallivm, idx * 4);
427 addr = si_shader_ctx->radeon_bld.soa.addr[ireg->Index][ireg->Swizzle];
428 addr = LLVMBuildLoad(base->gallivm->builder, addr, "load addr reg");
429 addr = lp_build_mul_imm(&bld_base->uint_bld, addr, 16);
430 args[1] = lp_build_add(&bld_base->uint_bld, addr, args[1]);
Christian Könige7723b52012-08-24 12:55:34 +0200431
Christian Königf5298b02013-02-28 14:50:07 +0100432 result = build_intrinsic(base->gallivm->builder, "llvm.SI.load.const", base->elem_type,
Christian König44e32242013-03-20 12:10:35 +0100433 args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Tom Stellarda75c6162012-01-06 17:38:37 -0500434
Christian Königf5298b02013-02-28 14:50:07 +0100435 return bitcast(bld_base, type, result);
Tom Stellarda75c6162012-01-06 17:38:37 -0500436}
437
Michel Dänzer26c71392012-08-24 12:03:11 +0200438/* Initialize arguments for the shader export intrinsic */
439static void si_llvm_init_export_args(struct lp_build_tgsi_context *bld_base,
440 struct tgsi_full_declaration *d,
441 unsigned index,
442 unsigned target,
443 LLVMValueRef *args)
444{
445 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
446 struct lp_build_context *uint =
447 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
448 struct lp_build_context *base = &bld_base->base;
449 unsigned compressed = 0;
450 unsigned chan;
451
Michel Dänzerf402acd2012-08-22 18:15:36 +0200452 if (si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT) {
453 int cbuf = target - V_008DFC_SQ_EXP_MRT;
454
455 if (cbuf >= 0 && cbuf < 8) {
Christian Königa0dca442013-03-22 15:59:22 +0100456 compressed = (si_shader_ctx->shader->key.ps.export_16bpc >> cbuf) & 0x1;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100457
458 if (compressed)
459 si_shader_ctx->shader->spi_shader_col_format |=
460 V_028714_SPI_SHADER_FP16_ABGR << (4 * cbuf);
461 else
462 si_shader_ctx->shader->spi_shader_col_format |=
463 V_028714_SPI_SHADER_32_ABGR << (4 * cbuf);
Michel Dänzere369f402013-04-30 16:34:10 +0200464
465 si_shader_ctx->shader->cb_shader_mask |= 0xf << (4 * cbuf);
Michel Dänzerf402acd2012-08-22 18:15:36 +0200466 }
467 }
468
469 if (compressed) {
470 /* Pixel shader needs to pack output values before export */
471 for (chan = 0; chan < 2; chan++ ) {
472 LLVMValueRef *out_ptr =
473 si_shader_ctx->radeon_bld.soa.outputs[index];
474 args[0] = LLVMBuildLoad(base->gallivm->builder,
475 out_ptr[2 * chan], "");
476 args[1] = LLVMBuildLoad(base->gallivm->builder,
477 out_ptr[2 * chan + 1], "");
478 args[chan + 5] =
479 build_intrinsic(base->gallivm->builder,
480 "llvm.SI.packf16",
481 LLVMInt32TypeInContext(base->gallivm->context),
482 args, 2,
Christian Könige4188ee2013-02-27 22:39:26 +0100483 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer8b6aec62012-11-27 19:53:58 +0100484 args[chan + 7] = args[chan + 5] =
485 LLVMBuildBitCast(base->gallivm->builder,
486 args[chan + 5],
487 LLVMFloatTypeInContext(base->gallivm->context),
488 "");
Michel Dänzerf402acd2012-08-22 18:15:36 +0200489 }
490
491 /* Set COMPR flag */
492 args[4] = uint->one;
493 } else {
494 for (chan = 0; chan < 4; chan++ ) {
495 LLVMValueRef out_ptr =
496 si_shader_ctx->radeon_bld.soa.outputs[index][chan];
497 /* +5 because the first output value will be
498 * the 6th argument to the intrinsic. */
499 args[chan + 5] = LLVMBuildLoad(base->gallivm->builder,
500 out_ptr, "");
501 }
502
503 /* Clear COMPR flag */
504 args[4] = uint->zero;
Michel Dänzer26c71392012-08-24 12:03:11 +0200505 }
506
507 /* XXX: This controls which components of the output
508 * registers actually get exported. (e.g bit 0 means export
509 * X component, bit 1 means export Y component, etc.) I'm
510 * hard coding this to 0xf for now. In the future, we might
511 * want to do something else. */
512 args[0] = lp_build_const_int32(base->gallivm, 0xf);
513
514 /* Specify whether the EXEC mask represents the valid mask */
515 args[1] = uint->zero;
516
517 /* Specify whether this is the last export */
518 args[2] = uint->zero;
519
520 /* Specify the target we are exporting */
521 args[3] = lp_build_const_int32(base->gallivm, target);
522
Michel Dänzer26c71392012-08-24 12:03:11 +0200523 /* XXX: We probably need to keep track of the output
524 * values, so we know what we are passing to the next
525 * stage. */
526}
527
Michel Dänzer7708a862012-11-02 15:57:30 +0100528static void si_alpha_test(struct lp_build_tgsi_context *bld_base,
529 unsigned index)
530{
531 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
532 struct gallivm_state *gallivm = bld_base->base.gallivm;
533
Christian Königa0dca442013-03-22 15:59:22 +0100534 if (si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_NEVER) {
Michel Dänzer7708a862012-11-02 15:57:30 +0100535 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][3];
536 LLVMValueRef alpha_pass =
537 lp_build_cmp(&bld_base->base,
Christian Königa0dca442013-03-22 15:59:22 +0100538 si_shader_ctx->shader->key.ps.alpha_func,
Michel Dänzer7708a862012-11-02 15:57:30 +0100539 LLVMBuildLoad(gallivm->builder, out_ptr, ""),
Christian Königa0dca442013-03-22 15:59:22 +0100540 lp_build_const_float(gallivm, si_shader_ctx->shader->key.ps.alpha_ref));
Michel Dänzer7708a862012-11-02 15:57:30 +0100541 LLVMValueRef arg =
542 lp_build_select(&bld_base->base,
543 alpha_pass,
544 lp_build_const_float(gallivm, 1.0f),
545 lp_build_const_float(gallivm, -1.0f));
546
547 build_intrinsic(gallivm->builder,
548 "llvm.AMDGPU.kill",
549 LLVMVoidTypeInContext(gallivm->context),
550 &arg, 1, 0);
551 } else {
552 build_intrinsic(gallivm->builder,
553 "llvm.AMDGPU.kilp",
554 LLVMVoidTypeInContext(gallivm->context),
555 NULL, 0, 0);
556 }
557}
558
Michel Dänzere3befbc2013-05-15 18:09:50 +0200559static void si_llvm_emit_clipvertex(struct lp_build_tgsi_context * bld_base,
560 unsigned index)
561{
562 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
563 struct lp_build_context *base = &bld_base->base;
564 struct lp_build_context *uint = &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
565 LLVMValueRef args[9];
566 unsigned reg_index;
567 unsigned chan;
568 unsigned const_chan;
569 LLVMValueRef out_elts[4];
570 LLVMValueRef base_elt;
571 LLVMValueRef ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
572 LLVMValueRef const_resource = build_indexed_load(si_shader_ctx, ptr, uint->one);
573
574 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
575 LLVMValueRef out_ptr = si_shader_ctx->radeon_bld.soa.outputs[index][chan];
576 out_elts[chan] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
577 }
578
579 for (reg_index = 0; reg_index < 2; reg_index ++) {
580 args[5] =
581 args[6] =
582 args[7] =
583 args[8] = lp_build_const_float(base->gallivm, 0.0f);
584
585 /* Compute dot products of position and user clip plane vectors */
586 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
587 for (const_chan = 0; const_chan < TGSI_NUM_CHANNELS; const_chan++) {
588 args[0] = const_resource;
589 args[1] = lp_build_const_int32(base->gallivm,
590 ((reg_index * 4 + chan) * 4 +
591 const_chan) * 4);
592 base_elt = build_intrinsic(base->gallivm->builder,
593 "llvm.SI.load.const",
594 base->elem_type,
595 args, 2,
596 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
597 args[5 + chan] =
598 lp_build_add(base, args[5 + chan],
599 lp_build_mul(base, base_elt,
600 out_elts[const_chan]));
601 }
602 }
603
604 args[0] = lp_build_const_int32(base->gallivm, 0xf);
605 args[1] = uint->zero;
606 args[2] = uint->zero;
607 args[3] = lp_build_const_int32(base->gallivm,
608 V_008DFC_SQ_EXP_POS + 2 + reg_index);
609 args[4] = uint->zero;
610 lp_build_intrinsic(base->gallivm->builder,
611 "llvm.SI.export",
612 LLVMVoidTypeInContext(base->gallivm->context),
613 args, 9);
614 }
615}
616
Tom Stellarda75c6162012-01-06 17:38:37 -0500617/* XXX: This is partially implemented for VS only at this point. It is not complete */
618static void si_llvm_emit_epilogue(struct lp_build_tgsi_context * bld_base)
619{
620 struct si_shader_context * si_shader_ctx = si_shader_context(bld_base);
Christian König3c09f112012-07-18 17:39:15 +0200621 struct si_shader * shader = &si_shader_ctx->shader->shader;
Tom Stellarda75c6162012-01-06 17:38:37 -0500622 struct lp_build_context * base = &bld_base->base;
623 struct lp_build_context * uint =
624 &si_shader_ctx->radeon_bld.soa.bld_base.uint_bld;
625 struct tgsi_parse_context *parse = &si_shader_ctx->parse;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100626 LLVMValueRef args[9];
Tom Stellarda75c6162012-01-06 17:38:37 -0500627 LLVMValueRef last_args[9] = { 0 };
Christian König35088152012-08-01 22:35:24 +0200628 unsigned color_count = 0;
629 unsigned param_count = 0;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100630 int depth_index = -1, stencil_index = -1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500631
632 while (!tgsi_parse_end_of_tokens(parse)) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500633 struct tgsi_full_declaration *d =
634 &parse->FullToken.FullDeclaration;
Tom Stellarda75c6162012-01-06 17:38:37 -0500635 unsigned target;
636 unsigned index;
Tom Stellarda75c6162012-01-06 17:38:37 -0500637 int i;
638
639 tgsi_parse_token(parse);
Michel Dänzerc8402702013-02-12 18:37:22 +0100640
641 if (parse->FullToken.Token.Type == TGSI_TOKEN_TYPE_PROPERTY &&
642 parse->FullToken.FullProperty.Property.PropertyName ==
643 TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS)
644 shader->fs_write_all = TRUE;
645
Tom Stellarda75c6162012-01-06 17:38:37 -0500646 if (parse->FullToken.Token.Type != TGSI_TOKEN_TYPE_DECLARATION)
647 continue;
648
649 switch (d->Declaration.File) {
650 case TGSI_FILE_INPUT:
651 i = shader->ninput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200652 assert(i < Elements(shader->input));
Tom Stellarda75c6162012-01-06 17:38:37 -0500653 shader->input[i].name = d->Semantic.Name;
654 shader->input[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200655 shader->input[i].interpolate = d->Interp.Interpolate;
656 shader->input[i].centroid = d->Interp.Centroid;
Christian König35088152012-08-01 22:35:24 +0200657 continue;
658
Tom Stellarda75c6162012-01-06 17:38:37 -0500659 case TGSI_FILE_OUTPUT:
660 i = shader->noutput++;
Marek Olšák2eac0aa2013-05-14 19:37:17 +0200661 assert(i < Elements(shader->output));
Tom Stellarda75c6162012-01-06 17:38:37 -0500662 shader->output[i].name = d->Semantic.Name;
663 shader->output[i].sid = d->Semantic.Index;
Francisco Jerez12799232012-04-30 18:27:52 +0200664 shader->output[i].interpolate = d->Interp.Interpolate;
Tom Stellarda75c6162012-01-06 17:38:37 -0500665 break;
Tom Stellarda75c6162012-01-06 17:38:37 -0500666
Christian König35088152012-08-01 22:35:24 +0200667 default:
Tom Stellarda75c6162012-01-06 17:38:37 -0500668 continue;
Christian König35088152012-08-01 22:35:24 +0200669 }
Tom Stellarda75c6162012-01-06 17:38:37 -0500670
671 for (index = d->Range.First; index <= d->Range.Last; index++) {
Tom Stellarda75c6162012-01-06 17:38:37 -0500672 /* Select the correct target */
673 switch(d->Semantic.Name) {
Tom Stellardc3c323a2012-08-30 10:35:36 -0400674 case TGSI_SEMANTIC_PSIZE:
Michel Dänzer4730dea2013-05-03 17:59:34 +0200675 shader->vs_out_misc_write = 1;
676 shader->vs_out_point_size = 1;
677 target = V_008DFC_SQ_EXP_POS + 1;
Tom Stellarda75c6162012-01-06 17:38:37 -0500678 break;
Michel Dänzer1a616c12012-11-13 17:35:09 +0100679 case TGSI_SEMANTIC_POSITION:
680 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
681 target = V_008DFC_SQ_EXP_POS;
682 break;
683 } else {
684 depth_index = index;
685 continue;
686 }
687 case TGSI_SEMANTIC_STENCIL:
688 stencil_index = index;
689 continue;
Tom Stellarda75c6162012-01-06 17:38:37 -0500690 case TGSI_SEMANTIC_COLOR:
691 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Michel Dänzer691f08d2012-09-06 18:03:38 +0200692 case TGSI_SEMANTIC_BCOLOR:
Tom Stellarda75c6162012-01-06 17:38:37 -0500693 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200694 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500695 param_count++;
696 } else {
697 target = V_008DFC_SQ_EXP_MRT + color_count;
Michel Dänzer7708a862012-11-02 15:57:30 +0100698 if (color_count == 0 &&
Christian Königa0dca442013-03-22 15:59:22 +0100699 si_shader_ctx->shader->key.ps.alpha_func != PIPE_FUNC_ALWAYS)
Michel Dänzer7708a862012-11-02 15:57:30 +0100700 si_alpha_test(bld_base, index);
701
Tom Stellarda75c6162012-01-06 17:38:37 -0500702 color_count++;
703 }
704 break;
Michel Dänzere3befbc2013-05-15 18:09:50 +0200705 case TGSI_SEMANTIC_CLIPVERTEX:
706 si_llvm_emit_clipvertex(bld_base, index);
707 shader->clip_dist_write = 0xFF;
708 continue;
Michel Dänzer30b30372012-09-06 17:53:04 +0200709 case TGSI_SEMANTIC_FOG:
Tom Stellarda75c6162012-01-06 17:38:37 -0500710 case TGSI_SEMANTIC_GENERIC:
711 target = V_008DFC_SQ_EXP_PARAM + param_count;
Michel Dänzerdd9d6192012-05-18 15:01:10 +0200712 shader->output[i].param_offset = param_count;
Tom Stellarda75c6162012-01-06 17:38:37 -0500713 param_count++;
714 break;
715 default:
716 target = 0;
717 fprintf(stderr,
718 "Warning: SI unhandled output type:%d\n",
719 d->Semantic.Name);
720 }
721
Michel Dänzer26c71392012-08-24 12:03:11 +0200722 si_llvm_init_export_args(bld_base, d, index, target, args);
Tom Stellarda75c6162012-01-06 17:38:37 -0500723
724 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX ?
725 (d->Semantic.Name == TGSI_SEMANTIC_POSITION) :
726 (d->Semantic.Name == TGSI_SEMANTIC_COLOR)) {
727 if (last_args[0]) {
728 lp_build_intrinsic(base->gallivm->builder,
729 "llvm.SI.export",
730 LLVMVoidTypeInContext(base->gallivm->context),
731 last_args, 9);
732 }
733
734 memcpy(last_args, args, sizeof(args));
735 } else {
736 lp_build_intrinsic(base->gallivm->builder,
737 "llvm.SI.export",
738 LLVMVoidTypeInContext(base->gallivm->context),
739 args, 9);
740 }
741
742 }
743 }
744
Michel Dänzer1a616c12012-11-13 17:35:09 +0100745 if (depth_index >= 0 || stencil_index >= 0) {
746 LLVMValueRef out_ptr;
747 unsigned mask = 0;
748
749 /* Specify the target we are exporting */
750 args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRTZ);
751
752 if (depth_index >= 0) {
753 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[depth_index][2];
754 args[5] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
755 mask |= 0x1;
756
757 if (stencil_index < 0) {
758 args[6] =
759 args[7] =
760 args[8] = args[5];
761 }
762 }
763
764 if (stencil_index >= 0) {
765 out_ptr = si_shader_ctx->radeon_bld.soa.outputs[stencil_index][1];
766 args[7] =
767 args[8] =
768 args[6] = LLVMBuildLoad(base->gallivm->builder, out_ptr, "");
769 mask |= 0x2;
770
771 if (depth_index < 0)
772 args[5] = args[6];
773 }
774
775 /* Specify which components to enable */
776 args[0] = lp_build_const_int32(base->gallivm, mask);
777
778 args[1] =
779 args[2] =
780 args[4] = uint->zero;
781
782 if (last_args[0])
783 lp_build_intrinsic(base->gallivm->builder,
784 "llvm.SI.export",
785 LLVMVoidTypeInContext(base->gallivm->context),
786 args, 9);
787 else
788 memcpy(last_args, args, sizeof(args));
789 }
790
Christian Königf18fd252012-07-25 21:58:46 +0200791 if (!last_args[0]) {
792 assert(si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
793
794 /* Specify which components to enable */
795 last_args[0] = lp_build_const_int32(base->gallivm, 0x0);
796
797 /* Specify the target we are exporting */
798 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
799
800 /* Set COMPR flag to zero to export data as 32-bit */
801 last_args[4] = uint->zero;
802
803 /* dummy bits */
804 last_args[5]= uint->zero;
805 last_args[6]= uint->zero;
806 last_args[7]= uint->zero;
807 last_args[8]= uint->zero;
Michel Dänzer1ace2002012-12-21 15:39:26 +0100808
809 si_shader_ctx->shader->spi_shader_col_format |=
810 V_028714_SPI_SHADER_32_ABGR;
Michel Dänzere369f402013-04-30 16:34:10 +0200811 si_shader_ctx->shader->cb_shader_mask |= S_02823C_OUTPUT0_ENABLE(0xf);
Christian Königf18fd252012-07-25 21:58:46 +0200812 }
813
Tom Stellarda75c6162012-01-06 17:38:37 -0500814 /* Specify whether the EXEC mask represents the valid mask */
815 last_args[1] = lp_build_const_int32(base->gallivm,
816 si_shader_ctx->type == TGSI_PROCESSOR_FRAGMENT);
817
Michel Dänzerc8402702013-02-12 18:37:22 +0100818 if (shader->fs_write_all && shader->nr_cbufs > 1) {
819 int i;
820
821 /* Specify that this is not yet the last export */
822 last_args[2] = lp_build_const_int32(base->gallivm, 0);
823
824 for (i = 1; i < shader->nr_cbufs; i++) {
825 /* Specify the target we are exporting */
826 last_args[3] = lp_build_const_int32(base->gallivm,
827 V_008DFC_SQ_EXP_MRT + i);
828
829 lp_build_intrinsic(base->gallivm->builder,
830 "llvm.SI.export",
831 LLVMVoidTypeInContext(base->gallivm->context),
832 last_args, 9);
833
834 si_shader_ctx->shader->spi_shader_col_format |=
835 si_shader_ctx->shader->spi_shader_col_format << 4;
Michel Dänzere369f402013-04-30 16:34:10 +0200836 si_shader_ctx->shader->cb_shader_mask |=
837 si_shader_ctx->shader->cb_shader_mask << 4;
Michel Dänzerc8402702013-02-12 18:37:22 +0100838 }
839
840 last_args[3] = lp_build_const_int32(base->gallivm, V_008DFC_SQ_EXP_MRT);
841 }
842
Tom Stellarda75c6162012-01-06 17:38:37 -0500843 /* Specify that this is the last export */
844 last_args[2] = lp_build_const_int32(base->gallivm, 1);
845
846 lp_build_intrinsic(base->gallivm->builder,
847 "llvm.SI.export",
848 LLVMVoidTypeInContext(base->gallivm->context),
849 last_args, 9);
850
851/* XXX: Look up what this function does */
852/* ctx->shader->output[i].spi_sid = r600_spi_sid(&ctx->shader->output[i]);*/
853}
854
855static void tex_fetch_args(
856 struct lp_build_tgsi_context * bld_base,
857 struct lp_build_emit_data * emit_data)
858{
Christian König55fe5cc2013-03-04 16:30:06 +0100859 struct si_shader_context *si_shader_ctx = si_shader_context(bld_base);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100860 struct gallivm_state *gallivm = bld_base->base.gallivm;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200861 const struct tgsi_full_instruction * inst = emit_data->inst;
Michel Dänzer120efee2013-01-25 12:10:11 +0100862 unsigned opcode = inst->Instruction.Opcode;
863 unsigned target = inst->Texture.Texture;
Michel Dänzer120efee2013-01-25 12:10:11 +0100864 LLVMValueRef coords[4];
865 LLVMValueRef address[16];
866 unsigned count = 0;
Michel Dänzere5fb7342013-01-24 18:54:51 +0100867 unsigned chan;
Tom Stellard467f5162012-05-16 15:15:35 -0400868
Michel Dänzer120efee2013-01-25 12:10:11 +0100869 /* Fetch and project texture coordinates */
870 coords[3] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100871 for (chan = 0; chan < 3; chan++ ) {
872 coords[chan] = lp_build_emit_fetch(bld_base,
873 emit_data->inst, 0,
874 chan);
Michel Dänzer120efee2013-01-25 12:10:11 +0100875 if (opcode == TGSI_OPCODE_TXP)
Michel Dänzerc2bae6b2012-08-02 17:19:22 +0200876 coords[chan] = lp_build_emit_llvm_binary(bld_base,
877 TGSI_OPCODE_DIV,
Michel Dänzere5fb7342013-01-24 18:54:51 +0100878 coords[chan],
879 coords[3]);
880 }
881
Michel Dänzer120efee2013-01-25 12:10:11 +0100882 if (opcode == TGSI_OPCODE_TXP)
883 coords[3] = bld_base->base.one;
Tom Stellarda75c6162012-01-06 17:38:37 -0500884
Michel Dänzer120efee2013-01-25 12:10:11 +0100885 /* Pack LOD bias value */
886 if (opcode == TGSI_OPCODE_TXB)
887 address[count++] = coords[3];
Vadim Girlin8cf552b2012-12-18 17:39:19 +0400888
Michel Dänzer120efee2013-01-25 12:10:11 +0100889 if ((target == TGSI_TEXTURE_CUBE || target == TGSI_TEXTURE_SHADOWCUBE) &&
890 opcode != TGSI_OPCODE_TXQ)
Michel Dänzere5fb7342013-01-24 18:54:51 +0100891 radeon_llvm_emit_prepare_cube_coords(bld_base, emit_data, coords);
Michel Dänzer120efee2013-01-25 12:10:11 +0100892
893 /* Pack depth comparison value */
894 switch (target) {
895 case TGSI_TEXTURE_SHADOW1D:
896 case TGSI_TEXTURE_SHADOW1D_ARRAY:
897 case TGSI_TEXTURE_SHADOW2D:
898 case TGSI_TEXTURE_SHADOWRECT:
899 address[count++] = coords[2];
900 break;
901 case TGSI_TEXTURE_SHADOWCUBE:
902 case TGSI_TEXTURE_SHADOW2D_ARRAY:
903 address[count++] = coords[3];
904 break;
905 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
906 address[count++] = lp_build_emit_fetch(bld_base, inst, 1, 0);
Michel Dänzere0f2ffc2012-12-03 12:46:30 +0100907 }
908
Michel Dänzer120efee2013-01-25 12:10:11 +0100909 /* Pack texture coordinates */
910 address[count++] = coords[0];
911 switch (target) {
912 case TGSI_TEXTURE_2D:
913 case TGSI_TEXTURE_2D_ARRAY:
914 case TGSI_TEXTURE_3D:
915 case TGSI_TEXTURE_CUBE:
916 case TGSI_TEXTURE_RECT:
917 case TGSI_TEXTURE_SHADOW2D:
918 case TGSI_TEXTURE_SHADOWRECT:
919 case TGSI_TEXTURE_SHADOW2D_ARRAY:
920 case TGSI_TEXTURE_SHADOWCUBE:
921 case TGSI_TEXTURE_2D_MSAA:
922 case TGSI_TEXTURE_2D_ARRAY_MSAA:
923 case TGSI_TEXTURE_CUBE_ARRAY:
924 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
925 address[count++] = coords[1];
926 }
927 switch (target) {
928 case TGSI_TEXTURE_3D:
929 case TGSI_TEXTURE_CUBE:
930 case TGSI_TEXTURE_SHADOWCUBE:
931 case TGSI_TEXTURE_CUBE_ARRAY:
932 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
933 address[count++] = coords[2];
Michel Dänzere5fb7342013-01-24 18:54:51 +0100934 }
935
Michel Dänzer120efee2013-01-25 12:10:11 +0100936 /* Pack array slice */
937 switch (target) {
938 case TGSI_TEXTURE_1D_ARRAY:
939 address[count++] = coords[1];
940 }
941 switch (target) {
942 case TGSI_TEXTURE_2D_ARRAY:
943 case TGSI_TEXTURE_2D_ARRAY_MSAA:
944 case TGSI_TEXTURE_SHADOW2D_ARRAY:
945 address[count++] = coords[2];
946 }
947 switch (target) {
948 case TGSI_TEXTURE_CUBE_ARRAY:
949 case TGSI_TEXTURE_SHADOW1D_ARRAY:
950 case TGSI_TEXTURE_SHADOWCUBE_ARRAY:
951 address[count++] = coords[3];
952 }
953
954 /* Pack LOD */
955 if (opcode == TGSI_OPCODE_TXL)
956 address[count++] = coords[3];
957
958 if (count > 16) {
959 assert(!"Cannot handle more than 16 texture address parameters");
960 count = 16;
961 }
962
963 for (chan = 0; chan < count; chan++ ) {
964 address[chan] = LLVMBuildBitCast(gallivm->builder,
965 address[chan],
966 LLVMInt32TypeInContext(gallivm->context),
967 "");
968 }
969
970 /* Pad to power of two vector */
971 while (count < util_next_power_of_two(count))
972 address[count++] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
973
Christian Königccf3e8f2013-03-26 15:09:27 +0100974 emit_data->args[0] = lp_build_gather_values(gallivm, address, count);
Michel Dänzere5fb7342013-01-24 18:54:51 +0100975
Tom Stellarda75c6162012-01-06 17:38:37 -0500976 /* Resource */
Christian Königccf3e8f2013-03-26 15:09:27 +0100977 emit_data->args[1] = si_shader_ctx->resources[emit_data->inst->Src[1].Register.Index];
Tom Stellarda75c6162012-01-06 17:38:37 -0500978
979 /* Sampler */
Christian Königccf3e8f2013-03-26 15:09:27 +0100980 emit_data->args[2] = si_shader_ctx->samplers[emit_data->inst->Src[1].Register.Index];
Tom Stellarda75c6162012-01-06 17:38:37 -0500981
982 /* Dimensions */
Christian Königccf3e8f2013-03-26 15:09:27 +0100983 emit_data->args[3] = lp_build_const_int32(bld_base->base.gallivm, target);
Tom Stellarda75c6162012-01-06 17:38:37 -0500984
Christian Königccf3e8f2013-03-26 15:09:27 +0100985 emit_data->arg_count = 4;
986
Tom Stellarda75c6162012-01-06 17:38:37 -0500987 emit_data->dst_type = LLVMVectorType(
988 LLVMFloatTypeInContext(bld_base->base.gallivm->context),
989 4);
990}
991
Michel Dänzer07eddc42013-02-06 15:43:10 +0100992static void build_tex_intrinsic(const struct lp_build_tgsi_action * action,
993 struct lp_build_tgsi_context * bld_base,
994 struct lp_build_emit_data * emit_data)
995{
996 struct lp_build_context * base = &bld_base->base;
997 char intr_name[23];
998
999 sprintf(intr_name, "%sv%ui32", action->intr_name,
Christian Königccf3e8f2013-03-26 15:09:27 +01001000 LLVMGetVectorSize(LLVMTypeOf(emit_data->args[0])));
Michel Dänzer07eddc42013-02-06 15:43:10 +01001001
Christian König44e32242013-03-20 12:10:35 +01001002 emit_data->output[emit_data->chan] = build_intrinsic(
Michel Dänzer07eddc42013-02-06 15:43:10 +01001003 base->gallivm->builder, intr_name, emit_data->dst_type,
Christian König44e32242013-03-20 12:10:35 +01001004 emit_data->args, emit_data->arg_count,
1005 LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
Michel Dänzer07eddc42013-02-06 15:43:10 +01001006}
1007
Tom Stellarda75c6162012-01-06 17:38:37 -05001008static const struct lp_build_tgsi_action tex_action = {
1009 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001010 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001011 .intr_name = "llvm.SI.sample."
Tom Stellarda75c6162012-01-06 17:38:37 -05001012};
1013
Michel Dänzer3e205132012-11-06 17:39:01 +01001014static const struct lp_build_tgsi_action txb_action = {
1015 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001016 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001017 .intr_name = "llvm.SI.sampleb."
Michel Dänzer3e205132012-11-06 17:39:01 +01001018};
1019
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001020static const struct lp_build_tgsi_action txl_action = {
1021 .fetch_args = tex_fetch_args,
Michel Dänzer07eddc42013-02-06 15:43:10 +01001022 .emit = build_tex_intrinsic,
Michel Dänzere5fb7342013-01-24 18:54:51 +01001023 .intr_name = "llvm.SI.samplel."
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001024};
1025
Christian König206f0592013-03-20 14:37:21 +01001026static void create_meta_data(struct si_shader_context *si_shader_ctx)
1027{
1028 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
1029 LLVMValueRef args[3];
1030
1031 args[0] = LLVMMDStringInContext(gallivm->context, "const", 5);
1032 args[1] = 0;
1033 args[2] = lp_build_const_int32(gallivm, 1);
1034
1035 si_shader_ctx->const_md = LLVMMDNodeInContext(gallivm->context, args, 3);
1036}
1037
Christian König55fe5cc2013-03-04 16:30:06 +01001038static void create_function(struct si_shader_context *si_shader_ctx)
1039{
1040 struct gallivm_state *gallivm = si_shader_ctx->radeon_bld.soa.bld_base.base.gallivm;
Christian König0666ffd2013-03-05 15:07:39 +01001041 LLVMTypeRef params[20], f32, i8, i32, v2i32, v3i32;
Christian König55fe5cc2013-03-04 16:30:06 +01001042 unsigned i;
1043
Christian König55fe5cc2013-03-04 16:30:06 +01001044 i8 = LLVMInt8TypeInContext(gallivm->context);
Christian Königc4973212013-03-05 12:14:02 +01001045 i32 = LLVMInt32TypeInContext(gallivm->context);
Christian König0666ffd2013-03-05 15:07:39 +01001046 f32 = LLVMFloatTypeInContext(gallivm->context);
1047 v2i32 = LLVMVectorType(i32, 2);
1048 v3i32 = LLVMVectorType(i32, 3);
Christian Königc4973212013-03-05 12:14:02 +01001049
Christian Königf5298b02013-02-28 14:50:07 +01001050 params[SI_PARAM_CONST] = LLVMPointerType(LLVMVectorType(i8, 16), CONST_ADDR_SPACE);
1051 params[SI_PARAM_SAMPLER] = params[SI_PARAM_CONST];
Christian König55fe5cc2013-03-04 16:30:06 +01001052 params[SI_PARAM_RESOURCE] = LLVMPointerType(LLVMVectorType(i8, 32), CONST_ADDR_SPACE);
1053
Christian Königc4973212013-03-05 12:14:02 +01001054 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
Christian König55fe5cc2013-03-04 16:30:06 +01001055 params[SI_PARAM_VERTEX_BUFFER] = params[SI_PARAM_SAMPLER];
Christian Königcf9b31f2013-03-21 18:30:23 +01001056 params[SI_PARAM_START_INSTANCE] = i32;
Christian Könige4ed5872013-03-21 18:02:52 +01001057 params[SI_PARAM_VERTEX_ID] = i32;
1058 params[SI_PARAM_DUMMY_0] = i32;
1059 params[SI_PARAM_DUMMY_1] = i32;
1060 params[SI_PARAM_INSTANCE_ID] = i32;
Christian Königcf9b31f2013-03-21 18:30:23 +01001061 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 9);
Christian König0666ffd2013-03-05 15:07:39 +01001062
Christian Königc4973212013-03-05 12:14:02 +01001063 } else {
Christian König0666ffd2013-03-05 15:07:39 +01001064 params[SI_PARAM_PRIM_MASK] = i32;
1065 params[SI_PARAM_PERSP_SAMPLE] = v2i32;
1066 params[SI_PARAM_PERSP_CENTER] = v2i32;
1067 params[SI_PARAM_PERSP_CENTROID] = v2i32;
1068 params[SI_PARAM_PERSP_PULL_MODEL] = v3i32;
1069 params[SI_PARAM_LINEAR_SAMPLE] = v2i32;
1070 params[SI_PARAM_LINEAR_CENTER] = v2i32;
1071 params[SI_PARAM_LINEAR_CENTROID] = v2i32;
1072 params[SI_PARAM_LINE_STIPPLE_TEX] = f32;
1073 params[SI_PARAM_POS_X_FLOAT] = f32;
1074 params[SI_PARAM_POS_Y_FLOAT] = f32;
1075 params[SI_PARAM_POS_Z_FLOAT] = f32;
1076 params[SI_PARAM_POS_W_FLOAT] = f32;
1077 params[SI_PARAM_FRONT_FACE] = f32;
1078 params[SI_PARAM_ANCILLARY] = f32;
1079 params[SI_PARAM_SAMPLE_COVERAGE] = f32;
1080 params[SI_PARAM_POS_FIXED_PT] = f32;
1081 radeon_llvm_create_func(&si_shader_ctx->radeon_bld, params, 20);
Christian Königc4973212013-03-05 12:14:02 +01001082 }
Christian König55fe5cc2013-03-04 16:30:06 +01001083
1084 radeon_llvm_shader_type(si_shader_ctx->radeon_bld.main_fn, si_shader_ctx->type);
1085 for (i = SI_PARAM_CONST; i <= SI_PARAM_VERTEX_BUFFER; ++i) {
1086 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, i);
1087 LLVMAddAttribute(P, LLVMInRegAttribute);
1088 }
Christian Königcf9b31f2013-03-21 18:30:23 +01001089
1090 if (si_shader_ctx->type == TGSI_PROCESSOR_VERTEX) {
1091 LLVMValueRef P = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn,
1092 SI_PARAM_START_INSTANCE);
1093 LLVMAddAttribute(P, LLVMInRegAttribute);
1094 }
Christian König55fe5cc2013-03-04 16:30:06 +01001095}
Tom Stellarda75c6162012-01-06 17:38:37 -05001096
Christian König0f6cf2b2013-03-15 15:53:25 +01001097static void preload_constants(struct si_shader_context *si_shader_ctx)
1098{
1099 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1100 struct gallivm_state * gallivm = bld_base->base.gallivm;
1101 const struct tgsi_shader_info * info = bld_base->info;
1102
1103 unsigned i, num_const = info->file_max[TGSI_FILE_CONSTANT] + 1;
1104
1105 LLVMValueRef ptr;
1106
1107 if (num_const == 0)
1108 return;
1109
1110 /* Allocate space for the constant values */
1111 si_shader_ctx->constants = CALLOC(num_const * 4, sizeof(LLVMValueRef));
1112
1113 /* Load the resource descriptor */
1114 ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_CONST);
1115 si_shader_ctx->const_resource = build_indexed_load(si_shader_ctx, ptr, bld_base->uint_bld.zero);
1116
1117 /* Load the constants, we rely on the code sinking to do the rest */
1118 for (i = 0; i < num_const * 4; ++i) {
1119 LLVMValueRef args[2] = {
1120 si_shader_ctx->const_resource,
1121 lp_build_const_int32(gallivm, i * 4)
1122 };
1123 si_shader_ctx->constants[i] = build_intrinsic(gallivm->builder, "llvm.SI.load.const",
1124 bld_base->base.elem_type, args, 2, LLVMReadNoneAttribute | LLVMNoUnwindAttribute);
1125 }
1126}
1127
Christian König1c100182013-03-17 16:02:42 +01001128static void preload_samplers(struct si_shader_context *si_shader_ctx)
1129{
1130 struct lp_build_tgsi_context * bld_base = &si_shader_ctx->radeon_bld.soa.bld_base;
1131 struct gallivm_state * gallivm = bld_base->base.gallivm;
1132 const struct tgsi_shader_info * info = bld_base->info;
1133
1134 unsigned i, num_samplers = info->file_max[TGSI_FILE_SAMPLER] + 1;
1135
1136 LLVMValueRef res_ptr, samp_ptr;
1137 LLVMValueRef offset;
1138
1139 if (num_samplers == 0)
1140 return;
1141
1142 /* Allocate space for the values */
1143 si_shader_ctx->resources = CALLOC(num_samplers, sizeof(LLVMValueRef));
1144 si_shader_ctx->samplers = CALLOC(num_samplers, sizeof(LLVMValueRef));
1145
1146 res_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_RESOURCE);
1147 samp_ptr = LLVMGetParam(si_shader_ctx->radeon_bld.main_fn, SI_PARAM_SAMPLER);
1148
1149 /* Load the resources and samplers, we rely on the code sinking to do the rest */
1150 for (i = 0; i < num_samplers; ++i) {
1151
1152 /* Resource */
1153 offset = lp_build_const_int32(gallivm, i);
1154 si_shader_ctx->resources[i] = build_indexed_load(si_shader_ctx, res_ptr, offset);
1155
1156 /* Sampler */
1157 offset = lp_build_const_int32(gallivm, i);
1158 si_shader_ctx->samplers[i] = build_indexed_load(si_shader_ctx, samp_ptr, offset);
1159 }
1160}
1161
Tom Stellard302f53d2012-10-25 13:50:10 -04001162int si_compile_llvm(struct r600_context *rctx, struct si_pipe_shader *shader,
1163 LLVMModuleRef mod)
1164{
Tom Stellard302f53d2012-10-25 13:50:10 -04001165 unsigned i;
1166 uint32_t *ptr;
1167 bool dump;
Tom Stellard7782d192013-04-04 09:57:13 -07001168 struct radeon_llvm_binary binary;
Tom Stellard302f53d2012-10-25 13:50:10 -04001169
1170 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
1171
Tom Stellard7782d192013-04-04 09:57:13 -07001172 memset(&binary, 0, sizeof(binary));
1173 radeon_llvm_compile(mod, &binary,
1174 r600_get_llvm_processor_name(rctx->screen->family), dump);
Tom Stellard302f53d2012-10-25 13:50:10 -04001175 if (dump) {
1176 fprintf(stderr, "SI CODE:\n");
Tom Stellard7782d192013-04-04 09:57:13 -07001177 for (i = 0; i < binary.code_size; i+=4 ) {
1178 fprintf(stderr, "%02x%02x%02x%02x\n", binary.code[i + 3],
1179 binary.code[i + 2], binary.code[i + 1],
1180 binary.code[i]);
Tom Stellard302f53d2012-10-25 13:50:10 -04001181 }
1182 }
1183
Tom Stellardd50343d2013-04-04 16:21:06 -04001184 /* XXX: We may be able to emit some of these values directly rather than
1185 * extracting fields to be emitted later.
1186 */
1187 for (i = 0; i < binary.config_size; i+= 8) {
1188 unsigned reg = util_le32_to_cpu(*(uint32_t*)(binary.config + i));
1189 unsigned value = util_le32_to_cpu(*(uint32_t*)(binary.config + i + 4));
1190 switch (reg) {
1191 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
1192 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
1193 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
1194 case R_00B848_COMPUTE_PGM_RSRC1:
1195 shader->num_sgprs = (G_00B028_SGPRS(value) + 1) * 8;
1196 shader->num_vgprs = (G_00B028_VGPRS(value) + 1) * 4;
1197 break;
1198 case R_0286CC_SPI_PS_INPUT_ENA:
1199 shader->spi_ps_input_ena = value;
1200 break;
1201 default:
1202 fprintf(stderr, "Warning: Compiler emitted unknown "
1203 "config register: 0x%x\n", reg);
1204 break;
1205 }
1206 }
Tom Stellard302f53d2012-10-25 13:50:10 -04001207
1208 /* copy new shader */
1209 si_resource_reference(&shader->bo, NULL);
1210 shader->bo = si_resource_create_custom(rctx->context.screen, PIPE_USAGE_IMMUTABLE,
Tom Stellardd50343d2013-04-04 16:21:06 -04001211 binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001212 if (shader->bo == NULL) {
1213 return -ENOMEM;
1214 }
1215
1216 ptr = (uint32_t*)rctx->ws->buffer_map(shader->bo->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
1217 if (0 /*R600_BIG_ENDIAN*/) {
Tom Stellardd50343d2013-04-04 16:21:06 -04001218 for (i = 0; i < binary.code_size / 4; ++i) {
1219 ptr[i] = util_bswap32(*(uint32_t*)(binary.code + i*4));
Tom Stellard302f53d2012-10-25 13:50:10 -04001220 }
1221 } else {
Tom Stellardd50343d2013-04-04 16:21:06 -04001222 memcpy(ptr, binary.code, binary.code_size);
Tom Stellard302f53d2012-10-25 13:50:10 -04001223 }
1224 rctx->ws->buffer_unmap(shader->bo->cs_buf);
1225
Tom Stellard7782d192013-04-04 09:57:13 -07001226 free(binary.code);
1227 free(binary.config);
Tom Stellard302f53d2012-10-25 13:50:10 -04001228
1229 return 0;
1230}
1231
Tom Stellarda75c6162012-01-06 17:38:37 -05001232int si_pipe_shader_create(
1233 struct pipe_context *ctx,
Christian Königa0dca442013-03-22 15:59:22 +01001234 struct si_pipe_shader *shader)
Tom Stellarda75c6162012-01-06 17:38:37 -05001235{
1236 struct r600_context *rctx = (struct r600_context*)ctx;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001237 struct si_pipe_shader_selector *sel = shader->selector;
Tom Stellarda75c6162012-01-06 17:38:37 -05001238 struct si_shader_context si_shader_ctx;
1239 struct tgsi_shader_info shader_info;
1240 struct lp_build_tgsi_context * bld_base;
1241 LLVMModuleRef mod;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001242 bool dump;
Tom Stellard302f53d2012-10-25 13:50:10 -04001243 int r = 0;
Michel Dänzer4c4ef9c2012-06-07 19:30:47 +02001244
1245 dump = debug_get_bool_option("RADEON_DUMP_SHADERS", FALSE);
Tom Stellarda75c6162012-01-06 17:38:37 -05001246
Michel Dänzer82e38ac2012-09-27 16:39:26 +02001247 assert(shader->shader.noutput == 0);
1248 assert(shader->shader.ninterp == 0);
1249 assert(shader->shader.ninput == 0);
1250
Michel Dänzercfebaf92012-08-31 19:04:08 +02001251 memset(&si_shader_ctx, 0, sizeof(si_shader_ctx));
Tom Stellarda75c6162012-01-06 17:38:37 -05001252 radeon_llvm_context_init(&si_shader_ctx.radeon_bld);
1253 bld_base = &si_shader_ctx.radeon_bld.soa.bld_base;
1254
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001255 tgsi_scan_shader(sel->tokens, &shader_info);
Michel Dänzere44dfd42012-11-07 17:33:08 +01001256 shader->shader.uses_kill = shader_info.uses_kill;
Christian Könige4ed5872013-03-21 18:02:52 +01001257 shader->shader.uses_instanceid = shader_info.uses_instanceid;
Tom Stellarda75c6162012-01-06 17:38:37 -05001258 bld_base->info = &shader_info;
1259 bld_base->emit_fetch_funcs[TGSI_FILE_CONSTANT] = fetch_constant;
Tom Stellarda75c6162012-01-06 17:38:37 -05001260 bld_base->emit_epilogue = si_llvm_emit_epilogue;
1261
1262 bld_base->op_actions[TGSI_OPCODE_TEX] = tex_action;
Michel Dänzer3e205132012-11-06 17:39:01 +01001263 bld_base->op_actions[TGSI_OPCODE_TXB] = txb_action;
Michel Dänzer56ae9be2012-11-06 17:41:50 +01001264 bld_base->op_actions[TGSI_OPCODE_TXL] = txl_action;
Michel Dänzerc2bae6b2012-08-02 17:19:22 +02001265 bld_base->op_actions[TGSI_OPCODE_TXP] = tex_action;
Tom Stellarda75c6162012-01-06 17:38:37 -05001266
1267 si_shader_ctx.radeon_bld.load_input = declare_input;
Christian Könige4ed5872013-03-21 18:02:52 +01001268 si_shader_ctx.radeon_bld.load_system_value = declare_system_value;
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001269 si_shader_ctx.tokens = sel->tokens;
Tom Stellarda75c6162012-01-06 17:38:37 -05001270 tgsi_parse_init(&si_shader_ctx.parse, si_shader_ctx.tokens);
1271 si_shader_ctx.shader = shader;
1272 si_shader_ctx.type = si_shader_ctx.parse.FullHeader.Processor.Processor;
Tom Stellarda75c6162012-01-06 17:38:37 -05001273
Christian König206f0592013-03-20 14:37:21 +01001274 create_meta_data(&si_shader_ctx);
Christian König55fe5cc2013-03-04 16:30:06 +01001275 create_function(&si_shader_ctx);
Christian König0f6cf2b2013-03-15 15:53:25 +01001276 preload_constants(&si_shader_ctx);
Christian König1c100182013-03-17 16:02:42 +01001277 preload_samplers(&si_shader_ctx);
Christian Königb8f4ca32013-03-04 15:35:30 +01001278
Christian König835098a2012-07-17 21:28:10 +02001279 shader->shader.nr_cbufs = rctx->framebuffer.nr_cbufs;
Tom Stellarda75c6162012-01-06 17:38:37 -05001280
Tom Stellard185fc9a2012-07-12 10:40:47 -04001281 /* Dump TGSI code before doing TGSI->LLVM conversion in case the
1282 * conversion fails. */
1283 if (dump) {
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001284 tgsi_dump(sel->tokens, 0);
Tom Stellard185fc9a2012-07-12 10:40:47 -04001285 }
1286
Michel Dänzerd1e40b32012-08-23 17:10:37 +02001287 if (!lp_build_tgsi_llvm(bld_base, sel->tokens)) {
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001288 fprintf(stderr, "Failed to translate shader from TGSI to LLVM\n");
Christian König0f6cf2b2013-03-15 15:53:25 +01001289 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001290 FREE(si_shader_ctx.resources);
1291 FREE(si_shader_ctx.samplers);
Michel Dänzer82cd9c02012-08-08 15:35:42 +02001292 return -EINVAL;
1293 }
Tom Stellarda75c6162012-01-06 17:38:37 -05001294
1295 radeon_llvm_finalize_module(&si_shader_ctx.radeon_bld);
1296
1297 mod = bld_base->base.gallivm->module;
Tom Stellard302f53d2012-10-25 13:50:10 -04001298 r = si_compile_llvm(rctx, shader, mod);
Tom Stellarda75c6162012-01-06 17:38:37 -05001299
Michel Dänzer4b64fa22012-08-15 18:22:46 +02001300 radeon_llvm_dispose(&si_shader_ctx.radeon_bld);
Tom Stellarda75c6162012-01-06 17:38:37 -05001301 tgsi_parse_free(&si_shader_ctx.parse);
1302
Christian König0f6cf2b2013-03-15 15:53:25 +01001303 FREE(si_shader_ctx.constants);
Christian König1c100182013-03-17 16:02:42 +01001304 FREE(si_shader_ctx.resources);
1305 FREE(si_shader_ctx.samplers);
Tom Stellarda75c6162012-01-06 17:38:37 -05001306
Tom Stellard302f53d2012-10-25 13:50:10 -04001307 return r;
Tom Stellarda75c6162012-01-06 17:38:37 -05001308}
1309
1310void si_pipe_shader_destroy(struct pipe_context *ctx, struct si_pipe_shader *shader)
1311{
Christian Königfe412872012-07-24 18:47:19 +02001312 si_resource_reference(&shader->bo, NULL);
Tom Stellarda75c6162012-01-06 17:38:37 -05001313}