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