From: Keith Whitwell Date: Wed, 30 Jun 2004 11:48:21 +0000 (+0000) Subject: Add infrastructure for t_vertex.c codegen. Add an example driver X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=009aa3ef5ea6bc13c903d6f902b7039adef8fc74;p=mesa.git Add infrastructure for t_vertex.c codegen. Add an example driver for this which spits out C code for the generated functions. --- diff --git a/src/mesa/glapi/glapi.c b/src/mesa/glapi/glapi.c index bba4b22fd6b..2a083949fe0 100644 --- a/src/mesa/glapi/glapi.c +++ b/src/mesa/glapi/glapi.c @@ -291,8 +291,8 @@ _glapi_set_dispatch(struct _glapi_table *dispatch) /* Use the no-op functions if a NULL dispatch table was requested. */ - old_style_dispatch = (struct _glapi_table *) (dispatch == NULL) - ? __glapi_noop_table : dispatch; + old_style_dispatch = (dispatch == NULL) + ? (struct _glapi_table *) __glapi_noop_table : dispatch; #ifdef DEBUG if (dispatch != NULL) { diff --git a/src/mesa/sources b/src/mesa/sources index 9ee404f10eb..9845dc033b5 100644 --- a/src/mesa/sources +++ b/src/mesa/sources @@ -125,6 +125,8 @@ TNL_SOURCES = \ tnl/t_vb_texmat.c \ tnl/t_vb_vertex.c \ tnl/t_vertex.c \ + tnl/t_vertex_c.c \ + tnl/t_vertex_codegen.c \ tnl/t_vtx_api.c \ tnl/t_vtx_generic.c \ tnl/t_vtx_x86.c \ diff --git a/src/mesa/tnl/t_context.h b/src/mesa/tnl/t_context.h index 3d099b205a0..7e1aaead885 100644 --- a/src/mesa/tnl/t_context.h +++ b/src/mesa/tnl/t_context.h @@ -524,6 +524,7 @@ struct tnl_pipeline { GLuint nr_stages; }; +struct tnl_clipspace; struct tnl_clipspace_attr; typedef void (*extract_func)( const struct tnl_clipspace_attr *a, GLfloat *out, @@ -532,6 +533,9 @@ typedef void (*extract_func)( const struct tnl_clipspace_attr *a, GLfloat *out, typedef void (*insert_func)( const struct tnl_clipspace_attr *a, GLubyte *v, const GLfloat *in ); +typedef void (*emit_func)( GLcontext *ctx, GLuint start, + GLuint end, void *dest ); + /** * Describes how to convert/move a vertex attribute from a vertex array @@ -540,6 +544,7 @@ typedef void (*insert_func)( const struct tnl_clipspace_attr *a, GLubyte *v, struct tnl_clipspace_attr { GLuint attrib; /* which vertex attrib (0=position, etc) */ + GLuint format; GLuint vertoffset; /* position of the attrib in the vertex struct */ GLuint vertattrsize; /* size of the attribute in bytes */ GLubyte *inputptr; @@ -551,6 +556,40 @@ struct tnl_clipspace_attr }; +struct tnl_clipspace_codegen { + GLboolean (*emit_header)( struct tnl_clipspace_codegen *, + struct tnl_clipspace *); + GLboolean (*emit_footer)( struct tnl_clipspace_codegen * ); + GLboolean (*emit_attr_header)( struct tnl_clipspace_codegen *, + struct tnl_clipspace_attr *, + GLint j, GLenum out_type, + GLboolean need_vp ); + GLboolean (*emit_attr_footer)( struct tnl_clipspace_codegen * ); + GLboolean (*emit_mov)( struct tnl_clipspace_codegen *, + GLint, GLint ); + GLboolean (*emit_const)( struct tnl_clipspace_codegen *, + GLint, GLfloat ); + GLboolean (*emit_mad)( struct tnl_clipspace_codegen *, + GLint, GLint, GLint, GLint ); + GLboolean (*emit_float_to_chan)( struct tnl_clipspace_codegen *, + GLint, GLint ); + GLboolean (*emit_const_chan)( struct tnl_clipspace_codegen *, + GLint, GLchan ); + GLboolean (*emit_float_to_ubyte)( struct tnl_clipspace_codegen *, + GLint, GLint ); + GLboolean (*emit_const_ubyte)( struct tnl_clipspace_codegen *, + GLint, GLubyte ); + emit_func (*emit_store_func)( struct tnl_clipspace_codegen * ); + + struct _tnl_dynfn codegen_list; + + char *buf; + int buf_size; + int buf_used; + int out_offset; +}; + + typedef void (*points_func)( GLcontext *ctx, GLuint first, GLuint last ); typedef void (*line_func)( GLcontext *ctx, GLuint v1, GLuint v2 ); @@ -586,9 +625,11 @@ struct tnl_clipspace struct tnl_clipspace_attr attr[_TNL_ATTRIB_MAX]; GLuint attr_count; - void (*emit)( GLcontext *ctx, GLuint start, GLuint end, void *dest ); + emit_func emit; interp_func interp; copy_pv_func copy_pv; + + struct tnl_clipspace_codegen codegen; }; diff --git a/src/mesa/tnl/t_vertex.c b/src/mesa/tnl/t_vertex.c index dcb6aef16b1..0adbfede2d5 100644 --- a/src/mesa/tnl/t_vertex.c +++ b/src/mesa/tnl/t_vertex.c @@ -43,7 +43,6 @@ -#define GET_VERTEX_STATE(ctx) &(TNL_CONTEXT(ctx)->clipspace) /* @@ -587,7 +586,7 @@ static void extract_1ub_1f( const struct tnl_clipspace_attr *a, GLfloat *out, co } -struct { +static struct { const char *name; extract_func extract; insert_func insert[4]; @@ -859,16 +858,36 @@ static void generic_copy_pv_extras( GLcontext *ctx, */ -static void choose_emit_func( GLcontext *ctx, - GLuint start, GLuint end, - void *dest ) +static void do_emit( GLcontext *ctx, GLuint start, GLuint end, + void *dest) { + TNLcontext *tnl = TNL_CONTEXT(ctx); + struct vertex_buffer *VB = &tnl->vb; struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx); - vtx->emit = generic_emit; + struct tnl_clipspace_attr *a = vtx->attr; + const GLuint count = vtx->attr_count; + GLuint j; + + for (j = 0; j < count; j++) { + GLvector4f *vptr = VB->AttribPtr[a[j].attrib]; + a[j].inputstride = vptr->stride; + a[j].inputptr = ((GLubyte *)vptr->data) + start * vptr->stride; + a[j].emit = a[j].insert[vptr->size - 1]; + } + + vtx->emit = 0; + + if (0) + vtx->emit = _tnl_codegen_emit(ctx); + + if (!vtx->emit) + vtx->emit = generic_emit; + vtx->emit( ctx, start, end, dest ); } + static void choose_interp_func( GLcontext *ctx, GLfloat t, GLuint edst, GLuint eout, GLuint ein, @@ -1000,7 +1019,7 @@ GLuint _tnl_install_attrs( GLcontext *ctx, const struct tnl_attr_map *map, assert(nr < _TNL_ATTRIB_MAX); assert(nr == 0 || map[0].attrib == VERT_ATTRIB_POS); - vtx->emit = choose_emit_func; + vtx->emit = 0; vtx->interp = choose_interp_func; vtx->copy_pv = choose_copy_pv_func; vtx->new_inputs = ~0; @@ -1008,14 +1027,15 @@ GLuint _tnl_install_attrs( GLcontext *ctx, const struct tnl_attr_map *map, for (j = 0, i = 0; i < nr; i++) { const GLuint format = map[i].format; if (format == EMIT_PAD) { - offset += map[i].offset; + fprintf(stderr, "%d: pad %d, offset %d\n", i, + map[i].offset, offset); -/* fprintf(stderr, "%d: pad %d, offset now %d\n", i, */ -/* map[i].offset, offset); */ + offset += map[i].offset; } else { vtx->attr[j].attrib = map[i].attrib; + vtx->attr[j].format = format; vtx->attr[j].vp = vp; vtx->attr[j].insert = format_info[format].insert; vtx->attr[j].extract = format_info[format].extract; @@ -1026,8 +1046,9 @@ GLuint _tnl_install_attrs( GLcontext *ctx, const struct tnl_attr_map *map, else vtx->attr[j].vertoffset = offset; -/* fprintf(stderr, "%d: %s offset %d\n", i, */ -/* format_info[format].name, vtx->attr[j].vertoffset); */ + fprintf(stderr, "%d: %s, vp %p, offset %d\n", i, + format_info[format].name, (void *)vp, + vtx->attr[j].vertoffset); offset += format_info[format].attrsize; j++; @@ -1055,7 +1076,6 @@ void _tnl_invalidate_vertices( GLcontext *ctx, GLuint newinputs ) } - void _tnl_build_vertices( GLcontext *ctx, GLuint start, GLuint end, @@ -1068,8 +1088,8 @@ void _tnl_build_vertices( GLcontext *ctx, newinputs |= vtx->new_inputs; vtx->new_inputs = 0; - if (newinputs) - vtx->emit( ctx, start, end, vDest ); + if (newinputs) + do_emit( ctx, start, end, vDest ); } @@ -1079,7 +1099,7 @@ void *_tnl_emit_vertices_to_buffer( GLcontext *ctx, void *dest ) { struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx); - vtx->emit( ctx, start, end, dest ); + do_emit( ctx, start, end, dest ); return (void *)((GLubyte *)dest + vtx->vertex_size * (end - start)); } @@ -1098,6 +1118,8 @@ void _tnl_init_vertices( GLcontext *ctx, vtx->max_vertex_size = max_vertex_size; vtx->vertex_buf = (GLubyte *)ALIGN_CALLOC(vb_size * max_vertex_size, 32 ); } + + _tnl_init_c_codegen( &vtx->codegen ); } diff --git a/src/mesa/tnl/t_vertex.h b/src/mesa/tnl/t_vertex.h index 8c93907c6f2..9f4847f6922 100644 --- a/src/mesa/tnl/t_vertex.h +++ b/src/mesa/tnl/t_vertex.h @@ -124,5 +124,24 @@ extern void _tnl_invalidate_vertices( GLcontext *ctx, GLuint newinputs ); extern void _tnl_invalidate_vertex_state( GLcontext *ctx, GLuint new_state ); +extern emit_func _tnl_codegen_emit( GLcontext *ctx ); + +#define REG_IN (0<<16) +#define REG_OUT (1<<16) +#define REG_VP (2<<16) +#define REG_TMP (3<<16) +#define REG_MASK (3<<16) + +#define REG_OFFSET_MASK 0xffff + +#define in( offset ) (REG_IN | (offset)) +#define out( offset ) (REG_OUT | (offset)) +#define vp( offset ) (REG_VP | (offset)) +#define tmp( offset ) (REG_TMP | (offset)) + + +extern void _tnl_init_c_codegen( struct tnl_clipspace_codegen *p ); + +#define GET_VERTEX_STATE(ctx) &(TNL_CONTEXT(ctx)->clipspace) #endif diff --git a/src/mesa/tnl/t_vertex_c.c b/src/mesa/tnl/t_vertex_c.c new file mode 100644 index 00000000000..d1ef7cd53c0 --- /dev/null +++ b/src/mesa/tnl/t_vertex_c.c @@ -0,0 +1,257 @@ +/* + * Copyright 2003 Tungsten Graphics, inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Keith Whitwell + */ + +#include "glheader.h" +#include "context.h" +#include "colormac.h" + +#include "t_context.h" +#include "t_vertex.h" + +#include "simple_list.h" + +/* A version of code generation for t_clipspace_codegen.c which prints + * out 'c' code to implement the generated function. A useful + * debugging tool, and in concert with something like tcc or a + * library-ized gcc, could do the whole job. + */ + + +static GLboolean emit( struct tnl_clipspace_codegen *p, + const char *fmt, + ... ) +{ + if (p->buf_used < p->buf_size) { + va_list ap; + va_start( ap, fmt ); + p->buf_used += vsnprintf( p->buf + p->buf_used, + p->buf_size - p->buf_used, + fmt, ap ); + va_end( ap ); + } + + return p->buf_used < p->buf_size; +} + + +static GLboolean print_header( struct tnl_clipspace_codegen *p, + struct tnl_clipspace *vtx ) +{ + p->buf_used = 0; + p->out_offset = 0; + + return + emit(p, + "struct tnl_clipspace_attr\n" + "{\n" + " unsigned int attrib; \n" + " unsigned int format;\n" + " unsigned int vertoffset; \n" + " unsigned int vertattrsize; \n" + " char *inputptr;\n" + " unsigned int inputstride;\n" + " void *insert;\n" + " void *emit;\n" + " void * extract;\n" + " const float *vp; \n" + "};\n" + "\n" + ) && + emit(p, + "void emit_vertices( int start, int end, char *dest, \n" + " struct tnl_clipspace_attr *a) \n" + "{\n" + " int i;" + " for (i = start ; i < end ; i++, dest += %d) {\n", + vtx->vertex_size); + +} + +static GLboolean print_footer( struct tnl_clipspace_codegen *p ) +{ + return + emit(p, + " }\n" + "}\n" + ); +} + +static GLboolean emit_reg( struct tnl_clipspace_codegen *p, GLint reg ) +{ + int idx = reg & REG_OFFSET_MASK; + + switch (reg & REG_MASK) { + case REG_IN: return emit(p, "in[%d]", idx); + case REG_VP: return emit(p, "vp[%d]", idx); + case REG_TMP: return emit(p, "temp[%d]", idx); /* not used? */ + case REG_OUT: return emit(p, "out[%d]", idx); + } + + return GL_FALSE; +} + +static GLboolean print_mov( struct tnl_clipspace_codegen *p, GLint dest, GLint src ) +{ + return + emit(p, " ") && + emit_reg(p, dest) && + emit(p, " = ") && + emit_reg(p, src) && + emit(p, ";\n"); +} + + +static GLboolean print_const( struct tnl_clipspace_codegen *p, + GLint dest, GLfloat c ) +{ + return + emit(p, " ") && + emit_reg(p, dest) && + emit(p, " = %g;\n", c); +} + +static GLboolean print_const_chan( struct tnl_clipspace_codegen *p, + GLint dest, GLchan c ) +{ + return + emit(p, " ") && + emit_reg(p, dest) && + emit(p, " = ") && +#if CHAN_TYPE == GL_FLOAT + emit(p, "%f", c) && +#else + emit(p, "%d", c) && +#endif + emit(p, ";\n"); +} + +static GLboolean print_const_ubyte( struct tnl_clipspace_codegen *p, + GLint dest, GLubyte c ) +{ + return + emit(p, " ") && + emit_reg(p, dest) && + emit(p, " = %x;\n", c); +} + +static GLboolean print_mad( struct tnl_clipspace_codegen *p, + GLint dest, GLint src0, GLint src1, GLint src2 ) +{ + return + emit(p, " ") && + emit_reg(p, dest) && + emit(p, " = ") && + emit_reg(p, src0) && + emit(p, " * ") && + emit_reg(p, src1) && + emit(p, " + ") && + emit_reg(p, src2) && + emit(p, ";\n"); +} + +static GLboolean print_float_to_ubyte( struct tnl_clipspace_codegen *p, + GLint dest, GLint src ) +{ + return + emit(p, " ") && + emit(p, "UNCLAMPED_FLOAT_TO_UBYTE(") && + emit_reg(p, dest) && + emit(p, ", ") && + emit_reg(p, src) && + emit(p, ");\n"); +} + +static GLboolean print_float_to_chan( struct tnl_clipspace_codegen *p, + GLint dest, GLint src ) +{ + return + emit(p, " ") && + emit(p, "UNCLAMPED_FLOAT_TO_CHAN(") && + emit_reg(p, dest) && + emit(p, ", ") && + emit_reg(p, src) && + emit(p, ");\n"); +} + + +static GLboolean print_attr_header( struct tnl_clipspace_codegen *p, + struct tnl_clipspace_attr *a, + GLint j, + GLenum out_type, + GLboolean need_vp) +{ + char *out_type_str = "void"; + + switch(out_type) { + case GL_FLOAT: out_type_str = "float"; break; + case GL_UNSIGNED_BYTE: out_type_str = "unsigned char"; break; + case GL_UNSIGNED_SHORT: out_type_str = "unsigned short"; break; + } + + return + emit(p, " {\n") && + (need_vp ? emit(p, " const float *vp = a[%d].vp;\n", j) : 1) && + emit(p, " %s *out = (%s *)(dest + %d);\n", + out_type_str, out_type_str, a[j].vertoffset) && + emit(p, " const float *in = (const float *)a[%d].inputptr;\n", + j) && + emit(p, " a[%d].inputptr += a[%d].inputstride;\n", j, j); +} + +static GLboolean print_attr_footer( struct tnl_clipspace_codegen *p ) +{ + return + emit(p, " }\n"); +} + +static emit_func print_store_func( struct tnl_clipspace_codegen *p ) +{ + fprintf(stderr, "%s: emitted:\n%s\n", __FUNCTION__, p->buf); + + return 0; +} + +void _tnl_init_c_codegen( struct tnl_clipspace_codegen *p ) +{ + p->emit_header = print_header; + p->emit_footer = print_footer; + p->emit_attr_header = print_attr_header; + p->emit_attr_footer = print_attr_footer; + p->emit_mov = print_mov; + p->emit_const = print_const; + p->emit_mad = print_mad; + p->emit_float_to_chan = print_float_to_chan; + p->emit_const_chan = print_const_chan; + p->emit_float_to_ubyte = print_float_to_ubyte; + p->emit_const_ubyte = print_const_ubyte; + p->emit_store_func = print_store_func; + + make_empty_list(&p->codegen_list); + + p->buf_size = 2048; + p->buf = MALLOC(p->buf_size); +} diff --git a/src/mesa/tnl/t_vertex_codegen.c b/src/mesa/tnl/t_vertex_codegen.c new file mode 100644 index 00000000000..3c99b0411ea --- /dev/null +++ b/src/mesa/tnl/t_vertex_codegen.c @@ -0,0 +1,509 @@ +/* + * Copyright 2003 Tungsten Graphics, inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * TUNGSTEN GRAPHICS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Keith Whitwell + */ + +#include "glheader.h" +#include "context.h" +#include "colormac.h" + +#include "t_context.h" +#include "t_vertex.h" + +#include "simple_list.h" + +/* Another codegen scheme, hopefully portable to a few different + * architectures without too much work. + */ + + + + +static GLboolean emit_4f_viewport_4( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mad(p, out(0), vp(0), in(0), vp(12)) && + p->emit_mad(p, out(1), vp(5), in(1), vp(13)) && + p->emit_mad(p, out(2), vp(10), in(2), vp(14)) && + p->emit_mov(p, out(3), in(3)); +} + +static GLboolean emit_4f_viewport_3( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mad(p, out(0), vp(0), in(0), vp(12)) && + p->emit_mad(p, out(1), vp(5), in(1), vp(13)) && + p->emit_mad(p, out(2), vp(10), in(2), vp(14)) && + p->emit_const(p, out(3), 1.0); +} + +static GLboolean emit_4f_viewport_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mad(p, out(0), vp(0), in(0), vp(12)) && + p->emit_mad(p, out(1), vp(5), in(1), vp(13)) && + p->emit_mov(p, out(2), vp(14)) && + p->emit_const(p, out(3), 1.0); +} + +static GLboolean emit_4f_viewport_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mad(p, out(0), vp(0), in(0), vp(12)) && + p->emit_mov(p, out(1), vp(13)) && + p->emit_mov(p, out(2), vp(14)) && + p->emit_const(p, out(3), 1.0); +} + +static GLboolean emit_3f_viewport_3( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mad(p, out(0), vp(0), in(0), vp(12)) && + p->emit_mad(p, out(1), vp(5), in(1), vp(13)) && + p->emit_mad(p, out(2), vp(10), in(2), vp(14)); +} + +static GLboolean emit_3f_viewport_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mad(p, out(0), vp(0), in(0), vp(12)) && + p->emit_mad(p, out(1), vp(5), in(1), vp(13)) && + p->emit_mov(p, out(2), vp(14)); +} + +static GLboolean emit_3f_viewport_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mad(p, out(0), vp(0), in(0), vp(12)) && + p->emit_mov(p, out(1), vp(13)) && + p->emit_mov(p, out(2), vp(14)); +} + +static GLboolean emit_2f_viewport_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mad(p, out(0), vp(0), in(0), vp(12)) && + p->emit_mad(p, out(1), vp(5), in(1), vp(13)); +} + +static GLboolean emit_2f_viewport_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mad(p, out(0), vp(0), in(0), vp(12)); +} + + +static GLboolean emit_4f_4( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)) && + p->emit_mov(p, out(1), in(1)) && + p->emit_mov(p, out(2), in(2)) && + p->emit_mov(p, out(3), in(3)); +} + +static GLboolean emit_4f_3( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)) && + p->emit_mov(p, out(1), in(1)) && + p->emit_mov(p, out(2), in(2)) && + p->emit_const(p, out(3), 1.0); +} + +static GLboolean emit_4f_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)) && + p->emit_mov(p, out(1), in(1)) && + p->emit_const(p, out(2), 0.0) && + p->emit_const(p, out(3), 1.0); +} + +static GLboolean emit_4f_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)) && + p->emit_const(p, out(1), 0.0) && + p->emit_const(p, out(2), 0.0) && + p->emit_const(p, out(3), 1.0); +} + +static GLboolean emit_3f_xyw_4( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)) && + p->emit_mov(p, out(1), in(1)) && + p->emit_mov(p, out(2), in(3)); +} + +static GLboolean emit_3f_xyw_err( struct tnl_clipspace_codegen *p ) +{ + assert(0); + return GL_FALSE; +} + +static GLboolean emit_3f_3( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)) && + p->emit_mov(p, out(1), in(1)) && + p->emit_mov(p, out(2), in(2)); +} + +static GLboolean emit_3f_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)) && + p->emit_mov(p, out(1), in(1)) && + p->emit_const(p, out(2), 0.0); +} + +static GLboolean emit_3f_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)) && + p->emit_const(p, out(1), 0.0) && + p->emit_const(p, out(2), 0.0); +} + + +static GLboolean emit_2f_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)) && + p->emit_mov(p, out(1), in(1)); +} + +static GLboolean emit_2f_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)) && + p->emit_const(p, out(1), 0.0); +} + +static GLboolean emit_1f_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_mov(p, out(0), in(0)); +} + +static GLboolean emit_4chan_4f_rgba_4( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_chan(p, out(0), in(0)) && + p->emit_float_to_chan(p, out(1), in(1)) && + p->emit_float_to_chan(p, out(2), in(2)) && + p->emit_float_to_chan(p, out(3), in(3)); +} + +static GLboolean emit_4chan_4f_rgba_3( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_chan(p, out(0), in(0)) && + p->emit_float_to_chan(p, out(1), in(1)) && + p->emit_float_to_chan(p, out(2), in(2)) && + p->emit_const_chan(p, out(3), CHAN_MAX); +} + +static GLboolean emit_4chan_4f_rgba_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_chan(p, out(0), in(0)) && + p->emit_float_to_chan(p, out(1), in(1)) && + p->emit_const_chan(p, out(2), 0) && + p->emit_const_chan(p, out(3), CHAN_MAX); +} + +static GLboolean emit_4chan_4f_rgba_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_chan(p, out(0), in(0)) && + p->emit_const_chan(p, out(1), 0) && + p->emit_const_chan(p, out(2), 0) && + p->emit_const_chan(p, out(3), CHAN_MAX); +} + +static GLboolean emit_4ub_4f_rgba_4( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(0), in(0)) && + p->emit_float_to_ubyte(p, out(1), in(1)) && + p->emit_float_to_ubyte(p, out(2), in(2)) && + p->emit_float_to_ubyte(p, out(3), in(3)); +} + +static GLboolean emit_4ub_4f_rgba_3( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(0), in(0)) && + p->emit_float_to_ubyte(p, out(1), in(1)) && + p->emit_float_to_ubyte(p, out(2), in(2)) && + p->emit_const_ubyte(p, out(3), 0xff); +} + +static GLboolean emit_4ub_4f_rgba_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(0), in(0)) && + p->emit_float_to_ubyte(p, out(1), in(1)) && + p->emit_const_ubyte(p, out(2), 0) && + p->emit_const_ubyte(p, out(3), 0xff); +} + +static GLboolean emit_4ub_4f_rgba_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(0), in(0)) && + p->emit_const_ubyte(p, out(1), 0) && + p->emit_const_ubyte(p, out(2), 0) && + p->emit_const_ubyte(p, out(3), 0xff); +} + +static GLboolean emit_4ub_4f_bgra_4( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(2), in(0)) && + p->emit_float_to_ubyte(p, out(1), in(1)) && + p->emit_float_to_ubyte(p, out(0), in(2)) && + p->emit_float_to_ubyte(p, out(3), in(3)); +} + +static GLboolean emit_4ub_4f_bgra_3( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(2), in(0)) && + p->emit_float_to_ubyte(p, out(1), in(1)) && + p->emit_float_to_ubyte(p, out(0), in(2)) && + p->emit_const_ubyte(p, out(3), 0xff); +} + +static GLboolean emit_4ub_4f_bgra_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(2), in(0)) && + p->emit_float_to_ubyte(p, out(1), in(1)) && + p->emit_const_ubyte(p, out(0), 0) && + p->emit_const_ubyte(p, out(3), 0xff); +} + +static GLboolean emit_4ub_4f_bgra_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(2), in(0)) && + p->emit_const_ubyte(p, out(1), 0) && + p->emit_const_ubyte(p, out(0), 0) && + p->emit_const_ubyte(p, out(3), 0xff); +} + +static GLboolean emit_3ub_3f_rgb_3( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(0), in(0)) && + p->emit_float_to_ubyte(p, out(1), in(1)) && + p->emit_float_to_ubyte(p, out(2), in(2)); +} + +static GLboolean emit_3ub_3f_rgb_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(0), in(0)) && + p->emit_float_to_ubyte(p, out(1), in(1)) && + p->emit_const_ubyte(p, out(2), 0); +} + +static GLboolean emit_3ub_3f_rgb_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(0), in(0)) && + p->emit_const_ubyte(p, out(1), 0) && + p->emit_const_ubyte(p, out(2), 0); +} + +static GLboolean emit_3ub_3f_bgr_3( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(2), in(0)) && + p->emit_float_to_ubyte(p, out(1), in(1)) && + p->emit_float_to_ubyte(p, out(0), in(2)); +} + +static GLboolean emit_3ub_3f_bgr_2( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(2), in(0)) && + p->emit_float_to_ubyte(p, out(1), in(1)) && + p->emit_const_ubyte(p, out(0), 0); +} + +static GLboolean emit_3ub_3f_bgr_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(2), in(0)) && + p->emit_const_ubyte(p, out(1), 0) && + p->emit_const_ubyte(p, out(0), 0); +} + + +static GLboolean emit_1ub_1f_1( struct tnl_clipspace_codegen *p ) +{ + return + p->emit_float_to_ubyte(p, out(0), in(0)); +} + + + + +static struct { + const char *name; + GLenum out_type; + GLboolean need_vp; + GLboolean (*emit[4])( struct tnl_clipspace_codegen * ); +} emit_info[EMIT_MAX] = { + + { "1f", GL_FLOAT, GL_FALSE, + { emit_1f_1, emit_1f_1, emit_1f_1, emit_1f_1 } }, + + { "2f", GL_FLOAT, GL_FALSE, + { emit_2f_1, emit_2f_2, emit_2f_2, emit_2f_2 } }, + + { "3f", GL_FLOAT, GL_FALSE, + { emit_3f_1, emit_3f_2, emit_3f_3, emit_3f_3 } }, + + { "4f", GL_FLOAT, GL_FALSE, + { emit_4f_1, emit_4f_2, emit_4f_3, emit_4f_4 } }, + + { "2f_viewport", GL_FLOAT, GL_TRUE, + { emit_2f_viewport_1, emit_2f_viewport_2, emit_2f_viewport_2, + emit_2f_viewport_2 } }, + + { "3f_viewport", GL_FLOAT, GL_TRUE, + { emit_3f_viewport_1, emit_3f_viewport_2, emit_3f_viewport_3, + emit_3f_viewport_3 } }, + + { "4f_viewport", GL_FLOAT, GL_TRUE, + { emit_4f_viewport_1, emit_4f_viewport_2, emit_4f_viewport_3, + emit_4f_viewport_4 } }, + + { "3f_xyw", GL_FLOAT, GL_FALSE, + { emit_3f_xyw_err, emit_3f_xyw_err, emit_3f_xyw_err, + emit_3f_xyw_4 } }, + + { "1ub_1f", GL_UNSIGNED_BYTE, GL_FALSE, + { emit_1ub_1f_1, emit_1ub_1f_1, emit_1ub_1f_1, emit_1ub_1f_1 } }, + + { "3ub_3f_rgb", GL_UNSIGNED_BYTE, GL_FALSE, + { emit_3ub_3f_rgb_1, emit_3ub_3f_rgb_2, emit_3ub_3f_rgb_3, + emit_3ub_3f_rgb_3 } }, + + { "3ub_3f_bgr", GL_UNSIGNED_BYTE, GL_FALSE, + { emit_3ub_3f_bgr_1, emit_3ub_3f_bgr_2, emit_3ub_3f_bgr_3, + emit_3ub_3f_bgr_3 } }, + + { "4ub_4f_rgba", GL_UNSIGNED_BYTE, GL_FALSE, + { emit_4ub_4f_rgba_1, emit_4ub_4f_rgba_2, emit_4ub_4f_rgba_3, + emit_4ub_4f_rgba_4 } }, + + { "4ub_4f_bgra", GL_UNSIGNED_BYTE, GL_FALSE, + { emit_4ub_4f_bgra_1, emit_4ub_4f_bgra_2, emit_4ub_4f_bgra_3, + emit_4ub_4f_bgra_4 } }, + + { "4chan_4f_rgba", CHAN_TYPE, GL_FALSE, + { emit_4chan_4f_rgba_1, emit_4chan_4f_rgba_2, emit_4chan_4f_rgba_3, + emit_4chan_4f_rgba_4 } }, + + { "pad", 0, 0, + { 0, 0, 0, 0 } } + +}; + + +/*********************************************************************** + * list(attrib, size) --> function + * + * Because of the dependence of size, this all has to take place after + * the pipeline has been run. + */ + +emit_func _tnl_codegen_emit( GLcontext *ctx ) +{ + struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; + struct tnl_clipspace *vtx = GET_VERTEX_STATE(ctx); + struct tnl_clipspace_attr *a = vtx->attr; + struct tnl_clipspace_codegen *p = &vtx->codegen; + const GLuint count = vtx->attr_count; + GLuint j; + + /* Need a faster lookup, or is this linear scan of an MRU list good + * enough? MRU chosen based on the guess that consecutive VB's are + * likely to be of the same format. A hash of attributes and sizes + * might be a better technique. + * + * With the vtx code now in place, it should be possible to track + * changes to the sizes of input arrays (and state, of course) and + * only invalidate this function when those sizes have changed. + */ +#if 0 + foreach (l, p->codegen_list) { + if (l->attr_count != count) + continue; + + /* Assumptions: + * a[j].vp will not change for a given attrib + * a[j].vertex_offset will not change nothing else has changed. + */ + for (j = 0; j < count; j++) + if (a[j].attrib != l->a[j].attrib || + a[j].sz != l->a[j].sz) + break; + + if (j == count) { + move_to_head(l, p->codegen_list); + return l->func; + } + } +#endif + + p->emit_header( p, vtx ); + + for (j = 0; j < count; j++) { + GLuint sz = VB->AttribPtr[a[j].attrib]->size - 1; + p->emit_attr_header( p, a, j, + emit_info[a[j].format].out_type, + emit_info[a[j].format].need_vp ); + + if (!emit_info[a[j].format].emit[sz]( p )) { + fprintf(stderr, "codegen failed\n"); + return 0; + } + + p->emit_attr_footer( p ); + } + + p->emit_footer( p ); + + return p->emit_store_func( p ); +} +