From 22347eb0e9e71574136f0d7237155e73b3963e5c Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Mon, 2 Sep 2024 10:55:41 -0400 Subject: [PATCH] hk: lock device-wide scratch access Signed-off-by: Alyssa Rosenzweig Part-of: --- src/asahi/vulkan/hk_cmd_buffer.c | 6 ++---- src/asahi/vulkan/hk_device.c | 2 ++ src/asahi/vulkan/hk_device.h | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/asahi/vulkan/hk_cmd_buffer.c b/src/asahi/vulkan/hk_cmd_buffer.c index c8f1e960c58..4b51c612d6f 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.c +++ b/src/asahi/vulkan/hk_cmd_buffer.c @@ -583,21 +583,19 @@ hk_reserve_scratch(struct hk_cmd_buffer *cmd, struct hk_cs *cs, unsigned preamble_size = (s->b.info.preamble_scratch_size > 0) ? 1 : 0; - /* XXX: need to lock around agx_scratch_alloc... */ /* Note: this uses the hardware stage, not the software stage */ + hk_device_alloc_scratch(dev, s->b.info.stage, max_scratch_size); + switch (s->b.info.stage) { case PIPE_SHADER_FRAGMENT: - agx_scratch_alloc(&dev->scratch.fs, max_scratch_size, 0); cs->scratch.fs.main = true; cs->scratch.fs.preamble = MAX2(cs->scratch.fs.preamble, preamble_size); break; case PIPE_SHADER_VERTEX: - agx_scratch_alloc(&dev->scratch.vs, max_scratch_size, 0); cs->scratch.vs.main = true; cs->scratch.vs.preamble = MAX2(cs->scratch.vs.preamble, preamble_size); break; default: - agx_scratch_alloc(&dev->scratch.cs, max_scratch_size, 0); cs->scratch.cs.main = true; cs->scratch.cs.preamble = MAX2(cs->scratch.cs.preamble, preamble_size); break; diff --git a/src/asahi/vulkan/hk_device.c b/src/asahi/vulkan/hk_device.c index 2195d485da0..8d8493b52f8 100644 --- a/src/asahi/vulkan/hk_device.c +++ b/src/asahi/vulkan/hk_device.c @@ -421,6 +421,7 @@ hk_CreateDevice(VkPhysicalDevice physicalDevice, *pDevice = hk_device_to_handle(dev); + simple_mtx_init(&dev->scratch.lock, mtx_plain); agx_scratch_init(&dev->dev, &dev->scratch.vs); agx_scratch_init(&dev->dev, &dev->scratch.fs); agx_scratch_init(&dev->dev, &dev->scratch.cs); @@ -475,6 +476,7 @@ hk_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator) agx_scratch_fini(&dev->scratch.vs); agx_scratch_fini(&dev->scratch.fs); agx_scratch_fini(&dev->scratch.cs); + simple_mtx_destroy(&dev->scratch.lock); hk_destroy_sampler_heap(dev, &dev->samplers); hk_descriptor_table_finish(dev, &dev->images); diff --git a/src/asahi/vulkan/hk_device.h b/src/asahi/vulkan/hk_device.h index b6c57315390..a53bbb80b73 100644 --- a/src/asahi/vulkan/hk_device.h +++ b/src/asahi/vulkan/hk_device.h @@ -8,6 +8,7 @@ #pragma once #include "asahi/lib/agx_device.h" +#include "util/simple_mtx.h" #include "agx_bg_eot.h" #include "agx_pack.h" #include "agx_scratch.h" @@ -102,6 +103,7 @@ struct hk_device { struct { struct agx_scratch vs, fs, cs; + simple_mtx_t lock; } scratch; }; @@ -121,3 +123,27 @@ VkResult hk_sampler_heap_add(struct hk_device *dev, struct hk_rc_sampler **out); void hk_sampler_heap_remove(struct hk_device *dev, struct hk_rc_sampler *rc); + +static inline struct agx_scratch * +hk_device_scratch_locked(struct hk_device *dev, enum pipe_shader_type stage) +{ + simple_mtx_assert_locked(&dev->scratch.lock); + + switch (stage) { + case PIPE_SHADER_FRAGMENT: + return &dev->scratch.fs; + case PIPE_SHADER_VERTEX: + return &dev->scratch.vs; + default: + return &dev->scratch.cs; + } +} + +static inline void +hk_device_alloc_scratch(struct hk_device *dev, enum pipe_shader_type stage, + unsigned size) +{ + simple_mtx_lock(&dev->scratch.lock); + agx_scratch_alloc(hk_device_scratch_locked(dev, stage), size, 0); + simple_mtx_unlock(&dev->scratch.lock); +}