Speedup the venerable mm.[ch] allocator with doubly linked lists and a
[mesa.git] / src / mesa / tnl / t_vb_normals.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28
29 #include "glheader.h"
30 #include "colormac.h"
31 #include "context.h"
32 #include "macros.h"
33 #include "imports.h"
34 #include "mtypes.h"
35
36 #include "math/m_xform.h"
37
38 #include "t_context.h"
39 #include "t_pipeline.h"
40
41
42 struct normal_stage_data {
43 normal_func NormalTransform;
44 GLvector4f normal;
45 };
46
47 #define NORMAL_STAGE_DATA(stage) ((struct normal_stage_data *)stage->privatePtr)
48
49
50 static GLboolean
51 run_normal_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage)
52 {
53 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
54 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
55 const GLfloat *lengths;
56
57 if (!store->NormalTransform)
58 return GL_TRUE;
59
60 /* We can only use the display list's saved normal lengths if we've
61 * got a transformation matrix with uniform scaling.
62 */
63 if (_math_matrix_is_general_scale(ctx->ModelviewMatrixStack.Top))
64 lengths = NULL;
65 else
66 lengths = VB->NormalLengthPtr;
67
68 store->NormalTransform( ctx->ModelviewMatrixStack.Top,
69 ctx->_ModelViewInvScale,
70 VB->NormalPtr, /* input normals */
71 lengths,
72 &store->normal ); /* resulting normals */
73
74 if (VB->NormalPtr->count > 1) {
75 store->normal.stride = 4 * sizeof(GLfloat);
76 }
77 else {
78 store->normal.stride = 0;
79 }
80
81 VB->NormalPtr = &store->normal;
82 VB->AttribPtr[_TNL_ATTRIB_NORMAL] = VB->NormalPtr;
83
84 VB->NormalLengthPtr = NULL; /* no longer valid */
85 return GL_TRUE;
86 }
87
88
89 /**
90 * Examine current GL state and set the store->NormalTransform pointer
91 * to point to the appropriate normal transformation routine.
92 */
93 static void
94 validate_normal_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage)
95 {
96 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
97
98 if (ctx->ShaderObjects._VertexShaderPresent) {
99 store->NormalTransform = NULL;
100 return;
101 }
102
103 if (ctx->VertexProgram._Enabled ||
104 (!ctx->Light.Enabled &&
105 !(ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS))) {
106 store->NormalTransform = NULL;
107 return;
108 }
109
110 if (ctx->_NeedEyeCoords) {
111 /* Eye coordinates are needed, for whatever reasons.
112 * Do lighting in eye coordinates, as the GL spec says.
113 */
114 GLuint transform = NORM_TRANSFORM_NO_ROT;
115
116 if (_math_matrix_has_rotation(ctx->ModelviewMatrixStack.Top)) {
117 /* need to do full (3x3) matrix transform */
118 transform = NORM_TRANSFORM;
119 }
120
121 if (ctx->Transform.Normalize) {
122 store->NormalTransform = _mesa_normal_tab[transform | NORM_NORMALIZE];
123 }
124 else if (ctx->Transform.RescaleNormals &&
125 ctx->_ModelViewInvScale != 1.0) {
126 store->NormalTransform = _mesa_normal_tab[transform | NORM_RESCALE];
127 }
128 else {
129 store->NormalTransform = _mesa_normal_tab[transform];
130 }
131 }
132 else {
133 /* We don't need eye coordinates.
134 * Do lighting in object coordinates. Thus, we don't need to fully
135 * transform normal vectors (just leave them in object coordinates)
136 * but we still need to do normalization/rescaling if enabled.
137 */
138 if (ctx->Transform.Normalize) {
139 store->NormalTransform = _mesa_normal_tab[NORM_NORMALIZE];
140 }
141 else if (!ctx->Transform.RescaleNormals &&
142 ctx->_ModelViewInvScale != 1.0) {
143 store->NormalTransform = _mesa_normal_tab[NORM_RESCALE];
144 }
145 else {
146 store->NormalTransform = NULL;
147 }
148 }
149 }
150
151
152 /**
153 * Allocate stage's private data (storage for transformed normals).
154 */
155 static GLboolean
156 alloc_normal_data(GLcontext *ctx, struct tnl_pipeline_stage *stage)
157 {
158 TNLcontext *tnl = TNL_CONTEXT(ctx);
159 struct normal_stage_data *store;
160
161 stage->privatePtr = _mesa_malloc(sizeof(*store));
162 store = NORMAL_STAGE_DATA(stage);
163 if (!store)
164 return GL_FALSE;
165
166 _mesa_vector4f_alloc( &store->normal, 0, tnl->vb.Size, 32 );
167 return GL_TRUE;
168 }
169
170
171 /**
172 * Free stage's private data.
173 */
174 static void
175 free_normal_data(struct tnl_pipeline_stage *stage)
176 {
177 struct normal_stage_data *store = NORMAL_STAGE_DATA(stage);
178 if (store) {
179 _mesa_vector4f_free( &store->normal );
180 _mesa_free( store );
181 stage->privatePtr = NULL;
182 }
183 }
184
185
186 const struct tnl_pipeline_stage _tnl_normal_transform_stage =
187 {
188 "normal transform", /* name */
189 NULL, /* privatePtr */
190 alloc_normal_data, /* create */
191 free_normal_data, /* destroy */
192 validate_normal_stage, /* validate */
193 run_normal_stage /* run */
194 };