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 "shader/prog_print.h"
34 * Transform the given clause in the following way:
35 * 1. Replace it with an empty clause
36 * 2. For every instruction in the original clause, try the given
37 * transformations in order.
38 * 3. If one of the transformations returns GL_TRUE, assume that it
39 * has emitted the appropriate instruction(s) into the new clause;
40 * otherwise, copy the instruction verbatim.
42 * \note The transformation is currently not recursive; in other words,
43 * instructions emitted by transformations are not transformed.
45 * \note The transform is called 'local' because it can only look at
46 * one instruction at a time.
48 void radeonLocalTransform(
50 struct gl_program
*program
,
51 int num_transformations
,
52 struct radeon_program_transformation
* transformations
)
54 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
;