diff --git a/CMCore b/CMCore index 1bcd213b..5aad7d81 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 1bcd213b63ed080863fd4a51eaec5fe6205e7589 +Subproject commit 5aad7d81419d02141b3b3f53661e2c8e3e763163 diff --git a/inc/hgl/graph/RenderFramework.h b/inc/hgl/graph/RenderFramework.h index 8bf65edf..6fd68ba4 100644 --- a/inc/hgl/graph/RenderFramework.h +++ b/inc/hgl/graph/RenderFramework.h @@ -6,7 +6,12 @@ VK_NAMESPACE_BEGIN +class FontSource; +class TileFont; class RenderPassManager; +class TextureManager; +class RenderTargetManager; +class SwapchainModule; class RenderFramework:public io::WindowEvent { @@ -19,10 +24,15 @@ class RenderFramework:public io::WindowEvent protected: - GraphModuleManager *module_manager =nullptr; + GraphModuleManager * module_manager =nullptr; - RenderPassManager * render_pass_manager =nullptr; - RenderPass * device_render_pass =nullptr; + RenderPassManager * render_pass_manager =nullptr; + RenderPass * device_render_pass =nullptr; + + TextureManager * texture_manager =nullptr; + RenderTargetManager * rt_manager =nullptr; + + SwapchainModule * swapchain_module =nullptr; public: @@ -33,9 +43,13 @@ public: public: - GraphModuleManager *GetModuleManager(){return module_manager;} - RenderPassManager * GetRenderPassManager(){return render_pass_manager;} + GraphModuleManager * GetModuleManager(){return module_manager;} + RenderPassManager * GetRenderPassManager (){return render_pass_manager;} + TextureManager * GetTextureManager (){return texture_manager;} + RenderTargetManager * GetRenderTargetManager (){return rt_manager;} + + SwapchainModule * GetSwapchainModule (){return swapchain_module;} public: @@ -44,6 +58,10 @@ public: virtual bool Init(uint w,uint h); +public: + + TileFont *CreateTileFont(FontSource *fs,int limit_count=-1); ///<创建只使用一种字符的Tile字符管理对象 + public: // event void OnResize(uint w,uint h); diff --git a/inc/hgl/graph/TileData.h b/inc/hgl/graph/TileData.h index e3efc33f..61d39dd5 100644 --- a/inc/hgl/graph/TileData.h +++ b/inc/hgl/graph/TileData.h @@ -12,13 +12,15 @@ namespace hgl { namespace graph { + class TextureManager; + /** * TileData是一种处理大量等同尺寸及格式贴图的管理机制,程序会自动根据显卡最大贴图处理能力来创建尽可能符合需求的贴图。(注:Tile的大小不必符合2次幂) * Tile的增加或删除,程序会自动排序,尽可能小的减少I/O消耗。 */ class TileData ///Tile纹理管理 { - GPUDevice *device; + TextureManager *texture_manager; protected: @@ -51,8 +53,8 @@ namespace hgl Texture2D * GetTexture ()const{return tile_texture;} ///<取得贴图 public: - - TileData(GPUDevice *,Texture2D *,const uint tw,const uint th); + + TileData(TextureManager *,Texture2D *,const uint tw,const uint th); virtual ~TileData(); void BeginCommit(); diff --git a/inc/hgl/graph/VKDevice.h b/inc/hgl/graph/VKDevice.h index 618bf369..5b7525f5 100644 --- a/inc/hgl/graph/VKDevice.h +++ b/inc/hgl/graph/VKDevice.h @@ -152,57 +152,6 @@ public: //间接绘制 IndirectDrawIndexedBuffer * CreateIndirectDrawIndexedBuffer(const uint32_t cmd_count,SharingMode sm=SharingMode::Exclusive); IndirectDispatchBuffer * CreateIndirectDispatchBuffer( const uint32_t cmd_count,SharingMode sm=SharingMode::Exclusive); -public: //Image - - VkImage CreateImage (VkImageCreateInfo *); - void DestroyImage (VkImage); - -private: //texture - - bool CopyBufferToImage (const CopyBufferToImageInfo *info,VkPipelineStageFlags destinationStage); - - bool CopyBufferToImage (Texture *,DeviceBuffer *buf,const VkBufferImageCopy *,const int count,const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags);//=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); - - bool CopyBufferToImage2D (Texture *tex,DeviceBuffer *buf,const VkBufferImageCopy *bic_list,const int bic_count, VkPipelineStageFlags dstStage){return CopyBufferToImage(tex,buf,bic_list, bic_count, 0,1,dstStage);} - bool CopyBufferToImage2D (Texture *tex,DeviceBuffer *buf,const VkBufferImageCopy *bic, VkPipelineStageFlags dstStage){return CopyBufferToImage(tex,buf,bic, 1, 0,1,dstStage);} - - bool CopyBufferToImageCube (Texture *tex,DeviceBuffer *buf,const VkBufferImageCopy *bic_list,const int bic_count, VkPipelineStageFlags dstStage){return CopyBufferToImage(tex,buf,bic_list, bic_count, 0,6,dstStage);} - bool CopyBufferToImageCube (Texture *tex,DeviceBuffer *buf,const VkBufferImageCopy *bic, VkPipelineStageFlags dstStage){return CopyBufferToImage(tex,buf,bic, 1, 0,6,dstStage);} - - bool CommitTexture2D (Texture2D *,DeviceBuffer *buf,VkPipelineStageFlags stage); - bool CommitTexture2DMipmaps (Texture2D *,DeviceBuffer *buf,const VkExtent3D &,uint32_t); - - bool CommitTextureCube (TextureCube *,DeviceBuffer *buf,const uint32_t mipmaps_zero_bytes,VkPipelineStageFlags stage); - bool CommitTextureCubeMipmaps (TextureCube *,DeviceBuffer *buf,const VkExtent3D &,uint32_t); - - bool SubmitTexture (const VkCommandBuffer *cmd_bufs,const uint32_t count=1); ///<提交纹理处理到队列 - -public: //Texture - - bool CheckFormatSupport(const VkFormat,const uint32_t bits,ImageTiling tiling=ImageTiling::Optimal)const; - - bool CheckTextureFormatSupport(const VkFormat fmt,ImageTiling tiling=ImageTiling::Optimal)const{return CheckFormatSupport(fmt,VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT,tiling);} - - Texture2D *CreateTexture2D(TextureData *); - Texture2D *CreateTexture2D(TextureCreateInfo *ci); - - Texture2DArray *CreateTexture2DArray(TextureData *); - Texture2DArray *CreateTexture2DArray(TextureCreateInfo *ci); - Texture2DArray *CreateTexture2DArray(const uint32_t w,const uint32_t h,const uint32 l,const VkFormat fmt,const bool mipmaps); - - TextureCube *CreateTextureCube(TextureData *); - TextureCube *CreateTextureCube(TextureCreateInfo *ci); - - void Clear(TextureCreateInfo *); - - bool ChangeTexture2D(Texture2D *,DeviceBuffer *buf, const List &,VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); - bool ChangeTexture2D(Texture2D *,DeviceBuffer *buf, const RectScope2ui &, VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); - bool ChangeTexture2D(Texture2D *,void *data,const uint32_t size,const RectScope2ui &, VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); - -// bool ChangeTexture2DArray(Texture2DArray *,DeviceBuffer *buf, const List &, const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); - bool ChangeTexture2DArray(Texture2DArray *,DeviceBuffer *buf, const RectScope2ui &, const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); - bool ChangeTexture2DArray(Texture2DArray *,void *data,const uint32_t size,const RectScope2ui &, const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); - public: // Sampler *CreateSampler(VkSamplerCreateInfo *sci=nullptr); @@ -228,18 +177,6 @@ public: DeviceQueue *CreateQueue(const uint32_t fence_count=1,const bool create_signaled=false); -public: //FrameBuffer相关 - - Framebuffer *CreateFBO(RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth); -// Framebuffer *CreateFBO(RenderPass *,List &color,ImageView *depth); - Framebuffer *CreateFBO(RenderPass *,ImageView *color,ImageView *depth); - Framebuffer *CreateFBO(RenderPass *,ImageView *); - -public: - - RenderTarget *CreateRT( const FramebufferInfo *fbi,RenderPass *,const uint32_t fence_count=1); - RenderTarget *CreateRT( const FramebufferInfo *fbi,const uint32_t fence_count=1); - public: TileData *CreateTileData(const VkFormat video_format,const uint width,const uint height,const uint count); ///<创建一个Tile数据集 diff --git a/inc/hgl/graph/VKFramebuffer.h b/inc/hgl/graph/VKFramebuffer.h index 5f80a34d..af0ace67 100644 --- a/inc/hgl/graph/VKFramebuffer.h +++ b/inc/hgl/graph/VKFramebuffer.h @@ -16,7 +16,7 @@ class Framebuffer private: - friend class GPUDevice; + friend class RenderTargetManager; Framebuffer(VkDevice,VkFramebuffer,const VkExtent2D &,VkRenderPass,uint32_t color_count,bool depth); diff --git a/inc/hgl/graph/VKRenderResource.h b/inc/hgl/graph/VKRenderResource.h index 1c9b9b80..f95f9f7d 100644 --- a/inc/hgl/graph/VKRenderResource.h +++ b/inc/hgl/graph/VKRenderResource.h @@ -34,7 +34,6 @@ using DescriptorSetID =int; using PrimitiveID =int; using RenderableID =int; using SamplerID =int; -using TextureID =int; using StaticMeshID =int; class VertexAttribData; @@ -51,7 +50,6 @@ class RenderResource ShaderModuleMapByName shader_module_by_name[VK_SHADER_STAGE_TYPE_COUNT]; Map material_by_name; - Map texture_by_name; IDObjectManage rm_material; ///<材质合集 IDObjectManage rm_material_instance; ///<材质实例合集 @@ -59,7 +57,6 @@ class RenderResource IDObjectManage rm_primitives; ///<图元合集 IDObjectManage rm_buffers; ///<顶点缓冲区合集 IDObjectManage rm_samplers; ///<采样器合集 - IDObjectManage rm_textures; ///<纹理合集 IDObjectManage rm_renderables; ///<渲染实例集合集 IDObjectManage rm_static_mesh; ///<静态网格合集 @@ -105,7 +102,6 @@ public: //添加数据到管理器(如果指针为nullptr会返回-1) PrimitiveID Add(Primitive * p ){return rm_primitives.Add(p);} BufferID Add(DeviceBuffer * buf ){return rm_buffers.Add(buf);} SamplerID Add(Sampler * s ){return rm_samplers.Add(s);} - TextureID Add(Texture * t ){return rm_textures.Add(t);} RenderableID Add(Renderable * r ){return rm_renderables.Add(r);} StaticMeshID Add(StaticMesh * sm ){return rm_static_mesh.Add(sm);} @@ -159,14 +155,6 @@ public: //Material Sampler * CreateSampler(VkSamplerCreateInfo *sci=nullptr); Sampler * CreateSampler(Texture *); -public: //texture - - Texture2D * LoadTexture2D(const OSString &,bool auto_mipmaps=false); - TextureCube * LoadTextureCube(const OSString &,bool auto_mipmaps=false); - - Texture2DArray * CreateTexture2DArray(const AnsiString &name,const uint32_t width,const uint32_t height,const uint32_t layer,const VkFormat &fmt,bool auto_mipmaps=false); - bool LoadTexture2DToArray(Texture2DArray *,const uint32_t layer,const OSString &); - public: //Get Material * GetMaterial (const MaterialID &id){return rm_material.Get(id);} @@ -175,7 +163,6 @@ public: //Get Primitive * GetPrimitive (const PrimitiveID &id){return rm_primitives.Get(id);} DeviceBuffer * GetBuffer (const BufferID &id){return rm_buffers.Get(id);} Sampler * GetSampler (const SamplerID &id){return rm_samplers.Get(id);} - Texture * GetTexture (const TextureID &id){return rm_textures.Get(id);} Renderable * GetRenderable (const RenderableID &id){return rm_renderables.Get(id);} StaticMesh * GetStaticMesh (const StaticMeshID &id){return rm_static_mesh.Get(id);} @@ -188,7 +175,6 @@ public: //Release void Release(Primitive * p ){rm_primitives.Release(p);} void Release(DeviceBuffer * buf ){rm_buffers.Release(buf);} void Release(Sampler * s ){rm_samplers.Release(s);} - void Release(Texture * t ){rm_textures.Release(t);} void Release(Renderable * r ){rm_renderables.Release(r);} void Release(StaticMesh * sm ){rm_static_mesh.Release(sm);} diff --git a/inc/hgl/graph/VKRenderTarget.h b/inc/hgl/graph/VKRenderTarget.h index fbd933f7..6cbb7d56 100644 --- a/inc/hgl/graph/VKRenderTarget.h +++ b/inc/hgl/graph/VKRenderTarget.h @@ -32,7 +32,7 @@ protected: protected: - friend class GPUDevice; + friend class RenderTargetManager; RenderTarget(DeviceQueue *,Semaphore *); RenderTarget(DeviceQueue *,Semaphore *,RenderPass *_rp,Framebuffer *_fb,Texture2D **color_texture_list,const uint32_t color_count,Texture2D *depth_texture); diff --git a/inc/hgl/graph/VKSwapchain.h b/inc/hgl/graph/VKSwapchain.h index 9a509193..24fcb0a2 100644 --- a/inc/hgl/graph/VKSwapchain.h +++ b/inc/hgl/graph/VKSwapchain.h @@ -9,18 +9,21 @@ struct Swapchain { public: - VkDevice device =VK_NULL_HANDLE; + VkDevice device =VK_NULL_HANDLE; - VkExtent2D extent; + VkExtent2D extent; + VkSurfaceTransformFlagBitsKHR transform; - VkSwapchainKHR swap_chain =VK_NULL_HANDLE; + VkSwapchainKHR swap_chain =VK_NULL_HANDLE; + VkSurfaceFormatKHR surface_format; + VkFormat depth_format; - uint32_t color_count =0; + uint32_t color_count =0; - Texture2D ** sc_color =nullptr; - Texture2D * sc_depth =nullptr; + Texture2D ** sc_color =nullptr; + Texture2D * sc_depth =nullptr; - Framebuffer ** sc_fbo =nullptr; + Framebuffer ** sc_fbo =nullptr; public: diff --git a/inc/hgl/graph/VKTexture.h b/inc/hgl/graph/VKTexture.h index a322af3f..673c1d20 100644 --- a/inc/hgl/graph/VKTexture.h +++ b/inc/hgl/graph/VKTexture.h @@ -11,15 +11,23 @@ VK_NAMESPACE_BEGIN BitmapData *LoadBitmapFromFile(const OSString &filename); +using TextureID=int; +class TextureManager; + class Texture { protected: + + TextureManager *manager; + TextureID texture_id; - VkDevice device; TextureData *data; public: + TextureManager * GetManager () {return manager;} + const TextureID GetID ()const noexcept {return texture_id;} + TextureData * GetData () {return data;} VkDeviceMemory GetDeviceMemory () {return data?(data->memory?data->memory->operator VkDeviceMemory():VK_NULL_HANDLE):VK_NULL_HANDLE;} @@ -40,9 +48,10 @@ public: public: - Texture(VkDevice dev,TextureData *td) + Texture(TextureManager *tm,const TextureID &id,TextureData *td) { - device=dev; + manager=tm; + texture_id=id; data=td; } @@ -94,8 +103,8 @@ public: class TextureCube:public Texture { public: - - TextureCube(VkDevice dev,TextureData *td):Texture(dev,td){} + + using Texture::Texture; ~TextureCube()=default; static VkImageViewType GetImageViewType(){return VK_IMAGE_VIEW_TYPE_CUBE;} diff --git a/inc/hgl/graph/module/GraphModule.h b/inc/hgl/graph/module/GraphModule.h index 229965eb..5067cd17 100644 --- a/inc/hgl/graph/module/GraphModule.h +++ b/inc/hgl/graph/module/GraphModule.h @@ -25,7 +25,7 @@ public: virtual const AnsiString &GetName()const=0; };//class GraphModule -template class GraphModuleInherit:public GraphModule +template class GraphModuleInherit:public BASE { AnsiString manager_name; @@ -43,7 +43,7 @@ public: public: - GraphModuleInherit(GPUDevice *dev,const AnsiString &name):GraphModule(dev) + GraphModuleInherit(GPUDevice *dev,const AnsiString &name):BASE(dev) { manager_name=name; } @@ -51,8 +51,8 @@ public: virtual ~GraphModuleInherit()=default; };//class GraphModuleInherit -#define GRAPH_MODULE_CLASS(class_name) class class_name:public GraphModuleInherit +#define GRAPH_MODULE_CLASS(class_name) class class_name:public GraphModuleInherit -#define GRAPH_MODULE_CONSTRUCT(class_name) class_name::class_name(GPUDevice *dev):GraphModuleInherit(dev,#class_name) +#define GRAPH_MODULE_CONSTRUCT(class_name) class_name::class_name(GPUDevice *dev):GraphModuleInherit(dev,#class_name) VK_NAMESPACE_END diff --git a/inc/hgl/graph/module/RenderModule.h b/inc/hgl/graph/module/RenderModule.h index 9af9365d..682bc6ac 100644 --- a/inc/hgl/graph/module/RenderModule.h +++ b/inc/hgl/graph/module/RenderModule.h @@ -10,8 +10,6 @@ VK_NAMESPACE_BEGIN */ class RenderModule:public GraphModule { - VkExtent2D current_extent; - public: NO_COPY_NO_MOVE(RenderModule) @@ -19,9 +17,13 @@ public: using GraphModule::GraphModule; virtual ~RenderModule()=default; - virtual void OnResize(const VkExtent2D &ext){current_extent=ext;} ///<窗口大小改变 + virtual void OnResize(const VkExtent2D &)=0; ///<窗口大小改变 - virtual void OnFrameRender(const double,RenderCmdBuffer *)=0; ///<帧绘制回调 + //virtual void OnFrameRender(const double,RenderCmdBuffer *)=0; ///<帧绘制回调 };//class RenderModule +#define RENDER_MODULE_CLASS(class_name) class class_name:public GraphModuleInherit + +#define RENDER_MODULE_CONSTRUCT(class_name) class_name::class_name(GPUDevice *dev):GraphModuleInherit(dev,#class_name) + VK_NAMESPACE_END diff --git a/inc/hgl/graph/module/RenderPassManager.h b/inc/hgl/graph/module/RenderPassManager.h index c0c1cbd0..7cf6c5be 100644 --- a/inc/hgl/graph/module/RenderPassManager.h +++ b/inc/hgl/graph/module/RenderPassManager.h @@ -7,12 +7,13 @@ VK_NAMESPACE_BEGIN using RenderPassHASHCode=util::HashCode<128/8>; + inline util::Hash *CreateRenderPassHash() { return util::CreateHash(util::HASH::xxH3_128); } -class RenderPassManager:public GraphModuleInherit +GRAPH_MODULE_CLASS(RenderPassManager) { VkPipelineCache pipeline_cache; diff --git a/inc/hgl/graph/module/RenderTargetManager.h b/inc/hgl/graph/module/RenderTargetManager.h new file mode 100644 index 00000000..fecf121d --- /dev/null +++ b/inc/hgl/graph/module/RenderTargetManager.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +VK_NAMESPACE_BEGIN + +class TextureManager; +class RenderPassManager; + +GRAPH_MODULE_CLASS(RenderTargetManager) +{ + TextureManager *tex_manager; + RenderPassManager *rp_manager; + +public: + + RenderTargetManager(GPUDevice *,TextureManager *tm,RenderPassManager *rpm); + virtual ~RenderTargetManager(); + +public: //FrameBuffer相关 + + Framebuffer *CreateFBO(RenderPass *rp,ImageView **color_list,const uint image_count,ImageView *depth); +// Framebuffer *CreateFBO(RenderPass *,List &color,ImageView *depth); + Framebuffer *CreateFBO(RenderPass *,ImageView *color,ImageView *depth); + Framebuffer *CreateFBO(RenderPass *,ImageView *); + +public: + + RenderTarget *CreateRT( const FramebufferInfo *fbi,RenderPass *,const uint32_t fence_count=1); + RenderTarget *CreateRT( const FramebufferInfo *fbi,const uint32_t fence_count=1); +};//class RenderTargetManager + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/module/SwapchainModule.h b/inc/hgl/graph/module/SwapchainModule.h new file mode 100644 index 00000000..5626f16f --- /dev/null +++ b/inc/hgl/graph/module/SwapchainModule.h @@ -0,0 +1,56 @@ +#pragma once + +#include + +VK_NAMESPACE_BEGIN + +class RenderTargetManager; + +RENDER_MODULE_CLASS(SwapchainModule) +{ + Swapchain *swapchain=nullptr; + + TextureManager *tex_manager=nullptr; + RenderTargetManager *rt_manager=nullptr; + RenderPassManager *rp_manager=nullptr; + + RTSwapchain *swapchain_rt=nullptr; + + RenderPass *swapchain_rp=nullptr; + + RenderCmdBuffer **cmd_buf=nullptr; + +protected: + + bool CreateSwapchainFBO(); + bool CreateSwapchain(); + bool CreateSwapchainRenderTarget(); + + void InitRenderCmdBuffer(); + void ClearRenderCmdBuffer(); + +public: + + virtual void OnResize(const VkExtent2D &)override; ///<窗口大小改变 + +public: + + SwapchainModule(GPUDevice *,TextureManager *tm,RenderTargetManager *rtm,RenderPassManager *rpm); + virtual ~SwapchainModule(); + + bool BeginFrame(); + void EndFrame(); + +public: + + RenderPass * GetRenderPass () {return swapchain_rp;} + + RTSwapchain * GetRenderTarget () {return swapchain_rt;} + + const VkExtent2D & GetSwapchainSize()const {return swapchain_rt->GetExtent();} + + RenderCmdBuffer *GetRenderCmdBuffer(); + +};//class SwapchainModule:public RenderModule + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/module/TextureManager.h b/inc/hgl/graph/module/TextureManager.h new file mode 100644 index 00000000..348c2b6f --- /dev/null +++ b/inc/hgl/graph/module/TextureManager.h @@ -0,0 +1,126 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +VK_NAMESPACE_BEGIN + +GRAPH_MODULE_CLASS(TextureManager) +{ + DeviceQueue *texture_queue=nullptr; + TextureCmdBuffer *texture_cmd_buf=nullptr; + +private: + + TextureID texture_serial=0; + + const TextureID AcquireID(){return texture_serial++;} ///<取得一个新的纹理ID + +private: + + SortedSet image_set; + SortedSet texture_set; ///<纹理合集 + + Map texture_by_id; + Map texture_by_filename; + + const TextureID Add(Texture *); + const TextureID Add(Texture *,const OSString &); + +public: + + TextureManager(GPUDevice *); + virtual ~TextureManager(); + + const VkFormatProperties GetFormatProperties(const VkFormat)const; + +public: //Buffer + + DeviceBuffer *CreateTransferSourceBuffer(const VkDeviceSize,const void *data_ptr=nullptr); + +private: //Image + + VkImage CreateImage (VkImageCreateInfo *); + void DestroyImage (VkImage); + +private: //texture + + bool CopyBufferToImage (const CopyBufferToImageInfo *info,VkPipelineStageFlags destinationStage); + + bool CopyBufferToImage (Texture *,DeviceBuffer *buf,const VkBufferImageCopy *,const int count,const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags);//=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); + + bool CopyBufferToImage2D (Texture *tex,DeviceBuffer *buf,const VkBufferImageCopy *bic_list,const int bic_count, VkPipelineStageFlags dstStage){return CopyBufferToImage(tex,buf,bic_list, bic_count, 0,1,dstStage);} + bool CopyBufferToImage2D (Texture *tex,DeviceBuffer *buf,const VkBufferImageCopy *bic, VkPipelineStageFlags dstStage){return CopyBufferToImage(tex,buf,bic, 1, 0,1,dstStage);} + + bool CopyBufferToImageCube (Texture *tex,DeviceBuffer *buf,const VkBufferImageCopy *bic_list,const int bic_count, VkPipelineStageFlags dstStage){return CopyBufferToImage(tex,buf,bic_list, bic_count, 0,6,dstStage);} + bool CopyBufferToImageCube (Texture *tex,DeviceBuffer *buf,const VkBufferImageCopy *bic, VkPipelineStageFlags dstStage){return CopyBufferToImage(tex,buf,bic, 1, 0,6,dstStage);} + + bool CommitTexture2D (Texture2D *,DeviceBuffer *buf,VkPipelineStageFlags stage); + bool CommitTexture2DMipmaps (Texture2D *,DeviceBuffer *buf,const VkExtent3D &,uint32_t); + + bool CommitTextureCube (TextureCube *,DeviceBuffer *buf,const uint32_t mipmaps_zero_bytes,VkPipelineStageFlags stage); + bool CommitTextureCubeMipmaps (TextureCube *,DeviceBuffer *buf,const VkExtent3D &,uint32_t); + + bool SubmitTexture (const VkCommandBuffer *cmd_bufs,const uint32_t count=1); ///<提交纹理处理到队列 + +public: //Format + + bool CheckFormatSupport(const VkFormat,const uint32_t bits,ImageTiling tiling=ImageTiling::Optimal)const; + + bool CheckTextureFormatSupport(const VkFormat fmt,ImageTiling tiling=ImageTiling::Optimal)const{return CheckFormatSupport(fmt,VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT,tiling);} + + bool CheckColorAttachmentFormatSupport(const VkFormat fmt)const{return CheckFormatSupport(fmt,VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT,ImageTiling::Optimal);} + bool CheckDepthStencilAttachFormatSupport(const VkFormat fmt)const{return CheckFormatSupport(fmt,VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT,ImageTiling::Optimal);} + +public: //Create/Chagne + + Texture2D *CreateTexture2D(TextureData *); + Texture2D *CreateTexture2D(TextureCreateInfo *ci); + + Texture2DArray *CreateTexture2DArray(TextureData *); + Texture2DArray *CreateTexture2DArray(TextureCreateInfo *ci); + Texture2DArray *CreateTexture2DArray(const uint32_t w,const uint32_t h,const uint32 l,const VkFormat fmt,const bool mipmaps); + + TextureCube *CreateTextureCube(TextureData *); + TextureCube *CreateTextureCube(TextureCreateInfo *ci); + + void Clear(TextureCreateInfo *); + + bool ChangeTexture2D(Texture2D *,DeviceBuffer *buf, const List &,VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); + bool ChangeTexture2D(Texture2D *,DeviceBuffer *buf, const RectScope2ui &, VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); + bool ChangeTexture2D(Texture2D *,const void *data,const VkDeviceSize size, const RectScope2ui &, VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); + +// bool ChangeTexture2DArray(Texture2DArray *,DeviceBuffer *buf, const List &, const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); + bool ChangeTexture2DArray(Texture2DArray *,DeviceBuffer *buf, const RectScope2ui &, const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); + bool ChangeTexture2DArray(Texture2DArray *,const void *data,const VkDeviceSize size,const RectScope2ui &, const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags=VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); + +public: + + void Release(Texture *); + void Destory(Texture *tex) + { + if(!tex)return; + + Release(tex); + delete tex; + } + +public: // Load + + Texture2D * LoadTexture2D(const OSString &,bool auto_mipmaps=false); + TextureCube * LoadTextureCube(const OSString &,bool auto_mipmaps=false); + + Texture2DArray * CreateTexture2DArray(const AnsiString &name,const uint32_t width,const uint32_t height,const uint32_t layer,const VkFormat &fmt,bool auto_mipmaps=false); + bool LoadTexture2DToArray(Texture2DArray *,const uint32_t layer,const OSString &); + +public: //TileData + + TileData *CreateTileData(const VkFormat video_format,const uint width,const uint height,const uint count); ///<创建一个Tile数据集 + +};//class TextureManager +VK_NAMESPACE_END diff --git a/inc/hgl/graph/module/VkModuleDebugUtil.h b/inc/hgl/graph/module/VkModuleDebugUtil.h new file mode 100644 index 00000000..e69de29b diff --git a/res b/res index 475d8ad4..e1a36d78 160000 --- a/res +++ b/res @@ -1 +1 @@ -Subproject commit 475d8ad43ceee084cd24f5d0bed59de9f6aa36fd +Subproject commit e1a36d78f0eead5f6bb65493432c4690637b991d diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 61fce339..bf665936 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -47,11 +47,18 @@ SET(SGM_SOURCE_PATH module) SET(GRAPH_MODULE_FILES ${SGM_HEADER_PATH}/GraphModule.h ${SGM_HEADER_PATH}/GraphModuleManager.h ${SGM_HEADER_PATH}/RenderPassManager.h + ${SGM_HEADER_PATH}/TextureManager.h + ${SGM_HEADER_PATH}/RenderTargetManager.h ${SGM_HEADER_PATH}/RenderModule.h + ${SGM_HEADER_PATH}/SwapchainModule.h ${SGM_SOURCE_PATH}/GraphModuleManager.cpp ${SGM_SOURCE_PATH}/RenderPassManager.cpp + ${SGM_SOURCE_PATH}/TextureManager.cpp + ${SGM_SOURCE_PATH}/RenderTargetManager.cpp + + ${SGM_SOURCE_PATH}/SwapChainModule.cpp ) source_group("Framework\\Module" FILES ${GRAPH_MODULE_FILES}) @@ -181,9 +188,7 @@ SET(VK_DEVICE_SOURCE Vulkan/VKDeviceMemory.cpp Vulkan/VKDeviceImage.cpp Vulkan/VKDeviceSampler.cpp Vulkan/VKDeviceMaterial.cpp - Vulkan/VKDeviceFramebuffer.cpp - Vulkan/VKDeviceSwapchain.cpp - Vulkan/VKDeviceRenderTarget.cpp) + Vulkan/VKDeviceFramebuffer.cpp) SET(VK_PHYSICAL_DEVICE_SOURCE ${SG_INCLUDE_PATH}/VKPhysicalDevice.h Vulkan/VKPhysicalDevice.cpp diff --git a/src/SceneGraph/RenderFramework.cpp b/src/SceneGraph/RenderFramework.cpp index d92a8b82..6b7d070f 100644 --- a/src/SceneGraph/RenderFramework.cpp +++ b/src/SceneGraph/RenderFramework.cpp @@ -2,6 +2,9 @@ #include #include #include +#include +#include +#include #include VK_NAMESPACE_BEGIN @@ -31,8 +34,6 @@ namespace RenderFramework::RenderFramework(const OSString &an) { app_name=an; - - } RenderFramework::~RenderFramework() @@ -95,6 +96,17 @@ bool RenderFramework::Init(uint w,uint h) if(!render_pass_manager) return(false); + texture_manager=module_manager->GetOrCreate(); + + if(!texture_manager) + return(false); + + rt_manager=new RenderTargetManager(device,texture_manager,render_pass_manager); + module_manager->Registry(rt_manager); + + swapchain_module=new SwapchainModule(device,texture_manager,rt_manager,render_pass_manager); + module_manager->Registry(swapchain_module); + { auto *attr=GetDevice()->GetDeviceAttribute(); diff --git a/src/SceneGraph/TileData.cpp b/src/SceneGraph/TileData.cpp index aab8cb28..9dc90743 100644 --- a/src/SceneGraph/TileData.cpp +++ b/src/SceneGraph/TileData.cpp @@ -2,14 +2,15 @@ #include #include #include +#include namespace hgl { namespace graph { - TileData::TileData(GPUDevice *dev,Texture2D *tt,const uint tw,const uint th) + TileData::TileData(TextureManager *tm,Texture2D *tt,const uint tw,const uint th) { - device=dev; + texture_manager=tm; tile_texture=tt; @@ -52,7 +53,7 @@ namespace hgl tile_bytes=GetImageBytes(tile_texture->GetFormat(),tile_width*tile_height); - tile_buffer=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,tile_bytes*tile_max_count,nullptr); + tile_buffer=texture_manager->CreateTransferSourceBuffer(tile_bytes*tile_max_count); commit_ptr=nullptr; } @@ -79,7 +80,7 @@ namespace hgl tile_buffer->Unmap(); commit_ptr=nullptr; - if(!device->ChangeTexture2D(tile_texture,tile_buffer,commit_list)) + if(!texture_manager->ChangeTexture2D(tile_texture,tile_buffer,commit_list)) return -2; const int result=commit_list.GetCount(); diff --git a/src/SceneGraph/Vulkan/Texture/VKDeviceTexture.cpp b/src/SceneGraph/Vulkan/Texture/VKDeviceTexture.cpp index 23a712f3..63e0f943 100644 --- a/src/SceneGraph/Vulkan/Texture/VKDeviceTexture.cpp +++ b/src/SceneGraph/Vulkan/Texture/VKDeviceTexture.cpp @@ -1,12 +1,21 @@ #include +#include #include #include #include"CopyBufferToImage.h" VK_NAMESPACE_BEGIN -bool GPUDevice::CheckFormatSupport(const VkFormat format,const uint32_t bits,ImageTiling tiling) const +DeviceBuffer *TextureManager::CreateTransferSourceBuffer(const VkDeviceSize size,const void *data) { - const VkFormatProperties fp=attr->physical_device->GetFormatProperties(format); + if(size<=0) + return(nullptr); + + return GetDevice()->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,size,data); +} + +bool TextureManager::CheckFormatSupport(const VkFormat format,const uint32_t bits,ImageTiling tiling) const +{ + const VkFormatProperties fp=GetFormatProperties(format); if(tiling==ImageTiling::Optimal) return(fp.optimalTilingFeatures&bits); @@ -14,7 +23,7 @@ bool GPUDevice::CheckFormatSupport(const VkFormat format,const uint32_t bits,Ima return(fp.linearTilingFeatures&bits); } -void GPUDevice::Clear(TextureCreateInfo *tci) +void TextureManager::Clear(TextureCreateInfo *tci) { if(!tci)return; @@ -25,7 +34,7 @@ void GPUDevice::Clear(TextureCreateInfo *tci) delete tci; } -bool GPUDevice::CopyBufferToImage(const CopyBufferToImageInfo *info,VkPipelineStageFlags destinationStage) +bool TextureManager::CopyBufferToImage(const CopyBufferToImageInfo *info,VkPipelineStageFlags destinationStage) { if(!info) return(false); @@ -75,7 +84,7 @@ bool GPUDevice::CopyBufferToImage(const CopyBufferToImageInfo *info,VkPipelineSt return(true); } -bool GPUDevice::CopyBufferToImage(Texture *tex,DeviceBuffer *buf,const VkBufferImageCopy *buffer_image_copy,const int count,const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags destinationStage) +bool TextureManager::CopyBufferToImage(Texture *tex,DeviceBuffer *buf,const VkBufferImageCopy *buffer_image_copy,const int count,const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags destinationStage) { if(!tex||!buf) return(false); @@ -97,7 +106,7 @@ bool GPUDevice::CopyBufferToImage(Texture *tex,DeviceBuffer *buf,const VkBufferI return CopyBufferToImage(&info,destinationStage); } -bool GPUDevice::SubmitTexture(const VkCommandBuffer *cmd_bufs,const uint32_t count) +bool TextureManager::SubmitTexture(const VkCommandBuffer *cmd_bufs,const uint32_t count) { if(!cmd_bufs||count<=0) return(false); diff --git a/src/SceneGraph/Vulkan/Texture/VKDeviceTexture2D.cpp b/src/SceneGraph/Vulkan/Texture/VKDeviceTexture2D.cpp index 21021aef..47811bfd 100644 --- a/src/SceneGraph/Vulkan/Texture/VKDeviceTexture2D.cpp +++ b/src/SceneGraph/Vulkan/Texture/VKDeviceTexture2D.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -7,15 +7,19 @@ VK_NAMESPACE_BEGIN void GenerateMipmaps(TextureCmdBuffer *texture_cmd_buf,VkImage image,VkImageAspectFlags aspect_mask,VkExtent3D extent,const uint32_t mipLevels,const uint32_t layer_count); -Texture2D *GPUDevice::CreateTexture2D(TextureData *tex_data) +Texture2D *TextureManager::CreateTexture2D(TextureData *tex_data) { if(!tex_data) return(nullptr); + + Texture2D *tex=new Texture2D(this,AcquireID(),tex_data); - return(new Texture2D(attr->device,tex_data)); + Add(tex); + + return tex; } -Texture2D *GPUDevice::CreateTexture2D(TextureCreateInfo *tci) +Texture2D *TextureManager::CreateTexture2D(TextureCreateInfo *tci) { if(!tci)return(nullptr); @@ -39,11 +43,11 @@ Texture2D *GPUDevice::CreateTexture2D(TextureCreateInfo *tci) return(nullptr); } - tci->memory=CreateMemory(tci->image); + tci->memory=GetDevice()->CreateMemory(tci->image); } if(!tci->image_view) - tci->image_view=CreateImageView2D(attr->device,tci->format,tci->extent,tci->target_mipmaps,tci->aspect,tci->image); + tci->image_view=CreateImageView2D(GetVkDevice(),tci->format,tci->extent,tci->target_mipmaps,tci->aspect,tci->image); TextureData *tex_data=new TextureData(tci); @@ -56,7 +60,7 @@ Texture2D *GPUDevice::CreateTexture2D(TextureCreateInfo *tci) } if((!tci->buffer)&&tci->pixels&&tci->total_bytes>0) - tci->buffer=CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,tci->total_bytes,tci->pixels); + tci->buffer=CreateTransferSourceBuffer(tci->total_bytes,tci->pixels); if(tci->buffer) { @@ -89,7 +93,7 @@ Texture2D *GPUDevice::CreateTexture2D(TextureCreateInfo *tci) return tex; } -bool GPUDevice::CommitTexture2D(Texture2D *tex,DeviceBuffer *buf,VkPipelineStageFlags destinationStage) +bool TextureManager::CommitTexture2D(Texture2D *tex,DeviceBuffer *buf,VkPipelineStageFlags destinationStage) { if(!tex||!buf)return(false); @@ -98,7 +102,7 @@ bool GPUDevice::CommitTexture2D(Texture2D *tex,DeviceBuffer *buf,VkPipelineStage return CopyBufferToImage2D(tex,buf,&buffer_image_copy,destinationStage); } -bool GPUDevice::CommitTexture2DMipmaps(Texture2D *tex,DeviceBuffer *buf,const VkExtent3D &extent,uint32_t total_bytes) +bool TextureManager::CommitTexture2DMipmaps(Texture2D *tex,DeviceBuffer *buf,const VkExtent3D &extent,uint32_t total_bytes) { if(!tex||!buf ||extent.width*extent.height<=0) @@ -144,7 +148,7 @@ bool GPUDevice::CommitTexture2DMipmaps(Texture2D *tex,DeviceBuffer *buf,const Vk return CopyBufferToImage2D(tex,buf,buffer_image_copy,miplevel,VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); } -bool GPUDevice::ChangeTexture2D(Texture2D *tex,DeviceBuffer *buf,const List &ir_list,VkPipelineStageFlags destinationStage) +bool TextureManager::ChangeTexture2D(Texture2D *tex,DeviceBuffer *buf,const List &ir_list,VkPipelineStageFlags destinationStage) { if(!tex||!buf||ir_list.GetCount()<=0) return(false); @@ -184,7 +188,7 @@ bool GPUDevice::ChangeTexture2D(Texture2D *tex,DeviceBuffer *buf,const Listtex->GetWidth() ||scope.GetBottom()>tex->GetHeight()) return(false); - - DeviceBuffer *buf=CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,size,data); + + DeviceBuffer *buf=CreateTransferSourceBuffer(size,data); bool result=ChangeTexture2D(tex,buf,scope,destinationStage); diff --git a/src/SceneGraph/Vulkan/Texture/VKDeviceTexture2DArray.cpp b/src/SceneGraph/Vulkan/Texture/VKDeviceTexture2DArray.cpp index 8bde8b2d..b8176f07 100644 --- a/src/SceneGraph/Vulkan/Texture/VKDeviceTexture2DArray.cpp +++ b/src/SceneGraph/Vulkan/Texture/VKDeviceTexture2DArray.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -7,15 +7,19 @@ VK_NAMESPACE_BEGIN void GenerateMipmaps(TextureCmdBuffer *texture_cmd_buf,VkImage image,VkImageAspectFlags aspect_mask,VkExtent3D extent,const uint32_t mipLevels,const uint32_t layer_count); -Texture2DArray *GPUDevice::CreateTexture2DArray(TextureData *tex_data) +Texture2DArray *TextureManager::CreateTexture2DArray(TextureData *tex_data) { if(!tex_data) return(nullptr); - return(new Texture2DArray(attr->device,tex_data)); + Texture2DArray *tex=new Texture2DArray(this,AcquireID(),tex_data); + + Add(tex); + + return tex; } -Texture2DArray *GPUDevice::CreateTexture2DArray(TextureCreateInfo *tci) +Texture2DArray *TextureManager::CreateTexture2DArray(TextureCreateInfo *tci) { if(!tci)return(nullptr); @@ -38,12 +42,12 @@ Texture2DArray *GPUDevice::CreateTexture2DArray(TextureCreateInfo *tci) Clear(tci); return(nullptr); } - - tci->memory=CreateMemory(tci->image); + + tci->memory=GetDevice()->CreateMemory(tci->image); } if(!tci->image_view) - tci->image_view=CreateImageView2DArray(attr->device,tci->format,tci->extent,tci->target_mipmaps,tci->aspect,tci->image); + tci->image_view=CreateImageView2DArray(GetVkDevice(),tci->format,tci->extent,tci->target_mipmaps,tci->aspect,tci->image); TextureData *tex_data=new TextureData(tci); @@ -91,7 +95,7 @@ Texture2DArray *GPUDevice::CreateTexture2DArray(TextureCreateInfo *tci) return tex; } -Texture2DArray *GPUDevice::CreateTexture2DArray(const uint32_t w,const uint32_t h,const uint32 l,const VkFormat fmt,const bool mipmaps) +Texture2DArray *TextureManager::CreateTexture2DArray(const uint32_t w,const uint32_t h,const uint32 l,const VkFormat fmt,const bool mipmaps) { if(w*h*l<=0) return(nullptr); @@ -204,7 +208,7 @@ Texture2DArray *GPUDevice::CreateTexture2DArray(const uint32_t w,const uint32_t // return result; //} -bool GPUDevice::ChangeTexture2DArray(Texture2DArray *tex,DeviceBuffer *buf,const RectScope2ui &scope,const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags destinationStage) +bool TextureManager::ChangeTexture2DArray(Texture2DArray *tex,DeviceBuffer *buf,const RectScope2ui &scope,const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags destinationStage) { if(!tex||!buf ||base_layer<0 @@ -224,7 +228,7 @@ bool GPUDevice::ChangeTexture2DArray(Texture2DArray *tex,DeviceBuffer *buf,const return result; } -bool GPUDevice::ChangeTexture2DArray(Texture2DArray *tex,void *data,const uint32_t size,const RectScope2ui &scope,const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags destinationStage) +bool TextureManager::ChangeTexture2DArray(Texture2DArray *tex,const void *data,const VkDeviceSize size,const RectScope2ui &scope,const uint32_t base_layer,const uint32_t layer_count,VkPipelineStageFlags destinationStage) { if(!tex||!data ||size<=0 @@ -235,8 +239,8 @@ bool GPUDevice::ChangeTexture2DArray(Texture2DArray *tex,void *data,const uint32 ||scope.GetRight()>tex->GetWidth() ||scope.GetBottom()>tex->GetHeight()) return(false); - - DeviceBuffer *buf=CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,size,data); + + DeviceBuffer *buf=CreateTransferSourceBuffer(size,data); bool result=ChangeTexture2DArray(tex,buf,scope,base_layer,layer_count,destinationStage); diff --git a/src/SceneGraph/Vulkan/Texture/VKDeviceTextureCube.cpp b/src/SceneGraph/Vulkan/Texture/VKDeviceTextureCube.cpp index 614a85b0..a6347326 100644 --- a/src/SceneGraph/Vulkan/Texture/VKDeviceTextureCube.cpp +++ b/src/SceneGraph/Vulkan/Texture/VKDeviceTextureCube.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -6,15 +6,19 @@ VK_NAMESPACE_BEGIN void GenerateMipmaps(TextureCmdBuffer *texture_cmd_buf,VkImage image,VkImageAspectFlags aspect_mask,VkExtent3D extent,const uint32_t mipLevels,const uint32_t layer_count); -TextureCube *GPUDevice::CreateTextureCube(TextureData *tex_data) +TextureCube *TextureManager::CreateTextureCube(TextureData *tex_data) { if(!tex_data) return(nullptr); - return(new TextureCube(attr->device,tex_data)); + TextureCube *tex=new TextureCube(this,AcquireID(),tex_data); + + Add(tex); + + return tex; } -TextureCube *GPUDevice::CreateTextureCube(TextureCreateInfo *tci) +TextureCube *TextureManager::CreateTextureCube(TextureCreateInfo *tci) { if(!tci)return(nullptr); @@ -33,12 +37,12 @@ TextureCube *GPUDevice::CreateTextureCube(TextureCreateInfo *tci) Clear(tci); return(nullptr); } - - tci->memory=CreateMemory(tci->image); + + tci->memory=GetDevice()->CreateMemory(tci->image); } if(!tci->image_view) - tci->image_view=CreateImageViewCube(attr->device,tci->format,tci->extent,tci->target_mipmaps,tci->aspect,tci->image); + tci->image_view=CreateImageViewCube(GetVkDevice(),tci->format,tci->extent,tci->target_mipmaps,tci->aspect,tci->image); TextureData *tex_data=new TextureData(tci); @@ -51,24 +55,24 @@ TextureCube *GPUDevice::CreateTextureCube(TextureCreateInfo *tci) } if((!tci->buffer)&&tci->pixels&&tci->total_bytes>0) - tci->buffer=CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,tci->total_bytes,tci->pixels); + tci->buffer=CreateTransferSourceBuffer(tci->total_bytes,tci->pixels); if(tci->buffer) { texture_cmd_buf->Begin(); if(tci->target_mipmaps==tci->origin_mipmaps) { - if(tci->target_mipmaps<=1) //mipmapsҲҪmipmaps + if(tci->target_mipmaps<=1) //�������mipmaps����Ҳ��Ҫmipmaps { CommitTextureCube(tex,tci->buffer,tci->mipmap_zero_total_bytes,VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); } - else //mipmaps + else //������mipmaps���� { CommitTextureCubeMipmaps(tex,tci->buffer,tci->extent,tci->mipmap_zero_total_bytes); } } else - if(tci->origin_mipmaps<=1) //mipmaps,Ҫmipmaps + if(tci->origin_mipmaps<=1) //�������mipmaps����,����Ҫmipmaps { CommitTextureCube(tex,tci->buffer,tci->mipmap_zero_total_bytes,VK_PIPELINE_STAGE_TRANSFER_BIT); GenerateMipmaps(texture_cmd_buf,tex->GetImage(),tex->GetAspect(),tci->extent,tex_data->miplevel,6); @@ -84,7 +88,7 @@ TextureCube *GPUDevice::CreateTextureCube(TextureCreateInfo *tci) return tex; } -bool GPUDevice::CommitTextureCube(TextureCube *tex,DeviceBuffer *buf,const uint32_t mipmaps_zero_bytes,VkPipelineStageFlags destinationStage) +bool TextureManager::CommitTextureCube(TextureCube *tex,DeviceBuffer *buf,const uint32_t mipmaps_zero_bytes,VkPipelineStageFlags destinationStage) { if(!tex||!buf||!mipmaps_zero_bytes)return(false); @@ -93,7 +97,7 @@ bool GPUDevice::CommitTextureCube(TextureCube *tex,DeviceBuffer *buf,const uint3 return CopyBufferToImageCube(tex,buf,&buffer_image_copy,destinationStage); } -bool GPUDevice::CommitTextureCubeMipmaps(TextureCube *tex,DeviceBuffer *buf,const VkExtent3D &extent,uint32_t total_bytes) +bool TextureManager::CommitTextureCubeMipmaps(TextureCube *tex,DeviceBuffer *buf,const VkExtent3D &extent,uint32_t total_bytes) { if(!tex||!buf ||extent.width*extent.height<=0) diff --git a/src/SceneGraph/Vulkan/Texture/VKTexture2DArrayLoader.cpp b/src/SceneGraph/Vulkan/Texture/VKTexture2DArrayLoader.cpp index ee297393..828c50ae 100644 --- a/src/SceneGraph/Vulkan/Texture/VKTexture2DArrayLoader.cpp +++ b/src/SceneGraph/Vulkan/Texture/VKTexture2DArrayLoader.cpp @@ -1,6 +1,8 @@ #include"VKTextureLoader.h" #include #include +#include +#include VK_NAMESPACE_BEGIN //template<> void VkTextureLoader::OnExtent(VkExtent3D &extent) @@ -15,10 +17,13 @@ VK_NAMESPACE_BEGIN // return device->CreateTexture2DArray(tci); //} -bool LoadTexture2DLayerFromFile(GPUDevice *device,Texture2DArray *ta,const uint32_t layer,const OSString &filename,bool auto_mipmaps) +bool LoadTexture2DLayerFromFile(TextureManager *tm,Texture2DArray *ta,const uint32_t layer,const OSString &filename,bool auto_mipmaps) { + if(!tm||filename.IsEmpty()) + return(false); + //注:依然是Texture2D,则非Texture2DArray。因为这里LOAD的是2D纹理,并不是2DArray纹理 - VkTextureLoader loader(device,auto_mipmaps); + VkTextureLoader loader(tm,auto_mipmaps); if(!loader.Load(filename)) return(false); @@ -33,6 +38,6 @@ bool LoadTexture2DLayerFromFile(GPUDevice *device,Texture2DArray *ta,const uint3 scope.Width=ta->GetWidth(); scope.Height=ta->GetHeight(); - return device->ChangeTexture2DArray(ta,buf,scope,layer,1); + return tm->ChangeTexture2DArray(ta,buf,scope,layer,1); } VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/Texture/VKTexture2DLoader.cpp b/src/SceneGraph/Vulkan/Texture/VKTexture2DLoader.cpp index f0a6211d..a01e5ea2 100644 --- a/src/SceneGraph/Vulkan/Texture/VKTexture2DLoader.cpp +++ b/src/SceneGraph/Vulkan/Texture/VKTexture2DLoader.cpp @@ -12,12 +12,15 @@ template<> void VkTextureLoader::OnExtent(VkExtent3D template<> Texture2D *VkTextureLoader::OnCreateTexture(TextureCreateInfo *tci) { - return device->CreateTexture2D(tci); + return tex_manager->CreateTexture2D(tci); } -Texture2D *CreateTexture2DFromFile(GPUDevice *device,const OSString &filename,bool auto_mipmaps) +Texture2D *CreateTexture2DFromFile(TextureManager *tm,const OSString &filename,bool auto_mipmaps) { - VkTextureLoader loader(device,auto_mipmaps); + if(!tm||filename.IsEmpty()) + return(nullptr); + + VkTextureLoader loader(tm,auto_mipmaps); if(!loader.Load(filename)) return(nullptr); diff --git a/src/SceneGraph/Vulkan/Texture/VKTextureCubeLoader.cpp b/src/SceneGraph/Vulkan/Texture/VKTextureCubeLoader.cpp index 452e9766..bcdd4ca6 100644 --- a/src/SceneGraph/Vulkan/Texture/VKTextureCubeLoader.cpp +++ b/src/SceneGraph/Vulkan/Texture/VKTextureCubeLoader.cpp @@ -12,12 +12,15 @@ template<> void VkTextureLoader::OnExtent(VkExten template<> TextureCube *VkTextureLoader::OnCreateTexture(TextureCreateInfo *tci) { - return device->CreateTextureCube(tci); + return tex_manager->CreateTextureCube(tci); } -TextureCube *CreateTextureCubeFromFile(GPUDevice *device,const OSString &filename,bool auto_mipmaps) +TextureCube *CreateTextureCubeFromFile(TextureManager *tm,const OSString &filename,bool auto_mipmaps) { - VkTextureLoader loader(device,auto_mipmaps); + if(!tm||filename.IsEmpty()) + return(nullptr); + + VkTextureLoader loader(tm,auto_mipmaps); if(!loader.Load(filename)) return(nullptr); diff --git a/src/SceneGraph/Vulkan/Texture/VKTextureLoader.h b/src/SceneGraph/Vulkan/Texture/VKTextureLoader.h index be268e31..4d2a7404 100644 --- a/src/SceneGraph/Vulkan/Texture/VKTextureLoader.h +++ b/src/SceneGraph/Vulkan/Texture/VKTextureLoader.h @@ -3,24 +3,25 @@ #include #include #include +#include #include VK_NAMESPACE_BEGIN template class VkTextureLoader:public TL { protected: - - GPUDevice *device; + + TextureManager *tex_manager; DeviceBuffer *buf; T *tex; bool auto_mipmaps; public: - - VkTextureLoader(GPUDevice *dev,const bool am) + + VkTextureLoader(TextureManager *tm,const bool am) { - device=dev; + tex_manager=tm; buf=nullptr; tex=nullptr; auto_mipmaps=am; @@ -38,8 +39,8 @@ public: if(!CheckVulkanFormat(tex_format)) return(nullptr); - - buf=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,total_bytes); + + buf=tex_manager->CreateTransferSourceBuffer(total_bytes); if(!buf) return(nullptr); @@ -75,7 +76,7 @@ public: if(auto_mipmaps&&tex_file_header.mipmaps<=1) { - if(device->CheckFormatSupport(tex_format,VK_FORMAT_FEATURE_BLIT_DST_BIT)) + if(tex_manager->CheckFormatSupport(tex_format,VK_FORMAT_FEATURE_BLIT_DST_BIT)) { tci->usage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT; tci->SetAutoMipmaps(); diff --git a/src/SceneGraph/Vulkan/VKDeviceFramebuffer.cpp b/src/SceneGraph/Vulkan/VKDeviceFramebuffer.cpp index 61200448..95cf1a09 100644 --- a/src/SceneGraph/Vulkan/VKDeviceFramebuffer.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceFramebuffer.cpp @@ -1,4 +1,4 @@ -#include +#include VK_NAMESPACE_BEGIN VkFramebuffer CreateVulkanFramebuffer(VkDevice device,RenderPass *rp,const VkExtent2D &extent,VkImageView *attachments,const uint attachmentCount) @@ -20,7 +20,7 @@ VkFramebuffer CreateVulkanFramebuffer(VkDevice device,RenderPass *rp,const VkExt return fb; } -Framebuffer *GPUDevice::CreateFBO(RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth) +Framebuffer *RenderTargetManager::CreateFBO(RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth) { uint att_count=color_count; @@ -70,15 +70,15 @@ Framebuffer *GPUDevice::CreateFBO(RenderPass *rp,ImageView **color_list,const ui extent.height=color_list[0]->GetExtent().height; } - VkFramebuffer fbo=CreateVulkanFramebuffer(GetDevice(),rp,extent,attachments,att_count); + VkFramebuffer fbo=CreateVulkanFramebuffer(GetVkDevice(),rp,extent,attachments,att_count); if(!fbo) return(nullptr); - return(new Framebuffer(GetDevice(),fbo,extent,rp->GetVkRenderPass(),color_count,depth)); + return(new Framebuffer(GetVkDevice(),fbo,extent,rp->GetVkRenderPass(),color_count,depth)); } // -//Framebuffer *GPUDevice::CreateFBO(RenderPass *rp,List &color,ImageView *depth) +//Framebuffer *RenderTargetManager::CreateFBO(RenderPass *rp,List &color,ImageView *depth) //{ // if(!rp)return(nullptr); // @@ -89,7 +89,7 @@ Framebuffer *GPUDevice::CreateFBO(RenderPass *rp,ImageView **color_list,const ui // return CreateFBO(rp,color.GetData(),color.GetCount(),depth); //} -Framebuffer *GPUDevice::CreateFBO(RenderPass *rp,ImageView *color,ImageView *depth) +Framebuffer *RenderTargetManager::CreateFBO(RenderPass *rp,ImageView *color,ImageView *depth) { if(!rp)return(nullptr); if(!color&&!depth)return(nullptr); @@ -97,7 +97,7 @@ Framebuffer *GPUDevice::CreateFBO(RenderPass *rp,ImageView *color,ImageView *dep return CreateFBO(rp,&color,1,depth); } -Framebuffer *GPUDevice::CreateFBO(RenderPass *rp,ImageView *iv) +Framebuffer *RenderTargetManager::CreateFBO(RenderPass *rp,ImageView *iv) { if(!rp)return(nullptr); if(!iv)return(nullptr); diff --git a/src/SceneGraph/Vulkan/VKDeviceImage.cpp b/src/SceneGraph/Vulkan/VKDeviceImage.cpp index fda808d7..f4c92e73 100644 --- a/src/SceneGraph/Vulkan/VKDeviceImage.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceImage.cpp @@ -1,8 +1,8 @@ -#include +#include #include VK_NAMESPACE_BEGIN -VkImage GPUDevice::CreateImage(VkImageCreateInfo *ici) +VkImage TextureManager::CreateImage(VkImageCreateInfo *ici) { if(!ici)return(VK_NULL_HANDLE); if(!CheckVulkanFormat(ici->format))return(VK_NULL_HANDLE); @@ -10,16 +10,16 @@ VkImage GPUDevice::CreateImage(VkImageCreateInfo *ici) VkImage image; - if(vkCreateImage(attr->device,ici, nullptr, &image)!=VK_SUCCESS) + if(vkCreateImage(GetVkDevice(),ici, nullptr, &image)!=VK_SUCCESS) return(nullptr); return image; } -void GPUDevice::DestroyImage(VkImage img) +void TextureManager::DestroyImage(VkImage img) { if(img==VK_NULL_HANDLE)return; - vkDestroyImage(attr->device,img,nullptr); + vkDestroyImage(GetVkDevice(),img,nullptr); } VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp b/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp deleted file mode 100644 index d7df9916..00000000 --- a/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include - -VK_NAMESPACE_BEGIN -RenderTarget *GPUDevice::CreateRT(const FramebufferInfo *fbi,RenderPass *rp,const uint32_t fence_count) -{ - if(!fbi)return(nullptr); - if(!rp)return(nullptr); - - const uint32_t color_count=fbi->GetColorCount(); - const VkExtent2D extent=fbi->GetExtent(); - const VkFormat depth_format=fbi->GetDepthFormat(); - - AutoDeleteObjectArray color_texture_list(color_count); - AutoDeleteArray color_iv_list(color_count); //iv只是从Texture2D中取出来的,无需一个个delete - - Texture2D **tp=color_texture_list; - ImageView **iv=color_iv_list; - - for(const VkFormat &fmt:fbi->GetColorFormatList()) - { - Texture2D *color_texture=CreateTexture2D(new ColorAttachmentTextureCreateInfo(fmt,extent)); - - if(!color_texture) - return(nullptr); - - *tp++=color_texture; - *iv++=color_texture->GetImageView(); - } - - Texture2D *depth_texture=(depth_format!=PF_UNDEFINED)?CreateTexture2D(new DepthAttachmentTextureCreateInfo(depth_format,extent)):nullptr; - - Framebuffer *fb=CreateFBO(rp,color_iv_list,color_count,depth_texture?depth_texture->GetImageView():nullptr); - - if(fb) - { - DeviceQueue *q=CreateQueue(fence_count,false); - Semaphore *render_complete_semaphore=CreateGPUSemaphore(); - - RenderTarget *rt=new RenderTarget(q,render_complete_semaphore,rp,fb,color_texture_list,color_count,depth_texture); - - color_texture_list.DiscardObject(); - return rt; - } - - SAFE_CLEAR(depth_texture); - return nullptr; -} - -RenderTarget *GPUDevice::CreateRT(const FramebufferInfo *fbi,const uint32_t fence_count) -{ - if(!fbi)return(nullptr); - - RenderPass *rp=AcquireRenderPass(fbi); - - if(!rp)return(nullptr); - - return CreateRT(fbi,rp,fence_count); -} - -RTSwapchain *GPUDevice::CreateSwapchainRenderTarget() -{ - Swapchain *sc=CreateSwapchain(attr->surface_caps.currentExtent); - - if(!sc) - return(nullptr); - - DeviceQueue *q=CreateQueue(sc->color_count,false); - Semaphore *render_complete_semaphore=CreateGPUSemaphore(); - Semaphore *present_complete_semaphore=CreateGPUSemaphore(); - - RTSwapchain *srt=new RTSwapchain( attr->device, - sc, - q, - render_complete_semaphore, - present_complete_semaphore, - device_render_pass - ); - - return srt; -} -VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp b/src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp deleted file mode 100644 index c0686a23..00000000 --- a/src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp +++ /dev/null @@ -1,155 +0,0 @@ -#include -#include -#include -#include - -VK_NAMESPACE_BEGIN -namespace -{ - //VkExtent2D SwapchainExtentClamp(const VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent) - //{ - // VkExtent2D swapchain_extent; - - // swapchain_extent.width =hgl_clamp(acquire_extent.width, surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width ); - // swapchain_extent.height =hgl_clamp(acquire_extent.height, surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height ); - - // return swapchain_extent; - //} - - VkSwapchainKHR CreateSwapChain(const GPUDeviceAttribute *dev_attr,const VkExtent2D &extent) - { - VkSwapchainCreateInfoKHR swapchain_ci; - - swapchain_ci.sType =VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; - swapchain_ci.pNext =nullptr; - swapchain_ci.flags =0; - swapchain_ci.surface =dev_attr->surface; - swapchain_ci.minImageCount =3;//rsa->surface_caps.minImageCount; - swapchain_ci.imageFormat =dev_attr->surface_format.format; - swapchain_ci.imageColorSpace =dev_attr->surface_format.colorSpace; - swapchain_ci.imageExtent =extent; - swapchain_ci.imageArrayLayers =1; - swapchain_ci.imageUsage =VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - swapchain_ci.queueFamilyIndexCount =0; - swapchain_ci.pQueueFamilyIndices =nullptr; - swapchain_ci.preTransform =dev_attr->preTransform; - swapchain_ci.compositeAlpha =dev_attr->compositeAlpha; - swapchain_ci.presentMode =VK_PRESENT_MODE_FIFO_KHR; - swapchain_ci.clipped =VK_TRUE; - swapchain_ci.oldSwapchain =VK_NULL_HANDLE; - - if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_SRC_BIT) - swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT; - - if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_DST_BIT) - swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_DST_BIT; - - uint32_t queueFamilyIndices[2]={dev_attr->graphics_family, dev_attr->present_family}; - if(dev_attr->graphics_family!=dev_attr->present_family) - { - // If the graphics and present queues are from different queue families, - // we either have to explicitly transfer ownership of images between - // the queues, or we have to create the swapchain with imageSharingMode - // as VK_SHARING_MODE_CONCURRENT - swapchain_ci.imageSharingMode=VK_SHARING_MODE_CONCURRENT; - swapchain_ci.queueFamilyIndexCount=2; - swapchain_ci.pQueueFamilyIndices=queueFamilyIndices; - } - else - { - swapchain_ci.imageSharingMode = VkSharingMode(SharingMode::Exclusive); - } - - VkSwapchainKHR swap_chain; - VkResult result; - - result=vkCreateSwapchainKHR(dev_attr->device,&swapchain_ci,nullptr,&swap_chain); - - if(result!=VK_SUCCESS) - { - //LOG_ERROR(OS_TEXT("vkCreateSwapchainKHR failed, result = ")+OSString(result)); - os_err<<"vkCreateSwapchainKHR failed, result="<debug_utils) - dev_attr->debug_utils->SetSwapchainKHR(swap_chain,"SwapChain"); - #endif//_DEBUG - - return(swap_chain); - } -}//namespace - -bool GPUDevice::CreateSwapchainFBO(Swapchain *swapchain) -{ - if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->color_count),nullptr)!=VK_SUCCESS) - return(false); - - AutoDeleteArray sc_images(swapchain->color_count); - - if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->color_count),sc_images)!=VK_SUCCESS) - return(false); - - swapchain->sc_depth =CreateTexture2D(new SwapchainDepthTextureCreateInfo(attr->physical_device->GetDepthFormat(),swapchain->extent)); - - if(!swapchain->sc_depth) - return(false); - - #ifdef _DEBUG - if(attr->debug_utils) - { - attr->debug_utils->SetImage(swapchain->sc_depth->GetImage(),"SwapchainDepthImage"); - attr->debug_utils->SetImageView(swapchain->sc_depth->GetVulkanImageView(),"SwapchainDepthImageView"); - attr->debug_utils->SetDeviceMemory(swapchain->sc_depth->GetDeviceMemory(),"SwapchainDepthMemory"); - } - #endif//_DEBUG - - swapchain->sc_color =hgl_zero_new(swapchain->color_count); - swapchain->sc_fbo =hgl_zero_new(swapchain->color_count); - - for(uint32_t i=0;icolor_count;i++) - { - swapchain->sc_color[i]=CreateTexture2D(new SwapchainColorTextureCreateInfo(attr->surface_format.format,swapchain->extent,sc_images[i])); - - if(!swapchain->sc_color[i]) - return(false); - - swapchain->sc_fbo[i]=CreateFBO( device_render_pass, - swapchain->sc_color[i]->GetImageView(), - swapchain->sc_depth->GetImageView()); - - #ifdef _DEBUG - if(attr->debug_utils) - { - attr->debug_utils->SetImage(swapchain->sc_color[i]->GetImage(),"SwapchainColorImage_"+AnsiString::numberOf(i)); - attr->debug_utils->SetImageView(swapchain->sc_color[i]->GetVulkanImageView(),"SwapchainColorImageView_"+AnsiString::numberOf(i)); - - attr->debug_utils->SetFramebuffer(swapchain->sc_fbo[i]->GetFramebuffer(),"SwapchainFBO_"+AnsiString::numberOf(i)); - } - #endif//_DEBUG - } - - return(true); -} - -Swapchain *GPUDevice::CreateSwapchain(const VkExtent2D &acquire_extent) -{ - Swapchain *swapchain=new Swapchain; - - swapchain->device =attr->device; - swapchain->extent =acquire_extent; - - swapchain->swap_chain =CreateSwapChain(attr,acquire_extent); - - if(swapchain->swap_chain) - if(CreateSwapchainFBO(swapchain)) - return(swapchain); - - delete swapchain; - swapchain=nullptr; - - return(nullptr); -} -VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKRenderResource.cpp b/src/SceneGraph/Vulkan/VKRenderResource.cpp index a0499def..7edcc238 100644 --- a/src/SceneGraph/Vulkan/VKRenderResource.cpp +++ b/src/SceneGraph/Vulkan/VKRenderResource.cpp @@ -136,90 +136,4 @@ Sampler *RenderResource::CreateSampler(Texture *tex) return s; } - -Texture2D *CreateTexture2DFromFile(GPUDevice *device,const OSString &filename,bool auto_mipmaps); - -Texture2D *RenderResource::LoadTexture2D(const OSString &filename,bool auto_mipmaps) -{ - Texture2D *tex; - - if(texture_by_name.Get(filename,(Texture *&)tex)) - return tex; - - tex=CreateTexture2DFromFile(device,filename,auto_mipmaps); - - if(tex) - { - texture_by_name.Add(filename,tex); - Add(tex); - - #ifdef _DEBUG - DebugUtils *du=device->GetDebugUtils(); - - if(du) - { - const U8String name=U8_TEXT("Tex2D:")+ToU8String(filename); - - du->SetImage(tex->GetImage(),(char *)(name.c_str())); - } - #endif//_DEBUG - } - - return tex; -} - -Texture2DArray *RenderResource::CreateTexture2DArray(const AnsiString &name,const uint32_t width,const uint32_t height,const uint32_t layer,const VkFormat &fmt,bool auto_mipmaps) -{ - Texture2DArray *ta=device->CreateTexture2DArray(width,height,layer,fmt,auto_mipmaps); - - if(ta) - Add(ta); - else - return nullptr; - - #ifdef _DEBUG - DebugUtils *du=device->GetDebugUtils(); - - if(du) - { - du->SetImage(ta->GetImage(),"Tex2DArrayImage:"+name); - du->SetImageView(ta->GetVulkanImageView(),"Tex2DArrayImageView:"+name); - du->SetDeviceMemory(ta->GetDeviceMemory(),"Tex2DArrayMemory:"+name); - } - #endif//_DEBUG - - return ta; -} - -bool LoadTexture2DLayerFromFile(GPUDevice *device,Texture2DArray *t2d,const uint32_t layer,const OSString &filename,bool auto_mipmaps); - -bool RenderResource::LoadTexture2DToArray(Texture2DArray *ta,const uint32_t layer,const OSString &filename) -{ - if(!ta)return(false); - - if(!LoadTexture2DLayerFromFile(device,ta,layer,filename,false)) - return(false); - - return(true); -} - -TextureCube *CreateTextureCubeFromFile(GPUDevice *device,const OSString &filename,bool auto_mipmaps); - -TextureCube *RenderResource::LoadTextureCube(const OSString &filename,bool auto_mipmaps) -{ - TextureCube *tex; - - if(texture_by_name.Get(filename,(Texture *&)tex)) - return tex; - - tex=CreateTextureCubeFromFile(device,filename,auto_mipmaps); - - if(tex) - { - texture_by_name.Add(filename,tex); - Add(tex); - } - - return tex; -} VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKTexture.cpp b/src/SceneGraph/Vulkan/VKTexture.cpp index b8cbe216..65f2d7ff 100644 --- a/src/SceneGraph/Vulkan/VKTexture.cpp +++ b/src/SceneGraph/Vulkan/VKTexture.cpp @@ -1,6 +1,7 @@ #include #include #include +#include VK_NAMESPACE_BEGIN Texture::~Texture() { @@ -14,7 +15,7 @@ Texture::~Texture() delete data->memory; if(data->image) - vkDestroyImage(device,data->image,nullptr); + vkDestroyImage(manager->GetVkDevice(),data->image,nullptr); } } VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKTileData.cpp b/src/SceneGraph/Vulkan/VKTileData.cpp index 9cea3f83..8fd3ded1 100644 --- a/src/SceneGraph/Vulkan/VKTileData.cpp +++ b/src/SceneGraph/Vulkan/VKTileData.cpp @@ -1,6 +1,6 @@ -#include -#include +#include #include +#include namespace { @@ -61,7 +61,7 @@ namespace }//namespace VK_NAMESPACE_BEGIN -TileData *GPUDevice::CreateTileData(const VkFormat format,const uint width,const uint height,const uint count) +TileData *TextureManager::CreateTileData(const VkFormat format,const uint width,const uint height,const uint count) { if(!CheckVulkanFormat(format)) return(nullptr); @@ -69,7 +69,7 @@ TileData *GPUDevice::CreateTileData(const VkFormat format,const uint width,const if(width<=0||height<=0||count<=0) return(nullptr); - const uint32_t max_2d_texture=attr->physical_device->GetMaxImage2D(); + const uint32_t max_2d_texture=GetPhysicalDevice()->GetMaxImage2D(); uint tex_width,tex_height; diff --git a/src/SceneGraph/Vulkan/VKTileFont.cpp b/src/SceneGraph/Vulkan/VKTileFont.cpp index bfabc10a..ab3cdfea 100644 --- a/src/SceneGraph/Vulkan/VKTileFont.cpp +++ b/src/SceneGraph/Vulkan/VKTileFont.cpp @@ -1,5 +1,7 @@ #include -#include +#include +#include +#include VK_NAMESPACE_BEGIN /** @@ -7,7 +9,7 @@ VK_NAMESPACE_BEGIN * @param f 字体需求信息 * @param limit_count 缓冲字符数量上限 */ -TileFont *GPUDevice::CreateTileFont(FontSource *fs,int limit_count) +TileFont *RenderFramework::CreateTileFont(FontSource *fs,int limit_count) { if(!fs)return(nullptr); @@ -15,7 +17,7 @@ TileFont *GPUDevice::CreateTileFont(FontSource *fs,int limit_count) if(limit_count<=0) { - const VkExtent2D &ext=GetSwapchainSize(); + const VkExtent2D &ext=swapchain_module->GetSwapchainSize(); limit_count=(ext.width/height)*(ext.height/height); //按全屏幕放满不一样的字符为上限 } @@ -23,7 +25,7 @@ TileFont *GPUDevice::CreateTileFont(FontSource *fs,int limit_count) if(!fs) return(nullptr); - TileData *td=CreateTileData(UPF_R8,height,height,limit_count); + TileData *td=texture_manager->CreateTileData(UPF_R8,height,height,limit_count); if(!td) return nullptr; diff --git a/src/SceneGraph/module/RenderTargetManager.cpp b/src/SceneGraph/module/RenderTargetManager.cpp new file mode 100644 index 00000000..00c91242 --- /dev/null +++ b/src/SceneGraph/module/RenderTargetManager.cpp @@ -0,0 +1,72 @@ +#include +#include +#include + +VK_NAMESPACE_BEGIN + +RenderTargetManager::RenderTargetManager(GPUDevice *dev,TextureManager *tm,RenderPassManager *rpm):GraphModuleInherit(dev,"RenderTargetManager") +{ + tex_manager=tm; + rp_manager=rpm; +} + +RenderTarget *RenderTargetManager::CreateRT(const FramebufferInfo *fbi,RenderPass *rp,const uint32_t fence_count) +{ + if(!fbi)return(nullptr); + if(!rp)return(nullptr); + + const uint32_t image_count=fbi->GetColorCount(); + const VkExtent2D extent=fbi->GetExtent(); + const VkFormat depth_format=fbi->GetDepthFormat(); + + AutoDeleteObjectArray color_texture_list(image_count); + AutoDeleteArray color_iv_list(image_count); //iv只是从Texture2D中取出来的,无需一个个delete + + Texture2D **tp=color_texture_list; + ImageView **iv=color_iv_list; + + for(const VkFormat &fmt:fbi->GetColorFormatList()) + { + Texture2D *color_texture=tex_manager->CreateTexture2D(new ColorAttachmentTextureCreateInfo(fmt,extent)); + + if(!color_texture) + return(nullptr); + + *tp++=color_texture; + *iv++=color_texture->GetImageView(); + } + + Texture2D *depth_texture=(depth_format!=PF_UNDEFINED)?tex_manager->CreateTexture2D(new DepthAttachmentTextureCreateInfo(depth_format,extent)):nullptr; + + Framebuffer *fb=CreateFBO(rp,color_iv_list,image_count,depth_texture?depth_texture->GetImageView():nullptr); + + if(fb) + { + auto *dev=GetDevice(); + + DeviceQueue *q=dev->CreateQueue(fence_count,false); + Semaphore *render_complete_semaphore=dev->CreateGPUSemaphore(); + + RenderTarget *rt=new RenderTarget(q,render_complete_semaphore,rp,fb,color_texture_list,image_count,depth_texture); + + color_texture_list.DiscardObject(); + return rt; + } + + SAFE_CLEAR(depth_texture); + return nullptr; +} + +RenderTarget *RenderTargetManager::CreateRT(const FramebufferInfo *fbi,const uint32_t fence_count) +{ + if(!fbi)return(nullptr); + if(!rp_manager)return(nullptr); //这个判断理论上不可能成立 + + RenderPass *rp=rp_manager->AcquireRenderPass(fbi); + + if(!rp)return(nullptr); + + return CreateRT(fbi,rp,fence_count); +} + +VK_NAMESPACE_END diff --git a/src/SceneGraph/module/SwapchainModule.cpp b/src/SceneGraph/module/SwapchainModule.cpp new file mode 100644 index 00000000..946bb88a --- /dev/null +++ b/src/SceneGraph/module/SwapchainModule.cpp @@ -0,0 +1,294 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +VK_NAMESPACE_BEGIN +namespace +{ + //VkExtent2D SwapchainExtentClamp(const VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent) + //{ + // VkExtent2D swapchain_extent; + + // swapchain_extent.width =hgl_clamp(acquire_extent.width, surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width ); + // swapchain_extent.height =hgl_clamp(acquire_extent.height, surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height ); + + // return swapchain_extent; + //} + + VkSwapchainKHR CreateVulkanSwapChain(const GPUDeviceAttribute *dev_attr) + { + VkSwapchainCreateInfoKHR swapchain_ci; + + swapchain_ci.sType =VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; + swapchain_ci.pNext =nullptr; + swapchain_ci.flags =0; + swapchain_ci.surface =dev_attr->surface; + swapchain_ci.minImageCount =dev_attr->surface_caps.minImageCount; + swapchain_ci.imageFormat =dev_attr->surface_format.format; + swapchain_ci.imageColorSpace =dev_attr->surface_format.colorSpace; + swapchain_ci.imageExtent =dev_attr->surface_caps.currentExtent; + swapchain_ci.imageArrayLayers =1; + swapchain_ci.imageUsage =VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + swapchain_ci.queueFamilyIndexCount =0; + swapchain_ci.pQueueFamilyIndices =nullptr; + swapchain_ci.preTransform =dev_attr->preTransform; + swapchain_ci.compositeAlpha =dev_attr->compositeAlpha; + swapchain_ci.presentMode =VK_PRESENT_MODE_FIFO_KHR; + swapchain_ci.clipped =VK_TRUE; + swapchain_ci.oldSwapchain =VK_NULL_HANDLE; + + if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_SRC_BIT) + swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT; + + if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_DST_BIT) + swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_DST_BIT; + + uint32_t queueFamilyIndices[2]={dev_attr->graphics_family, dev_attr->present_family}; + if(dev_attr->graphics_family!=dev_attr->present_family) + { + // If the graphics and present queues are from different queue families, + // we either have to explicitly transfer ownership of images between + // the queues, or we have to create the swapchain with imageSharingMode + // as VK_SHARING_MODE_CONCURRENT + swapchain_ci.imageSharingMode=VK_SHARING_MODE_CONCURRENT; + swapchain_ci.queueFamilyIndexCount=2; + swapchain_ci.pQueueFamilyIndices=queueFamilyIndices; + } + else + { + swapchain_ci.imageSharingMode = VkSharingMode(SharingMode::Exclusive); + } + + VkSwapchainKHR swap_chain; + VkResult result; + + result=vkCreateSwapchainKHR(dev_attr->device,&swapchain_ci,nullptr,&swap_chain); + + if(result!=VK_SUCCESS) + { + //LOG_ERROR(OS_TEXT("vkCreateSwapchainKHR failed, result = ")+OSString(result)); +// os_err<<"vkCreateSwapchainKHR failed, result="<debug_utils) + dev_attr->debug_utils->SetSwapchainKHR(swap_chain,"SwapChain"); + #endif//_DEBUG + + return(swap_chain); + } +}//namespace + +bool SwapchainModule::CreateSwapchainFBO() +{ + if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),nullptr)!=VK_SUCCESS) + return(false); + + AutoDeleteArray sc_images(swapchain->color_count); + + if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),sc_images)!=VK_SUCCESS) + return(false); + + { + auto sc_depth_tci=new SwapchainDepthTextureCreateInfo(GetPhysicalDevice()->GetDepthFormat(),swapchain->extent); + + swapchain->sc_depth =tex_manager->CreateTexture2D(sc_depth_tci); + + if(!swapchain->sc_depth) + return(false); + } + + //#ifdef _DEBUG + // if(dev_attr->debug_utils) + // { + // dev_attr->debug_utils->SetImage(swapchain->sc_depth->GetImage(),"SwapchainDepthImage"); + // dev_attr->debug_utils->SetImageView(swapchain->sc_depth->GetVulkanImageView(),"SwapchainDepthImageView"); + // dev_attr->debug_utils->SetDeviceMemory(swapchain->sc_depth->GetDeviceMemory(),"SwapchainDepthMemory"); + // } + //#endif//_DEBUG + + swapchain->sc_color =hgl_zero_new(swapchain->color_count); + swapchain->sc_fbo =hgl_zero_new(swapchain->color_count); + + for(uint32_t i=0;icolor_count;i++) + { + auto sc_color_tci=new SwapchainColorTextureCreateInfo(swapchain->surface_format.format,swapchain->extent,sc_images[i]); + + swapchain->sc_color[i]=tex_manager->CreateTexture2D(sc_color_tci); + + if(!swapchain->sc_color[i]) + return(false); + + swapchain->sc_fbo[i]=rt_manager->CreateFBO(swapchain_rp, + swapchain->sc_color[i]->GetImageView(), + swapchain->sc_depth->GetImageView()); + + //#ifdef _DEBUG + // if(dev_attr->debug_utils) + // { + // dev_attr->debug_utils->SetImage(swapchain->sc_color[i]->GetImage(),"SwapchainColorImage_"+AnsiString::numberOf(i)); + // dev_attr->debug_utils->SetImageView(swapchain->sc_color[i]->GetVulkanImageView(),"SwapchainColorImageView_"+AnsiString::numberOf(i)); + + // dev_attr->debug_utils->SetFramebuffer(swapchain->sc_fbo[i]->GetFramebuffer(),"SwapchainFBO_"+AnsiString::numberOf(i)); + // } + //#endif//_DEBUG + } + + return(true); +} + +bool SwapchainModule::CreateSwapchain() +{ + auto *dev_attr=GetDeviceAttribute(); + + if(!dev_attr) + return(false); + + swapchain=new Swapchain; + + swapchain->device =dev_attr->device; + swapchain->extent =dev_attr->surface_caps.currentExtent; + swapchain->transform =dev_attr->surface_caps.currentTransform; + swapchain->surface_format =dev_attr->surface_format; + swapchain->depth_format =dev_attr->physical_device->GetDepthFormat(); + + swapchain->swap_chain=CreateVulkanSwapChain(dev_attr); + + if(swapchain->swap_chain) + { + if(CreateSwapchainFBO()) + return swapchain; + } + + delete swapchain; + swapchain=nullptr; + return(false); +} + +bool SwapchainModule::CreateSwapchainRenderTarget() +{ + GPUDevice *device=GetDevice(); + + DeviceQueue *q=device->CreateQueue(swapchain->color_count,false); + Semaphore *render_complete_semaphore=device->CreateGPUSemaphore(); + Semaphore *present_complete_semaphore=device->CreateGPUSemaphore(); + + swapchain_rt=new RTSwapchain( device->GetDevice(), + swapchain, + q, + render_complete_semaphore, + present_complete_semaphore, + swapchain_rp + ); + + return true; +} + +void SwapchainModule::ClearRenderCmdBuffer() +{ + SAFE_CLEAR_OBJECT_ARRAY_OBJECT(cmd_buf,swapchain->color_count); +} + +void SwapchainModule::InitRenderCmdBuffer() +{ + ClearRenderCmdBuffer(); + + cmd_buf=hgl_zero_new(swapchain->color_count); + + GPUDevice *device=GetDevice(); + + AnsiString cmd_buf_name; + + for(uint32_t i=0;icolor_count;i++) + { + cmd_buf_name=device->GetPhysicalDevice()->GetDeviceName()+AnsiString(":RenderCmdBuffer_")+AnsiString::numberOf(i); + + cmd_buf[i]=device->CreateRenderCommandBuffer(cmd_buf_name); + } +} + +SwapchainModule::~SwapchainModule() +{ + ClearRenderCmdBuffer(); + + SAFE_CLEAR(swapchain_rp); + SAFE_CLEAR(swapchain_rt); + SAFE_CLEAR(swapchain); +} + +SwapchainModule::SwapchainModule(GPUDevice *dev,TextureManager *tm,RenderTargetManager *rtm,RenderPassManager *rpm):GraphModuleInherit(dev,"SwapchainModule") +{ + tex_manager=tm; + rt_manager=rtm; + + SwapchainRenderbufferInfo rbi(swapchain->surface_format.format,swapchain->depth_format); + + swapchain_rp=rpm->AcquireRenderPass(&rbi); + + if(!CreateSwapchain()) + return; + + //#ifdef _DEBUG + // if(dev_attr->debug_utils) + // dev_attr->debug_utils->SetRenderPass(swapchain_rp->GetVkRenderPass(),"SwapchainRenderPass"); + //#endif//_DEBUG + + if(!CreateSwapchainRenderTarget()) + return; + + InitRenderCmdBuffer(); +} + +void SwapchainModule::OnResize(const VkExtent2D &extent) +{ + ClearRenderCmdBuffer(); + + SAFE_CLEAR(swapchain_rt) + + GetDeviceAttribute()->RefreshSurfaceCaps(); + + CreateSwapchainRenderTarget(); + + InitRenderCmdBuffer(); +} + +bool SwapchainModule::BeginFrame() +{ + uint32_t index=swapchain_rt->AcquireNextImage(); + + if(index>=swapchain->color_count) + return(false); + + return(true); +} + +void SwapchainModule::EndFrame() +{ + int index=swapchain_rt->GetCurrentFrameIndices(); + + VkCommandBuffer cb=*(cmd_buf[index]); + + swapchain_rt->Submit(cb); + swapchain_rt->PresentBackbuffer(); + swapchain_rt->WaitQueue(); + swapchain_rt->WaitFence(); +} + +RenderCmdBuffer *SwapchainModule::GetRenderCmdBuffer() +{ + int index=swapchain_rt->GetCurrentFrameIndices(); + + if(index>=swapchain->color_count) + return(nullptr); + + return cmd_buf[index]; +} + +VK_NAMESPACE_END diff --git a/src/SceneGraph/module/TextureManager.cpp b/src/SceneGraph/module/TextureManager.cpp new file mode 100644 index 00000000..ea972ba9 --- /dev/null +++ b/src/SceneGraph/module/TextureManager.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include + +VK_NAMESPACE_BEGIN +const VkFormatProperties TextureManager::GetFormatProperties(const VkFormat format) const +{ + return GetPhysicalDevice()->GetFormatProperties(format); +} + +GRAPH_MODULE_CONSTRUCT(TextureManager) +{ + auto phy_device=GetPhysicalDevice(); + + texture_cmd_buf=dev->CreateTextureCommandBuffer(phy_device->GetDeviceName()+AnsiString(":TexCmdBuffer")); + + if(!texture_cmd_buf) + return; + + texture_queue=dev->CreateQueue(); + + if(!texture_queue) + return; + + texture_serial=0; +} + +TextureManager::~TextureManager() +{ + SAFE_CLEAR(texture_queue); + SAFE_CLEAR(texture_cmd_buf); +} + +const TextureID TextureManager::Add(Texture *tex) +{ + if(!tex) + return(-1); + + if(tex->GetManager()!=this) + return(-2); + + if(texture_set.Contains(tex)) + return tex->GetID(); + + texture_set.Add(tex); + texture_by_id.Add(tex->GetID(),tex); + + return tex->GetID(); +} + +const TextureID TextureManager::Add(Texture *tex,const OSString &tn) +{ + TextureID id=Add(tex); + + if(id<0) + return id; + + if(!tn.IsEmpty()) + texture_by_filename.Add(tn,tex); + + return id; +} + +void TextureManager::Release(Texture *tex) +{ + if(!tex) + return; + + if(!texture_set.Contains(tex)) + return; + + texture_set.Delete(tex); + texture_by_id.DeleteByKey(tex->GetID()); + texture_by_filename.DeleteByValue(tex); +} + +Texture2D *CreateTexture2DFromFile(TextureManager *tm,const OSString &filename,bool auto_mipmaps); + +Texture2D *TextureManager::LoadTexture2D(const OSString &filename,bool auto_mipmaps) +{ + Texture2D *tex; + + if(texture_by_filename.Get(filename,(Texture *&)tex)) + return tex; + + tex=CreateTexture2DFromFile(this,filename,auto_mipmaps); + + if(tex) + { + Add(tex,filename); + + //#ifdef _DEBUG + // DebugUtils *du=device->GetDebugUtils(); + + // if(du) + // { + // const U8String name=U8_TEXT("Tex2D:")+ToUTF8String(filename); + // + // du->SetImage(tex->GetImage(),(char *)(name.c_str())); + // } + //#endif//_DEBUG + } + + return tex; +} + +Texture2DArray *TextureManager::CreateTexture2DArray(const AnsiString &name,const uint32_t width,const uint32_t height,const uint32_t layer,const VkFormat &fmt,bool auto_mipmaps) +{ + Texture2DArray *ta=CreateTexture2DArray(width,height,layer,fmt,auto_mipmaps); + + if(ta) + Add(ta); + else + return nullptr; + + //#ifdef _DEBUG + // DebugUtils *du=device->GetDebugUtils(); + // + // if(du) + // { + // du->SetImage(ta->GetImage(),"Tex2DArrayImage:"+name); + // du->SetImageView(ta->GetVulkanImageView(),"Tex2DArrayImageView:"+name); + // du->SetDeviceMemory(ta->GetDeviceMemory(),"Tex2DArrayMemory:"+name); + // } + //#endif//_DEBUG + + return ta; +} + +bool LoadTexture2DLayerFromFile(TextureManager *tm,Texture2DArray *t2d,const uint32_t layer,const OSString &filename,bool auto_mipmaps); + +bool TextureManager::LoadTexture2DToArray(Texture2DArray *ta,const uint32_t layer,const OSString &filename) +{ + if(!ta)return(false); + + if(!LoadTexture2DLayerFromFile(this,ta,layer,filename,false)) + return(false); + + return(true); +} + +TextureCube *CreateTextureCubeFromFile(TextureManager *tm,const OSString &filename,bool auto_mipmaps); + +TextureCube *TextureManager::LoadTextureCube(const OSString &filename,bool auto_mipmaps) +{ + TextureCube *tex; + + if(texture_by_filename.Get(filename,(Texture *&)tex)) + return tex; + + tex=CreateTextureCubeFromFile(this,filename,auto_mipmaps); + + if(tex) + { + Add(tex,filename); + } + + return tex; +} + +VK_NAMESPACE_END