From 122c6768e3d4c1d1b57203eca70569f9301baab5 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Sat, 3 Dec 2011 10:04:18 -0700 Subject: [PATCH] mesa: rewrite accum buffer support Implemented in terms of renderbuffer mapping/unmapping and format packing/unpacking functions. The swrast and state tracker code for implementing accumulation are unused and will be removed in the next commit. v2: don't use memcpy() in _mesa_clear_accum_buffer() v3: don't allocate MAX_WIDTH arrays, be more careful with mapping flags Reviewed-by: Eric Anholt --- src/mesa/SConscript | 2 - src/mesa/drivers/common/driverfuncs.c | 3 +- src/mesa/drivers/dri/intel/intel_pixel.c | 2 +- src/mesa/main/accum.c | 386 ++++++++++++++++++++++- src/mesa/main/accum.h | 18 ++ src/mesa/sources.mak | 2 - src/mesa/state_tracker/st_cb_clear.c | 5 +- src/mesa/state_tracker/st_cb_fbo.c | 21 ++ src/mesa/state_tracker/st_context.c | 5 +- src/mesa/swrast/s_clear.c | 5 +- 10 files changed, 434 insertions(+), 15 deletions(-) diff --git a/src/mesa/SConscript b/src/mesa/SConscript index 5c50189614f..633cb9cb8c3 100644 --- a/src/mesa/SConscript +++ b/src/mesa/SConscript @@ -153,7 +153,6 @@ math_sources = [ swrast_sources = [ 'swrast/s_aaline.c', 'swrast/s_aatriangle.c', - 'swrast/s_accum.c', 'swrast/s_alpha.c', 'swrast/s_atifragshader.c', 'swrast/s_bitmap.c', @@ -241,7 +240,6 @@ statetracker_sources = [ 'state_tracker/st_atom_stipple.c', 'state_tracker/st_atom_texture.c', 'state_tracker/st_atom_viewport.c', - 'state_tracker/st_cb_accum.c', 'state_tracker/st_cb_bitmap.c', 'state_tracker/st_cb_blit.c', 'state_tracker/st_cb_bufferobjects.c', diff --git a/src/mesa/drivers/common/driverfuncs.c b/src/mesa/drivers/common/driverfuncs.c index 50fcedee2ca..0785cffb42e 100644 --- a/src/mesa/drivers/common/driverfuncs.c +++ b/src/mesa/drivers/common/driverfuncs.c @@ -25,6 +25,7 @@ #include "main/glheader.h" #include "main/imports.h" +#include "main/accum.h" #include "main/arrayobj.h" #include "main/context.h" #include "main/framebuffer.h" @@ -80,7 +81,7 @@ _mesa_init_driver_functions(struct dd_function_table *driver) /* framebuffer/image functions */ driver->Clear = _swrast_Clear; - driver->Accum = _swrast_Accum; + driver->Accum = _mesa_accum; driver->RasterPos = _tnl_RasterPos; driver->DrawPixels = _swrast_DrawPixels; driver->ReadPixels = _mesa_readpixels; diff --git a/src/mesa/drivers/dri/intel/intel_pixel.c b/src/mesa/drivers/dri/intel/intel_pixel.c index ae2ac0537d3..4f665a746e7 100644 --- a/src/mesa/drivers/dri/intel/intel_pixel.c +++ b/src/mesa/drivers/dri/intel/intel_pixel.c @@ -157,7 +157,7 @@ intel_check_blit_format(struct intel_region * region, void intelInitPixelFuncs(struct dd_function_table *functions) { - functions->Accum = _swrast_Accum; + functions->Accum = _mesa_accum; if (!getenv("INTEL_NO_BLIT")) { functions->Bitmap = intelBitmap; functions->CopyPixels = intelCopyPixels; 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; + } +} diff --git a/src/mesa/main/accum.h b/src/mesa/main/accum.h index f16378cc157..5b3f06aa9f1 100644 --- a/src/mesa/main/accum.h +++ b/src/mesa/main/accum.h @@ -42,6 +42,7 @@ struct _glapi_table; struct gl_context; +struct gl_renderbuffer; #if FEATURE_accum @@ -51,6 +52,12 @@ _mesa_ClearAccum( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha ); extern void _mesa_init_accum_dispatch(struct _glapi_table *disp); +extern void +_mesa_accum(struct gl_context *ctx, GLenum op, GLfloat value); + +extern void +_mesa_clear_accum_buffer(struct gl_context *ctx); + #else /* FEATURE_accum */ #include "main/compiler.h" @@ -67,6 +74,17 @@ _mesa_init_accum_dispatch(struct _glapi_table *disp) { } +static inline void +_mesa_accum(struct gl_context *ctx, GLenum op, GLfloat value) +{ +} + +static inline void +_mesa_clear_accum_buffer(struct gl_context *ctx) +{ +} + + #endif /* FEATURE_accum */ extern void diff --git a/src/mesa/sources.mak b/src/mesa/sources.mak index cd18f46ea17..be27c9711f4 100644 --- a/src/mesa/sources.mak +++ b/src/mesa/sources.mak @@ -125,7 +125,6 @@ MATH_XFORM_SOURCES = \ SWRAST_SOURCES = \ swrast/s_aaline.c \ swrast/s_aatriangle.c \ - swrast/s_accum.c \ swrast/s_alpha.c \ swrast/s_atifragshader.c \ swrast/s_bitmap.c \ @@ -209,7 +208,6 @@ STATETRACKER_SOURCES = \ state_tracker/st_atom_stipple.c \ state_tracker/st_atom_texture.c \ state_tracker/st_atom_viewport.c \ - state_tracker/st_cb_accum.c \ state_tracker/st_cb_bitmap.c \ state_tracker/st_cb_blit.c \ state_tracker/st_cb_bufferobjects.c \ diff --git a/src/mesa/state_tracker/st_cb_clear.c b/src/mesa/state_tracker/st_cb_clear.c index 89273e28e89..61d98aeb9a3 100644 --- a/src/mesa/state_tracker/st_cb_clear.c +++ b/src/mesa/state_tracker/st_cb_clear.c @@ -34,12 +34,12 @@ */ #include "main/glheader.h" +#include "main/accum.h" #include "main/formats.h" #include "main/macros.h" #include "program/prog_instruction.h" #include "st_context.h" #include "st_atom.h" -#include "st_cb_accum.h" #include "st_cb_clear.h" #include "st_cb_fbo.h" #include "st_format.h" @@ -599,8 +599,7 @@ st_Clear(struct gl_context *ctx, GLbitfield mask) ctx->Depth.Clear, ctx->Stencil.Clear); } if (mask & BUFFER_BIT_ACCUM) - st_clear_accum_buffer(ctx, - ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer); + _mesa_clear_accum_buffer(ctx); } diff --git a/src/mesa/state_tracker/st_cb_fbo.c b/src/mesa/state_tracker/st_cb_fbo.c index 2a60ed4df8a..ec40a2b70c1 100644 --- a/src/mesa/state_tracker/st_cb_fbo.c +++ b/src/mesa/state_tracker/st_cb_fbo.c @@ -667,6 +667,22 @@ st_MapRenderbuffer(struct gl_context *ctx, unsigned usage; GLuint y2; + if (strb->software) { + /* software-allocated renderbuffer (probably an accum buffer) */ + GLubyte *map = (GLubyte *) strb->data; + if (strb->data) { + map += strb->stride * y; + map += util_format_get_blocksize(strb->format) * x; + *mapOut = map; + *rowStrideOut = strb->stride; + } + else { + *mapOut = NULL; + *rowStrideOut = 0; + } + return; + } + usage = 0x0; if (mode & GL_MAP_READ_BIT) usage |= PIPE_TRANSFER_READ; @@ -716,6 +732,11 @@ st_UnmapRenderbuffer(struct gl_context *ctx, struct st_renderbuffer *strb = st_renderbuffer(rb); struct pipe_context *pipe = st->pipe; + if (strb->software) { + /* software-allocated renderbuffer (probably an accum buffer) */ + return; + } + pipe_transfer_unmap(pipe, strb->transfer); pipe->transfer_destroy(pipe, strb->transfer); strb->transfer = NULL; diff --git a/src/mesa/state_tracker/st_context.c b/src/mesa/state_tracker/st_context.c index a1817720d90..dc1d33f1dae 100644 --- a/src/mesa/state_tracker/st_context.c +++ b/src/mesa/state_tracker/st_context.c @@ -26,6 +26,7 @@ **************************************************************************/ #include "main/imports.h" +#include "main/accum.h" #include "main/context.h" #include "main/samplerobj.h" #include "main/shaderobj.h" @@ -34,7 +35,6 @@ #include "glapi/glapi.h" #include "st_context.h" #include "st_debug.h" -#include "st_cb_accum.h" #include "st_cb_bitmap.h" #include "st_cb_blit.h" #include "st_cb_bufferobjects.h" @@ -276,7 +276,8 @@ void st_init_driver_functions(struct dd_function_table *functions) _mesa_init_shader_object_functions(functions); _mesa_init_sampler_object_functions(functions); - st_init_accum_functions(functions); + functions->Accum = _mesa_accum; + st_init_blit_functions(functions); st_init_bufferobject_functions(functions); st_init_clear_functions(functions); diff --git a/src/mesa/swrast/s_clear.c b/src/mesa/swrast/s_clear.c index 980d29bbade..615747d6984 100644 --- a/src/mesa/swrast/s_clear.c +++ b/src/mesa/swrast/s_clear.c @@ -23,13 +23,13 @@ */ #include "main/glheader.h" +#include "main/accum.h" #include "main/colormac.h" #include "main/condrender.h" #include "main/macros.h" #include "main/imports.h" #include "main/mtypes.h" -#include "s_accum.h" #include "s_context.h" #include "s_depth.h" #include "s_masking.h" @@ -225,8 +225,7 @@ _swrast_Clear(struct gl_context *ctx, GLbitfield buffers) _swrast_clear_depth_buffer(ctx, ctx->DrawBuffer->_DepthBuffer); } if (buffers & BUFFER_BIT_ACCUM) { - _swrast_clear_accum_buffer(ctx, - ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer); + _mesa_clear_accum_buffer(ctx); } if (buffers & BUFFER_BIT_STENCIL) { _swrast_clear_stencil_buffer(ctx, ctx->DrawBuffer->_StencilBuffer); -- 2.30.2