r300: Allow compiler to add constants in a cleaner way
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_program.c
1 /*
2 * Copyright (C) 2008 Nicolai Haehnle.
3 *
4 * All Rights Reserved.
5 *
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:
13 *
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.
17 *
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.
25 *
26 */
27
28 #include "radeon_program.h"
29
30 #include "radeon_compiler.h"
31 #include "shader/prog_parameter.h"
32 #include "shader/prog_print.h"
33
34
35 /**
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.
43 *
44 * \note The transformation is currently not recursive; in other words,
45 * instructions emitted by transformations are not transformed.
46 *
47 * \note The transform is called 'local' because it can only look at
48 * one instruction at a time.
49 */
50 void radeonLocalTransform(
51 struct gl_program *program,
52 int num_transformations,
53 struct radeon_program_transformation* transformations)
54 {
55 struct radeon_transform_context ctx;
56 int ip;
57
58 ctx.Program = program;
59 ctx.OldInstructions = program->Instructions;
60 ctx.OldNumInstructions = program->NumInstructions;
61
62 program->Instructions = 0;
63 program->NumInstructions = 0;
64
65 for(ip = 0; ip < ctx.OldNumInstructions; ++ip) {
66 struct prog_instruction *instr = ctx.OldInstructions + ip;
67 int i;
68
69 for(i = 0; i < num_transformations; ++i) {
70 struct radeon_program_transformation* t = transformations + i;
71
72 if (t->function(&ctx, instr, t->userData))
73 break;
74 }
75
76 if (i >= num_transformations) {
77 struct prog_instruction* dest = radeonAppendInstructions(program, 1);
78 _mesa_copy_instructions(dest, instr, 1);
79 }
80 }
81
82 _mesa_free_instructions(ctx.OldInstructions, ctx.OldNumInstructions);
83 }
84
85
86 static void scan_instructions(GLboolean* used, const struct prog_instruction* insts, GLuint count)
87 {
88 GLuint i;
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);
92 GLuint k;
93
94 for (k = 0; k < n; k++) {
95 if (inst->SrcReg[k].File == PROGRAM_TEMPORARY)
96 used[inst->SrcReg[k].Index] = GL_TRUE;
97 }
98 }
99 }
100
101 GLint radeonFindFreeTemporary(struct radeon_transform_context *t)
102 {
103 GLboolean used[MAX_PROGRAM_TEMPS];
104 GLuint i;
105
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);
109
110 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
111 if (!used[i])
112 return i;
113 }
114
115 return -1;
116 }
117
118
119 /**
120 * Append the given number of instructions to the program and return a
121 * pointer to the first new instruction.
122 */
123 struct prog_instruction *radeonAppendInstructions(struct gl_program *program, int count)
124 {
125 int oldnum = program->NumInstructions;
126 _mesa_insert_instructions(program, oldnum, count);
127 return program->Instructions + oldnum;
128 }
129
130
131 GLint rc_find_free_temporary(struct radeon_compiler * c)
132 {
133 GLboolean used[MAX_PROGRAM_TEMPS];
134 GLuint i;
135
136 memset(used, 0, sizeof(used));
137
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);
141 GLuint k;
142
143 for (k = 0; k < n; k++) {
144 if (inst->SrcReg[k].File == PROGRAM_TEMPORARY)
145 used[inst->SrcReg[k].Index] = GL_TRUE;
146 }
147 }
148
149 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
150 if (!used[i])
151 return i;
152 }
153
154 return -1;
155 }
156
157
158 struct rc_instruction *rc_alloc_instruction(struct radeon_compiler * c)
159 {
160 struct rc_instruction * inst = memory_pool_malloc(&c->Pool, sizeof(struct rc_instruction));
161
162 inst->Prev = 0;
163 inst->Next = 0;
164
165 _mesa_init_instructions(&inst->I, 1);
166
167 return inst;
168 }
169
170
171 struct rc_instruction *rc_insert_new_instruction(struct radeon_compiler * c, struct rc_instruction * after)
172 {
173 struct rc_instruction * inst = rc_alloc_instruction(c);
174
175 inst->Prev = after;
176 inst->Next = after->Next;
177
178 inst->Prev->Next = inst;
179 inst->Next->Prev = inst;
180
181 return inst;
182 }
183
184 void rc_remove_instruction(struct rc_instruction * inst)
185 {
186 inst->Prev->Next = inst->Next;
187 inst->Next->Prev = inst->Prev;
188 }
189
190
191 void rc_mesa_to_rc_program(struct radeon_compiler * c, struct gl_program * program)
192 {
193 struct prog_instruction *source;
194 unsigned int i;
195
196 for(source = program->Instructions; source->Opcode != OPCODE_END; ++source) {
197 struct rc_instruction * dest = rc_insert_new_instruction(c, c->Program.Instructions.Prev);
198 dest->I = *source;
199 }
200
201 c->Program.ShadowSamplers = program->ShadowSamplers;
202 c->Program.InputsRead = program->InputsRead;
203 c->Program.OutputsWritten = program->OutputsWritten;
204
205 for(i = 0; i < program->Parameters->NumParameters; ++i) {
206 struct rc_constant constant;
207
208 constant.Type = RC_CONSTANT_EXTERNAL;
209 constant.u.External = i;
210
211 rc_constants_add(&c->Program.Constants, &constant);
212 }
213 }
214
215
216 /**
217 * Print program to stderr, default options.
218 */
219 void rc_print_program(const struct rc_program *prog)
220 {
221 GLuint indent = 0;
222 GLuint linenum = 1;
223 struct rc_instruction *inst;
224
225 fprintf(stderr, "# Radeon Compiler Program\n");
226
227 for(inst = prog->Instructions.Next; inst != &prog->Instructions; inst = inst->Next) {
228 fprintf(stderr, "%3d: ", linenum);
229
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(
233 stderr, &inst->I,
234 indent, PROG_PRINT_DEBUG, 0);
235
236 linenum++;
237 }
238 }