Major rework of tnl module
[mesa.git] / src / mesa / tnl / t_vb_texmat.c
1 /* $Id: t_vb_texmat.c,v 1.1 2000/12/26 05:09:33 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 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 * Author:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30
31 #include "glheader.h"
32 #include "colormac.h"
33 #include "context.h"
34 #include "macros.h"
35 #include "mem.h"
36 #include "mmath.h"
37 #include "mtypes.h"
38
39 #include "math/m_xform.h"
40
41 #include "t_context.h"
42 #include "t_pipeline.h"
43
44 /* Is there any real benefit seperating texmat from texgen? It means
45 * we need two lots of intermediate storage. Any changes to
46 * _NEW_TEXTURE will invalidate both sets -- it's only on changes to
47 * *only* _NEW_TEXTURE_MATRIX that texgen survives but texmat doesn't.
48 *
49 * However, the seperation of this code from the complex texgen stuff
50 * is very appealing.
51 */
52 struct texmat_stage_data {
53 GLvector4f texcoord[MAX_TEXTURE_UNITS];
54 };
55
56 #define TEXMAT_STAGE_DATA(stage) ((struct texmat_stage_data *)stage->private)
57
58 static void check_texmat( GLcontext *ctx, struct gl_pipeline_stage *stage )
59 {
60 GLuint i;
61 stage->active = 0;
62
63 if (ctx->_Enabled & ENABLE_TEXMAT_ANY) {
64 GLuint flags = 0;
65
66 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
67 if (ctx->_Enabled & ENABLE_TEXMAT(i))
68 flags |= VERT_TEX(i);
69
70 stage->active = 1;
71 stage->inputs = flags;
72 stage->outputs = flags;
73 }
74 }
75
76 static GLboolean run_texmat_stage( GLcontext *ctx,
77 struct gl_pipeline_stage *stage )
78 {
79 struct texmat_stage_data *store = TEXMAT_STAGE_DATA(stage);
80 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
81 GLuint i;
82
83 /* ENABLE_TEXMAT implies that the texture matrix is not the
84 * identity, so we don't have to check that here.
85 */
86 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
87 if (ctx->_Enabled & ENABLE_TEXMAT(i)) {
88 if (stage->changed_inputs & VERT_TEX(i))
89 (void) TransformRaw( &store->texcoord[i], &ctx->TextureMatrix[i],
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->private = 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 gl_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_UNITS ; i++)
129 if (store->texcoord[i].data)
130 gl_vector4f_free( &store->texcoord[i] );
131 FREE( store );
132 stage->private = 0;
133 }
134 }
135
136
137
138 const struct gl_pipeline_stage _tnl_texture_transform_stage =
139 {
140 "texture transform",
141 _NEW_TEXTURE|_NEW_TEXTURE_MATRIX,
142 _NEW_TEXTURE|_NEW_TEXTURE_MATRIX,
143 0,0,0, /* active, inputs, outputs */
144 0,0, /* changed_inputs, private */
145 free_texmat_data, /* destructor */
146 check_texmat, /* check */
147 alloc_texmat_data, /* run -- initially set to init */
148 };