accum.c revision b167d5e7
1/* 2 * Mesa 3-D graphics library 3 * 4 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included 14 * in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25#include "glheader.h" 26#include "accum.h" 27#include "condrender.h" 28#include "context.h" 29#include "format_unpack.h" 30#include "format_pack.h" 31#include "imports.h" 32#include "macros.h" 33#include "state.h" 34#include "GL/glext.h" 35#include "mtypes.h" 36#include "main/dispatch.h" 37 38 39void GLAPIENTRY 40_mesa_ClearAccum( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha ) 41{ 42 GLfloat tmp[4]; 43 GET_CURRENT_CONTEXT(ctx); 44 45 tmp[0] = CLAMP( red, -1.0F, 1.0F ); 46 tmp[1] = CLAMP( green, -1.0F, 1.0F ); 47 tmp[2] = CLAMP( blue, -1.0F, 1.0F ); 48 tmp[3] = CLAMP( alpha, -1.0F, 1.0F ); 49 50 if (TEST_EQ_4V(tmp, ctx->Accum.ClearColor)) 51 return; 52 53 COPY_4FV( ctx->Accum.ClearColor, tmp ); 54} 55 56 57void GLAPIENTRY 58_mesa_Accum( GLenum op, GLfloat value ) 59{ 60 GET_CURRENT_CONTEXT(ctx); 61 FLUSH_VERTICES(ctx, 0); 62 63 switch (op) { 64 case GL_ADD: 65 case GL_MULT: 66 case GL_ACCUM: 67 case GL_LOAD: 68 case GL_RETURN: 69 /* OK */ 70 break; 71 default: 72 _mesa_error(ctx, GL_INVALID_ENUM, "glAccum(op)"); 73 return; 74 } 75 76 if (ctx->DrawBuffer->Visual.haveAccumBuffer == 0) { 77 _mesa_error(ctx, GL_INVALID_OPERATION, "glAccum(no accum buffer)"); 78 return; 79 } 80 81 if (ctx->DrawBuffer != ctx->ReadBuffer) { 82 /* See GLX_SGI_make_current_read or WGL_ARB_make_current_read, 83 * or GL_EXT_framebuffer_blit. 84 */ 85 _mesa_error(ctx, GL_INVALID_OPERATION, 86 "glAccum(different read/draw buffers)"); 87 return; 88 } 89 90 if (ctx->NewState) 91 _mesa_update_state(ctx); 92 93 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) { 94 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT, 95 "glAccum(incomplete framebuffer)"); 96 return; 97 } 98 99 if (ctx->RasterDiscard) 100 return; 101 102 if (ctx->RenderMode == GL_RENDER) { 103 _mesa_accum(ctx, op, value); 104 } 105} 106 107 108/** 109 * Clear the accumulation buffer by mapping the renderbuffer and 110 * writing the clear color to it. Called by the driver's implementation 111 * of the glClear function. 112 */ 113void 114_mesa_clear_accum_buffer(struct gl_context *ctx) 115{ 116 GLuint x, y, width, height; 117 GLubyte *accMap; 118 GLint accRowStride; 119 struct gl_renderbuffer *accRb; 120 121 if (!ctx->DrawBuffer) 122 return; 123 124 accRb = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer; 125 if (!accRb) 126 return; /* missing accum buffer, not an error */ 127 128 /* bounds, with scissor */ 129 x = ctx->DrawBuffer->_Xmin; 130 y = ctx->DrawBuffer->_Ymin; 131 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin; 132 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin; 133 134 ctx->Driver.MapRenderbuffer(ctx, accRb, x, y, width, height, 135 GL_MAP_WRITE_BIT, &accMap, &accRowStride); 136 137 if (!accMap) { 138 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); 139 return; 140 } 141 142 if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) { 143 const GLshort clearR = FLOAT_TO_SHORT(ctx->Accum.ClearColor[0]); 144 const GLshort clearG = FLOAT_TO_SHORT(ctx->Accum.ClearColor[1]); 145 const GLshort clearB = FLOAT_TO_SHORT(ctx->Accum.ClearColor[2]); 146 const GLshort clearA = FLOAT_TO_SHORT(ctx->Accum.ClearColor[3]); 147 GLuint i, j; 148 149 for (j = 0; j < height; j++) { 150 GLshort *row = (GLshort *) accMap; 151 152 for (i = 0; i < width; i++) { 153 row[i * 4 + 0] = clearR; 154 row[i * 4 + 1] = clearG; 155 row[i * 4 + 2] = clearB; 156 row[i * 4 + 3] = clearA; 157 } 158 accMap += accRowStride; 159 } 160 } 161 else { 162 /* other types someday? */ 163 _mesa_warning(ctx, "unexpected accum buffer type"); 164 } 165 166 ctx->Driver.UnmapRenderbuffer(ctx, accRb); 167} 168 169 170/** 171 * if (bias) 172 * Accum += value 173 * else 174 * Accum *= value 175 */ 176static void 177accum_scale_or_bias(struct gl_context *ctx, GLfloat value, 178 GLint xpos, GLint ypos, GLint width, GLint height, 179 GLboolean bias) 180{ 181 struct gl_renderbuffer *accRb = 182 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer; 183 GLubyte *accMap; 184 GLint accRowStride; 185 186 assert(accRb); 187 188 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height, 189 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT, 190 &accMap, &accRowStride); 191 192 if (!accMap) { 193 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); 194 return; 195 } 196 197 if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) { 198 const GLshort incr = (GLshort) (value * 32767.0f); 199 GLint i, j; 200 if (bias) { 201 for (j = 0; j < height; j++) { 202 GLshort *acc = (GLshort *) accMap; 203 for (i = 0; i < 4 * width; i++) { 204 acc[i] += incr; 205 } 206 accMap += accRowStride; 207 } 208 } 209 else { 210 /* scale */ 211 for (j = 0; j < height; j++) { 212 GLshort *acc = (GLshort *) accMap; 213 for (i = 0; i < 4 * width; i++) { 214 acc[i] = (GLshort) (acc[i] * value); 215 } 216 accMap += accRowStride; 217 } 218 } 219 } 220 else { 221 /* other types someday? */ 222 } 223 224 ctx->Driver.UnmapRenderbuffer(ctx, accRb); 225} 226 227 228/** 229 * if (load) 230 * Accum = ColorBuf * value 231 * else 232 * Accum += ColorBuf * value 233 */ 234static void 235accum_or_load(struct gl_context *ctx, GLfloat value, 236 GLint xpos, GLint ypos, GLint width, GLint height, 237 GLboolean load) 238{ 239 struct gl_renderbuffer *accRb = 240 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer; 241 struct gl_renderbuffer *colorRb = ctx->ReadBuffer->_ColorReadBuffer; 242 GLubyte *accMap, *colorMap; 243 GLint accRowStride, colorRowStride; 244 GLbitfield mappingFlags; 245 246 if (!colorRb) { 247 /* no read buffer - OK */ 248 return; 249 } 250 251 assert(accRb); 252 253 mappingFlags = GL_MAP_WRITE_BIT; 254 if (!load) /* if we're accumulating */ 255 mappingFlags |= GL_MAP_READ_BIT; 256 257 /* Map accum buffer */ 258 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height, 259 mappingFlags, &accMap, &accRowStride); 260 if (!accMap) { 261 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); 262 return; 263 } 264 265 /* Map color buffer */ 266 ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height, 267 GL_MAP_READ_BIT, 268 &colorMap, &colorRowStride); 269 if (!colorMap) { 270 ctx->Driver.UnmapRenderbuffer(ctx, accRb); 271 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); 272 return; 273 } 274 275 if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) { 276 const GLfloat scale = value * 32767.0f; 277 GLint i, j; 278 GLfloat (*rgba)[4]; 279 280 rgba = malloc(width * 4 * sizeof(GLfloat)); 281 if (rgba) { 282 for (j = 0; j < height; j++) { 283 GLshort *acc = (GLshort *) accMap; 284 285 /* read colors from source color buffer */ 286 _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, rgba); 287 288 if (load) { 289 for (i = 0; i < width; i++) { 290 acc[i * 4 + 0] = (GLshort) (rgba[i][RCOMP] * scale); 291 acc[i * 4 + 1] = (GLshort) (rgba[i][GCOMP] * scale); 292 acc[i * 4 + 2] = (GLshort) (rgba[i][BCOMP] * scale); 293 acc[i * 4 + 3] = (GLshort) (rgba[i][ACOMP] * scale); 294 } 295 } 296 else { 297 /* accumulate */ 298 for (i = 0; i < width; i++) { 299 acc[i * 4 + 0] += (GLshort) (rgba[i][RCOMP] * scale); 300 acc[i * 4 + 1] += (GLshort) (rgba[i][GCOMP] * scale); 301 acc[i * 4 + 2] += (GLshort) (rgba[i][BCOMP] * scale); 302 acc[i * 4 + 3] += (GLshort) (rgba[i][ACOMP] * scale); 303 } 304 } 305 306 colorMap += colorRowStride; 307 accMap += accRowStride; 308 } 309 310 free(rgba); 311 } 312 else { 313 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); 314 } 315 } 316 else { 317 /* other types someday? */ 318 } 319 320 ctx->Driver.UnmapRenderbuffer(ctx, accRb); 321 ctx->Driver.UnmapRenderbuffer(ctx, colorRb); 322} 323 324 325/** 326 * ColorBuffer = Accum * value 327 */ 328static void 329accum_return(struct gl_context *ctx, GLfloat value, 330 GLint xpos, GLint ypos, GLint width, GLint height) 331{ 332 struct gl_framebuffer *fb = ctx->DrawBuffer; 333 struct gl_renderbuffer *accRb = fb->Attachment[BUFFER_ACCUM].Renderbuffer; 334 GLubyte *accMap, *colorMap; 335 GLint accRowStride, colorRowStride; 336 GLuint buffer; 337 338 /* Map accum buffer */ 339 ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height, 340 GL_MAP_READ_BIT, 341 &accMap, &accRowStride); 342 if (!accMap) { 343 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); 344 return; 345 } 346 347 /* Loop over destination buffers */ 348 for (buffer = 0; buffer < fb->_NumColorDrawBuffers; buffer++) { 349 struct gl_renderbuffer *colorRb = fb->_ColorDrawBuffers[buffer]; 350 const GLboolean masking = (!ctx->Color.ColorMask[buffer][RCOMP] || 351 !ctx->Color.ColorMask[buffer][GCOMP] || 352 !ctx->Color.ColorMask[buffer][BCOMP] || 353 !ctx->Color.ColorMask[buffer][ACOMP]); 354 GLbitfield mappingFlags = GL_MAP_WRITE_BIT; 355 356 if (masking) 357 mappingFlags |= GL_MAP_READ_BIT; 358 359 /* Map color buffer */ 360 ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height, 361 mappingFlags, &colorMap, &colorRowStride); 362 if (!colorMap) { 363 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); 364 continue; 365 } 366 367 if (accRb->Format == MESA_FORMAT_RGBA_SNORM16) { 368 const GLfloat scale = value / 32767.0f; 369 GLint i, j; 370 GLfloat (*rgba)[4], (*dest)[4]; 371 372 rgba = malloc(width * 4 * sizeof(GLfloat)); 373 dest = malloc(width * 4 * sizeof(GLfloat)); 374 375 if (rgba && dest) { 376 for (j = 0; j < height; j++) { 377 GLshort *acc = (GLshort *) accMap; 378 379 for (i = 0; i < width; i++) { 380 rgba[i][0] = acc[i * 4 + 0] * scale; 381 rgba[i][1] = acc[i * 4 + 1] * scale; 382 rgba[i][2] = acc[i * 4 + 2] * scale; 383 rgba[i][3] = acc[i * 4 + 3] * scale; 384 } 385 386 if (masking) { 387 388 /* get existing colors from dest buffer */ 389 _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, dest); 390 391 /* use the dest colors where mask[channel] = 0 */ 392 if (ctx->Color.ColorMask[buffer][RCOMP] == 0) { 393 for (i = 0; i < width; i++) 394 rgba[i][RCOMP] = dest[i][RCOMP]; 395 } 396 if (ctx->Color.ColorMask[buffer][GCOMP] == 0) { 397 for (i = 0; i < width; i++) 398 rgba[i][GCOMP] = dest[i][GCOMP]; 399 } 400 if (ctx->Color.ColorMask[buffer][BCOMP] == 0) { 401 for (i = 0; i < width; i++) 402 rgba[i][BCOMP] = dest[i][BCOMP]; 403 } 404 if (ctx->Color.ColorMask[buffer][ACOMP] == 0) { 405 for (i = 0; i < width; i++) 406 rgba[i][ACOMP] = dest[i][ACOMP]; 407 } 408 } 409 410 _mesa_pack_float_rgba_row(colorRb->Format, width, 411 (const GLfloat (*)[4]) rgba, colorMap); 412 413 accMap += accRowStride; 414 colorMap += colorRowStride; 415 } 416 } 417 else { 418 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); 419 } 420 free(rgba); 421 free(dest); 422 } 423 else { 424 /* other types someday? */ 425 } 426 427 ctx->Driver.UnmapRenderbuffer(ctx, colorRb); 428 } 429 430 ctx->Driver.UnmapRenderbuffer(ctx, accRb); 431} 432 433 434 435/** 436 * Software fallback for glAccum. A hardware driver that supports 437 * signed 16-bit color channels could implement hardware accumulation 438 * operations, but no driver does so at this time. 439 */ 440void 441_mesa_accum(struct gl_context *ctx, GLenum op, GLfloat value) 442{ 443 GLint xpos, ypos, width, height; 444 445 if (!ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer) { 446 _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer"); 447 return; 448 } 449 450 if (!_mesa_check_conditional_render(ctx)) 451 return; 452 453 xpos = ctx->DrawBuffer->_Xmin; 454 ypos = ctx->DrawBuffer->_Ymin; 455 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin; 456 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin; 457 458 switch (op) { 459 case GL_ADD: 460 if (value != 0.0F) { 461 accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_TRUE); 462 } 463 break; 464 case GL_MULT: 465 if (value != 1.0F) { 466 accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_FALSE); 467 } 468 break; 469 case GL_ACCUM: 470 if (value != 0.0F) { 471 accum_or_load(ctx, value, xpos, ypos, width, height, GL_FALSE); 472 } 473 break; 474 case GL_LOAD: 475 accum_or_load(ctx, value, xpos, ypos, width, height, GL_TRUE); 476 break; 477 case GL_RETURN: 478 accum_return(ctx, value, xpos, ypos, width, height); 479 break; 480 default: 481 _mesa_problem(ctx, "invalid mode in _mesa_accum()"); 482 break; 483 } 484} 485 486 487void 488_mesa_init_accum( struct gl_context *ctx ) 489{ 490 /* Accumulate buffer group */ 491 ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 ); 492} 493