2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * 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.
28 * Texture state handling.
32 #include "mfeatures.h"
33 #include "bufferobj.h"
47 * Default texture combine environment state. This is used to initialize
48 * a context's texture units and as the basis for converting "classic"
49 * texture environmnets to ARB_texture_env_combine style values.
51 static const struct gl_tex_env_combine_state default_combine_state
= {
52 GL_MODULATE
, GL_MODULATE
,
53 { GL_TEXTURE
, GL_PREVIOUS
, GL_CONSTANT
, GL_CONSTANT
},
54 { GL_TEXTURE
, GL_PREVIOUS
, GL_CONSTANT
, GL_CONSTANT
},
55 { GL_SRC_COLOR
, GL_SRC_COLOR
, GL_SRC_ALPHA
, GL_SRC_ALPHA
},
56 { GL_SRC_ALPHA
, GL_SRC_ALPHA
, GL_SRC_ALPHA
, GL_SRC_ALPHA
},
64 * Used by glXCopyContext to copy texture state from one context to another.
67 _mesa_copy_texture_state( const struct gl_context
*src
, struct gl_context
*dst
)
74 dst
->Texture
._GenFlags
= src
->Texture
._GenFlags
;
75 dst
->Texture
._TexGenEnabled
= src
->Texture
._TexGenEnabled
;
76 dst
->Texture
._TexMatEnabled
= src
->Texture
._TexMatEnabled
;
78 dst
->Texture
.Unit
.Enabled
= src
->Texture
.Unit
.Enabled
;
79 dst
->Texture
.Unit
.EnvMode
= src
->Texture
.Unit
.EnvMode
;
80 COPY_4V(dst
->Texture
.Unit
.EnvColor
, src
->Texture
.Unit
.EnvColor
);
81 dst
->Texture
.Unit
.TexGenEnabled
= src
->Texture
.Unit
.TexGenEnabled
;
82 dst
->Texture
.Unit
.GenS
= src
->Texture
.Unit
.GenS
;
83 dst
->Texture
.Unit
.GenT
= src
->Texture
.Unit
.GenT
;
84 dst
->Texture
.Unit
.GenR
= src
->Texture
.Unit
.GenR
;
85 dst
->Texture
.Unit
.GenQ
= src
->Texture
.Unit
.GenQ
;
86 dst
->Texture
.Unit
.LodBias
= src
->Texture
.Unit
.LodBias
;
88 /* GL_EXT_texture_env_combine */
89 dst
->Texture
.Unit
.Combine
= src
->Texture
.Unit
.Combine
;
92 * XXX strictly speaking, we should compare texture names/ids and
93 * bind textures in the dest context according to id. For now, only
94 * copy bindings if the contexts share the same pool of textures to
95 * avoid refcounting bugs.
97 if (dst
->Shared
== src
->Shared
) {
98 /* copy texture object bindings, not contents of texture objects */
99 _mesa_lock_context_textures(dst
);
101 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
102 _mesa_reference_texobj(&dst
->Texture
.Unit
.CurrentTex
[tex
],
103 src
->Texture
.Unit
.CurrentTex
[tex
]);
105 _mesa_unlock_context_textures(dst
);
114 _mesa_print_texunit_state( struct gl_context
*ctx
)
116 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
;
117 printf("Texture Unit\n");
118 printf(" GL_TEXTURE_ENV_MODE = %s\n", _mesa_lookup_enum_by_nr(texUnit
->EnvMode
));
119 printf(" GL_COMBINE_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.ModeRGB
));
120 printf(" GL_COMBINE_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.ModeA
));
121 printf(" GL_SOURCE0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceRGB
[0]));
122 printf(" GL_SOURCE1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceRGB
[1]));
123 printf(" GL_SOURCE2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceRGB
[2]));
124 printf(" GL_SOURCE0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceA
[0]));
125 printf(" GL_SOURCE1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceA
[1]));
126 printf(" GL_SOURCE2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceA
[2]));
127 printf(" GL_OPERAND0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandRGB
[0]));
128 printf(" GL_OPERAND1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandRGB
[1]));
129 printf(" GL_OPERAND2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandRGB
[2]));
130 printf(" GL_OPERAND0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandA
[0]));
131 printf(" GL_OPERAND1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandA
[1]));
132 printf(" GL_OPERAND2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandA
[2]));
133 printf(" GL_RGB_SCALE = %d\n", 1 << texUnit
->Combine
.ScaleShiftRGB
);
134 printf(" GL_ALPHA_SCALE = %d\n", 1 << texUnit
->Combine
.ScaleShiftA
);
135 printf(" GL_TEXTURE_ENV_COLOR = (%f, %f, %f, %f)\n", texUnit
->EnvColor
[0], texUnit
->EnvColor
[1], texUnit
->EnvColor
[2], texUnit
->EnvColor
[3]);
140 /**********************************************************************/
141 /* Texture Environment */
142 /**********************************************************************/
145 * Convert "classic" texture environment to ARB_texture_env_combine style
148 * \param state texture_env_combine state vector to be filled-in.
149 * \param mode Classic texture environment mode (i.e., \c GL_REPLACE,
150 * \c GL_BLEND, \c GL_DECAL, etc.).
151 * \param texBaseFormat Base format of the texture associated with the
155 calculate_derived_texenv( struct gl_tex_env_combine_state
*state
,
156 GLenum mode
, GLenum texBaseFormat
)
161 *state
= default_combine_state
;
163 switch (texBaseFormat
) {
165 state
->SourceRGB
[0] = GL_PREVIOUS
;
168 case GL_LUMINANCE_ALPHA
:
178 state
->SourceA
[0] = GL_PREVIOUS
;
183 "Invalid texBaseFormat 0x%x in calculate_derived_texenv",
188 if (mode
== GL_REPLACE_EXT
)
194 mode_rgb
= (texBaseFormat
== GL_ALPHA
) ? GL_REPLACE
: mode
;
199 mode_rgb
= GL_INTERPOLATE
;
202 state
->SourceA
[0] = GL_PREVIOUS
;
204 /* Having alpha / luminance / intensity textures replace using the
205 * incoming fragment color matches the definition in NV_texture_shader.
206 * The 1.5 spec simply marks these as "undefined".
208 switch (texBaseFormat
) {
211 case GL_LUMINANCE_ALPHA
:
213 state
->SourceRGB
[0] = GL_PREVIOUS
;
219 mode_rgb
= GL_REPLACE
;
222 state
->SourceRGB
[2] = GL_TEXTURE
;
228 mode_rgb
= GL_INTERPOLATE
;
229 mode_a
= GL_MODULATE
;
231 switch (texBaseFormat
) {
233 mode_rgb
= GL_REPLACE
;
236 mode_a
= GL_INTERPOLATE
;
237 state
->SourceA
[0] = GL_CONSTANT
;
238 state
->OperandA
[2] = GL_SRC_ALPHA
;
244 case GL_LUMINANCE_ALPHA
:
247 state
->SourceRGB
[2] = GL_TEXTURE
;
248 state
->SourceA
[2] = GL_TEXTURE
;
249 state
->SourceRGB
[0] = GL_CONSTANT
;
250 state
->OperandRGB
[2] = GL_SRC_COLOR
;
256 mode_rgb
= (texBaseFormat
== GL_ALPHA
) ? GL_REPLACE
: GL_ADD
;
257 mode_a
= (texBaseFormat
== GL_INTENSITY
) ? GL_ADD
: GL_MODULATE
;
262 "Invalid texture env mode 0x%x in calculate_derived_texenv",
267 state
->ModeRGB
= (state
->SourceRGB
[0] != GL_PREVIOUS
)
268 ? mode_rgb
: GL_REPLACE
;
269 state
->ModeA
= (state
->SourceA
[0] != GL_PREVIOUS
)
270 ? mode_a
: GL_REPLACE
;
273 /**********************************************************************/
274 /***** State management *****/
275 /**********************************************************************/
279 * \note This routine refers to derived texture attribute values to
280 * compute the ENABLE_TEXMAT flags, but is only called on
281 * _NEW_TEXTURE_MATRIX. On changes to _NEW_TEXTURE, the ENABLE_TEXMAT
282 * flags are updated by _mesa_update_textures(), below.
284 * \param ctx GL context.
287 update_texture_matrices( struct gl_context
*ctx
)
289 ctx
->Texture
._TexMatEnabled
= 0x0;
291 if (_math_matrix_is_dirty(ctx
->TextureMatrixStack
.Top
)) {
292 _math_matrix_analyse( ctx
->TextureMatrixStack
.Top
);
294 if (ctx
->Texture
.Unit
._ReallyEnabled
&&
295 ctx
->TextureMatrixStack
.Top
->type
!= MATRIX_IDENTITY
)
296 ctx
->Texture
._TexMatEnabled
= GL_TRUE
;
302 * Examine texture unit's combine/env state to update derived state.
305 update_tex_combine(struct gl_context
*ctx
, struct gl_texture_unit
*texUnit
)
307 struct gl_tex_env_combine_state
*combine
;
309 /* Set the texUnit->_CurrentCombine field to point to the user's combiner
310 * state, or the combiner state which is derived from traditional texenv
313 if (texUnit
->EnvMode
== GL_COMBINE
||
314 texUnit
->EnvMode
== GL_COMBINE4_NV
) {
315 texUnit
->_CurrentCombine
= & texUnit
->Combine
;
318 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
319 GLenum format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
321 calculate_derived_texenv(&texUnit
->_EnvMode
, texUnit
->EnvMode
, format
);
322 texUnit
->_CurrentCombine
= & texUnit
->_EnvMode
;
325 combine
= texUnit
->_CurrentCombine
;
327 /* Determine number of source RGB terms in the combiner function */
328 switch (combine
->ModeRGB
) {
330 combine
->_NumArgsRGB
= 1;
334 if (texUnit
->EnvMode
== GL_COMBINE4_NV
)
335 combine
->_NumArgsRGB
= 4;
337 combine
->_NumArgsRGB
= 2;
343 case GL_DOT3_RGB_EXT
:
344 case GL_DOT3_RGBA_EXT
:
345 combine
->_NumArgsRGB
= 2;
348 case GL_MODULATE_ADD_ATI
:
349 case GL_MODULATE_SIGNED_ADD_ATI
:
350 case GL_MODULATE_SUBTRACT_ATI
:
351 combine
->_NumArgsRGB
= 3;
354 combine
->_NumArgsRGB
= 0;
355 _mesa_problem(ctx
, "invalid RGB combine mode in update_texture_state");
359 /* Determine number of source Alpha terms in the combiner function */
360 switch (combine
->ModeA
) {
362 combine
->_NumArgsA
= 1;
366 if (texUnit
->EnvMode
== GL_COMBINE4_NV
)
367 combine
->_NumArgsA
= 4;
369 combine
->_NumArgsA
= 2;
373 combine
->_NumArgsA
= 2;
376 case GL_MODULATE_ADD_ATI
:
377 case GL_MODULATE_SIGNED_ADD_ATI
:
378 case GL_MODULATE_SUBTRACT_ATI
:
379 combine
->_NumArgsA
= 3;
382 combine
->_NumArgsA
= 0;
383 _mesa_problem(ctx
, "invalid Alpha combine mode in update_texture_state");
390 * \note This routine refers to derived texture matrix values to
391 * compute the ENABLE_TEXMAT flags, but is only called on
392 * _NEW_TEXTURE. On changes to _NEW_TEXTURE_MATRIX, the ENABLE_TEXMAT
393 * flags are updated by _mesa_update_texture_matrices, above.
395 * \param ctx GL context.
398 update_texture_state( struct gl_context
*ctx
)
400 /* TODO: only set this if there are actual changes */
401 ctx
->NewState
|= _NEW_TEXTURE
;
403 ctx
->Texture
._Enabled
= GL_FALSE
;
404 ctx
->Texture
._GenFlags
= 0x0;
405 ctx
->Texture
._TexMatEnabled
= GL_FALSE
;
406 ctx
->Texture
._TexGenEnabled
= GL_FALSE
;
409 * Update texture unit state.
412 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
;
413 GLbitfield enabledTargets
= texUnit
->Enabled
;
415 texUnit
->_ReallyEnabled
= 0x0;
417 if (enabledTargets
) {
419 /* Look for the highest priority texture target that's enabled (or used
420 * by the vert/frag shaders) and "complete". That's the one we'll use
421 * for texturing. If we're using vert/frag program we're guaranteed
422 * that bitcount(enabledBits) <= 1.
423 * Note that the TEXTURE_x_INDEX values are in high to low priority.
425 for (texIndex
= 0; texIndex
< NUM_TEXTURE_TARGETS
; texIndex
++) {
426 if (enabledTargets
& (1 << texIndex
)) {
427 struct gl_texture_object
*texObj
= texUnit
->CurrentTex
[texIndex
];
428 if (!texObj
->_Complete
) {
429 _mesa_test_texobj_completeness(ctx
, texObj
);
431 if (texObj
->_Complete
) {
432 texUnit
->_ReallyEnabled
= 1 << texIndex
;
433 _mesa_reference_texobj(&texUnit
->_Current
, texObj
);
439 if (texUnit
->_ReallyEnabled
) {
440 /* if we get here, we know this texture unit is enabled */
441 ctx
->Texture
._Enabled
= GL_TRUE
;
442 update_tex_combine(ctx
, texUnit
);
448 /* Determine if texture coordinates are actually needed */
449 ctx
->Texture
._EnabledCoord
= ctx
->Texture
._Enabled
;
451 /* Setup texgen for those texture coordinate sets that are in use */
452 if(ctx
->Texture
._EnabledCoord
)
454 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
;
456 texUnit
->_GenFlags
= 0x0;
458 if (texUnit
->TexGenEnabled
) {
459 if (texUnit
->TexGenEnabled
& S_BIT
) {
460 texUnit
->_GenFlags
|= texUnit
->GenS
._ModeBit
;
462 if (texUnit
->TexGenEnabled
& T_BIT
) {
463 texUnit
->_GenFlags
|= texUnit
->GenT
._ModeBit
;
465 if (texUnit
->TexGenEnabled
& R_BIT
) {
466 texUnit
->_GenFlags
|= texUnit
->GenR
._ModeBit
;
468 if (texUnit
->TexGenEnabled
& Q_BIT
) {
469 texUnit
->_GenFlags
|= texUnit
->GenQ
._ModeBit
;
472 ctx
->Texture
._TexGenEnabled
= GL_TRUE
;
473 ctx
->Texture
._GenFlags
|= texUnit
->_GenFlags
;
476 if (ctx
->TextureMatrixStack
.Top
->type
!= MATRIX_IDENTITY
)
477 ctx
->Texture
._TexMatEnabled
= GL_TRUE
;
483 * Update texture-related derived state.
486 _mesa_update_texture( struct gl_context
*ctx
, GLuint new_state
)
488 if (new_state
& _NEW_TEXTURE_MATRIX
)
489 update_texture_matrices( ctx
);
491 if (new_state
& _NEW_TEXTURE
)
492 update_texture_state( ctx
);
496 /**********************************************************************/
497 /***** Initialization *****/
498 /**********************************************************************/
501 * Allocate the proxy textures for the given context.
503 * \param ctx the context to allocate proxies for.
505 * \return GL_TRUE on success, or GL_FALSE on failure
507 * If run out of memory part way through the allocations, clean up and return
511 alloc_proxy_textures( struct gl_context
*ctx
)
513 /* NOTE: these values must be in the same order as the TEXTURE_x_INDEX
516 static const GLenum targets
[] = {
517 GL_TEXTURE_CUBE_MAP_ARB
,
524 STATIC_ASSERT(Elements(targets
) == NUM_TEXTURE_TARGETS
);
525 assert(targets
[TEXTURE_2D_INDEX
] == GL_TEXTURE_2D
);
526 assert(targets
[TEXTURE_CUBE_INDEX
] == GL_TEXTURE_CUBE_MAP
);
528 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
529 if (!(ctx
->Texture
.ProxyTex
[tgt
]
530 = ctx
->Driver
.NewTextureObject(ctx
, 0, targets
[tgt
]))) {
531 /* out of memory, free what we did allocate */
533 ctx
->Driver
.DeleteTexture(ctx
, ctx
->Texture
.ProxyTex
[tgt
]);
539 assert(ctx
->Texture
.ProxyTex
[0]->RefCount
== 1); /* sanity check */
545 * Initialize a texture unit.
547 * \param ctx GL context.
548 * \param unit texture unit number to be initialized.
551 init_texture_unit( struct gl_context
*ctx
)
553 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
;
556 texUnit
->EnvMode
= GL_MODULATE
;
557 ASSIGN_4V( texUnit
->EnvColor
, 0.0, 0.0, 0.0, 0.0 );
559 texUnit
->Combine
= default_combine_state
;
560 texUnit
->_EnvMode
= default_combine_state
;
561 texUnit
->_CurrentCombine
= & texUnit
->_EnvMode
;
563 texUnit
->TexGenEnabled
= 0x0;
564 texUnit
->GenS
.Mode
= GL_EYE_LINEAR
;
565 texUnit
->GenT
.Mode
= GL_EYE_LINEAR
;
566 texUnit
->GenR
.Mode
= GL_EYE_LINEAR
;
567 texUnit
->GenQ
.Mode
= GL_EYE_LINEAR
;
568 texUnit
->GenS
._ModeBit
= TEXGEN_EYE_LINEAR
;
569 texUnit
->GenT
._ModeBit
= TEXGEN_EYE_LINEAR
;
570 texUnit
->GenR
._ModeBit
= TEXGEN_EYE_LINEAR
;
571 texUnit
->GenQ
._ModeBit
= TEXGEN_EYE_LINEAR
;
573 /* Yes, these plane coefficients are correct! */
574 ASSIGN_4V( texUnit
->GenS
.ObjectPlane
, 1.0, 0.0, 0.0, 0.0 );
575 ASSIGN_4V( texUnit
->GenT
.ObjectPlane
, 0.0, 1.0, 0.0, 0.0 );
576 ASSIGN_4V( texUnit
->GenR
.ObjectPlane
, 0.0, 0.0, 0.0, 0.0 );
577 ASSIGN_4V( texUnit
->GenQ
.ObjectPlane
, 0.0, 0.0, 0.0, 0.0 );
578 ASSIGN_4V( texUnit
->GenS
.EyePlane
, 1.0, 0.0, 0.0, 0.0 );
579 ASSIGN_4V( texUnit
->GenT
.EyePlane
, 0.0, 1.0, 0.0, 0.0 );
580 ASSIGN_4V( texUnit
->GenR
.EyePlane
, 0.0, 0.0, 0.0, 0.0 );
581 ASSIGN_4V( texUnit
->GenQ
.EyePlane
, 0.0, 0.0, 0.0, 0.0 );
583 /* initialize current texture object ptrs to the shared default objects */
584 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
585 _mesa_reference_texobj(&texUnit
->CurrentTex
[tex
],
586 ctx
->Shared
->DefaultTex
[tex
]);
592 * Initialize texture state for the given context.
595 _mesa_init_texture(struct gl_context
*ctx
)
598 ctx
->Texture
._Enabled
= GL_FALSE
;
600 init_texture_unit(ctx
);
602 /* Allocate proxy textures */
603 if (!alloc_proxy_textures( ctx
))
611 * Free dynamically-allocted texture data attached to the given context.
614 _mesa_free_texture_data(struct gl_context
*ctx
)
618 /* unreference current textures */
619 /* The _Current texture could account for another reference */
620 _mesa_reference_texobj(&ctx
->Texture
.Unit
._Current
, NULL
);
622 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
623 _mesa_reference_texobj(&ctx
->Texture
.Unit
.CurrentTex
[tgt
], NULL
);
626 /* Free proxy texture objects */
627 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++)
628 ctx
->Driver
.DeleteTexture(ctx
, ctx
->Texture
.ProxyTex
[tgt
]);
630 #if FEATURE_sampler_objects
631 _mesa_reference_sampler_object(ctx
, &ctx
->Texture
.Unit
.Sampler
, NULL
);
637 * Update the default texture objects in the given context to reference those
638 * specified in the shared state and release those referencing the old
642 _mesa_update_default_objects_texture(struct gl_context
*ctx
)
645 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
;
646 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
647 _mesa_reference_texobj(&texUnit
->CurrentTex
[tex
],
648 ctx
->Shared
->DefaultTex
[tex
]);