KiCad PCB EDA Suite
Loading...
Searching...
No Matches
rhi_deferred_release.h
Go to the documentation of this file.
1
/*
2
* This program source code file is part of KiCad, a free EDA CAD application.
3
*
4
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5
*
6
* This program is free software: you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License as published by the
8
* Free Software Foundation, either version 3 of the License, or (at your
9
* option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License along
17
* with this program. If not, see <http://www.gnu.org/licenses/>.
18
*/
19
20
#pragma once
21
22
#include <algorithm>
23
#include <cstdint>
24
#include <mutex>
25
#include <vector>
26
27
constexpr
uint64_t
RHI_FRAMES_IN_FLIGHT
= 2;
28
29
enum class
RHI_DEFERRED_RESOURCE_TYPE
: uint8_t
30
{
31
BUFFER
,
32
TEXTURE
,
33
SHADER
,
34
PIPELINE
,
35
BIND_GROUP_LAYOUT
,
36
BIND_GROUP
,
37
RENDER_PASS
,
38
FRAMEBUFFER
,
39
};
40
41
42
struct
RHI_DEFERRED_RELEASE_ENTRY
43
{
44
RHI_DEFERRED_RESOURCE_TYPE
type
;
45
uint32_t
handleValue
;
46
uint64_t
retireFrame
;
47
};
48
49
56
class
RHI_DEFERRED_RELEASE_QUEUE
57
{
58
public
:
59
void
Enqueue
(
RHI_DEFERRED_RESOURCE_TYPE
aType, uint32_t aHandleValue,
60
uint64_t aCurrentFrame )
61
{
62
std::lock_guard<std::mutex> lock(
m_mutex
);
63
m_entries
.push_back( { aType, aHandleValue, aCurrentFrame +
RHI_FRAMES_IN_FLIGHT
} );
64
}
65
66
template
<
typename
Func>
67
void
ProcessRetired
( uint64_t aCompletedFrame, Func&& aFreeFunc )
68
{
69
std::vector<RHI_DEFERRED_RELEASE_ENTRY> retired;
70
71
{
72
std::lock_guard<std::mutex> lock(
m_mutex
);
73
74
auto
firstRetired = std::stable_partition(
m_entries
.begin(),
m_entries
.end(),
75
[&](
const
RHI_DEFERRED_RELEASE_ENTRY
& aEntry )
76
{
77
return aEntry.retireFrame > aCompletedFrame;
78
} );
79
80
retired.assign( firstRetired,
m_entries
.end() );
81
m_entries
.erase( firstRetired,
m_entries
.end() );
82
}
83
84
for
(
auto
& entry : retired )
85
aFreeFunc( entry );
86
}
87
89
bool
HasRetired
( uint64_t aCompletedFrame )
const
90
{
91
std::lock_guard<std::mutex> lock(
m_mutex
);
92
93
return
std::ranges::any_of(
m_entries
,
94
[&](
const
RHI_DEFERRED_RELEASE_ENTRY
& aEntry )
95
{
96
return
aEntry.
retireFrame
<= aCompletedFrame;
97
} );
98
}
99
100
size_t
PendingCount
()
const
101
{
102
std::lock_guard<std::mutex> lock(
m_mutex
);
103
return
m_entries
.size();
104
}
105
106
template
<
typename
Func>
107
void
FlushAll
( Func&& aFreeFunc )
108
{
109
std::vector<RHI_DEFERRED_RELEASE_ENTRY> retired;
110
111
{
112
std::lock_guard<std::mutex> lock(
m_mutex
);
113
retired.swap(
m_entries
);
114
}
115
116
for
(
auto
& entry : retired )
117
aFreeFunc( entry );
118
}
119
120
private
:
121
mutable
std::mutex
m_mutex
;
122
std::vector<RHI_DEFERRED_RELEASE_ENTRY>
m_entries
;
123
};
RHI_DEFERRED_RELEASE_QUEUE
Holds destroyed GPU resource handles until no in-flight command buffer can still reference them.
Definition
rhi_deferred_release.h:57
RHI_DEFERRED_RELEASE_QUEUE::Enqueue
void Enqueue(RHI_DEFERRED_RESOURCE_TYPE aType, uint32_t aHandleValue, uint64_t aCurrentFrame)
Definition
rhi_deferred_release.h:59
RHI_DEFERRED_RELEASE_QUEUE::m_mutex
std::mutex m_mutex
Definition
rhi_deferred_release.h:121
RHI_DEFERRED_RELEASE_QUEUE::PendingCount
size_t PendingCount() const
Definition
rhi_deferred_release.h:100
RHI_DEFERRED_RELEASE_QUEUE::HasRetired
bool HasRetired(uint64_t aCompletedFrame) const
Definition
rhi_deferred_release.h:89
RHI_DEFERRED_RELEASE_QUEUE::FlushAll
void FlushAll(Func &&aFreeFunc)
Definition
rhi_deferred_release.h:107
RHI_DEFERRED_RELEASE_QUEUE::m_entries
std::vector< RHI_DEFERRED_RELEASE_ENTRY > m_entries
Definition
rhi_deferred_release.h:122
RHI_DEFERRED_RELEASE_QUEUE::ProcessRetired
void ProcessRetired(uint64_t aCompletedFrame, Func &&aFreeFunc)
Definition
rhi_deferred_release.h:67
RHI_FRAMES_IN_FLIGHT
constexpr uint64_t RHI_FRAMES_IN_FLIGHT
Definition
rhi_deferred_release.h:27
RHI_DEFERRED_RESOURCE_TYPE
RHI_DEFERRED_RESOURCE_TYPE
Definition
rhi_deferred_release.h:30
RHI_DEFERRED_RESOURCE_TYPE::BUFFER
@ BUFFER
Definition
rhi_deferred_release.h:31
RHI_DEFERRED_RESOURCE_TYPE::TEXTURE
@ TEXTURE
Definition
rhi_deferred_release.h:32
RHI_DEFERRED_RESOURCE_TYPE::BIND_GROUP
@ BIND_GROUP
Definition
rhi_deferred_release.h:36
RHI_DEFERRED_RESOURCE_TYPE::RENDER_PASS
@ RENDER_PASS
Definition
rhi_deferred_release.h:37
RHI_DEFERRED_RESOURCE_TYPE::BIND_GROUP_LAYOUT
@ BIND_GROUP_LAYOUT
Definition
rhi_deferred_release.h:35
RHI_DEFERRED_RESOURCE_TYPE::SHADER
@ SHADER
Definition
rhi_deferred_release.h:33
RHI_DEFERRED_RESOURCE_TYPE::FRAMEBUFFER
@ FRAMEBUFFER
Definition
rhi_deferred_release.h:38
RHI_DEFERRED_RESOURCE_TYPE::PIPELINE
@ PIPELINE
Definition
rhi_deferred_release.h:34
RHI_DEFERRED_RELEASE_ENTRY
Definition
rhi_deferred_release.h:43
RHI_DEFERRED_RELEASE_ENTRY::retireFrame
uint64_t retireFrame
Definition
rhi_deferred_release.h:46
RHI_DEFERRED_RELEASE_ENTRY::type
RHI_DEFERRED_RESOURCE_TYPE type
Definition
rhi_deferred_release.h:44
RHI_DEFERRED_RELEASE_ENTRY::handleValue
uint32_t handleValue
generation and index
Definition
rhi_deferred_release.h:45
src
include
rhi
rhi_deferred_release.h
Generated on Thu Oct 22 2026 00:06:41 for KiCad PCB EDA Suite by
1.13.2