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