add new Device::CreateImage functions

This commit is contained in:
hyzboy 2020-06-24 19:13:29 +08:00
parent 9d9dd30416
commit 2103c4d23c
8 changed files with 259 additions and 9 deletions

View File

@ -79,11 +79,11 @@ struct PushConstant
constexpr uint32_t MAX_PUSH_CONSTANT_BYTES=sizeof(PushConstant);
inline void copy(VkExtent3D &e3d,const VkExtent2D &e2d)
inline void copy(VkExtent3D &e3d,const VkExtent2D &e2d,const uint32 depth=1)
{
e3d.width =e2d.width;
e3d.height =e2d.height;
e3d.depth =1;
e3d.depth =depth;
}
inline void debug_out(const hgl::List<VkLayerProperties> &layer_properties)

View File

@ -108,7 +108,13 @@ public: //Buffer相关
public: //Image
VkImage CreateImage(const VkFormat format,uint32_t width,uint32_t height,const uint usage,const VkImageTiling tiling);
VkImage CreateImage1D (const VkFormat format,const uint32_t width,const uint usage,const VkImageTiling tiling);
VkImage CreateImage1DArray (const VkFormat format,const uint32_t width,const uint32_t layer,const uint usage,const VkImageTiling tiling);
VkImage CreateImage2D (const VkFormat format,const uint32_t width,const uint32_t height,const uint usage,const VkImageTiling tiling);
VkImage CreateImage2DArray (const VkFormat format,const uint32_t width,const uint32_t height,const uint32_t layer,const uint usage,const VkImageTiling tiling);
VkImage CreateImage3D (const VkFormat format,const uint32_t width,const uint32_t height,const uint32_t depth,const uint usage,const VkImageTiling tiling);
VkImage CreateImageCubemap (const VkFormat format,const uint32_t width,const uint32_t height,const uint usage,const VkImageTiling tiling);
void DestoryImage(VkImage);
Memory *CreateMemory(VkImage,const uint32 flag=VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);

View File

@ -157,4 +157,67 @@ Texture2D *CreateTextureFromFile(Device *device,const OSString &filename)
return tex;
}
}
struct Texture2DData
{
uint32 width;
uint32 height;
VkFormat format;
uint32 bytes;
};//
void *LoadTextureFromFile(const OSString &filename)
{
io::OpenFileInputStream fis(filename);
if(!fis)
{
LOG_ERROR(OS_TEXT("[ERROR] open texture file<")+filename+OS_TEXT("> failed."));
return(nullptr);
}
const int64 file_length=fis->GetSize();
if(file_length<sizeof(Tex2DFileHeader))
return(nullptr);
Tex2DFileHeader file_header;
if(fis->Read(&file_header,sizeof(Tex2DFileHeader))!=sizeof(Tex2DFileHeader))
return(nullptr);
if(file_header.version!=2)
return(nullptr);
if(file_header.total_bytes()==0)
return(nullptr);
const VkFormat format=file_header.vk_format();
if(!CheckVulkanFormat(format))
return(nullptr);
const uint total_bytes=file_header.total_bytes();
if(file_length<sizeof(Tex2DFileHeader)+total_bytes)
return(nullptr);
{
vulkan::Buffer *buf=device->CreateBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT,total_bytes);
if(!buf)
return(nullptr);
void *pixel_data=buf->Map();
fis->Read(pixel_data,total_bytes);
buf->Unmap();
Texture2D *tex=device->CreateTexture2D(format,buf,file_header.width,file_header.height);
delete buf;
return tex;
}
}
VK_NAMESPACE_END

View File

@ -87,7 +87,7 @@ namespace
{
VkExtent3D extent;
copy(extent,ext);
copy(extent,ext,1);
return CreateImageView(device,VK_IMAGE_VIEW_TYPE_2D,format,extent,VK_IMAGE_ASPECT_DEPTH_BIT,img);
}

View File

@ -1,7 +1,90 @@
#include<hgl/graph/vulkan/VKDevice.h>
VK_NAMESPACE_BEGIN
VkImage Device::CreateImage(const VkFormat format,uint32_t width,uint32_t height,const uint usage,const VkImageTiling tiling)
namespace
{
void InitImageCreateInfo(VkImageCreateInfo &imageCreateInfo)
{
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageCreateInfo.pNext = nullptr;
imageCreateInfo.flags = 0;
imageCreateInfo.mipLevels = 1;
imageCreateInfo.arrayLayers = 1;
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageCreateInfo.queueFamilyIndexCount = 0;
imageCreateInfo.pQueueFamilyIndices = nullptr;
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
}
}//namespace
VkImage Device::CreateImage1D(const VkFormat format,const uint32_t length,const uint usage,const VkImageTiling tiling)
{
if(!CheckVulkanFormat(format))return(nullptr);
if(length<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 = length;
imageCreateInfo.extent.height = 1;
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(attr->device,&imageCreateInfo, nullptr, &image)!=VK_SUCCESS)
return(nullptr);
return image;
}
VkImage Device::CreateImage1DArray(const VkFormat format,const uint32_t length,const uint32_t layer,const uint usage,const VkImageTiling tiling)
{
if(!CheckVulkanFormat(format))return(nullptr);
if(length<1||layer<1)return(nullptr);
VkImageCreateInfo imageCreateInfo;
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageCreateInfo.pNext = nullptr;
imageCreateInfo.flags = 0;
imageCreateInfo.imageType = VK_IMAGE_TYPE_1D;
imageCreateInfo.format = format;
imageCreateInfo.extent.width = length;
imageCreateInfo.extent.height = 1;
imageCreateInfo.extent.depth = 1;
imageCreateInfo.mipLevels = 1;
imageCreateInfo.arrayLayers = layer;
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(attr->device,&imageCreateInfo, nullptr, &image)!=VK_SUCCESS)
return(nullptr);
return image;
}
VkImage Device::CreateImage2D(const VkFormat format,const uint32_t width,const uint32_t height,const uint usage,const VkImageTiling tiling)
{
if(!CheckVulkanFormat(format))return(nullptr);
if(width<1||height<1)return(nullptr);
@ -34,6 +117,105 @@ VkImage Device::CreateImage(const VkFormat format,uint32_t width,uint32_t height
return image;
}
VkImage Device::CreateImage2DArray(const VkFormat format,const uint32_t width,const uint32_t height,const uint32_t layer,const uint usage,const VkImageTiling tiling)
{
if(!CheckVulkanFormat(format))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 = layer;
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(attr->device,&imageCreateInfo, nullptr, &image)!=VK_SUCCESS)
return(nullptr);
return image;
}
VkImage Device::CreateImage3D(const VkFormat format,const uint32_t width,const uint32_t height,const uint32_t depth,const uint usage,const VkImageTiling tiling)
{
if(!CheckVulkanFormat(format))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_3D;
imageCreateInfo.format = format;
imageCreateInfo.extent.width = width;
imageCreateInfo.extent.height = height;
imageCreateInfo.extent.depth = depth;
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(attr->device,&imageCreateInfo, nullptr, &image)!=VK_SUCCESS)
return(nullptr);
return image;
}
VkImage Device::CreateImageCubemap(const VkFormat format,const uint32_t width,const uint32_t height,const uint usage,const VkImageTiling tiling)
{
if(!CheckVulkanFormat(format))return(nullptr);
if(width<1||height<1)return(nullptr);
VkImageCreateInfo imageCreateInfo;
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageCreateInfo.pNext = nullptr;
imageCreateInfo.flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
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 = 6;
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(attr->device,&imageCreateInfo, nullptr, &image)!=VK_SUCCESS)
return(nullptr);
return image;
}
void Device::DestoryImage(VkImage img)
{
if(img==VK_NULL_HANDLE)return;

View File

@ -64,7 +64,7 @@ Texture2D *Device::CreateTexture2D(const VkFormat format,uint32_t width,uint32_t
return(nullptr);
}
VkImage img=CreateImage(format,width,height,usage,tiling);
VkImage img=CreateImage2D(format,width,height,usage,tiling);
if(!img)return(nullptr);

View File

@ -18,9 +18,9 @@ ImageView *CreateImageView(VkDevice device,VkImageViewType type,VkFormat format,
iv_createinfo.viewType=type;
iv_createinfo.subresourceRange.aspectMask=aspectMask;
iv_createinfo.subresourceRange.baseMipLevel=0;
iv_createinfo.subresourceRange.levelCount=1;
iv_createinfo.subresourceRange.levelCount=ext.depth;
iv_createinfo.subresourceRange.baseArrayLayer=0;
iv_createinfo.subresourceRange.layerCount=1;
iv_createinfo.subresourceRange.layerCount=ext.depth;
if(aspectMask&VK_IMAGE_ASPECT_DEPTH_BIT)
{

View File

@ -95,7 +95,6 @@ namespace hgl
// vertex->End();
//}
}
/**
* Tile