CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/GPU/Vulkan/VulkanImage.h
Views: 1401
#pragma once12#include "VulkanLoader.h"34class VulkanContext;5class VulkanDeviceAllocator;67VK_DEFINE_HANDLE(VmaAllocation);89class VulkanBarrierBatch;1011struct TextureCopyBatch {12std::vector<VkBufferImageCopy> copies;13VkBuffer buffer = VK_NULL_HANDLE;14void reserve(size_t mips) { copies.reserve(mips); }15bool empty() const { return copies.empty(); }16};1718// Wrapper around what you need to use a texture.19// ALWAYS use an allocator when calling CreateDirect.20class VulkanTexture {21public:22VulkanTexture(VulkanContext *vulkan, const char *tag);23~VulkanTexture() {24Destroy();25}2627// Fast uploads from buffer. Mipmaps supported.28// Usage must at least include VK_IMAGE_USAGE_TRANSFER_DST_BIT in order to use UploadMip.29// When using UploadMip, initialLayout should be VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL.30bool CreateDirect(int w, int h, int depth, int numMips, VkFormat format, VkImageLayout initialLayout, VkImageUsageFlags usage, VulkanBarrierBatch *barrierBatch, const VkComponentMapping *mapping = nullptr);31void ClearMip(VkCommandBuffer cmd, int mip, uint32_t value);3233// Can also be used to copy individual levels of a 3D texture.34// If possible, will just add to the batch instead of submitting a copy.35void CopyBufferToMipLevel(VkCommandBuffer cmd, TextureCopyBatch *copyBatch, int mip, int mipWidth, int mipHeight, int depthLayer, VkBuffer buffer, uint32_t offset, size_t rowLength); // rowLength is in pixels36void FinishCopyBatch(VkCommandBuffer cmd, TextureCopyBatch *copyBatch);3738void GenerateMips(VkCommandBuffer cmd, int firstMipToGenerate, bool fromCompute);39void EndCreate(VkCommandBuffer cmd, bool vertexTexture, VkPipelineStageFlags prevStage, VkImageLayout layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);4041// For updating levels after creation. Careful with the timelines!42void PrepareForTransferDst(VkCommandBuffer cmd, int levels);43void RestoreAfterTransferDst(int levels, VulkanBarrierBatch *barriers);4445// When loading mips from compute shaders, you need to pass VK_IMAGE_LAYOUT_GENERAL to the above function.46// In addition, ignore UploadMip and GenerateMip, and instead use GetViewForMip. Make sure to delete the returned views when used.47VkImageView CreateViewForMip(int mip);4849void Destroy();5051const char *Tag() const {52return tag_;53}5455// Used in image copies, etc.56VkImage GetImage() const { return image_; }5758// Used for sampling, generally.59VkImageView GetImageView() const { return view_; }6061// For use with some shaders, we might want to view it as a single entry array for convenience.62VkImageView GetImageArrayView() const { return arrayView_; }6364int32_t GetWidth() const { return width_; }65int32_t GetHeight() const { return height_; }66int32_t GetNumMips() const { return numMips_; }67VkFormat GetFormat() const { return format_; }6869private:70void Wipe();7172VulkanContext *vulkan_;73VkImage image_ = VK_NULL_HANDLE;74VkImageView view_ = VK_NULL_HANDLE;75VkImageView arrayView_ = VK_NULL_HANDLE;76VmaAllocation allocation_ = VK_NULL_HANDLE;7778int16_t width_ = 0;79int16_t height_ = 0;80int16_t numMips_ = 1;81int16_t depth_ = 1;8283VkFormat format_ = VK_FORMAT_UNDEFINED;84char tag_[64];85};868788