include texobj.h to silence warnings
[mesa.git] / src / mesa / drivers / dri / gamma / gamma_tex.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/gamma/gamma_tex.c,v 1.4 2002/11/05 17:46:07 tsi Exp $ */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 #include "glheader.h"
7 #include "mtypes.h"
8 #include "imports.h"
9 #include "simple_list.h"
10 #include "enums.h"
11 #include "texstore.h"
12 #include "teximage.h"
13 #include "texformat.h"
14 #include "texobj.h"
15 #include "swrast/swrast.h"
16
17 #include "mm.h"
18 #include "gamma_context.h"
19 #include "colormac.h"
20
21
22 /*
23 * Compute the 'S2.4' lod bias factor from the floating point OpenGL bias.
24 */
25 #if 0
26 static GLuint gammaComputeLodBias(GLfloat bias)
27 {
28 return bias;
29 }
30 #endif
31
32 static void gammaSetTexWrapping(gammaTextureObjectPtr t,
33 GLenum wraps, GLenum wrapt)
34 {
35 CARD32 t1 = t->TextureAddressMode;
36 CARD32 t2 = t->TextureReadMode;
37
38 t1 &= ~(TAM_SWrap_Mask | TAM_TWrap_Mask);
39 t2 &= ~(TRM_UWrap_Mask | TRM_VWrap_Mask);
40
41 if (wraps != GL_CLAMP) {
42 t1 |= TAM_SWrap_Repeat;
43 t2 |= TRM_UWrap_Repeat;
44 }
45
46 if (wrapt != GL_CLAMP) {
47 t1 |= TAM_TWrap_Repeat;
48 t2 |= TRM_VWrap_Repeat;
49 }
50
51 t->TextureAddressMode = t1;
52 t->TextureReadMode = t2;
53 }
54
55
56 static void gammaSetTexFilter(gammaContextPtr gmesa,
57 gammaTextureObjectPtr t,
58 GLenum minf, GLenum magf,
59 GLfloat bias)
60 {
61 CARD32 t1 = t->TextureAddressMode;
62 CARD32 t2 = t->TextureReadMode;
63
64 t2 &= ~(TRM_Mag_Mask | TRM_Min_Mask);
65
66 switch (minf) {
67 case GL_NEAREST:
68 t1 &= ~TAM_LODEnable;
69 t2 &= ~TRM_MipMapEnable;
70 t2 |= TRM_Min_Nearest;
71 break;
72 case GL_LINEAR:
73 t1 &= ~TAM_LODEnable;
74 t2 &= ~TRM_MipMapEnable;
75 t2 |= TRM_Min_Linear;
76 break;
77 case GL_NEAREST_MIPMAP_NEAREST:
78 t2 |= TRM_Min_NearestMMNearest;
79 break;
80 case GL_LINEAR_MIPMAP_NEAREST:
81 t2 |= TRM_Min_LinearMMNearest;
82 break;
83 case GL_NEAREST_MIPMAP_LINEAR:
84 t2 |= TRM_Min_NearestMMLinear;
85 break;
86 case GL_LINEAR_MIPMAP_LINEAR:
87 t2 |= TRM_Min_LinearMMLinear;
88 break;
89 default:
90 break;
91 }
92
93 switch (magf) {
94 case GL_NEAREST:
95 t2 |= TRM_Mag_Nearest;
96 break;
97 case GL_LINEAR:
98 t2 |= TRM_Mag_Linear;
99 break;
100 default:
101 break;
102 }
103
104 t->TextureAddressMode = t1;
105 t->TextureReadMode = t2;
106 }
107
108
109 static void gammaSetTexBorderColor(gammaContextPtr gmesa,
110 gammaTextureObjectPtr t,
111 GLubyte color[4])
112 {
113 t->TextureBorderColor = PACK_COLOR_8888(color[0], color[1], color[2], color[3]);
114 }
115
116
117 static void gammaTexParameter( GLcontext *ctx, GLenum target,
118 struct gl_texture_object *tObj,
119 GLenum pname, const GLfloat *params )
120 {
121 gammaContextPtr gmesa = GAMMA_CONTEXT(ctx);
122 gammaTextureObjectPtr t = (gammaTextureObjectPtr) tObj->DriverData;
123 if (!t)
124 return;
125
126 /* Can't do the update now as we don't know whether to flush
127 * vertices or not. Setting gmesa->new_state means that
128 * gammaUpdateTextureState() will be called before any triangles are
129 * rendered. If a statechange has occurred, it will be detected at
130 * that point, and buffered vertices flushed.
131 */
132 switch (pname) {
133 case GL_TEXTURE_MIN_FILTER:
134 case GL_TEXTURE_MAG_FILTER:
135 {
136 GLfloat bias = ctx->Texture.Unit[ctx->Texture.CurrentUnit].LodBias;
137 gammaSetTexFilter( gmesa, t, tObj->MinFilter, tObj->MagFilter, bias );
138 }
139 break;
140
141 case GL_TEXTURE_WRAP_S:
142 case GL_TEXTURE_WRAP_T:
143 gammaSetTexWrapping( t, tObj->WrapS, tObj->WrapT );
144 break;
145
146 case GL_TEXTURE_BORDER_COLOR:
147 gammaSetTexBorderColor( gmesa, t, tObj->_BorderChan );
148 break;
149
150 case GL_TEXTURE_BASE_LEVEL:
151 case GL_TEXTURE_MAX_LEVEL:
152 case GL_TEXTURE_MIN_LOD:
153 case GL_TEXTURE_MAX_LOD:
154 /* This isn't the most efficient solution but there doesn't appear to
155 * be a nice alternative for Radeon. Since there's no LOD clamping,
156 * we just have to rely on loading the right subset of mipmap levels
157 * to simulate a clamped LOD.
158 */
159 gammaSwapOutTexObj( gmesa, t );
160 break;
161
162 default:
163 return;
164 }
165
166 if (t == gmesa->CurrentTexObj[0])
167 gmesa->dirty |= GAMMA_UPLOAD_TEX0;
168
169 #if 0
170 if (t == gmesa->CurrentTexObj[1]) {
171 gmesa->dirty |= GAMMA_UPLOAD_TEX1;
172 }
173 #endif
174 }
175
176
177 static void gammaTexEnv( GLcontext *ctx, GLenum target,
178 GLenum pname, const GLfloat *param )
179 {
180 gammaContextPtr gmesa = GAMMA_CONTEXT( ctx );
181 GLuint unit = ctx->Texture.CurrentUnit;
182
183 /* Only one env color. Need a fallback if env colors are different
184 * and texture setup references env color in both units.
185 */
186 switch (pname) {
187 case GL_TEXTURE_ENV_COLOR: {
188 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
189 GLfloat *fc = texUnit->EnvColor;
190 GLuint r, g, b, a, col;
191 CLAMPED_FLOAT_TO_UBYTE(r, fc[0]);
192 CLAMPED_FLOAT_TO_UBYTE(g, fc[1]);
193 CLAMPED_FLOAT_TO_UBYTE(b, fc[2]);
194 CLAMPED_FLOAT_TO_UBYTE(a, fc[3]);
195
196 col = ((a << 24) |
197 (r << 16) |
198 (g << 8) |
199 (b << 0));
200
201 break;
202 }
203 case GL_TEXTURE_ENV_MODE:
204 gmesa->TexEnvImageFmt[unit] = 0; /* force recalc of env state */
205 break;
206
207 case GL_TEXTURE_LOD_BIAS_EXT:
208 #if 0 /* ?!?!?! */
209 {
210 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
211 gammaTextureObjectPtr t = (gammaTextureObjectPtr) tObj->DriverData;
212 (void) t;
213 /* XXX Looks like there's something missing here */
214 }
215 #endif
216 break;
217
218 default:
219 break;
220 }
221 }
222
223 #if 0
224 static void gammaTexImage1D( GLcontext *ctx, GLenum target, GLint level,
225 GLint internalFormat,
226 GLint width, GLint border,
227 GLenum format, GLenum type,
228 const GLvoid *pixels,
229 const struct gl_pixelstore_attrib *pack,
230 struct gl_texture_object *texObj,
231 struct gl_texture_image *texImage )
232 {
233 gammaTextureObjectPtr t = (gammaTextureObjectPtr) texObj->DriverData;
234 if (t) {
235 gammaSwapOutTexObj( GAMMA_CONTEXT(ctx), t );
236 }
237 _mesa_store_teximage1d( ctx, target, level, internalFormat,
238 width, border, format, type,
239 pixels, pack, texObj, texImage );
240 }
241 #endif
242
243 #if 0
244 static void gammaTexSubImage1D( GLcontext *ctx,
245 GLenum target,
246 GLint level,
247 GLint xoffset,
248 GLsizei width,
249 GLenum format, GLenum type,
250 const GLvoid *pixels,
251 const struct gl_pixelstore_attrib *pack,
252 struct gl_texture_object *texObj,
253 struct gl_texture_image *texImage )
254 {
255 gammaTextureObjectPtr t = (gammaTextureObjectPtr) texObj->DriverData;
256 if (t) {
257 gammaSwapOutTexObj( GAMMA_CONTEXT(ctx), t );
258 }
259 _mesa_store_texsubimage1d(ctx, target, level, xoffset, width,
260 format, type, pixels, pack, texObj,
261 texImage);
262 }
263 #endif
264
265 static void gammaTexImage2D( GLcontext *ctx, GLenum target, GLint level,
266 GLint internalFormat,
267 GLint width, GLint height, GLint border,
268 GLenum format, GLenum type, const GLvoid *pixels,
269 const struct gl_pixelstore_attrib *packing,
270 struct gl_texture_object *texObj,
271 struct gl_texture_image *texImage )
272 {
273 gammaTextureObjectPtr t = (gammaTextureObjectPtr) texObj->DriverData;
274 if (t) {
275 gammaSwapOutTexObj( GAMMA_CONTEXT(ctx), t );
276 }
277 _mesa_store_teximage2d( ctx, target, level, internalFormat,
278 width, height, border, format, type,
279 pixels, packing, texObj, texImage );
280 }
281
282 static void gammaTexSubImage2D( GLcontext *ctx,
283 GLenum target,
284 GLint level,
285 GLint xoffset, GLint yoffset,
286 GLsizei width, GLsizei height,
287 GLenum format, GLenum type,
288 const GLvoid *pixels,
289 const struct gl_pixelstore_attrib *packing,
290 struct gl_texture_object *texObj,
291 struct gl_texture_image *texImage )
292 {
293 gammaTextureObjectPtr t = (gammaTextureObjectPtr) texObj->DriverData;
294 if (t) {
295 gammaSwapOutTexObj( GAMMA_CONTEXT(ctx), t );
296 }
297 _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset, width,
298 height, format, type, pixels, packing, texObj,
299 texImage);
300 }
301
302
303 static void gammaBindTexture( GLcontext *ctx, GLenum target,
304 struct gl_texture_object *tObj )
305 {
306 gammaContextPtr gmesa = GAMMA_CONTEXT( ctx );
307 gammaTextureObjectPtr t = (gammaTextureObjectPtr) tObj->DriverData;
308
309 if (!t) {
310 GLfloat bias = ctx->Texture.Unit[ctx->Texture.CurrentUnit].LodBias;
311 t = CALLOC_STRUCT(gamma_texture_object_t);
312
313 /* Initialize non-image-dependent parts of the state:
314 */
315 t->globj = tObj;
316
317 t->TextureAddressMode = TextureAddressModeEnable | TAM_Operation_3D |
318 TAM_DY_Enable | TAM_LODEnable;
319 t->TextureReadMode = TextureReadModeEnable | TRM_PrimaryCacheEnable |
320 TRM_MipMapEnable | TRM_BorderClamp | TRM_Border;
321 t->TextureColorMode = TextureColorModeEnable;
322 t->TextureFilterMode = TextureFilterModeEnable;
323
324 if (target == GL_TEXTURE_2D) {
325 t->TextureAddressMode |= TAM_TexMapType_2D;
326 t->TextureReadMode |= TRM_TexMapType_2D;
327 } else
328 if (target == GL_TEXTURE_1D) {
329 t->TextureAddressMode |= TAM_TexMapType_1D;
330 t->TextureReadMode |= TRM_TexMapType_1D;
331 }
332
333 t->TextureColorMode = TextureColorModeEnable;
334
335 t->TextureFilterMode = TextureFilterModeEnable;
336
337 #ifdef MESA_LITTLE_ENDIAN
338 t->TextureFormat = (TF_LittleEndian |
339 #else
340 t->TextureFormat = (TF_BigEndian |
341 #endif
342 TF_ColorOrder_RGB |
343 TF_OutputFmt_Texel);
344
345 t->dirty_images = ~0;
346
347 tObj->DriverData = t;
348 make_empty_list( t );
349
350 gammaSetTexWrapping( t, tObj->WrapS, tObj->WrapT );
351 gammaSetTexFilter( gmesa, t, tObj->MinFilter, tObj->MagFilter, bias );
352 gammaSetTexBorderColor( gmesa, t, tObj->_BorderChan );
353 }
354 }
355
356
357 static void gammaDeleteTexture( GLcontext *ctx, struct gl_texture_object *tObj )
358 {
359 gammaTextureObjectPtr t = (gammaTextureObjectPtr)tObj->DriverData;
360
361 if (t) {
362 gammaContextPtr gmesa = GAMMA_CONTEXT( ctx );
363 #if 0
364 if (gmesa)
365 GAMMA_FIREVERTICES( gmesa );
366 #endif
367 gammaDestroyTexObj( gmesa, t );
368 tObj->DriverData = 0;
369 }
370 /* Free mipmap images and the texture object itself */
371 _mesa_delete_texture_object(ctx, tObj);
372 }
373
374 static GLboolean gammaIsTextureResident( GLcontext *ctx,
375 struct gl_texture_object *tObj )
376 {
377 gammaTextureObjectPtr t = (gammaTextureObjectPtr)tObj->DriverData;
378 return t && t->MemBlock;
379 }
380
381 static void gammaInitTextureObjects( GLcontext *ctx )
382 {
383 struct gl_texture_object *texObj;
384 GLuint tmp = ctx->Texture.CurrentUnit;
385
386 ctx->Texture.CurrentUnit = 0;
387
388 texObj = ctx->Texture.Unit[0].Current1D;
389 gammaBindTexture( ctx, GL_TEXTURE_1D, texObj );
390
391 texObj = ctx->Texture.Unit[0].Current2D;
392 gammaBindTexture( ctx, GL_TEXTURE_2D, texObj );
393
394 #if 0
395 ctx->Texture.CurrentUnit = 1;
396
397 texObj = ctx->Texture.Unit[1].Current1D;
398 gammaBindTexture( ctx, GL_TEXTURE_1D, texObj );
399
400 texObj = ctx->Texture.Unit[1].Current2D;
401 gammaBindTexture( ctx, GL_TEXTURE_2D, texObj );
402 #endif
403
404 ctx->Texture.CurrentUnit = tmp;
405 }
406
407
408 void gammaDDInitTextureFuncs( GLcontext *ctx )
409 {
410 ctx->Driver.TexEnv = gammaTexEnv;
411 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
412 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
413 ctx->Driver.TexImage2D = gammaTexImage2D;
414 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
415 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
416 ctx->Driver.TexSubImage2D = gammaTexSubImage2D;
417 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
418 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
419 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
420 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
421 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
422 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
423 ctx->Driver.BindTexture = gammaBindTexture;
424 ctx->Driver.DeleteTexture = gammaDeleteTexture;
425 ctx->Driver.TexParameter = gammaTexParameter;
426 ctx->Driver.UpdateTexturePalette = 0;
427 ctx->Driver.IsTextureResident = gammaIsTextureResident;
428 ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
429
430 gammaInitTextureObjects( ctx );
431 }