diff --git a/example/Vulkan/VulkanAppFramework.h b/example/Vulkan/VulkanAppFramework.h index be632f1f..8c72fb31 100644 --- a/example/Vulkan/VulkanAppFramework.h +++ b/example/Vulkan/VulkanAppFramework.h @@ -36,7 +36,6 @@ public: virtual ~VulkanApplicationFramework() { SAFE_CLEAR(shader_manage); - SAFE_CLEAR(device); SAFE_CLEAR(inst); SAFE_CLEAR(win); } @@ -62,7 +61,7 @@ public: if(!inst) return(false); - device=inst->CreateRenderDevice(win); + device=win->CreateRenderDevice(inst); if(!device) return(false); diff --git a/inc/hgl/graph/vulkan/VK.h b/inc/hgl/graph/vulkan/VK.h index 0c47f053..168f2787 100644 --- a/inc/hgl/graph/vulkan/VK.h +++ b/inc/hgl/graph/vulkan/VK.h @@ -4,8 +4,8 @@ #include #include #include -#include"VKFormat.h" -#include"VKPrimivate.h" +#include +#include #define VK_NAMESPACE hgl::graph::vulkan #define VK_NAMESPACE_BEGIN namespace hgl{namespace graph{namespace vulkan{ @@ -13,6 +13,32 @@ VK_NAMESPACE_BEGIN +class Instance; +struct PhysicalDevice; +class Device; +class ImageView; +class Framebuffer; + +class Buffer; +class VertexBuffer; +class IndexBuffer; + +class CommandBuffer; +class RenderPass; +class Fence; +class Semaphore; + +class ShaderModule; +class ShaderModuleManage; +class VertexShaderModule; +class Material; +class PipelineLayout; +class Pipeline; +class DescriptorSets; +class VertexAttributeBinding; + +class Renderable; + using CharPointerList=hgl::List; #ifdef _DEBUG diff --git a/inc/hgl/graph/vulkan/VKBuffer.h b/inc/hgl/graph/vulkan/VKBuffer.h index 9ae7c9f8..2c559617 100644 --- a/inc/hgl/graph/vulkan/VKBuffer.h +++ b/inc/hgl/graph/vulkan/VKBuffer.h @@ -1,7 +1,7 @@ #ifndef HGL_GRAPH_VULKAN_BUFFER_INCLUDE #define HGL_GRAPH_VULKAN_BUFFER_INCLUDE -#include"VK.h" +#include VK_NAMESPACE_BEGIN struct VulkanBuffer diff --git a/inc/hgl/graph/vulkan/VKBufferData.h b/inc/hgl/graph/vulkan/VKBufferData.h index 901ec28d..22e3336b 100644 --- a/inc/hgl/graph/vulkan/VKBufferData.h +++ b/inc/hgl/graph/vulkan/VKBufferData.h @@ -1,22 +1,22 @@ -#ifndef HGL_GRAPH_VULKAN_BUFFER_DATA_INCLUDE +#ifndef HGL_GRAPH_VULKAN_BUFFER_DATA_INCLUDE #define HGL_GRAPH_BUFFER_DATA_INCLUDE -#include"VK.h" +#include VK_NAMESPACE_BEGIN /** - * ݹ + * 缓冲区数据管理类 */ class BufferData { protected: - VkFormat format; ///<ݸʽ + VkFormat format; ///<数据格式 - uint32_t count; ///<ݸ - uint32_t stride; ///<ֽ + uint32_t count; ///<数据个数 + uint32_t stride; ///<单个数据字节数 - uint8_t * buffer_data; ///< - uint32_t total_bytes; ///<ֽ + uint8_t * buffer_data; ///<缓冲区数据 + uint32_t total_bytes; ///<数据总字节数 protected: @@ -37,10 +37,10 @@ VK_NAMESPACE_BEGIN virtual ~BufferData()=default; - uint GetStride ()const { return data_stride; } ///<ȡÿһֽ - uint32_t GetCount ()const { return data_count; } ///<ȡ - uint32_t GetTotalBytes ()const { return total_bytes; } ///<ȡֽ - void * GetData ()const { return buffer_data; } ///<ȡָ + uint GetStride ()const { return data_stride; } ///<取得每一组数据字节数 + uint32_t GetCount ()const { return data_count; } ///<取得数据数量 + uint32_t GetTotalBytes ()const { return total_bytes; } ///<取得数据总字节数 + void * GetData ()const { return buffer_data; } ///<取得数据指针 };//class BufferData BufferData *CreateBufferData(const uint32_t &length); @@ -58,8 +58,8 @@ VK_NAMESPACE_BEGIN #define DATA_COMPOMENT_DEPTH 0x10 /** - * Ǵ
- * δԿֱӶԳɷһ + * 非打包型数据
+ * 该类数据未被打包,所以可以直接对成份数据逐一访问 */ class BufferDataDirect:public BufferData { @@ -67,15 +67,15 @@ VK_NAMESPACE_BEGIN };// /** - *
- * ڱ޷ֱӽжд + * 打包型数据
+ * 该类数据由于被打包,所以无法直接进行读写 */ class BufferDataPack:public BufferData { - VkFormat format; ///<ݸʽ - uint byte; ///<ֽ + VkFormat format; ///<数据格式 + uint byte; ///<单个数据字节数 - uint compoment; ///<ݳɷ + uint compoment; ///<数据成份 public: @@ -84,13 +84,13 @@ VK_NAMESPACE_BEGIN class VertexBufferData:public BufferData { - uint32_t data_type; ///< (GL_BYTE,GL_UNSIGNED_SHORT,GL_FLOAT) - uint data_bytes; ///<ֽ (GL_BYTEΪ1,GL_UNSIGNED_SHORTΪ2,GL_FLOATΪ4) - uint data_comp; ///<ݳԱ (1/2/3/42D23D/3) + uint32_t data_type; ///<单个数据类型 (GL_BYTE,GL_UNSIGNED_SHORT,GL_FLOAT等) + uint data_bytes; ///<单个数据字节数 (GL_BYTE为1,GL_UNSIGNED_SHORT为2,GL_FLOAT为4等) + uint data_comp; ///<数据成员数 (1/2/3/4,如2D纹理坐标用2,3D坐标/法线用3) - uint data_stride; ///<ÿֽ + uint data_stride; ///<每组数据字节数 - uint32_t data_count; ///< + uint32_t data_count; ///<数据数量 protected: @@ -111,31 +111,31 @@ VK_NAMESPACE_BEGIN virtual ~VertexBufferData()=default; - uint32_t GetDataType ()const{return data_type;} ///<ȡ - uint GetComponent ()const{return data_comp;} ///<ȡÿһе - uint GetStride ()const{return data_stride;} ///<ȡÿһֽ + uint32_t GetDataType ()const{return data_type;} ///<取得数据类型 + uint GetComponent ()const{return data_comp;} ///<取数每一组数据中的数据数量 + uint GetStride ()const{return data_stride;} ///<取得每一组数据字节数 - uint32_t GetCount ()const{return data_count;} ///<ȡ - uint32_t GetTotalBytes ()const{return total_bytes;} ///<ȡֽ + uint32_t GetCount ()const{return data_count;} ///<取得数据数量 + uint32_t GetTotalBytes ()const{return total_bytes;} ///<取得数据总字节数 }; /** - * һݻ
- * ַʽĻзڴ棬ͷ - * @param dt (GL_BYTE,GL_UNSIGNED_SHORT,GL_FLOAT) - * @param dbytes ֽ (GL_BYTEΪ1,GL_UNSIGNED_SHORTΪ2,GL_FLOATΪ4) - * @param dcm ݳԱ (1/2/3/42D23D/3) - * @param count + * 创建一个顶点数据缓冲区
+ * 这种方式创建的缓冲区,它会自行分配内存,最终释放 + * @param dt 单个数据类型 (GL_BYTE,GL_UNSIGNED_SHORT,GL_FLOAT等) + * @param dbytes 单个数据字节数 (GL_BYTE为1,GL_UNSIGNED_SHORT为2,GL_FLOAT为4等) + * @param dcm 数据成员数 (1/2/3/4,如2D纹理坐标用2,3D坐标/法线用3) + * @param count 数据数量 */ VertexBufferData *CreateVertexBufferData(const uint32_t &dt,const uint &dbytes,const uint &dcm,const uint32_t &count); /** - * һݻ - * @param data ָ - * @param dt (GL_BYTE,GL_UNSIGNED_SHORT,GL_FLOAT) - * @param dbytes ֽ (GL_BYTEΪ1,GL_UNSIGNED_SHORTΪ2,GL_FLOATΪ4) - * @param dcm ݳԱ (1/2/3/42D23D/3) - * @param count + * 创建一个顶点数据缓冲区 + * @param data 数据指针 + * @param dt 单个数据类型 (GL_BYTE,GL_UNSIGNED_SHORT,GL_FLOAT等) + * @param dbytes 单个数据字节数 (GL_BYTE为1,GL_UNSIGNED_SHORT为2,GL_FLOAT为4等) + * @param dcm 数据成员数 (1/2/3/4,如2D纹理坐标用2,3D坐标/法线用3) + * @param count 数据数量 */ VertexBufferData *CreateVertexBufferData(void *data,const uint32_t &dt,const uint &dbytes,const uint &dcm,const uint32_t &count); @@ -143,8 +143,8 @@ VK_NAMESPACE_BEGIN inline VertexBufferData *VB##comp_count##short_name(const uint32_t &count){return CreateVertexBufferData(vk_type,sizeof(type),comp_count,count);} \ inline VertexBufferData *VB##comp_count##short_name(const uint32_t &count,const type *data){return CreateVertexBufferData((void *)data,vk_type,sizeof(type),comp_count,count);} - // UNORM ָ޷ԶתΪ 0.0 to 1.0 ĸ - // SNORM ָзԶתΪ-1.0 to +1.0 ĸ + // UNORM 指输入无符号数,自动转换为 0.0 to 1.0 的浮点数 + // SNORM 指输入有符号数,自动转换为-1.0 to +1.0 的浮点数 diff --git a/inc/hgl/graph/vulkan/VKCommandBuffer.h b/inc/hgl/graph/vulkan/VKCommandBuffer.h index 156b212b..6085cdbe 100644 --- a/inc/hgl/graph/vulkan/VKCommandBuffer.h +++ b/inc/hgl/graph/vulkan/VKCommandBuffer.h @@ -1,14 +1,8 @@ #ifndef HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE #define HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE -#include"VK.h" +#include VK_NAMESPACE_BEGIN -class RenderPass; -class Framebuffer; -class Pipeline; -class DescriptorSets; -class Renderable; - class CommandBuffer { VkDevice device; diff --git a/inc/hgl/graph/vulkan/VKDescriptorSets.h b/inc/hgl/graph/vulkan/VKDescriptorSets.h index 0a92bfcc..605d5a7f 100644 --- a/inc/hgl/graph/vulkan/VKDescriptorSets.h +++ b/inc/hgl/graph/vulkan/VKDescriptorSets.h @@ -1,7 +1,7 @@ #ifndef HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE #define HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE -#include"VK.h" +#include #include VK_NAMESPACE_BEGIN class Device; diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 5ac2663d..63489e70 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -1,4 +1,4 @@ -#ifndef HGL_GRAPH_RENDER_SURFACE_INCLUDE +#ifndef HGL_GRAPH_RENDER_SURFACE_INCLUDE #define HGL_GRAPH_RENDER_SURFACE_INCLUDE #include @@ -10,21 +10,6 @@ #include VK_NAMESPACE_BEGIN -struct PhysicalDevice; -class Buffer; -class VertexBuffer; -class IndexBuffer; -class CommandBuffer; -class RenderPass; -class Fence; -class Semaphore; -class ShaderModule; -class ShaderModuleManage; -class VertexShaderModule; -class Material; - -#define MAX_FRAMES_IN_FLIGHT 2 - class Device { DeviceAttribute *attr; @@ -41,7 +26,11 @@ class Device private: - friend Device *CreateRenderDevice(VkInstance,const PhysicalDevice *,Window *); + void CreateMainBufferAndPass(); + +private: + + friend Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,uint width,uint height); Device(DeviceAttribute *da); @@ -70,7 +59,9 @@ public: RenderPass * GetRenderPass () {return main_rp;} Framebuffer * GetFramebuffer (int index) {return main_fb[index];} -public: //Buffer + bool Resize (uint,uint); + +public: //Buffer相关 Buffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize size,const void *data,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE); Buffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize size,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE){return CreateBuffer(buf_usage,size,nullptr,sharing_mode);} @@ -95,18 +86,18 @@ public: //Buffer #undef CREATE_BUFFER_OBJECT -public: //material +public: //material相关 ShaderModuleManage *CreateShaderModuleManage(); -public: //Command Buffer +public: //Command Buffer 相关 CommandBuffer * CreateCommandBuffer(); RenderPass * CreateRenderPass(VkFormat color_format,VkFormat depth_format); Fence * CreateFence(); Semaphore * CreateSem(); -public: //ύ +public: //提交相关 bool AcquireNextImage (); bool QueueSubmit (const VkCommandBuffer *,const uint32_t count=1); diff --git a/inc/hgl/graph/vulkan/VKDeviceAttribute.h b/inc/hgl/graph/vulkan/VKDeviceAttribute.h index 48b2da8d..13b0dd90 100644 --- a/inc/hgl/graph/vulkan/VKDeviceAttribute.h +++ b/inc/hgl/graph/vulkan/VKDeviceAttribute.h @@ -1,14 +1,12 @@ #pragma once -#include"VK.h" -#include"VKImageView.h" +#include +#include VK_NAMESPACE_BEGIN constexpr uint32_t ERROR_FAMILY_INDEX=UINT32_MAX; -struct PhysicalDevice; - struct DeviceAttribute { VkInstance instance =nullptr; @@ -60,5 +58,7 @@ public: ~DeviceAttribute(); bool CheckMemoryType(uint32_t typeBits,VkFlags requirements_mask,uint32_t *typeIndex) const; + + void ClearSwapchain(); };//class DeviceAttribute VK_NAMESPACE_END diff --git a/inc/hgl/graph/vulkan/VKFence.h b/inc/hgl/graph/vulkan/VKFence.h index a5028517..0bbca265 100644 --- a/inc/hgl/graph/vulkan/VKFence.h +++ b/inc/hgl/graph/vulkan/VKFence.h @@ -1,7 +1,7 @@ #ifndef HGL_VULKAN_GRAPH_FENCE_INCLUDE #define HGL_VULKAN_GRAPH_FENCE_INCLUDE -#include"VK.h" +#include VK_NAMESPACE_BEGIN class Fence { diff --git a/inc/hgl/graph/vulkan/VKFramebuffer.h b/inc/hgl/graph/vulkan/VKFramebuffer.h index 48f4658c..75711674 100644 --- a/inc/hgl/graph/vulkan/VKFramebuffer.h +++ b/inc/hgl/graph/vulkan/VKFramebuffer.h @@ -1,11 +1,8 @@ #ifndef HGL_GRAPH_VULKAN_FRAMEBUFFER_INCLUDE #define HGL_GRAPH_VULKAN_FRAMEBUFFER_INCLUDE -#include"VK.h" +#include VK_NAMESPACE_BEGIN -class Device; -class RenderPass; -class ImageView; class Framebuffer { diff --git a/inc/hgl/graph/vulkan/VKImageView.h b/inc/hgl/graph/vulkan/VKImageView.h index 28ec5051..cc801c32 100644 --- a/inc/hgl/graph/vulkan/VKImageView.h +++ b/inc/hgl/graph/vulkan/VKImageView.h @@ -1,7 +1,7 @@ #ifndef HGL_GRAPH_VULKAN_IMAGE_VIEW_INCLUDE #define HGL_GRAPH_VULKAN_IMAGE_VIEW_INCLUDE -#include"VK.h" +#include VK_NAMESPACE_BEGIN class ImageView { diff --git a/inc/hgl/graph/vulkan/VKInstance.h b/inc/hgl/graph/vulkan/VKInstance.h index 6d8725c9..38be4468 100644 --- a/inc/hgl/graph/vulkan/VKInstance.h +++ b/inc/hgl/graph/vulkan/VKInstance.h @@ -7,9 +7,6 @@ #include VK_NAMESPACE_BEGIN -struct PhysicalDevice; -class Device; - class Instance { VkInstance inst; @@ -34,15 +31,13 @@ class Device; virtual ~Instance(); - VkInstance GetVkInstance () {return inst;} + operator VkInstance (){return inst;} const List & GetLayerProperties ()const {return layer_properties;} const bool CheckLayerSupport (const UTF8String &)const; const CharPointerList & GetExtList ()const {return ext_list;} const ObjectList &GetDeviceList ()const {return physical_devices;} const PhysicalDevice * GetDevice (VkPhysicalDeviceType)const; - - Device * CreateRenderDevice (Window *,const PhysicalDevice *pd=nullptr); };//class Instance Instance *CreateInstance(const UTF8String &); ///<创建一个Vulkan实例 diff --git a/inc/hgl/graph/vulkan/VKMaterial.h b/inc/hgl/graph/vulkan/VKMaterial.h index cba30280..8e9631d6 100644 --- a/inc/hgl/graph/vulkan/VKMaterial.h +++ b/inc/hgl/graph/vulkan/VKMaterial.h @@ -1,20 +1,11 @@ #ifndef HGL_GRAPH_VULKAN_MATERIAL_INCLUDE #define HGL_GRAPH_VULKAN_MATERIAL_INCLUDE -#include"VK.h" +#include #include #include VK_NAMESPACE_BEGIN -class Device; -class ShaderModule; -class VertexShaderModule; -class DescriptorSets; -class DescriptorSetLayoutCreater; -class VertexAttributeBinding; -class VertexBuffer; -class Renderable; -class PipelineLayout; - +class DescriptorSetLayoutCreater;; using ShaderModuleMap=hgl::Map; /** diff --git a/inc/hgl/graph/vulkan/VKPhysicalDevice.h b/inc/hgl/graph/vulkan/VKPhysicalDevice.h index bf66209c..dec93d53 100644 --- a/inc/hgl/graph/vulkan/VKPhysicalDevice.h +++ b/inc/hgl/graph/vulkan/VKPhysicalDevice.h @@ -1,6 +1,6 @@ #pragma once -#include"VK.h" +#include VK_NAMESPACE_BEGIN struct PhysicalDevice diff --git a/inc/hgl/graph/vulkan/VKPipeline.h b/inc/hgl/graph/vulkan/VKPipeline.h index 14af266a..2d470be9 100644 --- a/inc/hgl/graph/vulkan/VKPipeline.h +++ b/inc/hgl/graph/vulkan/VKPipeline.h @@ -1,14 +1,8 @@ #ifndef HGL_GRAPH_VULKAN_PIPELINE_INCLUDE #define HGL_GRAPH_VULKAN_PIPELINE_INCLUDE -#include"VK.h" - +#include VK_NAMESPACE_BEGIN -class Device; -class RenderPass; -class VertexAttributeBinding; -class Material; - class Pipeline { VkDevice device; diff --git a/inc/hgl/graph/vulkan/VKRenderPass.h b/inc/hgl/graph/vulkan/VKRenderPass.h index 8f6de7f0..d5e79f1e 100644 --- a/inc/hgl/graph/vulkan/VKRenderPass.h +++ b/inc/hgl/graph/vulkan/VKRenderPass.h @@ -1,11 +1,8 @@ #ifndef HGL_GRAPH_VULKAN_RENDER_PASS_INCLUDE #define HGL_GRAPH_VULKAN_RENDER_PASS_INCLUDE -#include"VK.h" +#include VK_NAMESPACE_BEGIN -class Framebuffer; -class ImageView; - /** * RenderPass功能封装
* RenderPass在创建时,需要指定输入的color imageview与depth imageview象素格式, diff --git a/inc/hgl/graph/vulkan/VKRenderable.h b/inc/hgl/graph/vulkan/VKRenderable.h index 2f681d2d..9104570d 100644 --- a/inc/hgl/graph/vulkan/VKRenderable.h +++ b/inc/hgl/graph/vulkan/VKRenderable.h @@ -1,13 +1,9 @@ #ifndef HGL_GRAPH_VULKAN_RENDERABLE_INCLUDE #define HGL_GRAPH_VULKAN_RENDERABLE_INCLUDE -#include"VK.h" +#include #include VK_NAMESPACE_BEGIN -class VertexShaderModule; -class VertexBuffer; -class IndexBuffer; - /** * 可渲染数据对象
* 本对象包含材质实例信息和Mesh信息,可渲染数据对象中包含供最终API使用的VBO数据,可能存在多个MESH数据的集合。

diff --git a/inc/hgl/graph/vulkan/VKSemaphore.h b/inc/hgl/graph/vulkan/VKSemaphore.h index 862c9178..2f2c5cf1 100644 --- a/inc/hgl/graph/vulkan/VKSemaphore.h +++ b/inc/hgl/graph/vulkan/VKSemaphore.h @@ -1,7 +1,7 @@ #ifndef HGL_GRAPH_VULKAN_SEMAPHORE_INCLUDE #define HGL_GRAPH_VULKAN_SEMAPHORE_INCLUDE -#include"VK.h" +#include VK_NAMESPACE_BEGIN class Semaphore { diff --git a/inc/hgl/graph/vulkan/VKShaderModuleManage.h b/inc/hgl/graph/vulkan/VKShaderModuleManage.h index 62189523..e1177b94 100644 --- a/inc/hgl/graph/vulkan/VKShaderModuleManage.h +++ b/inc/hgl/graph/vulkan/VKShaderModuleManage.h @@ -1,19 +1,14 @@ -#ifndef HGL_GRAPH_VULKAN_SHADER_MODULE_MANAGE_INCLUDE +#ifndef HGL_GRAPH_VULKAN_SHADER_MODULE_MANAGE_INCLUDE #define HGL_GRAPH_VULKAN_SHADER_MODULE_MANAGE_INCLUDE -#include"VK.h" +#include #include #include VK_NAMESPACE_BEGIN -class Device; -class ShaderModule; -class VertexShaderModule; -class Material; - /** - * Shaderģ
- * еshaderģͷ - * ùһ豸½һеshader(vs/fs/gsȵ) + * Shader模块管理器
+ * 所有的shader模块均由它创建和释放 + * 该管理器在一个设备下仅有一个,它负责管理所有的shader(仅vs/fs/gs等单个,非组合体) */ class ShaderModuleManage { diff --git a/inc/hgl/graph/vulkan/VKSurfaceExtensionName.h b/inc/hgl/graph/vulkan/VKSurfaceExtensionName.h index 35c5024a..5b9c1451 100644 --- a/inc/hgl/graph/vulkan/VKSurfaceExtensionName.h +++ b/inc/hgl/graph/vulkan/VKSurfaceExtensionName.h @@ -1,6 +1,6 @@ #pragma once -#include"VK.h" +#include #ifdef __ANDROID__ #include diff --git a/inc/hgl/graph/vulkan/VKVertexAttributeBinding.h b/inc/hgl/graph/vulkan/VKVertexAttributeBinding.h index 5f7ae60e..5470c7e0 100644 --- a/inc/hgl/graph/vulkan/VKVertexAttributeBinding.h +++ b/inc/hgl/graph/vulkan/VKVertexAttributeBinding.h @@ -1,13 +1,9 @@ #ifndef HGL_GRAPH_VULKAN_VERTEX_ATTRIBUTE_BINDING_INCLUDE #define HGL_GRAPH_VULKAN_VERTEX_ATTRIBUTE_BINDING_INCLUDE -#include"VK.h" +#include #include VK_NAMESPACE_BEGIN -class VertexBuffer; -class IndexBuffer; -class VertexShaderModule; - /** * 顶点输入状态实例
* 本对象用于传递给Material,用于已经确定好顶点格式的情况下,依然可修改部分设定(如instance)。 diff --git a/inc/hgl/platform/Window.h b/inc/hgl/platform/Window.h index 4c56b2c8..37b56d3c 100644 --- a/inc/hgl/platform/Window.h +++ b/inc/hgl/platform/Window.h @@ -3,10 +3,15 @@ #include #include +#include + namespace hgl { class Window { + VK_NAMESPACE::Device *device; + virtual VkSurfaceKHR CreateSurface(VkInstance)=0; + protected: uint width,height; @@ -16,6 +21,7 @@ namespace hgl bool active; bool is_close; + bool is_min; bool key_push[kbRangeSize]; @@ -47,7 +53,7 @@ namespace hgl virtual void OnChar (os_char){} - virtual void OnResize (int w,int h){width=w;height=h;} + virtual void OnResize (uint,uint); virtual void OnActive (bool a){active=a;} virtual void OnClose (){is_close=true;} @@ -56,18 +62,22 @@ namespace hgl Window(const OSString &wn) { + device=nullptr; width=height=0; + full_screen=false; win_name=wn; active=false; is_close=true; + is_min=false; hgl_zero(key_push); } - virtual ~Window()=default; + virtual ~Window(); virtual bool Create(uint,uint)=0; virtual bool Create(uint,uint,uint)=0; virtual void Close()=0; + bool IsMin()const{return is_min;} bool IsClose()const{return is_close;} bool IsVisible()const{return (!is_close)&&width&&height;} @@ -76,12 +86,17 @@ namespace hgl virtual void Show()=0; virtual void Hide()=0; - virtual void ToMinWindow(){} - virtual void ToMaxWindow(){} + virtual void ToMinWindow()=0; + virtual void ToMaxWindow()=0; virtual void SetSystemCursor(bool){} virtual bool Update(); + + public: + + VK_NAMESPACE::Device * CreateRenderDevice(VK_NAMESPACE::Instance *,const VK_NAMESPACE::PhysicalDevice *pd=nullptr); + void CloseRenderDevice(); };//class Window Window *CreateRenderWindow(const OSString &win_name); diff --git a/src/Platform/UNIX/XCBVulkan.cpp b/src/Platform/UNIX/XCBVulkan.cpp index 19993d3a..932e25a4 100644 --- a/src/Platform/UNIX/XCBVulkan.cpp +++ b/src/Platform/UNIX/XCBVulkan.cpp @@ -3,15 +3,15 @@ #include VK_NAMESPACE_BEGIN -VkSurfaceKHR CreateRenderDevice(VkInstance vk_inst,Window *win) +VkSurfaceKHR XCBWindow::CreateSurface(VkInstance vk_inst) { XCBWindow *xcb_win=(XCBWindow *)win; VkXcbSurfaceCreateInfoKHR createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; createInfo.pNext = nullptr; - createInfo.connection = xcb_win->GetConnection(); - createInfo.window = xcb_win->GetWindow(); + createInfo.connection = connection; + createInfo.window = window; VkSurfaceKHR surface; diff --git a/src/Platform/UNIX/XCBWindow.h b/src/Platform/UNIX/XCBWindow.h index 7313642e..29abd92e 100644 --- a/src/Platform/UNIX/XCBWindow.h +++ b/src/Platform/UNIX/XCBWindow.h @@ -13,6 +13,7 @@ namespace hgl private: bool InitConnection(); + VkSurfaceKHR CreateSurface(VkInstance) override; public: @@ -29,11 +30,6 @@ namespace hgl void Hide()override{} bool MessageProc() override; bool WaitMessage() override; - - public: - - xcb_connection_t *GetConnection(){return connection;} - xcb_window_t GetWindow(){return window;} };//class XCBWindow:public Window }//namespace hgl diff --git a/src/Platform/Win/WinVulkan.cpp b/src/Platform/Win/WinVulkan.cpp index 24aec8dc..37a241af 100644 --- a/src/Platform/Win/WinVulkan.cpp +++ b/src/Platform/Win/WinVulkan.cpp @@ -1,25 +1,23 @@ #include"WinWindow.h" -#include #include -VK_NAMESPACE_BEGIN -VkSurfaceKHR CreateRenderDevice(VkInstance vk_inst,Window *win) +namespace hgl { - WinWindow *ww=(WinWindow *)win; + VkSurfaceKHR WinWindow::CreateSurface(VkInstance vk_inst) + { + VkWin32SurfaceCreateInfoKHR createInfo={}; + createInfo.sType=VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; + createInfo.pNext=nullptr; + createInfo.hinstance=hInstance; + createInfo.hwnd=win_hwnd; - VkWin32SurfaceCreateInfoKHR createInfo={}; - createInfo.sType=VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; - createInfo.pNext=nullptr; - createInfo.hinstance=ww->GetHInstance(); - createInfo.hwnd=ww->GetHWnd(); + VkSurfaceKHR surface; - VkSurfaceKHR surface; + VkResult res=vkCreateWin32SurfaceKHR(vk_inst,&createInfo,nullptr,&surface); - VkResult res=vkCreateWin32SurfaceKHR(vk_inst,&createInfo,nullptr,&surface); + if(res!=VK_SUCCESS) + return(nullptr); - if(res!=VK_SUCCESS) - return(nullptr); - - return(surface); -} -VK_NAMESPACE_END + return(surface); + } +}//namespace hgl \ No newline at end of file diff --git a/src/Platform/Win/WinWindow.h b/src/Platform/Win/WinWindow.h index e379d6d5..f9a733fa 100644 --- a/src/Platform/Win/WinWindow.h +++ b/src/Platform/Win/WinWindow.h @@ -1,7 +1,6 @@ #pragma once #include #include - namespace hgl { /** @@ -18,17 +17,13 @@ namespace hgl protected: bool Create(); + VkSurfaceKHR CreateSurface(VkInstance)override; public: using Window::Window; ~WinWindow(); - HINSTANCE GetHInstance(){return hInstance;} - HWND GetHWnd(){return win_hwnd;} - - public: - bool Create(uint w, uint h) override; bool Create(uint, uint, uint) override; void Close() override; diff --git a/src/Platform/Window.cpp b/src/Platform/Window.cpp index ce3cf4b1..40906cc6 100644 --- a/src/Platform/Window.cpp +++ b/src/Platform/Window.cpp @@ -1,6 +1,55 @@ -#include +#include +#include +#include +#include + +VK_NAMESPACE_BEGIN +Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,uint width,uint height); +VK_NAMESPACE_END + namespace hgl { + Window::~Window() + { + SAFE_CLEAR(device); + } + + VK_NAMESPACE::Device *Window::CreateRenderDevice(VK_NAMESPACE::Instance *vk_inst,const VK_NAMESPACE::PhysicalDevice *pd) + { + if(device) + return(device); + + if(!vk_inst) + return(nullptr); + + if(!pd)pd=vk_inst->GetDevice(VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU); //先找独显 + if(!pd)pd=vk_inst->GetDevice(VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU); //再找集显 + if(!pd)pd=vk_inst->GetDevice(VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU); //最后找虚拟显卡 + + if(!pd) + return(nullptr); + + VkSurfaceKHR surface=CreateSurface(*vk_inst); + + if(!surface) + return(nullptr); + + device=VK_NAMESPACE::CreateRenderDevice(*vk_inst,pd,surface,width,height); + + if(!device) + { + vkDestroySurfaceKHR(*vk_inst,surface,nullptr); + return(nullptr); + } + + return(device); + } + + void Window::CloseRenderDevice() + { + SAFE_CLEAR(device); + } + void Window::OnKeyDown(KeyboardButton kb) { if(key_push[kb]) @@ -18,13 +67,33 @@ namespace hgl key_push[kb]=false; } + void Window::OnResize(uint w,uint h) + { + if(w==width&&height==h) + return; + + if(w==0||h==0) + { + is_min=true; + } + else + { + is_min=false; + width=w; + height=h; + + if(device) + device->Resize(width,height); + } + } + bool Window::Update() { while(MessageProc()) { } - if(!active||width==0||height==0) + if(!active||is_min) this->WaitMessage(); return(!is_close); diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index 84c2e9a9..8a306013 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -11,6 +11,8 @@ #include VK_NAMESPACE_BEGIN +bool ResizeRenderDevice(DeviceAttribute *attr,uint width,uint height); + Device::Device(DeviceAttribute *da) { attr=da; @@ -28,14 +30,7 @@ Device::Device(DeviceAttribute *da) present.waitSemaphoreCount = 0; present.pResults = nullptr; - { - const int sc_count=attr->sc_image_views.GetCount(); - - main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->depth.view->GetFormat()); - - for(int i=0;isc_image_views[i],attr->depth.view)); - } + CreateMainBufferAndPass(); } Device::~Device() @@ -50,6 +45,30 @@ Device::~Device() delete attr; } +void Device::CreateMainBufferAndPass() +{ + const int sc_count=attr->sc_image_views.GetCount(); + + main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->depth.view->GetFormat()); + + for(int i=0;isc_image_views[i],attr->depth.view)); +} + +bool Device::Resize(uint width,uint height) +{ + main_fb.Clear(); + delete main_rp; + main_rp=nullptr; + + if(!ResizeRenderDevice(attr,width,height)) + return(false); + + CreateMainBufferAndPass(); + + return(true); +} + CommandBuffer *Device::CreateCommandBuffer() { if(!attr->cmd_pool) diff --git a/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp b/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp index 7c042480..2a04635d 100644 --- a/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceAttribute.cpp @@ -138,19 +138,8 @@ DeviceAttribute::~DeviceAttribute() if(desc_pool) vkDestroyDescriptorPool(device,desc_pool,nullptr); - SAFE_CLEAR(depth.view); - - if(depth.image) - vkDestroyImage(device,depth.image,nullptr); - - if(depth.mem) - vkFreeMemory(device,depth.mem,nullptr); - - sc_image_views.Clear(); - - if(swap_chain) - vkDestroySwapchainKHR(device,swap_chain,nullptr); - + ClearSwapchain(); + if(cmd_pool) vkDestroyCommandPool(device,cmd_pool,nullptr); @@ -165,4 +154,29 @@ bool DeviceAttribute::CheckMemoryType(uint32_t typeBits,VkFlags requirements_mas { return physical_device->CheckMemoryType(typeBits,requirements_mask,typeIndex); } + +void DeviceAttribute::ClearSwapchain() +{ + SAFE_CLEAR(depth.view); + + if(depth.image) + { + vkDestroyImage(device,depth.image,nullptr); + depth.image=nullptr; + } + + if(depth.mem) + { + vkFreeMemory(device,depth.mem,nullptr); + depth.mem=nullptr; + } + + sc_image_views.Clear(); + + if(swap_chain) + { + vkDestroySwapchainKHR(device,swap_chain,nullptr); + swap_chain=nullptr; + } +} VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp index cdc9adfb..cca4371f 100644 --- a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp @@ -4,8 +4,6 @@ #include VK_NAMESPACE_BEGIN -VkSurfaceKHR CreateRenderDevice(VkInstance,Window *); - namespace { template T Clamp(const T &cur,const T &min_value,const T &max_value) @@ -286,20 +284,31 @@ namespace return cache; } + + bool CreateSwapchinAndImageView(DeviceAttribute *attr) + { + attr->swap_chain=CreateSwapChain(attr); + + if(!attr->swap_chain) + return(false); + + if(!CreateSwapchainImageView(attr)) + return(false); + + if(!CreateDepthBuffer(attr)) + return(false); + + return(true); + } }//namespace -Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,Window *win) +Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,uint width,uint height) { - VkSurfaceKHR surface=CreateRenderDevice(inst,win); - - if(!surface) - return(nullptr); - DeviceAttribute *attr=new DeviceAttribute(inst,physical_device,surface); AutoDelete auto_delete(attr); - attr->swapchain_extent=GetSwapchainExtent(attr->surface_caps,win->GetWidth(),win->GetHeight()); + attr->swapchain_extent=GetSwapchainExtent(attr->surface_caps,width,height); if(attr->graphics_family==ERROR_FAMILY_INDEX) return(nullptr); @@ -316,15 +325,7 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device if(!attr->cmd_pool) return(nullptr); - attr->swap_chain=CreateSwapChain(attr); - - if(!attr->swap_chain) - return(nullptr); - - if(!CreateSwapchainImageView(attr)) - return(nullptr); - - if(!CreateDepthBuffer(attr)) + if(!CreateSwapchinAndImageView(attr)) return(nullptr); attr->desc_pool=CreateDescriptorPool(attr->device,1); @@ -341,4 +342,17 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device return(new Device(attr)); } + +bool ResizeRenderDevice(DeviceAttribute *attr,uint width,uint height) +{ + if(attr->swapchain_extent.width==width + &&attr->swapchain_extent.height==height) + return(true); + + attr->ClearSwapchain(); + + attr->swapchain_extent=GetSwapchainExtent(attr->surface_caps,width,height); + + return CreateSwapchinAndImageView(attr); +} VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKInstance.cpp b/src/RenderDevice/Vulkan/VKInstance.cpp index 94218ff9..5a45e511 100644 --- a/src/RenderDevice/Vulkan/VKInstance.cpp +++ b/src/RenderDevice/Vulkan/VKInstance.cpp @@ -287,19 +287,4 @@ const PhysicalDevice *Instance::GetDevice(VkPhysicalDeviceType type)const return(nullptr); } - -Device *Instance::CreateRenderDevice(Window *win,const PhysicalDevice *pd) -{ - if(!win) - return(nullptr); - - if(!pd)pd=GetDevice(VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU); //先找独显 - if(!pd)pd=GetDevice(VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU); //再找集显 - if(!pd)pd=GetDevice(VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU); //最后找虚拟显卡 - - if(!pd) - return(nullptr); - - return VK_NAMESPACE::CreateRenderDevice(inst,pd,win); -} VK_NAMESPACE_END