2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
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"
37 * Called via glSampleCoverageARB
40 _mesa_SampleCoverage(GLclampf value
, GLboolean invert
)
42 GET_CURRENT_CONTEXT(ctx
);
44 value
= SATURATE(value
);
46 if (ctx
->Multisample
.SampleCoverageInvert
== invert
&&
47 ctx
->Multisample
.SampleCoverageValue
== value
)
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
;
58 * Initialize the context's multisample state.
59 * \param ctx the GL context.
62 _mesa_init_multisample(struct gl_context
*ctx
)
64 ctx
->Multisample
.Enabled
= GL_TRUE
;
65 ctx
->Multisample
.SampleAlphaToCoverage
= GL_FALSE
;
66 ctx
->Multisample
.SampleAlphaToCoverageDitherControl
= GL_ALPHA_TO_COVERAGE_DITHER_DEFAULT_NV
;
67 ctx
->Multisample
.SampleAlphaToOne
= GL_FALSE
;
68 ctx
->Multisample
.SampleCoverage
= GL_FALSE
;
69 ctx
->Multisample
.SampleCoverageValue
= 1.0;
70 ctx
->Multisample
.SampleCoverageInvert
= GL_FALSE
;
71 ctx
->Multisample
.SampleShading
= GL_FALSE
;
72 ctx
->Multisample
.MinSampleShadingValue
= 0.0f
;
74 /* ARB_texture_multisample / GL3.2 additions */
75 ctx
->Multisample
.SampleMask
= GL_FALSE
;
76 ctx
->Multisample
.SampleMaskValue
= ~(GLbitfield
)0;
81 _mesa_GetMultisamplefv(GLenum pname
, GLuint index
, GLfloat
* val
)
83 GET_CURRENT_CONTEXT(ctx
);
85 if (ctx
->NewState
& _NEW_BUFFERS
) {
86 _mesa_update_state(ctx
);
90 case GL_SAMPLE_POSITION
: {
91 if (index
>= ctx
->DrawBuffer
->Visual
.samples
) {
92 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetMultisamplefv(index)" );
96 ctx
->Driver
.GetSamplePosition(ctx
, ctx
->DrawBuffer
, index
, val
);
98 /* FBOs can be upside down (winsys always are)*/
99 if (ctx
->DrawBuffer
->FlipY
)
100 val
[1] = 1.0f
- val
[1];
105 case GL_PROGRAMMABLE_SAMPLE_LOCATION_ARB
:
106 if (!ctx
->Extensions
.ARB_sample_locations
) {
107 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMultisamplefv(pname)" );
111 if (index
>= MAX_SAMPLE_LOCATION_TABLE_SIZE
* 2) {
112 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetMultisamplefv(index)" );
116 if (ctx
->DrawBuffer
->SampleLocationTable
)
117 *val
= ctx
->DrawBuffer
->SampleLocationTable
[index
];
124 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMultisamplefv(pname)" );
130 sample_maski(struct gl_context
*ctx
, GLuint index
, GLbitfield mask
)
132 if (ctx
->Multisample
.SampleMaskValue
== mask
)
135 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewSampleMask
? 0 : _NEW_MULTISAMPLE
);
136 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleMask
;
137 ctx
->Multisample
.SampleMaskValue
= mask
;
141 _mesa_SampleMaski_no_error(GLuint index
, GLbitfield mask
)
143 GET_CURRENT_CONTEXT(ctx
);
144 sample_maski(ctx
, index
, mask
);
148 _mesa_SampleMaski(GLuint index
, GLbitfield mask
)
150 GET_CURRENT_CONTEXT(ctx
);
152 if (!ctx
->Extensions
.ARB_texture_multisample
) {
153 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glSampleMaski");
158 _mesa_error(ctx
, GL_INVALID_VALUE
, "glSampleMaski(index)");
162 sample_maski(ctx
, index
, mask
);
166 min_sample_shading(struct gl_context
*ctx
, GLclampf value
)
168 value
= SATURATE(value
);
170 if (ctx
->Multisample
.MinSampleShadingValue
== value
)
174 ctx
->DriverFlags
.NewSampleShading
? 0 : _NEW_MULTISAMPLE
);
175 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleShading
;
176 ctx
->Multisample
.MinSampleShadingValue
= value
;
180 * Called via glMinSampleShadingARB
183 _mesa_MinSampleShading_no_error(GLclampf value
)
185 GET_CURRENT_CONTEXT(ctx
);
186 min_sample_shading(ctx
, value
);
190 _mesa_MinSampleShading(GLclampf value
)
192 GET_CURRENT_CONTEXT(ctx
);
194 if (!_mesa_has_ARB_sample_shading(ctx
) &&
195 !_mesa_has_OES_sample_shading(ctx
)) {
196 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMinSampleShading");
200 min_sample_shading(ctx
, value
);
204 * Helper for checking a requested sample count against the limit
205 * for a particular (target, internalFormat) pair. The limit imposed,
206 * and the error generated, both depend on which extensions are supported.
208 * Returns a GL error enum, or GL_NO_ERROR if the requested sample count is
212 _mesa_check_sample_count(struct gl_context
*ctx
, GLenum target
,
213 GLenum internalFormat
, GLsizei samples
,
214 GLsizei storageSamples
)
216 /* Section 4.4 (Framebuffer objects), page 198 of the OpenGL ES 3.0.0
217 * specification says:
219 * "If internalformat is a signed or unsigned integer format and samples
220 * is greater than zero, then the error INVALID_OPERATION is generated."
222 * This restriction is relaxed for OpenGL ES 3.1.
224 if ((ctx
->API
== API_OPENGLES2
&& ctx
->Version
== 30) &&
225 _mesa_is_enum_format_integer(internalFormat
)
227 return GL_INVALID_OPERATION
;
230 if (ctx
->Extensions
.AMD_framebuffer_multisample_advanced
&&
231 target
== GL_RENDERBUFFER
) {
232 if (!_mesa_is_depth_or_stencil_format(internalFormat
)) {
233 /* From the AMD_framebuffer_multisample_advanced spec:
235 * "An INVALID_OPERATION error is generated if <internalformat>
236 * is a color format and <storageSamples> is greater than
237 * the implementation-dependent limit MAX_COLOR_FRAMEBUFFER_-
238 * STORAGE_SAMPLES_AMD."
240 if (samples
> ctx
->Const
.MaxColorFramebufferSamples
)
241 return GL_INVALID_OPERATION
;
243 /* From the AMD_framebuffer_multisample_advanced spec:
245 * "An INVALID_OPERATION error is generated if <internalformat>
246 * is a color format and <storageSamples> is greater than
247 * the implementation-dependent limit MAX_COLOR_FRAMEBUFFER_-
248 * STORAGE_SAMPLES_AMD."
250 if (storageSamples
> ctx
->Const
.MaxColorFramebufferStorageSamples
)
251 return GL_INVALID_OPERATION
;
253 /* From the AMD_framebuffer_multisample_advanced spec:
255 * "An INVALID_OPERATION error is generated if <storageSamples> is
256 * greater than <samples>."
258 if (storageSamples
> samples
)
259 return GL_INVALID_OPERATION
;
261 /* Color renderbuffer sample counts are now fully validated
262 * according to AMD_framebuffer_multisample_advanced.
266 /* From the AMD_framebuffer_multisample_advanced spec:
268 * "An INVALID_OPERATION error is generated if <internalformat> is
269 * a depth or stencil format and <storageSamples> is not equal to
272 if (storageSamples
!= samples
)
273 return GL_INVALID_OPERATION
;
276 /* If the extension is unsupported, it's not possible to set
277 * storageSamples differently.
279 assert(samples
== storageSamples
);
282 /* If ARB_internalformat_query is supported, then treat its highest
283 * returned sample count as the absolute maximum for this format; it is
284 * allowed to exceed MAX_SAMPLES.
286 * From the ARB_internalformat_query spec:
288 * "If <samples is greater than the maximum number of samples supported
289 * for <internalformat> then the error INVALID_OPERATION is generated."
291 if (ctx
->Extensions
.ARB_internalformat_query
) {
292 GLint buffer
[16] = {-1};
295 ctx
->Driver
.QueryInternalFormat(ctx
, target
, internalFormat
,
297 /* since the query returns samples sorted in descending order,
298 * the first element is the greatest supported sample value.
302 return samples
> limit
? GL_INVALID_OPERATION
: GL_NO_ERROR
;
305 /* If ARB_texture_multisample is supported, we have separate limits,
306 * which may be lower than MAX_SAMPLES:
308 * From the ARB_texture_multisample spec, when describing the operation
309 * of RenderbufferStorageMultisample:
311 * "If <internalformat> is a signed or unsigned integer format and
312 * <samples> is greater than the value of MAX_INTEGER_SAMPLES, then the
313 * error INVALID_OPERATION is generated"
315 * And when describing the operation of TexImage*Multisample:
317 * "The error INVALID_OPERATION may be generated if any of the following
320 * * <internalformat> is a depth/stencil-renderable format and <samples>
321 * is greater than the value of MAX_DEPTH_TEXTURE_SAMPLES
322 * * <internalformat> is a color-renderable format and <samples> is
323 * grater than the value of MAX_COLOR_TEXTURE_SAMPLES
324 * * <internalformat> is a signed or unsigned integer format and
325 * <samples> is greater than the value of MAX_INTEGER_SAMPLES
328 if (ctx
->Extensions
.ARB_texture_multisample
) {
329 if (_mesa_is_enum_format_integer(internalFormat
))
330 return samples
> ctx
->Const
.MaxIntegerSamples
331 ? GL_INVALID_OPERATION
: GL_NO_ERROR
;
333 if (target
== GL_TEXTURE_2D_MULTISAMPLE
||
334 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
336 if (_mesa_is_depth_or_stencil_format(internalFormat
))
337 return samples
> ctx
->Const
.MaxDepthTextureSamples
338 ? GL_INVALID_OPERATION
: GL_NO_ERROR
;
340 return samples
> ctx
->Const
.MaxColorTextureSamples
341 ? GL_INVALID_OPERATION
: GL_NO_ERROR
;
345 /* No more specific limit is available, so just use MAX_SAMPLES:
347 * On p205 of the GL3.1 spec:
349 * "... or if samples is greater than MAX_SAMPLES, then the error
350 * INVALID_VALUE is generated"
352 return (GLuint
) samples
> ctx
->Const
.MaxSamples
353 ? GL_INVALID_VALUE
: GL_NO_ERROR
;
357 _mesa_AlphaToCoverageDitherControlNV_no_error(GLenum mode
)
359 GET_CURRENT_CONTEXT(ctx
);
361 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewSampleAlphaToXEnable
? 0 :
363 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleAlphaToXEnable
;
364 ctx
->Multisample
.SampleAlphaToCoverageDitherControl
= mode
;
368 _mesa_AlphaToCoverageDitherControlNV(GLenum mode
)
370 GET_CURRENT_CONTEXT(ctx
);
372 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewSampleAlphaToXEnable
? 0 :
374 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleAlphaToXEnable
;
376 case GL_ALPHA_TO_COVERAGE_DITHER_DEFAULT_NV
:
377 case GL_ALPHA_TO_COVERAGE_DITHER_ENABLE_NV
:
378 case GL_ALPHA_TO_COVERAGE_DITHER_DISABLE_NV
:
379 ctx
->Multisample
.SampleAlphaToCoverageDitherControl
= mode
;
382 _mesa_error(ctx
, GL_INVALID_ENUM
, "glAlphaToCoverageDitherControlNV(invalid parameter)");