s_clear.c revision 810e0549afad45469bef532c1f2d355f3fde0bbb
1e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
2e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Mesa 3-D graphics library
3810e0549afad45469bef532c1f2d355f3fde0bbbBrian * Version:  7.1
422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
5810e0549afad45469bef532c1f2d355f3fde0bbbBrian * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
7e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
8e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * copy of this software and associated documentation files (the "Software"),
9e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * to deal in the Software without restriction, including without limitation
10e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * and/or sell copies of the Software, and to permit persons to whom the
12e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Software is furnished to do so, subject to the following conditions:
1322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
14e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * The above copyright notice and this permission notice shall be included
15e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * in all copies or substantial portions of the Software.
1622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
17e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
24e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
25cfca72cfb19367d824a3254b40566b3fc01723eaBrian Paul/** XXX This file should be named s_clear.c */
26e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
27e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "glheader.h"
28fc80ad6e62fb2b53d53756593099330477a44c52Brian Paul#include "colormac.h"
29e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "macros.h"
303c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul#include "imports.h"
31e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul#include "mtypes.h"
32e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
33e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_accum.h"
34709892459922a32096fe9dd8261d0d92337bb02fKeith Whitwell#include "s_context.h"
35e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_depth.h"
36e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_masking.h"
37e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_stencil.h"
38e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
39e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
40894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul/**
41e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul * Clear the color buffer when glColorMask is in effect.
42e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
43e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwellstatic void
44e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paulclear_rgba_buffer_with_masking(GLcontext *ctx, struct gl_renderbuffer *rb)
45e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
461e1aac034c986a08248861363c0baa27dc2ae2d5Keith Whitwell   const GLint x = ctx->DrawBuffer->_Xmin;
471e1aac034c986a08248861363c0baa27dc2ae2d5Keith Whitwell   const GLint y = ctx->DrawBuffer->_Ymin;
481e1aac034c986a08248861363c0baa27dc2ae2d5Keith Whitwell   const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
491e1aac034c986a08248861363c0baa27dc2ae2d5Keith Whitwell   const GLint width  = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
50cdb27e8242215271364602995d85607cfc06d441Brian Paul   SWspan span;
51e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   GLint i;
52e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
53e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   ASSERT(ctx->Visual.rgbMode);
54863bd565204c7fec515457dfd6c59066309cb136Brian Paul   ASSERT(rb->PutRow);
55e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
56f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul   /* Initialize color span with clear color */
57524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul   /* XXX optimize for clearcolor == black/zero (bzero) */
58f4b103dc993491355ec3e3640d9cb060138175c2Brian   INIT_SPAN(span, GL_BITMAP);
59f4b103dc993491355ec3e3640d9cb060138175c2Brian   span.end = width;
60f4b103dc993491355ec3e3640d9cb060138175c2Brian   span.arrayMask = SPAN_RGBA;
61524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul   span.array->ChanType = rb->DataType;
62524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul   if (span.array->ChanType == GL_UNSIGNED_BYTE) {
63524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      GLubyte clearColor[4];
64524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      UNCLAMPED_FLOAT_TO_UBYTE(clearColor[RCOMP], ctx->Color.ClearColor[0]);
65524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      UNCLAMPED_FLOAT_TO_UBYTE(clearColor[GCOMP], ctx->Color.ClearColor[1]);
66524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      UNCLAMPED_FLOAT_TO_UBYTE(clearColor[BCOMP], ctx->Color.ClearColor[2]);
67524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      UNCLAMPED_FLOAT_TO_UBYTE(clearColor[ACOMP], ctx->Color.ClearColor[3]);
68524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      for (i = 0; i < width; i++) {
69524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul         COPY_4UBV(span.array->rgba[i], clearColor);
70524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      }
71524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul   }
72524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul   else if (span.array->ChanType == GL_UNSIGNED_SHORT) {
73524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      GLushort clearColor[4];
74524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      UNCLAMPED_FLOAT_TO_USHORT(clearColor[RCOMP], ctx->Color.ClearColor[0]);
75524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      UNCLAMPED_FLOAT_TO_USHORT(clearColor[GCOMP], ctx->Color.ClearColor[1]);
76524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      UNCLAMPED_FLOAT_TO_USHORT(clearColor[BCOMP], ctx->Color.ClearColor[2]);
77524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      UNCLAMPED_FLOAT_TO_USHORT(clearColor[ACOMP], ctx->Color.ClearColor[3]);
78524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      for (i = 0; i < width; i++) {
79524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul         COPY_4V(span.array->rgba[i], clearColor);
80524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      }
81524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul   }
82524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul   else {
83524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      ASSERT(span.array->ChanType == GL_FLOAT);
84524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      for (i = 0; i < width; i++) {
857e85b0a025a82c3ffed060a757a3b4adae03d269Brian         CLAMPED_FLOAT_TO_CHAN(span.array->rgba[i][0], ctx->Color.ClearColor[0]);
867e85b0a025a82c3ffed060a757a3b4adae03d269Brian         CLAMPED_FLOAT_TO_CHAN(span.array->rgba[i][1], ctx->Color.ClearColor[1]);
877e85b0a025a82c3ffed060a757a3b4adae03d269Brian         CLAMPED_FLOAT_TO_CHAN(span.array->rgba[i][2], ctx->Color.ClearColor[2]);
887e85b0a025a82c3ffed060a757a3b4adae03d269Brian         CLAMPED_FLOAT_TO_CHAN(span.array->rgba[i][3], ctx->Color.ClearColor[3]);
89524bf7bbcf75bc4887dbc0f2f87ed79a8c44ab20Brian Paul      }
90f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul   }
91f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul
92f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul   /* Note that masking will change the color values, but only the
93f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul    * channels for which the write mask is GL_FALSE.  The channels
94f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul    * which which are write-enabled won't get modified.
95f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul    */
96e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   for (i = 0; i < height; i++) {
9743715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul      span.x = x;
9843715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul      span.y = y + i;
99f515c78a2320369ead087c4e0efd583f17b4b78bBrian Paul      _swrast_mask_rgba_span(ctx, rb, &span);
10043715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul      /* write masked row */
101f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul      rb->PutRow(ctx, rb, width, x, y + i, span.array->rgba, NULL);
102e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
103e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul}
104e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
105e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
106e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul/**
107e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul * Clear color index buffer with masking.
108e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul */
109e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paulstatic void
110e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paulclear_ci_buffer_with_masking(GLcontext *ctx, struct gl_renderbuffer *rb)
111e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul{
112e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   const GLint x = ctx->DrawBuffer->_Xmin;
113e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   const GLint y = ctx->DrawBuffer->_Ymin;
114e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
115e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   const GLint width  = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
116cdb27e8242215271364602995d85607cfc06d441Brian Paul   SWspan span;
11762a2f8cdd60df797daad106d51c3bcc56943c236Brian Paul   GLint i;
118e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
119e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   ASSERT(!ctx->Visual.rgbMode);
12062a2f8cdd60df797daad106d51c3bcc56943c236Brian Paul   ASSERT(rb->PutRow);
12162a2f8cdd60df797daad106d51c3bcc56943c236Brian Paul   ASSERT(rb->DataType == GL_UNSIGNED_INT);
122e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
123f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul   /* Initialize index span with clear index */
124f4b103dc993491355ec3e3640d9cb060138175c2Brian   INIT_SPAN(span, GL_BITMAP);
125f4b103dc993491355ec3e3640d9cb060138175c2Brian   span.end = width;
126f4b103dc993491355ec3e3640d9cb060138175c2Brian   span.arrayMask = SPAN_INDEX;
127f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul   for (i = 0; i < width;i++) {
128f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul      span.array->index[i] = ctx->Color.ClearIndex;
129f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul   }
130f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul
131f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul   /* Note that masking will change the color indexes, but only the
132f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul    * bits for which the write mask is GL_FALSE.  The bits
133f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul    * which are write-enabled won't get modified.
134f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul    */
135e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   for (i = 0; i < height;i++) {
13643715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul      span.x = x;
13743715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul      span.y = y + i;
138f515c78a2320369ead087c4e0efd583f17b4b78bBrian Paul      _swrast_mask_ci_span(ctx, rb, &span);
13943715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul      /* write masked row */
140f11508c29f4e2cbe50c5580e054342a4c3ddb4c0Brian Paul      rb->PutRow(ctx, rb, width, x, y + i, span.array->index, NULL);
141e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
142e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
143e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
144e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
145894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul/**
146e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul * Clear an rgba color buffer without channel masking.
147e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
148e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwellstatic void
149e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paulclear_rgba_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
150e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
1511e1aac034c986a08248861363c0baa27dc2ae2d5Keith Whitwell   const GLint x = ctx->DrawBuffer->_Xmin;
1521e1aac034c986a08248861363c0baa27dc2ae2d5Keith Whitwell   const GLint y = ctx->DrawBuffer->_Ymin;
1531e1aac034c986a08248861363c0baa27dc2ae2d5Keith Whitwell   const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
1541e1aac034c986a08248861363c0baa27dc2ae2d5Keith Whitwell   const GLint width  = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
155863bd565204c7fec515457dfd6c59066309cb136Brian Paul   GLubyte clear8[4];
156863bd565204c7fec515457dfd6c59066309cb136Brian Paul   GLushort clear16[4];
157863bd565204c7fec515457dfd6c59066309cb136Brian Paul   GLvoid *clearVal;
158e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   GLint i;
159e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
160e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   ASSERT(ctx->Visual.rgbMode);
161e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
162e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   ASSERT(ctx->Color.ColorMask[0] &&
163e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul          ctx->Color.ColorMask[1] &&
164e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul          ctx->Color.ColorMask[2] &&
165e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul          ctx->Color.ColorMask[3]);
166e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
167863bd565204c7fec515457dfd6c59066309cb136Brian Paul   ASSERT(rb->PutMonoRow);
168863bd565204c7fec515457dfd6c59066309cb136Brian Paul
169863bd565204c7fec515457dfd6c59066309cb136Brian Paul   switch (rb->DataType) {
170e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      case GL_UNSIGNED_BYTE:
171cfca72cfb19367d824a3254b40566b3fc01723eaBrian Paul         UNCLAMPED_FLOAT_TO_UBYTE(clear8[0], ctx->Color.ClearColor[0]);
172cfca72cfb19367d824a3254b40566b3fc01723eaBrian Paul         UNCLAMPED_FLOAT_TO_UBYTE(clear8[1], ctx->Color.ClearColor[1]);
173cfca72cfb19367d824a3254b40566b3fc01723eaBrian Paul         UNCLAMPED_FLOAT_TO_UBYTE(clear8[2], ctx->Color.ClearColor[2]);
174cfca72cfb19367d824a3254b40566b3fc01723eaBrian Paul         UNCLAMPED_FLOAT_TO_UBYTE(clear8[3], ctx->Color.ClearColor[3]);
175e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         clearVal = clear8;
176e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         break;
177e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      case GL_UNSIGNED_SHORT:
178cfca72cfb19367d824a3254b40566b3fc01723eaBrian Paul         UNCLAMPED_FLOAT_TO_USHORT(clear16[0], ctx->Color.ClearColor[0]);
179cfca72cfb19367d824a3254b40566b3fc01723eaBrian Paul         UNCLAMPED_FLOAT_TO_USHORT(clear16[1], ctx->Color.ClearColor[1]);
180cfca72cfb19367d824a3254b40566b3fc01723eaBrian Paul         UNCLAMPED_FLOAT_TO_USHORT(clear16[2], ctx->Color.ClearColor[2]);
181cfca72cfb19367d824a3254b40566b3fc01723eaBrian Paul         UNCLAMPED_FLOAT_TO_USHORT(clear16[3], ctx->Color.ClearColor[3]);
182e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         clearVal = clear16;
183e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         break;
184e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      case GL_FLOAT:
185e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         clearVal = ctx->Color.ClearColor;
186e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         break;
187e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      default:
188e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         _mesa_problem(ctx, "Bad rb DataType in clear_color_buffer");
189e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         return;
190e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   }
191863bd565204c7fec515457dfd6c59066309cb136Brian Paul
192863bd565204c7fec515457dfd6c59066309cb136Brian Paul   for (i = 0; i < height; i++) {
193863bd565204c7fec515457dfd6c59066309cb136Brian Paul      rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
194e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
195e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul}
196e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
197e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
198e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul/**
199e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul * Clear color index buffer without masking.
200e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul */
201e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paulstatic void
202e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paulclear_ci_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
203e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul{
204e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   const GLint x = ctx->DrawBuffer->_Xmin;
205e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   const GLint y = ctx->DrawBuffer->_Ymin;
206e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   const GLint height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
207e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   const GLint width  = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
208863bd565204c7fec515457dfd6c59066309cb136Brian Paul   GLubyte clear8;
209863bd565204c7fec515457dfd6c59066309cb136Brian Paul   GLushort clear16;
210863bd565204c7fec515457dfd6c59066309cb136Brian Paul   GLuint clear32;
211863bd565204c7fec515457dfd6c59066309cb136Brian Paul   GLvoid *clearVal;
212863bd565204c7fec515457dfd6c59066309cb136Brian Paul   GLint i;
213e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
214e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   ASSERT(!ctx->Visual.rgbMode);
215e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
2169c4fd26bcab7e4a5bd97a56deff89b8ea0ae5f1dBrian Paul   ASSERT((ctx->Color.IndexMask & ((1 << rb->IndexBits) - 1))
2179c4fd26bcab7e4a5bd97a56deff89b8ea0ae5f1dBrian Paul          == (GLuint) ((1 << rb->IndexBits) - 1));
218e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
219863bd565204c7fec515457dfd6c59066309cb136Brian Paul   ASSERT(rb->PutMonoRow);
220863bd565204c7fec515457dfd6c59066309cb136Brian Paul
221863bd565204c7fec515457dfd6c59066309cb136Brian Paul   /* setup clear value */
222863bd565204c7fec515457dfd6c59066309cb136Brian Paul   switch (rb->DataType) {
223e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      case GL_UNSIGNED_BYTE:
224e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         clear8 = (GLubyte) ctx->Color.ClearIndex;
225e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         clearVal = &clear8;
226e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         break;
227e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      case GL_UNSIGNED_SHORT:
228e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         clear16 = (GLushort) ctx->Color.ClearIndex;
229e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         clearVal = &clear16;
230e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         break;
231e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      case GL_UNSIGNED_INT:
232e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         clear32 = ctx->Color.ClearIndex;
233e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         clearVal = &clear32;
234e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         break;
235e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      default:
236e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         _mesa_problem(ctx, "Bad rb DataType in clear_color_buffer");
237e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         return;
238e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
239863bd565204c7fec515457dfd6c59066309cb136Brian Paul
240863bd565204c7fec515457dfd6c59066309cb136Brian Paul   for (i = 0; i < height; i++)
241863bd565204c7fec515457dfd6c59066309cb136Brian Paul      rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL);
242e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
243e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
244e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
245894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul/**
246894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul * Clear the front/back/left/right/aux color buffers.
247894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul * This function is usually only called if the device driver can't
248894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul * clear its own color buffers for some reason (such as with masking).
249e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
250e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwellstatic void
251e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwellclear_color_buffers(GLcontext *ctx)
252e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
253e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   GLboolean masking;
254ff73c783cc47361ff0dd819c82d067b4b85870ddBrian   GLuint buf;
255e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
256e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   if (ctx->Visual.rgbMode) {
257e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      if (ctx->Color.ColorMask[0] &&
258e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul          ctx->Color.ColorMask[1] &&
259e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul          ctx->Color.ColorMask[2] &&
260e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul          ctx->Color.ColorMask[3]) {
261e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         masking = GL_FALSE;
262e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      }
263e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      else {
264e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         masking = GL_TRUE;
265e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      }
266e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   }
267e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   else {
268ff73c783cc47361ff0dd819c82d067b4b85870ddBrian      struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
2699c4fd26bcab7e4a5bd97a56deff89b8ea0ae5f1dBrian Paul      const GLuint indexBits = (1 << rb->IndexBits) - 1;
270e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      if ((ctx->Color.IndexMask & indexBits) == indexBits) {
271e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         masking = GL_FALSE;
272e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      }
273e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      else {
274e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         masking = GL_TRUE;
275e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      }
276e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   }
27722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes
278ff73c783cc47361ff0dd819c82d067b4b85870ddBrian   for (buf = 0; buf < ctx->DrawBuffer->_NumColorDrawBuffers; buf++) {
279ff73c783cc47361ff0dd819c82d067b4b85870ddBrian      struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[buf];
280e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      if (ctx->Visual.rgbMode) {
281e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         if (masking) {
282e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul            clear_rgba_buffer_with_masking(ctx, rb);
283e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         }
284e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         else {
285e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul            clear_rgba_buffer(ctx, rb);
286e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         }
287e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      }
288e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul      else {
289e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         if (masking) {
290e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul            clear_ci_buffer_with_masking(ctx, rb);
291e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         }
292e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         else {
293e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul            clear_ci_buffer(ctx, rb);
294e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         }
295e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      }
296e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
297e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
298e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
299e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
300894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul/**
301894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul * Called via the device driver's ctx->Driver.Clear() function if the
302894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul * device driver can't clear one or more of the buffers itself.
303a5676795cfe2e24979b5da65c2f499049ab009d9Brian Paul * \param buffers  bitfield of BUFFER_BIT_* values indicating which
304a5676795cfe2e24979b5da65c2f499049ab009d9Brian Paul *                 renderbuffers are to be cleared.
305894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul * \param all  if GL_TRUE, clear whole buffer, else clear specified region.
306894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul */
30722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid
308a5676795cfe2e24979b5da65c2f499049ab009d9Brian Paul_swrast_Clear(GLcontext *ctx, GLbitfield buffers)
309e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
310709892459922a32096fe9dd8261d0d92337bb02fKeith Whitwell   SWcontext *swrast = SWRAST_CONTEXT(ctx);
311e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul
312e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul#ifdef DEBUG_FOO
313e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   {
314894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul      const GLbitfield legalBits =
315e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         BUFFER_BIT_FRONT_LEFT |
316e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul	 BUFFER_BIT_FRONT_RIGHT |
317e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul	 BUFFER_BIT_BACK_LEFT |
318e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul	 BUFFER_BIT_BACK_RIGHT |
319e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul	 BUFFER_BIT_DEPTH |
320e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul	 BUFFER_BIT_STENCIL |
321e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul	 BUFFER_BIT_ACCUM |
322e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         BUFFER_BIT_AUX0 |
323e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         BUFFER_BIT_AUX1 |
324e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         BUFFER_BIT_AUX2 |
325e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul         BUFFER_BIT_AUX3;
326a5676795cfe2e24979b5da65c2f499049ab009d9Brian Paul      assert((buffers & (~legalBits)) == 0);
327e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
328e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#endif
329e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
330709892459922a32096fe9dd8261d0d92337bb02fKeith Whitwell   RENDER_START(swrast,ctx);
331e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
332e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   /* do software clearing here */
333a5676795cfe2e24979b5da65c2f499049ab009d9Brian Paul   if (buffers) {
334810e0549afad45469bef532c1f2d355f3fde0bbbBrian      if ((buffers & BUFFER_BITS_COLOR)
335810e0549afad45469bef532c1f2d355f3fde0bbbBrian          && (ctx->DrawBuffer->_NumColorDrawBuffers > 0)) {
336894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul         clear_color_buffers(ctx);
337894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul      }
338a5676795cfe2e24979b5da65c2f499049ab009d9Brian Paul      if (buffers & BUFFER_BIT_DEPTH) {
3391a5f1bdb428ea3ded1e77796590014f89fe78f77Brian Paul         _swrast_clear_depth_buffer(ctx, ctx->DrawBuffer->_DepthBuffer);
340894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul      }
341a5676795cfe2e24979b5da65c2f499049ab009d9Brian Paul      if (buffers & BUFFER_BIT_ACCUM) {
3421a5f1bdb428ea3ded1e77796590014f89fe78f77Brian Paul         _swrast_clear_accum_buffer(ctx,
3431a5f1bdb428ea3ded1e77796590014f89fe78f77Brian Paul                       ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
344894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul      }
345a5676795cfe2e24979b5da65c2f499049ab009d9Brian Paul      if (buffers & BUFFER_BIT_STENCIL) {
3461a5f1bdb428ea3ded1e77796590014f89fe78f77Brian Paul         _swrast_clear_stencil_buffer(ctx, ctx->DrawBuffer->_StencilBuffer);
347894844a8d956a0ee5f95836331dc318f49fdb845Brian Paul      }
348e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
349e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
350709892459922a32096fe9dd8261d0d92337bb02fKeith Whitwell   RENDER_FINISH(swrast,ctx);
351e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
352