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_state.h"
40 #include "mach64_vb.h"
41 #include "mach64_tris.h"
42 #include "mach64_tex.h"
44 static void mach64SetTexImages( mach64ContextPtr mmesa
,
45 const struct gl_texture_object
*tObj
)
47 mach64TexObjPtr t
= (mach64TexObjPtr
) tObj
->DriverData
;
48 struct gl_texture_image
*baseImage
= tObj
->Image
[0][tObj
->BaseLevel
];
54 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
)
55 fprintf( stderr
, "%s( %p )\n", __FUNCTION__
, tObj
);
57 switch (baseImage
->TexFormat
) {
58 case MESA_FORMAT_ARGB8888
:
59 t
->textureFormat
= MACH64_DATATYPE_ARGB8888
;
61 case MESA_FORMAT_ARGB4444
:
62 t
->textureFormat
= MACH64_DATATYPE_ARGB4444
;
64 case MESA_FORMAT_RGB565
:
65 t
->textureFormat
= MACH64_DATATYPE_RGB565
;
67 case MESA_FORMAT_ARGB1555
:
68 t
->textureFormat
= MACH64_DATATYPE_ARGB1555
;
70 case MESA_FORMAT_RGB332
:
71 t
->textureFormat
= MACH64_DATATYPE_RGB332
;
73 case MESA_FORMAT_RGB888
:
74 t
->textureFormat
= MACH64_DATATYPE_RGB8
;
77 t
->textureFormat
= MACH64_DATATYPE_CI8
;
79 case MESA_FORMAT_YCBCR
:
80 t
->textureFormat
= MACH64_DATATYPE_YVYU422
;
82 case MESA_FORMAT_YCBCR_REV
:
83 t
->textureFormat
= MACH64_DATATYPE_VYUY422
;
86 _mesa_problem(mmesa
->glCtx
, "Bad texture format in %s", __FUNCTION__
);
89 totalSize
= ( baseImage
->Height
*
91 _mesa_get_format_bytes(baseImage
->TexFormat
) );
93 totalSize
= (totalSize
+ 31) & ~31;
95 t
->base
.totalSize
= totalSize
;
96 t
->base
.firstLevel
= tObj
->BaseLevel
;
97 t
->base
.lastLevel
= tObj
->BaseLevel
;
99 /* Set the texture format */
100 if ( ( baseImage
->_BaseFormat
== GL_RGBA
) ||
101 ( baseImage
->_BaseFormat
== GL_ALPHA
) ||
102 ( baseImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) ) {
108 t
->widthLog2
= baseImage
->WidthLog2
;
109 t
->heightLog2
= baseImage
->HeightLog2
;
110 t
->maxLog2
= baseImage
->MaxLog2
;
113 static void mach64UpdateTextureEnv( GLcontext
*ctx
, int unit
)
115 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
116 GLint source
= mmesa
->tmu_source
[unit
];
117 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[source
];
118 const struct gl_texture_object
*tObj
= texUnit
->_Current
;
119 const GLenum format
= tObj
->Image
[0][tObj
->BaseLevel
]->_BaseFormat
;
120 GLuint s
= mmesa
->setup
.scale_3d_cntl
;
122 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
123 fprintf( stderr
, "%s( %p, %d )\n",
124 __FUNCTION__
, ctx
, unit
);
127 /* REPLACE MODULATE DECAL GL_BLEND
129 * ALPHA C = Cf C = Cf undef C = Cf
130 * A = At A = AfAt A = AfAt
132 * LUMINANCE C = Ct C = CfCt undef C = Cf(1-Ct)+CcCt
133 * A = Af A = Af A = Af
135 * LUMINANCE_ALPHA C = Ct C = CfCt undef C = Cf(1-Ct)+CcCt
136 * A = At A = AfAt A = AfAt
138 * INTENSITY C = Ct C = CfCt undef C = Cf(1-Ct)+CcCt
139 * A = At A = AfAt A = Af(1-At)+AcAt
141 * RGB C = Ct C = CfCt C = Ct C = Cf(1-Ct)+CcCt
142 * A = Af A = Af A = Af A = Af
144 * RGBA C = Ct C = CfCt C = Cf(1-At)+CtAt C = Cf(1-Ct)+CcCt
145 * A = At A = AfAt A = Af A = AfAt
150 s
&= ~MACH64_TEX_LIGHT_FCN_MASK
;
152 /* Set the texture environment state
153 * Need to verify these are working correctly, but the
154 * texenv Mesa demo seems to work.
156 switch ( texUnit
->EnvMode
) {
160 case GL_LUMINANCE_ALPHA
:
162 /* Not compliant - can't get At */
163 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
164 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
167 s
|= MACH64_TEX_LIGHT_FCN_REPLACE
;
173 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
174 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
178 /* These should be compliant */
179 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
181 case GL_LUMINANCE_ALPHA
:
183 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
184 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
187 /* Should fallback when blending enabled for complete compliance */
188 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
191 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
197 s
|= MACH64_TEX_LIGHT_FCN_ALPHA_DECAL
;
200 s
|= MACH64_TEX_LIGHT_FCN_REPLACE
;
203 case GL_LUMINANCE_ALPHA
:
204 /* undefined - disable texturing, pass fragment unmodified */
205 /* Also, pass fragment alpha instead of texture alpha */
206 s
&= ~MACH64_TEX_MAP_AEN
;
207 s
|= MACH64_TEXTURE_DISABLE
;
208 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
212 /* undefined - disable texturing, pass fragment unmodified */
213 s
|= MACH64_TEXTURE_DISABLE
;
214 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
217 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
221 /* GL_BLEND not supported by RagePRO, use software */
222 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
223 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
227 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
228 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
231 s
|= MACH64_TEX_LIGHT_FCN_MODULATE
;
234 if ( mmesa
->setup
.scale_3d_cntl
!= s
) {
235 mmesa
->setup
.scale_3d_cntl
= s
;
236 mmesa
->dirty
|= MACH64_UPLOAD_SCALE_3D_CNTL
;
240 /* blend = 0, modulate = 1 - initialize to blend */
241 mmesa
->setup
.tex_cntl
&= ~MACH64_COMP_COMBINE_MODULATE
;
242 /* Set the texture composite function for multitexturing*/
243 switch ( texUnit
->EnvMode
) {
245 /* GL_BLEND not supported by RagePRO, use software */
246 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
247 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
250 /* Should fallback when blending enabled for complete compliance */
251 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
256 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
258 default: /* not supported by RagePRO */
259 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
260 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
267 case GL_LUMINANCE_ALPHA
:
269 /* undefined, disable compositing and pass fragment unmodified */
270 mmesa
->setup
.tex_cntl
&= ~MACH64_TEXTURE_COMPOSITE
;
272 default: /* not supported by RagePRO */
273 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
274 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
279 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
280 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
283 mmesa
->setup
.tex_cntl
|= MACH64_COMP_COMBINE_MODULATE
;
289 static void mach64UpdateTextureUnit( GLcontext
*ctx
, int unit
)
291 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
292 int source
= mmesa
->tmu_source
[unit
];
293 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[source
];
294 const struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[source
]._Current
;
295 mach64TexObjPtr t
= tObj
->DriverData
;
296 GLuint d
= mmesa
->setup
.dp_pix_width
;
297 GLuint s
= mmesa
->setup
.scale_3d_cntl
;
299 assert(unit
== 0 || unit
== 1); /* only two tex units */
301 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
302 fprintf( stderr
, "%s( %p, %d ) enabled=0x%x 0x%x\n",
303 __FUNCTION__
, ctx
, unit
, ctx
->Texture
.Unit
[0]._ReallyEnabled
,
304 ctx
->Texture
.Unit
[1]._ReallyEnabled
);
307 if (texUnit
->_ReallyEnabled
& (TEXTURE_1D_BIT
| TEXTURE_2D_BIT
)) {
309 assert(t
); /* should have driver tex data by now */
311 /* Fallback if there's a texture border */
312 if ( tObj
->Image
[0][tObj
->BaseLevel
]->Border
> 0 ) {
313 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
317 /* Upload teximages */
318 if (t
->base
.dirty_images
[0]) {
319 mach64SetTexImages( mmesa
, tObj
);
320 mmesa
->dirty
|= (MACH64_UPLOAD_TEX0IMAGE
<< unit
);
323 /* Bind to the given texture unit */
324 mmesa
->CurrentTexObj
[unit
] = t
;
325 t
->base
.bound
|= (1 << unit
);
327 if ( t
->base
.memBlock
)
328 driUpdateTextureLRU( (driTextureObject
*) t
); /* XXX: should be locked! */
332 d
&= ~MACH64_SCALE_PIX_WIDTH_MASK
;
333 d
|= (t
->textureFormat
<< 28);
335 s
&= ~(MACH64_TEXTURE_DISABLE
|
336 MACH64_TEX_CACHE_SPLIT
|
337 MACH64_TEX_BLEND_FCN_MASK
|
340 if ( mmesa
->multitex
) {
341 s
|= MACH64_TEX_BLEND_FCN_TRILINEAR
| MACH64_TEX_CACHE_SPLIT
;
342 } else if ( t
->BilinearMin
) {
343 s
|= MACH64_TEX_BLEND_FCN_LINEAR
;
345 s
|= MACH64_TEX_BLEND_FCN_NEAREST
;
347 if ( t
->BilinearMag
) {
348 s
|= MACH64_BILINEAR_TEX_EN
;
350 s
&= ~MACH64_BILINEAR_TEX_EN
;
354 s
|= MACH64_TEX_MAP_AEN
;
357 mmesa
->setup
.tex_cntl
&= ~(MACH64_TEXTURE_CLAMP_S
|
358 MACH64_TEXTURE_CLAMP_T
|
359 MACH64_SECONDARY_STW
);
362 mmesa
->setup
.tex_cntl
|= MACH64_TEXTURE_CLAMP_S
;
365 mmesa
->setup
.tex_cntl
|= MACH64_TEXTURE_CLAMP_T
;
368 mmesa
->setup
.tex_size_pitch
|= ((t
->widthLog2
<< 0) |
370 (t
->heightLog2
<< 8));
373 /* Enable texture mapping mode */
374 s
&= ~MACH64_TEXTURE_DISABLE
;
376 d
&= ~MACH64_COMPOSITE_PIX_WIDTH_MASK
;
377 d
|= (t
->textureFormat
<< 4);
379 mmesa
->setup
.tex_cntl
&= ~(MACH64_COMP_ALPHA
|
380 MACH64_SEC_TEX_CLAMP_S
|
381 MACH64_SEC_TEX_CLAMP_T
);
382 mmesa
->setup
.tex_cntl
|= (MACH64_TEXTURE_COMPOSITE
|
383 MACH64_SECONDARY_STW
);
385 if ( t
->BilinearMin
) {
386 mmesa
->setup
.tex_cntl
|= MACH64_COMP_BLEND_BILINEAR
;
388 mmesa
->setup
.tex_cntl
&= ~MACH64_COMP_BLEND_BILINEAR
;
390 if ( t
->BilinearMag
) {
391 mmesa
->setup
.tex_cntl
|= MACH64_COMP_FILTER_BILINEAR
;
393 mmesa
->setup
.tex_cntl
&= ~MACH64_COMP_FILTER_BILINEAR
;
397 mmesa
->setup
.tex_cntl
|= MACH64_COMP_ALPHA
;
400 mmesa
->setup
.tex_cntl
|= MACH64_SEC_TEX_CLAMP_S
;
403 mmesa
->setup
.tex_cntl
|= MACH64_SEC_TEX_CLAMP_T
;
406 mmesa
->setup
.tex_size_pitch
|= ((t
->widthLog2
<< 16) |
408 (t
->heightLog2
<< 24));
411 if ( mmesa
->setup
.scale_3d_cntl
!= s
) {
412 mmesa
->setup
.scale_3d_cntl
= s
;
413 mmesa
->dirty
|= MACH64_UPLOAD_SCALE_3D_CNTL
;
416 if ( mmesa
->setup
.dp_pix_width
!= d
) {
417 mmesa
->setup
.dp_pix_width
= d
;
418 mmesa
->dirty
|= MACH64_UPLOAD_DP_PIX_WIDTH
;
421 else if (texUnit
->_ReallyEnabled
) {
422 /* 3D or cube map texture enabled - fallback */
423 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_TRUE
);
426 /* texture unit disabled */
431 /* Update the hardware texture state */
432 void mach64UpdateTextureState( GLcontext
*ctx
)
434 mach64ContextPtr mmesa
= MACH64_CONTEXT(ctx
);
436 if ( MACH64_DEBUG
& DEBUG_VERBOSE_API
) {
437 fprintf( stderr
, "%s( %p ) en=0x%x 0x%x\n",
438 __FUNCTION__
, ctx
, ctx
->Texture
.Unit
[0]._ReallyEnabled
,
439 ctx
->Texture
.Unit
[1]._ReallyEnabled
);
442 /* Clear any texturing fallbacks */
443 FALLBACK( mmesa
, MACH64_FALLBACK_TEXTURE
, GL_FALSE
);
445 /* Unbind any currently bound textures */
446 if ( mmesa
->CurrentTexObj
[0] ) mmesa
->CurrentTexObj
[0]->base
.bound
= 0;
447 if ( mmesa
->CurrentTexObj
[1] ) mmesa
->CurrentTexObj
[1]->base
.bound
= 0;
448 mmesa
->CurrentTexObj
[0] = NULL
;
449 mmesa
->CurrentTexObj
[1] = NULL
;
451 /* Disable all texturing until it is known to be good */
452 mmesa
->setup
.scale_3d_cntl
|= MACH64_TEXTURE_DISABLE
;
453 mmesa
->setup
.scale_3d_cntl
&= ~MACH64_TEX_MAP_AEN
;
454 mmesa
->setup
.tex_cntl
&= ~MACH64_TEXTURE_COMPOSITE
;
456 mmesa
->setup
.tex_size_pitch
= 0x00000000;
458 mmesa
->tmu_source
[0] = 0;
459 mmesa
->tmu_source
[1] = 1;
462 if (ctx
->Texture
._EnabledUnits
& 0x2) {
464 if (ctx
->Texture
._EnabledUnits
& 0x1) {
465 /* units 0 and 1 enabled */
467 mach64UpdateTextureUnit( ctx
, 0 );
468 mach64UpdateTextureEnv( ctx
, 0 );
469 mach64UpdateTextureUnit( ctx
, 1 );
470 mach64UpdateTextureEnv( ctx
, 1 );
472 mmesa
->tmu_source
[0] = 1;
473 mmesa
->tmu_source
[1] = 0;
474 mach64UpdateTextureUnit( ctx
, 0 );
475 mach64UpdateTextureEnv( ctx
, 0 );
477 } else if (ctx
->Texture
._EnabledUnits
& 0x1) {
478 /* only unit 0 enabled */
479 mach64UpdateTextureUnit( ctx
, 0 );
480 mach64UpdateTextureEnv( ctx
, 0 );
483 mmesa
->dirty
|= (MACH64_UPLOAD_SCALE_3D_CNTL
|
484 MACH64_UPLOAD_TEXTURE
);
488 /* Due to the way we must program texture state into the Rage Pro,
489 * we must leave these calculations to the absolute last minute.
491 void mach64EmitTexStateLocked( mach64ContextPtr mmesa
,
495 drm_mach64_sarea_t
*sarea
= mmesa
->sarea
;
496 drm_mach64_context_regs_t
*regs
= &(mmesa
->setup
);
498 /* for multitex, both textures must be local or AGP */
500 assert(t0
->heap
== t1
->heap
);
503 if (t0
->heap
== MACH64_CARD_HEAP
) {
504 #if ENABLE_PERF_BOXES
505 mmesa
->c_texsrc_card
++;
507 mmesa
->setup
.tex_cntl
&= ~MACH64_TEX_SRC_AGP
;
509 #if ENABLE_PERF_BOXES
510 mmesa
->c_texsrc_agp
++;
512 mmesa
->setup
.tex_cntl
|= MACH64_TEX_SRC_AGP
;
514 mmesa
->setup
.tex_offset
= t0
->bufAddr
;
518 mmesa
->setup
.secondary_tex_off
= t1
->bufAddr
;
521 memcpy( &sarea
->context_state
.tex_size_pitch
, ®s
->tex_size_pitch
,
522 MACH64_NR_TEXTURE_REGS
* sizeof(GLuint
) );