blob: ead7d46ea74ad033b8cfec752e7f54d53bf1a446 [file] [log] [blame]
Brian Paule4b23562005-05-04 20:11:35 +00001/*
2 * Mesa 3-D graphics library
Brian Paul57223382006-04-06 04:09:03 +00003 * Version: 6.5.1
Brian Paule4b23562005-05-04 20:11:35 +00004 *
Brian Paul7275d4d2006-03-20 15:25:18 +00005 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
Brian Paule4b23562005-05-04 20:11:35 +00006 *
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 Paul84716042005-11-16 04:05:54 +000036#include "depthstencil.h"
Brian Paule4b23562005-05-04 20:11:35 +000037#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 */
48static void
49compute_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 Paulea4fe662006-03-26 05:22:17 +000073 * Set the framebuffer's _DepthBuffer field, taking care of
74 * reference counts, etc.
75 */
76static void
77set_depth_renderbuffer(struct gl_framebuffer *fb,
78 struct gl_renderbuffer *rb)
79{
80 if (fb->_DepthBuffer) {
81 fb->_DepthBuffer->RefCount--;
82 if (fb->_DepthBuffer->RefCount <= 0) {
83 fb->_DepthBuffer->Delete(fb->_DepthBuffer);
84 }
85 }
86 fb->_DepthBuffer = rb;
87 if (rb) {
88 rb->RefCount++;
89 }
90}
91
92
93/**
94 * Set the framebuffer's _StencilBuffer field, taking care of
95 * reference counts, etc.
96 */
97static void
98set_stencil_renderbuffer(struct gl_framebuffer *fb,
99 struct gl_renderbuffer *rb)
100{
101 if (fb->_StencilBuffer) {
102 fb->_StencilBuffer->RefCount--;
103 if (fb->_StencilBuffer->RefCount <= 0) {
104 fb->_StencilBuffer->Delete(fb->_StencilBuffer);
105 }
106 }
107 fb->_StencilBuffer = rb;
108 if (rb) {
109 rb->RefCount++;
110 }
111}
112
113
114/**
Brian Paule4b23562005-05-04 20:11:35 +0000115 * Create and initialize a gl_framebuffer object.
116 * This is intended for creating _window_system_ framebuffers, not generic
117 * framebuffer objects ala GL_EXT_framebuffer_object.
118 *
119 * \sa _mesa_new_framebuffer
120 */
121struct gl_framebuffer *
122_mesa_create_framebuffer(const GLvisual *visual)
123{
124 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
125 assert(visual);
126 if (fb) {
127 _mesa_initialize_framebuffer(fb, visual);
128 }
129 return fb;
130}
131
132
133/**
134 * Allocate a new gl_framebuffer object.
135 * This is the default function for ctx->Driver.NewFramebuffer().
136 * This is for allocating user-created framebuffers, not window-system
137 * framebuffers!
138 * \sa _mesa_create_framebuffer
139 */
140struct gl_framebuffer *
141_mesa_new_framebuffer(GLcontext *ctx, GLuint name)
142{
143 struct gl_framebuffer *fb;
Brian Paul28b014e2006-04-05 03:05:17 +0000144 (void) ctx;
Brian Paule4b23562005-05-04 20:11:35 +0000145 assert(name != 0);
146 fb = CALLOC_STRUCT(gl_framebuffer);
147 if (fb) {
148 fb->Name = name;
149 fb->RefCount = 1;
Brian Paule4b23562005-05-04 20:11:35 +0000150 fb->ColorDrawBuffer[0] = GL_COLOR_ATTACHMENT0_EXT;
151 fb->_ColorDrawBufferMask[0] = BUFFER_BIT_COLOR0;
152 fb->ColorReadBuffer = GL_COLOR_ATTACHMENT0_EXT;
Brian Paul048b13d2005-09-23 03:22:20 +0000153 fb->_ColorReadBufferIndex = BUFFER_COLOR0;
Brian Paule4b23562005-05-04 20:11:35 +0000154 fb->Delete = _mesa_destroy_framebuffer;
155 }
156 return fb;
157}
158
159
160/**
Brian Paulf084f602005-09-13 23:37:50 +0000161 * Initialize a gl_framebuffer object. Typically used to initialize
162 * window system-created framebuffers, not user-created framebuffers.
Brian Paule4b23562005-05-04 20:11:35 +0000163 * \sa _mesa_create_framebuffer
164 */
165void
166_mesa_initialize_framebuffer(struct gl_framebuffer *fb, const GLvisual *visual)
167{
168 assert(fb);
169 assert(visual);
170
171 _mesa_bzero(fb, sizeof(struct gl_framebuffer));
172
Brian Paulea4fe662006-03-26 05:22:17 +0000173 _glthread_INIT_MUTEX(fb->Mutex);
174
Brian Paule4b23562005-05-04 20:11:35 +0000175 /* save the visual */
176 fb->Visual = *visual;
177
178 /* Init glRead/DrawBuffer state */
179 if (visual->doubleBufferMode) {
180 fb->ColorDrawBuffer[0] = GL_BACK;
181 fb->_ColorDrawBufferMask[0] = BUFFER_BIT_BACK_LEFT;
182 fb->ColorReadBuffer = GL_BACK;
Brian Paul048b13d2005-09-23 03:22:20 +0000183 fb->_ColorReadBufferIndex = BUFFER_BACK_LEFT;
Brian Paule4b23562005-05-04 20:11:35 +0000184 }
185 else {
186 fb->ColorDrawBuffer[0] = GL_FRONT;
187 fb->_ColorDrawBufferMask[0] = BUFFER_BIT_FRONT_LEFT;
188 fb->ColorReadBuffer = GL_FRONT;
Brian Paul048b13d2005-09-23 03:22:20 +0000189 fb->_ColorReadBufferIndex = BUFFER_FRONT_LEFT;
Brian Paule4b23562005-05-04 20:11:35 +0000190 }
191
192 fb->Delete = _mesa_destroy_framebuffer;
Brian Pauld95000d2005-09-28 15:46:46 +0000193 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
Brian Paule4b23562005-05-04 20:11:35 +0000194
195 compute_depth_max(fb);
196}
197
198
199/**
Brian Paule4b23562005-05-04 20:11:35 +0000200 * Deallocate buffer and everything attached to it.
Brian Paulf084f602005-09-13 23:37:50 +0000201 * Typically called via the gl_framebuffer->Delete() method.
Brian Paule4b23562005-05-04 20:11:35 +0000202 */
203void
Brian Paulf084f602005-09-13 23:37:50 +0000204_mesa_destroy_framebuffer(struct gl_framebuffer *fb)
Brian Paule4b23562005-05-04 20:11:35 +0000205{
Brian Paulf084f602005-09-13 23:37:50 +0000206 if (fb) {
Brian Paulea4fe662006-03-26 05:22:17 +0000207 _glthread_DESTROY_MUTEX(fb->Mutex);
Brian Paulf084f602005-09-13 23:37:50 +0000208 _mesa_free_framebuffer_data(fb);
Brian Paul84716042005-11-16 04:05:54 +0000209 _mesa_free(fb);
Brian Paule4b23562005-05-04 20:11:35 +0000210 }
211}
212
213
214/**
215 * Free all the data hanging off the given gl_framebuffer, but don't free
216 * the gl_framebuffer object itself.
217 */
218void
219_mesa_free_framebuffer_data(struct gl_framebuffer *fb)
220{
221 GLuint i;
222
223 assert(fb);
224
225 for (i = 0; i < BUFFER_COUNT; i++) {
226 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
Brian Paul753af3a2006-03-25 17:57:52 +0000227 if (att->Renderbuffer) {
Brian Paule4b23562005-05-04 20:11:35 +0000228 struct gl_renderbuffer *rb = att->Renderbuffer;
Brian Paulea4fe662006-03-26 05:22:17 +0000229 _glthread_LOCK_MUTEX(rb->Mutex);
Brian Paule4b23562005-05-04 20:11:35 +0000230 rb->RefCount--;
Brian Paulea4fe662006-03-26 05:22:17 +0000231 _glthread_UNLOCK_MUTEX(rb->Mutex);
Brian Paule4b23562005-05-04 20:11:35 +0000232 if (rb->RefCount == 0) {
233 rb->Delete(rb);
234 }
235 }
236 att->Type = GL_NONE;
237 att->Renderbuffer = NULL;
238 }
Brian Paul84716042005-11-16 04:05:54 +0000239
Brian Paulea4fe662006-03-26 05:22:17 +0000240 /* unbind depth/stencil to decr ref counts */
241 set_depth_renderbuffer(fb, NULL);
242 set_stencil_renderbuffer(fb, NULL);
Brian Paule4b23562005-05-04 20:11:35 +0000243}
244
245
246/**
247 * Resize the given framebuffer's renderbuffers to the new width and height.
248 * This should only be used for window-system framebuffers, not
249 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
Brian Paul84716042005-11-16 04:05:54 +0000250 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
251 * from a device driver.
252 *
253 * \note it's possible for ctx to be null since a window can be resized
254 * without a currently bound rendering context.
Brian Paule4b23562005-05-04 20:11:35 +0000255 */
256void
257_mesa_resize_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb,
258 GLuint width, GLuint height)
259{
260 GLuint i;
261
Brian Paulea4fe662006-03-26 05:22:17 +0000262 /* XXX I think we could check if the size is not changing
263 * and return early.
264 */
265
Brian Paule4b23562005-05-04 20:11:35 +0000266 /* For window system framebuffers, Name is zero */
267 assert(fb->Name == 0);
268
269 for (i = 0; i < BUFFER_COUNT; i++) {
270 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
271 if (att->Type == GL_RENDERBUFFER_EXT && att->Renderbuffer) {
272 struct gl_renderbuffer *rb = att->Renderbuffer;
273 /* only resize if size is changing */
274 if (rb->Width != width || rb->Height != height) {
Brian Paulea4fe662006-03-26 05:22:17 +0000275 /* could just as well pass rb->_ActualFormat here */
Brian Paule4b23562005-05-04 20:11:35 +0000276 if (rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
Brian Paulea4fe662006-03-26 05:22:17 +0000277 ASSERT(rb->Width == width);
278 ASSERT(rb->Height == height);
Brian Paule4b23562005-05-04 20:11:35 +0000279 }
280 else {
281 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
Brian Paulea4fe662006-03-26 05:22:17 +0000282 /* no return */
Brian Paule4b23562005-05-04 20:11:35 +0000283 }
284 }
285 }
286 }
287
Brian Paulea4fe662006-03-26 05:22:17 +0000288 if (fb->_DepthBuffer) {
289 struct gl_renderbuffer *rb = fb->_DepthBuffer;
290 if (rb->Width != width || rb->Height != height) {
291 if (!rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
292 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
293 }
294 }
295 }
296
297 if (fb->_StencilBuffer) {
298 struct gl_renderbuffer *rb = fb->_StencilBuffer;
299 if (rb->Width != width || rb->Height != height) {
300 if (!rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
301 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
302 }
303 }
304 }
305
Brian Paule4b23562005-05-04 20:11:35 +0000306 fb->Width = width;
307 fb->Height = height;
Brian Paul52f686c2005-10-21 21:39:10 +0000308
309 /* to update scissor / window bounds */
Brian Paulea4fe662006-03-26 05:22:17 +0000310 _mesa_update_draw_buffer_bounds(ctx);
Brian Paule4b23562005-05-04 20:11:35 +0000311}
312
313
314/**
315 * Examine all the framebuffer's renderbuffers to update the Width/Height
316 * fields of the framebuffer. If we have renderbuffers with different
317 * sizes, set the framebuffer's width and height to zero.
318 * Note: this is only intended for user-created framebuffers, not
319 * window-system framebuffes.
320 */
321static void
322update_framebuffer_size(struct gl_framebuffer *fb)
323{
324 GLboolean haveSize = GL_FALSE;
325 GLuint i;
326
327 /* user-created framebuffers only */
328 assert(fb->Name);
329
330 for (i = 0; i < BUFFER_COUNT; i++) {
331 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
332 const struct gl_renderbuffer *rb = att->Renderbuffer;
333 if (rb) {
334 if (haveSize) {
335 if (rb->Width != fb->Width && rb->Height != fb->Height) {
336 /* size mismatch! */
337 fb->Width = 0;
338 fb->Height = 0;
339 return;
340 }
341 }
342 else {
343 fb->Width = rb->Width;
344 fb->Height = rb->Height;
345 haveSize = GL_TRUE;
346 }
347 }
348 }
349}
350
351
352/**
353 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
354 * These values are computed from the buffer's width and height and
355 * the scissor box, if it's enabled.
356 * \param ctx the GL context.
357 */
358void
359_mesa_update_draw_buffer_bounds(GLcontext *ctx)
360{
361 struct gl_framebuffer *buffer = ctx->DrawBuffer;
362
363 if (buffer->Name) {
364 /* user-created framebuffer size depends on the renderbuffers */
365 update_framebuffer_size(buffer);
366 }
367
368 buffer->_Xmin = 0;
369 buffer->_Ymin = 0;
370 buffer->_Xmax = buffer->Width;
371 buffer->_Ymax = buffer->Height;
372
373 if (ctx->Scissor.Enabled) {
374 if (ctx->Scissor.X > buffer->_Xmin) {
375 buffer->_Xmin = ctx->Scissor.X;
376 }
377 if (ctx->Scissor.Y > buffer->_Ymin) {
378 buffer->_Ymin = ctx->Scissor.Y;
379 }
380 if (ctx->Scissor.X + ctx->Scissor.Width < buffer->_Xmax) {
381 buffer->_Xmax = ctx->Scissor.X + ctx->Scissor.Width;
382 }
383 if (ctx->Scissor.Y + ctx->Scissor.Height < buffer->_Ymax) {
384 buffer->_Ymax = ctx->Scissor.Y + ctx->Scissor.Height;
385 }
386 /* finally, check for empty region */
387 if (buffer->_Xmin > buffer->_Xmax) {
388 buffer->_Xmin = buffer->_Xmax;
389 }
390 if (buffer->_Ymin > buffer->_Ymax) {
391 buffer->_Ymin = buffer->_Ymax;
392 }
393 }
394
395 ASSERT(buffer->_Xmin <= buffer->_Xmax);
396 ASSERT(buffer->_Ymin <= buffer->_Ymax);
397}
398
399
400/**
401 * The glGet queries of the framebuffer red/green/blue size, stencil size,
402 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
Brian Paulf084f602005-09-13 23:37:50 +0000403 * change depending on the renderbuffer bindings. This function updates
Brian Paule4b23562005-05-04 20:11:35 +0000404 * the given framebuffer's Visual from the current renderbuffer bindings.
405 * This is only intended for user-created framebuffers.
Brian Pauleb063cf2005-10-04 14:48:24 +0000406 *
407 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
408 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
409 * The former one is used to convert floating point depth values into
410 * integer Z values.
Brian Paule4b23562005-05-04 20:11:35 +0000411 */
412void
413_mesa_update_framebuffer_visual(struct gl_framebuffer *fb)
414{
Brian Paul474f28e2005-10-08 14:41:17 +0000415 GLuint i;
416
Brian Paule4b23562005-05-04 20:11:35 +0000417 _mesa_bzero(&fb->Visual, sizeof(fb->Visual));
Brian Paul474f28e2005-10-08 14:41:17 +0000418 fb->Visual.rgbMode = GL_TRUE; /* assume this */
Brian Paule4b23562005-05-04 20:11:35 +0000419
Brian Paul34b3b402006-04-20 00:45:08 +0000420#if 0 /* this _might_ be needed */
421 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
422 /* leave visual fields zero'd */
423 return;
424 }
425#endif
426
Brian Paul474f28e2005-10-08 14:41:17 +0000427 /* find first RGB or CI renderbuffer */
428 for (i = 0; i < BUFFER_COUNT; i++) {
429 if (fb->Attachment[i].Renderbuffer) {
430 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
431 if (rb->_BaseFormat == GL_RGBA || rb->_BaseFormat == GL_RGB) {
432 fb->Visual.redBits = rb->RedBits;
433 fb->Visual.greenBits = rb->GreenBits;
434 fb->Visual.blueBits = rb->BlueBits;
435 fb->Visual.alphaBits = rb->AlphaBits;
436 fb->Visual.rgbBits = fb->Visual.redBits
437 + fb->Visual.greenBits + fb->Visual.blueBits;
438 fb->Visual.floatMode = GL_FALSE;
439 break;
440 }
441 else if (rb->_BaseFormat == GL_COLOR_INDEX) {
442 fb->Visual.indexBits = rb->IndexBits;
443 fb->Visual.rgbMode = GL_FALSE;
444 break;
445 }
446 }
Brian Paule4b23562005-05-04 20:11:35 +0000447 }
448
449 if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
450 fb->Visual.haveDepthBuffer = GL_TRUE;
451 fb->Visual.depthBits
Brian Paul676d0ac2005-09-22 05:19:57 +0000452 = fb->Attachment[BUFFER_DEPTH].Renderbuffer->DepthBits;
Brian Paule4b23562005-05-04 20:11:35 +0000453 }
454
455 if (fb->Attachment[BUFFER_STENCIL].Renderbuffer) {
456 fb->Visual.haveStencilBuffer = GL_TRUE;
457 fb->Visual.stencilBits
Brian Paul676d0ac2005-09-22 05:19:57 +0000458 = fb->Attachment[BUFFER_STENCIL].Renderbuffer->StencilBits;
Brian Paule4b23562005-05-04 20:11:35 +0000459 }
460
461 compute_depth_max(fb);
462}
463
464
465/**
Brian Paulea4fe662006-03-26 05:22:17 +0000466 * Update the framebuffer's _DepthBuffer field using the renderbuffer
467 * found at the given attachment index.
468 *
469 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
470 * create and install a depth wrapper/adaptor.
471 *
472 * \param fb the framebuffer whose _DepthBuffer field to update
473 * \param attIndex indicates the renderbuffer to possibly wrap
Brian Paul84716042005-11-16 04:05:54 +0000474 */
Brian Paulea4fe662006-03-26 05:22:17 +0000475void
476_mesa_update_depth_buffer(GLcontext *ctx,
477 struct gl_framebuffer *fb,
478 GLuint attIndex)
Brian Paul84716042005-11-16 04:05:54 +0000479{
Brian Paulea4fe662006-03-26 05:22:17 +0000480 struct gl_renderbuffer *depthRb;
481
482 /* only one possiblity for now */
483 ASSERT(attIndex == BUFFER_DEPTH);
484
485 depthRb = fb->Attachment[attIndex].Renderbuffer;
486
487 if (depthRb && depthRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT) {
488 /* The attached depth buffer is a GL_DEPTH_STENCIL renderbuffer */
Brian Paul57223382006-04-06 04:09:03 +0000489 if (!fb->_DepthBuffer
490 || fb->_DepthBuffer->Wrapped != depthRb
491 || fb->_DepthBuffer->_BaseFormat != GL_DEPTH_COMPONENT) {
Brian Paulea4fe662006-03-26 05:22:17 +0000492 /* need to update wrapper */
493 struct gl_renderbuffer *wrapper
494 = _mesa_new_z24_renderbuffer_wrapper(ctx, depthRb);
495 set_depth_renderbuffer(fb, wrapper);
496 ASSERT(fb->_DepthBuffer->Wrapped == depthRb);
Brian Paul84716042005-11-16 04:05:54 +0000497 }
498 }
Brian Paulea4fe662006-03-26 05:22:17 +0000499 else {
500 /* depthRb may be null */
501 set_depth_renderbuffer(fb, depthRb);
502 }
Brian Paul84716042005-11-16 04:05:54 +0000503}
504
Brian Paulea4fe662006-03-26 05:22:17 +0000505
Brian Paul84716042005-11-16 04:05:54 +0000506/**
Brian Paulea4fe662006-03-26 05:22:17 +0000507 * Update the framebuffer's _StencilBuffer field using the renderbuffer
508 * found at the given attachment index.
509 *
510 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
511 * create and install a stencil wrapper/adaptor.
512 *
513 * \param fb the framebuffer whose _StencilBuffer field to update
514 * \param attIndex indicates the renderbuffer to possibly wrap
Brian Paul84716042005-11-16 04:05:54 +0000515 */
Brian Paulea4fe662006-03-26 05:22:17 +0000516void
517_mesa_update_stencil_buffer(GLcontext *ctx,
518 struct gl_framebuffer *fb,
519 GLuint attIndex)
Brian Paul84716042005-11-16 04:05:54 +0000520{
Brian Paulea4fe662006-03-26 05:22:17 +0000521 struct gl_renderbuffer *stencilRb;
522
523 ASSERT(attIndex == BUFFER_DEPTH ||
524 attIndex == BUFFER_STENCIL);
525
526 stencilRb = fb->Attachment[attIndex].Renderbuffer;
527
528 if (stencilRb && stencilRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT) {
529 /* The attached stencil buffer is a GL_DEPTH_STENCIL renderbuffer */
Brian Paul57223382006-04-06 04:09:03 +0000530 if (!fb->_StencilBuffer
531 || fb->_StencilBuffer->Wrapped != stencilRb
532 || fb->_StencilBuffer->_BaseFormat != GL_STENCIL_INDEX) {
Brian Paulea4fe662006-03-26 05:22:17 +0000533 /* need to update wrapper */
534 struct gl_renderbuffer *wrapper
535 = _mesa_new_s8_renderbuffer_wrapper(ctx, stencilRb);
536 set_stencil_renderbuffer(fb, wrapper);
537 ASSERT(fb->_StencilBuffer->Wrapped == stencilRb);
Brian Paul84716042005-11-16 04:05:54 +0000538 }
539 }
Brian Paulea4fe662006-03-26 05:22:17 +0000540 else {
541 /* stencilRb may be null */
542 set_stencil_renderbuffer(fb, stencilRb);
543 }
544}
545
546
547/**
548 * Update the list of color drawing renderbuffer pointers.
549 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
550 * writing colors.
551 */
552static void
553update_color_draw_buffers(GLcontext *ctx, struct gl_framebuffer *fb)
554{
555 GLuint output;
556
557 /*
558 * Fragment programs can write to multiple colorbuffers with
559 * the GL_ARB_draw_buffers extension.
560 */
561 for (output = 0; output < ctx->Const.MaxDrawBuffers; output++) {
562 GLbitfield bufferMask = fb->_ColorDrawBufferMask[output];
563 GLuint count = 0;
564 GLuint i;
565 /* We need the inner loop here because glDrawBuffer(GL_FRONT_AND_BACK)
566 * can specify writing to two or four color buffers (for example).
567 */
568 for (i = 0; bufferMask && i < BUFFER_COUNT; i++) {
569 const GLuint bufferBit = 1 << i;
570 if (bufferBit & bufferMask) {
571 struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
572 if (rb) {
573 fb->_ColorDrawBuffers[output][count] = rb;
574 count++;
575 }
576 else {
577 /*_mesa_warning(ctx, "DrawBuffer names a missing buffer!\n");*/
578 }
579 bufferMask &= ~bufferBit;
580 }
581 }
582 fb->_NumColorDrawBuffers[output] = count;
583 }
584}
585
586
587/**
588 * Update the color read renderbuffer pointer.
589 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
590 */
591static void
592update_color_read_buffer(GLcontext *ctx, struct gl_framebuffer *fb)
593{
Brian Paul28b014e2006-04-05 03:05:17 +0000594 (void) ctx;
Brian Paulea4fe662006-03-26 05:22:17 +0000595 if (fb->_ColorReadBufferIndex == -1) {
596 fb->_ColorReadBuffer = NULL; /* legal! */
597 }
598 else {
599 ASSERT(fb->_ColorReadBufferIndex >= 0);
600 ASSERT(fb->_ColorReadBufferIndex < BUFFER_COUNT);
601 fb->_ColorReadBuffer
602 = fb->Attachment[fb->_ColorReadBufferIndex].Renderbuffer;
603 }
Brian Paul84716042005-11-16 04:05:54 +0000604}
605
606
607/**
Brian Paule4b23562005-05-04 20:11:35 +0000608 * Update state related to the current draw/read framebuffers.
Brian Paul52783592005-08-31 21:38:53 +0000609 * Specifically, update these framebuffer fields:
610 * _ColorDrawBuffers
Brian Paul52783592005-08-31 21:38:53 +0000611 * _NumColorDrawBuffers
612 * _ColorReadBuffer
Brian Paul0f29ef62005-11-16 04:17:20 +0000613 * _DepthBuffer
614 * _StencilBuffer
Brian Paule4b23562005-05-04 20:11:35 +0000615 * If the current framebuffer is user-created, make sure it's complete.
Brian Paul52783592005-08-31 21:38:53 +0000616 * The following functions can effect this state: glReadBuffer,
Brian Paul0f29ef62005-11-16 04:17:20 +0000617 * glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
618 * glRenderbufferStorageEXT.
Brian Paule4b23562005-05-04 20:11:35 +0000619 */
620void
621_mesa_update_framebuffer(GLcontext *ctx)
622{
623 struct gl_framebuffer *fb = ctx->DrawBuffer;
Brian Paule4b23562005-05-04 20:11:35 +0000624
625 /* Completeness only matters for user-created framebuffers */
Brian Paul474f28e2005-10-08 14:41:17 +0000626 if (fb->Name != 0) {
Brian Paule4b23562005-05-04 20:11:35 +0000627 _mesa_test_framebuffer_completeness(ctx, fb);
Brian Paul474f28e2005-10-08 14:41:17 +0000628 _mesa_update_framebuffer_visual(fb);
629 }
Brian Paule4b23562005-05-04 20:11:35 +0000630
Brian Paulea4fe662006-03-26 05:22:17 +0000631 update_color_draw_buffers(ctx, fb);
632 update_color_read_buffer(ctx, fb);
633 _mesa_update_depth_buffer(ctx, fb, BUFFER_DEPTH);
634 _mesa_update_stencil_buffer(ctx, fb, BUFFER_STENCIL);
Brian Paul84716042005-11-16 04:05:54 +0000635
Brian Paule4b23562005-05-04 20:11:35 +0000636 compute_depth_max(fb);
637}
Brian Paul7275d4d2006-03-20 15:25:18 +0000638
639
640/**
641 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
642 * glCopyTex[Sub]Image, etc. exists.
643 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
644 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
645 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
646 */
647GLboolean
648_mesa_source_buffer_exists(GLcontext *ctx, GLenum format)
649{
650 const struct gl_renderbuffer_attachment *att
651 = ctx->ReadBuffer->Attachment;
652
653 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
654 return GL_FALSE;
655 }
656
657 switch (format) {
658 case GL_COLOR:
Brian Paul590cd262006-03-24 22:53:00 +0000659 case GL_RED:
660 case GL_GREEN:
661 case GL_BLUE:
Brian Paul7275d4d2006-03-20 15:25:18 +0000662 case GL_ALPHA:
663 case GL_LUMINANCE:
664 case GL_LUMINANCE_ALPHA:
665 case GL_INTENSITY:
666 case GL_RGB:
Brian Paul590cd262006-03-24 22:53:00 +0000667 case GL_BGR:
Brian Paul7275d4d2006-03-20 15:25:18 +0000668 case GL_RGBA:
Brian Paul590cd262006-03-24 22:53:00 +0000669 case GL_BGRA:
670 case GL_ABGR_EXT:
Brian Paul7275d4d2006-03-20 15:25:18 +0000671 case GL_COLOR_INDEX:
672 if (ctx->ReadBuffer->_ColorReadBuffer == NULL) {
673 return GL_FALSE;
674 }
Brian Paul84c5d0a2006-03-30 16:29:41 +0000675 /* XXX enable this post 6.5 release:
676 ASSERT(ctx->ReadBuffer->_ColorReadBuffer->RedBits > 0 ||
677 ctx->ReadBuffer->_ColorReadBuffer->IndexBits > 0);
678 */
Brian Paul7275d4d2006-03-20 15:25:18 +0000679 break;
680 case GL_DEPTH:
681 case GL_DEPTH_COMPONENT:
682 if (!att[BUFFER_DEPTH].Renderbuffer) {
683 return GL_FALSE;
684 }
685 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
686 break;
687 case GL_STENCIL:
688 case GL_STENCIL_INDEX:
689 if (!att[BUFFER_STENCIL].Renderbuffer) {
690 return GL_FALSE;
691 }
692 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
693 break;
694 case GL_DEPTH_STENCIL_EXT:
695 if (!att[BUFFER_DEPTH].Renderbuffer ||
696 !att[BUFFER_STENCIL].Renderbuffer) {
697 return GL_FALSE;
698 }
699 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
700 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
701 break;
702 default:
703 _mesa_problem(ctx,
Brian Paul590cd262006-03-24 22:53:00 +0000704 "Unexpected format 0x%x in _mesa_source_buffer_exists",
705 format);
Brian Paul7275d4d2006-03-20 15:25:18 +0000706 return GL_FALSE;
707 }
708
709 /* OK */
710 return GL_TRUE;
711}
712
713
714/**
715 * As above, but for drawing operations.
716 * XXX code do some code merging w/ above function.
717 */
718GLboolean
719_mesa_dest_buffer_exists(GLcontext *ctx, GLenum format)
720{
721 const struct gl_renderbuffer_attachment *att
722 = ctx->ReadBuffer->Attachment;
723
724 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
725 return GL_FALSE;
726 }
727
728 switch (format) {
729 case GL_COLOR:
Brian Paul590cd262006-03-24 22:53:00 +0000730 case GL_RED:
731 case GL_GREEN:
732 case GL_BLUE:
Brian Paul7275d4d2006-03-20 15:25:18 +0000733 case GL_ALPHA:
734 case GL_LUMINANCE:
735 case GL_LUMINANCE_ALPHA:
736 case GL_INTENSITY:
737 case GL_RGB:
Brian Paul590cd262006-03-24 22:53:00 +0000738 case GL_BGR:
Brian Paul7275d4d2006-03-20 15:25:18 +0000739 case GL_RGBA:
Brian Paul590cd262006-03-24 22:53:00 +0000740 case GL_BGRA:
741 case GL_ABGR_EXT:
Brian Paul7275d4d2006-03-20 15:25:18 +0000742 case GL_COLOR_INDEX:
743 /* nothing special */
Brian Paul84c5d0a2006-03-30 16:29:41 +0000744 /* Could assert that colorbuffer has RedBits > 0 */
Brian Paul7275d4d2006-03-20 15:25:18 +0000745 break;
746 case GL_DEPTH:
747 case GL_DEPTH_COMPONENT:
748 if (!att[BUFFER_DEPTH].Renderbuffer) {
749 return GL_FALSE;
750 }
751 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
752 break;
753 case GL_STENCIL:
754 case GL_STENCIL_INDEX:
755 if (!att[BUFFER_STENCIL].Renderbuffer) {
756 return GL_FALSE;
757 }
758 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
759 break;
760 case GL_DEPTH_STENCIL_EXT:
761 if (!att[BUFFER_DEPTH].Renderbuffer ||
762 !att[BUFFER_STENCIL].Renderbuffer) {
763 return GL_FALSE;
764 }
765 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
766 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
767 break;
768 default:
769 _mesa_problem(ctx,
Brian Paul590cd262006-03-24 22:53:00 +0000770 "Unexpected format 0x%x in _mesa_source_buffer_exists",
771 format);
Brian Paul7275d4d2006-03-20 15:25:18 +0000772 return GL_FALSE;
773 }
774
775 /* OK */
776 return GL_TRUE;
777}