texparam.c revision 1a9e4d5113cf1fd608668506312167a676fcdb4f
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.5
4 *
5 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
6 * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26/**
27 * \file texparam.c
28 *
29 * glTexParameter-related functions
30 */
31
32
33#include "main/glheader.h"
34#include "main/colormac.h"
35#include "main/context.h"
36#include "main/enums.h"
37#include "main/formats.h"
38#include "main/glformats.h"
39#include "main/macros.h"
40#include "main/mfeatures.h"
41#include "main/mtypes.h"
42#include "main/state.h"
43#include "main/texcompress.h"
44#include "main/texobj.h"
45#include "main/texparam.h"
46#include "main/teximage.h"
47#include "main/texstate.h"
48#include "program/prog_instruction.h"
49
50
51/**
52 * Check if a coordinate wrap mode is supported for the texture target.
53 * \return GL_TRUE if legal, GL_FALSE otherwise
54 */
55static GLboolean
56validate_texture_wrap_mode(struct gl_context * ctx, GLenum target, GLenum wrap)
57{
58   const struct gl_extensions * const e = & ctx->Extensions;
59
60   if (target == GL_TEXTURE_RECTANGLE_NV) {
61      if (wrap == GL_CLAMP || wrap == GL_CLAMP_TO_EDGE ||
62          (wrap == GL_CLAMP_TO_BORDER && e->ARB_texture_border_clamp))
63         return GL_TRUE;
64   }
65   else if (target == GL_TEXTURE_EXTERNAL_OES) {
66      if (wrap == GL_CLAMP_TO_EDGE)
67         return GL_TRUE;
68   }
69   else {
70      switch (wrap) {
71      case GL_CLAMP:
72      case GL_REPEAT:
73      case GL_CLAMP_TO_EDGE:
74      case GL_MIRRORED_REPEAT:
75         return GL_TRUE;
76      case GL_CLAMP_TO_BORDER:
77         if (e->ARB_texture_border_clamp)
78            return GL_TRUE;
79         break;
80      case GL_MIRROR_CLAMP_EXT:
81      case GL_MIRROR_CLAMP_TO_EDGE_EXT:
82         if (e->ATI_texture_mirror_once || e->EXT_texture_mirror_clamp)
83            return GL_TRUE;
84         break;
85      case GL_MIRROR_CLAMP_TO_BORDER_EXT:
86         if (e->EXT_texture_mirror_clamp)
87            return GL_TRUE;
88         break;
89      default:
90         break;
91      }
92   }
93
94   _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(param=0x%x)", wrap );
95   return GL_FALSE;
96}
97
98
99/**
100 * Get current texture object for given target.
101 * Return NULL if any error (and record the error).
102 * Note that this is different from _mesa_select_tex_object() in that proxy
103 * targets are not accepted.
104 * Only the glGetTexLevelParameter() functions accept proxy targets.
105 */
106static struct gl_texture_object *
107get_texobj(struct gl_context *ctx, GLenum target, GLboolean get)
108{
109   struct gl_texture_unit *texUnit;
110
111   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
112      _mesa_error(ctx, GL_INVALID_OPERATION,
113                  "gl%sTexParameter(current unit)", get ? "Get" : "");
114      return NULL;
115   }
116
117   texUnit = _mesa_get_current_tex_unit(ctx);
118
119   switch (target) {
120   case GL_TEXTURE_1D:
121      return texUnit->CurrentTex[TEXTURE_1D_INDEX];
122   case GL_TEXTURE_2D:
123      return texUnit->CurrentTex[TEXTURE_2D_INDEX];
124   case GL_TEXTURE_3D:
125      return texUnit->CurrentTex[TEXTURE_3D_INDEX];
126   case GL_TEXTURE_CUBE_MAP:
127      if (ctx->Extensions.ARB_texture_cube_map) {
128         return texUnit->CurrentTex[TEXTURE_CUBE_INDEX];
129      }
130      break;
131   case GL_TEXTURE_RECTANGLE_NV:
132      if (ctx->Extensions.NV_texture_rectangle) {
133         return texUnit->CurrentTex[TEXTURE_RECT_INDEX];
134      }
135      break;
136   case GL_TEXTURE_1D_ARRAY_EXT:
137      if (ctx->Extensions.MESA_texture_array ||
138          ctx->Extensions.EXT_texture_array) {
139         return texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX];
140      }
141      break;
142   case GL_TEXTURE_2D_ARRAY_EXT:
143      if (ctx->Extensions.MESA_texture_array ||
144          ctx->Extensions.EXT_texture_array) {
145         return texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX];
146      }
147      break;
148   case GL_TEXTURE_EXTERNAL_OES:
149      if (ctx->Extensions.OES_EGL_image_external) {
150         return texUnit->CurrentTex[TEXTURE_EXTERNAL_INDEX];
151      }
152      break;
153   default:
154      ;
155   }
156
157   _mesa_error(ctx, GL_INVALID_ENUM,
158                  "gl%sTexParameter(target)", get ? "Get" : "");
159   return NULL;
160}
161
162
163/**
164 * Convert GL_RED/GREEN/BLUE/ALPHA/ZERO/ONE to SWIZZLE_X/Y/Z/W/ZERO/ONE.
165 * \return -1 if error.
166 */
167static GLint
168comp_to_swizzle(GLenum comp)
169{
170   switch (comp) {
171   case GL_RED:
172      return SWIZZLE_X;
173   case GL_GREEN:
174      return SWIZZLE_Y;
175   case GL_BLUE:
176      return SWIZZLE_Z;
177   case GL_ALPHA:
178      return SWIZZLE_W;
179   case GL_ZERO:
180      return SWIZZLE_ZERO;
181   case GL_ONE:
182      return SWIZZLE_ONE;
183   default:
184      return -1;
185   }
186}
187
188
189static void
190set_swizzle_component(GLuint *swizzle, GLuint comp, GLuint swz)
191{
192   ASSERT(comp < 4);
193   ASSERT(swz <= SWIZZLE_NIL);
194   {
195      GLuint mask = 0x7 << (3 * comp);
196      GLuint s = (*swizzle & ~mask) | (swz << (3 * comp));
197      *swizzle = s;
198   }
199}
200
201
202/**
203 * This is called just prior to changing any texture object state which
204 * will not effect texture completeness.
205 */
206static inline void
207flush(struct gl_context *ctx)
208{
209   FLUSH_VERTICES(ctx, _NEW_TEXTURE);
210}
211
212
213/**
214 * This is called just prior to changing any texture object state which
215 * can effect texture completeness (texture base level, max level).
216 * Any pending rendering will be flushed out, we'll set the _NEW_TEXTURE
217 * state flag and then mark the texture object as 'incomplete' so that any
218 * per-texture derived state gets recomputed.
219 */
220static inline void
221incomplete(struct gl_context *ctx, struct gl_texture_object *texObj)
222{
223   FLUSH_VERTICES(ctx, _NEW_TEXTURE);
224   _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
225}
226
227
228/**
229 * Set an integer-valued texture parameter
230 * \return GL_TRUE if legal AND the value changed, GL_FALSE otherwise
231 */
232static GLboolean
233set_tex_parameteri(struct gl_context *ctx,
234                   struct gl_texture_object *texObj,
235                   GLenum pname, const GLint *params)
236{
237   switch (pname) {
238   case GL_TEXTURE_MIN_FILTER:
239      if (texObj->Sampler.MinFilter == params[0])
240         return GL_FALSE;
241      switch (params[0]) {
242      case GL_NEAREST:
243      case GL_LINEAR:
244         flush(ctx);
245         texObj->Sampler.MinFilter = params[0];
246         return GL_TRUE;
247      case GL_NEAREST_MIPMAP_NEAREST:
248      case GL_LINEAR_MIPMAP_NEAREST:
249      case GL_NEAREST_MIPMAP_LINEAR:
250      case GL_LINEAR_MIPMAP_LINEAR:
251         if (texObj->Target != GL_TEXTURE_RECTANGLE_NV &&
252             texObj->Target != GL_TEXTURE_EXTERNAL_OES) {
253            flush(ctx);
254            texObj->Sampler.MinFilter = params[0];
255            return GL_TRUE;
256         }
257         /* fall-through */
258      default:
259         goto invalid_param;
260      }
261      return GL_FALSE;
262
263   case GL_TEXTURE_MAG_FILTER:
264      if (texObj->Sampler.MagFilter == params[0])
265         return GL_FALSE;
266      switch (params[0]) {
267      case GL_NEAREST:
268      case GL_LINEAR:
269         flush(ctx); /* does not effect completeness */
270         texObj->Sampler.MagFilter = params[0];
271         return GL_TRUE;
272      default:
273         goto invalid_param;
274      }
275      return GL_FALSE;
276
277   case GL_TEXTURE_WRAP_S:
278      if (texObj->Sampler.WrapS == params[0])
279         return GL_FALSE;
280      if (validate_texture_wrap_mode(ctx, texObj->Target, params[0])) {
281         flush(ctx);
282         texObj->Sampler.WrapS = params[0];
283         return GL_TRUE;
284      }
285      return GL_FALSE;
286
287   case GL_TEXTURE_WRAP_T:
288      if (texObj->Sampler.WrapT == params[0])
289         return GL_FALSE;
290      if (validate_texture_wrap_mode(ctx, texObj->Target, params[0])) {
291         flush(ctx);
292         texObj->Sampler.WrapT = params[0];
293         return GL_TRUE;
294      }
295      return GL_FALSE;
296
297   case GL_TEXTURE_WRAP_R:
298      if (texObj->Sampler.WrapR == params[0])
299         return GL_FALSE;
300      if (validate_texture_wrap_mode(ctx, texObj->Target, params[0])) {
301         flush(ctx);
302         texObj->Sampler.WrapR = params[0];
303         return GL_TRUE;
304      }
305      return GL_FALSE;
306
307   case GL_TEXTURE_BASE_LEVEL:
308      if (texObj->BaseLevel == params[0])
309         return GL_FALSE;
310      if (params[0] < 0 ||
311          (texObj->Target == GL_TEXTURE_RECTANGLE_ARB && params[0] != 0)) {
312         _mesa_error(ctx, GL_INVALID_VALUE,
313                     "glTexParameter(param=%d)", params[0]);
314         return GL_FALSE;
315      }
316      incomplete(ctx, texObj);
317      texObj->BaseLevel = params[0];
318      return GL_TRUE;
319
320   case GL_TEXTURE_MAX_LEVEL:
321      if (texObj->MaxLevel == params[0])
322         return GL_FALSE;
323      if (params[0] < 0 || texObj->Target == GL_TEXTURE_RECTANGLE_ARB) {
324         _mesa_error(ctx, GL_INVALID_OPERATION,
325                     "glTexParameter(param=%d)", params[0]);
326         return GL_FALSE;
327      }
328      incomplete(ctx, texObj);
329      texObj->MaxLevel = params[0];
330      return GL_TRUE;
331
332   case GL_GENERATE_MIPMAP_SGIS:
333      if (params[0] && texObj->Target == GL_TEXTURE_EXTERNAL_OES)
334         goto invalid_param;
335      if (texObj->GenerateMipmap != params[0]) {
336         /* no flush() */
337	 texObj->GenerateMipmap = params[0] ? GL_TRUE : GL_FALSE;
338	 return GL_TRUE;
339      }
340      return GL_FALSE;
341
342   case GL_TEXTURE_COMPARE_MODE_ARB:
343      if (ctx->Extensions.ARB_shadow) {
344         if (texObj->Sampler.CompareMode == params[0])
345            return GL_FALSE;
346         if (params[0] == GL_NONE ||
347             params[0] == GL_COMPARE_R_TO_TEXTURE_ARB) {
348            flush(ctx);
349            texObj->Sampler.CompareMode = params[0];
350            return GL_TRUE;
351         }
352         goto invalid_param;
353      }
354      goto invalid_pname;
355
356   case GL_TEXTURE_COMPARE_FUNC_ARB:
357      if (ctx->Extensions.ARB_shadow) {
358         if (texObj->Sampler.CompareFunc == params[0])
359            return GL_FALSE;
360         switch (params[0]) {
361         case GL_LEQUAL:
362         case GL_GEQUAL:
363            flush(ctx);
364            texObj->Sampler.CompareFunc = params[0];
365            return GL_TRUE;
366         case GL_EQUAL:
367         case GL_NOTEQUAL:
368         case GL_LESS:
369         case GL_GREATER:
370         case GL_ALWAYS:
371         case GL_NEVER:
372            if (ctx->Extensions.EXT_shadow_funcs) {
373               flush(ctx);
374               texObj->Sampler.CompareFunc = params[0];
375               return GL_TRUE;
376            }
377            /* fall-through */
378         default:
379            goto invalid_param;
380         }
381      }
382      goto invalid_pname;
383
384   case GL_DEPTH_TEXTURE_MODE_ARB:
385      if (ctx->Extensions.ARB_depth_texture) {
386         if (texObj->DepthMode == params[0])
387            return GL_FALSE;
388         if (params[0] == GL_LUMINANCE ||
389             params[0] == GL_INTENSITY ||
390             params[0] == GL_ALPHA ||
391             (ctx->Extensions.ARB_texture_rg && params[0] == GL_RED)) {
392            flush(ctx);
393            texObj->DepthMode = params[0];
394            return GL_TRUE;
395         }
396         goto invalid_param;
397      }
398      goto invalid_pname;
399
400#if FEATURE_OES_draw_texture
401   case GL_TEXTURE_CROP_RECT_OES:
402      texObj->CropRect[0] = params[0];
403      texObj->CropRect[1] = params[1];
404      texObj->CropRect[2] = params[2];
405      texObj->CropRect[3] = params[3];
406      return GL_TRUE;
407#endif
408
409   case GL_TEXTURE_SWIZZLE_R_EXT:
410   case GL_TEXTURE_SWIZZLE_G_EXT:
411   case GL_TEXTURE_SWIZZLE_B_EXT:
412   case GL_TEXTURE_SWIZZLE_A_EXT:
413      if (ctx->Extensions.EXT_texture_swizzle) {
414         const GLuint comp = pname - GL_TEXTURE_SWIZZLE_R_EXT;
415         const GLint swz = comp_to_swizzle(params[0]);
416         if (swz < 0) {
417            _mesa_error(ctx, GL_INVALID_OPERATION,
418                        "glTexParameter(swizzle 0x%x)", params[0]);
419            return GL_FALSE;
420         }
421         ASSERT(comp < 4);
422
423         flush(ctx);
424         texObj->Swizzle[comp] = params[0];
425         set_swizzle_component(&texObj->_Swizzle, comp, swz);
426         return GL_TRUE;
427      }
428      goto invalid_pname;
429
430   case GL_TEXTURE_SWIZZLE_RGBA_EXT:
431      if (ctx->Extensions.EXT_texture_swizzle) {
432         GLuint comp;
433         flush(ctx);
434         for (comp = 0; comp < 4; comp++) {
435            const GLint swz = comp_to_swizzle(params[comp]);
436            if (swz >= 0) {
437               texObj->Swizzle[comp] = params[comp];
438               set_swizzle_component(&texObj->_Swizzle, comp, swz);
439            }
440            else {
441               _mesa_error(ctx, GL_INVALID_OPERATION,
442                           "glTexParameter(swizzle 0x%x)", params[comp]);
443               return GL_FALSE;
444            }
445         }
446         return GL_TRUE;
447      }
448      goto invalid_pname;
449
450   case GL_TEXTURE_SRGB_DECODE_EXT:
451      if (ctx->Extensions.EXT_texture_sRGB_decode) {
452	 GLenum decode = params[0];
453	 if (decode == GL_DECODE_EXT || decode == GL_SKIP_DECODE_EXT) {
454	    if (texObj->Sampler.sRGBDecode != decode) {
455	       flush(ctx);
456	       texObj->Sampler.sRGBDecode = decode;
457	    }
458	    return GL_TRUE;
459	 }
460      }
461      goto invalid_pname;
462
463   case GL_TEXTURE_CUBE_MAP_SEAMLESS:
464      if (ctx->Extensions.AMD_seamless_cubemap_per_texture) {
465         GLenum param = params[0];
466         if (param != GL_TRUE && param != GL_FALSE) {
467            goto invalid_param;
468         }
469         if (param != texObj->Sampler.CubeMapSeamless) {
470            flush(ctx);
471            texObj->Sampler.CubeMapSeamless = param;
472         }
473         return GL_TRUE;
474      }
475      goto invalid_pname;
476
477   default:
478      goto invalid_pname;
479   }
480
481invalid_pname:
482   _mesa_error(ctx, GL_INVALID_ENUM, "glTexParameter(pname=%s)",
483               _mesa_lookup_enum_by_nr(pname));
484   return GL_FALSE;
485
486invalid_param:
487   _mesa_error(ctx, GL_INVALID_ENUM, "glTexParameter(param=%s)",
488               _mesa_lookup_enum_by_nr(params[0]));
489   return GL_FALSE;
490}
491
492
493/**
494 * Set a float-valued texture parameter
495 * \return GL_TRUE if legal AND the value changed, GL_FALSE otherwise
496 */
497static GLboolean
498set_tex_parameterf(struct gl_context *ctx,
499                   struct gl_texture_object *texObj,
500                   GLenum pname, const GLfloat *params)
501{
502   switch (pname) {
503   case GL_TEXTURE_MIN_LOD:
504      if (texObj->Sampler.MinLod == params[0])
505         return GL_FALSE;
506      flush(ctx);
507      texObj->Sampler.MinLod = params[0];
508      return GL_TRUE;
509
510   case GL_TEXTURE_MAX_LOD:
511      if (texObj->Sampler.MaxLod == params[0])
512         return GL_FALSE;
513      flush(ctx);
514      texObj->Sampler.MaxLod = params[0];
515      return GL_TRUE;
516
517   case GL_TEXTURE_PRIORITY:
518      flush(ctx);
519      texObj->Priority = CLAMP(params[0], 0.0F, 1.0F);
520      return GL_TRUE;
521
522   case GL_TEXTURE_MAX_ANISOTROPY_EXT:
523      if (ctx->Extensions.EXT_texture_filter_anisotropic) {
524         if (texObj->Sampler.MaxAnisotropy == params[0])
525            return GL_FALSE;
526         if (params[0] < 1.0) {
527            _mesa_error(ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
528            return GL_FALSE;
529         }
530         flush(ctx);
531         /* clamp to max, that's what NVIDIA does */
532         texObj->Sampler.MaxAnisotropy = MIN2(params[0],
533                                      ctx->Const.MaxTextureMaxAnisotropy);
534         return GL_TRUE;
535      }
536      else {
537         static GLuint count = 0;
538         if (count++ < 10)
539            _mesa_error(ctx, GL_INVALID_ENUM,
540                        "glTexParameter(pname=GL_TEXTURE_MAX_ANISOTROPY_EXT)");
541      }
542      return GL_FALSE;
543
544   case GL_TEXTURE_LOD_BIAS:
545      /* NOTE: this is really part of OpenGL 1.4, not EXT_texture_lod_bias */
546      if (texObj->Sampler.LodBias != params[0]) {
547	 flush(ctx);
548	 texObj->Sampler.LodBias = params[0];
549	 return GL_TRUE;
550      }
551      break;
552
553   case GL_TEXTURE_BORDER_COLOR:
554      flush(ctx);
555      /* ARB_texture_float disables clamping */
556      if (ctx->Extensions.ARB_texture_float) {
557         texObj->Sampler.BorderColor.f[RCOMP] = params[0];
558         texObj->Sampler.BorderColor.f[GCOMP] = params[1];
559         texObj->Sampler.BorderColor.f[BCOMP] = params[2];
560         texObj->Sampler.BorderColor.f[ACOMP] = params[3];
561      } else {
562         texObj->Sampler.BorderColor.f[RCOMP] = CLAMP(params[0], 0.0F, 1.0F);
563         texObj->Sampler.BorderColor.f[GCOMP] = CLAMP(params[1], 0.0F, 1.0F);
564         texObj->Sampler.BorderColor.f[BCOMP] = CLAMP(params[2], 0.0F, 1.0F);
565         texObj->Sampler.BorderColor.f[ACOMP] = CLAMP(params[3], 0.0F, 1.0F);
566      }
567      return GL_TRUE;
568
569   default:
570      _mesa_error(ctx, GL_INVALID_ENUM, "glTexParameter(pname=0x%x)", pname);
571   }
572   return GL_FALSE;
573}
574
575
576void GLAPIENTRY
577_mesa_TexParameterf(GLenum target, GLenum pname, GLfloat param)
578{
579   GLboolean need_update;
580   struct gl_texture_object *texObj;
581   GET_CURRENT_CONTEXT(ctx);
582   ASSERT_OUTSIDE_BEGIN_END(ctx);
583
584   texObj = get_texobj(ctx, target, GL_FALSE);
585   if (!texObj)
586      return;
587
588   switch (pname) {
589   case GL_TEXTURE_MIN_FILTER:
590   case GL_TEXTURE_MAG_FILTER:
591   case GL_TEXTURE_WRAP_S:
592   case GL_TEXTURE_WRAP_T:
593   case GL_TEXTURE_WRAP_R:
594   case GL_TEXTURE_BASE_LEVEL:
595   case GL_TEXTURE_MAX_LEVEL:
596   case GL_GENERATE_MIPMAP_SGIS:
597   case GL_TEXTURE_COMPARE_MODE_ARB:
598   case GL_TEXTURE_COMPARE_FUNC_ARB:
599   case GL_DEPTH_TEXTURE_MODE_ARB:
600   case GL_TEXTURE_SRGB_DECODE_EXT:
601   case GL_TEXTURE_CUBE_MAP_SEAMLESS:
602      {
603         /* convert float param to int */
604         GLint p[4];
605         p[0] = (GLint) param;
606         p[1] = p[2] = p[3] = 0;
607         need_update = set_tex_parameteri(ctx, texObj, pname, p);
608      }
609      break;
610   case GL_TEXTURE_SWIZZLE_R_EXT:
611   case GL_TEXTURE_SWIZZLE_G_EXT:
612   case GL_TEXTURE_SWIZZLE_B_EXT:
613   case GL_TEXTURE_SWIZZLE_A_EXT:
614      {
615         GLint p[4];
616         p[0] = (GLint) param;
617         p[1] = p[2] = p[3] = 0;
618         need_update = set_tex_parameteri(ctx, texObj, pname, p);
619      }
620      break;
621   default:
622      {
623         /* this will generate an error if pname is illegal */
624         GLfloat p[4];
625         p[0] = param;
626         p[1] = p[2] = p[3] = 0.0F;
627         need_update = set_tex_parameterf(ctx, texObj, pname, p);
628      }
629   }
630
631   if (ctx->Driver.TexParameter && need_update) {
632      ctx->Driver.TexParameter(ctx, target, texObj, pname, &param);
633   }
634}
635
636
637void GLAPIENTRY
638_mesa_TexParameterfv(GLenum target, GLenum pname, const GLfloat *params)
639{
640   GLboolean need_update;
641   struct gl_texture_object *texObj;
642   GET_CURRENT_CONTEXT(ctx);
643   ASSERT_OUTSIDE_BEGIN_END(ctx);
644
645   texObj = get_texobj(ctx, target, GL_FALSE);
646   if (!texObj)
647      return;
648
649   switch (pname) {
650   case GL_TEXTURE_MIN_FILTER:
651   case GL_TEXTURE_MAG_FILTER:
652   case GL_TEXTURE_WRAP_S:
653   case GL_TEXTURE_WRAP_T:
654   case GL_TEXTURE_WRAP_R:
655   case GL_TEXTURE_BASE_LEVEL:
656   case GL_TEXTURE_MAX_LEVEL:
657   case GL_GENERATE_MIPMAP_SGIS:
658   case GL_TEXTURE_COMPARE_MODE_ARB:
659   case GL_TEXTURE_COMPARE_FUNC_ARB:
660   case GL_DEPTH_TEXTURE_MODE_ARB:
661   case GL_TEXTURE_SRGB_DECODE_EXT:
662   case GL_TEXTURE_CUBE_MAP_SEAMLESS:
663      {
664         /* convert float param to int */
665         GLint p[4];
666         p[0] = (GLint) params[0];
667         p[1] = p[2] = p[3] = 0;
668         need_update = set_tex_parameteri(ctx, texObj, pname, p);
669      }
670      break;
671
672#if FEATURE_OES_draw_texture
673   case GL_TEXTURE_CROP_RECT_OES:
674      {
675         /* convert float params to int */
676         GLint iparams[4];
677         iparams[0] = (GLint) params[0];
678         iparams[1] = (GLint) params[1];
679         iparams[2] = (GLint) params[2];
680         iparams[3] = (GLint) params[3];
681         need_update = set_tex_parameteri(ctx, texObj, pname, iparams);
682      }
683      break;
684#endif
685
686   case GL_TEXTURE_SWIZZLE_R_EXT:
687   case GL_TEXTURE_SWIZZLE_G_EXT:
688   case GL_TEXTURE_SWIZZLE_B_EXT:
689   case GL_TEXTURE_SWIZZLE_A_EXT:
690   case GL_TEXTURE_SWIZZLE_RGBA_EXT:
691      {
692         GLint p[4] = {0, 0, 0, 0};
693         p[0] = (GLint) params[0];
694         if (pname == GL_TEXTURE_SWIZZLE_RGBA_EXT) {
695            p[1] = (GLint) params[1];
696            p[2] = (GLint) params[2];
697            p[3] = (GLint) params[3];
698         }
699         need_update = set_tex_parameteri(ctx, texObj, pname, p);
700      }
701      break;
702   default:
703      /* this will generate an error if pname is illegal */
704      need_update = set_tex_parameterf(ctx, texObj, pname, params);
705   }
706
707   if (ctx->Driver.TexParameter && need_update) {
708      ctx->Driver.TexParameter(ctx, target, texObj, pname, params);
709   }
710}
711
712
713void GLAPIENTRY
714_mesa_TexParameteri(GLenum target, GLenum pname, GLint param)
715{
716   GLboolean need_update;
717   struct gl_texture_object *texObj;
718   GET_CURRENT_CONTEXT(ctx);
719   ASSERT_OUTSIDE_BEGIN_END(ctx);
720
721   texObj = get_texobj(ctx, target, GL_FALSE);
722   if (!texObj)
723      return;
724
725   switch (pname) {
726   case GL_TEXTURE_MIN_LOD:
727   case GL_TEXTURE_MAX_LOD:
728   case GL_TEXTURE_PRIORITY:
729   case GL_TEXTURE_MAX_ANISOTROPY_EXT:
730   case GL_TEXTURE_LOD_BIAS:
731   case GL_TEXTURE_COMPARE_FAIL_VALUE_ARB:
732      {
733         GLfloat fparam[4];
734         fparam[0] = (GLfloat) param;
735         fparam[1] = fparam[2] = fparam[3] = 0.0F;
736         /* convert int param to float */
737         need_update = set_tex_parameterf(ctx, texObj, pname, fparam);
738      }
739      break;
740   default:
741      /* this will generate an error if pname is illegal */
742      {
743         GLint iparam[4];
744         iparam[0] = param;
745         iparam[1] = iparam[2] = iparam[3] = 0;
746         need_update = set_tex_parameteri(ctx, texObj, pname, iparam);
747      }
748   }
749
750   if (ctx->Driver.TexParameter && need_update) {
751      GLfloat fparam = (GLfloat) param;
752      ctx->Driver.TexParameter(ctx, target, texObj, pname, &fparam);
753   }
754}
755
756
757void GLAPIENTRY
758_mesa_TexParameteriv(GLenum target, GLenum pname, const GLint *params)
759{
760   GLboolean need_update;
761   struct gl_texture_object *texObj;
762   GET_CURRENT_CONTEXT(ctx);
763   ASSERT_OUTSIDE_BEGIN_END(ctx);
764
765   texObj = get_texobj(ctx, target, GL_FALSE);
766   if (!texObj)
767      return;
768
769   switch (pname) {
770   case GL_TEXTURE_BORDER_COLOR:
771      {
772         /* convert int params to float */
773         GLfloat fparams[4];
774         fparams[0] = INT_TO_FLOAT(params[0]);
775         fparams[1] = INT_TO_FLOAT(params[1]);
776         fparams[2] = INT_TO_FLOAT(params[2]);
777         fparams[3] = INT_TO_FLOAT(params[3]);
778         need_update = set_tex_parameterf(ctx, texObj, pname, fparams);
779      }
780      break;
781   case GL_TEXTURE_MIN_LOD:
782   case GL_TEXTURE_MAX_LOD:
783   case GL_TEXTURE_PRIORITY:
784   case GL_TEXTURE_MAX_ANISOTROPY_EXT:
785   case GL_TEXTURE_LOD_BIAS:
786   case GL_TEXTURE_COMPARE_FAIL_VALUE_ARB:
787      {
788         /* convert int param to float */
789         GLfloat fparams[4];
790         fparams[0] = (GLfloat) params[0];
791         fparams[1] = fparams[2] = fparams[3] = 0.0F;
792         need_update = set_tex_parameterf(ctx, texObj, pname, fparams);
793      }
794      break;
795   default:
796      /* this will generate an error if pname is illegal */
797      need_update = set_tex_parameteri(ctx, texObj, pname, params);
798   }
799
800   if (ctx->Driver.TexParameter && need_update) {
801      GLfloat fparams[4];
802      fparams[0] = INT_TO_FLOAT(params[0]);
803      if (pname == GL_TEXTURE_BORDER_COLOR ||
804          pname == GL_TEXTURE_CROP_RECT_OES) {
805         fparams[1] = INT_TO_FLOAT(params[1]);
806         fparams[2] = INT_TO_FLOAT(params[2]);
807         fparams[3] = INT_TO_FLOAT(params[3]);
808      }
809      ctx->Driver.TexParameter(ctx, target, texObj, pname, fparams);
810   }
811}
812
813
814/**
815 * Set tex parameter to integer value(s).  Primarily intended to set
816 * integer-valued texture border color (for integer-valued textures).
817 * New in GL 3.0.
818 */
819void GLAPIENTRY
820_mesa_TexParameterIiv(GLenum target, GLenum pname, const GLint *params)
821{
822   struct gl_texture_object *texObj;
823   GET_CURRENT_CONTEXT(ctx);
824   ASSERT_OUTSIDE_BEGIN_END(ctx);
825
826   texObj = get_texobj(ctx, target, GL_FALSE);
827   if (!texObj)
828      return;
829
830   switch (pname) {
831   case GL_TEXTURE_BORDER_COLOR:
832      FLUSH_VERTICES(ctx, _NEW_TEXTURE);
833      /* set the integer-valued border color */
834      COPY_4V(texObj->Sampler.BorderColor.i, params);
835      break;
836   default:
837      _mesa_TexParameteriv(target, pname, params);
838      break;
839   }
840   /* XXX no driver hook for TexParameterIiv() yet */
841}
842
843
844/**
845 * Set tex parameter to unsigned integer value(s).  Primarily intended to set
846 * uint-valued texture border color (for integer-valued textures).
847 * New in GL 3.0
848 */
849void GLAPIENTRY
850_mesa_TexParameterIuiv(GLenum target, GLenum pname, const GLuint *params)
851{
852   struct gl_texture_object *texObj;
853   GET_CURRENT_CONTEXT(ctx);
854   ASSERT_OUTSIDE_BEGIN_END(ctx);
855
856   texObj = get_texobj(ctx, target, GL_FALSE);
857   if (!texObj)
858      return;
859
860   switch (pname) {
861   case GL_TEXTURE_BORDER_COLOR:
862      FLUSH_VERTICES(ctx, _NEW_TEXTURE);
863      /* set the unsigned integer-valued border color */
864      COPY_4V(texObj->Sampler.BorderColor.ui, params);
865      break;
866   default:
867      _mesa_TexParameteriv(target, pname, (const GLint *) params);
868      break;
869   }
870   /* XXX no driver hook for TexParameterIuiv() yet */
871}
872
873
874static GLboolean
875legal_get_tex_level_parameter_target(struct gl_context *ctx, GLenum target)
876{
877   switch (target) {
878   case GL_TEXTURE_1D:
879   case GL_PROXY_TEXTURE_1D:
880   case GL_TEXTURE_2D:
881   case GL_PROXY_TEXTURE_2D:
882   case GL_TEXTURE_3D:
883   case GL_PROXY_TEXTURE_3D:
884      return GL_TRUE;
885   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
886   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
887   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
888   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
889   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
890   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
891   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
892      return ctx->Extensions.ARB_texture_cube_map;
893   case GL_TEXTURE_RECTANGLE_NV:
894   case GL_PROXY_TEXTURE_RECTANGLE_NV:
895      return ctx->Extensions.NV_texture_rectangle;
896   case GL_TEXTURE_1D_ARRAY_EXT:
897   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
898   case GL_TEXTURE_2D_ARRAY_EXT:
899   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
900      return (ctx->Extensions.MESA_texture_array ||
901              ctx->Extensions.EXT_texture_array);
902   case GL_TEXTURE_BUFFER:
903      /* GetTexLevelParameter accepts GL_TEXTURE_BUFFER in GL 3.1+ contexts,
904       * but not in earlier versions that expose ARB_texture_buffer_object.
905       *
906       * From the ARB_texture_buffer_object spec:
907       * "(7) Do buffer textures support texture parameters (TexParameter) or
908       *      queries (GetTexParameter, GetTexLevelParameter, GetTexImage)?
909       *
910       *    RESOLVED:  No. [...] Note that the spec edits above don't add
911       *    explicit error language for any of these cases.  That is because
912       *    each of the functions enumerate the set of valid <target>
913       *    parameters.  Not editing the spec to allow TEXTURE_BUFFER_ARB in
914       *    these cases means that target is not legal, and an INVALID_ENUM
915       *    error should be generated."
916       *
917       * From the OpenGL 3.1 spec:
918       * "target may also be TEXTURE_BUFFER, indicating the texture buffer."
919       */
920      return _mesa_is_desktop_gl(ctx) && ctx->Version >= 31;
921   default:
922      return GL_FALSE;
923   }
924}
925
926
927static void
928get_tex_level_parameter_image(struct gl_context *ctx,
929                              const struct gl_texture_object *texObj,
930                              GLenum target, GLint level,
931                              GLenum pname, GLint *params)
932{
933   const struct gl_texture_image *img = NULL;
934   gl_format texFormat;
935
936   img = _mesa_select_tex_image(ctx, texObj, target, level);
937   if (!img || img->TexFormat == MESA_FORMAT_NONE) {
938      /* undefined texture image */
939      if (pname == GL_TEXTURE_COMPONENTS)
940         *params = 1;
941      else
942         *params = 0;
943      return;
944   }
945
946   texFormat = img->TexFormat;
947
948   switch (pname) {
949      case GL_TEXTURE_WIDTH:
950         *params = img->Width;
951         break;
952      case GL_TEXTURE_HEIGHT:
953         *params = img->Height;
954         break;
955      case GL_TEXTURE_DEPTH:
956         *params = img->Depth;
957         break;
958      case GL_TEXTURE_INTERNAL_FORMAT:
959         if (_mesa_is_format_compressed(texFormat)) {
960            /* need to return the actual compressed format */
961            *params = _mesa_compressed_format_to_glenum(ctx, texFormat);
962         }
963         else {
964	    /* If the true internal format is not compressed but the user
965	     * requested a generic compressed format, we have to return the
966	     * generic base format that matches.
967	     *
968	     * From page 119 (page 129 of the PDF) of the OpenGL 1.3 spec:
969	     *
970	     *     "If no specific compressed format is available,
971	     *     internalformat is instead replaced by the corresponding base
972	     *     internal format."
973	     *
974	     * Otherwise just return the user's requested internal format
975	     */
976	    const GLenum f =
977	       _mesa_gl_compressed_format_base_format(img->InternalFormat);
978
979	    *params = (f != 0) ? f : img->InternalFormat;
980	 }
981         break;
982      case GL_TEXTURE_BORDER:
983         *params = img->Border;
984         break;
985      case GL_TEXTURE_RED_SIZE:
986      case GL_TEXTURE_GREEN_SIZE:
987      case GL_TEXTURE_BLUE_SIZE:
988      case GL_TEXTURE_ALPHA_SIZE:
989         if (_mesa_base_format_has_channel(img->_BaseFormat, pname))
990            *params = _mesa_get_format_bits(texFormat, pname);
991         else
992            *params = 0;
993         break;
994      case GL_TEXTURE_INTENSITY_SIZE:
995      case GL_TEXTURE_LUMINANCE_SIZE:
996         if (_mesa_base_format_has_channel(img->_BaseFormat, pname)) {
997            *params = _mesa_get_format_bits(texFormat, pname);
998            if (*params == 0) {
999               /* intensity or luminance is probably stored as RGB[A] */
1000               *params = MIN2(_mesa_get_format_bits(texFormat,
1001                                                    GL_TEXTURE_RED_SIZE),
1002                              _mesa_get_format_bits(texFormat,
1003                                                    GL_TEXTURE_GREEN_SIZE));
1004            }
1005         }
1006         else {
1007            *params = 0;
1008         }
1009         break;
1010      case GL_TEXTURE_DEPTH_SIZE_ARB:
1011         if (!ctx->Extensions.ARB_depth_texture)
1012            goto invalid_pname;
1013         *params = _mesa_get_format_bits(texFormat, pname);
1014         break;
1015      case GL_TEXTURE_STENCIL_SIZE_EXT:
1016         if (!ctx->Extensions.EXT_packed_depth_stencil &&
1017             !ctx->Extensions.ARB_framebuffer_object)
1018            goto invalid_pname;
1019         *params = _mesa_get_format_bits(texFormat, pname);
1020         break;
1021      case GL_TEXTURE_SHARED_SIZE:
1022         if (ctx->Version < 30 &&
1023             !ctx->Extensions.EXT_texture_shared_exponent)
1024            goto invalid_pname;
1025         *params = texFormat == MESA_FORMAT_RGB9_E5_FLOAT ? 5 : 0;
1026         break;
1027
1028      /* GL_ARB_texture_compression */
1029      case GL_TEXTURE_COMPRESSED_IMAGE_SIZE:
1030	 if (_mesa_is_format_compressed(texFormat) &&
1031             !_mesa_is_proxy_texture(target)) {
1032            *params = _mesa_format_image_size(texFormat, img->Width,
1033                                              img->Height, img->Depth);
1034	 }
1035	 else {
1036	    _mesa_error(ctx, GL_INVALID_OPERATION,
1037			"glGetTexLevelParameter[if]v(pname)");
1038	 }
1039         break;
1040      case GL_TEXTURE_COMPRESSED:
1041         *params = (GLint) _mesa_is_format_compressed(texFormat);
1042         break;
1043
1044      /* GL_ARB_texture_float */
1045      case GL_TEXTURE_RED_TYPE_ARB:
1046      case GL_TEXTURE_GREEN_TYPE_ARB:
1047      case GL_TEXTURE_BLUE_TYPE_ARB:
1048      case GL_TEXTURE_ALPHA_TYPE_ARB:
1049      case GL_TEXTURE_LUMINANCE_TYPE_ARB:
1050      case GL_TEXTURE_INTENSITY_TYPE_ARB:
1051      case GL_TEXTURE_DEPTH_TYPE_ARB:
1052         if (!ctx->Extensions.ARB_texture_float)
1053            goto invalid_pname;
1054	 if (_mesa_base_format_has_channel(img->_BaseFormat, pname))
1055	    *params = _mesa_get_format_datatype(texFormat);
1056	 else
1057	    *params = GL_NONE;
1058         break;
1059
1060      default:
1061         goto invalid_pname;
1062   }
1063
1064   /* no error if we get here */
1065   return;
1066
1067invalid_pname:
1068   _mesa_error(ctx, GL_INVALID_ENUM,
1069               "glGetTexLevelParameter[if]v(pname=%s)",
1070               _mesa_lookup_enum_by_nr(pname));
1071}
1072
1073
1074static void
1075get_tex_level_parameter_buffer(struct gl_context *ctx,
1076                               const struct gl_texture_object *texObj,
1077                               GLenum pname, GLint *params)
1078{
1079   const struct gl_buffer_object *bo = texObj->BufferObject;
1080   gl_format texFormat = texObj->_BufferObjectFormat;
1081   GLenum internalFormat = texObj->BufferObjectFormat;
1082   GLenum baseFormat = _mesa_get_format_base_format(texFormat);
1083
1084   if (!bo) {
1085      /* undefined texture buffer object */
1086      *params = pname == GL_TEXTURE_COMPONENTS ? 1 : 0;
1087      return;
1088   }
1089
1090   switch (pname) {
1091      case GL_TEXTURE_BUFFER_DATA_STORE_BINDING:
1092         *params = bo->Name;
1093         break;
1094      case GL_TEXTURE_WIDTH:
1095         *params = bo->Size;
1096         break;
1097      case GL_TEXTURE_HEIGHT:
1098      case GL_TEXTURE_DEPTH:
1099      case GL_TEXTURE_BORDER:
1100      case GL_TEXTURE_SHARED_SIZE:
1101      case GL_TEXTURE_COMPRESSED:
1102         *params = 0;
1103         break;
1104      case GL_TEXTURE_INTERNAL_FORMAT:
1105         *params = internalFormat;
1106         break;
1107      case GL_TEXTURE_RED_SIZE:
1108      case GL_TEXTURE_GREEN_SIZE:
1109      case GL_TEXTURE_BLUE_SIZE:
1110      case GL_TEXTURE_ALPHA_SIZE:
1111         if (_mesa_base_format_has_channel(baseFormat, pname))
1112            *params = _mesa_get_format_bits(texFormat, pname);
1113         else
1114            *params = 0;
1115         break;
1116      case GL_TEXTURE_INTENSITY_SIZE:
1117      case GL_TEXTURE_LUMINANCE_SIZE:
1118         if (_mesa_base_format_has_channel(baseFormat, pname)) {
1119            *params = _mesa_get_format_bits(texFormat, pname);
1120            if (*params == 0) {
1121               /* intensity or luminance is probably stored as RGB[A] */
1122               *params = MIN2(_mesa_get_format_bits(texFormat,
1123                                                    GL_TEXTURE_RED_SIZE),
1124                              _mesa_get_format_bits(texFormat,
1125                                                    GL_TEXTURE_GREEN_SIZE));
1126            }
1127         } else {
1128            *params = 0;
1129         }
1130         break;
1131      case GL_TEXTURE_DEPTH_SIZE_ARB:
1132      case GL_TEXTURE_STENCIL_SIZE_EXT:
1133         *params = _mesa_get_format_bits(texFormat, pname);
1134         break;
1135
1136      /* GL_ARB_texture_compression */
1137      case GL_TEXTURE_COMPRESSED_IMAGE_SIZE:
1138         /* Always illegal for GL_TEXTURE_BUFFER */
1139         _mesa_error(ctx, GL_INVALID_OPERATION,
1140                     "glGetTexLevelParameter[if]v(pname)");
1141         break;
1142
1143      /* GL_ARB_texture_float */
1144      case GL_TEXTURE_RED_TYPE_ARB:
1145      case GL_TEXTURE_GREEN_TYPE_ARB:
1146      case GL_TEXTURE_BLUE_TYPE_ARB:
1147      case GL_TEXTURE_ALPHA_TYPE_ARB:
1148      case GL_TEXTURE_LUMINANCE_TYPE_ARB:
1149      case GL_TEXTURE_INTENSITY_TYPE_ARB:
1150      case GL_TEXTURE_DEPTH_TYPE_ARB:
1151         if (!ctx->Extensions.ARB_texture_float)
1152            goto invalid_pname;
1153         if (_mesa_base_format_has_channel(baseFormat, pname))
1154            *params = _mesa_get_format_datatype(texFormat);
1155         else
1156            *params = GL_NONE;
1157         break;
1158
1159      default:
1160         goto invalid_pname;
1161   }
1162
1163   /* no error if we get here */
1164   return;
1165
1166invalid_pname:
1167   _mesa_error(ctx, GL_INVALID_ENUM,
1168               "glGetTexLevelParameter[if]v(pname=%s)",
1169               _mesa_lookup_enum_by_nr(pname));
1170}
1171
1172
1173void GLAPIENTRY
1174_mesa_GetTexLevelParameterfv( GLenum target, GLint level,
1175                              GLenum pname, GLfloat *params )
1176{
1177   GLint iparam;
1178   _mesa_GetTexLevelParameteriv( target, level, pname, &iparam );
1179   *params = (GLfloat) iparam;
1180}
1181
1182
1183void GLAPIENTRY
1184_mesa_GetTexLevelParameteriv( GLenum target, GLint level,
1185                              GLenum pname, GLint *params )
1186{
1187   const struct gl_texture_unit *texUnit;
1188   struct gl_texture_object *texObj;
1189   GLint maxLevels;
1190   GET_CURRENT_CONTEXT(ctx);
1191   ASSERT_OUTSIDE_BEGIN_END(ctx);
1192
1193   if (ctx->Texture.CurrentUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
1194      _mesa_error(ctx, GL_INVALID_OPERATION,
1195                  "glGetTexLevelParameteriv(current unit)");
1196      return;
1197   }
1198
1199   texUnit = _mesa_get_current_tex_unit(ctx);
1200
1201   if (!legal_get_tex_level_parameter_target(ctx, target)) {
1202      _mesa_error(ctx, GL_INVALID_ENUM,
1203                  "glGetTexLevelParameter[if]v(target=0x%x)", target);
1204      return;
1205   }
1206
1207   maxLevels = _mesa_max_texture_levels(ctx, target);
1208   assert(maxLevels != 0);
1209
1210   if (level < 0 || level >= maxLevels) {
1211      _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexLevelParameter[if]v" );
1212      return;
1213   }
1214
1215   texObj = _mesa_select_tex_object(ctx, texUnit, target);
1216
1217   if (target == GL_TEXTURE_BUFFER)
1218      get_tex_level_parameter_buffer(ctx, texObj, pname, params);
1219   else
1220      get_tex_level_parameter_image(ctx, texObj, target, level, pname, params);
1221}
1222
1223
1224void GLAPIENTRY
1225_mesa_GetTexParameterfv( GLenum target, GLenum pname, GLfloat *params )
1226{
1227   struct gl_texture_object *obj;
1228   GET_CURRENT_CONTEXT(ctx);
1229   ASSERT_OUTSIDE_BEGIN_END(ctx);
1230
1231   obj = get_texobj(ctx, target, GL_TRUE);
1232   if (!obj)
1233      return;
1234
1235   _mesa_lock_texture(ctx, obj);
1236   switch (pname) {
1237      case GL_TEXTURE_MAG_FILTER:
1238	 *params = ENUM_TO_FLOAT(obj->Sampler.MagFilter);
1239	 break;
1240      case GL_TEXTURE_MIN_FILTER:
1241         *params = ENUM_TO_FLOAT(obj->Sampler.MinFilter);
1242         break;
1243      case GL_TEXTURE_WRAP_S:
1244         *params = ENUM_TO_FLOAT(obj->Sampler.WrapS);
1245         break;
1246      case GL_TEXTURE_WRAP_T:
1247         *params = ENUM_TO_FLOAT(obj->Sampler.WrapT);
1248         break;
1249      case GL_TEXTURE_WRAP_R:
1250         *params = ENUM_TO_FLOAT(obj->Sampler.WrapR);
1251         break;
1252      case GL_TEXTURE_BORDER_COLOR:
1253         if (ctx->NewState & (_NEW_BUFFERS | _NEW_FRAG_CLAMP))
1254            _mesa_update_state_locked(ctx);
1255         if (ctx->Color._ClampFragmentColor) {
1256            params[0] = CLAMP(obj->Sampler.BorderColor.f[0], 0.0F, 1.0F);
1257            params[1] = CLAMP(obj->Sampler.BorderColor.f[1], 0.0F, 1.0F);
1258            params[2] = CLAMP(obj->Sampler.BorderColor.f[2], 0.0F, 1.0F);
1259            params[3] = CLAMP(obj->Sampler.BorderColor.f[3], 0.0F, 1.0F);
1260         }
1261         else {
1262            params[0] = obj->Sampler.BorderColor.f[0];
1263            params[1] = obj->Sampler.BorderColor.f[1];
1264            params[2] = obj->Sampler.BorderColor.f[2];
1265            params[3] = obj->Sampler.BorderColor.f[3];
1266         }
1267         break;
1268      case GL_TEXTURE_RESIDENT:
1269         *params = 1.0F;
1270         break;
1271      case GL_TEXTURE_PRIORITY:
1272         *params = obj->Priority;
1273         break;
1274      case GL_TEXTURE_MIN_LOD:
1275         *params = obj->Sampler.MinLod;
1276         break;
1277      case GL_TEXTURE_MAX_LOD:
1278         *params = obj->Sampler.MaxLod;
1279         break;
1280      case GL_TEXTURE_BASE_LEVEL:
1281         *params = (GLfloat) obj->BaseLevel;
1282         break;
1283      case GL_TEXTURE_MAX_LEVEL:
1284         *params = (GLfloat) obj->MaxLevel;
1285         break;
1286      case GL_TEXTURE_MAX_ANISOTROPY_EXT:
1287         if (!ctx->Extensions.EXT_texture_filter_anisotropic)
1288            goto invalid_pname;
1289         *params = obj->Sampler.MaxAnisotropy;
1290         break;
1291      case GL_GENERATE_MIPMAP_SGIS:
1292	 *params = (GLfloat) obj->GenerateMipmap;
1293         break;
1294      case GL_TEXTURE_COMPARE_MODE_ARB:
1295         if (!ctx->Extensions.ARB_shadow)
1296            goto invalid_pname;
1297         *params = (GLfloat) obj->Sampler.CompareMode;
1298         break;
1299      case GL_TEXTURE_COMPARE_FUNC_ARB:
1300         if (!ctx->Extensions.ARB_shadow)
1301            goto invalid_pname;
1302         *params = (GLfloat) obj->Sampler.CompareFunc;
1303         break;
1304      case GL_DEPTH_TEXTURE_MODE_ARB:
1305         if (!ctx->Extensions.ARB_depth_texture)
1306            goto invalid_pname;
1307         *params = (GLfloat) obj->DepthMode;
1308         break;
1309      case GL_TEXTURE_LOD_BIAS:
1310         *params = obj->Sampler.LodBias;
1311         break;
1312#if FEATURE_OES_draw_texture
1313      case GL_TEXTURE_CROP_RECT_OES:
1314         params[0] = obj->CropRect[0];
1315         params[1] = obj->CropRect[1];
1316         params[2] = obj->CropRect[2];
1317         params[3] = obj->CropRect[3];
1318         break;
1319#endif
1320
1321      case GL_TEXTURE_SWIZZLE_R_EXT:
1322      case GL_TEXTURE_SWIZZLE_G_EXT:
1323      case GL_TEXTURE_SWIZZLE_B_EXT:
1324      case GL_TEXTURE_SWIZZLE_A_EXT:
1325         if (!ctx->Extensions.EXT_texture_swizzle)
1326            goto invalid_pname;
1327         *params = (GLfloat) obj->Swizzle[pname - GL_TEXTURE_SWIZZLE_R_EXT];
1328         break;
1329
1330      case GL_TEXTURE_SWIZZLE_RGBA_EXT:
1331         if (!ctx->Extensions.EXT_texture_swizzle) {
1332            goto invalid_pname;
1333         }
1334         else {
1335            GLuint comp;
1336            for (comp = 0; comp < 4; comp++) {
1337               params[comp] = (GLfloat) obj->Swizzle[comp];
1338            }
1339         }
1340         break;
1341
1342      case GL_TEXTURE_CUBE_MAP_SEAMLESS:
1343         if (!ctx->Extensions.AMD_seamless_cubemap_per_texture)
1344            goto invalid_pname;
1345         *params = (GLfloat) obj->Sampler.CubeMapSeamless;
1346         break;
1347
1348      case GL_TEXTURE_IMMUTABLE_FORMAT:
1349         if (!ctx->Extensions.ARB_texture_storage)
1350            goto invalid_pname;
1351         *params = (GLfloat) obj->Immutable;
1352         break;
1353
1354      default:
1355         goto invalid_pname;
1356   }
1357
1358   /* no error if we get here */
1359   _mesa_unlock_texture(ctx, obj);
1360   return;
1361
1362invalid_pname:
1363   _mesa_unlock_texture(ctx, obj);
1364   _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameterfv(pname=0x%x)", pname);
1365}
1366
1367
1368void GLAPIENTRY
1369_mesa_GetTexParameteriv( GLenum target, GLenum pname, GLint *params )
1370{
1371   struct gl_texture_object *obj;
1372   GET_CURRENT_CONTEXT(ctx);
1373   ASSERT_OUTSIDE_BEGIN_END(ctx);
1374
1375   obj = get_texobj(ctx, target, GL_TRUE);
1376   if (!obj)
1377      return;
1378
1379   _mesa_lock_texture(ctx, obj);
1380   switch (pname) {
1381      case GL_TEXTURE_MAG_FILTER:
1382         *params = (GLint) obj->Sampler.MagFilter;
1383         break;
1384      case GL_TEXTURE_MIN_FILTER:
1385         *params = (GLint) obj->Sampler.MinFilter;
1386         break;
1387      case GL_TEXTURE_WRAP_S:
1388         *params = (GLint) obj->Sampler.WrapS;
1389         break;
1390      case GL_TEXTURE_WRAP_T:
1391         *params = (GLint) obj->Sampler.WrapT;
1392         break;
1393      case GL_TEXTURE_WRAP_R:
1394         *params = (GLint) obj->Sampler.WrapR;
1395         break;
1396      case GL_TEXTURE_BORDER_COLOR:
1397         {
1398            GLfloat b[4];
1399            b[0] = CLAMP(obj->Sampler.BorderColor.f[0], 0.0F, 1.0F);
1400            b[1] = CLAMP(obj->Sampler.BorderColor.f[1], 0.0F, 1.0F);
1401            b[2] = CLAMP(obj->Sampler.BorderColor.f[2], 0.0F, 1.0F);
1402            b[3] = CLAMP(obj->Sampler.BorderColor.f[3], 0.0F, 1.0F);
1403            params[0] = FLOAT_TO_INT(b[0]);
1404            params[1] = FLOAT_TO_INT(b[1]);
1405            params[2] = FLOAT_TO_INT(b[2]);
1406            params[3] = FLOAT_TO_INT(b[3]);
1407         }
1408         break;
1409      case GL_TEXTURE_RESIDENT:
1410         *params = 1;
1411         break;
1412      case GL_TEXTURE_PRIORITY:
1413         *params = FLOAT_TO_INT(obj->Priority);
1414         break;
1415      case GL_TEXTURE_MIN_LOD:
1416         *params = (GLint) obj->Sampler.MinLod;
1417         break;
1418      case GL_TEXTURE_MAX_LOD:
1419         *params = (GLint) obj->Sampler.MaxLod;
1420         break;
1421      case GL_TEXTURE_BASE_LEVEL:
1422         *params = obj->BaseLevel;
1423         break;
1424      case GL_TEXTURE_MAX_LEVEL:
1425         *params = obj->MaxLevel;
1426         break;
1427      case GL_TEXTURE_MAX_ANISOTROPY_EXT:
1428         if (!ctx->Extensions.EXT_texture_filter_anisotropic)
1429            goto invalid_pname;
1430         *params = (GLint) obj->Sampler.MaxAnisotropy;
1431         break;
1432      case GL_GENERATE_MIPMAP_SGIS:
1433	 *params = (GLint) obj->GenerateMipmap;
1434         break;
1435      case GL_TEXTURE_COMPARE_MODE_ARB:
1436         if (!ctx->Extensions.ARB_shadow)
1437            goto invalid_pname;
1438         *params = (GLint) obj->Sampler.CompareMode;
1439         break;
1440      case GL_TEXTURE_COMPARE_FUNC_ARB:
1441         if (!ctx->Extensions.ARB_shadow)
1442            goto invalid_pname;
1443         *params = (GLint) obj->Sampler.CompareFunc;
1444         break;
1445      case GL_DEPTH_TEXTURE_MODE_ARB:
1446         if (!ctx->Extensions.ARB_depth_texture)
1447            goto invalid_pname;
1448         *params = (GLint) obj->DepthMode;
1449         break;
1450      case GL_TEXTURE_LOD_BIAS:
1451         *params = (GLint) obj->Sampler.LodBias;
1452         break;
1453#if FEATURE_OES_draw_texture
1454      case GL_TEXTURE_CROP_RECT_OES:
1455         params[0] = obj->CropRect[0];
1456         params[1] = obj->CropRect[1];
1457         params[2] = obj->CropRect[2];
1458         params[3] = obj->CropRect[3];
1459         break;
1460#endif
1461      case GL_TEXTURE_SWIZZLE_R_EXT:
1462      case GL_TEXTURE_SWIZZLE_G_EXT:
1463      case GL_TEXTURE_SWIZZLE_B_EXT:
1464      case GL_TEXTURE_SWIZZLE_A_EXT:
1465         if (!ctx->Extensions.EXT_texture_swizzle)
1466            goto invalid_pname;
1467         *params = obj->Swizzle[pname - GL_TEXTURE_SWIZZLE_R_EXT];
1468         break;
1469
1470      case GL_TEXTURE_SWIZZLE_RGBA_EXT:
1471         if (!ctx->Extensions.EXT_texture_swizzle)
1472            goto invalid_pname;
1473         COPY_4V(params, obj->Swizzle);
1474         break;
1475
1476      case GL_TEXTURE_CUBE_MAP_SEAMLESS:
1477         if (!ctx->Extensions.AMD_seamless_cubemap_per_texture)
1478            goto invalid_pname;
1479         *params = (GLint) obj->Sampler.CubeMapSeamless;
1480         break;
1481
1482      case GL_TEXTURE_IMMUTABLE_FORMAT:
1483         if (!ctx->Extensions.ARB_texture_storage)
1484            goto invalid_pname;
1485         *params = (GLint) obj->Immutable;
1486         break;
1487
1488      case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
1489         if (!ctx->Extensions.OES_EGL_image_external)
1490            goto invalid_pname;
1491         *params = obj->RequiredTextureImageUnits;
1492         break;
1493
1494      default:
1495         goto invalid_pname;
1496   }
1497
1498   /* no error if we get here */
1499   _mesa_unlock_texture(ctx, obj);
1500   return;
1501
1502invalid_pname:
1503   _mesa_unlock_texture(ctx, obj);
1504   _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameteriv(pname=0x%x)", pname);
1505}
1506
1507
1508/** New in GL 3.0 */
1509void GLAPIENTRY
1510_mesa_GetTexParameterIiv(GLenum target, GLenum pname, GLint *params)
1511{
1512   struct gl_texture_object *texObj;
1513   GET_CURRENT_CONTEXT(ctx);
1514   ASSERT_OUTSIDE_BEGIN_END(ctx);
1515
1516   texObj = get_texobj(ctx, target, GL_TRUE);
1517   if (!texObj)
1518      return;
1519
1520   switch (pname) {
1521   case GL_TEXTURE_BORDER_COLOR:
1522      COPY_4V(params, texObj->Sampler.BorderColor.i);
1523      break;
1524   default:
1525      _mesa_GetTexParameteriv(target, pname, params);
1526   }
1527}
1528
1529
1530/** New in GL 3.0 */
1531void GLAPIENTRY
1532_mesa_GetTexParameterIuiv(GLenum target, GLenum pname, GLuint *params)
1533{
1534   struct gl_texture_object *texObj;
1535   GET_CURRENT_CONTEXT(ctx);
1536   ASSERT_OUTSIDE_BEGIN_END(ctx);
1537
1538   texObj = get_texobj(ctx, target, GL_TRUE);
1539   if (!texObj)
1540      return;
1541
1542   switch (pname) {
1543   case GL_TEXTURE_BORDER_COLOR:
1544      COPY_4V(params, texObj->Sampler.BorderColor.i);
1545      break;
1546   default:
1547      {
1548         GLint ip[4];
1549         _mesa_GetTexParameteriv(target, pname, ip);
1550         params[0] = ip[0];
1551         if (pname == GL_TEXTURE_SWIZZLE_RGBA_EXT ||
1552             pname == GL_TEXTURE_CROP_RECT_OES) {
1553            params[1] = ip[1];
1554            params[2] = ip[2];
1555            params[3] = ip[3];
1556         }
1557      }
1558   }
1559}
1560