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.
33 #if FEATURE_colortable
39 #include "texcompress.h"
43 #include "texenvprogram.h"
45 #include "math/m_xform.h"
50 * Default texture combine environment state. This is used to initialize
51 * a context's texture units and as the basis for converting "classic"
52 * texture environmnets to ARB_texture_env_combine style values.
54 static const struct gl_tex_env_combine_state default_combine_state
= {
55 GL_MODULATE
, GL_MODULATE
,
56 { GL_TEXTURE
, GL_PREVIOUS
, GL_CONSTANT
},
57 { GL_TEXTURE
, GL_PREVIOUS
, GL_CONSTANT
},
58 { GL_SRC_COLOR
, GL_SRC_COLOR
, GL_SRC_ALPHA
},
59 { GL_SRC_ALPHA
, GL_SRC_ALPHA
, GL_SRC_ALPHA
},
67 * Used by glXCopyContext to copy texture state from one context to another.
70 _mesa_copy_texture_state( const GLcontext
*src
, GLcontext
*dst
)
77 dst
->Texture
.CurrentUnit
= src
->Texture
.CurrentUnit
;
78 dst
->Texture
._GenFlags
= src
->Texture
._GenFlags
;
79 dst
->Texture
._TexGenEnabled
= src
->Texture
._TexGenEnabled
;
80 dst
->Texture
._TexMatEnabled
= src
->Texture
._TexMatEnabled
;
81 dst
->Texture
.SharedPalette
= src
->Texture
.SharedPalette
;
84 for (i
= 0; i
< src
->Const
.MaxTextureUnits
; i
++) {
85 dst
->Texture
.Unit
[i
].Enabled
= src
->Texture
.Unit
[i
].Enabled
;
86 dst
->Texture
.Unit
[i
].EnvMode
= src
->Texture
.Unit
[i
].EnvMode
;
87 COPY_4V(dst
->Texture
.Unit
[i
].EnvColor
, src
->Texture
.Unit
[i
].EnvColor
);
88 dst
->Texture
.Unit
[i
].TexGenEnabled
= src
->Texture
.Unit
[i
].TexGenEnabled
;
89 dst
->Texture
.Unit
[i
].GenModeS
= src
->Texture
.Unit
[i
].GenModeS
;
90 dst
->Texture
.Unit
[i
].GenModeT
= src
->Texture
.Unit
[i
].GenModeT
;
91 dst
->Texture
.Unit
[i
].GenModeR
= src
->Texture
.Unit
[i
].GenModeR
;
92 dst
->Texture
.Unit
[i
].GenModeQ
= src
->Texture
.Unit
[i
].GenModeQ
;
93 dst
->Texture
.Unit
[i
]._GenBitS
= src
->Texture
.Unit
[i
]._GenBitS
;
94 dst
->Texture
.Unit
[i
]._GenBitT
= src
->Texture
.Unit
[i
]._GenBitT
;
95 dst
->Texture
.Unit
[i
]._GenBitR
= src
->Texture
.Unit
[i
]._GenBitR
;
96 dst
->Texture
.Unit
[i
]._GenBitQ
= src
->Texture
.Unit
[i
]._GenBitQ
;
97 dst
->Texture
.Unit
[i
]._GenFlags
= src
->Texture
.Unit
[i
]._GenFlags
;
98 COPY_4V(dst
->Texture
.Unit
[i
].ObjectPlaneS
, src
->Texture
.Unit
[i
].ObjectPlaneS
);
99 COPY_4V(dst
->Texture
.Unit
[i
].ObjectPlaneT
, src
->Texture
.Unit
[i
].ObjectPlaneT
);
100 COPY_4V(dst
->Texture
.Unit
[i
].ObjectPlaneR
, src
->Texture
.Unit
[i
].ObjectPlaneR
);
101 COPY_4V(dst
->Texture
.Unit
[i
].ObjectPlaneQ
, src
->Texture
.Unit
[i
].ObjectPlaneQ
);
102 COPY_4V(dst
->Texture
.Unit
[i
].EyePlaneS
, src
->Texture
.Unit
[i
].EyePlaneS
);
103 COPY_4V(dst
->Texture
.Unit
[i
].EyePlaneT
, src
->Texture
.Unit
[i
].EyePlaneT
);
104 COPY_4V(dst
->Texture
.Unit
[i
].EyePlaneR
, src
->Texture
.Unit
[i
].EyePlaneR
);
105 COPY_4V(dst
->Texture
.Unit
[i
].EyePlaneQ
, src
->Texture
.Unit
[i
].EyePlaneQ
);
106 dst
->Texture
.Unit
[i
].LodBias
= src
->Texture
.Unit
[i
].LodBias
;
108 /* GL_EXT_texture_env_combine */
109 dst
->Texture
.Unit
[i
].Combine
.ModeRGB
= src
->Texture
.Unit
[i
].Combine
.ModeRGB
;
110 dst
->Texture
.Unit
[i
].Combine
.ModeA
= src
->Texture
.Unit
[i
].Combine
.ModeA
;
111 COPY_3V(dst
->Texture
.Unit
[i
].Combine
.SourceRGB
, src
->Texture
.Unit
[i
].Combine
.SourceRGB
);
112 COPY_3V(dst
->Texture
.Unit
[i
].Combine
.SourceA
, src
->Texture
.Unit
[i
].Combine
.SourceA
);
113 COPY_3V(dst
->Texture
.Unit
[i
].Combine
.OperandRGB
, src
->Texture
.Unit
[i
].Combine
.OperandRGB
);
114 COPY_3V(dst
->Texture
.Unit
[i
].Combine
.OperandA
, src
->Texture
.Unit
[i
].Combine
.OperandA
);
115 dst
->Texture
.Unit
[i
].Combine
.ScaleShiftRGB
= src
->Texture
.Unit
[i
].Combine
.ScaleShiftRGB
;
116 dst
->Texture
.Unit
[i
].Combine
.ScaleShiftA
= src
->Texture
.Unit
[i
].Combine
.ScaleShiftA
;
118 /* copy texture object bindings, not contents of texture objects */
119 _mesa_lock_context_textures(dst
);
121 _mesa_reference_texobj(&dst
->Texture
.Unit
[i
].Current1D
,
122 src
->Texture
.Unit
[i
].Current1D
);
123 _mesa_reference_texobj(&dst
->Texture
.Unit
[i
].Current2D
,
124 src
->Texture
.Unit
[i
].Current2D
);
125 _mesa_reference_texobj(&dst
->Texture
.Unit
[i
].Current3D
,
126 src
->Texture
.Unit
[i
].Current3D
);
127 _mesa_reference_texobj(&dst
->Texture
.Unit
[i
].CurrentCubeMap
,
128 src
->Texture
.Unit
[i
].CurrentCubeMap
);
129 _mesa_reference_texobj(&dst
->Texture
.Unit
[i
].CurrentRect
,
130 src
->Texture
.Unit
[i
].CurrentRect
);
131 _mesa_reference_texobj(&dst
->Texture
.Unit
[i
].Current1DArray
,
132 src
->Texture
.Unit
[i
].Current1DArray
);
133 _mesa_reference_texobj(&dst
->Texture
.Unit
[i
].Current2DArray
,
134 src
->Texture
.Unit
[i
].Current2DArray
);
136 _mesa_unlock_context_textures(dst
);
145 _mesa_print_texunit_state( GLcontext
*ctx
, GLuint unit
)
147 const struct gl_texture_unit
*texUnit
= ctx
->Texture
.Unit
+ unit
;
148 _mesa_printf("Texture Unit %d\n", unit
);
149 _mesa_printf(" GL_TEXTURE_ENV_MODE = %s\n", _mesa_lookup_enum_by_nr(texUnit
->EnvMode
));
150 _mesa_printf(" GL_COMBINE_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.ModeRGB
));
151 _mesa_printf(" GL_COMBINE_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.ModeA
));
152 _mesa_printf(" GL_SOURCE0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceRGB
[0]));
153 _mesa_printf(" GL_SOURCE1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceRGB
[1]));
154 _mesa_printf(" GL_SOURCE2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceRGB
[2]));
155 _mesa_printf(" GL_SOURCE0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceA
[0]));
156 _mesa_printf(" GL_SOURCE1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceA
[1]));
157 _mesa_printf(" GL_SOURCE2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.SourceA
[2]));
158 _mesa_printf(" GL_OPERAND0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandRGB
[0]));
159 _mesa_printf(" GL_OPERAND1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandRGB
[1]));
160 _mesa_printf(" GL_OPERAND2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandRGB
[2]));
161 _mesa_printf(" GL_OPERAND0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandA
[0]));
162 _mesa_printf(" GL_OPERAND1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandA
[1]));
163 _mesa_printf(" GL_OPERAND2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit
->Combine
.OperandA
[2]));
164 _mesa_printf(" GL_RGB_SCALE = %d\n", 1 << texUnit
->Combine
.ScaleShiftRGB
);
165 _mesa_printf(" GL_ALPHA_SCALE = %d\n", 1 << texUnit
->Combine
.ScaleShiftA
);
166 _mesa_printf(" GL_TEXTURE_ENV_COLOR = (%f, %f, %f, %f)\n", texUnit
->EnvColor
[0], texUnit
->EnvColor
[1], texUnit
->EnvColor
[2], texUnit
->EnvColor
[3]);
171 /**********************************************************************/
172 /* Texture Environment */
173 /**********************************************************************/
176 * Convert "classic" texture environment to ARB_texture_env_combine style
179 * \param state texture_env_combine state vector to be filled-in.
180 * \param mode Classic texture environment mode (i.e., \c GL_REPLACE,
181 * \c GL_BLEND, \c GL_DECAL, etc.).
182 * \param texBaseFormat Base format of the texture associated with the
186 calculate_derived_texenv( struct gl_tex_env_combine_state
*state
,
187 GLenum mode
, GLenum texBaseFormat
)
192 *state
= default_combine_state
;
194 switch (texBaseFormat
) {
196 state
->SourceRGB
[0] = GL_PREVIOUS
;
199 case GL_LUMINANCE_ALPHA
:
207 state
->SourceA
[0] = GL_PREVIOUS
;
211 _mesa_problem(NULL
, "Invalid texBaseFormat in calculate_derived_texenv");
215 if (mode
== GL_REPLACE_EXT
)
221 mode_rgb
= (texBaseFormat
== GL_ALPHA
) ? GL_REPLACE
: mode
;
226 mode_rgb
= GL_INTERPOLATE
;
229 state
->SourceA
[0] = GL_PREVIOUS
;
231 /* Having alpha / luminance / intensity textures replace using the
232 * incoming fragment color matches the definition in NV_texture_shader.
233 * The 1.5 spec simply marks these as "undefined".
235 switch (texBaseFormat
) {
238 case GL_LUMINANCE_ALPHA
:
240 state
->SourceRGB
[0] = GL_PREVIOUS
;
244 mode_rgb
= GL_REPLACE
;
247 state
->SourceRGB
[2] = GL_TEXTURE
;
253 mode_rgb
= GL_INTERPOLATE
;
254 mode_a
= GL_MODULATE
;
256 switch (texBaseFormat
) {
258 mode_rgb
= GL_REPLACE
;
261 mode_a
= GL_INTERPOLATE
;
262 state
->SourceA
[0] = GL_CONSTANT
;
263 state
->OperandA
[2] = GL_SRC_ALPHA
;
267 case GL_LUMINANCE_ALPHA
:
270 state
->SourceRGB
[2] = GL_TEXTURE
;
271 state
->SourceA
[2] = GL_TEXTURE
;
272 state
->SourceRGB
[0] = GL_CONSTANT
;
273 state
->OperandRGB
[2] = GL_SRC_COLOR
;
279 mode_rgb
= (texBaseFormat
== GL_ALPHA
) ? GL_REPLACE
: GL_ADD
;
280 mode_a
= (texBaseFormat
== GL_INTENSITY
) ? GL_ADD
: GL_MODULATE
;
285 "Invalid texture env mode in calculate_derived_texenv");
289 state
->ModeRGB
= (state
->SourceRGB
[0] != GL_PREVIOUS
)
290 ? mode_rgb
: GL_REPLACE
;
291 state
->ModeA
= (state
->SourceA
[0] != GL_PREVIOUS
)
292 ? mode_a
: GL_REPLACE
;
298 /* GL_ARB_multitexture */
300 _mesa_ActiveTextureARB(GLenum texture
)
302 GET_CURRENT_CONTEXT(ctx
);
303 const GLuint texUnit
= texture
- GL_TEXTURE0
;
304 ASSERT_OUTSIDE_BEGIN_END(ctx
);
306 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
307 _mesa_debug(ctx
, "glActiveTexture %s\n",
308 _mesa_lookup_enum_by_nr(texture
));
310 /* XXX error-check against max(coordunits, imageunits) */
311 if (texUnit
>= ctx
->Const
.MaxTextureUnits
) {
312 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveTexture(texture)");
316 if (ctx
->Texture
.CurrentUnit
== texUnit
)
319 FLUSH_VERTICES(ctx
, _NEW_TEXTURE
);
321 ctx
->Texture
.CurrentUnit
= texUnit
;
322 if (ctx
->Transform
.MatrixMode
== GL_TEXTURE
) {
323 /* update current stack pointer */
324 ctx
->CurrentStack
= &ctx
->TextureMatrixStack
[texUnit
];
327 if (ctx
->Driver
.ActiveTexture
) {
328 (*ctx
->Driver
.ActiveTexture
)( ctx
, (GLuint
) texUnit
);
333 /* GL_ARB_multitexture */
335 _mesa_ClientActiveTextureARB(GLenum texture
)
337 GET_CURRENT_CONTEXT(ctx
);
338 GLuint texUnit
= texture
- GL_TEXTURE0
;
339 ASSERT_OUTSIDE_BEGIN_END(ctx
);
341 if (texUnit
>= ctx
->Const
.MaxTextureCoordUnits
) {
342 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClientActiveTexture(texture)");
346 FLUSH_VERTICES(ctx
, _NEW_ARRAY
);
347 ctx
->Array
.ActiveTexture
= texUnit
;
352 /**********************************************************************/
353 /***** State management *****/
354 /**********************************************************************/
358 * \note This routine refers to derived texture attribute values to
359 * compute the ENABLE_TEXMAT flags, but is only called on
360 * _NEW_TEXTURE_MATRIX. On changes to _NEW_TEXTURE, the ENABLE_TEXMAT
361 * flags are updated by _mesa_update_textures(), below.
363 * \param ctx GL context.
366 update_texture_matrices( GLcontext
*ctx
)
370 ctx
->Texture
._TexMatEnabled
= 0;
372 for (i
=0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
373 if (_math_matrix_is_dirty(ctx
->TextureMatrixStack
[i
].Top
)) {
374 _math_matrix_analyse( ctx
->TextureMatrixStack
[i
].Top
);
376 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
&&
377 ctx
->TextureMatrixStack
[i
].Top
->type
!= MATRIX_IDENTITY
)
378 ctx
->Texture
._TexMatEnabled
|= ENABLE_TEXMAT(i
);
380 if (ctx
->Driver
.TextureMatrix
)
381 ctx
->Driver
.TextureMatrix( ctx
, i
, ctx
->TextureMatrixStack
[i
].Top
);
388 * Update texture object's _Function field. We need to do this
389 * whenever any of the texture object's shadow-related fields change
390 * or when we start/stop using a fragment program.
392 * This function could be expanded someday to update additional per-object
393 * fields that depend on assorted state changes.
396 update_texture_compare_function(GLcontext
*ctx
,
397 struct gl_texture_object
*tObj
)
399 /* XXX temporarily disable this test since it breaks the GLSL
400 * shadow2D(), etc. functions.
402 if (0 /*ctx->FragmentProgram._Current*/) {
403 /* Texel/coordinate comparison is ignored for programs.
404 * See GL_ARB_fragment_program/shader spec for details.
406 tObj
->_Function
= GL_NONE
;
408 else if (tObj
->CompareFlag
) {
410 if (tObj
->CompareOperator
== GL_TEXTURE_LEQUAL_R_SGIX
) {
411 tObj
->_Function
= GL_LEQUAL
;
414 ASSERT(tObj
->CompareOperator
== GL_TEXTURE_GEQUAL_R_SGIX
);
415 tObj
->_Function
= GL_GEQUAL
;
418 else if (tObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
) {
420 tObj
->_Function
= tObj
->CompareFunc
;
423 tObj
->_Function
= GL_NONE
; /* pass depth through as grayscale */
429 * Helper function for determining which texture object (1D, 2D, cube, etc)
430 * should actually be used.
433 texture_override(GLcontext
*ctx
,
434 struct gl_texture_unit
*texUnit
, GLbitfield enableBits
,
435 struct gl_texture_object
*texObj
, GLuint textureBit
)
437 if (!texUnit
->_ReallyEnabled
&& (enableBits
& textureBit
)) {
438 if (!texObj
->_Complete
) {
439 _mesa_test_texobj_completeness(ctx
, texObj
);
441 if (texObj
->_Complete
) {
442 texUnit
->_ReallyEnabled
= textureBit
;
443 texUnit
->_Current
= texObj
;
444 update_texture_compare_function(ctx
, texObj
);
451 * \note This routine refers to derived texture matrix values to
452 * compute the ENABLE_TEXMAT flags, but is only called on
453 * _NEW_TEXTURE. On changes to _NEW_TEXTURE_MATRIX, the ENABLE_TEXMAT
454 * flags are updated by _mesa_update_texture_matrices, above.
456 * \param ctx GL context.
459 update_texture_state( GLcontext
*ctx
)
462 struct gl_fragment_program
*fprog
= NULL
;
463 struct gl_vertex_program
*vprog
= NULL
;
465 if (ctx
->Shader
.CurrentProgram
&&
466 ctx
->Shader
.CurrentProgram
->LinkStatus
) {
467 fprog
= ctx
->Shader
.CurrentProgram
->FragmentProgram
;
468 vprog
= ctx
->Shader
.CurrentProgram
->VertexProgram
;
471 if (ctx
->FragmentProgram
._Enabled
) {
472 fprog
= ctx
->FragmentProgram
.Current
;
474 if (ctx
->VertexProgram
._Enabled
) {
475 /* XXX enable this if/when non-shader vertex programs get
477 vprog = ctx->VertexProgram.Current;
482 ctx
->NewState
|= _NEW_TEXTURE
; /* TODO: only set this if there are
486 ctx
->Texture
._EnabledUnits
= 0;
487 ctx
->Texture
._GenFlags
= 0;
488 ctx
->Texture
._TexMatEnabled
= 0;
489 ctx
->Texture
._TexGenEnabled
= 0;
492 * Update texture unit state.
494 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
495 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
496 GLbitfield enableBits
;
498 texUnit
->_Current
= NULL
;
499 texUnit
->_ReallyEnabled
= 0;
500 texUnit
->_GenFlags
= 0;
502 /* Get the bitmask of texture enables.
503 * enableBits will be a mask of the TEXTURE_*_BIT flags indicating
504 * which texture targets are enabled (fixed function) or referenced
505 * by a fragment shader/program. When multiple flags are set, we'll
506 * settle on the one with highest priority (see texture_override below).
508 if (fprog
|| vprog
) {
511 enableBits
|= fprog
->Base
.TexturesUsed
[unit
];
513 enableBits
|= vprog
->Base
.TexturesUsed
[unit
];
516 if (!texUnit
->Enabled
)
518 enableBits
= texUnit
->Enabled
;
521 /* Look for the highest-priority texture target that's enabled and
522 * complete. That's the one we'll use for texturing. If we're using
523 * a fragment program we're guaranteed that bitcount(enabledBits) <= 1.
525 texture_override(ctx
, texUnit
, enableBits
,
526 texUnit
->Current2DArray
, TEXTURE_2D_ARRAY_BIT
);
527 texture_override(ctx
, texUnit
, enableBits
,
528 texUnit
->Current1DArray
, TEXTURE_1D_ARRAY_BIT
);
529 texture_override(ctx
, texUnit
, enableBits
,
530 texUnit
->CurrentCubeMap
, TEXTURE_CUBE_BIT
);
531 texture_override(ctx
, texUnit
, enableBits
,
532 texUnit
->Current3D
, TEXTURE_3D_BIT
);
533 texture_override(ctx
, texUnit
, enableBits
,
534 texUnit
->CurrentRect
, TEXTURE_RECT_BIT
);
535 texture_override(ctx
, texUnit
, enableBits
,
536 texUnit
->Current2D
, TEXTURE_2D_BIT
);
537 texture_override(ctx
, texUnit
, enableBits
,
538 texUnit
->Current1D
, TEXTURE_1D_BIT
);
540 if (!texUnit
->_ReallyEnabled
) {
544 if (texUnit
->_ReallyEnabled
)
545 ctx
->Texture
._EnabledUnits
|= (1 << unit
);
547 if (texUnit
->EnvMode
== GL_COMBINE
) {
548 texUnit
->_CurrentCombine
= & texUnit
->Combine
;
551 const struct gl_texture_object
*texObj
= texUnit
->_Current
;
552 GLenum format
= texObj
->Image
[0][texObj
->BaseLevel
]->_BaseFormat
;
553 if (format
== GL_COLOR_INDEX
) {
554 format
= GL_RGBA
; /* a bit of a hack */
556 else if (format
== GL_DEPTH_COMPONENT
557 || format
== GL_DEPTH_STENCIL_EXT
) {
558 format
= texObj
->DepthMode
;
560 calculate_derived_texenv(&texUnit
->_EnvMode
, texUnit
->EnvMode
, format
);
561 texUnit
->_CurrentCombine
= & texUnit
->_EnvMode
;
564 switch (texUnit
->_CurrentCombine
->ModeRGB
) {
566 texUnit
->_CurrentCombine
->_NumArgsRGB
= 1;
574 case GL_DOT3_RGB_EXT
:
575 case GL_DOT3_RGBA_EXT
:
576 texUnit
->_CurrentCombine
->_NumArgsRGB
= 2;
579 case GL_MODULATE_ADD_ATI
:
580 case GL_MODULATE_SIGNED_ADD_ATI
:
581 case GL_MODULATE_SUBTRACT_ATI
:
582 texUnit
->_CurrentCombine
->_NumArgsRGB
= 3;
585 texUnit
->_CurrentCombine
->_NumArgsRGB
= 0;
586 _mesa_problem(ctx
, "invalid RGB combine mode in update_texture_state");
590 switch (texUnit
->_CurrentCombine
->ModeA
) {
592 texUnit
->_CurrentCombine
->_NumArgsA
= 1;
598 texUnit
->_CurrentCombine
->_NumArgsA
= 2;
601 case GL_MODULATE_ADD_ATI
:
602 case GL_MODULATE_SIGNED_ADD_ATI
:
603 case GL_MODULATE_SUBTRACT_ATI
:
604 texUnit
->_CurrentCombine
->_NumArgsA
= 3;
607 texUnit
->_CurrentCombine
->_NumArgsA
= 0;
608 _mesa_problem(ctx
, "invalid Alpha combine mode in update_texture_state");
613 /* Determine which texture coordinate sets are actually needed */
615 const GLuint coordMask
= (1 << MAX_TEXTURE_COORD_UNITS
) - 1;
616 ctx
->Texture
._EnabledCoordUnits
617 = (fprog
->Base
.InputsRead
>> FRAG_ATTRIB_TEX0
) & coordMask
;
620 ctx
->Texture
._EnabledCoordUnits
= ctx
->Texture
._EnabledUnits
;
623 /* Setup texgen for those texture coordinate sets that are in use */
624 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
625 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
627 if (!(ctx
->Texture
._EnabledCoordUnits
& (1 << unit
)))
630 if (texUnit
->TexGenEnabled
) {
631 if (texUnit
->TexGenEnabled
& S_BIT
) {
632 texUnit
->_GenFlags
|= texUnit
->_GenBitS
;
634 if (texUnit
->TexGenEnabled
& T_BIT
) {
635 texUnit
->_GenFlags
|= texUnit
->_GenBitT
;
637 if (texUnit
->TexGenEnabled
& Q_BIT
) {
638 texUnit
->_GenFlags
|= texUnit
->_GenBitQ
;
640 if (texUnit
->TexGenEnabled
& R_BIT
) {
641 texUnit
->_GenFlags
|= texUnit
->_GenBitR
;
644 ctx
->Texture
._TexGenEnabled
|= ENABLE_TEXGEN(unit
);
645 ctx
->Texture
._GenFlags
|= texUnit
->_GenFlags
;
648 if (ctx
->TextureMatrixStack
[unit
].Top
->type
!= MATRIX_IDENTITY
)
649 ctx
->Texture
._TexMatEnabled
|= ENABLE_TEXMAT(unit
);
655 * Update texture-related derived state.
658 _mesa_update_texture( GLcontext
*ctx
, GLuint new_state
)
660 if (new_state
& _NEW_TEXTURE_MATRIX
)
661 update_texture_matrices( ctx
);
663 if (new_state
& (_NEW_TEXTURE
| _NEW_PROGRAM
))
664 update_texture_state( ctx
);
668 /**********************************************************************/
669 /***** Initialization *****/
670 /**********************************************************************/
673 * Allocate the proxy textures for the given context.
675 * \param ctx the context to allocate proxies for.
677 * \return GL_TRUE on success, or GL_FALSE on failure
679 * If run out of memory part way through the allocations, clean up and return
683 alloc_proxy_textures( GLcontext
*ctx
)
685 static const GLenum targets
[] = {
689 GL_TEXTURE_CUBE_MAP_ARB
,
690 GL_TEXTURE_RECTANGLE_NV
,
691 GL_TEXTURE_1D_ARRAY_EXT
,
692 GL_TEXTURE_2D_ARRAY_EXT
696 ASSERT(Elements(targets
) == NUM_TEXTURE_TARGETS
);
698 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++) {
699 if (!(ctx
->Texture
.ProxyTex
[tgt
]
700 = ctx
->Driver
.NewTextureObject(ctx
, 0, targets
[tgt
]))) {
701 /* out of memory, free what we did allocate */
703 ctx
->Driver
.DeleteTexture(ctx
, ctx
->Texture
.ProxyTex
[tgt
]);
709 assert(ctx
->Texture
.ProxyTex
[0]->RefCount
== 1); /* sanity check */
715 * Initialize a texture unit.
717 * \param ctx GL context.
718 * \param unit texture unit number to be initialized.
721 init_texture_unit( GLcontext
*ctx
, GLuint unit
)
723 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
725 texUnit
->EnvMode
= GL_MODULATE
;
726 ASSIGN_4V( texUnit
->EnvColor
, 0.0, 0.0, 0.0, 0.0 );
728 texUnit
->Combine
= default_combine_state
;
729 texUnit
->_EnvMode
= default_combine_state
;
730 texUnit
->_CurrentCombine
= & texUnit
->_EnvMode
;
732 texUnit
->TexGenEnabled
= 0;
733 texUnit
->GenModeS
= GL_EYE_LINEAR
;
734 texUnit
->GenModeT
= GL_EYE_LINEAR
;
735 texUnit
->GenModeR
= GL_EYE_LINEAR
;
736 texUnit
->GenModeQ
= GL_EYE_LINEAR
;
737 texUnit
->_GenBitS
= TEXGEN_EYE_LINEAR
;
738 texUnit
->_GenBitT
= TEXGEN_EYE_LINEAR
;
739 texUnit
->_GenBitR
= TEXGEN_EYE_LINEAR
;
740 texUnit
->_GenBitQ
= TEXGEN_EYE_LINEAR
;
742 /* Yes, these plane coefficients are correct! */
743 ASSIGN_4V( texUnit
->ObjectPlaneS
, 1.0, 0.0, 0.0, 0.0 );
744 ASSIGN_4V( texUnit
->ObjectPlaneT
, 0.0, 1.0, 0.0, 0.0 );
745 ASSIGN_4V( texUnit
->ObjectPlaneR
, 0.0, 0.0, 0.0, 0.0 );
746 ASSIGN_4V( texUnit
->ObjectPlaneQ
, 0.0, 0.0, 0.0, 0.0 );
747 ASSIGN_4V( texUnit
->EyePlaneS
, 1.0, 0.0, 0.0, 0.0 );
748 ASSIGN_4V( texUnit
->EyePlaneT
, 0.0, 1.0, 0.0, 0.0 );
749 ASSIGN_4V( texUnit
->EyePlaneR
, 0.0, 0.0, 0.0, 0.0 );
750 ASSIGN_4V( texUnit
->EyePlaneQ
, 0.0, 0.0, 0.0, 0.0 );
752 /* initialize current texture object ptrs to the shared default objects */
753 _mesa_reference_texobj(&texUnit
->Current1D
, ctx
->Shared
->Default1D
);
754 _mesa_reference_texobj(&texUnit
->Current2D
, ctx
->Shared
->Default2D
);
755 _mesa_reference_texobj(&texUnit
->Current3D
, ctx
->Shared
->Default3D
);
756 _mesa_reference_texobj(&texUnit
->CurrentCubeMap
, ctx
->Shared
->DefaultCubeMap
);
757 _mesa_reference_texobj(&texUnit
->CurrentRect
, ctx
->Shared
->DefaultRect
);
758 _mesa_reference_texobj(&texUnit
->Current1DArray
, ctx
->Shared
->Default1DArray
);
759 _mesa_reference_texobj(&texUnit
->Current2DArray
, ctx
->Shared
->Default2DArray
);
764 * Initialize texture state for the given context.
767 _mesa_init_texture(GLcontext
*ctx
)
771 assert(MAX_TEXTURE_LEVELS
>= MAX_3D_TEXTURE_LEVELS
);
772 assert(MAX_TEXTURE_LEVELS
>= MAX_CUBE_TEXTURE_LEVELS
);
775 ctx
->Texture
.CurrentUnit
= 0; /* multitexture */
776 ctx
->Texture
._EnabledUnits
= 0;
777 ctx
->Texture
.SharedPalette
= GL_FALSE
;
778 #if FEATURE_colortable
779 _mesa_init_colortable(&ctx
->Texture
.Palette
);
782 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++)
783 init_texture_unit( ctx
, i
);
785 /* After we're done initializing the context's texture state the default
786 * texture objects' refcounts should be at least MAX_TEXTURE_UNITS + 1.
788 assert(ctx
->Shared
->Default1D
->RefCount
>= MAX_TEXTURE_UNITS
+ 1);
790 /* Allocate proxy textures */
791 if (!alloc_proxy_textures( ctx
))
799 * Free dynamically-allocted texture data attached to the given context.
802 _mesa_free_texture_data(GLcontext
*ctx
)
806 /* unreference current textures */
807 for (u
= 0; u
< MAX_TEXTURE_IMAGE_UNITS
; u
++) {
808 struct gl_texture_unit
*unit
= ctx
->Texture
.Unit
+ u
;
809 _mesa_reference_texobj(&unit
->Current1D
, NULL
);
810 _mesa_reference_texobj(&unit
->Current2D
, NULL
);
811 _mesa_reference_texobj(&unit
->Current3D
, NULL
);
812 _mesa_reference_texobj(&unit
->CurrentCubeMap
, NULL
);
813 _mesa_reference_texobj(&unit
->CurrentRect
, NULL
);
814 _mesa_reference_texobj(&unit
->Current1DArray
, NULL
);
815 _mesa_reference_texobj(&unit
->Current2DArray
, NULL
);
818 /* Free proxy texture objects */
819 for (tgt
= 0; tgt
< NUM_TEXTURE_TARGETS
; tgt
++)
820 ctx
->Driver
.DeleteTexture(ctx
, ctx
->Texture
.ProxyTex
[tgt
]);
823 #if FEATURE_colortable
826 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++)
827 _mesa_free_colortable_data( &ctx
->Texture
.Unit
[i
].ColorTable
);
834 * Update the default texture objects in the given context to reference those
835 * specified in the shared state and release those referencing the old
839 _mesa_update_default_objects_texture(GLcontext
*ctx
)
843 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
844 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
846 _mesa_reference_texobj(&texUnit
->Current1D
, ctx
->Shared
->Default1D
);
847 _mesa_reference_texobj(&texUnit
->Current2D
, ctx
->Shared
->Default2D
);
848 _mesa_reference_texobj(&texUnit
->Current3D
, ctx
->Shared
->Default3D
);
849 _mesa_reference_texobj(&texUnit
->CurrentCubeMap
, ctx
->Shared
->DefaultCubeMap
);
850 _mesa_reference_texobj(&texUnit
->CurrentRect
, ctx
->Shared
->DefaultRect
);
851 _mesa_reference_texobj(&texUnit
->Current1DArray
, ctx
->Shared
->Default1DArray
);
852 _mesa_reference_texobj(&texUnit
->Current2DArray
, ctx
->Shared
->Default2DArray
);