1 /* $Id: s_buffers.c,v 1.4 2001/01/24 00:04:59 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #include "s_alphabuf.h"
35 #include "s_masking.h"
36 #include "s_stencil.h"
42 * Clear the color buffer when glColorMask or glIndexMask is in effect.
45 clear_color_buffer_with_masking( GLcontext
*ctx
)
47 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
48 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
49 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
50 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
52 if (ctx
->Visual
.rgbMode
) {
54 const GLchan r
= ctx
->Color
.ClearColor
[0];
55 const GLchan g
= ctx
->Color
.ClearColor
[1];
56 const GLchan b
= ctx
->Color
.ClearColor
[2];
57 const GLchan a
= ctx
->Color
.ClearColor
[3];
59 for (i
= 0; i
< height
; i
++) {
60 GLchan rgba
[MAX_WIDTH
][4];
62 for (j
= 0; j
< width
; j
++) {
68 _mesa_mask_rgba_span( ctx
, width
, x
, y
+ i
, rgba
);
69 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, x
, y
+ i
,
70 (CONST
GLchan (*)[4]) rgba
, NULL
);
74 /* Color index mode */
75 GLuint span
[MAX_WIDTH
];
76 GLubyte mask
[MAX_WIDTH
];
78 MEMSET( mask
, 1, width
);
79 for (i
=0;i
<height
;i
++) {
80 for (j
=0;j
<width
;j
++) {
81 span
[j
] = ctx
->Color
.ClearIndex
;
83 _mesa_mask_index_span( ctx
, width
, x
, y
+ i
, span
);
84 (*ctx
->Driver
.WriteCI32Span
)( ctx
, width
, x
, y
+ i
, span
, mask
);
92 * Clear a color buffer without index/channel masking.
95 clear_color_buffer(GLcontext
*ctx
)
97 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
98 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
99 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
100 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
102 if (ctx
->Visual
.rgbMode
) {
104 const GLchan r
= ctx
->Color
.ClearColor
[0];
105 const GLchan g
= ctx
->Color
.ClearColor
[1];
106 const GLchan b
= ctx
->Color
.ClearColor
[2];
107 const GLchan a
= ctx
->Color
.ClearColor
[3];
108 GLchan span
[MAX_WIDTH
][4];
111 ASSERT(*((GLuint
*) &ctx
->Color
.ColorMask
) == 0xffffffff);
113 for (i
= 0; i
< width
; i
++) {
119 for (i
= 0; i
< height
; i
++) {
120 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, width
, x
, y
+ i
,
121 (CONST
GLchan (*)[4]) span
, NULL
);
125 /* Color index mode */
126 ASSERT(ctx
->Color
.IndexMask
== ~0);
127 if (ctx
->Visual
.indexBits
== 8) {
129 GLubyte span
[MAX_WIDTH
];
131 MEMSET(span
, ctx
->Color
.ClearIndex
, width
);
132 for (i
= 0; i
< height
; i
++) {
133 (*ctx
->Driver
.WriteCI8Span
)( ctx
, width
, x
, y
+ i
, span
, NULL
);
137 /* non 8-bit clear */
138 GLuint span
[MAX_WIDTH
];
140 for (i
= 0; i
< width
; i
++) {
141 span
[i
] = ctx
->Color
.ClearIndex
;
143 for (i
= 0; i
< height
; i
++) {
144 (*ctx
->Driver
.WriteCI32Span
)( ctx
, width
, x
, y
+ i
, span
, NULL
);
153 * Clear the front/back/left/right color buffers.
154 * This function is usually only called if we need to clear the
155 * buffers with masking.
158 clear_color_buffers(GLcontext
*ctx
)
160 const GLuint colorMask
= *((GLuint
*) &ctx
->Color
.ColorMask
);
163 /* loop over four possible dest color buffers */
164 for (bufferBit
= 1; bufferBit
<= 8; bufferBit
= bufferBit
<< 1) {
165 if (bufferBit
& ctx
->Color
.DrawDestMask
) {
166 if (bufferBit
== FRONT_LEFT_BIT
) {
167 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_FRONT_LEFT
);
168 (void) (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
, GL_FRONT_LEFT
);
170 else if (bufferBit
== FRONT_RIGHT_BIT
) {
171 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_FRONT_RIGHT
);
172 (void) (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
, GL_FRONT_RIGHT
);
174 else if (bufferBit
== BACK_LEFT_BIT
) {
175 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_BACK_LEFT
);
176 (void) (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
, GL_BACK_LEFT
);
179 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_BACK_RIGHT
);
180 (void) (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->DrawBuffer
, GL_BACK_RIGHT
);
183 if (colorMask
!= 0xffffffff) {
184 clear_color_buffer_with_masking(ctx
);
187 clear_color_buffer(ctx
);
192 /* restore default read/draw buffers */
193 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
194 (void) (*ctx
->Driver
.SetReadBuffer
)( ctx
, ctx
->ReadBuffer
, ctx
->Pixel
.DriverReadBuffer
);
200 _swrast_Clear( GLcontext
*ctx
, GLbitfield mask
,
202 GLint x
, GLint y
, GLint width
, GLint height
)
207 GLbitfield legalBits
= DD_FRONT_LEFT_BIT
|
214 assert((mask
& (~legalBits
)) == 0);
220 /* do software clearing here */
222 if (mask
& ctx
->Color
.DrawDestMask
) clear_color_buffers(ctx
);
223 if (mask
& GL_DEPTH_BUFFER_BIT
) _mesa_clear_depth_buffer(ctx
);
224 if (mask
& GL_ACCUM_BUFFER_BIT
) _mesa_clear_accum_buffer(ctx
);
225 if (mask
& GL_STENCIL_BUFFER_BIT
) _mesa_clear_stencil_buffer(ctx
);
228 /* clear software-based alpha buffer(s) */
229 if ( (mask
& GL_COLOR_BUFFER_BIT
)
230 && ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
231 && ctx
->Color
.ColorMask
[ACOMP
]) {
232 _mesa_clear_alpha_buffers( ctx
);
240 _swrast_alloc_buffers( GLcontext
*ctx
)
242 /* Reallocate other buffers if needed. */
243 if (ctx
->DrawBuffer
->UseSoftwareDepthBuffer
) {
244 _mesa_alloc_depth_buffer( ctx
);
246 if (ctx
->DrawBuffer
->UseSoftwareStencilBuffer
) {
247 _mesa_alloc_stencil_buffer( ctx
);
249 if (ctx
->DrawBuffer
->UseSoftwareAccumBuffer
) {
250 _mesa_alloc_accum_buffer( ctx
);
252 if (ctx
->DrawBuffer
->UseSoftwareAlphaBuffers
) {
253 _mesa_alloc_alpha_buffers( ctx
);