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