2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include "s_context.h"
36 #include "s_masking.h"
37 #include "s_stencil.h"
41 * Clear the color buffer when glColorMask is in effect.
44 clear_rgba_buffer_with_masking(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
46 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
47 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
48 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
49 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
53 ASSERT(ctx
->Visual
.rgbMode
);
56 CLAMPED_FLOAT_TO_CHAN(clearColor
[RCOMP
], ctx
->Color
.ClearColor
[0]);
57 CLAMPED_FLOAT_TO_CHAN(clearColor
[GCOMP
], ctx
->Color
.ClearColor
[1]);
58 CLAMPED_FLOAT_TO_CHAN(clearColor
[BCOMP
], ctx
->Color
.ClearColor
[2]);
59 CLAMPED_FLOAT_TO_CHAN(clearColor
[ACOMP
], ctx
->Color
.ClearColor
[3]);
61 for (i
= 0; i
< height
; i
++) {
62 GLchan rgba
[MAX_WIDTH
][4];
64 for (j
= 0; j
< width
; j
++) {
65 COPY_CHAN4(rgba
[j
], clearColor
);
67 _swrast_mask_rgba_array( ctx
, rb
, width
, x
, y
+ i
, rgba
);
68 rb
->PutRow(ctx
, rb
, width
, x
, y
+ i
, rgba
, NULL
);
74 * Clear color index buffer with masking.
77 clear_ci_buffer_with_masking(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
79 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
80 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
81 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
82 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
83 GLuint span
[MAX_WIDTH
];
84 GLubyte mask
[MAX_WIDTH
];
87 ASSERT(!ctx
->Visual
.rgbMode
);
89 MEMSET( mask
, 1, width
);
90 for (i
= 0; i
< height
;i
++) {
91 for (j
= 0; j
< width
;j
++) {
92 span
[j
] = ctx
->Color
.ClearIndex
;
94 _swrast_mask_ci_array(ctx
, rb
, width
, x
, y
+ i
, span
);
96 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
97 rb
->PutRow(ctx
, rb
, width
, x
, y
+ i
, span
, mask
);
103 * Clear an rgba color buffer without channel masking.
106 clear_rgba_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
108 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
109 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
110 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
111 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
117 ASSERT(ctx
->Visual
.rgbMode
);
119 ASSERT(ctx
->Color
.ColorMask
[0] &&
120 ctx
->Color
.ColorMask
[1] &&
121 ctx
->Color
.ColorMask
[2] &&
122 ctx
->Color
.ColorMask
[3]);
124 ASSERT(rb
->PutMonoRow
);
126 switch (rb
->DataType
) {
127 case GL_UNSIGNED_BYTE
:
128 clear8
[0] = FLOAT_TO_UBYTE(ctx
->Color
.ClearColor
[0]);
129 clear8
[1] = FLOAT_TO_UBYTE(ctx
->Color
.ClearColor
[1]);
130 clear8
[2] = FLOAT_TO_UBYTE(ctx
->Color
.ClearColor
[2]);
131 clear8
[3] = FLOAT_TO_UBYTE(ctx
->Color
.ClearColor
[3]);
134 case GL_UNSIGNED_SHORT
:
135 clear16
[0] = FLOAT_TO_USHORT(ctx
->Color
.ClearColor
[0]);
136 clear16
[1] = FLOAT_TO_USHORT(ctx
->Color
.ClearColor
[1]);
137 clear16
[2] = FLOAT_TO_USHORT(ctx
->Color
.ClearColor
[2]);
138 clear16
[3] = FLOAT_TO_USHORT(ctx
->Color
.ClearColor
[3]);
142 clearVal
= ctx
->Color
.ClearColor
;
145 _mesa_problem(ctx
, "Bad rb DataType in clear_color_buffer");
149 for (i
= 0; i
< height
; i
++) {
150 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, clearVal
, NULL
);
156 * Clear color index buffer without masking.
159 clear_ci_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
161 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
162 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
163 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
164 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
171 ASSERT(!ctx
->Visual
.rgbMode
);
173 ASSERT((ctx
->Color
.IndexMask
& ((1 << ctx
->Visual
.indexBits
) - 1))
174 == (GLuint
) ((1 << ctx
->Visual
.indexBits
) - 1));
176 ASSERT(rb
->PutMonoRow
);
178 /* setup clear value */
179 switch (rb
->DataType
) {
180 case GL_UNSIGNED_BYTE
:
181 clear8
= (GLubyte
) ctx
->Color
.ClearIndex
;
184 case GL_UNSIGNED_SHORT
:
185 clear16
= (GLushort
) ctx
->Color
.ClearIndex
;
188 case GL_UNSIGNED_INT
:
189 clear32
= ctx
->Color
.ClearIndex
;
193 _mesa_problem(ctx
, "Bad rb DataType in clear_color_buffer");
197 for (i
= 0; i
< height
; i
++)
198 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, clearVal
, NULL
);
203 * Clear the front/back/left/right/aux color buffers.
204 * This function is usually only called if the device driver can't
205 * clear its own color buffers for some reason (such as with masking).
208 clear_color_buffers(GLcontext
*ctx
)
210 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
214 if (ctx
->Visual
.rgbMode
) {
215 if (ctx
->Color
.ColorMask
[0] &&
216 ctx
->Color
.ColorMask
[1] &&
217 ctx
->Color
.ColorMask
[2] &&
218 ctx
->Color
.ColorMask
[3]) {
226 const GLuint indexBits
= (1 << ctx
->Visual
.indexBits
) - 1;
227 if ((ctx
->Color
.IndexMask
& indexBits
) == indexBits
) {
235 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
[0]; i
++) {
236 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][i
];
237 #if OLD_RENDERBUFFER || NEW_RENDERBUFFER
238 /* SetBuffer will go away */
239 swrast
->Driver
.SetBuffer(ctx
, ctx
->DrawBuffer
,
240 ctx
->DrawBuffer
->_ColorDrawBit
[0][i
]);
243 if (ctx
->Visual
.rgbMode
) {
245 clear_rgba_buffer_with_masking(ctx
, rb
);
248 clear_rgba_buffer(ctx
, rb
);
253 clear_ci_buffer_with_masking(ctx
, rb
);
256 clear_ci_buffer(ctx
, rb
);
261 /* restore default read/draw buffer */
262 _swrast_use_draw_buffer(ctx
);
267 * Called via the device driver's ctx->Driver.Clear() function if the
268 * device driver can't clear one or more of the buffers itself.
269 * \param mask bitwise-OR of DD_*_BIT flags.
270 * \param all if GL_TRUE, clear whole buffer, else clear specified region.
273 _swrast_Clear(GLcontext
*ctx
, GLbitfield mask
,
274 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
276 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
278 (void) all
; (void) x
; (void) y
; (void) width
; (void) height
;
282 const GLbitfield legalBits
=
283 BUFFER_BIT_FRONT_LEFT
|
284 BUFFER_BIT_FRONT_RIGHT
|
285 BUFFER_BIT_BACK_LEFT
|
286 BUFFER_BIT_BACK_RIGHT
|
294 assert((mask
& (~legalBits
)) == 0);
298 RENDER_START(swrast
,ctx
);
300 /* do software clearing here */
302 if (mask
& ctx
->DrawBuffer
->_ColorDrawBufferMask
[0]) {
303 clear_color_buffers(ctx
);
305 if (mask
& BUFFER_BIT_DEPTH
) {
306 struct gl_renderbuffer
*rb
307 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
308 _swrast_clear_depth_buffer(ctx
, rb
);
310 if (mask
& BUFFER_BIT_ACCUM
) {
311 struct gl_renderbuffer
*rb
312 = ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
313 _swrast_clear_accum_buffer(ctx
, rb
);
315 if (mask
& BUFFER_BIT_STENCIL
) {
316 struct gl_renderbuffer
*rb
317 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
318 _swrast_clear_stencil_buffer(ctx
, rb
);
322 RENDER_FINISH(swrast
,ctx
);
327 * Fallback for ctx->Driver.DrawBuffer()
330 _swrast_DrawBuffer( GLcontext
*ctx
, GLenum mode
)
333 _swrast_use_draw_buffer(ctx
);
338 * Fallback for ctx->Driver.DrawBuffers()
341 _swrast_DrawBuffers( GLcontext
*ctx
, GLsizei n
, const GLenum
*buffers
)
343 _swrast_use_draw_buffer(ctx
);
348 * Setup things so that we read/write spans from the user-designated
349 * read buffer (set via glReadPixels). We usually just have to call
350 * this for glReadPixels, glCopyPixels, etc.
353 _swrast_use_read_buffer( GLcontext
*ctx
)
355 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
357 /* Do this so the software-emulated alpha plane span functions work! */
358 swrast
->CurrentBufferBit
= ctx
->ReadBuffer
->_ColorReadBufferMask
;
359 /* Tell the device driver where to read/write spans */
360 swrast
->Driver
.SetBuffer(ctx
, ctx
->ReadBuffer
, swrast
->CurrentBufferBit
);
365 * Setup things so that we read/write spans from the default draw buffer.
366 * This is the usual mode that Mesa's software rasterizer operates in.
369 _swrast_use_draw_buffer( GLcontext
*ctx
)
371 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
373 /* The user can specify rendering to zero, one, two, or four color
374 * buffers simultaneously with glDrawBuffer()!
375 * We don't expect the span/point/line/triangle functions to deal with
376 * that mess so we'll iterate over the multiple buffers as needed.
377 * But usually we only render to one color buffer at a time.
378 * We set ctx->Color._DriverDrawBuffer to that buffer and tell the
379 * device driver to use that buffer.
380 * Look in s_span.c's multi_write_rgba_span() function to see how
381 * we loop over multiple color buffers when needed.
384 if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_FRONT_LEFT
)
385 swrast
->CurrentBufferBit
= BUFFER_BIT_FRONT_LEFT
;
386 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_BACK_LEFT
)
387 swrast
->CurrentBufferBit
= BUFFER_BIT_BACK_LEFT
;
388 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_FRONT_RIGHT
)
389 swrast
->CurrentBufferBit
= BUFFER_BIT_FRONT_RIGHT
;
390 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_BACK_RIGHT
)
391 swrast
->CurrentBufferBit
= BUFFER_BIT_BACK_RIGHT
;
392 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_AUX0
)
393 swrast
->CurrentBufferBit
= BUFFER_BIT_AUX0
;
394 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_AUX1
)
395 swrast
->CurrentBufferBit
= BUFFER_BIT_AUX1
;
396 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_AUX2
)
397 swrast
->CurrentBufferBit
= BUFFER_BIT_AUX2
;
398 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_AUX3
)
399 swrast
->CurrentBufferBit
= BUFFER_BIT_AUX3
;
401 /* glDrawBuffer(GL_NONE) */
402 swrast
->CurrentBufferBit
= BUFFER_BIT_FRONT_LEFT
; /* we always have this buffer */
404 if (swrast
->Driver
.SetBuffer
)
405 swrast
->Driver
.SetBuffer(ctx
, ctx
->DrawBuffer
, swrast
->CurrentBufferBit
);