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 radeon_compiler
* c
,
52 int num_transformations
,
53 struct radeon_program_transformation
* transformations
)
55 struct rc_instruction
* inst
= c
->Program
.Instructions
.Next
;
57 while(inst
!= &c
->Program
.Instructions
) {
58 struct rc_instruction
* current
= inst
;
63 for(i
= 0; i
< num_transformations
; ++i
) {
64 struct radeon_program_transformation
* t
= transformations
+ i
;
66 if (t
->function(c
, current
, t
->userData
))
73 GLint
rc_find_free_temporary(struct radeon_compiler
* c
)
75 GLboolean used
[MAX_PROGRAM_TEMPS
];
78 memset(used
, 0, sizeof(used
));
80 for (struct rc_instruction
* rcinst
= c
->Program
.Instructions
.Next
; rcinst
!= &c
->Program
.Instructions
; rcinst
= rcinst
->Next
) {
81 const struct prog_instruction
*inst
= &rcinst
->I
;
82 const GLuint nsrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
83 const GLuint ndst
= _mesa_num_inst_dst_regs(inst
->Opcode
);
86 for (k
= 0; k
< nsrc
; k
++) {
87 if (inst
->SrcReg
[k
].File
== PROGRAM_TEMPORARY
)
88 used
[inst
->SrcReg
[k
].Index
] = GL_TRUE
;
92 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
)
93 used
[inst
->DstReg
.Index
] = GL_TRUE
;
97 for (i
= 0; i
< MAX_PROGRAM_TEMPS
; i
++) {
106 struct rc_instruction
*rc_alloc_instruction(struct radeon_compiler
* c
)
108 struct rc_instruction
* inst
= memory_pool_malloc(&c
->Pool
, sizeof(struct rc_instruction
));
113 _mesa_init_instructions(&inst
->I
, 1);
119 struct rc_instruction
*rc_insert_new_instruction(struct radeon_compiler
* c
, struct rc_instruction
* after
)
121 struct rc_instruction
* inst
= rc_alloc_instruction(c
);
124 inst
->Next
= after
->Next
;
126 inst
->Prev
->Next
= inst
;
127 inst
->Next
->Prev
= inst
;
132 void rc_remove_instruction(struct rc_instruction
* inst
)
134 inst
->Prev
->Next
= inst
->Next
;
135 inst
->Next
->Prev
= inst
->Prev
;
139 void rc_mesa_to_rc_program(struct radeon_compiler
* c
, struct gl_program
* program
)
141 struct prog_instruction
*source
;
144 for(source
= program
->Instructions
; source
->Opcode
!= OPCODE_END
; ++source
) {
145 struct rc_instruction
* dest
= rc_insert_new_instruction(c
, c
->Program
.Instructions
.Prev
);
149 c
->Program
.ShadowSamplers
= program
->ShadowSamplers
;
150 c
->Program
.InputsRead
= program
->InputsRead
;
151 c
->Program
.OutputsWritten
= program
->OutputsWritten
;
155 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
156 struct gl_vertex_program
* vp
= (struct gl_vertex_program
*) program
;
157 isNVProgram
= vp
->IsNVProgram
;
161 /* NV_vertex_program has a fixed-sized constant environment.
162 * This could be handled more efficiently for programs that
163 * do not use relative addressing.
165 for(i
= 0; i
< 96; ++i
) {
166 struct rc_constant constant
;
168 constant
.Type
= RC_CONSTANT_EXTERNAL
;
170 constant
.u
.External
= i
;
172 rc_constants_add(&c
->Program
.Constants
, &constant
);
175 for(i
= 0; i
< program
->Parameters
->NumParameters
; ++i
) {
176 struct rc_constant constant
;
178 constant
.Type
= RC_CONSTANT_EXTERNAL
;
180 constant
.u
.External
= i
;
182 rc_constants_add(&c
->Program
.Constants
, &constant
);
189 * Print program to stderr, default options.
191 void rc_print_program(const struct rc_program
*prog
)
195 struct rc_instruction
*inst
;
197 fprintf(stderr
, "# Radeon Compiler Program\n");
199 for(inst
= prog
->Instructions
.Next
; inst
!= &prog
->Instructions
; inst
= inst
->Next
) {
200 fprintf(stderr
, "%3d: ", linenum
);
202 /* Massive hack: We rely on the fact that the printers do not actually
203 * use the gl_program argument (last argument) in debug mode */
204 indent
= _mesa_fprint_instruction_opt(
206 indent
, PROG_PRINT_DEBUG
, 0);