FCT
载入中...
搜索中...
未找到
layout.cpp
浏览该文件的文档.
1#include "layout.h"
2#include "layout.hpp"
3#include "./PixelShader.h"
4#include "./VertexShader.h"
5#include "PassResource.h"
7#include "../RHI/BlendState.h"
10#include "Context.hpp"
11
12namespace FCT
13{
15 {
16 m_ctx = ctx;
17 }
18
19 void Layout::setFixedImage(std::string name, FCT::Image* image)
20 {
21 m_fixedImages[name] = image;
22 }
23
25 {
26 m_resourceLayout.addTexture(element);
28 {
29 m_vertexResourceLayout.addTexture(element);
30 }
32 {
33 m_pixelResourceLayout.addTexture(element);
34 }
35 }
36
37 void Layout::attachPass(FCT::RenderGraph* graph, std::string passName)
38 {
39 auto edges = graph->getTextureEdges(passName);
40 for (auto edge : edges)
41 {
43 addTextureSlot(element);
44 setFixedImage(edge->fromImage,graph->getImage(edge->fromImage));
45 }
46 m_pass = graph->getPass(passName);
47 }
48
50 {
51 return std::move(Uniform(m_ctx,m_uniformLayouts[name]));
52 }
53
55 {
56 auto ret = m_ctx->createResource<FCT::ContextResource::VertexShader>();
57 for (auto& layout : m_vertexLayouts)
58 {
59 ret->addLayout(layout.first,layout.second);
60 }
61 ret->pixelLayout(m_pixelLayout);
62 for (auto& uniform : m_uniformLayouts)
63 {
64 ret->addUniform(uniform.second);
65 }
66 ret->resourceLayout(m_vertexResourceLayout);
67 ret->code(code);
68 ret->create();
69 return ret;
70 }
71
73 {
74 auto ret = m_ctx->createResource<FCT::ContextResource::PixelShader>();
75
76 for (auto& layout : m_uniformLayouts)
77 {
78 ret->addUniform(layout.second);
79 }
80 ret->pixelLayout(m_pixelLayout);
81 ret->resourceLayout(m_pixelResourceLayout);
82 ret->code(code);
83 ret->create();
84 return ret;
85 }
86
88 {
89 m_passResourceState.clear();
90
91 for (const auto& fixedImage : m_fixedImages)
92 {
93 m_passResourceState.bindTexture(fixedImage.first, fixedImage.second);
94 }
95
97 }
98
99 void Layout::bindUniform(const Uniform& uniform)
100 {
101 std::string name = (static_cast<FCT::RHI::ConstBuffer*>(uniform))->layout().getName(); // 你可能需要在 Uniform 类中添加这个方法
102 m_passResourceState.bindUniform(name, uniform);
103 }
104
105 void Layout::bindTexture(std::string name, FCT::Image* image)
106 {
107 m_passResourceState.bindTexture(name, image);
108 }
109
110 void Layout::bindSampler(std::string name, FCT::Sampler* sampler)
111 {
112 m_passResourceState.bindSampler(name, sampler);
113 }
114
116 {
117 m_pipelineState.vertexShader = shader;
118 }
119
121 {
122 m_pipelineState.pixelShader = shader;
123 }
124
125 /*
126 void Layout::drawMesh(FCT::RHI::CommandBuffer* cmdBuffer, FCT::StaticMesh<uint32_t>* mesh)
127 {
128 auto resource = getCurrentPassResource();
129 auto pipeline = getCurrentPipeline();
130
131 pipeline->bind(cmdBuffer);
132 resource->bind(cmdBuffer,pipeline);
133 mesh->bind(cmdBuffer);
134 mesh->draw(cmdBuffer);
135 }*/
136
138 {
139
140 }
141
143 {
144 size_t hash = 0;
145 boost::hash_combine(hash, vertexShader);
146 boost::hash_combine(hash, pixelShader);
147 boost::hash_combine(hash, blendState);
148 boost::hash_combine(hash, rasterizationState);
149 boost::hash_combine(hash, depthStencilState);
150 return hash;
151 }
152
161
163 {
164 boundTextures.clear();
165 boundSamplers.clear();
166 boundUniforms.clear();
167 }
168
169 void Layout::PassResourceState::bindTexture(const std::string& name, FCT::Image* image)
170 {
171 boundTextures[name] = image;
172 }
173
174 void Layout::PassResourceState::bindSampler(const std::string& name, FCT::Sampler* sampler)
175 {
176 boundSamplers[name] = sampler;
177 }
178
179 void Layout::PassResourceState::bindUniform(const std::string& name, const Uniform& uniform)
180 {
181 boundUniforms[name] = uniform;
182 }
183
185 {
186 size_t hash = 0;
187
188 for (const auto& tex : boundTextures) {
189 boost::hash_combine(hash, tex.first);
190 boost::hash_combine(hash, tex.second);
191 }
192
193 for (const auto& samp : boundSamplers) {
194 boost::hash_combine(hash, samp.first);
195 boost::hash_combine(hash, samp.second);
196 }
197
198 for (const auto& uni : boundUniforms) {
199 boost::hash_combine(hash, uni.first);
200 boost::hash_combine(hash, uni.second);
201 }
202
203 return hash;
204 }
205
207 const std::function<FCT::PassResource*(const PassResourceState& state)>& creator)
208 {
209 auto hash = state.hash();
210 if (m_passResources.count(hash))
211 {
212 return m_passResources[hash];
213 } else
214 {
215 auto passResource = creator(state);
216 m_passResources[hash] = passResource;
217 return passResource;
218 }
219 }
220
222 const std::function<FCT::RHI::RasterizationPipeline*(const TraditionPipelineState& state)>& creator)
223 {
224 auto hash = state.hash();
225 if (m_pipelines.count(hash))
226 {
227 return m_pipelines[hash];
228 } else
229 {
230 auto pipeline = creator(state);
231 m_pipelines[hash] = pipeline;
232 return pipeline;
233 }
234 }
235
237 {
238 auto it = m_unhandledTextureSlots.begin();
239 while (it != m_unhandledTextureSlots.end())
240 {
241 FCT::ShaderStages usage;
242
244 {
245 ++it;
246 continue;
247 }
249 {
251 }
253 {
255 }
256 else
257 {
259 }
260 m_textureNames[it->name] = nullptr;
261 m_textureNames[it->name] = m_textureNames.find(it->name)->first.c_str();
263 addTextureSlot(element);
264
265 it = m_unhandledTextureSlots.erase(it);
266 }
267 }
268
270 {
273 }
274 return m_nextAvailableIndex++;
275 }
276
278 {
279 return m_passResourceCache.get(m_passResourceState, [this](const PassResourceState& state)
280 {
281 auto ret = m_ctx->createResource<FCT::PassResource>();
282 for (const auto& tex : state.boundTextures) {
283 ret->addTexture( tex.second,m_resourceLayout.findTexture(tex.first.c_str()));
284 }
285 for (const auto& samp : state.boundSamplers) {
286 ret->addSampler(samp.second,m_resourceLayout.findSampler(samp.first.c_str()));
287 }
288 for (const auto& uni : state.boundUniforms) {
289 ret->addConstBuffer(uni.second);
290 }
291 ret->create();
292 return ret;
293 });
294 }
295
297 {
298 return m_pipelineCache.get(m_pipelineState, [this](const TraditionPipelineState& state)
299 {
300 auto ret = m_ctx->createTraditionPipeline();
301
302 for (auto& layout : m_vertexLayouts)
303 {
304 ret->vertexLayout(layout.second);
305 }
306
307 ret->pixelLayout(m_pixelLayout);
308
309 if (state.vertexShader) {
310 ret->addResources(state.vertexShader);
311 }
312 if (state.pixelShader) {
313 ret->addResources(state.pixelShader);
314 }
315
316 if (state.blendState) {
317 ret->addResources(state.blendState);
318 }
319 if (state.rasterizationState) {
320 ret->addResources(state.rasterizationState);
321 }
322 if (state.depthStencilState) {
323 ret->addResources(state.depthStencilState);
324 }
325
326 if (m_pass) {
327 ret->bindPass(m_pass);
328 }
329
330 ret->create();
331 return ret;
332 });
333 }
334}
std::map< std::string, FCT::ConstLayout > m_uniformLayouts
void bindVertexShader(FCT::VertexShader *shader)
void bindUniform(const Uniform &uniform)
std::unordered_map< std::string, const char * > m_textureNames
void setFixedImage(std::string name, FCT::Image *image)
void bindPixelShader(FCT::PixelShader *shader)
void processUnhandledTextureSlots()
FCT::ResourceLayout m_pixelResourceLayout
void attachPass(FCT::RenderGraph *graph, std::string passName)
bool m_hasVertexLayout
FCT::Context * m_ctx
void bindSampler(std::string name, FCT::Sampler *sampler)
uint32_t m_nextAvailableIndex
uint32_t findNextAvailableIndex()
void bindTexture(std::string name, FCT::Image *image)
FCT::ResourceLayout m_vertexResourceLayout
std::unordered_map< std::string, FCT::Image * > m_fixedImages
void addTextureSlot(FCT::TextureElement element)
TraditionPipelineState m_pipelineState
FCT::ResourceLayout m_resourceLayout
std::map< uint32_t, FCT::VertexLayout > m_vertexLayouts
FCT::RHI::RasterizationPipeline * getCurrentPipeline()
PassResourceState m_passResourceState
PassResourceCache m_passResourceCache
FCT::PixelLayout m_pixelLayout
PipelineCache m_pipelineCache
bool m_hasPixelLayout
FCT::RHI::Pass * m_pass
void ctx(FCT::Context *ctx)
FCT::PixelShader * allocatePixelShader(std::string code)
Uniform allocateUniform(std::string name)
FCT::VertexShader * allocateVertexShader(std::string code)
FCT::PassResource * getCurrentPassResource()
std::vector< TextureSlot > m_unhandledTextureSlots
virtual void create()=0
std::vector< TextureEdge * > getTextureEdges(const std::string &passName) const
RHI::Pass * getPass(const std::string &name) const
Image * getImage(const std::string &name) const
constexpr ShaderStages getShaderStages() const noexcept
FCT::PixelShader PixelShader
FCT::VertexShader VertexShader
FCT::ShaderStages getAllAfterTheStage(FCT::ShaderStage stages)
FCT::PassResource * get(const PassResourceState &state, const std::function< FCT::PassResource *(const PassResourceState &state)> &creator)
std::unordered_map< size_t, FCT::PassResource * > m_passResources
std::unordered_map< std::string, FCT::RHI::ConstBuffer * > boundUniforms
void bindUniform(const std::string &name, const Uniform &uniform)
void bindTexture(const std::string &name, FCT::Image *image)
std::unordered_map< std::string, FCT::Image * > boundTextures
std::unordered_map< std::string, FCT::Sampler * > boundSamplers
void bindSampler(const std::string &name, FCT::Sampler *sampler)
std::unordered_map< size_t, FCT::RHI::RasterizationPipeline * > m_pipelines
FCT::RHI::RasterizationPipeline * get(const TraditionPipelineState &state, const std::function< FCT::RHI::RasterizationPipeline *(const TraditionPipelineState &state)> &creator)
FCT::DepthStencilState * depthStencilState
FCT::RasterizationState * rasterizationState
bool operator==(const TraditionPipelineState &other) const