Add printable Enum

This commit is contained in:
Thraix
2023-05-01 18:23:22 +02:00
parent ca7286807a
commit ad69293faa
17 changed files with 122 additions and 76 deletions
+1
View File
@@ -258,6 +258,7 @@
<ClInclude Include="src\copium\core\DebugMessenger.h" /> <ClInclude Include="src\copium\core\DebugMessenger.h" />
<ClInclude Include="src\copium\pipeline\DescriptorSet.h" /> <ClInclude Include="src\copium\pipeline\DescriptorSet.h" />
<ClInclude Include="src\copium\pipeline\DescriptorPool.h" /> <ClInclude Include="src\copium\pipeline\DescriptorPool.h" />
<ClInclude Include="src\copium\util\Enum.h" />
<ClInclude Include="src\copium\util\RuntimeException.h" /> <ClInclude Include="src\copium\util\RuntimeException.h" />
<ClInclude Include="src\copium\util\FileSystem.h" /> <ClInclude Include="src\copium\util\FileSystem.h" />
<ClInclude Include="src\copium\buffer\Framebuffer.h" /> <ClInclude Include="src\copium\buffer\Framebuffer.h" />
@@ -4,19 +4,19 @@
namespace Copium namespace Copium
{ {
CommandBuffer::CommandBuffer(Type type) CommandBuffer::CommandBuffer(CommandBufferType type)
: type{type} : type{type}
{ {
switch (type) switch (type)
{ {
case Type::SingleUse: case CommandBufferType::SingleUse:
commandBuffers.resize(1); commandBuffers.resize(1);
break; break;
case Type::Dynamic: case CommandBufferType::Dynamic:
commandBuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT); commandBuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT);
break; break;
default: default:
CP_ABORT("Unreachable switch case"); CP_ABORT("Unreachable switch case: %s", ToString(type).c_str());
} }
VkCommandBufferAllocateInfo allocateInfo{}; VkCommandBufferAllocateInfo allocateInfo{};
@@ -42,13 +42,13 @@ namespace Copium
switch (type) switch (type)
{ {
case Type::SingleUse: case CommandBufferType::SingleUse:
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
break; break;
case Type::Dynamic: case CommandBufferType::Dynamic:
break; break;
default: default:
CP_ABORT("Unreachable switch case"); CP_ABORT("Unreachable switch case: %s", ToString(type).c_str());
} }
vkResetCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], 0); vkResetCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], 0);
@@ -1,25 +1,24 @@
#pragma once #pragma once
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include "copium/util/Enum.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
#define CP_COMMAND_BUFFER_TYPE_ENUMS SingleUse, Dynamic
CP_ENUM_CREATOR(Copium, CommandBufferType, CP_COMMAND_BUFFER_TYPE_ENUMS);
namespace Copium namespace Copium
{ {
class CommandBuffer class CommandBuffer
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer); CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer);
public:
enum class Type
{
SingleUse, Dynamic
};
private: private:
std::vector<VkCommandBuffer> commandBuffers; std::vector<VkCommandBuffer> commandBuffers;
const Type type; const CommandBufferType type;
public: public:
CommandBuffer(Type type); CommandBuffer(CommandBufferType type);
virtual ~CommandBuffer(); virtual ~CommandBuffer();
void Begin(); void Begin();
@@ -3,7 +3,7 @@
namespace Copium namespace Copium
{ {
CommandBufferScoped::CommandBufferScoped() CommandBufferScoped::CommandBufferScoped()
: CommandBuffer{Type::SingleUse} : CommandBuffer{CommandBufferType::SingleUse}
{ {
CommandBuffer::Begin(); CommandBuffer::Begin();
} }
+1 -1
View File
@@ -173,7 +173,7 @@ namespace Copium
void Application::InitializeCommandBuffer() void Application::InitializeCommandBuffer()
{ {
commandBuffer = std::make_unique<CommandBuffer>(CommandBuffer::Type::Dynamic); commandBuffer = std::make_unique<CommandBuffer>(CommandBufferType::Dynamic);
} }
void Application::RecordCommandBuffer() void Application::RecordCommandBuffer()
+1 -1
View File
@@ -16,7 +16,7 @@ namespace Copium
void Vulkan::Initialize() void Vulkan::Initialize()
{ {
instance = std::make_unique<Instance>("Copium Engine"); instance = std::make_unique<Instance>("Copium Engine");
window = std::make_unique<Window>("Copium Engine", 1920, 1080, Window::Mode::Windowed); window = std::make_unique<Window>("Copium Engine", 1920, 1080, WindowMode::Windowed);
device = std::make_unique<Device>(); device = std::make_unique<Device>();
swapChain = std::make_unique<SwapChain>(); swapChain = std::make_unique<SwapChain>();
+6 -6
View File
@@ -13,7 +13,7 @@
namespace Copium 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); InitializeWindow(windowName, width, height, mode);
InitializeSurface(); InitializeSurface();
@@ -35,20 +35,20 @@ namespace Copium
return window; 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); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
switch (mode) switch (mode)
{ {
case Mode::Fullscreen: case WindowMode::Fullscreen:
{ {
GLFWmonitor* monitor = glfwGetPrimaryMonitor(); GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor); const GLFWvidmode* mode = glfwGetVideoMode(monitor);
window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), glfwGetPrimaryMonitor(), nullptr); window = glfwCreateWindow(mode->width, mode->height, windowName.c_str(), glfwGetPrimaryMonitor(), nullptr);
break; break;
} }
case Mode::BorderlessWindowed: case WindowMode::BorderlessWindowed:
{ {
GLFWmonitor* monitor = glfwGetPrimaryMonitor(); GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor); const GLFWvidmode* mode = glfwGetVideoMode(monitor);
@@ -56,13 +56,13 @@ namespace Copium
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
break; break;
} }
case Mode::Windowed: case WindowMode::Windowed:
{ {
window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr); window = glfwCreateWindow(width, height, windowName.c_str(), nullptr, nullptr);
break; break;
} }
default: default:
CP_ABORT("Unreachable switch case"); CP_ABORT("Unreachable switch case: %s", ToString(mode).c_str());
} }
CP_ASSERT(window, "Failed to initialize glfw window"); CP_ASSERT(window, "Failed to initialize glfw window");
+6 -7
View File
@@ -1,32 +1,31 @@
#pragma once #pragma once
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include "copium/util/Enum.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#define CP_WINDOW_MODE_ENUMS Fullscreen, BorderlessWindowed, Windowed
CP_ENUM_CREATOR(Copium, WindowMode, CP_WINDOW_MODE_ENUMS);
namespace Copium namespace Copium
{ {
class Window final class Window final
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Window); CP_DELETE_COPY_AND_MOVE_CTOR(Window);
public:
enum class Mode
{
Fullscreen, BorderlessWindowed, Windowed
};
private: private:
GLFWwindow* window; GLFWwindow* window;
VkSurfaceKHR surface; VkSurfaceKHR surface;
public: public:
Window(const std::string& windowName, int width, int height, Mode mode); Window(const std::string& windowName, int width, int height, WindowMode mode);
~Window(); ~Window();
VkSurfaceKHR GetSurface() const; VkSurfaceKHR GetSurface() const;
GLFWwindow* GetWindow(); GLFWwindow* GetWindow();
private: 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(); void InitializeSurface();
static void FramebufferResizeCallback(GLFWwindow* glfwWindow, int width, int height); static void FramebufferResizeCallback(GLFWwindow* glfwWindow, int width, int height);
@@ -35,7 +35,7 @@ namespace Copium
focusedEventHandler = eventHandler; focusedEventHandler = eventHandler;
return; return;
default: default:
CP_ABORT("Unreachable switch case"); CP_ABORT("Unreachable switch case: %s", ToString(result).c_str());
} }
} }
} }
+7 -5
View File
@@ -1,8 +1,10 @@
#pragma once #pragma once
enum class EventResult #include "copium/util/Enum.h"
{
Continue, #define CP_EVENT_RESULT_ENUMS \
Handled, Continue, \
Handled, \
Focus Focus
};
CP_ENUM_CREATOR(Copium, EventResult, CP_EVENT_RESULT_ENUMS);
+8 -9
View File
@@ -1,11 +1,10 @@
#pragma once #pragma once
namespace Copium #include "copium/util/Enum.h"
{
enum class EventType #define CP_EVENT_TYPE_ENUMS \
{ MouseMove, MousePress, MouseRelease, MouseScroll, \
MouseMove, MousePress, MouseRelease, MouseScroll, KeyPress, KeyRelease, \
KeyPress, KeyRelease, WindowResize, WindowFocus
WindowResize, WindowFocus,
}; CP_ENUM_CREATOR(Copium, EventType, CP_EVENT_TYPE_ENUMS);
}
@@ -126,7 +126,7 @@ namespace Copium
void Pipeline::InitializePipeline(const PipelineCreator& creator) 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{}; VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{};
vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
+6 -6
View File
@@ -6,28 +6,28 @@
namespace Copium 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) switch (type)
{ {
case Type::GlslCode: case ShaderReadType::GlslCode:
vertShaderModule = InitializeShaderModuleFromGlslCode(vertexInput, shaderc_vertex_shader); vertShaderModule = InitializeShaderModuleFromGlslCode(vertexInput, shaderc_vertex_shader);
fragShaderModule = InitializeShaderModuleFromGlslCode(fragmentInput, shaderc_fragment_shader); fragShaderModule = InitializeShaderModuleFromGlslCode(fragmentInput, shaderc_fragment_shader);
break; break;
case Type::GlslFile: case ShaderReadType::GlslFile:
vertShaderModule = InitializeShaderModuleFromGlslFile(vertexInput, shaderc_vertex_shader); vertShaderModule = InitializeShaderModuleFromGlslFile(vertexInput, shaderc_vertex_shader);
fragShaderModule = InitializeShaderModuleFromGlslFile(fragmentInput, shaderc_fragment_shader); fragShaderModule = InitializeShaderModuleFromGlslFile(fragmentInput, shaderc_fragment_shader);
break; break;
case Type::SpvCode: case ShaderReadType::SpvCode:
vertShaderModule = InitializeShaderModule(vertexInput); vertShaderModule = InitializeShaderModule(vertexInput);
fragShaderModule = InitializeShaderModule(fragmentInput); fragShaderModule = InitializeShaderModule(fragmentInput);
break; break;
case Type::SpvFile: case ShaderReadType::SpvFile:
vertShaderModule = InitializeShaderModule(FileSystem::ReadFile(vertexInput)); vertShaderModule = InitializeShaderModule(FileSystem::ReadFile(vertexInput));
fragShaderModule = InitializeShaderModule(FileSystem::ReadFile(fragmentInput)); fragShaderModule = InitializeShaderModule(FileSystem::ReadFile(fragmentInput));
break; break;
default: default:
CP_ASSERT(false, "Unreachable switch case %d", (int)type); CP_ASSERT(false, "Unreachable switch case: %s", ToString(type).c_str());
} }
shaderStages.resize(2); shaderStages.resize(2);
+5 -7
View File
@@ -1,27 +1,25 @@
#pragma once #pragma once
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include "copium/util/Enum.h"
#include <shaderc/shaderc.hpp> #include <shaderc/shaderc.hpp>
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
#define CP_SHADER_READ_TYPE_ENUMS GlslFile, GlslCode, SpvFile, SpvCode
CP_ENUM_CREATOR(Copium, ShaderReadType, CP_SHADER_READ_TYPE_ENUMS);
namespace Copium namespace Copium
{ {
class Shader final class Shader final
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Shader); CP_DELETE_COPY_AND_MOVE_CTOR(Shader);
public:
enum class Type
{
GlslFile, GlslCode, SpvFile, SpvCode
};
private: private:
VkShaderModule vertShaderModule; VkShaderModule vertShaderModule;
VkShaderModule fragShaderModule; VkShaderModule fragShaderModule;
std::vector<VkPipelineShaderStageCreateInfo> shaderStages; std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
public: public:
Shader(Type type, const std::string& vertexInput, const std::string& fragmentInput); Shader(ShaderReadType type, const std::string& vertexInput, const std::string& fragmentInput);
~Shader(); ~Shader();
const std::vector<VkPipelineShaderStageCreateInfo> GetShaderStages() const; const std::vector<VkPipelineShaderStageCreateInfo> GetShaderStages() const;
@@ -79,7 +79,7 @@ namespace Copium
case UniformType::Float: case UniformType::Float:
return 4; // float return 4; // float
default: 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: case UniformType::Float:
return 16; // alignas(16) glm::vec2 return 16; // alignas(16) glm::vec2
default: default:
CP_ABORT("Unhandled switch case"); CP_ABORT("Unhandled switch case", ToString(type).c_str());
} }
} }
} }
@@ -1,25 +1,21 @@
#pragma once #pragma once
#include "copium/util/Enum.h"
#include <string> #include <string>
#include <vector> #include <vector>
#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 namespace Copium
{ {
enum class BindingType
{
Sampler2D, UniformBuffer
};
enum class ShaderType
{
Vertex, Fragment
};
enum class UniformType
{
Mat3, Mat4, Vec2, Vec3, Vec4, Float, Int
};
struct ShaderBinding struct ShaderBinding
{ {
std::string name; std::string name;
+52
View File
@@ -0,0 +1,52 @@
#pragma once
#include <ostream>
#include <string>
#include <vector>
#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<std::string> 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<std::string> GetEnumNames(const std::string& enumNames)
{
std::vector<std::string> 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;
}
};
}