Merge branch 'master' of git+ssh://michal@git.freedesktop.org/git/mesa/mesa into...
[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(swrast->_ActiveAttribMask & FRAG_BIT_FOGC);
172 ASSERT(span->arrayMask & SPAN_RGBA);
173
174 /* compute (scaled) fog color */
175 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
176 rFog = ctx->Fog.Color[RCOMP] * 255.0;
177 gFog = ctx->Fog.Color[GCOMP] * 255.0;
178 bFog = ctx->Fog.Color[BCOMP] * 255.0;
179 }
180 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
181 rFog = ctx->Fog.Color[RCOMP] * 65535.0;
182 gFog = ctx->Fog.Color[GCOMP] * 65535.0;
183 bFog = ctx->Fog.Color[BCOMP] * 65535.0;
184 }
185 else {
186 rFog = ctx->Fog.Color[RCOMP];
187 gFog = ctx->Fog.Color[GCOMP];
188 bFog = ctx->Fog.Color[BCOMP];
189 }
190
191 if (swrast->_PreferPixelFog) {
192 /* The span's fog values are fog coordinates, now compute blend factors
193 * and blend the fragment colors with the fog color.
194 */
195 switch (swrast->_FogMode) {
196 case GL_LINEAR:
197 {
198 const GLfloat fogEnd = ctx->Fog.End;
199 const GLfloat fogScale = (ctx->Fog.Start == ctx->Fog.End)
200 ? 1.0F : 1.0F / (ctx->Fog.End - ctx->Fog.Start);
201 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
202 GLubyte (*rgba)[4] = span->array->rgba8;
203 FOG_LOOP(GLubyte, LINEAR_FOG);
204 }
205 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
206 GLushort (*rgba)[4] = span->array->rgba16;
207 FOG_LOOP(GLushort, LINEAR_FOG);
208 }
209 else {
210 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
211 ASSERT(span->array->ChanType == GL_FLOAT);
212 FOG_LOOP(GLfloat, LINEAR_FOG);
213 }
214 }
215 break;
216
217 case GL_EXP:
218 {
219 const GLfloat density = -ctx->Fog.Density;
220 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
221 GLubyte (*rgba)[4] = span->array->rgba8;
222 FOG_LOOP(GLubyte, EXP_FOG);
223 }
224 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
225 GLushort (*rgba)[4] = span->array->rgba16;
226 FOG_LOOP(GLushort, EXP_FOG);
227 }
228 else {
229 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
230 ASSERT(span->array->ChanType == GL_FLOAT);
231 FOG_LOOP(GLfloat, EXP_FOG);
232 }
233 }
234 break;
235
236 case GL_EXP2:
237 {
238 const GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
239 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
240 GLubyte (*rgba)[4] = span->array->rgba8;
241 FOG_LOOP(GLubyte, EXP2_FOG);
242 }
243 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
244 GLushort (*rgba)[4] = span->array->rgba16;
245 FOG_LOOP(GLushort, EXP2_FOG);
246 }
247 else {
248 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
249 ASSERT(span->array->ChanType == GL_FLOAT);
250 FOG_LOOP(GLfloat, EXP2_FOG);
251 }
252 }
253 break;
254
255 default:
256 _mesa_problem(ctx, "Bad fog mode in _swrast_fog_rgba_span");
257 return;
258 }
259 }
260 else {
261 /* The span's fog start/step/array values are blend factors in [0,1].
262 * They were previously computed per-vertex.
263 */
264 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
265 GLubyte (*rgba)[4] = span->array->rgba8;
266 FOG_LOOP(GLubyte, BLEND_FOG);
267 }
268 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
269 GLushort (*rgba)[4] = span->array->rgba16;
270 FOG_LOOP(GLushort, BLEND_FOG);
271 }
272 else {
273 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0];
274 ASSERT(span->array->ChanType == GL_FLOAT);
275 FOG_LOOP(GLfloat, BLEND_FOG);
276 }
277 }
278 }
279
280
281 /**
282 * As above, but color index mode.
283 */
284 void
285 _swrast_fog_ci_span( const GLcontext *ctx, SWspan *span )
286 {
287 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
288 const GLuint fogIndex = (GLuint) ctx->Fog.Index;
289 GLuint *index = span->array->index;
290
291 ASSERT(swrast->_FogEnabled);
292 ASSERT(span->arrayMask & SPAN_INDEX);
293
294 /* we need to compute fog blend factors */
295 if (swrast->_PreferPixelFog) {
296 /* The span's fog values are fog coordinates, now compute blend factors
297 * and blend the fragment colors with the fog color.
298 */
299 switch (ctx->Fog.Mode) {
300 case GL_LINEAR:
301 {
302 const GLfloat fogEnd = ctx->Fog.End;
303 const GLfloat fogScale = (ctx->Fog.Start == ctx->Fog.End)
304 ? 1.0F : 1.0F / (ctx->Fog.End - ctx->Fog.Start);
305 FOG_LOOP_CI(LINEAR_FOG);
306 }
307 break;
308 case GL_EXP:
309 {
310 const GLfloat density = -ctx->Fog.Density;
311 FOG_LOOP_CI(EXP_FOG);
312 }
313 break;
314 case GL_EXP2:
315 {
316 const GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
317 FOG_LOOP_CI(EXP2_FOG);
318 }
319 break;
320 default:
321 _mesa_problem(ctx, "Bad fog mode in _swrast_fog_ci_span");
322 return;
323 }
324 }
325 else {
326 /* The span's fog start/step/array values are blend factors in [0,1].
327 * They were previously computed per-vertex.
328 */
329 FOG_LOOP_CI(BLEND_FOG);
330 }
331 }