17117f1b4Smrg/*
27117f1b4Smrg * Mesa 3-D graphics library
37117f1b4Smrg *
47117f1b4Smrg * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
57117f1b4Smrg *
67117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
87117f1b4Smrg * to deal in the Software without restriction, including without limitation
97117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
107117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the
117117f1b4Smrg * Software is furnished to do so, subject to the following conditions:
127117f1b4Smrg *
137117f1b4Smrg * The above copyright notice and this permission notice shall be included
147117f1b4Smrg * in all copies or substantial portions of the Software.
157117f1b4Smrg *
167117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
177117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE.
237117f1b4Smrg *
247117f1b4Smrg * Authors:
25af69d88dSmrg *    Keith Whitwell <keithw@vmware.com>
267117f1b4Smrg */
277117f1b4Smrg
287117f1b4Smrg
29c1f859d4Smrg#include "main/glheader.h"
30c1f859d4Smrg#include "main/macros.h"
317ec681f3Smrg
32c1f859d4Smrg#include "main/mtypes.h"
337117f1b4Smrg
347117f1b4Smrg#include "math/m_xform.h"
357117f1b4Smrg
367117f1b4Smrg#include "t_context.h"
377117f1b4Smrg#include "t_pipeline.h"
387117f1b4Smrg
397117f1b4Smrg/* Is there any real benefit seperating texmat from texgen?  It means
407117f1b4Smrg * we need two lots of intermediate storage.  Any changes to
417117f1b4Smrg * _NEW_TEXTURE will invalidate both sets -- it's only on changes to
427117f1b4Smrg * *only* _NEW_TEXTURE_MATRIX that texgen survives but texmat doesn't.
437117f1b4Smrg *
447117f1b4Smrg * However, the seperation of this code from the complex texgen stuff
457117f1b4Smrg * is very appealing.
467117f1b4Smrg */
477117f1b4Smrgstruct texmat_stage_data {
487117f1b4Smrg   GLvector4f texcoord[MAX_TEXTURE_COORD_UNITS];
497117f1b4Smrg};
507117f1b4Smrg
517117f1b4Smrg#define TEXMAT_STAGE_DATA(stage) ((struct texmat_stage_data *)stage->privatePtr)
527117f1b4Smrg
537117f1b4Smrg
547117f1b4Smrg
553464ebd5Sriastradhstatic GLboolean run_texmat_stage( struct gl_context *ctx,
567117f1b4Smrg				   struct tnl_pipeline_stage *stage )
577117f1b4Smrg{
587117f1b4Smrg   struct texmat_stage_data *store = TEXMAT_STAGE_DATA(stage);
597117f1b4Smrg   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
607117f1b4Smrg   GLuint i;
617117f1b4Smrg
627ec681f3Smrg   if (!ctx->Texture._TexMatEnabled || ctx->VertexProgram._Current)
637117f1b4Smrg      return GL_TRUE;
647117f1b4Smrg
657117f1b4Smrg   /* ENABLE_TEXMAT implies that the texture matrix is not the
667117f1b4Smrg    * identity, so we don't have to check that here.
677117f1b4Smrg    */
687117f1b4Smrg   for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) {
697117f1b4Smrg      if (ctx->Texture._TexMatEnabled & ENABLE_TEXMAT(i)) {
707117f1b4Smrg	 (void) TransformRaw( &store->texcoord[i],
717117f1b4Smrg			      ctx->TextureMatrixStack[i].Top,
727117f1b4Smrg			      VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]);
737117f1b4Smrg
747117f1b4Smrg	 VB->AttribPtr[VERT_ATTRIB_TEX0+i] = &store->texcoord[i];
757117f1b4Smrg      }
767117f1b4Smrg   }
777117f1b4Smrg
787117f1b4Smrg   return GL_TRUE;
797117f1b4Smrg}
807117f1b4Smrg
817117f1b4Smrg
827117f1b4Smrg/* Called the first time stage->run() is invoked.
837117f1b4Smrg */
843464ebd5Sriastradhstatic GLboolean alloc_texmat_data( struct gl_context *ctx,
857117f1b4Smrg				    struct tnl_pipeline_stage *stage )
867117f1b4Smrg{
877117f1b4Smrg   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
887117f1b4Smrg   struct texmat_stage_data *store;
897117f1b4Smrg   GLuint i;
907117f1b4Smrg
91af69d88dSmrg   stage->privatePtr = calloc(1, sizeof(*store));
927117f1b4Smrg   store = TEXMAT_STAGE_DATA(stage);
937117f1b4Smrg   if (!store)
947117f1b4Smrg      return GL_FALSE;
957117f1b4Smrg
967117f1b4Smrg   for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
977117f1b4Smrg      _mesa_vector4f_alloc( &store->texcoord[i], 0, VB->Size, 32 );
987117f1b4Smrg
997117f1b4Smrg   return GL_TRUE;
1007117f1b4Smrg}
1017117f1b4Smrg
1027117f1b4Smrg
1037117f1b4Smrgstatic void free_texmat_data( struct tnl_pipeline_stage *stage )
1047117f1b4Smrg{
1057117f1b4Smrg   struct texmat_stage_data *store = TEXMAT_STAGE_DATA(stage);
1067117f1b4Smrg   GLuint i;
1077117f1b4Smrg
1087117f1b4Smrg   if (store) {
1097117f1b4Smrg      for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++)
1107117f1b4Smrg	 if (store->texcoord[i].data)
1117117f1b4Smrg	    _mesa_vector4f_free( &store->texcoord[i] );
112af69d88dSmrg      free( store );
1137117f1b4Smrg      stage->privatePtr = NULL;
1147117f1b4Smrg   }
1157117f1b4Smrg}
1167117f1b4Smrg
1177117f1b4Smrg
1187117f1b4Smrg
1197117f1b4Smrgconst struct tnl_pipeline_stage _tnl_texture_transform_stage =
1207117f1b4Smrg{
1217117f1b4Smrg   "texture transform",			/* name */
1227117f1b4Smrg   NULL,				/* private data */
1237117f1b4Smrg   alloc_texmat_data,
1247117f1b4Smrg   free_texmat_data,			/* destructor */
1257117f1b4Smrg   NULL,
1267117f1b4Smrg   run_texmat_stage,
1277117f1b4Smrg};
128