a926ff135b4196e76246595629b1aa1092588d23
[mesa.git] / src / mesa / main / texstate.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2003 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 #include "colortab.h"
34 #include "context.h"
35 #include "enums.h"
36 #include "extensions.h"
37 #include "macros.h"
38 #include "nvfragprog.h"
39 #include "texobj.h"
40 #include "teximage.h"
41 #include "texstate.h"
42 #include "mtypes.h"
43 #include "math/m_xform.h"
44 #include "math/m_matrix.h"
45
46
47
48 #ifdef SPECIALCAST
49 /* Needed for an Amiga compiler */
50 #define ENUM_TO_FLOAT(X) ((GLfloat)(GLint)(X))
51 #define ENUM_TO_DOUBLE(X) ((GLdouble)(GLint)(X))
52 #else
53 /* all other compilers */
54 #define ENUM_TO_FLOAT(X) ((GLfloat)(X))
55 #define ENUM_TO_DOUBLE(X) ((GLdouble)(X))
56 #endif
57
58 /**
59 * Default texture combine environment state. This is used to initialize
60 * a context's texture units and as the basis for converting "classic"
61 * texture environmnets to ARB_texture_env_combine style values.
62 */
63 static const struct gl_tex_env_combine_state default_combine_state = {
64 GL_MODULATE, GL_MODULATE,
65 { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT },
66 { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT },
67 { GL_SRC_COLOR, GL_SRC_COLOR, GL_SRC_ALPHA },
68 { GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA },
69 0, 0,
70 2, 2
71 };
72
73
74 void
75 _mesa_copy_texture_state( const GLcontext *src, GLcontext *dst )
76 {
77 GLuint i;
78
79 ASSERT(src);
80 ASSERT(dst);
81
82 dst->Texture.CurrentUnit = src->Texture.CurrentUnit;
83 dst->Texture._GenFlags = src->Texture._GenFlags;
84 dst->Texture._TexGenEnabled = src->Texture._TexGenEnabled;
85 dst->Texture._TexMatEnabled = src->Texture._TexMatEnabled;
86 dst->Texture.SharedPalette = src->Texture.SharedPalette;
87
88 /* per-unit state */
89 for (i = 0; i < src->Const.MaxTextureUnits; i++) {
90 dst->Texture.Unit[i].Enabled = src->Texture.Unit[i].Enabled;
91 dst->Texture.Unit[i].EnvMode = src->Texture.Unit[i].EnvMode;
92 COPY_4V(dst->Texture.Unit[i].EnvColor, src->Texture.Unit[i].EnvColor);
93 dst->Texture.Unit[i].TexGenEnabled = src->Texture.Unit[i].TexGenEnabled;
94 dst->Texture.Unit[i].GenModeS = src->Texture.Unit[i].GenModeS;
95 dst->Texture.Unit[i].GenModeT = src->Texture.Unit[i].GenModeT;
96 dst->Texture.Unit[i].GenModeR = src->Texture.Unit[i].GenModeR;
97 dst->Texture.Unit[i].GenModeQ = src->Texture.Unit[i].GenModeQ;
98 dst->Texture.Unit[i]._GenBitS = src->Texture.Unit[i]._GenBitS;
99 dst->Texture.Unit[i]._GenBitT = src->Texture.Unit[i]._GenBitT;
100 dst->Texture.Unit[i]._GenBitR = src->Texture.Unit[i]._GenBitR;
101 dst->Texture.Unit[i]._GenBitQ = src->Texture.Unit[i]._GenBitQ;
102 dst->Texture.Unit[i]._GenFlags = src->Texture.Unit[i]._GenFlags;
103 COPY_4V(dst->Texture.Unit[i].ObjectPlaneS, src->Texture.Unit[i].ObjectPlaneS);
104 COPY_4V(dst->Texture.Unit[i].ObjectPlaneT, src->Texture.Unit[i].ObjectPlaneT);
105 COPY_4V(dst->Texture.Unit[i].ObjectPlaneR, src->Texture.Unit[i].ObjectPlaneR);
106 COPY_4V(dst->Texture.Unit[i].ObjectPlaneQ, src->Texture.Unit[i].ObjectPlaneQ);
107 COPY_4V(dst->Texture.Unit[i].EyePlaneS, src->Texture.Unit[i].EyePlaneS);
108 COPY_4V(dst->Texture.Unit[i].EyePlaneT, src->Texture.Unit[i].EyePlaneT);
109 COPY_4V(dst->Texture.Unit[i].EyePlaneR, src->Texture.Unit[i].EyePlaneR);
110 COPY_4V(dst->Texture.Unit[i].EyePlaneQ, src->Texture.Unit[i].EyePlaneQ);
111 dst->Texture.Unit[i].LodBias = src->Texture.Unit[i].LodBias;
112
113 /* GL_EXT_texture_env_combine */
114 dst->Texture.Unit[i].Combine.ModeRGB = src->Texture.Unit[i].Combine.ModeRGB;
115 dst->Texture.Unit[i].Combine.ModeA = src->Texture.Unit[i].Combine.ModeA;
116 COPY_3V(dst->Texture.Unit[i].Combine.SourceRGB, src->Texture.Unit[i].Combine.SourceRGB);
117 COPY_3V(dst->Texture.Unit[i].Combine.SourceA, src->Texture.Unit[i].Combine.SourceA);
118 COPY_3V(dst->Texture.Unit[i].Combine.OperandRGB, src->Texture.Unit[i].Combine.OperandRGB);
119 COPY_3V(dst->Texture.Unit[i].Combine.OperandA, src->Texture.Unit[i].Combine.OperandA);
120 dst->Texture.Unit[i].Combine.ScaleShiftRGB = src->Texture.Unit[i].Combine.ScaleShiftRGB;
121 dst->Texture.Unit[i].Combine.ScaleShiftA = src->Texture.Unit[i].Combine.ScaleShiftA;
122
123 /* texture object state */
124 _mesa_copy_texture_object(dst->Texture.Unit[i].Current1D,
125 src->Texture.Unit[i].Current1D);
126 _mesa_copy_texture_object(dst->Texture.Unit[i].Current2D,
127 src->Texture.Unit[i].Current2D);
128 _mesa_copy_texture_object(dst->Texture.Unit[i].Current3D,
129 src->Texture.Unit[i].Current3D);
130 _mesa_copy_texture_object(dst->Texture.Unit[i].CurrentCubeMap,
131 src->Texture.Unit[i].CurrentCubeMap);
132 _mesa_copy_texture_object(dst->Texture.Unit[i].CurrentRect,
133 src->Texture.Unit[i].CurrentRect);
134 }
135 }
136
137
138 /*
139 * For debugging
140 */
141 void
142 _mesa_print_texunit_state( GLcontext *ctx, GLuint unit )
143 {
144 const struct gl_texture_unit *texUnit = ctx->Texture.Unit + unit;
145 _mesa_printf("Texture Unit %d\n", unit);
146 _mesa_printf(" GL_TEXTURE_ENV_MODE = %s\n", _mesa_lookup_enum_by_nr(texUnit->EnvMode));
147 _mesa_printf(" GL_COMBINE_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.ModeRGB));
148 _mesa_printf(" GL_COMBINE_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.ModeA));
149 _mesa_printf(" GL_SOURCE0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[0]));
150 _mesa_printf(" GL_SOURCE1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[1]));
151 _mesa_printf(" GL_SOURCE2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[2]));
152 _mesa_printf(" GL_SOURCE0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[0]));
153 _mesa_printf(" GL_SOURCE1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[1]));
154 _mesa_printf(" GL_SOURCE2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[2]));
155 _mesa_printf(" GL_OPERAND0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[0]));
156 _mesa_printf(" GL_OPERAND1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[1]));
157 _mesa_printf(" GL_OPERAND2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[2]));
158 _mesa_printf(" GL_OPERAND0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[0]));
159 _mesa_printf(" GL_OPERAND1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[1]));
160 _mesa_printf(" GL_OPERAND2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[2]));
161 _mesa_printf(" GL_RGB_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftRGB);
162 _mesa_printf(" GL_ALPHA_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftA);
163 _mesa_printf(" GL_TEXTURE_ENV_COLOR = (%f, %f, %f, %f)\n", texUnit->EnvColor[0], texUnit->EnvColor[1], texUnit->EnvColor[2], texUnit->EnvColor[3]);
164 }
165
166
167
168 /**********************************************************************/
169 /* Texture Environment */
170 /**********************************************************************/
171
172 /**
173 * Convert "classic" texture environment to ARB_texture_env_combine style
174 * environments.
175 *
176 * \param state texture_env_combine state vector to be filled-in.
177 * \param mode Classic texture environment mode (i.e., \c GL_REPLACE,
178 * \c GL_BLEND, \c GL_DECAL, etc.).
179 * \param texBaseFormat Base format of the texture associated with the
180 * texture unit.
181 */
182 static void
183 calculate_derived_texenv( struct gl_tex_env_combine_state *state,
184 GLenum mode, GLenum texBaseFormat )
185 {
186 GLenum mode_rgb;
187 GLenum mode_a;
188
189 *state = default_combine_state;
190
191 switch (texBaseFormat) {
192 case GL_ALPHA:
193 state->SourceRGB[0] = GL_PREVIOUS;
194 break;
195
196 case GL_LUMINANCE_ALPHA:
197 case GL_INTENSITY:
198 case GL_RGBA:
199 break;
200
201 case GL_LUMINANCE:
202 case GL_RGB:
203 case GL_YCBCR_MESA:
204 state->SourceA[0] = GL_PREVIOUS;
205 break;
206
207 default:
208 _mesa_problem(NULL, "Invalid texBaseFormat in calculate_derived_texenv");
209 return;
210 }
211
212 switch (mode) {
213 case GL_REPLACE:
214 case GL_MODULATE:
215 mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : mode;
216 mode_a = mode;
217 break;
218
219 case GL_DECAL:
220 mode_rgb = GL_INTERPOLATE;
221 mode_a = GL_REPLACE;
222
223 state->SourceA[0] = GL_PREVIOUS;
224
225 /* Having alpha / luminance / intensity textures replace using the
226 * incoming fragment color matches the definition in NV_texture_shader.
227 * The 1.5 spec simply marks these as "undefined".
228 */
229 switch (texBaseFormat) {
230 case GL_ALPHA:
231 case GL_LUMINANCE:
232 case GL_LUMINANCE_ALPHA:
233 case GL_INTENSITY:
234 state->SourceRGB[0] = GL_PREVIOUS;
235 break;
236 case GL_RGB:
237 case GL_YCBCR_MESA:
238 mode_rgb = GL_REPLACE;
239 break;
240 case GL_RGBA:
241 state->SourceRGB[2] = GL_TEXTURE;
242 break;
243 }
244 break;
245
246 case GL_BLEND:
247 mode_rgb = GL_INTERPOLATE;
248 mode_a = GL_MODULATE;
249
250 switch (texBaseFormat) {
251 case GL_ALPHA:
252 mode_rgb = GL_REPLACE;
253 break;
254 case GL_INTENSITY:
255 mode_a = GL_INTERPOLATE;
256 /* FALLTHROUGH */
257 case GL_LUMINANCE:
258 case GL_RGB:
259 case GL_LUMINANCE_ALPHA:
260 case GL_RGBA:
261 case GL_YCBCR_MESA:
262 state->SourceRGB[2] = GL_TEXTURE;
263 state->SourceA[2] = GL_TEXTURE;
264 state->SourceRGB[0] = GL_CONSTANT;
265 state->OperandRGB[2] = GL_SRC_COLOR;
266 break;
267 }
268 break;
269
270 case GL_ADD:
271 mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : GL_ADD;
272 mode_a = (texBaseFormat == GL_INTENSITY) ? GL_ADD : GL_MODULATE;
273 break;
274
275 default:
276 _mesa_problem(NULL,
277 "Invalid texture env mode in calculate_derived_texenv");
278 return;
279 }
280
281 state->ModeRGB = (state->SourceRGB[0] != GL_PREVIOUS)
282 ? mode_rgb : GL_REPLACE;
283 state->ModeA = (state->SourceA[0] != GL_PREVIOUS)
284 ? mode_a : GL_REPLACE;
285 }
286
287
288 void GLAPIENTRY
289 _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
290 {
291 GET_CURRENT_CONTEXT(ctx);
292 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
293 ASSERT_OUTSIDE_BEGIN_END(ctx);
294
295 #define TE_ERROR(errCode, msg, value) \
296 _mesa_error(ctx, errCode, msg, _mesa_lookup_enum_by_nr(value));
297
298 if (target == GL_TEXTURE_ENV) {
299 switch (pname) {
300 case GL_TEXTURE_ENV_MODE:
301 {
302 const GLenum mode = (GLenum) (GLint) *param;
303 if (mode == GL_MODULATE ||
304 mode == GL_BLEND ||
305 mode == GL_DECAL ||
306 mode == GL_REPLACE ||
307 (mode == GL_ADD && ctx->Extensions.EXT_texture_env_add) ||
308 (mode == GL_COMBINE &&
309 (ctx->Extensions.EXT_texture_env_combine ||
310 ctx->Extensions.ARB_texture_env_combine))) {
311 /* legal */
312 if (texUnit->EnvMode == mode)
313 return;
314 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
315 texUnit->EnvMode = mode;
316
317 /* FIXME: Is this needed here or is having it in
318 * FIXME: update_texture_state enough?
319 */
320 if ( mode == GL_COMBINE ) {
321 texUnit->_CurrentCombine = & texUnit->Combine;
322 }
323 else if ( texUnit->_Current != NULL ) {
324 calculate_derived_texenv( & texUnit->_EnvMode, mode,
325 texUnit->_Current->Image[0][0]->Format );
326 texUnit->_CurrentCombine = & texUnit->_EnvMode;
327 }
328 }
329 else {
330 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
331 return;
332 }
333 }
334 break;
335 case GL_TEXTURE_ENV_COLOR:
336 {
337 GLfloat tmp[4];
338 tmp[0] = CLAMP( param[0], 0.0F, 1.0F );
339 tmp[1] = CLAMP( param[1], 0.0F, 1.0F );
340 tmp[2] = CLAMP( param[2], 0.0F, 1.0F );
341 tmp[3] = CLAMP( param[3], 0.0F, 1.0F );
342 if (TEST_EQ_4V(tmp, texUnit->EnvColor))
343 return;
344 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
345 COPY_4FV(texUnit->EnvColor, tmp);
346 }
347 break;
348 case GL_COMBINE_RGB:
349 if (ctx->Extensions.EXT_texture_env_combine ||
350 ctx->Extensions.ARB_texture_env_combine) {
351 const GLenum mode = (GLenum) (GLint) *param;
352 switch (mode) {
353 case GL_REPLACE:
354 case GL_MODULATE:
355 case GL_ADD:
356 case GL_ADD_SIGNED:
357 case GL_INTERPOLATE:
358 /* OK */
359 break;
360 case GL_SUBTRACT:
361 if (!ctx->Extensions.ARB_texture_env_combine) {
362 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
363 return;
364 }
365 break;
366 case GL_DOT3_RGB_EXT:
367 case GL_DOT3_RGBA_EXT:
368 if (!ctx->Extensions.EXT_texture_env_dot3) {
369 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
370 return;
371 }
372 break;
373 case GL_DOT3_RGB:
374 case GL_DOT3_RGBA:
375 if (!ctx->Extensions.ARB_texture_env_dot3) {
376 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
377 return;
378 }
379 break;
380 case GL_MODULATE_ADD_ATI:
381 case GL_MODULATE_SIGNED_ADD_ATI:
382 case GL_MODULATE_SUBTRACT_ATI:
383 if (!ctx->Extensions.ATI_texture_env_combine3) {
384 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
385 return;
386 }
387 break;
388 default:
389 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
390 return;
391 }
392 if (texUnit->Combine.ModeRGB == mode)
393 return;
394 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
395 texUnit->Combine.ModeRGB = mode;
396 }
397 else {
398 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
399 return;
400 }
401 break;
402 case GL_COMBINE_ALPHA:
403 if (ctx->Extensions.EXT_texture_env_combine ||
404 ctx->Extensions.ARB_texture_env_combine) {
405 const GLenum mode = (GLenum) (GLint) *param;
406 switch (mode) {
407 case GL_REPLACE:
408 case GL_MODULATE:
409 case GL_ADD:
410 case GL_ADD_SIGNED:
411 case GL_INTERPOLATE:
412 /* OK */
413 break;
414 case GL_SUBTRACT:
415 if (!ctx->Extensions.ARB_texture_env_combine) {
416 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
417 return;
418 }
419 break;
420 case GL_MODULATE_ADD_ATI:
421 case GL_MODULATE_SIGNED_ADD_ATI:
422 case GL_MODULATE_SUBTRACT_ATI:
423 if (!ctx->Extensions.ATI_texture_env_combine3) {
424 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
425 return;
426 }
427 break;
428 default:
429 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
430 return;
431 }
432
433 if (texUnit->Combine.ModeA == mode)
434 return;
435 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
436 texUnit->Combine.ModeA = mode;
437 }
438 else {
439 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
440 return;
441 }
442 break;
443 case GL_SOURCE0_RGB:
444 case GL_SOURCE1_RGB:
445 case GL_SOURCE2_RGB:
446 if (ctx->Extensions.EXT_texture_env_combine ||
447 ctx->Extensions.ARB_texture_env_combine) {
448 const GLenum source = (GLenum) (GLint) *param;
449 const GLuint s = pname - GL_SOURCE0_RGB;
450 if (source == GL_TEXTURE ||
451 source == GL_CONSTANT ||
452 source == GL_PRIMARY_COLOR ||
453 source == GL_PREVIOUS ||
454 (ctx->Extensions.ARB_texture_env_crossbar &&
455 source >= GL_TEXTURE0 &&
456 source < GL_TEXTURE0 + ctx->Const.MaxTextureUnits) ||
457 (ctx->Extensions.ATI_texture_env_combine3 &&
458 (source == GL_ZERO || source == GL_ONE))) {
459 /* legal */
460 if (texUnit->Combine.SourceRGB[s] == source)
461 return;
462 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
463 texUnit->Combine.SourceRGB[s] = source;
464 }
465 else {
466 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", source);
467 return;
468 }
469 }
470 else {
471 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
472 return;
473 }
474 break;
475 case GL_SOURCE0_ALPHA:
476 case GL_SOURCE1_ALPHA:
477 case GL_SOURCE2_ALPHA:
478 if (ctx->Extensions.EXT_texture_env_combine ||
479 ctx->Extensions.ARB_texture_env_combine) {
480 const GLenum source = (GLenum) (GLint) *param;
481 const GLuint s = pname - GL_SOURCE0_ALPHA;
482 if (source == GL_TEXTURE ||
483 source == GL_CONSTANT ||
484 source == GL_PRIMARY_COLOR ||
485 source == GL_PREVIOUS ||
486 (ctx->Extensions.ARB_texture_env_crossbar &&
487 source >= GL_TEXTURE0 &&
488 source < GL_TEXTURE0 + ctx->Const.MaxTextureUnits) ||
489 (ctx->Extensions.ATI_texture_env_combine3 &&
490 (source == GL_ZERO || source == GL_ONE))) {
491 /* legal */
492 if (texUnit->Combine.SourceA[s] == source)
493 return;
494 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
495 texUnit->Combine.SourceA[s] = source;
496 }
497 else {
498 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", source);
499 return;
500 }
501 }
502 else {
503 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
504 return;
505 }
506 break;
507 case GL_OPERAND0_RGB:
508 case GL_OPERAND1_RGB:
509 if (ctx->Extensions.EXT_texture_env_combine ||
510 ctx->Extensions.ARB_texture_env_combine) {
511 const GLenum operand = (GLenum) (GLint) *param;
512 const GLuint s = pname - GL_OPERAND0_RGB;
513 switch (operand) {
514 case GL_SRC_COLOR:
515 case GL_ONE_MINUS_SRC_COLOR:
516 case GL_SRC_ALPHA:
517 case GL_ONE_MINUS_SRC_ALPHA:
518 if (texUnit->Combine.OperandRGB[s] == operand)
519 return;
520 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
521 texUnit->Combine.OperandRGB[s] = operand;
522 break;
523 default:
524 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", operand);
525 return;
526 }
527 }
528 else {
529 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
530 return;
531 }
532 break;
533 case GL_OPERAND0_ALPHA:
534 case GL_OPERAND1_ALPHA:
535 if (ctx->Extensions.EXT_texture_env_combine ||
536 ctx->Extensions.ARB_texture_env_combine) {
537 const GLenum operand = (GLenum) (GLint) *param;
538 switch (operand) {
539 case GL_SRC_ALPHA:
540 case GL_ONE_MINUS_SRC_ALPHA:
541 if (texUnit->Combine.OperandA[pname-GL_OPERAND0_ALPHA] ==
542 operand)
543 return;
544 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
545 texUnit->Combine.OperandA[pname-GL_OPERAND0_ALPHA] = operand;
546 break;
547 default:
548 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", operand);
549 return;
550 }
551 }
552 else {
553 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
554 return;
555 }
556 break;
557 case GL_OPERAND2_RGB:
558 if (ctx->Extensions.EXT_texture_env_combine ||
559 ctx->Extensions.ARB_texture_env_combine) {
560 const GLenum operand = (GLenum) (GLint) *param;
561 switch (operand) {
562 case GL_SRC_COLOR: /* ARB combine only */
563 case GL_ONE_MINUS_SRC_COLOR: /* ARB combine only */
564 case GL_SRC_ALPHA:
565 case GL_ONE_MINUS_SRC_ALPHA: /* ARB combine only */
566 if (texUnit->Combine.OperandRGB[2] == operand)
567 return;
568 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
569 texUnit->Combine.OperandRGB[2] = operand;
570 break;
571 default:
572 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", operand);
573 return;
574 }
575 }
576 else {
577 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
578 return;
579 }
580 break;
581 case GL_OPERAND2_ALPHA:
582 if (ctx->Extensions.EXT_texture_env_combine ||
583 ctx->Extensions.ARB_texture_env_combine) {
584 const GLenum operand = (GLenum) (GLint) *param;
585 switch (operand) {
586 case GL_SRC_ALPHA:
587 case GL_ONE_MINUS_SRC_ALPHA: /* ARB combine only */
588 if (texUnit->Combine.OperandA[2] == operand)
589 return;
590 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
591 texUnit->Combine.OperandA[2] = operand;
592 break;
593 default:
594 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", operand);
595 return;
596 }
597 }
598 else {
599 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
600 return;
601 }
602 break;
603 case GL_RGB_SCALE:
604 if (ctx->Extensions.EXT_texture_env_combine ||
605 ctx->Extensions.ARB_texture_env_combine) {
606 GLuint newshift;
607 if (*param == 1.0) {
608 newshift = 0;
609 }
610 else if (*param == 2.0) {
611 newshift = 1;
612 }
613 else if (*param == 4.0) {
614 newshift = 2;
615 }
616 else {
617 _mesa_error( ctx, GL_INVALID_VALUE,
618 "glTexEnv(GL_RGB_SCALE not 1, 2 or 4)" );
619 return;
620 }
621 if (texUnit->Combine.ScaleShiftRGB == newshift)
622 return;
623 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
624 texUnit->Combine.ScaleShiftRGB = newshift;
625 }
626 else {
627 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
628 return;
629 }
630 break;
631 case GL_ALPHA_SCALE:
632 if (ctx->Extensions.EXT_texture_env_combine ||
633 ctx->Extensions.ARB_texture_env_combine) {
634 GLuint newshift;
635 if (*param == 1.0) {
636 newshift = 0;
637 }
638 else if (*param == 2.0) {
639 newshift = 1;
640 }
641 else if (*param == 4.0) {
642 newshift = 2;
643 }
644 else {
645 _mesa_error( ctx, GL_INVALID_VALUE,
646 "glTexEnv(GL_ALPHA_SCALE not 1, 2 or 4)" );
647 return;
648 }
649 if (texUnit->Combine.ScaleShiftA == newshift)
650 return;
651 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
652 texUnit->Combine.ScaleShiftA = newshift;
653 }
654 else {
655 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
656 return;
657 }
658 break;
659 default:
660 _mesa_error( ctx, GL_INVALID_ENUM, "glTexEnv(pname)" );
661 return;
662 }
663 }
664 else if (target == GL_TEXTURE_FILTER_CONTROL_EXT) {
665 /* GL_EXT_texture_lod_bias */
666 if (!ctx->Extensions.EXT_texture_lod_bias) {
667 _mesa_error( ctx, GL_INVALID_ENUM, "glTexEnv(target=0x%x)", target );
668 return;
669 }
670 if (pname == GL_TEXTURE_LOD_BIAS_EXT) {
671 if (texUnit->LodBias == param[0])
672 return;
673 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
674 texUnit->LodBias = param[0];
675 }
676 else {
677 TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
678 return;
679 }
680 }
681 else if (target == GL_POINT_SPRITE_NV) {
682 /* GL_ARB_point_sprite / GL_NV_point_sprite */
683 if (!ctx->Extensions.NV_point_sprite
684 && !ctx->Extensions.ARB_point_sprite) {
685 _mesa_error( ctx, GL_INVALID_ENUM, "glTexEnv(target=0x%x)", target );
686 return;
687 }
688 if (pname == GL_COORD_REPLACE_NV) {
689 const GLenum value = (GLenum) param[0];
690 if (value == GL_TRUE || value == GL_FALSE) {
691 /* It's kind of weird to set point state via glTexEnv,
692 * but that's what the spec calls for.
693 */
694 const GLboolean state = (GLboolean) value;
695 if (ctx->Point.CoordReplace[ctx->Texture.CurrentUnit] == state)
696 return;
697 FLUSH_VERTICES(ctx, _NEW_POINT);
698 ctx->Point.CoordReplace[ctx->Texture.CurrentUnit] = state;
699 }
700 else {
701 _mesa_error( ctx, GL_INVALID_VALUE, "glTexEnv(param=0x%x)", value);
702 return;
703 }
704 }
705 else {
706 _mesa_error( ctx, GL_INVALID_ENUM, "glTexEnv(pname=0x%x)", pname );
707 return;
708 }
709 }
710 else {
711 _mesa_error( ctx, GL_INVALID_ENUM, "glTexEnv(target=0x%x)",target );
712 return;
713 }
714
715 if (MESA_VERBOSE&(VERBOSE_API|VERBOSE_TEXTURE))
716 _mesa_debug(ctx, "glTexEnv %s %s %.1f(%s) ...\n",
717 _mesa_lookup_enum_by_nr(target),
718 _mesa_lookup_enum_by_nr(pname),
719 *param,
720 _mesa_lookup_enum_by_nr((GLenum) (GLint) *param));
721
722 /* Tell device driver about the new texture environment */
723 if (ctx->Driver.TexEnv) {
724 (*ctx->Driver.TexEnv)( ctx, target, pname, param );
725 }
726 }
727
728
729 void GLAPIENTRY
730 _mesa_TexEnvf( GLenum target, GLenum pname, GLfloat param )
731 {
732 _mesa_TexEnvfv( target, pname, &param );
733 }
734
735
736
737 void GLAPIENTRY
738 _mesa_TexEnvi( GLenum target, GLenum pname, GLint param )
739 {
740 GLfloat p[4];
741 p[0] = (GLfloat) param;
742 p[1] = p[2] = p[3] = 0.0;
743 _mesa_TexEnvfv( target, pname, p );
744 }
745
746
747 void GLAPIENTRY
748 _mesa_TexEnviv( GLenum target, GLenum pname, const GLint *param )
749 {
750 GLfloat p[4];
751 if (pname == GL_TEXTURE_ENV_COLOR) {
752 p[0] = INT_TO_FLOAT( param[0] );
753 p[1] = INT_TO_FLOAT( param[1] );
754 p[2] = INT_TO_FLOAT( param[2] );
755 p[3] = INT_TO_FLOAT( param[3] );
756 }
757 else {
758 p[0] = (GLfloat) param[0];
759 p[1] = p[2] = p[3] = 0; /* init to zero, just to be safe */
760 }
761 _mesa_TexEnvfv( target, pname, p );
762 }
763
764
765 void GLAPIENTRY
766 _mesa_GetTexEnvfv( GLenum target, GLenum pname, GLfloat *params )
767 {
768 GET_CURRENT_CONTEXT(ctx);
769 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
770 ASSERT_OUTSIDE_BEGIN_END(ctx);
771
772 if (target == GL_TEXTURE_ENV) {
773 switch (pname) {
774 case GL_TEXTURE_ENV_MODE:
775 *params = ENUM_TO_FLOAT(texUnit->EnvMode);
776 break;
777 case GL_TEXTURE_ENV_COLOR:
778 COPY_4FV( params, texUnit->EnvColor );
779 break;
780 case GL_COMBINE_RGB:
781 if (ctx->Extensions.EXT_texture_env_combine ||
782 ctx->Extensions.ARB_texture_env_combine) {
783 *params = (GLfloat) texUnit->Combine.ModeRGB;
784 }
785 else {
786 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
787 }
788 break;
789 case GL_COMBINE_ALPHA:
790 if (ctx->Extensions.EXT_texture_env_combine ||
791 ctx->Extensions.ARB_texture_env_combine) {
792 *params = (GLfloat) texUnit->Combine.ModeA;
793 }
794 else {
795 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
796 }
797 break;
798 case GL_SOURCE0_RGB:
799 if (ctx->Extensions.EXT_texture_env_combine ||
800 ctx->Extensions.ARB_texture_env_combine) {
801 *params = (GLfloat) texUnit->Combine.SourceRGB[0];
802 }
803 else {
804 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
805 }
806 break;
807 case GL_SOURCE1_RGB:
808 if (ctx->Extensions.EXT_texture_env_combine ||
809 ctx->Extensions.ARB_texture_env_combine) {
810 *params = (GLfloat) texUnit->Combine.SourceRGB[1];
811 }
812 else {
813 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
814 }
815 break;
816 case GL_SOURCE2_RGB:
817 if (ctx->Extensions.EXT_texture_env_combine ||
818 ctx->Extensions.ARB_texture_env_combine) {
819 *params = (GLfloat) texUnit->Combine.SourceRGB[2];
820 }
821 else {
822 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
823 }
824 break;
825 case GL_SOURCE0_ALPHA:
826 if (ctx->Extensions.EXT_texture_env_combine ||
827 ctx->Extensions.ARB_texture_env_combine) {
828 *params = (GLfloat) texUnit->Combine.SourceA[0];
829 }
830 else {
831 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
832 }
833 break;
834 case GL_SOURCE1_ALPHA:
835 if (ctx->Extensions.EXT_texture_env_combine ||
836 ctx->Extensions.ARB_texture_env_combine) {
837 *params = (GLfloat) texUnit->Combine.SourceA[1];
838 }
839 else {
840 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
841 }
842 break;
843 case GL_SOURCE2_ALPHA:
844 if (ctx->Extensions.EXT_texture_env_combine ||
845 ctx->Extensions.ARB_texture_env_combine) {
846 *params = (GLfloat) texUnit->Combine.SourceA[2];
847 }
848 else {
849 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
850 }
851 break;
852 case GL_OPERAND0_RGB:
853 if (ctx->Extensions.EXT_texture_env_combine ||
854 ctx->Extensions.ARB_texture_env_combine) {
855 *params = (GLfloat) texUnit->Combine.OperandRGB[0];
856 }
857 else {
858 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
859 }
860 break;
861 case GL_OPERAND1_RGB:
862 if (ctx->Extensions.EXT_texture_env_combine ||
863 ctx->Extensions.ARB_texture_env_combine) {
864 *params = (GLfloat) texUnit->Combine.OperandRGB[1];
865 }
866 else {
867 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
868 }
869 break;
870 case GL_OPERAND2_RGB:
871 if (ctx->Extensions.EXT_texture_env_combine ||
872 ctx->Extensions.ARB_texture_env_combine) {
873 *params = (GLfloat) texUnit->Combine.OperandRGB[2];
874 }
875 else {
876 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
877 }
878 break;
879 case GL_OPERAND0_ALPHA:
880 if (ctx->Extensions.EXT_texture_env_combine ||
881 ctx->Extensions.ARB_texture_env_combine) {
882 *params = (GLfloat) texUnit->Combine.OperandA[0];
883 }
884 else {
885 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
886 }
887 break;
888 case GL_OPERAND1_ALPHA:
889 if (ctx->Extensions.EXT_texture_env_combine ||
890 ctx->Extensions.ARB_texture_env_combine) {
891 *params = (GLfloat) texUnit->Combine.OperandA[1];
892 }
893 else {
894 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
895 }
896 break;
897 case GL_OPERAND2_ALPHA:
898 if (ctx->Extensions.EXT_texture_env_combine ||
899 ctx->Extensions.ARB_texture_env_combine) {
900 *params = (GLfloat) texUnit->Combine.OperandA[2];
901 }
902 else {
903 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
904 }
905 break;
906 case GL_RGB_SCALE:
907 if (ctx->Extensions.EXT_texture_env_combine ||
908 ctx->Extensions.ARB_texture_env_combine) {
909 if (texUnit->Combine.ScaleShiftRGB == 0)
910 *params = 1.0;
911 else if (texUnit->Combine.ScaleShiftRGB == 1)
912 *params = 2.0;
913 else
914 *params = 4.0;
915 }
916 else {
917 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
918 return;
919 }
920 break;
921 case GL_ALPHA_SCALE:
922 if (ctx->Extensions.EXT_texture_env_combine ||
923 ctx->Extensions.ARB_texture_env_combine) {
924 if (texUnit->Combine.ScaleShiftA == 0)
925 *params = 1.0;
926 else if (texUnit->Combine.ScaleShiftA == 1)
927 *params = 2.0;
928 else
929 *params = 4.0;
930 }
931 else {
932 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
933 return;
934 }
935 break;
936 default:
937 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname=0x%x)", pname);
938 }
939 }
940 else if (target == GL_TEXTURE_FILTER_CONTROL_EXT) {
941 /* GL_EXT_texture_lod_bias */
942 if (!ctx->Extensions.EXT_texture_lod_bias) {
943 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnvfv(target)" );
944 return;
945 }
946 if (pname == GL_TEXTURE_LOD_BIAS_EXT) {
947 *params = texUnit->LodBias;
948 }
949 else {
950 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)" );
951 return;
952 }
953 }
954 else if (target == GL_POINT_SPRITE_NV) {
955 /* GL_ARB_point_sprite / GL_NV_point_sprite */
956 if (!ctx->Extensions.NV_point_sprite
957 && !ctx->Extensions.ARB_point_sprite) {
958 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnvfv(target)" );
959 return;
960 }
961 if (pname == GL_COORD_REPLACE_NV) {
962 *params = (GLfloat) ctx->Point.CoordReplace[ctx->Texture.CurrentUnit];
963 }
964 else {
965 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)" );
966 return;
967 }
968 }
969 else {
970 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnvfv(target)" );
971 return;
972 }
973 }
974
975
976 void GLAPIENTRY
977 _mesa_GetTexEnviv( GLenum target, GLenum pname, GLint *params )
978 {
979 GET_CURRENT_CONTEXT(ctx);
980 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
981 ASSERT_OUTSIDE_BEGIN_END(ctx);
982
983 if (target == GL_TEXTURE_ENV) {
984 switch (pname) {
985 case GL_TEXTURE_ENV_MODE:
986 *params = (GLint) texUnit->EnvMode;
987 break;
988 case GL_TEXTURE_ENV_COLOR:
989 params[0] = FLOAT_TO_INT( texUnit->EnvColor[0] );
990 params[1] = FLOAT_TO_INT( texUnit->EnvColor[1] );
991 params[2] = FLOAT_TO_INT( texUnit->EnvColor[2] );
992 params[3] = FLOAT_TO_INT( texUnit->EnvColor[3] );
993 break;
994 case GL_COMBINE_RGB:
995 if (ctx->Extensions.EXT_texture_env_combine ||
996 ctx->Extensions.ARB_texture_env_combine) {
997 *params = (GLint) texUnit->Combine.ModeRGB;
998 }
999 else {
1000 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1001 }
1002 break;
1003 case GL_COMBINE_ALPHA:
1004 if (ctx->Extensions.EXT_texture_env_combine ||
1005 ctx->Extensions.ARB_texture_env_combine) {
1006 *params = (GLint) texUnit->Combine.ModeA;
1007 }
1008 else {
1009 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1010 }
1011 break;
1012 case GL_SOURCE0_RGB:
1013 if (ctx->Extensions.EXT_texture_env_combine ||
1014 ctx->Extensions.ARB_texture_env_combine) {
1015 *params = (GLint) texUnit->Combine.SourceRGB[0];
1016 }
1017 else {
1018 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1019 }
1020 break;
1021 case GL_SOURCE1_RGB:
1022 if (ctx->Extensions.EXT_texture_env_combine ||
1023 ctx->Extensions.ARB_texture_env_combine) {
1024 *params = (GLint) texUnit->Combine.SourceRGB[1];
1025 }
1026 else {
1027 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1028 }
1029 break;
1030 case GL_SOURCE2_RGB:
1031 if (ctx->Extensions.EXT_texture_env_combine ||
1032 ctx->Extensions.ARB_texture_env_combine) {
1033 *params = (GLint) texUnit->Combine.SourceRGB[2];
1034 }
1035 else {
1036 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1037 }
1038 break;
1039 case GL_SOURCE0_ALPHA:
1040 if (ctx->Extensions.EXT_texture_env_combine ||
1041 ctx->Extensions.ARB_texture_env_combine) {
1042 *params = (GLint) texUnit->Combine.SourceA[0];
1043 }
1044 else {
1045 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1046 }
1047 break;
1048 case GL_SOURCE1_ALPHA:
1049 if (ctx->Extensions.EXT_texture_env_combine ||
1050 ctx->Extensions.ARB_texture_env_combine) {
1051 *params = (GLint) texUnit->Combine.SourceA[1];
1052 }
1053 else {
1054 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1055 }
1056 break;
1057 case GL_SOURCE2_ALPHA:
1058 if (ctx->Extensions.EXT_texture_env_combine ||
1059 ctx->Extensions.ARB_texture_env_combine) {
1060 *params = (GLint) texUnit->Combine.SourceA[2];
1061 }
1062 else {
1063 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1064 }
1065 break;
1066 case GL_OPERAND0_RGB:
1067 if (ctx->Extensions.EXT_texture_env_combine ||
1068 ctx->Extensions.ARB_texture_env_combine) {
1069 *params = (GLint) texUnit->Combine.OperandRGB[0];
1070 }
1071 else {
1072 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1073 }
1074 break;
1075 case GL_OPERAND1_RGB:
1076 if (ctx->Extensions.EXT_texture_env_combine ||
1077 ctx->Extensions.ARB_texture_env_combine) {
1078 *params = (GLint) texUnit->Combine.OperandRGB[1];
1079 }
1080 else {
1081 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1082 }
1083 break;
1084 case GL_OPERAND2_RGB:
1085 if (ctx->Extensions.EXT_texture_env_combine ||
1086 ctx->Extensions.ARB_texture_env_combine) {
1087 *params = (GLint) texUnit->Combine.OperandRGB[2];
1088 }
1089 else {
1090 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1091 }
1092 break;
1093 case GL_OPERAND0_ALPHA:
1094 if (ctx->Extensions.EXT_texture_env_combine ||
1095 ctx->Extensions.ARB_texture_env_combine) {
1096 *params = (GLint) texUnit->Combine.OperandA[0];
1097 }
1098 else {
1099 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1100 }
1101 break;
1102 case GL_OPERAND1_ALPHA:
1103 if (ctx->Extensions.EXT_texture_env_combine ||
1104 ctx->Extensions.ARB_texture_env_combine) {
1105 *params = (GLint) texUnit->Combine.OperandA[1];
1106 }
1107 else {
1108 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1109 }
1110 break;
1111 case GL_OPERAND2_ALPHA:
1112 if (ctx->Extensions.EXT_texture_env_combine ||
1113 ctx->Extensions.ARB_texture_env_combine) {
1114 *params = (GLint) texUnit->Combine.OperandA[2];
1115 }
1116 else {
1117 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1118 }
1119 break;
1120 case GL_RGB_SCALE:
1121 if (ctx->Extensions.EXT_texture_env_combine ||
1122 ctx->Extensions.ARB_texture_env_combine) {
1123 if (texUnit->Combine.ScaleShiftRGB == 0)
1124 *params = 1;
1125 else if (texUnit->Combine.ScaleShiftRGB == 1)
1126 *params = 2;
1127 else
1128 *params = 4;
1129 }
1130 else {
1131 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1132 return;
1133 }
1134 break;
1135 case GL_ALPHA_SCALE:
1136 if (ctx->Extensions.EXT_texture_env_combine ||
1137 ctx->Extensions.ARB_texture_env_combine) {
1138 if (texUnit->Combine.ScaleShiftA == 0)
1139 *params = 1;
1140 else if (texUnit->Combine.ScaleShiftA == 1)
1141 *params = 2;
1142 else
1143 *params = 4;
1144 }
1145 else {
1146 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
1147 return;
1148 }
1149 break;
1150 default:
1151 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname=0x%x)",
1152 pname);
1153 }
1154 }
1155 else if (target == GL_TEXTURE_FILTER_CONTROL_EXT) {
1156 /* GL_EXT_texture_lod_bias */
1157 if (!ctx->Extensions.EXT_texture_lod_bias) {
1158 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnviv(target)" );
1159 return;
1160 }
1161 if (pname == GL_TEXTURE_LOD_BIAS_EXT) {
1162 *params = (GLint) texUnit->LodBias;
1163 }
1164 else {
1165 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)" );
1166 return;
1167 }
1168 }
1169 else if (target == GL_POINT_SPRITE_NV) {
1170 /* GL_ARB_point_sprite / GL_NV_point_sprite */
1171 if (!ctx->Extensions.NV_point_sprite
1172 && !ctx->Extensions.ARB_point_sprite) {
1173 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnviv(target)" );
1174 return;
1175 }
1176 if (pname == GL_COORD_REPLACE_NV) {
1177 *params = (GLint) ctx->Point.CoordReplace[ctx->Texture.CurrentUnit];
1178 }
1179 else {
1180 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)" );
1181 return;
1182 }
1183 }
1184 else {
1185 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnviv(target)" );
1186 return;
1187 }
1188 }
1189
1190
1191
1192
1193 /**********************************************************************/
1194 /* Texture Parameters */
1195 /**********************************************************************/
1196
1197 static GLboolean
1198 _mesa_validate_texture_wrap_mode(GLcontext * ctx,
1199 GLenum target, GLenum eparam)
1200 {
1201 const struct gl_extensions * const e = & ctx->Extensions;
1202
1203 if (eparam == GL_CLAMP || eparam == GL_CLAMP_TO_EDGE ||
1204 (eparam == GL_CLAMP_TO_BORDER && e->ARB_texture_border_clamp)) {
1205 /* any texture target */
1206 return GL_TRUE;
1207 }
1208 else if (target != GL_TEXTURE_RECTANGLE_NV &&
1209 (eparam == GL_REPEAT ||
1210 (eparam == GL_MIRRORED_REPEAT &&
1211 e->ARB_texture_mirrored_repeat) ||
1212 (eparam == GL_MIRROR_CLAMP_EXT &&
1213 (e->ATI_texture_mirror_once || e->EXT_texture_mirror_clamp)) ||
1214 (eparam == GL_MIRROR_CLAMP_TO_EDGE_EXT &&
1215 (e->ATI_texture_mirror_once || e->EXT_texture_mirror_clamp)) ||
1216 (eparam == GL_MIRROR_CLAMP_TO_BORDER_EXT &&
1217 (e->EXT_texture_mirror_clamp)))) {
1218 /* non-rectangle texture */
1219 return GL_TRUE;
1220 }
1221
1222 _mesa_error( ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
1223 return GL_FALSE;
1224 }
1225
1226
1227 void GLAPIENTRY
1228 _mesa_TexParameterf( GLenum target, GLenum pname, GLfloat param )
1229 {
1230 _mesa_TexParameterfv(target, pname, &param);
1231 }
1232
1233
1234 void GLAPIENTRY
1235 _mesa_TexParameterfv( GLenum target, GLenum pname, const GLfloat *params )
1236 {
1237 GET_CURRENT_CONTEXT(ctx);
1238 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1239 GLenum eparam = (GLenum) (GLint) params[0];
1240 struct gl_texture_object *texObj;
1241 ASSERT_OUTSIDE_BEGIN_END(ctx);
1242
1243 if (MESA_VERBOSE&(VERBOSE_API|VERBOSE_TEXTURE))
1244 _mesa_debug(ctx, "glTexParameter %s %s %.1f(%s)...\n",
1245 _mesa_lookup_enum_by_nr(target),
1246 _mesa_lookup_enum_by_nr(pname),
1247 *params,
1248 _mesa_lookup_enum_by_nr(eparam));
1249
1250
1251 switch (target) {
1252 case GL_TEXTURE_1D:
1253 texObj = texUnit->Current1D;
1254 break;
1255 case GL_TEXTURE_2D:
1256 texObj = texUnit->Current2D;
1257 break;
1258 case GL_TEXTURE_3D:
1259 texObj = texUnit->Current3D;
1260 break;
1261 case GL_TEXTURE_CUBE_MAP:
1262 if (!ctx->Extensions.ARB_texture_cube_map) {
1263 _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(target)" );
1264 return;
1265 }
1266 texObj = texUnit->CurrentCubeMap;
1267 break;
1268 case GL_TEXTURE_RECTANGLE_NV:
1269 if (!ctx->Extensions.NV_texture_rectangle) {
1270 _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(target)" );
1271 return;
1272 }
1273 texObj = texUnit->CurrentRect;
1274 break;
1275 default:
1276 _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(target)" );
1277 return;
1278 }
1279
1280 switch (pname) {
1281 case GL_TEXTURE_MIN_FILTER:
1282 /* A small optimization */
1283 if (texObj->MinFilter == eparam)
1284 return;
1285 if (eparam==GL_NEAREST || eparam==GL_LINEAR) {
1286 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1287 texObj->MinFilter = eparam;
1288 }
1289 else if ((eparam==GL_NEAREST_MIPMAP_NEAREST ||
1290 eparam==GL_LINEAR_MIPMAP_NEAREST ||
1291 eparam==GL_NEAREST_MIPMAP_LINEAR ||
1292 eparam==GL_LINEAR_MIPMAP_LINEAR) &&
1293 texObj->Target != GL_TEXTURE_RECTANGLE_NV) {
1294 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1295 texObj->MinFilter = eparam;
1296 }
1297 else {
1298 _mesa_error( ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
1299 return;
1300 }
1301 break;
1302 case GL_TEXTURE_MAG_FILTER:
1303 /* A small optimization */
1304 if (texObj->MagFilter == eparam)
1305 return;
1306
1307 if (eparam==GL_NEAREST || eparam==GL_LINEAR) {
1308 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1309 texObj->MagFilter = eparam;
1310 }
1311 else {
1312 _mesa_error( ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
1313 return;
1314 }
1315 break;
1316 case GL_TEXTURE_WRAP_S:
1317 if (texObj->WrapS == eparam)
1318 return;
1319 if (_mesa_validate_texture_wrap_mode(ctx, texObj->Target, eparam)) {
1320 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1321 texObj->WrapS = eparam;
1322 }
1323 else {
1324 return;
1325 }
1326 break;
1327 case GL_TEXTURE_WRAP_T:
1328 if (texObj->WrapT == eparam)
1329 return;
1330 if (_mesa_validate_texture_wrap_mode(ctx, texObj->Target, eparam)) {
1331 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1332 texObj->WrapT = eparam;
1333 }
1334 else {
1335 return;
1336 }
1337 break;
1338 case GL_TEXTURE_WRAP_R:
1339 if (texObj->WrapR == eparam)
1340 return;
1341 if (_mesa_validate_texture_wrap_mode(ctx, texObj->Target, eparam)) {
1342 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1343 texObj->WrapR = eparam;
1344 }
1345 else {
1346 return;
1347 }
1348 break;
1349 case GL_TEXTURE_BORDER_COLOR:
1350 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1351 texObj->BorderColor[RCOMP] = params[0];
1352 texObj->BorderColor[GCOMP] = params[1];
1353 texObj->BorderColor[BCOMP] = params[2];
1354 texObj->BorderColor[ACOMP] = params[3];
1355 UNCLAMPED_FLOAT_TO_CHAN(texObj->_BorderChan[RCOMP], params[0]);
1356 UNCLAMPED_FLOAT_TO_CHAN(texObj->_BorderChan[GCOMP], params[1]);
1357 UNCLAMPED_FLOAT_TO_CHAN(texObj->_BorderChan[BCOMP], params[2]);
1358 UNCLAMPED_FLOAT_TO_CHAN(texObj->_BorderChan[ACOMP], params[3]);
1359 break;
1360 case GL_TEXTURE_MIN_LOD:
1361 if (texObj->MinLod == params[0])
1362 return;
1363 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1364 texObj->MinLod = params[0];
1365 break;
1366 case GL_TEXTURE_MAX_LOD:
1367 if (texObj->MaxLod == params[0])
1368 return;
1369 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1370 texObj->MaxLod = params[0];
1371 break;
1372 case GL_TEXTURE_BASE_LEVEL:
1373 if (params[0] < 0.0) {
1374 _mesa_error(ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
1375 return;
1376 }
1377 if (target == GL_TEXTURE_RECTANGLE_NV && params[0] != 0.0) {
1378 _mesa_error(ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
1379 return;
1380 }
1381 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1382 texObj->BaseLevel = (GLint) params[0];
1383 break;
1384 case GL_TEXTURE_MAX_LEVEL:
1385 if (params[0] < 0.0) {
1386 _mesa_error(ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
1387 return;
1388 }
1389 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1390 texObj->MaxLevel = (GLint) params[0];
1391 break;
1392 case GL_TEXTURE_PRIORITY:
1393 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1394 texObj->Priority = CLAMP( params[0], 0.0F, 1.0F );
1395 break;
1396 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
1397 if (ctx->Extensions.EXT_texture_filter_anisotropic) {
1398 if (params[0] < 1.0) {
1399 _mesa_error(ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
1400 return;
1401 }
1402 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1403 texObj->MaxAnisotropy = params[0];
1404 }
1405 else {
1406 _mesa_error(ctx, GL_INVALID_ENUM,
1407 "glTexParameter(pname=GL_TEXTURE_MAX_ANISOTROPY_EXT)");
1408 return;
1409 }
1410 break;
1411 case GL_TEXTURE_COMPARE_SGIX:
1412 if (ctx->Extensions.SGIX_shadow) {
1413 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1414 texObj->CompareFlag = params[0] ? GL_TRUE : GL_FALSE;
1415 }
1416 else {
1417 _mesa_error(ctx, GL_INVALID_ENUM,
1418 "glTexParameter(pname=GL_TEXTURE_COMPARE_SGIX)");
1419 return;
1420 }
1421 break;
1422 case GL_TEXTURE_COMPARE_OPERATOR_SGIX:
1423 if (ctx->Extensions.SGIX_shadow) {
1424 GLenum op = (GLenum) params[0];
1425 if (op == GL_TEXTURE_LEQUAL_R_SGIX ||
1426 op == GL_TEXTURE_GEQUAL_R_SGIX) {
1427 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1428 texObj->CompareOperator = op;
1429 }
1430 else {
1431 _mesa_error(ctx, GL_INVALID_ENUM, "glTexParameter(param)");
1432 }
1433 }
1434 else {
1435 _mesa_error(ctx, GL_INVALID_ENUM,
1436 "glTexParameter(pname=GL_TEXTURE_COMPARE_OPERATOR_SGIX)");
1437 return;
1438 }
1439 break;
1440 case GL_SHADOW_AMBIENT_SGIX: /* aka GL_TEXTURE_COMPARE_FAIL_VALUE_ARB */
1441 if (ctx->Extensions.SGIX_shadow_ambient) {
1442 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1443 texObj->ShadowAmbient = CLAMP(params[0], 0.0F, 1.0F);
1444 }
1445 else {
1446 _mesa_error(ctx, GL_INVALID_ENUM,
1447 "glTexParameter(pname=GL_SHADOW_AMBIENT_SGIX)");
1448 return;
1449 }
1450 break;
1451 case GL_GENERATE_MIPMAP_SGIS:
1452 if (ctx->Extensions.SGIS_generate_mipmap) {
1453 texObj->GenerateMipmap = params[0] ? GL_TRUE : GL_FALSE;
1454 }
1455 else {
1456 _mesa_error(ctx, GL_INVALID_ENUM,
1457 "glTexParameter(pname=GL_GENERATE_MIPMAP_SGIS)");
1458 return;
1459 }
1460 break;
1461 case GL_TEXTURE_COMPARE_MODE_ARB:
1462 if (ctx->Extensions.ARB_shadow) {
1463 const GLenum mode = (GLenum) params[0];
1464 if (mode == GL_NONE || mode == GL_COMPARE_R_TO_TEXTURE_ARB) {
1465 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1466 texObj->CompareMode = mode;
1467 }
1468 else {
1469 _mesa_error(ctx, GL_INVALID_ENUM,
1470 "glTexParameter(bad GL_TEXTURE_COMPARE_MODE_ARB: 0x%x)", mode);
1471 return;
1472 }
1473 }
1474 else {
1475 _mesa_error(ctx, GL_INVALID_ENUM,
1476 "glTexParameter(pname=GL_TEXTURE_COMPARE_MODE_ARB)");
1477 return;
1478 }
1479 break;
1480 case GL_TEXTURE_COMPARE_FUNC_ARB:
1481 if (ctx->Extensions.ARB_shadow) {
1482 const GLenum func = (GLenum) params[0];
1483 if (func == GL_LEQUAL || func == GL_GEQUAL) {
1484 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1485 texObj->CompareFunc = func;
1486 }
1487 else if (ctx->Extensions.EXT_shadow_funcs &&
1488 (func == GL_EQUAL ||
1489 func == GL_NOTEQUAL ||
1490 func == GL_LESS ||
1491 func == GL_GREATER ||
1492 func == GL_ALWAYS ||
1493 func == GL_NEVER)) {
1494 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1495 texObj->CompareFunc = func;
1496 }
1497 else {
1498 _mesa_error(ctx, GL_INVALID_ENUM,
1499 "glTexParameter(bad GL_TEXTURE_COMPARE_FUNC_ARB)");
1500 return;
1501 }
1502 }
1503 else {
1504 _mesa_error(ctx, GL_INVALID_ENUM,
1505 "glTexParameter(pname=GL_TEXTURE_COMPARE_FUNC_ARB)");
1506 return;
1507 }
1508 break;
1509 case GL_DEPTH_TEXTURE_MODE_ARB:
1510 if (ctx->Extensions.ARB_depth_texture) {
1511 const GLenum result = (GLenum) params[0];
1512 if (result == GL_LUMINANCE || result == GL_INTENSITY
1513 || result == GL_ALPHA) {
1514 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1515 texObj->DepthMode = result;
1516 }
1517 else {
1518 _mesa_error(ctx, GL_INVALID_ENUM,
1519 "glTexParameter(bad GL_DEPTH_TEXTURE_MODE_ARB)");
1520 return;
1521 }
1522 }
1523 else {
1524 _mesa_error(ctx, GL_INVALID_ENUM,
1525 "glTexParameter(pname=GL_DEPTH_TEXTURE_MODE_ARB)");
1526 return;
1527 }
1528 break;
1529 case GL_TEXTURE_LOD_BIAS:
1530 /* NOTE: this is really part of OpenGL 1.4, not EXT_texture_lod_bias*/
1531 if (ctx->Extensions.EXT_texture_lod_bias) {
1532 if (texObj->LodBias != params[0]) {
1533 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1534 texObj->LodBias = params[0];
1535 }
1536 }
1537 break;
1538
1539 default:
1540 _mesa_error(ctx, GL_INVALID_ENUM,
1541 "glTexParameter(pname=0x%x)", pname);
1542 return;
1543 }
1544
1545 texObj->Complete = GL_FALSE;
1546
1547 if (ctx->Driver.TexParameter) {
1548 (*ctx->Driver.TexParameter)( ctx, target, texObj, pname, params );
1549 }
1550 }
1551
1552
1553 void GLAPIENTRY
1554 _mesa_TexParameteri( GLenum target, GLenum pname, GLint param )
1555 {
1556 GLfloat fparam[4];
1557 if (pname == GL_TEXTURE_PRIORITY)
1558 fparam[0] = INT_TO_FLOAT(param);
1559 else
1560 fparam[0] = (GLfloat) param;
1561 fparam[1] = fparam[2] = fparam[3] = 0.0;
1562 _mesa_TexParameterfv(target, pname, fparam);
1563 }
1564
1565
1566 void GLAPIENTRY
1567 _mesa_TexParameteriv( GLenum target, GLenum pname, const GLint *params )
1568 {
1569 GLfloat fparam[4];
1570 if (pname == GL_TEXTURE_BORDER_COLOR) {
1571 fparam[0] = INT_TO_FLOAT(params[0]);
1572 fparam[1] = INT_TO_FLOAT(params[1]);
1573 fparam[2] = INT_TO_FLOAT(params[2]);
1574 fparam[3] = INT_TO_FLOAT(params[3]);
1575 }
1576 else {
1577 if (pname == GL_TEXTURE_PRIORITY)
1578 fparam[0] = INT_TO_FLOAT(params[0]);
1579 else
1580 fparam[0] = (GLfloat) params[0];
1581 fparam[1] = fparam[2] = fparam[3] = 0.0F;
1582 }
1583 _mesa_TexParameterfv(target, pname, fparam);
1584 }
1585
1586
1587 void GLAPIENTRY
1588 _mesa_GetTexLevelParameterfv( GLenum target, GLint level,
1589 GLenum pname, GLfloat *params )
1590 {
1591 GLint iparam;
1592 _mesa_GetTexLevelParameteriv( target, level, pname, &iparam );
1593 *params = (GLfloat) iparam;
1594 }
1595
1596
1597 static GLuint
1598 tex_image_dimensions(GLcontext *ctx, GLenum target)
1599 {
1600 switch (target) {
1601 case GL_TEXTURE_1D:
1602 case GL_PROXY_TEXTURE_1D:
1603 return 1;
1604 case GL_TEXTURE_2D:
1605 case GL_PROXY_TEXTURE_2D:
1606 return 2;
1607 case GL_TEXTURE_3D:
1608 case GL_PROXY_TEXTURE_3D:
1609 return 3;
1610 case GL_TEXTURE_CUBE_MAP:
1611 case GL_PROXY_TEXTURE_CUBE_MAP:
1612 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1613 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1614 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1615 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1616 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1617 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1618 return ctx->Extensions.ARB_texture_cube_map ? 2 : 0;
1619 case GL_TEXTURE_RECTANGLE_NV:
1620 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1621 return ctx->Extensions.NV_texture_rectangle ? 2 : 0;
1622 default:
1623 _mesa_problem(ctx, "bad target in _mesa_tex_target_dimensions()");
1624 return 0;
1625 }
1626 }
1627
1628
1629 void GLAPIENTRY
1630 _mesa_GetTexLevelParameteriv( GLenum target, GLint level,
1631 GLenum pname, GLint *params )
1632 {
1633 GET_CURRENT_CONTEXT(ctx);
1634 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1635 const struct gl_texture_image *img = NULL;
1636 GLuint dimensions;
1637 GLboolean isProxy;
1638 GLint maxLevels;
1639 ASSERT_OUTSIDE_BEGIN_END(ctx);
1640
1641 /* this will catch bad target values */
1642 dimensions = tex_image_dimensions(ctx, target); /* 1, 2 or 3 */
1643 if (dimensions == 0) {
1644 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexLevelParameter[if]v(target)");
1645 return;
1646 }
1647
1648 switch (target) {
1649 case GL_TEXTURE_1D:
1650 case GL_PROXY_TEXTURE_1D:
1651 case GL_TEXTURE_2D:
1652 case GL_PROXY_TEXTURE_2D:
1653 maxLevels = ctx->Const.MaxTextureLevels;
1654 break;
1655 case GL_TEXTURE_3D:
1656 case GL_PROXY_TEXTURE_3D:
1657 maxLevels = ctx->Const.Max3DTextureLevels;
1658 break;
1659 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1660 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1661 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1662 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1663 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1664 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1665 case GL_PROXY_TEXTURE_CUBE_MAP:
1666 maxLevels = ctx->Const.MaxCubeTextureLevels;
1667 break;
1668 case GL_TEXTURE_RECTANGLE_NV:
1669 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1670 maxLevels = 1;
1671 break;
1672 default:
1673 _mesa_problem(ctx, "switch in _mesa_GetTexLevelParameter");
1674 return;
1675 }
1676
1677 if (level < 0 || level >= maxLevels) {
1678 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexLevelParameter[if]v" );
1679 return;
1680 }
1681
1682 img = _mesa_select_tex_image(ctx, texUnit, target, level);
1683 if (!img || !img->TexFormat) {
1684 /* undefined texture image */
1685 if (pname == GL_TEXTURE_COMPONENTS)
1686 *params = 1;
1687 else
1688 *params = 0;
1689 return;
1690 }
1691
1692 isProxy = (target == GL_PROXY_TEXTURE_1D) ||
1693 (target == GL_PROXY_TEXTURE_2D) ||
1694 (target == GL_PROXY_TEXTURE_3D) ||
1695 (target == GL_PROXY_TEXTURE_CUBE_MAP) ||
1696 (target == GL_PROXY_TEXTURE_RECTANGLE_NV);
1697
1698 switch (pname) {
1699 case GL_TEXTURE_WIDTH:
1700 *params = img->Width;
1701 return;
1702 case GL_TEXTURE_HEIGHT:
1703 *params = img->Height;
1704 return;
1705 case GL_TEXTURE_DEPTH:
1706 *params = img->Depth;
1707 return;
1708 case GL_TEXTURE_INTERNAL_FORMAT:
1709 *params = img->IntFormat;
1710 return;
1711 case GL_TEXTURE_BORDER:
1712 *params = img->Border;
1713 return;
1714 case GL_TEXTURE_RED_SIZE:
1715 if (img->Format == GL_RGB || img->Format == GL_RGBA)
1716 *params = img->TexFormat->RedBits;
1717 else
1718 *params = 0;
1719 return;
1720 case GL_TEXTURE_GREEN_SIZE:
1721 if (img->Format == GL_RGB || img->Format == GL_RGBA)
1722 *params = img->TexFormat->GreenBits;
1723 else
1724 *params = 0;
1725 return;
1726 case GL_TEXTURE_BLUE_SIZE:
1727 if (img->Format == GL_RGB || img->Format == GL_RGBA)
1728 *params = img->TexFormat->BlueBits;
1729 else
1730 *params = 0;
1731 return;
1732 case GL_TEXTURE_ALPHA_SIZE:
1733 if (img->Format == GL_ALPHA || img->Format == GL_LUMINANCE_ALPHA ||
1734 img->Format == GL_RGBA)
1735 *params = img->TexFormat->AlphaBits;
1736 else
1737 *params = 0;
1738 return;
1739 case GL_TEXTURE_INTENSITY_SIZE:
1740 if (img->Format != GL_INTENSITY)
1741 *params = 0;
1742 else if (img->TexFormat->IntensityBits > 0)
1743 *params = img->TexFormat->IntensityBits;
1744 else /* intensity probably stored as rgb texture */
1745 *params = MIN2(img->TexFormat->RedBits, img->TexFormat->GreenBits);
1746 return;
1747 case GL_TEXTURE_LUMINANCE_SIZE:
1748 if (img->Format != GL_LUMINANCE &&
1749 img->Format != GL_LUMINANCE_ALPHA)
1750 *params = 0;
1751 else if (img->TexFormat->LuminanceBits > 0)
1752 *params = img->TexFormat->LuminanceBits;
1753 else /* luminance probably stored as rgb texture */
1754 *params = MIN2(img->TexFormat->RedBits, img->TexFormat->GreenBits);
1755 return;
1756 case GL_TEXTURE_INDEX_SIZE_EXT:
1757 if (img->Format == GL_COLOR_INDEX)
1758 *params = img->TexFormat->IndexBits;
1759 else
1760 *params = 0;
1761 return;
1762 case GL_DEPTH_BITS:
1763 /* XXX this isn't in the GL_SGIX_depth_texture spec
1764 * but seems appropriate.
1765 */
1766 if (ctx->Extensions.SGIX_depth_texture)
1767 *params = img->TexFormat->DepthBits;
1768 else
1769 _mesa_error(ctx, GL_INVALID_ENUM,
1770 "glGetTexLevelParameter[if]v(pname)");
1771 return;
1772
1773 /* GL_ARB_texture_compression */
1774 case GL_TEXTURE_COMPRESSED_IMAGE_SIZE:
1775 if (ctx->Extensions.ARB_texture_compression) {
1776 if (img->IsCompressed && !isProxy)
1777 *params = img->CompressedSize;
1778 else
1779 _mesa_error(ctx, GL_INVALID_OPERATION,
1780 "glGetTexLevelParameter[if]v(pname)");
1781 }
1782 else {
1783 _mesa_error(ctx, GL_INVALID_ENUM,
1784 "glGetTexLevelParameter[if]v(pname)");
1785 }
1786 return;
1787 case GL_TEXTURE_COMPRESSED:
1788 if (ctx->Extensions.ARB_texture_compression) {
1789 *params = (GLint) img->IsCompressed;
1790 }
1791 else {
1792 _mesa_error(ctx, GL_INVALID_ENUM,
1793 "glGetTexLevelParameter[if]v(pname)");
1794 }
1795 return;
1796
1797 default:
1798 _mesa_error(ctx, GL_INVALID_ENUM,
1799 "glGetTexLevelParameter[if]v(pname)");
1800 }
1801 }
1802
1803
1804
1805 void GLAPIENTRY
1806 _mesa_GetTexParameterfv( GLenum target, GLenum pname, GLfloat *params )
1807 {
1808 GET_CURRENT_CONTEXT(ctx);
1809 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1810 struct gl_texture_object *obj;
1811 ASSERT_OUTSIDE_BEGIN_END(ctx);
1812
1813 obj = _mesa_select_tex_object(ctx, texUnit, target);
1814 if (!obj) {
1815 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameterfv(target)");
1816 return;
1817 }
1818
1819 switch (pname) {
1820 case GL_TEXTURE_MAG_FILTER:
1821 *params = ENUM_TO_FLOAT(obj->MagFilter);
1822 return;
1823 case GL_TEXTURE_MIN_FILTER:
1824 *params = ENUM_TO_FLOAT(obj->MinFilter);
1825 return;
1826 case GL_TEXTURE_WRAP_S:
1827 *params = ENUM_TO_FLOAT(obj->WrapS);
1828 return;
1829 case GL_TEXTURE_WRAP_T:
1830 *params = ENUM_TO_FLOAT(obj->WrapT);
1831 return;
1832 case GL_TEXTURE_WRAP_R:
1833 *params = ENUM_TO_FLOAT(obj->WrapR);
1834 return;
1835 case GL_TEXTURE_BORDER_COLOR:
1836 params[0] = CLAMP(obj->BorderColor[0], 0.0F, 1.0F);
1837 params[1] = CLAMP(obj->BorderColor[1], 0.0F, 1.0F);
1838 params[2] = CLAMP(obj->BorderColor[2], 0.0F, 1.0F);
1839 params[3] = CLAMP(obj->BorderColor[3], 0.0F, 1.0F);
1840 return;
1841 case GL_TEXTURE_RESIDENT:
1842 {
1843 GLboolean resident;
1844 if (ctx->Driver.IsTextureResident)
1845 resident = ctx->Driver.IsTextureResident(ctx, obj);
1846 else
1847 resident = GL_TRUE;
1848 *params = ENUM_TO_FLOAT(resident);
1849 }
1850 return;
1851 case GL_TEXTURE_PRIORITY:
1852 *params = obj->Priority;
1853 return;
1854 case GL_TEXTURE_MIN_LOD:
1855 *params = obj->MinLod;
1856 return;
1857 case GL_TEXTURE_MAX_LOD:
1858 *params = obj->MaxLod;
1859 return;
1860 case GL_TEXTURE_BASE_LEVEL:
1861 *params = (GLfloat) obj->BaseLevel;
1862 return;
1863 case GL_TEXTURE_MAX_LEVEL:
1864 *params = (GLfloat) obj->MaxLevel;
1865 return;
1866 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
1867 if (ctx->Extensions.EXT_texture_filter_anisotropic) {
1868 *params = obj->MaxAnisotropy;
1869 return;
1870 }
1871 break;
1872 case GL_TEXTURE_COMPARE_SGIX:
1873 if (ctx->Extensions.SGIX_shadow) {
1874 *params = (GLfloat) obj->CompareFlag;
1875 return;
1876 }
1877 break;
1878 case GL_TEXTURE_COMPARE_OPERATOR_SGIX:
1879 if (ctx->Extensions.SGIX_shadow) {
1880 *params = (GLfloat) obj->CompareOperator;
1881 return;
1882 }
1883 break;
1884 case GL_SHADOW_AMBIENT_SGIX: /* aka GL_TEXTURE_COMPARE_FAIL_VALUE_ARB */
1885 if (ctx->Extensions.SGIX_shadow_ambient) {
1886 *params = obj->ShadowAmbient;
1887 return;
1888 }
1889 break;
1890 case GL_GENERATE_MIPMAP_SGIS:
1891 if (ctx->Extensions.SGIS_generate_mipmap) {
1892 *params = (GLfloat) obj->GenerateMipmap;
1893 return;
1894 }
1895 break;
1896 case GL_TEXTURE_COMPARE_MODE_ARB:
1897 if (ctx->Extensions.ARB_shadow) {
1898 *params = (GLfloat) obj->CompareMode;
1899 return;
1900 }
1901 break;
1902 case GL_TEXTURE_COMPARE_FUNC_ARB:
1903 if (ctx->Extensions.ARB_shadow) {
1904 *params = (GLfloat) obj->CompareFunc;
1905 return;
1906 }
1907 break;
1908 case GL_DEPTH_TEXTURE_MODE_ARB:
1909 if (ctx->Extensions.ARB_depth_texture) {
1910 *params = (GLfloat) obj->DepthMode;
1911 return;
1912 }
1913 break;
1914 case GL_TEXTURE_LOD_BIAS:
1915 if (ctx->Extensions.EXT_texture_lod_bias) {
1916 *params = obj->LodBias;
1917 break;
1918 }
1919 break;
1920 default:
1921 ; /* silence warnings */
1922 }
1923 /* If we get here, pname was an unrecognized enum */
1924 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameterfv(pname=0x%x)",
1925 pname);
1926 }
1927
1928
1929 void GLAPIENTRY
1930 _mesa_GetTexParameteriv( GLenum target, GLenum pname, GLint *params )
1931 {
1932 GET_CURRENT_CONTEXT(ctx);
1933 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1934 struct gl_texture_object *obj;
1935 ASSERT_OUTSIDE_BEGIN_END(ctx);
1936
1937 obj = _mesa_select_tex_object(ctx, texUnit, target);
1938 if (!obj) {
1939 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameteriv(target)");
1940 return;
1941 }
1942
1943 switch (pname) {
1944 case GL_TEXTURE_MAG_FILTER:
1945 *params = (GLint) obj->MagFilter;
1946 case GL_TEXTURE_LOD_BIAS:
1947 if (ctx->Extensions.EXT_texture_lod_bias) {
1948 *params = (GLint) obj->LodBias;
1949 break;
1950 }
1951 break;
1952 return;
1953 case GL_TEXTURE_MIN_FILTER:
1954 *params = (GLint) obj->MinFilter;
1955 return;
1956 case GL_TEXTURE_WRAP_S:
1957 *params = (GLint) obj->WrapS;
1958 return;
1959 case GL_TEXTURE_WRAP_T:
1960 *params = (GLint) obj->WrapT;
1961 return;
1962 case GL_TEXTURE_WRAP_R:
1963 *params = (GLint) obj->WrapR;
1964 return;
1965 case GL_TEXTURE_BORDER_COLOR:
1966 {
1967 GLfloat b[4];
1968 b[0] = CLAMP(obj->BorderColor[0], 0.0F, 1.0F);
1969 b[1] = CLAMP(obj->BorderColor[1], 0.0F, 1.0F);
1970 b[2] = CLAMP(obj->BorderColor[2], 0.0F, 1.0F);
1971 b[3] = CLAMP(obj->BorderColor[3], 0.0F, 1.0F);
1972 params[0] = FLOAT_TO_INT(b[0]);
1973 params[1] = FLOAT_TO_INT(b[1]);
1974 params[2] = FLOAT_TO_INT(b[2]);
1975 params[3] = FLOAT_TO_INT(b[3]);
1976 }
1977 return;
1978 case GL_TEXTURE_RESIDENT:
1979 {
1980 GLboolean resident;
1981 if (ctx->Driver.IsTextureResident)
1982 resident = ctx->Driver.IsTextureResident(ctx, obj);
1983 else
1984 resident = GL_TRUE;
1985 *params = (GLint) resident;
1986 }
1987 return;
1988 case GL_TEXTURE_PRIORITY:
1989 *params = FLOAT_TO_INT(obj->Priority);
1990 return;
1991 case GL_TEXTURE_MIN_LOD:
1992 *params = (GLint) obj->MinLod;
1993 return;
1994 case GL_TEXTURE_MAX_LOD:
1995 *params = (GLint) obj->MaxLod;
1996 return;
1997 case GL_TEXTURE_BASE_LEVEL:
1998 *params = obj->BaseLevel;
1999 return;
2000 case GL_TEXTURE_MAX_LEVEL:
2001 *params = obj->MaxLevel;
2002 return;
2003 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
2004 if (ctx->Extensions.EXT_texture_filter_anisotropic) {
2005 *params = (GLint) obj->MaxAnisotropy;
2006 return;
2007 }
2008 break;
2009 case GL_TEXTURE_COMPARE_SGIX:
2010 if (ctx->Extensions.SGIX_shadow) {
2011 *params = (GLint) obj->CompareFlag;
2012 return;
2013 }
2014 break;
2015 case GL_TEXTURE_COMPARE_OPERATOR_SGIX:
2016 if (ctx->Extensions.SGIX_shadow) {
2017 *params = (GLint) obj->CompareOperator;
2018 return;
2019 }
2020 break;
2021 case GL_SHADOW_AMBIENT_SGIX: /* aka GL_TEXTURE_COMPARE_FAIL_VALUE_ARB */
2022 if (ctx->Extensions.SGIX_shadow_ambient) {
2023 *params = (GLint) FLOAT_TO_INT(obj->ShadowAmbient);
2024 return;
2025 }
2026 break;
2027 case GL_GENERATE_MIPMAP_SGIS:
2028 if (ctx->Extensions.SGIS_generate_mipmap) {
2029 *params = (GLint) obj->GenerateMipmap;
2030 return;
2031 }
2032 break;
2033 case GL_TEXTURE_COMPARE_MODE_ARB:
2034 if (ctx->Extensions.ARB_shadow) {
2035 *params = (GLint) obj->CompareMode;
2036 return;
2037 }
2038 break;
2039 case GL_TEXTURE_COMPARE_FUNC_ARB:
2040 if (ctx->Extensions.ARB_shadow) {
2041 *params = (GLint) obj->CompareFunc;
2042 return;
2043 }
2044 break;
2045 case GL_DEPTH_TEXTURE_MODE_ARB:
2046 if (ctx->Extensions.ARB_depth_texture) {
2047 *params = (GLint) obj->DepthMode;
2048 return;
2049 }
2050 break;
2051 default:
2052 ; /* silence warnings */
2053 }
2054 /* If we get here, pname was an unrecognized enum */
2055 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameteriv(pname=0x%x)", pname);
2056 }
2057
2058
2059
2060
2061 /**********************************************************************/
2062 /* Texture Coord Generation */
2063 /**********************************************************************/
2064
2065 #if FEATURE_texgen
2066 void GLAPIENTRY
2067 _mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
2068 {
2069 GET_CURRENT_CONTEXT(ctx);
2070 GLuint tUnit = ctx->Texture.CurrentUnit;
2071 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[tUnit];
2072 ASSERT_OUTSIDE_BEGIN_END(ctx);
2073
2074 if (MESA_VERBOSE&(VERBOSE_API|VERBOSE_TEXTURE))
2075 _mesa_debug(ctx, "glTexGen %s %s %.1f(%s)...\n",
2076 _mesa_lookup_enum_by_nr(coord),
2077 _mesa_lookup_enum_by_nr(pname),
2078 *params,
2079 _mesa_lookup_enum_by_nr((GLenum) (GLint) *params));
2080
2081 switch (coord) {
2082 case GL_S:
2083 if (pname==GL_TEXTURE_GEN_MODE) {
2084 GLenum mode = (GLenum) (GLint) *params;
2085 GLuint bits;
2086 switch (mode) {
2087 case GL_OBJECT_LINEAR:
2088 bits = TEXGEN_OBJ_LINEAR;
2089 break;
2090 case GL_EYE_LINEAR:
2091 bits = TEXGEN_EYE_LINEAR;
2092 break;
2093 case GL_REFLECTION_MAP_NV:
2094 bits = TEXGEN_REFLECTION_MAP_NV;
2095 break;
2096 case GL_NORMAL_MAP_NV:
2097 bits = TEXGEN_NORMAL_MAP_NV;
2098 break;
2099 case GL_SPHERE_MAP:
2100 bits = TEXGEN_SPHERE_MAP;
2101 break;
2102 default:
2103 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(param)" );
2104 return;
2105 }
2106 if (texUnit->GenModeS == mode)
2107 return;
2108 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2109 texUnit->GenModeS = mode;
2110 texUnit->_GenBitS = bits;
2111 }
2112 else if (pname==GL_OBJECT_PLANE) {
2113 if (TEST_EQ_4V(texUnit->ObjectPlaneS, params))
2114 return;
2115 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2116 texUnit->ObjectPlaneS[0] = params[0];
2117 texUnit->ObjectPlaneS[1] = params[1];
2118 texUnit->ObjectPlaneS[2] = params[2];
2119 texUnit->ObjectPlaneS[3] = params[3];
2120 }
2121 else if (pname==GL_EYE_PLANE) {
2122 GLfloat tmp[4];
2123
2124 /* Transform plane equation by the inverse modelview matrix */
2125 if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
2126 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
2127 }
2128 _mesa_transform_vector( tmp, params, ctx->ModelviewMatrixStack.Top->inv );
2129 if (TEST_EQ_4V(texUnit->EyePlaneS, tmp))
2130 return;
2131 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2132 COPY_4FV(texUnit->EyePlaneS, tmp);
2133 }
2134 else {
2135 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(pname)" );
2136 return;
2137 }
2138 break;
2139 case GL_T:
2140 if (pname==GL_TEXTURE_GEN_MODE) {
2141 GLenum mode = (GLenum) (GLint) *params;
2142 GLuint bitt;
2143 switch (mode) {
2144 case GL_OBJECT_LINEAR:
2145 bitt = TEXGEN_OBJ_LINEAR;
2146 break;
2147 case GL_EYE_LINEAR:
2148 bitt = TEXGEN_EYE_LINEAR;
2149 break;
2150 case GL_REFLECTION_MAP_NV:
2151 bitt = TEXGEN_REFLECTION_MAP_NV;
2152 break;
2153 case GL_NORMAL_MAP_NV:
2154 bitt = TEXGEN_NORMAL_MAP_NV;
2155 break;
2156 case GL_SPHERE_MAP:
2157 bitt = TEXGEN_SPHERE_MAP;
2158 break;
2159 default:
2160 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(param)" );
2161 return;
2162 }
2163 if (texUnit->GenModeT == mode)
2164 return;
2165 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2166 texUnit->GenModeT = mode;
2167 texUnit->_GenBitT = bitt;
2168 }
2169 else if (pname==GL_OBJECT_PLANE) {
2170 if (TEST_EQ_4V(texUnit->ObjectPlaneT, params))
2171 return;
2172 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2173 texUnit->ObjectPlaneT[0] = params[0];
2174 texUnit->ObjectPlaneT[1] = params[1];
2175 texUnit->ObjectPlaneT[2] = params[2];
2176 texUnit->ObjectPlaneT[3] = params[3];
2177 }
2178 else if (pname==GL_EYE_PLANE) {
2179 GLfloat tmp[4];
2180 /* Transform plane equation by the inverse modelview matrix */
2181 if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
2182 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
2183 }
2184 _mesa_transform_vector( tmp, params, ctx->ModelviewMatrixStack.Top->inv );
2185 if (TEST_EQ_4V(texUnit->EyePlaneT, tmp))
2186 return;
2187 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2188 COPY_4FV(texUnit->EyePlaneT, tmp);
2189 }
2190 else {
2191 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(pname)" );
2192 return;
2193 }
2194 break;
2195 case GL_R:
2196 if (pname==GL_TEXTURE_GEN_MODE) {
2197 GLenum mode = (GLenum) (GLint) *params;
2198 GLuint bitr;
2199 switch (mode) {
2200 case GL_OBJECT_LINEAR:
2201 bitr = TEXGEN_OBJ_LINEAR;
2202 break;
2203 case GL_REFLECTION_MAP_NV:
2204 bitr = TEXGEN_REFLECTION_MAP_NV;
2205 break;
2206 case GL_NORMAL_MAP_NV:
2207 bitr = TEXGEN_NORMAL_MAP_NV;
2208 break;
2209 case GL_EYE_LINEAR:
2210 bitr = TEXGEN_EYE_LINEAR;
2211 break;
2212 default:
2213 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(param)" );
2214 return;
2215 }
2216 if (texUnit->GenModeR == mode)
2217 return;
2218 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2219 texUnit->GenModeR = mode;
2220 texUnit->_GenBitR = bitr;
2221 }
2222 else if (pname==GL_OBJECT_PLANE) {
2223 if (TEST_EQ_4V(texUnit->ObjectPlaneR, params))
2224 return;
2225 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2226 texUnit->ObjectPlaneR[0] = params[0];
2227 texUnit->ObjectPlaneR[1] = params[1];
2228 texUnit->ObjectPlaneR[2] = params[2];
2229 texUnit->ObjectPlaneR[3] = params[3];
2230 }
2231 else if (pname==GL_EYE_PLANE) {
2232 GLfloat tmp[4];
2233 /* Transform plane equation by the inverse modelview matrix */
2234 if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
2235 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
2236 }
2237 _mesa_transform_vector( tmp, params, ctx->ModelviewMatrixStack.Top->inv );
2238 if (TEST_EQ_4V(texUnit->EyePlaneR, tmp))
2239 return;
2240 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2241 COPY_4FV(texUnit->EyePlaneR, tmp);
2242 }
2243 else {
2244 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(pname)" );
2245 return;
2246 }
2247 break;
2248 case GL_Q:
2249 if (pname==GL_TEXTURE_GEN_MODE) {
2250 GLenum mode = (GLenum) (GLint) *params;
2251 GLuint bitq;
2252 switch (mode) {
2253 case GL_OBJECT_LINEAR:
2254 bitq = TEXGEN_OBJ_LINEAR;
2255 break;
2256 case GL_EYE_LINEAR:
2257 bitq = TEXGEN_EYE_LINEAR;
2258 break;
2259 default:
2260 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(param)" );
2261 return;
2262 }
2263 if (texUnit->GenModeQ == mode)
2264 return;
2265 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2266 texUnit->GenModeQ = mode;
2267 texUnit->_GenBitQ = bitq;
2268 }
2269 else if (pname==GL_OBJECT_PLANE) {
2270 if (TEST_EQ_4V(texUnit->ObjectPlaneQ, params))
2271 return;
2272 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2273 texUnit->ObjectPlaneQ[0] = params[0];
2274 texUnit->ObjectPlaneQ[1] = params[1];
2275 texUnit->ObjectPlaneQ[2] = params[2];
2276 texUnit->ObjectPlaneQ[3] = params[3];
2277 }
2278 else if (pname==GL_EYE_PLANE) {
2279 GLfloat tmp[4];
2280 /* Transform plane equation by the inverse modelview matrix */
2281 if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
2282 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
2283 }
2284 _mesa_transform_vector( tmp, params, ctx->ModelviewMatrixStack.Top->inv );
2285 if (TEST_EQ_4V(texUnit->EyePlaneQ, tmp))
2286 return;
2287 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2288 COPY_4FV(texUnit->EyePlaneQ, tmp);
2289 }
2290 else {
2291 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(pname)" );
2292 return;
2293 }
2294 break;
2295 default:
2296 _mesa_error( ctx, GL_INVALID_ENUM, "glTexGenfv(coord)" );
2297 return;
2298 }
2299
2300 if (ctx->Driver.TexGen)
2301 ctx->Driver.TexGen( ctx, coord, pname, params );
2302 }
2303
2304
2305 void GLAPIENTRY
2306 _mesa_TexGeniv(GLenum coord, GLenum pname, const GLint *params )
2307 {
2308 GLfloat p[4];
2309 p[0] = (GLfloat) params[0];
2310 if (pname == GL_TEXTURE_GEN_MODE) {
2311 p[1] = p[2] = p[3] = 0.0F;
2312 }
2313 else {
2314 p[1] = (GLfloat) params[1];
2315 p[2] = (GLfloat) params[2];
2316 p[3] = (GLfloat) params[3];
2317 }
2318 _mesa_TexGenfv(coord, pname, p);
2319 }
2320
2321
2322 void GLAPIENTRY
2323 _mesa_TexGend(GLenum coord, GLenum pname, GLdouble param )
2324 {
2325 GLfloat p = (GLfloat) param;
2326 _mesa_TexGenfv( coord, pname, &p );
2327 }
2328
2329
2330 void GLAPIENTRY
2331 _mesa_TexGendv(GLenum coord, GLenum pname, const GLdouble *params )
2332 {
2333 GLfloat p[4];
2334 p[0] = (GLfloat) params[0];
2335 if (pname == GL_TEXTURE_GEN_MODE) {
2336 p[1] = p[2] = p[3] = 0.0F;
2337 }
2338 else {
2339 p[1] = (GLfloat) params[1];
2340 p[2] = (GLfloat) params[2];
2341 p[3] = (GLfloat) params[3];
2342 }
2343 _mesa_TexGenfv( coord, pname, p );
2344 }
2345
2346
2347 void GLAPIENTRY
2348 _mesa_TexGenf( GLenum coord, GLenum pname, GLfloat param )
2349 {
2350 _mesa_TexGenfv(coord, pname, &param);
2351 }
2352
2353
2354 void GLAPIENTRY
2355 _mesa_TexGeni( GLenum coord, GLenum pname, GLint param )
2356 {
2357 _mesa_TexGeniv( coord, pname, &param );
2358 }
2359
2360
2361
2362 void GLAPIENTRY
2363 _mesa_GetTexGendv( GLenum coord, GLenum pname, GLdouble *params )
2364 {
2365 GET_CURRENT_CONTEXT(ctx);
2366 GLuint tUnit = ctx->Texture.CurrentUnit;
2367 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[tUnit];
2368 ASSERT_OUTSIDE_BEGIN_END(ctx);
2369
2370 switch (coord) {
2371 case GL_S:
2372 if (pname==GL_TEXTURE_GEN_MODE) {
2373 params[0] = ENUM_TO_DOUBLE(texUnit->GenModeS);
2374 }
2375 else if (pname==GL_OBJECT_PLANE) {
2376 COPY_4V( params, texUnit->ObjectPlaneS );
2377 }
2378 else if (pname==GL_EYE_PLANE) {
2379 COPY_4V( params, texUnit->EyePlaneS );
2380 }
2381 else {
2382 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGendv(pname)" );
2383 return;
2384 }
2385 break;
2386 case GL_T:
2387 if (pname==GL_TEXTURE_GEN_MODE) {
2388 params[0] = ENUM_TO_DOUBLE(texUnit->GenModeT);
2389 }
2390 else if (pname==GL_OBJECT_PLANE) {
2391 COPY_4V( params, texUnit->ObjectPlaneT );
2392 }
2393 else if (pname==GL_EYE_PLANE) {
2394 COPY_4V( params, texUnit->EyePlaneT );
2395 }
2396 else {
2397 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGendv(pname)" );
2398 return;
2399 }
2400 break;
2401 case GL_R:
2402 if (pname==GL_TEXTURE_GEN_MODE) {
2403 params[0] = ENUM_TO_DOUBLE(texUnit->GenModeR);
2404 }
2405 else if (pname==GL_OBJECT_PLANE) {
2406 COPY_4V( params, texUnit->ObjectPlaneR );
2407 }
2408 else if (pname==GL_EYE_PLANE) {
2409 COPY_4V( params, texUnit->EyePlaneR );
2410 }
2411 else {
2412 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGendv(pname)" );
2413 return;
2414 }
2415 break;
2416 case GL_Q:
2417 if (pname==GL_TEXTURE_GEN_MODE) {
2418 params[0] = ENUM_TO_DOUBLE(texUnit->GenModeQ);
2419 }
2420 else if (pname==GL_OBJECT_PLANE) {
2421 COPY_4V( params, texUnit->ObjectPlaneQ );
2422 }
2423 else if (pname==GL_EYE_PLANE) {
2424 COPY_4V( params, texUnit->EyePlaneQ );
2425 }
2426 else {
2427 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGendv(pname)" );
2428 return;
2429 }
2430 break;
2431 default:
2432 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGendv(coord)" );
2433 return;
2434 }
2435 }
2436
2437
2438
2439 void GLAPIENTRY
2440 _mesa_GetTexGenfv( GLenum coord, GLenum pname, GLfloat *params )
2441 {
2442 GET_CURRENT_CONTEXT(ctx);
2443 GLuint tUnit = ctx->Texture.CurrentUnit;
2444 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[tUnit];
2445 ASSERT_OUTSIDE_BEGIN_END(ctx);
2446
2447 switch (coord) {
2448 case GL_S:
2449 if (pname==GL_TEXTURE_GEN_MODE) {
2450 params[0] = ENUM_TO_FLOAT(texUnit->GenModeS);
2451 }
2452 else if (pname==GL_OBJECT_PLANE) {
2453 COPY_4V( params, texUnit->ObjectPlaneS );
2454 }
2455 else if (pname==GL_EYE_PLANE) {
2456 COPY_4V( params, texUnit->EyePlaneS );
2457 }
2458 else {
2459 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGenfv(pname)" );
2460 return;
2461 }
2462 break;
2463 case GL_T:
2464 if (pname==GL_TEXTURE_GEN_MODE) {
2465 params[0] = ENUM_TO_FLOAT(texUnit->GenModeT);
2466 }
2467 else if (pname==GL_OBJECT_PLANE) {
2468 COPY_4V( params, texUnit->ObjectPlaneT );
2469 }
2470 else if (pname==GL_EYE_PLANE) {
2471 COPY_4V( params, texUnit->EyePlaneT );
2472 }
2473 else {
2474 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGenfv(pname)" );
2475 return;
2476 }
2477 break;
2478 case GL_R:
2479 if (pname==GL_TEXTURE_GEN_MODE) {
2480 params[0] = ENUM_TO_FLOAT(texUnit->GenModeR);
2481 }
2482 else if (pname==GL_OBJECT_PLANE) {
2483 COPY_4V( params, texUnit->ObjectPlaneR );
2484 }
2485 else if (pname==GL_EYE_PLANE) {
2486 COPY_4V( params, texUnit->EyePlaneR );
2487 }
2488 else {
2489 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGenfv(pname)" );
2490 return;
2491 }
2492 break;
2493 case GL_Q:
2494 if (pname==GL_TEXTURE_GEN_MODE) {
2495 params[0] = ENUM_TO_FLOAT(texUnit->GenModeQ);
2496 }
2497 else if (pname==GL_OBJECT_PLANE) {
2498 COPY_4V( params, texUnit->ObjectPlaneQ );
2499 }
2500 else if (pname==GL_EYE_PLANE) {
2501 COPY_4V( params, texUnit->EyePlaneQ );
2502 }
2503 else {
2504 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGenfv(pname)" );
2505 return;
2506 }
2507 break;
2508 default:
2509 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGenfv(coord)" );
2510 return;
2511 }
2512 }
2513
2514
2515
2516 void GLAPIENTRY
2517 _mesa_GetTexGeniv( GLenum coord, GLenum pname, GLint *params )
2518 {
2519 GET_CURRENT_CONTEXT(ctx);
2520 GLuint tUnit = ctx->Texture.CurrentUnit;
2521 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[tUnit];
2522 ASSERT_OUTSIDE_BEGIN_END(ctx);
2523
2524 switch (coord) {
2525 case GL_S:
2526 if (pname==GL_TEXTURE_GEN_MODE) {
2527 params[0] = texUnit->GenModeS;
2528 }
2529 else if (pname==GL_OBJECT_PLANE) {
2530 params[0] = (GLint) texUnit->ObjectPlaneS[0];
2531 params[1] = (GLint) texUnit->ObjectPlaneS[1];
2532 params[2] = (GLint) texUnit->ObjectPlaneS[2];
2533 params[3] = (GLint) texUnit->ObjectPlaneS[3];
2534 }
2535 else if (pname==GL_EYE_PLANE) {
2536 params[0] = (GLint) texUnit->EyePlaneS[0];
2537 params[1] = (GLint) texUnit->EyePlaneS[1];
2538 params[2] = (GLint) texUnit->EyePlaneS[2];
2539 params[3] = (GLint) texUnit->EyePlaneS[3];
2540 }
2541 else {
2542 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGeniv(pname)" );
2543 return;
2544 }
2545 break;
2546 case GL_T:
2547 if (pname==GL_TEXTURE_GEN_MODE) {
2548 params[0] = texUnit->GenModeT;
2549 }
2550 else if (pname==GL_OBJECT_PLANE) {
2551 params[0] = (GLint) texUnit->ObjectPlaneT[0];
2552 params[1] = (GLint) texUnit->ObjectPlaneT[1];
2553 params[2] = (GLint) texUnit->ObjectPlaneT[2];
2554 params[3] = (GLint) texUnit->ObjectPlaneT[3];
2555 }
2556 else if (pname==GL_EYE_PLANE) {
2557 params[0] = (GLint) texUnit->EyePlaneT[0];
2558 params[1] = (GLint) texUnit->EyePlaneT[1];
2559 params[2] = (GLint) texUnit->EyePlaneT[2];
2560 params[3] = (GLint) texUnit->EyePlaneT[3];
2561 }
2562 else {
2563 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGeniv(pname)" );
2564 return;
2565 }
2566 break;
2567 case GL_R:
2568 if (pname==GL_TEXTURE_GEN_MODE) {
2569 params[0] = texUnit->GenModeR;
2570 }
2571 else if (pname==GL_OBJECT_PLANE) {
2572 params[0] = (GLint) texUnit->ObjectPlaneR[0];
2573 params[1] = (GLint) texUnit->ObjectPlaneR[1];
2574 params[2] = (GLint) texUnit->ObjectPlaneR[2];
2575 params[3] = (GLint) texUnit->ObjectPlaneR[3];
2576 }
2577 else if (pname==GL_EYE_PLANE) {
2578 params[0] = (GLint) texUnit->EyePlaneR[0];
2579 params[1] = (GLint) texUnit->EyePlaneR[1];
2580 params[2] = (GLint) texUnit->EyePlaneR[2];
2581 params[3] = (GLint) texUnit->EyePlaneR[3];
2582 }
2583 else {
2584 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGeniv(pname)" );
2585 return;
2586 }
2587 break;
2588 case GL_Q:
2589 if (pname==GL_TEXTURE_GEN_MODE) {
2590 params[0] = texUnit->GenModeQ;
2591 }
2592 else if (pname==GL_OBJECT_PLANE) {
2593 params[0] = (GLint) texUnit->ObjectPlaneQ[0];
2594 params[1] = (GLint) texUnit->ObjectPlaneQ[1];
2595 params[2] = (GLint) texUnit->ObjectPlaneQ[2];
2596 params[3] = (GLint) texUnit->ObjectPlaneQ[3];
2597 }
2598 else if (pname==GL_EYE_PLANE) {
2599 params[0] = (GLint) texUnit->EyePlaneQ[0];
2600 params[1] = (GLint) texUnit->EyePlaneQ[1];
2601 params[2] = (GLint) texUnit->EyePlaneQ[2];
2602 params[3] = (GLint) texUnit->EyePlaneQ[3];
2603 }
2604 else {
2605 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGeniv(pname)" );
2606 return;
2607 }
2608 break;
2609 default:
2610 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexGeniv(coord)" );
2611 return;
2612 }
2613 }
2614 #endif
2615
2616 /* GL_ARB_multitexture */
2617 void GLAPIENTRY
2618 _mesa_ActiveTextureARB( GLenum target )
2619 {
2620 GET_CURRENT_CONTEXT(ctx);
2621 const GLuint texUnit = target - GL_TEXTURE0;
2622 ASSERT_OUTSIDE_BEGIN_END(ctx);
2623
2624 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2625 _mesa_debug(ctx, "glActiveTexture %s\n",
2626 _mesa_lookup_enum_by_nr(target));
2627
2628 /* Cater for texture unit 0 is first, therefore use >= */
2629 if (texUnit >= ctx->Const.MaxTextureUnits) {
2630 _mesa_error(ctx, GL_INVALID_ENUM, "glActiveTexture(target)");
2631 return;
2632 }
2633
2634 if (ctx->Texture.CurrentUnit == texUnit)
2635 return;
2636
2637 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
2638
2639 ctx->Texture.CurrentUnit = texUnit;
2640 if (ctx->Transform.MatrixMode == GL_TEXTURE) {
2641 /* update current stack pointer */
2642 ctx->CurrentStack = &ctx->TextureMatrixStack[texUnit];
2643 }
2644
2645 if (ctx->Driver.ActiveTexture) {
2646 (*ctx->Driver.ActiveTexture)( ctx, (GLuint) texUnit );
2647 }
2648 }
2649
2650
2651 /* GL_ARB_multitexture */
2652 void GLAPIENTRY
2653 _mesa_ClientActiveTextureARB( GLenum target )
2654 {
2655 GET_CURRENT_CONTEXT(ctx);
2656 GLuint texUnit = target - GL_TEXTURE0;
2657 ASSERT_OUTSIDE_BEGIN_END(ctx);
2658
2659 if (texUnit > ctx->Const.MaxTextureUnits) {
2660 _mesa_error(ctx, GL_INVALID_ENUM, "glClientActiveTexture(target)");
2661 return;
2662 }
2663
2664 FLUSH_VERTICES(ctx, _NEW_ARRAY);
2665 ctx->Array.ActiveTexture = texUnit;
2666 }
2667
2668
2669
2670 /**********************************************************************/
2671 /* Pixel Texgen Extensions */
2672 /**********************************************************************/
2673
2674 void GLAPIENTRY
2675 _mesa_PixelTexGenSGIX(GLenum mode)
2676 {
2677 GLenum newRgbSource, newAlphaSource;
2678 GET_CURRENT_CONTEXT(ctx);
2679 ASSERT_OUTSIDE_BEGIN_END(ctx);
2680
2681 switch (mode) {
2682 case GL_NONE:
2683 newRgbSource = GL_PIXEL_GROUP_COLOR_SGIS;
2684 newAlphaSource = GL_PIXEL_GROUP_COLOR_SGIS;
2685 break;
2686 case GL_ALPHA:
2687 newRgbSource = GL_PIXEL_GROUP_COLOR_SGIS;
2688 newAlphaSource = GL_CURRENT_RASTER_COLOR;
2689 break;
2690 case GL_RGB:
2691 newRgbSource = GL_CURRENT_RASTER_COLOR;
2692 newAlphaSource = GL_PIXEL_GROUP_COLOR_SGIS;
2693 break;
2694 case GL_RGBA:
2695 newRgbSource = GL_CURRENT_RASTER_COLOR;
2696 newAlphaSource = GL_CURRENT_RASTER_COLOR;
2697 break;
2698 default:
2699 _mesa_error(ctx, GL_INVALID_ENUM, "glPixelTexGenSGIX(mode)");
2700 return;
2701 }
2702
2703 if (newRgbSource == ctx->Pixel.FragmentRgbSource &&
2704 newAlphaSource == ctx->Pixel.FragmentAlphaSource)
2705 return;
2706
2707 FLUSH_VERTICES(ctx, _NEW_PIXEL);
2708 ctx->Pixel.FragmentRgbSource = newRgbSource;
2709 ctx->Pixel.FragmentAlphaSource = newAlphaSource;
2710 }
2711
2712
2713 void GLAPIENTRY
2714 _mesa_PixelTexGenParameterfSGIS(GLenum target, GLfloat value)
2715 {
2716 _mesa_PixelTexGenParameteriSGIS(target, (GLint) value);
2717 }
2718
2719
2720 void GLAPIENTRY
2721 _mesa_PixelTexGenParameterfvSGIS(GLenum target, const GLfloat *value)
2722 {
2723 _mesa_PixelTexGenParameteriSGIS(target, (GLint) *value);
2724 }
2725
2726
2727 void GLAPIENTRY
2728 _mesa_PixelTexGenParameteriSGIS(GLenum target, GLint value)
2729 {
2730 GET_CURRENT_CONTEXT(ctx);
2731 ASSERT_OUTSIDE_BEGIN_END(ctx);
2732
2733 if (value != GL_CURRENT_RASTER_COLOR && value != GL_PIXEL_GROUP_COLOR_SGIS) {
2734 _mesa_error(ctx, GL_INVALID_ENUM, "glPixelTexGenParameterSGIS(value)");
2735 return;
2736 }
2737
2738 switch (target) {
2739 case GL_PIXEL_FRAGMENT_RGB_SOURCE_SGIS:
2740 if (ctx->Pixel.FragmentRgbSource == (GLenum) value)
2741 return;
2742 FLUSH_VERTICES(ctx, _NEW_PIXEL);
2743 ctx->Pixel.FragmentRgbSource = (GLenum) value;
2744 break;
2745 case GL_PIXEL_FRAGMENT_ALPHA_SOURCE_SGIS:
2746 if (ctx->Pixel.FragmentAlphaSource == (GLenum) value)
2747 return;
2748 FLUSH_VERTICES(ctx, _NEW_PIXEL);
2749 ctx->Pixel.FragmentAlphaSource = (GLenum) value;
2750 break;
2751 default:
2752 _mesa_error(ctx, GL_INVALID_ENUM, "glPixelTexGenParameterSGIS(target)");
2753 return;
2754 }
2755 }
2756
2757
2758 void GLAPIENTRY
2759 _mesa_PixelTexGenParameterivSGIS(GLenum target, const GLint *value)
2760 {
2761 _mesa_PixelTexGenParameteriSGIS(target, *value);
2762 }
2763
2764
2765 void GLAPIENTRY
2766 _mesa_GetPixelTexGenParameterfvSGIS(GLenum target, GLfloat *value)
2767 {
2768 GET_CURRENT_CONTEXT(ctx);
2769 ASSERT_OUTSIDE_BEGIN_END(ctx);
2770
2771 if (target == GL_PIXEL_FRAGMENT_RGB_SOURCE_SGIS) {
2772 *value = (GLfloat) ctx->Pixel.FragmentRgbSource;
2773 }
2774 else if (target == GL_PIXEL_FRAGMENT_ALPHA_SOURCE_SGIS) {
2775 *value = (GLfloat) ctx->Pixel.FragmentAlphaSource;
2776 }
2777 else {
2778 _mesa_error(ctx, GL_INVALID_ENUM, "glGetPixelTexGenParameterfvSGIS(target)");
2779 }
2780 }
2781
2782
2783 void GLAPIENTRY
2784 _mesa_GetPixelTexGenParameterivSGIS(GLenum target, GLint *value)
2785 {
2786 GET_CURRENT_CONTEXT(ctx);
2787 ASSERT_OUTSIDE_BEGIN_END(ctx);
2788
2789 if (target == GL_PIXEL_FRAGMENT_RGB_SOURCE_SGIS) {
2790 *value = (GLint) ctx->Pixel.FragmentRgbSource;
2791 }
2792 else if (target == GL_PIXEL_FRAGMENT_ALPHA_SOURCE_SGIS) {
2793 *value = (GLint) ctx->Pixel.FragmentAlphaSource;
2794 }
2795 else {
2796 _mesa_error(ctx, GL_INVALID_ENUM, "glGetPixelTexGenParameterivSGIS(target)");
2797 }
2798 }
2799
2800
2801
2802 /**********************************************************************/
2803 /***** State management *****/
2804 /**********************************************************************/
2805
2806
2807 /**
2808 * \note This routine refers to derived texture attribute values to
2809 * compute the ENABLE_TEXMAT flags, but is only called on
2810 * _NEW_TEXTURE_MATRIX. On changes to _NEW_TEXTURE, the ENABLE_TEXMAT
2811 * flags are updated by _mesa_update_textures(), below.
2812 *
2813 * \param ctx GL context.
2814 */
2815 static void
2816 update_texture_matrices( GLcontext *ctx )
2817 {
2818 GLuint i;
2819
2820 ctx->Texture._TexMatEnabled = 0;
2821
2822 for (i=0; i < ctx->Const.MaxTextureUnits; i++) {
2823 if (ctx->TextureMatrixStack[i].Top->flags & MAT_DIRTY) {
2824 _math_matrix_analyse( ctx->TextureMatrixStack[i].Top );
2825
2826 if (ctx->Texture.Unit[i]._ReallyEnabled &&
2827 ctx->TextureMatrixStack[i].Top->type != MATRIX_IDENTITY)
2828 ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(i);
2829
2830 if (ctx->Driver.TextureMatrix)
2831 ctx->Driver.TextureMatrix( ctx, i, ctx->TextureMatrixStack[i].Top);
2832 }
2833 }
2834 }
2835
2836
2837
2838
2839 /**
2840 * \note This routine refers to derived texture matrix values to
2841 * compute the ENABLE_TEXMAT flags, but is only called on
2842 * _NEW_TEXTURE. On changes to _NEW_TEXTURE_MATRIX, the ENABLE_TEXMAT
2843 * flags are updated by _mesa_update_texture_matrices, above.
2844 *
2845 * \param ctx GL context.
2846 */
2847 static void
2848 update_texture_state( GLcontext *ctx )
2849 {
2850 GLuint unit;
2851
2852 ctx->NewState |= _NEW_TEXTURE; /* TODO: only set this if there are
2853 * actual changes.
2854 */
2855
2856 ctx->Texture._EnabledUnits = 0;
2857 ctx->Texture._GenFlags = 0;
2858 ctx->Texture._TexMatEnabled = 0;
2859 ctx->Texture._TexGenEnabled = 0;
2860
2861 /* Update texture unit state.
2862 * XXX this loop should probably be broken into separate loops for
2863 * texture coord units and texture image units.
2864 */
2865 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
2866 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
2867 GLuint enableBits;
2868
2869 texUnit->_Current = NULL;
2870 texUnit->_ReallyEnabled = 0;
2871 texUnit->_GenFlags = 0;
2872
2873 /* Get the bitmask of texture enables */
2874 if (ctx->FragmentProgram.Enabled && ctx->FragmentProgram.Current) {
2875 enableBits = ctx->FragmentProgram.Current->TexturesUsed[unit];
2876 }
2877 else {
2878 if (!texUnit->Enabled)
2879 continue;
2880 enableBits = texUnit->Enabled;
2881 }
2882
2883 /* Look for the highest-priority texture target that's enabled and
2884 * complete. That's the one we'll use for texturing. If we're using
2885 * a fragment program we're guaranteed that bitcount(enabledBits) <= 1.
2886 */
2887 if (enableBits & TEXTURE_CUBE_BIT) {
2888 struct gl_texture_object *texObj = texUnit->CurrentCubeMap;
2889 if (!texObj->Complete) {
2890 _mesa_test_texobj_completeness(ctx, texObj);
2891 }
2892 if (texObj->Complete) {
2893 texUnit->_ReallyEnabled = TEXTURE_CUBE_BIT;
2894 texUnit->_Current = texObj;
2895 }
2896 }
2897
2898 if (!texUnit->_ReallyEnabled && (enableBits & TEXTURE_3D_BIT)) {
2899 struct gl_texture_object *texObj = texUnit->Current3D;
2900 if (!texObj->Complete) {
2901 _mesa_test_texobj_completeness(ctx, texObj);
2902 }
2903 if (texObj->Complete) {
2904 texUnit->_ReallyEnabled = TEXTURE_3D_BIT;
2905 texUnit->_Current = texObj;
2906 }
2907 }
2908
2909 if (!texUnit->_ReallyEnabled && (enableBits & TEXTURE_RECT_BIT)) {
2910 struct gl_texture_object *texObj = texUnit->CurrentRect;
2911 if (!texObj->Complete) {
2912 _mesa_test_texobj_completeness(ctx, texObj);
2913 }
2914 if (texObj->Complete) {
2915 texUnit->_ReallyEnabled = TEXTURE_RECT_BIT;
2916 texUnit->_Current = texObj;
2917 }
2918 }
2919
2920 if (!texUnit->_ReallyEnabled && (enableBits & TEXTURE_2D_BIT)) {
2921 struct gl_texture_object *texObj = texUnit->Current2D;
2922 if (!texObj->Complete) {
2923 _mesa_test_texobj_completeness(ctx, texObj);
2924 }
2925 if (texObj->Complete) {
2926 texUnit->_ReallyEnabled = TEXTURE_2D_BIT;
2927 texUnit->_Current = texObj;
2928 }
2929 }
2930
2931 if (!texUnit->_ReallyEnabled && (enableBits & TEXTURE_1D_BIT)) {
2932 struct gl_texture_object *texObj = texUnit->Current1D;
2933 if (!texObj->Complete) {
2934 _mesa_test_texobj_completeness(ctx, texObj);
2935 }
2936 if (texObj->Complete) {
2937 texUnit->_ReallyEnabled = TEXTURE_1D_BIT;
2938 texUnit->_Current = texObj;
2939 }
2940 }
2941
2942 if (!texUnit->_ReallyEnabled) {
2943 continue;
2944 }
2945
2946 if (texUnit->_ReallyEnabled)
2947 ctx->Texture._EnabledUnits |= (1 << unit);
2948
2949 if (texUnit->EnvMode == GL_COMBINE) {
2950 texUnit->_CurrentCombine = & texUnit->Combine;
2951 }
2952 else {
2953 GLenum format = texUnit->_Current->Image[0][0]->Format;
2954 if (format == GL_COLOR_INDEX) {
2955 format = GL_RGBA; /* a bit of a hack */
2956 }
2957 else if (format == GL_DEPTH_COMPONENT) {
2958 format = texUnit->_Current->DepthMode;
2959 }
2960 calculate_derived_texenv(&texUnit->_EnvMode, texUnit->EnvMode, format);
2961 texUnit->_CurrentCombine = & texUnit->_EnvMode;
2962 }
2963
2964 switch (texUnit->_CurrentCombine->ModeRGB) {
2965 case GL_REPLACE:
2966 texUnit->_CurrentCombine->_NumArgsRGB = 1;
2967 break;
2968 case GL_MODULATE:
2969 case GL_ADD:
2970 case GL_ADD_SIGNED:
2971 case GL_SUBTRACT:
2972 case GL_DOT3_RGB:
2973 case GL_DOT3_RGBA:
2974 case GL_DOT3_RGB_EXT:
2975 case GL_DOT3_RGBA_EXT:
2976 texUnit->_CurrentCombine->_NumArgsRGB = 2;
2977 break;
2978 case GL_INTERPOLATE:
2979 case GL_MODULATE_ADD_ATI:
2980 case GL_MODULATE_SIGNED_ADD_ATI:
2981 case GL_MODULATE_SUBTRACT_ATI:
2982 texUnit->_CurrentCombine->_NumArgsRGB = 3;
2983 break;
2984 default:
2985 texUnit->_CurrentCombine->_NumArgsRGB = 0;
2986 _mesa_problem(ctx, "invalid RGB combine mode in update_texture_state");
2987 return;
2988 }
2989
2990 switch (texUnit->_CurrentCombine->ModeA) {
2991 case GL_REPLACE:
2992 texUnit->_CurrentCombine->_NumArgsA = 1;
2993 break;
2994 case GL_MODULATE:
2995 case GL_ADD:
2996 case GL_ADD_SIGNED:
2997 case GL_SUBTRACT:
2998 texUnit->_CurrentCombine->_NumArgsA = 2;
2999 break;
3000 case GL_INTERPOLATE:
3001 case GL_MODULATE_ADD_ATI:
3002 case GL_MODULATE_SIGNED_ADD_ATI:
3003 case GL_MODULATE_SUBTRACT_ATI:
3004 texUnit->_CurrentCombine->_NumArgsA = 3;
3005 break;
3006 default:
3007 texUnit->_CurrentCombine->_NumArgsA = 0;
3008 _mesa_problem(ctx, "invalid Alpha combine mode in update_texture_state");
3009 break;
3010 }
3011
3012 if (texUnit->TexGenEnabled) {
3013 if (texUnit->TexGenEnabled & S_BIT) {
3014 texUnit->_GenFlags |= texUnit->_GenBitS;
3015 }
3016 if (texUnit->TexGenEnabled & T_BIT) {
3017 texUnit->_GenFlags |= texUnit->_GenBitT;
3018 }
3019 if (texUnit->TexGenEnabled & Q_BIT) {
3020 texUnit->_GenFlags |= texUnit->_GenBitQ;
3021 }
3022 if (texUnit->TexGenEnabled & R_BIT) {
3023 texUnit->_GenFlags |= texUnit->_GenBitR;
3024 }
3025
3026 ctx->Texture._TexGenEnabled |= ENABLE_TEXGEN(unit);
3027 ctx->Texture._GenFlags |= texUnit->_GenFlags;
3028 }
3029
3030 if (ctx->TextureMatrixStack[unit].Top->type != MATRIX_IDENTITY)
3031 ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(unit);
3032 }
3033
3034 ctx->Texture._EnabledCoordUnits = ctx->Texture._EnabledUnits;
3035 /* Fragment programs may need texture coordinates but not the
3036 * corresponding texture images.
3037 */
3038 if (ctx->FragmentProgram.Enabled && ctx->FragmentProgram.Current) {
3039 ctx->Texture._EnabledCoordUnits |=
3040 (ctx->FragmentProgram.Current->InputsRead >> FRAG_ATTRIB_TEX0);
3041 }
3042 }
3043
3044
3045 void _mesa_update_texture( GLcontext *ctx, GLuint new_state )
3046 {
3047 if (new_state & _NEW_TEXTURE_MATRIX)
3048 update_texture_matrices( ctx );
3049
3050 if (new_state & (_NEW_TEXTURE | _NEW_PROGRAM))
3051 update_texture_state( ctx );
3052 }
3053
3054 /**********************************************************************/
3055 /***** Initialization *****/
3056 /**********************************************************************/
3057
3058 /**
3059 * Allocate the proxy textures for the given context.
3060 *
3061 * \param ctx the context to allocate proxies for.
3062 *
3063 * \return GL_TRUE on success, or GL_FALSE on failure
3064 *
3065 * If run out of memory part way through the allocations, clean up and return
3066 * GL_FALSE.
3067 */
3068 static GLboolean
3069 alloc_proxy_textures( GLcontext *ctx )
3070 {
3071 ctx->Texture.Proxy1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
3072 if (!ctx->Texture.Proxy1D)
3073 goto cleanup;
3074
3075 ctx->Texture.Proxy2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
3076 if (!ctx->Texture.Proxy2D)
3077 goto cleanup;
3078
3079 ctx->Texture.Proxy3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
3080 if (!ctx->Texture.Proxy3D)
3081 goto cleanup;
3082
3083 ctx->Texture.ProxyCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
3084 if (!ctx->Texture.ProxyCubeMap)
3085 goto cleanup;
3086
3087 ctx->Texture.ProxyRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
3088 if (!ctx->Texture.ProxyRect)
3089 goto cleanup;
3090
3091 return GL_TRUE;
3092
3093 cleanup:
3094 if (ctx->Texture.Proxy1D)
3095 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy1D);
3096 if (ctx->Texture.Proxy2D)
3097 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy2D);
3098 if (ctx->Texture.Proxy3D)
3099 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy3D);
3100 if (ctx->Texture.ProxyCubeMap)
3101 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.ProxyCubeMap);
3102 if (ctx->Texture.ProxyRect)
3103 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.ProxyRect);
3104 return GL_FALSE;
3105 }
3106
3107
3108 /**
3109 * Initialize a texture unit.
3110 *
3111 * \param ctx GL context.
3112 * \param unit texture unit number to be initialized.
3113 */
3114 static void
3115 init_texture_unit( GLcontext *ctx, GLuint unit )
3116 {
3117 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
3118
3119 texUnit->EnvMode = GL_MODULATE;
3120 ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
3121
3122 texUnit->Combine = default_combine_state;
3123 texUnit->_EnvMode = default_combine_state;
3124 texUnit->_CurrentCombine = & texUnit->_EnvMode;
3125
3126 texUnit->TexGenEnabled = 0;
3127 texUnit->GenModeS = GL_EYE_LINEAR;
3128 texUnit->GenModeT = GL_EYE_LINEAR;
3129 texUnit->GenModeR = GL_EYE_LINEAR;
3130 texUnit->GenModeQ = GL_EYE_LINEAR;
3131 texUnit->_GenBitS = TEXGEN_EYE_LINEAR;
3132 texUnit->_GenBitT = TEXGEN_EYE_LINEAR;
3133 texUnit->_GenBitR = TEXGEN_EYE_LINEAR;
3134 texUnit->_GenBitQ = TEXGEN_EYE_LINEAR;
3135
3136 /* Yes, these plane coefficients are correct! */
3137 ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
3138 ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
3139 ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
3140 ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
3141 ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
3142 ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
3143 ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
3144 ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
3145
3146 texUnit->Current1D = ctx->Shared->Default1D;
3147 texUnit->Current2D = ctx->Shared->Default2D;
3148 texUnit->Current3D = ctx->Shared->Default3D;
3149 texUnit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
3150 texUnit->CurrentRect = ctx->Shared->DefaultRect;
3151 }
3152
3153
3154 GLboolean _mesa_init_texture( GLcontext * ctx )
3155 {
3156 int i;
3157
3158 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
3159 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
3160
3161 /* Effectively bind the default textures to all texture units */
3162 ctx->Shared->Default1D->RefCount += MAX_TEXTURE_UNITS;
3163 ctx->Shared->Default2D->RefCount += MAX_TEXTURE_UNITS;
3164 ctx->Shared->Default3D->RefCount += MAX_TEXTURE_UNITS;
3165 ctx->Shared->DefaultCubeMap->RefCount += MAX_TEXTURE_UNITS;
3166 ctx->Shared->DefaultRect->RefCount += MAX_TEXTURE_UNITS;
3167
3168 /* Texture group */
3169 ctx->Texture.CurrentUnit = 0; /* multitexture */
3170 ctx->Texture._EnabledUnits = 0;
3171 for (i=0; i<MAX_TEXTURE_UNITS; i++)
3172 init_texture_unit( ctx, i );
3173 ctx->Texture.SharedPalette = GL_FALSE;
3174 _mesa_init_colortable(&ctx->Texture.Palette);
3175
3176 /* Allocate proxy textures */
3177 if (!alloc_proxy_textures( ctx ))
3178 return GL_FALSE;
3179
3180 return GL_TRUE;
3181 }
3182
3183 void _mesa_free_texture_data( GLcontext *ctx )
3184 {
3185 int i;
3186
3187 /* Free proxy texture objects */
3188 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy1D );
3189 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy2D );
3190 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy3D );
3191 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.ProxyCubeMap );
3192 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.ProxyRect );
3193
3194 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
3195 _mesa_free_colortable_data( &ctx->Texture.Unit[i].ColorTable );
3196 }