From f5ee4a13bfeaa99f075b17d0b63715d3c18c3ea2 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Sun, 19 Jun 2022 04:33:41 +0100 Subject: [PATCH] Get some basic stuff compiling --- include/Orange/Core/AlignedStorage.h | 2 +- include/Orange/Core/Result.h | 6 +- include/Orange/Core/Traits.h | 7 ++- include/Orange/Render/ForwardRenderer.h | 28 ---------- include/Orange/Render/Renderer.h | 66 ++++++---------------- include/Orange/Render/Window.h | 23 ++++++++ src/Apps/Tools/BSPViewer.cpp | 3 +- src/Apps/Tools/CubeTest.cpp | 19 +++++++ src/Apps/Tools/meson.build | 10 +++- src/Core/meson.build | 0 src/Orange/Render/Instance.cpp | 74 +++++++++++++++++++++++++ src/Orange/Render/Window.cpp | 37 +++++++++++++ src/Orange/meson.build | 13 +++++ src/meson.build | 2 +- 14 files changed, 204 insertions(+), 86 deletions(-) delete mode 100644 include/Orange/Render/ForwardRenderer.h create mode 100644 include/Orange/Render/Window.h create mode 100644 src/Apps/Tools/CubeTest.cpp delete mode 100644 src/Core/meson.build create mode 100644 src/Orange/Render/Instance.cpp create mode 100644 src/Orange/Render/Window.cpp create mode 100644 src/Orange/meson.build diff --git a/include/Orange/Core/AlignedStorage.h b/include/Orange/Core/AlignedStorage.h index 567afd0..7911118 100644 --- a/include/Orange/Core/AlignedStorage.h +++ b/include/Orange/Core/AlignedStorage.h @@ -7,6 +7,6 @@ namespace orange template struct AlignedStorage { - alignas(Alignment) unsigned char data[Length]; + alignas(Alignment) uint8_t data[Length]; }; } diff --git a/include/Orange/Core/Result.h b/include/Orange/Core/Result.h index 4e6fcab..0533db3 100644 --- a/include/Orange/Core/Result.h +++ b/include/Orange/Core/Result.h @@ -53,12 +53,12 @@ namespace orange Result() = default; template - T& Create(Args&&... args) + Result& Create(Args&&... args) { Assert(!m_created); m_created = true; - new(&m_data) T(Forward(args)...); - return RefUnsafe(); + new(reinterpret_cast(&m_data)) T{ Forward(args)... }; + return *this; } void Destroy() diff --git a/include/Orange/Core/Traits.h b/include/Orange/Core/Traits.h index 473cfb5..012039a 100644 --- a/include/Orange/Core/Traits.h +++ b/include/Orange/Core/Traits.h @@ -1,5 +1,10 @@ #pragma once +#include + +// Needed for placement new/delete. +#include + namespace orange { struct FalseType { static constexpr bool Value = false; }; @@ -30,4 +35,4 @@ namespace orange { return static_cast&&>(arg); } -} \ No newline at end of file +} diff --git a/include/Orange/Render/ForwardRenderer.h b/include/Orange/Render/ForwardRenderer.h deleted file mode 100644 index 2627af6..0000000 --- a/include/Orange/Render/ForwardRenderer.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include -#include - -namespace orange -{ - class ForwardRenderer : public Renderer - { - public: - ~ForwardRenderer() = default; - - static Result create(SDL_Window* window, const char* name) - { - Result result; - - ForwardRenderer& renderer = result.Create(); - - if ((renderer.m_instance = createInstance(window, name)) == VK_NULL_HANDLE) - return result.Error(BasicErrorCode::Failed); - - return result.Success(); - } - protected: - friend Result; - ForwardRenderer() = default; - }; -} \ No newline at end of file diff --git a/include/Orange/Render/Renderer.h b/include/Orange/Render/Renderer.h index 8ab9166..54259dd 100644 --- a/include/Orange/Render/Renderer.h +++ b/include/Orange/Render/Renderer.h @@ -1,14 +1,28 @@ #pragma once #include +#include #include -#include -#include #include namespace orange { + class Window; + + class Instance + { + public: + ~Instance(); + + static Result Create(Window& window, const char* name); + protected: + friend Result; + Instance(VkInstance instance); + private: + VkInstance m_instance = VK_NULL_HANDLE; + }; + class Renderer { @@ -16,53 +30,9 @@ namespace orange ~Renderer() = default; protected: + friend Result; Renderer() = default; - - static VkInstance createInstance(SDL_Window* window, const char* name) - { - uint32_t instanceExtensionCount = {}; - if (SDL_Vulkan_GetInstanceExtensions(window, &instanceExtensionCount, nullptr) != SDL_TRUE) - { - //std::cerr << "Failed to get SDL instance extension count.\n"; - return VK_NULL_HANDLE; - } - - SmallVector instanceExtensions{ instanceExtensionCount }; - if (SDL_Vulkan_GetInstanceExtensions(window, &instanceExtensionCount, instanceExtensions.Data()) != SDL_TRUE) - { - //std::cerr << "Failed to get SDL instance extensions.\n"; - return VK_NULL_HANDLE; - } - - VkApplicationInfo appInfo = - { - .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, - .pApplicationName = name, - .applicationVersion = VK_MAKE_VERSION(1, 0, 0), - .pEngineName = "Orange", - .engineVersion = VK_MAKE_VERSION(1, 0, 0), - .apiVersion = VK_API_VERSION_1_3, - }; - - VkInstanceCreateInfo instanceInfo = - { - .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, - .pApplicationInfo = &appInfo, - .enabledExtensionCount = instanceExtensionCount, - .ppEnabledExtensionNames = instanceExtensions.Data(), - }; - - VkResult result = VK_SUCCESS; - VkInstance instance; - if ((result = vkCreateInstance(&instanceInfo, nullptr, &instance)) != VK_SUCCESS) - { - //std::cerr << "Failed to create Vulkan instance.\n"; - return VK_NULL_HANDLE; - } - - return instance; - } - + private: VkInstance m_instance = VK_NULL_HANDLE; VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE; VkDevice m_device = VK_NULL_HANDLE; diff --git a/include/Orange/Render/Window.h b/include/Orange/Render/Window.h new file mode 100644 index 0000000..60cc528 --- /dev/null +++ b/include/Orange/Render/Window.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +struct SDL_Window; + +namespace orange +{ + class Window + { + public: + ~Window(); + + bool GetInstanceExtensions(uint32_t *count, const char **extensions); + + static Result Create(); + protected: + friend Result; + Window(SDL_Window* window); + private: + SDL_Window* m_window = nullptr; + }; +} diff --git a/src/Apps/Tools/BSPViewer.cpp b/src/Apps/Tools/BSPViewer.cpp index 0b3f3fd..080a814 100644 --- a/src/Apps/Tools/BSPViewer.cpp +++ b/src/Apps/Tools/BSPViewer.cpp @@ -1,7 +1,6 @@ #include -#include int main(int argc, char** argv) { - + (void)argc; (void)argv; } diff --git a/src/Apps/Tools/CubeTest.cpp b/src/Apps/Tools/CubeTest.cpp new file mode 100644 index 0000000..2f45f96 --- /dev/null +++ b/src/Apps/Tools/CubeTest.cpp @@ -0,0 +1,19 @@ +#include + +#include +#include + +using namespace orange; + +int main(int argc, char** argv) +{ + (void)argc; (void)argv; + + Result r_window = Window::Create(); + if (!r_window) + return 1; + + Window& window = *r_window; + + return 0; +} diff --git a/src/Apps/Tools/meson.build b/src/Apps/Tools/meson.build index 662667b..878d71f 100644 --- a/src/Apps/Tools/meson.build +++ b/src/Apps/Tools/meson.build @@ -1,3 +1,9 @@ +executable('CubeTest', 'CubeTest.cpp', + dependencies : [ sdl2_dep, vulkan_dep, orange_dep ], + include_directories : [ orange_include ] +) + executable('BSPViewer', 'BSPViewer.cpp', - dependencies : [sdl2_dep, vulkan_dep], - include_directories : [orange_include]) + dependencies : [ sdl2_dep, vulkan_dep, orange_dep ], + include_directories : [ orange_include ] +) diff --git a/src/Core/meson.build b/src/Core/meson.build deleted file mode 100644 index e69de29..0000000 diff --git a/src/Orange/Render/Instance.cpp b/src/Orange/Render/Instance.cpp new file mode 100644 index 0000000..a569c25 --- /dev/null +++ b/src/Orange/Render/Instance.cpp @@ -0,0 +1,74 @@ +#include +#include + +namespace orange +{ + static VkInstance CreateVkInstance(Window& window, const char* name) + { +#if 0 + uint32_t instanceExtensionCount = {}; + if (!window.GetInstanceExtensions(&instanceExtensionCount, nullptr)) + { + //std::cerr << "Failed to get SDL instance extension count.\n"; + return VK_NULL_HANDLE; + } + + SmallVector instanceExtensions{ instanceExtensionCount }; + if (!window.GetInstanceExtensions(&instanceExtensionCount, instanceExtensions.Data())) + { + //std::cerr << "Failed to get SDL instance extensions.\n"; + return VK_NULL_HANDLE; + } + + VkApplicationInfo appInfo = + { + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .pApplicationName = name, + .applicationVersion = VK_MAKE_VERSION(1, 0, 0), + .pEngineName = "Orange", + .engineVersion = VK_MAKE_VERSION(1, 0, 0), + .apiVersion = VK_API_VERSION_1_3, + }; + + VkInstanceCreateInfo instanceInfo = + { + .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + .pApplicationInfo = &appInfo, + .enabledExtensionCount = instanceExtensionCount, + .ppEnabledExtensionNames = instanceExtensions.Data(), + }; + + VkResult result = VK_SUCCESS; + VkInstance instance; + if ((result = vkCreateInstance(&instanceInfo, nullptr, &instance)) != VK_SUCCESS) + { + //std::cerr << "Failed to create Vulkan instance.\n"; + return VK_NULL_HANDLE; + } + + return instance; +#else + return VK_NULL_HANDLE; +#endif + } + + Result Instance::Create(Window& window, const char* name) + { + Result result; + + if (VkInstance instance = CreateVkInstance(window, name)) + return result.Create(instance).Success(); + + return result.Error(BasicErrorCode::Failed); + } + + Instance::Instance(VkInstance instance) + : m_instance{ instance } + {} + + Instance::~Instance() + { + vkDestroyInstance(m_instance, nullptr); + } + +} diff --git a/src/Orange/Render/Window.cpp b/src/Orange/Render/Window.cpp new file mode 100644 index 0000000..26b6244 --- /dev/null +++ b/src/Orange/Render/Window.cpp @@ -0,0 +1,37 @@ +#include "Orange/Core/Result.h" +#include + +#include +#include + +namespace orange +{ + Result Window::Create() + { + Result result; + + if (!SDL_InitSubSystem(SDL_INIT_VIDEO)) + return result.Error(BasicErrorCode::Failed); + + if (SDL_Window* window = SDL_CreateWindow("Orange", 0, 0, 1280, 720, SDL_WINDOW_VULKAN)) + return result.Create(window).Success(); + + return result.Error(BasicErrorCode::Failed); + } + + Window::Window(SDL_Window* window) + : m_window{ window } + {} + + Window::~Window() + { + SDL_DestroyWindow(m_window); + + SDL_QuitSubSystem(SDL_INIT_VIDEO); + } + + bool Window::GetInstanceExtensions(uint32_t *count, const char **extensions) + { + return SDL_Vulkan_GetInstanceExtensions(m_window, count, extensions) == SDL_TRUE; + } +} diff --git a/src/Orange/meson.build b/src/Orange/meson.build new file mode 100644 index 0000000..b805e7c --- /dev/null +++ b/src/Orange/meson.build @@ -0,0 +1,13 @@ +orange_src = [ + 'Render/Instance.cpp', + 'Render/Window.cpp', +] + +orange_lib = static_library('orange', orange_src, + include_directories : [ orange_include ] +) + +orange_dep = declare_dependency( + link_with : [ orange_lib ], + include_directories : [ orange_include ] +) \ No newline at end of file diff --git a/src/meson.build b/src/meson.build index c63ab69..5eca29a 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,2 +1,2 @@ -subdir('Core') +subdir('Orange') subdir('Apps') \ No newline at end of file