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