1 /* -*- mode: c; c-basic-offset: 3 -*- */
3 * Copyright 2000 Gareth Hughes
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * GARETH HUGHES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Gareth Hughes <gareth@valinux.com>
28 * Leif Delgass <ldelgass@retinalburn.net>
29 * José Fonseca <j_r_fonseca@yahoo.co.uk>
32 #include "mach64_context.h"
33 #include "mach64_ioctl.h"
34 #include "mach64_state.h"
35 #include "mach64_vb.h"
36 #include "mach64_tris.h"
37 #include "mach64_tex.h"
39 #include "main/context.h"
40 #include "main/macros.h"
41 #include "main/simple_list.h"
42 #include "main/enums.h"
43 #include "main/texstore.h"
44 #include "main/texformat.h"
45 #include "main/teximage.h"
46 #include "main/texobj.h"
47 #include "main/imports.h"
50 static void mach64SetTexWrap( mach64TexObjPtr t
,
51 GLenum swrap
, GLenum twrap
)
55 case GL_CLAMP_TO_EDGE
:
56 case GL_CLAMP_TO_BORDER
:
66 case GL_CLAMP_TO_EDGE
:
67 case GL_CLAMP_TO_BORDER
:
76 static void mach64SetTexFilter( mach64TexObjPtr t
,
77 GLenum minf
, GLenum magf
)
81 case GL_NEAREST_MIPMAP_NEAREST
:
82 case GL_NEAREST_MIPMAP_LINEAR
:
83 t
->BilinearMin
= GL_FALSE
;
86 case GL_LINEAR_MIPMAP_NEAREST
:
87 case GL_LINEAR_MIPMAP_LINEAR
:
88 t
->BilinearMin
= GL_TRUE
;
94 t
->BilinearMag
= GL_FALSE
;
97 t
->BilinearMag
= GL_TRUE
;
102 static void mach64SetTexBorderColor( mach64TexObjPtr t
, GLubyte c
[4] )
105 GLuint border
= mach64PackColor( 4, c
[0], c
[1], c
[2], c
[3] );
110 static mach64TexObjPtr
111 mach64AllocTexObj( struct gl_texture_object
*texObj
)
115 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
)
116 fprintf( stderr
, "%s( %p )\n", __FUNCTION__
, texObj
);
118 t
= (mach64TexObjPtr
) CALLOC_STRUCT( mach64_texture_object
);
119 texObj
->DriverData
= t
;
123 /* Initialize non-image-dependent parts of the state:
125 t
->base
.tObj
= texObj
;
126 t
->base
.dirty_images
[0] = (1 << 0);
130 make_empty_list( (driTextureObject
*) t
);
132 mach64SetTexWrap( t
, texObj
->WrapS
, texObj
->WrapT
);
133 mach64SetTexFilter( t
, texObj
->MinFilter
, texObj
->MagFilter
);
134 mach64SetTexBorderColor( t
, texObj
->_BorderChan
);
140 /* Called by the _mesa_store_teximage[123]d() functions. */
141 static const struct gl_texture_format
*
142 mach64ChooseTextureFormat( GLcontext
*ctx
, GLint internalFormat
,
143 GLenum format
, GLenum type
)
145 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
149 switch ( internalFormat
) {
156 case GL_LUMINANCE_ALPHA
:
157 case GL_LUMINANCE4_ALPHA4
:
158 case GL_LUMINANCE6_ALPHA2
:
159 case GL_LUMINANCE8_ALPHA8
:
160 case GL_LUMINANCE12_ALPHA4
:
161 case GL_LUMINANCE12_ALPHA12
:
162 case GL_LUMINANCE16_ALPHA16
:
166 if (mmesa
->mach64Screen
->cpp
== 4)
167 return &_mesa_texformat_argb8888
;
169 return &_mesa_texformat_argb4444
;
172 if (mmesa
->mach64Screen
->cpp
== 4)
173 return &_mesa_texformat_argb8888
;
175 return &_mesa_texformat_argb1555
;
182 if (mmesa
->mach64Screen
->cpp
== 4)
183 return &_mesa_texformat_argb8888
;
185 return &_mesa_texformat_argb4444
;
196 if (mmesa
->mach64Screen
->cpp
== 4)
197 return &_mesa_texformat_argb8888
;
199 return &_mesa_texformat_rgb565
;
207 if (mmesa
->mach64Screen
->cpp
== 4)
208 return &_mesa_texformat_argb8888
; /* inefficient but accurate */
210 return &_mesa_texformat_argb1555
;
217 if (mmesa
->mach64Screen
->cpp
== 4)
218 return &_mesa_texformat_argb8888
; /* inefficient but accurate */
220 return &_mesa_texformat_argb4444
;
223 case GL_COLOR_INDEX1_EXT
:
224 case GL_COLOR_INDEX2_EXT
:
225 case GL_COLOR_INDEX4_EXT
:
226 case GL_COLOR_INDEX8_EXT
:
227 case GL_COLOR_INDEX12_EXT
:
228 case GL_COLOR_INDEX16_EXT
:
229 return &_mesa_texformat_ci8
;
232 if (type
== GL_UNSIGNED_SHORT_8_8_APPLE
||
233 type
== GL_UNSIGNED_BYTE
)
234 return &_mesa_texformat_ycbcr
;
236 return &_mesa_texformat_ycbcr_rev
;
239 _mesa_problem( ctx
, "unexpected format in %s", __FUNCTION__
);
244 static void mach64TexImage1D( GLcontext
*ctx
, GLenum target
, GLint level
,
245 GLint internalFormat
,
246 GLint width
, GLint border
,
247 GLenum format
, GLenum type
, const GLvoid
*pixels
,
248 const struct gl_pixelstore_attrib
*packing
,
249 struct gl_texture_object
*texObj
,
250 struct gl_texture_image
*texImage
)
252 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
253 driTextureObject
* t
= (driTextureObject
*) texObj
->DriverData
;
256 driSwapOutTextureObject( t
);
259 t
= (driTextureObject
*) mach64AllocTexObj(texObj
);
261 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
266 /* Note, this will call mach64ChooseTextureFormat */
267 _mesa_store_teximage1d( ctx
, target
, level
, internalFormat
,
268 width
, border
, format
, type
,
269 pixels
, packing
, texObj
, texImage
);
271 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
274 static void mach64TexSubImage1D( GLcontext
*ctx
,
279 GLenum format
, GLenum type
,
280 const GLvoid
*pixels
,
281 const struct gl_pixelstore_attrib
*packing
,
282 struct gl_texture_object
*texObj
,
283 struct gl_texture_image
*texImage
)
285 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
286 driTextureObject
* t
= (driTextureObject
*) texObj
->DriverData
;
288 assert( t
); /* this _should_ be true */
290 driSwapOutTextureObject( t
);
293 t
= (driTextureObject
*) mach64AllocTexObj(texObj
);
295 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage1D");
300 _mesa_store_texsubimage1d(ctx
, target
, level
, xoffset
, width
,
301 format
, type
, pixels
, packing
, texObj
,
304 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
307 static void mach64TexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
308 GLint internalFormat
,
309 GLint width
, GLint height
, GLint border
,
310 GLenum format
, GLenum type
, const GLvoid
*pixels
,
311 const struct gl_pixelstore_attrib
*packing
,
312 struct gl_texture_object
*texObj
,
313 struct gl_texture_image
*texImage
)
315 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
316 driTextureObject
* t
= (driTextureObject
*) texObj
->DriverData
;
319 driSwapOutTextureObject( t
);
322 t
= (driTextureObject
*) mach64AllocTexObj(texObj
);
324 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
329 /* Note, this will call mach64ChooseTextureFormat */
330 _mesa_store_teximage2d( ctx
, target
, level
, internalFormat
,
331 width
, height
, border
, format
, type
, pixels
,
332 &ctx
->Unpack
, texObj
, texImage
);
334 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
337 static void mach64TexSubImage2D( GLcontext
*ctx
,
340 GLint xoffset
, GLint yoffset
,
341 GLsizei width
, GLsizei height
,
342 GLenum format
, GLenum type
,
343 const GLvoid
*pixels
,
344 const struct gl_pixelstore_attrib
*packing
,
345 struct gl_texture_object
*texObj
,
346 struct gl_texture_image
*texImage
)
348 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
349 driTextureObject
* t
= (driTextureObject
*) texObj
->DriverData
;
351 assert( t
); /* this _should_ be true */
353 driSwapOutTextureObject( t
);
356 t
= (driTextureObject
*) mach64AllocTexObj(texObj
);
358 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage2D");
363 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
, width
,
364 height
, format
, type
, pixels
, packing
, texObj
,
367 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
370 /* ================================================================
371 * Device Driver API texture functions
374 static void mach64DDTexEnv( GLcontext
*ctx
, GLenum target
,
375 GLenum pname
, const GLfloat
*param
)
377 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
379 struct gl_texture_unit
*texUnit
;
383 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
384 fprintf( stderr
, "%s( %s )\n",
385 __FUNCTION__
, _mesa_lookup_enum_by_nr( pname
) );
389 case GL_TEXTURE_ENV_MODE
:
390 FLUSH_BATCH( mmesa
);
391 mmesa
->new_state
|= MACH64_NEW_TEXTURE
| MACH64_NEW_ALPHA
;
395 case GL_TEXTURE_ENV_COLOR
:
396 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
397 CLAMPED_FLOAT_TO_UBYTE( c
[0], texUnit
->EnvColor
[0] );
398 CLAMPED_FLOAT_TO_UBYTE( c
[1], texUnit
->EnvColor
[1] );
399 CLAMPED_FLOAT_TO_UBYTE( c
[2], texUnit
->EnvColor
[2] );
400 CLAMPED_FLOAT_TO_UBYTE( c
[3], texUnit
->EnvColor
[3] );
401 mmesa
->env_color
= mach64PackColor( 32, c
[0], c
[1], c
[2], c
[3] );
402 if ( mmesa
->setup
.constant_color_c
!= mmesa
->env_color
) {
403 FLUSH_BATCH( mmesa
);
404 mmesa
->setup
.constant_color_c
= mmesa
->env_color
;
406 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
408 /* More complex multitexture/multipass fallbacks for GL_BLEND
409 * can be done later, but this allows a single pass GL_BLEND
410 * in some cases (ie. Performer town demo).
412 mmesa
->blend_flags
&= ~MACH64_BLEND_ENV_COLOR
;
413 if ( mmesa
->env_color
!= 0x00000000 &&
414 mmesa
->env_color
!= 0xff000000 &&
415 mmesa
->env_color
!= 0x00ffffff &&
416 mmesa
->env_color
!= 0xffffffff )) {
417 mmesa
->blend_flags
|= MACH64_BLEND_ENV_COLOR
;
428 static void mach64DDTexParameter( GLcontext
*ctx
, GLenum target
,
429 struct gl_texture_object
*tObj
,
430 GLenum pname
, const GLfloat
*params
)
432 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
433 mach64TexObjPtr t
= (mach64TexObjPtr
)tObj
->DriverData
;
435 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
436 fprintf( stderr
, "%s( %s )\n",
437 __FUNCTION__
, _mesa_lookup_enum_by_nr( pname
) );
440 if ( ( target
!= GL_TEXTURE_2D
) &&
441 ( target
!= GL_TEXTURE_1D
) ) {
446 t
= mach64AllocTexObj(tObj
);
448 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexParameter");
454 case GL_TEXTURE_MIN_FILTER
:
455 case GL_TEXTURE_MAG_FILTER
:
456 if ( t
->base
.bound
) FLUSH_BATCH( mmesa
);
457 mach64SetTexFilter( t
, tObj
->MinFilter
, tObj
->MagFilter
);
460 case GL_TEXTURE_WRAP_S
:
461 case GL_TEXTURE_WRAP_T
:
462 if ( t
->base
.bound
) FLUSH_BATCH( mmesa
);
463 mach64SetTexWrap( t
, tObj
->WrapS
, tObj
->WrapT
);
466 case GL_TEXTURE_BORDER_COLOR
:
467 if ( t
->base
.bound
) FLUSH_BATCH( mmesa
);
468 mach64SetTexBorderColor( t
, tObj
->_BorderChan
);
471 case GL_TEXTURE_BASE_LEVEL
:
472 /* From Radeon/Rage128:
473 * This isn't the most efficient solution but there doesn't appear to
474 * be a nice alternative. Since there's no LOD clamping,
475 * we just have to rely on loading the right subset of mipmap levels
476 * to simulate a clamped LOD.
478 * For mach64 we're only concerned with the base level
479 * since that's the only texture we upload.
481 if ( t
->base
.bound
) FLUSH_BATCH( mmesa
);
482 driSwapOutTextureObject( (driTextureObject
*) t
);
489 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
492 static void mach64DDBindTexture( GLcontext
*ctx
, GLenum target
,
493 struct gl_texture_object
*tObj
)
495 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
496 GLint unit
= ctx
->Texture
.CurrentUnit
;
498 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
499 fprintf( stderr
, "%s( %p ) unit=%d\n",
500 __FUNCTION__
, tObj
, unit
);
503 FLUSH_BATCH( mmesa
);
505 if ( mmesa
->CurrentTexObj
[unit
] ) {
506 mmesa
->CurrentTexObj
[unit
]->base
.bound
&= ~(1 << unit
);
507 mmesa
->CurrentTexObj
[unit
] = NULL
;
510 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
513 static void mach64DDDeleteTexture( GLcontext
*ctx
,
514 struct gl_texture_object
*tObj
)
516 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
517 driTextureObject
* t
= (driTextureObject
*) tObj
->DriverData
;
520 if ( t
->bound
&& mmesa
) {
521 FLUSH_BATCH( mmesa
);
523 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
526 driDestroyTextureObject( t
);
528 /* Free mipmap images and the texture object itself */
529 _mesa_delete_texture_object(ctx
, tObj
);
534 * Allocate a new texture object.
535 * Called via ctx->Driver.NewTextureObject.
536 * Note: we could use containment here to 'derive' the driver-specific
537 * texture object from the core mesa gl_texture_object. Not done at this time.
539 static struct gl_texture_object
*
540 mach64NewTextureObject( GLcontext
*ctx
, GLuint name
, GLenum target
)
542 struct gl_texture_object
*obj
;
543 obj
= _mesa_new_texture_object(ctx
, name
, target
);
544 mach64AllocTexObj( obj
);
548 void mach64InitTextureFuncs( struct dd_function_table
*functions
)
550 functions
->TexEnv
= mach64DDTexEnv
;
551 functions
->ChooseTextureFormat
= mach64ChooseTextureFormat
;
552 functions
->TexImage1D
= mach64TexImage1D
;
553 functions
->TexSubImage1D
= mach64TexSubImage1D
;
554 functions
->TexImage2D
= mach64TexImage2D
;
555 functions
->TexSubImage2D
= mach64TexSubImage2D
;
556 functions
->TexParameter
= mach64DDTexParameter
;
557 functions
->BindTexture
= mach64DDBindTexture
;
558 functions
->NewTextureObject
= mach64NewTextureObject
;
559 functions
->DeleteTexture
= mach64DDDeleteTexture
;
560 functions
->IsTextureResident
= driIsTextureResident
;
562 functions
->UpdateTexturePalette
= NULL
;
563 functions
->ActiveTexture
= NULL
;
564 functions
->PrioritizeTexture
= NULL
;
566 driInitTextureFormats();