Add TGA loading support.

This commit is contained in:
Skyth 2024-10-26 11:55:37 +03:00
parent 0b3e0009d6
commit 31f34a8669
5 changed files with 64 additions and 1 deletions

View file

@ -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})

View file

@ -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<GuestTexture>(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<uint8_t*>(uploadBuffer->map());
if (rowPitch == (width * 4))
{
memcpy(mappedMemory, stbImage, slicePitch);
}
else
{
auto data = reinterpret_cast<const uint8_t*>(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;
}
}
}
}

View file

@ -0,0 +1,4 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include "stdafx.h"

View file

@ -19,6 +19,7 @@
#include <ppc/ppc_recomp_shared.h>
#include <toml++/toml.hpp>
#include <zstd.h>
#include <stb_image.h>
#include "framework.h"
#include "mutex.h"

View file

@ -13,6 +13,7 @@
"xxhash",
"pkgconf",
"tomlplusplus",
"zstd"
"zstd",
"stb"
]
}