2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Functions for allocating/managing framebuffers and renderbuffers.
28 * Also, routines for reading/writing renderbuffer data as ubytes,
29 * ushorts, uints, etc.
38 #include "framebuffer.h"
39 #include "renderbuffer.h"
44 * Compute/set the _DepthMax field for the given framebuffer.
45 * This value depends on the Z buffer resolution.
48 compute_depth_max(struct gl_framebuffer
*fb
)
50 if (fb
->Visual
.depthBits
== 0) {
51 /* Special case. Even if we don't have a depth buffer we need
52 * good values for DepthMax for Z vertex transformation purposes
53 * and for per-fragment fog computation.
55 fb
->_DepthMax
= (1 << 16) - 1;
57 else if (fb
->Visual
.depthBits
< 32) {
58 fb
->_DepthMax
= (1 << fb
->Visual
.depthBits
) - 1;
61 /* Special case since shift values greater than or equal to the
62 * number of bits in the left hand expression's type are undefined.
64 fb
->_DepthMax
= 0xffffffff;
66 fb
->_DepthMaxF
= (GLfloat
) fb
->_DepthMax
;
67 fb
->_MRD
= 1.0; /* Minimum resolvable depth value, for polygon offset */
72 * Create and initialize a gl_framebuffer object.
73 * This is intended for creating _window_system_ framebuffers, not generic
74 * framebuffer objects ala GL_EXT_framebuffer_object.
76 * \sa _mesa_new_framebuffer
78 struct gl_framebuffer
*
79 _mesa_create_framebuffer(const GLvisual
*visual
)
81 struct gl_framebuffer
*fb
= CALLOC_STRUCT(gl_framebuffer
);
84 _mesa_initialize_framebuffer(fb
, visual
);
91 * Allocate a new gl_framebuffer object.
92 * This is the default function for ctx->Driver.NewFramebuffer().
93 * This is for allocating user-created framebuffers, not window-system
95 * \sa _mesa_create_framebuffer
97 struct gl_framebuffer
*
98 _mesa_new_framebuffer(GLcontext
*ctx
, GLuint name
)
100 struct gl_framebuffer
*fb
;
102 fb
= CALLOC_STRUCT(gl_framebuffer
);
106 fb
->ColorDrawBuffer
[0] = GL_COLOR_ATTACHMENT0_EXT
;
107 fb
->_ColorDrawBufferMask
[0] = BUFFER_BIT_COLOR0
;
108 fb
->ColorReadBuffer
= GL_COLOR_ATTACHMENT0_EXT
;
109 fb
->_ColorReadBufferIndex
= BUFFER_COLOR0
;
110 fb
->Delete
= _mesa_destroy_framebuffer
;
117 * Initialize a gl_framebuffer object. Typically used to initialize
118 * window system-created framebuffers, not user-created framebuffers.
119 * \sa _mesa_create_framebuffer
122 _mesa_initialize_framebuffer(struct gl_framebuffer
*fb
, const GLvisual
*visual
)
127 _mesa_bzero(fb
, sizeof(struct gl_framebuffer
));
129 /* save the visual */
130 fb
->Visual
= *visual
;
132 /* Init glRead/DrawBuffer state */
133 if (visual
->doubleBufferMode
) {
134 fb
->ColorDrawBuffer
[0] = GL_BACK
;
135 fb
->_ColorDrawBufferMask
[0] = BUFFER_BIT_BACK_LEFT
;
136 fb
->ColorReadBuffer
= GL_BACK
;
137 fb
->_ColorReadBufferIndex
= BUFFER_BACK_LEFT
;
140 fb
->ColorDrawBuffer
[0] = GL_FRONT
;
141 fb
->_ColorDrawBufferMask
[0] = BUFFER_BIT_FRONT_LEFT
;
142 fb
->ColorReadBuffer
= GL_FRONT
;
143 fb
->_ColorReadBufferIndex
= BUFFER_FRONT_LEFT
;
146 fb
->Delete
= _mesa_destroy_framebuffer
;
147 fb
->_Status
= GL_FRAMEBUFFER_COMPLETE_EXT
;
149 compute_depth_max(fb
);
154 * Deallocate buffer and everything attached to it.
155 * Typically called via the gl_framebuffer->Delete() method.
158 _mesa_destroy_framebuffer(struct gl_framebuffer
*fb
)
161 _mesa_free_framebuffer_data(fb
);
168 * Free all the data hanging off the given gl_framebuffer, but don't free
169 * the gl_framebuffer object itself.
172 _mesa_free_framebuffer_data(struct gl_framebuffer
*fb
)
178 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
179 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
180 if (att
->Type
== GL_RENDERBUFFER_EXT
&& att
->Renderbuffer
) {
181 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
183 if (rb
->RefCount
== 0) {
188 att
->Renderbuffer
= NULL
;
194 * Resize the given framebuffer's renderbuffers to the new width and height.
195 * This should only be used for window-system framebuffers, not
196 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
197 * This will typically be called via ctx->Driver.ResizeBuffers()
200 _mesa_resize_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
201 GLuint width
, GLuint height
)
205 /* For window system framebuffers, Name is zero */
206 assert(fb
->Name
== 0);
208 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
209 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
210 if (att
->Type
== GL_RENDERBUFFER_EXT
&& att
->Renderbuffer
) {
211 struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
212 /* only resize if size is changing */
213 if (rb
->Width
!= width
|| rb
->Height
!= height
) {
214 if (rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
)) {
219 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Resizing framebuffer");
228 /* to update scissor / window bounds */
229 ctx
->NewState
|= _NEW_BUFFERS
;
234 * Examine all the framebuffer's renderbuffers to update the Width/Height
235 * fields of the framebuffer. If we have renderbuffers with different
236 * sizes, set the framebuffer's width and height to zero.
237 * Note: this is only intended for user-created framebuffers, not
238 * window-system framebuffes.
241 update_framebuffer_size(struct gl_framebuffer
*fb
)
243 GLboolean haveSize
= GL_FALSE
;
246 /* user-created framebuffers only */
249 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
250 struct gl_renderbuffer_attachment
*att
= &fb
->Attachment
[i
];
251 const struct gl_renderbuffer
*rb
= att
->Renderbuffer
;
254 if (rb
->Width
!= fb
->Width
&& rb
->Height
!= fb
->Height
) {
262 fb
->Width
= rb
->Width
;
263 fb
->Height
= rb
->Height
;
272 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
273 * These values are computed from the buffer's width and height and
274 * the scissor box, if it's enabled.
275 * \param ctx the GL context.
278 _mesa_update_draw_buffer_bounds(GLcontext
*ctx
)
280 struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
283 /* user-created framebuffer size depends on the renderbuffers */
284 update_framebuffer_size(buffer
);
289 buffer
->_Xmax
= buffer
->Width
;
290 buffer
->_Ymax
= buffer
->Height
;
292 if (ctx
->Scissor
.Enabled
) {
293 if (ctx
->Scissor
.X
> buffer
->_Xmin
) {
294 buffer
->_Xmin
= ctx
->Scissor
.X
;
296 if (ctx
->Scissor
.Y
> buffer
->_Ymin
) {
297 buffer
->_Ymin
= ctx
->Scissor
.Y
;
299 if (ctx
->Scissor
.X
+ ctx
->Scissor
.Width
< buffer
->_Xmax
) {
300 buffer
->_Xmax
= ctx
->Scissor
.X
+ ctx
->Scissor
.Width
;
302 if (ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
< buffer
->_Ymax
) {
303 buffer
->_Ymax
= ctx
->Scissor
.Y
+ ctx
->Scissor
.Height
;
305 /* finally, check for empty region */
306 if (buffer
->_Xmin
> buffer
->_Xmax
) {
307 buffer
->_Xmin
= buffer
->_Xmax
;
309 if (buffer
->_Ymin
> buffer
->_Ymax
) {
310 buffer
->_Ymin
= buffer
->_Ymax
;
314 ASSERT(buffer
->_Xmin
<= buffer
->_Xmax
);
315 ASSERT(buffer
->_Ymin
<= buffer
->_Ymax
);
320 * The glGet queries of the framebuffer red/green/blue size, stencil size,
321 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
322 * change depending on the renderbuffer bindings. This function updates
323 * the given framebuffer's Visual from the current renderbuffer bindings.
324 * This is only intended for user-created framebuffers.
326 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
327 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
328 * The former one is used to convert floating point depth values into
332 _mesa_update_framebuffer_visual(struct gl_framebuffer
*fb
)
336 assert(fb
->Name
!= 0);
338 _mesa_bzero(&fb
->Visual
, sizeof(fb
->Visual
));
339 fb
->Visual
.rgbMode
= GL_TRUE
; /* assume this */
341 /* find first RGB or CI renderbuffer */
342 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
343 if (fb
->Attachment
[i
].Renderbuffer
) {
344 const struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
345 if (rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
) {
346 fb
->Visual
.redBits
= rb
->RedBits
;
347 fb
->Visual
.greenBits
= rb
->GreenBits
;
348 fb
->Visual
.blueBits
= rb
->BlueBits
;
349 fb
->Visual
.alphaBits
= rb
->AlphaBits
;
350 fb
->Visual
.rgbBits
= fb
->Visual
.redBits
351 + fb
->Visual
.greenBits
+ fb
->Visual
.blueBits
;
352 fb
->Visual
.floatMode
= GL_FALSE
;
355 else if (rb
->_BaseFormat
== GL_COLOR_INDEX
) {
356 fb
->Visual
.indexBits
= rb
->IndexBits
;
357 fb
->Visual
.rgbMode
= GL_FALSE
;
363 if (fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
) {
364 fb
->Visual
.haveDepthBuffer
= GL_TRUE
;
366 = fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
->DepthBits
;
369 if (fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
) {
370 fb
->Visual
.haveStencilBuffer
= GL_TRUE
;
371 fb
->Visual
.stencilBits
372 = fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
->StencilBits
;
375 compute_depth_max(fb
);
380 * Update state related to the current draw/read framebuffers.
381 * Specifically, update these framebuffer fields:
383 * _NumColorDrawBuffers
385 * If the current framebuffer is user-created, make sure it's complete.
386 * The following functions can effect this state: glReadBuffer,
387 * glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT.
390 _mesa_update_framebuffer(GLcontext
*ctx
)
392 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
395 /* Completeness only matters for user-created framebuffers */
397 _mesa_test_framebuffer_completeness(ctx
, fb
);
398 _mesa_update_framebuffer_visual(fb
);
402 * Update the list of color drawing renderbuffer pointers.
403 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
404 * writing colors. We need the inner loop here because
405 * glDrawBuffer(GL_FRONT_AND_BACK) can specify writing to two or four
406 * color buffers (for example).
408 for (output
= 0; output
< ctx
->Const
.MaxDrawBuffers
; output
++) {
409 GLbitfield bufferMask
= fb
->_ColorDrawBufferMask
[output
];
412 for (i
= 0; bufferMask
&& i
< BUFFER_COUNT
; i
++) {
413 const GLuint bufferBit
= 1 << i
;
414 if (bufferBit
& bufferMask
) {
415 struct gl_renderbuffer
*rb
= fb
->Attachment
[i
].Renderbuffer
;
417 fb
->_ColorDrawBuffers
[output
][count
] = rb
;
421 /*_mesa_warning(ctx, "DrawBuffer names a missing buffer!\n");*/
423 bufferMask
&= ~bufferBit
;
426 fb
->_NumColorDrawBuffers
[output
] = count
;
430 * Update the color read renderbuffer pointer.
431 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
433 if (fb
->_ColorReadBufferIndex
== -1) {
434 fb
->_ColorReadBuffer
= NULL
; /* legal! */
437 ASSERT(fb
->_ColorReadBufferIndex
>= 0);
438 ASSERT(fb
->_ColorReadBufferIndex
< BUFFER_COUNT
);
440 = fb
->Attachment
[fb
->_ColorReadBufferIndex
].Renderbuffer
;
442 compute_depth_max(fb
);