Merge remote-tracking branch 'origin/master' into vulkan
[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 "enums.h"
29 #include "fbobject.h"
30 #include "formatquery.h"
31
32 /* default implementation of QuerySamplesForFormat driverfunc, for
33 * non-multisample-capable drivers. */
34 size_t
35 _mesa_query_samples_for_format(struct gl_context *ctx, GLenum target,
36 GLenum internalFormat, int samples[16])
37 {
38 (void) target;
39 (void) internalFormat;
40 (void) ctx;
41
42 samples[0] = 1;
43 return 1;
44 }
45
46
47 void GLAPIENTRY
48 _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
49 GLsizei bufSize, GLint *params)
50 {
51 GLint buffer[16];
52 GLsizei count = 0;
53 GET_CURRENT_CONTEXT(ctx);
54
55 ASSERT_OUTSIDE_BEGIN_END(ctx);
56
57 if (!ctx->Extensions.ARB_internalformat_query) {
58 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInternalformativ");
59 return;
60 }
61
62 assert(ctx->Driver.QuerySamplesForFormat != NULL);
63
64 /* The ARB_internalformat_query spec says:
65 *
66 * "If the <target> parameter to GetInternalformativ is not one of
67 * TEXTURE_2D_MULTISAMPLE, TEXTURE_2D_MULTISAMPLE_ARRAY or RENDERBUFFER
68 * then an INVALID_ENUM error is generated."
69 */
70 switch (target) {
71 case GL_RENDERBUFFER:
72 break;
73
74 case GL_TEXTURE_2D_MULTISAMPLE:
75 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
76 /* These enums are only valid if ARB_texture_multisample is supported */
77 if ((_mesa_is_desktop_gl(ctx) &&
78 ctx->Extensions.ARB_texture_multisample) ||
79 _mesa_is_gles31(ctx))
80 break;
81
82 default:
83 _mesa_error(ctx, GL_INVALID_ENUM,
84 "glGetInternalformativ(target=%s)",
85 _mesa_enum_to_string(target));
86 return;
87 }
88
89 /* The ARB_internalformat_query spec says:
90 *
91 * "If the <internalformat> parameter to GetInternalformativ is not
92 * color-, depth- or stencil-renderable, then an INVALID_ENUM error is
93 * generated."
94 *
95 * Page 243 of the GLES 3.0.4 spec says this for GetInternalformativ:
96 *
97 * "internalformat must be color-renderable, depth-renderable or
98 * stencilrenderable (as defined in section 4.4.4)."
99 *
100 * Section 4.4.4 on page 212 of the same spec says:
101 *
102 * "An internal format is color-renderable if it is one of the
103 * formats from table 3.13 noted as color-renderable or if it
104 * is unsized format RGBA or RGB."
105 *
106 * Therefore, we must accept GL_RGB and GL_RGBA here.
107 */
108 if (internalformat != GL_RGB && internalformat != GL_RGBA &&
109 _mesa_base_fbo_format(ctx, internalformat) == 0) {
110 _mesa_error(ctx, GL_INVALID_ENUM,
111 "glGetInternalformativ(internalformat=%s)",
112 _mesa_enum_to_string(internalformat));
113 return;
114 }
115
116 /* The ARB_internalformat_query spec says:
117 *
118 * "If the <bufSize> parameter to GetInternalformativ is negative, then
119 * an INVALID_VALUE error is generated."
120 */
121 if (bufSize < 0) {
122 _mesa_error(ctx, GL_INVALID_VALUE,
123 "glGetInternalformativ(target=%s)",
124 _mesa_enum_to_string(target));
125 return;
126 }
127
128 switch (pname) {
129 case GL_SAMPLES:
130 count = ctx->Driver.QuerySamplesForFormat(ctx, target,
131 internalformat, buffer);
132 break;
133 case GL_NUM_SAMPLE_COUNTS: {
134 if ((ctx->API == API_OPENGLES2 && ctx->Version == 30) &&
135 _mesa_is_enum_format_integer(internalformat)) {
136 /* From GL ES 3.0 specification, section 6.1.15 page 236: "Since
137 * multisampling is not supported for signed and unsigned integer
138 * internal formats, the value of NUM_SAMPLE_COUNTS will be zero
139 * for such formats.
140 *
141 * Such a restriction no longer exists in GL ES 3.1.
142 */
143 buffer[0] = 0;
144 count = 1;
145 } else {
146 size_t num_samples;
147
148 /* The driver can return 0, and we should pass that along to the
149 * application. The ARB decided that ARB_internalformat_query should
150 * behave as ARB_internalformat_query2 in this situation.
151 *
152 * The ARB_internalformat_query2 spec says:
153 *
154 * "- NUM_SAMPLE_COUNTS: The number of sample counts that would be
155 * returned by querying SAMPLES is returned in <params>.
156 * * If <internalformat> is not color-renderable,
157 * depth-renderable, or stencil-renderable (as defined in
158 * section 4.4.4), or if <target> does not support multiple
159 * samples (ie other than TEXTURE_2D_MULTISAMPLE,
160 * TEXTURE_2D_MULTISAMPLE_ARRAY, or RENDERBUFFER), 0 is
161 * returned."
162 */
163 num_samples = ctx->Driver.QuerySamplesForFormat(ctx, target, internalformat, buffer);
164
165 /* QuerySamplesForFormat writes some stuff to buffer, so we have to
166 * separately over-write it with the requested value.
167 */
168 buffer[0] = (GLint) num_samples;
169 count = 1;
170 }
171 break;
172 }
173 default:
174 _mesa_error(ctx, GL_INVALID_ENUM,
175 "glGetInternalformativ(pname=%s)",
176 _mesa_enum_to_string(pname));
177 return;
178 }
179
180 if (bufSize != 0 && params == NULL) {
181 /* Emit a warning to aid application debugging, but go ahead and do the
182 * memcpy (and probably crash) anyway.
183 */
184 _mesa_warning(ctx,
185 "glGetInternalformativ(bufSize = %d, but params = NULL)",
186 bufSize);
187 }
188
189 /* Copy the data from the temporary buffer to the buffer supplied by the
190 * application. Clamp the size of the copy to the size supplied by the
191 * application.
192 */
193 memcpy(params, buffer, MIN2(count, bufSize) * sizeof(GLint));
194
195 return;
196 }