Debian Codegraph @

nx-X11/extras/Mesa/src/mesa/main/framebuffer.c

raw
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.4
4 *
5 * Copyright (C) 1999-2005 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 * 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"
36 #include "mtypes.h"
37 #include "fbobject.h"
38 #include "framebuffer.h"
39 #include "renderbuffer.h"
40
41
42
43 /**
44 * Compute/set the _DepthMax field for the given framebuffer.
45 * This value depends on the Z buffer resolution.
46 */
47 static void
48 compute_depth_max(struct gl_framebuffer *fb)
49 {
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.
54 */
55 fb->_DepthMax = (1 << 16) - 1;
56 }
57 else if (fb->Visual.depthBits < 32) {
58 fb->_DepthMax = (1 << fb->Visual.depthBits) - 1;
59 }
60 else {
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.
63 */
64 fb->_DepthMax = 0xffffffff;
65 }
66 fb->_DepthMaxF = (GLfloat) fb->_DepthMax;
67 fb->_MRD = 1.0; /* Minimum resolvable depth value, for polygon offset */
68 }
69
70
71 /**
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.
75 *
76 * \sa _mesa_new_framebuffer
77 */
78 struct gl_framebuffer *
79 _mesa_create_framebuffer(const GLvisual *visual)
80 {
81 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
82 assert(visual);
83 if (fb) {
84 _mesa_initialize_framebuffer(fb, visual);
85 }
86 return fb;
87 }
88
89
90 /**
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
94 * framebuffers!
95 * \sa _mesa_create_framebuffer
96 */
97 struct gl_framebuffer *
98 _mesa_new_framebuffer(GLcontext *ctx, GLuint name)
99 {
100 struct gl_framebuffer *fb;
101 assert(name != 0);
102 fb = CALLOC_STRUCT(gl_framebuffer);
103 if (fb) {
104 fb->Name = name;
105 fb->RefCount = 1;
106 fb->Delete = _mesa_destroy_framebuffer;
107 fb->ColorDrawBuffer[0] = GL_COLOR_ATTACHMENT0_EXT;
108 fb->_ColorDrawBufferMask[0] = BUFFER_BIT_COLOR0;
109 fb->ColorReadBuffer = GL_COLOR_ATTACHMENT0_EXT;
110 fb->_ColorReadBufferMask = BUFFER_BIT_COLOR0;
111 fb->Delete = _mesa_destroy_framebuffer;
112 }
113 return fb;
114 }
115
116
117 /**
118 * Initialize a gl_framebuffer object.
119 * \sa _mesa_create_framebuffer
120 */
121 void
122 _mesa_initialize_framebuffer(struct gl_framebuffer *fb, const GLvisual *visual)
123 {
124 assert(fb);
125 assert(visual);
126
127 _mesa_bzero(fb, sizeof(struct gl_framebuffer));
128
129 /* save the visual */
130 fb->Visual = *visual;
131
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->_ColorReadBufferMask = BUFFER_BIT_BACK_LEFT;
138 }
139 else {
140 fb->ColorDrawBuffer[0] = GL_FRONT;
141 fb->_ColorDrawBufferMask[0] = BUFFER_BIT_FRONT_LEFT;
142 fb->ColorReadBuffer = GL_FRONT;
143 fb->_ColorReadBufferMask = BUFFER_BIT_FRONT_LEFT;
144 }
145
146 fb->Delete = _mesa_destroy_framebuffer;
147 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
148
149 compute_depth_max(fb);
150 }
151
152
153 /**
154 * Create/attach software-based renderbuffers to the given framebuffer.
155 * This is a helper routine for device drivers. Drivers can just as well
156 * call the individual _mesa_add_*_renderbuffer() routines directly.
157 */
158 void
159 _mesa_add_soft_renderbuffers(struct gl_framebuffer *fb,
160 GLboolean color,
161 GLboolean depth,
162 GLboolean stencil,
163 GLboolean accum,
164 GLboolean alpha,
165 GLboolean aux)
166 {
167 GLboolean frontLeft = GL_TRUE;
168 GLboolean backLeft = fb->Visual.doubleBufferMode;
169 GLboolean frontRight = fb->Visual.stereoMode;
170 GLboolean backRight = fb->Visual.stereoMode && fb->Visual.doubleBufferMode;
171
172 if (color) {
173 if (fb->Visual.rgbMode) {
174 assert(fb->Visual.redBits == fb->Visual.greenBits);
175 assert(fb->Visual.redBits == fb->Visual.blueBits);
176 _mesa_add_color_renderbuffers(NULL, fb,
177 fb->Visual.redBits,
178 fb->Visual.alphaBits,
179 frontLeft, backLeft,
180 frontRight, backRight);
181 }
182 else {
183 _mesa_add_color_index_renderbuffers(NULL, fb,
184 fb->Visual.indexBits,
185 frontLeft, backLeft,
186 frontRight, backRight);
187 }
188 }
189
190 if (depth) {
191 assert(fb->Visual.depthBits > 0);
192 _mesa_add_depth_renderbuffer(NULL, fb, fb->Visual.depthBits);
193 }
194
195 if (stencil) {
196 assert(fb->Visual.stencilBits > 0);
197 _mesa_add_stencil_renderbuffer(NULL, fb, fb->Visual.stencilBits);
198 }
199
200 if (accum) {
201 assert(fb->Visual.rgbMode);
202 assert(fb->Visual.accumRedBits > 0);
203 assert(fb->Visual.accumGreenBits > 0);
204 assert(fb->Visual.accumBlueBits > 0);
205 _mesa_add_accum_renderbuffer(NULL, fb,
206 fb->Visual.accumRedBits,
207 fb->Visual.accumGreenBits,
208 fb->Visual.accumBlueBits,
209 fb->Visual.accumAlphaBits);
210 }
211
212 if (aux) {
213 assert(fb->Visual.rgbMode);
214 assert(fb->Visual.numAuxBuffers > 0);
215 _mesa_add_aux_renderbuffers(NULL, fb, fb->Visual.redBits,
216 fb->Visual.numAuxBuffers);
217 }
218
219 #if 1
220 if (alpha) {
221 assert(fb->Visual.rgbMode);
222 assert(fb->Visual.alphaBits > 0);
223 _mesa_add_alpha_renderbuffers(NULL, fb, fb->Visual.alphaBits,
224 frontLeft, backLeft,
225 frontRight, backRight);
226 }
227 #endif
228
229 #if 0
230 if (multisample) {
231 /* maybe someday */
232 }
233 #endif
234 }
235
236
237 /**
238 * Deallocate buffer and everything attached to it.
239 */
240 void
241 _mesa_destroy_framebuffer(struct gl_framebuffer *buffer)
242 {
243 if (buffer) {
244 _mesa_free_framebuffer_data(buffer);
245 FREE(buffer);
246 }
247 }
248
249
250 /**
251 * Free all the data hanging off the given gl_framebuffer, but don't free
252 * the gl_framebuffer object itself.
253 */
254 void
255 _mesa_free_framebuffer_data(struct gl_framebuffer *fb)
256 {
257 GLuint i;
258
259 assert(fb);
260
261 for (i = 0; i < BUFFER_COUNT; i++) {
262 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
263 if (att->Type == GL_RENDERBUFFER_EXT && att->Renderbuffer) {
264 struct gl_renderbuffer *rb = att->Renderbuffer;
265 rb->RefCount--;
266 if (rb->RefCount == 0) {
267 rb->Delete(rb);
268 }
269 }
270 att->Type = GL_NONE;
271 att->Renderbuffer = NULL;
272 }
273 }
274
275
276 /**
277 * Resize the given framebuffer's renderbuffers to the new width and height.
278 * This should only be used for window-system framebuffers, not
279 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
280 * This will typically be called via ctx->Driver.ResizeBuffers()
281 */
282 void
283 _mesa_resize_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb,
284 GLuint width, GLuint height)
285 {
286 GLuint i;
287
288 /* For window system framebuffers, Name is zero */
289 assert(fb->Name == 0);
290
291 for (i = 0; i < BUFFER_COUNT; i++) {
292 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
293 if (att->Type == GL_RENDERBUFFER_EXT && att->Renderbuffer) {
294 struct gl_renderbuffer *rb = att->Renderbuffer;
295 /* only resize if size is changing */
296 if (rb->Width != width || rb->Height != height) {
297 if (rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
298 rb->Width = width;
299 rb->Height = height;
300 }
301 else {
302 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
303 }
304 }
305 }
306 }
307
308 fb->Width = width;
309 fb->Height = height;
310
311 /* to update scissor / window bounds */
312 if (ctx)
313 ctx->NewState |= _NEW_BUFFERS;
314 }
315
316
317 /**
318 * Examine all the framebuffer's renderbuffers to update the Width/Height
319 * fields of the framebuffer. If we have renderbuffers with different
320 * sizes, set the framebuffer's width and height to zero.
321 * Note: this is only intended for user-created framebuffers, not
322 * window-system framebuffes.
323 */
324 static void
325 update_framebuffer_size(struct gl_framebuffer *fb)
326 {
327 GLboolean haveSize = GL_FALSE;
328 GLuint i;
329
330 /* user-created framebuffers only */
331 assert(fb->Name);
332
333 for (i = 0; i < BUFFER_COUNT; i++) {
334 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
335 const struct gl_renderbuffer *rb = att->Renderbuffer;
336 if (rb) {
337 if (haveSize) {
338 if (rb->Width != fb->Width && rb->Height != fb->Height) {
339 /* size mismatch! */
340 fb->Width = 0;
341 fb->Height = 0;
342 return;
343 }
344 }
345 else {
346 fb->Width = rb->Width;
347 fb->Height = rb->Height;
348 haveSize = GL_TRUE;
349 }
350 }
351 }
352 }
353
354
355 /**
356 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
357 * These values are computed from the buffer's width and height and
358 * the scissor box, if it's enabled.
359 * \param ctx the GL context.
360 */
361 void
362 _mesa_update_draw_buffer_bounds(GLcontext *ctx)
363 {
364 struct gl_framebuffer *buffer = ctx->DrawBuffer;
365
366 if (buffer->Name) {
367 /* user-created framebuffer size depends on the renderbuffers */
368 update_framebuffer_size(buffer);
369 }
370
371 buffer->_Xmin = 0;
372 buffer->_Ymin = 0;
373 buffer->_Xmax = buffer->Width;
374 buffer->_Ymax = buffer->Height;
375
376 if (ctx->Scissor.Enabled) {
377 if (ctx->Scissor.X > buffer->_Xmin) {
378 buffer->_Xmin = ctx->Scissor.X;
379 }
380 if (ctx->Scissor.Y > buffer->_Ymin) {
381 buffer->_Ymin = ctx->Scissor.Y;
382 }
383 if (ctx->Scissor.X + ctx->Scissor.Width < buffer->_Xmax) {
384 buffer->_Xmax = ctx->Scissor.X + ctx->Scissor.Width;
385 }
386 if (ctx->Scissor.Y + ctx->Scissor.Height < buffer->_Ymax) {
387 buffer->_Ymax = ctx->Scissor.Y + ctx->Scissor.Height;
388 }
389 /* finally, check for empty region */
390 if (buffer->_Xmin > buffer->_Xmax) {
391 buffer->_Xmin = buffer->_Xmax;
392 }
393 if (buffer->_Ymin > buffer->_Ymax) {
394 buffer->_Ymin = buffer->_Ymax;
395 }
396 }
397
398 ASSERT(buffer->_Xmin <= buffer->_Xmax);
399 ASSERT(buffer->_Ymin <= buffer->_Ymax);
400 }
401
402
403 /**
404 * The glGet queries of the framebuffer red/green/blue size, stencil size,
405 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
406 * change depending on the renderbuffer bindings. This function update's
407 * the given framebuffer's Visual from the current renderbuffer bindings.
408 * This is only intended for user-created framebuffers.
409 */
410 void
411 _mesa_update_framebuffer_visual(struct gl_framebuffer *fb)
412 {
413 GLuint i;
414
415 assert(fb->Name != 0);
416
417 _mesa_bzero(&fb->Visual, sizeof(fb->Visual));
418 fb->Visual.rgbMode = GL_TRUE;
419
420 /* find first RGB or CI renderbuffer */
421 for (i = 0; i < BUFFER_COUNT; i++) {
422 if (fb->Attachment[i].Renderbuffer) {
423 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
424 if (rb->_BaseFormat == GL_RGBA || rb->_BaseFormat == GL_RGB) {
425 fb->Visual.redBits = rb->ComponentSizes[0];
426 fb->Visual.greenBits = rb->ComponentSizes[1];
427 fb->Visual.blueBits = rb->ComponentSizes[2];
428 fb->Visual.alphaBits = rb->ComponentSizes[3];
429 fb->Visual.floatMode = GL_FALSE;
430 break;
431 }
432 else if (rb->_BaseFormat == GL_COLOR_INDEX) {
433 fb->Visual.indexBits = rb->ComponentSizes[0];
434 fb->Visual.rgbMode = GL_FALSE;
435 break;
436 }
437 }
438 }
439
440 if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
441 fb->Visual.haveDepthBuffer = GL_TRUE;
442 fb->Visual.depthBits
443 = fb->Attachment[BUFFER_DEPTH].Renderbuffer->ComponentSizes[0];
444 }
445
446 if (fb->Attachment[BUFFER_STENCIL].Renderbuffer) {
447 fb->Visual.haveStencilBuffer = GL_TRUE;
448 fb->Visual.stencilBits
449 = fb->Attachment[BUFFER_STENCIL].Renderbuffer->ComponentSizes[0];
450 }
451
452 compute_depth_max(fb);
453 }
454
455
456 /**
457 * Given a framebuffer and a buffer bit (like BUFFER_BIT_FRONT_LEFT), return
458 * the corresponding renderbuffer.
459 */
460 static struct gl_renderbuffer *
461 get_renderbuffer(struct gl_framebuffer *fb, GLuint bufferBit)
462 {
463 GLuint index;
464 for (index = 0; index < BUFFER_COUNT; index++) {
465 if ((1 << index) == bufferBit) {
466 return fb->Attachment[index].Renderbuffer;
467 }
468 }
469 _mesa_problem(NULL, "Bad bufferBit in get_renderbuffer");
470 return NULL;
471 }
472
473
474 /**
475 * Update state related to the current draw/read framebuffers.
476 * If the current framebuffer is user-created, make sure it's complete.
477 */
478 void
479 _mesa_update_framebuffer(GLcontext *ctx)
480 {
481 struct gl_framebuffer *fb = ctx->DrawBuffer;
482 GLuint output;
483
484 /* Completeness only matters for user-created framebuffers */
485 if (fb->Name != 0) {
486 _mesa_test_framebuffer_completeness(ctx, fb);
487 _mesa_update_framebuffer_visual(fb);
488 }
489
490 /*
491 * Update the list of drawing renderbuffer pointers.
492 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
493 * writing colors. We have a loop because glDrawBuffer(GL_FRONT_AND_BACK)
494 * can specify writing to two or four color buffers.
495 */
496 for (output = 0; output < ctx->Const.MaxDrawBuffers; output++) {
497 GLuint bufferMask = fb->_ColorDrawBufferMask[output];
498 GLuint count = 0;
499 GLuint bufferBit;
500 /* for each bit that's set in the bufferMask... */
501 for (bufferBit = 1; bufferMask; bufferBit <<= 1) {
502 if (bufferBit & bufferMask) {
503 struct gl_renderbuffer *rb = get_renderbuffer(fb, bufferBit);
504 if (rb) {
505 fb->_ColorDrawBuffers[output][count] = rb;
506 fb->_ColorDrawBit[output][count] = bufferBit;
507 count++;
508 }
509 else {
510 /*_mesa_warning(ctx, "DrawBuffer names a missing buffer!");*/
511 }
512 bufferMask &= ~bufferBit;
513 }
514 }
515 fb->_NumColorDrawBuffers[output] = count;
516 }
517
518 /*
519 * Update the read renderbuffer pointer.
520 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
521 */
522 if (fb->_ColorReadBufferMask == 0x0)
523 fb->_ColorReadBuffer = NULL; /* legal! */
524 else
525 fb->_ColorReadBuffer = get_renderbuffer(fb, fb->_ColorReadBufferMask);
526
527 compute_depth_max(fb);
528 }