mesa: comments, formatting fixes in dlist code
[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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include "main/glheader.h"
28 #include "main/context.h"
29 #include "main/macros.h"
30 #include "main/multisample.h"
31 #include "main/mtypes.h"
32 #include "main/fbobject.h"
33 #include "main/glformats.h"
34 #include "main/state.h"
35
36
37 /**
38 * Called via glSampleCoverageARB
39 */
40 void GLAPIENTRY
41 _mesa_SampleCoverage(GLclampf value, GLboolean invert)
42 {
43 GET_CURRENT_CONTEXT(ctx);
44
45 FLUSH_VERTICES(ctx, 0);
46
47 ctx->Multisample.SampleCoverageValue = (GLfloat) CLAMP(value, 0.0, 1.0);
48 ctx->Multisample.SampleCoverageInvert = invert;
49 ctx->NewState |= _NEW_MULTISAMPLE;
50 }
51
52
53 /**
54 * Initialize the context's multisample state.
55 * \param ctx the GL context.
56 */
57 void
58 _mesa_init_multisample(struct gl_context *ctx)
59 {
60 ctx->Multisample.Enabled = GL_TRUE;
61 ctx->Multisample.SampleAlphaToCoverage = GL_FALSE;
62 ctx->Multisample.SampleAlphaToOne = GL_FALSE;
63 ctx->Multisample.SampleCoverage = GL_FALSE;
64 ctx->Multisample.SampleCoverageValue = 1.0;
65 ctx->Multisample.SampleCoverageInvert = GL_FALSE;
66
67 /* ARB_texture_multisample / GL3.2 additions */
68 ctx->Multisample.SampleMask = GL_FALSE;
69 ctx->Multisample.SampleMaskValue = ~(GLbitfield)0;
70 }
71
72
73 void GLAPIENTRY
74 _mesa_GetMultisamplefv(GLenum pname, GLuint index, GLfloat * val)
75 {
76 GET_CURRENT_CONTEXT(ctx);
77
78 if (ctx->NewState & _NEW_BUFFERS) {
79 _mesa_update_state(ctx);
80 }
81
82 switch (pname) {
83 case GL_SAMPLE_POSITION: {
84 if (index >= ctx->DrawBuffer->Visual.samples) {
85 _mesa_error( ctx, GL_INVALID_VALUE, "glGetMultisamplefv(index)" );
86 return;
87 }
88
89 ctx->Driver.GetSamplePosition(ctx, ctx->DrawBuffer, index, val);
90
91 /* winsys FBOs are upside down */
92 if (_mesa_is_winsys_fbo(ctx->DrawBuffer))
93 val[1] = 1 - val[1];
94
95 return;
96 }
97
98 default:
99 _mesa_error( ctx, GL_INVALID_ENUM, "glGetMultisamplefv(pname)" );
100 return;
101 }
102 }
103
104 void GLAPIENTRY
105 _mesa_SampleMaski(GLuint index, GLbitfield mask)
106 {
107 GET_CURRENT_CONTEXT(ctx);
108
109 if (!ctx->Extensions.ARB_texture_multisample) {
110 _mesa_error(ctx, GL_INVALID_OPERATION, "glSampleMaski");
111 return;
112 }
113
114 if (index != 0) {
115 _mesa_error(ctx, GL_INVALID_VALUE, "glSampleMaski(index)");
116 return;
117 }
118
119 FLUSH_VERTICES(ctx, _NEW_MULTISAMPLE);
120 ctx->Multisample.SampleMaskValue = mask;
121 }
122
123
124 /* Helper for checking a requested sample count against the limit
125 * for a particular (target, internalFormat) pair. The limit imposed,
126 * and the error generated, both depend on which extensions are supported.
127 *
128 * Returns a GL error enum, or GL_NO_ERROR if the requested sample count is
129 * acceptable.
130 */
131 GLenum
132 _mesa_check_sample_count(struct gl_context *ctx, GLenum target,
133 GLenum internalFormat, GLsizei samples)
134 {
135 /* If ARB_internalformat_query is supported, then treat its highest returned sample
136 * count as the absolute maximum for this format; it is allowed to exceed MAX_SAMPLES.
137 *
138 * From the ARB_internalformat_query spec:
139 *
140 * "If <samples is greater than the maximum number of samples supported
141 * for <internalformat> then the error INVALID_OPERATION is generated."
142 */
143 if (ctx->Extensions.ARB_internalformat_query) {
144 GLint buffer[16];
145 int count = ctx->Driver.QuerySamplesForFormat(ctx, target, internalFormat, buffer);
146 int limit = count ? buffer[0] : -1;
147
148 return samples > limit ? GL_INVALID_OPERATION : GL_NO_ERROR;
149 }
150
151 /* If ARB_texture_multisample is supported, we have separate limits,
152 * which may be lower than MAX_SAMPLES:
153 *
154 * From the ARB_texture_multisample spec, when describing the operation
155 * of RenderbufferStorageMultisample:
156 *
157 * "If <internalformat> is a signed or unsigned integer format and
158 * <samples> is greater than the value of MAX_INTEGER_SAMPLES, then the
159 * error INVALID_OPERATION is generated"
160 *
161 * And when describing the operation of TexImage*Multisample:
162 *
163 * "The error INVALID_OPERATION may be generated if any of the following are true:
164 *
165 * * <internalformat> is a depth/stencil-renderable format and <samples>
166 * is greater than the value of MAX_DEPTH_TEXTURE_SAMPLES
167 * * <internalformat> is a color-renderable format and <samples> is
168 * grater than the value of MAX_COLOR_TEXTURE_SAMPLES
169 * * <internalformat> is a signed or unsigned integer format and
170 * <samples> is greater than the value of MAX_INTEGER_SAMPLES
171 */
172
173 if (ctx->Extensions.ARB_texture_multisample) {
174 if (_mesa_is_enum_format_integer(internalFormat))
175 return samples > ctx->Const.MaxIntegerSamples ? GL_INVALID_OPERATION : GL_NO_ERROR;
176
177 if (target == GL_TEXTURE_2D_MULTISAMPLE ||
178 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
179
180 if (_mesa_is_depth_or_stencil_format(internalFormat))
181 return samples > ctx->Const.MaxDepthTextureSamples
182 ? GL_INVALID_OPERATION : GL_NO_ERROR;
183 else
184 return samples > ctx->Const.MaxColorTextureSamples
185 ? GL_INVALID_OPERATION : GL_NO_ERROR;
186 }
187 }
188
189 /* No more specific limit is available, so just use MAX_SAMPLES:
190 *
191 * On p205 of the GL3.1 spec:
192 *
193 * "... or if samples is greater than MAX_SAMPLES, then the error
194 * INVALID_VALUE is generated"
195 */
196 return samples > ctx->Const.MaxSamples ? GL_INVALID_VALUE : GL_NO_ERROR;
197 }