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 "tgsi/tgsi_strings.h"
35 #include "lp_bld_debug.h"
36 #include "lp_bld_tgsi.h"
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.
45 struct analysis_context
47 struct lp_tgsi_info
*info
;
50 float imm
[LP_MAX_TGSI_IMMEDIATES
][4];
52 struct lp_tgsi_channel_info temp
[32][4];
57 * Describe the specified channel of the src register.
60 analyse_src(struct analysis_context
*ctx
,
61 struct lp_tgsi_channel_info
*chan_info
,
62 const struct tgsi_src_register
*src
,
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
];
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
];
80 chan_info
->u
.index
= src
->Index
;
81 chan_info
->swizzle
= swizzle
;
89 * Whether this register channel refers to a specific immediate value.
92 is_immediate(const struct lp_tgsi_channel_info
*chan_info
, float value
)
94 return chan_info
->file
== TGSI_FILE_IMMEDIATE
&&
95 chan_info
->u
.value
== value
;
100 * Analyse properties of tex instructions, in particular used
101 * to figure out if a texture is considered indirect.
102 * Not actually used by much except the tgsi dumping code.
105 analyse_tex(struct analysis_context
*ctx
,
106 const struct tgsi_full_instruction
*inst
,
107 enum lp_build_tex_modifier modifier
)
109 struct lp_tgsi_info
*info
= ctx
->info
;
112 if (info
->num_texs
< Elements(info
->tex
)) {
113 struct lp_tgsi_texture_info
*tex_info
= &info
->tex
[info
->num_texs
];
114 boolean indirect
= FALSE
;
115 unsigned readmask
= 0;
117 tex_info
->target
= inst
->Texture
.Texture
;
118 switch (inst
->Texture
.Texture
) {
119 case TGSI_TEXTURE_1D
:
120 readmask
= TGSI_WRITEMASK_X
;
122 case TGSI_TEXTURE_1D_ARRAY
:
123 case TGSI_TEXTURE_2D
:
124 case TGSI_TEXTURE_RECT
:
125 readmask
= TGSI_WRITEMASK_XY
;
127 case TGSI_TEXTURE_SHADOW1D
:
128 case TGSI_TEXTURE_SHADOW1D_ARRAY
:
129 case TGSI_TEXTURE_SHADOW2D
:
130 case TGSI_TEXTURE_SHADOWRECT
:
131 case TGSI_TEXTURE_2D_ARRAY
:
132 case TGSI_TEXTURE_3D
:
133 case TGSI_TEXTURE_CUBE
:
134 readmask
= TGSI_WRITEMASK_XYZ
;
136 case TGSI_TEXTURE_SHADOW2D_ARRAY
:
137 case TGSI_TEXTURE_SHADOWCUBE
:
138 readmask
= TGSI_WRITEMASK_XYZW
;
140 case TGSI_TEXTURE_CUBE_ARRAY
:
141 readmask
= TGSI_WRITEMASK_XYZW
;
143 case TGSI_TEXTURE_SHADOWCUBE_ARRAY
:
144 readmask
= TGSI_WRITEMASK_XYZW
;
151 * For cube map arrays, this will not analyze lod or shadow argument.
152 * For shadow cube, this will not analyze lod bias argument.
153 * "Indirect" really has no meaning for such textures anyway though.
156 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
) {
157 /* We don't track explicit derivatives, although we could */
159 tex_info
->sampler_unit
= inst
->Src
[3].Register
.Index
;
160 tex_info
->texture_unit
= inst
->Src
[3].Register
.Index
;
162 if (modifier
== LP_BLD_TEX_MODIFIER_PROJECTED
||
163 modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
||
164 modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
) {
165 readmask
|= TGSI_WRITEMASK_W
;
167 tex_info
->sampler_unit
= inst
->Src
[1].Register
.Index
;
168 tex_info
->texture_unit
= inst
->Src
[1].Register
.Index
;
171 for (chan
= 0; chan
< 4; ++chan
) {
172 struct lp_tgsi_channel_info
*chan_info
= &tex_info
->coord
[chan
];
173 if (readmask
& (1 << chan
)) {
174 analyse_src(ctx
, chan_info
, &inst
->Src
[0].Register
, chan
);
175 if (chan_info
->file
!= TGSI_FILE_INPUT
) {
179 memset(chan_info
, 0, sizeof *chan_info
);
184 info
->indirect_textures
= TRUE
;
189 info
->indirect_textures
= TRUE
;
195 * Analyse properties of sample instructions, in particular used
196 * to figure out if a texture is considered indirect.
197 * Not actually used by much except the tgsi dumping code.
200 analyse_sample(struct analysis_context
*ctx
,
201 const struct tgsi_full_instruction
*inst
,
202 enum lp_build_tex_modifier modifier
,
205 struct lp_tgsi_info
*info
= ctx
->info
;
208 if (info
->num_texs
< Elements(info
->tex
)) {
209 struct lp_tgsi_texture_info
*tex_info
= &info
->tex
[info
->num_texs
];
210 boolean indirect
= FALSE
;
211 boolean shadow
= FALSE
;
215 * We don't really get much information here, in particular not
216 * the target info, hence no useful writemask neither. Maybe should just
217 * forget the whole function.
219 readmask
= TGSI_WRITEMASK_XYZW
;
221 tex_info
->texture_unit
= inst
->Src
[1].Register
.Index
;
222 tex_info
->sampler_unit
= inst
->Src
[2].Register
.Index
;
224 if (modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
||
225 modifier
== LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
||
226 modifier
== LP_BLD_TEX_MODIFIER_LOD_BIAS
|| shadow
) {
227 /* We don't track insts with additional regs, although we could */
231 for (chan
= 0; chan
< 4; ++chan
) {
232 struct lp_tgsi_channel_info
*chan_info
= &tex_info
->coord
[chan
];
233 if (readmask
& (1 << chan
)) {
234 analyse_src(ctx
, chan_info
, &inst
->Src
[0].Register
, chan
);
235 if (chan_info
->file
!= TGSI_FILE_INPUT
) {
239 memset(chan_info
, 0, sizeof *chan_info
);
244 info
->indirect_textures
= TRUE
;
249 info
->indirect_textures
= TRUE
;
255 * Process an instruction, and update the register values accordingly.
258 analyse_instruction(struct analysis_context
*ctx
,
259 struct tgsi_full_instruction
*inst
)
261 struct lp_tgsi_info
*info
= ctx
->info
;
262 struct lp_tgsi_channel_info (*regs
)[4];
268 for (i
= 0; i
< inst
->Instruction
.NumDstRegs
; ++i
) {
269 const struct tgsi_dst_register
*dst
= &inst
->Dst
[i
].Register
;
272 * Get the lp_tgsi_channel_info array corresponding to the destination
276 if (dst
->File
== TGSI_FILE_TEMPORARY
) {
278 max_regs
= Elements(ctx
->temp
);
279 } else if (dst
->File
== TGSI_FILE_OUTPUT
) {
281 max_regs
= Elements(info
->output
);
282 } else if (dst
->File
== TGSI_FILE_ADDRESS
||
283 dst
->File
== TGSI_FILE_PREDICATE
) {
291 * Detect direct TEX instructions
294 switch (inst
->Instruction
.Opcode
) {
295 case TGSI_OPCODE_TEX
:
296 analyse_tex(ctx
, inst
, LP_BLD_TEX_MODIFIER_NONE
);
298 case TGSI_OPCODE_TXD
:
299 analyse_tex(ctx
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
);
301 case TGSI_OPCODE_TXB
:
302 analyse_tex(ctx
, inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
);
304 case TGSI_OPCODE_TXL
:
305 analyse_tex(ctx
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
);
307 case TGSI_OPCODE_TXP
:
308 analyse_tex(ctx
, inst
, LP_BLD_TEX_MODIFIER_PROJECTED
);
310 case TGSI_OPCODE_TEX2
:
311 analyse_tex(ctx
, inst
, LP_BLD_TEX_MODIFIER_NONE
);
313 case TGSI_OPCODE_TXB2
:
314 analyse_tex(ctx
, inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
);
316 case TGSI_OPCODE_TXL2
:
317 analyse_tex(ctx
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
);
319 case TGSI_OPCODE_SAMPLE
:
320 analyse_sample(ctx
, inst
, LP_BLD_TEX_MODIFIER_NONE
, FALSE
);
322 case TGSI_OPCODE_SAMPLE_C
:
323 analyse_sample(ctx
, inst
, LP_BLD_TEX_MODIFIER_NONE
, TRUE
);
325 case TGSI_OPCODE_SAMPLE_C_LZ
:
326 analyse_sample(ctx
, inst
, LP_BLD_TEX_MODIFIER_LOD_ZERO
, TRUE
);
328 case TGSI_OPCODE_SAMPLE_D
:
329 analyse_sample(ctx
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
, FALSE
);
331 case TGSI_OPCODE_SAMPLE_B
:
332 analyse_sample(ctx
, inst
, LP_BLD_TEX_MODIFIER_LOD_BIAS
, FALSE
);
334 case TGSI_OPCODE_SAMPLE_L
:
335 analyse_sample(ctx
, inst
, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
, FALSE
);
342 * Keep track of assignments and writes
347 * It could be any register index so clear all register indices.
350 for (chan
= 0; chan
< 4; ++chan
) {
351 if (dst
->WriteMask
& (1 << chan
)) {
352 for (index
= 0; index
< max_regs
; ++index
) {
353 regs
[index
][chan
].file
= TGSI_FILE_NULL
;
357 } else if (dst
->Index
< max_regs
) {
359 * Update this destination register value.
362 struct lp_tgsi_channel_info res
[4];
364 memset(res
, 0, sizeof res
);
366 if (!inst
->Instruction
.Predicate
&&
367 !inst
->Instruction
.Saturate
) {
368 for (chan
= 0; chan
< 4; ++chan
) {
369 if (dst
->WriteMask
& (1 << chan
)) {
370 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_MOV
) {
371 analyse_src(ctx
, &res
[chan
],
372 &inst
->Src
[0].Register
, chan
);
373 } else if (inst
->Instruction
.Opcode
== TGSI_OPCODE_MUL
) {
375 * Propagate values across 1.0 and 0.0 multiplications.
378 struct lp_tgsi_channel_info src0
;
379 struct lp_tgsi_channel_info src1
;
381 analyse_src(ctx
, &src0
, &inst
->Src
[0].Register
, chan
);
382 analyse_src(ctx
, &src1
, &inst
->Src
[1].Register
, chan
);
384 if (is_immediate(&src0
, 0.0f
)) {
386 } else if (is_immediate(&src1
, 0.0f
)) {
388 } else if (is_immediate(&src0
, 1.0f
)) {
390 } else if (is_immediate(&src1
, 1.0f
)) {
398 for (chan
= 0; chan
< 4; ++chan
) {
399 if (dst
->WriteMask
& (1 << chan
)) {
400 regs
[dst
->Index
][chan
] = res
[chan
];
407 * Clear all temporaries information in presence of a control flow opcode.
410 switch (inst
->Instruction
.Opcode
) {
412 case TGSI_OPCODE_UIF
:
413 case TGSI_OPCODE_ELSE
:
414 case TGSI_OPCODE_ENDIF
:
415 case TGSI_OPCODE_BGNLOOP
:
416 case TGSI_OPCODE_BRK
:
417 case TGSI_OPCODE_BREAKC
:
418 case TGSI_OPCODE_CONT
:
419 case TGSI_OPCODE_ENDLOOP
:
420 case TGSI_OPCODE_CALLNZ
:
421 case TGSI_OPCODE_CAL
:
422 case TGSI_OPCODE_BGNSUB
:
423 case TGSI_OPCODE_ENDSUB
:
424 case TGSI_OPCODE_SWITCH
:
425 case TGSI_OPCODE_CASE
:
426 case TGSI_OPCODE_DEFAULT
:
427 case TGSI_OPCODE_ENDSWITCH
:
428 case TGSI_OPCODE_RET
:
429 case TGSI_OPCODE_END
:
430 /* XXX: Are there more cases? */
431 memset(&ctx
->temp
, 0, sizeof ctx
->temp
);
432 memset(&info
->output
, 0, sizeof info
->output
);
440 dump_info(const struct tgsi_token
*tokens
,
441 struct lp_tgsi_info
*info
)
446 tgsi_dump(tokens
, 0);
448 for (index
= 0; index
< info
->num_texs
; ++index
) {
449 const struct lp_tgsi_texture_info
*tex_info
= &info
->tex
[index
];
450 debug_printf("TEX[%u] =", index
);
451 for (chan
= 0; chan
< 4; ++chan
) {
452 const struct lp_tgsi_channel_info
*chan_info
=
453 &tex_info
->coord
[chan
];
454 if (chan_info
->file
!= TGSI_FILE_NULL
) {
455 debug_printf(" %s[%u].%c",
456 tgsi_file_name(chan_info
->file
),
458 "xyzw01"[chan_info
->swizzle
]);
463 debug_printf(", RES[%u], SAMP[%u], %s\n",
464 tex_info
->texture_unit
,
465 tex_info
->sampler_unit
,
466 tgsi_texture_names
[tex_info
->target
]);
469 for (index
= 0; index
< PIPE_MAX_SHADER_OUTPUTS
; ++index
) {
470 for (chan
= 0; chan
< 4; ++chan
) {
471 const struct lp_tgsi_channel_info
*chan_info
=
472 &info
->output
[index
][chan
];
473 if (chan_info
->file
!= TGSI_FILE_NULL
) {
474 debug_printf("OUT[%u].%c = ", index
, "xyzw"[chan
]);
475 if (chan_info
->file
== TGSI_FILE_IMMEDIATE
) {
476 debug_printf("%f", chan_info
->u
.value
);
478 const char *file_name
;
479 switch (chan_info
->file
) {
480 case TGSI_FILE_CONSTANT
:
483 case TGSI_FILE_INPUT
:
490 debug_printf("%s[%u].%c",
493 "xyzw01"[chan_info
->swizzle
]);
503 * Detect any direct relationship between the output color
506 lp_build_tgsi_info(const struct tgsi_token
*tokens
,
507 struct lp_tgsi_info
*info
)
509 struct tgsi_parse_context parse
;
510 struct analysis_context
*ctx
;
514 memset(info
, 0, sizeof *info
);
516 tgsi_scan_shader(tokens
, &info
->base
);
518 ctx
= CALLOC(1, sizeof(struct analysis_context
));
521 tgsi_parse_init(&parse
, tokens
);
523 while (!tgsi_parse_end_of_tokens(&parse
)) {
524 tgsi_parse_token(&parse
);
526 switch (parse
.FullToken
.Token
.Type
) {
527 case TGSI_TOKEN_TYPE_DECLARATION
:
530 case TGSI_TOKEN_TYPE_INSTRUCTION
:
532 struct tgsi_full_instruction
*inst
=
533 &parse
.FullToken
.FullInstruction
;
535 if (inst
->Instruction
.Opcode
== TGSI_OPCODE_END
||
536 inst
->Instruction
.Opcode
== TGSI_OPCODE_BGNSUB
) {
537 /* We reached the end of main function body. */
541 analyse_instruction(ctx
, inst
);
545 case TGSI_TOKEN_TYPE_IMMEDIATE
:
547 const unsigned size
=
548 parse
.FullToken
.FullImmediate
.Immediate
.NrTokens
- 1;
550 if (ctx
->num_imms
< Elements(ctx
->imm
)) {
551 for (chan
= 0; chan
< size
; ++chan
) {
552 float value
= parse
.FullToken
.FullImmediate
.u
[chan
].Float
;
553 ctx
->imm
[ctx
->num_imms
][chan
] = value
;
555 if (value
< 0.0f
|| value
> 1.0f
) {
556 info
->unclamped_immediates
= TRUE
;
564 case TGSI_TOKEN_TYPE_PROPERTY
:
573 tgsi_parse_free(&parse
);
578 * Link the output color values.
581 for (index
= 0; index
< PIPE_MAX_COLOR_BUFS
; ++index
) {
582 const struct lp_tgsi_channel_info null_output
[4];
583 info
->cbuf
[index
] = null_output
;
586 for (index
= 0; index
< info
->base
.num_outputs
; ++index
) {
587 unsigned semantic_name
= info
->base
.output_semantic_name
[index
];
588 unsigned semantic_index
= info
->base
.output_semantic_index
[index
];
589 if (semantic_name
== TGSI_SEMANTIC_COLOR
&&
590 semantic_index
< PIPE_MAX_COLOR_BUFS
) {
591 info
->cbuf
[semantic_index
] = info
->output
[index
];
595 if (gallivm_debug
& GALLIVM_DEBUG_TGSI
) {
596 dump_info(tokens
, info
);