-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathdynamicresourcepool.cpp
More file actions
185 lines (157 loc) · 7.49 KB
/
dynamicresourcepool.cpp
File metadata and controls
185 lines (157 loc) · 7.49 KB
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
32
33
34
35
36
37
38
39
40
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// This file is part of the FidelityFX SDK.
//
// Copyright (C) 2026 Advanced Micro Devices, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "dynamicresourcepool.h"
#include "../misc/assert.h"
#include "../core/framework.h"
#include "texture.h"
#include "buffer.h"
namespace cauldron
{
DynamicResourcePool::DynamicResourcePool()
{
}
DynamicResourcePool::~DynamicResourcePool()
{
// Release all resources
for (auto texEntry : m_Textures)
delete texEntry.second;
m_Textures.clear();
for (auto bufEntry : m_Buffers)
delete bufEntry.second;
m_Buffers.clear();
}
void DynamicResourcePool::OnResolutionChanged(const ResolutionInfo& resInfo)
{
std::lock_guard<std::mutex> lock(m_CriticalSection);
for (auto t : m_ResizableTextures)
t->OnRenderingResolutionResize(resInfo.DisplayWidth, resInfo.DisplayHeight, resInfo.RenderWidth, resInfo.RenderHeight);
for (auto b : m_ResizableBuffers)
b->OnRenderingResolutionResize(resInfo.DisplayWidth, resInfo.DisplayHeight, resInfo.RenderWidth, resInfo.RenderHeight);
}
void DynamicResourcePool::DestroyResource(const GPUResource* pResource)
{
if (pResource->IsBuffer())
{
for (auto bufPairItr = m_Buffers.begin(); bufPairItr != m_Buffers.end(); ++bufPairItr)
{
if (bufPairItr->second == pResource->GetBufferResource())
{
delete bufPairItr->second;
bufPairItr = m_Buffers.erase(bufPairItr);
return;
}
}
}
else
{
for (auto texPairItr = m_Textures.begin(); texPairItr != m_Textures.end(); ++texPairItr)
{
if (texPairItr->second == pResource->GetTextureResource())
{
delete texPairItr->second;
texPairItr = m_Textures.erase(texPairItr);
return;
}
}
}
CauldronWarning(L"Could not find resource %ls for destruction.", pResource->GetName());
}
const Texture* DynamicResourcePool::GetTexture(const wchar_t* name)
{
std::lock_guard<std::mutex> lock(m_CriticalSection);
CauldronAssert(ASSERT_WARNING, std::this_thread::get_id() != GetFramework()->MainThreadID() || !GetFramework()->IsRunning(), L"Performance Warning: Search for texture by name on the main thread while app is running.");
for (auto texEntry : m_Textures)
{
if (texEntry.first == name)
return texEntry.second;
}
return nullptr;
}
const Buffer* DynamicResourcePool::GetBuffer(const wchar_t* name)
{
std::lock_guard<std::mutex> lock(m_CriticalSection);
CauldronAssert(ASSERT_WARNING, std::this_thread::get_id() != GetFramework()->MainThreadID() || !GetFramework()->IsRunning(), L"Performance Warning: Search for buffer by name on the main thread while app is running.");
for (auto bufEntry : m_Buffers)
{
if (bufEntry.first == name)
return bufEntry.second;
}
return nullptr;
}
const Texture* DynamicResourcePool::CreateTexture(const TextureDesc* pDesc, ResourceState initialState, TextureResizeFunction fn)
{
Texture* pTex = Texture::CreateTexture(pDesc, initialState, fn);
std::lock_guard<std::mutex> lock(m_CriticalSection);
#ifdef _DEBUG
// In debug make sure we are not creating resources with the same name (as it could lead to errors later if someone looks it up by name)
for (auto texEntry : m_Textures)
{
if (texEntry.first == pDesc->Name)
CauldronError(L"DynamicResourcePool: Creating multiple textures with the name %ls, this can cause conflicts if searching for textures by name later.", pDesc->Name.c_str());
}
#endif // _DEBUG
m_Textures.emplace_back(pDesc->Name, pTex);
if (fn != nullptr)
m_ResizableTextures.push_back(pTex);
return pTex;
}
const Texture* DynamicResourcePool::CreateRenderTexture(const TextureDesc* pDesc, TextureResizeFunction fn)
{
// Explicitly add depth or render target flag when creating render textures
TextureDesc desc = *pDesc;
if (IsDepth(pDesc->Format))
desc.Flags = static_cast<ResourceFlags>(desc.Flags | ResourceFlags::AllowDepthStencil);
else
desc.Flags = static_cast<ResourceFlags>(desc.Flags | ResourceFlags::AllowRenderTarget);
Texture* pRT = Texture::CreateTexture(pDesc, static_cast<ResourceState>(ResourceState::NonPixelShaderResource | ResourceState::PixelShaderResource), fn);
std::lock_guard<std::mutex> lock(m_CriticalSection);
#ifdef _DEBUG
// In debug make sure we are not creating resources with the same name (as it could lead to errors later if someone looks it up by name)
for (auto texEntry : m_Textures)
{
if (texEntry.first == pDesc->Name)
CauldronError(L"DynamicResourcePool: Creating multiple textures with the name %ls, this can cause conflicts if searching for textures by name later.", pDesc->Name.c_str());
}
#endif // _DEBUG
m_Textures.emplace_back(pDesc->Name, pRT);
if (fn != nullptr)
m_ResizableTextures.push_back(pRT);
return pRT;
}
const Buffer* DynamicResourcePool::CreateBuffer(const BufferDesc* pDesc, ResourceState initialState, BufferResizeFunction fn/*=nullptr*/)
{
Buffer* pBuf = Buffer::CreateBufferResource(pDesc, initialState, fn);
std::lock_guard<std::mutex> lock(m_CriticalSection);
#ifdef _DEBUG
// In debug make sure we are not creating resources with the same name (as it could lead to errors later if someone looks it up by name)
for (auto bufEntry : m_Buffers)
{
if (bufEntry.first == pDesc->Name)
CauldronError(L"DynamicResourcePool: Creating multiple buffers with the name %ls, this can cause conflicts if searching for buffers by name later.", pDesc->Name.c_str());
}
#endif // _DEBUG
m_Buffers.emplace_back(pDesc->Name, pBuf);
if (fn != nullptr)
m_ResizableBuffers.push_back(pBuf);
return pBuf;
}
} // namespace cauldron