diff options
Diffstat (limited to 'src/mesa/main/accum.c')
-rw-r--r-- | src/mesa/main/accum.c | 386 |
1 files changed, 385 insertions, 1 deletions
diff --git a/src/mesa/main/accum.c b/src/mesa/main/accum.c index d7ed3a849be..eb06bbb6e89 100644 --- a/src/mesa/main/accum.c +++ b/src/mesa/main/accum.c @@ -24,7 +24,10 @@ #include "glheader.h" #include "accum.h" +#include "condrender.h" #include "context.h" +#include "format_unpack.h" +#include "format_pack.h" #include "imports.h" #include "macros.h" #include "mfeatures.h" @@ -101,7 +104,7 @@ _mesa_Accum( GLenum op, GLfloat value ) return; if (ctx->RenderMode == GL_RENDER) { - ctx->Driver.Accum(ctx, op, value); + _mesa_accum(ctx, op, value); } } @@ -123,3 +126,384 @@ _mesa_init_accum( struct gl_context *ctx ) /* Accumulate buffer group */ ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 ); } + + + + +/** + * Clear the accumulation buffer by mapping the renderbuffer and + * writing the clear color to it. Called by the driver's implementation + * of the glClear function. + */ +void +_mesa_clear_accum_buffer(struct gl_context *ctx) +{ + GLuint x, y, width, height; + GLubyte *accMap; + GLint accRowStride; + struct gl_renderbuffer *accRb; + + if (!ctx->DrawBuffer) + return; + + accRb = ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer; + if (!accRb) + return; /* missing accum buffer, not an error */ + + /* bounds, with scissor */ + x = ctx->DrawBuffer->_Xmin; + y = ctx->DrawBuffer->_Ymin; + width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin; + height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin; + + ctx->Driver.MapRenderbuffer(ctx, accRb, x, y, width, height, + GL_MAP_WRITE_BIT, &accMap, &accRowStride); + + if (!accMap) { + _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); + return; + } + + if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) { + const GLshort clearR = FLOAT_TO_SHORT(ctx->Accum.ClearColor[0]); + const GLshort clearG = FLOAT_TO_SHORT(ctx->Accum.ClearColor[1]); + const GLshort clearB = FLOAT_TO_SHORT(ctx->Accum.ClearColor[2]); + const GLshort clearA = FLOAT_TO_SHORT(ctx->Accum.ClearColor[3]); + GLuint i, j; + + for (j = 0; j < height; j++) { + GLshort *row = (GLshort *) accMap; + + for (i = 0; i < width; i++) { + row[i * 4 + 0] = clearR; + row[i * 4 + 1] = clearG; + row[i * 4 + 2] = clearB; + row[i * 4 + 3] = clearA; + } + accMap += accRowStride; + } + } + else { + /* other types someday? */ + _mesa_warning(ctx, "unexpected accum buffer type"); + } + + ctx->Driver.UnmapRenderbuffer(ctx, accRb); +} + + +/** + * if (bias) + * Accum += value + * else + * Accum *= value + */ +static void +accum_scale_or_bias(struct gl_context *ctx, GLfloat value, + GLint xpos, GLint ypos, GLint width, GLint height, + GLboolean bias) +{ + struct gl_renderbuffer *accRb = + ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer; + GLubyte *accMap; + GLint accRowStride; + + assert(accRb); + + ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height, + GL_MAP_READ_BIT | GL_MAP_WRITE_BIT, + &accMap, &accRowStride); + + if (!accMap) { + _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); + return; + } + + if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) { + const GLshort incr = (GLshort) (value * 32767.0f); + GLuint i, j; + if (bias) { + for (j = 0; j < height; j++) { + GLshort *acc = (GLshort *) accMap; + for (i = 0; i < 4 * width; i++) { + acc[i] += incr; + } + accMap += accRowStride; + } + } + else { + /* scale */ + for (j = 0; j < height; j++) { + GLshort *acc = (GLshort *) accMap; + for (i = 0; i < 4 * width; i++) { + acc[i] = (GLshort) (acc[i] * value); + } + accMap += accRowStride; + } + } + } + else { + /* other types someday? */ + } + + ctx->Driver.UnmapRenderbuffer(ctx, accRb); +} + + +/** + * if (load) + * Accum = ColorBuf * value + * else + * Accum += ColorBuf * value + */ +static void +accum_or_load(struct gl_context *ctx, GLfloat value, + GLint xpos, GLint ypos, GLint width, GLint height, + GLboolean load) +{ + struct gl_renderbuffer *accRb = + ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer; + struct gl_renderbuffer *colorRb = ctx->ReadBuffer->_ColorReadBuffer; + GLubyte *accMap, *colorMap; + GLint accRowStride, colorRowStride; + GLbitfield mappingFlags; + + if (!colorRb) { + /* no read buffer - OK */ + return; + } + + assert(accRb); + + mappingFlags = GL_MAP_WRITE_BIT; + if (!load) /* if we're accumulating */ + mappingFlags |= GL_MAP_READ_BIT; + + /* Map accum buffer */ + ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height, + mappingFlags, &accMap, &accRowStride); + if (!accMap) { + _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); + return; + } + + /* Map color buffer */ + ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height, + GL_MAP_READ_BIT, + &colorMap, &colorRowStride); + if (!colorMap) { + ctx->Driver.UnmapRenderbuffer(ctx, accRb); + _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); + return; + } + + if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) { + const GLfloat scale = value * 32767.0f; + GLuint i, j; + GLfloat (*rgba)[4]; + + rgba = (GLfloat (*)[4]) malloc(width * 4 * sizeof(GLfloat)); + if (rgba) { + for (j = 0; j < height; j++) { + GLshort *acc = (GLshort *) accMap; + + /* read colors from source color buffer */ + _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, rgba); + + if (load) { + for (i = 0; i < width; i++) { + acc[i * 4 + 0] = (GLshort) (rgba[i][RCOMP] * scale); + acc[i * 4 + 1] = (GLshort) (rgba[i][GCOMP] * scale); + acc[i * 4 + 2] = (GLshort) (rgba[i][BCOMP] * scale); + acc[i * 4 + 3] = (GLshort) (rgba[i][ACOMP] * scale); + } + } + else { + /* accumulate */ + for (i = 0; i < width; i++) { + acc[i * 4 + 0] += (GLshort) (rgba[i][RCOMP] * scale); + acc[i * 4 + 1] += (GLshort) (rgba[i][GCOMP] * scale); + acc[i * 4 + 2] += (GLshort) (rgba[i][BCOMP] * scale); + acc[i * 4 + 3] += (GLshort) (rgba[i][ACOMP] * scale); + } + } + + colorMap += colorRowStride; + accMap += accRowStride; + } + + free(rgba); + } + else { + _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); + } + } + else { + /* other types someday? */ + } + + ctx->Driver.UnmapRenderbuffer(ctx, accRb); + ctx->Driver.UnmapRenderbuffer(ctx, colorRb); +} + + +/** + * ColorBuffer = Accum * value + */ +static void +accum_return(struct gl_context *ctx, GLfloat value, + GLint xpos, GLint ypos, GLint width, GLint height) +{ + struct gl_framebuffer *fb = ctx->DrawBuffer; + struct gl_renderbuffer *accRb = fb->Attachment[BUFFER_ACCUM].Renderbuffer; + GLubyte *accMap, *colorMap; + GLint accRowStride, colorRowStride; + GLuint buffer; + + /* Map accum buffer */ + ctx->Driver.MapRenderbuffer(ctx, accRb, xpos, ypos, width, height, + GL_MAP_READ_BIT, + &accMap, &accRowStride); + if (!accMap) { + _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); + return; + } + + /* Loop over destination buffers */ + for (buffer = 0; buffer < fb->_NumColorDrawBuffers; buffer++) { + struct gl_renderbuffer *colorRb = fb->_ColorDrawBuffers[buffer]; + const GLboolean masking = (!ctx->Color.ColorMask[buffer][RCOMP] || + !ctx->Color.ColorMask[buffer][GCOMP] || + !ctx->Color.ColorMask[buffer][BCOMP] || + !ctx->Color.ColorMask[buffer][ACOMP]); + GLbitfield mappingFlags = GL_MAP_WRITE_BIT; + + if (masking) + mappingFlags |= GL_MAP_READ_BIT; + + /* Map color buffer */ + ctx->Driver.MapRenderbuffer(ctx, colorRb, xpos, ypos, width, height, + mappingFlags, &colorMap, &colorRowStride); + if (!colorMap) { + _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); + continue; + } + + if (accRb->Format == MESA_FORMAT_SIGNED_RGBA_16) { + const GLfloat scale = value / 32767.0f; + GLint i, j; + GLfloat (*rgba)[4], (*dest)[4]; + + rgba = (GLfloat (*)[4]) malloc(width * 4 * sizeof(GLfloat)); + dest = (GLfloat (*)[4]) malloc(width * 4 * sizeof(GLfloat)); + + if (rgba && dest) { + for (j = 0; j < height; j++) { + GLshort *acc = (GLshort *) accMap; + + for (i = 0; i < width; i++) { + rgba[i][0] = acc[i * 4 + 0] * scale; + rgba[i][1] = acc[i * 4 + 1] * scale; + rgba[i][2] = acc[i * 4 + 2] * scale; + rgba[i][3] = acc[i * 4 + 3] * scale; + } + + if (masking) { + + /* get existing colors from dest buffer */ + _mesa_unpack_rgba_row(colorRb->Format, width, colorMap, dest); + + /* use the dest colors where mask[channel] = 0 */ + if (ctx->Color.ColorMask[buffer][RCOMP] == 0) { + for (i = 0; i < width; i++) + rgba[i][RCOMP] = dest[i][RCOMP]; + } + if (ctx->Color.ColorMask[buffer][GCOMP] == 0) { + for (i = 0; i < width; i++) + rgba[i][GCOMP] = dest[i][GCOMP]; + } + if (ctx->Color.ColorMask[buffer][BCOMP] == 0) { + for (i = 0; i < width; i++) + rgba[i][BCOMP] = dest[i][BCOMP]; + } + if (ctx->Color.ColorMask[buffer][ACOMP] == 0) { + for (i = 0; i < width; i++) + rgba[i][ACOMP] = dest[i][ACOMP]; + } + } + + _mesa_pack_float_rgba_row(colorRb->Format, width, + (const GLfloat (*)[4]) rgba, colorMap); + + accMap += accRowStride; + colorMap += colorRowStride; + } + } + else { + _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAccum"); + } + free(rgba); + free(dest); + } + else { + /* other types someday? */ + } + + ctx->Driver.UnmapRenderbuffer(ctx, colorRb); + } + + ctx->Driver.UnmapRenderbuffer(ctx, accRb); +} + + + +/** + * Software fallback for glAccum. A hardware driver that supports + * signed 16-bit color channels could implement hardware accumulation + * operations, but no driver does so at this time. + */ +void +_mesa_accum(struct gl_context *ctx, GLenum op, GLfloat value) +{ + GLint xpos, ypos, width, height; + + if (!ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer) { + _mesa_warning(ctx, "Calling glAccum() without an accumulation buffer"); + return; + } + + if (!_mesa_check_conditional_render(ctx)) + return; + + xpos = ctx->DrawBuffer->_Xmin; + ypos = ctx->DrawBuffer->_Ymin; + width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin; + height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin; + + switch (op) { + case GL_ADD: + if (value != 0.0F) { + accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_TRUE); + } + break; + case GL_MULT: + if (value != 1.0F) { + accum_scale_or_bias(ctx, value, xpos, ypos, width, height, GL_FALSE); + } + break; + case GL_ACCUM: + if (value != 0.0F) { + accum_or_load(ctx, value, xpos, ypos, width, height, GL_FALSE); + } + break; + case GL_LOAD: + accum_or_load(ctx, value, xpos, ypos, width, height, GL_TRUE); + break; + case GL_RETURN: + accum_return(ctx, value, xpos, ypos, width, height); + break; + default: + _mesa_problem(ctx, "invalid mode in _mesa_accum()"); + break; + } +} |