mesa: merge gallium-0.2 into gallium-master-merge
[mesa.git] / 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 "fbobject.h"
39 #include "state.h"
40
41
42 #define BAD_MASK ~0u
43
44
45 /**
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
53 */
54 static GLbitfield
55 supported_buffer_bitmask(const GLcontext *ctx, const struct gl_framebuffer *fb)
56 {
57 GLbitfield mask = 0x0;
58
59 if (fb->Name > 0) {
60 /* A user-created renderbuffer */
61 GLuint i;
62 ASSERT(ctx->Extensions.EXT_framebuffer_object);
63 for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
64 mask |= (BUFFER_BIT_COLOR0 << i);
65 }
66 }
67 else {
68 /* A window system framebuffer */
69 GLint i;
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;
75 }
76 }
77 else if (fb->Visual.doubleBufferMode) {
78 mask |= BUFFER_BIT_BACK_LEFT;
79 }
80
81 for (i = 0; i < fb->Visual.numAuxBuffers; i++) {
82 mask |= (BUFFER_BIT_AUX0 << i);
83 }
84 }
85
86 return mask;
87 }
88
89
90 /**
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.
94 */
95 static GLbitfield
96 draw_buffer_enum_to_bitmask(GLenum buffer)
97 {
98 switch (buffer) {
99 case GL_NONE:
100 return 0;
101 case GL_FRONT:
102 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_FRONT_RIGHT;
103 case GL_BACK:
104 return BUFFER_BIT_BACK_LEFT | BUFFER_BIT_BACK_RIGHT;
105 case GL_RIGHT:
106 return BUFFER_BIT_FRONT_RIGHT | BUFFER_BIT_BACK_RIGHT;
107 case GL_FRONT_RIGHT:
108 return BUFFER_BIT_FRONT_RIGHT;
109 case GL_BACK_RIGHT:
110 return BUFFER_BIT_BACK_RIGHT;
111 case GL_BACK_LEFT:
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;
116 case GL_LEFT:
117 return BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT;
118 case GL_FRONT_LEFT:
119 return BUFFER_BIT_FRONT_LEFT;
120 case GL_AUX0:
121 return BUFFER_BIT_AUX0;
122 case GL_AUX1:
123 return BUFFER_BIT_AUX1;
124 case GL_AUX2:
125 return BUFFER_BIT_AUX2;
126 case GL_AUX3:
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;
136 case GL_COLOR_ATTACHMENT4_EXT:
137 return BUFFER_BIT_COLOR4;
138 case GL_COLOR_ATTACHMENT5_EXT:
139 return BUFFER_BIT_COLOR5;
140 case GL_COLOR_ATTACHMENT6_EXT:
141 return BUFFER_BIT_COLOR6;
142 case GL_COLOR_ATTACHMENT7_EXT:
143 return BUFFER_BIT_COLOR7;
144 default:
145 /* error */
146 return BAD_MASK;
147 }
148 }
149
150
151 /**
152 * Helper routine used by glReadBuffer.
153 * Given a GLenum naming a color buffer, return the index of the corresponding
154 * renderbuffer (a BUFFER_* value).
155 * return -1 for an invalid buffer.
156 */
157 static GLint
158 read_buffer_enum_to_index(GLenum buffer)
159 {
160 switch (buffer) {
161 case GL_FRONT:
162 return BUFFER_FRONT_LEFT;
163 case GL_BACK:
164 return BUFFER_BACK_LEFT;
165 case GL_RIGHT:
166 return BUFFER_FRONT_RIGHT;
167 case GL_FRONT_RIGHT:
168 return BUFFER_FRONT_RIGHT;
169 case GL_BACK_RIGHT:
170 return BUFFER_BACK_RIGHT;
171 case GL_BACK_LEFT:
172 return BUFFER_BACK_LEFT;
173 case GL_LEFT:
174 return BUFFER_FRONT_LEFT;
175 case GL_FRONT_LEFT:
176 return BUFFER_FRONT_LEFT;
177 case GL_AUX0:
178 return BUFFER_AUX0;
179 case GL_AUX1:
180 return BUFFER_AUX1;
181 case GL_AUX2:
182 return BUFFER_AUX2;
183 case GL_AUX3:
184 return BUFFER_AUX3;
185 case GL_COLOR_ATTACHMENT0_EXT:
186 return BUFFER_COLOR0;
187 case GL_COLOR_ATTACHMENT1_EXT:
188 return BUFFER_COLOR1;
189 case GL_COLOR_ATTACHMENT2_EXT:
190 return BUFFER_COLOR2;
191 case GL_COLOR_ATTACHMENT3_EXT:
192 return BUFFER_COLOR3;
193 case GL_COLOR_ATTACHMENT4_EXT:
194 return BUFFER_COLOR4;
195 case GL_COLOR_ATTACHMENT5_EXT:
196 return BUFFER_COLOR5;
197 case GL_COLOR_ATTACHMENT6_EXT:
198 return BUFFER_COLOR6;
199 case GL_COLOR_ATTACHMENT7_EXT:
200 return BUFFER_COLOR7;
201 default:
202 /* error */
203 return -1;
204 }
205 }
206
207
208 /**
209 * Called by glDrawBuffer().
210 * Specify which renderbuffer(s) to draw into for the first color output.
211 * <buffer> can name zero, one, two or four renderbuffers!
212 * \sa _mesa_DrawBuffersARB
213 *
214 * \param buffer buffer token such as GL_LEFT or GL_FRONT_AND_BACK, etc.
215 *
216 * Note that the behaviour of this function depends on whether the
217 * current ctx->DrawBuffer is a window-system framebuffer (Name=0) or
218 * a user-created framebuffer object (Name!=0).
219 * In the former case, we update the per-context ctx->Color.DrawBuffer
220 * state var _and_ the FB's ColorDrawBuffer state.
221 * In the later case, we update the FB's ColorDrawBuffer state only.
222 *
223 * Furthermore, upon a MakeCurrent() or BindFramebuffer() call, if the
224 * new FB is a window system FB, we need to re-update the FB's
225 * ColorDrawBuffer state to match the context. This is handled in
226 * _mesa_update_framebuffer().
227 *
228 * See the GL_EXT_framebuffer_object spec for more info.
229 */
230 void GLAPIENTRY
231 _mesa_DrawBuffer(GLenum buffer)
232 {
233 GLbitfield destMask;
234 GET_CURRENT_CONTEXT(ctx);
235 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex... */
236
237 if (MESA_VERBOSE & VERBOSE_API) {
238 _mesa_debug(ctx, "glDrawBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
239 }
240
241 if (buffer == GL_NONE) {
242 destMask = 0x0;
243 }
244 else {
245 const GLbitfield supportedMask
246 = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
247 destMask = draw_buffer_enum_to_bitmask(buffer);
248 if (destMask == BAD_MASK) {
249 /* totally bogus buffer */
250 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffer(buffer=0x%x)", buffer);
251 return;
252 }
253 destMask &= supportedMask;
254 if (destMask == 0x0) {
255 /* none of the named color buffers exist! */
256 _mesa_error(ctx, GL_INVALID_OPERATION,
257 "glDrawBuffer(buffer=0x%x)", buffer);
258 return;
259 }
260 }
261
262 /* if we get here, there's no error so set new state */
263 _mesa_drawbuffers(ctx, 1, &buffer, &destMask);
264
265 /*
266 * Call device driver function.
267 */
268 if (ctx->Driver.DrawBuffers)
269 ctx->Driver.DrawBuffers(ctx, 1, &buffer);
270 else if (ctx->Driver.DrawBuffer)
271 ctx->Driver.DrawBuffer(ctx, buffer);
272 }
273
274
275 /**
276 * Called by glDrawBuffersARB; specifies the destination color renderbuffers
277 * for N fragment program color outputs.
278 * \sa _mesa_DrawBuffer
279 * \param n number of outputs
280 * \param buffers array [n] of renderbuffer names. Unlike glDrawBuffer, the
281 * names cannot specify more than one buffer. For example,
282 * GL_FRONT_AND_BACK is illegal.
283 */
284 void GLAPIENTRY
285 _mesa_DrawBuffersARB(GLsizei n, const GLenum *buffers)
286 {
287 GLint output;
288 GLbitfield usedBufferMask, supportedMask;
289 GLbitfield destMask[MAX_DRAW_BUFFERS];
290 GET_CURRENT_CONTEXT(ctx);
291 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
292
293 if (n < 1 || n > (GLsizei) ctx->Const.MaxDrawBuffers) {
294 _mesa_error(ctx, GL_INVALID_VALUE, "glDrawBuffersARB(n)");
295 return;
296 }
297
298 supportedMask = supported_buffer_bitmask(ctx, ctx->DrawBuffer);
299 usedBufferMask = 0x0;
300
301 /* complicated error checking... */
302 for (output = 0; output < n; output++) {
303 if (buffers[output] == GL_NONE) {
304 destMask[output] = 0x0;
305 }
306 else {
307 destMask[output] = draw_buffer_enum_to_bitmask(buffers[output]);
308 if (destMask[output] == BAD_MASK
309 || _mesa_bitcount(destMask[output]) > 1) {
310 _mesa_error(ctx, GL_INVALID_ENUM, "glDrawBuffersARB(buffer)");
311 return;
312 }
313 destMask[output] &= supportedMask;
314 if (destMask[output] == 0) {
315 _mesa_error(ctx, GL_INVALID_OPERATION,
316 "glDrawBuffersARB(unsupported buffer)");
317 return;
318 }
319 if (destMask[output] & usedBufferMask) {
320 /* can't specify a dest buffer more than once! */
321 _mesa_error(ctx, GL_INVALID_OPERATION,
322 "glDrawBuffersARB(duplicated buffer)");
323 return;
324 }
325
326 /* update bitmask */
327 usedBufferMask |= destMask[output];
328 }
329 }
330
331 /* OK, if we get here, there were no errors so set the new state */
332 _mesa_drawbuffers(ctx, n, buffers, destMask);
333
334 /*
335 * Call device driver function.
336 */
337 if (ctx->Driver.DrawBuffers)
338 ctx->Driver.DrawBuffers(ctx, n, buffers);
339 else if (ctx->Driver.DrawBuffer)
340 ctx->Driver.DrawBuffer(ctx, buffers[0]);
341 }
342
343
344 /**
345 * Helper function to set the GL_DRAW_BUFFER state in the context and
346 * current FBO.
347 *
348 * All error checking will have been done prior to calling this function
349 * so nothing should go wrong at this point.
350 *
351 * \param ctx current context
352 * \param n number of color outputs to set
353 * \param buffers array[n] of colorbuffer names, like GL_LEFT.
354 * \param destMask array[n] of BUFFER_BIT_* bitmasks which correspond to the
355 * colorbuffer names. (i.e. GL_FRONT_AND_BACK =>
356 * BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT).
357 */
358 void
359 _mesa_drawbuffers(GLcontext *ctx, GLuint n, const GLenum *buffers,
360 const GLbitfield *destMask)
361 {
362 struct gl_framebuffer *fb = ctx->DrawBuffer;
363 GLbitfield mask[MAX_DRAW_BUFFERS];
364
365 if (!destMask) {
366 /* compute destMask values now */
367 const GLbitfield supportedMask = supported_buffer_bitmask(ctx, fb);
368 GLuint output;
369 for (output = 0; output < n; output++) {
370 mask[output] = draw_buffer_enum_to_bitmask(buffers[output]);
371 ASSERT(mask[output] != BAD_MASK);
372 mask[output] &= supportedMask;
373 }
374 destMask = mask;
375 }
376
377 if (n == 1) {
378 GLuint buf, count = 0;
379 /* init to -1 to help catch errors */
380 fb->_ColorDrawBufferIndexes[0] = -1;
381 for (buf = 0; buf < BUFFER_COUNT; buf++) {
382 if (destMask[0] & (1 << buf)) {
383 fb->_ColorDrawBufferIndexes[count] = buf;
384 count++;
385 }
386 }
387 fb->ColorDrawBuffer[0] = buffers[0];
388 fb->_NumColorDrawBuffers = count;
389 }
390 else {
391 GLuint buf, count = 0;
392 for (buf = 0; buf < n; buf++ ) {
393 if (destMask[buf]) {
394 fb->_ColorDrawBufferIndexes[buf] = _mesa_ffs(destMask[buf]) - 1;
395 fb->ColorDrawBuffer[buf] = buffers[buf];
396 count = buf + 1;
397 }
398 else {
399 fb->_ColorDrawBufferIndexes[buf] = -1;
400 }
401 }
402 /* set remaining outputs to -1 (GL_NONE) */
403 while (buf < ctx->Const.MaxDrawBuffers) {
404 fb->_ColorDrawBufferIndexes[buf] = -1;
405 fb->ColorDrawBuffer[buf] = GL_NONE;
406 buf++;
407 }
408 fb->_NumColorDrawBuffers = count;
409 }
410
411 if (fb->Name == 0) {
412 /* also set context drawbuffer state */
413 GLuint buf;
414 for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) {
415 ctx->Color.DrawBuffer[buf] = fb->ColorDrawBuffer[buf];
416 }
417 }
418
419 ctx->NewState |= _NEW_BUFFERS;
420 }
421
422
423 /**
424 * Like \sa _mesa_drawbuffers(), this is a helper function for setting
425 * GL_READ_BUFFER state in the context and current FBO.
426 * \param ctx the rendering context
427 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc.
428 * \param bufferIndex the numerical index corresponding to 'buffer'
429 */
430 void
431 _mesa_readbuffer(GLcontext *ctx, GLenum buffer, GLint bufferIndex)
432 {
433 struct gl_framebuffer *fb = ctx->ReadBuffer;
434
435 if (fb->Name == 0) {
436 /* Only update the per-context READ_BUFFER state if we're bound to
437 * a window-system framebuffer.
438 */
439 ctx->Pixel.ReadBuffer = buffer;
440 }
441
442 fb->ColorReadBuffer = buffer;
443 fb->_ColorReadBufferIndex = bufferIndex;
444
445 ctx->NewState |= _NEW_PIXEL;
446 }
447
448
449
450 /**
451 * Called by glReadBuffer to set the source renderbuffer for reading pixels.
452 * \param mode color buffer such as GL_FRONT, GL_BACK, etc.
453 */
454 void GLAPIENTRY
455 _mesa_ReadBuffer(GLenum buffer)
456 {
457 struct gl_framebuffer *fb;
458 GLbitfield supportedMask;
459 GLint srcBuffer;
460 GET_CURRENT_CONTEXT(ctx);
461 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
462
463 if (MESA_VERBOSE & VERBOSE_API)
464 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
465
466 fb = ctx->ReadBuffer;
467
468 if (MESA_VERBOSE & VERBOSE_API)
469 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer));
470
471 if (fb->Name > 0 && buffer == GL_NONE) {
472 /* This is legal for user-created framebuffer objects */
473 srcBuffer = -1;
474 }
475 else {
476 /* general case / window-system framebuffer */
477 srcBuffer = read_buffer_enum_to_index(buffer);
478 if (srcBuffer == -1) {
479 _mesa_error(ctx, GL_INVALID_ENUM,
480 "glReadBuffer(buffer=0x%x)", buffer);
481 return;
482 }
483 supportedMask = supported_buffer_bitmask(ctx, fb);
484 if (((1 << srcBuffer) & supportedMask) == 0) {
485 _mesa_error(ctx, GL_INVALID_OPERATION,
486 "glReadBuffer(buffer=0x%x)", buffer);
487 return;
488 }
489 }
490
491 /* OK, all error checking has been completed now */
492
493 _mesa_readbuffer(ctx, buffer, srcBuffer);
494 ctx->NewState |= _NEW_BUFFERS;
495
496 /*
497 * Call device driver function.
498 */
499 if (ctx->Driver.ReadBuffer)
500 (*ctx->Driver.ReadBuffer)(ctx, buffer);
501 }