Cleanup of derived state calculation prior to seperating software T&L
[mesa.git] / src / mesa / swrast / s_buffers.c
1 /* $Id: s_buffers.c,v 1.2 2000/11/13 20:02:57 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 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.RGBAflag) {
53 /* RGBA mode */
54 const GLchan r = (GLint) (ctx->Color.ClearColor[0] * CHAN_MAXF);
55 const GLchan g = (GLint) (ctx->Color.ClearColor[1] * CHAN_MAXF);
56 const GLchan b = (GLint) (ctx->Color.ClearColor[2] * CHAN_MAXF);
57 const GLchan a = (GLint) (ctx->Color.ClearColor[3] * CHAN_MAXF);
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.RGBAflag) {
103 /* RGBA mode */
104 const GLchan r = (GLint) (ctx->Color.ClearColor[0] * CHAN_MAXF);
105 const GLchan g = (GLint) (ctx->Color.ClearColor[1] * CHAN_MAXF);
106 const GLchan b = (GLint) (ctx->Color.ClearColor[2] * CHAN_MAXF);
107 const GLchan a = (GLint) (ctx->Color.ClearColor[3] * CHAN_MAXF);
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 == ~0);
127 if (ctx->Visual.IndexBits == 8) {
128 /* 8-bit clear */
129 GLubyte span[MAX_WIDTH];
130 GLint i;
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 );
134 }
135 }
136 else {
137 /* non 8-bit clear */
138 GLuint span[MAX_WIDTH];
139 GLint i;
140 for (i = 0; i < width; i++) {
141 span[i] = ctx->Color.ClearIndex;
142 }
143 for (i = 0; i < height; i++) {
144 (*ctx->Driver.WriteCI32Span)( ctx, width, x, y + i, span, NULL );
145 }
146 }
147 }
148 }
149
150
151
152 /*
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.
156 */
157 static void
158 clear_color_buffers(GLcontext *ctx)
159 {
160 const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
161 GLuint bufferBit;
162
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);
169 }
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);
173 }
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);
177 }
178 else {
179 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_RIGHT);
180 (void) (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer, GL_BACK_RIGHT);
181 }
182
183 if (colorMask != 0xffffffff) {
184 clear_color_buffer_with_masking(ctx);
185 }
186 else {
187 clear_color_buffer(ctx);
188 }
189 }
190 }
191
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 );
195 }
196
197
198
199 void
200 _swrast_Clear( GLcontext *ctx, GLbitfield mask,
201 GLboolean all,
202 GLint x, GLint y, GLint width, GLint height )
203 {
204
205 #ifdef DEBUG
206 {
207 GLbitfield legalBits = DD_FRONT_LEFT_BIT |
208 DD_FRONT_RIGHT_BIT |
209 DD_BACK_LEFT_BIT |
210 DD_BACK_RIGHT_BIT |
211 DD_DEPTH_BIT |
212 DD_STENCIL_BIT |
213 DD_ACCUM_BIT;
214 assert((mask & (~legalBits)) == 0);
215 }
216 #endif
217
218 RENDER_START(ctx);
219
220 /* do software clearing here */
221 if (mask) {
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);
226 }
227
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 );
233 }
234
235 RENDER_FINISH(ctx);
236 }
237
238
239 void
240 _swrast_alloc_buffers( GLcontext *ctx )
241 {
242 /* Reallocate other buffers if needed. */
243 if (ctx->DrawBuffer->UseSoftwareDepthBuffer) {
244 _mesa_alloc_depth_buffer( ctx );
245 }
246 if (ctx->DrawBuffer->UseSoftwareStencilBuffer) {
247 _mesa_alloc_stencil_buffer( ctx );
248 }
249 if (ctx->DrawBuffer->UseSoftwareAccumBuffer) {
250 _mesa_alloc_accum_buffer( ctx );
251 }
252 if (ctx->DrawBuffer->UseSoftwareAlphaBuffers) {
253 _mesa_alloc_alpha_buffers( ctx );
254 }
255 }
256
257