Toast Engine
Loading...
Searching...
No Matches
vulkan_texture.hpp
Go to the documentation of this file.
1
4
5#pragma once
6#include "ktx.h"
7#include "vulkan_common.hpp"
9
10#include <cstdint>
11#include <string>
12#include <string_view>
13
14namespace renderer {
15
16class VulkanTexture : public IVulkanResource {
17public:
18 VulkanTexture() = default;
19
20 struct Params {
21 vk::Format format;
22 vk::Extent3D extent;
23 uint32_t mip_levels = 1;
24 uint32_t layer_count = 1;
25 bool is_cubemap = false;
26 };
27
28 void create(const VulkanCore& core, Params params, std::string_view debug_name = {});
29 void destroy();
30
31 [[nodiscard]]
32 auto getImage() const -> vk::Image {
33 return **m_image;
34 }
35
36 [[nodiscard]]
37 auto getView() const -> vk::ImageView {
38 return *m_image_view;
39 }
40
41 [[nodiscard]]
42 auto getFormat() const -> vk::Format {
43 return m_params.format;
44 }
45
47 [[nodiscard]]
48 auto generation() const noexcept -> uint64_t {
49 return m_generation;
50 }
51
52private:
53 std::optional<vma::raii::Image> m_image;
54 vk::raii::ImageView m_image_view = nullptr;
55 Params m_params;
56 uint64_t m_generation = 0;
57};
58
59class TextureUpload : public PendingResourceUpload {
60public:
61 TextureUpload(VulkanTexture& texture, std::vector<uint8_t> data, std::string_view debug_name = {})
62 : m_texture(&texture),
63 m_data(std::move(data)),
64 m_debug_name(debug_name) { }
65
66 ~TextureUpload() override {
67 if (m_ktx_texture) {
68 ktxTexture2_Destroy(m_ktx_texture);
69 }
70 }
71
72 void build(const VulkanCore& core) override;
73 void record(vk::CommandBuffer cmd) override;
74 void recordAcquire(sync::BarrierBatch& batch) override;
75
76 auto resource() -> IVulkanResource* override { return m_texture; }
77
78private:
79 VulkanTexture* m_texture;
80 std::vector<uint8_t> m_data;
81 std::string m_debug_name;
82
83 ktxTexture2* m_ktx_texture = nullptr;
84 vma::raii::Buffer m_staging_buffer = nullptr;
85 std::vector<vk::BufferImageCopy> m_copy_regions;
86 VulkanTexture::Params m_tex_params {};
88 bool m_released = false;
89};
90
91class RawTextureUpload : public PendingResourceUpload {
92public:
93 RawTextureUpload(
94 VulkanTexture& texture, std::vector<uint8_t> data, uint32_t width, uint32_t height, vk::Format format,
95 std::string_view debug_name = {}
96 )
97 : m_texture(&texture),
98 m_data(std::move(data)),
99 m_width(width),
100 m_height(height),
101 m_format(format),
102 m_debug_name(debug_name) { }
103
104 void build(const VulkanCore& core) override;
105 void record(vk::CommandBuffer cmd) override;
106 void recordAcquire(sync::BarrierBatch& batch) override;
107
108 auto resource() -> IVulkanResource* override { return m_texture; }
109
110private:
111 VulkanTexture* m_texture = nullptr;
112 std::vector<uint8_t> m_data;
113 uint32_t m_width = 0;
114 uint32_t m_height = 0;
115 vk::Format m_format = vk::Format::eUndefined;
116 std::string m_debug_name;
117 vma::raii::Buffer m_staging_buffer = nullptr;
118 bool m_released = false;
119};
120
121auto uploadTextureSync(const VulkanCore& core, VulkanTexture& texture, std::vector<uint8_t> data, std::string_view debug_name)
122 -> bool;
123
124}
Definition vulkan_resource_base.hpp:12
Definition vulkan_resource_base.hpp:48
void recordAcquire(sync::BarrierBatch &batch) override
On the consuming queue after the upload fence signalled. No op for an upload that stayed on one queue...
Definition vulkan_texture.cpp:274
void recordAcquire(sync::BarrierBatch &batch) override
On the consuming queue after the upload fence signalled. No op for an upload that stayed on one queue...
Definition vulkan_texture.cpp:208
Definition vulkan_core.hpp:48
Definition vulkan_texture.hpp:16
auto generation() const noexcept -> uint64_t
Unique per create() so a descriptor cache notices a recreated view that reuses a destroyed handle val...
Definition vulkan_texture.hpp:48
Skips a barrier only when neither side writes and the layout and next read stages are already covered...
Definition gpu_sync.hpp:105
Definition vulkan_texture.hpp:20