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"
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.
44 static GLboolean
emit( struct tnl_clipspace_codegen
*p
,
48 if (p
->buf_used
< p
->buf_size
) {
51 p
->buf_used
+= vsnprintf( p
->buf
+ p
->buf_used
,
52 p
->buf_size
- p
->buf_used
,
57 return p
->buf_used
< p
->buf_size
;
61 static GLboolean
print_header( struct tnl_clipspace_codegen
*p
,
62 struct tnl_clipspace
*vtx
)
69 "struct tnl_clipspace_attr\n"
71 " unsigned int attrib; \n"
72 " unsigned int format;\n"
73 " unsigned int vertoffset; \n"
74 " unsigned int vertattrsize; \n"
76 " unsigned int inputstride;\n"
80 " const float *vp; \n"
85 "void emit_vertices( int start, int end, char *dest, \n"
86 " struct tnl_clipspace_attr *a) \n"
89 " for (i = start ; i < end ; i++, dest += %d) {\n",
94 static GLboolean
print_footer( struct tnl_clipspace_codegen
*p
)
103 static GLboolean
emit_reg( struct tnl_clipspace_codegen
*p
, GLint reg
)
105 int idx
= reg
& REG_OFFSET_MASK
;
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
);
117 static GLboolean
print_mov( struct tnl_clipspace_codegen
*p
, GLint dest
, GLint src
)
128 static GLboolean
print_const( struct tnl_clipspace_codegen
*p
,
129 GLint dest
, GLfloat c
)
134 emit(p
, " = %g;\n", c
);
137 static GLboolean
print_const_chan( struct tnl_clipspace_codegen
*p
,
138 GLint dest
, GLchan c
)
144 #if CHAN_TYPE == GL_FLOAT
152 static GLboolean
print_const_ubyte( struct tnl_clipspace_codegen
*p
,
153 GLint dest
, GLubyte c
)
158 emit(p
, " = %x;\n", c
);
161 static GLboolean
print_mad( struct tnl_clipspace_codegen
*p
,
162 GLint dest
, GLint src0
, GLint src1
, GLint src2
)
176 static GLboolean
print_float_to_ubyte( struct tnl_clipspace_codegen
*p
,
177 GLint dest
, GLint src
)
181 emit(p
, "UNCLAMPED_FLOAT_TO_UBYTE(") &&
188 static GLboolean
print_float_to_chan( struct tnl_clipspace_codegen
*p
,
189 GLint dest
, GLint src
)
193 emit(p
, "UNCLAMPED_FLOAT_TO_CHAN(") &&
201 static GLboolean
print_attr_header( struct tnl_clipspace_codegen
*p
,
202 struct tnl_clipspace_attr
*a
,
207 char *out_type_str
= "void";
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;
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",
222 emit(p
, " a[%d].inputptr += a[%d].inputstride;\n", j
, j
);
225 static GLboolean
print_attr_footer( struct tnl_clipspace_codegen
*p
)
231 static tnl_emit_func
print_store_func( struct tnl_clipspace_codegen
*p
)
233 fprintf(stderr
, "%s: emitted:\n%s\n", __FUNCTION__
, p
->buf
);
238 void _tnl_init_c_codegen( struct tnl_clipspace_codegen
*p
)
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
;
253 make_empty_list(&p
->codegen_list
);
256 p
->buf
= MALLOC(p
->buf_size
);