Compare commits

..

10 Commits

Author SHA1 Message Date
f3807f1557 Rename NOVA_LOG to NOVA_INFO 2025-09-07 18:23:57 +10:00
21cf49a1f9 Implement basic WindowDriver for Windows platform 2025-09-07 02:21:46 +10:00
ba30cc49d5 Use std::string when setting Window title instead of std::string_view 2025-09-07 02:21:45 +10:00
829a00bab8 Remove missing header 2025-09-07 02:14:25 +10:00
e7f2103fff Bump CMake and C++ versions 2025-06-26 22:15:30 +10:00
db7d50c371 Refactor various headers 2025-06-26 22:00:28 +10:00
04eec1b703 Use [[nodiscard]] attribute more sparingly
Removed all uses of the [[nodiscard]] attribute unless ignoring the
return value would lead to an error or leak e.g. memory allocations.
2025-06-26 20:28:21 +10:00
d89f8de08f Move WindowDriver implementations to platform folder 2025-06-21 01:23:54 +10:00
ccd5306636 Use std::any_of to check all surfaces in RenderDevice::choose_device() 2025-05-09 17:09:04 +10:00
b25e5936e2 Add prefer discrete logic to RenderDevice::choose_device() 2025-05-09 16:10:16 +10:00
31 changed files with 838 additions and 577 deletions

View File

@@ -1,12 +1,12 @@
# Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.12)
cmake_minimum_required(VERSION 3.20)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
project(nova)
enable_language(CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)

View File

@@ -8,7 +8,6 @@
#include <nova/platform/window_driver.h>
#include <nova/render/render_device.h>
#include <nova/render/render_driver.h>
#include <nova/render/render_params.h>
#include <nova/types.h>
#include <cstdlib>

View File

@@ -23,11 +23,10 @@ set(ENGINE_SRC
core/debug.cpp
drivers/dx12/render_driver.cpp
drivers/vulkan/render_driver.cpp
drivers/wayland/window_driver.cpp
drivers/win32/window_driver.cpp
drivers/x11/window_driver.cpp
platform/linux/wayland/window_driver.cpp
platform/linux/x11/window_driver.cpp
platform/windows/window_driver.cpp
platform/window_driver.cpp
render/renderer.cpp
render/render_device.cpp
render/render_driver.cpp
)

View File

@@ -44,7 +44,7 @@ namespace Nova {
};
} // namespace Nova
#define NOVA_LOG(...) ::Nova::Debug::get_logger()->info(__VA_ARGS__)
#define NOVA_INFO(...) ::Nova::Debug::get_logger()->info(__VA_ARGS__)
#define NOVA_WARN(...) ::Nova::Debug::get_logger()->warn(__VA_ARGS__)
#define NOVA_ERROR(...) ::Nova::Debug::get_logger()->error(__VA_ARGS__)
#define NOVA_CRITICAL(...) ::Nova::Debug::get_logger()->critical(__VA_ARGS__)

View File

@@ -7,10 +7,6 @@
#pragma once
namespace Nova {
enum class WindowAPI { WAYLAND, WINDOWS, X11 };
class WindowDriver;
struct Window;
using WindowID = Window*;
} // namespace Nova

View File

@@ -7,33 +7,37 @@
#pragma once
#include <nova/api.h>
#include <nova/platform/window_fwd.h>
#include <nova/render/render_fwd.h>
#include <nova/platform/platform_structs.h>
#include <nova/render/render_structs.h>
#include <nova/types.h>
#include <string_view>
#include <string>
namespace Nova {
class RenderDriver;
enum class WindowAPI { WAYLAND, WINDOWS, X11 };
class NOVA_API WindowDriver {
public:
static WindowDriver* create();
virtual ~WindowDriver() = default;
[[nodiscard]] virtual WindowAPI get_api() const = 0;
[[nodiscard]] virtual std::string get_api_name() const = 0;
virtual WindowAPI get_api() const = 0;
virtual std::string get_api_name() const = 0;
virtual void poll_events() = 0;
virtual void beep() = 0;
[[nodiscard]] virtual u32 get_window_count() const = 0;
[[nodiscard]] virtual WindowID create_window(std::string_view title, u32 width, u32 height) = 0;
virtual u32 get_window_count() const = 0;
[[nodiscard]] virtual WindowID create_window(const std::string& title, u32 width, u32 height) = 0;
virtual void destroy_window(WindowID window) = 0;
virtual void set_window_title(WindowID window, std::string_view title) = 0;
virtual void set_window_title(WindowID window, const std::string& title) = 0;
virtual void set_window_size(WindowID window, u32 width, u32 height) = 0;
virtual void set_window_position(WindowID window, i32 x, i32 y) = 0;
[[nodiscard]] virtual const char* get_surface_extension() const = 0;
virtual const char* get_surface_extension() const = 0;
[[nodiscard]] virtual SurfaceID create_surface(WindowID window, RenderDriver* driver) = 0;
};
} // namespace Nova

View File

@@ -7,15 +7,6 @@
#pragma once
namespace Nova {
enum class CullMode { NONE, FRONT, BACK };
enum class FrontFace { CLOCKWISE, COUNTER_CLOCKWISE };
enum class InputRate { VERTEX, INSTANCE };
enum class PipelineType { GRAPHICS, COMPUTE };
enum class PrimitiveTopology { POINT_LIST, LINE_LIST, LINE_STRIP, TRIANGLE_LIST, TRIANGLE_STRIP };
enum class QueueType { UNDEFINED, GRAPHICS, COMPUTE, TRANSFER };
enum class RenderAPI { DX12, VULKAN };
enum class ShaderStage { VERTEX, FRAGMENT, GEOMETRY, TESS_CONTROL, TESS_EVAL, COMPUTE, MESH, TASK };
enum class DataFormat {
UNDEFINED,
R4G4_UNORM_PACK8,
@@ -237,31 +228,4 @@ namespace Nova {
G16_B16R16_2PLANE_422_UNORM,
G16_B16_R16_3PLANE_444_UNORM,
};
class RenderDriver;
struct RenderDevice;
struct VertexAttribute;
struct VertexBinding;
struct RenderPassParams;
struct GraphicsPipelineParams;
struct ComputePipelineParams;
struct CommandBuffer;
struct CommandPool;
struct Pipeline;
struct Queue;
struct RenderPass;
struct Shader;
struct Surface;
struct Swapchain;
using CommandBufferID = CommandBuffer*;
using CommandPoolID = CommandPool*;
using PipelineID = Pipeline*;
using QueueID = Queue*;
using RenderPassID = RenderPass*;
using ShaderID = Shader*;
using SurfaceID = Surface*;
using SwapchainID = Swapchain*;
} // namespace Nova

View File

@@ -0,0 +1,13 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
namespace Nova {
struct ComputePipelineParams {
// TODO
};
} // namespace Nova

View File

@@ -6,12 +6,18 @@
#pragma once
#include <nova/render/render_fwd.h>
#include <nova/render/data_format.h>
#include <nova/render/render_structs.h>
#include <nova/types.h>
#include <vector>
namespace Nova {
enum class CullMode { NONE, FRONT, BACK };
enum class FrontFace { CLOCKWISE, COUNTER_CLOCKWISE };
enum class InputRate { VERTEX, INSTANCE };
enum class PrimitiveTopology { POINT_LIST, LINE_LIST, LINE_STRIP, TRIANGLE_LIST, TRIANGLE_STRIP };
struct VertexAttribute {
u32 binding = 0;
u32 location = 0;
@@ -25,8 +31,6 @@ namespace Nova {
InputRate rate = InputRate::VERTEX;
};
struct RenderPassParams {};
struct GraphicsPipelineParams {
std::vector<ShaderID> shaders;
std::vector<VertexBinding> bindings;
@@ -54,6 +58,4 @@ namespace Nova {
RenderPassID render_pass = nullptr;
u32 subpass = 0;
};
struct ComputePipelineParams {};
} // namespace Nova

View File

@@ -0,0 +1,13 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
namespace Nova {
struct RenderPassParams {
// TODO
};
} // namespace Nova

View File

@@ -7,7 +7,7 @@
#pragma once
#include <nova/api.h>
#include <nova/render/render_fwd.h>
#include <nova/render/render_structs.h>
#include <nova/types.h>
#include <initializer_list>
@@ -15,13 +15,15 @@
#include <string>
namespace Nova {
struct NOVA_API RenderDevice {
enum class Vendor { UNKNOWN = 0, INTEL = 0x8086, AMD = 0x1002, NVIDIA = 0x10de };
enum class Type { OTHER = 0, INTEGRATED = 1, DISCRETE = 2, VIRTUAL = 3, CPU = 4 };
class RenderDriver;
enum class DeviceVendor { UNKNOWN = 0, INTEL = 0x8086, AMD = 0x1002, NVIDIA = 0x10de };
enum class DeviceType { OTHER = 0, INTEGRATED = 1, DISCRETE = 2, VIRTUAL = 3, CPU = 4 };
struct NOVA_API RenderDevice {
std::string name;
Vendor vendor;
Type type;
DeviceVendor vendor;
DeviceType type;
u32 deviceID;
void* handle;

View File

@@ -7,30 +7,42 @@
#pragma once
#include <nova/api.h>
#include <nova/platform/window_fwd.h>
#include <nova/render/render_fwd.h>
#include <nova/platform/platform_structs.h>
#include <nova/render/params/compute_pipeline.h>
#include <nova/render/params/graphics_pipeline.h>
#include <nova/render/params/render_pass.h>
#include <nova/render/render_device.h>
#include <nova/render/render_structs.h>
#include <nova/types.h>
#include <span>
#include <string>
namespace Nova {
class WindowDriver;
enum class PipelineType { GRAPHICS, COMPUTE };
enum class QueueType { UNDEFINED, GRAPHICS, COMPUTE, TRANSFER };
enum class RenderAPI { DX12, VULKAN };
enum class ShaderStage { VERTEX, FRAGMENT, GEOMETRY, TESS_CONTROL, TESS_EVAL, COMPUTE, MESH, TASK };
class NOVA_API RenderDriver {
public:
static RenderDriver* create(RenderAPI api, WindowDriver* window_driver = nullptr);
virtual ~RenderDriver() = default;
[[nodiscard]] virtual RenderAPI get_api() const = 0;
[[nodiscard]] virtual u32 get_api_version() const = 0;
[[nodiscard]] virtual std::string get_api_name() const = 0;
[[nodiscard]] virtual std::string get_api_version_string() const = 0;
virtual RenderAPI get_api() const = 0;
virtual u32 get_api_version() const = 0;
virtual std::string get_api_name() const = 0;
virtual std::string get_api_version_string() const = 0;
[[nodiscard]] virtual u32 get_device_count() const = 0;
[[nodiscard]] virtual const RenderDevice& get_device(u32 index) const = 0;
[[nodiscard]] virtual bool get_device_supports_surface(u32 index, SurfaceID surface) const = 0;
virtual u32 get_device_count() const = 0;
virtual const RenderDevice& get_device(u32 index) const = 0;
virtual bool get_device_supports_surface(u32 index, SurfaceID surface) const = 0;
virtual void select_device(u32 index) = 0;
[[nodiscard]] virtual u32 choose_queue_family(QueueType type, SurfaceID surface) = 0;
virtual u32 choose_queue_family(QueueType type, SurfaceID surface) = 0;
[[nodiscard]] virtual QueueID get_queue(u32 queue_family) = 0;
virtual void free_queue(QueueID queue) = 0;
@@ -39,7 +51,7 @@ namespace Nova {
[[nodiscard]] virtual SwapchainID create_swapchain(SurfaceID surface) = 0;
virtual void resize_swapchain(SwapchainID swapchain) = 0;
[[nodiscard]] virtual RenderPassID get_swapchain_render_pass(SwapchainID swapchain) const = 0;
virtual RenderPassID get_swapchain_render_pass(SwapchainID swapchain) const = 0;
virtual void destroy_swapchain(SwapchainID swapchain) = 0;
[[nodiscard]] virtual ShaderID create_shader(const std::span<u8> bytes, ShaderStage stage) = 0;

View File

@@ -0,0 +1,27 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
namespace Nova {
struct CommandBuffer;
struct CommandPool;
struct Pipeline;
struct Queue;
struct RenderPass;
struct Shader;
struct Surface;
struct Swapchain;
using CommandBufferID = CommandBuffer*;
using CommandPoolID = CommandPool*;
using PipelineID = Pipeline*;
using QueueID = Queue*;
using RenderPassID = RenderPass*;
using ShaderID = Shader*;
using SurfaceID = Surface*;
using SwapchainID = Swapchain*;
} // namespace Nova

View File

@@ -1,19 +0,0 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
#include <nova/api.h>
#include <nova/render/render_fwd.h>
namespace Nova {
class NOVA_API Renderer {
public:
static void init(RenderAPI api);
static void shutdown();
static RenderDriver* get_driver();
};
} // namespace Nova

View File

@@ -8,10 +8,11 @@
#include "drivers/vulkan/render_driver.h"
#include "drivers/vulkan/render_structs.h"
#include <nova/core/debug.h>
#include <nova/platform/window_driver.h>
#include <nova/render/render_device.h>
#include <nova/render/render_params.h>
#include <nova/version.h>
#include <vulkan/vulkan.h>
@@ -19,13 +20,13 @@
#include <bit>
#include <format>
#include <limits>
#include <string_view>
#define VALIDATION_LAYER "VK_LAYER_KHRONOS_validation"
#define MAX_QUEUES_PER_FAMILY 2U
namespace {
static constexpr u32 MAX_QUEUES_PER_FAMILY = 2;
static constexpr std::string_view VALIDATION_LAYER = "VK_LAYER_KHRONOS_validation";
using namespace Nova;
static constexpr VkShaderStageFlagBits VK_SHADER_STAGE_MAP[] = {
static constexpr VkShaderStageFlagBits VK_SHADER_STAGE_MAP[] = {
VK_SHADER_STAGE_VERTEX_BIT,
VK_SHADER_STAGE_FRAGMENT_BIT,
VK_SHADER_STAGE_GEOMETRY_BIT,
@@ -34,44 +35,40 @@ static constexpr VkShaderStageFlagBits VK_SHADER_STAGE_MAP[] = {
VK_SHADER_STAGE_COMPUTE_BIT,
VK_SHADER_STAGE_MESH_BIT_EXT,
VK_SHADER_STAGE_TASK_BIT_EXT
};
};
static constexpr VkPrimitiveTopology VK_PRIMITIVE_TOPOLOGY_MAP[] = {
static constexpr VkPrimitiveTopology VK_PRIMITIVE_TOPOLOGY_MAP[] = {
VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP
};
};
// clang-format off
static constexpr VkCullModeFlags VK_CULL_MODE_MAP[] = {
static constexpr VkCullModeFlags VK_CULL_MODE_MAP[] = {
VK_CULL_MODE_NONE,
VK_CULL_MODE_FRONT_BIT,
VK_CULL_MODE_BACK_BIT
};
VK_CULL_MODE_BACK_BIT,
};
static constexpr VkFrontFace VK_FRONT_FACE_MAP[] = {
static constexpr VkFrontFace VK_FRONT_FACE_MAP[] = {
VK_FRONT_FACE_COUNTER_CLOCKWISE,
VK_FRONT_FACE_CLOCKWISE
};
VK_FRONT_FACE_CLOCKWISE,
};
static constexpr VkVertexInputRate VK_VERTEX_INPUT_RATE_MAP[] = {
static constexpr VkVertexInputRate VK_VERTEX_INPUT_RATE_MAP[] = {
VK_VERTEX_INPUT_RATE_VERTEX,
VK_VERTEX_INPUT_RATE_INSTANCE
};
VK_VERTEX_INPUT_RATE_INSTANCE,
};
static constexpr VkQueueFlagBits VK_QUEUE_FLAGS_MAP[] = {
static constexpr VkQueueFlagBits VK_QUEUE_FLAGS_MAP[] = {
static_cast<VkQueueFlagBits>(0),
VK_QUEUE_GRAPHICS_BIT,
VK_QUEUE_COMPUTE_BIT,
VK_QUEUE_TRANSFER_BIT
};
VK_QUEUE_TRANSFER_BIT,
};
// clang-format on
static constexpr VkFormat VK_FORMAT_MAP[] = {
static constexpr VkFormat VK_FORMAT_MAP[] = {
VK_FORMAT_UNDEFINED,
VK_FORMAT_R4G4_UNORM_PACK8,
VK_FORMAT_R4G4B4A4_UNORM_PACK16,
@@ -291,7 +288,10 @@ static constexpr VkFormat VK_FORMAT_MAP[] = {
VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM,
VK_FORMAT_G16_B16R16_2PLANE_422_UNORM,
VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM
};
};
} // namespace
using namespace Nova;
VulkanRenderDriver::VulkanRenderDriver(WindowDriver* p_driver) : m_window_driver(p_driver) {
NOVA_AUTO_TRACE();
@@ -375,7 +375,7 @@ void VulkanRenderDriver::select_device(const u32 p_index) {
NOVA_ASSERT(!m_device);
NOVA_ASSERT(p_index < m_devices.size());
NOVA_LOG("Using device: {}", m_devices[p_index].name);
NOVA_INFO("Using device: {}", m_devices[p_index].name);
m_physical_device = static_cast<VkPhysicalDevice>(m_devices[p_index].handle);
_check_device_extensions();
@@ -982,7 +982,7 @@ void VulkanRenderDriver::_check_version() const {
throw std::runtime_error("Vulkan API version is too low");
}
NOVA_LOG(
NOVA_INFO(
"Vulkan API version: {}.{}.{}-{}",
VK_API_VERSION_MAJOR(version),
VK_API_VERSION_MINOR(version),
@@ -1019,7 +1019,7 @@ void VulkanRenderDriver::_check_extensions() {
// Check found extensions
for (const auto& extension : available) {
if (auto it = requested.find(extension.extensionName); it != requested.end()) {
NOVA_LOG("Using extension: {}", extension.extensionName);
NOVA_INFO("Using extension: {}", extension.extensionName);
m_extensions.push_back(it->first.data());
requested.erase(it);
}
@@ -1057,8 +1057,8 @@ void VulkanRenderDriver::_check_layers() {
// Check found layers
for (const auto& layer : available) {
if (std::string_view(layer.layerName) == VALIDATION_LAYER) {
NOVA_LOG("Using layer: {}", layer.layerName);
m_layers.push_back(VALIDATION_LAYER);
NOVA_INFO("Using layer: {}", layer.layerName);
m_layers.push_back(VALIDATION_LAYER.data());
return;
}
}
@@ -1104,14 +1104,15 @@ void VulkanRenderDriver::_init_hardware() {
VkPhysicalDeviceProperties properties;
vkGetPhysicalDeviceProperties(device, &properties);
m_devices.emplace_back();
m_devices.back().name = properties.deviceName;
m_devices.back().vendor = static_cast<RenderDevice::Vendor>(properties.vendorID);
m_devices.back().type = static_cast<RenderDevice::Type>(properties.deviceType);
m_devices.back().deviceID = properties.deviceID;
m_devices.back().handle = device;
m_devices.emplace_back<RenderDevice>({
.name = properties.deviceName,
.vendor = static_cast<DeviceVendor>(properties.vendorID),
.type = static_cast<DeviceType>(properties.deviceType),
.deviceID = properties.deviceID,
.handle = device,
});
NOVA_LOG("Found device: {}", properties.deviceName);
NOVA_INFO("Found device: {}", properties.deviceName);
}
if (m_devices.empty()) {
@@ -1135,7 +1136,7 @@ void VulkanRenderDriver::_check_device_extensions() {
// Check found extensions
for (const auto& extension : available) {
if (auto it = requested.find(extension.extensionName); it != requested.end()) {
NOVA_LOG("Using device extension: {}", extension.extensionName);
NOVA_INFO("Using device extension: {}", extension.extensionName);
m_device_extensions.push_back(it->first.data());
requested.erase(it);
}
@@ -1192,7 +1193,7 @@ void VulkanRenderDriver::_init_queues(std::vector<VkDeviceQueueCreateInfo>& p_qu
continue;
}
NOVA_LOG("Using queue family: {}", i);
NOVA_INFO("Using queue family: {}", i);
found |= available[i].queueFlags;
VkDeviceQueueCreateInfo create {};

View File

@@ -9,78 +9,30 @@
#ifdef NOVA_VULKAN
#include <nova/render/render_driver.h>
#include <vulkan/vulkan.h>
#include <unordered_map>
#include <vector>
namespace Nova {
struct CommandBuffer {
VkCommandBuffer handle = VK_NULL_HANDLE;
};
struct CommandPool {
VkCommandPool handle = VK_NULL_HANDLE;
std::vector<CommandBufferID> allocated_buffers;
};
struct Pipeline {
PipelineType type;
VkPipeline handle = VK_NULL_HANDLE;
VkPipelineLayout layout = VK_NULL_HANDLE;
};
struct Queue {
VkQueue handle = VK_NULL_HANDLE;
u32 family_index;
u32 queue_index;
u32 usage_count = 0;
};
struct RenderPass {
VkRenderPass handle = VK_NULL_HANDLE;
};
struct Shader {
VkShaderModule handle = VK_NULL_HANDLE;
ShaderStage stage = ShaderStage::VERTEX;
std::string name;
};
struct Surface {
VkSurfaceKHR handle = VK_NULL_HANDLE;
u32 width = 0;
u32 height = 0;
bool dirty = false; // TODO: Use state enum
};
struct Swapchain {
VkSwapchainKHR handle = VK_NULL_HANDLE;
VkFormat format = VK_FORMAT_UNDEFINED;
VkColorSpaceKHR color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
std::vector<VkImage> images;
std::vector<VkImageView> image_views;
std::vector<VkFramebuffer> framebuffers;
SurfaceID surface = nullptr;
RenderPassID render_pass = nullptr;
};
class VulkanRenderDriver final : public RenderDriver {
public:
explicit VulkanRenderDriver(WindowDriver* window_driver);
~VulkanRenderDriver() override;
[[nodiscard]] RenderAPI get_api() const override;
[[nodiscard]] u32 get_api_version() const override;
[[nodiscard]] std::string get_api_name() const override;
[[nodiscard]] std::string get_api_version_string() const override;
RenderAPI get_api() const override;
u32 get_api_version() const override;
std::string get_api_name() const override;
std::string get_api_version_string() const override;
[[nodiscard]] u32 get_device_count() const override;
[[nodiscard]] const RenderDevice& get_device(u32 index) const override;
[[nodiscard]] bool get_device_supports_surface(u32 index, SurfaceID surface) const override;
u32 get_device_count() const override;
const RenderDevice& get_device(u32 index) const override;
bool get_device_supports_surface(u32 index, SurfaceID surface) const override;
void select_device(u32 index) override;
[[nodiscard]] u32 choose_queue_family(QueueType type, SurfaceID surface) override;
u32 choose_queue_family(QueueType type, SurfaceID surface) override;
[[nodiscard]] QueueID get_queue(u32 queue_family) override;
void free_queue(QueueID queue) override;
@@ -89,7 +41,7 @@ namespace Nova {
[[nodiscard]] SwapchainID create_swapchain(SurfaceID surface) override;
void resize_swapchain(SwapchainID swapchain) override;
[[nodiscard]] RenderPassID get_swapchain_render_pass(SwapchainID swapchain) const override;
RenderPassID get_swapchain_render_pass(SwapchainID swapchain) const override;
void destroy_swapchain(SwapchainID swapchain) override;
[[nodiscard]] ShaderID create_shader(const std::span<u8> bytes, ShaderStage stage) override;
@@ -109,8 +61,8 @@ namespace Nova {
void begin_command_buffer(CommandBufferID command_buffer) override;
void end_command_buffer(CommandBufferID command_buffer) override;
[[nodiscard]] VkInstance get_instance() const;
[[nodiscard]] VkAllocationCallbacks* get_allocator(VkObjectType type) const;
VkInstance get_instance() const;
VkAllocationCallbacks* get_allocator(VkObjectType type) const;
private:
WindowDriver* m_window_driver = nullptr;

View File

@@ -0,0 +1,70 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
/// NOTE: This header should only be included in implementation files
#include <nova/render/render_driver.h>
#include <nova/render/render_structs.h>
#include <nova/types.h>
#include <vulkan/vulkan.h>
#include <string>
#include <vector>
namespace Nova {
struct CommandBuffer {
VkCommandBuffer handle = VK_NULL_HANDLE;
};
struct CommandPool {
VkCommandPool handle = VK_NULL_HANDLE;
std::vector<CommandBufferID> allocated_buffers;
};
struct Pipeline {
PipelineType type;
VkPipeline handle = VK_NULL_HANDLE;
VkPipelineLayout layout = VK_NULL_HANDLE;
};
struct Queue {
VkQueue handle = VK_NULL_HANDLE;
u32 family_index;
u32 queue_index;
u32 usage_count = 0;
};
struct RenderPass {
VkRenderPass handle = VK_NULL_HANDLE;
};
struct Shader {
VkShaderModule handle = VK_NULL_HANDLE;
ShaderStage stage = ShaderStage::VERTEX;
std::string name;
};
struct Surface {
VkSurfaceKHR handle = VK_NULL_HANDLE;
u32 width = 0;
u32 height = 0;
bool dirty = false; // TODO: Use state enum
};
struct Swapchain {
VkSwapchainKHR handle = VK_NULL_HANDLE;
VkFormat format = VK_FORMAT_UNDEFINED;
VkColorSpaceKHR color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
std::vector<VkImage> images;
std::vector<VkImageView> image_views;
std::vector<VkFramebuffer> framebuffers;
SurfaceID surface = nullptr;
RenderPassID render_pass = nullptr;
};
} // namespace Nova

View File

@@ -1,36 +0,0 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifdef NOVA_WINDOWS
#include "drivers/win32/window_driver.h"
#ifdef NOVA_VULKAN
#include <vulkan/vulkan.h>
#include <vulkan/vulkan_win32.h>
#endif
#include <nova/core/debug.h>
using namespace Nova;
Win32WindowDriver::Win32WindowDriver() {
NOVA_AUTO_TRACE();
}
Win32WindowDriver::~Win32WindowDriver() {
NOVA_AUTO_TRACE();
}
const char* Win32WindowDriver::get_surface_extension() const {
#ifdef NOVA_VULKAN
return VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
#else
return nullptr;
#endif
}
#endif // NOVA_WINDOWS

View File

@@ -1,24 +0,0 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
#ifdef NOVA_WINDOWS
#include <nova/platform/window_driver.h>
#include <windows.h>
namespace Nova {
class Win32WindowDriver final : public WindowDriver {
public:
Win32WindowDriver();
~Win32WindowDriver() override;
[[nodiscard]] const char* get_surface_extension() const override;
};
} // namespace Nova
#endif // NOVA_WINDOWS

View File

@@ -6,7 +6,7 @@
#ifdef NOVA_WAYLAND
#include "drivers/wayland/window_driver.h"
#include "platform/linux/wayland/window_driver.h"
#ifdef NOVA_VULKAN
#include <vulkan/vulkan.h>

View File

@@ -6,10 +6,14 @@
#ifdef NOVA_X11
#include "drivers/x11/window_driver.h"
#include "platform/linux/x11/window_driver.h"
#include "platform/linux/x11/window_structs.h"
#include "platform/linux/x11/wrapper.h"
#ifdef NOVA_VULKAN
#include "drivers/vulkan/render_driver.h"
#include "drivers/vulkan/render_structs.h"
#include <vulkan/vulkan.h>
#include <vulkan/vulkan_xlib.h>
@@ -104,7 +108,7 @@ u32 X11WindowDriver::get_window_count() const {
return static_cast<u32>(m_windows.size());
}
WindowID X11WindowDriver::create_window(const std::string_view p_title, const u32 p_width, const u32 p_height) {
WindowID X11WindowDriver::create_window(const std::string& p_title, const u32 p_width, const u32 p_height) {
NOVA_AUTO_TRACE();
X11::Window handle = XCreateSimpleWindow(m_display, DefaultRootWindow(m_display), 0, 0, p_width, p_height, 0, 0, 0);
@@ -116,7 +120,7 @@ WindowID X11WindowDriver::create_window(const std::string_view p_title, const u3
XSetWMProtocols(m_display, handle, &m_window_close_atom, 1);
XSelectInput(m_display, handle, StructureNotifyMask);
XStoreName(m_display, handle, p_title.data());
XStoreName(m_display, handle, p_title.c_str());
XMapWindow(m_display, handle);
XFlush(m_display);
@@ -131,10 +135,10 @@ void X11WindowDriver::destroy_window(WindowID p_window) {
m_windows.erase(p_window->handle);
}
void X11WindowDriver::set_window_title(WindowID p_window, const std::string_view p_title) {
void X11WindowDriver::set_window_title(WindowID p_window, const std::string& p_title) {
NOVA_AUTO_TRACE();
NOVA_ASSERT(p_window);
XStoreName(m_display, p_window->handle, p_title.data());
XStoreName(m_display, p_window->handle, p_title.c_str());
}
void X11WindowDriver::set_window_size(WindowID p_window, const u32 p_width, const u32 p_height) {

View File

@@ -8,44 +8,33 @@
#ifdef NOVA_X11
#include <X11/Xlib.h>
#include "platform/linux/x11/wrapper.h"
#include <nova/platform/window_driver.h>
#include <unordered_map>
namespace X11 {
using Window = ::Window;
using Display = ::Display;
using Atom = ::Atom;
} // namespace X11
namespace Nova {
struct Window {
X11::Window handle = 0;
int width = 0;
int height = 0;
};
class X11WindowDriver final : public WindowDriver {
public:
X11WindowDriver();
~X11WindowDriver() override;
[[nodiscard]] WindowAPI get_api() const override;
[[nodiscard]] std::string get_api_name() const override;
WindowAPI get_api() const override;
std::string get_api_name() const override;
void poll_events() override;
void beep() override;
[[nodiscard]] u32 get_window_count() const override;
[[nodiscard]] WindowID create_window(std::string_view title, u32 width, u32 height) override;
u32 get_window_count() const override;
[[nodiscard]] WindowID create_window(const std::string& title, u32 width, u32 height) override;
void destroy_window(WindowID window) override;
void set_window_title(WindowID window, std::string_view title) override;
void set_window_title(WindowID window, const std::string& title) override;
void set_window_size(WindowID window, u32 width, u32 height) override;
void set_window_position(WindowID window, i32 x, i32 y) override;
[[nodiscard]] const char* get_surface_extension() const override;
const char* get_surface_extension() const override;
[[nodiscard]] SurfaceID create_surface(WindowID window, RenderDriver* driver) override;
private:

View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
/// NOTE: This header should only be included in implementation files
#include "platform/linux/x11/wrapper.h"
#include <nova/platform/platform_structs.h>
namespace Nova {
struct Window {
X11::Window handle = 0;
int width = 0;
int height = 0;
};
} // namespace Nova

View File

@@ -0,0 +1,15 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
#include <X11/Xlib.h>
namespace X11 {
using Window = ::Window;
using Display = ::Display;
using Atom = ::Atom;
} // namespace X11

View File

@@ -4,9 +4,9 @@
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "drivers/wayland/window_driver.h" // IWYU pragma: keep
#include "drivers/win32/window_driver.h" // IWYU pragma: keep
#include "drivers/x11/window_driver.h" // IWYU pragma: keep
#include "platform/linux/wayland/window_driver.h" // IWYU pragma: keep
#include "platform/linux/x11/window_driver.h" // IWYU pragma: keep
#include "platform/windows/window_driver.h" // IWYU pragma: keep
#include <nova/core/debug.h>
#include <nova/platform/window_driver.h>

View File

@@ -0,0 +1,222 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifdef NOVA_WINDOWS
#include "platform/windows/window_driver.h"
#include "platform/windows/window_structs.h"
#ifdef NOVA_VULKAN
#include "drivers/vulkan/render_driver.h"
#include "drivers/vulkan/render_structs.h"
#include <vulkan/vulkan.h>
#include <vulkan/vulkan_win32.h>
#endif
#include <nova/core/debug.h>
#include <nova/render/render_driver.h>
namespace {
static constexpr LPCSTR WINDOW_CLASS_NAME = "NOVA_WindowClass";
}
using namespace Nova;
Win32WindowDriver::Win32WindowDriver() {
NOVA_AUTO_TRACE();
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = _window_proc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = WINDOW_CLASS_NAME;
if (!RegisterClassEx(&wc)) {
throw std::runtime_error("Failed to register window class");
}
}
Win32WindowDriver::~Win32WindowDriver() {
NOVA_AUTO_TRACE();
UnregisterClass(WINDOW_CLASS_NAME, GetModuleHandle(nullptr));
}
WindowAPI Win32WindowDriver::get_api() const {
return WindowAPI::WINDOWS;
}
std::string Win32WindowDriver::get_api_name() const {
return "Win32";
}
void Win32WindowDriver::poll_events() {
MSG msg;
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void Win32WindowDriver::beep() {
MessageBeep(MB_OK);
}
u32 Win32WindowDriver::get_window_count() const {
return static_cast<u32>(m_windows.size());
}
WindowID Win32WindowDriver::create_window(const std::string& p_title, u32 p_width, u32 p_height) {
NOVA_AUTO_TRACE();
RECT rect = {0, 0, static_cast<LONG>(p_width), static_cast<LONG>(p_height)};
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
HWND handle = CreateWindowEx(
0,
WINDOW_CLASS_NAME,
p_title.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
rect.right - rect.left,
rect.bottom - rect.top,
nullptr,
nullptr,
GetModuleHandle(nullptr),
this
);
Window* window = new Window();
window->width = p_width;
window->height = p_height;
window->handle = handle;
ShowWindow(handle, SW_SHOW);
UpdateWindow(handle);
m_windows[handle] = window;
return window;
}
void Win32WindowDriver::destroy_window(WindowID p_window) {
NOVA_AUTO_TRACE();
NOVA_ASSERT(p_window);
DestroyWindow(p_window->handle);
m_windows.erase(p_window->handle);
}
void Win32WindowDriver::set_window_title(WindowID p_window, const std::string& p_title) {
NOVA_AUTO_TRACE();
NOVA_ASSERT(p_window);
SetWindowText(p_window->handle, p_title.c_str());
}
void Win32WindowDriver::set_window_size(WindowID p_window, u32 p_width, u32 p_height) {
NOVA_AUTO_TRACE();
NOVA_ASSERT(p_window);
RECT rect = {0, 0, static_cast<LONG>(p_width), static_cast<LONG>(p_height)};
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
SetWindowPos(p_window->handle, nullptr, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
}
void Win32WindowDriver::set_window_position(WindowID p_window, i32 p_x, i32 p_y) {
NOVA_AUTO_TRACE();
NOVA_ASSERT(p_window);
SetWindowPos(p_window->handle, nullptr, p_x, p_y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
const char* Win32WindowDriver::get_surface_extension() const {
#ifdef NOVA_VULKAN
return VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
#else
return nullptr;
#endif
}
SurfaceID Win32WindowDriver::create_surface(WindowID p_window, RenderDriver* p_driver) {
NOVA_AUTO_TRACE();
NOVA_ASSERT(p_window);
NOVA_ASSERT(p_driver);
NOVA_ASSERT(p_driver->get_api() == RenderAPI::VULKAN);
#ifdef NOVA_VULKAN
VkWin32SurfaceCreateInfoKHR create {};
create.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
create.hinstance = GetModuleHandle(nullptr);
create.hwnd = p_window->handle;
const auto vkrd = static_cast<VulkanRenderDriver*>(p_driver);
Surface* surface = new Surface();
if (vkCreateWin32SurfaceKHR(vkrd->get_instance(), &create, vkrd->get_allocator(VK_OBJECT_TYPE_SURFACE_KHR), &surface->handle)
!= VK_SUCCESS) {
throw std::runtime_error("Failed to create Vulkan surface");
}
return surface;
#else
return nullptr;
#endif
}
LRESULT Win32WindowDriver::_handle_message(HWND p_handle, UINT p_msg, WPARAM p_wparam, LPARAM p_lparam) {
auto iter = m_windows.find(p_handle);
if (iter == m_windows.end()) {
return DefWindowProc(p_handle, p_msg, p_wparam, p_lparam);
}
WindowID window = iter->second;
switch (p_msg) {
case WM_DESTROY: {
NOVA_DEBUG("Window event: DESTROYED");
if (m_windows.empty()) {
PostQuitMessage(0);
}
return 0;
}
case WM_SIZE: {
int width = LOWORD(p_lparam);
int height = HIWORD(p_lparam);
if (width != window->width || height != window->height) {
window->width = width;
window->height = height;
NOVA_DEBUG("Window event: RESIZED ({}x{})", width, height);
}
return 0;
}
case WM_CLOSE: {
NOVA_DEBUG("Window event: CLOSED");
destroy_window(window);
return 0;
}
default:
return DefWindowProc(p_handle, p_msg, p_wparam, p_lparam);
}
}
LRESULT CALLBACK Win32WindowDriver::_window_proc(HWND p_handle, UINT p_msg, WPARAM p_wparam, LPARAM p_lparam) {
Win32WindowDriver* driver = nullptr;
if (p_msg == WM_NCCREATE) {
CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(p_lparam);
driver = static_cast<Win32WindowDriver*>(cs->lpCreateParams);
SetWindowLongPtr(p_handle, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(driver));
} else {
driver = reinterpret_cast<Win32WindowDriver*>(GetWindowLongPtr(p_handle, GWLP_USERDATA));
}
if (driver) {
return driver->_handle_message(p_handle, p_msg, p_wparam, p_lparam);
}
return DefWindowProc(p_handle, p_msg, p_wparam, p_lparam);
}
#endif // NOVA_WINDOWS

View File

@@ -0,0 +1,48 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
#ifdef NOVA_WINDOWS
#include <nova/platform/window_driver.h>
#include <windows.h>
#include <unordered_map>
namespace Nova {
class Win32WindowDriver final : public WindowDriver {
public:
Win32WindowDriver();
~Win32WindowDriver() override;
WindowAPI get_api() const override;
std::string get_api_name() const override;
void poll_events() override;
void beep() override;
u32 get_window_count() const override;
[[nodiscard]] WindowID create_window(const std::string& title, u32 width, u32 height) override;
void destroy_window(WindowID window) override;
void set_window_title(WindowID window, const std::string& title) override;
void set_window_size(WindowID window, u32 width, u32 height) override;
void set_window_position(WindowID window, i32 x, i32 y) override;
[[nodiscard]] const char* get_surface_extension() const override;
[[nodiscard]] SurfaceID create_surface(WindowID window, RenderDriver* p_driver) override;
private:
std::unordered_map<HWND, WindowID> m_windows;
LRESULT _handle_message(HWND handle, UINT msg, WPARAM wparam, LPARAM lparam);
static LRESULT CALLBACK _window_proc(HWND handle, UINT msg, WPARAM wparam, LPARAM lparam);
};
} // namespace Nova
#endif // NOVA_WINDOWS

View File

@@ -0,0 +1,20 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
/// NOTE: This header should only be included in implementation files
#include <nova/platform/platform_structs.h>
#include <windows.h>
namespace Nova {
struct Window {
HWND handle = {};
int width = 0;
int height = 0;
};
} // namespace Nova

View File

@@ -8,6 +8,7 @@
#include <nova/render/render_device.h>
#include <nova/render/render_driver.h>
#include <algorithm>
#include <limits>
using namespace Nova;
@@ -18,31 +19,29 @@ u32 RenderDevice::choose_device(RenderDriver* p_driver, std::span<const SurfaceI
u32 best_index = std::numeric_limits<u32>::max();
u32 best_score = 0;
const bool prefer_discrete = true; // TODO: Get from config
for (u32 i = 0; i < p_driver->get_device_count(); i++) {
auto& device = p_driver->get_device(i);
u32 score = 1;
for (SurfaceID surface : p_surfaces) {
if (!p_driver->get_device_supports_surface(i, surface)) {
score = 0;
break;
}
}
if (score == 0) {
if (!std::all_of(p_surfaces.begin(), p_surfaces.end(), [&](SurfaceID surface) {
return p_driver->get_device_supports_surface(i, surface);
})) {
continue;
}
switch (device.type) {
case Type::DISCRETE:
score += 4;
case DeviceType::DISCRETE:
score += prefer_discrete ? 4 : 3;
break;
case Type::INTEGRATED:
score += 3;
case DeviceType::INTEGRATED:
score += prefer_discrete ? 3 : 4;
break;
case Type::VIRTUAL:
case DeviceType::VIRTUAL:
score += 2;
break;
case Type::CPU:
case DeviceType::CPU:
score += 1;
break;
default:

View File

@@ -1,32 +0,0 @@
/**
* Copyright (c) 2025, Jayden Grubb <contact@jaydengrubb.com>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <nova/core/debug.h>
#include <nova/render/render_driver.h>
#include <nova/render/renderer.h>
#include <memory>
using namespace Nova;
static std::unique_ptr<RenderDriver> s_driver;
void Renderer::init(const RenderAPI p_api) {
NOVA_AUTO_TRACE();
NOVA_ASSERT(!s_driver);
s_driver = std::unique_ptr<RenderDriver>(RenderDriver::create(p_api, nullptr));
}
void Renderer::shutdown() {
NOVA_AUTO_TRACE();
s_driver.reset();
}
RenderDriver* Renderer::get_driver() {
NOVA_ASSERT(s_driver);
return s_driver.get();
}