From 97ba7836442c55b57bcfb98c7ea6ef963c1b3120 Mon Sep 17 00:00:00 2001 From: antopilo Date: Sat, 4 Jan 2025 03:31:27 -0500 Subject: [PATCH] Filebrowser icons done --- Editor/src/Windows/FileSystemUI.cpp | 54 +++++++------- Editor/src/Windows/WelcomeWindow.cpp | 6 +- .../src/Rendering/Textures/TextureManager.cpp | 4 +- .../Vulkan/VulkanImage/VulkanImage.cpp | 70 +++++++++++++++++++ .../Vulkan/VulkanImage/VulkanImage.h | 2 + 5 files changed, 102 insertions(+), 34 deletions(-) diff --git a/Editor/src/Windows/FileSystemUI.cpp b/Editor/src/Windows/FileSystemUI.cpp index db44760a..49ee36cb 100644 --- a/Editor/src/Windows/FileSystemUI.cpp +++ b/Editor/src/Windows/FileSystemUI.cpp @@ -106,7 +106,7 @@ namespace Nuake ImGui::SetCursorPos(prevCursor); - //ImGui::Image((ImTextureID)TextureManager::Get()->GetTexture("Resources/Images/folder_icon.png")->GetID(), ImVec2(100, 100), ImVec2(0, 1), ImVec2(1, 0)); + ImGui::Image((ImTextureID)TextureManager::Get()->GetTexture2("Resources/Images/folder_icon.png")->GetImGuiDescriptorSet(), ImVec2(100, 100)); auto imguiStyle = ImGui::GetStyle(); @@ -344,74 +344,74 @@ namespace Nuake } - Ref textureImage = TextureManager::Get()->GetTexture("Resources/Images/file_icon.png"); + Ref textureImage = TextureManager::Get()->GetTexture2("Resources/Images/file_icon.png"); const auto textureMgr = TextureManager::Get(); const auto fileType = file->GetFileType(); if (fileType == FileType::Material) { - auto image = ThumbnailManager::Get().GetThumbnail(file->GetRelativePath()); - if (image) - { - textureImage = image; - } + //auto image = ThumbnailManager::Get().GetThumbnail(file->GetRelativePath()); + //if (image) + //{ + // textureImage = image; + //} } else if (fileType == FileType::Image) { const std::string path = file->GetAbsolutePath(); - textureImage = textureMgr->GetTexture(path); + textureImage = textureMgr->GetTexture2(path); } else if (fileType == FileType::Project) { - textureImage = textureMgr->GetTexture("Resources/Images/project_icon.png"); + textureImage = textureMgr->GetTexture2("Resources/Images/project_icon.png"); } else if (fileType == FileType::NetScript) { - textureImage = textureMgr->GetTexture("Resources/Images/csharp_icon.png"); + textureImage = textureMgr->GetTexture2("Resources/Images/csharp_icon.png"); } else if (fileType == FileType::Scene) { - textureImage = textureMgr->GetTexture("Resources/Images/scene_icon.png"); + textureImage = textureMgr->GetTexture2("Resources/Images/scene_icon.png"); } else if (fileType == FileType::Script) { - textureImage = textureMgr->GetTexture("Resources/Images/script_file_icon.png"); + textureImage = textureMgr->GetTexture2("Resources/Images/script_file_icon.png"); } else if (fileType == FileType::Audio) { - textureImage = textureMgr->GetTexture("Resources/Images/Audio_file_icon.png"); + textureImage = textureMgr->GetTexture2("Resources/Images/Audio_file_icon.png"); } else if (fileType == FileType::Prefab) { - auto image = ThumbnailManager::Get().GetThumbnail(file->GetRelativePath()); - if (image) - { - textureImage = image; - } + //auto image = ThumbnailManager::Get().GetThumbnail(file->GetRelativePath()); + //if (image) + //{ + // textureImage = image; + //} } else if (fileType == FileType::Mesh) { - auto image = ThumbnailManager::Get().GetThumbnail(file->GetRelativePath()); - if (image) - { - textureImage = image; - } + //auto image = ThumbnailManager::Get().GetThumbnail(file->GetRelativePath()); + //if (image) + //{ + // textureImage = image; + //} } else if (fileType == FileType::Solution) { - textureImage = textureMgr->GetTexture("Resources/Images/sln_icon.png"); + textureImage = textureMgr->GetTexture2("Resources/Images/sln_icon.png"); } else if (fileType == FileType::Map) { - textureImage = textureMgr->GetTexture("Resources/Images/trenchbroom_icon.png"); + textureImage = textureMgr->GetTexture2("Resources/Images/trenchbroom_icon.png"); } else if (fileType == FileType::Env) { - textureImage = textureMgr->GetTexture("Resources/Images/env_file_icon.png"); + textureImage = textureMgr->GetTexture2("Resources/Images/env_file_icon.png"); } ImGui::SetCursorPos(prevCursor); - //ImGui::Image(reinterpret_cast(textureImage->GetID()), ImVec2(100, 100), ImVec2(0, 1), ImVec2(1, 0)); + ImGui::Image(reinterpret_cast(textureImage->GetImGuiDescriptorSet()), ImVec2(100, 100)); ImGui::PopStyleVar(); auto& imguiStyle = ImGui::GetStyle(); diff --git a/Editor/src/Windows/WelcomeWindow.cpp b/Editor/src/Windows/WelcomeWindow.cpp index 7377b6fe..5f714412 100644 --- a/Editor/src/Windows/WelcomeWindow.cpp +++ b/Editor/src/Windows/WelcomeWindow.cpp @@ -42,7 +42,7 @@ namespace Nuake const std::string projectIconPath = Path + "/../icon.png"; if (FileSystem::FileExists(projectIconPath, true)) { - //ProjectIcon = TextureManager::Get()->GetTexture(projectIconPath); + ProjectIcon = TextureManager::Get()->GetTexture2(projectIconPath); } else { @@ -314,10 +314,6 @@ namespace Nuake { ImGui::Image((ImTextureID)project.ProjectIcon->GetImGuiDescriptorSet(), iconSize); } - else - { - //ImGui::Image((void*)Nuake::TextureManager::Get()->GetTexture("Resources/Images/cube.png")->GetID(), iconSize, ImVec2(0, 1), ImVec2(1, 0)); - } ImGui::SameLine(); ImGui::SetCursorPosX(padding.x + iconSize.x); diff --git a/Nuake/src/Rendering/Textures/TextureManager.cpp b/Nuake/src/Rendering/Textures/TextureManager.cpp index 6a67b07b..8a9f02ea 100644 --- a/Nuake/src/Rendering/Textures/TextureManager.cpp +++ b/Nuake/src/Rendering/Textures/TextureManager.cpp @@ -75,8 +75,8 @@ namespace Nuake Ref TextureManager::GetTexture2(const std::string path) { - //if (!IsTextureLoaded2(path)) - //m_Registry2.emplace(path, new VulkanImage(path)); + if (!IsTextureLoaded2(path)) + m_Registry2.emplace(path, new VulkanImage(path)); return m_Registry2.at(path); } diff --git a/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.cpp b/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.cpp index e0a9ed90..2e25e38f 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.cpp +++ b/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.cpp @@ -13,6 +13,76 @@ using namespace Nuake; #include "vk_mem_alloc.h" #include +VulkanImage::VulkanImage(const std::string & path) : + ImGuiDescriptorSetGenerated(false), + Format(ImageFormat::RGBA8) +{ + int channels = 0; + int width = 0; + int height = 0; + void* imageData = stbi_load(path.c_str(), &width, &height, &channels, 4); + + if (!imageData) + { + throw std::runtime_error("Failed to load image data using stb_image!"); + } + + // Use the loaded image data with the existing VulkanImage constructor logic + size_t data_size = width * height * 4; // 4 for RGBA + + Ref uploadBuffer = CreateRef(data_size, BufferUsage::TRANSFER_SRC, MemoryUsage::CPU_TO_GPU); + void* mappedData; + vmaMapMemory(VulkanAllocator::Get().GetAllocator(), uploadBuffer->GetAllocation(), &mappedData); + + std::memcpy(mappedData, imageData, data_size); + + // Free the image data as we no longer need it + stbi_image_free(imageData); + + VkExtent3D vkExtent = { + static_cast(width), + static_cast(height), + 1 // We don't support 3D images yet + }; + + Extent = { width, height, 1 }; + + VkImageUsageFlags drawImageUsages = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; + + VkImageCreateInfo imgCreateInfo = VulkanInit::ImageCreateInfo(static_cast(Format), 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 = VulkanInit::ImageviewCreateInfo(static_cast(Format), Image, VK_IMAGE_ASPECT_COLOR_BIT); + + VK_CALL(vkCreateImageView(VkRenderer::Get().GetDevice(), &imageViewCreateInfo, nullptr, &ImageView)); + + // Transition image and copy data to GPU + 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); + }); +} + VulkanImage::VulkanImage(ImageFormat inFormat, Vector2 inSize, ImageUsage usage) : Format(inFormat), Extent(inSize, 1), diff --git a/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.h b/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.h index 1918adf0..fcc5d08e 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.h +++ b/Nuake/src/Rendering/Vulkan/VulkanImage/VulkanImage.h @@ -5,6 +5,7 @@ #include "volk/volk.h" #include "vk_mem_alloc.h" +#include namespace Nuake { @@ -45,6 +46,7 @@ namespace Nuake VkDescriptorSet ImGuiDescriptorSet; public: + VulkanImage(const std::string& path); VulkanImage(ImageFormat format, Vector2 size, ImageUsage usage = ImageUsage::Default); VulkanImage(void* data, ImageFormat format, Vector2 size); VulkanImage(void* data, size_t size); // This one is for embedded files. Ignore.