Compare commits

..

4 Commits

Author SHA1 Message Date
Anish Bhobe 703624eb86 Reworked buffer types. 2025-04-08 23:33:07 +02:00
Anish Bhobe 1748a48272 Image, View and Sampler are all updated. 2025-04-07 00:21:50 +02:00
Anish Bhobe d8770c1e06 [WIP] Updated Buffers.
TODO: Update Image and Views.
2025-04-06 21:02:58 +02:00
Anish Bhobe 1bee73e46f [WIP] Move to shared_ptr. 2025-04-06 19:31:12 +02:00
29 changed files with 558 additions and 779 deletions

View File

@ -9,104 +9,109 @@
struct Device;
// TODO Refactor the Buffer Hierarchy
/// A Vulkan buffer wrapper.
struct Buffer
{
const Device *m_Device = nullptr; ///< Will be used for book-keeping when buffer is invalid.
enum class FlagBits : u8
{
eNone = 0x0,
eStaging = 0x1,
eUniform = 0x2,
eStorage = 0x4,
eIndex = 0x8,
eVertex = 0x10,
eIndirect = 0x20,
};
using Flags = vk::Flags<FlagBits>;
constexpr static Flags FLAGS = {};
const Device *m_Device = nullptr;
vk::Buffer m_Buffer = nullptr;
VmaAllocation m_Allocation = nullptr;
u8 *m_Mapped = nullptr; ///< If the buffer is host visible, it should be (and stay) mapped.
usize m_Size = 0;
std::atomic<u32> m_RefCount = 0;
[[nodiscard]] bool
IsHostVisible() const
{
return m_Mapped;
}
Flags m_Flags = {};
/// @returns True if it is a valid vulkan buffer.
[[nodiscard]] bool
IsValid() const
{
return m_Buffer;
}
/// If the buffer is host visible, it should be (and stay) mapped.
/// @returns True if the buffer is host-visible and mapped.
[[nodiscard]] bool
IsMapped() const
{
return m_Mapped;
}
void
AddRef()
{
assert(++m_RefCount > 0);
}
void
Release()
{
const auto rc = --m_RefCount;
assert(rc < MaxValue<u32>);
if (rc == 0)
{
Destroy();
}
}
[[nodiscard]] bool
IsReferenced()
{
return m_RefCount;
}
void Destroy();
void Write(usize offset, usize size, const void *data);
void Allocate(const Device *device, usize size, vk::BufferUsageFlags bufferUsage,
VmaAllocationCreateFlags allocationFlags, VmaMemoryUsage memoryUsage, cstr name);
/// Writes the data to the buffer.
/// @note The buffer must be mapped.
void Write(usize offset, usize size, const void *data) const;
/// If Buffer Device Address is enabled,
/// Get a pointer.
uptr GetDeviceAddress(const Device *device) const;
// Constructors
Buffer(const Device *device, usize size, vk::BufferUsageFlags bufferUsage, VmaAllocationCreateFlags allocationFlags,
VmaMemoryUsage memoryUsage, cstr name);
Buffer(Buffer &&other) noexcept;
Buffer &operator=(Buffer &&other) noexcept;
~Buffer();
DISALLOW_COPY_AND_ASSIGN(Buffer);
};
struct UniformBuffer : Buffer
{
void Init(const Device *device, usize size, cstr name = nullptr);
constexpr static Flags FLAGS = FlagBits::eUniform;
};
struct StorageBuffer : Buffer
{
void Init(const Device *device, usize size, bool hostVisible, cstr name = nullptr);
void Init(const Device *device, usize size, bool hostVisible, bool deviceAddress, cstr name = nullptr);
constexpr static Flags FLAGS = FlagBits::eStorage;
};
struct IndirectBuffer : Buffer
{
void Init(const Device *device, usize size, bool hostVisible, cstr name = nullptr);
};
struct StorageIndexBuffer : StorageBuffer
{
void Init(const Device *device, usize size, bool hostVisible, bool deviceAddress, cstr name = nullptr);
constexpr static Flags FLAGS = FlagBits::eIndirect;
};
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;
constexpr static Flags FLAGS = FlagBits::eVertex;
};
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;
constexpr static Flags FLAGS = FlagBits::eIndex;
};
struct StagingBuffer : Buffer
{
void Init(const Device *device, usize size, cstr name = nullptr);
constexpr static Flags FLAGS = FlagBits::eStaging;
};
namespace concepts
{
template <typename T>
concept AnyBuffer = std::derived_from<T, Buffer>;
template <typename T, typename TInto>
concept BufferInto = std::derived_from<T, Buffer> and std::derived_from<TInto, Buffer> and
(Cast<bool>(T::FLAGS & TInto::FLAGS) or std::same_as<Buffer, TInto>);
template <typename T>
concept AnyBufferRef = Deref<T> and AnyBuffer<DerefType<T>>;
template <typename T, typename TTo>
concept BufferRefTo = Deref<T> and BufferInto<DerefType<T>, TTo>;
} // namespace concepts

View File

@ -221,5 +221,8 @@ struct fmt::formatter<eastl::fixed_string<TType, TCount, TOverflow>> : nested_fo
}
};
template <concepts::Manageable T>
using Ref = eastl::intrusive_ptr<T>;
template <typename T>
using Ref = std::shared_ptr<T>;
template <typename T>
using WeakRef = std::weak_ptr<T>;

View File

@ -36,186 +36,97 @@ ToOffset3D(const vk::Extent3D &extent)
struct Image
{
enum class FlagBits : u8
{
eSampled = 0x1,
eStorage = 0x2,
eCube = 0x4,
};
using Flags = vk::Flags<FlagBits>;
constexpr static Flags FLAGS = {};
const Device *m_Device = nullptr;
vk::Image m_Image = nullptr;
VmaAllocation m_Allocation = nullptr;
vk::Extent3D m_Extent;
vk::Format m_Format;
std::atomic<u32> m_RefCount;
u8 m_EmptyPadding_ = 0;
u8 m_Flags_ = 0;
Flags m_Flags_ = {};
u8 m_LayerCount = 0;
u8 m_MipLevels = 0;
constexpr static u8 SAMPLED_BIT = 1 << 7;
constexpr static u8 STORAGE_BIT = 1 << 6;
constexpr static u8 CUBE_BIT = 1 << 5;
[[nodiscard]] bool
IsSampled() const
{
return m_Flags_ & SAMPLED_BIT;
}
[[nodiscard]] bool
IsStorage() const
{
return m_Flags_ & STORAGE_BIT;
}
[[nodiscard]] bool
IsCube() const
{
return m_Flags_ & CUBE_BIT;
}
[[nodiscard]] bool
IsValid() const
{
return m_Image;
}
[[nodiscard]] bool
IsReferenced() const
{
return m_RefCount;
}
[[nodiscard]] u32
GetMipLevels() const
{
return m_MipLevels;
}
void
AddRef()
{
const auto rc = ++m_RefCount;
assert(rc > 0);
}
void
Release()
{
const auto rc = --m_RefCount;
assert(rc < MaxValue<u32>);
if (rc == 0)
{
Destroy();
}
}
void Destroy();
static bool
Conforms(const Image &)
{
return true;
}
void DestroyView(vk::ImageView imageView) const;
// Constructors.
explicit Image(const Device *device, vk::Image image, VmaAllocation allocation, vk::Extent3D extent,
vk::Format format, Flags flags, u8 layerCount, u8 mipLevels);
Image(Image &&other) noexcept;
Image &operator=(Image &&other) noexcept;
~Image();
DISALLOW_COPY_AND_ASSIGN(Image);
};
namespace concepts
{
template <typename T>
concept Image = std::derived_from<T, Image> and Manageable<T>;
template <typename T>
concept ImageRef = Derefencable<T> and Image<DereferencesTo<T>>;
template <typename T>
concept SampledImage = requires() {
{ T::SAMPLED } -> std::convertible_to<bool>;
} and T::SAMPLED and Image<T>;
template <typename T>
concept SampledImageRef = Derefencable<T> and SampledImage<DereferencesTo<T>>;
template <typename T>
concept ImageCube = requires() {
{ T::CUBE } -> std::convertible_to<bool>;
} and T::CUBE and Image<T>;
template <typename T>
concept ImageCubeRef = Derefencable<T> and ImageCube<DereferencesTo<T>>;
template <typename T>
concept StorageImage = requires() {
{ T::STORAGE } -> std::convertible_to<bool>;
} and T::STORAGE and Image<T>;
template <typename T>
concept StorageImageRef = Derefencable<T> and StorageImage<DereferencesTo<T>>;
} // namespace concepts
struct Texture : Image
{
constexpr static bool SAMPLED = true;
static bool
Conforms(const Image &other)
{
return other.IsSampled();
}
constexpr static Flags FLAGS = FlagBits::eSampled;
};
struct ImageCube : Image
{
constexpr static bool CUBE = true;
static bool
Conforms(const Image &other)
{
return other.IsCube();
}
constexpr static Flags FLAGS = FlagBits::eCube;
};
struct TextureCube : Image
{
constexpr static bool SAMPLED = true;
constexpr static bool CUBE = true;
static bool
Conforms(const Image &other)
{
return other.IsSampled() && other.IsCube();
}
constexpr static Flags FLAGS = Texture::FLAGS | ImageCube::FLAGS;
};
struct StorageImage : Image
{
constexpr static bool STORAGE = true;
static bool
Conforms(const Image &other)
{
return other.IsStorage();
}
constexpr static Flags FLAGS = FlagBits::eStorage;
};
struct StorageTexture : StorageImage
{
constexpr static bool SAMPLED = true;
constexpr static bool STORAGE = true;
static bool
Conforms(const Image &other)
{
return other.IsStorage() && other.IsSampled();
}
constexpr static Flags FLAGS = StorageImage::FLAGS | Texture::FLAGS;
};
struct StorageTextureCube : StorageImage
{
constexpr static bool SAMPLED = true;
constexpr static bool CUBE = true;
constexpr static bool STORAGE = true;
static bool
Conforms(const Image &other)
{
return other.IsStorage() && other.IsSampled() && other.IsCube();
}
constexpr static Flags FLAGS = StorageImage::FLAGS | Texture::FLAGS | ImageCube::FLAGS;
};
namespace concepts
{
template <typename T>
concept AnyImage = std::derived_from<T, Image>;
template <typename T, typename TInto>
concept ImageInto = std::derived_from<T, Image> and std::derived_from<TInto, Image> and
(Cast<bool>(T::FLAGS & TInto::FLAGS) or std::same_as<Image, TInto>);
template <typename T>
concept AnyImageRef = Deref<T> and AnyImage<DerefType<T>>;
template <typename T, typename TTo>
concept ImageRefTo = Deref<T> and ImageInto<DerefType<T>, TTo>;
} // namespace concepts

View File

@ -8,16 +8,14 @@
#include "global.h"
#include "image.h"
template <concepts::Image TImage>
template <concepts::AnyImage TImage>
struct View
{
using ImageType = TImage;
const Device *m_Device;
Ref<Image> m_Image;
vk::ImageView m_View = nullptr;
vk::Extent3D m_Extent;
std::atomic<u32> m_RefCount;
u8 m_BaseLayer = 0;
u8 m_LayerCount = 0;
u8 m_BaseMipLevel = 0;
@ -29,38 +27,54 @@ struct View
return m_Image->m_Image;
}
void
AddRef()
{
const auto rc = ++m_RefCount;
assert(rc > 0);
}
void
Release()
{
const auto rc = --m_RefCount;
assert(rc < MaxValue<u32>);
if (rc == 0)
{
Destroy();
}
}
[[nodiscard]] bool
IsReferenced() const
{
return m_RefCount;
}
[[nodiscard]] bool
IsValid() const
{
return m_Image;
return Cast<bool>(m_Image);
}
void
Destroy()
View(Ref<Image> image, const vk::ImageView view, const vk::Extent3D extent, const u8 baseLayer, const u8 layerCount,
const u8 baseMipLevel, const u8 mipLevelCount)
: m_Image{std::move(image)}
, m_View{view}
, m_Extent{extent}
, m_BaseLayer{baseLayer}
, m_LayerCount{layerCount}
, m_BaseMipLevel{baseMipLevel}
, m_MipLevelCount{mipLevelCount}
{
}
View(View &&other) noexcept
: m_Image{std::move(other.m_Image)}
, m_View{Take(other.m_View)}
, m_Extent{std::move(other.m_Extent)}
, m_BaseLayer{other.m_BaseLayer}
, m_LayerCount{other.m_LayerCount}
, m_BaseMipLevel{other.m_BaseMipLevel}
, m_MipLevelCount{other.m_MipLevelCount}
{
}
View &
operator=(View &&other) noexcept
{
if (this == &other)
return *this;
using std::swap;
swap(m_Image, other.m_Image);
swap(m_View, other.m_View);
swap(m_Extent, other.m_Extent);
swap(m_BaseLayer, other.m_BaseLayer);
swap(m_LayerCount, other.m_LayerCount);
swap(m_BaseMipLevel, other.m_BaseMipLevel);
swap(m_MipLevelCount, other.m_MipLevelCount);
return *this;
}
DISALLOW_COPY_AND_ASSIGN(View);
~View()
{
if (!IsValid())
return;
@ -69,61 +83,26 @@ struct View
}
};
struct ImageView : View<Image>
{
};
struct ImageCubeView : View<ImageCube>
{
};
struct TextureView : View<Texture>
{
};
struct TextureCubeView : View<TextureCube>
{
};
struct StorageImageView : View<StorageImage>
{
};
struct StorageTextureView : View<StorageTexture>
{
};
struct StorageTextureCubeView : View<StorageTextureCube>
{
};
using ImageView = View<Image>;
using ImageCubeView = View<ImageCube>;
using TextureView = View<Texture>;
using TextureCubeView = View<TextureCube>;
using StorageImageView = View<StorageImage>;
using StorageTextureView = View<StorageTexture>;
using StorageTextureCubeView = View<StorageTextureCube>;
namespace concepts
{
template <typename TView>
concept View = std::derived_from<TView, View<typename TView::ImageType>>;
template <typename T>
concept View = std::derived_from<T, View<typename T::ImageType>>;
template <typename T, typename TTo>
concept ViewTo = View<T> and ImageInto<typename T::ImageType, TTo>;
template <typename T>
concept ViewRef = Derefencable<T> and View<DereferencesTo<T>>;
concept ViewRef = Deref<T> and View<DerefType<T>>;
template <typename T>
concept ImageView = View<T> and Image<typename T::ImageType>;
template <typename T>
concept ImageViewRef = Derefencable<T> and ImageView<DereferencesTo<T>>;
template <typename T>
concept ImageCubeView = View<T> and ImageCube<typename T::ImageType>;
template <typename T>
concept SampledImageView = View<T> and SampledImage<typename T::ImageType>;
template <typename T>
concept SampledImageViewRef = Derefencable<T> and SampledImageView<DereferencesTo<T>>;
template <typename T>
concept StorageImageView = View<T> and StorageImage<typename T::ImageType>;
template <typename T>
concept StorageImageViewRef = Derefencable<T> and StorageImageView<DereferencesTo<T>>;
template <typename T, typename TTo>
concept ViewRefTo = ViewRef<T> and ImageInto<typename DerefType<T>::ImageType, TTo>;
} // namespace concepts

View File

@ -9,44 +9,24 @@
struct Device;
// TODO Refactor the Buffer Hierarchy
struct Sampler
struct Sampler final
{
const Device *m_Device = nullptr;
vk::Sampler m_Sampler = nullptr;
std::atomic<u32> m_RefCount = 0;
void Init(const Device *device, const vk::SamplerCreateInfo &samplerCreateInfo, cstr name);
void Destroy();
void
AddRef()
{
const auto rc = ++m_RefCount;
assert(rc > 0);
}
void
Release()
{
const auto rc = --m_RefCount;
assert(rc < MaxValue<u32>);
if (rc == 0)
{
Destroy();
}
}
[[nodiscard]] bool
IsReferenced() const
{
return m_RefCount;
}
[[nodiscard]] bool
IsValid() const
{
return m_Sampler;
}
// Constructors
Sampler(const Device *device, const vk::SamplerCreateInfo &samplerCreateInfo, cstr name);
~Sampler();
Sampler(Sampler &&other) noexcept;
Sampler &operator=(Sampler &&other) noexcept;
DISALLOW_COPY_AND_ASSIGN(Sampler);
};

View File

@ -13,33 +13,16 @@ struct Image;
namespace concepts
{
template <typename T>
concept RefCounted = requires(T a) {
{ a.AddRef() } -> std::same_as<void>;
{ a.Release() } -> std::same_as<void>;
{ a.IsReferenced() } -> std::convertible_to<bool>;
};
template <typename T>
concept Derefencable = requires(T a) {
concept Deref = requires(T a) {
{ *a };
};
template <Derefencable T>
using DereferencesTo = std::remove_cvref_t<decltype(*std::declval<T>())>;
template <typename T>
concept DeviceDestructible = requires(T a, Device *p) {
{ a.Destroy(p) } -> std::same_as<void>;
template <typename TRef, typename TVal>
concept DerefTo = requires(TRef a) {
{ *a } -> std::convertible_to<TVal>;
};
template <typename T>
concept SelfDestructible = requires(T a) {
{ a.Destroy() } -> std::same_as<void>;
{ T::m_Device } -> std::convertible_to<const Device *>;
};
template <typename T>
concept Manageable =
std::is_default_constructible_v<T> and (DeviceDestructible<T> or SelfDestructible<T>) and RefCounted<T>;
template <Deref T>
using DerefType = std::remove_cvref_t<decltype(*std::declval<T>())>;
} // namespace concepts

View File

@ -4,7 +4,6 @@ cmake_minimum_required(VERSION 3.13)
target_sources(aster_core
INTERFACE
"manager.h"
"buffer_manager.h"
"image_manager.h"
"view_manager.h"

View File

@ -5,20 +5,30 @@
#pragma once
#include "manager.h"
#include "aster/aster.h"
#include "aster/core/buffer.h"
namespace systems
{
class BufferManager final : public Manager<Buffer>
{
public:
BufferManager(const Device *device, u32 maxCount);
[[nodiscard]] Handle CreateStorageBuffer(usize size, cstr name = nullptr);
[[nodiscard]] Handle CreateUniformBuffer(usize size, cstr name = nullptr);
[[nodiscard]] Handle CreateStagingBuffer(usize size, cstr name = nullptr);
template <std::derived_from<Buffer> TTo, std::derived_from<Buffer> TFrom>
static Ref<TTo>
CastBuffer(const Ref<TFrom> &from)
{
if constexpr (not concepts::BufferInto<TFrom, TTo>)
assert(TTo::FLAGS & from->m_Flags);
return std::reinterpret_pointer_cast<TTo>(from);
}
class BufferManager final
{
const Device *m_Device = nullptr;
public:
explicit BufferManager(const Device *device);
[[nodiscard]] Ref<StorageBuffer> CreateStorageBuffer(usize size, cstr name = nullptr) const;
[[nodiscard]] Ref<UniformBuffer> CreateUniformBuffer(usize size, cstr name = nullptr) const;
[[nodiscard]] Ref<StagingBuffer> CreateStagingBuffer(usize size, cstr name = nullptr) const;
};
} // namespace systems

View File

@ -6,6 +6,7 @@
#pragma once
#include "aster/aster.h"
#include "buffer_manager.h"
#include "sampler_manager.h"
#include "view_manager.h"
@ -22,12 +23,11 @@ namespace systems
class CommitManager
{
template <concepts::Manageable T>
template <typename T>
struct HandleMapper
{
using Type = T;
using Manager = Manager<Type>;
using Handle = typename Manager::Handle;
using Handle = Ref<Type>;
using Resource = ResId<Type>;
struct Entry : eastl::intrusive_hash_node_key<Handle>
@ -82,8 +82,6 @@ class CommitManager
};
};
static_assert(sizeof(Entry) == 24);
eastl::vector<Entry> m_Data;
FreeList<Entry> m_FreeList;
eastl::intrusive_hash_map<typename Entry::key_type, Entry, 31, typename Entry::Hash> m_InUse;
@ -226,7 +224,7 @@ class CommitManager
// Commit Storage Images
ResId<StorageImageView>
CommitStorageImage(const concepts::StorageImageViewRef auto &image)
CommitStorageImage(const concepts::ViewRefTo<StorageImage> auto &image)
{
return CommitStorageImage(CastView<StorageImageView>(image));
}
@ -235,19 +233,17 @@ class CommitManager
// Sampled Images
ResId<TextureView>
CommitTexture(const concepts::SampledImageViewRef auto &image, const Ref<Sampler> &sampler)
CommitTexture(const concepts::ViewRefTo<Texture> auto &image, const Ref<Sampler> &sampler)
{
return CommitTexture(CastView<TextureView>(image), sampler);
}
ResId<TextureView>
CommitTexture(const concepts::SampledImageViewRef auto &image)
CommitTexture(const concepts::ViewRefTo<Texture> auto &image)
{
return CommitTexture(CastView<TextureView>(image));
}
static_assert(concepts::SampledImageViewRef<Ref<TextureView>>);
ResId<TextureView> CommitTexture(const Ref<TextureView> &handle);
ResId<TextureView> CommitTexture(const Ref<TextureView> &image, const Ref<Sampler> &sampler);
@ -351,7 +347,7 @@ class CommitManager
}
};
template <concepts::Manageable T>
template <typename T>
void
ResId<T>::AddRef() const
{
@ -359,7 +355,7 @@ ResId<T>::AddRef() const
CommitManager::Instance().AddRef(*this);
}
template <concepts::Manageable T>
template <typename T>
void
ResId<T>::Release() const
{

View File

@ -5,20 +5,19 @@
#pragma once
#include "manager.h"
#include "aster/aster.h"
#include "aster/core/image.h"
namespace systems
{
template <std::derived_from<Image> TTo>
template <std::derived_from<Image> TTo, std::derived_from<Image> TFrom>
static Ref<TTo>
CastImage(const concepts::ImageRef auto &from)
CastImage(const Ref<TFrom> &from)
{
assert(TTo::Conforms(*from.get()));
return Recast<TTo *>(from.get());
if constexpr (not concepts::ImageInto<TFrom, TTo>)
assert(TTo::FLAGS & from->m_Flags_);
return std::reinterpret_pointer_cast<TTo>(from);
}
struct Texture2DCreateInfo
@ -54,12 +53,14 @@ struct DepthStencilImageCreateInfo
cstr m_Name = nullptr;
};
class ImageManager final : public Manager<Image>
class ImageManager final
{
public:
ImageManager(const Device *device, u32 maxCount);
const Device *m_Device;
template <concepts::Image T>
public:
explicit ImageManager(const Device *device);
template <concepts::ImageInto<Texture> T>
[[nodiscard]] Ref<T>
CreateTexture2D(const Texture2DCreateInfo &createInfo)
{
@ -67,7 +68,7 @@ class ImageManager final : public Manager<Image>
return CastImage<T>(handle);
}
template <concepts::Image T>
template <concepts::ImageInto<TextureCube> T>
[[nodiscard]] Ref<T>
CreateTextureCube(const TextureCubeCreateInfo &createInfo)
{

View File

@ -1,111 +0,0 @@
// =============================================
// Aster: manager.h
// Copyright (c) 2020-2025 Anish Bhobe
// =============================================
#pragma once
#include "aster/aster.h"
#include "aster/core/type_traits.h"
#include "aster/util/freelist.h"
#include <EASTL/intrusive_ptr.h>
#include <EASTL/vector.h>
struct Device;
namespace systems
{
template <concepts::Manageable T>
class Manager
{
public:
using Type = T;
using Handle = Ref<T>;
/**
* Constructor for the Manager class template.
* @param device Device with which resources are created.
* @param maxCount Max number of resources that can be created (maxCount <= Handle::INDEX_MASK)
*/
explicit Manager(const Device *device, const u32 maxCount)
: m_Data{maxCount}
, m_Device{device}
{
for (auto &element : m_Data)
{
m_FreeList.Push(element);
}
}
virtual ~Manager()
{
if constexpr (concepts::DeviceDestructible<Type>)
{
for (auto &element : m_Data)
{
element.Destroy(m_Device);
}
}
m_Device = nullptr;
}
// TODO: Work on deletion!!
void
Sweep()
requires concepts::DeviceDestructible<Type>
{
for (i64 i = m_Data.size() - 1; i >= 0; --i)
{
if (auto *pIter = &m_Data[i]; !pIter->IsReferenced())
{
pIter->Destroy(m_Device);
m_FreeList.Push(*pIter);
}
}
}
void
Sweep()
requires concepts::SelfDestructible<Type>
{
for (i64 i = m_Data.size() - 1; i >= 0; --i)
{
if (auto *pIter = &m_Data[i]; !pIter->IsValid())
{
pIter->Destroy();
m_FreeList.Push(*pIter);
}
}
}
PIN_MEMORY(Manager);
private:
eastl::vector<Type> m_Data; // Data also keeps the freelist during 'not use'.
FreeList<Type> m_FreeList;
protected:
const Device *m_Device;
/**
* Internal Method to Allocate a resource on the manager.
* @return [Handle, Type*] Where Type* is available to initialize the resource.
*/
[[nodiscard]] Handle
Alloc()
{
ERROR_IF(m_FreeList.Empty(), "Max buffers allocated.") THEN_ABORT(-1);
Type &pAlloc = m_FreeList.Pop();
memset(&pAlloc, 0, sizeof pAlloc);
if constexpr (concepts::SelfDestructible<Type>)
{
pAlloc.m_Device = m_Device;
}
return {&pAlloc};
}
};
} // namespace systems

View File

@ -14,7 +14,7 @@ namespace systems
* ResId manages the lifetime of the committed resource.
* @tparam T Type of the committed resource.
*/
template <concepts::Manageable T>
template <typename T>
class ResId
{
public:
@ -82,7 +82,7 @@ class ResId
struct NullId
{
template <concepts::Manageable T>
template <typename T>
operator ResId<T>()
{
return ResId<T>::Null();

View File

@ -28,7 +28,7 @@ class ResourceManager
{
}
template <concepts::ImageView T>
template <concepts::ViewTo<Image> T>
[[nodiscard]] Ref<T>
CreateTexture2D(const Texture2DCreateInfo &createInfo)
{
@ -36,7 +36,7 @@ class ResourceManager
return CastView<T>(handle);
}
template <concepts::ImageCubeView T>
template <concepts::ViewTo<ImageCube> T>
[[nodiscard]] Ref<T>
CreateTextureCube(const TextureCubeCreateInfo &createInfo)
{
@ -96,11 +96,11 @@ class ResourceManager
CombinedImageViewManager m_CombinedImageViews;
public:
ResourceManager(const Device *device, u32 maxBufferCount, u32 maxImageCount, u32 maxSamplerCount, u32 maxViewCount)
: m_Buffers{device, maxBufferCount}
, m_Images{device, maxImageCount}
, m_Samplers{device, maxSamplerCount}
, m_Views{device, maxViewCount}
explicit ResourceManager(const Device *device)
: m_Buffers{device}
, m_Images{device}
, m_Samplers{device}
, m_Views{device}
, m_CombinedImageViews{&m_Images, &m_Views}
{
}
@ -135,15 +135,6 @@ class ResourceManager
return m_CombinedImageViews;
}
void
Update()
{
m_Views.Sweep();
m_Images.Sweep();
m_Buffers.Sweep();
m_Samplers.Sweep();
}
~ResourceManager() = default;
PIN_MEMORY(ResourceManager);

View File

@ -6,7 +6,6 @@
#pragma once
#include "EASTL/hash_map.h"
#include "manager.h"
#include "aster/aster.h"
#include "aster/core/sampler.h"
@ -96,13 +95,17 @@ struct SamplerCreateInfo
*
* Manages (and caches) objects of sampler. Currently Samplers are never deleted.
*/
class SamplerManager final : public Manager<Sampler>
class SamplerManager final
{
eastl::hash_map<vk::SamplerCreateInfo, Handle> m_HashToSamplerIdx;
using Handle = Ref<Sampler>;
using WeakHandle = WeakRef<Sampler>;
eastl::hash_map<vk::SamplerCreateInfo, WeakHandle> m_HashToSamplerIdx;
const Device *m_Device;
public:
SamplerManager(const Device *device, u32 maxCount);
~SamplerManager() override;
explicit SamplerManager(const Device *device);
~SamplerManager();
Ref<Sampler> CreateSampler(const SamplerCreateInfo &createInfo);
};

View File

@ -5,8 +5,6 @@
#pragma once
#include "manager.h"
#include "aster/aster.h"
#include "aster/core/image_view.h"
@ -15,15 +13,16 @@
namespace systems
{
template <concepts::ImageView TTo>
template <concepts::View TTo, std::derived_from<Image> TFrom>
static Ref<TTo>
CastView(const concepts::ImageViewRef auto &from)
CastView(const Ref<View<TFrom>> &from)
{
assert(TTo::ImageType::Conforms(*from->m_Image.get()));
return Recast<TTo *>(from.get());
if constexpr (not concepts::ImageInto<TFrom, typename TTo::ImageType>)
assert(TTo::ImageType::FLAGS & from->m_Image->m_Flags_);
return std::reinterpret_pointer_cast<TTo>(from);
}
template <concepts::Image TImage = Image>
template <concepts::AnyImage TImage>
struct ViewCreateInfo
{
using ImageType = TImage;
@ -70,7 +69,7 @@ struct ViewCreateInfo
}
explicit
operator ViewCreateInfo<>() const
operator ViewCreateInfo<Image>() const
{
return {
.m_Image = CastImage<Image>(m_Image),
@ -86,19 +85,21 @@ struct ViewCreateInfo
}
};
class ViewManager final : public Manager<ImageView>
class ViewManager final
{
public:
ViewManager(const Device *device, u32 maxCount);
const Device *m_Device;
template <concepts::ImageView TImageView>
public:
explicit ViewManager(const Device *device);
template <concepts::View TImageView>
Ref<TImageView>
CreateView(const ViewCreateInfo<typename TImageView::ImageType> &createInfo)
{
return CastView<TImageView>(CreateView(ViewCreateInfo<>(createInfo)));
return CastView<TImageView>(CreateView(ViewCreateInfo<Image>(createInfo)));
}
Ref<ImageView> CreateView(const ViewCreateInfo<> &createInfo);
[[nodiscard]] Ref<ImageView> CreateView(const ViewCreateInfo<Image> &createInfo) const;
};
} // namespace systems

View File

@ -7,19 +7,8 @@
#include "core/device.h"
void
Buffer::Destroy()
{
if (!m_Buffer)
return;
vmaDestroyBuffer(m_Device->m_Allocator, Take(m_Buffer), m_Allocation);
m_Size = 0;
}
void
Buffer::Allocate(const Device *device, usize size, vk::BufferUsageFlags bufferUsage,
VmaAllocationCreateFlags allocationFlags, VmaMemoryUsage memoryUsage, cstr name)
Buffer::Buffer(const Device *device, const usize size, const vk::BufferUsageFlags bufferUsage,
const VmaAllocationCreateFlags allocationFlags, const VmaMemoryUsage memoryUsage, const cstr name)
{
assert(!m_Buffer);
@ -42,9 +31,9 @@ Buffer::Allocate(const Device *device, usize size, vk::BufferUsageFlags bufferUs
&allocationCreateInfo, &buffer, &allocation, &allocationInfo));
ERROR_IF(Failed(result), "Could not allocate buffer. Cause: {}", result) THEN_ABORT(result);
vk::MemoryPropertyFlags memoryPropertyFlags;
vmaGetAllocationMemoryProperties(device->m_Allocator, allocation,
Recast<VkMemoryPropertyFlags *>(&memoryPropertyFlags));
// vk::MemoryPropertyFlags memoryPropertyFlags;
// vmaGetAllocationMemoryProperties(device->m_Allocator, allocation, Recast<VkMemoryPropertyFlags
// *>(&memoryPropertyFlags));
// TODO: Actually track Host Access
// bool hostAccessible = Cast<bool>(memoryPropertyFlags & vk::MemoryPropertyFlagBits::eHostVisible);
@ -52,142 +41,69 @@ Buffer::Allocate(const Device *device, usize size, vk::BufferUsageFlags bufferUs
m_Size = size;
m_Allocation = allocation;
m_Mapped = Cast<u8 *>(allocationInfo.pMappedData);
m_Flags = {};
if (bufferUsage & vk::BufferUsageFlagBits::eTransferSrc)
m_Flags |= FlagBits::eStaging;
if (bufferUsage & vk::BufferUsageFlagBits::eIndexBuffer)
m_Flags |= FlagBits::eIndex;
if (bufferUsage & vk::BufferUsageFlagBits::eIndirectBuffer)
m_Flags |= FlagBits::eIndirect;
if (bufferUsage & vk::BufferUsageFlagBits::eVertexBuffer)
m_Flags |= FlagBits::eVertex;
if (bufferUsage & vk::BufferUsageFlagBits::eUniformBuffer)
m_Flags |= FlagBits::eUniform;
if (bufferUsage & vk::BufferUsageFlagBits::eStorageBuffer)
m_Flags |= FlagBits::eStorage;
device->SetName(m_Buffer, name);
}
Buffer::Buffer(Buffer &&other) noexcept
: m_Device{Take(other.m_Device)}
, m_Buffer{Take(other.m_Buffer)}
, m_Allocation{Take(other.m_Allocation)}
, m_Mapped{Take(other.m_Mapped)}
, m_Size{Take(other.m_Size)}
{
}
Buffer &
Buffer::operator=(Buffer &&other) noexcept
{
if (this == &other)
return *this;
using std::swap;
swap(m_Device, other.m_Device);
swap(m_Buffer, other.m_Buffer);
swap(m_Allocation, other.m_Allocation);
swap(m_Mapped, other.m_Mapped);
swap(m_Size, other.m_Size);
return *this;
}
Buffer::~Buffer()
{
if (!m_Buffer)
return;
vmaDestroyBuffer(m_Device->m_Allocator, Take(m_Buffer), m_Allocation);
m_Size = 0;
}
uptr
Buffer::GetDeviceAddress(const Device *device) const
{
vk::BufferDeviceAddressInfo addressInfo = {.buffer = m_Buffer};
const vk::BufferDeviceAddressInfo addressInfo = {.buffer = m_Buffer};
return device->m_Device.getBufferAddress(&addressInfo);
}
void
Buffer::Write(usize offset, usize size, const void *data)
Buffer::Write(const usize offset, const usize size, const void *data) const
{
assert(IsHostVisible());
if (!IsMapped())
{
void *mapped;
auto result = Cast<vk::Result>(vmaMapMemory(m_Device->m_Allocator, m_Allocation, &mapped));
ERROR_IF(Failed(result), "Memory mapping failed. Cause: {}", result);
if (!Failed(result))
{
m_Mapped = Cast<u8 *>(mapped);
assert(IsMapped());
memcpy(m_Mapped + offset, data, size);
vmaUnmapMemory(m_Device->m_Allocator, m_Allocation);
m_Mapped = nullptr;
}
}
else
{
memcpy(m_Mapped + offset, data, size);
}
// TODO: Debug this.
// auto result = Cast<vk::Result>(vmaCopyMemoryToAllocation(device->m_Allocator, &data, m_Allocation, 0, size));
// ERROR_IF(Failed(result), "Writing to buffer failed. Cause: {}", result) THEN_ABORT(result);
}
void
UniformBuffer::Init(const Device *device, const usize size, const cstr name)
{
Allocate(device, size, vk::BufferUsageFlagBits::eUniformBuffer,
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
VMA_MEMORY_USAGE_AUTO, name);
}
void
StorageBuffer::Init(const Device *device, usize size, bool hostVisible, cstr name)
{
Init(device, size, hostVisible, false, name);
}
void
StorageBuffer::Init(const Device *device, usize size, bool hostVisible, bool deviceAddress, cstr name)
{
vk::BufferUsageFlags usage = vk::BufferUsageFlagBits::eStorageBuffer;
if (deviceAddress)
{
usage |= vk::BufferUsageFlagBits::eShaderDeviceAddress;
}
if (hostVisible)
{
Allocate(device, size, usage,
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
VMA_MEMORY_USAGE_AUTO, name);
}
else
{
usage |= vk::BufferUsageFlagBits::eTransferDst;
Allocate(device, size, usage, 0, VMA_MEMORY_USAGE_AUTO, name);
}
}
void
StorageIndexBuffer::Init(const Device *device, usize size, bool hostVisible, bool deviceAddress, cstr name)
{
vk::BufferUsageFlags usage = vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eIndexBuffer;
if (deviceAddress)
{
usage |= vk::BufferUsageFlagBits::eShaderDeviceAddress;
}
if (hostVisible)
{
Allocate(device, size, usage,
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
VMA_MEMORY_USAGE_AUTO, name);
}
else
{
usage |= vk::BufferUsageFlagBits::eTransferDst;
Allocate(device, size, usage, 0, VMA_MEMORY_USAGE_AUTO, name);
}
}
void
IndirectBuffer::Init(const Device *device, usize size, bool hostVisible, cstr name)
{
vk::BufferUsageFlags usage = vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eIndirectBuffer |
vk::BufferUsageFlagBits::eShaderDeviceAddress;
if (hostVisible)
{
Allocate(device, size, usage,
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
VMA_MEMORY_USAGE_AUTO, name);
}
else
{
usage |= vk::BufferUsageFlagBits::eTransferDst;
Allocate(device, size, usage, 0, VMA_MEMORY_USAGE_AUTO, name);
}
}
void
VertexBuffer::Init(const Device *device, usize size, cstr name)
{
Allocate(device, size, vk::BufferUsageFlagBits::eVertexBuffer | vk::BufferUsageFlagBits::eTransferDst, 0,
VMA_MEMORY_USAGE_AUTO, name);
}
void
IndexBuffer::Init(const Device *device, usize size, cstr name)
{
Allocate(device, size, vk::BufferUsageFlagBits::eIndexBuffer | vk::BufferUsageFlagBits::eTransferDst, 0,
VMA_MEMORY_USAGE_AUTO, name);
}
void
StagingBuffer::Init(const Device *device, usize size, cstr name)
{
Allocate(device, size, vk::BufferUsageFlagBits::eTransferSrc,
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
VMA_MEMORY_USAGE_AUTO, name);
}

View File

@ -7,14 +7,31 @@
#include "core/device.h"
void
Image::Destroy()
Image &
Image::operator=(Image &&other) noexcept
{
if (this == &other)
return *this;
using std::swap;
swap(m_Device, other.m_Device);
swap(m_Image, other.m_Image);
swap(m_Allocation, other.m_Allocation);
swap(m_Extent, other.m_Extent);
swap(m_Format, other.m_Format);
swap(m_EmptyPadding_, other.m_EmptyPadding_);
swap(m_Flags_, other.m_Flags_);
swap(m_LayerCount, other.m_LayerCount);
swap(m_MipLevels, other.m_MipLevels);
return *this;
}
Image::~Image()
{
if (!IsValid())
return;
vmaDestroyImage(m_Device->m_Allocator, Take(m_Image), m_Allocation);
m_Flags_ = 0;
m_Flags_ = {};
}
void
@ -432,3 +449,28 @@ Image::DestroyView(const vk::ImageView imageView) const
//
// device->SetName(m_Image, name);
// }
Image::Image(Image &&other) noexcept
: m_Device{Take(other.m_Device)}
, m_Image{Take(other.m_Image)}
, m_Allocation{Take(other.m_Allocation)}
, m_Extent{other.m_Extent}
, m_Format{other.m_Format}
, m_EmptyPadding_{other.m_EmptyPadding_}
, m_Flags_{other.m_Flags_}
, m_LayerCount{other.m_LayerCount}
, m_MipLevels{other.m_MipLevels}
{
}
Image::Image(const Device *device, const vk::Image image, const VmaAllocation allocation, const vk::Extent3D extent,
const vk::Format format, const Flags flags, const u8 layerCount, const u8 mipLevels)
: m_Device{device}
, m_Image{image}
, m_Allocation{allocation}
, m_Extent{extent}
, m_Format{format}
, m_Flags_{flags}
, m_LayerCount{layerCount}
, m_MipLevels{mipLevels}
{
}

View File

@ -7,8 +7,7 @@
#include "core/device.h"
void
Sampler::Destroy()
Sampler::~Sampler()
{
if (!IsValid())
return;
@ -16,10 +15,25 @@ Sampler::Destroy()
m_Device->m_Device.destroy(Take(m_Sampler), nullptr);
}
void
Sampler::Init(const Device *device, const vk::SamplerCreateInfo &samplerCreateInfo, cstr name)
Sampler::Sampler(const Device *device, const vk::SamplerCreateInfo &samplerCreateInfo, cstr name)
{
m_Device = device;
const auto result = device->m_Device.createSampler(&samplerCreateInfo, nullptr, &m_Sampler);
ERROR_IF(Failed(result), "Could not create a sampler {}", name ? name : "<unnamed>") THEN_ABORT(-1);
}
Sampler &
Sampler::operator=(Sampler &&other) noexcept
{
if (this == &other)
return *this;
using std::swap;
swap(m_Device, other.m_Device);
swap(m_Sampler, other.m_Sampler);
return *this;
}
Sampler::Sampler(Sampler &&other) noexcept: m_Device{other.m_Device}
, m_Sampler{Take(other.m_Sampler)}
{
}

View File

@ -4,7 +4,6 @@ cmake_minimum_required(VERSION 3.13)
target_sources(aster_core
PRIVATE
"manager.cpp"
"buffer_manager.cpp"
"image_manager.cpp"
"view_manager.cpp"

View File

@ -7,11 +7,9 @@
using namespace systems;
Ref<Buffer>
BufferManager::CreateStorageBuffer(const usize size, const cstr name)
Ref<StorageBuffer>
BufferManager::CreateStorageBuffer(const usize size, const cstr name) const
{
auto object = Alloc();
// TODO: Storage and Index buffer are set.
// This is hacky and should be improved.
constexpr vk::BufferUsageFlags usage =
@ -21,41 +19,132 @@ BufferManager::CreateStorageBuffer(const usize size, const cstr name)
VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT |
VMA_ALLOCATION_CREATE_MAPPED_BIT;
constexpr VmaMemoryUsage memoryUsage = VMA_MEMORY_USAGE_AUTO;
object->Allocate(m_Device, size, usage, createFlags, memoryUsage, name);
return object;
return std::make_shared<StorageBuffer>(Buffer{m_Device, size, usage, createFlags, memoryUsage, name});
}
Ref<Buffer>
BufferManager::CreateUniformBuffer(const usize size, const cstr name)
Ref<UniformBuffer>
BufferManager::CreateUniformBuffer(const usize size, const cstr name) const
{
auto object = Alloc();
constexpr vk::BufferUsageFlags usage = vk::BufferUsageFlagBits::eUniformBuffer;
constexpr VmaAllocationCreateFlags createFlags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT |
VMA_ALLOCATION_CREATE_MAPPED_BIT;
constexpr VmaMemoryUsage memoryUsage = VMA_MEMORY_USAGE_AUTO;
object->Allocate(m_Device, size, usage, createFlags, memoryUsage, name);
return object;
return std::make_shared<UniformBuffer>(Buffer{m_Device, size, usage, createFlags, memoryUsage, name});
}
Manager<Buffer>::Handle
BufferManager::CreateStagingBuffer(const usize size, const cstr name)
Ref<StagingBuffer>
BufferManager::CreateStagingBuffer(const usize size, const cstr name) const
{
auto object = Alloc();
constexpr vk::BufferUsageFlags usage = vk::BufferUsageFlagBits::eTransferSrc;
constexpr VmaAllocationCreateFlags createFlags =
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;
constexpr VmaMemoryUsage memoryUsage = VMA_MEMORY_USAGE_AUTO;
object->Allocate(m_Device, size, usage, createFlags, memoryUsage, name);
return object;
return std::make_shared<StagingBuffer>(Buffer{m_Device, size, usage, createFlags, memoryUsage, name});
}
BufferManager::BufferManager(const Device *device, const u32 maxCount)
: Manager{device, maxCount}
//
// void
// UniformBuffer::Init(const Device *device, const usize size, const cstr name)
//{
// Allocate(device, size, vk::BufferUsageFlagBits::eUniformBuffer,
// VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
// VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
// VMA_MEMORY_USAGE_AUTO, name);
//}
//
// void
// StorageBuffer::Init(const Device *device, usize size, bool hostVisible, cstr name)
//{
// Init(device, size, hostVisible, false, name);
//}
//
// void
// StorageBuffer::Init(const Device *device, usize size, bool hostVisible, bool deviceAddress, cstr name)
//{
// vk::BufferUsageFlags usage = vk::BufferUsageFlagBits::eStorageBuffer;
// if (deviceAddress)
// {
// usage |= vk::BufferUsageFlagBits::eShaderDeviceAddress;
// }
// if (hostVisible)
// {
// Allocate(device, size, usage,
// VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
// VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
// VMA_MEMORY_USAGE_AUTO, name);
// }
// else
// {
// usage |= vk::BufferUsageFlagBits::eTransferDst;
// Allocate(device, size, usage, 0, VMA_MEMORY_USAGE_AUTO, name);
// }
//}
//
// void
// StorageIndexBuffer::Init(const Device *device, usize size, bool hostVisible, bool deviceAddress, cstr name)
//{
// vk::BufferUsageFlags usage = vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eIndexBuffer;
// if (deviceAddress)
// {
// usage |= vk::BufferUsageFlagBits::eShaderDeviceAddress;
// }
// if (hostVisible)
// {
// Allocate(device, size, usage,
// VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
// VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
// VMA_MEMORY_USAGE_AUTO, name);
// }
// else
// {
// usage |= vk::BufferUsageFlagBits::eTransferDst;
// Allocate(device, size, usage, 0, VMA_MEMORY_USAGE_AUTO, name);
// }
//}
//
// void
// IndirectBuffer::Init(const Device *device, usize size, bool hostVisible, cstr name)
//{
// vk::BufferUsageFlags usage = vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eIndirectBuffer |
// vk::BufferUsageFlagBits::eShaderDeviceAddress;
// if (hostVisible)
// {
// Allocate(device, size, usage,
// VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
// VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
// VMA_MEMORY_USAGE_AUTO, name);
// }
// else
// {
// usage |= vk::BufferUsageFlagBits::eTransferDst;
// Allocate(device, size, usage, 0, VMA_MEMORY_USAGE_AUTO, name);
// }
//}
//
// void
// VertexBuffer::Init(const Device *device, usize size, cstr name)
//{
// Allocate(device, size, vk::BufferUsageFlagBits::eVertexBuffer | vk::BufferUsageFlagBits::eTransferDst, 0,
// VMA_MEMORY_USAGE_AUTO, name);
//}
//
// void
// IndexBuffer::Init(const Device *device, usize size, cstr name)
//{
// Allocate(device, size, vk::BufferUsageFlagBits::eIndexBuffer | vk::BufferUsageFlagBits::eTransferDst, 0,
// VMA_MEMORY_USAGE_AUTO, name);
//}
//
// void
// StagingBuffer::Init(const Device *device, usize size, cstr name)
//{
// Allocate(device, size, vk::BufferUsageFlagBits::eTransferSrc,
// VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
// VMA_MEMORY_USAGE_AUTO, name);
//}
BufferManager::BufferManager(const Device *device)
: m_Device{device}
{
}

View File

@ -25,6 +25,11 @@ constexpr vk::ImageUsageFlags COLOR_ATTACHMENT =
constexpr vk::ImageUsageFlags DEPTH_STENCIL_ATTACHMENT = vk::ImageUsageFlagBits::eDepthStencilAttachment;
} // namespace usage_flags
ImageManager::ImageManager(const Device *device)
: m_Device{device}
{
}
Ref<Image>
ImageManager::CreateTexture2D(const Texture2DCreateInfo &createInfo)
{
@ -33,29 +38,27 @@ ImageManager::CreateTexture2D(const Texture2DCreateInfo &createInfo)
.usage = VMA_MEMORY_USAGE_AUTO,
};
VkImage image;
VkImage rawImage;
VmaAllocation allocation;
vk::ImageCreateInfo imageCreateInfo = ToImageCreateInfo(createInfo);
auto result = Cast<vk::Result>(vmaCreateImage(m_Device->m_Allocator, Recast<VkImageCreateInfo *>(&imageCreateInfo),
&allocationCreateInfo, &image, &allocation, nullptr));
&allocationCreateInfo, &rawImage, &allocation, nullptr));
ERROR_IF(Failed(result), "Could not allocate image {}. Cause: {}", createInfo.m_Name, result) THEN_ABORT(result);
auto object = Alloc();
object->m_Image = image;
object->m_Allocation = allocation;
object->m_Extent = imageCreateInfo.extent;
object->m_Format = imageCreateInfo.format;
object->m_LayerCount = Cast<u8>(imageCreateInfo.arrayLayers);
object->m_MipLevels = Cast<u8>(imageCreateInfo.mipLevels);
object->m_Flags_ = {};
vk::Image image = rawImage;
u8 layerCount = Cast<u8>(imageCreateInfo.arrayLayers);
u8 mipLevels = Cast<u8>(imageCreateInfo.mipLevels);
Image::Flags flags = {};
if (createInfo.m_IsSampled)
object->m_Flags_ |= Image::SAMPLED_BIT;
flags |= Image::FlagBits::eSampled;
if (createInfo.m_IsStorage)
object->m_Flags_ |= Image::STORAGE_BIT;
flags |= Image::FlagBits::eStorage;
m_Device->SetName(object->m_Image, createInfo.m_Name);
m_Device->SetName(image, createInfo.m_Name);
return object;
return std::make_shared<Image>(m_Device, image, allocation, imageCreateInfo.extent, imageCreateInfo.format, flags,
layerCount, mipLevels);
}
Ref<ImageCube>
@ -66,29 +69,27 @@ ImageManager::CreateTextureCube(const TextureCubeCreateInfo &createInfo)
.usage = VMA_MEMORY_USAGE_AUTO,
};
VkImage image;
VkImage rawImage;
VmaAllocation allocation;
vk::ImageCreateInfo imageCreateInfo = ToImageCreateInfo(createInfo);
auto result = Cast<vk::Result>(vmaCreateImage(m_Device->m_Allocator, Recast<VkImageCreateInfo *>(&imageCreateInfo),
&allocationCreateInfo, &image, &allocation, nullptr));
&allocationCreateInfo, &rawImage, &allocation, nullptr));
ERROR_IF(Failed(result), "Could not allocate image {}. Cause: {}", createInfo.m_Name, result) THEN_ABORT(result);
auto object = Alloc();
object->m_Image = image;
object->m_Allocation = allocation;
object->m_Extent = imageCreateInfo.extent;
object->m_Format = imageCreateInfo.format;
object->m_LayerCount = Cast<u8>(imageCreateInfo.arrayLayers);
object->m_MipLevels = Cast<u8>(imageCreateInfo.mipLevels);
object->m_Flags_ = Image::CUBE_BIT;
vk::Image image = rawImage;
u8 layerCount = Cast<u8>(imageCreateInfo.arrayLayers);
u8 mipLevels = Cast<u8>(imageCreateInfo.mipLevels);
Image::Flags flags = Image::FlagBits::eCube;
if (createInfo.m_IsSampled)
object->m_Flags_ |= Image::SAMPLED_BIT;
flags |= Image::FlagBits::eSampled;
if (createInfo.m_IsStorage)
object->m_Flags_ |= Image::STORAGE_BIT;
flags |= Image::FlagBits::eStorage;
m_Device->SetName(object->m_Image, createInfo.m_Name);
m_Device->SetName(image, createInfo.m_Name);
return CastImage<ImageCube>(object);
return CastImage<ImageCube>(std::make_shared<Image>(m_Device, image, allocation, imageCreateInfo.extent,
imageCreateInfo.format, flags, layerCount, mipLevels));
}
Ref<Image>
@ -99,24 +100,22 @@ ImageManager::CreateAttachment(const AttachmentCreateInfo &createInfo)
.usage = VMA_MEMORY_USAGE_AUTO,
};
VkImage image;
VkImage rawImage;
VmaAllocation allocation;
vk::ImageCreateInfo imageCreateInfo = ToImageCreateInfo(createInfo);
auto result = Cast<vk::Result>(vmaCreateImage(m_Device->m_Allocator, Recast<VkImageCreateInfo *>(&imageCreateInfo),
&allocationCreateInfo, &image, &allocation, nullptr));
&allocationCreateInfo, &rawImage, &allocation, nullptr));
ERROR_IF(Failed(result), "Could not allocate image {}. Cause: {}", createInfo.m_Name, result) THEN_ABORT(result);
auto object = Alloc();
object->m_Image = image;
object->m_Allocation = allocation;
object->m_Extent = imageCreateInfo.extent;
object->m_Format = imageCreateInfo.format;
object->m_LayerCount = Cast<u8>(imageCreateInfo.arrayLayers);
object->m_MipLevels = Cast<u8>(imageCreateInfo.mipLevels);
vk::Image image = rawImage;
m_Device->SetName(object->m_Image, createInfo.m_Name);
u8 layerCount = Cast<u8>(imageCreateInfo.arrayLayers);
u8 mipLevels = Cast<u8>(imageCreateInfo.mipLevels);
return object;
m_Device->SetName(image, createInfo.m_Name);
return std::make_shared<Image>(m_Device, image, allocation, imageCreateInfo.extent, imageCreateInfo.format,
Image::Flags{}, layerCount, mipLevels);
}
Ref<Image>
@ -127,24 +126,22 @@ ImageManager::CreateDepthStencilImage(const DepthStencilImageCreateInfo &createI
.usage = VMA_MEMORY_USAGE_AUTO,
};
VkImage image;
VkImage rawImage;
VmaAllocation allocation;
vk::ImageCreateInfo imageCreateInfo = ToImageCreateInfo(createInfo);
auto result = Cast<vk::Result>(vmaCreateImage(m_Device->m_Allocator, Recast<VkImageCreateInfo *>(&imageCreateInfo),
&allocationCreateInfo, &image, &allocation, nullptr));
&allocationCreateInfo, &rawImage, &allocation, nullptr));
ERROR_IF(Failed(result), "Could not allocate image {}. Cause: {}", createInfo.m_Name, result) THEN_ABORT(result);
auto object = Alloc();
object->m_Image = image;
object->m_Allocation = allocation;
object->m_Extent = imageCreateInfo.extent;
object->m_Format = imageCreateInfo.format;
object->m_LayerCount = Cast<u8>(imageCreateInfo.arrayLayers);
object->m_MipLevels = Cast<u8>(imageCreateInfo.mipLevels);
vk::Image image = rawImage;
m_Device->SetName(object->m_Image, createInfo.m_Name);
u8 layerCount = Cast<u8>(imageCreateInfo.arrayLayers);
u8 mipLevels = Cast<u8>(imageCreateInfo.mipLevels);
return object;
m_Device->SetName(image, createInfo.m_Name);
return std::make_shared<Image>(m_Device, image, allocation, imageCreateInfo.extent, imageCreateInfo.format,
Image::Flags{}, layerCount, mipLevels);
}
vk::ImageCreateInfo
@ -237,8 +234,3 @@ ToImageCreateInfo(const DepthStencilImageCreateInfo &createInfo)
.usage = usage,
};
}
ImageManager::ImageManager(const Device *device, const u32 maxCount)
: Manager{device, maxCount}
{
}

View File

@ -1,6 +0,0 @@
// =============================================
// Aster: manager.cpp
// Copyright (c) 2020-2025 Anish Bhobe
// =============================================
#include "systems/manager.h"

View File

@ -9,8 +9,8 @@
using namespace systems;
SamplerManager::SamplerManager(const Device *device, const u32 maxCount)
: Manager{device, maxCount}
SamplerManager::SamplerManager(const Device *device)
: m_Device{device}
{
}
@ -24,14 +24,12 @@ SamplerManager::CreateSampler(const SamplerCreateInfo &createInfo)
{
auto vkCreateInfo = Cast<vk::SamplerCreateInfo>(createInfo);
if (const auto iter = m_HashToSamplerIdx.find(vkCreateInfo); iter != m_HashToSamplerIdx.end())
if (const auto iter = m_HashToSamplerIdx.find(vkCreateInfo); iter != m_HashToSamplerIdx.end() && !iter->second.expired())
{
return iter->second;
return iter->second.lock();
}
auto object = Alloc();
object->Init(m_Device, vkCreateInfo, createInfo.m_Name ? createInfo.m_Name : nullptr);
auto object = std::make_shared<Sampler>(m_Device, vkCreateInfo, createInfo.m_Name ? createInfo.m_Name : nullptr);
m_HashToSamplerIdx.emplace(vkCreateInfo, object);
return object;

View File

@ -9,13 +9,13 @@
using namespace systems;
ViewManager::ViewManager(const Device *device, const u32 maxCount)
: Manager{device, maxCount}
ViewManager::ViewManager(const Device *device)
: m_Device{device}
{
}
Ref<ImageView>
ViewManager::CreateView(const ViewCreateInfo<> &createInfo)
ViewManager::CreateView(const ViewCreateInfo<Image> &createInfo) const
{
const auto layerCount = createInfo.GetLayerCount();
const auto mipCount = createInfo.GetMipLevelCount();
@ -32,14 +32,6 @@ ViewManager::CreateView(const ViewCreateInfo<> &createInfo)
m_Device->SetName(view, createInfo.m_Name);
auto object = Alloc();
object->m_Image = createInfo.m_Image;
object->m_View = view;
object->m_Extent = createInfo.m_Image->m_Extent;
object->m_BaseLayer = createInfo.m_BaseLayer;
object->m_LayerCount = layerCount;
object->m_BaseMipLevel = createInfo.m_BaseMipLevel;
object->m_MipLevelCount = mipCount;
return object;
return std::make_shared<ImageView>(createInfo.m_Image, view, createInfo.m_Image->m_Extent, createInfo.m_BaseLayer,
layerCount, createInfo.m_BaseMipLevel, mipCount);
}

View File

@ -131,7 +131,7 @@ main(int, char **)
vk::Queue commandQueue = device.GetQueue(queueAllocation.m_Family, 0);
Swapchain swapchain = {&surface, &device, window.GetSize(), "Primary Chain"};
systems::ResourceManager resourceManager{&device, 12, 12, 1, 12};
systems::ResourceManager resourceManager{&device};
systems::CommitManager commitManager{&device, 12, 12, 12, resourceManager.Samplers().CreateSampler({})};
@ -221,10 +221,9 @@ main(int, char **)
});
{
StagingBuffer imageStaging;
imageStaging.Init(&device, imageFile.GetSize(), "Image Staging");
imageStaging.Write(0, imageFile.GetSize(), imageFile.m_Data);
auto imageStaging = resourceManager.Buffers().CreateStagingBuffer(imageFile.GetSize(), "Image Staging");
imageStaging->Write(0, imageFile.GetSize(), imageFile.m_Data);
vk::ImageMemoryBarrier2 imageReadyToWrite = {
.srcStageMask = vk::PipelineStageFlagBits2::eTransfer,
@ -297,7 +296,7 @@ main(int, char **)
.imageOffset = {},
.imageExtent = {imageFile.m_Width, imageFile.m_Height, 1},
};
copyBuffer.copyBufferToImage(imageStaging.m_Buffer, crate->GetImage(), vk::ImageLayout::eTransferDstOptimal, 1,
copyBuffer.copyBufferToImage(imageStaging->m_Buffer, crate->GetImage(), vk::ImageLayout::eTransferDstOptimal, 1,
&imageCopy);
copyBuffer.pipelineBarrier2(&imageReadyToReadDependency);
@ -318,7 +317,6 @@ main(int, char **)
AbortIfFailedM(device.m_Device.resetCommandPool(copyPool, {}), "Couldn't reset command pool.");
device.m_Device.destroy(fence, nullptr);
imageStaging.Destroy();
}
auto ubo = resourceManager.Buffers().CreateStorageBuffer(sizeof camera, "Camera UBO");
@ -426,7 +424,6 @@ main(int, char **)
{
Time::Update();
commitManager.Update();
resourceManager.Update();
camera.m_Model *= rotate(mat4{1.0f}, Cast<f32>(45.0_deg * Time::m_Delta), vec3(0.0f, 1.0f, 0.0f));
ubo->Write(0, sizeof camera, &camera);

View File

@ -76,9 +76,9 @@ AssetLoader::LoadHdrImage(cstr path, cstr name) const
auto *pDevice = m_CommitManager->m_Device;
StagingBuffer stagingBuffer;
stagingBuffer.Init(pDevice, (sizeof *data) * x * y * 4, "HDR Staging Buffer");
stagingBuffer.Write(0, stagingBuffer.m_Size, data);
auto stagingBuffer =
m_ResourceManager->Buffers().CreateStagingBuffer((sizeof *data) * x * y * 4, "HDR Staging Buffer");
stagingBuffer->Write(0, stagingBuffer->m_Size, data);
stbi_image_free(data);
@ -98,7 +98,7 @@ AssetLoader::LoadHdrImage(cstr path, cstr name) const
.imageExtent = texture->m_Extent,
};
vk::CopyBufferToImageInfo2 stagingInfo = {
.srcBuffer = stagingBuffer.m_Buffer,
.srcBuffer = stagingBuffer->m_Buffer,
.dstImage = texture->GetImage(),
.dstImageLayout = vk::ImageLayout::eTransferDstOptimal,
.regionCount = 1,
@ -194,8 +194,6 @@ AssetLoader::LoadHdrImage(cstr path, cstr name) const
AbortIfFailed(pDevice->m_Device.resetCommandPool(m_CommandPool, {}));
stagingBuffer.Destroy();
return texture;
}

View File

@ -139,7 +139,7 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, const Ref<Texture> &textureView
vk::ImageLayout finalLayout, vk::PipelineStageFlags2 prevStage, vk::PipelineStageFlags2 finalStage);
void
GenerateMipMaps(vk::CommandBuffer commandBuffer, concepts::SampledImageRef auto &texture, vk::ImageLayout initialLayout,
GenerateMipMaps(vk::CommandBuffer commandBuffer, concepts::ImageRefTo<Texture> auto &texture, vk::ImageLayout initialLayout,
vk::ImageLayout finalLayout,
vk::PipelineStageFlags2 prevStage = vk::PipelineStageFlagBits2::eAllCommands,
vk::PipelineStageFlags2 finalStage = vk::PipelineStageFlagBits2::eAllCommands)
@ -149,7 +149,7 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, concepts::SampledImageRef auto
}
void
GenerateMipMaps(vk::CommandBuffer commandBuffer, concepts::SampledImageViewRef auto &texture,
GenerateMipMaps(vk::CommandBuffer commandBuffer, concepts::ViewRefTo<Texture> auto &texture,
vk::ImageLayout initialLayout, vk::ImageLayout finalLayout,
vk::PipelineStageFlags2 prevStage = vk::PipelineStageFlagBits2::eAllCommands,
vk::PipelineStageFlags2 finalStage = vk::PipelineStageFlagBits2::eAllCommands)
@ -157,5 +157,3 @@ GenerateMipMaps(vk::CommandBuffer commandBuffer, concepts::SampledImageViewRef a
GenerateMipMaps(commandBuffer, systems::CastImage<Texture>(texture->m_Image), initialLayout, finalLayout, prevStage,
finalStage);
}
static_assert(concepts::SampledImageRef<Ref<Texture>>);

View File

@ -178,7 +178,7 @@ main(int, char **)
vk::Queue graphicsQueue = device.GetQueue(queueAllocation.m_Family, 0);
Swapchain swapchain = {&surface, &device, window.GetSize(), "Primary Chain"};
systems::ResourceManager resourceManager = {&device, 1000, 1000, 10, 1000};
auto resourceManager = systems::ResourceManager{&device};
systems::CommitManager commitManager = {&device, 1000, 1000, 1000,
resourceManager.Samplers().CreateSampler({.m_Name = "Default Sampler"})};
@ -434,7 +434,6 @@ main(int, char **)
{
Time::Update();
commitManager.Update();
resourceManager.Update();
gui::StartBuild();

View File

@ -3,7 +3,7 @@
cmake_minimum_required(VERSION 3.13)
add_subdirectory("00_util")
add_subdirectory("01_triangle")
# add_subdirectory("01_triangle")
add_subdirectory("02_box")
add_subdirectory("03_model_render")
# add_subdirectory("04_scenes")