From 9a3b3aa13cc0b1c120a4a315f48f0a7c90d0fde3 Mon Sep 17 00:00:00 2001 From: Thraix Date: Sun, 3 May 2026 12:40:47 +0200 Subject: [PATCH] Rework tracing system and enum creators - Rework tracing system, now using "{}" for formatting instead of the standard %s,%d,%f formatting that C++ uses. - Rework enums creators to not take in namespace - Fix single use CommandBuffers sometimes failing due to indexing them with the current frame in flight - Add DropEvent to Window --- CopiumEngine/src/copium/asset/AssetFile.cpp | 5 +- .../src/copium/asset/AssetManager.cpp | 31 ++- CopiumEngine/src/copium/asset/AssetManager.h | 5 +- .../src/copium/buffer/CommandBuffer.cpp | 37 ++- .../src/copium/buffer/CommandBuffer.h | 5 +- .../src/copium/buffer/Framebuffer.cpp | 13 +- .../src/copium/buffer/IndexBuffer.cpp | 2 + CopiumEngine/src/copium/buffer/IndexBuffer.h | 2 +- .../src/copium/buffer/UniformBuffer.cpp | 14 +- .../src/copium/core/DebugMessenger.cpp | 5 +- CopiumEngine/src/copium/core/Device.cpp | 4 +- CopiumEngine/src/copium/core/Instance.cpp | 8 +- CopiumEngine/src/copium/core/Instance.h | 2 + CopiumEngine/src/copium/core/SwapChain.h | 1 + CopiumEngine/src/copium/core/Vulkan.cpp | 6 +- CopiumEngine/src/copium/core/Window.cpp | 14 +- CopiumEngine/src/copium/core/Window.h | 7 +- CopiumEngine/src/copium/ecs/ComponentPool.h | 14 +- CopiumEngine/src/copium/ecs/ECSManager.cpp | 6 +- CopiumEngine/src/copium/ecs/ECSManager.h | 14 +- CopiumEngine/src/copium/ecs/SystemPool.cpp | 22 +- CopiumEngine/src/copium/event/DropEvent.cpp | 15 ++ CopiumEngine/src/copium/event/DropEvent.h | 17 ++ .../src/copium/event/EventDispatcher.cpp | 4 +- CopiumEngine/src/copium/event/EventResult.h | 7 +- CopiumEngine/src/copium/event/EventType.h | 18 +- CopiumEngine/src/copium/event/Input.cpp | 22 +- .../src/copium/pipeline/DescriptorPool.cpp | 5 +- .../src/copium/pipeline/DescriptorSet.cpp | 2 +- .../src/copium/pipeline/DescriptorSet.h | 1 + .../src/copium/pipeline/PipelineCreator.cpp | 2 +- CopiumEngine/src/copium/pipeline/Shader.cpp | 14 +- CopiumEngine/src/copium/pipeline/Shader.h | 5 +- .../src/copium/pipeline/ShaderBinding.cpp | 12 +- .../src/copium/pipeline/ShaderBinding.h | 12 +- .../src/copium/pipeline/ShaderReflector.cpp | 5 +- .../src/copium/pipeline/VertexDescriptor.cpp | 2 +- CopiumEngine/src/copium/sampler/Font.cpp | 4 +- CopiumEngine/src/copium/sampler/Image.h | 2 +- .../src/copium/sampler/SamplerCreator.cpp | 6 +- CopiumEngine/src/copium/sampler/Texture2D.cpp | 4 +- CopiumEngine/src/copium/util/Common.h | 93 -------- CopiumEngine/src/copium/util/Enum.h | 31 ++- CopiumEngine/src/copium/util/FileSystem.cpp | 14 +- CopiumEngine/src/copium/util/FileSystem.h | 1 + CopiumEngine/src/copium/util/MetaFile.cpp | 18 +- CopiumEngine/src/copium/util/Trace.h | 212 ++++++++++++++++++ CopiumEngine/src/copium/util/Uuid.cpp | 8 +- 48 files changed, 476 insertions(+), 277 deletions(-) create mode 100644 CopiumEngine/src/copium/event/DropEvent.cpp create mode 100644 CopiumEngine/src/copium/event/DropEvent.h create mode 100644 CopiumEngine/src/copium/util/Trace.h diff --git a/CopiumEngine/src/copium/asset/AssetFile.cpp b/CopiumEngine/src/copium/asset/AssetFile.cpp index ff78289..d5302ab 100644 --- a/CopiumEngine/src/copium/asset/AssetFile.cpp +++ b/CopiumEngine/src/copium/asset/AssetFile.cpp @@ -3,6 +3,7 @@ #include #include "copium/util/FileSystem.h" +#include "copium/util/Trace.h" namespace Copium { @@ -30,7 +31,7 @@ namespace Copium Load(metaFile, assetType); return; } - CP_WARN("Unknown Asset type in file: %s", metaFile.GetFilePath().c_str()); + CP_WARN("Unknown Asset type in file: {}", metaFile.GetFilePath()); } const std::string& AssetFile::GetPath() const @@ -48,7 +49,7 @@ namespace Copium MetaFileClass& metaClass = metaFile.GetMetaClass(className); if (!metaClass.HasValue("uuid")) { - CP_WARN("Asset (%s) has no UUID assigned, generating new one", path.c_str()); + CP_WARN("Asset ({}) has no UUID assigned, generating new one", path); metaClass.AddValue("uuid", Uuid{}.ToString()); std::fstream file{path}; file << metaFile; diff --git a/CopiumEngine/src/copium/asset/AssetManager.cpp b/CopiumEngine/src/copium/asset/AssetManager.cpp index a2916c9..af4f1a4 100644 --- a/CopiumEngine/src/copium/asset/AssetManager.cpp +++ b/CopiumEngine/src/copium/asset/AssetManager.cpp @@ -3,8 +3,8 @@ #include #include -#include "copium/util/Common.h" #include "copium/util/MetaFile.h" +#include "copium/util/Trace.h" namespace Copium { @@ -34,12 +34,12 @@ namespace Copium .c_str(); try { - CP_DEBUG("Registering Asset: %s", assetPath.c_str()); + CP_DEBUG("Registering Asset: {}", assetPath); cachedAssetFiles.emplace_back(assetPath); } catch (RuntimeException& exception) { - CP_ERR("Failed to register Asset: %s", assetPath.c_str()); + CP_ERR("Failed to register Asset: {}", assetPath); } } } @@ -68,7 +68,7 @@ namespace Copium Asset& AssetManager::LoadAsset(const std::string& assetPath) { - CP_DEBUG("Loading Asset: %s", assetPath.c_str()); + CP_DEBUG("Loading Asset: {}", assetPath); for (auto& dir : assetDirs) { @@ -80,12 +80,12 @@ namespace Copium return LoadAssetFromPath(path); } - CP_ABORT("Unknown Asset: %s", assetPath.c_str()); + CP_ABORT("Unknown Asset: {}", assetPath); } Asset& AssetManager::LoadAsset(const Uuid& uuid) { - CP_DEBUG("Loading uuid Asset: %s", uuid.ToString().c_str()); + CP_DEBUG("Loading uuid Asset: {}", uuid); for (auto&& assetFile : cachedAssetFiles) { if (assetFile.NeedReload()) @@ -96,16 +96,16 @@ namespace Copium return LoadAssetFromPath(assetFile.GetPath()); } - CP_ABORT("Asset not found with uuid=%s", uuid.ToString().c_str()); + CP_ABORT("Asset not found with uuid={}", uuid); // TODO: Reload the assetCache to see if a new file has appeared with that uuid } AssetId AssetManager::DuplicateAsset(AssetId id) { auto it = assets.find(id); - CP_ASSERT(it != assets.end(), "Failed to find asset with id=%d", id); + CP_ASSERT(it != assets.end(), "Failed to find asset with id={}", id); - CP_DEBUG("Duplicating asset: %s", it->second->GetName().c_str()); + CP_DEBUG("Duplicating asset: {}", it->second->GetName()); it->second->metaData.loadCount++; return id; } @@ -118,8 +118,7 @@ namespace Copium CP_WARN("Asset not loaded"); return; } - CP_DEBUG( - "Unloading Asset: %s (%d instances left)", it->second->GetName().c_str(), it->second->metaData.loadCount - 1); + CP_DEBUG("Unloading Asset: {} ({} instances left)", it->second->GetName(), it->second->metaData.loadCount - 1); it->second->metaData.loadCount--; if (it->second->metaData.loadCount > 0) @@ -141,7 +140,7 @@ namespace Copium { if (assets.empty()) return; - CP_WARN("Performing auto clean up of %d non unloaded assets", assets.size()); + CP_WARN("Performing auto clean up of {} non unloaded assets", assets.size()); while (!assets.empty()) { UnloadAsset(assets.begin()->second->GetId()); @@ -152,10 +151,10 @@ namespace Copium Asset& AssetManager::RegisterRuntimeAsset(const std::string& name, const Uuid& uuid, std::unique_ptr&& asset) { - CP_DEBUG("Registering Runtime Asset: %s", name.c_str()); + CP_DEBUG("Registering Runtime Asset: {}", name); auto it = nameToAssetCache.find(name); - CP_ASSERT(it == nameToAssetCache.end(), "Asset already exists: %s", name.c_str()); + CP_ASSERT(it == nameToAssetCache.end(), "Asset already exists: {}", name); AssetId id = runtimeAssetId++; Asset* asset2 = assets.emplace(id, std::move(asset)).first->second.get(); @@ -169,7 +168,7 @@ namespace Copium Asset& AssetManager::LoadAssetFromPath(const std::string& filepath) { - CP_DEBUG("Loading Asset: %s", filepath.c_str()); + CP_DEBUG("Loading Asset: {}", filepath); auto it = pathToAssetCache.find(filepath); if (it != pathToAssetCache.end()) { @@ -184,6 +183,6 @@ namespace Copium if (metaFile.HasMetaClass(assetType.first)) return assetType.second(metaFile, assetType.first); } - CP_ABORT("Unknown Asset type: %s", filepath.c_str()); + CP_ABORT("Unknown Asset type: {}", filepath); } } diff --git a/CopiumEngine/src/copium/asset/AssetManager.h b/CopiumEngine/src/copium/asset/AssetManager.h index ee18485..03d2b26 100644 --- a/CopiumEngine/src/copium/asset/AssetManager.h +++ b/CopiumEngine/src/copium/asset/AssetManager.h @@ -2,11 +2,12 @@ #include #include +#include #include #include "copium/asset/Asset.h" #include "copium/asset/AssetFile.h" -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { @@ -42,7 +43,7 @@ namespace Copium static void RegisterAssetType(const std::string& assetType) { CP_ASSERT(assetTypes.emplace(assetType, &AssetManager::CreateAsset).second, - "Asset type already exists: %s", + "Asset type already exists: {}", assetType.c_str()); AssetFile::RegisterAssetType(assetType); } diff --git a/CopiumEngine/src/copium/buffer/CommandBuffer.cpp b/CopiumEngine/src/copium/buffer/CommandBuffer.cpp index e1ad7c4..138602c 100644 --- a/CopiumEngine/src/copium/buffer/CommandBuffer.cpp +++ b/CopiumEngine/src/copium/buffer/CommandBuffer.cpp @@ -1,6 +1,7 @@ #include "copium/buffer/CommandBuffer.h" #include "copium/core/Vulkan.h" +#include "copium/util/Trace.h" namespace Copium { @@ -16,7 +17,7 @@ namespace Copium commandBuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT); break; default: - CP_ABORT("Unreachable switch case: %s", ToString(type).c_str()); + CP_ABORT("Unreachable switch case: {}", type); } VkCommandBufferAllocateInfo allocateInfo{}; @@ -49,33 +50,46 @@ namespace Copium beginInfo.flags = 0; beginInfo.pInheritanceInfo = nullptr; + int index = Vulkan::GetSwapChain().GetFlightIndex(); + switch (type) { case CommandBufferType::SingleUse: beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + index = 0; break; case CommandBufferType::Dynamic: break; default: - CP_ABORT("Unreachable switch case: %s", ToString(type).c_str()); + CP_ABORT("Unreachable switch case: {}", type); } - vkResetCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], 0); - CP_VK_ASSERT(vkBeginCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()], &beginInfo), - "Failed to begin command buffer"); + vkResetCommandBuffer(commandBuffers[index], 0); + CP_VK_ASSERT(vkBeginCommandBuffer(commandBuffers[index], &beginInfo), "Failed to begin command buffer"); } void CommandBuffer::End() { - vkEndCommandBuffer(commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()]); + int index = Vulkan::GetSwapChain().GetFlightIndex(); + if (type == CommandBufferType::SingleUse) + { + index = 0; + } + vkEndCommandBuffer(commandBuffers[index]); } void CommandBuffer::Submit() { + int index = Vulkan::GetSwapChain().GetFlightIndex(); + if (type == CommandBufferType::SingleUse) + { + index = 0; + } + VkSubmitInfo submitInfo{}; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.commandBufferCount = 1; - submitInfo.pCommandBuffers = &commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()]; + submitInfo.pCommandBuffers = &commandBuffers[index]; vkQueueSubmit(Vulkan::GetDevice().GetGraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE); // TODO: if singleUse? @@ -84,6 +98,11 @@ namespace Copium CommandBuffer::operator VkCommandBuffer() const { - return commandBuffers[Vulkan::GetSwapChain().GetFlightIndex()]; + int index = Vulkan::GetSwapChain().GetFlightIndex(); + if (type == CommandBufferType::SingleUse) + { + index = 0; + } + return commandBuffers[index]; } -} \ No newline at end of file +} diff --git a/CopiumEngine/src/copium/buffer/CommandBuffer.h b/CopiumEngine/src/copium/buffer/CommandBuffer.h index 108bcbf..8e64133 100644 --- a/CopiumEngine/src/copium/buffer/CommandBuffer.h +++ b/CopiumEngine/src/copium/buffer/CommandBuffer.h @@ -5,11 +5,10 @@ #include "copium/util/Common.h" #include "copium/util/Enum.h" -#define CP_COMMAND_BUFFER_TYPE_ENUMS SingleUse, Dynamic -CP_ENUM_CREATOR(Copium, CommandBufferType, CP_COMMAND_BUFFER_TYPE_ENUMS); - namespace Copium { + CP_ENUM_CREATOR(CommandBufferType, SingleUse, Dynamic); + class CommandBuffer { CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer); diff --git a/CopiumEngine/src/copium/buffer/Framebuffer.cpp b/CopiumEngine/src/copium/buffer/Framebuffer.cpp index 55a5b69..da42cf1 100644 --- a/CopiumEngine/src/copium/buffer/Framebuffer.cpp +++ b/CopiumEngine/src/copium/buffer/Framebuffer.cpp @@ -1,6 +1,5 @@ #include "copium/buffer/Framebuffer.h" -#include "copium/asset/AssetManager.h" #include "copium/buffer/CommandBuffer.h" #include "copium/core/Vulkan.h" #include "copium/sampler/Image.h" @@ -15,8 +14,8 @@ namespace Copium width = attachment.GetWidth(); height = attachment.GetHeight(); - CP_ASSERT(width > 0, "Width of framebuffer is less than 1: %d", width); - CP_ASSERT(height > 0, "Height of framebuffer is less than 1: %d", height); + CP_ASSERT(width > 0, "Width of framebuffer is less than 1: {}", width); + CP_ASSERT(height > 0, "Height of framebuffer is less than 1: {}", height); InitializeDepthBuffer(); InitializeRenderPass(); @@ -27,8 +26,8 @@ namespace Copium : width{width}, height{height} { - CP_ASSERT(width > 0, "Width of framebuffer is less than 1: %d", width); - CP_ASSERT(height > 0, "Height of framebuffer is less than 1: %d", height); + CP_ASSERT(width > 0, "Width of framebuffer is less than 1: {}", width); + CP_ASSERT(height > 0, "Height of framebuffer is less than 1: {}", height); InitializeImage(samplerCreator); InitializeDepthBuffer(); @@ -51,8 +50,8 @@ namespace Copium void Framebuffer::Resize(int width, int height) { - CP_ASSERT(width > 0, "Width of framebuffer is less than 1: %d", width); - CP_ASSERT(height > 0, "Height of framebuffer is less than 1: %d", height); + CP_ASSERT(width > 0, "Width of framebuffer is less than 1: {}", width); + CP_ASSERT(height > 0, "Height of framebuffer is less than 1: {}", height); Vulkan::GetDevice().WaitIdle(); this->width = width; diff --git a/CopiumEngine/src/copium/buffer/IndexBuffer.cpp b/CopiumEngine/src/copium/buffer/IndexBuffer.cpp index 176c22e..79fba5c 100644 --- a/CopiumEngine/src/copium/buffer/IndexBuffer.cpp +++ b/CopiumEngine/src/copium/buffer/IndexBuffer.cpp @@ -2,6 +2,8 @@ #include +#include "copium/util/Trace.h" + namespace Copium { IndexBuffer::IndexBuffer(int indexCount) diff --git a/CopiumEngine/src/copium/buffer/IndexBuffer.h b/CopiumEngine/src/copium/buffer/IndexBuffer.h index 98a3a4f..3dca272 100644 --- a/CopiumEngine/src/copium/buffer/IndexBuffer.h +++ b/CopiumEngine/src/copium/buffer/IndexBuffer.h @@ -20,4 +20,4 @@ namespace Copium void Draw(const CommandBuffer& commandBuffer); void Draw(const CommandBuffer& commandBuffer, int indices); }; -} \ No newline at end of file +} diff --git a/CopiumEngine/src/copium/buffer/UniformBuffer.cpp b/CopiumEngine/src/copium/buffer/UniformBuffer.cpp index 2a95a68..dfa3ba9 100644 --- a/CopiumEngine/src/copium/buffer/UniformBuffer.cpp +++ b/CopiumEngine/src/copium/buffer/UniformBuffer.cpp @@ -25,7 +25,7 @@ namespace Copium void UniformBuffer::Set(const std::string& str, const glm::mat3& data) { - CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat3, "Uniform type missmatch = %s", str.c_str()); + CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat3, "Uniform type missmatch = {}", str); uint32_t offset = binding.GetUniformOffset(str); // memcpy(buffer.data() + offset, &data[0], sizeof(glm::vec3)); // memcpy(buffer.data() + offset + 16, &data[1], sizeof(glm::vec3)); @@ -36,42 +36,42 @@ namespace Copium void UniformBuffer::Set(const std::string& str, const glm::mat4& data) { - CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat4, "Uniform type missmatch = %s", str.c_str()); + CP_ASSERT(binding.GetUniformType(str) == UniformType::Mat4, "Uniform type missmatch = {}", str); uint32_t offset = binding.GetUniformOffset(str); memcpy(buffer.data() + offset, &data, sizeof(glm::mat4)); } void UniformBuffer::Set(const std::string& str, const glm::vec2& data) { - CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec2, "Uniform type missmatch = %s", str.c_str()); + CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec2, "Uniform type missmatch = {}", str); uint32_t offset = binding.GetUniformOffset(str); memcpy(buffer.data() + offset, &data, sizeof(glm::vec2)); } void UniformBuffer::Set(const std::string& str, const glm::vec3& data) { - CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec3, "Uniform type missmatch = %s", str.c_str()); + CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec3, "Uniform type missmatch = {}", str); uint32_t offset = binding.GetUniformOffset(str); memcpy(buffer.data() + offset, &data, sizeof(glm::vec3)); } void UniformBuffer::Set(const std::string& str, const glm::vec4& data) { - CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec4, "Uniform type missmatch = %s", str.c_str()); + CP_ASSERT(binding.GetUniformType(str) == UniformType::Vec4, "Uniform type missmatch = {}", str); uint32_t offset = binding.GetUniformOffset(str); memcpy(buffer.data() + offset, &data, sizeof(glm::vec4)); } void UniformBuffer::Set(const std::string& str, float data) { - CP_ASSERT(binding.GetUniformType(str) == UniformType::Float, "Uniform type missmatch = %s", str.c_str()); + CP_ASSERT(binding.GetUniformType(str) == UniformType::Float, "Uniform type missmatch = {}", str); uint32_t offset = binding.GetUniformOffset(str); memcpy(buffer.data() + offset, &data, sizeof(float)); } void UniformBuffer::Set(const std::string& str, int data) { - CP_ASSERT(binding.GetUniformType(str) == UniformType::Int, "Uniform type missmatch = %s", str.c_str()); + CP_ASSERT(binding.GetUniformType(str) == UniformType::Int, "Uniform type missmatch = {}", str); uint32_t offset = binding.GetUniformOffset(str); memcpy(buffer.data() + offset, &data, sizeof(int)); } diff --git a/CopiumEngine/src/copium/core/DebugMessenger.cpp b/CopiumEngine/src/copium/core/DebugMessenger.cpp index 4bb7539..480520b 100644 --- a/CopiumEngine/src/copium/core/DebugMessenger.cpp +++ b/CopiumEngine/src/copium/core/DebugMessenger.cpp @@ -1,6 +1,7 @@ #include "copium/core/DebugMessenger.h" #include "copium/core/Instance.h" +#include "copium/util/Trace.h" namespace Copium { @@ -64,9 +65,9 @@ namespace Copium if (messageSeverity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) { if (messageSeverity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) - CP_ABORT("%s", pCallbackData->pMessage); + CP_ABORT("{}", pCallbackData->pMessage); else - CP_WARN("%s", pCallbackData->pMessage); + CP_WARN("{}", pCallbackData->pMessage); } return VK_FALSE; } diff --git a/CopiumEngine/src/copium/core/Device.cpp b/CopiumEngine/src/copium/core/Device.cpp index 0b8eb67..1a12457 100644 --- a/CopiumEngine/src/copium/core/Device.cpp +++ b/CopiumEngine/src/copium/core/Device.cpp @@ -111,7 +111,7 @@ namespace Copium { VkPhysicalDeviceProperties deviceProperties; vkGetPhysicalDeviceProperties(device, &deviceProperties); - CP_INFO_CONT("\t%s", deviceProperties.deviceName); + CP_INFO_CONT("\t{}", deviceProperties.deviceName); devicePriorities.emplace_back(device, GetPhysicalDevicePriority(device)); } @@ -125,7 +125,7 @@ namespace Copium VkPhysicalDeviceProperties deviceProperties; vkGetPhysicalDeviceProperties(it->first, &deviceProperties); physicalDevice = it->first; - CP_INFO("Selecting device: %s", deviceProperties.deviceName); + CP_INFO("Selecting device: {}", deviceProperties.deviceName); } void Device::InitializeLogicalDevice() diff --git a/CopiumEngine/src/copium/core/Instance.cpp b/CopiumEngine/src/copium/core/Instance.cpp index 0f065e5..3335210 100644 --- a/CopiumEngine/src/copium/core/Instance.cpp +++ b/CopiumEngine/src/copium/core/Instance.cpp @@ -1,8 +1,8 @@ -#include "Instance.h" +#include "copium/core/Instance.h" #include -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { @@ -43,7 +43,7 @@ namespace Copium CP_INFO("Supported Extensions:"); for (auto&& extension : extensions) { - CP_INFO_CONT("\t%s", extension.extensionName); + CP_INFO_CONT("\t{}", extension.extensionName); } std::vector layers{}; @@ -89,7 +89,7 @@ namespace Copium CP_INFO("Supported Layers:"); for (auto&& availableLayer : availableLayers) { - CP_INFO_CONT("\t%s", availableLayer.layerName); + CP_INFO_CONT("\t{}", availableLayer.layerName); } for (auto&& layer : layers) diff --git a/CopiumEngine/src/copium/core/Instance.h b/CopiumEngine/src/copium/core/Instance.h index c33afe2..6c664c3 100644 --- a/CopiumEngine/src/copium/core/Instance.h +++ b/CopiumEngine/src/copium/core/Instance.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "copium/core/DebugMessenger.h" namespace Copium diff --git a/CopiumEngine/src/copium/core/SwapChain.h b/CopiumEngine/src/copium/core/SwapChain.h index 470bdff..7577acf 100644 --- a/CopiumEngine/src/copium/core/SwapChain.h +++ b/CopiumEngine/src/copium/core/SwapChain.h @@ -3,6 +3,7 @@ #include #include +#include #include #include "copium/buffer/CommandBuffer.h" diff --git a/CopiumEngine/src/copium/core/Vulkan.cpp b/CopiumEngine/src/copium/core/Vulkan.cpp index fc747e0..0c12bd0 100644 --- a/CopiumEngine/src/copium/core/Vulkan.cpp +++ b/CopiumEngine/src/copium/core/Vulkan.cpp @@ -31,7 +31,7 @@ namespace Copium device = std::make_unique(); swapChain = std::make_unique(); imGuiInstance = std::make_unique(); - CP_INFO("Initialized Vulkan in %f seconds", timer.Elapsed()); + CP_INFO("Initialized Vulkan in {} seconds", timer.Elapsed()); timer.Start(); AssetManager::RegisterAssetType("Texture2D"); @@ -48,7 +48,7 @@ namespace Copium "empty_texture2d", std::make_unique(std::vector{255, 0, 255, 255}, 1, 1, SamplerCreator{})}; whiteTexture2D = AssetHandle{ "white_texture2d", std::make_unique(std::vector{255, 255, 255, 255}, 1, 1, SamplerCreator{})}; - CP_INFO("Initialized AssetManager in %f seconds", timer.Elapsed()); + CP_INFO("Initialized AssetManager in {} seconds", timer.Elapsed()); } void Vulkan::Destroy() @@ -108,6 +108,6 @@ namespace Copium void Vulkan::glfw_error_callback(int error, const char* description) { - CP_ABORT("GLFW Error %d: %s\n", error, description); + CP_ABORT("GLFW Error {}: {}\n", error, description); } } diff --git a/CopiumEngine/src/copium/core/Window.cpp b/CopiumEngine/src/copium/core/Window.cpp index b37f61f..5667454 100644 --- a/CopiumEngine/src/copium/core/Window.cpp +++ b/CopiumEngine/src/copium/core/Window.cpp @@ -3,6 +3,7 @@ #include #include "copium/core/Vulkan.h" +#include "copium/event/DropEvent.h" #include "copium/event/EventDispatcher.h" #include "copium/event/Input.h" #include "copium/event/KeyPressEvent.h" @@ -101,7 +102,7 @@ namespace Copium break; } default: - CP_ABORT("Unreachable switch case: %s", ToString(mode).c_str()); + CP_ABORT("Unreachable switch case: {}", mode); } CP_ASSERT(window, "Failed to initialize glfw window"); @@ -113,6 +114,7 @@ namespace Copium glfwSetCursorPosCallback(window, MouseMoveCallback); glfwSetWindowFocusCallback(window, WindowFocusCallback); glfwSetScrollCallback(window, MouseScrollCallback); + glfwSetDropCallback(window, DropCallback); } void Window::InitializeSurface() @@ -192,4 +194,14 @@ namespace Copium { EventDispatcher::QueueEvent(MouseScrollEvent{(int)xoffset, (int)yoffset}); } + + void Window::DropCallback(GLFWwindow* window, int pathCount, const char* paths[]) + { + std::vector filePaths; + for (int i = 0; i < pathCount; i++) + { + filePaths.emplace_back(paths[i]); + } + EventDispatcher::QueueEvent(DropEvent{filePaths}); + } } diff --git a/CopiumEngine/src/copium/core/Window.h b/CopiumEngine/src/copium/core/Window.h index 66bb722..a363510 100644 --- a/CopiumEngine/src/copium/core/Window.h +++ b/CopiumEngine/src/copium/core/Window.h @@ -5,14 +5,14 @@ #include "copium/util/Common.h" #include "copium/util/Enum.h" -#define CP_WINDOW_MODE_ENUMS Fullscreen, BorderlessWindowed, Windowed - -CP_ENUM_CREATOR(Copium, WindowMode, CP_WINDOW_MODE_ENUMS); +#define CP_WINDOW_MODE_ENUMS struct GLFWwindow; namespace Copium { + CP_ENUM_CREATOR(WindowMode, Fullscreen, BorderlessWindowed, Windowed); + class Window final { CP_DELETE_COPY_AND_MOVE_CTOR(Window); @@ -48,5 +48,6 @@ namespace Copium static void MouseMoveCallback(GLFWwindow* window, double xpos, double ypos); static void WindowFocusCallback(GLFWwindow* window, int focused); static void MouseScrollCallback(GLFWwindow* window, double xoffset, double yoffset); + static void DropCallback(GLFWwindow* window, int path_count, const char* paths[]); }; } diff --git a/CopiumEngine/src/copium/ecs/ComponentPool.h b/CopiumEngine/src/copium/ecs/ComponentPool.h index d83e1ea..1bd8b0b 100644 --- a/CopiumEngine/src/copium/ecs/ComponentPool.h +++ b/CopiumEngine/src/copium/ecs/ComponentPool.h @@ -6,7 +6,7 @@ #include "copium/ecs/ComponentPoolBase.h" #include "copium/ecs/Config.h" #include "copium/ecs/EntitySet.h" -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { @@ -68,8 +68,8 @@ namespace Copium return; CP_ASSERT(queueOperationOrder.size() == addQueue.size() + removeQueue.size(), - "queueOperationOrder size=%zu doesn't match the sum of the addQueue size=%zu and removeQueue size=%zu, " - "which is %zu", + "queueOperationOrder size={} doesn't match the sum of the addQueue size={} and removeQueue size={}, " + "which is {}", queueOperationOrder.size(), addQueue.size(), removeQueue.size(), @@ -152,14 +152,14 @@ namespace Copium void CommitAddComponent(int queueIndex) { CP_ASSERT( - queueIndex < addQueue.size(), "queueIndex=%d is greater than the addQueueSize=%d", queueIndex, addQueue.size()); + queueIndex < addQueue.size(), "queueIndex={} is greater than the addQueueSize={}", queueIndex, addQueue.size()); const auto& [entity, component] = addQueue[queueIndex]; // TODO: Debugging errors caused by this assert might be a bit difficult, since there wont be any stacktrace for // where the component was added. Might want to validate this in AddComponent somehow (like looping through // the queued changes and work out if the component already exists) CP_ASSERT(Find(entity) == Size(), - "Component already exists in entity (entity=%u, Component=%s)", + "Component already exists in entity (entity={}, Component={})", entity, typeid(Component).name()); @@ -172,7 +172,7 @@ namespace Copium void CommitRemoveComponent(int queueIndex) { CP_ASSERT(queueIndex < removeQueue.size(), - "queueIndex=%d is greater than the removeQueueSize=%d", + "queueIndex={} is greater than the removeQueueSize={}", queueIndex, removeQueue.size()); @@ -183,7 +183,7 @@ namespace Copium // TODO: Debugging warnings caused by this might be a bit difficult, since there wont be any stacktrace for // where the component was added. Might want to validate this in AddComponent somehow (like looping // through the queued changes and work out if the component already exists) - CP_WARN("Entity did not contain component (entity=%u, Component=%s)", entity, typeid(Component).name()); + CP_WARN("Entity did not contain component (entity={}, Component={})", entity, typeid(Component).name()); return; } diff --git a/CopiumEngine/src/copium/ecs/ECSManager.cpp b/CopiumEngine/src/copium/ecs/ECSManager.cpp index 5e5af79..21f2aca 100644 --- a/CopiumEngine/src/copium/ecs/ECSManager.cpp +++ b/CopiumEngine/src/copium/ecs/ECSManager.cpp @@ -1,6 +1,6 @@ #include "copium/ecs/ECSManager.h" -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { @@ -30,7 +30,7 @@ namespace Copium void ECSManager::UpdateSystems(const Uuid& systemPoolId) { auto it = systemPools.find(systemPoolId); - CP_ASSERT(it != systemPools.end(), "SystemPool doesn't exist with Uuid=%s", systemPoolId.ToString().c_str()); + CP_ASSERT(it != systemPools.end(), "SystemPool doesn't exist with Uuid={}", systemPoolId); it->second->CommitSignals(); CommitEntityUpdates(); it->second->CommitUpdates(); @@ -60,7 +60,7 @@ namespace Copium void ECSManager::DestroyEntity(EntityId entity) { auto it = entities.find(entity); - CP_ASSERT(it != entities.end(), "Entity does not exist in ECSManager (entity=%u)", entity); + CP_ASSERT(it != entities.end(), "Entity does not exist in ECSManager (entity={})", entity); if (entity == currentEntityId - 1) { currentEntityId--; diff --git a/CopiumEngine/src/copium/ecs/ECSManager.h b/CopiumEngine/src/copium/ecs/ECSManager.h index 3f3b0c3..6bde12e 100644 --- a/CopiumEngine/src/copium/ecs/ECSManager.h +++ b/CopiumEngine/src/copium/ecs/ECSManager.h @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -12,6 +13,7 @@ #include "copium/ecs/SystemPool.h" #include "copium/util/Common.h" #include "copium/util/GenericType.h" +#include "copium/util/Trace.h" #include "copium/util/Uuid.h" namespace Copium @@ -50,7 +52,7 @@ namespace Copium void RemoveSystem(const Uuid& systemPoolId) { auto it = systemPools.find(systemPoolId); - CP_ASSERT(it != systemPools.end(), "SystemPool doesn't exist with Uuid=%s", systemPoolId.ToString().c_str()); + CP_ASSERT(it != systemPools.end(), "SystemPool doesn't exist with Uuid={}", systemPoolId); it->second->RemoveSystem(typeid(SystemClass)); @@ -146,7 +148,7 @@ namespace Copium auto pool = GetComponentPoolAssure(); Component* component = pool->FindComponent(entity); CP_ASSERT( - component, "Entity did not contain component (entity=%u, Component=%s)", entity, typeid(Component).name()); + component, "Entity did not contain component (entity={}, Component={})", entity, typeid(Component).name()); return *component; } @@ -258,7 +260,7 @@ namespace Copium void AddGlobalData(const Args&... args) { auto it = globalDatas.find(typeid(T)); - CP_ASSERT(!HasGlobalData(), "Global with typeid=%s already exists. Do nothing", typeid(T).name()); + CP_ASSERT(!HasGlobalData(), "Global with typeid={} already exists. Do nothing", typeid(T).name()); globalDatas.emplace(typeid(T), GenericType::Create(args...)); } @@ -267,7 +269,7 @@ namespace Copium T& GetGlobalData() { auto it = globalDatas.find(typeid(T)); - CP_ASSERT(it != globalDatas.end(), "Global with typeid=%s doesn't exist"); + CP_ASSERT(it != globalDatas.end(), "Global with typeid={} doesn't exist"); return it->second.Get(); } @@ -282,7 +284,7 @@ namespace Copium void RemoveGlobalData() { auto it = globalDatas.find(typeid(T)); - CP_ASSERT("Global with typeid=%s doesn't exist. Do nothing", typeid(T).name()); + CP_ASSERT("Global with typeid={} doesn't exist. Do nothing", typeid(T).name()); globalDatas.erase(it); } @@ -299,7 +301,7 @@ namespace Copium { auto it = componentPools.find(GetComponentId()); CP_ASSERT(it != componentPools.end(), - "Component has not been added to an entity (Component=%s)", + "Component has not been added to an entity (Component={})", typeid(Component).name()); return static_cast>*>(it->second); } diff --git a/CopiumEngine/src/copium/ecs/SystemPool.cpp b/CopiumEngine/src/copium/ecs/SystemPool.cpp index 261771f..953dce2 100644 --- a/CopiumEngine/src/copium/ecs/SystemPool.cpp +++ b/CopiumEngine/src/copium/ecs/SystemPool.cpp @@ -41,8 +41,8 @@ namespace Copium return; CP_ASSERT(queueOperationOrder.size() == addQueue.size() + removeQueue.size(), - "queueOperationOrder size=%zu doesn't match the sum of the addQueue size=%zu and removeQueue size=%zu, " - "which is %zu", + "queueOperationOrder size={} doesn't match the sum of the addQueue size={} and removeQueue size={}, " + "which is {}", queueOperationOrder.size(), addQueue.size(), removeQueue.size(), @@ -116,9 +116,9 @@ namespace Copium void SystemPool::MoveSystemAfter(const std::type_index& systemId, const std::type_index& afterSystemId) { auto it1 = systems.find(systemId); - CP_ASSERT(it1 != systems.end(), "System with typeid=%s does not exist in SystemPool", systemId.name()); + CP_ASSERT(it1 != systems.end(), "System with typeid={} does not exist in SystemPool", systemId.name()); auto it2 = systems.find(afterSystemId); - CP_ASSERT(it2 != systems.end(), "System with typeid=%s does not exist in SystemPool", afterSystemId.name()); + CP_ASSERT(it2 != systems.end(), "System with typeid={} does not exist in SystemPool", afterSystemId.name()); auto itSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it1->second); auto itAfterSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it2->second); @@ -128,9 +128,9 @@ namespace Copium void SystemPool::MoveSystemBefore(const std::type_index& systemId, const std::type_index& beforeSystemId) { auto it1 = systems.find(systemId); - CP_ASSERT(it1 != systems.end(), "System with typeid=%s does not exist in SystemPool", systemId.name()); + CP_ASSERT(it1 != systems.end(), "System with typeid={} does not exist in SystemPool", systemId.name()); auto it2 = systems.find(beforeSystemId); - CP_ASSERT(it2 != systems.end(), "System with typeid=%s does not exist in SystemPool", beforeSystemId.name()); + CP_ASSERT(it2 != systems.end(), "System with typeid={} does not exist in SystemPool", beforeSystemId.name()); auto itSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it1->second); auto itBeforeSystemId = std::find(systemOrder.rbegin(), systemOrder.rend(), it2->second); @@ -141,10 +141,10 @@ namespace Copium void SystemPool::CommitAddSystem(int queueIndex) { CP_ASSERT( - queueIndex < addQueue.size(), "queueIndex=%d is greater than the addQueueSize=%d", queueIndex, addQueue.size()); + queueIndex < addQueue.size(), "queueIndex={} is greater than the addQueueSize={}", queueIndex, addQueue.size()); auto& [systemId, system, ordering] = addQueue[queueIndex]; - CP_ASSERT(systems.find(systemId) == systems.end(), "System with typeid=%s already exists in SystemPool"); + CP_ASSERT(systems.find(systemId) == systems.end(), "System with typeid={} already exists in SystemPool"); systems.emplace(systemId, system); systemOrder.emplace_back(system); @@ -154,16 +154,16 @@ namespace Copium void SystemPool::CommitRemoveSystem(int queueIndex) { CP_ASSERT(queueIndex < removeQueue.size(), - "queueIndex=%d is greater than the removeQueueSize=%d", + "queueIndex={} is greater than the removeQueueSize={}", queueIndex, removeQueue.size()); const auto& systemId = removeQueue[queueIndex]; auto it = systems.find(systemId); - CP_ASSERT(it != systems.end(), "System with typeid=%s does not exist in SystemPool", systemId.name()); + CP_ASSERT(it != systems.end(), "System with typeid={} does not exist in SystemPool", systemId.name()); auto itOrder = std::find(systemOrder.begin(), systemOrder.end(), it->second); - CP_ASSERT(itOrder != systemOrder.end(), "System with typeid=%s does not exist in systemOrder", systemId.name()); + CP_ASSERT(itOrder != systemOrder.end(), "System with typeid={} does not exist in systemOrder", systemId.name()); delete it->second; systems.erase(it); diff --git a/CopiumEngine/src/copium/event/DropEvent.cpp b/CopiumEngine/src/copium/event/DropEvent.cpp new file mode 100644 index 0000000..8d58faf --- /dev/null +++ b/CopiumEngine/src/copium/event/DropEvent.cpp @@ -0,0 +1,15 @@ +#include "copium/event/DropEvent.h" + +namespace Copium +{ + DropEvent::DropEvent(const std::vector& filePaths) + : Event{EventType::Drop}, + filePaths{filePaths} + { + } + + const std::vector& DropEvent::GetFilePaths() const + { + return filePaths; + } +} diff --git a/CopiumEngine/src/copium/event/DropEvent.h b/CopiumEngine/src/copium/event/DropEvent.h new file mode 100644 index 0000000..40abc57 --- /dev/null +++ b/CopiumEngine/src/copium/event/DropEvent.h @@ -0,0 +1,17 @@ +#pragma once + +#include "copium/event/Event.h" + +namespace Copium +{ + class DropEvent : public Event + { + private: + std::vector filePaths; + + public: + DropEvent(const std::vector& filePaths); + + const std::vector& GetFilePaths() const; + }; +} diff --git a/CopiumEngine/src/copium/event/EventDispatcher.cpp b/CopiumEngine/src/copium/event/EventDispatcher.cpp index d1b29fe..71544c8 100644 --- a/CopiumEngine/src/copium/event/EventDispatcher.cpp +++ b/CopiumEngine/src/copium/event/EventDispatcher.cpp @@ -1,6 +1,6 @@ #include "copium/event/EventDispatcher.h" -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { @@ -35,7 +35,7 @@ namespace Copium focusedEventHandler = eventHandler; return; default: - CP_ABORT("Unreachable switch case: %s", ToString(result).c_str()); + CP_ABORT("Unreachable switch case: {}", result); } } } diff --git a/CopiumEngine/src/copium/event/EventResult.h b/CopiumEngine/src/copium/event/EventResult.h index a6b6af9..f682a5a 100644 --- a/CopiumEngine/src/copium/event/EventResult.h +++ b/CopiumEngine/src/copium/event/EventResult.h @@ -2,6 +2,7 @@ #include "copium/util/Enum.h" -#define CP_EVENT_RESULT_ENUMS Continue, Handled, Focus - -CP_ENUM_CREATOR(Copium, EventResult, CP_EVENT_RESULT_ENUMS); +namespace Copium +{ + CP_ENUM_CREATOR(EventResult, Continue, Handled, Focus); +} diff --git a/CopiumEngine/src/copium/event/EventType.h b/CopiumEngine/src/copium/event/EventType.h index 9607d4a..dd4792d 100644 --- a/CopiumEngine/src/copium/event/EventType.h +++ b/CopiumEngine/src/copium/event/EventType.h @@ -2,7 +2,17 @@ #include "copium/util/Enum.h" -#define CP_EVENT_TYPE_ENUMS \ - MouseMove, MousePress, MouseRelease, MouseScroll, KeyPress, KeyRelease, WindowResize, WindowFocus, ViewportResize - -CP_ENUM_CREATOR(Copium, EventType, CP_EVENT_TYPE_ENUMS); +namespace Copium +{ + CP_ENUM_CREATOR(EventType, + MouseMove, + MousePress, + MouseRelease, + MouseScroll, + KeyPress, + KeyRelease, + WindowResize, + WindowFocus, + ViewportResize, + Drop); +} diff --git a/CopiumEngine/src/copium/event/Input.cpp b/CopiumEngine/src/copium/event/Input.cpp index 9ba1fab..66acf25 100644 --- a/CopiumEngine/src/copium/event/Input.cpp +++ b/CopiumEngine/src/copium/event/Input.cpp @@ -4,7 +4,7 @@ #include "copium/core/Vulkan.h" #include "copium/event/InputCode.h" -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { @@ -17,49 +17,49 @@ namespace Copium bool Input::IsKeyPressed(int keyCode) { - CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range %d", keyCode); + CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range {}", keyCode); return keyEventList[keyCode] && keyDownList[keyCode]; } bool Input::IsKeyReleased(int keyCode) { - CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range %d", keyCode); + CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range {}", keyCode); return keyEventList[keyCode] && !keyDownList[keyCode]; } bool Input::IsKeyDown(int keyCode) { - CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range %d", keyCode); + CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range {}", keyCode); return keyDownList[keyCode]; } bool Input::IsKeyUp(int keyCode) { - CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range %d", keyCode); + CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range {}", keyCode); return !keyDownList[keyCode]; } bool Input::IsMousePressed(int button) { - CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range %d", button); + CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range {}", button); return mouseEventList[button] && mouseDownList[button]; } bool Input::IsMouseReleased(int button) { - CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range %d", button); + CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range {}", button); return mouseEventList[button] && !mouseDownList[button]; } bool Input::IsMouseDown(int button) { - CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range %d", button); + CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range {}", button); return mouseDownList[button]; } bool Input::IsMouseUp(int button) { - CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range %d", button); + CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range {}", button); return !mouseDownList[button]; } @@ -81,14 +81,14 @@ namespace Copium void Input::OnKey(int keyCode, bool pressed) { - CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range %d", keyCode); + CP_ASSERT(keyCode >= 0 && keyCode < MAX_NUM_KEYS, "KeyCode is out of range {}", keyCode); keyDownList[keyCode] = pressed; keyEventList[keyCode] = true; } void Input::OnMouse(int button, bool pressed) { - CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range %d", button); + CP_ASSERT(button >= 0 && button < MAX_NUM_MOUSE_BUTTONS, "button is out of range {}", button); mouseDownList[button] = pressed; mouseEventList[button] = true; } diff --git a/CopiumEngine/src/copium/pipeline/DescriptorPool.cpp b/CopiumEngine/src/copium/pipeline/DescriptorPool.cpp index 374a25a..f41d9d6 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorPool.cpp +++ b/CopiumEngine/src/copium/pipeline/DescriptorPool.cpp @@ -1,5 +1,6 @@ #include "copium/pipeline/DescriptorPool.h" +#include "copium/core/SwapChain.h" #include "copium/core/Vulkan.h" namespace Copium @@ -43,8 +44,8 @@ namespace Copium std::vector DescriptorPool::AllocateDescriptorSets(VkDescriptorSetLayout descriptorSetLayout) { - std::vector descriptorSets{SwapChain::MAX_FRAMES_IN_FLIGHT}; - std::vector layouts{SwapChain::MAX_FRAMES_IN_FLIGHT, descriptorSetLayout}; + std::vector descriptorSets{(size_t)SwapChain::MAX_FRAMES_IN_FLIGHT}; + std::vector layouts{(size_t)SwapChain::MAX_FRAMES_IN_FLIGHT, descriptorSetLayout}; VkDescriptorSetAllocateInfo allocateInfo{}; allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; allocateInfo.descriptorPool = descriptorPool; diff --git a/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp b/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp index 5a685a9..90eb1b5 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp +++ b/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp @@ -110,7 +110,7 @@ namespace Copium UniformBuffer& DescriptorSet::GetUniformBuffer(const std::string& uniformBuffer) { auto it = uniformBuffers.find(uniformBuffer); - CP_ASSERT(it != uniformBuffers.end(), "UniformBuffer not found = %s", uniformBuffer.c_str()); + CP_ASSERT(it != uniformBuffers.end(), "UniformBuffer not found = {}", uniformBuffer); return *it->second; } diff --git a/CopiumEngine/src/copium/pipeline/DescriptorSet.h b/CopiumEngine/src/copium/pipeline/DescriptorSet.h index 32a5af7..89c5574 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorSet.h +++ b/CopiumEngine/src/copium/pipeline/DescriptorSet.h @@ -2,6 +2,7 @@ #include #include +#include #include #include #include diff --git a/CopiumEngine/src/copium/pipeline/PipelineCreator.cpp b/CopiumEngine/src/copium/pipeline/PipelineCreator.cpp index 7173479..1113d0e 100644 --- a/CopiumEngine/src/copium/pipeline/PipelineCreator.cpp +++ b/CopiumEngine/src/copium/pipeline/PipelineCreator.cpp @@ -1,6 +1,6 @@ #include "copium/pipeline/PipelineCreator.h" -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { diff --git a/CopiumEngine/src/copium/pipeline/Shader.cpp b/CopiumEngine/src/copium/pipeline/Shader.cpp index b6f7af0..3627e76 100644 --- a/CopiumEngine/src/copium/pipeline/Shader.cpp +++ b/CopiumEngine/src/copium/pipeline/Shader.cpp @@ -27,7 +27,7 @@ namespace Copium fragShaderModule = InitializeShaderModule(FileSystem::ReadFile(fragmentInput)); break; default: - CP_ASSERT(false, "Unreachable switch case: %s", ToString(type).c_str()); + CP_ASSERT(false, "Unreachable switch case: {}", type); } shaderStages.resize(2); @@ -85,7 +85,7 @@ namespace Copium { if (FileSystem::DateModified(filename) < FileSystem::DateModified(spvFilename)) { - CP_DEBUG("Loading cached shader file: %s", filename.c_str()); + CP_DEBUG("Loading cached shader file: {}", filename); std::vector data = FileSystem::ReadFile32(spvFilename); return InitializeShaderModule(data.data(), data.size() * sizeof(uint32_t)); } @@ -95,7 +95,7 @@ namespace Copium { CP_WARN("Cached shader file is invalid, recreating it"); } - CP_DEBUG("Compiling shader file: %s", filename.c_str()); + CP_DEBUG("Compiling shader file: {}", filename); shaderc::Compiler compiler; shaderc::CompileOptions options; @@ -105,9 +105,9 @@ namespace Copium shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(glslCode.data(), glslCode.size(), type, filename.c_str(), options); CP_ASSERT(result.GetCompilationStatus() == shaderc_compilation_status_success, - "Failed to compile shader: %s\n%s", - filename.c_str(), - result.GetErrorMessage().c_str()); + "Failed to compile shader: {}\n{}", + filename, + result.GetErrorMessage()); std::vector data{result.cbegin(), result.cend()}; FileSystem::WriteFile(spvFilename, (const char*)data.data(), data.size() * sizeof(uint32_t)); @@ -123,7 +123,7 @@ namespace Copium shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(code.data(), type, "inline_shader_code", options); CP_ASSERT(result.GetCompilationStatus() == shaderc_compilation_status_success, - "Failed to compile inline shader code: %s", + "Failed to compile inline shader code: {}", result.GetErrorMessage()); std::vector data{result.cbegin(), result.cend()}; diff --git a/CopiumEngine/src/copium/pipeline/Shader.h b/CopiumEngine/src/copium/pipeline/Shader.h index f1f4ab3..89f9afc 100644 --- a/CopiumEngine/src/copium/pipeline/Shader.h +++ b/CopiumEngine/src/copium/pipeline/Shader.h @@ -6,11 +6,10 @@ #include "copium/util/Common.h" #include "copium/util/Enum.h" -#define CP_SHADER_READ_TYPE_ENUMS GlslFile, GlslCode, SpvFile, SpvCode -CP_ENUM_CREATOR(Copium, ShaderReadType, CP_SHADER_READ_TYPE_ENUMS); - namespace Copium { + CP_ENUM_CREATOR(ShaderReadType, GlslFile, GlslCode, SpvFile, SpvCode); + class Shader final { CP_DELETE_COPY_AND_MOVE_CTOR(Shader); diff --git a/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp b/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp index dc25077..a5161f7 100644 --- a/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp +++ b/CopiumEngine/src/copium/pipeline/ShaderBinding.cpp @@ -1,6 +1,6 @@ #include "copium/pipeline/ShaderBinding.h" -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { @@ -21,7 +21,7 @@ namespace Copium return offset; offset += GetUniformTypeOffset(uniformElem.first); } - CP_ABORT("Uniform not found=%s", uniform.c_str()); + CP_ABORT("Uniform not found={}", uniform); } uint32_t ShaderBinding::GetUniformSize(const std::string& uniform) const @@ -31,7 +31,7 @@ namespace Copium if (uniformElem.second == uniform) return GetUniformTypeSize(uniformElem.first); } - CP_ABORT("Uniform not found=%s", uniform.c_str()); + CP_ABORT("Uniform not found={}", uniform); } UniformType ShaderBinding::GetUniformType(const std::string& uniform) const @@ -41,7 +41,7 @@ namespace Copium if (uniformElem.second == uniform) return uniformElem.first; } - CP_ABORT("Uniform not found=%s", uniform.c_str()); + CP_ABORT("Uniform not found={}", uniform); } uint32_t ShaderBinding::GetUniformBufferSize() const @@ -79,7 +79,7 @@ namespace Copium case UniformType::Float: return 4; // float default: - CP_ABORT("Unhandled switch case: %s", ToString(type).c_str()); + CP_ABORT("Unhandled switch case: {}", type); } } @@ -102,7 +102,7 @@ namespace Copium case UniformType::Float: return 16; // alignas(16) glm::vec2 default: - CP_ABORT("Unhandled switch case", ToString(type).c_str()); + CP_ABORT("Unhandled switch case", type); } } } diff --git a/CopiumEngine/src/copium/pipeline/ShaderBinding.h b/CopiumEngine/src/copium/pipeline/ShaderBinding.h index 1a2ee8b..9c021e9 100644 --- a/CopiumEngine/src/copium/pipeline/ShaderBinding.h +++ b/CopiumEngine/src/copium/pipeline/ShaderBinding.h @@ -6,16 +6,12 @@ #include "copium/util/Enum.h" -#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 { + CP_ENUM_CREATOR(BindingType, Sampler2D, UniformBuffer); + CP_ENUM_CREATOR(ShaderType, Vertex, Fragment); + CP_ENUM_CREATOR(UniformType, Mat3, Mat4, Vec2, Vec3, Vec4, Float, Int); + struct ShaderBinding { std::string name; diff --git a/CopiumEngine/src/copium/pipeline/ShaderReflector.cpp b/CopiumEngine/src/copium/pipeline/ShaderReflector.cpp index 25698bc..4d220e5 100644 --- a/CopiumEngine/src/copium/pipeline/ShaderReflector.cpp +++ b/CopiumEngine/src/copium/pipeline/ShaderReflector.cpp @@ -3,6 +3,7 @@ #include #include "copium/util/FileSystem.h" +#include "copium/util/Trace.h" namespace Copium { @@ -143,7 +144,7 @@ namespace Copium else if (type == "int") binding.uniforms.emplace_back(UniformType::Int, std::string(name)); else - CP_ABORT("Unsupported uniform type=%s", std::string(type).c_str()); + CP_ABORT("Unsupported uniform type={}", type); ParseWhitespace(str, index); index++; // ";" ParseWhitespace(str, index); @@ -151,4 +152,4 @@ namespace Copium if (index < str.size()) index++; // go past "}" } -} \ No newline at end of file +} diff --git a/CopiumEngine/src/copium/pipeline/VertexDescriptor.cpp b/CopiumEngine/src/copium/pipeline/VertexDescriptor.cpp index e336273..86f24e2 100644 --- a/CopiumEngine/src/copium/pipeline/VertexDescriptor.cpp +++ b/CopiumEngine/src/copium/pipeline/VertexDescriptor.cpp @@ -1,6 +1,6 @@ #include "copium/pipeline/VertexDescriptor.h" -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { diff --git a/CopiumEngine/src/copium/sampler/Font.cpp b/CopiumEngine/src/copium/sampler/Font.cpp index c8d3007..8154ff3 100644 --- a/CopiumEngine/src/copium/sampler/Font.cpp +++ b/CopiumEngine/src/copium/sampler/Font.cpp @@ -15,7 +15,7 @@ namespace Copium CP_ASSERT(ft, "Failed to initialize FreeType"); // TODO: Move to Vulkan singleton class? std::string fontPath = metaFile.GetMetaClass("Font").GetValue("filepath"); msdfgen::FontHandle* font = msdfgen::loadFont(ft, fontPath.c_str()); - CP_ASSERT(font, "Failed to initialize font: %s", fontPath.c_str()); + CP_ASSERT(font, "Failed to initialize font: {}", fontPath); std::vector glyphs; msdf_atlas::FontGeometry fontGeometry(&glyphs); @@ -101,7 +101,7 @@ namespace Copium const Glyph& Font::GetGlyph(char c) const { - CP_ASSERT(glyphs.find(c) != glyphs.end(), "Glyph not found: %c", c); + CP_ASSERT(glyphs.find(c) != glyphs.end(), "Glyph not found: {}", c); return glyphs.find(c)->second; } diff --git a/CopiumEngine/src/copium/sampler/Image.h b/CopiumEngine/src/copium/sampler/Image.h index 00877fc..90d5754 100644 --- a/CopiumEngine/src/copium/sampler/Image.h +++ b/CopiumEngine/src/copium/sampler/Image.h @@ -31,4 +31,4 @@ namespace Copium VkImageTiling tiling, VkFormatFeatureFlags features); }; -} \ No newline at end of file +} diff --git a/CopiumEngine/src/copium/sampler/SamplerCreator.cpp b/CopiumEngine/src/copium/sampler/SamplerCreator.cpp index 7093892..8cc40dc 100644 --- a/CopiumEngine/src/copium/sampler/SamplerCreator.cpp +++ b/CopiumEngine/src/copium/sampler/SamplerCreator.cpp @@ -1,6 +1,6 @@ #include "copium/sampler/SamplerCreator.h" -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { @@ -34,7 +34,7 @@ namespace Copium else if (str == "linear") return VK_FILTER_LINEAR; else - CP_ABORT("Invalid texture filtering: %s", str.c_str()); + CP_ABORT("Invalid texture filtering: {}", str); } VkSamplerAddressMode SamplerCreator::GetAddressModeFromString(const std::string& str) const @@ -44,6 +44,6 @@ namespace Copium else if (str == "repeat") return VK_SAMPLER_ADDRESS_MODE_REPEAT; else - CP_ABORT("Invalid texture address mode: %s", str.c_str()); + CP_ABORT("Invalid texture address mode: {}", str); } } diff --git a/CopiumEngine/src/copium/sampler/Texture2D.cpp b/CopiumEngine/src/copium/sampler/Texture2D.cpp index 1df604b..9535927 100644 --- a/CopiumEngine/src/copium/sampler/Texture2D.cpp +++ b/CopiumEngine/src/copium/sampler/Texture2D.cpp @@ -12,7 +12,7 @@ namespace Copium : Sampler{metaFile.GetMetaClass("Texture2D")} { const std::string& filepath = metaFile.GetMetaClass("Texture2D").GetValue("filepath"); - CP_DEBUG("Loading texture file: %s", filepath.c_str()); + CP_DEBUG("Loading texture file: {}", filepath); InitializeTextureImageFromFile(filepath); } @@ -25,7 +25,7 @@ namespace Copium height{height} { CP_ASSERT(rgbaData.size() == width * height * 4, - "rgbaData has invalid size, should be equal to width * height * 4 (%d) actually is %d", + "rgbaData has invalid size, should be equal to width * height * 4 ({}) actually is {}", width * height * 4, rgbaData.size()); InitializeTextureImageFromData(rgbaData.data(), width, height); diff --git a/CopiumEngine/src/copium/util/Common.h b/CopiumEngine/src/copium/util/Common.h index 62f7cea..6f78d68 100644 --- a/CopiumEngine/src/copium/util/Common.h +++ b/CopiumEngine/src/copium/util/Common.h @@ -1,101 +1,8 @@ #pragma once -#include -#include -#include - -#include "copium/util/RuntimeException.h" -#include "copium/util/VulkanException.h" - -#define CP_TERM_RED "\x1B[31m" -#define CP_TERM_GREEN "\x1B[32m" -#define CP_TERM_YELLOW "\x1B[33m" -#define CP_TERM_GRAY "\x1B[90m" -#define CP_TERM_CLEAR "\033[0m" - -#define CP_DEBUG(...) \ - std::cout << CP_TERM_GRAY << "[DBG] " << __func__ << " : " << Copium::String::Format(__VA_ARGS__) << CP_TERM_CLEAR \ - << std::endl -#define CP_INFO(...) std::cout << "[INF] " << __func__ << " : " << Copium::String::Format(__VA_ARGS__) << std::endl -#define CP_WARN(...) \ - std::cout << CP_TERM_YELLOW << "[WRN] " << __func__ << " : " << Copium::String::Format(__VA_ARGS__) << CP_TERM_CLEAR \ - << std::endl -#define CP_ERR(...) \ - std::cout << CP_TERM_RED << "[ERR] " << __func__ << " : " << Copium::String::Format(__VA_ARGS__) << CP_TERM_CLEAR \ - << std::endl - -// Continue traces, will not print the [XXX] tag before the log -#define CP_DEBUG_CONT(...) \ - std::cout << CP_TERM_GRAY << " " << std::setfill(' ') << std::setw(sizeof(__func__)) << " " \ - << Copium::String::Format(__VA_ARGS__) << CP_TERM_CLEAR << std::endl -#define CP_INFO_CONT(...) \ - std::cout << " " << std::setfill(' ') << std::setw(sizeof(__func__)) << " " \ - << Copium::String::Format(__VA_ARGS__) << std::endl -#define CP_WARN_CONT(...) \ - std::cout << CP_TERM_YELLOW << " " << std::setfill(' ') << std::setw(sizeof(__func__)) << " " \ - << Copium::String::Format(__VA_ARGS__) << CP_TERM_CLEAR << std::endl -#define CP_ERR_CONT(...) \ - std::cout << CP_TERM_RED << " " << std::setfill(' ') << std::setw(sizeof(__func__)) << " " \ - << Copium::String::Format(__VA_ARGS__) << CP_TERM_CLEAR << std::endl - -#define CP_ABORT(...) \ - do \ - { \ - CP_ERR("Aborted at %s:%d", __FILE__, __LINE__); \ - CP_ERR_CONT(__VA_ARGS__); \ - throw Copium::RuntimeException(Copium::String::Format(__VA_ARGS__)); \ - } while (false) -#define CP_ASSERT(Function, ...) \ - do \ - { \ - if (!(Function)) \ - { \ - CP_ERR("Assertion failed at %s:%d", __FILE__, __LINE__); \ - CP_ERR_CONT("%s : %s", #Function, Copium::String::Format(__VA_ARGS__).c_str()); \ - throw Copium::RuntimeException(Copium::String::Format(__VA_ARGS__)); \ - } \ - } while (false) -#define CP_VK_ASSERT(Function, ...) \ - do \ - { \ - if (Function != VK_SUCCESS) \ - { \ - CP_ERR("Assertion failed at %s:%d", __FILE__, __LINE__); \ - CP_ERR_CONT("%s : %s", #Function, Copium::String::Format(__VA_ARGS__).c_str()); \ - throw Copium::VulkanException(Copium::String::Format(__VA_ARGS__)); \ - } \ - } while (false) - -#define CP_UNIMPLEMENTED() CP_WARN("%s is unimplemented", __FUNCTION__) -#define CP_ABORT_UNIMPLEMENTED() CP_ABORT("%s is unimplemented", __FUNCTION__) - #define CP_STATIC_CLASS(ClassName) ClassName() = delete #define CP_DELETE_COPY_AND_MOVE_CTOR(ClassName) \ ClassName(ClassName&&) = delete; \ ClassName(const ClassName&) = delete; \ ClassName& operator=(ClassName&&) = delete; \ ClassName& operator=(const ClassName&) = delete - -namespace Copium -{ - class String - { - CP_STATIC_CLASS(String); - - public: - static std::string Format(const std::string& format) - { - return format; - } - - template - static std::string Format(const std::string& format, Args... args) - { - int size = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; - CP_ASSERT(size > 0, "Error during formatting"); - std::unique_ptr buf(new char[size]); - std::snprintf(buf.get(), size, format.c_str(), args...); - return std::string(buf.get(), buf.get() + size - 1); - } - }; -} diff --git a/CopiumEngine/src/copium/util/Enum.h b/CopiumEngine/src/copium/util/Enum.h index 1069b71..61bfab4 100644 --- a/CopiumEngine/src/copium/util/Enum.h +++ b/CopiumEngine/src/copium/util/Enum.h @@ -6,23 +6,20 @@ #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); \ - } \ +#define CP_ENUM_CREATOR(EnumName, ...) \ + enum class EnumName \ + { \ + __VA_ARGS__ \ + }; \ + static const std::string& ToString(EnumName enumName) \ + { \ + static std::vector enumNames = Copium::EnumPrinter::GetEnumNames(CP_TO_STRING(#__VA_ARGS__)); \ + return enumNames[(int)enumName]; \ + } \ + \ + static std::ostream& operator<<(std::ostream& ostream, EnumName enumName) \ + { \ + return ostream << ToString(enumName); \ } namespace Copium diff --git a/CopiumEngine/src/copium/util/FileSystem.cpp b/CopiumEngine/src/copium/util/FileSystem.cpp index a96a151..9357199 100644 --- a/CopiumEngine/src/copium/util/FileSystem.cpp +++ b/CopiumEngine/src/copium/util/FileSystem.cpp @@ -6,12 +6,14 @@ #include #include +#include "copium/util/Trace.h" + namespace Copium { std::vector FileSystem::ReadFile(const std::string& filename) { std::ifstream file(filename, std::ios::ate | std::ios::binary); - CP_ASSERT(file.is_open(), "Failed to open file: %s", filename.c_str()); + CP_ASSERT(file.is_open(), "Failed to open file: {}", filename); size_t fileSize = (size_t)file.tellg(); std::vector buffer(fileSize); @@ -25,7 +27,7 @@ namespace Copium std::vector FileSystem::ReadFile32(const std::string& filename) { std::ifstream file(filename, std::ios::ate | std::ios::binary); - CP_ASSERT(file.is_open(), "Failed to open file: %s", filename.c_str()); + CP_ASSERT(file.is_open(), "Failed to open file: {}", filename); size_t fileSize = (size_t)file.tellg(); CP_ASSERT(fileSize % 4 == 0, "byte size is not divisible by 4"); @@ -40,7 +42,7 @@ namespace Copium std::string FileSystem::ReadFileStr(const std::string& filename) { std::ifstream file(filename, std::ios::ate | std::ios::binary); - CP_ASSERT(file.is_open(), "Failed to open file: %s", filename.c_str()); + CP_ASSERT(file.is_open(), "Failed to open file: {}", filename); size_t fileSize = (size_t)file.tellg(); std::string buffer; @@ -57,7 +59,7 @@ namespace Copium std::filesystem::path path{filename}; std::filesystem::create_directories(path.parent_path()); std::ofstream file(filename, std::ios::binary); - CP_ASSERT(file.is_open(), "Failed to open file: %s", filename.c_str()); + CP_ASSERT(file.is_open(), "Failed to open file: {}", filename); file.write(data.c_str(), data.size()); } @@ -67,7 +69,7 @@ namespace Copium std::filesystem::path path{filename}; std::filesystem::create_directories(path.parent_path()); std::ofstream file(filename, std::ios::binary); - CP_ASSERT(file.is_open(), "Failed to open file: %s", filename.c_str()); + CP_ASSERT(file.is_open(), "Failed to open file: {}", filename); file.write(data, size); } @@ -81,7 +83,7 @@ namespace Copium int64_t FileSystem::DateModified(const std::string& filename) { struct stat result; - CP_ASSERT(stat(filename.c_str(), &result) == 0, "Cannot stat file %s", filename.c_str()); + CP_ASSERT(stat(filename.c_str(), &result) == 0, "Cannot stat file {}", filename); return (int64_t)result.st_mtime; } } diff --git a/CopiumEngine/src/copium/util/FileSystem.h b/CopiumEngine/src/copium/util/FileSystem.h index f882b81..b478748 100644 --- a/CopiumEngine/src/copium/util/FileSystem.h +++ b/CopiumEngine/src/copium/util/FileSystem.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "copium/util/Common.h" diff --git a/CopiumEngine/src/copium/util/MetaFile.cpp b/CopiumEngine/src/copium/util/MetaFile.cpp index 0a12046..52e5ab1 100644 --- a/CopiumEngine/src/copium/util/MetaFile.cpp +++ b/CopiumEngine/src/copium/util/MetaFile.cpp @@ -2,7 +2,7 @@ #include -#include "copium/util/Common.h" +#include "copium/util/Trace.h" #include "copium/util/StringUtil.h" namespace Copium @@ -23,7 +23,7 @@ namespace Copium const std::string& MetaFileClass::GetValue(const std::string& key) const { auto it = values.find(key); - CP_ASSERT(it != values.end(), "Value does not exist: %s", key.c_str()); + CP_ASSERT(it != values.end(), "Value does not exist: {}", key); return it->second; } @@ -55,7 +55,7 @@ namespace Copium { std::ifstream stream(filepath); - CP_ASSERT(stream.is_open(), "Could not find meta file: %s", filepath.c_str()); + CP_ASSERT(stream.is_open(), "Could not find meta file: {}", filepath); LoadMetaFile(stream); } @@ -77,14 +77,14 @@ namespace Copium MetaFileClass& MetaFile::GetMetaClass(const std::string& className) { auto it = classes.find(className); - CP_ASSERT(it != classes.end(), "class does not exist: %s", className.c_str()); + CP_ASSERT(it != classes.end(), "class does not exist: {}", className); return it->second; } const MetaFileClass& MetaFile::GetMetaClass(const std::string& className) const { auto it = classes.find(className); - CP_ASSERT(it != classes.end(), "class does not exist: ", className.c_str()); + CP_ASSERT(it != classes.end(), "class does not exist: {}", className); return it->second; } @@ -139,7 +139,7 @@ namespace Copium currentClass = StringUtil::Trim(line); currentClass = currentClass.substr(1, currentClass.size() - 2); metaClassIt = classes.find(currentClass); - CP_ASSERT(metaClassIt == classes.end(), "Meta file contains two of the same class: %s", currentClass.c_str()); + CP_ASSERT(metaClassIt == classes.end(), "Meta file contains two of the same class: {}", currentClass); metaClassIt = classes.emplace(currentClass, MetaFileClass{}).first; continue; } @@ -159,11 +159,11 @@ namespace Copium continue; } - CP_ASSERT(metaClassIt != classes.end(), "No meta file header specified: ", filepath.c_str()); + CP_ASSERT(metaClassIt != classes.end(), "No meta file header specified: {}", filepath); auto res = metaClassIt->second.values.emplace(key, value); if (!res.second) { - CP_WARN("Meta file key is defined twice: %s", std::string(key).c_str()); + CP_WARN("Meta file key is defined twice: {}", key); } } } @@ -173,7 +173,7 @@ namespace Copium std::vector metaFiles; std::ifstream stream{file}; - CP_ASSERT(stream, "Failed to read file: %s", file.c_str()); + CP_ASSERT(stream, "Failed to read file: {}", file); MetaFile meta; while (!stream.eof()) diff --git a/CopiumEngine/src/copium/util/Trace.h b/CopiumEngine/src/copium/util/Trace.h new file mode 100644 index 0000000..9f38270 --- /dev/null +++ b/CopiumEngine/src/copium/util/Trace.h @@ -0,0 +1,212 @@ +#pragma once + +#include +#include +#include + +#include "copium/util/Common.h" +#include "copium/util/RuntimeException.h" +#include "copium/util/VulkanException.h" + +#define CP_DEBUG(...) Copium::Trace::Print(std::cout, Copium::TermColor::Gray, "[DBG]", __func__, __VA_ARGS__) +#define CP_INFO(...) Copium::Trace::Print(std::cout, Copium::TermColor::None, "[INF]", __func__, __VA_ARGS__) +#define CP_WARN(...) Copium::Trace::Print(std::cout, Copium::TermColor::Yellow, "[WRN]", __func__, __VA_ARGS__) +#define CP_ERR(...) Copium::Trace::Print(std::cout, Copium::TermColor::Red, "[ERR]", __func__, __VA_ARGS__) + +// Continue traces, will not print the [XXX] tag before the log +#define CP_DEBUG_CONT(...) Copium::Trace::PrintCont(std::cout, Copium::TermColor::Gray, sizeof(__func__), __VA_ARGS__) +#define CP_INFO_CONT(...) Copium::Trace::PrintCont(std::cout, Copium::TermColor::None, sizeof(__func__), __VA_ARGS__) +#define CP_WARN_CONT(...) Copium::Trace::PrintCont(std::cout, Copium::TermColor::Yellow, sizeof(__func__), __VA_ARGS__) +#define CP_ERR_CONT(...) Copium::Trace::PrintCont(std::cout, Copium::TermColor::Red, sizeof(__func__), __VA_ARGS__) + +#define CP_ABORT(...) \ + do \ + { \ + CP_ERR("Aborted at {}:{}", __FILE__, __LINE__); \ + std::stringstream ss{}; \ + Copium::Trace::Print(ss, __VA_ARGS__); \ + CP_ERR_CONT(ss.str()); \ + throw Copium::RuntimeException(ss.str()); \ + } while (false) + +#define CP_ASSERT(Function, ...) \ + do \ + { \ + if (!(Function)) \ + { \ + CP_ERR("Assertion failed at {}:{}", __FILE__, __LINE__); \ + std::stringstream ss{}; \ + Copium::Trace::Print(ss, __VA_ARGS__); \ + CP_ERR_CONT("{} : {}", #Function, ss.str()); \ + throw Copium::RuntimeException(ss.str()); \ + } \ + } while (false) + +#define CP_VK_ASSERT(Function, ...) \ + do \ + { \ + if (Function != VK_SUCCESS) \ + { \ + CP_ERR("Assertion failed at {}:{}", __FILE__, __LINE__); \ + std::stringstream ss{}; \ + Copium::Trace::Print(ss, __VA_ARGS__); \ + CP_ERR_CONT("{} : {}", #Function, ss.str()); \ + throw Copium::VulkanException(ss.str()); \ + } \ + } while (false) + +#define CP_UNIMPLEMENTED() CP_WARN("{} is unimplemented", __func__) +#define CP_ABORT_UNIMPLEMENTED() CP_ABORT("{} is unimplemented", __func__) + +namespace Copium +{ + enum class TermColor + { + Red, + Green, + Yellow, + Blue, + Magenta, + Cyan, + Gray, + Clear, + None, + }; + + class Trace + { + CP_STATIC_CLASS(Trace); + + public: + template + static std::ostream& Print(std::ostream& ostream, const std::string& format, const Args&... args) + { + PrintInternal(ostream, format, args...); + return ostream; + } + + template + static std::ostream& PrintCont( + std::ostream& ostream, TermColor color, int padding, const std::string& format, const Args&... args) + { + ostream << GetColor(color) << " " << std::setfill(' ') << std::setw(padding) << " "; + PrintInternal(ostream, format, args...); + return ostream << GetColor(TermColor::Clear) << std::endl; + } + + template + static std::ostream& Print(std::ostream& ostream, + TermColor color, + const std::string& logLevel, + const char* functionName, + const std::string& format, + const Args&... args) + { + ostream << GetColor(color) << logLevel << " " << functionName << " : "; + PrintInternal(ostream, format, args...); + return ostream << GetColor(TermColor::Clear) << std::endl; + } + + private: + static const char* GetColor(TermColor color) + { + switch (color) + { + case TermColor::Red: + return "\x1B[31m"; + case TermColor::Green: + return "\x1B[32m"; + case TermColor::Yellow: + return "\x1B[33m"; + case TermColor::Blue: + return "\x1B[34m"; + case TermColor::Magenta: + return "\x1B[35m"; + case TermColor::Cyan: + return "\x1B[36m"; + case TermColor::Gray: + return "\x1B[90m"; + case TermColor::Clear: + return "\033[0m"; + case TermColor::None: + return ""; + } + return ""; + } + + static size_t FindFormat(const std::string_view& format) + { + for (int i = 0; i < format.size() - 1; i++) + { + if (format[i] == '{' && format[i + 1] != '{') + { + i++; + } + if (format[i] == '{' && format[i + 1] == '}') + { + return i; + } + } + return std::string::npos; + } + + template + static size_t Print1(std::ostream& ostream, const std::string_view& format, const Arg& arg) + { + auto posFormat = format.find("{}"); + auto posOpenEscape = format.find("{{"); + auto posCloseEscape = format.find("}}"); + if (posFormat < posOpenEscape && posFormat < posCloseEscape) + { + ostream << format.substr(0, posFormat); + ostream << arg; + return posFormat + 2; + } + else if (posOpenEscape < posCloseEscape) + { + ostream << format.substr(0, posOpenEscape) << '{'; + return posOpenEscape + 2 + Print1(ostream, format.substr(posOpenEscape + 2), arg); + } + else if (posCloseEscape < posOpenEscape) + { + ostream << format.substr(0, posCloseEscape) << '}'; + return posCloseEscape + 2 + Print1(ostream, format.substr(posCloseEscape + 2), arg); + } + + ostream << format; + return format.size(); + } + + static size_t PrintInternal(std::ostream& ostream, const std::string& format) + { + auto posFormat = format.find("{}"); + auto posOpenEscape = format.find("{{"); + auto posCloseEscape = format.find("}}"); + if (posFormat < posOpenEscape && posFormat < posCloseEscape) + { + ostream << format.substr(0, posFormat); + return posFormat + 2; + } + else if (posOpenEscape < posCloseEscape) + { + ostream << format.substr(0, posOpenEscape) << '{'; + return posOpenEscape + 2 + PrintInternal(ostream, format.substr(posOpenEscape + 2)); + } + else if (posCloseEscape < posOpenEscape) + { + ostream << format.substr(0, posCloseEscape) << '}'; + return posCloseEscape + 2 + PrintInternal(ostream, format.substr(posCloseEscape + 2)); + } + + ostream << format; + return format.size(); + } + + template + static size_t PrintInternal(std::ostream& ostream, const std::string& format, const Arg1& arg1, const Args&... args) + { + size_t newPos = Print1(ostream, format, arg1); + return PrintInternal(ostream, format.substr(newPos), args...); + } + }; +} diff --git a/CopiumEngine/src/copium/util/Uuid.cpp b/CopiumEngine/src/copium/util/Uuid.cpp index 3cc403e..c860a5d 100644 --- a/CopiumEngine/src/copium/util/Uuid.cpp +++ b/CopiumEngine/src/copium/util/Uuid.cpp @@ -1,6 +1,6 @@ #include "copium/util/Uuid.h" -#include "copium/util/Common.h" +#include "copium/util/Trace.h" namespace Copium { @@ -26,7 +26,7 @@ namespace Copium : msb{0}, lsb{0} { - CP_ASSERT(uuidString.size() == 36, "Invalid Uuid string size: %s", uuidString.c_str()); + CP_ASSERT(uuidString.size() == 36, "Invalid Uuid string size: {}", uuidString); for (int i = 0; i < 18; i++) { if (i == 8 || i == 13) // skip "-" @@ -91,7 +91,7 @@ namespace Copium return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; - CP_ABORT("Invalid char value: %c (%d)", c, (int)c); + CP_ABORT("Invalid char value: {} ({})", c, (int)c); } char Uuid::DecToHex(uint8_t nibble) const @@ -100,6 +100,6 @@ namespace Copium return '0' + nibble; if (nibble >= 10 && nibble <= 15) return 'a' + nibble - 10; - CP_ABORT("Invalid nibble value: %d", (int)nibble); + CP_ABORT("Invalid nibble value: {}", (int)nibble); } }