1 /**************************************************************************
3 Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
4 Copyright 2003 Eric Anholt
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:
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
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.
26 **************************************************************************/
27 /* $XFree86: xc/lib/GL/mesa/src/drv/sis/sis_fog.c,v 1.3 2000/09/26 15:56:48 tsi Exp $ */
31 * Sung-Ching Lin <sclin@sis.com.tw>
32 * Eric Anholt <anholt@FreeBSD.org>
35 #include "sis_context.h"
36 #include "sis_state.h"
37 #include "swrast/swrast.h"
41 static GLint
convertFtToFogFt( GLfloat dwInValue
);
42 static GLint
doFPtoFixedNoRound( GLfloat dwInValue
, int nFraction
);
45 sisDDFogfv( GLcontext
*ctx
, GLenum pname
, const GLfloat
*params
)
47 sisContextPtr smesa
= SIS_CONTEXT(ctx
);
48 __GLSiSHardware
*prev
= &smesa
->prev
;
49 __GLSiSHardware
*current
= &smesa
->current
;
56 case GL_FOG_COORDINATE_SOURCE_EXT
:
57 current
->hwFog
&= ~MASK_FogMode
;
58 switch (ctx
->Fog
.FogCoordinateSource
)
60 case GL_FOG_COORDINATE_EXT
:
61 current
->hwFog
&= ~MASK_FogZLookup
;
63 case GL_FRAGMENT_DEPTH_EXT
:
64 current
->hwFog
|= MASK_FogZLookup
;
67 if (current
->hwFog
!= prev
->hwFog
) {
68 prev
->hwFog
= current
->hwFog
;
69 smesa
->GlobalFlag
|= GFLAG_FOGSETTING
;
73 current
->hwFog
&= ~MASK_FogMode
;
74 switch (ctx
->Fog
.Mode
)
77 current
->hwFog
|= FOGMODE_LINEAR
;
80 current
->hwFog
|= FOGMODE_EXP
;
83 current
->hwFog
|= FOGMODE_EXP2
;
86 if (current
->hwFog
!= prev
->hwFog
) {
87 prev
->hwFog
= current
->hwFog
;
88 smesa
->GlobalFlag
|= GFLAG_FOGSETTING
;
92 current
->hwFogDensity
= convertFtToFogFt( ctx
->Fog
.Density
);
93 if (current
->hwFogDensity
!= prev
->hwFogDensity
) {
94 prev
->hwFogDensity
= current
->hwFogDensity
;
95 smesa
->GlobalFlag
|= GFLAG_FOGSETTING
;
100 fArg
= 1.0 / (ctx
->Fog
.End
- ctx
->Fog
.Start
);
101 current
->hwFogInverse
= doFPtoFixedNoRound( fArg
, 10 );
102 if (pname
== GL_FOG_END
)
104 if (smesa
->Chipset
== PCI_CHIP_SIS300
)
105 current
->hwFogFar
= doFPtoFixedNoRound( ctx
->Fog
.End
, 10 );
107 current
->hwFogFar
= doFPtoFixedNoRound( ctx
->Fog
.End
, 6 );
109 if (current
->hwFogFar
!= prev
->hwFogFar
||
110 current
->hwFogInverse
!= prev
->hwFogInverse
)
112 prev
->hwFogFar
= current
->hwFogFar
;
113 prev
->hwFogInverse
= current
->hwFogInverse
;
114 smesa
->GlobalFlag
|= GFLAG_FOGSETTING
;
121 fogColor
= FLOAT_TO_UBYTE( ctx
->Fog
.Color
[0] ) << 16;
122 fogColor
|= FLOAT_TO_UBYTE( ctx
->Fog
.Color
[1] ) << 8;
123 fogColor
|= FLOAT_TO_UBYTE( ctx
->Fog
.Color
[2] );
124 current
->hwFog
&= 0xff000000;
125 current
->hwFog
|= fogColor
;
126 if (current
->hwFog
!= prev
->hwFog
) {
127 prev
->hwFog
= current
->hwFog
;
128 smesa
->GlobalFlag
|= GFLAG_FOGSETTING
;
135 doFPtoFixedNoRound( GLfloat dwInValue
, int nFraction
)
139 union { int i
; float f
; } u
;
147 nTemp
= (int) (val
& 0x7F800000) >> 23;
148 nTemp
= nTemp
- 127 + nFraction
- 23;
149 dwMantissa
= (val
& 0x007FFFFF) | 0x00800000;
154 dwMantissa
<<= nTemp
;
157 dwMantissa
>>= nTemp
;
159 if (val
& 0x80000000)
160 dwMantissa
= ~dwMantissa
+ 1;
164 /* s[8].23->s[7].10 */
166 convertFtToFogFt( GLfloat dwInValue
)
168 GLint dwMantissa
, dwExp
;
170 union { int i
; float f
; } u
;
179 /* ----- Standard float Format: s[8].23 -----
180 * ----- = (-1)^S * 2^(E - 127) * (1 + M / 2^23) -----
181 * ----- = (-1)^S * 2^((E-63) - 64) * (1 + (M/2^13) / 2^10) -----
182 * ----- Density float Format: s[7].10 -----
183 * ----- New Exponential = E - 63 -----
184 * ----- New Mantissa = M / 2^13 -----
188 dwExp
= (val
& 0x7F800000) >> 23;
195 dwMantissa
= (val
& 0x007FFFFF) >> (23 - 10);
197 /* ----- To Return +Max(or -Max) ----- */
202 dwRet
= (val
& 0x80000000) >> (31 - 17); /* Shift Sign Bit */
204 dwRet
|= (dwExp
<< 10) | dwMantissa
;