FCT
载入中...
搜索中...
未找到
ResourceManager.cpp
浏览该文件的文档.
1//
2// Created by Administrator on 2025/7/28.
3//
4#include "ResourceManager.h"
5
6#include "MutilBufferImage.h"
7#include "SingleBufferImage.h"
8
9#include "./Context.h"
10#include "../Base/TokenGraph.h"
11#include "../RHI/Swapcain.h"
12#include "../UI/Window.h"
13
14namespace FCT {
15 std::string WindowToToken(Window* wnd)
16 {
17 return SwapchainToToken(wnd->swapchain());
18 }
19
20 std::string SwapchainToToken(RHI::Swapchain* swapchain)
21 {
22 return "Swapchain_" + std::to_string(reinterpret_cast<uintptr_t>(swapchain));
23 }
24
26 {
27 img = swapchain->image();
28 autoIndex = false;
29 }
30
32 {
33 mutilBuffer = denpendency->mutilBuffer;
34 autoIndex = true;
35 }
36
38 {
40 m_context = ctx;
42 {
43 registerWindow(env.window);
44 });
45 auto& tickerGraph = m_context->submitTickers();
47 [this]()
48 {
49 for (auto image : m_needChangeIndexImages)
50 {
51 auto img = dynamic_cast<MutilBufferImage*>(image.img);
52 img->changeCurrentIndex(m_context->currentLogicFrameIndex());
53 }
54 },
56 {}
57 };
58 for (auto wnd : m_context->getBindWindows())
59 {
60 registerWindow(wnd);
61 }
62 }
63
64 Image* ResourceManager::allocateImage(std::string name, Window* dependency,ImageDesc desc)
65 {
66 return allocateImage(name, WindowToToken(dependency),desc);
67 }
68
69 Image* ResourceManager::allocateImage(std::string name, std::string dependency,ImageDesc desc)
70 {
71 auto saved = m_dependencyGraph[dependency]->value;
72 auto ret = ImageSaved(saved);
73 if (ret.mutilBuffer)
74 {
75 auto savedImg = dynamic_cast<MutilBufferImage*>(saved.img);
76 auto img = m_resourceDevice->createResource<MutilBufferImage>();
77 img->samples(savedImg->samples());
78 img->format(desc.format);
79 img->width(savedImg->width());
80 img->height(savedImg->height());
81 img->imageCount(savedImg->imageCount());
82 img->as(desc.usage);
83 img->create();
84 img->changeCurrentIndex(m_context->currentLogicFrameIndex());
85 ret.img = img;
86 } else
87 {
88 auto savedImg = dynamic_cast<SingleBufferImage*>(saved.img);
89 auto img = m_resourceDevice->createResource<SingleBufferImage>();
90 img->samples(savedImg->samples());
91 img->format(desc.format);
92 img->width(savedImg->width());
93 img->height(savedImg->height());
94 img->as(desc.usage);
95 img->create();
96 ret.img = img;
97 }
98 m_dependencyGraph[name] = {
99 ret,
100 {dependency},
101 {},
102 };
103 updateGraph();
104 return ret.img;
105 }
106
107 void ResourceManager::clearImage(std::string name)
108 {
109 if (m_dependencyGraph.containsNode(name))
110 {
111 {
112 auto node = m_dependencyGraph[name];
113 auto& imageSaved = node->value;
114
115 if (imageSaved.img)
116 {
117 imageSaved.img->release();
118 }
119 }
120
121 m_dependencyGraph.removeNode(name);
122
123 updateGraph();
124 }
125 }
126
128 {
129 m_dependencyGraph.update();
130 m_needChangeIndexImages = m_dependencyGraph.computeOrder([](const ImageSaved& saved)
131 {
132 return saved.mutilBuffer && saved.autoIndex;
133 });
134 }
135
137 {
138 std::string token = WindowToToken(wnd);
139 RHI::Swapchain* swapchain = wnd->swapchain();
140 swapchain->subscribe<SwapchainEvent::Recreate>([this](const SwapchainEvent::Recreate& env)
141 {
142 resizeSub(
143 SwapchainToToken(env.swapchain),
144 env.width,
145 env.height);
146 });
147 m_dependencyGraph[token] = {
148 ImageSaved(swapchain),
149 {},
150 {},
151 };
152 updateGraph();
153 }
154
155 void ResourceManager::resizeSub(std::string token, int width, int height)
156 {
157 m_dependencyGraph.visitBFS(token,[width,height](ImageSaved img)
158 {
159 img.img->resize(width,height);
160 },false);
161 }
162
163 void ResourceManager::resize(std::string token, int width, int height)
164 {
165 m_dependencyGraph.visitBFS(token,[width,height](ImageSaved img)
166 {
167 img.img->resize(width,height);
168 },true);
169 }
170
171
172}
173//todo:
174namespace FCT
175{
176 void ResourceManager::resizeImage(std::string name, int width, int height)
177 {
178
179 }
181 {
182 auto ret = ImageSaved();
183 auto img = m_resourceDevice->createResource<MutilBufferImage>();
184 img->samples(desc.samples);
185 img->format(desc.format);
186 img->width(desc.width);
187 img->height(desc.height);
188 img->imageCount(m_context->maxFrameInFlight());
189 img->as(desc.usage);
190 img->create();
191 img->changeCurrentIndex(m_context->currentLogicFrameIndex());
192 ret.img = img;
193 m_dependencyGraph[name] = {
194 ret,
195 {},
196 {},
197 };
198 updateGraph();
199 return ret.img;
200 }
201}
SubscribeId subscribe(Func &&func)
Samples samples() const
定义 Image.h:65
virtual void resize(uint32_t width, uint32_t height)=0
virtual FCT::Image * image() const =0
void clearImage(std::string name)
void resizeSub(std::string token, int width, int height)
void resize(std::string token, int width, int height)
TokenGraph< std::string, ImageSaved > m_dependencyGraph
void resizeImage(std::string name, int width, int height)
挂起resize一个image
Image * allocateTarget(std::string name, TargetDesc desc)
分配的是独立的Target,例如窗口一类的就算为独立Target
std::vector< ImageSaved > m_needChangeIndexImages
Image * allocateImage(std::string name, Window *dependency, ImageDesc desc)
分配中间Target / 图像
RHI::Swapchain * swapchain() const
constexpr const char * ImageGraph_ChangeIndex
std::string WindowToToken(Window *wnd)
constexpr const char * SwapBufferSubmitTicker
std::string SwapchainToToken(RHI::Swapchain *swapchain)
ImageSaved(RHI::Swapchain *swapchain)