2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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 software-based renderbuffers.
28 * Also, routines for reading/writing software-based renderbuffer data as
29 * ubytes, ushorts, uints, etc.
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"
45 * This is the default software fallback for gl_renderbuffer's span
48 * The assumptions are that rb->Data will be a pointer to (0,0), that pixels
49 * are packed in the type of rb->Format, and that subsequent rows appear
50 * rb->RowStride pixels later.
53 _swrast_set_renderbuffer_accessors(struct gl_renderbuffer
*rb
)
56 case MESA_FORMAT_RGB888
:
57 rb
->DataType
= GL_UNSIGNED_BYTE
;
60 case MESA_FORMAT_RGBA8888
:
61 case MESA_FORMAT_RGBA8888_REV
:
62 rb
->DataType
= GL_UNSIGNED_BYTE
;
66 rb
->DataType
= GL_UNSIGNED_BYTE
;
70 case MESA_FORMAT_GR88
:
71 rb
->DataType
= GL_UNSIGNED_BYTE
;
75 rb
->DataType
= GL_UNSIGNED_SHORT
;
79 case MESA_FORMAT_RG1616
:
80 rb
->DataType
= GL_UNSIGNED_SHORT
;
83 case MESA_FORMAT_SIGNED_RGBA_16
:
84 rb
->DataType
= GL_SHORT
;
88 rb
->DataType
= GL_UNSIGNED_BYTE
;
92 rb
->DataType
= GL_UNSIGNED_SHORT
;
96 case MESA_FORMAT_X8_Z24
:
97 case MESA_FORMAT_Z24_X8
:
98 rb
->DataType
= GL_UNSIGNED_INT
;
101 case MESA_FORMAT_Z24_S8
:
102 case MESA_FORMAT_S8_Z24
:
103 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
106 case MESA_FORMAT_RGBA_FLOAT32
:
107 rb
->DataType
= GL_FLOAT
;
110 case MESA_FORMAT_INTENSITY_FLOAT32
:
111 rb
->DataType
= GL_FLOAT
;
114 case MESA_FORMAT_LUMINANCE_FLOAT32
:
115 rb
->DataType
= GL_FLOAT
;
118 case MESA_FORMAT_ALPHA_FLOAT32
:
119 rb
->DataType
= GL_FLOAT
;
122 case MESA_FORMAT_RG_FLOAT32
:
123 rb
->DataType
= GL_FLOAT
;
126 case MESA_FORMAT_R_FLOAT32
:
127 rb
->DataType
= GL_FLOAT
;
136 * This is a software fallback for the gl_renderbuffer->AllocStorage
138 * Device drivers will typically override this function for the buffers
139 * which it manages (typically color buffers, Z and stencil).
140 * Other buffers (like software accumulation and aux buffers) which the driver
141 * doesn't manage can be handled with this function.
143 * This one multi-purpose function can allocate stencil, depth, accum, color
144 * or color-index buffers!
147 soft_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
148 GLenum internalFormat
,
149 GLuint width
, GLuint height
)
151 switch (internalFormat
) {
160 rb
->Format
= MESA_FORMAT_RGB888
;
171 if (_mesa_little_endian())
172 rb
->Format
= MESA_FORMAT_RGBA8888_REV
;
174 rb
->Format
= MESA_FORMAT_RGBA8888
;
177 case GL_RGBA16_SNORM
:
178 /* for accum buffer */
179 rb
->Format
= MESA_FORMAT_SIGNED_RGBA_16
;
181 case GL_STENCIL_INDEX
:
182 case GL_STENCIL_INDEX1_EXT
:
183 case GL_STENCIL_INDEX4_EXT
:
184 case GL_STENCIL_INDEX8_EXT
:
185 case GL_STENCIL_INDEX16_EXT
:
186 rb
->Format
= MESA_FORMAT_S8
;
188 case GL_DEPTH_COMPONENT
:
189 case GL_DEPTH_COMPONENT16
:
190 rb
->Format
= MESA_FORMAT_Z16
;
192 case GL_DEPTH_COMPONENT24
:
193 rb
->Format
= MESA_FORMAT_X8_Z24
;
195 case GL_DEPTH_COMPONENT32
:
196 rb
->Format
= MESA_FORMAT_Z32
;
198 case GL_DEPTH_STENCIL_EXT
:
199 case GL_DEPTH24_STENCIL8_EXT
:
200 rb
->Format
= MESA_FORMAT_Z24_S8
;
203 /* unsupported format */
207 _swrast_set_renderbuffer_accessors(rb
);
209 ASSERT(rb
->DataType
);
211 /* free old buffer storage */
217 rb
->RowStride
= width
;
219 if (width
> 0 && height
> 0) {
220 /* allocate new buffer storage */
221 rb
->Data
= malloc(width
* height
* _mesa_get_format_bytes(rb
->Format
));
223 if (rb
->Data
== NULL
) {
227 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
228 "software renderbuffer allocation (%d x %d x %d)",
229 width
, height
, _mesa_get_format_bytes(rb
->Format
));
236 rb
->_BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
239 internalFormat
== GL_RGBA16_SNORM
&&
240 rb
->_BaseFormat
== 0) {
241 /* NOTE: This is a special case just for accumulation buffers.
242 * This is a very limited use case- there's no snorm texturing or
243 * rendering going on.
245 rb
->_BaseFormat
= GL_RGBA
;
248 /* the internalFormat should have been error checked long ago */
249 ASSERT(rb
->_BaseFormat
);
257 _swrast_map_soft_renderbuffer(struct gl_context
*ctx
,
258 struct gl_renderbuffer
*rb
,
259 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
264 GLubyte
*map
= rb
->Data
;
265 int cpp
= _mesa_get_format_bytes(rb
->Format
);
266 int stride
= rb
->RowStride
* cpp
;
274 *out_stride
= stride
;
279 _swrast_unmap_soft_renderbuffer(struct gl_context
*ctx
,
280 struct gl_renderbuffer
*rb
)
287 * Allocate a software-based renderbuffer. This is called via the
288 * ctx->Driver.NewRenderbuffer() function when the user creates a new
290 * This would not be used for hardware-based renderbuffers.
292 struct gl_renderbuffer
*
293 _swrast_new_soft_renderbuffer(struct gl_context
*ctx
, GLuint name
)
295 struct gl_renderbuffer
*rb
= _mesa_new_renderbuffer(ctx
, name
);
297 rb
->AllocStorage
= soft_renderbuffer_storage
;
304 * Add software-based color renderbuffers to the given framebuffer.
305 * This is a helper routine for device drivers when creating a
306 * window system framebuffer (not a user-created render/framebuffer).
307 * Once this function is called, you can basically forget about this
308 * renderbuffer; core Mesa will handle all the buffer management and
312 add_color_renderbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
313 GLuint rgbBits
, GLuint alphaBits
,
314 GLboolean frontLeft
, GLboolean backLeft
,
315 GLboolean frontRight
, GLboolean backRight
)
319 if (rgbBits
> 16 || alphaBits
> 16) {
321 "Unsupported bit depth in add_color_renderbuffers");
325 assert(MAX_COLOR_ATTACHMENTS
>= 4);
327 for (b
= BUFFER_FRONT_LEFT
; b
<= BUFFER_BACK_RIGHT
; b
++) {
328 struct gl_renderbuffer
*rb
;
330 if (b
== BUFFER_FRONT_LEFT
&& !frontLeft
)
332 else if (b
== BUFFER_BACK_LEFT
&& !backLeft
)
334 else if (b
== BUFFER_FRONT_RIGHT
&& !frontRight
)
336 else if (b
== BUFFER_BACK_RIGHT
&& !backRight
)
339 assert(fb
->Attachment
[b
].Renderbuffer
== NULL
);
341 rb
= _mesa_new_renderbuffer(ctx
, 0);
343 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating color buffer");
347 rb
->InternalFormat
= GL_RGBA
;
349 rb
->AllocStorage
= soft_renderbuffer_storage
;
350 _mesa_add_renderbuffer(fb
, b
, rb
);
358 * Add a software-based depth renderbuffer to the given framebuffer.
359 * This is a helper routine for device drivers when creating a
360 * window system framebuffer (not a user-created render/framebuffer).
361 * Once this function is called, you can basically forget about this
362 * renderbuffer; core Mesa will handle all the buffer management and
366 add_depth_renderbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
369 struct gl_renderbuffer
*rb
;
371 if (depthBits
> 32) {
373 "Unsupported depthBits in add_depth_renderbuffer");
377 assert(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
== NULL
);
379 rb
= _mesa_new_renderbuffer(ctx
, 0);
381 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating depth buffer");
385 if (depthBits
<= 16) {
386 rb
->InternalFormat
= GL_DEPTH_COMPONENT16
;
388 else if (depthBits
<= 24) {
389 rb
->InternalFormat
= GL_DEPTH_COMPONENT24
;
392 rb
->InternalFormat
= GL_DEPTH_COMPONENT32
;
395 rb
->AllocStorage
= soft_renderbuffer_storage
;
396 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, rb
);
403 * Add a software-based stencil renderbuffer to the given framebuffer.
404 * This is a helper routine for device drivers when creating a
405 * window system framebuffer (not a user-created render/framebuffer).
406 * Once this function is called, you can basically forget about this
407 * renderbuffer; core Mesa will handle all the buffer management and
411 add_stencil_renderbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
414 struct gl_renderbuffer
*rb
;
416 if (stencilBits
> 16) {
418 "Unsupported stencilBits in add_stencil_renderbuffer");
422 assert(fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
== NULL
);
424 rb
= _mesa_new_renderbuffer(ctx
, 0);
426 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating stencil buffer");
430 assert(stencilBits
<= 8);
431 rb
->InternalFormat
= GL_STENCIL_INDEX8
;
433 rb
->AllocStorage
= soft_renderbuffer_storage
;
434 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, rb
);
441 add_depth_stencil_renderbuffer(struct gl_context
*ctx
,
442 struct gl_framebuffer
*fb
)
444 struct gl_renderbuffer
*rb
;
446 assert(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
== NULL
);
447 assert(fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
== NULL
);
449 rb
= _mesa_new_renderbuffer(ctx
, 0);
451 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating depth+stencil buffer");
455 rb
->InternalFormat
= GL_DEPTH_STENCIL
;
457 rb
->AllocStorage
= soft_renderbuffer_storage
;
458 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, rb
);
459 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, rb
);
466 * Add a software-based accumulation renderbuffer to the given framebuffer.
467 * This is a helper routine for device drivers when creating a
468 * window system framebuffer (not a user-created render/framebuffer).
469 * Once this function is called, you can basically forget about this
470 * renderbuffer; core Mesa will handle all the buffer management and
474 add_accum_renderbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
475 GLuint redBits
, GLuint greenBits
,
476 GLuint blueBits
, GLuint alphaBits
)
478 struct gl_renderbuffer
*rb
;
480 if (redBits
> 16 || greenBits
> 16 || blueBits
> 16 || alphaBits
> 16) {
482 "Unsupported accumBits in add_accum_renderbuffer");
486 assert(fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
== NULL
);
488 rb
= _mesa_new_renderbuffer(ctx
, 0);
490 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating accum buffer");
494 rb
->InternalFormat
= GL_RGBA16_SNORM
;
495 rb
->AllocStorage
= soft_renderbuffer_storage
;
496 _mesa_add_renderbuffer(fb
, BUFFER_ACCUM
, rb
);
504 * Add a software-based aux renderbuffer to the given framebuffer.
505 * This is a helper routine for device drivers when creating a
506 * window system framebuffer (not a user-created render/framebuffer).
507 * Once this function is called, you can basically forget about this
508 * renderbuffer; core Mesa will handle all the buffer management and
511 * NOTE: color-index aux buffers not supported.
514 add_aux_renderbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
515 GLuint colorBits
, GLuint numBuffers
)
519 if (colorBits
> 16) {
521 "Unsupported colorBits in add_aux_renderbuffers");
525 assert(numBuffers
<= MAX_AUX_BUFFERS
);
527 for (i
= 0; i
< numBuffers
; i
++) {
528 struct gl_renderbuffer
*rb
= _mesa_new_renderbuffer(ctx
, 0);
530 assert(fb
->Attachment
[BUFFER_AUX0
+ i
].Renderbuffer
== NULL
);
533 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating aux buffer");
537 assert (colorBits
<= 8);
538 rb
->InternalFormat
= GL_RGBA
;
540 rb
->AllocStorage
= soft_renderbuffer_storage
;
541 _mesa_add_renderbuffer(fb
, BUFFER_AUX0
+ i
, rb
);
548 * Create/attach software-based renderbuffers to the given framebuffer.
549 * This is a helper routine for device drivers. Drivers can just as well
550 * call the individual _mesa_add_*_renderbuffer() routines directly.
553 _swrast_add_soft_renderbuffers(struct gl_framebuffer
*fb
,
561 GLboolean frontLeft
= GL_TRUE
;
562 GLboolean backLeft
= fb
->Visual
.doubleBufferMode
;
563 GLboolean frontRight
= fb
->Visual
.stereoMode
;
564 GLboolean backRight
= fb
->Visual
.stereoMode
&& fb
->Visual
.doubleBufferMode
;
567 assert(fb
->Visual
.redBits
== fb
->Visual
.greenBits
);
568 assert(fb
->Visual
.redBits
== fb
->Visual
.blueBits
);
569 add_color_renderbuffers(NULL
, fb
,
571 fb
->Visual
.alphaBits
,
573 frontRight
, backRight
);
577 /* This is pretty much for debugging purposes only since there's a perf
578 * hit for using combined depth/stencil in swrast.
580 if (depth
&& fb
->Visual
.depthBits
== 24 &&
581 stencil
&& fb
->Visual
.stencilBits
== 8) {
582 /* use combined depth/stencil buffer */
583 add_depth_stencil_renderbuffer(NULL
, fb
);
587 (void) add_depth_stencil_renderbuffer
;
591 assert(fb
->Visual
.depthBits
> 0);
592 add_depth_renderbuffer(NULL
, fb
, fb
->Visual
.depthBits
);
596 assert(fb
->Visual
.stencilBits
> 0);
597 add_stencil_renderbuffer(NULL
, fb
, fb
->Visual
.stencilBits
);
602 assert(fb
->Visual
.accumRedBits
> 0);
603 assert(fb
->Visual
.accumGreenBits
> 0);
604 assert(fb
->Visual
.accumBlueBits
> 0);
605 add_accum_renderbuffer(NULL
, fb
,
606 fb
->Visual
.accumRedBits
,
607 fb
->Visual
.accumGreenBits
,
608 fb
->Visual
.accumBlueBits
,
609 fb
->Visual
.accumAlphaBits
);
613 assert(fb
->Visual
.numAuxBuffers
> 0);
614 add_aux_renderbuffers(NULL
, fb
, fb
->Visual
.redBits
,
615 fb
->Visual
.numAuxBuffers
);