Toast Engine
Loading...
Searching...
No Matches
vulkan_mesh.hpp
1
4
5#pragma once
6
7#include "glm/glm.hpp"
8#include "vertex.hpp"
9#include "vulkan_common.hpp"
11
12#include <array>
13#include <optional>
14#include <string>
15#include <string_view>
17#include <utility>
18
19namespace renderer {
20class VulkanCore;
21
22auto vertexBindingDescription() -> vk::VertexInputBindingDescription;
23
24auto vertexAttributeDescriptions() -> std::array<vk::VertexInputAttributeDescription, 5>;
25
26class VulkanMesh : public IVulkanResource {
27public:
28 VulkanMesh() = default;
29
30 struct UploadData {
31 std::span<const Vertex> vertices;
32 std::span<const uint32_t> indices;
33 std::span<const SkinVertex> skin_vertices;
34 };
35
36 void create(
37 const renderer::VulkanCore& core, UploadData data, uint32_t graphics_queue_family_index,
38 uint32_t transfer_queue_family_index, std::string_view debug_name = {}
39 );
40
41 void destroy();
42
43 void bind(vk::CommandBuffer cmd) const;
44
45 void bindPosed(vk::CommandBuffer cmd, vk::Buffer posed_vertices, uint32_t posed_vertex_offset) const;
46
47 [[nodiscard]]
48 auto getVertexCount() const noexcept -> uint32_t {
49 return m_vertex_count;
50 }
51
52 [[nodiscard]]
53 auto getVertexBuffer() const -> vk::Buffer {
54 return m_vertex_buffer.has_value() ? **m_vertex_buffer : vk::Buffer {};
55 }
56
57 [[nodiscard]]
58 auto getSkinVertexBuffer() const -> vk::Buffer {
59 return m_skin_vertex_buffer.has_value() ? **m_skin_vertex_buffer : vk::Buffer {};
60 }
61
62 [[nodiscard]]
63 auto getIndexBuffer() const -> vk::Buffer {
64 return m_index_buffer.has_value() ? **m_index_buffer : vk::Buffer {};
65 }
66
67 [[nodiscard]]
68 auto getIndexCount() const noexcept -> uint32_t {
69 return m_index_count;
70 }
71
72 void draw(vk::CommandBuffer cmd, uint32_t instance_count = 1) const;
73
74 [[nodiscard]]
75 auto isSkinned() const noexcept -> bool {
76 return m_skin_vertex_buffer.has_value();
77 }
78
79 void recordUpload(
80 vk::CommandBuffer cmd, vk::Buffer staging_buffer, vk::DeviceSize vertex_offset, vk::DeviceSize index_offset,
81 vk::DeviceSize skin_vertex_offset
82 ) const;
83
84 auto createAccelerationStructure(const VulkanCore& core) -> std::optional<vma::raii::Buffer>;
85
86 void recordBuildAccelerationStructure(vk::CommandBuffer cmd) const;
87
88 [[nodiscard]]
89 auto hasAccelerationStructure() const noexcept -> bool {
90 return m_blas_address != 0;
91 }
92
93 [[nodiscard]]
94 auto getAccelerationStructureAddress() const noexcept -> vk::DeviceAddress {
95 return m_blas_address;
96 }
97
98private:
99 std::optional<vma::raii::Buffer> m_vertex_buffer;
100 std::optional<vma::raii::Buffer> m_index_buffer;
101 std::optional<vma::raii::Buffer> m_skin_vertex_buffer;
102
103 vk::DeviceSize m_vertex_size = 0;
104 vk::DeviceSize m_index_size = 0;
105 vk::DeviceSize m_skin_vertex_size = 0;
106
107 uint32_t m_vertex_count = 0;
108 uint32_t m_index_count = 0;
109
110 std::optional<vma::raii::Buffer> m_blas_buffer;
111 vk::raii::AccelerationStructureKHR m_blas = nullptr;
112 vk::DeviceAddress m_blas_address = 0;
113 vk::AccelerationStructureBuildGeometryInfoKHR m_blas_build_info {};
114 vk::AccelerationStructureGeometryKHR m_blas_geometry {};
115 uint32_t m_blas_primitive_count = 0;
117 mutable vk::DeviceAddress m_blas_scratch_address = 0;
118
120 PFN_vkCmdBuildAccelerationStructuresKHR m_build_acceleration_structures = nullptr;
121
122 friend class MeshUpload;
123};
124
125class MeshUpload : public PendingResourceUpload {
126public:
127 MeshUpload(VulkanMesh& mesh, VulkanMesh::UploadData data, assets::HandleBase source, std::string_view debug_name = {});
128
129 VulkanMesh* mesh;
130
132 assets::HandleBase source;
133 std::string debug_name;
134
135 vma::raii::Buffer vertex_staging = nullptr;
136
137 void build(const VulkanCore& core) override;
138
139 void record(vk::CommandBuffer cmd) override;
140
141 void recordAcquire(sync::BarrierBatch& batch) override;
142
143 auto resource() -> IVulkanResource* override { return mesh; }
144
145private:
147 [[nodiscard]]
148 auto uploadedBuffers() const -> std::array<std::pair<vk::Buffer, vk::DeviceSize>, 3>;
149
150 bool m_released = false;
151};
152
153}
Base class for type-erased asset handles.
Definition core_types.hpp:72
Definition vulkan_resource_base.hpp:12
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_mesh.cpp:368
Definition vulkan_resource_base.hpp:48
Definition vulkan_core.hpp:48
Definition vulkan_mesh.hpp:26
Skips a barrier only when neither side writes and the layout and next read stages are already covered...
Definition gpu_sync.hpp:105
Base types for the asset system.
Definition vulkan_mesh.hpp:30