mesa: refuse to update tex parameters when a handle is allocated
[mesa.git] / src / mesa / main / texparam.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. 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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file texparam.c
28 *
29 * glTexParameter-related functions
30 */
31
32 #include <stdbool.h>
33 #include "main/glheader.h"
34 #include "main/blend.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/mtypes.h"
41 #include "main/state.h"
42 #include "main/texcompress.h"
43 #include "main/texobj.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 const bool is_desktop_gl = _mesa_is_desktop_gl(ctx);
59 bool supported;
60
61 switch (wrap) {
62 case GL_CLAMP:
63 /* GL_CLAMP was removed in the core profile, and it has never existed in
64 * OpenGL ES.
65 */
66 supported = (ctx->API == API_OPENGL_COMPAT)
67 && (target != GL_TEXTURE_EXTERNAL_OES);
68 break;
69
70 case GL_CLAMP_TO_EDGE:
71 supported = true;
72 break;
73
74 case GL_CLAMP_TO_BORDER:
75 supported = ctx->API != API_OPENGLES && e->ARB_texture_border_clamp
76 && (target != GL_TEXTURE_EXTERNAL_OES);
77 break;
78
79 case GL_REPEAT:
80 case GL_MIRRORED_REPEAT:
81 supported = (target != GL_TEXTURE_RECTANGLE_NV)
82 && (target != GL_TEXTURE_EXTERNAL_OES);
83 break;
84
85 case GL_MIRROR_CLAMP_EXT:
86 supported = is_desktop_gl
87 && (e->ATI_texture_mirror_once || e->EXT_texture_mirror_clamp)
88 && (target != GL_TEXTURE_RECTANGLE_NV)
89 && (target != GL_TEXTURE_EXTERNAL_OES);
90 break;
91
92 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
93 supported = is_desktop_gl
94 && (e->ATI_texture_mirror_once || e->EXT_texture_mirror_clamp || e->ARB_texture_mirror_clamp_to_edge)
95 && (target != GL_TEXTURE_RECTANGLE_NV)
96 && (target != GL_TEXTURE_EXTERNAL_OES);
97 break;
98
99 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
100 supported = is_desktop_gl && e->EXT_texture_mirror_clamp
101 && (target != GL_TEXTURE_RECTANGLE_NV)
102 && (target != GL_TEXTURE_EXTERNAL_OES);
103 break;
104
105 default:
106 supported = false;
107 break;
108 }
109
110 if (!supported)
111 _mesa_error( ctx, GL_INVALID_ENUM, "glTexParameter(param=0x%x)", wrap );
112
113 return supported;
114 }
115
116
117 /**
118 * Get current texture object for given target.
119 * Return NULL if any error (and record the error).
120 * Note that this is different from _mesa_get_current_tex_object() in that
121 * proxy targets are not accepted.
122 * Only the glGetTexLevelParameter() functions accept proxy targets.
123 */
124 static struct gl_texture_object *
125 get_texobj_by_target(struct gl_context *ctx, GLenum target, GLboolean get)
126 {
127 struct gl_texture_unit *texUnit;
128 int targetIndex;
129
130 if (ctx->Texture.CurrentUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
131 _mesa_error(ctx, GL_INVALID_OPERATION,
132 "gl%sTexParameter(current unit)", get ? "Get" : "");
133 return NULL;
134 }
135
136 texUnit = _mesa_get_current_tex_unit(ctx);
137
138 targetIndex = _mesa_tex_target_to_index(ctx, target);
139 if (targetIndex < 0 || targetIndex == TEXTURE_BUFFER_INDEX) {
140 _mesa_error(ctx, GL_INVALID_ENUM,
141 "gl%sTexParameter(target)", get ? "Get" : "");
142 return NULL;
143 }
144 assert(targetIndex < NUM_TEXTURE_TARGETS);
145
146 return texUnit->CurrentTex[targetIndex];
147 }
148
149 /**
150 * Get current texture object for given name.
151 * Return NULL if any error (and record the error).
152 * Note that proxy targets are not accepted.
153 * Only the glGetTexLevelParameter() functions accept proxy targets.
154 */
155 static struct gl_texture_object *
156 get_texobj_by_name(struct gl_context *ctx, GLuint texture, const char *name)
157 {
158 struct gl_texture_object *texObj;
159
160 texObj = _mesa_lookup_texture(ctx, texture);
161 if (!texObj) {
162 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(texture)", name);
163 return NULL;
164 }
165
166 switch (texObj->Target) {
167 case GL_TEXTURE_1D:
168 case GL_TEXTURE_1D_ARRAY:
169 case GL_TEXTURE_2D:
170 case GL_TEXTURE_2D_ARRAY:
171 case GL_TEXTURE_2D_MULTISAMPLE:
172 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
173 case GL_TEXTURE_3D:
174 case GL_TEXTURE_CUBE_MAP:
175 case GL_TEXTURE_CUBE_MAP_ARRAY:
176 case GL_TEXTURE_RECTANGLE:
177 return texObj;
178 default:
179 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", name);
180 return NULL;
181 }
182
183 }
184
185
186 /**
187 * Convert GL_RED/GREEN/BLUE/ALPHA/ZERO/ONE to SWIZZLE_X/Y/Z/W/ZERO/ONE.
188 * \return -1 if error.
189 */
190 static GLint
191 comp_to_swizzle(GLenum comp)
192 {
193 switch (comp) {
194 case GL_RED:
195 return SWIZZLE_X;
196 case GL_GREEN:
197 return SWIZZLE_Y;
198 case GL_BLUE:
199 return SWIZZLE_Z;
200 case GL_ALPHA:
201 return SWIZZLE_W;
202 case GL_ZERO:
203 return SWIZZLE_ZERO;
204 case GL_ONE:
205 return SWIZZLE_ONE;
206 default:
207 return -1;
208 }
209 }
210
211
212 static void
213 set_swizzle_component(GLuint *swizzle, GLuint comp, GLuint swz)
214 {
215 assert(comp < 4);
216 assert(swz <= SWIZZLE_NIL);
217 {
218 GLuint mask = 0x7 << (3 * comp);
219 GLuint s = (*swizzle & ~mask) | (swz << (3 * comp));
220 *swizzle = s;
221 }
222 }
223
224
225 /**
226 * This is called just prior to changing any texture object state which
227 * will not affect texture completeness.
228 */
229 static inline void
230 flush(struct gl_context *ctx)
231 {
232 FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT);
233 }
234
235
236 /**
237 * This is called just prior to changing any texture object state which
238 * could affect texture completeness (texture base level, max level).
239 * Any pending rendering will be flushed out, we'll set the _NEW_TEXTURE_OBJECT
240 * state flag and then mark the texture object as 'incomplete' so that any
241 * per-texture derived state gets recomputed.
242 */
243 static inline void
244 incomplete(struct gl_context *ctx, struct gl_texture_object *texObj)
245 {
246 FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT);
247 _mesa_dirty_texobj(ctx, texObj);
248 }
249
250
251 GLboolean
252 _mesa_target_allows_setting_sampler_parameters(GLenum target)
253 {
254 switch (target) {
255 case GL_TEXTURE_2D_MULTISAMPLE:
256 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
257 return GL_FALSE;
258
259 default:
260 return GL_TRUE;
261 }
262 }
263
264
265 /**
266 * Set an integer-valued texture parameter
267 * \return GL_TRUE if legal AND the value changed, GL_FALSE otherwise
268 */
269 static GLboolean
270 set_tex_parameteri(struct gl_context *ctx,
271 struct gl_texture_object *texObj,
272 GLenum pname, const GLint *params, bool dsa)
273 {
274 const char *suffix = dsa ? "ture" : "";
275
276 if (texObj->HandleAllocated) {
277 /* The ARB_bindless_texture spec says:
278 *
279 * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
280 * CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
281 * functions defined in terms of these, if the texture object to be
282 * modified is referenced by one or more texture or image handles."
283 */
284 _mesa_error(ctx, GL_INVALID_OPERATION,
285 "glTex%sParameter(immutable texture)", suffix);
286 return GL_FALSE;
287 }
288
289 switch (pname) {
290 case GL_TEXTURE_MIN_FILTER:
291 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
292 goto invalid_enum;
293
294 if (texObj->Sampler.MinFilter == params[0])
295 return GL_FALSE;
296 switch (params[0]) {
297 case GL_NEAREST:
298 case GL_LINEAR:
299 flush(ctx);
300 texObj->Sampler.MinFilter = params[0];
301 return GL_TRUE;
302 case GL_NEAREST_MIPMAP_NEAREST:
303 case GL_LINEAR_MIPMAP_NEAREST:
304 case GL_NEAREST_MIPMAP_LINEAR:
305 case GL_LINEAR_MIPMAP_LINEAR:
306 if (texObj->Target != GL_TEXTURE_RECTANGLE_NV &&
307 texObj->Target != GL_TEXTURE_EXTERNAL_OES) {
308 flush(ctx);
309 texObj->Sampler.MinFilter = params[0];
310 return GL_TRUE;
311 }
312 /* fall-through */
313 default:
314 goto invalid_param;
315 }
316 return GL_FALSE;
317
318 case GL_TEXTURE_MAG_FILTER:
319 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
320 goto invalid_enum;
321
322 if (texObj->Sampler.MagFilter == params[0])
323 return GL_FALSE;
324 switch (params[0]) {
325 case GL_NEAREST:
326 case GL_LINEAR:
327 flush(ctx); /* does not effect completeness */
328 texObj->Sampler.MagFilter = params[0];
329 return GL_TRUE;
330 default:
331 goto invalid_param;
332 }
333 return GL_FALSE;
334
335 case GL_TEXTURE_WRAP_S:
336 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
337 goto invalid_enum;
338
339 if (texObj->Sampler.WrapS == params[0])
340 return GL_FALSE;
341 if (validate_texture_wrap_mode(ctx, texObj->Target, params[0])) {
342 flush(ctx);
343 texObj->Sampler.WrapS = params[0];
344 return GL_TRUE;
345 }
346 return GL_FALSE;
347
348 case GL_TEXTURE_WRAP_T:
349 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
350 goto invalid_enum;
351
352 if (texObj->Sampler.WrapT == params[0])
353 return GL_FALSE;
354 if (validate_texture_wrap_mode(ctx, texObj->Target, params[0])) {
355 flush(ctx);
356 texObj->Sampler.WrapT = params[0];
357 return GL_TRUE;
358 }
359 return GL_FALSE;
360
361 case GL_TEXTURE_WRAP_R:
362 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
363 goto invalid_enum;
364
365 if (texObj->Sampler.WrapR == params[0])
366 return GL_FALSE;
367 if (validate_texture_wrap_mode(ctx, texObj->Target, params[0])) {
368 flush(ctx);
369 texObj->Sampler.WrapR = params[0];
370 return GL_TRUE;
371 }
372 return GL_FALSE;
373
374 case GL_TEXTURE_BASE_LEVEL:
375 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
376 goto invalid_pname;
377
378 if (texObj->BaseLevel == params[0])
379 return GL_FALSE;
380
381 if ((texObj->Target == GL_TEXTURE_2D_MULTISAMPLE ||
382 texObj->Target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) && params[0] != 0)
383 goto invalid_operation;
384
385 if (params[0] < 0) {
386 _mesa_error(ctx, GL_INVALID_VALUE,
387 "glTex%sParameter(param=%d)", suffix, params[0]);
388 return GL_FALSE;
389 }
390 if (texObj->Target == GL_TEXTURE_RECTANGLE_ARB && params[0] != 0) {
391 _mesa_error(ctx, GL_INVALID_OPERATION,
392 "glTex%sParameter(target=%s, param=%d)", suffix,
393 _mesa_enum_to_string(texObj->Target), params[0]);
394 return GL_FALSE;
395 }
396 incomplete(ctx, texObj);
397
398 /** See note about ARB_texture_storage below */
399 if (texObj->Immutable)
400 texObj->BaseLevel = MIN2(texObj->ImmutableLevels - 1, params[0]);
401 else
402 texObj->BaseLevel = params[0];
403
404 return GL_TRUE;
405
406 case GL_TEXTURE_MAX_LEVEL:
407 if (texObj->MaxLevel == params[0])
408 return GL_FALSE;
409
410 if (params[0] < 0 ||
411 (texObj->Target == GL_TEXTURE_RECTANGLE_ARB && params[0] > 0)) {
412 _mesa_error(ctx, GL_INVALID_VALUE,
413 "glTex%sParameter(param=%d)", suffix,
414 params[0]);
415 return GL_FALSE;
416 }
417 incomplete(ctx, texObj);
418
419 /** From ARB_texture_storage:
420 * However, if TEXTURE_IMMUTABLE_FORMAT is TRUE, then level_base is
421 * clamped to the range [0, <levels> - 1] and level_max is then clamped to
422 * the range [level_base, <levels> - 1], where <levels> is the parameter
423 * passed the call to TexStorage* for the texture object.
424 */
425 if (texObj->Immutable)
426 texObj->MaxLevel = CLAMP(params[0], texObj->BaseLevel,
427 texObj->ImmutableLevels - 1);
428 else
429 texObj->MaxLevel = params[0];
430
431 return GL_TRUE;
432
433 case GL_GENERATE_MIPMAP_SGIS:
434 if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
435 goto invalid_pname;
436
437 if (params[0] && texObj->Target == GL_TEXTURE_EXTERNAL_OES)
438 goto invalid_param;
439 if (texObj->GenerateMipmap != params[0]) {
440 /* no flush() */
441 texObj->GenerateMipmap = params[0] ? GL_TRUE : GL_FALSE;
442 return GL_TRUE;
443 }
444 return GL_FALSE;
445
446 case GL_TEXTURE_COMPARE_MODE_ARB:
447 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_shadow)
448 || _mesa_is_gles3(ctx)) {
449
450 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
451 goto invalid_enum;
452
453 if (texObj->Sampler.CompareMode == params[0])
454 return GL_FALSE;
455 if (params[0] == GL_NONE ||
456 params[0] == GL_COMPARE_R_TO_TEXTURE_ARB) {
457 flush(ctx);
458 texObj->Sampler.CompareMode = params[0];
459 return GL_TRUE;
460 }
461 goto invalid_param;
462 }
463 goto invalid_pname;
464
465 case GL_TEXTURE_COMPARE_FUNC_ARB:
466 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_shadow)
467 || _mesa_is_gles3(ctx)) {
468
469 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
470 goto invalid_enum;
471
472 if (texObj->Sampler.CompareFunc == params[0])
473 return GL_FALSE;
474 switch (params[0]) {
475 case GL_LEQUAL:
476 case GL_GEQUAL:
477 case GL_EQUAL:
478 case GL_NOTEQUAL:
479 case GL_LESS:
480 case GL_GREATER:
481 case GL_ALWAYS:
482 case GL_NEVER:
483 flush(ctx);
484 texObj->Sampler.CompareFunc = params[0];
485 return GL_TRUE;
486 default:
487 goto invalid_param;
488 }
489 }
490 goto invalid_pname;
491
492 case GL_DEPTH_TEXTURE_MODE_ARB:
493 /* GL_DEPTH_TEXTURE_MODE_ARB is removed in core-profile and it has never
494 * existed in OpenGL ES.
495 */
496 if (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_texture) {
497 if (texObj->DepthMode == params[0])
498 return GL_FALSE;
499 if (params[0] == GL_LUMINANCE ||
500 params[0] == GL_INTENSITY ||
501 params[0] == GL_ALPHA ||
502 (ctx->Extensions.ARB_texture_rg && params[0] == GL_RED)) {
503 flush(ctx);
504 texObj->DepthMode = params[0];
505 return GL_TRUE;
506 }
507 goto invalid_param;
508 }
509 goto invalid_pname;
510
511 case GL_DEPTH_STENCIL_TEXTURE_MODE:
512 if (_mesa_has_ARB_stencil_texturing(ctx) || _mesa_is_gles31(ctx)) {
513 bool stencil = params[0] == GL_STENCIL_INDEX;
514 if (!stencil && params[0] != GL_DEPTH_COMPONENT)
515 goto invalid_param;
516
517 if (texObj->StencilSampling == stencil)
518 return GL_FALSE;
519
520 texObj->StencilSampling = stencil;
521 return GL_TRUE;
522 }
523 goto invalid_pname;
524
525 case GL_TEXTURE_CROP_RECT_OES:
526 if (ctx->API != API_OPENGLES || !ctx->Extensions.OES_draw_texture)
527 goto invalid_pname;
528
529 texObj->CropRect[0] = params[0];
530 texObj->CropRect[1] = params[1];
531 texObj->CropRect[2] = params[2];
532 texObj->CropRect[3] = params[3];
533 return GL_TRUE;
534
535 case GL_TEXTURE_SWIZZLE_R_EXT:
536 case GL_TEXTURE_SWIZZLE_G_EXT:
537 case GL_TEXTURE_SWIZZLE_B_EXT:
538 case GL_TEXTURE_SWIZZLE_A_EXT:
539 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_swizzle)
540 || _mesa_is_gles3(ctx)) {
541 const GLuint comp = pname - GL_TEXTURE_SWIZZLE_R_EXT;
542 const GLint swz = comp_to_swizzle(params[0]);
543 if (swz < 0) {
544 _mesa_error(ctx, GL_INVALID_ENUM,
545 "glTex%sParameter(swizzle 0x%x)", suffix, params[0]);
546 return GL_FALSE;
547 }
548 assert(comp < 4);
549
550 flush(ctx);
551 texObj->Swizzle[comp] = params[0];
552 set_swizzle_component(&texObj->_Swizzle, comp, swz);
553 return GL_TRUE;
554 }
555 goto invalid_pname;
556
557 case GL_TEXTURE_SWIZZLE_RGBA_EXT:
558 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_swizzle)
559 || _mesa_is_gles3(ctx)) {
560 GLuint comp;
561 flush(ctx);
562 for (comp = 0; comp < 4; comp++) {
563 const GLint swz = comp_to_swizzle(params[comp]);
564 if (swz >= 0) {
565 texObj->Swizzle[comp] = params[comp];
566 set_swizzle_component(&texObj->_Swizzle, comp, swz);
567 }
568 else {
569 _mesa_error(ctx, GL_INVALID_ENUM,
570 "glTex%sParameter(swizzle 0x%x)",
571 suffix, params[comp]);
572 return GL_FALSE;
573 }
574 }
575 return GL_TRUE;
576 }
577 goto invalid_pname;
578
579 case GL_TEXTURE_SRGB_DECODE_EXT:
580 if (ctx->Extensions.EXT_texture_sRGB_decode) {
581 GLenum decode = params[0];
582
583 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
584 goto invalid_enum;
585
586 if (decode == GL_DECODE_EXT || decode == GL_SKIP_DECODE_EXT) {
587 if (texObj->Sampler.sRGBDecode != decode) {
588 flush(ctx);
589 texObj->Sampler.sRGBDecode = decode;
590 }
591 return GL_TRUE;
592 }
593 }
594 goto invalid_pname;
595
596 case GL_TEXTURE_CUBE_MAP_SEAMLESS:
597 if (_mesa_is_desktop_gl(ctx)
598 && ctx->Extensions.AMD_seamless_cubemap_per_texture) {
599 GLenum param = params[0];
600
601 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
602 goto invalid_enum;
603
604 if (param != GL_TRUE && param != GL_FALSE) {
605 goto invalid_param;
606 }
607 if (param != texObj->Sampler.CubeMapSeamless) {
608 flush(ctx);
609 texObj->Sampler.CubeMapSeamless = param;
610 }
611 return GL_TRUE;
612 }
613 goto invalid_pname;
614
615 default:
616 goto invalid_pname;
617 }
618
619 invalid_pname:
620 _mesa_error(ctx, GL_INVALID_ENUM, "glTex%sParameter(pname=%s)",
621 suffix, _mesa_enum_to_string(pname));
622 return GL_FALSE;
623
624 invalid_param:
625 _mesa_error(ctx, GL_INVALID_ENUM, "glTex%sParameter(param=%s)",
626 suffix, _mesa_enum_to_string(params[0]));
627 return GL_FALSE;
628
629 invalid_operation:
630 _mesa_error(ctx, GL_INVALID_OPERATION, "glTex%sParameter(pname=%s)",
631 suffix, _mesa_enum_to_string(pname));
632 return GL_FALSE;
633
634 invalid_enum:
635 _mesa_error(ctx, GL_INVALID_ENUM, "glTex%sParameter(pname=%s)",
636 suffix, _mesa_enum_to_string(pname));
637 return GL_FALSE;
638 }
639
640
641 /**
642 * Set a float-valued texture parameter
643 * \return GL_TRUE if legal AND the value changed, GL_FALSE otherwise
644 */
645 static GLboolean
646 set_tex_parameterf(struct gl_context *ctx,
647 struct gl_texture_object *texObj,
648 GLenum pname, const GLfloat *params, bool dsa)
649 {
650 const char *suffix = dsa ? "ture" : "";
651
652 if (texObj->HandleAllocated) {
653 /* The ARB_bindless_texture spec says:
654 *
655 * "The error INVALID_OPERATION is generated by TexImage*, CopyTexImage*,
656 * CompressedTexImage*, TexBuffer*, TexParameter*, as well as other
657 * functions defined in terms of these, if the texture object to be
658 * modified is referenced by one or more texture or image handles."
659 */
660 _mesa_error(ctx, GL_INVALID_OPERATION,
661 "glTex%sParameter(immutable texture)", suffix);
662 return GL_FALSE;
663 }
664
665 switch (pname) {
666 case GL_TEXTURE_MIN_LOD:
667 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
668 goto invalid_pname;
669
670 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
671 goto invalid_enum;
672
673 if (texObj->Sampler.MinLod == params[0])
674 return GL_FALSE;
675 flush(ctx);
676 texObj->Sampler.MinLod = params[0];
677 return GL_TRUE;
678
679 case GL_TEXTURE_MAX_LOD:
680 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
681 goto invalid_pname;
682
683 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
684 goto invalid_enum;
685
686 if (texObj->Sampler.MaxLod == params[0])
687 return GL_FALSE;
688 flush(ctx);
689 texObj->Sampler.MaxLod = params[0];
690 return GL_TRUE;
691
692 case GL_TEXTURE_PRIORITY:
693 if (ctx->API != API_OPENGL_COMPAT)
694 goto invalid_pname;
695
696 flush(ctx);
697 texObj->Priority = CLAMP(params[0], 0.0F, 1.0F);
698 return GL_TRUE;
699
700 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
701 if (ctx->Extensions.EXT_texture_filter_anisotropic) {
702 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
703 goto invalid_enum;
704
705 if (texObj->Sampler.MaxAnisotropy == params[0])
706 return GL_FALSE;
707 if (params[0] < 1.0F) {
708 _mesa_error(ctx, GL_INVALID_VALUE, "glTex%sParameter(param)",
709 suffix);
710 return GL_FALSE;
711 }
712 flush(ctx);
713 /* clamp to max, that's what NVIDIA does */
714 texObj->Sampler.MaxAnisotropy = MIN2(params[0],
715 ctx->Const.MaxTextureMaxAnisotropy);
716 return GL_TRUE;
717 }
718 else {
719 static GLuint count = 0;
720 if (count++ < 10)
721 goto invalid_pname;
722 }
723 return GL_FALSE;
724
725 case GL_TEXTURE_LOD_BIAS:
726 /* NOTE: this is really part of OpenGL 1.4, not EXT_texture_lod_bias. */
727 if (_mesa_is_gles(ctx))
728 goto invalid_pname;
729
730 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
731 goto invalid_enum;
732
733 if (texObj->Sampler.LodBias != params[0]) {
734 flush(ctx);
735 texObj->Sampler.LodBias = params[0];
736 return GL_TRUE;
737 }
738 break;
739
740 case GL_TEXTURE_BORDER_COLOR:
741 if (ctx->API == API_OPENGLES ||
742 !ctx->Extensions.ARB_texture_border_clamp)
743 goto invalid_pname;
744
745 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target))
746 goto invalid_enum;
747
748 flush(ctx);
749 /* ARB_texture_float disables clamping */
750 if (ctx->Extensions.ARB_texture_float) {
751 texObj->Sampler.BorderColor.f[RCOMP] = params[0];
752 texObj->Sampler.BorderColor.f[GCOMP] = params[1];
753 texObj->Sampler.BorderColor.f[BCOMP] = params[2];
754 texObj->Sampler.BorderColor.f[ACOMP] = params[3];
755 } else {
756 texObj->Sampler.BorderColor.f[RCOMP] = CLAMP(params[0], 0.0F, 1.0F);
757 texObj->Sampler.BorderColor.f[GCOMP] = CLAMP(params[1], 0.0F, 1.0F);
758 texObj->Sampler.BorderColor.f[BCOMP] = CLAMP(params[2], 0.0F, 1.0F);
759 texObj->Sampler.BorderColor.f[ACOMP] = CLAMP(params[3], 0.0F, 1.0F);
760 }
761 return GL_TRUE;
762
763 default:
764 goto invalid_pname;
765 }
766 return GL_FALSE;
767
768 invalid_pname:
769 _mesa_error(ctx, GL_INVALID_ENUM, "glTex%sParameter(pname=%s)",
770 suffix, _mesa_enum_to_string(pname));
771 return GL_FALSE;
772
773 invalid_enum:
774 _mesa_error(ctx, GL_INVALID_ENUM, "glTex%sParameter(pname=%s)",
775 suffix, _mesa_enum_to_string(pname));
776 return GL_FALSE;
777 }
778
779
780 void
781 _mesa_texture_parameterf(struct gl_context *ctx,
782 struct gl_texture_object *texObj,
783 GLenum pname, GLfloat param, bool dsa)
784 {
785 GLboolean need_update;
786
787 switch (pname) {
788 case GL_TEXTURE_MIN_FILTER:
789 case GL_TEXTURE_MAG_FILTER:
790 case GL_TEXTURE_WRAP_S:
791 case GL_TEXTURE_WRAP_T:
792 case GL_TEXTURE_WRAP_R:
793 case GL_TEXTURE_BASE_LEVEL:
794 case GL_TEXTURE_MAX_LEVEL:
795 case GL_GENERATE_MIPMAP_SGIS:
796 case GL_TEXTURE_COMPARE_MODE_ARB:
797 case GL_TEXTURE_COMPARE_FUNC_ARB:
798 case GL_DEPTH_TEXTURE_MODE_ARB:
799 case GL_DEPTH_STENCIL_TEXTURE_MODE:
800 case GL_TEXTURE_SRGB_DECODE_EXT:
801 case GL_TEXTURE_CUBE_MAP_SEAMLESS:
802 case GL_TEXTURE_SWIZZLE_R_EXT:
803 case GL_TEXTURE_SWIZZLE_G_EXT:
804 case GL_TEXTURE_SWIZZLE_B_EXT:
805 case GL_TEXTURE_SWIZZLE_A_EXT:
806 {
807 GLint p[4];
808 p[0] = (param > 0) ?
809 ((param > INT_MAX) ? INT_MAX : (GLint) (param + 0.5)) :
810 ((param < INT_MIN) ? INT_MIN : (GLint) (param - 0.5));
811
812 p[1] = p[2] = p[3] = 0;
813 need_update = set_tex_parameteri(ctx, texObj, pname, p, dsa);
814 }
815 break;
816 case GL_TEXTURE_BORDER_COLOR:
817 case GL_TEXTURE_SWIZZLE_RGBA:
818 _mesa_error(ctx, GL_INVALID_ENUM, "glTex%sParameterf(non-scalar pname)",
819 dsa ? "ture" : "");
820 return;
821 default:
822 {
823 /* this will generate an error if pname is illegal */
824 GLfloat p[4];
825 p[0] = param;
826 p[1] = p[2] = p[3] = 0.0F;
827 need_update = set_tex_parameterf(ctx, texObj, pname, p, dsa);
828 }
829 }
830
831 if (ctx->Driver.TexParameter && need_update) {
832 ctx->Driver.TexParameter(ctx, texObj, pname);
833 }
834 }
835
836
837 void
838 _mesa_texture_parameterfv(struct gl_context *ctx,
839 struct gl_texture_object *texObj,
840 GLenum pname, const GLfloat *params, bool dsa)
841 {
842 GLboolean need_update;
843 switch (pname) {
844 case GL_TEXTURE_MIN_FILTER:
845 case GL_TEXTURE_MAG_FILTER:
846 case GL_TEXTURE_WRAP_S:
847 case GL_TEXTURE_WRAP_T:
848 case GL_TEXTURE_WRAP_R:
849 case GL_TEXTURE_BASE_LEVEL:
850 case GL_TEXTURE_MAX_LEVEL:
851 case GL_GENERATE_MIPMAP_SGIS:
852 case GL_TEXTURE_COMPARE_MODE_ARB:
853 case GL_TEXTURE_COMPARE_FUNC_ARB:
854 case GL_DEPTH_TEXTURE_MODE_ARB:
855 case GL_DEPTH_STENCIL_TEXTURE_MODE:
856 case GL_TEXTURE_SRGB_DECODE_EXT:
857 case GL_TEXTURE_CUBE_MAP_SEAMLESS:
858 {
859 /* convert float param to int */
860 GLint p[4];
861 p[0] = (GLint) params[0];
862 p[1] = p[2] = p[3] = 0;
863 need_update = set_tex_parameteri(ctx, texObj, pname, p, dsa);
864 }
865 break;
866 case GL_TEXTURE_CROP_RECT_OES:
867 {
868 /* convert float params to int */
869 GLint iparams[4];
870 iparams[0] = (GLint) params[0];
871 iparams[1] = (GLint) params[1];
872 iparams[2] = (GLint) params[2];
873 iparams[3] = (GLint) params[3];
874 need_update = set_tex_parameteri(ctx, texObj, pname, iparams, dsa);
875 }
876 break;
877 case GL_TEXTURE_SWIZZLE_R_EXT:
878 case GL_TEXTURE_SWIZZLE_G_EXT:
879 case GL_TEXTURE_SWIZZLE_B_EXT:
880 case GL_TEXTURE_SWIZZLE_A_EXT:
881 case GL_TEXTURE_SWIZZLE_RGBA_EXT:
882 {
883 GLint p[4] = {0, 0, 0, 0};
884 p[0] = (GLint) params[0];
885 if (pname == GL_TEXTURE_SWIZZLE_RGBA_EXT) {
886 p[1] = (GLint) params[1];
887 p[2] = (GLint) params[2];
888 p[3] = (GLint) params[3];
889 }
890 need_update = set_tex_parameteri(ctx, texObj, pname, p, dsa);
891 }
892 break;
893 default:
894 /* this will generate an error if pname is illegal */
895 need_update = set_tex_parameterf(ctx, texObj, pname, params, dsa);
896 }
897
898 if (ctx->Driver.TexParameter && need_update) {
899 ctx->Driver.TexParameter(ctx, texObj, pname);
900 }
901 }
902
903
904 void
905 _mesa_texture_parameteri(struct gl_context *ctx,
906 struct gl_texture_object *texObj,
907 GLenum pname, GLint param, bool dsa)
908 {
909 GLboolean need_update;
910 switch (pname) {
911 case GL_TEXTURE_MIN_LOD:
912 case GL_TEXTURE_MAX_LOD:
913 case GL_TEXTURE_PRIORITY:
914 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
915 case GL_TEXTURE_LOD_BIAS:
916 case GL_TEXTURE_COMPARE_FAIL_VALUE_ARB:
917 {
918 GLfloat fparam[4];
919 fparam[0] = (GLfloat) param;
920 fparam[1] = fparam[2] = fparam[3] = 0.0F;
921 /* convert int param to float */
922 need_update = set_tex_parameterf(ctx, texObj, pname, fparam, dsa);
923 }
924 break;
925 case GL_TEXTURE_BORDER_COLOR:
926 case GL_TEXTURE_SWIZZLE_RGBA:
927 {
928 _mesa_error(ctx, GL_INVALID_ENUM,
929 "glTex%sParameteri(non-scalar pname)",
930 dsa ? "ture" : "");
931 return;
932 }
933 default:
934 /* this will generate an error if pname is illegal */
935 {
936 GLint iparam[4];
937 iparam[0] = param;
938 iparam[1] = iparam[2] = iparam[3] = 0;
939 need_update = set_tex_parameteri(ctx, texObj, pname, iparam, dsa);
940 }
941 }
942
943 if (ctx->Driver.TexParameter && need_update) {
944 ctx->Driver.TexParameter(ctx, texObj, pname);
945 }
946 }
947
948
949 void
950 _mesa_texture_parameteriv(struct gl_context *ctx,
951 struct gl_texture_object *texObj,
952 GLenum pname, const GLint *params, bool dsa)
953 {
954 GLboolean need_update;
955
956 switch (pname) {
957 case GL_TEXTURE_BORDER_COLOR:
958 {
959 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target)) {
960 _mesa_error(ctx, GL_INVALID_ENUM, "glTextureParameteriv(texture)");
961 return;
962 }
963 /* convert int params to float */
964 GLfloat fparams[4];
965 fparams[0] = INT_TO_FLOAT(params[0]);
966 fparams[1] = INT_TO_FLOAT(params[1]);
967 fparams[2] = INT_TO_FLOAT(params[2]);
968 fparams[3] = INT_TO_FLOAT(params[3]);
969 need_update = set_tex_parameterf(ctx, texObj, pname, fparams, dsa);
970 }
971 break;
972 case GL_TEXTURE_MIN_LOD:
973 case GL_TEXTURE_MAX_LOD:
974 case GL_TEXTURE_PRIORITY:
975 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
976 case GL_TEXTURE_LOD_BIAS:
977 case GL_TEXTURE_COMPARE_FAIL_VALUE_ARB:
978 {
979 /* convert int param to float */
980 GLfloat fparams[4];
981 fparams[0] = (GLfloat) params[0];
982 fparams[1] = fparams[2] = fparams[3] = 0.0F;
983 need_update = set_tex_parameterf(ctx, texObj, pname, fparams, dsa);
984 }
985 break;
986 default:
987 /* this will generate an error if pname is illegal */
988 need_update = set_tex_parameteri(ctx, texObj, pname, params, dsa);
989 }
990
991 if (ctx->Driver.TexParameter && need_update) {
992 ctx->Driver.TexParameter(ctx, texObj, pname);
993 }
994 }
995
996 void
997 _mesa_texture_parameterIiv(struct gl_context *ctx,
998 struct gl_texture_object *texObj,
999 GLenum pname, const GLint *params, bool dsa)
1000 {
1001 switch (pname) {
1002 case GL_TEXTURE_BORDER_COLOR:
1003 if (texObj->HandleAllocated) {
1004 _mesa_error(ctx, GL_INVALID_OPERATION,
1005 "glTextureParameterIiv(immutable texture)");
1006 return;
1007 }
1008
1009 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target)) {
1010 _mesa_error(ctx, GL_INVALID_ENUM, "glTextureParameterIiv(texture)");
1011 return;
1012 }
1013 FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT);
1014 /* set the integer-valued border color */
1015 COPY_4V(texObj->Sampler.BorderColor.i, params);
1016 break;
1017 default:
1018 _mesa_texture_parameteriv(ctx, texObj, pname, params, dsa);
1019 break;
1020 }
1021 /* XXX no driver hook for TexParameterIiv() yet */
1022 }
1023
1024 void
1025 _mesa_texture_parameterIuiv(struct gl_context *ctx,
1026 struct gl_texture_object *texObj,
1027 GLenum pname, const GLuint *params, bool dsa)
1028 {
1029 switch (pname) {
1030 case GL_TEXTURE_BORDER_COLOR:
1031 if (texObj->HandleAllocated) {
1032 _mesa_error(ctx, GL_INVALID_OPERATION,
1033 "glTextureParameterIuiv(immutable texture)");
1034 return;
1035 }
1036
1037 if (!_mesa_target_allows_setting_sampler_parameters(texObj->Target)) {
1038 _mesa_error(ctx, GL_INVALID_ENUM, "glTextureParameterIuiv(texture)");
1039 return;
1040 }
1041 FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT);
1042 /* set the unsigned integer-valued border color */
1043 COPY_4V(texObj->Sampler.BorderColor.ui, params);
1044 break;
1045 default:
1046 _mesa_texture_parameteriv(ctx, texObj, pname, (const GLint *) params,
1047 dsa);
1048 break;
1049 }
1050 /* XXX no driver hook for TexParameterIuiv() yet */
1051 }
1052
1053 void GLAPIENTRY
1054 _mesa_TexParameterf(GLenum target, GLenum pname, GLfloat param)
1055 {
1056 struct gl_texture_object *texObj;
1057 GET_CURRENT_CONTEXT(ctx);
1058
1059 texObj = get_texobj_by_target(ctx, target, GL_FALSE);
1060 if (!texObj)
1061 return;
1062
1063 _mesa_texture_parameterf(ctx, texObj, pname, param, false);
1064 }
1065
1066 void GLAPIENTRY
1067 _mesa_TexParameterfv(GLenum target, GLenum pname, const GLfloat *params)
1068 {
1069 struct gl_texture_object *texObj;
1070 GET_CURRENT_CONTEXT(ctx);
1071
1072 texObj = get_texobj_by_target(ctx, target, GL_FALSE);
1073 if (!texObj)
1074 return;
1075
1076 _mesa_texture_parameterfv(ctx, texObj, pname, params, false);
1077 }
1078
1079 void GLAPIENTRY
1080 _mesa_TexParameteri(GLenum target, GLenum pname, GLint param)
1081 {
1082 struct gl_texture_object *texObj;
1083 GET_CURRENT_CONTEXT(ctx);
1084
1085 texObj = get_texobj_by_target(ctx, target, GL_FALSE);
1086 if (!texObj)
1087 return;
1088
1089 _mesa_texture_parameteri(ctx, texObj, pname, param, false);
1090 }
1091
1092 void GLAPIENTRY
1093 _mesa_TexParameteriv(GLenum target, GLenum pname, const GLint *params)
1094 {
1095 struct gl_texture_object *texObj;
1096 GET_CURRENT_CONTEXT(ctx);
1097
1098 texObj = get_texobj_by_target(ctx, target, GL_FALSE);
1099 if (!texObj)
1100 return;
1101
1102 _mesa_texture_parameteriv(ctx, texObj, pname, params, false);
1103 }
1104
1105 /**
1106 * Set tex parameter to integer value(s). Primarily intended to set
1107 * integer-valued texture border color (for integer-valued textures).
1108 * New in GL 3.0.
1109 */
1110 void GLAPIENTRY
1111 _mesa_TexParameterIiv(GLenum target, GLenum pname, const GLint *params)
1112 {
1113 struct gl_texture_object *texObj;
1114 GET_CURRENT_CONTEXT(ctx);
1115
1116 texObj = get_texobj_by_target(ctx, target, GL_FALSE);
1117 if (!texObj)
1118 return;
1119
1120 _mesa_texture_parameterIiv(ctx, texObj, pname, params, false);
1121 }
1122
1123 /**
1124 * Set tex parameter to unsigned integer value(s). Primarily intended to set
1125 * uint-valued texture border color (for integer-valued textures).
1126 * New in GL 3.0
1127 */
1128 void GLAPIENTRY
1129 _mesa_TexParameterIuiv(GLenum target, GLenum pname, const GLuint *params)
1130 {
1131 struct gl_texture_object *texObj;
1132 GET_CURRENT_CONTEXT(ctx);
1133
1134 texObj = get_texobj_by_target(ctx, target, GL_FALSE);
1135 if (!texObj)
1136 return;
1137
1138 _mesa_texture_parameterIuiv(ctx, texObj, pname, params, false);
1139 }
1140
1141
1142 void GLAPIENTRY
1143 _mesa_TextureParameterfv(GLuint texture, GLenum pname, const GLfloat *params)
1144 {
1145 struct gl_texture_object *texObj;
1146 GET_CURRENT_CONTEXT(ctx);
1147
1148 texObj = get_texobj_by_name(ctx, texture, "glTextureParameterfv");
1149 if (!texObj)
1150 return;
1151
1152 _mesa_texture_parameterfv(ctx, texObj, pname, params, true);
1153 }
1154
1155 void GLAPIENTRY
1156 _mesa_TextureParameterf(GLuint texture, GLenum pname, GLfloat param)
1157 {
1158 struct gl_texture_object *texObj;
1159 GET_CURRENT_CONTEXT(ctx);
1160
1161 texObj = get_texobj_by_name(ctx, texture, "glTextureParameterf");
1162 if (!texObj)
1163 return;
1164
1165 _mesa_texture_parameterf(ctx, texObj, pname, param, true);
1166 }
1167
1168 void GLAPIENTRY
1169 _mesa_TextureParameteri(GLuint texture, GLenum pname, GLint param)
1170 {
1171 struct gl_texture_object *texObj;
1172 GET_CURRENT_CONTEXT(ctx);
1173
1174 texObj = get_texobj_by_name(ctx, texture, "glTextureParameteri");
1175 if (!texObj)
1176 return;
1177
1178 _mesa_texture_parameteri(ctx, texObj, pname, param, true);
1179 }
1180
1181 void GLAPIENTRY
1182 _mesa_TextureParameteriv(GLuint texture, GLenum pname,
1183 const GLint *params)
1184 {
1185 struct gl_texture_object *texObj;
1186 GET_CURRENT_CONTEXT(ctx);
1187
1188 texObj = get_texobj_by_name(ctx, texture, "glTextureParameteriv");
1189 if (!texObj)
1190 return;
1191
1192 _mesa_texture_parameteriv(ctx, texObj, pname, params, true);
1193 }
1194
1195
1196 void GLAPIENTRY
1197 _mesa_TextureParameterIiv(GLuint texture, GLenum pname, const GLint *params)
1198 {
1199 struct gl_texture_object *texObj;
1200 GET_CURRENT_CONTEXT(ctx);
1201
1202 texObj = get_texobj_by_name(ctx, texture, "glTextureParameterIiv");
1203 if (!texObj)
1204 return;
1205
1206 _mesa_texture_parameterIiv(ctx, texObj, pname, params, true);
1207 }
1208
1209 void GLAPIENTRY
1210 _mesa_TextureParameterIuiv(GLuint texture, GLenum pname, const GLuint *params)
1211 {
1212 struct gl_texture_object *texObj;
1213 GET_CURRENT_CONTEXT(ctx);
1214
1215 texObj = get_texobj_by_name(ctx, texture, "glTextureParameterIuiv");
1216 if (!texObj)
1217 return;
1218
1219 _mesa_texture_parameterIuiv(ctx, texObj, pname, params, true);
1220 }
1221
1222 GLboolean
1223 _mesa_legal_get_tex_level_parameter_target(struct gl_context *ctx, GLenum target,
1224 bool dsa)
1225 {
1226 /* Common targets for desktop GL and GLES 3.1. */
1227 switch (target) {
1228 case GL_TEXTURE_2D:
1229 case GL_TEXTURE_3D:
1230 return GL_TRUE;
1231 case GL_TEXTURE_2D_ARRAY_EXT:
1232 return ctx->Extensions.EXT_texture_array;
1233 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
1234 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
1235 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
1236 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
1237 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
1238 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
1239 return ctx->Extensions.ARB_texture_cube_map;
1240 case GL_TEXTURE_2D_MULTISAMPLE:
1241 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1242 return ctx->Extensions.ARB_texture_multisample;
1243 case GL_TEXTURE_BUFFER:
1244 /* GetTexLevelParameter accepts GL_TEXTURE_BUFFER in GL 3.1+ contexts,
1245 * but not in earlier versions that expose ARB_texture_buffer_object.
1246 *
1247 * From the ARB_texture_buffer_object spec:
1248 * "(7) Do buffer textures support texture parameters (TexParameter) or
1249 * queries (GetTexParameter, GetTexLevelParameter, GetTexImage)?
1250 *
1251 * RESOLVED: No. [...] Note that the spec edits above don't add
1252 * explicit error language for any of these cases. That is because
1253 * each of the functions enumerate the set of valid <target>
1254 * parameters. Not editing the spec to allow TEXTURE_BUFFER_ARB in
1255 * these cases means that target is not legal, and an INVALID_ENUM
1256 * error should be generated."
1257 *
1258 * From the OpenGL 3.1 spec:
1259 * "target may also be TEXTURE_BUFFER, indicating the texture buffer."
1260 */
1261 return (ctx->API == API_OPENGL_CORE && ctx->Version >= 31) ||
1262 _mesa_has_OES_texture_buffer(ctx);
1263 case GL_TEXTURE_CUBE_MAP_ARRAY:
1264 return _mesa_has_texture_cube_map_array(ctx);
1265 }
1266
1267 if (!_mesa_is_desktop_gl(ctx))
1268 return GL_FALSE;
1269
1270 /* Rest of the desktop GL targets. */
1271 switch (target) {
1272 case GL_TEXTURE_1D:
1273 case GL_PROXY_TEXTURE_1D:
1274 case GL_PROXY_TEXTURE_2D:
1275 case GL_PROXY_TEXTURE_3D:
1276 return GL_TRUE;
1277 case GL_PROXY_TEXTURE_CUBE_MAP:
1278 return ctx->Extensions.ARB_texture_cube_map;
1279 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
1280 return ctx->Extensions.ARB_texture_cube_map_array;
1281 case GL_TEXTURE_RECTANGLE_NV:
1282 case GL_PROXY_TEXTURE_RECTANGLE_NV:
1283 return ctx->Extensions.NV_texture_rectangle;
1284 case GL_TEXTURE_1D_ARRAY_EXT:
1285 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
1286 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
1287 return ctx->Extensions.EXT_texture_array;
1288 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
1289 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
1290 return ctx->Extensions.ARB_texture_multisample;
1291
1292 /* This is a valid target for dsa, but the OpenGL 4.5 core spec
1293 * (30.10.2014) Section 8.11 Texture Queries says:
1294 * "For GetTextureLevelParameter* only, texture may also be a cube
1295 * map texture object. In this case the query is always performed
1296 * for face zero (the TEXTURE_CUBE_MAP_POSITIVE_X face), since there
1297 * is no way to specify another face."
1298 */
1299 case GL_TEXTURE_CUBE_MAP:
1300 return dsa;
1301 default:
1302 return GL_FALSE;
1303 }
1304 }
1305
1306
1307 static void
1308 get_tex_level_parameter_image(struct gl_context *ctx,
1309 const struct gl_texture_object *texObj,
1310 GLenum target, GLint level,
1311 GLenum pname, GLint *params,
1312 bool dsa)
1313 {
1314 const struct gl_texture_image *img = NULL;
1315 struct gl_texture_image dummy_image;
1316 mesa_format texFormat;
1317 const char *suffix = dsa ? "ture" : "";
1318
1319 img = _mesa_select_tex_image(texObj, target, level);
1320 if (!img || img->TexFormat == MESA_FORMAT_NONE) {
1321 /* In case of undefined texture image return the default values.
1322 *
1323 * From OpenGL 4.0 spec, page 398:
1324 * "The initial internal format of a texel array is RGBA
1325 * instead of 1. TEXTURE_COMPONENTS is deprecated; always
1326 * use TEXTURE_INTERNAL_FORMAT."
1327 */
1328 memset(&dummy_image, 0, sizeof(dummy_image));
1329 dummy_image.TexFormat = MESA_FORMAT_NONE;
1330 dummy_image.InternalFormat = GL_RGBA;
1331 dummy_image._BaseFormat = GL_NONE;
1332 dummy_image.FixedSampleLocations = GL_TRUE;
1333
1334 img = &dummy_image;
1335 }
1336
1337 texFormat = img->TexFormat;
1338
1339 switch (pname) {
1340 case GL_TEXTURE_WIDTH:
1341 *params = img->Width;
1342 break;
1343 case GL_TEXTURE_HEIGHT:
1344 *params = img->Height;
1345 break;
1346 case GL_TEXTURE_DEPTH:
1347 *params = img->Depth;
1348 break;
1349 case GL_TEXTURE_INTERNAL_FORMAT:
1350 if (_mesa_is_format_compressed(texFormat)) {
1351 /* need to return the actual compressed format */
1352 *params = _mesa_compressed_format_to_glenum(ctx, texFormat);
1353 }
1354 else {
1355 /* If the true internal format is not compressed but the user
1356 * requested a generic compressed format, we have to return the
1357 * generic base format that matches.
1358 *
1359 * From page 119 (page 129 of the PDF) of the OpenGL 1.3 spec:
1360 *
1361 * "If no specific compressed format is available,
1362 * internalformat is instead replaced by the corresponding base
1363 * internal format."
1364 *
1365 * Otherwise just return the user's requested internal format
1366 */
1367 const GLenum f =
1368 _mesa_gl_compressed_format_base_format(img->InternalFormat);
1369
1370 *params = (f != 0) ? f : img->InternalFormat;
1371 }
1372 break;
1373 case GL_TEXTURE_BORDER:
1374 if (ctx->API != API_OPENGL_COMPAT)
1375 goto invalid_pname;
1376 *params = img->Border;
1377 break;
1378 case GL_TEXTURE_RED_SIZE:
1379 case GL_TEXTURE_GREEN_SIZE:
1380 case GL_TEXTURE_BLUE_SIZE:
1381 case GL_TEXTURE_ALPHA_SIZE:
1382 if (_mesa_base_format_has_channel(img->_BaseFormat, pname))
1383 *params = _mesa_get_format_bits(texFormat, pname);
1384 else
1385 *params = 0;
1386 break;
1387 case GL_TEXTURE_INTENSITY_SIZE:
1388 case GL_TEXTURE_LUMINANCE_SIZE:
1389 if (ctx->API != API_OPENGL_COMPAT)
1390 goto invalid_pname;
1391 if (_mesa_base_format_has_channel(img->_BaseFormat, pname)) {
1392 *params = _mesa_get_format_bits(texFormat, pname);
1393 if (*params == 0) {
1394 /* intensity or luminance is probably stored as RGB[A] */
1395 *params = MIN2(_mesa_get_format_bits(texFormat,
1396 GL_TEXTURE_RED_SIZE),
1397 _mesa_get_format_bits(texFormat,
1398 GL_TEXTURE_GREEN_SIZE));
1399 }
1400 }
1401 else {
1402 *params = 0;
1403 }
1404 break;
1405 case GL_TEXTURE_DEPTH_SIZE_ARB:
1406 if (!ctx->Extensions.ARB_depth_texture)
1407 goto invalid_pname;
1408 *params = _mesa_get_format_bits(texFormat, pname);
1409 break;
1410 case GL_TEXTURE_STENCIL_SIZE:
1411 *params = _mesa_get_format_bits(texFormat, pname);
1412 break;
1413 case GL_TEXTURE_SHARED_SIZE:
1414 if (ctx->Version < 30 &&
1415 !ctx->Extensions.EXT_texture_shared_exponent)
1416 goto invalid_pname;
1417 *params = texFormat == MESA_FORMAT_R9G9B9E5_FLOAT ? 5 : 0;
1418 break;
1419
1420 /* GL_ARB_texture_compression */
1421 case GL_TEXTURE_COMPRESSED_IMAGE_SIZE:
1422 if (_mesa_is_format_compressed(texFormat) &&
1423 !_mesa_is_proxy_texture(target)) {
1424 *params = _mesa_format_image_size(texFormat, img->Width,
1425 img->Height, img->Depth);
1426 } else {
1427 _mesa_error(ctx, GL_INVALID_OPERATION,
1428 "glGetTex%sLevelParameter[if]v(pname=%s)", suffix,
1429 _mesa_enum_to_string(pname));
1430 }
1431 break;
1432 case GL_TEXTURE_COMPRESSED:
1433 *params = (GLint) _mesa_is_format_compressed(texFormat);
1434 break;
1435
1436 /* GL_ARB_texture_float */
1437 case GL_TEXTURE_LUMINANCE_TYPE_ARB:
1438 case GL_TEXTURE_INTENSITY_TYPE_ARB:
1439 if (ctx->API != API_OPENGL_COMPAT)
1440 goto invalid_pname;
1441 /* FALLTHROUGH */
1442 case GL_TEXTURE_RED_TYPE_ARB:
1443 case GL_TEXTURE_GREEN_TYPE_ARB:
1444 case GL_TEXTURE_BLUE_TYPE_ARB:
1445 case GL_TEXTURE_ALPHA_TYPE_ARB:
1446 case GL_TEXTURE_DEPTH_TYPE_ARB:
1447 if (!ctx->Extensions.ARB_texture_float)
1448 goto invalid_pname;
1449 if (_mesa_base_format_has_channel(img->_BaseFormat, pname))
1450 *params = _mesa_get_format_datatype(texFormat);
1451 else
1452 *params = GL_NONE;
1453 break;
1454
1455 /* GL_ARB_texture_multisample */
1456 case GL_TEXTURE_SAMPLES:
1457 if (!ctx->Extensions.ARB_texture_multisample)
1458 goto invalid_pname;
1459 *params = img->NumSamples;
1460 break;
1461
1462 case GL_TEXTURE_FIXED_SAMPLE_LOCATIONS:
1463 if (!ctx->Extensions.ARB_texture_multisample)
1464 goto invalid_pname;
1465 *params = img->FixedSampleLocations;
1466 break;
1467
1468 /* There is never a buffer data store here, but these pnames still have
1469 * to work.
1470 */
1471
1472 /* GL_ARB_texture_buffer_object */
1473 case GL_TEXTURE_BUFFER_DATA_STORE_BINDING:
1474 if (!ctx->Extensions.ARB_texture_buffer_object)
1475 goto invalid_pname;
1476 *params = 0;
1477 break;
1478
1479 /* GL_ARB_texture_buffer_range */
1480 case GL_TEXTURE_BUFFER_OFFSET:
1481 if (!ctx->Extensions.ARB_texture_buffer_range)
1482 goto invalid_pname;
1483 *params = 0;
1484 break;
1485 case GL_TEXTURE_BUFFER_SIZE:
1486 if (!ctx->Extensions.ARB_texture_buffer_range)
1487 goto invalid_pname;
1488 *params = 0;
1489 break;
1490
1491 default:
1492 goto invalid_pname;
1493 }
1494
1495 /* no error if we get here */
1496 return;
1497
1498 invalid_pname:
1499 _mesa_error(ctx, GL_INVALID_ENUM,
1500 "glGetTex%sLevelParameter[if]v(pname=%s)", suffix,
1501 _mesa_enum_to_string(pname));
1502 }
1503
1504
1505 /**
1506 * Handle a glGetTexLevelParamteriv() call for a texture buffer.
1507 */
1508 static void
1509 get_tex_level_parameter_buffer(struct gl_context *ctx,
1510 const struct gl_texture_object *texObj,
1511 GLenum pname, GLint *params, bool dsa)
1512 {
1513 const struct gl_buffer_object *bo = texObj->BufferObject;
1514 mesa_format texFormat = texObj->_BufferObjectFormat;
1515 int bytes = MAX2(1, _mesa_get_format_bytes(texFormat));
1516 GLenum internalFormat = texObj->BufferObjectFormat;
1517 GLenum baseFormat = _mesa_get_format_base_format(texFormat);
1518 const char *suffix = dsa ? "ture" : "";
1519
1520 assert(texObj->Target == GL_TEXTURE_BUFFER);
1521
1522 if (!bo) {
1523 /* undefined texture buffer object */
1524 switch (pname) {
1525 case GL_TEXTURE_FIXED_SAMPLE_LOCATIONS:
1526 *params = GL_TRUE;
1527 break;
1528 case GL_TEXTURE_INTERNAL_FORMAT:
1529 *params = internalFormat;
1530 break;
1531 default:
1532 *params = 0;
1533 break;
1534 }
1535 return;
1536 }
1537
1538 switch (pname) {
1539 case GL_TEXTURE_BUFFER_DATA_STORE_BINDING:
1540 *params = bo->Name;
1541 break;
1542 case GL_TEXTURE_WIDTH:
1543 *params = ((texObj->BufferSize == -1) ? bo->Size : texObj->BufferSize)
1544 / bytes;
1545 break;
1546 case GL_TEXTURE_HEIGHT:
1547 case GL_TEXTURE_DEPTH:
1548 *params = 1;
1549 break;
1550 case GL_TEXTURE_BORDER:
1551 case GL_TEXTURE_SHARED_SIZE:
1552 case GL_TEXTURE_COMPRESSED:
1553 *params = 0;
1554 break;
1555 case GL_TEXTURE_INTERNAL_FORMAT:
1556 *params = internalFormat;
1557 break;
1558 case GL_TEXTURE_RED_SIZE:
1559 case GL_TEXTURE_GREEN_SIZE:
1560 case GL_TEXTURE_BLUE_SIZE:
1561 case GL_TEXTURE_ALPHA_SIZE:
1562 if (_mesa_base_format_has_channel(baseFormat, pname))
1563 *params = _mesa_get_format_bits(texFormat, pname);
1564 else
1565 *params = 0;
1566 break;
1567 case GL_TEXTURE_INTENSITY_SIZE:
1568 case GL_TEXTURE_LUMINANCE_SIZE:
1569 if (_mesa_base_format_has_channel(baseFormat, pname)) {
1570 *params = _mesa_get_format_bits(texFormat, pname);
1571 if (*params == 0) {
1572 /* intensity or luminance is probably stored as RGB[A] */
1573 *params = MIN2(_mesa_get_format_bits(texFormat,
1574 GL_TEXTURE_RED_SIZE),
1575 _mesa_get_format_bits(texFormat,
1576 GL_TEXTURE_GREEN_SIZE));
1577 }
1578 } else {
1579 *params = 0;
1580 }
1581 break;
1582 case GL_TEXTURE_DEPTH_SIZE_ARB:
1583 case GL_TEXTURE_STENCIL_SIZE_EXT:
1584 *params = _mesa_get_format_bits(texFormat, pname);
1585 break;
1586
1587 /* GL_ARB_texture_buffer_range */
1588 case GL_TEXTURE_BUFFER_OFFSET:
1589 if (!ctx->Extensions.ARB_texture_buffer_range)
1590 goto invalid_pname;
1591 *params = texObj->BufferOffset;
1592 break;
1593 case GL_TEXTURE_BUFFER_SIZE:
1594 if (!ctx->Extensions.ARB_texture_buffer_range)
1595 goto invalid_pname;
1596 *params = (texObj->BufferSize == -1) ? bo->Size : texObj->BufferSize;
1597 break;
1598
1599 /* GL_ARB_texture_multisample */
1600 case GL_TEXTURE_SAMPLES:
1601 if (!ctx->Extensions.ARB_texture_multisample)
1602 goto invalid_pname;
1603 *params = 0;
1604 break;
1605
1606 case GL_TEXTURE_FIXED_SAMPLE_LOCATIONS:
1607 if (!ctx->Extensions.ARB_texture_multisample)
1608 goto invalid_pname;
1609 *params = GL_TRUE;
1610 break;
1611
1612 /* GL_ARB_texture_compression */
1613 case GL_TEXTURE_COMPRESSED_IMAGE_SIZE:
1614 /* Always illegal for GL_TEXTURE_BUFFER */
1615 _mesa_error(ctx, GL_INVALID_OPERATION,
1616 "glGetTex%sLevelParameter[if]v(pname=%s)", suffix,
1617 _mesa_enum_to_string(pname));
1618 break;
1619
1620 /* GL_ARB_texture_float */
1621 case GL_TEXTURE_RED_TYPE_ARB:
1622 case GL_TEXTURE_GREEN_TYPE_ARB:
1623 case GL_TEXTURE_BLUE_TYPE_ARB:
1624 case GL_TEXTURE_ALPHA_TYPE_ARB:
1625 case GL_TEXTURE_LUMINANCE_TYPE_ARB:
1626 case GL_TEXTURE_INTENSITY_TYPE_ARB:
1627 case GL_TEXTURE_DEPTH_TYPE_ARB:
1628 if (!ctx->Extensions.ARB_texture_float)
1629 goto invalid_pname;
1630 if (_mesa_base_format_has_channel(baseFormat, pname))
1631 *params = _mesa_get_format_datatype(texFormat);
1632 else
1633 *params = GL_NONE;
1634 break;
1635
1636 default:
1637 goto invalid_pname;
1638 }
1639
1640 /* no error if we get here */
1641 return;
1642
1643 invalid_pname:
1644 _mesa_error(ctx, GL_INVALID_ENUM,
1645 "glGetTex%sLevelParameter[if]v(pname=%s)", suffix,
1646 _mesa_enum_to_string(pname));
1647 }
1648
1649 static bool
1650 valid_tex_level_parameteriv_target(struct gl_context *ctx, GLenum target,
1651 bool dsa)
1652 {
1653 const char *suffix = dsa ? "ture" : "";
1654 if (!_mesa_legal_get_tex_level_parameter_target(ctx, target, dsa)) {
1655 _mesa_error(ctx, GL_INVALID_ENUM,
1656 "glGetTex%sLevelParameter[if]v(target=%s)", suffix,
1657 _mesa_enum_to_string(target));
1658 return false;
1659 }
1660 return true;
1661 }
1662
1663 /**
1664 * This isn't exposed to the rest of the driver because it is a part of the
1665 * OpenGL API that is rarely used.
1666 */
1667 static void
1668 get_tex_level_parameteriv(struct gl_context *ctx,
1669 struct gl_texture_object *texObj,
1670 GLenum target, GLint level,
1671 GLenum pname, GLint *params,
1672 bool dsa)
1673 {
1674 GLint maxLevels;
1675 const char *suffix = dsa ? "ture" : "";
1676
1677 /* Check for errors */
1678 if (ctx->Texture.CurrentUnit >= ctx->Const.MaxCombinedTextureImageUnits) {
1679 _mesa_error(ctx, GL_INVALID_OPERATION,
1680 "glGetTex%sLevelParameter[if]v("
1681 "current unit >= max combined texture units)", suffix);
1682 return;
1683 }
1684
1685 maxLevels = _mesa_max_texture_levels(ctx, target);
1686 assert(maxLevels != 0);
1687
1688 if (level < 0 || level >= maxLevels) {
1689 _mesa_error(ctx, GL_INVALID_VALUE,
1690 "glGetTex%sLevelParameter[if]v(level out of range)", suffix);
1691 return;
1692 }
1693
1694 /* Get the level parameter */
1695 if (target == GL_TEXTURE_BUFFER) {
1696 get_tex_level_parameter_buffer(ctx, texObj, pname, params, dsa);
1697 }
1698 else {
1699 get_tex_level_parameter_image(ctx, texObj, target,
1700 level, pname, params, dsa);
1701 }
1702 }
1703
1704 void GLAPIENTRY
1705 _mesa_GetTexLevelParameterfv( GLenum target, GLint level,
1706 GLenum pname, GLfloat *params )
1707 {
1708 struct gl_texture_object *texObj;
1709 GLint iparam;
1710 GET_CURRENT_CONTEXT(ctx);
1711
1712 if (!valid_tex_level_parameteriv_target(ctx, target, false))
1713 return;
1714
1715 texObj = _mesa_get_current_tex_object(ctx, target);
1716 if (!texObj)
1717 return;
1718
1719 get_tex_level_parameteriv(ctx, texObj, target, level,
1720 pname, &iparam, false);
1721
1722 *params = (GLfloat) iparam;
1723 }
1724
1725 void GLAPIENTRY
1726 _mesa_GetTexLevelParameteriv( GLenum target, GLint level,
1727 GLenum pname, GLint *params )
1728 {
1729 struct gl_texture_object *texObj;
1730 GET_CURRENT_CONTEXT(ctx);
1731
1732 if (!valid_tex_level_parameteriv_target(ctx, target, false))
1733 return;
1734
1735 texObj = _mesa_get_current_tex_object(ctx, target);
1736 if (!texObj)
1737 return;
1738
1739 get_tex_level_parameteriv(ctx, texObj, target, level,
1740 pname, params, false);
1741 }
1742
1743 void GLAPIENTRY
1744 _mesa_GetTextureLevelParameterfv(GLuint texture, GLint level,
1745 GLenum pname, GLfloat *params)
1746 {
1747 struct gl_texture_object *texObj;
1748 GLint iparam;
1749 GET_CURRENT_CONTEXT(ctx);
1750
1751 texObj = _mesa_lookup_texture_err(ctx, texture,
1752 "glGetTextureLevelParameterfv");
1753 if (!texObj)
1754 return;
1755
1756 if (!valid_tex_level_parameteriv_target(ctx, texObj->Target, true))
1757 return;
1758
1759 get_tex_level_parameteriv(ctx, texObj, texObj->Target, level,
1760 pname, &iparam, true);
1761
1762 *params = (GLfloat) iparam;
1763 }
1764
1765 void GLAPIENTRY
1766 _mesa_GetTextureLevelParameteriv(GLuint texture, GLint level,
1767 GLenum pname, GLint *params)
1768 {
1769 struct gl_texture_object *texObj;
1770 GET_CURRENT_CONTEXT(ctx);
1771
1772 texObj = _mesa_lookup_texture_err(ctx, texture,
1773 "glGetTextureLevelParameteriv");
1774 if (!texObj)
1775 return;
1776
1777 if (!valid_tex_level_parameteriv_target(ctx, texObj->Target, true))
1778 return;
1779
1780 get_tex_level_parameteriv(ctx, texObj, texObj->Target, level,
1781 pname, params, true);
1782 }
1783
1784 /**
1785 * This isn't exposed to the rest of the driver because it is a part of the
1786 * OpenGL API that is rarely used.
1787 */
1788 static void
1789 get_tex_parameterfv(struct gl_context *ctx,
1790 struct gl_texture_object *obj,
1791 GLenum pname, GLfloat *params, bool dsa)
1792 {
1793 _mesa_lock_context_textures(ctx);
1794 switch (pname) {
1795 case GL_TEXTURE_MAG_FILTER:
1796 *params = ENUM_TO_FLOAT(obj->Sampler.MagFilter);
1797 break;
1798 case GL_TEXTURE_MIN_FILTER:
1799 *params = ENUM_TO_FLOAT(obj->Sampler.MinFilter);
1800 break;
1801 case GL_TEXTURE_WRAP_S:
1802 *params = ENUM_TO_FLOAT(obj->Sampler.WrapS);
1803 break;
1804 case GL_TEXTURE_WRAP_T:
1805 *params = ENUM_TO_FLOAT(obj->Sampler.WrapT);
1806 break;
1807 case GL_TEXTURE_WRAP_R:
1808 *params = ENUM_TO_FLOAT(obj->Sampler.WrapR);
1809 break;
1810 case GL_TEXTURE_BORDER_COLOR:
1811 if (ctx->API == API_OPENGLES ||
1812 !ctx->Extensions.ARB_texture_border_clamp)
1813 goto invalid_pname;
1814
1815 if (ctx->NewState & (_NEW_BUFFERS | _NEW_FRAG_CLAMP))
1816 _mesa_update_state_locked(ctx);
1817 if (_mesa_get_clamp_fragment_color(ctx, ctx->DrawBuffer)) {
1818 params[0] = CLAMP(obj->Sampler.BorderColor.f[0], 0.0F, 1.0F);
1819 params[1] = CLAMP(obj->Sampler.BorderColor.f[1], 0.0F, 1.0F);
1820 params[2] = CLAMP(obj->Sampler.BorderColor.f[2], 0.0F, 1.0F);
1821 params[3] = CLAMP(obj->Sampler.BorderColor.f[3], 0.0F, 1.0F);
1822 }
1823 else {
1824 params[0] = obj->Sampler.BorderColor.f[0];
1825 params[1] = obj->Sampler.BorderColor.f[1];
1826 params[2] = obj->Sampler.BorderColor.f[2];
1827 params[3] = obj->Sampler.BorderColor.f[3];
1828 }
1829 break;
1830 case GL_TEXTURE_RESIDENT:
1831 if (ctx->API != API_OPENGL_COMPAT)
1832 goto invalid_pname;
1833
1834 *params = 1.0F;
1835 break;
1836 case GL_TEXTURE_PRIORITY:
1837 if (ctx->API != API_OPENGL_COMPAT)
1838 goto invalid_pname;
1839
1840 *params = obj->Priority;
1841 break;
1842 case GL_TEXTURE_MIN_LOD:
1843 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
1844 goto invalid_pname;
1845
1846 *params = obj->Sampler.MinLod;
1847 break;
1848 case GL_TEXTURE_MAX_LOD:
1849 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
1850 goto invalid_pname;
1851
1852 *params = obj->Sampler.MaxLod;
1853 break;
1854 case GL_TEXTURE_BASE_LEVEL:
1855 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
1856 goto invalid_pname;
1857
1858 *params = (GLfloat) obj->BaseLevel;
1859 break;
1860 case GL_TEXTURE_MAX_LEVEL:
1861 *params = (GLfloat) obj->MaxLevel;
1862 break;
1863 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
1864 if (!ctx->Extensions.EXT_texture_filter_anisotropic)
1865 goto invalid_pname;
1866 *params = obj->Sampler.MaxAnisotropy;
1867 break;
1868 case GL_GENERATE_MIPMAP_SGIS:
1869 if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
1870 goto invalid_pname;
1871
1872 *params = (GLfloat) obj->GenerateMipmap;
1873 break;
1874 case GL_TEXTURE_COMPARE_MODE_ARB:
1875 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_shadow)
1876 && !_mesa_is_gles3(ctx))
1877 goto invalid_pname;
1878 *params = (GLfloat) obj->Sampler.CompareMode;
1879 break;
1880 case GL_TEXTURE_COMPARE_FUNC_ARB:
1881 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_shadow)
1882 && !_mesa_is_gles3(ctx))
1883 goto invalid_pname;
1884 *params = (GLfloat) obj->Sampler.CompareFunc;
1885 break;
1886 case GL_DEPTH_TEXTURE_MODE_ARB:
1887 /* GL_DEPTH_TEXTURE_MODE_ARB is removed in core-profile and it has
1888 * never existed in OpenGL ES.
1889 */
1890 if (ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.ARB_depth_texture)
1891 goto invalid_pname;
1892 *params = (GLfloat) obj->DepthMode;
1893 break;
1894 case GL_DEPTH_STENCIL_TEXTURE_MODE:
1895 if (!_mesa_has_ARB_stencil_texturing(ctx) && !_mesa_is_gles31(ctx))
1896 goto invalid_pname;
1897 *params = (GLfloat)
1898 (obj->StencilSampling ? GL_STENCIL_INDEX : GL_DEPTH_COMPONENT);
1899 break;
1900 case GL_TEXTURE_LOD_BIAS:
1901 if (_mesa_is_gles(ctx))
1902 goto invalid_pname;
1903
1904 *params = obj->Sampler.LodBias;
1905 break;
1906 case GL_TEXTURE_CROP_RECT_OES:
1907 if (ctx->API != API_OPENGLES || !ctx->Extensions.OES_draw_texture)
1908 goto invalid_pname;
1909
1910 params[0] = (GLfloat) obj->CropRect[0];
1911 params[1] = (GLfloat) obj->CropRect[1];
1912 params[2] = (GLfloat) obj->CropRect[2];
1913 params[3] = (GLfloat) obj->CropRect[3];
1914 break;
1915
1916 case GL_TEXTURE_SWIZZLE_R_EXT:
1917 case GL_TEXTURE_SWIZZLE_G_EXT:
1918 case GL_TEXTURE_SWIZZLE_B_EXT:
1919 case GL_TEXTURE_SWIZZLE_A_EXT:
1920 if ((!_mesa_is_desktop_gl(ctx)
1921 || !ctx->Extensions.EXT_texture_swizzle)
1922 && !_mesa_is_gles3(ctx))
1923 goto invalid_pname;
1924 *params = (GLfloat) obj->Swizzle[pname - GL_TEXTURE_SWIZZLE_R_EXT];
1925 break;
1926
1927 case GL_TEXTURE_SWIZZLE_RGBA_EXT:
1928 if ((!_mesa_is_desktop_gl(ctx)
1929 || !ctx->Extensions.EXT_texture_swizzle)
1930 && !_mesa_is_gles3(ctx)) {
1931 goto invalid_pname;
1932 }
1933 else {
1934 GLuint comp;
1935 for (comp = 0; comp < 4; comp++) {
1936 params[comp] = (GLfloat) obj->Swizzle[comp];
1937 }
1938 }
1939 break;
1940
1941 case GL_TEXTURE_CUBE_MAP_SEAMLESS:
1942 if (!_mesa_is_desktop_gl(ctx)
1943 || !ctx->Extensions.AMD_seamless_cubemap_per_texture)
1944 goto invalid_pname;
1945 *params = (GLfloat) obj->Sampler.CubeMapSeamless;
1946 break;
1947
1948 case GL_TEXTURE_IMMUTABLE_FORMAT:
1949 *params = (GLfloat) obj->Immutable;
1950 break;
1951
1952 case GL_TEXTURE_IMMUTABLE_LEVELS:
1953 if (_mesa_is_gles3(ctx) ||
1954 (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_view))
1955 *params = (GLfloat) obj->ImmutableLevels;
1956 else
1957 goto invalid_pname;
1958 break;
1959
1960 case GL_TEXTURE_VIEW_MIN_LEVEL:
1961 if (!ctx->Extensions.ARB_texture_view)
1962 goto invalid_pname;
1963 *params = (GLfloat) obj->MinLevel;
1964 break;
1965
1966 case GL_TEXTURE_VIEW_NUM_LEVELS:
1967 if (!ctx->Extensions.ARB_texture_view)
1968 goto invalid_pname;
1969 *params = (GLfloat) obj->NumLevels;
1970 break;
1971
1972 case GL_TEXTURE_VIEW_MIN_LAYER:
1973 if (!ctx->Extensions.ARB_texture_view)
1974 goto invalid_pname;
1975 *params = (GLfloat) obj->MinLayer;
1976 break;
1977
1978 case GL_TEXTURE_VIEW_NUM_LAYERS:
1979 if (!ctx->Extensions.ARB_texture_view)
1980 goto invalid_pname;
1981 *params = (GLfloat) obj->NumLayers;
1982 break;
1983
1984 case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
1985 if (!_mesa_is_gles(ctx) || !ctx->Extensions.OES_EGL_image_external)
1986 goto invalid_pname;
1987 *params = (GLfloat) obj->RequiredTextureImageUnits;
1988 break;
1989
1990 case GL_TEXTURE_SRGB_DECODE_EXT:
1991 if (!ctx->Extensions.EXT_texture_sRGB_decode)
1992 goto invalid_pname;
1993 *params = (GLfloat) obj->Sampler.sRGBDecode;
1994 break;
1995
1996 case GL_IMAGE_FORMAT_COMPATIBILITY_TYPE:
1997 if (!ctx->Extensions.ARB_shader_image_load_store)
1998 goto invalid_pname;
1999 *params = (GLfloat) obj->ImageFormatCompatibilityType;
2000 break;
2001
2002 case GL_TEXTURE_TARGET:
2003 if (ctx->API != API_OPENGL_CORE)
2004 goto invalid_pname;
2005 *params = ENUM_TO_FLOAT(obj->Target);
2006 break;
2007
2008 default:
2009 goto invalid_pname;
2010 }
2011
2012 /* no error if we get here */
2013 _mesa_unlock_context_textures(ctx);
2014 return;
2015
2016 invalid_pname:
2017 _mesa_unlock_context_textures(ctx);
2018 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTex%sParameterfv(pname=0x%x)",
2019 dsa ? "ture" : "", pname);
2020 }
2021
2022
2023 static void
2024 get_tex_parameteriv(struct gl_context *ctx,
2025 struct gl_texture_object *obj,
2026 GLenum pname, GLint *params, bool dsa)
2027 {
2028 _mesa_lock_texture(ctx, obj);
2029 switch (pname) {
2030 case GL_TEXTURE_MAG_FILTER:
2031 *params = (GLint) obj->Sampler.MagFilter;
2032 break;
2033 case GL_TEXTURE_MIN_FILTER:
2034 *params = (GLint) obj->Sampler.MinFilter;
2035 break;
2036 case GL_TEXTURE_WRAP_S:
2037 *params = (GLint) obj->Sampler.WrapS;
2038 break;
2039 case GL_TEXTURE_WRAP_T:
2040 *params = (GLint) obj->Sampler.WrapT;
2041 break;
2042 case GL_TEXTURE_WRAP_R:
2043 *params = (GLint) obj->Sampler.WrapR;
2044 break;
2045 case GL_TEXTURE_BORDER_COLOR:
2046 if (ctx->API == API_OPENGLES ||
2047 !ctx->Extensions.ARB_texture_border_clamp)
2048 goto invalid_pname;
2049
2050 {
2051 GLfloat b[4];
2052 b[0] = CLAMP(obj->Sampler.BorderColor.f[0], 0.0F, 1.0F);
2053 b[1] = CLAMP(obj->Sampler.BorderColor.f[1], 0.0F, 1.0F);
2054 b[2] = CLAMP(obj->Sampler.BorderColor.f[2], 0.0F, 1.0F);
2055 b[3] = CLAMP(obj->Sampler.BorderColor.f[3], 0.0F, 1.0F);
2056 params[0] = FLOAT_TO_INT(b[0]);
2057 params[1] = FLOAT_TO_INT(b[1]);
2058 params[2] = FLOAT_TO_INT(b[2]);
2059 params[3] = FLOAT_TO_INT(b[3]);
2060 }
2061 break;
2062 case GL_TEXTURE_RESIDENT:
2063 if (ctx->API != API_OPENGL_COMPAT)
2064 goto invalid_pname;
2065
2066 *params = 1;
2067 break;
2068 case GL_TEXTURE_PRIORITY:
2069 if (ctx->API != API_OPENGL_COMPAT)
2070 goto invalid_pname;
2071
2072 *params = FLOAT_TO_INT(obj->Priority);
2073 break;
2074 case GL_TEXTURE_MIN_LOD:
2075 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
2076 goto invalid_pname;
2077 /* GL spec 'Data Conversions' section specifies that floating-point
2078 * value in integer Get function is rounded to nearest integer
2079 */
2080 *params = IROUND(obj->Sampler.MinLod);
2081 break;
2082 case GL_TEXTURE_MAX_LOD:
2083 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
2084 goto invalid_pname;
2085 /* GL spec 'Data Conversions' section specifies that floating-point
2086 * value in integer Get function is rounded to nearest integer
2087 */
2088 *params = IROUND(obj->Sampler.MaxLod);
2089 break;
2090 case GL_TEXTURE_BASE_LEVEL:
2091 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
2092 goto invalid_pname;
2093
2094 *params = obj->BaseLevel;
2095 break;
2096 case GL_TEXTURE_MAX_LEVEL:
2097 *params = obj->MaxLevel;
2098 break;
2099 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
2100 if (!ctx->Extensions.EXT_texture_filter_anisotropic)
2101 goto invalid_pname;
2102 /* GL spec 'Data Conversions' section specifies that floating-point
2103 * value in integer Get function is rounded to nearest integer
2104 */
2105 *params = IROUND(obj->Sampler.MaxAnisotropy);
2106 break;
2107 case GL_GENERATE_MIPMAP_SGIS:
2108 if (ctx->API != API_OPENGL_COMPAT && ctx->API != API_OPENGLES)
2109 goto invalid_pname;
2110
2111 *params = (GLint) obj->GenerateMipmap;
2112 break;
2113 case GL_TEXTURE_COMPARE_MODE_ARB:
2114 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_shadow)
2115 && !_mesa_is_gles3(ctx))
2116 goto invalid_pname;
2117 *params = (GLint) obj->Sampler.CompareMode;
2118 break;
2119 case GL_TEXTURE_COMPARE_FUNC_ARB:
2120 if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_shadow)
2121 && !_mesa_is_gles3(ctx))
2122 goto invalid_pname;
2123 *params = (GLint) obj->Sampler.CompareFunc;
2124 break;
2125 case GL_DEPTH_TEXTURE_MODE_ARB:
2126 if (ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.ARB_depth_texture)
2127 goto invalid_pname;
2128 *params = (GLint) obj->DepthMode;
2129 break;
2130 case GL_DEPTH_STENCIL_TEXTURE_MODE:
2131 if (!_mesa_has_ARB_stencil_texturing(ctx) && !_mesa_is_gles31(ctx))
2132 goto invalid_pname;
2133 *params = (GLint)
2134 (obj->StencilSampling ? GL_STENCIL_INDEX : GL_DEPTH_COMPONENT);
2135 break;
2136 case GL_TEXTURE_LOD_BIAS:
2137 if (_mesa_is_gles(ctx))
2138 goto invalid_pname;
2139
2140 /* GL spec 'Data Conversions' section specifies that floating-point
2141 * value in integer Get function is rounded to nearest integer
2142 */
2143 *params = IROUND(obj->Sampler.LodBias);
2144 break;
2145 case GL_TEXTURE_CROP_RECT_OES:
2146 if (ctx->API != API_OPENGLES || !ctx->Extensions.OES_draw_texture)
2147 goto invalid_pname;
2148
2149 params[0] = obj->CropRect[0];
2150 params[1] = obj->CropRect[1];
2151 params[2] = obj->CropRect[2];
2152 params[3] = obj->CropRect[3];
2153 break;
2154 case GL_TEXTURE_SWIZZLE_R_EXT:
2155 case GL_TEXTURE_SWIZZLE_G_EXT:
2156 case GL_TEXTURE_SWIZZLE_B_EXT:
2157 case GL_TEXTURE_SWIZZLE_A_EXT:
2158 if ((!_mesa_is_desktop_gl(ctx)
2159 || !ctx->Extensions.EXT_texture_swizzle)
2160 && !_mesa_is_gles3(ctx))
2161 goto invalid_pname;
2162 *params = obj->Swizzle[pname - GL_TEXTURE_SWIZZLE_R_EXT];
2163 break;
2164
2165 case GL_TEXTURE_SWIZZLE_RGBA_EXT:
2166 if ((!_mesa_is_desktop_gl(ctx)
2167 || !ctx->Extensions.EXT_texture_swizzle)
2168 && !_mesa_is_gles3(ctx))
2169 goto invalid_pname;
2170 COPY_4V(params, obj->Swizzle);
2171 break;
2172
2173 case GL_TEXTURE_CUBE_MAP_SEAMLESS:
2174 if (!_mesa_is_desktop_gl(ctx)
2175 || !ctx->Extensions.AMD_seamless_cubemap_per_texture)
2176 goto invalid_pname;
2177 *params = (GLint) obj->Sampler.CubeMapSeamless;
2178 break;
2179
2180 case GL_TEXTURE_IMMUTABLE_FORMAT:
2181 *params = (GLint) obj->Immutable;
2182 break;
2183
2184 case GL_TEXTURE_IMMUTABLE_LEVELS:
2185 if (_mesa_is_gles3(ctx) ||
2186 (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_view))
2187 *params = obj->ImmutableLevels;
2188 else
2189 goto invalid_pname;
2190 break;
2191
2192 case GL_TEXTURE_VIEW_MIN_LEVEL:
2193 if (!ctx->Extensions.ARB_texture_view)
2194 goto invalid_pname;
2195 *params = (GLint) obj->MinLevel;
2196 break;
2197
2198 case GL_TEXTURE_VIEW_NUM_LEVELS:
2199 if (!ctx->Extensions.ARB_texture_view)
2200 goto invalid_pname;
2201 *params = (GLint) obj->NumLevels;
2202 break;
2203
2204 case GL_TEXTURE_VIEW_MIN_LAYER:
2205 if (!ctx->Extensions.ARB_texture_view)
2206 goto invalid_pname;
2207 *params = (GLint) obj->MinLayer;
2208 break;
2209
2210 case GL_TEXTURE_VIEW_NUM_LAYERS:
2211 if (!ctx->Extensions.ARB_texture_view)
2212 goto invalid_pname;
2213 *params = (GLint) obj->NumLayers;
2214 break;
2215
2216 case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
2217 if (!_mesa_is_gles(ctx) || !ctx->Extensions.OES_EGL_image_external)
2218 goto invalid_pname;
2219 *params = obj->RequiredTextureImageUnits;
2220 break;
2221
2222 case GL_TEXTURE_SRGB_DECODE_EXT:
2223 if (!ctx->Extensions.EXT_texture_sRGB_decode)
2224 goto invalid_pname;
2225 *params = obj->Sampler.sRGBDecode;
2226 break;
2227
2228 case GL_IMAGE_FORMAT_COMPATIBILITY_TYPE:
2229 if (!ctx->Extensions.ARB_shader_image_load_store)
2230 goto invalid_pname;
2231 *params = obj->ImageFormatCompatibilityType;
2232 break;
2233
2234 case GL_TEXTURE_TARGET:
2235 if (ctx->API != API_OPENGL_CORE)
2236 goto invalid_pname;
2237 *params = (GLint) obj->Target;
2238 break;
2239
2240 default:
2241 goto invalid_pname;
2242 }
2243
2244 /* no error if we get here */
2245 _mesa_unlock_texture(ctx, obj);
2246 return;
2247
2248 invalid_pname:
2249 _mesa_unlock_texture(ctx, obj);
2250 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTex%sParameteriv(pname=0x%x)",
2251 dsa ? "ture" : "", pname);
2252 }
2253
2254 static void
2255 get_tex_parameterIiv(struct gl_context *ctx,
2256 struct gl_texture_object *obj,
2257 GLenum pname, GLint *params, bool dsa)
2258 {
2259 switch (pname) {
2260 case GL_TEXTURE_BORDER_COLOR:
2261 COPY_4V(params, obj->Sampler.BorderColor.i);
2262 break;
2263 default:
2264 get_tex_parameteriv(ctx, obj, pname, params, dsa);
2265 }
2266 }
2267
2268 static void
2269 get_tex_parameterIuiv(struct gl_context *ctx,
2270 struct gl_texture_object *obj,
2271 GLenum pname, GLuint *params, bool dsa)
2272 {
2273 switch (pname) {
2274 case GL_TEXTURE_BORDER_COLOR:
2275 COPY_4V(params, obj->Sampler.BorderColor.i);
2276 break;
2277 default:
2278 {
2279 GLint ip[4];
2280 get_tex_parameteriv(ctx, obj, pname, ip, dsa);
2281 params[0] = ip[0];
2282 if (pname == GL_TEXTURE_SWIZZLE_RGBA_EXT ||
2283 pname == GL_TEXTURE_CROP_RECT_OES) {
2284 params[1] = ip[1];
2285 params[2] = ip[2];
2286 params[3] = ip[3];
2287 }
2288 }
2289 }
2290 }
2291
2292 void GLAPIENTRY
2293 _mesa_GetTexParameterfv(GLenum target, GLenum pname, GLfloat *params)
2294 {
2295 struct gl_texture_object *obj;
2296 GET_CURRENT_CONTEXT(ctx);
2297
2298 obj = get_texobj_by_target(ctx, target, GL_TRUE);
2299 if (!obj)
2300 return;
2301
2302 get_tex_parameterfv(ctx, obj, pname, params, false);
2303 }
2304
2305 void GLAPIENTRY
2306 _mesa_GetTexParameteriv(GLenum target, GLenum pname, GLint *params)
2307 {
2308 struct gl_texture_object *obj;
2309 GET_CURRENT_CONTEXT(ctx);
2310
2311 obj = get_texobj_by_target(ctx, target, GL_TRUE);
2312 if (!obj)
2313 return;
2314
2315 get_tex_parameteriv(ctx, obj, pname, params, false);
2316 }
2317
2318 /** New in GL 3.0 */
2319 void GLAPIENTRY
2320 _mesa_GetTexParameterIiv(GLenum target, GLenum pname, GLint *params)
2321 {
2322 struct gl_texture_object *texObj;
2323 GET_CURRENT_CONTEXT(ctx);
2324
2325 texObj = get_texobj_by_target(ctx, target, GL_TRUE);
2326 if (!texObj)
2327 return;
2328
2329 get_tex_parameterIiv(ctx, texObj, pname, params, false);
2330 }
2331
2332
2333 /** New in GL 3.0 */
2334 void GLAPIENTRY
2335 _mesa_GetTexParameterIuiv(GLenum target, GLenum pname, GLuint *params)
2336 {
2337 struct gl_texture_object *texObj;
2338 GET_CURRENT_CONTEXT(ctx);
2339
2340 texObj = get_texobj_by_target(ctx, target, GL_TRUE);
2341 if (!texObj)
2342 return;
2343
2344 get_tex_parameterIuiv(ctx, texObj, pname, params, false);
2345 }
2346
2347
2348 void GLAPIENTRY
2349 _mesa_GetTextureParameterfv(GLuint texture, GLenum pname, GLfloat *params)
2350 {
2351 struct gl_texture_object *obj;
2352 GET_CURRENT_CONTEXT(ctx);
2353
2354 obj = get_texobj_by_name(ctx, texture, "glGetTextureParameterfv");
2355 if (!obj)
2356 return;
2357
2358 get_tex_parameterfv(ctx, obj, pname, params, true);
2359 }
2360
2361 void GLAPIENTRY
2362 _mesa_GetTextureParameteriv(GLuint texture, GLenum pname, GLint *params)
2363 {
2364 struct gl_texture_object *obj;
2365 GET_CURRENT_CONTEXT(ctx);
2366
2367 obj = get_texobj_by_name(ctx, texture, "glGetTextureParameteriv");
2368 if (!obj)
2369 return;
2370
2371 get_tex_parameteriv(ctx, obj, pname, params, true);
2372 }
2373
2374 void GLAPIENTRY
2375 _mesa_GetTextureParameterIiv(GLuint texture, GLenum pname, GLint *params)
2376 {
2377 struct gl_texture_object *texObj;
2378 GET_CURRENT_CONTEXT(ctx);
2379
2380 texObj = get_texobj_by_name(ctx, texture, "glGetTextureParameterIiv");
2381 if (!texObj)
2382 return;
2383
2384 get_tex_parameterIiv(ctx, texObj, pname, params, true);
2385 }
2386
2387
2388 void GLAPIENTRY
2389 _mesa_GetTextureParameterIuiv(GLuint texture, GLenum pname, GLuint *params)
2390 {
2391 struct gl_texture_object *texObj;
2392 GET_CURRENT_CONTEXT(ctx);
2393
2394 texObj = get_texobj_by_name(ctx, texture, "glGetTextureParameterIuiv");
2395 if (!texObj)
2396 return;
2397
2398 get_tex_parameterIuiv(ctx, texObj, pname, params, true);
2399 }