da36b3d795fe0283f87ffdf911298e15b9747a38
[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 ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP 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
39 #include "macros.h"
40
41 static GLint convertFtToFogFt( GLfloat dwInValue );
42 static GLint doFPtoFixedNoRound( GLfloat dwInValue, int nFraction );
43
44 void
45 sisDDFogfv( GLcontext *ctx, GLenum pname, const GLfloat *params )
46 {
47 sisContextPtr smesa = SIS_CONTEXT(ctx);
48 __GLSiSHardware *prev = &smesa->prev;
49 __GLSiSHardware *current = &smesa->current;
50
51 float fArg;
52 GLint fogColor;
53
54 switch (pname)
55 {
56 case GL_FOG_MODE:
57 current->hwFog &= ~MASK_FogMode;
58 switch (ctx->Fog.Mode)
59 {
60 case GL_LINEAR:
61 current->hwFog |= FOGMODE_LINEAR;
62 break;
63 case GL_EXP:
64 current->hwFog |= FOGMODE_EXP;
65 break;
66 case GL_EXP2:
67 current->hwFog |= FOGMODE_EXP2;
68 break;
69 }
70 if (current->hwFog != prev->hwFog) {
71 prev->hwFog = current->hwFog;
72 smesa->GlobalFlag |= GFLAG_FOGSETTING;
73 }
74 break;
75 case GL_FOG_DENSITY:
76 current->hwFogDensity = convertFtToFogFt( ctx->Fog.Density );
77 if (current->hwFogDensity != prev->hwFogDensity) {
78 prev->hwFogDensity = current->hwFogDensity;
79 smesa->GlobalFlag |= GFLAG_FOGSETTING;
80 }
81 break;
82 case GL_FOG_START:
83 case GL_FOG_END:
84 fArg = 1.0 / (ctx->Fog.End - ctx->Fog.Start);
85 current->hwFogInverse = doFPtoFixedNoRound( fArg, 10 );
86 if (pname == GL_FOG_END)
87 {
88 if (smesa->Chipset == PCI_CHIP_SIS300)
89 current->hwFogFar = doFPtoFixedNoRound( ctx->Fog.End, 10 );
90 else
91 current->hwFogFar = doFPtoFixedNoRound( ctx->Fog.End, 6 );
92 }
93 if (current->hwFogFar != prev->hwFogFar ||
94 current->hwFogInverse != prev->hwFogInverse)
95 {
96 prev->hwFogFar = current->hwFogFar;
97 prev->hwFogInverse = current->hwFogInverse;
98 smesa->GlobalFlag |= GFLAG_FOGSETTING;
99 }
100 break;
101 case GL_FOG_INDEX:
102 /* TODO */
103 break;
104 case GL_FOG_COLOR:
105 fogColor = FLOAT_TO_UBYTE( ctx->Fog.Color[0] ) << 16;
106 fogColor |= FLOAT_TO_UBYTE( ctx->Fog.Color[1] ) << 8;
107 fogColor |= FLOAT_TO_UBYTE( ctx->Fog.Color[2] );
108 current->hwFog &= 0xff000000;
109 current->hwFog |= fogColor;
110 if (current->hwFog != prev->hwFog) {
111 prev->hwFog = current->hwFog;
112 smesa->GlobalFlag |= GFLAG_FOGSETTING;
113 }
114 break;
115 }
116 }
117
118 static GLint
119 doFPtoFixedNoRound( GLfloat dwInValue, int nFraction )
120 {
121 GLint dwMantissa;
122 int nTemp;
123 union { int i; float f; } u;
124 GLint val;
125
126 u.f = dwInValue;
127 val = u.i;
128
129 if (val == 0)
130 return 0;
131 nTemp = (int) (val & 0x7F800000) >> 23;
132 nTemp = nTemp - 127 + nFraction - 23;
133 dwMantissa = (val & 0x007FFFFF) | 0x00800000;
134
135 if (nTemp < -25)
136 return 0;
137 if (nTemp > 0)
138 dwMantissa <<= nTemp;
139 else {
140 nTemp = -nTemp;
141 dwMantissa >>= nTemp;
142 }
143 if (val & 0x80000000)
144 dwMantissa = ~dwMantissa + 1;
145 return dwMantissa;
146 }
147
148 /* s[8].23->s[7].10 */
149 static GLint
150 convertFtToFogFt( GLfloat dwInValue )
151 {
152 GLint dwMantissa, dwExp;
153 GLint dwRet;
154 union { int i; float f; } u;
155 GLint val;
156
157 u.f = dwInValue;
158 val = u.i;
159
160 if (val == 0)
161 return 0;
162
163 /* ----- Standard float Format: s[8].23 -----
164 * ----- = (-1)^S * 2^(E - 127) * (1 + M / 2^23) -----
165 * ----- = (-1)^S * 2^((E-63) - 64) * (1 + (M/2^13) / 2^10) -----
166 * ----- Density float Format: s[7].10 -----
167 * ----- New Exponential = E - 63 -----
168 * ----- New Mantissa = M / 2^13 -----
169 * ----- -----
170 */
171
172 dwExp = (val & 0x7F800000) >> 23;
173 dwExp -= 63;
174
175 if (dwExp < 0)
176 return 0;
177
178 if (dwExp <= 0x7F)
179 dwMantissa = (val & 0x007FFFFF) >> (23 - 10);
180 else {
181 /* ----- To Return +Max(or -Max) ----- */
182 dwExp = 0x7F;
183 dwMantissa = 0x3FF;
184 }
185
186 dwRet = (val & 0x80000000) >> (31 - 17); /* Shift Sign Bit */
187
188 dwRet |= (dwExp << 10) | dwMantissa;
189
190 return dwRet;
191 }