| 1 |
/* |
| 2 |
* Mesa 3-D graphics library |
| 3 |
* Version: 6.4.1 |
| 4 |
* |
| 5 |
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved. |
| 6 |
* |
| 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: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included |
| 15 |
* in all copies or substantial portions of the Software. |
| 16 |
* |
| 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. |
| 23 |
*/ |
| 24 |
|
| 25 |
|
| 26 |
#include "glheader.h" |
| 27 |
#include "colormac.h" |
| 28 |
#include "macros.h" |
| 29 |
#include "imports.h" |
| 30 |
#include "mtypes.h" |
| 31 |
#include "fbobject.h" |
| 32 |
|
| 33 |
#include "s_accum.h" |
| 34 |
#include "s_context.h" |
| 35 |
#include "s_depth.h" |
| 36 |
#include "s_masking.h" |
| 37 |
#include "s_stencil.h" |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Clear the color buffer when glColorMask is in effect. |
| 42 |
*/ |
| 43 |
static void |
| 44 |
clear_rgba_buffer_with_masking(GLcontext *ctx, struct gl_renderbuffer *rb) |
| 45 |
{ |
| 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; |
| 50 |
GLchan clearColor[4]; |
| 51 |
GLint i; |
| 52 |
|
| 53 |
ASSERT(ctx->Visual.rgbMode); |
| 54 |
ASSERT(rb->PutRow); |
| 55 |
|
| 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]); |
| 60 |
|
| 61 |
for (i = 0; i < height; i++) { |
| 62 |
GLchan rgba[MAX_WIDTH][4]; |
| 63 |
GLint j; |
| 64 |
for (j = 0; j < width; j++) { |
| 65 |
COPY_CHAN4(rgba[j], clearColor); |
| 66 |
} |
| 67 |
_swrast_mask_rgba_array( ctx, rb, width, x, y + i, rgba ); |
| 68 |
rb->PutRow(ctx, rb, width, x, y + i, rgba, NULL); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Clear color index buffer with masking. |
| 75 |
*/ |
| 76 |
static void |
| 77 |
clear_ci_buffer_with_masking(GLcontext *ctx, struct gl_renderbuffer *rb) |
| 78 |
{ |
| 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]; |
| 85 |
GLint i, j; |
| 86 |
|
| 87 |
ASSERT(!ctx->Visual.rgbMode); |
| 88 |
|
| 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; |
| 93 |
} |
| 94 |
_swrast_mask_ci_array(ctx, rb, width, x, y + i, span); |
| 95 |
ASSERT(rb->PutRow); |
| 96 |
ASSERT(rb->DataType == GL_UNSIGNED_INT); |
| 97 |
rb->PutRow(ctx, rb, width, x, y + i, span, mask); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* Clear an rgba color buffer without channel masking. |
| 104 |
*/ |
| 105 |
static void |
| 106 |
clear_rgba_buffer(GLcontext *ctx, struct gl_renderbuffer *rb) |
| 107 |
{ |
| 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; |
| 112 |
GLubyte clear8[4]; |
| 113 |
GLushort clear16[4]; |
| 114 |
GLvoid *clearVal; |
| 115 |
GLint i; |
| 116 |
|
| 117 |
ASSERT(ctx->Visual.rgbMode); |
| 118 |
|
| 119 |
ASSERT(ctx->Color.ColorMask[0] && |
| 120 |
ctx->Color.ColorMask[1] && |
| 121 |
ctx->Color.ColorMask[2] && |
| 122 |
ctx->Color.ColorMask[3]); |
| 123 |
|
| 124 |
ASSERT(rb->PutMonoRow); |
| 125 |
|
| 126 |
switch (rb->DataType) { |
| 127 |
case GL_UNSIGNED_BYTE: |
| 128 |
UNCLAMPED_FLOAT_TO_UBYTE(clear8[0], ctx->Color.ClearColor[0]); |
| 129 |
UNCLAMPED_FLOAT_TO_UBYTE(clear8[1], ctx->Color.ClearColor[1]); |
| 130 |
UNCLAMPED_FLOAT_TO_UBYTE(clear8[2], ctx->Color.ClearColor[2]); |
| 131 |
UNCLAMPED_FLOAT_TO_UBYTE(clear8[3], ctx->Color.ClearColor[3]); |
| 132 |
clearVal = clear8; |
| 133 |
break; |
| 134 |
case GL_UNSIGNED_SHORT: |
| 135 |
UNCLAMPED_FLOAT_TO_USHORT(clear16[0], ctx->Color.ClearColor[0]); |
| 136 |
UNCLAMPED_FLOAT_TO_USHORT(clear16[1], ctx->Color.ClearColor[1]); |
| 137 |
UNCLAMPED_FLOAT_TO_USHORT(clear16[2], ctx->Color.ClearColor[2]); |
| 138 |
UNCLAMPED_FLOAT_TO_USHORT(clear16[3], ctx->Color.ClearColor[3]); |
| 139 |
clearVal = clear16; |
| 140 |
break; |
| 141 |
case GL_FLOAT: |
| 142 |
clearVal = ctx->Color.ClearColor; |
| 143 |
break; |
| 144 |
default: |
| 145 |
_mesa_problem(ctx, "Bad rb DataType in clear_color_buffer"); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
for (i = 0; i < height; i++) { |
| 150 |
rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Clear color index buffer without masking. |
| 157 |
*/ |
| 158 |
static void |
| 159 |
clear_ci_buffer(GLcontext *ctx, struct gl_renderbuffer *rb) |
| 160 |
{ |
| 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; |
| 165 |
GLubyte clear8; |
| 166 |
GLushort clear16; |
| 167 |
GLuint clear32; |
| 168 |
GLvoid *clearVal; |
| 169 |
GLint i; |
| 170 |
|
| 171 |
ASSERT(!ctx->Visual.rgbMode); |
| 172 |
|
| 173 |
ASSERT((ctx->Color.IndexMask & ((1 << ctx->Visual.indexBits) - 1)) |
| 174 |
== (GLuint) ((1 << ctx->Visual.indexBits) - 1)); |
| 175 |
|
| 176 |
ASSERT(rb->PutMonoRow); |
| 177 |
|
| 178 |
/* setup clear value */ |
| 179 |
switch (rb->DataType) { |
| 180 |
case GL_UNSIGNED_BYTE: |
| 181 |
clear8 = (GLubyte) ctx->Color.ClearIndex; |
| 182 |
clearVal = &clear8; |
| 183 |
break; |
| 184 |
case GL_UNSIGNED_SHORT: |
| 185 |
clear16 = (GLushort) ctx->Color.ClearIndex; |
| 186 |
clearVal = &clear16; |
| 187 |
break; |
| 188 |
case GL_UNSIGNED_INT: |
| 189 |
clear32 = ctx->Color.ClearIndex; |
| 190 |
clearVal = &clear32; |
| 191 |
break; |
| 192 |
default: |
| 193 |
_mesa_problem(ctx, "Bad rb DataType in clear_color_buffer"); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
for (i = 0; i < height; i++) |
| 198 |
rb->PutMonoRow(ctx, rb, width, x, y + i, clearVal, NULL); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
/** |
| 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). |
| 206 |
*/ |
| 207 |
static void |
| 208 |
clear_color_buffers(GLcontext *ctx) |
| 209 |
{ |
| 210 |
SWcontext *swrast = SWRAST_CONTEXT(ctx); |
| 211 |
GLboolean masking; |
| 212 |
GLuint i; |
| 213 |
|
| 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]) { |
| 219 |
masking = GL_FALSE; |
| 220 |
} |
| 221 |
else { |
| 222 |
masking = GL_TRUE; |
| 223 |
} |
| 224 |
} |
| 225 |
else { |
| 226 |
const GLuint indexBits = (1 << ctx->Visual.indexBits) - 1; |
| 227 |
if ((ctx->Color.IndexMask & indexBits) == indexBits) { |
| 228 |
masking = GL_FALSE; |
| 229 |
} |
| 230 |
else { |
| 231 |
masking = GL_TRUE; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers[0]; i++) { |
| 236 |
struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][i]; |
| 237 |
#if OLD_RENDERBUFFER |
| 238 |
/* SetBuffer will go away */ |
| 239 |
if (swrast->Driver.SetBuffer) |
| 240 |
swrast->Driver.SetBuffer(ctx, ctx->DrawBuffer, |
| 241 |
ctx->DrawBuffer->_ColorDrawBit[0][i]); |
| 242 |
#endif |
| 243 |
|
| 244 |
if (ctx->Visual.rgbMode) { |
| 245 |
if (masking) { |
| 246 |
clear_rgba_buffer_with_masking(ctx, rb); |
| 247 |
} |
| 248 |
else { |
| 249 |
clear_rgba_buffer(ctx, rb); |
| 250 |
} |
| 251 |
} |
| 252 |
else { |
| 253 |
if (masking) { |
| 254 |
clear_ci_buffer_with_masking(ctx, rb); |
| 255 |
} |
| 256 |
else { |
| 257 |
clear_ci_buffer(ctx, rb); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/* restore default read/draw buffer */ |
| 263 |
_swrast_use_draw_buffer(ctx); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
/** |
| 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. |
| 272 |
*/ |
| 273 |
void |
| 274 |
_swrast_Clear(GLcontext *ctx, GLbitfield mask, |
| 275 |
GLboolean all, GLint x, GLint y, GLint width, GLint height) |
| 276 |
{ |
| 277 |
SWcontext *swrast = SWRAST_CONTEXT(ctx); |
| 278 |
|
| 279 |
(void) all; (void) x; (void) y; (void) width; (void) height; |
| 280 |
|
| 281 |
#ifdef DEBUG_FOO |
| 282 |
{ |
| 283 |
const GLbitfield legalBits = |
| 284 |
BUFFER_BIT_FRONT_LEFT | |
| 285 |
BUFFER_BIT_FRONT_RIGHT | |
| 286 |
BUFFER_BIT_BACK_LEFT | |
| 287 |
BUFFER_BIT_BACK_RIGHT | |
| 288 |
BUFFER_BIT_DEPTH | |
| 289 |
BUFFER_BIT_STENCIL | |
| 290 |
BUFFER_BIT_ACCUM | |
| 291 |
BUFFER_BIT_AUX0 | |
| 292 |
BUFFER_BIT_AUX1 | |
| 293 |
BUFFER_BIT_AUX2 | |
| 294 |
BUFFER_BIT_AUX3; |
| 295 |
assert((mask & (~legalBits)) == 0); |
| 296 |
} |
| 297 |
#endif |
| 298 |
|
| 299 |
RENDER_START(swrast,ctx); |
| 300 |
|
| 301 |
/* do software clearing here */ |
| 302 |
if (mask) { |
| 303 |
if (mask & ctx->DrawBuffer->_ColorDrawBufferMask[0]) { |
| 304 |
clear_color_buffers(ctx); |
| 305 |
} |
| 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); |
| 310 |
} |
| 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); |
| 315 |
} |
| 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); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
RENDER_FINISH(swrast,ctx); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
/* |
| 328 |
* Fallback for ctx->Driver.DrawBuffer() |
| 329 |
*/ |
| 330 |
void |
| 331 |
_swrast_DrawBuffer( GLcontext *ctx, GLenum mode ) |
| 332 |
{ |
| 333 |
(void) mode; |
| 334 |
_swrast_use_draw_buffer(ctx); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
/* |
| 339 |
* Fallback for ctx->Driver.DrawBuffers() |
| 340 |
*/ |
| 341 |
void |
| 342 |
_swrast_DrawBuffers( GLcontext *ctx, GLsizei n, const GLenum *buffers ) |
| 343 |
{ |
| 344 |
_swrast_use_draw_buffer(ctx); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
/* |
| 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. |
| 352 |
*/ |
| 353 |
void |
| 354 |
_swrast_use_read_buffer( GLcontext *ctx ) |
| 355 |
{ |
| 356 |
SWcontext *swrast = SWRAST_CONTEXT(ctx); |
| 357 |
|
| 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); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
/* |
| 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. |
| 369 |
*/ |
| 370 |
void |
| 371 |
_swrast_use_draw_buffer( GLcontext *ctx ) |
| 372 |
{ |
| 373 |
SWcontext *swrast = SWRAST_CONTEXT(ctx); |
| 374 |
|
| 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. |
| 384 |
*/ |
| 385 |
|
| 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; |
| 402 |
else |
| 403 |
/* glDrawBuffer(GL_NONE) */ |
| 404 |
swrast->CurrentBufferBit = BUFFER_BIT_FRONT_LEFT; /* we always have this buffer */ |
| 405 |
|
| 406 |
if (swrast->Driver.SetBuffer) |
| 407 |
swrast->Driver.SetBuffer(ctx, ctx->DrawBuffer, swrast->CurrentBufferBit); |
| 408 |
} |