nngn
Loading...
Searching...
No Matches
resource.h
Go to the documentation of this file.
1#ifndef NNGN_GRAPHICS_VULKAN_RESOURCE_H
2#define NNGN_GRAPHICS_VULKAN_RESOURCE_H
3
4#include <cassert>
5#include <cstddef>
6#include <cstdint>
7#include <span>
8
9#include "utils/log.h"
10
11#include "memory.h"
12#include "vulkan.h"
13
14namespace nngn {
15
16class DeviceMemory;
17
18namespace detail {
19
21class Buffer {
22public:
23 VkBuffer id() const { return this->h; }
24 VkDeviceSize size() const { return this->m_size; }
25 VkDeviceSize capacity() const { return this->m_capacity; }
26 void set_size(VkDeviceSize s)
28protected:
29 void set_capacity(VkDeviceSize c) { this->m_capacity = c; }
30 bool init(VkDevice dev, VkDeviceSize size, VkBufferUsageFlags usage);
31 bool alloc();
32 void destroy();
33 void destroy(VkDevice dev);
34private:
35 VkBuffer h = {};
36 VkDeviceSize m_size = {}, m_capacity = {};
37};
38
39}
40
42public:
43 VkDeviceMemory mem() const { return this->hm; }
44 template<VkMemoryPropertyFlags f>
45 bool init(
46 VkDevice dev, DeviceMemory *dev_mem,
47 VkDeviceSize size, VkBufferUsageFlags usage);
48 void destroy(VkDevice dev, DeviceMemory *dev_mem);
49 void memcpy(
50 VkDevice dev, VkDeviceSize off, std::span<const std::byte> s) const;
51 void fill(
52 VkDevice dev, VkDeviceSize off, VkDeviceSize n, VkDeviceSize stride,
53 std::span<const std::byte> s) const;
54private:
55 VkDeviceMemory hm = {};
56};
57
58class Buffer : public detail::Buffer {
59public:
60 template<VkMemoryPropertyFlags f>
61 bool init(
62 VkDevice dev, DeviceMemory *dev_mem,
63 VkDeviceSize size, VkBufferUsageFlags usage);
64 void destroy(VkDevice dev, DeviceMemory *dev_mem);
65private:
67};
68
69struct Image {
70public:
71 VkImage id() const { return this->h; }
72 VkDeviceMemory mem() const { return this->hm; }
73 void destroy(VkDevice dev, DeviceMemory *dev_mem);
74 template<VkMemoryPropertyFlags f>
75 bool init(
76 VkDevice dev, DeviceMemory *dev_mem,
77 VkImageCreateFlags flags, VkImageType type, VkFormat format,
78 VkExtent3D extent, std::uint32_t mip_levels, std::uint32_t n_layers,
79 VkSampleCountFlagBits n_samples, VkImageTiling tiling,
80 VkImageUsageFlags usage);
81 bool init_mipmaps(
83 VkExtent3D extent, std::uint32_t mip_levels, std::uint32_t base_layer,
84 std::uint32_t n_layers) const;
85 bool create_view(
86 VkDevice dev, VkImageViewType type, VkFormat format,
87 VkImageAspectFlags aspect_flags,
88 std::uint32_t mip_levels, std::uint32_t base_layer,
89 std::uint32_t n_layers, VkImageView *p) const;
92 VkPipelineStageFlags src_stage, VkPipelineStageFlags dst_stage,
93 VkAccessFlags src_mask, VkAccessFlags dst_mask,
94 VkImageLayout src, VkImageLayout dst,
95 const VkImageSubresourceRange &range) const;
96private:
97 bool init(
98 VkDevice dev,
99 VkImageCreateFlags flags, VkImageType type, VkFormat format,
100 VkExtent3D extent, std::uint32_t mip_levels, std::uint32_t n_layers,
101 VkSampleCountFlagBits n_samples, VkImageTiling tiling,
102 VkImageUsageFlags usage);
103 VkImage h = {};
104 VkDeviceMemory hm = {};
105};
106
107template<VkMemoryPropertyFlags f>
109 VkDevice dev, DeviceMemory *dev_mem,
110 VkDeviceSize size, VkBufferUsageFlags usage
111) {
113 this->destroy(dev, dev_mem);
114 if(!detail::Buffer::init(dev, size, usage))
115 return false;
116 if(!dev_mem->alloc<f>(this->id(), &this->hm))
117 return false;
118 vkBindBufferMemory(dev, this->id(), this->hm, 0);
119 this->set_capacity(size);
120 return true;
121}
122
123template<VkMemoryPropertyFlags f>
125 VkDevice dev, DeviceMemory *dev_mem,
126 VkDeviceSize size, VkBufferUsageFlags usage
127) {
129 this->destroy(dev, dev_mem);
130 if(!detail::Buffer::init(dev, size, usage))
131 return false;
132 VkDeviceMemory mem = {};
133 VkDeviceSize off = {};
134 if(!dev_mem->alloc<f>(this->id(), &this->ha, &mem, &off))
135 return false;
136 vkBindBufferMemory(dev, this->id(), mem, off);
137 this->set_capacity(size);
138 return true;
139}
140
141template<VkMemoryPropertyFlags f>
143 VkDevice dev, DeviceMemory *dev_mem,
144 VkImageCreateFlags flags, VkImageType type, VkFormat format,
145 VkExtent3D extent, std::uint32_t mip_levels, std::uint32_t n_layers,
146 VkSampleCountFlagBits n_samples, VkImageTiling tiling,
147 VkImageUsageFlags usage
148) {
150 return this->init(
151 dev, flags, type, format, extent, mip_levels, n_layers, n_samples,
152 tiling, usage)
153 && dev_mem->alloc<f>(this->h, &this->hm)
154 && (vkBindImageMemory(dev, this->h, this->hm, 0), true);
155}
156
157}
158
159#endif
Definition resource.h:58
MemoryAllocation ha
Definition resource.h:66
bool init(VkDevice dev, DeviceMemory *dev_mem, VkDeviceSize size, VkBufferUsageFlags usage)
Definition resource.h:124
Definition resource.h:41
VkDeviceMemory hm
Definition resource.h:55
VkDeviceMemory mem() const
Definition resource.h:43
void memcpy(VkDevice dev, VkDeviceSize off, std::span< const std::byte > s) const
Definition resource.cpp:51
bool init(VkDevice dev, DeviceMemory *dev_mem, VkDeviceSize size, VkBufferUsageFlags usage)
Definition resource.h:108
void fill(VkDevice dev, VkDeviceSize off, VkDeviceSize n, VkDeviceSize stride, std::span< const std::byte > s) const
Definition resource.cpp:37
Manages device memory queries, allocations, and lifetime.
Definition memory.h:20
bool alloc(const VkMemoryRequirements *req, VkDeviceMemory *p)
Allocates device memory from a type that fulfills req.
Definition memory.h:123
A buffer and its associated memory allocation.
Definition resource.h:21
VkDeviceSize m_capacity
Definition resource.h:36
VkBuffer id() const
Definition resource.h:23
void set_size(VkDeviceSize s)
Definition resource.h:26
bool init(VkDevice dev, VkDeviceSize size, VkBufferUsageFlags usage)
Definition resource.cpp:15
VkDeviceSize m_size
Definition resource.h:36
void set_capacity(VkDeviceSize c)
Definition resource.h:29
VkDeviceSize size() const
Definition resource.h:24
VkDeviceSize capacity() const
Definition resource.h:25
void destroy()
Definition resource.cpp:27
VkBuffer h
Definition resource.h:35
assert
Definition debug.lua:3
local n
Definition dump_lights.lua:5
local c
Definition gamma.lua:11
init
Definition img_common.lua:34
#define NNGN_LOG_CONTEXT_CF(c)
Definition log.h:11
Definition audio.cpp:7
std::uint64_t MemoryAllocation
Definition memory.h:17
VkCommandBuffer
Definition types.h:72
COMMAND_BUFFER DESCRIPTOR_POOL DESCRIPTOR_SET_LAYOUT FENCE VkImage
Definition types.h:72
local function f()) end
Definition resource.h:69
VkDeviceMemory mem() const
Definition resource.h:72
bool init(VkDevice dev, DeviceMemory *dev_mem, VkImageCreateFlags flags, VkImageType type, VkFormat format, VkExtent3D extent, std::uint32_t mip_levels, std::uint32_t n_layers, VkSampleCountFlagBits n_samples, VkImageTiling tiling, VkImageUsageFlags usage)
Definition resource.h:142
void transition_layout(VkCommandBuffer cmd, VkPipelineStageFlags src_stage, VkPipelineStageFlags dst_stage, VkAccessFlags src_mask, VkAccessFlags dst_mask, VkImageLayout src, VkImageLayout dst, const VkImageSubresourceRange &range) const
Definition resource.cpp:219
VkDeviceMemory hm
Definition resource.h:104
bool init_mipmaps(VkCommandBuffer cmd, VkExtent3D extent, std::uint32_t mip_levels, std::uint32_t base_layer, std::uint32_t n_layers) const
Definition resource.cpp:110
void destroy(VkDevice dev, DeviceMemory *dev_mem)
Definition resource.cpp:76
VkImage h
Definition resource.h:103
bool create_view(VkDevice dev, VkImageViewType type, VkFormat format, VkImageAspectFlags aspect_flags, std::uint32_t mip_levels, std::uint32_t base_layer, std::uint32_t n_layers, VkImageView *p) const
Definition resource.cpp:196
VkImage id() const
Definition resource.h:71
std::chrono::seconds s
Definition timing.cpp:6