From e8ad4be39d56122937e077141339facf5814d2e3 Mon Sep 17 00:00:00 2001 From: antopilo Date: Sat, 4 Jan 2025 01:54:13 -0500 Subject: [PATCH] Icons are back + Texture can now be created from data --- Editor/src/Misc/InterfaceFonts.cpp | 18 +++++- Editor/src/Windows/EditorInterface.cpp | 2 +- Nuake/Engine.cpp | 2 +- Nuake/src/Core/Maths.h | 2 + .../Vulkan/VulkanImage/VulkanImage.cpp | 64 ++++++++++++++++++- .../Vulkan/VulkanImage/VulkanImage.h | 4 +- 6 files changed, 85 insertions(+), 7 deletions(-) diff --git a/Editor/src/Misc/InterfaceFonts.cpp b/Editor/src/Misc/InterfaceFonts.cpp index dd874ad7..2173fb42 100644 --- a/Editor/src/Misc/InterfaceFonts.cpp +++ b/Editor/src/Misc/InterfaceFonts.cpp @@ -4,19 +4,23 @@ #include #include +#include std::map FontManager::mFonts = std::map(); +Ref fontImage; + void FontManager::LoadFonts() { ImGuiIO& io = ImGui::GetIO(); (void)io; static const ImWchar icons_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 }; - ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true; + ImFontConfig icons_config; + icons_config.MergeMode = true; + icons_config.PixelSnapH = true; using namespace Nuake::StaticResources; mFonts[Normal] = io.Fonts->AddFontFromMemoryTTF(Resources_Fonts_fa_solid_900_ttf, Resources_Fonts_fa_solid_900_ttf_len, 11.f, &icons_config, icons_ranges); - ImFontConfig iconsConfigBold; icons_config.PixelSnapH = true; mFonts[Bold] = io.Fonts->AddFontFromMemoryTTF(Resources_Fonts_FiraMono_Bold_ttf, Resources_Fonts_FiraMono_Bold_ttf_len, 16.0); @@ -30,6 +34,16 @@ void FontManager::LoadFonts() mFonts[SubTitle] = io.Fonts->AddFontFromMemoryTTF(Resources_Fonts_FiraMono_Regular_ttf, Resources_Fonts_FiraMono_Regular_ttf_len, 24.0); ImGui::GetIO().Fonts->AddFontDefault(); mFonts[BigIcon] = io.Fonts->AddFontFromMemoryTTF(Resources_Fonts_fa_solid_900_ttf, Resources_Fonts_fa_solid_900_ttf_len, 42.0f, &icons_config, icons_ranges); + + unsigned char* font_data = nullptr; + int width = 0, height = 0, bytes_per_pixel = 0; + + // Retrieve the texture data as RGBA32 + io.Fonts->GetTexDataAsRGBA32(&font_data, &width, &height, &bytes_per_pixel); + + using namespace Nuake; + fontImage = CreateRef(font_data, ImageFormat::RGBA8, Vector2{width, height}); + io.Fonts->SetTexID((ImTextureID)fontImage->GetImGuiDescriptorSet()); } ImFont* FontManager::GetFont(Fonts font) diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index d13a6ddb..5e2a9291 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -2793,7 +2793,7 @@ namespace Nuake { void EditorInterface::BuildFonts() { ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.5f, 0.5f)); - //FontManager::LoadFonts(); + FontManager::LoadFonts(); } std::string EditorInterface::GetEntityTypeName(const Entity& entity) const diff --git a/Nuake/Engine.cpp b/Nuake/Engine.cpp index df693d45..9472f2af 100644 --- a/Nuake/Engine.cpp +++ b/Nuake/Engine.cpp @@ -195,7 +195,7 @@ namespace Nuake ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - ImGui::ShowDemoWindow(); + //ImGui::ShowDemoWindow(); diff --git a/Nuake/src/Core/Maths.h b/Nuake/src/Core/Maths.h index 0e9c2260..943ed760 100644 --- a/Nuake/src/Core/Maths.h +++ b/Nuake/src/Core/Maths.h @@ -1,4 +1,6 @@ #pragma once +#define GLM_FORCE_DEPTH_ZERO_TO_ONE + #include #include #include diff --git a/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.cpp b/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.cpp index 47872eb3..beaf13af 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.cpp +++ b/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.cpp @@ -60,10 +60,70 @@ VulkanImage::VulkanImage(ImageFormat inFormat, Vector2 inSize, ImageUsage usage) VK_CALL(vkCreateImageView(VkRenderer::Get().GetDevice(), &imageViewCreateInfo, nullptr, &ImageView)); } -VulkanImage::VulkanImage(void* inData, ImageFormat inFormat, Vector2 inSize) +VulkanImage::VulkanImage(void* inData, ImageFormat inFormat, Vector2 inSize) : VulkanImage(inFormat, inSize) { - size_t data_size = inSize.x * inSize.y * 4; + size_t data_size = inSize.x * inSize.y; + if (inFormat != ImageFormat::A8) + { + data_size *= 4; + } + + Ref uploadBuffer = CreateRef(data_size, BufferUsage::TRANSFER_SRC, MemoryUsage::CPU_TO_GPU); + void* mappedData; + vmaMapMemory(VulkanAllocator::Get().GetAllocator(), uploadBuffer->GetAllocation(), &mappedData); + + std::memcpy(mappedData, inData, data_size); + + VkExtent3D vkExtent = + { + static_cast(inSize.x), + static_cast(inSize.y), + 1 // We dont support 3D images yet + }; + + VkImageUsageFlags drawImageUsages{}; + drawImageUsages |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; + drawImageUsages |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; + drawImageUsages |= VK_IMAGE_USAGE_STORAGE_BIT; + drawImageUsages |= VK_IMAGE_USAGE_SAMPLED_BIT; + + VkImageCreateInfo imgCreateInfo = VulkanInit::ImageCreateInfo(static_cast(inFormat), drawImageUsages, vkExtent); + + VmaAllocationCreateInfo imgAllocInfo = {}; + imgAllocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; + imgAllocInfo.requiredFlags = VkMemoryPropertyFlags(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + + vmaCreateImage(VulkanAllocator::Get().GetAllocator(), &imgCreateInfo, &imgAllocInfo, &Image, &Allocation, nullptr); + + VkImageViewCreateInfo imageViewCreateInfo; + imageViewCreateInfo = VulkanInit::ImageviewCreateInfo(static_cast(inFormat), Image, VK_IMAGE_ASPECT_COLOR_BIT); + + VK_CALL(vkCreateImageView(VkRenderer::Get().GetDevice(), &imageViewCreateInfo, nullptr, &ImageView)); + + VkRenderer::Get().ImmediateSubmit([&](VkCommandBuffer cmd) + { + VulkanUtil::TransitionImage(cmd, Image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); + + VkBufferImageCopy copyRegion = {}; + copyRegion.bufferOffset = 0; + copyRegion.bufferRowLength = 0; + copyRegion.bufferImageHeight = 0; + + copyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + copyRegion.imageSubresource.mipLevel = 0; + copyRegion.imageSubresource.baseArrayLayer = 0; + copyRegion.imageSubresource.layerCount = 1; + copyRegion.imageExtent = vkExtent; + + vkCmdCopyBufferToImage(cmd, uploadBuffer->GetBuffer(), Image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, + ©Region); + + VulkanUtil::TransitionImage(cmd, Image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + } + ); + + // TODO: Clean up temporary buffer! we leak memory here } VulkanImage::~VulkanImage() diff --git a/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.h b/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.h index 580acf65..e73cbe51 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.h +++ b/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.h @@ -10,8 +10,10 @@ namespace Nuake { enum class ImageFormat { - RGBA8 = 41, + A8 = 9, + RGBA8 = 37, RGBA16F = 97, + RGBA32F = 109, D32F = 126, };