2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
28 * glReadBuffer, DrawBuffer functions.
46 * Return bitmask of BUFFER_BIT_* flags indicating which color buffers are
47 * available to the rendering context (for drawing or reading).
48 * This depends on the type of framebuffer. For window system framebuffers
49 * we look at the framebuffer's visual. But for user-create framebuffers we
50 * look at the number of supported color attachments.
51 * \param fb the framebuffer to draw to, or read from
52 * \return bitmask of BUFFER_BIT_* flags
55 supported_buffer_bitmask(const GLcontext
*ctx
, const struct gl_framebuffer
*fb
)
57 GLbitfield mask
= 0x0;
60 /* A user-created renderbuffer */
62 ASSERT(ctx
->Extensions
.EXT_framebuffer_object
);
63 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
64 mask
|= (BUFFER_BIT_COLOR0
<< i
);
68 /* A window system framebuffer */
70 mask
= BUFFER_BIT_FRONT_LEFT
; /* always have this */
71 if (fb
->Visual
.stereoMode
) {
72 mask
|= BUFFER_BIT_FRONT_RIGHT
;
73 if (fb
->Visual
.doubleBufferMode
) {
74 mask
|= BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
77 else if (fb
->Visual
.doubleBufferMode
) {
78 mask
|= BUFFER_BIT_BACK_LEFT
;
81 for (i
= 0; i
< fb
->Visual
.numAuxBuffers
; i
++) {
82 mask
|= (BUFFER_BIT_AUX0
<< i
);
91 * Helper routine used by glDrawBuffer and glDrawBuffersARB.
92 * Given a GLenum naming one or more color buffers (such as
93 * GL_FRONT_AND_BACK), return the corresponding bitmask of BUFFER_BIT_* flags.
96 draw_buffer_enum_to_bitmask(GLenum buffer
)
102 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_FRONT_RIGHT
;
104 return BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_BACK_RIGHT
;
106 return BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
108 return BUFFER_BIT_FRONT_RIGHT
;
110 return BUFFER_BIT_BACK_RIGHT
;
112 return BUFFER_BIT_BACK_LEFT
;
113 case GL_FRONT_AND_BACK
:
114 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
115 | BUFFER_BIT_FRONT_RIGHT
| BUFFER_BIT_BACK_RIGHT
;
117 return BUFFER_BIT_FRONT_LEFT
| BUFFER_BIT_BACK_LEFT
;
119 return BUFFER_BIT_FRONT_LEFT
;
121 return BUFFER_BIT_AUX0
;
123 return BUFFER_BIT_AUX1
;
125 return BUFFER_BIT_AUX2
;
127 return BUFFER_BIT_AUX3
;
128 case GL_COLOR_ATTACHMENT0_EXT
:
129 return BUFFER_BIT_COLOR0
;
130 case GL_COLOR_ATTACHMENT1_EXT
:
131 return BUFFER_BIT_COLOR1
;
132 case GL_COLOR_ATTACHMENT2_EXT
:
133 return BUFFER_BIT_COLOR2
;
134 case GL_COLOR_ATTACHMENT3_EXT
:
135 return BUFFER_BIT_COLOR3
;
144 * Helper routine used by glReadBuffer.
145 * Given a GLenum naming a color buffer, return the index of the corresponding
146 * renderbuffer (a BUFFER_* value).
147 * return -1 for an invalid buffer.
150 read_buffer_enum_to_index(GLenum buffer
)
154 return BUFFER_FRONT_LEFT
;
156 return BUFFER_BACK_LEFT
;
158 return BUFFER_FRONT_RIGHT
;
160 return BUFFER_FRONT_RIGHT
;
162 return BUFFER_BACK_RIGHT
;
164 return BUFFER_BACK_LEFT
;
166 return BUFFER_FRONT_LEFT
;
168 return BUFFER_FRONT_LEFT
;
177 case GL_COLOR_ATTACHMENT0_EXT
:
178 return BUFFER_COLOR0
;
179 case GL_COLOR_ATTACHMENT1_EXT
:
180 return BUFFER_COLOR1
;
181 case GL_COLOR_ATTACHMENT2_EXT
:
182 return BUFFER_COLOR2
;
183 case GL_COLOR_ATTACHMENT3_EXT
:
184 return BUFFER_COLOR3
;
193 * Called by glDrawBuffer().
194 * Specify which renderbuffer(s) to draw into for the first color output.
195 * <buffer> can name zero, one, two or four renderbuffers!
196 * \sa _mesa_DrawBuffersARB
198 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
201 _mesa_DrawBuffer(GLenum buffer
)
204 GET_CURRENT_CONTEXT(ctx
);
205 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
); /* too complex... */
207 if (MESA_VERBOSE
& VERBOSE_API
) {
208 _mesa_debug(ctx
, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
211 if (buffer
== GL_NONE
) {
215 const GLbitfield supportedMask
216 = supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
217 destMask
= draw_buffer_enum_to_bitmask(buffer
);
218 if (destMask
== BAD_MASK
) {
219 /* totally bogus buffer */
220 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffer(buffer)");
223 destMask
&= supportedMask
;
224 if (destMask
== 0x0) {
225 /* none of the named color buffers exist! */
226 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffer(buffer)");
231 /* if we get here, there's no error so set new state */
232 _mesa_drawbuffers(ctx
, 1, &buffer
, &destMask
);
235 * Call device driver function.
237 if (ctx
->Driver
.DrawBuffers
)
238 ctx
->Driver
.DrawBuffers(ctx
, 1, &buffer
);
239 else if (ctx
->Driver
.DrawBuffer
)
240 ctx
->Driver
.DrawBuffer(ctx
, buffer
);
245 * Called by glDrawBuffersARB; specifies the destination color renderbuffers
246 * for N fragment program color outputs.
247 * \sa _mesa_DrawBuffer
248 * \param n number of outputs
249 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
250 * names cannot specify more than one buffer. For example,
251 * GL_FRONT_AND_BACK is illegal.
254 _mesa_DrawBuffersARB(GLsizei n
, const GLenum
*buffers
)
257 GLbitfield usedBufferMask
, supportedMask
;
258 GLbitfield destMask
[MAX_DRAW_BUFFERS
];
259 GET_CURRENT_CONTEXT(ctx
);
260 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
262 if (!ctx
->Extensions
.ARB_draw_buffers
) {
263 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawBuffersARB");
266 if (n
< 1 || n
> (GLsizei
) ctx
->Const
.MaxDrawBuffers
) {
267 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDrawBuffersARB(n)");
271 supportedMask
= supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
272 usedBufferMask
= 0x0;
274 /* complicated error checking... */
275 for (output
= 0; output
< n
; output
++) {
276 if (buffers
[output
] == GL_NONE
) {
277 destMask
[output
] = 0x0;
280 destMask
[output
] = draw_buffer_enum_to_bitmask(buffers
[output
]);
281 if (destMask
[output
] == BAD_MASK
282 || _mesa_bitcount(destMask
[output
]) > 1) {
283 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawBuffersARB(buffer)");
286 destMask
[output
] &= supportedMask
;
287 if (destMask
[output
] == 0) {
288 _mesa_error(ctx
, GL_INVALID_OPERATION
,
289 "glDrawBuffersARB(unsupported buffer)");
292 if (destMask
[output
] & usedBufferMask
) {
293 /* can't specify a dest buffer more than once! */
294 _mesa_error(ctx
, GL_INVALID_OPERATION
,
295 "glDrawBuffersARB(duplicated buffer)");
300 usedBufferMask
|= destMask
[output
];
304 /* OK, if we get here, there were no errors so set the new state */
305 _mesa_drawbuffers(ctx
, n
, buffers
, destMask
);
308 * Call device driver function.
310 if (ctx
->Driver
.DrawBuffers
)
311 ctx
->Driver
.DrawBuffers(ctx
, n
, buffers
);
312 else if (ctx
->Driver
.DrawBuffer
)
313 ctx
->Driver
.DrawBuffer(ctx
, buffers
[0]);
318 * Set color output state. Traditionally, there was only one color
319 * output, but fragment programs can now have several distinct color
320 * outputs (see GL_ARB_draw_buffers). This function sets the state
321 * for one such color output.
322 * \param ctx current context
323 * \param output which fragment program output
324 * \param buffer buffer to write to (like GL_LEFT)
325 * \param destMask BUFFER_* bitmask
326 * (like BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
329 set_color_output(GLcontext
*ctx
, GLuint output
, GLenum buffer
,
332 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
334 ASSERT(output
< ctx
->Const
.MaxDrawBuffers
);
336 /* Set per-FBO state */
337 fb
->ColorDrawBuffer
[output
] = buffer
;
338 fb
->_ColorDrawBufferMask
[output
] = destMask
;
339 /* not really needed, will be set later */
340 fb
->_NumColorDrawBuffers
[output
] = 0;
343 /* Set traditional state var */
344 ctx
->Color
.DrawBuffer
[output
] = buffer
;
349 * Helper routine used by _mesa_DrawBuffer, _mesa_DrawBuffersARB and
350 * other places (window fbo fixup) to set fbo (and the old ctx) fields.
351 * All error checking will have been done prior to calling this function
352 * so nothing should go wrong at this point.
353 * \param ctx current context
354 * \param n number of color outputs to set
355 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
356 * \param destMask array[n] of BUFFER_* bitmasks which correspond to the
357 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
358 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
361 _mesa_drawbuffers(GLcontext
*ctx
, GLuint n
, const GLenum
*buffers
,
362 const GLbitfield
*destMask
)
364 GLbitfield mask
[MAX_DRAW_BUFFERS
];
368 /* compute destMask values now */
369 const GLbitfield supportedMask
370 = supported_buffer_bitmask(ctx
, ctx
->DrawBuffer
);
371 for (output
= 0; output
< n
; output
++) {
372 mask
[output
] = draw_buffer_enum_to_bitmask(buffers
[output
]);
373 ASSERT(mask
[output
] != BAD_MASK
);
374 mask
[output
] &= supportedMask
;
379 for (output
= 0; output
< n
; output
++) {
380 set_color_output(ctx
, output
, buffers
[output
], destMask
[output
]);
383 /* set remaining color outputs to NONE */
384 for (output
= n
; output
< ctx
->Const
.MaxDrawBuffers
; output
++) {
385 set_color_output(ctx
, output
, GL_NONE
, 0x0);
388 ctx
->NewState
|= _NEW_BUFFERS
;
393 _mesa_readbuffer_update_fields(GLcontext
*ctx
, GLenum buffer
)
395 struct gl_framebuffer
*fb
;
396 GLbitfield supportedMask
;
399 fb
= ctx
->ReadBuffer
;
401 if (MESA_VERBOSE
& VERBOSE_API
)
402 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
404 if (fb
->Name
> 0 && buffer
== GL_NONE
) {
405 /* This is legal for user-created framebuffer objects */
409 /* general case / window-system framebuffer */
410 srcBuffer
= read_buffer_enum_to_index(buffer
);
411 if (srcBuffer
== -1) {
412 _mesa_error(ctx
, GL_INVALID_ENUM
, "glReadBuffer(buffer=0x%x)", buffer
);
415 supportedMask
= supported_buffer_bitmask(ctx
, fb
);
416 if (((1 << srcBuffer
) & supportedMask
) == 0) {
417 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadBuffer(buffer=0x%x)", buffer
);
423 ctx
->Pixel
.ReadBuffer
= buffer
;
425 fb
->ColorReadBuffer
= buffer
;
426 fb
->_ColorReadBufferIndex
= srcBuffer
;
434 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
435 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
438 _mesa_ReadBuffer(GLenum buffer
)
440 GET_CURRENT_CONTEXT(ctx
);
441 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
443 if (MESA_VERBOSE
& VERBOSE_API
)
444 _mesa_debug(ctx
, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer
));
446 if (!_mesa_readbuffer_update_fields(ctx
, buffer
))
449 ctx
->NewState
|= _NEW_BUFFERS
;
452 * Call device driver function.
454 if (ctx
->Driver
.ReadBuffer
)
455 (*ctx
->Driver
.ReadBuffer
)(ctx
, buffer
);