将CreateMemory,CreateTexture2D函数独立出来以方便调用
This commit is contained in:
parent
4083306b67
commit
6cdb573221
@ -13,7 +13,7 @@ class Memory
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
friend class Device;
|
friend Memory *CreateMemory(VkDevice device,const PhysicalDevice *pd,const VkMemoryRequirements &req,uint32_t properties);
|
||||||
|
|
||||||
Memory(VkDevice dev,VkDeviceMemory dm,const VkMemoryRequirements &mr,const uint32 i,const uint32_t p)
|
Memory(VkDevice dev,VkDeviceMemory dm,const VkMemoryRequirements &mr,const uint32 i,const uint32_t p)
|
||||||
{
|
{
|
||||||
@ -44,5 +44,7 @@ public:
|
|||||||
|
|
||||||
bool Bind(VkImage image);
|
bool Bind(VkImage image);
|
||||||
};//class Memory
|
};//class Memory
|
||||||
|
|
||||||
|
Memory *CreateMemory(VkDevice device,const PhysicalDevice *pd,const VkMemoryRequirements &req,uint32_t properties);
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
#endif//HGL_GRAPH_VULKAN_MEMORY_INCLUDE
|
#endif//HGL_GRAPH_VULKAN_MEMORY_INCLUDE
|
||||||
|
@ -95,5 +95,8 @@ public:
|
|||||||
//{
|
//{
|
||||||
// uint32_t width,height,count;
|
// uint32_t width,height,count;
|
||||||
//};//class TextureCubemapArray:public Texture
|
//};//class TextureCubemapArray:public Texture
|
||||||
|
|
||||||
|
Texture2D *CreateTexture2D(VkDevice device,const PhysicalDevice *pd,const VkFormat format,uint32_t width,uint32_t height,const VkImageAspectFlags aspectMask,const uint usage,const VkImageLayout image_layout,const VkImageTiling tiling);
|
||||||
|
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
#endif//HGL_GRAPH_VULKAN_TEXTURE_INCLUDE
|
#endif//HGL_GRAPH_VULKAN_TEXTURE_INCLUDE
|
||||||
|
@ -36,7 +36,6 @@ 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
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
#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
|
|
@ -20,78 +20,20 @@ namespace
|
|||||||
}
|
}
|
||||||
}//namespace
|
}//namespace
|
||||||
|
|
||||||
Texture2D *Device::CreateTexture2D(const VkFormat video_format,uint32_t width,uint32_t height,const VkImageAspectFlags aspectMask,const uint usage,const VkImageLayout image_layout)
|
Texture2D *Device::CreateTexture2D(const VkFormat format,uint32_t width,uint32_t height,const VkImageAspectFlags aspectMask,const uint usage,const VkImageLayout image_layout)
|
||||||
{
|
{
|
||||||
if(video_format<VK_FORMAT_BEGIN_RANGE||video_format>VK_FORMAT_END_RANGE)return(nullptr);
|
const VkFormatProperties fp=attr->physical_device->GetFormatProperties(format);
|
||||||
if(width<1||height<1)return(nullptr);
|
|
||||||
|
|
||||||
const VkFormatProperties fp=attr->physical_device->GetFormatProperties(video_format);
|
|
||||||
|
|
||||||
if(!(fp.optimalTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)
|
if(!(fp.optimalTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)
|
||||||
&&!(fp.linearTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
|
&&!(fp.linearTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
|
|
||||||
VkImageCreateInfo imageCreateInfo;
|
return VK_NAMESPACE::CreateTexture2D(attr->device,attr->physical_device,format,width,height,aspectMask,usage,image_layout,(fp.optimalTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)?VK_IMAGE_TILING_OPTIMAL:VK_IMAGE_TILING_LINEAR);
|
||||||
|
|
||||||
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
|
||||||
imageCreateInfo.pNext = nullptr;
|
|
||||||
imageCreateInfo.flags = 0;
|
|
||||||
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
||||||
imageCreateInfo.format = video_format;
|
|
||||||
imageCreateInfo.extent.width = width;
|
|
||||||
imageCreateInfo.extent.height = height;
|
|
||||||
imageCreateInfo.extent.depth = 1;
|
|
||||||
imageCreateInfo.mipLevels = 1;
|
|
||||||
imageCreateInfo.arrayLayers = 1;
|
|
||||||
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
||||||
imageCreateInfo.usage = usage;
|
|
||||||
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
||||||
imageCreateInfo.queueFamilyIndexCount = 0;
|
|
||||||
imageCreateInfo.pQueueFamilyIndices = nullptr;
|
|
||||||
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
||||||
imageCreateInfo.tiling = (fp.optimalTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)?VK_IMAGE_TILING_OPTIMAL:VK_IMAGE_TILING_LINEAR;
|
|
||||||
|
|
||||||
VkImage image;
|
|
||||||
|
|
||||||
if(vkCreateImage(attr->device, &imageCreateInfo, nullptr, &image)!=VK_SUCCESS)
|
|
||||||
return(nullptr);
|
|
||||||
|
|
||||||
VkMemoryRequirements memReqs;
|
|
||||||
|
|
||||||
vkGetImageMemoryRequirements(attr->device, image, &memReqs);
|
|
||||||
|
|
||||||
Memory *dm=CreateMemory(memReqs,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
||||||
|
|
||||||
if(dm&&dm->Bind(image))
|
|
||||||
{
|
|
||||||
ImageView *image_view=CreateImageView2D(attr->device,video_format,aspectMask,image);
|
|
||||||
|
|
||||||
if(image_view)
|
|
||||||
{
|
|
||||||
TextureData *tex_data=new TextureData;
|
|
||||||
|
|
||||||
tex_data->ref = false;
|
|
||||||
tex_data->mip_levels = 0;
|
|
||||||
tex_data->memory = dm;
|
|
||||||
tex_data->image_layout = image_layout;
|
|
||||||
tex_data->image = image;
|
|
||||||
tex_data->image_view = image_view;
|
|
||||||
tex_data->format = video_format;
|
|
||||||
tex_data->aspect = aspectMask;
|
|
||||||
tex_data->extent = imageCreateInfo.extent;
|
|
||||||
|
|
||||||
return(new Texture2D(width,height,attr->device,tex_data));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
delete dm;
|
|
||||||
vkDestroyImage(attr->device,image,nullptr);
|
|
||||||
return(nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture2D *Device::CreateTexture2D(const VkFormat video_format,void *data,uint32_t width,uint32_t height,uint32_t size,const VkImageAspectFlags aspectMask,const uint usage,const VkImageLayout image_layout)
|
Texture2D *Device::CreateTexture2D(const VkFormat format,void *data,uint32_t width,uint32_t height,uint32_t size,const VkImageAspectFlags aspectMask,const uint usage,const VkImageLayout image_layout)
|
||||||
{
|
{
|
||||||
Texture2D *tex=CreateTexture2D(video_format,width,height,aspectMask,usage,image_layout);
|
Texture2D *tex=CreateTexture2D(format,width,height,aspectMask,usage,image_layout);
|
||||||
|
|
||||||
if(!tex)return(nullptr);
|
if(!tex)return(nullptr);
|
||||||
|
|
||||||
|
@ -1,5 +1,28 @@
|
|||||||
#include<hgl/graph/vulkan/VKMemory.h>
|
#include<hgl/graph/vulkan/VKMemory.h>
|
||||||
|
#include<hgl/graph/vulkan/VKPhysicalDevice.h>
|
||||||
VK_NAMESPACE_BEGIN
|
VK_NAMESPACE_BEGIN
|
||||||
|
Memory *CreateMemory(VkDevice device,const PhysicalDevice *pd,const VkMemoryRequirements &req,uint32_t properties)
|
||||||
|
{
|
||||||
|
uint32_t index;
|
||||||
|
|
||||||
|
if(!pd->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(device,&alloc_info,nullptr,&memory)!=VK_SUCCESS)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
return(new Memory(device,memory,req,index,properties));
|
||||||
|
}
|
||||||
|
|
||||||
Memory::~Memory()
|
Memory::~Memory()
|
||||||
{
|
{
|
||||||
vkFreeMemory(device,memory,nullptr);
|
vkFreeMemory(device,memory,nullptr);
|
||||||
|
@ -17,4 +17,67 @@ Texture::~Texture()
|
|||||||
if(data->memory)
|
if(data->memory)
|
||||||
delete data->memory;
|
delete data->memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Texture2D *CreateTexture2D(VkDevice device,const PhysicalDevice *pd,const VkFormat format,uint32_t width,uint32_t height,const VkImageAspectFlags aspectMask,const uint usage,const VkImageLayout image_layout,const VkImageTiling tiling)
|
||||||
|
{
|
||||||
|
if(format<VK_FORMAT_BEGIN_RANGE||format>VK_FORMAT_END_RANGE)return(nullptr);
|
||||||
|
if(width<1||height<1)return(nullptr);
|
||||||
|
|
||||||
|
VkImageCreateInfo imageCreateInfo;
|
||||||
|
|
||||||
|
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||||||
|
imageCreateInfo.pNext = nullptr;
|
||||||
|
imageCreateInfo.flags = 0;
|
||||||
|
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
||||||
|
imageCreateInfo.format = format;
|
||||||
|
imageCreateInfo.extent.width = width;
|
||||||
|
imageCreateInfo.extent.height = height;
|
||||||
|
imageCreateInfo.extent.depth = 1;
|
||||||
|
imageCreateInfo.mipLevels = 1;
|
||||||
|
imageCreateInfo.arrayLayers = 1;
|
||||||
|
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
|
imageCreateInfo.usage = usage;
|
||||||
|
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
imageCreateInfo.queueFamilyIndexCount = 0;
|
||||||
|
imageCreateInfo.pQueueFamilyIndices = nullptr;
|
||||||
|
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||||
|
imageCreateInfo.tiling = tiling;
|
||||||
|
|
||||||
|
VkImage image;
|
||||||
|
|
||||||
|
if(vkCreateImage(device, &imageCreateInfo, nullptr, &image)!=VK_SUCCESS)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
VkMemoryRequirements memReqs;
|
||||||
|
|
||||||
|
vkGetImageMemoryRequirements(device, image, &memReqs);
|
||||||
|
|
||||||
|
Memory *dm=CreateMemory(device,pd,memReqs,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||||
|
|
||||||
|
if(dm&&dm->Bind(image))
|
||||||
|
{
|
||||||
|
ImageView *image_view=CreateImageView2D(device,format,aspectMask,image);
|
||||||
|
|
||||||
|
if(image_view)
|
||||||
|
{
|
||||||
|
TextureData *tex_data=new TextureData;
|
||||||
|
|
||||||
|
tex_data->ref = false;
|
||||||
|
tex_data->mip_levels = 0;
|
||||||
|
tex_data->memory = dm;
|
||||||
|
tex_data->image_layout = image_layout;
|
||||||
|
tex_data->image = image;
|
||||||
|
tex_data->image_view = image_view;
|
||||||
|
tex_data->format = format;
|
||||||
|
tex_data->aspect = aspectMask;
|
||||||
|
tex_data->extent = imageCreateInfo.extent;
|
||||||
|
|
||||||
|
return(new Texture2D(width,height,device,tex_data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delete dm;
|
||||||
|
vkDestroyImage(device,image,nullptr);
|
||||||
|
return(nullptr);
|
||||||
|
}
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
|
Loading…
x
Reference in New Issue
Block a user