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/teximage.h"
45 #include "main/texobj.h"
46 #include "main/imports.h"
49 static void mach64SetTexWrap( mach64TexObjPtr t
,
50 GLenum swrap
, GLenum twrap
)
54 case GL_CLAMP_TO_EDGE
:
55 case GL_CLAMP_TO_BORDER
:
65 case GL_CLAMP_TO_EDGE
:
66 case GL_CLAMP_TO_BORDER
:
75 static void mach64SetTexFilter( mach64TexObjPtr t
,
76 GLenum minf
, GLenum magf
)
80 case GL_NEAREST_MIPMAP_NEAREST
:
81 case GL_NEAREST_MIPMAP_LINEAR
:
82 t
->BilinearMin
= GL_FALSE
;
85 case GL_LINEAR_MIPMAP_NEAREST
:
86 case GL_LINEAR_MIPMAP_LINEAR
:
87 t
->BilinearMin
= GL_TRUE
;
93 t
->BilinearMag
= GL_FALSE
;
96 t
->BilinearMag
= GL_TRUE
;
101 static void mach64SetTexBorderColor( mach64TexObjPtr t
, const GLfloat c
[4] )
104 GLuint border
= mach64PackColor( 4, c
[0], c
[1], c
[2], c
[3] );
109 static mach64TexObjPtr
110 mach64AllocTexObj( struct gl_texture_object
*texObj
)
114 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
)
115 fprintf( stderr
, "%s( %p )\n", __FUNCTION__
, texObj
);
117 t
= (mach64TexObjPtr
) CALLOC_STRUCT( mach64_texture_object
);
118 texObj
->DriverData
= t
;
122 /* Initialize non-image-dependent parts of the state:
124 t
->base
.tObj
= texObj
;
125 t
->base
.dirty_images
[0] = (1 << 0);
129 make_empty_list( (driTextureObject
*) t
);
131 mach64SetTexWrap( t
, texObj
->WrapS
, texObj
->WrapT
);
132 mach64SetTexFilter( t
, texObj
->MinFilter
, texObj
->MagFilter
);
133 mach64SetTexBorderColor( t
, texObj
->BorderColor
.f
);
139 /* Called by the _mesa_store_teximage[123]d() functions. */
141 mach64ChooseTextureFormat( GLcontext
*ctx
, GLint internalFormat
,
142 GLenum format
, GLenum type
)
144 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
148 switch ( internalFormat
) {
154 case GL_COMPRESSED_ALPHA
:
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
:
163 case GL_COMPRESSED_LUMINANCE_ALPHA
:
167 case GL_COMPRESSED_RGBA
:
168 if (mmesa
->mach64Screen
->cpp
== 4)
169 return MESA_FORMAT_ARGB8888
;
171 return MESA_FORMAT_ARGB4444
;
174 if (mmesa
->mach64Screen
->cpp
== 4)
175 return MESA_FORMAT_ARGB8888
;
177 return MESA_FORMAT_ARGB1555
;
184 if (mmesa
->mach64Screen
->cpp
== 4)
185 return MESA_FORMAT_ARGB8888
;
187 return MESA_FORMAT_ARGB4444
;
198 case GL_COMPRESSED_RGB
:
199 if (mmesa
->mach64Screen
->cpp
== 4)
200 return MESA_FORMAT_ARGB8888
;
202 return MESA_FORMAT_RGB565
;
210 case GL_COMPRESSED_LUMINANCE
:
211 if (mmesa
->mach64Screen
->cpp
== 4)
212 return MESA_FORMAT_ARGB8888
; /* inefficient but accurate */
214 return MESA_FORMAT_ARGB1555
;
221 case GL_COMPRESSED_INTENSITY
:
222 if (mmesa
->mach64Screen
->cpp
== 4)
223 return MESA_FORMAT_ARGB8888
; /* inefficient but accurate */
225 return MESA_FORMAT_ARGB4444
;
228 case GL_COLOR_INDEX1_EXT
:
229 case GL_COLOR_INDEX2_EXT
:
230 case GL_COLOR_INDEX4_EXT
:
231 case GL_COLOR_INDEX8_EXT
:
232 case GL_COLOR_INDEX12_EXT
:
233 case GL_COLOR_INDEX16_EXT
:
234 return MESA_FORMAT_CI8
;
237 if (type
== GL_UNSIGNED_SHORT_8_8_APPLE
||
238 type
== GL_UNSIGNED_BYTE
)
239 return MESA_FORMAT_YCBCR
;
241 return MESA_FORMAT_YCBCR_REV
;
244 _mesa_problem( ctx
, "unexpected format in %s", __FUNCTION__
);
245 return MESA_FORMAT_NONE
;
249 static void mach64TexImage1D( GLcontext
*ctx
, GLenum target
, GLint level
,
250 GLint internalFormat
,
251 GLint width
, GLint border
,
252 GLenum format
, GLenum type
, const GLvoid
*pixels
,
253 const struct gl_pixelstore_attrib
*packing
,
254 struct gl_texture_object
*texObj
,
255 struct gl_texture_image
*texImage
)
257 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
258 driTextureObject
* t
= (driTextureObject
*) texObj
->DriverData
;
261 driSwapOutTextureObject( t
);
264 t
= (driTextureObject
*) mach64AllocTexObj(texObj
);
266 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage1D");
271 /* Note, this will call mach64ChooseTextureFormat */
272 _mesa_store_teximage1d( ctx
, target
, level
, internalFormat
,
273 width
, border
, format
, type
,
274 pixels
, packing
, texObj
, texImage
);
276 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
279 static void mach64TexSubImage1D( GLcontext
*ctx
,
284 GLenum format
, GLenum type
,
285 const GLvoid
*pixels
,
286 const struct gl_pixelstore_attrib
*packing
,
287 struct gl_texture_object
*texObj
,
288 struct gl_texture_image
*texImage
)
290 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
291 driTextureObject
* t
= (driTextureObject
*) texObj
->DriverData
;
293 assert( t
); /* this _should_ be true */
295 driSwapOutTextureObject( t
);
298 t
= (driTextureObject
*) mach64AllocTexObj(texObj
);
300 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage1D");
305 _mesa_store_texsubimage1d(ctx
, target
, level
, xoffset
, width
,
306 format
, type
, pixels
, packing
, texObj
,
309 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
312 static void mach64TexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
313 GLint internalFormat
,
314 GLint width
, GLint height
, GLint border
,
315 GLenum format
, GLenum type
, const GLvoid
*pixels
,
316 const struct gl_pixelstore_attrib
*packing
,
317 struct gl_texture_object
*texObj
,
318 struct gl_texture_image
*texImage
)
320 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
321 driTextureObject
* t
= (driTextureObject
*) texObj
->DriverData
;
324 driSwapOutTextureObject( t
);
327 t
= (driTextureObject
*) mach64AllocTexObj(texObj
);
329 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
334 /* Note, this will call mach64ChooseTextureFormat */
335 _mesa_store_teximage2d( ctx
, target
, level
, internalFormat
,
336 width
, height
, border
, format
, type
, pixels
,
337 &ctx
->Unpack
, texObj
, texImage
);
339 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
342 static void mach64TexSubImage2D( GLcontext
*ctx
,
345 GLint xoffset
, GLint yoffset
,
346 GLsizei width
, GLsizei height
,
347 GLenum format
, GLenum type
,
348 const GLvoid
*pixels
,
349 const struct gl_pixelstore_attrib
*packing
,
350 struct gl_texture_object
*texObj
,
351 struct gl_texture_image
*texImage
)
353 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
354 driTextureObject
* t
= (driTextureObject
*) texObj
->DriverData
;
356 assert( t
); /* this _should_ be true */
358 driSwapOutTextureObject( t
);
361 t
= (driTextureObject
*) mach64AllocTexObj(texObj
);
363 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage2D");
368 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
, width
,
369 height
, format
, type
, pixels
, packing
, texObj
,
372 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
375 /* ================================================================
376 * Device Driver API texture functions
379 static void mach64DDTexEnv( GLcontext
*ctx
, GLenum target
,
380 GLenum pname
, const GLfloat
*param
)
382 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
384 struct gl_texture_unit
*texUnit
;
388 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
389 fprintf( stderr
, "%s( %s )\n",
390 __FUNCTION__
, _mesa_lookup_enum_by_nr( pname
) );
394 case GL_TEXTURE_ENV_MODE
:
395 FLUSH_BATCH( mmesa
);
396 mmesa
->new_state
|= MACH64_NEW_TEXTURE
| MACH64_NEW_ALPHA
;
400 case GL_TEXTURE_ENV_COLOR
:
401 texUnit
= &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
402 CLAMPED_FLOAT_TO_UBYTE( c
[0], texUnit
->EnvColor
[0] );
403 CLAMPED_FLOAT_TO_UBYTE( c
[1], texUnit
->EnvColor
[1] );
404 CLAMPED_FLOAT_TO_UBYTE( c
[2], texUnit
->EnvColor
[2] );
405 CLAMPED_FLOAT_TO_UBYTE( c
[3], texUnit
->EnvColor
[3] );
406 mmesa
->env_color
= mach64PackColor( 32, c
[0], c
[1], c
[2], c
[3] );
407 if ( mmesa
->setup
.constant_color_c
!= mmesa
->env_color
) {
408 FLUSH_BATCH( mmesa
);
409 mmesa
->setup
.constant_color_c
= mmesa
->env_color
;
411 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
413 /* More complex multitexture/multipass fallbacks for GL_BLEND
414 * can be done later, but this allows a single pass GL_BLEND
415 * in some cases (ie. Performer town demo).
417 mmesa
->blend_flags
&= ~MACH64_BLEND_ENV_COLOR
;
418 if ( mmesa
->env_color
!= 0x00000000 &&
419 mmesa
->env_color
!= 0xff000000 &&
420 mmesa
->env_color
!= 0x00ffffff &&
421 mmesa
->env_color
!= 0xffffffff )) {
422 mmesa
->blend_flags
|= MACH64_BLEND_ENV_COLOR
;
433 static void mach64DDTexParameter( GLcontext
*ctx
, GLenum target
,
434 struct gl_texture_object
*tObj
,
435 GLenum pname
, const GLfloat
*params
)
437 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
438 mach64TexObjPtr t
= (mach64TexObjPtr
)tObj
->DriverData
;
440 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
441 fprintf( stderr
, "%s( %s )\n",
442 __FUNCTION__
, _mesa_lookup_enum_by_nr( pname
) );
445 if ( ( target
!= GL_TEXTURE_2D
) &&
446 ( target
!= GL_TEXTURE_1D
) ) {
451 t
= mach64AllocTexObj(tObj
);
453 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexParameter");
459 case GL_TEXTURE_MIN_FILTER
:
460 case GL_TEXTURE_MAG_FILTER
:
461 if ( t
->base
.bound
) FLUSH_BATCH( mmesa
);
462 mach64SetTexFilter( t
, tObj
->MinFilter
, tObj
->MagFilter
);
465 case GL_TEXTURE_WRAP_S
:
466 case GL_TEXTURE_WRAP_T
:
467 if ( t
->base
.bound
) FLUSH_BATCH( mmesa
);
468 mach64SetTexWrap( t
, tObj
->WrapS
, tObj
->WrapT
);
471 case GL_TEXTURE_BORDER_COLOR
:
472 if ( t
->base
.bound
) FLUSH_BATCH( mmesa
);
473 mach64SetTexBorderColor( t
, tObj
->BorderColor
.f
);
476 case GL_TEXTURE_BASE_LEVEL
:
477 /* From Radeon/Rage128:
478 * This isn't the most efficient solution but there doesn't appear to
479 * be a nice alternative. Since there's no LOD clamping,
480 * we just have to rely on loading the right subset of mipmap levels
481 * to simulate a clamped LOD.
483 * For mach64 we're only concerned with the base level
484 * since that's the only texture we upload.
486 if ( t
->base
.bound
) FLUSH_BATCH( mmesa
);
487 driSwapOutTextureObject( (driTextureObject
*) t
);
494 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
497 static void mach64DDBindTexture( GLcontext
*ctx
, GLenum target
,
498 struct gl_texture_object
*tObj
)
500 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
501 GLint unit
= ctx
->Texture
.CurrentUnit
;
503 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
504 fprintf( stderr
, "%s( %p ) unit=%d\n",
505 __FUNCTION__
, tObj
, unit
);
508 FLUSH_BATCH( mmesa
);
510 if ( mmesa
->CurrentTexObj
[unit
] ) {
511 mmesa
->CurrentTexObj
[unit
]->base
.bound
&= ~(1 << unit
);
512 mmesa
->CurrentTexObj
[unit
] = NULL
;
515 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
518 static void mach64DDDeleteTexture( GLcontext
*ctx
,
519 struct gl_texture_object
*tObj
)
521 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
522 driTextureObject
* t
= (driTextureObject
*) tObj
->DriverData
;
525 if ( t
->bound
&& mmesa
) {
526 FLUSH_BATCH( mmesa
);
528 mmesa
->new_state
|= MACH64_NEW_TEXTURE
;
531 driDestroyTextureObject( t
);
533 /* Free mipmap images and the texture object itself */
534 _mesa_delete_texture_object(ctx
, tObj
);
539 * Allocate a new texture object.
540 * Called via ctx->Driver.NewTextureObject.
541 * Note: we could use containment here to 'derive' the driver-specific
542 * texture object from the core mesa gl_texture_object. Not done at this time.
544 static struct gl_texture_object
*
545 mach64NewTextureObject( GLcontext
*ctx
, GLuint name
, GLenum target
)
547 struct gl_texture_object
*obj
;
548 obj
= _mesa_new_texture_object(ctx
, name
, target
);
549 mach64AllocTexObj( obj
);
553 void mach64InitTextureFuncs( struct dd_function_table
*functions
)
555 functions
->TexEnv
= mach64DDTexEnv
;
556 functions
->ChooseTextureFormat
= mach64ChooseTextureFormat
;
557 functions
->TexImage1D
= mach64TexImage1D
;
558 functions
->TexSubImage1D
= mach64TexSubImage1D
;
559 functions
->TexImage2D
= mach64TexImage2D
;
560 functions
->TexSubImage2D
= mach64TexSubImage2D
;
561 functions
->TexParameter
= mach64DDTexParameter
;
562 functions
->BindTexture
= mach64DDBindTexture
;
563 functions
->NewTextureObject
= mach64NewTextureObject
;
564 functions
->DeleteTexture
= mach64DDDeleteTexture
;
565 functions
->IsTextureResident
= driIsTextureResident
;
567 functions
->UpdateTexturePalette
= NULL
;
569 driInitTextureFormats();