FCT
载入中...
搜索中...
未找到
layout.h
浏览该文件的文档.
1#ifndef LAYOUT_H
2#define LAYOUT_H
3#include "ShaderStage.h"
4#include "Context.h"
5#include "UniformWrapper.h"
6#include "VertexShader.h"
7
8namespace FCT {
9 // --- 前向声明 ---
11 class Image;
12 class Sampler;
13 class PassResource;
14 namespace RHI {
15 class Pass;
16 class CommandBuffer;
18 }
19
20 // --- 用于构造 Layout 的参数结构体 ---
25 {
26 std::string name;
27 };
28
32 struct PassName
33 {
34 std::string name;
35 };
36
40 struct ShaderRef
41 {
42 std::string code;
43 size_t hash;
45 };
46
47 // --- 类型别名 ---
52
61 class Layout {
62 public:
66 template<typename... Args>
67 Layout(FCT::Context* ctx,Args... args);
74 void ctx(FCT::Context* ctx);
76
80
88 void addVertexLayout(const FCT::VertexLayout& vertexLayout);
89
97 void setPixelLayout(const FCT::PixelLayout& pixelLayout);
98
106 void addUniformSlot(const UniformSlot& uniformSlot);
107
108 void addTextureSlot(const FCT::TextureSlot& element);
117
125 void addSamplerSlot(const SamplerSlot& samplerSlot);
126
134 void removeTextureSlot(const char* name);
135
143 void setFixedImage(std::string name, FCT::Image* image);
144
151 void attachPass(std::string passName);
152
154
158
166 Uniform allocateUniform(std::string name);
167
174 void begin();
175
182 void bindUniform(const Uniform& uniform);
183 void bindUniform(Uniform* uniform);
184
192 void bindTexture(std::string name, FCT::Image* image);
193
201 void bindSampler(std::string name, FCT::Sampler* sampler);
202
209 void bindVertexShader(std::string code);
210
217 void bindPixelShader(std::string code);
218
225 void bindVertexShader(const ShaderRef& ref);
232 void bindPixelShader(const ShaderRef& ref);
233
242 template<typename T>
243 void drawMesh(RHI::CommandBuffer* cmdBuffer,T* mesh);
244
253 template<typename T>
254 void drawMesh(RHI::CommandBuffer* cmdBuffer,T& mesh);
255
262 void end();
263
265
269
277 ShaderRef cacheVertexShader(const std::string& code);
278
286 ShaderRef cachePixelShader(const std::string& code);
287
294// ... existing code
300 void clearPipelineCache();
301
307 void clearShaderCache();
308
310
311 private:
312 // =================================================================================
313 // 私有辅助结构体 (Private Helper Structs)
314 // =================================================================================
315
316 // 管线状态
327
328 // Pass 资源状态
330 {
331 std::unordered_map<std::string, FCT::Image*> boundTextures;
332 std::unordered_map<std::string, FCT::Sampler*> boundSamplers;
333 std::unordered_map<std::string, FCT::RHI::ConstBuffer*> boundUniforms;
334 void clear();
335 void bindTexture(const std::string& name, FCT::Image* image);
336 void bindSampler(const std::string& name, FCT::Sampler* sampler);
337 void bindUniform(const std::string& name, const Uniform& uniform);
338 void bindUniform(const std::string& name, Uniform* uniform);
339 size_t hash() const;
340 };
341
342 // Pass 资源缓存
344 {
345 std::unordered_map<size_t,FCT::PassResource*> m_passResources;
347 FCT::PassResource* get(const PassResourceState& state, const std::function<FCT::PassResource*(const PassResourceState& state)>& creator);
348 };
349
350 // 管线缓存
352 {
353 std::unordered_map<size_t, FCT::RHI::RasterizationPipeline*> m_pipelines;
356 };
357
358 // 着色器缓存
360 {
361 std::unordered_map<size_t, FCT::VertexShader*> m_vertexShaders;
362 std::unordered_map<size_t, FCT::PixelShader*> m_pixelShaders;
363 ~ShaderCache();
364 FCT::VertexShader* getVertexShader(const std::string& code,const std::function<FCT::VertexShader*(const std::string& code)>& creator);
365 FCT::PixelShader* getPixelShader(const std::string& code,const std::function<FCT::PixelShader*(const std::string& code)>& creator);
366 FCT::VertexShader* getVertexShader(const ShaderRef& ref,const std::function<FCT::VertexShader*(const ShaderRef& ref)>& creator);
367 FCT::PixelShader* getPixelShader(const ShaderRef& ref, const std::function<FCT::PixelShader*(const ShaderRef& ref)>& creator);
368 void clear();
369 };
370
371 // =================================================================================
372 // 私有成员变量 (Private Member Variables)
373 // =================================================================================
374
375 // --- 核心上下文与布局定义 ---
377 std::map<uint32_t, FCT::VertexLayout> m_vertexLayouts;
378 std::map<std::string, FCT::ConstLayout> m_uniformLayouts;
383 std::unordered_map<std::string, FCT::Image*> m_fixedImages;
384 std::map<std::string, Image*> m_textureFromPass;
385 std::unordered_map<std::string,const char*> m_textureNames;
386
387 // --- 状态与资源管理 ---
389 std::vector<TextureSlot> m_unhandledTextureSlots;
390 bool m_hasVertexLayout = false;
391 bool m_hasPixelLayout = false;
392
393 // --- 当前 Pass/Pipeline 状态 ---
397
398 // --- 缓存对象 ---
402
403 // =================================================================================
404 // 私有辅助函数 (Private Helper Functions)
405 // =================================================================================
406
407 // --- 构造函数参数处理 ---
408 template<typename... Args>
409 void proccessArgs(FCT::VertexLayout vertexLayout, Args... args);
410 template <class ... Args>
411 void proccessArgs(const std::vector<FCT::VertexLayout>& vertexLayouts, Args... args);
412 template<typename... Args>
413 void proccessArgs(SamplerSlot samplerSlot, Args... args);
414 template<typename... Args>
415 void proccessArgs(PassName passName, Args... args);
416 template<typename... Args>
417 void proccessArgs(uint32_t index, FCT::VertexLayout vertexLayout, Args... args);
418 template<typename... Args>
419 void proccessArgs(FCT::PixelLayout pixelLayout, Args... args);
420 template<typename... Args>
421 void proccessArgs(TextureSlot textureSlot, Args... args);
422 template<typename... Args>
423 void proccessArgs(UniformSlot uniformSlot, Args... args);
424 void proccessArgs();
425
426 // --- 内部资源获取 ---
429
430 // --- 内部绑定与分配 ---
432 void bindPixelShader(FCT::PixelShader* shader);
433 FCT::VertexShader* allocateVertexShader(std::string code);
434 FCT::PixelShader* allocatePixelShader(std::string code);
435
436 // --- 缓存访问 ---
437 VertexShader* getCacheVertexShader(std::string code);
438 PixelShader* getCachePixelShader(std::string code);
441
442 // --- 其他工具函数 ---
444 uint32_t findNextAvailableIndex();
445 };
446
447}
448#endif //LAYOUT_H
std::map< std::string, FCT::ConstLayout > m_uniformLayouts
ShaderRef cachePixelShader(const std::string &code)
预缓存一个像素着色器并返回其引用。
ShaderCache m_shaderCache
void bindUniform(const Uniform &uniform)
绑定一个 Uniform 常量缓冲区。
std::unordered_map< std::string, const char * > m_textureNames
void clearPassResourceCache()
清空所有已缓存的 PassResource。
void removeTextureSlot(const char *name)
移除一个纹理资源槽位。
void setFixedImage(std::string name, FCT::Image *image)
设置一个固定的图像资源,该资源不会在 begin/end 之间被清除。
void processUnhandledTextureSlots()
FCT::ResourceLayout m_pixelResourceLayout
void begin()
开始一个渲染批次。
void attachPass(std::string passName)
将此 Layout 附加到一个渲染通道。
void setPixelLayout(const FCT::PixelLayout &pixelLayout)
设置像素(片元)着色器的输出布局。
bool m_hasVertexLayout
FCT::Context * m_ctx
void addVertexLayout(const FCT::VertexLayout &vertexLayout)
添加一个顶点布局。
void proccessArgs()
Layout(FCT::Context *ctx, Args... args)
void bindSampler(std::string name, FCT::Sampler *sampler)
绑定一个采样器。
void drawMesh(RHI::CommandBuffer *cmdBuffer, T *mesh)
绘制一个网格。
void clearPipelineCache()
清空所有已缓存的渲染管线。
uint32_t m_nextAvailableIndex
uint32_t findNextAvailableIndex()
void bindTexture(std::string name, FCT::Image *image)
绑定一个纹理。
FCT::ResourceLayout m_vertexResourceLayout
ShaderRef cacheVertexShader(const std::string &code)
预缓存一个顶点着色器并返回其引用。
std::map< std::string, Image * > m_textureFromPass
void end()
结束渲染批次。
VertexShader * getCacheVertexShader(std::string code)
std::unordered_map< std::string, FCT::Image * > m_fixedImages
TraditionPipelineState m_pipelineState
void bindVertexShader(std::string code)
绑定顶点着色器。
FCT::ResourceLayout m_resourceLayout
std::map< uint32_t, FCT::VertexLayout > m_vertexLayouts
FCT::RHI::RasterizationPipeline * getCurrentPipeline()
PassResourceState m_passResourceState
void addTextureSlot(const FCT::TextureSlot &element)
PassResourceCache m_passResourceCache
FCT::PixelLayout m_pixelLayout
PipelineCache m_pipelineCache
void bindPixelShader(std::string code)
绑定像素着色器。
bool m_hasPixelLayout
void addSamplerSlot(const SamplerSlot &samplerSlot)
添加一个采样器资源槽位。
FCT::RHI::Pass * m_pass
void ctx(FCT::Context *ctx)
重新设置 FCT 上下文。
void clearShaderCache()
清空所有已缓存的着色器。
FCT::PixelShader * allocatePixelShader(std::string code)
PixelShader * getCachePixelShader(std::string code)
Uniform allocateUniform(std::string name)
根据已定义的 UniformSlot 分配一个 Uniform 实例。
FCT::VertexShader * allocateVertexShader(std::string code)
FCT::PassResource * getCurrentPassResource()
void addUniformSlot(const UniformSlot &uniformSlot)
添加一个常量缓冲区(Uniform Buffer)的布局槽位。
std::vector< TextureSlot > m_unhandledTextureSlots
FCT::SamplerElement SamplerSlot
FCT::ConstElement UniformVar
FCT::ConstLayout UniformSlot
FCT::ConstType UniformType
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::VertexShader * getVertexShader(const std::string &code, const std::function< FCT::VertexShader *(const std::string &code)> &creator)
std::unordered_map< size_t, FCT::VertexShader * > m_vertexShaders
FCT::PixelShader * getPixelShader(const std::string &code, const std::function< FCT::PixelShader *(const std::string &code)> &creator)
std::unordered_map< size_t, FCT::PixelShader * > m_pixelShaders
FCT::DepthStencilState * depthStencilState
FCT::RasterizationState * rasterizationState
bool operator==(const TraditionPipelineState &other) const
定义 Layout 将要附加到的渲染通道(Render Pass)的名称。
std::string name
定义着色器代码的引用,包含代码本身、哈希值和类型。
std::string code
ShaderKind kind
定义一个纹理槽位,用于在着色器中绑定纹理。
std::string name