From 31f34a8669c75ba5658f6baffd135e3a05b80c09 Mon Sep 17 00:00:00 2001 From: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com> Date: Sat, 26 Oct 2024 11:55:37 +0300 Subject: [PATCH] Add TGA loading support. --- UnleashedRecomp/CMakeLists.txt | 3 ++ UnleashedRecomp/gpu/video.cpp | 54 ++++++++++++++++++++++++++++++++++ UnleashedRecomp/stdafx.cpp | 4 +++ UnleashedRecomp/stdafx.h | 1 + vcpkg.json | 3 +- 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 UnleashedRecomp/stdafx.cpp diff --git a/UnleashedRecomp/CMakeLists.txt b/UnleashedRecomp/CMakeLists.txt index e9d10a6..609f177 100644 --- a/UnleashedRecomp/CMakeLists.txt +++ b/UnleashedRecomp/CMakeLists.txt @@ -58,6 +58,7 @@ set(SWA_CXX_SOURCES "game.cpp" "main.cpp" "misc_impl.cpp" + "stdafx.cpp" ${SWA_KERNEL_CXX_SOURCES} ${SWA_CPU_CXX_SOURCES} @@ -91,6 +92,7 @@ find_package(PkgConfig REQUIRED) pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus) find_package(directx-dxc REQUIRED) find_package(zstd CONFIG REQUIRED) +find_package(Stb REQUIRED) file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/D3D12) add_custom_command(TARGET UnleashedRecomp POST_BUILD @@ -125,6 +127,7 @@ target_link_libraries(UnleashedRecomp PRIVATE target_include_directories(UnleashedRecomp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${SWA_THIRDPARTY_ROOT}/ddspp + ${Stb_INCLUDE_DIR} ) target_precompile_headers(UnleashedRecomp PUBLIC ${SWA_PRECOMPILED_HEADERS}) diff --git a/UnleashedRecomp/gpu/video.cpp b/UnleashedRecomp/gpu/video.cpp index 60d2037..c3f8df3 100644 --- a/UnleashedRecomp/gpu/video.cpp +++ b/UnleashedRecomp/gpu/video.cpp @@ -2712,6 +2712,60 @@ static void MakePictureData(GuestPictureData* pictureData, uint8_t* data, uint32 pictureData->texture = g_memory.MapVirtual(texture); pictureData->type = 0; } + else + { + int width, height; + void* stbImage = stbi_load_from_memory(data, dataSize, &width, &height, nullptr, 4); + + if (stbImage != nullptr) + { + const auto texture = g_userHeap.AllocPhysical(ResourceType::Texture); + texture->textureHolder = g_device->createTexture(RenderTextureDesc::Texture2D(width, height, 1, RenderFormat::R8G8B8A8_UNORM)); + texture->texture = texture->textureHolder.get(); + texture->layout = RenderTextureLayout::COPY_DEST; + + texture->descriptorIndex = g_textureDescriptorAllocator.allocate(); + g_textureDescriptorSet->setTexture(texture->descriptorIndex, texture->texture, RenderTextureLayout::SHADER_READ); + + uint32_t rowPitch = (width * 4 + PITCH_ALIGNMENT - 1) & ~(PITCH_ALIGNMENT - 1); + uint32_t slicePitch = rowPitch * height; + + auto uploadBuffer = g_device->createBuffer(RenderBufferDesc::UploadBuffer(slicePitch)); + uint8_t* mappedMemory = reinterpret_cast(uploadBuffer->map()); + + if (rowPitch == (width * 4)) + { + memcpy(mappedMemory, stbImage, slicePitch); + } + else + { + auto data = reinterpret_cast(stbImage); + + for (size_t i = 0; i < height; i++) + { + memcpy(mappedMemory, data, width * 4); + data += width * 4; + mappedMemory += rowPitch; + } + } + + uploadBuffer->unmap(); + + stbi_image_free(stbImage); + + ExecuteCopyCommandList([&] + { + g_copyCommandList->barriers(RenderBarrierStage::COPY, RenderTextureBarrier(texture->texture, RenderTextureLayout::COPY_DEST)); + + g_copyCommandList->copyTextureRegion( + RenderTextureCopyLocation::Subresource(texture->texture, 0), + RenderTextureCopyLocation::PlacedFootprint(uploadBuffer.get(), RenderFormat::R8G8B8A8_UNORM, width, height, 1, rowPitch / 4, 0)); + }); + + pictureData->texture = g_memory.MapVirtual(texture); + pictureData->type = 0; + } + } } } diff --git a/UnleashedRecomp/stdafx.cpp b/UnleashedRecomp/stdafx.cpp new file mode 100644 index 0000000..7a2e063 --- /dev/null +++ b/UnleashedRecomp/stdafx.cpp @@ -0,0 +1,4 @@ +#define STB_IMAGE_IMPLEMENTATION +#include + +#include "stdafx.h" diff --git a/UnleashedRecomp/stdafx.h b/UnleashedRecomp/stdafx.h index 89d9dfa..170bee9 100644 --- a/UnleashedRecomp/stdafx.h +++ b/UnleashedRecomp/stdafx.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "framework.h" #include "mutex.h" diff --git a/vcpkg.json b/vcpkg.json index 1761d58..985f873 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -13,6 +13,7 @@ "xxhash", "pkgconf", "tomlplusplus", - "zstd" + "zstd", + "stb" ] }