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.
34 * Default texture combine environment state. This is used to initialize
35 * a context's texture units and as the basis for converting "classic"
36 * texture environmnets to ARB_texture_env_combine style values.
38 static const struct gl_tex_env_combine_state default_combine_state
= {
39 GL_MODULATE
, GL_MODULATE
,
40 { GL_TEXTURE
, GL_PREVIOUS
, GL_CONSTANT
, GL_CONSTANT
},
41 { GL_TEXTURE
, GL_PREVIOUS
, GL_CONSTANT
, GL_CONSTANT
},
42 { GL_SRC_COLOR
, GL_SRC_COLOR
, GL_SRC_ALPHA
, GL_SRC_ALPHA
},
43 { GL_SRC_ALPHA
, GL_SRC_ALPHA
, GL_SRC_ALPHA
, GL_SRC_ALPHA
},
51 * Used by glXCopyContext to copy texture state from one context to another.
54 _mesa_copy_texture_state( const struct gl_context
*src
, struct gl_context
*dst
)
61 dst
->Texture
._GenFlags
= src
->Texture
._GenFlags
;
62 dst
->Texture
._TexGenEnabled
= src
->Texture
._TexGenEnabled
;
63 dst
->Texture
._TexMatEnabled
= src
->Texture
._TexMatEnabled
;
65 dst
->Texture
.Unit
.Enabled
= src
->Texture
.Unit
.Enabled
;
66 dst
->Texture
.Unit
.EnvMode
= src
->Texture
.Unit
.EnvMode
;
67 COPY_4V(dst
->Texture
.Unit
.EnvColor
, src
->Texture
.Unit
.EnvColor
);
68 dst
->Texture
.Unit
.TexGenEnabled
= src
->Texture
.Unit
.TexGenEnabled
;
69 dst
->Texture
.Unit
.GenS
= src
->Texture
.Unit
.GenS
;
70 dst
->Texture
.Unit
.GenT
= src
->Texture
.Unit
.GenT
;
71 dst
->Texture
.Unit
.GenR
= src
->Texture
.Unit
.GenR
;
72 dst
->Texture
.Unit
.GenQ
= src
->Texture
.Unit
.GenQ
;
73 dst
->Texture
.Unit
.LodBias
= src
->Texture
.Unit
.LodBias
;
75 /* GL_EXT_texture_env_combine */
76 dst
->Texture
.Unit
.Combine
= src
->Texture
.Unit
.Combine
;
79 * XXX strictly speaking, we should compare texture names/ids and
80 * bind textures in the dest context according to id. For now, only
81 * copy bindings if the contexts share the same pool of textures to
82 * avoid refcounting bugs.
84 if (dst
->Shared
== src
->Shared
) {
85 /* copy texture object bindings, not contents of texture objects */
86 _mesa_lock_context_textures(dst
);
88 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
89 _mesa_reference_texobj(&dst
->Texture
.Unit
.CurrentTex
[tex
],
90 src
->Texture
.Unit
.CurrentTex
[tex
]);
92 _mesa_unlock_context_textures(dst
);
101 _mesa_print_texunit_state( struct gl_context
*ctx
)
103 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
;
104 printf("Texture Unit\n");
105 printf(" GL_TEXTURE_ENV_MODE = %s\n", _mesa_lookup_enum_by_nr(texUnit
->EnvMode
));
106 printf(" GL_COMBINE_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.ModeRGB
));
107 printf(" GL_COMBINE_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.ModeA
));
108 printf(" GL_SOURCE0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceRGB
[0]));
109 printf(" GL_SOURCE1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceRGB
[1]));
110 printf(" GL_SOURCE2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceRGB
[2]));
111 printf(" GL_SOURCE0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceA
[0]));
112 printf(" GL_SOURCE1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceA
[1]));
113 printf(" GL_SOURCE2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceA
[2]));
114 printf(" GL_OPERAND0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandRGB
[0]));
115 printf(" GL_OPERAND1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandRGB
[1]));
116 printf(" GL_OPERAND2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandRGB
[2]));
117 printf(" GL_OPERAND0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandA
[0]));
118 printf(" GL_OPERAND1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandA
[1]));
119 printf(" GL_OPERAND2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandA
[2]));
120 printf(" GL_RGB_SCALE = %d\n", 1 << texUnit
->Combine
.ScaleShiftRGB
);
121 printf(" GL_ALPHA_SCALE = %d\n", 1 << texUnit
->Combine
.ScaleShiftA
);
122 printf(" GL_TEXTURE_ENV_COLOR = (%f, %f, %f, %f)\n", texUnit
->EnvColor
[0], texUnit
->EnvColor
[1], texUnit
->EnvColor
[2], texUnit
->EnvColor
[3]);
127 /**********************************************************************/
128 /* Texture Environment */
129 /**********************************************************************/
132 * Convert "classic" texture environment to ARB_texture_env_combine style
135 * \param state texture_env_combine state vector to be filled-in.
136 * \param mode Classic texture environment mode (i.e., \c GL_REPLACE,
137 * \c GL_BLEND, \c GL_DECAL, etc.).
138 * \param texBaseFormat Base format of the texture associated with the
142 calculate_derived_texenv( struct gl_tex_env_combine_state
*state
,
143 GLenum mode
, GLenum texBaseFormat
)
148 *state
= default_combine_state
;
150 switch (texBaseFormat
) {
152 state
->SourceRGB
[0] = GL_PREVIOUS
;
155 case GL_LUMINANCE_ALPHA
:
165 state
->SourceA
[0] = GL_PREVIOUS
;
170 "Invalid texBaseFormat 0x%x in calculate_derived_texenv",
175 if (mode
== GL_REPLACE_EXT
)
181 mode_rgb
= (texBaseFormat
== GL_ALPHA
) ? GL_REPLACE
: mode
;
186 mode_rgb
= GL_INTERPOLATE
;
189 state
->SourceA
[0] = GL_PREVIOUS
;
191 /* Having alpha / luminance / intensity textures replace using the
192 * incoming fragment color matches the definition in NV_texture_shader.
193 * The 1.5 spec simply marks these as "undefined".
195 switch (texBaseFormat
) {
198 case GL_LUMINANCE_ALPHA
:
200 state
->SourceRGB
[0] = GL_PREVIOUS
;
206 mode_rgb
= GL_REPLACE
;
209 state
->SourceRGB
[2] = GL_TEXTURE
;
215 mode_rgb
= GL_INTERPOLATE
;
216 mode_a
= GL_MODULATE
;
218 switch (texBaseFormat
) {
220 mode_rgb
= GL_REPLACE
;
223 mode_a
= GL_INTERPOLATE
;
224 state
->SourceA
[0] = GL_CONSTANT
;
225 state
->OperandA
[2] = GL_SRC_ALPHA
;
231 case GL_LUMINANCE_ALPHA
:
234 state
->SourceRGB
[2] = GL_TEXTURE
;
235 state
->SourceA
[2] = GL_TEXTURE
;
236 state
->SourceRGB
[0] = GL_CONSTANT
;
237 state
->OperandRGB
[2] = GL_SRC_COLOR
;
243 mode_rgb
= (texBaseFormat
== GL_ALPHA
) ? GL_REPLACE
: GL_ADD
;
244 mode_a
= (texBaseFormat
== GL_INTENSITY
) ? GL_ADD
: GL_MODULATE
;
249 "Invalid texture env mode 0x%x in calculate_derived_texenv",
254 state
->ModeRGB
= (state
->SourceRGB
[0] != GL_PREVIOUS
)
255 ? mode_rgb
: GL_REPLACE
;
256 state
->ModeA
= (state
->SourceA
[0] != GL_PREVIOUS
)
257 ? mode_a
: GL_REPLACE
;
260 /**********************************************************************/
261 /***** State management *****/
262 /**********************************************************************/
266 * \note This routine refers to derived texture attribute values to
267 * compute the ENABLE_TEXMAT flags, but is only called on
268 * _NEW_TEXTURE_MATRIX. On changes to _NEW_TEXTURE, the ENABLE_TEXMAT
269 * flags are updated by _mesa_update_textures(), below.
271 * \param ctx GL context.
274 update_texture_matrices( struct gl_context
*ctx
)
276 ctx
->Texture
._TexMatEnabled
= 0x0;
278 if (_math_matrix_is_dirty(ctx
->TextureMatrixStack
.Top
)) {
279 _math_matrix_analyse( ctx
->TextureMatrixStack
.Top
);
281 if (ctx
->Texture
.Unit
._ReallyEnabled
&&
282 ctx
->TextureMatrixStack
.Top
->type
!= MATRIX_IDENTITY
)
283 ctx
->Texture
._TexMatEnabled
= GL_TRUE
;
289 * Examine texture unit's combine/env state to update derived state.
292 update_tex_combine(struct gl_context
*ctx
, struct gl_texture_unit
*texUnit
)
294 struct gl_tex_env_combine_state
*combine
;
296 /* Set the texUnit->_CurrentCombine field to point to the user's combiner
297 * state, or the combiner state which is derived from traditional texenv
300 if (texUnit
->EnvMode
== GL_COMBINE
||
301 texUnit
->EnvMode
== GL_COMBINE4_NV
) {
302 texUnit
->_CurrentCombine
= & texUnit
->Combine
;
305 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
306 GLenum format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
308 calculate_derived_texenv(&texUnit
->_EnvMode
, texUnit
->EnvMode
, format
);
309 texUnit
->_CurrentCombine
= & texUnit
->_EnvMode
;
312 combine
= texUnit
->_CurrentCombine
;
314 /* Determine number of source RGB terms in the combiner function */
315 switch (combine
->ModeRGB
) {
317 combine
->_NumArgsRGB
= 1;
321 if (texUnit
->EnvMode
== GL_COMBINE4_NV
)
322 combine
->_NumArgsRGB
= 4;
324 combine
->_NumArgsRGB
= 2;
330 case GL_DOT3_RGB_EXT
:
331 case GL_DOT3_RGBA_EXT
:
332 combine
->_NumArgsRGB
= 2;
335 case GL_MODULATE_ADD_ATI
:
336 case GL_MODULATE_SIGNED_ADD_ATI
:
337 case GL_MODULATE_SUBTRACT_ATI
:
338 combine
->_NumArgsRGB
= 3;
341 combine
->_NumArgsRGB
= 0;
342 _mesa_problem(ctx
, "invalid RGB combine mode in update_texture_state");
346 /* Determine number of source Alpha terms in the combiner function */
347 switch (combine
->ModeA
) {
349 combine
->_NumArgsA
= 1;
353 if (texUnit
->EnvMode
== GL_COMBINE4_NV
)
354 combine
->_NumArgsA
= 4;
356 combine
->_NumArgsA
= 2;
360 combine
->_NumArgsA
= 2;
363 case GL_MODULATE_ADD_ATI
:
364 case GL_MODULATE_SIGNED_ADD_ATI
:
365 case GL_MODULATE_SUBTRACT_ATI
:
366 combine
->_NumArgsA
= 3;
369 combine
->_NumArgsA
= 0;
370 _mesa_problem(ctx
, "invalid Alpha combine mode in update_texture_state");
377 * \note This routine refers to derived texture matrix values to
378 * compute the ENABLE_TEXMAT flags, but is only called on
379 * _NEW_TEXTURE. On changes to _NEW_TEXTURE_MATRIX, the ENABLE_TEXMAT
380 * flags are updated by _mesa_update_texture_matrices, above.
382 * \param ctx GL context.
385 update_texture_state( struct gl_context
*ctx
)
387 /* TODO: only set this if there are actual changes */
388 ctx
->NewState
|= _NEW_TEXTURE
;
390 ctx
->Texture
._Enabled
= GL_FALSE
;
391 ctx
->Texture
._GenFlags
= 0x0;
392 ctx
->Texture
._TexMatEnabled
= GL_FALSE
;
393 ctx
->Texture
._TexGenEnabled
= GL_FALSE
;
396 * Update texture unit state.
399 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
;
400 GLbitfield enabledTargets
= texUnit
->Enabled
;
402 texUnit
->_ReallyEnabled
= 0x0;
404 if (enabledTargets
) {
406 /* Look for the highest priority texture target that's enabled (or used
407 * by the vert/frag shaders) and "complete". That's the one we'll use
408 * for texturing. If we're using vert/frag program we're guaranteed
409 * that bitcount(enabledBits) <= 1.
410 * Note that the TEXTURE_x_INDEX values are in high to low priority.
412 for (texIndex
= 0; texIndex
< NUM_TEXTURE_TARGETS
; texIndex
++) {
413 if (enabledTargets
& (1 << texIndex
)) {
414 struct gl_texture_object
*texObj
= texUnit
->CurrentTex
[texIndex
];
415 if (!texObj
->_Complete
) {
416 _mesa_test_texobj_completeness(ctx
, texObj
);
418 if (texObj
->_Complete
) {
419 texUnit
->_ReallyEnabled
= 1 << texIndex
;
420 _mesa_reference_texobj(&texUnit
->_Current
, texObj
);
426 if (texUnit
->_ReallyEnabled
) {
427 /* if we get here, we know this texture unit is enabled */
428 ctx
->Texture
._Enabled
= GL_TRUE
;
429 update_tex_combine(ctx
, texUnit
);
435 /* Determine if texture coordinates are actually needed */
436 ctx
->Texture
._EnabledCoord
= ctx
->Texture
._Enabled
;
438 /* Setup texgen for those texture coordinate sets that are in use */
439 if(ctx
->Texture
._EnabledCoord
)
441 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
;
443 texUnit
->_GenFlags
= 0x0;
445 if (texUnit
->TexGenEnabled
) {
446 if (texUnit
->TexGenEnabled
& S_BIT
) {
447 texUnit
->_GenFlags
|= texUnit
->GenS
._ModeBit
;
449 if (texUnit
->TexGenEnabled
& T_BIT
) {
450 texUnit
->_GenFlags
|= texUnit
->GenT
._ModeBit
;
452 if (texUnit
->TexGenEnabled
& R_BIT
) {
453 texUnit
->_GenFlags
|= texUnit
->GenR
._ModeBit
;
455 if (texUnit
->TexGenEnabled
& Q_BIT
) {
456 texUnit
->_GenFlags
|= texUnit
->GenQ
._ModeBit
;
459 ctx
->Texture
._TexGenEnabled
= GL_TRUE
;
460 ctx
->Texture
._GenFlags
|= texUnit
->_GenFlags
;
463 if (ctx
->TextureMatrixStack
.Top
->type
!= MATRIX_IDENTITY
)
464 ctx
->Texture
._TexMatEnabled
= GL_TRUE
;
470 * Update texture-related derived state.
473 _mesa_update_texture( struct gl_context
*ctx
, GLuint new_state
)
475 if (new_state
& _NEW_TEXTURE_MATRIX
)
476 update_texture_matrices( ctx
);
478 if (new_state
& _NEW_TEXTURE
)
479 update_texture_state( ctx
);
483 /**********************************************************************/
484 /***** Initialization *****/
485 /**********************************************************************/
488 * Allocate the proxy textures for the given context.
490 * \param ctx the context to allocate proxies for.
492 * \return GL_TRUE on success, or GL_FALSE on failure
494 * If run out of memory part way through the allocations, clean up and return
498 alloc_proxy_textures( struct gl_context
*ctx
)
500 /* NOTE: these values must be in the same order as the TEXTURE_x_INDEX
503 static const GLenum targets
[] = {
504 GL_TEXTURE_CUBE_MAP_ARB
,
510 STATIC_ASSERT(Elements(targets
) == NUM_TEXTURE_TARGETS
);
511 assert(targets
[TEXTURE_2D_INDEX
] == GL_TEXTURE_2D
);
512 assert(targets
[TEXTURE_CUBE_INDEX
] == GL_TEXTURE_CUBE_MAP
);
514 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
515 if (!(ctx
->Texture
.ProxyTex
[tgt
]
516 = ctx
->Driver
.NewTextureObject(ctx
, 0, targets
[tgt
]))) {
517 /* out of memory, free what we did allocate */
519 ctx
->Driver
.DeleteTexture(ctx
, ctx
->Texture
.ProxyTex
[tgt
]);
525 assert(ctx
->Texture
.ProxyTex
[0]->RefCount
== 1); /* sanity check */
531 * Initialize a texture unit.
533 * \param ctx GL context.
534 * \param unit texture unit number to be initialized.
537 init_texture_unit( struct gl_context
*ctx
)
539 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
;
542 texUnit
->EnvMode
= GL_MODULATE
;
543 ASSIGN_4V( texUnit
->EnvColor
, 0.0, 0.0, 0.0, 0.0 );
545 texUnit
->Combine
= default_combine_state
;
546 texUnit
->_EnvMode
= default_combine_state
;
547 texUnit
->_CurrentCombine
= & texUnit
->_EnvMode
;
549 texUnit
->TexGenEnabled
= 0x0;
550 texUnit
->GenS
.Mode
= GL_EYE_LINEAR
;
551 texUnit
->GenT
.Mode
= GL_EYE_LINEAR
;
552 texUnit
->GenR
.Mode
= GL_EYE_LINEAR
;
553 texUnit
->GenQ
.Mode
= GL_EYE_LINEAR
;
554 texUnit
->GenS
._ModeBit
= TEXGEN_EYE_LINEAR
;
555 texUnit
->GenT
._ModeBit
= TEXGEN_EYE_LINEAR
;
556 texUnit
->GenR
._ModeBit
= TEXGEN_EYE_LINEAR
;
557 texUnit
->GenQ
._ModeBit
= TEXGEN_EYE_LINEAR
;
559 /* Yes, these plane coefficients are correct! */
560 ASSIGN_4V( texUnit
->GenS
.ObjectPlane
, 1.0, 0.0, 0.0, 0.0 );
561 ASSIGN_4V( texUnit
->GenT
.ObjectPlane
, 0.0, 1.0, 0.0, 0.0 );
562 ASSIGN_4V( texUnit
->GenR
.ObjectPlane
, 0.0, 0.0, 0.0, 0.0 );
563 ASSIGN_4V( texUnit
->GenQ
.ObjectPlane
, 0.0, 0.0, 0.0, 0.0 );
564 ASSIGN_4V( texUnit
->GenS
.EyePlane
, 1.0, 0.0, 0.0, 0.0 );
565 ASSIGN_4V( texUnit
->GenT
.EyePlane
, 0.0, 1.0, 0.0, 0.0 );
566 ASSIGN_4V( texUnit
->GenR
.EyePlane
, 0.0, 0.0, 0.0, 0.0 );
567 ASSIGN_4V( texUnit
->GenQ
.EyePlane
, 0.0, 0.0, 0.0, 0.0 );
569 /* initialize current texture object ptrs to the shared default objects */
570 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
571 _mesa_reference_texobj(&texUnit
->CurrentTex
[tex
],
572 ctx
->Shared
->DefaultTex
[tex
]);
578 * Initialize texture state for the given context.
581 _mesa_init_texture(struct gl_context
*ctx
)
584 ctx
->Texture
._Enabled
= GL_FALSE
;
586 init_texture_unit(ctx
);
588 /* Allocate proxy textures */
589 if (!alloc_proxy_textures( ctx
))
597 * Free dynamically-allocted texture data attached to the given context.
600 _mesa_free_texture_data(struct gl_context
*ctx
)
604 /* unreference current textures */
605 /* The _Current texture could account for another reference */
606 _mesa_reference_texobj(&ctx
->Texture
.Unit
._Current
, NULL
);
608 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
609 _mesa_reference_texobj(&ctx
->Texture
.Unit
.CurrentTex
[tgt
], NULL
);
612 /* Free proxy texture objects */
613 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++)
614 ctx
->Driver
.DeleteTexture(ctx
, ctx
->Texture
.ProxyTex
[tgt
]);
616 #if FEATURE_sampler_objects
617 _mesa_reference_sampler_object(ctx
, &ctx
->Texture
.Unit
.Sampler
, NULL
);
623 * Update the default texture objects in the given context to reference those
624 * specified in the shared state and release those referencing the old
628 _mesa_update_default_objects_texture(struct gl_context
*ctx
)
631 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
;
632 for (tex
= 0; tex
< NUM_TEXTURE_TARGETS
; tex
++) {
633 _mesa_reference_texobj(&texUnit
->CurrentTex
[tex
],
634 ctx
->Shared
->DefaultTex
[tex
]);