1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg *
24848b8605Smrg * Authors:
25848b8605Smrg *    Keith Whitwell <keithw@vmware.com>
26848b8605Smrg */
27848b8605Smrg
28848b8605Smrg
29848b8605Smrg#include "main/glheader.h"
30848b8605Smrg#include "main/macros.h"
31848b8605Smrg#include "main/imports.h"
32848b8605Smrg#include "main/mtypes.h"
33848b8605Smrg
34848b8605Smrg#include "math/m_xform.h"
35848b8605Smrg
36848b8605Smrg#include "t_context.h"
37848b8605Smrg#include "t_pipeline.h"
38848b8605Smrg
39848b8605Smrg/* Is there any real benefit seperating texmat from texgen?  It means
40848b8605Smrg * we need two lots of intermediate storage.  Any changes to
41848b8605Smrg * _NEW_TEXTURE will invalidate both sets -- it's only on changes to
42848b8605Smrg * *only* _NEW_TEXTURE_MATRIX that texgen survives but texmat doesn't.
43848b8605Smrg *
44848b8605Smrg * However, the seperation of this code from the complex texgen stuff
45848b8605Smrg * is very appealing.
46848b8605Smrg */
47848b8605Smrgstruct texmat_stage_data {
48848b8605Smrg   GLvector4f texcoord[MAX_TEXTURE_COORD_UNITS];
49848b8605Smrg};
50848b8605Smrg
51848b8605Smrg#define TEXMAT_STAGE_DATA(stage) ((struct texmat_stage_data *)stage->privatePtr)
52848b8605Smrg
53848b8605Smrg
54848b8605Smrg
55848b8605Smrgstatic GLboolean run_texmat_stage( struct gl_context *ctx,
56848b8605Smrg				   struct tnl_pipeline_stage *stage )
57848b8605Smrg{
58848b8605Smrg   struct texmat_stage_data *store = TEXMAT_STAGE_DATA(stage);
59848b8605Smrg   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
60848b8605Smrg   GLuint i;
61848b8605Smrg
62848b8605Smrg   if (!ctx->Texture._TexMatEnabled || ctx->VertexProgram._Current)
63848b8605Smrg      return GL_TRUE;
64848b8605Smrg
65848b8605Smrg   /* ENABLE_TEXMAT implies that the texture matrix is not the
66848b8605Smrg    * identity, so we don't have to check that here.
67848b8605Smrg    */
68848b8605Smrg   for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) {
69848b8605Smrg      if (ctx->Texture._TexMatEnabled & ENABLE_TEXMAT(i)) {
70848b8605Smrg	 (void) TransformRaw( &store->texcoord[i],
71848b8605Smrg			      ctx->TextureMatrixStack[i].Top,
72848b8605Smrg			      VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]);
73848b8605Smrg
74848b8605Smrg	 VB->AttribPtr[VERT_ATTRIB_TEX0+i] = &store->texcoord[i];
75848b8605Smrg      }
76848b8605Smrg   }
77848b8605Smrg
78848b8605Smrg   return GL_TRUE;
79848b8605Smrg}
80848b8605Smrg
81848b8605Smrg
82848b8605Smrg/* Called the first time stage->run() is invoked.
83848b8605Smrg */
84848b8605Smrgstatic GLboolean alloc_texmat_data( struct gl_context *ctx,
85848b8605Smrg				    struct tnl_pipeline_stage *stage )
86848b8605Smrg{
87848b8605Smrg   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
88848b8605Smrg   struct texmat_stage_data *store;
89848b8605Smrg   GLuint i;
90848b8605Smrg
91848b8605Smrg   stage->privatePtr = calloc(1, sizeof(*store));
92848b8605Smrg   store = TEXMAT_STAGE_DATA(stage);
93848b8605Smrg   if (!store)
94848b8605Smrg      return GL_FALSE;
95848b8605Smrg
96848b8605Smrg   for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
97848b8605Smrg      _mesa_vector4f_alloc( &store->texcoord[i], 0, VB->Size, 32 );
98848b8605Smrg
99848b8605Smrg   return GL_TRUE;
100848b8605Smrg}
101848b8605Smrg
102848b8605Smrg
103848b8605Smrgstatic void free_texmat_data( struct tnl_pipeline_stage *stage )
104848b8605Smrg{
105848b8605Smrg   struct texmat_stage_data *store = TEXMAT_STAGE_DATA(stage);
106848b8605Smrg   GLuint i;
107848b8605Smrg
108848b8605Smrg   if (store) {
109848b8605Smrg      for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++)
110848b8605Smrg	 if (store->texcoord[i].data)
111848b8605Smrg	    _mesa_vector4f_free( &store->texcoord[i] );
112848b8605Smrg      free( store );
113848b8605Smrg      stage->privatePtr = NULL;
114848b8605Smrg   }
115848b8605Smrg}
116848b8605Smrg
117848b8605Smrg
118848b8605Smrg
119848b8605Smrgconst struct tnl_pipeline_stage _tnl_texture_transform_stage =
120848b8605Smrg{
121848b8605Smrg   "texture transform",			/* name */
122848b8605Smrg   NULL,				/* private data */
123848b8605Smrg   alloc_texmat_data,
124848b8605Smrg   free_texmat_data,			/* destructor */
125848b8605Smrg   NULL,
126848b8605Smrg   run_texmat_stage,
127848b8605Smrg};
128