autodelete.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#pragma once


#include <planet/vk/engine/renderer.hpp>

#include <felspar/coro/eager.hpp>
#include <felspar/memory/any_buffer.hpp>


namespace planet::vk::engine {

Auto-delete resources after waiting a full frame cycle

14
15
16
    template<typename C = std::vector<felspar::memory::any_buffer<512>>>
    class autodelete final {
        using collection_type = C;

Hold management

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        engine::renderer &renderer;
        std::array<collection_type, max_frames_in_flight> holds;
        collection_type new_holds;


      public:
        autodelete(engine::renderer &r)
        : renderer{r}, deleter{delete_holds()} {}

        autodelete(autodelete const &) = delete;
        autodelete(autodelete &&ad)
        : renderer{ad.renderer}, deleter{delete_holds()} {
            assert_empty(ad);
            ad.deleter.destroy();
        }

        autodelete &operator=(autodelete const &) = delete;
        autodelete &operator=(autodelete &&) = delete;

Autodelete an asset

 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
        template<typename W>
        void manage(W w)
            requires requires { new_holds.emplace_back(std::move(w)); }
        {
            new_holds.emplace_back(std::move(w));
        }
        template<typename W>
        void manage(W w)
            requires requires { new_holds.emplace(std::move(w)); }
        {
            new_holds.emplace(std::move(w));
        }
        template<typename W>
        void manage(W &o, W w) {
            manage(std::exchange(o, std::move(w)));
        }


      private:
        felspar::coro::eager<>::task_type delete_holds() {
            while (true) {
                auto const frame_index =
                        co_await renderer.next_frame_prestart();
                std::swap(holds[frame_index], new_holds);
                reset_new_hold();
            }
        }
        felspar::coro::eager<> deleter;


        void reset_new_hold()
            requires requires { new_holds.clear(); }
        {
            new_holds.clear();
        }
        void reset_new_hold()
            requires requires { new_holds.reset(); }
        {
            new_holds.reset();
        }

        static void assert_empty(autodelete &ad)
            requires requires { ad.new_holds.empty(); }
        {
            if (not ad.new_holds.empty() or not ad.holds[0].empty()
                or not ad.holds[1].empty() or not ad.holds[2].empty()) {
                throw felspar::stdexcept::logic_error{
                        "The autodeleter being moved from must be empty to be "
                        "moved"};
            }
        }
        static void assert_empty(autodelete &ad)
            requires requires { ad.new_holds.has_value(); }
        {
            if (ad.new_holds.has_value() or ad.holds[0].has_value()
                or ad.holds[1].has_value() or ad.holds[2].has_value()) {
                throw felspar::stdexcept::logic_error{
                        "The autodeleter being moved from must be empty to be "
                        "moved"};
            }
        }
    };


}