2 * Copyright (C) 2008 Nicolai Haehnle.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a 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, sublicense, 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 above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "radeon_program.h"
30 #include "radeon_compiler.h"
31 #include "shader/prog_print.h"
35 * Transform the given clause in the following way:
36 * 1. Replace it with an empty clause
37 * 2. For every instruction in the original clause, try the given
38 * transformations in order.
39 * 3. If one of the transformations returns GL_TRUE, assume that it
40 * has emitted the appropriate instruction(s) into the new clause;
41 * otherwise, copy the instruction verbatim.
43 * \note The transformation is currently not recursive; in other words,
44 * instructions emitted by transformations are not transformed.
46 * \note The transform is called 'local' because it can only look at
47 * one instruction at a time.
49 void radeonLocalTransform(
50 struct gl_program
*program
,
51 int num_transformations
,
52 struct radeon_program_transformation
* transformations
)
54 struct radeon_transform_context ctx
;
57 ctx
.Program
= program
;
58 ctx
.OldInstructions
= program
->Instructions
;
59 ctx
.OldNumInstructions
= program
->NumInstructions
;
61 program
->Instructions
= 0;
62 program
->NumInstructions
= 0;
64 for(ip
= 0; ip
< ctx
.OldNumInstructions
; ++ip
) {
65 struct prog_instruction
*instr
= ctx
.OldInstructions
+ ip
;
68 for(i
= 0; i
< num_transformations
; ++i
) {
69 struct radeon_program_transformation
* t
= transformations
+ i
;
71 if (t
->function(&ctx
, instr
, t
->userData
))
75 if (i
>= num_transformations
) {
76 struct prog_instruction
* dest
= radeonAppendInstructions(program
, 1);
77 _mesa_copy_instructions(dest
, instr
, 1);
81 _mesa_free_instructions(ctx
.OldInstructions
, ctx
.OldNumInstructions
);
85 static void scan_instructions(GLboolean
* used
, const struct prog_instruction
* insts
, GLuint count
)
88 for (i
= 0; i
< count
; i
++) {
89 const struct prog_instruction
*inst
= insts
+ i
;
90 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
93 for (k
= 0; k
< n
; k
++) {
94 if (inst
->SrcReg
[k
].File
== PROGRAM_TEMPORARY
)
95 used
[inst
->SrcReg
[k
].Index
] = GL_TRUE
;
100 GLint
radeonFindFreeTemporary(struct radeon_transform_context
*t
)
102 GLboolean used
[MAX_PROGRAM_TEMPS
];
105 _mesa_memset(used
, 0, sizeof(used
));
106 scan_instructions(used
, t
->Program
->Instructions
, t
->Program
->NumInstructions
);
107 scan_instructions(used
, t
->OldInstructions
, t
->OldNumInstructions
);
109 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++) {
119 * Append the given number of instructions to the program and return a
120 * pointer to the first new instruction.
122 struct prog_instruction
*radeonAppendInstructions(struct gl_program
*program
, int count
)
124 int oldnum
= program
->NumInstructions
;
125 _mesa_insert_instructions(program
, oldnum
, count
);
126 return program
->Instructions
+ oldnum
;
130 GLint
rc_find_free_temporary(struct radeon_compiler
* c
)
132 GLboolean used
[MAX_PROGRAM_TEMPS
];
135 memset(used
, 0, sizeof(used
));
137 for (struct rc_instruction
* rcinst
= c
->Program
.Instructions
.Next
; rcinst
!= &c
->Program
.Instructions
; rcinst
= rcinst
->Next
) {
138 const struct prog_instruction
*inst
= &rcinst
->I
;
139 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
142 for (k
= 0; k
< n
; k
++) {
143 if (inst
->SrcReg
[k
].File
== PROGRAM_TEMPORARY
)
144 used
[inst
->SrcReg
[k
].Index
] = GL_TRUE
;
148 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++) {
157 struct rc_instruction
*rc_alloc_instruction(struct radeon_compiler
* c
)
159 struct rc_instruction
* inst
= memory_pool_malloc(&c
->Pool
, sizeof(struct rc_instruction
));
164 _mesa_init_instructions(&inst
->I
, 1);
170 struct rc_instruction
*rc_insert_new_instruction(struct radeon_compiler
* c
, struct rc_instruction
* after
)
172 struct rc_instruction
* inst
= rc_alloc_instruction(c
);
175 inst
->Next
= after
->Next
;
177 inst
->Prev
->Next
= inst
;
178 inst
->Next
->Prev
= inst
;
183 void rc_remove_instruction(struct rc_instruction
* inst
)
185 inst
->Prev
->Next
= inst
->Next
;
186 inst
->Next
->Prev
= inst
->Prev
;
190 void rc_mesa_to_rc_program(struct radeon_compiler
* c
, struct gl_program
* program
)
192 struct prog_instruction
*source
;
194 for(source
= program
->Instructions
; source
->Opcode
!= OPCODE_END
; ++source
) {
195 struct rc_instruction
* dest
= rc_insert_new_instruction(c
, c
->Program
.Instructions
.Prev
);
199 c
->Program
.ShadowSamplers
= program
->ShadowSamplers
;
200 c
->Program
.InputsRead
= program
->InputsRead
;
201 c
->Program
.OutputsWritten
= program
->OutputsWritten
;
206 * Print program to stderr, default options.
208 void rc_print_program(const struct rc_program
*prog
)
212 struct rc_instruction
*inst
;
214 fprintf(stderr
, "# Radeon Compiler Program\n");
216 for(inst
= prog
->Instructions
.Next
; inst
!= &prog
->Instructions
; inst
= inst
->Next
) {
217 fprintf(stderr
, "%3d: ", linenum
);
219 /* Massive hack: We rely on the fact that the printers do not actually
220 * use the gl_program argument (last argument) in debug mode */
221 indent
= _mesa_fprint_instruction_opt(
223 indent
, PROG_PRINT_DEBUG
, 0);