25a953853478a23ed085ccd3a4634d2324e6d043
[reactos.git] / reactos / dll / opengl / mesa / src / mesa / main / texparam.c
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/image.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/texparam.h"
45 #include "main/teximage.h"
46 #include "main/texstate.h"
47 #include "program/prog_instruction.h"
48
49
50 /**
51 * Check if a coordinate wrap mode is supported for the texture target.
52 * \return GL_TRUE if legal, GL_FALSE otherwise
53 */
54 static GLboolean
55 validate_texture_wrap_mode(struct gl_context * ctx, GLenum target, GLenum wrap)
56 {
57 const struct gl_extensions * const e = & ctx->Extensions;
58
59 if (target == GL_TEXTURE_RECTANGLE_NV) {
60 if (wrap == GL_CLAMP || wrap == GL_CLAMP_TO_EDGE ||
61 (wrap == GL_CLAMP_TO_BORDER && e->ARB_texture_border_clamp))
62 return GL_TRUE;
63 }
64 else if (target == GL_TEXTURE_EXTERNAL_OES) {
65 if (wrap == GL_CLAMP_TO_EDGE)
66 return GL_TRUE;
67 }
68 else {
69 switch (wrap) {
70 case GL_CLAMP:
71 case GL_REPEAT:
72 case GL_CLAMP_TO_EDGE:
73 case GL_MIRRORED_REPEAT:
74 return GL_TRUE;
75 case GL_CLAMP_TO_BORDER:
76 if (e->ARB_texture_border_clamp)
77 return GL_TRUE;
78 break;
79 case GL_MIRROR_CLAMP_EXT:
80 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
81 if (e->ATI_texture_mirror_once || e->EXT_texture_mirror_clamp)
82 return GL_TRUE;
83 break;
84 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
85 if (e->EXT_texture_mirror_clamp)
86 return GL_TRUE;
87 break;
88 default:
89 break;
90 }
91 }
92
93 _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(param=0x%x)", wrap );
94 return GL_FALSE;
95 }
96
97
98 /**
99 * Get current texture object for given target.
100 * Return NULL if any error (and record the error).
101 * Note that this is different from _mesa_select_tex_object() in that proxy
102 * targets are not accepted.
103 * Only the glGetTexLevelParameter() functions accept proxy targets.
104 */
105 static struct gl_texture_object *
106 get_texobj(struct gl_context *ctx, GLenum target, GLboolean get)
107 {
108 struct gl_texture_unit *texUnit;
109
110 if (ctx->Texture.CurrentUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
111 _mesa_error(ctx, GL_INVALID_OPERATION,
112 "gl%sTexParameter(current unit)", get ? "Get" : "");
113 return NULL;
114 }
115
116 texUnit = _mesa_get_current_tex_unit(ctx);
117
118 switch (target) {
119 case GL_TEXTURE_1D:
120 return texUnit->CurrentTex[TEXTURE_1D_INDEX];
121 case GL_TEXTURE_2D:
122 return texUnit->CurrentTex[TEXTURE_2D_INDEX];
123 case GL_TEXTURE_3D:
124 return texUnit->CurrentTex[TEXTURE_3D_INDEX];
125 case GL_TEXTURE_CUBE_MAP:
126 if (ctx->Extensions.ARB_texture_cube_map) {
127 return texUnit->CurrentTex[TEXTURE_CUBE_INDEX];
128 }
129 break;
130 case GL_TEXTURE_RECTANGLE_NV:
131 if (ctx->Extensions.NV_texture_rectangle) {
132 return texUnit->CurrentTex[TEXTURE_RECT_INDEX];
133 }
134 break;
135 case GL_TEXTURE_1D_ARRAY_EXT:
136 if (ctx->Extensions.MESA_texture_array ||
137 ctx->Extensions.EXT_texture_array) {
138 return texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX];
139 }
140 break;
141 case GL_TEXTURE_2D_ARRAY_EXT:
142 if (ctx->Extensions.MESA_texture_array ||
143 ctx->Extensions.EXT_texture_array) {
144 return texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX];
145 }
146 break;
147 case GL_TEXTURE_EXTERNAL_OES:
148 if (ctx->Extensions.OES_EGL_image_external) {
149 return texUnit->CurrentTex[TEXTURE_EXTERNAL_INDEX];
150 }
151 break;
152 default:
153 ;
154 }
155
156 _mesa_error(ctx, GL_INVALID_ENUM,
157 "gl%sTexParameter(target)", get ? "Get" : "");
158 return NULL;
159 }
160
161
162 /**
163 * This is called just prior to changing any texture object state which
164 * will not effect texture completeness.
165 */
166 static inline void
167 flush(struct gl_context *ctx)
168 {
169 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
170 }
171
172
173 /**
174 * This is called just prior to changing any texture object state which
175 * can effect texture completeness (texture base level, max level,
176 * minification filter).
177 * Any pending rendering will be flushed out, we'll set the _NEW_TEXTURE
178 * state flag and then mark the texture object as 'incomplete' so that any
179 * per-texture derived state gets recomputed.
180 */
181 static inline void
182 incomplete(struct gl_context *ctx, struct gl_texture_object *texObj)
183 {
184 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
185 texObj->_Complete = GL_FALSE;
186 }
187
188
189 /**
190 * Set an integer-valued texture parameter
191 * \return GL_TRUE if legal AND the value changed, GL_FALSE otherwise
192 */
193 static GLboolean
194 set_tex_parameteri(struct gl_context *ctx,
195 struct gl_texture_object *texObj,
196 GLenum pname, const GLint *params)
197 {
198 switch (pname) {
199 case GL_TEXTURE_MIN_FILTER:
200 if (texObj->Sampler.MinFilter == params[0])
201 return GL_FALSE;
202 switch (params[0]) {
203 case GL_NEAREST:
204 case GL_LINEAR:
205 incomplete(ctx, texObj);
206 texObj->Sampler.MinFilter = params[0];
207 return GL_TRUE;
208 case GL_NEAREST_MIPMAP_NEAREST:
209 case GL_LINEAR_MIPMAP_NEAREST:
210 case GL_NEAREST_MIPMAP_LINEAR:
211 case GL_LINEAR_MIPMAP_LINEAR:
212 if (texObj->Target != GL_TEXTURE_RECTANGLE_NV &&
213 texObj->Target != GL_TEXTURE_EXTERNAL_OES) {
214 incomplete(ctx, texObj);
215 texObj->Sampler.MinFilter = params[0];
216 return GL_TRUE;
217 }
218 /* fall-through */
219 default:
220 goto invalid_param;
221 }
222 return GL_FALSE;
223
224 case GL_TEXTURE_MAG_FILTER:
225 if (texObj->Sampler.MagFilter == params[0])
226 return GL_FALSE;
227 switch (params[0]) {
228 case GL_NEAREST:
229 case GL_LINEAR:
230 flush(ctx); /* does not effect completeness */
231 texObj->Sampler.MagFilter = params[0];
232 return GL_TRUE;
233 default:
234 goto invalid_param;
235 }
236 return GL_FALSE;
237
238 case GL_TEXTURE_WRAP_S:
239 if (texObj->Sampler.WrapS == params[0])
240 return GL_FALSE;
241 if (validate_texture_wrap_mode(ctx, texObj->Target, params[0])) {
242 flush(ctx);
243 texObj->Sampler.WrapS = params[0];
244 return GL_TRUE;
245 }
246 return GL_FALSE;
247
248 case GL_TEXTURE_WRAP_T:
249 if (texObj->Sampler.WrapT == params[0])
250 return GL_FALSE;
251 if (validate_texture_wrap_mode(ctx, texObj->Target, params[0])) {
252 flush(ctx);
253 texObj->Sampler.WrapT = params[0];
254 return GL_TRUE;
255 }
256 return GL_FALSE;
257
258 case GL_TEXTURE_WRAP_R:
259 if (texObj->Sampler.WrapR == params[0])
260 return GL_FALSE;
261 if (validate_texture_wrap_mode(ctx, texObj->Target, params[0])) {
262 flush(ctx);
263 texObj->Sampler.WrapR = params[0];
264 return GL_TRUE;
265 }
266 return GL_FALSE;
267
268 case GL_TEXTURE_BASE_LEVEL:
269 if (texObj->BaseLevel == params[0])
270 return GL_FALSE;
271 if (params[0] < 0 ||
272 (texObj->Target == GL_TEXTURE_RECTANGLE_ARB && params[0] != 0)) {
273 _mesa_error(ctx, GL_INVALID_VALUE,
274 "glTexParameter(param=%d)", params[0]);
275 return GL_FALSE;
276 }
277 incomplete(ctx, texObj);
278 texObj->BaseLevel = params[0];
279 return GL_TRUE;
280
281 case GL_TEXTURE_MAX_LEVEL:
282 if (texObj->MaxLevel == params[0])
283 return GL_FALSE;
284 if (params[0] < 0 || texObj->Target == GL_TEXTURE_RECTANGLE_ARB) {
285 _mesa_error(ctx, GL_INVALID_OPERATION,
286 "glTexParameter(param=%d)", params[0]);
287 return GL_FALSE;
288 }
289 incomplete(ctx, texObj);
290 texObj->MaxLevel = params[0];
291 return GL_TRUE;
292
293 case GL_GENERATE_MIPMAP_SGIS:
294 if (params[0] && texObj->Target == GL_TEXTURE_EXTERNAL_OES)
295 goto invalid_param;
296 if (texObj->GenerateMipmap != params[0]) {
297 /* no flush() */
298 texObj->GenerateMipmap = params[0] ? GL_TRUE : GL_FALSE;
299 return GL_TRUE;
300 }
301 return GL_FALSE;
302
303 #if FEATURE_OES_draw_texture
304 case GL_TEXTURE_CROP_RECT_OES:
305 texObj->CropRect[0] = params[0];
306 texObj->CropRect[1] = params[1];
307 texObj->CropRect[2] = params[2];
308 texObj->CropRect[3] = params[3];
309 return GL_TRUE;
310 #endif
311
312 default:
313 goto invalid_pname;
314 }
315
316 invalid_pname:
317 _mesa_error(ctx, GL_INVALID_ENUM, "glTexParameter(pname=%s)",
318 _mesa_lookup_enum_by_nr(pname));
319 return GL_FALSE;
320
321 invalid_param:
322 _mesa_error(ctx, GL_INVALID_ENUM, "glTexParameter(param=%s)",
323 _mesa_lookup_enum_by_nr(params[0]));
324 return GL_FALSE;
325 }
326
327
328 /**
329 * Set a float-valued texture parameter
330 * \return GL_TRUE if legal AND the value changed, GL_FALSE otherwise
331 */
332 static GLboolean
333 set_tex_parameterf(struct gl_context *ctx,
334 struct gl_texture_object *texObj,
335 GLenum pname, const GLfloat *params)
336 {
337 switch (pname) {
338 case GL_TEXTURE_MIN_LOD:
339 if (texObj->Sampler.MinLod == params[0])
340 return GL_FALSE;
341 flush(ctx);
342 texObj->Sampler.MinLod = params[0];
343 return GL_TRUE;
344
345 case GL_TEXTURE_MAX_LOD:
346 if (texObj->Sampler.MaxLod == params[0])
347 return GL_FALSE;
348 flush(ctx);
349 texObj->Sampler.MaxLod = params[0];
350 return GL_TRUE;
351
352 case GL_TEXTURE_PRIORITY:
353 flush(ctx);
354 texObj->Priority = CLAMP(params[0], 0.0F, 1.0F);
355 return GL_TRUE;
356
357 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
358 if (ctx->Extensions.EXT_texture_filter_anisotropic) {
359 if (texObj->Sampler.MaxAnisotropy == params[0])
360 return GL_FALSE;
361 if (params[0] < 1.0) {
362 _mesa_error(ctx, GL_INVALID_VALUE, "glTexParameter(param)" );
363 return GL_FALSE;
364 }
365 flush(ctx);
366 /* clamp to max, that's what NVIDIA does */
367 texObj->Sampler.MaxAnisotropy = MIN2(params[0],
368 ctx->Const.MaxTextureMaxAnisotropy);
369 return GL_TRUE;
370 }
371 else {
372 static GLuint count = 0;
373 if (count++ < 10)
374 _mesa_error(ctx, GL_INVALID_ENUM,
375 "glTexParameter(pname=GL_TEXTURE_MAX_ANISOTROPY_EXT)");
376 }
377 return GL_FALSE;
378
379 case GL_TEXTURE_LOD_BIAS:
380 /* NOTE: this is really part of OpenGL 1.4, not EXT_texture_lod_bias */
381 if (texObj->Sampler.LodBias != params[0]) {
382 flush(ctx);
383 texObj->Sampler.LodBias = params[0];
384 return GL_TRUE;
385 }
386 break;
387
388 case GL_TEXTURE_BORDER_COLOR:
389 flush(ctx);
390 /* ARB_texture_float disables clamping */
391 if (ctx->Extensions.ARB_texture_float) {
392 texObj->Sampler.BorderColor.f[RCOMP] = params[0];
393 texObj->Sampler.BorderColor.f[GCOMP] = params[1];
394 texObj->Sampler.BorderColor.f[BCOMP] = params[2];
395 texObj->Sampler.BorderColor.f[ACOMP] = params[3];
396 } else {
397 texObj->Sampler.BorderColor.f[RCOMP] = CLAMP(params[0], 0.0F, 1.0F);
398 texObj->Sampler.BorderColor.f[GCOMP] = CLAMP(params[1], 0.0F, 1.0F);
399 texObj->Sampler.BorderColor.f[BCOMP] = CLAMP(params[2], 0.0F, 1.0F);
400 texObj->Sampler.BorderColor.f[ACOMP] = CLAMP(params[3], 0.0F, 1.0F);
401 }
402 return GL_TRUE;
403
404 default:
405 _mesa_error(ctx, GL_INVALID_ENUM, "glTexParameter(pname=0x%x)", pname);
406 }
407 return GL_FALSE;
408 }
409
410
411 void GLAPIENTRY
412 _mesa_TexParameterf(GLenum target, GLenum pname, GLfloat param)
413 {
414 GLboolean need_update;
415 struct gl_texture_object *texObj;
416 GET_CURRENT_CONTEXT(ctx);
417 ASSERT_OUTSIDE_BEGIN_END(ctx);
418
419 texObj = get_texobj(ctx, target, GL_FALSE);
420 if (!texObj)
421 return;
422
423 switch (pname) {
424 case GL_TEXTURE_MIN_FILTER:
425 case GL_TEXTURE_MAG_FILTER:
426 case GL_TEXTURE_WRAP_S:
427 case GL_TEXTURE_WRAP_T:
428 case GL_TEXTURE_WRAP_R:
429 case GL_TEXTURE_BASE_LEVEL:
430 case GL_TEXTURE_MAX_LEVEL:
431 case GL_GENERATE_MIPMAP_SGIS:
432 case GL_TEXTURE_COMPARE_MODE_ARB:
433 case GL_TEXTURE_COMPARE_FUNC_ARB:
434 case GL_DEPTH_TEXTURE_MODE_ARB:
435 case GL_TEXTURE_CUBE_MAP_SEAMLESS:
436 {
437 /* convert float param to int */
438 GLint p[4];
439 p[0] = (GLint) param;
440 p[1] = p[2] = p[3] = 0;
441 need_update = set_tex_parameteri(ctx, texObj, pname, p);
442 }
443 break;
444 case GL_TEXTURE_SWIZZLE_R_EXT:
445 case GL_TEXTURE_SWIZZLE_G_EXT:
446 case GL_TEXTURE_SWIZZLE_B_EXT:
447 case GL_TEXTURE_SWIZZLE_A_EXT:
448 {
449 GLint p[4];
450 p[0] = (GLint) param;
451 p[1] = p[2] = p[3] = 0;
452 need_update = set_tex_parameteri(ctx, texObj, pname, p);
453 }
454 break;
455 default:
456 {
457 /* this will generate an error if pname is illegal */
458 GLfloat p[4];
459 p[0] = param;
460 p[1] = p[2] = p[3] = 0.0F;
461 need_update = set_tex_parameterf(ctx, texObj, pname, p);
462 }
463 }
464
465 if (ctx->Driver.TexParameter && need_update) {
466 ctx->Driver.TexParameter(ctx, target, texObj, pname, &param);
467 }
468 }
469
470
471 void GLAPIENTRY
472 _mesa_TexParameterfv(GLenum target, GLenum pname, const GLfloat *params)
473 {
474 GLboolean need_update;
475 struct gl_texture_object *texObj;
476 GET_CURRENT_CONTEXT(ctx);
477 ASSERT_OUTSIDE_BEGIN_END(ctx);
478
479 texObj = get_texobj(ctx, target, GL_FALSE);
480 if (!texObj)
481 return;
482
483 switch (pname) {
484 case GL_TEXTURE_MIN_FILTER:
485 case GL_TEXTURE_MAG_FILTER:
486 case GL_TEXTURE_WRAP_S:
487 case GL_TEXTURE_WRAP_T:
488 case GL_TEXTURE_WRAP_R:
489 case GL_TEXTURE_BASE_LEVEL:
490 case GL_TEXTURE_MAX_LEVEL:
491 case GL_GENERATE_MIPMAP_SGIS:
492 case GL_TEXTURE_COMPARE_MODE_ARB:
493 case GL_TEXTURE_COMPARE_FUNC_ARB:
494 case GL_DEPTH_TEXTURE_MODE_ARB:
495 case GL_TEXTURE_CUBE_MAP_SEAMLESS:
496 {
497 /* convert float param to int */
498 GLint p[4];
499 p[0] = (GLint) params[0];
500 p[1] = p[2] = p[3] = 0;
501 need_update = set_tex_parameteri(ctx, texObj, pname, p);
502 }
503 break;
504
505 #if FEATURE_OES_draw_texture
506 case GL_TEXTURE_CROP_RECT_OES:
507 {
508 /* convert float params to int */
509 GLint iparams[4];
510 iparams[0] = (GLint) params[0];
511 iparams[1] = (GLint) params[1];
512 iparams[2] = (GLint) params[2];
513 iparams[3] = (GLint) params[3];
514 need_update = set_tex_parameteri(ctx, texObj, pname, iparams);
515 }
516 break;
517 #endif
518
519 case GL_TEXTURE_SWIZZLE_R_EXT:
520 case GL_TEXTURE_SWIZZLE_G_EXT:
521 case GL_TEXTURE_SWIZZLE_B_EXT:
522 case GL_TEXTURE_SWIZZLE_A_EXT:
523 case GL_TEXTURE_SWIZZLE_RGBA_EXT:
524 {
525 GLint p[4] = {0, 0, 0, 0};
526 p[0] = (GLint) params[0];
527 if (pname == GL_TEXTURE_SWIZZLE_RGBA_EXT) {
528 p[1] = (GLint) params[1];
529 p[2] = (GLint) params[2];
530 p[3] = (GLint) params[3];
531 }
532 need_update = set_tex_parameteri(ctx, texObj, pname, p);
533 }
534 break;
535 default:
536 /* this will generate an error if pname is illegal */
537 need_update = set_tex_parameterf(ctx, texObj, pname, params);
538 }
539
540 if (ctx->Driver.TexParameter && need_update) {
541 ctx->Driver.TexParameter(ctx, target, texObj, pname, params);
542 }
543 }
544
545
546 void GLAPIENTRY
547 _mesa_TexParameteri(GLenum target, GLenum pname, GLint param)
548 {
549 GLboolean need_update;
550 struct gl_texture_object *texObj;
551 GET_CURRENT_CONTEXT(ctx);
552 ASSERT_OUTSIDE_BEGIN_END(ctx);
553
554 texObj = get_texobj(ctx, target, GL_FALSE);
555 if (!texObj)
556 return;
557
558 switch (pname) {
559 case GL_TEXTURE_MIN_LOD:
560 case GL_TEXTURE_MAX_LOD:
561 case GL_TEXTURE_PRIORITY:
562 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
563 case GL_TEXTURE_LOD_BIAS:
564 case GL_TEXTURE_COMPARE_FAIL_VALUE_ARB:
565 {
566 GLfloat fparam[4];
567 fparam[0] = (GLfloat) param;
568 fparam[1] = fparam[2] = fparam[3] = 0.0F;
569 /* convert int param to float */
570 need_update = set_tex_parameterf(ctx, texObj, pname, fparam);
571 }
572 break;
573 default:
574 /* this will generate an error if pname is illegal */
575 {
576 GLint iparam[4];
577 iparam[0] = param;
578 iparam[1] = iparam[2] = iparam[3] = 0;
579 need_update = set_tex_parameteri(ctx, texObj, pname, iparam);
580 }
581 }
582
583 if (ctx->Driver.TexParameter && need_update) {
584 GLfloat fparam = (GLfloat) param;
585 ctx->Driver.TexParameter(ctx, target, texObj, pname, &fparam);
586 }
587 }
588
589
590 void GLAPIENTRY
591 _mesa_TexParameteriv(GLenum target, GLenum pname, const GLint *params)
592 {
593 GLboolean need_update;
594 struct gl_texture_object *texObj;
595 GET_CURRENT_CONTEXT(ctx);
596 ASSERT_OUTSIDE_BEGIN_END(ctx);
597
598 texObj = get_texobj(ctx, target, GL_FALSE);
599 if (!texObj)
600 return;
601
602 switch (pname) {
603 case GL_TEXTURE_BORDER_COLOR:
604 {
605 /* convert int params to float */
606 GLfloat fparams[4];
607 fparams[0] = INT_TO_FLOAT(params[0]);
608 fparams[1] = INT_TO_FLOAT(params[1]);
609 fparams[2] = INT_TO_FLOAT(params[2]);
610 fparams[3] = INT_TO_FLOAT(params[3]);
611 need_update = set_tex_parameterf(ctx, texObj, pname, fparams);
612 }
613 break;
614 case GL_TEXTURE_MIN_LOD:
615 case GL_TEXTURE_MAX_LOD:
616 case GL_TEXTURE_PRIORITY:
617 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
618 case GL_TEXTURE_LOD_BIAS:
619 case GL_TEXTURE_COMPARE_FAIL_VALUE_ARB:
620 {
621 /* convert int param to float */
622 GLfloat fparams[4];
623 fparams[0] = (GLfloat) params[0];
624 fparams[1] = fparams[2] = fparams[3] = 0.0F;
625 need_update = set_tex_parameterf(ctx, texObj, pname, fparams);
626 }
627 break;
628 default:
629 /* this will generate an error if pname is illegal */
630 need_update = set_tex_parameteri(ctx, texObj, pname, params);
631 }
632
633 if (ctx->Driver.TexParameter && need_update) {
634 GLfloat fparams[4];
635 fparams[0] = INT_TO_FLOAT(params[0]);
636 if (pname == GL_TEXTURE_BORDER_COLOR ||
637 pname == GL_TEXTURE_CROP_RECT_OES) {
638 fparams[1] = INT_TO_FLOAT(params[1]);
639 fparams[2] = INT_TO_FLOAT(params[2]);
640 fparams[3] = INT_TO_FLOAT(params[3]);
641 }
642 ctx->Driver.TexParameter(ctx, target, texObj, pname, fparams);
643 }
644 }
645
646
647 /**
648 * Set tex parameter to integer value(s). Primarily intended to set
649 * integer-valued texture border color (for integer-valued textures).
650 * New in GL 3.0.
651 */
652 void GLAPIENTRY
653 _mesa_TexParameterIiv(GLenum target, GLenum pname, const GLint *params)
654 {
655 struct gl_texture_object *texObj;
656 GET_CURRENT_CONTEXT(ctx);
657 ASSERT_OUTSIDE_BEGIN_END(ctx);
658
659 texObj = get_texobj(ctx, target, GL_FALSE);
660 if (!texObj)
661 return;
662
663 switch (pname) {
664 case GL_TEXTURE_BORDER_COLOR:
665 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
666 /* set the integer-valued border color */
667 COPY_4V(texObj->Sampler.BorderColor.i, params);
668 break;
669 default:
670 _mesa_TexParameteriv(target, pname, params);
671 break;
672 }
673 /* XXX no driver hook for TexParameterIiv() yet */
674 }
675
676
677 /**
678 * Set tex parameter to unsigned integer value(s). Primarily intended to set
679 * uint-valued texture border color (for integer-valued textures).
680 * New in GL 3.0
681 */
682 void GLAPIENTRY
683 _mesa_TexParameterIuiv(GLenum target, GLenum pname, const GLuint *params)
684 {
685 struct gl_texture_object *texObj;
686 GET_CURRENT_CONTEXT(ctx);
687 ASSERT_OUTSIDE_BEGIN_END(ctx);
688
689 texObj = get_texobj(ctx, target, GL_FALSE);
690 if (!texObj)
691 return;
692
693 switch (pname) {
694 case GL_TEXTURE_BORDER_COLOR:
695 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
696 /* set the unsigned integer-valued border color */
697 COPY_4V(texObj->Sampler.BorderColor.ui, params);
698 break;
699 default:
700 _mesa_TexParameteriv(target, pname, (const GLint *) params);
701 break;
702 }
703 /* XXX no driver hook for TexParameterIuiv() yet */
704 }
705
706
707 void GLAPIENTRY
708 _mesa_GetTexLevelParameterfv( GLenum target, GLint level,
709 GLenum pname, GLfloat *params )
710 {
711 GLint iparam;
712 _mesa_GetTexLevelParameteriv( target, level, pname, &iparam );
713 *params = (GLfloat) iparam;
714 }
715
716
717 void GLAPIENTRY
718 _mesa_GetTexLevelParameteriv( GLenum target, GLint level,
719 GLenum pname, GLint *params )
720 {
721 const struct gl_texture_unit *texUnit;
722 struct gl_texture_object *texObj;
723 const struct gl_texture_image *img = NULL;
724 GLint maxLevels;
725 gl_format texFormat;
726 GET_CURRENT_CONTEXT(ctx);
727 ASSERT_OUTSIDE_BEGIN_END(ctx);
728
729 if (ctx->Texture.CurrentUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
730 _mesa_error(ctx, GL_INVALID_OPERATION,
731 "glGetTexLevelParameteriv(current unit)");
732 return;
733 }
734
735 texUnit = _mesa_get_current_tex_unit(ctx);
736
737 /* this will catch bad target values */
738 maxLevels = _mesa_max_texture_levels(ctx, target);
739 if (maxLevels == 0) {
740 _mesa_error(ctx, GL_INVALID_ENUM,
741 "glGetTexLevelParameter[if]v(target=0x%x)", target);
742 return;
743 }
744
745 if (level < 0 || level >= maxLevels) {
746 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexLevelParameter[if]v" );
747 return;
748 }
749
750 texObj = _mesa_select_tex_object(ctx, texUnit, target);
751
752 img = _mesa_select_tex_image(ctx, texObj, target, level);
753 if (!img || img->TexFormat == MESA_FORMAT_NONE) {
754 /* undefined texture image */
755 if (pname == GL_TEXTURE_COMPONENTS)
756 *params = 1;
757 else
758 *params = 0;
759 return;
760 }
761
762 texFormat = img->TexFormat;
763
764 switch (pname) {
765 case GL_TEXTURE_WIDTH:
766 *params = img->Width;
767 break;
768 case GL_TEXTURE_HEIGHT:
769 *params = img->Height;
770 break;
771 case GL_TEXTURE_DEPTH:
772 *params = img->Depth;
773 break;
774 case GL_TEXTURE_INTERNAL_FORMAT:
775 if (_mesa_is_format_compressed(texFormat)) {
776 /* need to return the actual compressed format */
777 *params = _mesa_compressed_format_to_glenum(ctx, texFormat);
778 }
779 else {
780 /* If the true internal format is not compressed but the user
781 * requested a generic compressed format, we have to return the
782 * generic base format that matches.
783 *
784 * From page 119 (page 129 of the PDF) of the OpenGL 1.3 spec:
785 *
786 * "If no specific compressed format is available,
787 * internalformat is instead replaced by the corresponding base
788 * internal format."
789 *
790 * Otherwise just return the user's requested internal format
791 */
792 const GLenum f =
793 _mesa_gl_compressed_format_base_format(img->InternalFormat);
794
795 *params = (f != 0) ? f : img->InternalFormat;
796 }
797 break;
798 case GL_TEXTURE_BORDER:
799 *params = img->Border;
800 break;
801 case GL_TEXTURE_RED_SIZE:
802 case GL_TEXTURE_GREEN_SIZE:
803 case GL_TEXTURE_BLUE_SIZE:
804 case GL_TEXTURE_ALPHA_SIZE:
805 if (_mesa_base_format_has_channel(img->_BaseFormat, pname))
806 *params = _mesa_get_format_bits(texFormat, pname);
807 else
808 *params = 0;
809 break;
810 case GL_TEXTURE_INTENSITY_SIZE:
811 case GL_TEXTURE_LUMINANCE_SIZE:
812 if (_mesa_base_format_has_channel(img->_BaseFormat, pname)) {
813 *params = _mesa_get_format_bits(texFormat, pname);
814 if (*params == 0) {
815 /* intensity or luminance is probably stored as RGB[A] */
816 *params = MIN2(_mesa_get_format_bits(texFormat,
817 GL_TEXTURE_RED_SIZE),
818 _mesa_get_format_bits(texFormat,
819 GL_TEXTURE_GREEN_SIZE));
820 }
821 }
822 else {
823 *params = 0;
824 }
825 break;
826 case GL_TEXTURE_STENCIL_SIZE_EXT:
827 if (!ctx->Extensions.EXT_packed_depth_stencil &&
828 !ctx->Extensions.ARB_framebuffer_object)
829 goto invalid_pname;
830 *params = _mesa_get_format_bits(texFormat, pname);
831 break;
832
833 /* GL_ARB_texture_compression */
834 case GL_TEXTURE_COMPRESSED_IMAGE_SIZE:
835 if (_mesa_is_format_compressed(texFormat) &&
836 !_mesa_is_proxy_texture(target)) {
837 *params = _mesa_format_image_size(texFormat, img->Width,
838 img->Height, img->Depth);
839 }
840 else {
841 _mesa_error(ctx, GL_INVALID_OPERATION,
842 "glGetTexLevelParameter[if]v(pname)");
843 }
844 break;
845 case GL_TEXTURE_COMPRESSED:
846 *params = (GLint) _mesa_is_format_compressed(texFormat);
847 break;
848
849 /* GL_ARB_texture_float */
850 case GL_TEXTURE_RED_TYPE_ARB:
851 case GL_TEXTURE_GREEN_TYPE_ARB:
852 case GL_TEXTURE_BLUE_TYPE_ARB:
853 case GL_TEXTURE_ALPHA_TYPE_ARB:
854 case GL_TEXTURE_LUMINANCE_TYPE_ARB:
855 case GL_TEXTURE_INTENSITY_TYPE_ARB:
856 case GL_TEXTURE_DEPTH_TYPE_ARB:
857 if (!ctx->Extensions.ARB_texture_float)
858 goto invalid_pname;
859 if (_mesa_base_format_has_channel(img->_BaseFormat, pname))
860 *params = _mesa_get_format_datatype(texFormat);
861 else
862 *params = GL_NONE;
863 break;
864
865 default:
866 goto invalid_pname;
867 }
868
869 /* no error if we get here */
870 return;
871
872 invalid_pname:
873 _mesa_error(ctx, GL_INVALID_ENUM,
874 "glGetTexLevelParameter[if]v(pname=%s)",
875 _mesa_lookup_enum_by_nr(pname));
876 }
877
878
879
880 void GLAPIENTRY
881 _mesa_GetTexParameterfv( GLenum target, GLenum pname, GLfloat *params )
882 {
883 struct gl_texture_object *obj;
884 GET_CURRENT_CONTEXT(ctx);
885 ASSERT_OUTSIDE_BEGIN_END(ctx);
886
887 obj = get_texobj(ctx, target, GL_TRUE);
888 if (!obj)
889 return;
890
891 _mesa_lock_texture(ctx, obj);
892 switch (pname) {
893 case GL_TEXTURE_MAG_FILTER:
894 *params = ENUM_TO_FLOAT(obj->Sampler.MagFilter);
895 break;
896 case GL_TEXTURE_MIN_FILTER:
897 *params = ENUM_TO_FLOAT(obj->Sampler.MinFilter);
898 break;
899 case GL_TEXTURE_WRAP_S:
900 *params = ENUM_TO_FLOAT(obj->Sampler.WrapS);
901 break;
902 case GL_TEXTURE_WRAP_T:
903 *params = ENUM_TO_FLOAT(obj->Sampler.WrapT);
904 break;
905 case GL_TEXTURE_WRAP_R:
906 *params = ENUM_TO_FLOAT(obj->Sampler.WrapR);
907 break;
908 case GL_TEXTURE_BORDER_COLOR:
909 if (ctx->NewState & (_NEW_BUFFERS | _NEW_FRAG_CLAMP))
910 _mesa_update_state_locked(ctx);
911 if (ctx->Color._ClampFragmentColor) {
912 params[0] = CLAMP(obj->Sampler.BorderColor.f[0], 0.0F, 1.0F);
913 params[1] = CLAMP(obj->Sampler.BorderColor.f[1], 0.0F, 1.0F);
914 params[2] = CLAMP(obj->Sampler.BorderColor.f[2], 0.0F, 1.0F);
915 params[3] = CLAMP(obj->Sampler.BorderColor.f[3], 0.0F, 1.0F);
916 }
917 else {
918 params[0] = obj->Sampler.BorderColor.f[0];
919 params[1] = obj->Sampler.BorderColor.f[1];
920 params[2] = obj->Sampler.BorderColor.f[2];
921 params[3] = obj->Sampler.BorderColor.f[3];
922 }
923 break;
924 case GL_TEXTURE_RESIDENT:
925 *params = 1.0F;
926 break;
927 case GL_TEXTURE_PRIORITY:
928 *params = obj->Priority;
929 break;
930 case GL_TEXTURE_MIN_LOD:
931 *params = obj->Sampler.MinLod;
932 break;
933 case GL_TEXTURE_MAX_LOD:
934 *params = obj->Sampler.MaxLod;
935 break;
936 case GL_TEXTURE_BASE_LEVEL:
937 *params = (GLfloat) obj->BaseLevel;
938 break;
939 case GL_TEXTURE_MAX_LEVEL:
940 *params = (GLfloat) obj->MaxLevel;
941 break;
942 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
943 if (!ctx->Extensions.EXT_texture_filter_anisotropic)
944 goto invalid_pname;
945 *params = obj->Sampler.MaxAnisotropy;
946 break;
947 case GL_GENERATE_MIPMAP_SGIS:
948 *params = (GLfloat) obj->GenerateMipmap;
949 break;
950 case GL_TEXTURE_LOD_BIAS:
951 *params = obj->Sampler.LodBias;
952 break;
953 #if FEATURE_OES_draw_texture
954 case GL_TEXTURE_CROP_RECT_OES:
955 params[0] = obj->CropRect[0];
956 params[1] = obj->CropRect[1];
957 params[2] = obj->CropRect[2];
958 params[3] = obj->CropRect[3];
959 break;
960 #endif
961
962 case GL_TEXTURE_IMMUTABLE_FORMAT:
963 if (!ctx->Extensions.ARB_texture_storage)
964 goto invalid_pname;
965 *params = (GLfloat) obj->Immutable;
966 break;
967
968 default:
969 goto invalid_pname;
970 }
971
972 /* no error if we get here */
973 _mesa_unlock_texture(ctx, obj);
974 return;
975
976 invalid_pname:
977 _mesa_unlock_texture(ctx, obj);
978 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameterfv(pname=0x%x)", pname);
979 }
980
981
982 void GLAPIENTRY
983 _mesa_GetTexParameteriv( GLenum target, GLenum pname, GLint *params )
984 {
985 struct gl_texture_object *obj;
986 GET_CURRENT_CONTEXT(ctx);
987 ASSERT_OUTSIDE_BEGIN_END(ctx);
988
989 obj = get_texobj(ctx, target, GL_TRUE);
990 if (!obj)
991 return;
992
993 _mesa_lock_texture(ctx, obj);
994 switch (pname) {
995 case GL_TEXTURE_MAG_FILTER:
996 *params = (GLint) obj->Sampler.MagFilter;
997 break;;
998 case GL_TEXTURE_MIN_FILTER:
999 *params = (GLint) obj->Sampler.MinFilter;
1000 break;;
1001 case GL_TEXTURE_WRAP_S:
1002 *params = (GLint) obj->Sampler.WrapS;
1003 break;;
1004 case GL_TEXTURE_WRAP_T:
1005 *params = (GLint) obj->Sampler.WrapT;
1006 break;;
1007 case GL_TEXTURE_WRAP_R:
1008 *params = (GLint) obj->Sampler.WrapR;
1009 break;;
1010 case GL_TEXTURE_BORDER_COLOR:
1011 {
1012 GLfloat b[4];
1013 b[0] = CLAMP(obj->Sampler.BorderColor.f[0], 0.0F, 1.0F);
1014 b[1] = CLAMP(obj->Sampler.BorderColor.f[1], 0.0F, 1.0F);
1015 b[2] = CLAMP(obj->Sampler.BorderColor.f[2], 0.0F, 1.0F);
1016 b[3] = CLAMP(obj->Sampler.BorderColor.f[3], 0.0F, 1.0F);
1017 params[0] = FLOAT_TO_INT(b[0]);
1018 params[1] = FLOAT_TO_INT(b[1]);
1019 params[2] = FLOAT_TO_INT(b[2]);
1020 params[3] = FLOAT_TO_INT(b[3]);
1021 }
1022 break;;
1023 case GL_TEXTURE_RESIDENT:
1024 *params = 1;
1025 break;;
1026 case GL_TEXTURE_PRIORITY:
1027 *params = FLOAT_TO_INT(obj->Priority);
1028 break;;
1029 case GL_TEXTURE_MIN_LOD:
1030 *params = (GLint) obj->Sampler.MinLod;
1031 break;;
1032 case GL_TEXTURE_MAX_LOD:
1033 *params = (GLint) obj->Sampler.MaxLod;
1034 break;;
1035 case GL_TEXTURE_BASE_LEVEL:
1036 *params = obj->BaseLevel;
1037 break;;
1038 case GL_TEXTURE_MAX_LEVEL:
1039 *params = obj->MaxLevel;
1040 break;;
1041 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
1042 if (!ctx->Extensions.EXT_texture_filter_anisotropic)
1043 goto invalid_pname;
1044 *params = (GLint) obj->Sampler.MaxAnisotropy;
1045 break;
1046 case GL_GENERATE_MIPMAP_SGIS:
1047 *params = (GLint) obj->GenerateMipmap;
1048 break;
1049 case GL_TEXTURE_LOD_BIAS:
1050 *params = (GLint) obj->Sampler.LodBias;
1051 break;
1052 #if FEATURE_OES_draw_texture
1053 case GL_TEXTURE_CROP_RECT_OES:
1054 params[0] = obj->CropRect[0];
1055 params[1] = obj->CropRect[1];
1056 params[2] = obj->CropRect[2];
1057 params[3] = obj->CropRect[3];
1058 break;
1059 #endif
1060
1061 case GL_TEXTURE_IMMUTABLE_FORMAT:
1062 if (!ctx->Extensions.ARB_texture_storage)
1063 goto invalid_pname;
1064 *params = (GLint) obj->Immutable;
1065 break;
1066
1067 case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
1068 if (!ctx->Extensions.OES_EGL_image_external)
1069 goto invalid_pname;
1070 *params = obj->RequiredTextureImageUnits;
1071 break;
1072
1073 default:
1074 goto invalid_pname;
1075 }
1076
1077 /* no error if we get here */
1078 _mesa_unlock_texture(ctx, obj);
1079 return;
1080
1081 invalid_pname:
1082 _mesa_unlock_texture(ctx, obj);
1083 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexParameteriv(pname=0x%x)", pname);
1084 }
1085
1086
1087 /** New in GL 3.0 */
1088 void GLAPIENTRY
1089 _mesa_GetTexParameterIiv(GLenum target, GLenum pname, GLint *params)
1090 {
1091 struct gl_texture_object *texObj;
1092 GET_CURRENT_CONTEXT(ctx);
1093 ASSERT_OUTSIDE_BEGIN_END(ctx);
1094
1095 texObj = get_texobj(ctx, target, GL_TRUE);
1096 if (!texObj)
1097 return;
1098
1099 switch (pname) {
1100 case GL_TEXTURE_BORDER_COLOR:
1101 COPY_4V(params, texObj->Sampler.BorderColor.i);
1102 break;
1103 default:
1104 _mesa_GetTexParameteriv(target, pname, params);
1105 }
1106 }
1107
1108
1109 /** New in GL 3.0 */
1110 void GLAPIENTRY
1111 _mesa_GetTexParameterIuiv(GLenum target, GLenum pname, GLuint *params)
1112 {
1113 struct gl_texture_object *texObj;
1114 GET_CURRENT_CONTEXT(ctx);
1115 ASSERT_OUTSIDE_BEGIN_END(ctx);
1116
1117 texObj = get_texobj(ctx, target, GL_TRUE);
1118 if (!texObj)
1119 return;
1120
1121 switch (pname) {
1122 case GL_TEXTURE_BORDER_COLOR:
1123 COPY_4V(params, texObj->Sampler.BorderColor.i);
1124 break;
1125 default:
1126 {
1127 GLint ip[4];
1128 _mesa_GetTexParameteriv(target, pname, ip);
1129 params[0] = ip[0];
1130 if (pname == GL_TEXTURE_SWIZZLE_RGBA_EXT ||
1131 pname == GL_TEXTURE_CROP_RECT_OES) {
1132 params[1] = ip[1];
1133 params[2] = ip[2];
1134 params[3] = ip[3];
1135 }
1136 }
1137 }
1138 }