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