Add printable Enum
This commit is contained in:
@@ -258,6 +258,7 @@
|
||||
<ClInclude Include="src\copium\core\DebugMessenger.h" />
|
||||
<ClInclude Include="src\copium\pipeline\DescriptorSet.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\FileSystem.h" />
|
||||
<ClInclude Include="src\copium\buffer\Framebuffer.h" />
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,25 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/util/Common.h"
|
||||
#include "copium/util/Enum.h"
|
||||
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
#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<VkCommandBuffer> commandBuffers;
|
||||
const Type type;
|
||||
const CommandBufferType type;
|
||||
|
||||
public:
|
||||
CommandBuffer(Type type);
|
||||
CommandBuffer(CommandBufferType type);
|
||||
virtual ~CommandBuffer();
|
||||
|
||||
void Begin();
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Copium
|
||||
{
|
||||
CommandBufferScoped::CommandBufferScoped()
|
||||
: CommandBuffer{Type::SingleUse}
|
||||
: CommandBuffer{CommandBufferType::SingleUse}
|
||||
{
|
||||
CommandBuffer::Begin();
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@ namespace Copium
|
||||
|
||||
void Application::InitializeCommandBuffer()
|
||||
{
|
||||
commandBuffer = std::make_unique<CommandBuffer>(CommandBuffer::Type::Dynamic);
|
||||
commandBuffer = std::make_unique<CommandBuffer>(CommandBufferType::Dynamic);
|
||||
}
|
||||
|
||||
void Application::RecordCommandBuffer()
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Copium
|
||||
void Vulkan::Initialize()
|
||||
{
|
||||
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>();
|
||||
swapChain = std::make_unique<SwapChain>();
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -1,32 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/util/Common.h"
|
||||
#include "copium/util/Enum.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
enum class EventResult
|
||||
{
|
||||
Continue,
|
||||
Handled,
|
||||
#include "copium/util/Enum.h"
|
||||
|
||||
#define CP_EVENT_RESULT_ENUMS \
|
||||
Continue, \
|
||||
Handled, \
|
||||
Focus
|
||||
};
|
||||
|
||||
CP_ENUM_CREATOR(Copium, EventResult, CP_EVENT_RESULT_ENUMS);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,27 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/util/Common.h"
|
||||
#include "copium/util/Enum.h"
|
||||
|
||||
#include <shaderc/shaderc.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
|
||||
{
|
||||
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<VkPipelineShaderStageCreateInfo> 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<VkPipelineShaderStageCreateInfo> GetShaderStages() const;
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/util/Enum.h"
|
||||
|
||||
#include <string>
|
||||
#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
|
||||
{
|
||||
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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user