1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
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.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 **************************************************************************/
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"
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.
44 struct analysis_context
46 struct lp_tgsi_info *info;
51 struct lp_tgsi_channel_info temp[32][4];
56 * Describe the specified channel of the src register.
59 analyse_src(struct analysis_context *ctx,
60 struct lp_tgsi_channel_info *chan_info,
61 const struct tgsi_src_register *src,
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];
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];
79 chan_info->u.index = src->Index;
80 chan_info->swizzle = swizzle;
88 * Whether this register channel refers to a specific immediate value.
91 is_immediate(const struct lp_tgsi_channel_info *chan_info, float value)
93 return chan_info->file == TGSI_FILE_IMMEDIATE &&
94 chan_info->u.value == value;
99 analyse_tex(struct analysis_context *ctx,
100 const struct tgsi_full_instruction *inst,
101 enum lp_build_tex_modifier modifier)
103 struct lp_tgsi_info *info = ctx->info;
106 if (info->num_texs < Elements(info->tex)) {
107 struct lp_tgsi_texture_info *tex_info = &info->tex[info->num_texs];
108 boolean indirect = FALSE;
109 unsigned readmask = 0;
111 tex_info->target = inst->Texture.Texture;
112 switch (inst->Texture.Texture) {
113 case TGSI_TEXTURE_1D:
114 readmask = TGSI_WRITEMASK_X;
116 case TGSI_TEXTURE_2D:
117 case TGSI_TEXTURE_RECT:
118 readmask = TGSI_WRITEMASK_XY;
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;
132 if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV) {
133 /* We don't track explicit derivatives, although we could */
135 tex_info->unit = inst->Src[3].Register.Index;
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;
142 tex_info->unit = inst->Src[1].Register.Index;
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) {
153 memset(chan_info, 0, sizeof *chan_info);
158 info->indirect_textures = TRUE;
163 info->indirect_textures = TRUE;
169 * Process an instruction, and update the register values accordingly.
172 analyse_instruction(struct analysis_context *ctx,
173 struct tgsi_full_instruction *inst)
175 struct lp_tgsi_info *info = ctx->info;
176 struct lp_tgsi_channel_info (*regs)[4];
182 for (i = 0; i < inst->Instruction.NumDstRegs; ++i) {
183 const struct tgsi_dst_register *dst = &inst->Dst[i].Register;
186 * Get the lp_tgsi_channel_info array corresponding to the destination
190 if (dst->File == TGSI_FILE_TEMPORARY) {
192 max_regs = Elements(ctx->temp);
193 } else if (dst->File == TGSI_FILE_OUTPUT) {
195 max_regs = Elements(info->output);
196 } else if (dst->File == TGSI_FILE_ADDRESS ||
197 dst->File == TGSI_FILE_PREDICATE) {
205 * Detect direct TEX instructions
208 switch (inst->Instruction.Opcode) {
209 case TGSI_OPCODE_TEX:
210 analyse_tex(ctx, inst, LP_BLD_TEX_MODIFIER_NONE);
212 case TGSI_OPCODE_TXD:
213 analyse_tex(ctx, inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV);
215 case TGSI_OPCODE_TXB:
216 analyse_tex(ctx, inst, LP_BLD_TEX_MODIFIER_LOD_BIAS);
218 case TGSI_OPCODE_TXL:
219 analyse_tex(ctx, inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD);
221 case TGSI_OPCODE_TXP:
222 analyse_tex(ctx, inst, LP_BLD_TEX_MODIFIER_PROJECTED);
229 * Keep track of assignments and writes
234 * It could be any register index so clear all register indices.
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;
244 } else if (dst->Index < max_regs) {
246 * Update this destination register value.
249 struct lp_tgsi_channel_info res[4];
251 memset(res, 0, sizeof res);
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) {
262 * Propagate values across 1.0 and 0.0 multiplications.
265 struct lp_tgsi_channel_info src0;
266 struct lp_tgsi_channel_info src1;
268 analyse_src(ctx, &src0, &inst->Src[0].Register, chan);
269 analyse_src(ctx, &src1, &inst->Src[1].Register, chan);
271 if (is_immediate(&src0, 0.0f)) {
273 } else if (is_immediate(&src1, 0.0f)) {
275 } else if (is_immediate(&src0, 1.0f)) {
277 } else if (is_immediate(&src1, 1.0f)) {
285 for (chan = 0; chan < 4; ++chan) {
286 if (dst->WriteMask & (1 << chan)) {
287 regs[dst->Index][chan] = res[chan];
294 * Clear all temporaries information in presence of a control flow opcode.
297 switch (inst->Instruction.Opcode) {
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);
327 dump_info(const struct tgsi_token *tokens,
328 struct lp_tgsi_info *info)
333 tgsi_dump(tokens, 0);
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],
345 "xyzw01"[chan_info->swizzle]);
350 debug_printf(", SAMP[%u], %s\n",
352 tgsi_texture_names[tex_info->target]);
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);
364 const char *file_name;
365 switch (chan_info->file) {
366 case TGSI_FILE_CONSTANT:
369 case TGSI_FILE_INPUT:
376 debug_printf("%s[%u].%c",
379 "xyzw01"[chan_info->swizzle]);
389 * Detect any direct relationship between the output color
392 lp_build_tgsi_info(const struct tgsi_token *tokens,
393 struct lp_tgsi_info *info)
395 struct tgsi_parse_context parse;
396 struct analysis_context ctx;
400 memset(info, 0, sizeof *info);
402 tgsi_scan_shader(tokens, &info->base);
404 memset(&ctx, 0, sizeof ctx);
407 tgsi_parse_init(&parse, tokens);
409 while (!tgsi_parse_end_of_tokens(&parse)) {
410 tgsi_parse_token(&parse);
412 switch (parse.FullToken.Token.Type) {
413 case TGSI_TOKEN_TYPE_DECLARATION:
416 case TGSI_TOKEN_TYPE_INSTRUCTION:
418 struct tgsi_full_instruction *inst =
419 &parse.FullToken.FullInstruction;
421 if (inst->Instruction.Opcode == TGSI_OPCODE_END ||
422 inst->Instruction.Opcode == TGSI_OPCODE_BGNSUB) {
423 /* We reached the end of main function body. */
427 analyse_instruction(&ctx, inst);
431 case TGSI_TOKEN_TYPE_IMMEDIATE:
433 const unsigned size =
434 parse.FullToken.FullImmediate.Immediate.NrTokens - 1;
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;
446 case TGSI_TOKEN_TYPE_PROPERTY:
455 tgsi_parse_free(&parse);
459 * Link the output color values.
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;
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];
476 if (gallivm_debug & GALLIVM_DEBUG_TGSI) {
477 dump_info(tokens, info);