1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 *    Keith Whitwell <keithw@vmware.com>
26 */
27
28
29#include "main/glheader.h"
30#include "main/macros.h"
31#include "main/imports.h"
32#include "main/mtypes.h"
33
34#include "math/m_xform.h"
35
36#include "t_context.h"
37#include "t_pipeline.h"
38
39/* Is there any real benefit seperating texmat from texgen?  It means
40 * we need two lots of intermediate storage.  Any changes to
41 * _NEW_TEXTURE will invalidate both sets -- it's only on changes to
42 * *only* _NEW_TEXTURE_MATRIX that texgen survives but texmat doesn't.
43 *
44 * However, the seperation of this code from the complex texgen stuff
45 * is very appealing.
46 */
47struct texmat_stage_data {
48   GLvector4f texcoord[MAX_TEXTURE_COORD_UNITS];
49};
50
51#define TEXMAT_STAGE_DATA(stage) ((struct texmat_stage_data *)stage->privatePtr)
52
53
54
55static GLboolean run_texmat_stage( struct gl_context *ctx,
56				   struct tnl_pipeline_stage *stage )
57{
58   struct texmat_stage_data *store = TEXMAT_STAGE_DATA(stage);
59   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
60   GLuint i;
61
62   if (!ctx->Texture._TexMatEnabled || ctx->VertexProgram._Current)
63      return GL_TRUE;
64
65   /* ENABLE_TEXMAT implies that the texture matrix is not the
66    * identity, so we don't have to check that here.
67    */
68   for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) {
69      if (ctx->Texture._TexMatEnabled & ENABLE_TEXMAT(i)) {
70	 (void) TransformRaw( &store->texcoord[i],
71			      ctx->TextureMatrixStack[i].Top,
72			      VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]);
73
74	 VB->AttribPtr[VERT_ATTRIB_TEX0+i] = &store->texcoord[i];
75      }
76   }
77
78   return GL_TRUE;
79}
80
81
82/* Called the first time stage->run() is invoked.
83 */
84static GLboolean alloc_texmat_data( struct gl_context *ctx,
85				    struct tnl_pipeline_stage *stage )
86{
87   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
88   struct texmat_stage_data *store;
89   GLuint i;
90
91   stage->privatePtr = calloc(1, sizeof(*store));
92   store = TEXMAT_STAGE_DATA(stage);
93   if (!store)
94      return GL_FALSE;
95
96   for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
97      _mesa_vector4f_alloc( &store->texcoord[i], 0, VB->Size, 32 );
98
99   return GL_TRUE;
100}
101
102
103static void free_texmat_data( struct tnl_pipeline_stage *stage )
104{
105   struct texmat_stage_data *store = TEXMAT_STAGE_DATA(stage);
106   GLuint i;
107
108   if (store) {
109      for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++)
110	 if (store->texcoord[i].data)
111	    _mesa_vector4f_free( &store->texcoord[i] );
112      free( store );
113      stage->privatePtr = NULL;
114   }
115}
116
117
118
119const struct tnl_pipeline_stage _tnl_texture_transform_stage =
120{
121   "texture transform",			/* name */
122   NULL,				/* private data */
123   alloc_texmat_data,
124   free_texmat_data,			/* destructor */
125   NULL,
126   run_texmat_stage,
127};
128