Merge branch 'mesa_7_5_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 n = _mesa_num_inst_src_regs(inst->Opcode);
83 GLuint k;
84
85 for (k = 0; k < n; k++) {
86 if (inst->SrcReg[k].File == PROGRAM_TEMPORARY)
87 used[inst->SrcReg[k].Index] = GL_TRUE;
88 }
89 }
90
91 for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
92 if (!used[i])
93 return i;
94 }
95
96 return -1;
97 }
98
99
100 struct rc_instruction *rc_alloc_instruction(struct radeon_compiler * c)
101 {
102 struct rc_instruction * inst = memory_pool_malloc(&c->Pool, sizeof(struct rc_instruction));
103
104 inst->Prev = 0;
105 inst->Next = 0;
106
107 _mesa_init_instructions(&inst->I, 1);
108
109 return inst;
110 }
111
112
113 struct rc_instruction *rc_insert_new_instruction(struct radeon_compiler * c, struct rc_instruction * after)
114 {
115 struct rc_instruction * inst = rc_alloc_instruction(c);
116
117 inst->Prev = after;
118 inst->Next = after->Next;
119
120 inst->Prev->Next = inst;
121 inst->Next->Prev = inst;
122
123 return inst;
124 }
125
126 void rc_remove_instruction(struct rc_instruction * inst)
127 {
128 inst->Prev->Next = inst->Next;
129 inst->Next->Prev = inst->Prev;
130 }
131
132
133 void rc_mesa_to_rc_program(struct radeon_compiler * c, struct gl_program * program)
134 {
135 struct prog_instruction *source;
136 unsigned int i;
137
138 for(source = program->Instructions; source->Opcode != OPCODE_END; ++source) {
139 struct rc_instruction * dest = rc_insert_new_instruction(c, c->Program.Instructions.Prev);
140 dest->I = *source;
141 }
142
143 c->Program.ShadowSamplers = program->ShadowSamplers;
144 c->Program.InputsRead = program->InputsRead;
145 c->Program.OutputsWritten = program->OutputsWritten;
146
147 for(i = 0; i < program->Parameters->NumParameters; ++i) {
148 struct rc_constant constant;
149
150 constant.Type = RC_CONSTANT_EXTERNAL;
151 constant.Size = 4;
152 constant.u.External = i;
153
154 rc_constants_add(&c->Program.Constants, &constant);
155 }
156 }
157
158
159 /**
160 * Print program to stderr, default options.
161 */
162 void rc_print_program(const struct rc_program *prog)
163 {
164 GLuint indent = 0;
165 GLuint linenum = 1;
166 struct rc_instruction *inst;
167
168 fprintf(stderr, "# Radeon Compiler Program\n");
169
170 for(inst = prog->Instructions.Next; inst != &prog->Instructions; inst = inst->Next) {
171 fprintf(stderr, "%3d: ", linenum);
172
173 /* Massive hack: We rely on the fact that the printers do not actually
174 * use the gl_program argument (last argument) in debug mode */
175 indent = _mesa_fprint_instruction_opt(
176 stderr, &inst->I,
177 indent, PROG_PRINT_DEBUG, 0);
178
179 linenum++;
180 }
181 }