CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/GPU/Vulkan/VulkanImage.h
Views: 1401
1
#pragma once
2
3
#include "VulkanLoader.h"
4
5
class VulkanContext;
6
class VulkanDeviceAllocator;
7
8
VK_DEFINE_HANDLE(VmaAllocation);
9
10
class VulkanBarrierBatch;
11
12
struct TextureCopyBatch {
13
std::vector<VkBufferImageCopy> copies;
14
VkBuffer buffer = VK_NULL_HANDLE;
15
void reserve(size_t mips) { copies.reserve(mips); }
16
bool empty() const { return copies.empty(); }
17
};
18
19
// Wrapper around what you need to use a texture.
20
// ALWAYS use an allocator when calling CreateDirect.
21
class VulkanTexture {
22
public:
23
VulkanTexture(VulkanContext *vulkan, const char *tag);
24
~VulkanTexture() {
25
Destroy();
26
}
27
28
// Fast uploads from buffer. Mipmaps supported.
29
// Usage must at least include VK_IMAGE_USAGE_TRANSFER_DST_BIT in order to use UploadMip.
30
// When using UploadMip, initialLayout should be VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL.
31
bool CreateDirect(int w, int h, int depth, int numMips, VkFormat format, VkImageLayout initialLayout, VkImageUsageFlags usage, VulkanBarrierBatch *barrierBatch, const VkComponentMapping *mapping = nullptr);
32
void ClearMip(VkCommandBuffer cmd, int mip, uint32_t value);
33
34
// Can also be used to copy individual levels of a 3D texture.
35
// If possible, will just add to the batch instead of submitting a copy.
36
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
37
void FinishCopyBatch(VkCommandBuffer cmd, TextureCopyBatch *copyBatch);
38
39
void GenerateMips(VkCommandBuffer cmd, int firstMipToGenerate, bool fromCompute);
40
void EndCreate(VkCommandBuffer cmd, bool vertexTexture, VkPipelineStageFlags prevStage, VkImageLayout layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
41
42
// For updating levels after creation. Careful with the timelines!
43
void PrepareForTransferDst(VkCommandBuffer cmd, int levels);
44
void RestoreAfterTransferDst(int levels, VulkanBarrierBatch *barriers);
45
46
// When loading mips from compute shaders, you need to pass VK_IMAGE_LAYOUT_GENERAL to the above function.
47
// In addition, ignore UploadMip and GenerateMip, and instead use GetViewForMip. Make sure to delete the returned views when used.
48
VkImageView CreateViewForMip(int mip);
49
50
void Destroy();
51
52
const char *Tag() const {
53
return tag_;
54
}
55
56
// Used in image copies, etc.
57
VkImage GetImage() const { return image_; }
58
59
// Used for sampling, generally.
60
VkImageView GetImageView() const { return view_; }
61
62
// For use with some shaders, we might want to view it as a single entry array for convenience.
63
VkImageView GetImageArrayView() const { return arrayView_; }
64
65
int32_t GetWidth() const { return width_; }
66
int32_t GetHeight() const { return height_; }
67
int32_t GetNumMips() const { return numMips_; }
68
VkFormat GetFormat() const { return format_; }
69
70
private:
71
void Wipe();
72
73
VulkanContext *vulkan_;
74
VkImage image_ = VK_NULL_HANDLE;
75
VkImageView view_ = VK_NULL_HANDLE;
76
VkImageView arrayView_ = VK_NULL_HANDLE;
77
VmaAllocation allocation_ = VK_NULL_HANDLE;
78
79
int16_t width_ = 0;
80
int16_t height_ = 0;
81
int16_t numMips_ = 1;
82
int16_t depth_ = 1;
83
84
VkFormat format_ = VK_FORMAT_UNDEFINED;
85
char tag_[64];
86
};
87
88