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