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