mesa: Replace MESA_VERSION with PACKAGE_VERSION.
[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 /* Mesa does not currently support GL_ARB_texture_multisample, so these
63 * enums are not valid on this implementation either.
64 */
65 default:
66 _mesa_error(ctx, GL_INVALID_ENUM,
67 "glGetInternalformativ(target=%s)",
68 _mesa_lookup_enum_by_nr(target));
69 return;
70 }
71
72 /* The ARB_internalformat_query spec says:
73 *
74 * "If the <internalformat> parameter to GetInternalformativ is not
75 * color-, depth- or stencil-renderable, then an INVALID_ENUM error is
76 * generated."
77 */
78 if (_mesa_base_fbo_format(ctx, internalformat) == 0) {
79 _mesa_error(ctx, GL_INVALID_ENUM,
80 "glGetInternalformativ(internalformat=%s)",
81 _mesa_lookup_enum_by_nr(internalformat));
82 return;
83 }
84
85 /* The ARB_internalformat_query spec says:
86 *
87 * "If the <bufSize> parameter to GetInternalformativ is negative, then
88 * an INVALID_VALUE error is generated."
89 */
90 if (bufSize < 0) {
91 _mesa_error(ctx, GL_INVALID_VALUE,
92 "glGetInternalformativ(target=%s)",
93 _mesa_lookup_enum_by_nr(target));
94 return;
95 }
96
97 switch (pname) {
98 case GL_SAMPLES:
99 count = ctx->Driver.QuerySamplesForFormat(ctx, internalformat, buffer);
100 break;
101 case GL_NUM_SAMPLE_COUNTS: {
102 /* The driver can return 0, and we should pass that along to the
103 * application. The ARB decided that ARB_internalformat_query should
104 * behave as ARB_internalformat_query2 in this situation.
105 *
106 * The ARB_internalformat_query2 spec says:
107 *
108 * "- NUM_SAMPLE_COUNTS: The number of sample counts that would be
109 * returned by querying SAMPLES is returned in <params>.
110 * * If <internalformat> is not color-renderable,
111 * depth-renderable, or stencil-renderable (as defined in
112 * section 4.4.4), or if <target> does not support multiple
113 * samples (ie other than TEXTURE_2D_MULTISAMPLE,
114 * TEXTURE_2D_MULTISAMPLE_ARRAY, or RENDERBUFFER), 0 is
115 * returned."
116 */
117 const size_t num_samples =
118 ctx->Driver.QuerySamplesForFormat(ctx, internalformat, buffer);
119
120 /* QuerySamplesForFormat writes some stuff to buffer, so we have to
121 * separately over-write it with the requested value.
122 */
123 buffer[0] = (GLint) num_samples;
124 count = 1;
125 break;
126 }
127 default:
128 _mesa_error(ctx, GL_INVALID_ENUM,
129 "glGetInternalformativ(pname=%s)",
130 _mesa_lookup_enum_by_nr(pname));
131 return;
132 }
133
134 if (bufSize != 0 && params == NULL) {
135 /* Emit a warning to aid application debugging, but go ahead and do the
136 * memcpy (and probably crash) anyway.
137 */
138 _mesa_warning(ctx,
139 "glGetInternalformativ(bufSize = %d, but params = NULL)",
140 bufSize);
141 }
142
143 /* Copy the data from the temporary buffer to the buffer supplied by the
144 * application. Clamp the size of the copy to the size supplied by the
145 * application.
146 */
147 memcpy(params, buffer, MIN2(count, bufSize) * sizeof(GLint));
148
149 return;
150 }