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
= CLAMP(value
, 0.0f
, 1.0f
);
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
.SampleAlphaToOne
= GL_FALSE
;
67 ctx
->Multisample
.SampleCoverage
= GL_FALSE
;
68 ctx
->Multisample
.SampleCoverageValue
= 1.0;
69 ctx
->Multisample
.SampleCoverageInvert
= GL_FALSE
;
71 /* ARB_texture_multisample / GL3.2 additions */
72 ctx
->Multisample
.SampleMask
= GL_FALSE
;
73 ctx
->Multisample
.SampleMaskValue
= ~(GLbitfield
)0;
78 _mesa_GetMultisamplefv(GLenum pname
, GLuint index
, GLfloat
* val
)
80 GET_CURRENT_CONTEXT(ctx
);
82 if (ctx
->NewState
& _NEW_BUFFERS
) {
83 _mesa_update_state(ctx
);
87 case GL_SAMPLE_POSITION
: {
88 if ((int) index
>= ctx
->DrawBuffer
->Visual
.samples
) {
89 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetMultisamplefv(index)" );
93 ctx
->Driver
.GetSamplePosition(ctx
, ctx
->DrawBuffer
, index
, val
);
95 /* winsys FBOs are upside down */
96 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
))
97 val
[1] = 1.0f
- val
[1];
103 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetMultisamplefv(pname)" );
109 _mesa_SampleMaski(GLuint index
, GLbitfield mask
)
111 GET_CURRENT_CONTEXT(ctx
);
113 if (!ctx
->Extensions
.ARB_texture_multisample
) {
114 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glSampleMaski");
119 _mesa_error(ctx
, GL_INVALID_VALUE
, "glSampleMaski(index)");
123 if (ctx
->Multisample
.SampleMaskValue
== mask
)
126 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewSampleMask
? 0 : _NEW_MULTISAMPLE
);
127 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleMask
;
128 ctx
->Multisample
.SampleMaskValue
= mask
;
132 * Called via glMinSampleShadingARB
135 _mesa_MinSampleShading(GLclampf value
)
137 GET_CURRENT_CONTEXT(ctx
);
139 if (!_mesa_has_ARB_sample_shading(ctx
) &&
140 !_mesa_has_OES_sample_shading(ctx
)) {
141 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glMinSampleShading");
145 value
= CLAMP(value
, 0.0f
, 1.0f
);
147 if (ctx
->Multisample
.MinSampleShadingValue
== value
)
151 ctx
->DriverFlags
.NewSampleShading
? 0 : _NEW_MULTISAMPLE
);
152 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewSampleShading
;
153 ctx
->Multisample
.MinSampleShadingValue
= value
;
157 * Helper for checking a requested sample count against the limit
158 * for a particular (target, internalFormat) pair. The limit imposed,
159 * and the error generated, both depend on which extensions are supported.
161 * Returns a GL error enum, or GL_NO_ERROR if the requested sample count is
165 _mesa_check_sample_count(struct gl_context
*ctx
, GLenum target
,
166 GLenum internalFormat
, GLsizei samples
)
168 /* Section 4.4 (Framebuffer objects), page 198 of the OpenGL ES 3.0.0
169 * specification says:
171 * "If internalformat is a signed or unsigned integer format and samples
172 * is greater than zero, then the error INVALID_OPERATION is generated."
174 * This restriction is relaxed for OpenGL ES 3.1.
176 if ((ctx
->API
== API_OPENGLES2
&& ctx
->Version
== 30) &&
177 _mesa_is_enum_format_integer(internalFormat
)
179 return GL_INVALID_OPERATION
;
182 /* If ARB_internalformat_query is supported, then treat its highest
183 * returned sample count as the absolute maximum for this format; it is
184 * allowed to exceed MAX_SAMPLES.
186 * From the ARB_internalformat_query spec:
188 * "If <samples is greater than the maximum number of samples supported
189 * for <internalformat> then the error INVALID_OPERATION is generated."
191 if (ctx
->Extensions
.ARB_internalformat_query
) {
192 GLint buffer
[16] = {-1};
195 ctx
->Driver
.QueryInternalFormat(ctx
, target
, internalFormat
,
197 /* since the query returns samples sorted in descending order,
198 * the first element is the greatest supported sample value.
202 return samples
> limit
? GL_INVALID_OPERATION
: GL_NO_ERROR
;
205 /* If ARB_texture_multisample is supported, we have separate limits,
206 * which may be lower than MAX_SAMPLES:
208 * From the ARB_texture_multisample spec, when describing the operation
209 * of RenderbufferStorageMultisample:
211 * "If <internalformat> is a signed or unsigned integer format and
212 * <samples> is greater than the value of MAX_INTEGER_SAMPLES, then the
213 * error INVALID_OPERATION is generated"
215 * And when describing the operation of TexImage*Multisample:
217 * "The error INVALID_OPERATION may be generated if any of the following
220 * * <internalformat> is a depth/stencil-renderable format and <samples>
221 * is greater than the value of MAX_DEPTH_TEXTURE_SAMPLES
222 * * <internalformat> is a color-renderable format and <samples> is
223 * grater than the value of MAX_COLOR_TEXTURE_SAMPLES
224 * * <internalformat> is a signed or unsigned integer format and
225 * <samples> is greater than the value of MAX_INTEGER_SAMPLES
228 if (ctx
->Extensions
.ARB_texture_multisample
) {
229 if (_mesa_is_enum_format_integer(internalFormat
))
230 return samples
> ctx
->Const
.MaxIntegerSamples
231 ? GL_INVALID_OPERATION
: GL_NO_ERROR
;
233 if (target
== GL_TEXTURE_2D_MULTISAMPLE
||
234 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
236 if (_mesa_is_depth_or_stencil_format(internalFormat
))
237 return samples
> ctx
->Const
.MaxDepthTextureSamples
238 ? GL_INVALID_OPERATION
: GL_NO_ERROR
;
240 return samples
> ctx
->Const
.MaxColorTextureSamples
241 ? GL_INVALID_OPERATION
: GL_NO_ERROR
;
245 /* No more specific limit is available, so just use MAX_SAMPLES:
247 * On p205 of the GL3.1 spec:
249 * "... or if samples is greater than MAX_SAMPLES, then the error
250 * INVALID_VALUE is generated"
252 return (GLuint
) samples
> ctx
->Const
.MaxSamples
253 ? GL_INVALID_VALUE
: GL_NO_ERROR
;