buffers.c revision 158d42c8b08411d761fa40299f3f29027ad3905f
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 */ 53static GLbitfield 54supported_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 */ 94static GLbitfield 95draw_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 */ 154static GLint 155read_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 */ 225void 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 */ 279void 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/** 345 * Helper function to set the GL_DRAW_BUFFER state in the context and 346 * current FBO. Called via glDrawBuffer(), glDrawBuffersARB() 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 */ 358void 359_mesa_drawbuffers(struct gl_context *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 GLboolean newState = GL_FALSE; 365 366 if (!destMask) { 367 /* compute destMask values now */ 368 const GLbitfield supportedMask = supported_buffer_bitmask(ctx, fb); 369 GLuint output; 370 for (output = 0; output < n; output++) { 371 mask[output] = draw_buffer_enum_to_bitmask(buffers[output]); 372 ASSERT(mask[output] != BAD_MASK); 373 mask[output] &= supportedMask; 374 } 375 destMask = mask; 376 } 377 378 /* 379 * If n==1, destMask[0] may have up to four bits set. 380 * Otherwise, destMask[x] can only have one bit set. 381 */ 382 if (n == 1) { 383 GLuint count = 0, destMask0 = destMask[0]; 384 while (destMask0) { 385 GLint bufIndex = _mesa_ffs(destMask0) - 1; 386 if (fb->_ColorDrawBufferIndexes[count] != bufIndex) { 387 fb->_ColorDrawBufferIndexes[count] = bufIndex; 388 newState = GL_TRUE; 389 } 390 count++; 391 destMask0 &= ~(1 << bufIndex); 392 } 393 fb->ColorDrawBuffer[0] = buffers[0]; 394 if (fb->_NumColorDrawBuffers != count) { 395 fb->_NumColorDrawBuffers = count; 396 newState = GL_TRUE; 397 } 398 } 399 else { 400 GLuint buf, count = 0; 401 for (buf = 0; buf < n; buf++ ) { 402 if (destMask[buf]) { 403 GLint bufIndex = _mesa_ffs(destMask[buf]) - 1; 404 /* only one bit should be set in the destMask[buf] field */ 405 ASSERT(_mesa_bitcount(destMask[buf]) == 1); 406 if (fb->_ColorDrawBufferIndexes[buf] != bufIndex) { 407 fb->_ColorDrawBufferIndexes[buf] = bufIndex; 408 newState = GL_TRUE; 409 } 410 count = buf + 1; 411 } 412 else { 413 if (fb->_ColorDrawBufferIndexes[buf] != -1) { 414 fb->_ColorDrawBufferIndexes[buf] = -1; 415 newState = GL_TRUE; 416 } 417 } 418 fb->ColorDrawBuffer[buf] = buffers[buf]; 419 } 420 /* set remaining outputs to -1 (GL_NONE) */ 421 while (buf < ctx->Const.MaxDrawBuffers) { 422 if (fb->_ColorDrawBufferIndexes[buf] != -1) { 423 fb->_ColorDrawBufferIndexes[buf] = -1; 424 newState = GL_TRUE; 425 } 426 fb->ColorDrawBuffer[buf] = GL_NONE; 427 buf++; 428 } 429 fb->_NumColorDrawBuffers = count; 430 } 431 432 if (fb->Name == 0) { 433 /* also set context drawbuffer state */ 434 GLuint buf; 435 for (buf = 0; buf < ctx->Const.MaxDrawBuffers; buf++) { 436 if (ctx->Color.DrawBuffer[buf] != fb->ColorDrawBuffer[buf]) { 437 ctx->Color.DrawBuffer[buf] = fb->ColorDrawBuffer[buf]; 438 newState = GL_TRUE; 439 } 440 } 441 } 442 443 if (newState) 444 FLUSH_VERTICES(ctx, _NEW_BUFFERS); 445} 446 447 448/** 449 * Like \sa _mesa_drawbuffers(), this is a helper function for setting 450 * GL_READ_BUFFER state in the context and current FBO. 451 * \param ctx the rendering context 452 * \param buffer GL_FRONT, GL_BACK, GL_COLOR_ATTACHMENT0, etc. 453 * \param bufferIndex the numerical index corresponding to 'buffer' 454 */ 455void 456_mesa_readbuffer(struct gl_context *ctx, GLenum buffer, GLint bufferIndex) 457{ 458 struct gl_framebuffer *fb = ctx->ReadBuffer; 459 460 if (fb->Name == 0) { 461 /* Only update the per-context READ_BUFFER state if we're bound to 462 * a window-system framebuffer. 463 */ 464 ctx->Pixel.ReadBuffer = buffer; 465 } 466 467 fb->ColorReadBuffer = buffer; 468 fb->_ColorReadBufferIndex = bufferIndex; 469 470 ctx->NewState |= _NEW_BUFFERS; 471} 472 473 474 475/** 476 * Called by glReadBuffer to set the source renderbuffer for reading pixels. 477 * \param mode color buffer such as GL_FRONT, GL_BACK, etc. 478 */ 479void GLAPIENTRY 480_mesa_ReadBuffer(GLenum buffer) 481{ 482 struct gl_framebuffer *fb; 483 GLbitfield supportedMask; 484 GLint srcBuffer; 485 GET_CURRENT_CONTEXT(ctx); 486 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); 487 488 if (MESA_VERBOSE & VERBOSE_API) 489 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer)); 490 491 fb = ctx->ReadBuffer; 492 493 if (MESA_VERBOSE & VERBOSE_API) 494 _mesa_debug(ctx, "glReadBuffer %s\n", _mesa_lookup_enum_by_nr(buffer)); 495 496 if (fb->Name > 0 && buffer == GL_NONE) { 497 /* This is legal for user-created framebuffer objects */ 498 srcBuffer = -1; 499 } 500 else { 501 /* general case / window-system framebuffer */ 502 srcBuffer = read_buffer_enum_to_index(buffer); 503 if (srcBuffer == -1) { 504 _mesa_error(ctx, GL_INVALID_ENUM, 505 "glReadBuffer(buffer=0x%x)", buffer); 506 return; 507 } 508 supportedMask = supported_buffer_bitmask(ctx, fb); 509 if (((1 << srcBuffer) & supportedMask) == 0) { 510 _mesa_error(ctx, GL_INVALID_OPERATION, 511 "glReadBuffer(buffer=0x%x)", buffer); 512 return; 513 } 514 } 515 516 /* OK, all error checking has been completed now */ 517 518 _mesa_readbuffer(ctx, buffer, srcBuffer); 519 ctx->NewState |= _NEW_BUFFERS; 520 521 /* 522 * Call device driver function. 523 */ 524 if (ctx->Driver.ReadBuffer) 525 (*ctx->Driver.ReadBuffer)(ctx, buffer); 526} 527