/* $Id: t_vb_texgen.c,v 1.1 2000/12/26 05:09:33 keithw Exp $ */ /* * Mesa 3-D graphics library * Version: 3.5 * * Copyright (C) 1999-2000 Brian Paul 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 * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 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 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 NONINFRINGEMENT. IN NO EVENT SHALL * BRIAN PAUL 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: * Brian Paul * Keith Whitwell */ #include "glheader.h" #include "colormac.h" #include "context.h" #include "macros.h" #include "mmath.h" #include "mem.h" #include "mtypes.h" #include "math/m_xform.h" #include "t_context.h" #include "t_pipeline.h" /*********************************************************************** * Automatic texture coordinate generation (texgen) code. */ struct texgen_stage_data; typedef void (*texgen_func)( GLcontext *ctx, struct texgen_stage_data *store, GLuint unit); struct texgen_stage_data { /* Per-texunit derived state. */ GLuint TexgenSize[MAX_TEXTURE_UNITS]; GLuint TexgenHoles[MAX_TEXTURE_UNITS]; texgen_func TexgenFunc[MAX_TEXTURE_UNITS]; /* Temporary values used in texgen. */ GLfloat (*tmp_f)[3]; GLfloat *tmp_m; /* Buffered outputs of the stage. */ GLvector4f texcoord[MAX_TEXTURE_UNITS]; }; #define TEXGEN_STAGE_DATA(stage) ((struct texgen_stage_data *)stage->private) static GLuint all_bits[5] = { 0, VEC_SIZE_1, VEC_SIZE_2, VEC_SIZE_3, VEC_SIZE_4, }; #define VEC_SIZE_FLAGS (VEC_SIZE_1|VEC_SIZE_2|VEC_SIZE_3|VEC_SIZE_4) /* */ static void build_m3(GLfloat f[][3], GLfloat m[], const GLvector3f *normal, const GLvector4f *eye ) { GLuint stride = eye->stride; GLfloat *coord = (GLfloat *)eye->start; GLuint count = eye->count; const GLfloat *norm = normal->start; GLuint i; /* KW: Had to rearrange this loop to avoid a compiler bug with gcc * 2.7.3.1 at -O3 optimization. Using -fno-strength-reduce * also fixed the bug - is this generally necessary? */ for (i=0;istride); } } static void build_m2(GLfloat f[][3], GLfloat m[], const GLvector3f *normal, const GLvector4f *eye ) { GLuint stride = eye->stride; GLfloat *coord = eye->start; GLuint count = eye->count; GLfloat *norm = normal->start; GLuint i; for (i=0;istride); } } typedef void (*build_m_func)(GLfloat f[][3], GLfloat m[], const GLvector3f *normal, const GLvector4f *eye ); static build_m_func build_m_tab[5] = { 0, 0, build_m2, build_m3, build_m3 }; /* This is unusual in that we respect the stride of the output vector * (f). This allows us to pass in either a texcoord vector4f, or a * temporary vector3f. */ static void build_f3( GLfloat *f, GLuint fstride, const GLvector3f *normal, const GLvector4f *eye ) { GLuint stride = eye->stride; GLfloat *coord = eye->start; GLuint count = eye->count; GLfloat *norm = normal->start; GLuint i; for (i=0;istride); } } static void build_f2( GLfloat *f, GLuint fstride, const GLvector3f *normal, const GLvector4f *eye ) { GLuint stride = eye->stride; GLfloat *coord = eye->start; GLuint count = eye->count; GLfloat *norm = normal->start; GLuint i; for (i=0;istride); } } typedef void (*build_f_func)( GLfloat *f, GLuint fstride, const GLvector3f *normal_vec, const GLvector4f *eye ); /* Just treat 4-vectors as 3-vectors. */ static build_f_func build_f_tab[5] = { 0, 0, build_f2, build_f3, build_f3 }; /* Special case texgen functions. */ static void texgen_reflection_map_nv( GLcontext *ctx, struct texgen_stage_data *store, GLuint unit ) { struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; GLvector4f *in = VB->TexCoordPtr[unit]; GLvector4f *out = &store->texcoord[unit]; build_f_tab[VB->EyePtr->size]( out->start, out->stride, VB->NormalPtr, VB->EyePtr ); if (in) { out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_3; out->count = in->count; out->size = MAX2(in->size, 3); if (in->size == 4) gl_copy_tab[0][0x8](out, in, 0); } else { out->flags |= VEC_SIZE_3; out->size = 3; out->count = in->count; } } static void texgen_normal_map_nv( GLcontext *ctx, struct texgen_stage_data *store, GLuint unit ) { struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; GLvector4f *in = VB->TexCoordPtr[unit]; GLvector4f *out = &store->texcoord[unit]; GLvector3f *normal = VB->NormalPtr; GLfloat (*texcoord)[4] = (GLfloat (*)[4])out->start; GLuint count = VB->Count; GLuint i; const GLfloat *norm = normal->start; for (i=0;istride)) { texcoord[i][0] = norm[0]; texcoord[i][1] = norm[1]; texcoord[i][2] = norm[2]; } if (in) { out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_3; out->count = in->count; out->size = MAX2(in->size, 3); if (in->size == 4) gl_copy_tab[0][0x8](out, in, 0); } else { out->flags |= VEC_SIZE_3; out->size = 3; out->count = in->count; } } static void texgen_sphere_map( GLcontext *ctx, struct texgen_stage_data *store, GLuint unit ) { struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; GLvector4f *in = VB->TexCoordPtr[unit]; GLvector4f *out = &store->texcoord[unit]; GLfloat (*texcoord)[4] = (GLfloat (*)[4]) out->start; GLuint count = VB->Count; GLuint i; GLfloat (*f)[3] = store->tmp_f; GLfloat *m = store->tmp_m; (build_m_tab[VB->EyePtr->size])( store->tmp_f, store->tmp_m, VB->NormalPtr, VB->EyePtr ); for (i=0;isize = MAX2(in->size,2); out->count = in->count; out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_2; if (in->size > 2) gl_copy_tab[0][all_bits[in->size] & ~0x3](out, in, 0); } else { out->size = 2; out->flags |= VEC_SIZE_2; out->count = in->count; } } static void texgen( GLcontext *ctx, struct texgen_stage_data *store, GLuint unit ) { TNLcontext *tnl = TNL_CONTEXT(ctx); struct vertex_buffer *VB = &tnl->vb; GLvector4f *in = VB->TexCoordPtr[unit]; GLvector4f *out = &store->texcoord[unit]; struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; const GLvector4f *obj = VB->ObjPtr; const GLvector4f *eye = VB->EyePtr; const GLvector3f *normal = VB->NormalPtr; GLfloat (*texcoord)[4] = (GLfloat (*)[4])out->data; GLfloat *indata; GLuint count = VB->Count; GLfloat (*f)[3] = store->tmp_f; GLfloat *m = store->tmp_m; if (texUnit->_GenFlags & TEXGEN_NEED_M) { build_m_tab[in->size]( store->tmp_f, store->tmp_m, normal, eye ); } else if (texUnit->_GenFlags & TEXGEN_NEED_F) { build_f_tab[in->size]( (GLfloat *)store->tmp_f, 3, normal, eye ); } if (in != out) { GLuint copy = (all_bits[in->size] & ~texUnit->TexGenEnabled); if (copy) gl_copy_tab[0][copy](out, in, 0); } if (store->TexgenHoles[unit]) { GLuint holes = (~all_bits[in->size] & store->TexgenHoles[unit]); if (holes) { if (holes & VEC_DIRTY_2) gl_vector4f_clean_elem(out, count, 2); if (holes & VEC_DIRTY_1) gl_vector4f_clean_elem(out, count, 1); if (holes & VEC_DIRTY_0) gl_vector4f_clean_elem(out, count, 0); } } out->size = MAX2(in->size, store->TexgenSize[unit]); out->flags |= (in->flags & VEC_SIZE_FLAGS) | texUnit->TexGenEnabled; out->count = in->count; if (texUnit->TexGenEnabled & S_BIT) { GLuint i; switch (texUnit->GenModeS) { case GL_OBJECT_LINEAR: (gl_dotprod_tab[0][obj->size])((GLfloat *)out->data, sizeof(out->data[0]), obj, texUnit->ObjectPlaneS, 0); break; case GL_EYE_LINEAR: (gl_dotprod_tab[0][eye->size])((GLfloat *)out->data, sizeof(out->data[0]), eye, texUnit->EyePlaneS, 0); break; case GL_SPHERE_MAP: for (indata=in->start,i=0 ; istride)) texcoord[i][0] = indata[0] * m[i] + 0.5F; break; case GL_REFLECTION_MAP_NV: for (i=0;istart; for (i=0;istride)) { texcoord[i][0] = norm[0]; } break; } default: gl_problem(ctx, "Bad S texgen"); } } if (texUnit->TexGenEnabled & T_BIT) { GLuint i; switch (texUnit->GenModeT) { case GL_OBJECT_LINEAR: (gl_dotprod_tab[0][obj->size])(&(out->data[0][1]), sizeof(out->data[0]), obj, texUnit->ObjectPlaneT, 0); break; case GL_EYE_LINEAR: (gl_dotprod_tab[0][eye->size])(&(out->data[0][1]), sizeof(out->data[0]), eye, texUnit->EyePlaneT, 0); break; case GL_SPHERE_MAP: for (indata=in->start,i=0; istride)) texcoord[i][1] = indata[1] * m[i] + 0.5F; break; case GL_REFLECTION_MAP_NV: for (i=0;istart; for (i=0;istride)) { texcoord[i][1] = norm[1]; } break; } default: gl_problem(ctx, "Bad T texgen"); } } if (texUnit->TexGenEnabled & R_BIT) { GLuint i; switch (texUnit->GenModeR) { case GL_OBJECT_LINEAR: (gl_dotprod_tab[0][obj->size])(&(out->data[0][2]), sizeof(out->data[0]), obj, texUnit->ObjectPlaneR, 0); break; case GL_EYE_LINEAR: (gl_dotprod_tab[0][eye->size])(&(out->data[0][2]), sizeof(out->data[0]), eye, texUnit->EyePlaneR, 0); break; case GL_REFLECTION_MAP_NV: for (i=0;istart; for (i=0;istride)) { texcoord[i][2] = norm[2]; } break; } default: gl_problem(ctx, "Bad R texgen"); } } if (texUnit->TexGenEnabled & Q_BIT) { switch (texUnit->GenModeQ) { case GL_OBJECT_LINEAR: (gl_dotprod_tab[0][obj->size])(&(out->data[0][3]), sizeof(out->data[0]), obj, texUnit->ObjectPlaneQ, 0); break; case GL_EYE_LINEAR: (gl_dotprod_tab[0][eye->size])(&(out->data[0][3]), sizeof(out->data[0]), eye, texUnit->EyePlaneQ, 0); break; default: gl_problem(ctx, "Bad Q texgen"); } } } static GLboolean run_texgen_stage( GLcontext *ctx, struct gl_pipeline_stage *stage ) { struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; struct texgen_stage_data *store = TEXGEN_STAGE_DATA( stage ); GLuint i; for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) if (ctx->_Enabled & ENABLE_TEXGEN(i)) { if (stage->changed_inputs & (VERT_EYE | VERT_NORM | VERT_TEX(i))) store->TexgenFunc[i]( ctx, store, i ); VB->TexCoordPtr[i] = &store->texcoord[i]; } return GL_TRUE; } static GLboolean run_validate_texgen_stage( GLcontext *ctx, struct gl_pipeline_stage *stage ) { struct texgen_stage_data *store = TEXGEN_STAGE_DATA(stage); GLuint i; for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) { struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i]; if (texUnit->TexGenEnabled) { GLuint sz; if (texUnit->TexGenEnabled & R_BIT) sz = 4; else if (texUnit->TexGenEnabled & Q_BIT) sz = 3; else if (texUnit->TexGenEnabled & T_BIT) sz = 2; else sz = 1; store->TexgenSize[i] = sz; store->TexgenHoles[i] = (all_bits[sz] & ~texUnit->TexGenEnabled); store->TexgenFunc[i] = texgen; if (texUnit->TexGenEnabled == (S_BIT|T_BIT|R_BIT)) { if (texUnit->_GenFlags == TEXGEN_REFLECTION_MAP_NV) { store->TexgenFunc[i] = texgen_reflection_map_nv; } else if (texUnit->_GenFlags == TEXGEN_NORMAL_MAP_NV) { store->TexgenFunc[i] = texgen_normal_map_nv; } } else if (texUnit->TexGenEnabled == (S_BIT|T_BIT) && texUnit->_GenFlags == TEXGEN_SPHERE_MAP) { store->TexgenFunc[i] = texgen_sphere_map; } } } stage->run = run_texgen_stage; return stage->run( ctx, stage ); } static void check_texgen( GLcontext *ctx, struct gl_pipeline_stage *stage ) { GLuint i; stage->active = 0; if (ctx->_Enabled & ENABLE_TEXGEN_ANY) { GLuint inputs = 0; GLuint outputs = 0; if (ctx->Texture._GenFlags & TEXGEN_NEED_VERTICES) inputs |= VERT_EYE; if (ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS) inputs |= VERT_NORM; for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) if (ctx->_Enabled & ENABLE_TEXGEN(i)) { outputs |= VERT_TEX(i); /* Need the original input in case it contains a Q coord: * (sigh) */ if ((ctx->Texture.Unit[i]._ReallyEnabled|Q_BIT) & ~ctx->Texture.Unit[i].TexGenEnabled) inputs |= VERT_TEX(i); /* Something for Feedback? */ } if (stage->private) stage->run = run_validate_texgen_stage; stage->active = 1; stage->inputs = inputs; stage->outputs = outputs; } } /* Called the first time stage->run() is invoked. */ static GLboolean alloc_texgen_data( GLcontext *ctx, struct gl_pipeline_stage *stage ) { struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; struct texgen_stage_data *store; GLuint i; stage->private = CALLOC(sizeof(*store)); store = TEXGEN_STAGE_DATA(stage); if (!store) return GL_FALSE; for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) gl_vector4f_alloc( &store->texcoord[i], 0, VB->Size, 32 ); store->tmp_f = (GLfloat (*)[3]) MALLOC(VB->Size * sizeof(GLfloat) * 3); store->tmp_m = (GLfloat *) MALLOC(VB->Size * sizeof(GLfloat)); /* Now validate and run the stage. */ stage->run = run_validate_texgen_stage; return stage->run( ctx, stage ); } static void free_texgen_data( struct gl_pipeline_stage *stage ) { struct texgen_stage_data *store = TEXGEN_STAGE_DATA(stage); GLuint i; if (store) { for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++) if (store->texcoord[i].data) gl_vector4f_free( &store->texcoord[i] ); if (store->tmp_f) FREE( store->tmp_f ); if (store->tmp_m) FREE( store->tmp_m ); FREE( store ); stage->private = 0; } } const struct gl_pipeline_stage _tnl_texgen_stage = { "texgen", _NEW_TEXTURE, /* when to call check() */ _NEW_TEXTURE, /* when to invalidate stored data */ 0,0,0, /* active, inputs, outputs */ 0,0, /* changed_inputs, private */ free_texgen_data, /* destructor */ check_texgen, /* check */ alloc_texgen_data /* run -- initially set to alloc data */ };