From 68da4b50e9b6aa72a9b155f650952620063e1b94 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Sun, 23 Oct 2011 10:44:47 -0600 Subject: [PATCH] mesa: add swrast_texture_image::Buffer In the past, swrast_texture_image::Data has been overloaded. It could either point to malloc'd memory storing texture data, or it could point to a current mapping of GPU memory. Now, Buffer always points to malloc'd memory (if we're not using GPU memory) and Data always points to mapped memory. The next step would be to rename Data -> Map. This change also involves adding swrast functions for mapping textures and renderbuffers prior to rendering to setup the Data pointer. Plus, corresponding functions to unmap texures and renderbuffers. This is very much like similar code in the dri drivers. --- src/mesa/drivers/dri/intel/intel_tex.c | 10 +- src/mesa/drivers/dri/radeon/radeon_texture.c | 14 +- src/mesa/swrast/s_context.c | 24 +++ src/mesa/swrast/s_context.h | 28 ++++ src/mesa/swrast/s_texrender.c | 31 ++-- src/mesa/swrast/s_texture.c | 151 ++++++++++++++++++- src/mesa/tnl/t_context.h | 1 - src/mesa/tnl/t_vb_program.c | 2 + 8 files changed, 225 insertions(+), 36 deletions(-) diff --git a/src/mesa/drivers/dri/intel/intel_tex.c b/src/mesa/drivers/dri/intel/intel_tex.c index 0e82e136e5a..58066598beb 100644 --- a/src/mesa/drivers/dri/intel/intel_tex.c +++ b/src/mesa/drivers/dri/intel/intel_tex.c @@ -134,9 +134,9 @@ intel_free_texture_image_buffer(struct gl_context * ctx, intel_miptree_release(&intelImage->mt); - if (intelImage->base.Data) { - _mesa_align_free(intelImage->base.Data); - intelImage->base.Data = NULL; + if (intelImage->base.Buffer) { + _mesa_align_free(intelImage->base.Buffer); + intelImage->base.Buffer = NULL; } if (intelImage->base.ImageOffsets) { @@ -214,11 +214,11 @@ intel_map_texture_image(struct gl_context *ctx, assert(map); *stride = _mesa_format_row_stride(tex_image->TexFormat, width); - *map = intel_image->base.Data + (slice * height + y) * *stride + x * texelSize; + *map = intel_image->base.Buffer + (slice * height + y) * *stride + x * texelSize; DBG("%s: %d,%d %dx%d from data %p = %p/%d\n", __FUNCTION__, x, y, w, h, - intel_image->base.Data, *map, *stride); + intel_image->base.Buffer, *map, *stride); } } diff --git a/src/mesa/drivers/dri/radeon/radeon_texture.c b/src/mesa/drivers/dri/radeon/radeon_texture.c index abe751053e6..4f8daa72e84 100644 --- a/src/mesa/drivers/dri/radeon/radeon_texture.c +++ b/src/mesa/drivers/dri/radeon/radeon_texture.c @@ -104,7 +104,7 @@ void radeonFreeTextureImageBuffer(struct gl_context *ctx, struct gl_texture_imag if (image->mt) { radeon_miptree_unreference(&image->mt); - assert(!image->base.Data); + assert(!image->base.Buffer); } else { _swrast_free_texture_image_buffer(ctx, timage); } @@ -112,9 +112,9 @@ void radeonFreeTextureImageBuffer(struct gl_context *ctx, struct gl_texture_imag radeon_bo_unref(image->bo); image->bo = NULL; } - if (image->base.Data) { - _mesa_align_free(image->base.Data); - image->base.Data = NULL; + if (image->base.Buffer) { + _mesa_align_free(image->base.Buffer); + image->base.Buffer = NULL; } if (image->base.ImageOffsets) { @@ -314,7 +314,7 @@ radeon_map_texture_image(struct gl_context *ctx, assert(map); *stride = _mesa_format_row_stride(texImage->TexFormat, width); - *map = image->base.Data + (slice * height) * *stride; + *map = image->base.Buffer + (slice * height) * *stride; } *map += y * *stride + x * texel_size; @@ -828,12 +828,12 @@ static void radeon_teximage( texImage->Width, texImage->Height, texImage->Depth); - image->base.Data = _mesa_align_malloc(size, 512); + image->base.Buffer = _mesa_align_malloc(size, 512); radeon_print(RADEON_TEXTURE, RADEON_VERBOSE, "%s %dd: texObj %p, texImage %p, " " no miptree assigned, using local memory %p\n", - __func__, dims, texObj, texImage, image->base.Data); + __func__, dims, texObj, texImage, image->base.Buffer); } } diff --git a/src/mesa/swrast/s_context.c b/src/mesa/swrast/s_context.c index 5287671d780..9112cf30d81 100644 --- a/src/mesa/swrast/s_context.c +++ b/src/mesa/swrast/s_context.c @@ -747,6 +747,12 @@ _swrast_CreateContext( struct gl_context *ctx ) swrast->AllowVertexFog = GL_TRUE; swrast->AllowPixelFog = GL_TRUE; + swrast->Driver.SpanRenderStart = _swrast_span_render_start; + swrast->Driver.SpanRenderFinish = _swrast_span_render_finish; + + ctx->Driver.MapTexture = _swrast_map_texture; + ctx->Driver.UnmapTexture = _swrast_unmap_texture; + /* Optimized Accum buffer */ swrast->_IntegerAccumMode = GL_FALSE; swrast->_IntegerAccumScaler = 0.0; @@ -837,6 +843,24 @@ _swrast_render_primitive( struct gl_context *ctx, GLenum prim ) } +/** called via swrast->Driver.SpanRenderStart() */ +void +_swrast_span_render_start(struct gl_context *ctx) +{ + _swrast_map_textures(ctx); + _swrast_map_renderbuffers(ctx); +} + + +/** called via swrast->Driver.SpanRenderFinish() */ +void +_swrast_span_render_finish(struct gl_context *ctx) +{ + _swrast_unmap_textures(ctx); + _swrast_unmap_renderbuffers(ctx); +} + + void _swrast_render_start( struct gl_context *ctx ) { diff --git a/src/mesa/swrast/s_context.h b/src/mesa/swrast/s_context.h index 55f7c31ce51..a83ac614560 100644 --- a/src/mesa/swrast/s_context.h +++ b/src/mesa/swrast/s_context.h @@ -144,6 +144,9 @@ struct swrast_texture_image each 2D slice in 'Data', in texels */ GLubyte *Data; /**< Image data, accessed via FetchTexel() */ + /** Malloc'd texure memory */ + GLubyte *Buffer; + FetchTexelFunc FetchTexel; StoreTexelFunc Store; }; @@ -333,6 +336,31 @@ swrast_render_finish(struct gl_context *ctx) } +extern void +_swrast_span_render_start(struct gl_context *ctx); + +extern void +_swrast_span_render_finish(struct gl_context *ctx); + +extern void +_swrast_map_textures(struct gl_context *ctx); + +extern void +_swrast_unmap_textures(struct gl_context *ctx); + +extern void +_swrast_map_texture(struct gl_context *ctx, struct gl_texture_object *texObj); + +extern void +_swrast_unmap_texture(struct gl_context *ctx, struct gl_texture_object *texObj); + + +extern void +_swrast_map_renderbuffers(struct gl_context *ctx); + +extern void +_swrast_unmap_renderbuffers(struct gl_context *ctx); + /** * Size of an RGBA pixel, in bytes, for given datatype. diff --git a/src/mesa/swrast/s_texrender.c b/src/mesa/swrast/s_texrender.c index caa17f983e3..e2b921512f2 100644 --- a/src/mesa/swrast/s_texrender.c +++ b/src/mesa/swrast/s_texrender.c @@ -31,6 +31,15 @@ struct texture_renderbuffer }; +/** cast wrapper */ +static inline struct texture_renderbuffer * +texture_renderbuffer(struct gl_renderbuffer *rb) +{ + return (struct texture_renderbuffer *) rb; +} + + + /** * Get row of values from the renderbuffer that wraps a texture image. */ @@ -38,8 +47,7 @@ static void texture_get_row(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count, GLint x, GLint y, void *values) { - const struct texture_renderbuffer *trb - = (const struct texture_renderbuffer *) rb; + struct texture_renderbuffer *trb = texture_renderbuffer(rb); const GLint z = trb->Zoffset; GLuint i; @@ -107,8 +115,7 @@ static void texture_get_values(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count, const GLint x[], const GLint y[], void *values) { - const struct texture_renderbuffer *trb - = (const struct texture_renderbuffer *) rb; + struct texture_renderbuffer *trb = texture_renderbuffer(rb); const GLint z = trb->Zoffset; GLuint i; @@ -174,8 +181,7 @@ static void texture_put_row(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count, GLint x, GLint y, const void *values, const GLubyte *mask) { - const struct texture_renderbuffer *trb - = (const struct texture_renderbuffer *) rb; + struct texture_renderbuffer *trb = texture_renderbuffer(rb); const GLint z = trb->Zoffset; GLuint i; @@ -236,8 +242,7 @@ static void texture_put_row_rgb(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count, GLint x, GLint y, const void *values, const GLubyte *mask) { - const struct texture_renderbuffer *trb - = (const struct texture_renderbuffer *) rb; + struct texture_renderbuffer *trb = texture_renderbuffer(rb); const GLint z = trb->Zoffset; GLuint i; @@ -296,8 +301,7 @@ static void texture_put_mono_row(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count, GLint x, GLint y, const void *value, const GLubyte *mask) { - const struct texture_renderbuffer *trb - = (const struct texture_renderbuffer *) rb; + struct texture_renderbuffer *trb = texture_renderbuffer(rb); const GLint z = trb->Zoffset; GLuint i; @@ -356,8 +360,7 @@ texture_put_values(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint co const GLint x[], const GLint y[], const void *values, const GLubyte *mask) { - const struct texture_renderbuffer *trb - = (const struct texture_renderbuffer *) rb; + struct texture_renderbuffer *trb = texture_renderbuffer(rb); const GLint z = trb->Zoffset; GLuint i; @@ -415,8 +418,7 @@ texture_put_mono_values(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count, const GLint x[], const GLint y[], const void *value, const GLubyte *mask) { - const struct texture_renderbuffer *trb - = (const struct texture_renderbuffer *) rb; + struct texture_renderbuffer *trb = texture_renderbuffer(rb); const GLint z = trb->Zoffset; GLuint i; @@ -610,7 +612,6 @@ update_wrapper(struct gl_context *ctx, struct gl_renderbuffer_attachment *att) trb->Base.DataType = CHAN_TYPE; trb->Base._BaseFormat = GL_RGBA; } - trb->Base.Data = trb->TexImage->Data; } diff --git a/src/mesa/swrast/s_texture.c b/src/mesa/swrast/s_texture.c index df371c30e3c..80c3070c941 100644 --- a/src/mesa/swrast/s_texture.c +++ b/src/mesa/swrast/s_texture.c @@ -76,9 +76,9 @@ _swrast_alloc_texture_image_buffer(struct gl_context *ctx, assert(texImage->Height == height); assert(texImage->Depth == depth); - assert(!swImg->Data); - swImg->Data = _mesa_align_malloc(bytes, 512); - if (!swImg->Data) + assert(!swImg->Buffer); + swImg->Buffer = _mesa_align_malloc(bytes, 512); + if (!swImg->Buffer) return GL_FALSE; /* RowStride and ImageOffsets[] describe how to address texels in 'Data' */ @@ -128,9 +128,9 @@ _swrast_free_texture_image_buffer(struct gl_context *ctx, struct gl_texture_image *texImage) { struct swrast_texture_image *swImage = swrast_texture_image(texImage); - if (swImage->Data) { - _mesa_align_free(swImage->Data); - swImage->Data = NULL; + if (swImage->Buffer) { + _mesa_align_free(swImage->Buffer); + swImage->Buffer = NULL; } if (swImage->ImageOffsets) { @@ -189,9 +189,9 @@ _swrast_map_teximage(struct gl_context *ctx, stride = _mesa_format_row_stride(texImage->TexFormat, texImage->Width); _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh); - assert(swImage->Data); + assert(swImage->Buffer); - map = swImage->Data; + map = swImage->Buffer; if (texImage->TexObject->Target == GL_TEXTURE_3D || texImage->TexObject->Target == GL_TEXTURE_2D_ARRAY) { @@ -224,3 +224,138 @@ _swrast_unmap_teximage(struct gl_context *ctx, { /* nop */ } + + +void +_swrast_map_texture(struct gl_context *ctx, struct gl_texture_object *texObj) +{ + const GLuint faces = texObj->Target == GL_TEXTURE_CUBE_MAP ? 6 : 1; + GLuint face, level; + + for (face = 0; face < faces; face++) { + for (level = texObj->BaseLevel; level < MAX_TEXTURE_LEVELS; level++) { + struct gl_texture_image *texImage = texObj->Image[face][level]; + if (texImage) { + struct swrast_texture_image *swImage = + swrast_texture_image(texImage); + + /* XXX we'll eventually call _swrast_map_teximage() here */ + swImage->Data = swImage->Buffer; + assert(swImage->Buffer); + } + } + } +} + + +void +_swrast_unmap_texture(struct gl_context *ctx, struct gl_texture_object *texObj) +{ + const GLuint faces = texObj->Target == GL_TEXTURE_CUBE_MAP ? 6 : 1; + GLuint face, level; + + for (face = 0; face < faces; face++) { + for (level = texObj->BaseLevel; level < MAX_TEXTURE_LEVELS; level++) { + struct gl_texture_image *texImage = texObj->Image[face][level]; + if (texImage) { + struct swrast_texture_image *swImage + = swrast_texture_image(texImage); + + /* XXX we'll eventually call _swrast_unmap_teximage() here */ + swImage->Data = NULL; + } + } + } +} + + +/** + * Map all textures for reading prior to software rendering. + */ +void +_swrast_map_textures(struct gl_context *ctx) +{ + GLbitfield enabledUnits = ctx->Texture._EnabledUnits; + + /* loop over enabled texture units */ + while (enabledUnits) { + GLuint unit = ffs(enabledUnits) - 1; + struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current; + + _swrast_map_texture(ctx, texObj); + + enabledUnits &= ~(1 << unit); + } +} + + +/** + * Unmap all textures for reading prior to software rendering. + */ +void +_swrast_unmap_textures(struct gl_context *ctx) +{ + GLbitfield enabledUnits = ctx->Texture._EnabledUnits; + + /* loop over enabled texture units */ + while (enabledUnits) { + GLuint unit = ffs(enabledUnits) - 1; + struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current; + + _swrast_unmap_texture(ctx, texObj); + + enabledUnits &= ~(1 << unit); + } +} + + +/** + * Map or unmap any textures that we may be rendering to as renderbuffers. + */ +static void +map_unmap_renderbuffers(struct gl_context *ctx, + struct gl_framebuffer *fb, + GLboolean map) +{ + GLuint i; + + for (i = 0; i < Elements(fb->Attachment); i++) { + struct gl_texture_object *texObj = fb->Attachment[i].Texture; + if (texObj) { + const GLuint level = fb->Attachment[i].TextureLevel; + const GLuint face = fb->Attachment[i].CubeMapFace; + struct gl_texture_image *texImage = texObj->Image[face][level]; + if (texImage) { + struct swrast_texture_image *swImage + = swrast_texture_image(texImage); + + if (map) { + /* XXX we'll eventually call _swrast_map_teximage() here */ + swImage->Data = swImage->Buffer; + } + else { + /* XXX we'll eventually call _swrast_unmap_teximage() here */ + swImage->Data = NULL; + } + } + } + } +} + + +void +_swrast_map_renderbuffers(struct gl_context *ctx) +{ + map_unmap_renderbuffers(ctx, ctx->DrawBuffer, GL_TRUE); + if (ctx->ReadBuffer != ctx->DrawBuffer) + map_unmap_renderbuffers(ctx, ctx->ReadBuffer, GL_TRUE); +} + + +void +_swrast_unmap_renderbuffers(struct gl_context *ctx) +{ + map_unmap_renderbuffers(ctx, ctx->DrawBuffer, GL_FALSE); + if (ctx->ReadBuffer != ctx->DrawBuffer) + map_unmap_renderbuffers(ctx, ctx->ReadBuffer, GL_FALSE); +} diff --git a/src/mesa/tnl/t_context.h b/src/mesa/tnl/t_context.h index 6a9444216c0..b3fce81b459 100644 --- a/src/mesa/tnl/t_context.h +++ b/src/mesa/tnl/t_context.h @@ -528,7 +528,6 @@ typedef struct GLuint nr_blocks; GLuint CurInstance; - } TNLcontext; diff --git a/src/mesa/tnl/t_vb_program.c b/src/mesa/tnl/t_vb_program.c index 367dfd5bb23..836e8e8fae6 100644 --- a/src/mesa/tnl/t_vb_program.c +++ b/src/mesa/tnl/t_vb_program.c @@ -68,6 +68,8 @@ struct vp_stage_data { GLubyte *clipmask; /**< clip flags */ GLubyte ormask, andmask; /**< for clipping */ + GLboolean vertex_textures; + struct gl_program_machine machine; }; -- 2.30.2