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_context.h"
41 #include "swrast/s_renderbuffer.h"
45 * This is a software fallback for the gl_renderbuffer->AllocStorage
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.
52 * This one multi-purpose function can allocate stencil, depth, accum, color
53 * or color-index buffers!
56 soft_renderbuffer_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
57 GLenum internalFormat
,
58 GLuint width
, GLuint height
)
60 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
63 switch (internalFormat
) {
72 rb
->Format
= MESA_FORMAT_RGB888
;
83 if (_mesa_little_endian())
84 rb
->Format
= MESA_FORMAT_RGBA8888_REV
;
86 rb
->Format
= MESA_FORMAT_RGBA8888
;
90 /* for accum buffer */
91 rb
->Format
= MESA_FORMAT_SIGNED_RGBA_16
;
93 case GL_STENCIL_INDEX
:
94 case GL_STENCIL_INDEX1_EXT
:
95 case GL_STENCIL_INDEX4_EXT
:
96 case GL_STENCIL_INDEX8_EXT
:
97 case GL_STENCIL_INDEX16_EXT
:
98 rb
->Format
= MESA_FORMAT_S8
;
100 case GL_DEPTH_COMPONENT
:
101 case GL_DEPTH_COMPONENT16
:
102 rb
->Format
= MESA_FORMAT_Z16
;
104 case GL_DEPTH_COMPONENT24
:
105 rb
->Format
= MESA_FORMAT_X8_Z24
;
107 case GL_DEPTH_COMPONENT32
:
108 rb
->Format
= MESA_FORMAT_Z32
;
110 case GL_DEPTH_STENCIL_EXT
:
111 case GL_DEPTH24_STENCIL8_EXT
:
112 rb
->Format
= MESA_FORMAT_Z24_S8
;
115 /* unsupported format */
119 bpp
= _mesa_get_format_bytes(rb
->Format
);
121 /* free old buffer storage */
127 srb
->RowStride
= width
* bpp
;
129 if (width
> 0 && height
> 0) {
130 /* allocate new buffer storage */
131 srb
->Buffer
= malloc(srb
->RowStride
* height
);
133 if (srb
->Buffer
== NULL
) {
136 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
137 "software renderbuffer allocation (%d x %d x %d)",
145 rb
->_BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
148 internalFormat
== GL_RGBA16_SNORM
&&
149 rb
->_BaseFormat
== 0) {
150 /* NOTE: This is a special case just for accumulation buffers.
151 * This is a very limited use case- there's no snorm texturing or
152 * rendering going on.
154 rb
->_BaseFormat
= GL_RGBA
;
157 /* the internalFormat should have been error checked long ago */
158 ASSERT(rb
->_BaseFormat
);
166 * Called via gl_renderbuffer::Delete()
169 soft_renderbuffer_delete(struct gl_renderbuffer
*rb
)
171 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
177 _mesa_delete_renderbuffer(rb
);
182 _swrast_map_soft_renderbuffer(struct gl_context
*ctx
,
183 struct gl_renderbuffer
*rb
,
184 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
189 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
190 GLubyte
*map
= srb
->Buffer
;
191 int cpp
= _mesa_get_format_bytes(rb
->Format
);
192 int stride
= rb
->Width
* cpp
;
203 *out_stride
= stride
;
208 _swrast_unmap_soft_renderbuffer(struct gl_context
*ctx
,
209 struct gl_renderbuffer
*rb
)
216 * Allocate a software-based renderbuffer. This is called via the
217 * ctx->Driver.NewRenderbuffer() function when the user creates a new
219 * This would not be used for hardware-based renderbuffers.
221 struct gl_renderbuffer
*
222 _swrast_new_soft_renderbuffer(struct gl_context
*ctx
, GLuint name
)
224 struct swrast_renderbuffer
*srb
= CALLOC_STRUCT(swrast_renderbuffer
);
226 _mesa_init_renderbuffer(&srb
->Base
, name
);
227 srb
->Base
.AllocStorage
= soft_renderbuffer_storage
;
228 srb
->Base
.Delete
= soft_renderbuffer_delete
;
235 * Add software-based color renderbuffers to the given framebuffer.
236 * This is a helper routine for device drivers when creating a
237 * window system framebuffer (not a user-created render/framebuffer).
238 * Once this function is called, you can basically forget about this
239 * renderbuffer; core Mesa will handle all the buffer management and
243 add_color_renderbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
244 GLuint rgbBits
, GLuint alphaBits
,
245 GLboolean frontLeft
, GLboolean backLeft
,
246 GLboolean frontRight
, GLboolean backRight
)
250 if (rgbBits
> 16 || alphaBits
> 16) {
252 "Unsupported bit depth in add_color_renderbuffers");
256 assert(MAX_COLOR_ATTACHMENTS
>= 4);
258 for (b
= BUFFER_FRONT_LEFT
; b
<= BUFFER_BACK_RIGHT
; b
++) {
259 struct gl_renderbuffer
*rb
;
261 if (b
== BUFFER_FRONT_LEFT
&& !frontLeft
)
263 else if (b
== BUFFER_BACK_LEFT
&& !backLeft
)
265 else if (b
== BUFFER_FRONT_RIGHT
&& !frontRight
)
267 else if (b
== BUFFER_BACK_RIGHT
&& !backRight
)
270 assert(fb
->Attachment
[b
].Renderbuffer
== NULL
);
272 rb
= ctx
->Driver
.NewRenderbuffer(ctx
, 0);
274 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating color buffer");
278 rb
->InternalFormat
= GL_RGBA
;
280 rb
->AllocStorage
= soft_renderbuffer_storage
;
281 _mesa_add_renderbuffer(fb
, b
, rb
);
289 * Add a software-based depth renderbuffer to the given framebuffer.
290 * This is a helper routine for device drivers when creating a
291 * window system framebuffer (not a user-created render/framebuffer).
292 * Once this function is called, you can basically forget about this
293 * renderbuffer; core Mesa will handle all the buffer management and
297 add_depth_renderbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
300 struct gl_renderbuffer
*rb
;
302 if (depthBits
> 32) {
304 "Unsupported depthBits in add_depth_renderbuffer");
308 assert(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
== NULL
);
310 rb
= _swrast_new_soft_renderbuffer(ctx
, 0);
312 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating depth buffer");
316 if (depthBits
<= 16) {
317 rb
->InternalFormat
= GL_DEPTH_COMPONENT16
;
319 else if (depthBits
<= 24) {
320 rb
->InternalFormat
= GL_DEPTH_COMPONENT24
;
323 rb
->InternalFormat
= GL_DEPTH_COMPONENT32
;
326 rb
->AllocStorage
= soft_renderbuffer_storage
;
327 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, rb
);
334 * Add a software-based stencil renderbuffer to the given framebuffer.
335 * This is a helper routine for device drivers when creating a
336 * window system framebuffer (not a user-created render/framebuffer).
337 * Once this function is called, you can basically forget about this
338 * renderbuffer; core Mesa will handle all the buffer management and
342 add_stencil_renderbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
345 struct gl_renderbuffer
*rb
;
347 if (stencilBits
> 16) {
349 "Unsupported stencilBits in add_stencil_renderbuffer");
353 assert(fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
== NULL
);
355 rb
= _swrast_new_soft_renderbuffer(ctx
, 0);
357 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating stencil buffer");
361 assert(stencilBits
<= 8);
362 rb
->InternalFormat
= GL_STENCIL_INDEX8
;
364 rb
->AllocStorage
= soft_renderbuffer_storage
;
365 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, rb
);
372 add_depth_stencil_renderbuffer(struct gl_context
*ctx
,
373 struct gl_framebuffer
*fb
)
375 struct gl_renderbuffer
*rb
;
377 assert(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
== NULL
);
378 assert(fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
== NULL
);
380 rb
= _swrast_new_soft_renderbuffer(ctx
, 0);
382 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating depth+stencil buffer");
386 rb
->InternalFormat
= GL_DEPTH_STENCIL
;
388 rb
->AllocStorage
= soft_renderbuffer_storage
;
389 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, rb
);
390 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, rb
);
397 * Add a software-based accumulation renderbuffer to the given framebuffer.
398 * This is a helper routine for device drivers when creating a
399 * window system framebuffer (not a user-created render/framebuffer).
400 * Once this function is called, you can basically forget about this
401 * renderbuffer; core Mesa will handle all the buffer management and
405 add_accum_renderbuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
406 GLuint redBits
, GLuint greenBits
,
407 GLuint blueBits
, GLuint alphaBits
)
409 struct gl_renderbuffer
*rb
;
411 if (redBits
> 16 || greenBits
> 16 || blueBits
> 16 || alphaBits
> 16) {
413 "Unsupported accumBits in add_accum_renderbuffer");
417 assert(fb
->Attachment
[BUFFER_ACCUM
].Renderbuffer
== NULL
);
419 rb
= _swrast_new_soft_renderbuffer(ctx
, 0);
421 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating accum buffer");
425 rb
->InternalFormat
= GL_RGBA16_SNORM
;
426 rb
->AllocStorage
= soft_renderbuffer_storage
;
427 _mesa_add_renderbuffer(fb
, BUFFER_ACCUM
, rb
);
435 * Add a software-based aux renderbuffer to the given framebuffer.
436 * This is a helper routine for device drivers when creating a
437 * window system framebuffer (not a user-created render/framebuffer).
438 * Once this function is called, you can basically forget about this
439 * renderbuffer; core Mesa will handle all the buffer management and
442 * NOTE: color-index aux buffers not supported.
445 add_aux_renderbuffers(struct gl_context
*ctx
, struct gl_framebuffer
*fb
,
446 GLuint colorBits
, GLuint numBuffers
)
450 if (colorBits
> 16) {
452 "Unsupported colorBits in add_aux_renderbuffers");
456 assert(numBuffers
<= MAX_AUX_BUFFERS
);
458 for (i
= 0; i
< numBuffers
; i
++) {
459 struct gl_renderbuffer
*rb
= _swrast_new_soft_renderbuffer(ctx
, 0);
461 assert(fb
->Attachment
[BUFFER_AUX0
+ i
].Renderbuffer
== NULL
);
464 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Allocating aux buffer");
468 assert (colorBits
<= 8);
469 rb
->InternalFormat
= GL_RGBA
;
471 rb
->AllocStorage
= soft_renderbuffer_storage
;
472 _mesa_add_renderbuffer(fb
, BUFFER_AUX0
+ i
, rb
);
479 * Create/attach software-based renderbuffers to the given framebuffer.
480 * This is a helper routine for device drivers. Drivers can just as well
481 * call the individual _mesa_add_*_renderbuffer() routines directly.
484 _swrast_add_soft_renderbuffers(struct gl_framebuffer
*fb
,
492 GLboolean frontLeft
= GL_TRUE
;
493 GLboolean backLeft
= fb
->Visual
.doubleBufferMode
;
494 GLboolean frontRight
= fb
->Visual
.stereoMode
;
495 GLboolean backRight
= fb
->Visual
.stereoMode
&& fb
->Visual
.doubleBufferMode
;
498 assert(fb
->Visual
.redBits
== fb
->Visual
.greenBits
);
499 assert(fb
->Visual
.redBits
== fb
->Visual
.blueBits
);
500 add_color_renderbuffers(NULL
, fb
,
502 fb
->Visual
.alphaBits
,
504 frontRight
, backRight
);
508 /* This is pretty much for debugging purposes only since there's a perf
509 * hit for using combined depth/stencil in swrast.
511 if (depth
&& fb
->Visual
.depthBits
== 24 &&
512 stencil
&& fb
->Visual
.stencilBits
== 8) {
513 /* use combined depth/stencil buffer */
514 add_depth_stencil_renderbuffer(NULL
, fb
);
518 (void) add_depth_stencil_renderbuffer
;
522 assert(fb
->Visual
.depthBits
> 0);
523 add_depth_renderbuffer(NULL
, fb
, fb
->Visual
.depthBits
);
527 assert(fb
->Visual
.stencilBits
> 0);
528 add_stencil_renderbuffer(NULL
, fb
, fb
->Visual
.stencilBits
);
533 assert(fb
->Visual
.accumRedBits
> 0);
534 assert(fb
->Visual
.accumGreenBits
> 0);
535 assert(fb
->Visual
.accumBlueBits
> 0);
536 add_accum_renderbuffer(NULL
, fb
,
537 fb
->Visual
.accumRedBits
,
538 fb
->Visual
.accumGreenBits
,
539 fb
->Visual
.accumBlueBits
,
540 fb
->Visual
.accumAlphaBits
);
544 assert(fb
->Visual
.numAuxBuffers
> 0);
545 add_aux_renderbuffers(NULL
, fb
, fb
->Visual
.redBits
,
546 fb
->Visual
.numAuxBuffers
);
559 map_attachment(struct gl_context
*ctx
,
560 struct gl_framebuffer
*fb
,
561 gl_buffer_index buffer
)
563 struct gl_texture_object
*texObj
= fb
->Attachment
[buffer
].Texture
;
564 struct gl_renderbuffer
*rb
= fb
->Attachment
[buffer
].Renderbuffer
;
565 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
568 /* map texture image (render to texture) */
569 const GLuint level
= fb
->Attachment
[buffer
].TextureLevel
;
570 const GLuint face
= fb
->Attachment
[buffer
].CubeMapFace
;
571 const GLuint slice
= fb
->Attachment
[buffer
].Zoffset
;
572 struct gl_texture_image
*texImage
= texObj
->Image
[face
][level
];
574 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
,
575 0, 0, texImage
->Width
, texImage
->Height
,
576 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
577 &srb
->Map
, &srb
->RowStride
);
581 /* Map ordinary renderbuffer */
582 ctx
->Driver
.MapRenderbuffer(ctx
, rb
,
583 0, 0, rb
->Width
, rb
->Height
,
584 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
585 &srb
->Map
, &srb
->RowStride
);
593 unmap_attachment(struct gl_context
*ctx
,
594 struct gl_framebuffer
*fb
,
595 gl_buffer_index buffer
)
597 struct gl_texture_object
*texObj
= fb
->Attachment
[buffer
].Texture
;
598 struct gl_renderbuffer
*rb
= fb
->Attachment
[buffer
].Renderbuffer
;
599 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
602 /* unmap texture image (render to texture) */
603 const GLuint level
= fb
->Attachment
[buffer
].TextureLevel
;
604 const GLuint face
= fb
->Attachment
[buffer
].CubeMapFace
;
605 const GLuint slice
= fb
->Attachment
[buffer
].Zoffset
;
606 struct gl_texture_image
*texImage
= texObj
->Image
[face
][level
];
608 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
);
612 /* unmap ordinary renderbuffer */
613 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
621 * Determine what type to use (ubyte vs. float) for span colors for the
622 * given renderbuffer.
623 * See also _swrast_write_rgba_span().
626 find_renderbuffer_colortype(struct gl_renderbuffer
*rb
)
628 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
629 GLuint rbMaxBits
= _mesa_get_format_max_bits(rb
->Format
);
630 GLenum rbDatatype
= _mesa_get_format_datatype(rb
->Format
);
632 if (rbDatatype
== GL_UNSIGNED_NORMALIZED
&& rbMaxBits
<= 8) {
633 /* the buffer's values fit in GLubyte values */
634 srb
->ColorType
= GL_UNSIGNED_BYTE
;
637 /* use floats otherwise */
638 srb
->ColorType
= GL_FLOAT
;
644 * Map the renderbuffers we'll use for tri/line/point rendering.
647 _swrast_map_renderbuffers(struct gl_context
*ctx
)
649 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
650 struct gl_renderbuffer
*depthRb
, *stencilRb
;
653 depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
655 /* map depth buffer */
656 map_attachment(ctx
, fb
, BUFFER_DEPTH
);
659 stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
660 if (stencilRb
&& stencilRb
!= depthRb
) {
661 /* map stencil buffer */
662 map_attachment(ctx
, fb
, BUFFER_STENCIL
);
665 for (buf
= 0; buf
< fb
->_NumColorDrawBuffers
; buf
++) {
666 map_attachment(ctx
, fb
, fb
->_ColorDrawBufferIndexes
[buf
]);
667 find_renderbuffer_colortype(fb
->_ColorDrawBuffers
[buf
]);
673 * Unmap renderbuffers after rendering.
676 _swrast_unmap_renderbuffers(struct gl_context
*ctx
)
678 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
679 struct gl_renderbuffer
*depthRb
, *stencilRb
;
682 depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
684 /* map depth buffer */
685 unmap_attachment(ctx
, fb
, BUFFER_DEPTH
);
688 stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
689 if (stencilRb
&& stencilRb
!= depthRb
) {
690 /* map stencil buffer */
691 unmap_attachment(ctx
, fb
, BUFFER_STENCIL
);
694 for (buf
= 0; buf
< fb
->_NumColorDrawBuffers
; buf
++) {
695 unmap_attachment(ctx
, fb
, fb
->_ColorDrawBufferIndexes
[buf
]);