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