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