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