Subversion Repositories shark

Rev

Rev 57 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
57 pj 1
/* $Id: s_buffers.c,v 1.1 2003-02-28 11:49:41 pj Exp $ */
2
 
3
/*
4
 * Mesa 3-D graphics library
5
 * Version:  4.1
6
 *
7
 * Copyright (C) 1999-2002  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 "colormac.h"
30
#include "macros.h"
31
#include "imports.h"
32
 
33
#include "s_accum.h"
34
#include "s_alphabuf.h"
35
#include "s_context.h"
36
#include "s_depth.h"
37
#include "s_masking.h"
38
#include "s_stencil.h"
39
 
40
 
41
 
42
 
43
/*
44
 * Clear the color buffer when glColorMask or glIndexMask is in effect.
45
 */
46
static void
47
clear_color_buffer_with_masking( GLcontext *ctx )
48
{
49
   SWcontext *swrast = SWRAST_CONTEXT(ctx);
50
   const GLint x = ctx->DrawBuffer->_Xmin;
51
   const GLint y = ctx->DrawBuffer->_Ymin;
52
   const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
53
   const GLint width  = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
54
 
55
   if (ctx->Visual.rgbMode) {
56
      /* RGBA mode */
57
      GLchan clearColor[4];
58
      GLint i;
59
      CLAMPED_FLOAT_TO_CHAN(clearColor[RCOMP], ctx->Color.ClearColor[0]);
60
      CLAMPED_FLOAT_TO_CHAN(clearColor[GCOMP], ctx->Color.ClearColor[1]);
61
      CLAMPED_FLOAT_TO_CHAN(clearColor[BCOMP], ctx->Color.ClearColor[2]);
62
      CLAMPED_FLOAT_TO_CHAN(clearColor[ACOMP], ctx->Color.ClearColor[3]);
63
      for (i = 0; i < height; i++) {
64
         GLchan rgba[MAX_WIDTH][4];
65
         GLint j;
66
         for (j = 0; j < width; j++) {
67
            COPY_CHAN4(rgba[j], clearColor);
68
         }
69
         _mesa_mask_rgba_array( ctx, width, x, y + i, rgba );
70
         (*swrast->Driver.WriteRGBASpan)( ctx, width, x, y + i,
71
                                       (CONST GLchan (*)[4]) rgba, NULL );
72
      }
73
   }
74
   else {
75
      /* Color index mode */
76
      GLuint span[MAX_WIDTH];
77
      GLubyte mask[MAX_WIDTH];
78
      GLint i, j;
79
      MEMSET( mask, 1, width );
80
      for (i=0;i<height;i++) {
81
         for (j=0;j<width;j++) {
82
            span[j] = ctx->Color.ClearIndex;
83
         }
84
         _mesa_mask_index_array( ctx, width, x, y + i, span );
85
         (*swrast->Driver.WriteCI32Span)( ctx, width, x, y + i, span, mask );
86
      }
87
   }
88
}
89
 
90
 
91
 
92
/*
93
 * Clear a color buffer without index/channel masking.
94
 */
95
static void
96
clear_color_buffer(GLcontext *ctx)
97
{
98
   SWcontext *swrast = SWRAST_CONTEXT(ctx);
99
   const GLint x = ctx->DrawBuffer->_Xmin;
100
   const GLint y = ctx->DrawBuffer->_Ymin;
101
   const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
102
   const GLint width  = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
103
 
104
   if (ctx->Visual.rgbMode) {
105
      /* RGBA mode */
106
      GLchan clearColor[4];
107
      GLchan span[MAX_WIDTH][4];
108
      GLint i;
109
 
110
      CLAMPED_FLOAT_TO_CHAN(clearColor[RCOMP], ctx->Color.ClearColor[0]);
111
      CLAMPED_FLOAT_TO_CHAN(clearColor[GCOMP], ctx->Color.ClearColor[1]);
112
      CLAMPED_FLOAT_TO_CHAN(clearColor[BCOMP], ctx->Color.ClearColor[2]);
113
      CLAMPED_FLOAT_TO_CHAN(clearColor[ACOMP], ctx->Color.ClearColor[3]);
114
 
115
      ASSERT(*((GLuint *) &ctx->Color.ColorMask) == 0xffffffff);
116
 
117
      for (i = 0; i < width; i++) {
118
         COPY_CHAN4(span[i], clearColor);
119
      }
120
      for (i = 0; i < height; i++) {
121
         (*swrast->Driver.WriteRGBASpan)( ctx, width, x, y + i,
122
                                       (CONST GLchan (*)[4]) span, NULL );
123
      }
124
   }
125
   else {
126
      /* Color index mode */
127
      ASSERT((ctx->Color.IndexMask & ((1 << ctx->Visual.indexBits) - 1))
128
             == (GLuint) ((1 << ctx->Visual.indexBits) - 1));
129
      if (ctx->Visual.indexBits == 8) {
130
         /* 8-bit clear */
131
         GLubyte span[MAX_WIDTH];
132
         GLint i;
133
         MEMSET(span, ctx->Color.ClearIndex, width);
134
         for (i = 0; i < height; i++) {
135
            (*swrast->Driver.WriteCI8Span)( ctx, width, x, y + i, span, NULL );
136
         }
137
      }
138
      else {
139
         /* non 8-bit clear */
140
         GLuint span[MAX_WIDTH];
141
         GLint i;
142
         for (i = 0; i < width; i++) {
143
            span[i] = ctx->Color.ClearIndex;
144
         }
145
         for (i = 0; i < height; i++) {
146
            (*swrast->Driver.WriteCI32Span)( ctx, width, x, y + i, span, NULL );
147
         }
148
      }
149
   }
150
}
151
 
152
 
153
 
154
/*
155
 * Clear the front/back/left/right color buffers.
156
 * This function is usually only called if we need to clear the
157
 * buffers with masking.
158
 */
159
static void
160
clear_color_buffers(GLcontext *ctx)
161
{
162
   SWcontext *swrast = SWRAST_CONTEXT(ctx);
163
   const GLuint colorMask = *((GLuint *) &ctx->Color.ColorMask);
164
   GLuint bufferBit;
165
 
166
   /* loop over four possible dest color buffers */
167
   for (bufferBit = 1; bufferBit <= 8; bufferBit = bufferBit << 1) {
168
      if (bufferBit & ctx->Color._DrawDestMask) {
169
         (*swrast->Driver.SetBuffer)(ctx, ctx->DrawBuffer, bufferBit);
170
 
171
         if (colorMask != 0xffffffff) {
172
            clear_color_buffer_with_masking(ctx);
173
         }
174
         else {
175
            clear_color_buffer(ctx);
176
         }
177
      }
178
   }
179
 
180
   /* restore default read/draw buffer */
181
   _swrast_use_draw_buffer(ctx);
182
}
183
 
184
 
185
 
186
void
187
_swrast_Clear( GLcontext *ctx, GLbitfield mask,
188
               GLboolean all,
189
               GLint x, GLint y, GLint width, GLint height )
190
{
191
   SWcontext *swrast = SWRAST_CONTEXT(ctx);
192
#ifdef DEBUG
193
   {
194
      GLbitfield legalBits = DD_FRONT_LEFT_BIT |
195
         DD_FRONT_RIGHT_BIT |
196
         DD_BACK_LEFT_BIT |
197
         DD_BACK_RIGHT_BIT |
198
         DD_DEPTH_BIT |
199
         DD_STENCIL_BIT |
200
         DD_ACCUM_BIT;
201
      assert((mask & (~legalBits)) == 0);
202
   }
203
#endif
204
 
205
   RENDER_START(swrast,ctx);
206
 
207
   /* do software clearing here */
208
   if (mask) {
209
      if (mask & ctx->Color._DrawDestMask)   clear_color_buffers(ctx);
210
      if (mask & GL_DEPTH_BUFFER_BIT)    _mesa_clear_depth_buffer(ctx);
211
      if (mask & GL_ACCUM_BUFFER_BIT)    _mesa_clear_accum_buffer(ctx);
212
      if (mask & GL_STENCIL_BUFFER_BIT)  _mesa_clear_stencil_buffer(ctx);
213
   }
214
 
215
   /* clear software-based alpha buffer(s) */
216
   if ( (mask & GL_COLOR_BUFFER_BIT)
217
        && ctx->DrawBuffer->UseSoftwareAlphaBuffers
218
        && ctx->Color.ColorMask[ACOMP]) {
219
      _mesa_clear_alpha_buffers( ctx );
220
   }
221
 
222
   RENDER_FINISH(swrast,ctx);
223
}
224
 
225
 
226
void
227
_swrast_alloc_buffers( GLframebuffer *buffer )
228
{
229
   /* Reallocate other buffers if needed. */
230
   if (buffer->UseSoftwareDepthBuffer) {
231
      _mesa_alloc_depth_buffer( buffer );
232
   }
233
   if (buffer->UseSoftwareStencilBuffer) {
234
      _mesa_alloc_stencil_buffer( buffer );
235
   }
236
   if (buffer->UseSoftwareAccumBuffer) {
237
      _mesa_alloc_accum_buffer( buffer );
238
   }
239
   if (buffer->UseSoftwareAlphaBuffers) {
240
      _mesa_alloc_alpha_buffers( buffer );
241
   }
242
}
243
 
244
 
245
/*
246
 * Fallback for ctx->Driver.DrawBuffer()
247
 */
248
void
249
_swrast_DrawBuffer( GLcontext *ctx, GLenum mode )
250
{
251
   _swrast_use_draw_buffer(ctx);
252
}
253
 
254
 
255
/*
256
 * Setup things so that we read/write spans from the user-designated
257
 * read buffer (set via glReadPixels).  We usually just have to call
258
 * this for glReadPixels, glCopyPixels, etc.
259
 */
260
void
261
_swrast_use_read_buffer( GLcontext *ctx )
262
{
263
   SWcontext *swrast = SWRAST_CONTEXT(ctx);
264
 
265
   /* Do this so the software-emulated alpha plane span functions work! */
266
   swrast->CurrentBuffer = ctx->Pixel._ReadSrcMask;
267
   /* Tell the device driver where to read/write spans */
268
   (*swrast->Driver.SetBuffer)( ctx, ctx->ReadBuffer, swrast->CurrentBuffer );
269
}
270
 
271
 
272
/*
273
 * Setup things so that we read/write spans from the default draw buffer.
274
 * This is the usual mode that Mesa's software rasterizer operates in.
275
 */
276
void
277
_swrast_use_draw_buffer( GLcontext *ctx )
278
{
279
   SWcontext *swrast = SWRAST_CONTEXT(ctx);
280
 
281
   /* The user can specify rendering to zero, one, two, or four color
282
    * buffers simultaneously with glDrawBuffer()!
283
    * We don't expect the span/point/line/triangle functions to deal with
284
    * that mess so we'll iterate over the multiple buffers as needed.
285
    * But usually we only render to one color buffer at a time.
286
    * We set ctx->Color._DriverDrawBuffer to that buffer and tell the
287
    * device driver to use that buffer.
288
    * Look in s_span.c's multi_write_rgba_span() function to see how
289
    * we loop over multiple color buffers when needed.
290
    */
291
 
292
   if (ctx->Color._DrawDestMask & FRONT_LEFT_BIT)
293
      swrast->CurrentBuffer = FRONT_LEFT_BIT;
294
   else if (ctx->Color._DrawDestMask & BACK_LEFT_BIT)
295
      swrast->CurrentBuffer = BACK_LEFT_BIT;
296
   else if (ctx->Color._DrawDestMask & FRONT_RIGHT_BIT)
297
      swrast->CurrentBuffer = FRONT_RIGHT_BIT;
298
   else if (ctx->Color._DrawDestMask & BACK_RIGHT_BIT)
299
      swrast->CurrentBuffer = BACK_RIGHT_BIT;
300
   else
301
      /* glDrawBuffer(GL_NONE) */
302
      swrast->CurrentBuffer = FRONT_LEFT_BIT; /* we always have this buffer */
303
 
304
   (*swrast->Driver.SetBuffer)( ctx, ctx->DrawBuffer, swrast->CurrentBuffer );
305
}