将CreateRenderDevice转移到Window类中,并记录device,以方便在RESIZE等时重建SwapChain

This commit is contained in:
hyzboy 2019-05-07 12:46:25 +08:00
parent 1ee9eef78c
commit d2cad7bf2f
31 changed files with 305 additions and 229 deletions

View File

@ -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);

View File

@ -4,8 +4,8 @@
#include<hgl/type/List.h>
#include<vulkan/vulkan.h>
#include<iostream>
#include"VKFormat.h"
#include"VKPrimivate.h"
#include<hgl/graph/vulkan/VKFormat.h>
#include<hgl/graph/vulkan/VKPrimivate.h>
#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<const char *>;
#ifdef _DEBUG

View File

@ -1,7 +1,7 @@
#ifndef HGL_GRAPH_VULKAN_BUFFER_INCLUDE
#define HGL_GRAPH_VULKAN_BUFFER_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
VK_NAMESPACE_BEGIN
struct VulkanBuffer

View File

@ -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<hgl/graph/vulkan/VK.h>
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
/**
* <Br>
* 访
* <Br>
* 访
*/
class BufferDataDirect:public BufferData
{
@ -67,15 +67,15 @@ VK_NAMESPACE_BEGIN
};//
/**
* <Br>
*
* <Br>
*
*/
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/4如2D纹理坐标用23D坐标/法线用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纹理坐标用23D坐标/法线用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;} ///<取得数据总字节数
};
/**
* <br>
*
* @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/42D纹理坐标用23D坐标/线3)
* @param count
* <br>
*
* @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/42D纹理坐标用23D坐标/线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/42D纹理坐标用23D坐标/线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/42D纹理坐标用23D坐标/线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 的浮点数

View File

@ -1,14 +1,8 @@
#ifndef HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
#define HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
VK_NAMESPACE_BEGIN
class RenderPass;
class Framebuffer;
class Pipeline;
class DescriptorSets;
class Renderable;
class CommandBuffer
{
VkDevice device;

View File

@ -1,7 +1,7 @@
#ifndef HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE
#define HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
#include<hgl/type/Map.h>
VK_NAMESPACE_BEGIN
class Device;

View File

@ -1,4 +1,4 @@
#ifndef HGL_GRAPH_RENDER_SURFACE_INCLUDE
#ifndef HGL_GRAPH_RENDER_SURFACE_INCLUDE
#define HGL_GRAPH_RENDER_SURFACE_INCLUDE
#include<hgl/type/List.h>
@ -10,21 +10,6 @@
#include<hgl/graph/vulkan/VKFramebuffer.h>
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);

View File

@ -1,14 +1,12 @@
#pragma once
#include"VK.h"
#include"VKImageView.h"
#include<hgl/graph/vulkan/VK.h>
#include<hgl/graph/vulkan/VKImageView.h>
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

View File

@ -1,7 +1,7 @@
#ifndef HGL_VULKAN_GRAPH_FENCE_INCLUDE
#define HGL_VULKAN_GRAPH_FENCE_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
VK_NAMESPACE_BEGIN
class Fence
{

View File

@ -1,11 +1,8 @@
#ifndef HGL_GRAPH_VULKAN_FRAMEBUFFER_INCLUDE
#define HGL_GRAPH_VULKAN_FRAMEBUFFER_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
VK_NAMESPACE_BEGIN
class Device;
class RenderPass;
class ImageView;
class Framebuffer
{

View File

@ -1,7 +1,7 @@
#ifndef HGL_GRAPH_VULKAN_IMAGE_VIEW_INCLUDE
#define HGL_GRAPH_VULKAN_IMAGE_VIEW_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
VK_NAMESPACE_BEGIN
class ImageView
{

View File

@ -7,9 +7,6 @@
#include<hgl/graph/vulkan/VK.h>
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<VkLayerProperties> & GetLayerProperties ()const {return layer_properties;}
const bool CheckLayerSupport (const UTF8String &)const;
const CharPointerList & GetExtList ()const {return ext_list;}
const ObjectList<PhysicalDevice> &GetDeviceList ()const {return physical_devices;}
const PhysicalDevice * GetDevice (VkPhysicalDeviceType)const;
Device * CreateRenderDevice (Window *,const PhysicalDevice *pd=nullptr);
};//class Instance
Instance *CreateInstance(const UTF8String &); ///<创建一个Vulkan实例

View File

@ -1,20 +1,11 @@
#ifndef HGL_GRAPH_VULKAN_MATERIAL_INCLUDE
#define HGL_GRAPH_VULKAN_MATERIAL_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
#include<hgl/type/Map.h>
#include<hgl/type/BaseString.h>
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<VkShaderStageFlagBits,const ShaderModule *>;
/**

View File

@ -1,6 +1,6 @@
#pragma once
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
VK_NAMESPACE_BEGIN
struct PhysicalDevice

View File

@ -1,14 +1,8 @@
#ifndef HGL_GRAPH_VULKAN_PIPELINE_INCLUDE
#define HGL_GRAPH_VULKAN_PIPELINE_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
VK_NAMESPACE_BEGIN
class Device;
class RenderPass;
class VertexAttributeBinding;
class Material;
class Pipeline
{
VkDevice device;

View File

@ -1,11 +1,8 @@
#ifndef HGL_GRAPH_VULKAN_RENDER_PASS_INCLUDE
#define HGL_GRAPH_VULKAN_RENDER_PASS_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
VK_NAMESPACE_BEGIN
class Framebuffer;
class ImageView;
/**
* RenderPass功能封装<br>
* RenderPass在创建时color imageview与depth imageview象素格式

View File

@ -1,13 +1,9 @@
#ifndef HGL_GRAPH_VULKAN_RENDERABLE_INCLUDE
#define HGL_GRAPH_VULKAN_RENDERABLE_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
#include<hgl/type/BaseString.h>
VK_NAMESPACE_BEGIN
class VertexShaderModule;
class VertexBuffer;
class IndexBuffer;
/**
* <br>
* Mesh信息API使用的VBO数据MESH数据的集合</p>

View File

@ -1,7 +1,7 @@
#ifndef HGL_GRAPH_VULKAN_SEMAPHORE_INCLUDE
#define HGL_GRAPH_VULKAN_SEMAPHORE_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
VK_NAMESPACE_BEGIN
class Semaphore
{

View File

@ -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<hgl/graph/vulkan/VK.h>
#include<hgl/type/Map.h>
#include<hgl/type/BaseString.h>
VK_NAMESPACE_BEGIN
class Device;
class ShaderModule;
class VertexShaderModule;
class Material;
/**
* Shader模块管理器<br>
* shader模块均由它创建和释放
* shader(vs/fs/gs等单个)
* Shader模块管理器<br>
* shader模块均由它创建和释放
* shader(vs/fs/gs等单个)
*/
class ShaderModuleManage
{

View File

@ -1,6 +1,6 @@
#pragma once
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
#ifdef __ANDROID__
#include<vulkan/vulkan_android.h>

View File

@ -1,13 +1,9 @@
#ifndef HGL_GRAPH_VULKAN_VERTEX_ATTRIBUTE_BINDING_INCLUDE
#define HGL_GRAPH_VULKAN_VERTEX_ATTRIBUTE_BINDING_INCLUDE
#include"VK.h"
#include<hgl/graph/vulkan/VK.h>
#include<hgl/type/BaseString.h>
VK_NAMESPACE_BEGIN
class VertexBuffer;
class IndexBuffer;
class VertexShaderModule;
/**
* <br>
* Material,(instance)

View File

@ -3,10 +3,15 @@
#include<hgl/type/BaseString.h>
#include<hgl/platform/InputDevice.h>
#include<hgl/graph/vulkan/VK.h>
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);

View File

@ -3,15 +3,15 @@
#include<vulkan/vulkan_xcb.h>
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;

View File

@ -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

View File

@ -1,17 +1,15 @@
#include"WinWindow.h"
#include<hgl/graph/vulkan/VK.h>
#include<vulkan/vulkan_win32.h>
VK_NAMESPACE_BEGIN
VkSurfaceKHR CreateRenderDevice(VkInstance vk_inst,Window *win)
namespace hgl
{
VkSurfaceKHR WinWindow::CreateSurface(VkInstance vk_inst)
{
WinWindow *ww=(WinWindow *)win;
VkWin32SurfaceCreateInfoKHR createInfo={};
createInfo.sType=VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
createInfo.pNext=nullptr;
createInfo.hinstance=ww->GetHInstance();
createInfo.hwnd=ww->GetHWnd();
createInfo.hinstance=hInstance;
createInfo.hwnd=win_hwnd;
VkSurfaceKHR surface;
@ -22,4 +20,4 @@ VkSurfaceKHR CreateRenderDevice(VkInstance vk_inst,Window *win)
return(surface);
}
VK_NAMESPACE_END
}//namespace hgl

View File

@ -1,7 +1,6 @@
#pragma once
#include<hgl/platform/Window.h>
#include<Windows.h>
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;

View File

@ -1,6 +1,55 @@
#include<hgl/platform/Window.h>
#include<hgl/platform/Window.h>
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/graph/vulkan/VKInstance.h>
#include<hgl/graph/vulkan/VKPhysicalDevice.h>
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);

View File

@ -11,6 +11,8 @@
#include<hgl/graph/vulkan/VKDescriptorSets.h>
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;i<sc_count;i++)
main_fb.Add(vulkan::CreateFramebuffer(this,main_rp,attr->sc_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;i<sc_count;i++)
main_fb.Add(vulkan::CreateFramebuffer(this,main_rp,attr->sc_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)

View File

@ -138,18 +138,7 @@ 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

View File

@ -4,8 +4,6 @@
#include<hgl/graph/vulkan/VKFramebuffer.h>
VK_NAMESPACE_BEGIN
VkSurfaceKHR CreateRenderDevice(VkInstance,Window *);
namespace
{
template<typename T> 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<DeviceAttribute> 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

View File

@ -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