mesa: add sample_maski() helper
[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
71 /* ARB_texture_multisample / GL3.2 additions */
72 ctx->Multisample.SampleMask = GL_FALSE;
73 ctx->Multisample.SampleMaskValue = ~(GLbitfield)0;
74 }
75
76
77 void GLAPIENTRY
78 _mesa_GetMultisamplefv(GLenum pname, GLuint index, GLfloat * val)
79 {
80 GET_CURRENT_CONTEXT(ctx);
81
82 if (ctx->NewState & _NEW_BUFFERS) {
83 _mesa_update_state(ctx);
84 }
85
86 switch (pname) {
87 case GL_SAMPLE_POSITION: {
88 if ((int) index >= ctx->DrawBuffer->Visual.samples) {
89 _mesa_error( ctx, GL_INVALID_VALUE, "glGetMultisamplefv(index)" );
90 return;
91 }
92
93 ctx->Driver.GetSamplePosition(ctx, ctx->DrawBuffer, index, val);
94
95 /* winsys FBOs are upside down */
96 if (_mesa_is_winsys_fbo(ctx->DrawBuffer))
97 val[1] = 1.0f - val[1];
98
99 return;
100 }
101
102 default:
103 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMultisamplefv(pname)" );
104 return;
105 }
106 }
107
108 static void
109 sample_maski(struct gl_context *ctx, GLuint index, GLbitfield mask)
110 {
111 if (ctx->Multisample.SampleMaskValue == mask)
112 return;
113
114 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewSampleMask ? 0 : _NEW_MULTISAMPLE);
115 ctx->NewDriverState |= ctx->DriverFlags.NewSampleMask;
116 ctx->Multisample.SampleMaskValue = mask;
117 }
118
119 void GLAPIENTRY
120 _mesa_SampleMaski(GLuint index, GLbitfield mask)
121 {
122 GET_CURRENT_CONTEXT(ctx);
123
124 if (!ctx->Extensions.ARB_texture_multisample) {
125 _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleMaski");
126 return;
127 }
128
129 if (index != 0) {
130 _mesa_error(ctx, GL_INVALID_VALUE, "glSampleMaski(index)");
131 return;
132 }
133
134 sample_maski(ctx, index, mask);
135 }
136
137 /**
138 * Called via glMinSampleShadingARB
139 */
140 void GLAPIENTRY
141 _mesa_MinSampleShading(GLclampf value)
142 {
143 GET_CURRENT_CONTEXT(ctx);
144
145 if (!_mesa_has_ARB_sample_shading(ctx) &&
146 !_mesa_has_OES_sample_shading(ctx)) {
147 _mesa_error(ctx, GL_INVALID_OPERATION, "glMinSampleShading");
148 return;
149 }
150
151 value = CLAMP(value, 0.0f, 1.0f);
152
153 if (ctx->Multisample.MinSampleShadingValue == value)
154 return;
155
156 FLUSH_VERTICES(ctx,
157 ctx->DriverFlags.NewSampleShading ? 0 : _NEW_MULTISAMPLE);
158 ctx->NewDriverState |= ctx->DriverFlags.NewSampleShading;
159 ctx->Multisample.MinSampleShadingValue = value;
160 }
161
162 /**
163 * Helper for checking a requested sample count against the limit
164 * for a particular (target, internalFormat) pair. The limit imposed,
165 * and the error generated, both depend on which extensions are supported.
166 *
167 * Returns a GL error enum, or GL_NO_ERROR if the requested sample count is
168 * acceptable.
169 */
170 GLenum
171 _mesa_check_sample_count(struct gl_context *ctx, GLenum target,
172 GLenum internalFormat, GLsizei samples)
173 {
174 /* Section 4.4 (Framebuffer objects), page 198 of the OpenGL ES 3.0.0
175 * specification says:
176 *
177 * "If internalformat is a signed or unsigned integer format and samples
178 * is greater than zero, then the error INVALID_OPERATION is generated."
179 *
180 * This restriction is relaxed for OpenGL ES 3.1.
181 */
182 if ((ctx->API == API_OPENGLES2 && ctx->Version == 30) &&
183 _mesa_is_enum_format_integer(internalFormat)
184 && samples > 0) {
185 return GL_INVALID_OPERATION;
186 }
187
188 /* If ARB_internalformat_query is supported, then treat its highest
189 * returned sample count as the absolute maximum for this format; it is
190 * allowed to exceed MAX_SAMPLES.
191 *
192 * From the ARB_internalformat_query spec:
193 *
194 * "If <samples is greater than the maximum number of samples supported
195 * for <internalformat> then the error INVALID_OPERATION is generated."
196 */
197 if (ctx->Extensions.ARB_internalformat_query) {
198 GLint buffer[16] = {-1};
199 GLint limit;
200
201 ctx->Driver.QueryInternalFormat(ctx, target, internalFormat,
202 GL_SAMPLES, buffer);
203 /* since the query returns samples sorted in descending order,
204 * the first element is the greatest supported sample value.
205 */
206 limit = buffer[0];
207
208 return samples > limit ? GL_INVALID_OPERATION : GL_NO_ERROR;
209 }
210
211 /* If ARB_texture_multisample is supported, we have separate limits,
212 * which may be lower than MAX_SAMPLES:
213 *
214 * From the ARB_texture_multisample spec, when describing the operation
215 * of RenderbufferStorageMultisample:
216 *
217 * "If <internalformat> is a signed or unsigned integer format and
218 * <samples> is greater than the value of MAX_INTEGER_SAMPLES, then the
219 * error INVALID_OPERATION is generated"
220 *
221 * And when describing the operation of TexImage*Multisample:
222 *
223 * "The error INVALID_OPERATION may be generated if any of the following
224 * are true:
225 *
226 * * <internalformat> is a depth/stencil-renderable format and <samples>
227 * is greater than the value of MAX_DEPTH_TEXTURE_SAMPLES
228 * * <internalformat> is a color-renderable format and <samples> is
229 * grater than the value of MAX_COLOR_TEXTURE_SAMPLES
230 * * <internalformat> is a signed or unsigned integer format and
231 * <samples> is greater than the value of MAX_INTEGER_SAMPLES
232 */
233
234 if (ctx->Extensions.ARB_texture_multisample) {
235 if (_mesa_is_enum_format_integer(internalFormat))
236 return samples > ctx->Const.MaxIntegerSamples
237 ? GL_INVALID_OPERATION : GL_NO_ERROR;
238
239 if (target == GL_TEXTURE_2D_MULTISAMPLE ||
240 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
241
242 if (_mesa_is_depth_or_stencil_format(internalFormat))
243 return samples > ctx->Const.MaxDepthTextureSamples
244 ? GL_INVALID_OPERATION : GL_NO_ERROR;
245 else
246 return samples > ctx->Const.MaxColorTextureSamples
247 ? GL_INVALID_OPERATION : GL_NO_ERROR;
248 }
249 }
250
251 /* No more specific limit is available, so just use MAX_SAMPLES:
252 *
253 * On p205 of the GL3.1 spec:
254 *
255 * "... or if samples is greater than MAX_SAMPLES, then the error
256 * INVALID_VALUE is generated"
257 */
258 return (GLuint) samples > ctx->Const.MaxSamples
259 ? GL_INVALID_VALUE : GL_NO_ERROR;
260 }