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