project-aster/aster/buffer.h

89 lines
2.2 KiB
C++

// =============================================
// Aster: buffer.h
// Copyright (c) 2020-2024 Anish Bhobe
// =============================================
#pragma once
#include "global.h"
struct Device;
struct Buffer
{
vk::Buffer m_Buffer = nullptr;
VmaAllocation m_Allocation = nullptr;
void *m_Mapped = nullptr;
[[nodiscard]] usize GetSize() const;
[[nodiscard]] bool IsHostVisible() const;
[[nodiscard]] bool IsValid() const;
[[nodiscard]] bool IsMapped() const;
void Destroy(const Device *device);
void Write(const Device *device, usize offset, usize size, const void *data);
protected:
void Allocate(const Device *device, usize size, vk::BufferUsageFlags bufferUsage,
VmaAllocationCreateFlags allocationFlags, VmaMemoryUsage memoryUsage, cstr name);
// Buffer size is used intrusively by the Render Resource Manager
// If the buffer is Invalid, the remaining data in Buffer is used for other tasks.
usize m_Size = 0;
constexpr static usize VALID_BUFFER_BIT = Cast<usize>(1llu << 63);
constexpr static usize HOST_ACCESSIBLE_BIT = 1llu << 62;
constexpr static usize SIZE_MASK = ~(VALID_BUFFER_BIT | HOST_ACCESSIBLE_BIT);
};
struct UniformBuffer : Buffer
{
void Init(const Device *device, usize size, cstr name = nullptr);
};
struct StorageBuffer : Buffer
{
void Init(const Device *device, usize size, bool hostVisible, cstr name = nullptr);
};
struct VertexBuffer : Buffer
{
void Init(const Device *device, usize size, cstr name = nullptr);
void Write(const Device *device, void *data, usize size, usize offset) const = delete;
};
struct IndexBuffer : Buffer
{
void Init(const Device *device, usize size, cstr name = nullptr);
void Write(const Device *device, void *data, usize size, usize offset) const = delete;
};
struct StagingBuffer : Buffer
{
void Init(const Device *device, usize size, cstr name = nullptr);
};
inline usize
Buffer::GetSize() const
{
return m_Size & SIZE_MASK;
}
inline bool
Buffer::IsHostVisible() const
{
return m_Size & HOST_ACCESSIBLE_BIT;
}
inline bool
Buffer::IsValid() const
{
return m_Size & VALID_BUFFER_BIT;
}
inline bool
Buffer::IsMapped() const
{
return m_Mapped;
}