mesa: helper for checking renderbuffer sample count
[mesa.git] / src / mesa / main / multisample.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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
34
35 /**
36 * Called via glSampleCoverageARB
37 */
38 void GLAPIENTRY
39 _mesa_SampleCoverage(GLclampf value, GLboolean invert)
40 {
41 GET_CURRENT_CONTEXT(ctx);
42
43 FLUSH_VERTICES(ctx, 0);
44
45 ctx->Multisample.SampleCoverageValue = (GLfloat) CLAMP(value, 0.0, 1.0);
46 ctx->Multisample.SampleCoverageInvert = invert;
47 ctx->NewState |= _NEW_MULTISAMPLE;
48 }
49
50
51 /**
52 * Initialize the context's multisample state.
53 * \param ctx the GL context.
54 */
55 void
56 _mesa_init_multisample(struct gl_context *ctx)
57 {
58 ctx->Multisample.Enabled = GL_TRUE;
59 ctx->Multisample.SampleAlphaToCoverage = GL_FALSE;
60 ctx->Multisample.SampleAlphaToOne = GL_FALSE;
61 ctx->Multisample.SampleCoverage = GL_FALSE;
62 ctx->Multisample.SampleCoverageValue = 1.0;
63 ctx->Multisample.SampleCoverageInvert = GL_FALSE;
64
65 /* ARB_texture_multisample / GL3.2 additions */
66 ctx->Multisample.SampleMask = GL_FALSE;
67 ctx->Multisample.SampleMaskValue = ~(GLbitfield)0;
68 }
69
70
71 void GLAPIENTRY
72 _mesa_GetMultisamplefv(GLenum pname, GLuint index, GLfloat * val)
73 {
74 GET_CURRENT_CONTEXT(ctx);
75
76 switch (pname) {
77 case GL_SAMPLE_POSITION: {
78 if (index >= ctx->DrawBuffer->Visual.samples) {
79 _mesa_error( ctx, GL_INVALID_VALUE, "glGetMultisamplefv(index)" );
80 return;
81 }
82
83 ctx->Driver.GetSamplePosition(ctx, ctx->DrawBuffer, index, val);
84
85 /* winsys FBOs are upside down */
86 if (_mesa_is_winsys_fbo(ctx->DrawBuffer))
87 val[1] = 1 - val[1];
88
89 return;
90 }
91
92 default:
93 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMultisamplefv(pname)" );
94 return;
95 }
96 }
97
98 void GLAPIENTRY
99 _mesa_SampleMaski(GLuint index, GLbitfield mask)
100 {
101 GET_CURRENT_CONTEXT(ctx);
102
103 if (!ctx->Extensions.ARB_texture_multisample) {
104 _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleMaski");
105 return;
106 }
107
108 if (index != 0) {
109 _mesa_error(ctx, GL_INVALID_VALUE, "glSampleMaski(index)");
110 return;
111 }
112
113 FLUSH_VERTICES(ctx, _NEW_MULTISAMPLE);
114 ctx->Multisample.SampleMaskValue = mask;
115 }
116
117
118 /* Helper for checking a requested sample count against the limit
119 * for a particular (target, internalFormat) pair. The limit imposed,
120 * and the error generated, both depend on which extensions are supported.
121 *
122 * Returns a GL error enum, or GL_NO_ERROR if the requested sample count is
123 * acceptable.
124 */
125 GLenum
126 _mesa_check_sample_count(struct gl_context *ctx, GLenum target,
127 GLenum internalFormat, GLsizei samples)
128 {
129 /* If ARB_internalformat_query is supported, then treat its highest returned sample
130 * count as the absolute maximum for this format; it is allowed to exceed MAX_SAMPLES.
131 *
132 * From the ARB_internalformat_query spec:
133 *
134 * "If <samples is greater than the maximum number of samples supported
135 * for <internalformat> then the error INVALID_OPERATION is generated."
136 */
137 if (ctx->Extensions.ARB_internalformat_query) {
138 GLint buffer[16];
139 int count = ctx->Driver.QuerySamplesForFormat(ctx, target, internalFormat, buffer);
140 int limit = count ? buffer[0] : -1;
141
142 return samples > limit ? GL_INVALID_OPERATION : GL_NO_ERROR;
143 }
144
145 /* If ARB_texture_multisample is supported, we have separate limits for
146 * integer formats.
147 *
148 * From the ARB_texture_multisample spec:
149 *
150 * "If <internalformat> is a signed or unsigned integer format and
151 * <samples> is greater than the value of MAX_INTEGER_SAMPLES, then the
152 * error INVALID_OPERATION is generated"
153 */
154
155 if (ctx->Extensions.ARB_texture_multisample) {
156 if (_mesa_is_enum_format_integer(internalFormat))
157 return samples > ctx->Const.MaxIntegerSamples ? GL_INVALID_OPERATION : GL_NO_ERROR;
158 }
159
160 /* No more specific limit is available, so just use MAX_SAMPLES:
161 *
162 * On p205 of the GL3.1 spec:
163 *
164 * "... or if samples is greater than MAX_SAMPLES, then the error
165 * INVALID_VALUE is generated"
166 */
167 return samples > ctx->Const.MaxSamples ? GL_INVALID_VALUE : GL_NO_ERROR;
168 }