Unravel Engine C++ Reference
Loading...
Searching...
No Matches
handle_impl.h
Go to the documentation of this file.
1#pragma once
2
3#include "eviction.h"
4#include "graphics.h"
5#include <functional>
6#include <memory>
7#include <hpp/string_view.hpp>
8
9namespace gfx
10{
11
12template<typename Base, typename T>
13class handle_impl : public ievictable
14{
15public:
16 using ptr = std::shared_ptr<Base>;
17 using uptr = std::unique_ptr<Base>;
18 using weak_ptr = std::weak_ptr<Base>;
19
20 using handle_type_t = T;
21 using base_type = Base;
22
23 handle_impl() = default;
24 handle_impl(const handle_impl&) = delete;
25 auto operator=(const handle_impl&) -> handle_impl& = delete;
27 auto operator=(handle_impl&&) -> handle_impl& = delete;
28
29 ~handle_impl() override
30 {
32 {
34 }
35 dispose();
36 }
37
38 void dispose()
39 {
40 if(is_valid())
41 {
43 }
44
46 }
47
48 [[nodiscard]] auto is_valid() const -> bool
49 {
50 return bgfx::isValid(handle_);
51 }
52
53 auto native_handle() const -> T
54 {
56 {
57 eviction::restore_resource(const_cast<handle_impl*>(this));
58 }
59 touch();
60
61 if(!is_valid())
62 {
63 return get_fallback_handle();
64 }
65 return handle_;
66 }
67
68 static auto invalid_handle() -> T
69 {
70 T invalid = {bgfx::kInvalidHandle};
71 return invalid;
72 }
73
74 void set_name(const hpp::string_view& _name)
75 {
76 gfx::set_name(handle_, _name.data(), static_cast<int32_t>(_name.size()));
77 }
78
79 auto on_evict() -> std::uint64_t override
80 {
82 {
83 return 0;
84 }
85 const std::uint64_t freed = gpu_size_;
86 if(is_valid())
87 {
89 }
92 return freed;
93 }
94
95 auto on_restore() -> bool override
96 {
98 {
99 return true;
100 }
101 const bool ok = restore_fn_ && restore_fn_(static_cast<Base&>(*this));
103 return ok;
104 }
105
106 [[nodiscard]] auto gpu_size() const -> std::uint64_t override
107 {
108 return gpu_size_;
109 }
110
111protected:
112 auto fallback_handle() const -> T
113 {
114 return handle_;
115 }
116
117 auto get_fallback_handle() const -> T
118 {
119 return static_cast<const Base&>(*this).fallback_handle();
120 }
125 void make_evictable(std::uint64_t gpu_bytes,
126 std::function<bool(Base&)> restore_fn,
128 {
130 {
131 return;
132 }
133 gpu_size_ = gpu_bytes;
134 restore_fn_ = std::move(restore_fn);
135 evict_class_ = cls;
137 touch();
138 }
139
142 void make_evictable_deferred(std::uint64_t gpu_bytes,
143 std::function<bool(Base&)> restore_fn,
145 {
147 {
148 return;
149 }
150 gpu_size_ = gpu_bytes;
151 restore_fn_ = std::move(restore_fn);
152 evict_class_ = cls;
155 }
156
158 std::function<bool(Base&)> restore_fn_;
159 std::uint64_t gpu_size_ = 0;
160};
161} // namespace gfx
std::unique_ptr< Base > uptr
Definition handle_impl.h:17
std::function< bool(Base &)> restore_fn_
auto on_evict() -> std::uint64_t override
Release the GPU handle. Returns the number of GPU bytes freed. Must be idempotent.
Definition handle_impl.h:79
std::uint64_t gpu_size_
auto is_valid() const -> bool
Definition handle_impl.h:48
auto operator=(handle_impl &&) -> handle_impl &=delete
handle_impl()=default
void set_name(const hpp::string_view &_name)
Definition handle_impl.h:74
static auto invalid_handle() -> T
Definition handle_impl.h:68
auto fallback_handle() const -> T
auto gpu_size() const -> std::uint64_t override
Approximate GPU footprint of the resource in bytes.
std::shared_ptr< Base > ptr
Definition handle_impl.h:16
auto native_handle() const -> T
Definition handle_impl.h:53
void make_evictable(std::uint64_t gpu_bytes, std::function< bool(Base &)> restore_fn, evict_class cls=evict_class::evictable)
auto get_fallback_handle() const -> T
auto operator=(const handle_impl &) -> handle_impl &=delete
~handle_impl() override
Definition handle_impl.h:29
handle_impl(const handle_impl &)=delete
void make_evictable_deferred(std::uint64_t gpu_bytes, std::function< bool(Base &)> restore_fn, evict_class cls=evict_class::evictable)
handle_impl(handle_impl &&)=delete
auto on_restore() -> bool override
Recreate the GPU handle from the CPU backing. Returns true on success.
Definition handle_impl.h:95
std::weak_ptr< Base > weak_ptr
Definition handle_impl.h:18
evict_class evict_class_
Eligibility class. Set once when the resource opts into eviction.
Definition evictable.h:94
void touch() const noexcept
Definition evictable.h:80
evict_state evict_state_
Current residency state. Written by the owning resource during evict/restore.
Definition evictable.h:92
auto is_supported() -> bool
Definition eviction.cpp:650
void restore_resource(ievictable *resource)
Definition eviction.cpp:987
void register_resource(ievictable *resource)
Begin tracking a resource (assumed resident). Called from handle_impl::make_evictable.
Definition eviction.cpp:972
void register_evicted_resource(ievictable *resource)
Definition eviction.cpp:977
void unregister_resource(ievictable *resource)
Stop tracking a resource. Called from handle_impl's destructor.
Definition eviction.cpp:982
evict_class
Eviction eligibility of a tracked resource.
Definition evictable.h:17
@ non_evictable
No CPU backing (render targets, uniforms, ...). Never registered.
@ evictable
May be evicted by a sweep.
void set_name(index_buffer_handle _handle, const char *_name, int32_t _len)
Definition graphics.cpp:500
@ evicted
GPU handle was destroyed; a CPU-side backing is retained for restore.
@ resident
GPU handle is live and usable.
void destroy(index_buffer_handle _handle)
Definition graphics.cpp:505
Hash specialization for batch_key to enable use in std::unordered_map.