fixed a bunch of g++ warnings/errors. Compiling with g++ can help find lots of poten...
[mesa.git] / src / mesa / swrast / s_buffers.c
1 /* $Id: s_buffers.c,v 1.6 2001/03/07 05:06:12 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28 #include "glheader.h"
29 #include "macros.h"
30 #include "mem.h"
31
32 #include "s_accum.h"
33 #include "s_alphabuf.h"
34 #include "s_depth.h"
35 #include "s_masking.h"
36 #include "s_stencil.h"
37
38
39
40
41 /*
42 * Clear the color buffer when glColorMask or glIndexMask is in effect.
43 */
44 static void
45 clear_color_buffer_with_masking( GLcontext *ctx )
46 {
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;
51
52 if (ctx->Visual.rgbMode) {
53 /* RGBA mode */
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];
58 GLint i;
59 for (i = 0; i < height; i++) {
60 GLchan rgba[MAX_WIDTH][4];
61 GLint j;
62 for (j = 0; j < width; j++) {
63 rgba[j][RCOMP] = r;
64 rgba[j][GCOMP] = g;
65 rgba[j][BCOMP] = b;
66 rgba[j][ACOMP] = a;
67 }
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 );
71 }
72 }
73 else {
74 /* Color index mode */
75 GLuint span[MAX_WIDTH];
76 GLubyte mask[MAX_WIDTH];
77 GLint i, j;
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;
82 }
83 _mesa_mask_index_span( ctx, width, x, y + i, span );
84 (*ctx->Driver.WriteCI32Span)( ctx, width, x, y + i, span, mask );
85 }
86 }
87 }
88
89
90
91 /*
92 * Clear a color buffer without index/channel masking.
93 */
94 static void
95 clear_color_buffer(GLcontext *ctx)
96 {
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;
101
102 if (ctx->Visual.rgbMode) {
103 /* RGBA mode */
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];
109 GLint i;
110
111 ASSERT(*((GLuint *) &ctx->Color.ColorMask) == 0xffffffff);
112
113 for (i = 0; i < width; i++) {
114 span[i][RCOMP] = r;
115 span[i][GCOMP] = g;
116 span[i][BCOMP] = b;
117 span[i][ACOMP] = a;
118 }
119 for (i = 0; i < height; i++) {
120 (*ctx->Driver.WriteRGBASpan)( ctx, width, x, y + i,
121 (CONST GLchan (*)[4]) span, NULL );
122 }
123 }
124 else {
125 /* Color index mode */
126 ASSERT((ctx->Color.IndexMask & ((1 << ctx->Visual.indexBits) - 1))
127 == (GLuint) ((1 << ctx->Visual.indexBits) - 1));
128 if (ctx->Visual.indexBits == 8) {
129 /* 8-bit clear */
130 GLubyte span[MAX_WIDTH];
131 GLint i;
132 MEMSET(span, ctx->Color.ClearIndex, width);
133 for (i = 0; i < height; i++) {
134 (*ctx->Driver.WriteCI8Span)( ctx, width, x, y + i, span, NULL );
135 }
136 }
137 else {
138 /* non 8-bit clear */
139 GLuint span[MAX_WIDTH];
140 GLint i;
141 for (i = 0; i < width; i++) {
142 span[i] = ctx->Color.ClearIndex;
143 }
144 for (i = 0; i < height; i++) {
145 (*ctx->Driver.WriteCI32Span)( ctx, width, x, y + i, span, NULL );
146 }
147 }
148 }
149 }
150
151
152
153 /*
154 * Clear the front/back/left/right color buffers.
155 * This function is usually only called if we need to clear the
156 * buffers with masking.
157 */
158 static void
159 clear_color_buffers(GLcontext *ctx)
160 {
161 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
162 GLuint bufferBit;
163
164 /* loop over four possible dest color buffers */
165 for (bufferBit = 1; bufferBit <= 8; bufferBit = bufferBit << 1) {
166 if (bufferBit & ctx->Color.DrawDestMask) {
167 if (bufferBit == FRONT_LEFT_BIT) {
168 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_LEFT);
169 (void) (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer, GL_FRONT_LEFT);
170 }
171 else if (bufferBit == FRONT_RIGHT_BIT) {
172 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_RIGHT);
173 (void) (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer, GL_FRONT_RIGHT);
174 }
175 else if (bufferBit == BACK_LEFT_BIT) {
176 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_LEFT);
177 (void) (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer, GL_BACK_LEFT);
178 }
179 else {
180 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_RIGHT);
181 (void) (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer, GL_BACK_RIGHT);
182 }
183
184 if (colorMask != 0xffffffff) {
185 clear_color_buffer_with_masking(ctx);
186 }
187 else {
188 clear_color_buffer(ctx);
189 }
190 }
191 }
192
193 /* restore default read/draw buffers */
194 (void) (*ctx->Driver.SetDrawBuffer)( ctx, ctx->Color.DriverDrawBuffer );
195 (void) (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer, ctx->Pixel.DriverReadBuffer );
196 }
197
198
199
200 void
201 _swrast_Clear( GLcontext *ctx, GLbitfield mask,
202 GLboolean all,
203 GLint x, GLint y, GLint width, GLint height )
204 {
205
206 #ifdef DEBUG
207 {
208 GLbitfield legalBits = DD_FRONT_LEFT_BIT |
209 DD_FRONT_RIGHT_BIT |
210 DD_BACK_LEFT_BIT |
211 DD_BACK_RIGHT_BIT |
212 DD_DEPTH_BIT |
213 DD_STENCIL_BIT |
214 DD_ACCUM_BIT;
215 assert((mask & (~legalBits)) == 0);
216 }
217 #endif
218
219 RENDER_START(ctx);
220
221 /* do software clearing here */
222 if (mask) {
223 if (mask & ctx->Color.DrawDestMask) clear_color_buffers(ctx);
224 if (mask & GL_DEPTH_BUFFER_BIT) _mesa_clear_depth_buffer(ctx);
225 if (mask & GL_ACCUM_BUFFER_BIT) _mesa_clear_accum_buffer(ctx);
226 if (mask & GL_STENCIL_BUFFER_BIT) _mesa_clear_stencil_buffer(ctx);
227 }
228
229 /* clear software-based alpha buffer(s) */
230 if ( (mask & GL_COLOR_BUFFER_BIT)
231 && ctx->DrawBuffer->UseSoftwareAlphaBuffers
232 && ctx->Color.ColorMask[ACOMP]) {
233 _mesa_clear_alpha_buffers( ctx );
234 }
235
236 RENDER_FINISH(ctx);
237 }
238
239
240 void
241 _swrast_alloc_buffers( GLcontext *ctx )
242 {
243 /* Reallocate other buffers if needed. */
244 if (ctx->DrawBuffer->UseSoftwareDepthBuffer) {
245 _mesa_alloc_depth_buffer( ctx );
246 }
247 if (ctx->DrawBuffer->UseSoftwareStencilBuffer) {
248 _mesa_alloc_stencil_buffer( ctx );
249 }
250 if (ctx->DrawBuffer->UseSoftwareAccumBuffer) {
251 _mesa_alloc_accum_buffer( ctx );
252 }
253 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers) {
254 _mesa_alloc_alpha_buffers( ctx );
255 }
256 }
257
258