From 356f5b2d2e551aa0b79956770383dbf53addaef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ol=C5=A1=C3=A1k?= Date: Tue, 7 Jan 2025 20:59:35 -0500 Subject: [PATCH] radeonsi: move buffer reallocation to a separate function to be used later Reviewed-by: Pierre-Eric Pelloux-Prayer Part-of: --- src/gallium/drivers/radeonsi/si_buffer.c | 24 +++++++++++++++++++++++ src/gallium/drivers/radeonsi/si_pipe.h | 2 ++ src/gallium/drivers/radeonsi/si_texture.c | 15 ++------------ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/gallium/drivers/radeonsi/si_buffer.c b/src/gallium/drivers/radeonsi/si_buffer.c index 1c05df780ac..adacf4e2735 100644 --- a/src/gallium/drivers/radeonsi/si_buffer.c +++ b/src/gallium/drivers/radeonsi/si_buffer.c @@ -279,7 +279,9 @@ void si_replace_buffer_storage(struct pipe_context *ctx, struct pipe_resource *d radeon_bo_reference(sctx->screen->ws, &sdst->buf, ssrc->buf); sdst->gpu_address = ssrc->gpu_address; + sdst->b.b.usage = ssrc->b.b.usage; sdst->b.b.bind = ssrc->b.b.bind; + sdst->domains = ssrc->domains; sdst->flags = ssrc->flags; assert(sdst->bo_size == ssrc->bo_size); @@ -291,6 +293,28 @@ void si_replace_buffer_storage(struct pipe_context *ctx, struct pipe_resource *d util_idalloc_mt_free(&sctx->screen->buffer_ids, delete_buffer_id); } +bool si_reallocate_buffer_change_flags(struct si_context *sctx, struct pipe_resource *buf, + unsigned usage, unsigned bind) +{ + struct pipe_resource templ = *buf; + templ.usage = usage; + templ.bind = bind; + + struct pipe_resource *new_buf = sctx->b.screen->resource_create(sctx->b.screen, &templ); + if (!new_buf) + return false; + + /* Copy the old buffer contents to the new one. */ + struct pipe_box box; + u_box_1d(0, new_buf->width0, &box); + sctx->b.resource_copy_region(&sctx->b, new_buf, 0, 0, 0, 0, buf, 0, &box); + + /* Move the new buffer storage to the old pipe_resource. */ + si_replace_buffer_storage(&sctx->b, buf, new_buf, 0, 0, 0); + pipe_resource_reference(&new_buf, NULL); + return true; +} + static void si_invalidate_resource(struct pipe_context *ctx, struct pipe_resource *resource) { struct si_context *sctx = (struct si_context *)ctx; diff --git a/src/gallium/drivers/radeonsi/si_pipe.h b/src/gallium/drivers/radeonsi/si_pipe.h index aeceac3bad1..a66a752163e 100644 --- a/src/gallium/drivers/radeonsi/si_pipe.h +++ b/src/gallium/drivers/radeonsi/si_pipe.h @@ -1440,6 +1440,8 @@ struct pipe_resource *si_buffer_from_winsys_buffer(struct pipe_screen *screen, void si_replace_buffer_storage(struct pipe_context *ctx, struct pipe_resource *dst, struct pipe_resource *src, unsigned num_rebinds, uint32_t rebind_mask, uint32_t delete_buffer_id); +bool si_reallocate_buffer_change_flags(struct si_context *sctx, struct pipe_resource *buf, + unsigned usage, unsigned bind); void si_init_screen_buffer_functions(struct si_screen *sscreen); void si_init_buffer_functions(struct si_context *sctx); diff --git a/src/gallium/drivers/radeonsi/si_texture.c b/src/gallium/drivers/radeonsi/si_texture.c index 76982cedc48..25514f21679 100644 --- a/src/gallium/drivers/radeonsi/si_texture.c +++ b/src/gallium/drivers/radeonsi/si_texture.c @@ -874,25 +874,14 @@ static bool si_texture_get_handle(struct pipe_screen *screen, struct pipe_contex assert(!res->b.is_shared); /* Allocate a new buffer with PIPE_BIND_SHARED. */ - struct pipe_resource templ = res->b.b; - templ.bind |= PIPE_BIND_SHARED; - - struct pipe_resource *newb = screen->resource_create(screen, &templ); - if (!newb) { + if (!si_reallocate_buffer_change_flags(sctx, &res->b.b, res->b.b.usage, + res->b.b.bind | PIPE_BIND_SHARED)) { if (!ctx) si_put_aux_context_flush(&sscreen->aux_context.general); return false; } - /* Copy the old buffer contents to the new one. */ - struct pipe_box box; - u_box_1d(0, newb->width0, &box); - sctx->b.resource_copy_region(&sctx->b, newb, 0, 0, 0, 0, &res->b.b, 0, &box); flush = true; - /* Move the new buffer storage to the old pipe_resource. */ - si_replace_buffer_storage(&sctx->b, &res->b.b, newb, 0, 0, 0); - pipe_resource_reference(&newb, NULL); - assert(res->b.b.bind & PIPE_BIND_SHARED); assert(res->flags & RADEON_FLAG_NO_SUBALLOC); }