r300: Detangle fragment program compiler from driver-specific structure
[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 "shader/prog_print.h"
31
32
33 /**
34 * Transform the given clause in the following way:
35 * 1. Replace it with an empty clause
36 * 2. For every instruction in the original clause, try the given
37 * transformations in order.
38 * 3. If one of the transformations returns GL_TRUE, assume that it
39 * has emitted the appropriate instruction(s) into the new clause;
40 * otherwise, copy the instruction verbatim.
41 *
42 * \note The transformation is currently not recursive; in other words,
43 * instructions emitted by transformations are not transformed.
44 *
45 * \note The transform is called 'local' because it can only look at
46 * one instruction at a time.
47 */
48 void radeonLocalTransform(
49 GLcontext *Ctx,
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.Ctx = Ctx;
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 }