mesa: move freeing of software renderbuffers into swrast
[mesa.git] / src / mesa / swrast / s_renderbuffer.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 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 software-based renderbuffers.
28 * Also, routines for reading/writing software-based renderbuffer data as
29 * ubytes, ushorts, uints, etc.
30 */
31
32
33 #include "main/glheader.h"
34 #include "main/imports.h"
35 #include "main/context.h"
36 #include "main/fbobject.h"
37 #include "main/formats.h"
38 #include "main/mtypes.h"
39 #include "main/renderbuffer.h"
40 #include "swrast/s_renderbuffer.h"
41
42
43 /**
44 * This is a software fallback for the gl_renderbuffer->AllocStorage
45 * function.
46 * Device drivers will typically override this function for the buffers
47 * which it manages (typically color buffers, Z and stencil).
48 * Other buffers (like software accumulation and aux buffers) which the driver
49 * doesn't manage can be handled with this function.
50 *
51 * This one multi-purpose function can allocate stencil, depth, accum, color
52 * or color-index buffers!
53 */
54 static GLboolean
55 soft_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
56 GLenum internalFormat,
57 GLuint width, GLuint height)
58 {
59 switch (internalFormat) {
60 case GL_RGB:
61 case GL_R3_G3_B2:
62 case GL_RGB4:
63 case GL_RGB5:
64 case GL_RGB8:
65 case GL_RGB10:
66 case GL_RGB12:
67 case GL_RGB16:
68 rb->Format = MESA_FORMAT_RGB888;
69 break;
70 case GL_RGBA:
71 case GL_RGBA2:
72 case GL_RGBA4:
73 case GL_RGB5_A1:
74 case GL_RGBA8:
75 #if 1
76 case GL_RGB10_A2:
77 case GL_RGBA12:
78 #endif
79 if (_mesa_little_endian())
80 rb->Format = MESA_FORMAT_RGBA8888_REV;
81 else
82 rb->Format = MESA_FORMAT_RGBA8888;
83 break;
84 case GL_RGBA16:
85 case GL_RGBA16_SNORM:
86 /* for accum buffer */
87 rb->Format = MESA_FORMAT_SIGNED_RGBA_16;
88 break;
89 case GL_STENCIL_INDEX:
90 case GL_STENCIL_INDEX1_EXT:
91 case GL_STENCIL_INDEX4_EXT:
92 case GL_STENCIL_INDEX8_EXT:
93 case GL_STENCIL_INDEX16_EXT:
94 rb->Format = MESA_FORMAT_S8;
95 break;
96 case GL_DEPTH_COMPONENT:
97 case GL_DEPTH_COMPONENT16:
98 rb->Format = MESA_FORMAT_Z16;
99 break;
100 case GL_DEPTH_COMPONENT24:
101 rb->Format = MESA_FORMAT_X8_Z24;
102 break;
103 case GL_DEPTH_COMPONENT32:
104 rb->Format = MESA_FORMAT_Z32;
105 break;
106 case GL_DEPTH_STENCIL_EXT:
107 case GL_DEPTH24_STENCIL8_EXT:
108 rb->Format = MESA_FORMAT_Z24_S8;
109 break;
110 default:
111 /* unsupported format */
112 return GL_FALSE;
113 }
114
115 /* free old buffer storage */
116 if (rb->Data) {
117 free(rb->Data);
118 rb->Data = NULL;
119 }
120
121 rb->RowStrideBytes = width * _mesa_get_format_bytes(rb->Format);
122
123 if (width > 0 && height > 0) {
124 /* allocate new buffer storage */
125 rb->Data = malloc(width * height * _mesa_get_format_bytes(rb->Format));
126
127 if (rb->Data == NULL) {
128 rb->Width = 0;
129 rb->Height = 0;
130 _mesa_error(ctx, GL_OUT_OF_MEMORY,
131 "software renderbuffer allocation (%d x %d x %d)",
132 width, height, _mesa_get_format_bytes(rb->Format));
133 return GL_FALSE;
134 }
135 }
136
137 rb->Width = width;
138 rb->Height = height;
139 rb->_BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
140
141 if (rb->Name == 0 &&
142 internalFormat == GL_RGBA16_SNORM &&
143 rb->_BaseFormat == 0) {
144 /* NOTE: This is a special case just for accumulation buffers.
145 * This is a very limited use case- there's no snorm texturing or
146 * rendering going on.
147 */
148 rb->_BaseFormat = GL_RGBA;
149 }
150 else {
151 /* the internalFormat should have been error checked long ago */
152 ASSERT(rb->_BaseFormat);
153 }
154
155 return GL_TRUE;
156 }
157
158
159 /**
160 * Called via gl_renderbuffer::Delete()
161 */
162 static void
163 soft_renderbuffer_delete(struct gl_renderbuffer *rb)
164 {
165 if (rb->Data) {
166 free(rb->Data);
167 rb->Data = NULL;
168 }
169 free(rb);
170 }
171
172
173 void
174 _swrast_map_soft_renderbuffer(struct gl_context *ctx,
175 struct gl_renderbuffer *rb,
176 GLuint x, GLuint y, GLuint w, GLuint h,
177 GLbitfield mode,
178 GLubyte **out_map,
179 GLint *out_stride)
180 {
181 GLubyte *map = rb->Data;
182 int cpp = _mesa_get_format_bytes(rb->Format);
183 int stride = rb->Width * cpp;
184
185 ASSERT(rb->Data);
186
187 map += y * stride;
188 map += x * cpp;
189
190 *out_map = map;
191 *out_stride = stride;
192 }
193
194
195 void
196 _swrast_unmap_soft_renderbuffer(struct gl_context *ctx,
197 struct gl_renderbuffer *rb)
198 {
199 }
200
201
202
203 /**
204 * Allocate a software-based renderbuffer. This is called via the
205 * ctx->Driver.NewRenderbuffer() function when the user creates a new
206 * renderbuffer.
207 * This would not be used for hardware-based renderbuffers.
208 */
209 struct gl_renderbuffer *
210 _swrast_new_soft_renderbuffer(struct gl_context *ctx, GLuint name)
211 {
212 struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, name);
213 if (rb) {
214 rb->AllocStorage = soft_renderbuffer_storage;
215 rb->Delete = soft_renderbuffer_delete;
216 }
217 return rb;
218 }
219
220
221 /**
222 * Add software-based color renderbuffers to the given framebuffer.
223 * This is a helper routine for device drivers when creating a
224 * window system framebuffer (not a user-created render/framebuffer).
225 * Once this function is called, you can basically forget about this
226 * renderbuffer; core Mesa will handle all the buffer management and
227 * rendering!
228 */
229 static GLboolean
230 add_color_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
231 GLuint rgbBits, GLuint alphaBits,
232 GLboolean frontLeft, GLboolean backLeft,
233 GLboolean frontRight, GLboolean backRight)
234 {
235 gl_buffer_index b;
236
237 if (rgbBits > 16 || alphaBits > 16) {
238 _mesa_problem(ctx,
239 "Unsupported bit depth in add_color_renderbuffers");
240 return GL_FALSE;
241 }
242
243 assert(MAX_COLOR_ATTACHMENTS >= 4);
244
245 for (b = BUFFER_FRONT_LEFT; b <= BUFFER_BACK_RIGHT; b++) {
246 struct gl_renderbuffer *rb;
247
248 if (b == BUFFER_FRONT_LEFT && !frontLeft)
249 continue;
250 else if (b == BUFFER_BACK_LEFT && !backLeft)
251 continue;
252 else if (b == BUFFER_FRONT_RIGHT && !frontRight)
253 continue;
254 else if (b == BUFFER_BACK_RIGHT && !backRight)
255 continue;
256
257 assert(fb->Attachment[b].Renderbuffer == NULL);
258
259 rb = _mesa_new_renderbuffer(ctx, 0);
260 if (!rb) {
261 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating color buffer");
262 return GL_FALSE;
263 }
264
265 rb->InternalFormat = GL_RGBA;
266
267 rb->AllocStorage = soft_renderbuffer_storage;
268 _mesa_add_renderbuffer(fb, b, rb);
269 }
270
271 return GL_TRUE;
272 }
273
274
275 /**
276 * Add a software-based depth renderbuffer to the given framebuffer.
277 * This is a helper routine for device drivers when creating a
278 * window system framebuffer (not a user-created render/framebuffer).
279 * Once this function is called, you can basically forget about this
280 * renderbuffer; core Mesa will handle all the buffer management and
281 * rendering!
282 */
283 static GLboolean
284 add_depth_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
285 GLuint depthBits)
286 {
287 struct gl_renderbuffer *rb;
288
289 if (depthBits > 32) {
290 _mesa_problem(ctx,
291 "Unsupported depthBits in add_depth_renderbuffer");
292 return GL_FALSE;
293 }
294
295 assert(fb->Attachment[BUFFER_DEPTH].Renderbuffer == NULL);
296
297 rb = _mesa_new_renderbuffer(ctx, 0);
298 if (!rb) {
299 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating depth buffer");
300 return GL_FALSE;
301 }
302
303 if (depthBits <= 16) {
304 rb->InternalFormat = GL_DEPTH_COMPONENT16;
305 }
306 else if (depthBits <= 24) {
307 rb->InternalFormat = GL_DEPTH_COMPONENT24;
308 }
309 else {
310 rb->InternalFormat = GL_DEPTH_COMPONENT32;
311 }
312
313 rb->AllocStorage = soft_renderbuffer_storage;
314 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
315
316 return GL_TRUE;
317 }
318
319
320 /**
321 * Add a software-based stencil renderbuffer to the given framebuffer.
322 * This is a helper routine for device drivers when creating a
323 * window system framebuffer (not a user-created render/framebuffer).
324 * Once this function is called, you can basically forget about this
325 * renderbuffer; core Mesa will handle all the buffer management and
326 * rendering!
327 */
328 static GLboolean
329 add_stencil_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
330 GLuint stencilBits)
331 {
332 struct gl_renderbuffer *rb;
333
334 if (stencilBits > 16) {
335 _mesa_problem(ctx,
336 "Unsupported stencilBits in add_stencil_renderbuffer");
337 return GL_FALSE;
338 }
339
340 assert(fb->Attachment[BUFFER_STENCIL].Renderbuffer == NULL);
341
342 rb = _mesa_new_renderbuffer(ctx, 0);
343 if (!rb) {
344 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating stencil buffer");
345 return GL_FALSE;
346 }
347
348 assert(stencilBits <= 8);
349 rb->InternalFormat = GL_STENCIL_INDEX8;
350
351 rb->AllocStorage = soft_renderbuffer_storage;
352 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, rb);
353
354 return GL_TRUE;
355 }
356
357
358 static GLboolean
359 add_depth_stencil_renderbuffer(struct gl_context *ctx,
360 struct gl_framebuffer *fb)
361 {
362 struct gl_renderbuffer *rb;
363
364 assert(fb->Attachment[BUFFER_DEPTH].Renderbuffer == NULL);
365 assert(fb->Attachment[BUFFER_STENCIL].Renderbuffer == NULL);
366
367 rb = _mesa_new_renderbuffer(ctx, 0);
368 if (!rb) {
369 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating depth+stencil buffer");
370 return GL_FALSE;
371 }
372
373 rb->InternalFormat = GL_DEPTH_STENCIL;
374
375 rb->AllocStorage = soft_renderbuffer_storage;
376 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
377 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, rb);
378
379 return GL_TRUE;
380 }
381
382
383 /**
384 * Add a software-based accumulation renderbuffer to the given framebuffer.
385 * This is a helper routine for device drivers when creating a
386 * window system framebuffer (not a user-created render/framebuffer).
387 * Once this function is called, you can basically forget about this
388 * renderbuffer; core Mesa will handle all the buffer management and
389 * rendering!
390 */
391 static GLboolean
392 add_accum_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
393 GLuint redBits, GLuint greenBits,
394 GLuint blueBits, GLuint alphaBits)
395 {
396 struct gl_renderbuffer *rb;
397
398 if (redBits > 16 || greenBits > 16 || blueBits > 16 || alphaBits > 16) {
399 _mesa_problem(ctx,
400 "Unsupported accumBits in add_accum_renderbuffer");
401 return GL_FALSE;
402 }
403
404 assert(fb->Attachment[BUFFER_ACCUM].Renderbuffer == NULL);
405
406 rb = _mesa_new_renderbuffer(ctx, 0);
407 if (!rb) {
408 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating accum buffer");
409 return GL_FALSE;
410 }
411
412 rb->InternalFormat = GL_RGBA16_SNORM;
413 rb->AllocStorage = soft_renderbuffer_storage;
414 _mesa_add_renderbuffer(fb, BUFFER_ACCUM, rb);
415
416 return GL_TRUE;
417 }
418
419
420
421 /**
422 * Add a software-based aux renderbuffer to the given framebuffer.
423 * This is a helper routine for device drivers when creating a
424 * window system framebuffer (not a user-created render/framebuffer).
425 * Once this function is called, you can basically forget about this
426 * renderbuffer; core Mesa will handle all the buffer management and
427 * rendering!
428 *
429 * NOTE: color-index aux buffers not supported.
430 */
431 static GLboolean
432 add_aux_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
433 GLuint colorBits, GLuint numBuffers)
434 {
435 GLuint i;
436
437 if (colorBits > 16) {
438 _mesa_problem(ctx,
439 "Unsupported colorBits in add_aux_renderbuffers");
440 return GL_FALSE;
441 }
442
443 assert(numBuffers <= MAX_AUX_BUFFERS);
444
445 for (i = 0; i < numBuffers; i++) {
446 struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, 0);
447
448 assert(fb->Attachment[BUFFER_AUX0 + i].Renderbuffer == NULL);
449
450 if (!rb) {
451 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating aux buffer");
452 return GL_FALSE;
453 }
454
455 assert (colorBits <= 8);
456 rb->InternalFormat = GL_RGBA;
457
458 rb->AllocStorage = soft_renderbuffer_storage;
459 _mesa_add_renderbuffer(fb, BUFFER_AUX0 + i, rb);
460 }
461 return GL_TRUE;
462 }
463
464
465 /**
466 * Create/attach software-based renderbuffers to the given framebuffer.
467 * This is a helper routine for device drivers. Drivers can just as well
468 * call the individual _mesa_add_*_renderbuffer() routines directly.
469 */
470 void
471 _swrast_add_soft_renderbuffers(struct gl_framebuffer *fb,
472 GLboolean color,
473 GLboolean depth,
474 GLboolean stencil,
475 GLboolean accum,
476 GLboolean alpha,
477 GLboolean aux)
478 {
479 GLboolean frontLeft = GL_TRUE;
480 GLboolean backLeft = fb->Visual.doubleBufferMode;
481 GLboolean frontRight = fb->Visual.stereoMode;
482 GLboolean backRight = fb->Visual.stereoMode && fb->Visual.doubleBufferMode;
483
484 if (color) {
485 assert(fb->Visual.redBits == fb->Visual.greenBits);
486 assert(fb->Visual.redBits == fb->Visual.blueBits);
487 add_color_renderbuffers(NULL, fb,
488 fb->Visual.redBits,
489 fb->Visual.alphaBits,
490 frontLeft, backLeft,
491 frontRight, backRight);
492 }
493
494 #if 0
495 /* This is pretty much for debugging purposes only since there's a perf
496 * hit for using combined depth/stencil in swrast.
497 */
498 if (depth && fb->Visual.depthBits == 24 &&
499 stencil && fb->Visual.stencilBits == 8) {
500 /* use combined depth/stencil buffer */
501 add_depth_stencil_renderbuffer(NULL, fb);
502 }
503 else
504 #else
505 (void) add_depth_stencil_renderbuffer;
506 #endif
507 {
508 if (depth) {
509 assert(fb->Visual.depthBits > 0);
510 add_depth_renderbuffer(NULL, fb, fb->Visual.depthBits);
511 }
512
513 if (stencil) {
514 assert(fb->Visual.stencilBits > 0);
515 add_stencil_renderbuffer(NULL, fb, fb->Visual.stencilBits);
516 }
517 }
518
519 if (accum) {
520 assert(fb->Visual.accumRedBits > 0);
521 assert(fb->Visual.accumGreenBits > 0);
522 assert(fb->Visual.accumBlueBits > 0);
523 add_accum_renderbuffer(NULL, fb,
524 fb->Visual.accumRedBits,
525 fb->Visual.accumGreenBits,
526 fb->Visual.accumBlueBits,
527 fb->Visual.accumAlphaBits);
528 }
529
530 if (aux) {
531 assert(fb->Visual.numAuxBuffers > 0);
532 add_aux_renderbuffers(NULL, fb, fb->Visual.redBits,
533 fb->Visual.numAuxBuffers);
534 }
535
536 #if 0
537 if (multisample) {
538 /* maybe someday */
539 }
540 #endif
541 }