MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ allocate_field()

bool MayaFlux::Buffers::VolumeGridBuffer::allocate_field ( const std::string &  name,
size_t  stride_bytes,
bool  double_buffered 
)
private

Allocate one field's slots and register it.

Parameters
nameLookup key.
stride_bytesBytes per cell.
double_bufferedWhether to allocate a second slot.
Returns
True if the field was registered.

Definition at line 59 of file VolumeGridBuffer.cpp.

61{
62 if (name.empty()) {
64 "VolumeGridBuffer: field declared with empty name, skipped");
65 return false;
66 }
67
68 if (stride_bytes == 0) {
70 "VolumeGridBuffer: field '{}' declares zero stride, skipped", name);
71 return false;
72 }
73
74 if (m_fields.contains(name)) {
76 "VolumeGridBuffer: duplicate field '{}', later declaration discarded", name);
77 return false;
78 }
79
80 auto buffer_service = Registry::BackendRegistry::instance()
81 .get_service<Registry::Service::BufferService>();
82
83 if (!buffer_service || !buffer_service->allocate_raw_buffer) {
84 error<std::runtime_error>(
87 std::source_location::current(),
88 "VolumeGridBuffer requires a valid buffer service");
89 }
90
91 const auto usage_flags = static_cast<uint32_t>(
92 static_cast<VkBufferUsageFlags>(
93 vk::BufferUsageFlagBits::eStorageBuffer
94 | vk::BufferUsageFlagBits::eTransferSrc
95 | vk::BufferUsageFlagBits::eTransferDst));
96
97 const auto memory_flags = static_cast<uint32_t>(
98 static_cast<VkMemoryPropertyFlags>(vk::MemoryPropertyFlagBits::eDeviceLocal));
99
100 auto& resources = get_buffer_resources();
101 const size_t field_bytes = static_cast<size_t>(get_cell_count()) * stride_bytes;
102 const uint32_t slot_count = double_buffered ? 2 : 1;
103
104 Field field {
105 .stride_bytes = stride_bytes,
106 .slot_a = static_cast<uint32_t>(resources.back_buffers.size()),
107 .slot_b = 0,
108 .read_is_a = true,
109 };
110
111 for (uint32_t i = 0; i < slot_count; ++i) {
112 void* out_buffer = nullptr;
113 void* out_memory = nullptr;
114 void* out_mapped = nullptr;
115
116 buffer_service->allocate_raw_buffer(
117 field_bytes, usage_flags, memory_flags, false,
118 out_buffer, out_memory, out_mapped);
119
120 VKBufferResources::GenerationSlot slot;
121 slot.buffer = static_cast<vk::Buffer>(static_cast<VkBuffer>(out_buffer));
122 slot.memory = static_cast<vk::DeviceMemory>(static_cast<VkDeviceMemory>(out_memory));
123 slot.mapped_ptr = out_mapped;
124
125 resources.back_buffers.push_back(slot);
126 }
127
128 field.slot_b = double_buffered ? field.slot_a + 1 : field.slot_a;
129
130 m_fields.emplace(name, field);
131 m_field_order.push_back(name);
132
134 "VolumeGridBuffer: field '{}', stride {}, {} slot(s), {} bytes",
135 name, stride_bytes, slot_count, field_bytes * slot_count);
136
137 return true;
138}
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
const VKBufferResources & get_buffer_resources() const
Get all buffer resources at once (read-only)
Definition VKBuffer.hpp:341
std::unordered_map< std::string, Field > m_fields
uint32_t get_cell_count() const
Total cell count.
std::vector< std::string > m_field_order
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
@ Init
Engine/subsystem initialization.
@ Buffers
Buffers, Managers, processors and processing chains.

References MayaFlux::Buffers::VKBufferResources::GenerationSlot::buffer, MayaFlux::Journal::Buffers, MayaFlux::Buffers::VKBuffer::get_buffer_resources(), get_cell_count(), MayaFlux::Registry::BackendRegistry::get_service(), MayaFlux::Journal::Init, MayaFlux::Registry::BackendRegistry::instance(), m_field_order, m_fields, MayaFlux::Buffers::VKBufferResources::GenerationSlot::mapped_ptr, MayaFlux::Buffers::VKBufferResources::GenerationSlot::memory, MF_DEBUG, MF_ERROR, and MayaFlux::Buffers::VolumeGridBuffer::Field::stride_bytes.

Referenced by allocate_fields(), declare_scalar(), declare_scratch(), and declare_vector().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: