Fixed MAXFIFO_S4. Removed WAIT_IDLE_EMPTY from savage_BCI_swap which resulted
[mesa.git] / src / mesa / tnl / t_vb_fog.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 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 fog_stage_data {
43 GLvector4f fogcoord; /* has actual storage allocated */
44 GLvector4f input; /* points into VB->EyePtr Z values */
45 };
46
47 #define FOG_STAGE_DATA(stage) ((struct fog_stage_data *)stage->privatePtr)
48
49 #define FOG_EXP_TABLE_SIZE 256
50 #define FOG_MAX (10.0)
51 #define EXP_FOG_MAX .0006595
52 #define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE)
53 static GLfloat exp_table[FOG_EXP_TABLE_SIZE];
54 static GLfloat inited = 0;
55
56 #if 1
57 #define NEG_EXP( result, narg ) \
58 do { \
59 GLfloat f = (GLfloat) (narg * (1.0/FOG_INCR)); \
60 GLint k = (GLint) f; \
61 if (k > FOG_EXP_TABLE_SIZE-2) \
62 result = (GLfloat) EXP_FOG_MAX; \
63 else \
64 result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]); \
65 } while (0)
66 #else
67 #define NEG_EXP( result, narg ) \
68 do { \
69 result = exp(-narg); \
70 } while (0)
71 #endif
72
73
74 /**
75 * Initialize the exp_table[] lookup table for approximating exp().
76 */
77 static void
78 init_static_data( void )
79 {
80 GLfloat f = 0.0F;
81 GLint i = 0;
82 for ( ; i < FOG_EXP_TABLE_SIZE ; i++, f += FOG_INCR) {
83 exp_table[i] = (GLfloat) exp(-f);
84 }
85 inited = 1;
86 }
87
88
89 /**
90 * Compute per-vertex fog blend factors from fog coordinates by
91 * evaluating the GL_LINEAR, GL_EXP or GL_EXP2 fog function.
92 * Fog coordinates are distances from the eye (typically between the
93 * near and far clip plane distances).
94 * Note the fog (eye Z) coords may be negative so we use ABS(z) below.
95 * Fog blend factors are in the range [0,1].
96 */
97 static void
98 compute_fog_blend_factors(GLcontext *ctx, GLvector4f *out, const GLvector4f *in)
99 {
100 GLfloat end = ctx->Fog.End;
101 GLfloat *v = in->start;
102 GLuint stride = in->stride;
103 GLuint n = in->count;
104 GLfloat (*data)[4] = out->data;
105 GLfloat d;
106 GLuint i;
107
108 out->count = in->count;
109
110 switch (ctx->Fog.Mode) {
111 case GL_LINEAR:
112 if (ctx->Fog.Start == ctx->Fog.End)
113 d = 1.0F;
114 else
115 d = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
116 for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) {
117 const GLfloat z = FABSF(*v);
118 GLfloat f = (end - z) * d;
119 data[i][0] = CLAMP(f, 0.0F, 1.0F);
120 }
121 break;
122 case GL_EXP:
123 d = ctx->Fog.Density;
124 for ( i = 0 ; i < n ; i++, STRIDE_F(v,stride)) {
125 const GLfloat z = FABSF(*v);
126 NEG_EXP( data[i][0], d * z );
127 }
128 break;
129 case GL_EXP2:
130 d = ctx->Fog.Density*ctx->Fog.Density;
131 for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) {
132 const GLfloat z = FABSF(*v);
133 NEG_EXP( data[i][0], d * z * z );
134 }
135 break;
136 default:
137 _mesa_problem(ctx, "Bad fog mode in make_fog_coord");
138 return;
139 }
140 }
141
142
143 static GLboolean
144 run_fog_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage)
145 {
146 TNLcontext *tnl = TNL_CONTEXT(ctx);
147 struct vertex_buffer *VB = &tnl->vb;
148 struct fog_stage_data *store = FOG_STAGE_DATA(stage);
149 GLvector4f *input;
150
151 if (stage->changed_inputs == 0)
152 return GL_TRUE;
153
154 if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT) {
155 /* Fog is computed from vertex or fragment Z values */
156 /* source = VB->ObjPtr or VB->EyePtr coords */
157 /* dest = VB->FogCoordPtr = fog stage private storage */
158 VB->FogCoordPtr = &store->fogcoord;
159
160 if (!ctx->_NeedEyeCoords) {
161 /* compute fog coords from object coords */
162 const GLfloat *m = ctx->ModelviewMatrixStack.Top->m;
163 GLfloat plane[4];
164
165 /* Use this to store calculated eye z values:
166 */
167 input = &store->fogcoord;
168
169 /* NOTE: negate plane here so we get positive fog coords! */
170 plane[0] = -m[2];
171 plane[1] = -m[6];
172 plane[2] = -m[10];
173 plane[3] = -m[14];
174 /* Full eye coords weren't required, just calculate the
175 * eye Z values.
176 */
177 _mesa_dotprod_tab[VB->ObjPtr->size]( (GLfloat *) input->data,
178 4 * sizeof(GLfloat),
179 VB->ObjPtr, plane );
180
181 input->count = VB->ObjPtr->count;
182 }
183 else {
184 /* fog coordinates = eye Z coordinates (use ABS later) */
185 input = &store->input;
186
187 if (VB->EyePtr->size < 2)
188 _mesa_vector4f_clean_elem( VB->EyePtr, VB->Count, 2 );
189
190 input->data = (GLfloat (*)[4]) &(VB->EyePtr->data[0][2]);
191 input->start = VB->EyePtr->start+2;
192 input->stride = VB->EyePtr->stride;
193 input->count = VB->EyePtr->count;
194 }
195 }
196 else {
197 /* use glFogCoord() coordinates */
198 input = VB->FogCoordPtr; /* source data */
199 VB->FogCoordPtr = &store->fogcoord; /* dest data */
200 }
201
202 if (tnl->_DoVertexFog) {
203 /* compute blend factors from fog coordinates */
204 compute_fog_blend_factors( ctx, VB->FogCoordPtr, input );
205 }
206 else {
207 /* results = incoming fog coords (compute fog per-fragment later) */
208 VB->FogCoordPtr = input;
209 }
210
211 VB->AttribPtr[_TNL_ATTRIB_FOG] = VB->FogCoordPtr;
212 return GL_TRUE;
213 }
214
215
216 static void
217 check_fog_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage)
218 {
219 stage->active = ctx->Fog.Enabled && !ctx->VertexProgram.Enabled;
220
221 if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT)
222 stage->inputs = _TNL_BIT_POS;
223 else
224 stage->inputs = _TNL_BIT_FOG;
225 }
226
227
228 /* Called the first time stage->run() is invoked.
229 */
230 static GLboolean
231 alloc_fog_data(GLcontext *ctx, struct tnl_pipeline_stage *stage)
232 {
233 TNLcontext *tnl = TNL_CONTEXT(ctx);
234 struct fog_stage_data *store;
235 stage->privatePtr = MALLOC(sizeof(*store));
236 store = FOG_STAGE_DATA(stage);
237 if (!store)
238 return GL_FALSE;
239
240 _mesa_vector4f_alloc( &store->fogcoord, 0, tnl->vb.Size, 32 );
241 _mesa_vector4f_init( &store->input, 0, 0 );
242
243 if (!inited)
244 init_static_data();
245
246 /* Now run the stage.
247 */
248 stage->run = run_fog_stage;
249 return stage->run( ctx, stage );
250 }
251
252
253 static void
254 free_fog_data(struct tnl_pipeline_stage *stage)
255 {
256 struct fog_stage_data *store = FOG_STAGE_DATA(stage);
257 if (store) {
258 _mesa_vector4f_free( &store->fogcoord );
259 FREE( store );
260 stage->privatePtr = NULL;
261 }
262 }
263
264
265 const struct tnl_pipeline_stage _tnl_fog_coordinate_stage =
266 {
267 "build fog coordinates", /* name */
268 _NEW_FOG|_NEW_PROGRAM, /* check_state */
269 _NEW_FOG, /* run_state */
270 GL_FALSE, /* active? */
271 0, /* inputs */
272 _TNL_BIT_FOG, /* outputs */
273 0, /* changed_inputs */
274 NULL, /* private_data */
275 free_fog_data, /* dtr */
276 check_fog_stage, /* check */
277 alloc_fog_data /* run -- initially set to init. */
278 };