e7889008972c34803f08117afd873407c9adc436
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 if (swrast
->Driver
.SetBuffer
)
240 swrast
->Driver
.SetBuffer(ctx
, ctx
->DrawBuffer
,
241 ctx
->DrawBuffer
->_ColorDrawBit
[0][i
]);
244 if (ctx
->Visual
.rgbMode
) {
246 clear_rgba_buffer_with_masking(ctx
, rb
);
249 clear_rgba_buffer(ctx
, rb
);
254 clear_ci_buffer_with_masking(ctx
, rb
);
257 clear_ci_buffer(ctx
, rb
);
262 /* restore default read/draw buffer */
263 _swrast_use_draw_buffer(ctx
);
268 * Called via the device driver's ctx->Driver.Clear() function if the
269 * device driver can't clear one or more of the buffers itself.
270 * \param mask bitwise-OR of DD_*_BIT flags.
271 * \param all if GL_TRUE, clear whole buffer, else clear specified region.
274 _swrast_Clear(GLcontext
*ctx
, GLbitfield mask
,
275 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
277 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
279 (void) all
; (void) x
; (void) y
; (void) width
; (void) height
;
283 const GLbitfield legalBits
=
284 BUFFER_BIT_FRONT_LEFT
|
285 BUFFER_BIT_FRONT_RIGHT
|
286 BUFFER_BIT_BACK_LEFT
|
287 BUFFER_BIT_BACK_RIGHT
|
295 assert((mask
& (~legalBits
)) == 0);
299 RENDER_START(swrast
,ctx
);
301 /* do software clearing here */
303 if (mask
& ctx
->DrawBuffer
->_ColorDrawBufferMask
[0]) {
304 clear_color_buffers(ctx
);
306 if (mask
& BUFFER_BIT_DEPTH
) {
307 struct gl_renderbuffer
*rb
308 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
309 _swrast_clear_depth_buffer(ctx
, rb
);
311 if (mask
& BUFFER_BIT_ACCUM
) {
312 struct gl_renderbuffer
*rb
313 = ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
;
314 _swrast_clear_accum_buffer(ctx
, rb
);
316 if (mask
& BUFFER_BIT_STENCIL
) {
317 struct gl_renderbuffer
*rb
318 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
319 _swrast_clear_stencil_buffer(ctx
, rb
);
323 RENDER_FINISH(swrast
,ctx
);
328 * Fallback for ctx->Driver.DrawBuffer()
331 _swrast_DrawBuffer( GLcontext
*ctx
, GLenum mode
)
334 _swrast_use_draw_buffer(ctx
);
339 * Fallback for ctx->Driver.DrawBuffers()
342 _swrast_DrawBuffers( GLcontext
*ctx
, GLsizei n
, const GLenum
*buffers
)
344 _swrast_use_draw_buffer(ctx
);
349 * Setup things so that we read/write spans from the user-designated
350 * read buffer (set via glReadPixels). We usually just have to call
351 * this for glReadPixels, glCopyPixels, etc.
354 _swrast_use_read_buffer( GLcontext
*ctx
)
356 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
358 /* Do this so the software-emulated alpha plane span functions work! */
359 swrast
->CurrentBufferBit
= ctx
->ReadBuffer
->_ColorReadBufferMask
;
360 /* Tell the device driver where to read/write spans */
361 if (swrast
->Driver
.SetBuffer
)
362 swrast
->Driver
.SetBuffer(ctx
, ctx
->ReadBuffer
, swrast
->CurrentBufferBit
);
367 * Setup things so that we read/write spans from the default draw buffer.
368 * This is the usual mode that Mesa's software rasterizer operates in.
371 _swrast_use_draw_buffer( GLcontext
*ctx
)
373 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
375 /* The user can specify rendering to zero, one, two, or four color
376 * buffers simultaneously with glDrawBuffer()!
377 * We don't expect the span/point/line/triangle functions to deal with
378 * that mess so we'll iterate over the multiple buffers as needed.
379 * But usually we only render to one color buffer at a time.
380 * We set ctx->Color._DriverDrawBuffer to that buffer and tell the
381 * device driver to use that buffer.
382 * Look in s_span.c's multi_write_rgba_span() function to see how
383 * we loop over multiple color buffers when needed.
386 if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_FRONT_LEFT
)
387 swrast
->CurrentBufferBit
= BUFFER_BIT_FRONT_LEFT
;
388 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_BACK_LEFT
)
389 swrast
->CurrentBufferBit
= BUFFER_BIT_BACK_LEFT
;
390 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_FRONT_RIGHT
)
391 swrast
->CurrentBufferBit
= BUFFER_BIT_FRONT_RIGHT
;
392 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_BACK_RIGHT
)
393 swrast
->CurrentBufferBit
= BUFFER_BIT_BACK_RIGHT
;
394 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_AUX0
)
395 swrast
->CurrentBufferBit
= BUFFER_BIT_AUX0
;
396 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_AUX1
)
397 swrast
->CurrentBufferBit
= BUFFER_BIT_AUX1
;
398 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_AUX2
)
399 swrast
->CurrentBufferBit
= BUFFER_BIT_AUX2
;
400 else if (ctx
->DrawBuffer
->_ColorDrawBufferMask
[0] & BUFFER_BIT_AUX3
)
401 swrast
->CurrentBufferBit
= BUFFER_BIT_AUX3
;
403 /* glDrawBuffer(GL_NONE) */
404 swrast
->CurrentBufferBit
= BUFFER_BIT_FRONT_LEFT
; /* we always have this buffer */
406 if (swrast
->Driver
.SetBuffer
)
407 swrast
->Driver
.SetBuffer(ctx
, ctx
->DrawBuffer
, swrast
->CurrentBufferBit
);