summaryrefslogtreecommitdiff
path: root/src/mesa/main/multisample.c
blob: 0dbf374b8cfdac824b814609c430b9c76651d6b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */


#include "main/glheader.h"
#include "main/context.h"
#include "main/macros.h"
#include "main/multisample.h"
#include "main/mtypes.h"
#include "main/fbobject.h"
#include "main/glformats.h"
#include "main/state.h"

#include "state_tracker/st_format.h"
#include "state_tracker/st_cb_msaa.h"

/**
 * Called via glSampleCoverageARB
 */
void GLAPIENTRY
_mesa_SampleCoverage(GLclampf value, GLboolean invert)
{
   GET_CURRENT_CONTEXT(ctx);

   value = SATURATE(value);

   if (ctx->Multisample.SampleCoverageInvert == invert &&
       ctx->Multisample.SampleCoverageValue == value)
      return;

   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewSampleMask ? 0 : _NEW_MULTISAMPLE,
                  GL_MULTISAMPLE_BIT);
   ctx->NewDriverState |= ctx->DriverFlags.NewSampleMask;
   ctx->Multisample.SampleCoverageValue = value;
   ctx->Multisample.SampleCoverageInvert = invert;
}


/**
 * Initialize the context's multisample state.
 * \param ctx  the GL context.
 */
void
_mesa_init_multisample(struct gl_context *ctx)
{
   ctx->Multisample.Enabled = GL_TRUE;
   ctx->Multisample.SampleAlphaToCoverage = GL_FALSE;
   ctx->Multisample.SampleAlphaToCoverageDitherControl = GL_ALPHA_TO_COVERAGE_DITHER_DEFAULT_NV;
   ctx->Multisample.SampleAlphaToOne = GL_FALSE;
   ctx->Multisample.SampleCoverage = GL_FALSE;
   ctx->Multisample.SampleCoverageValue = 1.0;
   ctx->Multisample.SampleCoverageInvert = GL_FALSE;
   ctx->Multisample.SampleShading = GL_FALSE;
   ctx->Multisample.MinSampleShadingValue = 0.0f;

   /* ARB_texture_multisample / GL3.2 additions */
   ctx->Multisample.SampleMask = GL_FALSE;
   ctx->Multisample.SampleMaskValue = ~(GLbitfield)0;
}


void GLAPIENTRY
_mesa_GetMultisamplefv(GLenum pname, GLuint index, GLfloat * val)
{
   GET_CURRENT_CONTEXT(ctx);

   if (ctx->NewState & _NEW_BUFFERS) {
      _mesa_update_state(ctx);
   }

   switch (pname) {
   case GL_SAMPLE_POSITION: {
      if (index >= ctx->DrawBuffer->Visual.samples) {
         _mesa_error( ctx, GL_INVALID_VALUE, "glGetMultisamplefv(index)" );
         return;
      }

      st_GetSamplePosition(ctx, ctx->DrawBuffer, index, val);

      /* FBOs can be upside down (winsys always are)*/
      if (ctx->DrawBuffer->FlipY)
         val[1] = 1.0f - val[1];

      return;
   }

   case GL_PROGRAMMABLE_SAMPLE_LOCATION_ARB:
      if (!ctx->Extensions.ARB_sample_locations) {
         _mesa_error( ctx, GL_INVALID_ENUM, "glGetMultisamplefv(pname)" );
         return;
      }

      if (index >= MAX_SAMPLE_LOCATION_TABLE_SIZE * 2) {
         _mesa_error( ctx, GL_INVALID_VALUE, "glGetMultisamplefv(index)" );
         return;
      }

      if (ctx->DrawBuffer->SampleLocationTable)
         *val = ctx->DrawBuffer->SampleLocationTable[index];
      else
         *val = 0.5f;

      return;

   default:
      _mesa_error( ctx, GL_INVALID_ENUM, "glGetMultisamplefv(pname)" );
      return;
   }
}

static void
sample_maski(struct gl_context *ctx, GLuint index, GLbitfield mask)
{
   if (ctx->Multisample.SampleMaskValue == mask)
      return;

   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewSampleMask ? 0 : _NEW_MULTISAMPLE, 0);
   ctx->NewDriverState |= ctx->DriverFlags.NewSampleMask;
   ctx->Multisample.SampleMaskValue = mask;
}

void GLAPIENTRY
_mesa_SampleMaski_no_error(GLuint index, GLbitfield mask)
{
   GET_CURRENT_CONTEXT(ctx);
   sample_maski(ctx, index, mask);
}

void GLAPIENTRY
_mesa_SampleMaski(GLuint index, GLbitfield mask)
{
   GET_CURRENT_CONTEXT(ctx);

   if (!ctx->Extensions.ARB_texture_multisample) {
      _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleMaski");
      return;
   }

   if (index != 0) {
      _mesa_error(ctx, GL_INVALID_VALUE, "glSampleMaski(index)");
      return;
   }

   sample_maski(ctx, index, mask);
}

static void
min_sample_shading(struct gl_context *ctx, GLclampf value)
{
   value = SATURATE(value);

   if (ctx->Multisample.MinSampleShadingValue == value)
      return;

   FLUSH_VERTICES(ctx,
                  ctx->DriverFlags.NewSampleShading ? 0 : _NEW_MULTISAMPLE,
                  GL_MULTISAMPLE_BIT);
   ctx->NewDriverState |= ctx->DriverFlags.NewSampleShading;
   ctx->Multisample.MinSampleShadingValue = value;
}

/**
 * Called via glMinSampleShadingARB
 */
void GLAPIENTRY
_mesa_MinSampleShading_no_error(GLclampf value)
{
   GET_CURRENT_CONTEXT(ctx);
   min_sample_shading(ctx, value);
}

void GLAPIENTRY
_mesa_MinSampleShading(GLclampf value)
{
   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_has_ARB_sample_shading(ctx) &&
       !_mesa_has_OES_sample_shading(ctx)) {
      _mesa_error(ctx, GL_INVALID_OPERATION, "glMinSampleShading");
      return;
   }

   min_sample_shading(ctx, value);
}

/**
 * Helper for checking a requested sample count against the limit
 * for a particular (target, internalFormat) pair. The limit imposed,
 * and the error generated, both depend on which extensions are supported.
 *
 * Returns a GL error enum, or GL_NO_ERROR if the requested sample count is
 * acceptable.
 */
GLenum
_mesa_check_sample_count(struct gl_context *ctx, GLenum target,
                         GLenum internalFormat, GLsizei samples,
                         GLsizei storageSamples)
{
   /* Section 4.4 (Framebuffer objects), page 198 of the OpenGL ES 3.0.0
    * specification says:
    *
    *     "If internalformat is a signed or unsigned integer format and samples
    *     is greater than zero, then the error INVALID_OPERATION is generated."
    *
    * This restriction is relaxed for OpenGL ES 3.1.
    */
   if ((ctx->API == API_OPENGLES2 && ctx->Version == 30) &&
       _mesa_is_enum_format_integer(internalFormat)
       && samples > 0) {
      return GL_INVALID_OPERATION;
   }

   if (ctx->Extensions.AMD_framebuffer_multisample_advanced &&
       target == GL_RENDERBUFFER) {
      if (!_mesa_is_depth_or_stencil_format(internalFormat)) {
         /* From the AMD_framebuffer_multisample_advanced spec:
          *
          *    "An INVALID_OPERATION error is generated if <internalformat>
          *     is a color format and <storageSamples> is greater than
          *     the implementation-dependent limit MAX_COLOR_FRAMEBUFFER_-
          *     STORAGE_SAMPLES_AMD."
          */
         if (samples > ctx->Const.MaxColorFramebufferSamples)
            return GL_INVALID_OPERATION;

         /* From the AMD_framebuffer_multisample_advanced spec:
          *
          *    "An INVALID_OPERATION error is generated if <internalformat>
          *     is a color format and <storageSamples> is greater than
          *     the implementation-dependent limit MAX_COLOR_FRAMEBUFFER_-
          *     STORAGE_SAMPLES_AMD."
          */
         if (storageSamples > ctx->Const.MaxColorFramebufferStorageSamples)
            return GL_INVALID_OPERATION;

         /* From the AMD_framebuffer_multisample_advanced spec:
          *
          *    "An INVALID_OPERATION error is generated if <storageSamples> is
          *     greater than <samples>."
          */
         if (storageSamples > samples)
            return GL_INVALID_OPERATION;

         /* Color renderbuffer sample counts are now fully validated
          * according to AMD_framebuffer_multisample_advanced.
          */
         return GL_NO_ERROR;
      } else {
         /* From the AMD_framebuffer_multisample_advanced spec:
          *
          *    "An INVALID_OPERATION error is generated if <internalformat> is
          *     a depth or stencil format and <storageSamples> is not equal to
          *     <samples>."
          */
         if (storageSamples != samples)
            return GL_INVALID_OPERATION;
      }
   } else {
      /* If the extension is unsupported, it's not possible to set
       * storageSamples differently.
       */
      assert(samples == storageSamples);
   }

   /* If ARB_internalformat_query is supported, then treat its highest
    * returned sample count as the absolute maximum for this format; it is
    * allowed to exceed MAX_SAMPLES.
    *
    * From the ARB_internalformat_query spec:
    *
    * "If <samples is greater than the maximum number of samples supported
    * for <internalformat> then the error INVALID_OPERATION is generated."
    */
   if (ctx->Extensions.ARB_internalformat_query) {
      GLint buffer[16] = {-1};
      GLint limit;

      st_QueryInternalFormat(ctx, target, internalFormat,
                             GL_SAMPLES, buffer);
      /* since the query returns samples sorted in descending order,
       * the first element is the greatest supported sample value.
       */
      limit = buffer[0];

      return samples > limit ? GL_INVALID_OPERATION : GL_NO_ERROR;
   }

   /* If ARB_texture_multisample is supported, we have separate limits,
    * which may be lower than MAX_SAMPLES:
    *
    * From the ARB_texture_multisample spec, when describing the operation
    * of RenderbufferStorageMultisample:
    *
    * "If <internalformat> is a signed or unsigned integer format and
    * <samples> is greater than the value of MAX_INTEGER_SAMPLES, then the
    * error INVALID_OPERATION is generated"
    *
    * And when describing the operation of TexImage*Multisample:
    *
    * "The error INVALID_OPERATION may be generated if any of the following
    * are true:
    *
    * * <internalformat> is a depth/stencil-renderable format and <samples>
    *   is greater than the value of MAX_DEPTH_TEXTURE_SAMPLES
    * * <internalformat> is a color-renderable format and <samples> is
    *   grater than the value of MAX_COLOR_TEXTURE_SAMPLES
    * * <internalformat> is a signed or unsigned integer format and
    *   <samples> is greater than the value of MAX_INTEGER_SAMPLES
    */

   if (ctx->Extensions.ARB_texture_multisample) {
      if (_mesa_is_enum_format_integer(internalFormat))
         return samples > ctx->Const.MaxIntegerSamples
            ? GL_INVALID_OPERATION : GL_NO_ERROR;

      if (target == GL_TEXTURE_2D_MULTISAMPLE ||
          target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {

         if (_mesa_is_depth_or_stencil_format(internalFormat))
            return samples > ctx->Const.MaxDepthTextureSamples
               ? GL_INVALID_OPERATION : GL_NO_ERROR;
         else
            return samples > ctx->Const.MaxColorTextureSamples
               ? GL_INVALID_OPERATION : GL_NO_ERROR;
      }
   }

   /* No more specific limit is available, so just use MAX_SAMPLES:
    *
    * On p205 of the GL3.1 spec:
    *
    * "... or if samples is greater than MAX_SAMPLES, then the error
    * INVALID_VALUE is generated"
    */
   return (GLuint) samples > ctx->Const.MaxSamples
      ? GL_INVALID_VALUE : GL_NO_ERROR;
}

void GLAPIENTRY
_mesa_AlphaToCoverageDitherControlNV_no_error(GLenum mode)
{
   GET_CURRENT_CONTEXT(ctx);

   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewSampleAlphaToXEnable ? 0 :
                                                   _NEW_MULTISAMPLE,
                  GL_MULTISAMPLE_BIT);
   ctx->NewDriverState |= ctx->DriverFlags.NewSampleAlphaToXEnable;
   ctx->Multisample.SampleAlphaToCoverageDitherControl = mode;
}

void GLAPIENTRY
_mesa_AlphaToCoverageDitherControlNV(GLenum mode)
{
   GET_CURRENT_CONTEXT(ctx);

   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewSampleAlphaToXEnable ? 0 :
                                                   _NEW_MULTISAMPLE,
                  GL_MULTISAMPLE_BIT);
   ctx->NewDriverState |= ctx->DriverFlags.NewSampleAlphaToXEnable;
   switch (mode) {
      case GL_ALPHA_TO_COVERAGE_DITHER_DEFAULT_NV:
      case GL_ALPHA_TO_COVERAGE_DITHER_ENABLE_NV:
      case GL_ALPHA_TO_COVERAGE_DITHER_DISABLE_NV:
         ctx->Multisample.SampleAlphaToCoverageDitherControl = mode;
         break;
      default:
         _mesa_error(ctx, GL_INVALID_ENUM, "glAlphaToCoverageDitherControlNV(invalid parameter)");
   }
}