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