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