1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "mtypes.h"
25 #include "context.h"
26 #include "glformats.h"
27 #include "macros.h"
28 #include "enums.h"
29 #include "fbobject.h"
30 #include "formatquery.h"
31 #include "teximage.h"
32 #include "texparam.h"
33 #include "texobj.h"
34 #include "get.h"
35 #include "genmipmap.h"
36 #include "shaderimage.h"
37 #include "texcompress.h"
38 #include "textureview.h"
39 
40 static bool
_is_renderable(struct gl_context * ctx,GLenum internalformat)41 _is_renderable(struct gl_context *ctx, GLenum internalformat)
42 {
43    /*  Section 4.4.4 on page 212 of the  GLES 3.0.4 spec says:
44     *
45     *     "An internal format is color-renderable if it is one of the
46     *     formats from table 3.13 noted as color-renderable or if it
47     *     is unsized format RGBA or RGB."
48     *
49     * Therefore, we must accept GL_RGB and GL_RGBA here.
50     */
51    if (internalformat != GL_RGB && internalformat != GL_RGBA &&
52        _mesa_base_fbo_format(ctx, internalformat) == 0)
53       return false;
54 
55    return true;
56 }
57 
58 /* Handles the cases where either ARB_internalformat_query or
59  * ARB_internalformat_query2 have to return an error.
60  */
61 static bool
_legal_parameters(struct gl_context * ctx,GLenum target,GLenum internalformat,GLenum pname,GLsizei bufSize,GLint * params)62 _legal_parameters(struct gl_context *ctx, GLenum target, GLenum internalformat,
63                   GLenum pname, GLsizei bufSize, GLint *params)
64 
65 {
66    bool query2 = _mesa_has_ARB_internalformat_query2(ctx);
67 
68    /* The ARB_internalformat_query2 spec says:
69     *
70     *    "The INVALID_ENUM error is generated if the <target> parameter to
71     *    GetInternalformati*v is not one of the targets listed in Table 6.xx.
72     */
73    switch(target){
74    case GL_TEXTURE_1D:
75    case GL_TEXTURE_1D_ARRAY:
76    case GL_TEXTURE_2D:
77    case GL_TEXTURE_2D_ARRAY:
78    case GL_TEXTURE_3D:
79    case GL_TEXTURE_CUBE_MAP:
80    case GL_TEXTURE_CUBE_MAP_ARRAY:
81    case GL_TEXTURE_RECTANGLE:
82    case GL_TEXTURE_BUFFER:
83       if (!query2) {
84          /* The ARB_internalformat_query spec says:
85           *
86           *     "If the <target> parameter to GetInternalformativ is not one of
87           *      TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY
88           *      or RENDERBUFFER then an INVALID_ENUM error is generated.
89           */
90          _mesa_error(ctx, GL_INVALID_ENUM,
91                      "glGetInternalformativ(target=%s)",
92                      _mesa_enum_to_string(target));
93 
94          return false;
95       }
96       break;
97 
98    case GL_RENDERBUFFER:
99       break;
100 
101    case GL_TEXTURE_2D_MULTISAMPLE:
102    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
103       /* The non-existence of ARB_texture_multisample is treated in
104        * ARB_internalformat_query implementation like an error.
105        */
106       if (!query2 &&
107           !(_mesa_has_ARB_texture_multisample(ctx) || _mesa_is_gles31(ctx))) {
108          _mesa_error(ctx, GL_INVALID_ENUM,
109                      "glGetInternalformativ(target=%s)",
110                      _mesa_enum_to_string(target));
111 
112          return false;
113       }
114       break;
115 
116    default:
117       _mesa_error(ctx, GL_INVALID_ENUM,
118                   "glGetInternalformativ(target=%s)",
119                   _mesa_enum_to_string(target));
120       return false;
121    }
122 
123 
124    /* The ARB_internalformat_query2 spec says:
125     *
126     *     "The INVALID_ENUM error is generated if the <pname> parameter is
127     *     not one of the listed possibilities.
128     */
129    switch(pname){
130    case GL_SAMPLES:
131    case GL_NUM_SAMPLE_COUNTS:
132       break;
133 
134    case GL_SRGB_DECODE_ARB:
135       /* The ARB_internalformat_query2 spec says:
136        *
137        *     "If ARB_texture_sRGB_decode or EXT_texture_sRGB_decode or
138        *     equivalent functionality is not supported, queries for the
139        *     SRGB_DECODE_ARB <pname> set the INVALID_ENUM error.
140        */
141       if (!_mesa_has_EXT_texture_sRGB_decode(ctx)) {
142          _mesa_error(ctx, GL_INVALID_ENUM,
143                      "glGetInternalformativ(pname=%s)",
144                      _mesa_enum_to_string(pname));
145          return false;
146       }
147       /* fallthrough */
148    case GL_INTERNALFORMAT_SUPPORTED:
149    case GL_INTERNALFORMAT_PREFERRED:
150    case GL_INTERNALFORMAT_RED_SIZE:
151    case GL_INTERNALFORMAT_GREEN_SIZE:
152    case GL_INTERNALFORMAT_BLUE_SIZE:
153    case GL_INTERNALFORMAT_ALPHA_SIZE:
154    case GL_INTERNALFORMAT_DEPTH_SIZE:
155    case GL_INTERNALFORMAT_STENCIL_SIZE:
156    case GL_INTERNALFORMAT_SHARED_SIZE:
157    case GL_INTERNALFORMAT_RED_TYPE:
158    case GL_INTERNALFORMAT_GREEN_TYPE:
159    case GL_INTERNALFORMAT_BLUE_TYPE:
160    case GL_INTERNALFORMAT_ALPHA_TYPE:
161    case GL_INTERNALFORMAT_DEPTH_TYPE:
162    case GL_INTERNALFORMAT_STENCIL_TYPE:
163    case GL_MAX_WIDTH:
164    case GL_MAX_HEIGHT:
165    case GL_MAX_DEPTH:
166    case GL_MAX_LAYERS:
167    case GL_MAX_COMBINED_DIMENSIONS:
168    case GL_COLOR_COMPONENTS:
169    case GL_DEPTH_COMPONENTS:
170    case GL_STENCIL_COMPONENTS:
171    case GL_COLOR_RENDERABLE:
172    case GL_DEPTH_RENDERABLE:
173    case GL_STENCIL_RENDERABLE:
174    case GL_FRAMEBUFFER_RENDERABLE:
175    case GL_FRAMEBUFFER_RENDERABLE_LAYERED:
176    case GL_FRAMEBUFFER_BLEND:
177    case GL_READ_PIXELS:
178    case GL_READ_PIXELS_FORMAT:
179    case GL_READ_PIXELS_TYPE:
180    case GL_TEXTURE_IMAGE_FORMAT:
181    case GL_TEXTURE_IMAGE_TYPE:
182    case GL_GET_TEXTURE_IMAGE_FORMAT:
183    case GL_GET_TEXTURE_IMAGE_TYPE:
184    case GL_MIPMAP:
185    case GL_MANUAL_GENERATE_MIPMAP:
186    case GL_AUTO_GENERATE_MIPMAP:
187    case GL_COLOR_ENCODING:
188    case GL_SRGB_READ:
189    case GL_SRGB_WRITE:
190    case GL_FILTER:
191    case GL_VERTEX_TEXTURE:
192    case GL_TESS_CONTROL_TEXTURE:
193    case GL_TESS_EVALUATION_TEXTURE:
194    case GL_GEOMETRY_TEXTURE:
195    case GL_FRAGMENT_TEXTURE:
196    case GL_COMPUTE_TEXTURE:
197    case GL_TEXTURE_SHADOW:
198    case GL_TEXTURE_GATHER:
199    case GL_TEXTURE_GATHER_SHADOW:
200    case GL_SHADER_IMAGE_LOAD:
201    case GL_SHADER_IMAGE_STORE:
202    case GL_SHADER_IMAGE_ATOMIC:
203    case GL_IMAGE_TEXEL_SIZE:
204    case GL_IMAGE_COMPATIBILITY_CLASS:
205    case GL_IMAGE_PIXEL_FORMAT:
206    case GL_IMAGE_PIXEL_TYPE:
207    case GL_IMAGE_FORMAT_COMPATIBILITY_TYPE:
208    case GL_SIMULTANEOUS_TEXTURE_AND_DEPTH_TEST:
209    case GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_TEST:
210    case GL_SIMULTANEOUS_TEXTURE_AND_DEPTH_WRITE:
211    case GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_WRITE:
212    case GL_TEXTURE_COMPRESSED:
213    case GL_TEXTURE_COMPRESSED_BLOCK_WIDTH:
214    case GL_TEXTURE_COMPRESSED_BLOCK_HEIGHT:
215    case GL_TEXTURE_COMPRESSED_BLOCK_SIZE:
216    case GL_CLEAR_BUFFER:
217    case GL_TEXTURE_VIEW:
218    case GL_VIEW_COMPATIBILITY_CLASS:
219    case GL_NUM_TILING_TYPES_EXT:
220    case GL_TILING_TYPES_EXT:
221       /* The ARB_internalformat_query spec says:
222        *
223        *     "If the <pname> parameter to GetInternalformativ is not SAMPLES
224        *     or NUM_SAMPLE_COUNTS, then an INVALID_ENUM error is generated."
225        */
226       if (!query2) {
227          _mesa_error(ctx, GL_INVALID_ENUM,
228                      "glGetInternalformativ(pname=%s)",
229                      _mesa_enum_to_string(pname));
230 
231          return false;
232       }
233       break;
234 
235    default:
236       _mesa_error(ctx, GL_INVALID_ENUM,
237                   "glGetInternalformativ(pname=%s)",
238                   _mesa_enum_to_string(pname));
239       return false;
240    }
241 
242    /* The ARB_internalformat_query spec says:
243     *
244     *     "If the <bufSize> parameter to GetInternalformativ is negative, then
245     *     an INVALID_VALUE error is generated."
246     *
247     * Nothing is said in ARB_internalformat_query2 but we assume the same.
248     */
249    if (bufSize < 0) {
250       _mesa_error(ctx, GL_INVALID_VALUE,
251                   "glGetInternalformativ(target=%s)",
252                   _mesa_enum_to_string(target));
253       return false;
254    }
255 
256    /* The ARB_internalformat_query spec says:
257     *
258     *     "If the <internalformat> parameter to GetInternalformativ is not
259     *     color-, depth- or stencil-renderable, then an INVALID_ENUM error is
260     *     generated."
261     */
262    if (!query2 && !_is_renderable(ctx, internalformat)) {
263       _mesa_error(ctx, GL_INVALID_ENUM,
264                   "glGetInternalformativ(internalformat=%s)",
265                   _mesa_enum_to_string(internalformat));
266       return false;
267    }
268 
269    return true;
270 }
271 
272 /* Sets the appropriate "unsupported" response as defined by the
273  * ARB_internalformat_query2 spec for each each <pname>.
274  */
275 static void
_set_default_response(GLenum pname,GLint buffer[16])276 _set_default_response(GLenum pname, GLint buffer[16])
277 {
278    /* The ARB_internalformat_query2 defines which is the reponse best
279     * representing "not supported" or "not applicable" for each <pname>.
280     *
281     *     " In general:
282     *          - size- or count-based queries will return zero,
283     *          - support-, format- or type-based queries will return NONE,
284     *          - boolean-based queries will return FALSE, and
285     *          - list-based queries return no entries."
286     */
287    switch(pname) {
288    case GL_SAMPLES:
289    case GL_TILING_TYPES_EXT:
290       break;
291 
292    case GL_MAX_COMBINED_DIMENSIONS:
293       /* This value can be a 64-bit value. As the default is the 32-bit query,
294        * we pack 2 32-bit integers. So we need to clean both */
295       buffer[0] = 0;
296       buffer[1] = 0;
297       break;
298 
299    case GL_NUM_SAMPLE_COUNTS:
300    case GL_INTERNALFORMAT_RED_SIZE:
301    case GL_INTERNALFORMAT_GREEN_SIZE:
302    case GL_INTERNALFORMAT_BLUE_SIZE:
303    case GL_INTERNALFORMAT_ALPHA_SIZE:
304    case GL_INTERNALFORMAT_DEPTH_SIZE:
305    case GL_INTERNALFORMAT_STENCIL_SIZE:
306    case GL_INTERNALFORMAT_SHARED_SIZE:
307    case GL_MAX_WIDTH:
308    case GL_MAX_HEIGHT:
309    case GL_MAX_DEPTH:
310    case GL_MAX_LAYERS:
311    case GL_IMAGE_TEXEL_SIZE:
312    case GL_TEXTURE_COMPRESSED_BLOCK_WIDTH:
313    case GL_TEXTURE_COMPRESSED_BLOCK_HEIGHT:
314    case GL_TEXTURE_COMPRESSED_BLOCK_SIZE:
315    case GL_NUM_TILING_TYPES_EXT:
316       buffer[0] = 0;
317       break;
318 
319    case GL_INTERNALFORMAT_PREFERRED:
320    case GL_INTERNALFORMAT_RED_TYPE:
321    case GL_INTERNALFORMAT_GREEN_TYPE:
322    case GL_INTERNALFORMAT_BLUE_TYPE:
323    case GL_INTERNALFORMAT_ALPHA_TYPE:
324    case GL_INTERNALFORMAT_DEPTH_TYPE:
325    case GL_INTERNALFORMAT_STENCIL_TYPE:
326    case GL_FRAMEBUFFER_RENDERABLE:
327    case GL_FRAMEBUFFER_RENDERABLE_LAYERED:
328    case GL_FRAMEBUFFER_BLEND:
329    case GL_READ_PIXELS:
330    case GL_READ_PIXELS_FORMAT:
331    case GL_READ_PIXELS_TYPE:
332    case GL_TEXTURE_IMAGE_FORMAT:
333    case GL_TEXTURE_IMAGE_TYPE:
334    case GL_GET_TEXTURE_IMAGE_FORMAT:
335    case GL_GET_TEXTURE_IMAGE_TYPE:
336    case GL_MANUAL_GENERATE_MIPMAP:
337    case GL_AUTO_GENERATE_MIPMAP:
338    case GL_COLOR_ENCODING:
339    case GL_SRGB_READ:
340    case GL_SRGB_WRITE:
341    case GL_SRGB_DECODE_ARB:
342    case GL_FILTER:
343    case GL_VERTEX_TEXTURE:
344    case GL_TESS_CONTROL_TEXTURE:
345    case GL_TESS_EVALUATION_TEXTURE:
346    case GL_GEOMETRY_TEXTURE:
347    case GL_FRAGMENT_TEXTURE:
348    case GL_COMPUTE_TEXTURE:
349    case GL_TEXTURE_SHADOW:
350    case GL_TEXTURE_GATHER:
351    case GL_TEXTURE_GATHER_SHADOW:
352    case GL_SHADER_IMAGE_LOAD:
353    case GL_SHADER_IMAGE_STORE:
354    case GL_SHADER_IMAGE_ATOMIC:
355    case GL_IMAGE_COMPATIBILITY_CLASS:
356    case GL_IMAGE_PIXEL_FORMAT:
357    case GL_IMAGE_PIXEL_TYPE:
358    case GL_IMAGE_FORMAT_COMPATIBILITY_TYPE:
359    case GL_SIMULTANEOUS_TEXTURE_AND_DEPTH_TEST:
360    case GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_TEST:
361    case GL_SIMULTANEOUS_TEXTURE_AND_DEPTH_WRITE:
362    case GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_WRITE:
363    case GL_CLEAR_BUFFER:
364    case GL_TEXTURE_VIEW:
365    case GL_VIEW_COMPATIBILITY_CLASS:
366       buffer[0] = GL_NONE;
367       break;
368 
369    case GL_INTERNALFORMAT_SUPPORTED:
370    case GL_COLOR_COMPONENTS:
371    case GL_DEPTH_COMPONENTS:
372    case GL_STENCIL_COMPONENTS:
373    case GL_COLOR_RENDERABLE:
374    case GL_DEPTH_RENDERABLE:
375    case GL_STENCIL_RENDERABLE:
376    case GL_MIPMAP:
377    case GL_TEXTURE_COMPRESSED:
378       buffer[0] = GL_FALSE;
379       break;
380 
381    default:
382       unreachable("invalid 'pname'");
383    }
384 }
385 
386 static bool
_is_target_supported(struct gl_context * ctx,GLenum target)387 _is_target_supported(struct gl_context *ctx, GLenum target)
388 {
389    /* The ARB_internalformat_query2 spec says:
390     *
391     *     "if a particular type of <target> is not supported by the
392     *     implementation the "unsupported" answer should be given.
393     *     This is not an error."
394     *
395     * For OpenGL ES, queries can only be used with GL_RENDERBUFFER or MS.
396     */
397    switch(target){
398    case GL_TEXTURE_1D:
399    case GL_TEXTURE_2D:
400    case GL_TEXTURE_3D:
401       if (!_mesa_is_desktop_gl(ctx))
402          return false;
403       break;
404 
405    case GL_TEXTURE_1D_ARRAY:
406       if (!_mesa_has_EXT_texture_array(ctx))
407          return false;
408       break;
409 
410    case GL_TEXTURE_2D_ARRAY:
411       if (!_mesa_has_EXT_texture_array(ctx))
412          return false;
413       break;
414 
415    case GL_TEXTURE_CUBE_MAP:
416       if (ctx->API != API_OPENGL_CORE && !_mesa_has_ARB_texture_cube_map(ctx))
417          return false;
418       break;
419 
420    case GL_TEXTURE_CUBE_MAP_ARRAY:
421       if (!_mesa_has_ARB_texture_cube_map_array(ctx))
422          return false;
423       break;
424 
425    case GL_TEXTURE_RECTANGLE:
426       if (!_mesa_has_ARB_texture_rectangle(ctx))
427           return false;
428       break;
429 
430    case GL_TEXTURE_BUFFER:
431       if (!_mesa_has_ARB_texture_buffer_object(ctx))
432          return false;
433       break;
434 
435    case GL_RENDERBUFFER:
436       if (!(_mesa_has_ARB_framebuffer_object(ctx) ||
437             _mesa_is_gles3(ctx)))
438          return false;
439       break;
440 
441    case GL_TEXTURE_2D_MULTISAMPLE:
442    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
443       if (!(_mesa_has_ARB_texture_multisample(ctx) ||
444             _mesa_is_gles31(ctx)))
445          return false;
446       break;
447 
448    default:
449       unreachable("invalid target");
450    }
451 
452    return true;
453 }
454 
455 static bool
_is_resource_supported(struct gl_context * ctx,GLenum target,GLenum internalformat,GLenum pname)456 _is_resource_supported(struct gl_context *ctx, GLenum target,
457                        GLenum internalformat, GLenum pname)
458 {
459    /* From the ARB_internalformat_query2 spec:
460     *
461     * In the following descriptions, the term /resource/ is used to generically
462     * refer to an object of the appropriate type that has been created with
463     * <internalformat> and <target>.  If the particular <target> and
464     * <internalformat> combination do not make sense, ... the "unsupported"
465     * answer should be given. This is not an error.
466     */
467 
468    /* In the ARB_internalformat_query2 spec wording, some <pnames> do not care
469     * about the /resource/ being supported or not, we return 'true' for those.
470     */
471    switch (pname) {
472    case GL_INTERNALFORMAT_SUPPORTED:
473    case GL_INTERNALFORMAT_PREFERRED:
474    case GL_COLOR_COMPONENTS:
475    case GL_DEPTH_COMPONENTS:
476    case GL_STENCIL_COMPONENTS:
477    case GL_COLOR_RENDERABLE:
478    case GL_DEPTH_RENDERABLE:
479    case GL_STENCIL_RENDERABLE:
480       return true;
481    default:
482       break;
483    }
484 
485    switch(target){
486    case GL_TEXTURE_1D:
487    case GL_TEXTURE_1D_ARRAY:
488    case GL_TEXTURE_2D:
489    case GL_TEXTURE_2D_ARRAY:
490    case GL_TEXTURE_3D:
491    case GL_TEXTURE_CUBE_MAP:
492    case GL_TEXTURE_CUBE_MAP_ARRAY:
493    case GL_TEXTURE_RECTANGLE:
494       /* Based on what Mesa does for glTexImage1D/2D/3D and
495        * glCompressedTexImage1D/2D/3D functions.
496        */
497       if (_mesa_base_tex_format(ctx, internalformat) < 0)
498          return false;
499 
500       /* additional checks for depth textures */
501       if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalformat))
502          return false;
503 
504       /* additional checks for compressed textures */
505       if (_mesa_is_compressed_format(ctx, internalformat) &&
506           (!_mesa_target_can_be_compressed(ctx, target, internalformat, NULL) ||
507            _mesa_format_no_online_compression(ctx, internalformat)))
508          return false;
509 
510       break;
511    case GL_TEXTURE_2D_MULTISAMPLE:
512    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
513       /* Based on what Mesa does for glTexImage2D/3DMultisample,
514        * glTexStorage2D/3DMultisample and
515        * glTextureStorage2D/3DMultisample functions.
516        */
517       if (!_mesa_is_renderable_texture_format(ctx, internalformat))
518          return false;
519 
520       break;
521    case GL_TEXTURE_BUFFER:
522       /* Based on what Mesa does for the glTexBuffer function. */
523       if (_mesa_validate_texbuffer_format(ctx, internalformat) ==
524           MESA_FORMAT_NONE)
525          return false;
526 
527       break;
528    case GL_RENDERBUFFER:
529       /* Based on what Mesa does for glRenderbufferStorage(Multisample) and
530        * glNamedRenderbufferStorage functions.
531        */
532       if (!_mesa_base_fbo_format(ctx, internalformat))
533          return false;
534 
535       break;
536    default:
537       unreachable("bad target");
538    }
539 
540    return true;
541 }
542 
543 static bool
_is_internalformat_supported(struct gl_context * ctx,GLenum target,GLenum internalformat)544 _is_internalformat_supported(struct gl_context *ctx, GLenum target,
545                              GLenum internalformat)
546 {
547    /* From the ARB_internalformat_query2 specification:
548     *
549     *     "- INTERNALFORMAT_SUPPORTED: If <internalformat> is an internal format
550     *     that is supported by the implementation in at least some subset of
551     *     possible operations, TRUE is written to <params>.  If <internalformat>
552     *     if not a valid token for any internal format usage, FALSE is returned.
553     *
554     *     <internalformats> that must be supported (in GL 4.2 or later) include
555     *      the following:
556     *         - "sized internal formats" from Table 3.12, 3.13, and 3.15,
557     *         - any specific "compressed internal format" from Table 3.14,
558     *         - any "image unit format" from Table 3.21.
559     *         - any generic "compressed internal format" from Table 3.14, if the
560     *         implementation accepts it for any texture specification commands, and
561     *         - unsized or base internal format, if the implementation accepts
562     *         it for texture or image specification.
563     */
564    GLint buffer[1];
565 
566    /* At this point an internalformat is valid if it is valid as a texture or
567     * as a renderbuffer format. The checks are different because those methods
568     * return different values when passing non supported internalformats */
569    if (_mesa_base_tex_format(ctx, internalformat) < 0 &&
570        _mesa_base_fbo_format(ctx, internalformat) == 0)
571       return false;
572 
573    /* Let the driver have the final word */
574    ctx->Driver.QueryInternalFormat(ctx, target, internalformat,
575                                    GL_INTERNALFORMAT_SUPPORTED, buffer);
576 
577    return (buffer[0] == GL_TRUE);
578 }
579 
580 static bool
_legal_target_for_framebuffer_texture_layer(struct gl_context * ctx,GLenum target)581 _legal_target_for_framebuffer_texture_layer(struct gl_context *ctx,
582                                             GLenum target)
583 {
584    switch (target) {
585    case GL_TEXTURE_3D:
586    case GL_TEXTURE_1D_ARRAY:
587    case GL_TEXTURE_2D_ARRAY:
588    case GL_TEXTURE_CUBE_MAP_ARRAY:
589    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
590    case GL_TEXTURE_CUBE_MAP:
591       return true;
592    default:
593       return false;
594    }
595 }
596 
597 static GLenum
_mesa_generic_type_for_internal_format(GLenum internalFormat)598 _mesa_generic_type_for_internal_format(GLenum internalFormat)
599 {
600    if (_mesa_is_enum_format_unsigned_int(internalFormat))
601       return GL_UNSIGNED_BYTE;
602    else if (_mesa_is_enum_format_signed_int(internalFormat))
603       return GL_BYTE;
604    else
605       return GL_FLOAT;
606 }
607 
608 /* default implementation of QueryInternalFormat driverfunc, for
609  * drivers not implementing ARB_internalformat_query2.
610  */
611 void
_mesa_query_internal_format_default(struct gl_context * ctx,GLenum target,GLenum internalFormat,GLenum pname,GLint * params)612 _mesa_query_internal_format_default(struct gl_context *ctx, GLenum target,
613                                     GLenum internalFormat, GLenum pname,
614                                     GLint *params)
615 {
616    (void) target;
617 
618    switch (pname) {
619    case GL_SAMPLES:
620    case GL_NUM_SAMPLE_COUNTS:
621       params[0] = 1;
622       break;
623 
624    case GL_INTERNALFORMAT_SUPPORTED:
625       params[0] = GL_TRUE;
626       break;
627 
628    case GL_INTERNALFORMAT_PREFERRED:
629       params[0] = internalFormat;
630       break;
631 
632    case GL_READ_PIXELS_FORMAT: {
633       GLenum base_format = _mesa_base_tex_format(ctx, internalFormat);
634       switch (base_format) {
635       case GL_STENCIL_INDEX:
636       case GL_DEPTH_COMPONENT:
637       case GL_DEPTH_STENCIL:
638       case GL_RED:
639       case GL_RGB:
640       case GL_BGR:
641       case GL_RGBA:
642       case GL_BGRA:
643          params[0] = base_format;
644          break;
645       default:
646          params[0] = GL_NONE;
647          break;
648       }
649       break;
650    }
651 
652    case GL_READ_PIXELS_TYPE:
653    case GL_TEXTURE_IMAGE_TYPE:
654    case GL_GET_TEXTURE_IMAGE_TYPE: {
655       GLenum base_format = _mesa_base_tex_format(ctx, internalFormat);
656       if (base_format > 0)
657          params[0] = _mesa_generic_type_for_internal_format(internalFormat);
658       else
659          params[0] = GL_NONE;
660       break;
661    }
662 
663    case GL_TEXTURE_IMAGE_FORMAT:
664    case GL_GET_TEXTURE_IMAGE_FORMAT: {
665       GLenum format = GL_NONE;
666       GLenum base_format = _mesa_base_tex_format(ctx, internalFormat);
667       if (base_format > 0) {
668          if (_mesa_is_enum_format_integer(internalFormat))
669            format = _mesa_base_format_to_integer_format(base_format);
670          else
671            format = base_format;
672       }
673 
674       params[0] = format;
675       break;
676    }
677 
678    case GL_MANUAL_GENERATE_MIPMAP:
679    case GL_AUTO_GENERATE_MIPMAP:
680    case GL_SRGB_READ:
681    case GL_SRGB_WRITE:
682    case GL_SRGB_DECODE_ARB:
683    case GL_VERTEX_TEXTURE:
684    case GL_TESS_CONTROL_TEXTURE:
685    case GL_TESS_EVALUATION_TEXTURE:
686    case GL_GEOMETRY_TEXTURE:
687    case GL_FRAGMENT_TEXTURE:
688    case GL_COMPUTE_TEXTURE:
689    case GL_SHADER_IMAGE_LOAD:
690    case GL_SHADER_IMAGE_STORE:
691    case GL_SHADER_IMAGE_ATOMIC:
692    case GL_SIMULTANEOUS_TEXTURE_AND_DEPTH_TEST:
693    case GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_TEST:
694    case GL_SIMULTANEOUS_TEXTURE_AND_DEPTH_WRITE:
695    case GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_WRITE:
696    case GL_CLEAR_BUFFER:
697    case GL_TEXTURE_VIEW:
698    case GL_TEXTURE_SHADOW:
699    case GL_TEXTURE_GATHER:
700    case GL_TEXTURE_GATHER_SHADOW:
701    case GL_FRAMEBUFFER_RENDERABLE:
702    case GL_FRAMEBUFFER_RENDERABLE_LAYERED:
703    case GL_FRAMEBUFFER_BLEND:
704    case GL_FILTER:
705       params[0] = GL_FULL_SUPPORT;
706       break;
707    case GL_NUM_TILING_TYPES_EXT:
708       params[0] = 2;
709       break;
710    case GL_TILING_TYPES_EXT:
711       params[0] = GL_OPTIMAL_TILING_EXT;
712       params[1] = GL_LINEAR_TILING_EXT;
713       break;
714 
715    default:
716       _set_default_response(pname, params);
717       break;
718    }
719 }
720 
721 /*
722  * For MAX_WIDTH/MAX_HEIGHT/MAX_DEPTH it returns the equivalent GetInteger
723  * pname for a Getinternalformat pname/target combination. target/pname
724  * combinations that would return 0 due dimension number or unsupported status
725  * should be already filtered out
726  *
727  * Note that this means that the returned value would be independent of the
728  * internalformat. This possibility is already mentioned at the Issue 7 of the
729  * arb_internalformat_query2 spec.
730  */
731 static GLenum
_equivalent_size_pname(GLenum target,GLenum pname)732 _equivalent_size_pname(GLenum target,
733                        GLenum pname)
734 {
735    switch (target) {
736    case GL_TEXTURE_1D:
737    case GL_TEXTURE_2D:
738    case GL_TEXTURE_2D_MULTISAMPLE:
739       return GL_MAX_TEXTURE_SIZE;
740    case GL_TEXTURE_3D:
741       return GL_MAX_3D_TEXTURE_SIZE;
742    case GL_TEXTURE_CUBE_MAP:
743       return GL_MAX_CUBE_MAP_TEXTURE_SIZE;
744    case GL_TEXTURE_RECTANGLE:
745       return GL_MAX_RECTANGLE_TEXTURE_SIZE;
746    case GL_RENDERBUFFER:
747       return GL_MAX_RENDERBUFFER_SIZE;
748    case GL_TEXTURE_1D_ARRAY:
749       if (pname == GL_MAX_HEIGHT)
750          return GL_MAX_ARRAY_TEXTURE_LAYERS;
751       else
752          return GL_MAX_TEXTURE_SIZE;
753    case GL_TEXTURE_2D_ARRAY:
754    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
755       if (pname == GL_MAX_DEPTH)
756          return GL_MAX_ARRAY_TEXTURE_LAYERS;
757       else
758          return GL_MAX_TEXTURE_SIZE;
759    case GL_TEXTURE_CUBE_MAP_ARRAY:
760       if (pname == GL_MAX_DEPTH)
761          return GL_MAX_ARRAY_TEXTURE_LAYERS;
762       else
763          return GL_MAX_CUBE_MAP_TEXTURE_SIZE;
764    case GL_TEXTURE_BUFFER:
765       return GL_MAX_TEXTURE_BUFFER_SIZE;
766    default:
767       return 0;
768    }
769 }
770 
771 /*
772  * Returns the dimensions associated to a target. GL_TEXTURE_BUFFER and
773  * GL_RENDERBUFFER have associated a dimension, but they are not textures
774  * per-se, so we can't just call _mesa_get_texture_dimension directly.
775  */
776 static GLint
_get_target_dimensions(GLenum target)777 _get_target_dimensions(GLenum target)
778 {
779    switch(target) {
780    case GL_TEXTURE_BUFFER:
781       return 1;
782    case GL_RENDERBUFFER:
783       return 2;
784    default:
785       return _mesa_get_texture_dimensions(target);
786    }
787 }
788 
789 /*
790  * Returns the minimum amount of dimensions associated to a pname. So for
791  * example, if querying GL_MAX_HEIGHT, it is assumed that your target would
792  * have as minimum 2 dimensions.
793  *
794  * Useful to handle sentences like this from query2 spec:
795  *
796  * "MAX_HEIGHT:
797  *  <skip>
798  *  If the resource does not have at least two dimensions
799  *  <skip>."
800  */
801 static GLint
_get_min_dimensions(GLenum pname)802 _get_min_dimensions(GLenum pname)
803 {
804    switch(pname) {
805    case GL_MAX_WIDTH:
806       return 1;
807    case GL_MAX_HEIGHT:
808       return 2;
809    case GL_MAX_DEPTH:
810       return 3;
811    default:
812       return 0;
813    }
814 }
815 
816 /*
817  * Similar to teximage.c:check_multisample_target, but independent of the
818  * dimensions.
819  */
820 static bool
_is_multisample_target(GLenum target)821 _is_multisample_target(GLenum target)
822 {
823    switch(target) {
824    case GL_TEXTURE_2D_MULTISAMPLE:
825    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
826       return true;
827    default:
828       return false;
829    }
830 
831 }
832 
833 void GLAPIENTRY
_mesa_GetInternalformativ(GLenum target,GLenum internalformat,GLenum pname,GLsizei bufSize,GLint * params)834 _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
835                           GLsizei bufSize, GLint *params)
836 {
837    GLint buffer[16];
838    GET_CURRENT_CONTEXT(ctx);
839 
840    ASSERT_OUTSIDE_BEGIN_END(ctx);
841 
842    /* ARB_internalformat_query is also mandatory for ARB_internalformat_query2 */
843    if (!(_mesa_has_ARB_internalformat_query(ctx) ||
844          _mesa_is_gles3(ctx))) {
845       _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInternalformativ");
846       return;
847    }
848 
849    assert(ctx->Driver.QueryInternalFormat != NULL);
850 
851    if (!_legal_parameters(ctx, target, internalformat, pname, bufSize, params))
852       return;
853 
854    /* initialize the contents of the temporary buffer */
855    memcpy(buffer, params, MIN2(bufSize, 16) * sizeof(GLint));
856 
857    /* Use the 'unsupported' response defined by the spec for every pname
858     * as the default answer.
859     */
860    _set_default_response(pname, buffer);
861 
862    if (!_is_target_supported(ctx, target) ||
863        !_is_internalformat_supported(ctx, target, internalformat) ||
864        !_is_resource_supported(ctx, target, internalformat, pname))
865       goto end;
866 
867    switch (pname) {
868    case GL_SAMPLES:
869       /* fall-through */
870    case GL_NUM_SAMPLE_COUNTS:
871       /* The ARB_internalformat_query2 sets the response as 'unsupported' for
872        * SAMPLES and NUM_SAMPLE_COUNTS:
873        *
874        *     "If <internalformat> is not color-renderable, depth-renderable, or
875        *     stencil-renderable (as defined in section 4.4.4), or if <target>
876        *     does not support multiple samples (ie other than
877        *     TEXTURE_2D_MULTISAMPLE,  TEXTURE_2D_MULTISAMPLE_ARRAY,
878        *     or RENDERBUFFER)."
879        */
880       if ((target != GL_RENDERBUFFER &&
881            target != GL_TEXTURE_2D_MULTISAMPLE &&
882            target != GL_TEXTURE_2D_MULTISAMPLE_ARRAY) ||
883           !_is_renderable(ctx, internalformat))
884          goto end;
885 
886       /* The GL ES 3.0 specification, section 6.1.15 page 236 says:
887        *
888        *     "Since multisampling is not supported for signed and unsigned
889        *     integer internal formats, the value of NUM_SAMPLE_COUNTS will be
890        *     zero for such formats.
891        *
892        * Since OpenGL ES 3.1 adds support for multisampled integer formats, we
893        * have to check the version for 30 exactly.
894        */
895       if (pname == GL_NUM_SAMPLE_COUNTS && ctx->API == API_OPENGLES2 &&
896           ctx->Version == 30 && _mesa_is_enum_format_integer(internalformat)) {
897          goto end;
898       }
899 
900       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
901                                       buffer);
902       break;
903 
904    case GL_INTERNALFORMAT_SUPPORTED:
905       /* Having a supported <internalformat> is implemented as a prerequisite
906        * for all the <pnames>. Thus,  if we reach this point, the internalformat is
907        * supported.
908        */
909       buffer[0] = GL_TRUE;
910       break;
911 
912    case GL_INTERNALFORMAT_PREFERRED:
913       /* The ARB_internalformat_query2 spec says:
914        *
915        *     "- INTERNALFORMAT_PREFERRED: The implementation-preferred internal
916        *     format for representing resources of the specified <internalformat> is
917        *     returned in <params>.
918        *
919        * Therefore, we let the driver answer. Note that if we reach this
920        * point, it means that the internalformat is supported, so the driver
921        * is called just to try to get a preferred format. If not supported,
922        * GL_NONE was already returned and the driver is not called.
923        */
924       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
925                                       buffer);
926       break;
927 
928    case GL_INTERNALFORMAT_RED_SIZE:
929    case GL_INTERNALFORMAT_GREEN_SIZE:
930    case GL_INTERNALFORMAT_BLUE_SIZE:
931    case GL_INTERNALFORMAT_ALPHA_SIZE:
932    case GL_INTERNALFORMAT_DEPTH_SIZE:
933    case GL_INTERNALFORMAT_STENCIL_SIZE:
934    case GL_INTERNALFORMAT_SHARED_SIZE:
935    case GL_INTERNALFORMAT_RED_TYPE:
936    case GL_INTERNALFORMAT_GREEN_TYPE:
937    case GL_INTERNALFORMAT_BLUE_TYPE:
938    case GL_INTERNALFORMAT_ALPHA_TYPE:
939    case GL_INTERNALFORMAT_DEPTH_TYPE:
940    case GL_INTERNALFORMAT_STENCIL_TYPE: {
941       GLint baseformat;
942       mesa_format texformat;
943 
944       if (target != GL_RENDERBUFFER) {
945          if (!_mesa_legal_get_tex_level_parameter_target(ctx, target, true))
946             goto end;
947 
948          baseformat = _mesa_base_tex_format(ctx, internalformat);
949       } else {
950          baseformat = _mesa_base_fbo_format(ctx, internalformat);
951       }
952 
953       /* Let the driver choose the texture format.
954        *
955        * Disclaimer: I am considering that drivers use for renderbuffers the
956        * same format-choice logic as for textures.
957        */
958       texformat = ctx->Driver.ChooseTextureFormat(ctx, target, internalformat,
959                                                   GL_NONE /*format */, GL_NONE /* type */);
960 
961       if (texformat == MESA_FORMAT_NONE || baseformat <= 0)
962          goto end;
963 
964       /* Implementation based on what Mesa does for glGetTexLevelParameteriv
965        * and glGetRenderbufferParameteriv functions.
966        */
967       if (pname == GL_INTERNALFORMAT_SHARED_SIZE) {
968          if (_mesa_has_EXT_texture_shared_exponent(ctx) &&
969              target != GL_TEXTURE_BUFFER &&
970              target != GL_RENDERBUFFER &&
971              texformat == MESA_FORMAT_R9G9B9E5_FLOAT) {
972             buffer[0] = 5;
973          }
974          goto end;
975       }
976 
977       if (!_mesa_base_format_has_channel(baseformat, pname))
978          goto end;
979 
980       switch (pname) {
981       case GL_INTERNALFORMAT_DEPTH_SIZE:
982          if (ctx->API != API_OPENGL_CORE &&
983              !_mesa_has_ARB_depth_texture(ctx) &&
984              target != GL_RENDERBUFFER &&
985              target != GL_TEXTURE_BUFFER)
986             goto end;
987          /* fallthrough */
988       case GL_INTERNALFORMAT_RED_SIZE:
989       case GL_INTERNALFORMAT_GREEN_SIZE:
990       case GL_INTERNALFORMAT_BLUE_SIZE:
991       case GL_INTERNALFORMAT_ALPHA_SIZE:
992       case GL_INTERNALFORMAT_STENCIL_SIZE:
993          buffer[0] = _mesa_get_format_bits(texformat, pname);
994          break;
995 
996       case GL_INTERNALFORMAT_DEPTH_TYPE:
997          if (!_mesa_has_ARB_texture_float(ctx))
998             goto end;
999          /* fallthrough */
1000       case GL_INTERNALFORMAT_RED_TYPE:
1001       case GL_INTERNALFORMAT_GREEN_TYPE:
1002       case GL_INTERNALFORMAT_BLUE_TYPE:
1003       case GL_INTERNALFORMAT_ALPHA_TYPE:
1004       case GL_INTERNALFORMAT_STENCIL_TYPE:
1005          buffer[0]  = _mesa_get_format_datatype(texformat);
1006          break;
1007 
1008       default:
1009          break;
1010 
1011       }
1012       break;
1013    }
1014 
1015       /* For WIDTH/HEIGHT/DEPTH/LAYERS there is no reason to think that the
1016        * returned values should be different to the values returned by
1017        * GetInteger with MAX_TEXTURE_SIZE, MAX_3D_TEXTURE_SIZE, etc.*/
1018    case GL_MAX_WIDTH:
1019    case GL_MAX_HEIGHT:
1020    case GL_MAX_DEPTH: {
1021       GLenum get_pname;
1022       GLint dimensions;
1023       GLint min_dimensions;
1024 
1025       /* From query2:MAX_HEIGHT spec (as example):
1026        *
1027        * "If the resource does not have at least two dimensions, or if the
1028        * resource is unsupported, zero is returned."
1029        */
1030       dimensions = _get_target_dimensions(target);
1031       min_dimensions = _get_min_dimensions(pname);
1032       if (dimensions < min_dimensions)
1033          goto end;
1034 
1035       get_pname = _equivalent_size_pname(target, pname);
1036       if (get_pname == 0)
1037          goto end;
1038 
1039       _mesa_GetIntegerv(get_pname, buffer);
1040       break;
1041    }
1042 
1043    case GL_MAX_LAYERS:
1044       if (!_mesa_has_EXT_texture_array(ctx))
1045          goto end;
1046 
1047       if (!_mesa_is_array_texture(target))
1048          goto end;
1049 
1050       _mesa_GetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, buffer);
1051       break;
1052 
1053    case GL_MAX_COMBINED_DIMENSIONS:{
1054       GLint64 combined_value = 1;
1055       GLenum max_dimensions_pnames[] = {
1056          GL_MAX_WIDTH,
1057          GL_MAX_HEIGHT,
1058          GL_MAX_DEPTH,
1059          GL_SAMPLES
1060       };
1061       unsigned i;
1062       GLint current_value;
1063 
1064       /* Combining the dimensions. Note that for array targets, this would
1065        * automatically include the value of MAX_LAYERS, as that value is
1066        * returned as MAX_HEIGHT or MAX_DEPTH */
1067       for (i = 0; i < 4; i++) {
1068          if (max_dimensions_pnames[i] == GL_SAMPLES &&
1069              !_is_multisample_target(target))
1070             continue;
1071 
1072          _mesa_GetInternalformativ(target, internalformat,
1073                                    max_dimensions_pnames[i],
1074                                    1, &current_value);
1075 
1076          if (current_value != 0)
1077             combined_value *= current_value;
1078       }
1079 
1080       if (_mesa_is_cube_map_texture(target))
1081          combined_value *= 6;
1082 
1083       /* We pack the 64-bit value on two 32-bit values. Calling the 32-bit
1084        * query, this would work as far as the value can be hold on a 32-bit
1085        * signed integer. For the 64-bit query, the wrapper around the 32-bit
1086        * query will unpack the value */
1087       memcpy(buffer, &combined_value, sizeof(GLint64));
1088       break;
1089    }
1090 
1091    case GL_COLOR_COMPONENTS:
1092       /* The ARB_internalformat_query2 spec says:
1093        *
1094        *     "- COLOR_COMPONENTS: If the internal format contains any color
1095        *     components (R, G, B, or A), TRUE is returned in <params>.
1096        *     If the internal format is unsupported or contains no color
1097        *     components, FALSE is returned."
1098        */
1099       if (_mesa_is_color_format(internalformat))
1100          buffer[0] = GL_TRUE;
1101       break;
1102 
1103    case GL_DEPTH_COMPONENTS:
1104       /* The ARB_internalformat_query2 spec says:
1105        *
1106        *     "- DEPTH_COMPONENTS: If the internal format contains a depth
1107        *     component (D), TRUE is returned in <params>. If the internal format
1108        *     is unsupported or contains no depth component, FALSE is returned."
1109        */
1110       if (_mesa_is_depth_format(internalformat) ||
1111           _mesa_is_depthstencil_format(internalformat))
1112          buffer[0] = GL_TRUE;
1113       break;
1114 
1115    case GL_STENCIL_COMPONENTS:
1116       /* The ARB_internalformat_query2 spec says:
1117        *
1118        *     "- STENCIL_COMPONENTS: If the internal format contains a stencil
1119        *     component (S), TRUE is returned in <params>. If the internal format
1120        *     is unsupported or contains no stencil component, FALSE is returned.
1121        */
1122       if (_mesa_is_stencil_format(internalformat) ||
1123           _mesa_is_depthstencil_format(internalformat))
1124          buffer[0] = GL_TRUE;
1125       break;
1126 
1127    case GL_COLOR_RENDERABLE:
1128    case GL_DEPTH_RENDERABLE:
1129    case GL_STENCIL_RENDERABLE:
1130       if (!_is_renderable(ctx, internalformat))
1131          goto end;
1132 
1133       if (pname == GL_COLOR_RENDERABLE) {
1134          if (!_mesa_is_color_format(internalformat))
1135             goto end;
1136       } else {
1137          GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
1138          if (baseFormat != GL_DEPTH_STENCIL &&
1139              ((pname == GL_DEPTH_RENDERABLE && baseFormat != GL_DEPTH_COMPONENT) ||
1140               (pname == GL_STENCIL_RENDERABLE && baseFormat != GL_STENCIL_INDEX)))
1141             goto end;
1142       }
1143 
1144       buffer[0] = GL_TRUE;
1145       break;
1146 
1147    case GL_FRAMEBUFFER_RENDERABLE_LAYERED:
1148       if (!_mesa_has_EXT_texture_array(ctx) ||
1149           _legal_target_for_framebuffer_texture_layer(ctx, target))
1150          goto end;
1151       /* fallthrough */
1152    case GL_FRAMEBUFFER_RENDERABLE:
1153    case GL_FRAMEBUFFER_BLEND:
1154       if (!_mesa_has_ARB_framebuffer_object(ctx))
1155          goto end;
1156 
1157       if (target == GL_TEXTURE_BUFFER ||
1158           !_is_renderable(ctx, internalformat))
1159          goto end;
1160 
1161       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1162                                       buffer);
1163       break;
1164 
1165    case GL_READ_PIXELS:
1166    case GL_READ_PIXELS_FORMAT:
1167    case GL_READ_PIXELS_TYPE:
1168       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1169                                       buffer);
1170       break;
1171 
1172    case GL_TEXTURE_IMAGE_FORMAT:
1173    case GL_GET_TEXTURE_IMAGE_FORMAT:
1174    case GL_TEXTURE_IMAGE_TYPE:
1175    case GL_GET_TEXTURE_IMAGE_TYPE:
1176       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1177                                       buffer);
1178       break;
1179 
1180    case GL_MIPMAP:
1181    case GL_MANUAL_GENERATE_MIPMAP:
1182    case GL_AUTO_GENERATE_MIPMAP:
1183       if (!_mesa_is_valid_generate_texture_mipmap_target(ctx, target) ||
1184           !_mesa_is_valid_generate_texture_mipmap_internalformat(ctx,
1185                                                               internalformat)) {
1186          goto end;
1187       }
1188 
1189       if (pname == GL_MIPMAP) {
1190          buffer[0] = GL_TRUE;
1191          goto end;
1192       }
1193       else if (pname == GL_MANUAL_GENERATE_MIPMAP) {
1194          if (!_mesa_has_ARB_framebuffer_object(ctx))
1195             goto end;
1196       }
1197       else {
1198          /* From ARB_internalformat_query2:
1199           *    "Dependencies on OpenGL 3.2 (Core Profile)
1200           *     In core profiles for OpenGL 3.2 and later versions, queries
1201           *     for the AUTO_GENERATE_MIPMAP <pname> return the appropriate
1202           *     unsupported response."
1203           */
1204          if (_mesa_is_desktop_gl(ctx) && ctx->Version >= 32)
1205             goto end;
1206       }
1207 
1208       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1209                                       buffer);
1210       break;
1211 
1212    case GL_COLOR_ENCODING:
1213       if (!_mesa_is_color_format(internalformat))
1214          goto end;
1215 
1216       if (_mesa_is_srgb_format(internalformat))
1217          buffer[0] = GL_SRGB;
1218       else
1219          buffer[0] = GL_LINEAR;
1220       break;
1221 
1222    case GL_SRGB_READ:
1223       if (!_mesa_has_EXT_texture_sRGB(ctx) ||
1224           !_mesa_is_srgb_format(internalformat)) {
1225          goto end;
1226       }
1227 
1228       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1229                                       buffer);
1230       break;
1231 
1232    case GL_SRGB_WRITE:
1233       if (!_mesa_has_EXT_framebuffer_sRGB(ctx) ||
1234           !_mesa_is_color_format(internalformat)) {
1235          goto end;
1236       }
1237 
1238       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1239                                       buffer);
1240       break;
1241 
1242    case GL_SRGB_DECODE_ARB:
1243       /* Presence of EXT_texture_sRGB_decode was already verified */
1244       if (!_mesa_has_EXT_texture_sRGB(ctx) ||
1245           target == GL_RENDERBUFFER ||
1246           !_mesa_is_srgb_format(internalformat)) {
1247          goto end;
1248       }
1249 
1250       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1251                                       buffer);
1252       break;
1253 
1254    case GL_FILTER:
1255       /* If it doesn't allow to set sampler parameters then it would not allow
1256        * to set a filter different to GL_NEAREST. In practice, this method
1257        * only filters out MULTISAMPLE/MULTISAMPLE_ARRAY */
1258       if (!_mesa_target_allows_setting_sampler_parameters(target))
1259          goto end;
1260 
1261       if (_mesa_is_enum_format_integer(internalformat))
1262          goto end;
1263 
1264       if (target == GL_TEXTURE_BUFFER)
1265          goto end;
1266 
1267       /* At this point we know that multi-texel filtering is supported. We
1268        * need to call the driver to know if it is CAVEAT_SUPPORT or
1269        * FULL_SUPPORT.
1270        */
1271       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1272                                       buffer);
1273       break;
1274 
1275    case GL_VERTEX_TEXTURE:
1276    case GL_TESS_CONTROL_TEXTURE:
1277    case GL_TESS_EVALUATION_TEXTURE:
1278    case GL_GEOMETRY_TEXTURE:
1279    case GL_FRAGMENT_TEXTURE:
1280    case GL_COMPUTE_TEXTURE:
1281       if (target == GL_RENDERBUFFER)
1282          goto end;
1283 
1284       if ((pname == GL_TESS_CONTROL_TEXTURE ||
1285            pname == GL_TESS_EVALUATION_TEXTURE) &&
1286           !_mesa_has_tessellation(ctx))
1287          goto end;
1288 
1289       if (pname == GL_GEOMETRY_TEXTURE && !_mesa_has_geometry_shaders(ctx))
1290          goto end;
1291 
1292       if (pname == GL_COMPUTE_TEXTURE && !_mesa_has_compute_shaders(ctx))
1293          goto end;
1294 
1295       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1296                                       buffer);
1297       break;
1298 
1299    case GL_TEXTURE_GATHER:
1300    case GL_TEXTURE_GATHER_SHADOW:
1301       if (!_mesa_has_ARB_texture_gather(ctx))
1302          goto end;
1303 
1304       /* fallthrough */
1305    case GL_TEXTURE_SHADOW:
1306       /* Only depth or depth-stencil image formats make sense in shadow
1307          samplers */
1308       if (pname != GL_TEXTURE_GATHER &&
1309           !_mesa_is_depth_format(internalformat) &&
1310           !_mesa_is_depthstencil_format(internalformat))
1311          goto end;
1312 
1313       /* Validate the target for shadow and gather operations */
1314       switch (target) {
1315       case GL_TEXTURE_2D:
1316       case GL_TEXTURE_2D_ARRAY:
1317       case GL_TEXTURE_CUBE_MAP:
1318       case GL_TEXTURE_CUBE_MAP_ARRAY:
1319       case GL_TEXTURE_RECTANGLE:
1320          break;
1321 
1322       case GL_TEXTURE_1D:
1323       case GL_TEXTURE_1D_ARRAY:
1324          /* 1D and 1DArray textures are not admitted in gather operations */
1325          if (pname != GL_TEXTURE_SHADOW)
1326             goto end;
1327          break;
1328 
1329       default:
1330          goto end;
1331       }
1332 
1333       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1334                                       buffer);
1335       break;
1336 
1337    case GL_SHADER_IMAGE_LOAD:
1338    case GL_SHADER_IMAGE_STORE:
1339       if (!_mesa_has_ARB_shader_image_load_store(ctx))
1340          goto end;
1341 
1342       /* We call to _mesa_is_shader_image_format_supported
1343        * using "internalformat" as parameter, because the
1344        * the ARB_internalformat_query2 spec says:
1345        * "In this case the <internalformat> is the value of the <format>
1346        * parameter that is passed to BindImageTexture."
1347        */
1348       if (target == GL_RENDERBUFFER ||
1349           !_mesa_is_shader_image_format_supported(ctx, internalformat))
1350          goto end;
1351 
1352       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1353                                       buffer);
1354       break;
1355 
1356    case GL_SHADER_IMAGE_ATOMIC:
1357       if (!_mesa_has_ARB_shader_image_load_store(ctx))
1358          goto end;
1359 
1360       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1361                                       buffer);
1362       break;
1363 
1364    case GL_IMAGE_TEXEL_SIZE: {
1365       mesa_format image_format;
1366 
1367       if (!_mesa_has_ARB_shader_image_load_store(ctx) ||
1368           target == GL_RENDERBUFFER)
1369          goto end;
1370 
1371       image_format = _mesa_get_shader_image_format(internalformat);
1372       if (image_format == MESA_FORMAT_NONE)
1373          goto end;
1374 
1375       /* We return bits */
1376       buffer[0] = (_mesa_get_format_bytes(image_format) * 8);
1377       break;
1378    }
1379 
1380    case GL_IMAGE_COMPATIBILITY_CLASS:
1381       if (!_mesa_has_ARB_shader_image_load_store(ctx) ||
1382           target == GL_RENDERBUFFER)
1383          goto end;
1384 
1385       buffer[0] = _mesa_get_image_format_class(internalformat);
1386       break;
1387 
1388    case GL_IMAGE_PIXEL_FORMAT: {
1389       GLint base_format;
1390 
1391       if (!_mesa_has_ARB_shader_image_load_store(ctx) ||
1392           target == GL_RENDERBUFFER ||
1393           !_mesa_is_shader_image_format_supported(ctx, internalformat))
1394          goto end;
1395 
1396       base_format = _mesa_base_tex_format(ctx, internalformat);
1397       if (base_format == -1)
1398          goto end;
1399 
1400       if (_mesa_is_enum_format_integer(internalformat))
1401          buffer[0] = _mesa_base_format_to_integer_format(base_format);
1402       else
1403          buffer[0] = base_format;
1404       break;
1405    }
1406 
1407    case GL_IMAGE_PIXEL_TYPE: {
1408       mesa_format image_format;
1409       GLenum datatype;
1410       GLuint comps;
1411 
1412       if (!_mesa_has_ARB_shader_image_load_store(ctx) ||
1413           target == GL_RENDERBUFFER)
1414          goto end;
1415 
1416       image_format = _mesa_get_shader_image_format(internalformat);
1417       if (image_format == MESA_FORMAT_NONE)
1418          goto end;
1419 
1420       _mesa_uncompressed_format_to_type_and_comps(image_format, &datatype,
1421                                                   &comps);
1422       if (!datatype)
1423          goto end;
1424 
1425       buffer[0] = datatype;
1426       break;
1427    }
1428 
1429    case GL_IMAGE_FORMAT_COMPATIBILITY_TYPE: {
1430       if (!_mesa_has_ARB_shader_image_load_store(ctx))
1431          goto end;
1432 
1433       /* As pointed by the spec quote below, this pname query should return
1434        * the same value that GetTexParameter. So if the target is not valid
1435        * for GetTexParameter we return the unsupported value. The check below
1436        * is the same target check used by GetTexParameter.
1437        */
1438       int targetIndex = _mesa_tex_target_to_index(ctx, target);
1439       if (targetIndex < 0 || targetIndex == TEXTURE_BUFFER_INDEX)
1440          goto end;
1441 
1442       /* From spec: "Equivalent to calling GetTexParameter with <value> set
1443        * to IMAGE_FORMAT_COMPATIBILITY_TYPE."
1444        *
1445        * GetTexParameter just returns
1446        * tex_obj->ImageFormatCompatibilityType. We create a fake tex_obj
1447        * just with the purpose of getting the value.
1448        */
1449       struct gl_texture_object *tex_obj = _mesa_new_texture_object(ctx, 0, target);
1450       buffer[0] = tex_obj->ImageFormatCompatibilityType;
1451       _mesa_delete_texture_object(ctx, tex_obj);
1452 
1453       break;
1454    }
1455 
1456    case GL_SIMULTANEOUS_TEXTURE_AND_DEPTH_TEST:
1457    case GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_TEST:
1458    case GL_SIMULTANEOUS_TEXTURE_AND_DEPTH_WRITE:
1459    case GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_WRITE:
1460       if (target == GL_RENDERBUFFER)
1461          goto end;
1462 
1463       if (!_mesa_is_depthstencil_format(internalformat)) {
1464          if (((pname == GL_SIMULTANEOUS_TEXTURE_AND_DEPTH_TEST ||
1465                pname == GL_SIMULTANEOUS_TEXTURE_AND_DEPTH_WRITE) &&
1466               !_mesa_is_depth_format(internalformat)) ||
1467              ((pname == GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_TEST ||
1468                pname == GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_WRITE) &&
1469               !_mesa_is_stencil_format(internalformat)))
1470             goto end;
1471       }
1472 
1473       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1474                                       buffer);
1475       break;
1476 
1477    case GL_TEXTURE_COMPRESSED:
1478       buffer[0] = _mesa_is_compressed_format(ctx, internalformat);
1479       break;
1480 
1481    case GL_TEXTURE_COMPRESSED_BLOCK_WIDTH:
1482    case GL_TEXTURE_COMPRESSED_BLOCK_HEIGHT:
1483    case GL_TEXTURE_COMPRESSED_BLOCK_SIZE: {
1484       mesa_format mesaformat;
1485       GLint block_size;
1486 
1487       mesaformat = _mesa_glenum_to_compressed_format(internalformat);
1488       if (mesaformat == MESA_FORMAT_NONE)
1489          goto end;
1490 
1491       block_size = _mesa_get_format_bytes(mesaformat);
1492       assert(block_size > 0);
1493 
1494       if (pname == GL_TEXTURE_COMPRESSED_BLOCK_SIZE) {
1495          buffer[0] = block_size;
1496       } else {
1497          GLuint bwidth, bheight;
1498 
1499          /* Returns the width and height in pixels. We return bytes */
1500          _mesa_get_format_block_size(mesaformat, &bwidth, &bheight);
1501          assert(bwidth > 0 && bheight > 0);
1502 
1503          if (pname == GL_TEXTURE_COMPRESSED_BLOCK_WIDTH)
1504             buffer[0] = block_size / bheight;
1505          else
1506             buffer[0] = block_size / bwidth;
1507       }
1508       break;
1509    }
1510 
1511    case GL_CLEAR_BUFFER:
1512       if (target != GL_TEXTURE_BUFFER)
1513          goto end;
1514 
1515       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1516                                       buffer);
1517       break;
1518 
1519    case GL_TEXTURE_VIEW:
1520    case GL_VIEW_COMPATIBILITY_CLASS:
1521       if (!_mesa_has_ARB_texture_view(ctx) ||
1522           target == GL_TEXTURE_BUFFER ||
1523           target == GL_RENDERBUFFER)
1524          goto end;
1525 
1526       if (pname == GL_TEXTURE_VIEW) {
1527          ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1528                                          buffer);
1529       } else {
1530          GLenum view_class = _mesa_texture_view_lookup_view_class(ctx,
1531                                                                   internalformat);
1532          if (view_class == GL_FALSE)
1533             goto end;
1534 
1535          buffer[0] = view_class;
1536       }
1537       break;
1538 
1539    case GL_NUM_TILING_TYPES_EXT:
1540    case GL_TILING_TYPES_EXT:
1541       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
1542                                       buffer);
1543       break;
1544 
1545    default:
1546       unreachable("bad param");
1547    }
1548 
1549  end:
1550    if (bufSize != 0 && params == NULL) {
1551       /* Emit a warning to aid application debugging, but go ahead and do the
1552        * memcpy (and probably crash) anyway.
1553        */
1554       _mesa_warning(ctx,
1555                     "glGetInternalformativ(bufSize = %d, but params = NULL)",
1556                     bufSize);
1557    }
1558 
1559    /* Copy the data from the temporary buffer to the buffer supplied by the
1560     * application.  Clamp the size of the copy to the size supplied by the
1561     * application.
1562     */
1563    memcpy(params, buffer, MIN2(bufSize, 16) * sizeof(GLint));
1564 
1565    return;
1566 }
1567 
1568 void GLAPIENTRY
_mesa_GetInternalformati64v(GLenum target,GLenum internalformat,GLenum pname,GLsizei bufSize,GLint64 * params)1569 _mesa_GetInternalformati64v(GLenum target, GLenum internalformat,
1570                             GLenum pname, GLsizei bufSize, GLint64 *params)
1571 {
1572    GLint params32[16];
1573    unsigned i;
1574    GLsizei realSize = MIN2(bufSize, 16);
1575    GLsizei callSize;
1576 
1577    GET_CURRENT_CONTEXT(ctx);
1578 
1579    ASSERT_OUTSIDE_BEGIN_END(ctx);
1580 
1581    if (!_mesa_has_ARB_internalformat_query2(ctx)) {
1582       _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInternalformati64v");
1583       return;
1584    }
1585 
1586    /* For SAMPLES there are cases where params needs to remain unmodified. As
1587     * no pname can return a negative value, we fill params32 with negative
1588     * values as reference values, that can be used to know what copy-back to
1589     * params */
1590    for (i = 0; i < realSize; i++)
1591       params32[i] = -1;
1592 
1593    /* For GL_MAX_COMBINED_DIMENSIONS we need to get back 2 32-bit integers,
1594     * and at the same time we only need 2. So for that pname, we call the
1595     * 32-bit query with bufSize 2, except on the case of bufSize 0, that is
1596     * basically like asking to not get the value, but that is a caller
1597     * problem. */
1598    if (pname == GL_MAX_COMBINED_DIMENSIONS && bufSize > 0)
1599       callSize = 2;
1600    else
1601       callSize = bufSize;
1602 
1603    _mesa_GetInternalformativ(target, internalformat, pname, callSize, params32);
1604 
1605    if (pname == GL_MAX_COMBINED_DIMENSIONS) {
1606       memcpy(params, params32, sizeof(GLint64));
1607    } else {
1608       for (i = 0; i < realSize; i++) {
1609          /* We only copy back the values that changed */
1610          if (params32[i] < 0)
1611             break;
1612          params[i] = (GLint64) params32[i];
1613       }
1614    }
1615 }
1616