mesa: add support for memory object creation/import/delete
[mesa.git] / src / mesa / main / multisample.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/macros.h"
29 #include "main/multisample.h"
30 #include "main/mtypes.h"
31 #include "main/fbobject.h"
32 #include "main/glformats.h"
33 #include "main/state.h"
34
35
36 /**
37 * Called via glSampleCoverageARB
38 */
39 void GLAPIENTRY
40 _mesa_SampleCoverage(GLclampf value, GLboolean invert)
41 {
42 GET_CURRENT_CONTEXT(ctx);
43
44 value = CLAMP(value, 0.0f, 1.0f);
45
46 if (ctx->Multisample.SampleCoverageInvert == invert &&
47 ctx->Multisample.SampleCoverageValue == value)
48 return;
49
50 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewSampleMask ? 0 : _NEW_MULTISAMPLE);
51 ctx->NewDriverState |= ctx->DriverFlags.NewSampleMask;
52 ctx->Multisample.SampleCoverageValue = value;
53 ctx->Multisample.SampleCoverageInvert = invert;
54 }
55
56
57 /**
58 * Initialize the context's multisample state.
59 * \param ctx the GL context.
60 */
61 void
62 _mesa_init_multisample(struct gl_context *ctx)
63 {
64 ctx->Multisample.Enabled = GL_TRUE;
65 ctx->Multisample.SampleAlphaToCoverage = GL_FALSE;
66 ctx->Multisample.SampleAlphaToOne = GL_FALSE;
67 ctx->Multisample.SampleCoverage = GL_FALSE;
68 ctx->Multisample.SampleCoverageValue = 1.0;
69 ctx->Multisample.SampleCoverageInvert = GL_FALSE;
70 ctx->Multisample.SampleShading = GL_FALSE;
71 ctx->Multisample.MinSampleShadingValue = 0.0f;
72
73 /* ARB_texture_multisample / GL3.2 additions */
74 ctx->Multisample.SampleMask = GL_FALSE;
75 ctx->Multisample.SampleMaskValue = ~(GLbitfield)0;
76 }
77
78
79 void GLAPIENTRY
80 _mesa_GetMultisamplefv(GLenum pname, GLuint index, GLfloat * val)
81 {
82 GET_CURRENT_CONTEXT(ctx);
83
84 if (ctx->NewState & _NEW_BUFFERS) {
85 _mesa_update_state(ctx);
86 }
87
88 switch (pname) {
89 case GL_SAMPLE_POSITION: {
90 if ((int) index >= ctx->DrawBuffer->Visual.samples) {
91 _mesa_error( ctx, GL_INVALID_VALUE, "glGetMultisamplefv(index)" );
92 return;
93 }
94
95 ctx->Driver.GetSamplePosition(ctx, ctx->DrawBuffer, index, val);
96
97 /* winsys FBOs are upside down */
98 if (_mesa_is_winsys_fbo(ctx->DrawBuffer))
99 val[1] = 1.0f - val[1];
100
101 return;
102 }
103
104 default:
105 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMultisamplefv(pname)" );
106 return;
107 }
108 }
109
110 static void
111 sample_maski(struct gl_context *ctx, GLuint index, GLbitfield mask)
112 {
113 if (ctx->Multisample.SampleMaskValue == mask)
114 return;
115
116 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewSampleMask ? 0 : _NEW_MULTISAMPLE);
117 ctx->NewDriverState |= ctx->DriverFlags.NewSampleMask;
118 ctx->Multisample.SampleMaskValue = mask;
119 }
120
121 void GLAPIENTRY
122 _mesa_SampleMaski_no_error(GLuint index, GLbitfield mask)
123 {
124 GET_CURRENT_CONTEXT(ctx);
125 sample_maski(ctx, index, mask);
126 }
127
128 void GLAPIENTRY
129 _mesa_SampleMaski(GLuint index, GLbitfield mask)
130 {
131 GET_CURRENT_CONTEXT(ctx);
132
133 if (!ctx->Extensions.ARB_texture_multisample) {
134 _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleMaski");
135 return;
136 }
137
138 if (index != 0) {
139 _mesa_error(ctx, GL_INVALID_VALUE, "glSampleMaski(index)");
140 return;
141 }
142
143 sample_maski(ctx, index, mask);
144 }
145
146 static void
147 min_sample_shading(struct gl_context *ctx, GLclampf value)
148 {
149 value = CLAMP(value, 0.0f, 1.0f);
150
151 if (ctx->Multisample.MinSampleShadingValue == value)
152 return;
153
154 FLUSH_VERTICES(ctx,
155 ctx->DriverFlags.NewSampleShading ? 0 : _NEW_MULTISAMPLE);
156 ctx->NewDriverState |= ctx->DriverFlags.NewSampleShading;
157 ctx->Multisample.MinSampleShadingValue = value;
158 }
159
160 /**
161 * Called via glMinSampleShadingARB
162 */
163 void GLAPIENTRY
164 _mesa_MinSampleShading_no_error(GLclampf value)
165 {
166 GET_CURRENT_CONTEXT(ctx);
167 min_sample_shading(ctx, value);
168 }
169
170 void GLAPIENTRY
171 _mesa_MinSampleShading(GLclampf value)
172 {
173 GET_CURRENT_CONTEXT(ctx);
174
175 if (!_mesa_has_ARB_sample_shading(ctx) &&
176 !_mesa_has_OES_sample_shading(ctx)) {
177 _mesa_error(ctx, GL_INVALID_OPERATION, "glMinSampleShading");
178 return;
179 }
180
181 min_sample_shading(ctx, value);
182 }
183
184 /**
185 * Helper for checking a requested sample count against the limit
186 * for a particular (target, internalFormat) pair. The limit imposed,
187 * and the error generated, both depend on which extensions are supported.
188 *
189 * Returns a GL error enum, or GL_NO_ERROR if the requested sample count is
190 * acceptable.
191 */
192 GLenum
193 _mesa_check_sample_count(struct gl_context *ctx, GLenum target,
194 GLenum internalFormat, GLsizei samples)
195 {
196 /* Section 4.4 (Framebuffer objects), page 198 of the OpenGL ES 3.0.0
197 * specification says:
198 *
199 * "If internalformat is a signed or unsigned integer format and samples
200 * is greater than zero, then the error INVALID_OPERATION is generated."
201 *
202 * This restriction is relaxed for OpenGL ES 3.1.
203 */
204 if ((ctx->API == API_OPENGLES2 && ctx->Version == 30) &&
205 _mesa_is_enum_format_integer(internalFormat)
206 && samples > 0) {
207 return GL_INVALID_OPERATION;
208 }
209
210 /* If ARB_internalformat_query is supported, then treat its highest
211 * returned sample count as the absolute maximum for this format; it is
212 * allowed to exceed MAX_SAMPLES.
213 *
214 * From the ARB_internalformat_query spec:
215 *
216 * "If <samples is greater than the maximum number of samples supported
217 * for <internalformat> then the error INVALID_OPERATION is generated."
218 */
219 if (ctx->Extensions.ARB_internalformat_query) {
220 GLint buffer[16] = {-1};
221 GLint limit;
222
223 ctx->Driver.QueryInternalFormat(ctx, target, internalFormat,
224 GL_SAMPLES, buffer);
225 /* since the query returns samples sorted in descending order,
226 * the first element is the greatest supported sample value.
227 */
228 limit = buffer[0];
229
230 return samples > limit ? GL_INVALID_OPERATION : GL_NO_ERROR;
231 }
232
233 /* If ARB_texture_multisample is supported, we have separate limits,
234 * which may be lower than MAX_SAMPLES:
235 *
236 * From the ARB_texture_multisample spec, when describing the operation
237 * of RenderbufferStorageMultisample:
238 *
239 * "If <internalformat> is a signed or unsigned integer format and
240 * <samples> is greater than the value of MAX_INTEGER_SAMPLES, then the
241 * error INVALID_OPERATION is generated"
242 *
243 * And when describing the operation of TexImage*Multisample:
244 *
245 * "The error INVALID_OPERATION may be generated if any of the following
246 * are true:
247 *
248 * * <internalformat> is a depth/stencil-renderable format and <samples>
249 * is greater than the value of MAX_DEPTH_TEXTURE_SAMPLES
250 * * <internalformat> is a color-renderable format and <samples> is
251 * grater than the value of MAX_COLOR_TEXTURE_SAMPLES
252 * * <internalformat> is a signed or unsigned integer format and
253 * <samples> is greater than the value of MAX_INTEGER_SAMPLES
254 */
255
256 if (ctx->Extensions.ARB_texture_multisample) {
257 if (_mesa_is_enum_format_integer(internalFormat))
258 return samples > ctx->Const.MaxIntegerSamples
259 ? GL_INVALID_OPERATION : GL_NO_ERROR;
260
261 if (target == GL_TEXTURE_2D_MULTISAMPLE ||
262 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
263
264 if (_mesa_is_depth_or_stencil_format(internalFormat))
265 return samples > ctx->Const.MaxDepthTextureSamples
266 ? GL_INVALID_OPERATION : GL_NO_ERROR;
267 else
268 return samples > ctx->Const.MaxColorTextureSamples
269 ? GL_INVALID_OPERATION : GL_NO_ERROR;
270 }
271 }
272
273 /* No more specific limit is available, so just use MAX_SAMPLES:
274 *
275 * On p205 of the GL3.1 spec:
276 *
277 * "... or if samples is greater than MAX_SAMPLES, then the error
278 * INVALID_VALUE is generated"
279 */
280 return (GLuint) samples > ctx->Const.MaxSamples
281 ? GL_INVALID_VALUE : GL_NO_ERROR;
282 }