Rename temp_flag to temp_in_use. Use ctx->Const.MaxVertexProgramTemps
[mesa.git] / src / mesa / tnl / t_vertex_c.c
1 /*
2 * Copyright 2003 Tungsten Graphics, inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keithw@tungstengraphics.com>
26 */
27
28 #include "glheader.h"
29 #include "context.h"
30 #include "colormac.h"
31
32 #include "t_context.h"
33 #include "t_vertex.h"
34
35 #include "simple_list.h"
36
37 #ifdef WIN32
38 #define vsnprintf _vsnprintf
39 #endif
40
41 /* A version of code generation for t_clipspace_codegen.c which prints
42 * out 'c' code to implement the generated function. A useful
43 * debugging tool, and in concert with something like tcc or a
44 * library-ized gcc, could do the whole job.
45 */
46
47
48 static GLboolean emit( struct tnl_clipspace_codegen *p,
49 const char *fmt,
50 ... )
51 {
52 if (p->buf_used < p->buf_size) {
53 va_list ap;
54 va_start( ap, fmt );
55 p->buf_used += vsnprintf( p->buf + p->buf_used,
56 p->buf_size - p->buf_used,
57 fmt, ap );
58 va_end( ap );
59 }
60
61 return p->buf_used < p->buf_size;
62 }
63
64
65 static GLboolean print_header( struct tnl_clipspace_codegen *p,
66 struct tnl_clipspace *vtx )
67 {
68 p->buf_used = 0;
69 p->out_offset = 0;
70
71 return
72 emit(p,
73 "struct tnl_clipspace_attr\n"
74 "{\n"
75 " unsigned int attrib; \n"
76 " unsigned int format;\n"
77 " unsigned int vertoffset; \n"
78 " unsigned int vertattrsize; \n"
79 " char *inputptr;\n"
80 " unsigned int inputstride;\n"
81 " void *insert;\n"
82 " void *emit;\n"
83 " void * extract;\n"
84 " const float *vp; \n"
85 "};\n"
86 "\n"
87 ) &&
88 emit(p,
89 "void emit_vertices( int start, int end, char *dest, \n"
90 " struct tnl_clipspace_attr *a) \n"
91 "{\n"
92 " int i;"
93 " for (i = start ; i < end ; i++, dest += %d) {\n",
94 vtx->vertex_size);
95
96 }
97
98 static GLboolean print_footer( struct tnl_clipspace_codegen *p )
99 {
100 return
101 emit(p,
102 " }\n"
103 "}\n"
104 );
105 }
106
107 static GLboolean emit_reg( struct tnl_clipspace_codegen *p, GLint reg )
108 {
109 int idx = reg & REG_OFFSET_MASK;
110
111 switch (reg & REG_MASK) {
112 case REG_IN: return emit(p, "in[%d]", idx);
113 case REG_VP: return emit(p, "vp[%d]", idx);
114 case REG_TMP: return emit(p, "temp[%d]", idx); /* not used? */
115 case REG_OUT: return emit(p, "out[%d]", idx);
116 }
117
118 return GL_FALSE;
119 }
120
121 static GLboolean print_mov( struct tnl_clipspace_codegen *p, GLint dest, GLint src )
122 {
123 return
124 emit(p, " ") &&
125 emit_reg(p, dest) &&
126 emit(p, " = ") &&
127 emit_reg(p, src) &&
128 emit(p, ";\n");
129 }
130
131
132 static GLboolean print_const( struct tnl_clipspace_codegen *p,
133 GLint dest, GLfloat c )
134 {
135 return
136 emit(p, " ") &&
137 emit_reg(p, dest) &&
138 emit(p, " = %g;\n", c);
139 }
140
141 static GLboolean print_const_chan( struct tnl_clipspace_codegen *p,
142 GLint dest, GLchan c )
143 {
144 return
145 emit(p, " ") &&
146 emit_reg(p, dest) &&
147 emit(p, " = ") &&
148 #if CHAN_TYPE == GL_FLOAT
149 emit(p, "%f", c) &&
150 #else
151 emit(p, "%d", c) &&
152 #endif
153 emit(p, ";\n");
154 }
155
156 static GLboolean print_const_ubyte( struct tnl_clipspace_codegen *p,
157 GLint dest, GLubyte c )
158 {
159 return
160 emit(p, " ") &&
161 emit_reg(p, dest) &&
162 emit(p, " = %x;\n", c);
163 }
164
165 static GLboolean print_mad( struct tnl_clipspace_codegen *p,
166 GLint dest, GLint src0, GLint src1, GLint src2 )
167 {
168 return
169 emit(p, " ") &&
170 emit_reg(p, dest) &&
171 emit(p, " = ") &&
172 emit_reg(p, src0) &&
173 emit(p, " * ") &&
174 emit_reg(p, src1) &&
175 emit(p, " + ") &&
176 emit_reg(p, src2) &&
177 emit(p, ";\n");
178 }
179
180 static GLboolean print_float_to_ubyte( struct tnl_clipspace_codegen *p,
181 GLint dest, GLint src )
182 {
183 return
184 emit(p, " ") &&
185 emit(p, "UNCLAMPED_FLOAT_TO_UBYTE(") &&
186 emit_reg(p, dest) &&
187 emit(p, ", ") &&
188 emit_reg(p, src) &&
189 emit(p, ");\n");
190 }
191
192 static GLboolean print_float_to_chan( struct tnl_clipspace_codegen *p,
193 GLint dest, GLint src )
194 {
195 return
196 emit(p, " ") &&
197 emit(p, "UNCLAMPED_FLOAT_TO_CHAN(") &&
198 emit_reg(p, dest) &&
199 emit(p, ", ") &&
200 emit_reg(p, src) &&
201 emit(p, ");\n");
202 }
203
204
205 static GLboolean print_attr_header( struct tnl_clipspace_codegen *p,
206 struct tnl_clipspace_attr *a,
207 GLint j,
208 GLenum out_type,
209 GLboolean need_vp)
210 {
211 char *out_type_str = "void";
212
213 switch(out_type) {
214 case GL_FLOAT: out_type_str = "float"; break;
215 case GL_UNSIGNED_BYTE: out_type_str = "unsigned char"; break;
216 case GL_UNSIGNED_SHORT: out_type_str = "unsigned short"; break;
217 }
218
219 return
220 emit(p, " {\n") &&
221 (need_vp ? emit(p, " const float *vp = a[%d].vp;\n", j) : 1) &&
222 emit(p, " %s *out = (%s *)(dest + %d);\n",
223 out_type_str, out_type_str, a[j].vertoffset) &&
224 emit(p, " const float *in = (const float *)a[%d].inputptr;\n",
225 j) &&
226 emit(p, " a[%d].inputptr += a[%d].inputstride;\n", j, j);
227 }
228
229 static GLboolean print_attr_footer( struct tnl_clipspace_codegen *p )
230 {
231 return emit(p, " }\n");
232 }
233
234 static tnl_emit_func print_store_func( struct tnl_clipspace_codegen *p )
235 {
236 fprintf(stderr, "print_store_func: emitted:\n%s\n", p->buf);
237 return NULL;
238 }
239
240 void _tnl_init_c_codegen( struct tnl_clipspace_codegen *p )
241 {
242 p->emit_header = print_header;
243 p->emit_footer = print_footer;
244 p->emit_attr_header = print_attr_header;
245 p->emit_attr_footer = print_attr_footer;
246 p->emit_mov = print_mov;
247 p->emit_const = print_const;
248 p->emit_mad = print_mad;
249 p->emit_float_to_chan = print_float_to_chan;
250 p->emit_const_chan = print_const_chan;
251 p->emit_float_to_ubyte = print_float_to_ubyte;
252 p->emit_const_ubyte = print_const_ubyte;
253 p->emit_store_func = print_store_func;
254
255 make_empty_list(&p->codegen_list);
256
257 p->buf_size = 2048;
258 p->buf = (char *) MALLOC(p->buf_size);
259 }
260
261 void _tnl_free_c_codegen( struct tnl_clipspace_codegen *p )
262 {
263 if (p->buf) {
264 FREE(p->buf);
265 p->buf = NULL;
266 }
267 }