gallivm: support more immediates in lp_build_tgsi_info()
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_tgsi_info.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 #include "util/u_memory.h"
30 #include "util/u_math.h"
31 #include "tgsi/tgsi_parse.h"
32 #include "tgsi/tgsi_util.h"
33 #include "tgsi/tgsi_dump.h"
34 #include "tgsi/tgsi_strings.h"
35 #include "lp_bld_debug.h"
36 #include "lp_bld_tgsi.h"
37
38
39 /**
40 * Analysis context.
41 *
42 * This is where we keep store the value of each channel of the IMM/TEMP/OUT
43 * register values, as we walk the shader.
44 */
45 struct analysis_context
46 {
47 struct lp_tgsi_info *info;
48
49 unsigned num_imms;
50 float imm[128][4];
51
52 struct lp_tgsi_channel_info temp[32][4];
53 };
54
55
56 /**
57 * Describe the specified channel of the src register.
58 */
59 static void
60 analyse_src(struct analysis_context *ctx,
61 struct lp_tgsi_channel_info *chan_info,
62 const struct tgsi_src_register *src,
63 unsigned chan)
64 {
65 chan_info->file = TGSI_FILE_NULL;
66 if (!src->Indirect && !src->Absolute && !src->Negate) {
67 unsigned swizzle = tgsi_util_get_src_register_swizzle(src, chan);
68 if (src->File == TGSI_FILE_TEMPORARY) {
69 if (src->Index < Elements(ctx->temp)) {
70 *chan_info = ctx->temp[src->Index][swizzle];
71 }
72 } else {
73 chan_info->file = src->File;
74 if (src->File == TGSI_FILE_IMMEDIATE) {
75 assert(src->Index < Elements(ctx->imm));
76 if (src->Index < Elements(ctx->imm)) {
77 chan_info->u.value = ctx->imm[src->Index][swizzle];
78 }
79 } else {
80 chan_info->u.index = src->Index;
81 chan_info->swizzle = swizzle;
82 }
83 }
84 }
85 }
86
87
88 /**
89 * Whether this register channel refers to a specific immediate value.
90 */
91 static boolean
92 is_immediate(const struct lp_tgsi_channel_info *chan_info, float value)
93 {
94 return chan_info->file == TGSI_FILE_IMMEDIATE &&
95 chan_info->u.value == value;
96 }
97
98
99 static void
100 analyse_tex(struct analysis_context *ctx,
101 const struct tgsi_full_instruction *inst,
102 enum lp_build_tex_modifier modifier)
103 {
104 struct lp_tgsi_info *info = ctx->info;
105 unsigned chan;
106
107 if (info->num_texs < Elements(info->tex)) {
108 struct lp_tgsi_texture_info *tex_info = &info->tex[info->num_texs];
109 boolean indirect = FALSE;
110 unsigned readmask = 0;
111
112 tex_info->target = inst->Texture.Texture;
113 switch (inst->Texture.Texture) {
114 case TGSI_TEXTURE_1D:
115 readmask = TGSI_WRITEMASK_X;
116 break;
117 case TGSI_TEXTURE_1D_ARRAY:
118 case TGSI_TEXTURE_2D:
119 case TGSI_TEXTURE_RECT:
120 readmask = TGSI_WRITEMASK_XY;
121 break;
122 case TGSI_TEXTURE_SHADOW1D:
123 case TGSI_TEXTURE_SHADOW1D_ARRAY:
124 case TGSI_TEXTURE_SHADOW2D:
125 case TGSI_TEXTURE_SHADOWRECT:
126 case TGSI_TEXTURE_2D_ARRAY:
127 case TGSI_TEXTURE_3D:
128 case TGSI_TEXTURE_CUBE:
129 readmask = TGSI_WRITEMASK_XYZ;
130 break;
131 case TGSI_TEXTURE_SHADOW2D_ARRAY:
132 case TGSI_TEXTURE_SHADOWCUBE:
133 readmask = TGSI_WRITEMASK_XYZW;
134 break;
135 default:
136 assert(0);
137 return;
138 }
139
140 if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV) {
141 /* We don't track explicit derivatives, although we could */
142 indirect = TRUE;
143 tex_info->unit = inst->Src[3].Register.Index;
144 } else {
145 if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED ||
146 modifier == LP_BLD_TEX_MODIFIER_LOD_BIAS ||
147 modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_LOD) {
148 readmask |= TGSI_WRITEMASK_W;
149 }
150 tex_info->unit = inst->Src[1].Register.Index;
151 }
152
153 for (chan = 0; chan < 4; ++chan) {
154 struct lp_tgsi_channel_info *chan_info = &tex_info->coord[chan];
155 if (readmask & (1 << chan)) {
156 analyse_src(ctx, chan_info, &inst->Src[0].Register, chan);
157 if (chan_info->file != TGSI_FILE_INPUT) {
158 indirect = TRUE;
159 }
160 } else {
161 memset(chan_info, 0, sizeof *chan_info);
162 }
163 }
164
165 if (indirect) {
166 info->indirect_textures = TRUE;
167 }
168
169 ++info->num_texs;
170 } else {
171 info->indirect_textures = TRUE;
172 }
173 }
174
175
176 /**
177 * Process an instruction, and update the register values accordingly.
178 */
179 static void
180 analyse_instruction(struct analysis_context *ctx,
181 struct tgsi_full_instruction *inst)
182 {
183 struct lp_tgsi_info *info = ctx->info;
184 struct lp_tgsi_channel_info (*regs)[4];
185 unsigned max_regs;
186 unsigned i;
187 unsigned index;
188 unsigned chan;
189
190 for (i = 0; i < inst->Instruction.NumDstRegs; ++i) {
191 const struct tgsi_dst_register *dst = &inst->Dst[i].Register;
192
193 /*
194 * Get the lp_tgsi_channel_info array corresponding to the destination
195 * register file.
196 */
197
198 if (dst->File == TGSI_FILE_TEMPORARY) {
199 regs = ctx->temp;
200 max_regs = Elements(ctx->temp);
201 } else if (dst->File == TGSI_FILE_OUTPUT) {
202 regs = info->output;
203 max_regs = Elements(info->output);
204 } else if (dst->File == TGSI_FILE_ADDRESS ||
205 dst->File == TGSI_FILE_PREDICATE) {
206 continue;
207 } else {
208 assert(0);
209 continue;
210 }
211
212 /*
213 * Detect direct TEX instructions
214 */
215
216 switch (inst->Instruction.Opcode) {
217 case TGSI_OPCODE_TEX:
218 analyse_tex(ctx, inst, LP_BLD_TEX_MODIFIER_NONE);
219 break;
220 case TGSI_OPCODE_TXD:
221 analyse_tex(ctx, inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV);
222 break;
223 case TGSI_OPCODE_TXB:
224 analyse_tex(ctx, inst, LP_BLD_TEX_MODIFIER_LOD_BIAS);
225 break;
226 case TGSI_OPCODE_TXL:
227 analyse_tex(ctx, inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD);
228 break;
229 case TGSI_OPCODE_TXP:
230 analyse_tex(ctx, inst, LP_BLD_TEX_MODIFIER_PROJECTED);
231 break;
232 default:
233 break;
234 }
235
236 /*
237 * Keep track of assignments and writes
238 */
239
240 if (dst->Indirect) {
241 /*
242 * It could be any register index so clear all register indices.
243 */
244
245 for (chan = 0; chan < 4; ++chan) {
246 if (dst->WriteMask & (1 << chan)) {
247 for (index = 0; index < max_regs; ++index) {
248 regs[index][chan].file = TGSI_FILE_NULL;
249 }
250 }
251 }
252 } else if (dst->Index < max_regs) {
253 /*
254 * Update this destination register value.
255 */
256
257 struct lp_tgsi_channel_info res[4];
258
259 memset(res, 0, sizeof res);
260
261 if (!inst->Instruction.Predicate &&
262 !inst->Instruction.Saturate) {
263 for (chan = 0; chan < 4; ++chan) {
264 if (dst->WriteMask & (1 << chan)) {
265 if (inst->Instruction.Opcode == TGSI_OPCODE_MOV) {
266 analyse_src(ctx, &res[chan],
267 &inst->Src[0].Register, chan);
268 } else if (inst->Instruction.Opcode == TGSI_OPCODE_MUL) {
269 /*
270 * Propagate values across 1.0 and 0.0 multiplications.
271 */
272
273 struct lp_tgsi_channel_info src0;
274 struct lp_tgsi_channel_info src1;
275
276 analyse_src(ctx, &src0, &inst->Src[0].Register, chan);
277 analyse_src(ctx, &src1, &inst->Src[1].Register, chan);
278
279 if (is_immediate(&src0, 0.0f)) {
280 res[chan] = src0;
281 } else if (is_immediate(&src1, 0.0f)) {
282 res[chan] = src1;
283 } else if (is_immediate(&src0, 1.0f)) {
284 res[chan] = src1;
285 } else if (is_immediate(&src1, 1.0f)) {
286 res[chan] = src0;
287 }
288 }
289 }
290 }
291 }
292
293 for (chan = 0; chan < 4; ++chan) {
294 if (dst->WriteMask & (1 << chan)) {
295 regs[dst->Index][chan] = res[chan];
296 }
297 }
298 }
299 }
300
301 /*
302 * Clear all temporaries information in presence of a control flow opcode.
303 */
304
305 switch (inst->Instruction.Opcode) {
306 case TGSI_OPCODE_IF:
307 case TGSI_OPCODE_IFC:
308 case TGSI_OPCODE_ELSE:
309 case TGSI_OPCODE_ENDIF:
310 case TGSI_OPCODE_BGNLOOP:
311 case TGSI_OPCODE_BRK:
312 case TGSI_OPCODE_BREAKC:
313 case TGSI_OPCODE_CONT:
314 case TGSI_OPCODE_ENDLOOP:
315 case TGSI_OPCODE_CALLNZ:
316 case TGSI_OPCODE_CAL:
317 case TGSI_OPCODE_BGNSUB:
318 case TGSI_OPCODE_ENDSUB:
319 case TGSI_OPCODE_SWITCH:
320 case TGSI_OPCODE_CASE:
321 case TGSI_OPCODE_DEFAULT:
322 case TGSI_OPCODE_ENDSWITCH:
323 case TGSI_OPCODE_RET:
324 case TGSI_OPCODE_END:
325 /* XXX: Are there more cases? */
326 memset(&ctx->temp, 0, sizeof ctx->temp);
327 memset(&info->output, 0, sizeof info->output);
328 default:
329 break;
330 }
331 }
332
333
334 static INLINE void
335 dump_info(const struct tgsi_token *tokens,
336 struct lp_tgsi_info *info)
337 {
338 unsigned index;
339 unsigned chan;
340
341 tgsi_dump(tokens, 0);
342
343 for (index = 0; index < info->num_texs; ++index) {
344 const struct lp_tgsi_texture_info *tex_info = &info->tex[index];
345 debug_printf("TEX[%u] =", index);
346 for (chan = 0; chan < 4; ++chan) {
347 const struct lp_tgsi_channel_info *chan_info =
348 &tex_info->coord[chan];
349 if (chan_info->file != TGSI_FILE_NULL) {
350 debug_printf(" %s[%u].%c",
351 tgsi_file_names[chan_info->file],
352 chan_info->u.index,
353 "xyzw01"[chan_info->swizzle]);
354 } else {
355 debug_printf(" _");
356 }
357 }
358 debug_printf(", SAMP[%u], %s\n",
359 tex_info->unit,
360 tgsi_texture_names[tex_info->target]);
361 }
362
363 for (index = 0; index < PIPE_MAX_SHADER_OUTPUTS; ++index) {
364 for (chan = 0; chan < 4; ++chan) {
365 const struct lp_tgsi_channel_info *chan_info =
366 &info->output[index][chan];
367 if (chan_info->file != TGSI_FILE_NULL) {
368 debug_printf("OUT[%u].%c = ", index, "xyzw"[chan]);
369 if (chan_info->file == TGSI_FILE_IMMEDIATE) {
370 debug_printf("%f", chan_info->u.value);
371 } else {
372 const char *file_name;
373 switch (chan_info->file) {
374 case TGSI_FILE_CONSTANT:
375 file_name = "CONST";
376 break;
377 case TGSI_FILE_INPUT:
378 file_name = "IN";
379 break;
380 default:
381 file_name = "???";
382 break;
383 }
384 debug_printf("%s[%u].%c",
385 file_name,
386 chan_info->u.index,
387 "xyzw01"[chan_info->swizzle]);
388 }
389 debug_printf("\n");
390 }
391 }
392 }
393 }
394
395
396 /**
397 * Detect any direct relationship between the output color
398 */
399 void
400 lp_build_tgsi_info(const struct tgsi_token *tokens,
401 struct lp_tgsi_info *info)
402 {
403 struct tgsi_parse_context parse;
404 struct analysis_context ctx;
405 unsigned index;
406 unsigned chan;
407
408 memset(info, 0, sizeof *info);
409
410 tgsi_scan_shader(tokens, &info->base);
411
412 memset(&ctx, 0, sizeof ctx);
413 ctx.info = info;
414
415 tgsi_parse_init(&parse, tokens);
416
417 while (!tgsi_parse_end_of_tokens(&parse)) {
418 tgsi_parse_token(&parse);
419
420 switch (parse.FullToken.Token.Type) {
421 case TGSI_TOKEN_TYPE_DECLARATION:
422 break;
423
424 case TGSI_TOKEN_TYPE_INSTRUCTION:
425 {
426 struct tgsi_full_instruction *inst =
427 &parse.FullToken.FullInstruction;
428
429 if (inst->Instruction.Opcode == TGSI_OPCODE_END ||
430 inst->Instruction.Opcode == TGSI_OPCODE_BGNSUB) {
431 /* We reached the end of main function body. */
432 goto finished;
433 }
434
435 analyse_instruction(&ctx, inst);
436 }
437 break;
438
439 case TGSI_TOKEN_TYPE_IMMEDIATE:
440 {
441 const unsigned size =
442 parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
443 assert(size <= 4);
444 if (ctx.num_imms < Elements(ctx.imm)) {
445 for (chan = 0; chan < size; ++chan) {
446 float value = parse.FullToken.FullImmediate.u[chan].Float;
447 ctx.imm[ctx.num_imms][chan] = value;
448
449 if (value < 0.0f || value > 1.0f) {
450 info->unclamped_immediates = TRUE;
451 }
452 }
453 ++ctx.num_imms;
454 }
455 }
456 break;
457
458 case TGSI_TOKEN_TYPE_PROPERTY:
459 break;
460
461 default:
462 assert(0);
463 }
464 }
465 finished:
466
467 tgsi_parse_free(&parse);
468
469
470 /*
471 * Link the output color values.
472 */
473
474 for (index = 0; index < PIPE_MAX_COLOR_BUFS; ++index) {
475 const struct lp_tgsi_channel_info null_output[4];
476 info->cbuf[index] = null_output;
477 }
478
479 for (index = 0; index < info->base.num_outputs; ++index) {
480 unsigned semantic_name = info->base.output_semantic_name[index];
481 unsigned semantic_index = info->base.output_semantic_index[index];
482 if (semantic_name == TGSI_SEMANTIC_COLOR &&
483 semantic_index < PIPE_MAX_COLOR_BUFS) {
484 info->cbuf[semantic_index] = info->output[index];
485 }
486 }
487
488 if (gallivm_debug & GALLIVM_DEBUG_TGSI) {
489 dump_info(tokens, info);
490 }
491 }