upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/deferred_image_binding.h
blob: 668cf593db160749c46bb89472ad17e362cf8f26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#pragma once

#include <atomic>
#include <mutex>

namespace leonida {

// A placed image can be named concurrently by command replay and descriptor creation.
// Vulkan permits one successful bind for the lifetime of an ordinary image.
class DeferredImageBinding {
public:
    using Bind = bool (*)(void*);
    DeferredImageBinding(void* owner, Bind bind) : owner_(owner), bind_(bind) {}
    bool bound() const { return bound_.load(std::memory_order_acquire); }
    bool ensure() {
        if (bound()) return true;
        std::lock_guard lock(mutex_);
        if (bound()) return true;
        if (!bind_(owner_)) return false;
        bound_.store(true, std::memory_order_release);
        return true;
    }

private:
    void* owner_;
    Bind bind_;
    std::mutex mutex_;
    std::atomic<bool> bound_{false};
};

}  // namespace leonida