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
27constexpr uint64_t RHI_FRAMES_IN_FLIGHT = 2;
28
40
41
48
49
57{
58public:
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
120private:
121 mutable std::mutex m_mutex;
122 std::vector<RHI_DEFERRED_RELEASE_ENTRY> m_entries;
123};
Holds destroyed GPU resource handles until no in-flight command buffer can still reference them.
void Enqueue(RHI_DEFERRED_RESOURCE_TYPE aType, uint32_t aHandleValue, uint64_t aCurrentFrame)
bool HasRetired(uint64_t aCompletedFrame) const
void FlushAll(Func &&aFreeFunc)
std::vector< RHI_DEFERRED_RELEASE_ENTRY > m_entries
void ProcessRetired(uint64_t aCompletedFrame, Func &&aFreeFunc)
constexpr uint64_t RHI_FRAMES_IN_FLIGHT
RHI_DEFERRED_RESOURCE_TYPE
uint64_t retireFrame
RHI_DEFERRED_RESOURCE_TYPE type
uint32_t handleValue
generation and index