8daa9a0f5e3ea23fbd5b611fdfdf6bd20c9cfefd
[mesa.git] / src / mesa / drivers / dri / sis / sis_fog.c
1 /**************************************************************************
2
3 Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
4 Copyright 2003 Eric Anholt
5 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 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
16 Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 ATI, PRECISION INSIGHT AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27 /* $XFree86: xc/lib/GL/mesa/src/drv/sis/sis_fog.c,v 1.3 2000/09/26 15:56:48 tsi Exp $ */
28
29 /*
30 * Authors:
31 * Sung-Ching Lin <sclin@sis.com.tw>
32 * Eric Anholt <anholt@FreeBSD.org>
33 */
34
35 #include "sis_context.h"
36 #include "sis_state.h"
37 #include "swrast/swrast.h"
38 #include "macros.h"
39
40 static GLint convertFtToFogFt( GLfloat dwInValue );
41
42 void
43 sisDDFogfv( GLcontext *ctx, GLenum pname, const GLfloat *params )
44 {
45 sisContextPtr smesa = SIS_CONTEXT(ctx);
46 __GLSiSHardware *current = &smesa->current;
47
48 float fArg;
49 GLint fogColor;
50
51 switch (pname)
52 {
53 case GL_FOG_MODE:
54 current->hwFog &= ~MASK_FogMode;
55 switch (ctx->Fog.Mode)
56 {
57 case GL_LINEAR:
58 current->hwFog |= FOGMODE_LINEAR;
59 _swrast_allow_pixel_fog( ctx, GL_TRUE );
60 _swrast_allow_vertex_fog( ctx, GL_FALSE );
61 break;
62 case GL_EXP:
63 if (ctx->Hint.Fog == GL_NICEST || smesa->AGPCmdModeEnabled) {
64 current->hwFog |= FOGMODE_EXP;
65 _swrast_allow_pixel_fog( ctx, GL_TRUE );
66 _swrast_allow_vertex_fog( ctx, GL_FALSE );
67 } else { /* GL_DONTCARE or GL_FASTEST */
68 current->hwFog |= FOGMODE_CHEAP;
69 _swrast_allow_pixel_fog( ctx, GL_FALSE );
70 _swrast_allow_vertex_fog( ctx, GL_TRUE );
71 }
72 break;
73 case GL_EXP2:
74 current->hwFog |= FOGMODE_EXP2;
75 _swrast_allow_pixel_fog( ctx, GL_TRUE );
76 _swrast_allow_vertex_fog( ctx, GL_FALSE );
77 break;
78 }
79 break;
80 case GL_FOG_DENSITY:
81 current->hwFogDensity = convertFtToFogFt( ctx->Fog.Density );
82 break;
83 case GL_FOG_START:
84 case GL_FOG_END:
85 fArg = 1.0 / (ctx->Fog.End - ctx->Fog.Start);
86 current->hwFogInverse = doFPtoFixedNoRound( fArg, 10 );
87 if (pname == GL_FOG_END)
88 {
89 if (smesa->Chipset == PCI_CHIP_SIS300)
90 current->hwFogFar = doFPtoFixedNoRound( ctx->Fog.End, 10 );
91 else
92 current->hwFogFar = doFPtoFixedNoRound( ctx->Fog.End, 6 );
93 }
94 break;
95 case GL_FOG_INDEX:
96 /* TODO */
97 break;
98 case GL_FOG_COLOR:
99 fogColor = FLOAT_TO_UBYTE( ctx->Fog.Color[0] ) << 16;
100 fogColor |= FLOAT_TO_UBYTE( ctx->Fog.Color[1] ) << 8;
101 fogColor |= FLOAT_TO_UBYTE( ctx->Fog.Color[2] );
102 current->hwFog &= 0xff000000;
103 current->hwFog |= fogColor;
104 break;
105 }
106 }
107
108 GLint
109 doFPtoFixedNoRound( GLfloat dwInValue, int nFraction )
110 {
111 GLint dwMantissa;
112 int nTemp;
113 union { int i; float f; } u;
114 GLint val;
115
116 u.f = dwInValue;
117 val = u.i;
118
119 if (val == 0)
120 return 0;
121 nTemp = (int) (val & 0x7F800000) >> 23;
122 nTemp = nTemp - 127 + nFraction - 23;
123 dwMantissa = (val & 0x007FFFFF) | 0x00800000;
124
125 if (nTemp < -25)
126 return 0;
127 if (nTemp > 0)
128 dwMantissa <<= nTemp;
129 else {
130 nTemp = -nTemp;
131 dwMantissa >>= nTemp;
132 }
133 if (val & 0x80000000)
134 dwMantissa = ~dwMantissa + 1;
135 return dwMantissa;
136 }
137
138 /* s[8].23->s[7].10 */
139 static GLint
140 convertFtToFogFt( GLfloat dwInValue )
141 {
142 GLint dwMantissa, dwExp;
143 GLint dwRet;
144 union { int i; float f; } u;
145 GLint val;
146
147 u.f = dwInValue;
148 val = u.i;
149
150 if (val == 0)
151 return 0;
152
153 /* ----- Standard float Format: s[8].23 -----
154 * ----- = (-1)^S * 2^(E - 127) * (1 + M / 2^23) -----
155 * ----- = (-1)^S * 2^((E-63) - 64) * (1 + (M/2^13) / 2^10) -----
156 * ----- Density float Format: s[7].10 -----
157 * ----- New Exponential = E - 63 -----
158 * ----- New Mantissa = M / 2^13 -----
159 * ----- -----
160 */
161
162 dwExp = (val & 0x7F800000) >> 23;
163 dwExp -= 63;
164
165 if (dwExp < 0)
166 return 0;
167
168 if (dwExp <= 0x7F)
169 dwMantissa = (val & 0x007FFFFF) >> (23 - 10);
170 else {
171 /* ----- To Return +Max(or -Max) ----- */
172 dwExp = 0x7F;
173 dwMantissa = 0x3FF;
174 }
175
176 dwRet = (val & 0x80000000) >> (31 - 17); /* Shift Sign Bit */
177
178 dwRet |= (dwExp << 10) | dwMantissa;
179
180 return dwRet;
181 }