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