Rename the various function types in t_context.h to include a tnl_ prefix.
[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 /* A version of code generation for t_clipspace_codegen.c which prints
38 * out 'c' code to implement the generated function. A useful
39 * debugging tool, and in concert with something like tcc or a
40 * library-ized gcc, could do the whole job.
41 */
42
43
44 static GLboolean emit( struct tnl_clipspace_codegen *p,
45 const char *fmt,
46 ... )
47 {
48 if (p->buf_used < p->buf_size) {
49 va_list ap;
50 va_start( ap, fmt );
51 p->buf_used += vsnprintf( p->buf + p->buf_used,
52 p->buf_size - p->buf_used,
53 fmt, ap );
54 va_end( ap );
55 }
56
57 return p->buf_used < p->buf_size;
58 }
59
60
61 static GLboolean print_header( struct tnl_clipspace_codegen *p,
62 struct tnl_clipspace *vtx )
63 {
64 p->buf_used = 0;
65 p->out_offset = 0;
66
67 return
68 emit(p,
69 "struct tnl_clipspace_attr\n"
70 "{\n"
71 " unsigned int attrib; \n"
72 " unsigned int format;\n"
73 " unsigned int vertoffset; \n"
74 " unsigned int vertattrsize; \n"
75 " char *inputptr;\n"
76 " unsigned int inputstride;\n"
77 " void *insert;\n"
78 " void *emit;\n"
79 " void * extract;\n"
80 " const float *vp; \n"
81 "};\n"
82 "\n"
83 ) &&
84 emit(p,
85 "void emit_vertices( int start, int end, char *dest, \n"
86 " struct tnl_clipspace_attr *a) \n"
87 "{\n"
88 " int i;"
89 " for (i = start ; i < end ; i++, dest += %d) {\n",
90 vtx->vertex_size);
91
92 }
93
94 static GLboolean print_footer( struct tnl_clipspace_codegen *p )
95 {
96 return
97 emit(p,
98 " }\n"
99 "}\n"
100 );
101 }
102
103 static GLboolean emit_reg( struct tnl_clipspace_codegen *p, GLint reg )
104 {
105 int idx = reg & REG_OFFSET_MASK;
106
107 switch (reg & REG_MASK) {
108 case REG_IN: return emit(p, "in[%d]", idx);
109 case REG_VP: return emit(p, "vp[%d]", idx);
110 case REG_TMP: return emit(p, "temp[%d]", idx); /* not used? */
111 case REG_OUT: return emit(p, "out[%d]", idx);
112 }
113
114 return GL_FALSE;
115 }
116
117 static GLboolean print_mov( struct tnl_clipspace_codegen *p, GLint dest, GLint src )
118 {
119 return
120 emit(p, " ") &&
121 emit_reg(p, dest) &&
122 emit(p, " = ") &&
123 emit_reg(p, src) &&
124 emit(p, ";\n");
125 }
126
127
128 static GLboolean print_const( struct tnl_clipspace_codegen *p,
129 GLint dest, GLfloat c )
130 {
131 return
132 emit(p, " ") &&
133 emit_reg(p, dest) &&
134 emit(p, " = %g;\n", c);
135 }
136
137 static GLboolean print_const_chan( struct tnl_clipspace_codegen *p,
138 GLint dest, GLchan c )
139 {
140 return
141 emit(p, " ") &&
142 emit_reg(p, dest) &&
143 emit(p, " = ") &&
144 #if CHAN_TYPE == GL_FLOAT
145 emit(p, "%f", c) &&
146 #else
147 emit(p, "%d", c) &&
148 #endif
149 emit(p, ";\n");
150 }
151
152 static GLboolean print_const_ubyte( struct tnl_clipspace_codegen *p,
153 GLint dest, GLubyte c )
154 {
155 return
156 emit(p, " ") &&
157 emit_reg(p, dest) &&
158 emit(p, " = %x;\n", c);
159 }
160
161 static GLboolean print_mad( struct tnl_clipspace_codegen *p,
162 GLint dest, GLint src0, GLint src1, GLint src2 )
163 {
164 return
165 emit(p, " ") &&
166 emit_reg(p, dest) &&
167 emit(p, " = ") &&
168 emit_reg(p, src0) &&
169 emit(p, " * ") &&
170 emit_reg(p, src1) &&
171 emit(p, " + ") &&
172 emit_reg(p, src2) &&
173 emit(p, ";\n");
174 }
175
176 static GLboolean print_float_to_ubyte( struct tnl_clipspace_codegen *p,
177 GLint dest, GLint src )
178 {
179 return
180 emit(p, " ") &&
181 emit(p, "UNCLAMPED_FLOAT_TO_UBYTE(") &&
182 emit_reg(p, dest) &&
183 emit(p, ", ") &&
184 emit_reg(p, src) &&
185 emit(p, ");\n");
186 }
187
188 static GLboolean print_float_to_chan( struct tnl_clipspace_codegen *p,
189 GLint dest, GLint src )
190 {
191 return
192 emit(p, " ") &&
193 emit(p, "UNCLAMPED_FLOAT_TO_CHAN(") &&
194 emit_reg(p, dest) &&
195 emit(p, ", ") &&
196 emit_reg(p, src) &&
197 emit(p, ");\n");
198 }
199
200
201 static GLboolean print_attr_header( struct tnl_clipspace_codegen *p,
202 struct tnl_clipspace_attr *a,
203 GLint j,
204 GLenum out_type,
205 GLboolean need_vp)
206 {
207 char *out_type_str = "void";
208
209 switch(out_type) {
210 case GL_FLOAT: out_type_str = "float"; break;
211 case GL_UNSIGNED_BYTE: out_type_str = "unsigned char"; break;
212 case GL_UNSIGNED_SHORT: out_type_str = "unsigned short"; break;
213 }
214
215 return
216 emit(p, " {\n") &&
217 (need_vp ? emit(p, " const float *vp = a[%d].vp;\n", j) : 1) &&
218 emit(p, " %s *out = (%s *)(dest + %d);\n",
219 out_type_str, out_type_str, a[j].vertoffset) &&
220 emit(p, " const float *in = (const float *)a[%d].inputptr;\n",
221 j) &&
222 emit(p, " a[%d].inputptr += a[%d].inputstride;\n", j, j);
223 }
224
225 static GLboolean print_attr_footer( struct tnl_clipspace_codegen *p )
226 {
227 return
228 emit(p, " }\n");
229 }
230
231 static tnl_emit_func print_store_func( struct tnl_clipspace_codegen *p )
232 {
233 fprintf(stderr, "%s: emitted:\n%s\n", __FUNCTION__, p->buf);
234
235 return 0;
236 }
237
238 void _tnl_init_c_codegen( struct tnl_clipspace_codegen *p )
239 {
240 p->emit_header = print_header;
241 p->emit_footer = print_footer;
242 p->emit_attr_header = print_attr_header;
243 p->emit_attr_footer = print_attr_footer;
244 p->emit_mov = print_mov;
245 p->emit_const = print_const;
246 p->emit_mad = print_mad;
247 p->emit_float_to_chan = print_float_to_chan;
248 p->emit_const_chan = print_const_chan;
249 p->emit_float_to_ubyte = print_float_to_ubyte;
250 p->emit_const_ubyte = print_const_ubyte;
251 p->emit_store_func = print_store_func;
252
253 make_empty_list(&p->codegen_list);
254
255 p->buf_size = 2048;
256 p->buf = MALLOC(p->buf_size);
257 }