Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
Brian Paul | 5722338 | 2006-04-06 04:09:03 +0000 | [diff] [blame] | 3 | * Version: 6.5.1 |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 4 | * |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2006 Brian Paul All Rights Reserved. |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 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 | * Functions for allocating/managing framebuffers and renderbuffers. |
| 28 | * Also, routines for reading/writing renderbuffer data as ubytes, |
| 29 | * ushorts, uints, etc. |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | #include "glheader.h" |
| 34 | #include "imports.h" |
| 35 | #include "context.h" |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 36 | #include "depthstencil.h" |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 37 | #include "mtypes.h" |
| 38 | #include "fbobject.h" |
| 39 | #include "framebuffer.h" |
| 40 | #include "renderbuffer.h" |
| 41 | |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Compute/set the _DepthMax field for the given framebuffer. |
| 46 | * This value depends on the Z buffer resolution. |
| 47 | */ |
| 48 | static void |
| 49 | compute_depth_max(struct gl_framebuffer *fb) |
| 50 | { |
| 51 | if (fb->Visual.depthBits == 0) { |
| 52 | /* Special case. Even if we don't have a depth buffer we need |
| 53 | * good values for DepthMax for Z vertex transformation purposes |
| 54 | * and for per-fragment fog computation. |
| 55 | */ |
| 56 | fb->_DepthMax = (1 << 16) - 1; |
| 57 | } |
| 58 | else if (fb->Visual.depthBits < 32) { |
| 59 | fb->_DepthMax = (1 << fb->Visual.depthBits) - 1; |
| 60 | } |
| 61 | else { |
| 62 | /* Special case since shift values greater than or equal to the |
| 63 | * number of bits in the left hand expression's type are undefined. |
| 64 | */ |
| 65 | fb->_DepthMax = 0xffffffff; |
| 66 | } |
| 67 | fb->_DepthMaxF = (GLfloat) fb->_DepthMax; |
| 68 | fb->_MRD = 1.0; /* Minimum resolvable depth value, for polygon offset */ |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 73 | * Set the framebuffer's _DepthBuffer field, taking care of |
| 74 | * reference counts, etc. |
| 75 | */ |
| 76 | static void |
| 77 | set_depth_renderbuffer(struct gl_framebuffer *fb, |
| 78 | struct gl_renderbuffer *rb) |
| 79 | { |
| 80 | if (fb->_DepthBuffer) { |
Brian | a510bc3 | 2007-03-06 10:07:59 -0700 | [diff] [blame] | 81 | _mesa_unreference_renderbuffer(&fb->_DepthBuffer); |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 82 | } |
| 83 | fb->_DepthBuffer = rb; |
| 84 | if (rb) { |
| 85 | rb->RefCount++; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Set the framebuffer's _StencilBuffer field, taking care of |
| 92 | * reference counts, etc. |
| 93 | */ |
| 94 | static void |
| 95 | set_stencil_renderbuffer(struct gl_framebuffer *fb, |
| 96 | struct gl_renderbuffer *rb) |
| 97 | { |
| 98 | if (fb->_StencilBuffer) { |
Brian | a510bc3 | 2007-03-06 10:07:59 -0700 | [diff] [blame] | 99 | _mesa_unreference_renderbuffer(&fb->_StencilBuffer); |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 100 | } |
| 101 | fb->_StencilBuffer = rb; |
| 102 | if (rb) { |
| 103 | rb->RefCount++; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 109 | * Create and initialize a gl_framebuffer object. |
| 110 | * This is intended for creating _window_system_ framebuffers, not generic |
| 111 | * framebuffer objects ala GL_EXT_framebuffer_object. |
| 112 | * |
| 113 | * \sa _mesa_new_framebuffer |
| 114 | */ |
| 115 | struct gl_framebuffer * |
| 116 | _mesa_create_framebuffer(const GLvisual *visual) |
| 117 | { |
| 118 | struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer); |
| 119 | assert(visual); |
| 120 | if (fb) { |
| 121 | _mesa_initialize_framebuffer(fb, visual); |
| 122 | } |
| 123 | return fb; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Allocate a new gl_framebuffer object. |
| 129 | * This is the default function for ctx->Driver.NewFramebuffer(). |
| 130 | * This is for allocating user-created framebuffers, not window-system |
| 131 | * framebuffers! |
| 132 | * \sa _mesa_create_framebuffer |
| 133 | */ |
| 134 | struct gl_framebuffer * |
| 135 | _mesa_new_framebuffer(GLcontext *ctx, GLuint name) |
| 136 | { |
| 137 | struct gl_framebuffer *fb; |
Brian Paul | 28b014e | 2006-04-05 03:05:17 +0000 | [diff] [blame] | 138 | (void) ctx; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 139 | assert(name != 0); |
| 140 | fb = CALLOC_STRUCT(gl_framebuffer); |
| 141 | if (fb) { |
| 142 | fb->Name = name; |
| 143 | fb->RefCount = 1; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 144 | fb->ColorDrawBuffer[0] = GL_COLOR_ATTACHMENT0_EXT; |
| 145 | fb->_ColorDrawBufferMask[0] = BUFFER_BIT_COLOR0; |
| 146 | fb->ColorReadBuffer = GL_COLOR_ATTACHMENT0_EXT; |
Brian Paul | 048b13d | 2005-09-23 03:22:20 +0000 | [diff] [blame] | 147 | fb->_ColorReadBufferIndex = BUFFER_COLOR0; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 148 | fb->Delete = _mesa_destroy_framebuffer; |
| 149 | } |
| 150 | return fb; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
Brian Paul | f084f60 | 2005-09-13 23:37:50 +0000 | [diff] [blame] | 155 | * Initialize a gl_framebuffer object. Typically used to initialize |
| 156 | * window system-created framebuffers, not user-created framebuffers. |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 157 | * \sa _mesa_create_framebuffer |
| 158 | */ |
| 159 | void |
| 160 | _mesa_initialize_framebuffer(struct gl_framebuffer *fb, const GLvisual *visual) |
| 161 | { |
| 162 | assert(fb); |
| 163 | assert(visual); |
| 164 | |
| 165 | _mesa_bzero(fb, sizeof(struct gl_framebuffer)); |
| 166 | |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 167 | _glthread_INIT_MUTEX(fb->Mutex); |
| 168 | |
Brian | e6a9381 | 2007-02-26 11:37:37 -0700 | [diff] [blame] | 169 | fb->RefCount = 1; |
| 170 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 171 | /* save the visual */ |
| 172 | fb->Visual = *visual; |
| 173 | |
| 174 | /* Init glRead/DrawBuffer state */ |
| 175 | if (visual->doubleBufferMode) { |
| 176 | fb->ColorDrawBuffer[0] = GL_BACK; |
| 177 | fb->_ColorDrawBufferMask[0] = BUFFER_BIT_BACK_LEFT; |
| 178 | fb->ColorReadBuffer = GL_BACK; |
Brian Paul | 048b13d | 2005-09-23 03:22:20 +0000 | [diff] [blame] | 179 | fb->_ColorReadBufferIndex = BUFFER_BACK_LEFT; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 180 | } |
| 181 | else { |
| 182 | fb->ColorDrawBuffer[0] = GL_FRONT; |
| 183 | fb->_ColorDrawBufferMask[0] = BUFFER_BIT_FRONT_LEFT; |
| 184 | fb->ColorReadBuffer = GL_FRONT; |
Brian Paul | 048b13d | 2005-09-23 03:22:20 +0000 | [diff] [blame] | 185 | fb->_ColorReadBufferIndex = BUFFER_FRONT_LEFT; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | fb->Delete = _mesa_destroy_framebuffer; |
Brian Paul | d95000d | 2005-09-28 15:46:46 +0000 | [diff] [blame] | 189 | fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 190 | |
| 191 | compute_depth_max(fb); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /** |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 196 | * Deallocate buffer and everything attached to it. |
Brian Paul | f084f60 | 2005-09-13 23:37:50 +0000 | [diff] [blame] | 197 | * Typically called via the gl_framebuffer->Delete() method. |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 198 | */ |
| 199 | void |
Brian Paul | f084f60 | 2005-09-13 23:37:50 +0000 | [diff] [blame] | 200 | _mesa_destroy_framebuffer(struct gl_framebuffer *fb) |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 201 | { |
Brian Paul | f084f60 | 2005-09-13 23:37:50 +0000 | [diff] [blame] | 202 | if (fb) { |
| 203 | _mesa_free_framebuffer_data(fb); |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 204 | _mesa_free(fb); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * Free all the data hanging off the given gl_framebuffer, but don't free |
| 211 | * the gl_framebuffer object itself. |
| 212 | */ |
| 213 | void |
| 214 | _mesa_free_framebuffer_data(struct gl_framebuffer *fb) |
| 215 | { |
| 216 | GLuint i; |
| 217 | |
| 218 | assert(fb); |
Brian | f30e312 | 2007-02-27 11:09:28 -0700 | [diff] [blame] | 219 | assert(fb->RefCount == 0); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 220 | |
Brian | e6a9381 | 2007-02-26 11:37:37 -0700 | [diff] [blame] | 221 | _glthread_DESTROY_MUTEX(fb->Mutex); |
| 222 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 223 | for (i = 0; i < BUFFER_COUNT; i++) { |
| 224 | struct gl_renderbuffer_attachment *att = &fb->Attachment[i]; |
Brian Paul | 753af3a | 2006-03-25 17:57:52 +0000 | [diff] [blame] | 225 | if (att->Renderbuffer) { |
Brian | a510bc3 | 2007-03-06 10:07:59 -0700 | [diff] [blame] | 226 | _mesa_unreference_renderbuffer(&att->Renderbuffer); |
Brian | 593802c | 2007-03-06 09:49:15 -0700 | [diff] [blame] | 227 | } |
| 228 | if (att->Texture) { |
| 229 | /* render to texture */ |
| 230 | att->Texture->RefCount--; |
| 231 | if (att->Texture->RefCount == 0) { |
| 232 | GET_CURRENT_CONTEXT(ctx); |
| 233 | if (ctx) { |
| 234 | ctx->Driver.DeleteTexture(ctx, att->Texture); |
| 235 | } |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | att->Type = GL_NONE; |
Brian | 593802c | 2007-03-06 09:49:15 -0700 | [diff] [blame] | 239 | att->Texture = NULL; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 240 | } |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 241 | |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 242 | /* unbind depth/stencil to decr ref counts */ |
| 243 | set_depth_renderbuffer(fb, NULL); |
| 244 | set_stencil_renderbuffer(fb, NULL); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
| 248 | /** |
Brian | a510bc3 | 2007-03-06 10:07:59 -0700 | [diff] [blame] | 249 | * Set *ptr to point to fb, with refcounting and locking. |
Brian Paul | 2eb88c17 | 2006-05-20 15:06:35 +0000 | [diff] [blame] | 250 | */ |
| 251 | void |
Brian | a510bc3 | 2007-03-06 10:07:59 -0700 | [diff] [blame] | 252 | _mesa_reference_framebuffer(struct gl_framebuffer **ptr, |
| 253 | struct gl_framebuffer *fb) |
Brian Paul | 2eb88c17 | 2006-05-20 15:06:35 +0000 | [diff] [blame] | 254 | { |
Brian | a510bc3 | 2007-03-06 10:07:59 -0700 | [diff] [blame] | 255 | assert(ptr); |
Brian | 1a6baf0 | 2007-03-06 16:26:02 -0700 | [diff] [blame^] | 256 | if (*ptr == fb) { |
| 257 | /* no change */ |
| 258 | return; |
| 259 | } |
| 260 | if (*ptr) { |
| 261 | _mesa_unreference_framebuffer(ptr); |
| 262 | } |
Brian | a510bc3 | 2007-03-06 10:07:59 -0700 | [diff] [blame] | 263 | assert(!*ptr); |
| 264 | assert(fb); |
| 265 | _glthread_LOCK_MUTEX(fb->Mutex); |
| 266 | fb->RefCount++; |
| 267 | _glthread_UNLOCK_MUTEX(fb->Mutex); |
| 268 | *ptr = fb; |
| 269 | } |
Brian Paul | 2eb88c17 | 2006-05-20 15:06:35 +0000 | [diff] [blame] | 270 | |
Brian | a510bc3 | 2007-03-06 10:07:59 -0700 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * Undo/remove a reference to a framebuffer object. |
| 274 | * Decrement the framebuffer object's reference count and delete it when |
| 275 | * the refcount hits zero. |
| 276 | * Note: we pass the address of a pointer and set it to NULL. |
| 277 | */ |
| 278 | void |
| 279 | _mesa_unreference_framebuffer(struct gl_framebuffer **fb) |
| 280 | { |
| 281 | assert(fb); |
| 282 | if (*fb) { |
| 283 | GLboolean deleteFlag = GL_FALSE; |
| 284 | |
| 285 | _glthread_LOCK_MUTEX((*fb)->Mutex); |
Brian Paul | 2eb88c17 | 2006-05-20 15:06:35 +0000 | [diff] [blame] | 286 | ASSERT((*fb)->RefCount > 0); |
| 287 | (*fb)->RefCount--; |
| 288 | deleteFlag = ((*fb)->RefCount == 0); |
Brian | a510bc3 | 2007-03-06 10:07:59 -0700 | [diff] [blame] | 289 | _glthread_UNLOCK_MUTEX((*fb)->Mutex); |
| 290 | |
| 291 | if (deleteFlag) |
| 292 | (*fb)->Delete(*fb); |
Brian Paul | 2eb88c17 | 2006-05-20 15:06:35 +0000 | [diff] [blame] | 293 | |
Brian Paul | 2eb88c17 | 2006-05-20 15:06:35 +0000 | [diff] [blame] | 294 | *fb = NULL; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | /** |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 302 | * Resize the given framebuffer's renderbuffers to the new width and height. |
| 303 | * This should only be used for window-system framebuffers, not |
| 304 | * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object). |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 305 | * This will typically be called via ctx->Driver.ResizeBuffers() or directly |
| 306 | * from a device driver. |
| 307 | * |
| 308 | * \note it's possible for ctx to be null since a window can be resized |
| 309 | * without a currently bound rendering context. |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 310 | */ |
| 311 | void |
| 312 | _mesa_resize_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb, |
| 313 | GLuint width, GLuint height) |
| 314 | { |
| 315 | GLuint i; |
| 316 | |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 317 | /* XXX I think we could check if the size is not changing |
| 318 | * and return early. |
| 319 | */ |
| 320 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 321 | /* For window system framebuffers, Name is zero */ |
| 322 | assert(fb->Name == 0); |
| 323 | |
| 324 | for (i = 0; i < BUFFER_COUNT; i++) { |
| 325 | struct gl_renderbuffer_attachment *att = &fb->Attachment[i]; |
| 326 | if (att->Type == GL_RENDERBUFFER_EXT && att->Renderbuffer) { |
| 327 | struct gl_renderbuffer *rb = att->Renderbuffer; |
| 328 | /* only resize if size is changing */ |
| 329 | if (rb->Width != width || rb->Height != height) { |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 330 | /* could just as well pass rb->_ActualFormat here */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 331 | if (rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) { |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 332 | ASSERT(rb->Width == width); |
| 333 | ASSERT(rb->Height == height); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 334 | } |
| 335 | else { |
| 336 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer"); |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 337 | /* no return */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 343 | if (fb->_DepthBuffer) { |
| 344 | struct gl_renderbuffer *rb = fb->_DepthBuffer; |
| 345 | if (rb->Width != width || rb->Height != height) { |
| 346 | if (!rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) { |
| 347 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer"); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (fb->_StencilBuffer) { |
| 353 | struct gl_renderbuffer *rb = fb->_StencilBuffer; |
| 354 | if (rb->Width != width || rb->Height != height) { |
| 355 | if (!rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) { |
| 356 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer"); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 361 | fb->Width = width; |
| 362 | fb->Height = height; |
Brian Paul | 52f686c | 2005-10-21 21:39:10 +0000 | [diff] [blame] | 363 | |
Brian Paul | 041d648 | 2006-10-16 17:26:30 +0000 | [diff] [blame] | 364 | if (ctx) { |
| 365 | /* update scissor / window bounds */ |
Alan Hourihane | feb0ff1 | 2006-06-21 10:58:04 +0000 | [diff] [blame] | 366 | _mesa_update_draw_buffer_bounds(ctx); |
Brian Paul | 041d648 | 2006-10-16 17:26:30 +0000 | [diff] [blame] | 367 | /* Signal new buffer state so that swrast will update its clipping |
| 368 | * info (the CLIP_BIT flag). |
| 369 | */ |
| 370 | ctx->NewState |= _NEW_BUFFERS; |
| 371 | } |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
| 375 | /** |
| 376 | * Examine all the framebuffer's renderbuffers to update the Width/Height |
| 377 | * fields of the framebuffer. If we have renderbuffers with different |
| 378 | * sizes, set the framebuffer's width and height to zero. |
| 379 | * Note: this is only intended for user-created framebuffers, not |
| 380 | * window-system framebuffes. |
| 381 | */ |
| 382 | static void |
| 383 | update_framebuffer_size(struct gl_framebuffer *fb) |
| 384 | { |
| 385 | GLboolean haveSize = GL_FALSE; |
| 386 | GLuint i; |
| 387 | |
| 388 | /* user-created framebuffers only */ |
| 389 | assert(fb->Name); |
| 390 | |
| 391 | for (i = 0; i < BUFFER_COUNT; i++) { |
| 392 | struct gl_renderbuffer_attachment *att = &fb->Attachment[i]; |
| 393 | const struct gl_renderbuffer *rb = att->Renderbuffer; |
| 394 | if (rb) { |
| 395 | if (haveSize) { |
| 396 | if (rb->Width != fb->Width && rb->Height != fb->Height) { |
| 397 | /* size mismatch! */ |
| 398 | fb->Width = 0; |
| 399 | fb->Height = 0; |
| 400 | return; |
| 401 | } |
| 402 | } |
| 403 | else { |
| 404 | fb->Width = rb->Width; |
| 405 | fb->Height = rb->Height; |
| 406 | haveSize = GL_TRUE; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | |
| 413 | /** |
| 414 | * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields. |
| 415 | * These values are computed from the buffer's width and height and |
| 416 | * the scissor box, if it's enabled. |
| 417 | * \param ctx the GL context. |
| 418 | */ |
| 419 | void |
| 420 | _mesa_update_draw_buffer_bounds(GLcontext *ctx) |
| 421 | { |
| 422 | struct gl_framebuffer *buffer = ctx->DrawBuffer; |
| 423 | |
Alan Hourihane | 161de10 | 2006-06-19 09:27:04 +0000 | [diff] [blame] | 424 | if (!buffer) |
| 425 | return; |
| 426 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 427 | if (buffer->Name) { |
| 428 | /* user-created framebuffer size depends on the renderbuffers */ |
| 429 | update_framebuffer_size(buffer); |
| 430 | } |
| 431 | |
| 432 | buffer->_Xmin = 0; |
| 433 | buffer->_Ymin = 0; |
| 434 | buffer->_Xmax = buffer->Width; |
| 435 | buffer->_Ymax = buffer->Height; |
| 436 | |
| 437 | if (ctx->Scissor.Enabled) { |
| 438 | if (ctx->Scissor.X > buffer->_Xmin) { |
| 439 | buffer->_Xmin = ctx->Scissor.X; |
| 440 | } |
| 441 | if (ctx->Scissor.Y > buffer->_Ymin) { |
| 442 | buffer->_Ymin = ctx->Scissor.Y; |
| 443 | } |
| 444 | if (ctx->Scissor.X + ctx->Scissor.Width < buffer->_Xmax) { |
| 445 | buffer->_Xmax = ctx->Scissor.X + ctx->Scissor.Width; |
| 446 | } |
| 447 | if (ctx->Scissor.Y + ctx->Scissor.Height < buffer->_Ymax) { |
| 448 | buffer->_Ymax = ctx->Scissor.Y + ctx->Scissor.Height; |
| 449 | } |
| 450 | /* finally, check for empty region */ |
| 451 | if (buffer->_Xmin > buffer->_Xmax) { |
| 452 | buffer->_Xmin = buffer->_Xmax; |
| 453 | } |
| 454 | if (buffer->_Ymin > buffer->_Ymax) { |
| 455 | buffer->_Ymin = buffer->_Ymax; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | ASSERT(buffer->_Xmin <= buffer->_Xmax); |
| 460 | ASSERT(buffer->_Ymin <= buffer->_Ymax); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | /** |
| 465 | * The glGet queries of the framebuffer red/green/blue size, stencil size, |
| 466 | * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can |
Brian Paul | f084f60 | 2005-09-13 23:37:50 +0000 | [diff] [blame] | 467 | * change depending on the renderbuffer bindings. This function updates |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 468 | * the given framebuffer's Visual from the current renderbuffer bindings. |
Brian Paul | 02aa5fb | 2006-09-11 15:04:23 +0000 | [diff] [blame] | 469 | * |
| 470 | * This may apply to user-created framebuffers or window system framebuffers. |
Brian Paul | eb063cf | 2005-10-04 14:48:24 +0000 | [diff] [blame] | 471 | * |
| 472 | * Also note: ctx->DrawBuffer->Visual.depthBits might not equal |
| 473 | * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits. |
| 474 | * The former one is used to convert floating point depth values into |
| 475 | * integer Z values. |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 476 | */ |
| 477 | void |
| 478 | _mesa_update_framebuffer_visual(struct gl_framebuffer *fb) |
| 479 | { |
Brian Paul | 474f28e | 2005-10-08 14:41:17 +0000 | [diff] [blame] | 480 | GLuint i; |
| 481 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 482 | _mesa_bzero(&fb->Visual, sizeof(fb->Visual)); |
Brian Paul | 474f28e | 2005-10-08 14:41:17 +0000 | [diff] [blame] | 483 | fb->Visual.rgbMode = GL_TRUE; /* assume this */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 484 | |
Brian Paul | 34b3b40 | 2006-04-20 00:45:08 +0000 | [diff] [blame] | 485 | #if 0 /* this _might_ be needed */ |
| 486 | if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 487 | /* leave visual fields zero'd */ |
| 488 | return; |
| 489 | } |
| 490 | #endif |
| 491 | |
Brian Paul | 474f28e | 2005-10-08 14:41:17 +0000 | [diff] [blame] | 492 | /* find first RGB or CI renderbuffer */ |
| 493 | for (i = 0; i < BUFFER_COUNT; i++) { |
| 494 | if (fb->Attachment[i].Renderbuffer) { |
| 495 | const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer; |
| 496 | if (rb->_BaseFormat == GL_RGBA || rb->_BaseFormat == GL_RGB) { |
| 497 | fb->Visual.redBits = rb->RedBits; |
| 498 | fb->Visual.greenBits = rb->GreenBits; |
| 499 | fb->Visual.blueBits = rb->BlueBits; |
| 500 | fb->Visual.alphaBits = rb->AlphaBits; |
| 501 | fb->Visual.rgbBits = fb->Visual.redBits |
| 502 | + fb->Visual.greenBits + fb->Visual.blueBits; |
| 503 | fb->Visual.floatMode = GL_FALSE; |
| 504 | break; |
| 505 | } |
| 506 | else if (rb->_BaseFormat == GL_COLOR_INDEX) { |
| 507 | fb->Visual.indexBits = rb->IndexBits; |
| 508 | fb->Visual.rgbMode = GL_FALSE; |
| 509 | break; |
| 510 | } |
| 511 | } |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) { |
| 515 | fb->Visual.haveDepthBuffer = GL_TRUE; |
| 516 | fb->Visual.depthBits |
Brian Paul | 676d0ac | 2005-09-22 05:19:57 +0000 | [diff] [blame] | 517 | = fb->Attachment[BUFFER_DEPTH].Renderbuffer->DepthBits; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | if (fb->Attachment[BUFFER_STENCIL].Renderbuffer) { |
| 521 | fb->Visual.haveStencilBuffer = GL_TRUE; |
| 522 | fb->Visual.stencilBits |
Brian Paul | 676d0ac | 2005-09-22 05:19:57 +0000 | [diff] [blame] | 523 | = fb->Attachment[BUFFER_STENCIL].Renderbuffer->StencilBits; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Brian Paul | 02aa5fb | 2006-09-11 15:04:23 +0000 | [diff] [blame] | 526 | if (fb->Attachment[BUFFER_ACCUM].Renderbuffer) { |
| 527 | fb->Visual.haveAccumBuffer = GL_TRUE; |
| 528 | fb->Visual.accumRedBits |
| 529 | = fb->Attachment[BUFFER_DEPTH].Renderbuffer->RedBits; |
| 530 | fb->Visual.accumGreenBits |
| 531 | = fb->Attachment[BUFFER_DEPTH].Renderbuffer->GreenBits; |
| 532 | fb->Visual.accumBlueBits |
| 533 | = fb->Attachment[BUFFER_DEPTH].Renderbuffer->BlueBits; |
| 534 | fb->Visual.accumAlphaBits |
| 535 | = fb->Attachment[BUFFER_DEPTH].Renderbuffer->AlphaBits; |
| 536 | } |
| 537 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 538 | compute_depth_max(fb); |
| 539 | } |
| 540 | |
| 541 | |
| 542 | /** |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 543 | * Update the framebuffer's _DepthBuffer field using the renderbuffer |
| 544 | * found at the given attachment index. |
| 545 | * |
| 546 | * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer, |
| 547 | * create and install a depth wrapper/adaptor. |
| 548 | * |
| 549 | * \param fb the framebuffer whose _DepthBuffer field to update |
| 550 | * \param attIndex indicates the renderbuffer to possibly wrap |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 551 | */ |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 552 | void |
| 553 | _mesa_update_depth_buffer(GLcontext *ctx, |
| 554 | struct gl_framebuffer *fb, |
| 555 | GLuint attIndex) |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 556 | { |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 557 | struct gl_renderbuffer *depthRb; |
| 558 | |
| 559 | /* only one possiblity for now */ |
| 560 | ASSERT(attIndex == BUFFER_DEPTH); |
| 561 | |
| 562 | depthRb = fb->Attachment[attIndex].Renderbuffer; |
| 563 | |
| 564 | if (depthRb && depthRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT) { |
| 565 | /* The attached depth buffer is a GL_DEPTH_STENCIL renderbuffer */ |
Brian Paul | 5722338 | 2006-04-06 04:09:03 +0000 | [diff] [blame] | 566 | if (!fb->_DepthBuffer |
| 567 | || fb->_DepthBuffer->Wrapped != depthRb |
| 568 | || fb->_DepthBuffer->_BaseFormat != GL_DEPTH_COMPONENT) { |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 569 | /* need to update wrapper */ |
| 570 | struct gl_renderbuffer *wrapper |
| 571 | = _mesa_new_z24_renderbuffer_wrapper(ctx, depthRb); |
| 572 | set_depth_renderbuffer(fb, wrapper); |
| 573 | ASSERT(fb->_DepthBuffer->Wrapped == depthRb); |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 576 | else { |
| 577 | /* depthRb may be null */ |
| 578 | set_depth_renderbuffer(fb, depthRb); |
| 579 | } |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 582 | |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 583 | /** |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 584 | * Update the framebuffer's _StencilBuffer field using the renderbuffer |
| 585 | * found at the given attachment index. |
| 586 | * |
| 587 | * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer, |
| 588 | * create and install a stencil wrapper/adaptor. |
| 589 | * |
| 590 | * \param fb the framebuffer whose _StencilBuffer field to update |
| 591 | * \param attIndex indicates the renderbuffer to possibly wrap |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 592 | */ |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 593 | void |
| 594 | _mesa_update_stencil_buffer(GLcontext *ctx, |
| 595 | struct gl_framebuffer *fb, |
| 596 | GLuint attIndex) |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 597 | { |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 598 | struct gl_renderbuffer *stencilRb; |
| 599 | |
| 600 | ASSERT(attIndex == BUFFER_DEPTH || |
| 601 | attIndex == BUFFER_STENCIL); |
| 602 | |
| 603 | stencilRb = fb->Attachment[attIndex].Renderbuffer; |
| 604 | |
| 605 | if (stencilRb && stencilRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT) { |
| 606 | /* The attached stencil buffer is a GL_DEPTH_STENCIL renderbuffer */ |
Brian Paul | 5722338 | 2006-04-06 04:09:03 +0000 | [diff] [blame] | 607 | if (!fb->_StencilBuffer |
| 608 | || fb->_StencilBuffer->Wrapped != stencilRb |
| 609 | || fb->_StencilBuffer->_BaseFormat != GL_STENCIL_INDEX) { |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 610 | /* need to update wrapper */ |
| 611 | struct gl_renderbuffer *wrapper |
| 612 | = _mesa_new_s8_renderbuffer_wrapper(ctx, stencilRb); |
| 613 | set_stencil_renderbuffer(fb, wrapper); |
| 614 | ASSERT(fb->_StencilBuffer->Wrapped == stencilRb); |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 617 | else { |
| 618 | /* stencilRb may be null */ |
| 619 | set_stencil_renderbuffer(fb, stencilRb); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | |
| 624 | /** |
| 625 | * Update the list of color drawing renderbuffer pointers. |
| 626 | * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers |
| 627 | * writing colors. |
| 628 | */ |
| 629 | static void |
| 630 | update_color_draw_buffers(GLcontext *ctx, struct gl_framebuffer *fb) |
| 631 | { |
| 632 | GLuint output; |
| 633 | |
| 634 | /* |
| 635 | * Fragment programs can write to multiple colorbuffers with |
| 636 | * the GL_ARB_draw_buffers extension. |
| 637 | */ |
| 638 | for (output = 0; output < ctx->Const.MaxDrawBuffers; output++) { |
| 639 | GLbitfield bufferMask = fb->_ColorDrawBufferMask[output]; |
| 640 | GLuint count = 0; |
| 641 | GLuint i; |
Brian | e6a9381 | 2007-02-26 11:37:37 -0700 | [diff] [blame] | 642 | if (!fb->DeletePending) { |
| 643 | /* We need the inner loop here because glDrawBuffer(GL_FRONT_AND_BACK) |
| 644 | * can specify writing to two or four color buffers (for example). |
| 645 | */ |
| 646 | for (i = 0; bufferMask && i < BUFFER_COUNT; i++) { |
| 647 | const GLuint bufferBit = 1 << i; |
| 648 | if (bufferBit & bufferMask) { |
| 649 | struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer; |
| 650 | if (rb) { |
| 651 | fb->_ColorDrawBuffers[output][count] = rb; |
| 652 | count++; |
| 653 | } |
| 654 | else { |
| 655 | /* |
| 656 | _mesa_warning(ctx, "DrawBuffer names a missing buffer!\n"); |
| 657 | */ |
| 658 | } |
| 659 | bufferMask &= ~bufferBit; |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 660 | } |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | fb->_NumColorDrawBuffers[output] = count; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | |
| 668 | /** |
| 669 | * Update the color read renderbuffer pointer. |
| 670 | * Unlike the DrawBuffer, we can only read from one (or zero) color buffers. |
| 671 | */ |
| 672 | static void |
| 673 | update_color_read_buffer(GLcontext *ctx, struct gl_framebuffer *fb) |
| 674 | { |
Brian Paul | 28b014e | 2006-04-05 03:05:17 +0000 | [diff] [blame] | 675 | (void) ctx; |
Brian | e6a9381 | 2007-02-26 11:37:37 -0700 | [diff] [blame] | 676 | if (fb->_ColorReadBufferIndex == -1 || fb->DeletePending) { |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 677 | fb->_ColorReadBuffer = NULL; /* legal! */ |
| 678 | } |
| 679 | else { |
| 680 | ASSERT(fb->_ColorReadBufferIndex >= 0); |
| 681 | ASSERT(fb->_ColorReadBufferIndex < BUFFER_COUNT); |
| 682 | fb->_ColorReadBuffer |
| 683 | = fb->Attachment[fb->_ColorReadBufferIndex].Renderbuffer; |
| 684 | } |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | |
| 688 | /** |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 689 | * Update state related to the current draw/read framebuffers. |
Brian Paul | 5278359 | 2005-08-31 21:38:53 +0000 | [diff] [blame] | 690 | * Specifically, update these framebuffer fields: |
| 691 | * _ColorDrawBuffers |
Brian Paul | 5278359 | 2005-08-31 21:38:53 +0000 | [diff] [blame] | 692 | * _NumColorDrawBuffers |
| 693 | * _ColorReadBuffer |
Brian Paul | 0f29ef6 | 2005-11-16 04:17:20 +0000 | [diff] [blame] | 694 | * _DepthBuffer |
| 695 | * _StencilBuffer |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 696 | * If the current framebuffer is user-created, make sure it's complete. |
Brian Paul | 5278359 | 2005-08-31 21:38:53 +0000 | [diff] [blame] | 697 | * The following functions can effect this state: glReadBuffer, |
Brian Paul | 0f29ef6 | 2005-11-16 04:17:20 +0000 | [diff] [blame] | 698 | * glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT, |
| 699 | * glRenderbufferStorageEXT. |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 700 | */ |
| 701 | void |
| 702 | _mesa_update_framebuffer(GLcontext *ctx) |
| 703 | { |
| 704 | struct gl_framebuffer *fb = ctx->DrawBuffer; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 705 | |
| 706 | /* Completeness only matters for user-created framebuffers */ |
Brian Paul | 474f28e | 2005-10-08 14:41:17 +0000 | [diff] [blame] | 707 | if (fb->Name != 0) { |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 708 | _mesa_test_framebuffer_completeness(ctx, fb); |
Brian Paul | 474f28e | 2005-10-08 14:41:17 +0000 | [diff] [blame] | 709 | _mesa_update_framebuffer_visual(fb); |
| 710 | } |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 711 | |
Brian Paul | ea4fe66 | 2006-03-26 05:22:17 +0000 | [diff] [blame] | 712 | update_color_draw_buffers(ctx, fb); |
| 713 | update_color_read_buffer(ctx, fb); |
| 714 | _mesa_update_depth_buffer(ctx, fb, BUFFER_DEPTH); |
| 715 | _mesa_update_stencil_buffer(ctx, fb, BUFFER_STENCIL); |
Brian Paul | 8471604 | 2005-11-16 04:05:54 +0000 | [diff] [blame] | 716 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 717 | compute_depth_max(fb); |
| 718 | } |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 719 | |
| 720 | |
| 721 | /** |
| 722 | * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels, |
| 723 | * glCopyTex[Sub]Image, etc. exists. |
| 724 | * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA, |
| 725 | * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL. |
| 726 | * \return GL_TRUE if buffer exists, GL_FALSE otherwise |
| 727 | */ |
| 728 | GLboolean |
| 729 | _mesa_source_buffer_exists(GLcontext *ctx, GLenum format) |
| 730 | { |
| 731 | const struct gl_renderbuffer_attachment *att |
| 732 | = ctx->ReadBuffer->Attachment; |
| 733 | |
| 734 | if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 735 | return GL_FALSE; |
| 736 | } |
| 737 | |
| 738 | switch (format) { |
| 739 | case GL_COLOR: |
Brian Paul | 590cd26 | 2006-03-24 22:53:00 +0000 | [diff] [blame] | 740 | case GL_RED: |
| 741 | case GL_GREEN: |
| 742 | case GL_BLUE: |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 743 | case GL_ALPHA: |
| 744 | case GL_LUMINANCE: |
| 745 | case GL_LUMINANCE_ALPHA: |
| 746 | case GL_INTENSITY: |
| 747 | case GL_RGB: |
Brian Paul | 590cd26 | 2006-03-24 22:53:00 +0000 | [diff] [blame] | 748 | case GL_BGR: |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 749 | case GL_RGBA: |
Brian Paul | 590cd26 | 2006-03-24 22:53:00 +0000 | [diff] [blame] | 750 | case GL_BGRA: |
| 751 | case GL_ABGR_EXT: |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 752 | case GL_COLOR_INDEX: |
| 753 | if (ctx->ReadBuffer->_ColorReadBuffer == NULL) { |
| 754 | return GL_FALSE; |
| 755 | } |
Brian Paul | 84c5d0a | 2006-03-30 16:29:41 +0000 | [diff] [blame] | 756 | /* XXX enable this post 6.5 release: |
| 757 | ASSERT(ctx->ReadBuffer->_ColorReadBuffer->RedBits > 0 || |
| 758 | ctx->ReadBuffer->_ColorReadBuffer->IndexBits > 0); |
| 759 | */ |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 760 | break; |
| 761 | case GL_DEPTH: |
| 762 | case GL_DEPTH_COMPONENT: |
| 763 | if (!att[BUFFER_DEPTH].Renderbuffer) { |
| 764 | return GL_FALSE; |
| 765 | } |
| 766 | ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0); |
| 767 | break; |
| 768 | case GL_STENCIL: |
| 769 | case GL_STENCIL_INDEX: |
| 770 | if (!att[BUFFER_STENCIL].Renderbuffer) { |
| 771 | return GL_FALSE; |
| 772 | } |
| 773 | ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0); |
| 774 | break; |
| 775 | case GL_DEPTH_STENCIL_EXT: |
| 776 | if (!att[BUFFER_DEPTH].Renderbuffer || |
| 777 | !att[BUFFER_STENCIL].Renderbuffer) { |
| 778 | return GL_FALSE; |
| 779 | } |
| 780 | ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0); |
| 781 | ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0); |
| 782 | break; |
| 783 | default: |
| 784 | _mesa_problem(ctx, |
Brian Paul | 590cd26 | 2006-03-24 22:53:00 +0000 | [diff] [blame] | 785 | "Unexpected format 0x%x in _mesa_source_buffer_exists", |
| 786 | format); |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 787 | return GL_FALSE; |
| 788 | } |
| 789 | |
| 790 | /* OK */ |
| 791 | return GL_TRUE; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | /** |
| 796 | * As above, but for drawing operations. |
| 797 | * XXX code do some code merging w/ above function. |
| 798 | */ |
| 799 | GLboolean |
| 800 | _mesa_dest_buffer_exists(GLcontext *ctx, GLenum format) |
| 801 | { |
| 802 | const struct gl_renderbuffer_attachment *att |
| 803 | = ctx->ReadBuffer->Attachment; |
| 804 | |
| 805 | if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 806 | return GL_FALSE; |
| 807 | } |
| 808 | |
| 809 | switch (format) { |
| 810 | case GL_COLOR: |
Brian Paul | 590cd26 | 2006-03-24 22:53:00 +0000 | [diff] [blame] | 811 | case GL_RED: |
| 812 | case GL_GREEN: |
| 813 | case GL_BLUE: |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 814 | case GL_ALPHA: |
| 815 | case GL_LUMINANCE: |
| 816 | case GL_LUMINANCE_ALPHA: |
| 817 | case GL_INTENSITY: |
| 818 | case GL_RGB: |
Brian Paul | 590cd26 | 2006-03-24 22:53:00 +0000 | [diff] [blame] | 819 | case GL_BGR: |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 820 | case GL_RGBA: |
Brian Paul | 590cd26 | 2006-03-24 22:53:00 +0000 | [diff] [blame] | 821 | case GL_BGRA: |
| 822 | case GL_ABGR_EXT: |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 823 | case GL_COLOR_INDEX: |
| 824 | /* nothing special */ |
Brian Paul | 84c5d0a | 2006-03-30 16:29:41 +0000 | [diff] [blame] | 825 | /* Could assert that colorbuffer has RedBits > 0 */ |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 826 | break; |
| 827 | case GL_DEPTH: |
| 828 | case GL_DEPTH_COMPONENT: |
| 829 | if (!att[BUFFER_DEPTH].Renderbuffer) { |
| 830 | return GL_FALSE; |
| 831 | } |
| 832 | ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0); |
| 833 | break; |
| 834 | case GL_STENCIL: |
| 835 | case GL_STENCIL_INDEX: |
| 836 | if (!att[BUFFER_STENCIL].Renderbuffer) { |
| 837 | return GL_FALSE; |
| 838 | } |
| 839 | ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0); |
| 840 | break; |
| 841 | case GL_DEPTH_STENCIL_EXT: |
| 842 | if (!att[BUFFER_DEPTH].Renderbuffer || |
| 843 | !att[BUFFER_STENCIL].Renderbuffer) { |
| 844 | return GL_FALSE; |
| 845 | } |
| 846 | ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0); |
| 847 | ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0); |
| 848 | break; |
| 849 | default: |
| 850 | _mesa_problem(ctx, |
Brian Paul | 590cd26 | 2006-03-24 22:53:00 +0000 | [diff] [blame] | 851 | "Unexpected format 0x%x in _mesa_source_buffer_exists", |
| 852 | format); |
Brian Paul | 7275d4d | 2006-03-20 15:25:18 +0000 | [diff] [blame] | 853 | return GL_FALSE; |
| 854 | } |
| 855 | |
| 856 | /* OK */ |
| 857 | return GL_TRUE; |
| 858 | } |