mesa: allow internalformat_query with multisample texture targets
[mesa.git] / src / mesa / main / formatquery.c
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 "mfeatures.h"
29 #include "enums.h"
30 #include "fbobject.h"
31 #include "formatquery.h"
32
33 void GLAPIENTRY
34 _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
35 GLsizei bufSize, GLint *params)
36 {
37 GLint buffer[16];
38 GLsizei count = 0;
39 GET_CURRENT_CONTEXT(ctx);
40
41 ASSERT_OUTSIDE_BEGIN_END(ctx);
42
43 if (!ctx->Extensions.ARB_internalformat_query) {
44 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInternalformativ");
45 return;
46 }
47
48 assert(ctx->Driver.QuerySamplesForFormat != NULL);
49
50 /* The ARB_internalformat_query spec says:
51 *
52 * "If the <target> parameter to GetInternalformativ is not one of
53 * TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY or RENDERBUFFER
54 * then an INVALID_ENUM error is generated."
55 */
56 switch (target) {
57 case GL_RENDERBUFFER:
58 break;
59
60 case GL_TEXTURE_2D_MULTISAMPLE:
61 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
62 /* These enums are only valid if ARB_texture_multisample is supported */
63 if (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_multisample)
64 break;
65
66 default:
67 _mesa_error(ctx, GL_INVALID_ENUM,
68 "glGetInternalformativ(target=%s)",
69 _mesa_lookup_enum_by_nr(target));
70 return;
71 }
72
73 /* The ARB_internalformat_query spec says:
74 *
75 * "If the <internalformat> parameter to GetInternalformativ is not
76 * color-, depth- or stencil-renderable, then an INVALID_ENUM error is
77 * generated."
78 */
79 if (_mesa_base_fbo_format(ctx, internalformat) == 0) {
80 _mesa_error(ctx, GL_INVALID_ENUM,
81 "glGetInternalformativ(internalformat=%s)",
82 _mesa_lookup_enum_by_nr(internalformat));
83 return;
84 }
85
86 /* The ARB_internalformat_query spec says:
87 *
88 * "If the <bufSize> parameter to GetInternalformativ is negative, then
89 * an INVALID_VALUE error is generated."
90 */
91 if (bufSize < 0) {
92 _mesa_error(ctx, GL_INVALID_VALUE,
93 "glGetInternalformativ(target=%s)",
94 _mesa_lookup_enum_by_nr(target));
95 return;
96 }
97
98 switch (pname) {
99 case GL_SAMPLES:
100 count = ctx->Driver.QuerySamplesForFormat(ctx, target,
101 internalformat, buffer);
102 break;
103 case GL_NUM_SAMPLE_COUNTS: {
104 /* The driver can return 0, and we should pass that along to the
105 * application. The ARB decided that ARB_internalformat_query should
106 * behave as ARB_internalformat_query2 in this situation.
107 *
108 * The ARB_internalformat_query2 spec says:
109 *
110 * "- NUM_SAMPLE_COUNTS: The number of sample counts that would be
111 * returned by querying SAMPLES is returned in <params>.
112 * * If <internalformat> is not color-renderable,
113 * depth-renderable, or stencil-renderable (as defined in
114 * section 4.4.4), or if <target> does not support multiple
115 * samples (ie other than TEXTURE_2D_MULTISAMPLE,
116 * TEXTURE_2D_MULTISAMPLE_ARRAY, or RENDERBUFFER), 0 is
117 * returned."
118 */
119 const size_t num_samples =
120 ctx->Driver.QuerySamplesForFormat(ctx, target, internalformat, buffer);
121
122 /* QuerySamplesForFormat writes some stuff to buffer, so we have to
123 * separately over-write it with the requested value.
124 */
125 buffer[0] = (GLint) num_samples;
126 count = 1;
127 break;
128 }
129 default:
130 _mesa_error(ctx, GL_INVALID_ENUM,
131 "glGetInternalformativ(pname=%s)",
132 _mesa_lookup_enum_by_nr(pname));
133 return;
134 }
135
136 if (bufSize != 0 && params == NULL) {
137 /* Emit a warning to aid application debugging, but go ahead and do the
138 * memcpy (and probably crash) anyway.
139 */
140 _mesa_warning(ctx,
141 "glGetInternalformativ(bufSize = %d, but params = NULL)",
142 bufSize);
143 }
144
145 /* Copy the data from the temporary buffer to the buffer supplied by the
146 * application. Clamp the size of the copy to the size supplied by the
147 * application.
148 */
149 memcpy(params, buffer, MIN2(count, bufSize) * sizeof(GLint));
150
151 return;
152 }