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_parameter.h"
32 #include "shader/prog_print.h"
36 * Transform the given clause in the following way:
37 * 1. Replace it with an empty clause
38 * 2. For every instruction in the original clause, try the given
39 * transformations in order.
40 * 3. If one of the transformations returns GL_TRUE, assume that it
41 * has emitted the appropriate instruction(s) into the new clause;
42 * otherwise, copy the instruction verbatim.
44 * \note The transformation is currently not recursive; in other words,
45 * instructions emitted by transformations are not transformed.
47 * \note The transform is called 'local' because it can only look at
48 * one instruction at a time.
50 void radeonLocalTransform(
51 struct gl_program
*program
,
52 int num_transformations
,
53 struct radeon_program_transformation
* transformations
)
55 struct radeon_transform_context ctx
;
58 ctx
.Program
= program
;
59 ctx
.OldInstructions
= program
->Instructions
;
60 ctx
.OldNumInstructions
= program
->NumInstructions
;
62 program
->Instructions
= 0;
63 program
->NumInstructions
= 0;
65 for(ip
= 0; ip
< ctx
.OldNumInstructions
; ++ip
) {
66 struct prog_instruction
*instr
= ctx
.OldInstructions
+ ip
;
69 for(i
= 0; i
< num_transformations
; ++i
) {
70 struct radeon_program_transformation
* t
= transformations
+ i
;
72 if (t
->function(&ctx
, instr
, t
->userData
))
76 if (i
>= num_transformations
) {
77 struct prog_instruction
* dest
= radeonAppendInstructions(program
, 1);
78 _mesa_copy_instructions(dest
, instr
, 1);
82 _mesa_free_instructions(ctx
.OldInstructions
, ctx
.OldNumInstructions
);
86 static void scan_instructions(GLboolean
* used
, const struct prog_instruction
* insts
, GLuint count
)
89 for (i
= 0; i
< count
; i
++) {
90 const struct prog_instruction
*inst
= insts
+ i
;
91 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
94 for (k
= 0; k
< n
; k
++) {
95 if (inst
->SrcReg
[k
].File
== PROGRAM_TEMPORARY
)
96 used
[inst
->SrcReg
[k
].Index
] = GL_TRUE
;
101 GLint
radeonFindFreeTemporary(struct radeon_transform_context
*t
)
103 GLboolean used
[MAX_PROGRAM_TEMPS
];
106 _mesa_memset(used
, 0, sizeof(used
));
107 scan_instructions(used
, t
->Program
->Instructions
, t
->Program
->NumInstructions
);
108 scan_instructions(used
, t
->OldInstructions
, t
->OldNumInstructions
);
110 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++) {
120 * Append the given number of instructions to the program and return a
121 * pointer to the first new instruction.
123 struct prog_instruction
*radeonAppendInstructions(struct gl_program
*program
, int count
)
125 int oldnum
= program
->NumInstructions
;
126 _mesa_insert_instructions(program
, oldnum
, count
);
127 return program
->Instructions
+ oldnum
;
131 GLint
rc_find_free_temporary(struct radeon_compiler
* c
)
133 GLboolean used
[MAX_PROGRAM_TEMPS
];
136 memset(used
, 0, sizeof(used
));
138 for (struct rc_instruction
* rcinst
= c
->Program
.Instructions
.Next
; rcinst
!= &c
->Program
.Instructions
; rcinst
= rcinst
->Next
) {
139 const struct prog_instruction
*inst
= &rcinst
->I
;
140 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
143 for (k
= 0; k
< n
; k
++) {
144 if (inst
->SrcReg
[k
].File
== PROGRAM_TEMPORARY
)
145 used
[inst
->SrcReg
[k
].Index
] = GL_TRUE
;
149 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++) {
158 struct rc_instruction
*rc_alloc_instruction(struct radeon_compiler
* c
)
160 struct rc_instruction
* inst
= memory_pool_malloc(&c
->Pool
, sizeof(struct rc_instruction
));
165 _mesa_init_instructions(&inst
->I
, 1);
171 struct rc_instruction
*rc_insert_new_instruction(struct radeon_compiler
* c
, struct rc_instruction
* after
)
173 struct rc_instruction
* inst
= rc_alloc_instruction(c
);
176 inst
->Next
= after
->Next
;
178 inst
->Prev
->Next
= inst
;
179 inst
->Next
->Prev
= inst
;
184 void rc_remove_instruction(struct rc_instruction
* inst
)
186 inst
->Prev
->Next
= inst
->Next
;
187 inst
->Next
->Prev
= inst
->Prev
;
191 void rc_mesa_to_rc_program(struct radeon_compiler
* c
, struct gl_program
* program
)
193 struct prog_instruction
*source
;
196 for(source
= program
->Instructions
; source
->Opcode
!= OPCODE_END
; ++source
) {
197 struct rc_instruction
* dest
= rc_insert_new_instruction(c
, c
->Program
.Instructions
.Prev
);
201 c
->Program
.ShadowSamplers
= program
->ShadowSamplers
;
202 c
->Program
.InputsRead
= program
->InputsRead
;
203 c
->Program
.OutputsWritten
= program
->OutputsWritten
;
205 for(i
= 0; i
< program
->Parameters
->NumParameters
; ++i
) {
206 struct rc_constant constant
;
208 constant
.Type
= RC_CONSTANT_EXTERNAL
;
209 constant
.u
.External
= i
;
211 rc_constants_add(&c
->Program
.Constants
, &constant
);
217 * Print program to stderr, default options.
219 void rc_print_program(const struct rc_program
*prog
)
223 struct rc_instruction
*inst
;
225 fprintf(stderr
, "# Radeon Compiler Program\n");
227 for(inst
= prog
->Instructions
.Next
; inst
!= &prog
->Instructions
; inst
= inst
->Next
) {
228 fprintf(stderr
, "%3d: ", linenum
);
230 /* Massive hack: We rely on the fact that the printers do not actually
231 * use the gl_program argument (last argument) in debug mode */
232 indent
= _mesa_fprint_instruction_opt(
234 indent
, PROG_PRINT_DEBUG
, 0);