Window is now independent of vulkan.

This commit is contained in:
Anish Bhobe 2025-01-20 23:00:49 +01:00
parent a3dcf22fa5
commit 0462dc33f0
17 changed files with 194 additions and 82 deletions

5
Folder.DotSettings.user Normal file
View File

@ -0,0 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/Housekeeping/Bookmarks/NumberedBookmarks/=Bookmark5/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/Housekeeping/Bookmarks/NumberedBookmarks/=Bookmark5/Coords/@EntryValue">(Doc Ln 10 Col 4)</s:String>
<s:String x:Key="/Default/Housekeeping/Bookmarks/NumberedBookmarks/=Bookmark5/FileId/@EntryValue">DB94C55F-6B9A-4AAE-80D7-29FD8153E6B5/d:aster/f:window.h</s:String>
<s:String x:Key="/Default/Housekeeping/Bookmarks/NumberedBookmarks/=Bookmark5/Owner/@EntryValue">NumberedBookmarkManager</s:String></wpf:ResourceDictionary>

View File

@ -22,7 +22,7 @@ set(HEADER_FILES
swapchain.h
pipeline.h
queue_allocation.h
buffer.h)
buffer.h "surface.h" "size.h")
set(SOURCE_FILES
logger.cpp
@ -35,7 +35,7 @@ set(SOURCE_FILES
pipeline.cpp
buffer.cpp
image.cpp
image.h)
image.h "surface.cpp")
add_library(aster_core STATIC ${SOURCE_FILES} ${HEADER_FILES})
set_property(TARGET aster_core PROPERTY CXX_STANDARD 20)

View File

@ -42,14 +42,6 @@ Context::Context(const cstr appName, const Version version, bool enableValidatio
{
INFO_IF(enableValidation, "Validation Layers enabled");
if (!glfwInit())
{
const char *error = nullptr;
const auto code = glfwGetError(&error);
ERROR("GLFW Init failed. Cause: ({}) {}", code, error)
THEN_ABORT(code);
}
// TODO Get/Check API Version
// Creating Instance
@ -119,8 +111,6 @@ Context::~Context()
}
m_Instance.destroy(nullptr);
DEBUG("Instance destroyed");
glfwTerminate();
}
Context::Context(Context &&other) noexcept

View File

@ -6,6 +6,7 @@
#include "physical_device.h"
#include "context.h"
#include "surface.h"
#include "window.h"
[[nodiscard]] vk::SurfaceCapabilitiesKHR
@ -154,12 +155,11 @@ EnumeratePhysicalDevices(const vk::Instance instance)
return physicalDevices;
}
PhysicalDevices::PhysicalDevices(const Window *window, const Context *context)
PhysicalDevices::PhysicalDevices(const Surface *surface, const Context *context)
{
auto surface = window->m_Surface;
auto physicalDevices = EnumeratePhysicalDevices(context->m_Instance);
for (auto physicalDevice : physicalDevices)
{
this->emplace_back(surface, physicalDevice);
this->emplace_back(surface->m_Surface, physicalDevice);
}
}

View File

@ -6,6 +6,8 @@
#pragma once
#include "global.h"
#include "surface.h"
#include <EASTL/fixed_vector.h>
struct Window;
@ -52,5 +54,5 @@ struct PhysicalDevice final
class PhysicalDevices : public eastl::fixed_vector<PhysicalDevice, 4>
{
public:
PhysicalDevices(const Window *window, const Context *context);
PhysicalDevices(const Surface *surface, const Context *context);
};

20
aster/size.h Normal file
View File

@ -0,0 +1,20 @@
// =============================================
// Aster: size.h
// Copyright (c) 2020-2024 Anish Bhobe
// =============================================
#pragma once
#include "global.h"
struct Size2D
{
u32 m_Width;
u32 m_Height;
explicit
operator vk::Extent2D() const
{
return {m_Width, m_Height};
}
};

51
aster/surface.cpp Normal file
View File

@ -0,0 +1,51 @@
// =============================================
// Aster: surface.cpp
// Copyright (c) 2020-2024 Anish Bhobe
// =============================================
#include "surface.h"
#include "context.h"
#include "window.h"
Surface::Surface(Context *context, const Window *window, cstr name)
: m_Context(context)
{
VkSurfaceKHR surface;
auto result = Cast<vk::Result>(
glfwCreateWindowSurface(Cast<VkInstance>(m_Context->m_Instance), window->m_Window, nullptr, &surface));
ERROR_IF(Failed(result), "Failed to create Surface with {}", result)
THEN_ABORT(result)
ELSE_DEBUG("Surface {} Created", m_Name);
m_Surface = vk::SurfaceKHR(surface);
}
Surface::~Surface()
{
if (m_Context && m_Surface)
{
m_Context->m_Instance.destroy(m_Surface, nullptr);
DEBUG("Surface Destroyed");
m_Surface = nullptr;
m_Context = nullptr;
}
}
Surface::Surface(Surface &&other) noexcept
: m_Context(Take(other.m_Context))
, m_Surface(Take(other.m_Surface))
, m_Name(std::move(other.m_Name))
{
}
Surface &
Surface::operator=(Surface &&other) noexcept
{
if (this == &other)
return *this;
m_Context = Take(other.m_Context);
m_Surface = Take(other.m_Surface);
m_Name = std::move(other.m_Name);
return *this;
}

28
aster/surface.h Normal file
View File

@ -0,0 +1,28 @@
// =============================================
// Aster: surface.h
// Copyright (c) 2020-2024 Anish Bhobe
// =============================================
#pragma once
#include "global.h"
struct Context;
struct Window;
struct Surface
{
Context *m_Context;
vk::SurfaceKHR m_Surface;
NameString m_Name;
// Ctor Dtor
Surface(Context *context, const Window *window, cstr name);
~Surface();
// Move
Surface(Surface &&other) noexcept;
Surface &operator=(Surface &&other) noexcept;
DISALLOW_COPY_AND_ASSIGN(Surface);
};

View File

@ -7,16 +7,17 @@
#include "device.h"
#include "physical_device.h"
#include "surface.h"
#include "window.h"
[[nodiscard]] vk::Extent2D GetExtent(const Window *window, vk::SurfaceCapabilitiesKHR *surfaceCapabilities);
[[nodiscard]] vk::Extent2D GetExtent(Size2D size, vk::SurfaceCapabilitiesKHR *surfaceCapabilities);
Swapchain::Swapchain(const Window *window, const Device *device, NameString &&name)
Swapchain::Swapchain(const Surface *surface, const Device *device, Size2D size, NameString &&name)
: m_Device(device)
, m_Name(std::move(name))
, m_Format(vk::Format::eUndefined)
{
this->Create(window);
this->Create(surface, size);
}
Swapchain::~Swapchain()
@ -51,20 +52,20 @@ Swapchain::operator=(Swapchain &&other) noexcept
}
void
Swapchain::Create(const Window *window)
Swapchain::Create(const Surface *surface, Size2D size)
{
auto surfaceCapabilities = GetSurfaceCapabilities(m_Device->m_PhysicalDevice, window->m_Surface);
m_Extent = GetExtent(window, &surfaceCapabilities);
auto surfaceCapabilities = GetSurfaceCapabilities(m_Device->m_PhysicalDevice, surface->m_Surface);
m_Extent = GetExtent(size, &surfaceCapabilities);
while (m_Extent.width == 0 || m_Extent.height == 0)
{
glfwWaitEvents();
surfaceCapabilities = GetSurfaceCapabilities(m_Device->m_PhysicalDevice, window->m_Surface);
m_Extent = GetExtent(window, &surfaceCapabilities);
surfaceCapabilities = GetSurfaceCapabilities(m_Device->m_PhysicalDevice, surface->m_Surface);
m_Extent = GetExtent(size, &surfaceCapabilities);
}
auto surfaceFormats = GetSurfaceFormats(m_Device->m_PhysicalDevice, window->m_Surface);
auto presentModes = GetSurfacePresentModes(m_Device->m_PhysicalDevice, window->m_Surface);
auto surfaceFormats = GetSurfaceFormats(m_Device->m_PhysicalDevice, surface->m_Surface);
auto presentModes = GetSurfacePresentModes(m_Device->m_PhysicalDevice, surface->m_Surface);
m_Format = vk::Format::eUndefined;
vk::ColorSpaceKHR swapchainColorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
@ -104,7 +105,7 @@ Swapchain::Create(const Window *window)
// TODO: Note that different queues might need the images to be shared.
const vk::SwapchainCreateInfoKHR swapchainCreateInfo = {
.surface = window->m_Surface,
.surface = surface->m_Surface,
.minImageCount = swapchainImageCount,
.imageFormat = m_Format,
.imageColorSpace = swapchainColorSpace,
@ -206,14 +207,14 @@ Swapchain::Cleanup()
}
vk::Extent2D
GetExtent(const Window *window, vk::SurfaceCapabilitiesKHR *surfaceCapabilities)
GetExtent(Size2D size, vk::SurfaceCapabilitiesKHR *surfaceCapabilities)
{
if (surfaceCapabilities->currentExtent.width != MaxValue<u32>)
{
return surfaceCapabilities->currentExtent;
}
auto [width, height] = window->GetSize();
auto [width, height] = size;
auto [minWidth, minHeight] = surfaceCapabilities->minImageExtent;
auto [maxWidth, maxHeight] = surfaceCapabilities->maxImageExtent;

View File

@ -7,10 +7,12 @@
#include "global.h"
#include "size.h"
#include <EASTL/fixed_vector.h>
struct PhysicalDevice;
struct Window;
struct Surface;
struct Device;
struct Swapchain final
@ -27,11 +29,11 @@ struct Swapchain final
eastl::vector<FnResizeCallback> m_ResizeCallbacks;
void Create(const Window *window);
void Create(const Surface *window, Size2D size);
void RegisterResizeCallback(FnResizeCallback &&callback);
// Ctor/Dtor
Swapchain(const Window *window, const Device *device, NameString &&name);
Swapchain(const Surface *window, const Device *device, Size2D size, NameString &&name);
~Swapchain();
// Move

View File

@ -8,6 +8,9 @@
#include "context.h"
#include "logger.h"
std::atomic_uint64_t Window::m_WindowCount = 0;
std::atomic_bool Window::m_IsGlfwInit = false;
void
Window::RequestExit() const noexcept
{
@ -26,7 +29,7 @@ Window::SetWindowSize(const u32 width, const u32 height) const noexcept
glfwSetWindowSize(m_Window, Cast<i32>(width), Cast<i32>(height));
}
vk::Extent2D
Size2D
Window::GetSize() const
{
int width;
@ -35,11 +38,23 @@ Window::GetSize() const
return {Cast<u32>(width), Cast<u32>(height)};
}
Window::Window(const cstr title, Context *context, vk::Extent2D extent, const b8 isFullScreen)
Window::Window(const cstr title, Size2D extent, const b8 isFullScreen)
{
m_Context = context;
m_Name = title;
if (!m_IsGlfwInit)
{
if (!glfwInit())
{
const char *error = nullptr;
const auto code = glfwGetError(&error);
ERROR("GLFW Init failed. Cause: ({}) {}", code, error)
THEN_ABORT(code);
}
m_WindowCount = 0;
m_IsGlfwInit = true;
}
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
ERROR_IF(!monitor, "No monitor found");
@ -49,10 +64,10 @@ Window::Window(const cstr title, Context *context, vk::Extent2D extent, const b8
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_CENTER_CURSOR, GLFW_TRUE);
m_Window = glfwCreateWindow(Cast<i32>(extent.width), Cast<i32>(extent.height), m_Name.c_str(),
m_Window = glfwCreateWindow(Cast<i32>(extent.m_Width), Cast<i32>(extent.m_Height), m_Name.c_str(),
isFullScreen ? monitor : nullptr, nullptr);
ERROR_IF(m_Window == nullptr, "Window creation failed")
ELSE_DEBUG("Window '{}' created with resolution '{}x{}'", m_Name, extent.width, extent.height);
ELSE_DEBUG("Window '{}' created with resolution '{}x{}'", m_Name, extent.m_Width, extent.m_Height);
if (m_Window == nullptr)
{
const char *error = nullptr;
@ -63,41 +78,35 @@ Window::Window(const cstr title, Context *context, vk::Extent2D extent, const b8
if (isFullScreen == false)
{
glfwSetWindowPos(m_Window, Cast<i32>(windowWidth - extent.width) / 2,
Cast<i32>(windowHeight - extent.height) / 2);
glfwSetWindowPos(m_Window, Cast<i32>(windowWidth - extent.m_Width) / 2,
Cast<i32>(windowHeight - extent.m_Height) / 2);
}
glfwSetInputMode(m_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
VkSurfaceKHR surface;
auto result =
Cast<vk::Result>(glfwCreateWindowSurface(Cast<VkInstance>(m_Context->m_Instance), m_Window, nullptr, &surface));
ERROR_IF(Failed(result), "Failed to create Surface with {}", result)
THEN_ABORT(result)
ELSE_DEBUG("Surface {} Created", m_Name);
m_Surface = vk::SurfaceKHR(surface);
++m_WindowCount;
}
Window::~Window()
{
if (m_Context && m_Surface)
{
m_Context->m_Instance.destroy(m_Surface, nullptr);
DEBUG("Surface Destroyed");
}
if (m_Window)
{
glfwDestroyWindow(m_Window);
m_Window = nullptr;
--m_WindowCount;
}
if (m_WindowCount== 0 && m_IsGlfwInit)
{
glfwTerminate();
m_IsGlfwInit = false;
}
DEBUG("Window '{}' Destroyed", m_Name);
}
Window::Window(Window &&other) noexcept
: m_Context(other.m_Context)
, m_Window(Take(other.m_Window))
, m_Surface(Take(other.m_Surface))
: m_Window(Take(other.m_Window))
, m_Name(Take(other.m_Name))
{
}
@ -107,9 +116,7 @@ Window::operator=(Window &&other) noexcept
{
if (this == &other)
return *this;
m_Context = other.m_Context;
m_Window = Take(other.m_Window);
m_Surface = Take(other.m_Surface);
m_Name = Take(other.m_Name);
return *this;
}

View File

@ -6,18 +6,19 @@
#pragma once
#include "global.h"
#include <EASTL/fixed_string.h>
struct Context;
#include "size.h"
#include <EASTL/fixed_string.h>
struct Window final
{
// fields
Context *m_Context{};
GLFWwindow *m_Window = nullptr;
vk::SurfaceKHR m_Surface;
eastl::fixed_string<char, 32> m_Name;
NameString m_Name;
static std::atomic_uint64_t m_WindowCount;
static std::atomic_bool m_IsGlfwInit;
// Methods
[[nodiscard]] bool
@ -31,10 +32,10 @@ struct Window final
void SetWindowSize(const vk::Extent2D &extent) const noexcept;
void SetWindowSize(u32 width, u32 height) const noexcept;
/// Actual size of the framebuffer being used for the window render.
[[nodiscard]] vk::Extent2D GetSize() const;
[[nodiscard]] Size2D GetSize() const;
// Ctor/Dtor
Window(cstr title, Context *context, vk::Extent2D extent, b8 isFullScreen = false);
Window(cstr title, Size2D extent, b8 isFullScreen = false);
~Window();
// Move

View File

@ -52,7 +52,7 @@ Frame::Frame(const Device *device, const u32 queueFamilyIndex, const u32 frameCo
}
void
Frame::Present(const vk::Queue commandQueue, Swapchain *swapchain, const Window *window)
Frame::Present(const vk::Queue commandQueue, Swapchain *swapchain, const Surface *surface, Size2D size)
{
vk::PresentInfoKHR presentInfo = {
@ -70,7 +70,7 @@ Frame::Present(const vk::Queue commandQueue, Swapchain *swapchain, const Window
case vk::Result::eErrorOutOfDateKHR:
case vk::Result::eSuboptimalKHR:
DEBUG("Recreating Swapchain. Cause: {}", result);
swapchain->Create(window);
swapchain->Create(surface, size);
break; // Present failed. We do nothing. Frame is skipped.
default:
AbortIfFailedM(result, "Swapchain Present failed.");
@ -128,7 +128,7 @@ FrameManager::FrameManager(const Device *device, u32 queueFamilyIndex, u32 frame
}
Frame *
FrameManager::GetNextFrame(Swapchain *swapchain, const Window *window)
FrameManager::GetNextFrame(Swapchain *swapchain, const Surface *surface, Size2D size)
{
Frame *currentFrame = &m_Frames[m_CurrentFrameIdx];
@ -153,7 +153,7 @@ FrameManager::GetNextFrame(Swapchain *swapchain, const Window *window)
break; // Image acquired. Break out of loop.
case vk::Result::eErrorOutOfDateKHR:
DEBUG("Recreating Swapchain. Cause: {}", result);
swapchain->Create(window);
swapchain->Create(surface, size);
break; // Image acquire has failed. We move to the next frame.
default:
AbortIfFailedMV(result, "Waiting for swapchain image {} failed.", frameIndex);

View File

@ -8,11 +8,14 @@
#include "global.h"
#include "helpers.h"
#include "size.h"
#include <EASTL/fixed_vector.h>
struct Device;
struct Window;
struct Swapchain;
struct Surface;
struct Frame
{
@ -28,7 +31,7 @@ struct Frame
// Transient
u32 m_ImageIdx;
void Present(vk::Queue commandQueue, Swapchain *swapchain, const Window *window);
void Present(const vk::Queue commandQueue, Swapchain* swapchain, const Surface* surface, Size2D size);
Frame(const Device *device, u32 queueFamilyIndex, u32 frameCount);
~Frame();
@ -48,5 +51,5 @@ struct FrameManager
FrameManager(const Device *device, u32 queueFamilyIndex, u32 framesInFlight);
Frame *GetNextFrame(Swapchain *swapchain, const Window *window);
Frame *GetNextFrame(Swapchain *swapchain, const Surface *surface, Size2D size);
};

View File

@ -528,7 +528,7 @@ main(int, char **)
};
AbortIfFailed(commandQueue.submit(1, &submitInfo, currentFrame->m_FrameAvailableFence));
currentFrame->Present(commandQueue, &swapchain, &window);
currentFrame->Present(commandQueue, &swapchain, TODO, TODO);
}
device.WaitIdle();

View File

@ -134,9 +134,10 @@ main(int, char **)
MIN_LOG_LEVEL(Logger::LogType::eInfo);
Context context = {"ModelRender", VERSION};
Window window = {"ModelRender (Aster)", &context, {INIT_WIDTH, INIT_HEIGHT}};
Window window = {"ModelRender (Aster)", {INIT_WIDTH, INIT_HEIGHT}};
Surface surface = {&context, &window, "Primary Surface"};
PhysicalDevices physicalDevices = {&window, &context};
PhysicalDevices physicalDevices = {&surface, &context};
PhysicalDevice deviceToUse = FindSuitableDevice(physicalDevices);
usize physicalDeviceOffsetAlignment = deviceToUse.m_DeviceProperties.limits.minUniformBufferOffsetAlignment;
@ -173,7 +174,7 @@ main(int, char **)
Device device = {&context, &deviceToUse, &enabledDeviceFeatures,
{queueAllocation}, pipelineCacheData, "Primary Device"};
vk::Queue graphicsQueue = device.GetQueue(queueAllocation.m_Family, 0);
Swapchain swapchain = {&window, &device, "Primary Chain"};
Swapchain swapchain = {&surface, &device, window.GetSize(), "Primary Chain"};
GpuResourceManager resourceManager = {&device, 1000};
AssetLoader assetLoader = {&resourceManager, graphicsQueue, queueAllocation.m_Family, queueAllocation.m_Family};
@ -513,7 +514,7 @@ main(int, char **)
cameraController.m_Camera.CalculateInverses();
ubo.Write(&device, 0, sizeof cameraController.m_Camera, &cameraController.m_Camera);
Frame *currentFrame = frameManager.GetNextFrame(&swapchain, &window);
Frame *currentFrame = frameManager.GetNextFrame(&swapchain, &surface, window.GetSize());
u32 imageIndex = currentFrame->m_ImageIdx;
vk::Image currentSwapchainImage = swapchain.m_Images[imageIndex];
@ -686,7 +687,7 @@ main(int, char **)
};
AbortIfFailed(graphicsQueue.submit(1, &submitInfo, currentFrame->m_FrameAvailableFence));
currentFrame->Present(graphicsQueue, &swapchain, &window);
currentFrame->Present(graphicsQueue, &swapchain, &surface, window.GetSize());
}
device.WaitIdle();

View File

@ -37,10 +37,11 @@ main(int, char *[])
{
MIN_LOG_LEVEL(Logger::LogType::eInfo);
Window window = {"Scene Render [WIP] (Aster)", {INIT_WIDTH, INIT_HEIGHT}};
Context context = {"Scene Render [WIP]", VERSION};
Window window = {"Scene Render [WIP] (Aster)", &context, {INIT_WIDTH, INIT_HEIGHT}};
Surface surface = {&context, &window, "Primary Surface"};
PhysicalDevices physicalDevices = {&window, &context};
PhysicalDevices physicalDevices = {&surface, &context};
PhysicalDevice deviceToUse = FindSuitableDevice(physicalDevices);
vk::Extent2D internalResolution = {1920, 1080};
@ -91,7 +92,7 @@ main(int, char *[])
Device device = {&context, &deviceToUse, &enabledDeviceFeatures,
{queueAllocation}, pipelineCacheData, "Primary Device"};
vk::Queue graphicsQueue = device.GetQueue(queueAllocation.m_Family, 0);
Swapchain swapchain = {&window, &device, "Primary Chain"};
Swapchain swapchain = {&surface, &device, window.GetSize(),"Primary Chain"};
RenderResourceManager resourceManager = {&device, 1024};
EcsRegistry registry;
@ -355,7 +356,7 @@ main(int, char *[])
// glm::rotate(dynTrans.m_Rotation, Cast<f32>(30_deg * (++index) * Time::m_Delta), vec3{0.0f, 1.0f, 0.0f});
//}
Frame *currentFrame = frameManager.GetNextFrame(&swapchain, &window);
Frame *currentFrame = frameManager.GetNextFrame(&swapchain, &surface, window.GetSize());
u32 imageIndex = currentFrame->m_ImageIdx;
vk::Image currentSwapchainImage = swapchain.m_Images[imageIndex];
@ -552,7 +553,7 @@ main(int, char *[])
};
AbortIfFailed(graphicsQueue.submit(1, &submitInfo, currentFrame->m_FrameAvailableFence));
currentFrame->Present(graphicsQueue, &swapchain, &window);
currentFrame->Present(graphicsQueue, &swapchain, &surface, window.GetSize());
}
device.WaitIdle();