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 "main/glheader.h"
33 #include "main/imports.h"
34 #include "main/context.h"
35 #include "main/macros.h"
37 #include "mach64_context.h"
38 #include "mach64_ioctl.h"
39 #include "mach64_tris.h"
40 #include "mach64_tex.h"
42 static void mach64SetTexImages( mach64ContextPtr mmesa
,
43 const struct gl_texture_object
*tObj
)
45 mach64TexObjPtr t
= (mach64TexObjPtr
) tObj
->DriverData
;
46 struct gl_texture_image
*baseImage
= tObj
->Image
[0][tObj
->BaseLevel
];
52 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
)
53 fprintf( stderr
, "%s( %p )\n", __FUNCTION__
, tObj
);
55 switch (baseImage
->TexFormat
) {
56 case MESA_FORMAT_ARGB8888
:
57 t
->textureFormat
= MACH64_DATATYPE_ARGB8888
;
59 case MESA_FORMAT_ARGB4444
:
60 t
->textureFormat
= MACH64_DATATYPE_ARGB4444
;
62 case MESA_FORMAT_RGB565
:
63 t
->textureFormat
= MACH64_DATATYPE_RGB565
;
65 case MESA_FORMAT_ARGB1555
:
66 t
->textureFormat
= MACH64_DATATYPE_ARGB1555
;
68 case MESA_FORMAT_RGB332
:
69 t
->textureFormat
= MACH64_DATATYPE_RGB332
;
71 case MESA_FORMAT_RGB888
:
72 t
->textureFormat
= MACH64_DATATYPE_RGB8
;
75 t
->textureFormat
= MACH64_DATATYPE_CI8
;
77 case MESA_FORMAT_YCBCR
:
78 t
->textureFormat
= MACH64_DATATYPE_YVYU422
;
80 case MESA_FORMAT_YCBCR_REV
:
81 t
->textureFormat
= MACH64_DATATYPE_VYUY422
;
84 _mesa_problem(mmesa
->glCtx
, "Bad texture format in %s", __FUNCTION__
);
87 totalSize
= ( baseImage
->Height
*
89 _mesa_get_format_bytes(baseImage
->TexFormat
) );
91 totalSize
= (totalSize
+ 31) & ~31;
93 t
->base
.totalSize
= totalSize
;
94 t
->base
.firstLevel
= tObj
->BaseLevel
;
95 t
->base
.lastLevel
= tObj
->BaseLevel
;
97 /* Set the texture format */
98 if ( ( baseImage
->_BaseFormat
== GL_RGBA
) ||
99 ( baseImage
->_BaseFormat
== GL_ALPHA
) ||
100 ( baseImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) ) {
106 t
->widthLog2
= baseImage
->WidthLog2
;
107 t
->heightLog2
= baseImage
->HeightLog2
;
108 t
->maxLog2
= baseImage
->MaxLog2
;
111 static void mach64UpdateTextureEnv( GLcontext
*ctx
, int unit
)
113 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
114 GLint source
= mmesa
->tmu_source
[unit
];
115 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[source
];
116 const struct gl_texture_object
*tObj
= texUnit
->_Current
;
117 const GLenum format
= tObj
->Image
[0][tObj
->BaseLevel
]->_BaseFormat
;
118 GLuint s
= mmesa
->setup
.scale_3d_cntl
;
120 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
121 fprintf( stderr
, "%s( %p, %d )\n",
122 __FUNCTION__
, ctx
, unit
);
125 /* REPLACE MODULATE DECAL GL_BLEND
127 * ALPHA C = Cf C = Cf undef C = Cf
128 * A = At A = AfAt A = AfAt
130 * LUMINANCE C = Ct C = CfCt undef C = Cf(1-Ct)+CcCt
131 * A = Af A = Af A = Af
133 * LUMINANCE_ALPHA C = Ct C = CfCt undef C = Cf(1-Ct)+CcCt
134 * A = At A = AfAt A = AfAt
136 * INTENSITY C = Ct C = CfCt undef C = Cf(1-Ct)+CcCt
137 * A = At A = AfAt A = Af(1-At)+AcAt
139 * RGB C = Ct C = CfCt C = Ct C = Cf(1-Ct)+CcCt
140 * A = Af A = Af A = Af A = Af
142 * RGBA C = Ct C = CfCt C = Cf(1-At)+CtAt C = Cf(1-Ct)+CcCt
143 * A = At A = AfAt A = Af A = AfAt
148 s
&= ~MACH64_TEX_LIGHT_FCN_MASK
;
150 /* Set the texture environment state
151 * Need to verify these are working correctly, but the
152 * texenv Mesa demo seems to work.
154 switch ( texUnit
->EnvMode
) {
158 case GL_LUMINANCE_ALPHA
:
160 /* Not compliant - can't get At */
161 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
162 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
165 s
|= MACH64_TEX_LIGHT_FCN_REPLACE
;
171 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
172 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
176 /* These should be compliant */
177 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
179 case GL_LUMINANCE_ALPHA
:
181 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
182 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
185 /* Should fallback when blending enabled for complete compliance */
186 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
189 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
195 s
|= MACH64_TEX_LIGHT_FCN_ALPHA_DECAL
;
198 s
|= MACH64_TEX_LIGHT_FCN_REPLACE
;
201 case GL_LUMINANCE_ALPHA
:
202 /* undefined - disable texturing, pass fragment unmodified */
203 /* Also, pass fragment alpha instead of texture alpha */
204 s
&= ~MACH64_TEX_MAP_AEN
;
205 s
|= MACH64_TEXTURE_DISABLE
;
206 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
210 /* undefined - disable texturing, pass fragment unmodified */
211 s
|= MACH64_TEXTURE_DISABLE
;
212 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
215 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
219 /* GL_BLEND not supported by RagePRO, use software */
220 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
221 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
225 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
226 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
229 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
232 if ( mmesa
->setup
.scale_3d_cntl
!= s
) {
233 mmesa
->setup
.scale_3d_cntl
= s
;
234 mmesa
->dirty
|= MACH64_UPLOAD_SCALE_3D_CNTL
;
238 /* blend = 0, modulate = 1 - initialize to blend */
239 mmesa
->setup
.tex_cntl
&= ~MACH64_COMP_COMBINE_MODULATE
;
240 /* Set the texture composite function for multitexturing*/
241 switch ( texUnit
->EnvMode
) {
243 /* GL_BLEND not supported by RagePRO, use software */
244 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
245 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
248 /* Should fallback when blending enabled for complete compliance */
249 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
254 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
256 default: /* not supported by RagePRO */
257 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
258 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
265 case GL_LUMINANCE_ALPHA
:
267 /* undefined, disable compositing and pass fragment unmodified */
268 mmesa
->setup
.tex_cntl
&= ~MACH64_TEXTURE_COMPOSITE
;
270 default: /* not supported by RagePRO */
271 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
272 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
277 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
278 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
281 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
287 static void mach64UpdateTextureUnit( GLcontext
*ctx
, int unit
)
289 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
290 int source
= mmesa
->tmu_source
[unit
];
291 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[source
];
292 const struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[source
]._Current
;
293 mach64TexObjPtr t
= tObj
->DriverData
;
294 GLuint d
= mmesa
->setup
.dp_pix_width
;
295 GLuint s
= mmesa
->setup
.scale_3d_cntl
;
297 assert(unit
== 0 || unit
== 1); /* only two tex units */
299 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
300 fprintf( stderr
, "%s( %p, %d ) enabled=0x%x 0x%x\n",
301 __FUNCTION__
, ctx
, unit
, ctx
->Texture
.Unit
[0]._ReallyEnabled
,
302 ctx
->Texture
.Unit
[1]._ReallyEnabled
);
305 if (texUnit
->_ReallyEnabled
& (TEXTURE_1D_BIT
| TEXTURE_2D_BIT
)) {
307 assert(t
); /* should have driver tex data by now */
309 /* Fallback if there's a texture border */
310 if ( tObj
->Image
[0][tObj
->BaseLevel
]->Border
> 0 ) {
311 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
315 /* Upload teximages */
316 if (t
->base
.dirty_images
[0]) {
317 mach64SetTexImages( mmesa
, tObj
);
318 mmesa
->dirty
|= (MACH64_UPLOAD_TEX0IMAGE
<< unit
);
321 /* Bind to the given texture unit */
322 mmesa
->CurrentTexObj
[unit
] = t
;
323 t
->base
.bound
|= (1 << unit
);
325 if ( t
->base
.memBlock
)
326 driUpdateTextureLRU( (driTextureObject
*) t
); /* XXX: should be locked! */
330 d
&= ~MACH64_SCALE_PIX_WIDTH_MASK
;
331 d
|= (t
->textureFormat
<< 28);
333 s
&= ~(MACH64_TEXTURE_DISABLE
|
334 MACH64_TEX_CACHE_SPLIT
|
335 MACH64_TEX_BLEND_FCN_MASK
|
338 if ( mmesa
->multitex
) {
339 s
|= MACH64_TEX_BLEND_FCN_TRILINEAR
| MACH64_TEX_CACHE_SPLIT
;
340 } else if ( t
->BilinearMin
) {
341 s
|= MACH64_TEX_BLEND_FCN_LINEAR
;
343 s
|= MACH64_TEX_BLEND_FCN_NEAREST
;
345 if ( t
->BilinearMag
) {
346 s
|= MACH64_BILINEAR_TEX_EN
;
348 s
&= ~MACH64_BILINEAR_TEX_EN
;
352 s
|= MACH64_TEX_MAP_AEN
;
355 mmesa
->setup
.tex_cntl
&= ~(MACH64_TEXTURE_CLAMP_S
|
356 MACH64_TEXTURE_CLAMP_T
|
357 MACH64_SECONDARY_STW
);
360 mmesa
->setup
.tex_cntl
|= MACH64_TEXTURE_CLAMP_S
;
363 mmesa
->setup
.tex_cntl
|= MACH64_TEXTURE_CLAMP_T
;
366 mmesa
->setup
.tex_size_pitch
|= ((t
->widthLog2
<< 0) |
368 (t
->heightLog2
<< 8));
371 /* Enable texture mapping mode */
372 s
&= ~MACH64_TEXTURE_DISABLE
;
374 d
&= ~MACH64_COMPOSITE_PIX_WIDTH_MASK
;
375 d
|= (t
->textureFormat
<< 4);
377 mmesa
->setup
.tex_cntl
&= ~(MACH64_COMP_ALPHA
|
378 MACH64_SEC_TEX_CLAMP_S
|
379 MACH64_SEC_TEX_CLAMP_T
);
380 mmesa
->setup
.tex_cntl
|= (MACH64_TEXTURE_COMPOSITE
|
381 MACH64_SECONDARY_STW
);
383 if ( t
->BilinearMin
) {
384 mmesa
->setup
.tex_cntl
|= MACH64_COMP_BLEND_BILINEAR
;
386 mmesa
->setup
.tex_cntl
&= ~MACH64_COMP_BLEND_BILINEAR
;
388 if ( t
->BilinearMag
) {
389 mmesa
->setup
.tex_cntl
|= MACH64_COMP_FILTER_BILINEAR
;
391 mmesa
->setup
.tex_cntl
&= ~MACH64_COMP_FILTER_BILINEAR
;
395 mmesa
->setup
.tex_cntl
|= MACH64_COMP_ALPHA
;
398 mmesa
->setup
.tex_cntl
|= MACH64_SEC_TEX_CLAMP_S
;
401 mmesa
->setup
.tex_cntl
|= MACH64_SEC_TEX_CLAMP_T
;
404 mmesa
->setup
.tex_size_pitch
|= ((t
->widthLog2
<< 16) |
406 (t
->heightLog2
<< 24));
409 if ( mmesa
->setup
.scale_3d_cntl
!= s
) {
410 mmesa
->setup
.scale_3d_cntl
= s
;
411 mmesa
->dirty
|= MACH64_UPLOAD_SCALE_3D_CNTL
;
414 if ( mmesa
->setup
.dp_pix_width
!= d
) {
415 mmesa
->setup
.dp_pix_width
= d
;
416 mmesa
->dirty
|= MACH64_UPLOAD_DP_PIX_WIDTH
;
419 else if (texUnit
->_ReallyEnabled
) {
420 /* 3D or cube map texture enabled - fallback */
421 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
424 /* texture unit disabled */
429 /* Update the hardware texture state */
430 void mach64UpdateTextureState( GLcontext
*ctx
)
432 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
434 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
435 fprintf( stderr
, "%s( %p ) en=0x%x 0x%x\n",
436 __FUNCTION__
, ctx
, ctx
->Texture
.Unit
[0]._ReallyEnabled
,
437 ctx
->Texture
.Unit
[1]._ReallyEnabled
);
440 /* Clear any texturing fallbacks */
441 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_FALSE
);
443 /* Unbind any currently bound textures */
444 if ( mmesa
->CurrentTexObj
[0] ) mmesa
->CurrentTexObj
[0]->base
.bound
= 0;
445 if ( mmesa
->CurrentTexObj
[1] ) mmesa
->CurrentTexObj
[1]->base
.bound
= 0;
446 mmesa
->CurrentTexObj
[0] = NULL
;
447 mmesa
->CurrentTexObj
[1] = NULL
;
449 /* Disable all texturing until it is known to be good */
450 mmesa
->setup
.scale_3d_cntl
|= MACH64_TEXTURE_DISABLE
;
451 mmesa
->setup
.scale_3d_cntl
&= ~MACH64_TEX_MAP_AEN
;
452 mmesa
->setup
.tex_cntl
&= ~MACH64_TEXTURE_COMPOSITE
;
454 mmesa
->setup
.tex_size_pitch
= 0x00000000;
456 mmesa
->tmu_source
[0] = 0;
457 mmesa
->tmu_source
[1] = 1;
460 if (ctx
->Texture
._EnabledUnits
& 0x2) {
462 if (ctx
->Texture
._EnabledUnits
& 0x1) {
463 /* units 0 and 1 enabled */
465 mach64UpdateTextureUnit( ctx
, 0 );
466 mach64UpdateTextureEnv( ctx
, 0 );
467 mach64UpdateTextureUnit( ctx
, 1 );
468 mach64UpdateTextureEnv( ctx
, 1 );
470 mmesa
->tmu_source
[0] = 1;
471 mmesa
->tmu_source
[1] = 0;
472 mach64UpdateTextureUnit( ctx
, 0 );
473 mach64UpdateTextureEnv( ctx
, 0 );
475 } else if (ctx
->Texture
._EnabledUnits
& 0x1) {
476 /* only unit 0 enabled */
477 mach64UpdateTextureUnit( ctx
, 0 );
478 mach64UpdateTextureEnv( ctx
, 0 );
481 mmesa
->dirty
|= (MACH64_UPLOAD_SCALE_3D_CNTL
|
482 MACH64_UPLOAD_TEXTURE
);
486 /* Due to the way we must program texture state into the Rage Pro,
487 * we must leave these calculations to the absolute last minute.
489 void mach64EmitTexStateLocked( mach64ContextPtr mmesa
,
493 drm_mach64_sarea_t
*sarea
= mmesa
->sarea
;
494 drm_mach64_context_regs_t
*regs
= &(mmesa
->setup
);
496 /* for multitex, both textures must be local or AGP */
498 assert(t0
->heap
== t1
->heap
);
501 if (t0
->heap
== MACH64_CARD_HEAP
) {
502 #if ENABLE_PERF_BOXES
503 mmesa
->c_texsrc_card
++;
505 mmesa
->setup
.tex_cntl
&= ~MACH64_TEX_SRC_AGP
;
507 #if ENABLE_PERF_BOXES
508 mmesa
->c_texsrc_agp
++;
510 mmesa
->setup
.tex_cntl
|= MACH64_TEX_SRC_AGP
;
512 mmesa
->setup
.tex_offset
= t0
->bufAddr
;
516 mmesa
->setup
.secondary_tex_off
= t1
->bufAddr
;
519 memcpy( &sarea
->context_state
.tex_size_pitch
, ®s
->tex_size_pitch
,
520 MACH64_NR_TEXTURE_REGS
* sizeof(GLuint
) );