multisample.c revision 01e04c3f
1c1f859d4Smrg/*
2c1f859d4Smrg * Mesa 3-D graphics library
3c1f859d4Smrg *
4c1f859d4Smrg * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
5c1f859d4Smrg *
6c1f859d4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7c1f859d4Smrg * copy of this software and associated documentation files (the "Software"),
8c1f859d4Smrg * to deal in the Software without restriction, including without limitation
9c1f859d4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10c1f859d4Smrg * and/or sell copies of the Software, and to permit persons to whom the
11c1f859d4Smrg * Software is furnished to do so, subject to the following conditions:
12c1f859d4Smrg *
13c1f859d4Smrg * The above copyright notice and this permission notice shall be included
14c1f859d4Smrg * in all copies or substantial portions of the Software.
15c1f859d4Smrg *
16c1f859d4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17c1f859d4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18c1f859d4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE.
23c1f859d4Smrg */
24c1f859d4Smrg
25c1f859d4Smrg
26c1f859d4Smrg#include "main/glheader.h"
27c1f859d4Smrg#include "main/context.h"
28c1f859d4Smrg#include "main/macros.h"
29c1f859d4Smrg#include "main/multisample.h"
303464ebd5Sriastradh#include "main/mtypes.h"
31af69d88dSmrg#include "main/fbobject.h"
32af69d88dSmrg#include "main/glformats.h"
33af69d88dSmrg#include "main/state.h"
34c1f859d4Smrg
35c1f859d4Smrg
36c1f859d4Smrg/**
37c1f859d4Smrg * Called via glSampleCoverageARB
38c1f859d4Smrg */
39c1f859d4Smrgvoid GLAPIENTRY
40af69d88dSmrg_mesa_SampleCoverage(GLclampf value, GLboolean invert)
41c1f859d4Smrg{
42c1f859d4Smrg   GET_CURRENT_CONTEXT(ctx);
43c1f859d4Smrg
4401e04c3fSmrg   value = CLAMP(value, 0.0f, 1.0f);
45c1f859d4Smrg
4601e04c3fSmrg   if (ctx->Multisample.SampleCoverageInvert == invert &&
4701e04c3fSmrg       ctx->Multisample.SampleCoverageValue == value)
4801e04c3fSmrg      return;
4901e04c3fSmrg
5001e04c3fSmrg   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewSampleMask ? 0 : _NEW_MULTISAMPLE);
5101e04c3fSmrg   ctx->NewDriverState |= ctx->DriverFlags.NewSampleMask;
5201e04c3fSmrg   ctx->Multisample.SampleCoverageValue = value;
53c1f859d4Smrg   ctx->Multisample.SampleCoverageInvert = invert;
54c1f859d4Smrg}
55c1f859d4Smrg
56c1f859d4Smrg
57c1f859d4Smrg/**
58c1f859d4Smrg * Initialize the context's multisample state.
59c1f859d4Smrg * \param ctx  the GL context.
60c1f859d4Smrg */
61c1f859d4Smrgvoid
623464ebd5Sriastradh_mesa_init_multisample(struct gl_context *ctx)
63c1f859d4Smrg{
64c1f859d4Smrg   ctx->Multisample.Enabled = GL_TRUE;
65c1f859d4Smrg   ctx->Multisample.SampleAlphaToCoverage = GL_FALSE;
66c1f859d4Smrg   ctx->Multisample.SampleAlphaToOne = GL_FALSE;
67c1f859d4Smrg   ctx->Multisample.SampleCoverage = GL_FALSE;
68c1f859d4Smrg   ctx->Multisample.SampleCoverageValue = 1.0;
69c1f859d4Smrg   ctx->Multisample.SampleCoverageInvert = GL_FALSE;
7001e04c3fSmrg   ctx->Multisample.SampleShading = GL_FALSE;
7101e04c3fSmrg   ctx->Multisample.MinSampleShadingValue = 0.0f;
72af69d88dSmrg
73af69d88dSmrg   /* ARB_texture_multisample / GL3.2 additions */
74af69d88dSmrg   ctx->Multisample.SampleMask = GL_FALSE;
75af69d88dSmrg   ctx->Multisample.SampleMaskValue = ~(GLbitfield)0;
76af69d88dSmrg}
77af69d88dSmrg
78af69d88dSmrg
79af69d88dSmrgvoid GLAPIENTRY
80af69d88dSmrg_mesa_GetMultisamplefv(GLenum pname, GLuint index, GLfloat * val)
81af69d88dSmrg{
82af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
83af69d88dSmrg
84af69d88dSmrg   if (ctx->NewState & _NEW_BUFFERS) {
85af69d88dSmrg      _mesa_update_state(ctx);
86af69d88dSmrg   }
87af69d88dSmrg
88af69d88dSmrg   switch (pname) {
89af69d88dSmrg   case GL_SAMPLE_POSITION: {
9001e04c3fSmrg      if (index >= ctx->DrawBuffer->Visual.samples) {
91af69d88dSmrg         _mesa_error( ctx, GL_INVALID_VALUE, "glGetMultisamplefv(index)" );
92af69d88dSmrg         return;
93af69d88dSmrg      }
94af69d88dSmrg
95af69d88dSmrg      ctx->Driver.GetSamplePosition(ctx, ctx->DrawBuffer, index, val);
96af69d88dSmrg
9701e04c3fSmrg      /* FBOs can be upside down (winsys always are)*/
9801e04c3fSmrg      if (ctx->DrawBuffer->FlipY)
99af69d88dSmrg         val[1] = 1.0f - val[1];
100af69d88dSmrg
101af69d88dSmrg      return;
102af69d88dSmrg   }
103af69d88dSmrg
10401e04c3fSmrg   case GL_PROGRAMMABLE_SAMPLE_LOCATION_ARB:
10501e04c3fSmrg      if (!ctx->Extensions.ARB_sample_locations) {
10601e04c3fSmrg         _mesa_error( ctx, GL_INVALID_ENUM, "glGetMultisamplefv(pname)" );
10701e04c3fSmrg         return;
10801e04c3fSmrg      }
10901e04c3fSmrg
11001e04c3fSmrg      if (index >= MAX_SAMPLE_LOCATION_TABLE_SIZE * 2) {
11101e04c3fSmrg         _mesa_error( ctx, GL_INVALID_VALUE, "glGetMultisamplefv(index)" );
11201e04c3fSmrg         return;
11301e04c3fSmrg      }
11401e04c3fSmrg
11501e04c3fSmrg      if (ctx->DrawBuffer->SampleLocationTable)
11601e04c3fSmrg         *val = ctx->DrawBuffer->SampleLocationTable[index];
11701e04c3fSmrg      else
11801e04c3fSmrg         *val = 0.5f;
11901e04c3fSmrg
12001e04c3fSmrg      return;
12101e04c3fSmrg
122af69d88dSmrg   default:
123af69d88dSmrg      _mesa_error( ctx, GL_INVALID_ENUM, "glGetMultisamplefv(pname)" );
124af69d88dSmrg      return;
125af69d88dSmrg   }
126af69d88dSmrg}
127af69d88dSmrg
12801e04c3fSmrgstatic void
12901e04c3fSmrgsample_maski(struct gl_context *ctx, GLuint index, GLbitfield mask)
13001e04c3fSmrg{
13101e04c3fSmrg   if (ctx->Multisample.SampleMaskValue == mask)
13201e04c3fSmrg      return;
13301e04c3fSmrg
13401e04c3fSmrg   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewSampleMask ? 0 : _NEW_MULTISAMPLE);
13501e04c3fSmrg   ctx->NewDriverState |= ctx->DriverFlags.NewSampleMask;
13601e04c3fSmrg   ctx->Multisample.SampleMaskValue = mask;
13701e04c3fSmrg}
13801e04c3fSmrg
13901e04c3fSmrgvoid GLAPIENTRY
14001e04c3fSmrg_mesa_SampleMaski_no_error(GLuint index, GLbitfield mask)
14101e04c3fSmrg{
14201e04c3fSmrg   GET_CURRENT_CONTEXT(ctx);
14301e04c3fSmrg   sample_maski(ctx, index, mask);
14401e04c3fSmrg}
14501e04c3fSmrg
146af69d88dSmrgvoid GLAPIENTRY
147af69d88dSmrg_mesa_SampleMaski(GLuint index, GLbitfield mask)
148af69d88dSmrg{
149af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
150af69d88dSmrg
151af69d88dSmrg   if (!ctx->Extensions.ARB_texture_multisample) {
152af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleMaski");
153af69d88dSmrg      return;
154af69d88dSmrg   }
155af69d88dSmrg
156af69d88dSmrg   if (index != 0) {
157af69d88dSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "glSampleMaski(index)");
158af69d88dSmrg      return;
159af69d88dSmrg   }
160af69d88dSmrg
16101e04c3fSmrg   sample_maski(ctx, index, mask);
16201e04c3fSmrg}
16301e04c3fSmrg
16401e04c3fSmrgstatic void
16501e04c3fSmrgmin_sample_shading(struct gl_context *ctx, GLclampf value)
16601e04c3fSmrg{
16701e04c3fSmrg   value = CLAMP(value, 0.0f, 1.0f);
16801e04c3fSmrg
16901e04c3fSmrg   if (ctx->Multisample.MinSampleShadingValue == value)
17001e04c3fSmrg      return;
17101e04c3fSmrg
17201e04c3fSmrg   FLUSH_VERTICES(ctx,
17301e04c3fSmrg                  ctx->DriverFlags.NewSampleShading ? 0 : _NEW_MULTISAMPLE);
17401e04c3fSmrg   ctx->NewDriverState |= ctx->DriverFlags.NewSampleShading;
17501e04c3fSmrg   ctx->Multisample.MinSampleShadingValue = value;
176af69d88dSmrg}
177af69d88dSmrg
178af69d88dSmrg/**
179af69d88dSmrg * Called via glMinSampleShadingARB
180af69d88dSmrg */
18101e04c3fSmrgvoid GLAPIENTRY
18201e04c3fSmrg_mesa_MinSampleShading_no_error(GLclampf value)
18301e04c3fSmrg{
18401e04c3fSmrg   GET_CURRENT_CONTEXT(ctx);
18501e04c3fSmrg   min_sample_shading(ctx, value);
18601e04c3fSmrg}
18701e04c3fSmrg
188af69d88dSmrgvoid GLAPIENTRY
189af69d88dSmrg_mesa_MinSampleShading(GLclampf value)
190af69d88dSmrg{
191af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
192af69d88dSmrg
19301e04c3fSmrg   if (!_mesa_has_ARB_sample_shading(ctx) &&
19401e04c3fSmrg       !_mesa_has_OES_sample_shading(ctx)) {
195af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "glMinSampleShading");
196af69d88dSmrg      return;
197af69d88dSmrg   }
198af69d88dSmrg
19901e04c3fSmrg   min_sample_shading(ctx, value);
200af69d88dSmrg}
201af69d88dSmrg
202af69d88dSmrg/**
203af69d88dSmrg * Helper for checking a requested sample count against the limit
204af69d88dSmrg * for a particular (target, internalFormat) pair. The limit imposed,
205af69d88dSmrg * and the error generated, both depend on which extensions are supported.
206af69d88dSmrg *
207af69d88dSmrg * Returns a GL error enum, or GL_NO_ERROR if the requested sample count is
208af69d88dSmrg * acceptable.
209af69d88dSmrg */
210af69d88dSmrgGLenum
211af69d88dSmrg_mesa_check_sample_count(struct gl_context *ctx, GLenum target,
21201e04c3fSmrg                         GLenum internalFormat, GLsizei samples,
21301e04c3fSmrg                         GLsizei storageSamples)
214af69d88dSmrg{
21501e04c3fSmrg   /* Section 4.4 (Framebuffer objects), page 198 of the OpenGL ES 3.0.0
21601e04c3fSmrg    * specification says:
21701e04c3fSmrg    *
21801e04c3fSmrg    *     "If internalformat is a signed or unsigned integer format and samples
21901e04c3fSmrg    *     is greater than zero, then the error INVALID_OPERATION is generated."
22001e04c3fSmrg    *
22101e04c3fSmrg    * This restriction is relaxed for OpenGL ES 3.1.
22201e04c3fSmrg    */
22301e04c3fSmrg   if ((ctx->API == API_OPENGLES2 && ctx->Version == 30) &&
22401e04c3fSmrg       _mesa_is_enum_format_integer(internalFormat)
22501e04c3fSmrg       && samples > 0) {
22601e04c3fSmrg      return GL_INVALID_OPERATION;
22701e04c3fSmrg   }
22801e04c3fSmrg
22901e04c3fSmrg   if (ctx->Extensions.AMD_framebuffer_multisample_advanced &&
23001e04c3fSmrg       target == GL_RENDERBUFFER) {
23101e04c3fSmrg      if (!_mesa_is_depth_or_stencil_format(internalFormat)) {
23201e04c3fSmrg         /* From the AMD_framebuffer_multisample_advanced spec:
23301e04c3fSmrg          *
23401e04c3fSmrg          *    "An INVALID_OPERATION error is generated if <internalformat>
23501e04c3fSmrg          *     is a color format and <storageSamples> is greater than
23601e04c3fSmrg          *     the implementation-dependent limit MAX_COLOR_FRAMEBUFFER_-
23701e04c3fSmrg          *     STORAGE_SAMPLES_AMD."
23801e04c3fSmrg          */
23901e04c3fSmrg         if (samples > ctx->Const.MaxColorFramebufferSamples)
24001e04c3fSmrg            return GL_INVALID_OPERATION;
24101e04c3fSmrg
24201e04c3fSmrg         /* From the AMD_framebuffer_multisample_advanced spec:
24301e04c3fSmrg          *
24401e04c3fSmrg          *    "An INVALID_OPERATION error is generated if <internalformat>
24501e04c3fSmrg          *     is a color format and <storageSamples> is greater than
24601e04c3fSmrg          *     the implementation-dependent limit MAX_COLOR_FRAMEBUFFER_-
24701e04c3fSmrg          *     STORAGE_SAMPLES_AMD."
24801e04c3fSmrg          */
24901e04c3fSmrg         if (storageSamples > ctx->Const.MaxColorFramebufferStorageSamples)
25001e04c3fSmrg            return GL_INVALID_OPERATION;
25101e04c3fSmrg
25201e04c3fSmrg         /* From the AMD_framebuffer_multisample_advanced spec:
25301e04c3fSmrg          *
25401e04c3fSmrg          *    "An INVALID_OPERATION error is generated if <storageSamples> is
25501e04c3fSmrg          *     greater than <samples>."
25601e04c3fSmrg          */
25701e04c3fSmrg         if (storageSamples > samples)
25801e04c3fSmrg            return GL_INVALID_OPERATION;
25901e04c3fSmrg
26001e04c3fSmrg         /* Color renderbuffer sample counts are now fully validated
26101e04c3fSmrg          * according to AMD_framebuffer_multisample_advanced.
26201e04c3fSmrg          */
26301e04c3fSmrg         return GL_NO_ERROR;
26401e04c3fSmrg      } else {
26501e04c3fSmrg         /* From the AMD_framebuffer_multisample_advanced spec:
26601e04c3fSmrg          *
26701e04c3fSmrg          *    "An INVALID_OPERATION error is generated if <internalformat> is
26801e04c3fSmrg          *     a depth or stencil format and <storageSamples> is not equal to
26901e04c3fSmrg          *     <samples>."
27001e04c3fSmrg          */
27101e04c3fSmrg         if (storageSamples != samples)
27201e04c3fSmrg            return GL_INVALID_OPERATION;
27301e04c3fSmrg      }
27401e04c3fSmrg   } else {
27501e04c3fSmrg      /* If the extension is unsupported, it's not possible to set
27601e04c3fSmrg       * storageSamples differently.
27701e04c3fSmrg       */
27801e04c3fSmrg      assert(samples == storageSamples);
27901e04c3fSmrg   }
28001e04c3fSmrg
281af69d88dSmrg   /* If ARB_internalformat_query is supported, then treat its highest
282af69d88dSmrg    * returned sample count as the absolute maximum for this format; it is
283af69d88dSmrg    * allowed to exceed MAX_SAMPLES.
284af69d88dSmrg    *
285af69d88dSmrg    * From the ARB_internalformat_query spec:
286af69d88dSmrg    *
287af69d88dSmrg    * "If <samples is greater than the maximum number of samples supported
288af69d88dSmrg    * for <internalformat> then the error INVALID_OPERATION is generated."
289af69d88dSmrg    */
290af69d88dSmrg   if (ctx->Extensions.ARB_internalformat_query) {
29101e04c3fSmrg      GLint buffer[16] = {-1};
29201e04c3fSmrg      GLint limit;
29301e04c3fSmrg
29401e04c3fSmrg      ctx->Driver.QueryInternalFormat(ctx, target, internalFormat,
29501e04c3fSmrg                                      GL_SAMPLES, buffer);
29601e04c3fSmrg      /* since the query returns samples sorted in descending order,
29701e04c3fSmrg       * the first element is the greatest supported sample value.
29801e04c3fSmrg       */
29901e04c3fSmrg      limit = buffer[0];
300af69d88dSmrg
301af69d88dSmrg      return samples > limit ? GL_INVALID_OPERATION : GL_NO_ERROR;
302af69d88dSmrg   }
303af69d88dSmrg
304af69d88dSmrg   /* If ARB_texture_multisample is supported, we have separate limits,
305af69d88dSmrg    * which may be lower than MAX_SAMPLES:
306af69d88dSmrg    *
307af69d88dSmrg    * From the ARB_texture_multisample spec, when describing the operation
308af69d88dSmrg    * of RenderbufferStorageMultisample:
309af69d88dSmrg    *
310af69d88dSmrg    * "If <internalformat> is a signed or unsigned integer format and
311af69d88dSmrg    * <samples> is greater than the value of MAX_INTEGER_SAMPLES, then the
312af69d88dSmrg    * error INVALID_OPERATION is generated"
313af69d88dSmrg    *
314af69d88dSmrg    * And when describing the operation of TexImage*Multisample:
315af69d88dSmrg    *
316af69d88dSmrg    * "The error INVALID_OPERATION may be generated if any of the following
317af69d88dSmrg    * are true:
318af69d88dSmrg    *
319af69d88dSmrg    * * <internalformat> is a depth/stencil-renderable format and <samples>
320af69d88dSmrg    *   is greater than the value of MAX_DEPTH_TEXTURE_SAMPLES
321af69d88dSmrg    * * <internalformat> is a color-renderable format and <samples> is
322af69d88dSmrg    *   grater than the value of MAX_COLOR_TEXTURE_SAMPLES
323af69d88dSmrg    * * <internalformat> is a signed or unsigned integer format and
324af69d88dSmrg    *   <samples> is greater than the value of MAX_INTEGER_SAMPLES
325af69d88dSmrg    */
326af69d88dSmrg
327af69d88dSmrg   if (ctx->Extensions.ARB_texture_multisample) {
328af69d88dSmrg      if (_mesa_is_enum_format_integer(internalFormat))
329af69d88dSmrg         return samples > ctx->Const.MaxIntegerSamples
330af69d88dSmrg            ? GL_INVALID_OPERATION : GL_NO_ERROR;
331af69d88dSmrg
332af69d88dSmrg      if (target == GL_TEXTURE_2D_MULTISAMPLE ||
333af69d88dSmrg          target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
334af69d88dSmrg
335af69d88dSmrg         if (_mesa_is_depth_or_stencil_format(internalFormat))
336af69d88dSmrg            return samples > ctx->Const.MaxDepthTextureSamples
337af69d88dSmrg               ? GL_INVALID_OPERATION : GL_NO_ERROR;
338af69d88dSmrg         else
339af69d88dSmrg            return samples > ctx->Const.MaxColorTextureSamples
340af69d88dSmrg               ? GL_INVALID_OPERATION : GL_NO_ERROR;
341af69d88dSmrg      }
342af69d88dSmrg   }
343af69d88dSmrg
344af69d88dSmrg   /* No more specific limit is available, so just use MAX_SAMPLES:
345af69d88dSmrg    *
346af69d88dSmrg    * On p205 of the GL3.1 spec:
347af69d88dSmrg    *
348af69d88dSmrg    * "... or if samples is greater than MAX_SAMPLES, then the error
349af69d88dSmrg    * INVALID_VALUE is generated"
350af69d88dSmrg    */
351af69d88dSmrg   return (GLuint) samples > ctx->Const.MaxSamples
352af69d88dSmrg      ? GL_INVALID_VALUE : GL_NO_ERROR;
353c1f859d4Smrg}
354