增加新的VkMemory封装,并将贴图的显存分配转移到上面
This commit is contained in:
parent
2881665f50
commit
4083306b67
@ -31,6 +31,7 @@ class TextureCubemapArray;
|
|||||||
|
|
||||||
class Sampler;
|
class Sampler;
|
||||||
|
|
||||||
|
class Memory;
|
||||||
class Buffer;
|
class Buffer;
|
||||||
class VertexBuffer;
|
class VertexBuffer;
|
||||||
class IndexBuffer;
|
class IndexBuffer;
|
||||||
|
@ -77,6 +77,10 @@ public:
|
|||||||
|
|
||||||
bool Resize (uint,uint);
|
bool Resize (uint,uint);
|
||||||
|
|
||||||
|
public: //内存相关
|
||||||
|
|
||||||
|
Memory *Device::CreateMemory(const VkMemoryRequirements &,uint32_t properties);
|
||||||
|
|
||||||
public: //Buffer相关
|
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,const void *data,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE);
|
||||||
|
48
inc/hgl/graph/vulkan/VKMemory.h
Normal file
48
inc/hgl/graph/vulkan/VKMemory.h
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#ifndef HGL_GRAPH_VULKAN_MEMORY_INCLUDE
|
||||||
|
#define HGL_GRAPH_VULKAN_MEMORY_INCLUDE
|
||||||
|
|
||||||
|
#include<hgl/graph/vulkan/VK.h>
|
||||||
|
VK_NAMESPACE_BEGIN
|
||||||
|
class Memory
|
||||||
|
{
|
||||||
|
VkDevice device;
|
||||||
|
VkDeviceMemory memory;
|
||||||
|
VkMemoryRequirements req;
|
||||||
|
uint32_t index;
|
||||||
|
uint32_t properties;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
friend class Device;
|
||||||
|
|
||||||
|
Memory(VkDevice dev,VkDeviceMemory dm,const VkMemoryRequirements &mr,const uint32 i,const uint32_t p)
|
||||||
|
{
|
||||||
|
device=dev;
|
||||||
|
memory=dm;
|
||||||
|
req=mr;
|
||||||
|
index=i;
|
||||||
|
properties=p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
virtual ~Memory();
|
||||||
|
|
||||||
|
operator VkDeviceMemory(){return memory;}
|
||||||
|
|
||||||
|
const VkMemoryRequirements &GetRequirements ()const{return req;}
|
||||||
|
|
||||||
|
const uint32_t GetType ()const{return req.memoryTypeBits;}
|
||||||
|
const VkDeviceSize GetSize ()const{return req.size;}
|
||||||
|
const VkDeviceSize GetAligment ()const{return req.alignment;}
|
||||||
|
const uint32_t GetTypeIndex ()const{return index;}
|
||||||
|
const uint32_t GetProperties ()const{return properties;}
|
||||||
|
|
||||||
|
void *Map();
|
||||||
|
void *Map(VkDeviceSize offset,VkDeviceSize map_size);
|
||||||
|
void Unmap();
|
||||||
|
|
||||||
|
bool Bind(VkImage image);
|
||||||
|
};//class Memory
|
||||||
|
VK_NAMESPACE_END
|
||||||
|
#endif//HGL_GRAPH_VULKAN_MEMORY_INCLUDE
|
@ -6,7 +6,7 @@
|
|||||||
VK_NAMESPACE_BEGIN
|
VK_NAMESPACE_BEGIN
|
||||||
struct TextureData
|
struct TextureData
|
||||||
{
|
{
|
||||||
VkDeviceMemory memory =nullptr;
|
Memory * memory =nullptr;
|
||||||
VkImage image =nullptr;
|
VkImage image =nullptr;
|
||||||
VkImageLayout image_layout=VK_IMAGE_LAYOUT_UNDEFINED;
|
VkImageLayout image_layout=VK_IMAGE_LAYOUT_UNDEFINED;
|
||||||
ImageView * image_view =nullptr;
|
ImageView * image_view =nullptr;
|
||||||
@ -30,7 +30,7 @@ public:
|
|||||||
|
|
||||||
operator TextureData * (){return data;}
|
operator TextureData * (){return data;}
|
||||||
|
|
||||||
operator VkDeviceMemory (){return data?data->memory:nullptr;}
|
operator Memory * (){return data?data->memory:nullptr;}
|
||||||
operator VkImage (){return data?data->image:nullptr;}
|
operator VkImage (){return data?data->image:nullptr;}
|
||||||
operator VkImageLayout (){return data?data->image_layout:VK_IMAGE_LAYOUT_UNDEFINED;}
|
operator VkImageLayout (){return data?data->image_layout:VK_IMAGE_LAYOUT_UNDEFINED;}
|
||||||
operator VkImageView (){return data?data->image_view->operator VkImageView():nullptr;}
|
operator VkImageView (){return data?data->image_view->operator VkImageView():nullptr;}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
SET(RENDER_DEVICE_VULKAN_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VK.h
|
SET(RENDER_DEVICE_VULKAN_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VK.h
|
||||||
|
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKMemory.h
|
||||||
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKBuffer.h
|
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKBuffer.h
|
||||||
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKBufferData.h
|
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKBufferData.h
|
||||||
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKCommandBuffer.h
|
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKCommandBuffer.h
|
||||||
@ -27,6 +28,7 @@
|
|||||||
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderTarget.h)
|
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderTarget.h)
|
||||||
|
|
||||||
SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
|
SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
|
||||||
|
VKMemory.cpp
|
||||||
VKInstance.cpp
|
VKInstance.cpp
|
||||||
VKPhysicalDevice.cpp
|
VKPhysicalDevice.cpp
|
||||||
VKImageView.cpp
|
VKImageView.cpp
|
||||||
@ -34,6 +36,7 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
|
|||||||
VKDeviceAttribute.cpp
|
VKDeviceAttribute.cpp
|
||||||
VKDeviceCreater.cpp
|
VKDeviceCreater.cpp
|
||||||
VKDevice.cpp
|
VKDevice.cpp
|
||||||
|
VKDeviceMemory.cpp
|
||||||
VKDeviceBuffer.cpp
|
VKDeviceBuffer.cpp
|
||||||
VKDeviceTexture.cpp
|
VKDeviceTexture.cpp
|
||||||
VKDeviceRenderPass.cpp
|
VKDeviceRenderPass.cpp
|
||||||
|
@ -23,6 +23,7 @@ uint8_t *Buffer::Map(uint32_t start,uint32_t size)
|
|||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::Unmap()
|
void Buffer::Unmap()
|
||||||
{
|
{
|
||||||
vkUnmapMemory(device,buf.memory);
|
vkUnmapMemory(device,buf.memory);
|
||||||
|
25
src/RenderDevice/Vulkan/VKDeviceMemory.cpp
Normal file
25
src/RenderDevice/Vulkan/VKDeviceMemory.cpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#include<hgl/graph/vulkan/VKDevice.h>
|
||||||
|
#include<hgl/graph/vulkan/VKMemory.h>
|
||||||
|
VK_NAMESPACE_BEGIN
|
||||||
|
Memory *Device::CreateMemory(const VkMemoryRequirements &req,uint32_t properties)
|
||||||
|
{
|
||||||
|
uint32_t index;
|
||||||
|
|
||||||
|
if(!attr->CheckMemoryType(req.memoryTypeBits,properties,&index))
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
VkMemoryAllocateInfo alloc_info;
|
||||||
|
|
||||||
|
alloc_info.sType =VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
|
alloc_info.pNext =nullptr;
|
||||||
|
alloc_info.memoryTypeIndex =index;
|
||||||
|
alloc_info.allocationSize =req.size;
|
||||||
|
|
||||||
|
VkDeviceMemory memory;
|
||||||
|
|
||||||
|
if(vkAllocateMemory(attr->device,&alloc_info,nullptr,&memory)!=VK_SUCCESS)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
return(new Memory(attr->device,memory,req,index,properties));
|
||||||
|
}
|
||||||
|
VK_NAMESPACE_END
|
@ -5,6 +5,7 @@
|
|||||||
#include<hgl/graph/vulkan/VKFence.h>
|
#include<hgl/graph/vulkan/VKFence.h>
|
||||||
#include<hgl/graph/vulkan/VKBuffer.h>
|
#include<hgl/graph/vulkan/VKBuffer.h>
|
||||||
#include<hgl/graph/vulkan/VKCommandBuffer.h>
|
#include<hgl/graph/vulkan/VKCommandBuffer.h>
|
||||||
|
#include<hgl/graph/vulkan/VKMemory.h>
|
||||||
VK_NAMESPACE_BEGIN
|
VK_NAMESPACE_BEGIN
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@ -55,21 +56,13 @@ Texture2D *Device::CreateTexture2D(const VkFormat video_format,uint32_t width,ui
|
|||||||
if(vkCreateImage(attr->device, &imageCreateInfo, nullptr, &image)!=VK_SUCCESS)
|
if(vkCreateImage(attr->device, &imageCreateInfo, nullptr, &image)!=VK_SUCCESS)
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
|
|
||||||
VkDeviceMemory memory;
|
|
||||||
|
|
||||||
VkMemoryAllocateInfo memAllocInfo;
|
|
||||||
VkMemoryRequirements memReqs;
|
VkMemoryRequirements memReqs;
|
||||||
|
|
||||||
memAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
||||||
memAllocInfo.pNext = nullptr;
|
|
||||||
|
|
||||||
vkGetImageMemoryRequirements(attr->device, image, &memReqs);
|
vkGetImageMemoryRequirements(attr->device, image, &memReqs);
|
||||||
|
|
||||||
memAllocInfo.allocationSize = memReqs.size;
|
Memory *dm=CreateMemory(memReqs,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||||
attr->CheckMemoryType(memReqs.memoryTypeBits,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,&memAllocInfo.memoryTypeIndex);
|
|
||||||
|
|
||||||
if(vkAllocateMemory(attr->device, &memAllocInfo, nullptr, &memory)==VK_SUCCESS)
|
if(dm&&dm->Bind(image))
|
||||||
if(vkBindImageMemory(attr->device, image, memory, 0)==VK_SUCCESS)
|
|
||||||
{
|
{
|
||||||
ImageView *image_view=CreateImageView2D(attr->device,video_format,aspectMask,image);
|
ImageView *image_view=CreateImageView2D(attr->device,video_format,aspectMask,image);
|
||||||
|
|
||||||
@ -79,7 +72,7 @@ Texture2D *Device::CreateTexture2D(const VkFormat video_format,uint32_t width,ui
|
|||||||
|
|
||||||
tex_data->ref = false;
|
tex_data->ref = false;
|
||||||
tex_data->mip_levels = 0;
|
tex_data->mip_levels = 0;
|
||||||
tex_data->memory = memory;
|
tex_data->memory = dm;
|
||||||
tex_data->image_layout = image_layout;
|
tex_data->image_layout = image_layout;
|
||||||
tex_data->image = image;
|
tex_data->image = image;
|
||||||
tex_data->image_view = image_view;
|
tex_data->image_view = image_view;
|
||||||
@ -91,6 +84,7 @@ Texture2D *Device::CreateTexture2D(const VkFormat video_format,uint32_t width,ui
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete dm;
|
||||||
vkDestroyImage(attr->device,image,nullptr);
|
vkDestroyImage(attr->device,image,nullptr);
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
}
|
}
|
||||||
|
42
src/RenderDevice/Vulkan/VKMemory.cpp
Normal file
42
src/RenderDevice/Vulkan/VKMemory.cpp
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#include<hgl/graph/vulkan/VKMemory.h>
|
||||||
|
VK_NAMESPACE_BEGIN
|
||||||
|
Memory::~Memory()
|
||||||
|
{
|
||||||
|
vkFreeMemory(device,memory,nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *Memory::Map()
|
||||||
|
{
|
||||||
|
void *result;
|
||||||
|
|
||||||
|
if(vkMapMemory(device,memory,0,req.size,0,&result)==VK_SUCCESS)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
return(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *Memory::Map(const VkDeviceSize offset,const VkDeviceSize size)
|
||||||
|
{
|
||||||
|
if(offset<0||offset+size>=req.size)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
void *result;
|
||||||
|
|
||||||
|
if(vkMapMemory(device,memory,0,size,0,&result)==VK_SUCCESS)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
return(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Memory::Unmap()
|
||||||
|
{
|
||||||
|
vkUnmapMemory(device,memory);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Memory::Bind(VkImage image)
|
||||||
|
{
|
||||||
|
if(!image)return(false);
|
||||||
|
|
||||||
|
return(vkBindImageMemory(device,image,memory,0)==VK_SUCCESS);
|
||||||
|
}
|
||||||
|
VK_NAMESPACE_END
|
@ -1,5 +1,6 @@
|
|||||||
#include<hgl/graph/vulkan/VKTexture.h>
|
#include<hgl/graph/vulkan/VKTexture.h>
|
||||||
#include<hgl/graph/vulkan/VKImageView.h>
|
#include<hgl/graph/vulkan/VKImageView.h>
|
||||||
|
#include<hgl/graph/vulkan/VKMemory.h>
|
||||||
VK_NAMESPACE_BEGIN
|
VK_NAMESPACE_BEGIN
|
||||||
Texture::~Texture()
|
Texture::~Texture()
|
||||||
{
|
{
|
||||||
@ -14,6 +15,6 @@ Texture::~Texture()
|
|||||||
vkDestroyImage(device,data->image,nullptr);
|
vkDestroyImage(device,data->image,nullptr);
|
||||||
|
|
||||||
if(data->memory)
|
if(data->memory)
|
||||||
vkFreeMemory(device,data->memory,nullptr);
|
delete data->memory;
|
||||||
}
|
}
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
|
Loading…
x
Reference in New Issue
Block a user