269 lines
6.6 KiB
C++
269 lines
6.6 KiB
C++
#ifndef GFXDEVICE_H
|
|
#define GFXDEVICE_H
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <vulkan/vulkan.h>
|
|
#include <vk_mem_alloc.h>
|
|
|
|
#include <glm/vec2.hpp>
|
|
#include <glm/vec4.hpp>
|
|
|
|
#include <destrum/Graphics/DeletionQueue.h>
|
|
#include <destrum/Graphics/GPUImage.h>
|
|
#include <destrum/Graphics/Swapchain.h>
|
|
#include <destrum/Graphics/Resources/Buffer.h>
|
|
#include <destrum/Graphics/TextureIntent.h>
|
|
#include <destrum/Graphics/ids.h>
|
|
|
|
#include "ImmediateExecuter.h"
|
|
#include "Util.h"
|
|
#include "Managers/FrameManager.h"
|
|
#include "Managers/VulkanInstanceManager.h"
|
|
#include "Managers/MemoryManager.h"
|
|
#include "Managers/ImageManager.h"
|
|
|
|
class ImguiPass;
|
|
|
|
#if defined(TRACY_ENABLE)
|
|
namespace tracy
|
|
{
|
|
class VkCtx;
|
|
}
|
|
using DestrumTracyVkCtx = tracy::VkCtx*;
|
|
#else
|
|
using DestrumTracyVkCtx = void*;
|
|
#endif
|
|
|
|
using ImmediateExecuteFunction = std::function<void(VkCommandBuffer)>;
|
|
|
|
class GfxDevice
|
|
{
|
|
public:
|
|
struct EndFrameProps
|
|
{
|
|
VkClearColorValue clearColor{{0.f, 0.f, 0.f, 1.f}};
|
|
ImguiPass* imguiPass{nullptr};
|
|
bool copyDrawImageToSwapchain{true};
|
|
};
|
|
|
|
public:
|
|
GfxDevice();
|
|
~GfxDevice();
|
|
|
|
GfxDevice(const GfxDevice&) = delete;
|
|
GfxDevice& operator=(const GfxDevice&) = delete;
|
|
|
|
void init(SDL_Window* window, const std::string& appName, bool vSync);
|
|
void cleanup();
|
|
|
|
[[nodiscard]] bool recreateSwapchain(int width, int height);
|
|
|
|
VkCommandBuffer beginFrame();
|
|
|
|
void endFrame(
|
|
VkCommandBuffer cmd,
|
|
const GPUImage& drawImage,
|
|
const EndFrameProps& props);
|
|
|
|
void waitIdle();
|
|
|
|
[[nodiscard]] bool needsSwapchainRecreate() const
|
|
{
|
|
return swapchain.isDirty();
|
|
}
|
|
|
|
VulkanImmediateExecutor& getImmediateExecuter();
|
|
[[deprecated("Use getImmediateExecuter()")]]
|
|
VulkanImmediateExecutor& GetImmediateExecuter() { return getImmediateExecuter(); }
|
|
|
|
void immediateSubmit(ImmediateExecuteFunction&& f) const;
|
|
|
|
[[nodiscard]] std::uint32_t getCurrentFrameIndex() const
|
|
{
|
|
return frameManager.getCurrentFrameIndex();
|
|
}
|
|
|
|
[[nodiscard]] FrameManager::FrameData& getCurrentFrame()
|
|
{
|
|
return frameManager.getCurrentFrame();
|
|
}
|
|
|
|
[[nodiscard]] const FrameManager::FrameData& getCurrentFrame() const
|
|
{
|
|
return frameManager.getCurrentFrame();
|
|
}
|
|
|
|
[[nodiscard]] VkExtent2D getSwapchainExtent() const
|
|
{
|
|
return swapchain.getExtent();
|
|
}
|
|
|
|
[[nodiscard]] GPUBuffer createBuffer(
|
|
std::size_t allocSize,
|
|
VkBufferUsageFlags usage,
|
|
VmaMemoryUsage memoryUsage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE) const
|
|
{
|
|
return memoryManager.createBuffer(allocSize, usage, memoryUsage);
|
|
}
|
|
|
|
void destroyBuffer(GPUBuffer& buffer);
|
|
void destroyBufferNow(GPUBuffer& buffer) const
|
|
{
|
|
memoryManager.destroyBuffer(buffer);
|
|
}
|
|
|
|
[[nodiscard]] VkDeviceAddress getBufferAddress(const GPUBuffer& buffer) const
|
|
{
|
|
return memoryManager.getBufferAddress(buffer);
|
|
}
|
|
|
|
[[nodiscard]] GPUImage createImageRaw(
|
|
const vkutil::CreateImageInfo& createInfo,
|
|
std::optional<VmaAllocationCreateInfo> customAllocationCreateInfo = std::nullopt) const
|
|
{
|
|
return imageManager.createImage(createInfo, customAllocationCreateInfo);
|
|
}
|
|
|
|
[[nodiscard]] std::optional<GPUImage> loadImageFromFileRaw(
|
|
const std::filesystem::path& path,
|
|
VkImageUsageFlags usage,
|
|
bool mipMap,
|
|
TextureIntent intent) const
|
|
{
|
|
return imageManager.loadImageFromFile(path, usage, mipMap, intent);
|
|
}
|
|
|
|
[[nodiscard]] std::optional<GPUImage> createImageFromData(
|
|
const ImageData& data,
|
|
VkImageUsageFlags usage,
|
|
bool mipMap,
|
|
const std::string& debugName) const
|
|
{
|
|
return imageManager.createImageFromData(data, usage, mipMap, debugName);
|
|
}
|
|
|
|
void uploadImageDataSized(
|
|
const GPUImage& image,
|
|
const void* pixelData,
|
|
std::size_t byteSize,
|
|
std::uint32_t layer) const
|
|
{
|
|
imageManager.uploadImageData(image, pixelData, byteSize, layer);
|
|
}
|
|
|
|
void destroyImage(GPUImage& image);
|
|
void destroyImageNow(GPUImage& image) const
|
|
{
|
|
imageManager.destroyImage(image);
|
|
}
|
|
|
|
void deferDestroy(std::function<void()>&& deleter);
|
|
|
|
[[nodiscard]] std::uint64_t getFrameNumber() const
|
|
{
|
|
return frameManager.getFrameNumber();
|
|
}
|
|
|
|
[[nodiscard]] std::size_t getPendingDeletionCount() const
|
|
{
|
|
return deletionQueue.size();
|
|
}
|
|
|
|
[[nodiscard]] VkDevice getVkDevice() const
|
|
{
|
|
return instanceManager.getDevice();
|
|
}
|
|
|
|
[[nodiscard]] VkDevice getDevice() const
|
|
{
|
|
return getVkDevice();
|
|
}
|
|
|
|
[[nodiscard]] vkb::Device getVkbDevice() const
|
|
{
|
|
return instanceManager.getVkbDevice();
|
|
}
|
|
|
|
[[nodiscard]] VkInstance getVkInstance() const
|
|
{
|
|
return instanceManager.getInstance();
|
|
}
|
|
|
|
[[nodiscard]] VkPhysicalDevice getVkPhysicalDevice() const
|
|
{
|
|
return instanceManager.getPhysicalDevice();
|
|
}
|
|
|
|
[[nodiscard]] VmaAllocator getAllocator() const
|
|
{
|
|
return memoryManager.getAllocator();
|
|
}
|
|
|
|
[[nodiscard]] std::uint32_t getGraphicsQueueFamily() const
|
|
{
|
|
return instanceManager.getGraphicsQueueFamily();
|
|
}
|
|
|
|
[[nodiscard]] VkQueue getGraphicsQueue() const
|
|
{
|
|
return instanceManager.getGraphicsQueue();
|
|
}
|
|
|
|
[[nodiscard]] VkQueue getPresentQueue() const
|
|
{
|
|
return instanceManager.getPresentQueue();
|
|
}
|
|
|
|
[[nodiscard]] VkFormat getSwapchainFormat() const
|
|
{
|
|
return swapchainFormat;
|
|
}
|
|
|
|
[[nodiscard]] std::uint32_t getSwapchainImageCount() const
|
|
{
|
|
return swapchain.getImageCount();
|
|
}
|
|
|
|
[[nodiscard]] VkImageView getSwapchainImageView(std::uint32_t imageIndex) const
|
|
{
|
|
return swapchain.getImageView(imageIndex);
|
|
}
|
|
|
|
[[nodiscard]] DestrumTracyVkCtx getTracyVkCtx() const
|
|
{
|
|
return instanceManager.getTracyVkCtx();
|
|
}
|
|
|
|
VulkanInstanceManager& getInstanceManager() { return instanceManager; }
|
|
MemoryManager& getMemoryManager() { return memoryManager; }
|
|
|
|
private:
|
|
VulkanInstanceManager instanceManager;
|
|
MemoryManager memoryManager;
|
|
FrameManager frameManager;
|
|
ImageManager imageManager;
|
|
|
|
VulkanImmediateExecutor executor;
|
|
DeletionQueue deletionQueue;
|
|
|
|
Swapchain swapchain;
|
|
VkFormat swapchainFormat{VK_FORMAT_UNDEFINED};
|
|
|
|
std::uint32_t activeFrameIndex{0};
|
|
std::uint32_t activeSwapchainImageIndex{0};
|
|
bool frameActive{false};
|
|
|
|
bool m_vSync{false};
|
|
bool initialized{false};
|
|
};
|
|
|
|
#endif
|