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