Fixed typo
[reactos.git] / dll / opengl / mesa / main / buffers.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 * \file buffers.c
27 * glReadBuffer, DrawBuffer functions.
28 */
29
30 #include <precomp.h>
31
32 #define BAD_MASK ~0u
33
34 /**
35 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
36 * available to the rendering context (for drawing or reading).
37 * This depends on the type of framebuffer. For window system framebuffers
38 * we look at the framebuffer's visual. But for user-create framebuffers we
39 * look at the number of supported color attachments.
40 * \param fb the framebuffer to draw to, or read from
41 * \return bitmask of BUFFER_BIT_* flags
42 */
43 static GLbitfield
44 supported_buffer_bitmask(const struct gl_context *ctx, const struct gl_framebuffer *fb)
45 {
46 GLbitfield mask = 0x0;
47
48 /* A window system framebuffer */
49 GLint i;
50 mask = BUFFER_BIT_FRONT_LEFT; /* always have this */
51 if (fb->Visual.stereoMode) {
52 mask |= BUFFER_BIT_FRONT_RIGHT;
53 if (fb->Visual.doubleBufferMode) {
54 mask |= BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
55 }
56 }
57 else if (fb->Visual.doubleBufferMode) {
58 mask |= BUFFER_BIT_BACK_LEFT;
59 }
60
61 for (i = 0; i < fb->Visual.numAuxBuffers; i++) {
62 mask |= (BUFFER_BIT_AUX0 << i);
63 }
64
65 return mask;
66 }
67
68
69 /**
70 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
71 * Given a GLenum naming one or more color buffers (such as
72 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
73 */
74 static GLbitfield
75 draw_buffer_enum_to_bitmask(GLenum buffer)
76 {
77 switch (buffer) {
78 case GL_NONE:
79 return 0;
80 case GL_FRONT:
81 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_FRONT_RIGHT;
82 case GL_BACK:
83 return BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
84 case GL_RIGHT:
85 return BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
86 case GL_FRONT_RIGHT:
87 return BUFFER_BIT_FRONT_RIGHT;
88 case GL_BACK_RIGHT:
89 return BUFFER_BIT_BACK_RIGHT;
90 case GL_BACK_LEFT:
91 return BUFFER_BIT_BACK_LEFT;
92 case GL_FRONT_AND_BACK:
93 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT
94 | BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
95 case GL_LEFT:
96 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT;
97 case GL_FRONT_LEFT:
98 return BUFFER_BIT_FRONT_LEFT;
99 case GL_AUX0:
100 return BUFFER_BIT_AUX0;
101 case GL_AUX1:
102 case GL_AUX2:
103 case GL_AUX3:
104 return 1 << BUFFER_COUNT; /* invalid, but not BAD_MASK */
105 default:
106 /* error */
107 return BAD_MASK;
108 }
109 }
110
111
112 /**
113 * Helper routine used by glReadBuffer.
114 * Given a GLenum naming a color buffer, return the index of the corresponding
115 * renderbuffer (a BUFFER_* value).
116 * return -1 for an invalid buffer.
117 */
118 static GLint
119 read_buffer_enum_to_index(GLenum buffer)
120 {
121 switch (buffer) {
122 case GL_FRONT:
123 return BUFFER_FRONT_LEFT;
124 case GL_BACK:
125 return BUFFER_BACK_LEFT;
126 case GL_RIGHT:
127 return BUFFER_FRONT_RIGHT;
128 case GL_FRONT_RIGHT:
129 return BUFFER_FRONT_RIGHT;
130 case GL_BACK_RIGHT:
131 return BUFFER_BACK_RIGHT;
132 case GL_BACK_LEFT:
133 return BUFFER_BACK_LEFT;
134 case GL_LEFT:
135 return BUFFER_FRONT_LEFT;
136 case GL_FRONT_LEFT:
137 return BUFFER_FRONT_LEFT;
138 case GL_AUX0:
139 return BUFFER_AUX0;
140 case GL_AUX1:
141 case GL_AUX2:
142 case GL_AUX3:
143 return BUFFER_COUNT; /* invalid, but not -1 */
144 default:
145 /* error */
146 return -1;
147 }
148 }
149
150
151 /**
152 * Called by glDrawBuffer().
153 * Specify which renderbuffer(s) to draw into for the first color output.
154 * <buffer> can name zero, one, two or four renderbuffers!
155 * \sa _mesa_DrawBuffersARB
156 *
157 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
158 *
159 * Note that the behaviour of this function depends on whether the
160 * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
161 * a user-created framebuffer object (Name!=0).
162 * In the former case, we update the per-context ctx->Color.DrawBuffer
163 * state var _and_ the FB's ColorDrawBuffer state.
164 * In the later case, we update the FB's ColorDrawBuffer state only.
165 *
166 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
167 * new FB is a window system FB, we need to re-update the FB's
168 * ColorDrawBuffer state to match the context. This is handled in
169 * _mesa_update_framebuffer().
170 *
171 * See the GL_EXT_framebuffer_object spec for more info.
172 */
173 void GLAPIENTRY
174 _mesa_DrawBuffer(GLenum buffer)
175 {
176 GLbitfield destMask;
177 GET_CURRENT_CONTEXT(ctx);
178 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex... */
179
180 if (MESA_VERBOSE & VERBOSE_API) {
181 _mesa_debug(ctx, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
182 }
183
184 if (buffer == GL_NONE) {
185 destMask = 0x0;
186 }
187 else {
188 const GLbitfield supportedMask
189 = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
190 destMask = draw_buffer_enum_to_bitmask(buffer);
191 if (destMask == BAD_MASK) {
192 /* totally bogus buffer */
193 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffer(buffer=0x%x)", buffer);
194 return;
195 }
196 destMask &= supportedMask;
197 if (destMask == 0x0) {
198 /* none of the named color buffers exist! */
199 _mesa_error(ctx, GL_INVALID_OPERATION,
200 "glDrawBuffer(buffer=0x%x)", buffer);
201 return;
202 }
203 }
204
205 /* if we get here, there's no error so set new state */
206 _mesa_drawbuffer(ctx, buffer, destMask);
207
208 /*
209 * Call device driver function.
210 */
211 if (ctx->Driver.DrawBuffer)
212 ctx->Driver.DrawBuffer(ctx, buffer);
213 }
214
215 /**
216 * Performs necessary state updates when _mesa_drawbuffers makes an
217 * actual change.
218 */
219 static void
220 updated_drawbuffers(struct gl_context *ctx)
221 {
222 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
223 }
224
225 /**
226 * Helper function to set the GL_DRAW_BUFFER state in the context and
227 * current FBO. Called via glDrawBuffer(), glDrawBuffersARB()
228 *
229 * All error checking will have been done prior to calling this function
230 * so nothing should go wrong at this point.
231 *
232 * \param ctx current context
233 * \param n number of color outputs to set
234 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
235 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
236 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
237 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
238 */
239 void
240 _mesa_drawbuffer(struct gl_context *ctx, const GLenum buffers, GLbitfield destMask)
241 {
242 struct gl_framebuffer *fb = ctx->DrawBuffer;
243 GLint bufIndex;
244
245 if (!destMask) {
246 /* compute destMask values now */
247 const GLbitfield supportedMask = supported_buffer_bitmask(ctx, fb);
248 destMask = draw_buffer_enum_to_bitmask(buffers);
249 ASSERT(destmask != BAD_MASK);
250 destMask &= supportedMask;
251 }
252
253 bufIndex = _mesa_ffs(destMask) - 1;
254
255 if (fb->_ColorDrawBufferIndex != bufIndex) {
256 updated_drawbuffers(ctx);
257 fb->_ColorDrawBufferIndex = bufIndex;
258 }
259 fb->ColorDrawBuffer = buffers;
260
261 if (ctx->Color.DrawBuffer != fb->ColorDrawBuffer) {
262 updated_drawbuffers(ctx);
263 ctx->Color.DrawBuffer = fb->ColorDrawBuffer;
264 }
265 }
266
267
268 /**
269 * Update the current drawbuffer's _ColorDrawBufferIndex[] list, etc.
270 * from the context's Color.DrawBuffer[] state.
271 * Use when changing contexts.
272 */
273 void
274 _mesa_update_draw_buffer(struct gl_context *ctx)
275 {
276 _mesa_drawbuffer(ctx, ctx->Color.DrawBuffer, 0);
277 }
278
279
280 /**
281 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
282 * GL_READ_BUFFER state in the context and current FBO.
283 * \param ctx the rendering context
284 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
285 * \param bufferIndex the numerical index corresponding to 'buffer'
286 */
287 void
288 _mesa_readbuffer(struct gl_context *ctx, GLenum buffer, GLint bufferIndex)
289 {
290 struct gl_framebuffer *fb = ctx->ReadBuffer;
291
292 ctx->Pixel.ReadBuffer = buffer;
293
294 fb->ColorReadBuffer = buffer;
295 fb->_ColorReadBufferIndex = bufferIndex;
296
297 ctx->NewState |= _NEW_BUFFERS;
298 }
299
300
301
302 /**
303 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
304 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
305 */
306 void GLAPIENTRY
307 _mesa_ReadBuffer(GLenum buffer)
308 {
309 struct gl_framebuffer *fb;
310 GLbitfield supportedMask;
311 GLint srcBuffer;
312 GET_CURRENT_CONTEXT(ctx);
313 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
314
315 if (MESA_VERBOSE & VERBOSE_API)
316 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
317
318 fb = ctx->ReadBuffer;
319
320 if (MESA_VERBOSE & VERBOSE_API)
321 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
322
323 /* general case / window-system framebuffer */
324 srcBuffer = read_buffer_enum_to_index(buffer);
325 if (srcBuffer == -1) {
326 _mesa_error(ctx, GL_INVALID_ENUM,
327 "glReadBuffer(buffer=0x%x)", buffer);
328 return;
329 }
330 supportedMask = supported_buffer_bitmask(ctx, fb);
331 if (((1 << srcBuffer) & supportedMask) == 0) {
332 _mesa_error(ctx, GL_INVALID_OPERATION,
333 "glReadBuffer(buffer=0x%x)", buffer);
334 return;
335 }
336
337 /* OK, all error checking has been completed now */
338
339 _mesa_readbuffer(ctx, buffer, srcBuffer);
340 ctx->NewState |= _NEW_BUFFERS;
341
342 /*
343 * Call device driver function.
344 */
345 if (ctx->Driver.ReadBuffer)
346 (*ctx->Driver.ReadBuffer)(ctx, buffer);
347 }