From ad69293faabc9b84e48f67b9a1e06e9a73e9d6ea Mon Sep 17 00:00:00 2001 From: Thraix Date: Mon, 1 May 2023 18:23:22 +0200 Subject: [PATCH] Add printable Enum --- CopiumEngine/CopiumEngine.vcxproj | 1 + .../src/copium/buffer/CommandBuffer.cpp | 14 ++--- .../src/copium/buffer/CommandBuffer.h | 13 +++-- .../src/copium/buffer/CommandBufferScoped.cpp | 2 +- CopiumEngine/src/copium/core/Application.cpp | 2 +- CopiumEngine/src/copium/core/Vulkan.cpp | 2 +- CopiumEngine/src/copium/core/Window.cpp | 12 ++--- CopiumEngine/src/copium/core/Window.h | 13 +++-- .../src/copium/event/EventDispatcher.cpp | 2 +- CopiumEngine/src/copium/event/EventResult.h | 12 +++-- CopiumEngine/src/copium/event/EventType.h | 17 +++--- CopiumEngine/src/copium/pipeline/Pipeline.cpp | 2 +- CopiumEngine/src/copium/pipeline/Shader.cpp | 12 ++--- CopiumEngine/src/copium/pipeline/Shader.h | 12 ++--- .../src/copium/pipeline/ShaderBinding.cpp | 4 +- .../src/copium/pipeline/ShaderBinding.h | 26 ++++------ CopiumEngine/src/copium/util/Enum.h | 52 +++++++++++++++++++ 17 files changed, 122 insertions(+), 76 deletions(-) create mode 100644 CopiumEngine/src/copium/util/Enum.h diff --git a/CopiumEngine/CopiumEngine.vcxproj b/CopiumEngine/CopiumEngine.vcxproj index 0429967..45514e7 100644 --- a/CopiumEngine/CopiumEngine.vcxproj +++ b/CopiumEngine/CopiumEngine.vcxproj @@ -258,6 +258,7 @@ + diff --git a/CopiumEngine/src/copium/buffer/CommandBuffer.cpp b/CopiumEngine/src/copium/buffer/CommandBuffer.cpp index 20e0b43..49417ab 100644 --- a/CopiumEngine/src/copium/buffer/CommandBuffer.cpp +++ b/CopiumEngine/src/copium/buffer/CommandBuffer.cpp @@ -4,19 +4,19 @@ namespace Copium { - CommandBuffer::CommandBuffer(Type type) + CommandBuffer::CommandBuffer(CommandBufferType type) : type{type} { switch (type) { - case Type::SingleUse: + case CommandBufferType::SingleUse: commandBuffers.resize(1); break; - case Type::Dynamic: + case CommandBufferType::Dynamic: commandBuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT); break; default: - CP_ABORT("Unreachable switch case"); + CP_ABORT("Unreachable switch case: %s", ToString(type).c_str()); } VkCommandBufferAllocateInfo allocateInfo{}; @@ -42,13 +42,13 @@ namespace Copium switch (type) { - case Type::SingleUse: + case CommandBufferType::SingleUse: beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; break; - case Type::Dynamic: + case CommandBufferType::Dynamic: break; default: - CP_ABORT("Unreachable switch case"); + CP_ABORT("Unreachable switch case: %s", ToString(type).c_str()); } vkResetCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], 0); diff --git a/CopiumEngine/src/copium/buffer/CommandBuffer.h b/CopiumEngine/src/copium/buffer/CommandBuffer.h index 907481c..56aa724 100644 --- a/CopiumEngine/src/copium/buffer/CommandBuffer.h +++ b/CopiumEngine/src/copium/buffer/CommandBuffer.h @@ -1,25 +1,24 @@ #pragma once #include "copium/util/Common.h" +#include "copium/util/Enum.h" #include +#define CP_COMMAND_BUFFER_TYPE_ENUMS SingleUse, Dynamic +CP_ENUM_CREATOR(Copium, CommandBufferType, CP_COMMAND_BUFFER_TYPE_ENUMS); + namespace Copium { class CommandBuffer { CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer); - public: - enum class Type - { - SingleUse, Dynamic - }; private: std::vector commandBuffers; - const Type type; + const CommandBufferType type; public: - CommandBuffer(Type type); + CommandBuffer(CommandBufferType type); virtual ~CommandBuffer(); void Begin(); diff --git a/CopiumEngine/src/copium/buffer/CommandBufferScoped.cpp b/CopiumEngine/src/copium/buffer/CommandBufferScoped.cpp index 023a749..6901eaf 100644 --- a/CopiumEngine/src/copium/buffer/CommandBufferScoped.cpp +++ b/CopiumEngine/src/copium/buffer/CommandBufferScoped.cpp @@ -3,7 +3,7 @@ namespace Copium { CommandBufferScoped::CommandBufferScoped() - : CommandBuffer{Type::SingleUse} + : CommandBuffer{CommandBufferType::SingleUse} { CommandBuffer::Begin(); } diff --git a/CopiumEngine/src/copium/core/Application.cpp b/CopiumEngine/src/copium/core/Application.cpp index a2756d2..dff5261 100644 --- a/CopiumEngine/src/copium/core/Application.cpp +++ b/CopiumEngine/src/copium/core/Application.cpp @@ -173,7 +173,7 @@ namespace Copium void Application::InitializeCommandBuffer() { - commandBuffer = std::make_unique(CommandBuffer::Type::Dynamic); + commandBuffer = std::make_unique(CommandBufferType::Dynamic); } void Application::RecordCommandBuffer() diff --git a/CopiumEngine/src/copium/core/Vulkan.cpp b/CopiumEngine/src/copium/core/Vulkan.cpp index 33a00e4..8a38023 100644 --- a/CopiumEngine/src/copium/core/Vulkan.cpp +++ b/CopiumEngine/src/copium/core/Vulkan.cpp @@ -16,7 +16,7 @@ namespace Copium void Vulkan::Initialize() { instance = std::make_unique("Copium Engine"); - window = std::make_unique("Copium Engine", 1920, 1080, Window::Mode::Windowed); + window = std::make_unique("Copium Engine", 1920, 1080, WindowMode::Windowed); device = std::make_unique(); swapChain = std::make_unique(); diff --git a/CopiumEngine/src/copium/core/Window.cpp b/CopiumEngine/src/copium/core/Window.cpp index 7405822..792dd27 100644 --- a/CopiumEngine/src/copium/core/Window.cpp +++ b/CopiumEngine/src/copium/core/Window.cpp @@ -13,7 +13,7 @@ namespace Copium { - Window::Window(const std::string& windowName, int width, int height, Mode mode) + Window::Window(const std::string& windowName, int width, int height, WindowMode mode) { InitializeWindow(windowName, width, height, mode); InitializeSurface(); @@ -35,20 +35,20 @@ namespace Copium return window; } - void Window::InitializeWindow(const std::string& windowName, int width, int height, Mode mode) + void Window::InitializeWindow(const std::string& windowName, int width, int height, WindowMode mode) { glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); switch (mode) { - case Mode::Fullscreen: + case WindowMode::Fullscreen: { GLFWmonitor* monitor = glfwGetPrimaryMonitor(); const GLFWvidmode* mode = glfwGetVideoMode(monitor); window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), glfwGetPrimaryMonitor(), nullptr); break; } - case Mode::BorderlessWindowed: + case WindowMode::BorderlessWindowed: { GLFWmonitor* monitor = glfwGetPrimaryMonitor(); const GLFWvidmode* mode = glfwGetVideoMode(monitor); @@ -56,13 +56,13 @@ namespace Copium glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); break; } - case Mode::Windowed: + case WindowMode::Windowed: { window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr); break; } default: - CP_ABORT("Unreachable switch case"); + CP_ABORT("Unreachable switch case: %s", ToString(mode).c_str()); } CP_ASSERT(window, "Failed to initialize glfw window"); diff --git a/CopiumEngine/src/copium/core/Window.h b/CopiumEngine/src/copium/core/Window.h index 25dd67a..4f7871a 100644 --- a/CopiumEngine/src/copium/core/Window.h +++ b/CopiumEngine/src/copium/core/Window.h @@ -1,32 +1,31 @@ #pragma once #include "copium/util/Common.h" +#include "copium/util/Enum.h" #include +#define CP_WINDOW_MODE_ENUMS Fullscreen, BorderlessWindowed, Windowed +CP_ENUM_CREATOR(Copium, WindowMode, CP_WINDOW_MODE_ENUMS); + namespace Copium { class Window final { CP_DELETE_COPY_AND_MOVE_CTOR(Window); - public: - enum class Mode - { - Fullscreen, BorderlessWindowed, Windowed - }; private: GLFWwindow* window; VkSurfaceKHR surface; public: - Window(const std::string& windowName, int width, int height, Mode mode); + Window(const std::string& windowName, int width, int height, WindowMode mode); ~Window(); VkSurfaceKHR GetSurface() const; GLFWwindow* GetWindow(); private: - void InitializeWindow(const std::string& windowName, int width, int height, Mode mode); + void InitializeWindow(const std::string& windowName, int width, int height, WindowMode mode); void InitializeSurface(); static void FramebufferResizeCallback(GLFWwindow* glfwWindow, int width, int height); diff --git a/CopiumEngine/src/copium/event/EventDispatcher.cpp b/CopiumEngine/src/copium/event/EventDispatcher.cpp index 7ad1246..77a6cc2 100644 --- a/CopiumEngine/src/copium/event/EventDispatcher.cpp +++ b/CopiumEngine/src/copium/event/EventDispatcher.cpp @@ -35,7 +35,7 @@ namespace Copium focusedEventHandler = eventHandler; return; default: - CP_ABORT("Unreachable switch case"); + CP_ABORT("Unreachable switch case: %s", ToString(result).c_str()); } } } diff --git a/CopiumEngine/src/copium/event/EventResult.h b/CopiumEngine/src/copium/event/EventResult.h index d02e142..928e862 100644 --- a/CopiumEngine/src/copium/event/EventResult.h +++ b/CopiumEngine/src/copium/event/EventResult.h @@ -1,8 +1,10 @@ #pragma once -enum class EventResult -{ - Continue, - Handled, +#include "copium/util/Enum.h" + +#define CP_EVENT_RESULT_ENUMS \ + Continue, \ + Handled, \ Focus -}; \ No newline at end of file + +CP_ENUM_CREATOR(Copium, EventResult, CP_EVENT_RESULT_ENUMS); diff --git a/CopiumEngine/src/copium/event/EventType.h b/CopiumEngine/src/copium/event/EventType.h index 44549e8..7586d56 100644 --- a/CopiumEngine/src/copium/event/EventType.h +++ b/CopiumEngine/src/copium/event/EventType.h @@ -1,11 +1,10 @@ #pragma once -namespace Copium -{ - enum class EventType - { - MouseMove, MousePress, MouseRelease, MouseScroll, - KeyPress, KeyRelease, - WindowResize, WindowFocus, - }; -} +#include "copium/util/Enum.h" + +#define CP_EVENT_TYPE_ENUMS \ + MouseMove, MousePress, MouseRelease, MouseScroll, \ + KeyPress, KeyRelease, \ + WindowResize, WindowFocus + +CP_ENUM_CREATOR(Copium, EventType, CP_EVENT_TYPE_ENUMS); diff --git a/CopiumEngine/src/copium/pipeline/Pipeline.cpp b/CopiumEngine/src/copium/pipeline/Pipeline.cpp index a4fefd9..29588b5 100644 --- a/CopiumEngine/src/copium/pipeline/Pipeline.cpp +++ b/CopiumEngine/src/copium/pipeline/Pipeline.cpp @@ -126,7 +126,7 @@ namespace Copium void Pipeline::InitializePipeline(const PipelineCreator& creator) { - Shader shader{Shader::Type::GlslFile, creator.vertexShader, creator.fragmentShader}; + Shader shader{ShaderReadType::GlslFile, creator.vertexShader, creator.fragmentShader}; VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{}; vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; diff --git a/CopiumEngine/src/copium/pipeline/Shader.cpp b/CopiumEngine/src/copium/pipeline/Shader.cpp index 241d005..86cfa9f 100644 --- a/CopiumEngine/src/copium/pipeline/Shader.cpp +++ b/CopiumEngine/src/copium/pipeline/Shader.cpp @@ -6,28 +6,28 @@ namespace Copium { - Shader::Shader(Type type, const std::string& vertexInput, const std::string& fragmentInput) + Shader::Shader(ShaderReadType type, const std::string& vertexInput, const std::string& fragmentInput) { switch (type) { - case Type::GlslCode: + case ShaderReadType::GlslCode: vertShaderModule = InitializeShaderModuleFromGlslCode(vertexInput, shaderc_vertex_shader); fragShaderModule = InitializeShaderModuleFromGlslCode(fragmentInput, shaderc_fragment_shader); break; - case Type::GlslFile: + case ShaderReadType::GlslFile: vertShaderModule = InitializeShaderModuleFromGlslFile(vertexInput, shaderc_vertex_shader); fragShaderModule = InitializeShaderModuleFromGlslFile(fragmentInput, shaderc_fragment_shader); break; - case Type::SpvCode: + case ShaderReadType::SpvCode: vertShaderModule = InitializeShaderModule(vertexInput); fragShaderModule = InitializeShaderModule(fragmentInput); break; - case Type::SpvFile: + case ShaderReadType::SpvFile: vertShaderModule = InitializeShaderModule(FileSystem::ReadFile(vertexInput)); fragShaderModule = InitializeShaderModule(FileSystem::ReadFile(fragmentInput)); break; default: - CP_ASSERT(false, "Unreachable switch case %d", (int)type); + CP_ASSERT(false, "Unreachable switch case: %s", ToString(type).c_str()); } shaderStages.resize(2); diff --git a/CopiumEngine/src/copium/pipeline/Shader.h b/CopiumEngine/src/copium/pipeline/Shader.h index 361356e..1e4b28e 100644 --- a/CopiumEngine/src/copium/pipeline/Shader.h +++ b/CopiumEngine/src/copium/pipeline/Shader.h @@ -1,27 +1,25 @@ #pragma once #include "copium/util/Common.h" +#include "copium/util/Enum.h" #include #include +#define CP_SHADER_READ_TYPE_ENUMS GlslFile, GlslCode, SpvFile, SpvCode +CP_ENUM_CREATOR(Copium, ShaderReadType, CP_SHADER_READ_TYPE_ENUMS); + namespace Copium { class Shader final { CP_DELETE_COPY_AND_MOVE_CTOR(Shader); - public: - enum class Type - { - GlslFile, GlslCode, SpvFile, SpvCode - }; - private: VkShaderModule vertShaderModule; VkShaderModule fragShaderModule; std::vector shaderStages; public: - Shader(Type type, const std::string& vertexInput, const std::string& fragmentInput); + Shader(ShaderReadType type, const std::string& vertexInput, const std::string& fragmentInput); ~Shader(); const std::vector GetShaderStages() const; diff --git a/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp b/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp index 93f5b2d..f4947a9 100644 --- a/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp +++ b/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp @@ -79,7 +79,7 @@ namespace Copium case UniformType::Float: return 4; // float default: - CP_ABORT("Unhandled switch case"); + CP_ABORT("Unhandled switch case: %s", ToString(type).c_str()); } } @@ -102,7 +102,7 @@ namespace Copium case UniformType::Float: return 16; // alignas(16) glm::vec2 default: - CP_ABORT("Unhandled switch case"); + CP_ABORT("Unhandled switch case", ToString(type).c_str()); } } } diff --git a/CopiumEngine/src/copium/pipeline/ShaderBinding.h b/CopiumEngine/src/copium/pipeline/ShaderBinding.h index f95240c..0817f0d 100644 --- a/CopiumEngine/src/copium/pipeline/ShaderBinding.h +++ b/CopiumEngine/src/copium/pipeline/ShaderBinding.h @@ -1,25 +1,21 @@ #pragma once +#include "copium/util/Enum.h" + #include #include + +#define CP_BINDING_TYPE_ENUMS Sampler2D, UniformBuffer +#define CP_SHADER_TYPE_ENUMS Vertex, Fragment +#define CP_UNIFORM_TYPE_ENUMS Mat3, Mat4, Vec2, Vec3, Vec4, Float, Int + +CP_ENUM_CREATOR(Copium, BindingType, CP_BINDING_TYPE_ENUMS); +CP_ENUM_CREATOR(Copium, ShaderType, CP_SHADER_TYPE_ENUMS); +CP_ENUM_CREATOR(Copium, UniformType, CP_UNIFORM_TYPE_ENUMS); + namespace Copium { - enum class BindingType - { - Sampler2D, UniformBuffer - }; - - enum class ShaderType - { - Vertex, Fragment - }; - - enum class UniformType - { - Mat3, Mat4, Vec2, Vec3, Vec4, Float, Int - }; - struct ShaderBinding { std::string name; diff --git a/CopiumEngine/src/copium/util/Enum.h b/CopiumEngine/src/copium/util/Enum.h new file mode 100644 index 0000000..bccc5bb --- /dev/null +++ b/CopiumEngine/src/copium/util/Enum.h @@ -0,0 +1,52 @@ +#pragma once + +#include +#include +#include + +#define CP_STRINGIFY(x) #x +#define CP_TO_STRING(x) CP_STRINGIFY(x) + +#define CP_ENUM_CREATOR(NameSpace, EnumName, EnumList) \ +namespace NameSpace { \ + enum class EnumName \ + { \ + EnumList \ + }; \ + static const std::string& ToString(EnumName enumName) \ + { \ + static std::vector enumNames = Copium::EnumPrinter::GetEnumNames(CP_TO_STRING(EnumList)); \ + return enumNames[(int)enumName]; \ + } \ + \ + static std::ostream& operator<<(std::ostream& ostream, EnumName enumName) \ + { \ + return ostream << ToString(enumName); \ + } \ +} + + + +namespace Copium +{ + class EnumPrinter + { + public: + static std::vector GetEnumNames(const std::string& enumNames) + { + std::vector strs; + size_t lastPos = 0; + size_t pos = enumNames.find(',', lastPos); + while (pos != std::string::npos) + { + strs.emplace_back(enumNames.substr(lastPos, pos - lastPos)); + + lastPos = pos + 1; + while (enumNames[lastPos] == ' ') lastPos++; + pos = enumNames.find(',', lastPos); + } + strs.emplace_back(enumNames.substr(lastPos)); + return strs; + } + }; +}