448fc539121251a60568522062a49ffa461d24c7
[mesa.git] / src / mesa / main / texstate.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file texstate.c
27 *
28 * Texture state handling.
29 */
30
31 #include "glheader.h"
32 #include "colormac.h"
33 #if FEATURE_colortable
34 #include "colortab.h"
35 #endif
36 #include "context.h"
37 #include "enums.h"
38 #include "macros.h"
39 #include "texcompress.h"
40 #include "texobj.h"
41 #include "teximage.h"
42 #include "texstate.h"
43 #include "texenvprogram.h"
44 #include "mtypes.h"
45 #include "math/m_xform.h"
46
47
48 #define ENUM_TO_FLOAT(X) ((GLfloat)(GLint)(X))
49 #define ENUM_TO_DOUBLE(X) ((GLdouble)(GLint)(X))
50
51
52 /**
53 * Default texture combine environment state. This is used to initialize
54 * a context's texture units and as the basis for converting "classic"
55 * texture environmnets to ARB_texture_env_combine style values.
56 */
57 static const struct gl_tex_env_combine_state default_combine_state = {
58 GL_MODULATE, GL_MODULATE,
59 { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT },
60 { GL_TEXTURE, GL_PREVIOUS, GL_CONSTANT },
61 { GL_SRC_COLOR, GL_SRC_COLOR, GL_SRC_ALPHA },
62 { GL_SRC_ALPHA, GL_SRC_ALPHA, GL_SRC_ALPHA },
63 0, 0,
64 2, 2
65 };
66
67
68 /**
69 * Copy a texture binding. Helper used by _mesa_copy_texture_state().
70 */
71 static void
72 copy_texture_binding(const GLcontext *ctx,
73 struct gl_texture_object **dst,
74 struct gl_texture_object *src)
75 {
76 /* only copy if names differ (per OpenGL SI) */
77 if ((*dst)->Name != src->Name) {
78 /* unbind/delete dest binding which we're changing */
79 (*dst)->RefCount--;
80 if ((*dst)->RefCount == 0) {
81 /* time to delete this texture object */
82 ASSERT((*dst)->Name != 0);
83 ASSERT(ctx->Driver.DeleteTexture);
84 /* XXX cast-away const, unfortunately */
85 (*ctx->Driver.DeleteTexture)((GLcontext *) ctx, *dst);
86 }
87 /* make new binding, incrementing ref count */
88 *dst = src;
89 src->RefCount++;
90 }
91 }
92
93
94 /**
95 * Used by glXCopyContext to copy texture state from one context to another.
96 */
97 void
98 _mesa_copy_texture_state( const GLcontext *src, GLcontext *dst )
99 {
100 GLuint i;
101
102 ASSERT(src);
103 ASSERT(dst);
104
105 dst->Texture.CurrentUnit = src->Texture.CurrentUnit;
106 dst->Texture._GenFlags = src->Texture._GenFlags;
107 dst->Texture._TexGenEnabled = src->Texture._TexGenEnabled;
108 dst->Texture._TexMatEnabled = src->Texture._TexMatEnabled;
109 dst->Texture.SharedPalette = src->Texture.SharedPalette;
110
111 /* per-unit state */
112 for (i = 0; i < src->Const.MaxTextureUnits; i++) {
113 dst->Texture.Unit[i].Enabled = src->Texture.Unit[i].Enabled;
114 dst->Texture.Unit[i].EnvMode = src->Texture.Unit[i].EnvMode;
115 COPY_4V(dst->Texture.Unit[i].EnvColor, src->Texture.Unit[i].EnvColor);
116 dst->Texture.Unit[i].TexGenEnabled = src->Texture.Unit[i].TexGenEnabled;
117 dst->Texture.Unit[i].GenModeS = src->Texture.Unit[i].GenModeS;
118 dst->Texture.Unit[i].GenModeT = src->Texture.Unit[i].GenModeT;
119 dst->Texture.Unit[i].GenModeR = src->Texture.Unit[i].GenModeR;
120 dst->Texture.Unit[i].GenModeQ = src->Texture.Unit[i].GenModeQ;
121 dst->Texture.Unit[i]._GenBitS = src->Texture.Unit[i]._GenBitS;
122 dst->Texture.Unit[i]._GenBitT = src->Texture.Unit[i]._GenBitT;
123 dst->Texture.Unit[i]._GenBitR = src->Texture.Unit[i]._GenBitR;
124 dst->Texture.Unit[i]._GenBitQ = src->Texture.Unit[i]._GenBitQ;
125 dst->Texture.Unit[i]._GenFlags = src->Texture.Unit[i]._GenFlags;
126 COPY_4V(dst->Texture.Unit[i].ObjectPlaneS, src->Texture.Unit[i].ObjectPlaneS);
127 COPY_4V(dst->Texture.Unit[i].ObjectPlaneT, src->Texture.Unit[i].ObjectPlaneT);
128 COPY_4V(dst->Texture.Unit[i].ObjectPlaneR, src->Texture.Unit[i].ObjectPlaneR);
129 COPY_4V(dst->Texture.Unit[i].ObjectPlaneQ, src->Texture.Unit[i].ObjectPlaneQ);
130 COPY_4V(dst->Texture.Unit[i].EyePlaneS, src->Texture.Unit[i].EyePlaneS);
131 COPY_4V(dst->Texture.Unit[i].EyePlaneT, src->Texture.Unit[i].EyePlaneT);
132 COPY_4V(dst->Texture.Unit[i].EyePlaneR, src->Texture.Unit[i].EyePlaneR);
133 COPY_4V(dst->Texture.Unit[i].EyePlaneQ, src->Texture.Unit[i].EyePlaneQ);
134 dst->Texture.Unit[i].LodBias = src->Texture.Unit[i].LodBias;
135
136 /* GL_EXT_texture_env_combine */
137 dst->Texture.Unit[i].Combine.ModeRGB = src->Texture.Unit[i].Combine.ModeRGB;
138 dst->Texture.Unit[i].Combine.ModeA = src->Texture.Unit[i].Combine.ModeA;
139 COPY_3V(dst->Texture.Unit[i].Combine.SourceRGB, src->Texture.Unit[i].Combine.SourceRGB);
140 COPY_3V(dst->Texture.Unit[i].Combine.SourceA, src->Texture.Unit[i].Combine.SourceA);
141 COPY_3V(dst->Texture.Unit[i].Combine.OperandRGB, src->Texture.Unit[i].Combine.OperandRGB);
142 COPY_3V(dst->Texture.Unit[i].Combine.OperandA, src->Texture.Unit[i].Combine.OperandA);
143 dst->Texture.Unit[i].Combine.ScaleShiftRGB = src->Texture.Unit[i].Combine.ScaleShiftRGB;
144 dst->Texture.Unit[i].Combine.ScaleShiftA = src->Texture.Unit[i].Combine.ScaleShiftA;
145
146 /* copy texture object bindings, not contents of texture objects */
147 _mesa_lock_context_textures(dst);
148
149 copy_texture_binding(src, &dst->Texture.Unit[i].Current1D,
150 src->Texture.Unit[i].Current1D);
151 copy_texture_binding(src, &dst->Texture.Unit[i].Current2D,
152 src->Texture.Unit[i].Current2D);
153 copy_texture_binding(src, &dst->Texture.Unit[i].Current3D,
154 src->Texture.Unit[i].Current3D);
155 copy_texture_binding(src, &dst->Texture.Unit[i].CurrentCubeMap,
156 src->Texture.Unit[i].CurrentCubeMap);
157 copy_texture_binding(src, &dst->Texture.Unit[i].CurrentRect,
158 src->Texture.Unit[i].CurrentRect);
159 copy_texture_binding(src, &dst->Texture.Unit[i].Current1DArray,
160 src->Texture.Unit[i].Current1DArray);
161 copy_texture_binding(src, &dst->Texture.Unit[i].Current2DArray,
162 src->Texture.Unit[i].Current2DArray);
163
164 _mesa_unlock_context_textures(dst);
165 }
166 }
167
168
169 /*
170 * For debugging
171 */
172 void
173 _mesa_print_texunit_state( GLcontext *ctx, GLuint unit )
174 {
175 const struct gl_texture_unit *texUnit = ctx->Texture.Unit + unit;
176 _mesa_printf("Texture Unit %d\n", unit);
177 _mesa_printf(" GL_TEXTURE_ENV_MODE = %s\n", _mesa_lookup_enum_by_nr(texUnit->EnvMode));
178 _mesa_printf(" GL_COMBINE_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.ModeRGB));
179 _mesa_printf(" GL_COMBINE_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.ModeA));
180 _mesa_printf(" GL_SOURCE0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[0]));
181 _mesa_printf(" GL_SOURCE1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[1]));
182 _mesa_printf(" GL_SOURCE2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceRGB[2]));
183 _mesa_printf(" GL_SOURCE0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[0]));
184 _mesa_printf(" GL_SOURCE1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[1]));
185 _mesa_printf(" GL_SOURCE2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.SourceA[2]));
186 _mesa_printf(" GL_OPERAND0_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[0]));
187 _mesa_printf(" GL_OPERAND1_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[1]));
188 _mesa_printf(" GL_OPERAND2_RGB = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandRGB[2]));
189 _mesa_printf(" GL_OPERAND0_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[0]));
190 _mesa_printf(" GL_OPERAND1_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[1]));
191 _mesa_printf(" GL_OPERAND2_ALPHA = %s\n", _mesa_lookup_enum_by_nr(texUnit->Combine.OperandA[2]));
192 _mesa_printf(" GL_RGB_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftRGB);
193 _mesa_printf(" GL_ALPHA_SCALE = %d\n", 1 << texUnit->Combine.ScaleShiftA);
194 _mesa_printf(" GL_TEXTURE_ENV_COLOR = (%f, %f, %f, %f)\n", texUnit->EnvColor[0], texUnit->EnvColor[1], texUnit->EnvColor[2], texUnit->EnvColor[3]);
195 }
196
197
198
199 /**********************************************************************/
200 /* Texture Environment */
201 /**********************************************************************/
202
203 /**
204 * Convert "classic" texture environment to ARB_texture_env_combine style
205 * environments.
206 *
207 * \param state texture_env_combine state vector to be filled-in.
208 * \param mode Classic texture environment mode (i.e., \c GL_REPLACE,
209 * \c GL_BLEND, \c GL_DECAL, etc.).
210 * \param texBaseFormat Base format of the texture associated with the
211 * texture unit.
212 */
213 static void
214 calculate_derived_texenv( struct gl_tex_env_combine_state *state,
215 GLenum mode, GLenum texBaseFormat )
216 {
217 GLenum mode_rgb;
218 GLenum mode_a;
219
220 *state = default_combine_state;
221
222 switch (texBaseFormat) {
223 case GL_ALPHA:
224 state->SourceRGB[0] = GL_PREVIOUS;
225 break;
226
227 case GL_LUMINANCE_ALPHA:
228 case GL_INTENSITY:
229 case GL_RGBA:
230 break;
231
232 case GL_LUMINANCE:
233 case GL_RGB:
234 case GL_YCBCR_MESA:
235 state->SourceA[0] = GL_PREVIOUS;
236 break;
237
238 default:
239 _mesa_problem(NULL, "Invalid texBaseFormat in calculate_derived_texenv");
240 return;
241 }
242
243 if (mode == GL_REPLACE_EXT)
244 mode = GL_REPLACE;
245
246 switch (mode) {
247 case GL_REPLACE:
248 case GL_MODULATE:
249 mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : mode;
250 mode_a = mode;
251 break;
252
253 case GL_DECAL:
254 mode_rgb = GL_INTERPOLATE;
255 mode_a = GL_REPLACE;
256
257 state->SourceA[0] = GL_PREVIOUS;
258
259 /* Having alpha / luminance / intensity textures replace using the
260 * incoming fragment color matches the definition in NV_texture_shader.
261 * The 1.5 spec simply marks these as "undefined".
262 */
263 switch (texBaseFormat) {
264 case GL_ALPHA:
265 case GL_LUMINANCE:
266 case GL_LUMINANCE_ALPHA:
267 case GL_INTENSITY:
268 state->SourceRGB[0] = GL_PREVIOUS;
269 break;
270 case GL_RGB:
271 case GL_YCBCR_MESA:
272 mode_rgb = GL_REPLACE;
273 break;
274 case GL_RGBA:
275 state->SourceRGB[2] = GL_TEXTURE;
276 break;
277 }
278 break;
279
280 case GL_BLEND:
281 mode_rgb = GL_INTERPOLATE;
282 mode_a = GL_MODULATE;
283
284 switch (texBaseFormat) {
285 case GL_ALPHA:
286 mode_rgb = GL_REPLACE;
287 break;
288 case GL_INTENSITY:
289 mode_a = GL_INTERPOLATE;
290 state->SourceA[0] = GL_CONSTANT;
291 state->OperandA[2] = GL_SRC_ALPHA;
292 /* FALLTHROUGH */
293 case GL_LUMINANCE:
294 case GL_RGB:
295 case GL_LUMINANCE_ALPHA:
296 case GL_RGBA:
297 case GL_YCBCR_MESA:
298 state->SourceRGB[2] = GL_TEXTURE;
299 state->SourceA[2] = GL_TEXTURE;
300 state->SourceRGB[0] = GL_CONSTANT;
301 state->OperandRGB[2] = GL_SRC_COLOR;
302 break;
303 }
304 break;
305
306 case GL_ADD:
307 mode_rgb = (texBaseFormat == GL_ALPHA) ? GL_REPLACE : GL_ADD;
308 mode_a = (texBaseFormat == GL_INTENSITY) ? GL_ADD : GL_MODULATE;
309 break;
310
311 default:
312 _mesa_problem(NULL,
313 "Invalid texture env mode in calculate_derived_texenv");
314 return;
315 }
316
317 state->ModeRGB = (state->SourceRGB[0] != GL_PREVIOUS)
318 ? mode_rgb : GL_REPLACE;
319 state->ModeA = (state->SourceA[0] != GL_PREVIOUS)
320 ? mode_a : GL_REPLACE;
321 }
322
323
324 /**********************************************************************/
325 /* Texture Parameters */
326 /**********************************************************************/
327
328 /**
329 * Check if a coordinate wrap mode is supported for the texture target.
330 * \return GL_TRUE if legal, GL_FALSE otherwise
331 */
332 static GLboolean
333 validate_texture_wrap_mode(GLcontext * ctx, GLenum target, GLenum wrap)
334 {
335 const struct gl_extensions * const e = & ctx->Extensions;
336
337 if (wrap == GL_CLAMP || wrap == GL_CLAMP_TO_EDGE ||
338 (wrap == GL_CLAMP_TO_BORDER && e->ARB_texture_border_clamp)) {
339 /* any texture target */
340 return GL_TRUE;
341 }
342 else if (target != GL_TEXTURE_RECTANGLE_NV &&
343 (wrap == GL_REPEAT ||
344 (wrap == GL_MIRRORED_REPEAT &&
345 e->ARB_texture_mirrored_repeat) ||
346 (wrap == GL_MIRROR_CLAMP_EXT &&
347 (e->ATI_texture_mirror_once || e->EXT_texture_mirror_clamp)) ||
348 (wrap == GL_MIRROR_CLAMP_TO_EDGE_EXT &&
349 (e->ATI_texture_mirror_once || e->EXT_texture_mirror_clamp)) ||
350 (wrap == GL_MIRROR_CLAMP_TO_BORDER_EXT &&
351 (e->EXT_texture_mirror_clamp)))) {
352 /* non-rectangle texture */
353 return GL_TRUE;
354 }
355
356 _mesa_error( ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
357 return GL_FALSE;
358 }
359
360
361 void GLAPIENTRY
362 _mesa_TexParameterf( GLenum target, GLenum pname, GLfloat param )
363 {
364 _mesa_TexParameterfv(target, pname, &param);
365 }
366
367
368 void GLAPIENTRY
369 _mesa_TexParameterfv( GLenum target, GLenum pname, const GLfloat *params )
370 {
371 const GLenum eparam = (GLenum) (GLint) params[0];
372 struct gl_texture_unit *texUnit;
373 struct gl_texture_object *texObj;
374 GET_CURRENT_CONTEXT(ctx);
375 ASSERT_OUTSIDE_BEGIN_END(ctx);
376
377 if (MESA_VERBOSE&(VERBOSE_API|VERBOSE_TEXTURE))
378 _mesa_debug(ctx, "glTexParameter %s %s %.1f(%s)...\n",
379 _mesa_lookup_enum_by_nr(target),
380 _mesa_lookup_enum_by_nr(pname),
381 *params,
382 _mesa_lookup_enum_by_nr(eparam));
383
384 if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureImageUnits) {
385 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexParameterfv(current unit)");
386 return;
387 }
388
389 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
390
391 switch (target) {
392 case GL_TEXTURE_1D:
393 texObj = texUnit->Current1D;
394 break;
395 case GL_TEXTURE_2D:
396 texObj = texUnit->Current2D;
397 break;
398 case GL_TEXTURE_3D:
399 texObj = texUnit->Current3D;
400 break;
401 case GL_TEXTURE_CUBE_MAP:
402 if (!ctx->Extensions.ARB_texture_cube_map) {
403 _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(target)" );
404 return;
405 }
406 texObj = texUnit->CurrentCubeMap;
407 break;
408 case GL_TEXTURE_RECTANGLE_NV:
409 if (!ctx->Extensions.NV_texture_rectangle) {
410 _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(target)" );
411 return;
412 }
413 texObj = texUnit->CurrentRect;
414 break;
415 case GL_TEXTURE_1D_ARRAY_EXT:
416 if (!ctx->Extensions.MESA_texture_array) {
417 _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(target)" );
418 return;
419 }
420 texObj = texUnit->Current1DArray;
421 break;
422 case GL_TEXTURE_2D_ARRAY_EXT:
423 if (!ctx->Extensions.MESA_texture_array) {
424 _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(target)" );
425 return;
426 }
427 texObj = texUnit->Current2DArray;
428 break;
429 default:
430 _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(target)" );
431 return;
432 }
433
434 switch (pname) {
435 case GL_TEXTURE_MIN_FILTER:
436 /* A small optimization */
437 if (texObj->MinFilter == eparam)
438 return;
439 if (eparam==GL_NEAREST || eparam==GL_LINEAR) {
440 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
441 texObj->MinFilter = eparam;
442 }
443 else if ((eparam==GL_NEAREST_MIPMAP_NEAREST ||
444 eparam==GL_LINEAR_MIPMAP_NEAREST ||
445 eparam==GL_NEAREST_MIPMAP_LINEAR ||
446 eparam==GL_LINEAR_MIPMAP_LINEAR) &&
447 texObj->Target != GL_TEXTURE_RECTANGLE_NV) {
448 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
449 texObj->MinFilter = eparam;
450 }
451 else {
452 _mesa_error( ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
453 return;
454 }
455 break;
456 case GL_TEXTURE_MAG_FILTER:
457 /* A small optimization */
458 if (texObj->MagFilter == eparam)
459 return;
460
461 if (eparam==GL_NEAREST || eparam==GL_LINEAR) {
462 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
463 texObj->MagFilter = eparam;
464 }
465 else {
466 _mesa_error( ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
467 return;
468 }
469 break;
470 case GL_TEXTURE_WRAP_S:
471 if (texObj->WrapS == eparam)
472 return;
473 if (validate_texture_wrap_mode(ctx, texObj->Target, eparam)) {
474 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
475 texObj->WrapS = eparam;
476 }
477 else {
478 return;
479 }
480 break;
481 case GL_TEXTURE_WRAP_T:
482 if (texObj->WrapT == eparam)
483 return;
484 if (validate_texture_wrap_mode(ctx, texObj->Target, eparam)) {
485 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
486 texObj->WrapT = eparam;
487 }
488 else {
489 return;
490 }
491 break;
492 case GL_TEXTURE_WRAP_R:
493 if (texObj->WrapR == eparam)
494 return;
495 if (validate_texture_wrap_mode(ctx, texObj->Target, eparam)) {
496 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
497 texObj->WrapR = eparam;
498 }
499 else {
500 return;
501 }
502 break;
503 case GL_TEXTURE_BORDER_COLOR:
504 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
505 texObj->BorderColor[RCOMP] = params[0];
506 texObj->BorderColor[GCOMP] = params[1];
507 texObj->BorderColor[BCOMP] = params[2];
508 texObj->BorderColor[ACOMP] = params[3];
509 UNCLAMPED_FLOAT_TO_CHAN(texObj->_BorderChan[RCOMP], params[0]);
510 UNCLAMPED_FLOAT_TO_CHAN(texObj->_BorderChan[GCOMP], params[1]);
511 UNCLAMPED_FLOAT_TO_CHAN(texObj->_BorderChan[BCOMP], params[2]);
512 UNCLAMPED_FLOAT_TO_CHAN(texObj->_BorderChan[ACOMP], params[3]);
513 break;
514 case GL_TEXTURE_MIN_LOD:
515 if (texObj->MinLod == params[0])
516 return;
517 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
518 texObj->MinLod = params[0];
519 break;
520 case GL_TEXTURE_MAX_LOD:
521 if (texObj->MaxLod == params[0])
522 return;
523 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
524 texObj->MaxLod = params[0];
525 break;
526 case GL_TEXTURE_BASE_LEVEL:
527 if (params[0] < 0.0) {
528 _mesa_error(ctx, GL_INVALID_VALUE, "glTexParameter(param)");
529 return;
530 }
531 if (target == GL_TEXTURE_RECTANGLE_ARB && params[0] != 0.0) {
532 _mesa_error(ctx, GL_INVALID_VALUE, "glTexParameter(param)");
533 return;
534 }
535 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
536 texObj->BaseLevel = (GLint) params[0];
537 break;
538 case GL_TEXTURE_MAX_LEVEL:
539 if (params[0] < 0.0) {
540 _mesa_error(ctx, GL_INVALID_VALUE, "glTexParameter(param)");
541 return;
542 }
543 if (target == GL_TEXTURE_RECTANGLE_ARB) {
544 _mesa_error(ctx, GL_INVALID_OPERATION, "glTexParameter(param)");
545 return;
546 }
547 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
548 texObj->MaxLevel = (GLint) params[0];
549 break;
550 case GL_TEXTURE_PRIORITY:
551 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
552 texObj->Priority = CLAMP( params[0], 0.0F, 1.0F );
553 break;
554 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
555 if (ctx->Extensions.EXT_texture_filter_anisotropic) {
556 if (params[0] < 1.0) {
557 _mesa_error(ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
558 return;
559 }
560 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
561 /* clamp to max, that's what NVIDIA does */
562 texObj->MaxAnisotropy = MIN2(params[0],
563 ctx->Const.MaxTextureMaxAnisotropy);
564 }
565 else {
566 _mesa_error(ctx, GL_INVALID_ENUM,
567 "glTexParameter(pname=GL_TEXTURE_MAX_ANISOTROPY_EXT)");
568 return;
569 }
570 break;
571 case GL_TEXTURE_COMPARE_SGIX:
572 if (ctx->Extensions.SGIX_shadow) {
573 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
574 texObj->CompareFlag = params[0] ? GL_TRUE : GL_FALSE;
575 }
576 else {
577 _mesa_error(ctx, GL_INVALID_ENUM,
578 "glTexParameter(pname=GL_TEXTURE_COMPARE_SGIX)");
579 return;
580 }
581 break;
582 case GL_TEXTURE_COMPARE_OPERATOR_SGIX:
583 if (ctx->Extensions.SGIX_shadow) {
584 GLenum op = (GLenum) params[0];
585 if (op == GL_TEXTURE_LEQUAL_R_SGIX ||
586 op == GL_TEXTURE_GEQUAL_R_SGIX) {
587 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
588 texObj->CompareOperator = op;
589 }
590 else {
591 _mesa_error(ctx, GL_INVALID_ENUM, "glTexParameter(param)");
592 }
593 }
594 else {
595 _mesa_error(ctx, GL_INVALID_ENUM,
596 "glTexParameter(pname=GL_TEXTURE_COMPARE_OPERATOR_SGIX)");
597 return;
598 }
599 break;
600 case GL_SHADOW_AMBIENT_SGIX: /* aka GL_TEXTURE_COMPARE_FAIL_VALUE_ARB */
601 if (ctx->Extensions.SGIX_shadow_ambient) {
602 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
603 texObj->ShadowAmbient = CLAMP(params[0], 0.0F, 1.0F);
604 }
605 else {
606 _mesa_error(ctx, GL_INVALID_ENUM,
607 "glTexParameter(pname=GL_SHADOW_AMBIENT_SGIX)");
608 return;
609 }
610 break;
611 case GL_GENERATE_MIPMAP_SGIS:
612 if (ctx->Extensions.SGIS_generate_mipmap) {
613 texObj->GenerateMipmap = params[0] ? GL_TRUE : GL_FALSE;
614 }
615 else {
616 _mesa_error(ctx, GL_INVALID_ENUM,
617 "glTexParameter(pname=GL_GENERATE_MIPMAP_SGIS)");
618 return;
619 }
620 break;
621 case GL_TEXTURE_COMPARE_MODE_ARB:
622 if (ctx->Extensions.ARB_shadow) {
623 const GLenum mode = (GLenum) params[0];
624 if (mode == GL_NONE || mode == GL_COMPARE_R_TO_TEXTURE_ARB) {
625 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
626 texObj->CompareMode = mode;
627 }
628 else {
629 _mesa_error(ctx, GL_INVALID_ENUM,
630 "glTexParameter(bad GL_TEXTURE_COMPARE_MODE_ARB: 0x%x)", mode);
631 return;
632 }
633 }
634 else {
635 _mesa_error(ctx, GL_INVALID_ENUM,
636 "glTexParameter(pname=GL_TEXTURE_COMPARE_MODE_ARB)");
637 return;
638 }
639 break;
640 case GL_TEXTURE_COMPARE_FUNC_ARB:
641 if (ctx->Extensions.ARB_shadow) {
642 const GLenum func = (GLenum) params[0];
643 if (func == GL_LEQUAL || func == GL_GEQUAL) {
644 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
645 texObj->CompareFunc = func;
646 }
647 else if (ctx->Extensions.EXT_shadow_funcs &&
648 (func == GL_EQUAL ||
649 func == GL_NOTEQUAL ||
650 func == GL_LESS ||
651 func == GL_GREATER ||
652 func == GL_ALWAYS ||
653 func == GL_NEVER)) {
654 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
655 texObj->CompareFunc = func;
656 }
657 else {
658 _mesa_error(ctx, GL_INVALID_ENUM,
659 "glTexParameter(bad GL_TEXTURE_COMPARE_FUNC_ARB)");
660 return;
661 }
662 }
663 else {
664 _mesa_error(ctx, GL_INVALID_ENUM,
665 "glTexParameter(pname=GL_TEXTURE_COMPARE_FUNC_ARB)");
666 return;
667 }
668 break;
669 case GL_DEPTH_TEXTURE_MODE_ARB:
670 if (ctx->Extensions.ARB_depth_texture) {
671 const GLenum result = (GLenum) params[0];
672 if (result == GL_LUMINANCE || result == GL_INTENSITY
673 || result == GL_ALPHA) {
674 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
675 texObj->DepthMode = result;
676 }
677 else {
678 _mesa_error(ctx, GL_INVALID_ENUM,
679 "glTexParameter(bad GL_DEPTH_TEXTURE_MODE_ARB)");
680 return;
681 }
682 }
683 else {
684 _mesa_error(ctx, GL_INVALID_ENUM,
685 "glTexParameter(pname=GL_DEPTH_TEXTURE_MODE_ARB)");
686 return;
687 }
688 break;
689 case GL_TEXTURE_LOD_BIAS:
690 /* NOTE: this is really part of OpenGL 1.4, not EXT_texture_lod_bias*/
691 if (ctx->Extensions.EXT_texture_lod_bias) {
692 if (texObj->LodBias != params[0]) {
693 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
694 texObj->LodBias = params[0];
695 }
696 }
697 break;
698
699 default:
700 _mesa_error(ctx, GL_INVALID_ENUM,
701 "glTexParameter(pname=0x%x)", pname);
702 return;
703 }
704
705 texObj->_Complete = GL_FALSE;
706
707 if (ctx->Driver.TexParameter) {
708 (*ctx->Driver.TexParameter)( ctx, target, texObj, pname, params );
709 }
710 }
711
712
713 void GLAPIENTRY
714 _mesa_TexParameteri( GLenum target, GLenum pname, GLint param )
715 {
716 GLfloat fparam[4];
717 if (pname == GL_TEXTURE_PRIORITY)
718 fparam[0] = INT_TO_FLOAT(param);
719 else
720 fparam[0] = (GLfloat) param;
721 fparam[1] = fparam[2] = fparam[3] = 0.0;
722 _mesa_TexParameterfv(target, pname, fparam);
723 }
724
725
726 void GLAPIENTRY
727 _mesa_TexParameteriv( GLenum target, GLenum pname, const GLint *params )
728 {
729 GLfloat fparam[4];
730 if (pname == GL_TEXTURE_BORDER_COLOR) {
731 fparam[0] = INT_TO_FLOAT(params[0]);
732 fparam[1] = INT_TO_FLOAT(params[1]);
733 fparam[2] = INT_TO_FLOAT(params[2]);
734 fparam[3] = INT_TO_FLOAT(params[3]);
735 }
736 else {
737 if (pname == GL_TEXTURE_PRIORITY)
738 fparam[0] = INT_TO_FLOAT(params[0]);
739 else
740 fparam[0] = (GLfloat) params[0];
741 fparam[1] = fparam[2] = fparam[3] = 0.0F;
742 }
743 _mesa_TexParameterfv(target, pname, fparam);
744 }
745
746
747 void GLAPIENTRY
748 _mesa_GetTexLevelParameterfv( GLenum target, GLint level,
749 GLenum pname, GLfloat *params )
750 {
751 GLint iparam;
752 _mesa_GetTexLevelParameteriv( target, level, pname, &iparam );
753 *params = (GLfloat) iparam;
754 }
755
756
757 static GLuint
758 tex_image_dimensions(GLcontext *ctx, GLenum target)
759 {
760 switch (target) {
761 case GL_TEXTURE_1D:
762 case GL_PROXY_TEXTURE_1D:
763 return 1;
764 case GL_TEXTURE_2D:
765 case GL_PROXY_TEXTURE_2D:
766 return 2;
767 case GL_TEXTURE_3D:
768 case GL_PROXY_TEXTURE_3D:
769 return 3;
770 case GL_TEXTURE_CUBE_MAP:
771 case GL_PROXY_TEXTURE_CUBE_MAP:
772 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
773 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
774 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
775 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
776 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
777 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
778 return ctx->Extensions.ARB_texture_cube_map ? 2 : 0;
779 case GL_TEXTURE_RECTANGLE_NV:
780 case GL_PROXY_TEXTURE_RECTANGLE_NV:
781 return ctx->Extensions.NV_texture_rectangle ? 2 : 0;
782 case GL_TEXTURE_1D_ARRAY_EXT:
783 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
784 return ctx->Extensions.MESA_texture_array ? 2 : 0;
785 case GL_TEXTURE_2D_ARRAY_EXT:
786 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
787 return ctx->Extensions.MESA_texture_array ? 3 : 0;
788 default:
789 _mesa_problem(ctx, "bad target in _mesa_tex_target_dimensions()");
790 return 0;
791 }
792 }
793
794
795 void GLAPIENTRY
796 _mesa_GetTexLevelParameteriv( GLenum target, GLint level,
797 GLenum pname, GLint *params )
798 {
799 const struct gl_texture_unit *texUnit;
800 struct gl_texture_object *texObj;
801 const struct gl_texture_image *img = NULL;
802 GLuint dimensions;
803 GLboolean isProxy;
804 GLint maxLevels;
805 GET_CURRENT_CONTEXT(ctx);
806 ASSERT_OUTSIDE_BEGIN_END(ctx);
807
808 if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureImageUnits) {
809 _mesa_error(ctx, GL_INVALID_OPERATION,
810 "glGetTexLevelParameteriv(current unit)");
811 return;
812 }
813
814 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
815
816 /* this will catch bad target values */
817 dimensions = tex_image_dimensions(ctx, target); /* 1, 2 or 3 */
818 if (dimensions == 0) {
819 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexLevelParameter[if]v(target)");
820 return;
821 }
822
823 maxLevels = _mesa_max_texture_levels(ctx, target);
824 if (maxLevels == 0) {
825 /* should not happen since <target> was just checked above */
826 _mesa_problem(ctx, "maxLevels=0 in _mesa_GetTexLevelParameter");
827 return;
828 }
829
830 if (level < 0 || level >= maxLevels) {
831 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexLevelParameter[if]v" );
832 return;
833 }
834
835 texObj = _mesa_select_tex_object(ctx, texUnit, target);
836 _mesa_lock_texture(ctx, texObj);
837
838 img = _mesa_select_tex_image(ctx, texObj, target, level);
839 if (!img || !img->TexFormat) {
840 /* undefined texture image */
841 if (pname == GL_TEXTURE_COMPONENTS)
842 *params = 1;
843 else
844 *params = 0;
845 goto out;
846 }
847
848 isProxy = _mesa_is_proxy_texture(target);
849
850 switch (pname) {
851 case GL_TEXTURE_WIDTH:
852 *params = img->Width;
853 break;
854 case GL_TEXTURE_HEIGHT:
855 *params = img->Height;
856 break;
857 case GL_TEXTURE_DEPTH:
858 *params = img->Depth;
859 break;
860 case GL_TEXTURE_INTERNAL_FORMAT:
861 *params = img->InternalFormat;
862 break;
863 case GL_TEXTURE_BORDER:
864 *params = img->Border;
865 break;
866 case GL_TEXTURE_RED_SIZE:
867 if (img->_BaseFormat == GL_RGB || img->_BaseFormat == GL_RGBA)
868 *params = img->TexFormat->RedBits;
869 else
870 *params = 0;
871 break;
872 case GL_TEXTURE_GREEN_SIZE:
873 if (img->_BaseFormat == GL_RGB || img->_BaseFormat == GL_RGBA)
874 *params = img->TexFormat->GreenBits;
875 else
876 *params = 0;
877 break;
878 case GL_TEXTURE_BLUE_SIZE:
879 if (img->_BaseFormat == GL_RGB || img->_BaseFormat == GL_RGBA)
880 *params = img->TexFormat->BlueBits;
881 else
882 *params = 0;
883 break;
884 case GL_TEXTURE_ALPHA_SIZE:
885 if (img->_BaseFormat == GL_ALPHA ||
886 img->_BaseFormat == GL_LUMINANCE_ALPHA ||
887 img->_BaseFormat == GL_RGBA)
888 *params = img->TexFormat->AlphaBits;
889 else
890 *params = 0;
891 break;
892 case GL_TEXTURE_INTENSITY_SIZE:
893 if (img->_BaseFormat != GL_INTENSITY)
894 *params = 0;
895 else if (img->TexFormat->IntensityBits > 0)
896 *params = img->TexFormat->IntensityBits;
897 else /* intensity probably stored as rgb texture */
898 *params = MIN2(img->TexFormat->RedBits, img->TexFormat->GreenBits);
899 break;
900 case GL_TEXTURE_LUMINANCE_SIZE:
901 if (img->_BaseFormat != GL_LUMINANCE &&
902 img->_BaseFormat != GL_LUMINANCE_ALPHA)
903 *params = 0;
904 else if (img->TexFormat->LuminanceBits > 0)
905 *params = img->TexFormat->LuminanceBits;
906 else /* luminance probably stored as rgb texture */
907 *params = MIN2(img->TexFormat->RedBits, img->TexFormat->GreenBits);
908 break;
909 case GL_TEXTURE_INDEX_SIZE_EXT:
910 if (img->_BaseFormat == GL_COLOR_INDEX)
911 *params = img->TexFormat->IndexBits;
912 else
913 *params = 0;
914 break;
915 case GL_TEXTURE_DEPTH_SIZE_ARB:
916 if (ctx->Extensions.SGIX_depth_texture ||
917 ctx->Extensions.ARB_depth_texture)
918 *params = img->TexFormat->DepthBits;
919 else
920 _mesa_error(ctx, GL_INVALID_ENUM,
921 "glGetTexLevelParameter[if]v(pname)");
922 break;
923 case GL_TEXTURE_STENCIL_SIZE_EXT:
924 if (ctx->Extensions.EXT_packed_depth_stencil) {
925 *params = img->TexFormat->StencilBits;
926 }
927 else {
928 _mesa_error(ctx, GL_INVALID_ENUM,
929 "glGetTexLevelParameter[if]v(pname)");
930 }
931 break;
932
933 /* GL_ARB_texture_compression */
934 case GL_TEXTURE_COMPRESSED_IMAGE_SIZE:
935 if (ctx->Extensions.ARB_texture_compression) {
936 if (img->IsCompressed && !isProxy) {
937 /* Don't use ctx->Driver.CompressedTextureSize() since that
938 * may returned a padded hardware size.
939 */
940 *params = _mesa_compressed_texture_size(ctx, img->Width,
941 img->Height, img->Depth,
942 img->TexFormat->MesaFormat);
943 }
944 else {
945 _mesa_error(ctx, GL_INVALID_OPERATION,
946 "glGetTexLevelParameter[if]v(pname)");
947 }
948 }
949 else {
950 _mesa_error(ctx, GL_INVALID_ENUM,
951 "glGetTexLevelParameter[if]v(pname)");
952 }
953 break;
954 case GL_TEXTURE_COMPRESSED:
955 if (ctx->Extensions.ARB_texture_compression) {
956 *params = (GLint) img->IsCompressed;
957 }
958 else {
959 _mesa_error(ctx, GL_INVALID_ENUM,
960 "glGetTexLevelParameter[if]v(pname)");
961 }
962 break;
963
964 /* GL_ARB_texture_float */
965 case GL_TEXTURE_RED_TYPE_ARB:
966 if (ctx->Extensions.ARB_texture_float) {
967 *params = img->TexFormat->RedBits ? img->TexFormat->DataType : GL_NONE;
968 }
969 else {
970 _mesa_error(ctx, GL_INVALID_ENUM,
971 "glGetTexLevelParameter[if]v(pname)");
972 }
973 break;
974 case GL_TEXTURE_GREEN_TYPE_ARB:
975 if (ctx->Extensions.ARB_texture_float) {
976 *params = img->TexFormat->GreenBits ? img->TexFormat->DataType : GL_NONE;
977 }
978 else {
979 _mesa_error(ctx, GL_INVALID_ENUM,
980 "glGetTexLevelParameter[if]v(pname)");
981 }
982 break;
983 case GL_TEXTURE_BLUE_TYPE_ARB:
984 if (ctx->Extensions.ARB_texture_float) {
985 *params = img->TexFormat->BlueBits ? img->TexFormat->DataType : GL_NONE;
986 }
987 else {
988 _mesa_error(ctx, GL_INVALID_ENUM,
989 "glGetTexLevelParameter[if]v(pname)");
990 }
991 break;
992 case GL_TEXTURE_ALPHA_TYPE_ARB:
993 if (ctx->Extensions.ARB_texture_float) {
994 *params = img->TexFormat->AlphaBits ? img->TexFormat->DataType : GL_NONE;
995 }
996 else {
997 _mesa_error(ctx, GL_INVALID_ENUM,
998 "glGetTexLevelParameter[if]v(pname)");
999 }
1000 break;
1001 case GL_TEXTURE_LUMINANCE_TYPE_ARB:
1002 if (ctx->Extensions.ARB_texture_float) {
1003 *params = img->TexFormat->LuminanceBits ? img->TexFormat->DataType : GL_NONE;
1004 }
1005 else {
1006 _mesa_error(ctx, GL_INVALID_ENUM,
1007 "glGetTexLevelParameter[if]v(pname)");
1008 }
1009 break;
1010 case GL_TEXTURE_INTENSITY_TYPE_ARB:
1011 if (ctx->Extensions.ARB_texture_float) {
1012 *params = img->TexFormat->IntensityBits ? img->TexFormat->DataType : GL_NONE;
1013 }
1014 else {
1015 _mesa_error(ctx, GL_INVALID_ENUM,
1016 "glGetTexLevelParameter[if]v(pname)");
1017 }
1018 break;
1019 case GL_TEXTURE_DEPTH_TYPE_ARB:
1020 if (ctx->Extensions.ARB_texture_float) {
1021 *params = img->TexFormat->DepthBits ? img->TexFormat->DataType : GL_NONE;
1022 }
1023 else {
1024 _mesa_error(ctx, GL_INVALID_ENUM,
1025 "glGetTexLevelParameter[if]v(pname)");
1026 }
1027 break;
1028
1029 default:
1030 _mesa_error(ctx, GL_INVALID_ENUM,
1031 "glGetTexLevelParameter[if]v(pname)");
1032 }
1033
1034 out:
1035 _mesa_unlock_texture(ctx, texObj);
1036 }
1037
1038
1039
1040 void GLAPIENTRY
1041 _mesa_GetTexParameterfv( GLenum target, GLenum pname, GLfloat *params )
1042 {
1043 struct gl_texture_unit *texUnit;
1044 struct gl_texture_object *obj;
1045 GLboolean error = GL_FALSE;
1046 GET_CURRENT_CONTEXT(ctx);
1047 ASSERT_OUTSIDE_BEGIN_END(ctx);
1048
1049 if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureImageUnits) {
1050 _mesa_error(ctx, GL_INVALID_OPERATION,
1051 "glGetTexParameterfv(current unit)");
1052 return;
1053 }
1054
1055 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1056
1057 obj = _mesa_select_tex_object(ctx, texUnit, target);
1058 if (!obj) {
1059 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameterfv(target)");
1060 return;
1061 }
1062
1063 _mesa_lock_texture(ctx, obj);
1064 switch (pname) {
1065 case GL_TEXTURE_MAG_FILTER:
1066 *params = ENUM_TO_FLOAT(obj->MagFilter);
1067 break;
1068 case GL_TEXTURE_MIN_FILTER:
1069 *params = ENUM_TO_FLOAT(obj->MinFilter);
1070 break;
1071 case GL_TEXTURE_WRAP_S:
1072 *params = ENUM_TO_FLOAT(obj->WrapS);
1073 break;
1074 case GL_TEXTURE_WRAP_T:
1075 *params = ENUM_TO_FLOAT(obj->WrapT);
1076 break;
1077 case GL_TEXTURE_WRAP_R:
1078 *params = ENUM_TO_FLOAT(obj->WrapR);
1079 break;
1080 case GL_TEXTURE_BORDER_COLOR:
1081 params[0] = CLAMP(obj->BorderColor[0], 0.0F, 1.0F);
1082 params[1] = CLAMP(obj->BorderColor[1], 0.0F, 1.0F);
1083 params[2] = CLAMP(obj->BorderColor[2], 0.0F, 1.0F);
1084 params[3] = CLAMP(obj->BorderColor[3], 0.0F, 1.0F);
1085 break;
1086 case GL_TEXTURE_RESIDENT:
1087 {
1088 GLboolean resident;
1089 if (ctx->Driver.IsTextureResident)
1090 resident = ctx->Driver.IsTextureResident(ctx, obj);
1091 else
1092 resident = GL_TRUE;
1093 *params = ENUM_TO_FLOAT(resident);
1094 }
1095 break;
1096 case GL_TEXTURE_PRIORITY:
1097 *params = obj->Priority;
1098 break;
1099 case GL_TEXTURE_MIN_LOD:
1100 *params = obj->MinLod;
1101 break;
1102 case GL_TEXTURE_MAX_LOD:
1103 *params = obj->MaxLod;
1104 break;
1105 case GL_TEXTURE_BASE_LEVEL:
1106 *params = (GLfloat) obj->BaseLevel;
1107 break;
1108 case GL_TEXTURE_MAX_LEVEL:
1109 *params = (GLfloat) obj->MaxLevel;
1110 break;
1111 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
1112 if (ctx->Extensions.EXT_texture_filter_anisotropic) {
1113 *params = obj->MaxAnisotropy;
1114 }
1115 else
1116 error = 1;
1117 break;
1118 case GL_TEXTURE_COMPARE_SGIX:
1119 if (ctx->Extensions.SGIX_shadow) {
1120 *params = (GLfloat) obj->CompareFlag;
1121 }
1122 else
1123 error = 1;
1124 break;
1125 case GL_TEXTURE_COMPARE_OPERATOR_SGIX:
1126 if (ctx->Extensions.SGIX_shadow) {
1127 *params = (GLfloat) obj->CompareOperator;
1128 }
1129 else
1130 error = 1;
1131 break;
1132 case GL_SHADOW_AMBIENT_SGIX: /* aka GL_TEXTURE_COMPARE_FAIL_VALUE_ARB */
1133 if (ctx->Extensions.SGIX_shadow_ambient) {
1134 *params = obj->ShadowAmbient;
1135 }
1136 else
1137 error = 1;
1138 break;
1139 case GL_GENERATE_MIPMAP_SGIS:
1140 if (ctx->Extensions.SGIS_generate_mipmap) {
1141 *params = (GLfloat) obj->GenerateMipmap;
1142 }
1143 else
1144 error = 1;
1145 break;
1146 case GL_TEXTURE_COMPARE_MODE_ARB:
1147 if (ctx->Extensions.ARB_shadow) {
1148 *params = (GLfloat) obj->CompareMode;
1149 }
1150 else
1151 error = 1;
1152 break;
1153 case GL_TEXTURE_COMPARE_FUNC_ARB:
1154 if (ctx->Extensions.ARB_shadow) {
1155 *params = (GLfloat) obj->CompareFunc;
1156 }
1157 else
1158 error = 1;
1159 break;
1160 case GL_DEPTH_TEXTURE_MODE_ARB:
1161 if (ctx->Extensions.ARB_depth_texture) {
1162 *params = (GLfloat) obj->DepthMode;
1163 }
1164 else
1165 error = 1;
1166 break;
1167 case GL_TEXTURE_LOD_BIAS:
1168 if (ctx->Extensions.EXT_texture_lod_bias) {
1169 *params = obj->LodBias;
1170 }
1171 else
1172 error = 1;
1173 break;
1174 default:
1175 error = 1;
1176 break;
1177 }
1178 if (error)
1179 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameterfv(pname=0x%x)",
1180 pname);
1181
1182 _mesa_unlock_texture(ctx, obj);
1183 }
1184
1185
1186 void GLAPIENTRY
1187 _mesa_GetTexParameteriv( GLenum target, GLenum pname, GLint *params )
1188 {
1189 struct gl_texture_unit *texUnit;
1190 struct gl_texture_object *obj;
1191 GET_CURRENT_CONTEXT(ctx);
1192 ASSERT_OUTSIDE_BEGIN_END(ctx);
1193
1194 if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureImageUnits) {
1195 _mesa_error(ctx, GL_INVALID_OPERATION,
1196 "glGetTexParameteriv(current unit)");
1197 return;
1198 }
1199
1200 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1201
1202 obj = _mesa_select_tex_object(ctx, texUnit, target);
1203 if (!obj) {
1204 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameteriv(target)");
1205 return;
1206 }
1207
1208 switch (pname) {
1209 case GL_TEXTURE_MAG_FILTER:
1210 *params = (GLint) obj->MagFilter;
1211 return;
1212 case GL_TEXTURE_MIN_FILTER:
1213 *params = (GLint) obj->MinFilter;
1214 return;
1215 case GL_TEXTURE_WRAP_S:
1216 *params = (GLint) obj->WrapS;
1217 return;
1218 case GL_TEXTURE_WRAP_T:
1219 *params = (GLint) obj->WrapT;
1220 return;
1221 case GL_TEXTURE_WRAP_R:
1222 *params = (GLint) obj->WrapR;
1223 return;
1224 case GL_TEXTURE_BORDER_COLOR:
1225 {
1226 GLfloat b[4];
1227 b[0] = CLAMP(obj->BorderColor[0], 0.0F, 1.0F);
1228 b[1] = CLAMP(obj->BorderColor[1], 0.0F, 1.0F);
1229 b[2] = CLAMP(obj->BorderColor[2], 0.0F, 1.0F);
1230 b[3] = CLAMP(obj->BorderColor[3], 0.0F, 1.0F);
1231 params[0] = FLOAT_TO_INT(b[0]);
1232 params[1] = FLOAT_TO_INT(b[1]);
1233 params[2] = FLOAT_TO_INT(b[2]);
1234 params[3] = FLOAT_TO_INT(b[3]);
1235 }
1236 return;
1237 case GL_TEXTURE_RESIDENT:
1238 {
1239 GLboolean resident;
1240 if (ctx->Driver.IsTextureResident)
1241 resident = ctx->Driver.IsTextureResident(ctx, obj);
1242 else
1243 resident = GL_TRUE;
1244 *params = (GLint) resident;
1245 }
1246 return;
1247 case GL_TEXTURE_PRIORITY:
1248 *params = FLOAT_TO_INT(obj->Priority);
1249 return;
1250 case GL_TEXTURE_MIN_LOD:
1251 *params = (GLint) obj->MinLod;
1252 return;
1253 case GL_TEXTURE_MAX_LOD:
1254 *params = (GLint) obj->MaxLod;
1255 return;
1256 case GL_TEXTURE_BASE_LEVEL:
1257 *params = obj->BaseLevel;
1258 return;
1259 case GL_TEXTURE_MAX_LEVEL:
1260 *params = obj->MaxLevel;
1261 return;
1262 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
1263 if (ctx->Extensions.EXT_texture_filter_anisotropic) {
1264 *params = (GLint) obj->MaxAnisotropy;
1265 return;
1266 }
1267 break;
1268 case GL_TEXTURE_COMPARE_SGIX:
1269 if (ctx->Extensions.SGIX_shadow) {
1270 *params = (GLint) obj->CompareFlag;
1271 return;
1272 }
1273 break;
1274 case GL_TEXTURE_COMPARE_OPERATOR_SGIX:
1275 if (ctx->Extensions.SGIX_shadow) {
1276 *params = (GLint) obj->CompareOperator;
1277 return;
1278 }
1279 break;
1280 case GL_SHADOW_AMBIENT_SGIX: /* aka GL_TEXTURE_COMPARE_FAIL_VALUE_ARB */
1281 if (ctx->Extensions.SGIX_shadow_ambient) {
1282 *params = (GLint) FLOAT_TO_INT(obj->ShadowAmbient);
1283 return;
1284 }
1285 break;
1286 case GL_GENERATE_MIPMAP_SGIS:
1287 if (ctx->Extensions.SGIS_generate_mipmap) {
1288 *params = (GLint) obj->GenerateMipmap;
1289 return;
1290 }
1291 break;
1292 case GL_TEXTURE_COMPARE_MODE_ARB:
1293 if (ctx->Extensions.ARB_shadow) {
1294 *params = (GLint) obj->CompareMode;
1295 return;
1296 }
1297 break;
1298 case GL_TEXTURE_COMPARE_FUNC_ARB:
1299 if (ctx->Extensions.ARB_shadow) {
1300 *params = (GLint) obj->CompareFunc;
1301 return;
1302 }
1303 break;
1304 case GL_DEPTH_TEXTURE_MODE_ARB:
1305 if (ctx->Extensions.ARB_depth_texture) {
1306 *params = (GLint) obj->DepthMode;
1307 return;
1308 }
1309 break;
1310 case GL_TEXTURE_LOD_BIAS:
1311 if (ctx->Extensions.EXT_texture_lod_bias) {
1312 *params = (GLint) obj->LodBias;
1313 return;
1314 }
1315 break;
1316 default:
1317 ; /* silence warnings */
1318 }
1319 /* If we get here, pname was an unrecognized enum */
1320 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameteriv(pname=0x%x)", pname);
1321 }
1322
1323
1324
1325 /* GL_ARB_multitexture */
1326 void GLAPIENTRY
1327 _mesa_ActiveTextureARB(GLenum texture)
1328 {
1329 GET_CURRENT_CONTEXT(ctx);
1330 const GLuint texUnit = texture - GL_TEXTURE0;
1331 ASSERT_OUTSIDE_BEGIN_END(ctx);
1332
1333 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1334 _mesa_debug(ctx, "glActiveTexture %s\n",
1335 _mesa_lookup_enum_by_nr(texture));
1336
1337 /* XXX error-check against max(coordunits, imageunits) */
1338 if (texUnit >= ctx->Const.MaxTextureUnits) {
1339 _mesa_error(ctx, GL_INVALID_ENUM, "glActiveTexture(texture)");
1340 return;
1341 }
1342
1343 if (ctx->Texture.CurrentUnit == texUnit)
1344 return;
1345
1346 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1347
1348 ctx->Texture.CurrentUnit = texUnit;
1349 if (ctx->Transform.MatrixMode == GL_TEXTURE) {
1350 /* update current stack pointer */
1351 ctx->CurrentStack = &ctx->TextureMatrixStack[texUnit];
1352 }
1353
1354 if (ctx->Driver.ActiveTexture) {
1355 (*ctx->Driver.ActiveTexture)( ctx, (GLuint) texUnit );
1356 }
1357 }
1358
1359
1360 /* GL_ARB_multitexture */
1361 void GLAPIENTRY
1362 _mesa_ClientActiveTextureARB(GLenum texture)
1363 {
1364 GET_CURRENT_CONTEXT(ctx);
1365 GLuint texUnit = texture - GL_TEXTURE0;
1366 ASSERT_OUTSIDE_BEGIN_END(ctx);
1367
1368 if (texUnit >= ctx->Const.MaxTextureCoordUnits) {
1369 _mesa_error(ctx, GL_INVALID_ENUM, "glClientActiveTexture(texture)");
1370 return;
1371 }
1372
1373 FLUSH_VERTICES(ctx, _NEW_ARRAY);
1374 ctx->Array.ActiveTexture = texUnit;
1375 }
1376
1377
1378
1379 /**********************************************************************/
1380 /***** State management *****/
1381 /**********************************************************************/
1382
1383
1384 /**
1385 * \note This routine refers to derived texture attribute values to
1386 * compute the ENABLE_TEXMAT flags, but is only called on
1387 * _NEW_TEXTURE_MATRIX. On changes to _NEW_TEXTURE, the ENABLE_TEXMAT
1388 * flags are updated by _mesa_update_textures(), below.
1389 *
1390 * \param ctx GL context.
1391 */
1392 static void
1393 update_texture_matrices( GLcontext *ctx )
1394 {
1395 GLuint i;
1396
1397 ctx->Texture._TexMatEnabled = 0;
1398
1399 for (i=0; i < ctx->Const.MaxTextureUnits; i++) {
1400 if (_math_matrix_is_dirty(ctx->TextureMatrixStack[i].Top)) {
1401 _math_matrix_analyse( ctx->TextureMatrixStack[i].Top );
1402
1403 if (ctx->Texture.Unit[i]._ReallyEnabled &&
1404 ctx->TextureMatrixStack[i].Top->type != MATRIX_IDENTITY)
1405 ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(i);
1406
1407 if (ctx->Driver.TextureMatrix)
1408 ctx->Driver.TextureMatrix( ctx, i, ctx->TextureMatrixStack[i].Top);
1409 }
1410 }
1411 }
1412
1413
1414 /**
1415 * Update texture object's _Function field. We need to do this
1416 * whenever any of the texture object's shadow-related fields change
1417 * or when we start/stop using a fragment program.
1418 *
1419 * This function could be expanded someday to update additional per-object
1420 * fields that depend on assorted state changes.
1421 */
1422 static void
1423 update_texture_compare_function(GLcontext *ctx,
1424 struct gl_texture_object *tObj)
1425 {
1426 /* XXX temporarily disable this test since it breaks the GLSL
1427 * shadow2D(), etc. functions.
1428 */
1429 if (0 /*ctx->FragmentProgram._Current*/) {
1430 /* Texel/coordinate comparison is ignored for programs.
1431 * See GL_ARB_fragment_program/shader spec for details.
1432 */
1433 tObj->_Function = GL_NONE;
1434 }
1435 else if (tObj->CompareFlag) {
1436 /* GL_SGIX_shadow */
1437 if (tObj->CompareOperator == GL_TEXTURE_LEQUAL_R_SGIX) {
1438 tObj->_Function = GL_LEQUAL;
1439 }
1440 else {
1441 ASSERT(tObj->CompareOperator == GL_TEXTURE_GEQUAL_R_SGIX);
1442 tObj->_Function = GL_GEQUAL;
1443 }
1444 }
1445 else if (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
1446 /* GL_ARB_shadow */
1447 tObj->_Function = tObj->CompareFunc;
1448 }
1449 else {
1450 tObj->_Function = GL_NONE; /* pass depth through as grayscale */
1451 }
1452 }
1453
1454
1455 /**
1456 * Helper function for determining which texture object (1D, 2D, cube, etc)
1457 * should actually be used.
1458 */
1459 static void
1460 texture_override(GLcontext *ctx,
1461 struct gl_texture_unit *texUnit, GLbitfield enableBits,
1462 struct gl_texture_object *texObj, GLuint textureBit)
1463 {
1464 if (!texUnit->_ReallyEnabled && (enableBits & textureBit)) {
1465 if (!texObj->_Complete) {
1466 _mesa_test_texobj_completeness(ctx, texObj);
1467 }
1468 if (texObj->_Complete) {
1469 texUnit->_ReallyEnabled = textureBit;
1470 texUnit->_Current = texObj;
1471 update_texture_compare_function(ctx, texObj);
1472 }
1473 }
1474 }
1475
1476
1477 /**
1478 * \note This routine refers to derived texture matrix values to
1479 * compute the ENABLE_TEXMAT flags, but is only called on
1480 * _NEW_TEXTURE. On changes to _NEW_TEXTURE_MATRIX, the ENABLE_TEXMAT
1481 * flags are updated by _mesa_update_texture_matrices, above.
1482 *
1483 * \param ctx GL context.
1484 */
1485 static void
1486 update_texture_state( GLcontext *ctx )
1487 {
1488 GLuint unit;
1489 struct gl_fragment_program *fprog = NULL;
1490 struct gl_vertex_program *vprog = NULL;
1491
1492 if (ctx->Shader.CurrentProgram &&
1493 ctx->Shader.CurrentProgram->LinkStatus) {
1494 fprog = ctx->Shader.CurrentProgram->FragmentProgram;
1495 vprog = ctx->Shader.CurrentProgram->VertexProgram;
1496 }
1497 else {
1498 if (ctx->FragmentProgram._Enabled) {
1499 fprog = ctx->FragmentProgram.Current;
1500 }
1501 if (ctx->VertexProgram._Enabled) {
1502 /* XXX enable this if/when non-shader vertex programs get
1503 * texture fetches:
1504 vprog = ctx->VertexProgram.Current;
1505 */
1506 }
1507 }
1508
1509 ctx->NewState |= _NEW_TEXTURE; /* TODO: only set this if there are
1510 * actual changes.
1511 */
1512
1513 ctx->Texture._EnabledUnits = 0;
1514 ctx->Texture._GenFlags = 0;
1515 ctx->Texture._TexMatEnabled = 0;
1516 ctx->Texture._TexGenEnabled = 0;
1517
1518 /*
1519 * Update texture unit state.
1520 */
1521 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1522 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
1523 GLbitfield enableBits;
1524
1525 texUnit->_Current = NULL;
1526 texUnit->_ReallyEnabled = 0;
1527 texUnit->_GenFlags = 0;
1528
1529 /* Get the bitmask of texture enables.
1530 * enableBits will be a mask of the TEXTURE_*_BIT flags indicating
1531 * which texture targets are enabled (fixed function) or referenced
1532 * by a fragment shader/program. When multiple flags are set, we'll
1533 * settle on the one with highest priority (see texture_override below).
1534 */
1535 if (fprog || vprog) {
1536 enableBits = 0x0;
1537 if (fprog)
1538 enableBits |= fprog->Base.TexturesUsed[unit];
1539 if (vprog)
1540 enableBits |= vprog->Base.TexturesUsed[unit];
1541 }
1542 else {
1543 if (!texUnit->Enabled)
1544 continue;
1545 enableBits = texUnit->Enabled;
1546 }
1547
1548 /* Look for the highest-priority texture target that's enabled and
1549 * complete. That's the one we'll use for texturing. If we're using
1550 * a fragment program we're guaranteed that bitcount(enabledBits) <= 1.
1551 */
1552 texture_override(ctx, texUnit, enableBits,
1553 texUnit->Current2DArray, TEXTURE_2D_ARRAY_BIT);
1554 texture_override(ctx, texUnit, enableBits,
1555 texUnit->Current1DArray, TEXTURE_1D_ARRAY_BIT);
1556 texture_override(ctx, texUnit, enableBits,
1557 texUnit->CurrentCubeMap, TEXTURE_CUBE_BIT);
1558 texture_override(ctx, texUnit, enableBits,
1559 texUnit->Current3D, TEXTURE_3D_BIT);
1560 texture_override(ctx, texUnit, enableBits,
1561 texUnit->CurrentRect, TEXTURE_RECT_BIT);
1562 texture_override(ctx, texUnit, enableBits,
1563 texUnit->Current2D, TEXTURE_2D_BIT);
1564 texture_override(ctx, texUnit, enableBits,
1565 texUnit->Current1D, TEXTURE_1D_BIT);
1566
1567 if (!texUnit->_ReallyEnabled) {
1568 continue;
1569 }
1570
1571 if (texUnit->_ReallyEnabled)
1572 ctx->Texture._EnabledUnits |= (1 << unit);
1573
1574 if (texUnit->EnvMode == GL_COMBINE) {
1575 texUnit->_CurrentCombine = & texUnit->Combine;
1576 }
1577 else {
1578 const struct gl_texture_object *texObj = texUnit->_Current;
1579 GLenum format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
1580 if (format == GL_COLOR_INDEX) {
1581 format = GL_RGBA; /* a bit of a hack */
1582 }
1583 else if (format == GL_DEPTH_COMPONENT
1584 || format == GL_DEPTH_STENCIL_EXT) {
1585 format = texObj->DepthMode;
1586 }
1587 calculate_derived_texenv(&texUnit->_EnvMode, texUnit->EnvMode, format);
1588 texUnit->_CurrentCombine = & texUnit->_EnvMode;
1589 }
1590
1591 switch (texUnit->_CurrentCombine->ModeRGB) {
1592 case GL_REPLACE:
1593 texUnit->_CurrentCombine->_NumArgsRGB = 1;
1594 break;
1595 case GL_MODULATE:
1596 case GL_ADD:
1597 case GL_ADD_SIGNED:
1598 case GL_SUBTRACT:
1599 case GL_DOT3_RGB:
1600 case GL_DOT3_RGBA:
1601 case GL_DOT3_RGB_EXT:
1602 case GL_DOT3_RGBA_EXT:
1603 texUnit->_CurrentCombine->_NumArgsRGB = 2;
1604 break;
1605 case GL_INTERPOLATE:
1606 case GL_MODULATE_ADD_ATI:
1607 case GL_MODULATE_SIGNED_ADD_ATI:
1608 case GL_MODULATE_SUBTRACT_ATI:
1609 texUnit->_CurrentCombine->_NumArgsRGB = 3;
1610 break;
1611 default:
1612 texUnit->_CurrentCombine->_NumArgsRGB = 0;
1613 _mesa_problem(ctx, "invalid RGB combine mode in update_texture_state");
1614 return;
1615 }
1616
1617 switch (texUnit->_CurrentCombine->ModeA) {
1618 case GL_REPLACE:
1619 texUnit->_CurrentCombine->_NumArgsA = 1;
1620 break;
1621 case GL_MODULATE:
1622 case GL_ADD:
1623 case GL_ADD_SIGNED:
1624 case GL_SUBTRACT:
1625 texUnit->_CurrentCombine->_NumArgsA = 2;
1626 break;
1627 case GL_INTERPOLATE:
1628 case GL_MODULATE_ADD_ATI:
1629 case GL_MODULATE_SIGNED_ADD_ATI:
1630 case GL_MODULATE_SUBTRACT_ATI:
1631 texUnit->_CurrentCombine->_NumArgsA = 3;
1632 break;
1633 default:
1634 texUnit->_CurrentCombine->_NumArgsA = 0;
1635 _mesa_problem(ctx, "invalid Alpha combine mode in update_texture_state");
1636 break;
1637 }
1638
1639 if (texUnit->TexGenEnabled) {
1640 if (texUnit->TexGenEnabled & S_BIT) {
1641 texUnit->_GenFlags |= texUnit->_GenBitS;
1642 }
1643 if (texUnit->TexGenEnabled & T_BIT) {
1644 texUnit->_GenFlags |= texUnit->_GenBitT;
1645 }
1646 if (texUnit->TexGenEnabled & Q_BIT) {
1647 texUnit->_GenFlags |= texUnit->_GenBitQ;
1648 }
1649 if (texUnit->TexGenEnabled & R_BIT) {
1650 texUnit->_GenFlags |= texUnit->_GenBitR;
1651 }
1652
1653 ctx->Texture._TexGenEnabled |= ENABLE_TEXGEN(unit);
1654 ctx->Texture._GenFlags |= texUnit->_GenFlags;
1655 }
1656
1657 if (ctx->TextureMatrixStack[unit].Top->type != MATRIX_IDENTITY)
1658 ctx->Texture._TexMatEnabled |= ENABLE_TEXMAT(unit);
1659 }
1660
1661 /* Determine which texture coordinate sets are actually needed */
1662 if (fprog) {
1663 const GLuint coordMask = (1 << MAX_TEXTURE_COORD_UNITS) - 1;
1664 ctx->Texture._EnabledCoordUnits
1665 = (fprog->Base.InputsRead >> FRAG_ATTRIB_TEX0) & coordMask;
1666 }
1667 else {
1668 ctx->Texture._EnabledCoordUnits = ctx->Texture._EnabledUnits;
1669 }
1670 }
1671
1672
1673 /**
1674 * Update texture-related derived state.
1675 */
1676 void
1677 _mesa_update_texture( GLcontext *ctx, GLuint new_state )
1678 {
1679 if (new_state & _NEW_TEXTURE_MATRIX)
1680 update_texture_matrices( ctx );
1681
1682 if (new_state & (_NEW_TEXTURE | _NEW_PROGRAM))
1683 update_texture_state( ctx );
1684 }
1685
1686
1687 /**********************************************************************/
1688 /***** Initialization *****/
1689 /**********************************************************************/
1690
1691 /**
1692 * Allocate the proxy textures for the given context.
1693 *
1694 * \param ctx the context to allocate proxies for.
1695 *
1696 * \return GL_TRUE on success, or GL_FALSE on failure
1697 *
1698 * If run out of memory part way through the allocations, clean up and return
1699 * GL_FALSE.
1700 */
1701 static GLboolean
1702 alloc_proxy_textures( GLcontext *ctx )
1703 {
1704 ctx->Texture.Proxy1D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D);
1705 if (!ctx->Texture.Proxy1D)
1706 goto cleanup;
1707
1708 ctx->Texture.Proxy2D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D);
1709 if (!ctx->Texture.Proxy2D)
1710 goto cleanup;
1711
1712 ctx->Texture.Proxy3D = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_3D);
1713 if (!ctx->Texture.Proxy3D)
1714 goto cleanup;
1715
1716 ctx->Texture.ProxyCubeMap = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_CUBE_MAP_ARB);
1717 if (!ctx->Texture.ProxyCubeMap)
1718 goto cleanup;
1719
1720 ctx->Texture.ProxyRect = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_RECTANGLE_NV);
1721 if (!ctx->Texture.ProxyRect)
1722 goto cleanup;
1723
1724 ctx->Texture.Proxy1DArray = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_1D_ARRAY_EXT);
1725 if (!ctx->Texture.Proxy1DArray)
1726 goto cleanup;
1727
1728 ctx->Texture.Proxy2DArray = (*ctx->Driver.NewTextureObject)(ctx, 0, GL_TEXTURE_2D_ARRAY_EXT);
1729 if (!ctx->Texture.Proxy2DArray)
1730 goto cleanup;
1731
1732 return GL_TRUE;
1733
1734 cleanup:
1735 if (ctx->Texture.Proxy1D)
1736 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy1D);
1737 if (ctx->Texture.Proxy2D)
1738 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy2D);
1739 if (ctx->Texture.Proxy3D)
1740 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy3D);
1741 if (ctx->Texture.ProxyCubeMap)
1742 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.ProxyCubeMap);
1743 if (ctx->Texture.ProxyRect)
1744 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.ProxyRect);
1745 if (ctx->Texture.Proxy1DArray)
1746 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy1DArray);
1747 if (ctx->Texture.Proxy2DArray)
1748 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy2DArray);
1749 return GL_FALSE;
1750 }
1751
1752
1753 /**
1754 * Initialize a texture unit.
1755 *
1756 * \param ctx GL context.
1757 * \param unit texture unit number to be initialized.
1758 */
1759 static void
1760 init_texture_unit( GLcontext *ctx, GLuint unit )
1761 {
1762 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
1763
1764 texUnit->EnvMode = GL_MODULATE;
1765 ASSIGN_4V( texUnit->EnvColor, 0.0, 0.0, 0.0, 0.0 );
1766
1767 texUnit->Combine = default_combine_state;
1768 texUnit->_EnvMode = default_combine_state;
1769 texUnit->_CurrentCombine = & texUnit->_EnvMode;
1770
1771 texUnit->TexGenEnabled = 0;
1772 texUnit->GenModeS = GL_EYE_LINEAR;
1773 texUnit->GenModeT = GL_EYE_LINEAR;
1774 texUnit->GenModeR = GL_EYE_LINEAR;
1775 texUnit->GenModeQ = GL_EYE_LINEAR;
1776 texUnit->_GenBitS = TEXGEN_EYE_LINEAR;
1777 texUnit->_GenBitT = TEXGEN_EYE_LINEAR;
1778 texUnit->_GenBitR = TEXGEN_EYE_LINEAR;
1779 texUnit->_GenBitQ = TEXGEN_EYE_LINEAR;
1780
1781 /* Yes, these plane coefficients are correct! */
1782 ASSIGN_4V( texUnit->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
1783 ASSIGN_4V( texUnit->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
1784 ASSIGN_4V( texUnit->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
1785 ASSIGN_4V( texUnit->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
1786 ASSIGN_4V( texUnit->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
1787 ASSIGN_4V( texUnit->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
1788 ASSIGN_4V( texUnit->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
1789 ASSIGN_4V( texUnit->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
1790
1791 texUnit->Current1D = ctx->Shared->Default1D;
1792 texUnit->Current2D = ctx->Shared->Default2D;
1793 texUnit->Current3D = ctx->Shared->Default3D;
1794 texUnit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
1795 texUnit->CurrentRect = ctx->Shared->DefaultRect;
1796 texUnit->Current1DArray = ctx->Shared->Default1DArray;
1797 texUnit->Current2DArray = ctx->Shared->Default2DArray;
1798 }
1799
1800
1801 /**
1802 * Initialize texture state for the given context.
1803 */
1804 GLboolean
1805 _mesa_init_texture(GLcontext *ctx)
1806 {
1807 GLuint i;
1808
1809 assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
1810 assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
1811
1812 /* Effectively bind the default textures to all texture units */
1813 ctx->Shared->Default1D->RefCount += MAX_TEXTURE_UNITS;
1814 ctx->Shared->Default2D->RefCount += MAX_TEXTURE_UNITS;
1815 ctx->Shared->Default3D->RefCount += MAX_TEXTURE_UNITS;
1816 ctx->Shared->DefaultCubeMap->RefCount += MAX_TEXTURE_UNITS;
1817 ctx->Shared->DefaultRect->RefCount += MAX_TEXTURE_UNITS;
1818 ctx->Shared->Default1DArray->RefCount += MAX_TEXTURE_UNITS;
1819 ctx->Shared->Default2DArray->RefCount += MAX_TEXTURE_UNITS;
1820
1821 /* Texture group */
1822 ctx->Texture.CurrentUnit = 0; /* multitexture */
1823 ctx->Texture._EnabledUnits = 0;
1824 for (i=0; i<MAX_TEXTURE_UNITS; i++)
1825 init_texture_unit( ctx, i );
1826 ctx->Texture.SharedPalette = GL_FALSE;
1827 #if FEATURE_colortable
1828 _mesa_init_colortable(&ctx->Texture.Palette);
1829 #endif
1830
1831 /* Allocate proxy textures */
1832 if (!alloc_proxy_textures( ctx ))
1833 return GL_FALSE;
1834
1835 return GL_TRUE;
1836 }
1837
1838
1839 /**
1840 * Free dynamically-allocted texture data attached to the given context.
1841 */
1842 void
1843 _mesa_free_texture_data(GLcontext *ctx)
1844 {
1845 /* Free proxy texture objects */
1846 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy1D );
1847 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy2D );
1848 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy3D );
1849 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.ProxyCubeMap );
1850 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.ProxyRect );
1851 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy1DArray );
1852 (ctx->Driver.DeleteTexture)(ctx, ctx->Texture.Proxy2DArray );
1853
1854 #if FEATURE_colortable
1855 {
1856 GLuint i;
1857 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
1858 _mesa_free_colortable_data( &ctx->Texture.Unit[i].ColorTable );
1859 }
1860 #endif
1861 }