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