Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[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 radeon_compiler * c,
52 int num_transformations,
53 struct radeon_program_transformation* transformations)
54 {
55 struct rc_instruction * inst = c->Program.Instructions.Next;
56
57 while(inst != &c->Program.Instructions) {
58 struct rc_instruction * current = inst;
59 int i;
60
61 inst = inst->Next;
62
63 for(i = 0; i < num_transformations; ++i) {
64 struct radeon_program_transformation* t = transformations + i;
65
66 if (t->function(c, current, t->userData))
67 break;
68 }
69 }
70 }
71
72
73 GLint rc_find_free_temporary(struct radeon_compiler * c)
74 {
75 GLboolean used[MAX_PROGRAM_TEMPS];
76 GLuint i;
77
78 memset(used, 0, sizeof(used));
79
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);
84 GLuint k;
85
86 for (k = 0; k < nsrc; k++) {
87 if (inst->SrcReg[k].File == PROGRAM_TEMPORARY)
88 used[inst->SrcReg[k].Index] = GL_TRUE;
89 }
90
91 if (ndst) {
92 if (inst->DstReg.File == PROGRAM_TEMPORARY)
93 used[inst->DstReg.Index] = GL_TRUE;
94 }
95 }
96
97 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
98 if (!used[i])
99 return i;
100 }
101
102 return -1;
103 }
104
105
106 struct rc_instruction *rc_alloc_instruction(struct radeon_compiler * c)
107 {
108 struct rc_instruction * inst = memory_pool_malloc(&c->Pool, sizeof(struct rc_instruction));
109
110 inst->Prev = 0;
111 inst->Next = 0;
112
113 _mesa_init_instructions(&inst->I, 1);
114
115 return inst;
116 }
117
118
119 struct rc_instruction *rc_insert_new_instruction(struct radeon_compiler * c, struct rc_instruction * after)
120 {
121 struct rc_instruction * inst = rc_alloc_instruction(c);
122
123 inst->Prev = after;
124 inst->Next = after->Next;
125
126 inst->Prev->Next = inst;
127 inst->Next->Prev = inst;
128
129 return inst;
130 }
131
132 void rc_remove_instruction(struct rc_instruction * inst)
133 {
134 inst->Prev->Next = inst->Next;
135 inst->Next->Prev = inst->Prev;
136 }
137
138
139 void rc_mesa_to_rc_program(struct radeon_compiler * c, struct gl_program * program)
140 {
141 struct prog_instruction *source;
142 unsigned int i;
143
144 for(source = program->Instructions; source->Opcode != OPCODE_END; ++source) {
145 struct rc_instruction * dest = rc_insert_new_instruction(c, c->Program.Instructions.Prev);
146 dest->I = *source;
147 }
148
149 c->Program.ShadowSamplers = program->ShadowSamplers;
150 c->Program.InputsRead = program->InputsRead;
151 c->Program.OutputsWritten = program->OutputsWritten;
152
153 int isNVProgram = 0;
154
155 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
156 struct gl_vertex_program * vp = (struct gl_vertex_program *) program;
157 isNVProgram = vp->IsNVProgram;
158 }
159
160 if (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.
164 */
165 for(i = 0; i < 96; ++i) {
166 struct rc_constant constant;
167
168 constant.Type = RC_CONSTANT_EXTERNAL;
169 constant.Size = 4;
170 constant.u.External = i;
171
172 rc_constants_add(&c->Program.Constants, &constant);
173 }
174 } else {
175 for(i = 0; i < program->Parameters->NumParameters; ++i) {
176 struct rc_constant constant;
177
178 constant.Type = RC_CONSTANT_EXTERNAL;
179 constant.Size = 4;
180 constant.u.External = i;
181
182 rc_constants_add(&c->Program.Constants, &constant);
183 }
184 }
185 }
186
187
188 /**
189 * Print program to stderr, default options.
190 */
191 void rc_print_program(const struct rc_program *prog)
192 {
193 GLuint indent = 0;
194 GLuint linenum = 1;
195 struct rc_instruction *inst;
196
197 fprintf(stderr, "# Radeon Compiler Program\n");
198
199 for(inst = prog->Instructions.Next; inst != &prog->Instructions; inst = inst->Next) {
200 fprintf(stderr, "%3d: ", linenum);
201
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(
205 stderr, &inst->I,
206 indent, PROG_PRINT_DEBUG, 0);
207
208 linenum++;
209 }
210 }