Merge branch 'master' of git+ssh://joukj@git.freedesktop.org/git/mesa/mesa
[mesa.git] / src / mesa / swrast / s_fog.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
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
25
26 #include "glheader.h"
27 #include "colormac.h"
28 #include "context.h"
29 #include "macros.h"
30
31 #include "s_context.h"
32 #include "s_fog.h"
33
34
35 /**
36 * Used to convert current raster distance to a fog factor in [0,1].
37 */
38 GLfloat
39 _swrast_z_to_fogfactor(GLcontext *ctx, GLfloat z)
40 {
41 GLfloat d, f;
42
43 switch (ctx->Fog.Mode) {
44 case GL_LINEAR:
45 if (ctx->Fog.Start == ctx->Fog.End)
46 d = 1.0F;
47 else
48 d = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
49 f = (ctx->Fog.End - z) * d;
50 return CLAMP(f, 0.0F, 1.0F);
51 case GL_EXP:
52 d = ctx->Fog.Density;
53 f = EXPF(-d * z);
54 f = CLAMP(f, 0.0F, 1.0F);
55 return f;
56 case GL_EXP2:
57 d = ctx->Fog.Density;
58 f = EXPF(-(d * d * z * z));
59 f = CLAMP(f, 0.0F, 1.0F);
60 return f;
61 default:
62 _mesa_problem(ctx, "Bad fog mode in _swrast_z_to_fogfactor");
63 return 0.0;
64 }
65 }
66
67
68 #define LINEAR_FOG(f, coord) f = (fogEnd - coord) * fogScale
69
70 #define EXP_FOG(f, coord) f = EXPF(density * coord)
71
72 #define EXP2_FOG(f, coord) \
73 do { \
74 GLfloat tmp = negDensitySquared * coord * coord; \
75 if (tmp < FLT_MIN_10_EXP) \
76 tmp = FLT_MIN_10_EXP; \
77 f = EXPF(tmp); \
78 } while(0)
79
80
81 #define BLEND_FOG(f, coord) f = coord
82
83
84
85 /**
86 * Template code for computing fog blend factor and applying it to colors.
87 * \param TYPE either GLubyte, GLushort or GLfloat.
88 * \param COMPUTE_F code to compute the fog blend factor, f.
89 */
90 #define FOG_LOOP(TYPE, FOG_FUNC) \
91 if (span->arrayAttribs & FRAG_BIT_FOGC) { \
92 GLuint i; \
93 for (i = 0; i < span->end; i++) { \
94 const GLfloat fogCoord = span->array->attribs[FRAG_ATTRIB_FOGC][i][0]; \
95 const GLfloat c = FABSF(fogCoord); \
96 GLfloat f, oneMinusF; \
97 FOG_FUNC(f, c); \
98 f = CLAMP(f, 0.0F, 1.0F); \
99 oneMinusF = 1.0F - f; \
100 rgba[i][RCOMP] = (TYPE) (f * rgba[i][RCOMP] + oneMinusF * rFog); \
101 rgba[i][GCOMP] = (TYPE) (f * rgba[i][GCOMP] + oneMinusF * gFog); \
102 rgba[i][BCOMP] = (TYPE) (f * rgba[i][BCOMP] + oneMinusF * bFog); \
103 } \
104 } \
105 else { \
106 const GLfloat fogStep = span->attrStepX[FRAG_ATTRIB_FOGC][0]; \
107 GLfloat fogCoord = span->attrStart[FRAG_ATTRIB_FOGC][0]; \
108 const GLfloat wStep = span->attrStepX[FRAG_ATTRIB_WPOS][3]; \
109 GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3]; \
110 GLuint i; \
111 for (i = 0; i < span->end; i++) { \
112 const GLfloat c = FABSF(fogCoord) / w; \
113 GLfloat f, oneMinusF; \
114 FOG_FUNC(f, c); \
115 f = CLAMP(f, 0.0F, 1.0F); \
116 oneMinusF = 1.0F - f; \
117 rgba[i][RCOMP] = (TYPE) (f * rgba[i][RCOMP] + oneMinusF * rFog); \
118 rgba[i][GCOMP] = (TYPE) (f * rgba[i][GCOMP] + oneMinusF * gFog); \
119 rgba[i][BCOMP] = (TYPE) (f * rgba[i][BCOMP] + oneMinusF * bFog); \
120 fogCoord += fogStep; \
121 w += wStep; \
122 } \
123 }
124
125 /* As above, but CI mode (XXX try to merge someday) */
126 #define FOG_LOOP_CI(FOG_FUNC) \
127 if (span->arrayAttribs & FRAG_BIT_FOGC) { \
128 GLuint i; \
129 for (i = 0; i < span->end; i++) { \
130 const GLfloat fogCoord = span->array->attribs[FRAG_ATTRIB_FOGC][i][0]; \
131 const GLfloat c = FABSF(fogCoord); \
132 GLfloat f; \
133 FOG_FUNC(f, c); \
134 f = CLAMP(f, 0.0F, 1.0F); \
135 index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex); \
136 } \
137 } \
138 else { \
139 const GLfloat fogStep = span->attrStepX[FRAG_ATTRIB_FOGC][0]; \
140 GLfloat fogCoord = span->attrStart[FRAG_ATTRIB_FOGC][0]; \
141 const GLfloat wStep = span->attrStepX[FRAG_ATTRIB_WPOS][3]; \
142 GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3]; \
143 GLuint i; \
144 for (i = 0; i < span->end; i++) { \
145 const GLfloat c = FABSF(fogCoord) / w; \
146 GLfloat f; \
147 FOG_FUNC(f, c); \
148 f = CLAMP(f, 0.0F, 1.0F); \
149 index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex); \
150 fogCoord += fogStep; \
151 w += wStep; \
152 } \
153 }
154
155
156
157 /**
158 * Apply fog to a span of RGBA pixels.
159 * The fog value are either in the span->array->fog array or interpolated from
160 * the fog/fogStep values.
161 * They fog values are either fog coordinates (Z) or fog blend factors.
162 * _PreferPixelFog should be in sync with that state!
163 */
164 void
165 _swrast_fog_rgba_span( const GLcontext *ctx, SWspan *span )
166 {
167 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
168 GLfloat rFog, gFog, bFog;
169
170 ASSERT(swrast->_FogEnabled);
171 ASSERT(span->arrayMask & SPAN_RGBA);
172
173 /* compute (scaled) fog color */
174 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
175 rFog = ctx->Fog.Color[RCOMP] * 255.0;
176 gFog = ctx->Fog.Color[GCOMP] * 255.0;
177 bFog = ctx->Fog.Color[BCOMP] * 255.0;
178 }
179 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
180 rFog = ctx->Fog.Color[RCOMP] * 65535.0;
181 gFog = ctx->Fog.Color[GCOMP] * 65535.0;
182 bFog = ctx->Fog.Color[BCOMP] * 65535.0;
183 }
184 else {
185 rFog = ctx->Fog.Color[RCOMP];
186 gFog = ctx->Fog.Color[GCOMP];
187 bFog = ctx->Fog.Color[BCOMP];
188 }
189
190 if (swrast->_PreferPixelFog) {
191 /* The span's fog values are fog coordinates, now compute blend factors
192 * and blend the fragment colors with the fog color.
193 */
194 switch (swrast->_FogMode) {
195 case GL_LINEAR:
196 {
197 const GLfloat fogEnd = ctx->Fog.End;
198 const GLfloat fogScale = (ctx->Fog.Start == ctx->Fog.End)
199 ? 1.0F : 1.0F / (ctx->Fog.End - ctx->Fog.Start);
200 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
201 GLubyte (*rgba)[4] = span->array->rgba8;
202 FOG_LOOP(GLubyte, LINEAR_FOG);
203 }
204 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
205 GLushort (*rgba)[4] = span->array->rgba16;
206 FOG_LOOP(GLushort, LINEAR_FOG);
207 }
208 else {
209 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
210 ASSERT(span->array->ChanType == GL_FLOAT);
211 FOG_LOOP(GLfloat, LINEAR_FOG);
212 }
213 }
214 break;
215
216 case GL_EXP:
217 {
218 const GLfloat density = -ctx->Fog.Density;
219 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
220 GLubyte (*rgba)[4] = span->array->rgba8;
221 FOG_LOOP(GLubyte, EXP_FOG);
222 }
223 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
224 GLushort (*rgba)[4] = span->array->rgba16;
225 FOG_LOOP(GLushort, EXP_FOG);
226 }
227 else {
228 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
229 ASSERT(span->array->ChanType == GL_FLOAT);
230 FOG_LOOP(GLfloat, EXP_FOG);
231 }
232 }
233 break;
234
235 case GL_EXP2:
236 {
237 const GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
238 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
239 GLubyte (*rgba)[4] = span->array->rgba8;
240 FOG_LOOP(GLubyte, EXP2_FOG);
241 }
242 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
243 GLushort (*rgba)[4] = span->array->rgba16;
244 FOG_LOOP(GLushort, EXP2_FOG);
245 }
246 else {
247 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
248 ASSERT(span->array->ChanType == GL_FLOAT);
249 FOG_LOOP(GLfloat, EXP2_FOG);
250 }
251 }
252 break;
253
254 default:
255 _mesa_problem(ctx, "Bad fog mode in _swrast_fog_rgba_span");
256 return;
257 }
258 }
259 else {
260 /* The span's fog start/step/array values are blend factors in [0,1].
261 * They were previously computed per-vertex.
262 */
263 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
264 GLubyte (*rgba)[4] = span->array->rgba8;
265 FOG_LOOP(GLubyte, BLEND_FOG);
266 }
267 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
268 GLushort (*rgba)[4] = span->array->rgba16;
269 FOG_LOOP(GLushort, BLEND_FOG);
270 }
271 else {
272 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
273 ASSERT(span->array->ChanType == GL_FLOAT);
274 FOG_LOOP(GLfloat, BLEND_FOG);
275 }
276 }
277 }
278
279
280 /**
281 * As above, but color index mode.
282 */
283 void
284 _swrast_fog_ci_span( const GLcontext *ctx, SWspan *span )
285 {
286 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
287 const GLuint fogIndex = (GLuint) ctx->Fog.Index;
288 GLuint *index = span->array->index;
289
290 ASSERT(swrast->_FogEnabled);
291 ASSERT(span->arrayMask & SPAN_INDEX);
292
293 /* we need to compute fog blend factors */
294 if (swrast->_PreferPixelFog) {
295 /* The span's fog values are fog coordinates, now compute blend factors
296 * and blend the fragment colors with the fog color.
297 */
298 switch (ctx->Fog.Mode) {
299 case GL_LINEAR:
300 {
301 const GLfloat fogEnd = ctx->Fog.End;
302 const GLfloat fogScale = (ctx->Fog.Start == ctx->Fog.End)
303 ? 1.0F : 1.0F / (ctx->Fog.End - ctx->Fog.Start);
304 FOG_LOOP_CI(LINEAR_FOG);
305 }
306 break;
307 case GL_EXP:
308 {
309 const GLfloat density = -ctx->Fog.Density;
310 FOG_LOOP_CI(EXP_FOG);
311 }
312 break;
313 case GL_EXP2:
314 {
315 const GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
316 FOG_LOOP_CI(EXP2_FOG);
317 }
318 break;
319 default:
320 _mesa_problem(ctx, "Bad fog mode in _swrast_fog_ci_span");
321 return;
322 }
323 }
324 else {
325 /* The span's fog start/step/array values are blend factors in [0,1].
326 * They were previously computed per-vertex.
327 */
328 FOG_LOOP_CI(BLEND_FOG);
329 }
330 }