2 * Copyright © 2017 Gert Wollny
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #include "st_glsl_to_tgsi_temprename.h"
25 #include <tgsi/tgsi_info.h>
26 #include <tgsi/tgsi_strings.h>
27 #include <program/prog_instruction.h>
31 /* std::sort is significantly faster than qsort */
40 #include <program/prog_print.h>
41 #include <util/debug.h>
46 /* If <windows.h> is included this is defined and clashes with
47 * std::numeric_limits<>::max()
53 using std::numeric_limits
;
55 /* Without c++11 define the nullptr for forward-compatibility
56 * and better readibility */
57 #if __cplusplus < 201103L
62 /* Helper function to check whether we want to seen debugging output */
63 static inline bool is_debug_enabled ()
65 static int debug_enabled
= -1;
66 if (debug_enabled
< 0)
67 debug_enabled
= env_var_as_boolean("GLSL_TO_TGSI_RENAME_DEBUG", false);
68 return debug_enabled
> 0;
70 #define RENAME_DEBUG(X) if (is_debug_enabled()) do { X; } while (false);
72 #define RENAME_DEBUG(X)
77 enum prog_scope_type
{
78 outer_scope
, /* Outer program scope */
79 loop_body
, /* Inside a loop */
80 if_branch
, /* Inside if branch */
81 else_branch
, /* Inside else branch */
82 switch_body
, /* Inside switch statmenet */
83 switch_case_branch
, /* Inside switch case statmenet */
84 switch_default_branch
, /* Inside switch default statmenet */
90 prog_scope(prog_scope
*parent
, prog_scope_type type
, int id
,
91 int depth
, int begin
);
93 prog_scope_type
type() const;
94 prog_scope
*parent() const;
95 int nesting_depth() const;
99 int loop_break_line() const;
101 const prog_scope
*in_ifelse_scope() const;
102 const prog_scope
*in_switchcase_scope() const;
103 const prog_scope
*innermost_loop() const;
104 const prog_scope
*outermost_loop() const;
105 const prog_scope
*enclosing_conditional() const;
107 bool is_loop() const;
108 bool is_in_loop() const;
109 bool is_conditional() const;
111 bool break_is_for_switchcase() const;
112 bool contains_range_of(const prog_scope
& other
) const;
114 void set_end(int end
);
115 void set_loop_break_line(int line
);
118 prog_scope_type scope_type
;
120 int scope_nesting_depth
;
124 prog_scope
*parent_scope
;
127 /* Some storage class to encapsulate the prog_scope (de-)allocations */
128 class prog_scope_storage
{
130 prog_scope_storage(void *mem_ctx
, int n
);
131 ~prog_scope_storage();
132 prog_scope
* create(prog_scope
*p
, prog_scope_type type
, int id
,
133 int lvl
, int s_begin
);
140 class temp_comp_access
{
143 void record_read(int line
, prog_scope
*scope
);
144 void record_write(int line
, prog_scope
*scope
);
145 lifetime
get_required_lifetime();
147 void propagate_lifetime_to_dominant_write_scope();
149 prog_scope
*last_read_scope
;
150 prog_scope
*first_read_scope
;
151 prog_scope
*first_write_scope
;
156 bool keep_for_full_loop
;
162 void record_read(int line
, prog_scope
*scope
, int swizzle
);
163 void record_write(int line
, prog_scope
*scope
, int writemask
);
164 lifetime
get_required_lifetime();
166 void update_access_mask(int mask
);
168 temp_comp_access comp
[4];
170 bool needs_component_tracking
;
173 prog_scope_storage::prog_scope_storage(void *mc
, int n
):
177 storage
= ralloc_array(mem_ctx
, prog_scope
, n
);
180 prog_scope_storage::~prog_scope_storage()
182 ralloc_free(storage
);
186 prog_scope_storage::create(prog_scope
*p
, prog_scope_type type
, int id
,
187 int lvl
, int s_begin
)
189 storage
[current_slot
] = prog_scope(p
, type
, id
, lvl
, s_begin
);
190 return &storage
[current_slot
++];
193 prog_scope::prog_scope(prog_scope
*parent
, prog_scope_type type
, int id
,
194 int depth
, int scope_begin
):
197 scope_nesting_depth(depth
),
198 scope_begin(scope_begin
),
200 break_loop_line(numeric_limits
<int>::max()),
205 prog_scope_type
prog_scope::type() const
210 prog_scope
*prog_scope::parent() const
215 int prog_scope::nesting_depth() const
217 return scope_nesting_depth
;
220 bool prog_scope::is_loop() const
222 return (scope_type
== loop_body
);
225 bool prog_scope::is_in_loop() const
227 if (scope_type
== loop_body
)
231 return parent_scope
->is_in_loop();
236 const prog_scope
*prog_scope::innermost_loop() const
238 if (scope_type
== loop_body
)
242 return parent_scope
->innermost_loop();
247 const prog_scope
*prog_scope::outermost_loop() const
249 const prog_scope
*loop
= nullptr;
250 const prog_scope
*p
= this;
253 if (p
->type() == loop_body
)
261 const prog_scope
*prog_scope::enclosing_conditional() const
263 if (is_conditional())
267 return parent_scope
->enclosing_conditional();
272 bool prog_scope::contains_range_of(const prog_scope
& other
) const
274 return (begin() <= other
.begin()) && (end() >= other
.end());
277 bool prog_scope::is_conditional() const
279 return scope_type
== if_branch
||
280 scope_type
== else_branch
||
281 scope_type
== switch_case_branch
||
282 scope_type
== switch_default_branch
;
285 const prog_scope
*prog_scope::in_ifelse_scope() const
287 if (scope_type
== if_branch
||
288 scope_type
== else_branch
)
292 return parent_scope
->in_ifelse_scope();
297 const prog_scope
*prog_scope::in_switchcase_scope() const
299 if (scope_type
== switch_case_branch
||
300 scope_type
== switch_default_branch
)
304 return parent_scope
->in_switchcase_scope();
309 bool prog_scope::break_is_for_switchcase() const
311 if (scope_type
== loop_body
)
314 if (scope_type
== switch_case_branch
||
315 scope_type
== switch_default_branch
||
316 scope_type
== switch_body
)
320 return parent_scope
->break_is_for_switchcase();
325 int prog_scope::id() const
330 int prog_scope::begin() const
335 int prog_scope::end() const
340 void prog_scope::set_end(int end
)
346 void prog_scope::set_loop_break_line(int line
)
348 if (scope_type
== loop_body
) {
349 break_loop_line
= MIN2(break_loop_line
, line
);
352 parent()->set_loop_break_line(line
);
356 int prog_scope::loop_break_line() const
358 return break_loop_line
;
361 temp_access::temp_access():
363 needs_component_tracking(false)
367 void temp_access::update_access_mask(int mask
)
369 if (access_mask
&& access_mask
!= mask
)
370 needs_component_tracking
= true;
374 void temp_access::record_write(int line
, prog_scope
*scope
, int writemask
)
376 update_access_mask(writemask
);
378 if (writemask
& WRITEMASK_X
)
379 comp
[0].record_write(line
, scope
);
380 if (writemask
& WRITEMASK_Y
)
381 comp
[1].record_write(line
, scope
);
382 if (writemask
& WRITEMASK_Z
)
383 comp
[2].record_write(line
, scope
);
384 if (writemask
& WRITEMASK_W
)
385 comp
[3].record_write(line
, scope
);
388 void temp_access::record_read(int line
, prog_scope
*scope
, int swizzle
)
391 for (int idx
= 0; idx
< 4; ++idx
) {
392 int swz
= GET_SWZ(swizzle
, idx
);
393 readmask
|= (1 << swz
) & 0xF;
395 update_access_mask(readmask
);
397 if (readmask
& WRITEMASK_X
)
398 comp
[0].record_read(line
, scope
);
399 if (readmask
& WRITEMASK_Y
)
400 comp
[1].record_read(line
, scope
);
401 if (readmask
& WRITEMASK_Z
)
402 comp
[2].record_read(line
, scope
);
403 if (readmask
& WRITEMASK_W
)
404 comp
[3].record_read(line
, scope
);
407 inline static lifetime
make_lifetime(int b
, int e
)
415 lifetime
temp_access::get_required_lifetime()
417 lifetime result
= make_lifetime(-1, -1);
419 unsigned mask
= access_mask
;
421 unsigned chan
= u_bit_scan(&mask
);
422 lifetime lt
= comp
[chan
].get_required_lifetime();
425 if ((result
.begin
< 0) || (result
.begin
> lt
.begin
))
426 result
.begin
= lt
.begin
;
429 if (lt
.end
> result
.end
)
432 if (!needs_component_tracking
)
438 temp_comp_access::temp_comp_access():
439 last_read_scope(nullptr),
440 first_read_scope(nullptr),
441 first_write_scope(nullptr),
445 first_read(numeric_limits
<int>::max())
449 void temp_comp_access::record_read(int line
, prog_scope
*scope
)
451 last_read_scope
= scope
;
454 if (first_read
> line
) {
456 first_read_scope
= scope
;
460 void temp_comp_access::record_write(int line
, prog_scope
*scope
)
464 if (first_write
< 0) {
466 first_write_scope
= scope
;
470 void temp_comp_access::propagate_lifetime_to_dominant_write_scope()
472 first_write
= first_write_scope
->begin();
473 int lr
= first_write_scope
->end();
479 lifetime
temp_comp_access::get_required_lifetime()
481 bool keep_for_full_loop
= false;
483 /* This register component is not used at all, or only read,
484 * mark it as unused and ignore it when renaming.
485 * glsl_to_tgsi_visitor::renumber_registers will take care of
486 * eliminating registers that are not written to.
489 return make_lifetime(-1, -1);
491 assert(first_write_scope
);
493 /* Only written to, just make sure the register component is not
494 * reused in the range it is used to write to
496 if (!last_read_scope
)
497 return make_lifetime(first_write
, last_write
+ 1);
499 const prog_scope
*enclosing_scope_first_read
= first_read_scope
;
500 const prog_scope
*enclosing_scope_first_write
= first_write_scope
;
502 /* We read before writing in a loop
503 * hence the value must survive the loops
505 if ((first_read
<= first_write
) &&
506 first_read_scope
->is_in_loop()) {
507 keep_for_full_loop
= true;
508 enclosing_scope_first_read
= first_read_scope
->outermost_loop();
511 /* A conditional write within a nested loop must survive
512 * the outermost loop, but only if it is read outside
513 * the condition scope where we write.
515 const prog_scope
*conditional
= enclosing_scope_first_write
->enclosing_conditional();
516 if (conditional
&& conditional
->is_in_loop() &&
517 !conditional
->contains_range_of(*last_read_scope
)) {
518 keep_for_full_loop
= true;
519 enclosing_scope_first_write
= conditional
->outermost_loop();
522 /* Evaluate the scope that is shared by all: required first write scope,
523 * required first read before write scope, and last read scope.
525 const prog_scope
*enclosing_scope
= enclosing_scope_first_read
;
526 if (enclosing_scope_first_write
->contains_range_of(*enclosing_scope
))
527 enclosing_scope
= enclosing_scope_first_write
;
529 if (last_read_scope
->contains_range_of(*enclosing_scope
))
530 enclosing_scope
= last_read_scope
;
532 while (!enclosing_scope
->contains_range_of(*enclosing_scope_first_write
) ||
533 !enclosing_scope
->contains_range_of(*last_read_scope
)) {
534 enclosing_scope
= enclosing_scope
->parent();
535 assert(enclosing_scope
);
538 /* Propagate the last read scope to the target scope */
539 while (enclosing_scope
->nesting_depth() < last_read_scope
->nesting_depth()) {
540 /* If the read is in a loop and we have to move up the scope we need to
541 * extend the life time to the end of this current loop because at this
542 * point we don't know whether the component was written before
543 * un-conditionally in the same loop.
545 if (last_read_scope
->is_loop())
546 last_read
= last_read_scope
->end();
548 last_read_scope
= last_read_scope
->parent();
551 /* If the variable has to be kept for the whole loop, and we
552 * are currently in a loop, then propagate the life time.
554 if (keep_for_full_loop
&& first_write_scope
->is_loop())
555 propagate_lifetime_to_dominant_write_scope();
557 /* Propagate the first_dominant_write scope to the target scope */
558 while (enclosing_scope
->nesting_depth() < first_write_scope
->nesting_depth()) {
559 /* Propagate lifetime if there was a break in a loop and the write was
560 * after the break inside that loop. Note, that this is only needed if
561 * we move up in the scopes.
563 if (first_write_scope
->loop_break_line() < first_write
) {
564 keep_for_full_loop
= true;
565 propagate_lifetime_to_dominant_write_scope();
568 first_write_scope
= first_write_scope
->parent();
570 /* Propagte lifetime if we are now in a loop */
571 if (keep_for_full_loop
&& first_write_scope
->is_loop())
572 propagate_lifetime_to_dominant_write_scope();
575 /* The last write past the last read is dead code, but we have to
576 * ensure that the component is not reused too early, hence extend the
577 * lifetime past the last write.
579 if (last_write
>= last_read
)
580 last_read
= last_write
+ 1;
582 /* Here we are at the same scope, all is resolved */
583 return make_lifetime(first_write
, last_read
);
586 /* Helper class for sorting and searching the registers based
588 class access_record
{
595 bool operator < (const access_record
& rhs
) const {
596 return begin
< rhs
.begin
;
603 /* Function used for debugging. */
604 static void dump_instruction(int line
, prog_scope
*scope
,
605 const glsl_to_tgsi_instruction
& inst
);
608 /* Scan the program and estimate the required register life times.
609 * The array lifetimes must be pre-allocated
612 get_temp_registers_required_lifetimes(void *mem_ctx
, exec_list
*instructions
,
613 int ntemps
, struct lifetime
*lifetimes
)
619 bool is_at_end
= false;
623 /* Count scopes to allocate the needed space without the need for
626 foreach_in_list(glsl_to_tgsi_instruction
, inst
, instructions
) {
627 if (inst
->op
== TGSI_OPCODE_BGNLOOP
||
628 inst
->op
== TGSI_OPCODE_SWITCH
||
629 inst
->op
== TGSI_OPCODE_CASE
||
630 inst
->op
== TGSI_OPCODE_IF
||
631 inst
->op
== TGSI_OPCODE_UIF
||
632 inst
->op
== TGSI_OPCODE_ELSE
||
633 inst
->op
== TGSI_OPCODE_DEFAULT
)
637 prog_scope_storage
scopes(mem_ctx
, n_scopes
);
638 temp_access
*acc
= new temp_access
[ntemps
];
640 prog_scope
*cur_scope
= scopes
.create(nullptr, outer_scope
, 0, 0, line
);
642 RENAME_DEBUG(cerr
<< "========= Begin shader ============\n");
644 foreach_in_list(glsl_to_tgsi_instruction
, inst
, instructions
) {
646 assert(!"GLSL_TO_TGSI: shader has instructions past end marker");
650 RENAME_DEBUG(dump_instruction(line
, cur_scope
, *inst
));
653 case TGSI_OPCODE_BGNLOOP
: {
654 cur_scope
= scopes
.create(cur_scope
, loop_body
, loop_id
++,
655 cur_scope
->nesting_depth() + 1, line
);
658 case TGSI_OPCODE_ENDLOOP
: {
659 cur_scope
->set_end(line
);
660 cur_scope
= cur_scope
->parent();
665 case TGSI_OPCODE_UIF
: {
666 assert(num_inst_src_regs(inst
) == 1);
667 const st_src_reg
& src
= inst
->src
[0];
668 if (src
.file
== PROGRAM_TEMPORARY
)
669 acc
[src
.index
].record_read(line
, cur_scope
, src
.swizzle
);
670 cur_scope
= scopes
.create(cur_scope
, if_branch
, if_id
++,
671 cur_scope
->nesting_depth() + 1, line
+ 1);
674 case TGSI_OPCODE_ELSE
: {
675 assert(cur_scope
->type() == if_branch
);
676 cur_scope
->set_end(line
- 1);
677 cur_scope
= scopes
.create(cur_scope
->parent(), else_branch
,
678 cur_scope
->id(), cur_scope
->nesting_depth(),
682 case TGSI_OPCODE_END
: {
683 cur_scope
->set_end(line
);
687 case TGSI_OPCODE_ENDIF
: {
688 cur_scope
->set_end(line
- 1);
689 cur_scope
= cur_scope
->parent();
693 case TGSI_OPCODE_SWITCH
: {
694 assert(num_inst_src_regs(inst
) == 1);
695 const st_src_reg
& src
= inst
->src
[0];
696 prog_scope
*scope
= scopes
.create(cur_scope
, switch_body
, switch_id
++,
697 cur_scope
->nesting_depth() + 1, line
);
698 /* We record the read only for the SWITCH statement itself, like it
699 * is used by the only consumer of TGSI_OPCODE_SWITCH in tgsi_exec.c.
701 if (src
.file
== PROGRAM_TEMPORARY
)
702 acc
[src
.index
].record_read(line
, cur_scope
, src
.swizzle
);
706 case TGSI_OPCODE_ENDSWITCH
: {
707 cur_scope
->set_end(line
- 1);
708 /* Remove the case level, it might not have been
709 * closed with a break.
711 if (cur_scope
->type() != switch_body
)
712 cur_scope
= cur_scope
->parent();
714 cur_scope
= cur_scope
->parent();
718 case TGSI_OPCODE_CASE
: {
719 /* Take care of tracking the registers. */
720 prog_scope
*switch_scope
= cur_scope
->type() == switch_body
?
721 cur_scope
: cur_scope
->parent();
723 assert(num_inst_src_regs(inst
) == 1);
724 const st_src_reg
& src
= inst
->src
[0];
725 if (src
.file
== PROGRAM_TEMPORARY
)
726 acc
[src
.index
].record_read(line
, switch_scope
, src
.swizzle
);
728 /* Fall through to allocate the scope. */
730 case TGSI_OPCODE_DEFAULT
: {
731 prog_scope_type t
= inst
->op
== TGSI_OPCODE_CASE
? switch_case_branch
732 : switch_default_branch
;
733 prog_scope
*switch_scope
= (cur_scope
->type() == switch_body
) ?
734 cur_scope
: cur_scope
->parent();
735 assert(switch_scope
->type() == switch_body
);
736 prog_scope
*scope
= scopes
.create(switch_scope
, t
,
738 switch_scope
->nesting_depth() + 1,
740 /* Previous case falls through, so scope was not yet closed. */
741 if ((cur_scope
!= switch_scope
) && (cur_scope
->end() == -1))
742 cur_scope
->set_end(line
- 1);
746 case TGSI_OPCODE_BRK
: {
747 if (cur_scope
->break_is_for_switchcase()) {
748 cur_scope
->set_end(line
- 1);
750 cur_scope
->set_loop_break_line(line
);
754 case TGSI_OPCODE_CAL
:
755 case TGSI_OPCODE_RET
:
756 /* These opcodes are not supported and if a subroutine would
757 * be called in a shader, then the lifetime tracking would have
758 * to follow that call to see which registers are used there.
759 * Since this is not done, we have to bail out here and signal
760 * that no register merge will take place.
765 for (unsigned j
= 0; j
< num_inst_src_regs(inst
); j
++) {
766 const st_src_reg
& src
= inst
->src
[j
];
767 if (src
.file
== PROGRAM_TEMPORARY
)
768 acc
[src
.index
].record_read(line
, cur_scope
, src
.swizzle
);
770 for (unsigned j
= 0; j
< inst
->tex_offset_num_offset
; j
++) {
771 const st_src_reg
& src
= inst
->tex_offsets
[j
];
772 if (src
.file
== PROGRAM_TEMPORARY
)
773 acc
[src
.index
].record_read(line
, cur_scope
, src
.swizzle
);
775 for (unsigned j
= 0; j
< num_inst_dst_regs(inst
); j
++) {
776 const st_dst_reg
& dst
= inst
->dst
[j
];
777 if (dst
.file
== PROGRAM_TEMPORARY
)
778 acc
[dst
.index
].record_write(line
, cur_scope
, dst
.writemask
);
785 RENAME_DEBUG(cerr
<< "==================================\n\n");
787 /* Make sure last scope is closed, even though no
788 * TGSI_OPCODE_END was given.
790 if (cur_scope
->end() < 0)
791 cur_scope
->set_end(line
- 1);
793 RENAME_DEBUG(cerr
<< "========= lifetimes ==============\n");
794 for(int i
= 0; i
< ntemps
; ++i
) {
795 RENAME_DEBUG(cerr
<< setw(4) << i
);
796 lifetimes
[i
] = acc
[i
].get_required_lifetime();
797 RENAME_DEBUG(cerr
<< ": [" << lifetimes
[i
].begin
<< ", "
798 << lifetimes
[i
].end
<< "]\n");
800 RENAME_DEBUG(cerr
<< "==================================\n\n");
807 /* Find the next register between [start, end) that has a life time starting
808 * at or after bound by using a binary search.
809 * start points at the beginning of the search range,
810 * end points at the element past the end of the search range, and
811 * the array comprising [start, end) must be sorted in ascending order.
813 static access_record
*
814 find_next_rename(access_record
* start
, access_record
* end
, int bound
)
816 int delta
= (end
- start
);
819 int half
= delta
>> 1;
820 access_record
* middle
= start
+ half
;
822 if (bound
<= middle
->begin
) {
835 static int access_record_compare (const void *a
, const void *b
) {
836 const access_record
*aa
= static_cast<const access_record
*>(a
);
837 const access_record
*bb
= static_cast<const access_record
*>(b
);
838 return aa
->begin
< bb
->begin
? -1 : (aa
->begin
> bb
->begin
? 1 : 0);
842 /* This functions evaluates the register merges by using a binary
843 * search to find suitable merge candidates. */
844 void get_temp_registers_remapping(void *mem_ctx
, int ntemps
,
845 const struct lifetime
* lifetimes
,
846 struct rename_reg_pair
*result
)
848 access_record
*reg_access
= ralloc_array(mem_ctx
, access_record
, ntemps
);
851 for (int i
= 0; i
< ntemps
; ++i
) {
852 if (lifetimes
[i
].begin
>= 0) {
853 reg_access
[used_temps
].begin
= lifetimes
[i
].begin
;
854 reg_access
[used_temps
].end
= lifetimes
[i
].end
;
855 reg_access
[used_temps
].reg
= i
;
856 reg_access
[used_temps
].erase
= false;
862 std::sort(reg_access
, reg_access
+ used_temps
);
864 std::qsort(reg_access
, used_temps
, sizeof(access_record
), access_record_compare
);
867 access_record
*trgt
= reg_access
;
868 access_record
*reg_access_end
= reg_access
+ used_temps
;
869 access_record
*first_erase
= reg_access_end
;
870 access_record
*search_start
= trgt
+ 1;
872 while (trgt
!= reg_access_end
) {
873 access_record
*src
= find_next_rename(search_start
, reg_access_end
,
875 if (src
!= reg_access_end
) {
876 result
[src
->reg
].new_reg
= trgt
->reg
;
877 result
[src
->reg
].valid
= true;
878 trgt
->end
= src
->end
;
880 /* Since we only search forward, don't remove the renamed
881 * register just now, only mark it. */
884 if (first_erase
== reg_access_end
)
887 search_start
= src
+ 1;
889 /* Moving to the next target register it is time to remove
890 * the already merged registers from the search range */
891 if (first_erase
!= reg_access_end
) {
892 access_record
*outp
= first_erase
;
893 access_record
*inp
= first_erase
+ 1;
895 while (inp
!= reg_access_end
) {
901 reg_access_end
= outp
;
902 first_erase
= reg_access_end
;
905 search_start
= trgt
+ 1;
908 ralloc_free(reg_access
);
911 /* Code below used for debugging */
913 static const char swizzle_txt
[] = "xyzw";
915 static const char *tgsi_file_names
[PROGRAM_FILE_MAX
] = {
916 "TEMP", "ARRAY", "IN", "OUT", "STATE", "CONST",
917 "UNIFORM", "WO", "ADDR", "SAMPLER", "SV", "UNDEF",
918 "IMM", "BUF", "MEM", "IMAGE"
922 void dump_instruction(int line
, prog_scope
*scope
,
923 const glsl_to_tgsi_instruction
& inst
)
925 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info(inst
.op
);
927 int indent
= scope
->nesting_depth();
928 if ((scope
->type() == switch_case_branch
||
929 scope
->type() == switch_default_branch
) &&
930 (info
->opcode
== TGSI_OPCODE_CASE
||
931 info
->opcode
== TGSI_OPCODE_DEFAULT
))
934 if (info
->opcode
== TGSI_OPCODE_ENDIF
||
935 info
->opcode
== TGSI_OPCODE_ELSE
||
936 info
->opcode
== TGSI_OPCODE_ENDLOOP
||
937 info
->opcode
== TGSI_OPCODE_ENDSWITCH
)
940 cerr
<< setw(4) << line
<< ": ";
941 for (int i
= 0; i
< indent
; ++i
)
943 cerr
<< tgsi_get_opcode_name(info
->opcode
) << " ";
945 bool has_operators
= false;
946 for (unsigned j
= 0; j
< num_inst_dst_regs(&inst
); j
++) {
947 has_operators
= true;
951 const st_dst_reg
& dst
= inst
.dst
[j
];
952 cerr
<< tgsi_file_names
[dst
.file
];
954 if (dst
.file
== PROGRAM_ARRAY
)
955 cerr
<< "(" << dst
.array_id
<< ")";
957 cerr
<< "[" << dst
.index
<< "]";
959 if (dst
.writemask
!= TGSI_WRITEMASK_XYZW
) {
961 if (dst
.writemask
& TGSI_WRITEMASK_X
) cerr
<< "x";
962 if (dst
.writemask
& TGSI_WRITEMASK_Y
) cerr
<< "y";
963 if (dst
.writemask
& TGSI_WRITEMASK_Z
) cerr
<< "z";
964 if (dst
.writemask
& TGSI_WRITEMASK_W
) cerr
<< "w";
970 for (unsigned j
= 0; j
< num_inst_src_regs(&inst
); j
++) {
974 const st_src_reg
& src
= inst
.src
[j
];
975 cerr
<< tgsi_file_names
[src
.file
]
976 << "[" << src
.index
<< "]";
977 if (src
.swizzle
!= SWIZZLE_XYZW
) {
979 for (int idx
= 0; idx
< 4; ++idx
) {
980 int swz
= GET_SWZ(src
.swizzle
, idx
);
982 cerr
<< swizzle_txt
[swz
];
988 if (inst
.tex_offset_num_offset
> 0) {
989 cerr
<< ", TEXOFS: ";
990 for (unsigned j
= 0; j
< inst
.tex_offset_num_offset
; j
++) {
994 const st_src_reg
& src
= inst
.tex_offsets
[j
];
995 cerr
<< tgsi_file_names
[src
.file
]
996 << "[" << src
.index
<< "]";
997 if (src
.swizzle
!= SWIZZLE_XYZW
) {
999 for (int idx
= 0; idx
< 4; ++idx
) {
1000 int swz
= GET_SWZ(src
.swizzle
, idx
);
1002 cerr
<< swizzle_txt
[swz
];