mesa: code refactoring: move texcombine code into update_tex_combine()
[mesa.git] / src / mesa / main / texstate.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 /**
26 * \file texstate.c
27 *
28 * Texture state handling.
29 */
30
31 #include "glheader.h"
32 #include "colormac.h"
33 #if FEATURE_colortable
34 #include "colortab.h"
35 #endif
36 #include "context.h"
37 #include "enums.h"
38 #include "macros.h"
39 #include "texcompress.h"
40 #include "texobj.h"
41 #include "teximage.h"
42 #include "texstate.h"
43 #include "texenvprogram.h"
44 #include "mtypes.h"
45 #include "math/m_xform.h"
46
47
48
49 /**
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.
53 */
54 static const struct gl_tex_env_combine_state default_combine_state = {
55 GL_MODULATE, GL_MODULATE,
56 { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT, GL_CONSTANT },
57 { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT, GL_CONSTANT },
58 { GL_SRC_COLOR, GL_SRC_COLOR, GL_SRC_ALPHA, GL_SRC_ALPHA },
59 { GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA },
60 0, 0,
61 2, 2
62 };
63
64
65
66 /**
67 * Used by glXCopyContext to copy texture state from one context to another.
68 */
69 void
70 _mesa_copy_texture_state( const GLcontext *src, GLcontext *dst )
71 {
72 GLuint i;
73
74 ASSERT(src);
75 ASSERT(dst);
76
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;
82
83 /* per-unit state */
84 for (i = 0; i < src->Const.MaxTextureImageUnits; 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;
107
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;
117
118 /* copy texture object bindings, not contents of texture objects */
119 _mesa_lock_context_textures(dst);
120
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);
135
136 _mesa_unlock_context_textures(dst);
137 }
138 }
139
140
141 /*
142 * For debugging
143 */
144 void
145 _mesa_print_texunit_state( GLcontext *ctx, GLuint unit )
146 {
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]);
167 }
168
169
170
171 /**********************************************************************/
172 /* Texture Environment */
173 /**********************************************************************/
174
175 /**
176 * Convert "classic" texture environment to ARB_texture_env_combine style
177 * environments.
178 *
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
183 * texture unit.
184 */
185 static void
186 calculate_derived_texenv( struct gl_tex_env_combine_state *state,
187 GLenum mode, GLenum texBaseFormat )
188 {
189 GLenum mode_rgb;
190 GLenum mode_a;
191
192 *state = default_combine_state;
193
194 switch (texBaseFormat) {
195 case GL_ALPHA:
196 state->SourceRGB[0] = GL_PREVIOUS;
197 break;
198
199 case GL_LUMINANCE_ALPHA:
200 case GL_INTENSITY:
201 case GL_RGBA:
202 break;
203
204 case GL_LUMINANCE:
205 case GL_RGB:
206 case GL_YCBCR_MESA:
207 state->SourceA[0] = GL_PREVIOUS;
208 break;
209
210 default:
211 _mesa_problem(NULL, "Invalid texBaseFormat in calculate_derived_texenv");
212 return;
213 }
214
215 if (mode == GL_REPLACE_EXT)
216 mode = GL_REPLACE;
217
218 switch (mode) {
219 case GL_REPLACE:
220 case GL_MODULATE:
221 mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : mode;
222 mode_a = mode;
223 break;
224
225 case GL_DECAL:
226 mode_rgb = GL_INTERPOLATE;
227 mode_a = GL_REPLACE;
228
229 state->SourceA[0] = GL_PREVIOUS;
230
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".
234 */
235 switch (texBaseFormat) {
236 case GL_ALPHA:
237 case GL_LUMINANCE:
238 case GL_LUMINANCE_ALPHA:
239 case GL_INTENSITY:
240 state->SourceRGB[0] = GL_PREVIOUS;
241 break;
242 case GL_RGB:
243 case GL_YCBCR_MESA:
244 mode_rgb = GL_REPLACE;
245 break;
246 case GL_RGBA:
247 state->SourceRGB[2] = GL_TEXTURE;
248 break;
249 }
250 break;
251
252 case GL_BLEND:
253 mode_rgb = GL_INTERPOLATE;
254 mode_a = GL_MODULATE;
255
256 switch (texBaseFormat) {
257 case GL_ALPHA:
258 mode_rgb = GL_REPLACE;
259 break;
260 case GL_INTENSITY:
261 mode_a = GL_INTERPOLATE;
262 state->SourceA[0] = GL_CONSTANT;
263 state->OperandA[2] = GL_SRC_ALPHA;
264 /* FALLTHROUGH */
265 case GL_LUMINANCE:
266 case GL_RGB:
267 case GL_LUMINANCE_ALPHA:
268 case GL_RGBA:
269 case GL_YCBCR_MESA:
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;
274 break;
275 }
276 break;
277
278 case GL_ADD:
279 mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : GL_ADD;
280 mode_a = (texBaseFormat == GL_INTENSITY) ? GL_ADD : GL_MODULATE;
281 break;
282
283 default:
284 _mesa_problem(NULL,
285 "Invalid texture env mode in calculate_derived_texenv");
286 return;
287 }
288
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;
293 }
294
295
296
297
298 /* GL_ARB_multitexture */
299 void GLAPIENTRY
300 _mesa_ActiveTextureARB(GLenum texture)
301 {
302 GET_CURRENT_CONTEXT(ctx);
303 const GLuint texUnit = texture - GL_TEXTURE0;
304 ASSERT_OUTSIDE_BEGIN_END(ctx);
305
306 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
307 _mesa_debug(ctx, "glActiveTexture %s\n",
308 _mesa_lookup_enum_by_nr(texture));
309
310 if (texUnit >= ctx->Const.MaxTextureImageUnits) {
311 _mesa_error(ctx, GL_INVALID_ENUM, "glActiveTexture(texture)");
312 return;
313 }
314
315 if (ctx->Texture.CurrentUnit == texUnit)
316 return;
317
318 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
319
320 ctx->Texture.CurrentUnit = texUnit;
321 if (ctx->Transform.MatrixMode == GL_TEXTURE) {
322 /* update current stack pointer */
323 ctx->CurrentStack = &ctx->TextureMatrixStack[texUnit];
324 }
325
326 if (ctx->Driver.ActiveTexture) {
327 (*ctx->Driver.ActiveTexture)( ctx, (GLuint) texUnit );
328 }
329 }
330
331
332 /* GL_ARB_multitexture */
333 void GLAPIENTRY
334 _mesa_ClientActiveTextureARB(GLenum texture)
335 {
336 GET_CURRENT_CONTEXT(ctx);
337 GLuint texUnit = texture - GL_TEXTURE0;
338 ASSERT_OUTSIDE_BEGIN_END(ctx);
339
340 if (texUnit >= ctx->Const.MaxTextureCoordUnits) {
341 _mesa_error(ctx, GL_INVALID_ENUM, "glClientActiveTexture(texture)");
342 return;
343 }
344
345 FLUSH_VERTICES(ctx, _NEW_ARRAY);
346 ctx->Array.ActiveTexture = texUnit;
347 }
348
349
350
351 /**********************************************************************/
352 /***** State management *****/
353 /**********************************************************************/
354
355
356 /**
357 * \note This routine refers to derived texture attribute values to
358 * compute the ENABLE_TEXMAT flags, but is only called on
359 * _NEW_TEXTURE_MATRIX. On changes to _NEW_TEXTURE, the ENABLE_TEXMAT
360 * flags are updated by _mesa_update_textures(), below.
361 *
362 * \param ctx GL context.
363 */
364 static void
365 update_texture_matrices( GLcontext *ctx )
366 {
367 GLuint i;
368
369 ctx->Texture._TexMatEnabled = 0;
370
371 for (i=0; i < ctx->Const.MaxTextureCoordUnits; i++) {
372 if (_math_matrix_is_dirty(ctx->TextureMatrixStack[i].Top)) {
373 _math_matrix_analyse( ctx->TextureMatrixStack[i].Top );
374
375 if (ctx->Texture.Unit[i]._ReallyEnabled &&
376 ctx->TextureMatrixStack[i].Top->type != MATRIX_IDENTITY)
377 ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(i);
378
379 if (ctx->Driver.TextureMatrix)
380 ctx->Driver.TextureMatrix( ctx, i, ctx->TextureMatrixStack[i].Top);
381 }
382 }
383 }
384
385
386 /**
387 * Update texture object's _Function field. We need to do this
388 * whenever any of the texture object's shadow-related fields change
389 * or when we start/stop using a fragment program.
390 *
391 * This function could be expanded someday to update additional per-object
392 * fields that depend on assorted state changes.
393 */
394 static void
395 update_texture_compare_function(GLcontext *ctx,
396 struct gl_texture_object *tObj)
397 {
398 /* XXX temporarily disable this test since it breaks the GLSL
399 * shadow2D(), etc. functions.
400 */
401 if (0 /*ctx->FragmentProgram._Current*/) {
402 /* Texel/coordinate comparison is ignored for programs.
403 * See GL_ARB_fragment_program/shader spec for details.
404 */
405 tObj->_Function = GL_NONE;
406 }
407 else if (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
408 /* GL_ARB_shadow */
409 tObj->_Function = tObj->CompareFunc;
410 }
411 else {
412 tObj->_Function = GL_NONE; /* pass depth through as grayscale */
413 }
414 }
415
416
417 /**
418 * Helper function for determining which texture object (1D, 2D, cube, etc)
419 * should actually be used.
420 */
421 static void
422 texture_override(GLcontext *ctx,
423 struct gl_texture_unit *texUnit, GLbitfield enableBits,
424 struct gl_texture_object *texObj, GLuint textureBit)
425 {
426 if (!texUnit->_ReallyEnabled && (enableBits & textureBit)) {
427 if (!texObj->_Complete) {
428 _mesa_test_texobj_completeness(ctx, texObj);
429 }
430 if (texObj->_Complete) {
431 texUnit->_ReallyEnabled = textureBit;
432 texUnit->_Current = texObj;
433 update_texture_compare_function(ctx, texObj);
434 }
435 }
436 }
437
438
439 /**
440 * Examine texture unit's combine/env state to update derived state.
441 */
442 static void
443 update_tex_combine(GLcontext *ctx, struct gl_texture_unit *texUnit)
444 {
445 /* Set the texUnit->_CurrentCombine field to point to the user's combiner
446 * state, or the combiner state which is derived from traditional texenv
447 * mode.
448 */
449 if (texUnit->EnvMode == GL_COMBINE ||
450 texUnit->EnvMode == GL_COMBINE4_NV) {
451 texUnit->_CurrentCombine = & texUnit->Combine;
452 }
453 else {
454 const struct gl_texture_object *texObj = texUnit->_Current;
455 GLenum format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
456 if (format == GL_COLOR_INDEX) {
457 format = GL_RGBA; /* a bit of a hack */
458 }
459 else if (format == GL_DEPTH_COMPONENT ||
460 format == GL_DEPTH_STENCIL_EXT) {
461 format = texObj->DepthMode;
462 }
463 calculate_derived_texenv(&texUnit->_EnvMode, texUnit->EnvMode, format);
464 texUnit->_CurrentCombine = & texUnit->_EnvMode;
465 }
466
467 /* Determine number of source RGB terms in the combiner function */
468 switch (texUnit->_CurrentCombine->ModeRGB) {
469 case GL_REPLACE:
470 texUnit->_CurrentCombine->_NumArgsRGB = 1;
471 break;
472 case GL_ADD:
473 case GL_ADD_SIGNED:
474 if (texUnit->EnvMode == GL_COMBINE4_NV)
475 texUnit->_CurrentCombine->_NumArgsRGB = 4;
476 else
477 texUnit->_CurrentCombine->_NumArgsRGB = 2;
478 break;
479 case GL_MODULATE:
480 case GL_SUBTRACT:
481 case GL_DOT3_RGB:
482 case GL_DOT3_RGBA:
483 case GL_DOT3_RGB_EXT:
484 case GL_DOT3_RGBA_EXT:
485 texUnit->_CurrentCombine->_NumArgsRGB = 2;
486 break;
487 case GL_INTERPOLATE:
488 case GL_MODULATE_ADD_ATI:
489 case GL_MODULATE_SIGNED_ADD_ATI:
490 case GL_MODULATE_SUBTRACT_ATI:
491 texUnit->_CurrentCombine->_NumArgsRGB = 3;
492 break;
493 default:
494 texUnit->_CurrentCombine->_NumArgsRGB = 0;
495 _mesa_problem(ctx, "invalid RGB combine mode in update_texture_state");
496 return;
497 }
498
499 /* Determine number of source Alpha terms in the combiner function */
500 switch (texUnit->_CurrentCombine->ModeA) {
501 case GL_REPLACE:
502 texUnit->_CurrentCombine->_NumArgsA = 1;
503 break;
504 case GL_ADD:
505 case GL_ADD_SIGNED:
506 if (texUnit->EnvMode == GL_COMBINE4_NV)
507 texUnit->_CurrentCombine->_NumArgsA = 4;
508 else
509 texUnit->_CurrentCombine->_NumArgsA = 2;
510 break;
511 case GL_MODULATE:
512 case GL_SUBTRACT:
513 texUnit->_CurrentCombine->_NumArgsA = 2;
514 break;
515 case GL_INTERPOLATE:
516 case GL_MODULATE_ADD_ATI:
517 case GL_MODULATE_SIGNED_ADD_ATI:
518 case GL_MODULATE_SUBTRACT_ATI:
519 texUnit->_CurrentCombine->_NumArgsA = 3;
520 break;
521 default:
522 texUnit->_CurrentCombine->_NumArgsA = 0;
523 _mesa_problem(ctx, "invalid Alpha combine mode in update_texture_state");
524 break;
525 }
526 }
527
528
529 /**
530 * \note This routine refers to derived texture matrix values to
531 * compute the ENABLE_TEXMAT flags, but is only called on
532 * _NEW_TEXTURE. On changes to _NEW_TEXTURE_MATRIX, the ENABLE_TEXMAT
533 * flags are updated by _mesa_update_texture_matrices, above.
534 *
535 * \param ctx GL context.
536 */
537 static void
538 update_texture_state( GLcontext *ctx )
539 {
540 GLuint unit;
541 struct gl_fragment_program *fprog = NULL;
542 struct gl_vertex_program *vprog = NULL;
543
544 if (ctx->Shader.CurrentProgram &&
545 ctx->Shader.CurrentProgram->LinkStatus) {
546 fprog = ctx->Shader.CurrentProgram->FragmentProgram;
547 vprog = ctx->Shader.CurrentProgram->VertexProgram;
548 }
549 else {
550 if (ctx->FragmentProgram._Enabled) {
551 fprog = ctx->FragmentProgram.Current;
552 }
553 if (ctx->VertexProgram._Enabled) {
554 /* XXX enable this if/when non-shader vertex programs get
555 * texture fetches:
556 vprog = ctx->VertexProgram.Current;
557 */
558 }
559 }
560
561 /* TODO: only set this if there are actual changes */
562 ctx->NewState |= _NEW_TEXTURE;
563
564 ctx->Texture._EnabledUnits = 0;
565 ctx->Texture._GenFlags = 0;
566 ctx->Texture._TexMatEnabled = 0;
567 ctx->Texture._TexGenEnabled = 0;
568
569 /*
570 * Update texture unit state.
571 */
572 for (unit = 0; unit < ctx->Const.MaxTextureImageUnits; unit++) {
573 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
574 GLbitfield enableBits;
575
576 texUnit->_Current = NULL;
577 texUnit->_ReallyEnabled = 0;
578 texUnit->_GenFlags = 0;
579
580 /* Get the bitmask of texture target enables.
581 * enableBits will be a mask of the TEXTURE_*_BIT flags indicating
582 * which texture targets are enabled (fixed function) or referenced
583 * by a fragment shader/program. When multiple flags are set, we'll
584 * settle on the one with highest priority (see texture_override below).
585 */
586 enableBits = 0x0;
587 if (vprog) {
588 enableBits |= vprog->Base.TexturesUsed[unit];
589 }
590 if (fprog) {
591 enableBits |= fprog->Base.TexturesUsed[unit];
592 }
593 else {
594 /* fixed-function fragment program */
595 enableBits |= texUnit->Enabled;
596 }
597
598 if (enableBits == 0x0)
599 continue;
600
601 ASSERT(texUnit->Current1D);
602 ASSERT(texUnit->Current2D);
603 ASSERT(texUnit->Current3D);
604 ASSERT(texUnit->CurrentCubeMap);
605 ASSERT(texUnit->CurrentRect);
606 ASSERT(texUnit->Current1DArray);
607 ASSERT(texUnit->Current2DArray);
608
609 /* Look for the highest-priority texture target that's enabled and
610 * complete. That's the one we'll use for texturing. If we're using
611 * a fragment program we're guaranteed that bitcount(enabledBits) <= 1.
612 */
613 texture_override(ctx, texUnit, enableBits,
614 texUnit->Current2DArray, TEXTURE_2D_ARRAY_BIT);
615 texture_override(ctx, texUnit, enableBits,
616 texUnit->Current1DArray, TEXTURE_1D_ARRAY_BIT);
617 texture_override(ctx, texUnit, enableBits,
618 texUnit->CurrentCubeMap, TEXTURE_CUBE_BIT);
619 texture_override(ctx, texUnit, enableBits,
620 texUnit->Current3D, TEXTURE_3D_BIT);
621 texture_override(ctx, texUnit, enableBits,
622 texUnit->CurrentRect, TEXTURE_RECT_BIT);
623 texture_override(ctx, texUnit, enableBits,
624 texUnit->Current2D, TEXTURE_2D_BIT);
625 texture_override(ctx, texUnit, enableBits,
626 texUnit->Current1D, TEXTURE_1D_BIT);
627
628 if (!texUnit->_ReallyEnabled) {
629 continue;
630 }
631
632 /* if we get here, we know this texture unit is enabled */
633
634 ctx->Texture._EnabledUnits |= (1 << unit);
635
636 update_tex_combine(ctx, texUnit);
637 }
638
639
640 /* Determine which texture coordinate sets are actually needed */
641 if (fprog) {
642 const GLuint coordMask = (1 << MAX_TEXTURE_COORD_UNITS) - 1;
643 ctx->Texture._EnabledCoordUnits
644 = (fprog->Base.InputsRead >> FRAG_ATTRIB_TEX0) & coordMask;
645 }
646 else {
647 ctx->Texture._EnabledCoordUnits = ctx->Texture._EnabledUnits;
648 }
649
650 /* Setup texgen for those texture coordinate sets that are in use */
651 for (unit = 0; unit < ctx->Const.MaxTextureCoordUnits; unit++) {
652 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
653
654 if (!(ctx->Texture._EnabledCoordUnits & (1 << unit)))
655 continue;
656
657 if (texUnit->TexGenEnabled) {
658 if (texUnit->TexGenEnabled & S_BIT) {
659 texUnit->_GenFlags |= texUnit->_GenBitS;
660 }
661 if (texUnit->TexGenEnabled & T_BIT) {
662 texUnit->_GenFlags |= texUnit->_GenBitT;
663 }
664 if (texUnit->TexGenEnabled & Q_BIT) {
665 texUnit->_GenFlags |= texUnit->_GenBitQ;
666 }
667 if (texUnit->TexGenEnabled & R_BIT) {
668 texUnit->_GenFlags |= texUnit->_GenBitR;
669 }
670
671 ctx->Texture._TexGenEnabled |= ENABLE_TEXGEN(unit);
672 ctx->Texture._GenFlags |= texUnit->_GenFlags;
673 }
674
675 if (ctx->TextureMatrixStack[unit].Top->type != MATRIX_IDENTITY)
676 ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(unit);
677 }
678 }
679
680
681 /**
682 * Update texture-related derived state.
683 */
684 void
685 _mesa_update_texture( GLcontext *ctx, GLuint new_state )
686 {
687 if (new_state & _NEW_TEXTURE_MATRIX)
688 update_texture_matrices( ctx );
689
690 if (new_state & (_NEW_TEXTURE | _NEW_PROGRAM))
691 update_texture_state( ctx );
692 }
693
694
695 /**********************************************************************/
696 /***** Initialization *****/
697 /**********************************************************************/
698
699 /**
700 * Allocate the proxy textures for the given context.
701 *
702 * \param ctx the context to allocate proxies for.
703 *
704 * \return GL_TRUE on success, or GL_FALSE on failure
705 *
706 * If run out of memory part way through the allocations, clean up and return
707 * GL_FALSE.
708 */
709 static GLboolean
710 alloc_proxy_textures( GLcontext *ctx )
711 {
712 static const GLenum targets[] = {
713 GL_TEXTURE_1D,
714 GL_TEXTURE_2D,
715 GL_TEXTURE_3D,
716 GL_TEXTURE_CUBE_MAP_ARB,
717 GL_TEXTURE_RECTANGLE_NV,
718 GL_TEXTURE_1D_ARRAY_EXT,
719 GL_TEXTURE_2D_ARRAY_EXT
720 };
721 GLint tgt;
722
723 ASSERT(Elements(targets) == NUM_TEXTURE_TARGETS);
724
725 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
726 if (!(ctx->Texture.ProxyTex[tgt]
727 = ctx->Driver.NewTextureObject(ctx, 0, targets[tgt]))) {
728 /* out of memory, free what we did allocate */
729 while (--tgt >= 0) {
730 ctx->Driver.DeleteTexture(ctx, ctx->Texture.ProxyTex[tgt]);
731 }
732 return GL_FALSE;
733 }
734 }
735
736 assert(ctx->Texture.ProxyTex[0]->RefCount == 1); /* sanity check */
737 return GL_TRUE;
738 }
739
740
741 /**
742 * Initialize a texture unit.
743 *
744 * \param ctx GL context.
745 * \param unit texture unit number to be initialized.
746 */
747 static void
748 init_texture_unit( GLcontext *ctx, GLuint unit )
749 {
750 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
751
752 texUnit->EnvMode = GL_MODULATE;
753 ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
754
755 texUnit->Combine = default_combine_state;
756 texUnit->_EnvMode = default_combine_state;
757 texUnit->_CurrentCombine = & texUnit->_EnvMode;
758
759 texUnit->TexGenEnabled = 0;
760 texUnit->GenModeS = GL_EYE_LINEAR;
761 texUnit->GenModeT = GL_EYE_LINEAR;
762 texUnit->GenModeR = GL_EYE_LINEAR;
763 texUnit->GenModeQ = GL_EYE_LINEAR;
764 texUnit->_GenBitS = TEXGEN_EYE_LINEAR;
765 texUnit->_GenBitT = TEXGEN_EYE_LINEAR;
766 texUnit->_GenBitR = TEXGEN_EYE_LINEAR;
767 texUnit->_GenBitQ = TEXGEN_EYE_LINEAR;
768
769 /* Yes, these plane coefficients are correct! */
770 ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
771 ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
772 ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
773 ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
774 ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
775 ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
776 ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
777 ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
778
779 /* initialize current texture object ptrs to the shared default objects */
780 _mesa_reference_texobj(&texUnit->Current1D, ctx->Shared->Default1D);
781 _mesa_reference_texobj(&texUnit->Current2D, ctx->Shared->Default2D);
782 _mesa_reference_texobj(&texUnit->Current3D, ctx->Shared->Default3D);
783 _mesa_reference_texobj(&texUnit->CurrentCubeMap, ctx->Shared->DefaultCubeMap);
784 _mesa_reference_texobj(&texUnit->CurrentRect, ctx->Shared->DefaultRect);
785 _mesa_reference_texobj(&texUnit->Current1DArray, ctx->Shared->Default1DArray);
786 _mesa_reference_texobj(&texUnit->Current2DArray, ctx->Shared->Default2DArray);
787 }
788
789
790 /**
791 * Initialize texture state for the given context.
792 */
793 GLboolean
794 _mesa_init_texture(GLcontext *ctx)
795 {
796 GLuint i;
797
798 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
799 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
800
801 /* Texture group */
802 ctx->Texture.CurrentUnit = 0; /* multitexture */
803 ctx->Texture._EnabledUnits = 0;
804 ctx->Texture.SharedPalette = GL_FALSE;
805 #if FEATURE_colortable
806 _mesa_init_colortable(&ctx->Texture.Palette);
807 #endif
808
809 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
810 init_texture_unit( ctx, i );
811
812 /* After we're done initializing the context's texture state the default
813 * texture objects' refcounts should be at least MAX_TEXTURE_UNITS + 1.
814 */
815 assert(ctx->Shared->Default1D->RefCount >= MAX_TEXTURE_UNITS + 1);
816
817 /* Allocate proxy textures */
818 if (!alloc_proxy_textures( ctx ))
819 return GL_FALSE;
820
821 return GL_TRUE;
822 }
823
824
825 /**
826 * Free dynamically-allocted texture data attached to the given context.
827 */
828 void
829 _mesa_free_texture_data(GLcontext *ctx)
830 {
831 GLuint u, tgt;
832
833 /* unreference current textures */
834 for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
835 struct gl_texture_unit *unit = ctx->Texture.Unit + u;
836 _mesa_reference_texobj(&unit->Current1D, NULL);
837 _mesa_reference_texobj(&unit->Current2D, NULL);
838 _mesa_reference_texobj(&unit->Current3D, NULL);
839 _mesa_reference_texobj(&unit->CurrentCubeMap, NULL);
840 _mesa_reference_texobj(&unit->CurrentRect, NULL);
841 _mesa_reference_texobj(&unit->Current1DArray, NULL);
842 _mesa_reference_texobj(&unit->Current2DArray, NULL);
843 }
844
845 /* Free proxy texture objects */
846 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++)
847 ctx->Driver.DeleteTexture(ctx, ctx->Texture.ProxyTex[tgt]);
848
849 #if FEATURE_colortable
850 {
851 GLuint i;
852 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
853 _mesa_free_colortable_data( &ctx->Texture.Unit[i].ColorTable );
854 }
855 #endif
856 }
857
858
859 /**
860 * Update the default texture objects in the given context to reference those
861 * specified in the shared state and release those referencing the old
862 * shared state.
863 */
864 void
865 _mesa_update_default_objects_texture(GLcontext *ctx)
866 {
867 GLuint i;
868
869 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
870 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
871
872 _mesa_reference_texobj(&texUnit->Current1D, ctx->Shared->Default1D);
873 _mesa_reference_texobj(&texUnit->Current2D, ctx->Shared->Default2D);
874 _mesa_reference_texobj(&texUnit->Current3D, ctx->Shared->Default3D);
875 _mesa_reference_texobj(&texUnit->CurrentCubeMap, ctx->Shared->DefaultCubeMap);
876 _mesa_reference_texobj(&texUnit->CurrentRect, ctx->Shared->DefaultRect);
877 _mesa_reference_texobj(&texUnit->Current1DArray, ctx->Shared->Default1DArray);
878 _mesa_reference_texobj(&texUnit->Current2DArray, ctx->Shared->Default2DArray);
879 }
880 }