multisample.c revision 01e04c3f
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 */
39void 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 */
61void
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
79void 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 (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      /* FBOs can be upside down (winsys always are)*/
98      if (ctx->DrawBuffer->FlipY)
99         val[1] = 1.0f - val[1];
100
101      return;
102   }
103
104   case GL_PROGRAMMABLE_SAMPLE_LOCATION_ARB:
105      if (!ctx->Extensions.ARB_sample_locations) {
106         _mesa_error( ctx, GL_INVALID_ENUM, "glGetMultisamplefv(pname)" );
107         return;
108      }
109
110      if (index >= MAX_SAMPLE_LOCATION_TABLE_SIZE * 2) {
111         _mesa_error( ctx, GL_INVALID_VALUE, "glGetMultisamplefv(index)" );
112         return;
113      }
114
115      if (ctx->DrawBuffer->SampleLocationTable)
116         *val = ctx->DrawBuffer->SampleLocationTable[index];
117      else
118         *val = 0.5f;
119
120      return;
121
122   default:
123      _mesa_error( ctx, GL_INVALID_ENUM, "glGetMultisamplefv(pname)" );
124      return;
125   }
126}
127
128static void
129sample_maski(struct gl_context *ctx, GLuint index, GLbitfield mask)
130{
131   if (ctx->Multisample.SampleMaskValue == mask)
132      return;
133
134   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewSampleMask ? 0 : _NEW_MULTISAMPLE);
135   ctx->NewDriverState |= ctx->DriverFlags.NewSampleMask;
136   ctx->Multisample.SampleMaskValue = mask;
137}
138
139void GLAPIENTRY
140_mesa_SampleMaski_no_error(GLuint index, GLbitfield mask)
141{
142   GET_CURRENT_CONTEXT(ctx);
143   sample_maski(ctx, index, mask);
144}
145
146void GLAPIENTRY
147_mesa_SampleMaski(GLuint index, GLbitfield mask)
148{
149   GET_CURRENT_CONTEXT(ctx);
150
151   if (!ctx->Extensions.ARB_texture_multisample) {
152      _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleMaski");
153      return;
154   }
155
156   if (index != 0) {
157      _mesa_error(ctx, GL_INVALID_VALUE, "glSampleMaski(index)");
158      return;
159   }
160
161   sample_maski(ctx, index, mask);
162}
163
164static void
165min_sample_shading(struct gl_context *ctx, GLclampf value)
166{
167   value = CLAMP(value, 0.0f, 1.0f);
168
169   if (ctx->Multisample.MinSampleShadingValue == value)
170      return;
171
172   FLUSH_VERTICES(ctx,
173                  ctx->DriverFlags.NewSampleShading ? 0 : _NEW_MULTISAMPLE);
174   ctx->NewDriverState |= ctx->DriverFlags.NewSampleShading;
175   ctx->Multisample.MinSampleShadingValue = value;
176}
177
178/**
179 * Called via glMinSampleShadingARB
180 */
181void GLAPIENTRY
182_mesa_MinSampleShading_no_error(GLclampf value)
183{
184   GET_CURRENT_CONTEXT(ctx);
185   min_sample_shading(ctx, value);
186}
187
188void GLAPIENTRY
189_mesa_MinSampleShading(GLclampf value)
190{
191   GET_CURRENT_CONTEXT(ctx);
192
193   if (!_mesa_has_ARB_sample_shading(ctx) &&
194       !_mesa_has_OES_sample_shading(ctx)) {
195      _mesa_error(ctx, GL_INVALID_OPERATION, "glMinSampleShading");
196      return;
197   }
198
199   min_sample_shading(ctx, value);
200}
201
202/**
203 * Helper for checking a requested sample count against the limit
204 * for a particular (target, internalFormat) pair. The limit imposed,
205 * and the error generated, both depend on which extensions are supported.
206 *
207 * Returns a GL error enum, or GL_NO_ERROR if the requested sample count is
208 * acceptable.
209 */
210GLenum
211_mesa_check_sample_count(struct gl_context *ctx, GLenum target,
212                         GLenum internalFormat, GLsizei samples,
213                         GLsizei storageSamples)
214{
215   /* Section 4.4 (Framebuffer objects), page 198 of the OpenGL ES 3.0.0
216    * specification says:
217    *
218    *     "If internalformat is a signed or unsigned integer format and samples
219    *     is greater than zero, then the error INVALID_OPERATION is generated."
220    *
221    * This restriction is relaxed for OpenGL ES 3.1.
222    */
223   if ((ctx->API == API_OPENGLES2 && ctx->Version == 30) &&
224       _mesa_is_enum_format_integer(internalFormat)
225       && samples > 0) {
226      return GL_INVALID_OPERATION;
227   }
228
229   if (ctx->Extensions.AMD_framebuffer_multisample_advanced &&
230       target == GL_RENDERBUFFER) {
231      if (!_mesa_is_depth_or_stencil_format(internalFormat)) {
232         /* From the AMD_framebuffer_multisample_advanced spec:
233          *
234          *    "An INVALID_OPERATION error is generated if <internalformat>
235          *     is a color format and <storageSamples> is greater than
236          *     the implementation-dependent limit MAX_COLOR_FRAMEBUFFER_-
237          *     STORAGE_SAMPLES_AMD."
238          */
239         if (samples > ctx->Const.MaxColorFramebufferSamples)
240            return GL_INVALID_OPERATION;
241
242         /* From the AMD_framebuffer_multisample_advanced spec:
243          *
244          *    "An INVALID_OPERATION error is generated if <internalformat>
245          *     is a color format and <storageSamples> is greater than
246          *     the implementation-dependent limit MAX_COLOR_FRAMEBUFFER_-
247          *     STORAGE_SAMPLES_AMD."
248          */
249         if (storageSamples > ctx->Const.MaxColorFramebufferStorageSamples)
250            return GL_INVALID_OPERATION;
251
252         /* From the AMD_framebuffer_multisample_advanced spec:
253          *
254          *    "An INVALID_OPERATION error is generated if <storageSamples> is
255          *     greater than <samples>."
256          */
257         if (storageSamples > samples)
258            return GL_INVALID_OPERATION;
259
260         /* Color renderbuffer sample counts are now fully validated
261          * according to AMD_framebuffer_multisample_advanced.
262          */
263         return GL_NO_ERROR;
264      } else {
265         /* From the AMD_framebuffer_multisample_advanced spec:
266          *
267          *    "An INVALID_OPERATION error is generated if <internalformat> is
268          *     a depth or stencil format and <storageSamples> is not equal to
269          *     <samples>."
270          */
271         if (storageSamples != samples)
272            return GL_INVALID_OPERATION;
273      }
274   } else {
275      /* If the extension is unsupported, it's not possible to set
276       * storageSamples differently.
277       */
278      assert(samples == storageSamples);
279   }
280
281   /* If ARB_internalformat_query is supported, then treat its highest
282    * returned sample count as the absolute maximum for this format; it is
283    * allowed to exceed MAX_SAMPLES.
284    *
285    * From the ARB_internalformat_query spec:
286    *
287    * "If <samples is greater than the maximum number of samples supported
288    * for <internalformat> then the error INVALID_OPERATION is generated."
289    */
290   if (ctx->Extensions.ARB_internalformat_query) {
291      GLint buffer[16] = {-1};
292      GLint limit;
293
294      ctx->Driver.QueryInternalFormat(ctx, target, internalFormat,
295                                      GL_SAMPLES, buffer);
296      /* since the query returns samples sorted in descending order,
297       * the first element is the greatest supported sample value.
298       */
299      limit = buffer[0];
300
301      return samples > limit ? GL_INVALID_OPERATION : GL_NO_ERROR;
302   }
303
304   /* If ARB_texture_multisample is supported, we have separate limits,
305    * which may be lower than MAX_SAMPLES:
306    *
307    * From the ARB_texture_multisample spec, when describing the operation
308    * of RenderbufferStorageMultisample:
309    *
310    * "If <internalformat> is a signed or unsigned integer format and
311    * <samples> is greater than the value of MAX_INTEGER_SAMPLES, then the
312    * error INVALID_OPERATION is generated"
313    *
314    * And when describing the operation of TexImage*Multisample:
315    *
316    * "The error INVALID_OPERATION may be generated if any of the following
317    * are true:
318    *
319    * * <internalformat> is a depth/stencil-renderable format and <samples>
320    *   is greater than the value of MAX_DEPTH_TEXTURE_SAMPLES
321    * * <internalformat> is a color-renderable format and <samples> is
322    *   grater than the value of MAX_COLOR_TEXTURE_SAMPLES
323    * * <internalformat> is a signed or unsigned integer format and
324    *   <samples> is greater than the value of MAX_INTEGER_SAMPLES
325    */
326
327   if (ctx->Extensions.ARB_texture_multisample) {
328      if (_mesa_is_enum_format_integer(internalFormat))
329         return samples > ctx->Const.MaxIntegerSamples
330            ? GL_INVALID_OPERATION : GL_NO_ERROR;
331
332      if (target == GL_TEXTURE_2D_MULTISAMPLE ||
333          target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
334
335         if (_mesa_is_depth_or_stencil_format(internalFormat))
336            return samples > ctx->Const.MaxDepthTextureSamples
337               ? GL_INVALID_OPERATION : GL_NO_ERROR;
338         else
339            return samples > ctx->Const.MaxColorTextureSamples
340               ? GL_INVALID_OPERATION : GL_NO_ERROR;
341      }
342   }
343
344   /* No more specific limit is available, so just use MAX_SAMPLES:
345    *
346    * On p205 of the GL3.1 spec:
347    *
348    * "... or if samples is greater than MAX_SAMPLES, then the error
349    * INVALID_VALUE is generated"
350    */
351   return (GLuint) samples > ctx->Const.MaxSamples
352      ? GL_INVALID_VALUE : GL_NO_ERROR;
353}
354