2016-03-17 11:56:43 +01:00
# pragma once
2021-12-12 11:34:05 +01:00
# include "VulkanLoader.h"
2016-03-17 11:56:43 +01:00
2021-12-12 11:34:05 +01:00
class VulkanContext ;
2016-03-26 18:22:21 -07:00
class VulkanDeviceAllocator ;
2021-11-21 23:38:14 +01:00
VK_DEFINE_HANDLE ( VmaAllocation ) ;
2023-03-14 10:11:19 +01:00
struct TextureCopyBatch {
std : : vector < VkBufferImageCopy > copies ;
VkBuffer buffer = VK_NULL_HANDLE ;
void reserve ( size_t mips ) { copies . reserve ( mips ) ; }
bool empty ( ) const { return copies . empty ( ) ; }
} ;
2016-03-17 11:56:43 +01:00
// Wrapper around what you need to use a texture.
2020-05-06 22:21:22 +02:00
// ALWAYS use an allocator when calling CreateDirect.
2016-03-17 11:56:43 +01:00
class VulkanTexture {
public :
2022-12-01 11:58:16 +01:00
VulkanTexture ( VulkanContext * vulkan , const char * tag ) ;
2016-03-17 11:56:43 +01:00
~ VulkanTexture ( ) {
Destroy ( ) ;
}
2016-03-28 18:18:49 +02:00
// Fast uploads from buffer. Mipmaps supported.
// Usage must at least include VK_IMAGE_USAGE_TRANSFER_DST_BIT in order to use UploadMip.
// When using UploadMip, initialLayout should be VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL.
2022-07-25 18:51:08 +02:00
bool CreateDirect ( VkCommandBuffer cmd , int w , int h , int depth , int numMips , VkFormat format , VkImageLayout initialLayout , VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT , const VkComponentMapping * mapping = nullptr ) ;
2020-09-01 00:05:37 +02:00
void ClearMip ( VkCommandBuffer cmd , int mip , uint32_t value ) ;
2022-07-25 18:51:08 +02:00
// Can also be used to copy individual levels of a 3D texture.
2023-03-14 10:11:19 +01:00
// If possible, will just add to the batch instead of submitting a copy.
void CopyBufferToMipLevel ( VkCommandBuffer cmd , TextureCopyBatch * copyBatch , int mip , int mipWidth , int mipHeight , int depthLayer , VkBuffer buffer , uint32_t offset , size_t rowLength ) ; // rowLength is in pixels
void FinishCopyBatch ( VkCommandBuffer cmd , TextureCopyBatch * copyBatch ) ;
2021-10-05 22:49:33 +02:00
void GenerateMips ( VkCommandBuffer cmd , int firstMipToGenerate , bool fromCompute ) ;
2021-10-08 21:58:03 +02:00
void EndCreate ( VkCommandBuffer cmd , bool vertexTexture , VkPipelineStageFlags prevStage , VkImageLayout layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL ) ;
2017-12-11 12:22:24 +01:00
2023-05-30 17:32:11 +02:00
// For updating levels after creation. Careful with the timelines!
void PrepareForTransferDst ( VkCommandBuffer cmd , int levels ) ;
void RestoreAfterTransferDst ( VkCommandBuffer cmd , int levels ) ;
2017-12-11 12:22:24 +01:00
// When loading mips from compute shaders, you need to pass VK_IMAGE_LAYOUT_GENERAL to the above function.
// In addition, ignore UploadMip and GenerateMip, and instead use GetViewForMip. Make sure to delete the returned views when used.
VkImageView CreateViewForMip ( int mip ) ;
2017-05-21 23:13:53 +02:00
2016-03-17 11:56:43 +01:00
void Destroy ( ) ;
2022-12-01 11:58:16 +01:00
const char * Tag ( ) const {
2018-04-06 21:14:53 -07:00
return tag_ ;
}
2022-12-01 11:58:16 +01:00
2016-03-28 19:57:42 +02:00
// Used in image copies, etc.
2018-02-26 10:50:29 +01:00
VkImage GetImage ( ) const { return image_ ; }
2016-03-28 19:57:42 +02:00
// Used for sampling, generally.
2018-02-26 10:50:29 +01:00
VkImageView GetImageView ( ) const { return view_ ; }
2016-03-17 11:56:43 +01:00
2022-10-25 23:32:28 +02:00
// For use with some shaders, we might want to view it as a single entry array for convenience.
VkImageView GetImageArrayView ( ) const { return arrayView_ ; }
2018-02-26 10:50:29 +01:00
int32_t GetWidth ( ) const { return width_ ; }
int32_t GetHeight ( ) const { return height_ ; }
2017-11-05 18:04:56 -08:00
int32_t GetNumMips ( ) const { return numMips_ ; }
VkFormat GetFormat ( ) const { return format_ ; }
2016-03-28 19:57:42 +02:00
2016-03-17 11:56:43 +01:00
private :
void Wipe ( ) ;
2016-03-28 19:57:42 +02:00
2016-03-17 11:56:43 +01:00
VulkanContext * vulkan_ ;
2018-02-26 10:50:29 +01:00
VkImage image_ = VK_NULL_HANDLE ;
VkImageView view_ = VK_NULL_HANDLE ;
2022-10-25 23:32:28 +02:00
VkImageView arrayView_ = VK_NULL_HANDLE ;
2022-04-23 22:52:28 +02:00
VmaAllocation allocation_ = VK_NULL_HANDLE ;
2021-11-21 23:38:14 +01:00
2022-07-25 18:51:08 +02:00
int16_t width_ = 0 ;
int16_t height_ = 0 ;
int16_t numMips_ = 1 ;
int16_t depth_ = 1 ;
2018-02-26 10:50:29 +01:00
VkFormat format_ = VK_FORMAT_UNDEFINED ;
2022-12-01 11:58:16 +01:00
char tag_ [ 64 ] ;
2016-03-17 11:56:43 +01:00
} ;