2 * Copyright 2003 Tungsten Graphics, inc.
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:
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
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.
25 * Keith Whitwell <keithw@tungstengraphics.com>
32 #include "t_context.h"
35 #include "simple_list.h"
38 #define vsnprintf _vsnprintf
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.
48 static GLboolean
emit( struct tnl_clipspace_codegen
*p
,
52 if (p
->buf_used
< p
->buf_size
) {
55 p
->buf_used
+= vsnprintf( p
->buf
+ p
->buf_used
,
56 p
->buf_size
- p
->buf_used
,
61 return p
->buf_used
< p
->buf_size
;
65 static GLboolean
print_header( struct tnl_clipspace_codegen
*p
,
66 struct tnl_clipspace
*vtx
)
73 "struct tnl_clipspace_attr\n"
75 " unsigned int attrib; \n"
76 " unsigned int format;\n"
77 " unsigned int vertoffset; \n"
78 " unsigned int vertattrsize; \n"
80 " unsigned int inputstride;\n"
84 " const float *vp; \n"
89 "void emit_vertices( int start, int end, char *dest, \n"
90 " struct tnl_clipspace_attr *a) \n"
93 " for (i = start ; i < end ; i++, dest += %d) {\n",
98 static GLboolean
print_footer( struct tnl_clipspace_codegen
*p
)
107 static GLboolean
emit_reg( struct tnl_clipspace_codegen
*p
, GLint reg
)
109 int idx
= reg
& REG_OFFSET_MASK
;
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
);
121 static GLboolean
print_mov( struct tnl_clipspace_codegen
*p
, GLint dest
, GLint src
)
132 static GLboolean
print_const( struct tnl_clipspace_codegen
*p
,
133 GLint dest
, GLfloat c
)
138 emit(p
, " = %g;\n", c
);
141 static GLboolean
print_const_chan( struct tnl_clipspace_codegen
*p
,
142 GLint dest
, GLchan c
)
148 #if CHAN_TYPE == GL_FLOAT
156 static GLboolean
print_const_ubyte( struct tnl_clipspace_codegen
*p
,
157 GLint dest
, GLubyte c
)
162 emit(p
, " = %x;\n", c
);
165 static GLboolean
print_mad( struct tnl_clipspace_codegen
*p
,
166 GLint dest
, GLint src0
, GLint src1
, GLint src2
)
180 static GLboolean
print_float_to_ubyte( struct tnl_clipspace_codegen
*p
,
181 GLint dest
, GLint src
)
185 emit(p
, "UNCLAMPED_FLOAT_TO_UBYTE(") &&
192 static GLboolean
print_float_to_chan( struct tnl_clipspace_codegen
*p
,
193 GLint dest
, GLint src
)
197 emit(p
, "UNCLAMPED_FLOAT_TO_CHAN(") &&
205 static GLboolean
print_attr_header( struct tnl_clipspace_codegen
*p
,
206 struct tnl_clipspace_attr
*a
,
211 char *out_type_str
= "void";
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;
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",
226 emit(p
, " a[%d].inputptr += a[%d].inputstride;\n", j
, j
);
229 static GLboolean
print_attr_footer( struct tnl_clipspace_codegen
*p
)
231 return emit(p
, " }\n");
234 static tnl_emit_func
print_store_func( struct tnl_clipspace_codegen
*p
)
236 fprintf(stderr
, "print_store_func: emitted:\n%s\n", p
->buf
);
240 void _tnl_init_c_codegen( struct tnl_clipspace_codegen
*p
)
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
;
255 make_empty_list(&p
->codegen_list
);
258 p
->buf
= (char *) MALLOC(p
->buf_size
);